Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 1 | //===- Reassociate.cpp - Reassociate binary expressions -------------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 9 | // |
| 10 | // This pass reassociates commutative expressions in an order that is designed |
Chris Lattner | 3666378 | 2003-05-02 19:26:34 +0000 | [diff] [blame] | 11 | // to promote better constant propagation, GCSE, LICM, PRE... |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 12 | // |
| 13 | // For example: 4 + (x + 5) -> x + (4 + 5) |
| 14 | // |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 15 | // In the implementation of this algorithm, constants are assigned rank = 0, |
| 16 | // function arguments are rank = 1, and other values are assigned ranks |
| 17 | // corresponding to the reverse post order traversal of current function |
| 18 | // (starting at 2), which effectively gives values in deep loops higher rank |
| 19 | // than values not in loops. |
| 20 | // |
| 21 | //===----------------------------------------------------------------------===// |
| 22 | |
Chris Lattner | f43e974 | 2005-05-07 04:08:02 +0000 | [diff] [blame] | 23 | #define DEBUG_TYPE "reassociate" |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 24 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | cea5799 | 2005-05-07 04:24:13 +0000 | [diff] [blame] | 25 | #include "llvm/Constants.h" |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 26 | #include "llvm/Function.h" |
Misha Brukman | 2b3387a | 2004-07-29 17:05:13 +0000 | [diff] [blame] | 27 | #include "llvm/Instructions.h" |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 28 | #include "llvm/Pass.h" |
Chris Lattner | cea5799 | 2005-05-07 04:24:13 +0000 | [diff] [blame] | 29 | #include "llvm/Type.h" |
Chris Lattner | 9187f39 | 2005-05-08 20:09:57 +0000 | [diff] [blame] | 30 | #include "llvm/Assembly/Writer.h" |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 31 | #include "llvm/Support/CFG.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 32 | #include "llvm/Support/Debug.h" |
| 33 | #include "llvm/ADT/PostOrderIterator.h" |
| 34 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | 1e50650 | 2005-05-07 21:59:39 +0000 | [diff] [blame] | 35 | #include <algorithm> |
Chris Lattner | 49525f8 | 2004-01-09 06:02:20 +0000 | [diff] [blame] | 36 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 37 | |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 38 | namespace { |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 39 | Statistic<> NumLinear ("reassociate","Number of insts linearized"); |
| 40 | Statistic<> NumChanged("reassociate","Number of insts reassociated"); |
| 41 | Statistic<> NumSwapped("reassociate","Number of insts with operands swapped"); |
Chris Lattner | 5847e5e | 2005-05-08 18:59:37 +0000 | [diff] [blame] | 42 | Statistic<> NumAnnihil("reassociate","Number of expr tree annihilated"); |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 43 | |
Chris Lattner | 1e50650 | 2005-05-07 21:59:39 +0000 | [diff] [blame] | 44 | struct ValueEntry { |
| 45 | unsigned Rank; |
| 46 | Value *Op; |
| 47 | ValueEntry(unsigned R, Value *O) : Rank(R), Op(O) {} |
| 48 | }; |
| 49 | inline bool operator<(const ValueEntry &LHS, const ValueEntry &RHS) { |
| 50 | return LHS.Rank > RHS.Rank; // Sort so that highest rank goes to start. |
| 51 | } |
| 52 | |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 53 | class Reassociate : public FunctionPass { |
Chris Lattner | 10073a9 | 2002-07-25 06:17:51 +0000 | [diff] [blame] | 54 | std::map<BasicBlock*, unsigned> RankMap; |
Chris Lattner | 8ac196d | 2003-08-13 16:16:26 +0000 | [diff] [blame] | 55 | std::map<Value*, unsigned> ValueRankMap; |
Chris Lattner | 1e50650 | 2005-05-07 21:59:39 +0000 | [diff] [blame] | 56 | bool MadeChange; |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 57 | public: |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 58 | bool runOnFunction(Function &F); |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 59 | |
| 60 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | 820d971 | 2002-10-21 20:00:28 +0000 | [diff] [blame] | 61 | AU.setPreservesCFG(); |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 62 | } |
| 63 | private: |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 64 | void BuildRankMap(Function &F); |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 65 | unsigned getRank(Value *V); |
Chris Lattner | 1e50650 | 2005-05-07 21:59:39 +0000 | [diff] [blame] | 66 | void RewriteExprTree(BinaryOperator *I, unsigned Idx, |
| 67 | std::vector<ValueEntry> &Ops); |
Chris Lattner | e1850b8 | 2005-05-08 00:19:31 +0000 | [diff] [blame] | 68 | void OptimizeExpression(unsigned Opcode, std::vector<ValueEntry> &Ops); |
Chris Lattner | 1e50650 | 2005-05-07 21:59:39 +0000 | [diff] [blame] | 69 | void LinearizeExprTree(BinaryOperator *I, std::vector<ValueEntry> &Ops); |
| 70 | void LinearizeExpr(BinaryOperator *I); |
| 71 | void ReassociateBB(BasicBlock *BB); |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 72 | }; |
Chris Lattner | b28b680 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 73 | |
Chris Lattner | c8b7092 | 2002-07-26 21:12:46 +0000 | [diff] [blame] | 74 | RegisterOpt<Reassociate> X("reassociate", "Reassociate expressions"); |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 77 | // Public interface to the Reassociate pass |
Chris Lattner | 49525f8 | 2004-01-09 06:02:20 +0000 | [diff] [blame] | 78 | FunctionPass *llvm::createReassociatePass() { return new Reassociate(); } |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 79 | |
Chris Lattner | 9f284e0 | 2005-05-08 20:57:04 +0000 | [diff] [blame^] | 80 | |
| 81 | static bool isUnmovableInstruction(Instruction *I) { |
| 82 | if (I->getOpcode() == Instruction::PHI || |
| 83 | I->getOpcode() == Instruction::Alloca || |
| 84 | I->getOpcode() == Instruction::Load || |
| 85 | I->getOpcode() == Instruction::Malloc || |
| 86 | I->getOpcode() == Instruction::Invoke || |
| 87 | I->getOpcode() == Instruction::Call || |
| 88 | I->getOpcode() == Instruction::Div || |
| 89 | I->getOpcode() == Instruction::Rem) |
| 90 | return true; |
| 91 | return false; |
| 92 | } |
| 93 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 94 | void Reassociate::BuildRankMap(Function &F) { |
Chris Lattner | 58c7eb6 | 2003-08-12 20:14:27 +0000 | [diff] [blame] | 95 | unsigned i = 2; |
Chris Lattner | 8ac196d | 2003-08-13 16:16:26 +0000 | [diff] [blame] | 96 | |
| 97 | // Assign distinct ranks to function arguments |
Chris Lattner | 531f9e9 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 98 | for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) |
Chris Lattner | 8ac196d | 2003-08-13 16:16:26 +0000 | [diff] [blame] | 99 | ValueRankMap[I] = ++i; |
| 100 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 101 | ReversePostOrderTraversal<Function*> RPOT(&F); |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 102 | for (ReversePostOrderTraversal<Function*>::rpo_iterator I = RPOT.begin(), |
Chris Lattner | 9f284e0 | 2005-05-08 20:57:04 +0000 | [diff] [blame^] | 103 | E = RPOT.end(); I != E; ++I) { |
| 104 | BasicBlock *BB = *I; |
| 105 | unsigned BBRank = RankMap[BB] = ++i << 16; |
| 106 | |
| 107 | // Walk the basic block, adding precomputed ranks for any instructions that |
| 108 | // we cannot move. This ensures that the ranks for these instructions are |
| 109 | // all different in the block. |
| 110 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
| 111 | if (isUnmovableInstruction(I)) |
| 112 | ValueRankMap[I] = ++BBRank; |
| 113 | } |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | unsigned Reassociate::getRank(Value *V) { |
Chris Lattner | 8ac196d | 2003-08-13 16:16:26 +0000 | [diff] [blame] | 117 | if (isa<Argument>(V)) return ValueRankMap[V]; // Function argument... |
| 118 | |
Chris Lattner | f43e974 | 2005-05-07 04:08:02 +0000 | [diff] [blame] | 119 | Instruction *I = dyn_cast<Instruction>(V); |
| 120 | if (I == 0) return 0; // Otherwise it's a global or constant, rank 0. |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 121 | |
Chris Lattner | f43e974 | 2005-05-07 04:08:02 +0000 | [diff] [blame] | 122 | unsigned &CachedRank = ValueRankMap[I]; |
| 123 | if (CachedRank) return CachedRank; // Rank already known? |
| 124 | |
| 125 | // If this is an expression, return the 1+MAX(rank(LHS), rank(RHS)) so that |
| 126 | // we can reassociate expressions for code motion! Since we do not recurse |
| 127 | // for PHI nodes, we cannot have infinite recursion here, because there |
| 128 | // cannot be loops in the value graph that do not go through PHI nodes. |
Chris Lattner | f43e974 | 2005-05-07 04:08:02 +0000 | [diff] [blame] | 129 | unsigned Rank = 0, MaxRank = RankMap[I->getParent()]; |
| 130 | for (unsigned i = 0, e = I->getNumOperands(); |
| 131 | i != e && Rank != MaxRank; ++i) |
| 132 | Rank = std::max(Rank, getRank(I->getOperand(i))); |
| 133 | |
Chris Lattner | 6e2086d | 2005-05-08 00:08:33 +0000 | [diff] [blame] | 134 | // If this is a not or neg instruction, do not count it for rank. This |
| 135 | // assures us that X and ~X will have the same rank. |
| 136 | if (!I->getType()->isIntegral() || |
| 137 | (!BinaryOperator::isNot(I) && !BinaryOperator::isNeg(I))) |
| 138 | ++Rank; |
| 139 | |
Chris Lattner | 9f284e0 | 2005-05-08 20:57:04 +0000 | [diff] [blame^] | 140 | //DEBUG(std::cerr << "Calculated Rank[" << V->getName() << "] = " |
| 141 | //<< Rank << "\n"); |
Chris Lattner | f43e974 | 2005-05-07 04:08:02 +0000 | [diff] [blame] | 142 | |
Chris Lattner | 6e2086d | 2005-05-08 00:08:33 +0000 | [diff] [blame] | 143 | return CachedRank = Rank; |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Chris Lattner | 1e50650 | 2005-05-07 21:59:39 +0000 | [diff] [blame] | 146 | /// isReassociableOp - Return true if V is an instruction of the specified |
| 147 | /// opcode and if it only has one use. |
| 148 | static BinaryOperator *isReassociableOp(Value *V, unsigned Opcode) { |
| 149 | if (V->hasOneUse() && isa<Instruction>(V) && |
| 150 | cast<Instruction>(V)->getOpcode() == Opcode) |
| 151 | return cast<BinaryOperator>(V); |
| 152 | return 0; |
| 153 | } |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 154 | |
Chris Lattner | 1e50650 | 2005-05-07 21:59:39 +0000 | [diff] [blame] | 155 | // Given an expression of the form '(A+B)+(D+C)', turn it into '(((A+B)+C)+D)'. |
| 156 | // Note that if D is also part of the expression tree that we recurse to |
| 157 | // linearize it as well. Besides that case, this does not recurse into A,B, or |
| 158 | // C. |
| 159 | void Reassociate::LinearizeExpr(BinaryOperator *I) { |
| 160 | BinaryOperator *LHS = cast<BinaryOperator>(I->getOperand(0)); |
| 161 | BinaryOperator *RHS = cast<BinaryOperator>(I->getOperand(1)); |
| 162 | assert(isReassociableOp(LHS, I->getOpcode()) && |
| 163 | isReassociableOp(RHS, I->getOpcode()) && |
| 164 | "Not an expression that needs linearization?"); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 165 | |
Chris Lattner | 1e50650 | 2005-05-07 21:59:39 +0000 | [diff] [blame] | 166 | DEBUG(std::cerr << "Linear" << *LHS << *RHS << *I); |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 167 | |
Chris Lattner | 1e50650 | 2005-05-07 21:59:39 +0000 | [diff] [blame] | 168 | // Move the RHS instruction to live immediately before I, avoiding breaking |
| 169 | // dominator properties. |
| 170 | I->getParent()->getInstList().splice(I, RHS->getParent()->getInstList(), RHS); |
Chris Lattner | 8fdf75c | 2002-10-31 17:12:59 +0000 | [diff] [blame] | 171 | |
Chris Lattner | 1e50650 | 2005-05-07 21:59:39 +0000 | [diff] [blame] | 172 | // Move operands around to do the linearization. |
| 173 | I->setOperand(1, RHS->getOperand(0)); |
| 174 | RHS->setOperand(0, LHS); |
| 175 | I->setOperand(0, RHS); |
| 176 | |
| 177 | ++NumLinear; |
| 178 | MadeChange = true; |
| 179 | DEBUG(std::cerr << "Linearized: " << *I); |
| 180 | |
| 181 | // If D is part of this expression tree, tail recurse. |
| 182 | if (isReassociableOp(I->getOperand(1), I->getOpcode())) |
| 183 | LinearizeExpr(I); |
| 184 | } |
| 185 | |
| 186 | |
| 187 | /// LinearizeExprTree - Given an associative binary expression tree, traverse |
| 188 | /// all of the uses putting it into canonical form. This forces a left-linear |
| 189 | /// form of the the expression (((a+b)+c)+d), and collects information about the |
| 190 | /// rank of the non-tree operands. |
| 191 | /// |
| 192 | /// This returns the rank of the RHS operand, which is known to be the highest |
| 193 | /// rank value in the expression tree. |
| 194 | /// |
| 195 | void Reassociate::LinearizeExprTree(BinaryOperator *I, |
| 196 | std::vector<ValueEntry> &Ops) { |
| 197 | Value *LHS = I->getOperand(0), *RHS = I->getOperand(1); |
| 198 | unsigned Opcode = I->getOpcode(); |
| 199 | |
| 200 | // First step, linearize the expression if it is in ((A+B)+(C+D)) form. |
| 201 | BinaryOperator *LHSBO = isReassociableOp(LHS, Opcode); |
| 202 | BinaryOperator *RHSBO = isReassociableOp(RHS, Opcode); |
| 203 | |
| 204 | if (!LHSBO) { |
| 205 | if (!RHSBO) { |
| 206 | // Neither the LHS or RHS as part of the tree, thus this is a leaf. As |
| 207 | // such, just remember these operands and their rank. |
| 208 | Ops.push_back(ValueEntry(getRank(LHS), LHS)); |
| 209 | Ops.push_back(ValueEntry(getRank(RHS), RHS)); |
| 210 | return; |
| 211 | } else { |
| 212 | // Turn X+(Y+Z) -> (Y+Z)+X |
| 213 | std::swap(LHSBO, RHSBO); |
| 214 | std::swap(LHS, RHS); |
| 215 | bool Success = !I->swapOperands(); |
| 216 | assert(Success && "swapOperands failed"); |
| 217 | MadeChange = true; |
| 218 | } |
| 219 | } else if (RHSBO) { |
| 220 | // Turn (A+B)+(C+D) -> (((A+B)+C)+D). This guarantees the the RHS is not |
| 221 | // part of the expression tree. |
| 222 | LinearizeExpr(I); |
| 223 | LHS = LHSBO = cast<BinaryOperator>(I->getOperand(0)); |
| 224 | RHS = I->getOperand(1); |
| 225 | RHSBO = 0; |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 226 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 227 | |
Chris Lattner | 1e50650 | 2005-05-07 21:59:39 +0000 | [diff] [blame] | 228 | // Okay, now we know that the LHS is a nested expression and that the RHS is |
| 229 | // not. Perform reassociation. |
| 230 | assert(!isReassociableOp(RHS, Opcode) && "LinearizeExpr failed!"); |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 231 | |
Chris Lattner | 1e50650 | 2005-05-07 21:59:39 +0000 | [diff] [blame] | 232 | // Move LHS right before I to make sure that the tree expression dominates all |
| 233 | // values. |
| 234 | I->getParent()->getInstList().splice(I, |
| 235 | LHSBO->getParent()->getInstList(), LHSBO); |
Chris Lattner | 98b3ecd | 2003-08-12 21:45:24 +0000 | [diff] [blame] | 236 | |
Chris Lattner | 1e50650 | 2005-05-07 21:59:39 +0000 | [diff] [blame] | 237 | // Linearize the expression tree on the LHS. |
| 238 | LinearizeExprTree(LHSBO, Ops); |
Chris Lattner | 8fdf75c | 2002-10-31 17:12:59 +0000 | [diff] [blame] | 239 | |
Chris Lattner | 1e50650 | 2005-05-07 21:59:39 +0000 | [diff] [blame] | 240 | // Remember the RHS operand and its rank. |
| 241 | Ops.push_back(ValueEntry(getRank(RHS), RHS)); |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Chris Lattner | 1e50650 | 2005-05-07 21:59:39 +0000 | [diff] [blame] | 244 | // RewriteExprTree - Now that the operands for this expression tree are |
| 245 | // linearized and optimized, emit them in-order. This function is written to be |
| 246 | // tail recursive. |
| 247 | void Reassociate::RewriteExprTree(BinaryOperator *I, unsigned i, |
| 248 | std::vector<ValueEntry> &Ops) { |
| 249 | if (i+2 == Ops.size()) { |
| 250 | if (I->getOperand(0) != Ops[i].Op || |
| 251 | I->getOperand(1) != Ops[i+1].Op) { |
| 252 | DEBUG(std::cerr << "RA: " << *I); |
| 253 | I->setOperand(0, Ops[i].Op); |
| 254 | I->setOperand(1, Ops[i+1].Op); |
| 255 | DEBUG(std::cerr << "TO: " << *I); |
| 256 | MadeChange = true; |
| 257 | ++NumChanged; |
| 258 | } |
| 259 | return; |
| 260 | } |
| 261 | assert(i+2 < Ops.size() && "Ops index out of range!"); |
| 262 | |
| 263 | if (I->getOperand(1) != Ops[i].Op) { |
| 264 | DEBUG(std::cerr << "RA: " << *I); |
| 265 | I->setOperand(1, Ops[i].Op); |
| 266 | DEBUG(std::cerr << "TO: " << *I); |
| 267 | MadeChange = true; |
| 268 | ++NumChanged; |
| 269 | } |
| 270 | RewriteExprTree(cast<BinaryOperator>(I->getOperand(0)), i+1, Ops); |
| 271 | } |
| 272 | |
| 273 | |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 274 | |
Chris Lattner | 7bc532d | 2002-05-16 04:37:07 +0000 | [diff] [blame] | 275 | // NegateValue - Insert instructions before the instruction pointed to by BI, |
| 276 | // that computes the negative version of the value specified. The negative |
| 277 | // version of the value is returned, and BI is left pointing at the instruction |
| 278 | // that should be processed next by the reassociation pass. |
| 279 | // |
Chris Lattner | f43e974 | 2005-05-07 04:08:02 +0000 | [diff] [blame] | 280 | static Value *NegateValue(Value *V, Instruction *BI) { |
Chris Lattner | 7bc532d | 2002-05-16 04:37:07 +0000 | [diff] [blame] | 281 | // We are trying to expose opportunity for reassociation. One of the things |
| 282 | // that we want to do to achieve this is to push a negation as deep into an |
| 283 | // expression chain as possible, to expose the add instructions. In practice, |
| 284 | // this means that we turn this: |
| 285 | // X = -(A+12+C+D) into X = -A + -12 + -C + -D = -12 + -A + -C + -D |
| 286 | // so that later, a: Y = 12+X could get reassociated with the -12 to eliminate |
| 287 | // the constants. We assume that instcombine will clean up the mess later if |
Misha Brukman | 7eb05a1 | 2003-08-18 14:43:39 +0000 | [diff] [blame] | 288 | // we introduce tons of unnecessary negation instructions... |
Chris Lattner | 7bc532d | 2002-05-16 04:37:07 +0000 | [diff] [blame] | 289 | // |
| 290 | if (Instruction *I = dyn_cast<Instruction>(V)) |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 291 | if (I->getOpcode() == Instruction::Add && I->hasOneUse()) { |
Chris Lattner | 8fdf75c | 2002-10-31 17:12:59 +0000 | [diff] [blame] | 292 | Value *RHS = NegateValue(I->getOperand(1), BI); |
| 293 | Value *LHS = NegateValue(I->getOperand(0), BI); |
Chris Lattner | 7bc532d | 2002-05-16 04:37:07 +0000 | [diff] [blame] | 294 | |
| 295 | // We must actually insert a new add instruction here, because the neg |
| 296 | // instructions do not dominate the old add instruction in general. By |
| 297 | // adding it now, we are assured that the neg instructions we just |
| 298 | // inserted dominate the instruction we are about to insert after them. |
| 299 | // |
Chris Lattner | 28a8d24 | 2002-09-10 17:04:02 +0000 | [diff] [blame] | 300 | return BinaryOperator::create(Instruction::Add, LHS, RHS, |
Chris Lattner | f43e974 | 2005-05-07 04:08:02 +0000 | [diff] [blame] | 301 | I->getName()+".neg", BI); |
Chris Lattner | 7bc532d | 2002-05-16 04:37:07 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | // Insert a 'neg' instruction that subtracts the value from zero to get the |
| 305 | // negation. |
| 306 | // |
Chris Lattner | f43e974 | 2005-05-07 04:08:02 +0000 | [diff] [blame] | 307 | return BinaryOperator::createNeg(V, V->getName() + ".neg", BI); |
| 308 | } |
| 309 | |
Chris Lattner | f43e974 | 2005-05-07 04:08:02 +0000 | [diff] [blame] | 310 | /// BreakUpSubtract - If we have (X-Y), and if either X is an add, or if this is |
| 311 | /// only used by an add, transform this into (X+(0-Y)) to promote better |
| 312 | /// reassociation. |
| 313 | static Instruction *BreakUpSubtract(Instruction *Sub) { |
| 314 | // Reject cases where it is pointless to do this. |
| 315 | if (Sub->getType()->isFloatingPoint()) |
| 316 | return 0; // Floating point adds are not associative. |
| 317 | |
| 318 | // Don't bother to break this up unless either the LHS is an associable add or |
| 319 | // if this is only used by one. |
| 320 | if (!isReassociableOp(Sub->getOperand(0), Instruction::Add) && |
| 321 | !isReassociableOp(Sub->getOperand(1), Instruction::Add) && |
| 322 | !(Sub->hasOneUse() &&isReassociableOp(Sub->use_back(), Instruction::Add))) |
| 323 | return 0; |
| 324 | |
| 325 | // Convert a subtract into an add and a neg instruction... so that sub |
| 326 | // instructions can be commuted with other add instructions... |
| 327 | // |
| 328 | // Calculate the negative value of Operand 1 of the sub instruction... |
| 329 | // and set it as the RHS of the add instruction we just made... |
| 330 | // |
| 331 | std::string Name = Sub->getName(); |
| 332 | Sub->setName(""); |
| 333 | Value *NegVal = NegateValue(Sub->getOperand(1), Sub); |
| 334 | Instruction *New = |
| 335 | BinaryOperator::createAdd(Sub->getOperand(0), NegVal, Name, Sub); |
| 336 | |
| 337 | // Everyone now refers to the add instruction. |
| 338 | Sub->replaceAllUsesWith(New); |
| 339 | Sub->eraseFromParent(); |
| 340 | |
| 341 | DEBUG(std::cerr << "Negated: " << *New); |
| 342 | return New; |
Chris Lattner | 7bc532d | 2002-05-16 04:37:07 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Chris Lattner | cea5799 | 2005-05-07 04:24:13 +0000 | [diff] [blame] | 345 | /// ConvertShiftToMul - If this is a shift of a reassociable multiply or is used |
| 346 | /// by one, change this into a multiply by a constant to assist with further |
| 347 | /// reassociation. |
| 348 | static Instruction *ConvertShiftToMul(Instruction *Shl) { |
| 349 | if (!isReassociableOp(Shl->getOperand(0), Instruction::Mul) && |
| 350 | !(Shl->hasOneUse() && isReassociableOp(Shl->use_back(),Instruction::Mul))) |
| 351 | return 0; |
| 352 | |
| 353 | Constant *MulCst = ConstantInt::get(Shl->getType(), 1); |
| 354 | MulCst = ConstantExpr::getShl(MulCst, cast<Constant>(Shl->getOperand(1))); |
| 355 | |
| 356 | std::string Name = Shl->getName(); Shl->setName(""); |
| 357 | Instruction *Mul = BinaryOperator::createMul(Shl->getOperand(0), MulCst, |
| 358 | Name, Shl); |
| 359 | Shl->replaceAllUsesWith(Mul); |
| 360 | Shl->eraseFromParent(); |
| 361 | return Mul; |
| 362 | } |
| 363 | |
Chris Lattner | 5847e5e | 2005-05-08 18:59:37 +0000 | [diff] [blame] | 364 | // Scan backwards and forwards among values with the same rank as element i to |
| 365 | // see if X exists. If X does not exist, return i. |
| 366 | static unsigned FindInOperandList(std::vector<ValueEntry> &Ops, unsigned i, |
| 367 | Value *X) { |
| 368 | unsigned XRank = Ops[i].Rank; |
| 369 | unsigned e = Ops.size(); |
| 370 | for (unsigned j = i+1; j != e && Ops[j].Rank == XRank; ++j) |
| 371 | if (Ops[j].Op == X) |
| 372 | return j; |
| 373 | // Scan backwards |
| 374 | for (unsigned j = i-1; j != ~0U && Ops[j].Rank == XRank; --j) |
| 375 | if (Ops[j].Op == X) |
| 376 | return j; |
| 377 | return i; |
| 378 | } |
| 379 | |
Chris Lattner | e1850b8 | 2005-05-08 00:19:31 +0000 | [diff] [blame] | 380 | void Reassociate::OptimizeExpression(unsigned Opcode, |
| 381 | std::vector<ValueEntry> &Ops) { |
| 382 | // Now that we have the linearized expression tree, try to optimize it. |
| 383 | // Start by folding any constants that we found. |
Chris Lattner | 5847e5e | 2005-05-08 18:59:37 +0000 | [diff] [blame] | 384 | bool IterateOptimization = false; |
Chris Lattner | e1850b8 | 2005-05-08 00:19:31 +0000 | [diff] [blame] | 385 | if (Ops.size() == 1) return; |
| 386 | |
| 387 | if (Constant *V1 = dyn_cast<Constant>(Ops[Ops.size()-2].Op)) |
| 388 | if (Constant *V2 = dyn_cast<Constant>(Ops.back().Op)) { |
| 389 | Ops.pop_back(); |
| 390 | Ops.back().Op = ConstantExpr::get(Opcode, V1, V2); |
Chris Lattner | 08582be | 2005-05-08 19:48:43 +0000 | [diff] [blame] | 391 | OptimizeExpression(Opcode, Ops); |
| 392 | return; |
Chris Lattner | e1850b8 | 2005-05-08 00:19:31 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | // Check for destructive annihilation due to a constant being used. |
| 396 | if (ConstantIntegral *CstVal = dyn_cast<ConstantIntegral>(Ops.back().Op)) |
| 397 | switch (Opcode) { |
| 398 | default: break; |
| 399 | case Instruction::And: |
| 400 | if (CstVal->isNullValue()) { // ... & 0 -> 0 |
| 401 | Ops[0].Op = CstVal; |
| 402 | Ops.erase(Ops.begin()+1, Ops.end()); |
Chris Lattner | 5847e5e | 2005-05-08 18:59:37 +0000 | [diff] [blame] | 403 | ++NumAnnihil; |
| 404 | return; |
Chris Lattner | e1850b8 | 2005-05-08 00:19:31 +0000 | [diff] [blame] | 405 | } else if (CstVal->isAllOnesValue()) { // ... & -1 -> ... |
| 406 | Ops.pop_back(); |
| 407 | } |
| 408 | break; |
| 409 | case Instruction::Mul: |
| 410 | if (CstVal->isNullValue()) { // ... * 0 -> 0 |
| 411 | Ops[0].Op = CstVal; |
| 412 | Ops.erase(Ops.begin()+1, Ops.end()); |
Chris Lattner | 5847e5e | 2005-05-08 18:59:37 +0000 | [diff] [blame] | 413 | ++NumAnnihil; |
| 414 | return; |
Chris Lattner | e1850b8 | 2005-05-08 00:19:31 +0000 | [diff] [blame] | 415 | } else if (cast<ConstantInt>(CstVal)->getRawValue() == 1) { |
| 416 | Ops.pop_back(); // ... * 1 -> ... |
| 417 | } |
| 418 | break; |
| 419 | case Instruction::Or: |
| 420 | if (CstVal->isAllOnesValue()) { // ... | -1 -> -1 |
| 421 | Ops[0].Op = CstVal; |
| 422 | Ops.erase(Ops.begin()+1, Ops.end()); |
Chris Lattner | 5847e5e | 2005-05-08 18:59:37 +0000 | [diff] [blame] | 423 | ++NumAnnihil; |
| 424 | return; |
Chris Lattner | e1850b8 | 2005-05-08 00:19:31 +0000 | [diff] [blame] | 425 | } |
| 426 | // FALLTHROUGH! |
| 427 | case Instruction::Add: |
| 428 | case Instruction::Xor: |
| 429 | if (CstVal->isNullValue()) // ... [|^+] 0 -> ... |
| 430 | Ops.pop_back(); |
| 431 | break; |
| 432 | } |
| 433 | |
| 434 | // Handle destructive annihilation do to identities between elements in the |
| 435 | // argument list here. |
Chris Lattner | 5847e5e | 2005-05-08 18:59:37 +0000 | [diff] [blame] | 436 | switch (Opcode) { |
| 437 | default: break; |
| 438 | case Instruction::And: |
| 439 | case Instruction::Or: |
| 440 | case Instruction::Xor: |
| 441 | // Scan the operand lists looking for X and ~X pairs, along with X,X pairs. |
| 442 | // If we find any, we can simplify the expression. X&~X == 0, X|~X == -1. |
| 443 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) { |
| 444 | // First, check for X and ~X in the operand list. |
| 445 | if (BinaryOperator::isNot(Ops[i].Op)) { // Cannot occur for ^. |
| 446 | Value *X = BinaryOperator::getNotArgument(Ops[i].Op); |
| 447 | unsigned FoundX = FindInOperandList(Ops, i, X); |
| 448 | if (FoundX != i) { |
| 449 | if (Opcode == Instruction::And) { // ...&X&~X = 0 |
| 450 | Ops[0].Op = Constant::getNullValue(X->getType()); |
| 451 | Ops.erase(Ops.begin()+1, Ops.end()); |
| 452 | ++NumAnnihil; |
| 453 | return; |
| 454 | } else if (Opcode == Instruction::Or) { // ...|X|~X = -1 |
| 455 | Ops[0].Op = ConstantIntegral::getAllOnesValue(X->getType()); |
| 456 | Ops.erase(Ops.begin()+1, Ops.end()); |
| 457 | ++NumAnnihil; |
| 458 | return; |
| 459 | } |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | // Next, check for duplicate pairs of values, which we assume are next to |
| 464 | // each other, due to our sorting criteria. |
| 465 | if (i+1 != Ops.size() && Ops[i+1].Op == Ops[i].Op) { |
| 466 | if (Opcode == Instruction::And || Opcode == Instruction::Or) { |
| 467 | // Drop duplicate values. |
| 468 | Ops.erase(Ops.begin()+i); |
| 469 | --i; --e; |
| 470 | IterateOptimization = true; |
| 471 | ++NumAnnihil; |
| 472 | } else { |
| 473 | assert(Opcode == Instruction::Xor); |
| 474 | // ... X^X -> ... |
| 475 | Ops.erase(Ops.begin()+i, Ops.begin()+i+2); |
| 476 | i -= 2; e -= 2; |
| 477 | IterateOptimization = true; |
| 478 | ++NumAnnihil; |
| 479 | } |
| 480 | } |
| 481 | } |
| 482 | break; |
| 483 | |
| 484 | case Instruction::Add: |
| 485 | // Scan the operand lists looking for X and -X pairs. If we find any, we |
| 486 | // can simplify the expression. X+-X == 0 |
| 487 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) { |
| 488 | // Check for X and -X in the operand list. |
| 489 | if (BinaryOperator::isNeg(Ops[i].Op)) { |
| 490 | Value *X = BinaryOperator::getNegArgument(Ops[i].Op); |
| 491 | unsigned FoundX = FindInOperandList(Ops, i, X); |
| 492 | if (FoundX != i) { |
| 493 | // Remove X and -X from the operand list. |
| 494 | if (Ops.size() == 2) { |
| 495 | Ops[0].Op = Constant::getNullValue(X->getType()); |
| 496 | Ops.erase(Ops.begin()+1); |
| 497 | ++NumAnnihil; |
| 498 | return; |
| 499 | } else { |
| 500 | Ops.erase(Ops.begin()+i); |
| 501 | if (i < FoundX) --FoundX; |
| 502 | Ops.erase(Ops.begin()+FoundX); |
| 503 | IterateOptimization = true; |
| 504 | ++NumAnnihil; |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | break; |
| 510 | //case Instruction::Mul: |
| 511 | } |
| 512 | |
Chris Lattner | 08582be | 2005-05-08 19:48:43 +0000 | [diff] [blame] | 513 | if (IterateOptimization) |
| 514 | OptimizeExpression(Opcode, Ops); |
Chris Lattner | e1850b8 | 2005-05-08 00:19:31 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Chris Lattner | 9187f39 | 2005-05-08 20:09:57 +0000 | [diff] [blame] | 517 | /// PrintOps - Print out the expression identified in the Ops list. |
| 518 | /// |
| 519 | static void PrintOps(unsigned Opcode, const std::vector<ValueEntry> &Ops, |
| 520 | BasicBlock *BB) { |
| 521 | Module *M = BB->getParent()->getParent(); |
| 522 | std::cerr << Instruction::getOpcodeName(Opcode) << " " |
| 523 | << *Ops[0].Op->getType(); |
| 524 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 525 | WriteAsOperand(std::cerr << " ", Ops[i].Op, false, true, M) |
| 526 | << "," << Ops[i].Rank; |
| 527 | } |
Chris Lattner | 7bc532d | 2002-05-16 04:37:07 +0000 | [diff] [blame] | 528 | |
Chris Lattner | f43e974 | 2005-05-07 04:08:02 +0000 | [diff] [blame] | 529 | /// ReassociateBB - Inspect all of the instructions in this basic block, |
| 530 | /// reassociating them as we go. |
Chris Lattner | 1e50650 | 2005-05-07 21:59:39 +0000 | [diff] [blame] | 531 | void Reassociate::ReassociateBB(BasicBlock *BB) { |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 532 | for (BasicBlock::iterator BI = BB->begin(); BI != BB->end(); ++BI) { |
Chris Lattner | f43e974 | 2005-05-07 04:08:02 +0000 | [diff] [blame] | 533 | // If this is a subtract instruction which is not already in negate form, |
| 534 | // see if we can convert it to X+-Y. |
| 535 | if (BI->getOpcode() == Instruction::Sub && !BinaryOperator::isNeg(BI)) |
| 536 | if (Instruction *NI = BreakUpSubtract(BI)) { |
Chris Lattner | 1e50650 | 2005-05-07 21:59:39 +0000 | [diff] [blame] | 537 | MadeChange = true; |
Chris Lattner | f43e974 | 2005-05-07 04:08:02 +0000 | [diff] [blame] | 538 | BI = NI; |
| 539 | } |
Chris Lattner | cea5799 | 2005-05-07 04:24:13 +0000 | [diff] [blame] | 540 | if (BI->getOpcode() == Instruction::Shl && |
| 541 | isa<ConstantInt>(BI->getOperand(1))) |
| 542 | if (Instruction *NI = ConvertShiftToMul(BI)) { |
Chris Lattner | 1e50650 | 2005-05-07 21:59:39 +0000 | [diff] [blame] | 543 | MadeChange = true; |
Chris Lattner | cea5799 | 2005-05-07 04:24:13 +0000 | [diff] [blame] | 544 | BI = NI; |
| 545 | } |
Chris Lattner | 8fdf75c | 2002-10-31 17:12:59 +0000 | [diff] [blame] | 546 | |
Chris Lattner | 1e50650 | 2005-05-07 21:59:39 +0000 | [diff] [blame] | 547 | // If this instruction is a commutative binary operator, process it. |
| 548 | if (!BI->isAssociative()) continue; |
| 549 | BinaryOperator *I = cast<BinaryOperator>(BI); |
| 550 | |
| 551 | // If this is an interior node of a reassociable tree, ignore it until we |
| 552 | // get to the root of the tree, to avoid N^2 analysis. |
| 553 | if (I->hasOneUse() && isReassociableOp(I->use_back(), I->getOpcode())) |
| 554 | continue; |
Chris Lattner | 7bc532d | 2002-05-16 04:37:07 +0000 | [diff] [blame] | 555 | |
Chris Lattner | 1e50650 | 2005-05-07 21:59:39 +0000 | [diff] [blame] | 556 | // First, walk the expression tree, linearizing the tree, collecting |
| 557 | std::vector<ValueEntry> Ops; |
| 558 | LinearizeExprTree(I, Ops); |
| 559 | |
Chris Lattner | 9187f39 | 2005-05-08 20:09:57 +0000 | [diff] [blame] | 560 | DEBUG(std::cerr << "RAIn:\t"; PrintOps(I->getOpcode(), Ops, BB); |
| 561 | std::cerr << "\n"); |
| 562 | |
Chris Lattner | 1e50650 | 2005-05-07 21:59:39 +0000 | [diff] [blame] | 563 | // Now that we have linearized the tree to a list and have gathered all of |
| 564 | // the operands and their ranks, sort the operands by their rank. Use a |
| 565 | // stable_sort so that values with equal ranks will have their relative |
| 566 | // positions maintained (and so the compiler is deterministic). Note that |
| 567 | // this sorts so that the highest ranking values end up at the beginning of |
| 568 | // the vector. |
| 569 | std::stable_sort(Ops.begin(), Ops.end()); |
| 570 | |
Chris Lattner | e1850b8 | 2005-05-08 00:19:31 +0000 | [diff] [blame] | 571 | // OptimizeExpression - Now that we have the expression tree in a convenient |
| 572 | // sorted form, optimize it globally if possible. |
| 573 | OptimizeExpression(I->getOpcode(), Ops); |
Chris Lattner | 1e50650 | 2005-05-07 21:59:39 +0000 | [diff] [blame] | 574 | |
Chris Lattner | 9187f39 | 2005-05-08 20:09:57 +0000 | [diff] [blame] | 575 | DEBUG(std::cerr << "RAOut:\t"; PrintOps(I->getOpcode(), Ops, BB); |
| 576 | std::cerr << "\n"); |
| 577 | |
Chris Lattner | 1e50650 | 2005-05-07 21:59:39 +0000 | [diff] [blame] | 578 | if (Ops.size() == 1) { |
| 579 | // This expression tree simplified to something that isn't a tree, |
| 580 | // eliminate it. |
| 581 | I->replaceAllUsesWith(Ops[0].Op); |
| 582 | } else { |
| 583 | // Now that we ordered and optimized the expressions, splat them back into |
| 584 | // the expression tree, removing any unneeded nodes. |
| 585 | RewriteExprTree(I, 0, Ops); |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 586 | } |
| 587 | } |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 591 | bool Reassociate::runOnFunction(Function &F) { |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 592 | // Recalculate the rank map for F |
| 593 | BuildRankMap(F); |
| 594 | |
Chris Lattner | 1e50650 | 2005-05-07 21:59:39 +0000 | [diff] [blame] | 595 | MadeChange = false; |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 596 | for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) |
Chris Lattner | 1e50650 | 2005-05-07 21:59:39 +0000 | [diff] [blame] | 597 | ReassociateBB(FI); |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 598 | |
| 599 | // We are done with the rank map... |
| 600 | RankMap.clear(); |
Chris Lattner | 8ac196d | 2003-08-13 16:16:26 +0000 | [diff] [blame] | 601 | ValueRankMap.clear(); |
Chris Lattner | 1e50650 | 2005-05-07 21:59:39 +0000 | [diff] [blame] | 602 | return MadeChange; |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 603 | } |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 604 | |