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