LoopInfoTest.cpp 43 KB

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