LoopInfoTest.cpp 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  1. //===- LoopInfoTest.cpp - LoopInfo unit tests -----------------------------===//
  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. #include "llvm/Analysis/LoopInfo.h"
  9. #include "llvm/Analysis/AssumptionCache.h"
  10. #include "llvm/Analysis/ScalarEvolution.h"
  11. #include "llvm/Analysis/TargetLibraryInfo.h"
  12. #include "llvm/AsmParser/Parser.h"
  13. #include "llvm/IR/Dominators.h"
  14. #include "llvm/Support/SourceMgr.h"
  15. #include "gtest/gtest.h"
  16. using namespace llvm;
  17. /// Build the loop info for the function and run the Test.
  18. static void
  19. runWithLoopInfo(Module &M, StringRef FuncName,
  20. function_ref<void(Function &F, LoopInfo &LI)> Test) {
  21. auto *F = M.getFunction(FuncName);
  22. ASSERT_NE(F, nullptr) << "Could not find " << FuncName;
  23. // Compute the dominator tree and the loop info for the function.
  24. DominatorTree DT(*F);
  25. LoopInfo LI(DT);
  26. Test(*F, LI);
  27. }
  28. /// Build the loop info and scalar evolution for the function and run the Test.
  29. static void runWithLoopInfoPlus(
  30. Module &M, StringRef FuncName,
  31. function_ref<void(Function &F, LoopInfo &LI, ScalarEvolution &SE)> Test) {
  32. auto *F = M.getFunction(FuncName);
  33. ASSERT_NE(F, nullptr) << "Could not find " << FuncName;
  34. TargetLibraryInfoImpl TLII;
  35. TargetLibraryInfo TLI(TLII);
  36. AssumptionCache AC(*F);
  37. DominatorTree DT(*F);
  38. LoopInfo LI(DT);
  39. ScalarEvolution SE(*F, TLI, AC, DT, LI);
  40. Test(*F, LI, SE);
  41. }
  42. static std::unique_ptr<Module> makeLLVMModule(LLVMContext &Context,
  43. const char *ModuleStr) {
  44. SMDiagnostic Err;
  45. return parseAssemblyString(ModuleStr, Err, Context);
  46. }
  47. // This tests that for a loop with a single latch, we get the loop id from
  48. // its only latch, even in case the loop may not be in a simplified form.
  49. TEST(LoopInfoTest, LoopWithSingleLatch) {
  50. const char *ModuleStr =
  51. "target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n"
  52. "define void @foo(i32 %n) {\n"
  53. "entry:\n"
  54. " br i1 undef, label %for.cond, label %for.end\n"
  55. "for.cond:\n"
  56. " %i.0 = phi i32 [ 0, %entry ], [ %inc, %for.inc ]\n"
  57. " %cmp = icmp slt i32 %i.0, %n\n"
  58. " br i1 %cmp, label %for.inc, label %for.end\n"
  59. "for.inc:\n"
  60. " %inc = add nsw i32 %i.0, 1\n"
  61. " br label %for.cond, !llvm.loop !0\n"
  62. "for.end:\n"
  63. " ret void\n"
  64. "}\n"
  65. "!0 = distinct !{!0, !1}\n"
  66. "!1 = !{!\"llvm.loop.distribute.enable\", i1 true}\n";
  67. // Parse the module.
  68. LLVMContext Context;
  69. std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
  70. runWithLoopInfo(*M, "foo", [&](Function &F, LoopInfo &LI) {
  71. Function::iterator FI = F.begin();
  72. // First basic block is entry - skip it.
  73. BasicBlock *Header = &*(++FI);
  74. assert(Header->getName() == "for.cond");
  75. Loop *L = LI.getLoopFor(Header);
  76. // This loop is not in simplified form.
  77. EXPECT_FALSE(L->isLoopSimplifyForm());
  78. // Analyze the loop metadata id.
  79. bool loopIDFoundAndSet = false;
  80. // Try to get and set the metadata id for the loop.
  81. if (MDNode *D = L->getLoopID()) {
  82. L->setLoopID(D);
  83. loopIDFoundAndSet = true;
  84. }
  85. // We must have successfully found and set the loop id in the
  86. // only latch the loop has.
  87. EXPECT_TRUE(loopIDFoundAndSet);
  88. });
  89. }
  90. // Test loop id handling for a loop with multiple latches.
  91. TEST(LoopInfoTest, LoopWithMultipleLatches) {
  92. const char *ModuleStr =
  93. "target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n"
  94. "define void @foo(i32 %n) {\n"
  95. "entry:\n"
  96. " br i1 undef, label %for.cond, label %for.end\n"
  97. "for.cond:\n"
  98. " %i.0 = phi i32 [ 0, %entry ], [ %inc, %latch.1 ], [ %inc, %latch.2 ]\n"
  99. " %inc = add nsw i32 %i.0, 1\n"
  100. " %cmp = icmp slt i32 %i.0, %n\n"
  101. " br i1 %cmp, label %latch.1, label %for.end\n"
  102. "latch.1:\n"
  103. " br i1 undef, label %for.cond, label %latch.2, !llvm.loop !0\n"
  104. "latch.2:\n"
  105. " br label %for.cond, !llvm.loop !0\n"
  106. "for.end:\n"
  107. " ret void\n"
  108. "}\n"
  109. "!0 = distinct !{!0, !1}\n"
  110. "!1 = !{!\"llvm.loop.distribute.enable\", i1 true}\n";
  111. // Parse the module.
  112. LLVMContext Context;
  113. std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
  114. runWithLoopInfo(*M, "foo", [&](Function &F, LoopInfo &LI) {
  115. Function::iterator FI = F.begin();
  116. // First basic block is entry - skip it.
  117. BasicBlock *Header = &*(++FI);
  118. assert(Header->getName() == "for.cond");
  119. Loop *L = LI.getLoopFor(Header);
  120. EXPECT_NE(L, nullptr);
  121. // This loop is not in simplified form.
  122. EXPECT_FALSE(L->isLoopSimplifyForm());
  123. // Try to get and set the metadata id for the loop.
  124. MDNode *OldLoopID = L->getLoopID();
  125. EXPECT_NE(OldLoopID, nullptr);
  126. MDNode *NewLoopID = MDNode::get(Context, {nullptr});
  127. // Set operand 0 to refer to the loop id itself.
  128. NewLoopID->replaceOperandWith(0, NewLoopID);
  129. L->setLoopID(NewLoopID);
  130. EXPECT_EQ(L->getLoopID(), NewLoopID);
  131. EXPECT_NE(L->getLoopID(), OldLoopID);
  132. L->setLoopID(OldLoopID);
  133. EXPECT_EQ(L->getLoopID(), OldLoopID);
  134. EXPECT_NE(L->getLoopID(), NewLoopID);
  135. });
  136. }
  137. TEST(LoopInfoTest, PreorderTraversals) {
  138. const char *ModuleStr = "define void @f() {\n"
  139. "entry:\n"
  140. " br label %loop.0\n"
  141. "loop.0:\n"
  142. " br i1 undef, label %loop.0.0, label %loop.1\n"
  143. "loop.0.0:\n"
  144. " br i1 undef, label %loop.0.0, label %loop.0.1\n"
  145. "loop.0.1:\n"
  146. " br i1 undef, label %loop.0.1, label %loop.0.2\n"
  147. "loop.0.2:\n"
  148. " br i1 undef, label %loop.0.2, label %loop.0\n"
  149. "loop.1:\n"
  150. " br i1 undef, label %loop.1.0, label %end\n"
  151. "loop.1.0:\n"
  152. " br i1 undef, label %loop.1.0, label %loop.1.1\n"
  153. "loop.1.1:\n"
  154. " br i1 undef, label %loop.1.1, label %loop.1.2\n"
  155. "loop.1.2:\n"
  156. " br i1 undef, label %loop.1.2, label %loop.1\n"
  157. "end:\n"
  158. " ret void\n"
  159. "}\n";
  160. // Parse the module.
  161. LLVMContext Context;
  162. std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
  163. Function &F = *M->begin();
  164. DominatorTree DT(F);
  165. LoopInfo LI;
  166. LI.analyze(DT);
  167. Function::iterator I = F.begin();
  168. ASSERT_EQ("entry", I->getName());
  169. ++I;
  170. Loop &L_0 = *LI.getLoopFor(&*I++);
  171. ASSERT_EQ("loop.0", L_0.getHeader()->getName());
  172. Loop &L_0_0 = *LI.getLoopFor(&*I++);
  173. ASSERT_EQ("loop.0.0", L_0_0.getHeader()->getName());
  174. Loop &L_0_1 = *LI.getLoopFor(&*I++);
  175. ASSERT_EQ("loop.0.1", L_0_1.getHeader()->getName());
  176. Loop &L_0_2 = *LI.getLoopFor(&*I++);
  177. ASSERT_EQ("loop.0.2", L_0_2.getHeader()->getName());
  178. Loop &L_1 = *LI.getLoopFor(&*I++);
  179. ASSERT_EQ("loop.1", L_1.getHeader()->getName());
  180. Loop &L_1_0 = *LI.getLoopFor(&*I++);
  181. ASSERT_EQ("loop.1.0", L_1_0.getHeader()->getName());
  182. Loop &L_1_1 = *LI.getLoopFor(&*I++);
  183. ASSERT_EQ("loop.1.1", L_1_1.getHeader()->getName());
  184. Loop &L_1_2 = *LI.getLoopFor(&*I++);
  185. ASSERT_EQ("loop.1.2", L_1_2.getHeader()->getName());
  186. auto Preorder = LI.getLoopsInPreorder();
  187. ASSERT_EQ(8u, Preorder.size());
  188. EXPECT_EQ(&L_0, Preorder[0]);
  189. EXPECT_EQ(&L_0_0, Preorder[1]);
  190. EXPECT_EQ(&L_0_1, Preorder[2]);
  191. EXPECT_EQ(&L_0_2, Preorder[3]);
  192. EXPECT_EQ(&L_1, Preorder[4]);
  193. EXPECT_EQ(&L_1_0, Preorder[5]);
  194. EXPECT_EQ(&L_1_1, Preorder[6]);
  195. EXPECT_EQ(&L_1_2, Preorder[7]);
  196. auto ReverseSiblingPreorder = LI.getLoopsInReverseSiblingPreorder();
  197. ASSERT_EQ(8u, ReverseSiblingPreorder.size());
  198. EXPECT_EQ(&L_1, ReverseSiblingPreorder[0]);
  199. EXPECT_EQ(&L_1_2, ReverseSiblingPreorder[1]);
  200. EXPECT_EQ(&L_1_1, ReverseSiblingPreorder[2]);
  201. EXPECT_EQ(&L_1_0, ReverseSiblingPreorder[3]);
  202. EXPECT_EQ(&L_0, ReverseSiblingPreorder[4]);
  203. EXPECT_EQ(&L_0_2, ReverseSiblingPreorder[5]);
  204. EXPECT_EQ(&L_0_1, ReverseSiblingPreorder[6]);
  205. EXPECT_EQ(&L_0_0, ReverseSiblingPreorder[7]);
  206. }
  207. TEST(LoopInfoTest, CanonicalLoop) {
  208. const char *ModuleStr =
  209. "define void @foo(i32* %A, i32 %ub) {\n"
  210. "entry:\n"
  211. " %guardcmp = icmp slt i32 0, %ub\n"
  212. " br i1 %guardcmp, label %for.preheader, label %for.end\n"
  213. "for.preheader:\n"
  214. " br label %for.body\n"
  215. "for.body:\n"
  216. " %i = phi i32 [ 0, %for.preheader ], [ %inc, %for.body ]\n"
  217. " %idxprom = sext i32 %i to i64\n"
  218. " %arrayidx = getelementptr inbounds i32, i32* %A, i64 %idxprom\n"
  219. " store i32 %i, i32* %arrayidx, align 4\n"
  220. " %inc = add nsw i32 %i, 1\n"
  221. " %cmp = icmp slt i32 %inc, %ub\n"
  222. " br i1 %cmp, label %for.body, label %for.exit\n"
  223. "for.exit:\n"
  224. " br label %for.end\n"
  225. "for.end:\n"
  226. " ret void\n"
  227. "}\n";
  228. // Parse the module.
  229. LLVMContext Context;
  230. std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
  231. runWithLoopInfoPlus(
  232. *M, "foo",
  233. [&](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
  234. Function::iterator FI = F.begin();
  235. BasicBlock *Entry = &*(FI);
  236. BranchInst *Guard = dyn_cast<BranchInst>(Entry->getTerminator());
  237. // First two basic block are entry and for.preheader - skip them.
  238. ++FI;
  239. BasicBlock *Header = &*(++FI);
  240. assert(Header->getName() == "for.body");
  241. Loop *L = LI.getLoopFor(Header);
  242. EXPECT_NE(L, nullptr);
  243. Optional<Loop::LoopBounds> Bounds = L->getBounds(SE);
  244. EXPECT_NE(Bounds, None);
  245. ConstantInt *InitialIVValue =
  246. dyn_cast<ConstantInt>(&Bounds->getInitialIVValue());
  247. EXPECT_TRUE(InitialIVValue && InitialIVValue->isZero());
  248. EXPECT_EQ(Bounds->getStepInst().getName(), "inc");
  249. ConstantInt *StepValue =
  250. dyn_cast_or_null<ConstantInt>(Bounds->getStepValue());
  251. EXPECT_TRUE(StepValue && StepValue->isOne());
  252. EXPECT_EQ(Bounds->getFinalIVValue().getName(), "ub");
  253. EXPECT_EQ(Bounds->getCanonicalPredicate(), ICmpInst::ICMP_SLT);
  254. EXPECT_EQ(Bounds->getDirection(),
  255. Loop::LoopBounds::Direction::Increasing);
  256. EXPECT_EQ(L->getInductionVariable(SE)->getName(), "i");
  257. EXPECT_EQ(L->getLoopGuardBranch(), Guard);
  258. EXPECT_TRUE(L->isGuarded());
  259. });
  260. }
  261. TEST(LoopInfoTest, LoopWithInverseGuardSuccs) {
  262. const char *ModuleStr =
  263. "define void @foo(i32* %A, i32 %ub) {\n"
  264. "entry:\n"
  265. " %guardcmp = icmp sge i32 0, %ub\n"
  266. " br i1 %guardcmp, label %for.end, label %for.preheader\n"
  267. "for.preheader:\n"
  268. " br label %for.body\n"
  269. "for.body:\n"
  270. " %i = phi i32 [ 0, %for.preheader ], [ %inc, %for.body ]\n"
  271. " %idxprom = sext i32 %i to i64\n"
  272. " %arrayidx = getelementptr inbounds i32, i32* %A, i64 %idxprom\n"
  273. " store i32 %i, i32* %arrayidx, align 4\n"
  274. " %inc = add nsw i32 %i, 1\n"
  275. " %cmp = icmp slt i32 %inc, %ub\n"
  276. " br i1 %cmp, label %for.body, label %for.exit\n"
  277. "for.exit:\n"
  278. " br label %for.end\n"
  279. "for.end:\n"
  280. " ret void\n"
  281. "}\n";
  282. // Parse the module.
  283. LLVMContext Context;
  284. std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
  285. runWithLoopInfoPlus(
  286. *M, "foo",
  287. [&](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
  288. Function::iterator FI = F.begin();
  289. BasicBlock *Entry = &*(FI);
  290. BranchInst *Guard = dyn_cast<BranchInst>(Entry->getTerminator());
  291. // First two basic block are entry and for.preheader - skip them.
  292. ++FI;
  293. BasicBlock *Header = &*(++FI);
  294. assert(Header->getName() == "for.body");
  295. Loop *L = LI.getLoopFor(Header);
  296. EXPECT_NE(L, nullptr);
  297. Optional<Loop::LoopBounds> Bounds = L->getBounds(SE);
  298. EXPECT_NE(Bounds, None);
  299. ConstantInt *InitialIVValue =
  300. dyn_cast<ConstantInt>(&Bounds->getInitialIVValue());
  301. EXPECT_TRUE(InitialIVValue && InitialIVValue->isZero());
  302. EXPECT_EQ(Bounds->getStepInst().getName(), "inc");
  303. ConstantInt *StepValue =
  304. dyn_cast_or_null<ConstantInt>(Bounds->getStepValue());
  305. EXPECT_TRUE(StepValue && StepValue->isOne());
  306. EXPECT_EQ(Bounds->getFinalIVValue().getName(), "ub");
  307. EXPECT_EQ(Bounds->getCanonicalPredicate(), ICmpInst::ICMP_SLT);
  308. EXPECT_EQ(Bounds->getDirection(),
  309. Loop::LoopBounds::Direction::Increasing);
  310. EXPECT_EQ(L->getInductionVariable(SE)->getName(), "i");
  311. EXPECT_EQ(L->getLoopGuardBranch(), Guard);
  312. EXPECT_TRUE(L->isGuarded());
  313. });
  314. }
  315. TEST(LoopInfoTest, LoopWithSwappedGuardCmp) {
  316. const char *ModuleStr =
  317. "define void @foo(i32* %A, i32 %ub) {\n"
  318. "entry:\n"
  319. " %guardcmp = icmp sgt i32 %ub, 0\n"
  320. " br i1 %guardcmp, label %for.preheader, label %for.end\n"
  321. "for.preheader:\n"
  322. " br label %for.body\n"
  323. "for.body:\n"
  324. " %i = phi i32 [ 0, %for.preheader ], [ %inc, %for.body ]\n"
  325. " %idxprom = sext i32 %i to i64\n"
  326. " %arrayidx = getelementptr inbounds i32, i32* %A, i64 %idxprom\n"
  327. " store i32 %i, i32* %arrayidx, align 4\n"
  328. " %inc = add nsw i32 %i, 1\n"
  329. " %cmp = icmp sge i32 %inc, %ub\n"
  330. " br i1 %cmp, label %for.exit, label %for.body\n"
  331. "for.exit:\n"
  332. " br label %for.end\n"
  333. "for.end:\n"
  334. " ret void\n"
  335. "}\n";
  336. // Parse the module.
  337. LLVMContext Context;
  338. std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
  339. runWithLoopInfoPlus(
  340. *M, "foo",
  341. [&](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
  342. Function::iterator FI = F.begin();
  343. BasicBlock *Entry = &*(FI);
  344. BranchInst *Guard = dyn_cast<BranchInst>(Entry->getTerminator());
  345. // First two basic block are entry and for.preheader - skip them.
  346. ++FI;
  347. BasicBlock *Header = &*(++FI);
  348. assert(Header->getName() == "for.body");
  349. Loop *L = LI.getLoopFor(Header);
  350. EXPECT_NE(L, nullptr);
  351. Optional<Loop::LoopBounds> Bounds = L->getBounds(SE);
  352. EXPECT_NE(Bounds, None);
  353. ConstantInt *InitialIVValue =
  354. dyn_cast<ConstantInt>(&Bounds->getInitialIVValue());
  355. EXPECT_TRUE(InitialIVValue && InitialIVValue->isZero());
  356. EXPECT_EQ(Bounds->getStepInst().getName(), "inc");
  357. ConstantInt *StepValue =
  358. dyn_cast_or_null<ConstantInt>(Bounds->getStepValue());
  359. EXPECT_TRUE(StepValue && StepValue->isOne());
  360. EXPECT_EQ(Bounds->getFinalIVValue().getName(), "ub");
  361. EXPECT_EQ(Bounds->getCanonicalPredicate(), ICmpInst::ICMP_SLT);
  362. EXPECT_EQ(Bounds->getDirection(),
  363. Loop::LoopBounds::Direction::Increasing);
  364. EXPECT_EQ(L->getInductionVariable(SE)->getName(), "i");
  365. EXPECT_EQ(L->getLoopGuardBranch(), Guard);
  366. EXPECT_TRUE(L->isGuarded());
  367. });
  368. }
  369. TEST(LoopInfoTest, LoopWithInverseLatchSuccs) {
  370. const char *ModuleStr =
  371. "define void @foo(i32* %A, i32 %ub) {\n"
  372. "entry:\n"
  373. " %guardcmp = icmp slt i32 0, %ub\n"
  374. " br i1 %guardcmp, label %for.preheader, label %for.end\n"
  375. "for.preheader:\n"
  376. " br label %for.body\n"
  377. "for.body:\n"
  378. " %i = phi i32 [ 0, %for.preheader ], [ %inc, %for.body ]\n"
  379. " %idxprom = sext i32 %i to i64\n"
  380. " %arrayidx = getelementptr inbounds i32, i32* %A, i64 %idxprom\n"
  381. " store i32 %i, i32* %arrayidx, align 4\n"
  382. " %inc = add nsw i32 %i, 1\n"
  383. " %cmp = icmp sge i32 %inc, %ub\n"
  384. " br i1 %cmp, label %for.exit, label %for.body\n"
  385. "for.exit:\n"
  386. " br label %for.end\n"
  387. "for.end:\n"
  388. " ret void\n"
  389. "}\n";
  390. // Parse the module.
  391. LLVMContext Context;
  392. std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
  393. runWithLoopInfoPlus(
  394. *M, "foo",
  395. [&](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
  396. Function::iterator FI = F.begin();
  397. BasicBlock *Entry = &*(FI);
  398. BranchInst *Guard = dyn_cast<BranchInst>(Entry->getTerminator());
  399. // First two basic block are entry and for.preheader - skip them.
  400. ++FI;
  401. BasicBlock *Header = &*(++FI);
  402. assert(Header->getName() == "for.body");
  403. Loop *L = LI.getLoopFor(Header);
  404. EXPECT_NE(L, nullptr);
  405. Optional<Loop::LoopBounds> Bounds = L->getBounds(SE);
  406. EXPECT_NE(Bounds, None);
  407. ConstantInt *InitialIVValue =
  408. dyn_cast<ConstantInt>(&Bounds->getInitialIVValue());
  409. EXPECT_TRUE(InitialIVValue && InitialIVValue->isZero());
  410. EXPECT_EQ(Bounds->getStepInst().getName(), "inc");
  411. ConstantInt *StepValue =
  412. dyn_cast_or_null<ConstantInt>(Bounds->getStepValue());
  413. EXPECT_TRUE(StepValue && StepValue->isOne());
  414. EXPECT_EQ(Bounds->getFinalIVValue().getName(), "ub");
  415. EXPECT_EQ(Bounds->getCanonicalPredicate(), ICmpInst::ICMP_SLT);
  416. EXPECT_EQ(Bounds->getDirection(),
  417. Loop::LoopBounds::Direction::Increasing);
  418. EXPECT_EQ(L->getInductionVariable(SE)->getName(), "i");
  419. EXPECT_EQ(L->getLoopGuardBranch(), Guard);
  420. EXPECT_TRUE(L->isGuarded());
  421. });
  422. }
  423. TEST(LoopInfoTest, LoopWithLatchCmpNE) {
  424. const char *ModuleStr =
  425. "define void @foo(i32* %A, i32 %ub) {\n"
  426. "entry:\n"
  427. " %guardcmp = icmp slt i32 0, %ub\n"
  428. " br i1 %guardcmp, label %for.preheader, label %for.end\n"
  429. "for.preheader:\n"
  430. " br label %for.body\n"
  431. "for.body:\n"
  432. " %i = phi i32 [ 0, %for.preheader ], [ %inc, %for.body ]\n"
  433. " %idxprom = sext i32 %i to i64\n"
  434. " %arrayidx = getelementptr inbounds i32, i32* %A, i64 %idxprom\n"
  435. " store i32 %i, i32* %arrayidx, align 4\n"
  436. " %inc = add nsw i32 %i, 1\n"
  437. " %cmp = icmp ne i32 %i, %ub\n"
  438. " br i1 %cmp, label %for.body, label %for.exit\n"
  439. "for.exit:\n"
  440. " br label %for.end\n"
  441. "for.end:\n"
  442. " ret void\n"
  443. "}\n";
  444. // Parse the module.
  445. LLVMContext Context;
  446. std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
  447. runWithLoopInfoPlus(
  448. *M, "foo",
  449. [&](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
  450. Function::iterator FI = F.begin();
  451. BasicBlock *Entry = &*(FI);
  452. BranchInst *Guard = dyn_cast<BranchInst>(Entry->getTerminator());
  453. // First two basic block are entry and for.preheader - skip them.
  454. ++FI;
  455. BasicBlock *Header = &*(++FI);
  456. assert(Header->getName() == "for.body");
  457. Loop *L = LI.getLoopFor(Header);
  458. EXPECT_NE(L, nullptr);
  459. Optional<Loop::LoopBounds> Bounds = L->getBounds(SE);
  460. EXPECT_NE(Bounds, None);
  461. ConstantInt *InitialIVValue =
  462. dyn_cast<ConstantInt>(&Bounds->getInitialIVValue());
  463. EXPECT_TRUE(InitialIVValue && InitialIVValue->isZero());
  464. EXPECT_EQ(Bounds->getStepInst().getName(), "inc");
  465. ConstantInt *StepValue =
  466. dyn_cast_or_null<ConstantInt>(Bounds->getStepValue());
  467. EXPECT_TRUE(StepValue && StepValue->isOne());
  468. EXPECT_EQ(Bounds->getFinalIVValue().getName(), "ub");
  469. EXPECT_EQ(Bounds->getCanonicalPredicate(), ICmpInst::ICMP_SLT);
  470. EXPECT_EQ(Bounds->getDirection(),
  471. Loop::LoopBounds::Direction::Increasing);
  472. EXPECT_EQ(L->getInductionVariable(SE)->getName(), "i");
  473. EXPECT_EQ(L->getLoopGuardBranch(), Guard);
  474. EXPECT_TRUE(L->isGuarded());
  475. });
  476. }
  477. TEST(LoopInfoTest, LoopWithGuardCmpSLE) {
  478. const char *ModuleStr =
  479. "define void @foo(i32* %A, i32 %ub) {\n"
  480. "entry:\n"
  481. " %ubPlusOne = add i32 %ub, 1\n"
  482. " %guardcmp = icmp sle i32 0, %ub\n"
  483. " br i1 %guardcmp, label %for.preheader, label %for.end\n"
  484. "for.preheader:\n"
  485. " br label %for.body\n"
  486. "for.body:\n"
  487. " %i = phi i32 [ 0, %for.preheader ], [ %inc, %for.body ]\n"
  488. " %idxprom = sext i32 %i to i64\n"
  489. " %arrayidx = getelementptr inbounds i32, i32* %A, i64 %idxprom\n"
  490. " store i32 %i, i32* %arrayidx, align 4\n"
  491. " %inc = add nsw i32 %i, 1\n"
  492. " %cmp = icmp ne i32 %i, %ubPlusOne\n"
  493. " br i1 %cmp, label %for.body, label %for.exit\n"
  494. "for.exit:\n"
  495. " br label %for.end\n"
  496. "for.end:\n"
  497. " ret void\n"
  498. "}\n";
  499. // Parse the module.
  500. LLVMContext Context;
  501. std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
  502. runWithLoopInfoPlus(
  503. *M, "foo",
  504. [&](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
  505. Function::iterator FI = F.begin();
  506. BasicBlock *Entry = &*(FI);
  507. BranchInst *Guard = dyn_cast<BranchInst>(Entry->getTerminator());
  508. // First two basic block are entry and for.preheader - skip them.
  509. ++FI;
  510. BasicBlock *Header = &*(++FI);
  511. assert(Header->getName() == "for.body");
  512. Loop *L = LI.getLoopFor(Header);
  513. EXPECT_NE(L, nullptr);
  514. Optional<Loop::LoopBounds> Bounds = L->getBounds(SE);
  515. EXPECT_NE(Bounds, None);
  516. ConstantInt *InitialIVValue =
  517. dyn_cast<ConstantInt>(&Bounds->getInitialIVValue());
  518. EXPECT_TRUE(InitialIVValue && InitialIVValue->isZero());
  519. EXPECT_EQ(Bounds->getStepInst().getName(), "inc");
  520. ConstantInt *StepValue =
  521. dyn_cast_or_null<ConstantInt>(Bounds->getStepValue());
  522. EXPECT_TRUE(StepValue && StepValue->isOne());
  523. EXPECT_EQ(Bounds->getFinalIVValue().getName(), "ubPlusOne");
  524. EXPECT_EQ(Bounds->getCanonicalPredicate(), ICmpInst::ICMP_SLT);
  525. EXPECT_EQ(Bounds->getDirection(),
  526. Loop::LoopBounds::Direction::Increasing);
  527. EXPECT_EQ(L->getInductionVariable(SE)->getName(), "i");
  528. EXPECT_EQ(L->getLoopGuardBranch(), Guard);
  529. EXPECT_TRUE(L->isGuarded());
  530. });
  531. }
  532. TEST(LoopInfoTest, LoopNonConstantStep) {
  533. const char *ModuleStr =
  534. "define void @foo(i32* %A, i32 %ub, i32 %step) {\n"
  535. "entry:\n"
  536. " %guardcmp = icmp slt i32 0, %ub\n"
  537. " br i1 %guardcmp, label %for.preheader, label %for.end\n"
  538. "for.preheader:\n"
  539. " br label %for.body\n"
  540. "for.body:\n"
  541. " %i = phi i32 [ 0, %for.preheader ], [ %inc, %for.body ]\n"
  542. " %idxprom = zext i32 %i to i64\n"
  543. " %arrayidx = getelementptr inbounds i32, i32* %A, i64 %idxprom\n"
  544. " store i32 %i, i32* %arrayidx, align 4\n"
  545. " %inc = add nsw i32 %i, %step\n"
  546. " %cmp = icmp slt i32 %inc, %ub\n"
  547. " br i1 %cmp, label %for.body, label %for.exit\n"
  548. "for.exit:\n"
  549. " br label %for.end\n"
  550. "for.end:\n"
  551. " ret void\n"
  552. "}\n";
  553. // Parse the module.
  554. LLVMContext Context;
  555. std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
  556. runWithLoopInfoPlus(
  557. *M, "foo",
  558. [&](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
  559. Function::iterator FI = F.begin();
  560. BasicBlock *Entry = &*(FI);
  561. BranchInst *Guard = dyn_cast<BranchInst>(Entry->getTerminator());
  562. // First two basic block are entry and for.preheader - skip them.
  563. ++FI;
  564. BasicBlock *Header = &*(++FI);
  565. assert(Header->getName() == "for.body");
  566. Loop *L = LI.getLoopFor(Header);
  567. EXPECT_NE(L, nullptr);
  568. Optional<Loop::LoopBounds> Bounds = L->getBounds(SE);
  569. EXPECT_NE(Bounds, None);
  570. ConstantInt *InitialIVValue =
  571. dyn_cast<ConstantInt>(&Bounds->getInitialIVValue());
  572. EXPECT_TRUE(InitialIVValue && InitialIVValue->isZero());
  573. EXPECT_EQ(Bounds->getStepInst().getName(), "inc");
  574. EXPECT_EQ(Bounds->getStepValue()->getName(), "step");
  575. EXPECT_EQ(Bounds->getFinalIVValue().getName(), "ub");
  576. EXPECT_EQ(Bounds->getCanonicalPredicate(), ICmpInst::ICMP_SLT);
  577. EXPECT_EQ(Bounds->getDirection(), Loop::LoopBounds::Direction::Unknown);
  578. EXPECT_EQ(L->getInductionVariable(SE)->getName(), "i");
  579. EXPECT_EQ(L->getLoopGuardBranch(), Guard);
  580. EXPECT_TRUE(L->isGuarded());
  581. });
  582. }
  583. TEST(LoopInfoTest, LoopUnsignedBounds) {
  584. const char *ModuleStr =
  585. "define void @foo(i32* %A, i32 %ub) {\n"
  586. "entry:\n"
  587. " %guardcmp = icmp ult i32 0, %ub\n"
  588. " br i1 %guardcmp, label %for.preheader, label %for.end\n"
  589. "for.preheader:\n"
  590. " br label %for.body\n"
  591. "for.body:\n"
  592. " %i = phi i32 [ 0, %for.preheader ], [ %inc, %for.body ]\n"
  593. " %idxprom = zext i32 %i to i64\n"
  594. " %arrayidx = getelementptr inbounds i32, i32* %A, i64 %idxprom\n"
  595. " store i32 %i, i32* %arrayidx, align 4\n"
  596. " %inc = add i32 %i, 1\n"
  597. " %cmp = icmp ult i32 %inc, %ub\n"
  598. " br i1 %cmp, label %for.body, label %for.exit\n"
  599. "for.exit:\n"
  600. " br label %for.end\n"
  601. "for.end:\n"
  602. " ret void\n"
  603. "}\n";
  604. // Parse the module.
  605. LLVMContext Context;
  606. std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
  607. runWithLoopInfoPlus(
  608. *M, "foo",
  609. [&](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
  610. Function::iterator FI = F.begin();
  611. BasicBlock *Entry = &*(FI);
  612. BranchInst *Guard = dyn_cast<BranchInst>(Entry->getTerminator());
  613. // First two basic block are entry and for.preheader - skip them.
  614. ++FI;
  615. BasicBlock *Header = &*(++FI);
  616. assert(Header->getName() == "for.body");
  617. Loop *L = LI.getLoopFor(Header);
  618. EXPECT_NE(L, nullptr);
  619. Optional<Loop::LoopBounds> Bounds = L->getBounds(SE);
  620. EXPECT_NE(Bounds, None);
  621. ConstantInt *InitialIVValue =
  622. dyn_cast<ConstantInt>(&Bounds->getInitialIVValue());
  623. EXPECT_TRUE(InitialIVValue && InitialIVValue->isZero());
  624. EXPECT_EQ(Bounds->getStepInst().getName(), "inc");
  625. ConstantInt *StepValue =
  626. dyn_cast_or_null<ConstantInt>(Bounds->getStepValue());
  627. EXPECT_TRUE(StepValue && StepValue->isOne());
  628. EXPECT_EQ(Bounds->getFinalIVValue().getName(), "ub");
  629. EXPECT_EQ(Bounds->getCanonicalPredicate(), ICmpInst::ICMP_ULT);
  630. EXPECT_EQ(Bounds->getDirection(),
  631. Loop::LoopBounds::Direction::Increasing);
  632. EXPECT_EQ(L->getInductionVariable(SE)->getName(), "i");
  633. EXPECT_EQ(L->getLoopGuardBranch(), Guard);
  634. EXPECT_TRUE(L->isGuarded());
  635. });
  636. }
  637. TEST(LoopInfoTest, DecreasingLoop) {
  638. const char *ModuleStr =
  639. "define void @foo(i32* %A, i32 %ub) {\n"
  640. "entry:\n"
  641. " %guardcmp = icmp slt i32 0, %ub\n"
  642. " br i1 %guardcmp, label %for.preheader, label %for.end\n"
  643. "for.preheader:\n"
  644. " br label %for.body\n"
  645. "for.body:\n"
  646. " %i = phi i32 [ %ub, %for.preheader ], [ %inc, %for.body ]\n"
  647. " %idxprom = sext i32 %i to i64\n"
  648. " %arrayidx = getelementptr inbounds i32, i32* %A, i64 %idxprom\n"
  649. " store i32 %i, i32* %arrayidx, align 4\n"
  650. " %inc = sub nsw i32 %i, 1\n"
  651. " %cmp = icmp sgt i32 %inc, 0\n"
  652. " br i1 %cmp, label %for.body, label %for.exit\n"
  653. "for.exit:\n"
  654. " br label %for.end\n"
  655. "for.end:\n"
  656. " ret void\n"
  657. "}\n";
  658. // Parse the module.
  659. LLVMContext Context;
  660. std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
  661. runWithLoopInfoPlus(
  662. *M, "foo",
  663. [&](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
  664. Function::iterator FI = F.begin();
  665. BasicBlock *Entry = &*(FI);
  666. BranchInst *Guard = dyn_cast<BranchInst>(Entry->getTerminator());
  667. // First two basic block are entry and for.preheader - skip them.
  668. ++FI;
  669. BasicBlock *Header = &*(++FI);
  670. assert(Header->getName() == "for.body");
  671. Loop *L = LI.getLoopFor(Header);
  672. EXPECT_NE(L, nullptr);
  673. Optional<Loop::LoopBounds> Bounds = L->getBounds(SE);
  674. EXPECT_NE(Bounds, None);
  675. EXPECT_EQ(Bounds->getInitialIVValue().getName(), "ub");
  676. EXPECT_EQ(Bounds->getStepInst().getName(), "inc");
  677. ConstantInt *StepValue =
  678. dyn_cast_or_null<ConstantInt>(Bounds->getStepValue());
  679. EXPECT_EQ(StepValue, nullptr);
  680. ConstantInt *FinalIVValue =
  681. dyn_cast<ConstantInt>(&Bounds->getFinalIVValue());
  682. EXPECT_TRUE(FinalIVValue && FinalIVValue->isZero());
  683. EXPECT_EQ(Bounds->getCanonicalPredicate(), ICmpInst::ICMP_SGT);
  684. EXPECT_EQ(Bounds->getDirection(),
  685. Loop::LoopBounds::Direction::Decreasing);
  686. EXPECT_EQ(L->getInductionVariable(SE)->getName(), "i");
  687. EXPECT_EQ(L->getLoopGuardBranch(), Guard);
  688. EXPECT_TRUE(L->isGuarded());
  689. });
  690. }
  691. TEST(LoopInfoTest, CannotFindDirection) {
  692. const char *ModuleStr =
  693. "define void @foo(i32* %A, i32 %ub, i32 %step) {\n"
  694. "entry:\n"
  695. " %guardcmp = icmp slt i32 0, %ub\n"
  696. " br i1 %guardcmp, label %for.preheader, label %for.end\n"
  697. "for.preheader:\n"
  698. " br label %for.body\n"
  699. "for.body:\n"
  700. " %i = phi i32 [ 0, %for.preheader ], [ %inc, %for.body ]\n"
  701. " %idxprom = sext i32 %i to i64\n"
  702. " %arrayidx = getelementptr inbounds i32, i32* %A, i64 %idxprom\n"
  703. " store i32 %i, i32* %arrayidx, align 4\n"
  704. " %inc = add nsw i32 %i, %step\n"
  705. " %cmp = icmp ne i32 %i, %ub\n"
  706. " br i1 %cmp, label %for.body, label %for.exit\n"
  707. "for.exit:\n"
  708. " br label %for.end\n"
  709. "for.end:\n"
  710. " ret void\n"
  711. "}\n";
  712. // Parse the module.
  713. LLVMContext Context;
  714. std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
  715. runWithLoopInfoPlus(
  716. *M, "foo",
  717. [&](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
  718. Function::iterator FI = F.begin();
  719. BasicBlock *Entry = &*(FI);
  720. BranchInst *Guard = dyn_cast<BranchInst>(Entry->getTerminator());
  721. // First two basic block are entry and for.preheader
  722. // - skip them.
  723. ++FI;
  724. BasicBlock *Header = &*(++FI);
  725. assert(Header->getName() == "for.body");
  726. Loop *L = LI.getLoopFor(Header);
  727. EXPECT_NE(L, nullptr);
  728. Optional<Loop::LoopBounds> Bounds = L->getBounds(SE);
  729. EXPECT_NE(Bounds, None);
  730. ConstantInt *InitialIVValue =
  731. dyn_cast<ConstantInt>(&Bounds->getInitialIVValue());
  732. EXPECT_TRUE(InitialIVValue && InitialIVValue->isZero());
  733. EXPECT_EQ(Bounds->getStepInst().getName(), "inc");
  734. EXPECT_EQ(Bounds->getStepValue()->getName(), "step");
  735. EXPECT_EQ(Bounds->getFinalIVValue().getName(), "ub");
  736. EXPECT_EQ(Bounds->getCanonicalPredicate(),
  737. ICmpInst::BAD_ICMP_PREDICATE);
  738. EXPECT_EQ(Bounds->getDirection(), Loop::LoopBounds::Direction::Unknown);
  739. EXPECT_EQ(L->getInductionVariable(SE)->getName(), "i");
  740. EXPECT_EQ(L->getLoopGuardBranch(), Guard);
  741. EXPECT_TRUE(L->isGuarded());
  742. });
  743. }
  744. TEST(LoopInfoTest, ZextIndVar) {
  745. const char *ModuleStr =
  746. "define void @foo(i32* %A, i32 %ub) {\n"
  747. "entry:\n"
  748. " %guardcmp = icmp slt i32 0, %ub\n"
  749. " br i1 %guardcmp, label %for.preheader, label %for.end\n"
  750. "for.preheader:\n"
  751. " br label %for.body\n"
  752. "for.body:\n"
  753. " %indvars.iv = phi i64 [ 0, %for.preheader ], [ %indvars.iv.next, %for.body ]\n"
  754. " %i = phi i32 [ 0, %for.preheader ], [ %inc, %for.body ]\n"
  755. " %idxprom = sext i32 %i to i64\n"
  756. " %arrayidx = getelementptr inbounds i32, i32* %A, i64 %idxprom\n"
  757. " store i32 %i, i32* %arrayidx, align 4\n"
  758. " %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1\n"
  759. " %inc = add nsw i32 %i, 1\n"
  760. " %wide.trip.count = zext i32 %ub to i64\n"
  761. " %exitcond = icmp ne i64 %indvars.iv.next, %wide.trip.count\n"
  762. " br i1 %exitcond, label %for.body, label %for.exit\n"
  763. "for.exit:\n"
  764. " br label %for.end\n"
  765. "for.end:\n"
  766. " ret void\n"
  767. "}\n";
  768. // Parse the module.
  769. LLVMContext Context;
  770. std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
  771. runWithLoopInfoPlus(
  772. *M, "foo",
  773. [&](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
  774. Function::iterator FI = F.begin();
  775. BasicBlock *Entry = &*(FI);
  776. BranchInst *Guard = dyn_cast<BranchInst>(Entry->getTerminator());
  777. // First two basic block are entry and for.preheader - skip them.
  778. ++FI;
  779. BasicBlock *Header = &*(++FI);
  780. assert(Header->getName() == "for.body");
  781. Loop *L = LI.getLoopFor(Header);
  782. EXPECT_NE(L, nullptr);
  783. Optional<Loop::LoopBounds> Bounds = L->getBounds(SE);
  784. EXPECT_NE(Bounds, None);
  785. ConstantInt *InitialIVValue =
  786. dyn_cast<ConstantInt>(&Bounds->getInitialIVValue());
  787. EXPECT_TRUE(InitialIVValue && InitialIVValue->isZero());
  788. EXPECT_EQ(Bounds->getStepInst().getName(), "indvars.iv.next");
  789. ConstantInt *StepValue =
  790. dyn_cast_or_null<ConstantInt>(Bounds->getStepValue());
  791. EXPECT_TRUE(StepValue && StepValue->isOne());
  792. EXPECT_EQ(Bounds->getFinalIVValue().getName(), "wide.trip.count");
  793. EXPECT_EQ(Bounds->getCanonicalPredicate(), ICmpInst::ICMP_NE);
  794. EXPECT_EQ(Bounds->getDirection(),
  795. Loop::LoopBounds::Direction::Increasing);
  796. EXPECT_EQ(L->getInductionVariable(SE)->getName(), "indvars.iv");
  797. EXPECT_EQ(L->getLoopGuardBranch(), Guard);
  798. EXPECT_TRUE(L->isGuarded());
  799. });
  800. }
  801. TEST(LoopInfoTest, UnguardedLoop) {
  802. const char *ModuleStr =
  803. "define void @foo(i32* %A, i32 %ub) {\n"
  804. "entry:\n"
  805. " br label %for.body\n"
  806. "for.body:\n"
  807. " %i = phi i32 [ 0, %entry ], [ %inc, %for.body ]\n"
  808. " %idxprom = sext i32 %i to i64\n"
  809. " %arrayidx = getelementptr inbounds i32, i32* %A, i64 %idxprom\n"
  810. " store i32 %i, i32* %arrayidx, align 4\n"
  811. " %inc = add nsw i32 %i, 1\n"
  812. " %cmp = icmp slt i32 %inc, %ub\n"
  813. " br i1 %cmp, label %for.body, label %for.exit\n"
  814. "for.exit:\n"
  815. " br label %for.end\n"
  816. "for.end:\n"
  817. " ret void\n"
  818. "}\n";
  819. // Parse the module.
  820. LLVMContext Context;
  821. std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
  822. runWithLoopInfoPlus(
  823. *M, "foo",
  824. [&](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
  825. Function::iterator FI = F.begin();
  826. // First basic block is entry - skip it.
  827. BasicBlock *Header = &*(++FI);
  828. assert(Header->getName() == "for.body");
  829. Loop *L = LI.getLoopFor(Header);
  830. EXPECT_NE(L, nullptr);
  831. Optional<Loop::LoopBounds> Bounds = L->getBounds(SE);
  832. EXPECT_NE(Bounds, None);
  833. ConstantInt *InitialIVValue =
  834. dyn_cast<ConstantInt>(&Bounds->getInitialIVValue());
  835. EXPECT_TRUE(InitialIVValue && InitialIVValue->isZero());
  836. EXPECT_EQ(Bounds->getStepInst().getName(), "inc");
  837. ConstantInt *StepValue =
  838. dyn_cast_or_null<ConstantInt>(Bounds->getStepValue());
  839. EXPECT_TRUE(StepValue && StepValue->isOne());
  840. EXPECT_EQ(Bounds->getFinalIVValue().getName(), "ub");
  841. EXPECT_EQ(Bounds->getCanonicalPredicate(), ICmpInst::ICMP_SLT);
  842. EXPECT_EQ(Bounds->getDirection(),
  843. Loop::LoopBounds::Direction::Increasing);
  844. EXPECT_EQ(L->getInductionVariable(SE)->getName(), "i");
  845. EXPECT_EQ(L->getLoopGuardBranch(), nullptr);
  846. EXPECT_FALSE(L->isGuarded());
  847. });
  848. }
  849. TEST(LoopInfoTest, UnguardedLoopWithControlFlow) {
  850. const char *ModuleStr =
  851. "define void @foo(i32* %A, i32 %ub, i1 %cond) {\n"
  852. "entry:\n"
  853. " br i1 %cond, label %for.preheader, label %for.end\n"
  854. "for.preheader:\n"
  855. " br label %for.body\n"
  856. "for.body:\n"
  857. " %i = phi i32 [ 0, %for.preheader ], [ %inc, %for.body ]\n"
  858. " %idxprom = sext i32 %i to i64\n"
  859. " %arrayidx = getelementptr inbounds i32, i32* %A, i64 %idxprom\n"
  860. " store i32 %i, i32* %arrayidx, align 4\n"
  861. " %inc = add nsw i32 %i, 1\n"
  862. " %cmp = icmp slt i32 %inc, %ub\n"
  863. " br i1 %cmp, label %for.body, label %for.exit\n"
  864. "for.exit:\n"
  865. " br label %for.end\n"
  866. "for.end:\n"
  867. " ret void\n"
  868. "}\n";
  869. // Parse the module.
  870. LLVMContext Context;
  871. std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
  872. runWithLoopInfoPlus(
  873. *M, "foo",
  874. [&](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
  875. Function::iterator FI = F.begin();
  876. BasicBlock *Entry = &*(FI);
  877. BranchInst *Guard = dyn_cast<BranchInst>(Entry->getTerminator());
  878. // First two basic block are entry and for.preheader - skip them.
  879. ++FI;
  880. BasicBlock *Header = &*(++FI);
  881. assert(Header->getName() == "for.body");
  882. Loop *L = LI.getLoopFor(Header);
  883. EXPECT_NE(L, nullptr);
  884. Optional<Loop::LoopBounds> Bounds = L->getBounds(SE);
  885. EXPECT_NE(Bounds, None);
  886. ConstantInt *InitialIVValue =
  887. dyn_cast<ConstantInt>(&Bounds->getInitialIVValue());
  888. EXPECT_TRUE(InitialIVValue && InitialIVValue->isZero());
  889. EXPECT_EQ(Bounds->getStepInst().getName(), "inc");
  890. ConstantInt *StepValue =
  891. dyn_cast_or_null<ConstantInt>(Bounds->getStepValue());
  892. EXPECT_TRUE(StepValue && StepValue->isOne());
  893. EXPECT_EQ(Bounds->getFinalIVValue().getName(), "ub");
  894. EXPECT_EQ(Bounds->getCanonicalPredicate(), ICmpInst::ICMP_SLT);
  895. EXPECT_EQ(Bounds->getDirection(),
  896. Loop::LoopBounds::Direction::Increasing);
  897. EXPECT_EQ(L->getInductionVariable(SE)->getName(), "i");
  898. EXPECT_EQ(L->getLoopGuardBranch(), Guard);
  899. EXPECT_TRUE(L->isGuarded());
  900. });
  901. }
  902. TEST(LoopInfoTest, LoopNest) {
  903. const char *ModuleStr =
  904. "define void @foo(i32* %A, i32 %ub) {\n"
  905. "entry:\n"
  906. " %guardcmp = icmp slt i32 0, %ub\n"
  907. " br i1 %guardcmp, label %for.outer.preheader, label %for.end\n"
  908. "for.outer.preheader:\n"
  909. " br label %for.outer\n"
  910. "for.outer:\n"
  911. " %j = phi i32 [ 0, %for.outer.preheader ], [ %inc.outer, %for.outer.latch ]\n"
  912. " br i1 %guardcmp, label %for.inner.preheader, label %for.outer.latch\n"
  913. "for.inner.preheader:\n"
  914. " br label %for.inner\n"
  915. "for.inner:\n"
  916. " %i = phi i32 [ 0, %for.inner.preheader ], [ %inc, %for.inner ]\n"
  917. " %idxprom = sext i32 %i to i64\n"
  918. " %arrayidx = getelementptr inbounds i32, i32* %A, i64 %idxprom\n"
  919. " store i32 %i, i32* %arrayidx, align 4\n"
  920. " %inc = add nsw i32 %i, 1\n"
  921. " %cmp = icmp slt i32 %inc, %ub\n"
  922. " br i1 %cmp, label %for.inner, label %for.inner.exit\n"
  923. "for.inner.exit:\n"
  924. " br label %for.outer.latch\n"
  925. "for.outer.latch:\n"
  926. " %inc.outer = add nsw i32 %j, 1\n"
  927. " %cmp.outer = icmp slt i32 %inc.outer, %ub\n"
  928. " br i1 %cmp.outer, label %for.outer, label %for.outer.exit\n"
  929. "for.outer.exit:\n"
  930. " br label %for.end\n"
  931. "for.end:\n"
  932. " ret void\n"
  933. "}\n";
  934. // Parse the module.
  935. LLVMContext Context;
  936. std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
  937. runWithLoopInfoPlus(
  938. *M, "foo",
  939. [&](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
  940. Function::iterator FI = F.begin();
  941. BasicBlock *Entry = &*(FI);
  942. BranchInst *OuterGuard = dyn_cast<BranchInst>(Entry->getTerminator());
  943. // First two basic block are entry and for.outer.preheader - skip them.
  944. ++FI;
  945. BasicBlock *Header = &*(++FI);
  946. assert(Header->getName() == "for.outer");
  947. BranchInst *InnerGuard = dyn_cast<BranchInst>(Header->getTerminator());
  948. Loop *L = LI.getLoopFor(Header);
  949. EXPECT_NE(L, nullptr);
  950. Optional<Loop::LoopBounds> Bounds = L->getBounds(SE);
  951. EXPECT_NE(Bounds, None);
  952. ConstantInt *InitialIVValue =
  953. dyn_cast<ConstantInt>(&Bounds->getInitialIVValue());
  954. EXPECT_TRUE(InitialIVValue && InitialIVValue->isZero());
  955. EXPECT_EQ(Bounds->getStepInst().getName(), "inc.outer");
  956. ConstantInt *StepValue =
  957. dyn_cast_or_null<ConstantInt>(Bounds->getStepValue());
  958. EXPECT_TRUE(StepValue && StepValue->isOne());
  959. EXPECT_EQ(Bounds->getFinalIVValue().getName(), "ub");
  960. EXPECT_EQ(Bounds->getCanonicalPredicate(), ICmpInst::ICMP_SLT);
  961. EXPECT_EQ(Bounds->getDirection(),
  962. Loop::LoopBounds::Direction::Increasing);
  963. EXPECT_EQ(L->getInductionVariable(SE)->getName(), "j");
  964. EXPECT_EQ(L->getLoopGuardBranch(), OuterGuard);
  965. EXPECT_TRUE(L->isGuarded());
  966. // Next two basic blocks are for.outer and for.inner.preheader - skip
  967. // them.
  968. ++FI;
  969. Header = &*(++FI);
  970. assert(Header->getName() == "for.inner");
  971. L = LI.getLoopFor(Header);
  972. EXPECT_NE(L, nullptr);
  973. Optional<Loop::LoopBounds> InnerBounds = L->getBounds(SE);
  974. EXPECT_NE(InnerBounds, None);
  975. InitialIVValue =
  976. dyn_cast<ConstantInt>(&InnerBounds->getInitialIVValue());
  977. EXPECT_TRUE(InitialIVValue && InitialIVValue->isZero());
  978. EXPECT_EQ(InnerBounds->getStepInst().getName(), "inc");
  979. StepValue = dyn_cast_or_null<ConstantInt>(InnerBounds->getStepValue());
  980. EXPECT_TRUE(StepValue && StepValue->isOne());
  981. EXPECT_EQ(InnerBounds->getFinalIVValue().getName(), "ub");
  982. EXPECT_EQ(InnerBounds->getCanonicalPredicate(), ICmpInst::ICMP_SLT);
  983. EXPECT_EQ(InnerBounds->getDirection(),
  984. Loop::LoopBounds::Direction::Increasing);
  985. EXPECT_EQ(L->getInductionVariable(SE)->getName(), "i");
  986. EXPECT_EQ(L->getLoopGuardBranch(), InnerGuard);
  987. EXPECT_TRUE(L->isGuarded());
  988. });
  989. }
  990. TEST(LoopInfoTest, AuxiliaryIV) {
  991. const char *ModuleStr =
  992. "define void @foo(i32* %A, i32 %ub) {\n"
  993. "entry:\n"
  994. " %guardcmp = icmp slt i32 0, %ub\n"
  995. " br i1 %guardcmp, label %for.preheader, label %for.end\n"
  996. "for.preheader:\n"
  997. " br label %for.body\n"
  998. "for.body:\n"
  999. " %i = phi i32 [ 0, %for.preheader ], [ %inc, %for.body ]\n"
  1000. " %aux = phi i32 [ 0, %for.preheader ], [ %auxinc, %for.body ]\n"
  1001. " %loopvariant = phi i32 [ 0, %for.preheader ], [ %loopvariantinc, %for.body ]\n"
  1002. " %usedoutside = phi i32 [ 0, %for.preheader ], [ %usedoutsideinc, %for.body ]\n"
  1003. " %mulopcode = phi i32 [ 0, %for.preheader ], [ %mulopcodeinc, %for.body ]\n"
  1004. " %idxprom = sext i32 %i to i64\n"
  1005. " %arrayidx = getelementptr inbounds i32, i32* %A, i64 %idxprom\n"
  1006. " store i32 %i, i32* %arrayidx, align 4\n"
  1007. " %mulopcodeinc = mul nsw i32 %mulopcode, 5\n"
  1008. " %usedoutsideinc = add nsw i32 %usedoutside, 5\n"
  1009. " %loopvariantinc = add nsw i32 %loopvariant, %i\n"
  1010. " %auxinc = add nsw i32 %aux, 5\n"
  1011. " %inc = add nsw i32 %i, 1\n"
  1012. " %cmp = icmp slt i32 %inc, %ub\n"
  1013. " br i1 %cmp, label %for.body, label %for.exit\n"
  1014. "for.exit:\n"
  1015. " %lcssa = phi i32 [ %usedoutside, %for.body ]\n"
  1016. " br label %for.end\n"
  1017. "for.end:\n"
  1018. " ret void\n"
  1019. "}\n";
  1020. // Parse the module.
  1021. LLVMContext Context;
  1022. std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
  1023. runWithLoopInfoPlus(
  1024. *M, "foo",
  1025. [&](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
  1026. Function::iterator FI = F.begin();
  1027. BasicBlock *Entry = &*(FI);
  1028. BranchInst *Guard = dyn_cast<BranchInst>(Entry->getTerminator());
  1029. // First two basic block are entry and for.preheader - skip them.
  1030. ++FI;
  1031. BasicBlock *Header = &*(++FI);
  1032. assert(Header->getName() == "for.body");
  1033. Loop *L = LI.getLoopFor(Header);
  1034. EXPECT_NE(L, nullptr);
  1035. Optional<Loop::LoopBounds> Bounds = L->getBounds(SE);
  1036. EXPECT_NE(Bounds, None);
  1037. ConstantInt *InitialIVValue =
  1038. dyn_cast<ConstantInt>(&Bounds->getInitialIVValue());
  1039. EXPECT_TRUE(InitialIVValue && InitialIVValue->isZero());
  1040. EXPECT_EQ(Bounds->getStepInst().getName(), "inc");
  1041. ConstantInt *StepValue =
  1042. dyn_cast_or_null<ConstantInt>(Bounds->getStepValue());
  1043. EXPECT_TRUE(StepValue && StepValue->isOne());
  1044. EXPECT_EQ(Bounds->getFinalIVValue().getName(), "ub");
  1045. EXPECT_EQ(Bounds->getCanonicalPredicate(), ICmpInst::ICMP_SLT);
  1046. EXPECT_EQ(Bounds->getDirection(),
  1047. Loop::LoopBounds::Direction::Increasing);
  1048. EXPECT_EQ(L->getInductionVariable(SE)->getName(), "i");
  1049. BasicBlock::iterator II = Header->begin();
  1050. PHINode &Instruction_i = cast<PHINode>(*(II));
  1051. EXPECT_TRUE(L->isAuxiliaryInductionVariable(Instruction_i, SE));
  1052. PHINode &Instruction_aux = cast<PHINode>(*(++II));
  1053. EXPECT_TRUE(L->isAuxiliaryInductionVariable(Instruction_aux, SE));
  1054. PHINode &Instruction_loopvariant = cast<PHINode>(*(++II));
  1055. EXPECT_FALSE(
  1056. L->isAuxiliaryInductionVariable(Instruction_loopvariant, SE));
  1057. PHINode &Instruction_usedoutside = cast<PHINode>(*(++II));
  1058. EXPECT_FALSE(
  1059. L->isAuxiliaryInductionVariable(Instruction_usedoutside, SE));
  1060. PHINode &Instruction_mulopcode = cast<PHINode>(*(++II));
  1061. EXPECT_FALSE(
  1062. L->isAuxiliaryInductionVariable(Instruction_mulopcode, SE));
  1063. EXPECT_EQ(L->getLoopGuardBranch(), Guard);
  1064. EXPECT_TRUE(L->isGuarded());
  1065. });
  1066. }
  1067. // Examine getUniqueExitBlocks/getUniqueNonLatchExitBlocks functions.
  1068. TEST(LoopInfoTest, LoopUniqueExitBlocks) {
  1069. const char *ModuleStr =
  1070. "target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n"
  1071. "define void @foo(i32 %n, i1 %cond) {\n"
  1072. "entry:\n"
  1073. " br label %for.cond\n"
  1074. "for.cond:\n"
  1075. " %i.0 = phi i32 [ 0, %entry ], [ %inc, %for.inc ]\n"
  1076. " %cmp = icmp slt i32 %i.0, %n\n"
  1077. " br i1 %cond, label %for.inc, label %for.end1\n"
  1078. "for.inc:\n"
  1079. " %inc = add nsw i32 %i.0, 1\n"
  1080. " br i1 %cmp, label %for.cond, label %for.end2, !llvm.loop !0\n"
  1081. "for.end1:\n"
  1082. " br label %for.end\n"
  1083. "for.end2:\n"
  1084. " br label %for.end\n"
  1085. "for.end:\n"
  1086. " ret void\n"
  1087. "}\n"
  1088. "!0 = distinct !{!0, !1}\n"
  1089. "!1 = !{!\"llvm.loop.distribute.enable\", i1 true}\n";
  1090. // Parse the module.
  1091. LLVMContext Context;
  1092. std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
  1093. runWithLoopInfo(*M, "foo", [&](Function &F, LoopInfo &LI) {
  1094. Function::iterator FI = F.begin();
  1095. // First basic block is entry - skip it.
  1096. BasicBlock *Header = &*(++FI);
  1097. assert(Header->getName() == "for.cond");
  1098. Loop *L = LI.getLoopFor(Header);
  1099. SmallVector<BasicBlock *, 2> Exits;
  1100. // This loop has 2 unique exits.
  1101. L->getUniqueExitBlocks(Exits);
  1102. EXPECT_TRUE(Exits.size() == 2);
  1103. // And one unique non latch exit.
  1104. Exits.clear();
  1105. L->getUniqueNonLatchExitBlocks(Exits);
  1106. EXPECT_TRUE(Exits.size() == 1);
  1107. });
  1108. }
  1109. // Regression test for getUniqueNonLatchExitBlocks functions.
  1110. // It should detect the exit if it comes from both latch and non-latch blocks.
  1111. TEST(LoopInfoTest, LoopNonLatchUniqueExitBlocks) {
  1112. const char *ModuleStr =
  1113. "target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n"
  1114. "define void @foo(i32 %n, i1 %cond) {\n"
  1115. "entry:\n"
  1116. " br label %for.cond\n"
  1117. "for.cond:\n"
  1118. " %i.0 = phi i32 [ 0, %entry ], [ %inc, %for.inc ]\n"
  1119. " %cmp = icmp slt i32 %i.0, %n\n"
  1120. " br i1 %cond, label %for.inc, label %for.end\n"
  1121. "for.inc:\n"
  1122. " %inc = add nsw i32 %i.0, 1\n"
  1123. " br i1 %cmp, label %for.cond, label %for.end, !llvm.loop !0\n"
  1124. "for.end:\n"
  1125. " ret void\n"
  1126. "}\n"
  1127. "!0 = distinct !{!0, !1}\n"
  1128. "!1 = !{!\"llvm.loop.distribute.enable\", i1 true}\n";
  1129. // Parse the module.
  1130. LLVMContext Context;
  1131. std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
  1132. runWithLoopInfo(*M, "foo", [&](Function &F, LoopInfo &LI) {
  1133. Function::iterator FI = F.begin();
  1134. // First basic block is entry - skip it.
  1135. BasicBlock *Header = &*(++FI);
  1136. assert(Header->getName() == "for.cond");
  1137. Loop *L = LI.getLoopFor(Header);
  1138. SmallVector<BasicBlock *, 2> Exits;
  1139. // This loop has 1 unique exit.
  1140. L->getUniqueExitBlocks(Exits);
  1141. EXPECT_TRUE(Exits.size() == 1);
  1142. // And one unique non latch exit.
  1143. Exits.clear();
  1144. L->getUniqueNonLatchExitBlocks(Exits);
  1145. EXPECT_TRUE(Exits.size() == 1);
  1146. });
  1147. }