Skip to content

Commit 3fc92ae

Browse files
authored
[InstCombine][NFC] Use custom inserter for metadata (#202206)
Proactively getting metadata for every visited instruction is expensive. Therefore, only store the current instruction and get the metadata only when an instruction is actually inserted.
1 parent 833d241 commit 3fc92ae

3 files changed

Lines changed: 53 additions & 30 deletions

File tree

llvm/include/llvm/Transforms/InstCombine/InstCombiner.h

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,19 @@ class TargetTransformInfo;
4747
/// This class provides both the logic to recursively visit instructions and
4848
/// combine them.
4949
class LLVM_LIBRARY_VISIBILITY InstCombiner {
50+
/// IRBuilder inserter that adds new instructions to the worklist and new
51+
/// assumptions to the AssumptionCache.
52+
class IRBuilderInstCombineInserter final : public IRBuilderDefaultInserter {
53+
InstCombiner ⁣
54+
55+
public:
56+
~IRBuilderInstCombineInserter() override;
57+
IRBuilderInstCombineInserter(InstCombiner &IC) : IC(IC) {}
58+
59+
void InsertHelper(Instruction *I, const Twine &Name,
60+
BasicBlock::iterator InsertPt) const override;
61+
};
62+
5063
/// Only used to call target specific intrinsic combining.
5164
/// It must **NOT** be used for any other purpose, as InstCombine is a
5265
/// target-independent canonicalization transform.
@@ -58,8 +71,8 @@ class LLVM_LIBRARY_VISIBILITY InstCombiner {
5871

5972
/// An IRBuilder that automatically inserts new instructions into the
6073
/// worklist.
61-
using BuilderTy = IRBuilder<TargetFolder, IRBuilderCallbackInserter>;
62-
BuilderTy &Builder;
74+
using BuilderTy = IRBuilder<TargetFolder, IRBuilderInstCombineInserter>;
75+
BuilderTy Builder;
6376

6477
protected:
6578
/// A worklist of the instructions that need to be simplified.
@@ -100,18 +113,24 @@ class LLVM_LIBRARY_VISIBILITY InstCombiner {
100113
SmallDenseSet<std::pair<const BasicBlock *, const BasicBlock *>, 8> BackEdges;
101114
bool ComputedBackEdges = false;
102115

116+
/// Source for annotation metadata, used by the IRBuilder inserter.
117+
Instruction *AnnotationMetadataSource = nullptr;
118+
103119
public:
104-
InstCombiner(InstructionWorklist &Worklist, BuilderTy &Builder, Function &F,
105-
AAResults *AA, AssumptionCache &AC, TargetLibraryInfo &TLI,
120+
InstCombiner(InstructionWorklist &Worklist, Function &F, AAResults *AA,
121+
AssumptionCache &AC, TargetLibraryInfo &TLI,
106122
TargetTransformInfo &TTI, DominatorTree &DT,
107123
OptimizationRemarkEmitter &ORE, BlockFrequencyInfo *BFI,
108124
BranchProbabilityInfo *BPI, ProfileSummaryInfo *PSI,
109125
const DataLayout &DL,
110126
ReversePostOrderTraversal<BasicBlock *> &RPOT)
111-
: TTIForTargetIntrinsicsOnly(TTI), Builder(Builder), Worklist(Worklist),
112-
F(F), MinimizeSize(F.hasMinSize()), AA(AA), AC(AC), TLI(TLI), DT(DT),
113-
DL(DL), SQ(DL, &TLI, &DT, &AC, nullptr, /*UseInstrInfo*/ true,
114-
/*CanUseUndef*/ true, &DC),
127+
: TTIForTargetIntrinsicsOnly(TTI),
128+
Builder(F.getContext(), TargetFolder(DL),
129+
IRBuilderInstCombineInserter(*this)),
130+
Worklist(Worklist), F(F), MinimizeSize(F.hasMinSize()), AA(AA), AC(AC),
131+
TLI(TLI), DT(DT), DL(DL),
132+
SQ(DL, &TLI, &DT, &AC, nullptr, /*UseInstrInfo*/ true,
133+
/*CanUseUndef*/ true, &DC),
115134
ORE(ORE), BFI(BFI), BPI(BPI), PSI(PSI), RPOT(RPOT) {}
116135

117136
virtual ~InstCombiner() = default;

llvm/lib/Transforms/InstCombine/InstCombineInternal.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,15 @@ class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final
7171
: public InstCombiner,
7272
public InstVisitor<InstCombinerImpl, Instruction *> {
7373
public:
74-
InstCombinerImpl(InstructionWorklist &Worklist, BuilderTy &Builder,
75-
Function &F, AAResults *AA, AssumptionCache &AC,
76-
TargetLibraryInfo &TLI, TargetTransformInfo &TTI,
77-
DominatorTree &DT, OptimizationRemarkEmitter &ORE,
78-
BlockFrequencyInfo *BFI, BranchProbabilityInfo *BPI,
79-
ProfileSummaryInfo *PSI, const DataLayout &DL,
74+
InstCombinerImpl(InstructionWorklist &Worklist, Function &F, AAResults *AA,
75+
AssumptionCache &AC, TargetLibraryInfo &TLI,
76+
TargetTransformInfo &TTI, DominatorTree &DT,
77+
OptimizationRemarkEmitter &ORE, BlockFrequencyInfo *BFI,
78+
BranchProbabilityInfo *BPI, ProfileSummaryInfo *PSI,
79+
const DataLayout &DL,
8080
ReversePostOrderTraversal<BasicBlock *> &RPOT)
81-
: InstCombiner(Worklist, Builder, F, AA, AC, TLI, TTI, DT, ORE, BFI, BPI,
82-
PSI, DL, RPOT) {}
81+
: InstCombiner(Worklist, F, AA, AC, TLI, TTI, DT, ORE, BFI, BPI, PSI, DL,
82+
RPOT) {}
8383

8484
~InstCombinerImpl() override = default;
8585

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,19 @@ extern cl::opt<bool> ProfcheckDisableMetadataFixes;
163163
static cl::opt<unsigned> ShouldLowerDbgDeclare("instcombine-lower-dbg-declare",
164164
cl::Hidden, cl::init(true));
165165

166+
InstCombiner::IRBuilderInstCombineInserter::~IRBuilderInstCombineInserter() =
167+
default;
168+
169+
void InstCombiner::IRBuilderInstCombineInserter::InsertHelper(
170+
Instruction *I, const Twine &Name, BasicBlock::iterator InsertPt) const {
171+
IRBuilderDefaultInserter::InsertHelper(I, Name, InsertPt);
172+
IC.Worklist.add(I);
173+
if (auto *Assume = dyn_cast<AssumeInst>(I))
174+
IC.AC.registerAssumption(Assume);
175+
if (IC.AnnotationMetadataSource)
176+
I->copyMetadata(*IC.AnnotationMetadataSource, LLVMContext::MD_annotation);
177+
}
178+
166179
std::optional<Instruction *>
167180
InstCombiner::targetInstCombineIntrinsic(IntrinsicInst &II) {
168181
// Handle target specific intrinsics
@@ -5903,8 +5916,9 @@ bool InstCombinerImpl::run() {
59035916

59045917
// Now that we have an instruction, try combining it to simplify it.
59055918
Builder.SetInsertPoint(I);
5906-
Builder.CollectMetadataToCopy(
5907-
I, {LLVMContext::MD_dbg, LLVMContext::MD_annotation});
5919+
Builder.SetCurrentDebugLocation(I->getDebugLoc());
5920+
// Used by our IRBuilder inserter to copy annotation metadata.
5921+
AnnotationMetadataSource = I;
59085922

59095923
#ifndef NDEBUG
59105924
std::string OrigI;
@@ -6188,16 +6202,6 @@ static bool combineInstructionsOverFunction(
61886202
bool VerifyFixpoint = Opts.VerifyFixpoint &&
61896203
!F.hasFnAttribute("instcombine-no-verify-fixpoint");
61906204

6191-
/// Builder - This is an IRBuilder that automatically inserts new
6192-
/// instructions into the worklist when they are created.
6193-
IRBuilder<TargetFolder, IRBuilderCallbackInserter> Builder(
6194-
F.getContext(), TargetFolder(DL),
6195-
IRBuilderCallbackInserter([&Worklist, &AC](Instruction *I) {
6196-
Worklist.add(I);
6197-
if (auto *Assume = dyn_cast<AssumeInst>(I))
6198-
AC.registerAssumption(Assume);
6199-
}));
6200-
62016205
ReversePostOrderTraversal<BasicBlock *> RPOT(&F.front());
62026206

62036207
// Lower dbg.declare intrinsics otherwise their value may be clobbered
@@ -6221,8 +6225,8 @@ static bool combineInstructionsOverFunction(
62216225
LLVM_DEBUG(dbgs() << "\n\nINSTCOMBINE ITERATION #" << Iteration << " on "
62226226
<< F.getName() << "\n");
62236227

6224-
InstCombinerImpl IC(Worklist, Builder, F, AA, AC, TLI, TTI, DT, ORE, BFI,
6225-
BPI, PSI, DL, RPOT);
6228+
InstCombinerImpl IC(Worklist, F, AA, AC, TLI, TTI, DT, ORE, BFI, BPI, PSI,
6229+
DL, RPOT);
62266230
IC.MaxArraySizeForCombine = MaxArraySize;
62276231
bool MadeChangeInThisIteration = IC.prepareWorklist(F);
62286232
MadeChangeInThisIteration |= IC.run();

0 commit comments

Comments
 (0)