InductiveRangeCheckElimination.cpp 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997
  1. //===- InductiveRangeCheckElimination.cpp - -------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // The InductiveRangeCheckElimination pass splits a loop's iteration space into
  10. // three disjoint ranges. It does that in a way such that the loop running in
  11. // the middle loop provably does not need range checks. As an example, it will
  12. // convert
  13. //
  14. // len = < known positive >
  15. // for (i = 0; i < n; i++) {
  16. // if (0 <= i && i < len) {
  17. // do_something();
  18. // } else {
  19. // throw_out_of_bounds();
  20. // }
  21. // }
  22. //
  23. // to
  24. //
  25. // len = < known positive >
  26. // limit = smin(n, len)
  27. // // no first segment
  28. // for (i = 0; i < limit; i++) {
  29. // if (0 <= i && i < len) { // this check is fully redundant
  30. // do_something();
  31. // } else {
  32. // throw_out_of_bounds();
  33. // }
  34. // }
  35. // for (i = limit; i < n; i++) {
  36. // if (0 <= i && i < len) {
  37. // do_something();
  38. // } else {
  39. // throw_out_of_bounds();
  40. // }
  41. // }
  42. //
  43. //===----------------------------------------------------------------------===//
  44. #include "llvm/Transforms/Scalar/InductiveRangeCheckElimination.h"
  45. #include "llvm/ADT/APInt.h"
  46. #include "llvm/ADT/ArrayRef.h"
  47. #include "llvm/ADT/None.h"
  48. #include "llvm/ADT/Optional.h"
  49. #include "llvm/ADT/PriorityWorklist.h"
  50. #include "llvm/ADT/SmallPtrSet.h"
  51. #include "llvm/ADT/SmallVector.h"
  52. #include "llvm/ADT/StringRef.h"
  53. #include "llvm/ADT/Twine.h"
  54. #include "llvm/Analysis/BlockFrequencyInfo.h"
  55. #include "llvm/Analysis/BranchProbabilityInfo.h"
  56. #include "llvm/Analysis/LoopAnalysisManager.h"
  57. #include "llvm/Analysis/LoopInfo.h"
  58. #include "llvm/Analysis/LoopPass.h"
  59. #include "llvm/Analysis/PostDominators.h"
  60. #include "llvm/Analysis/ScalarEvolution.h"
  61. #include "llvm/Analysis/ScalarEvolutionExpressions.h"
  62. #include "llvm/IR/BasicBlock.h"
  63. #include "llvm/IR/CFG.h"
  64. #include "llvm/IR/Constants.h"
  65. #include "llvm/IR/DerivedTypes.h"
  66. #include "llvm/IR/Dominators.h"
  67. #include "llvm/IR/Function.h"
  68. #include "llvm/IR/IRBuilder.h"
  69. #include "llvm/IR/InstrTypes.h"
  70. #include "llvm/IR/Instructions.h"
  71. #include "llvm/IR/Metadata.h"
  72. #include "llvm/IR/Module.h"
  73. #include "llvm/IR/PatternMatch.h"
  74. #include "llvm/IR/Type.h"
  75. #include "llvm/IR/Use.h"
  76. #include "llvm/IR/User.h"
  77. #include "llvm/IR/Value.h"
  78. #include "llvm/InitializePasses.h"
  79. #include "llvm/Pass.h"
  80. #include "llvm/Support/BranchProbability.h"
  81. #include "llvm/Support/Casting.h"
  82. #include "llvm/Support/CommandLine.h"
  83. #include "llvm/Support/Compiler.h"
  84. #include "llvm/Support/Debug.h"
  85. #include "llvm/Support/ErrorHandling.h"
  86. #include "llvm/Support/raw_ostream.h"
  87. #include "llvm/Transforms/Scalar.h"
  88. #include "llvm/Transforms/Utils/Cloning.h"
  89. #include "llvm/Transforms/Utils/LoopSimplify.h"
  90. #include "llvm/Transforms/Utils/LoopUtils.h"
  91. #include "llvm/Transforms/Utils/ScalarEvolutionExpander.h"
  92. #include "llvm/Transforms/Utils/ValueMapper.h"
  93. #include <algorithm>
  94. #include <cassert>
  95. #include <iterator>
  96. #include <limits>
  97. #include <utility>
  98. #include <vector>
  99. using namespace llvm;
  100. using namespace llvm::PatternMatch;
  101. static cl::opt<unsigned> LoopSizeCutoff("irce-loop-size-cutoff", cl::Hidden,
  102. cl::init(64));
  103. static cl::opt<bool> PrintChangedLoops("irce-print-changed-loops", cl::Hidden,
  104. cl::init(false));
  105. static cl::opt<bool> PrintRangeChecks("irce-print-range-checks", cl::Hidden,
  106. cl::init(false));
  107. static cl::opt<bool> SkipProfitabilityChecks("irce-skip-profitability-checks",
  108. cl::Hidden, cl::init(false));
  109. static cl::opt<unsigned> MinRuntimeIterations("irce-min-runtime-iterations",
  110. cl::Hidden, cl::init(10));
  111. static cl::opt<bool> AllowUnsignedLatchCondition("irce-allow-unsigned-latch",
  112. cl::Hidden, cl::init(true));
  113. static cl::opt<bool> AllowNarrowLatchCondition(
  114. "irce-allow-narrow-latch", cl::Hidden, cl::init(true),
  115. cl::desc("If set to true, IRCE may eliminate wide range checks in loops "
  116. "with narrow latch condition."));
  117. static const char *ClonedLoopTag = "irce.loop.clone";
  118. #define DEBUG_TYPE "irce"
  119. namespace {
  120. /// An inductive range check is conditional branch in a loop with
  121. ///
  122. /// 1. a very cold successor (i.e. the branch jumps to that successor very
  123. /// rarely)
  124. ///
  125. /// and
  126. ///
  127. /// 2. a condition that is provably true for some contiguous range of values
  128. /// taken by the containing loop's induction variable.
  129. ///
  130. class InductiveRangeCheck {
  131. const SCEV *Begin = nullptr;
  132. const SCEV *Step = nullptr;
  133. const SCEV *End = nullptr;
  134. Use *CheckUse = nullptr;
  135. static bool parseRangeCheckICmp(Loop *L, ICmpInst *ICI, ScalarEvolution &SE,
  136. Value *&Index, Value *&Length,
  137. bool &IsSigned);
  138. static void
  139. extractRangeChecksFromCond(Loop *L, ScalarEvolution &SE, Use &ConditionUse,
  140. SmallVectorImpl<InductiveRangeCheck> &Checks,
  141. SmallPtrSetImpl<Value *> &Visited);
  142. public:
  143. const SCEV *getBegin() const { return Begin; }
  144. const SCEV *getStep() const { return Step; }
  145. const SCEV *getEnd() const { return End; }
  146. void print(raw_ostream &OS) const {
  147. OS << "InductiveRangeCheck:\n";
  148. OS << " Begin: ";
  149. Begin->print(OS);
  150. OS << " Step: ";
  151. Step->print(OS);
  152. OS << " End: ";
  153. End->print(OS);
  154. OS << "\n CheckUse: ";
  155. getCheckUse()->getUser()->print(OS);
  156. OS << " Operand: " << getCheckUse()->getOperandNo() << "\n";
  157. }
  158. LLVM_DUMP_METHOD
  159. void dump() {
  160. print(dbgs());
  161. }
  162. Use *getCheckUse() const { return CheckUse; }
  163. /// Represents an signed integer range [Range.getBegin(), Range.getEnd()). If
  164. /// R.getEnd() le R.getBegin(), then R denotes the empty range.
  165. class Range {
  166. const SCEV *Begin;
  167. const SCEV *End;
  168. public:
  169. Range(const SCEV *Begin, const SCEV *End) : Begin(Begin), End(End) {
  170. assert(Begin->getType() == End->getType() && "ill-typed range!");
  171. }
  172. Type *getType() const { return Begin->getType(); }
  173. const SCEV *getBegin() const { return Begin; }
  174. const SCEV *getEnd() const { return End; }
  175. bool isEmpty(ScalarEvolution &SE, bool IsSigned) const {
  176. if (Begin == End)
  177. return true;
  178. if (IsSigned)
  179. return SE.isKnownPredicate(ICmpInst::ICMP_SGE, Begin, End);
  180. else
  181. return SE.isKnownPredicate(ICmpInst::ICMP_UGE, Begin, End);
  182. }
  183. };
  184. /// This is the value the condition of the branch needs to evaluate to for the
  185. /// branch to take the hot successor (see (1) above).
  186. bool getPassingDirection() { return true; }
  187. /// Computes a range for the induction variable (IndVar) in which the range
  188. /// check is redundant and can be constant-folded away. The induction
  189. /// variable is not required to be the canonical {0,+,1} induction variable.
  190. Optional<Range> computeSafeIterationSpace(ScalarEvolution &SE,
  191. const SCEVAddRecExpr *IndVar,
  192. bool IsLatchSigned) const;
  193. /// Parse out a set of inductive range checks from \p BI and append them to \p
  194. /// Checks.
  195. ///
  196. /// NB! There may be conditions feeding into \p BI that aren't inductive range
  197. /// checks, and hence don't end up in \p Checks.
  198. static void
  199. extractRangeChecksFromBranch(BranchInst *BI, Loop *L, ScalarEvolution &SE,
  200. BranchProbabilityInfo *BPI,
  201. SmallVectorImpl<InductiveRangeCheck> &Checks);
  202. };
  203. struct LoopStructure;
  204. class InductiveRangeCheckElimination {
  205. ScalarEvolution &SE;
  206. BranchProbabilityInfo *BPI;
  207. DominatorTree &DT;
  208. LoopInfo &LI;
  209. using GetBFIFunc =
  210. llvm::Optional<llvm::function_ref<llvm::BlockFrequencyInfo &()> >;
  211. GetBFIFunc GetBFI;
  212. // Returns true if it is profitable to do a transform basing on estimation of
  213. // number of iterations.
  214. bool isProfitableToTransform(const Loop &L, LoopStructure &LS);
  215. public:
  216. InductiveRangeCheckElimination(ScalarEvolution &SE,
  217. BranchProbabilityInfo *BPI, DominatorTree &DT,
  218. LoopInfo &LI, GetBFIFunc GetBFI = None)
  219. : SE(SE), BPI(BPI), DT(DT), LI(LI), GetBFI(GetBFI) {}
  220. bool run(Loop *L, function_ref<void(Loop *, bool)> LPMAddNewLoop);
  221. };
  222. class IRCELegacyPass : public FunctionPass {
  223. public:
  224. static char ID;
  225. IRCELegacyPass() : FunctionPass(ID) {
  226. initializeIRCELegacyPassPass(*PassRegistry::getPassRegistry());
  227. }
  228. void getAnalysisUsage(AnalysisUsage &AU) const override {
  229. AU.addRequired<BranchProbabilityInfoWrapperPass>();
  230. AU.addRequired<DominatorTreeWrapperPass>();
  231. AU.addPreserved<DominatorTreeWrapperPass>();
  232. AU.addRequired<LoopInfoWrapperPass>();
  233. AU.addPreserved<LoopInfoWrapperPass>();
  234. AU.addRequired<ScalarEvolutionWrapperPass>();
  235. AU.addPreserved<ScalarEvolutionWrapperPass>();
  236. }
  237. bool runOnFunction(Function &F) override;
  238. };
  239. } // end anonymous namespace
  240. char IRCELegacyPass::ID = 0;
  241. INITIALIZE_PASS_BEGIN(IRCELegacyPass, "irce",
  242. "Inductive range check elimination", false, false)
  243. INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass)
  244. INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
  245. INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
  246. INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
  247. INITIALIZE_PASS_END(IRCELegacyPass, "irce", "Inductive range check elimination",
  248. false, false)
  249. /// Parse a single ICmp instruction, `ICI`, into a range check. If `ICI` cannot
  250. /// be interpreted as a range check, return false and set `Index` and `Length`
  251. /// to `nullptr`. Otherwise set `Index` to the value being range checked, and
  252. /// set `Length` to the upper limit `Index` is being range checked.
  253. bool
  254. InductiveRangeCheck::parseRangeCheckICmp(Loop *L, ICmpInst *ICI,
  255. ScalarEvolution &SE, Value *&Index,
  256. Value *&Length, bool &IsSigned) {
  257. auto IsLoopInvariant = [&SE, L](Value *V) {
  258. return SE.isLoopInvariant(SE.getSCEV(V), L);
  259. };
  260. ICmpInst::Predicate Pred = ICI->getPredicate();
  261. Value *LHS = ICI->getOperand(0);
  262. Value *RHS = ICI->getOperand(1);
  263. switch (Pred) {
  264. default:
  265. return false;
  266. case ICmpInst::ICMP_SLE:
  267. std::swap(LHS, RHS);
  268. LLVM_FALLTHROUGH;
  269. case ICmpInst::ICMP_SGE:
  270. IsSigned = true;
  271. if (match(RHS, m_ConstantInt<0>())) {
  272. Index = LHS;
  273. return true; // Lower.
  274. }
  275. return false;
  276. case ICmpInst::ICMP_SLT:
  277. std::swap(LHS, RHS);
  278. LLVM_FALLTHROUGH;
  279. case ICmpInst::ICMP_SGT:
  280. IsSigned = true;
  281. if (match(RHS, m_ConstantInt<-1>())) {
  282. Index = LHS;
  283. return true; // Lower.
  284. }
  285. if (IsLoopInvariant(LHS)) {
  286. Index = RHS;
  287. Length = LHS;
  288. return true; // Upper.
  289. }
  290. return false;
  291. case ICmpInst::ICMP_ULT:
  292. std::swap(LHS, RHS);
  293. LLVM_FALLTHROUGH;
  294. case ICmpInst::ICMP_UGT:
  295. IsSigned = false;
  296. if (IsLoopInvariant(LHS)) {
  297. Index = RHS;
  298. Length = LHS;
  299. return true; // Both lower and upper.
  300. }
  301. return false;
  302. }
  303. llvm_unreachable("default clause returns!");
  304. }
  305. void InductiveRangeCheck::extractRangeChecksFromCond(
  306. Loop *L, ScalarEvolution &SE, Use &ConditionUse,
  307. SmallVectorImpl<InductiveRangeCheck> &Checks,
  308. SmallPtrSetImpl<Value *> &Visited) {
  309. Value *Condition = ConditionUse.get();
  310. if (!Visited.insert(Condition).second)
  311. return;
  312. // TODO: Do the same for OR, XOR, NOT etc?
  313. if (match(Condition, m_LogicalAnd(m_Value(), m_Value()))) {
  314. extractRangeChecksFromCond(L, SE, cast<User>(Condition)->getOperandUse(0),
  315. Checks, Visited);
  316. extractRangeChecksFromCond(L, SE, cast<User>(Condition)->getOperandUse(1),
  317. Checks, Visited);
  318. return;
  319. }
  320. ICmpInst *ICI = dyn_cast<ICmpInst>(Condition);
  321. if (!ICI)
  322. return;
  323. Value *Length = nullptr, *Index;
  324. bool IsSigned;
  325. if (!parseRangeCheckICmp(L, ICI, SE, Index, Length, IsSigned))
  326. return;
  327. const auto *IndexAddRec = dyn_cast<SCEVAddRecExpr>(SE.getSCEV(Index));
  328. bool IsAffineIndex =
  329. IndexAddRec && (IndexAddRec->getLoop() == L) && IndexAddRec->isAffine();
  330. if (!IsAffineIndex)
  331. return;
  332. const SCEV *End = nullptr;
  333. // We strengthen "0 <= I" to "0 <= I < INT_SMAX" and "I < L" to "0 <= I < L".
  334. // We can potentially do much better here.
  335. if (Length)
  336. End = SE.getSCEV(Length);
  337. else {
  338. // So far we can only reach this point for Signed range check. This may
  339. // change in future. In this case we will need to pick Unsigned max for the
  340. // unsigned range check.
  341. unsigned BitWidth = cast<IntegerType>(IndexAddRec->getType())->getBitWidth();
  342. const SCEV *SIntMax = SE.getConstant(APInt::getSignedMaxValue(BitWidth));
  343. End = SIntMax;
  344. }
  345. InductiveRangeCheck IRC;
  346. IRC.End = End;
  347. IRC.Begin = IndexAddRec->getStart();
  348. IRC.Step = IndexAddRec->getStepRecurrence(SE);
  349. IRC.CheckUse = &ConditionUse;
  350. Checks.push_back(IRC);
  351. }
  352. void InductiveRangeCheck::extractRangeChecksFromBranch(
  353. BranchInst *BI, Loop *L, ScalarEvolution &SE, BranchProbabilityInfo *BPI,
  354. SmallVectorImpl<InductiveRangeCheck> &Checks) {
  355. if (BI->isUnconditional() || BI->getParent() == L->getLoopLatch())
  356. return;
  357. BranchProbability LikelyTaken(15, 16);
  358. if (!SkipProfitabilityChecks && BPI &&
  359. BPI->getEdgeProbability(BI->getParent(), (unsigned)0) < LikelyTaken)
  360. return;
  361. SmallPtrSet<Value *, 8> Visited;
  362. InductiveRangeCheck::extractRangeChecksFromCond(L, SE, BI->getOperandUse(0),
  363. Checks, Visited);
  364. }
  365. // Add metadata to the loop L to disable loop optimizations. Callers need to
  366. // confirm that optimizing loop L is not beneficial.
  367. static void DisableAllLoopOptsOnLoop(Loop &L) {
  368. // We do not care about any existing loopID related metadata for L, since we
  369. // are setting all loop metadata to false.
  370. LLVMContext &Context = L.getHeader()->getContext();
  371. // Reserve first location for self reference to the LoopID metadata node.
  372. MDNode *Dummy = MDNode::get(Context, {});
  373. MDNode *DisableUnroll = MDNode::get(
  374. Context, {MDString::get(Context, "llvm.loop.unroll.disable")});
  375. Metadata *FalseVal =
  376. ConstantAsMetadata::get(ConstantInt::get(Type::getInt1Ty(Context), 0));
  377. MDNode *DisableVectorize = MDNode::get(
  378. Context,
  379. {MDString::get(Context, "llvm.loop.vectorize.enable"), FalseVal});
  380. MDNode *DisableLICMVersioning = MDNode::get(
  381. Context, {MDString::get(Context, "llvm.loop.licm_versioning.disable")});
  382. MDNode *DisableDistribution= MDNode::get(
  383. Context,
  384. {MDString::get(Context, "llvm.loop.distribute.enable"), FalseVal});
  385. MDNode *NewLoopID =
  386. MDNode::get(Context, {Dummy, DisableUnroll, DisableVectorize,
  387. DisableLICMVersioning, DisableDistribution});
  388. // Set operand 0 to refer to the loop id itself.
  389. NewLoopID->replaceOperandWith(0, NewLoopID);
  390. L.setLoopID(NewLoopID);
  391. }
  392. namespace {
  393. // Keeps track of the structure of a loop. This is similar to llvm::Loop,
  394. // except that it is more lightweight and can track the state of a loop through
  395. // changing and potentially invalid IR. This structure also formalizes the
  396. // kinds of loops we can deal with -- ones that have a single latch that is also
  397. // an exiting block *and* have a canonical induction variable.
  398. struct LoopStructure {
  399. const char *Tag = "";
  400. BasicBlock *Header = nullptr;
  401. BasicBlock *Latch = nullptr;
  402. // `Latch's terminator instruction is `LatchBr', and it's `LatchBrExitIdx'th
  403. // successor is `LatchExit', the exit block of the loop.
  404. BranchInst *LatchBr = nullptr;
  405. BasicBlock *LatchExit = nullptr;
  406. unsigned LatchBrExitIdx = std::numeric_limits<unsigned>::max();
  407. // The loop represented by this instance of LoopStructure is semantically
  408. // equivalent to:
  409. //
  410. // intN_ty inc = IndVarIncreasing ? 1 : -1;
  411. // pred_ty predicate = IndVarIncreasing ? ICMP_SLT : ICMP_SGT;
  412. //
  413. // for (intN_ty iv = IndVarStart; predicate(iv, LoopExitAt); iv = IndVarBase)
  414. // ... body ...
  415. Value *IndVarBase = nullptr;
  416. Value *IndVarStart = nullptr;
  417. Value *IndVarStep = nullptr;
  418. Value *LoopExitAt = nullptr;
  419. bool IndVarIncreasing = false;
  420. bool IsSignedPredicate = true;
  421. LoopStructure() = default;
  422. template <typename M> LoopStructure map(M Map) const {
  423. LoopStructure Result;
  424. Result.Tag = Tag;
  425. Result.Header = cast<BasicBlock>(Map(Header));
  426. Result.Latch = cast<BasicBlock>(Map(Latch));
  427. Result.LatchBr = cast<BranchInst>(Map(LatchBr));
  428. Result.LatchExit = cast<BasicBlock>(Map(LatchExit));
  429. Result.LatchBrExitIdx = LatchBrExitIdx;
  430. Result.IndVarBase = Map(IndVarBase);
  431. Result.IndVarStart = Map(IndVarStart);
  432. Result.IndVarStep = Map(IndVarStep);
  433. Result.LoopExitAt = Map(LoopExitAt);
  434. Result.IndVarIncreasing = IndVarIncreasing;
  435. Result.IsSignedPredicate = IsSignedPredicate;
  436. return Result;
  437. }
  438. static Optional<LoopStructure> parseLoopStructure(ScalarEvolution &, Loop &,
  439. const char *&);
  440. };
  441. /// This class is used to constrain loops to run within a given iteration space.
  442. /// The algorithm this class implements is given a Loop and a range [Begin,
  443. /// End). The algorithm then tries to break out a "main loop" out of the loop
  444. /// it is given in a way that the "main loop" runs with the induction variable
  445. /// in a subset of [Begin, End). The algorithm emits appropriate pre and post
  446. /// loops to run any remaining iterations. The pre loop runs any iterations in
  447. /// which the induction variable is < Begin, and the post loop runs any
  448. /// iterations in which the induction variable is >= End.
  449. class LoopConstrainer {
  450. // The representation of a clone of the original loop we started out with.
  451. struct ClonedLoop {
  452. // The cloned blocks
  453. std::vector<BasicBlock *> Blocks;
  454. // `Map` maps values in the clonee into values in the cloned version
  455. ValueToValueMapTy Map;
  456. // An instance of `LoopStructure` for the cloned loop
  457. LoopStructure Structure;
  458. };
  459. // Result of rewriting the range of a loop. See changeIterationSpaceEnd for
  460. // more details on what these fields mean.
  461. struct RewrittenRangeInfo {
  462. BasicBlock *PseudoExit = nullptr;
  463. BasicBlock *ExitSelector = nullptr;
  464. std::vector<PHINode *> PHIValuesAtPseudoExit;
  465. PHINode *IndVarEnd = nullptr;
  466. RewrittenRangeInfo() = default;
  467. };
  468. // Calculated subranges we restrict the iteration space of the main loop to.
  469. // See the implementation of `calculateSubRanges' for more details on how
  470. // these fields are computed. `LowLimit` is None if there is no restriction
  471. // on low end of the restricted iteration space of the main loop. `HighLimit`
  472. // is None if there is no restriction on high end of the restricted iteration
  473. // space of the main loop.
  474. struct SubRanges {
  475. Optional<const SCEV *> LowLimit;
  476. Optional<const SCEV *> HighLimit;
  477. };
  478. // Compute a safe set of limits for the main loop to run in -- effectively the
  479. // intersection of `Range' and the iteration space of the original loop.
  480. // Return None if unable to compute the set of subranges.
  481. Optional<SubRanges> calculateSubRanges(bool IsSignedPredicate) const;
  482. // Clone `OriginalLoop' and return the result in CLResult. The IR after
  483. // running `cloneLoop' is well formed except for the PHI nodes in CLResult --
  484. // the PHI nodes say that there is an incoming edge from `OriginalPreheader`
  485. // but there is no such edge.
  486. void cloneLoop(ClonedLoop &CLResult, const char *Tag) const;
  487. // Create the appropriate loop structure needed to describe a cloned copy of
  488. // `Original`. The clone is described by `VM`.
  489. Loop *createClonedLoopStructure(Loop *Original, Loop *Parent,
  490. ValueToValueMapTy &VM, bool IsSubloop);
  491. // Rewrite the iteration space of the loop denoted by (LS, Preheader). The
  492. // iteration space of the rewritten loop ends at ExitLoopAt. The start of the
  493. // iteration space is not changed. `ExitLoopAt' is assumed to be slt
  494. // `OriginalHeaderCount'.
  495. //
  496. // If there are iterations left to execute, control is made to jump to
  497. // `ContinuationBlock', otherwise they take the normal loop exit. The
  498. // returned `RewrittenRangeInfo' object is populated as follows:
  499. //
  500. // .PseudoExit is a basic block that unconditionally branches to
  501. // `ContinuationBlock'.
  502. //
  503. // .ExitSelector is a basic block that decides, on exit from the loop,
  504. // whether to branch to the "true" exit or to `PseudoExit'.
  505. //
  506. // .PHIValuesAtPseudoExit are PHINodes in `PseudoExit' that compute the value
  507. // for each PHINode in the loop header on taking the pseudo exit.
  508. //
  509. // After changeIterationSpaceEnd, `Preheader' is no longer a legitimate
  510. // preheader because it is made to branch to the loop header only
  511. // conditionally.
  512. RewrittenRangeInfo
  513. changeIterationSpaceEnd(const LoopStructure &LS, BasicBlock *Preheader,
  514. Value *ExitLoopAt,
  515. BasicBlock *ContinuationBlock) const;
  516. // The loop denoted by `LS' has `OldPreheader' as its preheader. This
  517. // function creates a new preheader for `LS' and returns it.
  518. BasicBlock *createPreheader(const LoopStructure &LS, BasicBlock *OldPreheader,
  519. const char *Tag) const;
  520. // `ContinuationBlockAndPreheader' was the continuation block for some call to
  521. // `changeIterationSpaceEnd' and is the preheader to the loop denoted by `LS'.
  522. // This function rewrites the PHI nodes in `LS.Header' to start with the
  523. // correct value.
  524. void rewriteIncomingValuesForPHIs(
  525. LoopStructure &LS, BasicBlock *ContinuationBlockAndPreheader,
  526. const LoopConstrainer::RewrittenRangeInfo &RRI) const;
  527. // Even though we do not preserve any passes at this time, we at least need to
  528. // keep the parent loop structure consistent. The `LPPassManager' seems to
  529. // verify this after running a loop pass. This function adds the list of
  530. // blocks denoted by BBs to this loops parent loop if required.
  531. void addToParentLoopIfNeeded(ArrayRef<BasicBlock *> BBs);
  532. // Some global state.
  533. Function &F;
  534. LLVMContext &Ctx;
  535. ScalarEvolution &SE;
  536. DominatorTree &DT;
  537. LoopInfo &LI;
  538. function_ref<void(Loop *, bool)> LPMAddNewLoop;
  539. // Information about the original loop we started out with.
  540. Loop &OriginalLoop;
  541. const SCEV *LatchTakenCount = nullptr;
  542. BasicBlock *OriginalPreheader = nullptr;
  543. // The preheader of the main loop. This may or may not be different from
  544. // `OriginalPreheader'.
  545. BasicBlock *MainLoopPreheader = nullptr;
  546. // The range we need to run the main loop in.
  547. InductiveRangeCheck::Range Range;
  548. // The structure of the main loop (see comment at the beginning of this class
  549. // for a definition)
  550. LoopStructure MainLoopStructure;
  551. public:
  552. LoopConstrainer(Loop &L, LoopInfo &LI,
  553. function_ref<void(Loop *, bool)> LPMAddNewLoop,
  554. const LoopStructure &LS, ScalarEvolution &SE,
  555. DominatorTree &DT, InductiveRangeCheck::Range R)
  556. : F(*L.getHeader()->getParent()), Ctx(L.getHeader()->getContext()),
  557. SE(SE), DT(DT), LI(LI), LPMAddNewLoop(LPMAddNewLoop), OriginalLoop(L),
  558. Range(R), MainLoopStructure(LS) {}
  559. // Entry point for the algorithm. Returns true on success.
  560. bool run();
  561. };
  562. } // end anonymous namespace
  563. /// Given a loop with an deccreasing induction variable, is it possible to
  564. /// safely calculate the bounds of a new loop using the given Predicate.
  565. static bool isSafeDecreasingBound(const SCEV *Start,
  566. const SCEV *BoundSCEV, const SCEV *Step,
  567. ICmpInst::Predicate Pred,
  568. unsigned LatchBrExitIdx,
  569. Loop *L, ScalarEvolution &SE) {
  570. if (Pred != ICmpInst::ICMP_SLT && Pred != ICmpInst::ICMP_SGT &&
  571. Pred != ICmpInst::ICMP_ULT && Pred != ICmpInst::ICMP_UGT)
  572. return false;
  573. if (!SE.isAvailableAtLoopEntry(BoundSCEV, L))
  574. return false;
  575. assert(SE.isKnownNegative(Step) && "expecting negative step");
  576. LLVM_DEBUG(dbgs() << "irce: isSafeDecreasingBound with:\n");
  577. LLVM_DEBUG(dbgs() << "irce: Start: " << *Start << "\n");
  578. LLVM_DEBUG(dbgs() << "irce: Step: " << *Step << "\n");
  579. LLVM_DEBUG(dbgs() << "irce: BoundSCEV: " << *BoundSCEV << "\n");
  580. LLVM_DEBUG(dbgs() << "irce: Pred: " << ICmpInst::getPredicateName(Pred)
  581. << "\n");
  582. LLVM_DEBUG(dbgs() << "irce: LatchExitBrIdx: " << LatchBrExitIdx << "\n");
  583. bool IsSigned = ICmpInst::isSigned(Pred);
  584. // The predicate that we need to check that the induction variable lies
  585. // within bounds.
  586. ICmpInst::Predicate BoundPred =
  587. IsSigned ? CmpInst::ICMP_SGT : CmpInst::ICMP_UGT;
  588. if (LatchBrExitIdx == 1)
  589. return SE.isLoopEntryGuardedByCond(L, BoundPred, Start, BoundSCEV);
  590. assert(LatchBrExitIdx == 0 &&
  591. "LatchBrExitIdx should be either 0 or 1");
  592. const SCEV *StepPlusOne = SE.getAddExpr(Step, SE.getOne(Step->getType()));
  593. unsigned BitWidth = cast<IntegerType>(BoundSCEV->getType())->getBitWidth();
  594. APInt Min = IsSigned ? APInt::getSignedMinValue(BitWidth) :
  595. APInt::getMinValue(BitWidth);
  596. const SCEV *Limit = SE.getMinusSCEV(SE.getConstant(Min), StepPlusOne);
  597. const SCEV *MinusOne =
  598. SE.getMinusSCEV(BoundSCEV, SE.getOne(BoundSCEV->getType()));
  599. return SE.isLoopEntryGuardedByCond(L, BoundPred, Start, MinusOne) &&
  600. SE.isLoopEntryGuardedByCond(L, BoundPred, BoundSCEV, Limit);
  601. }
  602. /// Given a loop with an increasing induction variable, is it possible to
  603. /// safely calculate the bounds of a new loop using the given Predicate.
  604. static bool isSafeIncreasingBound(const SCEV *Start,
  605. const SCEV *BoundSCEV, const SCEV *Step,
  606. ICmpInst::Predicate Pred,
  607. unsigned LatchBrExitIdx,
  608. Loop *L, ScalarEvolution &SE) {
  609. if (Pred != ICmpInst::ICMP_SLT && Pred != ICmpInst::ICMP_SGT &&
  610. Pred != ICmpInst::ICMP_ULT && Pred != ICmpInst::ICMP_UGT)
  611. return false;
  612. if (!SE.isAvailableAtLoopEntry(BoundSCEV, L))
  613. return false;
  614. LLVM_DEBUG(dbgs() << "irce: isSafeIncreasingBound with:\n");
  615. LLVM_DEBUG(dbgs() << "irce: Start: " << *Start << "\n");
  616. LLVM_DEBUG(dbgs() << "irce: Step: " << *Step << "\n");
  617. LLVM_DEBUG(dbgs() << "irce: BoundSCEV: " << *BoundSCEV << "\n");
  618. LLVM_DEBUG(dbgs() << "irce: Pred: " << ICmpInst::getPredicateName(Pred)
  619. << "\n");
  620. LLVM_DEBUG(dbgs() << "irce: LatchExitBrIdx: " << LatchBrExitIdx << "\n");
  621. bool IsSigned = ICmpInst::isSigned(Pred);
  622. // The predicate that we need to check that the induction variable lies
  623. // within bounds.
  624. ICmpInst::Predicate BoundPred =
  625. IsSigned ? CmpInst::ICMP_SLT : CmpInst::ICMP_ULT;
  626. if (LatchBrExitIdx == 1)
  627. return SE.isLoopEntryGuardedByCond(L, BoundPred, Start, BoundSCEV);
  628. assert(LatchBrExitIdx == 0 && "LatchBrExitIdx should be 0 or 1");
  629. const SCEV *StepMinusOne =
  630. SE.getMinusSCEV(Step, SE.getOne(Step->getType()));
  631. unsigned BitWidth = cast<IntegerType>(BoundSCEV->getType())->getBitWidth();
  632. APInt Max = IsSigned ? APInt::getSignedMaxValue(BitWidth) :
  633. APInt::getMaxValue(BitWidth);
  634. const SCEV *Limit = SE.getMinusSCEV(SE.getConstant(Max), StepMinusOne);
  635. return (SE.isLoopEntryGuardedByCond(L, BoundPred, Start,
  636. SE.getAddExpr(BoundSCEV, Step)) &&
  637. SE.isLoopEntryGuardedByCond(L, BoundPred, BoundSCEV, Limit));
  638. }
  639. Optional<LoopStructure>
  640. LoopStructure::parseLoopStructure(ScalarEvolution &SE, Loop &L,
  641. const char *&FailureReason) {
  642. if (!L.isLoopSimplifyForm()) {
  643. FailureReason = "loop not in LoopSimplify form";
  644. return None;
  645. }
  646. BasicBlock *Latch = L.getLoopLatch();
  647. assert(Latch && "Simplified loops only have one latch!");
  648. if (Latch->getTerminator()->getMetadata(ClonedLoopTag)) {
  649. FailureReason = "loop has already been cloned";
  650. return None;
  651. }
  652. if (!L.isLoopExiting(Latch)) {
  653. FailureReason = "no loop latch";
  654. return None;
  655. }
  656. BasicBlock *Header = L.getHeader();
  657. BasicBlock *Preheader = L.getLoopPreheader();
  658. if (!Preheader) {
  659. FailureReason = "no preheader";
  660. return None;
  661. }
  662. BranchInst *LatchBr = dyn_cast<BranchInst>(Latch->getTerminator());
  663. if (!LatchBr || LatchBr->isUnconditional()) {
  664. FailureReason = "latch terminator not conditional branch";
  665. return None;
  666. }
  667. unsigned LatchBrExitIdx = LatchBr->getSuccessor(0) == Header ? 1 : 0;
  668. ICmpInst *ICI = dyn_cast<ICmpInst>(LatchBr->getCondition());
  669. if (!ICI || !isa<IntegerType>(ICI->getOperand(0)->getType())) {
  670. FailureReason = "latch terminator branch not conditional on integral icmp";
  671. return None;
  672. }
  673. const SCEV *LatchCount = SE.getExitCount(&L, Latch);
  674. if (isa<SCEVCouldNotCompute>(LatchCount)) {
  675. FailureReason = "could not compute latch count";
  676. return None;
  677. }
  678. ICmpInst::Predicate Pred = ICI->getPredicate();
  679. Value *LeftValue = ICI->getOperand(0);
  680. const SCEV *LeftSCEV = SE.getSCEV(LeftValue);
  681. IntegerType *IndVarTy = cast<IntegerType>(LeftValue->getType());
  682. Value *RightValue = ICI->getOperand(1);
  683. const SCEV *RightSCEV = SE.getSCEV(RightValue);
  684. // We canonicalize `ICI` such that `LeftSCEV` is an add recurrence.
  685. if (!isa<SCEVAddRecExpr>(LeftSCEV)) {
  686. if (isa<SCEVAddRecExpr>(RightSCEV)) {
  687. std::swap(LeftSCEV, RightSCEV);
  688. std::swap(LeftValue, RightValue);
  689. Pred = ICmpInst::getSwappedPredicate(Pred);
  690. } else {
  691. FailureReason = "no add recurrences in the icmp";
  692. return None;
  693. }
  694. }
  695. auto HasNoSignedWrap = [&](const SCEVAddRecExpr *AR) {
  696. if (AR->getNoWrapFlags(SCEV::FlagNSW))
  697. return true;
  698. IntegerType *Ty = cast<IntegerType>(AR->getType());
  699. IntegerType *WideTy =
  700. IntegerType::get(Ty->getContext(), Ty->getBitWidth() * 2);
  701. const SCEVAddRecExpr *ExtendAfterOp =
  702. dyn_cast<SCEVAddRecExpr>(SE.getSignExtendExpr(AR, WideTy));
  703. if (ExtendAfterOp) {
  704. const SCEV *ExtendedStart = SE.getSignExtendExpr(AR->getStart(), WideTy);
  705. const SCEV *ExtendedStep =
  706. SE.getSignExtendExpr(AR->getStepRecurrence(SE), WideTy);
  707. bool NoSignedWrap = ExtendAfterOp->getStart() == ExtendedStart &&
  708. ExtendAfterOp->getStepRecurrence(SE) == ExtendedStep;
  709. if (NoSignedWrap)
  710. return true;
  711. }
  712. // We may have proved this when computing the sign extension above.
  713. return AR->getNoWrapFlags(SCEV::FlagNSW) != SCEV::FlagAnyWrap;
  714. };
  715. // `ICI` is interpreted as taking the backedge if the *next* value of the
  716. // induction variable satisfies some constraint.
  717. const SCEVAddRecExpr *IndVarBase = cast<SCEVAddRecExpr>(LeftSCEV);
  718. if (!IndVarBase->isAffine()) {
  719. FailureReason = "LHS in icmp not induction variable";
  720. return None;
  721. }
  722. const SCEV* StepRec = IndVarBase->getStepRecurrence(SE);
  723. if (!isa<SCEVConstant>(StepRec)) {
  724. FailureReason = "LHS in icmp not induction variable";
  725. return None;
  726. }
  727. ConstantInt *StepCI = cast<SCEVConstant>(StepRec)->getValue();
  728. if (ICI->isEquality() && !HasNoSignedWrap(IndVarBase)) {
  729. FailureReason = "LHS in icmp needs nsw for equality predicates";
  730. return None;
  731. }
  732. assert(!StepCI->isZero() && "Zero step?");
  733. bool IsIncreasing = !StepCI->isNegative();
  734. bool IsSignedPredicate;
  735. const SCEV *StartNext = IndVarBase->getStart();
  736. const SCEV *Addend = SE.getNegativeSCEV(IndVarBase->getStepRecurrence(SE));
  737. const SCEV *IndVarStart = SE.getAddExpr(StartNext, Addend);
  738. const SCEV *Step = SE.getSCEV(StepCI);
  739. const SCEV *FixedRightSCEV = nullptr;
  740. // If RightValue resides within loop (but still being loop invariant),
  741. // regenerate it as preheader.
  742. if (auto *I = dyn_cast<Instruction>(RightValue))
  743. if (L.contains(I->getParent()))
  744. FixedRightSCEV = RightSCEV;
  745. if (IsIncreasing) {
  746. bool DecreasedRightValueByOne = false;
  747. if (StepCI->isOne()) {
  748. // Try to turn eq/ne predicates to those we can work with.
  749. if (Pred == ICmpInst::ICMP_NE && LatchBrExitIdx == 1)
  750. // while (++i != len) { while (++i < len) {
  751. // ... ---> ...
  752. // } }
  753. // If both parts are known non-negative, it is profitable to use
  754. // unsigned comparison in increasing loop. This allows us to make the
  755. // comparison check against "RightSCEV + 1" more optimistic.
  756. if (isKnownNonNegativeInLoop(IndVarStart, &L, SE) &&
  757. isKnownNonNegativeInLoop(RightSCEV, &L, SE))
  758. Pred = ICmpInst::ICMP_ULT;
  759. else
  760. Pred = ICmpInst::ICMP_SLT;
  761. else if (Pred == ICmpInst::ICMP_EQ && LatchBrExitIdx == 0) {
  762. // while (true) { while (true) {
  763. // if (++i == len) ---> if (++i > len - 1)
  764. // break; break;
  765. // ... ...
  766. // } }
  767. if (IndVarBase->getNoWrapFlags(SCEV::FlagNUW) &&
  768. cannotBeMinInLoop(RightSCEV, &L, SE, /*Signed*/false)) {
  769. Pred = ICmpInst::ICMP_UGT;
  770. RightSCEV = SE.getMinusSCEV(RightSCEV,
  771. SE.getOne(RightSCEV->getType()));
  772. DecreasedRightValueByOne = true;
  773. } else if (cannotBeMinInLoop(RightSCEV, &L, SE, /*Signed*/true)) {
  774. Pred = ICmpInst::ICMP_SGT;
  775. RightSCEV = SE.getMinusSCEV(RightSCEV,
  776. SE.getOne(RightSCEV->getType()));
  777. DecreasedRightValueByOne = true;
  778. }
  779. }
  780. }
  781. bool LTPred = (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_ULT);
  782. bool GTPred = (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_UGT);
  783. bool FoundExpectedPred =
  784. (LTPred && LatchBrExitIdx == 1) || (GTPred && LatchBrExitIdx == 0);
  785. if (!FoundExpectedPred) {
  786. FailureReason = "expected icmp slt semantically, found something else";
  787. return None;
  788. }
  789. IsSignedPredicate = ICmpInst::isSigned(Pred);
  790. if (!IsSignedPredicate && !AllowUnsignedLatchCondition) {
  791. FailureReason = "unsigned latch conditions are explicitly prohibited";
  792. return None;
  793. }
  794. if (!isSafeIncreasingBound(IndVarStart, RightSCEV, Step, Pred,
  795. LatchBrExitIdx, &L, SE)) {
  796. FailureReason = "Unsafe loop bounds";
  797. return None;
  798. }
  799. if (LatchBrExitIdx == 0) {
  800. // We need to increase the right value unless we have already decreased
  801. // it virtually when we replaced EQ with SGT.
  802. if (!DecreasedRightValueByOne)
  803. FixedRightSCEV =
  804. SE.getAddExpr(RightSCEV, SE.getOne(RightSCEV->getType()));
  805. } else {
  806. assert(!DecreasedRightValueByOne &&
  807. "Right value can be decreased only for LatchBrExitIdx == 0!");
  808. }
  809. } else {
  810. bool IncreasedRightValueByOne = false;
  811. if (StepCI->isMinusOne()) {
  812. // Try to turn eq/ne predicates to those we can work with.
  813. if (Pred == ICmpInst::ICMP_NE && LatchBrExitIdx == 1)
  814. // while (--i != len) { while (--i > len) {
  815. // ... ---> ...
  816. // } }
  817. // We intentionally don't turn the predicate into UGT even if we know
  818. // that both operands are non-negative, because it will only pessimize
  819. // our check against "RightSCEV - 1".
  820. Pred = ICmpInst::ICMP_SGT;
  821. else if (Pred == ICmpInst::ICMP_EQ && LatchBrExitIdx == 0) {
  822. // while (true) { while (true) {
  823. // if (--i == len) ---> if (--i < len + 1)
  824. // break; break;
  825. // ... ...
  826. // } }
  827. if (IndVarBase->getNoWrapFlags(SCEV::FlagNUW) &&
  828. cannotBeMaxInLoop(RightSCEV, &L, SE, /* Signed */ false)) {
  829. Pred = ICmpInst::ICMP_ULT;
  830. RightSCEV = SE.getAddExpr(RightSCEV, SE.getOne(RightSCEV->getType()));
  831. IncreasedRightValueByOne = true;
  832. } else if (cannotBeMaxInLoop(RightSCEV, &L, SE, /* Signed */ true)) {
  833. Pred = ICmpInst::ICMP_SLT;
  834. RightSCEV = SE.getAddExpr(RightSCEV, SE.getOne(RightSCEV->getType()));
  835. IncreasedRightValueByOne = true;
  836. }
  837. }
  838. }
  839. bool LTPred = (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_ULT);
  840. bool GTPred = (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_UGT);
  841. bool FoundExpectedPred =
  842. (GTPred && LatchBrExitIdx == 1) || (LTPred && LatchBrExitIdx == 0);
  843. if (!FoundExpectedPred) {
  844. FailureReason = "expected icmp sgt semantically, found something else";
  845. return None;
  846. }
  847. IsSignedPredicate =
  848. Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SGT;
  849. if (!IsSignedPredicate && !AllowUnsignedLatchCondition) {
  850. FailureReason = "unsigned latch conditions are explicitly prohibited";
  851. return None;
  852. }
  853. if (!isSafeDecreasingBound(IndVarStart, RightSCEV, Step, Pred,
  854. LatchBrExitIdx, &L, SE)) {
  855. FailureReason = "Unsafe bounds";
  856. return None;
  857. }
  858. if (LatchBrExitIdx == 0) {
  859. // We need to decrease the right value unless we have already increased
  860. // it virtually when we replaced EQ with SLT.
  861. if (!IncreasedRightValueByOne)
  862. FixedRightSCEV =
  863. SE.getMinusSCEV(RightSCEV, SE.getOne(RightSCEV->getType()));
  864. } else {
  865. assert(!IncreasedRightValueByOne &&
  866. "Right value can be increased only for LatchBrExitIdx == 0!");
  867. }
  868. }
  869. BasicBlock *LatchExit = LatchBr->getSuccessor(LatchBrExitIdx);
  870. assert(SE.getLoopDisposition(LatchCount, &L) ==
  871. ScalarEvolution::LoopInvariant &&
  872. "loop variant exit count doesn't make sense!");
  873. assert(!L.contains(LatchExit) && "expected an exit block!");
  874. const DataLayout &DL = Preheader->getModule()->getDataLayout();
  875. SCEVExpander Expander(SE, DL, "irce");
  876. Instruction *Ins = Preheader->getTerminator();
  877. if (FixedRightSCEV)
  878. RightValue =
  879. Expander.expandCodeFor(FixedRightSCEV, FixedRightSCEV->getType(), Ins);
  880. Value *IndVarStartV = Expander.expandCodeFor(IndVarStart, IndVarTy, Ins);
  881. IndVarStartV->setName("indvar.start");
  882. LoopStructure Result;
  883. Result.Tag = "main";
  884. Result.Header = Header;
  885. Result.Latch = Latch;
  886. Result.LatchBr = LatchBr;
  887. Result.LatchExit = LatchExit;
  888. Result.LatchBrExitIdx = LatchBrExitIdx;
  889. Result.IndVarStart = IndVarStartV;
  890. Result.IndVarStep = StepCI;
  891. Result.IndVarBase = LeftValue;
  892. Result.IndVarIncreasing = IsIncreasing;
  893. Result.LoopExitAt = RightValue;
  894. Result.IsSignedPredicate = IsSignedPredicate;
  895. FailureReason = nullptr;
  896. return Result;
  897. }
  898. /// If the type of \p S matches with \p Ty, return \p S. Otherwise, return
  899. /// signed or unsigned extension of \p S to type \p Ty.
  900. static const SCEV *NoopOrExtend(const SCEV *S, Type *Ty, ScalarEvolution &SE,
  901. bool Signed) {
  902. return Signed ? SE.getNoopOrSignExtend(S, Ty) : SE.getNoopOrZeroExtend(S, Ty);
  903. }
  904. Optional<LoopConstrainer::SubRanges>
  905. LoopConstrainer::calculateSubRanges(bool IsSignedPredicate) const {
  906. IntegerType *Ty = cast<IntegerType>(LatchTakenCount->getType());
  907. auto *RTy = cast<IntegerType>(Range.getType());
  908. // We only support wide range checks and narrow latches.
  909. if (!AllowNarrowLatchCondition && RTy != Ty)
  910. return None;
  911. if (RTy->getBitWidth() < Ty->getBitWidth())
  912. return None;
  913. LoopConstrainer::SubRanges Result;
  914. // I think we can be more aggressive here and make this nuw / nsw if the
  915. // addition that feeds into the icmp for the latch's terminating branch is nuw
  916. // / nsw. In any case, a wrapping 2's complement addition is safe.
  917. const SCEV *Start = NoopOrExtend(SE.getSCEV(MainLoopStructure.IndVarStart),
  918. RTy, SE, IsSignedPredicate);
  919. const SCEV *End = NoopOrExtend(SE.getSCEV(MainLoopStructure.LoopExitAt), RTy,
  920. SE, IsSignedPredicate);
  921. bool Increasing = MainLoopStructure.IndVarIncreasing;
  922. // We compute `Smallest` and `Greatest` such that [Smallest, Greatest), or
  923. // [Smallest, GreatestSeen] is the range of values the induction variable
  924. // takes.
  925. const SCEV *Smallest = nullptr, *Greatest = nullptr, *GreatestSeen = nullptr;
  926. const SCEV *One = SE.getOne(RTy);
  927. if (Increasing) {
  928. Smallest = Start;
  929. Greatest = End;
  930. // No overflow, because the range [Smallest, GreatestSeen] is not empty.
  931. GreatestSeen = SE.getMinusSCEV(End, One);
  932. } else {
  933. // These two computations may sign-overflow. Here is why that is okay:
  934. //
  935. // We know that the induction variable does not sign-overflow on any
  936. // iteration except the last one, and it starts at `Start` and ends at
  937. // `End`, decrementing by one every time.
  938. //
  939. // * if `Smallest` sign-overflows we know `End` is `INT_SMAX`. Since the
  940. // induction variable is decreasing we know that that the smallest value
  941. // the loop body is actually executed with is `INT_SMIN` == `Smallest`.
  942. //
  943. // * if `Greatest` sign-overflows, we know it can only be `INT_SMIN`. In
  944. // that case, `Clamp` will always return `Smallest` and
  945. // [`Result.LowLimit`, `Result.HighLimit`) = [`Smallest`, `Smallest`)
  946. // will be an empty range. Returning an empty range is always safe.
  947. Smallest = SE.getAddExpr(End, One);
  948. Greatest = SE.getAddExpr(Start, One);
  949. GreatestSeen = Start;
  950. }
  951. auto Clamp = [this, Smallest, Greatest, IsSignedPredicate](const SCEV *S) {
  952. return IsSignedPredicate
  953. ? SE.getSMaxExpr(Smallest, SE.getSMinExpr(Greatest, S))
  954. : SE.getUMaxExpr(Smallest, SE.getUMinExpr(Greatest, S));
  955. };
  956. // In some cases we can prove that we don't need a pre or post loop.
  957. ICmpInst::Predicate PredLE =
  958. IsSignedPredicate ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE;
  959. ICmpInst::Predicate PredLT =
  960. IsSignedPredicate ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
  961. bool ProvablyNoPreloop =
  962. SE.isKnownPredicate(PredLE, Range.getBegin(), Smallest);
  963. if (!ProvablyNoPreloop)
  964. Result.LowLimit = Clamp(Range.getBegin());
  965. bool ProvablyNoPostLoop =
  966. SE.isKnownPredicate(PredLT, GreatestSeen, Range.getEnd());
  967. if (!ProvablyNoPostLoop)
  968. Result.HighLimit = Clamp(Range.getEnd());
  969. return Result;
  970. }
  971. void LoopConstrainer::cloneLoop(LoopConstrainer::ClonedLoop &Result,
  972. const char *Tag) const {
  973. for (BasicBlock *BB : OriginalLoop.getBlocks()) {
  974. BasicBlock *Clone = CloneBasicBlock(BB, Result.Map, Twine(".") + Tag, &F);
  975. Result.Blocks.push_back(Clone);
  976. Result.Map[BB] = Clone;
  977. }
  978. auto GetClonedValue = [&Result](Value *V) {
  979. assert(V && "null values not in domain!");
  980. auto It = Result.Map.find(V);
  981. if (It == Result.Map.end())
  982. return V;
  983. return static_cast<Value *>(It->second);
  984. };
  985. auto *ClonedLatch =
  986. cast<BasicBlock>(GetClonedValue(OriginalLoop.getLoopLatch()));
  987. ClonedLatch->getTerminator()->setMetadata(ClonedLoopTag,
  988. MDNode::get(Ctx, {}));
  989. Result.Structure = MainLoopStructure.map(GetClonedValue);
  990. Result.Structure.Tag = Tag;
  991. for (unsigned i = 0, e = Result.Blocks.size(); i != e; ++i) {
  992. BasicBlock *ClonedBB = Result.Blocks[i];
  993. BasicBlock *OriginalBB = OriginalLoop.getBlocks()[i];
  994. assert(Result.Map[OriginalBB] == ClonedBB && "invariant!");
  995. for (Instruction &I : *ClonedBB)
  996. RemapInstruction(&I, Result.Map,
  997. RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);
  998. // Exit blocks will now have one more predecessor and their PHI nodes need
  999. // to be edited to reflect that. No phi nodes need to be introduced because
  1000. // the loop is in LCSSA.
  1001. for (auto *SBB : successors(OriginalBB)) {
  1002. if (OriginalLoop.contains(SBB))
  1003. continue; // not an exit block
  1004. for (PHINode &PN : SBB->phis()) {
  1005. Value *OldIncoming = PN.getIncomingValueForBlock(OriginalBB);
  1006. PN.addIncoming(GetClonedValue(OldIncoming), ClonedBB);
  1007. }
  1008. }
  1009. }
  1010. }
  1011. LoopConstrainer::RewrittenRangeInfo LoopConstrainer::changeIterationSpaceEnd(
  1012. const LoopStructure &LS, BasicBlock *Preheader, Value *ExitSubloopAt,
  1013. BasicBlock *ContinuationBlock) const {
  1014. // We start with a loop with a single latch:
  1015. //
  1016. // +--------------------+
  1017. // | |
  1018. // | preheader |
  1019. // | |
  1020. // +--------+-----------+
  1021. // | ----------------\
  1022. // | / |
  1023. // +--------v----v------+ |
  1024. // | | |
  1025. // | header | |
  1026. // | | |
  1027. // +--------------------+ |
  1028. // |
  1029. // ..... |
  1030. // |
  1031. // +--------------------+ |
  1032. // | | |
  1033. // | latch >----------/
  1034. // | |
  1035. // +-------v------------+
  1036. // |
  1037. // |
  1038. // | +--------------------+
  1039. // | | |
  1040. // +---> original exit |
  1041. // | |
  1042. // +--------------------+
  1043. //
  1044. // We change the control flow to look like
  1045. //
  1046. //
  1047. // +--------------------+
  1048. // | |
  1049. // | preheader >-------------------------+
  1050. // | | |
  1051. // +--------v-----------+ |
  1052. // | /-------------+ |
  1053. // | / | |
  1054. // +--------v--v--------+ | |
  1055. // | | | |
  1056. // | header | | +--------+ |
  1057. // | | | | | |
  1058. // +--------------------+ | | +-----v-----v-----------+
  1059. // | | | |
  1060. // | | | .pseudo.exit |
  1061. // | | | |
  1062. // | | +-----------v-----------+
  1063. // | | |
  1064. // ..... | | |
  1065. // | | +--------v-------------+
  1066. // +--------------------+ | | | |
  1067. // | | | | | ContinuationBlock |
  1068. // | latch >------+ | | |
  1069. // | | | +----------------------+
  1070. // +---------v----------+ |
  1071. // | |
  1072. // | |
  1073. // | +---------------^-----+
  1074. // | | |
  1075. // +-----> .exit.selector |
  1076. // | |
  1077. // +----------v----------+
  1078. // |
  1079. // +--------------------+ |
  1080. // | | |
  1081. // | original exit <----+
  1082. // | |
  1083. // +--------------------+
  1084. RewrittenRangeInfo RRI;
  1085. BasicBlock *BBInsertLocation = LS.Latch->getNextNode();
  1086. RRI.ExitSelector = BasicBlock::Create(Ctx, Twine(LS.Tag) + ".exit.selector",
  1087. &F, BBInsertLocation);
  1088. RRI.PseudoExit = BasicBlock::Create(Ctx, Twine(LS.Tag) + ".pseudo.exit", &F,
  1089. BBInsertLocation);
  1090. BranchInst *PreheaderJump = cast<BranchInst>(Preheader->getTerminator());
  1091. bool Increasing = LS.IndVarIncreasing;
  1092. bool IsSignedPredicate = LS.IsSignedPredicate;
  1093. IRBuilder<> B(PreheaderJump);
  1094. auto *RangeTy = Range.getBegin()->getType();
  1095. auto NoopOrExt = [&](Value *V) {
  1096. if (V->getType() == RangeTy)
  1097. return V;
  1098. return IsSignedPredicate ? B.CreateSExt(V, RangeTy, "wide." + V->getName())
  1099. : B.CreateZExt(V, RangeTy, "wide." + V->getName());
  1100. };
  1101. // EnterLoopCond - is it okay to start executing this `LS'?
  1102. Value *EnterLoopCond = nullptr;
  1103. auto Pred =
  1104. Increasing
  1105. ? (IsSignedPredicate ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT)
  1106. : (IsSignedPredicate ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT);
  1107. Value *IndVarStart = NoopOrExt(LS.IndVarStart);
  1108. EnterLoopCond = B.CreateICmp(Pred, IndVarStart, ExitSubloopAt);
  1109. B.CreateCondBr(EnterLoopCond, LS.Header, RRI.PseudoExit);
  1110. PreheaderJump->eraseFromParent();
  1111. LS.LatchBr->setSuccessor(LS.LatchBrExitIdx, RRI.ExitSelector);
  1112. B.SetInsertPoint(LS.LatchBr);
  1113. Value *IndVarBase = NoopOrExt(LS.IndVarBase);
  1114. Value *TakeBackedgeLoopCond = B.CreateICmp(Pred, IndVarBase, ExitSubloopAt);
  1115. Value *CondForBranch = LS.LatchBrExitIdx == 1
  1116. ? TakeBackedgeLoopCond
  1117. : B.CreateNot(TakeBackedgeLoopCond);
  1118. LS.LatchBr->setCondition(CondForBranch);
  1119. B.SetInsertPoint(RRI.ExitSelector);
  1120. // IterationsLeft - are there any more iterations left, given the original
  1121. // upper bound on the induction variable? If not, we branch to the "real"
  1122. // exit.
  1123. Value *LoopExitAt = NoopOrExt(LS.LoopExitAt);
  1124. Value *IterationsLeft = B.CreateICmp(Pred, IndVarBase, LoopExitAt);
  1125. B.CreateCondBr(IterationsLeft, RRI.PseudoExit, LS.LatchExit);
  1126. BranchInst *BranchToContinuation =
  1127. BranchInst::Create(ContinuationBlock, RRI.PseudoExit);
  1128. // We emit PHI nodes into `RRI.PseudoExit' that compute the "latest" value of
  1129. // each of the PHI nodes in the loop header. This feeds into the initial
  1130. // value of the same PHI nodes if/when we continue execution.
  1131. for (PHINode &PN : LS.Header->phis()) {
  1132. PHINode *NewPHI = PHINode::Create(PN.getType(), 2, PN.getName() + ".copy",
  1133. BranchToContinuation);
  1134. NewPHI->addIncoming(PN.getIncomingValueForBlock(Preheader), Preheader);
  1135. NewPHI->addIncoming(PN.getIncomingValueForBlock(LS.Latch),
  1136. RRI.ExitSelector);
  1137. RRI.PHIValuesAtPseudoExit.push_back(NewPHI);
  1138. }
  1139. RRI.IndVarEnd = PHINode::Create(IndVarBase->getType(), 2, "indvar.end",
  1140. BranchToContinuation);
  1141. RRI.IndVarEnd->addIncoming(IndVarStart, Preheader);
  1142. RRI.IndVarEnd->addIncoming(IndVarBase, RRI.ExitSelector);
  1143. // The latch exit now has a branch from `RRI.ExitSelector' instead of
  1144. // `LS.Latch'. The PHI nodes need to be updated to reflect that.
  1145. LS.LatchExit->replacePhiUsesWith(LS.Latch, RRI.ExitSelector);
  1146. return RRI;
  1147. }
  1148. void LoopConstrainer::rewriteIncomingValuesForPHIs(
  1149. LoopStructure &LS, BasicBlock *ContinuationBlock,
  1150. const LoopConstrainer::RewrittenRangeInfo &RRI) const {
  1151. unsigned PHIIndex = 0;
  1152. for (PHINode &PN : LS.Header->phis())
  1153. PN.setIncomingValueForBlock(ContinuationBlock,
  1154. RRI.PHIValuesAtPseudoExit[PHIIndex++]);
  1155. LS.IndVarStart = RRI.IndVarEnd;
  1156. }
  1157. BasicBlock *LoopConstrainer::createPreheader(const LoopStructure &LS,
  1158. BasicBlock *OldPreheader,
  1159. const char *Tag) const {
  1160. BasicBlock *Preheader = BasicBlock::Create(Ctx, Tag, &F, LS.Header);
  1161. BranchInst::Create(LS.Header, Preheader);
  1162. LS.Header->replacePhiUsesWith(OldPreheader, Preheader);
  1163. return Preheader;
  1164. }
  1165. void LoopConstrainer::addToParentLoopIfNeeded(ArrayRef<BasicBlock *> BBs) {
  1166. Loop *ParentLoop = OriginalLoop.getParentLoop();
  1167. if (!ParentLoop)
  1168. return;
  1169. for (BasicBlock *BB : BBs)
  1170. ParentLoop->addBasicBlockToLoop(BB, LI);
  1171. }
  1172. Loop *LoopConstrainer::createClonedLoopStructure(Loop *Original, Loop *Parent,
  1173. ValueToValueMapTy &VM,
  1174. bool IsSubloop) {
  1175. Loop &New = *LI.AllocateLoop();
  1176. if (Parent)
  1177. Parent->addChildLoop(&New);
  1178. else
  1179. LI.addTopLevelLoop(&New);
  1180. LPMAddNewLoop(&New, IsSubloop);
  1181. // Add all of the blocks in Original to the new loop.
  1182. for (auto *BB : Original->blocks())
  1183. if (LI.getLoopFor(BB) == Original)
  1184. New.addBasicBlockToLoop(cast<BasicBlock>(VM[BB]), LI);
  1185. // Add all of the subloops to the new loop.
  1186. for (Loop *SubLoop : *Original)
  1187. createClonedLoopStructure(SubLoop, &New, VM, /* IsSubloop */ true);
  1188. return &New;
  1189. }
  1190. bool LoopConstrainer::run() {
  1191. BasicBlock *Preheader = nullptr;
  1192. LatchTakenCount = SE.getExitCount(&OriginalLoop, MainLoopStructure.Latch);
  1193. Preheader = OriginalLoop.getLoopPreheader();
  1194. assert(!isa<SCEVCouldNotCompute>(LatchTakenCount) && Preheader != nullptr &&
  1195. "preconditions!");
  1196. OriginalPreheader = Preheader;
  1197. MainLoopPreheader = Preheader;
  1198. bool IsSignedPredicate = MainLoopStructure.IsSignedPredicate;
  1199. Optional<SubRanges> MaybeSR = calculateSubRanges(IsSignedPredicate);
  1200. if (!MaybeSR.hasValue()) {
  1201. LLVM_DEBUG(dbgs() << "irce: could not compute subranges\n");
  1202. return false;
  1203. }
  1204. SubRanges SR = MaybeSR.getValue();
  1205. bool Increasing = MainLoopStructure.IndVarIncreasing;
  1206. IntegerType *IVTy =
  1207. cast<IntegerType>(Range.getBegin()->getType());
  1208. SCEVExpander Expander(SE, F.getParent()->getDataLayout(), "irce");
  1209. Instruction *InsertPt = OriginalPreheader->getTerminator();
  1210. // It would have been better to make `PreLoop' and `PostLoop'
  1211. // `Optional<ClonedLoop>'s, but `ValueToValueMapTy' does not have a copy
  1212. // constructor.
  1213. ClonedLoop PreLoop, PostLoop;
  1214. bool NeedsPreLoop =
  1215. Increasing ? SR.LowLimit.hasValue() : SR.HighLimit.hasValue();
  1216. bool NeedsPostLoop =
  1217. Increasing ? SR.HighLimit.hasValue() : SR.LowLimit.hasValue();
  1218. Value *ExitPreLoopAt = nullptr;
  1219. Value *ExitMainLoopAt = nullptr;
  1220. const SCEVConstant *MinusOneS =
  1221. cast<SCEVConstant>(SE.getConstant(IVTy, -1, true /* isSigned */));
  1222. if (NeedsPreLoop) {
  1223. const SCEV *ExitPreLoopAtSCEV = nullptr;
  1224. if (Increasing)
  1225. ExitPreLoopAtSCEV = *SR.LowLimit;
  1226. else if (cannotBeMinInLoop(*SR.HighLimit, &OriginalLoop, SE,
  1227. IsSignedPredicate))
  1228. ExitPreLoopAtSCEV = SE.getAddExpr(*SR.HighLimit, MinusOneS);
  1229. else {
  1230. LLVM_DEBUG(dbgs() << "irce: could not prove no-overflow when computing "
  1231. << "preloop exit limit. HighLimit = "
  1232. << *(*SR.HighLimit) << "\n");
  1233. return false;
  1234. }
  1235. if (!isSafeToExpandAt(ExitPreLoopAtSCEV, InsertPt, SE)) {
  1236. LLVM_DEBUG(dbgs() << "irce: could not prove that it is safe to expand the"
  1237. << " preloop exit limit " << *ExitPreLoopAtSCEV
  1238. << " at block " << InsertPt->getParent()->getName()
  1239. << "\n");
  1240. return false;
  1241. }
  1242. ExitPreLoopAt = Expander.expandCodeFor(ExitPreLoopAtSCEV, IVTy, InsertPt);
  1243. ExitPreLoopAt->setName("exit.preloop.at");
  1244. }
  1245. if (NeedsPostLoop) {
  1246. const SCEV *ExitMainLoopAtSCEV = nullptr;
  1247. if (Increasing)
  1248. ExitMainLoopAtSCEV = *SR.HighLimit;
  1249. else if (cannotBeMinInLoop(*SR.LowLimit, &OriginalLoop, SE,
  1250. IsSignedPredicate))
  1251. ExitMainLoopAtSCEV = SE.getAddExpr(*SR.LowLimit, MinusOneS);
  1252. else {
  1253. LLVM_DEBUG(dbgs() << "irce: could not prove no-overflow when computing "
  1254. << "mainloop exit limit. LowLimit = "
  1255. << *(*SR.LowLimit) << "\n");
  1256. return false;
  1257. }
  1258. if (!isSafeToExpandAt(ExitMainLoopAtSCEV, InsertPt, SE)) {
  1259. LLVM_DEBUG(dbgs() << "irce: could not prove that it is safe to expand the"
  1260. << " main loop exit limit " << *ExitMainLoopAtSCEV
  1261. << " at block " << InsertPt->getParent()->getName()
  1262. << "\n");
  1263. return false;
  1264. }
  1265. ExitMainLoopAt = Expander.expandCodeFor(ExitMainLoopAtSCEV, IVTy, InsertPt);
  1266. ExitMainLoopAt->setName("exit.mainloop.at");
  1267. }
  1268. // We clone these ahead of time so that we don't have to deal with changing
  1269. // and temporarily invalid IR as we transform the loops.
  1270. if (NeedsPreLoop)
  1271. cloneLoop(PreLoop, "preloop");
  1272. if (NeedsPostLoop)
  1273. cloneLoop(PostLoop, "postloop");
  1274. RewrittenRangeInfo PreLoopRRI;
  1275. if (NeedsPreLoop) {
  1276. Preheader->getTerminator()->replaceUsesOfWith(MainLoopStructure.Header,
  1277. PreLoop.Structure.Header);
  1278. MainLoopPreheader =
  1279. createPreheader(MainLoopStructure, Preheader, "mainloop");
  1280. PreLoopRRI = changeIterationSpaceEnd(PreLoop.Structure, Preheader,
  1281. ExitPreLoopAt, MainLoopPreheader);
  1282. rewriteIncomingValuesForPHIs(MainLoopStructure, MainLoopPreheader,
  1283. PreLoopRRI);
  1284. }
  1285. BasicBlock *PostLoopPreheader = nullptr;
  1286. RewrittenRangeInfo PostLoopRRI;
  1287. if (NeedsPostLoop) {
  1288. PostLoopPreheader =
  1289. createPreheader(PostLoop.Structure, Preheader, "postloop");
  1290. PostLoopRRI = changeIterationSpaceEnd(MainLoopStructure, MainLoopPreheader,
  1291. ExitMainLoopAt, PostLoopPreheader);
  1292. rewriteIncomingValuesForPHIs(PostLoop.Structure, PostLoopPreheader,
  1293. PostLoopRRI);
  1294. }
  1295. BasicBlock *NewMainLoopPreheader =
  1296. MainLoopPreheader != Preheader ? MainLoopPreheader : nullptr;
  1297. BasicBlock *NewBlocks[] = {PostLoopPreheader, PreLoopRRI.PseudoExit,
  1298. PreLoopRRI.ExitSelector, PostLoopRRI.PseudoExit,
  1299. PostLoopRRI.ExitSelector, NewMainLoopPreheader};
  1300. // Some of the above may be nullptr, filter them out before passing to
  1301. // addToParentLoopIfNeeded.
  1302. auto NewBlocksEnd =
  1303. std::remove(std::begin(NewBlocks), std::end(NewBlocks), nullptr);
  1304. addToParentLoopIfNeeded(makeArrayRef(std::begin(NewBlocks), NewBlocksEnd));
  1305. DT.recalculate(F);
  1306. // We need to first add all the pre and post loop blocks into the loop
  1307. // structures (as part of createClonedLoopStructure), and then update the
  1308. // LCSSA form and LoopSimplifyForm. This is necessary for correctly updating
  1309. // LI when LoopSimplifyForm is generated.
  1310. Loop *PreL = nullptr, *PostL = nullptr;
  1311. if (!PreLoop.Blocks.empty()) {
  1312. PreL = createClonedLoopStructure(&OriginalLoop,
  1313. OriginalLoop.getParentLoop(), PreLoop.Map,
  1314. /* IsSubLoop */ false);
  1315. }
  1316. if (!PostLoop.Blocks.empty()) {
  1317. PostL =
  1318. createClonedLoopStructure(&OriginalLoop, OriginalLoop.getParentLoop(),
  1319. PostLoop.Map, /* IsSubLoop */ false);
  1320. }
  1321. // This function canonicalizes the loop into Loop-Simplify and LCSSA forms.
  1322. auto CanonicalizeLoop = [&] (Loop *L, bool IsOriginalLoop) {
  1323. formLCSSARecursively(*L, DT, &LI, &SE);
  1324. simplifyLoop(L, &DT, &LI, &SE, nullptr, nullptr, true);
  1325. // Pre/post loops are slow paths, we do not need to perform any loop
  1326. // optimizations on them.
  1327. if (!IsOriginalLoop)
  1328. DisableAllLoopOptsOnLoop(*L);
  1329. };
  1330. if (PreL)
  1331. CanonicalizeLoop(PreL, false);
  1332. if (PostL)
  1333. CanonicalizeLoop(PostL, false);
  1334. CanonicalizeLoop(&OriginalLoop, true);
  1335. return true;
  1336. }
  1337. /// Computes and returns a range of values for the induction variable (IndVar)
  1338. /// in which the range check can be safely elided. If it cannot compute such a
  1339. /// range, returns None.
  1340. Optional<InductiveRangeCheck::Range>
  1341. InductiveRangeCheck::computeSafeIterationSpace(
  1342. ScalarEvolution &SE, const SCEVAddRecExpr *IndVar,
  1343. bool IsLatchSigned) const {
  1344. // We can deal when types of latch check and range checks don't match in case
  1345. // if latch check is more narrow.
  1346. auto *IVType = cast<IntegerType>(IndVar->getType());
  1347. auto *RCType = cast<IntegerType>(getBegin()->getType());
  1348. if (IVType->getBitWidth() > RCType->getBitWidth())
  1349. return None;
  1350. // IndVar is of the form "A + B * I" (where "I" is the canonical induction
  1351. // variable, that may or may not exist as a real llvm::Value in the loop) and
  1352. // this inductive range check is a range check on the "C + D * I" ("C" is
  1353. // getBegin() and "D" is getStep()). We rewrite the value being range
  1354. // checked to "M + N * IndVar" where "N" = "D * B^(-1)" and "M" = "C - NA".
  1355. //
  1356. // The actual inequalities we solve are of the form
  1357. //
  1358. // 0 <= M + 1 * IndVar < L given L >= 0 (i.e. N == 1)
  1359. //
  1360. // Here L stands for upper limit of the safe iteration space.
  1361. // The inequality is satisfied by (0 - M) <= IndVar < (L - M). To avoid
  1362. // overflows when calculating (0 - M) and (L - M) we, depending on type of
  1363. // IV's iteration space, limit the calculations by borders of the iteration
  1364. // space. For example, if IndVar is unsigned, (0 - M) overflows for any M > 0.
  1365. // If we figured out that "anything greater than (-M) is safe", we strengthen
  1366. // this to "everything greater than 0 is safe", assuming that values between
  1367. // -M and 0 just do not exist in unsigned iteration space, and we don't want
  1368. // to deal with overflown values.
  1369. if (!IndVar->isAffine())
  1370. return None;
  1371. const SCEV *A = NoopOrExtend(IndVar->getStart(), RCType, SE, IsLatchSigned);
  1372. const SCEVConstant *B = dyn_cast<SCEVConstant>(
  1373. NoopOrExtend(IndVar->getStepRecurrence(SE), RCType, SE, IsLatchSigned));
  1374. if (!B)
  1375. return None;
  1376. assert(!B->isZero() && "Recurrence with zero step?");
  1377. const SCEV *C = getBegin();
  1378. const SCEVConstant *D = dyn_cast<SCEVConstant>(getStep());
  1379. if (D != B)
  1380. return None;
  1381. assert(!D->getValue()->isZero() && "Recurrence with zero step?");
  1382. unsigned BitWidth = RCType->getBitWidth();
  1383. const SCEV *SIntMax = SE.getConstant(APInt::getSignedMaxValue(BitWidth));
  1384. // Subtract Y from X so that it does not go through border of the IV
  1385. // iteration space. Mathematically, it is equivalent to:
  1386. //
  1387. // ClampedSubtract(X, Y) = min(max(X - Y, INT_MIN), INT_MAX). [1]
  1388. //
  1389. // In [1], 'X - Y' is a mathematical subtraction (result is not bounded to
  1390. // any width of bit grid). But after we take min/max, the result is
  1391. // guaranteed to be within [INT_MIN, INT_MAX].
  1392. //
  1393. // In [1], INT_MAX and INT_MIN are respectively signed and unsigned max/min
  1394. // values, depending on type of latch condition that defines IV iteration
  1395. // space.
  1396. auto ClampedSubtract = [&](const SCEV *X, const SCEV *Y) {
  1397. // FIXME: The current implementation assumes that X is in [0, SINT_MAX].
  1398. // This is required to ensure that SINT_MAX - X does not overflow signed and
  1399. // that X - Y does not overflow unsigned if Y is negative. Can we lift this
  1400. // restriction and make it work for negative X either?
  1401. if (IsLatchSigned) {
  1402. // X is a number from signed range, Y is interpreted as signed.
  1403. // Even if Y is SINT_MAX, (X - Y) does not reach SINT_MIN. So the only
  1404. // thing we should care about is that we didn't cross SINT_MAX.
  1405. // So, if Y is positive, we subtract Y safely.
  1406. // Rule 1: Y > 0 ---> Y.
  1407. // If 0 <= -Y <= (SINT_MAX - X), we subtract Y safely.
  1408. // Rule 2: Y >=s (X - SINT_MAX) ---> Y.
  1409. // If 0 <= (SINT_MAX - X) < -Y, we can only subtract (X - SINT_MAX).
  1410. // Rule 3: Y <s (X - SINT_MAX) ---> (X - SINT_MAX).
  1411. // It gives us smax(Y, X - SINT_MAX) to subtract in all cases.
  1412. const SCEV *XMinusSIntMax = SE.getMinusSCEV(X, SIntMax);
  1413. return SE.getMinusSCEV(X, SE.getSMaxExpr(Y, XMinusSIntMax),
  1414. SCEV::FlagNSW);
  1415. } else
  1416. // X is a number from unsigned range, Y is interpreted as signed.
  1417. // Even if Y is SINT_MIN, (X - Y) does not reach UINT_MAX. So the only
  1418. // thing we should care about is that we didn't cross zero.
  1419. // So, if Y is negative, we subtract Y safely.
  1420. // Rule 1: Y <s 0 ---> Y.
  1421. // If 0 <= Y <= X, we subtract Y safely.
  1422. // Rule 2: Y <=s X ---> Y.
  1423. // If 0 <= X < Y, we should stop at 0 and can only subtract X.
  1424. // Rule 3: Y >s X ---> X.
  1425. // It gives us smin(X, Y) to subtract in all cases.
  1426. return SE.getMinusSCEV(X, SE.getSMinExpr(X, Y), SCEV::FlagNUW);
  1427. };
  1428. const SCEV *M = SE.getMinusSCEV(C, A);
  1429. const SCEV *Zero = SE.getZero(M->getType());
  1430. // This function returns SCEV equal to 1 if X is non-negative 0 otherwise.
  1431. auto SCEVCheckNonNegative = [&](const SCEV *X) {
  1432. const Loop *L = IndVar->getLoop();
  1433. const SCEV *One = SE.getOne(X->getType());
  1434. // Can we trivially prove that X is a non-negative or negative value?
  1435. if (isKnownNonNegativeInLoop(X, L, SE))
  1436. return One;
  1437. else if (isKnownNegativeInLoop(X, L, SE))
  1438. return Zero;
  1439. // If not, we will have to figure it out during the execution.
  1440. // Function smax(smin(X, 0), -1) + 1 equals to 1 if X >= 0 and 0 if X < 0.
  1441. const SCEV *NegOne = SE.getNegativeSCEV(One);
  1442. return SE.getAddExpr(SE.getSMaxExpr(SE.getSMinExpr(X, Zero), NegOne), One);
  1443. };
  1444. // FIXME: Current implementation of ClampedSubtract implicitly assumes that
  1445. // X is non-negative (in sense of a signed value). We need to re-implement
  1446. // this function in a way that it will correctly handle negative X as well.
  1447. // We use it twice: for X = 0 everything is fine, but for X = getEnd() we can
  1448. // end up with a negative X and produce wrong results. So currently we ensure
  1449. // that if getEnd() is negative then both ends of the safe range are zero.
  1450. // Note that this may pessimize elimination of unsigned range checks against
  1451. // negative values.
  1452. const SCEV *REnd = getEnd();
  1453. const SCEV *EndIsNonNegative = SCEVCheckNonNegative(REnd);
  1454. const SCEV *Begin = SE.getMulExpr(ClampedSubtract(Zero, M), EndIsNonNegative);
  1455. const SCEV *End = SE.getMulExpr(ClampedSubtract(REnd, M), EndIsNonNegative);
  1456. return InductiveRangeCheck::Range(Begin, End);
  1457. }
  1458. static Optional<InductiveRangeCheck::Range>
  1459. IntersectSignedRange(ScalarEvolution &SE,
  1460. const Optional<InductiveRangeCheck::Range> &R1,
  1461. const InductiveRangeCheck::Range &R2) {
  1462. if (R2.isEmpty(SE, /* IsSigned */ true))
  1463. return None;
  1464. if (!R1.hasValue())
  1465. return R2;
  1466. auto &R1Value = R1.getValue();
  1467. // We never return empty ranges from this function, and R1 is supposed to be
  1468. // a result of intersection. Thus, R1 is never empty.
  1469. assert(!R1Value.isEmpty(SE, /* IsSigned */ true) &&
  1470. "We should never have empty R1!");
  1471. // TODO: we could widen the smaller range and have this work; but for now we
  1472. // bail out to keep things simple.
  1473. if (R1Value.getType() != R2.getType())
  1474. return None;
  1475. const SCEV *NewBegin = SE.getSMaxExpr(R1Value.getBegin(), R2.getBegin());
  1476. const SCEV *NewEnd = SE.getSMinExpr(R1Value.getEnd(), R2.getEnd());
  1477. // If the resulting range is empty, just return None.
  1478. auto Ret = InductiveRangeCheck::Range(NewBegin, NewEnd);
  1479. if (Ret.isEmpty(SE, /* IsSigned */ true))
  1480. return None;
  1481. return Ret;
  1482. }
  1483. static Optional<InductiveRangeCheck::Range>
  1484. IntersectUnsignedRange(ScalarEvolution &SE,
  1485. const Optional<InductiveRangeCheck::Range> &R1,
  1486. const InductiveRangeCheck::Range &R2) {
  1487. if (R2.isEmpty(SE, /* IsSigned */ false))
  1488. return None;
  1489. if (!R1.hasValue())
  1490. return R2;
  1491. auto &R1Value = R1.getValue();
  1492. // We never return empty ranges from this function, and R1 is supposed to be
  1493. // a result of intersection. Thus, R1 is never empty.
  1494. assert(!R1Value.isEmpty(SE, /* IsSigned */ false) &&
  1495. "We should never have empty R1!");
  1496. // TODO: we could widen the smaller range and have this work; but for now we
  1497. // bail out to keep things simple.
  1498. if (R1Value.getType() != R2.getType())
  1499. return None;
  1500. const SCEV *NewBegin = SE.getUMaxExpr(R1Value.getBegin(), R2.getBegin());
  1501. const SCEV *NewEnd = SE.getUMinExpr(R1Value.getEnd(), R2.getEnd());
  1502. // If the resulting range is empty, just return None.
  1503. auto Ret = InductiveRangeCheck::Range(NewBegin, NewEnd);
  1504. if (Ret.isEmpty(SE, /* IsSigned */ false))
  1505. return None;
  1506. return Ret;
  1507. }
  1508. PreservedAnalyses IRCEPass::run(Function &F, FunctionAnalysisManager &AM) {
  1509. auto &SE = AM.getResult<ScalarEvolutionAnalysis>(F);
  1510. auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
  1511. auto &BPI = AM.getResult<BranchProbabilityAnalysis>(F);
  1512. LoopInfo &LI = AM.getResult<LoopAnalysis>(F);
  1513. // Get BFI analysis result on demand. Please note that modification of
  1514. // CFG invalidates this analysis and we should handle it.
  1515. auto getBFI = [&F, &AM ]()->BlockFrequencyInfo & {
  1516. return AM.getResult<BlockFrequencyAnalysis>(F);
  1517. };
  1518. InductiveRangeCheckElimination IRCE(SE, &BPI, DT, LI, { getBFI });
  1519. bool Changed = false;
  1520. {
  1521. bool CFGChanged = false;
  1522. for (const auto &L : LI) {
  1523. CFGChanged |= simplifyLoop(L, &DT, &LI, &SE, nullptr, nullptr,
  1524. /*PreserveLCSSA=*/false);
  1525. Changed |= formLCSSARecursively(*L, DT, &LI, &SE);
  1526. }
  1527. Changed |= CFGChanged;
  1528. if (CFGChanged && !SkipProfitabilityChecks) {
  1529. PreservedAnalyses PA = PreservedAnalyses::all();
  1530. PA.abandon<BlockFrequencyAnalysis>();
  1531. AM.invalidate(F, PA);
  1532. }
  1533. }
  1534. SmallPriorityWorklist<Loop *, 4> Worklist;
  1535. appendLoopsToWorklist(LI, Worklist);
  1536. auto LPMAddNewLoop = [&Worklist](Loop *NL, bool IsSubloop) {
  1537. if (!IsSubloop)
  1538. appendLoopsToWorklist(*NL, Worklist);
  1539. };
  1540. while (!Worklist.empty()) {
  1541. Loop *L = Worklist.pop_back_val();
  1542. if (IRCE.run(L, LPMAddNewLoop)) {
  1543. Changed = true;
  1544. if (!SkipProfitabilityChecks) {
  1545. PreservedAnalyses PA = PreservedAnalyses::all();
  1546. PA.abandon<BlockFrequencyAnalysis>();
  1547. AM.invalidate(F, PA);
  1548. }
  1549. }
  1550. }
  1551. if (!Changed)
  1552. return PreservedAnalyses::all();
  1553. return getLoopPassPreservedAnalyses();
  1554. }
  1555. bool IRCELegacyPass::runOnFunction(Function &F) {
  1556. if (skipFunction(F))
  1557. return false;
  1558. ScalarEvolution &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE();
  1559. BranchProbabilityInfo &BPI =
  1560. getAnalysis<BranchProbabilityInfoWrapperPass>().getBPI();
  1561. auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
  1562. auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
  1563. InductiveRangeCheckElimination IRCE(SE, &BPI, DT, LI);
  1564. bool Changed = false;
  1565. for (const auto &L : LI) {
  1566. Changed |= simplifyLoop(L, &DT, &LI, &SE, nullptr, nullptr,
  1567. /*PreserveLCSSA=*/false);
  1568. Changed |= formLCSSARecursively(*L, DT, &LI, &SE);
  1569. }
  1570. SmallPriorityWorklist<Loop *, 4> Worklist;
  1571. appendLoopsToWorklist(LI, Worklist);
  1572. auto LPMAddNewLoop = [&](Loop *NL, bool IsSubloop) {
  1573. if (!IsSubloop)
  1574. appendLoopsToWorklist(*NL, Worklist);
  1575. };
  1576. while (!Worklist.empty()) {
  1577. Loop *L = Worklist.pop_back_val();
  1578. Changed |= IRCE.run(L, LPMAddNewLoop);
  1579. }
  1580. return Changed;
  1581. }
  1582. bool
  1583. InductiveRangeCheckElimination::isProfitableToTransform(const Loop &L,
  1584. LoopStructure &LS) {
  1585. if (SkipProfitabilityChecks)
  1586. return true;
  1587. if (GetBFI.hasValue()) {
  1588. BlockFrequencyInfo &BFI = (*GetBFI)();
  1589. uint64_t hFreq = BFI.getBlockFreq(LS.Header).getFrequency();
  1590. uint64_t phFreq = BFI.getBlockFreq(L.getLoopPreheader()).getFrequency();
  1591. if (phFreq != 0 && hFreq != 0 && (hFreq / phFreq < MinRuntimeIterations)) {
  1592. LLVM_DEBUG(dbgs() << "irce: could not prove profitability: "
  1593. << "the estimated number of iterations basing on "
  1594. "frequency info is " << (hFreq / phFreq) << "\n";);
  1595. return false;
  1596. }
  1597. return true;
  1598. }
  1599. if (!BPI)
  1600. return true;
  1601. BranchProbability ExitProbability =
  1602. BPI->getEdgeProbability(LS.Latch, LS.LatchBrExitIdx);
  1603. if (ExitProbability > BranchProbability(1, MinRuntimeIterations)) {
  1604. LLVM_DEBUG(dbgs() << "irce: could not prove profitability: "
  1605. << "the exit probability is too big " << ExitProbability
  1606. << "\n";);
  1607. return false;
  1608. }
  1609. return true;
  1610. }
  1611. bool InductiveRangeCheckElimination::run(
  1612. Loop *L, function_ref<void(Loop *, bool)> LPMAddNewLoop) {
  1613. if (L->getBlocks().size() >= LoopSizeCutoff) {
  1614. LLVM_DEBUG(dbgs() << "irce: giving up constraining loop, too large\n");
  1615. return false;
  1616. }
  1617. BasicBlock *Preheader = L->getLoopPreheader();
  1618. if (!Preheader) {
  1619. LLVM_DEBUG(dbgs() << "irce: loop has no preheader, leaving\n");
  1620. return false;
  1621. }
  1622. LLVMContext &Context = Preheader->getContext();
  1623. SmallVector<InductiveRangeCheck, 16> RangeChecks;
  1624. for (auto BBI : L->getBlocks())
  1625. if (BranchInst *TBI = dyn_cast<BranchInst>(BBI->getTerminator()))
  1626. InductiveRangeCheck::extractRangeChecksFromBranch(TBI, L, SE, BPI,
  1627. RangeChecks);
  1628. if (RangeChecks.empty())
  1629. return false;
  1630. auto PrintRecognizedRangeChecks = [&](raw_ostream &OS) {
  1631. OS << "irce: looking at loop "; L->print(OS);
  1632. OS << "irce: loop has " << RangeChecks.size()
  1633. << " inductive range checks: \n";
  1634. for (InductiveRangeCheck &IRC : RangeChecks)
  1635. IRC.print(OS);
  1636. };
  1637. LLVM_DEBUG(PrintRecognizedRangeChecks(dbgs()));
  1638. if (PrintRangeChecks)
  1639. PrintRecognizedRangeChecks(errs());
  1640. const char *FailureReason = nullptr;
  1641. Optional<LoopStructure> MaybeLoopStructure =
  1642. LoopStructure::parseLoopStructure(SE, *L, FailureReason);
  1643. if (!MaybeLoopStructure.hasValue()) {
  1644. LLVM_DEBUG(dbgs() << "irce: could not parse loop structure: "
  1645. << FailureReason << "\n";);
  1646. return false;
  1647. }
  1648. LoopStructure LS = MaybeLoopStructure.getValue();
  1649. if (!isProfitableToTransform(*L, LS))
  1650. return false;
  1651. const SCEVAddRecExpr *IndVar =
  1652. cast<SCEVAddRecExpr>(SE.getMinusSCEV(SE.getSCEV(LS.IndVarBase), SE.getSCEV(LS.IndVarStep)));
  1653. Optional<InductiveRangeCheck::Range> SafeIterRange;
  1654. Instruction *ExprInsertPt = Preheader->getTerminator();
  1655. SmallVector<InductiveRangeCheck, 4> RangeChecksToEliminate;
  1656. // Basing on the type of latch predicate, we interpret the IV iteration range
  1657. // as signed or unsigned range. We use different min/max functions (signed or
  1658. // unsigned) when intersecting this range with safe iteration ranges implied
  1659. // by range checks.
  1660. auto IntersectRange =
  1661. LS.IsSignedPredicate ? IntersectSignedRange : IntersectUnsignedRange;
  1662. IRBuilder<> B(ExprInsertPt);
  1663. for (InductiveRangeCheck &IRC : RangeChecks) {
  1664. auto Result = IRC.computeSafeIterationSpace(SE, IndVar,
  1665. LS.IsSignedPredicate);
  1666. if (Result.hasValue()) {
  1667. auto MaybeSafeIterRange =
  1668. IntersectRange(SE, SafeIterRange, Result.getValue());
  1669. if (MaybeSafeIterRange.hasValue()) {
  1670. assert(
  1671. !MaybeSafeIterRange.getValue().isEmpty(SE, LS.IsSignedPredicate) &&
  1672. "We should never return empty ranges!");
  1673. RangeChecksToEliminate.push_back(IRC);
  1674. SafeIterRange = MaybeSafeIterRange.getValue();
  1675. }
  1676. }
  1677. }
  1678. if (!SafeIterRange.hasValue())
  1679. return false;
  1680. LoopConstrainer LC(*L, LI, LPMAddNewLoop, LS, SE, DT,
  1681. SafeIterRange.getValue());
  1682. bool Changed = LC.run();
  1683. if (Changed) {
  1684. auto PrintConstrainedLoopInfo = [L]() {
  1685. dbgs() << "irce: in function ";
  1686. dbgs() << L->getHeader()->getParent()->getName() << ": ";
  1687. dbgs() << "constrained ";
  1688. L->print(dbgs());
  1689. };
  1690. LLVM_DEBUG(PrintConstrainedLoopInfo());
  1691. if (PrintChangedLoops)
  1692. PrintConstrainedLoopInfo();
  1693. // Optimize away the now-redundant range checks.
  1694. for (InductiveRangeCheck &IRC : RangeChecksToEliminate) {
  1695. ConstantInt *FoldedRangeCheck = IRC.getPassingDirection()
  1696. ? ConstantInt::getTrue(Context)
  1697. : ConstantInt::getFalse(Context);
  1698. IRC.getCheckUse()->set(FoldedRangeCheck);
  1699. }
  1700. }
  1701. return Changed;
  1702. }
  1703. Pass *llvm::createInductiveRangeCheckEliminationPass() {
  1704. return new IRCELegacyPass();
  1705. }