[llvm] 3fc92ae - [InstCombine][NFC] Use custom inserter for metadata (#202206)
via llvm-commits
llvm-commits at lists.llvm.org
Sun Jun 7 13:16:54 PDT 2026
Author: Alexis Engelke
Date: 2026-06-07T22:16:49+02:00
New Revision: 3fc92aeb55d5852dfb32ec0061e2b41c2424856c
URL: https://github.com/llvm/llvm-project/commit/3fc92aeb55d5852dfb32ec0061e2b41c2424856c
DIFF: https://github.com/llvm/llvm-project/commit/3fc92aeb55d5852dfb32ec0061e2b41c2424856c.diff
LOG: [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.
Added:
Modified:
llvm/include/llvm/Transforms/InstCombine/InstCombiner.h
llvm/lib/Transforms/InstCombine/InstCombineInternal.h
llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/Transforms/InstCombine/InstCombiner.h b/llvm/include/llvm/Transforms/InstCombine/InstCombiner.h
index e15b9f70991b6..d59ac3af4c59f 100644
--- a/llvm/include/llvm/Transforms/InstCombine/InstCombiner.h
+++ b/llvm/include/llvm/Transforms/InstCombine/InstCombiner.h
@@ -47,6 +47,19 @@ class TargetTransformInfo;
/// This class provides both the logic to recursively visit instructions and
/// combine them.
class LLVM_LIBRARY_VISIBILITY InstCombiner {
+ /// IRBuilder inserter that adds new instructions to the worklist and new
+ /// assumptions to the AssumptionCache.
+ class IRBuilderInstCombineInserter final : public IRBuilderDefaultInserter {
+ InstCombiner ⁣
+
+ public:
+ ~IRBuilderInstCombineInserter() override;
+ IRBuilderInstCombineInserter(InstCombiner &IC) : IC(IC) {}
+
+ void InsertHelper(Instruction *I, const Twine &Name,
+ BasicBlock::iterator InsertPt) const override;
+ };
+
/// Only used to call target specific intrinsic combining.
/// It must **NOT** be used for any other purpose, as InstCombine is a
/// target-independent canonicalization transform.
@@ -58,8 +71,8 @@ class LLVM_LIBRARY_VISIBILITY InstCombiner {
/// An IRBuilder that automatically inserts new instructions into the
/// worklist.
- using BuilderTy = IRBuilder<TargetFolder, IRBuilderCallbackInserter>;
- BuilderTy &Builder;
+ using BuilderTy = IRBuilder<TargetFolder, IRBuilderInstCombineInserter>;
+ BuilderTy Builder;
protected:
/// A worklist of the instructions that need to be simplified.
@@ -100,18 +113,24 @@ class LLVM_LIBRARY_VISIBILITY InstCombiner {
SmallDenseSet<std::pair<const BasicBlock *, const BasicBlock *>, 8> BackEdges;
bool ComputedBackEdges = false;
+ /// Source for annotation metadata, used by the IRBuilder inserter.
+ Instruction *AnnotationMetadataSource = nullptr;
+
public:
- InstCombiner(InstructionWorklist &Worklist, BuilderTy &Builder, Function &F,
- AAResults *AA, AssumptionCache &AC, TargetLibraryInfo &TLI,
+ InstCombiner(InstructionWorklist &Worklist, Function &F, AAResults *AA,
+ AssumptionCache &AC, TargetLibraryInfo &TLI,
TargetTransformInfo &TTI, DominatorTree &DT,
OptimizationRemarkEmitter &ORE, BlockFrequencyInfo *BFI,
BranchProbabilityInfo *BPI, ProfileSummaryInfo *PSI,
const DataLayout &DL,
ReversePostOrderTraversal<BasicBlock *> &RPOT)
- : TTIForTargetIntrinsicsOnly(TTI), Builder(Builder), Worklist(Worklist),
- F(F), MinimizeSize(F.hasMinSize()), AA(AA), AC(AC), TLI(TLI), DT(DT),
- DL(DL), SQ(DL, &TLI, &DT, &AC, nullptr, /*UseInstrInfo*/ true,
- /*CanUseUndef*/ true, &DC),
+ : TTIForTargetIntrinsicsOnly(TTI),
+ Builder(F.getContext(), TargetFolder(DL),
+ IRBuilderInstCombineInserter(*this)),
+ Worklist(Worklist), F(F), MinimizeSize(F.hasMinSize()), AA(AA), AC(AC),
+ TLI(TLI), DT(DT), DL(DL),
+ SQ(DL, &TLI, &DT, &AC, nullptr, /*UseInstrInfo*/ true,
+ /*CanUseUndef*/ true, &DC),
ORE(ORE), BFI(BFI), BPI(BPI), PSI(PSI), RPOT(RPOT) {}
virtual ~InstCombiner() = default;
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
index 4a09f870ccc33..8b759e701da60 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
+++ b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
@@ -71,15 +71,15 @@ class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final
: public InstCombiner,
public InstVisitor<InstCombinerImpl, Instruction *> {
public:
- InstCombinerImpl(InstructionWorklist &Worklist, BuilderTy &Builder,
- Function &F, AAResults *AA, AssumptionCache &AC,
- TargetLibraryInfo &TLI, TargetTransformInfo &TTI,
- DominatorTree &DT, OptimizationRemarkEmitter &ORE,
- BlockFrequencyInfo *BFI, BranchProbabilityInfo *BPI,
- ProfileSummaryInfo *PSI, const DataLayout &DL,
+ InstCombinerImpl(InstructionWorklist &Worklist, Function &F, AAResults *AA,
+ AssumptionCache &AC, TargetLibraryInfo &TLI,
+ TargetTransformInfo &TTI, DominatorTree &DT,
+ OptimizationRemarkEmitter &ORE, BlockFrequencyInfo *BFI,
+ BranchProbabilityInfo *BPI, ProfileSummaryInfo *PSI,
+ const DataLayout &DL,
ReversePostOrderTraversal<BasicBlock *> &RPOT)
- : InstCombiner(Worklist, Builder, F, AA, AC, TLI, TTI, DT, ORE, BFI, BPI,
- PSI, DL, RPOT) {}
+ : InstCombiner(Worklist, F, AA, AC, TLI, TTI, DT, ORE, BFI, BPI, PSI, DL,
+ RPOT) {}
~InstCombinerImpl() override = default;
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 9182454b74bfb..89bf4bf713421 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -163,6 +163,19 @@ extern cl::opt<bool> ProfcheckDisableMetadataFixes;
static cl::opt<unsigned> ShouldLowerDbgDeclare("instcombine-lower-dbg-declare",
cl::Hidden, cl::init(true));
+InstCombiner::IRBuilderInstCombineInserter::~IRBuilderInstCombineInserter() =
+ default;
+
+void InstCombiner::IRBuilderInstCombineInserter::InsertHelper(
+ Instruction *I, const Twine &Name, BasicBlock::iterator InsertPt) const {
+ IRBuilderDefaultInserter::InsertHelper(I, Name, InsertPt);
+ IC.Worklist.add(I);
+ if (auto *Assume = dyn_cast<AssumeInst>(I))
+ IC.AC.registerAssumption(Assume);
+ if (IC.AnnotationMetadataSource)
+ I->copyMetadata(*IC.AnnotationMetadataSource, LLVMContext::MD_annotation);
+}
+
std::optional<Instruction *>
InstCombiner::targetInstCombineIntrinsic(IntrinsicInst &II) {
// Handle target specific intrinsics
@@ -5903,8 +5916,9 @@ bool InstCombinerImpl::run() {
// Now that we have an instruction, try combining it to simplify it.
Builder.SetInsertPoint(I);
- Builder.CollectMetadataToCopy(
- I, {LLVMContext::MD_dbg, LLVMContext::MD_annotation});
+ Builder.SetCurrentDebugLocation(I->getDebugLoc());
+ // Used by our IRBuilder inserter to copy annotation metadata.
+ AnnotationMetadataSource = I;
#ifndef NDEBUG
std::string OrigI;
@@ -6188,16 +6202,6 @@ static bool combineInstructionsOverFunction(
bool VerifyFixpoint = Opts.VerifyFixpoint &&
!F.hasFnAttribute("instcombine-no-verify-fixpoint");
- /// Builder - This is an IRBuilder that automatically inserts new
- /// instructions into the worklist when they are created.
- IRBuilder<TargetFolder, IRBuilderCallbackInserter> Builder(
- F.getContext(), TargetFolder(DL),
- IRBuilderCallbackInserter([&Worklist, &AC](Instruction *I) {
- Worklist.add(I);
- if (auto *Assume = dyn_cast<AssumeInst>(I))
- AC.registerAssumption(Assume);
- }));
-
ReversePostOrderTraversal<BasicBlock *> RPOT(&F.front());
// Lower dbg.declare intrinsics otherwise their value may be clobbered
@@ -6221,8 +6225,8 @@ static bool combineInstructionsOverFunction(
LLVM_DEBUG(dbgs() << "\n\nINSTCOMBINE ITERATION #" << Iteration << " on "
<< F.getName() << "\n");
- InstCombinerImpl IC(Worklist, Builder, F, AA, AC, TLI, TTI, DT, ORE, BFI,
- BPI, PSI, DL, RPOT);
+ InstCombinerImpl IC(Worklist, F, AA, AC, TLI, TTI, DT, ORE, BFI, BPI, PSI,
+ DL, RPOT);
IC.MaxArraySizeForCombine = MaxArraySize;
bool MadeChangeInThisIteration = IC.prepareWorklist(F);
MadeChangeInThisIteration |= IC.run();
More information about the llvm-commits
mailing list