Michael Zolotukhin | 1da4afd | 2016-02-08 23:03:59 +0000 | [diff] [blame] | 1 | //===- UnrollAnalyzerTest.cpp - UnrollAnalyzer 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/AsmParser/Parser.h" |
| 11 | #include "llvm/IR/LegacyPassManager.h" |
| 12 | #include "llvm/Support/SourceMgr.h" |
| 13 | #include "llvm/Analysis/LoopUnrollAnalyzer.h" |
| 14 | #include "llvm/IR/Dominators.h" |
| 15 | #include "gtest/gtest.h" |
| 16 | |
| 17 | using namespace llvm; |
| 18 | namespace llvm { |
| 19 | void initializeUnrollAnalyzerTestPass(PassRegistry &); |
| 20 | |
| 21 | static SmallVector<DenseMap<Value *, Constant *>, 16> SimplifiedValuesVector; |
| 22 | static unsigned TripCount = 0; |
| 23 | |
| 24 | namespace { |
| 25 | struct UnrollAnalyzerTest : public FunctionPass { |
| 26 | static char ID; |
| 27 | bool runOnFunction(Function &F) override { |
| 28 | LoopInfo *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
| 29 | ScalarEvolution *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
| 30 | |
| 31 | Function::iterator FI = F.begin(); |
| 32 | FI++; // First basic block is entry - skip it. |
| 33 | BasicBlock *Header = &*FI++; |
| 34 | Loop *L = LI->getLoopFor(Header); |
Michael Zolotukhin | 374651d | 2016-02-26 01:44:04 +0000 | [diff] [blame] | 35 | BasicBlock *Exiting = L->getExitingBlock(); |
Michael Zolotukhin | 1da4afd | 2016-02-08 23:03:59 +0000 | [diff] [blame] | 36 | |
| 37 | SimplifiedValuesVector.clear(); |
Michael Zolotukhin | 374651d | 2016-02-26 01:44:04 +0000 | [diff] [blame] | 38 | TripCount = SE->getSmallConstantTripCount(L, Exiting); |
Michael Zolotukhin | 1da4afd | 2016-02-08 23:03:59 +0000 | [diff] [blame] | 39 | for (unsigned Iteration = 0; Iteration < TripCount; Iteration++) { |
| 40 | DenseMap<Value *, Constant *> SimplifiedValues; |
Michael Zolotukhin | 9f520eb | 2016-02-26 02:57:05 +0000 | [diff] [blame^] | 41 | UnrolledInstAnalyzer Analyzer(Iteration, SimplifiedValues, *SE, L); |
Michael Zolotukhin | 374651d | 2016-02-26 01:44:04 +0000 | [diff] [blame] | 42 | for (auto *BB : L->getBlocks()) |
| 43 | for (Instruction &I : *BB) |
| 44 | Analyzer.visit(I); |
Michael Zolotukhin | 1da4afd | 2016-02-08 23:03:59 +0000 | [diff] [blame] | 45 | SimplifiedValuesVector.push_back(SimplifiedValues); |
| 46 | } |
| 47 | return false; |
| 48 | } |
| 49 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 50 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 51 | AU.addRequired<LoopInfoWrapperPass>(); |
| 52 | AU.addRequired<ScalarEvolutionWrapperPass>(); |
| 53 | AU.setPreservesAll(); |
| 54 | } |
| 55 | UnrollAnalyzerTest() : FunctionPass(ID) { |
| 56 | initializeUnrollAnalyzerTestPass(*PassRegistry::getPassRegistry()); |
| 57 | } |
| 58 | }; |
| 59 | } |
| 60 | |
| 61 | char UnrollAnalyzerTest::ID = 0; |
| 62 | |
| 63 | std::unique_ptr<Module> makeLLVMModule(UnrollAnalyzerTest *P, |
| 64 | const char *ModuleStr) { |
| 65 | LLVMContext &C = getGlobalContext(); |
| 66 | SMDiagnostic Err; |
| 67 | return parseAssemblyString(ModuleStr, Err, C); |
| 68 | } |
| 69 | |
| 70 | TEST(UnrollAnalyzerTest, BasicSimplifications) { |
| 71 | const char *ModuleStr = |
| 72 | "target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n" |
| 73 | "define i64 @propagate_loop_phis() {\n" |
| 74 | "entry:\n" |
| 75 | " br label %loop\n" |
| 76 | "loop:\n" |
| 77 | " %iv = phi i64 [ 0, %entry ], [ %inc, %loop ]\n" |
| 78 | " %x0 = phi i64 [ 0, %entry ], [ %x2, %loop ]\n" |
| 79 | " %x1 = or i64 %x0, 1\n" |
| 80 | " %x2 = or i64 %x1, 2\n" |
| 81 | " %inc = add nuw nsw i64 %iv, 1\n" |
| 82 | " %cond = icmp sge i64 %inc, 8\n" |
| 83 | " br i1 %cond, label %loop.end, label %loop\n" |
| 84 | "loop.end:\n" |
| 85 | " %x.lcssa = phi i64 [ %x2, %loop ]\n" |
| 86 | " ret i64 %x.lcssa\n" |
| 87 | "}\n"; |
| 88 | UnrollAnalyzerTest *P = new UnrollAnalyzerTest(); |
| 89 | std::unique_ptr<Module> M = makeLLVMModule(P, ModuleStr); |
| 90 | legacy::PassManager Passes; |
| 91 | Passes.add(P); |
| 92 | Passes.run(*M); |
| 93 | |
| 94 | // Perform checks |
| 95 | Module::iterator MI = M->begin(); |
| 96 | Function *F = &*MI++; |
| 97 | Function::iterator FI = F->begin(); |
| 98 | FI++; // First basic block is entry - skip it. |
| 99 | BasicBlock *Header = &*FI++; |
| 100 | |
| 101 | BasicBlock::iterator BBI = Header->begin(); |
| 102 | std::advance(BBI, 4); |
| 103 | Instruction *Y1 = &*BBI++; |
| 104 | Instruction *Y2 = &*BBI++; |
| 105 | // Check simplification expected on the 1st iteration. |
| 106 | // Check that "%inc = add nuw nsw i64 %iv, 1" is simplified to 1 |
| 107 | auto I1 = SimplifiedValuesVector[0].find(Y1); |
| 108 | EXPECT_TRUE(I1 != SimplifiedValuesVector[0].end()); |
| 109 | EXPECT_EQ(dyn_cast<ConstantInt>((*I1).second)->getZExtValue(), 1U); |
| 110 | |
| 111 | // Check that "%cond = icmp sge i64 %inc, 10" is simplified to false |
| 112 | auto I2 = SimplifiedValuesVector[0].find(Y2); |
| 113 | EXPECT_TRUE(I2 != SimplifiedValuesVector[0].end()); |
| 114 | EXPECT_FALSE(dyn_cast<ConstantInt>((*I2).second)->getZExtValue()); |
| 115 | |
| 116 | // Check simplification expected on the last iteration. |
| 117 | // Check that "%inc = add nuw nsw i64 %iv, 1" is simplified to 8 |
| 118 | I1 = SimplifiedValuesVector[TripCount - 1].find(Y1); |
| 119 | EXPECT_TRUE(I1 != SimplifiedValuesVector[TripCount - 1].end()); |
| 120 | EXPECT_EQ(dyn_cast<ConstantInt>((*I1).second)->getZExtValue(), TripCount); |
| 121 | |
| 122 | // Check that "%cond = icmp sge i64 %inc, 10" is simplified to false |
| 123 | I2 = SimplifiedValuesVector[TripCount - 1].find(Y2); |
| 124 | EXPECT_TRUE(I2 != SimplifiedValuesVector[TripCount - 1].end()); |
| 125 | EXPECT_TRUE(dyn_cast<ConstantInt>((*I2).second)->getZExtValue()); |
| 126 | } |
Michael Zolotukhin | 9f520eb | 2016-02-26 02:57:05 +0000 | [diff] [blame^] | 127 | |
| 128 | TEST(UnrollAnalyzerTest, OuterLoopSimplification) { |
| 129 | const char *ModuleStr = |
| 130 | "target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n" |
| 131 | "define void @foo() {\n" |
| 132 | "entry:\n" |
| 133 | " br label %outer.loop\n" |
| 134 | "outer.loop:\n" |
| 135 | " %iv.outer = phi i64 [ 0, %entry ], [ %iv.outer.next, %outer.loop.latch ]\n" |
| 136 | " br label %inner.loop\n" |
| 137 | "inner.loop:\n" |
| 138 | " %iv.inner = phi i64 [ 0, %outer.loop ], [ %iv.inner.next, %inner.loop ]\n" |
| 139 | " %iv.inner.next = add nuw nsw i64 %iv.inner, 1\n" |
| 140 | " %exitcond.inner = icmp eq i64 %iv.inner.next, 1000\n" |
| 141 | " br i1 %exitcond.inner, label %outer.loop.latch, label %inner.loop\n" |
| 142 | "outer.loop.latch:\n" |
| 143 | " %iv.outer.next = add nuw nsw i64 %iv.outer, 1\n" |
| 144 | " %exitcond.outer = icmp eq i64 %iv.outer.next, 40\n" |
| 145 | " br i1 %exitcond.outer, label %exit, label %outer.loop\n" |
| 146 | "exit:\n" |
| 147 | " ret void\n" |
| 148 | "}\n"; |
| 149 | |
| 150 | UnrollAnalyzerTest *P = new UnrollAnalyzerTest(); |
| 151 | std::unique_ptr<Module> M = makeLLVMModule(P, ModuleStr); |
| 152 | legacy::PassManager Passes; |
| 153 | Passes.add(P); |
| 154 | Passes.run(*M); |
| 155 | |
| 156 | Module::iterator MI = M->begin(); |
| 157 | Function *F = &*MI++; |
| 158 | Function::iterator FI = F->begin(); |
| 159 | FI++; |
| 160 | BasicBlock *Header = &*FI++; |
| 161 | BasicBlock *InnerBody = &*FI++; |
| 162 | |
| 163 | BasicBlock::iterator BBI = Header->begin(); |
| 164 | Instruction *Y1 = &*BBI++; |
| 165 | BBI = InnerBody->begin(); |
| 166 | Instruction *Y2 = &*BBI++; |
| 167 | // Check that we can simplify IV of the outer loop, but can't simplify the IV |
| 168 | // of the inner loop if we only know the iteration number of the outer loop. |
| 169 | auto I1 = SimplifiedValuesVector[0].find(Y1); |
| 170 | EXPECT_TRUE(I1 != SimplifiedValuesVector[0].end()); |
| 171 | auto I2 = SimplifiedValuesVector[0].find(Y2); |
| 172 | EXPECT_TRUE(I2 == SimplifiedValuesVector[0].end()); |
| 173 | } |
Michael Zolotukhin | 1da4afd | 2016-02-08 23:03:59 +0000 | [diff] [blame] | 174 | } // end namespace llvm |
| 175 | |
| 176 | INITIALIZE_PASS_BEGIN(UnrollAnalyzerTest, "unrollanalyzertestpass", |
| 177 | "unrollanalyzertestpass", false, false) |
| 178 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
| 179 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) |
| 180 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass) |
| 181 | INITIALIZE_PASS_END(UnrollAnalyzerTest, "unrollanalyzertestpass", |
| 182 | "unrollanalyzertestpass", false, false) |