Jingyue Wu | 8cb6b2a | 2015-04-14 04:59:22 +0000 | [diff] [blame^] | 1 | //===- NaryReassociate.cpp - Reassociate n-ary expressions ----------------===// |
| 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 | // This pass reassociates n-ary add expressions and eliminates the redundancy |
| 11 | // exposed by the reassociation. |
| 12 | // |
| 13 | // A motivating example: |
| 14 | // |
| 15 | // void foo(int a, int b) { |
| 16 | // bar(a + b); |
| 17 | // bar((a + 2) + b); |
| 18 | // } |
| 19 | // |
| 20 | // An ideal compiler should reassociate (a + 2) + b to (a + b) + 2 and simplify |
| 21 | // the above code to |
| 22 | // |
| 23 | // int t = a + b; |
| 24 | // bar(t); |
| 25 | // bar(t + 2); |
| 26 | // |
| 27 | // However, the Reassociate pass is unable to do that because it processes each |
| 28 | // instruction individually and believes (a + 2) + b is the best form according |
| 29 | // to its rank system. |
| 30 | // |
| 31 | // To address this limitation, NaryReassociate reassociates an expression in a |
| 32 | // form that reuses existing instructions. As a result, NaryReassociate can |
| 33 | // reassociate (a + 2) + b in the example to (a + b) + 2 because it detects that |
| 34 | // (a + b) is computed before. |
| 35 | // |
| 36 | // NaryReassociate works as follows. For every instruction in the form of (a + |
| 37 | // b) + c, it checks whether a + c or b + c is already computed by a dominating |
| 38 | // instruction. If so, it then reassociates (a + b) + c into (a + c) + b or (b + |
| 39 | // c) + a respectively. To efficiently look up whether an expression is |
| 40 | // computed before, we store each instruction seen and its SCEV into an |
| 41 | // SCEV-to-instruction map. |
| 42 | // |
| 43 | // Although the algorithm pattern-matches only ternary additions, it |
| 44 | // automatically handles many >3-ary expressions by walking through the function |
| 45 | // in the depth-first order. For example, given |
| 46 | // |
| 47 | // (a + c) + d |
| 48 | // ((a + b) + c) + d |
| 49 | // |
| 50 | // NaryReassociate first rewrites (a + b) + c to (a + c) + b, and then rewrites |
| 51 | // ((a + c) + b) + d into ((a + c) + d) + b. |
| 52 | // |
| 53 | // Limitations and TODO items: |
| 54 | // |
| 55 | // 1) We only considers n-ary adds for now. This should be extended and |
| 56 | // generalized. |
| 57 | // |
| 58 | // 2) Besides arithmetic operations, similar reassociation can be applied to |
| 59 | // GEPs. For example, if |
| 60 | // X = &arr[a] |
| 61 | // dominates |
| 62 | // Y = &arr[a + b] |
| 63 | // we may rewrite Y into X + b. |
| 64 | // |
| 65 | //===----------------------------------------------------------------------===// |
| 66 | |
| 67 | #include "llvm/Analysis/ScalarEvolution.h" |
| 68 | #include "llvm/IR/Dominators.h" |
| 69 | #include "llvm/IR/Module.h" |
| 70 | #include "llvm/IR/PatternMatch.h" |
| 71 | #include "llvm/Transforms/Scalar.h" |
| 72 | using namespace llvm; |
| 73 | using namespace PatternMatch; |
| 74 | |
| 75 | #define DEBUG_TYPE "nary-reassociate" |
| 76 | |
| 77 | namespace { |
| 78 | class NaryReassociate : public FunctionPass { |
| 79 | public: |
| 80 | static char ID; |
| 81 | |
| 82 | NaryReassociate(): FunctionPass(ID) { |
| 83 | initializeNaryReassociatePass(*PassRegistry::getPassRegistry()); |
| 84 | } |
| 85 | |
| 86 | bool runOnFunction(Function &F) override; |
| 87 | |
| 88 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 89 | AU.addPreserved<DominatorTreeWrapperPass>(); |
| 90 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 91 | // TODO: can we preserve ScalarEvolution? |
| 92 | AU.addRequired<ScalarEvolution>(); |
| 93 | AU.setPreservesCFG(); |
| 94 | } |
| 95 | |
| 96 | private: |
| 97 | // Reasssociates I to a better form. |
| 98 | Instruction *tryReassociateAdd(Instruction *I); |
| 99 | // A helper function for tryReassociateAdd. LHS and RHS are explicitly passed. |
| 100 | Instruction *tryReassociateAdd(Value *LHS, Value *RHS, Instruction *I); |
| 101 | // Rewrites I to LHS + RHS if LHS is computed already. |
| 102 | Instruction *tryReassociatedAdd(const SCEV *LHS, Value *RHS, Instruction *I); |
| 103 | |
| 104 | DominatorTree *DT; |
| 105 | ScalarEvolution *SE; |
| 106 | // A lookup table quickly telling which instructions compute the given SCEV. |
| 107 | // Note that there can be multiple instructions at different locations |
| 108 | // computing to the same SCEV. For example, |
| 109 | // if (p1) |
| 110 | // foo(a + b); |
| 111 | // if (p2) |
| 112 | // bar(a + b); |
| 113 | DenseMap<const SCEV *, SmallVector<Instruction *, 2>> SeenExprs; |
| 114 | }; |
| 115 | } // anonymous namespace |
| 116 | |
| 117 | char NaryReassociate::ID = 0; |
| 118 | INITIALIZE_PASS_BEGIN(NaryReassociate, "nary-reassociate", "Nary reassociation", |
| 119 | false, false) |
| 120 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
| 121 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolution) |
| 122 | INITIALIZE_PASS_END(NaryReassociate, "nary-reassociate", "Nary reassociation", |
| 123 | false, false) |
| 124 | |
| 125 | FunctionPass *llvm::createNaryReassociatePass() { |
| 126 | return new NaryReassociate(); |
| 127 | } |
| 128 | |
| 129 | bool NaryReassociate::runOnFunction(Function &F) { |
| 130 | if (skipOptnoneFunction(F)) |
| 131 | return false; |
| 132 | |
| 133 | DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
| 134 | SE = &getAnalysis<ScalarEvolution>(); |
| 135 | |
| 136 | // Traverse the dominator tree in the depth-first order. This order makes sure |
| 137 | // all bases of a candidate are in Candidates when we process it. |
| 138 | bool Changed = false; |
| 139 | SeenExprs.clear(); |
| 140 | for (auto Node = GraphTraits<DominatorTree *>::nodes_begin(DT); |
| 141 | Node != GraphTraits<DominatorTree *>::nodes_end(DT); ++Node) { |
| 142 | BasicBlock *BB = Node->getBlock(); |
| 143 | for (auto I = BB->begin(); I != BB->end(); ++I) { |
| 144 | if (I->getOpcode() == Instruction::Add) { |
| 145 | if (Instruction *NewI = tryReassociateAdd(I)) { |
| 146 | I->replaceAllUsesWith(NewI); |
| 147 | I->eraseFromParent(); |
| 148 | I = NewI; |
| 149 | } |
| 150 | // We should add the rewritten instruction because tryReassociateAdd may |
| 151 | // have invalidated the original one. |
| 152 | SeenExprs[SE->getSCEV(I)].push_back(I); |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | return Changed; |
| 157 | } |
| 158 | |
| 159 | Instruction *NaryReassociate::tryReassociateAdd(Instruction *I) { |
| 160 | Value *LHS = I->getOperand(0), *RHS = I->getOperand(1); |
| 161 | if (auto *NewI = tryReassociateAdd(LHS, RHS, I)) |
| 162 | return NewI; |
| 163 | if (auto *NewI = tryReassociateAdd(RHS, LHS, I)) |
| 164 | return NewI; |
| 165 | return nullptr; |
| 166 | } |
| 167 | |
| 168 | Instruction *NaryReassociate::tryReassociateAdd(Value *LHS, Value *RHS, |
| 169 | Instruction *I) { |
| 170 | Value *A = nullptr, *B = nullptr; |
| 171 | // To be conservative, we reassociate I only when it is the only user of A+B. |
| 172 | if (LHS->hasOneUse() && match(LHS, m_Add(m_Value(A), m_Value(B)))) { |
| 173 | // I = (A + B) + RHS |
| 174 | // = (A + RHS) + B or (B + RHS) + A |
| 175 | const SCEV *AExpr = SE->getSCEV(A), *BExpr = SE->getSCEV(B); |
| 176 | const SCEV *RHSExpr = SE->getSCEV(RHS); |
| 177 | if (auto *NewI = tryReassociatedAdd(SE->getAddExpr(AExpr, RHSExpr), B, I)) |
| 178 | return NewI; |
| 179 | if (auto *NewI = tryReassociatedAdd(SE->getAddExpr(BExpr, RHSExpr), A, I)) |
| 180 | return NewI; |
| 181 | } |
| 182 | return nullptr; |
| 183 | } |
| 184 | |
| 185 | Instruction *NaryReassociate::tryReassociatedAdd(const SCEV *LHSExpr, |
| 186 | Value *RHS, Instruction *I) { |
| 187 | auto Pos = SeenExprs.find(LHSExpr); |
| 188 | // Bail out if LHSExpr is not previously seen. |
| 189 | if (Pos == SeenExprs.end()) |
| 190 | return nullptr; |
| 191 | |
| 192 | auto &LHSCandidates = Pos->second; |
| 193 | unsigned NumIterations = 0; |
| 194 | // Search at most 10 items to avoid running quadratically. |
| 195 | static const unsigned MaxNumIterations = 10; |
| 196 | for (auto LHS = LHSCandidates.rbegin(); |
| 197 | LHS != LHSCandidates.rend() && NumIterations < MaxNumIterations; |
| 198 | ++LHS, ++NumIterations) { |
| 199 | if (DT->dominates(*LHS, I)) { |
| 200 | Instruction *NewI = BinaryOperator::CreateAdd(*LHS, RHS, "", I); |
| 201 | NewI->takeName(I); |
| 202 | return NewI; |
| 203 | } |
| 204 | } |
| 205 | return nullptr; |
| 206 | } |