Index: llvm/lib/Transforms/Scalar/LICM.cpp =================================================================== --- llvm/lib/Transforms/Scalar/LICM.cpp +++ llvm/lib/Transforms/Scalar/LICM.cpp @@ -77,6 +77,16 @@ static cl::opt DisablePromotion("disable-licm-promotion", cl::Hidden, cl::desc("Disable memory promotion in LICM pass")); +static cl::opt + LICMMaxHoist("licm-max-hoist", cl::Hidden, cl::init(-1), + cl::desc("Max number of instructions to hoist " + "(default unlimited = -1)")); +static cl::opt + LICMMaxSink("licm-max-sink", cl::Hidden, cl::init(-1), + cl::desc("Max number of instructions to sink " + "(default unlimited = -1)")); +static int HoistCtr = 0; +static int SinkCtr = 0; static bool inSubLoop(BasicBlock *BB, Loop *CurLoop, LoopInfo *LI); static bool isNotUsedInLoop(const Instruction &I, const Loop *CurLoop, @@ -628,14 +638,18 @@ static bool sink(Instruction &I, const LoopInfo *LI, const DominatorTree *DT, const Loop *CurLoop, AliasSetTracker *CurAST, const LICMSafetyInfo *SafetyInfo) { + if (LICMMaxSink != -1) { + if (SinkCtr >= LICMMaxSink) + return false; + ++SinkCtr; + } + DEBUG(dbgs() << "LICM sinking instruction: " << I << "\n"); - bool Changed = false; if (isa(I)) ++NumMovedLoads; else if (isa(I)) ++NumMovedCalls; ++NumSunk; - Changed = true; #ifndef NDEBUG SmallVector ExitBlocks; @@ -688,14 +702,20 @@ CurAST->deleteValue(&I); I.eraseFromParent(); - return Changed; + return true; } -/// When an instruction is found to only use loop invariant operands that -/// is safe to hoist, this instruction is called to do the dirty work. +/// When an instruction is found to only use loop invariant operands that is +/// safe to hoist, this function is called to do the dirty work. /// static bool hoist(Instruction &I, const DominatorTree *DT, const Loop *CurLoop, const LICMSafetyInfo *SafetyInfo) { + if (LICMMaxHoist != -1) { + if (HoistCtr >= LICMMaxHoist) + return false; + ++HoistCtr; + } + auto *Preheader = CurLoop->getLoopPreheader(); DEBUG(dbgs() << "LICM hoisting to " << Preheader->getName() << ": " << I << "\n");