LoopInfoTest.cpp 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447
  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, MultiExitingLoop) {
  802. const char *ModuleStr =
  803. "define void @foo(i32* %A, i32 %ub, i1 %cond) {\n"
  804. "entry:\n"
  805. " %guardcmp = icmp slt i32 0, %ub\n"
  806. " br i1 %guardcmp, label %for.preheader, label %for.end\n"
  807. "for.preheader:\n"
  808. " br label %for.body\n"
  809. "for.body:\n"
  810. " %i = phi i32 [ 0, %for.preheader ], [ %inc, %for.body.1 ]\n"
  811. " br i1 %cond, label %for.body.1, label %for.exit\n"
  812. "for.body.1:\n"
  813. " %idxprom = sext i32 %i to i64\n"
  814. " %arrayidx = getelementptr inbounds i32, i32* %A, i64 %idxprom\n"
  815. " store i32 %i, i32* %arrayidx, align 4\n"
  816. " %inc = add nsw i32 %i, 1\n"
  817. " %cmp = icmp slt i32 %inc, %ub\n"
  818. " br i1 %cmp, label %for.body, label %for.exit\n"
  819. "for.exit:\n"
  820. " br label %for.end\n"
  821. "for.end:\n"
  822. " ret void\n"
  823. "}\n";
  824. // Parse the module.
  825. LLVMContext Context;
  826. std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
  827. runWithLoopInfoPlus(
  828. *M, "foo",
  829. [&](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
  830. Function::iterator FI = F.begin();
  831. BasicBlock *Entry = &*(FI);
  832. BranchInst *Guard = dyn_cast<BranchInst>(Entry->getTerminator());
  833. // First two basic block are entry and for.preheader - skip them.
  834. ++FI;
  835. BasicBlock *Header = &*(++FI);
  836. assert(Header->getName() == "for.body");
  837. Loop *L = LI.getLoopFor(Header);
  838. EXPECT_NE(L, nullptr);
  839. Optional<Loop::LoopBounds> Bounds = L->getBounds(SE);
  840. EXPECT_NE(Bounds, None);
  841. ConstantInt *InitialIVValue =
  842. dyn_cast<ConstantInt>(&Bounds->getInitialIVValue());
  843. EXPECT_TRUE(InitialIVValue && InitialIVValue->isZero());
  844. EXPECT_EQ(Bounds->getStepInst().getName(), "inc");
  845. ConstantInt *StepValue =
  846. dyn_cast_or_null<ConstantInt>(Bounds->getStepValue());
  847. EXPECT_TRUE(StepValue && StepValue->isOne());
  848. EXPECT_EQ(Bounds->getFinalIVValue().getName(), "ub");
  849. EXPECT_EQ(Bounds->getCanonicalPredicate(), ICmpInst::ICMP_SLT);
  850. EXPECT_EQ(Bounds->getDirection(),
  851. Loop::LoopBounds::Direction::Increasing);
  852. EXPECT_EQ(L->getInductionVariable(SE)->getName(), "i");
  853. EXPECT_EQ(L->getLoopGuardBranch(), Guard);
  854. EXPECT_TRUE(L->isGuarded());
  855. });
  856. }
  857. TEST(LoopInfoTest, MultiExitLoop) {
  858. const char *ModuleStr =
  859. "define void @foo(i32* %A, i32 %ub, i1 %cond) {\n"
  860. "entry:\n"
  861. " %guardcmp = icmp slt i32 0, %ub\n"
  862. " br i1 %guardcmp, label %for.preheader, label %for.end\n"
  863. "for.preheader:\n"
  864. " br label %for.body\n"
  865. "for.body:\n"
  866. " %i = phi i32 [ 0, %for.preheader ], [ %inc, %for.body.1 ]\n"
  867. " br i1 %cond, label %for.body.1, label %for.exit\n"
  868. "for.body.1:\n"
  869. " %idxprom = sext i32 %i to i64\n"
  870. " %arrayidx = getelementptr inbounds i32, i32* %A, i64 %idxprom\n"
  871. " store i32 %i, i32* %arrayidx, align 4\n"
  872. " %inc = add nsw i32 %i, 1\n"
  873. " %cmp = icmp slt i32 %inc, %ub\n"
  874. " br i1 %cmp, label %for.body, label %for.exit.1\n"
  875. "for.exit:\n"
  876. " br label %for.end\n"
  877. "for.exit.1:\n"
  878. " br label %for.end\n"
  879. "for.end:\n"
  880. " ret void\n"
  881. "}\n";
  882. // Parse the module.
  883. LLVMContext Context;
  884. std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
  885. runWithLoopInfoPlus(
  886. *M, "foo",
  887. [&](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
  888. Function::iterator FI = F.begin();
  889. // First two basic block are entry and for.preheader - skip them.
  890. ++FI;
  891. BasicBlock *Header = &*(++FI);
  892. assert(Header->getName() == "for.body");
  893. Loop *L = LI.getLoopFor(Header);
  894. EXPECT_NE(L, nullptr);
  895. Optional<Loop::LoopBounds> Bounds = L->getBounds(SE);
  896. EXPECT_NE(Bounds, None);
  897. ConstantInt *InitialIVValue =
  898. dyn_cast<ConstantInt>(&Bounds->getInitialIVValue());
  899. EXPECT_TRUE(InitialIVValue && InitialIVValue->isZero());
  900. EXPECT_EQ(Bounds->getStepInst().getName(), "inc");
  901. ConstantInt *StepValue =
  902. dyn_cast_or_null<ConstantInt>(Bounds->getStepValue());
  903. EXPECT_TRUE(StepValue && StepValue->isOne());
  904. EXPECT_EQ(Bounds->getFinalIVValue().getName(), "ub");
  905. EXPECT_EQ(Bounds->getCanonicalPredicate(), ICmpInst::ICMP_SLT);
  906. EXPECT_EQ(Bounds->getDirection(),
  907. Loop::LoopBounds::Direction::Increasing);
  908. EXPECT_EQ(L->getInductionVariable(SE)->getName(), "i");
  909. EXPECT_EQ(L->getLoopGuardBranch(), nullptr);
  910. EXPECT_FALSE(L->isGuarded());
  911. });
  912. }
  913. TEST(LoopInfoTest, UnguardedLoop) {
  914. const char *ModuleStr =
  915. "define void @foo(i32* %A, i32 %ub) {\n"
  916. "entry:\n"
  917. " br label %for.body\n"
  918. "for.body:\n"
  919. " %i = phi i32 [ 0, %entry ], [ %inc, %for.body ]\n"
  920. " %idxprom = sext i32 %i to i64\n"
  921. " %arrayidx = getelementptr inbounds i32, i32* %A, i64 %idxprom\n"
  922. " store i32 %i, i32* %arrayidx, align 4\n"
  923. " %inc = add nsw i32 %i, 1\n"
  924. " %cmp = icmp slt i32 %inc, %ub\n"
  925. " br i1 %cmp, label %for.body, label %for.exit\n"
  926. "for.exit:\n"
  927. " br label %for.end\n"
  928. "for.end:\n"
  929. " ret void\n"
  930. "}\n";
  931. // Parse the module.
  932. LLVMContext Context;
  933. std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
  934. runWithLoopInfoPlus(
  935. *M, "foo",
  936. [&](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
  937. Function::iterator FI = F.begin();
  938. // First basic block is entry - skip it.
  939. BasicBlock *Header = &*(++FI);
  940. assert(Header->getName() == "for.body");
  941. Loop *L = LI.getLoopFor(Header);
  942. EXPECT_NE(L, nullptr);
  943. Optional<Loop::LoopBounds> Bounds = L->getBounds(SE);
  944. EXPECT_NE(Bounds, None);
  945. ConstantInt *InitialIVValue =
  946. dyn_cast<ConstantInt>(&Bounds->getInitialIVValue());
  947. EXPECT_TRUE(InitialIVValue && InitialIVValue->isZero());
  948. EXPECT_EQ(Bounds->getStepInst().getName(), "inc");
  949. ConstantInt *StepValue =
  950. dyn_cast_or_null<ConstantInt>(Bounds->getStepValue());
  951. EXPECT_TRUE(StepValue && StepValue->isOne());
  952. EXPECT_EQ(Bounds->getFinalIVValue().getName(), "ub");
  953. EXPECT_EQ(Bounds->getCanonicalPredicate(), ICmpInst::ICMP_SLT);
  954. EXPECT_EQ(Bounds->getDirection(),
  955. Loop::LoopBounds::Direction::Increasing);
  956. EXPECT_EQ(L->getInductionVariable(SE)->getName(), "i");
  957. EXPECT_EQ(L->getLoopGuardBranch(), nullptr);
  958. EXPECT_FALSE(L->isGuarded());
  959. });
  960. }
  961. TEST(LoopInfoTest, UnguardedLoopWithControlFlow) {
  962. const char *ModuleStr =
  963. "define void @foo(i32* %A, i32 %ub, i1 %cond) {\n"
  964. "entry:\n"
  965. " br i1 %cond, label %for.preheader, label %for.end\n"
  966. "for.preheader:\n"
  967. " br label %for.body\n"
  968. "for.body:\n"
  969. " %i = phi i32 [ 0, %for.preheader ], [ %inc, %for.body ]\n"
  970. " %idxprom = sext i32 %i to i64\n"
  971. " %arrayidx = getelementptr inbounds i32, i32* %A, i64 %idxprom\n"
  972. " store i32 %i, i32* %arrayidx, align 4\n"
  973. " %inc = add nsw i32 %i, 1\n"
  974. " %cmp = icmp slt i32 %inc, %ub\n"
  975. " br i1 %cmp, label %for.body, label %for.exit\n"
  976. "for.exit:\n"
  977. " br label %for.end\n"
  978. "for.end:\n"
  979. " ret void\n"
  980. "}\n";
  981. // Parse the module.
  982. LLVMContext Context;
  983. std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
  984. runWithLoopInfoPlus(
  985. *M, "foo",
  986. [&](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
  987. Function::iterator FI = F.begin();
  988. BasicBlock *Entry = &*(FI);
  989. BranchInst *Guard = dyn_cast<BranchInst>(Entry->getTerminator());
  990. // First two basic block are entry and for.preheader - skip them.
  991. ++FI;
  992. BasicBlock *Header = &*(++FI);
  993. assert(Header->getName() == "for.body");
  994. Loop *L = LI.getLoopFor(Header);
  995. EXPECT_NE(L, nullptr);
  996. Optional<Loop::LoopBounds> Bounds = L->getBounds(SE);
  997. EXPECT_NE(Bounds, None);
  998. ConstantInt *InitialIVValue =
  999. dyn_cast<ConstantInt>(&Bounds->getInitialIVValue());
  1000. EXPECT_TRUE(InitialIVValue && InitialIVValue->isZero());
  1001. EXPECT_EQ(Bounds->getStepInst().getName(), "inc");
  1002. ConstantInt *StepValue =
  1003. dyn_cast_or_null<ConstantInt>(Bounds->getStepValue());
  1004. EXPECT_TRUE(StepValue && StepValue->isOne());
  1005. EXPECT_EQ(Bounds->getFinalIVValue().getName(), "ub");
  1006. EXPECT_EQ(Bounds->getCanonicalPredicate(), ICmpInst::ICMP_SLT);
  1007. EXPECT_EQ(Bounds->getDirection(),
  1008. Loop::LoopBounds::Direction::Increasing);
  1009. EXPECT_EQ(L->getInductionVariable(SE)->getName(), "i");
  1010. EXPECT_EQ(L->getLoopGuardBranch(), Guard);
  1011. EXPECT_TRUE(L->isGuarded());
  1012. });
  1013. }
  1014. TEST(LoopInfoTest, LoopNest) {
  1015. const char *ModuleStr =
  1016. "define void @foo(i32* %A, i32 %ub) {\n"
  1017. "entry:\n"
  1018. " %guardcmp = icmp slt i32 0, %ub\n"
  1019. " br i1 %guardcmp, label %for.outer.preheader, label %for.end\n"
  1020. "for.outer.preheader:\n"
  1021. " br label %for.outer\n"
  1022. "for.outer:\n"
  1023. " %j = phi i32 [ 0, %for.outer.preheader ], [ %inc.outer, %for.outer.latch ]\n"
  1024. " br i1 %guardcmp, label %for.inner.preheader, label %for.outer.latch\n"
  1025. "for.inner.preheader:\n"
  1026. " br label %for.inner\n"
  1027. "for.inner:\n"
  1028. " %i = phi i32 [ 0, %for.inner.preheader ], [ %inc, %for.inner ]\n"
  1029. " %idxprom = sext i32 %i to i64\n"
  1030. " %arrayidx = getelementptr inbounds i32, i32* %A, i64 %idxprom\n"
  1031. " store i32 %i, i32* %arrayidx, align 4\n"
  1032. " %inc = add nsw i32 %i, 1\n"
  1033. " %cmp = icmp slt i32 %inc, %ub\n"
  1034. " br i1 %cmp, label %for.inner, label %for.inner.exit\n"
  1035. "for.inner.exit:\n"
  1036. " br label %for.outer.latch\n"
  1037. "for.outer.latch:\n"
  1038. " %inc.outer = add nsw i32 %j, 1\n"
  1039. " %cmp.outer = icmp slt i32 %inc.outer, %ub\n"
  1040. " br i1 %cmp.outer, label %for.outer, label %for.outer.exit\n"
  1041. "for.outer.exit:\n"
  1042. " br label %for.end\n"
  1043. "for.end:\n"
  1044. " ret void\n"
  1045. "}\n";
  1046. // Parse the module.
  1047. LLVMContext Context;
  1048. std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
  1049. runWithLoopInfoPlus(
  1050. *M, "foo",
  1051. [&](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
  1052. Function::iterator FI = F.begin();
  1053. BasicBlock *Entry = &*(FI);
  1054. BranchInst *OuterGuard = dyn_cast<BranchInst>(Entry->getTerminator());
  1055. // First two basic block are entry and for.outer.preheader - skip them.
  1056. ++FI;
  1057. BasicBlock *Header = &*(++FI);
  1058. assert(Header->getName() == "for.outer");
  1059. BranchInst *InnerGuard = dyn_cast<BranchInst>(Header->getTerminator());
  1060. Loop *L = LI.getLoopFor(Header);
  1061. EXPECT_NE(L, nullptr);
  1062. Optional<Loop::LoopBounds> Bounds = L->getBounds(SE);
  1063. EXPECT_NE(Bounds, None);
  1064. ConstantInt *InitialIVValue =
  1065. dyn_cast<ConstantInt>(&Bounds->getInitialIVValue());
  1066. EXPECT_TRUE(InitialIVValue && InitialIVValue->isZero());
  1067. EXPECT_EQ(Bounds->getStepInst().getName(), "inc.outer");
  1068. ConstantInt *StepValue =
  1069. dyn_cast_or_null<ConstantInt>(Bounds->getStepValue());
  1070. EXPECT_TRUE(StepValue && StepValue->isOne());
  1071. EXPECT_EQ(Bounds->getFinalIVValue().getName(), "ub");
  1072. EXPECT_EQ(Bounds->getCanonicalPredicate(), ICmpInst::ICMP_SLT);
  1073. EXPECT_EQ(Bounds->getDirection(),
  1074. Loop::LoopBounds::Direction::Increasing);
  1075. EXPECT_EQ(L->getInductionVariable(SE)->getName(), "j");
  1076. EXPECT_EQ(L->getLoopGuardBranch(), OuterGuard);
  1077. EXPECT_TRUE(L->isGuarded());
  1078. // Next two basic blocks are for.outer and for.inner.preheader - skip
  1079. // them.
  1080. ++FI;
  1081. Header = &*(++FI);
  1082. assert(Header->getName() == "for.inner");
  1083. L = LI.getLoopFor(Header);
  1084. EXPECT_NE(L, nullptr);
  1085. Optional<Loop::LoopBounds> InnerBounds = L->getBounds(SE);
  1086. EXPECT_NE(InnerBounds, None);
  1087. InitialIVValue =
  1088. dyn_cast<ConstantInt>(&InnerBounds->getInitialIVValue());
  1089. EXPECT_TRUE(InitialIVValue && InitialIVValue->isZero());
  1090. EXPECT_EQ(InnerBounds->getStepInst().getName(), "inc");
  1091. StepValue = dyn_cast_or_null<ConstantInt>(InnerBounds->getStepValue());
  1092. EXPECT_TRUE(StepValue && StepValue->isOne());
  1093. EXPECT_EQ(InnerBounds->getFinalIVValue().getName(), "ub");
  1094. EXPECT_EQ(InnerBounds->getCanonicalPredicate(), ICmpInst::ICMP_SLT);
  1095. EXPECT_EQ(InnerBounds->getDirection(),
  1096. Loop::LoopBounds::Direction::Increasing);
  1097. EXPECT_EQ(L->getInductionVariable(SE)->getName(), "i");
  1098. EXPECT_EQ(L->getLoopGuardBranch(), InnerGuard);
  1099. EXPECT_TRUE(L->isGuarded());
  1100. });
  1101. }
  1102. TEST(LoopInfoTest, AuxiliaryIV) {
  1103. const char *ModuleStr =
  1104. "define void @foo(i32* %A, i32 %ub) {\n"
  1105. "entry:\n"
  1106. " %guardcmp = icmp slt i32 0, %ub\n"
  1107. " br i1 %guardcmp, label %for.preheader, label %for.end\n"
  1108. "for.preheader:\n"
  1109. " br label %for.body\n"
  1110. "for.body:\n"
  1111. " %i = phi i32 [ 0, %for.preheader ], [ %inc, %for.body ]\n"
  1112. " %aux = phi i32 [ 0, %for.preheader ], [ %auxinc, %for.body ]\n"
  1113. " %loopvariant = phi i32 [ 0, %for.preheader ], [ %loopvariantinc, %for.body ]\n"
  1114. " %usedoutside = phi i32 [ 0, %for.preheader ], [ %usedoutsideinc, %for.body ]\n"
  1115. " %mulopcode = phi i32 [ 0, %for.preheader ], [ %mulopcodeinc, %for.body ]\n"
  1116. " %idxprom = sext i32 %i to i64\n"
  1117. " %arrayidx = getelementptr inbounds i32, i32* %A, i64 %idxprom\n"
  1118. " store i32 %i, i32* %arrayidx, align 4\n"
  1119. " %mulopcodeinc = mul nsw i32 %mulopcode, 5\n"
  1120. " %usedoutsideinc = add nsw i32 %usedoutside, 5\n"
  1121. " %loopvariantinc = add nsw i32 %loopvariant, %i\n"
  1122. " %auxinc = add nsw i32 %aux, 5\n"
  1123. " %inc = add nsw i32 %i, 1\n"
  1124. " %cmp = icmp slt i32 %inc, %ub\n"
  1125. " br i1 %cmp, label %for.body, label %for.exit\n"
  1126. "for.exit:\n"
  1127. " %lcssa = phi i32 [ %usedoutside, %for.body ]\n"
  1128. " br label %for.end\n"
  1129. "for.end:\n"
  1130. " ret void\n"
  1131. "}\n";
  1132. // Parse the module.
  1133. LLVMContext Context;
  1134. std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
  1135. runWithLoopInfoPlus(
  1136. *M, "foo",
  1137. [&](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
  1138. Function::iterator FI = F.begin();
  1139. BasicBlock *Entry = &*(FI);
  1140. BranchInst *Guard = dyn_cast<BranchInst>(Entry->getTerminator());
  1141. // First two basic block are entry and for.preheader - skip them.
  1142. ++FI;
  1143. BasicBlock *Header = &*(++FI);
  1144. assert(Header->getName() == "for.body");
  1145. Loop *L = LI.getLoopFor(Header);
  1146. EXPECT_NE(L, nullptr);
  1147. Optional<Loop::LoopBounds> Bounds = L->getBounds(SE);
  1148. EXPECT_NE(Bounds, None);
  1149. ConstantInt *InitialIVValue =
  1150. dyn_cast<ConstantInt>(&Bounds->getInitialIVValue());
  1151. EXPECT_TRUE(InitialIVValue && InitialIVValue->isZero());
  1152. EXPECT_EQ(Bounds->getStepInst().getName(), "inc");
  1153. ConstantInt *StepValue =
  1154. dyn_cast_or_null<ConstantInt>(Bounds->getStepValue());
  1155. EXPECT_TRUE(StepValue && StepValue->isOne());
  1156. EXPECT_EQ(Bounds->getFinalIVValue().getName(), "ub");
  1157. EXPECT_EQ(Bounds->getCanonicalPredicate(), ICmpInst::ICMP_SLT);
  1158. EXPECT_EQ(Bounds->getDirection(),
  1159. Loop::LoopBounds::Direction::Increasing);
  1160. EXPECT_EQ(L->getInductionVariable(SE)->getName(), "i");
  1161. BasicBlock::iterator II = Header->begin();
  1162. PHINode &Instruction_i = cast<PHINode>(*(II));
  1163. EXPECT_TRUE(L->isAuxiliaryInductionVariable(Instruction_i, SE));
  1164. PHINode &Instruction_aux = cast<PHINode>(*(++II));
  1165. EXPECT_TRUE(L->isAuxiliaryInductionVariable(Instruction_aux, SE));
  1166. PHINode &Instruction_loopvariant = cast<PHINode>(*(++II));
  1167. EXPECT_FALSE(
  1168. L->isAuxiliaryInductionVariable(Instruction_loopvariant, SE));
  1169. PHINode &Instruction_usedoutside = cast<PHINode>(*(++II));
  1170. EXPECT_FALSE(
  1171. L->isAuxiliaryInductionVariable(Instruction_usedoutside, SE));
  1172. PHINode &Instruction_mulopcode = cast<PHINode>(*(++II));
  1173. EXPECT_FALSE(
  1174. L->isAuxiliaryInductionVariable(Instruction_mulopcode, SE));
  1175. EXPECT_EQ(L->getLoopGuardBranch(), Guard);
  1176. EXPECT_TRUE(L->isGuarded());
  1177. });
  1178. }
  1179. TEST(LoopInfoTest, LoopNotInSimplifyForm) {
  1180. const char *ModuleStr =
  1181. "define void @foo(i32 %n) {\n"
  1182. "entry:\n"
  1183. " %guard.cmp = icmp sgt i32 %n, 0\n"
  1184. " br i1 %guard.cmp, label %for.cond, label %for.end\n"
  1185. "for.cond:\n"
  1186. " %i.0 = phi i32 [ 0, %entry ], [ %inc, %latch.1 ], [ %inc, %latch.2 ]\n"
  1187. " %inc = add nsw i32 %i.0, 1\n"
  1188. " %cmp = icmp slt i32 %i.0, %n\n"
  1189. " br i1 %cmp, label %latch.1, label %for.end\n"
  1190. "latch.1:\n"
  1191. " br i1 undef, label %for.cond, label %latch.2\n"
  1192. "latch.2:\n"
  1193. " br label %for.cond\n"
  1194. "for.end:\n"
  1195. " ret void\n"
  1196. "}\n";
  1197. // Parse the module.
  1198. LLVMContext Context;
  1199. std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
  1200. runWithLoopInfo(*M, "foo", [&](Function &F, LoopInfo &LI) {
  1201. Function::iterator FI = F.begin();
  1202. // First basic block is entry - skip it.
  1203. BasicBlock *Header = &*(++FI);
  1204. assert(Header && "No header");
  1205. Loop *L = LI.getLoopFor(Header);
  1206. EXPECT_NE(L, nullptr);
  1207. EXPECT_FALSE(L->isLoopSimplifyForm());
  1208. // No loop guard because loop in not in simplify form.
  1209. EXPECT_EQ(L->getLoopGuardBranch(), nullptr);
  1210. EXPECT_FALSE(L->isGuarded());
  1211. });
  1212. }
  1213. TEST(LoopInfoTest, LoopLatchNotExiting) {
  1214. const char *ModuleStr =
  1215. "define void @foo(i32* %A, i32 %ub) {\n"
  1216. "entry:\n"
  1217. " %guardcmp = icmp slt i32 0, %ub\n"
  1218. " br i1 %guardcmp, label %for.preheader, label %for.end\n"
  1219. "for.preheader:\n"
  1220. " br label %for.body\n"
  1221. "for.body:\n"
  1222. " %i = phi i32 [ 0, %for.preheader ], [ %inc, %for.body ]\n"
  1223. " %idxprom = sext i32 %i to i64\n"
  1224. " %arrayidx = getelementptr inbounds i32, i32* %A, i64 %idxprom\n"
  1225. " store i32 %i, i32* %arrayidx, align 4\n"
  1226. " %inc = add nsw i32 %i, 1\n"
  1227. " %cmp = icmp slt i32 %inc, %ub\n"
  1228. " br i1 %cmp, label %for.latch, label %for.exit\n"
  1229. "for.latch:\n"
  1230. " br label %for.body\n"
  1231. "for.exit:\n"
  1232. " br label %for.end\n"
  1233. "for.end:\n"
  1234. " ret void\n"
  1235. "}\n";
  1236. // Parse the module.
  1237. LLVMContext Context;
  1238. std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
  1239. runWithLoopInfoPlus(
  1240. *M, "foo",
  1241. [&](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
  1242. Function::iterator FI = F.begin();
  1243. // First two basic block are entry and for.preheader - skip them.
  1244. ++FI;
  1245. BasicBlock *Header = &*(++FI);
  1246. BasicBlock *Latch = &*(++FI);
  1247. assert(Header && "No header");
  1248. Loop *L = LI.getLoopFor(Header);
  1249. EXPECT_NE(L, nullptr);
  1250. EXPECT_TRUE(L->isLoopSimplifyForm());
  1251. EXPECT_EQ(L->getLoopLatch(), Latch);
  1252. EXPECT_FALSE(L->isLoopExiting(Latch));
  1253. // No loop guard becuase loop is not exiting on latch.
  1254. EXPECT_EQ(L->getLoopGuardBranch(), nullptr);
  1255. EXPECT_FALSE(L->isGuarded());
  1256. });
  1257. }
  1258. // Examine getUniqueExitBlocks/getUniqueNonLatchExitBlocks functions.
  1259. TEST(LoopInfoTest, LoopUniqueExitBlocks) {
  1260. const char *ModuleStr =
  1261. "target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n"
  1262. "define void @foo(i32 %n, i1 %cond) {\n"
  1263. "entry:\n"
  1264. " br label %for.cond\n"
  1265. "for.cond:\n"
  1266. " %i.0 = phi i32 [ 0, %entry ], [ %inc, %for.inc ]\n"
  1267. " %cmp = icmp slt i32 %i.0, %n\n"
  1268. " br i1 %cond, label %for.inc, label %for.end1\n"
  1269. "for.inc:\n"
  1270. " %inc = add nsw i32 %i.0, 1\n"
  1271. " br i1 %cmp, label %for.cond, label %for.end2, !llvm.loop !0\n"
  1272. "for.end1:\n"
  1273. " br label %for.end\n"
  1274. "for.end2:\n"
  1275. " br label %for.end\n"
  1276. "for.end:\n"
  1277. " ret void\n"
  1278. "}\n"
  1279. "!0 = distinct !{!0, !1}\n"
  1280. "!1 = !{!\"llvm.loop.distribute.enable\", i1 true}\n";
  1281. // Parse the module.
  1282. LLVMContext Context;
  1283. std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
  1284. runWithLoopInfo(*M, "foo", [&](Function &F, LoopInfo &LI) {
  1285. Function::iterator FI = F.begin();
  1286. // First basic block is entry - skip it.
  1287. BasicBlock *Header = &*(++FI);
  1288. assert(Header->getName() == "for.cond");
  1289. Loop *L = LI.getLoopFor(Header);
  1290. SmallVector<BasicBlock *, 2> Exits;
  1291. // This loop has 2 unique exits.
  1292. L->getUniqueExitBlocks(Exits);
  1293. EXPECT_TRUE(Exits.size() == 2);
  1294. // And one unique non latch exit.
  1295. Exits.clear();
  1296. L->getUniqueNonLatchExitBlocks(Exits);
  1297. EXPECT_TRUE(Exits.size() == 1);
  1298. });
  1299. }
  1300. // Regression test for getUniqueNonLatchExitBlocks functions.
  1301. // It should detect the exit if it comes from both latch and non-latch blocks.
  1302. TEST(LoopInfoTest, LoopNonLatchUniqueExitBlocks) {
  1303. const char *ModuleStr =
  1304. "target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n"
  1305. "define void @foo(i32 %n, i1 %cond) {\n"
  1306. "entry:\n"
  1307. " br label %for.cond\n"
  1308. "for.cond:\n"
  1309. " %i.0 = phi i32 [ 0, %entry ], [ %inc, %for.inc ]\n"
  1310. " %cmp = icmp slt i32 %i.0, %n\n"
  1311. " br i1 %cond, label %for.inc, label %for.end\n"
  1312. "for.inc:\n"
  1313. " %inc = add nsw i32 %i.0, 1\n"
  1314. " br i1 %cmp, label %for.cond, label %for.end, !llvm.loop !0\n"
  1315. "for.end:\n"
  1316. " ret void\n"
  1317. "}\n"
  1318. "!0 = distinct !{!0, !1}\n"
  1319. "!1 = !{!\"llvm.loop.distribute.enable\", i1 true}\n";
  1320. // Parse the module.
  1321. LLVMContext Context;
  1322. std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
  1323. runWithLoopInfo(*M, "foo", [&](Function &F, LoopInfo &LI) {
  1324. Function::iterator FI = F.begin();
  1325. // First basic block is entry - skip it.
  1326. BasicBlock *Header = &*(++FI);
  1327. assert(Header->getName() == "for.cond");
  1328. Loop *L = LI.getLoopFor(Header);
  1329. SmallVector<BasicBlock *, 2> Exits;
  1330. // This loop has 1 unique exit.
  1331. L->getUniqueExitBlocks(Exits);
  1332. EXPECT_TRUE(Exits.size() == 1);
  1333. // And one unique non latch exit.
  1334. Exits.clear();
  1335. L->getUniqueNonLatchExitBlocks(Exits);
  1336. EXPECT_TRUE(Exits.size() == 1);
  1337. });
  1338. }