6
6
//
7
7
// ===----------------------------------------------------------------------===//
8
8
9
+ #include " llvm/Analysis/AssumptionCache.h"
9
10
#include " llvm/Analysis/LoopInfo.h"
10
11
#include " llvm/Analysis/LoopUnrollAnalyzer.h"
12
+ #include " llvm/Analysis/ScalarEvolution.h"
13
+ #include " llvm/Analysis/TargetLibraryInfo.h"
11
14
#include " llvm/AsmParser/Parser.h"
12
15
#include " llvm/IR/Dominators.h"
13
- #include " llvm/IR/LegacyPassManager.h"
14
- #include " llvm/InitializePasses.h"
15
16
#include " llvm/Support/SourceMgr.h"
16
17
#include " gtest/gtest.h"
17
18
18
19
using namespace llvm ;
19
- namespace llvm {
20
- void initializeUnrollAnalyzerTestPass (PassRegistry &);
21
20
22
- static SmallVector<DenseMap<Value *, Value *>, 16 > SimplifiedValuesVector;
23
- static unsigned TripCount = 0 ;
21
+ typedef SmallVector<DenseMap<Value *, Value *>, 16 > SimplifiedValuesVectorTy;
24
22
25
- namespace {
26
- struct UnrollAnalyzerTest : public FunctionPass {
27
- static char ID;
28
- bool runOnFunction (Function &F) override {
29
- LoopInfo *LI = &getAnalysis<LoopInfoWrapperPass>(). getLoopInfo ( );
30
- ScalarEvolution *SE = &getAnalysis<ScalarEvolutionWrapperPass>(). getSE () ;
23
+ // / Build loop info and scalar evolution for the function and run the analysis.
24
+ static void
25
+ runUnrollAnalyzer (Module &M, StringRef FuncName,
26
+ SimplifiedValuesVectorTy &SimplifiedValuesVector) {
27
+ auto *F = M. getFunction (FuncName );
28
+ ASSERT_NE (F, nullptr ) << " Could not find " << FuncName ;
31
29
32
- Function::iterator FI = F.begin ();
33
- FI++; // First basic block is entry - skip it.
34
- BasicBlock *Header = &*FI++;
35
- Loop *L = LI->getLoopFor (Header);
36
- BasicBlock *Exiting = L->getExitingBlock ();
30
+ TargetLibraryInfoImpl TLII;
31
+ TargetLibraryInfo TLI (TLII);
32
+ AssumptionCache AC (*F);
33
+ DominatorTree DT (*F);
34
+ LoopInfo LI (DT);
35
+ ScalarEvolution SE (*F, TLI, AC, DT, LI);
37
36
38
- SimplifiedValuesVector.clear ();
39
- TripCount = SE->getSmallConstantTripCount (L, Exiting);
40
- for (unsigned Iteration = 0 ; Iteration < TripCount; Iteration++) {
41
- DenseMap<Value *, Value *> SimplifiedValues;
42
- UnrolledInstAnalyzer Analyzer (Iteration, SimplifiedValues, *SE, L);
43
- for (auto *BB : L->getBlocks ())
44
- for (Instruction &I : *BB)
45
- Analyzer.visit (I);
46
- SimplifiedValuesVector.push_back (SimplifiedValues);
47
- }
48
- return false ;
49
- }
50
- void getAnalysisUsage (AnalysisUsage &AU) const override {
51
- AU.addRequired <DominatorTreeWrapperPass>();
52
- AU.addRequired <LoopInfoWrapperPass>();
53
- AU.addRequired <ScalarEvolutionWrapperPass>();
54
- AU.setPreservesAll ();
55
- }
56
- UnrollAnalyzerTest () : FunctionPass(ID) {
57
- initializeUnrollAnalyzerTestPass (*PassRegistry::getPassRegistry ());
37
+ Function::iterator FI = F->begin ();
38
+ FI++; // First basic block is entry - skip it.
39
+ BasicBlock *Header = &*FI++;
40
+ Loop *L = LI.getLoopFor (Header);
41
+ BasicBlock *Exiting = L->getExitingBlock ();
42
+
43
+ SimplifiedValuesVector.clear ();
44
+ unsigned TripCount = SE.getSmallConstantTripCount (L, Exiting);
45
+ for (unsigned Iteration = 0 ; Iteration < TripCount; Iteration++) {
46
+ DenseMap<Value *, Value *> SimplifiedValues;
47
+ UnrolledInstAnalyzer Analyzer (Iteration, SimplifiedValues, SE, L);
48
+ for (auto *BB : L->getBlocks ())
49
+ for (Instruction &I : *BB)
50
+ Analyzer.visit (I);
51
+ SimplifiedValuesVector.push_back (SimplifiedValues);
58
52
}
59
- };
60
53
}
61
54
62
- char UnrollAnalyzerTest::ID = 0 ;
63
-
64
55
std::unique_ptr<Module> makeLLVMModule (LLVMContext &Context,
65
56
const char *ModuleStr) {
66
57
SMDiagnostic Err;
@@ -85,12 +76,11 @@ TEST(UnrollAnalyzerTest, BasicSimplifications) {
85
76
" %x.lcssa = phi i64 [ %x2, %loop ]\n "
86
77
" ret i64 %x.lcssa\n "
87
78
" }\n " ;
88
- UnrollAnalyzerTest *P = new UnrollAnalyzerTest ();
89
79
LLVMContext Context;
90
80
std::unique_ptr<Module> M = makeLLVMModule (Context, ModuleStr);
91
- legacy::PassManager Passes ;
92
- Passes. add (P );
93
- Passes. run (*M );
81
+ SimplifiedValuesVectorTy SimplifiedValuesVector ;
82
+ runUnrollAnalyzer (*M, " propagate_loop_phis " , SimplifiedValuesVector );
83
+ unsigned TripCount = SimplifiedValuesVector. size ( );
94
84
95
85
// Perform checks
96
86
Module::iterator MI = M->begin ();
@@ -148,12 +138,10 @@ TEST(UnrollAnalyzerTest, OuterLoopSimplification) {
148
138
" ret void\n "
149
139
" }\n " ;
150
140
151
- UnrollAnalyzerTest *P = new UnrollAnalyzerTest ();
152
141
LLVMContext Context;
153
142
std::unique_ptr<Module> M = makeLLVMModule (Context, ModuleStr);
154
- legacy::PassManager Passes;
155
- Passes.add (P);
156
- Passes.run (*M);
143
+ SimplifiedValuesVectorTy SimplifiedValuesVector;
144
+ runUnrollAnalyzer (*M, " foo" , SimplifiedValuesVector);
157
145
158
146
Module::iterator MI = M->begin ();
159
147
Function *F = &*MI++;
@@ -177,6 +165,7 @@ TEST(UnrollAnalyzerTest, OuterLoopSimplification) {
177
165
auto I2 = SimplifiedValuesVector[0 ].find (Y2);
178
166
EXPECT_TRUE (I2 == SimplifiedValuesVector[0 ].end ());
179
167
}
168
+
180
169
TEST (UnrollAnalyzerTest, CmpSimplifications) {
181
170
const char *ModuleStr =
182
171
" target datalayout = \" e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n "
@@ -193,12 +182,10 @@ TEST(UnrollAnalyzerTest, CmpSimplifications) {
193
182
" for.end:\n "
194
183
" ret void\n "
195
184
" }\n " ;
196
- UnrollAnalyzerTest *P = new UnrollAnalyzerTest ();
197
185
LLVMContext Context;
198
186
std::unique_ptr<Module> M = makeLLVMModule (Context, ModuleStr);
199
- legacy::PassManager Passes;
200
- Passes.add (P);
201
- Passes.run (*M);
187
+ SimplifiedValuesVectorTy SimplifiedValuesVector;
188
+ runUnrollAnalyzer (*M, " branch_iv_trunc" , SimplifiedValuesVector);
202
189
203
190
// Perform checks
204
191
Module::iterator MI = M->begin ();
@@ -221,6 +208,7 @@ TEST(UnrollAnalyzerTest, CmpSimplifications) {
221
208
EXPECT_TRUE (I2 != SimplifiedValuesVector[5 ].end ());
222
209
EXPECT_EQ (cast<ConstantInt>((*I2).second )->getZExtValue (), 1U );
223
210
}
211
+
224
212
TEST (UnrollAnalyzerTest, PtrCmpSimplifications) {
225
213
const char *ModuleStr =
226
214
" target datalayout = \" e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n "
@@ -240,12 +228,10 @@ TEST(UnrollAnalyzerTest, PtrCmpSimplifications) {
240
228
" loop.exit:\n "
241
229
" ret void\n "
242
230
" }\n " ;
243
- UnrollAnalyzerTest *P = new UnrollAnalyzerTest ();
244
231
LLVMContext Context;
245
232
std::unique_ptr<Module> M = makeLLVMModule (Context, ModuleStr);
246
- legacy::PassManager Passes;
247
- Passes.add (P);
248
- Passes.run (*M);
233
+ SimplifiedValuesVectorTy SimplifiedValuesVector;
234
+ runUnrollAnalyzer (*M, " ptr_cmp" , SimplifiedValuesVector);
249
235
250
236
// Perform checks
251
237
Module::iterator MI = M->begin ();
@@ -263,6 +249,7 @@ TEST(UnrollAnalyzerTest, PtrCmpSimplifications) {
263
249
EXPECT_TRUE (I1 != SimplifiedValuesVector[5 ].end ());
264
250
EXPECT_EQ (cast<ConstantInt>((*I1).second )->getZExtValue (), 0U );
265
251
}
252
+
266
253
TEST (UnrollAnalyzerTest, CastSimplifications) {
267
254
const char *ModuleStr =
268
255
" target datalayout = \" e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n "
@@ -286,12 +273,10 @@ TEST(UnrollAnalyzerTest, CastSimplifications) {
286
273
" ret void\n "
287
274
" }\n " ;
288
275
289
- UnrollAnalyzerTest *P = new UnrollAnalyzerTest ();
290
276
LLVMContext Context;
291
277
std::unique_ptr<Module> M = makeLLVMModule (Context, ModuleStr);
292
- legacy::PassManager Passes;
293
- Passes.add (P);
294
- Passes.run (*M);
278
+ SimplifiedValuesVectorTy SimplifiedValuesVector;
279
+ runUnrollAnalyzer (*M, " const_load_cast" , SimplifiedValuesVector);
295
280
296
281
// Perform checks
297
282
Module::iterator MI = M->begin ();
@@ -319,13 +304,3 @@ TEST(UnrollAnalyzerTest, CastSimplifications) {
319
304
EXPECT_TRUE (I3 != SimplifiedValuesVector[5 ].end ());
320
305
EXPECT_EQ (cast<ConstantInt>((*I3).second )->getZExtValue (), 3U );
321
306
}
322
-
323
- } // end namespace llvm
324
-
325
- INITIALIZE_PASS_BEGIN (UnrollAnalyzerTest, " unrollanalyzertestpass" ,
326
- " unrollanalyzertestpass" , false , false )
327
- INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
328
- INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
329
- INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
330
- INITIALIZE_PASS_END(UnrollAnalyzerTest, " unrollanalyzertestpass" ,
331
- " unrollanalyzertestpass" , false , false )
0 commit comments