[llvm] Value-initialize values with *Map::try_emplace (NFC) (#141522)

try_emplace value-initializes values, so we do not need to pass
nullptr to try_emplace when the value types are raw pointers or
std::unique_ptr<T>.
This commit is contained in:
Kazu Hirata
2025-05-26 15:13:02 -07:00
committed by GitHub
parent a0c33e535b
commit 89308de4b0
18 changed files with 26 additions and 31 deletions

View File

@@ -101,12 +101,12 @@ protected:
/// Ensure that a label will be emitted before MI.
void requestLabelBeforeInsn(const MachineInstr *MI) {
LabelsBeforeInsn.insert(std::make_pair(MI, nullptr));
LabelsBeforeInsn.try_emplace(MI);
}
/// Ensure that a label will be emitted after MI.
void requestLabelAfterInsn(const MachineInstr *MI) {
LabelsAfterInsn.insert(std::make_pair(MI, nullptr));
LabelsAfterInsn.try_emplace(MI);
}
virtual void beginFunctionImpl(const MachineFunction *MF) = 0;

View File

@@ -253,7 +253,7 @@ public:
Type *getType(llvm::Type *LLVMTy) {
if (LLVMTy == nullptr)
return nullptr;
auto Pair = LLVMTypeToTypeMap.insert({LLVMTy, nullptr});
auto Pair = LLVMTypeToTypeMap.try_emplace(LLVMTy);
auto It = Pair.first;
if (Pair.second)
It->second = std::unique_ptr<Type, TypeDeleter>(new Type(LLVMTy, *this));

View File

@@ -305,13 +305,13 @@ public:
uint32_t Index = BBInfos.size();
auto Iter = BBInfos.end();
bool Inserted;
std::tie(Iter, Inserted) = BBInfos.insert(std::make_pair(Src, nullptr));
std::tie(Iter, Inserted) = BBInfos.try_emplace(Src);
if (Inserted) {
// Newly inserted, update the real info.
Iter->second = std::make_unique<BBInfo>(Index);
Index++;
}
std::tie(Iter, Inserted) = BBInfos.insert(std::make_pair(Dest, nullptr));
std::tie(Iter, Inserted) = BBInfos.try_emplace(Dest);
if (Inserted)
// Newly inserted, update the real info.
Iter->second = std::make_unique<BBInfo>(Index);

View File

@@ -216,7 +216,7 @@ bool EarliestEscapeAnalysis::isNotCapturedBefore(const Value *Object,
if (!isIdentifiedFunctionLocal(Object))
return false;
auto Iter = EarliestEscapes.insert({Object, nullptr});
auto Iter = EarliestEscapes.try_emplace(Object);
if (Iter.second) {
Instruction *EarliestCapture = FindEarliestCapture(
Object, *const_cast<Function *>(DT.getRoot()->getParent()),

View File

@@ -2979,7 +2979,7 @@ void LoopAccessInfo::print(raw_ostream &OS, unsigned Depth) const {
}
const LoopAccessInfo &LoopAccessInfoManager::getInfo(Loop &L) {
const auto &[It, Inserted] = LoopAccessInfoMap.insert({&L, nullptr});
const auto &[It, Inserted] = LoopAccessInfoMap.try_emplace(&L);
if (Inserted)
It->second =

View File

@@ -1277,7 +1277,7 @@ MemorySSA::~MemorySSA() {
}
MemorySSA::AccessList *MemorySSA::getOrCreateAccessList(const BasicBlock *BB) {
auto Res = PerBlockAccesses.insert(std::make_pair(BB, nullptr));
auto Res = PerBlockAccesses.try_emplace(BB);
if (Res.second)
Res.first->second = std::make_unique<AccessList>();
@@ -1285,7 +1285,7 @@ MemorySSA::AccessList *MemorySSA::getOrCreateAccessList(const BasicBlock *BB) {
}
MemorySSA::DefsList *MemorySSA::getOrCreateDefsList(const BasicBlock *BB) {
auto Res = PerBlockDefs.insert(std::make_pair(BB, nullptr));
auto Res = PerBlockDefs.try_emplace(BB);
if (Res.second)
Res.first->second = std::make_unique<DefsList>();

View File

@@ -4470,7 +4470,7 @@ GCMetadataPrinter *AsmPrinter::getOrCreateGCPrinter(GCStrategy &S) {
if (!S.usesMetadata())
return nullptr;
auto [GCPI, Inserted] = GCMetadataPrinters.insert({&S, nullptr});
auto [GCPI, Inserted] = GCMetadataPrinters.try_emplace(&S);
if (!Inserted)
return GCPI->second.get();

View File

@@ -326,7 +326,7 @@ PerFunctionMIParsingState::PerFunctionMIParsingState(MachineFunction &MF,
}
VRegInfo &PerFunctionMIParsingState::getVRegInfo(Register Num) {
auto I = VRegInfos.insert(std::make_pair(Num, nullptr));
auto I = VRegInfos.try_emplace(Num);
if (I.second) {
MachineRegisterInfo &MRI = MF.getRegInfo();
VRegInfo *Info = new (Allocator) VRegInfo;
@@ -339,7 +339,7 @@ VRegInfo &PerFunctionMIParsingState::getVRegInfo(Register Num) {
VRegInfo &PerFunctionMIParsingState::getVRegInfoNamed(StringRef RegName) {
assert(RegName != "" && "Expected named reg.");
auto I = VRegInfosNamed.insert(std::make_pair(RegName.str(), nullptr));
auto I = VRegInfosNamed.try_emplace(RegName.str());
if (I.second) {
VRegInfo *Info = new (Allocator) VRegInfo;
Info->VReg = MF.getRegInfo().createIncompleteVirtualRegister(RegName);

View File

@@ -2876,9 +2876,7 @@ Constant *ConstantDataSequential::getImpl(StringRef Elements, Type *Ty) {
// Do a lookup to see if we have already formed one of these.
auto &Slot =
*Ty->getContext()
.pImpl->CDSConstants.insert(std::make_pair(Elements, nullptr))
.first;
*Ty->getContext().pImpl->CDSConstants.try_emplace(Elements).first;
// The bucket can point to a linked list of different CDS's that have the same
// body but different types. For example, 0,0,0,1 could be a 4 element array

View File

@@ -965,8 +965,7 @@ ELFFile<ELFT>::getSectionAndRelocations(
continue;
}
if (*DoesSectionMatch) {
if (SecToRelocMap.insert(std::make_pair(&Sec, (const Elf_Shdr *)nullptr))
.second)
if (SecToRelocMap.try_emplace(&Sec).second)
continue;
}

View File

@@ -54,7 +54,7 @@ Value *Context::registerValue(std::unique_ptr<Value> &&VPtr) {
}
Value *Context::getOrCreateValueInternal(llvm::Value *LLVMV, llvm::User *U) {
auto Pair = LLVMValueToValueMap.insert({LLVMV, nullptr});
auto Pair = LLVMValueToValueMap.try_emplace(LLVMV);
auto It = Pair.first;
if (!Pair.second)
return It->second.get();
@@ -432,7 +432,7 @@ Value *Context::getOrCreateValueInternal(llvm::Value *LLVMV, llvm::User *U) {
}
Argument *Context::getOrCreateArgument(llvm::Argument *LLVMArg) {
auto Pair = LLVMValueToValueMap.insert({LLVMArg, nullptr});
auto Pair = LLVMValueToValueMap.try_emplace(LLVMArg);
auto It = Pair.first;
if (Pair.second) {
It->second = std::unique_ptr<Argument>(new Argument(LLVMArg, *this));
@@ -652,7 +652,7 @@ Module *Context::getModule(llvm::Module *LLVMM) const {
}
Module *Context::getOrCreateModule(llvm::Module *LLVMM) {
auto Pair = LLVMModuleToModuleMap.insert({LLVMM, nullptr});
auto Pair = LLVMModuleToModuleMap.try_emplace(LLVMM);
auto It = Pair.first;
if (!Pair.second)
return It->second.get();

View File

@@ -270,7 +270,7 @@ bool FixFunctionBitcasts::runOnModule(Module &M) {
Function *F = UseFunc.second;
FunctionType *Ty = CB->getFunctionType();
auto Pair = Wrappers.insert(std::make_pair(std::make_pair(F, Ty), nullptr));
auto Pair = Wrappers.try_emplace(std::make_pair(F, Ty));
if (Pair.second)
Pair.first->second = createWrapper(F, Ty);

View File

@@ -179,7 +179,7 @@ GlobalRecord *RecordsSlice::addGlobal(StringRef Name, RecordLinkage Linkage,
Flags |= SymbolFlags::Data;
Name = copyString(Name);
auto Result = Globals.insert({Name, nullptr});
auto Result = Globals.try_emplace(Name);
if (Result.second)
Result.first->second =
std::make_unique<GlobalRecord>(Name, Linkage, Flags, GV, Inlined);
@@ -194,7 +194,7 @@ ObjCInterfaceRecord *RecordsSlice::addObjCInterface(StringRef Name,
RecordLinkage Linkage,
ObjCIFSymbolKind SymType) {
Name = copyString(Name);
auto Result = Classes.insert({Name, nullptr});
auto Result = Classes.try_emplace(Name);
if (Result.second)
Result.first->second =
std::make_unique<ObjCInterfaceRecord>(Name, Linkage, SymType);
@@ -228,8 +228,7 @@ ObjCCategoryRecord *RecordsSlice::addObjCCategory(StringRef ClassToExtend,
ClassToExtend = copyString(ClassToExtend);
// Add owning record first into record slice.
auto Result =
Categories.insert({std::make_pair(ClassToExtend, Category), nullptr});
auto Result = Categories.try_emplace(std::make_pair(ClassToExtend, Category));
if (Result.second)
Result.first->second =
std::make_unique<ObjCCategoryRecord>(ClassToExtend, Category);

View File

@@ -1115,7 +1115,7 @@ Instruction *InstCombinerImpl::foldAggregateConstructionIntoAggregateReuse(
bool FoundSrcAgg = false;
for (BasicBlock *Pred : Preds) {
std::pair<decltype(SourceAggregates)::iterator, bool> IV =
SourceAggregates.insert({Pred, nullptr});
SourceAggregates.try_emplace(Pred);
// Did we already evaluate this predecessor?
if (!IV.second)
continue;

View File

@@ -5880,7 +5880,7 @@ void LSRInstance::RewriteForPHI(PHINode *PN, const LSRUse &LU,
}
std::pair<DenseMap<BasicBlock *, Value *>::iterator, bool> Pair =
Inserted.insert(std::make_pair(BB, static_cast<Value *>(nullptr)));
Inserted.try_emplace(BB);
if (!Pair.second)
PN->setIncomingValue(i, Pair.first->second);
else {

View File

@@ -2383,7 +2383,7 @@ void NewGVN::performCongruenceFinding(Instruction *I, const Expression *E) {
EClass = TOPClass;
}
if (!EClass) {
auto lookupResult = ExpressionToClass.insert({E, nullptr});
auto lookupResult = ExpressionToClass.try_emplace(E);
// If it's not in the value table, create a new congruence class.
if (lookupResult.second) {

View File

@@ -434,7 +434,7 @@ static const Loop *PickMostRelevantLoop(const Loop *A, const Loop *B,
/// expression, according to PickMostRelevantLoop.
const Loop *SCEVExpander::getRelevantLoop(const SCEV *S) {
// Test whether we've already computed the most relevant loop for this SCEV.
auto Pair = RelevantLoops.insert(std::make_pair(S, nullptr));
auto Pair = RelevantLoops.try_emplace(S);
if (!Pair.second)
return Pair.first->second;

View File

@@ -1820,8 +1820,7 @@ void VPlanTransforms::truncateToMinimalBitwidths(
if (OpSizeInBits == NewResSizeInBits)
continue;
assert(OpSizeInBits > NewResSizeInBits && "nothing to truncate");
auto [ProcessedIter, IterIsEmpty] =
ProcessedTruncs.insert({Op, nullptr});
auto [ProcessedIter, IterIsEmpty] = ProcessedTruncs.try_emplace(Op);
VPWidenCastRecipe *NewOp =
IterIsEmpty
? new VPWidenCastRecipe(Instruction::Trunc, Op, NewResTy)