@@ -84,6 +84,8 @@ class LLVM_LIBRARY_VISIBILITY InstCombiner {
8484 // combining and will be updated to reflect any changes.
8585 LoopInfo *LI;
8686
87+ ReversePostOrderTraversal<BasicBlock *> &RPOT;
88+
8789 bool MadeIRChange = false ;
8890
8991 // / Edges that are known to never be taken.
@@ -92,18 +94,25 @@ class LLVM_LIBRARY_VISIBILITY InstCombiner {
9294 // / Order of predecessors to canonicalize phi nodes towards.
9395 SmallDenseMap<BasicBlock *, SmallVector<BasicBlock *>, 8 > PredOrder;
9496
97+ // / Backedges, used to avoid pushing instructions across backedges in cases
98+ // / where this may result in infinite combine loops. For irreducible loops
99+ // / this picks an arbitrary backedge.
100+ SmallDenseSet<std::pair<const BasicBlock *, const BasicBlock *>, 8 > BackEdges;
101+ bool ComputedBackEdges = false ;
102+
95103public:
96104 InstCombiner (InstructionWorklist &Worklist, BuilderTy &Builder,
97105 bool MinimizeSize, AAResults *AA, AssumptionCache &AC,
98106 TargetLibraryInfo &TLI, TargetTransformInfo &TTI,
99107 DominatorTree &DT, OptimizationRemarkEmitter &ORE,
100108 BlockFrequencyInfo *BFI, BranchProbabilityInfo *BPI,
101- ProfileSummaryInfo *PSI, const DataLayout &DL, LoopInfo *LI)
109+ ProfileSummaryInfo *PSI, const DataLayout &DL, LoopInfo *LI,
110+ ReversePostOrderTraversal<BasicBlock *> &RPOT)
102111 : TTI(TTI), Builder(Builder), Worklist(Worklist),
103112 MinimizeSize (MinimizeSize), AA(AA), AC(AC), TLI(TLI), DT(DT), DL(DL),
104113 SQ(DL, &TLI, &DT, &AC, nullptr , /* UseInstrInfo*/ true ,
105114 /* CanUseUndef*/ true , &DC),
106- ORE(ORE), BFI(BFI), BPI(BPI), PSI(PSI), LI(LI) {}
115+ ORE(ORE), BFI(BFI), BPI(BPI), PSI(PSI), LI(LI), RPOT(RPOT) {}
107116
108117 virtual ~InstCombiner () = default ;
109118
@@ -359,6 +368,13 @@ class LLVM_LIBRARY_VISIBILITY InstCombiner {
359368 std::function<void (Instruction *, unsigned , APInt, APInt &)>
360369 SimplifyAndSetOp);
361370
371+ void computeBackEdges ();
372+ bool isBackEdge (const BasicBlock *From, const BasicBlock *To) {
373+ if (!ComputedBackEdges)
374+ computeBackEdges ();
375+ return BackEdges.contains ({From, To});
376+ }
377+
362378 // / Inserts an instruction \p New before instruction \p Old
363379 // /
364380 // / Also adds the new instruction to the worklist and returns \p New so that
0 commit comments