blob: 2f509fd7e1b3420e1b723342a9efd8b0f1d93f37 [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"
Sanjoy Das507dd402016-10-18 17:45:16 +000017#include "llvm/AsmParser/Parser.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/Constants.h"
Chandler Carruth2f1fd162015-08-17 02:08:17 +000019#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/GlobalVariable.h"
Sanjoy Das507dd402016-10-18 17:45:16 +000021#include "llvm/IR/InstIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/LLVMContext.h"
23#include "llvm/IR/Module.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +000024#include "llvm/IR/LegacyPassManager.h"
Sanjoy Das507dd402016-10-18 17:45:16 +000025#include "llvm/IR/Verifier.h"
26#include "llvm/Support/SourceMgr.h"
Dan Gohman7cac9572010-08-02 23:49:30 +000027#include "gtest/gtest.h"
28
29namespace llvm {
30namespace {
31
Nick Lewycky287682e2011-10-04 06:51:26 +000032// We use this fixture to ensure that we clean up ScalarEvolution before
33// deleting the PassManager.
34class ScalarEvolutionsTest : public testing::Test {
35protected:
Dan Gohman7cac9572010-08-02 23:49:30 +000036 LLVMContext Context;
Nick Lewycky287682e2011-10-04 06:51:26 +000037 Module M;
Chandler Carruth2f1fd162015-08-17 02:08:17 +000038 TargetLibraryInfoImpl TLII;
39 TargetLibraryInfo TLI;
40
41 std::unique_ptr<AssumptionCache> AC;
42 std::unique_ptr<DominatorTree> DT;
43 std::unique_ptr<LoopInfo> LI;
44
45 ScalarEvolutionsTest() : M("", Context), TLII(), TLI(TLII) {}
46
47 ScalarEvolution buildSE(Function &F) {
48 AC.reset(new AssumptionCache(F));
49 DT.reset(new DominatorTree(F));
50 LI.reset(new LoopInfo(*DT));
51 return ScalarEvolution(F, TLI, *AC, *DT, *LI);
52 }
Sanjoy Das6764b9a2016-11-10 07:56:05 +000053
Sanjoy Dasb1227db2016-12-12 14:57:11 +000054 void runSCEVTest(Module &M, StringRef FuncName,
55 function_ref<void(Function &F, DominatorTree &DT,
56 LoopInfo &LI, ScalarEvolution &SE)>
57 Test) {
Sanjoy Das6764b9a2016-11-10 07:56:05 +000058 auto *F = M.getFunction(FuncName);
59 ASSERT_NE(F, nullptr) << "Could not find " << FuncName;
60 ScalarEvolution SE = buildSE(*F);
Sanjoy Dasb1227db2016-12-12 14:57:11 +000061 Test(*F, *DT, *LI, SE);
62 }
63
64 void runWithFunctionAndSE(
65 Module &M, StringRef FuncName,
66 function_ref<void(Function &F, ScalarEvolution &SE)> Test) {
67 runSCEVTest(M, FuncName, [&](Function &F, DominatorTree &DT, LoopInfo &LI,
68 ScalarEvolution &SE) { Test(F, SE); });
Sanjoy Das6764b9a2016-11-10 07:56:05 +000069 }
Nick Lewycky287682e2011-10-04 06:51:26 +000070};
Dan Gohman7cac9572010-08-02 23:49:30 +000071
Nick Lewycky287682e2011-10-04 06:51:26 +000072TEST_F(ScalarEvolutionsTest, SCEVUnknownRAUW) {
Chris Lattner229907c2011-07-18 04:54:35 +000073 FunctionType *FTy = FunctionType::get(Type::getVoidTy(Context),
Jay Foadb804a2b2011-07-12 14:06:48 +000074 std::vector<Type *>(), false);
Dan Gohman7cac9572010-08-02 23:49:30 +000075 Function *F = cast<Function>(M.getOrInsertFunction("f", FTy));
76 BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
Craig Topper66f09ad2014-06-08 22:29:17 +000077 ReturnInst::Create(Context, nullptr, BB);
Dan Gohman7cac9572010-08-02 23:49:30 +000078
Chris Lattner229907c2011-07-18 04:54:35 +000079 Type *Ty = Type::getInt1Ty(Context);
Dan Gohman7cac9572010-08-02 23:49:30 +000080 Constant *Init = Constant::getNullValue(Ty);
81 Value *V0 = new GlobalVariable(M, Ty, false, GlobalValue::ExternalLinkage, Init, "V0");
82 Value *V1 = new GlobalVariable(M, Ty, false, GlobalValue::ExternalLinkage, Init, "V1");
83 Value *V2 = new GlobalVariable(M, Ty, false, GlobalValue::ExternalLinkage, Init, "V2");
84
Chandler Carruth2f1fd162015-08-17 02:08:17 +000085 ScalarEvolution SE = buildSE(*F);
Dan Gohman7cac9572010-08-02 23:49:30 +000086
87 const SCEV *S0 = SE.getSCEV(V0);
88 const SCEV *S1 = SE.getSCEV(V1);
89 const SCEV *S2 = SE.getSCEV(V2);
90
91 const SCEV *P0 = SE.getAddExpr(S0, S0);
92 const SCEV *P1 = SE.getAddExpr(S1, S1);
93 const SCEV *P2 = SE.getAddExpr(S2, S2);
94
95 const SCEVMulExpr *M0 = cast<SCEVMulExpr>(P0);
96 const SCEVMulExpr *M1 = cast<SCEVMulExpr>(P1);
97 const SCEVMulExpr *M2 = cast<SCEVMulExpr>(P2);
98
99 EXPECT_EQ(cast<SCEVConstant>(M0->getOperand(0))->getValue()->getZExtValue(),
100 2u);
101 EXPECT_EQ(cast<SCEVConstant>(M1->getOperand(0))->getValue()->getZExtValue(),
102 2u);
103 EXPECT_EQ(cast<SCEVConstant>(M2->getOperand(0))->getValue()->getZExtValue(),
104 2u);
105
106 // Before the RAUWs, these are all pointing to separate values.
107 EXPECT_EQ(cast<SCEVUnknown>(M0->getOperand(1))->getValue(), V0);
108 EXPECT_EQ(cast<SCEVUnknown>(M1->getOperand(1))->getValue(), V1);
109 EXPECT_EQ(cast<SCEVUnknown>(M2->getOperand(1))->getValue(), V2);
110
111 // Do some RAUWs.
112 V2->replaceAllUsesWith(V1);
113 V1->replaceAllUsesWith(V0);
114
115 // After the RAUWs, these should all be pointing to V0.
116 EXPECT_EQ(cast<SCEVUnknown>(M0->getOperand(1))->getValue(), V0);
117 EXPECT_EQ(cast<SCEVUnknown>(M1->getOperand(1))->getValue(), V0);
118 EXPECT_EQ(cast<SCEVUnknown>(M2->getOperand(1))->getValue(), V0);
Nick Lewycky287682e2011-10-04 06:51:26 +0000119}
Dan Gohman7cac9572010-08-02 23:49:30 +0000120
Nick Lewycky287682e2011-10-04 06:51:26 +0000121TEST_F(ScalarEvolutionsTest, SCEVMultiplyAddRecs) {
122 Type *Ty = Type::getInt32Ty(Context);
123 SmallVector<Type *, 10> Types;
124 Types.append(10, Ty);
125 FunctionType *FTy = FunctionType::get(Type::getVoidTy(Context), Types, false);
126 Function *F = cast<Function>(M.getOrInsertFunction("f", FTy));
127 BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
Craig Topper66f09ad2014-06-08 22:29:17 +0000128 ReturnInst::Create(Context, nullptr, BB);
Nick Lewycky287682e2011-10-04 06:51:26 +0000129
Chandler Carruth2f1fd162015-08-17 02:08:17 +0000130 ScalarEvolution SE = buildSE(*F);
Nick Lewycky287682e2011-10-04 06:51:26 +0000131
132 // It's possible to produce an empty loop through the default constructor,
133 // but you can't add any blocks to it without a LoopInfo pass.
134 Loop L;
135 const_cast<std::vector<BasicBlock*>&>(L.getBlocks()).push_back(BB);
136
137 Function::arg_iterator AI = F->arg_begin();
138 SmallVector<const SCEV *, 5> A;
139 A.push_back(SE.getSCEV(&*AI++));
140 A.push_back(SE.getSCEV(&*AI++));
141 A.push_back(SE.getSCEV(&*AI++));
142 A.push_back(SE.getSCEV(&*AI++));
143 A.push_back(SE.getSCEV(&*AI++));
144 const SCEV *A_rec = SE.getAddRecExpr(A, &L, SCEV::FlagAnyWrap);
145
146 SmallVector<const SCEV *, 5> B;
147 B.push_back(SE.getSCEV(&*AI++));
148 B.push_back(SE.getSCEV(&*AI++));
149 B.push_back(SE.getSCEV(&*AI++));
150 B.push_back(SE.getSCEV(&*AI++));
151 B.push_back(SE.getSCEV(&*AI++));
152 const SCEV *B_rec = SE.getAddRecExpr(B, &L, SCEV::FlagAnyWrap);
153
154 /* Spot check that we perform this transformation:
155 {A0,+,A1,+,A2,+,A3,+,A4} * {B0,+,B1,+,B2,+,B3,+,B4} =
156 {A0*B0,+,
157 A1*B0 + A0*B1 + A1*B1,+,
158 A2*B0 + 2A1*B1 + A0*B2 + 2A2*B1 + 2A1*B2 + A2*B2,+,
159 A3*B0 + 3A2*B1 + 3A1*B2 + A0*B3 + 3A3*B1 + 6A2*B2 + 3A1*B3 + 3A3*B2 +
160 3A2*B3 + A3*B3,+,
161 A4*B0 + 4A3*B1 + 6A2*B2 + 4A1*B3 + A0*B4 + 4A4*B1 + 12A3*B2 + 12A2*B3 +
162 4A1*B4 + 6A4*B2 + 12A3*B3 + 6A2*B4 + 4A4*B3 + 4A3*B4 + A4*B4,+,
163 5A4*B1 + 10A3*B2 + 10A2*B3 + 5A1*B4 + 20A4*B2 + 30A3*B3 + 20A2*B4 +
164 30A4*B3 + 30A3*B4 + 20A4*B4,+,
165 15A4*B2 + 20A3*B3 + 15A2*B4 + 60A4*B3 + 60A3*B4 + 90A4*B4,+,
166 35A4*B3 + 35A3*B4 + 140A4*B4,+,
167 70A4*B4}
168 */
169
170 const SCEVAddRecExpr *Product =
171 dyn_cast<SCEVAddRecExpr>(SE.getMulExpr(A_rec, B_rec));
172 ASSERT_TRUE(Product);
173 ASSERT_EQ(Product->getNumOperands(), 9u);
174
175 SmallVector<const SCEV *, 16> Sum;
176 Sum.push_back(SE.getMulExpr(A[0], B[0]));
177 EXPECT_EQ(Product->getOperand(0), SE.getAddExpr(Sum));
178 Sum.clear();
179
180 // SCEV produces different an equal but different expression for these.
181 // Re-enable when PR11052 is fixed.
182#if 0
183 Sum.push_back(SE.getMulExpr(A[1], B[0]));
184 Sum.push_back(SE.getMulExpr(A[0], B[1]));
185 Sum.push_back(SE.getMulExpr(A[1], B[1]));
186 EXPECT_EQ(Product->getOperand(1), SE.getAddExpr(Sum));
187 Sum.clear();
188
189 Sum.push_back(SE.getMulExpr(A[2], B[0]));
190 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 2), A[1], B[1]));
191 Sum.push_back(SE.getMulExpr(A[0], B[2]));
192 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 2), A[2], B[1]));
193 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 2), A[1], B[2]));
194 Sum.push_back(SE.getMulExpr(A[2], B[2]));
195 EXPECT_EQ(Product->getOperand(2), SE.getAddExpr(Sum));
196 Sum.clear();
197
198 Sum.push_back(SE.getMulExpr(A[3], B[0]));
199 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 3), A[2], B[1]));
200 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 3), A[1], B[2]));
201 Sum.push_back(SE.getMulExpr(A[0], B[3]));
202 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 3), A[3], B[1]));
203 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 6), A[2], B[2]));
204 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 3), A[1], B[3]));
205 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 3), A[3], B[2]));
206 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 3), A[2], B[3]));
207 Sum.push_back(SE.getMulExpr(A[3], B[3]));
208 EXPECT_EQ(Product->getOperand(3), SE.getAddExpr(Sum));
209 Sum.clear();
210
211 Sum.push_back(SE.getMulExpr(A[4], B[0]));
212 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 4), A[3], B[1]));
213 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 6), A[2], B[2]));
214 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 4), A[1], B[3]));
215 Sum.push_back(SE.getMulExpr(A[0], B[4]));
216 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 4), A[4], B[1]));
217 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 12), A[3], B[2]));
218 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 12), A[2], B[3]));
219 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 4), A[1], B[4]));
220 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 6), A[4], B[2]));
221 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 12), A[3], B[3]));
222 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 6), A[2], B[4]));
223 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 4), A[4], B[3]));
224 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 4), A[3], B[4]));
225 Sum.push_back(SE.getMulExpr(A[4], B[4]));
226 EXPECT_EQ(Product->getOperand(4), SE.getAddExpr(Sum));
227 Sum.clear();
228
229 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 5), A[4], B[1]));
230 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 10), A[3], B[2]));
231 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 10), A[2], B[3]));
232 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 5), A[1], B[4]));
233 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 20), A[4], B[2]));
234 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 30), A[3], B[3]));
235 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 20), A[2], B[4]));
236 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 30), A[4], B[3]));
237 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 30), A[3], B[4]));
238 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 20), A[4], B[4]));
239 EXPECT_EQ(Product->getOperand(5), SE.getAddExpr(Sum));
240 Sum.clear();
241
242 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 15), A[4], B[2]));
243 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 20), A[3], B[3]));
244 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 15), A[2], B[4]));
245 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 60), A[4], B[3]));
246 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 60), A[3], B[4]));
247 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 90), A[4], B[4]));
248 EXPECT_EQ(Product->getOperand(6), SE.getAddExpr(Sum));
249 Sum.clear();
250
251 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 35), A[4], B[3]));
252 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 35), A[3], B[4]));
253 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 140), A[4], B[4]));
254 EXPECT_EQ(Product->getOperand(7), SE.getAddExpr(Sum));
255 Sum.clear();
256#endif
257
258 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 70), A[4], B[4]));
259 EXPECT_EQ(Product->getOperand(8), SE.getAddExpr(Sum));
Dan Gohman7cac9572010-08-02 23:49:30 +0000260}
261
Tobias Grosser11332e52016-02-21 17:42:10 +0000262TEST_F(ScalarEvolutionsTest, SimplifiedPHI) {
263 FunctionType *FTy = FunctionType::get(Type::getVoidTy(Context),
264 std::vector<Type *>(), false);
265 Function *F = cast<Function>(M.getOrInsertFunction("f", FTy));
266 BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", F);
267 BasicBlock *LoopBB = BasicBlock::Create(Context, "loop", F);
268 BasicBlock *ExitBB = BasicBlock::Create(Context, "exit", F);
269 BranchInst::Create(LoopBB, EntryBB);
270 BranchInst::Create(LoopBB, ExitBB, UndefValue::get(Type::getInt1Ty(Context)),
271 LoopBB);
272 ReturnInst::Create(Context, nullptr, ExitBB);
273 auto *Ty = Type::getInt32Ty(Context);
274 auto *PN = PHINode::Create(Ty, 2, "", &*LoopBB->begin());
275 PN->addIncoming(Constant::getNullValue(Ty), EntryBB);
276 PN->addIncoming(UndefValue::get(Ty), LoopBB);
277 ScalarEvolution SE = buildSE(*F);
278 auto *S1 = SE.getSCEV(PN);
279 auto *S2 = SE.getSCEV(PN);
Tobias Grosser946ca0a2016-02-22 07:20:40 +0000280 auto *ZeroConst = SE.getConstant(Ty, 0);
Tobias Grosser11332e52016-02-21 17:42:10 +0000281
282 // At some point, only the first call to getSCEV returned the simplified
283 // SCEVConstant and later calls just returned a SCEVUnknown referencing the
284 // PHI node.
Tobias Grosser946ca0a2016-02-22 07:20:40 +0000285 EXPECT_EQ(S1, ZeroConst);
286 EXPECT_EQ(S1, S2);
Tobias Grosser11332e52016-02-21 17:42:10 +0000287}
288
Wei Mi3076cc32016-09-15 04:06:44 +0000289TEST_F(ScalarEvolutionsTest, ExpandPtrTypeSCEV) {
290 // It is to test the fix for PR30213. It exercises the branch in scev
291 // expansion when the value in ValueOffsetPair is a ptr and the offset
292 // is not divisible by the elem type size of value.
293 auto *I8Ty = Type::getInt8Ty(Context);
294 auto *I8PtrTy = Type::getInt8PtrTy(Context);
295 auto *I32Ty = Type::getInt32Ty(Context);
296 auto *I32PtrTy = Type::getInt32PtrTy(Context);
297 FunctionType *FTy =
298 FunctionType::get(Type::getVoidTy(Context), std::vector<Type *>(), false);
299 Function *F = cast<Function>(M.getOrInsertFunction("f", FTy));
300 BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", F);
301 BasicBlock *LoopBB = BasicBlock::Create(Context, "loop", F);
302 BasicBlock *ExitBB = BasicBlock::Create(Context, "exit", F);
303 BranchInst::Create(LoopBB, EntryBB);
304 ReturnInst::Create(Context, nullptr, ExitBB);
305
306 // loop: ; preds = %loop, %entry
307 // %alloca = alloca i32
308 // %gep0 = getelementptr i32, i32* %alloca, i32 1
309 // %bitcast1 = bitcast i32* %gep0 to i8*
310 // %gep1 = getelementptr i8, i8* %bitcast1, i32 1
311 // %gep2 = getelementptr i8, i8* undef, i32 1
312 // %cmp = icmp ult i8* undef, %bitcast1
313 // %select = select i1 %cmp, i8* %gep1, i8* %gep2
314 // %bitcast2 = bitcast i8* %select to i32*
315 // br i1 undef, label %loop, label %exit
316
317 BranchInst *Br = BranchInst::Create(
318 LoopBB, ExitBB, UndefValue::get(Type::getInt1Ty(Context)), LoopBB);
319 AllocaInst *Alloca = new AllocaInst(I32Ty, "alloca", Br);
320 ConstantInt *Ci32 = ConstantInt::get(Context, APInt(32, 1));
321 GetElementPtrInst *Gep0 =
322 GetElementPtrInst::Create(I32Ty, Alloca, Ci32, "gep0", Br);
323 CastInst *CastA =
324 CastInst::CreateBitOrPointerCast(Gep0, I8PtrTy, "bitcast1", Br);
325 GetElementPtrInst *Gep1 =
326 GetElementPtrInst::Create(I8Ty, CastA, Ci32, "gep1", Br);
327 GetElementPtrInst *Gep2 = GetElementPtrInst::Create(
328 I8Ty, UndefValue::get(I8PtrTy), Ci32, "gep2", Br);
329 CmpInst *Cmp = CmpInst::Create(Instruction::ICmp, CmpInst::ICMP_ULT,
330 UndefValue::get(I8PtrTy), CastA, "cmp", Br);
331 SelectInst *Sel = SelectInst::Create(Cmp, Gep1, Gep2, "select", Br);
332 CastInst *CastB =
333 CastInst::CreateBitOrPointerCast(Sel, I32PtrTy, "bitcast2", Br);
334
335 ScalarEvolution SE = buildSE(*F);
336 auto *S = SE.getSCEV(CastB);
337 SCEVExpander Exp(SE, M.getDataLayout(), "expander");
338 Value *V =
339 Exp.expandCodeFor(cast<SCEVAddExpr>(S)->getOperand(1), nullptr, Br);
340
341 // Expect the expansion code contains:
342 // %0 = bitcast i32* %bitcast2 to i8*
343 // %uglygep = getelementptr i8, i8* %0, i64 -1
344 // %1 = bitcast i8* %uglygep to i32*
345 EXPECT_TRUE(isa<BitCastInst>(V));
346 Instruction *Gep = cast<Instruction>(V)->getPrevNode();
347 EXPECT_TRUE(isa<GetElementPtrInst>(Gep));
348 EXPECT_TRUE(isa<ConstantInt>(Gep->getOperand(1)));
349 EXPECT_EQ(cast<ConstantInt>(Gep->getOperand(1))->getSExtValue(), -1);
350 EXPECT_TRUE(isa<BitCastInst>(Gep->getPrevNode()));
351}
352
Sanjoy Dasb53021d2016-10-30 23:52:50 +0000353static Instruction *getInstructionByName(Function &F, StringRef Name) {
354 for (auto &I : instructions(F))
355 if (I.getName() == Name)
356 return &I;
Sanjoy Das507dd402016-10-18 17:45:16 +0000357 llvm_unreachable("Expected to find instruction!");
358}
359
Sebastian Pop8c9cc8c2016-12-12 02:52:51 +0000360static Argument *getArgByName(Function &F, StringRef Name) {
361 for (auto &A : F.args())
362 if (A.getName() == Name)
363 return &A;
364 llvm_unreachable("Expected to find argument!");
365}
366
Sanjoy Das507dd402016-10-18 17:45:16 +0000367TEST_F(ScalarEvolutionsTest, CommutativeExprOperandOrder) {
368 LLVMContext C;
369 SMDiagnostic Err;
370 std::unique_ptr<Module> M = parseAssemblyString(
371 "target datalayout = \"e-m:e-p:32:32-f64:32:64-f80:32-n8:16:32-S128\" "
Sanjoy Dasb53021d2016-10-30 23:52:50 +0000372 " "
Sanjoy Das299e6722016-10-30 23:52:56 +0000373 "@var_0 = external global i32, align 4"
374 "@var_1 = external global i32, align 4"
Sanjoy Dasfd080902016-10-31 03:32:45 +0000375 "@var_2 = external global i32, align 4"
Sanjoy Das299e6722016-10-30 23:52:56 +0000376 " "
Sanjoy Das17078692016-10-31 03:32:43 +0000377 "declare i32 @unknown(i32, i32, i32)"
378 " "
Sanjoy Dasb53021d2016-10-30 23:52:50 +0000379 "define void @f_1(i8* nocapture %arr, i32 %n, i32* %A, i32* %B) "
Sanjoy Das507dd402016-10-18 17:45:16 +0000380 " local_unnamed_addr { "
381 "entry: "
382 " %entrycond = icmp sgt i32 %n, 0 "
383 " br i1 %entrycond, label %loop.ph, label %for.end "
384 " "
385 "loop.ph: "
386 " %a = load i32, i32* %A, align 4 "
387 " %b = load i32, i32* %B, align 4 "
388 " %mul = mul nsw i32 %b, %a "
389 " %iv0.init = getelementptr inbounds i8, i8* %arr, i32 %mul "
390 " br label %loop "
391 " "
392 "loop: "
393 " %iv0 = phi i8* [ %iv0.inc, %loop ], [ %iv0.init, %loop.ph ] "
394 " %iv1 = phi i32 [ %iv1.inc, %loop ], [ 0, %loop.ph ] "
395 " %conv = trunc i32 %iv1 to i8 "
396 " store i8 %conv, i8* %iv0, align 1 "
397 " %iv0.inc = getelementptr inbounds i8, i8* %iv0, i32 %b "
398 " %iv1.inc = add nuw nsw i32 %iv1, 1 "
399 " %exitcond = icmp eq i32 %iv1.inc, %n "
400 " br i1 %exitcond, label %for.end.loopexit, label %loop "
401 " "
402 "for.end.loopexit: "
403 " br label %for.end "
404 " "
405 "for.end: "
406 " ret void "
407 "} "
408 " "
Sanjoy Dasb53021d2016-10-30 23:52:50 +0000409 "define void @f_2(i32* %X, i32* %Y, i32* %Z) { "
Sanjoy Das507dd402016-10-18 17:45:16 +0000410 " %x = load i32, i32* %X "
411 " %y = load i32, i32* %Y "
412 " %z = load i32, i32* %Z "
413 " ret void "
Sanjoy Das299e6722016-10-30 23:52:56 +0000414 "} "
415 " "
Sanjoy Das299e6722016-10-30 23:52:56 +0000416 "define void @f_3() { "
417 " %x = load i32, i32* @var_0"
418 " %y = load i32, i32* @var_1"
Sanjoy Dasfd080902016-10-31 03:32:45 +0000419 " %z = load i32, i32* @var_2"
Sanjoy Das299e6722016-10-30 23:52:56 +0000420 " ret void"
421 "} "
Sanjoy Das17078692016-10-31 03:32:43 +0000422 " "
423 "define void @f_4(i32 %a, i32 %b, i32 %c) { "
424 " %x = call i32 @unknown(i32 %a, i32 %b, i32 %c)"
425 " %y = call i32 @unknown(i32 %b, i32 %c, i32 %a)"
426 " %z = call i32 @unknown(i32 %c, i32 %a, i32 %b)"
427 " ret void"
428 "} "
Sanjoy Das299e6722016-10-30 23:52:56 +0000429 ,
Sanjoy Das507dd402016-10-18 17:45:16 +0000430 Err, C);
431
432 assert(M && "Could not parse module?");
433 assert(!verifyModule(*M) && "Must have been well formed!");
434
Sanjoy Das6764b9a2016-11-10 07:56:05 +0000435 runWithFunctionAndSE(*M, "f_1", [&](Function &F, ScalarEvolution &SE) {
Sanjoy Das3d6e3df2016-10-31 03:32:39 +0000436 auto *IV0 = getInstructionByName(F, "iv0");
437 auto *IV0Inc = getInstructionByName(F, "iv0.inc");
Sanjoy Das507dd402016-10-18 17:45:16 +0000438
Sanjoy Das507dd402016-10-18 17:45:16 +0000439 auto *FirstExprForIV0 = SE.getSCEV(IV0);
440 auto *FirstExprForIV0Inc = SE.getSCEV(IV0Inc);
441 auto *SecondExprForIV0 = SE.getSCEV(IV0);
442
443 EXPECT_TRUE(isa<SCEVAddRecExpr>(FirstExprForIV0));
444 EXPECT_TRUE(isa<SCEVAddRecExpr>(FirstExprForIV0Inc));
445 EXPECT_TRUE(isa<SCEVAddRecExpr>(SecondExprForIV0));
Sanjoy Das3d6e3df2016-10-31 03:32:39 +0000446 });
Sanjoy Das507dd402016-10-18 17:45:16 +0000447
Sanjoy Das17078692016-10-31 03:32:43 +0000448 auto CheckCommutativeMulExprs = [&](ScalarEvolution &SE, const SCEV *A,
449 const SCEV *B, const SCEV *C) {
450 EXPECT_EQ(SE.getMulExpr(A, B), SE.getMulExpr(B, A));
451 EXPECT_EQ(SE.getMulExpr(B, C), SE.getMulExpr(C, B));
452 EXPECT_EQ(SE.getMulExpr(A, C), SE.getMulExpr(C, A));
Sanjoy Das507dd402016-10-18 17:45:16 +0000453
Sanjoy Das17078692016-10-31 03:32:43 +0000454 SmallVector<const SCEV *, 3> Ops0 = {A, B, C};
455 SmallVector<const SCEV *, 3> Ops1 = {A, C, B};
456 SmallVector<const SCEV *, 3> Ops2 = {B, A, C};
457 SmallVector<const SCEV *, 3> Ops3 = {B, C, A};
458 SmallVector<const SCEV *, 3> Ops4 = {C, B, A};
459 SmallVector<const SCEV *, 3> Ops5 = {C, A, B};
Sanjoy Das507dd402016-10-18 17:45:16 +0000460
461 auto *Mul0 = SE.getMulExpr(Ops0);
462 auto *Mul1 = SE.getMulExpr(Ops1);
463 auto *Mul2 = SE.getMulExpr(Ops2);
464 auto *Mul3 = SE.getMulExpr(Ops3);
465 auto *Mul4 = SE.getMulExpr(Ops4);
466 auto *Mul5 = SE.getMulExpr(Ops5);
467
Sanjoy Das17078692016-10-31 03:32:43 +0000468 EXPECT_EQ(Mul0, Mul1) << "Expected " << *Mul0 << " == " << *Mul1;
469 EXPECT_EQ(Mul1, Mul2) << "Expected " << *Mul1 << " == " << *Mul2;
470 EXPECT_EQ(Mul2, Mul3) << "Expected " << *Mul2 << " == " << *Mul3;
471 EXPECT_EQ(Mul3, Mul4) << "Expected " << *Mul3 << " == " << *Mul4;
472 EXPECT_EQ(Mul4, Mul5) << "Expected " << *Mul4 << " == " << *Mul5;
473 };
474
Sanjoy Dasfd080902016-10-31 03:32:45 +0000475 for (StringRef FuncName : {"f_2", "f_3", "f_4"})
Sanjoy Das6764b9a2016-11-10 07:56:05 +0000476 runWithFunctionAndSE(*M, FuncName, [&](Function &F, ScalarEvolution &SE) {
Sanjoy Dasfd080902016-10-31 03:32:45 +0000477 CheckCommutativeMulExprs(SE, SE.getSCEV(getInstructionByName(F, "x")),
478 SE.getSCEV(getInstructionByName(F, "y")),
479 SE.getSCEV(getInstructionByName(F, "z")));
480 });
Sanjoy Das507dd402016-10-18 17:45:16 +0000481}
482
Daniil Fukalov4c3322c2016-11-17 16:07:52 +0000483TEST_F(ScalarEvolutionsTest, SCEVCompareComplexity) {
484 FunctionType *FTy =
485 FunctionType::get(Type::getVoidTy(Context), std::vector<Type *>(), false);
486 Function *F = cast<Function>(M.getOrInsertFunction("f", FTy));
487 BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", F);
488 BasicBlock *LoopBB = BasicBlock::Create(Context, "bb1", F);
489 BranchInst::Create(LoopBB, EntryBB);
490
491 auto *Ty = Type::getInt32Ty(Context);
492 SmallVector<Instruction*, 8> Muls(8), Acc(8), NextAcc(8);
493
494 Acc[0] = PHINode::Create(Ty, 2, "", LoopBB);
495 Acc[1] = PHINode::Create(Ty, 2, "", LoopBB);
496 Acc[2] = PHINode::Create(Ty, 2, "", LoopBB);
497 Acc[3] = PHINode::Create(Ty, 2, "", LoopBB);
498 Acc[4] = PHINode::Create(Ty, 2, "", LoopBB);
499 Acc[5] = PHINode::Create(Ty, 2, "", LoopBB);
500 Acc[6] = PHINode::Create(Ty, 2, "", LoopBB);
501 Acc[7] = PHINode::Create(Ty, 2, "", LoopBB);
502
503 for (int i = 0; i < 20; i++) {
504 Muls[0] = BinaryOperator::CreateMul(Acc[0], Acc[0], "", LoopBB);
505 NextAcc[0] = BinaryOperator::CreateAdd(Muls[0], Acc[4], "", LoopBB);
506 Muls[1] = BinaryOperator::CreateMul(Acc[1], Acc[1], "", LoopBB);
507 NextAcc[1] = BinaryOperator::CreateAdd(Muls[1], Acc[5], "", LoopBB);
508 Muls[2] = BinaryOperator::CreateMul(Acc[2], Acc[2], "", LoopBB);
509 NextAcc[2] = BinaryOperator::CreateAdd(Muls[2], Acc[6], "", LoopBB);
510 Muls[3] = BinaryOperator::CreateMul(Acc[3], Acc[3], "", LoopBB);
511 NextAcc[3] = BinaryOperator::CreateAdd(Muls[3], Acc[7], "", LoopBB);
512
513 Muls[4] = BinaryOperator::CreateMul(Acc[4], Acc[4], "", LoopBB);
514 NextAcc[4] = BinaryOperator::CreateAdd(Muls[4], Acc[0], "", LoopBB);
515 Muls[5] = BinaryOperator::CreateMul(Acc[5], Acc[5], "", LoopBB);
516 NextAcc[5] = BinaryOperator::CreateAdd(Muls[5], Acc[1], "", LoopBB);
517 Muls[6] = BinaryOperator::CreateMul(Acc[6], Acc[6], "", LoopBB);
518 NextAcc[6] = BinaryOperator::CreateAdd(Muls[6], Acc[2], "", LoopBB);
519 Muls[7] = BinaryOperator::CreateMul(Acc[7], Acc[7], "", LoopBB);
520 NextAcc[7] = BinaryOperator::CreateAdd(Muls[7], Acc[3], "", LoopBB);
521 Acc = NextAcc;
522 }
523
524 auto II = LoopBB->begin();
525 for (int i = 0; i < 8; i++) {
526 PHINode *Phi = cast<PHINode>(&*II++);
527 Phi->addIncoming(Acc[i], LoopBB);
528 Phi->addIncoming(UndefValue::get(Ty), EntryBB);
529 }
530
531 BasicBlock *ExitBB = BasicBlock::Create(Context, "bb2", F);
532 BranchInst::Create(LoopBB, ExitBB, UndefValue::get(Type::getInt1Ty(Context)),
533 LoopBB);
534
535 Acc[0] = BinaryOperator::CreateAdd(Acc[0], Acc[1], "", ExitBB);
536 Acc[1] = BinaryOperator::CreateAdd(Acc[2], Acc[3], "", ExitBB);
537 Acc[2] = BinaryOperator::CreateAdd(Acc[4], Acc[5], "", ExitBB);
538 Acc[3] = BinaryOperator::CreateAdd(Acc[6], Acc[7], "", ExitBB);
539 Acc[0] = BinaryOperator::CreateAdd(Acc[0], Acc[1], "", ExitBB);
540 Acc[1] = BinaryOperator::CreateAdd(Acc[2], Acc[3], "", ExitBB);
541 Acc[0] = BinaryOperator::CreateAdd(Acc[0], Acc[1], "", ExitBB);
542
543 ReturnInst::Create(Context, nullptr, ExitBB);
544
545 ScalarEvolution SE = buildSE(*F);
546
547 EXPECT_NE(nullptr, SE.getSCEV(Acc[0]));
548}
549
Sebastian Pop8c9cc8c2016-12-12 02:52:51 +0000550TEST_F(ScalarEvolutionsTest, BadHoistingSCEVExpander_PR30942) {
551 LLVMContext C;
552 SMDiagnostic Err;
553 std::unique_ptr<Module> M = parseAssemblyString(
554 "target datalayout = \"e-m:e-p:32:32-f64:32:64-f80:32-n8:16:32-S128\" "
555 " "
556 "define void @f_1(i32 %x, i32 %y, i32 %n, i1* %cond_buf) "
557 " local_unnamed_addr { "
558 "entry: "
559 " %entrycond = icmp sgt i32 %n, 0 "
560 " br i1 %entrycond, label %loop.ph, label %for.end "
561 " "
562 "loop.ph: "
563 " br label %loop "
564 " "
565 "loop: "
566 " %iv1 = phi i32 [ %iv1.inc, %right ], [ 0, %loop.ph ] "
567 " %iv1.inc = add nuw nsw i32 %iv1, 1 "
568 " %cond = load volatile i1, i1* %cond_buf "
569 " br i1 %cond, label %left, label %right "
570 " "
571 "left: "
572 " %div = udiv i32 %x, %y "
573 " br label %right "
574 " "
575 "right: "
576 " %exitcond = icmp eq i32 %iv1.inc, %n "
577 " br i1 %exitcond, label %for.end.loopexit, label %loop "
578 " "
579 "for.end.loopexit: "
580 " br label %for.end "
581 " "
582 "for.end: "
583 " ret void "
584 "} ",
585 Err, C);
586
587 assert(M && "Could not parse module?");
588 assert(!verifyModule(*M) && "Must have been well formed!");
589
Sanjoy Dasb1227db2016-12-12 14:57:11 +0000590 runSCEVTest(*M, "f_1", [&](Function &F, DominatorTree &DT, LoopInfo &LI,
591 ScalarEvolution &SE) {
Sebastian Pop8c9cc8c2016-12-12 02:52:51 +0000592 SCEVExpander Expander(SE, M->getDataLayout(), "unittests");
593 auto *DivInst = getInstructionByName(F, "div");
594
595 {
596 auto *DivSCEV = SE.getSCEV(DivInst);
597 auto *DivExpansion = Expander.expandCodeFor(
598 DivSCEV, DivSCEV->getType(), DivInst->getParent()->getTerminator());
599 auto *DivExpansionInst = dyn_cast<Instruction>(DivExpansion);
600 ASSERT_NE(DivExpansionInst, nullptr);
601 EXPECT_EQ(DivInst->getParent(), DivExpansionInst->getParent());
602 }
603
604 {
605 auto *ArgY = getArgByName(F, "y");
606 auto *DivFromScratchSCEV =
607 SE.getUDivExpr(SE.getOne(ArgY->getType()), SE.getSCEV(ArgY));
608
609 auto *DivFromScratchExpansion = Expander.expandCodeFor(
610 DivFromScratchSCEV, DivFromScratchSCEV->getType(),
611 DivInst->getParent()->getTerminator());
612 auto *DivFromScratchExpansionInst =
613 dyn_cast<Instruction>(DivFromScratchExpansion);
614 ASSERT_NE(DivFromScratchExpansionInst, nullptr);
615 EXPECT_EQ(DivInst->getParent(), DivFromScratchExpansionInst->getParent());
616 }
Sanjoy Dasb1227db2016-12-12 14:57:11 +0000617
618 {
619 auto *ArgY = getArgByName(F, "y");
620 auto *One = SE.getOne(ArgY->getType());
621 auto *DivFromScratchSCEV = SE.getUDivExpr(One, SE.getSCEV(ArgY));
622 auto *L = LI.getLoopFor(DivInst->getParent());
623 auto *ARFromScratchSCEV =
624 SE.getAddRecExpr(DivFromScratchSCEV, One, L, SCEV::FlagAnyWrap);
625
626 Expander.disableCanonicalMode();
627
628 auto *ARFromScratchExpansion = Expander.expandCodeFor(
629 ARFromScratchSCEV, ARFromScratchSCEV->getType(),
630 DivInst->getParent()->getTerminator());
631 auto *ARFromScratchExpansionInst =
632 dyn_cast<Instruction>(ARFromScratchExpansion);
633 ASSERT_NE(ARFromScratchExpansionInst, nullptr);
634 ASSERT_FALSE(verifyFunction(F));
635 }
Sebastian Pop8c9cc8c2016-12-12 02:52:51 +0000636 });
637}
638
Dan Gohman7cac9572010-08-02 23:49:30 +0000639} // end anonymous namespace
640} // end namespace llvm