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