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