blob: 0bc99a37dd8e56c8c217bac1741135be15174d62 [file] [log] [blame]
Dan Gohman7cac9572010-08-02 23:49:30 +00001//===- ScalarEvolutionsTest.cpp - ScalarEvolution unit tests --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Wei Mi3076cc32016-09-15 04:06:44 +000010#include "llvm/Analysis/ScalarEvolutionExpander.h"
Craig Topperbc40d7e2012-09-15 18:45:38 +000011#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chandler Carruth2f1fd162015-08-17 02:08:17 +000012#include "llvm/Analysis/AssumptionCache.h"
13#include "llvm/Analysis/LoopInfo.h"
14#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruth130cec22012-12-04 10:23:08 +000015#include "llvm/ADT/SmallVector.h"
Craig Topperbc40d7e2012-09-15 18:45:38 +000016#include "llvm/Analysis/LoopInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#include "llvm/IR/Constants.h"
Chandler Carruth2f1fd162015-08-17 02:08:17 +000018#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/GlobalVariable.h"
20#include "llvm/IR/LLVMContext.h"
21#include "llvm/IR/Module.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +000022#include "llvm/IR/LegacyPassManager.h"
Dan Gohman7cac9572010-08-02 23:49:30 +000023#include "gtest/gtest.h"
24
25namespace llvm {
26namespace {
27
Nick Lewycky287682e2011-10-04 06:51:26 +000028// We use this fixture to ensure that we clean up ScalarEvolution before
29// deleting the PassManager.
30class ScalarEvolutionsTest : public testing::Test {
31protected:
Dan Gohman7cac9572010-08-02 23:49:30 +000032 LLVMContext Context;
Nick Lewycky287682e2011-10-04 06:51:26 +000033 Module M;
Chandler Carruth2f1fd162015-08-17 02:08:17 +000034 TargetLibraryInfoImpl TLII;
35 TargetLibraryInfo TLI;
36
37 std::unique_ptr<AssumptionCache> AC;
38 std::unique_ptr<DominatorTree> DT;
39 std::unique_ptr<LoopInfo> LI;
40
41 ScalarEvolutionsTest() : M("", Context), TLII(), TLI(TLII) {}
42
43 ScalarEvolution buildSE(Function &F) {
44 AC.reset(new AssumptionCache(F));
45 DT.reset(new DominatorTree(F));
46 LI.reset(new LoopInfo(*DT));
47 return ScalarEvolution(F, TLI, *AC, *DT, *LI);
48 }
Nick Lewycky287682e2011-10-04 06:51:26 +000049};
Dan Gohman7cac9572010-08-02 23:49:30 +000050
Nick Lewycky287682e2011-10-04 06:51:26 +000051TEST_F(ScalarEvolutionsTest, SCEVUnknownRAUW) {
Chris Lattner229907c2011-07-18 04:54:35 +000052 FunctionType *FTy = FunctionType::get(Type::getVoidTy(Context),
Jay Foadb804a2b2011-07-12 14:06:48 +000053 std::vector<Type *>(), false);
Dan Gohman7cac9572010-08-02 23:49:30 +000054 Function *F = cast<Function>(M.getOrInsertFunction("f", FTy));
55 BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
Craig Topper66f09ad2014-06-08 22:29:17 +000056 ReturnInst::Create(Context, nullptr, BB);
Dan Gohman7cac9572010-08-02 23:49:30 +000057
Chris Lattner229907c2011-07-18 04:54:35 +000058 Type *Ty = Type::getInt1Ty(Context);
Dan Gohman7cac9572010-08-02 23:49:30 +000059 Constant *Init = Constant::getNullValue(Ty);
60 Value *V0 = new GlobalVariable(M, Ty, false, GlobalValue::ExternalLinkage, Init, "V0");
61 Value *V1 = new GlobalVariable(M, Ty, false, GlobalValue::ExternalLinkage, Init, "V1");
62 Value *V2 = new GlobalVariable(M, Ty, false, GlobalValue::ExternalLinkage, Init, "V2");
63
Chandler Carruth2f1fd162015-08-17 02:08:17 +000064 ScalarEvolution SE = buildSE(*F);
Dan Gohman7cac9572010-08-02 23:49:30 +000065
66 const SCEV *S0 = SE.getSCEV(V0);
67 const SCEV *S1 = SE.getSCEV(V1);
68 const SCEV *S2 = SE.getSCEV(V2);
69
70 const SCEV *P0 = SE.getAddExpr(S0, S0);
71 const SCEV *P1 = SE.getAddExpr(S1, S1);
72 const SCEV *P2 = SE.getAddExpr(S2, S2);
73
74 const SCEVMulExpr *M0 = cast<SCEVMulExpr>(P0);
75 const SCEVMulExpr *M1 = cast<SCEVMulExpr>(P1);
76 const SCEVMulExpr *M2 = cast<SCEVMulExpr>(P2);
77
78 EXPECT_EQ(cast<SCEVConstant>(M0->getOperand(0))->getValue()->getZExtValue(),
79 2u);
80 EXPECT_EQ(cast<SCEVConstant>(M1->getOperand(0))->getValue()->getZExtValue(),
81 2u);
82 EXPECT_EQ(cast<SCEVConstant>(M2->getOperand(0))->getValue()->getZExtValue(),
83 2u);
84
85 // Before the RAUWs, these are all pointing to separate values.
86 EXPECT_EQ(cast<SCEVUnknown>(M0->getOperand(1))->getValue(), V0);
87 EXPECT_EQ(cast<SCEVUnknown>(M1->getOperand(1))->getValue(), V1);
88 EXPECT_EQ(cast<SCEVUnknown>(M2->getOperand(1))->getValue(), V2);
89
90 // Do some RAUWs.
91 V2->replaceAllUsesWith(V1);
92 V1->replaceAllUsesWith(V0);
93
94 // After the RAUWs, these should all be pointing to V0.
95 EXPECT_EQ(cast<SCEVUnknown>(M0->getOperand(1))->getValue(), V0);
96 EXPECT_EQ(cast<SCEVUnknown>(M1->getOperand(1))->getValue(), V0);
97 EXPECT_EQ(cast<SCEVUnknown>(M2->getOperand(1))->getValue(), V0);
Nick Lewycky287682e2011-10-04 06:51:26 +000098}
Dan Gohman7cac9572010-08-02 23:49:30 +000099
Nick Lewycky287682e2011-10-04 06:51:26 +0000100TEST_F(ScalarEvolutionsTest, SCEVMultiplyAddRecs) {
101 Type *Ty = Type::getInt32Ty(Context);
102 SmallVector<Type *, 10> Types;
103 Types.append(10, Ty);
104 FunctionType *FTy = FunctionType::get(Type::getVoidTy(Context), Types, false);
105 Function *F = cast<Function>(M.getOrInsertFunction("f", FTy));
106 BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
Craig Topper66f09ad2014-06-08 22:29:17 +0000107 ReturnInst::Create(Context, nullptr, BB);
Nick Lewycky287682e2011-10-04 06:51:26 +0000108
Chandler Carruth2f1fd162015-08-17 02:08:17 +0000109 ScalarEvolution SE = buildSE(*F);
Nick Lewycky287682e2011-10-04 06:51:26 +0000110
111 // It's possible to produce an empty loop through the default constructor,
112 // but you can't add any blocks to it without a LoopInfo pass.
113 Loop L;
114 const_cast<std::vector<BasicBlock*>&>(L.getBlocks()).push_back(BB);
115
116 Function::arg_iterator AI = F->arg_begin();
117 SmallVector<const SCEV *, 5> A;
118 A.push_back(SE.getSCEV(&*AI++));
119 A.push_back(SE.getSCEV(&*AI++));
120 A.push_back(SE.getSCEV(&*AI++));
121 A.push_back(SE.getSCEV(&*AI++));
122 A.push_back(SE.getSCEV(&*AI++));
123 const SCEV *A_rec = SE.getAddRecExpr(A, &L, SCEV::FlagAnyWrap);
124
125 SmallVector<const SCEV *, 5> B;
126 B.push_back(SE.getSCEV(&*AI++));
127 B.push_back(SE.getSCEV(&*AI++));
128 B.push_back(SE.getSCEV(&*AI++));
129 B.push_back(SE.getSCEV(&*AI++));
130 B.push_back(SE.getSCEV(&*AI++));
131 const SCEV *B_rec = SE.getAddRecExpr(B, &L, SCEV::FlagAnyWrap);
132
133 /* Spot check that we perform this transformation:
134 {A0,+,A1,+,A2,+,A3,+,A4} * {B0,+,B1,+,B2,+,B3,+,B4} =
135 {A0*B0,+,
136 A1*B0 + A0*B1 + A1*B1,+,
137 A2*B0 + 2A1*B1 + A0*B2 + 2A2*B1 + 2A1*B2 + A2*B2,+,
138 A3*B0 + 3A2*B1 + 3A1*B2 + A0*B3 + 3A3*B1 + 6A2*B2 + 3A1*B3 + 3A3*B2 +
139 3A2*B3 + A3*B3,+,
140 A4*B0 + 4A3*B1 + 6A2*B2 + 4A1*B3 + A0*B4 + 4A4*B1 + 12A3*B2 + 12A2*B3 +
141 4A1*B4 + 6A4*B2 + 12A3*B3 + 6A2*B4 + 4A4*B3 + 4A3*B4 + A4*B4,+,
142 5A4*B1 + 10A3*B2 + 10A2*B3 + 5A1*B4 + 20A4*B2 + 30A3*B3 + 20A2*B4 +
143 30A4*B3 + 30A3*B4 + 20A4*B4,+,
144 15A4*B2 + 20A3*B3 + 15A2*B4 + 60A4*B3 + 60A3*B4 + 90A4*B4,+,
145 35A4*B3 + 35A3*B4 + 140A4*B4,+,
146 70A4*B4}
147 */
148
149 const SCEVAddRecExpr *Product =
150 dyn_cast<SCEVAddRecExpr>(SE.getMulExpr(A_rec, B_rec));
151 ASSERT_TRUE(Product);
152 ASSERT_EQ(Product->getNumOperands(), 9u);
153
154 SmallVector<const SCEV *, 16> Sum;
155 Sum.push_back(SE.getMulExpr(A[0], B[0]));
156 EXPECT_EQ(Product->getOperand(0), SE.getAddExpr(Sum));
157 Sum.clear();
158
159 // SCEV produces different an equal but different expression for these.
160 // Re-enable when PR11052 is fixed.
161#if 0
162 Sum.push_back(SE.getMulExpr(A[1], B[0]));
163 Sum.push_back(SE.getMulExpr(A[0], B[1]));
164 Sum.push_back(SE.getMulExpr(A[1], B[1]));
165 EXPECT_EQ(Product->getOperand(1), SE.getAddExpr(Sum));
166 Sum.clear();
167
168 Sum.push_back(SE.getMulExpr(A[2], B[0]));
169 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 2), A[1], B[1]));
170 Sum.push_back(SE.getMulExpr(A[0], B[2]));
171 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 2), A[2], B[1]));
172 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 2), A[1], B[2]));
173 Sum.push_back(SE.getMulExpr(A[2], B[2]));
174 EXPECT_EQ(Product->getOperand(2), SE.getAddExpr(Sum));
175 Sum.clear();
176
177 Sum.push_back(SE.getMulExpr(A[3], B[0]));
178 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 3), A[2], B[1]));
179 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 3), A[1], B[2]));
180 Sum.push_back(SE.getMulExpr(A[0], B[3]));
181 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 3), A[3], B[1]));
182 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 6), A[2], B[2]));
183 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 3), A[1], B[3]));
184 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 3), A[3], B[2]));
185 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 3), A[2], B[3]));
186 Sum.push_back(SE.getMulExpr(A[3], B[3]));
187 EXPECT_EQ(Product->getOperand(3), SE.getAddExpr(Sum));
188 Sum.clear();
189
190 Sum.push_back(SE.getMulExpr(A[4], B[0]));
191 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 4), A[3], B[1]));
192 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 6), A[2], B[2]));
193 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 4), A[1], B[3]));
194 Sum.push_back(SE.getMulExpr(A[0], B[4]));
195 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 4), A[4], B[1]));
196 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 12), A[3], B[2]));
197 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 12), A[2], B[3]));
198 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 4), A[1], B[4]));
199 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 6), A[4], B[2]));
200 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 12), A[3], B[3]));
201 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 6), A[2], B[4]));
202 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 4), A[4], B[3]));
203 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 4), A[3], B[4]));
204 Sum.push_back(SE.getMulExpr(A[4], B[4]));
205 EXPECT_EQ(Product->getOperand(4), SE.getAddExpr(Sum));
206 Sum.clear();
207
208 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 5), A[4], B[1]));
209 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 10), A[3], B[2]));
210 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 10), A[2], B[3]));
211 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 5), A[1], B[4]));
212 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 20), A[4], B[2]));
213 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 30), A[3], B[3]));
214 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 20), A[2], B[4]));
215 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 30), A[4], B[3]));
216 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 30), A[3], B[4]));
217 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 20), A[4], B[4]));
218 EXPECT_EQ(Product->getOperand(5), SE.getAddExpr(Sum));
219 Sum.clear();
220
221 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 15), A[4], B[2]));
222 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 20), A[3], B[3]));
223 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 15), A[2], B[4]));
224 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 60), A[4], B[3]));
225 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 60), A[3], B[4]));
226 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 90), A[4], B[4]));
227 EXPECT_EQ(Product->getOperand(6), SE.getAddExpr(Sum));
228 Sum.clear();
229
230 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 35), A[4], B[3]));
231 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 35), A[3], B[4]));
232 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 140), A[4], B[4]));
233 EXPECT_EQ(Product->getOperand(7), SE.getAddExpr(Sum));
234 Sum.clear();
235#endif
236
237 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 70), A[4], B[4]));
238 EXPECT_EQ(Product->getOperand(8), SE.getAddExpr(Sum));
Dan Gohman7cac9572010-08-02 23:49:30 +0000239}
240
Tobias Grosser11332e52016-02-21 17:42:10 +0000241TEST_F(ScalarEvolutionsTest, SimplifiedPHI) {
242 FunctionType *FTy = FunctionType::get(Type::getVoidTy(Context),
243 std::vector<Type *>(), false);
244 Function *F = cast<Function>(M.getOrInsertFunction("f", FTy));
245 BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", F);
246 BasicBlock *LoopBB = BasicBlock::Create(Context, "loop", F);
247 BasicBlock *ExitBB = BasicBlock::Create(Context, "exit", F);
248 BranchInst::Create(LoopBB, EntryBB);
249 BranchInst::Create(LoopBB, ExitBB, UndefValue::get(Type::getInt1Ty(Context)),
250 LoopBB);
251 ReturnInst::Create(Context, nullptr, ExitBB);
252 auto *Ty = Type::getInt32Ty(Context);
253 auto *PN = PHINode::Create(Ty, 2, "", &*LoopBB->begin());
254 PN->addIncoming(Constant::getNullValue(Ty), EntryBB);
255 PN->addIncoming(UndefValue::get(Ty), LoopBB);
256 ScalarEvolution SE = buildSE(*F);
257 auto *S1 = SE.getSCEV(PN);
258 auto *S2 = SE.getSCEV(PN);
Tobias Grosser946ca0a2016-02-22 07:20:40 +0000259 auto *ZeroConst = SE.getConstant(Ty, 0);
Tobias Grosser11332e52016-02-21 17:42:10 +0000260
261 // At some point, only the first call to getSCEV returned the simplified
262 // SCEVConstant and later calls just returned a SCEVUnknown referencing the
263 // PHI node.
Tobias Grosser946ca0a2016-02-22 07:20:40 +0000264 EXPECT_EQ(S1, ZeroConst);
265 EXPECT_EQ(S1, S2);
Tobias Grosser11332e52016-02-21 17:42:10 +0000266}
267
Wei Mi3076cc32016-09-15 04:06:44 +0000268TEST_F(ScalarEvolutionsTest, ExpandPtrTypeSCEV) {
269 // It is to test the fix for PR30213. It exercises the branch in scev
270 // expansion when the value in ValueOffsetPair is a ptr and the offset
271 // is not divisible by the elem type size of value.
272 auto *I8Ty = Type::getInt8Ty(Context);
273 auto *I8PtrTy = Type::getInt8PtrTy(Context);
274 auto *I32Ty = Type::getInt32Ty(Context);
275 auto *I32PtrTy = Type::getInt32PtrTy(Context);
276 FunctionType *FTy =
277 FunctionType::get(Type::getVoidTy(Context), std::vector<Type *>(), false);
278 Function *F = cast<Function>(M.getOrInsertFunction("f", FTy));
279 BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", F);
280 BasicBlock *LoopBB = BasicBlock::Create(Context, "loop", F);
281 BasicBlock *ExitBB = BasicBlock::Create(Context, "exit", F);
282 BranchInst::Create(LoopBB, EntryBB);
283 ReturnInst::Create(Context, nullptr, ExitBB);
284
285 // loop: ; preds = %loop, %entry
286 // %alloca = alloca i32
287 // %gep0 = getelementptr i32, i32* %alloca, i32 1
288 // %bitcast1 = bitcast i32* %gep0 to i8*
289 // %gep1 = getelementptr i8, i8* %bitcast1, i32 1
290 // %gep2 = getelementptr i8, i8* undef, i32 1
291 // %cmp = icmp ult i8* undef, %bitcast1
292 // %select = select i1 %cmp, i8* %gep1, i8* %gep2
293 // %bitcast2 = bitcast i8* %select to i32*
294 // br i1 undef, label %loop, label %exit
295
296 BranchInst *Br = BranchInst::Create(
297 LoopBB, ExitBB, UndefValue::get(Type::getInt1Ty(Context)), LoopBB);
298 AllocaInst *Alloca = new AllocaInst(I32Ty, "alloca", Br);
299 ConstantInt *Ci32 = ConstantInt::get(Context, APInt(32, 1));
300 GetElementPtrInst *Gep0 =
301 GetElementPtrInst::Create(I32Ty, Alloca, Ci32, "gep0", Br);
302 CastInst *CastA =
303 CastInst::CreateBitOrPointerCast(Gep0, I8PtrTy, "bitcast1", Br);
304 GetElementPtrInst *Gep1 =
305 GetElementPtrInst::Create(I8Ty, CastA, Ci32, "gep1", Br);
306 GetElementPtrInst *Gep2 = GetElementPtrInst::Create(
307 I8Ty, UndefValue::get(I8PtrTy), Ci32, "gep2", Br);
308 CmpInst *Cmp = CmpInst::Create(Instruction::ICmp, CmpInst::ICMP_ULT,
309 UndefValue::get(I8PtrTy), CastA, "cmp", Br);
310 SelectInst *Sel = SelectInst::Create(Cmp, Gep1, Gep2, "select", Br);
311 CastInst *CastB =
312 CastInst::CreateBitOrPointerCast(Sel, I32PtrTy, "bitcast2", Br);
313
314 ScalarEvolution SE = buildSE(*F);
315 auto *S = SE.getSCEV(CastB);
316 SCEVExpander Exp(SE, M.getDataLayout(), "expander");
317 Value *V =
318 Exp.expandCodeFor(cast<SCEVAddExpr>(S)->getOperand(1), nullptr, Br);
319
320 // Expect the expansion code contains:
321 // %0 = bitcast i32* %bitcast2 to i8*
322 // %uglygep = getelementptr i8, i8* %0, i64 -1
323 // %1 = bitcast i8* %uglygep to i32*
324 EXPECT_TRUE(isa<BitCastInst>(V));
325 Instruction *Gep = cast<Instruction>(V)->getPrevNode();
326 EXPECT_TRUE(isa<GetElementPtrInst>(Gep));
327 EXPECT_TRUE(isa<ConstantInt>(Gep->getOperand(1)));
328 EXPECT_EQ(cast<ConstantInt>(Gep->getOperand(1))->getSExtValue(), -1);
329 EXPECT_TRUE(isa<BitCastInst>(Gep->getPrevNode()));
330}
331
Dan Gohman7cac9572010-08-02 23:49:30 +0000332} // end anonymous namespace
333} // end namespace llvm