Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1 | //===- InstructionCombining.cpp - Combine multiple instructions -----------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9 | // |
| 10 | // InstructionCombining - Combine instructions to form fewer, simple |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 11 | // instructions. This pass does not modify the CFG. This pass is where |
| 12 | // algebraic simplification happens. |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 13 | // |
| 14 | // This pass combines things like: |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 15 | // %Y = add i32 %X, 1 |
| 16 | // %Z = add i32 %Y, 1 |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 17 | // into: |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 18 | // %Z = add i32 %X, 2 |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 19 | // |
| 20 | // This is a simple worklist driven algorithm. |
| 21 | // |
Chris Lattner | 065a616 | 2003-09-10 05:29:43 +0000 | [diff] [blame] | 22 | // This pass guarantees that the following canonicalizations are performed on |
Chris Lattner | 2cd9196 | 2003-07-23 21:41:57 +0000 | [diff] [blame] | 23 | // the program: |
| 24 | // 1. If a binary operator has a constant operand, it is moved to the RHS |
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 25 | // 2. Bitwise operators with constant operands are always grouped so that |
| 26 | // shifts are performed first, then or's, then and's, then xor's. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 27 | // 3. Compare instructions are converted from <,>,<=,>= to ==,!= if possible |
| 28 | // 4. All cmp instructions on boolean values are replaced with logical ops |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 29 | // 5. add X, X is represented as (X*2) => (X << 1) |
| 30 | // 6. Multiplies with a power-of-two constant argument are transformed into |
| 31 | // shifts. |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 32 | // ... etc. |
Chris Lattner | 2cd9196 | 2003-07-23 21:41:57 +0000 | [diff] [blame] | 33 | // |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 34 | //===----------------------------------------------------------------------===// |
| 35 | |
Chris Lattner | 0cea42a | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 36 | #define DEBUG_TYPE "instcombine" |
Chris Lattner | 022103b | 2002-05-07 20:03:00 +0000 | [diff] [blame] | 37 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | ac8f2fd | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 38 | #include "InstCombine.h" |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 39 | #include "llvm/IntrinsicInst.h" |
Chris Lattner | 79066fa | 2007-01-30 23:46:24 +0000 | [diff] [blame] | 40 | #include "llvm/Analysis/ConstantFolding.h" |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 41 | #include "llvm/Analysis/InstructionSimplify.h" |
Victor Hernandez | f006b18 | 2009-10-27 20:05:49 +0000 | [diff] [blame] | 42 | #include "llvm/Analysis/MemoryBuiltins.h" |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 43 | #include "llvm/Target/TargetData.h" |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 44 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | 804272c | 2010-01-05 07:54:43 +0000 | [diff] [blame] | 45 | #include "llvm/Support/CFG.h" |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 46 | #include "llvm/Support/Debug.h" |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 47 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 48 | #include "llvm/Support/PatternMatch.h" |
Chris Lattner | 1f87a58 | 2007-02-15 19:41:52 +0000 | [diff] [blame] | 49 | #include "llvm/ADT/SmallPtrSet.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 50 | #include "llvm/ADT/Statistic.h" |
Owen Anderson | 74cfb0c | 2010-10-07 20:04:55 +0000 | [diff] [blame] | 51 | #include "llvm-c/Initialization.h" |
Chris Lattner | b3bc8fa | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 52 | #include <algorithm> |
Torok Edwin | 3eaee31 | 2008-04-20 08:33:11 +0000 | [diff] [blame] | 53 | #include <climits> |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 54 | using namespace llvm; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 55 | using namespace llvm::PatternMatch; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 56 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 57 | STATISTIC(NumCombined , "Number of insts combined"); |
| 58 | STATISTIC(NumConstProp, "Number of constant folds"); |
| 59 | STATISTIC(NumDeadInst , "Number of dead inst eliminated"); |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 60 | STATISTIC(NumSunkInst , "Number of instructions sunk"); |
Duncan Sands | 37bf92b | 2010-12-22 13:36:08 +0000 | [diff] [blame] | 61 | STATISTIC(NumExpand, "Number of expansions"); |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 62 | STATISTIC(NumFactor , "Number of factorizations"); |
| 63 | STATISTIC(NumReassoc , "Number of reassociations"); |
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 64 | |
Owen Anderson | 74cfb0c | 2010-10-07 20:04:55 +0000 | [diff] [blame] | 65 | // Initialization Routines |
| 66 | void llvm::initializeInstCombine(PassRegistry &Registry) { |
| 67 | initializeInstCombinerPass(Registry); |
| 68 | } |
| 69 | |
| 70 | void LLVMInitializeInstCombine(LLVMPassRegistryRef R) { |
| 71 | initializeInstCombine(*unwrap(R)); |
| 72 | } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 73 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 74 | char InstCombiner::ID = 0; |
Owen Anderson | d13db2c | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 75 | INITIALIZE_PASS(InstCombiner, "instcombine", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 76 | "Combine redundant instructions", false, false) |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 77 | |
Chris Lattner | e0b4b72 | 2010-01-04 07:17:19 +0000 | [diff] [blame] | 78 | void InstCombiner::getAnalysisUsage(AnalysisUsage &AU) const { |
| 79 | AU.addPreservedID(LCSSAID); |
| 80 | AU.setPreservesCFG(); |
| 81 | } |
| 82 | |
| 83 | |
Chris Lattner | c22d4d1 | 2009-11-10 07:23:37 +0000 | [diff] [blame] | 84 | /// ShouldChangeType - Return true if it is desirable to convert a computation |
| 85 | /// from 'From' to 'To'. We don't want to convert from a legal to an illegal |
| 86 | /// type for example, or from a smaller to a larger illegal type. |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 87 | bool InstCombiner::ShouldChangeType(const Type *From, const Type *To) const { |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 88 | assert(From->isIntegerTy() && To->isIntegerTy()); |
Chris Lattner | c22d4d1 | 2009-11-10 07:23:37 +0000 | [diff] [blame] | 89 | |
| 90 | // If we don't have TD, we don't know if the source/dest are legal. |
| 91 | if (!TD) return false; |
| 92 | |
| 93 | unsigned FromWidth = From->getPrimitiveSizeInBits(); |
| 94 | unsigned ToWidth = To->getPrimitiveSizeInBits(); |
| 95 | bool FromLegal = TD->isLegalInteger(FromWidth); |
| 96 | bool ToLegal = TD->isLegalInteger(ToWidth); |
| 97 | |
| 98 | // If this is a legal integer from type, and the result would be an illegal |
| 99 | // type, don't do the transformation. |
| 100 | if (FromLegal && !ToLegal) |
| 101 | return false; |
| 102 | |
| 103 | // Otherwise, if both are illegal, do not increase the size of the result. We |
| 104 | // do allow things like i160 -> i64, but not i64 -> i160. |
| 105 | if (!FromLegal && !ToLegal && ToWidth > FromWidth) |
| 106 | return false; |
| 107 | |
| 108 | return true; |
| 109 | } |
| 110 | |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 111 | |
Duncan Sands | 096aa79 | 2010-11-13 15:10:37 +0000 | [diff] [blame] | 112 | /// SimplifyAssociativeOrCommutative - This performs a few simplifications for |
| 113 | /// operators which are associative or commutative: |
| 114 | // |
| 115 | // Commutative operators: |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 116 | // |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 117 | // 1. Order operands such that they are listed from right (least complex) to |
| 118 | // left (most complex). This puts constants before unary operators before |
| 119 | // binary operators. |
| 120 | // |
Duncan Sands | 096aa79 | 2010-11-13 15:10:37 +0000 | [diff] [blame] | 121 | // Associative operators: |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 122 | // |
Duncan Sands | 096aa79 | 2010-11-13 15:10:37 +0000 | [diff] [blame] | 123 | // 2. Transform: "(A op B) op C" ==> "A op (B op C)" if "B op C" simplifies. |
| 124 | // 3. Transform: "A op (B op C)" ==> "(A op B) op C" if "A op B" simplifies. |
| 125 | // |
| 126 | // Associative and commutative operators: |
| 127 | // |
| 128 | // 4. Transform: "(A op B) op C" ==> "(C op A) op B" if "C op A" simplifies. |
| 129 | // 5. Transform: "A op (B op C)" ==> "B op (C op A)" if "C op A" simplifies. |
| 130 | // 6. Transform: "(A op C1) op (B op C2)" ==> "(A op B) op (C1 op C2)" |
| 131 | // if C1 and C2 are constants. |
| 132 | // |
| 133 | bool InstCombiner::SimplifyAssociativeOrCommutative(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 134 | Instruction::BinaryOps Opcode = I.getOpcode(); |
Duncan Sands | 096aa79 | 2010-11-13 15:10:37 +0000 | [diff] [blame] | 135 | bool Changed = false; |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 136 | |
Duncan Sands | 096aa79 | 2010-11-13 15:10:37 +0000 | [diff] [blame] | 137 | do { |
| 138 | // Order operands such that they are listed from right (least complex) to |
| 139 | // left (most complex). This puts constants before unary operators before |
| 140 | // binary operators. |
| 141 | if (I.isCommutative() && getComplexity(I.getOperand(0)) < |
| 142 | getComplexity(I.getOperand(1))) |
| 143 | Changed = !I.swapOperands(); |
| 144 | |
| 145 | BinaryOperator *Op0 = dyn_cast<BinaryOperator>(I.getOperand(0)); |
| 146 | BinaryOperator *Op1 = dyn_cast<BinaryOperator>(I.getOperand(1)); |
| 147 | |
| 148 | if (I.isAssociative()) { |
| 149 | // Transform: "(A op B) op C" ==> "A op (B op C)" if "B op C" simplifies. |
| 150 | if (Op0 && Op0->getOpcode() == Opcode) { |
| 151 | Value *A = Op0->getOperand(0); |
| 152 | Value *B = Op0->getOperand(1); |
| 153 | Value *C = I.getOperand(1); |
| 154 | |
| 155 | // Does "B op C" simplify? |
| 156 | if (Value *V = SimplifyBinOp(Opcode, B, C, TD)) { |
| 157 | // It simplifies to V. Form "A op V". |
| 158 | I.setOperand(0, A); |
| 159 | I.setOperand(1, V); |
| 160 | Changed = true; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 161 | ++NumReassoc; |
Duncan Sands | 096aa79 | 2010-11-13 15:10:37 +0000 | [diff] [blame] | 162 | continue; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 163 | } |
Duncan Sands | 096aa79 | 2010-11-13 15:10:37 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | // Transform: "A op (B op C)" ==> "(A op B) op C" if "A op B" simplifies. |
| 167 | if (Op1 && Op1->getOpcode() == Opcode) { |
| 168 | Value *A = I.getOperand(0); |
| 169 | Value *B = Op1->getOperand(0); |
| 170 | Value *C = Op1->getOperand(1); |
| 171 | |
| 172 | // Does "A op B" simplify? |
| 173 | if (Value *V = SimplifyBinOp(Opcode, A, B, TD)) { |
| 174 | // It simplifies to V. Form "V op C". |
| 175 | I.setOperand(0, V); |
| 176 | I.setOperand(1, C); |
| 177 | Changed = true; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 178 | ++NumReassoc; |
Duncan Sands | 096aa79 | 2010-11-13 15:10:37 +0000 | [diff] [blame] | 179 | continue; |
| 180 | } |
| 181 | } |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 182 | } |
Duncan Sands | 096aa79 | 2010-11-13 15:10:37 +0000 | [diff] [blame] | 183 | |
| 184 | if (I.isAssociative() && I.isCommutative()) { |
| 185 | // Transform: "(A op B) op C" ==> "(C op A) op B" if "C op A" simplifies. |
| 186 | if (Op0 && Op0->getOpcode() == Opcode) { |
| 187 | Value *A = Op0->getOperand(0); |
| 188 | Value *B = Op0->getOperand(1); |
| 189 | Value *C = I.getOperand(1); |
| 190 | |
| 191 | // Does "C op A" simplify? |
| 192 | if (Value *V = SimplifyBinOp(Opcode, C, A, TD)) { |
| 193 | // It simplifies to V. Form "V op B". |
| 194 | I.setOperand(0, V); |
| 195 | I.setOperand(1, B); |
| 196 | Changed = true; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 197 | ++NumReassoc; |
Duncan Sands | 096aa79 | 2010-11-13 15:10:37 +0000 | [diff] [blame] | 198 | continue; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | // Transform: "A op (B op C)" ==> "B op (C op A)" if "C op A" simplifies. |
| 203 | if (Op1 && Op1->getOpcode() == Opcode) { |
| 204 | Value *A = I.getOperand(0); |
| 205 | Value *B = Op1->getOperand(0); |
| 206 | Value *C = Op1->getOperand(1); |
| 207 | |
| 208 | // Does "C op A" simplify? |
| 209 | if (Value *V = SimplifyBinOp(Opcode, C, A, TD)) { |
| 210 | // It simplifies to V. Form "B op V". |
| 211 | I.setOperand(0, B); |
| 212 | I.setOperand(1, V); |
| 213 | Changed = true; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 214 | ++NumReassoc; |
Duncan Sands | 096aa79 | 2010-11-13 15:10:37 +0000 | [diff] [blame] | 215 | continue; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | // Transform: "(A op C1) op (B op C2)" ==> "(A op B) op (C1 op C2)" |
| 220 | // if C1 and C2 are constants. |
| 221 | if (Op0 && Op1 && |
| 222 | Op0->getOpcode() == Opcode && Op1->getOpcode() == Opcode && |
| 223 | isa<Constant>(Op0->getOperand(1)) && |
| 224 | isa<Constant>(Op1->getOperand(1)) && |
| 225 | Op0->hasOneUse() && Op1->hasOneUse()) { |
| 226 | Value *A = Op0->getOperand(0); |
| 227 | Constant *C1 = cast<Constant>(Op0->getOperand(1)); |
| 228 | Value *B = Op1->getOperand(0); |
| 229 | Constant *C2 = cast<Constant>(Op1->getOperand(1)); |
| 230 | |
| 231 | Constant *Folded = ConstantExpr::get(Opcode, C1, C2); |
| 232 | Instruction *New = BinaryOperator::Create(Opcode, A, B, Op1->getName(), |
| 233 | &I); |
| 234 | Worklist.Add(New); |
| 235 | I.setOperand(0, New); |
| 236 | I.setOperand(1, Folded); |
| 237 | Changed = true; |
| 238 | continue; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | // No further simplifications. |
| 243 | return Changed; |
| 244 | } while (1); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 245 | } |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 246 | |
Duncan Sands | 5057f38 | 2010-11-23 14:23:47 +0000 | [diff] [blame] | 247 | /// LeftDistributesOverRight - Whether "X LOp (Y ROp Z)" is always equal to |
Duncan Sands | c2b1c0b | 2010-11-23 15:25:34 +0000 | [diff] [blame] | 248 | /// "(X LOp Y) ROp (X LOp Z)". |
Duncan Sands | 5057f38 | 2010-11-23 14:23:47 +0000 | [diff] [blame] | 249 | static bool LeftDistributesOverRight(Instruction::BinaryOps LOp, |
| 250 | Instruction::BinaryOps ROp) { |
| 251 | switch (LOp) { |
| 252 | default: |
| 253 | return false; |
| 254 | |
| 255 | case Instruction::And: |
| 256 | // And distributes over Or and Xor. |
| 257 | switch (ROp) { |
| 258 | default: |
| 259 | return false; |
| 260 | case Instruction::Or: |
| 261 | case Instruction::Xor: |
| 262 | return true; |
| 263 | } |
| 264 | |
| 265 | case Instruction::Mul: |
| 266 | // Multiplication distributes over addition and subtraction. |
| 267 | switch (ROp) { |
| 268 | default: |
| 269 | return false; |
| 270 | case Instruction::Add: |
| 271 | case Instruction::Sub: |
| 272 | return true; |
| 273 | } |
| 274 | |
| 275 | case Instruction::Or: |
| 276 | // Or distributes over And. |
| 277 | switch (ROp) { |
| 278 | default: |
| 279 | return false; |
| 280 | case Instruction::And: |
| 281 | return true; |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | /// RightDistributesOverLeft - Whether "(X LOp Y) ROp Z" is always equal to |
| 287 | /// "(X ROp Z) LOp (Y ROp Z)". |
| 288 | static bool RightDistributesOverLeft(Instruction::BinaryOps LOp, |
| 289 | Instruction::BinaryOps ROp) { |
| 290 | if (Instruction::isCommutative(ROp)) |
| 291 | return LeftDistributesOverRight(ROp, LOp); |
| 292 | // TODO: It would be nice to handle division, aka "(X + Y)/Z = X/Z + Y/Z", |
| 293 | // but this requires knowing that the addition does not overflow and other |
| 294 | // such subtleties. |
| 295 | return false; |
| 296 | } |
| 297 | |
Duncan Sands | 37bf92b | 2010-12-22 13:36:08 +0000 | [diff] [blame] | 298 | /// SimplifyUsingDistributiveLaws - This tries to simplify binary operations |
| 299 | /// which some other binary operation distributes over either by factorizing |
| 300 | /// out common terms (eg "(A*B)+(A*C)" -> "A*(B+C)") or expanding out if this |
| 301 | /// results in simplifications (eg: "A & (B | C) -> (A&B) | (A&C)" if this is |
| 302 | /// a win). Returns the simplified value, or null if it didn't simplify. |
| 303 | Value *InstCombiner::SimplifyUsingDistributiveLaws(BinaryOperator &I) { |
| 304 | Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); |
| 305 | BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS); |
| 306 | BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS); |
| 307 | Instruction::BinaryOps TopLevelOpcode = I.getOpcode(); // op |
Duncan Sands | 5057f38 | 2010-11-23 14:23:47 +0000 | [diff] [blame] | 308 | |
Duncan Sands | 37bf92b | 2010-12-22 13:36:08 +0000 | [diff] [blame] | 309 | // Factorization. |
| 310 | if (Op0 && Op1 && Op0->getOpcode() == Op1->getOpcode()) { |
| 311 | // The instruction has the form "(A op' B) op (C op' D)". Try to factorize |
| 312 | // a common term. |
| 313 | Value *A = Op0->getOperand(0), *B = Op0->getOperand(1); |
| 314 | Value *C = Op1->getOperand(0), *D = Op1->getOperand(1); |
| 315 | Instruction::BinaryOps InnerOpcode = Op0->getOpcode(); // op' |
Duncan Sands | 5057f38 | 2010-11-23 14:23:47 +0000 | [diff] [blame] | 316 | |
Duncan Sands | 37bf92b | 2010-12-22 13:36:08 +0000 | [diff] [blame] | 317 | // Does "X op' Y" always equal "Y op' X"? |
| 318 | bool InnerCommutative = Instruction::isCommutative(InnerOpcode); |
Duncan Sands | 5057f38 | 2010-11-23 14:23:47 +0000 | [diff] [blame] | 319 | |
Duncan Sands | 37bf92b | 2010-12-22 13:36:08 +0000 | [diff] [blame] | 320 | // Does "X op' (Y op Z)" always equal "(X op' Y) op (X op' Z)"? |
| 321 | if (LeftDistributesOverRight(InnerOpcode, TopLevelOpcode)) |
| 322 | // Does the instruction have the form "(A op' B) op (A op' D)" or, in the |
| 323 | // commutative case, "(A op' B) op (C op' A)"? |
| 324 | if (A == C || (InnerCommutative && A == D)) { |
| 325 | if (A != C) |
| 326 | std::swap(C, D); |
| 327 | // Consider forming "A op' (B op D)". |
| 328 | // If "B op D" simplifies then it can be formed with no cost. |
| 329 | Value *V = SimplifyBinOp(TopLevelOpcode, B, D, TD); |
| 330 | // If "B op D" doesn't simplify then only go on if both of the existing |
| 331 | // operations "A op' B" and "C op' D" will be zapped as no longer used. |
| 332 | if (!V && Op0->hasOneUse() && Op1->hasOneUse()) |
| 333 | V = Builder->CreateBinOp(TopLevelOpcode, B, D, Op1->getName()); |
| 334 | if (V) { |
| 335 | ++NumFactor; |
| 336 | V = Builder->CreateBinOp(InnerOpcode, A, V); |
| 337 | V->takeName(&I); |
| 338 | return V; |
| 339 | } |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 340 | } |
Duncan Sands | 5057f38 | 2010-11-23 14:23:47 +0000 | [diff] [blame] | 341 | |
Duncan Sands | 37bf92b | 2010-12-22 13:36:08 +0000 | [diff] [blame] | 342 | // Does "(X op Y) op' Z" always equal "(X op' Z) op (Y op' Z)"? |
| 343 | if (RightDistributesOverLeft(TopLevelOpcode, InnerOpcode)) |
| 344 | // Does the instruction have the form "(A op' B) op (C op' B)" or, in the |
| 345 | // commutative case, "(A op' B) op (B op' D)"? |
| 346 | if (B == D || (InnerCommutative && B == C)) { |
| 347 | if (B != D) |
| 348 | std::swap(C, D); |
| 349 | // Consider forming "(A op C) op' B". |
| 350 | // If "A op C" simplifies then it can be formed with no cost. |
| 351 | Value *V = SimplifyBinOp(TopLevelOpcode, A, C, TD); |
| 352 | // If "A op C" doesn't simplify then only go on if both of the existing |
| 353 | // operations "A op' B" and "C op' D" will be zapped as no longer used. |
| 354 | if (!V && Op0->hasOneUse() && Op1->hasOneUse()) |
| 355 | V = Builder->CreateBinOp(TopLevelOpcode, A, C, Op0->getName()); |
| 356 | if (V) { |
| 357 | ++NumFactor; |
| 358 | V = Builder->CreateBinOp(InnerOpcode, V, B); |
| 359 | V->takeName(&I); |
| 360 | return V; |
| 361 | } |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 362 | } |
Duncan Sands | 37bf92b | 2010-12-22 13:36:08 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | // Expansion. |
| 366 | if (Op0 && RightDistributesOverLeft(Op0->getOpcode(), TopLevelOpcode)) { |
| 367 | // The instruction has the form "(A op' B) op C". See if expanding it out |
| 368 | // to "(A op C) op' (B op C)" results in simplifications. |
| 369 | Value *A = Op0->getOperand(0), *B = Op0->getOperand(1), *C = RHS; |
| 370 | Instruction::BinaryOps InnerOpcode = Op0->getOpcode(); // op' |
| 371 | |
| 372 | // Do "A op C" and "B op C" both simplify? |
| 373 | if (Value *L = SimplifyBinOp(TopLevelOpcode, A, C, TD)) |
| 374 | if (Value *R = SimplifyBinOp(TopLevelOpcode, B, C, TD)) { |
| 375 | // They do! Return "L op' R". |
| 376 | ++NumExpand; |
| 377 | // If "L op' R" equals "A op' B" then "L op' R" is just the LHS. |
| 378 | if ((L == A && R == B) || |
| 379 | (Instruction::isCommutative(InnerOpcode) && L == B && R == A)) |
| 380 | return Op0; |
| 381 | // Otherwise return "L op' R" if it simplifies. |
| 382 | if (Value *V = SimplifyBinOp(InnerOpcode, L, R, TD)) |
| 383 | return V; |
| 384 | // Otherwise, create a new instruction. |
| 385 | C = Builder->CreateBinOp(InnerOpcode, L, R); |
| 386 | C->takeName(&I); |
| 387 | return C; |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | if (Op1 && LeftDistributesOverRight(TopLevelOpcode, Op1->getOpcode())) { |
| 392 | // The instruction has the form "A op (B op' C)". See if expanding it out |
| 393 | // to "(A op B) op' (A op C)" results in simplifications. |
| 394 | Value *A = LHS, *B = Op1->getOperand(0), *C = Op1->getOperand(1); |
| 395 | Instruction::BinaryOps InnerOpcode = Op1->getOpcode(); // op' |
| 396 | |
| 397 | // Do "A op B" and "A op C" both simplify? |
| 398 | if (Value *L = SimplifyBinOp(TopLevelOpcode, A, B, TD)) |
| 399 | if (Value *R = SimplifyBinOp(TopLevelOpcode, A, C, TD)) { |
| 400 | // They do! Return "L op' R". |
| 401 | ++NumExpand; |
| 402 | // If "L op' R" equals "B op' C" then "L op' R" is just the RHS. |
| 403 | if ((L == B && R == C) || |
| 404 | (Instruction::isCommutative(InnerOpcode) && L == C && R == B)) |
| 405 | return Op1; |
| 406 | // Otherwise return "L op' R" if it simplifies. |
| 407 | if (Value *V = SimplifyBinOp(InnerOpcode, L, R, TD)) |
| 408 | return V; |
| 409 | // Otherwise, create a new instruction. |
| 410 | A = Builder->CreateBinOp(InnerOpcode, L, R); |
| 411 | A->takeName(&I); |
| 412 | return A; |
| 413 | } |
| 414 | } |
Duncan Sands | 5057f38 | 2010-11-23 14:23:47 +0000 | [diff] [blame] | 415 | |
| 416 | return 0; |
| 417 | } |
| 418 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 419 | // dyn_castNegVal - Given a 'sub' instruction, return the RHS of the instruction |
| 420 | // if the LHS is a constant zero (which is the 'negate' form). |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 421 | // |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 422 | Value *InstCombiner::dyn_castNegVal(Value *V) const { |
Owen Anderson | fa82b6e | 2009-07-13 22:18:28 +0000 | [diff] [blame] | 423 | if (BinaryOperator::isNeg(V)) |
Chris Lattner | a1df33c | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 424 | return BinaryOperator::getNegArgument(V); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 425 | |
Chris Lattner | 0ce8580 | 2004-12-14 20:08:06 +0000 | [diff] [blame] | 426 | // Constants can be considered to be negated values if they can be folded. |
| 427 | if (ConstantInt *C = dyn_cast<ConstantInt>(V)) |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 428 | return ConstantExpr::getNeg(C); |
Nick Lewycky | 18b3da6 | 2008-05-23 04:54:45 +0000 | [diff] [blame] | 429 | |
| 430 | if (ConstantVector *C = dyn_cast<ConstantVector>(V)) |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 431 | if (C->getType()->getElementType()->isIntegerTy()) |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 432 | return ConstantExpr::getNeg(C); |
Nick Lewycky | 18b3da6 | 2008-05-23 04:54:45 +0000 | [diff] [blame] | 433 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 434 | return 0; |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 437 | // dyn_castFNegVal - Given a 'fsub' instruction, return the RHS of the |
| 438 | // instruction if the LHS is a constant negative zero (which is the 'negate' |
| 439 | // form). |
| 440 | // |
Chris Lattner | d12c27c | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 441 | Value *InstCombiner::dyn_castFNegVal(Value *V) const { |
Owen Anderson | fa82b6e | 2009-07-13 22:18:28 +0000 | [diff] [blame] | 442 | if (BinaryOperator::isFNeg(V)) |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 443 | return BinaryOperator::getFNegArgument(V); |
| 444 | |
| 445 | // Constants can be considered to be negated values if they can be folded. |
| 446 | if (ConstantFP *C = dyn_cast<ConstantFP>(V)) |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 447 | return ConstantExpr::getFNeg(C); |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 448 | |
| 449 | if (ConstantVector *C = dyn_cast<ConstantVector>(V)) |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 450 | if (C->getType()->getElementType()->isFloatingPointTy()) |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 451 | return ConstantExpr::getFNeg(C); |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 452 | |
| 453 | return 0; |
| 454 | } |
| 455 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 456 | static Value *FoldOperationIntoSelectOperand(Instruction &I, Value *SO, |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 457 | InstCombiner *IC) { |
Chris Lattner | 08142f2 | 2009-08-30 19:47:22 +0000 | [diff] [blame] | 458 | if (CastInst *CI = dyn_cast<CastInst>(&I)) |
Chris Lattner | 2345d1d | 2009-08-30 20:01:10 +0000 | [diff] [blame] | 459 | return IC->Builder->CreateCast(CI->getOpcode(), SO, I.getType()); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 460 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 461 | // Figure out if the constant is the left or the right argument. |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 462 | bool ConstIsRHS = isa<Constant>(I.getOperand(1)); |
| 463 | Constant *ConstOperand = cast<Constant>(I.getOperand(ConstIsRHS)); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 464 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 465 | if (Constant *SOC = dyn_cast<Constant>(SO)) { |
| 466 | if (ConstIsRHS) |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 467 | return ConstantExpr::get(I.getOpcode(), SOC, ConstOperand); |
| 468 | return ConstantExpr::get(I.getOpcode(), ConstOperand, SOC); |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | Value *Op0 = SO, *Op1 = ConstOperand; |
| 472 | if (!ConstIsRHS) |
| 473 | std::swap(Op0, Op1); |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 474 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 475 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I)) |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 476 | return IC->Builder->CreateBinOp(BO->getOpcode(), Op0, Op1, |
| 477 | SO->getName()+".op"); |
| 478 | if (ICmpInst *CI = dyn_cast<ICmpInst>(&I)) |
| 479 | return IC->Builder->CreateICmp(CI->getPredicate(), Op0, Op1, |
| 480 | SO->getName()+".cmp"); |
| 481 | if (FCmpInst *CI = dyn_cast<FCmpInst>(&I)) |
| 482 | return IC->Builder->CreateICmp(CI->getPredicate(), Op0, Op1, |
| 483 | SO->getName()+".cmp"); |
| 484 | llvm_unreachable("Unknown binary instruction type!"); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | // FoldOpIntoSelect - Given an instruction with a select as one operand and a |
| 488 | // constant as the other operand, try to fold the binary operator into the |
| 489 | // select arguments. This also works for Cast instructions, which obviously do |
| 490 | // not have a second operand. |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 491 | Instruction *InstCombiner::FoldOpIntoSelect(Instruction &Op, SelectInst *SI) { |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 492 | // Don't modify shared select instructions |
| 493 | if (!SI->hasOneUse()) return 0; |
| 494 | Value *TV = SI->getOperand(1); |
| 495 | Value *FV = SI->getOperand(2); |
| 496 | |
| 497 | if (isa<Constant>(TV) || isa<Constant>(FV)) { |
Chris Lattner | 956db27 | 2005-04-21 05:43:13 +0000 | [diff] [blame] | 498 | // Bool selects with constant operands can be folded to logical ops. |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 499 | if (SI->getType()->isIntegerTy(1)) return 0; |
Chris Lattner | 956db27 | 2005-04-21 05:43:13 +0000 | [diff] [blame] | 500 | |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 501 | Value *SelectTrueVal = FoldOperationIntoSelectOperand(Op, TV, this); |
| 502 | Value *SelectFalseVal = FoldOperationIntoSelectOperand(Op, FV, this); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 503 | |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 504 | return SelectInst::Create(SI->getCondition(), SelectTrueVal, |
| 505 | SelectFalseVal); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 506 | } |
| 507 | return 0; |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 510 | |
Chris Lattner | 5d1704d | 2009-09-27 19:57:57 +0000 | [diff] [blame] | 511 | /// FoldOpIntoPhi - Given a binary operator, cast instruction, or select which |
| 512 | /// has a PHI node as operand #0, see if we can fold the instruction into the |
| 513 | /// PHI (which is only possible if all operands to the PHI are constants). |
Chris Lattner | 213cd61 | 2009-09-27 20:46:36 +0000 | [diff] [blame] | 514 | /// |
| 515 | /// If AllowAggressive is true, FoldOpIntoPhi will allow certain transforms |
| 516 | /// that would normally be unprofitable because they strongly encourage jump |
| 517 | /// threading. |
| 518 | Instruction *InstCombiner::FoldOpIntoPhi(Instruction &I, |
| 519 | bool AllowAggressive) { |
| 520 | AllowAggressive = false; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 521 | PHINode *PN = cast<PHINode>(I.getOperand(0)); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 522 | unsigned NumPHIValues = PN->getNumIncomingValues(); |
Chris Lattner | 5aac832 | 2011-01-16 04:37:29 +0000 | [diff] [blame^] | 523 | if (NumPHIValues == 0) |
Chris Lattner | 213cd61 | 2009-09-27 20:46:36 +0000 | [diff] [blame] | 524 | return 0; |
| 525 | |
Chris Lattner | 5aac832 | 2011-01-16 04:37:29 +0000 | [diff] [blame^] | 526 | // We normally only transform phis with a single use, unless we're trying |
| 527 | // hard to make jump threading happen. |
| 528 | if (!PN->hasOneUse() && !AllowAggressive) |
| 529 | return 0; |
Chris Lattner | 213cd61 | 2009-09-27 20:46:36 +0000 | [diff] [blame] | 530 | |
Chris Lattner | 5d1704d | 2009-09-27 19:57:57 +0000 | [diff] [blame] | 531 | // Check to see if all of the operands of the PHI are simple constants |
| 532 | // (constantint/constantfp/undef). If there is one non-constant value, |
Chris Lattner | c6df8f4 | 2009-09-27 20:18:49 +0000 | [diff] [blame] | 533 | // remember the BB it is in. If there is more than one or if *it* is a PHI, |
| 534 | // bail out. We don't do arbitrary constant expressions here because moving |
| 535 | // their computation can be expensive without a cost model. |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 536 | BasicBlock *NonConstBB = 0; |
Chris Lattner | 5aac832 | 2011-01-16 04:37:29 +0000 | [diff] [blame^] | 537 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
| 538 | Value *InVal = PN->getIncomingValue(i); |
| 539 | if (isa<Constant>(InVal) && !isa<ConstantExpr>(InVal)) |
| 540 | continue; |
| 541 | |
| 542 | if (isa<PHINode>(InVal)) return 0; // Itself a phi. |
| 543 | if (NonConstBB) return 0; // More than one non-const value. |
| 544 | |
| 545 | NonConstBB = PN->getIncomingBlock(i); |
| 546 | |
| 547 | // If the incoming non-constant value is in I's block, we have an infinite |
| 548 | // loop. |
| 549 | if (NonConstBB == I.getParent()) |
| 550 | return 0; |
| 551 | } |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 552 | |
| 553 | // If there is exactly one non-constant value, we can insert a copy of the |
| 554 | // operation in that block. However, if this is a critical edge, we would be |
| 555 | // inserting the computation one some other paths (e.g. inside a loop). Only |
| 556 | // do this if the pred block is unconditionally branching into the phi block. |
Chris Lattner | 213cd61 | 2009-09-27 20:46:36 +0000 | [diff] [blame] | 557 | if (NonConstBB != 0 && !AllowAggressive) { |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 558 | BranchInst *BI = dyn_cast<BranchInst>(NonConstBB->getTerminator()); |
| 559 | if (!BI || !BI->isUnconditional()) return 0; |
| 560 | } |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 561 | |
| 562 | // Okay, we can do the transformation: create the new PHI node. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 563 | PHINode *NewPN = PHINode::Create(I.getType(), ""); |
Chris Lattner | 5551706 | 2005-01-29 00:39:08 +0000 | [diff] [blame] | 564 | NewPN->reserveOperandSpace(PN->getNumOperands()/2); |
Chris Lattner | 857eb57 | 2009-10-21 23:41:58 +0000 | [diff] [blame] | 565 | InsertNewInstBefore(NewPN, *PN); |
| 566 | NewPN->takeName(PN); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 567 | |
| 568 | // Next, add all of the operands to the PHI. |
Chris Lattner | 5d1704d | 2009-09-27 19:57:57 +0000 | [diff] [blame] | 569 | if (SelectInst *SI = dyn_cast<SelectInst>(&I)) { |
| 570 | // We only currently try to fold the condition of a select when it is a phi, |
| 571 | // not the true/false values. |
Chris Lattner | c6df8f4 | 2009-09-27 20:18:49 +0000 | [diff] [blame] | 572 | Value *TrueV = SI->getTrueValue(); |
| 573 | Value *FalseV = SI->getFalseValue(); |
Chris Lattner | 3ddfb21 | 2009-09-28 06:49:44 +0000 | [diff] [blame] | 574 | BasicBlock *PhiTransBB = PN->getParent(); |
Chris Lattner | 5d1704d | 2009-09-27 19:57:57 +0000 | [diff] [blame] | 575 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
Chris Lattner | c6df8f4 | 2009-09-27 20:18:49 +0000 | [diff] [blame] | 576 | BasicBlock *ThisBB = PN->getIncomingBlock(i); |
Chris Lattner | 3ddfb21 | 2009-09-28 06:49:44 +0000 | [diff] [blame] | 577 | Value *TrueVInPred = TrueV->DoPHITranslation(PhiTransBB, ThisBB); |
| 578 | Value *FalseVInPred = FalseV->DoPHITranslation(PhiTransBB, ThisBB); |
Chris Lattner | 5d1704d | 2009-09-27 19:57:57 +0000 | [diff] [blame] | 579 | Value *InV = 0; |
| 580 | if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) { |
Chris Lattner | c6df8f4 | 2009-09-27 20:18:49 +0000 | [diff] [blame] | 581 | InV = InC->isNullValue() ? FalseVInPred : TrueVInPred; |
Chris Lattner | 5d1704d | 2009-09-27 19:57:57 +0000 | [diff] [blame] | 582 | } else { |
| 583 | assert(PN->getIncomingBlock(i) == NonConstBB); |
Chris Lattner | c6df8f4 | 2009-09-27 20:18:49 +0000 | [diff] [blame] | 584 | InV = SelectInst::Create(PN->getIncomingValue(i), TrueVInPred, |
| 585 | FalseVInPred, |
Chris Lattner | 5d1704d | 2009-09-27 19:57:57 +0000 | [diff] [blame] | 586 | "phitmp", NonConstBB->getTerminator()); |
Chris Lattner | 857eb57 | 2009-10-21 23:41:58 +0000 | [diff] [blame] | 587 | Worklist.Add(cast<Instruction>(InV)); |
Chris Lattner | 5d1704d | 2009-09-27 19:57:57 +0000 | [diff] [blame] | 588 | } |
Chris Lattner | c6df8f4 | 2009-09-27 20:18:49 +0000 | [diff] [blame] | 589 | NewPN->addIncoming(InV, ThisBB); |
Chris Lattner | 5d1704d | 2009-09-27 19:57:57 +0000 | [diff] [blame] | 590 | } |
| 591 | } else if (I.getNumOperands() == 2) { |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 592 | Constant *C = cast<Constant>(I.getOperand(1)); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 593 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
Chris Lattner | a9ff5eb | 2007-08-05 08:47:58 +0000 | [diff] [blame] | 594 | Value *InV = 0; |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 595 | if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 596 | if (CmpInst *CI = dyn_cast<CmpInst>(&I)) |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 597 | InV = ConstantExpr::getCompare(CI->getPredicate(), InC, C); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 598 | else |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 599 | InV = ConstantExpr::get(I.getOpcode(), InC, C); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 600 | } else { |
| 601 | assert(PN->getIncomingBlock(i) == NonConstBB); |
| 602 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 603 | InV = BinaryOperator::Create(BO->getOpcode(), |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 604 | PN->getIncomingValue(i), C, "phitmp", |
| 605 | NonConstBB->getTerminator()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 606 | else if (CmpInst *CI = dyn_cast<CmpInst>(&I)) |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 607 | InV = CmpInst::Create(CI->getOpcode(), |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 608 | CI->getPredicate(), |
| 609 | PN->getIncomingValue(i), C, "phitmp", |
| 610 | NonConstBB->getTerminator()); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 611 | else |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 612 | llvm_unreachable("Unknown binop!"); |
Chris Lattner | 857eb57 | 2009-10-21 23:41:58 +0000 | [diff] [blame] | 613 | |
| 614 | Worklist.Add(cast<Instruction>(InV)); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 615 | } |
| 616 | NewPN->addIncoming(InV, PN->getIncomingBlock(i)); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 617 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 618 | } else { |
| 619 | CastInst *CI = cast<CastInst>(&I); |
| 620 | const Type *RetTy = CI->getType(); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 621 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 622 | Value *InV; |
| 623 | if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) { |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 624 | InV = ConstantExpr::getCast(CI->getOpcode(), InC, RetTy); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 625 | } else { |
| 626 | assert(PN->getIncomingBlock(i) == NonConstBB); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 627 | InV = CastInst::Create(CI->getOpcode(), PN->getIncomingValue(i), |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 628 | I.getType(), "phitmp", |
| 629 | NonConstBB->getTerminator()); |
Chris Lattner | 857eb57 | 2009-10-21 23:41:58 +0000 | [diff] [blame] | 630 | Worklist.Add(cast<Instruction>(InV)); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 631 | } |
| 632 | NewPN->addIncoming(InV, PN->getIncomingBlock(i)); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 633 | } |
| 634 | } |
| 635 | return ReplaceInstUsesWith(I, NewPN); |
| 636 | } |
| 637 | |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 638 | /// FindElementAtOffset - Given a type and a constant offset, determine whether |
| 639 | /// or not there is a sequence of GEP indices into the type that will land us at |
Chris Lattner | 3914f72 | 2009-01-24 01:00:13 +0000 | [diff] [blame] | 640 | /// the specified offset. If so, fill them into NewIndices and return the |
| 641 | /// resultant element type, otherwise return null. |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 642 | const Type *InstCombiner::FindElementAtOffset(const Type *Ty, int64_t Offset, |
| 643 | SmallVectorImpl<Value*> &NewIndices) { |
Dan Gohman | ce9fe9f | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 644 | if (!TD) return 0; |
Chris Lattner | 3914f72 | 2009-01-24 01:00:13 +0000 | [diff] [blame] | 645 | if (!Ty->isSized()) return 0; |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 646 | |
| 647 | // Start with the index over the outer type. Note that the type size |
| 648 | // might be zero (even if the offset isn't zero) if the indexed type |
| 649 | // is something like [0 x {int, int}] |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 650 | const Type *IntPtrTy = TD->getIntPtrType(Ty->getContext()); |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 651 | int64_t FirstIdx = 0; |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 652 | if (int64_t TySize = TD->getTypeAllocSize(Ty)) { |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 653 | FirstIdx = Offset/TySize; |
Chris Lattner | 31a69cb | 2009-01-11 20:41:36 +0000 | [diff] [blame] | 654 | Offset -= FirstIdx*TySize; |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 655 | |
Chris Lattner | dbc3bc2 | 2009-01-11 20:15:20 +0000 | [diff] [blame] | 656 | // Handle hosts where % returns negative instead of values [0..TySize). |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 657 | if (Offset < 0) { |
| 658 | --FirstIdx; |
| 659 | Offset += TySize; |
| 660 | assert(Offset >= 0); |
| 661 | } |
| 662 | assert((uint64_t)Offset < (uint64_t)TySize && "Out of range offset"); |
| 663 | } |
| 664 | |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 665 | NewIndices.push_back(ConstantInt::get(IntPtrTy, FirstIdx)); |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 666 | |
| 667 | // Index into the types. If we fail, set OrigBase to null. |
| 668 | while (Offset) { |
Chris Lattner | dbc3bc2 | 2009-01-11 20:15:20 +0000 | [diff] [blame] | 669 | // Indexing into tail padding between struct/array elements. |
| 670 | if (uint64_t(Offset*8) >= TD->getTypeSizeInBits(Ty)) |
Chris Lattner | 3914f72 | 2009-01-24 01:00:13 +0000 | [diff] [blame] | 671 | return 0; |
Chris Lattner | dbc3bc2 | 2009-01-11 20:15:20 +0000 | [diff] [blame] | 672 | |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 673 | if (const StructType *STy = dyn_cast<StructType>(Ty)) { |
| 674 | const StructLayout *SL = TD->getStructLayout(STy); |
Chris Lattner | dbc3bc2 | 2009-01-11 20:15:20 +0000 | [diff] [blame] | 675 | assert(Offset < (int64_t)SL->getSizeInBytes() && |
| 676 | "Offset must stay within the indexed type"); |
| 677 | |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 678 | unsigned Elt = SL->getElementContainingOffset(Offset); |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 679 | NewIndices.push_back(ConstantInt::get(Type::getInt32Ty(Ty->getContext()), |
| 680 | Elt)); |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 681 | |
| 682 | Offset -= SL->getElementOffset(Elt); |
| 683 | Ty = STy->getElementType(Elt); |
Chris Lattner | 1c412d9 | 2009-01-11 20:23:52 +0000 | [diff] [blame] | 684 | } else if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) { |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 685 | uint64_t EltSize = TD->getTypeAllocSize(AT->getElementType()); |
Chris Lattner | dbc3bc2 | 2009-01-11 20:15:20 +0000 | [diff] [blame] | 686 | assert(EltSize && "Cannot index into a zero-sized array"); |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 687 | NewIndices.push_back(ConstantInt::get(IntPtrTy,Offset/EltSize)); |
Chris Lattner | dbc3bc2 | 2009-01-11 20:15:20 +0000 | [diff] [blame] | 688 | Offset %= EltSize; |
Chris Lattner | 1c412d9 | 2009-01-11 20:23:52 +0000 | [diff] [blame] | 689 | Ty = AT->getElementType(); |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 690 | } else { |
Chris Lattner | dbc3bc2 | 2009-01-11 20:15:20 +0000 | [diff] [blame] | 691 | // Otherwise, we can't index into the middle of this atomic type, bail. |
Chris Lattner | 3914f72 | 2009-01-24 01:00:13 +0000 | [diff] [blame] | 692 | return 0; |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 693 | } |
| 694 | } |
| 695 | |
Chris Lattner | 3914f72 | 2009-01-24 01:00:13 +0000 | [diff] [blame] | 696 | return Ty; |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 697 | } |
| 698 | |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 699 | |
Chris Lattner | 473945d | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 700 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 701 | Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 702 | SmallVector<Value*, 8> Ops(GEP.op_begin(), GEP.op_end()); |
| 703 | |
| 704 | if (Value *V = SimplifyGEPInst(&Ops[0], Ops.size(), TD)) |
| 705 | return ReplaceInstUsesWith(GEP, V); |
| 706 | |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 707 | Value *PtrOp = GEP.getOperand(0); |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 708 | |
Duncan Sands | a63395a | 2010-11-22 16:32:50 +0000 | [diff] [blame] | 709 | // Eliminate unneeded casts for indices, and replace indices which displace |
| 710 | // by multiples of a zero size type with zero. |
Chris Lattner | ccf4b34 | 2009-08-30 04:49:01 +0000 | [diff] [blame] | 711 | if (TD) { |
| 712 | bool MadeChange = false; |
Duncan Sands | a63395a | 2010-11-22 16:32:50 +0000 | [diff] [blame] | 713 | const Type *IntPtrTy = TD->getIntPtrType(GEP.getContext()); |
| 714 | |
Chris Lattner | ccf4b34 | 2009-08-30 04:49:01 +0000 | [diff] [blame] | 715 | gep_type_iterator GTI = gep_type_begin(GEP); |
| 716 | for (User::op_iterator I = GEP.op_begin() + 1, E = GEP.op_end(); |
| 717 | I != E; ++I, ++GTI) { |
Duncan Sands | a63395a | 2010-11-22 16:32:50 +0000 | [diff] [blame] | 718 | // Skip indices into struct types. |
| 719 | const SequentialType *SeqTy = dyn_cast<SequentialType>(*GTI); |
| 720 | if (!SeqTy) continue; |
| 721 | |
| 722 | // If the element type has zero size then any index over it is equivalent |
| 723 | // to an index of zero, so replace it with zero if it is not zero already. |
| 724 | if (SeqTy->getElementType()->isSized() && |
| 725 | TD->getTypeAllocSize(SeqTy->getElementType()) == 0) |
| 726 | if (!isa<Constant>(*I) || !cast<Constant>(*I)->isNullValue()) { |
| 727 | *I = Constant::getNullValue(IntPtrTy); |
| 728 | MadeChange = true; |
| 729 | } |
| 730 | |
| 731 | if ((*I)->getType() != IntPtrTy) { |
| 732 | // If we are using a wider index than needed for this platform, shrink |
| 733 | // it to what we need. If narrower, sign-extend it to what we need. |
| 734 | // This explicit cast can make subsequent optimizations more obvious. |
| 735 | *I = Builder->CreateIntCast(*I, IntPtrTy, true); |
| 736 | MadeChange = true; |
| 737 | } |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 738 | } |
Chris Lattner | ccf4b34 | 2009-08-30 04:49:01 +0000 | [diff] [blame] | 739 | if (MadeChange) return &GEP; |
Chris Lattner | db9654e | 2007-03-25 20:43:09 +0000 | [diff] [blame] | 740 | } |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 741 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 742 | // Combine Indices - If the source pointer to this getelementptr instruction |
| 743 | // is a getelementptr instruction, combine the indices of the two |
| 744 | // getelementptr instructions into a single instruction. |
| 745 | // |
Dan Gohman | d6aa02d | 2009-07-28 01:40:03 +0000 | [diff] [blame] | 746 | if (GEPOperator *Src = dyn_cast<GEPOperator>(PtrOp)) { |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 747 | // Note that if our source is a gep chain itself that we wait for that |
| 748 | // chain to be resolved before we perform this transformation. This |
| 749 | // avoids us creating a TON of code in some cases. |
| 750 | // |
Chris Lattner | f9b91bb | 2009-08-30 05:08:50 +0000 | [diff] [blame] | 751 | if (GetElementPtrInst *SrcGEP = |
| 752 | dyn_cast<GetElementPtrInst>(Src->getOperand(0))) |
| 753 | if (SrcGEP->getNumOperands() == 2) |
| 754 | return 0; // Wait until our source is folded to completion. |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 755 | |
Chris Lattner | 72588fc | 2007-02-15 22:48:32 +0000 | [diff] [blame] | 756 | SmallVector<Value*, 8> Indices; |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 757 | |
| 758 | // Find out whether the last index in the source GEP is a sequential idx. |
| 759 | bool EndsWithSequential = false; |
Chris Lattner | ab98484 | 2009-08-30 05:30:55 +0000 | [diff] [blame] | 760 | for (gep_type_iterator I = gep_type_begin(*Src), E = gep_type_end(*Src); |
| 761 | I != E; ++I) |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 762 | EndsWithSequential = !(*I)->isStructTy(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 763 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 764 | // Can we combine the two pointer arithmetics offsets? |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 765 | if (EndsWithSequential) { |
Chris Lattner | decd081 | 2003-03-05 22:33:14 +0000 | [diff] [blame] | 766 | // Replace: gep (gep %P, long B), long A, ... |
| 767 | // With: T = long A+B; gep %P, T, ... |
| 768 | // |
Chris Lattner | f9b91bb | 2009-08-30 05:08:50 +0000 | [diff] [blame] | 769 | Value *Sum; |
| 770 | Value *SO1 = Src->getOperand(Src->getNumOperands()-1); |
| 771 | Value *GO1 = GEP.getOperand(1); |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 772 | if (SO1 == Constant::getNullValue(SO1->getType())) { |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 773 | Sum = GO1; |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 774 | } else if (GO1 == Constant::getNullValue(GO1->getType())) { |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 775 | Sum = SO1; |
| 776 | } else { |
Chris Lattner | ab98484 | 2009-08-30 05:30:55 +0000 | [diff] [blame] | 777 | // If they aren't the same type, then the input hasn't been processed |
| 778 | // by the loop above yet (which canonicalizes sequential index types to |
| 779 | // intptr_t). Just avoid transforming this until the input has been |
| 780 | // normalized. |
| 781 | if (SO1->getType() != GO1->getType()) |
| 782 | return 0; |
Chris Lattner | f925cbd | 2009-08-30 18:50:58 +0000 | [diff] [blame] | 783 | Sum = Builder->CreateAdd(SO1, GO1, PtrOp->getName()+".sum"); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 784 | } |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 785 | |
Chris Lattner | ab98484 | 2009-08-30 05:30:55 +0000 | [diff] [blame] | 786 | // Update the GEP in place if possible. |
Chris Lattner | f9b91bb | 2009-08-30 05:08:50 +0000 | [diff] [blame] | 787 | if (Src->getNumOperands() == 2) { |
| 788 | GEP.setOperand(0, Src->getOperand(0)); |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 789 | GEP.setOperand(1, Sum); |
| 790 | return &GEP; |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 791 | } |
Chris Lattner | ab98484 | 2009-08-30 05:30:55 +0000 | [diff] [blame] | 792 | Indices.append(Src->op_begin()+1, Src->op_end()-1); |
Chris Lattner | ccf4b34 | 2009-08-30 04:49:01 +0000 | [diff] [blame] | 793 | Indices.push_back(Sum); |
Chris Lattner | ab98484 | 2009-08-30 05:30:55 +0000 | [diff] [blame] | 794 | Indices.append(GEP.op_begin()+2, GEP.op_end()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 795 | } else if (isa<Constant>(*GEP.idx_begin()) && |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 796 | cast<Constant>(*GEP.idx_begin())->isNullValue() && |
Chris Lattner | f9b91bb | 2009-08-30 05:08:50 +0000 | [diff] [blame] | 797 | Src->getNumOperands() != 1) { |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 798 | // Otherwise we can do the fold if the first index of the GEP is a zero |
Chris Lattner | ab98484 | 2009-08-30 05:30:55 +0000 | [diff] [blame] | 799 | Indices.append(Src->op_begin()+1, Src->op_end()); |
| 800 | Indices.append(GEP.idx_begin()+1, GEP.idx_end()); |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 801 | } |
| 802 | |
Dan Gohman | f8dbee7 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 803 | if (!Indices.empty()) |
Chris Lattner | 948cdeb | 2010-01-05 07:42:10 +0000 | [diff] [blame] | 804 | return (GEP.isInBounds() && Src->isInBounds()) ? |
Dan Gohman | f8dbee7 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 805 | GetElementPtrInst::CreateInBounds(Src->getOperand(0), Indices.begin(), |
| 806 | Indices.end(), GEP.getName()) : |
Chris Lattner | f9b91bb | 2009-08-30 05:08:50 +0000 | [diff] [blame] | 807 | GetElementPtrInst::Create(Src->getOperand(0), Indices.begin(), |
Chris Lattner | ccf4b34 | 2009-08-30 04:49:01 +0000 | [diff] [blame] | 808 | Indices.end(), GEP.getName()); |
Chris Lattner | 6e24d83 | 2009-08-30 05:00:50 +0000 | [diff] [blame] | 809 | } |
| 810 | |
Chris Lattner | f9b91bb | 2009-08-30 05:08:50 +0000 | [diff] [blame] | 811 | // Handle gep(bitcast x) and gep(gep x, 0, 0, 0). |
Chris Lattner | 948cdeb | 2010-01-05 07:42:10 +0000 | [diff] [blame] | 812 | Value *StrippedPtr = PtrOp->stripPointerCasts(); |
| 813 | if (StrippedPtr != PtrOp) { |
| 814 | const PointerType *StrippedPtrTy =cast<PointerType>(StrippedPtr->getType()); |
Chris Lattner | 963f4ba | 2009-08-30 20:36:46 +0000 | [diff] [blame] | 815 | |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 816 | bool HasZeroPointerIndex = false; |
| 817 | if (ConstantInt *C = dyn_cast<ConstantInt>(GEP.getOperand(1))) |
| 818 | HasZeroPointerIndex = C->isZero(); |
| 819 | |
Chris Lattner | 963f4ba | 2009-08-30 20:36:46 +0000 | [diff] [blame] | 820 | // Transform: GEP (bitcast [10 x i8]* X to [0 x i8]*), i32 0, ... |
| 821 | // into : GEP [10 x i8]* X, i32 0, ... |
| 822 | // |
| 823 | // Likewise, transform: GEP (bitcast i8* X to [0 x i8]*), i32 0, ... |
| 824 | // into : GEP i8* X, ... |
| 825 | // |
| 826 | // This occurs when the program declares an array extern like "int X[];" |
Chris Lattner | 6e24d83 | 2009-08-30 05:00:50 +0000 | [diff] [blame] | 827 | if (HasZeroPointerIndex) { |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 828 | const PointerType *CPTy = cast<PointerType>(PtrOp->getType()); |
Duncan Sands | 5b7cfb0 | 2009-03-02 09:18:21 +0000 | [diff] [blame] | 829 | if (const ArrayType *CATy = |
| 830 | dyn_cast<ArrayType>(CPTy->getElementType())) { |
| 831 | // GEP (bitcast i8* X to [0 x i8]*), i32 0, ... ? |
Chris Lattner | 948cdeb | 2010-01-05 07:42:10 +0000 | [diff] [blame] | 832 | if (CATy->getElementType() == StrippedPtrTy->getElementType()) { |
Duncan Sands | 5b7cfb0 | 2009-03-02 09:18:21 +0000 | [diff] [blame] | 833 | // -> GEP i8* X, ... |
Chris Lattner | 948cdeb | 2010-01-05 07:42:10 +0000 | [diff] [blame] | 834 | SmallVector<Value*, 8> Idx(GEP.idx_begin()+1, GEP.idx_end()); |
| 835 | GetElementPtrInst *Res = |
| 836 | GetElementPtrInst::Create(StrippedPtr, Idx.begin(), |
| 837 | Idx.end(), GEP.getName()); |
| 838 | Res->setIsInBounds(GEP.isInBounds()); |
| 839 | return Res; |
Chris Lattner | 963f4ba | 2009-08-30 20:36:46 +0000 | [diff] [blame] | 840 | } |
| 841 | |
Chris Lattner | 948cdeb | 2010-01-05 07:42:10 +0000 | [diff] [blame] | 842 | if (const ArrayType *XATy = |
| 843 | dyn_cast<ArrayType>(StrippedPtrTy->getElementType())){ |
Duncan Sands | 5b7cfb0 | 2009-03-02 09:18:21 +0000 | [diff] [blame] | 844 | // GEP (bitcast [10 x i8]* X to [0 x i8]*), i32 0, ... ? |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 845 | if (CATy->getElementType() == XATy->getElementType()) { |
Duncan Sands | 5b7cfb0 | 2009-03-02 09:18:21 +0000 | [diff] [blame] | 846 | // -> GEP [10 x i8]* X, i32 0, ... |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 847 | // At this point, we know that the cast source type is a pointer |
| 848 | // to an array of the same type as the destination pointer |
| 849 | // array. Because the array type is never stepped over (there |
| 850 | // is a leading zero) we can fold the cast into this GEP. |
Chris Lattner | 948cdeb | 2010-01-05 07:42:10 +0000 | [diff] [blame] | 851 | GEP.setOperand(0, StrippedPtr); |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 852 | return &GEP; |
| 853 | } |
Duncan Sands | 5b7cfb0 | 2009-03-02 09:18:21 +0000 | [diff] [blame] | 854 | } |
| 855 | } |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 856 | } else if (GEP.getNumOperands() == 2) { |
| 857 | // Transform things like: |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 858 | // %t = getelementptr i32* bitcast ([2 x i32]* %str to i32*), i32 %V |
| 859 | // into: %t1 = getelementptr [2 x i32]* %str, i32 0, i32 %V; bitcast |
Chris Lattner | 948cdeb | 2010-01-05 07:42:10 +0000 | [diff] [blame] | 860 | const Type *SrcElTy = StrippedPtrTy->getElementType(); |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 861 | const Type *ResElTy=cast<PointerType>(PtrOp->getType())->getElementType(); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 862 | if (TD && SrcElTy->isArrayTy() && |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 863 | TD->getTypeAllocSize(cast<ArrayType>(SrcElTy)->getElementType()) == |
| 864 | TD->getTypeAllocSize(ResElTy)) { |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 865 | Value *Idx[2]; |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 866 | Idx[0] = Constant::getNullValue(Type::getInt32Ty(GEP.getContext())); |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 867 | Idx[1] = GEP.getOperand(1); |
Chris Lattner | 948cdeb | 2010-01-05 07:42:10 +0000 | [diff] [blame] | 868 | Value *NewGEP = GEP.isInBounds() ? |
| 869 | Builder->CreateInBoundsGEP(StrippedPtr, Idx, Idx + 2, GEP.getName()) : |
| 870 | Builder->CreateGEP(StrippedPtr, Idx, Idx + 2, GEP.getName()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 871 | // V and GEP are both pointer types --> BitCast |
Chris Lattner | f925cbd | 2009-08-30 18:50:58 +0000 | [diff] [blame] | 872 | return new BitCastInst(NewGEP, GEP.getType()); |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 873 | } |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 874 | |
| 875 | // Transform things like: |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 876 | // getelementptr i8* bitcast ([100 x double]* X to i8*), i32 %tmp |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 877 | // (where tmp = 8*tmp2) into: |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 878 | // getelementptr [100 x double]* %arr, i32 0, i32 %tmp2; bitcast |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 879 | |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 880 | if (TD && SrcElTy->isArrayTy() && ResElTy->isIntegerTy(8)) { |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 881 | uint64_t ArrayEltSize = |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 882 | TD->getTypeAllocSize(cast<ArrayType>(SrcElTy)->getElementType()); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 883 | |
| 884 | // Check to see if "tmp" is a scale by a multiple of ArrayEltSize. We |
| 885 | // allow either a mul, shift, or constant here. |
| 886 | Value *NewIdx = 0; |
| 887 | ConstantInt *Scale = 0; |
| 888 | if (ArrayEltSize == 1) { |
| 889 | NewIdx = GEP.getOperand(1); |
Chris Lattner | ab98484 | 2009-08-30 05:30:55 +0000 | [diff] [blame] | 890 | Scale = ConstantInt::get(cast<IntegerType>(NewIdx->getType()), 1); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 891 | } else if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP.getOperand(1))) { |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 892 | NewIdx = ConstantInt::get(CI->getType(), 1); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 893 | Scale = CI; |
| 894 | } else if (Instruction *Inst =dyn_cast<Instruction>(GEP.getOperand(1))){ |
| 895 | if (Inst->getOpcode() == Instruction::Shl && |
| 896 | isa<ConstantInt>(Inst->getOperand(1))) { |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 897 | ConstantInt *ShAmt = cast<ConstantInt>(Inst->getOperand(1)); |
| 898 | uint32_t ShAmtVal = ShAmt->getLimitedValue(64); |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 899 | Scale = ConstantInt::get(cast<IntegerType>(Inst->getType()), |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 900 | 1ULL << ShAmtVal); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 901 | NewIdx = Inst->getOperand(0); |
| 902 | } else if (Inst->getOpcode() == Instruction::Mul && |
| 903 | isa<ConstantInt>(Inst->getOperand(1))) { |
| 904 | Scale = cast<ConstantInt>(Inst->getOperand(1)); |
| 905 | NewIdx = Inst->getOperand(0); |
| 906 | } |
| 907 | } |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 908 | |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 909 | // If the index will be to exactly the right offset with the scale taken |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 910 | // out, perform the transformation. Note, we don't know whether Scale is |
| 911 | // signed or not. We'll use unsigned version of division/modulo |
| 912 | // operation after making sure Scale doesn't have the sign bit set. |
Chris Lattner | 58b1ac7 | 2009-02-25 18:20:01 +0000 | [diff] [blame] | 913 | if (ArrayEltSize && Scale && Scale->getSExtValue() >= 0LL && |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 914 | Scale->getZExtValue() % ArrayEltSize == 0) { |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 915 | Scale = ConstantInt::get(Scale->getType(), |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 916 | Scale->getZExtValue() / ArrayEltSize); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 917 | if (Scale->getZExtValue() != 1) { |
Chris Lattner | 878daed | 2009-08-30 05:56:44 +0000 | [diff] [blame] | 918 | Constant *C = ConstantExpr::getIntegerCast(Scale, NewIdx->getType(), |
| 919 | false /*ZExt*/); |
Chris Lattner | f925cbd | 2009-08-30 18:50:58 +0000 | [diff] [blame] | 920 | NewIdx = Builder->CreateMul(NewIdx, C, "idxscale"); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | // Insert the new GEP instruction. |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 924 | Value *Idx[2]; |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 925 | Idx[0] = Constant::getNullValue(Type::getInt32Ty(GEP.getContext())); |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 926 | Idx[1] = NewIdx; |
Chris Lattner | 948cdeb | 2010-01-05 07:42:10 +0000 | [diff] [blame] | 927 | Value *NewGEP = GEP.isInBounds() ? |
| 928 | Builder->CreateInBoundsGEP(StrippedPtr, Idx, Idx + 2,GEP.getName()): |
| 929 | Builder->CreateGEP(StrippedPtr, Idx, Idx + 2, GEP.getName()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 930 | // The NewGEP must be pointer typed, so must the old one -> BitCast |
| 931 | return new BitCastInst(NewGEP, GEP.getType()); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 932 | } |
| 933 | } |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 934 | } |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 935 | } |
Chris Lattner | 5840779 | 2009-01-09 04:53:57 +0000 | [diff] [blame] | 936 | |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 937 | /// See if we can simplify: |
Chris Lattner | 873ff01 | 2009-08-30 05:55:36 +0000 | [diff] [blame] | 938 | /// X = bitcast A* to B* |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 939 | /// Y = gep X, <...constant indices...> |
| 940 | /// into a gep of the original struct. This is important for SROA and alias |
| 941 | /// analysis of unions. If "A" is also a bitcast, wait for A/X to be merged. |
Chris Lattner | 5840779 | 2009-01-09 04:53:57 +0000 | [diff] [blame] | 942 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(PtrOp)) { |
Dan Gohman | ce9fe9f | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 943 | if (TD && |
| 944 | !isa<BitCastInst>(BCI->getOperand(0)) && GEP.hasAllConstantIndices()) { |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 945 | // Determine how much the GEP moves the pointer. We are guaranteed to get |
| 946 | // a constant back from EmitGEPOffset. |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 947 | ConstantInt *OffsetV = cast<ConstantInt>(EmitGEPOffset(&GEP)); |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 948 | int64_t Offset = OffsetV->getSExtValue(); |
| 949 | |
| 950 | // If this GEP instruction doesn't move the pointer, just replace the GEP |
| 951 | // with a bitcast of the real input to the dest type. |
| 952 | if (Offset == 0) { |
| 953 | // If the bitcast is of an allocation, and the allocation will be |
| 954 | // converted to match the type of the cast, don't touch this. |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 955 | if (isa<AllocaInst>(BCI->getOperand(0)) || |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 956 | isMalloc(BCI->getOperand(0))) { |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 957 | // See if the bitcast simplifies, if so, don't nuke this GEP yet. |
| 958 | if (Instruction *I = visitBitCast(*BCI)) { |
| 959 | if (I != BCI) { |
| 960 | I->takeName(BCI); |
| 961 | BCI->getParent()->getInstList().insert(BCI, I); |
| 962 | ReplaceInstUsesWith(*BCI, I); |
| 963 | } |
| 964 | return &GEP; |
Chris Lattner | 5840779 | 2009-01-09 04:53:57 +0000 | [diff] [blame] | 965 | } |
Chris Lattner | 5840779 | 2009-01-09 04:53:57 +0000 | [diff] [blame] | 966 | } |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 967 | return new BitCastInst(BCI->getOperand(0), GEP.getType()); |
Chris Lattner | 5840779 | 2009-01-09 04:53:57 +0000 | [diff] [blame] | 968 | } |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 969 | |
| 970 | // Otherwise, if the offset is non-zero, we need to find out if there is a |
| 971 | // field at Offset in 'A's type. If so, we can pull the cast through the |
| 972 | // GEP. |
| 973 | SmallVector<Value*, 8> NewIndices; |
| 974 | const Type *InTy = |
| 975 | cast<PointerType>(BCI->getOperand(0)->getType())->getElementType(); |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 976 | if (FindElementAtOffset(InTy, Offset, NewIndices)) { |
Chris Lattner | 948cdeb | 2010-01-05 07:42:10 +0000 | [diff] [blame] | 977 | Value *NGEP = GEP.isInBounds() ? |
Dan Gohman | f8dbee7 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 978 | Builder->CreateInBoundsGEP(BCI->getOperand(0), NewIndices.begin(), |
| 979 | NewIndices.end()) : |
| 980 | Builder->CreateGEP(BCI->getOperand(0), NewIndices.begin(), |
| 981 | NewIndices.end()); |
Chris Lattner | f925cbd | 2009-08-30 18:50:58 +0000 | [diff] [blame] | 982 | |
| 983 | if (NGEP->getType() == GEP.getType()) |
| 984 | return ReplaceInstUsesWith(GEP, NGEP); |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 985 | NGEP->takeName(&GEP); |
| 986 | return new BitCastInst(NGEP, GEP.getType()); |
| 987 | } |
Chris Lattner | 5840779 | 2009-01-09 04:53:57 +0000 | [diff] [blame] | 988 | } |
| 989 | } |
| 990 | |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 991 | return 0; |
| 992 | } |
| 993 | |
Duncan Sands | 1d9b973 | 2010-05-27 19:09:06 +0000 | [diff] [blame] | 994 | |
| 995 | |
| 996 | static bool IsOnlyNullComparedAndFreed(const Value &V) { |
| 997 | for (Value::const_use_iterator UI = V.use_begin(), UE = V.use_end(); |
| 998 | UI != UE; ++UI) { |
Gabor Greif | fc36c0f | 2010-07-09 15:01:36 +0000 | [diff] [blame] | 999 | const User *U = *UI; |
| 1000 | if (isFreeCall(U)) |
Duncan Sands | 1d9b973 | 2010-05-27 19:09:06 +0000 | [diff] [blame] | 1001 | continue; |
Gabor Greif | fc36c0f | 2010-07-09 15:01:36 +0000 | [diff] [blame] | 1002 | if (const ICmpInst *ICI = dyn_cast<ICmpInst>(U)) |
Duncan Sands | 1d9b973 | 2010-05-27 19:09:06 +0000 | [diff] [blame] | 1003 | if (ICI->isEquality() && isa<ConstantPointerNull>(ICI->getOperand(1))) |
| 1004 | continue; |
| 1005 | return false; |
| 1006 | } |
| 1007 | return true; |
| 1008 | } |
| 1009 | |
| 1010 | Instruction *InstCombiner::visitMalloc(Instruction &MI) { |
| 1011 | // If we have a malloc call which is only used in any amount of comparisons |
| 1012 | // to null and free calls, delete the calls and replace the comparisons with |
| 1013 | // true or false as appropriate. |
| 1014 | if (IsOnlyNullComparedAndFreed(MI)) { |
| 1015 | for (Value::use_iterator UI = MI.use_begin(), UE = MI.use_end(); |
| 1016 | UI != UE;) { |
| 1017 | // We can assume that every remaining use is a free call or an icmp eq/ne |
| 1018 | // to null, so the cast is safe. |
| 1019 | Instruction *I = cast<Instruction>(*UI); |
| 1020 | |
| 1021 | // Early increment here, as we're about to get rid of the user. |
| 1022 | ++UI; |
| 1023 | |
| 1024 | if (isFreeCall(I)) { |
| 1025 | EraseInstFromFunction(*cast<CallInst>(I)); |
| 1026 | continue; |
| 1027 | } |
| 1028 | // Again, the cast is safe. |
| 1029 | ICmpInst *C = cast<ICmpInst>(I); |
| 1030 | ReplaceInstUsesWith(*C, ConstantInt::get(Type::getInt1Ty(C->getContext()), |
| 1031 | C->isFalseWhenEqual())); |
| 1032 | EraseInstFromFunction(*C); |
| 1033 | } |
| 1034 | return EraseInstFromFunction(MI); |
| 1035 | } |
| 1036 | return 0; |
| 1037 | } |
| 1038 | |
| 1039 | |
| 1040 | |
Gabor Greif | 9169737 | 2010-06-24 12:21:15 +0000 | [diff] [blame] | 1041 | Instruction *InstCombiner::visitFree(CallInst &FI) { |
| 1042 | Value *Op = FI.getArgOperand(0); |
Victor Hernandez | 66284e0 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 1043 | |
| 1044 | // free undef -> unreachable. |
| 1045 | if (isa<UndefValue>(Op)) { |
| 1046 | // Insert a new store to null because we cannot modify the CFG here. |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 1047 | new StoreInst(ConstantInt::getTrue(FI.getContext()), |
| 1048 | UndefValue::get(Type::getInt1PtrTy(FI.getContext())), &FI); |
Victor Hernandez | 66284e0 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 1049 | return EraseInstFromFunction(FI); |
| 1050 | } |
| 1051 | |
| 1052 | // If we have 'free null' delete the instruction. This can happen in stl code |
| 1053 | // when lots of inlining happens. |
| 1054 | if (isa<ConstantPointerNull>(Op)) |
| 1055 | return EraseInstFromFunction(FI); |
| 1056 | |
Victor Hernandez | 66284e0 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 1057 | return 0; |
| 1058 | } |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 1059 | |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 1060 | |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 1061 | |
Chris Lattner | c4d10eb | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 1062 | Instruction *InstCombiner::visitBranchInst(BranchInst &BI) { |
| 1063 | // Change br (not X), label True, label False to: br X, label False, True |
Reid Spencer | 4b828e6 | 2005-06-18 17:37:34 +0000 | [diff] [blame] | 1064 | Value *X = 0; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1065 | BasicBlock *TrueDest; |
| 1066 | BasicBlock *FalseDest; |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 1067 | if (match(&BI, m_Br(m_Not(m_Value(X)), TrueDest, FalseDest)) && |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1068 | !isa<Constant>(X)) { |
| 1069 | // Swap Destinations and condition... |
| 1070 | BI.setCondition(X); |
| 1071 | BI.setSuccessor(0, FalseDest); |
| 1072 | BI.setSuccessor(1, TrueDest); |
| 1073 | return &BI; |
| 1074 | } |
| 1075 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1076 | // Cannonicalize fcmp_one -> fcmp_oeq |
| 1077 | FCmpInst::Predicate FPred; Value *Y; |
| 1078 | if (match(&BI, m_Br(m_FCmp(FPred, m_Value(X), m_Value(Y)), |
Chris Lattner | 7a1e924 | 2009-08-30 06:13:40 +0000 | [diff] [blame] | 1079 | TrueDest, FalseDest)) && |
| 1080 | BI.getCondition()->hasOneUse()) |
| 1081 | if (FPred == FCmpInst::FCMP_ONE || FPred == FCmpInst::FCMP_OLE || |
| 1082 | FPred == FCmpInst::FCMP_OGE) { |
| 1083 | FCmpInst *Cond = cast<FCmpInst>(BI.getCondition()); |
| 1084 | Cond->setPredicate(FCmpInst::getInversePredicate(FPred)); |
| 1085 | |
| 1086 | // Swap Destinations and condition. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1087 | BI.setSuccessor(0, FalseDest); |
| 1088 | BI.setSuccessor(1, TrueDest); |
Chris Lattner | 7a1e924 | 2009-08-30 06:13:40 +0000 | [diff] [blame] | 1089 | Worklist.Add(Cond); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1090 | return &BI; |
| 1091 | } |
| 1092 | |
| 1093 | // Cannonicalize icmp_ne -> icmp_eq |
| 1094 | ICmpInst::Predicate IPred; |
| 1095 | if (match(&BI, m_Br(m_ICmp(IPred, m_Value(X), m_Value(Y)), |
Chris Lattner | 7a1e924 | 2009-08-30 06:13:40 +0000 | [diff] [blame] | 1096 | TrueDest, FalseDest)) && |
| 1097 | BI.getCondition()->hasOneUse()) |
| 1098 | if (IPred == ICmpInst::ICMP_NE || IPred == ICmpInst::ICMP_ULE || |
| 1099 | IPred == ICmpInst::ICMP_SLE || IPred == ICmpInst::ICMP_UGE || |
| 1100 | IPred == ICmpInst::ICMP_SGE) { |
| 1101 | ICmpInst *Cond = cast<ICmpInst>(BI.getCondition()); |
| 1102 | Cond->setPredicate(ICmpInst::getInversePredicate(IPred)); |
| 1103 | // Swap Destinations and condition. |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 1104 | BI.setSuccessor(0, FalseDest); |
| 1105 | BI.setSuccessor(1, TrueDest); |
Chris Lattner | 7a1e924 | 2009-08-30 06:13:40 +0000 | [diff] [blame] | 1106 | Worklist.Add(Cond); |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 1107 | return &BI; |
| 1108 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1109 | |
Chris Lattner | c4d10eb | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 1110 | return 0; |
| 1111 | } |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 1112 | |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 1113 | Instruction *InstCombiner::visitSwitchInst(SwitchInst &SI) { |
| 1114 | Value *Cond = SI.getCondition(); |
| 1115 | if (Instruction *I = dyn_cast<Instruction>(Cond)) { |
| 1116 | if (I->getOpcode() == Instruction::Add) |
| 1117 | if (ConstantInt *AddRHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 1118 | // change 'switch (X+4) case 1:' into 'switch (X) case -3' |
| 1119 | for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2) |
Owen Anderson | d672ecb | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 1120 | SI.setOperand(i, |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1121 | ConstantExpr::getSub(cast<Constant>(SI.getOperand(i)), |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 1122 | AddRHS)); |
| 1123 | SI.setOperand(0, I->getOperand(0)); |
Chris Lattner | 7a1e924 | 2009-08-30 06:13:40 +0000 | [diff] [blame] | 1124 | Worklist.Add(I); |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 1125 | return &SI; |
| 1126 | } |
| 1127 | } |
| 1128 | return 0; |
| 1129 | } |
| 1130 | |
Matthijs Kooijman | a9012ec | 2008-06-11 14:05:05 +0000 | [diff] [blame] | 1131 | Instruction *InstCombiner::visitExtractValueInst(ExtractValueInst &EV) { |
Matthijs Kooijman | 780ae5e | 2008-07-16 12:55:45 +0000 | [diff] [blame] | 1132 | Value *Agg = EV.getAggregateOperand(); |
Matthijs Kooijman | a9012ec | 2008-06-11 14:05:05 +0000 | [diff] [blame] | 1133 | |
Matthijs Kooijman | 780ae5e | 2008-07-16 12:55:45 +0000 | [diff] [blame] | 1134 | if (!EV.hasIndices()) |
| 1135 | return ReplaceInstUsesWith(EV, Agg); |
| 1136 | |
| 1137 | if (Constant *C = dyn_cast<Constant>(Agg)) { |
| 1138 | if (isa<UndefValue>(C)) |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 1139 | return ReplaceInstUsesWith(EV, UndefValue::get(EV.getType())); |
Matthijs Kooijman | 780ae5e | 2008-07-16 12:55:45 +0000 | [diff] [blame] | 1140 | |
| 1141 | if (isa<ConstantAggregateZero>(C)) |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1142 | return ReplaceInstUsesWith(EV, Constant::getNullValue(EV.getType())); |
Matthijs Kooijman | 780ae5e | 2008-07-16 12:55:45 +0000 | [diff] [blame] | 1143 | |
| 1144 | if (isa<ConstantArray>(C) || isa<ConstantStruct>(C)) { |
| 1145 | // Extract the element indexed by the first index out of the constant |
| 1146 | Value *V = C->getOperand(*EV.idx_begin()); |
| 1147 | if (EV.getNumIndices() > 1) |
| 1148 | // Extract the remaining indices out of the constant indexed by the |
| 1149 | // first index |
| 1150 | return ExtractValueInst::Create(V, EV.idx_begin() + 1, EV.idx_end()); |
| 1151 | else |
| 1152 | return ReplaceInstUsesWith(EV, V); |
| 1153 | } |
| 1154 | return 0; // Can't handle other constants |
| 1155 | } |
| 1156 | if (InsertValueInst *IV = dyn_cast<InsertValueInst>(Agg)) { |
| 1157 | // We're extracting from an insertvalue instruction, compare the indices |
| 1158 | const unsigned *exti, *exte, *insi, *inse; |
| 1159 | for (exti = EV.idx_begin(), insi = IV->idx_begin(), |
| 1160 | exte = EV.idx_end(), inse = IV->idx_end(); |
| 1161 | exti != exte && insi != inse; |
| 1162 | ++exti, ++insi) { |
| 1163 | if (*insi != *exti) |
| 1164 | // The insert and extract both reference distinctly different elements. |
| 1165 | // This means the extract is not influenced by the insert, and we can |
| 1166 | // replace the aggregate operand of the extract with the aggregate |
| 1167 | // operand of the insert. i.e., replace |
| 1168 | // %I = insertvalue { i32, { i32 } } %A, { i32 } { i32 42 }, 1 |
| 1169 | // %E = extractvalue { i32, { i32 } } %I, 0 |
| 1170 | // with |
| 1171 | // %E = extractvalue { i32, { i32 } } %A, 0 |
| 1172 | return ExtractValueInst::Create(IV->getAggregateOperand(), |
| 1173 | EV.idx_begin(), EV.idx_end()); |
| 1174 | } |
| 1175 | if (exti == exte && insi == inse) |
| 1176 | // Both iterators are at the end: Index lists are identical. Replace |
| 1177 | // %B = insertvalue { i32, { i32 } } %A, i32 42, 1, 0 |
| 1178 | // %C = extractvalue { i32, { i32 } } %B, 1, 0 |
| 1179 | // with "i32 42" |
| 1180 | return ReplaceInstUsesWith(EV, IV->getInsertedValueOperand()); |
| 1181 | if (exti == exte) { |
| 1182 | // The extract list is a prefix of the insert list. i.e. replace |
| 1183 | // %I = insertvalue { i32, { i32 } } %A, i32 42, 1, 0 |
| 1184 | // %E = extractvalue { i32, { i32 } } %I, 1 |
| 1185 | // with |
| 1186 | // %X = extractvalue { i32, { i32 } } %A, 1 |
| 1187 | // %E = insertvalue { i32 } %X, i32 42, 0 |
| 1188 | // by switching the order of the insert and extract (though the |
| 1189 | // insertvalue should be left in, since it may have other uses). |
Chris Lattner | f925cbd | 2009-08-30 18:50:58 +0000 | [diff] [blame] | 1190 | Value *NewEV = Builder->CreateExtractValue(IV->getAggregateOperand(), |
| 1191 | EV.idx_begin(), EV.idx_end()); |
Matthijs Kooijman | 780ae5e | 2008-07-16 12:55:45 +0000 | [diff] [blame] | 1192 | return InsertValueInst::Create(NewEV, IV->getInsertedValueOperand(), |
| 1193 | insi, inse); |
| 1194 | } |
| 1195 | if (insi == inse) |
| 1196 | // The insert list is a prefix of the extract list |
| 1197 | // We can simply remove the common indices from the extract and make it |
| 1198 | // operate on the inserted value instead of the insertvalue result. |
| 1199 | // i.e., replace |
| 1200 | // %I = insertvalue { i32, { i32 } } %A, { i32 } { i32 42 }, 1 |
| 1201 | // %E = extractvalue { i32, { i32 } } %I, 1, 0 |
| 1202 | // with |
| 1203 | // %E extractvalue { i32 } { i32 42 }, 0 |
| 1204 | return ExtractValueInst::Create(IV->getInsertedValueOperand(), |
| 1205 | exti, exte); |
| 1206 | } |
Chris Lattner | 7e606e2 | 2009-11-09 07:07:56 +0000 | [diff] [blame] | 1207 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Agg)) { |
| 1208 | // We're extracting from an intrinsic, see if we're the only user, which |
| 1209 | // allows us to simplify multiple result intrinsics to simpler things that |
Gabor Greif | 9169737 | 2010-06-24 12:21:15 +0000 | [diff] [blame] | 1210 | // just get one value. |
Chris Lattner | 7e606e2 | 2009-11-09 07:07:56 +0000 | [diff] [blame] | 1211 | if (II->hasOneUse()) { |
| 1212 | // Check if we're grabbing the overflow bit or the result of a 'with |
| 1213 | // overflow' intrinsic. If it's the latter we can remove the intrinsic |
| 1214 | // and replace it with a traditional binary instruction. |
| 1215 | switch (II->getIntrinsicID()) { |
| 1216 | case Intrinsic::uadd_with_overflow: |
| 1217 | case Intrinsic::sadd_with_overflow: |
| 1218 | if (*EV.idx_begin() == 0) { // Normal result. |
Gabor Greif | 9169737 | 2010-06-24 12:21:15 +0000 | [diff] [blame] | 1219 | Value *LHS = II->getArgOperand(0), *RHS = II->getArgOperand(1); |
Chris Lattner | 7e606e2 | 2009-11-09 07:07:56 +0000 | [diff] [blame] | 1220 | II->replaceAllUsesWith(UndefValue::get(II->getType())); |
| 1221 | EraseInstFromFunction(*II); |
| 1222 | return BinaryOperator::CreateAdd(LHS, RHS); |
| 1223 | } |
Chris Lattner | 74b6461 | 2010-12-19 19:43:52 +0000 | [diff] [blame] | 1224 | |
| 1225 | // If the normal result of the add is dead, and the RHS is a constant, |
| 1226 | // we can transform this into a range comparison. |
| 1227 | // overflow = uadd a, -4 --> overflow = icmp ugt a, 3 |
Chris Lattner | f2a97ed | 2010-12-19 23:24:04 +0000 | [diff] [blame] | 1228 | if (II->getIntrinsicID() == Intrinsic::uadd_with_overflow) |
| 1229 | if (ConstantInt *CI = dyn_cast<ConstantInt>(II->getArgOperand(1))) |
| 1230 | return new ICmpInst(ICmpInst::ICMP_UGT, II->getArgOperand(0), |
| 1231 | ConstantExpr::getNot(CI)); |
Chris Lattner | 7e606e2 | 2009-11-09 07:07:56 +0000 | [diff] [blame] | 1232 | break; |
| 1233 | case Intrinsic::usub_with_overflow: |
| 1234 | case Intrinsic::ssub_with_overflow: |
| 1235 | if (*EV.idx_begin() == 0) { // Normal result. |
Gabor Greif | 9169737 | 2010-06-24 12:21:15 +0000 | [diff] [blame] | 1236 | Value *LHS = II->getArgOperand(0), *RHS = II->getArgOperand(1); |
Chris Lattner | 7e606e2 | 2009-11-09 07:07:56 +0000 | [diff] [blame] | 1237 | II->replaceAllUsesWith(UndefValue::get(II->getType())); |
| 1238 | EraseInstFromFunction(*II); |
| 1239 | return BinaryOperator::CreateSub(LHS, RHS); |
| 1240 | } |
| 1241 | break; |
| 1242 | case Intrinsic::umul_with_overflow: |
| 1243 | case Intrinsic::smul_with_overflow: |
| 1244 | if (*EV.idx_begin() == 0) { // Normal result. |
Gabor Greif | 9169737 | 2010-06-24 12:21:15 +0000 | [diff] [blame] | 1245 | Value *LHS = II->getArgOperand(0), *RHS = II->getArgOperand(1); |
Chris Lattner | 7e606e2 | 2009-11-09 07:07:56 +0000 | [diff] [blame] | 1246 | II->replaceAllUsesWith(UndefValue::get(II->getType())); |
| 1247 | EraseInstFromFunction(*II); |
| 1248 | return BinaryOperator::CreateMul(LHS, RHS); |
| 1249 | } |
| 1250 | break; |
| 1251 | default: |
| 1252 | break; |
| 1253 | } |
| 1254 | } |
| 1255 | } |
Frits van Bommel | 34ceb4d | 2010-11-29 21:56:20 +0000 | [diff] [blame] | 1256 | if (LoadInst *L = dyn_cast<LoadInst>(Agg)) |
| 1257 | // If the (non-volatile) load only has one use, we can rewrite this to a |
| 1258 | // load from a GEP. This reduces the size of the load. |
| 1259 | // FIXME: If a load is used only by extractvalue instructions then this |
| 1260 | // could be done regardless of having multiple uses. |
| 1261 | if (!L->isVolatile() && L->hasOneUse()) { |
| 1262 | // extractvalue has integer indices, getelementptr has Value*s. Convert. |
| 1263 | SmallVector<Value*, 4> Indices; |
| 1264 | // Prefix an i32 0 since we need the first element. |
| 1265 | Indices.push_back(Builder->getInt32(0)); |
| 1266 | for (ExtractValueInst::idx_iterator I = EV.idx_begin(), E = EV.idx_end(); |
| 1267 | I != E; ++I) |
| 1268 | Indices.push_back(Builder->getInt32(*I)); |
| 1269 | |
| 1270 | // We need to insert these at the location of the old load, not at that of |
| 1271 | // the extractvalue. |
| 1272 | Builder->SetInsertPoint(L->getParent(), L); |
| 1273 | Value *GEP = Builder->CreateInBoundsGEP(L->getPointerOperand(), |
| 1274 | Indices.begin(), Indices.end()); |
| 1275 | // Returning the load directly will cause the main loop to insert it in |
| 1276 | // the wrong spot, so use ReplaceInstUsesWith(). |
| 1277 | return ReplaceInstUsesWith(EV, Builder->CreateLoad(GEP)); |
| 1278 | } |
| 1279 | // We could simplify extracts from other values. Note that nested extracts may |
| 1280 | // already be simplified implicitly by the above: extract (extract (insert) ) |
Matthijs Kooijman | 780ae5e | 2008-07-16 12:55:45 +0000 | [diff] [blame] | 1281 | // will be translated into extract ( insert ( extract ) ) first and then just |
Frits van Bommel | 34ceb4d | 2010-11-29 21:56:20 +0000 | [diff] [blame] | 1282 | // the value inserted, if appropriate. Similarly for extracts from single-use |
| 1283 | // loads: extract (extract (load)) will be translated to extract (load (gep)) |
| 1284 | // and if again single-use then via load (gep (gep)) to load (gep). |
| 1285 | // However, double extracts from e.g. function arguments or return values |
| 1286 | // aren't handled yet. |
Matthijs Kooijman | a9012ec | 2008-06-11 14:05:05 +0000 | [diff] [blame] | 1287 | return 0; |
| 1288 | } |
| 1289 | |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 1290 | |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 1291 | |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 1292 | |
| 1293 | /// TryToSinkInstruction - Try to move the specified instruction from its |
| 1294 | /// current block into the beginning of DestBlock, which can only happen if it's |
| 1295 | /// safe to move the instruction past all of the instructions between it and the |
| 1296 | /// end of its block. |
| 1297 | static bool TryToSinkInstruction(Instruction *I, BasicBlock *DestBlock) { |
| 1298 | assert(I->hasOneUse() && "Invariants didn't hold!"); |
| 1299 | |
Chris Lattner | 108e902 | 2005-10-27 17:13:11 +0000 | [diff] [blame] | 1300 | // Cannot move control-flow-involving, volatile loads, vaarg, etc. |
Duncan Sands | 7af1c78 | 2009-05-06 06:49:50 +0000 | [diff] [blame] | 1301 | if (isa<PHINode>(I) || I->mayHaveSideEffects() || isa<TerminatorInst>(I)) |
Chris Lattner | bfc538c | 2008-05-09 15:07:33 +0000 | [diff] [blame] | 1302 | return false; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1303 | |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 1304 | // Do not sink alloca instructions out of the entry block. |
Dan Gohman | ecb7a77 | 2007-03-22 16:38:57 +0000 | [diff] [blame] | 1305 | if (isa<AllocaInst>(I) && I->getParent() == |
| 1306 | &DestBlock->getParent()->getEntryBlock()) |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 1307 | return false; |
| 1308 | |
Chris Lattner | 96a52a6 | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 1309 | // We can only sink load instructions if there is nothing between the load and |
| 1310 | // the end of block that could change the value. |
Chris Lattner | 2539e33 | 2008-05-08 17:37:37 +0000 | [diff] [blame] | 1311 | if (I->mayReadFromMemory()) { |
| 1312 | for (BasicBlock::iterator Scan = I, E = I->getParent()->end(); |
Chris Lattner | 96a52a6 | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 1313 | Scan != E; ++Scan) |
| 1314 | if (Scan->mayWriteToMemory()) |
| 1315 | return false; |
Chris Lattner | 96a52a6 | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 1316 | } |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 1317 | |
Dan Gohman | 02dea8b | 2008-05-23 21:05:58 +0000 | [diff] [blame] | 1318 | BasicBlock::iterator InsertPos = DestBlock->getFirstNonPHI(); |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 1319 | |
Chris Lattner | 4bc5f80 | 2005-08-08 19:11:57 +0000 | [diff] [blame] | 1320 | I->moveBefore(InsertPos); |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 1321 | ++NumSunkInst; |
| 1322 | return true; |
| 1323 | } |
| 1324 | |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 1325 | |
| 1326 | /// AddReachableCodeToWorklist - Walk the function in depth-first order, adding |
| 1327 | /// all reachable code to the worklist. |
| 1328 | /// |
| 1329 | /// This has a couple of tricks to make the code faster and more powerful. In |
| 1330 | /// particular, we constant fold and DCE instructions as we go, to avoid adding |
| 1331 | /// them to the worklist (this significantly speeds up instcombine on code where |
| 1332 | /// many instructions are dead or constant). Additionally, if we find a branch |
| 1333 | /// whose condition is a known constant, we only visit the reachable successors. |
| 1334 | /// |
Chris Lattner | 2ee743b | 2009-10-15 04:59:28 +0000 | [diff] [blame] | 1335 | static bool AddReachableCodeToWorklist(BasicBlock *BB, |
Chris Lattner | 1f87a58 | 2007-02-15 19:41:52 +0000 | [diff] [blame] | 1336 | SmallPtrSet<BasicBlock*, 64> &Visited, |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 1337 | InstCombiner &IC, |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 1338 | const TargetData *TD) { |
Chris Lattner | 2ee743b | 2009-10-15 04:59:28 +0000 | [diff] [blame] | 1339 | bool MadeIRChange = false; |
Chris Lattner | 2806dff | 2008-08-15 04:03:01 +0000 | [diff] [blame] | 1340 | SmallVector<BasicBlock*, 256> Worklist; |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 1341 | Worklist.push_back(BB); |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 1342 | |
Benjamin Kramer | a53fe60 | 2010-10-23 17:10:24 +0000 | [diff] [blame] | 1343 | SmallVector<Instruction*, 128> InstrsForInstCombineWorklist; |
Chris Lattner | 2ee743b | 2009-10-15 04:59:28 +0000 | [diff] [blame] | 1344 | SmallPtrSet<ConstantExpr*, 64> FoldedConstants; |
| 1345 | |
Dan Gohman | 321a813 | 2010-01-05 16:27:25 +0000 | [diff] [blame] | 1346 | do { |
| 1347 | BB = Worklist.pop_back_val(); |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 1348 | |
| 1349 | // We have now visited this block! If we've already been here, ignore it. |
| 1350 | if (!Visited.insert(BB)) continue; |
Devang Patel | 7fe1dec | 2008-11-19 18:56:50 +0000 | [diff] [blame] | 1351 | |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 1352 | for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ) { |
| 1353 | Instruction *Inst = BBI++; |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 1354 | |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 1355 | // DCE instruction if trivially dead. |
| 1356 | if (isInstructionTriviallyDead(Inst)) { |
| 1357 | ++NumDeadInst; |
Chris Lattner | bdff548 | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 1358 | DEBUG(errs() << "IC: DCE: " << *Inst << '\n'); |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 1359 | Inst->eraseFromParent(); |
| 1360 | continue; |
| 1361 | } |
| 1362 | |
| 1363 | // ConstantProp instruction if trivially constant. |
Chris Lattner | e2cc1ad | 2009-10-15 04:13:44 +0000 | [diff] [blame] | 1364 | if (!Inst->use_empty() && isa<Constant>(Inst->getOperand(0))) |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1365 | if (Constant *C = ConstantFoldInstruction(Inst, TD)) { |
Chris Lattner | e2cc1ad | 2009-10-15 04:13:44 +0000 | [diff] [blame] | 1366 | DEBUG(errs() << "IC: ConstFold to: " << *C << " from: " |
| 1367 | << *Inst << '\n'); |
| 1368 | Inst->replaceAllUsesWith(C); |
| 1369 | ++NumConstProp; |
| 1370 | Inst->eraseFromParent(); |
| 1371 | continue; |
| 1372 | } |
Chris Lattner | 2ee743b | 2009-10-15 04:59:28 +0000 | [diff] [blame] | 1373 | |
Chris Lattner | 2ee743b | 2009-10-15 04:59:28 +0000 | [diff] [blame] | 1374 | if (TD) { |
| 1375 | // See if we can constant fold its operands. |
| 1376 | for (User::op_iterator i = Inst->op_begin(), e = Inst->op_end(); |
| 1377 | i != e; ++i) { |
| 1378 | ConstantExpr *CE = dyn_cast<ConstantExpr>(i); |
| 1379 | if (CE == 0) continue; |
| 1380 | |
| 1381 | // If we already folded this constant, don't try again. |
| 1382 | if (!FoldedConstants.insert(CE)) |
| 1383 | continue; |
| 1384 | |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1385 | Constant *NewC = ConstantFoldConstantExpression(CE, TD); |
Chris Lattner | 2ee743b | 2009-10-15 04:59:28 +0000 | [diff] [blame] | 1386 | if (NewC && NewC != CE) { |
| 1387 | *i = NewC; |
| 1388 | MadeIRChange = true; |
| 1389 | } |
| 1390 | } |
| 1391 | } |
Devang Patel | 7fe1dec | 2008-11-19 18:56:50 +0000 | [diff] [blame] | 1392 | |
Chris Lattner | 67f7d54 | 2009-10-12 03:58:40 +0000 | [diff] [blame] | 1393 | InstrsForInstCombineWorklist.push_back(Inst); |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 1394 | } |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 1395 | |
| 1396 | // Recursively visit successors. If this is a branch or switch on a |
| 1397 | // constant, only visit the reachable successor. |
| 1398 | TerminatorInst *TI = BB->getTerminator(); |
| 1399 | if (BranchInst *BI = dyn_cast<BranchInst>(TI)) { |
| 1400 | if (BI->isConditional() && isa<ConstantInt>(BI->getCondition())) { |
| 1401 | bool CondVal = cast<ConstantInt>(BI->getCondition())->getZExtValue(); |
Nick Lewycky | 9143699 | 2008-03-09 08:50:23 +0000 | [diff] [blame] | 1402 | BasicBlock *ReachableBB = BI->getSuccessor(!CondVal); |
Nick Lewycky | 280a6e6 | 2008-04-25 16:53:59 +0000 | [diff] [blame] | 1403 | Worklist.push_back(ReachableBB); |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 1404 | continue; |
| 1405 | } |
| 1406 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) { |
| 1407 | if (ConstantInt *Cond = dyn_cast<ConstantInt>(SI->getCondition())) { |
| 1408 | // See if this is an explicit destination. |
| 1409 | for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i) |
| 1410 | if (SI->getCaseValue(i) == Cond) { |
Nick Lewycky | 9143699 | 2008-03-09 08:50:23 +0000 | [diff] [blame] | 1411 | BasicBlock *ReachableBB = SI->getSuccessor(i); |
Nick Lewycky | 280a6e6 | 2008-04-25 16:53:59 +0000 | [diff] [blame] | 1412 | Worklist.push_back(ReachableBB); |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 1413 | continue; |
| 1414 | } |
| 1415 | |
| 1416 | // Otherwise it is the default destination. |
| 1417 | Worklist.push_back(SI->getSuccessor(0)); |
| 1418 | continue; |
| 1419 | } |
| 1420 | } |
| 1421 | |
| 1422 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) |
| 1423 | Worklist.push_back(TI->getSuccessor(i)); |
Dan Gohman | 321a813 | 2010-01-05 16:27:25 +0000 | [diff] [blame] | 1424 | } while (!Worklist.empty()); |
Chris Lattner | 67f7d54 | 2009-10-12 03:58:40 +0000 | [diff] [blame] | 1425 | |
| 1426 | // Once we've found all of the instructions to add to instcombine's worklist, |
| 1427 | // add them in reverse order. This way instcombine will visit from the top |
| 1428 | // of the function down. This jives well with the way that it adds all uses |
| 1429 | // of instructions to the worklist after doing a transformation, thus avoiding |
| 1430 | // some N^2 behavior in pathological cases. |
| 1431 | IC.Worklist.AddInitialGroup(&InstrsForInstCombineWorklist[0], |
| 1432 | InstrsForInstCombineWorklist.size()); |
Chris Lattner | 2ee743b | 2009-10-15 04:59:28 +0000 | [diff] [blame] | 1433 | |
| 1434 | return MadeIRChange; |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 1435 | } |
| 1436 | |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 1437 | bool InstCombiner::DoOneIteration(Function &F, unsigned Iteration) { |
Chris Lattner | b0b822c | 2009-08-31 06:57:37 +0000 | [diff] [blame] | 1438 | MadeIRChange = false; |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 1439 | |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 1440 | DEBUG(errs() << "\n\nINSTCOMBINE ITERATION #" << Iteration << " on " |
| 1441 | << F.getNameStr() << "\n"); |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 1442 | |
Chris Lattner | b3d5970 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 1443 | { |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 1444 | // Do a depth-first traversal of the function, populate the worklist with |
| 1445 | // the reachable instructions. Ignore blocks that are not reachable. Keep |
| 1446 | // track of which blocks we visit. |
Chris Lattner | 1f87a58 | 2007-02-15 19:41:52 +0000 | [diff] [blame] | 1447 | SmallPtrSet<BasicBlock*, 64> Visited; |
Chris Lattner | 2ee743b | 2009-10-15 04:59:28 +0000 | [diff] [blame] | 1448 | MadeIRChange |= AddReachableCodeToWorklist(F.begin(), Visited, *this, TD); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 1449 | |
Chris Lattner | b3d5970 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 1450 | // Do a quick scan over the function. If we find any blocks that are |
| 1451 | // unreachable, remove any instructions inside of them. This prevents |
| 1452 | // the instcombine code from having to deal with some bad special cases. |
| 1453 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 1454 | if (!Visited.count(BB)) { |
| 1455 | Instruction *Term = BB->getTerminator(); |
| 1456 | while (Term != BB->begin()) { // Remove instrs bottom-up |
| 1457 | BasicBlock::iterator I = Term; --I; |
Chris Lattner | 6ffe551 | 2004-04-27 15:13:33 +0000 | [diff] [blame] | 1458 | |
Chris Lattner | bdff548 | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 1459 | DEBUG(errs() << "IC: DCE: " << *I << '\n'); |
Dale Johannesen | ff278b1 | 2009-03-10 21:19:49 +0000 | [diff] [blame] | 1460 | // A debug intrinsic shouldn't force another iteration if we weren't |
| 1461 | // going to do one without it. |
| 1462 | if (!isa<DbgInfoIntrinsic>(I)) { |
| 1463 | ++NumDeadInst; |
Chris Lattner | b0b822c | 2009-08-31 06:57:37 +0000 | [diff] [blame] | 1464 | MadeIRChange = true; |
Dale Johannesen | ff278b1 | 2009-03-10 21:19:49 +0000 | [diff] [blame] | 1465 | } |
Devang Patel | 228ebd0 | 2009-10-13 22:56:32 +0000 | [diff] [blame] | 1466 | |
Devang Patel | 228ebd0 | 2009-10-13 22:56:32 +0000 | [diff] [blame] | 1467 | // If I is not void type then replaceAllUsesWith undef. |
| 1468 | // This allows ValueHandlers and custom metadata to adjust itself. |
Devang Patel | 9674d15 | 2009-10-14 17:29:00 +0000 | [diff] [blame] | 1469 | if (!I->getType()->isVoidTy()) |
Devang Patel | 228ebd0 | 2009-10-13 22:56:32 +0000 | [diff] [blame] | 1470 | I->replaceAllUsesWith(UndefValue::get(I->getType())); |
Chris Lattner | b3d5970 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 1471 | I->eraseFromParent(); |
| 1472 | } |
| 1473 | } |
| 1474 | } |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 1475 | |
Chris Lattner | 873ff01 | 2009-08-30 05:55:36 +0000 | [diff] [blame] | 1476 | while (!Worklist.isEmpty()) { |
| 1477 | Instruction *I = Worklist.RemoveOne(); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 1478 | if (I == 0) continue; // skip null values. |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 1479 | |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 1480 | // Check to see if we can DCE the instruction. |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 1481 | if (isInstructionTriviallyDead(I)) { |
Chris Lattner | bdff548 | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 1482 | DEBUG(errs() << "IC: DCE: " << *I << '\n'); |
Chris Lattner | 7a1e924 | 2009-08-30 06:13:40 +0000 | [diff] [blame] | 1483 | EraseInstFromFunction(*I); |
| 1484 | ++NumDeadInst; |
Chris Lattner | b0b822c | 2009-08-31 06:57:37 +0000 | [diff] [blame] | 1485 | MadeIRChange = true; |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 1486 | continue; |
| 1487 | } |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 1488 | |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 1489 | // Instruction isn't dead, see if we can constant propagate it. |
Chris Lattner | e2cc1ad | 2009-10-15 04:13:44 +0000 | [diff] [blame] | 1490 | if (!I->use_empty() && isa<Constant>(I->getOperand(0))) |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1491 | if (Constant *C = ConstantFoldInstruction(I, TD)) { |
Chris Lattner | e2cc1ad | 2009-10-15 04:13:44 +0000 | [diff] [blame] | 1492 | DEBUG(errs() << "IC: ConstFold to: " << *C << " from: " << *I << '\n'); |
Chris Lattner | ad5fec1 | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 1493 | |
Chris Lattner | e2cc1ad | 2009-10-15 04:13:44 +0000 | [diff] [blame] | 1494 | // Add operands to the worklist. |
| 1495 | ReplaceInstUsesWith(*I, C); |
| 1496 | ++NumConstProp; |
| 1497 | EraseInstFromFunction(*I); |
| 1498 | MadeIRChange = true; |
| 1499 | continue; |
| 1500 | } |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 1501 | |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 1502 | // See if we can trivially sink this instruction to a successor basic block. |
Dan Gohman | fc74abf | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 1503 | if (I->hasOneUse()) { |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 1504 | BasicBlock *BB = I->getParent(); |
Chris Lattner | 8db2cd1 | 2009-10-14 15:21:58 +0000 | [diff] [blame] | 1505 | Instruction *UserInst = cast<Instruction>(I->use_back()); |
| 1506 | BasicBlock *UserParent; |
| 1507 | |
| 1508 | // Get the block the use occurs in. |
| 1509 | if (PHINode *PN = dyn_cast<PHINode>(UserInst)) |
| 1510 | UserParent = PN->getIncomingBlock(I->use_begin().getUse()); |
| 1511 | else |
| 1512 | UserParent = UserInst->getParent(); |
| 1513 | |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 1514 | if (UserParent != BB) { |
| 1515 | bool UserIsSuccessor = false; |
| 1516 | // See if the user is one of our successors. |
| 1517 | for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI) |
| 1518 | if (*SI == UserParent) { |
| 1519 | UserIsSuccessor = true; |
| 1520 | break; |
| 1521 | } |
| 1522 | |
| 1523 | // If the user is one of our immediate successors, and if that successor |
| 1524 | // only has us as a predecessors (we'd have to split the critical edge |
| 1525 | // otherwise), we can keep going. |
Chris Lattner | 8db2cd1 | 2009-10-14 15:21:58 +0000 | [diff] [blame] | 1526 | if (UserIsSuccessor && UserParent->getSinglePredecessor()) |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 1527 | // Okay, the CFG is simple enough, try to sink this instruction. |
Chris Lattner | b0b822c | 2009-08-31 06:57:37 +0000 | [diff] [blame] | 1528 | MadeIRChange |= TryToSinkInstruction(I, UserParent); |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 1529 | } |
| 1530 | } |
| 1531 | |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 1532 | // Now that we have an instruction, try combining it to simplify it. |
| 1533 | Builder->SetInsertPoint(I->getParent(), I); |
| 1534 | |
Reid Spencer | a9b8101 | 2007-03-26 17:44:01 +0000 | [diff] [blame] | 1535 | #ifndef NDEBUG |
| 1536 | std::string OrigI; |
| 1537 | #endif |
Chris Lattner | bdff548 | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 1538 | DEBUG(raw_string_ostream SS(OrigI); I->print(SS); OrigI = SS.str();); |
Jeffrey Yasskin | 4306963 | 2009-10-08 00:12:24 +0000 | [diff] [blame] | 1539 | DEBUG(errs() << "IC: Visiting: " << OrigI << '\n'); |
| 1540 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 1541 | if (Instruction *Result = visit(*I)) { |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 1542 | ++NumCombined; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1543 | // Should we replace the old instruction with a new one? |
Chris Lattner | b3bc8fa | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 1544 | if (Result != I) { |
Chris Lattner | bdff548 | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 1545 | DEBUG(errs() << "IC: Old = " << *I << '\n' |
| 1546 | << " New = " << *Result << '\n'); |
Chris Lattner | 0cea42a | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 1547 | |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 1548 | // Everything uses the new instruction now. |
| 1549 | I->replaceAllUsesWith(Result); |
| 1550 | |
| 1551 | // Push the new instruction and any users onto the worklist. |
Chris Lattner | 7a1e924 | 2009-08-30 06:13:40 +0000 | [diff] [blame] | 1552 | Worklist.Add(Result); |
Chris Lattner | e5ecdb5 | 2009-08-30 06:22:51 +0000 | [diff] [blame] | 1553 | Worklist.AddUsersToWorkList(*Result); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 1554 | |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 1555 | // Move the name to the new instruction first. |
| 1556 | Result->takeName(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 1557 | |
| 1558 | // Insert the new instruction into the basic block... |
| 1559 | BasicBlock *InstParent = I->getParent(); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 1560 | BasicBlock::iterator InsertPos = I; |
| 1561 | |
| 1562 | if (!isa<PHINode>(Result)) // If combining a PHI, don't insert |
| 1563 | while (isa<PHINode>(InsertPos)) // middle of a block of PHIs. |
| 1564 | ++InsertPos; |
| 1565 | |
| 1566 | InstParent->getInstList().insert(InsertPos, Result); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 1567 | |
Chris Lattner | 7a1e924 | 2009-08-30 06:13:40 +0000 | [diff] [blame] | 1568 | EraseInstFromFunction(*I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1569 | } else { |
Evan Cheng | c7baf68 | 2007-03-27 16:44:48 +0000 | [diff] [blame] | 1570 | #ifndef NDEBUG |
Chris Lattner | bdff548 | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 1571 | DEBUG(errs() << "IC: Mod = " << OrigI << '\n' |
| 1572 | << " New = " << *I << '\n'); |
Evan Cheng | c7baf68 | 2007-03-27 16:44:48 +0000 | [diff] [blame] | 1573 | #endif |
Chris Lattner | 0cea42a | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 1574 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 1575 | // If the instruction was modified, it's possible that it is now dead. |
| 1576 | // if so, remove it. |
Chris Lattner | 00d5131 | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 1577 | if (isInstructionTriviallyDead(I)) { |
Chris Lattner | 7a1e924 | 2009-08-30 06:13:40 +0000 | [diff] [blame] | 1578 | EraseInstFromFunction(*I); |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 1579 | } else { |
Chris Lattner | 7a1e924 | 2009-08-30 06:13:40 +0000 | [diff] [blame] | 1580 | Worklist.Add(I); |
Chris Lattner | e5ecdb5 | 2009-08-30 06:22:51 +0000 | [diff] [blame] | 1581 | Worklist.AddUsersToWorkList(*I); |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 1582 | } |
Chris Lattner | b3bc8fa | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 1583 | } |
Chris Lattner | b0b822c | 2009-08-31 06:57:37 +0000 | [diff] [blame] | 1584 | MadeIRChange = true; |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 1585 | } |
| 1586 | } |
| 1587 | |
Chris Lattner | 873ff01 | 2009-08-30 05:55:36 +0000 | [diff] [blame] | 1588 | Worklist.Zap(); |
Chris Lattner | b0b822c | 2009-08-31 06:57:37 +0000 | [diff] [blame] | 1589 | return MadeIRChange; |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 1590 | } |
| 1591 | |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 1592 | |
| 1593 | bool InstCombiner::runOnFunction(Function &F) { |
Chris Lattner | f964f32 | 2007-03-04 04:27:24 +0000 | [diff] [blame] | 1594 | MustPreserveLCSSA = mustPreserveAnalysisID(LCSSAID); |
Chris Lattner | e2cc1ad | 2009-10-15 04:13:44 +0000 | [diff] [blame] | 1595 | TD = getAnalysisIfAvailable<TargetData>(); |
| 1596 | |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 1597 | |
| 1598 | /// Builder - This is an IRBuilder that automatically inserts new |
| 1599 | /// instructions into the worklist when they are created. |
Chris Lattner | e2cc1ad | 2009-10-15 04:13:44 +0000 | [diff] [blame] | 1600 | IRBuilder<true, TargetFolder, InstCombineIRInserter> |
Chris Lattner | f55eeb9 | 2009-11-06 05:59:53 +0000 | [diff] [blame] | 1601 | TheBuilder(F.getContext(), TargetFolder(TD), |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 1602 | InstCombineIRInserter(Worklist)); |
| 1603 | Builder = &TheBuilder; |
| 1604 | |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 1605 | bool EverMadeChange = false; |
| 1606 | |
| 1607 | // Iterate while there is work to do. |
| 1608 | unsigned Iteration = 0; |
Bill Wendling | a6c3112 | 2008-05-14 22:45:20 +0000 | [diff] [blame] | 1609 | while (DoOneIteration(F, Iteration++)) |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 1610 | EverMadeChange = true; |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 1611 | |
| 1612 | Builder = 0; |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 1613 | return EverMadeChange; |
| 1614 | } |
| 1615 | |
Brian Gaeke | 96d4bf7 | 2004-07-27 17:43:21 +0000 | [diff] [blame] | 1616 | FunctionPass *llvm::createInstructionCombiningPass() { |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1617 | return new InstCombiner(); |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 1618 | } |