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