1 //===- ScalarEvolutionsTest.cpp - ScalarEvolution unit tests --------------===//
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
7 //===----------------------------------------------------------------------===//
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/Module.h"
23 #include "llvm/IR/Verifier.h"
24 #include "llvm/Support/SourceMgr.h"
25 #include "gtest/gtest.h"
29 // We use this fixture to ensure that we clean up ScalarEvolution before
30 // deleting the PassManager.
31 class ScalarEvolutionsTest
: public testing::Test
{
35 TargetLibraryInfoImpl TLII
;
36 TargetLibraryInfo TLI
;
38 std::unique_ptr
<AssumptionCache
> AC
;
39 std::unique_ptr
<DominatorTree
> DT
;
40 std::unique_ptr
<LoopInfo
> LI
;
42 ScalarEvolutionsTest() : M("", Context
), TLII(), TLI(TLII
) {}
44 ScalarEvolution
buildSE(Function
&F
) {
45 AC
.reset(new AssumptionCache(F
));
46 DT
.reset(new DominatorTree(F
));
47 LI
.reset(new LoopInfo(*DT
));
48 return ScalarEvolution(F
, TLI
, *AC
, *DT
, *LI
);
52 Module
&M
, StringRef FuncName
,
53 function_ref
<void(Function
&F
, LoopInfo
&LI
, ScalarEvolution
&SE
)> Test
) {
54 auto *F
= M
.getFunction(FuncName
);
55 ASSERT_NE(F
, nullptr) << "Could not find " << FuncName
;
56 ScalarEvolution SE
= buildSE(*F
);
60 static std::optional
<APInt
> computeConstantDifference(ScalarEvolution
&SE
,
63 return SE
.computeConstantDifference(LHS
, RHS
);
66 static bool matchURem(ScalarEvolution
&SE
, const SCEV
*Expr
, const SCEV
*&LHS
,
68 return SE
.matchURem(Expr
, LHS
, RHS
);
71 static bool isImpliedCond(
72 ScalarEvolution
&SE
, ICmpInst::Predicate Pred
, const SCEV
*LHS
,
73 const SCEV
*RHS
, ICmpInst::Predicate FoundPred
, const SCEV
*FoundLHS
,
74 const SCEV
*FoundRHS
) {
75 return SE
.isImpliedCond(Pred
, LHS
, RHS
, FoundPred
, FoundLHS
, FoundRHS
);
79 TEST_F(ScalarEvolutionsTest
, SCEVUnknownRAUW
) {
80 FunctionType
*FTy
= FunctionType::get(Type::getVoidTy(Context
),
81 std::vector
<Type
*>(), false);
82 Function
*F
= Function::Create(FTy
, Function::ExternalLinkage
, "f", M
);
83 BasicBlock
*BB
= BasicBlock::Create(Context
, "entry", F
);
84 ReturnInst::Create(Context
, nullptr, BB
);
86 Type
*Ty
= Type::getInt1Ty(Context
);
87 Constant
*Init
= Constant::getNullValue(Ty
);
88 Value
*V0
= new GlobalVariable(M
, Ty
, false, GlobalValue::ExternalLinkage
, Init
, "V0");
89 Value
*V1
= new GlobalVariable(M
, Ty
, false, GlobalValue::ExternalLinkage
, Init
, "V1");
90 Value
*V2
= new GlobalVariable(M
, Ty
, false, GlobalValue::ExternalLinkage
, Init
, "V2");
92 ScalarEvolution SE
= buildSE(*F
);
94 const SCEV
*S0
= SE
.getSCEV(V0
);
95 const SCEV
*S1
= SE
.getSCEV(V1
);
96 const SCEV
*S2
= SE
.getSCEV(V2
);
98 const SCEV
*P0
= SE
.getAddExpr(S0
, SE
.getConstant(S0
->getType(), 2));
99 const SCEV
*P1
= SE
.getAddExpr(S1
, SE
.getConstant(S0
->getType(), 2));
100 const SCEV
*P2
= SE
.getAddExpr(S2
, SE
.getConstant(S0
->getType(), 2));
102 auto *M0
= cast
<SCEVAddExpr
>(P0
);
103 auto *M1
= cast
<SCEVAddExpr
>(P1
);
104 auto *M2
= cast
<SCEVAddExpr
>(P2
);
106 EXPECT_EQ(cast
<SCEVConstant
>(M0
->getOperand(0))->getValue()->getZExtValue(),
108 EXPECT_EQ(cast
<SCEVConstant
>(M1
->getOperand(0))->getValue()->getZExtValue(),
110 EXPECT_EQ(cast
<SCEVConstant
>(M2
->getOperand(0))->getValue()->getZExtValue(),
113 // Before the RAUWs, these are all pointing to separate values.
114 EXPECT_EQ(cast
<SCEVUnknown
>(M0
->getOperand(1))->getValue(), V0
);
115 EXPECT_EQ(cast
<SCEVUnknown
>(M1
->getOperand(1))->getValue(), V1
);
116 EXPECT_EQ(cast
<SCEVUnknown
>(M2
->getOperand(1))->getValue(), V2
);
119 V2
->replaceAllUsesWith(V1
);
120 V1
->replaceAllUsesWith(V0
);
122 // After the RAUWs, these should all be pointing to V0.
123 EXPECT_EQ(cast
<SCEVUnknown
>(M0
->getOperand(1))->getValue(), V0
);
124 EXPECT_EQ(cast
<SCEVUnknown
>(M1
->getOperand(1))->getValue(), V0
);
125 EXPECT_EQ(cast
<SCEVUnknown
>(M2
->getOperand(1))->getValue(), V0
);
128 TEST_F(ScalarEvolutionsTest
, SimplifiedPHI
) {
129 FunctionType
*FTy
= FunctionType::get(Type::getVoidTy(Context
),
130 std::vector
<Type
*>(), false);
131 Function
*F
= Function::Create(FTy
, Function::ExternalLinkage
, "f", M
);
132 BasicBlock
*EntryBB
= BasicBlock::Create(Context
, "entry", F
);
133 BasicBlock
*LoopBB
= BasicBlock::Create(Context
, "loop", F
);
134 BasicBlock
*ExitBB
= BasicBlock::Create(Context
, "exit", F
);
135 BranchInst::Create(LoopBB
, EntryBB
);
136 BranchInst::Create(LoopBB
, ExitBB
, UndefValue::get(Type::getInt1Ty(Context
)),
138 ReturnInst::Create(Context
, nullptr, ExitBB
);
139 auto *Ty
= Type::getInt32Ty(Context
);
140 auto *PN
= PHINode::Create(Ty
, 2, "", &*LoopBB
->begin());
141 PN
->addIncoming(Constant::getNullValue(Ty
), EntryBB
);
142 PN
->addIncoming(UndefValue::get(Ty
), LoopBB
);
143 ScalarEvolution SE
= buildSE(*F
);
144 auto *S1
= SE
.getSCEV(PN
);
145 auto *S2
= SE
.getSCEV(PN
);
146 auto *ZeroConst
= SE
.getConstant(Ty
, 0);
148 // At some point, only the first call to getSCEV returned the simplified
149 // SCEVConstant and later calls just returned a SCEVUnknown referencing the
151 EXPECT_EQ(S1
, ZeroConst
);
156 static Instruction
*getInstructionByName(Function
&F
, StringRef Name
) {
157 for (auto &I
: instructions(F
))
158 if (I
.getName() == Name
)
160 llvm_unreachable("Expected to find instruction!");
163 static Value
*getArgByName(Function
&F
, StringRef Name
) {
164 for (auto &Arg
: F
.args())
165 if (Arg
.getName() == Name
)
167 llvm_unreachable("Expected to find instruction!");
169 TEST_F(ScalarEvolutionsTest
, CommutativeExprOperandOrder
) {
172 std::unique_ptr
<Module
> M
= parseAssemblyString(
173 "target datalayout = \"e-m:e-p:32:32-f64:32:64-f80:32-n8:16:32-S128\" "
175 "@var_0 = external global i32, align 4"
176 "@var_1 = external global i32, align 4"
177 "@var_2 = external global i32, align 4"
179 "declare i32 @unknown(i32, i32, i32)"
181 "define void @f_1(i8* nocapture %arr, i32 %n, i32* %A, i32* %B) "
182 " local_unnamed_addr { "
184 " %entrycond = icmp sgt i32 %n, 0 "
185 " br i1 %entrycond, label %loop.ph, label %for.end "
188 " %a = load i32, i32* %A, align 4 "
189 " %b = load i32, i32* %B, align 4 "
190 " %mul = mul nsw i32 %b, %a "
191 " %iv0.init = getelementptr inbounds i8, i8* %arr, i32 %mul "
195 " %iv0 = phi i8* [ %iv0.inc, %loop ], [ %iv0.init, %loop.ph ] "
196 " %iv1 = phi i32 [ %iv1.inc, %loop ], [ 0, %loop.ph ] "
197 " %conv = trunc i32 %iv1 to i8 "
198 " store i8 %conv, i8* %iv0, align 1 "
199 " %iv0.inc = getelementptr inbounds i8, i8* %iv0, i32 %b "
200 " %iv1.inc = add nuw nsw i32 %iv1, 1 "
201 " %exitcond = icmp eq i32 %iv1.inc, %n "
202 " br i1 %exitcond, label %for.end.loopexit, label %loop "
205 " br label %for.end "
211 "define void @f_2(i32* %X, i32* %Y, i32* %Z) { "
212 " %x = load i32, i32* %X "
213 " %y = load i32, i32* %Y "
214 " %z = load i32, i32* %Z "
218 "define void @f_3() { "
219 " %x = load i32, i32* @var_0"
220 " %y = load i32, i32* @var_1"
221 " %z = load i32, i32* @var_2"
225 "define void @f_4(i32 %a, i32 %b, i32 %c) { "
226 " %x = call i32 @unknown(i32 %a, i32 %b, i32 %c)"
227 " %y = call i32 @unknown(i32 %b, i32 %c, i32 %a)"
228 " %z = call i32 @unknown(i32 %c, i32 %a, i32 %b)"
234 assert(M
&& "Could not parse module?");
235 assert(!verifyModule(*M
) && "Must have been well formed!");
237 runWithSE(*M
, "f_1", [&](Function
&F
, LoopInfo
&LI
, ScalarEvolution
&SE
) {
238 auto *IV0
= getInstructionByName(F
, "iv0");
239 auto *IV0Inc
= getInstructionByName(F
, "iv0.inc");
241 auto *FirstExprForIV0
= SE
.getSCEV(IV0
);
242 auto *FirstExprForIV0Inc
= SE
.getSCEV(IV0Inc
);
243 auto *SecondExprForIV0
= SE
.getSCEV(IV0
);
245 EXPECT_TRUE(isa
<SCEVAddRecExpr
>(FirstExprForIV0
));
246 EXPECT_TRUE(isa
<SCEVAddRecExpr
>(FirstExprForIV0Inc
));
247 EXPECT_TRUE(isa
<SCEVAddRecExpr
>(SecondExprForIV0
));
250 auto CheckCommutativeMulExprs
= [&](ScalarEvolution
&SE
, const SCEV
*A
,
251 const SCEV
*B
, const SCEV
*C
) {
252 EXPECT_EQ(SE
.getMulExpr(A
, B
), SE
.getMulExpr(B
, A
));
253 EXPECT_EQ(SE
.getMulExpr(B
, C
), SE
.getMulExpr(C
, B
));
254 EXPECT_EQ(SE
.getMulExpr(A
, C
), SE
.getMulExpr(C
, A
));
256 SmallVector
<const SCEV
*, 3> Ops0
= {A
, B
, C
};
257 SmallVector
<const SCEV
*, 3> Ops1
= {A
, C
, B
};
258 SmallVector
<const SCEV
*, 3> Ops2
= {B
, A
, C
};
259 SmallVector
<const SCEV
*, 3> Ops3
= {B
, C
, A
};
260 SmallVector
<const SCEV
*, 3> Ops4
= {C
, B
, A
};
261 SmallVector
<const SCEV
*, 3> Ops5
= {C
, A
, B
};
263 auto *Mul0
= SE
.getMulExpr(Ops0
);
264 auto *Mul1
= SE
.getMulExpr(Ops1
);
265 auto *Mul2
= SE
.getMulExpr(Ops2
);
266 auto *Mul3
= SE
.getMulExpr(Ops3
);
267 auto *Mul4
= SE
.getMulExpr(Ops4
);
268 auto *Mul5
= SE
.getMulExpr(Ops5
);
270 EXPECT_EQ(Mul0
, Mul1
) << "Expected " << *Mul0
<< " == " << *Mul1
;
271 EXPECT_EQ(Mul1
, Mul2
) << "Expected " << *Mul1
<< " == " << *Mul2
;
272 EXPECT_EQ(Mul2
, Mul3
) << "Expected " << *Mul2
<< " == " << *Mul3
;
273 EXPECT_EQ(Mul3
, Mul4
) << "Expected " << *Mul3
<< " == " << *Mul4
;
274 EXPECT_EQ(Mul4
, Mul5
) << "Expected " << *Mul4
<< " == " << *Mul5
;
277 for (StringRef FuncName
: {"f_2", "f_3", "f_4"})
279 *M
, FuncName
, [&](Function
&F
, LoopInfo
&LI
, ScalarEvolution
&SE
) {
280 CheckCommutativeMulExprs(SE
, SE
.getSCEV(getInstructionByName(F
, "x")),
281 SE
.getSCEV(getInstructionByName(F
, "y")),
282 SE
.getSCEV(getInstructionByName(F
, "z")));
286 TEST_F(ScalarEvolutionsTest
, CompareSCEVComplexity
) {
288 FunctionType::get(Type::getVoidTy(Context
), std::vector
<Type
*>(), false);
289 Function
*F
= Function::Create(FTy
, Function::ExternalLinkage
, "f", M
);
290 BasicBlock
*EntryBB
= BasicBlock::Create(Context
, "entry", F
);
291 BasicBlock
*LoopBB
= BasicBlock::Create(Context
, "bb1", F
);
292 BranchInst::Create(LoopBB
, EntryBB
);
294 auto *Ty
= Type::getInt32Ty(Context
);
295 SmallVector
<Instruction
*, 8> Muls(8), Acc(8), NextAcc(8);
297 Acc
[0] = PHINode::Create(Ty
, 2, "", LoopBB
);
298 Acc
[1] = PHINode::Create(Ty
, 2, "", LoopBB
);
299 Acc
[2] = PHINode::Create(Ty
, 2, "", LoopBB
);
300 Acc
[3] = PHINode::Create(Ty
, 2, "", LoopBB
);
301 Acc
[4] = PHINode::Create(Ty
, 2, "", LoopBB
);
302 Acc
[5] = PHINode::Create(Ty
, 2, "", LoopBB
);
303 Acc
[6] = PHINode::Create(Ty
, 2, "", LoopBB
);
304 Acc
[7] = PHINode::Create(Ty
, 2, "", LoopBB
);
306 for (int i
= 0; i
< 20; i
++) {
307 Muls
[0] = BinaryOperator::CreateMul(Acc
[0], Acc
[0], "", LoopBB
);
308 NextAcc
[0] = BinaryOperator::CreateAdd(Muls
[0], Acc
[4], "", LoopBB
);
309 Muls
[1] = BinaryOperator::CreateMul(Acc
[1], Acc
[1], "", LoopBB
);
310 NextAcc
[1] = BinaryOperator::CreateAdd(Muls
[1], Acc
[5], "", LoopBB
);
311 Muls
[2] = BinaryOperator::CreateMul(Acc
[2], Acc
[2], "", LoopBB
);
312 NextAcc
[2] = BinaryOperator::CreateAdd(Muls
[2], Acc
[6], "", LoopBB
);
313 Muls
[3] = BinaryOperator::CreateMul(Acc
[3], Acc
[3], "", LoopBB
);
314 NextAcc
[3] = BinaryOperator::CreateAdd(Muls
[3], Acc
[7], "", LoopBB
);
316 Muls
[4] = BinaryOperator::CreateMul(Acc
[4], Acc
[4], "", LoopBB
);
317 NextAcc
[4] = BinaryOperator::CreateAdd(Muls
[4], Acc
[0], "", LoopBB
);
318 Muls
[5] = BinaryOperator::CreateMul(Acc
[5], Acc
[5], "", LoopBB
);
319 NextAcc
[5] = BinaryOperator::CreateAdd(Muls
[5], Acc
[1], "", LoopBB
);
320 Muls
[6] = BinaryOperator::CreateMul(Acc
[6], Acc
[6], "", LoopBB
);
321 NextAcc
[6] = BinaryOperator::CreateAdd(Muls
[6], Acc
[2], "", LoopBB
);
322 Muls
[7] = BinaryOperator::CreateMul(Acc
[7], Acc
[7], "", LoopBB
);
323 NextAcc
[7] = BinaryOperator::CreateAdd(Muls
[7], Acc
[3], "", LoopBB
);
327 auto II
= LoopBB
->begin();
328 for (int i
= 0; i
< 8; i
++) {
329 PHINode
*Phi
= cast
<PHINode
>(&*II
++);
330 Phi
->addIncoming(Acc
[i
], LoopBB
);
331 Phi
->addIncoming(UndefValue::get(Ty
), EntryBB
);
334 BasicBlock
*ExitBB
= BasicBlock::Create(Context
, "bb2", F
);
335 BranchInst::Create(LoopBB
, ExitBB
, UndefValue::get(Type::getInt1Ty(Context
)),
338 Acc
[0] = BinaryOperator::CreateAdd(Acc
[0], Acc
[1], "", ExitBB
);
339 Acc
[1] = BinaryOperator::CreateAdd(Acc
[2], Acc
[3], "", ExitBB
);
340 Acc
[2] = BinaryOperator::CreateAdd(Acc
[4], Acc
[5], "", ExitBB
);
341 Acc
[3] = BinaryOperator::CreateAdd(Acc
[6], Acc
[7], "", ExitBB
);
342 Acc
[0] = BinaryOperator::CreateAdd(Acc
[0], Acc
[1], "", ExitBB
);
343 Acc
[1] = BinaryOperator::CreateAdd(Acc
[2], Acc
[3], "", ExitBB
);
344 Acc
[0] = BinaryOperator::CreateAdd(Acc
[0], Acc
[1], "", ExitBB
);
346 ReturnInst::Create(Context
, nullptr, ExitBB
);
348 ScalarEvolution SE
= buildSE(*F
);
350 EXPECT_NE(nullptr, SE
.getSCEV(Acc
[0]));
353 TEST_F(ScalarEvolutionsTest
, CompareValueComplexity
) {
354 IntegerType
*IntPtrTy
= M
.getDataLayout().getIntPtrType(Context
);
355 PointerType
*IntPtrPtrTy
= PointerType::getUnqual(Context
);
358 FunctionType::get(Type::getVoidTy(Context
), {IntPtrTy
, IntPtrTy
}, false);
359 Function
*F
= Function::Create(FTy
, Function::ExternalLinkage
, "f", M
);
360 BasicBlock
*EntryBB
= BasicBlock::Create(Context
, "entry", F
);
362 Value
*X
= &*F
->arg_begin();
363 Value
*Y
= &*std::next(F
->arg_begin());
365 const int ValueDepth
= 10;
366 for (int i
= 0; i
< ValueDepth
; i
++) {
367 X
= new LoadInst(IntPtrTy
, new IntToPtrInst(X
, IntPtrPtrTy
, "", EntryBB
),
369 /*isVolatile*/ false, EntryBB
);
370 Y
= new LoadInst(IntPtrTy
, new IntToPtrInst(Y
, IntPtrPtrTy
, "", EntryBB
),
372 /*isVolatile*/ false, EntryBB
);
375 auto *MulA
= BinaryOperator::CreateMul(X
, Y
, "", EntryBB
);
376 auto *MulB
= BinaryOperator::CreateMul(Y
, X
, "", EntryBB
);
377 ReturnInst::Create(Context
, nullptr, EntryBB
);
379 // This test isn't checking for correctness. Today making A and B resolve to
380 // the same SCEV would require deeper searching in CompareValueComplexity,
381 // which will slow down compilation. However, this test can fail (with LLVM's
382 // behavior still being correct) if we ever have a smarter
383 // CompareValueComplexity that is both fast and more accurate.
385 ScalarEvolution SE
= buildSE(*F
);
386 auto *A
= SE
.getSCEV(MulA
);
387 auto *B
= SE
.getSCEV(MulB
);
391 TEST_F(ScalarEvolutionsTest
, SCEVAddExpr
) {
392 Type
*Ty32
= Type::getInt32Ty(Context
);
393 Type
*ArgTys
[] = {Type::getInt64Ty(Context
), Ty32
, Ty32
, Ty32
, Ty32
, Ty32
};
396 FunctionType::get(Type::getVoidTy(Context
), ArgTys
, false);
397 Function
*F
= Function::Create(FTy
, Function::ExternalLinkage
, "f", M
);
399 Argument
*A1
= &*F
->arg_begin();
400 Argument
*A2
= &*(std::next(F
->arg_begin()));
401 BasicBlock
*EntryBB
= BasicBlock::Create(Context
, "entry", F
);
403 Instruction
*Trunc
= CastInst::CreateTruncOrBitCast(A1
, Ty32
, "", EntryBB
);
404 Instruction
*Mul1
= BinaryOperator::CreateMul(Trunc
, A2
, "", EntryBB
);
405 Instruction
*Add1
= BinaryOperator::CreateAdd(Mul1
, Trunc
, "", EntryBB
);
406 Mul1
= BinaryOperator::CreateMul(Add1
, Trunc
, "", EntryBB
);
407 Instruction
*Add2
= BinaryOperator::CreateAdd(Mul1
, Add1
, "", EntryBB
);
408 // FIXME: The size of this is arbitrary and doesn't seem to change the
409 // result, but SCEV will do quadratic work for these so a large number here
410 // will be extremely slow. We should revisit what and how this is testing
412 for (int i
= 0; i
< 10; i
++) {
413 Mul1
= BinaryOperator::CreateMul(Add2
, Add1
, "", EntryBB
);
415 Add2
= BinaryOperator::CreateAdd(Mul1
, Add1
, "", EntryBB
);
418 ReturnInst::Create(Context
, nullptr, EntryBB
);
419 ScalarEvolution SE
= buildSE(*F
);
420 EXPECT_NE(nullptr, SE
.getSCEV(Mul1
));
422 Argument
*A3
= &*(std::next(F
->arg_begin(), 2));
423 Argument
*A4
= &*(std::next(F
->arg_begin(), 3));
424 Argument
*A5
= &*(std::next(F
->arg_begin(), 4));
425 Argument
*A6
= &*(std::next(F
->arg_begin(), 5));
427 auto *AddWithNUW
= cast
<SCEVAddExpr
>(SE
.getAddExpr(
428 SE
.getAddExpr(SE
.getSCEV(A2
), SE
.getSCEV(A3
), SCEV::FlagNUW
),
429 SE
.getConstant(APInt(/*numBits=*/32, 5)), SCEV::FlagNUW
));
430 EXPECT_EQ(AddWithNUW
->getNumOperands(), 3u);
431 EXPECT_EQ(AddWithNUW
->getNoWrapFlags(), SCEV::FlagNUW
);
433 auto *AddWithAnyWrap
=
434 SE
.getAddExpr(SE
.getSCEV(A3
), SE
.getSCEV(A4
), SCEV::FlagAnyWrap
);
435 auto *AddWithAnyWrapNUW
= cast
<SCEVAddExpr
>(
436 SE
.getAddExpr(AddWithAnyWrap
, SE
.getSCEV(A5
), SCEV::FlagNUW
));
437 EXPECT_EQ(AddWithAnyWrapNUW
->getNumOperands(), 3u);
438 EXPECT_EQ(AddWithAnyWrapNUW
->getNoWrapFlags(), SCEV::FlagAnyWrap
);
440 auto *AddWithNSW
= SE
.getAddExpr(
441 SE
.getSCEV(A2
), SE
.getConstant(APInt(32, 99)), SCEV::FlagNSW
);
442 auto *AddWithNSW_NUW
= cast
<SCEVAddExpr
>(
443 SE
.getAddExpr(AddWithNSW
, SE
.getSCEV(A5
), SCEV::FlagNUW
));
444 EXPECT_EQ(AddWithNSW_NUW
->getNumOperands(), 3u);
445 EXPECT_EQ(AddWithNSW_NUW
->getNoWrapFlags(), SCEV::FlagAnyWrap
);
447 auto *AddWithNSWNUW
=
448 SE
.getAddExpr(SE
.getSCEV(A2
), SE
.getSCEV(A4
),
449 ScalarEvolution::setFlags(SCEV::FlagNUW
, SCEV::FlagNSW
));
450 auto *AddWithNSWNUW_NUW
= cast
<SCEVAddExpr
>(
451 SE
.getAddExpr(AddWithNSWNUW
, SE
.getSCEV(A5
), SCEV::FlagNUW
));
452 EXPECT_EQ(AddWithNSWNUW_NUW
->getNumOperands(), 3u);
453 EXPECT_EQ(AddWithNSWNUW_NUW
->getNoWrapFlags(), SCEV::FlagNUW
);
455 auto *AddWithNSW_NSWNUW
= cast
<SCEVAddExpr
>(
456 SE
.getAddExpr(AddWithNSW
, SE
.getSCEV(A6
),
457 ScalarEvolution::setFlags(SCEV::FlagNUW
, SCEV::FlagNSW
)));
458 EXPECT_EQ(AddWithNSW_NSWNUW
->getNumOperands(), 3u);
459 EXPECT_EQ(AddWithNSW_NSWNUW
->getNoWrapFlags(), SCEV::FlagAnyWrap
);
462 static Instruction
&GetInstByName(Function
&F
, StringRef Name
) {
463 for (auto &I
: instructions(F
))
464 if (I
.getName() == Name
)
466 llvm_unreachable("Could not find instructions!");
469 TEST_F(ScalarEvolutionsTest
, SCEVNormalization
) {
472 std::unique_ptr
<Module
> M
= parseAssemblyString(
473 "target datalayout = \"e-m:e-p:32:32-f64:32:64-f80:32-n8:16:32-S128\" "
475 "@var_0 = external global i32, align 4"
476 "@var_1 = external global i32, align 4"
477 "@var_2 = external global i32, align 4"
479 "declare i32 @unknown(i32, i32, i32)"
481 "define void @f_1(i8* nocapture %arr, i32 %n, i32* %A, i32* %B) "
482 " local_unnamed_addr { "
484 " br label %loop.ph "
490 " %iv0 = phi i32 [ %iv0.inc, %loop ], [ 0, %loop.ph ] "
491 " %iv1 = phi i32 [ %iv1.inc, %loop ], [ -2147483648, %loop.ph ] "
492 " %iv0.inc = add i32 %iv0, 1 "
493 " %iv1.inc = add i32 %iv1, 3 "
494 " br i1 undef, label %for.end.loopexit, label %loop "
500 "define void @f_2(i32 %a, i32 %b, i32 %c, i32 %d) "
501 " local_unnamed_addr { "
506 " br i1 undef, label %loop_0, label %loop_1 "
509 " br i1 undef, label %loop_2, label %loop_1 "
513 " br i1 undef, label %end, label %loop_2 "
521 assert(M
&& "Could not parse module?");
522 assert(!verifyModule(*M
) && "Must have been well formed!");
524 runWithSE(*M
, "f_1", [&](Function
&F
, LoopInfo
&LI
, ScalarEvolution
&SE
) {
525 auto &I0
= GetInstByName(F
, "iv0");
526 auto &I1
= *I0
.getNextNode();
528 auto *S0
= cast
<SCEVAddRecExpr
>(SE
.getSCEV(&I0
));
529 PostIncLoopSet Loops
;
530 Loops
.insert(S0
->getLoop());
531 auto *N0
= normalizeForPostIncUse(S0
, Loops
, SE
);
532 auto *D0
= denormalizeForPostIncUse(N0
, Loops
, SE
);
533 EXPECT_EQ(S0
, D0
) << *S0
<< " " << *D0
;
535 auto *S1
= cast
<SCEVAddRecExpr
>(SE
.getSCEV(&I1
));
537 Loops
.insert(S1
->getLoop());
538 auto *N1
= normalizeForPostIncUse(S1
, Loops
, SE
);
539 auto *D1
= denormalizeForPostIncUse(N1
, Loops
, SE
);
540 EXPECT_EQ(S1
, D1
) << *S1
<< " " << *D1
;
543 runWithSE(*M
, "f_2", [&](Function
&F
, LoopInfo
&LI
, ScalarEvolution
&SE
) {
544 auto *L2
= *LI
.begin();
545 auto *L1
= *std::next(LI
.begin());
546 auto *L0
= *std::next(LI
.begin(), 2);
548 auto GetAddRec
= [&SE
](const Loop
*L
, std::initializer_list
<const SCEV
*> Ops
) {
549 SmallVector
<const SCEV
*, 4> OpsCopy(Ops
);
550 return SE
.getAddRecExpr(OpsCopy
, L
, SCEV::FlagAnyWrap
);
553 auto GetAdd
= [&SE
](std::initializer_list
<const SCEV
*> Ops
) {
554 SmallVector
<const SCEV
*, 4> OpsCopy(Ops
);
555 return SE
.getAddExpr(OpsCopy
, SCEV::FlagAnyWrap
);
558 // We first populate the AddRecs vector with a few "interesting" SCEV
559 // expressions, and then we go through the list and assert that each
560 // expression in it has an invertible normalization.
562 std::vector
<const SCEV
*> Exprs
;
564 const SCEV
*V0
= SE
.getSCEV(&*F
.arg_begin());
565 const SCEV
*V1
= SE
.getSCEV(&*std::next(F
.arg_begin(), 1));
566 const SCEV
*V2
= SE
.getSCEV(&*std::next(F
.arg_begin(), 2));
567 const SCEV
*V3
= SE
.getSCEV(&*std::next(F
.arg_begin(), 3));
569 Exprs
.push_back(GetAddRec(L0
, {V0
})); // 0
570 Exprs
.push_back(GetAddRec(L0
, {V0
, V1
})); // 1
571 Exprs
.push_back(GetAddRec(L0
, {V0
, V1
, V2
})); // 2
572 Exprs
.push_back(GetAddRec(L0
, {V0
, V1
, V2
, V3
})); // 3
575 GetAddRec(L1
, {Exprs
[1], Exprs
[2], Exprs
[3], Exprs
[0]})); // 4
577 GetAddRec(L1
, {Exprs
[1], Exprs
[2], Exprs
[0], Exprs
[3]})); // 5
579 GetAddRec(L1
, {Exprs
[1], Exprs
[3], Exprs
[3], Exprs
[1]})); // 6
581 Exprs
.push_back(GetAdd({Exprs
[6], Exprs
[3], V2
})); // 7
584 GetAddRec(L2
, {Exprs
[4], Exprs
[3], Exprs
[3], Exprs
[5]})); // 8
587 GetAddRec(L2
, {Exprs
[4], Exprs
[6], Exprs
[7], Exprs
[3], V0
})); // 9
590 std::vector
<PostIncLoopSet
> LoopSets
;
591 for (int i
= 0; i
< 8; i
++) {
592 LoopSets
.emplace_back();
594 LoopSets
.back().insert(L0
);
596 LoopSets
.back().insert(L1
);
598 LoopSets
.back().insert(L2
);
601 for (const auto &LoopSet
: LoopSets
)
602 for (auto *S
: Exprs
) {
604 auto *N
= llvm::normalizeForPostIncUse(S
, LoopSet
, SE
);
605 auto *D
= llvm::denormalizeForPostIncUse(N
, LoopSet
, SE
);
607 // Normalization and then denormalizing better give us back the same
609 EXPECT_EQ(S
, D
) << "S = " << *S
<< " D = " << *D
<< " N = " << *N
;
612 auto *D
= llvm::denormalizeForPostIncUse(S
, LoopSet
, SE
);
613 auto *N
= llvm::normalizeForPostIncUse(D
, LoopSet
, SE
);
615 // Denormalization and then normalizing better give us back the same
617 EXPECT_EQ(S
, N
) << "S = " << *S
<< " N = " << *N
;
623 // Expect the call of getZeroExtendExpr will not cost exponential time.
624 TEST_F(ScalarEvolutionsTest
, SCEVZeroExtendExpr
) {
628 // Generate a function like below:
629 // define void @foo() {
631 // br label %for.cond
634 // %0 = phi i64 [ 100, %entry ], [ %dec, %for.inc ]
635 // %cmp = icmp sgt i64 %0, 90
636 // br i1 %cmp, label %for.inc, label %for.cond1
639 // %dec = add nsw i64 %0, -1
640 // br label %for.cond
643 // %1 = phi i64 [ 100, %for.cond ], [ %dec5, %for.inc2 ]
644 // %cmp3 = icmp sgt i64 %1, 90
645 // br i1 %cmp3, label %for.inc2, label %for.cond4
648 // %dec5 = add nsw i64 %1, -1
649 // br label %for.cond1
654 // %19 = phi i64 [ 100, %for.cond84 ], [ %dec94, %for.inc92 ]
655 // %cmp93 = icmp sgt i64 %19, 90
656 // br i1 %cmp93, label %for.inc92, label %for.end
659 // %dec94 = add nsw i64 %19, -1
660 // br label %for.cond89
663 // %gep = getelementptr i8, i8* null, i64 %dec
664 // %gep6 = getelementptr i8, i8* %gep, i64 %dec5
666 // %gep95 = getelementptr i8, i8* %gep91, i64 %dec94
669 FunctionType
*FTy
= FunctionType::get(Type::getVoidTy(Context
), {}, false);
670 Function
*F
= Function::Create(FTy
, Function::ExternalLinkage
, "foo", M
);
672 BasicBlock
*EntryBB
= BasicBlock::Create(Context
, "entry", F
);
673 BasicBlock
*CondBB
= BasicBlock::Create(Context
, "for.cond", F
);
674 BasicBlock
*EndBB
= BasicBlock::Create(Context
, "for.end", F
);
675 BranchInst::Create(CondBB
, EntryBB
);
676 BasicBlock
*PrevBB
= EntryBB
;
678 Type
*I64Ty
= Type::getInt64Ty(Context
);
679 Type
*I8Ty
= Type::getInt8Ty(Context
);
680 Type
*I8PtrTy
= PointerType::getUnqual(Context
);
681 Value
*Accum
= Constant::getNullValue(I8PtrTy
);
683 for (int i
= 0; i
< Iters
; i
++) {
684 BasicBlock
*IncBB
= BasicBlock::Create(Context
, "for.inc", F
, EndBB
);
685 auto *PN
= PHINode::Create(I64Ty
, 2, "", CondBB
);
686 PN
->addIncoming(ConstantInt::get(Context
, APInt(64, 100)), PrevBB
);
687 auto *Cmp
= CmpInst::Create(Instruction::ICmp
, CmpInst::ICMP_SGT
, PN
,
688 ConstantInt::get(Context
, APInt(64, 90)), "cmp",
692 NextBB
= BasicBlock::Create(Context
, "for.cond", F
, EndBB
);
695 BranchInst::Create(IncBB
, NextBB
, Cmp
, CondBB
);
696 auto *Dec
= BinaryOperator::CreateNSWAdd(
697 PN
, ConstantInt::get(Context
, APInt(64, -1)), "dec", IncBB
);
698 PN
->addIncoming(Dec
, IncBB
);
699 BranchInst::Create(CondBB
, IncBB
);
701 Accum
= GetElementPtrInst::Create(I8Ty
, Accum
, PN
, "gep", EndBB
);
706 ReturnInst::Create(Context
, nullptr, EndBB
);
707 ScalarEvolution SE
= buildSE(*F
);
708 const SCEV
*S
= SE
.getSCEV(Accum
);
709 S
= SE
.getLosslessPtrToIntExpr(S
);
710 Type
*I128Ty
= Type::getInt128Ty(Context
);
711 SE
.getZeroExtendExpr(S
, I128Ty
);
714 // Make sure that SCEV invalidates exit limits after invalidating the values it
715 // depends on when we forget a loop.
716 TEST_F(ScalarEvolutionsTest
, SCEVExitLimitForgetLoop
) {
718 * Create the following code:
719 * func(i64 addrspace(10)* %arg)
725 * %phi = phi i64 [i64 0, %L.ph], [ %add, %L2 ]
726 * %add = add i64 %phi2, 1
727 * %cond = icmp slt i64 %add, 1000; then becomes 2000.
728 * br i1 %cond, label %post, label %L2
734 // Create a module with non-integral pointers in it's datalayout
735 Module
NIM("nonintegral", Context
);
736 std::string DataLayout
= M
.getDataLayoutStr();
737 if (!DataLayout
.empty())
739 DataLayout
+= "ni:10";
740 NIM
.setDataLayout(DataLayout
);
742 Type
*T_int64
= Type::getInt64Ty(Context
);
743 Type
*T_pint64
= PointerType::get(Context
, 10);
746 FunctionType::get(Type::getVoidTy(Context
), {T_pint64
}, false);
747 Function
*F
= Function::Create(FTy
, Function::ExternalLinkage
, "foo", NIM
);
749 BasicBlock
*Top
= BasicBlock::Create(Context
, "top", F
);
750 BasicBlock
*LPh
= BasicBlock::Create(Context
, "L.ph", F
);
751 BasicBlock
*L
= BasicBlock::Create(Context
, "L", F
);
752 BasicBlock
*Post
= BasicBlock::Create(Context
, "post", F
);
754 IRBuilder
<> Builder(Top
);
755 Builder
.CreateBr(LPh
);
757 Builder
.SetInsertPoint(LPh
);
760 Builder
.SetInsertPoint(L
);
761 PHINode
*Phi
= Builder
.CreatePHI(T_int64
, 2);
762 auto *Add
= cast
<Instruction
>(
763 Builder
.CreateAdd(Phi
, ConstantInt::get(T_int64
, 1), "add"));
764 auto *Limit
= ConstantInt::get(T_int64
, 1000);
765 auto *Cond
= cast
<Instruction
>(
766 Builder
.CreateICmp(ICmpInst::ICMP_SLT
, Add
, Limit
, "cond"));
767 auto *Br
= cast
<Instruction
>(Builder
.CreateCondBr(Cond
, L
, Post
));
768 Phi
->addIncoming(ConstantInt::get(T_int64
, 0), LPh
);
769 Phi
->addIncoming(Add
, L
);
771 Builder
.SetInsertPoint(Post
);
772 Builder
.CreateRetVoid();
774 ScalarEvolution SE
= buildSE(*F
);
775 auto *Loop
= LI
->getLoopFor(L
);
776 const SCEV
*EC
= SE
.getBackedgeTakenCount(Loop
);
777 EXPECT_FALSE(isa
<SCEVCouldNotCompute
>(EC
));
778 EXPECT_TRUE(isa
<SCEVConstant
>(EC
));
779 EXPECT_EQ(cast
<SCEVConstant
>(EC
)->getAPInt().getLimitedValue(), 999u);
781 // The add recurrence {5,+,1} does not correspond to any PHI in the IR, and
782 // that is relevant to this test.
783 auto *Five
= SE
.getConstant(APInt(/*numBits=*/64, 5));
785 SE
.getAddRecExpr(Five
, SE
.getOne(T_int64
), Loop
, SCEV::FlagAnyWrap
);
786 const SCEV
*ARAtLoopExit
= SE
.getSCEVAtScope(AR
, nullptr);
787 EXPECT_FALSE(isa
<SCEVCouldNotCompute
>(ARAtLoopExit
));
788 EXPECT_TRUE(isa
<SCEVConstant
>(ARAtLoopExit
));
789 EXPECT_EQ(cast
<SCEVConstant
>(ARAtLoopExit
)->getAPInt().getLimitedValue(),
793 Br
->eraseFromParent();
794 Cond
->eraseFromParent();
796 Builder
.SetInsertPoint(L
);
797 auto *NewCond
= Builder
.CreateICmp(
798 ICmpInst::ICMP_SLT
, Add
, ConstantInt::get(T_int64
, 2000), "new.cond");
799 Builder
.CreateCondBr(NewCond
, L
, Post
);
800 const SCEV
*NewEC
= SE
.getBackedgeTakenCount(Loop
);
801 EXPECT_FALSE(isa
<SCEVCouldNotCompute
>(NewEC
));
802 EXPECT_TRUE(isa
<SCEVConstant
>(NewEC
));
803 EXPECT_EQ(cast
<SCEVConstant
>(NewEC
)->getAPInt().getLimitedValue(), 1999u);
804 const SCEV
*NewARAtLoopExit
= SE
.getSCEVAtScope(AR
, nullptr);
805 EXPECT_FALSE(isa
<SCEVCouldNotCompute
>(NewARAtLoopExit
));
806 EXPECT_TRUE(isa
<SCEVConstant
>(NewARAtLoopExit
));
807 EXPECT_EQ(cast
<SCEVConstant
>(NewARAtLoopExit
)->getAPInt().getLimitedValue(),
811 // Make sure that SCEV invalidates exit limits after invalidating the values it
812 // depends on when we forget a value.
813 TEST_F(ScalarEvolutionsTest
, SCEVExitLimitForgetValue
) {
815 * Create the following code:
816 * func(i64 addrspace(10)* %arg)
820 * %load = load i64 addrspace(10)* %arg
823 * %phi = phi i64 [i64 0, %L.ph], [ %add, %L2 ]
824 * %add = add i64 %phi2, 1
825 * %cond = icmp slt i64 %add, %load ; then becomes 2000.
826 * br i1 %cond, label %post, label %L2
832 // Create a module with non-integral pointers in it's datalayout
833 Module
NIM("nonintegral", Context
);
834 std::string DataLayout
= M
.getDataLayoutStr();
835 if (!DataLayout
.empty())
837 DataLayout
+= "ni:10";
838 NIM
.setDataLayout(DataLayout
);
840 Type
*T_int64
= Type::getInt64Ty(Context
);
841 Type
*T_pint64
= PointerType::get(Context
, 10);
844 FunctionType::get(Type::getVoidTy(Context
), {T_pint64
}, false);
845 Function
*F
= Function::Create(FTy
, Function::ExternalLinkage
, "foo", NIM
);
847 Argument
*Arg
= &*F
->arg_begin();
849 BasicBlock
*Top
= BasicBlock::Create(Context
, "top", F
);
850 BasicBlock
*LPh
= BasicBlock::Create(Context
, "L.ph", F
);
851 BasicBlock
*L
= BasicBlock::Create(Context
, "L", F
);
852 BasicBlock
*Post
= BasicBlock::Create(Context
, "post", F
);
854 IRBuilder
<> Builder(Top
);
855 Builder
.CreateBr(LPh
);
857 Builder
.SetInsertPoint(LPh
);
858 auto *Load
= cast
<Instruction
>(Builder
.CreateLoad(T_int64
, Arg
, "load"));
861 Builder
.SetInsertPoint(L
);
862 PHINode
*Phi
= Builder
.CreatePHI(T_int64
, 2);
863 auto *Add
= cast
<Instruction
>(
864 Builder
.CreateAdd(Phi
, ConstantInt::get(T_int64
, 1), "add"));
865 auto *Cond
= cast
<Instruction
>(
866 Builder
.CreateICmp(ICmpInst::ICMP_SLT
, Add
, Load
, "cond"));
867 auto *Br
= cast
<Instruction
>(Builder
.CreateCondBr(Cond
, L
, Post
));
868 Phi
->addIncoming(ConstantInt::get(T_int64
, 0), LPh
);
869 Phi
->addIncoming(Add
, L
);
871 Builder
.SetInsertPoint(Post
);
872 Builder
.CreateRetVoid();
874 ScalarEvolution SE
= buildSE(*F
);
875 auto *Loop
= LI
->getLoopFor(L
);
876 const SCEV
*EC
= SE
.getBackedgeTakenCount(Loop
);
877 EXPECT_FALSE(isa
<SCEVCouldNotCompute
>(EC
));
878 EXPECT_FALSE(isa
<SCEVConstant
>(EC
));
880 SE
.forgetValue(Load
);
881 Br
->eraseFromParent();
882 Cond
->eraseFromParent();
883 Load
->eraseFromParent();
885 Builder
.SetInsertPoint(L
);
886 auto *NewCond
= Builder
.CreateICmp(
887 ICmpInst::ICMP_SLT
, Add
, ConstantInt::get(T_int64
, 2000), "new.cond");
888 Builder
.CreateCondBr(NewCond
, L
, Post
);
889 const SCEV
*NewEC
= SE
.getBackedgeTakenCount(Loop
);
890 EXPECT_FALSE(isa
<SCEVCouldNotCompute
>(NewEC
));
891 EXPECT_TRUE(isa
<SCEVConstant
>(NewEC
));
892 EXPECT_EQ(cast
<SCEVConstant
>(NewEC
)->getAPInt().getLimitedValue(), 1999u);
895 TEST_F(ScalarEvolutionsTest
, SCEVAddRecFromPHIwithLargeConstants
) {
896 // Reference: https://reviews.llvm.org/D37265
897 // Make sure that SCEV does not blow up when constructing an AddRec
898 // with predicates for a phi with the update pattern:
899 // (SExt/ZExt ix (Trunc iy (%SymbolicPHI) to ix) to iy) + InvariantAccum
900 // when either the initial value of the Phi or the InvariantAccum are
901 // constants that are too large to fit in an ix but are zero when truncated to
904 FunctionType::get(Type::getVoidTy(Context
), std::vector
<Type
*>(), false);
906 Function::Create(FTy
, Function::ExternalLinkage
, "addrecphitest", M
);
913 %0 = phi i64 [-9223372036854775808, %entry], [%3, %loop]
915 %2 = ashr exact i64 %1, 32
916 %3 = add i64 %2, -9223372036854775808
917 br i1 undef, label %exit, label %loop
921 BasicBlock
*EntryBB
= BasicBlock::Create(Context
, "entry", F
);
922 BasicBlock
*LoopBB
= BasicBlock::Create(Context
, "loop", F
);
923 BasicBlock
*ExitBB
= BasicBlock::Create(Context
, "exit", F
);
926 BranchInst::Create(LoopBB
, EntryBB
);
929 ConstantInt::get(Context
, APInt(64, 0x8000000000000000U
, true));
930 auto *Int64_32
= ConstantInt::get(Context
, APInt(64, 32));
931 auto *Br
= BranchInst::Create(
932 LoopBB
, ExitBB
, UndefValue::get(Type::getInt1Ty(Context
)), LoopBB
);
933 auto *Phi
= PHINode::Create(Type::getInt64Ty(Context
), 2, "", Br
);
934 auto *Shl
= BinaryOperator::CreateShl(Phi
, Int64_32
, "", Br
);
935 auto *AShr
= BinaryOperator::CreateExactAShr(Shl
, Int64_32
, "", Br
);
936 auto *Add
= BinaryOperator::CreateAdd(AShr
, MinInt64
, "", Br
);
937 Phi
->addIncoming(MinInt64
, EntryBB
);
938 Phi
->addIncoming(Add
, LoopBB
);
940 ReturnInst::Create(Context
, nullptr, ExitBB
);
942 // Make sure that SCEV doesn't blow up
943 ScalarEvolution SE
= buildSE(*F
);
944 const SCEV
*Expr
= SE
.getSCEV(Phi
);
945 EXPECT_NE(nullptr, Expr
);
946 EXPECT_TRUE(isa
<SCEVUnknown
>(Expr
));
947 auto Result
= SE
.createAddRecFromPHIWithCasts(cast
<SCEVUnknown
>(Expr
));
950 TEST_F(ScalarEvolutionsTest
, SCEVAddRecFromPHIwithLargeConstantAccum
) {
951 // Make sure that SCEV does not blow up when constructing an AddRec
952 // with predicates for a phi with the update pattern:
953 // (SExt/ZExt ix (Trunc iy (%SymbolicPHI) to ix) to iy) + InvariantAccum
954 // when the InvariantAccum is a constant that is too large to fit in an
955 // ix but are zero when truncated to ix, and the initial value of the
956 // phi is not a constant.
957 Type
*Int32Ty
= Type::getInt32Ty(Context
);
958 SmallVector
<Type
*, 1> Types
;
959 Types
.push_back(Int32Ty
);
960 FunctionType
*FTy
= FunctionType::get(Type::getVoidTy(Context
), Types
, false);
962 Function::Create(FTy
, Function::ExternalLinkage
, "addrecphitest", M
);
966 define @addrecphitest(i32)
970 %1 = phi i32 [%0, %entry], [%4, %loop]
972 %3 = ashr exact i32 %2, 16
973 %4 = add i32 %3, -2147483648
974 br i1 undef, label %exit, label %loop
978 BasicBlock
*EntryBB
= BasicBlock::Create(Context
, "entry", F
);
979 BasicBlock
*LoopBB
= BasicBlock::Create(Context
, "loop", F
);
980 BasicBlock
*ExitBB
= BasicBlock::Create(Context
, "exit", F
);
983 BranchInst::Create(LoopBB
, EntryBB
);
985 auto *MinInt32
= ConstantInt::get(Context
, APInt(32, 0x80000000U
, true));
986 auto *Int32_16
= ConstantInt::get(Context
, APInt(32, 16));
987 auto *Br
= BranchInst::Create(
988 LoopBB
, ExitBB
, UndefValue::get(Type::getInt1Ty(Context
)), LoopBB
);
989 auto *Phi
= PHINode::Create(Int32Ty
, 2, "", Br
);
990 auto *Shl
= BinaryOperator::CreateShl(Phi
, Int32_16
, "", Br
);
991 auto *AShr
= BinaryOperator::CreateExactAShr(Shl
, Int32_16
, "", Br
);
992 auto *Add
= BinaryOperator::CreateAdd(AShr
, MinInt32
, "", Br
);
993 auto *Arg
= &*(F
->arg_begin());
994 Phi
->addIncoming(Arg
, EntryBB
);
995 Phi
->addIncoming(Add
, LoopBB
);
997 ReturnInst::Create(Context
, nullptr, ExitBB
);
999 // Make sure that SCEV doesn't blow up
1000 ScalarEvolution SE
= buildSE(*F
);
1001 const SCEV
*Expr
= SE
.getSCEV(Phi
);
1002 EXPECT_NE(nullptr, Expr
);
1003 EXPECT_TRUE(isa
<SCEVUnknown
>(Expr
));
1004 auto Result
= SE
.createAddRecFromPHIWithCasts(cast
<SCEVUnknown
>(Expr
));
1007 TEST_F(ScalarEvolutionsTest
, SCEVFoldSumOfTruncs
) {
1008 // Verify that the following SCEV gets folded to a zero:
1009 // (-1 * (trunc i64 (-1 * %0) to i32)) + (-1 * (trunc i64 %0 to i32)
1010 Type
*ArgTy
= Type::getInt64Ty(Context
);
1011 Type
*Int32Ty
= Type::getInt32Ty(Context
);
1012 SmallVector
<Type
*, 1> Types
;
1013 Types
.push_back(ArgTy
);
1014 FunctionType
*FTy
= FunctionType::get(Type::getVoidTy(Context
), Types
, false);
1015 Function
*F
= Function::Create(FTy
, Function::ExternalLinkage
, "f", M
);
1016 BasicBlock
*BB
= BasicBlock::Create(Context
, "entry", F
);
1017 ReturnInst::Create(Context
, nullptr, BB
);
1019 ScalarEvolution SE
= buildSE(*F
);
1021 auto *Arg
= &*(F
->arg_begin());
1022 const auto *ArgSCEV
= SE
.getSCEV(Arg
);
1025 const auto *A0
= SE
.getNegativeSCEV(ArgSCEV
);
1026 const auto *A1
= SE
.getTruncateExpr(A0
, Int32Ty
);
1027 const auto *A
= SE
.getNegativeSCEV(A1
);
1029 const auto *B0
= SE
.getTruncateExpr(ArgSCEV
, Int32Ty
);
1030 const auto *B
= SE
.getNegativeSCEV(B0
);
1032 const auto *Expr
= SE
.getAddExpr(A
, B
);
1033 // Verify that the SCEV was folded to 0
1034 const auto *ZeroConst
= SE
.getConstant(Int32Ty
, 0);
1035 EXPECT_EQ(Expr
, ZeroConst
);
1038 // Check logic of SCEV expression size computation.
1039 TEST_F(ScalarEvolutionsTest
, SCEVComputeExpressionSize
) {
1041 * Create the following code:
1042 * void func(i64 %a, i64 %b)
1044 * %s1 = add i64 %a, 1
1045 * %s2 = udiv i64 %s1, %b
1052 Module
M("SCEVComputeExpressionSize", Context
);
1054 Type
*T_int64
= Type::getInt64Ty(Context
);
1057 FunctionType::get(Type::getVoidTy(Context
), { T_int64
, T_int64
}, false);
1058 Function
*F
= Function::Create(FTy
, Function::ExternalLinkage
, "func", M
);
1059 Argument
*A
= &*F
->arg_begin();
1060 Argument
*B
= &*std::next(F
->arg_begin());
1061 ConstantInt
*C
= ConstantInt::get(Context
, APInt(64, 1));
1063 BasicBlock
*Entry
= BasicBlock::Create(Context
, "entry", F
);
1064 BasicBlock
*Exit
= BasicBlock::Create(Context
, "exit", F
);
1066 IRBuilder
<> Builder(Entry
);
1067 auto *S1
= cast
<Instruction
>(Builder
.CreateAdd(A
, C
, "s1"));
1068 auto *S2
= cast
<Instruction
>(Builder
.CreateUDiv(S1
, B
, "s2"));
1069 Builder
.CreateBr(Exit
);
1071 Builder
.SetInsertPoint(Exit
);
1072 Builder
.CreateRetVoid();
1074 ScalarEvolution SE
= buildSE(*F
);
1075 // Get S2 first to move it to cache.
1076 const SCEV
*AS
= SE
.getSCEV(A
);
1077 const SCEV
*BS
= SE
.getSCEV(B
);
1078 const SCEV
*CS
= SE
.getSCEV(C
);
1079 const SCEV
*S1S
= SE
.getSCEV(S1
);
1080 const SCEV
*S2S
= SE
.getSCEV(S2
);
1081 EXPECT_EQ(AS
->getExpressionSize(), 1u);
1082 EXPECT_EQ(BS
->getExpressionSize(), 1u);
1083 EXPECT_EQ(CS
->getExpressionSize(), 1u);
1084 EXPECT_EQ(S1S
->getExpressionSize(), 3u);
1085 EXPECT_EQ(S2S
->getExpressionSize(), 5u);
1088 TEST_F(ScalarEvolutionsTest
, SCEVLoopDecIntrinsic
) {
1091 std::unique_ptr
<Module
> M
= parseAssemblyString(
1092 "define void @foo(i32 %N) { "
1094 " %cmp3 = icmp sgt i32 %N, 0 "
1095 " br i1 %cmp3, label %for.body, label %for.cond.cleanup "
1096 "for.cond.cleanup: "
1099 " %i.04 = phi i32 [ %inc, %for.body ], [ 100, %entry ] "
1100 " %inc = call i32 @llvm.loop.decrement.reg.i32.i32.i32(i32 %i.04, i32 1) "
1101 " %exitcond = icmp ne i32 %inc, 0 "
1102 " br i1 %exitcond, label %for.cond.cleanup, label %for.body "
1104 "declare i32 @llvm.loop.decrement.reg.i32.i32.i32(i32, i32) ",
1107 ASSERT_TRUE(M
&& "Could not parse module?");
1108 ASSERT_TRUE(!verifyModule(*M
) && "Must have been well formed!");
1110 runWithSE(*M
, "foo", [&](Function
&F
, LoopInfo
&LI
, ScalarEvolution
&SE
) {
1111 auto *ScevInc
= SE
.getSCEV(getInstructionByName(F
, "inc"));
1112 EXPECT_TRUE(isa
<SCEVAddRecExpr
>(ScevInc
));
1116 TEST_F(ScalarEvolutionsTest
, SCEVComputeConstantDifference
) {
1119 std::unique_ptr
<Module
> M
= parseAssemblyString(
1120 "define void @foo(i32 %sz, i32 %pp) { "
1122 " %v0 = add i32 %pp, 0 "
1123 " %v3 = add i32 %pp, 3 "
1124 " br label %loop.body "
1126 " %iv = phi i32 [ %iv.next, %loop.body ], [ 0, %entry ] "
1127 " %xa = add nsw i32 %iv, %v0 "
1128 " %yy = add nsw i32 %iv, %v3 "
1129 " %xb = sub nsw i32 %yy, 3 "
1130 " %iv.next = add nsw i32 %iv, 1 "
1131 " %cmp = icmp sle i32 %iv.next, %sz "
1132 " br i1 %cmp, label %loop.body, label %exit "
1138 ASSERT_TRUE(M
&& "Could not parse module?");
1139 ASSERT_TRUE(!verifyModule(*M
) && "Must have been well formed!");
1141 runWithSE(*M
, "foo", [](Function
&F
, LoopInfo
&LI
, ScalarEvolution
&SE
) {
1142 auto *ScevV0
= SE
.getSCEV(getInstructionByName(F
, "v0")); // %pp
1143 auto *ScevV3
= SE
.getSCEV(getInstructionByName(F
, "v3")); // (3 + %pp)
1144 auto *ScevIV
= SE
.getSCEV(getInstructionByName(F
, "iv")); // {0,+,1}
1145 auto *ScevXA
= SE
.getSCEV(getInstructionByName(F
, "xa")); // {%pp,+,1}
1146 auto *ScevYY
= SE
.getSCEV(getInstructionByName(F
, "yy")); // {(3 + %pp),+,1}
1147 auto *ScevXB
= SE
.getSCEV(getInstructionByName(F
, "xb")); // {%pp,+,1}
1148 auto *ScevIVNext
= SE
.getSCEV(getInstructionByName(F
, "iv.next")); // {1,+,1}
1150 auto diff
= [&SE
](const SCEV
*LHS
, const SCEV
*RHS
) -> std::optional
<int> {
1151 auto ConstantDiffOrNone
= computeConstantDifference(SE
, LHS
, RHS
);
1152 if (!ConstantDiffOrNone
)
1153 return std::nullopt
;
1155 auto ExtDiff
= ConstantDiffOrNone
->getSExtValue();
1157 assert(Diff
== ExtDiff
&& "Integer overflow");
1161 EXPECT_EQ(diff(ScevV3
, ScevV0
), 3);
1162 EXPECT_EQ(diff(ScevV0
, ScevV3
), -3);
1163 EXPECT_EQ(diff(ScevV0
, ScevV0
), 0);
1164 EXPECT_EQ(diff(ScevV3
, ScevV3
), 0);
1165 EXPECT_EQ(diff(ScevIV
, ScevIV
), 0);
1166 EXPECT_EQ(diff(ScevXA
, ScevXB
), 0);
1167 EXPECT_EQ(diff(ScevXA
, ScevYY
), -3);
1168 EXPECT_EQ(diff(ScevYY
, ScevXB
), 3);
1169 EXPECT_EQ(diff(ScevIV
, ScevIVNext
), -1);
1170 EXPECT_EQ(diff(ScevIVNext
, ScevIV
), 1);
1171 EXPECT_EQ(diff(ScevIVNext
, ScevIVNext
), 0);
1172 EXPECT_EQ(diff(ScevV0
, ScevIV
), std::nullopt
);
1173 EXPECT_EQ(diff(ScevIVNext
, ScevV3
), std::nullopt
);
1174 EXPECT_EQ(diff(ScevYY
, ScevV3
), std::nullopt
);
1178 TEST_F(ScalarEvolutionsTest
, SCEVrewriteUnknowns
) {
1181 std::unique_ptr
<Module
> M
= parseAssemblyString(
1182 "define void @foo(i32 %i) { "
1184 " %cmp3 = icmp ult i32 %i, 16 "
1185 " br i1 %cmp3, label %loop.body, label %exit "
1187 " %iv = phi i32 [ %iv.next, %loop.body ], [ %i, %entry ] "
1188 " %iv.next = add nsw i32 %iv, 1 "
1189 " %cmp = icmp eq i32 %iv.next, 16 "
1190 " br i1 %cmp, label %exit, label %loop.body "
1196 ASSERT_TRUE(M
&& "Could not parse module?");
1197 ASSERT_TRUE(!verifyModule(*M
) && "Must have been well formed!");
1199 runWithSE(*M
, "foo", [](Function
&F
, LoopInfo
&LI
, ScalarEvolution
&SE
) {
1200 auto *ScevIV
= SE
.getSCEV(getInstructionByName(F
, "iv")); // {0,+,1}
1201 auto *ScevI
= SE
.getSCEV(getArgByName(F
, "i")); // {0,+,1}
1203 ValueToSCEVMapTy RewriteMap
;
1204 RewriteMap
[cast
<SCEVUnknown
>(ScevI
)->getValue()] =
1205 SE
.getUMinExpr(ScevI
, SE
.getConstant(ScevI
->getType(), 17));
1206 auto *WithUMin
= SCEVParameterRewriter::rewrite(ScevIV
, SE
, RewriteMap
);
1208 EXPECT_NE(WithUMin
, ScevIV
);
1209 auto *AR
= dyn_cast
<SCEVAddRecExpr
>(WithUMin
);
1211 EXPECT_EQ(AR
->getStart(),
1212 SE
.getUMinExpr(ScevI
, SE
.getConstant(ScevI
->getType(), 17)));
1213 EXPECT_EQ(AR
->getStepRecurrence(SE
),
1214 cast
<SCEVAddRecExpr
>(ScevIV
)->getStepRecurrence(SE
));
1218 TEST_F(ScalarEvolutionsTest
, SCEVAddNUW
) {
1221 std::unique_ptr
<Module
> M
= parseAssemblyString("define void @foo(i32 %x) { "
1226 ASSERT_TRUE(M
&& "Could not parse module?");
1227 ASSERT_TRUE(!verifyModule(*M
) && "Must have been well formed!");
1229 runWithSE(*M
, "foo", [](Function
&F
, LoopInfo
&LI
, ScalarEvolution
&SE
) {
1230 auto *X
= SE
.getSCEV(getArgByName(F
, "x"));
1231 auto *One
= SE
.getOne(X
->getType());
1232 auto *Sum
= SE
.getAddExpr(X
, One
, SCEV::FlagNUW
);
1233 EXPECT_TRUE(SE
.isKnownPredicate(ICmpInst::ICMP_UGE
, Sum
, X
));
1234 EXPECT_TRUE(SE
.isKnownPredicate(ICmpInst::ICMP_UGT
, Sum
, X
));
1238 TEST_F(ScalarEvolutionsTest
, SCEVgetRanges
) {
1241 std::unique_ptr
<Module
> M
= parseAssemblyString(
1242 "define void @foo(i32 %i) { "
1244 " br label %loop.body "
1246 " %iv = phi i32 [ %iv.next, %loop.body ], [ 0, %entry ] "
1247 " %iv.next = add nsw i32 %iv, 1 "
1248 " %cmp = icmp eq i32 %iv.next, 16 "
1249 " br i1 %cmp, label %exit, label %loop.body "
1255 runWithSE(*M
, "foo", [](Function
&F
, LoopInfo
&LI
, ScalarEvolution
&SE
) {
1256 auto *ScevIV
= SE
.getSCEV(getInstructionByName(F
, "iv")); // {0,+,1}
1257 auto *ScevI
= SE
.getSCEV(getArgByName(F
, "i"));
1258 EXPECT_EQ(SE
.getUnsignedRange(ScevIV
).getLower(), 0);
1259 EXPECT_EQ(SE
.getUnsignedRange(ScevIV
).getUpper(), 16);
1261 auto *Add
= SE
.getAddExpr(ScevI
, ScevIV
);
1262 ValueToSCEVMapTy RewriteMap
;
1263 RewriteMap
[cast
<SCEVUnknown
>(ScevI
)->getValue()] =
1264 SE
.getUMinExpr(ScevI
, SE
.getConstant(ScevI
->getType(), 17));
1265 auto *AddWithUMin
= SCEVParameterRewriter::rewrite(Add
, SE
, RewriteMap
);
1266 EXPECT_EQ(SE
.getUnsignedRange(AddWithUMin
).getLower(), 0);
1267 EXPECT_EQ(SE
.getUnsignedRange(AddWithUMin
).getUpper(), 33);
1271 TEST_F(ScalarEvolutionsTest
, SCEVgetExitLimitForGuardedLoop
) {
1274 std::unique_ptr
<Module
> M
= parseAssemblyString(
1275 "define void @foo(i32 %i) { "
1277 " %cmp3 = icmp ult i32 %i, 16 "
1278 " br i1 %cmp3, label %loop.body, label %exit "
1280 " %iv = phi i32 [ %iv.next, %loop.body ], [ %i, %entry ] "
1281 " %iv.next = add nsw i32 %iv, 1 "
1282 " %cmp = icmp eq i32 %iv.next, 16 "
1283 " br i1 %cmp, label %exit, label %loop.body "
1289 ASSERT_TRUE(M
&& "Could not parse module?");
1290 ASSERT_TRUE(!verifyModule(*M
) && "Must have been well formed!");
1292 runWithSE(*M
, "foo", [](Function
&F
, LoopInfo
&LI
, ScalarEvolution
&SE
) {
1293 auto *ScevIV
= SE
.getSCEV(getInstructionByName(F
, "iv")); // {0,+,1}
1294 const Loop
*L
= cast
<SCEVAddRecExpr
>(ScevIV
)->getLoop();
1296 const SCEV
*BTC
= SE
.getBackedgeTakenCount(L
);
1297 EXPECT_FALSE(isa
<SCEVConstant
>(BTC
));
1298 const SCEV
*MaxBTC
= SE
.getConstantMaxBackedgeTakenCount(L
);
1299 EXPECT_EQ(cast
<SCEVConstant
>(MaxBTC
)->getAPInt(), 15);
1303 TEST_F(ScalarEvolutionsTest
, ImpliedViaAddRecStart
) {
1306 std::unique_ptr
<Module
> M
= parseAssemblyString(
1307 "define void @foo(i32* %p) { "
1309 " %x = load i32, i32* %p, !range !0 "
1312 " %iv = phi i32 [ %x, %entry], [%iv.next, %backedge] "
1313 " %ne.check = icmp ne i32 %iv, 0 "
1314 " br i1 %ne.check, label %backedge, label %exit "
1316 " %iv.next = add i32 %iv, -1 "
1321 "!0 = !{i32 0, i32 2147483647}",
1324 ASSERT_TRUE(M
&& "Could not parse module?");
1325 ASSERT_TRUE(!verifyModule(*M
) && "Must have been well formed!");
1327 runWithSE(*M
, "foo", [](Function
&F
, LoopInfo
&LI
, ScalarEvolution
&SE
) {
1328 auto *X
= SE
.getSCEV(getInstructionByName(F
, "x"));
1329 auto *Context
= getInstructionByName(F
, "iv.next");
1330 EXPECT_TRUE(SE
.isKnownPredicateAt(ICmpInst::ICMP_NE
, X
,
1331 SE
.getZero(X
->getType()), Context
));
1335 TEST_F(ScalarEvolutionsTest
, UnsignedIsImpliedViaOperations
) {
1338 std::unique_ptr
<Module
> M
=
1339 parseAssemblyString("define void @foo(i32* %p1, i32* %p2) { "
1341 " %x = load i32, i32* %p1, !range !0 "
1342 " %cond = icmp ne i32 %x, 0 "
1343 " br i1 %cond, label %guarded, label %exit "
1345 " %y = add i32 %x, -1 "
1350 "!0 = !{i32 0, i32 2147483647}",
1353 ASSERT_TRUE(M
&& "Could not parse module?");
1354 ASSERT_TRUE(!verifyModule(*M
) && "Must have been well formed!");
1356 runWithSE(*M
, "foo", [](Function
&F
, LoopInfo
&LI
, ScalarEvolution
&SE
) {
1357 auto *X
= SE
.getSCEV(getInstructionByName(F
, "x"));
1358 auto *Y
= SE
.getSCEV(getInstructionByName(F
, "y"));
1359 auto *Guarded
= getInstructionByName(F
, "y")->getParent();
1360 ASSERT_TRUE(Guarded
);
1362 SE
.isBasicBlockEntryGuardedByCond(Guarded
, ICmpInst::ICMP_ULT
, Y
, X
));
1364 SE
.isBasicBlockEntryGuardedByCond(Guarded
, ICmpInst::ICMP_UGT
, X
, Y
));
1368 TEST_F(ScalarEvolutionsTest
, ProveImplicationViaNarrowing
) {
1371 std::unique_ptr
<Module
> M
= parseAssemblyString(
1372 "define i32 @foo(i32 %start, i32* %q) { "
1374 " %wide.start = zext i32 %start to i64 "
1377 " %wide.iv = phi i64 [%wide.start, %entry], [%wide.iv.next, %backedge] "
1378 " %iv = phi i32 [%start, %entry], [%iv.next, %backedge] "
1379 " %cond = icmp eq i64 %wide.iv, 0 "
1380 " br i1 %cond, label %exit, label %backedge "
1382 " %iv.next = add i32 %iv, -1 "
1383 " %index = zext i32 %iv.next to i64 "
1384 " %load.addr = getelementptr i32, i32* %q, i64 %index "
1385 " %stop = load i32, i32* %load.addr "
1386 " %loop.cond = icmp eq i32 %stop, 0 "
1387 " %wide.iv.next = add nsw i64 %wide.iv, -1 "
1388 " br i1 %loop.cond, label %loop, label %failure "
1396 ASSERT_TRUE(M
&& "Could not parse module?");
1397 ASSERT_TRUE(!verifyModule(*M
) && "Must have been well formed!");
1399 runWithSE(*M
, "foo", [](Function
&F
, LoopInfo
&LI
, ScalarEvolution
&SE
) {
1400 auto *IV
= SE
.getSCEV(getInstructionByName(F
, "iv"));
1401 auto *Zero
= SE
.getZero(IV
->getType());
1402 auto *Backedge
= getInstructionByName(F
, "iv.next")->getParent();
1403 ASSERT_TRUE(Backedge
);
1406 // FIXME: This can only be proved with turned on option
1407 // scalar-evolution-use-expensive-range-sharpening which is currently off.
1408 // Enable the check once it's switched true by default.
1409 // EXPECT_TRUE(SE.isBasicBlockEntryGuardedByCond(Backedge,
1410 // ICmpInst::ICMP_UGT,
1415 TEST_F(ScalarEvolutionsTest
, ImpliedCond
) {
1418 std::unique_ptr
<Module
> M
= parseAssemblyString(
1419 "define void @foo(i32 %len) { "
1423 " %iv = phi i32 [ 0, %entry], [%iv.next, %loop] "
1424 " %iv.next = add nsw i32 %iv, 1 "
1425 " %cmp = icmp slt i32 %iv, %len "
1426 " br i1 %cmp, label %loop, label %exit "
1432 ASSERT_TRUE(M
&& "Could not parse module?");
1433 ASSERT_TRUE(!verifyModule(*M
) && "Must have been well formed!");
1435 runWithSE(*M
, "foo", [](Function
&F
, LoopInfo
&LI
, ScalarEvolution
&SE
) {
1436 Instruction
*IV
= getInstructionByName(F
, "iv");
1437 Type
*Ty
= IV
->getType();
1438 const SCEV
*Zero
= SE
.getZero(Ty
);
1439 const SCEV
*MinusOne
= SE
.getMinusOne(Ty
);
1440 // {0,+,1}<nuw><nsw>
1441 const SCEV
*AddRec_0_1
= SE
.getSCEV(IV
);
1443 const SCEV
*AddRec_0_N1
= SE
.getNegativeSCEV(AddRec_0_1
);
1445 // {0,+,1}<nuw><nsw> > 0 -> {0,+,-1}<nw> < 0
1446 EXPECT_TRUE(isImpliedCond(SE
, ICmpInst::ICMP_SLT
, AddRec_0_N1
, Zero
,
1447 ICmpInst::ICMP_SGT
, AddRec_0_1
, Zero
));
1448 // {0,+,-1}<nw> < -1 -> {0,+,1}<nuw><nsw> > 0
1449 EXPECT_TRUE(isImpliedCond(SE
, ICmpInst::ICMP_SGT
, AddRec_0_1
, Zero
,
1450 ICmpInst::ICMP_SLT
, AddRec_0_N1
, MinusOne
));
1454 TEST_F(ScalarEvolutionsTest
, MatchURem
) {
1457 std::unique_ptr
<Module
> M
= parseAssemblyString(
1458 "target datalayout = \"e-m:e-p:32:32-f64:32:64-f80:32-n8:16:32-S128\" "
1460 "define void @test(i32 %a, i32 %b, i16 %c, i64 %d) {"
1462 " %rem1 = urem i32 %a, 2"
1463 " %rem2 = urem i32 %a, 5"
1464 " %rem3 = urem i32 %a, %b"
1465 " %c.ext = zext i16 %c to i32"
1466 " %rem4 = urem i32 %c.ext, 2"
1467 " %ext = zext i32 %rem4 to i64"
1468 " %rem5 = urem i64 %d, 17179869184"
1473 assert(M
&& "Could not parse module?");
1474 assert(!verifyModule(*M
) && "Must have been well formed!");
1476 runWithSE(*M
, "test", [&](Function
&F
, LoopInfo
&LI
, ScalarEvolution
&SE
) {
1477 for (auto *N
: {"rem1", "rem2", "rem3", "rem5"}) {
1478 auto *URemI
= getInstructionByName(F
, N
);
1479 auto *S
= SE
.getSCEV(URemI
);
1480 const SCEV
*LHS
, *RHS
;
1481 EXPECT_TRUE(matchURem(SE
, S
, LHS
, RHS
));
1482 EXPECT_EQ(LHS
, SE
.getSCEV(URemI
->getOperand(0)));
1483 EXPECT_EQ(RHS
, SE
.getSCEV(URemI
->getOperand(1)));
1484 EXPECT_EQ(LHS
->getType(), S
->getType());
1485 EXPECT_EQ(RHS
->getType(), S
->getType());
1488 // Check the case where the urem operand is zero-extended. Make sure the
1489 // match results are extended to the size of the input expression.
1490 auto *Ext
= getInstructionByName(F
, "ext");
1491 auto *URem1
= getInstructionByName(F
, "rem4");
1492 auto *S
= SE
.getSCEV(Ext
);
1493 const SCEV
*LHS
, *RHS
;
1494 EXPECT_TRUE(matchURem(SE
, S
, LHS
, RHS
));
1495 EXPECT_NE(LHS
, SE
.getSCEV(URem1
->getOperand(0)));
1496 // RHS and URem1->getOperand(1) have different widths, so compare the
1498 EXPECT_EQ(cast
<SCEVConstant
>(RHS
)->getValue()->getZExtValue(),
1499 cast
<SCEVConstant
>(SE
.getSCEV(URem1
->getOperand(1)))
1502 EXPECT_EQ(LHS
->getType(), S
->getType());
1503 EXPECT_EQ(RHS
->getType(), S
->getType());
1507 TEST_F(ScalarEvolutionsTest
, SCEVUDivFloorCeiling
) {
1510 std::unique_ptr
<Module
> M
= parseAssemblyString("define void @foo() { "
1515 ASSERT_TRUE(M
&& "Could not parse module?");
1516 ASSERT_TRUE(!verifyModule(*M
) && "Must have been well formed!");
1518 runWithSE(*M
, "foo", [](Function
&F
, LoopInfo
&LI
, ScalarEvolution
&SE
) {
1519 // Check that SCEV's udiv and uceil handling produce the correct results
1520 // for all 8 bit options. Div-by-zero is deliberately excluded.
1521 for (unsigned N
= 0; N
< 256; N
++)
1522 for (unsigned D
= 1; D
< 256; D
++) {
1525 using namespace llvm::APIntOps
;
1526 APInt FloorInt
= RoundingUDiv(NInt
, DInt
, APInt::Rounding::DOWN
);
1527 APInt CeilingInt
= RoundingUDiv(NInt
, DInt
, APInt::Rounding::UP
);
1528 auto *NS
= SE
.getConstant(NInt
);
1529 auto *DS
= SE
.getConstant(DInt
);
1530 auto *FloorS
= cast
<SCEVConstant
>(SE
.getUDivExpr(NS
, DS
));
1531 auto *CeilingS
= cast
<SCEVConstant
>(SE
.getUDivCeilSCEV(NS
, DS
));
1532 ASSERT_TRUE(FloorS
->getAPInt() == FloorInt
);
1533 ASSERT_TRUE(CeilingS
->getAPInt() == CeilingInt
);
1538 TEST_F(ScalarEvolutionsTest
, CheckGetPowerOfTwo
) {
1539 Module
M("CheckGetPowerOfTwo", Context
);
1540 FunctionType
*FTy
= FunctionType::get(Type::getVoidTy(Context
), {}, false);
1541 Function
*F
= Function::Create(FTy
, Function::ExternalLinkage
, "foo", M
);
1542 BasicBlock
*Entry
= BasicBlock::Create(Context
, "entry", F
);
1543 IRBuilder
<> Builder(Entry
);
1544 Builder
.CreateRetVoid();
1545 ScalarEvolution SE
= buildSE(*F
);
1547 for (unsigned short i
= 0; i
< 64; ++i
)
1549 dyn_cast
<SCEVConstant
>(SE
.getPowerOfTwo(Type::getInt64Ty(Context
), i
))
1551 ->equalsInt(1ULL << i
));
1554 TEST_F(ScalarEvolutionsTest
, ApplyLoopGuards
) {
1557 std::unique_ptr
<Module
> M
= parseAssemblyString(
1558 "declare void @llvm.assume(i1)\n"
1559 "define void @test(i32 %num) {\n"
1561 " %u = urem i32 %num, 4\n"
1562 " %cmp = icmp eq i32 %u, 0\n"
1563 " tail call void @llvm.assume(i1 %cmp)\n"
1564 " %cmp.1 = icmp ugt i32 %num, 0\n"
1565 " tail call void @llvm.assume(i1 %cmp.1)\n"
1566 " br label %for.body\n"
1568 " %i.010 = phi i32 [ 0, %entry ], [ %inc, %for.body ]\n"
1569 " %inc = add nuw nsw i32 %i.010, 1\n"
1570 " %cmp2 = icmp ult i32 %inc, %num\n"
1571 " br i1 %cmp2, label %for.body, label %exit\n"
1577 ASSERT_TRUE(M
&& "Could not parse module?");
1578 ASSERT_TRUE(!verifyModule(*M
) && "Must have been well formed!");
1580 runWithSE(*M
, "test", [](Function
&F
, LoopInfo
&LI
, ScalarEvolution
&SE
) {
1581 auto *TCScev
= SE
.getSCEV(getArgByName(F
, "num"));
1582 auto *ApplyLoopGuardsTC
= SE
.applyLoopGuards(TCScev
, *LI
.begin());
1583 // Assert that the new TC is (4 * ((4 umax %num) /u 4))
1585 auto *Constant4
= SE
.getConstant(Four
);
1586 auto *Max
= SE
.getUMaxExpr(TCScev
, Constant4
);
1587 auto *Mul
= SE
.getMulExpr(SE
.getUDivExpr(Max
, Constant4
), Constant4
);
1588 ASSERT_TRUE(Mul
== ApplyLoopGuardsTC
);
1592 TEST_F(ScalarEvolutionsTest
, ForgetValueWithOverflowInst
) {
1595 std::unique_ptr
<Module
> M
= parseAssemblyString(
1596 "declare { i32, i1 } @llvm.smul.with.overflow.i32(i32, i32) "
1597 "define void @foo(i32 %i) { "
1599 " br label %loop.body "
1601 " %iv = phi i32 [ %iv.next, %loop.body ], [ 0, %entry ] "
1602 " %iv.next = add nsw i32 %iv, 1 "
1603 " %call = call {i32, i1} @llvm.smul.with.overflow.i32(i32 %iv, i32 -2) "
1604 " %extractvalue = extractvalue {i32, i1} %call, 0 "
1605 " %cmp = icmp eq i32 %iv.next, 16 "
1606 " br i1 %cmp, label %exit, label %loop.body "
1612 ASSERT_TRUE(M
&& "Could not parse module?");
1613 ASSERT_TRUE(!verifyModule(*M
) && "Must have been well formed!");
1615 runWithSE(*M
, "foo", [](Function
&F
, LoopInfo
&LI
, ScalarEvolution
&SE
) {
1616 auto *ExtractValue
= getInstructionByName(F
, "extractvalue");
1617 auto *IV
= getInstructionByName(F
, "iv");
1619 auto *ExtractValueScev
= SE
.getSCEV(ExtractValue
);
1620 EXPECT_NE(ExtractValueScev
, nullptr);
1623 auto *ExtractValueScevForgotten
= SE
.getExistingSCEV(ExtractValue
);
1624 EXPECT_EQ(ExtractValueScevForgotten
, nullptr);
1628 } // end namespace llvm