summaryrefslogtreecommitdiffstats
path: root/llvm/unittests/Analysis/UnrollAnalyzer.cpp
blob: df8d2fac7e4e7e92b0a22980e5c51fb14b6c9112 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//===- UnrollAnalyzerTest.cpp - UnrollAnalyzer unit tests -----------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "llvm/AsmParser/Parser.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Analysis/LoopUnrollAnalyzer.h"
#include "llvm/IR/Dominators.h"
#include "gtest/gtest.h"

using namespace llvm;
namespace llvm {
void initializeUnrollAnalyzerTestPass(PassRegistry &);

static SmallVector<DenseMap<Value *, Constant *>, 16> SimplifiedValuesVector;
static unsigned TripCount = 0;

namespace {
struct UnrollAnalyzerTest : public FunctionPass {
  static char ID;
  bool runOnFunction(Function &F) override {
    LoopInfo *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
    ScalarEvolution *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();

    Function::iterator FI = F.begin();
    FI++; // First basic block is entry - skip it.
    BasicBlock *Header = &*FI++;
    Loop *L = LI->getLoopFor(Header);
    BasicBlock *Exiting = L->getExitingBlock();

    SimplifiedValuesVector.clear();
    TripCount = SE->getSmallConstantTripCount(L, Exiting);
    for (unsigned Iteration = 0; Iteration < TripCount; Iteration++) {
      DenseMap<Value *, Constant *> SimplifiedValues;
      UnrolledInstAnalyzer Analyzer(Iteration, SimplifiedValues, *SE, L);
      for (auto *BB : L->getBlocks())
        for (Instruction &I : *BB)
          Analyzer.visit(I);
      SimplifiedValuesVector.push_back(SimplifiedValues);
    }
    return false;
  }
  void getAnalysisUsage(AnalysisUsage &AU) const override {
    AU.addRequired<DominatorTreeWrapperPass>();
    AU.addRequired<LoopInfoWrapperPass>();
    AU.addRequired<ScalarEvolutionWrapperPass>();
    AU.setPreservesAll();
  }
  UnrollAnalyzerTest() : FunctionPass(ID) {
    initializeUnrollAnalyzerTestPass(*PassRegistry::getPassRegistry());
  }
};
}

char UnrollAnalyzerTest::ID = 0;

std::unique_ptr<Module> makeLLVMModule(UnrollAnalyzerTest *P,
                                       const char *ModuleStr) {
  LLVMContext &C = getGlobalContext();
  SMDiagnostic Err;
  return parseAssemblyString(ModuleStr, Err, C);
}

TEST(UnrollAnalyzerTest, BasicSimplifications) {
  const char *ModuleStr =
      "target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n"
      "define i64 @propagate_loop_phis() {\n"
      "entry:\n"
      "  br label %loop\n"
      "loop:\n"
      "  %iv = phi i64 [ 0, %entry ], [ %inc, %loop ]\n"
      "  %x0 = phi i64 [ 0, %entry ], [ %x2, %loop ]\n"
      "  %x1 = or i64 %x0, 1\n"
      "  %x2 = or i64 %x1, 2\n"
      "  %inc = add nuw nsw i64 %iv, 1\n"
      "  %cond = icmp sge i64 %inc, 8\n"
      "  br i1 %cond, label %loop.end, label %loop\n"
      "loop.end:\n"
      "  %x.lcssa = phi i64 [ %x2, %loop ]\n"
      "  ret i64 %x.lcssa\n"
      "}\n";
  UnrollAnalyzerTest *P = new UnrollAnalyzerTest();
  std::unique_ptr<Module> M = makeLLVMModule(P, ModuleStr);
  legacy::PassManager Passes;
  Passes.add(P);
  Passes.run(*M);

  // Perform checks
  Module::iterator MI = M->begin();
  Function *F = &*MI++;
  Function::iterator FI = F->begin();
  FI++; // First basic block is entry - skip it.
  BasicBlock *Header = &*FI++;

  BasicBlock::iterator BBI = Header->begin();
  std::advance(BBI, 4);
  Instruction *Y1 = &*BBI++;
  Instruction *Y2 = &*BBI++;
  // Check simplification expected on the 1st iteration.
  // Check that "%inc = add nuw nsw i64 %iv, 1" is simplified to 1
  auto I1 = SimplifiedValuesVector[0].find(Y1);
  EXPECT_TRUE(I1 != SimplifiedValuesVector[0].end());
  EXPECT_EQ(dyn_cast<ConstantInt>((*I1).second)->getZExtValue(), 1U);

  // Check that "%cond = icmp sge i64 %inc, 10" is simplified to false
  auto I2 = SimplifiedValuesVector[0].find(Y2);
  EXPECT_TRUE(I2 != SimplifiedValuesVector[0].end());
  EXPECT_FALSE(dyn_cast<ConstantInt>((*I2).second)->getZExtValue());

  // Check simplification expected on the last iteration.
  // Check that "%inc = add nuw nsw i64 %iv, 1" is simplified to 8
  I1 = SimplifiedValuesVector[TripCount - 1].find(Y1);
  EXPECT_TRUE(I1 != SimplifiedValuesVector[TripCount - 1].end());
  EXPECT_EQ(dyn_cast<ConstantInt>((*I1).second)->getZExtValue(), TripCount);

  // Check that "%cond = icmp sge i64 %inc, 10" is simplified to false
  I2 = SimplifiedValuesVector[TripCount - 1].find(Y2);
  EXPECT_TRUE(I2 != SimplifiedValuesVector[TripCount - 1].end());
  EXPECT_TRUE(dyn_cast<ConstantInt>((*I2).second)->getZExtValue());
}

TEST(UnrollAnalyzerTest, OuterLoopSimplification) {
  const char *ModuleStr =
      "target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n"
      "define void @foo() {\n"
      "entry:\n"
      "  br label %outer.loop\n"
      "outer.loop:\n"
      "  %iv.outer = phi i64 [ 0, %entry ], [ %iv.outer.next, %outer.loop.latch ]\n"
      "  br label %inner.loop\n"
      "inner.loop:\n"
      "  %iv.inner = phi i64 [ 0, %outer.loop ], [ %iv.inner.next, %inner.loop ]\n"
      "  %iv.inner.next = add nuw nsw i64 %iv.inner, 1\n"
      "  %exitcond.inner = icmp eq i64 %iv.inner.next, 1000\n"
      "  br i1 %exitcond.inner, label %outer.loop.latch, label %inner.loop\n"
      "outer.loop.latch:\n"
      "  %iv.outer.next = add nuw nsw i64 %iv.outer, 1\n"
      "  %exitcond.outer = icmp eq i64 %iv.outer.next, 40\n"
      "  br i1 %exitcond.outer, label %exit, label %outer.loop\n"
      "exit:\n"
      "  ret void\n"
      "}\n";

  UnrollAnalyzerTest *P = new UnrollAnalyzerTest();
  std::unique_ptr<Module> M = makeLLVMModule(P, ModuleStr);
  legacy::PassManager Passes;
  Passes.add(P);
  Passes.run(*M);

  Module::iterator MI = M->begin();
  Function *F = &*MI++;
  Function::iterator FI = F->begin();
  FI++;
  BasicBlock *Header = &*FI++;
  BasicBlock *InnerBody = &*FI++;

  BasicBlock::iterator BBI = Header->begin();
  Instruction *Y1 = &*BBI++;
  BBI = InnerBody->begin();
  Instruction *Y2 = &*BBI++;
  // Check that we can simplify IV of the outer loop, but can't simplify the IV
  // of the inner loop if we only know the iteration number of the outer loop.
  auto I1 = SimplifiedValuesVector[0].find(Y1);
  EXPECT_TRUE(I1 != SimplifiedValuesVector[0].end());
  auto I2 = SimplifiedValuesVector[0].find(Y2);
  EXPECT_TRUE(I2 == SimplifiedValuesVector[0].end());
}
} // end namespace llvm

INITIALIZE_PASS_BEGIN(UnrollAnalyzerTest, "unrollanalyzertestpass",
                      "unrollanalyzertestpass", false, false)
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
INITIALIZE_PASS_END(UnrollAnalyzerTest, "unrollanalyzertestpass",
                    "unrollanalyzertestpass", false, false)
OpenPOWER on IntegriCloud