Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 1 | //===- Reassociate.cpp - Reassociate binary expressions -------------------===// |
| 2 | // |
| 3 | // This pass reassociates commutative expressions in an order that is designed |
| 4 | // to promote better constant propogation, GCSE, LICM, PRE... |
| 5 | // |
| 6 | // For example: 4 + (x + 5) -> x + (4 + 5) |
| 7 | // |
| 8 | // Note that this pass works best if left shifts have been promoted to explicit |
| 9 | // multiplies before this pass executes. |
| 10 | // |
| 11 | // In the implementation of this algorithm, constants are assigned rank = 0, |
| 12 | // function arguments are rank = 1, and other values are assigned ranks |
| 13 | // corresponding to the reverse post order traversal of current function |
| 14 | // (starting at 2), which effectively gives values in deep loops higher rank |
| 15 | // than values not in loops. |
| 16 | // |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
| 19 | #include "llvm/Transforms/Scalar.h" |
| 20 | #include "llvm/Function.h" |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 21 | #include "llvm/iOperators.h" |
| 22 | #include "llvm/Type.h" |
| 23 | #include "llvm/Pass.h" |
| 24 | #include "llvm/Constant.h" |
| 25 | #include "llvm/Support/CFG.h" |
| 26 | #include "Support/PostOrderIterator.h" |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 27 | #include "Support/Statistic.h" |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 28 | |
| 29 | namespace { |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 30 | Statistic<> NumLinear ("reassociate","Number of insts linearized"); |
| 31 | Statistic<> NumChanged("reassociate","Number of insts reassociated"); |
| 32 | Statistic<> NumSwapped("reassociate","Number of insts with operands swapped"); |
| 33 | |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 34 | class Reassociate : public FunctionPass { |
Chris Lattner | 10073a9 | 2002-07-25 06:17:51 +0000 | [diff] [blame] | 35 | std::map<BasicBlock*, unsigned> RankMap; |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 36 | public: |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 37 | bool runOnFunction(Function &F); |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 38 | |
| 39 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | 820d971 | 2002-10-21 20:00:28 +0000 | [diff] [blame] | 40 | AU.setPreservesCFG(); |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 41 | } |
| 42 | private: |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 43 | void BuildRankMap(Function &F); |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 44 | unsigned getRank(Value *V); |
| 45 | bool ReassociateExpr(BinaryOperator *I); |
| 46 | bool ReassociateBB(BasicBlock *BB); |
| 47 | }; |
Chris Lattner | b28b680 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 48 | |
Chris Lattner | c8b7092 | 2002-07-26 21:12:46 +0000 | [diff] [blame] | 49 | RegisterOpt<Reassociate> X("reassociate", "Reassociate expressions"); |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | Pass *createReassociatePass() { return new Reassociate(); } |
| 53 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 54 | void Reassociate::BuildRankMap(Function &F) { |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 55 | unsigned i = 1; |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 56 | ReversePostOrderTraversal<Function*> RPOT(&F); |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 57 | for (ReversePostOrderTraversal<Function*>::rpo_iterator I = RPOT.begin(), |
| 58 | E = RPOT.end(); I != E; ++I) |
| 59 | RankMap[*I] = ++i; |
| 60 | } |
| 61 | |
| 62 | unsigned Reassociate::getRank(Value *V) { |
| 63 | if (isa<Argument>(V)) return 1; // Function argument... |
| 64 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
| 65 | // If this is an expression, return the MAX(rank(LHS), rank(RHS)) so that we |
| 66 | // can reassociate expressions for code motion! Since we do not recurse for |
| 67 | // PHI nodes, we cannot have infinite recursion here, because there cannot |
| 68 | // be loops in the value graph (except for PHI nodes). |
| 69 | // |
| 70 | if (I->getOpcode() == Instruction::PHINode || |
| 71 | I->getOpcode() == Instruction::Alloca || |
| 72 | I->getOpcode() == Instruction::Malloc || isa<TerminatorInst>(I) || |
| 73 | I->hasSideEffects()) |
| 74 | return RankMap[I->getParent()]; |
| 75 | |
Chris Lattner | 7bc532d | 2002-05-16 04:37:07 +0000 | [diff] [blame] | 76 | unsigned Rank = 0, MaxRank = RankMap[I->getParent()]; |
| 77 | for (unsigned i = 0, e = I->getNumOperands(); |
| 78 | i != e && Rank != MaxRank; ++i) |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 79 | Rank = std::max(Rank, getRank(I->getOperand(i))); |
| 80 | |
| 81 | return Rank; |
| 82 | } |
| 83 | |
| 84 | // Otherwise it's a global or constant, rank 0. |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | |
| 89 | // isCommutativeOperator - Return true if the specified instruction is |
| 90 | // commutative and associative. If the instruction is not commutative and |
| 91 | // associative, we can not reorder its operands! |
| 92 | // |
| 93 | static inline BinaryOperator *isCommutativeOperator(Instruction *I) { |
| 94 | // Floating point operations do not commute! |
| 95 | if (I->getType()->isFloatingPoint()) return 0; |
| 96 | |
| 97 | if (I->getOpcode() == Instruction::Add || |
| 98 | I->getOpcode() == Instruction::Mul || |
| 99 | I->getOpcode() == Instruction::And || |
| 100 | I->getOpcode() == Instruction::Or || |
| 101 | I->getOpcode() == Instruction::Xor) |
| 102 | return cast<BinaryOperator>(I); |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | |
| 107 | bool Reassociate::ReassociateExpr(BinaryOperator *I) { |
| 108 | Value *LHS = I->getOperand(0); |
| 109 | Value *RHS = I->getOperand(1); |
| 110 | unsigned LHSRank = getRank(LHS); |
| 111 | unsigned RHSRank = getRank(RHS); |
| 112 | |
| 113 | bool Changed = false; |
| 114 | |
| 115 | // Make sure the LHS of the operand always has the greater rank... |
| 116 | if (LHSRank < RHSRank) { |
| 117 | I->swapOperands(); |
| 118 | std::swap(LHS, RHS); |
| 119 | std::swap(LHSRank, RHSRank); |
| 120 | Changed = true; |
Chris Lattner | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 121 | ++NumSwapped; |
Chris Lattner | 71cbd42 | 2002-05-22 17:17:27 +0000 | [diff] [blame] | 122 | DEBUG(std::cerr << "Transposed: " << I << " Result BB: " << I->getParent()); |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | // If the LHS is the same operator as the current one is, and if we are the |
| 126 | // only expression using it... |
| 127 | // |
| 128 | if (BinaryOperator *LHSI = dyn_cast<BinaryOperator>(LHS)) |
| 129 | if (LHSI->getOpcode() == I->getOpcode() && LHSI->use_size() == 1) { |
| 130 | // If the rank of our current RHS is less than the rank of the LHS's LHS, |
| 131 | // then we reassociate the two instructions... |
| 132 | if (RHSRank < getRank(LHSI->getOperand(0))) { |
| 133 | unsigned TakeOp = 0; |
| 134 | if (BinaryOperator *IOp = dyn_cast<BinaryOperator>(LHSI->getOperand(0))) |
| 135 | if (IOp->getOpcode() == LHSI->getOpcode()) |
| 136 | TakeOp = 1; // Hoist out non-tree portion |
| 137 | |
| 138 | // Convert ((a + 12) + 10) into (a + (12 + 10)) |
| 139 | I->setOperand(0, LHSI->getOperand(TakeOp)); |
| 140 | LHSI->setOperand(TakeOp, RHS); |
| 141 | I->setOperand(1, LHSI); |
| 142 | |
Chris Lattner | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 143 | ++NumChanged; |
Chris Lattner | 71cbd42 | 2002-05-22 17:17:27 +0000 | [diff] [blame] | 144 | DEBUG(std::cerr << "Reassociated: " << I << " Result BB: " |
| 145 | << I->getParent()); |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 146 | |
| 147 | // Since we modified the RHS instruction, make sure that we recheck it. |
| 148 | ReassociateExpr(LHSI); |
| 149 | return true; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | return Changed; |
| 154 | } |
| 155 | |
| 156 | |
Chris Lattner | 7bc532d | 2002-05-16 04:37:07 +0000 | [diff] [blame] | 157 | // NegateValue - Insert instructions before the instruction pointed to by BI, |
| 158 | // that computes the negative version of the value specified. The negative |
| 159 | // version of the value is returned, and BI is left pointing at the instruction |
| 160 | // that should be processed next by the reassociation pass. |
| 161 | // |
| 162 | static Value *NegateValue(Value *V, BasicBlock *BB, BasicBlock::iterator &BI) { |
| 163 | // We are trying to expose opportunity for reassociation. One of the things |
| 164 | // that we want to do to achieve this is to push a negation as deep into an |
| 165 | // expression chain as possible, to expose the add instructions. In practice, |
| 166 | // this means that we turn this: |
| 167 | // X = -(A+12+C+D) into X = -A + -12 + -C + -D = -12 + -A + -C + -D |
| 168 | // so that later, a: Y = 12+X could get reassociated with the -12 to eliminate |
| 169 | // the constants. We assume that instcombine will clean up the mess later if |
| 170 | // we introduce tons of unneccesary negation instructions... |
| 171 | // |
| 172 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 173 | if (I->getOpcode() == Instruction::Add && I->use_size() == 1) { |
| 174 | Value *RHS = NegateValue(I->getOperand(1), BB, BI); |
| 175 | Value *LHS = NegateValue(I->getOperand(0), BB, BI); |
| 176 | |
| 177 | // We must actually insert a new add instruction here, because the neg |
| 178 | // instructions do not dominate the old add instruction in general. By |
| 179 | // adding it now, we are assured that the neg instructions we just |
| 180 | // inserted dominate the instruction we are about to insert after them. |
| 181 | // |
Chris Lattner | 28a8d24 | 2002-09-10 17:04:02 +0000 | [diff] [blame] | 182 | return BinaryOperator::create(Instruction::Add, LHS, RHS, |
| 183 | I->getName()+".neg", |
| 184 | cast<Instruction>(RHS)->getNext()); |
Chris Lattner | 7bc532d | 2002-05-16 04:37:07 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | // Insert a 'neg' instruction that subtracts the value from zero to get the |
| 188 | // negation. |
| 189 | // |
| 190 | Instruction *Neg = |
| 191 | BinaryOperator::create(Instruction::Sub, |
| 192 | Constant::getNullValue(V->getType()), V, |
Chris Lattner | 28a8d24 | 2002-09-10 17:04:02 +0000 | [diff] [blame] | 193 | V->getName()+".neg", BI); |
| 194 | --BI; |
Chris Lattner | 7bc532d | 2002-05-16 04:37:07 +0000 | [diff] [blame] | 195 | return Neg; |
| 196 | } |
| 197 | |
| 198 | |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 199 | bool Reassociate::ReassociateBB(BasicBlock *BB) { |
| 200 | bool Changed = false; |
| 201 | for (BasicBlock::iterator BI = BB->begin(); BI != BB->end(); ++BI) { |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 202 | |
| 203 | // If this instruction is a commutative binary operator, and the ranks of |
| 204 | // the two operands are sorted incorrectly, fix it now. |
| 205 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 206 | if (BinaryOperator *I = isCommutativeOperator(BI)) { |
Chris Lattner | 7bc532d | 2002-05-16 04:37:07 +0000 | [diff] [blame] | 207 | if (!I->use_empty()) { |
| 208 | // Make sure that we don't have a tree-shaped computation. If we do, |
| 209 | // linearize it. Convert (A+B)+(C+D) into ((A+B)+C)+D |
| 210 | // |
| 211 | Instruction *LHSI = dyn_cast<Instruction>(I->getOperand(0)); |
| 212 | Instruction *RHSI = dyn_cast<Instruction>(I->getOperand(1)); |
| 213 | if (LHSI && (int)LHSI->getOpcode() == I->getOpcode() && |
| 214 | RHSI && (int)RHSI->getOpcode() == I->getOpcode() && |
| 215 | RHSI->use_size() == 1) { |
| 216 | // Insert a new temporary instruction... (A+B)+C |
| 217 | BinaryOperator *Tmp = BinaryOperator::create(I->getOpcode(), LHSI, |
| 218 | RHSI->getOperand(0), |
Chris Lattner | 28a8d24 | 2002-09-10 17:04:02 +0000 | [diff] [blame] | 219 | RHSI->getName()+".ra", |
| 220 | BI); |
| 221 | BI = Tmp; |
Chris Lattner | 7bc532d | 2002-05-16 04:37:07 +0000 | [diff] [blame] | 222 | I->setOperand(0, Tmp); |
| 223 | I->setOperand(1, RHSI->getOperand(1)); |
| 224 | |
| 225 | // Process the temporary instruction for reassociation now. |
| 226 | I = Tmp; |
| 227 | ++NumLinear; |
| 228 | Changed = true; |
Chris Lattner | 71cbd42 | 2002-05-22 17:17:27 +0000 | [diff] [blame] | 229 | DEBUG(std::cerr << "Linearized: " << I << " Result BB: " << BB); |
Chris Lattner | 7bc532d | 2002-05-16 04:37:07 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | // Make sure that this expression is correctly reassociated with respect |
| 233 | // to it's used values... |
| 234 | // |
| 235 | Changed |= ReassociateExpr(I); |
| 236 | } |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 237 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 238 | } else if (BI->getOpcode() == Instruction::Sub && |
| 239 | BI->getOperand(0) != Constant::getNullValue(BI->getType())) { |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 240 | // Convert a subtract into an add and a neg instruction... so that sub |
| 241 | // instructions can be commuted with other add instructions... |
| 242 | // |
| 243 | Instruction *New = BinaryOperator::create(Instruction::Add, |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 244 | BI->getOperand(0), |
| 245 | BI->getOperand(1), |
| 246 | BI->getName()); |
| 247 | Value *NegatedValue = BI->getOperand(1); |
Chris Lattner | 7bc532d | 2002-05-16 04:37:07 +0000 | [diff] [blame] | 248 | |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 249 | // Everyone now refers to the add instruction... |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 250 | BI->replaceAllUsesWith(New); |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 251 | |
Chris Lattner | 7bc532d | 2002-05-16 04:37:07 +0000 | [diff] [blame] | 252 | // Put the new add in the place of the subtract... deleting the subtract |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 253 | BI = BB->getInstList().erase(BI); |
| 254 | BI = ++BB->getInstList().insert(BI, New); |
Chris Lattner | 7bc532d | 2002-05-16 04:37:07 +0000 | [diff] [blame] | 255 | |
| 256 | // Calculate the negative value of Operand 1 of the sub instruction... |
| 257 | // and set it as the RHS of the add instruction we just made... |
| 258 | New->setOperand(1, NegateValue(NegatedValue, BB, BI)); |
| 259 | --BI; |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 260 | Changed = true; |
Chris Lattner | 71cbd42 | 2002-05-22 17:17:27 +0000 | [diff] [blame] | 261 | DEBUG(std::cerr << "Negated: " << New << " Result BB: " << BB); |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | |
| 265 | return Changed; |
| 266 | } |
| 267 | |
| 268 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 269 | bool Reassociate::runOnFunction(Function &F) { |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 270 | // Recalculate the rank map for F |
| 271 | BuildRankMap(F); |
| 272 | |
| 273 | bool Changed = false; |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 274 | for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) |
| 275 | Changed |= ReassociateBB(FI); |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 276 | |
| 277 | // We are done with the rank map... |
| 278 | RankMap.clear(); |
| 279 | return Changed; |
| 280 | } |