Skip to content

Commit 86beba9

Browse files
authored
[PredicateInfo] Cache ssa.copy declarations (NFC) (#145020)
This pass creates a lot of ssa.copy intrinsics, typically for a small set of types. Determining the function type, performing intrinsic name mangling and looking up the declaration has noticeable overhead in this case. Improve this by caching the declarations by type. I've made this a separate map from CreatedDeclarations, which only tracks the declarations that were newly inserted (but not pre-existing ones).
1 parent ae8c85c commit 86beba9

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

llvm/include/llvm/Transforms/Utils/PredicateInfo.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ class PredicateInfo {
206206
DenseMap<const Value *, const PredicateBase *> PredicateMap;
207207
// The set of ssa_copy declarations we created with our custom mangling.
208208
SmallSet<AssertingVH<Function>, 20> CreatedDeclarations;
209+
// Cache of ssa.copy declaration for a given type.
210+
SmallDenseMap<Type *, Function *> DeclarationCache;
209211
};
210212

211213
/// Printer pass for \c PredicateInfo.

llvm/lib/Transforms/Utils/PredicateInfo.cpp

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -506,25 +506,33 @@ Value *PredicateInfoBuilder::materializeStack(unsigned int &Counter,
506506
ValInfo->RenamedOp = (RenameStack.end() - Start) == RenameStack.begin()
507507
? OrigOp
508508
: (RenameStack.end() - Start - 1)->Def;
509+
auto CreateSSACopy = [this](IRBuilderBase &B, Value *Op,
510+
const Twine &Name = "") {
511+
auto It = PI.DeclarationCache.try_emplace(Op->getType());
512+
if (It.second) {
513+
// The number of named values is used to detect if a new declaration
514+
// was added. If so, that declaration is tracked so that it can be
515+
// removed when the analysis is done. The corner case were a new
516+
// declaration results in a name clash and the old name being renamed
517+
// is not considered as that represents an invalid module.
518+
auto NumDecls = F.getParent()->getNumNamedValues();
519+
Function *IF = Intrinsic::getOrInsertDeclaration(
520+
F.getParent(), Intrinsic::ssa_copy, Op->getType());
521+
if (NumDecls != F.getParent()->getNumNamedValues())
522+
PI.CreatedDeclarations.insert(IF);
523+
It.first->second = IF;
524+
}
525+
return B.CreateCall(It.first->second, Op, Name);
526+
};
509527
// For edge predicates, we can just place the operand in the block before
510528
// the terminator. For assume, we have to place it right after the assume
511529
// to ensure we dominate all uses except assume itself. Always insert
512530
// right before the terminator or after the assume, so that we insert in
513531
// proper order in the case of multiple predicateinfo in the same block.
514-
// The number of named values is used to detect if a new declaration was
515-
// added. If so, that declaration is tracked so that it can be removed when
516-
// the analysis is done. The corner case were a new declaration results in
517-
// a name clash and the old name being renamed is not considered as that
518-
// represents an invalid module.
519532
if (isa<PredicateWithEdge>(ValInfo)) {
520533
IRBuilder<> B(getBranchTerminator(ValInfo));
521-
auto NumDecls = F.getParent()->getNumNamedValues();
522-
Function *IF = Intrinsic::getOrInsertDeclaration(
523-
F.getParent(), Intrinsic::ssa_copy, Op->getType());
524-
if (NumDecls != F.getParent()->getNumNamedValues())
525-
PI.CreatedDeclarations.insert(IF);
526534
CallInst *PIC =
527-
B.CreateCall(IF, Op, Op->getName() + "." + Twine(Counter++));
535+
CreateSSACopy(B, Op, Op->getName() + "." + Twine(Counter++));
528536
PI.PredicateMap.insert({PIC, ValInfo});
529537
Result.Def = PIC;
530538
} else {
@@ -534,12 +542,7 @@ Value *PredicateInfoBuilder::materializeStack(unsigned int &Counter,
534542
// Insert the predicate directly after the assume. While it also holds
535543
// directly before it, assume(i1 true) is not a useful fact.
536544
IRBuilder<> B(PAssume->AssumeInst->getNextNode());
537-
auto NumDecls = F.getParent()->getNumNamedValues();
538-
Function *IF = Intrinsic::getOrInsertDeclaration(
539-
F.getParent(), Intrinsic::ssa_copy, Op->getType());
540-
if (NumDecls != F.getParent()->getNumNamedValues())
541-
PI.CreatedDeclarations.insert(IF);
542-
CallInst *PIC = B.CreateCall(IF, Op);
545+
CallInst *PIC = CreateSSACopy(B, Op);
543546
PI.PredicateMap.insert({PIC, ValInfo});
544547
Result.Def = PIC;
545548
}

0 commit comments

Comments
 (0)