1 //===- ScalarEvolutionsTest.cpp - ScalarEvolution 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 
9 #include "llvm/ADT/SmallVector.h"
10 #include "llvm/Analysis/AssumptionCache.h"
11 #include "llvm/Analysis/LoopInfo.h"
12 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
13 #include "llvm/Analysis/ScalarEvolutionNormalization.h"
14 #include "llvm/Analysis/TargetLibraryInfo.h"
15 #include "llvm/AsmParser/Parser.h"
16 #include "llvm/IR/Constants.h"
17 #include "llvm/IR/Dominators.h"
18 #include "llvm/IR/GlobalVariable.h"
19 #include "llvm/IR/IRBuilder.h"
20 #include "llvm/IR/InstIterator.h"
21 #include "llvm/IR/LLVMContext.h"
22 #include "llvm/IR/LegacyPassManager.h"
23 #include "llvm/IR/Module.h"
24 #include "llvm/IR/Verifier.h"
25 #include "llvm/Support/SourceMgr.h"
26 #include "gtest/gtest.h"
27 
28 namespace llvm {
29 
30 // We use this fixture to ensure that we clean up ScalarEvolution before
31 // deleting the PassManager.
32 class ScalarEvolutionsTest : public testing::Test {
33 protected:
34   LLVMContext Context;
35   Module M;
36   TargetLibraryInfoImpl TLII;
37   TargetLibraryInfo TLI;
38 
39   std::unique_ptr<AssumptionCache> AC;
40   std::unique_ptr<DominatorTree> DT;
41   std::unique_ptr<LoopInfo> LI;
42 
43   ScalarEvolutionsTest() : M("", Context), TLII(), TLI(TLII) {}
44 
45   ScalarEvolution buildSE(Function &F) {
46     AC.reset(new AssumptionCache(F));
47     DT.reset(new DominatorTree(F));
48     LI.reset(new LoopInfo(*DT));
49     return ScalarEvolution(F, TLI, *AC, *DT, *LI);
50   }
51 
52   void runWithSE(
53       Module &M, StringRef FuncName,
54       function_ref<void(Function &F, LoopInfo &LI, ScalarEvolution &SE)> Test) {
55     auto *F = M.getFunction(FuncName);
56     ASSERT_NE(F, nullptr) << "Could not find " << FuncName;
57     ScalarEvolution SE = buildSE(*F);
58     Test(*F, *LI, SE);
59   }
60 
61   static Optional<APInt> computeConstantDifference(ScalarEvolution &SE,
62                                                    const SCEV *LHS,
63                                                    const SCEV *RHS) {
64     return SE.computeConstantDifference(LHS, RHS);
65   }
66 
67   static bool matchURem(ScalarEvolution &SE, const SCEV *Expr, const SCEV *&LHS,
68                         const SCEV *&RHS) {
69     return SE.matchURem(Expr, LHS, RHS);
70   }
71 
72   static bool isImpliedCond(
73       ScalarEvolution &SE, ICmpInst::Predicate Pred, const SCEV *LHS,
74       const SCEV *RHS, ICmpInst::Predicate FoundPred, const SCEV *FoundLHS,
75       const SCEV *FoundRHS) {
76     return SE.isImpliedCond(Pred, LHS, RHS, FoundPred, FoundLHS, FoundRHS);
77   }
78 };
79 
80 TEST_F(ScalarEvolutionsTest, SCEVUnknownRAUW) {
81   FunctionType *FTy = FunctionType::get(Type::getVoidTy(Context),
82                                               std::vector<Type *>(), false);
83   Function *F = Function::Create(FTy, Function::ExternalLinkage, "f", M);
84   BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
85   ReturnInst::Create(Context, nullptr, BB);
86 
87   Type *Ty = Type::getInt1Ty(Context);
88   Constant *Init = Constant::getNullValue(Ty);
89   Value *V0 = new GlobalVariable(M, Ty, false, GlobalValue::ExternalLinkage, Init, "V0");
90   Value *V1 = new GlobalVariable(M, Ty, false, GlobalValue::ExternalLinkage, Init, "V1");
91   Value *V2 = new GlobalVariable(M, Ty, false, GlobalValue::ExternalLinkage, Init, "V2");
92 
93   ScalarEvolution SE = buildSE(*F);
94 
95   const SCEV *S0 = SE.getSCEV(V0);
96   const SCEV *S1 = SE.getSCEV(V1);
97   const SCEV *S2 = SE.getSCEV(V2);
98 
99   const SCEV *P0 = SE.getAddExpr(S0, SE.getConstant(S0->getType(), 2));
100   const SCEV *P1 = SE.getAddExpr(S1, SE.getConstant(S0->getType(), 2));
101   const SCEV *P2 = SE.getAddExpr(S2, SE.getConstant(S0->getType(), 2));
102 
103   auto *M0 = cast<SCEVAddExpr>(P0);
104   auto *M1 = cast<SCEVAddExpr>(P1);
105   auto *M2 = cast<SCEVAddExpr>(P2);
106 
107   EXPECT_EQ(cast<SCEVConstant>(M0->getOperand(0))->getValue()->getZExtValue(),
108             2u);
109   EXPECT_EQ(cast<SCEVConstant>(M1->getOperand(0))->getValue()->getZExtValue(),
110             2u);
111   EXPECT_EQ(cast<SCEVConstant>(M2->getOperand(0))->getValue()->getZExtValue(),
112             2u);
113 
114   // Before the RAUWs, these are all pointing to separate values.
115   EXPECT_EQ(cast<SCEVUnknown>(M0->getOperand(1))->getValue(), V0);
116   EXPECT_EQ(cast<SCEVUnknown>(M1->getOperand(1))->getValue(), V1);
117   EXPECT_EQ(cast<SCEVUnknown>(M2->getOperand(1))->getValue(), V2);
118 
119   // Do some RAUWs.
120   V2->replaceAllUsesWith(V1);
121   V1->replaceAllUsesWith(V0);
122 
123   // After the RAUWs, these should all be pointing to V0.
124   EXPECT_EQ(cast<SCEVUnknown>(M0->getOperand(1))->getValue(), V0);
125   EXPECT_EQ(cast<SCEVUnknown>(M1->getOperand(1))->getValue(), V0);
126   EXPECT_EQ(cast<SCEVUnknown>(M2->getOperand(1))->getValue(), V0);
127 }
128 
129 TEST_F(ScalarEvolutionsTest, SimplifiedPHI) {
130   FunctionType *FTy = FunctionType::get(Type::getVoidTy(Context),
131                                               std::vector<Type *>(), false);
132   Function *F = Function::Create(FTy, Function::ExternalLinkage, "f", M);
133   BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", F);
134   BasicBlock *LoopBB = BasicBlock::Create(Context, "loop", F);
135   BasicBlock *ExitBB = BasicBlock::Create(Context, "exit", F);
136   BranchInst::Create(LoopBB, EntryBB);
137   BranchInst::Create(LoopBB, ExitBB, UndefValue::get(Type::getInt1Ty(Context)),
138                      LoopBB);
139   ReturnInst::Create(Context, nullptr, ExitBB);
140   auto *Ty = Type::getInt32Ty(Context);
141   auto *PN = PHINode::Create(Ty, 2, "", &*LoopBB->begin());
142   PN->addIncoming(Constant::getNullValue(Ty), EntryBB);
143   PN->addIncoming(UndefValue::get(Ty), LoopBB);
144   ScalarEvolution SE = buildSE(*F);
145   auto *S1 = SE.getSCEV(PN);
146   auto *S2 = SE.getSCEV(PN);
147   auto *ZeroConst = SE.getConstant(Ty, 0);
148 
149   // At some point, only the first call to getSCEV returned the simplified
150   // SCEVConstant and later calls just returned a SCEVUnknown referencing the
151   // PHI node.
152   EXPECT_EQ(S1, ZeroConst);
153   EXPECT_EQ(S1, S2);
154 }
155 
156 
157 static Instruction *getInstructionByName(Function &F, StringRef Name) {
158   for (auto &I : instructions(F))
159     if (I.getName() == Name)
160       return &I;
161   llvm_unreachable("Expected to find instruction!");
162 }
163 
164 static Value *getArgByName(Function &F, StringRef Name) {
165   for (auto &Arg : F.args())
166     if (Arg.getName() == Name)
167       return &Arg;
168   llvm_unreachable("Expected to find instruction!");
169 }
170 TEST_F(ScalarEvolutionsTest, CommutativeExprOperandOrder) {
171   LLVMContext C;
172   SMDiagnostic Err;
173   std::unique_ptr<Module> M = parseAssemblyString(
174       "target datalayout = \"e-m:e-p:32:32-f64:32:64-f80:32-n8:16:32-S128\" "
175       " "
176       "@var_0 = external global i32, align 4"
177       "@var_1 = external global i32, align 4"
178       "@var_2 = external global i32, align 4"
179       " "
180       "declare i32 @unknown(i32, i32, i32)"
181       " "
182       "define void @f_1(i8* nocapture %arr, i32 %n, i32* %A, i32* %B) "
183       "    local_unnamed_addr { "
184       "entry: "
185       "  %entrycond = icmp sgt i32 %n, 0 "
186       "  br i1 %entrycond, label %loop.ph, label %for.end "
187       " "
188       "loop.ph: "
189       "  %a = load i32, i32* %A, align 4 "
190       "  %b = load i32, i32* %B, align 4 "
191       "  %mul = mul nsw i32 %b, %a "
192       "  %iv0.init = getelementptr inbounds i8, i8* %arr, i32 %mul "
193       "  br label %loop "
194       " "
195       "loop: "
196       "  %iv0 = phi i8* [ %iv0.inc, %loop ], [ %iv0.init, %loop.ph ] "
197       "  %iv1 = phi i32 [ %iv1.inc, %loop ], [ 0, %loop.ph ] "
198       "  %conv = trunc i32 %iv1 to i8 "
199       "  store i8 %conv, i8* %iv0, align 1 "
200       "  %iv0.inc = getelementptr inbounds i8, i8* %iv0, i32 %b "
201       "  %iv1.inc = add nuw nsw i32 %iv1, 1 "
202       "  %exitcond = icmp eq i32 %iv1.inc, %n "
203       "  br i1 %exitcond, label %for.end.loopexit, label %loop "
204       " "
205       "for.end.loopexit: "
206       "  br label %for.end "
207       " "
208       "for.end: "
209       "  ret void "
210       "} "
211       " "
212       "define void @f_2(i32* %X, i32* %Y, i32* %Z) { "
213       "  %x = load i32, i32* %X "
214       "  %y = load i32, i32* %Y "
215       "  %z = load i32, i32* %Z "
216       "  ret void "
217       "} "
218       " "
219       "define void @f_3() { "
220       "  %x = load i32, i32* @var_0"
221       "  %y = load i32, i32* @var_1"
222       "  %z = load i32, i32* @var_2"
223       "  ret void"
224       "} "
225       " "
226       "define void @f_4(i32 %a, i32 %b, i32 %c) { "
227       "  %x = call i32 @unknown(i32 %a, i32 %b, i32 %c)"
228       "  %y = call i32 @unknown(i32 %b, i32 %c, i32 %a)"
229       "  %z = call i32 @unknown(i32 %c, i32 %a, i32 %b)"
230       "  ret void"
231       "} "
232       ,
233       Err, C);
234 
235   assert(M && "Could not parse module?");
236   assert(!verifyModule(*M) && "Must have been well formed!");
237 
238   runWithSE(*M, "f_1", [&](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
239     auto *IV0 = getInstructionByName(F, "iv0");
240     auto *IV0Inc = getInstructionByName(F, "iv0.inc");
241 
242     auto *FirstExprForIV0 = SE.getSCEV(IV0);
243     auto *FirstExprForIV0Inc = SE.getSCEV(IV0Inc);
244     auto *SecondExprForIV0 = SE.getSCEV(IV0);
245 
246     EXPECT_TRUE(isa<SCEVAddRecExpr>(FirstExprForIV0));
247     EXPECT_TRUE(isa<SCEVAddRecExpr>(FirstExprForIV0Inc));
248     EXPECT_TRUE(isa<SCEVAddRecExpr>(SecondExprForIV0));
249   });
250 
251   auto CheckCommutativeMulExprs = [&](ScalarEvolution &SE, const SCEV *A,
252                                       const SCEV *B, const SCEV *C) {
253     EXPECT_EQ(SE.getMulExpr(A, B), SE.getMulExpr(B, A));
254     EXPECT_EQ(SE.getMulExpr(B, C), SE.getMulExpr(C, B));
255     EXPECT_EQ(SE.getMulExpr(A, C), SE.getMulExpr(C, A));
256 
257     SmallVector<const SCEV *, 3> Ops0 = {A, B, C};
258     SmallVector<const SCEV *, 3> Ops1 = {A, C, B};
259     SmallVector<const SCEV *, 3> Ops2 = {B, A, C};
260     SmallVector<const SCEV *, 3> Ops3 = {B, C, A};
261     SmallVector<const SCEV *, 3> Ops4 = {C, B, A};
262     SmallVector<const SCEV *, 3> Ops5 = {C, A, B};
263 
264     auto *Mul0 = SE.getMulExpr(Ops0);
265     auto *Mul1 = SE.getMulExpr(Ops1);
266     auto *Mul2 = SE.getMulExpr(Ops2);
267     auto *Mul3 = SE.getMulExpr(Ops3);
268     auto *Mul4 = SE.getMulExpr(Ops4);
269     auto *Mul5 = SE.getMulExpr(Ops5);
270 
271     EXPECT_EQ(Mul0, Mul1) << "Expected " << *Mul0 << " == " << *Mul1;
272     EXPECT_EQ(Mul1, Mul2) << "Expected " << *Mul1 << " == " << *Mul2;
273     EXPECT_EQ(Mul2, Mul3) << "Expected " << *Mul2 << " == " << *Mul3;
274     EXPECT_EQ(Mul3, Mul4) << "Expected " << *Mul3 << " == " << *Mul4;
275     EXPECT_EQ(Mul4, Mul5) << "Expected " << *Mul4 << " == " << *Mul5;
276   };
277 
278   for (StringRef FuncName : {"f_2", "f_3", "f_4"})
279     runWithSE(
280         *M, FuncName, [&](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
281           CheckCommutativeMulExprs(SE, SE.getSCEV(getInstructionByName(F, "x")),
282                                    SE.getSCEV(getInstructionByName(F, "y")),
283                                    SE.getSCEV(getInstructionByName(F, "z")));
284         });
285 }
286 
287 TEST_F(ScalarEvolutionsTest, CompareSCEVComplexity) {
288   FunctionType *FTy =
289       FunctionType::get(Type::getVoidTy(Context), std::vector<Type *>(), false);
290   Function *F = Function::Create(FTy, Function::ExternalLinkage, "f", M);
291   BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", F);
292   BasicBlock *LoopBB = BasicBlock::Create(Context, "bb1", F);
293   BranchInst::Create(LoopBB, EntryBB);
294 
295   auto *Ty = Type::getInt32Ty(Context);
296   SmallVector<Instruction*, 8> Muls(8), Acc(8), NextAcc(8);
297 
298   Acc[0] = PHINode::Create(Ty, 2, "", LoopBB);
299   Acc[1] = PHINode::Create(Ty, 2, "", LoopBB);
300   Acc[2] = PHINode::Create(Ty, 2, "", LoopBB);
301   Acc[3] = PHINode::Create(Ty, 2, "", LoopBB);
302   Acc[4] = PHINode::Create(Ty, 2, "", LoopBB);
303   Acc[5] = PHINode::Create(Ty, 2, "", LoopBB);
304   Acc[6] = PHINode::Create(Ty, 2, "", LoopBB);
305   Acc[7] = PHINode::Create(Ty, 2, "", LoopBB);
306 
307   for (int i = 0; i < 20; i++) {
308     Muls[0] = BinaryOperator::CreateMul(Acc[0], Acc[0], "", LoopBB);
309     NextAcc[0] = BinaryOperator::CreateAdd(Muls[0], Acc[4], "", LoopBB);
310     Muls[1] = BinaryOperator::CreateMul(Acc[1], Acc[1], "", LoopBB);
311     NextAcc[1] = BinaryOperator::CreateAdd(Muls[1], Acc[5], "", LoopBB);
312     Muls[2] = BinaryOperator::CreateMul(Acc[2], Acc[2], "", LoopBB);
313     NextAcc[2] = BinaryOperator::CreateAdd(Muls[2], Acc[6], "", LoopBB);
314     Muls[3] = BinaryOperator::CreateMul(Acc[3], Acc[3], "", LoopBB);
315     NextAcc[3] = BinaryOperator::CreateAdd(Muls[3], Acc[7], "", LoopBB);
316 
317     Muls[4] = BinaryOperator::CreateMul(Acc[4], Acc[4], "", LoopBB);
318     NextAcc[4] = BinaryOperator::CreateAdd(Muls[4], Acc[0], "", LoopBB);
319     Muls[5] = BinaryOperator::CreateMul(Acc[5], Acc[5], "", LoopBB);
320     NextAcc[5] = BinaryOperator::CreateAdd(Muls[5], Acc[1], "", LoopBB);
321     Muls[6] = BinaryOperator::CreateMul(Acc[6], Acc[6], "", LoopBB);
322     NextAcc[6] = BinaryOperator::CreateAdd(Muls[6], Acc[2], "", LoopBB);
323     Muls[7] = BinaryOperator::CreateMul(Acc[7], Acc[7], "", LoopBB);
324     NextAcc[7] = BinaryOperator::CreateAdd(Muls[7], Acc[3], "", LoopBB);
325     Acc = NextAcc;
326   }
327 
328   auto II = LoopBB->begin();
329   for (int i = 0; i < 8; i++) {
330     PHINode *Phi = cast<PHINode>(&*II++);
331     Phi->addIncoming(Acc[i], LoopBB);
332     Phi->addIncoming(UndefValue::get(Ty), EntryBB);
333   }
334 
335   BasicBlock *ExitBB = BasicBlock::Create(Context, "bb2", F);
336   BranchInst::Create(LoopBB, ExitBB, UndefValue::get(Type::getInt1Ty(Context)),
337                      LoopBB);
338 
339   Acc[0] = BinaryOperator::CreateAdd(Acc[0], Acc[1], "", ExitBB);
340   Acc[1] = BinaryOperator::CreateAdd(Acc[2], Acc[3], "", ExitBB);
341   Acc[2] = BinaryOperator::CreateAdd(Acc[4], Acc[5], "", ExitBB);
342   Acc[3] = BinaryOperator::CreateAdd(Acc[6], Acc[7], "", ExitBB);
343   Acc[0] = BinaryOperator::CreateAdd(Acc[0], Acc[1], "", ExitBB);
344   Acc[1] = BinaryOperator::CreateAdd(Acc[2], Acc[3], "", ExitBB);
345   Acc[0] = BinaryOperator::CreateAdd(Acc[0], Acc[1], "", ExitBB);
346 
347   ReturnInst::Create(Context, nullptr, ExitBB);
348 
349   ScalarEvolution SE = buildSE(*F);
350 
351   EXPECT_NE(nullptr, SE.getSCEV(Acc[0]));
352 }
353 
354 TEST_F(ScalarEvolutionsTest, CompareValueComplexity) {
355   IntegerType *IntPtrTy = M.getDataLayout().getIntPtrType(Context);
356   PointerType *IntPtrPtrTy = IntPtrTy->getPointerTo();
357 
358   FunctionType *FTy =
359       FunctionType::get(Type::getVoidTy(Context), {IntPtrTy, IntPtrTy}, false);
360   Function *F = Function::Create(FTy, Function::ExternalLinkage, "f", M);
361   BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", F);
362 
363   Value *X = &*F->arg_begin();
364   Value *Y = &*std::next(F->arg_begin());
365 
366   const int ValueDepth = 10;
367   for (int i = 0; i < ValueDepth; i++) {
368     X = new LoadInst(IntPtrTy, new IntToPtrInst(X, IntPtrPtrTy, "", EntryBB),
369                      "",
370                      /*isVolatile*/ false, EntryBB);
371     Y = new LoadInst(IntPtrTy, new IntToPtrInst(Y, IntPtrPtrTy, "", EntryBB),
372                      "",
373                      /*isVolatile*/ false, EntryBB);
374   }
375 
376   auto *MulA = BinaryOperator::CreateMul(X, Y, "", EntryBB);
377   auto *MulB = BinaryOperator::CreateMul(Y, X, "", EntryBB);
378   ReturnInst::Create(Context, nullptr, EntryBB);
379 
380   // This test isn't checking for correctness.  Today making A and B resolve to
381   // the same SCEV would require deeper searching in CompareValueComplexity,
382   // which will slow down compilation.  However, this test can fail (with LLVM's
383   // behavior still being correct) if we ever have a smarter
384   // CompareValueComplexity that is both fast and more accurate.
385 
386   ScalarEvolution SE = buildSE(*F);
387   auto *A = SE.getSCEV(MulA);
388   auto *B = SE.getSCEV(MulB);
389   EXPECT_NE(A, B);
390 }
391 
392 TEST_F(ScalarEvolutionsTest, SCEVAddExpr) {
393   Type *Ty32 = Type::getInt32Ty(Context);
394   Type *ArgTys[] = {Type::getInt64Ty(Context), Ty32, Ty32, Ty32, Ty32, Ty32};
395 
396   FunctionType *FTy =
397       FunctionType::get(Type::getVoidTy(Context), ArgTys, false);
398   Function *F = Function::Create(FTy, Function::ExternalLinkage, "f", M);
399 
400   Argument *A1 = &*F->arg_begin();
401   Argument *A2 = &*(std::next(F->arg_begin()));
402   BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", F);
403 
404   Instruction *Trunc = CastInst::CreateTruncOrBitCast(A1, Ty32, "", EntryBB);
405   Instruction *Mul1 = BinaryOperator::CreateMul(Trunc, A2, "", EntryBB);
406   Instruction *Add1 = BinaryOperator::CreateAdd(Mul1, Trunc, "", EntryBB);
407   Mul1 = BinaryOperator::CreateMul(Add1, Trunc, "", EntryBB);
408   Instruction *Add2 = BinaryOperator::CreateAdd(Mul1, Add1, "", EntryBB);
409   // FIXME: The size of this is arbitrary and doesn't seem to change the
410   // result, but SCEV will do quadratic work for these so a large number here
411   // will be extremely slow. We should revisit what and how this is testing
412   // SCEV.
413   for (int i = 0; i < 10; i++) {
414     Mul1 = BinaryOperator::CreateMul(Add2, Add1, "", EntryBB);
415     Add1 = Add2;
416     Add2 = BinaryOperator::CreateAdd(Mul1, Add1, "", EntryBB);
417   }
418 
419   ReturnInst::Create(Context, nullptr, EntryBB);
420   ScalarEvolution SE = buildSE(*F);
421   EXPECT_NE(nullptr, SE.getSCEV(Mul1));
422 
423   Argument *A3 = &*(std::next(F->arg_begin(), 2));
424   Argument *A4 = &*(std::next(F->arg_begin(), 3));
425   Argument *A5 = &*(std::next(F->arg_begin(), 4));
426   Argument *A6 = &*(std::next(F->arg_begin(), 5));
427 
428   auto *AddWithNUW = cast<SCEVAddExpr>(SE.getAddExpr(
429       SE.getAddExpr(SE.getSCEV(A2), SE.getSCEV(A3), SCEV::FlagNUW),
430       SE.getConstant(APInt(/*numBits=*/32, 5)), SCEV::FlagNUW));
431   EXPECT_EQ(AddWithNUW->getNumOperands(), 3u);
432   EXPECT_EQ(AddWithNUW->getNoWrapFlags(), SCEV::FlagNUW);
433 
434   auto *AddWithAnyWrap =
435       SE.getAddExpr(SE.getSCEV(A3), SE.getSCEV(A4), SCEV::FlagAnyWrap);
436   auto *AddWithAnyWrapNUW = cast<SCEVAddExpr>(
437       SE.getAddExpr(AddWithAnyWrap, SE.getSCEV(A5), SCEV::FlagNUW));
438   EXPECT_EQ(AddWithAnyWrapNUW->getNumOperands(), 3u);
439   EXPECT_EQ(AddWithAnyWrapNUW->getNoWrapFlags(), SCEV::FlagAnyWrap);
440 
441   auto *AddWithNSW = SE.getAddExpr(
442       SE.getSCEV(A2), SE.getConstant(APInt(32, 99)), SCEV::FlagNSW);
443   auto *AddWithNSW_NUW = cast<SCEVAddExpr>(
444       SE.getAddExpr(AddWithNSW, SE.getSCEV(A5), SCEV::FlagNUW));
445   EXPECT_EQ(AddWithNSW_NUW->getNumOperands(), 3u);
446   EXPECT_EQ(AddWithNSW_NUW->getNoWrapFlags(), SCEV::FlagAnyWrap);
447 
448   auto *AddWithNSWNUW =
449       SE.getAddExpr(SE.getSCEV(A2), SE.getSCEV(A4),
450                     ScalarEvolution::setFlags(SCEV::FlagNUW, SCEV::FlagNSW));
451   auto *AddWithNSWNUW_NUW = cast<SCEVAddExpr>(
452       SE.getAddExpr(AddWithNSWNUW, SE.getSCEV(A5), SCEV::FlagNUW));
453   EXPECT_EQ(AddWithNSWNUW_NUW->getNumOperands(), 3u);
454   EXPECT_EQ(AddWithNSWNUW_NUW->getNoWrapFlags(), SCEV::FlagNUW);
455 
456   auto *AddWithNSW_NSWNUW = cast<SCEVAddExpr>(
457       SE.getAddExpr(AddWithNSW, SE.getSCEV(A6),
458                     ScalarEvolution::setFlags(SCEV::FlagNUW, SCEV::FlagNSW)));
459   EXPECT_EQ(AddWithNSW_NSWNUW->getNumOperands(), 3u);
460   EXPECT_EQ(AddWithNSW_NSWNUW->getNoWrapFlags(), SCEV::FlagAnyWrap);
461 }
462 
463 static Instruction &GetInstByName(Function &F, StringRef Name) {
464   for (auto &I : instructions(F))
465     if (I.getName() == Name)
466       return I;
467   llvm_unreachable("Could not find instructions!");
468 }
469 
470 TEST_F(ScalarEvolutionsTest, SCEVNormalization) {
471   LLVMContext C;
472   SMDiagnostic Err;
473   std::unique_ptr<Module> M = parseAssemblyString(
474       "target datalayout = \"e-m:e-p:32:32-f64:32:64-f80:32-n8:16:32-S128\" "
475       " "
476       "@var_0 = external global i32, align 4"
477       "@var_1 = external global i32, align 4"
478       "@var_2 = external global i32, align 4"
479       " "
480       "declare i32 @unknown(i32, i32, i32)"
481       " "
482       "define void @f_1(i8* nocapture %arr, i32 %n, i32* %A, i32* %B) "
483       "    local_unnamed_addr { "
484       "entry: "
485       "  br label %loop.ph "
486       " "
487       "loop.ph: "
488       "  br label %loop "
489       " "
490       "loop: "
491       "  %iv0 = phi i32 [ %iv0.inc, %loop ], [ 0, %loop.ph ] "
492       "  %iv1 = phi i32 [ %iv1.inc, %loop ], [ -2147483648, %loop.ph ] "
493       "  %iv0.inc = add i32 %iv0, 1 "
494       "  %iv1.inc = add i32 %iv1, 3 "
495       "  br i1 undef, label %for.end.loopexit, label %loop "
496       " "
497       "for.end.loopexit: "
498       "  ret void "
499       "} "
500       " "
501       "define void @f_2(i32 %a, i32 %b, i32 %c, i32 %d) "
502       "    local_unnamed_addr { "
503       "entry: "
504       "  br label %loop_0 "
505       " "
506       "loop_0: "
507       "  br i1 undef, label %loop_0, label %loop_1 "
508       " "
509       "loop_1: "
510       "  br i1 undef, label %loop_2, label %loop_1 "
511       " "
512       " "
513       "loop_2: "
514       "  br i1 undef, label %end, label %loop_2 "
515       " "
516       "end: "
517       "  ret void "
518       "} "
519       ,
520       Err, C);
521 
522   assert(M && "Could not parse module?");
523   assert(!verifyModule(*M) && "Must have been well formed!");
524 
525   runWithSE(*M, "f_1", [&](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
526     auto &I0 = GetInstByName(F, "iv0");
527     auto &I1 = *I0.getNextNode();
528 
529     auto *S0 = cast<SCEVAddRecExpr>(SE.getSCEV(&I0));
530     PostIncLoopSet Loops;
531     Loops.insert(S0->getLoop());
532     auto *N0 = normalizeForPostIncUse(S0, Loops, SE);
533     auto *D0 = denormalizeForPostIncUse(N0, Loops, SE);
534     EXPECT_EQ(S0, D0) << *S0 << " " << *D0;
535 
536     auto *S1 = cast<SCEVAddRecExpr>(SE.getSCEV(&I1));
537     Loops.clear();
538     Loops.insert(S1->getLoop());
539     auto *N1 = normalizeForPostIncUse(S1, Loops, SE);
540     auto *D1 = denormalizeForPostIncUse(N1, Loops, SE);
541     EXPECT_EQ(S1, D1) << *S1 << " " << *D1;
542   });
543 
544   runWithSE(*M, "f_2", [&](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
545     auto *L2 = *LI.begin();
546     auto *L1 = *std::next(LI.begin());
547     auto *L0 = *std::next(LI.begin(), 2);
548 
549     auto GetAddRec = [&SE](const Loop *L, std::initializer_list<const SCEV *> Ops) {
550       SmallVector<const SCEV *, 4> OpsCopy(Ops);
551       return SE.getAddRecExpr(OpsCopy, L, SCEV::FlagAnyWrap);
552     };
553 
554     auto GetAdd = [&SE](std::initializer_list<const SCEV *> Ops) {
555       SmallVector<const SCEV *, 4> OpsCopy(Ops);
556       return SE.getAddExpr(OpsCopy, SCEV::FlagAnyWrap);
557     };
558 
559     // We first populate the AddRecs vector with a few "interesting" SCEV
560     // expressions, and then we go through the list and assert that each
561     // expression in it has an invertible normalization.
562 
563     std::vector<const SCEV *> Exprs;
564     {
565       const SCEV *V0 = SE.getSCEV(&*F.arg_begin());
566       const SCEV *V1 = SE.getSCEV(&*std::next(F.arg_begin(), 1));
567       const SCEV *V2 = SE.getSCEV(&*std::next(F.arg_begin(), 2));
568       const SCEV *V3 = SE.getSCEV(&*std::next(F.arg_begin(), 3));
569 
570       Exprs.push_back(GetAddRec(L0, {V0}));             // 0
571       Exprs.push_back(GetAddRec(L0, {V0, V1}));         // 1
572       Exprs.push_back(GetAddRec(L0, {V0, V1, V2}));     // 2
573       Exprs.push_back(GetAddRec(L0, {V0, V1, V2, V3})); // 3
574 
575       Exprs.push_back(
576           GetAddRec(L1, {Exprs[1], Exprs[2], Exprs[3], Exprs[0]})); // 4
577       Exprs.push_back(
578           GetAddRec(L1, {Exprs[1], Exprs[2], Exprs[0], Exprs[3]})); // 5
579       Exprs.push_back(
580           GetAddRec(L1, {Exprs[1], Exprs[3], Exprs[3], Exprs[1]})); // 6
581 
582       Exprs.push_back(GetAdd({Exprs[6], Exprs[3], V2})); // 7
583 
584       Exprs.push_back(
585           GetAddRec(L2, {Exprs[4], Exprs[3], Exprs[3], Exprs[5]})); // 8
586 
587       Exprs.push_back(
588           GetAddRec(L2, {Exprs[4], Exprs[6], Exprs[7], Exprs[3], V0})); // 9
589     }
590 
591     std::vector<PostIncLoopSet> LoopSets;
592     for (int i = 0; i < 8; i++) {
593       LoopSets.emplace_back();
594       if (i & 1)
595         LoopSets.back().insert(L0);
596       if (i & 2)
597         LoopSets.back().insert(L1);
598       if (i & 4)
599         LoopSets.back().insert(L2);
600     }
601 
602     for (const auto &LoopSet : LoopSets)
603       for (auto *S : Exprs) {
604         {
605           auto *N = llvm::normalizeForPostIncUse(S, LoopSet, SE);
606           auto *D = llvm::denormalizeForPostIncUse(N, LoopSet, SE);
607 
608           // Normalization and then denormalizing better give us back the same
609           // value.
610           EXPECT_EQ(S, D) << "S = " << *S << "  D = " << *D << " N = " << *N;
611         }
612         {
613           auto *D = llvm::denormalizeForPostIncUse(S, LoopSet, SE);
614           auto *N = llvm::normalizeForPostIncUse(D, LoopSet, SE);
615 
616           // Denormalization and then normalizing better give us back the same
617           // value.
618           EXPECT_EQ(S, N) << "S = " << *S << "  N = " << *N;
619         }
620       }
621   });
622 }
623 
624 // Expect the call of getZeroExtendExpr will not cost exponential time.
625 TEST_F(ScalarEvolutionsTest, SCEVZeroExtendExpr) {
626   LLVMContext C;
627   SMDiagnostic Err;
628 
629   // Generate a function like below:
630   // define void @foo() {
631   // entry:
632   //   br label %for.cond
633   //
634   // for.cond:
635   //   %0 = phi i64 [ 100, %entry ], [ %dec, %for.inc ]
636   //   %cmp = icmp sgt i64 %0, 90
637   //   br i1 %cmp, label %for.inc, label %for.cond1
638   //
639   // for.inc:
640   //   %dec = add nsw i64 %0, -1
641   //   br label %for.cond
642   //
643   // for.cond1:
644   //   %1 = phi i64 [ 100, %for.cond ], [ %dec5, %for.inc2 ]
645   //   %cmp3 = icmp sgt i64 %1, 90
646   //   br i1 %cmp3, label %for.inc2, label %for.cond4
647   //
648   // for.inc2:
649   //   %dec5 = add nsw i64 %1, -1
650   //   br label %for.cond1
651   //
652   // ......
653   //
654   // for.cond89:
655   //   %19 = phi i64 [ 100, %for.cond84 ], [ %dec94, %for.inc92 ]
656   //   %cmp93 = icmp sgt i64 %19, 90
657   //   br i1 %cmp93, label %for.inc92, label %for.end
658   //
659   // for.inc92:
660   //   %dec94 = add nsw i64 %19, -1
661   //   br label %for.cond89
662   //
663   // for.end:
664   //   %gep = getelementptr i8, i8* null, i64 %dec
665   //   %gep6 = getelementptr i8, i8* %gep, i64 %dec5
666   //   ......
667   //   %gep95 = getelementptr i8, i8* %gep91, i64 %dec94
668   //   ret void
669   // }
670   FunctionType *FTy = FunctionType::get(Type::getVoidTy(Context), {}, false);
671   Function *F = Function::Create(FTy, Function::ExternalLinkage, "foo", M);
672 
673   BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", F);
674   BasicBlock *CondBB = BasicBlock::Create(Context, "for.cond", F);
675   BasicBlock *EndBB = BasicBlock::Create(Context, "for.end", F);
676   BranchInst::Create(CondBB, EntryBB);
677   BasicBlock *PrevBB = EntryBB;
678 
679   Type *I64Ty = Type::getInt64Ty(Context);
680   Type *I8Ty = Type::getInt8Ty(Context);
681   Type *I8PtrTy = Type::getInt8PtrTy(Context);
682   Value *Accum = Constant::getNullValue(I8PtrTy);
683   int Iters = 20;
684   for (int i = 0; i < Iters; i++) {
685     BasicBlock *IncBB = BasicBlock::Create(Context, "for.inc", F, EndBB);
686     auto *PN = PHINode::Create(I64Ty, 2, "", CondBB);
687     PN->addIncoming(ConstantInt::get(Context, APInt(64, 100)), PrevBB);
688     auto *Cmp = CmpInst::Create(Instruction::ICmp, CmpInst::ICMP_SGT, PN,
689                                 ConstantInt::get(Context, APInt(64, 90)), "cmp",
690                                 CondBB);
691     BasicBlock *NextBB;
692     if (i != Iters - 1)
693       NextBB = BasicBlock::Create(Context, "for.cond", F, EndBB);
694     else
695       NextBB = EndBB;
696     BranchInst::Create(IncBB, NextBB, Cmp, CondBB);
697     auto *Dec = BinaryOperator::CreateNSWAdd(
698         PN, ConstantInt::get(Context, APInt(64, -1)), "dec", IncBB);
699     PN->addIncoming(Dec, IncBB);
700     BranchInst::Create(CondBB, IncBB);
701 
702     Accum = GetElementPtrInst::Create(I8Ty, Accum, PN, "gep", EndBB);
703 
704     PrevBB = CondBB;
705     CondBB = NextBB;
706   }
707   ReturnInst::Create(Context, nullptr, EndBB);
708   ScalarEvolution SE = buildSE(*F);
709   const SCEV *S = SE.getSCEV(Accum);
710   S = SE.getLosslessPtrToIntExpr(S);
711   Type *I128Ty = Type::getInt128Ty(Context);
712   SE.getZeroExtendExpr(S, I128Ty);
713 }
714 
715 // Make sure that SCEV invalidates exit limits after invalidating the values it
716 // depends on when we forget a loop.
717 TEST_F(ScalarEvolutionsTest, SCEVExitLimitForgetLoop) {
718   /*
719    * Create the following code:
720    * func(i64 addrspace(10)* %arg)
721    * top:
722    *  br label %L.ph
723    * L.ph:
724    *  br label %L
725    * L:
726    *  %phi = phi i64 [i64 0, %L.ph], [ %add, %L2 ]
727    *  %add = add i64 %phi2, 1
728    *  %cond = icmp slt i64 %add, 1000; then becomes 2000.
729    *  br i1 %cond, label %post, label %L2
730    * post:
731    *  ret void
732    *
733    */
734 
735   // Create a module with non-integral pointers in it's datalayout
736   Module NIM("nonintegral", Context);
737   std::string DataLayout = M.getDataLayoutStr();
738   if (!DataLayout.empty())
739     DataLayout += "-";
740   DataLayout += "ni:10";
741   NIM.setDataLayout(DataLayout);
742 
743   Type *T_int64 = Type::getInt64Ty(Context);
744   Type *T_pint64 = T_int64->getPointerTo(10);
745 
746   FunctionType *FTy =
747       FunctionType::get(Type::getVoidTy(Context), {T_pint64}, false);
748   Function *F = Function::Create(FTy, Function::ExternalLinkage, "foo", NIM);
749 
750   BasicBlock *Top = BasicBlock::Create(Context, "top", F);
751   BasicBlock *LPh = BasicBlock::Create(Context, "L.ph", F);
752   BasicBlock *L = BasicBlock::Create(Context, "L", F);
753   BasicBlock *Post = BasicBlock::Create(Context, "post", F);
754 
755   IRBuilder<> Builder(Top);
756   Builder.CreateBr(LPh);
757 
758   Builder.SetInsertPoint(LPh);
759   Builder.CreateBr(L);
760 
761   Builder.SetInsertPoint(L);
762   PHINode *Phi = Builder.CreatePHI(T_int64, 2);
763   auto *Add = cast<Instruction>(
764       Builder.CreateAdd(Phi, ConstantInt::get(T_int64, 1), "add"));
765   auto *Limit = ConstantInt::get(T_int64, 1000);
766   auto *Cond = cast<Instruction>(
767       Builder.CreateICmp(ICmpInst::ICMP_SLT, Add, Limit, "cond"));
768   auto *Br = cast<Instruction>(Builder.CreateCondBr(Cond, L, Post));
769   Phi->addIncoming(ConstantInt::get(T_int64, 0), LPh);
770   Phi->addIncoming(Add, L);
771 
772   Builder.SetInsertPoint(Post);
773   Builder.CreateRetVoid();
774 
775   ScalarEvolution SE = buildSE(*F);
776   auto *Loop = LI->getLoopFor(L);
777   const SCEV *EC = SE.getBackedgeTakenCount(Loop);
778   EXPECT_FALSE(isa<SCEVCouldNotCompute>(EC));
779   EXPECT_TRUE(isa<SCEVConstant>(EC));
780   EXPECT_EQ(cast<SCEVConstant>(EC)->getAPInt().getLimitedValue(), 999u);
781 
782   // The add recurrence {5,+,1} does not correspond to any PHI in the IR, and
783   // that is relevant to this test.
784   auto *Five = SE.getConstant(APInt(/*numBits=*/64, 5));
785   auto *AR =
786       SE.getAddRecExpr(Five, SE.getOne(T_int64), Loop, SCEV::FlagAnyWrap);
787   const SCEV *ARAtLoopExit = SE.getSCEVAtScope(AR, nullptr);
788   EXPECT_FALSE(isa<SCEVCouldNotCompute>(ARAtLoopExit));
789   EXPECT_TRUE(isa<SCEVConstant>(ARAtLoopExit));
790   EXPECT_EQ(cast<SCEVConstant>(ARAtLoopExit)->getAPInt().getLimitedValue(),
791             1004u);
792 
793   SE.forgetLoop(Loop);
794   Br->eraseFromParent();
795   Cond->eraseFromParent();
796 
797   Builder.SetInsertPoint(L);
798   auto *NewCond = Builder.CreateICmp(
799       ICmpInst::ICMP_SLT, Add, ConstantInt::get(T_int64, 2000), "new.cond");
800   Builder.CreateCondBr(NewCond, L, Post);
801   const SCEV *NewEC = SE.getBackedgeTakenCount(Loop);
802   EXPECT_FALSE(isa<SCEVCouldNotCompute>(NewEC));
803   EXPECT_TRUE(isa<SCEVConstant>(NewEC));
804   EXPECT_EQ(cast<SCEVConstant>(NewEC)->getAPInt().getLimitedValue(), 1999u);
805   const SCEV *NewARAtLoopExit = SE.getSCEVAtScope(AR, nullptr);
806   EXPECT_FALSE(isa<SCEVCouldNotCompute>(NewARAtLoopExit));
807   EXPECT_TRUE(isa<SCEVConstant>(NewARAtLoopExit));
808   EXPECT_EQ(cast<SCEVConstant>(NewARAtLoopExit)->getAPInt().getLimitedValue(),
809             2004u);
810 }
811 
812 // Make sure that SCEV invalidates exit limits after invalidating the values it
813 // depends on when we forget a value.
814 TEST_F(ScalarEvolutionsTest, SCEVExitLimitForgetValue) {
815   /*
816    * Create the following code:
817    * func(i64 addrspace(10)* %arg)
818    * top:
819    *  br label %L.ph
820    * L.ph:
821    *  %load = load i64 addrspace(10)* %arg
822    *  br label %L
823    * L:
824    *  %phi = phi i64 [i64 0, %L.ph], [ %add, %L2 ]
825    *  %add = add i64 %phi2, 1
826    *  %cond = icmp slt i64 %add, %load ; then becomes 2000.
827    *  br i1 %cond, label %post, label %L2
828    * post:
829    *  ret void
830    *
831    */
832 
833   // Create a module with non-integral pointers in it's datalayout
834   Module NIM("nonintegral", Context);
835   std::string DataLayout = M.getDataLayoutStr();
836   if (!DataLayout.empty())
837     DataLayout += "-";
838   DataLayout += "ni:10";
839   NIM.setDataLayout(DataLayout);
840 
841   Type *T_int64 = Type::getInt64Ty(Context);
842   Type *T_pint64 = T_int64->getPointerTo(10);
843 
844   FunctionType *FTy =
845       FunctionType::get(Type::getVoidTy(Context), {T_pint64}, false);
846   Function *F = Function::Create(FTy, Function::ExternalLinkage, "foo", NIM);
847 
848   Argument *Arg = &*F->arg_begin();
849 
850   BasicBlock *Top = BasicBlock::Create(Context, "top", F);
851   BasicBlock *LPh = BasicBlock::Create(Context, "L.ph", F);
852   BasicBlock *L = BasicBlock::Create(Context, "L", F);
853   BasicBlock *Post = BasicBlock::Create(Context, "post", F);
854 
855   IRBuilder<> Builder(Top);
856   Builder.CreateBr(LPh);
857 
858   Builder.SetInsertPoint(LPh);
859   auto *Load = cast<Instruction>(Builder.CreateLoad(T_int64, Arg, "load"));
860   Builder.CreateBr(L);
861 
862   Builder.SetInsertPoint(L);
863   PHINode *Phi = Builder.CreatePHI(T_int64, 2);
864   auto *Add = cast<Instruction>(
865       Builder.CreateAdd(Phi, ConstantInt::get(T_int64, 1), "add"));
866   auto *Cond = cast<Instruction>(
867       Builder.CreateICmp(ICmpInst::ICMP_SLT, Add, Load, "cond"));
868   auto *Br = cast<Instruction>(Builder.CreateCondBr(Cond, L, Post));
869   Phi->addIncoming(ConstantInt::get(T_int64, 0), LPh);
870   Phi->addIncoming(Add, L);
871 
872   Builder.SetInsertPoint(Post);
873   Builder.CreateRetVoid();
874 
875   ScalarEvolution SE = buildSE(*F);
876   auto *Loop = LI->getLoopFor(L);
877   const SCEV *EC = SE.getBackedgeTakenCount(Loop);
878   EXPECT_FALSE(isa<SCEVCouldNotCompute>(EC));
879   EXPECT_FALSE(isa<SCEVConstant>(EC));
880 
881   SE.forgetValue(Load);
882   Br->eraseFromParent();
883   Cond->eraseFromParent();
884   Load->eraseFromParent();
885 
886   Builder.SetInsertPoint(L);
887   auto *NewCond = Builder.CreateICmp(
888       ICmpInst::ICMP_SLT, Add, ConstantInt::get(T_int64, 2000), "new.cond");
889   Builder.CreateCondBr(NewCond, L, Post);
890   const SCEV *NewEC = SE.getBackedgeTakenCount(Loop);
891   EXPECT_FALSE(isa<SCEVCouldNotCompute>(NewEC));
892   EXPECT_TRUE(isa<SCEVConstant>(NewEC));
893   EXPECT_EQ(cast<SCEVConstant>(NewEC)->getAPInt().getLimitedValue(), 1999u);
894 }
895 
896 TEST_F(ScalarEvolutionsTest, SCEVAddRecFromPHIwithLargeConstants) {
897   // Reference: https://reviews.llvm.org/D37265
898   // Make sure that SCEV does not blow up when constructing an AddRec
899   // with predicates for a phi with the update pattern:
900   //  (SExt/ZExt ix (Trunc iy (%SymbolicPHI) to ix) to iy) + InvariantAccum
901   // when either the initial value of the Phi or the InvariantAccum are
902   // constants that are too large to fit in an ix but are zero when truncated to
903   // ix.
904   FunctionType *FTy =
905       FunctionType::get(Type::getVoidTy(Context), std::vector<Type *>(), false);
906   Function *F =
907       Function::Create(FTy, Function::ExternalLinkage, "addrecphitest", M);
908 
909   /*
910     Create IR:
911     entry:
912      br label %loop
913     loop:
914      %0 = phi i64 [-9223372036854775808, %entry], [%3, %loop]
915      %1 = shl i64 %0, 32
916      %2 = ashr exact i64 %1, 32
917      %3 = add i64 %2, -9223372036854775808
918      br i1 undef, label %exit, label %loop
919     exit:
920      ret void
921    */
922   BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", F);
923   BasicBlock *LoopBB = BasicBlock::Create(Context, "loop", F);
924   BasicBlock *ExitBB = BasicBlock::Create(Context, "exit", F);
925 
926   // entry:
927   BranchInst::Create(LoopBB, EntryBB);
928   // loop:
929   auto *MinInt64 =
930       ConstantInt::get(Context, APInt(64, 0x8000000000000000U, true));
931   auto *Int64_32 = ConstantInt::get(Context, APInt(64, 32));
932   auto *Br = BranchInst::Create(
933       LoopBB, ExitBB, UndefValue::get(Type::getInt1Ty(Context)), LoopBB);
934   auto *Phi = PHINode::Create(Type::getInt64Ty(Context), 2, "", Br);
935   auto *Shl = BinaryOperator::CreateShl(Phi, Int64_32, "", Br);
936   auto *AShr = BinaryOperator::CreateExactAShr(Shl, Int64_32, "", Br);
937   auto *Add = BinaryOperator::CreateAdd(AShr, MinInt64, "", Br);
938   Phi->addIncoming(MinInt64, EntryBB);
939   Phi->addIncoming(Add, LoopBB);
940   // exit:
941   ReturnInst::Create(Context, nullptr, ExitBB);
942 
943   // Make sure that SCEV doesn't blow up
944   ScalarEvolution SE = buildSE(*F);
945   SCEVUnionPredicate Preds;
946   const SCEV *Expr = SE.getSCEV(Phi);
947   EXPECT_NE(nullptr, Expr);
948   EXPECT_TRUE(isa<SCEVUnknown>(Expr));
949   auto Result = SE.createAddRecFromPHIWithCasts(cast<SCEVUnknown>(Expr));
950 }
951 
952 TEST_F(ScalarEvolutionsTest, SCEVAddRecFromPHIwithLargeConstantAccum) {
953   // Make sure that SCEV does not blow up when constructing an AddRec
954   // with predicates for a phi with the update pattern:
955   //  (SExt/ZExt ix (Trunc iy (%SymbolicPHI) to ix) to iy) + InvariantAccum
956   // when the InvariantAccum is a constant that is too large to fit in an
957   // ix but are zero when truncated to ix, and the initial value of the
958   // phi is not a constant.
959   Type *Int32Ty = Type::getInt32Ty(Context);
960   SmallVector<Type *, 1> Types;
961   Types.push_back(Int32Ty);
962   FunctionType *FTy = FunctionType::get(Type::getVoidTy(Context), Types, false);
963   Function *F =
964       Function::Create(FTy, Function::ExternalLinkage, "addrecphitest", M);
965 
966   /*
967     Create IR:
968     define @addrecphitest(i32)
969     entry:
970      br label %loop
971     loop:
972      %1 = phi i32 [%0, %entry], [%4, %loop]
973      %2 = shl i32 %1, 16
974      %3 = ashr exact i32 %2, 16
975      %4 = add i32 %3, -2147483648
976      br i1 undef, label %exit, label %loop
977     exit:
978      ret void
979    */
980   BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", F);
981   BasicBlock *LoopBB = BasicBlock::Create(Context, "loop", F);
982   BasicBlock *ExitBB = BasicBlock::Create(Context, "exit", F);
983 
984   // entry:
985   BranchInst::Create(LoopBB, EntryBB);
986   // loop:
987   auto *MinInt32 = ConstantInt::get(Context, APInt(32, 0x80000000U, true));
988   auto *Int32_16 = ConstantInt::get(Context, APInt(32, 16));
989   auto *Br = BranchInst::Create(
990       LoopBB, ExitBB, UndefValue::get(Type::getInt1Ty(Context)), LoopBB);
991   auto *Phi = PHINode::Create(Int32Ty, 2, "", Br);
992   auto *Shl = BinaryOperator::CreateShl(Phi, Int32_16, "", Br);
993   auto *AShr = BinaryOperator::CreateExactAShr(Shl, Int32_16, "", Br);
994   auto *Add = BinaryOperator::CreateAdd(AShr, MinInt32, "", Br);
995   auto *Arg = &*(F->arg_begin());
996   Phi->addIncoming(Arg, EntryBB);
997   Phi->addIncoming(Add, LoopBB);
998   // exit:
999   ReturnInst::Create(Context, nullptr, ExitBB);
1000 
1001   // Make sure that SCEV doesn't blow up
1002   ScalarEvolution SE = buildSE(*F);
1003   SCEVUnionPredicate Preds;
1004   const SCEV *Expr = SE.getSCEV(Phi);
1005   EXPECT_NE(nullptr, Expr);
1006   EXPECT_TRUE(isa<SCEVUnknown>(Expr));
1007   auto Result = SE.createAddRecFromPHIWithCasts(cast<SCEVUnknown>(Expr));
1008 }
1009 
1010 TEST_F(ScalarEvolutionsTest, SCEVFoldSumOfTruncs) {
1011   // Verify that the following SCEV gets folded to a zero:
1012   //  (-1 * (trunc i64 (-1 * %0) to i32)) + (-1 * (trunc i64 %0 to i32)
1013   Type *ArgTy = Type::getInt64Ty(Context);
1014   Type *Int32Ty = Type::getInt32Ty(Context);
1015   SmallVector<Type *, 1> Types;
1016   Types.push_back(ArgTy);
1017   FunctionType *FTy = FunctionType::get(Type::getVoidTy(Context), Types, false);
1018   Function *F = Function::Create(FTy, Function::ExternalLinkage, "f", M);
1019   BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
1020   ReturnInst::Create(Context, nullptr, BB);
1021 
1022   ScalarEvolution SE = buildSE(*F);
1023 
1024   auto *Arg = &*(F->arg_begin());
1025   const auto *ArgSCEV = SE.getSCEV(Arg);
1026 
1027   // Build the SCEV
1028   const auto *A0 = SE.getNegativeSCEV(ArgSCEV);
1029   const auto *A1 = SE.getTruncateExpr(A0, Int32Ty);
1030   const auto *A = SE.getNegativeSCEV(A1);
1031 
1032   const auto *B0 = SE.getTruncateExpr(ArgSCEV, Int32Ty);
1033   const auto *B = SE.getNegativeSCEV(B0);
1034 
1035   const auto *Expr = SE.getAddExpr(A, B);
1036   // Verify that the SCEV was folded to 0
1037   const auto *ZeroConst = SE.getConstant(Int32Ty, 0);
1038   EXPECT_EQ(Expr, ZeroConst);
1039 }
1040 
1041 // Check logic of SCEV expression size computation.
1042 TEST_F(ScalarEvolutionsTest, SCEVComputeExpressionSize) {
1043   /*
1044    * Create the following code:
1045    * void func(i64 %a, i64 %b)
1046    * entry:
1047    *  %s1 = add i64 %a, 1
1048    *  %s2 = udiv i64 %s1, %b
1049    *  br label %exit
1050    * exit:
1051    *  ret
1052    */
1053 
1054   // Create a module.
1055   Module M("SCEVComputeExpressionSize", Context);
1056 
1057   Type *T_int64 = Type::getInt64Ty(Context);
1058 
1059   FunctionType *FTy =
1060       FunctionType::get(Type::getVoidTy(Context), { T_int64, T_int64 }, false);
1061   Function *F = Function::Create(FTy, Function::ExternalLinkage, "func", M);
1062   Argument *A = &*F->arg_begin();
1063   Argument *B = &*std::next(F->arg_begin());
1064   ConstantInt *C = ConstantInt::get(Context, APInt(64, 1));
1065 
1066   BasicBlock *Entry = BasicBlock::Create(Context, "entry", F);
1067   BasicBlock *Exit = BasicBlock::Create(Context, "exit", F);
1068 
1069   IRBuilder<> Builder(Entry);
1070   auto *S1 = cast<Instruction>(Builder.CreateAdd(A, C, "s1"));
1071   auto *S2 = cast<Instruction>(Builder.CreateUDiv(S1, B, "s2"));
1072   Builder.CreateBr(Exit);
1073 
1074   Builder.SetInsertPoint(Exit);
1075   Builder.CreateRetVoid();
1076 
1077   ScalarEvolution SE = buildSE(*F);
1078   // Get S2 first to move it to cache.
1079   const SCEV *AS = SE.getSCEV(A);
1080   const SCEV *BS = SE.getSCEV(B);
1081   const SCEV *CS = SE.getSCEV(C);
1082   const SCEV *S1S = SE.getSCEV(S1);
1083   const SCEV *S2S = SE.getSCEV(S2);
1084   EXPECT_EQ(AS->getExpressionSize(), 1u);
1085   EXPECT_EQ(BS->getExpressionSize(), 1u);
1086   EXPECT_EQ(CS->getExpressionSize(), 1u);
1087   EXPECT_EQ(S1S->getExpressionSize(), 3u);
1088   EXPECT_EQ(S2S->getExpressionSize(), 5u);
1089 }
1090 
1091 TEST_F(ScalarEvolutionsTest, SCEVLoopDecIntrinsic) {
1092   LLVMContext C;
1093   SMDiagnostic Err;
1094   std::unique_ptr<Module> M = parseAssemblyString(
1095       "define void @foo(i32 %N) { "
1096       "entry: "
1097       "  %cmp3 = icmp sgt i32 %N, 0 "
1098       "  br i1 %cmp3, label %for.body, label %for.cond.cleanup "
1099       "for.cond.cleanup: "
1100       "  ret void "
1101       "for.body: "
1102       "  %i.04 = phi i32 [ %inc, %for.body ], [ 100, %entry ] "
1103       "  %inc = call i32 @llvm.loop.decrement.reg.i32.i32.i32(i32 %i.04, i32 1) "
1104       "  %exitcond = icmp ne i32 %inc, 0 "
1105       "  br i1 %exitcond, label %for.cond.cleanup, label %for.body "
1106       "} "
1107       "declare i32 @llvm.loop.decrement.reg.i32.i32.i32(i32, i32) ",
1108       Err, C);
1109 
1110   ASSERT_TRUE(M && "Could not parse module?");
1111   ASSERT_TRUE(!verifyModule(*M) && "Must have been well formed!");
1112 
1113   runWithSE(*M, "foo", [&](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
1114     auto *ScevInc = SE.getSCEV(getInstructionByName(F, "inc"));
1115     EXPECT_TRUE(isa<SCEVAddRecExpr>(ScevInc));
1116   });
1117 }
1118 
1119 TEST_F(ScalarEvolutionsTest, SCEVComputeConstantDifference) {
1120   LLVMContext C;
1121   SMDiagnostic Err;
1122   std::unique_ptr<Module> M = parseAssemblyString(
1123       "define void @foo(i32 %sz, i32 %pp) { "
1124       "entry: "
1125       "  %v0 = add i32 %pp, 0 "
1126       "  %v3 = add i32 %pp, 3 "
1127       "  br label %loop.body "
1128       "loop.body: "
1129       "  %iv = phi i32 [ %iv.next, %loop.body ], [ 0, %entry ] "
1130       "  %xa = add nsw i32 %iv, %v0 "
1131       "  %yy = add nsw i32 %iv, %v3 "
1132       "  %xb = sub nsw i32 %yy, 3 "
1133       "  %iv.next = add nsw i32 %iv, 1 "
1134       "  %cmp = icmp sle i32 %iv.next, %sz "
1135       "  br i1 %cmp, label %loop.body, label %exit "
1136       "exit: "
1137       "  ret void "
1138       "} ",
1139       Err, C);
1140 
1141   ASSERT_TRUE(M && "Could not parse module?");
1142   ASSERT_TRUE(!verifyModule(*M) && "Must have been well formed!");
1143 
1144   runWithSE(*M, "foo", [](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
1145     auto *ScevV0 = SE.getSCEV(getInstructionByName(F, "v0")); // %pp
1146     auto *ScevV3 = SE.getSCEV(getInstructionByName(F, "v3")); // (3 + %pp)
1147     auto *ScevIV = SE.getSCEV(getInstructionByName(F, "iv")); // {0,+,1}
1148     auto *ScevXA = SE.getSCEV(getInstructionByName(F, "xa")); // {%pp,+,1}
1149     auto *ScevYY = SE.getSCEV(getInstructionByName(F, "yy")); // {(3 + %pp),+,1}
1150     auto *ScevXB = SE.getSCEV(getInstructionByName(F, "xb")); // {%pp,+,1}
1151     auto *ScevIVNext = SE.getSCEV(getInstructionByName(F, "iv.next")); // {1,+,1}
1152 
1153     auto diff = [&SE](const SCEV *LHS, const SCEV *RHS) -> Optional<int> {
1154       auto ConstantDiffOrNone = computeConstantDifference(SE, LHS, RHS);
1155       if (!ConstantDiffOrNone)
1156         return None;
1157 
1158       auto ExtDiff = ConstantDiffOrNone->getSExtValue();
1159       int Diff = ExtDiff;
1160       assert(Diff == ExtDiff && "Integer overflow");
1161       return Diff;
1162     };
1163 
1164     EXPECT_EQ(diff(ScevV3, ScevV0), 3);
1165     EXPECT_EQ(diff(ScevV0, ScevV3), -3);
1166     EXPECT_EQ(diff(ScevV0, ScevV0), 0);
1167     EXPECT_EQ(diff(ScevV3, ScevV3), 0);
1168     EXPECT_EQ(diff(ScevIV, ScevIV), 0);
1169     EXPECT_EQ(diff(ScevXA, ScevXB), 0);
1170     EXPECT_EQ(diff(ScevXA, ScevYY), -3);
1171     EXPECT_EQ(diff(ScevYY, ScevXB), 3);
1172     EXPECT_EQ(diff(ScevIV, ScevIVNext), -1);
1173     EXPECT_EQ(diff(ScevIVNext, ScevIV), 1);
1174     EXPECT_EQ(diff(ScevIVNext, ScevIVNext), 0);
1175     EXPECT_EQ(diff(ScevV0, ScevIV), None);
1176     EXPECT_EQ(diff(ScevIVNext, ScevV3), None);
1177     EXPECT_EQ(diff(ScevYY, ScevV3), None);
1178   });
1179 }
1180 
1181 TEST_F(ScalarEvolutionsTest, SCEVrewriteUnknowns) {
1182   LLVMContext C;
1183   SMDiagnostic Err;
1184   std::unique_ptr<Module> M = parseAssemblyString(
1185       "define void @foo(i32 %i) { "
1186       "entry: "
1187       "  %cmp3 = icmp ult i32 %i, 16 "
1188       "  br i1 %cmp3, label %loop.body, label %exit "
1189       "loop.body: "
1190       "  %iv = phi i32 [ %iv.next, %loop.body ], [ %i, %entry ] "
1191       "  %iv.next = add nsw i32 %iv, 1 "
1192       "  %cmp = icmp eq i32 %iv.next, 16 "
1193       "  br i1 %cmp, label %exit, label %loop.body "
1194       "exit: "
1195       "  ret void "
1196       "} ",
1197       Err, C);
1198 
1199   ASSERT_TRUE(M && "Could not parse module?");
1200   ASSERT_TRUE(!verifyModule(*M) && "Must have been well formed!");
1201 
1202   runWithSE(*M, "foo", [](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
1203     auto *ScevIV = SE.getSCEV(getInstructionByName(F, "iv")); // {0,+,1}
1204     auto *ScevI = SE.getSCEV(getArgByName(F, "i"));           // {0,+,1}
1205 
1206     ValueToSCEVMapTy RewriteMap;
1207     RewriteMap[cast<SCEVUnknown>(ScevI)->getValue()] =
1208         SE.getUMinExpr(ScevI, SE.getConstant(ScevI->getType(), 17));
1209     auto *WithUMin = SCEVParameterRewriter::rewrite(ScevIV, SE, RewriteMap);
1210 
1211     EXPECT_NE(WithUMin, ScevIV);
1212     auto *AR = dyn_cast<SCEVAddRecExpr>(WithUMin);
1213     EXPECT_TRUE(AR);
1214     EXPECT_EQ(AR->getStart(),
1215               SE.getUMinExpr(ScevI, SE.getConstant(ScevI->getType(), 17)));
1216     EXPECT_EQ(AR->getStepRecurrence(SE),
1217               cast<SCEVAddRecExpr>(ScevIV)->getStepRecurrence(SE));
1218   });
1219 }
1220 
1221 TEST_F(ScalarEvolutionsTest, SCEVAddNUW) {
1222   LLVMContext C;
1223   SMDiagnostic Err;
1224   std::unique_ptr<Module> M = parseAssemblyString("define void @foo(i32 %x) { "
1225                                                   "  ret void "
1226                                                   "} ",
1227                                                   Err, C);
1228 
1229   ASSERT_TRUE(M && "Could not parse module?");
1230   ASSERT_TRUE(!verifyModule(*M) && "Must have been well formed!");
1231 
1232   runWithSE(*M, "foo", [](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
1233     auto *X = SE.getSCEV(getArgByName(F, "x"));
1234     auto *One = SE.getOne(X->getType());
1235     auto *Sum = SE.getAddExpr(X, One, SCEV::FlagNUW);
1236     EXPECT_TRUE(SE.isKnownPredicate(ICmpInst::ICMP_UGE, Sum, X));
1237     EXPECT_TRUE(SE.isKnownPredicate(ICmpInst::ICMP_UGT, Sum, X));
1238   });
1239 }
1240 
1241 TEST_F(ScalarEvolutionsTest, SCEVgetRanges) {
1242   LLVMContext C;
1243   SMDiagnostic Err;
1244   std::unique_ptr<Module> M = parseAssemblyString(
1245       "define void @foo(i32 %i) { "
1246       "entry: "
1247       "  br label %loop.body "
1248       "loop.body: "
1249       "  %iv = phi i32 [ %iv.next, %loop.body ], [ 0, %entry ] "
1250       "  %iv.next = add nsw i32 %iv, 1 "
1251       "  %cmp = icmp eq i32 %iv.next, 16 "
1252       "  br i1 %cmp, label %exit, label %loop.body "
1253       "exit: "
1254       "  ret void "
1255       "} ",
1256       Err, C);
1257 
1258   runWithSE(*M, "foo", [](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
1259     auto *ScevIV = SE.getSCEV(getInstructionByName(F, "iv")); // {0,+,1}
1260     auto *ScevI = SE.getSCEV(getArgByName(F, "i"));
1261     EXPECT_EQ(SE.getUnsignedRange(ScevIV).getLower(), 0);
1262     EXPECT_EQ(SE.getUnsignedRange(ScevIV).getUpper(), 16);
1263 
1264     auto *Add = SE.getAddExpr(ScevI, ScevIV);
1265     ValueToSCEVMapTy RewriteMap;
1266     RewriteMap[cast<SCEVUnknown>(ScevI)->getValue()] =
1267         SE.getUMinExpr(ScevI, SE.getConstant(ScevI->getType(), 17));
1268     auto *AddWithUMin = SCEVParameterRewriter::rewrite(Add, SE, RewriteMap);
1269     EXPECT_EQ(SE.getUnsignedRange(AddWithUMin).getLower(), 0);
1270     EXPECT_EQ(SE.getUnsignedRange(AddWithUMin).getUpper(), 33);
1271   });
1272 }
1273 
1274 TEST_F(ScalarEvolutionsTest, SCEVgetExitLimitForGuardedLoop) {
1275   LLVMContext C;
1276   SMDiagnostic Err;
1277   std::unique_ptr<Module> M = parseAssemblyString(
1278       "define void @foo(i32 %i) { "
1279       "entry: "
1280       "  %cmp3 = icmp ult i32 %i, 16 "
1281       "  br i1 %cmp3, label %loop.body, label %exit "
1282       "loop.body: "
1283       "  %iv = phi i32 [ %iv.next, %loop.body ], [ %i, %entry ] "
1284       "  %iv.next = add nsw i32 %iv, 1 "
1285       "  %cmp = icmp eq i32 %iv.next, 16 "
1286       "  br i1 %cmp, label %exit, label %loop.body "
1287       "exit: "
1288       "  ret void "
1289       "} ",
1290       Err, C);
1291 
1292   ASSERT_TRUE(M && "Could not parse module?");
1293   ASSERT_TRUE(!verifyModule(*M) && "Must have been well formed!");
1294 
1295   runWithSE(*M, "foo", [](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
1296     auto *ScevIV = SE.getSCEV(getInstructionByName(F, "iv")); // {0,+,1}
1297     const Loop *L = cast<SCEVAddRecExpr>(ScevIV)->getLoop();
1298 
1299     const SCEV *BTC = SE.getBackedgeTakenCount(L);
1300     EXPECT_FALSE(isa<SCEVConstant>(BTC));
1301     const SCEV *MaxBTC = SE.getConstantMaxBackedgeTakenCount(L);
1302     EXPECT_EQ(cast<SCEVConstant>(MaxBTC)->getAPInt(), 15);
1303   });
1304 }
1305 
1306 TEST_F(ScalarEvolutionsTest, ImpliedViaAddRecStart) {
1307   LLVMContext C;
1308   SMDiagnostic Err;
1309   std::unique_ptr<Module> M = parseAssemblyString(
1310       "define void @foo(i32* %p) { "
1311       "entry: "
1312       "  %x = load i32, i32* %p, !range !0 "
1313       "  br label %loop "
1314       "loop: "
1315       "  %iv = phi i32 [ %x, %entry], [%iv.next, %backedge] "
1316       "  %ne.check = icmp ne i32 %iv, 0 "
1317       "  br i1 %ne.check, label %backedge, label %exit "
1318       "backedge: "
1319       "  %iv.next = add i32 %iv, -1 "
1320       "  br label %loop "
1321       "exit:"
1322       "  ret void "
1323       "} "
1324       "!0 = !{i32 0, i32 2147483647}",
1325       Err, C);
1326 
1327   ASSERT_TRUE(M && "Could not parse module?");
1328   ASSERT_TRUE(!verifyModule(*M) && "Must have been well formed!");
1329 
1330   runWithSE(*M, "foo", [](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
1331     auto *X = SE.getSCEV(getInstructionByName(F, "x"));
1332     auto *Context = getInstructionByName(F, "iv.next");
1333     EXPECT_TRUE(SE.isKnownPredicateAt(ICmpInst::ICMP_NE, X,
1334                                       SE.getZero(X->getType()), Context));
1335   });
1336 }
1337 
1338 TEST_F(ScalarEvolutionsTest, UnsignedIsImpliedViaOperations) {
1339   LLVMContext C;
1340   SMDiagnostic Err;
1341   std::unique_ptr<Module> M =
1342       parseAssemblyString("define void @foo(i32* %p1, i32* %p2) { "
1343                           "entry: "
1344                           "  %x = load i32, i32* %p1, !range !0 "
1345                           "  %cond = icmp ne i32 %x, 0 "
1346                           "  br i1 %cond, label %guarded, label %exit "
1347                           "guarded: "
1348                           "  %y = add i32 %x, -1 "
1349                           "  ret void "
1350                           "exit: "
1351                           "  ret void "
1352                           "} "
1353                           "!0 = !{i32 0, i32 2147483647}",
1354                           Err, C);
1355 
1356   ASSERT_TRUE(M && "Could not parse module?");
1357   ASSERT_TRUE(!verifyModule(*M) && "Must have been well formed!");
1358 
1359   runWithSE(*M, "foo", [](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
1360     auto *X = SE.getSCEV(getInstructionByName(F, "x"));
1361     auto *Y = SE.getSCEV(getInstructionByName(F, "y"));
1362     auto *Guarded = getInstructionByName(F, "y")->getParent();
1363     ASSERT_TRUE(Guarded);
1364     EXPECT_TRUE(
1365         SE.isBasicBlockEntryGuardedByCond(Guarded, ICmpInst::ICMP_ULT, Y, X));
1366     EXPECT_TRUE(
1367         SE.isBasicBlockEntryGuardedByCond(Guarded, ICmpInst::ICMP_UGT, X, Y));
1368   });
1369 }
1370 
1371 TEST_F(ScalarEvolutionsTest, ProveImplicationViaNarrowing) {
1372   LLVMContext C;
1373   SMDiagnostic Err;
1374   std::unique_ptr<Module> M = parseAssemblyString(
1375       "define i32 @foo(i32 %start, i32* %q) { "
1376       "entry: "
1377       "  %wide.start = zext i32 %start to i64 "
1378       "  br label %loop "
1379       "loop: "
1380       "  %wide.iv = phi i64 [%wide.start, %entry], [%wide.iv.next, %backedge] "
1381       "  %iv = phi i32 [%start, %entry], [%iv.next, %backedge] "
1382       "  %cond = icmp eq i64 %wide.iv, 0 "
1383       "  br i1 %cond, label %exit, label %backedge "
1384       "backedge: "
1385       "  %iv.next = add i32 %iv, -1 "
1386       "  %index = zext i32 %iv.next to i64 "
1387       "  %load.addr = getelementptr i32, i32* %q, i64 %index "
1388       "  %stop = load i32, i32* %load.addr "
1389       "  %loop.cond = icmp eq i32 %stop, 0 "
1390       "  %wide.iv.next = add nsw i64 %wide.iv, -1 "
1391       "  br i1 %loop.cond, label %loop, label %failure "
1392       "exit: "
1393       "  ret i32 0 "
1394       "failure: "
1395       "  unreachable "
1396       "} ",
1397       Err, C);
1398 
1399   ASSERT_TRUE(M && "Could not parse module?");
1400   ASSERT_TRUE(!verifyModule(*M) && "Must have been well formed!");
1401 
1402   runWithSE(*M, "foo", [](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
1403     auto *IV = SE.getSCEV(getInstructionByName(F, "iv"));
1404     auto *Zero = SE.getZero(IV->getType());
1405     auto *Backedge = getInstructionByName(F, "iv.next")->getParent();
1406     ASSERT_TRUE(Backedge);
1407     (void)IV;
1408     (void)Zero;
1409     // FIXME: This can only be proved with turned on option
1410     // scalar-evolution-use-expensive-range-sharpening which is currently off.
1411     // Enable the check once it's switched true by default.
1412     // EXPECT_TRUE(SE.isBasicBlockEntryGuardedByCond(Backedge,
1413     //                                               ICmpInst::ICMP_UGT,
1414     //                                               IV, Zero));
1415   });
1416 }
1417 
1418 TEST_F(ScalarEvolutionsTest, ImpliedCond) {
1419   LLVMContext C;
1420   SMDiagnostic Err;
1421   std::unique_ptr<Module> M = parseAssemblyString(
1422       "define void @foo(i32 %len) { "
1423       "entry: "
1424       "  br label %loop "
1425       "loop: "
1426       "  %iv = phi i32 [ 0, %entry], [%iv.next, %loop] "
1427       "  %iv.next = add nsw i32 %iv, 1 "
1428       "  %cmp = icmp slt i32 %iv, %len "
1429       "  br i1 %cmp, label %loop, label %exit "
1430       "exit:"
1431       "  ret void "
1432       "}",
1433       Err, C);
1434 
1435   ASSERT_TRUE(M && "Could not parse module?");
1436   ASSERT_TRUE(!verifyModule(*M) && "Must have been well formed!");
1437 
1438   runWithSE(*M, "foo", [](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
1439     Instruction *IV = getInstructionByName(F, "iv");
1440     Type *Ty = IV->getType();
1441     const SCEV *Zero = SE.getZero(Ty);
1442     const SCEV *MinusOne = SE.getMinusOne(Ty);
1443     // {0,+,1}<nuw><nsw>
1444     const SCEV *AddRec_0_1 = SE.getSCEV(IV);
1445     // {0,+,-1}<nw>
1446     const SCEV *AddRec_0_N1 = SE.getNegativeSCEV(AddRec_0_1);
1447 
1448     // {0,+,1}<nuw><nsw> > 0  ->  {0,+,-1}<nw> < 0
1449     EXPECT_TRUE(isImpliedCond(SE, ICmpInst::ICMP_SLT, AddRec_0_N1, Zero,
1450                                   ICmpInst::ICMP_SGT, AddRec_0_1, Zero));
1451     // {0,+,-1}<nw> < -1  ->  {0,+,1}<nuw><nsw> > 0
1452     EXPECT_TRUE(isImpliedCond(SE, ICmpInst::ICMP_SGT, AddRec_0_1, Zero,
1453                                   ICmpInst::ICMP_SLT, AddRec_0_N1, MinusOne));
1454   });
1455 }
1456 
1457 TEST_F(ScalarEvolutionsTest, MatchURem) {
1458   LLVMContext C;
1459   SMDiagnostic Err;
1460   std::unique_ptr<Module> M = parseAssemblyString(
1461       "target datalayout = \"e-m:e-p:32:32-f64:32:64-f80:32-n8:16:32-S128\" "
1462       " "
1463       "define void @test(i32 %a, i32 %b, i16 %c, i64 %d) {"
1464       "entry: "
1465       "  %rem1 = urem i32 %a, 2"
1466       "  %rem2 = urem i32 %a, 5"
1467       "  %rem3 = urem i32 %a, %b"
1468       "  %c.ext = zext i16 %c to i32"
1469       "  %rem4 = urem i32 %c.ext, 2"
1470       "  %ext = zext i32 %rem4 to i64"
1471       "  %rem5 = urem i64 %d, 17179869184"
1472       "  ret void "
1473       "} ",
1474       Err, C);
1475 
1476   assert(M && "Could not parse module?");
1477   assert(!verifyModule(*M) && "Must have been well formed!");
1478 
1479   runWithSE(*M, "test", [&](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
1480     for (auto *N : {"rem1", "rem2", "rem3", "rem5"}) {
1481       auto *URemI = getInstructionByName(F, N);
1482       auto *S = SE.getSCEV(URemI);
1483       const SCEV *LHS, *RHS;
1484       EXPECT_TRUE(matchURem(SE, S, LHS, RHS));
1485       EXPECT_EQ(LHS, SE.getSCEV(URemI->getOperand(0)));
1486       EXPECT_EQ(RHS, SE.getSCEV(URemI->getOperand(1)));
1487       EXPECT_EQ(LHS->getType(), S->getType());
1488       EXPECT_EQ(RHS->getType(), S->getType());
1489     }
1490 
1491     // Check the case where the urem operand is zero-extended. Make sure the
1492     // match results are extended to the size of the input expression.
1493     auto *Ext = getInstructionByName(F, "ext");
1494     auto *URem1 = getInstructionByName(F, "rem4");
1495     auto *S = SE.getSCEV(Ext);
1496     const SCEV *LHS, *RHS;
1497     EXPECT_TRUE(matchURem(SE, S, LHS, RHS));
1498     EXPECT_NE(LHS, SE.getSCEV(URem1->getOperand(0)));
1499     // RHS and URem1->getOperand(1) have different widths, so compare the
1500     // integer values.
1501     EXPECT_EQ(cast<SCEVConstant>(RHS)->getValue()->getZExtValue(),
1502               cast<SCEVConstant>(SE.getSCEV(URem1->getOperand(1)))
1503                   ->getValue()
1504                   ->getZExtValue());
1505     EXPECT_EQ(LHS->getType(), S->getType());
1506     EXPECT_EQ(RHS->getType(), S->getType());
1507   });
1508 }
1509 
1510 TEST_F(ScalarEvolutionsTest, SCEVUDivFloorCeiling) {
1511   LLVMContext C;
1512   SMDiagnostic Err;
1513   std::unique_ptr<Module> M = parseAssemblyString("define void @foo() { "
1514                                                   "  ret void "
1515                                                   "} ",
1516                                                   Err, C);
1517 
1518   ASSERT_TRUE(M && "Could not parse module?");
1519   ASSERT_TRUE(!verifyModule(*M) && "Must have been well formed!");
1520 
1521   runWithSE(*M, "foo", [](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
1522     // Check that SCEV's udiv and uceil handling produce the correct results
1523     // for all 8 bit options. Div-by-zero is deliberately excluded.
1524     for (unsigned N = 0; N < 256; N++)
1525       for (unsigned D = 1; D < 256; D++) {
1526         APInt NInt(8, N);
1527         APInt DInt(8, D);
1528         using namespace llvm::APIntOps;
1529         APInt FloorInt = RoundingUDiv(NInt, DInt, APInt::Rounding::DOWN);
1530         APInt CeilingInt = RoundingUDiv(NInt, DInt, APInt::Rounding::UP);
1531         auto *NS = SE.getConstant(NInt);
1532         auto *DS = SE.getConstant(DInt);
1533         auto *FloorS = cast<SCEVConstant>(SE.getUDivExpr(NS, DS));
1534         auto *CeilingS = cast<SCEVConstant>(SE.getUDivCeilSCEV(NS, DS));
1535         ASSERT_TRUE(FloorS->getAPInt() == FloorInt);
1536         ASSERT_TRUE(CeilingS->getAPInt() == CeilingInt);
1537       }
1538   });
1539 }
1540 
1541 TEST_F(ScalarEvolutionsTest, ComputeMaxTripCountFromArrayNormal) {
1542   LLVMContext C;
1543   SMDiagnostic Err;
1544   std::unique_ptr<Module> M = parseAssemblyString(
1545       "define void @foo(i32 signext %len) { "
1546       "entry: "
1547       "  %a = alloca [7 x i32], align 4 "
1548       "  %cmp4 = icmp sgt i32 %len, 0 "
1549       "  br i1 %cmp4, label %for.body.preheader, label %for.cond.cleanup "
1550       "for.body.preheader: "
1551       "  br label %for.body "
1552       "for.cond.cleanup.loopexit: "
1553       "  br label %for.cond.cleanup "
1554       "for.cond.cleanup: "
1555       "  ret void "
1556       "for.body: "
1557       "  %iv = phi i32 [ %inc, %for.body ], [ 0, %for.body.preheader ] "
1558       "  %idxprom = zext i32 %iv to i64 "
1559       "  %arrayidx = getelementptr inbounds [7 x i32], [7 x i32]* %a, i64 0, \
1560     i64 %idxprom "
1561       "  store i32 0, i32* %arrayidx, align 4 "
1562       "  %inc = add nuw nsw i32 %iv, 1 "
1563       "  %cmp = icmp slt i32 %inc, %len "
1564       "  br i1 %cmp, label %for.body, label %for.cond.cleanup.loopexit "
1565       "} ",
1566       Err, C);
1567 
1568   ASSERT_TRUE(M && "Could not parse module?");
1569   ASSERT_TRUE(!verifyModule(*M) && "Must have been well formed!");
1570 
1571   runWithSE(*M, "foo", [](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
1572     auto *ScevIV = SE.getSCEV(getInstructionByName(F, "iv"));
1573     const Loop *L = cast<SCEVAddRecExpr>(ScevIV)->getLoop();
1574 
1575     const SCEV *ITC = SE.getConstantMaxTripCountFromArray(L);
1576     EXPECT_FALSE(isa<SCEVCouldNotCompute>(ITC));
1577     EXPECT_TRUE(isa<SCEVConstant>(ITC));
1578     EXPECT_EQ(cast<SCEVConstant>(ITC)->getAPInt().getSExtValue(), 8);
1579   });
1580 }
1581 
1582 TEST_F(ScalarEvolutionsTest, ComputeMaxTripCountFromZeroArray) {
1583   LLVMContext C;
1584   SMDiagnostic Err;
1585   std::unique_ptr<Module> M = parseAssemblyString(
1586       "define void @foo(i32 signext %len) { "
1587       "entry: "
1588       "  %a = alloca [0 x i32], align 4 "
1589       "  %cmp4 = icmp sgt i32 %len, 0 "
1590       "  br i1 %cmp4, label %for.body.preheader, label %for.cond.cleanup "
1591       "for.body.preheader: "
1592       "  br label %for.body "
1593       "for.cond.cleanup.loopexit: "
1594       "  br label %for.cond.cleanup "
1595       "for.cond.cleanup: "
1596       "  ret void "
1597       "for.body: "
1598       "  %iv = phi i32 [ %inc, %for.body ], [ 0, %for.body.preheader ] "
1599       "  %idxprom = zext i32 %iv to i64 "
1600       "  %arrayidx = getelementptr inbounds [0 x i32], [0 x i32]* %a, i64 0, \
1601     i64 %idxprom "
1602       "  store i32 0, i32* %arrayidx, align 4 "
1603       "  %inc = add nuw nsw i32 %iv, 1 "
1604       "  %cmp = icmp slt i32 %inc, %len "
1605       "  br i1 %cmp, label %for.body, label %for.cond.cleanup.loopexit "
1606       "} ",
1607       Err, C);
1608 
1609   ASSERT_TRUE(M && "Could not parse module?");
1610   ASSERT_TRUE(!verifyModule(*M) && "Must have been well formed!");
1611 
1612   runWithSE(*M, "foo", [](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
1613     auto *ScevIV = SE.getSCEV(getInstructionByName(F, "iv"));
1614     const Loop *L = cast<SCEVAddRecExpr>(ScevIV)->getLoop();
1615 
1616     const SCEV *ITC = SE.getConstantMaxTripCountFromArray(L);
1617     EXPECT_FALSE(isa<SCEVCouldNotCompute>(ITC));
1618     EXPECT_TRUE(isa<SCEVConstant>(ITC));
1619     EXPECT_EQ(cast<SCEVConstant>(ITC)->getAPInt().getSExtValue(), 1);
1620   });
1621 }
1622 
1623 TEST_F(ScalarEvolutionsTest, ComputeMaxTripCountFromExtremArray) {
1624   LLVMContext C;
1625   SMDiagnostic Err;
1626   std::unique_ptr<Module> M = parseAssemblyString(
1627       "define void @foo(i32 signext %len) { "
1628       "entry: "
1629       "  %a = alloca [4294967295 x i1], align 4 "
1630       "  %cmp4 = icmp sgt i32 %len, 0 "
1631       "  br i1 %cmp4, label %for.body.preheader, label %for.cond.cleanup "
1632       "for.body.preheader: "
1633       "  br label %for.body "
1634       "for.cond.cleanup.loopexit: "
1635       "  br label %for.cond.cleanup "
1636       "for.cond.cleanup: "
1637       "  ret void "
1638       "for.body: "
1639       "  %iv = phi i32 [ %inc, %for.body ], [ 0, %for.body.preheader ] "
1640       "  %idxprom = zext i32 %iv to i64 "
1641       "  %arrayidx = getelementptr inbounds [4294967295 x i1], \
1642     [4294967295 x i1]* %a, i64 0, i64 %idxprom "
1643       "  store i1 0, i1* %arrayidx, align 4 "
1644       "  %inc = add nuw nsw i32 %iv, 1 "
1645       "  %cmp = icmp slt i32 %inc, %len "
1646       "  br i1 %cmp, label %for.body, label %for.cond.cleanup.loopexit "
1647       "} ",
1648       Err, C);
1649 
1650   ASSERT_TRUE(M && "Could not parse module?");
1651   ASSERT_TRUE(!verifyModule(*M) && "Must have been well formed!");
1652 
1653   runWithSE(*M, "foo", [](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
1654     auto *ScevIV = SE.getSCEV(getInstructionByName(F, "iv"));
1655     const Loop *L = cast<SCEVAddRecExpr>(ScevIV)->getLoop();
1656 
1657     const SCEV *ITC = SE.getConstantMaxTripCountFromArray(L);
1658     EXPECT_TRUE(isa<SCEVCouldNotCompute>(ITC));
1659   });
1660 }
1661 
1662 TEST_F(ScalarEvolutionsTest, ComputeMaxTripCountFromArrayInBranch) {
1663   LLVMContext C;
1664   SMDiagnostic Err;
1665   std::unique_ptr<Module> M = parseAssemblyString(
1666       "define void @foo(i32 signext %len) { "
1667       "entry: "
1668       "  %a = alloca [8 x i32], align 4 "
1669       "  br label %for.cond "
1670       "for.cond: "
1671       "  %iv = phi i32 [ %inc, %for.inc ], [ 0, %entry ] "
1672       "  %cmp = icmp slt i32 %iv, %len "
1673       "  br i1 %cmp, label %for.body, label %for.cond.cleanup "
1674       "for.cond.cleanup: "
1675       "  br label %for.end "
1676       "for.body: "
1677       "  %cmp1 = icmp slt i32 %iv, 8 "
1678       "  br i1 %cmp1, label %if.then, label %if.end "
1679       "if.then: "
1680       "  %idxprom = sext i32 %iv to i64 "
1681       "  %arrayidx = getelementptr inbounds [8 x i32], [8 x i32]* %a, i64 0, \
1682     i64 %idxprom "
1683       "  store i32 0, i32* %arrayidx, align 4 "
1684       "  br label %if.end "
1685       "if.end: "
1686       "  br label %for.inc "
1687       "for.inc: "
1688       "  %inc = add nsw i32 %iv, 1 "
1689       "  br label %for.cond "
1690       "for.end: "
1691       "  ret void "
1692       "} ",
1693       Err, C);
1694 
1695   ASSERT_TRUE(M && "Could not parse module?");
1696   ASSERT_TRUE(!verifyModule(*M) && "Must have been well formed!");
1697 
1698   runWithSE(*M, "foo", [](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
1699     auto *ScevIV = SE.getSCEV(getInstructionByName(F, "iv"));
1700     const Loop *L = cast<SCEVAddRecExpr>(ScevIV)->getLoop();
1701 
1702     const SCEV *ITC = SE.getConstantMaxTripCountFromArray(L);
1703     EXPECT_TRUE(isa<SCEVCouldNotCompute>(ITC));
1704   });
1705 }
1706 
1707 TEST_F(ScalarEvolutionsTest, ComputeMaxTripCountFromMultiDemArray) {
1708   LLVMContext C;
1709   SMDiagnostic Err;
1710   std::unique_ptr<Module> M = parseAssemblyString(
1711       "define void @foo(i32 signext %len) { "
1712       "entry: "
1713       "  %a = alloca [3 x [5 x i32]], align 4 "
1714       "  br label %for.cond "
1715       "for.cond: "
1716       "  %iv = phi i32 [ %inc, %for.inc ], [ 0, %entry ] "
1717       "  %cmp = icmp slt i32 %iv, %len "
1718       "  br i1 %cmp, label %for.body, label %for.cond.cleanup "
1719       "for.cond.cleanup: "
1720       "  br label %for.end "
1721       "for.body: "
1722       "  %arrayidx = getelementptr inbounds [3 x [5 x i32]], \
1723     [3 x [5 x i32]]* %a, i64 0, i64 3 "
1724       "  %idxprom = sext i32 %iv to i64 "
1725       "  %arrayidx1 = getelementptr inbounds [5 x i32], [5 x i32]* %arrayidx, \
1726     i64 0, i64 %idxprom "
1727       "  store i32 0, i32* %arrayidx1, align 4"
1728       "  br label %for.inc "
1729       "for.inc: "
1730       "  %inc = add nsw i32 %iv, 1 "
1731       "  br label %for.cond "
1732       "for.end: "
1733       "  ret void "
1734       "} ",
1735       Err, C);
1736 
1737   ASSERT_TRUE(M && "Could not parse module?");
1738   ASSERT_TRUE(!verifyModule(*M) && "Must have been well formed!");
1739 
1740   runWithSE(*M, "foo", [](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
1741     auto *ScevIV = SE.getSCEV(getInstructionByName(F, "iv"));
1742     const Loop *L = cast<SCEVAddRecExpr>(ScevIV)->getLoop();
1743 
1744     const SCEV *ITC = SE.getConstantMaxTripCountFromArray(L);
1745     EXPECT_TRUE(isa<SCEVCouldNotCompute>(ITC));
1746   });
1747 }
1748 
1749 }  // end namespace llvm
1750