1 //===- ScalarEvolutionsTest.cpp - ScalarEvolution unit tests --------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "llvm/ADT/SmallVector.h"
11 #include "llvm/Analysis/AssumptionCache.h"
12 #include "llvm/Analysis/LoopInfo.h"
13 #include "llvm/Analysis/ScalarEvolutionExpander.h"
14 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
15 #include "llvm/Analysis/TargetLibraryInfo.h"
16 #include "llvm/AsmParser/Parser.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/Dominators.h"
19 #include "llvm/IR/GlobalVariable.h"
20 #include "llvm/IR/IRBuilder.h"
21 #include "llvm/IR/InstIterator.h"
22 #include "llvm/IR/LLVMContext.h"
23 #include "llvm/IR/LegacyPassManager.h"
24 #include "llvm/IR/Module.h"
25 #include "llvm/IR/Verifier.h"
26 #include "llvm/Support/SourceMgr.h"
27 #include "gmock/gmock.h"
28 #include "gtest/gtest.h"
29 
30 namespace llvm {
31 namespace {
32 
33 MATCHER_P3(IsAffineAddRec, S, X, L, "") {
34   if (auto *AR = dyn_cast<SCEVAddRecExpr>(arg))
35     return AR->isAffine() && AR->getLoop() == L && AR->getOperand(0) == S &&
36            AR->getOperand(1) == X;
37   return false;
38 }
39 
40 // We use this fixture to ensure that we clean up ScalarEvolution before
41 // deleting the PassManager.
42 class ScalarEvolutionsTest : public testing::Test {
43 protected:
44   LLVMContext Context;
45   Module M;
46   TargetLibraryInfoImpl TLII;
47   TargetLibraryInfo TLI;
48 
49   std::unique_ptr<AssumptionCache> AC;
50   std::unique_ptr<DominatorTree> DT;
51   std::unique_ptr<LoopInfo> LI;
52 
53   ScalarEvolutionsTest() : M("", Context), TLII(), TLI(TLII) {}
54 
55   ScalarEvolution buildSE(Function &F) {
56     AC.reset(new AssumptionCache(F));
57     DT.reset(new DominatorTree(F));
58     LI.reset(new LoopInfo(*DT));
59     return ScalarEvolution(F, TLI, *AC, *DT, *LI);
60   }
61 
62   void runWithSE(
63       Module &M, StringRef FuncName,
64       function_ref<void(Function &F, LoopInfo &LI, ScalarEvolution &SE)> Test) {
65     auto *F = M.getFunction(FuncName);
66     ASSERT_NE(F, nullptr) << "Could not find " << FuncName;
67     ScalarEvolution SE = buildSE(*F);
68     Test(*F, *LI, SE);
69   }
70 };
71 
72 TEST_F(ScalarEvolutionsTest, SCEVUnknownRAUW) {
73   FunctionType *FTy = FunctionType::get(Type::getVoidTy(Context),
74                                               std::vector<Type *>(), false);
75   Function *F = cast<Function>(M.getOrInsertFunction("f", FTy));
76   BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
77   ReturnInst::Create(Context, nullptr, BB);
78 
79   Type *Ty = Type::getInt1Ty(Context);
80   Constant *Init = Constant::getNullValue(Ty);
81   Value *V0 = new GlobalVariable(M, Ty, false, GlobalValue::ExternalLinkage, Init, "V0");
82   Value *V1 = new GlobalVariable(M, Ty, false, GlobalValue::ExternalLinkage, Init, "V1");
83   Value *V2 = new GlobalVariable(M, Ty, false, GlobalValue::ExternalLinkage, Init, "V2");
84 
85   ScalarEvolution SE = buildSE(*F);
86 
87   const SCEV *S0 = SE.getSCEV(V0);
88   const SCEV *S1 = SE.getSCEV(V1);
89   const SCEV *S2 = SE.getSCEV(V2);
90 
91   const SCEV *P0 = SE.getAddExpr(S0, S0);
92   const SCEV *P1 = SE.getAddExpr(S1, S1);
93   const SCEV *P2 = SE.getAddExpr(S2, S2);
94 
95   const SCEVMulExpr *M0 = cast<SCEVMulExpr>(P0);
96   const SCEVMulExpr *M1 = cast<SCEVMulExpr>(P1);
97   const SCEVMulExpr *M2 = cast<SCEVMulExpr>(P2);
98 
99   EXPECT_EQ(cast<SCEVConstant>(M0->getOperand(0))->getValue()->getZExtValue(),
100             2u);
101   EXPECT_EQ(cast<SCEVConstant>(M1->getOperand(0))->getValue()->getZExtValue(),
102             2u);
103   EXPECT_EQ(cast<SCEVConstant>(M2->getOperand(0))->getValue()->getZExtValue(),
104             2u);
105 
106   // Before the RAUWs, these are all pointing to separate values.
107   EXPECT_EQ(cast<SCEVUnknown>(M0->getOperand(1))->getValue(), V0);
108   EXPECT_EQ(cast<SCEVUnknown>(M1->getOperand(1))->getValue(), V1);
109   EXPECT_EQ(cast<SCEVUnknown>(M2->getOperand(1))->getValue(), V2);
110 
111   // Do some RAUWs.
112   V2->replaceAllUsesWith(V1);
113   V1->replaceAllUsesWith(V0);
114 
115   // After the RAUWs, these should all be pointing to V0.
116   EXPECT_EQ(cast<SCEVUnknown>(M0->getOperand(1))->getValue(), V0);
117   EXPECT_EQ(cast<SCEVUnknown>(M1->getOperand(1))->getValue(), V0);
118   EXPECT_EQ(cast<SCEVUnknown>(M2->getOperand(1))->getValue(), V0);
119 }
120 
121 TEST_F(ScalarEvolutionsTest, SimplifiedPHI) {
122   FunctionType *FTy = FunctionType::get(Type::getVoidTy(Context),
123                                               std::vector<Type *>(), false);
124   Function *F = cast<Function>(M.getOrInsertFunction("f", FTy));
125   BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", F);
126   BasicBlock *LoopBB = BasicBlock::Create(Context, "loop", F);
127   BasicBlock *ExitBB = BasicBlock::Create(Context, "exit", F);
128   BranchInst::Create(LoopBB, EntryBB);
129   BranchInst::Create(LoopBB, ExitBB, UndefValue::get(Type::getInt1Ty(Context)),
130                      LoopBB);
131   ReturnInst::Create(Context, nullptr, ExitBB);
132   auto *Ty = Type::getInt32Ty(Context);
133   auto *PN = PHINode::Create(Ty, 2, "", &*LoopBB->begin());
134   PN->addIncoming(Constant::getNullValue(Ty), EntryBB);
135   PN->addIncoming(UndefValue::get(Ty), LoopBB);
136   ScalarEvolution SE = buildSE(*F);
137   auto *S1 = SE.getSCEV(PN);
138   auto *S2 = SE.getSCEV(PN);
139   auto *ZeroConst = SE.getConstant(Ty, 0);
140 
141   // At some point, only the first call to getSCEV returned the simplified
142   // SCEVConstant and later calls just returned a SCEVUnknown referencing the
143   // PHI node.
144   EXPECT_EQ(S1, ZeroConst);
145   EXPECT_EQ(S1, S2);
146 }
147 
148 TEST_F(ScalarEvolutionsTest, ExpandPtrTypeSCEV) {
149   // It is to test the fix for PR30213. It exercises the branch in scev
150   // expansion when the value in ValueOffsetPair is a ptr and the offset
151   // is not divisible by the elem type size of value.
152   auto *I8Ty = Type::getInt8Ty(Context);
153   auto *I8PtrTy = Type::getInt8PtrTy(Context);
154   auto *I32Ty = Type::getInt32Ty(Context);
155   auto *I32PtrTy = Type::getInt32PtrTy(Context);
156   FunctionType *FTy =
157       FunctionType::get(Type::getVoidTy(Context), std::vector<Type *>(), false);
158   Function *F = cast<Function>(M.getOrInsertFunction("f", FTy));
159   BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", F);
160   BasicBlock *LoopBB = BasicBlock::Create(Context, "loop", F);
161   BasicBlock *ExitBB = BasicBlock::Create(Context, "exit", F);
162   BranchInst::Create(LoopBB, EntryBB);
163   ReturnInst::Create(Context, nullptr, ExitBB);
164 
165   // loop:                            ; preds = %loop, %entry
166   //   %alloca = alloca i32
167   //   %gep0 = getelementptr i32, i32* %alloca, i32 1
168   //   %bitcast1 = bitcast i32* %gep0 to i8*
169   //   %gep1 = getelementptr i8, i8* %bitcast1, i32 1
170   //   %gep2 = getelementptr i8, i8* undef, i32 1
171   //   %cmp = icmp ult i8* undef, %bitcast1
172   //   %select = select i1 %cmp, i8* %gep1, i8* %gep2
173   //   %bitcast2 = bitcast i8* %select to i32*
174   //   br i1 undef, label %loop, label %exit
175 
176   const DataLayout &DL = F->getParent()->getDataLayout();
177   BranchInst *Br = BranchInst::Create(
178       LoopBB, ExitBB, UndefValue::get(Type::getInt1Ty(Context)), LoopBB);
179   AllocaInst *Alloca = new AllocaInst(I32Ty, DL.getAllocaAddrSpace(),
180                                       "alloca", Br);
181   ConstantInt *Ci32 = ConstantInt::get(Context, APInt(32, 1));
182   GetElementPtrInst *Gep0 =
183       GetElementPtrInst::Create(I32Ty, Alloca, Ci32, "gep0", Br);
184   CastInst *CastA =
185       CastInst::CreateBitOrPointerCast(Gep0, I8PtrTy, "bitcast1", Br);
186   GetElementPtrInst *Gep1 =
187       GetElementPtrInst::Create(I8Ty, CastA, Ci32, "gep1", Br);
188   GetElementPtrInst *Gep2 = GetElementPtrInst::Create(
189       I8Ty, UndefValue::get(I8PtrTy), Ci32, "gep2", Br);
190   CmpInst *Cmp = CmpInst::Create(Instruction::ICmp, CmpInst::ICMP_ULT,
191                                  UndefValue::get(I8PtrTy), CastA, "cmp", Br);
192   SelectInst *Sel = SelectInst::Create(Cmp, Gep1, Gep2, "select", Br);
193   CastInst *CastB =
194       CastInst::CreateBitOrPointerCast(Sel, I32PtrTy, "bitcast2", Br);
195 
196   ScalarEvolution SE = buildSE(*F);
197   auto *S = SE.getSCEV(CastB);
198   SCEVExpander Exp(SE, M.getDataLayout(), "expander");
199   Value *V =
200       Exp.expandCodeFor(cast<SCEVAddExpr>(S)->getOperand(1), nullptr, Br);
201 
202   // Expect the expansion code contains:
203   //   %0 = bitcast i32* %bitcast2 to i8*
204   //   %uglygep = getelementptr i8, i8* %0, i64 -1
205   //   %1 = bitcast i8* %uglygep to i32*
206   EXPECT_TRUE(isa<BitCastInst>(V));
207   Instruction *Gep = cast<Instruction>(V)->getPrevNode();
208   EXPECT_TRUE(isa<GetElementPtrInst>(Gep));
209   EXPECT_TRUE(isa<ConstantInt>(Gep->getOperand(1)));
210   EXPECT_EQ(cast<ConstantInt>(Gep->getOperand(1))->getSExtValue(), -1);
211   EXPECT_TRUE(isa<BitCastInst>(Gep->getPrevNode()));
212 }
213 
214 static Instruction *getInstructionByName(Function &F, StringRef Name) {
215   for (auto &I : instructions(F))
216     if (I.getName() == Name)
217       return &I;
218   llvm_unreachable("Expected to find instruction!");
219 }
220 
221 TEST_F(ScalarEvolutionsTest, CommutativeExprOperandOrder) {
222   LLVMContext C;
223   SMDiagnostic Err;
224   std::unique_ptr<Module> M = parseAssemblyString(
225       "target datalayout = \"e-m:e-p:32:32-f64:32:64-f80:32-n8:16:32-S128\" "
226       " "
227       "@var_0 = external global i32, align 4"
228       "@var_1 = external global i32, align 4"
229       "@var_2 = external global i32, align 4"
230       " "
231       "declare i32 @unknown(i32, i32, i32)"
232       " "
233       "define void @f_1(i8* nocapture %arr, i32 %n, i32* %A, i32* %B) "
234       "    local_unnamed_addr { "
235       "entry: "
236       "  %entrycond = icmp sgt i32 %n, 0 "
237       "  br i1 %entrycond, label %loop.ph, label %for.end "
238       " "
239       "loop.ph: "
240       "  %a = load i32, i32* %A, align 4 "
241       "  %b = load i32, i32* %B, align 4 "
242       "  %mul = mul nsw i32 %b, %a "
243       "  %iv0.init = getelementptr inbounds i8, i8* %arr, i32 %mul "
244       "  br label %loop "
245       " "
246       "loop: "
247       "  %iv0 = phi i8* [ %iv0.inc, %loop ], [ %iv0.init, %loop.ph ] "
248       "  %iv1 = phi i32 [ %iv1.inc, %loop ], [ 0, %loop.ph ] "
249       "  %conv = trunc i32 %iv1 to i8 "
250       "  store i8 %conv, i8* %iv0, align 1 "
251       "  %iv0.inc = getelementptr inbounds i8, i8* %iv0, i32 %b "
252       "  %iv1.inc = add nuw nsw i32 %iv1, 1 "
253       "  %exitcond = icmp eq i32 %iv1.inc, %n "
254       "  br i1 %exitcond, label %for.end.loopexit, label %loop "
255       " "
256       "for.end.loopexit: "
257       "  br label %for.end "
258       " "
259       "for.end: "
260       "  ret void "
261       "} "
262       " "
263       "define void @f_2(i32* %X, i32* %Y, i32* %Z) { "
264       "  %x = load i32, i32* %X "
265       "  %y = load i32, i32* %Y "
266       "  %z = load i32, i32* %Z "
267       "  ret void "
268       "} "
269       " "
270       "define void @f_3() { "
271       "  %x = load i32, i32* @var_0"
272       "  %y = load i32, i32* @var_1"
273       "  %z = load i32, i32* @var_2"
274       "  ret void"
275       "} "
276       " "
277       "define void @f_4(i32 %a, i32 %b, i32 %c) { "
278       "  %x = call i32 @unknown(i32 %a, i32 %b, i32 %c)"
279       "  %y = call i32 @unknown(i32 %b, i32 %c, i32 %a)"
280       "  %z = call i32 @unknown(i32 %c, i32 %a, i32 %b)"
281       "  ret void"
282       "} "
283       ,
284       Err, C);
285 
286   assert(M && "Could not parse module?");
287   assert(!verifyModule(*M) && "Must have been well formed!");
288 
289   runWithSE(*M, "f_1", [&](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
290     auto *IV0 = getInstructionByName(F, "iv0");
291     auto *IV0Inc = getInstructionByName(F, "iv0.inc");
292 
293     auto *FirstExprForIV0 = SE.getSCEV(IV0);
294     auto *FirstExprForIV0Inc = SE.getSCEV(IV0Inc);
295     auto *SecondExprForIV0 = SE.getSCEV(IV0);
296 
297     EXPECT_TRUE(isa<SCEVAddRecExpr>(FirstExprForIV0));
298     EXPECT_TRUE(isa<SCEVAddRecExpr>(FirstExprForIV0Inc));
299     EXPECT_TRUE(isa<SCEVAddRecExpr>(SecondExprForIV0));
300   });
301 
302   auto CheckCommutativeMulExprs = [&](ScalarEvolution &SE, const SCEV *A,
303                                       const SCEV *B, const SCEV *C) {
304     EXPECT_EQ(SE.getMulExpr(A, B), SE.getMulExpr(B, A));
305     EXPECT_EQ(SE.getMulExpr(B, C), SE.getMulExpr(C, B));
306     EXPECT_EQ(SE.getMulExpr(A, C), SE.getMulExpr(C, A));
307 
308     SmallVector<const SCEV *, 3> Ops0 = {A, B, C};
309     SmallVector<const SCEV *, 3> Ops1 = {A, C, B};
310     SmallVector<const SCEV *, 3> Ops2 = {B, A, C};
311     SmallVector<const SCEV *, 3> Ops3 = {B, C, A};
312     SmallVector<const SCEV *, 3> Ops4 = {C, B, A};
313     SmallVector<const SCEV *, 3> Ops5 = {C, A, B};
314 
315     auto *Mul0 = SE.getMulExpr(Ops0);
316     auto *Mul1 = SE.getMulExpr(Ops1);
317     auto *Mul2 = SE.getMulExpr(Ops2);
318     auto *Mul3 = SE.getMulExpr(Ops3);
319     auto *Mul4 = SE.getMulExpr(Ops4);
320     auto *Mul5 = SE.getMulExpr(Ops5);
321 
322     EXPECT_EQ(Mul0, Mul1) << "Expected " << *Mul0 << " == " << *Mul1;
323     EXPECT_EQ(Mul1, Mul2) << "Expected " << *Mul1 << " == " << *Mul2;
324     EXPECT_EQ(Mul2, Mul3) << "Expected " << *Mul2 << " == " << *Mul3;
325     EXPECT_EQ(Mul3, Mul4) << "Expected " << *Mul3 << " == " << *Mul4;
326     EXPECT_EQ(Mul4, Mul5) << "Expected " << *Mul4 << " == " << *Mul5;
327   };
328 
329   for (StringRef FuncName : {"f_2", "f_3", "f_4"})
330     runWithSE(
331         *M, FuncName, [&](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
332           CheckCommutativeMulExprs(SE, SE.getSCEV(getInstructionByName(F, "x")),
333                                    SE.getSCEV(getInstructionByName(F, "y")),
334                                    SE.getSCEV(getInstructionByName(F, "z")));
335         });
336 }
337 
338 TEST_F(ScalarEvolutionsTest, CompareSCEVComplexity) {
339   FunctionType *FTy =
340       FunctionType::get(Type::getVoidTy(Context), std::vector<Type *>(), false);
341   Function *F = cast<Function>(M.getOrInsertFunction("f", FTy));
342   BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", F);
343   BasicBlock *LoopBB = BasicBlock::Create(Context, "bb1", F);
344   BranchInst::Create(LoopBB, EntryBB);
345 
346   auto *Ty = Type::getInt32Ty(Context);
347   SmallVector<Instruction*, 8> Muls(8), Acc(8), NextAcc(8);
348 
349   Acc[0] = PHINode::Create(Ty, 2, "", LoopBB);
350   Acc[1] = PHINode::Create(Ty, 2, "", LoopBB);
351   Acc[2] = PHINode::Create(Ty, 2, "", LoopBB);
352   Acc[3] = PHINode::Create(Ty, 2, "", LoopBB);
353   Acc[4] = PHINode::Create(Ty, 2, "", LoopBB);
354   Acc[5] = PHINode::Create(Ty, 2, "", LoopBB);
355   Acc[6] = PHINode::Create(Ty, 2, "", LoopBB);
356   Acc[7] = PHINode::Create(Ty, 2, "", LoopBB);
357 
358   for (int i = 0; i < 20; i++) {
359     Muls[0] = BinaryOperator::CreateMul(Acc[0], Acc[0], "", LoopBB);
360     NextAcc[0] = BinaryOperator::CreateAdd(Muls[0], Acc[4], "", LoopBB);
361     Muls[1] = BinaryOperator::CreateMul(Acc[1], Acc[1], "", LoopBB);
362     NextAcc[1] = BinaryOperator::CreateAdd(Muls[1], Acc[5], "", LoopBB);
363     Muls[2] = BinaryOperator::CreateMul(Acc[2], Acc[2], "", LoopBB);
364     NextAcc[2] = BinaryOperator::CreateAdd(Muls[2], Acc[6], "", LoopBB);
365     Muls[3] = BinaryOperator::CreateMul(Acc[3], Acc[3], "", LoopBB);
366     NextAcc[3] = BinaryOperator::CreateAdd(Muls[3], Acc[7], "", LoopBB);
367 
368     Muls[4] = BinaryOperator::CreateMul(Acc[4], Acc[4], "", LoopBB);
369     NextAcc[4] = BinaryOperator::CreateAdd(Muls[4], Acc[0], "", LoopBB);
370     Muls[5] = BinaryOperator::CreateMul(Acc[5], Acc[5], "", LoopBB);
371     NextAcc[5] = BinaryOperator::CreateAdd(Muls[5], Acc[1], "", LoopBB);
372     Muls[6] = BinaryOperator::CreateMul(Acc[6], Acc[6], "", LoopBB);
373     NextAcc[6] = BinaryOperator::CreateAdd(Muls[6], Acc[2], "", LoopBB);
374     Muls[7] = BinaryOperator::CreateMul(Acc[7], Acc[7], "", LoopBB);
375     NextAcc[7] = BinaryOperator::CreateAdd(Muls[7], Acc[3], "", LoopBB);
376     Acc = NextAcc;
377   }
378 
379   auto II = LoopBB->begin();
380   for (int i = 0; i < 8; i++) {
381     PHINode *Phi = cast<PHINode>(&*II++);
382     Phi->addIncoming(Acc[i], LoopBB);
383     Phi->addIncoming(UndefValue::get(Ty), EntryBB);
384   }
385 
386   BasicBlock *ExitBB = BasicBlock::Create(Context, "bb2", F);
387   BranchInst::Create(LoopBB, ExitBB, UndefValue::get(Type::getInt1Ty(Context)),
388                      LoopBB);
389 
390   Acc[0] = BinaryOperator::CreateAdd(Acc[0], Acc[1], "", ExitBB);
391   Acc[1] = BinaryOperator::CreateAdd(Acc[2], Acc[3], "", ExitBB);
392   Acc[2] = BinaryOperator::CreateAdd(Acc[4], Acc[5], "", ExitBB);
393   Acc[3] = BinaryOperator::CreateAdd(Acc[6], Acc[7], "", ExitBB);
394   Acc[0] = BinaryOperator::CreateAdd(Acc[0], Acc[1], "", ExitBB);
395   Acc[1] = BinaryOperator::CreateAdd(Acc[2], Acc[3], "", ExitBB);
396   Acc[0] = BinaryOperator::CreateAdd(Acc[0], Acc[1], "", ExitBB);
397 
398   ReturnInst::Create(Context, nullptr, ExitBB);
399 
400   ScalarEvolution SE = buildSE(*F);
401 
402   EXPECT_NE(nullptr, SE.getSCEV(Acc[0]));
403 }
404 
405 TEST_F(ScalarEvolutionsTest, CompareValueComplexity) {
406   IntegerType *IntPtrTy = M.getDataLayout().getIntPtrType(Context);
407   PointerType *IntPtrPtrTy = IntPtrTy->getPointerTo();
408 
409   FunctionType *FTy =
410       FunctionType::get(Type::getVoidTy(Context), {IntPtrTy, IntPtrTy}, false);
411   Function *F = cast<Function>(M.getOrInsertFunction("f", FTy));
412   BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", F);
413 
414   Value *X = &*F->arg_begin();
415   Value *Y = &*std::next(F->arg_begin());
416 
417   const int ValueDepth = 10;
418   for (int i = 0; i < ValueDepth; i++) {
419     X = new LoadInst(new IntToPtrInst(X, IntPtrPtrTy, "", EntryBB), "",
420                      /*isVolatile*/ false, EntryBB);
421     Y = new LoadInst(new IntToPtrInst(Y, IntPtrPtrTy, "", EntryBB), "",
422                      /*isVolatile*/ false, EntryBB);
423   }
424 
425   auto *MulA = BinaryOperator::CreateMul(X, Y, "", EntryBB);
426   auto *MulB = BinaryOperator::CreateMul(Y, X, "", EntryBB);
427   ReturnInst::Create(Context, nullptr, EntryBB);
428 
429   // This test isn't checking for correctness.  Today making A and B resolve to
430   // the same SCEV would require deeper searching in CompareValueComplexity,
431   // which will slow down compilation.  However, this test can fail (with LLVM's
432   // behavior still being correct) if we ever have a smarter
433   // CompareValueComplexity that is both fast and more accurate.
434 
435   ScalarEvolution SE = buildSE(*F);
436   auto *A = SE.getSCEV(MulA);
437   auto *B = SE.getSCEV(MulB);
438   EXPECT_NE(A, B);
439 }
440 
441 TEST_F(ScalarEvolutionsTest, SCEVAddExpr) {
442   Type *Ty32 = Type::getInt32Ty(Context);
443   Type *ArgTys[] = {Type::getInt64Ty(Context), Ty32};
444 
445   FunctionType *FTy =
446       FunctionType::get(Type::getVoidTy(Context), ArgTys, false);
447   Function *F = cast<Function>(M.getOrInsertFunction("f", FTy));
448 
449   Argument *A1 = &*F->arg_begin();
450   Argument *A2 = &*(std::next(F->arg_begin()));
451   BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", F);
452 
453   Instruction *Trunc = CastInst::CreateTruncOrBitCast(A1, Ty32, "", EntryBB);
454   Instruction *Mul1 = BinaryOperator::CreateMul(Trunc, A2, "", EntryBB);
455   Instruction *Add1 = BinaryOperator::CreateAdd(Mul1, Trunc, "", EntryBB);
456   Mul1 = BinaryOperator::CreateMul(Add1, Trunc, "", EntryBB);
457   Instruction *Add2 = BinaryOperator::CreateAdd(Mul1, Add1, "", EntryBB);
458   // FIXME: The size of this is arbitrary and doesn't seem to change the
459   // result, but SCEV will do quadratic work for these so a large number here
460   // will be extremely slow. We should revisit what and how this is testing
461   // SCEV.
462   for (int i = 0; i < 10; i++) {
463     Mul1 = BinaryOperator::CreateMul(Add2, Add1, "", EntryBB);
464     Add1 = Add2;
465     Add2 = BinaryOperator::CreateAdd(Mul1, Add1, "", EntryBB);
466   }
467 
468   ReturnInst::Create(Context, nullptr, EntryBB);
469   ScalarEvolution SE = buildSE(*F);
470   EXPECT_NE(nullptr, SE.getSCEV(Mul1));
471 }
472 
473 static Instruction &GetInstByName(Function &F, StringRef Name) {
474   for (auto &I : instructions(F))
475     if (I.getName() == Name)
476       return I;
477   llvm_unreachable("Could not find instructions!");
478 }
479 
480 TEST_F(ScalarEvolutionsTest, SCEVNormalization) {
481   LLVMContext C;
482   SMDiagnostic Err;
483   std::unique_ptr<Module> M = parseAssemblyString(
484       "target datalayout = \"e-m:e-p:32:32-f64:32:64-f80:32-n8:16:32-S128\" "
485       " "
486       "@var_0 = external global i32, align 4"
487       "@var_1 = external global i32, align 4"
488       "@var_2 = external global i32, align 4"
489       " "
490       "declare i32 @unknown(i32, i32, i32)"
491       " "
492       "define void @f_1(i8* nocapture %arr, i32 %n, i32* %A, i32* %B) "
493       "    local_unnamed_addr { "
494       "entry: "
495       "  br label %loop.ph "
496       " "
497       "loop.ph: "
498       "  br label %loop "
499       " "
500       "loop: "
501       "  %iv0 = phi i32 [ %iv0.inc, %loop ], [ 0, %loop.ph ] "
502       "  %iv1 = phi i32 [ %iv1.inc, %loop ], [ -2147483648, %loop.ph ] "
503       "  %iv0.inc = add i32 %iv0, 1 "
504       "  %iv1.inc = add i32 %iv1, 3 "
505       "  br i1 undef, label %for.end.loopexit, label %loop "
506       " "
507       "for.end.loopexit: "
508       "  ret void "
509       "} "
510       " "
511       "define void @f_2(i32 %a, i32 %b, i32 %c, i32 %d) "
512       "    local_unnamed_addr { "
513       "entry: "
514       "  br label %loop_0 "
515       " "
516       "loop_0: "
517       "  br i1 undef, label %loop_0, label %loop_1 "
518       " "
519       "loop_1: "
520       "  br i1 undef, label %loop_2, label %loop_1 "
521       " "
522       " "
523       "loop_2: "
524       "  br i1 undef, label %end, label %loop_2 "
525       " "
526       "end: "
527       "  ret void "
528       "} "
529       ,
530       Err, C);
531 
532   assert(M && "Could not parse module?");
533   assert(!verifyModule(*M) && "Must have been well formed!");
534 
535   runWithSE(*M, "f_1", [&](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
536     auto &I0 = GetInstByName(F, "iv0");
537     auto &I1 = *I0.getNextNode();
538 
539     auto *S0 = cast<SCEVAddRecExpr>(SE.getSCEV(&I0));
540     PostIncLoopSet Loops;
541     Loops.insert(S0->getLoop());
542     auto *N0 = normalizeForPostIncUse(S0, Loops, SE);
543     auto *D0 = denormalizeForPostIncUse(N0, Loops, SE);
544     EXPECT_EQ(S0, D0) << *S0 << " " << *D0;
545 
546     auto *S1 = cast<SCEVAddRecExpr>(SE.getSCEV(&I1));
547     Loops.clear();
548     Loops.insert(S1->getLoop());
549     auto *N1 = normalizeForPostIncUse(S1, Loops, SE);
550     auto *D1 = denormalizeForPostIncUse(N1, Loops, SE);
551     EXPECT_EQ(S1, D1) << *S1 << " " << *D1;
552   });
553 
554   runWithSE(*M, "f_2", [&](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
555     auto *L2 = *LI.begin();
556     auto *L1 = *std::next(LI.begin());
557     auto *L0 = *std::next(LI.begin(), 2);
558 
559     auto GetAddRec = [&SE](const Loop *L, std::initializer_list<const SCEV *> Ops) {
560       SmallVector<const SCEV *, 4> OpsCopy(Ops);
561       return SE.getAddRecExpr(OpsCopy, L, SCEV::FlagAnyWrap);
562     };
563 
564     auto GetAdd = [&SE](std::initializer_list<const SCEV *> Ops) {
565       SmallVector<const SCEV *, 4> OpsCopy(Ops);
566       return SE.getAddExpr(OpsCopy, SCEV::FlagAnyWrap);
567     };
568 
569     // We first populate the AddRecs vector with a few "interesting" SCEV
570     // expressions, and then we go through the list and assert that each
571     // expression in it has an invertible normalization.
572 
573     std::vector<const SCEV *> Exprs;
574     {
575       const SCEV *V0 = SE.getSCEV(&*F.arg_begin());
576       const SCEV *V1 = SE.getSCEV(&*std::next(F.arg_begin(), 1));
577       const SCEV *V2 = SE.getSCEV(&*std::next(F.arg_begin(), 2));
578       const SCEV *V3 = SE.getSCEV(&*std::next(F.arg_begin(), 3));
579 
580       Exprs.push_back(GetAddRec(L0, {V0}));             // 0
581       Exprs.push_back(GetAddRec(L0, {V0, V1}));         // 1
582       Exprs.push_back(GetAddRec(L0, {V0, V1, V2}));     // 2
583       Exprs.push_back(GetAddRec(L0, {V0, V1, V2, V3})); // 3
584 
585       Exprs.push_back(
586           GetAddRec(L1, {Exprs[1], Exprs[2], Exprs[3], Exprs[0]})); // 4
587       Exprs.push_back(
588           GetAddRec(L1, {Exprs[1], Exprs[2], Exprs[0], Exprs[3]})); // 5
589       Exprs.push_back(
590           GetAddRec(L1, {Exprs[1], Exprs[3], Exprs[3], Exprs[1]})); // 6
591 
592       Exprs.push_back(GetAdd({Exprs[6], Exprs[3], V2})); // 7
593 
594       Exprs.push_back(
595           GetAddRec(L2, {Exprs[4], Exprs[3], Exprs[3], Exprs[5]})); // 8
596 
597       Exprs.push_back(
598           GetAddRec(L2, {Exprs[4], Exprs[6], Exprs[7], Exprs[3], V0})); // 9
599     }
600 
601     std::vector<PostIncLoopSet> LoopSets;
602     for (int i = 0; i < 8; i++) {
603       LoopSets.emplace_back();
604       if (i & 1)
605         LoopSets.back().insert(L0);
606       if (i & 2)
607         LoopSets.back().insert(L1);
608       if (i & 4)
609         LoopSets.back().insert(L2);
610     }
611 
612     for (const auto &LoopSet : LoopSets)
613       for (auto *S : Exprs) {
614         {
615           auto *N = llvm::normalizeForPostIncUse(S, LoopSet, SE);
616           auto *D = llvm::denormalizeForPostIncUse(N, LoopSet, SE);
617 
618           // Normalization and then denormalizing better give us back the same
619           // value.
620           EXPECT_EQ(S, D) << "S = " << *S << "  D = " << *D << " N = " << *N;
621         }
622         {
623           auto *D = llvm::denormalizeForPostIncUse(S, LoopSet, SE);
624           auto *N = llvm::normalizeForPostIncUse(D, LoopSet, SE);
625 
626           // Denormalization and then normalizing better give us back the same
627           // value.
628           EXPECT_EQ(S, N) << "S = " << *S << "  N = " << *N;
629         }
630       }
631   });
632 }
633 
634 // Expect the call of getZeroExtendExpr will not cost exponential time.
635 TEST_F(ScalarEvolutionsTest, SCEVZeroExtendExpr) {
636   LLVMContext C;
637   SMDiagnostic Err;
638 
639   // Generate a function like below:
640   // define void @foo() {
641   // entry:
642   //   br label %for.cond
643   //
644   // for.cond:
645   //   %0 = phi i64 [ 100, %entry ], [ %dec, %for.inc ]
646   //   %cmp = icmp sgt i64 %0, 90
647   //   br i1 %cmp, label %for.inc, label %for.cond1
648   //
649   // for.inc:
650   //   %dec = add nsw i64 %0, -1
651   //   br label %for.cond
652   //
653   // for.cond1:
654   //   %1 = phi i64 [ 100, %for.cond ], [ %dec5, %for.inc2 ]
655   //   %cmp3 = icmp sgt i64 %1, 90
656   //   br i1 %cmp3, label %for.inc2, label %for.cond4
657   //
658   // for.inc2:
659   //   %dec5 = add nsw i64 %1, -1
660   //   br label %for.cond1
661   //
662   // ......
663   //
664   // for.cond89:
665   //   %19 = phi i64 [ 100, %for.cond84 ], [ %dec94, %for.inc92 ]
666   //   %cmp93 = icmp sgt i64 %19, 90
667   //   br i1 %cmp93, label %for.inc92, label %for.end
668   //
669   // for.inc92:
670   //   %dec94 = add nsw i64 %19, -1
671   //   br label %for.cond89
672   //
673   // for.end:
674   //   %gep = getelementptr i8, i8* null, i64 %dec
675   //   %gep6 = getelementptr i8, i8* %gep, i64 %dec5
676   //   ......
677   //   %gep95 = getelementptr i8, i8* %gep91, i64 %dec94
678   //   ret void
679   // }
680   FunctionType *FTy = FunctionType::get(Type::getVoidTy(Context), {}, false);
681   Function *F = cast<Function>(M.getOrInsertFunction("foo", FTy));
682 
683   BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", F);
684   BasicBlock *CondBB = BasicBlock::Create(Context, "for.cond", F);
685   BasicBlock *EndBB = BasicBlock::Create(Context, "for.end", F);
686   BranchInst::Create(CondBB, EntryBB);
687   BasicBlock *PrevBB = EntryBB;
688 
689   Type *I64Ty = Type::getInt64Ty(Context);
690   Type *I8Ty = Type::getInt8Ty(Context);
691   Type *I8PtrTy = Type::getInt8PtrTy(Context);
692   Value *Accum = Constant::getNullValue(I8PtrTy);
693   int Iters = 20;
694   for (int i = 0; i < Iters; i++) {
695     BasicBlock *IncBB = BasicBlock::Create(Context, "for.inc", F, EndBB);
696     auto *PN = PHINode::Create(I64Ty, 2, "", CondBB);
697     PN->addIncoming(ConstantInt::get(Context, APInt(64, 100)), PrevBB);
698     auto *Cmp = CmpInst::Create(Instruction::ICmp, CmpInst::ICMP_SGT, PN,
699                                 ConstantInt::get(Context, APInt(64, 90)), "cmp",
700                                 CondBB);
701     BasicBlock *NextBB;
702     if (i != Iters - 1)
703       NextBB = BasicBlock::Create(Context, "for.cond", F, EndBB);
704     else
705       NextBB = EndBB;
706     BranchInst::Create(IncBB, NextBB, Cmp, CondBB);
707     auto *Dec = BinaryOperator::CreateNSWAdd(
708         PN, ConstantInt::get(Context, APInt(64, -1)), "dec", IncBB);
709     PN->addIncoming(Dec, IncBB);
710     BranchInst::Create(CondBB, IncBB);
711 
712     Accum = GetElementPtrInst::Create(I8Ty, Accum, Dec, "gep", EndBB);
713 
714     PrevBB = CondBB;
715     CondBB = NextBB;
716   }
717   ReturnInst::Create(Context, nullptr, EndBB);
718   ScalarEvolution SE = buildSE(*F);
719   const SCEV *S = SE.getSCEV(Accum);
720   Type *I128Ty = Type::getInt128Ty(Context);
721   SE.getZeroExtendExpr(S, I128Ty);
722 }
723 
724 // Make sure that SCEV doesn't introduce illegal ptrtoint/inttoptr instructions
725 TEST_F(ScalarEvolutionsTest, SCEVZeroExtendExprNonIntegral) {
726   /*
727    * Create the following code:
728    * func(i64 addrspace(10)* %arg)
729    * top:
730    *  br label %L.ph
731    * L.ph:
732    *  br label %L
733    * L:
734    *  %phi = phi i64 [i64 0, %L.ph], [ %add, %L2 ]
735    *  %add = add i64 %phi2, 1
736    *  br i1 undef, label %post, label %L2
737    * post:
738    *  %gepbase = getelementptr i64 addrspace(10)* %arg, i64 1
739    *  #= %gep = getelementptr i64 addrspace(10)* %gepbase, i64 %add =#
740    *  ret void
741    *
742    * We will create the appropriate SCEV expression for %gep and expand it,
743    * then check that no inttoptr/ptrtoint instructions got inserted.
744    */
745 
746   // Create a module with non-integral pointers in it's datalayout
747   Module NIM("nonintegral", Context);
748   std::string DataLayout = M.getDataLayoutStr();
749   if (!DataLayout.empty())
750     DataLayout += "-";
751   DataLayout += "ni:10";
752   NIM.setDataLayout(DataLayout);
753 
754   Type *T_int1 = Type::getInt1Ty(Context);
755   Type *T_int64 = Type::getInt64Ty(Context);
756   Type *T_pint64 = T_int64->getPointerTo(10);
757 
758   FunctionType *FTy =
759       FunctionType::get(Type::getVoidTy(Context), {T_pint64}, false);
760   Function *F = cast<Function>(NIM.getOrInsertFunction("foo", FTy));
761 
762   Argument *Arg = &*F->arg_begin();
763 
764   BasicBlock *Top = BasicBlock::Create(Context, "top", F);
765   BasicBlock *LPh = BasicBlock::Create(Context, "L.ph", F);
766   BasicBlock *L = BasicBlock::Create(Context, "L", F);
767   BasicBlock *Post = BasicBlock::Create(Context, "post", F);
768 
769   IRBuilder<> Builder(Top);
770   Builder.CreateBr(LPh);
771 
772   Builder.SetInsertPoint(LPh);
773   Builder.CreateBr(L);
774 
775   Builder.SetInsertPoint(L);
776   PHINode *Phi = Builder.CreatePHI(T_int64, 2);
777   Value *Add = Builder.CreateAdd(Phi, ConstantInt::get(T_int64, 1), "add");
778   Builder.CreateCondBr(UndefValue::get(T_int1), L, Post);
779   Phi->addIncoming(ConstantInt::get(T_int64, 0), LPh);
780   Phi->addIncoming(Add, L);
781 
782   Builder.SetInsertPoint(Post);
783   Value *GepBase = Builder.CreateGEP(Arg, ConstantInt::get(T_int64, 1));
784   Instruction *Ret = Builder.CreateRetVoid();
785 
786   ScalarEvolution SE = buildSE(*F);
787   auto *AddRec =
788       SE.getAddRecExpr(SE.getUnknown(GepBase), SE.getConstant(T_int64, 1),
789                        LI->getLoopFor(L), SCEV::FlagNUW);
790 
791   SCEVExpander Exp(SE, NIM.getDataLayout(), "expander");
792   Exp.disableCanonicalMode();
793   Exp.expandCodeFor(AddRec, T_pint64, Ret);
794 
795   // Make sure none of the instructions inserted were inttoptr/ptrtoint.
796   // The verifier will check this.
797   EXPECT_FALSE(verifyFunction(*F, &errs()));
798 }
799 
800 // Make sure that SCEV invalidates exit limits after invalidating the values it
801 // depends on when we forget a loop.
802 TEST_F(ScalarEvolutionsTest, SCEVExitLimitForgetLoop) {
803   /*
804    * Create the following code:
805    * func(i64 addrspace(10)* %arg)
806    * top:
807    *  br label %L.ph
808    * L.ph:
809    *  br label %L
810    * L:
811    *  %phi = phi i64 [i64 0, %L.ph], [ %add, %L2 ]
812    *  %add = add i64 %phi2, 1
813    *  %cond = icmp slt i64 %add, 1000; then becomes 2000.
814    *  br i1 %cond, label %post, label %L2
815    * post:
816    *  ret void
817    *
818    */
819 
820   // Create a module with non-integral pointers in it's datalayout
821   Module NIM("nonintegral", Context);
822   std::string DataLayout = M.getDataLayoutStr();
823   if (!DataLayout.empty())
824     DataLayout += "-";
825   DataLayout += "ni:10";
826   NIM.setDataLayout(DataLayout);
827 
828   Type *T_int64 = Type::getInt64Ty(Context);
829   Type *T_pint64 = T_int64->getPointerTo(10);
830 
831   FunctionType *FTy =
832       FunctionType::get(Type::getVoidTy(Context), {T_pint64}, false);
833   Function *F = cast<Function>(NIM.getOrInsertFunction("foo", FTy));
834 
835   BasicBlock *Top = BasicBlock::Create(Context, "top", F);
836   BasicBlock *LPh = BasicBlock::Create(Context, "L.ph", F);
837   BasicBlock *L = BasicBlock::Create(Context, "L", F);
838   BasicBlock *Post = BasicBlock::Create(Context, "post", F);
839 
840   IRBuilder<> Builder(Top);
841   Builder.CreateBr(LPh);
842 
843   Builder.SetInsertPoint(LPh);
844   Builder.CreateBr(L);
845 
846   Builder.SetInsertPoint(L);
847   PHINode *Phi = Builder.CreatePHI(T_int64, 2);
848   auto *Add = cast<Instruction>(
849       Builder.CreateAdd(Phi, ConstantInt::get(T_int64, 1), "add"));
850   auto *Limit = ConstantInt::get(T_int64, 1000);
851   auto *Cond = cast<Instruction>(
852       Builder.CreateICmp(ICmpInst::ICMP_SLT, Add, Limit, "cond"));
853   auto *Br = cast<Instruction>(Builder.CreateCondBr(Cond, L, Post));
854   Phi->addIncoming(ConstantInt::get(T_int64, 0), LPh);
855   Phi->addIncoming(Add, L);
856 
857   Builder.SetInsertPoint(Post);
858   Builder.CreateRetVoid();
859 
860   ScalarEvolution SE = buildSE(*F);
861   auto *Loop = LI->getLoopFor(L);
862   const SCEV *EC = SE.getBackedgeTakenCount(Loop);
863   EXPECT_FALSE(isa<SCEVCouldNotCompute>(EC));
864   EXPECT_TRUE(isa<SCEVConstant>(EC));
865   EXPECT_EQ(cast<SCEVConstant>(EC)->getAPInt().getLimitedValue(), 999u);
866 
867   // The add recurrence {5,+,1} does not correspond to any PHI in the IR, and
868   // that is relevant to this test.
869   auto *Five = SE.getConstant(APInt(/*numBits=*/64, 5));
870   auto *AR =
871       SE.getAddRecExpr(Five, SE.getOne(T_int64), Loop, SCEV::FlagAnyWrap);
872   const SCEV *ARAtLoopExit = SE.getSCEVAtScope(AR, nullptr);
873   EXPECT_FALSE(isa<SCEVCouldNotCompute>(ARAtLoopExit));
874   EXPECT_TRUE(isa<SCEVConstant>(ARAtLoopExit));
875   EXPECT_EQ(cast<SCEVConstant>(ARAtLoopExit)->getAPInt().getLimitedValue(),
876             1004u);
877 
878   SE.forgetLoop(Loop);
879   Br->eraseFromParent();
880   Cond->eraseFromParent();
881 
882   Builder.SetInsertPoint(L);
883   auto *NewCond = Builder.CreateICmp(
884       ICmpInst::ICMP_SLT, Add, ConstantInt::get(T_int64, 2000), "new.cond");
885   Builder.CreateCondBr(NewCond, L, Post);
886   const SCEV *NewEC = SE.getBackedgeTakenCount(Loop);
887   EXPECT_FALSE(isa<SCEVCouldNotCompute>(NewEC));
888   EXPECT_TRUE(isa<SCEVConstant>(NewEC));
889   EXPECT_EQ(cast<SCEVConstant>(NewEC)->getAPInt().getLimitedValue(), 1999u);
890   const SCEV *NewARAtLoopExit = SE.getSCEVAtScope(AR, nullptr);
891   EXPECT_FALSE(isa<SCEVCouldNotCompute>(NewARAtLoopExit));
892   EXPECT_TRUE(isa<SCEVConstant>(NewARAtLoopExit));
893   EXPECT_EQ(cast<SCEVConstant>(NewARAtLoopExit)->getAPInt().getLimitedValue(),
894             2004u);
895 }
896 
897 TEST_F(ScalarEvolutionsTest, SCEVAddRecFromPHIwithLargeConstants) {
898   // Reference: https://reviews.llvm.org/D37265
899   // Make sure that SCEV does not blow up when constructing an AddRec
900   // with predicates for a phi with the update pattern:
901   //  (SExt/ZExt ix (Trunc iy (%SymbolicPHI) to ix) to iy) + InvariantAccum
902   // when either the initial value of the Phi or the InvariantAccum are
903   // constants that are too large to fit in an ix but are zero when truncated to
904   // ix.
905   FunctionType *FTy =
906       FunctionType::get(Type::getVoidTy(Context), std::vector<Type *>(), false);
907   Function *F = cast<Function>(M.getOrInsertFunction("addrecphitest", FTy));
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 = cast<Function>(M.getOrInsertFunction("addrecphitest", FTy));
964 
965   /*
966     Create IR:
967     define @addrecphitest(i32)
968     entry:
969      br label %loop
970     loop:
971      %1 = phi i32 [%0, %entry], [%4, %loop]
972      %2 = shl i32 %1, 16
973      %3 = ashr exact i32 %2, 16
974      %4 = add i32 %3, -2147483648
975      br i1 undef, label %exit, label %loop
976     exit:
977      ret void
978    */
979   BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", F);
980   BasicBlock *LoopBB = BasicBlock::Create(Context, "loop", F);
981   BasicBlock *ExitBB = BasicBlock::Create(Context, "exit", F);
982 
983   // entry:
984   BranchInst::Create(LoopBB, EntryBB);
985   // loop:
986   auto *MinInt32 = ConstantInt::get(Context, APInt(32, 0x80000000U, true));
987   auto *Int32_16 = ConstantInt::get(Context, APInt(32, 16));
988   auto *Br = BranchInst::Create(
989       LoopBB, ExitBB, UndefValue::get(Type::getInt1Ty(Context)), LoopBB);
990   auto *Phi = PHINode::Create(Int32Ty, 2, "", Br);
991   auto *Shl = BinaryOperator::CreateShl(Phi, Int32_16, "", Br);
992   auto *AShr = BinaryOperator::CreateExactAShr(Shl, Int32_16, "", Br);
993   auto *Add = BinaryOperator::CreateAdd(AShr, MinInt32, "", Br);
994   auto *Arg = &*(F->arg_begin());
995   Phi->addIncoming(Arg, EntryBB);
996   Phi->addIncoming(Add, LoopBB);
997   // exit:
998   ReturnInst::Create(Context, nullptr, ExitBB);
999 
1000   // Make sure that SCEV doesn't blow up
1001   ScalarEvolution SE = buildSE(*F);
1002   SCEVUnionPredicate Preds;
1003   const SCEV *Expr = SE.getSCEV(Phi);
1004   EXPECT_NE(nullptr, Expr);
1005   EXPECT_TRUE(isa<SCEVUnknown>(Expr));
1006   auto Result = SE.createAddRecFromPHIWithCasts(cast<SCEVUnknown>(Expr));
1007 }
1008 
1009 TEST_F(ScalarEvolutionsTest, SCEVForgetDependentLoop) {
1010   LLVMContext C;
1011   SMDiagnostic Err;
1012   std::unique_ptr<Module> M = parseAssemblyString(
1013       "target datalayout = \"e-m:e-p:32:32-f64:32:64-f80:32-n8:16:32-S128\" "
1014       " "
1015       "define void @f(i32 %first_limit, i1* %cond) { "
1016       "entry: "
1017       " br label %first_loop.ph "
1018       " "
1019       "first_loop.ph: "
1020       "  br label %first_loop "
1021       " "
1022       "first_loop: "
1023       "  %iv_first = phi i32 [0, %first_loop.ph], [%iv_first.inc, %first_loop] "
1024       "  %iv_first.inc = add i32 %iv_first, 1 "
1025       "  %known_cond = icmp slt i32 %iv_first, 2000 "
1026       "  %unknown_cond = load volatile i1, i1* %cond "
1027       "  br i1 %unknown_cond, label %first_loop, label %first_loop.exit "
1028       " "
1029       "first_loop.exit: "
1030       "  %iv_first.3x = mul i32 %iv_first, 3 "
1031       "  %iv_first.5x = mul i32 %iv_first, 5 "
1032       "  br label %second_loop.ph "
1033       " "
1034       "second_loop.ph: "
1035       "  br label %second_loop "
1036       " "
1037       "second_loop: "
1038       "  %iv_second = phi i32 [%iv_first.3x, %second_loop.ph], [%iv_second.inc, %second_loop] "
1039       "  %iv_second.inc = add i32 %iv_second, 1 "
1040       "  %second_loop.cond = icmp ne i32 %iv_second, %iv_first.5x "
1041       "  br i1 %second_loop.cond, label %second_loop, label %second_loop.exit "
1042       " "
1043       "second_loop.exit: "
1044       "  ret void "
1045       "} "
1046       " ",
1047       Err, C);
1048 
1049   assert(M && "Could not parse module?");
1050   assert(!verifyModule(*M) && "Must have been well formed!");
1051 
1052   runWithSE(*M, "f", [&](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
1053     auto &FirstIV = GetInstByName(F, "iv_first");
1054     auto &SecondIV = GetInstByName(F, "iv_second");
1055 
1056     auto *FirstLoop = LI.getLoopFor(FirstIV.getParent());
1057     auto *SecondLoop = LI.getLoopFor(SecondIV.getParent());
1058 
1059     auto *Zero = SE.getZero(FirstIV.getType());
1060     auto *Two = SE.getConstant(APInt(32, 2));
1061 
1062     EXPECT_EQ(SE.getBackedgeTakenCount(FirstLoop), SE.getCouldNotCompute());
1063     EXPECT_THAT(SE.getBackedgeTakenCount(SecondLoop),
1064                 IsAffineAddRec(Zero, Two, FirstLoop));
1065 
1066     auto &UnknownCond = GetInstByName(F, "unknown_cond");
1067     auto &KnownCond = GetInstByName(F, "known_cond");
1068 
1069     UnknownCond.replaceAllUsesWith(&KnownCond);
1070 
1071     SE.forgetLoop(FirstLoop);
1072 
1073     EXPECT_EQ(SE.getBackedgeTakenCount(FirstLoop), SE.getConstant(APInt(32, 2000)));
1074     EXPECT_EQ(SE.getBackedgeTakenCount(SecondLoop), SE.getConstant(APInt(32, 4000)));
1075   });
1076 }
1077 
1078 TEST_F(ScalarEvolutionsTest, SCEVFoldSumOfTruncs) {
1079   // Verify that the following SCEV gets folded to a zero:
1080   //  (-1 * (trunc i64 (-1 * %0) to i32)) + (-1 * (trunc i64 %0 to i32)
1081   Type *ArgTy = Type::getInt64Ty(Context);
1082   Type *Int32Ty = Type::getInt32Ty(Context);
1083   SmallVector<Type *, 1> Types;
1084   Types.push_back(ArgTy);
1085   FunctionType *FTy = FunctionType::get(Type::getVoidTy(Context), Types, false);
1086   Function *F = cast<Function>(M.getOrInsertFunction("f", FTy));
1087   BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
1088   ReturnInst::Create(Context, nullptr, BB);
1089 
1090   ScalarEvolution SE = buildSE(*F);
1091 
1092   auto *Arg = &*(F->arg_begin());
1093   const auto *ArgSCEV = SE.getSCEV(Arg);
1094 
1095   // Build the SCEV
1096   const auto *A0 = SE.getNegativeSCEV(ArgSCEV);
1097   const auto *A1 = SE.getTruncateExpr(A0, Int32Ty);
1098   const auto *A = SE.getNegativeSCEV(A1);
1099 
1100   const auto *B0 = SE.getTruncateExpr(ArgSCEV, Int32Ty);
1101   const auto *B = SE.getNegativeSCEV(B0);
1102 
1103   const auto *Expr = SE.getAddExpr(A, B);
1104   // Verify that the SCEV was folded to 0
1105   const auto *ZeroConst = SE.getConstant(Int32Ty, 0);
1106   EXPECT_EQ(Expr, ZeroConst);
1107 }
1108 
1109 }  // end anonymous namespace
1110 }  // end namespace llvm
1111