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 
68 TEST_F(ScalarEvolutionsTest, SCEVUnknownRAUW) {
69   FunctionType *FTy = FunctionType::get(Type::getVoidTy(Context),
70                                               std::vector<Type *>(), false);
71   Function *F = Function::Create(FTy, Function::ExternalLinkage, "f", M);
72   BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
73   ReturnInst::Create(Context, nullptr, BB);
74 
75   Type *Ty = Type::getInt1Ty(Context);
76   Constant *Init = Constant::getNullValue(Ty);
77   Value *V0 = new GlobalVariable(M, Ty, false, GlobalValue::ExternalLinkage, Init, "V0");
78   Value *V1 = new GlobalVariable(M, Ty, false, GlobalValue::ExternalLinkage, Init, "V1");
79   Value *V2 = new GlobalVariable(M, Ty, false, GlobalValue::ExternalLinkage, Init, "V2");
80 
81   ScalarEvolution SE = buildSE(*F);
82 
83   const SCEV *S0 = SE.getSCEV(V0);
84   const SCEV *S1 = SE.getSCEV(V1);
85   const SCEV *S2 = SE.getSCEV(V2);
86 
87   const SCEV *P0 = SE.getAddExpr(S0, S0);
88   const SCEV *P1 = SE.getAddExpr(S1, S1);
89   const SCEV *P2 = SE.getAddExpr(S2, S2);
90 
91   const SCEVMulExpr *M0 = cast<SCEVMulExpr>(P0);
92   const SCEVMulExpr *M1 = cast<SCEVMulExpr>(P1);
93   const SCEVMulExpr *M2 = cast<SCEVMulExpr>(P2);
94 
95   EXPECT_EQ(cast<SCEVConstant>(M0->getOperand(0))->getValue()->getZExtValue(),
96             2u);
97   EXPECT_EQ(cast<SCEVConstant>(M1->getOperand(0))->getValue()->getZExtValue(),
98             2u);
99   EXPECT_EQ(cast<SCEVConstant>(M2->getOperand(0))->getValue()->getZExtValue(),
100             2u);
101 
102   // Before the RAUWs, these are all pointing to separate values.
103   EXPECT_EQ(cast<SCEVUnknown>(M0->getOperand(1))->getValue(), V0);
104   EXPECT_EQ(cast<SCEVUnknown>(M1->getOperand(1))->getValue(), V1);
105   EXPECT_EQ(cast<SCEVUnknown>(M2->getOperand(1))->getValue(), V2);
106 
107   // Do some RAUWs.
108   V2->replaceAllUsesWith(V1);
109   V1->replaceAllUsesWith(V0);
110 
111   // After the RAUWs, these should all be pointing to V0.
112   EXPECT_EQ(cast<SCEVUnknown>(M0->getOperand(1))->getValue(), V0);
113   EXPECT_EQ(cast<SCEVUnknown>(M1->getOperand(1))->getValue(), V0);
114   EXPECT_EQ(cast<SCEVUnknown>(M2->getOperand(1))->getValue(), V0);
115 }
116 
117 TEST_F(ScalarEvolutionsTest, SimplifiedPHI) {
118   FunctionType *FTy = FunctionType::get(Type::getVoidTy(Context),
119                                               std::vector<Type *>(), false);
120   Function *F = Function::Create(FTy, Function::ExternalLinkage, "f", M);
121   BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", F);
122   BasicBlock *LoopBB = BasicBlock::Create(Context, "loop", F);
123   BasicBlock *ExitBB = BasicBlock::Create(Context, "exit", F);
124   BranchInst::Create(LoopBB, EntryBB);
125   BranchInst::Create(LoopBB, ExitBB, UndefValue::get(Type::getInt1Ty(Context)),
126                      LoopBB);
127   ReturnInst::Create(Context, nullptr, ExitBB);
128   auto *Ty = Type::getInt32Ty(Context);
129   auto *PN = PHINode::Create(Ty, 2, "", &*LoopBB->begin());
130   PN->addIncoming(Constant::getNullValue(Ty), EntryBB);
131   PN->addIncoming(UndefValue::get(Ty), LoopBB);
132   ScalarEvolution SE = buildSE(*F);
133   auto *S1 = SE.getSCEV(PN);
134   auto *S2 = SE.getSCEV(PN);
135   auto *ZeroConst = SE.getConstant(Ty, 0);
136 
137   // At some point, only the first call to getSCEV returned the simplified
138   // SCEVConstant and later calls just returned a SCEVUnknown referencing the
139   // PHI node.
140   EXPECT_EQ(S1, ZeroConst);
141   EXPECT_EQ(S1, S2);
142 }
143 
144 
145 static Instruction *getInstructionByName(Function &F, StringRef Name) {
146   for (auto &I : instructions(F))
147     if (I.getName() == Name)
148       return &I;
149   llvm_unreachable("Expected to find instruction!");
150 }
151 
152 static Value *getArgByName(Function &F, StringRef Name) {
153   for (auto &Arg : F.args())
154     if (Arg.getName() == Name)
155       return &Arg;
156   llvm_unreachable("Expected to find instruction!");
157 }
158 TEST_F(ScalarEvolutionsTest, CommutativeExprOperandOrder) {
159   LLVMContext C;
160   SMDiagnostic Err;
161   std::unique_ptr<Module> M = parseAssemblyString(
162       "target datalayout = \"e-m:e-p:32:32-f64:32:64-f80:32-n8:16:32-S128\" "
163       " "
164       "@var_0 = external global i32, align 4"
165       "@var_1 = external global i32, align 4"
166       "@var_2 = external global i32, align 4"
167       " "
168       "declare i32 @unknown(i32, i32, i32)"
169       " "
170       "define void @f_1(i8* nocapture %arr, i32 %n, i32* %A, i32* %B) "
171       "    local_unnamed_addr { "
172       "entry: "
173       "  %entrycond = icmp sgt i32 %n, 0 "
174       "  br i1 %entrycond, label %loop.ph, label %for.end "
175       " "
176       "loop.ph: "
177       "  %a = load i32, i32* %A, align 4 "
178       "  %b = load i32, i32* %B, align 4 "
179       "  %mul = mul nsw i32 %b, %a "
180       "  %iv0.init = getelementptr inbounds i8, i8* %arr, i32 %mul "
181       "  br label %loop "
182       " "
183       "loop: "
184       "  %iv0 = phi i8* [ %iv0.inc, %loop ], [ %iv0.init, %loop.ph ] "
185       "  %iv1 = phi i32 [ %iv1.inc, %loop ], [ 0, %loop.ph ] "
186       "  %conv = trunc i32 %iv1 to i8 "
187       "  store i8 %conv, i8* %iv0, align 1 "
188       "  %iv0.inc = getelementptr inbounds i8, i8* %iv0, i32 %b "
189       "  %iv1.inc = add nuw nsw i32 %iv1, 1 "
190       "  %exitcond = icmp eq i32 %iv1.inc, %n "
191       "  br i1 %exitcond, label %for.end.loopexit, label %loop "
192       " "
193       "for.end.loopexit: "
194       "  br label %for.end "
195       " "
196       "for.end: "
197       "  ret void "
198       "} "
199       " "
200       "define void @f_2(i32* %X, i32* %Y, i32* %Z) { "
201       "  %x = load i32, i32* %X "
202       "  %y = load i32, i32* %Y "
203       "  %z = load i32, i32* %Z "
204       "  ret void "
205       "} "
206       " "
207       "define void @f_3() { "
208       "  %x = load i32, i32* @var_0"
209       "  %y = load i32, i32* @var_1"
210       "  %z = load i32, i32* @var_2"
211       "  ret void"
212       "} "
213       " "
214       "define void @f_4(i32 %a, i32 %b, i32 %c) { "
215       "  %x = call i32 @unknown(i32 %a, i32 %b, i32 %c)"
216       "  %y = call i32 @unknown(i32 %b, i32 %c, i32 %a)"
217       "  %z = call i32 @unknown(i32 %c, i32 %a, i32 %b)"
218       "  ret void"
219       "} "
220       ,
221       Err, C);
222 
223   assert(M && "Could not parse module?");
224   assert(!verifyModule(*M) && "Must have been well formed!");
225 
226   runWithSE(*M, "f_1", [&](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
227     auto *IV0 = getInstructionByName(F, "iv0");
228     auto *IV0Inc = getInstructionByName(F, "iv0.inc");
229 
230     auto *FirstExprForIV0 = SE.getSCEV(IV0);
231     auto *FirstExprForIV0Inc = SE.getSCEV(IV0Inc);
232     auto *SecondExprForIV0 = SE.getSCEV(IV0);
233 
234     EXPECT_TRUE(isa<SCEVAddRecExpr>(FirstExprForIV0));
235     EXPECT_TRUE(isa<SCEVAddRecExpr>(FirstExprForIV0Inc));
236     EXPECT_TRUE(isa<SCEVAddRecExpr>(SecondExprForIV0));
237   });
238 
239   auto CheckCommutativeMulExprs = [&](ScalarEvolution &SE, const SCEV *A,
240                                       const SCEV *B, const SCEV *C) {
241     EXPECT_EQ(SE.getMulExpr(A, B), SE.getMulExpr(B, A));
242     EXPECT_EQ(SE.getMulExpr(B, C), SE.getMulExpr(C, B));
243     EXPECT_EQ(SE.getMulExpr(A, C), SE.getMulExpr(C, A));
244 
245     SmallVector<const SCEV *, 3> Ops0 = {A, B, C};
246     SmallVector<const SCEV *, 3> Ops1 = {A, C, B};
247     SmallVector<const SCEV *, 3> Ops2 = {B, A, C};
248     SmallVector<const SCEV *, 3> Ops3 = {B, C, A};
249     SmallVector<const SCEV *, 3> Ops4 = {C, B, A};
250     SmallVector<const SCEV *, 3> Ops5 = {C, A, B};
251 
252     auto *Mul0 = SE.getMulExpr(Ops0);
253     auto *Mul1 = SE.getMulExpr(Ops1);
254     auto *Mul2 = SE.getMulExpr(Ops2);
255     auto *Mul3 = SE.getMulExpr(Ops3);
256     auto *Mul4 = SE.getMulExpr(Ops4);
257     auto *Mul5 = SE.getMulExpr(Ops5);
258 
259     EXPECT_EQ(Mul0, Mul1) << "Expected " << *Mul0 << " == " << *Mul1;
260     EXPECT_EQ(Mul1, Mul2) << "Expected " << *Mul1 << " == " << *Mul2;
261     EXPECT_EQ(Mul2, Mul3) << "Expected " << *Mul2 << " == " << *Mul3;
262     EXPECT_EQ(Mul3, Mul4) << "Expected " << *Mul3 << " == " << *Mul4;
263     EXPECT_EQ(Mul4, Mul5) << "Expected " << *Mul4 << " == " << *Mul5;
264   };
265 
266   for (StringRef FuncName : {"f_2", "f_3", "f_4"})
267     runWithSE(
268         *M, FuncName, [&](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
269           CheckCommutativeMulExprs(SE, SE.getSCEV(getInstructionByName(F, "x")),
270                                    SE.getSCEV(getInstructionByName(F, "y")),
271                                    SE.getSCEV(getInstructionByName(F, "z")));
272         });
273 }
274 
275 TEST_F(ScalarEvolutionsTest, CompareSCEVComplexity) {
276   FunctionType *FTy =
277       FunctionType::get(Type::getVoidTy(Context), std::vector<Type *>(), false);
278   Function *F = Function::Create(FTy, Function::ExternalLinkage, "f", M);
279   BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", F);
280   BasicBlock *LoopBB = BasicBlock::Create(Context, "bb1", F);
281   BranchInst::Create(LoopBB, EntryBB);
282 
283   auto *Ty = Type::getInt32Ty(Context);
284   SmallVector<Instruction*, 8> Muls(8), Acc(8), NextAcc(8);
285 
286   Acc[0] = PHINode::Create(Ty, 2, "", LoopBB);
287   Acc[1] = PHINode::Create(Ty, 2, "", LoopBB);
288   Acc[2] = PHINode::Create(Ty, 2, "", LoopBB);
289   Acc[3] = PHINode::Create(Ty, 2, "", LoopBB);
290   Acc[4] = PHINode::Create(Ty, 2, "", LoopBB);
291   Acc[5] = PHINode::Create(Ty, 2, "", LoopBB);
292   Acc[6] = PHINode::Create(Ty, 2, "", LoopBB);
293   Acc[7] = PHINode::Create(Ty, 2, "", LoopBB);
294 
295   for (int i = 0; i < 20; i++) {
296     Muls[0] = BinaryOperator::CreateMul(Acc[0], Acc[0], "", LoopBB);
297     NextAcc[0] = BinaryOperator::CreateAdd(Muls[0], Acc[4], "", LoopBB);
298     Muls[1] = BinaryOperator::CreateMul(Acc[1], Acc[1], "", LoopBB);
299     NextAcc[1] = BinaryOperator::CreateAdd(Muls[1], Acc[5], "", LoopBB);
300     Muls[2] = BinaryOperator::CreateMul(Acc[2], Acc[2], "", LoopBB);
301     NextAcc[2] = BinaryOperator::CreateAdd(Muls[2], Acc[6], "", LoopBB);
302     Muls[3] = BinaryOperator::CreateMul(Acc[3], Acc[3], "", LoopBB);
303     NextAcc[3] = BinaryOperator::CreateAdd(Muls[3], Acc[7], "", LoopBB);
304 
305     Muls[4] = BinaryOperator::CreateMul(Acc[4], Acc[4], "", LoopBB);
306     NextAcc[4] = BinaryOperator::CreateAdd(Muls[4], Acc[0], "", LoopBB);
307     Muls[5] = BinaryOperator::CreateMul(Acc[5], Acc[5], "", LoopBB);
308     NextAcc[5] = BinaryOperator::CreateAdd(Muls[5], Acc[1], "", LoopBB);
309     Muls[6] = BinaryOperator::CreateMul(Acc[6], Acc[6], "", LoopBB);
310     NextAcc[6] = BinaryOperator::CreateAdd(Muls[6], Acc[2], "", LoopBB);
311     Muls[7] = BinaryOperator::CreateMul(Acc[7], Acc[7], "", LoopBB);
312     NextAcc[7] = BinaryOperator::CreateAdd(Muls[7], Acc[3], "", LoopBB);
313     Acc = NextAcc;
314   }
315 
316   auto II = LoopBB->begin();
317   for (int i = 0; i < 8; i++) {
318     PHINode *Phi = cast<PHINode>(&*II++);
319     Phi->addIncoming(Acc[i], LoopBB);
320     Phi->addIncoming(UndefValue::get(Ty), EntryBB);
321   }
322 
323   BasicBlock *ExitBB = BasicBlock::Create(Context, "bb2", F);
324   BranchInst::Create(LoopBB, ExitBB, UndefValue::get(Type::getInt1Ty(Context)),
325                      LoopBB);
326 
327   Acc[0] = BinaryOperator::CreateAdd(Acc[0], Acc[1], "", ExitBB);
328   Acc[1] = BinaryOperator::CreateAdd(Acc[2], Acc[3], "", ExitBB);
329   Acc[2] = BinaryOperator::CreateAdd(Acc[4], Acc[5], "", ExitBB);
330   Acc[3] = BinaryOperator::CreateAdd(Acc[6], Acc[7], "", ExitBB);
331   Acc[0] = BinaryOperator::CreateAdd(Acc[0], Acc[1], "", ExitBB);
332   Acc[1] = BinaryOperator::CreateAdd(Acc[2], Acc[3], "", ExitBB);
333   Acc[0] = BinaryOperator::CreateAdd(Acc[0], Acc[1], "", ExitBB);
334 
335   ReturnInst::Create(Context, nullptr, ExitBB);
336 
337   ScalarEvolution SE = buildSE(*F);
338 
339   EXPECT_NE(nullptr, SE.getSCEV(Acc[0]));
340 }
341 
342 TEST_F(ScalarEvolutionsTest, CompareValueComplexity) {
343   IntegerType *IntPtrTy = M.getDataLayout().getIntPtrType(Context);
344   PointerType *IntPtrPtrTy = IntPtrTy->getPointerTo();
345 
346   FunctionType *FTy =
347       FunctionType::get(Type::getVoidTy(Context), {IntPtrTy, IntPtrTy}, false);
348   Function *F = Function::Create(FTy, Function::ExternalLinkage, "f", M);
349   BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", F);
350 
351   Value *X = &*F->arg_begin();
352   Value *Y = &*std::next(F->arg_begin());
353 
354   const int ValueDepth = 10;
355   for (int i = 0; i < ValueDepth; i++) {
356     X = new LoadInst(IntPtrTy, new IntToPtrInst(X, IntPtrPtrTy, "", EntryBB),
357                      "",
358                      /*isVolatile*/ false, EntryBB);
359     Y = new LoadInst(IntPtrTy, new IntToPtrInst(Y, IntPtrPtrTy, "", EntryBB),
360                      "",
361                      /*isVolatile*/ false, EntryBB);
362   }
363 
364   auto *MulA = BinaryOperator::CreateMul(X, Y, "", EntryBB);
365   auto *MulB = BinaryOperator::CreateMul(Y, X, "", EntryBB);
366   ReturnInst::Create(Context, nullptr, EntryBB);
367 
368   // This test isn't checking for correctness.  Today making A and B resolve to
369   // the same SCEV would require deeper searching in CompareValueComplexity,
370   // which will slow down compilation.  However, this test can fail (with LLVM's
371   // behavior still being correct) if we ever have a smarter
372   // CompareValueComplexity that is both fast and more accurate.
373 
374   ScalarEvolution SE = buildSE(*F);
375   auto *A = SE.getSCEV(MulA);
376   auto *B = SE.getSCEV(MulB);
377   EXPECT_NE(A, B);
378 }
379 
380 TEST_F(ScalarEvolutionsTest, SCEVAddExpr) {
381   Type *Ty32 = Type::getInt32Ty(Context);
382   Type *ArgTys[] = {Type::getInt64Ty(Context), Ty32};
383 
384   FunctionType *FTy =
385       FunctionType::get(Type::getVoidTy(Context), ArgTys, false);
386   Function *F = Function::Create(FTy, Function::ExternalLinkage, "f", M);
387 
388   Argument *A1 = &*F->arg_begin();
389   Argument *A2 = &*(std::next(F->arg_begin()));
390   BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", F);
391 
392   Instruction *Trunc = CastInst::CreateTruncOrBitCast(A1, Ty32, "", EntryBB);
393   Instruction *Mul1 = BinaryOperator::CreateMul(Trunc, A2, "", EntryBB);
394   Instruction *Add1 = BinaryOperator::CreateAdd(Mul1, Trunc, "", EntryBB);
395   Mul1 = BinaryOperator::CreateMul(Add1, Trunc, "", EntryBB);
396   Instruction *Add2 = BinaryOperator::CreateAdd(Mul1, Add1, "", EntryBB);
397   // FIXME: The size of this is arbitrary and doesn't seem to change the
398   // result, but SCEV will do quadratic work for these so a large number here
399   // will be extremely slow. We should revisit what and how this is testing
400   // SCEV.
401   for (int i = 0; i < 10; i++) {
402     Mul1 = BinaryOperator::CreateMul(Add2, Add1, "", EntryBB);
403     Add1 = Add2;
404     Add2 = BinaryOperator::CreateAdd(Mul1, Add1, "", EntryBB);
405   }
406 
407   ReturnInst::Create(Context, nullptr, EntryBB);
408   ScalarEvolution SE = buildSE(*F);
409   EXPECT_NE(nullptr, SE.getSCEV(Mul1));
410 }
411 
412 static Instruction &GetInstByName(Function &F, StringRef Name) {
413   for (auto &I : instructions(F))
414     if (I.getName() == Name)
415       return I;
416   llvm_unreachable("Could not find instructions!");
417 }
418 
419 TEST_F(ScalarEvolutionsTest, SCEVNormalization) {
420   LLVMContext C;
421   SMDiagnostic Err;
422   std::unique_ptr<Module> M = parseAssemblyString(
423       "target datalayout = \"e-m:e-p:32:32-f64:32:64-f80:32-n8:16:32-S128\" "
424       " "
425       "@var_0 = external global i32, align 4"
426       "@var_1 = external global i32, align 4"
427       "@var_2 = external global i32, align 4"
428       " "
429       "declare i32 @unknown(i32, i32, i32)"
430       " "
431       "define void @f_1(i8* nocapture %arr, i32 %n, i32* %A, i32* %B) "
432       "    local_unnamed_addr { "
433       "entry: "
434       "  br label %loop.ph "
435       " "
436       "loop.ph: "
437       "  br label %loop "
438       " "
439       "loop: "
440       "  %iv0 = phi i32 [ %iv0.inc, %loop ], [ 0, %loop.ph ] "
441       "  %iv1 = phi i32 [ %iv1.inc, %loop ], [ -2147483648, %loop.ph ] "
442       "  %iv0.inc = add i32 %iv0, 1 "
443       "  %iv1.inc = add i32 %iv1, 3 "
444       "  br i1 undef, label %for.end.loopexit, label %loop "
445       " "
446       "for.end.loopexit: "
447       "  ret void "
448       "} "
449       " "
450       "define void @f_2(i32 %a, i32 %b, i32 %c, i32 %d) "
451       "    local_unnamed_addr { "
452       "entry: "
453       "  br label %loop_0 "
454       " "
455       "loop_0: "
456       "  br i1 undef, label %loop_0, label %loop_1 "
457       " "
458       "loop_1: "
459       "  br i1 undef, label %loop_2, label %loop_1 "
460       " "
461       " "
462       "loop_2: "
463       "  br i1 undef, label %end, label %loop_2 "
464       " "
465       "end: "
466       "  ret void "
467       "} "
468       ,
469       Err, C);
470 
471   assert(M && "Could not parse module?");
472   assert(!verifyModule(*M) && "Must have been well formed!");
473 
474   runWithSE(*M, "f_1", [&](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
475     auto &I0 = GetInstByName(F, "iv0");
476     auto &I1 = *I0.getNextNode();
477 
478     auto *S0 = cast<SCEVAddRecExpr>(SE.getSCEV(&I0));
479     PostIncLoopSet Loops;
480     Loops.insert(S0->getLoop());
481     auto *N0 = normalizeForPostIncUse(S0, Loops, SE);
482     auto *D0 = denormalizeForPostIncUse(N0, Loops, SE);
483     EXPECT_EQ(S0, D0) << *S0 << " " << *D0;
484 
485     auto *S1 = cast<SCEVAddRecExpr>(SE.getSCEV(&I1));
486     Loops.clear();
487     Loops.insert(S1->getLoop());
488     auto *N1 = normalizeForPostIncUse(S1, Loops, SE);
489     auto *D1 = denormalizeForPostIncUse(N1, Loops, SE);
490     EXPECT_EQ(S1, D1) << *S1 << " " << *D1;
491   });
492 
493   runWithSE(*M, "f_2", [&](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
494     auto *L2 = *LI.begin();
495     auto *L1 = *std::next(LI.begin());
496     auto *L0 = *std::next(LI.begin(), 2);
497 
498     auto GetAddRec = [&SE](const Loop *L, std::initializer_list<const SCEV *> Ops) {
499       SmallVector<const SCEV *, 4> OpsCopy(Ops);
500       return SE.getAddRecExpr(OpsCopy, L, SCEV::FlagAnyWrap);
501     };
502 
503     auto GetAdd = [&SE](std::initializer_list<const SCEV *> Ops) {
504       SmallVector<const SCEV *, 4> OpsCopy(Ops);
505       return SE.getAddExpr(OpsCopy, SCEV::FlagAnyWrap);
506     };
507 
508     // We first populate the AddRecs vector with a few "interesting" SCEV
509     // expressions, and then we go through the list and assert that each
510     // expression in it has an invertible normalization.
511 
512     std::vector<const SCEV *> Exprs;
513     {
514       const SCEV *V0 = SE.getSCEV(&*F.arg_begin());
515       const SCEV *V1 = SE.getSCEV(&*std::next(F.arg_begin(), 1));
516       const SCEV *V2 = SE.getSCEV(&*std::next(F.arg_begin(), 2));
517       const SCEV *V3 = SE.getSCEV(&*std::next(F.arg_begin(), 3));
518 
519       Exprs.push_back(GetAddRec(L0, {V0}));             // 0
520       Exprs.push_back(GetAddRec(L0, {V0, V1}));         // 1
521       Exprs.push_back(GetAddRec(L0, {V0, V1, V2}));     // 2
522       Exprs.push_back(GetAddRec(L0, {V0, V1, V2, V3})); // 3
523 
524       Exprs.push_back(
525           GetAddRec(L1, {Exprs[1], Exprs[2], Exprs[3], Exprs[0]})); // 4
526       Exprs.push_back(
527           GetAddRec(L1, {Exprs[1], Exprs[2], Exprs[0], Exprs[3]})); // 5
528       Exprs.push_back(
529           GetAddRec(L1, {Exprs[1], Exprs[3], Exprs[3], Exprs[1]})); // 6
530 
531       Exprs.push_back(GetAdd({Exprs[6], Exprs[3], V2})); // 7
532 
533       Exprs.push_back(
534           GetAddRec(L2, {Exprs[4], Exprs[3], Exprs[3], Exprs[5]})); // 8
535 
536       Exprs.push_back(
537           GetAddRec(L2, {Exprs[4], Exprs[6], Exprs[7], Exprs[3], V0})); // 9
538     }
539 
540     std::vector<PostIncLoopSet> LoopSets;
541     for (int i = 0; i < 8; i++) {
542       LoopSets.emplace_back();
543       if (i & 1)
544         LoopSets.back().insert(L0);
545       if (i & 2)
546         LoopSets.back().insert(L1);
547       if (i & 4)
548         LoopSets.back().insert(L2);
549     }
550 
551     for (const auto &LoopSet : LoopSets)
552       for (auto *S : Exprs) {
553         {
554           auto *N = llvm::normalizeForPostIncUse(S, LoopSet, SE);
555           auto *D = llvm::denormalizeForPostIncUse(N, LoopSet, SE);
556 
557           // Normalization and then denormalizing better give us back the same
558           // value.
559           EXPECT_EQ(S, D) << "S = " << *S << "  D = " << *D << " N = " << *N;
560         }
561         {
562           auto *D = llvm::denormalizeForPostIncUse(S, LoopSet, SE);
563           auto *N = llvm::normalizeForPostIncUse(D, LoopSet, SE);
564 
565           // Denormalization and then normalizing better give us back the same
566           // value.
567           EXPECT_EQ(S, N) << "S = " << *S << "  N = " << *N;
568         }
569       }
570   });
571 }
572 
573 // Expect the call of getZeroExtendExpr will not cost exponential time.
574 TEST_F(ScalarEvolutionsTest, SCEVZeroExtendExpr) {
575   LLVMContext C;
576   SMDiagnostic Err;
577 
578   // Generate a function like below:
579   // define void @foo() {
580   // entry:
581   //   br label %for.cond
582   //
583   // for.cond:
584   //   %0 = phi i64 [ 100, %entry ], [ %dec, %for.inc ]
585   //   %cmp = icmp sgt i64 %0, 90
586   //   br i1 %cmp, label %for.inc, label %for.cond1
587   //
588   // for.inc:
589   //   %dec = add nsw i64 %0, -1
590   //   br label %for.cond
591   //
592   // for.cond1:
593   //   %1 = phi i64 [ 100, %for.cond ], [ %dec5, %for.inc2 ]
594   //   %cmp3 = icmp sgt i64 %1, 90
595   //   br i1 %cmp3, label %for.inc2, label %for.cond4
596   //
597   // for.inc2:
598   //   %dec5 = add nsw i64 %1, -1
599   //   br label %for.cond1
600   //
601   // ......
602   //
603   // for.cond89:
604   //   %19 = phi i64 [ 100, %for.cond84 ], [ %dec94, %for.inc92 ]
605   //   %cmp93 = icmp sgt i64 %19, 90
606   //   br i1 %cmp93, label %for.inc92, label %for.end
607   //
608   // for.inc92:
609   //   %dec94 = add nsw i64 %19, -1
610   //   br label %for.cond89
611   //
612   // for.end:
613   //   %gep = getelementptr i8, i8* null, i64 %dec
614   //   %gep6 = getelementptr i8, i8* %gep, i64 %dec5
615   //   ......
616   //   %gep95 = getelementptr i8, i8* %gep91, i64 %dec94
617   //   ret void
618   // }
619   FunctionType *FTy = FunctionType::get(Type::getVoidTy(Context), {}, false);
620   Function *F = Function::Create(FTy, Function::ExternalLinkage, "foo", M);
621 
622   BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", F);
623   BasicBlock *CondBB = BasicBlock::Create(Context, "for.cond", F);
624   BasicBlock *EndBB = BasicBlock::Create(Context, "for.end", F);
625   BranchInst::Create(CondBB, EntryBB);
626   BasicBlock *PrevBB = EntryBB;
627 
628   Type *I64Ty = Type::getInt64Ty(Context);
629   Type *I8Ty = Type::getInt8Ty(Context);
630   Type *I8PtrTy = Type::getInt8PtrTy(Context);
631   Value *Accum = Constant::getNullValue(I8PtrTy);
632   int Iters = 20;
633   for (int i = 0; i < Iters; i++) {
634     BasicBlock *IncBB = BasicBlock::Create(Context, "for.inc", F, EndBB);
635     auto *PN = PHINode::Create(I64Ty, 2, "", CondBB);
636     PN->addIncoming(ConstantInt::get(Context, APInt(64, 100)), PrevBB);
637     auto *Cmp = CmpInst::Create(Instruction::ICmp, CmpInst::ICMP_SGT, PN,
638                                 ConstantInt::get(Context, APInt(64, 90)), "cmp",
639                                 CondBB);
640     BasicBlock *NextBB;
641     if (i != Iters - 1)
642       NextBB = BasicBlock::Create(Context, "for.cond", F, EndBB);
643     else
644       NextBB = EndBB;
645     BranchInst::Create(IncBB, NextBB, Cmp, CondBB);
646     auto *Dec = BinaryOperator::CreateNSWAdd(
647         PN, ConstantInt::get(Context, APInt(64, -1)), "dec", IncBB);
648     PN->addIncoming(Dec, IncBB);
649     BranchInst::Create(CondBB, IncBB);
650 
651     Accum = GetElementPtrInst::Create(I8Ty, Accum, PN, "gep", EndBB);
652 
653     PrevBB = CondBB;
654     CondBB = NextBB;
655   }
656   ReturnInst::Create(Context, nullptr, EndBB);
657   ScalarEvolution SE = buildSE(*F);
658   const SCEV *S = SE.getSCEV(Accum);
659   Type *I128Ty = Type::getInt128Ty(Context);
660   SE.getZeroExtendExpr(S, I128Ty);
661 }
662 
663 // Make sure that SCEV invalidates exit limits after invalidating the values it
664 // depends on when we forget a loop.
665 TEST_F(ScalarEvolutionsTest, SCEVExitLimitForgetLoop) {
666   /*
667    * Create the following code:
668    * func(i64 addrspace(10)* %arg)
669    * top:
670    *  br label %L.ph
671    * L.ph:
672    *  br label %L
673    * L:
674    *  %phi = phi i64 [i64 0, %L.ph], [ %add, %L2 ]
675    *  %add = add i64 %phi2, 1
676    *  %cond = icmp slt i64 %add, 1000; then becomes 2000.
677    *  br i1 %cond, label %post, label %L2
678    * post:
679    *  ret void
680    *
681    */
682 
683   // Create a module with non-integral pointers in it's datalayout
684   Module NIM("nonintegral", Context);
685   std::string DataLayout = M.getDataLayoutStr();
686   if (!DataLayout.empty())
687     DataLayout += "-";
688   DataLayout += "ni:10";
689   NIM.setDataLayout(DataLayout);
690 
691   Type *T_int64 = Type::getInt64Ty(Context);
692   Type *T_pint64 = T_int64->getPointerTo(10);
693 
694   FunctionType *FTy =
695       FunctionType::get(Type::getVoidTy(Context), {T_pint64}, false);
696   Function *F = Function::Create(FTy, Function::ExternalLinkage, "foo", NIM);
697 
698   BasicBlock *Top = BasicBlock::Create(Context, "top", F);
699   BasicBlock *LPh = BasicBlock::Create(Context, "L.ph", F);
700   BasicBlock *L = BasicBlock::Create(Context, "L", F);
701   BasicBlock *Post = BasicBlock::Create(Context, "post", F);
702 
703   IRBuilder<> Builder(Top);
704   Builder.CreateBr(LPh);
705 
706   Builder.SetInsertPoint(LPh);
707   Builder.CreateBr(L);
708 
709   Builder.SetInsertPoint(L);
710   PHINode *Phi = Builder.CreatePHI(T_int64, 2);
711   auto *Add = cast<Instruction>(
712       Builder.CreateAdd(Phi, ConstantInt::get(T_int64, 1), "add"));
713   auto *Limit = ConstantInt::get(T_int64, 1000);
714   auto *Cond = cast<Instruction>(
715       Builder.CreateICmp(ICmpInst::ICMP_SLT, Add, Limit, "cond"));
716   auto *Br = cast<Instruction>(Builder.CreateCondBr(Cond, L, Post));
717   Phi->addIncoming(ConstantInt::get(T_int64, 0), LPh);
718   Phi->addIncoming(Add, L);
719 
720   Builder.SetInsertPoint(Post);
721   Builder.CreateRetVoid();
722 
723   ScalarEvolution SE = buildSE(*F);
724   auto *Loop = LI->getLoopFor(L);
725   const SCEV *EC = SE.getBackedgeTakenCount(Loop);
726   EXPECT_FALSE(isa<SCEVCouldNotCompute>(EC));
727   EXPECT_TRUE(isa<SCEVConstant>(EC));
728   EXPECT_EQ(cast<SCEVConstant>(EC)->getAPInt().getLimitedValue(), 999u);
729 
730   // The add recurrence {5,+,1} does not correspond to any PHI in the IR, and
731   // that is relevant to this test.
732   auto *Five = SE.getConstant(APInt(/*numBits=*/64, 5));
733   auto *AR =
734       SE.getAddRecExpr(Five, SE.getOne(T_int64), Loop, SCEV::FlagAnyWrap);
735   const SCEV *ARAtLoopExit = SE.getSCEVAtScope(AR, nullptr);
736   EXPECT_FALSE(isa<SCEVCouldNotCompute>(ARAtLoopExit));
737   EXPECT_TRUE(isa<SCEVConstant>(ARAtLoopExit));
738   EXPECT_EQ(cast<SCEVConstant>(ARAtLoopExit)->getAPInt().getLimitedValue(),
739             1004u);
740 
741   SE.forgetLoop(Loop);
742   Br->eraseFromParent();
743   Cond->eraseFromParent();
744 
745   Builder.SetInsertPoint(L);
746   auto *NewCond = Builder.CreateICmp(
747       ICmpInst::ICMP_SLT, Add, ConstantInt::get(T_int64, 2000), "new.cond");
748   Builder.CreateCondBr(NewCond, L, Post);
749   const SCEV *NewEC = SE.getBackedgeTakenCount(Loop);
750   EXPECT_FALSE(isa<SCEVCouldNotCompute>(NewEC));
751   EXPECT_TRUE(isa<SCEVConstant>(NewEC));
752   EXPECT_EQ(cast<SCEVConstant>(NewEC)->getAPInt().getLimitedValue(), 1999u);
753   const SCEV *NewARAtLoopExit = SE.getSCEVAtScope(AR, nullptr);
754   EXPECT_FALSE(isa<SCEVCouldNotCompute>(NewARAtLoopExit));
755   EXPECT_TRUE(isa<SCEVConstant>(NewARAtLoopExit));
756   EXPECT_EQ(cast<SCEVConstant>(NewARAtLoopExit)->getAPInt().getLimitedValue(),
757             2004u);
758 }
759 
760 // Make sure that SCEV invalidates exit limits after invalidating the values it
761 // depends on when we forget a value.
762 TEST_F(ScalarEvolutionsTest, SCEVExitLimitForgetValue) {
763   /*
764    * Create the following code:
765    * func(i64 addrspace(10)* %arg)
766    * top:
767    *  br label %L.ph
768    * L.ph:
769    *  %load = load i64 addrspace(10)* %arg
770    *  br label %L
771    * L:
772    *  %phi = phi i64 [i64 0, %L.ph], [ %add, %L2 ]
773    *  %add = add i64 %phi2, 1
774    *  %cond = icmp slt i64 %add, %load ; then becomes 2000.
775    *  br i1 %cond, label %post, label %L2
776    * post:
777    *  ret void
778    *
779    */
780 
781   // Create a module with non-integral pointers in it's datalayout
782   Module NIM("nonintegral", Context);
783   std::string DataLayout = M.getDataLayoutStr();
784   if (!DataLayout.empty())
785     DataLayout += "-";
786   DataLayout += "ni:10";
787   NIM.setDataLayout(DataLayout);
788 
789   Type *T_int64 = Type::getInt64Ty(Context);
790   Type *T_pint64 = T_int64->getPointerTo(10);
791 
792   FunctionType *FTy =
793       FunctionType::get(Type::getVoidTy(Context), {T_pint64}, false);
794   Function *F = Function::Create(FTy, Function::ExternalLinkage, "foo", NIM);
795 
796   Argument *Arg = &*F->arg_begin();
797 
798   BasicBlock *Top = BasicBlock::Create(Context, "top", F);
799   BasicBlock *LPh = BasicBlock::Create(Context, "L.ph", F);
800   BasicBlock *L = BasicBlock::Create(Context, "L", F);
801   BasicBlock *Post = BasicBlock::Create(Context, "post", F);
802 
803   IRBuilder<> Builder(Top);
804   Builder.CreateBr(LPh);
805 
806   Builder.SetInsertPoint(LPh);
807   auto *Load = cast<Instruction>(Builder.CreateLoad(T_int64, Arg, "load"));
808   Builder.CreateBr(L);
809 
810   Builder.SetInsertPoint(L);
811   PHINode *Phi = Builder.CreatePHI(T_int64, 2);
812   auto *Add = cast<Instruction>(
813       Builder.CreateAdd(Phi, ConstantInt::get(T_int64, 1), "add"));
814   auto *Cond = cast<Instruction>(
815       Builder.CreateICmp(ICmpInst::ICMP_SLT, Add, Load, "cond"));
816   auto *Br = cast<Instruction>(Builder.CreateCondBr(Cond, L, Post));
817   Phi->addIncoming(ConstantInt::get(T_int64, 0), LPh);
818   Phi->addIncoming(Add, L);
819 
820   Builder.SetInsertPoint(Post);
821   Builder.CreateRetVoid();
822 
823   ScalarEvolution SE = buildSE(*F);
824   auto *Loop = LI->getLoopFor(L);
825   const SCEV *EC = SE.getBackedgeTakenCount(Loop);
826   EXPECT_FALSE(isa<SCEVCouldNotCompute>(EC));
827   EXPECT_FALSE(isa<SCEVConstant>(EC));
828 
829   SE.forgetValue(Load);
830   Br->eraseFromParent();
831   Cond->eraseFromParent();
832   Load->eraseFromParent();
833 
834   Builder.SetInsertPoint(L);
835   auto *NewCond = Builder.CreateICmp(
836       ICmpInst::ICMP_SLT, Add, ConstantInt::get(T_int64, 2000), "new.cond");
837   Builder.CreateCondBr(NewCond, L, Post);
838   const SCEV *NewEC = SE.getBackedgeTakenCount(Loop);
839   EXPECT_FALSE(isa<SCEVCouldNotCompute>(NewEC));
840   EXPECT_TRUE(isa<SCEVConstant>(NewEC));
841   EXPECT_EQ(cast<SCEVConstant>(NewEC)->getAPInt().getLimitedValue(), 1999u);
842 }
843 
844 TEST_F(ScalarEvolutionsTest, SCEVAddRecFromPHIwithLargeConstants) {
845   // Reference: https://reviews.llvm.org/D37265
846   // Make sure that SCEV does not blow up when constructing an AddRec
847   // with predicates for a phi with the update pattern:
848   //  (SExt/ZExt ix (Trunc iy (%SymbolicPHI) to ix) to iy) + InvariantAccum
849   // when either the initial value of the Phi or the InvariantAccum are
850   // constants that are too large to fit in an ix but are zero when truncated to
851   // ix.
852   FunctionType *FTy =
853       FunctionType::get(Type::getVoidTy(Context), std::vector<Type *>(), false);
854   Function *F =
855       Function::Create(FTy, Function::ExternalLinkage, "addrecphitest", M);
856 
857   /*
858     Create IR:
859     entry:
860      br label %loop
861     loop:
862      %0 = phi i64 [-9223372036854775808, %entry], [%3, %loop]
863      %1 = shl i64 %0, 32
864      %2 = ashr exact i64 %1, 32
865      %3 = add i64 %2, -9223372036854775808
866      br i1 undef, label %exit, label %loop
867     exit:
868      ret void
869    */
870   BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", F);
871   BasicBlock *LoopBB = BasicBlock::Create(Context, "loop", F);
872   BasicBlock *ExitBB = BasicBlock::Create(Context, "exit", F);
873 
874   // entry:
875   BranchInst::Create(LoopBB, EntryBB);
876   // loop:
877   auto *MinInt64 =
878       ConstantInt::get(Context, APInt(64, 0x8000000000000000U, true));
879   auto *Int64_32 = ConstantInt::get(Context, APInt(64, 32));
880   auto *Br = BranchInst::Create(
881       LoopBB, ExitBB, UndefValue::get(Type::getInt1Ty(Context)), LoopBB);
882   auto *Phi = PHINode::Create(Type::getInt64Ty(Context), 2, "", Br);
883   auto *Shl = BinaryOperator::CreateShl(Phi, Int64_32, "", Br);
884   auto *AShr = BinaryOperator::CreateExactAShr(Shl, Int64_32, "", Br);
885   auto *Add = BinaryOperator::CreateAdd(AShr, MinInt64, "", Br);
886   Phi->addIncoming(MinInt64, EntryBB);
887   Phi->addIncoming(Add, LoopBB);
888   // exit:
889   ReturnInst::Create(Context, nullptr, ExitBB);
890 
891   // Make sure that SCEV doesn't blow up
892   ScalarEvolution SE = buildSE(*F);
893   SCEVUnionPredicate Preds;
894   const SCEV *Expr = SE.getSCEV(Phi);
895   EXPECT_NE(nullptr, Expr);
896   EXPECT_TRUE(isa<SCEVUnknown>(Expr));
897   auto Result = SE.createAddRecFromPHIWithCasts(cast<SCEVUnknown>(Expr));
898 }
899 
900 TEST_F(ScalarEvolutionsTest, SCEVAddRecFromPHIwithLargeConstantAccum) {
901   // Make sure that SCEV does not blow up when constructing an AddRec
902   // with predicates for a phi with the update pattern:
903   //  (SExt/ZExt ix (Trunc iy (%SymbolicPHI) to ix) to iy) + InvariantAccum
904   // when the InvariantAccum is a constant that is too large to fit in an
905   // ix but are zero when truncated to ix, and the initial value of the
906   // phi is not a constant.
907   Type *Int32Ty = Type::getInt32Ty(Context);
908   SmallVector<Type *, 1> Types;
909   Types.push_back(Int32Ty);
910   FunctionType *FTy = FunctionType::get(Type::getVoidTy(Context), Types, false);
911   Function *F =
912       Function::Create(FTy, Function::ExternalLinkage, "addrecphitest", M);
913 
914   /*
915     Create IR:
916     define @addrecphitest(i32)
917     entry:
918      br label %loop
919     loop:
920      %1 = phi i32 [%0, %entry], [%4, %loop]
921      %2 = shl i32 %1, 16
922      %3 = ashr exact i32 %2, 16
923      %4 = add i32 %3, -2147483648
924      br i1 undef, label %exit, label %loop
925     exit:
926      ret void
927    */
928   BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", F);
929   BasicBlock *LoopBB = BasicBlock::Create(Context, "loop", F);
930   BasicBlock *ExitBB = BasicBlock::Create(Context, "exit", F);
931 
932   // entry:
933   BranchInst::Create(LoopBB, EntryBB);
934   // loop:
935   auto *MinInt32 = ConstantInt::get(Context, APInt(32, 0x80000000U, true));
936   auto *Int32_16 = ConstantInt::get(Context, APInt(32, 16));
937   auto *Br = BranchInst::Create(
938       LoopBB, ExitBB, UndefValue::get(Type::getInt1Ty(Context)), LoopBB);
939   auto *Phi = PHINode::Create(Int32Ty, 2, "", Br);
940   auto *Shl = BinaryOperator::CreateShl(Phi, Int32_16, "", Br);
941   auto *AShr = BinaryOperator::CreateExactAShr(Shl, Int32_16, "", Br);
942   auto *Add = BinaryOperator::CreateAdd(AShr, MinInt32, "", Br);
943   auto *Arg = &*(F->arg_begin());
944   Phi->addIncoming(Arg, EntryBB);
945   Phi->addIncoming(Add, LoopBB);
946   // exit:
947   ReturnInst::Create(Context, nullptr, ExitBB);
948 
949   // Make sure that SCEV doesn't blow up
950   ScalarEvolution SE = buildSE(*F);
951   SCEVUnionPredicate Preds;
952   const SCEV *Expr = SE.getSCEV(Phi);
953   EXPECT_NE(nullptr, Expr);
954   EXPECT_TRUE(isa<SCEVUnknown>(Expr));
955   auto Result = SE.createAddRecFromPHIWithCasts(cast<SCEVUnknown>(Expr));
956 }
957 
958 TEST_F(ScalarEvolutionsTest, SCEVFoldSumOfTruncs) {
959   // Verify that the following SCEV gets folded to a zero:
960   //  (-1 * (trunc i64 (-1 * %0) to i32)) + (-1 * (trunc i64 %0 to i32)
961   Type *ArgTy = Type::getInt64Ty(Context);
962   Type *Int32Ty = Type::getInt32Ty(Context);
963   SmallVector<Type *, 1> Types;
964   Types.push_back(ArgTy);
965   FunctionType *FTy = FunctionType::get(Type::getVoidTy(Context), Types, false);
966   Function *F = Function::Create(FTy, Function::ExternalLinkage, "f", M);
967   BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
968   ReturnInst::Create(Context, nullptr, BB);
969 
970   ScalarEvolution SE = buildSE(*F);
971 
972   auto *Arg = &*(F->arg_begin());
973   const auto *ArgSCEV = SE.getSCEV(Arg);
974 
975   // Build the SCEV
976   const auto *A0 = SE.getNegativeSCEV(ArgSCEV);
977   const auto *A1 = SE.getTruncateExpr(A0, Int32Ty);
978   const auto *A = SE.getNegativeSCEV(A1);
979 
980   const auto *B0 = SE.getTruncateExpr(ArgSCEV, Int32Ty);
981   const auto *B = SE.getNegativeSCEV(B0);
982 
983   const auto *Expr = SE.getAddExpr(A, B);
984   // Verify that the SCEV was folded to 0
985   const auto *ZeroConst = SE.getConstant(Int32Ty, 0);
986   EXPECT_EQ(Expr, ZeroConst);
987 }
988 
989 // Check logic of SCEV expression size computation.
990 TEST_F(ScalarEvolutionsTest, SCEVComputeExpressionSize) {
991   /*
992    * Create the following code:
993    * void func(i64 %a, i64 %b)
994    * entry:
995    *  %s1 = add i64 %a, 1
996    *  %s2 = udiv i64 %s1, %b
997    *  br label %exit
998    * exit:
999    *  ret
1000    */
1001 
1002   // Create a module.
1003   Module M("SCEVComputeExpressionSize", Context);
1004 
1005   Type *T_int64 = Type::getInt64Ty(Context);
1006 
1007   FunctionType *FTy =
1008       FunctionType::get(Type::getVoidTy(Context), { T_int64, T_int64 }, false);
1009   Function *F = Function::Create(FTy, Function::ExternalLinkage, "func", M);
1010   Argument *A = &*F->arg_begin();
1011   Argument *B = &*std::next(F->arg_begin());
1012   ConstantInt *C = ConstantInt::get(Context, APInt(64, 1));
1013 
1014   BasicBlock *Entry = BasicBlock::Create(Context, "entry", F);
1015   BasicBlock *Exit = BasicBlock::Create(Context, "exit", F);
1016 
1017   IRBuilder<> Builder(Entry);
1018   auto *S1 = cast<Instruction>(Builder.CreateAdd(A, C, "s1"));
1019   auto *S2 = cast<Instruction>(Builder.CreateUDiv(S1, B, "s2"));
1020   Builder.CreateBr(Exit);
1021 
1022   Builder.SetInsertPoint(Exit);
1023   Builder.CreateRetVoid();
1024 
1025   ScalarEvolution SE = buildSE(*F);
1026   // Get S2 first to move it to cache.
1027   const SCEV *AS = SE.getSCEV(A);
1028   const SCEV *BS = SE.getSCEV(B);
1029   const SCEV *CS = SE.getSCEV(C);
1030   const SCEV *S1S = SE.getSCEV(S1);
1031   const SCEV *S2S = SE.getSCEV(S2);
1032   EXPECT_EQ(AS->getExpressionSize(), 1u);
1033   EXPECT_EQ(BS->getExpressionSize(), 1u);
1034   EXPECT_EQ(CS->getExpressionSize(), 1u);
1035   EXPECT_EQ(S1S->getExpressionSize(), 3u);
1036   EXPECT_EQ(S2S->getExpressionSize(), 5u);
1037 }
1038 
1039 TEST_F(ScalarEvolutionsTest, SCEVLoopDecIntrinsic) {
1040   LLVMContext C;
1041   SMDiagnostic Err;
1042   std::unique_ptr<Module> M = parseAssemblyString(
1043       "define void @foo(i32 %N) { "
1044       "entry: "
1045       "  %cmp3 = icmp sgt i32 %N, 0 "
1046       "  br i1 %cmp3, label %for.body, label %for.cond.cleanup "
1047       "for.cond.cleanup: "
1048       "  ret void "
1049       "for.body: "
1050       "  %i.04 = phi i32 [ %inc, %for.body ], [ 100, %entry ] "
1051       "  %inc = call i32 @llvm.loop.decrement.reg.i32.i32.i32(i32 %i.04, i32 1) "
1052       "  %exitcond = icmp ne i32 %inc, 0 "
1053       "  br i1 %exitcond, label %for.cond.cleanup, label %for.body "
1054       "} "
1055       "declare i32 @llvm.loop.decrement.reg.i32.i32.i32(i32, i32) ",
1056       Err, C);
1057 
1058   ASSERT_TRUE(M && "Could not parse module?");
1059   ASSERT_TRUE(!verifyModule(*M) && "Must have been well formed!");
1060 
1061   runWithSE(*M, "foo", [&](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
1062     auto *ScevInc = SE.getSCEV(getInstructionByName(F, "inc"));
1063     EXPECT_TRUE(isa<SCEVAddRecExpr>(ScevInc));
1064   });
1065 }
1066 
1067 TEST_F(ScalarEvolutionsTest, SCEVComputeConstantDifference) {
1068   LLVMContext C;
1069   SMDiagnostic Err;
1070   std::unique_ptr<Module> M = parseAssemblyString(
1071       "define void @foo(i32 %sz, i32 %pp) { "
1072       "entry: "
1073       "  %v0 = add i32 %pp, 0 "
1074       "  %v3 = add i32 %pp, 3 "
1075       "  br label %loop.body "
1076       "loop.body: "
1077       "  %iv = phi i32 [ %iv.next, %loop.body ], [ 0, %entry ] "
1078       "  %xa = add nsw i32 %iv, %v0 "
1079       "  %yy = add nsw i32 %iv, %v3 "
1080       "  %xb = sub nsw i32 %yy, 3 "
1081       "  %iv.next = add nsw i32 %iv, 1 "
1082       "  %cmp = icmp sle i32 %iv.next, %sz "
1083       "  br i1 %cmp, label %loop.body, label %exit "
1084       "exit: "
1085       "  ret void "
1086       "} ",
1087       Err, C);
1088 
1089   ASSERT_TRUE(M && "Could not parse module?");
1090   ASSERT_TRUE(!verifyModule(*M) && "Must have been well formed!");
1091 
1092   runWithSE(*M, "foo", [](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
1093     auto *ScevV0 = SE.getSCEV(getInstructionByName(F, "v0")); // %pp
1094     auto *ScevV3 = SE.getSCEV(getInstructionByName(F, "v3")); // (3 + %pp)
1095     auto *ScevIV = SE.getSCEV(getInstructionByName(F, "iv")); // {0,+,1}
1096     auto *ScevXA = SE.getSCEV(getInstructionByName(F, "xa")); // {%pp,+,1}
1097     auto *ScevYY = SE.getSCEV(getInstructionByName(F, "yy")); // {(3 + %pp),+,1}
1098     auto *ScevXB = SE.getSCEV(getInstructionByName(F, "xb")); // {%pp,+,1}
1099     auto *ScevIVNext = SE.getSCEV(getInstructionByName(F, "iv.next")); // {1,+,1}
1100 
1101     auto diff = [&SE](const SCEV *LHS, const SCEV *RHS) -> Optional<int> {
1102       auto ConstantDiffOrNone = computeConstantDifference(SE, LHS, RHS);
1103       if (!ConstantDiffOrNone)
1104         return None;
1105 
1106       auto ExtDiff = ConstantDiffOrNone->getSExtValue();
1107       int Diff = ExtDiff;
1108       assert(Diff == ExtDiff && "Integer overflow");
1109       return Diff;
1110     };
1111 
1112     EXPECT_EQ(diff(ScevV3, ScevV0), 3);
1113     EXPECT_EQ(diff(ScevV0, ScevV3), -3);
1114     EXPECT_EQ(diff(ScevV0, ScevV0), 0);
1115     EXPECT_EQ(diff(ScevV3, ScevV3), 0);
1116     EXPECT_EQ(diff(ScevIV, ScevIV), 0);
1117     EXPECT_EQ(diff(ScevXA, ScevXB), 0);
1118     EXPECT_EQ(diff(ScevXA, ScevYY), -3);
1119     EXPECT_EQ(diff(ScevYY, ScevXB), 3);
1120     EXPECT_EQ(diff(ScevIV, ScevIVNext), -1);
1121     EXPECT_EQ(diff(ScevIVNext, ScevIV), 1);
1122     EXPECT_EQ(diff(ScevIVNext, ScevIVNext), 0);
1123     EXPECT_EQ(diff(ScevV0, ScevIV), None);
1124     EXPECT_EQ(diff(ScevIVNext, ScevV3), None);
1125     EXPECT_EQ(diff(ScevYY, ScevV3), None);
1126   });
1127 }
1128 
1129 TEST_F(ScalarEvolutionsTest, SCEVrewriteUnknowns) {
1130   LLVMContext C;
1131   SMDiagnostic Err;
1132   std::unique_ptr<Module> M = parseAssemblyString(
1133       "define void @foo(i32 %i) { "
1134       "entry: "
1135       "  %cmp3 = icmp ult i32 %i, 16 "
1136       "  br i1 %cmp3, label %loop.body, label %exit "
1137       "loop.body: "
1138       "  %iv = phi i32 [ %iv.next, %loop.body ], [ %i, %entry ] "
1139       "  %iv.next = add nsw i32 %iv, 1 "
1140       "  %cmp = icmp eq i32 %iv.next, 16 "
1141       "  br i1 %cmp, label %exit, label %loop.body "
1142       "exit: "
1143       "  ret void "
1144       "} ",
1145       Err, C);
1146 
1147   ASSERT_TRUE(M && "Could not parse module?");
1148   ASSERT_TRUE(!verifyModule(*M) && "Must have been well formed!");
1149 
1150   runWithSE(*M, "foo", [](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
1151     auto *ScevIV = SE.getSCEV(getInstructionByName(F, "iv")); // {0,+,1}
1152     auto *ScevI = SE.getSCEV(getArgByName(F, "i"));           // {0,+,1}
1153 
1154     ValueToSCEVMapTy RewriteMap;
1155     RewriteMap[cast<SCEVUnknown>(ScevI)->getValue()] =
1156         SE.getUMinExpr(ScevI, SE.getConstant(ScevI->getType(), 17));
1157     auto *WithUMin = SCEVParameterRewriter::rewrite(ScevIV, SE, RewriteMap);
1158 
1159     EXPECT_NE(WithUMin, ScevIV);
1160     auto *AR = dyn_cast<SCEVAddRecExpr>(WithUMin);
1161     EXPECT_TRUE(AR);
1162     EXPECT_EQ(AR->getStart(),
1163               SE.getUMinExpr(ScevI, SE.getConstant(ScevI->getType(), 17)));
1164     EXPECT_EQ(AR->getStepRecurrence(SE),
1165               cast<SCEVAddRecExpr>(ScevIV)->getStepRecurrence(SE));
1166   });
1167 }
1168 
1169 TEST_F(ScalarEvolutionsTest, SCEVAddNUW) {
1170   LLVMContext C;
1171   SMDiagnostic Err;
1172   std::unique_ptr<Module> M = parseAssemblyString("define void @foo(i32 %x) { "
1173                                                   "  ret void "
1174                                                   "} ",
1175                                                   Err, C);
1176 
1177   ASSERT_TRUE(M && "Could not parse module?");
1178   ASSERT_TRUE(!verifyModule(*M) && "Must have been well formed!");
1179 
1180   runWithSE(*M, "foo", [](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
1181     auto *X = SE.getSCEV(getArgByName(F, "x"));
1182     auto *One = SE.getOne(X->getType());
1183     auto *Sum = SE.getAddExpr(X, One, SCEV::FlagNUW);
1184     EXPECT_TRUE(SE.isKnownPredicate(ICmpInst::ICMP_UGE, Sum, X));
1185     EXPECT_TRUE(SE.isKnownPredicate(ICmpInst::ICMP_UGT, Sum, X));
1186   });
1187 }
1188 
1189 TEST_F(ScalarEvolutionsTest, SCEVgetRanges) {
1190   LLVMContext C;
1191   SMDiagnostic Err;
1192   std::unique_ptr<Module> M = parseAssemblyString(
1193       "define void @foo(i32 %i) { "
1194       "entry: "
1195       "  br label %loop.body "
1196       "loop.body: "
1197       "  %iv = phi i32 [ %iv.next, %loop.body ], [ 0, %entry ] "
1198       "  %iv.next = add nsw i32 %iv, 1 "
1199       "  %cmp = icmp eq i32 %iv.next, 16 "
1200       "  br i1 %cmp, label %exit, label %loop.body "
1201       "exit: "
1202       "  ret void "
1203       "} ",
1204       Err, C);
1205 
1206   runWithSE(*M, "foo", [](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
1207     auto *ScevIV = SE.getSCEV(getInstructionByName(F, "iv")); // {0,+,1}
1208     auto *ScevI = SE.getSCEV(getArgByName(F, "i"));
1209     EXPECT_EQ(SE.getUnsignedRange(ScevIV).getLower(), 0);
1210     EXPECT_EQ(SE.getUnsignedRange(ScevIV).getUpper(), 16);
1211 
1212     auto *Add = SE.getAddExpr(ScevI, ScevIV);
1213     ValueToSCEVMapTy RewriteMap;
1214     RewriteMap[cast<SCEVUnknown>(ScevI)->getValue()] =
1215         SE.getUMinExpr(ScevI, SE.getConstant(ScevI->getType(), 17));
1216     auto *AddWithUMin = SCEVParameterRewriter::rewrite(Add, SE, RewriteMap);
1217     EXPECT_EQ(SE.getUnsignedRange(AddWithUMin).getLower(), 0);
1218     EXPECT_EQ(SE.getUnsignedRange(AddWithUMin).getUpper(), 33);
1219   });
1220 }
1221 
1222 TEST_F(ScalarEvolutionsTest, SCEVgetExitLimitForGuardedLoop) {
1223   LLVMContext C;
1224   SMDiagnostic Err;
1225   std::unique_ptr<Module> M = parseAssemblyString(
1226       "define void @foo(i32 %i) { "
1227       "entry: "
1228       "  %cmp3 = icmp ult i32 %i, 16 "
1229       "  br i1 %cmp3, label %loop.body, label %exit "
1230       "loop.body: "
1231       "  %iv = phi i32 [ %iv.next, %loop.body ], [ %i, %entry ] "
1232       "  %iv.next = add nsw i32 %iv, 1 "
1233       "  %cmp = icmp eq i32 %iv.next, 16 "
1234       "  br i1 %cmp, label %exit, label %loop.body "
1235       "exit: "
1236       "  ret void "
1237       "} ",
1238       Err, C);
1239 
1240   ASSERT_TRUE(M && "Could not parse module?");
1241   ASSERT_TRUE(!verifyModule(*M) && "Must have been well formed!");
1242 
1243   runWithSE(*M, "foo", [](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
1244     auto *ScevIV = SE.getSCEV(getInstructionByName(F, "iv")); // {0,+,1}
1245     const Loop *L = cast<SCEVAddRecExpr>(ScevIV)->getLoop();
1246 
1247     const SCEV *BTC = SE.getBackedgeTakenCount(L);
1248     EXPECT_FALSE(isa<SCEVConstant>(BTC));
1249     const SCEV *MaxBTC = SE.getConstantMaxBackedgeTakenCount(L);
1250     EXPECT_EQ(cast<SCEVConstant>(MaxBTC)->getAPInt(), 15);
1251   });
1252 }
1253 
1254 }  // end namespace llvm
1255