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/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"
30 // We use this fixture to ensure that we clean up ScalarEvolution before
31 // deleting the PassManager.
32 class ScalarEvolutionsTest
: public testing::Test
{
36 TargetLibraryInfoImpl TLII
;
37 TargetLibraryInfo TLI
;
39 std::unique_ptr
<AssumptionCache
> AC
;
40 std::unique_ptr
<DominatorTree
> DT
;
41 std::unique_ptr
<LoopInfo
> LI
;
43 ScalarEvolutionsTest() : M("", Context
), TLII(), TLI(TLII
) {}
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
);
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
);
61 static Optional
<APInt
> computeConstantDifference(ScalarEvolution
&SE
,
64 return SE
.computeConstantDifference(LHS
, RHS
);
67 static bool matchURem(ScalarEvolution
&SE
, const SCEV
*Expr
, const SCEV
*&LHS
,
69 return SE
.matchURem(Expr
, LHS
, RHS
);
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
);
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
);
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");
93 ScalarEvolution SE
= buildSE(*F
);
95 const SCEV
*S0
= SE
.getSCEV(V0
);
96 const SCEV
*S1
= SE
.getSCEV(V1
);
97 const SCEV
*S2
= SE
.getSCEV(V2
);
99 const SCEV
*P0
= SE
.getAddExpr(S0
, SE
.getConstant(S0
->getType(), 2));
100 const SCEV
*P1
= SE
.getAddExpr(S1
, SE
.getConstant(S0
->getType(), 2));
101 const SCEV
*P2
= SE
.getAddExpr(S2
, SE
.getConstant(S0
->getType(), 2));
103 auto *M0
= cast
<SCEVAddExpr
>(P0
);
104 auto *M1
= cast
<SCEVAddExpr
>(P1
);
105 auto *M2
= cast
<SCEVAddExpr
>(P2
);
107 EXPECT_EQ(cast
<SCEVConstant
>(M0
->getOperand(0))->getValue()->getZExtValue(),
109 EXPECT_EQ(cast
<SCEVConstant
>(M1
->getOperand(0))->getValue()->getZExtValue(),
111 EXPECT_EQ(cast
<SCEVConstant
>(M2
->getOperand(0))->getValue()->getZExtValue(),
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
);
120 V2
->replaceAllUsesWith(V1
);
121 V1
->replaceAllUsesWith(V0
);
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
);
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
)),
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);
149 // At some point, only the first call to getSCEV returned the simplified
150 // SCEVConstant and later calls just returned a SCEVUnknown referencing the
152 EXPECT_EQ(S1
, ZeroConst
);
157 static Instruction
*getInstructionByName(Function
&F
, StringRef Name
) {
158 for (auto &I
: instructions(F
))
159 if (I
.getName() == Name
)
161 llvm_unreachable("Expected to find instruction!");
164 static Value
*getArgByName(Function
&F
, StringRef Name
) {
165 for (auto &Arg
: F
.args())
166 if (Arg
.getName() == Name
)
168 llvm_unreachable("Expected to find instruction!");
170 TEST_F(ScalarEvolutionsTest
, CommutativeExprOperandOrder
) {
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\" "
176 "@var_0 = external global i32, align 4"
177 "@var_1 = external global i32, align 4"
178 "@var_2 = external global i32, align 4"
180 "declare i32 @unknown(i32, i32, i32)"
182 "define void @f_1(i8* nocapture %arr, i32 %n, i32* %A, i32* %B) "
183 " local_unnamed_addr { "
185 " %entrycond = icmp sgt i32 %n, 0 "
186 " br i1 %entrycond, label %loop.ph, label %for.end "
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 "
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 "
206 " br label %for.end "
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 "
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"
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)"
235 assert(M
&& "Could not parse module?");
236 assert(!verifyModule(*M
) && "Must have been well formed!");
238 runWithSE(*M
, "f_1", [&](Function
&F
, LoopInfo
&LI
, ScalarEvolution
&SE
) {
239 auto *IV0
= getInstructionByName(F
, "iv0");
240 auto *IV0Inc
= getInstructionByName(F
, "iv0.inc");
242 auto *FirstExprForIV0
= SE
.getSCEV(IV0
);
243 auto *FirstExprForIV0Inc
= SE
.getSCEV(IV0Inc
);
244 auto *SecondExprForIV0
= SE
.getSCEV(IV0
);
246 EXPECT_TRUE(isa
<SCEVAddRecExpr
>(FirstExprForIV0
));
247 EXPECT_TRUE(isa
<SCEVAddRecExpr
>(FirstExprForIV0Inc
));
248 EXPECT_TRUE(isa
<SCEVAddRecExpr
>(SecondExprForIV0
));
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
));
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
};
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
);
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
;
278 for (StringRef FuncName
: {"f_2", "f_3", "f_4"})
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")));
287 TEST_F(ScalarEvolutionsTest
, CompareSCEVComplexity
) {
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
);
295 auto *Ty
= Type::getInt32Ty(Context
);
296 SmallVector
<Instruction
*, 8> Muls(8), Acc(8), NextAcc(8);
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
);
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
);
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
);
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
);
335 BasicBlock
*ExitBB
= BasicBlock::Create(Context
, "bb2", F
);
336 BranchInst::Create(LoopBB
, ExitBB
, UndefValue::get(Type::getInt1Ty(Context
)),
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
);
347 ReturnInst::Create(Context
, nullptr, ExitBB
);
349 ScalarEvolution SE
= buildSE(*F
);
351 EXPECT_NE(nullptr, SE
.getSCEV(Acc
[0]));
354 TEST_F(ScalarEvolutionsTest
, CompareValueComplexity
) {
355 IntegerType
*IntPtrTy
= M
.getDataLayout().getIntPtrType(Context
);
356 PointerType
*IntPtrPtrTy
= IntPtrTy
->getPointerTo();
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
);
363 Value
*X
= &*F
->arg_begin();
364 Value
*Y
= &*std::next(F
->arg_begin());
366 const int ValueDepth
= 10;
367 for (int i
= 0; i
< ValueDepth
; i
++) {
368 X
= new LoadInst(IntPtrTy
, new IntToPtrInst(X
, IntPtrPtrTy
, "", EntryBB
),
370 /*isVolatile*/ false, EntryBB
);
371 Y
= new LoadInst(IntPtrTy
, new IntToPtrInst(Y
, IntPtrPtrTy
, "", EntryBB
),
373 /*isVolatile*/ false, EntryBB
);
376 auto *MulA
= BinaryOperator::CreateMul(X
, Y
, "", EntryBB
);
377 auto *MulB
= BinaryOperator::CreateMul(Y
, X
, "", EntryBB
);
378 ReturnInst::Create(Context
, nullptr, EntryBB
);
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.
386 ScalarEvolution SE
= buildSE(*F
);
387 auto *A
= SE
.getSCEV(MulA
);
388 auto *B
= SE
.getSCEV(MulB
);
392 TEST_F(ScalarEvolutionsTest
, SCEVAddExpr
) {
393 Type
*Ty32
= Type::getInt32Ty(Context
);
394 Type
*ArgTys
[] = {Type::getInt64Ty(Context
), Ty32
, Ty32
, Ty32
, Ty32
, Ty32
};
397 FunctionType::get(Type::getVoidTy(Context
), ArgTys
, false);
398 Function
*F
= Function::Create(FTy
, Function::ExternalLinkage
, "f", M
);
400 Argument
*A1
= &*F
->arg_begin();
401 Argument
*A2
= &*(std::next(F
->arg_begin()));
402 BasicBlock
*EntryBB
= BasicBlock::Create(Context
, "entry", F
);
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
413 for (int i
= 0; i
< 10; i
++) {
414 Mul1
= BinaryOperator::CreateMul(Add2
, Add1
, "", EntryBB
);
416 Add2
= BinaryOperator::CreateAdd(Mul1
, Add1
, "", EntryBB
);
419 ReturnInst::Create(Context
, nullptr, EntryBB
);
420 ScalarEvolution SE
= buildSE(*F
);
421 EXPECT_NE(nullptr, SE
.getSCEV(Mul1
));
423 Argument
*A3
= &*(std::next(F
->arg_begin(), 2));
424 Argument
*A4
= &*(std::next(F
->arg_begin(), 3));
425 Argument
*A5
= &*(std::next(F
->arg_begin(), 4));
426 Argument
*A6
= &*(std::next(F
->arg_begin(), 5));
428 auto *AddWithNUW
= cast
<SCEVAddExpr
>(SE
.getAddExpr(
429 SE
.getAddExpr(SE
.getSCEV(A2
), SE
.getSCEV(A3
), SCEV::FlagNUW
),
430 SE
.getConstant(APInt(/*numBits=*/32, 5)), SCEV::FlagNUW
));
431 EXPECT_EQ(AddWithNUW
->getNumOperands(), 3u);
432 EXPECT_EQ(AddWithNUW
->getNoWrapFlags(), SCEV::FlagNUW
);
434 auto *AddWithAnyWrap
=
435 SE
.getAddExpr(SE
.getSCEV(A3
), SE
.getSCEV(A4
), SCEV::FlagAnyWrap
);
436 auto *AddWithAnyWrapNUW
= cast
<SCEVAddExpr
>(
437 SE
.getAddExpr(AddWithAnyWrap
, SE
.getSCEV(A5
), SCEV::FlagNUW
));
438 EXPECT_EQ(AddWithAnyWrapNUW
->getNumOperands(), 3u);
439 EXPECT_EQ(AddWithAnyWrapNUW
->getNoWrapFlags(), SCEV::FlagAnyWrap
);
441 auto *AddWithNSW
= SE
.getAddExpr(
442 SE
.getSCEV(A2
), SE
.getConstant(APInt(32, 99)), SCEV::FlagNSW
);
443 auto *AddWithNSW_NUW
= cast
<SCEVAddExpr
>(
444 SE
.getAddExpr(AddWithNSW
, SE
.getSCEV(A5
), SCEV::FlagNUW
));
445 EXPECT_EQ(AddWithNSW_NUW
->getNumOperands(), 3u);
446 EXPECT_EQ(AddWithNSW_NUW
->getNoWrapFlags(), SCEV::FlagAnyWrap
);
448 auto *AddWithNSWNUW
=
449 SE
.getAddExpr(SE
.getSCEV(A2
), SE
.getSCEV(A4
),
450 ScalarEvolution::setFlags(SCEV::FlagNUW
, SCEV::FlagNSW
));
451 auto *AddWithNSWNUW_NUW
= cast
<SCEVAddExpr
>(
452 SE
.getAddExpr(AddWithNSWNUW
, SE
.getSCEV(A5
), SCEV::FlagNUW
));
453 EXPECT_EQ(AddWithNSWNUW_NUW
->getNumOperands(), 3u);
454 EXPECT_EQ(AddWithNSWNUW_NUW
->getNoWrapFlags(), SCEV::FlagNUW
);
456 auto *AddWithNSW_NSWNUW
= cast
<SCEVAddExpr
>(
457 SE
.getAddExpr(AddWithNSW
, SE
.getSCEV(A6
),
458 ScalarEvolution::setFlags(SCEV::FlagNUW
, SCEV::FlagNSW
)));
459 EXPECT_EQ(AddWithNSW_NSWNUW
->getNumOperands(), 3u);
460 EXPECT_EQ(AddWithNSW_NSWNUW
->getNoWrapFlags(), SCEV::FlagAnyWrap
);
463 static Instruction
&GetInstByName(Function
&F
, StringRef Name
) {
464 for (auto &I
: instructions(F
))
465 if (I
.getName() == Name
)
467 llvm_unreachable("Could not find instructions!");
470 TEST_F(ScalarEvolutionsTest
, SCEVNormalization
) {
473 std::unique_ptr
<Module
> M
= parseAssemblyString(
474 "target datalayout = \"e-m:e-p:32:32-f64:32:64-f80:32-n8:16:32-S128\" "
476 "@var_0 = external global i32, align 4"
477 "@var_1 = external global i32, align 4"
478 "@var_2 = external global i32, align 4"
480 "declare i32 @unknown(i32, i32, i32)"
482 "define void @f_1(i8* nocapture %arr, i32 %n, i32* %A, i32* %B) "
483 " local_unnamed_addr { "
485 " br label %loop.ph "
491 " %iv0 = phi i32 [ %iv0.inc, %loop ], [ 0, %loop.ph ] "
492 " %iv1 = phi i32 [ %iv1.inc, %loop ], [ -2147483648, %loop.ph ] "
493 " %iv0.inc = add i32 %iv0, 1 "
494 " %iv1.inc = add i32 %iv1, 3 "
495 " br i1 undef, label %for.end.loopexit, label %loop "
501 "define void @f_2(i32 %a, i32 %b, i32 %c, i32 %d) "
502 " local_unnamed_addr { "
507 " br i1 undef, label %loop_0, label %loop_1 "
510 " br i1 undef, label %loop_2, label %loop_1 "
514 " br i1 undef, label %end, label %loop_2 "
522 assert(M
&& "Could not parse module?");
523 assert(!verifyModule(*M
) && "Must have been well formed!");
525 runWithSE(*M
, "f_1", [&](Function
&F
, LoopInfo
&LI
, ScalarEvolution
&SE
) {
526 auto &I0
= GetInstByName(F
, "iv0");
527 auto &I1
= *I0
.getNextNode();
529 auto *S0
= cast
<SCEVAddRecExpr
>(SE
.getSCEV(&I0
));
530 PostIncLoopSet Loops
;
531 Loops
.insert(S0
->getLoop());
532 auto *N0
= normalizeForPostIncUse(S0
, Loops
, SE
);
533 auto *D0
= denormalizeForPostIncUse(N0
, Loops
, SE
);
534 EXPECT_EQ(S0
, D0
) << *S0
<< " " << *D0
;
536 auto *S1
= cast
<SCEVAddRecExpr
>(SE
.getSCEV(&I1
));
538 Loops
.insert(S1
->getLoop());
539 auto *N1
= normalizeForPostIncUse(S1
, Loops
, SE
);
540 auto *D1
= denormalizeForPostIncUse(N1
, Loops
, SE
);
541 EXPECT_EQ(S1
, D1
) << *S1
<< " " << *D1
;
544 runWithSE(*M
, "f_2", [&](Function
&F
, LoopInfo
&LI
, ScalarEvolution
&SE
) {
545 auto *L2
= *LI
.begin();
546 auto *L1
= *std::next(LI
.begin());
547 auto *L0
= *std::next(LI
.begin(), 2);
549 auto GetAddRec
= [&SE
](const Loop
*L
, std::initializer_list
<const SCEV
*> Ops
) {
550 SmallVector
<const SCEV
*, 4> OpsCopy(Ops
);
551 return SE
.getAddRecExpr(OpsCopy
, L
, SCEV::FlagAnyWrap
);
554 auto GetAdd
= [&SE
](std::initializer_list
<const SCEV
*> Ops
) {
555 SmallVector
<const SCEV
*, 4> OpsCopy(Ops
);
556 return SE
.getAddExpr(OpsCopy
, SCEV::FlagAnyWrap
);
559 // We first populate the AddRecs vector with a few "interesting" SCEV
560 // expressions, and then we go through the list and assert that each
561 // expression in it has an invertible normalization.
563 std::vector
<const SCEV
*> Exprs
;
565 const SCEV
*V0
= SE
.getSCEV(&*F
.arg_begin());
566 const SCEV
*V1
= SE
.getSCEV(&*std::next(F
.arg_begin(), 1));
567 const SCEV
*V2
= SE
.getSCEV(&*std::next(F
.arg_begin(), 2));
568 const SCEV
*V3
= SE
.getSCEV(&*std::next(F
.arg_begin(), 3));
570 Exprs
.push_back(GetAddRec(L0
, {V0
})); // 0
571 Exprs
.push_back(GetAddRec(L0
, {V0
, V1
})); // 1
572 Exprs
.push_back(GetAddRec(L0
, {V0
, V1
, V2
})); // 2
573 Exprs
.push_back(GetAddRec(L0
, {V0
, V1
, V2
, V3
})); // 3
576 GetAddRec(L1
, {Exprs
[1], Exprs
[2], Exprs
[3], Exprs
[0]})); // 4
578 GetAddRec(L1
, {Exprs
[1], Exprs
[2], Exprs
[0], Exprs
[3]})); // 5
580 GetAddRec(L1
, {Exprs
[1], Exprs
[3], Exprs
[3], Exprs
[1]})); // 6
582 Exprs
.push_back(GetAdd({Exprs
[6], Exprs
[3], V2
})); // 7
585 GetAddRec(L2
, {Exprs
[4], Exprs
[3], Exprs
[3], Exprs
[5]})); // 8
588 GetAddRec(L2
, {Exprs
[4], Exprs
[6], Exprs
[7], Exprs
[3], V0
})); // 9
591 std::vector
<PostIncLoopSet
> LoopSets
;
592 for (int i
= 0; i
< 8; i
++) {
593 LoopSets
.emplace_back();
595 LoopSets
.back().insert(L0
);
597 LoopSets
.back().insert(L1
);
599 LoopSets
.back().insert(L2
);
602 for (const auto &LoopSet
: LoopSets
)
603 for (auto *S
: Exprs
) {
605 auto *N
= llvm::normalizeForPostIncUse(S
, LoopSet
, SE
);
606 auto *D
= llvm::denormalizeForPostIncUse(N
, LoopSet
, SE
);
608 // Normalization and then denormalizing better give us back the same
610 EXPECT_EQ(S
, D
) << "S = " << *S
<< " D = " << *D
<< " N = " << *N
;
613 auto *D
= llvm::denormalizeForPostIncUse(S
, LoopSet
, SE
);
614 auto *N
= llvm::normalizeForPostIncUse(D
, LoopSet
, SE
);
616 // Denormalization and then normalizing better give us back the same
618 EXPECT_EQ(S
, N
) << "S = " << *S
<< " N = " << *N
;
624 // Expect the call of getZeroExtendExpr will not cost exponential time.
625 TEST_F(ScalarEvolutionsTest
, SCEVZeroExtendExpr
) {
629 // Generate a function like below:
630 // define void @foo() {
632 // br label %for.cond
635 // %0 = phi i64 [ 100, %entry ], [ %dec, %for.inc ]
636 // %cmp = icmp sgt i64 %0, 90
637 // br i1 %cmp, label %for.inc, label %for.cond1
640 // %dec = add nsw i64 %0, -1
641 // br label %for.cond
644 // %1 = phi i64 [ 100, %for.cond ], [ %dec5, %for.inc2 ]
645 // %cmp3 = icmp sgt i64 %1, 90
646 // br i1 %cmp3, label %for.inc2, label %for.cond4
649 // %dec5 = add nsw i64 %1, -1
650 // br label %for.cond1
655 // %19 = phi i64 [ 100, %for.cond84 ], [ %dec94, %for.inc92 ]
656 // %cmp93 = icmp sgt i64 %19, 90
657 // br i1 %cmp93, label %for.inc92, label %for.end
660 // %dec94 = add nsw i64 %19, -1
661 // br label %for.cond89
664 // %gep = getelementptr i8, i8* null, i64 %dec
665 // %gep6 = getelementptr i8, i8* %gep, i64 %dec5
667 // %gep95 = getelementptr i8, i8* %gep91, i64 %dec94
670 FunctionType
*FTy
= FunctionType::get(Type::getVoidTy(Context
), {}, false);
671 Function
*F
= Function::Create(FTy
, Function::ExternalLinkage
, "foo", M
);
673 BasicBlock
*EntryBB
= BasicBlock::Create(Context
, "entry", F
);
674 BasicBlock
*CondBB
= BasicBlock::Create(Context
, "for.cond", F
);
675 BasicBlock
*EndBB
= BasicBlock::Create(Context
, "for.end", F
);
676 BranchInst::Create(CondBB
, EntryBB
);
677 BasicBlock
*PrevBB
= EntryBB
;
679 Type
*I64Ty
= Type::getInt64Ty(Context
);
680 Type
*I8Ty
= Type::getInt8Ty(Context
);
681 Type
*I8PtrTy
= Type::getInt8PtrTy(Context
);
682 Value
*Accum
= Constant::getNullValue(I8PtrTy
);
684 for (int i
= 0; i
< Iters
; i
++) {
685 BasicBlock
*IncBB
= BasicBlock::Create(Context
, "for.inc", F
, EndBB
);
686 auto *PN
= PHINode::Create(I64Ty
, 2, "", CondBB
);
687 PN
->addIncoming(ConstantInt::get(Context
, APInt(64, 100)), PrevBB
);
688 auto *Cmp
= CmpInst::Create(Instruction::ICmp
, CmpInst::ICMP_SGT
, PN
,
689 ConstantInt::get(Context
, APInt(64, 90)), "cmp",
693 NextBB
= BasicBlock::Create(Context
, "for.cond", F
, EndBB
);
696 BranchInst::Create(IncBB
, NextBB
, Cmp
, CondBB
);
697 auto *Dec
= BinaryOperator::CreateNSWAdd(
698 PN
, ConstantInt::get(Context
, APInt(64, -1)), "dec", IncBB
);
699 PN
->addIncoming(Dec
, IncBB
);
700 BranchInst::Create(CondBB
, IncBB
);
702 Accum
= GetElementPtrInst::Create(I8Ty
, Accum
, PN
, "gep", EndBB
);
707 ReturnInst::Create(Context
, nullptr, EndBB
);
708 ScalarEvolution SE
= buildSE(*F
);
709 const SCEV
*S
= SE
.getSCEV(Accum
);
710 S
= SE
.getLosslessPtrToIntExpr(S
);
711 Type
*I128Ty
= Type::getInt128Ty(Context
);
712 SE
.getZeroExtendExpr(S
, I128Ty
);
715 // Make sure that SCEV invalidates exit limits after invalidating the values it
716 // depends on when we forget a loop.
717 TEST_F(ScalarEvolutionsTest
, SCEVExitLimitForgetLoop
) {
719 * Create the following code:
720 * func(i64 addrspace(10)* %arg)
726 * %phi = phi i64 [i64 0, %L.ph], [ %add, %L2 ]
727 * %add = add i64 %phi2, 1
728 * %cond = icmp slt i64 %add, 1000; then becomes 2000.
729 * br i1 %cond, label %post, label %L2
735 // Create a module with non-integral pointers in it's datalayout
736 Module
NIM("nonintegral", Context
);
737 std::string DataLayout
= M
.getDataLayoutStr();
738 if (!DataLayout
.empty())
740 DataLayout
+= "ni:10";
741 NIM
.setDataLayout(DataLayout
);
743 Type
*T_int64
= Type::getInt64Ty(Context
);
744 Type
*T_pint64
= T_int64
->getPointerTo(10);
747 FunctionType::get(Type::getVoidTy(Context
), {T_pint64
}, false);
748 Function
*F
= Function::Create(FTy
, Function::ExternalLinkage
, "foo", NIM
);
750 BasicBlock
*Top
= BasicBlock::Create(Context
, "top", F
);
751 BasicBlock
*LPh
= BasicBlock::Create(Context
, "L.ph", F
);
752 BasicBlock
*L
= BasicBlock::Create(Context
, "L", F
);
753 BasicBlock
*Post
= BasicBlock::Create(Context
, "post", F
);
755 IRBuilder
<> Builder(Top
);
756 Builder
.CreateBr(LPh
);
758 Builder
.SetInsertPoint(LPh
);
761 Builder
.SetInsertPoint(L
);
762 PHINode
*Phi
= Builder
.CreatePHI(T_int64
, 2);
763 auto *Add
= cast
<Instruction
>(
764 Builder
.CreateAdd(Phi
, ConstantInt::get(T_int64
, 1), "add"));
765 auto *Limit
= ConstantInt::get(T_int64
, 1000);
766 auto *Cond
= cast
<Instruction
>(
767 Builder
.CreateICmp(ICmpInst::ICMP_SLT
, Add
, Limit
, "cond"));
768 auto *Br
= cast
<Instruction
>(Builder
.CreateCondBr(Cond
, L
, Post
));
769 Phi
->addIncoming(ConstantInt::get(T_int64
, 0), LPh
);
770 Phi
->addIncoming(Add
, L
);
772 Builder
.SetInsertPoint(Post
);
773 Builder
.CreateRetVoid();
775 ScalarEvolution SE
= buildSE(*F
);
776 auto *Loop
= LI
->getLoopFor(L
);
777 const SCEV
*EC
= SE
.getBackedgeTakenCount(Loop
);
778 EXPECT_FALSE(isa
<SCEVCouldNotCompute
>(EC
));
779 EXPECT_TRUE(isa
<SCEVConstant
>(EC
));
780 EXPECT_EQ(cast
<SCEVConstant
>(EC
)->getAPInt().getLimitedValue(), 999u);
782 // The add recurrence {5,+,1} does not correspond to any PHI in the IR, and
783 // that is relevant to this test.
784 auto *Five
= SE
.getConstant(APInt(/*numBits=*/64, 5));
786 SE
.getAddRecExpr(Five
, SE
.getOne(T_int64
), Loop
, SCEV::FlagAnyWrap
);
787 const SCEV
*ARAtLoopExit
= SE
.getSCEVAtScope(AR
, nullptr);
788 EXPECT_FALSE(isa
<SCEVCouldNotCompute
>(ARAtLoopExit
));
789 EXPECT_TRUE(isa
<SCEVConstant
>(ARAtLoopExit
));
790 EXPECT_EQ(cast
<SCEVConstant
>(ARAtLoopExit
)->getAPInt().getLimitedValue(),
794 Br
->eraseFromParent();
795 Cond
->eraseFromParent();
797 Builder
.SetInsertPoint(L
);
798 auto *NewCond
= Builder
.CreateICmp(
799 ICmpInst::ICMP_SLT
, Add
, ConstantInt::get(T_int64
, 2000), "new.cond");
800 Builder
.CreateCondBr(NewCond
, L
, Post
);
801 const SCEV
*NewEC
= SE
.getBackedgeTakenCount(Loop
);
802 EXPECT_FALSE(isa
<SCEVCouldNotCompute
>(NewEC
));
803 EXPECT_TRUE(isa
<SCEVConstant
>(NewEC
));
804 EXPECT_EQ(cast
<SCEVConstant
>(NewEC
)->getAPInt().getLimitedValue(), 1999u);
805 const SCEV
*NewARAtLoopExit
= SE
.getSCEVAtScope(AR
, nullptr);
806 EXPECT_FALSE(isa
<SCEVCouldNotCompute
>(NewARAtLoopExit
));
807 EXPECT_TRUE(isa
<SCEVConstant
>(NewARAtLoopExit
));
808 EXPECT_EQ(cast
<SCEVConstant
>(NewARAtLoopExit
)->getAPInt().getLimitedValue(),
812 // Make sure that SCEV invalidates exit limits after invalidating the values it
813 // depends on when we forget a value.
814 TEST_F(ScalarEvolutionsTest
, SCEVExitLimitForgetValue
) {
816 * Create the following code:
817 * func(i64 addrspace(10)* %arg)
821 * %load = load i64 addrspace(10)* %arg
824 * %phi = phi i64 [i64 0, %L.ph], [ %add, %L2 ]
825 * %add = add i64 %phi2, 1
826 * %cond = icmp slt i64 %add, %load ; then becomes 2000.
827 * br i1 %cond, label %post, label %L2
833 // Create a module with non-integral pointers in it's datalayout
834 Module
NIM("nonintegral", Context
);
835 std::string DataLayout
= M
.getDataLayoutStr();
836 if (!DataLayout
.empty())
838 DataLayout
+= "ni:10";
839 NIM
.setDataLayout(DataLayout
);
841 Type
*T_int64
= Type::getInt64Ty(Context
);
842 Type
*T_pint64
= T_int64
->getPointerTo(10);
845 FunctionType::get(Type::getVoidTy(Context
), {T_pint64
}, false);
846 Function
*F
= Function::Create(FTy
, Function::ExternalLinkage
, "foo", NIM
);
848 Argument
*Arg
= &*F
->arg_begin();
850 BasicBlock
*Top
= BasicBlock::Create(Context
, "top", F
);
851 BasicBlock
*LPh
= BasicBlock::Create(Context
, "L.ph", F
);
852 BasicBlock
*L
= BasicBlock::Create(Context
, "L", F
);
853 BasicBlock
*Post
= BasicBlock::Create(Context
, "post", F
);
855 IRBuilder
<> Builder(Top
);
856 Builder
.CreateBr(LPh
);
858 Builder
.SetInsertPoint(LPh
);
859 auto *Load
= cast
<Instruction
>(Builder
.CreateLoad(T_int64
, Arg
, "load"));
862 Builder
.SetInsertPoint(L
);
863 PHINode
*Phi
= Builder
.CreatePHI(T_int64
, 2);
864 auto *Add
= cast
<Instruction
>(
865 Builder
.CreateAdd(Phi
, ConstantInt::get(T_int64
, 1), "add"));
866 auto *Cond
= cast
<Instruction
>(
867 Builder
.CreateICmp(ICmpInst::ICMP_SLT
, Add
, Load
, "cond"));
868 auto *Br
= cast
<Instruction
>(Builder
.CreateCondBr(Cond
, L
, Post
));
869 Phi
->addIncoming(ConstantInt::get(T_int64
, 0), LPh
);
870 Phi
->addIncoming(Add
, L
);
872 Builder
.SetInsertPoint(Post
);
873 Builder
.CreateRetVoid();
875 ScalarEvolution SE
= buildSE(*F
);
876 auto *Loop
= LI
->getLoopFor(L
);
877 const SCEV
*EC
= SE
.getBackedgeTakenCount(Loop
);
878 EXPECT_FALSE(isa
<SCEVCouldNotCompute
>(EC
));
879 EXPECT_FALSE(isa
<SCEVConstant
>(EC
));
881 SE
.forgetValue(Load
);
882 Br
->eraseFromParent();
883 Cond
->eraseFromParent();
884 Load
->eraseFromParent();
886 Builder
.SetInsertPoint(L
);
887 auto *NewCond
= Builder
.CreateICmp(
888 ICmpInst::ICMP_SLT
, Add
, ConstantInt::get(T_int64
, 2000), "new.cond");
889 Builder
.CreateCondBr(NewCond
, L
, Post
);
890 const SCEV
*NewEC
= SE
.getBackedgeTakenCount(Loop
);
891 EXPECT_FALSE(isa
<SCEVCouldNotCompute
>(NewEC
));
892 EXPECT_TRUE(isa
<SCEVConstant
>(NewEC
));
893 EXPECT_EQ(cast
<SCEVConstant
>(NewEC
)->getAPInt().getLimitedValue(), 1999u);
896 TEST_F(ScalarEvolutionsTest
, SCEVAddRecFromPHIwithLargeConstants
) {
897 // Reference: https://reviews.llvm.org/D37265
898 // Make sure that SCEV does not blow up when constructing an AddRec
899 // with predicates for a phi with the update pattern:
900 // (SExt/ZExt ix (Trunc iy (%SymbolicPHI) to ix) to iy) + InvariantAccum
901 // when either the initial value of the Phi or the InvariantAccum are
902 // constants that are too large to fit in an ix but are zero when truncated to
905 FunctionType::get(Type::getVoidTy(Context
), std::vector
<Type
*>(), false);
907 Function::Create(FTy
, Function::ExternalLinkage
, "addrecphitest", M
);
914 %0 = phi i64 [-9223372036854775808, %entry], [%3, %loop]
916 %2 = ashr exact i64 %1, 32
917 %3 = add i64 %2, -9223372036854775808
918 br i1 undef, label %exit, label %loop
922 BasicBlock
*EntryBB
= BasicBlock::Create(Context
, "entry", F
);
923 BasicBlock
*LoopBB
= BasicBlock::Create(Context
, "loop", F
);
924 BasicBlock
*ExitBB
= BasicBlock::Create(Context
, "exit", F
);
927 BranchInst::Create(LoopBB
, EntryBB
);
930 ConstantInt::get(Context
, APInt(64, 0x8000000000000000U
, true));
931 auto *Int64_32
= ConstantInt::get(Context
, APInt(64, 32));
932 auto *Br
= BranchInst::Create(
933 LoopBB
, ExitBB
, UndefValue::get(Type::getInt1Ty(Context
)), LoopBB
);
934 auto *Phi
= PHINode::Create(Type::getInt64Ty(Context
), 2, "", Br
);
935 auto *Shl
= BinaryOperator::CreateShl(Phi
, Int64_32
, "", Br
);
936 auto *AShr
= BinaryOperator::CreateExactAShr(Shl
, Int64_32
, "", Br
);
937 auto *Add
= BinaryOperator::CreateAdd(AShr
, MinInt64
, "", Br
);
938 Phi
->addIncoming(MinInt64
, EntryBB
);
939 Phi
->addIncoming(Add
, LoopBB
);
941 ReturnInst::Create(Context
, nullptr, ExitBB
);
943 // Make sure that SCEV doesn't blow up
944 ScalarEvolution SE
= buildSE(*F
);
945 const SCEV
*Expr
= SE
.getSCEV(Phi
);
946 EXPECT_NE(nullptr, Expr
);
947 EXPECT_TRUE(isa
<SCEVUnknown
>(Expr
));
948 auto Result
= SE
.createAddRecFromPHIWithCasts(cast
<SCEVUnknown
>(Expr
));
951 TEST_F(ScalarEvolutionsTest
, SCEVAddRecFromPHIwithLargeConstantAccum
) {
952 // Make sure that SCEV does not blow up when constructing an AddRec
953 // with predicates for a phi with the update pattern:
954 // (SExt/ZExt ix (Trunc iy (%SymbolicPHI) to ix) to iy) + InvariantAccum
955 // when the InvariantAccum is a constant that is too large to fit in an
956 // ix but are zero when truncated to ix, and the initial value of the
957 // phi is not a constant.
958 Type
*Int32Ty
= Type::getInt32Ty(Context
);
959 SmallVector
<Type
*, 1> Types
;
960 Types
.push_back(Int32Ty
);
961 FunctionType
*FTy
= FunctionType::get(Type::getVoidTy(Context
), Types
, false);
963 Function::Create(FTy
, Function::ExternalLinkage
, "addrecphitest", M
);
967 define @addrecphitest(i32)
971 %1 = phi i32 [%0, %entry], [%4, %loop]
973 %3 = ashr exact i32 %2, 16
974 %4 = add i32 %3, -2147483648
975 br i1 undef, label %exit, label %loop
979 BasicBlock
*EntryBB
= BasicBlock::Create(Context
, "entry", F
);
980 BasicBlock
*LoopBB
= BasicBlock::Create(Context
, "loop", F
);
981 BasicBlock
*ExitBB
= BasicBlock::Create(Context
, "exit", F
);
984 BranchInst::Create(LoopBB
, EntryBB
);
986 auto *MinInt32
= ConstantInt::get(Context
, APInt(32, 0x80000000U
, true));
987 auto *Int32_16
= ConstantInt::get(Context
, APInt(32, 16));
988 auto *Br
= BranchInst::Create(
989 LoopBB
, ExitBB
, UndefValue::get(Type::getInt1Ty(Context
)), LoopBB
);
990 auto *Phi
= PHINode::Create(Int32Ty
, 2, "", Br
);
991 auto *Shl
= BinaryOperator::CreateShl(Phi
, Int32_16
, "", Br
);
992 auto *AShr
= BinaryOperator::CreateExactAShr(Shl
, Int32_16
, "", Br
);
993 auto *Add
= BinaryOperator::CreateAdd(AShr
, MinInt32
, "", Br
);
994 auto *Arg
= &*(F
->arg_begin());
995 Phi
->addIncoming(Arg
, EntryBB
);
996 Phi
->addIncoming(Add
, LoopBB
);
998 ReturnInst::Create(Context
, nullptr, ExitBB
);
1000 // Make sure that SCEV doesn't blow up
1001 ScalarEvolution SE
= buildSE(*F
);
1002 const SCEV
*Expr
= SE
.getSCEV(Phi
);
1003 EXPECT_NE(nullptr, Expr
);
1004 EXPECT_TRUE(isa
<SCEVUnknown
>(Expr
));
1005 auto Result
= SE
.createAddRecFromPHIWithCasts(cast
<SCEVUnknown
>(Expr
));
1008 TEST_F(ScalarEvolutionsTest
, SCEVFoldSumOfTruncs
) {
1009 // Verify that the following SCEV gets folded to a zero:
1010 // (-1 * (trunc i64 (-1 * %0) to i32)) + (-1 * (trunc i64 %0 to i32)
1011 Type
*ArgTy
= Type::getInt64Ty(Context
);
1012 Type
*Int32Ty
= Type::getInt32Ty(Context
);
1013 SmallVector
<Type
*, 1> Types
;
1014 Types
.push_back(ArgTy
);
1015 FunctionType
*FTy
= FunctionType::get(Type::getVoidTy(Context
), Types
, false);
1016 Function
*F
= Function::Create(FTy
, Function::ExternalLinkage
, "f", M
);
1017 BasicBlock
*BB
= BasicBlock::Create(Context
, "entry", F
);
1018 ReturnInst::Create(Context
, nullptr, BB
);
1020 ScalarEvolution SE
= buildSE(*F
);
1022 auto *Arg
= &*(F
->arg_begin());
1023 const auto *ArgSCEV
= SE
.getSCEV(Arg
);
1026 const auto *A0
= SE
.getNegativeSCEV(ArgSCEV
);
1027 const auto *A1
= SE
.getTruncateExpr(A0
, Int32Ty
);
1028 const auto *A
= SE
.getNegativeSCEV(A1
);
1030 const auto *B0
= SE
.getTruncateExpr(ArgSCEV
, Int32Ty
);
1031 const auto *B
= SE
.getNegativeSCEV(B0
);
1033 const auto *Expr
= SE
.getAddExpr(A
, B
);
1034 // Verify that the SCEV was folded to 0
1035 const auto *ZeroConst
= SE
.getConstant(Int32Ty
, 0);
1036 EXPECT_EQ(Expr
, ZeroConst
);
1039 // Check logic of SCEV expression size computation.
1040 TEST_F(ScalarEvolutionsTest
, SCEVComputeExpressionSize
) {
1042 * Create the following code:
1043 * void func(i64 %a, i64 %b)
1045 * %s1 = add i64 %a, 1
1046 * %s2 = udiv i64 %s1, %b
1053 Module
M("SCEVComputeExpressionSize", Context
);
1055 Type
*T_int64
= Type::getInt64Ty(Context
);
1058 FunctionType::get(Type::getVoidTy(Context
), { T_int64
, T_int64
}, false);
1059 Function
*F
= Function::Create(FTy
, Function::ExternalLinkage
, "func", M
);
1060 Argument
*A
= &*F
->arg_begin();
1061 Argument
*B
= &*std::next(F
->arg_begin());
1062 ConstantInt
*C
= ConstantInt::get(Context
, APInt(64, 1));
1064 BasicBlock
*Entry
= BasicBlock::Create(Context
, "entry", F
);
1065 BasicBlock
*Exit
= BasicBlock::Create(Context
, "exit", F
);
1067 IRBuilder
<> Builder(Entry
);
1068 auto *S1
= cast
<Instruction
>(Builder
.CreateAdd(A
, C
, "s1"));
1069 auto *S2
= cast
<Instruction
>(Builder
.CreateUDiv(S1
, B
, "s2"));
1070 Builder
.CreateBr(Exit
);
1072 Builder
.SetInsertPoint(Exit
);
1073 Builder
.CreateRetVoid();
1075 ScalarEvolution SE
= buildSE(*F
);
1076 // Get S2 first to move it to cache.
1077 const SCEV
*AS
= SE
.getSCEV(A
);
1078 const SCEV
*BS
= SE
.getSCEV(B
);
1079 const SCEV
*CS
= SE
.getSCEV(C
);
1080 const SCEV
*S1S
= SE
.getSCEV(S1
);
1081 const SCEV
*S2S
= SE
.getSCEV(S2
);
1082 EXPECT_EQ(AS
->getExpressionSize(), 1u);
1083 EXPECT_EQ(BS
->getExpressionSize(), 1u);
1084 EXPECT_EQ(CS
->getExpressionSize(), 1u);
1085 EXPECT_EQ(S1S
->getExpressionSize(), 3u);
1086 EXPECT_EQ(S2S
->getExpressionSize(), 5u);
1089 TEST_F(ScalarEvolutionsTest
, SCEVLoopDecIntrinsic
) {
1092 std::unique_ptr
<Module
> M
= parseAssemblyString(
1093 "define void @foo(i32 %N) { "
1095 " %cmp3 = icmp sgt i32 %N, 0 "
1096 " br i1 %cmp3, label %for.body, label %for.cond.cleanup "
1097 "for.cond.cleanup: "
1100 " %i.04 = phi i32 [ %inc, %for.body ], [ 100, %entry ] "
1101 " %inc = call i32 @llvm.loop.decrement.reg.i32.i32.i32(i32 %i.04, i32 1) "
1102 " %exitcond = icmp ne i32 %inc, 0 "
1103 " br i1 %exitcond, label %for.cond.cleanup, label %for.body "
1105 "declare i32 @llvm.loop.decrement.reg.i32.i32.i32(i32, i32) ",
1108 ASSERT_TRUE(M
&& "Could not parse module?");
1109 ASSERT_TRUE(!verifyModule(*M
) && "Must have been well formed!");
1111 runWithSE(*M
, "foo", [&](Function
&F
, LoopInfo
&LI
, ScalarEvolution
&SE
) {
1112 auto *ScevInc
= SE
.getSCEV(getInstructionByName(F
, "inc"));
1113 EXPECT_TRUE(isa
<SCEVAddRecExpr
>(ScevInc
));
1117 TEST_F(ScalarEvolutionsTest
, SCEVComputeConstantDifference
) {
1120 std::unique_ptr
<Module
> M
= parseAssemblyString(
1121 "define void @foo(i32 %sz, i32 %pp) { "
1123 " %v0 = add i32 %pp, 0 "
1124 " %v3 = add i32 %pp, 3 "
1125 " br label %loop.body "
1127 " %iv = phi i32 [ %iv.next, %loop.body ], [ 0, %entry ] "
1128 " %xa = add nsw i32 %iv, %v0 "
1129 " %yy = add nsw i32 %iv, %v3 "
1130 " %xb = sub nsw i32 %yy, 3 "
1131 " %iv.next = add nsw i32 %iv, 1 "
1132 " %cmp = icmp sle i32 %iv.next, %sz "
1133 " br i1 %cmp, label %loop.body, label %exit "
1139 ASSERT_TRUE(M
&& "Could not parse module?");
1140 ASSERT_TRUE(!verifyModule(*M
) && "Must have been well formed!");
1142 runWithSE(*M
, "foo", [](Function
&F
, LoopInfo
&LI
, ScalarEvolution
&SE
) {
1143 auto *ScevV0
= SE
.getSCEV(getInstructionByName(F
, "v0")); // %pp
1144 auto *ScevV3
= SE
.getSCEV(getInstructionByName(F
, "v3")); // (3 + %pp)
1145 auto *ScevIV
= SE
.getSCEV(getInstructionByName(F
, "iv")); // {0,+,1}
1146 auto *ScevXA
= SE
.getSCEV(getInstructionByName(F
, "xa")); // {%pp,+,1}
1147 auto *ScevYY
= SE
.getSCEV(getInstructionByName(F
, "yy")); // {(3 + %pp),+,1}
1148 auto *ScevXB
= SE
.getSCEV(getInstructionByName(F
, "xb")); // {%pp,+,1}
1149 auto *ScevIVNext
= SE
.getSCEV(getInstructionByName(F
, "iv.next")); // {1,+,1}
1151 auto diff
= [&SE
](const SCEV
*LHS
, const SCEV
*RHS
) -> Optional
<int> {
1152 auto ConstantDiffOrNone
= computeConstantDifference(SE
, LHS
, RHS
);
1153 if (!ConstantDiffOrNone
)
1156 auto ExtDiff
= ConstantDiffOrNone
->getSExtValue();
1158 assert(Diff
== ExtDiff
&& "Integer overflow");
1162 EXPECT_EQ(diff(ScevV3
, ScevV0
), 3);
1163 EXPECT_EQ(diff(ScevV0
, ScevV3
), -3);
1164 EXPECT_EQ(diff(ScevV0
, ScevV0
), 0);
1165 EXPECT_EQ(diff(ScevV3
, ScevV3
), 0);
1166 EXPECT_EQ(diff(ScevIV
, ScevIV
), 0);
1167 EXPECT_EQ(diff(ScevXA
, ScevXB
), 0);
1168 EXPECT_EQ(diff(ScevXA
, ScevYY
), -3);
1169 EXPECT_EQ(diff(ScevYY
, ScevXB
), 3);
1170 EXPECT_EQ(diff(ScevIV
, ScevIVNext
), -1);
1171 EXPECT_EQ(diff(ScevIVNext
, ScevIV
), 1);
1172 EXPECT_EQ(diff(ScevIVNext
, ScevIVNext
), 0);
1173 EXPECT_EQ(diff(ScevV0
, ScevIV
), None
);
1174 EXPECT_EQ(diff(ScevIVNext
, ScevV3
), None
);
1175 EXPECT_EQ(diff(ScevYY
, ScevV3
), None
);
1179 TEST_F(ScalarEvolutionsTest
, SCEVrewriteUnknowns
) {
1182 std::unique_ptr
<Module
> M
= parseAssemblyString(
1183 "define void @foo(i32 %i) { "
1185 " %cmp3 = icmp ult i32 %i, 16 "
1186 " br i1 %cmp3, label %loop.body, label %exit "
1188 " %iv = phi i32 [ %iv.next, %loop.body ], [ %i, %entry ] "
1189 " %iv.next = add nsw i32 %iv, 1 "
1190 " %cmp = icmp eq i32 %iv.next, 16 "
1191 " br i1 %cmp, label %exit, label %loop.body "
1197 ASSERT_TRUE(M
&& "Could not parse module?");
1198 ASSERT_TRUE(!verifyModule(*M
) && "Must have been well formed!");
1200 runWithSE(*M
, "foo", [](Function
&F
, LoopInfo
&LI
, ScalarEvolution
&SE
) {
1201 auto *ScevIV
= SE
.getSCEV(getInstructionByName(F
, "iv")); // {0,+,1}
1202 auto *ScevI
= SE
.getSCEV(getArgByName(F
, "i")); // {0,+,1}
1204 ValueToSCEVMapTy RewriteMap
;
1205 RewriteMap
[cast
<SCEVUnknown
>(ScevI
)->getValue()] =
1206 SE
.getUMinExpr(ScevI
, SE
.getConstant(ScevI
->getType(), 17));
1207 auto *WithUMin
= SCEVParameterRewriter::rewrite(ScevIV
, SE
, RewriteMap
);
1209 EXPECT_NE(WithUMin
, ScevIV
);
1210 auto *AR
= dyn_cast
<SCEVAddRecExpr
>(WithUMin
);
1212 EXPECT_EQ(AR
->getStart(),
1213 SE
.getUMinExpr(ScevI
, SE
.getConstant(ScevI
->getType(), 17)));
1214 EXPECT_EQ(AR
->getStepRecurrence(SE
),
1215 cast
<SCEVAddRecExpr
>(ScevIV
)->getStepRecurrence(SE
));
1219 TEST_F(ScalarEvolutionsTest
, SCEVAddNUW
) {
1222 std::unique_ptr
<Module
> M
= parseAssemblyString("define void @foo(i32 %x) { "
1227 ASSERT_TRUE(M
&& "Could not parse module?");
1228 ASSERT_TRUE(!verifyModule(*M
) && "Must have been well formed!");
1230 runWithSE(*M
, "foo", [](Function
&F
, LoopInfo
&LI
, ScalarEvolution
&SE
) {
1231 auto *X
= SE
.getSCEV(getArgByName(F
, "x"));
1232 auto *One
= SE
.getOne(X
->getType());
1233 auto *Sum
= SE
.getAddExpr(X
, One
, SCEV::FlagNUW
);
1234 EXPECT_TRUE(SE
.isKnownPredicate(ICmpInst::ICMP_UGE
, Sum
, X
));
1235 EXPECT_TRUE(SE
.isKnownPredicate(ICmpInst::ICMP_UGT
, Sum
, X
));
1239 TEST_F(ScalarEvolutionsTest
, SCEVgetRanges
) {
1242 std::unique_ptr
<Module
> M
= parseAssemblyString(
1243 "define void @foo(i32 %i) { "
1245 " br label %loop.body "
1247 " %iv = phi i32 [ %iv.next, %loop.body ], [ 0, %entry ] "
1248 " %iv.next = add nsw i32 %iv, 1 "
1249 " %cmp = icmp eq i32 %iv.next, 16 "
1250 " br i1 %cmp, label %exit, label %loop.body "
1256 runWithSE(*M
, "foo", [](Function
&F
, LoopInfo
&LI
, ScalarEvolution
&SE
) {
1257 auto *ScevIV
= SE
.getSCEV(getInstructionByName(F
, "iv")); // {0,+,1}
1258 auto *ScevI
= SE
.getSCEV(getArgByName(F
, "i"));
1259 EXPECT_EQ(SE
.getUnsignedRange(ScevIV
).getLower(), 0);
1260 EXPECT_EQ(SE
.getUnsignedRange(ScevIV
).getUpper(), 16);
1262 auto *Add
= SE
.getAddExpr(ScevI
, ScevIV
);
1263 ValueToSCEVMapTy RewriteMap
;
1264 RewriteMap
[cast
<SCEVUnknown
>(ScevI
)->getValue()] =
1265 SE
.getUMinExpr(ScevI
, SE
.getConstant(ScevI
->getType(), 17));
1266 auto *AddWithUMin
= SCEVParameterRewriter::rewrite(Add
, SE
, RewriteMap
);
1267 EXPECT_EQ(SE
.getUnsignedRange(AddWithUMin
).getLower(), 0);
1268 EXPECT_EQ(SE
.getUnsignedRange(AddWithUMin
).getUpper(), 33);
1272 TEST_F(ScalarEvolutionsTest
, SCEVgetExitLimitForGuardedLoop
) {
1275 std::unique_ptr
<Module
> M
= parseAssemblyString(
1276 "define void @foo(i32 %i) { "
1278 " %cmp3 = icmp ult i32 %i, 16 "
1279 " br i1 %cmp3, label %loop.body, label %exit "
1281 " %iv = phi i32 [ %iv.next, %loop.body ], [ %i, %entry ] "
1282 " %iv.next = add nsw i32 %iv, 1 "
1283 " %cmp = icmp eq i32 %iv.next, 16 "
1284 " br i1 %cmp, label %exit, label %loop.body "
1290 ASSERT_TRUE(M
&& "Could not parse module?");
1291 ASSERT_TRUE(!verifyModule(*M
) && "Must have been well formed!");
1293 runWithSE(*M
, "foo", [](Function
&F
, LoopInfo
&LI
, ScalarEvolution
&SE
) {
1294 auto *ScevIV
= SE
.getSCEV(getInstructionByName(F
, "iv")); // {0,+,1}
1295 const Loop
*L
= cast
<SCEVAddRecExpr
>(ScevIV
)->getLoop();
1297 const SCEV
*BTC
= SE
.getBackedgeTakenCount(L
);
1298 EXPECT_FALSE(isa
<SCEVConstant
>(BTC
));
1299 const SCEV
*MaxBTC
= SE
.getConstantMaxBackedgeTakenCount(L
);
1300 EXPECT_EQ(cast
<SCEVConstant
>(MaxBTC
)->getAPInt(), 15);
1304 TEST_F(ScalarEvolutionsTest
, ImpliedViaAddRecStart
) {
1307 std::unique_ptr
<Module
> M
= parseAssemblyString(
1308 "define void @foo(i32* %p) { "
1310 " %x = load i32, i32* %p, !range !0 "
1313 " %iv = phi i32 [ %x, %entry], [%iv.next, %backedge] "
1314 " %ne.check = icmp ne i32 %iv, 0 "
1315 " br i1 %ne.check, label %backedge, label %exit "
1317 " %iv.next = add i32 %iv, -1 "
1322 "!0 = !{i32 0, i32 2147483647}",
1325 ASSERT_TRUE(M
&& "Could not parse module?");
1326 ASSERT_TRUE(!verifyModule(*M
) && "Must have been well formed!");
1328 runWithSE(*M
, "foo", [](Function
&F
, LoopInfo
&LI
, ScalarEvolution
&SE
) {
1329 auto *X
= SE
.getSCEV(getInstructionByName(F
, "x"));
1330 auto *Context
= getInstructionByName(F
, "iv.next");
1331 EXPECT_TRUE(SE
.isKnownPredicateAt(ICmpInst::ICMP_NE
, X
,
1332 SE
.getZero(X
->getType()), Context
));
1336 TEST_F(ScalarEvolutionsTest
, UnsignedIsImpliedViaOperations
) {
1339 std::unique_ptr
<Module
> M
=
1340 parseAssemblyString("define void @foo(i32* %p1, i32* %p2) { "
1342 " %x = load i32, i32* %p1, !range !0 "
1343 " %cond = icmp ne i32 %x, 0 "
1344 " br i1 %cond, label %guarded, label %exit "
1346 " %y = add i32 %x, -1 "
1351 "!0 = !{i32 0, i32 2147483647}",
1354 ASSERT_TRUE(M
&& "Could not parse module?");
1355 ASSERT_TRUE(!verifyModule(*M
) && "Must have been well formed!");
1357 runWithSE(*M
, "foo", [](Function
&F
, LoopInfo
&LI
, ScalarEvolution
&SE
) {
1358 auto *X
= SE
.getSCEV(getInstructionByName(F
, "x"));
1359 auto *Y
= SE
.getSCEV(getInstructionByName(F
, "y"));
1360 auto *Guarded
= getInstructionByName(F
, "y")->getParent();
1361 ASSERT_TRUE(Guarded
);
1363 SE
.isBasicBlockEntryGuardedByCond(Guarded
, ICmpInst::ICMP_ULT
, Y
, X
));
1365 SE
.isBasicBlockEntryGuardedByCond(Guarded
, ICmpInst::ICMP_UGT
, X
, Y
));
1369 TEST_F(ScalarEvolutionsTest
, ProveImplicationViaNarrowing
) {
1372 std::unique_ptr
<Module
> M
= parseAssemblyString(
1373 "define i32 @foo(i32 %start, i32* %q) { "
1375 " %wide.start = zext i32 %start to i64 "
1378 " %wide.iv = phi i64 [%wide.start, %entry], [%wide.iv.next, %backedge] "
1379 " %iv = phi i32 [%start, %entry], [%iv.next, %backedge] "
1380 " %cond = icmp eq i64 %wide.iv, 0 "
1381 " br i1 %cond, label %exit, label %backedge "
1383 " %iv.next = add i32 %iv, -1 "
1384 " %index = zext i32 %iv.next to i64 "
1385 " %load.addr = getelementptr i32, i32* %q, i64 %index "
1386 " %stop = load i32, i32* %load.addr "
1387 " %loop.cond = icmp eq i32 %stop, 0 "
1388 " %wide.iv.next = add nsw i64 %wide.iv, -1 "
1389 " br i1 %loop.cond, label %loop, label %failure "
1397 ASSERT_TRUE(M
&& "Could not parse module?");
1398 ASSERT_TRUE(!verifyModule(*M
) && "Must have been well formed!");
1400 runWithSE(*M
, "foo", [](Function
&F
, LoopInfo
&LI
, ScalarEvolution
&SE
) {
1401 auto *IV
= SE
.getSCEV(getInstructionByName(F
, "iv"));
1402 auto *Zero
= SE
.getZero(IV
->getType());
1403 auto *Backedge
= getInstructionByName(F
, "iv.next")->getParent();
1404 ASSERT_TRUE(Backedge
);
1407 // FIXME: This can only be proved with turned on option
1408 // scalar-evolution-use-expensive-range-sharpening which is currently off.
1409 // Enable the check once it's switched true by default.
1410 // EXPECT_TRUE(SE.isBasicBlockEntryGuardedByCond(Backedge,
1411 // ICmpInst::ICMP_UGT,
1416 TEST_F(ScalarEvolutionsTest
, ImpliedCond
) {
1419 std::unique_ptr
<Module
> M
= parseAssemblyString(
1420 "define void @foo(i32 %len) { "
1424 " %iv = phi i32 [ 0, %entry], [%iv.next, %loop] "
1425 " %iv.next = add nsw i32 %iv, 1 "
1426 " %cmp = icmp slt i32 %iv, %len "
1427 " br i1 %cmp, label %loop, label %exit "
1433 ASSERT_TRUE(M
&& "Could not parse module?");
1434 ASSERT_TRUE(!verifyModule(*M
) && "Must have been well formed!");
1436 runWithSE(*M
, "foo", [](Function
&F
, LoopInfo
&LI
, ScalarEvolution
&SE
) {
1437 Instruction
*IV
= getInstructionByName(F
, "iv");
1438 Type
*Ty
= IV
->getType();
1439 const SCEV
*Zero
= SE
.getZero(Ty
);
1440 const SCEV
*MinusOne
= SE
.getMinusOne(Ty
);
1441 // {0,+,1}<nuw><nsw>
1442 const SCEV
*AddRec_0_1
= SE
.getSCEV(IV
);
1444 const SCEV
*AddRec_0_N1
= SE
.getNegativeSCEV(AddRec_0_1
);
1446 // {0,+,1}<nuw><nsw> > 0 -> {0,+,-1}<nw> < 0
1447 EXPECT_TRUE(isImpliedCond(SE
, ICmpInst::ICMP_SLT
, AddRec_0_N1
, Zero
,
1448 ICmpInst::ICMP_SGT
, AddRec_0_1
, Zero
));
1449 // {0,+,-1}<nw> < -1 -> {0,+,1}<nuw><nsw> > 0
1450 EXPECT_TRUE(isImpliedCond(SE
, ICmpInst::ICMP_SGT
, AddRec_0_1
, Zero
,
1451 ICmpInst::ICMP_SLT
, AddRec_0_N1
, MinusOne
));
1455 TEST_F(ScalarEvolutionsTest
, MatchURem
) {
1458 std::unique_ptr
<Module
> M
= parseAssemblyString(
1459 "target datalayout = \"e-m:e-p:32:32-f64:32:64-f80:32-n8:16:32-S128\" "
1461 "define void @test(i32 %a, i32 %b, i16 %c, i64 %d) {"
1463 " %rem1 = urem i32 %a, 2"
1464 " %rem2 = urem i32 %a, 5"
1465 " %rem3 = urem i32 %a, %b"
1466 " %c.ext = zext i16 %c to i32"
1467 " %rem4 = urem i32 %c.ext, 2"
1468 " %ext = zext i32 %rem4 to i64"
1469 " %rem5 = urem i64 %d, 17179869184"
1474 assert(M
&& "Could not parse module?");
1475 assert(!verifyModule(*M
) && "Must have been well formed!");
1477 runWithSE(*M
, "test", [&](Function
&F
, LoopInfo
&LI
, ScalarEvolution
&SE
) {
1478 for (auto *N
: {"rem1", "rem2", "rem3", "rem5"}) {
1479 auto *URemI
= getInstructionByName(F
, N
);
1480 auto *S
= SE
.getSCEV(URemI
);
1481 const SCEV
*LHS
, *RHS
;
1482 EXPECT_TRUE(matchURem(SE
, S
, LHS
, RHS
));
1483 EXPECT_EQ(LHS
, SE
.getSCEV(URemI
->getOperand(0)));
1484 EXPECT_EQ(RHS
, SE
.getSCEV(URemI
->getOperand(1)));
1485 EXPECT_EQ(LHS
->getType(), S
->getType());
1486 EXPECT_EQ(RHS
->getType(), S
->getType());
1489 // Check the case where the urem operand is zero-extended. Make sure the
1490 // match results are extended to the size of the input expression.
1491 auto *Ext
= getInstructionByName(F
, "ext");
1492 auto *URem1
= getInstructionByName(F
, "rem4");
1493 auto *S
= SE
.getSCEV(Ext
);
1494 const SCEV
*LHS
, *RHS
;
1495 EXPECT_TRUE(matchURem(SE
, S
, LHS
, RHS
));
1496 EXPECT_NE(LHS
, SE
.getSCEV(URem1
->getOperand(0)));
1497 // RHS and URem1->getOperand(1) have different widths, so compare the
1499 EXPECT_EQ(cast
<SCEVConstant
>(RHS
)->getValue()->getZExtValue(),
1500 cast
<SCEVConstant
>(SE
.getSCEV(URem1
->getOperand(1)))
1503 EXPECT_EQ(LHS
->getType(), S
->getType());
1504 EXPECT_EQ(RHS
->getType(), S
->getType());
1508 TEST_F(ScalarEvolutionsTest
, SCEVUDivFloorCeiling
) {
1511 std::unique_ptr
<Module
> M
= parseAssemblyString("define void @foo() { "
1516 ASSERT_TRUE(M
&& "Could not parse module?");
1517 ASSERT_TRUE(!verifyModule(*M
) && "Must have been well formed!");
1519 runWithSE(*M
, "foo", [](Function
&F
, LoopInfo
&LI
, ScalarEvolution
&SE
) {
1520 // Check that SCEV's udiv and uceil handling produce the correct results
1521 // for all 8 bit options. Div-by-zero is deliberately excluded.
1522 for (unsigned N
= 0; N
< 256; N
++)
1523 for (unsigned D
= 1; D
< 256; D
++) {
1526 using namespace llvm::APIntOps
;
1527 APInt FloorInt
= RoundingUDiv(NInt
, DInt
, APInt::Rounding::DOWN
);
1528 APInt CeilingInt
= RoundingUDiv(NInt
, DInt
, APInt::Rounding::UP
);
1529 auto *NS
= SE
.getConstant(NInt
);
1530 auto *DS
= SE
.getConstant(DInt
);
1531 auto *FloorS
= cast
<SCEVConstant
>(SE
.getUDivExpr(NS
, DS
));
1532 auto *CeilingS
= cast
<SCEVConstant
>(SE
.getUDivCeilSCEV(NS
, DS
));
1533 ASSERT_TRUE(FloorS
->getAPInt() == FloorInt
);
1534 ASSERT_TRUE(CeilingS
->getAPInt() == CeilingInt
);
1539 TEST_F(ScalarEvolutionsTest
, ComputeMaxTripCountFromArrayNormal
) {
1542 std::unique_ptr
<Module
> M
= parseAssemblyString(
1543 "define void @foo(i32 signext %len) { "
1545 " %a = alloca [7 x i32], align 4 "
1546 " %cmp4 = icmp sgt i32 %len, 0 "
1547 " br i1 %cmp4, label %for.body.preheader, label %for.cond.cleanup "
1548 "for.body.preheader: "
1549 " br label %for.body "
1550 "for.cond.cleanup.loopexit: "
1551 " br label %for.cond.cleanup "
1552 "for.cond.cleanup: "
1555 " %iv = phi i32 [ %inc, %for.body ], [ 0, %for.body.preheader ] "
1556 " %idxprom = zext i32 %iv to i64 "
1557 " %arrayidx = getelementptr inbounds [7 x i32], [7 x i32]* %a, i64 0, \
1559 " store i32 0, i32* %arrayidx, align 4 "
1560 " %inc = add nuw nsw i32 %iv, 1 "
1561 " %cmp = icmp slt i32 %inc, %len "
1562 " br i1 %cmp, label %for.body, label %for.cond.cleanup.loopexit "
1566 ASSERT_TRUE(M
&& "Could not parse module?");
1567 ASSERT_TRUE(!verifyModule(*M
) && "Must have been well formed!");
1569 runWithSE(*M
, "foo", [](Function
&F
, LoopInfo
&LI
, ScalarEvolution
&SE
) {
1570 auto *ScevIV
= SE
.getSCEV(getInstructionByName(F
, "iv"));
1571 const Loop
*L
= cast
<SCEVAddRecExpr
>(ScevIV
)->getLoop();
1573 const SCEV
*ITC
= SE
.getConstantMaxTripCountFromArray(L
);
1574 EXPECT_FALSE(isa
<SCEVCouldNotCompute
>(ITC
));
1575 EXPECT_TRUE(isa
<SCEVConstant
>(ITC
));
1576 EXPECT_EQ(cast
<SCEVConstant
>(ITC
)->getAPInt().getSExtValue(), 8);
1580 TEST_F(ScalarEvolutionsTest
, ComputeMaxTripCountFromZeroArray
) {
1583 std::unique_ptr
<Module
> M
= parseAssemblyString(
1584 "define void @foo(i32 signext %len) { "
1586 " %a = alloca [0 x i32], align 4 "
1587 " %cmp4 = icmp sgt i32 %len, 0 "
1588 " br i1 %cmp4, label %for.body.preheader, label %for.cond.cleanup "
1589 "for.body.preheader: "
1590 " br label %for.body "
1591 "for.cond.cleanup.loopexit: "
1592 " br label %for.cond.cleanup "
1593 "for.cond.cleanup: "
1596 " %iv = phi i32 [ %inc, %for.body ], [ 0, %for.body.preheader ] "
1597 " %idxprom = zext i32 %iv to i64 "
1598 " %arrayidx = getelementptr inbounds [0 x i32], [0 x i32]* %a, i64 0, \
1600 " store i32 0, i32* %arrayidx, align 4 "
1601 " %inc = add nuw nsw i32 %iv, 1 "
1602 " %cmp = icmp slt i32 %inc, %len "
1603 " br i1 %cmp, label %for.body, label %for.cond.cleanup.loopexit "
1607 ASSERT_TRUE(M
&& "Could not parse module?");
1608 ASSERT_TRUE(!verifyModule(*M
) && "Must have been well formed!");
1610 runWithSE(*M
, "foo", [](Function
&F
, LoopInfo
&LI
, ScalarEvolution
&SE
) {
1611 auto *ScevIV
= SE
.getSCEV(getInstructionByName(F
, "iv"));
1612 const Loop
*L
= cast
<SCEVAddRecExpr
>(ScevIV
)->getLoop();
1614 const SCEV
*ITC
= SE
.getConstantMaxTripCountFromArray(L
);
1615 EXPECT_FALSE(isa
<SCEVCouldNotCompute
>(ITC
));
1616 EXPECT_TRUE(isa
<SCEVConstant
>(ITC
));
1617 EXPECT_EQ(cast
<SCEVConstant
>(ITC
)->getAPInt().getSExtValue(), 1);
1621 TEST_F(ScalarEvolutionsTest
, ComputeMaxTripCountFromExtremArray
) {
1624 std::unique_ptr
<Module
> M
= parseAssemblyString(
1625 "define void @foo(i32 signext %len) { "
1627 " %a = alloca [4294967295 x i1], align 4 "
1628 " %cmp4 = icmp sgt i32 %len, 0 "
1629 " br i1 %cmp4, label %for.body.preheader, label %for.cond.cleanup "
1630 "for.body.preheader: "
1631 " br label %for.body "
1632 "for.cond.cleanup.loopexit: "
1633 " br label %for.cond.cleanup "
1634 "for.cond.cleanup: "
1637 " %iv = phi i32 [ %inc, %for.body ], [ 0, %for.body.preheader ] "
1638 " %idxprom = zext i32 %iv to i64 "
1639 " %arrayidx = getelementptr inbounds [4294967295 x i1], \
1640 [4294967295 x i1]* %a, i64 0, i64 %idxprom "
1641 " store i1 0, i1* %arrayidx, align 4 "
1642 " %inc = add nuw nsw i32 %iv, 1 "
1643 " %cmp = icmp slt i32 %inc, %len "
1644 " br i1 %cmp, label %for.body, label %for.cond.cleanup.loopexit "
1648 ASSERT_TRUE(M
&& "Could not parse module?");
1649 ASSERT_TRUE(!verifyModule(*M
) && "Must have been well formed!");
1651 runWithSE(*M
, "foo", [](Function
&F
, LoopInfo
&LI
, ScalarEvolution
&SE
) {
1652 auto *ScevIV
= SE
.getSCEV(getInstructionByName(F
, "iv"));
1653 const Loop
*L
= cast
<SCEVAddRecExpr
>(ScevIV
)->getLoop();
1655 const SCEV
*ITC
= SE
.getConstantMaxTripCountFromArray(L
);
1656 EXPECT_TRUE(isa
<SCEVCouldNotCompute
>(ITC
));
1660 TEST_F(ScalarEvolutionsTest
, ComputeMaxTripCountFromArrayInBranch
) {
1663 std::unique_ptr
<Module
> M
= parseAssemblyString(
1664 "define void @foo(i32 signext %len) { "
1666 " %a = alloca [8 x i32], align 4 "
1667 " br label %for.cond "
1669 " %iv = phi i32 [ %inc, %for.inc ], [ 0, %entry ] "
1670 " %cmp = icmp slt i32 %iv, %len "
1671 " br i1 %cmp, label %for.body, label %for.cond.cleanup "
1672 "for.cond.cleanup: "
1673 " br label %for.end "
1675 " %cmp1 = icmp slt i32 %iv, 8 "
1676 " br i1 %cmp1, label %if.then, label %if.end "
1678 " %idxprom = sext i32 %iv to i64 "
1679 " %arrayidx = getelementptr inbounds [8 x i32], [8 x i32]* %a, i64 0, \
1681 " store i32 0, i32* %arrayidx, align 4 "
1682 " br label %if.end "
1684 " br label %for.inc "
1686 " %inc = add nsw i32 %iv, 1 "
1687 " br label %for.cond "
1693 ASSERT_TRUE(M
&& "Could not parse module?");
1694 ASSERT_TRUE(!verifyModule(*M
) && "Must have been well formed!");
1696 runWithSE(*M
, "foo", [](Function
&F
, LoopInfo
&LI
, ScalarEvolution
&SE
) {
1697 auto *ScevIV
= SE
.getSCEV(getInstructionByName(F
, "iv"));
1698 const Loop
*L
= cast
<SCEVAddRecExpr
>(ScevIV
)->getLoop();
1700 const SCEV
*ITC
= SE
.getConstantMaxTripCountFromArray(L
);
1701 EXPECT_TRUE(isa
<SCEVCouldNotCompute
>(ITC
));
1705 TEST_F(ScalarEvolutionsTest
, ComputeMaxTripCountFromMultiDemArray
) {
1708 std::unique_ptr
<Module
> M
= parseAssemblyString(
1709 "define void @foo(i32 signext %len) { "
1711 " %a = alloca [3 x [5 x i32]], align 4 "
1712 " br label %for.cond "
1714 " %iv = phi i32 [ %inc, %for.inc ], [ 0, %entry ] "
1715 " %cmp = icmp slt i32 %iv, %len "
1716 " br i1 %cmp, label %for.body, label %for.cond.cleanup "
1717 "for.cond.cleanup: "
1718 " br label %for.end "
1720 " %arrayidx = getelementptr inbounds [3 x [5 x i32]], \
1721 [3 x [5 x i32]]* %a, i64 0, i64 3 "
1722 " %idxprom = sext i32 %iv to i64 "
1723 " %arrayidx1 = getelementptr inbounds [5 x i32], [5 x i32]* %arrayidx, \
1724 i64 0, i64 %idxprom "
1725 " store i32 0, i32* %arrayidx1, align 4"
1726 " br label %for.inc "
1728 " %inc = add nsw i32 %iv, 1 "
1729 " br label %for.cond "
1735 ASSERT_TRUE(M
&& "Could not parse module?");
1736 ASSERT_TRUE(!verifyModule(*M
) && "Must have been well formed!");
1738 runWithSE(*M
, "foo", [](Function
&F
, LoopInfo
&LI
, ScalarEvolution
&SE
) {
1739 auto *ScevIV
= SE
.getSCEV(getInstructionByName(F
, "iv"));
1740 const Loop
*L
= cast
<SCEVAddRecExpr
>(ScevIV
)->getLoop();
1742 const SCEV
*ITC
= SE
.getConstantMaxTripCountFromArray(L
);
1743 EXPECT_TRUE(isa
<SCEVCouldNotCompute
>(ITC
));
1747 } // end namespace llvm