blob: 1d6ae6f6652b2b6508eda76488b221c4cb927c4d [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 }
Nick Lewycky287682e2011-10-04 06:51:26 +000053};
Dan Gohman7cac9572010-08-02 23:49:30 +000054
Nick Lewycky287682e2011-10-04 06:51:26 +000055TEST_F(ScalarEvolutionsTest, SCEVUnknownRAUW) {
Chris Lattner229907c2011-07-18 04:54:35 +000056 FunctionType *FTy = FunctionType::get(Type::getVoidTy(Context),
Jay Foadb804a2b2011-07-12 14:06:48 +000057 std::vector<Type *>(), false);
Dan Gohman7cac9572010-08-02 23:49:30 +000058 Function *F = cast<Function>(M.getOrInsertFunction("f", FTy));
59 BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
Craig Topper66f09ad2014-06-08 22:29:17 +000060 ReturnInst::Create(Context, nullptr, BB);
Dan Gohman7cac9572010-08-02 23:49:30 +000061
Chris Lattner229907c2011-07-18 04:54:35 +000062 Type *Ty = Type::getInt1Ty(Context);
Dan Gohman7cac9572010-08-02 23:49:30 +000063 Constant *Init = Constant::getNullValue(Ty);
64 Value *V0 = new GlobalVariable(M, Ty, false, GlobalValue::ExternalLinkage, Init, "V0");
65 Value *V1 = new GlobalVariable(M, Ty, false, GlobalValue::ExternalLinkage, Init, "V1");
66 Value *V2 = new GlobalVariable(M, Ty, false, GlobalValue::ExternalLinkage, Init, "V2");
67
Chandler Carruth2f1fd162015-08-17 02:08:17 +000068 ScalarEvolution SE = buildSE(*F);
Dan Gohman7cac9572010-08-02 23:49:30 +000069
70 const SCEV *S0 = SE.getSCEV(V0);
71 const SCEV *S1 = SE.getSCEV(V1);
72 const SCEV *S2 = SE.getSCEV(V2);
73
74 const SCEV *P0 = SE.getAddExpr(S0, S0);
75 const SCEV *P1 = SE.getAddExpr(S1, S1);
76 const SCEV *P2 = SE.getAddExpr(S2, S2);
77
78 const SCEVMulExpr *M0 = cast<SCEVMulExpr>(P0);
79 const SCEVMulExpr *M1 = cast<SCEVMulExpr>(P1);
80 const SCEVMulExpr *M2 = cast<SCEVMulExpr>(P2);
81
82 EXPECT_EQ(cast<SCEVConstant>(M0->getOperand(0))->getValue()->getZExtValue(),
83 2u);
84 EXPECT_EQ(cast<SCEVConstant>(M1->getOperand(0))->getValue()->getZExtValue(),
85 2u);
86 EXPECT_EQ(cast<SCEVConstant>(M2->getOperand(0))->getValue()->getZExtValue(),
87 2u);
88
89 // Before the RAUWs, these are all pointing to separate values.
90 EXPECT_EQ(cast<SCEVUnknown>(M0->getOperand(1))->getValue(), V0);
91 EXPECT_EQ(cast<SCEVUnknown>(M1->getOperand(1))->getValue(), V1);
92 EXPECT_EQ(cast<SCEVUnknown>(M2->getOperand(1))->getValue(), V2);
93
94 // Do some RAUWs.
95 V2->replaceAllUsesWith(V1);
96 V1->replaceAllUsesWith(V0);
97
98 // After the RAUWs, these should all be pointing to V0.
99 EXPECT_EQ(cast<SCEVUnknown>(M0->getOperand(1))->getValue(), V0);
100 EXPECT_EQ(cast<SCEVUnknown>(M1->getOperand(1))->getValue(), V0);
101 EXPECT_EQ(cast<SCEVUnknown>(M2->getOperand(1))->getValue(), V0);
Nick Lewycky287682e2011-10-04 06:51:26 +0000102}
Dan Gohman7cac9572010-08-02 23:49:30 +0000103
Nick Lewycky287682e2011-10-04 06:51:26 +0000104TEST_F(ScalarEvolutionsTest, SCEVMultiplyAddRecs) {
105 Type *Ty = Type::getInt32Ty(Context);
106 SmallVector<Type *, 10> Types;
107 Types.append(10, Ty);
108 FunctionType *FTy = FunctionType::get(Type::getVoidTy(Context), Types, false);
109 Function *F = cast<Function>(M.getOrInsertFunction("f", FTy));
110 BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
Craig Topper66f09ad2014-06-08 22:29:17 +0000111 ReturnInst::Create(Context, nullptr, BB);
Nick Lewycky287682e2011-10-04 06:51:26 +0000112
Chandler Carruth2f1fd162015-08-17 02:08:17 +0000113 ScalarEvolution SE = buildSE(*F);
Nick Lewycky287682e2011-10-04 06:51:26 +0000114
115 // It's possible to produce an empty loop through the default constructor,
116 // but you can't add any blocks to it without a LoopInfo pass.
117 Loop L;
118 const_cast<std::vector<BasicBlock*>&>(L.getBlocks()).push_back(BB);
119
120 Function::arg_iterator AI = F->arg_begin();
121 SmallVector<const SCEV *, 5> A;
122 A.push_back(SE.getSCEV(&*AI++));
123 A.push_back(SE.getSCEV(&*AI++));
124 A.push_back(SE.getSCEV(&*AI++));
125 A.push_back(SE.getSCEV(&*AI++));
126 A.push_back(SE.getSCEV(&*AI++));
127 const SCEV *A_rec = SE.getAddRecExpr(A, &L, SCEV::FlagAnyWrap);
128
129 SmallVector<const SCEV *, 5> B;
130 B.push_back(SE.getSCEV(&*AI++));
131 B.push_back(SE.getSCEV(&*AI++));
132 B.push_back(SE.getSCEV(&*AI++));
133 B.push_back(SE.getSCEV(&*AI++));
134 B.push_back(SE.getSCEV(&*AI++));
135 const SCEV *B_rec = SE.getAddRecExpr(B, &L, SCEV::FlagAnyWrap);
136
137 /* Spot check that we perform this transformation:
138 {A0,+,A1,+,A2,+,A3,+,A4} * {B0,+,B1,+,B2,+,B3,+,B4} =
139 {A0*B0,+,
140 A1*B0 + A0*B1 + A1*B1,+,
141 A2*B0 + 2A1*B1 + A0*B2 + 2A2*B1 + 2A1*B2 + A2*B2,+,
142 A3*B0 + 3A2*B1 + 3A1*B2 + A0*B3 + 3A3*B1 + 6A2*B2 + 3A1*B3 + 3A3*B2 +
143 3A2*B3 + A3*B3,+,
144 A4*B0 + 4A3*B1 + 6A2*B2 + 4A1*B3 + A0*B4 + 4A4*B1 + 12A3*B2 + 12A2*B3 +
145 4A1*B4 + 6A4*B2 + 12A3*B3 + 6A2*B4 + 4A4*B3 + 4A3*B4 + A4*B4,+,
146 5A4*B1 + 10A3*B2 + 10A2*B3 + 5A1*B4 + 20A4*B2 + 30A3*B3 + 20A2*B4 +
147 30A4*B3 + 30A3*B4 + 20A4*B4,+,
148 15A4*B2 + 20A3*B3 + 15A2*B4 + 60A4*B3 + 60A3*B4 + 90A4*B4,+,
149 35A4*B3 + 35A3*B4 + 140A4*B4,+,
150 70A4*B4}
151 */
152
153 const SCEVAddRecExpr *Product =
154 dyn_cast<SCEVAddRecExpr>(SE.getMulExpr(A_rec, B_rec));
155 ASSERT_TRUE(Product);
156 ASSERT_EQ(Product->getNumOperands(), 9u);
157
158 SmallVector<const SCEV *, 16> Sum;
159 Sum.push_back(SE.getMulExpr(A[0], B[0]));
160 EXPECT_EQ(Product->getOperand(0), SE.getAddExpr(Sum));
161 Sum.clear();
162
163 // SCEV produces different an equal but different expression for these.
164 // Re-enable when PR11052 is fixed.
165#if 0
166 Sum.push_back(SE.getMulExpr(A[1], B[0]));
167 Sum.push_back(SE.getMulExpr(A[0], B[1]));
168 Sum.push_back(SE.getMulExpr(A[1], B[1]));
169 EXPECT_EQ(Product->getOperand(1), SE.getAddExpr(Sum));
170 Sum.clear();
171
172 Sum.push_back(SE.getMulExpr(A[2], B[0]));
173 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 2), A[1], B[1]));
174 Sum.push_back(SE.getMulExpr(A[0], B[2]));
175 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 2), A[2], B[1]));
176 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 2), A[1], B[2]));
177 Sum.push_back(SE.getMulExpr(A[2], B[2]));
178 EXPECT_EQ(Product->getOperand(2), SE.getAddExpr(Sum));
179 Sum.clear();
180
181 Sum.push_back(SE.getMulExpr(A[3], B[0]));
182 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 3), A[2], B[1]));
183 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 3), A[1], B[2]));
184 Sum.push_back(SE.getMulExpr(A[0], B[3]));
185 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 3), A[3], B[1]));
186 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 6), A[2], B[2]));
187 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 3), A[1], B[3]));
188 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 3), A[3], B[2]));
189 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 3), A[2], B[3]));
190 Sum.push_back(SE.getMulExpr(A[3], B[3]));
191 EXPECT_EQ(Product->getOperand(3), SE.getAddExpr(Sum));
192 Sum.clear();
193
194 Sum.push_back(SE.getMulExpr(A[4], B[0]));
195 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 4), A[3], B[1]));
196 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 6), A[2], B[2]));
197 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 4), A[1], B[3]));
198 Sum.push_back(SE.getMulExpr(A[0], B[4]));
199 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 4), A[4], B[1]));
200 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 12), A[3], B[2]));
201 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 12), A[2], B[3]));
202 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 4), A[1], B[4]));
203 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 6), A[4], B[2]));
204 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 12), A[3], B[3]));
205 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 6), A[2], B[4]));
206 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 4), A[4], B[3]));
207 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 4), A[3], B[4]));
208 Sum.push_back(SE.getMulExpr(A[4], B[4]));
209 EXPECT_EQ(Product->getOperand(4), SE.getAddExpr(Sum));
210 Sum.clear();
211
212 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 5), A[4], B[1]));
213 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 10), A[3], B[2]));
214 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 10), A[2], B[3]));
215 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 5), A[1], B[4]));
216 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 20), A[4], B[2]));
217 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 30), A[3], B[3]));
218 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 20), A[2], B[4]));
219 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 30), A[4], B[3]));
220 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 30), A[3], B[4]));
221 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 20), A[4], B[4]));
222 EXPECT_EQ(Product->getOperand(5), SE.getAddExpr(Sum));
223 Sum.clear();
224
225 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 15), A[4], B[2]));
226 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 20), A[3], B[3]));
227 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 15), A[2], B[4]));
228 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 60), A[4], B[3]));
229 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 60), A[3], B[4]));
230 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 90), A[4], B[4]));
231 EXPECT_EQ(Product->getOperand(6), SE.getAddExpr(Sum));
232 Sum.clear();
233
234 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 35), A[4], B[3]));
235 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 35), A[3], B[4]));
236 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 140), A[4], B[4]));
237 EXPECT_EQ(Product->getOperand(7), SE.getAddExpr(Sum));
238 Sum.clear();
239#endif
240
241 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 70), A[4], B[4]));
242 EXPECT_EQ(Product->getOperand(8), SE.getAddExpr(Sum));
Dan Gohman7cac9572010-08-02 23:49:30 +0000243}
244
Tobias Grosser11332e52016-02-21 17:42:10 +0000245TEST_F(ScalarEvolutionsTest, SimplifiedPHI) {
246 FunctionType *FTy = FunctionType::get(Type::getVoidTy(Context),
247 std::vector<Type *>(), false);
248 Function *F = cast<Function>(M.getOrInsertFunction("f", FTy));
249 BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", F);
250 BasicBlock *LoopBB = BasicBlock::Create(Context, "loop", F);
251 BasicBlock *ExitBB = BasicBlock::Create(Context, "exit", F);
252 BranchInst::Create(LoopBB, EntryBB);
253 BranchInst::Create(LoopBB, ExitBB, UndefValue::get(Type::getInt1Ty(Context)),
254 LoopBB);
255 ReturnInst::Create(Context, nullptr, ExitBB);
256 auto *Ty = Type::getInt32Ty(Context);
257 auto *PN = PHINode::Create(Ty, 2, "", &*LoopBB->begin());
258 PN->addIncoming(Constant::getNullValue(Ty), EntryBB);
259 PN->addIncoming(UndefValue::get(Ty), LoopBB);
260 ScalarEvolution SE = buildSE(*F);
261 auto *S1 = SE.getSCEV(PN);
262 auto *S2 = SE.getSCEV(PN);
Tobias Grosser946ca0a2016-02-22 07:20:40 +0000263 auto *ZeroConst = SE.getConstant(Ty, 0);
Tobias Grosser11332e52016-02-21 17:42:10 +0000264
265 // At some point, only the first call to getSCEV returned the simplified
266 // SCEVConstant and later calls just returned a SCEVUnknown referencing the
267 // PHI node.
Tobias Grosser946ca0a2016-02-22 07:20:40 +0000268 EXPECT_EQ(S1, ZeroConst);
269 EXPECT_EQ(S1, S2);
Tobias Grosser11332e52016-02-21 17:42:10 +0000270}
271
Wei Mi3076cc32016-09-15 04:06:44 +0000272TEST_F(ScalarEvolutionsTest, ExpandPtrTypeSCEV) {
273 // It is to test the fix for PR30213. It exercises the branch in scev
274 // expansion when the value in ValueOffsetPair is a ptr and the offset
275 // is not divisible by the elem type size of value.
276 auto *I8Ty = Type::getInt8Ty(Context);
277 auto *I8PtrTy = Type::getInt8PtrTy(Context);
278 auto *I32Ty = Type::getInt32Ty(Context);
279 auto *I32PtrTy = Type::getInt32PtrTy(Context);
280 FunctionType *FTy =
281 FunctionType::get(Type::getVoidTy(Context), std::vector<Type *>(), false);
282 Function *F = cast<Function>(M.getOrInsertFunction("f", FTy));
283 BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", F);
284 BasicBlock *LoopBB = BasicBlock::Create(Context, "loop", F);
285 BasicBlock *ExitBB = BasicBlock::Create(Context, "exit", F);
286 BranchInst::Create(LoopBB, EntryBB);
287 ReturnInst::Create(Context, nullptr, ExitBB);
288
289 // loop: ; preds = %loop, %entry
290 // %alloca = alloca i32
291 // %gep0 = getelementptr i32, i32* %alloca, i32 1
292 // %bitcast1 = bitcast i32* %gep0 to i8*
293 // %gep1 = getelementptr i8, i8* %bitcast1, i32 1
294 // %gep2 = getelementptr i8, i8* undef, i32 1
295 // %cmp = icmp ult i8* undef, %bitcast1
296 // %select = select i1 %cmp, i8* %gep1, i8* %gep2
297 // %bitcast2 = bitcast i8* %select to i32*
298 // br i1 undef, label %loop, label %exit
299
300 BranchInst *Br = BranchInst::Create(
301 LoopBB, ExitBB, UndefValue::get(Type::getInt1Ty(Context)), LoopBB);
302 AllocaInst *Alloca = new AllocaInst(I32Ty, "alloca", Br);
303 ConstantInt *Ci32 = ConstantInt::get(Context, APInt(32, 1));
304 GetElementPtrInst *Gep0 =
305 GetElementPtrInst::Create(I32Ty, Alloca, Ci32, "gep0", Br);
306 CastInst *CastA =
307 CastInst::CreateBitOrPointerCast(Gep0, I8PtrTy, "bitcast1", Br);
308 GetElementPtrInst *Gep1 =
309 GetElementPtrInst::Create(I8Ty, CastA, Ci32, "gep1", Br);
310 GetElementPtrInst *Gep2 = GetElementPtrInst::Create(
311 I8Ty, UndefValue::get(I8PtrTy), Ci32, "gep2", Br);
312 CmpInst *Cmp = CmpInst::Create(Instruction::ICmp, CmpInst::ICMP_ULT,
313 UndefValue::get(I8PtrTy), CastA, "cmp", Br);
314 SelectInst *Sel = SelectInst::Create(Cmp, Gep1, Gep2, "select", Br);
315 CastInst *CastB =
316 CastInst::CreateBitOrPointerCast(Sel, I32PtrTy, "bitcast2", Br);
317
318 ScalarEvolution SE = buildSE(*F);
319 auto *S = SE.getSCEV(CastB);
320 SCEVExpander Exp(SE, M.getDataLayout(), "expander");
321 Value *V =
322 Exp.expandCodeFor(cast<SCEVAddExpr>(S)->getOperand(1), nullptr, Br);
323
324 // Expect the expansion code contains:
325 // %0 = bitcast i32* %bitcast2 to i8*
326 // %uglygep = getelementptr i8, i8* %0, i64 -1
327 // %1 = bitcast i8* %uglygep to i32*
328 EXPECT_TRUE(isa<BitCastInst>(V));
329 Instruction *Gep = cast<Instruction>(V)->getPrevNode();
330 EXPECT_TRUE(isa<GetElementPtrInst>(Gep));
331 EXPECT_TRUE(isa<ConstantInt>(Gep->getOperand(1)));
332 EXPECT_EQ(cast<ConstantInt>(Gep->getOperand(1))->getSExtValue(), -1);
333 EXPECT_TRUE(isa<BitCastInst>(Gep->getPrevNode()));
334}
335
Sanjoy Dasb53021d2016-10-30 23:52:50 +0000336static Instruction *getInstructionByName(Function &F, StringRef Name) {
337 for (auto &I : instructions(F))
338 if (I.getName() == Name)
339 return &I;
Sanjoy Das507dd402016-10-18 17:45:16 +0000340 llvm_unreachable("Expected to find instruction!");
341}
342
343TEST_F(ScalarEvolutionsTest, CommutativeExprOperandOrder) {
344 LLVMContext C;
345 SMDiagnostic Err;
346 std::unique_ptr<Module> M = parseAssemblyString(
347 "target datalayout = \"e-m:e-p:32:32-f64:32:64-f80:32-n8:16:32-S128\" "
Sanjoy Dasb53021d2016-10-30 23:52:50 +0000348 " "
Sanjoy Das299e6722016-10-30 23:52:56 +0000349 "@var_0 = external global i32, align 4"
350 "@var_1 = external global i32, align 4"
351 " "
Sanjoy Das17078692016-10-31 03:32:43 +0000352 "declare i32 @unknown(i32, i32, i32)"
353 " "
Sanjoy Dasb53021d2016-10-30 23:52:50 +0000354 "define void @f_1(i8* nocapture %arr, i32 %n, i32* %A, i32* %B) "
Sanjoy Das507dd402016-10-18 17:45:16 +0000355 " local_unnamed_addr { "
356 "entry: "
357 " %entrycond = icmp sgt i32 %n, 0 "
358 " br i1 %entrycond, label %loop.ph, label %for.end "
359 " "
360 "loop.ph: "
361 " %a = load i32, i32* %A, align 4 "
362 " %b = load i32, i32* %B, align 4 "
363 " %mul = mul nsw i32 %b, %a "
364 " %iv0.init = getelementptr inbounds i8, i8* %arr, i32 %mul "
365 " br label %loop "
366 " "
367 "loop: "
368 " %iv0 = phi i8* [ %iv0.inc, %loop ], [ %iv0.init, %loop.ph ] "
369 " %iv1 = phi i32 [ %iv1.inc, %loop ], [ 0, %loop.ph ] "
370 " %conv = trunc i32 %iv1 to i8 "
371 " store i8 %conv, i8* %iv0, align 1 "
372 " %iv0.inc = getelementptr inbounds i8, i8* %iv0, i32 %b "
373 " %iv1.inc = add nuw nsw i32 %iv1, 1 "
374 " %exitcond = icmp eq i32 %iv1.inc, %n "
375 " br i1 %exitcond, label %for.end.loopexit, label %loop "
376 " "
377 "for.end.loopexit: "
378 " br label %for.end "
379 " "
380 "for.end: "
381 " ret void "
382 "} "
383 " "
Sanjoy Dasb53021d2016-10-30 23:52:50 +0000384 "define void @f_2(i32* %X, i32* %Y, i32* %Z) { "
Sanjoy Das507dd402016-10-18 17:45:16 +0000385 " %x = load i32, i32* %X "
386 " %y = load i32, i32* %Y "
387 " %z = load i32, i32* %Z "
388 " ret void "
Sanjoy Das299e6722016-10-30 23:52:56 +0000389 "} "
390 " "
Sanjoy Das299e6722016-10-30 23:52:56 +0000391 "define void @f_3() { "
392 " %x = load i32, i32* @var_0"
393 " %y = load i32, i32* @var_1"
394 " ret void"
395 "} "
Sanjoy Das17078692016-10-31 03:32:43 +0000396 " "
397 "define void @f_4(i32 %a, i32 %b, i32 %c) { "
398 " %x = call i32 @unknown(i32 %a, i32 %b, i32 %c)"
399 " %y = call i32 @unknown(i32 %b, i32 %c, i32 %a)"
400 " %z = call i32 @unknown(i32 %c, i32 %a, i32 %b)"
401 " ret void"
402 "} "
Sanjoy Das299e6722016-10-30 23:52:56 +0000403 ,
Sanjoy Das507dd402016-10-18 17:45:16 +0000404 Err, C);
405
406 assert(M && "Could not parse module?");
407 assert(!verifyModule(*M) && "Must have been well formed!");
408
Sanjoy Das3d6e3df2016-10-31 03:32:39 +0000409 auto RunWithFunctionAndSE =
410 [&](StringRef FuncName,
411 function_ref<void(Function &F, ScalarEvolution& SE)> Test) {
412 auto *F = M->getFunction(FuncName);
413 ASSERT_NE(F, nullptr) << "Could not find " << FuncName;
414 ScalarEvolution SE = buildSE(*F);
415 Test(*F, SE);
416 };
Sanjoy Das507dd402016-10-18 17:45:16 +0000417
Sanjoy Das3d6e3df2016-10-31 03:32:39 +0000418 RunWithFunctionAndSE("f_1", [&](Function &F, ScalarEvolution &SE) {
419 auto *IV0 = getInstructionByName(F, "iv0");
420 auto *IV0Inc = getInstructionByName(F, "iv0.inc");
Sanjoy Das507dd402016-10-18 17:45:16 +0000421
Sanjoy Das507dd402016-10-18 17:45:16 +0000422 auto *FirstExprForIV0 = SE.getSCEV(IV0);
423 auto *FirstExprForIV0Inc = SE.getSCEV(IV0Inc);
424 auto *SecondExprForIV0 = SE.getSCEV(IV0);
425
426 EXPECT_TRUE(isa<SCEVAddRecExpr>(FirstExprForIV0));
427 EXPECT_TRUE(isa<SCEVAddRecExpr>(FirstExprForIV0Inc));
428 EXPECT_TRUE(isa<SCEVAddRecExpr>(SecondExprForIV0));
Sanjoy Das3d6e3df2016-10-31 03:32:39 +0000429 });
Sanjoy Das507dd402016-10-18 17:45:16 +0000430
Sanjoy Das17078692016-10-31 03:32:43 +0000431 auto CheckCommutativeMulExprs = [&](ScalarEvolution &SE, const SCEV *A,
432 const SCEV *B, const SCEV *C) {
433 EXPECT_EQ(SE.getMulExpr(A, B), SE.getMulExpr(B, A));
434 EXPECT_EQ(SE.getMulExpr(B, C), SE.getMulExpr(C, B));
435 EXPECT_EQ(SE.getMulExpr(A, C), SE.getMulExpr(C, A));
Sanjoy Das507dd402016-10-18 17:45:16 +0000436
Sanjoy Das17078692016-10-31 03:32:43 +0000437 SmallVector<const SCEV *, 3> Ops0 = {A, B, C};
438 SmallVector<const SCEV *, 3> Ops1 = {A, C, B};
439 SmallVector<const SCEV *, 3> Ops2 = {B, A, C};
440 SmallVector<const SCEV *, 3> Ops3 = {B, C, A};
441 SmallVector<const SCEV *, 3> Ops4 = {C, B, A};
442 SmallVector<const SCEV *, 3> Ops5 = {C, A, B};
Sanjoy Das507dd402016-10-18 17:45:16 +0000443
444 auto *Mul0 = SE.getMulExpr(Ops0);
445 auto *Mul1 = SE.getMulExpr(Ops1);
446 auto *Mul2 = SE.getMulExpr(Ops2);
447 auto *Mul3 = SE.getMulExpr(Ops3);
448 auto *Mul4 = SE.getMulExpr(Ops4);
449 auto *Mul5 = SE.getMulExpr(Ops5);
450
Sanjoy Das17078692016-10-31 03:32:43 +0000451 EXPECT_EQ(Mul0, Mul1) << "Expected " << *Mul0 << " == " << *Mul1;
452 EXPECT_EQ(Mul1, Mul2) << "Expected " << *Mul1 << " == " << *Mul2;
453 EXPECT_EQ(Mul2, Mul3) << "Expected " << *Mul2 << " == " << *Mul3;
454 EXPECT_EQ(Mul3, Mul4) << "Expected " << *Mul3 << " == " << *Mul4;
455 EXPECT_EQ(Mul4, Mul5) << "Expected " << *Mul4 << " == " << *Mul5;
456 };
457
458 RunWithFunctionAndSE("f_2", [&](Function &F, ScalarEvolution &SE) {
459 CheckCommutativeMulExprs(SE, SE.getSCEV(getInstructionByName(F, "x")),
460 SE.getSCEV(getInstructionByName(F, "y")),
461 SE.getSCEV(getInstructionByName(F, "z")));
Sanjoy Das3d6e3df2016-10-31 03:32:39 +0000462 });
Sanjoy Das299e6722016-10-30 23:52:56 +0000463
Sanjoy Das3d6e3df2016-10-31 03:32:39 +0000464 RunWithFunctionAndSE("f_3", [&](Function &F, ScalarEvolution &SE) {
465 auto *LoadArg0 = SE.getSCEV(getInstructionByName(F, "x"));
466 auto *LoadArg1 = SE.getSCEV(getInstructionByName(F, "y"));
Sanjoy Das299e6722016-10-30 23:52:56 +0000467
468 auto *MulA = SE.getMulExpr(LoadArg0, LoadArg1);
469 auto *MulB = SE.getMulExpr(LoadArg1, LoadArg0);
470
471 EXPECT_EQ(MulA, MulB) << "MulA = " << *MulA << ", MulB = " << *MulB;
Sanjoy Das3d6e3df2016-10-31 03:32:39 +0000472 });
Sanjoy Das17078692016-10-31 03:32:43 +0000473
474 RunWithFunctionAndSE("f_4", [&](Function &F, ScalarEvolution &SE) {
475 CheckCommutativeMulExprs(SE, SE.getSCEV(getInstructionByName(F, "x")),
476 SE.getSCEV(getInstructionByName(F, "y")),
477 SE.getSCEV(getInstructionByName(F, "z")));
478 });
Sanjoy Das507dd402016-10-18 17:45:16 +0000479}
480
Dan Gohman7cac9572010-08-02 23:49:30 +0000481} // end anonymous namespace
482} // end namespace llvm