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