Index: lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp =================================================================== --- lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp +++ lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp @@ -199,7 +199,10 @@ class InductiveRangeCheckElimination : public LoopPass { public: static char ID; - InductiveRangeCheckElimination() : LoopPass(ID) { + InductiveRangeCheckElimination(bool AllowPreLoopInsertion = true, + bool AllowPostLoopInsertion = true) + : LoopPass(ID), AllowPreLoopInsertion(AllowPreLoopInsertion), + AllowPostLoopInsertion(AllowPostLoopInsertion) { initializeInductiveRangeCheckEliminationPass( *PassRegistry::getPassRegistry()); } @@ -210,6 +213,13 @@ } bool runOnLoop(Loop *L, LPPassManager &LPM) override; + +private: + // Whether or not this pass run allows inserting of pre- and postloop. In case + // if either of them is not allowed but required, the whole transform gets + // rejected. + bool AllowPreLoopInsertion; + bool AllowPostLoopInsertion; }; char InductiveRangeCheckElimination::ID = 0; @@ -644,14 +654,21 @@ // for a definition) LoopStructure MainLoopStructure; + // Whether or not we are allowed to insert pre- and postloop constraining. + bool AllowPreLoopInsertion; + bool AllowPostLoopInsertion; + public: LoopConstrainer(Loop &L, LoopInfo &LI, LPPassManager &LPM, const LoopStructure &LS, ScalarEvolution &SE, - DominatorTree &DT, InductiveRangeCheck::Range R) + DominatorTree &DT, InductiveRangeCheck::Range R, + bool AllowPreLoopInsertion, bool AllowPostLoopInsertion) : F(*L.getHeader()->getParent()), Ctx(L.getHeader()->getContext()), SE(SE), DT(DT), LPM(LPM), LI(LI), OriginalLoop(L), LatchTakenCount(nullptr), OriginalPreheader(nullptr), - MainLoopPreheader(nullptr), Range(R), MainLoopStructure(LS) {} + MainLoopPreheader(nullptr), Range(R), MainLoopStructure(LS), + AllowPreLoopInsertion(AllowPreLoopInsertion), + AllowPostLoopInsertion(AllowPostLoopInsertion) {} // Entry point for the algorithm. Returns true on success. bool run(); @@ -1473,6 +1490,19 @@ bool NeedsPostLoop = Increasing ? SR.HighLimit.hasValue() : SR.LowLimit.hasValue(); + // Check if we have prohibited insertion of pre- and postloop explicitly. + if (NeedsPreLoop && !AllowPreLoopInsertion) { + DEBUG(dbgs() << "irce: could not insert a preloop because it was explicitly" + << " prohibited\n"); + return false; + } + + if (NeedsPostLoop && !AllowPostLoopInsertion) { + DEBUG(dbgs() << "irce: could not insert a postloop because it was" + << " explicitly prohibited\n"); + return false; + } + Value *ExitPreLoopAt = nullptr; Value *ExitMainLoopAt = nullptr; const SCEVConstant *MinusOneS = @@ -1763,7 +1793,8 @@ auto &DT = getAnalysis().getDomTree(); LoopConstrainer LC(*L, getAnalysis().getLoopInfo(), LPM, - LS, SE, DT, SafeIterRange.getValue()); + LS, SE, DT, SafeIterRange.getValue(), + AllowPreLoopInsertion, AllowPostLoopInsertion); bool Changed = LC.run(); if (Changed) {