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