blob: 94850a030c7ba2554e5bb96bc2e8a3648aec371a [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
Keno Fischer090f1952017-05-27 03:22:55 +000010#include "llvm/ADT/SmallVector.h"
Daniel Jasperaec2fa32016-12-19 08:22:17 +000011#include "llvm/Analysis/AssumptionCache.h"
Chandler Carruth2f1fd162015-08-17 02:08:17 +000012#include "llvm/Analysis/LoopInfo.h"
Keno Fischer090f1952017-05-27 03:22:55 +000013#include "llvm/Analysis/ScalarEvolutionExpander.h"
14#include "llvm/Analysis/ScalarEvolutionExpressions.h"
15#include "llvm/Analysis/TargetLibraryInfo.h"
Sanjoy Das507dd402016-10-18 17:45:16 +000016#include "llvm/AsmParser/Parser.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"
Keno Fischer090f1952017-05-27 03:22:55 +000020#include "llvm/IR/IRBuilder.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"
Chandler Carruth30d69c22015-02-13 10:01:29 +000023#include "llvm/IR/LegacyPassManager.h"
Keno Fischer090f1952017-05-27 03:22:55 +000024#include "llvm/IR/Module.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
Daniel Jasperaec2fa32016-12-19 08:22:17 +000041 std::unique_ptr<AssumptionCache> AC;
Chandler Carruth2f1fd162015-08-17 02:08:17 +000042 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) {
Daniel Jasperaec2fa32016-12-19 08:22:17 +000048 AC.reset(new AssumptionCache(F));
Chandler Carruth2f1fd162015-08-17 02:08:17 +000049 DT.reset(new DominatorTree(F));
50 LI.reset(new LoopInfo(*DT));
Daniel Jasperaec2fa32016-12-19 08:22:17 +000051 return ScalarEvolution(F, TLI, *AC, *DT, *LI);
Chandler Carruth2f1fd162015-08-17 02:08:17 +000052 }
Sanjoy Das6764b9a2016-11-10 07:56:05 +000053
Sanjoy Das044f9562017-04-14 23:47:53 +000054 void runWithSE(
Sanjoy Dasb1227db2016-12-12 14:57:11 +000055 Module &M, StringRef FuncName,
Sanjoy Das044f9562017-04-14 23:47:53 +000056 function_ref<void(Function &F, LoopInfo &LI, ScalarEvolution &SE)> Test) {
Reid Kleckner30422ee2016-12-12 18:52:32 +000057 auto *F = M.getFunction(FuncName);
58 ASSERT_NE(F, nullptr) << "Could not find " << FuncName;
59 ScalarEvolution SE = buildSE(*F);
Sanjoy Das044f9562017-04-14 23:47:53 +000060 Test(*F, *LI, SE);
Sanjoy Das6764b9a2016-11-10 07:56:05 +000061 }
Nick Lewycky287682e2011-10-04 06:51:26 +000062};
Dan Gohman7cac9572010-08-02 23:49:30 +000063
Nick Lewycky287682e2011-10-04 06:51:26 +000064TEST_F(ScalarEvolutionsTest, SCEVUnknownRAUW) {
Chris Lattner229907c2011-07-18 04:54:35 +000065 FunctionType *FTy = FunctionType::get(Type::getVoidTy(Context),
Jay Foadb804a2b2011-07-12 14:06:48 +000066 std::vector<Type *>(), false);
Dan Gohman7cac9572010-08-02 23:49:30 +000067 Function *F = cast<Function>(M.getOrInsertFunction("f", FTy));
68 BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
Craig Topper66f09ad2014-06-08 22:29:17 +000069 ReturnInst::Create(Context, nullptr, BB);
Dan Gohman7cac9572010-08-02 23:49:30 +000070
Chris Lattner229907c2011-07-18 04:54:35 +000071 Type *Ty = Type::getInt1Ty(Context);
Dan Gohman7cac9572010-08-02 23:49:30 +000072 Constant *Init = Constant::getNullValue(Ty);
73 Value *V0 = new GlobalVariable(M, Ty, false, GlobalValue::ExternalLinkage, Init, "V0");
74 Value *V1 = new GlobalVariable(M, Ty, false, GlobalValue::ExternalLinkage, Init, "V1");
75 Value *V2 = new GlobalVariable(M, Ty, false, GlobalValue::ExternalLinkage, Init, "V2");
76
Chandler Carruth2f1fd162015-08-17 02:08:17 +000077 ScalarEvolution SE = buildSE(*F);
Dan Gohman7cac9572010-08-02 23:49:30 +000078
79 const SCEV *S0 = SE.getSCEV(V0);
80 const SCEV *S1 = SE.getSCEV(V1);
81 const SCEV *S2 = SE.getSCEV(V2);
82
83 const SCEV *P0 = SE.getAddExpr(S0, S0);
84 const SCEV *P1 = SE.getAddExpr(S1, S1);
85 const SCEV *P2 = SE.getAddExpr(S2, S2);
86
87 const SCEVMulExpr *M0 = cast<SCEVMulExpr>(P0);
88 const SCEVMulExpr *M1 = cast<SCEVMulExpr>(P1);
89 const SCEVMulExpr *M2 = cast<SCEVMulExpr>(P2);
90
91 EXPECT_EQ(cast<SCEVConstant>(M0->getOperand(0))->getValue()->getZExtValue(),
92 2u);
93 EXPECT_EQ(cast<SCEVConstant>(M1->getOperand(0))->getValue()->getZExtValue(),
94 2u);
95 EXPECT_EQ(cast<SCEVConstant>(M2->getOperand(0))->getValue()->getZExtValue(),
96 2u);
97
98 // Before the RAUWs, these are all pointing to separate values.
99 EXPECT_EQ(cast<SCEVUnknown>(M0->getOperand(1))->getValue(), V0);
100 EXPECT_EQ(cast<SCEVUnknown>(M1->getOperand(1))->getValue(), V1);
101 EXPECT_EQ(cast<SCEVUnknown>(M2->getOperand(1))->getValue(), V2);
102
103 // Do some RAUWs.
104 V2->replaceAllUsesWith(V1);
105 V1->replaceAllUsesWith(V0);
106
107 // After the RAUWs, these should all be pointing to V0.
108 EXPECT_EQ(cast<SCEVUnknown>(M0->getOperand(1))->getValue(), V0);
109 EXPECT_EQ(cast<SCEVUnknown>(M1->getOperand(1))->getValue(), V0);
110 EXPECT_EQ(cast<SCEVUnknown>(M2->getOperand(1))->getValue(), V0);
Nick Lewycky287682e2011-10-04 06:51:26 +0000111}
Dan Gohman7cac9572010-08-02 23:49:30 +0000112
Nick Lewycky287682e2011-10-04 06:51:26 +0000113TEST_F(ScalarEvolutionsTest, SCEVMultiplyAddRecs) {
114 Type *Ty = Type::getInt32Ty(Context);
115 SmallVector<Type *, 10> Types;
116 Types.append(10, Ty);
117 FunctionType *FTy = FunctionType::get(Type::getVoidTy(Context), Types, false);
118 Function *F = cast<Function>(M.getOrInsertFunction("f", FTy));
119 BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
Craig Topper66f09ad2014-06-08 22:29:17 +0000120 ReturnInst::Create(Context, nullptr, BB);
Nick Lewycky287682e2011-10-04 06:51:26 +0000121
Chandler Carruth2f1fd162015-08-17 02:08:17 +0000122 ScalarEvolution SE = buildSE(*F);
Nick Lewycky287682e2011-10-04 06:51:26 +0000123
124 // It's possible to produce an empty loop through the default constructor,
125 // but you can't add any blocks to it without a LoopInfo pass.
126 Loop L;
127 const_cast<std::vector<BasicBlock*>&>(L.getBlocks()).push_back(BB);
128
129 Function::arg_iterator AI = F->arg_begin();
130 SmallVector<const SCEV *, 5> A;
131 A.push_back(SE.getSCEV(&*AI++));
132 A.push_back(SE.getSCEV(&*AI++));
133 A.push_back(SE.getSCEV(&*AI++));
134 A.push_back(SE.getSCEV(&*AI++));
135 A.push_back(SE.getSCEV(&*AI++));
136 const SCEV *A_rec = SE.getAddRecExpr(A, &L, SCEV::FlagAnyWrap);
137
138 SmallVector<const SCEV *, 5> B;
139 B.push_back(SE.getSCEV(&*AI++));
140 B.push_back(SE.getSCEV(&*AI++));
141 B.push_back(SE.getSCEV(&*AI++));
142 B.push_back(SE.getSCEV(&*AI++));
143 B.push_back(SE.getSCEV(&*AI++));
144 const SCEV *B_rec = SE.getAddRecExpr(B, &L, SCEV::FlagAnyWrap);
145
146 /* Spot check that we perform this transformation:
147 {A0,+,A1,+,A2,+,A3,+,A4} * {B0,+,B1,+,B2,+,B3,+,B4} =
148 {A0*B0,+,
149 A1*B0 + A0*B1 + A1*B1,+,
150 A2*B0 + 2A1*B1 + A0*B2 + 2A2*B1 + 2A1*B2 + A2*B2,+,
151 A3*B0 + 3A2*B1 + 3A1*B2 + A0*B3 + 3A3*B1 + 6A2*B2 + 3A1*B3 + 3A3*B2 +
152 3A2*B3 + A3*B3,+,
153 A4*B0 + 4A3*B1 + 6A2*B2 + 4A1*B3 + A0*B4 + 4A4*B1 + 12A3*B2 + 12A2*B3 +
154 4A1*B4 + 6A4*B2 + 12A3*B3 + 6A2*B4 + 4A4*B3 + 4A3*B4 + A4*B4,+,
155 5A4*B1 + 10A3*B2 + 10A2*B3 + 5A1*B4 + 20A4*B2 + 30A3*B3 + 20A2*B4 +
156 30A4*B3 + 30A3*B4 + 20A4*B4,+,
157 15A4*B2 + 20A3*B3 + 15A2*B4 + 60A4*B3 + 60A3*B4 + 90A4*B4,+,
158 35A4*B3 + 35A3*B4 + 140A4*B4,+,
159 70A4*B4}
160 */
161
162 const SCEVAddRecExpr *Product =
163 dyn_cast<SCEVAddRecExpr>(SE.getMulExpr(A_rec, B_rec));
164 ASSERT_TRUE(Product);
165 ASSERT_EQ(Product->getNumOperands(), 9u);
166
167 SmallVector<const SCEV *, 16> Sum;
168 Sum.push_back(SE.getMulExpr(A[0], B[0]));
169 EXPECT_EQ(Product->getOperand(0), SE.getAddExpr(Sum));
170 Sum.clear();
171
172 // SCEV produces different an equal but different expression for these.
173 // Re-enable when PR11052 is fixed.
174#if 0
175 Sum.push_back(SE.getMulExpr(A[1], B[0]));
176 Sum.push_back(SE.getMulExpr(A[0], B[1]));
177 Sum.push_back(SE.getMulExpr(A[1], B[1]));
178 EXPECT_EQ(Product->getOperand(1), SE.getAddExpr(Sum));
179 Sum.clear();
180
181 Sum.push_back(SE.getMulExpr(A[2], B[0]));
182 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 2), A[1], B[1]));
183 Sum.push_back(SE.getMulExpr(A[0], B[2]));
184 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 2), A[2], B[1]));
185 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 2), A[1], B[2]));
186 Sum.push_back(SE.getMulExpr(A[2], B[2]));
187 EXPECT_EQ(Product->getOperand(2), SE.getAddExpr(Sum));
188 Sum.clear();
189
190 Sum.push_back(SE.getMulExpr(A[3], B[0]));
191 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 3), A[2], B[1]));
192 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 3), A[1], B[2]));
193 Sum.push_back(SE.getMulExpr(A[0], B[3]));
194 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 3), A[3], B[1]));
195 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 6), A[2], B[2]));
196 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 3), A[1], B[3]));
197 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 3), A[3], B[2]));
198 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 3), A[2], B[3]));
199 Sum.push_back(SE.getMulExpr(A[3], B[3]));
200 EXPECT_EQ(Product->getOperand(3), SE.getAddExpr(Sum));
201 Sum.clear();
202
203 Sum.push_back(SE.getMulExpr(A[4], B[0]));
204 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 4), A[3], B[1]));
205 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 6), A[2], B[2]));
206 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 4), A[1], B[3]));
207 Sum.push_back(SE.getMulExpr(A[0], B[4]));
208 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 4), A[4], B[1]));
209 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 12), A[3], B[2]));
210 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 12), A[2], B[3]));
211 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 4), A[1], B[4]));
212 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 6), A[4], B[2]));
213 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 12), A[3], B[3]));
214 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 6), A[2], B[4]));
215 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 4), A[4], B[3]));
216 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 4), A[3], B[4]));
217 Sum.push_back(SE.getMulExpr(A[4], B[4]));
218 EXPECT_EQ(Product->getOperand(4), SE.getAddExpr(Sum));
219 Sum.clear();
220
221 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 5), A[4], B[1]));
222 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 10), A[3], B[2]));
223 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 10), A[2], B[3]));
224 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 5), A[1], B[4]));
225 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 20), A[4], B[2]));
226 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 30), A[3], B[3]));
227 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 20), A[2], B[4]));
228 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 30), A[4], B[3]));
229 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 30), A[3], B[4]));
230 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 20), A[4], B[4]));
231 EXPECT_EQ(Product->getOperand(5), SE.getAddExpr(Sum));
232 Sum.clear();
233
234 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 15), A[4], B[2]));
235 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 20), A[3], B[3]));
236 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 15), A[2], B[4]));
237 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 60), A[4], B[3]));
238 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 60), A[3], B[4]));
239 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 90), A[4], B[4]));
240 EXPECT_EQ(Product->getOperand(6), SE.getAddExpr(Sum));
241 Sum.clear();
242
243 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 35), A[4], B[3]));
244 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 35), A[3], B[4]));
245 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 140), A[4], B[4]));
246 EXPECT_EQ(Product->getOperand(7), SE.getAddExpr(Sum));
247 Sum.clear();
248#endif
249
250 Sum.push_back(SE.getMulExpr(SE.getConstant(Ty, 70), A[4], B[4]));
251 EXPECT_EQ(Product->getOperand(8), SE.getAddExpr(Sum));
Dan Gohman7cac9572010-08-02 23:49:30 +0000252}
253
Tobias Grosser11332e52016-02-21 17:42:10 +0000254TEST_F(ScalarEvolutionsTest, SimplifiedPHI) {
255 FunctionType *FTy = FunctionType::get(Type::getVoidTy(Context),
256 std::vector<Type *>(), false);
257 Function *F = cast<Function>(M.getOrInsertFunction("f", FTy));
258 BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", F);
259 BasicBlock *LoopBB = BasicBlock::Create(Context, "loop", F);
260 BasicBlock *ExitBB = BasicBlock::Create(Context, "exit", F);
261 BranchInst::Create(LoopBB, EntryBB);
262 BranchInst::Create(LoopBB, ExitBB, UndefValue::get(Type::getInt1Ty(Context)),
263 LoopBB);
264 ReturnInst::Create(Context, nullptr, ExitBB);
265 auto *Ty = Type::getInt32Ty(Context);
266 auto *PN = PHINode::Create(Ty, 2, "", &*LoopBB->begin());
267 PN->addIncoming(Constant::getNullValue(Ty), EntryBB);
268 PN->addIncoming(UndefValue::get(Ty), LoopBB);
269 ScalarEvolution SE = buildSE(*F);
270 auto *S1 = SE.getSCEV(PN);
271 auto *S2 = SE.getSCEV(PN);
Tobias Grosser946ca0a2016-02-22 07:20:40 +0000272 auto *ZeroConst = SE.getConstant(Ty, 0);
Tobias Grosser11332e52016-02-21 17:42:10 +0000273
274 // At some point, only the first call to getSCEV returned the simplified
275 // SCEVConstant and later calls just returned a SCEVUnknown referencing the
276 // PHI node.
Tobias Grosser946ca0a2016-02-22 07:20:40 +0000277 EXPECT_EQ(S1, ZeroConst);
278 EXPECT_EQ(S1, S2);
Tobias Grosser11332e52016-02-21 17:42:10 +0000279}
280
Wei Mi3076cc32016-09-15 04:06:44 +0000281TEST_F(ScalarEvolutionsTest, ExpandPtrTypeSCEV) {
282 // It is to test the fix for PR30213. It exercises the branch in scev
283 // expansion when the value in ValueOffsetPair is a ptr and the offset
284 // is not divisible by the elem type size of value.
285 auto *I8Ty = Type::getInt8Ty(Context);
286 auto *I8PtrTy = Type::getInt8PtrTy(Context);
287 auto *I32Ty = Type::getInt32Ty(Context);
288 auto *I32PtrTy = Type::getInt32PtrTy(Context);
289 FunctionType *FTy =
290 FunctionType::get(Type::getVoidTy(Context), std::vector<Type *>(), false);
291 Function *F = cast<Function>(M.getOrInsertFunction("f", FTy));
292 BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", F);
293 BasicBlock *LoopBB = BasicBlock::Create(Context, "loop", F);
294 BasicBlock *ExitBB = BasicBlock::Create(Context, "exit", F);
295 BranchInst::Create(LoopBB, EntryBB);
296 ReturnInst::Create(Context, nullptr, ExitBB);
297
298 // loop: ; preds = %loop, %entry
299 // %alloca = alloca i32
300 // %gep0 = getelementptr i32, i32* %alloca, i32 1
301 // %bitcast1 = bitcast i32* %gep0 to i8*
302 // %gep1 = getelementptr i8, i8* %bitcast1, i32 1
303 // %gep2 = getelementptr i8, i8* undef, i32 1
304 // %cmp = icmp ult i8* undef, %bitcast1
305 // %select = select i1 %cmp, i8* %gep1, i8* %gep2
306 // %bitcast2 = bitcast i8* %select to i32*
307 // br i1 undef, label %loop, label %exit
308
Matt Arsenault3c1fc762017-04-10 22:27:50 +0000309 const DataLayout &DL = F->getParent()->getDataLayout();
Wei Mi3076cc32016-09-15 04:06:44 +0000310 BranchInst *Br = BranchInst::Create(
311 LoopBB, ExitBB, UndefValue::get(Type::getInt1Ty(Context)), LoopBB);
Matt Arsenault3c1fc762017-04-10 22:27:50 +0000312 AllocaInst *Alloca = new AllocaInst(I32Ty, DL.getAllocaAddrSpace(),
313 "alloca", Br);
Wei Mi3076cc32016-09-15 04:06:44 +0000314 ConstantInt *Ci32 = ConstantInt::get(Context, APInt(32, 1));
315 GetElementPtrInst *Gep0 =
316 GetElementPtrInst::Create(I32Ty, Alloca, Ci32, "gep0", Br);
317 CastInst *CastA =
318 CastInst::CreateBitOrPointerCast(Gep0, I8PtrTy, "bitcast1", Br);
319 GetElementPtrInst *Gep1 =
320 GetElementPtrInst::Create(I8Ty, CastA, Ci32, "gep1", Br);
321 GetElementPtrInst *Gep2 = GetElementPtrInst::Create(
322 I8Ty, UndefValue::get(I8PtrTy), Ci32, "gep2", Br);
323 CmpInst *Cmp = CmpInst::Create(Instruction::ICmp, CmpInst::ICMP_ULT,
324 UndefValue::get(I8PtrTy), CastA, "cmp", Br);
325 SelectInst *Sel = SelectInst::Create(Cmp, Gep1, Gep2, "select", Br);
326 CastInst *CastB =
327 CastInst::CreateBitOrPointerCast(Sel, I32PtrTy, "bitcast2", Br);
328
329 ScalarEvolution SE = buildSE(*F);
330 auto *S = SE.getSCEV(CastB);
331 SCEVExpander Exp(SE, M.getDataLayout(), "expander");
332 Value *V =
333 Exp.expandCodeFor(cast<SCEVAddExpr>(S)->getOperand(1), nullptr, Br);
334
335 // Expect the expansion code contains:
336 // %0 = bitcast i32* %bitcast2 to i8*
337 // %uglygep = getelementptr i8, i8* %0, i64 -1
338 // %1 = bitcast i8* %uglygep to i32*
339 EXPECT_TRUE(isa<BitCastInst>(V));
340 Instruction *Gep = cast<Instruction>(V)->getPrevNode();
341 EXPECT_TRUE(isa<GetElementPtrInst>(Gep));
342 EXPECT_TRUE(isa<ConstantInt>(Gep->getOperand(1)));
343 EXPECT_EQ(cast<ConstantInt>(Gep->getOperand(1))->getSExtValue(), -1);
344 EXPECT_TRUE(isa<BitCastInst>(Gep->getPrevNode()));
345}
346
Sanjoy Dasb53021d2016-10-30 23:52:50 +0000347static Instruction *getInstructionByName(Function &F, StringRef Name) {
348 for (auto &I : instructions(F))
349 if (I.getName() == Name)
350 return &I;
Sanjoy Das507dd402016-10-18 17:45:16 +0000351 llvm_unreachable("Expected to find instruction!");
352}
353
354TEST_F(ScalarEvolutionsTest, CommutativeExprOperandOrder) {
355 LLVMContext C;
356 SMDiagnostic Err;
357 std::unique_ptr<Module> M = parseAssemblyString(
358 "target datalayout = \"e-m:e-p:32:32-f64:32:64-f80:32-n8:16:32-S128\" "
Sanjoy Dasb53021d2016-10-30 23:52:50 +0000359 " "
Sanjoy Das299e6722016-10-30 23:52:56 +0000360 "@var_0 = external global i32, align 4"
361 "@var_1 = external global i32, align 4"
Sanjoy Dasfd080902016-10-31 03:32:45 +0000362 "@var_2 = external global i32, align 4"
Sanjoy Das299e6722016-10-30 23:52:56 +0000363 " "
Sanjoy Das17078692016-10-31 03:32:43 +0000364 "declare i32 @unknown(i32, i32, i32)"
365 " "
Sanjoy Dasb53021d2016-10-30 23:52:50 +0000366 "define void @f_1(i8* nocapture %arr, i32 %n, i32* %A, i32* %B) "
Sanjoy Das507dd402016-10-18 17:45:16 +0000367 " local_unnamed_addr { "
368 "entry: "
369 " %entrycond = icmp sgt i32 %n, 0 "
370 " br i1 %entrycond, label %loop.ph, label %for.end "
371 " "
372 "loop.ph: "
373 " %a = load i32, i32* %A, align 4 "
374 " %b = load i32, i32* %B, align 4 "
375 " %mul = mul nsw i32 %b, %a "
376 " %iv0.init = getelementptr inbounds i8, i8* %arr, i32 %mul "
377 " br label %loop "
378 " "
379 "loop: "
380 " %iv0 = phi i8* [ %iv0.inc, %loop ], [ %iv0.init, %loop.ph ] "
381 " %iv1 = phi i32 [ %iv1.inc, %loop ], [ 0, %loop.ph ] "
382 " %conv = trunc i32 %iv1 to i8 "
383 " store i8 %conv, i8* %iv0, align 1 "
384 " %iv0.inc = getelementptr inbounds i8, i8* %iv0, i32 %b "
385 " %iv1.inc = add nuw nsw i32 %iv1, 1 "
386 " %exitcond = icmp eq i32 %iv1.inc, %n "
387 " br i1 %exitcond, label %for.end.loopexit, label %loop "
388 " "
389 "for.end.loopexit: "
390 " br label %for.end "
391 " "
392 "for.end: "
393 " ret void "
394 "} "
395 " "
Sanjoy Dasb53021d2016-10-30 23:52:50 +0000396 "define void @f_2(i32* %X, i32* %Y, i32* %Z) { "
Sanjoy Das507dd402016-10-18 17:45:16 +0000397 " %x = load i32, i32* %X "
398 " %y = load i32, i32* %Y "
399 " %z = load i32, i32* %Z "
400 " ret void "
Sanjoy Das299e6722016-10-30 23:52:56 +0000401 "} "
402 " "
Sanjoy Das299e6722016-10-30 23:52:56 +0000403 "define void @f_3() { "
404 " %x = load i32, i32* @var_0"
405 " %y = load i32, i32* @var_1"
Sanjoy Dasfd080902016-10-31 03:32:45 +0000406 " %z = load i32, i32* @var_2"
Sanjoy Das299e6722016-10-30 23:52:56 +0000407 " ret void"
408 "} "
Sanjoy Das17078692016-10-31 03:32:43 +0000409 " "
410 "define void @f_4(i32 %a, i32 %b, i32 %c) { "
411 " %x = call i32 @unknown(i32 %a, i32 %b, i32 %c)"
412 " %y = call i32 @unknown(i32 %b, i32 %c, i32 %a)"
413 " %z = call i32 @unknown(i32 %c, i32 %a, i32 %b)"
414 " ret void"
415 "} "
Sanjoy Das299e6722016-10-30 23:52:56 +0000416 ,
Sanjoy Das507dd402016-10-18 17:45:16 +0000417 Err, C);
418
419 assert(M && "Could not parse module?");
420 assert(!verifyModule(*M) && "Must have been well formed!");
421
Sanjoy Das044f9562017-04-14 23:47:53 +0000422 runWithSE(*M, "f_1", [&](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
Sanjoy Das3d6e3df2016-10-31 03:32:39 +0000423 auto *IV0 = getInstructionByName(F, "iv0");
424 auto *IV0Inc = getInstructionByName(F, "iv0.inc");
Sanjoy Das507dd402016-10-18 17:45:16 +0000425
Sanjoy Das507dd402016-10-18 17:45:16 +0000426 auto *FirstExprForIV0 = SE.getSCEV(IV0);
427 auto *FirstExprForIV0Inc = SE.getSCEV(IV0Inc);
428 auto *SecondExprForIV0 = SE.getSCEV(IV0);
429
430 EXPECT_TRUE(isa<SCEVAddRecExpr>(FirstExprForIV0));
431 EXPECT_TRUE(isa<SCEVAddRecExpr>(FirstExprForIV0Inc));
432 EXPECT_TRUE(isa<SCEVAddRecExpr>(SecondExprForIV0));
Sanjoy Das3d6e3df2016-10-31 03:32:39 +0000433 });
Sanjoy Das507dd402016-10-18 17:45:16 +0000434
Sanjoy Das17078692016-10-31 03:32:43 +0000435 auto CheckCommutativeMulExprs = [&](ScalarEvolution &SE, const SCEV *A,
436 const SCEV *B, const SCEV *C) {
437 EXPECT_EQ(SE.getMulExpr(A, B), SE.getMulExpr(B, A));
438 EXPECT_EQ(SE.getMulExpr(B, C), SE.getMulExpr(C, B));
439 EXPECT_EQ(SE.getMulExpr(A, C), SE.getMulExpr(C, A));
Sanjoy Das507dd402016-10-18 17:45:16 +0000440
Sanjoy Das17078692016-10-31 03:32:43 +0000441 SmallVector<const SCEV *, 3> Ops0 = {A, B, C};
442 SmallVector<const SCEV *, 3> Ops1 = {A, C, B};
443 SmallVector<const SCEV *, 3> Ops2 = {B, A, C};
444 SmallVector<const SCEV *, 3> Ops3 = {B, C, A};
445 SmallVector<const SCEV *, 3> Ops4 = {C, B, A};
446 SmallVector<const SCEV *, 3> Ops5 = {C, A, B};
Sanjoy Das507dd402016-10-18 17:45:16 +0000447
448 auto *Mul0 = SE.getMulExpr(Ops0);
449 auto *Mul1 = SE.getMulExpr(Ops1);
450 auto *Mul2 = SE.getMulExpr(Ops2);
451 auto *Mul3 = SE.getMulExpr(Ops3);
452 auto *Mul4 = SE.getMulExpr(Ops4);
453 auto *Mul5 = SE.getMulExpr(Ops5);
454
Sanjoy Das17078692016-10-31 03:32:43 +0000455 EXPECT_EQ(Mul0, Mul1) << "Expected " << *Mul0 << " == " << *Mul1;
456 EXPECT_EQ(Mul1, Mul2) << "Expected " << *Mul1 << " == " << *Mul2;
457 EXPECT_EQ(Mul2, Mul3) << "Expected " << *Mul2 << " == " << *Mul3;
458 EXPECT_EQ(Mul3, Mul4) << "Expected " << *Mul3 << " == " << *Mul4;
459 EXPECT_EQ(Mul4, Mul5) << "Expected " << *Mul4 << " == " << *Mul5;
460 };
461
Sanjoy Dasfd080902016-10-31 03:32:45 +0000462 for (StringRef FuncName : {"f_2", "f_3", "f_4"})
Sanjoy Das044f9562017-04-14 23:47:53 +0000463 runWithSE(
464 *M, FuncName, [&](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
465 CheckCommutativeMulExprs(SE, SE.getSCEV(getInstructionByName(F, "x")),
466 SE.getSCEV(getInstructionByName(F, "y")),
467 SE.getSCEV(getInstructionByName(F, "z")));
468 });
Sanjoy Das507dd402016-10-18 17:45:16 +0000469}
470
Sanjoy Das1bd479d2017-03-05 23:49:17 +0000471TEST_F(ScalarEvolutionsTest, CompareSCEVComplexity) {
Daniil Fukalov4c3322c2016-11-17 16:07:52 +0000472 FunctionType *FTy =
473 FunctionType::get(Type::getVoidTy(Context), std::vector<Type *>(), false);
474 Function *F = cast<Function>(M.getOrInsertFunction("f", FTy));
475 BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", F);
476 BasicBlock *LoopBB = BasicBlock::Create(Context, "bb1", F);
477 BranchInst::Create(LoopBB, EntryBB);
478
479 auto *Ty = Type::getInt32Ty(Context);
480 SmallVector<Instruction*, 8> Muls(8), Acc(8), NextAcc(8);
481
482 Acc[0] = PHINode::Create(Ty, 2, "", LoopBB);
483 Acc[1] = PHINode::Create(Ty, 2, "", LoopBB);
484 Acc[2] = PHINode::Create(Ty, 2, "", LoopBB);
485 Acc[3] = PHINode::Create(Ty, 2, "", LoopBB);
486 Acc[4] = PHINode::Create(Ty, 2, "", LoopBB);
487 Acc[5] = PHINode::Create(Ty, 2, "", LoopBB);
488 Acc[6] = PHINode::Create(Ty, 2, "", LoopBB);
489 Acc[7] = PHINode::Create(Ty, 2, "", LoopBB);
490
491 for (int i = 0; i < 20; i++) {
492 Muls[0] = BinaryOperator::CreateMul(Acc[0], Acc[0], "", LoopBB);
493 NextAcc[0] = BinaryOperator::CreateAdd(Muls[0], Acc[4], "", LoopBB);
494 Muls[1] = BinaryOperator::CreateMul(Acc[1], Acc[1], "", LoopBB);
495 NextAcc[1] = BinaryOperator::CreateAdd(Muls[1], Acc[5], "", LoopBB);
496 Muls[2] = BinaryOperator::CreateMul(Acc[2], Acc[2], "", LoopBB);
497 NextAcc[2] = BinaryOperator::CreateAdd(Muls[2], Acc[6], "", LoopBB);
498 Muls[3] = BinaryOperator::CreateMul(Acc[3], Acc[3], "", LoopBB);
499 NextAcc[3] = BinaryOperator::CreateAdd(Muls[3], Acc[7], "", LoopBB);
500
501 Muls[4] = BinaryOperator::CreateMul(Acc[4], Acc[4], "", LoopBB);
502 NextAcc[4] = BinaryOperator::CreateAdd(Muls[4], Acc[0], "", LoopBB);
503 Muls[5] = BinaryOperator::CreateMul(Acc[5], Acc[5], "", LoopBB);
504 NextAcc[5] = BinaryOperator::CreateAdd(Muls[5], Acc[1], "", LoopBB);
505 Muls[6] = BinaryOperator::CreateMul(Acc[6], Acc[6], "", LoopBB);
506 NextAcc[6] = BinaryOperator::CreateAdd(Muls[6], Acc[2], "", LoopBB);
507 Muls[7] = BinaryOperator::CreateMul(Acc[7], Acc[7], "", LoopBB);
508 NextAcc[7] = BinaryOperator::CreateAdd(Muls[7], Acc[3], "", LoopBB);
509 Acc = NextAcc;
510 }
511
512 auto II = LoopBB->begin();
513 for (int i = 0; i < 8; i++) {
514 PHINode *Phi = cast<PHINode>(&*II++);
515 Phi->addIncoming(Acc[i], LoopBB);
516 Phi->addIncoming(UndefValue::get(Ty), EntryBB);
517 }
518
519 BasicBlock *ExitBB = BasicBlock::Create(Context, "bb2", F);
520 BranchInst::Create(LoopBB, ExitBB, UndefValue::get(Type::getInt1Ty(Context)),
521 LoopBB);
522
523 Acc[0] = BinaryOperator::CreateAdd(Acc[0], Acc[1], "", ExitBB);
524 Acc[1] = BinaryOperator::CreateAdd(Acc[2], Acc[3], "", ExitBB);
525 Acc[2] = BinaryOperator::CreateAdd(Acc[4], Acc[5], "", ExitBB);
526 Acc[3] = BinaryOperator::CreateAdd(Acc[6], Acc[7], "", ExitBB);
527 Acc[0] = BinaryOperator::CreateAdd(Acc[0], Acc[1], "", ExitBB);
528 Acc[1] = BinaryOperator::CreateAdd(Acc[2], Acc[3], "", ExitBB);
529 Acc[0] = BinaryOperator::CreateAdd(Acc[0], Acc[1], "", ExitBB);
530
531 ReturnInst::Create(Context, nullptr, ExitBB);
532
533 ScalarEvolution SE = buildSE(*F);
534
535 EXPECT_NE(nullptr, SE.getSCEV(Acc[0]));
536}
537
Sanjoy Das1bd479d2017-03-05 23:49:17 +0000538TEST_F(ScalarEvolutionsTest, CompareValueComplexity) {
539 IntegerType *IntPtrTy = M.getDataLayout().getIntPtrType(Context);
540 PointerType *IntPtrPtrTy = IntPtrTy->getPointerTo();
541
542 FunctionType *FTy =
543 FunctionType::get(Type::getVoidTy(Context), {IntPtrTy, IntPtrTy}, false);
544 Function *F = cast<Function>(M.getOrInsertFunction("f", FTy));
545 BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", F);
546
547 Value *X = &*F->arg_begin();
548 Value *Y = &*std::next(F->arg_begin());
549
550 const int ValueDepth = 10;
551 for (int i = 0; i < ValueDepth; i++) {
552 X = new LoadInst(new IntToPtrInst(X, IntPtrPtrTy, "", EntryBB), "",
553 /*isVolatile*/ false, EntryBB);
554 Y = new LoadInst(new IntToPtrInst(Y, IntPtrPtrTy, "", EntryBB), "",
555 /*isVolatile*/ false, EntryBB);
556 }
557
558 auto *MulA = BinaryOperator::CreateMul(X, Y, "", EntryBB);
559 auto *MulB = BinaryOperator::CreateMul(Y, X, "", EntryBB);
560 ReturnInst::Create(Context, nullptr, EntryBB);
561
562 // This test isn't checking for correctness. Today making A and B resolve to
563 // the same SCEV would require deeper searching in CompareValueComplexity,
564 // which will slow down compilation. However, this test can fail (with LLVM's
565 // behavior still being correct) if we ever have a smarter
566 // CompareValueComplexity that is both fast and more accurate.
567
568 ScalarEvolution SE = buildSE(*F);
569 auto *A = SE.getSCEV(MulA);
570 auto *B = SE.getSCEV(MulB);
571 EXPECT_NE(A, B);
572}
573
Daniil Fukalov6378bdb2017-02-06 12:38:06 +0000574TEST_F(ScalarEvolutionsTest, SCEVAddExpr) {
575 Type *Ty32 = Type::getInt32Ty(Context);
576 Type *ArgTys[] = {Type::getInt64Ty(Context), Ty32};
577
578 FunctionType *FTy =
579 FunctionType::get(Type::getVoidTy(Context), ArgTys, false);
580 Function *F = cast<Function>(M.getOrInsertFunction("f", FTy));
581
582 Argument *A1 = &*F->arg_begin();
583 Argument *A2 = &*(std::next(F->arg_begin()));
584 BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", F);
585
586 Instruction *Trunc = CastInst::CreateTruncOrBitCast(A1, Ty32, "", EntryBB);
587 Instruction *Mul1 = BinaryOperator::CreateMul(Trunc, A2, "", EntryBB);
588 Instruction *Add1 = BinaryOperator::CreateAdd(Mul1, Trunc, "", EntryBB);
589 Mul1 = BinaryOperator::CreateMul(Add1, Trunc, "", EntryBB);
590 Instruction *Add2 = BinaryOperator::CreateAdd(Mul1, Add1, "", EntryBB);
Chandler Carruthcd07efc2017-02-06 21:27:12 +0000591 // FIXME: The size of this is arbitrary and doesn't seem to change the
592 // result, but SCEV will do quadratic work for these so a large number here
593 // will be extremely slow. We should revisit what and how this is testing
594 // SCEV.
595 for (int i = 0; i < 10; i++) {
Daniil Fukalov6378bdb2017-02-06 12:38:06 +0000596 Mul1 = BinaryOperator::CreateMul(Add2, Add1, "", EntryBB);
597 Add1 = Add2;
598 Add2 = BinaryOperator::CreateAdd(Mul1, Add1, "", EntryBB);
599 }
600
601 ReturnInst::Create(Context, nullptr, EntryBB);
602 ScalarEvolution SE = buildSE(*F);
603 EXPECT_NE(nullptr, SE.getSCEV(Mul1));
604}
605
Sanjoy Dasb600d3f2017-04-14 15:50:04 +0000606static Instruction &GetInstByName(Function &F, StringRef Name) {
607 for (auto &I : instructions(F))
608 if (I.getName() == Name)
609 return I;
610 llvm_unreachable("Could not find instructions!");
611}
612
613TEST_F(ScalarEvolutionsTest, SCEVNormalization) {
614 LLVMContext C;
615 SMDiagnostic Err;
616 std::unique_ptr<Module> M = parseAssemblyString(
617 "target datalayout = \"e-m:e-p:32:32-f64:32:64-f80:32-n8:16:32-S128\" "
618 " "
619 "@var_0 = external global i32, align 4"
620 "@var_1 = external global i32, align 4"
621 "@var_2 = external global i32, align 4"
622 " "
623 "declare i32 @unknown(i32, i32, i32)"
624 " "
625 "define void @f_1(i8* nocapture %arr, i32 %n, i32* %A, i32* %B) "
626 " local_unnamed_addr { "
627 "entry: "
628 " br label %loop.ph "
629 " "
630 "loop.ph: "
631 " br label %loop "
632 " "
633 "loop: "
634 " %iv0 = phi i32 [ %iv0.inc, %loop ], [ 0, %loop.ph ] "
635 " %iv1 = phi i32 [ %iv1.inc, %loop ], [ -2147483648, %loop.ph ] "
636 " %iv0.inc = add i32 %iv0, 1 "
637 " %iv1.inc = add i32 %iv1, 3 "
638 " br i1 undef, label %for.end.loopexit, label %loop "
639 " "
640 "for.end.loopexit: "
641 " ret void "
642 "} "
Sanjoy Dasbbebcb62017-04-25 00:09:19 +0000643 " "
644 "define void @f_2(i32 %a, i32 %b, i32 %c, i32 %d) "
645 " local_unnamed_addr { "
646 "entry: "
647 " br label %loop_0 "
648 " "
649 "loop_0: "
650 " br i1 undef, label %loop_0, label %loop_1 "
651 " "
652 "loop_1: "
653 " br i1 undef, label %loop_2, label %loop_1 "
654 " "
655 " "
656 "loop_2: "
657 " br i1 undef, label %end, label %loop_2 "
658 " "
659 "end: "
660 " ret void "
661 "} "
Sanjoy Dasb600d3f2017-04-14 15:50:04 +0000662 ,
663 Err, C);
664
665 assert(M && "Could not parse module?");
666 assert(!verifyModule(*M) && "Must have been well formed!");
667
Sanjoy Das044f9562017-04-14 23:47:53 +0000668 runWithSE(*M, "f_1", [&](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
Sanjoy Dasb600d3f2017-04-14 15:50:04 +0000669 auto &I0 = GetInstByName(F, "iv0");
670 auto &I1 = *I0.getNextNode();
671
672 auto *S0 = cast<SCEVAddRecExpr>(SE.getSCEV(&I0));
673 PostIncLoopSet Loops;
674 Loops.insert(S0->getLoop());
675 auto *N0 = normalizeForPostIncUse(S0, Loops, SE);
676 auto *D0 = denormalizeForPostIncUse(N0, Loops, SE);
677 EXPECT_EQ(S0, D0) << *S0 << " " << *D0;
678
679 auto *S1 = cast<SCEVAddRecExpr>(SE.getSCEV(&I1));
680 Loops.clear();
681 Loops.insert(S1->getLoop());
682 auto *N1 = normalizeForPostIncUse(S1, Loops, SE);
683 auto *D1 = denormalizeForPostIncUse(N1, Loops, SE);
684 EXPECT_EQ(S1, D1) << *S1 << " " << *D1;
685 });
Sanjoy Dasbbebcb62017-04-25 00:09:19 +0000686
687 runWithSE(*M, "f_2", [&](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
688 auto *L2 = *LI.begin();
689 auto *L1 = *std::next(LI.begin());
690 auto *L0 = *std::next(LI.begin(), 2);
691
692 auto GetAddRec = [&SE](const Loop *L, std::initializer_list<const SCEV *> Ops) {
693 SmallVector<const SCEV *, 4> OpsCopy(Ops);
694 return SE.getAddRecExpr(OpsCopy, L, SCEV::FlagAnyWrap);
695 };
696
697 auto GetAdd = [&SE](std::initializer_list<const SCEV *> Ops) {
698 SmallVector<const SCEV *, 4> OpsCopy(Ops);
699 return SE.getAddExpr(OpsCopy, SCEV::FlagAnyWrap);
700 };
701
702 // We first populate the AddRecs vector with a few "interesting" SCEV
703 // expressions, and then we go through the list and assert that each
704 // expression in it has an invertible normalization.
705
706 std::vector<const SCEV *> Exprs;
707 {
708 const SCEV *V0 = SE.getSCEV(&*F.arg_begin());
709 const SCEV *V1 = SE.getSCEV(&*std::next(F.arg_begin(), 1));
710 const SCEV *V2 = SE.getSCEV(&*std::next(F.arg_begin(), 2));
711 const SCEV *V3 = SE.getSCEV(&*std::next(F.arg_begin(), 3));
712
713 Exprs.push_back(GetAddRec(L0, {V0})); // 0
714 Exprs.push_back(GetAddRec(L0, {V0, V1})); // 1
715 Exprs.push_back(GetAddRec(L0, {V0, V1, V2})); // 2
716 Exprs.push_back(GetAddRec(L0, {V0, V1, V2, V3})); // 3
717
718 Exprs.push_back(
719 GetAddRec(L1, {Exprs[1], Exprs[2], Exprs[3], Exprs[0]})); // 4
720 Exprs.push_back(
721 GetAddRec(L1, {Exprs[1], Exprs[2], Exprs[0], Exprs[3]})); // 5
722 Exprs.push_back(
723 GetAddRec(L1, {Exprs[1], Exprs[3], Exprs[3], Exprs[1]})); // 6
724
725 Exprs.push_back(GetAdd({Exprs[6], Exprs[3], V2})); // 7
726
727 Exprs.push_back(
728 GetAddRec(L2, {Exprs[4], Exprs[3], Exprs[3], Exprs[5]})); // 8
729
730 Exprs.push_back(
731 GetAddRec(L2, {Exprs[4], Exprs[6], Exprs[7], Exprs[3], V0})); // 9
732 }
733
734 std::vector<PostIncLoopSet> LoopSets;
735 for (int i = 0; i < 8; i++) {
736 LoopSets.emplace_back();
737 if (i & 1)
738 LoopSets.back().insert(L0);
739 if (i & 2)
740 LoopSets.back().insert(L1);
741 if (i & 4)
742 LoopSets.back().insert(L2);
743 }
744
745 for (const auto &LoopSet : LoopSets)
746 for (auto *S : Exprs) {
747 {
748 auto *N = llvm::normalizeForPostIncUse(S, LoopSet, SE);
749 auto *D = llvm::denormalizeForPostIncUse(N, LoopSet, SE);
750
751 // Normalization and then denormalizing better give us back the same
752 // value.
753 EXPECT_EQ(S, D) << "S = " << *S << " D = " << *D << " N = " << *N;
754 }
755 {
756 auto *D = llvm::denormalizeForPostIncUse(S, LoopSet, SE);
757 auto *N = llvm::normalizeForPostIncUse(D, LoopSet, SE);
758
759 // Denormalization and then normalizing better give us back the same
760 // value.
761 EXPECT_EQ(S, N) << "S = " << *S << " N = " << *N;
762 }
763 }
764 });
Sanjoy Dasb600d3f2017-04-14 15:50:04 +0000765}
766
Wei Mi8c405332017-04-17 20:40:05 +0000767// Expect the call of getZeroExtendExpr will not cost exponential time.
768TEST_F(ScalarEvolutionsTest, SCEVZeroExtendExpr) {
769 LLVMContext C;
770 SMDiagnostic Err;
771
772 // Generate a function like below:
773 // define void @foo() {
774 // entry:
775 // br label %for.cond
776 //
777 // for.cond:
778 // %0 = phi i64 [ 100, %entry ], [ %dec, %for.inc ]
779 // %cmp = icmp sgt i64 %0, 90
780 // br i1 %cmp, label %for.inc, label %for.cond1
781 //
782 // for.inc:
783 // %dec = add nsw i64 %0, -1
784 // br label %for.cond
785 //
786 // for.cond1:
787 // %1 = phi i64 [ 100, %for.cond ], [ %dec5, %for.inc2 ]
788 // %cmp3 = icmp sgt i64 %1, 90
789 // br i1 %cmp3, label %for.inc2, label %for.cond4
790 //
791 // for.inc2:
792 // %dec5 = add nsw i64 %1, -1
793 // br label %for.cond1
794 //
795 // ......
796 //
797 // for.cond89:
798 // %19 = phi i64 [ 100, %for.cond84 ], [ %dec94, %for.inc92 ]
799 // %cmp93 = icmp sgt i64 %19, 90
800 // br i1 %cmp93, label %for.inc92, label %for.end
801 //
802 // for.inc92:
803 // %dec94 = add nsw i64 %19, -1
804 // br label %for.cond89
805 //
806 // for.end:
807 // %gep = getelementptr i8, i8* null, i64 %dec
808 // %gep6 = getelementptr i8, i8* %gep, i64 %dec5
809 // ......
810 // %gep95 = getelementptr i8, i8* %gep91, i64 %dec94
811 // ret void
812 // }
813 FunctionType *FTy = FunctionType::get(Type::getVoidTy(Context), {}, false);
814 Function *F = cast<Function>(M.getOrInsertFunction("foo", FTy));
815
816 BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", F);
817 BasicBlock *CondBB = BasicBlock::Create(Context, "for.cond", F);
818 BasicBlock *EndBB = BasicBlock::Create(Context, "for.end", F);
819 BranchInst::Create(CondBB, EntryBB);
820 BasicBlock *PrevBB = EntryBB;
821
822 Type *I64Ty = Type::getInt64Ty(Context);
823 Type *I8Ty = Type::getInt8Ty(Context);
824 Type *I8PtrTy = Type::getInt8PtrTy(Context);
825 Value *Accum = Constant::getNullValue(I8PtrTy);
826 int Iters = 20;
827 for (int i = 0; i < Iters; i++) {
828 BasicBlock *IncBB = BasicBlock::Create(Context, "for.inc", F, EndBB);
829 auto *PN = PHINode::Create(I64Ty, 2, "", CondBB);
830 PN->addIncoming(ConstantInt::get(Context, APInt(64, 100)), PrevBB);
831 auto *Cmp = CmpInst::Create(Instruction::ICmp, CmpInst::ICMP_SGT, PN,
832 ConstantInt::get(Context, APInt(64, 90)), "cmp",
833 CondBB);
834 BasicBlock *NextBB;
835 if (i != Iters - 1)
836 NextBB = BasicBlock::Create(Context, "for.cond", F, EndBB);
837 else
838 NextBB = EndBB;
839 BranchInst::Create(IncBB, NextBB, Cmp, CondBB);
840 auto *Dec = BinaryOperator::CreateNSWAdd(
841 PN, ConstantInt::get(Context, APInt(64, -1)), "dec", IncBB);
842 PN->addIncoming(Dec, IncBB);
843 BranchInst::Create(CondBB, IncBB);
844
845 Accum = GetElementPtrInst::Create(I8Ty, Accum, Dec, "gep", EndBB);
846
847 PrevBB = CondBB;
848 CondBB = NextBB;
849 }
850 ReturnInst::Create(Context, nullptr, EndBB);
851 ScalarEvolution SE = buildSE(*F);
852 const SCEV *S = SE.getSCEV(Accum);
853 Type *I128Ty = Type::getInt128Ty(Context);
854 SE.getZeroExtendExpr(S, I128Ty);
855}
Keno Fischer090f1952017-05-27 03:22:55 +0000856
857// Make sure that SCEV doesn't introduce illegal ptrtoint/inttoptr instructions
858TEST_F(ScalarEvolutionsTest, SCEVZeroExtendExprNonIntegral) {
859 /*
860 * Create the following code:
861 * func(i64 addrspace(10)* %arg)
862 * top:
863 * br label %L.ph
864 * L.ph:
865 * br label %L
866 * L:
867 * %phi = phi i64 [i64 0, %L.ph], [ %add, %L2 ]
868 * %add = add i64 %phi2, 1
869 * br i1 undef, label %post, label %L2
870 * post:
871 * %gepbase = getelementptr i64 addrspace(10)* %arg, i64 1
872 * #= %gep = getelementptr i64 addrspace(10)* %gepbase, i64 %add =#
873 * ret void
874 *
875 * We will create the appropriate SCEV expression for %gep and expand it,
876 * then check that no inttoptr/ptrtoint instructions got inserted.
877 */
878
879 // Create a module with non-integral pointers in it's datalayout
880 Module NIM("nonintegral", Context);
881 std::string DataLayout = M.getDataLayoutStr();
882 if (!DataLayout.empty())
883 DataLayout += "-";
884 DataLayout += "ni:10";
885 NIM.setDataLayout(DataLayout);
886
887 Type *T_int1 = Type::getInt1Ty(Context);
888 Type *T_int64 = Type::getInt64Ty(Context);
889 Type *T_pint64 = T_int64->getPointerTo(10);
890
891 FunctionType *FTy =
892 FunctionType::get(Type::getVoidTy(Context), {T_pint64}, false);
893 Function *F = cast<Function>(NIM.getOrInsertFunction("foo", FTy));
894
895 Argument *Arg = &*F->arg_begin();
896
897 BasicBlock *Top = BasicBlock::Create(Context, "top", F);
898 BasicBlock *LPh = BasicBlock::Create(Context, "L.ph", F);
899 BasicBlock *L = BasicBlock::Create(Context, "L", F);
900 BasicBlock *Post = BasicBlock::Create(Context, "post", F);
901
902 IRBuilder<> Builder(Top);
903 Builder.CreateBr(LPh);
904
905 Builder.SetInsertPoint(LPh);
906 Builder.CreateBr(L);
907
908 Builder.SetInsertPoint(L);
909 PHINode *Phi = Builder.CreatePHI(T_int64, 2);
910 Value *Add = Builder.CreateAdd(Phi, ConstantInt::get(T_int64, 1), "add");
911 Builder.CreateCondBr(UndefValue::get(T_int1), L, Post);
912 Phi->addIncoming(ConstantInt::get(T_int64, 0), LPh);
913 Phi->addIncoming(Add, L);
914
915 Builder.SetInsertPoint(Post);
Gor Nishanove5d29112017-05-27 05:24:30 +0000916 Value *GepBase = Builder.CreateGEP(Arg, ConstantInt::get(T_int64, 1));
Keno Fischer090f1952017-05-27 03:22:55 +0000917 Instruction *Ret = Builder.CreateRetVoid();
918
919 ScalarEvolution SE = buildSE(*F);
920 auto *AddRec =
921 SE.getAddRecExpr(SE.getUnknown(GepBase), SE.getConstant(T_int64, 1),
922 LI->getLoopFor(L), SCEV::FlagNUW);
923
924 SCEVExpander Exp(SE, NIM.getDataLayout(), "expander");
925 Exp.disableCanonicalMode();
926 Exp.expandCodeFor(AddRec, T_pint64, Ret);
927
928 // Make sure none of the instructions inserted were inttoptr/ptrtoint.
929 // The verifier will check this.
930 EXPECT_FALSE(verifyFunction(*F, &errs()));
931}
932
Max Kazantsev2cb36532017-08-03 08:41:30 +0000933// Make sure that SCEV invalidates exit limits after invalidating the values it
934// depends on when we forget a loop.
935TEST_F(ScalarEvolutionsTest, SCEVExitLimitForgetLoop) {
936 /*
937 * Create the following code:
938 * func(i64 addrspace(10)* %arg)
939 * top:
940 * br label %L.ph
941 * L.ph:
942 * br label %L
943 * L:
944 * %phi = phi i64 [i64 0, %L.ph], [ %add, %L2 ]
945 * %add = add i64 %phi2, 1
946 * %cond = icmp slt i64 %add, 1000; then becomes 2000.
947 * br i1 %cond, label %post, label %L2
948 * post:
949 * ret void
950 *
951 */
952
953 // Create a module with non-integral pointers in it's datalayout
954 Module NIM("nonintegral", Context);
955 std::string DataLayout = M.getDataLayoutStr();
956 if (!DataLayout.empty())
957 DataLayout += "-";
958 DataLayout += "ni:10";
959 NIM.setDataLayout(DataLayout);
960
961 Type *T_int64 = Type::getInt64Ty(Context);
962 Type *T_pint64 = T_int64->getPointerTo(10);
963
964 FunctionType *FTy =
965 FunctionType::get(Type::getVoidTy(Context), {T_pint64}, false);
966 Function *F = cast<Function>(NIM.getOrInsertFunction("foo", FTy));
967
Max Kazantsev2cb36532017-08-03 08:41:30 +0000968 BasicBlock *Top = BasicBlock::Create(Context, "top", F);
969 BasicBlock *LPh = BasicBlock::Create(Context, "L.ph", F);
970 BasicBlock *L = BasicBlock::Create(Context, "L", F);
971 BasicBlock *Post = BasicBlock::Create(Context, "post", F);
972
973 IRBuilder<> Builder(Top);
974 Builder.CreateBr(LPh);
975
976 Builder.SetInsertPoint(LPh);
977 Builder.CreateBr(L);
978
979 Builder.SetInsertPoint(L);
980 PHINode *Phi = Builder.CreatePHI(T_int64, 2);
981 auto *Add = cast<Instruction>(
982 Builder.CreateAdd(Phi, ConstantInt::get(T_int64, 1), "add"));
983 auto *Limit = ConstantInt::get(T_int64, 1000);
984 auto *Cond = cast<Instruction>(
985 Builder.CreateICmp(ICmpInst::ICMP_SLT, Add, Limit, "cond"));
986 auto *Br = cast<Instruction>(Builder.CreateCondBr(Cond, L, Post));
987 Phi->addIncoming(ConstantInt::get(T_int64, 0), LPh);
988 Phi->addIncoming(Add, L);
989
990 Builder.SetInsertPoint(Post);
991 Builder.CreateRetVoid();
992
993 ScalarEvolution SE = buildSE(*F);
994 auto *Loop = LI->getLoopFor(L);
995 const SCEV *EC = SE.getBackedgeTakenCount(Loop);
996 EXPECT_FALSE(isa<SCEVCouldNotCompute>(EC));
Max Kazantsevba1e70e2017-08-04 05:06:44 +0000997 EXPECT_TRUE(isa<SCEVConstant>(EC));
998 EXPECT_EQ(cast<SCEVConstant>(EC)->getAPInt().getLimitedValue(), 999);
Max Kazantsev2cb36532017-08-03 08:41:30 +0000999
1000 SE.forgetLoop(Loop);
1001 Br->eraseFromParent();
1002 Cond->eraseFromParent();
1003
1004 Builder.SetInsertPoint(L);
Benjamin Kramera73b4102017-08-03 15:59:37 +00001005 auto *NewCond = Builder.CreateICmp(
1006 ICmpInst::ICMP_SLT, Add, ConstantInt::get(T_int64, 2000), "new.cond");
1007 Builder.CreateCondBr(NewCond, L, Post);
Max Kazantsev2cb36532017-08-03 08:41:30 +00001008 const SCEV *NewEC = SE.getBackedgeTakenCount(Loop);
Max Kazantsevba1e70e2017-08-04 05:06:44 +00001009 EXPECT_FALSE(isa<SCEVCouldNotCompute>(NewEC));
1010 EXPECT_TRUE(isa<SCEVConstant>(NewEC));
1011 EXPECT_EQ(cast<SCEVConstant>(NewEC)->getAPInt().getLimitedValue(), 1999);
Max Kazantsev2cb36532017-08-03 08:41:30 +00001012}
1013
1014// Make sure that SCEV invalidates exit limits after invalidating the values it
1015// depends on when we forget a value.
1016TEST_F(ScalarEvolutionsTest, SCEVExitLimitForgetValue) {
1017 /*
1018 * Create the following code:
1019 * func(i64 addrspace(10)* %arg)
1020 * top:
1021 * br label %L.ph
1022 * L.ph:
1023 * %load = load i64 addrspace(10)* %arg
1024 * br label %L
1025 * L:
1026 * %phi = phi i64 [i64 0, %L.ph], [ %add, %L2 ]
1027 * %add = add i64 %phi2, 1
1028 * %cond = icmp slt i64 %add, %load ; then becomes 2000.
1029 * br i1 %cond, label %post, label %L2
1030 * post:
1031 * ret void
1032 *
1033 */
1034
1035 // Create a module with non-integral pointers in it's datalayout
1036 Module NIM("nonintegral", Context);
1037 std::string DataLayout = M.getDataLayoutStr();
1038 if (!DataLayout.empty())
1039 DataLayout += "-";
1040 DataLayout += "ni:10";
1041 NIM.setDataLayout(DataLayout);
1042
1043 Type *T_int64 = Type::getInt64Ty(Context);
1044 Type *T_pint64 = T_int64->getPointerTo(10);
1045
1046 FunctionType *FTy =
1047 FunctionType::get(Type::getVoidTy(Context), {T_pint64}, false);
1048 Function *F = cast<Function>(NIM.getOrInsertFunction("foo", FTy));
1049
1050 Argument *Arg = &*F->arg_begin();
1051
1052 BasicBlock *Top = BasicBlock::Create(Context, "top", F);
1053 BasicBlock *LPh = BasicBlock::Create(Context, "L.ph", F);
1054 BasicBlock *L = BasicBlock::Create(Context, "L", F);
1055 BasicBlock *Post = BasicBlock::Create(Context, "post", F);
1056
1057 IRBuilder<> Builder(Top);
1058 Builder.CreateBr(LPh);
1059
1060 Builder.SetInsertPoint(LPh);
1061 auto *Load = cast<Instruction>(Builder.CreateLoad(T_int64, Arg, "load"));
1062 Builder.CreateBr(L);
1063
1064 Builder.SetInsertPoint(L);
1065 PHINode *Phi = Builder.CreatePHI(T_int64, 2);
1066 auto *Add = cast<Instruction>(
1067 Builder.CreateAdd(Phi, ConstantInt::get(T_int64, 1), "add"));
1068 auto *Cond = cast<Instruction>(
1069 Builder.CreateICmp(ICmpInst::ICMP_SLT, Add, Load, "cond"));
1070 auto *Br = cast<Instruction>(Builder.CreateCondBr(Cond, L, Post));
1071 Phi->addIncoming(ConstantInt::get(T_int64, 0), LPh);
1072 Phi->addIncoming(Add, L);
1073
1074 Builder.SetInsertPoint(Post);
1075 Builder.CreateRetVoid();
1076
1077 ScalarEvolution SE = buildSE(*F);
1078 auto *Loop = LI->getLoopFor(L);
1079 const SCEV *EC = SE.getBackedgeTakenCount(Loop);
1080 EXPECT_FALSE(isa<SCEVCouldNotCompute>(EC));
Max Kazantsevba1e70e2017-08-04 05:06:44 +00001081 EXPECT_FALSE(isa<SCEVConstant>(EC));
Max Kazantsev2cb36532017-08-03 08:41:30 +00001082
1083 SE.forgetValue(Load);
1084 Br->eraseFromParent();
1085 Cond->eraseFromParent();
1086 Load->eraseFromParent();
1087
1088 Builder.SetInsertPoint(L);
Benjamin Kramera73b4102017-08-03 15:59:37 +00001089 auto *NewCond = Builder.CreateICmp(
1090 ICmpInst::ICMP_SLT, Add, ConstantInt::get(T_int64, 2000), "new.cond");
1091 Builder.CreateCondBr(NewCond, L, Post);
Max Kazantsev2cb36532017-08-03 08:41:30 +00001092 const SCEV *NewEC = SE.getBackedgeTakenCount(Loop);
Max Kazantsevba1e70e2017-08-04 05:06:44 +00001093 EXPECT_FALSE(isa<SCEVCouldNotCompute>(NewEC));
1094 EXPECT_TRUE(isa<SCEVConstant>(NewEC));
1095 EXPECT_EQ(cast<SCEVConstant>(NewEC)->getAPInt().getLimitedValue(), 1999);
Max Kazantsev2cb36532017-08-03 08:41:30 +00001096}
1097
Dan Gohman7cac9572010-08-02 23:49:30 +00001098} // end anonymous namespace
1099} // end namespace llvm