Skip to content

Commit 6155771

Browse files
committed
[UnrollAnalyzerTest] Remove dependency to Pass Managers
Remove use of LegacyPassManager in the UnrollAnalyzerTest unit test. Given that the goal isn't to test pass manager interfaces, and since the LoopUnrollAnalyzer isn't even implemented as a pass, we do not really need the complexity of using a pass manager. Instead we just make sure that we run LoopUnrollAnalyzer and other needed analyses standalone (without any pass manager). This was inspired by the LoopInfoTest unit test.
1 parent f12059e commit 6155771

File tree

1 file changed

+36
-65
lines changed

1 file changed

+36
-65
lines changed

llvm/unittests/Analysis/UnrollAnalyzerTest.cpp

Lines changed: 36 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -6,61 +6,52 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include "llvm/Analysis/AssumptionCache.h"
910
#include "llvm/Analysis/LoopInfo.h"
1011
#include "llvm/Analysis/LoopUnrollAnalyzer.h"
12+
#include "llvm/Analysis/ScalarEvolution.h"
13+
#include "llvm/Analysis/TargetLibraryInfo.h"
1114
#include "llvm/AsmParser/Parser.h"
1215
#include "llvm/IR/Dominators.h"
13-
#include "llvm/IR/LegacyPassManager.h"
14-
#include "llvm/InitializePasses.h"
1516
#include "llvm/Support/SourceMgr.h"
1617
#include "gtest/gtest.h"
1718

1819
using namespace llvm;
1920
namespace llvm {
20-
void initializeUnrollAnalyzerTestPass(PassRegistry &);
2121

2222
static SmallVector<DenseMap<Value *, Value *>, 16> SimplifiedValuesVector;
2323
static unsigned TripCount = 0;
2424

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();
25+
/// Build loop info and scalar evolution for the function and run the analysis.
26+
static void runUnrollAnalyzer(Module &M, StringRef FuncName) {
27+
auto *F = M.getFunction(FuncName);
28+
ASSERT_NE(F, nullptr) << "Could not find " << FuncName;
3129

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);
3736

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+
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);
5852
}
59-
};
6053
}
6154

62-
char UnrollAnalyzerTest::ID = 0;
63-
6455
std::unique_ptr<Module> makeLLVMModule(LLVMContext &Context,
6556
const char *ModuleStr) {
6657
SMDiagnostic Err;
@@ -85,12 +76,9 @@ TEST(UnrollAnalyzerTest, BasicSimplifications) {
8576
" %x.lcssa = phi i64 [ %x2, %loop ]\n"
8677
" ret i64 %x.lcssa\n"
8778
"}\n";
88-
UnrollAnalyzerTest *P = new UnrollAnalyzerTest();
8979
LLVMContext Context;
9080
std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
91-
legacy::PassManager Passes;
92-
Passes.add(P);
93-
Passes.run(*M);
81+
runUnrollAnalyzer(*M, "propagate_loop_phis");
9482

9583
// Perform checks
9684
Module::iterator MI = M->begin();
@@ -148,12 +136,9 @@ TEST(UnrollAnalyzerTest, OuterLoopSimplification) {
148136
" ret void\n"
149137
"}\n";
150138

151-
UnrollAnalyzerTest *P = new UnrollAnalyzerTest();
152139
LLVMContext Context;
153140
std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
154-
legacy::PassManager Passes;
155-
Passes.add(P);
156-
Passes.run(*M);
141+
runUnrollAnalyzer(*M, "foo");
157142

158143
Module::iterator MI = M->begin();
159144
Function *F = &*MI++;
@@ -177,6 +162,7 @@ TEST(UnrollAnalyzerTest, OuterLoopSimplification) {
177162
auto I2 = SimplifiedValuesVector[0].find(Y2);
178163
EXPECT_TRUE(I2 == SimplifiedValuesVector[0].end());
179164
}
165+
180166
TEST(UnrollAnalyzerTest, CmpSimplifications) {
181167
const char *ModuleStr =
182168
"target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n"
@@ -193,12 +179,9 @@ TEST(UnrollAnalyzerTest, CmpSimplifications) {
193179
"for.end:\n"
194180
" ret void\n"
195181
"}\n";
196-
UnrollAnalyzerTest *P = new UnrollAnalyzerTest();
197182
LLVMContext Context;
198183
std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
199-
legacy::PassManager Passes;
200-
Passes.add(P);
201-
Passes.run(*M);
184+
runUnrollAnalyzer(*M, "branch_iv_trunc");
202185

203186
// Perform checks
204187
Module::iterator MI = M->begin();
@@ -221,6 +204,7 @@ TEST(UnrollAnalyzerTest, CmpSimplifications) {
221204
EXPECT_TRUE(I2 != SimplifiedValuesVector[5].end());
222205
EXPECT_EQ(cast<ConstantInt>((*I2).second)->getZExtValue(), 1U);
223206
}
207+
224208
TEST(UnrollAnalyzerTest, PtrCmpSimplifications) {
225209
const char *ModuleStr =
226210
"target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n"
@@ -240,12 +224,9 @@ TEST(UnrollAnalyzerTest, PtrCmpSimplifications) {
240224
"loop.exit:\n"
241225
" ret void\n"
242226
"}\n";
243-
UnrollAnalyzerTest *P = new UnrollAnalyzerTest();
244227
LLVMContext Context;
245228
std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
246-
legacy::PassManager Passes;
247-
Passes.add(P);
248-
Passes.run(*M);
229+
runUnrollAnalyzer(*M, "ptr_cmp");
249230

250231
// Perform checks
251232
Module::iterator MI = M->begin();
@@ -263,6 +244,7 @@ TEST(UnrollAnalyzerTest, PtrCmpSimplifications) {
263244
EXPECT_TRUE(I1 != SimplifiedValuesVector[5].end());
264245
EXPECT_EQ(cast<ConstantInt>((*I1).second)->getZExtValue(), 0U);
265246
}
247+
266248
TEST(UnrollAnalyzerTest, CastSimplifications) {
267249
const char *ModuleStr =
268250
"target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n"
@@ -286,12 +268,9 @@ TEST(UnrollAnalyzerTest, CastSimplifications) {
286268
" ret void\n"
287269
"}\n";
288270

289-
UnrollAnalyzerTest *P = new UnrollAnalyzerTest();
290271
LLVMContext Context;
291272
std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
292-
legacy::PassManager Passes;
293-
Passes.add(P);
294-
Passes.run(*M);
273+
runUnrollAnalyzer(*M, "const_load_cast");
295274

296275
// Perform checks
297276
Module::iterator MI = M->begin();
@@ -321,11 +300,3 @@ TEST(UnrollAnalyzerTest, CastSimplifications) {
321300
}
322301

323302
} // 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

Comments
 (0)