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 | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 30 | #include "llvm/Support/CFG.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Debug.h" |
| 32 | #include "llvm/ADT/PostOrderIterator.h" |
| 33 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | 49525f8 | 2004-01-09 06:02:20 +0000 | [diff] [blame] | 34 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 35 | |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 36 | namespace { |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 37 | Statistic<> NumLinear ("reassociate","Number of insts linearized"); |
| 38 | Statistic<> NumChanged("reassociate","Number of insts reassociated"); |
| 39 | Statistic<> NumSwapped("reassociate","Number of insts with operands swapped"); |
| 40 | |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 41 | class Reassociate : public FunctionPass { |
Chris Lattner | 10073a9 | 2002-07-25 06:17:51 +0000 | [diff] [blame] | 42 | std::map<BasicBlock*, unsigned> RankMap; |
Chris Lattner | 8ac196d | 2003-08-13 16:16:26 +0000 | [diff] [blame] | 43 | std::map<Value*, unsigned> ValueRankMap; |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 44 | public: |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 45 | bool runOnFunction(Function &F); |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 46 | |
| 47 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | 820d971 | 2002-10-21 20:00:28 +0000 | [diff] [blame] | 48 | AU.setPreservesCFG(); |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 49 | } |
| 50 | private: |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 51 | void BuildRankMap(Function &F); |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 52 | unsigned getRank(Value *V); |
| 53 | bool ReassociateExpr(BinaryOperator *I); |
| 54 | bool ReassociateBB(BasicBlock *BB); |
| 55 | }; |
Chris Lattner | b28b680 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 56 | |
Chris Lattner | c8b7092 | 2002-07-26 21:12:46 +0000 | [diff] [blame] | 57 | RegisterOpt<Reassociate> X("reassociate", "Reassociate expressions"); |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 60 | // Public interface to the Reassociate pass |
Chris Lattner | 49525f8 | 2004-01-09 06:02:20 +0000 | [diff] [blame] | 61 | FunctionPass *llvm::createReassociatePass() { return new Reassociate(); } |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 62 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 63 | void Reassociate::BuildRankMap(Function &F) { |
Chris Lattner | 58c7eb6 | 2003-08-12 20:14:27 +0000 | [diff] [blame] | 64 | unsigned i = 2; |
Chris Lattner | 8ac196d | 2003-08-13 16:16:26 +0000 | [diff] [blame] | 65 | |
| 66 | // Assign distinct ranks to function arguments |
Chris Lattner | 531f9e9 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 67 | 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] | 68 | ValueRankMap[I] = ++i; |
| 69 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 70 | ReversePostOrderTraversal<Function*> RPOT(&F); |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 71 | for (ReversePostOrderTraversal<Function*>::rpo_iterator I = RPOT.begin(), |
| 72 | E = RPOT.end(); I != E; ++I) |
Chris Lattner | 58c7eb6 | 2003-08-12 20:14:27 +0000 | [diff] [blame] | 73 | RankMap[*I] = ++i << 16; |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | unsigned Reassociate::getRank(Value *V) { |
Chris Lattner | 8ac196d | 2003-08-13 16:16:26 +0000 | [diff] [blame] | 77 | if (isa<Argument>(V)) return ValueRankMap[V]; // Function argument... |
| 78 | |
Chris Lattner | f43e974 | 2005-05-07 04:08:02 +0000 | [diff] [blame] | 79 | Instruction *I = dyn_cast<Instruction>(V); |
| 80 | 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] | 81 | |
Chris Lattner | f43e974 | 2005-05-07 04:08:02 +0000 | [diff] [blame] | 82 | unsigned &CachedRank = ValueRankMap[I]; |
| 83 | if (CachedRank) return CachedRank; // Rank already known? |
| 84 | |
| 85 | // If this is an expression, return the 1+MAX(rank(LHS), rank(RHS)) so that |
| 86 | // we can reassociate expressions for code motion! Since we do not recurse |
| 87 | // for PHI nodes, we cannot have infinite recursion here, because there |
| 88 | // cannot be loops in the value graph that do not go through PHI nodes. |
| 89 | // |
| 90 | if (I->getOpcode() == Instruction::PHI || |
| 91 | I->getOpcode() == Instruction::Alloca || |
| 92 | I->getOpcode() == Instruction::Malloc || isa<TerminatorInst>(I) || |
| 93 | I->mayWriteToMemory()) // Cannot move inst if it writes to memory! |
| 94 | return RankMap[I->getParent()]; |
| 95 | |
| 96 | // If not, compute it! |
| 97 | unsigned Rank = 0, MaxRank = RankMap[I->getParent()]; |
| 98 | for (unsigned i = 0, e = I->getNumOperands(); |
| 99 | i != e && Rank != MaxRank; ++i) |
| 100 | Rank = std::max(Rank, getRank(I->getOperand(i))); |
| 101 | |
| 102 | DEBUG(std::cerr << "Calculated Rank[" << V->getName() << "] = " |
| 103 | << Rank+1 << "\n"); |
| 104 | |
| 105 | return CachedRank = Rank+1; |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 109 | bool Reassociate::ReassociateExpr(BinaryOperator *I) { |
| 110 | Value *LHS = I->getOperand(0); |
| 111 | Value *RHS = I->getOperand(1); |
| 112 | unsigned LHSRank = getRank(LHS); |
| 113 | unsigned RHSRank = getRank(RHS); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 114 | |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 115 | bool Changed = false; |
| 116 | |
| 117 | // Make sure the LHS of the operand always has the greater rank... |
| 118 | if (LHSRank < RHSRank) { |
Chris Lattner | 8fdf75c | 2002-10-31 17:12:59 +0000 | [diff] [blame] | 119 | bool Success = !I->swapOperands(); |
| 120 | assert(Success && "swapOperands failed"); |
| 121 | |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 122 | std::swap(LHS, RHS); |
| 123 | std::swap(LHSRank, RHSRank); |
| 124 | Changed = true; |
Chris Lattner | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 125 | ++NumSwapped; |
Chris Lattner | 9a63520b | 2004-07-15 01:50:47 +0000 | [diff] [blame] | 126 | DEBUG(std::cerr << "Transposed: " << *I |
Chris Lattner | f96c8be | 2002-12-15 03:49:50 +0000 | [diff] [blame] | 127 | /* << " Result BB: " << I->getParent()*/); |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 128 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 129 | |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 130 | // If the LHS is the same operator as the current one is, and if we are the |
| 131 | // only expression using it... |
| 132 | // |
| 133 | if (BinaryOperator *LHSI = dyn_cast<BinaryOperator>(LHS)) |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 134 | if (LHSI->getOpcode() == I->getOpcode() && LHSI->hasOneUse()) { |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 135 | // If the rank of our current RHS is less than the rank of the LHS's LHS, |
| 136 | // then we reassociate the two instructions... |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 137 | |
Chris Lattner | 98b3ecd | 2003-08-12 21:45:24 +0000 | [diff] [blame] | 138 | unsigned TakeOp = 0; |
| 139 | if (BinaryOperator *IOp = dyn_cast<BinaryOperator>(LHSI->getOperand(0))) |
| 140 | if (IOp->getOpcode() == LHSI->getOpcode()) |
| 141 | TakeOp = 1; // Hoist out non-tree portion |
| 142 | |
| 143 | if (RHSRank < getRank(LHSI->getOperand(TakeOp))) { |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 144 | // Convert ((a + 12) + 10) into (a + (12 + 10)) |
| 145 | I->setOperand(0, LHSI->getOperand(TakeOp)); |
Chris Lattner | f96c8be | 2002-12-15 03:49:50 +0000 | [diff] [blame] | 146 | LHSI->setOperand(TakeOp, RHS); |
| 147 | I->setOperand(1, LHSI); |
Chris Lattner | 8fdf75c | 2002-10-31 17:12:59 +0000 | [diff] [blame] | 148 | |
| 149 | // Move the LHS expression forward, to ensure that it is dominated by |
| 150 | // its operands. |
Chris Lattner | f96c8be | 2002-12-15 03:49:50 +0000 | [diff] [blame] | 151 | LHSI->getParent()->getInstList().remove(LHSI); |
| 152 | I->getParent()->getInstList().insert(I, LHSI); |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 153 | |
Chris Lattner | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 154 | ++NumChanged; |
Chris Lattner | 9a63520b | 2004-07-15 01:50:47 +0000 | [diff] [blame] | 155 | DEBUG(std::cerr << "Reassociated: " << *I/* << " Result BB: " |
Chris Lattner | f96c8be | 2002-12-15 03:49:50 +0000 | [diff] [blame] | 156 | << I->getParent()*/); |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 157 | |
| 158 | // Since we modified the RHS instruction, make sure that we recheck it. |
Chris Lattner | f96c8be | 2002-12-15 03:49:50 +0000 | [diff] [blame] | 159 | ReassociateExpr(LHSI); |
Chris Lattner | 58c7eb6 | 2003-08-12 20:14:27 +0000 | [diff] [blame] | 160 | ReassociateExpr(I); |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 161 | return true; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | return Changed; |
| 166 | } |
| 167 | |
| 168 | |
Chris Lattner | 7bc532d | 2002-05-16 04:37:07 +0000 | [diff] [blame] | 169 | // NegateValue - Insert instructions before the instruction pointed to by BI, |
| 170 | // that computes the negative version of the value specified. The negative |
| 171 | // version of the value is returned, and BI is left pointing at the instruction |
| 172 | // that should be processed next by the reassociation pass. |
| 173 | // |
Chris Lattner | f43e974 | 2005-05-07 04:08:02 +0000 | [diff] [blame] | 174 | static Value *NegateValue(Value *V, Instruction *BI) { |
Chris Lattner | 7bc532d | 2002-05-16 04:37:07 +0000 | [diff] [blame] | 175 | // We are trying to expose opportunity for reassociation. One of the things |
| 176 | // that we want to do to achieve this is to push a negation as deep into an |
| 177 | // expression chain as possible, to expose the add instructions. In practice, |
| 178 | // this means that we turn this: |
| 179 | // X = -(A+12+C+D) into X = -A + -12 + -C + -D = -12 + -A + -C + -D |
| 180 | // so that later, a: Y = 12+X could get reassociated with the -12 to eliminate |
| 181 | // 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] | 182 | // we introduce tons of unnecessary negation instructions... |
Chris Lattner | 7bc532d | 2002-05-16 04:37:07 +0000 | [diff] [blame] | 183 | // |
| 184 | if (Instruction *I = dyn_cast<Instruction>(V)) |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 185 | if (I->getOpcode() == Instruction::Add && I->hasOneUse()) { |
Chris Lattner | 8fdf75c | 2002-10-31 17:12:59 +0000 | [diff] [blame] | 186 | Value *RHS = NegateValue(I->getOperand(1), BI); |
| 187 | Value *LHS = NegateValue(I->getOperand(0), BI); |
Chris Lattner | 7bc532d | 2002-05-16 04:37:07 +0000 | [diff] [blame] | 188 | |
| 189 | // We must actually insert a new add instruction here, because the neg |
| 190 | // instructions do not dominate the old add instruction in general. By |
| 191 | // adding it now, we are assured that the neg instructions we just |
| 192 | // inserted dominate the instruction we are about to insert after them. |
| 193 | // |
Chris Lattner | 28a8d24 | 2002-09-10 17:04:02 +0000 | [diff] [blame] | 194 | return BinaryOperator::create(Instruction::Add, LHS, RHS, |
Chris Lattner | f43e974 | 2005-05-07 04:08:02 +0000 | [diff] [blame] | 195 | I->getName()+".neg", BI); |
Chris Lattner | 7bc532d | 2002-05-16 04:37:07 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | // Insert a 'neg' instruction that subtracts the value from zero to get the |
| 199 | // negation. |
| 200 | // |
Chris Lattner | f43e974 | 2005-05-07 04:08:02 +0000 | [diff] [blame] | 201 | return BinaryOperator::createNeg(V, V->getName() + ".neg", BI); |
| 202 | } |
| 203 | |
| 204 | /// isReassociableOp - Return true if V is an instruction of the specified |
| 205 | /// opcode and if it only has one use. |
| 206 | static bool isReassociableOp(Value *V, unsigned Opcode) { |
| 207 | return V->hasOneUse() && isa<Instruction>(V) && |
| 208 | cast<Instruction>(V)->getOpcode() == Opcode; |
| 209 | } |
| 210 | |
| 211 | /// BreakUpSubtract - If we have (X-Y), and if either X is an add, or if this is |
| 212 | /// only used by an add, transform this into (X+(0-Y)) to promote better |
| 213 | /// reassociation. |
| 214 | static Instruction *BreakUpSubtract(Instruction *Sub) { |
| 215 | // Reject cases where it is pointless to do this. |
| 216 | if (Sub->getType()->isFloatingPoint()) |
| 217 | return 0; // Floating point adds are not associative. |
| 218 | |
| 219 | // Don't bother to break this up unless either the LHS is an associable add or |
| 220 | // if this is only used by one. |
| 221 | if (!isReassociableOp(Sub->getOperand(0), Instruction::Add) && |
| 222 | !isReassociableOp(Sub->getOperand(1), Instruction::Add) && |
| 223 | !(Sub->hasOneUse() &&isReassociableOp(Sub->use_back(), Instruction::Add))) |
| 224 | return 0; |
| 225 | |
| 226 | // Convert a subtract into an add and a neg instruction... so that sub |
| 227 | // instructions can be commuted with other add instructions... |
| 228 | // |
| 229 | // Calculate the negative value of Operand 1 of the sub instruction... |
| 230 | // and set it as the RHS of the add instruction we just made... |
| 231 | // |
| 232 | std::string Name = Sub->getName(); |
| 233 | Sub->setName(""); |
| 234 | Value *NegVal = NegateValue(Sub->getOperand(1), Sub); |
| 235 | Instruction *New = |
| 236 | BinaryOperator::createAdd(Sub->getOperand(0), NegVal, Name, Sub); |
| 237 | |
| 238 | // Everyone now refers to the add instruction. |
| 239 | Sub->replaceAllUsesWith(New); |
| 240 | Sub->eraseFromParent(); |
| 241 | |
| 242 | DEBUG(std::cerr << "Negated: " << *New); |
| 243 | return New; |
Chris Lattner | 7bc532d | 2002-05-16 04:37:07 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Chris Lattner | cea5799 | 2005-05-07 04:24:13 +0000 | [diff] [blame] | 246 | /// ConvertShiftToMul - If this is a shift of a reassociable multiply or is used |
| 247 | /// by one, change this into a multiply by a constant to assist with further |
| 248 | /// reassociation. |
| 249 | static Instruction *ConvertShiftToMul(Instruction *Shl) { |
| 250 | if (!isReassociableOp(Shl->getOperand(0), Instruction::Mul) && |
| 251 | !(Shl->hasOneUse() && isReassociableOp(Shl->use_back(),Instruction::Mul))) |
| 252 | return 0; |
| 253 | |
| 254 | Constant *MulCst = ConstantInt::get(Shl->getType(), 1); |
| 255 | MulCst = ConstantExpr::getShl(MulCst, cast<Constant>(Shl->getOperand(1))); |
| 256 | |
| 257 | std::string Name = Shl->getName(); Shl->setName(""); |
| 258 | Instruction *Mul = BinaryOperator::createMul(Shl->getOperand(0), MulCst, |
| 259 | Name, Shl); |
| 260 | Shl->replaceAllUsesWith(Mul); |
| 261 | Shl->eraseFromParent(); |
| 262 | return Mul; |
| 263 | } |
| 264 | |
Chris Lattner | 7bc532d | 2002-05-16 04:37:07 +0000 | [diff] [blame] | 265 | |
Chris Lattner | f43e974 | 2005-05-07 04:08:02 +0000 | [diff] [blame] | 266 | /// ReassociateBB - Inspect all of the instructions in this basic block, |
| 267 | /// reassociating them as we go. |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 268 | bool Reassociate::ReassociateBB(BasicBlock *BB) { |
| 269 | bool Changed = false; |
| 270 | for (BasicBlock::iterator BI = BB->begin(); BI != BB->end(); ++BI) { |
Chris Lattner | f43e974 | 2005-05-07 04:08:02 +0000 | [diff] [blame] | 271 | // If this is a subtract instruction which is not already in negate form, |
| 272 | // see if we can convert it to X+-Y. |
| 273 | if (BI->getOpcode() == Instruction::Sub && !BinaryOperator::isNeg(BI)) |
| 274 | if (Instruction *NI = BreakUpSubtract(BI)) { |
| 275 | Changed = true; |
| 276 | BI = NI; |
| 277 | } |
Chris Lattner | cea5799 | 2005-05-07 04:24:13 +0000 | [diff] [blame] | 278 | if (BI->getOpcode() == Instruction::Shl && |
| 279 | isa<ConstantInt>(BI->getOperand(1))) |
| 280 | if (Instruction *NI = ConvertShiftToMul(BI)) { |
| 281 | Changed = true; |
| 282 | BI = NI; |
| 283 | } |
Chris Lattner | 8fdf75c | 2002-10-31 17:12:59 +0000 | [diff] [blame] | 284 | |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 285 | // If this instruction is a commutative binary operator, and the ranks of |
| 286 | // the two operands are sorted incorrectly, fix it now. |
| 287 | // |
Chris Lattner | 8fdf75c | 2002-10-31 17:12:59 +0000 | [diff] [blame] | 288 | if (BI->isAssociative()) { |
Chris Lattner | f43e974 | 2005-05-07 04:08:02 +0000 | [diff] [blame] | 289 | DEBUG(std::cerr << "Reassociating: " << *BI); |
Chris Lattner | 889f620 | 2003-04-23 16:37:45 +0000 | [diff] [blame] | 290 | BinaryOperator *I = cast<BinaryOperator>(BI); |
Chris Lattner | 7bc532d | 2002-05-16 04:37:07 +0000 | [diff] [blame] | 291 | if (!I->use_empty()) { |
| 292 | // Make sure that we don't have a tree-shaped computation. If we do, |
| 293 | // linearize it. Convert (A+B)+(C+D) into ((A+B)+C)+D |
| 294 | // |
| 295 | Instruction *LHSI = dyn_cast<Instruction>(I->getOperand(0)); |
| 296 | Instruction *RHSI = dyn_cast<Instruction>(I->getOperand(1)); |
| 297 | if (LHSI && (int)LHSI->getOpcode() == I->getOpcode() && |
| 298 | RHSI && (int)RHSI->getOpcode() == I->getOpcode() && |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 299 | RHSI->hasOneUse()) { |
Chris Lattner | 7bc532d | 2002-05-16 04:37:07 +0000 | [diff] [blame] | 300 | // Insert a new temporary instruction... (A+B)+C |
| 301 | BinaryOperator *Tmp = BinaryOperator::create(I->getOpcode(), LHSI, |
| 302 | RHSI->getOperand(0), |
Chris Lattner | 28a8d24 | 2002-09-10 17:04:02 +0000 | [diff] [blame] | 303 | RHSI->getName()+".ra", |
| 304 | BI); |
| 305 | BI = Tmp; |
Chris Lattner | 7bc532d | 2002-05-16 04:37:07 +0000 | [diff] [blame] | 306 | I->setOperand(0, Tmp); |
| 307 | I->setOperand(1, RHSI->getOperand(1)); |
| 308 | |
| 309 | // Process the temporary instruction for reassociation now. |
| 310 | I = Tmp; |
| 311 | ++NumLinear; |
| 312 | Changed = true; |
Chris Lattner | 9a63520b | 2004-07-15 01:50:47 +0000 | [diff] [blame] | 313 | DEBUG(std::cerr << "Linearized: " << *I/* << " Result BB: " << BB*/); |
Chris Lattner | 7bc532d | 2002-05-16 04:37:07 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | // Make sure that this expression is correctly reassociated with respect |
| 317 | // to it's used values... |
| 318 | // |
| 319 | Changed |= ReassociateExpr(I); |
| 320 | } |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 321 | } |
| 322 | } |
| 323 | |
| 324 | return Changed; |
| 325 | } |
| 326 | |
| 327 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 328 | bool Reassociate::runOnFunction(Function &F) { |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 329 | // Recalculate the rank map for F |
| 330 | BuildRankMap(F); |
| 331 | |
| 332 | bool Changed = false; |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 333 | for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) |
| 334 | Changed |= ReassociateBB(FI); |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 335 | |
| 336 | // We are done with the rank map... |
| 337 | RankMap.clear(); |
Chris Lattner | 8ac196d | 2003-08-13 16:16:26 +0000 | [diff] [blame] | 338 | ValueRankMap.clear(); |
Chris Lattner | c0f5800 | 2002-05-08 22:19:27 +0000 | [diff] [blame] | 339 | return Changed; |
| 340 | } |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 341 | |