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" |
Owen Anderson | d672ecb | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 40 | #include "llvm/LLVMContext.h" |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 41 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 42 | #include "llvm/GlobalVariable.h" |
Dan Gohman | ca17890 | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 43 | #include "llvm/Operator.h" |
Chris Lattner | 79066fa | 2007-01-30 23:46:24 +0000 | [diff] [blame] | 44 | #include "llvm/Analysis/ConstantFolding.h" |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 45 | #include "llvm/Analysis/InstructionSimplify.h" |
Victor Hernandez | f006b18 | 2009-10-27 20:05:49 +0000 | [diff] [blame] | 46 | #include "llvm/Analysis/MemoryBuiltins.h" |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 47 | #include "llvm/Target/TargetData.h" |
| 48 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 49 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 50 | #include "llvm/Support/CallSite.h" |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 51 | #include "llvm/Support/Debug.h" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 52 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 53 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Chris Lattner | bcd7db5 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 54 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 55 | #include "llvm/Support/PatternMatch.h" |
Chris Lattner | 1f87a58 | 2007-02-15 19:41:52 +0000 | [diff] [blame] | 56 | #include "llvm/ADT/SmallPtrSet.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 57 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 58 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | b3bc8fa | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 59 | #include <algorithm> |
Torok Edwin | 3eaee31 | 2008-04-20 08:33:11 +0000 | [diff] [blame] | 60 | #include <climits> |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 61 | using namespace llvm; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 62 | using namespace llvm::PatternMatch; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 63 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 64 | STATISTIC(NumCombined , "Number of insts combined"); |
| 65 | STATISTIC(NumConstProp, "Number of constant folds"); |
| 66 | STATISTIC(NumDeadInst , "Number of dead inst eliminated"); |
| 67 | STATISTIC(NumDeadStore, "Number of dead stores eliminated"); |
| 68 | STATISTIC(NumSunkInst , "Number of instructions sunk"); |
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 69 | |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 70 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 71 | char InstCombiner::ID = 0; |
| 72 | static RegisterPass<InstCombiner> |
| 73 | X("instcombine", "Combine redundant instructions"); |
| 74 | |
Chris Lattner | e0b4b72 | 2010-01-04 07:17:19 +0000 | [diff] [blame] | 75 | void InstCombiner::getAnalysisUsage(AnalysisUsage &AU) const { |
| 76 | AU.addPreservedID(LCSSAID); |
| 77 | AU.setPreservesCFG(); |
| 78 | } |
| 79 | |
| 80 | |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 81 | // isOnlyUse - Return true if this instruction will be deleted if we stop using |
| 82 | // it. |
| 83 | static bool isOnlyUse(Value *V) { |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 84 | return V->hasOneUse() || isa<Constant>(V); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 87 | // getPromotedType - Return the specified type promoted as it would be to pass |
| 88 | // though a va_arg area... |
| 89 | static const Type *getPromotedType(const Type *Ty) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 90 | if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty)) { |
| 91 | if (ITy->getBitWidth() < 32) |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 92 | return Type::getInt32Ty(Ty->getContext()); |
Chris Lattner | 2b7e0ad | 2007-05-23 01:17:04 +0000 | [diff] [blame] | 93 | } |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 94 | return Ty; |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Chris Lattner | c22d4d1 | 2009-11-10 07:23:37 +0000 | [diff] [blame] | 97 | /// ShouldChangeType - Return true if it is desirable to convert a computation |
| 98 | /// from 'From' to 'To'. We don't want to convert from a legal to an illegal |
| 99 | /// type for example, or from a smaller to a larger illegal type. |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame^] | 100 | bool InstCombiner::ShouldChangeType(const Type *From, const Type *To) const { |
Chris Lattner | c22d4d1 | 2009-11-10 07:23:37 +0000 | [diff] [blame] | 101 | assert(isa<IntegerType>(From) && isa<IntegerType>(To)); |
| 102 | |
| 103 | // If we don't have TD, we don't know if the source/dest are legal. |
| 104 | if (!TD) return false; |
| 105 | |
| 106 | unsigned FromWidth = From->getPrimitiveSizeInBits(); |
| 107 | unsigned ToWidth = To->getPrimitiveSizeInBits(); |
| 108 | bool FromLegal = TD->isLegalInteger(FromWidth); |
| 109 | bool ToLegal = TD->isLegalInteger(ToWidth); |
| 110 | |
| 111 | // If this is a legal integer from type, and the result would be an illegal |
| 112 | // type, don't do the transformation. |
| 113 | if (FromLegal && !ToLegal) |
| 114 | return false; |
| 115 | |
| 116 | // Otherwise, if both are illegal, do not increase the size of the result. We |
| 117 | // do allow things like i160 -> i64, but not i64 -> i160. |
| 118 | if (!FromLegal && !ToLegal && ToWidth > FromWidth) |
| 119 | return false; |
| 120 | |
| 121 | return true; |
| 122 | } |
| 123 | |
Matthijs Kooijman | 7e6d9b9 | 2008-10-13 15:17:01 +0000 | [diff] [blame] | 124 | /// getBitCastOperand - If the specified operand is a CastInst, a constant |
| 125 | /// expression bitcast, or a GetElementPtrInst with all zero indices, return the |
| 126 | /// operand value, otherwise return null. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 127 | static Value *getBitCastOperand(Value *V) { |
Dan Gohman | 016de81 | 2009-07-17 23:55:56 +0000 | [diff] [blame] | 128 | if (Operator *O = dyn_cast<Operator>(V)) { |
| 129 | if (O->getOpcode() == Instruction::BitCast) |
| 130 | return O->getOperand(0); |
| 131 | if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) |
| 132 | if (GEP->hasAllZeroIndices()) |
| 133 | return GEP->getPointerOperand(); |
Matthijs Kooijman | 7e6d9b9 | 2008-10-13 15:17:01 +0000 | [diff] [blame] | 134 | } |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 135 | return 0; |
| 136 | } |
| 137 | |
Dan Gohman | ce9fe9f | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 138 | |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 139 | |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 140 | // SimplifyCommutative - This performs a few simplifications for commutative |
| 141 | // operators: |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 142 | // |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 143 | // 1. Order operands such that they are listed from right (least complex) to |
| 144 | // left (most complex). This puts constants before unary operators before |
| 145 | // binary operators. |
| 146 | // |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 147 | // 2. Transform: (op (op V, C1), C2) ==> (op V, (op C1, C2)) |
| 148 | // 3. Transform: (op (op V1, C1), (op V2, C2)) ==> (op (op V1, V2), (op C1,C2)) |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 149 | // |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 150 | bool InstCombiner::SimplifyCommutative(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 151 | bool Changed = false; |
Dan Gohman | 14ef4f0 | 2009-08-29 23:39:38 +0000 | [diff] [blame] | 152 | if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1))) |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 153 | Changed = !I.swapOperands(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 154 | |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 155 | if (!I.isAssociative()) return Changed; |
| 156 | Instruction::BinaryOps Opcode = I.getOpcode(); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 157 | if (BinaryOperator *Op = dyn_cast<BinaryOperator>(I.getOperand(0))) |
| 158 | if (Op->getOpcode() == Opcode && isa<Constant>(Op->getOperand(1))) { |
| 159 | if (isa<Constant>(I.getOperand(1))) { |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 160 | Constant *Folded = ConstantExpr::get(I.getOpcode(), |
Chris Lattner | 2a9c847 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 161 | cast<Constant>(I.getOperand(1)), |
| 162 | cast<Constant>(Op->getOperand(1))); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 163 | I.setOperand(0, Op->getOperand(0)); |
| 164 | I.setOperand(1, Folded); |
| 165 | return true; |
| 166 | } else if (BinaryOperator *Op1=dyn_cast<BinaryOperator>(I.getOperand(1))) |
| 167 | if (Op1->getOpcode() == Opcode && isa<Constant>(Op1->getOperand(1)) && |
| 168 | isOnlyUse(Op) && isOnlyUse(Op1)) { |
| 169 | Constant *C1 = cast<Constant>(Op->getOperand(1)); |
| 170 | Constant *C2 = cast<Constant>(Op1->getOperand(1)); |
| 171 | |
| 172 | // Fold (op (op V1, C1), (op V2, C2)) ==> (op (op V1, V2), (op C1,C2)) |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 173 | Constant *Folded = ConstantExpr::get(I.getOpcode(), C1, C2); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 174 | Instruction *New = BinaryOperator::Create(Opcode, Op->getOperand(0), |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 175 | Op1->getOperand(0), |
| 176 | Op1->getName(), &I); |
Chris Lattner | 7a1e924 | 2009-08-30 06:13:40 +0000 | [diff] [blame] | 177 | Worklist.Add(New); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 178 | I.setOperand(0, New); |
| 179 | I.setOperand(1, Folded); |
| 180 | return true; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 181 | } |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 182 | } |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 183 | return Changed; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 184 | } |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 185 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 186 | // dyn_castNegVal - Given a 'sub' instruction, return the RHS of the instruction |
| 187 | // if the LHS is a constant zero (which is the 'negate' form). |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 188 | // |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 189 | Value *InstCombiner::dyn_castNegVal(Value *V) const { |
Owen Anderson | fa82b6e | 2009-07-13 22:18:28 +0000 | [diff] [blame] | 190 | if (BinaryOperator::isNeg(V)) |
Chris Lattner | a1df33c | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 191 | return BinaryOperator::getNegArgument(V); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 192 | |
Chris Lattner | 0ce8580 | 2004-12-14 20:08:06 +0000 | [diff] [blame] | 193 | // Constants can be considered to be negated values if they can be folded. |
| 194 | if (ConstantInt *C = dyn_cast<ConstantInt>(V)) |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 195 | return ConstantExpr::getNeg(C); |
Nick Lewycky | 18b3da6 | 2008-05-23 04:54:45 +0000 | [diff] [blame] | 196 | |
| 197 | if (ConstantVector *C = dyn_cast<ConstantVector>(V)) |
| 198 | if (C->getType()->getElementType()->isInteger()) |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 199 | return ConstantExpr::getNeg(C); |
Nick Lewycky | 18b3da6 | 2008-05-23 04:54:45 +0000 | [diff] [blame] | 200 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 201 | return 0; |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 204 | // dyn_castFNegVal - Given a 'fsub' instruction, return the RHS of the |
| 205 | // instruction if the LHS is a constant negative zero (which is the 'negate' |
| 206 | // form). |
| 207 | // |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 208 | static inline Value *dyn_castFNegVal(Value *V) { |
Owen Anderson | fa82b6e | 2009-07-13 22:18:28 +0000 | [diff] [blame] | 209 | if (BinaryOperator::isFNeg(V)) |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 210 | return BinaryOperator::getFNegArgument(V); |
| 211 | |
| 212 | // Constants can be considered to be negated values if they can be folded. |
| 213 | if (ConstantFP *C = dyn_cast<ConstantFP>(V)) |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 214 | return ConstantExpr::getFNeg(C); |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 215 | |
| 216 | if (ConstantVector *C = dyn_cast<ConstantVector>(V)) |
| 217 | if (C->getType()->getElementType()->isFloatingPoint()) |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 218 | return ConstantExpr::getFNeg(C); |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 219 | |
| 220 | return 0; |
| 221 | } |
| 222 | |
Chris Lattner | b109b5c | 2009-12-21 06:03:05 +0000 | [diff] [blame] | 223 | /// MatchSelectPattern - Pattern match integer [SU]MIN, [SU]MAX, and ABS idioms, |
| 224 | /// returning the kind and providing the out parameter results if we |
| 225 | /// successfully match. |
| 226 | static SelectPatternFlavor |
| 227 | MatchSelectPattern(Value *V, Value *&LHS, Value *&RHS) { |
| 228 | SelectInst *SI = dyn_cast<SelectInst>(V); |
| 229 | if (SI == 0) return SPF_UNKNOWN; |
| 230 | |
| 231 | ICmpInst *ICI = dyn_cast<ICmpInst>(SI->getCondition()); |
| 232 | if (ICI == 0) return SPF_UNKNOWN; |
| 233 | |
| 234 | LHS = ICI->getOperand(0); |
| 235 | RHS = ICI->getOperand(1); |
| 236 | |
| 237 | // (icmp X, Y) ? X : Y |
| 238 | if (SI->getTrueValue() == ICI->getOperand(0) && |
| 239 | SI->getFalseValue() == ICI->getOperand(1)) { |
| 240 | switch (ICI->getPredicate()) { |
| 241 | default: return SPF_UNKNOWN; // Equality. |
| 242 | case ICmpInst::ICMP_UGT: |
| 243 | case ICmpInst::ICMP_UGE: return SPF_UMAX; |
| 244 | case ICmpInst::ICMP_SGT: |
| 245 | case ICmpInst::ICMP_SGE: return SPF_SMAX; |
| 246 | case ICmpInst::ICMP_ULT: |
| 247 | case ICmpInst::ICMP_ULE: return SPF_UMIN; |
| 248 | case ICmpInst::ICMP_SLT: |
| 249 | case ICmpInst::ICMP_SLE: return SPF_SMIN; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | // (icmp X, Y) ? Y : X |
| 254 | if (SI->getTrueValue() == ICI->getOperand(1) && |
| 255 | SI->getFalseValue() == ICI->getOperand(0)) { |
| 256 | switch (ICI->getPredicate()) { |
| 257 | default: return SPF_UNKNOWN; // Equality. |
| 258 | case ICmpInst::ICMP_UGT: |
| 259 | case ICmpInst::ICMP_UGE: return SPF_UMIN; |
| 260 | case ICmpInst::ICMP_SGT: |
| 261 | case ICmpInst::ICMP_SGE: return SPF_SMIN; |
| 262 | case ICmpInst::ICMP_ULT: |
| 263 | case ICmpInst::ICMP_ULE: return SPF_UMAX; |
| 264 | case ICmpInst::ICMP_SLT: |
| 265 | case ICmpInst::ICMP_SLE: return SPF_SMAX; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | // TODO: (X > 4) ? X : 5 --> (X >= 5) ? X : 5 --> MAX(X, 5) |
| 270 | |
| 271 | return SPF_UNKNOWN; |
| 272 | } |
| 273 | |
Chris Lattner | 48b59ec | 2009-10-26 15:40:07 +0000 | [diff] [blame] | 274 | /// isFreeToInvert - Return true if the specified value is free to invert (apply |
| 275 | /// ~ to). This happens in cases where the ~ can be eliminated. |
| 276 | static inline bool isFreeToInvert(Value *V) { |
| 277 | // ~(~(X)) -> X. |
Evan Cheng | 85def16 | 2009-10-26 03:51:32 +0000 | [diff] [blame] | 278 | if (BinaryOperator::isNot(V)) |
Chris Lattner | 48b59ec | 2009-10-26 15:40:07 +0000 | [diff] [blame] | 279 | return true; |
| 280 | |
| 281 | // Constants can be considered to be not'ed values. |
| 282 | if (isa<ConstantInt>(V)) |
| 283 | return true; |
| 284 | |
| 285 | // Compares can be inverted if they have a single use. |
| 286 | if (CmpInst *CI = dyn_cast<CmpInst>(V)) |
| 287 | return CI->hasOneUse(); |
| 288 | |
| 289 | return false; |
| 290 | } |
| 291 | |
| 292 | static inline Value *dyn_castNotVal(Value *V) { |
| 293 | // If this is not(not(x)) don't return that this is a not: we want the two |
| 294 | // not's to be folded first. |
| 295 | if (BinaryOperator::isNot(V)) { |
| 296 | Value *Operand = BinaryOperator::getNotArgument(V); |
| 297 | if (!isFreeToInvert(Operand)) |
| 298 | return Operand; |
| 299 | } |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 300 | |
| 301 | // Constants can be considered to be not'ed values... |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 302 | if (ConstantInt *C = dyn_cast<ConstantInt>(V)) |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 303 | return ConstantInt::get(C->getType(), ~C->getValue()); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 304 | return 0; |
| 305 | } |
| 306 | |
Chris Lattner | 48b59ec | 2009-10-26 15:40:07 +0000 | [diff] [blame] | 307 | |
| 308 | |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 309 | // dyn_castFoldableMul - If this value is a multiply that can be folded into |
| 310 | // other computations (because it has a constant operand), return the |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 311 | // non-constant operand of the multiply, and set CST to point to the multiplier. |
| 312 | // Otherwise, return null. |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 313 | // |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 314 | static inline Value *dyn_castFoldableMul(Value *V, ConstantInt *&CST) { |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 315 | if (V->hasOneUse() && V->getType()->isInteger()) |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 316 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 317 | if (I->getOpcode() == Instruction::Mul) |
Chris Lattner | 50e60c7 | 2004-11-15 05:54:07 +0000 | [diff] [blame] | 318 | if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 319 | return I->getOperand(0); |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 320 | if (I->getOpcode() == Instruction::Shl) |
Chris Lattner | 50e60c7 | 2004-11-15 05:54:07 +0000 | [diff] [blame] | 321 | if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) { |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 322 | // The multiplier is really 1 << CST. |
Zhou Sheng | 97b52c2 | 2007-03-29 01:57:21 +0000 | [diff] [blame] | 323 | uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth(); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 324 | uint32_t CSTVal = CST->getLimitedValue(BitWidth); |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 325 | CST = ConstantInt::get(V->getType()->getContext(), |
| 326 | APInt(BitWidth, 1).shl(CSTVal)); |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 327 | return I->getOperand(0); |
| 328 | } |
| 329 | } |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 330 | return 0; |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 331 | } |
Chris Lattner | af2930e | 2002-08-14 17:51:49 +0000 | [diff] [blame] | 332 | |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 333 | /// AddOne - Add one to a ConstantInt |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 334 | static Constant *AddOne(Constant *C) { |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 335 | return ConstantExpr::getAdd(C, ConstantInt::get(C->getType(), 1)); |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 336 | } |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 337 | /// SubOne - Subtract one from a ConstantInt |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 338 | static Constant *SubOne(ConstantInt *C) { |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 339 | return ConstantExpr::getSub(C, ConstantInt::get(C->getType(), 1)); |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 340 | } |
Nick Lewycky | e0cfecf | 2008-02-18 22:48:05 +0000 | [diff] [blame] | 341 | /// MultiplyOverflows - True if the multiply can not be expressed in an int |
| 342 | /// this size. |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 343 | static bool MultiplyOverflows(ConstantInt *C1, ConstantInt *C2, bool sign) { |
Nick Lewycky | e0cfecf | 2008-02-18 22:48:05 +0000 | [diff] [blame] | 344 | uint32_t W = C1->getBitWidth(); |
| 345 | APInt LHSExt = C1->getValue(), RHSExt = C2->getValue(); |
| 346 | if (sign) { |
| 347 | LHSExt.sext(W * 2); |
| 348 | RHSExt.sext(W * 2); |
| 349 | } else { |
| 350 | LHSExt.zext(W * 2); |
| 351 | RHSExt.zext(W * 2); |
| 352 | } |
| 353 | |
| 354 | APInt MulExt = LHSExt * RHSExt; |
| 355 | |
Chris Lattner | b109b5c | 2009-12-21 06:03:05 +0000 | [diff] [blame] | 356 | if (!sign) |
Nick Lewycky | e0cfecf | 2008-02-18 22:48:05 +0000 | [diff] [blame] | 357 | return MulExt.ugt(APInt::getLowBitsSet(W * 2, W)); |
Chris Lattner | b109b5c | 2009-12-21 06:03:05 +0000 | [diff] [blame] | 358 | |
| 359 | APInt Min = APInt::getSignedMinValue(W).sext(W * 2); |
| 360 | APInt Max = APInt::getSignedMaxValue(W).sext(W * 2); |
| 361 | return MulExt.slt(Min) || MulExt.sgt(Max); |
Nick Lewycky | e0cfecf | 2008-02-18 22:48:05 +0000 | [diff] [blame] | 362 | } |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 363 | |
Reid Spencer | e7816b5 | 2007-03-08 01:52:58 +0000 | [diff] [blame] | 364 | |
Dan Gohman | 45b4e48 | 2008-05-19 22:14:15 +0000 | [diff] [blame] | 365 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 366 | /// AssociativeOpt - Perform an optimization on an associative operator. This |
| 367 | /// function is designed to check a chain of associative operators for a |
| 368 | /// potential to apply a certain optimization. Since the optimization may be |
| 369 | /// applicable if the expression was reassociated, this checks the chain, then |
| 370 | /// reassociates the expression as necessary to expose the optimization |
| 371 | /// opportunity. This makes use of a special Functor, which must define |
| 372 | /// 'shouldApply' and 'apply' methods. |
| 373 | /// |
| 374 | template<typename Functor> |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 375 | static Instruction *AssociativeOpt(BinaryOperator &Root, const Functor &F) { |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 376 | unsigned Opcode = Root.getOpcode(); |
| 377 | Value *LHS = Root.getOperand(0); |
| 378 | |
| 379 | // Quick check, see if the immediate LHS matches... |
| 380 | if (F.shouldApply(LHS)) |
| 381 | return F.apply(Root); |
| 382 | |
| 383 | // Otherwise, if the LHS is not of the same opcode as the root, return. |
| 384 | Instruction *LHSI = dyn_cast<Instruction>(LHS); |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 385 | while (LHSI && LHSI->getOpcode() == Opcode && LHSI->hasOneUse()) { |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 386 | // Should we apply this transform to the RHS? |
| 387 | bool ShouldApply = F.shouldApply(LHSI->getOperand(1)); |
| 388 | |
| 389 | // If not to the RHS, check to see if we should apply to the LHS... |
| 390 | if (!ShouldApply && F.shouldApply(LHSI->getOperand(0))) { |
| 391 | cast<BinaryOperator>(LHSI)->swapOperands(); // Make the LHS the RHS |
| 392 | ShouldApply = true; |
| 393 | } |
| 394 | |
| 395 | // If the functor wants to apply the optimization to the RHS of LHSI, |
| 396 | // reassociate the expression from ((? op A) op B) to (? op (A op B)) |
| 397 | if (ShouldApply) { |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 398 | // Now all of the instructions are in the current basic block, go ahead |
| 399 | // and perform the reassociation. |
| 400 | Instruction *TmpLHSI = cast<Instruction>(Root.getOperand(0)); |
| 401 | |
| 402 | // First move the selected RHS to the LHS of the root... |
| 403 | Root.setOperand(0, LHSI->getOperand(1)); |
| 404 | |
| 405 | // Make what used to be the LHS of the root be the user of the root... |
| 406 | Value *ExtraOperand = TmpLHSI->getOperand(1); |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 407 | if (&Root == TmpLHSI) { |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 408 | Root.replaceAllUsesWith(Constant::getNullValue(TmpLHSI->getType())); |
Chris Lattner | 15a76c0 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 409 | return 0; |
| 410 | } |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 411 | Root.replaceAllUsesWith(TmpLHSI); // Users now use TmpLHSI |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 412 | TmpLHSI->setOperand(1, &Root); // TmpLHSI now uses the root |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 413 | BasicBlock::iterator ARI = &Root; ++ARI; |
Dan Gohman | d02d917 | 2008-06-19 17:47:47 +0000 | [diff] [blame] | 414 | TmpLHSI->moveBefore(ARI); // Move TmpLHSI to after Root |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 415 | ARI = Root; |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 416 | |
| 417 | // Now propagate the ExtraOperand down the chain of instructions until we |
| 418 | // get to LHSI. |
| 419 | while (TmpLHSI != LHSI) { |
| 420 | Instruction *NextLHSI = cast<Instruction>(TmpLHSI->getOperand(0)); |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 421 | // Move the instruction to immediately before the chain we are |
| 422 | // constructing to avoid breaking dominance properties. |
Dan Gohman | d02d917 | 2008-06-19 17:47:47 +0000 | [diff] [blame] | 423 | NextLHSI->moveBefore(ARI); |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 424 | ARI = NextLHSI; |
| 425 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 426 | Value *NextOp = NextLHSI->getOperand(1); |
| 427 | NextLHSI->setOperand(1, ExtraOperand); |
| 428 | TmpLHSI = NextLHSI; |
| 429 | ExtraOperand = NextOp; |
| 430 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 431 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 432 | // Now that the instructions are reassociated, have the functor perform |
| 433 | // the transformation... |
| 434 | return F.apply(Root); |
| 435 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 436 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 437 | LHSI = dyn_cast<Instruction>(LHSI->getOperand(0)); |
| 438 | } |
| 439 | return 0; |
| 440 | } |
| 441 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 442 | namespace { |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 443 | |
Nick Lewycky | 02d639f | 2008-05-23 04:34:58 +0000 | [diff] [blame] | 444 | // AddRHS - Implements: X + X --> X << 1 |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 445 | struct AddRHS { |
| 446 | Value *RHS; |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 447 | explicit AddRHS(Value *rhs) : RHS(rhs) {} |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 448 | bool shouldApply(Value *LHS) const { return LHS == RHS; } |
| 449 | Instruction *apply(BinaryOperator &Add) const { |
Nick Lewycky | 02d639f | 2008-05-23 04:34:58 +0000 | [diff] [blame] | 450 | return BinaryOperator::CreateShl(Add.getOperand(0), |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 451 | ConstantInt::get(Add.getType(), 1)); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 452 | } |
| 453 | }; |
| 454 | |
| 455 | // AddMaskingAnd - Implements (A & C1)+(B & C2) --> (A & C1)|(B & C2) |
| 456 | // iff C1&C2 == 0 |
| 457 | struct AddMaskingAnd { |
| 458 | Constant *C2; |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 459 | explicit AddMaskingAnd(Constant *c) : C2(c) {} |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 460 | bool shouldApply(Value *LHS) const { |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 461 | ConstantInt *C1; |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 462 | return match(LHS, m_And(m_Value(), m_ConstantInt(C1))) && |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 463 | ConstantExpr::getAnd(C1, C2)->isNullValue(); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 464 | } |
| 465 | Instruction *apply(BinaryOperator &Add) const { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 466 | return BinaryOperator::CreateOr(Add.getOperand(0), Add.getOperand(1)); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 467 | } |
| 468 | }; |
| 469 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 472 | static Value *FoldOperationIntoSelectOperand(Instruction &I, Value *SO, |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 473 | InstCombiner *IC) { |
Chris Lattner | 08142f2 | 2009-08-30 19:47:22 +0000 | [diff] [blame] | 474 | if (CastInst *CI = dyn_cast<CastInst>(&I)) |
Chris Lattner | 2345d1d | 2009-08-30 20:01:10 +0000 | [diff] [blame] | 475 | return IC->Builder->CreateCast(CI->getOpcode(), SO, I.getType()); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 476 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 477 | // Figure out if the constant is the left or the right argument. |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 478 | bool ConstIsRHS = isa<Constant>(I.getOperand(1)); |
| 479 | Constant *ConstOperand = cast<Constant>(I.getOperand(ConstIsRHS)); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 480 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 481 | if (Constant *SOC = dyn_cast<Constant>(SO)) { |
| 482 | if (ConstIsRHS) |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 483 | return ConstantExpr::get(I.getOpcode(), SOC, ConstOperand); |
| 484 | return ConstantExpr::get(I.getOpcode(), ConstOperand, SOC); |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | Value *Op0 = SO, *Op1 = ConstOperand; |
| 488 | if (!ConstIsRHS) |
| 489 | std::swap(Op0, Op1); |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 490 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 491 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I)) |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 492 | return IC->Builder->CreateBinOp(BO->getOpcode(), Op0, Op1, |
| 493 | SO->getName()+".op"); |
| 494 | if (ICmpInst *CI = dyn_cast<ICmpInst>(&I)) |
| 495 | return IC->Builder->CreateICmp(CI->getPredicate(), Op0, Op1, |
| 496 | SO->getName()+".cmp"); |
| 497 | if (FCmpInst *CI = dyn_cast<FCmpInst>(&I)) |
| 498 | return IC->Builder->CreateICmp(CI->getPredicate(), Op0, Op1, |
| 499 | SO->getName()+".cmp"); |
| 500 | llvm_unreachable("Unknown binary instruction type!"); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | // FoldOpIntoSelect - Given an instruction with a select as one operand and a |
| 504 | // constant as the other operand, try to fold the binary operator into the |
| 505 | // select arguments. This also works for Cast instructions, which obviously do |
| 506 | // not have a second operand. |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame^] | 507 | Instruction *InstCombiner::FoldOpIntoSelect(Instruction &Op, SelectInst *SI) { |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 508 | // Don't modify shared select instructions |
| 509 | if (!SI->hasOneUse()) return 0; |
| 510 | Value *TV = SI->getOperand(1); |
| 511 | Value *FV = SI->getOperand(2); |
| 512 | |
| 513 | if (isa<Constant>(TV) || isa<Constant>(FV)) { |
Chris Lattner | 956db27 | 2005-04-21 05:43:13 +0000 | [diff] [blame] | 514 | // Bool selects with constant operands can be folded to logical ops. |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 515 | if (SI->getType() == Type::getInt1Ty(SI->getContext())) return 0; |
Chris Lattner | 956db27 | 2005-04-21 05:43:13 +0000 | [diff] [blame] | 516 | |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame^] | 517 | Value *SelectTrueVal = FoldOperationIntoSelectOperand(Op, TV, this); |
| 518 | Value *SelectFalseVal = FoldOperationIntoSelectOperand(Op, FV, this); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 519 | |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 520 | return SelectInst::Create(SI->getCondition(), SelectTrueVal, |
| 521 | SelectFalseVal); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 522 | } |
| 523 | return 0; |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 524 | } |
| 525 | |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 526 | |
Chris Lattner | 5d1704d | 2009-09-27 19:57:57 +0000 | [diff] [blame] | 527 | /// FoldOpIntoPhi - Given a binary operator, cast instruction, or select which |
| 528 | /// has a PHI node as operand #0, see if we can fold the instruction into the |
| 529 | /// 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] | 530 | /// |
| 531 | /// If AllowAggressive is true, FoldOpIntoPhi will allow certain transforms |
| 532 | /// that would normally be unprofitable because they strongly encourage jump |
| 533 | /// threading. |
| 534 | Instruction *InstCombiner::FoldOpIntoPhi(Instruction &I, |
| 535 | bool AllowAggressive) { |
| 536 | AllowAggressive = false; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 537 | PHINode *PN = cast<PHINode>(I.getOperand(0)); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 538 | unsigned NumPHIValues = PN->getNumIncomingValues(); |
Chris Lattner | 213cd61 | 2009-09-27 20:46:36 +0000 | [diff] [blame] | 539 | if (NumPHIValues == 0 || |
| 540 | // We normally only transform phis with a single use, unless we're trying |
| 541 | // hard to make jump threading happen. |
| 542 | (!PN->hasOneUse() && !AllowAggressive)) |
| 543 | return 0; |
| 544 | |
| 545 | |
Chris Lattner | 5d1704d | 2009-09-27 19:57:57 +0000 | [diff] [blame] | 546 | // Check to see if all of the operands of the PHI are simple constants |
| 547 | // (constantint/constantfp/undef). If there is one non-constant value, |
Chris Lattner | c6df8f4 | 2009-09-27 20:18:49 +0000 | [diff] [blame] | 548 | // remember the BB it is in. If there is more than one or if *it* is a PHI, |
| 549 | // bail out. We don't do arbitrary constant expressions here because moving |
| 550 | // their computation can be expensive without a cost model. |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 551 | BasicBlock *NonConstBB = 0; |
| 552 | for (unsigned i = 0; i != NumPHIValues; ++i) |
Chris Lattner | 5d1704d | 2009-09-27 19:57:57 +0000 | [diff] [blame] | 553 | if (!isa<Constant>(PN->getIncomingValue(i)) || |
| 554 | isa<ConstantExpr>(PN->getIncomingValue(i))) { |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 555 | if (NonConstBB) return 0; // More than one non-const value. |
Chris Lattner | b303668 | 2007-02-24 01:03:45 +0000 | [diff] [blame] | 556 | if (isa<PHINode>(PN->getIncomingValue(i))) return 0; // Itself a phi. |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 557 | NonConstBB = PN->getIncomingBlock(i); |
| 558 | |
| 559 | // If the incoming non-constant value is in I's block, we have an infinite |
| 560 | // loop. |
| 561 | if (NonConstBB == I.getParent()) |
| 562 | return 0; |
| 563 | } |
| 564 | |
| 565 | // If there is exactly one non-constant value, we can insert a copy of the |
| 566 | // operation in that block. However, if this is a critical edge, we would be |
| 567 | // inserting the computation one some other paths (e.g. inside a loop). Only |
| 568 | // do this if the pred block is unconditionally branching into the phi block. |
Chris Lattner | 213cd61 | 2009-09-27 20:46:36 +0000 | [diff] [blame] | 569 | if (NonConstBB != 0 && !AllowAggressive) { |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 570 | BranchInst *BI = dyn_cast<BranchInst>(NonConstBB->getTerminator()); |
| 571 | if (!BI || !BI->isUnconditional()) return 0; |
| 572 | } |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 573 | |
| 574 | // Okay, we can do the transformation: create the new PHI node. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 575 | PHINode *NewPN = PHINode::Create(I.getType(), ""); |
Chris Lattner | 5551706 | 2005-01-29 00:39:08 +0000 | [diff] [blame] | 576 | NewPN->reserveOperandSpace(PN->getNumOperands()/2); |
Chris Lattner | 857eb57 | 2009-10-21 23:41:58 +0000 | [diff] [blame] | 577 | InsertNewInstBefore(NewPN, *PN); |
| 578 | NewPN->takeName(PN); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 579 | |
| 580 | // Next, add all of the operands to the PHI. |
Chris Lattner | 5d1704d | 2009-09-27 19:57:57 +0000 | [diff] [blame] | 581 | if (SelectInst *SI = dyn_cast<SelectInst>(&I)) { |
| 582 | // We only currently try to fold the condition of a select when it is a phi, |
| 583 | // not the true/false values. |
Chris Lattner | c6df8f4 | 2009-09-27 20:18:49 +0000 | [diff] [blame] | 584 | Value *TrueV = SI->getTrueValue(); |
| 585 | Value *FalseV = SI->getFalseValue(); |
Chris Lattner | 3ddfb21 | 2009-09-28 06:49:44 +0000 | [diff] [blame] | 586 | BasicBlock *PhiTransBB = PN->getParent(); |
Chris Lattner | 5d1704d | 2009-09-27 19:57:57 +0000 | [diff] [blame] | 587 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
Chris Lattner | c6df8f4 | 2009-09-27 20:18:49 +0000 | [diff] [blame] | 588 | BasicBlock *ThisBB = PN->getIncomingBlock(i); |
Chris Lattner | 3ddfb21 | 2009-09-28 06:49:44 +0000 | [diff] [blame] | 589 | Value *TrueVInPred = TrueV->DoPHITranslation(PhiTransBB, ThisBB); |
| 590 | Value *FalseVInPred = FalseV->DoPHITranslation(PhiTransBB, ThisBB); |
Chris Lattner | 5d1704d | 2009-09-27 19:57:57 +0000 | [diff] [blame] | 591 | Value *InV = 0; |
| 592 | if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) { |
Chris Lattner | c6df8f4 | 2009-09-27 20:18:49 +0000 | [diff] [blame] | 593 | InV = InC->isNullValue() ? FalseVInPred : TrueVInPred; |
Chris Lattner | 5d1704d | 2009-09-27 19:57:57 +0000 | [diff] [blame] | 594 | } else { |
| 595 | assert(PN->getIncomingBlock(i) == NonConstBB); |
Chris Lattner | c6df8f4 | 2009-09-27 20:18:49 +0000 | [diff] [blame] | 596 | InV = SelectInst::Create(PN->getIncomingValue(i), TrueVInPred, |
| 597 | FalseVInPred, |
Chris Lattner | 5d1704d | 2009-09-27 19:57:57 +0000 | [diff] [blame] | 598 | "phitmp", NonConstBB->getTerminator()); |
Chris Lattner | 857eb57 | 2009-10-21 23:41:58 +0000 | [diff] [blame] | 599 | Worklist.Add(cast<Instruction>(InV)); |
Chris Lattner | 5d1704d | 2009-09-27 19:57:57 +0000 | [diff] [blame] | 600 | } |
Chris Lattner | c6df8f4 | 2009-09-27 20:18:49 +0000 | [diff] [blame] | 601 | NewPN->addIncoming(InV, ThisBB); |
Chris Lattner | 5d1704d | 2009-09-27 19:57:57 +0000 | [diff] [blame] | 602 | } |
| 603 | } else if (I.getNumOperands() == 2) { |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 604 | Constant *C = cast<Constant>(I.getOperand(1)); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 605 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
Chris Lattner | a9ff5eb | 2007-08-05 08:47:58 +0000 | [diff] [blame] | 606 | Value *InV = 0; |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 607 | if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 608 | if (CmpInst *CI = dyn_cast<CmpInst>(&I)) |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 609 | InV = ConstantExpr::getCompare(CI->getPredicate(), InC, C); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 610 | else |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 611 | InV = ConstantExpr::get(I.getOpcode(), InC, C); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 612 | } else { |
| 613 | assert(PN->getIncomingBlock(i) == NonConstBB); |
| 614 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 615 | InV = BinaryOperator::Create(BO->getOpcode(), |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 616 | PN->getIncomingValue(i), C, "phitmp", |
| 617 | NonConstBB->getTerminator()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 618 | else if (CmpInst *CI = dyn_cast<CmpInst>(&I)) |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 619 | InV = CmpInst::Create(CI->getOpcode(), |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 620 | CI->getPredicate(), |
| 621 | PN->getIncomingValue(i), C, "phitmp", |
| 622 | NonConstBB->getTerminator()); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 623 | else |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 624 | llvm_unreachable("Unknown binop!"); |
Chris Lattner | 857eb57 | 2009-10-21 23:41:58 +0000 | [diff] [blame] | 625 | |
| 626 | Worklist.Add(cast<Instruction>(InV)); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 627 | } |
| 628 | NewPN->addIncoming(InV, PN->getIncomingBlock(i)); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 629 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 630 | } else { |
| 631 | CastInst *CI = cast<CastInst>(&I); |
| 632 | const Type *RetTy = CI->getType(); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 633 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 634 | Value *InV; |
| 635 | if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) { |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 636 | InV = ConstantExpr::getCast(CI->getOpcode(), InC, RetTy); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 637 | } else { |
| 638 | assert(PN->getIncomingBlock(i) == NonConstBB); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 639 | InV = CastInst::Create(CI->getOpcode(), PN->getIncomingValue(i), |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 640 | I.getType(), "phitmp", |
| 641 | NonConstBB->getTerminator()); |
Chris Lattner | 857eb57 | 2009-10-21 23:41:58 +0000 | [diff] [blame] | 642 | Worklist.Add(cast<Instruction>(InV)); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 643 | } |
| 644 | NewPN->addIncoming(InV, PN->getIncomingBlock(i)); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 645 | } |
| 646 | } |
| 647 | return ReplaceInstUsesWith(I, NewPN); |
| 648 | } |
| 649 | |
Chris Lattner | 2454a2e | 2008-01-29 06:52:45 +0000 | [diff] [blame] | 650 | |
Chris Lattner | 3d28b1b | 2008-05-20 05:46:13 +0000 | [diff] [blame] | 651 | /// WillNotOverflowSignedAdd - Return true if we can prove that: |
| 652 | /// (sext (add LHS, RHS)) === (add (sext LHS), (sext RHS)) |
| 653 | /// This basically requires proving that the add in the original type would not |
| 654 | /// overflow to change the sign bit or have a carry out. |
| 655 | bool InstCombiner::WillNotOverflowSignedAdd(Value *LHS, Value *RHS) { |
| 656 | // There are different heuristics we can use for this. Here are some simple |
| 657 | // ones. |
| 658 | |
| 659 | // Add has the property that adding any two 2's complement numbers can only |
| 660 | // have one carry bit which can change a sign. As such, if LHS and RHS each |
Chris Lattner | 8aee8ef | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 661 | // have at least two sign bits, we know that the addition of the two values |
| 662 | // will sign extend fine. |
Chris Lattner | 3d28b1b | 2008-05-20 05:46:13 +0000 | [diff] [blame] | 663 | if (ComputeNumSignBits(LHS) > 1 && ComputeNumSignBits(RHS) > 1) |
| 664 | return true; |
| 665 | |
| 666 | |
| 667 | // If one of the operands only has one non-zero bit, and if the other operand |
| 668 | // has a known-zero bit in a more significant place than it (not including the |
| 669 | // sign bit) the ripple may go up to and fill the zero, but won't change the |
| 670 | // sign. For example, (X & ~4) + 1. |
| 671 | |
| 672 | // TODO: Implement. |
| 673 | |
| 674 | return false; |
| 675 | } |
| 676 | |
Chris Lattner | 2454a2e | 2008-01-29 06:52:45 +0000 | [diff] [blame] | 677 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 678 | Instruction *InstCombiner::visitAdd(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 679 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 680 | Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 681 | |
Chris Lattner | 8aee8ef | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 682 | if (Value *V = SimplifyAddInst(LHS, RHS, I.hasNoSignedWrap(), |
| 683 | I.hasNoUnsignedWrap(), TD)) |
| 684 | return ReplaceInstUsesWith(I, V); |
| 685 | |
| 686 | |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 687 | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 688 | if (ConstantInt *CI = dyn_cast<ConstantInt>(RHSC)) { |
Chris Lattner | b4a2f05 | 2006-11-09 05:12:27 +0000 | [diff] [blame] | 689 | // X + (signbit) --> X ^ signbit |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 690 | const APInt& Val = CI->getValue(); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 691 | uint32_t BitWidth = Val.getBitWidth(); |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 692 | if (Val == APInt::getSignBit(BitWidth)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 693 | return BinaryOperator::CreateXor(LHS, RHS); |
Chris Lattner | b4a2f05 | 2006-11-09 05:12:27 +0000 | [diff] [blame] | 694 | |
| 695 | // See if SimplifyDemandedBits can simplify this. This handles stuff like |
| 696 | // (X & 254)+1 -> (X&254)|1 |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 697 | if (SimplifyDemandedInstructionBits(I)) |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 698 | return &I; |
Dan Gohman | 1975d03 | 2008-10-30 20:40:10 +0000 | [diff] [blame] | 699 | |
Eli Friedman | 709b33d | 2009-07-13 22:27:52 +0000 | [diff] [blame] | 700 | // zext(bool) + C -> bool ? C + 1 : C |
Dan Gohman | 1975d03 | 2008-10-30 20:40:10 +0000 | [diff] [blame] | 701 | if (ZExtInst *ZI = dyn_cast<ZExtInst>(LHS)) |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 702 | if (ZI->getSrcTy() == Type::getInt1Ty(I.getContext())) |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 703 | return SelectInst::Create(ZI->getOperand(0), AddOne(CI), CI); |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 704 | } |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 705 | |
| 706 | if (isa<PHINode>(LHS)) |
| 707 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 708 | return NV; |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 709 | |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 710 | ConstantInt *XorRHS = 0; |
| 711 | Value *XorLHS = 0; |
Chris Lattner | c5eff44 | 2007-01-30 22:32:46 +0000 | [diff] [blame] | 712 | if (isa<ConstantInt>(RHSC) && |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 713 | match(LHS, m_Xor(m_Value(XorLHS), m_ConstantInt(XorRHS)))) { |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 714 | uint32_t TySizeBits = I.getType()->getScalarSizeInBits(); |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 715 | const APInt& RHSVal = cast<ConstantInt>(RHSC)->getValue(); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 716 | |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 717 | uint32_t Size = TySizeBits / 2; |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 718 | APInt C0080Val(APInt(TySizeBits, 1ULL).shl(Size - 1)); |
| 719 | APInt CFF80Val(-C0080Val); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 720 | do { |
| 721 | if (TySizeBits > Size) { |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 722 | // If we have ADD(XOR(AND(X, 0xFF), 0x80), 0xF..F80), it's a sext. |
| 723 | // If we have ADD(XOR(AND(X, 0xFF), 0xF..F80), 0x80), it's a sext. |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 724 | if ((RHSVal == CFF80Val && XorRHS->getValue() == C0080Val) || |
| 725 | (RHSVal == C0080Val && XorRHS->getValue() == CFF80Val)) { |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 726 | // This is a sign extend if the top bits are known zero. |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 727 | if (!MaskedValueIsZero(XorLHS, |
| 728 | APInt::getHighBitsSet(TySizeBits, TySizeBits - Size))) |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 729 | Size = 0; // Not a sign ext, but can't be any others either. |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 730 | break; |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 731 | } |
| 732 | } |
| 733 | Size >>= 1; |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 734 | C0080Val = APIntOps::lshr(C0080Val, Size); |
| 735 | CFF80Val = APIntOps::ashr(CFF80Val, Size); |
| 736 | } while (Size >= 1); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 737 | |
Reid Spencer | 35c3885 | 2007-03-28 01:36:16 +0000 | [diff] [blame] | 738 | // FIXME: This shouldn't be necessary. When the backends can handle types |
Chris Lattner | 0c7a9a0 | 2008-05-19 20:25:04 +0000 | [diff] [blame] | 739 | // with funny bit widths then this switch statement should be removed. It |
| 740 | // is just here to get the size of the "middle" type back up to something |
| 741 | // that the back ends can handle. |
Reid Spencer | 35c3885 | 2007-03-28 01:36:16 +0000 | [diff] [blame] | 742 | const Type *MiddleType = 0; |
| 743 | switch (Size) { |
| 744 | default: break; |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 745 | case 32: |
| 746 | case 16: |
| 747 | case 8: MiddleType = IntegerType::get(I.getContext(), Size); break; |
Reid Spencer | 35c3885 | 2007-03-28 01:36:16 +0000 | [diff] [blame] | 748 | } |
| 749 | if (MiddleType) { |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 750 | Value *NewTrunc = Builder->CreateTrunc(XorLHS, MiddleType, "sext"); |
Reid Spencer | 35c3885 | 2007-03-28 01:36:16 +0000 | [diff] [blame] | 751 | return new SExtInst(NewTrunc, I.getType(), I.getName()); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 752 | } |
| 753 | } |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 754 | } |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 755 | |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 756 | if (I.getType() == Type::getInt1Ty(I.getContext())) |
Nick Lewycky | 9419ddb | 2008-05-31 17:59:52 +0000 | [diff] [blame] | 757 | return BinaryOperator::CreateXor(LHS, RHS); |
| 758 | |
Nick Lewycky | 7d26bd8 | 2008-05-23 04:39:38 +0000 | [diff] [blame] | 759 | // X + X --> X << 1 |
Nick Lewycky | 9419ddb | 2008-05-31 17:59:52 +0000 | [diff] [blame] | 760 | if (I.getType()->isInteger()) { |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 761 | if (Instruction *Result = AssociativeOpt(I, AddRHS(RHS))) |
Owen Anderson | d672ecb | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 762 | return Result; |
Chris Lattner | 7edc8c2 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 763 | |
| 764 | if (Instruction *RHSI = dyn_cast<Instruction>(RHS)) { |
| 765 | if (RHSI->getOpcode() == Instruction::Sub) |
| 766 | if (LHS == RHSI->getOperand(1)) // A + (B - A) --> B |
| 767 | return ReplaceInstUsesWith(I, RHSI->getOperand(0)); |
| 768 | } |
| 769 | if (Instruction *LHSI = dyn_cast<Instruction>(LHS)) { |
| 770 | if (LHSI->getOpcode() == Instruction::Sub) |
| 771 | if (RHS == LHSI->getOperand(1)) // (B - A) + A --> B |
| 772 | return ReplaceInstUsesWith(I, LHSI->getOperand(0)); |
| 773 | } |
Robert Bocchino | 7169828 | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 774 | } |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 775 | |
Chris Lattner | 5c4afb9 | 2002-05-08 22:46:53 +0000 | [diff] [blame] | 776 | // -A + B --> B - A |
Chris Lattner | dd12f96 | 2008-02-17 21:03:36 +0000 | [diff] [blame] | 777 | // -A + -B --> -(A + B) |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 778 | if (Value *LHSV = dyn_castNegVal(LHS)) { |
Chris Lattner | e10c0b9 | 2008-02-18 17:50:16 +0000 | [diff] [blame] | 779 | if (LHS->getType()->isIntOrIntVector()) { |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 780 | if (Value *RHSV = dyn_castNegVal(RHS)) { |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 781 | Value *NewAdd = Builder->CreateAdd(LHSV, RHSV, "sum"); |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 782 | return BinaryOperator::CreateNeg(NewAdd); |
Chris Lattner | e10c0b9 | 2008-02-18 17:50:16 +0000 | [diff] [blame] | 783 | } |
Chris Lattner | dd12f96 | 2008-02-17 21:03:36 +0000 | [diff] [blame] | 784 | } |
| 785 | |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 786 | return BinaryOperator::CreateSub(RHS, LHSV); |
Chris Lattner | dd12f96 | 2008-02-17 21:03:36 +0000 | [diff] [blame] | 787 | } |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 788 | |
| 789 | // A + -B --> A - B |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 790 | if (!isa<Constant>(RHS)) |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 791 | if (Value *V = dyn_castNegVal(RHS)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 792 | return BinaryOperator::CreateSub(LHS, V); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 793 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 794 | |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 795 | ConstantInt *C2; |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 796 | if (Value *X = dyn_castFoldableMul(LHS, C2)) { |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 797 | if (X == RHS) // X*C + X --> X * (C+1) |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 798 | return BinaryOperator::CreateMul(RHS, AddOne(C2)); |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 799 | |
| 800 | // X*C1 + X*C2 --> X * (C1+C2) |
| 801 | ConstantInt *C1; |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 802 | if (X == dyn_castFoldableMul(RHS, C1)) |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 803 | return BinaryOperator::CreateMul(X, ConstantExpr::getAdd(C1, C2)); |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | // X + X*C --> X * (C+1) |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 807 | if (dyn_castFoldableMul(RHS, C2) == LHS) |
| 808 | return BinaryOperator::CreateMul(LHS, AddOne(C2)); |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 809 | |
Chris Lattner | e617c9e | 2007-01-05 02:17:46 +0000 | [diff] [blame] | 810 | // X + ~X --> -1 since ~X = -X-1 |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 811 | if (dyn_castNotVal(LHS) == RHS || |
| 812 | dyn_castNotVal(RHS) == LHS) |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 813 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | e617c9e | 2007-01-05 02:17:46 +0000 | [diff] [blame] | 814 | |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 815 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 816 | // (A & C1)+(B & C2) --> (A & C1)|(B & C2) iff C1&C2 == 0 |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 817 | if (match(RHS, m_And(m_Value(), m_ConstantInt(C2)))) |
| 818 | if (Instruction *R = AssociativeOpt(I, AddMaskingAnd(C2))) |
Chris Lattner | e617c9e | 2007-01-05 02:17:46 +0000 | [diff] [blame] | 819 | return R; |
Chris Lattner | 5e0d718 | 2008-05-19 20:01:56 +0000 | [diff] [blame] | 820 | |
| 821 | // A+B --> A|B iff A and B have no bits set in common. |
| 822 | if (const IntegerType *IT = dyn_cast<IntegerType>(I.getType())) { |
| 823 | APInt Mask = APInt::getAllOnesValue(IT->getBitWidth()); |
| 824 | APInt LHSKnownOne(IT->getBitWidth(), 0); |
| 825 | APInt LHSKnownZero(IT->getBitWidth(), 0); |
| 826 | ComputeMaskedBits(LHS, Mask, LHSKnownZero, LHSKnownOne); |
| 827 | if (LHSKnownZero != 0) { |
| 828 | APInt RHSKnownOne(IT->getBitWidth(), 0); |
| 829 | APInt RHSKnownZero(IT->getBitWidth(), 0); |
| 830 | ComputeMaskedBits(RHS, Mask, RHSKnownZero, RHSKnownOne); |
| 831 | |
| 832 | // No bits in common -> bitwise or. |
Chris Lattner | 9d60ba9 | 2008-05-19 20:03:53 +0000 | [diff] [blame] | 833 | if ((LHSKnownZero|RHSKnownZero).isAllOnesValue()) |
Chris Lattner | 5e0d718 | 2008-05-19 20:01:56 +0000 | [diff] [blame] | 834 | return BinaryOperator::CreateOr(LHS, RHS); |
Chris Lattner | 5e0d718 | 2008-05-19 20:01:56 +0000 | [diff] [blame] | 835 | } |
| 836 | } |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 837 | |
Nick Lewycky | b6eabff | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 838 | // W*X + Y*Z --> W * (X+Z) iff W == Y |
Nick Lewycky | 0c2c3f6 | 2008-02-03 08:19:11 +0000 | [diff] [blame] | 839 | if (I.getType()->isIntOrIntVector()) { |
Nick Lewycky | b6eabff | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 840 | Value *W, *X, *Y, *Z; |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 841 | if (match(LHS, m_Mul(m_Value(W), m_Value(X))) && |
| 842 | match(RHS, m_Mul(m_Value(Y), m_Value(Z)))) { |
Nick Lewycky | b6eabff | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 843 | if (W != Y) { |
| 844 | if (W == Z) { |
Bill Wendling | 587c01d | 2008-02-26 10:53:30 +0000 | [diff] [blame] | 845 | std::swap(Y, Z); |
Nick Lewycky | b6eabff | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 846 | } else if (Y == X) { |
Bill Wendling | 587c01d | 2008-02-26 10:53:30 +0000 | [diff] [blame] | 847 | std::swap(W, X); |
| 848 | } else if (X == Z) { |
Nick Lewycky | b6eabff | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 849 | std::swap(Y, Z); |
| 850 | std::swap(W, X); |
| 851 | } |
| 852 | } |
| 853 | |
| 854 | if (W == Y) { |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 855 | Value *NewAdd = Builder->CreateAdd(X, Z, LHS->getName()); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 856 | return BinaryOperator::CreateMul(W, NewAdd); |
Nick Lewycky | b6eabff | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 857 | } |
| 858 | } |
| 859 | } |
| 860 | |
Chris Lattner | 6b03205 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 861 | if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) { |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 862 | Value *X = 0; |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 863 | if (match(LHS, m_Not(m_Value(X)))) // ~X + C --> (C-1) - X |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 864 | return BinaryOperator::CreateSub(SubOne(CRHS), X); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 865 | |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 866 | // (X & FF00) + xx00 -> (X+xx00) & FF00 |
Owen Anderson | c7d2ce7 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 867 | if (LHS->hasOneUse() && |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 868 | match(LHS, m_And(m_Value(X), m_ConstantInt(C2)))) { |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 869 | Constant *Anded = ConstantExpr::getAnd(CRHS, C2); |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 870 | if (Anded == CRHS) { |
| 871 | // See if all bits from the first bit set in the Add RHS up are included |
| 872 | // in the mask. First, get the rightmost bit. |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 873 | const APInt& AddRHSV = CRHS->getValue(); |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 874 | |
| 875 | // Form a mask of all bits from the lowest bit added through the top. |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 876 | APInt AddRHSHighBits(~((AddRHSV & -AddRHSV)-1)); |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 877 | |
| 878 | // See if the and mask includes all of these bits. |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 879 | APInt AddRHSHighBitsAnd(AddRHSHighBits & C2->getValue()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 880 | |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 881 | if (AddRHSHighBits == AddRHSHighBitsAnd) { |
| 882 | // Okay, the xform is safe. Insert the new add pronto. |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 883 | Value *NewAdd = Builder->CreateAdd(X, CRHS, LHS->getName()); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 884 | return BinaryOperator::CreateAnd(NewAdd, C2); |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 885 | } |
| 886 | } |
| 887 | } |
| 888 | |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 889 | // Try to fold constant add into select arguments. |
| 890 | if (SelectInst *SI = dyn_cast<SelectInst>(LHS)) |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame^] | 891 | if (Instruction *R = FoldOpIntoSelect(I, SI)) |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 892 | return R; |
Chris Lattner | 6b03205 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 893 | } |
| 894 | |
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 895 | // add (select X 0 (sub n A)) A --> select X A n |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 896 | { |
| 897 | SelectInst *SI = dyn_cast<SelectInst>(LHS); |
Chris Lattner | 6046fb7 | 2008-11-16 04:46:19 +0000 | [diff] [blame] | 898 | Value *A = RHS; |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 899 | if (!SI) { |
| 900 | SI = dyn_cast<SelectInst>(RHS); |
Chris Lattner | 6046fb7 | 2008-11-16 04:46:19 +0000 | [diff] [blame] | 901 | A = LHS; |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 902 | } |
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 903 | if (SI && SI->hasOneUse()) { |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 904 | Value *TV = SI->getTrueValue(); |
| 905 | Value *FV = SI->getFalseValue(); |
Chris Lattner | 6046fb7 | 2008-11-16 04:46:19 +0000 | [diff] [blame] | 906 | Value *N; |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 907 | |
| 908 | // Can we fold the add into the argument of the select? |
| 909 | // We check both true and false select arguments for a matching subtract. |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 910 | if (match(FV, m_Zero()) && |
| 911 | match(TV, m_Sub(m_Value(N), m_Specific(A)))) |
Chris Lattner | 6046fb7 | 2008-11-16 04:46:19 +0000 | [diff] [blame] | 912 | // Fold the add into the true select value. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 913 | return SelectInst::Create(SI->getCondition(), N, A); |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 914 | if (match(TV, m_Zero()) && |
| 915 | match(FV, m_Sub(m_Value(N), m_Specific(A)))) |
Chris Lattner | 6046fb7 | 2008-11-16 04:46:19 +0000 | [diff] [blame] | 916 | // Fold the add into the false select value. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 917 | return SelectInst::Create(SI->getCondition(), A, N); |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 918 | } |
| 919 | } |
Andrew Lenharth | 16d7955 | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 920 | |
Chris Lattner | 3d28b1b | 2008-05-20 05:46:13 +0000 | [diff] [blame] | 921 | // Check for (add (sext x), y), see if we can merge this into an |
| 922 | // integer add followed by a sext. |
| 923 | if (SExtInst *LHSConv = dyn_cast<SExtInst>(LHS)) { |
| 924 | // (add (sext x), cst) --> (sext (add x, cst')) |
| 925 | if (ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS)) { |
| 926 | Constant *CI = |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 927 | ConstantExpr::getTrunc(RHSC, LHSConv->getOperand(0)->getType()); |
Chris Lattner | 3d28b1b | 2008-05-20 05:46:13 +0000 | [diff] [blame] | 928 | if (LHSConv->hasOneUse() && |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 929 | ConstantExpr::getSExt(CI, I.getType()) == RHSC && |
Chris Lattner | 3d28b1b | 2008-05-20 05:46:13 +0000 | [diff] [blame] | 930 | WillNotOverflowSignedAdd(LHSConv->getOperand(0), CI)) { |
| 931 | // Insert the new, smaller add. |
Dan Gohman | fe35955 | 2009-10-26 22:14:22 +0000 | [diff] [blame] | 932 | Value *NewAdd = Builder->CreateNSWAdd(LHSConv->getOperand(0), |
| 933 | CI, "addconv"); |
Chris Lattner | 3d28b1b | 2008-05-20 05:46:13 +0000 | [diff] [blame] | 934 | return new SExtInst(NewAdd, I.getType()); |
| 935 | } |
| 936 | } |
| 937 | |
| 938 | // (add (sext x), (sext y)) --> (sext (add int x, y)) |
| 939 | if (SExtInst *RHSConv = dyn_cast<SExtInst>(RHS)) { |
| 940 | // Only do this if x/y have the same type, if at last one of them has a |
| 941 | // single use (so we don't increase the number of sexts), and if the |
| 942 | // integer add will not overflow. |
| 943 | if (LHSConv->getOperand(0)->getType()==RHSConv->getOperand(0)->getType()&& |
| 944 | (LHSConv->hasOneUse() || RHSConv->hasOneUse()) && |
| 945 | WillNotOverflowSignedAdd(LHSConv->getOperand(0), |
| 946 | RHSConv->getOperand(0))) { |
| 947 | // Insert the new integer add. |
Dan Gohman | fe35955 | 2009-10-26 22:14:22 +0000 | [diff] [blame] | 948 | Value *NewAdd = Builder->CreateNSWAdd(LHSConv->getOperand(0), |
| 949 | RHSConv->getOperand(0), "addconv"); |
Chris Lattner | 3d28b1b | 2008-05-20 05:46:13 +0000 | [diff] [blame] | 950 | return new SExtInst(NewAdd, I.getType()); |
| 951 | } |
| 952 | } |
| 953 | } |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 954 | |
| 955 | return Changed ? &I : 0; |
| 956 | } |
| 957 | |
| 958 | Instruction *InstCombiner::visitFAdd(BinaryOperator &I) { |
| 959 | bool Changed = SimplifyCommutative(I); |
| 960 | Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); |
| 961 | |
| 962 | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { |
| 963 | // X + 0 --> X |
| 964 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) { |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 965 | if (CFP->isExactlyValue(ConstantFP::getNegativeZero |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 966 | (I.getType())->getValueAPF())) |
| 967 | return ReplaceInstUsesWith(I, LHS); |
| 968 | } |
| 969 | |
| 970 | if (isa<PHINode>(LHS)) |
| 971 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 972 | return NV; |
| 973 | } |
| 974 | |
| 975 | // -A + B --> B - A |
| 976 | // -A + -B --> -(A + B) |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 977 | if (Value *LHSV = dyn_castFNegVal(LHS)) |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 978 | return BinaryOperator::CreateFSub(RHS, LHSV); |
| 979 | |
| 980 | // A + -B --> A - B |
| 981 | if (!isa<Constant>(RHS)) |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 982 | if (Value *V = dyn_castFNegVal(RHS)) |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 983 | return BinaryOperator::CreateFSub(LHS, V); |
| 984 | |
| 985 | // Check for X+0.0. Simplify it to X if we know X is not -0.0. |
| 986 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHS)) |
| 987 | if (CFP->getValueAPF().isPosZero() && CannotBeNegativeZero(LHS)) |
| 988 | return ReplaceInstUsesWith(I, LHS); |
| 989 | |
Chris Lattner | 3d28b1b | 2008-05-20 05:46:13 +0000 | [diff] [blame] | 990 | // Check for (add double (sitofp x), y), see if we can merge this into an |
| 991 | // integer add followed by a promotion. |
| 992 | if (SIToFPInst *LHSConv = dyn_cast<SIToFPInst>(LHS)) { |
| 993 | // (add double (sitofp x), fpcst) --> (sitofp (add int x, intcst)) |
| 994 | // ... if the constant fits in the integer value. This is useful for things |
| 995 | // like (double)(x & 1234) + 4.0 -> (double)((X & 1234)+4) which no longer |
| 996 | // requires a constant pool load, and generally allows the add to be better |
| 997 | // instcombined. |
| 998 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHS)) { |
| 999 | Constant *CI = |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1000 | ConstantExpr::getFPToSI(CFP, LHSConv->getOperand(0)->getType()); |
Chris Lattner | 3d28b1b | 2008-05-20 05:46:13 +0000 | [diff] [blame] | 1001 | if (LHSConv->hasOneUse() && |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1002 | ConstantExpr::getSIToFP(CI, I.getType()) == CFP && |
Chris Lattner | 3d28b1b | 2008-05-20 05:46:13 +0000 | [diff] [blame] | 1003 | WillNotOverflowSignedAdd(LHSConv->getOperand(0), CI)) { |
| 1004 | // Insert the new integer add. |
Dan Gohman | fe35955 | 2009-10-26 22:14:22 +0000 | [diff] [blame] | 1005 | Value *NewAdd = Builder->CreateNSWAdd(LHSConv->getOperand(0), |
| 1006 | CI, "addconv"); |
Chris Lattner | 3d28b1b | 2008-05-20 05:46:13 +0000 | [diff] [blame] | 1007 | return new SIToFPInst(NewAdd, I.getType()); |
| 1008 | } |
| 1009 | } |
| 1010 | |
| 1011 | // (add double (sitofp x), (sitofp y)) --> (sitofp (add int x, y)) |
| 1012 | if (SIToFPInst *RHSConv = dyn_cast<SIToFPInst>(RHS)) { |
| 1013 | // Only do this if x/y have the same type, if at last one of them has a |
| 1014 | // single use (so we don't increase the number of int->fp conversions), |
| 1015 | // and if the integer add will not overflow. |
| 1016 | if (LHSConv->getOperand(0)->getType()==RHSConv->getOperand(0)->getType()&& |
| 1017 | (LHSConv->hasOneUse() || RHSConv->hasOneUse()) && |
| 1018 | WillNotOverflowSignedAdd(LHSConv->getOperand(0), |
| 1019 | RHSConv->getOperand(0))) { |
| 1020 | // Insert the new integer add. |
Dan Gohman | fe35955 | 2009-10-26 22:14:22 +0000 | [diff] [blame] | 1021 | Value *NewAdd = Builder->CreateNSWAdd(LHSConv->getOperand(0), |
Chris Lattner | 092543c | 2009-11-04 08:05:20 +0000 | [diff] [blame] | 1022 | RHSConv->getOperand(0),"addconv"); |
Chris Lattner | 3d28b1b | 2008-05-20 05:46:13 +0000 | [diff] [blame] | 1023 | return new SIToFPInst(NewAdd, I.getType()); |
| 1024 | } |
| 1025 | } |
| 1026 | } |
| 1027 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1028 | return Changed ? &I : 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1029 | } |
| 1030 | |
Chris Lattner | 092543c | 2009-11-04 08:05:20 +0000 | [diff] [blame] | 1031 | |
| 1032 | /// EmitGEPOffset - Given a getelementptr instruction/constantexpr, emit the |
| 1033 | /// code necessary to compute the offset from the base pointer (without adding |
| 1034 | /// in the base pointer). Return the result as a signed integer of intptr size. |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1035 | Value *InstCombiner::EmitGEPOffset(User *GEP) { |
| 1036 | TargetData &TD = *getTargetData(); |
Chris Lattner | 092543c | 2009-11-04 08:05:20 +0000 | [diff] [blame] | 1037 | gep_type_iterator GTI = gep_type_begin(GEP); |
| 1038 | const Type *IntPtrTy = TD.getIntPtrType(GEP->getContext()); |
| 1039 | Value *Result = Constant::getNullValue(IntPtrTy); |
| 1040 | |
| 1041 | // Build a mask for high order bits. |
| 1042 | unsigned IntPtrWidth = TD.getPointerSizeInBits(); |
| 1043 | uint64_t PtrSizeMask = ~0ULL >> (64-IntPtrWidth); |
| 1044 | |
| 1045 | for (User::op_iterator i = GEP->op_begin() + 1, e = GEP->op_end(); i != e; |
| 1046 | ++i, ++GTI) { |
| 1047 | Value *Op = *i; |
| 1048 | uint64_t Size = TD.getTypeAllocSize(GTI.getIndexedType()) & PtrSizeMask; |
| 1049 | if (ConstantInt *OpC = dyn_cast<ConstantInt>(Op)) { |
| 1050 | if (OpC->isZero()) continue; |
| 1051 | |
| 1052 | // Handle a struct index, which adds its field offset to the pointer. |
| 1053 | if (const StructType *STy = dyn_cast<StructType>(*GTI)) { |
| 1054 | Size = TD.getStructLayout(STy)->getElementOffset(OpC->getZExtValue()); |
| 1055 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1056 | Result = Builder->CreateAdd(Result, |
| 1057 | ConstantInt::get(IntPtrTy, Size), |
| 1058 | GEP->getName()+".offs"); |
Chris Lattner | 092543c | 2009-11-04 08:05:20 +0000 | [diff] [blame] | 1059 | continue; |
| 1060 | } |
| 1061 | |
| 1062 | Constant *Scale = ConstantInt::get(IntPtrTy, Size); |
| 1063 | Constant *OC = |
| 1064 | ConstantExpr::getIntegerCast(OpC, IntPtrTy, true /*SExt*/); |
| 1065 | Scale = ConstantExpr::getMul(OC, Scale); |
| 1066 | // Emit an add instruction. |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1067 | Result = Builder->CreateAdd(Result, Scale, GEP->getName()+".offs"); |
Chris Lattner | 092543c | 2009-11-04 08:05:20 +0000 | [diff] [blame] | 1068 | continue; |
| 1069 | } |
| 1070 | // Convert to correct type. |
| 1071 | if (Op->getType() != IntPtrTy) |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1072 | Op = Builder->CreateIntCast(Op, IntPtrTy, true, Op->getName()+".c"); |
Chris Lattner | 092543c | 2009-11-04 08:05:20 +0000 | [diff] [blame] | 1073 | if (Size != 1) { |
| 1074 | Constant *Scale = ConstantInt::get(IntPtrTy, Size); |
| 1075 | // We'll let instcombine(mul) convert this to a shl if possible. |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1076 | Op = Builder->CreateMul(Op, Scale, GEP->getName()+".idx"); |
Chris Lattner | 092543c | 2009-11-04 08:05:20 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | // Emit an add instruction. |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1080 | Result = Builder->CreateAdd(Op, Result, GEP->getName()+".offs"); |
Chris Lattner | 092543c | 2009-11-04 08:05:20 +0000 | [diff] [blame] | 1081 | } |
| 1082 | return Result; |
| 1083 | } |
| 1084 | |
| 1085 | |
Chris Lattner | 092543c | 2009-11-04 08:05:20 +0000 | [diff] [blame] | 1086 | |
| 1087 | |
| 1088 | /// Optimize pointer differences into the same array into a size. Consider: |
| 1089 | /// &A[10] - &A[0]: we should compile this to "10". LHS/RHS are the pointer |
| 1090 | /// operands to the ptrtoint instructions for the LHS/RHS of the subtract. |
| 1091 | /// |
| 1092 | Value *InstCombiner::OptimizePointerDifference(Value *LHS, Value *RHS, |
| 1093 | const Type *Ty) { |
| 1094 | assert(TD && "Must have target data info for this"); |
| 1095 | |
| 1096 | // If LHS is a gep based on RHS or RHS is a gep based on LHS, we can optimize |
| 1097 | // this. |
| 1098 | bool Swapped; |
Chris Lattner | 85c1c96 | 2010-01-01 22:42:29 +0000 | [diff] [blame] | 1099 | GetElementPtrInst *GEP = 0; |
| 1100 | ConstantExpr *CstGEP = 0; |
Chris Lattner | 092543c | 2009-11-04 08:05:20 +0000 | [diff] [blame] | 1101 | |
Chris Lattner | 85c1c96 | 2010-01-01 22:42:29 +0000 | [diff] [blame] | 1102 | // TODO: Could also optimize &A[i] - &A[j] -> "i-j", and "&A.foo[i] - &A.foo". |
| 1103 | // For now we require one side to be the base pointer "A" or a constant |
| 1104 | // expression derived from it. |
| 1105 | if (GetElementPtrInst *LHSGEP = dyn_cast<GetElementPtrInst>(LHS)) { |
| 1106 | // (gep X, ...) - X |
| 1107 | if (LHSGEP->getOperand(0) == RHS) { |
| 1108 | GEP = LHSGEP; |
| 1109 | Swapped = false; |
| 1110 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(RHS)) { |
| 1111 | // (gep X, ...) - (ce_gep X, ...) |
| 1112 | if (CE->getOpcode() == Instruction::GetElementPtr && |
| 1113 | LHSGEP->getOperand(0) == CE->getOperand(0)) { |
| 1114 | CstGEP = CE; |
| 1115 | GEP = LHSGEP; |
| 1116 | Swapped = false; |
| 1117 | } |
| 1118 | } |
| 1119 | } |
| 1120 | |
| 1121 | if (GetElementPtrInst *RHSGEP = dyn_cast<GetElementPtrInst>(RHS)) { |
| 1122 | // X - (gep X, ...) |
| 1123 | if (RHSGEP->getOperand(0) == LHS) { |
| 1124 | GEP = RHSGEP; |
| 1125 | Swapped = true; |
| 1126 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(LHS)) { |
| 1127 | // (ce_gep X, ...) - (gep X, ...) |
| 1128 | if (CE->getOpcode() == Instruction::GetElementPtr && |
| 1129 | RHSGEP->getOperand(0) == CE->getOperand(0)) { |
| 1130 | CstGEP = CE; |
| 1131 | GEP = RHSGEP; |
| 1132 | Swapped = true; |
| 1133 | } |
| 1134 | } |
| 1135 | } |
| 1136 | |
| 1137 | if (GEP == 0) |
Chris Lattner | 092543c | 2009-11-04 08:05:20 +0000 | [diff] [blame] | 1138 | return 0; |
| 1139 | |
Chris Lattner | 092543c | 2009-11-04 08:05:20 +0000 | [diff] [blame] | 1140 | // Emit the offset of the GEP and an intptr_t. |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1141 | Value *Result = EmitGEPOffset(GEP); |
Chris Lattner | 85c1c96 | 2010-01-01 22:42:29 +0000 | [diff] [blame] | 1142 | |
| 1143 | // If we had a constant expression GEP on the other side offsetting the |
| 1144 | // pointer, subtract it from the offset we have. |
| 1145 | if (CstGEP) { |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1146 | Value *CstOffset = EmitGEPOffset(CstGEP); |
Chris Lattner | 85c1c96 | 2010-01-01 22:42:29 +0000 | [diff] [blame] | 1147 | Result = Builder->CreateSub(Result, CstOffset); |
| 1148 | } |
| 1149 | |
Chris Lattner | 092543c | 2009-11-04 08:05:20 +0000 | [diff] [blame] | 1150 | |
| 1151 | // If we have p - gep(p, ...) then we have to negate the result. |
| 1152 | if (Swapped) |
| 1153 | Result = Builder->CreateNeg(Result, "diff.neg"); |
| 1154 | |
| 1155 | return Builder->CreateIntCast(Result, Ty, true); |
| 1156 | } |
| 1157 | |
| 1158 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1159 | Instruction *InstCombiner::visitSub(BinaryOperator &I) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1160 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1161 | |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1162 | if (Op0 == Op1) // sub X, X -> 0 |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1163 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1164 | |
Chris Lattner | 3bf6815 | 2009-12-21 04:04:05 +0000 | [diff] [blame] | 1165 | // If this is a 'B = x-(-A)', change to B = x+A. This preserves NSW/NUW. |
| 1166 | if (Value *V = dyn_castNegVal(Op1)) { |
| 1167 | BinaryOperator *Res = BinaryOperator::CreateAdd(Op0, V); |
| 1168 | Res->setHasNoSignedWrap(I.hasNoSignedWrap()); |
| 1169 | Res->setHasNoUnsignedWrap(I.hasNoUnsignedWrap()); |
| 1170 | return Res; |
| 1171 | } |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 1172 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 1173 | if (isa<UndefValue>(Op0)) |
| 1174 | return ReplaceInstUsesWith(I, Op0); // undef - X -> undef |
| 1175 | if (isa<UndefValue>(Op1)) |
| 1176 | return ReplaceInstUsesWith(I, Op1); // X - undef -> undef |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 1177 | if (I.getType() == Type::getInt1Ty(I.getContext())) |
Chris Lattner | 092543c | 2009-11-04 08:05:20 +0000 | [diff] [blame] | 1178 | return BinaryOperator::CreateXor(Op0, Op1); |
| 1179 | |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 1180 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op0)) { |
Chris Lattner | 092543c | 2009-11-04 08:05:20 +0000 | [diff] [blame] | 1181 | // Replace (-1 - A) with (~A). |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1182 | if (C->isAllOnesValue()) |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 1183 | return BinaryOperator::CreateNot(Op1); |
Chris Lattner | 4037171 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 1184 | |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 1185 | // C - ~X == X + (1+C) |
Reid Spencer | 4b828e6 | 2005-06-18 17:37:34 +0000 | [diff] [blame] | 1186 | Value *X = 0; |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 1187 | if (match(Op1, m_Not(m_Value(X)))) |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 1188 | return BinaryOperator::CreateAdd(X, AddOne(C)); |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 1189 | |
Chris Lattner | 76b7a06 | 2007-01-15 07:02:54 +0000 | [diff] [blame] | 1190 | // -(X >>u 31) -> (X >>s 31) |
| 1191 | // -(X >>s 31) -> (X >>u 31) |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 1192 | if (C->isZero()) { |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 1193 | if (BinaryOperator *SI = dyn_cast<BinaryOperator>(Op1)) { |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1194 | if (SI->getOpcode() == Instruction::LShr) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1195 | if (ConstantInt *CU = dyn_cast<ConstantInt>(SI->getOperand(1))) { |
Chris Lattner | 9c29067 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 1196 | // Check to see if we are shifting out everything but the sign bit. |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 1197 | if (CU->getLimitedValue(SI->getType()->getPrimitiveSizeInBits()) == |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1198 | SI->getType()->getPrimitiveSizeInBits()-1) { |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1199 | // Ok, the transformation is safe. Insert AShr. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1200 | return BinaryOperator::Create(Instruction::AShr, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1201 | SI->getOperand(0), CU, SI->getName()); |
Chris Lattner | 9c29067 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 1202 | } |
| 1203 | } |
Chris Lattner | 092543c | 2009-11-04 08:05:20 +0000 | [diff] [blame] | 1204 | } else if (SI->getOpcode() == Instruction::AShr) { |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1205 | if (ConstantInt *CU = dyn_cast<ConstantInt>(SI->getOperand(1))) { |
| 1206 | // Check to see if we are shifting out everything but the sign bit. |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 1207 | if (CU->getLimitedValue(SI->getType()->getPrimitiveSizeInBits()) == |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1208 | SI->getType()->getPrimitiveSizeInBits()-1) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 1209 | // Ok, the transformation is safe. Insert LShr. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1210 | return BinaryOperator::CreateLShr( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1211 | SI->getOperand(0), CU, SI->getName()); |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1212 | } |
| 1213 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 1214 | } |
| 1215 | } |
Chris Lattner | bfe492b | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 1216 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1217 | |
| 1218 | // Try to fold constant sub into select arguments. |
| 1219 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame^] | 1220 | if (Instruction *R = FoldOpIntoSelect(I, SI)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1221 | return R; |
Eli Friedman | 709b33d | 2009-07-13 22:27:52 +0000 | [diff] [blame] | 1222 | |
| 1223 | // C - zext(bool) -> bool ? C - 1 : C |
| 1224 | if (ZExtInst *ZI = dyn_cast<ZExtInst>(Op1)) |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 1225 | if (ZI->getSrcTy() == Type::getInt1Ty(I.getContext())) |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 1226 | return SelectInst::Create(ZI->getOperand(0), SubOne(C), C); |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 1229 | if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) { |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1230 | if (Op1I->getOpcode() == Instruction::Add) { |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 1231 | if (Op1I->getOperand(0) == Op0) // X-(X+Y) == -Y |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 1232 | return BinaryOperator::CreateNeg(Op1I->getOperand(1), |
Owen Anderson | 0a5372e | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1233 | I.getName()); |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 1234 | else if (Op1I->getOperand(1) == Op0) // X-(Y+X) == -Y |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 1235 | return BinaryOperator::CreateNeg(Op1I->getOperand(0), |
Owen Anderson | 0a5372e | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1236 | I.getName()); |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 1237 | else if (ConstantInt *CI1 = dyn_cast<ConstantInt>(I.getOperand(0))) { |
| 1238 | if (ConstantInt *CI2 = dyn_cast<ConstantInt>(Op1I->getOperand(1))) |
| 1239 | // C1-(X+C2) --> (C1-C2)-X |
Owen Anderson | d672ecb | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 1240 | return BinaryOperator::CreateSub( |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1241 | ConstantExpr::getSub(CI1, CI2), Op1I->getOperand(0)); |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 1242 | } |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 1245 | if (Op1I->hasOneUse()) { |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1246 | // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression |
| 1247 | // is not used by anyone else... |
| 1248 | // |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1249 | if (Op1I->getOpcode() == Instruction::Sub) { |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1250 | // Swap the two operands of the subexpr... |
| 1251 | Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1); |
| 1252 | Op1I->setOperand(0, IIOp1); |
| 1253 | Op1I->setOperand(1, IIOp0); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1254 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1255 | // Create the new top level add instruction... |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1256 | return BinaryOperator::CreateAdd(Op0, Op1); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1257 | } |
| 1258 | |
| 1259 | // Replace (A - (A & B)) with (A & ~B) if this is the only use of (A&B)... |
| 1260 | // |
| 1261 | if (Op1I->getOpcode() == Instruction::And && |
| 1262 | (Op1I->getOperand(0) == Op0 || Op1I->getOperand(1) == Op0)) { |
| 1263 | Value *OtherOp = Op1I->getOperand(Op1I->getOperand(0) == Op0); |
| 1264 | |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 1265 | Value *NewNot = Builder->CreateNot(OtherOp, "B.not"); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1266 | return BinaryOperator::CreateAnd(Op0, NewNot); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1267 | } |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 1268 | |
Reid Spencer | ac5209e | 2006-10-16 23:08:08 +0000 | [diff] [blame] | 1269 | // 0 - (X sdiv C) -> (X sdiv -C) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1270 | if (Op1I->getOpcode() == Instruction::SDiv) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1271 | if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0)) |
Zhou Sheng | 843f0767 | 2007-04-19 05:39:12 +0000 | [diff] [blame] | 1272 | if (CSI->isZero()) |
Chris Lattner | 91ccc15 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 1273 | if (Constant *DivRHS = dyn_cast<Constant>(Op1I->getOperand(1))) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1274 | return BinaryOperator::CreateSDiv(Op1I->getOperand(0), |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1275 | ConstantExpr::getNeg(DivRHS)); |
Chris Lattner | 91ccc15 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 1276 | |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 1277 | // X - X*C --> X * (1-C) |
Reid Spencer | 4b828e6 | 2005-06-18 17:37:34 +0000 | [diff] [blame] | 1278 | ConstantInt *C2 = 0; |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 1279 | if (dyn_castFoldableMul(Op1I, C2) == Op0) { |
Owen Anderson | d672ecb | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 1280 | Constant *CP1 = |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1281 | ConstantExpr::getSub(ConstantInt::get(I.getType(), 1), |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1282 | C2); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1283 | return BinaryOperator::CreateMul(Op0, CP1); |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 1284 | } |
Chris Lattner | 4037171 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 1285 | } |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 1286 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1287 | |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1288 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) { |
| 1289 | if (Op0I->getOpcode() == Instruction::Add) { |
| 1290 | if (Op0I->getOperand(0) == Op1) // (Y+X)-Y == X |
| 1291 | return ReplaceInstUsesWith(I, Op0I->getOperand(1)); |
| 1292 | else if (Op0I->getOperand(1) == Op1) // (X+Y)-Y == X |
| 1293 | return ReplaceInstUsesWith(I, Op0I->getOperand(0)); |
| 1294 | } else if (Op0I->getOpcode() == Instruction::Sub) { |
| 1295 | if (Op0I->getOperand(0) == Op1) // (X-Y)-X == -Y |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 1296 | return BinaryOperator::CreateNeg(Op0I->getOperand(1), |
Owen Anderson | 0a5372e | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1297 | I.getName()); |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 1298 | } |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1299 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1300 | |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 1301 | ConstantInt *C1; |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 1302 | if (Value *X = dyn_castFoldableMul(Op0, C1)) { |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 1303 | if (X == Op1) // X*C - X --> X * (C-1) |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 1304 | return BinaryOperator::CreateMul(Op1, SubOne(C1)); |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 1305 | |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 1306 | ConstantInt *C2; // X*C1 - X*C2 -> X * (C1-C2) |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 1307 | if (X == dyn_castFoldableMul(Op1, C2)) |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1308 | return BinaryOperator::CreateMul(X, ConstantExpr::getSub(C1, C2)); |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 1309 | } |
Chris Lattner | 092543c | 2009-11-04 08:05:20 +0000 | [diff] [blame] | 1310 | |
| 1311 | // Optimize pointer differences into the same array into a size. Consider: |
| 1312 | // &A[10] - &A[0]: we should compile this to "10". |
| 1313 | if (TD) { |
Chris Lattner | 3376718 | 2010-01-01 22:12:03 +0000 | [diff] [blame] | 1314 | Value *LHSOp, *RHSOp; |
Chris Lattner | f2ebc68 | 2010-01-01 22:29:12 +0000 | [diff] [blame] | 1315 | if (match(Op0, m_PtrToInt(m_Value(LHSOp))) && |
| 1316 | match(Op1, m_PtrToInt(m_Value(RHSOp)))) |
Chris Lattner | 3376718 | 2010-01-01 22:12:03 +0000 | [diff] [blame] | 1317 | if (Value *Res = OptimizePointerDifference(LHSOp, RHSOp, I.getType())) |
| 1318 | return ReplaceInstUsesWith(I, Res); |
Chris Lattner | 092543c | 2009-11-04 08:05:20 +0000 | [diff] [blame] | 1319 | |
| 1320 | // trunc(p)-trunc(q) -> trunc(p-q) |
Chris Lattner | f2ebc68 | 2010-01-01 22:29:12 +0000 | [diff] [blame] | 1321 | if (match(Op0, m_Trunc(m_PtrToInt(m_Value(LHSOp)))) && |
| 1322 | match(Op1, m_Trunc(m_PtrToInt(m_Value(RHSOp))))) |
| 1323 | if (Value *Res = OptimizePointerDifference(LHSOp, RHSOp, I.getType())) |
| 1324 | return ReplaceInstUsesWith(I, Res); |
Chris Lattner | 092543c | 2009-11-04 08:05:20 +0000 | [diff] [blame] | 1325 | } |
| 1326 | |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1327 | return 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1328 | } |
| 1329 | |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1330 | Instruction *InstCombiner::visitFSub(BinaryOperator &I) { |
| 1331 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 1332 | |
| 1333 | // If this is a 'B = x-(-A)', change to B = x+A... |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 1334 | if (Value *V = dyn_castFNegVal(Op1)) |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1335 | return BinaryOperator::CreateFAdd(Op0, V); |
| 1336 | |
| 1337 | if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) { |
| 1338 | if (Op1I->getOpcode() == Instruction::FAdd) { |
| 1339 | if (Op1I->getOperand(0) == Op0) // X-(X+Y) == -Y |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 1340 | return BinaryOperator::CreateFNeg(Op1I->getOperand(1), |
Owen Anderson | 0a5372e | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1341 | I.getName()); |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1342 | else if (Op1I->getOperand(1) == Op0) // X-(Y+X) == -Y |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 1343 | return BinaryOperator::CreateFNeg(Op1I->getOperand(0), |
Owen Anderson | 0a5372e | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1344 | I.getName()); |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1345 | } |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1346 | } |
| 1347 | |
| 1348 | return 0; |
| 1349 | } |
| 1350 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1351 | Instruction *InstCombiner::visitMul(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 1352 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | a249847 | 2009-10-11 21:36:10 +0000 | [diff] [blame] | 1353 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1354 | |
Chris Lattner | a249847 | 2009-10-11 21:36:10 +0000 | [diff] [blame] | 1355 | if (isa<UndefValue>(Op1)) // undef * X -> 0 |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1356 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 1357 | |
Chris Lattner | 8af304a | 2009-10-11 07:53:15 +0000 | [diff] [blame] | 1358 | // Simplify mul instructions with a constant RHS. |
Chris Lattner | a249847 | 2009-10-11 21:36:10 +0000 | [diff] [blame] | 1359 | if (Constant *Op1C = dyn_cast<Constant>(Op1)) { |
| 1360 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1C)) { |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 1361 | |
| 1362 | // ((X << C1)*C2) == (X * (C2 << C1)) |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1363 | if (BinaryOperator *SI = dyn_cast<BinaryOperator>(Op0)) |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 1364 | if (SI->getOpcode() == Instruction::Shl) |
| 1365 | if (Constant *ShOp = dyn_cast<Constant>(SI->getOperand(1))) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1366 | return BinaryOperator::CreateMul(SI->getOperand(0), |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1367 | ConstantExpr::getShl(CI, ShOp)); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1368 | |
Zhou Sheng | 843f0767 | 2007-04-19 05:39:12 +0000 | [diff] [blame] | 1369 | if (CI->isZero()) |
Chris Lattner | a249847 | 2009-10-11 21:36:10 +0000 | [diff] [blame] | 1370 | return ReplaceInstUsesWith(I, Op1C); // X * 0 == 0 |
Chris Lattner | 515c97c | 2003-09-11 22:24:54 +0000 | [diff] [blame] | 1371 | if (CI->equalsInt(1)) // X * 1 == X |
| 1372 | return ReplaceInstUsesWith(I, Op0); |
| 1373 | if (CI->isAllOnesValue()) // X * -1 == 0 - X |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 1374 | return BinaryOperator::CreateNeg(Op0, I.getName()); |
Chris Lattner | 6c1ce21 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 1375 | |
Zhou Sheng | 97b52c2 | 2007-03-29 01:57:21 +0000 | [diff] [blame] | 1376 | const APInt& Val = cast<ConstantInt>(CI)->getValue(); |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 1377 | if (Val.isPowerOf2()) { // Replace X*(2^C) with X << C |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1378 | return BinaryOperator::CreateShl(Op0, |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1379 | ConstantInt::get(Op0->getType(), Val.logBase2())); |
Chris Lattner | bcd7db5 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 1380 | } |
Chris Lattner | a249847 | 2009-10-11 21:36:10 +0000 | [diff] [blame] | 1381 | } else if (isa<VectorType>(Op1C->getType())) { |
| 1382 | if (Op1C->isNullValue()) |
| 1383 | return ReplaceInstUsesWith(I, Op1C); |
Nick Lewycky | 895f085 | 2008-11-27 20:21:08 +0000 | [diff] [blame] | 1384 | |
Chris Lattner | a249847 | 2009-10-11 21:36:10 +0000 | [diff] [blame] | 1385 | if (ConstantVector *Op1V = dyn_cast<ConstantVector>(Op1C)) { |
Nick Lewycky | 895f085 | 2008-11-27 20:21:08 +0000 | [diff] [blame] | 1386 | if (Op1V->isAllOnesValue()) // X * -1 == 0 - X |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 1387 | return BinaryOperator::CreateNeg(Op0, I.getName()); |
Nick Lewycky | 895f085 | 2008-11-27 20:21:08 +0000 | [diff] [blame] | 1388 | |
| 1389 | // As above, vector X*splat(1.0) -> X in all defined cases. |
| 1390 | if (Constant *Splat = Op1V->getSplatValue()) { |
Nick Lewycky | 895f085 | 2008-11-27 20:21:08 +0000 | [diff] [blame] | 1391 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Splat)) |
| 1392 | if (CI->equalsInt(1)) |
| 1393 | return ReplaceInstUsesWith(I, Op0); |
| 1394 | } |
| 1395 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1396 | } |
Chris Lattner | ab51f3f | 2006-03-04 06:04:02 +0000 | [diff] [blame] | 1397 | |
| 1398 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) |
| 1399 | if (Op0I->getOpcode() == Instruction::Add && Op0I->hasOneUse() && |
Chris Lattner | a249847 | 2009-10-11 21:36:10 +0000 | [diff] [blame] | 1400 | isa<ConstantInt>(Op0I->getOperand(1)) && isa<ConstantInt>(Op1C)) { |
Chris Lattner | ab51f3f | 2006-03-04 06:04:02 +0000 | [diff] [blame] | 1401 | // Canonicalize (X+C1)*C2 -> X*C2+C1*C2. |
Chris Lattner | a249847 | 2009-10-11 21:36:10 +0000 | [diff] [blame] | 1402 | Value *Add = Builder->CreateMul(Op0I->getOperand(0), Op1C, "tmp"); |
| 1403 | Value *C1C2 = Builder->CreateMul(Op1C, Op0I->getOperand(1)); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1404 | return BinaryOperator::CreateAdd(Add, C1C2); |
Chris Lattner | ab51f3f | 2006-03-04 06:04:02 +0000 | [diff] [blame] | 1405 | |
| 1406 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1407 | |
| 1408 | // Try to fold constant mul into select arguments. |
| 1409 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame^] | 1410 | if (Instruction *R = FoldOpIntoSelect(I, SI)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1411 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1412 | |
| 1413 | if (isa<PHINode>(Op0)) |
| 1414 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 1415 | return NV; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1416 | } |
| 1417 | |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 1418 | if (Value *Op0v = dyn_castNegVal(Op0)) // -X * -Y = X*Y |
Chris Lattner | a249847 | 2009-10-11 21:36:10 +0000 | [diff] [blame] | 1419 | if (Value *Op1v = dyn_castNegVal(Op1)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1420 | return BinaryOperator::CreateMul(Op0v, Op1v); |
Chris Lattner | a4f445b | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 1421 | |
Nick Lewycky | 0c73079 | 2008-11-21 07:33:58 +0000 | [diff] [blame] | 1422 | // (X / Y) * Y = X - (X % Y) |
| 1423 | // (X / Y) * -Y = (X % Y) - X |
| 1424 | { |
Chris Lattner | a249847 | 2009-10-11 21:36:10 +0000 | [diff] [blame] | 1425 | Value *Op1C = Op1; |
Nick Lewycky | 0c73079 | 2008-11-21 07:33:58 +0000 | [diff] [blame] | 1426 | BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0); |
| 1427 | if (!BO || |
| 1428 | (BO->getOpcode() != Instruction::UDiv && |
| 1429 | BO->getOpcode() != Instruction::SDiv)) { |
Chris Lattner | a249847 | 2009-10-11 21:36:10 +0000 | [diff] [blame] | 1430 | Op1C = Op0; |
| 1431 | BO = dyn_cast<BinaryOperator>(Op1); |
Nick Lewycky | 0c73079 | 2008-11-21 07:33:58 +0000 | [diff] [blame] | 1432 | } |
Chris Lattner | a249847 | 2009-10-11 21:36:10 +0000 | [diff] [blame] | 1433 | Value *Neg = dyn_castNegVal(Op1C); |
Nick Lewycky | 0c73079 | 2008-11-21 07:33:58 +0000 | [diff] [blame] | 1434 | if (BO && BO->hasOneUse() && |
Chris Lattner | a249847 | 2009-10-11 21:36:10 +0000 | [diff] [blame] | 1435 | (BO->getOperand(1) == Op1C || BO->getOperand(1) == Neg) && |
Nick Lewycky | 0c73079 | 2008-11-21 07:33:58 +0000 | [diff] [blame] | 1436 | (BO->getOpcode() == Instruction::UDiv || |
| 1437 | BO->getOpcode() == Instruction::SDiv)) { |
| 1438 | Value *Op0BO = BO->getOperand(0), *Op1BO = BO->getOperand(1); |
| 1439 | |
Dan Gohman | fa94b94 | 2009-08-12 16:33:09 +0000 | [diff] [blame] | 1440 | // If the division is exact, X % Y is zero. |
| 1441 | if (SDivOperator *SDiv = dyn_cast<SDivOperator>(BO)) |
| 1442 | if (SDiv->isExact()) { |
Chris Lattner | a249847 | 2009-10-11 21:36:10 +0000 | [diff] [blame] | 1443 | if (Op1BO == Op1C) |
Dan Gohman | fa94b94 | 2009-08-12 16:33:09 +0000 | [diff] [blame] | 1444 | return ReplaceInstUsesWith(I, Op0BO); |
Chris Lattner | a249847 | 2009-10-11 21:36:10 +0000 | [diff] [blame] | 1445 | return BinaryOperator::CreateNeg(Op0BO); |
Dan Gohman | fa94b94 | 2009-08-12 16:33:09 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 1448 | Value *Rem; |
Nick Lewycky | 0c73079 | 2008-11-21 07:33:58 +0000 | [diff] [blame] | 1449 | if (BO->getOpcode() == Instruction::UDiv) |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 1450 | Rem = Builder->CreateURem(Op0BO, Op1BO); |
Nick Lewycky | 0c73079 | 2008-11-21 07:33:58 +0000 | [diff] [blame] | 1451 | else |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 1452 | Rem = Builder->CreateSRem(Op0BO, Op1BO); |
Nick Lewycky | 0c73079 | 2008-11-21 07:33:58 +0000 | [diff] [blame] | 1453 | Rem->takeName(BO); |
| 1454 | |
Chris Lattner | a249847 | 2009-10-11 21:36:10 +0000 | [diff] [blame] | 1455 | if (Op1BO == Op1C) |
Nick Lewycky | 0c73079 | 2008-11-21 07:33:58 +0000 | [diff] [blame] | 1456 | return BinaryOperator::CreateSub(Op0BO, Rem); |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 1457 | return BinaryOperator::CreateSub(Rem, Op0BO); |
Nick Lewycky | 0c73079 | 2008-11-21 07:33:58 +0000 | [diff] [blame] | 1458 | } |
| 1459 | } |
| 1460 | |
Chris Lattner | 8af304a | 2009-10-11 07:53:15 +0000 | [diff] [blame] | 1461 | /// i1 mul -> i1 and. |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 1462 | if (I.getType() == Type::getInt1Ty(I.getContext())) |
Chris Lattner | a249847 | 2009-10-11 21:36:10 +0000 | [diff] [blame] | 1463 | return BinaryOperator::CreateAnd(Op0, Op1); |
Nick Lewycky | 9419ddb | 2008-05-31 17:59:52 +0000 | [diff] [blame] | 1464 | |
Chris Lattner | 8af304a | 2009-10-11 07:53:15 +0000 | [diff] [blame] | 1465 | // X*(1 << Y) --> X << Y |
| 1466 | // (1 << Y)*X --> X << Y |
| 1467 | { |
| 1468 | Value *Y; |
| 1469 | if (match(Op0, m_Shl(m_One(), m_Value(Y)))) |
Chris Lattner | a249847 | 2009-10-11 21:36:10 +0000 | [diff] [blame] | 1470 | return BinaryOperator::CreateShl(Op1, Y); |
| 1471 | if (match(Op1, m_Shl(m_One(), m_Value(Y)))) |
Chris Lattner | 8af304a | 2009-10-11 07:53:15 +0000 | [diff] [blame] | 1472 | return BinaryOperator::CreateShl(Op0, Y); |
| 1473 | } |
| 1474 | |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 1475 | // If one of the operands of the multiply is a cast from a boolean value, then |
| 1476 | // we know the bool is either zero or one, so this is a 'masking' multiply. |
Chris Lattner | d2c5836 | 2009-10-11 21:29:45 +0000 | [diff] [blame] | 1477 | // X * Y (where Y is 0 or 1) -> X & (0-Y) |
| 1478 | if (!isa<VectorType>(I.getType())) { |
| 1479 | // -2 is "-1 << 1" so it is all bits set except the low one. |
Dale Johannesen | c1deda5 | 2009-10-12 18:45:32 +0000 | [diff] [blame] | 1480 | APInt Negative2(I.getType()->getPrimitiveSizeInBits(), (uint64_t)-2, true); |
Chris Lattner | 0036e3a | 2009-10-11 21:22:21 +0000 | [diff] [blame] | 1481 | |
Chris Lattner | d2c5836 | 2009-10-11 21:29:45 +0000 | [diff] [blame] | 1482 | Value *BoolCast = 0, *OtherOp = 0; |
| 1483 | if (MaskedValueIsZero(Op0, Negative2)) |
Chris Lattner | a249847 | 2009-10-11 21:36:10 +0000 | [diff] [blame] | 1484 | BoolCast = Op0, OtherOp = Op1; |
| 1485 | else if (MaskedValueIsZero(Op1, Negative2)) |
| 1486 | BoolCast = Op1, OtherOp = Op0; |
Chris Lattner | d2c5836 | 2009-10-11 21:29:45 +0000 | [diff] [blame] | 1487 | |
Chris Lattner | 0036e3a | 2009-10-11 21:22:21 +0000 | [diff] [blame] | 1488 | if (BoolCast) { |
Chris Lattner | 0036e3a | 2009-10-11 21:22:21 +0000 | [diff] [blame] | 1489 | Value *V = Builder->CreateSub(Constant::getNullValue(I.getType()), |
| 1490 | BoolCast, "tmp"); |
| 1491 | return BinaryOperator::CreateAnd(V, OtherOp); |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 1492 | } |
| 1493 | } |
| 1494 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1495 | return Changed ? &I : 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1496 | } |
| 1497 | |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1498 | Instruction *InstCombiner::visitFMul(BinaryOperator &I) { |
| 1499 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | a249847 | 2009-10-11 21:36:10 +0000 | [diff] [blame] | 1500 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1501 | |
| 1502 | // Simplify mul instructions with a constant RHS... |
Chris Lattner | a249847 | 2009-10-11 21:36:10 +0000 | [diff] [blame] | 1503 | if (Constant *Op1C = dyn_cast<Constant>(Op1)) { |
| 1504 | if (ConstantFP *Op1F = dyn_cast<ConstantFP>(Op1C)) { |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1505 | // "In IEEE floating point, x*1 is not equivalent to x for nans. However, |
| 1506 | // ANSI says we can drop signals, so we can do this anyway." (from GCC) |
| 1507 | if (Op1F->isExactlyValue(1.0)) |
| 1508 | return ReplaceInstUsesWith(I, Op0); // Eliminate 'mul double %X, 1.0' |
Chris Lattner | a249847 | 2009-10-11 21:36:10 +0000 | [diff] [blame] | 1509 | } else if (isa<VectorType>(Op1C->getType())) { |
| 1510 | if (ConstantVector *Op1V = dyn_cast<ConstantVector>(Op1C)) { |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1511 | // As above, vector X*splat(1.0) -> X in all defined cases. |
| 1512 | if (Constant *Splat = Op1V->getSplatValue()) { |
| 1513 | if (ConstantFP *F = dyn_cast<ConstantFP>(Splat)) |
| 1514 | if (F->isExactlyValue(1.0)) |
| 1515 | return ReplaceInstUsesWith(I, Op0); |
| 1516 | } |
| 1517 | } |
| 1518 | } |
| 1519 | |
| 1520 | // Try to fold constant mul into select arguments. |
| 1521 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame^] | 1522 | if (Instruction *R = FoldOpIntoSelect(I, SI)) |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1523 | return R; |
| 1524 | |
| 1525 | if (isa<PHINode>(Op0)) |
| 1526 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 1527 | return NV; |
| 1528 | } |
| 1529 | |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 1530 | if (Value *Op0v = dyn_castFNegVal(Op0)) // -X * -Y = X*Y |
Chris Lattner | a249847 | 2009-10-11 21:36:10 +0000 | [diff] [blame] | 1531 | if (Value *Op1v = dyn_castFNegVal(Op1)) |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1532 | return BinaryOperator::CreateFMul(Op0v, Op1v); |
| 1533 | |
| 1534 | return Changed ? &I : 0; |
| 1535 | } |
| 1536 | |
Chris Lattner | fdb19e5 | 2008-07-14 00:15:52 +0000 | [diff] [blame] | 1537 | /// SimplifyDivRemOfSelect - Try to fold a divide or remainder of a select |
| 1538 | /// instruction. |
| 1539 | bool InstCombiner::SimplifyDivRemOfSelect(BinaryOperator &I) { |
| 1540 | SelectInst *SI = cast<SelectInst>(I.getOperand(1)); |
| 1541 | |
| 1542 | // div/rem X, (Cond ? 0 : Y) -> div/rem X, Y |
| 1543 | int NonNullOperand = -1; |
| 1544 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(1))) |
| 1545 | if (ST->isNullValue()) |
| 1546 | NonNullOperand = 2; |
| 1547 | // div/rem X, (Cond ? Y : 0) -> div/rem X, Y |
| 1548 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(2))) |
| 1549 | if (ST->isNullValue()) |
| 1550 | NonNullOperand = 1; |
| 1551 | |
| 1552 | if (NonNullOperand == -1) |
| 1553 | return false; |
| 1554 | |
| 1555 | Value *SelectCond = SI->getOperand(0); |
| 1556 | |
| 1557 | // Change the div/rem to use 'Y' instead of the select. |
| 1558 | I.setOperand(1, SI->getOperand(NonNullOperand)); |
| 1559 | |
| 1560 | // Okay, we know we replace the operand of the div/rem with 'Y' with no |
| 1561 | // problem. However, the select, or the condition of the select may have |
| 1562 | // multiple uses. Based on our knowledge that the operand must be non-zero, |
| 1563 | // propagate the known value for the select into other uses of it, and |
| 1564 | // propagate a known value of the condition into its other users. |
| 1565 | |
| 1566 | // If the select and condition only have a single use, don't bother with this, |
| 1567 | // early exit. |
| 1568 | if (SI->use_empty() && SelectCond->hasOneUse()) |
| 1569 | return true; |
| 1570 | |
| 1571 | // Scan the current block backward, looking for other uses of SI. |
| 1572 | BasicBlock::iterator BBI = &I, BBFront = I.getParent()->begin(); |
| 1573 | |
| 1574 | while (BBI != BBFront) { |
| 1575 | --BBI; |
| 1576 | // If we found a call to a function, we can't assume it will return, so |
| 1577 | // information from below it cannot be propagated above it. |
| 1578 | if (isa<CallInst>(BBI) && !isa<IntrinsicInst>(BBI)) |
| 1579 | break; |
| 1580 | |
| 1581 | // Replace uses of the select or its condition with the known values. |
| 1582 | for (Instruction::op_iterator I = BBI->op_begin(), E = BBI->op_end(); |
| 1583 | I != E; ++I) { |
| 1584 | if (*I == SI) { |
| 1585 | *I = SI->getOperand(NonNullOperand); |
Chris Lattner | 7a1e924 | 2009-08-30 06:13:40 +0000 | [diff] [blame] | 1586 | Worklist.Add(BBI); |
Chris Lattner | fdb19e5 | 2008-07-14 00:15:52 +0000 | [diff] [blame] | 1587 | } else if (*I == SelectCond) { |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 1588 | *I = NonNullOperand == 1 ? ConstantInt::getTrue(BBI->getContext()) : |
| 1589 | ConstantInt::getFalse(BBI->getContext()); |
Chris Lattner | 7a1e924 | 2009-08-30 06:13:40 +0000 | [diff] [blame] | 1590 | Worklist.Add(BBI); |
Chris Lattner | fdb19e5 | 2008-07-14 00:15:52 +0000 | [diff] [blame] | 1591 | } |
| 1592 | } |
| 1593 | |
| 1594 | // If we past the instruction, quit looking for it. |
| 1595 | if (&*BBI == SI) |
| 1596 | SI = 0; |
| 1597 | if (&*BBI == SelectCond) |
| 1598 | SelectCond = 0; |
| 1599 | |
| 1600 | // If we ran out of things to eliminate, break out of the loop. |
| 1601 | if (SelectCond == 0 && SI == 0) |
| 1602 | break; |
| 1603 | |
| 1604 | } |
| 1605 | return true; |
| 1606 | } |
| 1607 | |
| 1608 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1609 | /// This function implements the transforms on div instructions that work |
| 1610 | /// regardless of the kind of div instruction it is (udiv, sdiv, or fdiv). It is |
| 1611 | /// used by the visitors to those instructions. |
| 1612 | /// @brief Transforms common to all three div instructions |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1613 | Instruction *InstCombiner::commonDivTransforms(BinaryOperator &I) { |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1614 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 1615 | |
Chris Lattner | 50b2ca4 | 2008-02-19 06:12:18 +0000 | [diff] [blame] | 1616 | // undef / X -> 0 for integer. |
| 1617 | // undef / X -> undef for FP (the undef could be a snan). |
| 1618 | if (isa<UndefValue>(Op0)) { |
| 1619 | if (Op0->getType()->isFPOrFPVector()) |
| 1620 | return ReplaceInstUsesWith(I, Op0); |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1621 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | 50b2ca4 | 2008-02-19 06:12:18 +0000 | [diff] [blame] | 1622 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1623 | |
| 1624 | // X / undef -> undef |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1625 | if (isa<UndefValue>(Op1)) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1626 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1627 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1628 | return 0; |
| 1629 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1630 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1631 | /// This function implements the transforms common to both integer division |
| 1632 | /// instructions (udiv and sdiv). It is called by the visitors to those integer |
| 1633 | /// division instructions. |
| 1634 | /// @brief Common integer divide transforms |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1635 | Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1636 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 1637 | |
Chris Lattner | b2ae9e3 | 2008-05-16 02:59:42 +0000 | [diff] [blame] | 1638 | // (sdiv X, X) --> 1 (udiv X, X) --> 1 |
Nick Lewycky | 39ac3b5 | 2008-05-23 03:26:47 +0000 | [diff] [blame] | 1639 | if (Op0 == Op1) { |
| 1640 | if (const VectorType *Ty = dyn_cast<VectorType>(I.getType())) { |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1641 | Constant *CI = ConstantInt::get(Ty->getElementType(), 1); |
Nick Lewycky | 39ac3b5 | 2008-05-23 03:26:47 +0000 | [diff] [blame] | 1642 | std::vector<Constant*> Elts(Ty->getNumElements(), CI); |
Owen Anderson | af7ec97 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 1643 | return ReplaceInstUsesWith(I, ConstantVector::get(Elts)); |
Nick Lewycky | 39ac3b5 | 2008-05-23 03:26:47 +0000 | [diff] [blame] | 1644 | } |
| 1645 | |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1646 | Constant *CI = ConstantInt::get(I.getType(), 1); |
Nick Lewycky | 39ac3b5 | 2008-05-23 03:26:47 +0000 | [diff] [blame] | 1647 | return ReplaceInstUsesWith(I, CI); |
| 1648 | } |
Chris Lattner | b2ae9e3 | 2008-05-16 02:59:42 +0000 | [diff] [blame] | 1649 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1650 | if (Instruction *Common = commonDivTransforms(I)) |
| 1651 | return Common; |
Chris Lattner | fdb19e5 | 2008-07-14 00:15:52 +0000 | [diff] [blame] | 1652 | |
| 1653 | // Handle cases involving: [su]div X, (select Cond, Y, Z) |
| 1654 | // This does not apply for fdiv. |
| 1655 | if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I)) |
| 1656 | return &I; |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1657 | |
| 1658 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
| 1659 | // div X, 1 == X |
| 1660 | if (RHS->equalsInt(1)) |
| 1661 | return ReplaceInstUsesWith(I, Op0); |
| 1662 | |
| 1663 | // (X / C1) / C2 -> X / (C1*C2) |
| 1664 | if (Instruction *LHS = dyn_cast<Instruction>(Op0)) |
| 1665 | if (Instruction::BinaryOps(LHS->getOpcode()) == I.getOpcode()) |
| 1666 | if (ConstantInt *LHSRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) { |
Owen Anderson | d672ecb | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 1667 | if (MultiplyOverflows(RHS, LHSRHS, |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 1668 | I.getOpcode()==Instruction::SDiv)) |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1669 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Nick Lewycky | e0cfecf | 2008-02-18 22:48:05 +0000 | [diff] [blame] | 1670 | else |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1671 | return BinaryOperator::Create(I.getOpcode(), LHS->getOperand(0), |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1672 | ConstantExpr::getMul(RHS, LHSRHS)); |
Chris Lattner | bf70b83 | 2005-04-08 04:03:26 +0000 | [diff] [blame] | 1673 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1674 | |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 1675 | if (!RHS->isZero()) { // avoid X udiv 0 |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1676 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame^] | 1677 | if (Instruction *R = FoldOpIntoSelect(I, SI)) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1678 | return R; |
| 1679 | if (isa<PHINode>(Op0)) |
| 1680 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 1681 | return NV; |
| 1682 | } |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 1683 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1684 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1685 | // 0 / X == 0, we don't need to preserve faults! |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1686 | if (ConstantInt *LHS = dyn_cast<ConstantInt>(Op0)) |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1687 | if (LHS->equalsInt(0)) |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1688 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1689 | |
Nick Lewycky | 9419ddb | 2008-05-31 17:59:52 +0000 | [diff] [blame] | 1690 | // It can't be division by zero, hence it must be division by one. |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 1691 | if (I.getType() == Type::getInt1Ty(I.getContext())) |
Nick Lewycky | 9419ddb | 2008-05-31 17:59:52 +0000 | [diff] [blame] | 1692 | return ReplaceInstUsesWith(I, Op0); |
| 1693 | |
Nick Lewycky | 895f085 | 2008-11-27 20:21:08 +0000 | [diff] [blame] | 1694 | if (ConstantVector *Op1V = dyn_cast<ConstantVector>(Op1)) { |
| 1695 | if (ConstantInt *X = cast_or_null<ConstantInt>(Op1V->getSplatValue())) |
| 1696 | // div X, 1 == X |
| 1697 | if (X->isOne()) |
| 1698 | return ReplaceInstUsesWith(I, Op0); |
| 1699 | } |
| 1700 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1701 | return 0; |
| 1702 | } |
| 1703 | |
| 1704 | Instruction *InstCombiner::visitUDiv(BinaryOperator &I) { |
| 1705 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 1706 | |
| 1707 | // Handle the integer div common cases |
| 1708 | if (Instruction *Common = commonIDivTransforms(I)) |
| 1709 | return Common; |
| 1710 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1711 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op1)) { |
Nick Lewycky | 8ca5248 | 2008-11-27 22:41:10 +0000 | [diff] [blame] | 1712 | // X udiv C^2 -> X >> C |
| 1713 | // Check to see if this is an unsigned division with an exact power of 2, |
| 1714 | // if so, convert to a right shift. |
Reid Spencer | 6eb0d99 | 2007-03-26 23:58:26 +0000 | [diff] [blame] | 1715 | if (C->getValue().isPowerOf2()) // 0 not included in isPowerOf2 |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1716 | return BinaryOperator::CreateLShr(Op0, |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1717 | ConstantInt::get(Op0->getType(), C->getValue().logBase2())); |
Nick Lewycky | 8ca5248 | 2008-11-27 22:41:10 +0000 | [diff] [blame] | 1718 | |
| 1719 | // X udiv C, where C >= signbit |
| 1720 | if (C->getValue().isNegative()) { |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 1721 | Value *IC = Builder->CreateICmpULT( Op0, C); |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1722 | return SelectInst::Create(IC, Constant::getNullValue(I.getType()), |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1723 | ConstantInt::get(I.getType(), 1)); |
Nick Lewycky | 8ca5248 | 2008-11-27 22:41:10 +0000 | [diff] [blame] | 1724 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1725 | } |
| 1726 | |
| 1727 | // X udiv (C1 << N), where C1 is "1<<C2" --> X >> (N+C2) |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1728 | if (BinaryOperator *RHSI = dyn_cast<BinaryOperator>(I.getOperand(1))) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1729 | if (RHSI->getOpcode() == Instruction::Shl && |
| 1730 | isa<ConstantInt>(RHSI->getOperand(0))) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 1731 | const APInt& C1 = cast<ConstantInt>(RHSI->getOperand(0))->getValue(); |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 1732 | if (C1.isPowerOf2()) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1733 | Value *N = RHSI->getOperand(1); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1734 | const Type *NTy = N->getType(); |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 1735 | if (uint32_t C2 = C1.logBase2()) |
| 1736 | N = Builder->CreateAdd(N, ConstantInt::get(NTy, C2), "tmp"); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1737 | return BinaryOperator::CreateLShr(Op0, N); |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 1738 | } |
| 1739 | } |
Chris Lattner | c812e5d | 2005-11-05 07:40:31 +0000 | [diff] [blame] | 1740 | } |
| 1741 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1742 | // udiv X, (Select Cond, C1, C2) --> Select Cond, (shr X, C1), (shr X, C2) |
| 1743 | // where C1&C2 are powers of two. |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 1744 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1745 | if (ConstantInt *STO = dyn_cast<ConstantInt>(SI->getOperand(1))) |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 1746 | if (ConstantInt *SFO = dyn_cast<ConstantInt>(SI->getOperand(2))) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 1747 | const APInt &TVA = STO->getValue(), &FVA = SFO->getValue(); |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 1748 | if (TVA.isPowerOf2() && FVA.isPowerOf2()) { |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 1749 | // Compute the shift amounts |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 1750 | uint32_t TSA = TVA.logBase2(), FSA = FVA.logBase2(); |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 1751 | // Construct the "on true" case of the select |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1752 | Constant *TC = ConstantInt::get(Op0->getType(), TSA); |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 1753 | Value *TSI = Builder->CreateLShr(Op0, TC, SI->getName()+".t"); |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 1754 | |
| 1755 | // Construct the "on false" case of the select |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1756 | Constant *FC = ConstantInt::get(Op0->getType(), FSA); |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 1757 | Value *FSI = Builder->CreateLShr(Op0, FC, SI->getName()+".f"); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1758 | |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 1759 | // construct the select instruction and return it. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 1760 | return SelectInst::Create(SI->getOperand(0), TSI, FSI, SI->getName()); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1761 | } |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 1762 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1763 | return 0; |
| 1764 | } |
| 1765 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1766 | Instruction *InstCombiner::visitSDiv(BinaryOperator &I) { |
| 1767 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 1768 | |
| 1769 | // Handle the integer div common cases |
| 1770 | if (Instruction *Common = commonIDivTransforms(I)) |
| 1771 | return Common; |
| 1772 | |
| 1773 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
| 1774 | // sdiv X, -1 == -X |
| 1775 | if (RHS->isAllOnesValue()) |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 1776 | return BinaryOperator::CreateNeg(Op0); |
Dan Gohman | 1bdf5dc | 2009-08-11 20:47:47 +0000 | [diff] [blame] | 1777 | |
Dan Gohman | fa94b94 | 2009-08-12 16:33:09 +0000 | [diff] [blame] | 1778 | // sdiv X, C --> ashr X, log2(C) |
Dan Gohman | 1bdf5dc | 2009-08-11 20:47:47 +0000 | [diff] [blame] | 1779 | if (cast<SDivOperator>(&I)->isExact() && |
| 1780 | RHS->getValue().isNonNegative() && |
| 1781 | RHS->getValue().isPowerOf2()) { |
| 1782 | Value *ShAmt = llvm::ConstantInt::get(RHS->getType(), |
| 1783 | RHS->getValue().exactLogBase2()); |
| 1784 | return BinaryOperator::CreateAShr(Op0, ShAmt, I.getName()); |
| 1785 | } |
Dan Gohman | 9ca9daa | 2009-08-12 16:37:02 +0000 | [diff] [blame] | 1786 | |
| 1787 | // -X/C --> X/-C provided the negation doesn't overflow. |
| 1788 | if (SubOperator *Sub = dyn_cast<SubOperator>(Op0)) |
| 1789 | if (isa<Constant>(Sub->getOperand(0)) && |
| 1790 | cast<Constant>(Sub->getOperand(0))->isNullValue() && |
Dan Gohman | 5078f84 | 2009-08-20 17:11:38 +0000 | [diff] [blame] | 1791 | Sub->hasNoSignedWrap()) |
Dan Gohman | 9ca9daa | 2009-08-12 16:37:02 +0000 | [diff] [blame] | 1792 | return BinaryOperator::CreateSDiv(Sub->getOperand(1), |
| 1793 | ConstantExpr::getNeg(RHS)); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1794 | } |
| 1795 | |
| 1796 | // If the sign bits of both operands are zero (i.e. we can prove they are |
| 1797 | // unsigned inputs), turn this into a udiv. |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1798 | if (I.getType()->isInteger()) { |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 1799 | APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits())); |
Eli Friedman | 8be1739 | 2009-07-18 09:53:21 +0000 | [diff] [blame] | 1800 | if (MaskedValueIsZero(Op0, Mask)) { |
| 1801 | if (MaskedValueIsZero(Op1, Mask)) { |
| 1802 | // X sdiv Y -> X udiv Y, iff X and Y don't have sign bit set |
| 1803 | return BinaryOperator::CreateUDiv(Op0, Op1, I.getName()); |
| 1804 | } |
| 1805 | ConstantInt *ShiftedInt; |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 1806 | if (match(Op1, m_Shl(m_ConstantInt(ShiftedInt), m_Value())) && |
Eli Friedman | 8be1739 | 2009-07-18 09:53:21 +0000 | [diff] [blame] | 1807 | ShiftedInt->getValue().isPowerOf2()) { |
| 1808 | // X sdiv (1 << Y) -> X udiv (1 << Y) ( -> X u>> Y) |
| 1809 | // Safe because the only negative value (1 << Y) can take on is |
| 1810 | // INT_MIN, and X sdiv INT_MIN == X udiv INT_MIN == 0 if X doesn't have |
| 1811 | // the sign bit set. |
| 1812 | return BinaryOperator::CreateUDiv(Op0, Op1, I.getName()); |
| 1813 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1814 | } |
Eli Friedman | 8be1739 | 2009-07-18 09:53:21 +0000 | [diff] [blame] | 1815 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1816 | |
| 1817 | return 0; |
| 1818 | } |
| 1819 | |
| 1820 | Instruction *InstCombiner::visitFDiv(BinaryOperator &I) { |
| 1821 | return commonDivTransforms(I); |
| 1822 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1823 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1824 | /// This function implements the transforms on rem instructions that work |
| 1825 | /// regardless of the kind of rem instruction it is (urem, srem, or frem). It |
| 1826 | /// is used by the visitors to those instructions. |
| 1827 | /// @brief Transforms common to all three rem instructions |
| 1828 | Instruction *InstCombiner::commonRemTransforms(BinaryOperator &I) { |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1829 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1830 | |
Chris Lattner | 50b2ca4 | 2008-02-19 06:12:18 +0000 | [diff] [blame] | 1831 | if (isa<UndefValue>(Op0)) { // undef % X -> 0 |
| 1832 | if (I.getType()->isFPOrFPVector()) |
| 1833 | return ReplaceInstUsesWith(I, Op0); // X % undef -> undef (could be SNaN) |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1834 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | 50b2ca4 | 2008-02-19 06:12:18 +0000 | [diff] [blame] | 1835 | } |
Chris Lattner | 19ccd5c | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 1836 | if (isa<UndefValue>(Op1)) |
| 1837 | return ReplaceInstUsesWith(I, Op1); // X % undef -> undef |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1838 | |
| 1839 | // Handle cases involving: rem X, (select Cond, Y, Z) |
Chris Lattner | fdb19e5 | 2008-07-14 00:15:52 +0000 | [diff] [blame] | 1840 | if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I)) |
| 1841 | return &I; |
Chris Lattner | 5b73c08 | 2004-07-06 07:01:22 +0000 | [diff] [blame] | 1842 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1843 | return 0; |
| 1844 | } |
| 1845 | |
| 1846 | /// This function implements the transforms common to both integer remainder |
| 1847 | /// instructions (urem and srem). It is called by the visitors to those integer |
| 1848 | /// remainder instructions. |
| 1849 | /// @brief Common integer remainder transforms |
| 1850 | Instruction *InstCombiner::commonIRemTransforms(BinaryOperator &I) { |
| 1851 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 1852 | |
| 1853 | if (Instruction *common = commonRemTransforms(I)) |
| 1854 | return common; |
| 1855 | |
Dale Johannesen | ed6af24 | 2009-01-21 00:35:19 +0000 | [diff] [blame] | 1856 | // 0 % X == 0 for integer, we don't need to preserve faults! |
| 1857 | if (Constant *LHS = dyn_cast<Constant>(Op0)) |
| 1858 | if (LHS->isNullValue()) |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1859 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Dale Johannesen | ed6af24 | 2009-01-21 00:35:19 +0000 | [diff] [blame] | 1860 | |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1861 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 19ccd5c | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 1862 | // X % 0 == undef, we don't need to preserve faults! |
| 1863 | if (RHS->equalsInt(0)) |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 1864 | return ReplaceInstUsesWith(I, UndefValue::get(I.getType())); |
Chris Lattner | 19ccd5c | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 1865 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1866 | if (RHS->equalsInt(1)) // X % 1 == 0 |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1867 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1868 | |
Chris Lattner | 9794392 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 1869 | if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) { |
| 1870 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0I)) { |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame^] | 1871 | if (Instruction *R = FoldOpIntoSelect(I, SI)) |
Chris Lattner | 9794392 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 1872 | return R; |
| 1873 | } else if (isa<PHINode>(Op0I)) { |
| 1874 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 1875 | return NV; |
Chris Lattner | 9794392 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 1876 | } |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 1877 | |
| 1878 | // See if we can fold away this rem instruction. |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1879 | if (SimplifyDemandedInstructionBits(I)) |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 1880 | return &I; |
Chris Lattner | 9794392 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 1881 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1882 | } |
| 1883 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1884 | return 0; |
| 1885 | } |
| 1886 | |
| 1887 | Instruction *InstCombiner::visitURem(BinaryOperator &I) { |
| 1888 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 1889 | |
| 1890 | if (Instruction *common = commonIRemTransforms(I)) |
| 1891 | return common; |
| 1892 | |
| 1893 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
| 1894 | // X urem C^2 -> X and C |
| 1895 | // Check to see if this is an unsigned remainder with an exact power of 2, |
| 1896 | // if so, convert to a bitwise and. |
| 1897 | if (ConstantInt *C = dyn_cast<ConstantInt>(RHS)) |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 1898 | if (C->getValue().isPowerOf2()) |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 1899 | return BinaryOperator::CreateAnd(Op0, SubOne(C)); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1900 | } |
| 1901 | |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 1902 | if (Instruction *RHSI = dyn_cast<Instruction>(I.getOperand(1))) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1903 | // Turn A % (C << N), where C is 2^k, into A & ((C << N)-1) |
| 1904 | if (RHSI->getOpcode() == Instruction::Shl && |
| 1905 | isa<ConstantInt>(RHSI->getOperand(0))) { |
Zhou Sheng | 0fc5095 | 2007-03-25 05:01:29 +0000 | [diff] [blame] | 1906 | if (cast<ConstantInt>(RHSI->getOperand(0))->getValue().isPowerOf2()) { |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1907 | Constant *N1 = Constant::getAllOnesValue(I.getType()); |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 1908 | Value *Add = Builder->CreateAdd(RHSI, N1, "tmp"); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1909 | return BinaryOperator::CreateAnd(Op0, Add); |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 1910 | } |
| 1911 | } |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1912 | } |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 1913 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1914 | // urem X, (select Cond, 2^C1, 2^C2) --> select Cond, (and X, C1), (and X, C2) |
| 1915 | // where C1&C2 are powers of two. |
| 1916 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { |
| 1917 | if (ConstantInt *STO = dyn_cast<ConstantInt>(SI->getOperand(1))) |
| 1918 | if (ConstantInt *SFO = dyn_cast<ConstantInt>(SI->getOperand(2))) { |
| 1919 | // STO == 0 and SFO == 0 handled above. |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 1920 | if ((STO->getValue().isPowerOf2()) && |
| 1921 | (SFO->getValue().isPowerOf2())) { |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 1922 | Value *TrueAnd = Builder->CreateAnd(Op0, SubOne(STO), |
| 1923 | SI->getName()+".t"); |
| 1924 | Value *FalseAnd = Builder->CreateAnd(Op0, SubOne(SFO), |
| 1925 | SI->getName()+".f"); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 1926 | return SelectInst::Create(SI->getOperand(0), TrueAnd, FalseAnd); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1927 | } |
| 1928 | } |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 1929 | } |
| 1930 | |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1931 | return 0; |
| 1932 | } |
| 1933 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1934 | Instruction *InstCombiner::visitSRem(BinaryOperator &I) { |
| 1935 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 1936 | |
Dan Gohman | cff5509 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 1937 | // Handle the integer rem common cases |
Chris Lattner | e5ecdb5 | 2009-08-30 06:22:51 +0000 | [diff] [blame] | 1938 | if (Instruction *Common = commonIRemTransforms(I)) |
| 1939 | return Common; |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1940 | |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 1941 | if (Value *RHSNeg = dyn_castNegVal(Op1)) |
Nick Lewycky | 23c0430 | 2008-09-03 06:24:21 +0000 | [diff] [blame] | 1942 | if (!isa<Constant>(RHSNeg) || |
| 1943 | (isa<ConstantInt>(RHSNeg) && |
| 1944 | cast<ConstantInt>(RHSNeg)->getValue().isStrictlyPositive())) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1945 | // X % -Y -> X % Y |
Chris Lattner | 3c4e38e | 2009-08-30 06:27:41 +0000 | [diff] [blame] | 1946 | Worklist.AddValue(I.getOperand(1)); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1947 | I.setOperand(1, RHSNeg); |
| 1948 | return &I; |
| 1949 | } |
Nick Lewycky | a06cf82 | 2008-09-30 06:08:34 +0000 | [diff] [blame] | 1950 | |
Dan Gohman | cff5509 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 1951 | // If the sign bits of both operands are zero (i.e. we can prove they are |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1952 | // unsigned inputs), turn this into a urem. |
Dan Gohman | cff5509 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 1953 | if (I.getType()->isInteger()) { |
| 1954 | APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits())); |
| 1955 | if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) { |
| 1956 | // X srem Y -> X urem Y, iff X and Y don't have sign bit set |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1957 | return BinaryOperator::CreateURem(Op0, Op1, I.getName()); |
Dan Gohman | cff5509 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 1958 | } |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1959 | } |
| 1960 | |
Nick Lewycky | 2a8f659 | 2008-12-18 06:31:11 +0000 | [diff] [blame] | 1961 | // If it's a constant vector, flip any negative values positive. |
Nick Lewycky | 9dce873 | 2008-12-20 16:48:00 +0000 | [diff] [blame] | 1962 | if (ConstantVector *RHSV = dyn_cast<ConstantVector>(Op1)) { |
| 1963 | unsigned VWidth = RHSV->getNumOperands(); |
Nick Lewycky | 2a8f659 | 2008-12-18 06:31:11 +0000 | [diff] [blame] | 1964 | |
Nick Lewycky | 9dce873 | 2008-12-20 16:48:00 +0000 | [diff] [blame] | 1965 | bool hasNegative = false; |
| 1966 | for (unsigned i = 0; !hasNegative && i != VWidth; ++i) |
| 1967 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(RHSV->getOperand(i))) |
| 1968 | if (RHS->getValue().isNegative()) |
| 1969 | hasNegative = true; |
| 1970 | |
| 1971 | if (hasNegative) { |
| 1972 | std::vector<Constant *> Elts(VWidth); |
Nick Lewycky | 2a8f659 | 2008-12-18 06:31:11 +0000 | [diff] [blame] | 1973 | for (unsigned i = 0; i != VWidth; ++i) { |
| 1974 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(RHSV->getOperand(i))) { |
| 1975 | if (RHS->getValue().isNegative()) |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1976 | Elts[i] = cast<ConstantInt>(ConstantExpr::getNeg(RHS)); |
Nick Lewycky | 2a8f659 | 2008-12-18 06:31:11 +0000 | [diff] [blame] | 1977 | else |
| 1978 | Elts[i] = RHS; |
| 1979 | } |
| 1980 | } |
| 1981 | |
Owen Anderson | af7ec97 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 1982 | Constant *NewRHSV = ConstantVector::get(Elts); |
Nick Lewycky | 2a8f659 | 2008-12-18 06:31:11 +0000 | [diff] [blame] | 1983 | if (NewRHSV != RHSV) { |
Chris Lattner | 3c4e38e | 2009-08-30 06:27:41 +0000 | [diff] [blame] | 1984 | Worklist.AddValue(I.getOperand(1)); |
Nick Lewycky | 2a8f659 | 2008-12-18 06:31:11 +0000 | [diff] [blame] | 1985 | I.setOperand(1, NewRHSV); |
| 1986 | return &I; |
| 1987 | } |
| 1988 | } |
| 1989 | } |
| 1990 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1991 | return 0; |
| 1992 | } |
| 1993 | |
| 1994 | Instruction *InstCombiner::visitFRem(BinaryOperator &I) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1995 | return commonRemTransforms(I); |
| 1996 | } |
| 1997 | |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 1998 | // isOneBitSet - Return true if there is exactly one bit set in the specified |
| 1999 | // constant. |
| 2000 | static bool isOneBitSet(const ConstantInt *CI) { |
Reid Spencer | 5f6a895 | 2007-03-20 00:16:52 +0000 | [diff] [blame] | 2001 | return CI->getValue().isPowerOf2(); |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2002 | } |
| 2003 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2004 | /// getICmpCode - Encode a icmp predicate into a three bit mask. These bits |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2005 | /// are carefully arranged to allow folding of expressions such as: |
| 2006 | /// |
| 2007 | /// (A < B) | (A > B) --> (A != B) |
| 2008 | /// |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2009 | /// Note that this is only valid if the first and second predicates have the |
| 2010 | /// same sign. Is illegal to do: (A u< B) | (A s> B) |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2011 | /// |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2012 | /// Three bits are used to represent the condition, as follows: |
| 2013 | /// 0 A > B |
| 2014 | /// 1 A == B |
| 2015 | /// 2 A < B |
| 2016 | /// |
| 2017 | /// <=> Value Definition |
| 2018 | /// 000 0 Always false |
| 2019 | /// 001 1 A > B |
| 2020 | /// 010 2 A == B |
| 2021 | /// 011 3 A >= B |
| 2022 | /// 100 4 A < B |
| 2023 | /// 101 5 A != B |
| 2024 | /// 110 6 A <= B |
| 2025 | /// 111 7 Always true |
| 2026 | /// |
| 2027 | static unsigned getICmpCode(const ICmpInst *ICI) { |
| 2028 | switch (ICI->getPredicate()) { |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2029 | // False -> 0 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2030 | case ICmpInst::ICMP_UGT: return 1; // 001 |
| 2031 | case ICmpInst::ICMP_SGT: return 1; // 001 |
| 2032 | case ICmpInst::ICMP_EQ: return 2; // 010 |
| 2033 | case ICmpInst::ICMP_UGE: return 3; // 011 |
| 2034 | case ICmpInst::ICMP_SGE: return 3; // 011 |
| 2035 | case ICmpInst::ICMP_ULT: return 4; // 100 |
| 2036 | case ICmpInst::ICMP_SLT: return 4; // 100 |
| 2037 | case ICmpInst::ICMP_NE: return 5; // 101 |
| 2038 | case ICmpInst::ICMP_ULE: return 6; // 110 |
| 2039 | case ICmpInst::ICMP_SLE: return 6; // 110 |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2040 | // True -> 7 |
| 2041 | default: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 2042 | llvm_unreachable("Invalid ICmp predicate!"); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2043 | return 0; |
| 2044 | } |
| 2045 | } |
| 2046 | |
Evan Cheng | 8db9072 | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 2047 | /// getFCmpCode - Similar to getICmpCode but for FCmpInst. This encodes a fcmp |
| 2048 | /// predicate into a three bit mask. It also returns whether it is an ordered |
| 2049 | /// predicate by reference. |
| 2050 | static unsigned getFCmpCode(FCmpInst::Predicate CC, bool &isOrdered) { |
| 2051 | isOrdered = false; |
| 2052 | switch (CC) { |
| 2053 | case FCmpInst::FCMP_ORD: isOrdered = true; return 0; // 000 |
| 2054 | case FCmpInst::FCMP_UNO: return 0; // 000 |
Evan Cheng | 4990b25 | 2008-10-14 18:13:38 +0000 | [diff] [blame] | 2055 | case FCmpInst::FCMP_OGT: isOrdered = true; return 1; // 001 |
| 2056 | case FCmpInst::FCMP_UGT: return 1; // 001 |
| 2057 | case FCmpInst::FCMP_OEQ: isOrdered = true; return 2; // 010 |
| 2058 | case FCmpInst::FCMP_UEQ: return 2; // 010 |
Evan Cheng | 8db9072 | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 2059 | case FCmpInst::FCMP_OGE: isOrdered = true; return 3; // 011 |
| 2060 | case FCmpInst::FCMP_UGE: return 3; // 011 |
| 2061 | case FCmpInst::FCMP_OLT: isOrdered = true; return 4; // 100 |
| 2062 | case FCmpInst::FCMP_ULT: return 4; // 100 |
Evan Cheng | 4990b25 | 2008-10-14 18:13:38 +0000 | [diff] [blame] | 2063 | case FCmpInst::FCMP_ONE: isOrdered = true; return 5; // 101 |
| 2064 | case FCmpInst::FCMP_UNE: return 5; // 101 |
Evan Cheng | 8db9072 | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 2065 | case FCmpInst::FCMP_OLE: isOrdered = true; return 6; // 110 |
| 2066 | case FCmpInst::FCMP_ULE: return 6; // 110 |
Evan Cheng | 4030062 | 2008-10-14 18:44:08 +0000 | [diff] [blame] | 2067 | // True -> 7 |
Evan Cheng | 8db9072 | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 2068 | default: |
| 2069 | // Not expecting FCMP_FALSE and FCMP_TRUE; |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 2070 | llvm_unreachable("Unexpected FCmp predicate!"); |
Evan Cheng | 8db9072 | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 2071 | return 0; |
| 2072 | } |
| 2073 | } |
| 2074 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2075 | /// getICmpValue - This is the complement of getICmpCode, which turns an |
| 2076 | /// opcode and two operands into either a constant true or false, or a brand |
Dan Gohman | 5d066ff | 2007-09-17 17:31:57 +0000 | [diff] [blame] | 2077 | /// new ICmp instruction. The sign is passed in to determine which kind |
Evan Cheng | 8db9072 | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 2078 | /// of predicate to use in the new icmp instruction. |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 2079 | static Value *getICmpValue(bool sign, unsigned code, Value *LHS, Value *RHS) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2080 | switch (code) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 2081 | default: llvm_unreachable("Illegal ICmp code!"); |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 2082 | case 0: return ConstantInt::getFalse(LHS->getContext()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2083 | case 1: |
| 2084 | if (sign) |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2085 | return new ICmpInst(ICmpInst::ICMP_SGT, LHS, RHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2086 | else |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2087 | return new ICmpInst(ICmpInst::ICMP_UGT, LHS, RHS); |
| 2088 | case 2: return new ICmpInst(ICmpInst::ICMP_EQ, LHS, RHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2089 | case 3: |
| 2090 | if (sign) |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2091 | return new ICmpInst(ICmpInst::ICMP_SGE, LHS, RHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2092 | else |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2093 | return new ICmpInst(ICmpInst::ICMP_UGE, LHS, RHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2094 | case 4: |
| 2095 | if (sign) |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2096 | return new ICmpInst(ICmpInst::ICMP_SLT, LHS, RHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2097 | else |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2098 | return new ICmpInst(ICmpInst::ICMP_ULT, LHS, RHS); |
| 2099 | case 5: return new ICmpInst(ICmpInst::ICMP_NE, LHS, RHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2100 | case 6: |
| 2101 | if (sign) |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2102 | return new ICmpInst(ICmpInst::ICMP_SLE, LHS, RHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2103 | else |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2104 | return new ICmpInst(ICmpInst::ICMP_ULE, LHS, RHS); |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 2105 | case 7: return ConstantInt::getTrue(LHS->getContext()); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2106 | } |
| 2107 | } |
| 2108 | |
Evan Cheng | 8db9072 | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 2109 | /// getFCmpValue - This is the complement of getFCmpCode, which turns an |
| 2110 | /// opcode and two operands into either a FCmp instruction. isordered is passed |
| 2111 | /// in to determine which kind of predicate to use in the new fcmp instruction. |
| 2112 | static Value *getFCmpValue(bool isordered, unsigned code, |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 2113 | Value *LHS, Value *RHS) { |
Evan Cheng | 8db9072 | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 2114 | switch (code) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 2115 | default: llvm_unreachable("Illegal FCmp code!"); |
Evan Cheng | 8db9072 | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 2116 | case 0: |
| 2117 | if (isordered) |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2118 | return new FCmpInst(FCmpInst::FCMP_ORD, LHS, RHS); |
Evan Cheng | 8db9072 | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 2119 | else |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2120 | return new FCmpInst(FCmpInst::FCMP_UNO, LHS, RHS); |
Evan Cheng | 8db9072 | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 2121 | case 1: |
| 2122 | if (isordered) |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2123 | return new FCmpInst(FCmpInst::FCMP_OGT, LHS, RHS); |
Evan Cheng | 8db9072 | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 2124 | else |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2125 | return new FCmpInst(FCmpInst::FCMP_UGT, LHS, RHS); |
Evan Cheng | 4990b25 | 2008-10-14 18:13:38 +0000 | [diff] [blame] | 2126 | case 2: |
| 2127 | if (isordered) |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2128 | return new FCmpInst(FCmpInst::FCMP_OEQ, LHS, RHS); |
Evan Cheng | 4990b25 | 2008-10-14 18:13:38 +0000 | [diff] [blame] | 2129 | else |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2130 | return new FCmpInst(FCmpInst::FCMP_UEQ, LHS, RHS); |
Evan Cheng | 8db9072 | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 2131 | case 3: |
| 2132 | if (isordered) |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2133 | return new FCmpInst(FCmpInst::FCMP_OGE, LHS, RHS); |
Evan Cheng | 8db9072 | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 2134 | else |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2135 | return new FCmpInst(FCmpInst::FCMP_UGE, LHS, RHS); |
Evan Cheng | 8db9072 | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 2136 | case 4: |
| 2137 | if (isordered) |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2138 | return new FCmpInst(FCmpInst::FCMP_OLT, LHS, RHS); |
Evan Cheng | 8db9072 | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 2139 | else |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2140 | return new FCmpInst(FCmpInst::FCMP_ULT, LHS, RHS); |
Evan Cheng | 8db9072 | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 2141 | case 5: |
| 2142 | if (isordered) |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2143 | return new FCmpInst(FCmpInst::FCMP_ONE, LHS, RHS); |
Evan Cheng | 4990b25 | 2008-10-14 18:13:38 +0000 | [diff] [blame] | 2144 | else |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2145 | return new FCmpInst(FCmpInst::FCMP_UNE, LHS, RHS); |
Evan Cheng | 4990b25 | 2008-10-14 18:13:38 +0000 | [diff] [blame] | 2146 | case 6: |
| 2147 | if (isordered) |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2148 | return new FCmpInst(FCmpInst::FCMP_OLE, LHS, RHS); |
Evan Cheng | 8db9072 | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 2149 | else |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2150 | return new FCmpInst(FCmpInst::FCMP_ULE, LHS, RHS); |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 2151 | case 7: return ConstantInt::getTrue(LHS->getContext()); |
Evan Cheng | 8db9072 | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 2152 | } |
| 2153 | } |
| 2154 | |
Chris Lattner | b9553d6 | 2008-11-16 04:55:20 +0000 | [diff] [blame] | 2155 | /// PredicatesFoldable - Return true if both predicates match sign or if at |
| 2156 | /// least one of them is an equality comparison (which is signless). |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2157 | static bool PredicatesFoldable(ICmpInst::Predicate p1, ICmpInst::Predicate p2) { |
Nick Lewycky | 4a134af | 2009-10-25 05:20:17 +0000 | [diff] [blame] | 2158 | return (CmpInst::isSigned(p1) == CmpInst::isSigned(p2)) || |
| 2159 | (CmpInst::isSigned(p1) && ICmpInst::isEquality(p2)) || |
| 2160 | (CmpInst::isSigned(p2) && ICmpInst::isEquality(p1)); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2161 | } |
| 2162 | |
| 2163 | namespace { |
| 2164 | // FoldICmpLogical - Implements (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B) |
| 2165 | struct FoldICmpLogical { |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2166 | InstCombiner &IC; |
| 2167 | Value *LHS, *RHS; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2168 | ICmpInst::Predicate pred; |
| 2169 | FoldICmpLogical(InstCombiner &ic, ICmpInst *ICI) |
| 2170 | : IC(ic), LHS(ICI->getOperand(0)), RHS(ICI->getOperand(1)), |
| 2171 | pred(ICI->getPredicate()) {} |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2172 | bool shouldApply(Value *V) const { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2173 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(V)) |
| 2174 | if (PredicatesFoldable(pred, ICI->getPredicate())) |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 2175 | return ((ICI->getOperand(0) == LHS && ICI->getOperand(1) == RHS) || |
| 2176 | (ICI->getOperand(0) == RHS && ICI->getOperand(1) == LHS)); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2177 | return false; |
| 2178 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2179 | Instruction *apply(Instruction &Log) const { |
| 2180 | ICmpInst *ICI = cast<ICmpInst>(Log.getOperand(0)); |
| 2181 | if (ICI->getOperand(0) != LHS) { |
| 2182 | assert(ICI->getOperand(1) == LHS); |
| 2183 | ICI->swapOperands(); // Swap the LHS and RHS of the ICmp |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2184 | } |
| 2185 | |
Chris Lattner | bc1dbfc | 2007-03-13 14:27:42 +0000 | [diff] [blame] | 2186 | ICmpInst *RHSICI = cast<ICmpInst>(Log.getOperand(1)); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2187 | unsigned LHSCode = getICmpCode(ICI); |
Chris Lattner | bc1dbfc | 2007-03-13 14:27:42 +0000 | [diff] [blame] | 2188 | unsigned RHSCode = getICmpCode(RHSICI); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2189 | unsigned Code; |
| 2190 | switch (Log.getOpcode()) { |
| 2191 | case Instruction::And: Code = LHSCode & RHSCode; break; |
| 2192 | case Instruction::Or: Code = LHSCode | RHSCode; break; |
| 2193 | case Instruction::Xor: Code = LHSCode ^ RHSCode; break; |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 2194 | default: llvm_unreachable("Illegal logical opcode!"); return 0; |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2195 | } |
| 2196 | |
Nick Lewycky | 4a134af | 2009-10-25 05:20:17 +0000 | [diff] [blame] | 2197 | bool isSigned = RHSICI->isSigned() || ICI->isSigned(); |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 2198 | Value *RV = getICmpValue(isSigned, Code, LHS, RHS); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2199 | if (Instruction *I = dyn_cast<Instruction>(RV)) |
| 2200 | return I; |
| 2201 | // Otherwise, it's a constant boolean value... |
| 2202 | return IC.ReplaceInstUsesWith(Log, RV); |
| 2203 | } |
| 2204 | }; |
Chris Lattner | d23b5ba | 2006-11-15 04:53:24 +0000 | [diff] [blame] | 2205 | } // end anonymous namespace |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2206 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2207 | // OptAndOp - This handles expressions of the form ((val OP C1) & C2). Where |
| 2208 | // the Op parameter is 'OP', OpRHS is 'C1', and AndRHS is 'C2'. Op is |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2209 | // guaranteed to be a binary operator. |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2210 | Instruction *InstCombiner::OptAndOp(Instruction *Op, |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2211 | ConstantInt *OpRHS, |
| 2212 | ConstantInt *AndRHS, |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2213 | BinaryOperator &TheAnd) { |
| 2214 | Value *X = Op->getOperand(0); |
Chris Lattner | 76f7fe2 | 2004-01-12 19:47:05 +0000 | [diff] [blame] | 2215 | Constant *Together = 0; |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2216 | if (!Op->isShift()) |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2217 | Together = ConstantExpr::getAnd(AndRHS, OpRHS); |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2218 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2219 | switch (Op->getOpcode()) { |
| 2220 | case Instruction::Xor: |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2221 | if (Op->hasOneUse()) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2222 | // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2) |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 2223 | Value *And = Builder->CreateAnd(X, AndRHS); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 2224 | And->takeName(Op); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2225 | return BinaryOperator::CreateXor(And, Together); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2226 | } |
| 2227 | break; |
| 2228 | case Instruction::Or: |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2229 | if (Together == AndRHS) // (X | C) & C --> C |
| 2230 | return ReplaceInstUsesWith(TheAnd, AndRHS); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2231 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2232 | if (Op->hasOneUse() && Together != OpRHS) { |
| 2233 | // (X | C1) & C2 --> (X | (C1&C2)) & C2 |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 2234 | Value *Or = Builder->CreateOr(X, Together); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 2235 | Or->takeName(Op); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2236 | return BinaryOperator::CreateAnd(Or, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2237 | } |
| 2238 | break; |
| 2239 | case Instruction::Add: |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 2240 | if (Op->hasOneUse()) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2241 | // Adding a one to a single bit bit-field should be turned into an XOR |
| 2242 | // of the bit. First thing to check is to see if this AND is with a |
| 2243 | // single bit constant. |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2244 | const APInt& AndRHSV = cast<ConstantInt>(AndRHS)->getValue(); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2245 | |
| 2246 | // If there is only one bit set... |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2247 | if (isOneBitSet(cast<ConstantInt>(AndRHS))) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2248 | // Ok, at this point, we know that we are masking the result of the |
| 2249 | // ADD down to exactly one bit. If the constant we are adding has |
| 2250 | // no bits set below this bit, then we can eliminate the ADD. |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2251 | const APInt& AddRHS = cast<ConstantInt>(OpRHS)->getValue(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2252 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2253 | // Check to see if any bits below the one bit set in AndRHSV are set. |
| 2254 | if ((AddRHS & (AndRHSV-1)) == 0) { |
| 2255 | // If not, the only thing that can effect the output of the AND is |
| 2256 | // the bit specified by AndRHSV. If that bit is set, the effect of |
| 2257 | // the XOR is to toggle the bit. If it is clear, then the ADD has |
| 2258 | // no effect. |
| 2259 | if ((AddRHS & AndRHSV) == 0) { // Bit is not set, noop |
| 2260 | TheAnd.setOperand(0, X); |
| 2261 | return &TheAnd; |
| 2262 | } else { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2263 | // Pull the XOR out of the AND. |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 2264 | Value *NewAnd = Builder->CreateAnd(X, AndRHS); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 2265 | NewAnd->takeName(Op); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2266 | return BinaryOperator::CreateXor(NewAnd, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2267 | } |
| 2268 | } |
| 2269 | } |
| 2270 | } |
| 2271 | break; |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 2272 | |
| 2273 | case Instruction::Shl: { |
| 2274 | // We know that the AND will not produce any of the bits shifted in, so if |
| 2275 | // the anded constant includes them, clear them now! |
| 2276 | // |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 2277 | uint32_t BitWidth = AndRHS->getType()->getBitWidth(); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 2278 | uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 2279 | APInt ShlMask(APInt::getHighBitsSet(BitWidth, BitWidth-OpRHSVal)); |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 2280 | ConstantInt *CI = ConstantInt::get(AndRHS->getContext(), |
| 2281 | AndRHS->getValue() & ShlMask); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2282 | |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 2283 | if (CI->getValue() == ShlMask) { |
| 2284 | // Masking out bits that the shift already masks |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 2285 | return ReplaceInstUsesWith(TheAnd, Op); // No need for the and. |
| 2286 | } else if (CI != AndRHS) { // Reducing bits set in and. |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 2287 | TheAnd.setOperand(1, CI); |
| 2288 | return &TheAnd; |
| 2289 | } |
| 2290 | break; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2291 | } |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 2292 | case Instruction::LShr: { |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 2293 | // We know that the AND will not produce any of the bits shifted in, so if |
| 2294 | // the anded constant includes them, clear them now! This only applies to |
| 2295 | // unsigned shifts, because a signed shr may bring in set bits! |
| 2296 | // |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 2297 | uint32_t BitWidth = AndRHS->getType()->getBitWidth(); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 2298 | uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 2299 | APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal)); |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 2300 | ConstantInt *CI = ConstantInt::get(Op->getContext(), |
| 2301 | AndRHS->getValue() & ShrMask); |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 2302 | |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 2303 | if (CI->getValue() == ShrMask) { |
| 2304 | // Masking out bits that the shift already masks. |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2305 | return ReplaceInstUsesWith(TheAnd, Op); |
| 2306 | } else if (CI != AndRHS) { |
| 2307 | TheAnd.setOperand(1, CI); // Reduce bits set in and cst. |
| 2308 | return &TheAnd; |
| 2309 | } |
| 2310 | break; |
| 2311 | } |
| 2312 | case Instruction::AShr: |
| 2313 | // Signed shr. |
| 2314 | // See if this is shifting in some sign extension, then masking it out |
| 2315 | // with an and. |
| 2316 | if (Op->hasOneUse()) { |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 2317 | uint32_t BitWidth = AndRHS->getType()->getBitWidth(); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 2318 | uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 2319 | APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal)); |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 2320 | Constant *C = ConstantInt::get(Op->getContext(), |
| 2321 | AndRHS->getValue() & ShrMask); |
Reid Spencer | 7eb7638 | 2006-12-13 17:19:09 +0000 | [diff] [blame] | 2322 | if (C == AndRHS) { // Masking out bits shifted in. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 2323 | // (Val ashr C1) & C2 -> (Val lshr C1) & C2 |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2324 | // Make the argument unsigned. |
| 2325 | Value *ShVal = Op->getOperand(0); |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 2326 | ShVal = Builder->CreateLShr(ShVal, OpRHS, Op->getName()); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2327 | return BinaryOperator::CreateAnd(ShVal, AndRHS, TheAnd.getName()); |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 2328 | } |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 2329 | } |
| 2330 | break; |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2331 | } |
| 2332 | return 0; |
| 2333 | } |
| 2334 | |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2335 | |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2336 | /// InsertRangeTest - Emit a computation of: (V >= Lo && V < Hi) if Inside is |
| 2337 | /// true, otherwise (V < Lo || V >= Hi). In pratice, we emit the more efficient |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2338 | /// (V-Lo) <u Hi-Lo. This method expects that Lo <= Hi. isSigned indicates |
| 2339 | /// whether to treat the V, Lo and HI as signed or not. IB is the location to |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2340 | /// insert new instructions. |
| 2341 | Instruction *InstCombiner::InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2342 | bool isSigned, bool Inside, |
| 2343 | Instruction &IB) { |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2344 | assert(cast<ConstantInt>(ConstantExpr::getICmp((isSigned ? |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 2345 | ICmpInst::ICMP_SLE:ICmpInst::ICMP_ULE), Lo, Hi))->getZExtValue() && |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2346 | "Lo is not <= Hi in range emission code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2347 | |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2348 | if (Inside) { |
| 2349 | if (Lo == Hi) // Trivially false. |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2350 | return new ICmpInst(ICmpInst::ICMP_NE, V, V); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2351 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2352 | // V >= Min && V < Hi --> V < Hi |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2353 | if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) { |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 2354 | ICmpInst::Predicate pred = (isSigned ? |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2355 | ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT); |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2356 | return new ICmpInst(pred, V, Hi); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2357 | } |
| 2358 | |
| 2359 | // Emit V-Lo <u Hi-Lo |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2360 | Constant *NegLo = ConstantExpr::getNeg(Lo); |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 2361 | Value *Add = Builder->CreateAdd(V, NegLo, V->getName()+".off"); |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2362 | Constant *UpperBound = ConstantExpr::getAdd(NegLo, Hi); |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2363 | return new ICmpInst(ICmpInst::ICMP_ULT, Add, UpperBound); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2364 | } |
| 2365 | |
| 2366 | if (Lo == Hi) // Trivially true. |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2367 | return new ICmpInst(ICmpInst::ICMP_EQ, V, V); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2368 | |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 2369 | // V < Min || V >= Hi -> V > Hi-1 |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 2370 | Hi = SubOne(cast<ConstantInt>(Hi)); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2371 | if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2372 | ICmpInst::Predicate pred = (isSigned ? |
| 2373 | ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT); |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2374 | return new ICmpInst(pred, V, Hi); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2375 | } |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2376 | |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 2377 | // Emit V-Lo >u Hi-1-Lo |
| 2378 | // Note that Hi has already had one subtracted from it, above. |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2379 | ConstantInt *NegLo = cast<ConstantInt>(ConstantExpr::getNeg(Lo)); |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 2380 | Value *Add = Builder->CreateAdd(V, NegLo, V->getName()+".off"); |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2381 | Constant *LowerBound = ConstantExpr::getAdd(NegLo, Hi); |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2382 | return new ICmpInst(ICmpInst::ICMP_UGT, Add, LowerBound); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2383 | } |
| 2384 | |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 2385 | // isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with |
| 2386 | // any number of 0s on either side. The 1s are allowed to wrap from LSB to |
| 2387 | // MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is |
| 2388 | // not, since all 1s are not contiguous. |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 2389 | static bool isRunOfOnes(ConstantInt *Val, uint32_t &MB, uint32_t &ME) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2390 | const APInt& V = Val->getValue(); |
Reid Spencer | f244252 | 2007-03-24 00:42:08 +0000 | [diff] [blame] | 2391 | uint32_t BitWidth = Val->getType()->getBitWidth(); |
| 2392 | if (!APIntOps::isShiftedMask(BitWidth, V)) return false; |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 2393 | |
| 2394 | // look for the first zero bit after the run of ones |
Reid Spencer | f244252 | 2007-03-24 00:42:08 +0000 | [diff] [blame] | 2395 | MB = BitWidth - ((V - 1) ^ V).countLeadingZeros(); |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 2396 | // look for the first non-zero bit |
Reid Spencer | f244252 | 2007-03-24 00:42:08 +0000 | [diff] [blame] | 2397 | ME = V.getActiveBits(); |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 2398 | return true; |
| 2399 | } |
| 2400 | |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 2401 | /// FoldLogicalPlusAnd - This is part of an expression (LHS +/- RHS) & Mask, |
| 2402 | /// where isSub determines whether the operator is a sub. If we can fold one of |
| 2403 | /// the following xforms: |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 2404 | /// |
| 2405 | /// ((A & N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == Mask |
| 2406 | /// ((A | N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0 |
| 2407 | /// ((A ^ N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0 |
| 2408 | /// |
| 2409 | /// return (A +/- B). |
| 2410 | /// |
| 2411 | Value *InstCombiner::FoldLogicalPlusAnd(Value *LHS, Value *RHS, |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2412 | ConstantInt *Mask, bool isSub, |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 2413 | Instruction &I) { |
| 2414 | Instruction *LHSI = dyn_cast<Instruction>(LHS); |
| 2415 | if (!LHSI || LHSI->getNumOperands() != 2 || |
| 2416 | !isa<ConstantInt>(LHSI->getOperand(1))) return 0; |
| 2417 | |
| 2418 | ConstantInt *N = cast<ConstantInt>(LHSI->getOperand(1)); |
| 2419 | |
| 2420 | switch (LHSI->getOpcode()) { |
| 2421 | default: return 0; |
| 2422 | case Instruction::And: |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2423 | if (ConstantExpr::getAnd(N, Mask) == Mask) { |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 2424 | // If the AndRHS is a power of two minus one (0+1+), this is simple. |
Zhou Sheng | 00f436c | 2007-03-24 15:34:37 +0000 | [diff] [blame] | 2425 | if ((Mask->getValue().countLeadingZeros() + |
| 2426 | Mask->getValue().countPopulation()) == |
| 2427 | Mask->getValue().getBitWidth()) |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 2428 | break; |
| 2429 | |
| 2430 | // Otherwise, if Mask is 0+1+0+, and if B is known to have the low 0+ |
| 2431 | // part, we don't need any explicit masks to take them out of A. If that |
| 2432 | // is all N is, ignore it. |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 2433 | uint32_t MB = 0, ME = 0; |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 2434 | if (isRunOfOnes(Mask, MB, ME)) { // begin/end bit of run, inclusive |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 2435 | uint32_t BitWidth = cast<IntegerType>(RHS->getType())->getBitWidth(); |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 2436 | APInt Mask(APInt::getLowBitsSet(BitWidth, MB-1)); |
Chris Lattner | 3bedbd9 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 2437 | if (MaskedValueIsZero(RHS, Mask)) |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 2438 | break; |
| 2439 | } |
| 2440 | } |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 2441 | return 0; |
| 2442 | case Instruction::Or: |
| 2443 | case Instruction::Xor: |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 2444 | // If the AndRHS is a power of two minus one (0+1+), and N&Mask == 0 |
Zhou Sheng | 00f436c | 2007-03-24 15:34:37 +0000 | [diff] [blame] | 2445 | if ((Mask->getValue().countLeadingZeros() + |
| 2446 | Mask->getValue().countPopulation()) == Mask->getValue().getBitWidth() |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2447 | && ConstantExpr::getAnd(N, Mask)->isNullValue()) |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 2448 | break; |
| 2449 | return 0; |
| 2450 | } |
| 2451 | |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 2452 | if (isSub) |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 2453 | return Builder->CreateSub(LHSI->getOperand(0), RHS, "fold"); |
| 2454 | return Builder->CreateAdd(LHSI->getOperand(0), RHS, "fold"); |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 2455 | } |
| 2456 | |
Chris Lattner | 29cd5ba | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 2457 | /// FoldAndOfICmps - Fold (icmp)&(icmp) if possible. |
| 2458 | Instruction *InstCombiner::FoldAndOfICmps(Instruction &I, |
| 2459 | ICmpInst *LHS, ICmpInst *RHS) { |
Chris Lattner | ea065fb | 2008-11-16 05:10:52 +0000 | [diff] [blame] | 2460 | Value *Val, *Val2; |
Chris Lattner | 29cd5ba | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 2461 | ConstantInt *LHSCst, *RHSCst; |
| 2462 | ICmpInst::Predicate LHSCC, RHSCC; |
| 2463 | |
Chris Lattner | ea065fb | 2008-11-16 05:10:52 +0000 | [diff] [blame] | 2464 | // This only handles icmp of constants: (icmp1 A, C1) & (icmp2 B, C2). |
Owen Anderson | c7d2ce7 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 2465 | if (!match(LHS, m_ICmp(LHSCC, m_Value(Val), |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 2466 | m_ConstantInt(LHSCst))) || |
Owen Anderson | c7d2ce7 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 2467 | !match(RHS, m_ICmp(RHSCC, m_Value(Val2), |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 2468 | m_ConstantInt(RHSCst)))) |
Chris Lattner | 29cd5ba | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 2469 | return 0; |
Chris Lattner | ea065fb | 2008-11-16 05:10:52 +0000 | [diff] [blame] | 2470 | |
Chris Lattner | 3f40e23 | 2009-11-29 00:51:17 +0000 | [diff] [blame] | 2471 | if (LHSCst == RHSCst && LHSCC == RHSCC) { |
| 2472 | // (icmp ult A, C) & (icmp ult B, C) --> (icmp ult (A|B), C) |
| 2473 | // where C is a power of 2 |
| 2474 | if (LHSCC == ICmpInst::ICMP_ULT && |
| 2475 | LHSCst->getValue().isPowerOf2()) { |
| 2476 | Value *NewOr = Builder->CreateOr(Val, Val2); |
| 2477 | return new ICmpInst(LHSCC, NewOr, LHSCst); |
| 2478 | } |
| 2479 | |
| 2480 | // (icmp eq A, 0) & (icmp eq B, 0) --> (icmp eq (A|B), 0) |
| 2481 | if (LHSCC == ICmpInst::ICMP_EQ && LHSCst->isZero()) { |
| 2482 | Value *NewOr = Builder->CreateOr(Val, Val2); |
| 2483 | return new ICmpInst(LHSCC, NewOr, LHSCst); |
| 2484 | } |
Chris Lattner | ea065fb | 2008-11-16 05:10:52 +0000 | [diff] [blame] | 2485 | } |
| 2486 | |
| 2487 | // From here on, we only handle: |
| 2488 | // (icmp1 A, C1) & (icmp2 A, C2) --> something simpler. |
| 2489 | if (Val != Val2) return 0; |
| 2490 | |
Chris Lattner | 29cd5ba | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 2491 | // ICMP_[US][GL]E X, CST is folded to ICMP_[US][GL]T elsewhere. |
| 2492 | if (LHSCC == ICmpInst::ICMP_UGE || LHSCC == ICmpInst::ICMP_ULE || |
| 2493 | RHSCC == ICmpInst::ICMP_UGE || RHSCC == ICmpInst::ICMP_ULE || |
| 2494 | LHSCC == ICmpInst::ICMP_SGE || LHSCC == ICmpInst::ICMP_SLE || |
| 2495 | RHSCC == ICmpInst::ICMP_SGE || RHSCC == ICmpInst::ICMP_SLE) |
| 2496 | return 0; |
| 2497 | |
| 2498 | // We can't fold (ugt x, C) & (sgt x, C2). |
| 2499 | if (!PredicatesFoldable(LHSCC, RHSCC)) |
| 2500 | return 0; |
| 2501 | |
| 2502 | // Ensure that the larger constant is on the RHS. |
Chris Lattner | aa3e157 | 2008-11-16 05:14:43 +0000 | [diff] [blame] | 2503 | bool ShouldSwap; |
Nick Lewycky | 4a134af | 2009-10-25 05:20:17 +0000 | [diff] [blame] | 2504 | if (CmpInst::isSigned(LHSCC) || |
Chris Lattner | 29cd5ba | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 2505 | (ICmpInst::isEquality(LHSCC) && |
Nick Lewycky | 4a134af | 2009-10-25 05:20:17 +0000 | [diff] [blame] | 2506 | CmpInst::isSigned(RHSCC))) |
Chris Lattner | aa3e157 | 2008-11-16 05:14:43 +0000 | [diff] [blame] | 2507 | ShouldSwap = LHSCst->getValue().sgt(RHSCst->getValue()); |
Chris Lattner | 29cd5ba | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 2508 | else |
Chris Lattner | aa3e157 | 2008-11-16 05:14:43 +0000 | [diff] [blame] | 2509 | ShouldSwap = LHSCst->getValue().ugt(RHSCst->getValue()); |
| 2510 | |
| 2511 | if (ShouldSwap) { |
Chris Lattner | 29cd5ba | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 2512 | std::swap(LHS, RHS); |
| 2513 | std::swap(LHSCst, RHSCst); |
| 2514 | std::swap(LHSCC, RHSCC); |
| 2515 | } |
| 2516 | |
| 2517 | // At this point, we know we have have two icmp instructions |
| 2518 | // comparing a value against two constants and and'ing the result |
| 2519 | // together. Because of the above check, we know that we only have |
| 2520 | // icmp eq, icmp ne, icmp [su]lt, and icmp [SU]gt here. We also know |
| 2521 | // (from the FoldICmpLogical check above), that the two constants |
| 2522 | // are not equal and that the larger constant is on the RHS |
| 2523 | assert(LHSCst != RHSCst && "Compares not folded above?"); |
| 2524 | |
| 2525 | switch (LHSCC) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 2526 | default: llvm_unreachable("Unknown integer condition code!"); |
Chris Lattner | 29cd5ba | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 2527 | case ICmpInst::ICMP_EQ: |
| 2528 | switch (RHSCC) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 2529 | default: llvm_unreachable("Unknown integer condition code!"); |
Chris Lattner | 29cd5ba | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 2530 | case ICmpInst::ICMP_EQ: // (X == 13 & X == 15) -> false |
| 2531 | case ICmpInst::ICMP_UGT: // (X == 13 & X > 15) -> false |
| 2532 | case ICmpInst::ICMP_SGT: // (X == 13 & X > 15) -> false |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 2533 | return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext())); |
Chris Lattner | 29cd5ba | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 2534 | case ICmpInst::ICMP_NE: // (X == 13 & X != 15) -> X == 13 |
| 2535 | case ICmpInst::ICMP_ULT: // (X == 13 & X < 15) -> X == 13 |
| 2536 | case ICmpInst::ICMP_SLT: // (X == 13 & X < 15) -> X == 13 |
| 2537 | return ReplaceInstUsesWith(I, LHS); |
| 2538 | } |
| 2539 | case ICmpInst::ICMP_NE: |
| 2540 | switch (RHSCC) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 2541 | default: llvm_unreachable("Unknown integer condition code!"); |
Chris Lattner | 29cd5ba | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 2542 | case ICmpInst::ICMP_ULT: |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 2543 | if (LHSCst == SubOne(RHSCst)) // (X != 13 & X u< 14) -> X < 13 |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2544 | return new ICmpInst(ICmpInst::ICMP_ULT, Val, LHSCst); |
Chris Lattner | 29cd5ba | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 2545 | break; // (X != 13 & X u< 15) -> no change |
| 2546 | case ICmpInst::ICMP_SLT: |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 2547 | if (LHSCst == SubOne(RHSCst)) // (X != 13 & X s< 14) -> X < 13 |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2548 | return new ICmpInst(ICmpInst::ICMP_SLT, Val, LHSCst); |
Chris Lattner | 29cd5ba | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 2549 | break; // (X != 13 & X s< 15) -> no change |
| 2550 | case ICmpInst::ICMP_EQ: // (X != 13 & X == 15) -> X == 15 |
| 2551 | case ICmpInst::ICMP_UGT: // (X != 13 & X u> 15) -> X u> 15 |
| 2552 | case ICmpInst::ICMP_SGT: // (X != 13 & X s> 15) -> X s> 15 |
| 2553 | return ReplaceInstUsesWith(I, RHS); |
| 2554 | case ICmpInst::ICMP_NE: |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 2555 | if (LHSCst == SubOne(RHSCst)){// (X != 13 & X != 14) -> X-13 >u 1 |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2556 | Constant *AddCST = ConstantExpr::getNeg(LHSCst); |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 2557 | Value *Add = Builder->CreateAdd(Val, AddCST, Val->getName()+".off"); |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2558 | return new ICmpInst(ICmpInst::ICMP_UGT, Add, |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 2559 | ConstantInt::get(Add->getType(), 1)); |
Chris Lattner | 29cd5ba | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 2560 | } |
| 2561 | break; // (X != 13 & X != 15) -> no change |
| 2562 | } |
| 2563 | break; |
| 2564 | case ICmpInst::ICMP_ULT: |
| 2565 | switch (RHSCC) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 2566 | default: llvm_unreachable("Unknown integer condition code!"); |
Chris Lattner | 29cd5ba | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 2567 | case ICmpInst::ICMP_EQ: // (X u< 13 & X == 15) -> false |
| 2568 | case ICmpInst::ICMP_UGT: // (X u< 13 & X u> 15) -> false |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 2569 | return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext())); |
Chris Lattner | 29cd5ba | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 2570 | case ICmpInst::ICMP_SGT: // (X u< 13 & X s> 15) -> no change |
| 2571 | break; |
| 2572 | case ICmpInst::ICMP_NE: // (X u< 13 & X != 15) -> X u< 13 |
| 2573 | case ICmpInst::ICMP_ULT: // (X u< 13 & X u< 15) -> X u< 13 |
| 2574 | return ReplaceInstUsesWith(I, LHS); |
| 2575 | case ICmpInst::ICMP_SLT: // (X u< 13 & X s< 15) -> no change |
| 2576 | break; |
| 2577 | } |
| 2578 | break; |
| 2579 | case ICmpInst::ICMP_SLT: |
| 2580 | switch (RHSCC) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 2581 | default: llvm_unreachable("Unknown integer condition code!"); |
Chris Lattner | 29cd5ba | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 2582 | case ICmpInst::ICMP_EQ: // (X s< 13 & X == 15) -> false |
| 2583 | case ICmpInst::ICMP_SGT: // (X s< 13 & X s> 15) -> false |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 2584 | return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext())); |
Chris Lattner | 29cd5ba | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 2585 | case ICmpInst::ICMP_UGT: // (X s< 13 & X u> 15) -> no change |
| 2586 | break; |
| 2587 | case ICmpInst::ICMP_NE: // (X s< 13 & X != 15) -> X < 13 |
| 2588 | case ICmpInst::ICMP_SLT: // (X s< 13 & X s< 15) -> X < 13 |
| 2589 | return ReplaceInstUsesWith(I, LHS); |
| 2590 | case ICmpInst::ICMP_ULT: // (X s< 13 & X u< 15) -> no change |
| 2591 | break; |
| 2592 | } |
| 2593 | break; |
| 2594 | case ICmpInst::ICMP_UGT: |
| 2595 | switch (RHSCC) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 2596 | default: llvm_unreachable("Unknown integer condition code!"); |
Chris Lattner | 29cd5ba | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 2597 | case ICmpInst::ICMP_EQ: // (X u> 13 & X == 15) -> X == 15 |
| 2598 | case ICmpInst::ICMP_UGT: // (X u> 13 & X u> 15) -> X u> 15 |
| 2599 | return ReplaceInstUsesWith(I, RHS); |
| 2600 | case ICmpInst::ICMP_SGT: // (X u> 13 & X s> 15) -> no change |
| 2601 | break; |
| 2602 | case ICmpInst::ICMP_NE: |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 2603 | if (RHSCst == AddOne(LHSCst)) // (X u> 13 & X != 14) -> X u> 14 |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2604 | return new ICmpInst(LHSCC, Val, RHSCst); |
Chris Lattner | 29cd5ba | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 2605 | break; // (X u> 13 & X != 15) -> no change |
Chris Lattner | 69d4ced | 2008-11-16 05:20:07 +0000 | [diff] [blame] | 2606 | case ICmpInst::ICMP_ULT: // (X u> 13 & X u< 15) -> (X-14) <u 1 |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 2607 | return InsertRangeTest(Val, AddOne(LHSCst), |
Owen Anderson | d672ecb | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2608 | RHSCst, false, true, I); |
Chris Lattner | 29cd5ba | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 2609 | case ICmpInst::ICMP_SLT: // (X u> 13 & X s< 15) -> no change |
| 2610 | break; |
| 2611 | } |
| 2612 | break; |
| 2613 | case ICmpInst::ICMP_SGT: |
| 2614 | switch (RHSCC) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 2615 | default: llvm_unreachable("Unknown integer condition code!"); |
Chris Lattner | 29cd5ba | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 2616 | case ICmpInst::ICMP_EQ: // (X s> 13 & X == 15) -> X == 15 |
| 2617 | case ICmpInst::ICMP_SGT: // (X s> 13 & X s> 15) -> X s> 15 |
| 2618 | return ReplaceInstUsesWith(I, RHS); |
| 2619 | case ICmpInst::ICMP_UGT: // (X s> 13 & X u> 15) -> no change |
| 2620 | break; |
| 2621 | case ICmpInst::ICMP_NE: |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 2622 | if (RHSCst == AddOne(LHSCst)) // (X s> 13 & X != 14) -> X s> 14 |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2623 | return new ICmpInst(LHSCC, Val, RHSCst); |
Chris Lattner | 29cd5ba | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 2624 | break; // (X s> 13 & X != 15) -> no change |
Chris Lattner | 69d4ced | 2008-11-16 05:20:07 +0000 | [diff] [blame] | 2625 | case ICmpInst::ICMP_SLT: // (X s> 13 & X s< 15) -> (X-14) s< 1 |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 2626 | return InsertRangeTest(Val, AddOne(LHSCst), |
Owen Anderson | d672ecb | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2627 | RHSCst, true, true, I); |
Chris Lattner | 29cd5ba | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 2628 | case ICmpInst::ICMP_ULT: // (X s> 13 & X u< 15) -> no change |
| 2629 | break; |
| 2630 | } |
| 2631 | break; |
| 2632 | } |
Chris Lattner | 29cd5ba | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 2633 | |
| 2634 | return 0; |
| 2635 | } |
| 2636 | |
Chris Lattner | 42d1be0 | 2009-07-23 05:14:02 +0000 | [diff] [blame] | 2637 | Instruction *InstCombiner::FoldAndOfFCmps(Instruction &I, FCmpInst *LHS, |
| 2638 | FCmpInst *RHS) { |
| 2639 | |
| 2640 | if (LHS->getPredicate() == FCmpInst::FCMP_ORD && |
| 2641 | RHS->getPredicate() == FCmpInst::FCMP_ORD) { |
| 2642 | // (fcmp ord x, c) & (fcmp ord y, c) -> (fcmp ord x, y) |
| 2643 | if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1))) |
| 2644 | if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) { |
| 2645 | // If either of the constants are nans, then the whole thing returns |
| 2646 | // false. |
| 2647 | if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN()) |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 2648 | return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext())); |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2649 | return new FCmpInst(FCmpInst::FCMP_ORD, |
Chris Lattner | 42d1be0 | 2009-07-23 05:14:02 +0000 | [diff] [blame] | 2650 | LHS->getOperand(0), RHS->getOperand(0)); |
| 2651 | } |
Chris Lattner | f98d253 | 2009-07-23 05:32:17 +0000 | [diff] [blame] | 2652 | |
| 2653 | // Handle vector zeros. This occurs because the canonical form of |
| 2654 | // "fcmp ord x,x" is "fcmp ord x, 0". |
| 2655 | if (isa<ConstantAggregateZero>(LHS->getOperand(1)) && |
| 2656 | isa<ConstantAggregateZero>(RHS->getOperand(1))) |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2657 | return new FCmpInst(FCmpInst::FCMP_ORD, |
Chris Lattner | f98d253 | 2009-07-23 05:32:17 +0000 | [diff] [blame] | 2658 | LHS->getOperand(0), RHS->getOperand(0)); |
Chris Lattner | 42d1be0 | 2009-07-23 05:14:02 +0000 | [diff] [blame] | 2659 | return 0; |
| 2660 | } |
| 2661 | |
| 2662 | Value *Op0LHS = LHS->getOperand(0), *Op0RHS = LHS->getOperand(1); |
| 2663 | Value *Op1LHS = RHS->getOperand(0), *Op1RHS = RHS->getOperand(1); |
| 2664 | FCmpInst::Predicate Op0CC = LHS->getPredicate(), Op1CC = RHS->getPredicate(); |
| 2665 | |
| 2666 | |
| 2667 | if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) { |
| 2668 | // Swap RHS operands to match LHS. |
| 2669 | Op1CC = FCmpInst::getSwappedPredicate(Op1CC); |
| 2670 | std::swap(Op1LHS, Op1RHS); |
| 2671 | } |
| 2672 | |
| 2673 | if (Op0LHS == Op1LHS && Op0RHS == Op1RHS) { |
| 2674 | // Simplify (fcmp cc0 x, y) & (fcmp cc1 x, y). |
| 2675 | if (Op0CC == Op1CC) |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2676 | return new FCmpInst((FCmpInst::Predicate)Op0CC, Op0LHS, Op0RHS); |
Chris Lattner | 42d1be0 | 2009-07-23 05:14:02 +0000 | [diff] [blame] | 2677 | |
| 2678 | if (Op0CC == FCmpInst::FCMP_FALSE || Op1CC == FCmpInst::FCMP_FALSE) |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 2679 | return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext())); |
Chris Lattner | 42d1be0 | 2009-07-23 05:14:02 +0000 | [diff] [blame] | 2680 | if (Op0CC == FCmpInst::FCMP_TRUE) |
| 2681 | return ReplaceInstUsesWith(I, RHS); |
| 2682 | if (Op1CC == FCmpInst::FCMP_TRUE) |
| 2683 | return ReplaceInstUsesWith(I, LHS); |
| 2684 | |
| 2685 | bool Op0Ordered; |
| 2686 | bool Op1Ordered; |
| 2687 | unsigned Op0Pred = getFCmpCode(Op0CC, Op0Ordered); |
| 2688 | unsigned Op1Pred = getFCmpCode(Op1CC, Op1Ordered); |
| 2689 | if (Op1Pred == 0) { |
| 2690 | std::swap(LHS, RHS); |
| 2691 | std::swap(Op0Pred, Op1Pred); |
| 2692 | std::swap(Op0Ordered, Op1Ordered); |
| 2693 | } |
| 2694 | if (Op0Pred == 0) { |
| 2695 | // uno && ueq -> uno && (uno || eq) -> ueq |
| 2696 | // ord && olt -> ord && (ord && lt) -> olt |
| 2697 | if (Op0Ordered == Op1Ordered) |
| 2698 | return ReplaceInstUsesWith(I, RHS); |
| 2699 | |
| 2700 | // uno && oeq -> uno && (ord && eq) -> false |
| 2701 | // uno && ord -> false |
| 2702 | if (!Op0Ordered) |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 2703 | return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext())); |
Chris Lattner | 42d1be0 | 2009-07-23 05:14:02 +0000 | [diff] [blame] | 2704 | // ord && ueq -> ord && (uno || eq) -> oeq |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 2705 | return cast<Instruction>(getFCmpValue(true, Op1Pred, Op0LHS, Op0RHS)); |
Chris Lattner | 42d1be0 | 2009-07-23 05:14:02 +0000 | [diff] [blame] | 2706 | } |
| 2707 | } |
| 2708 | |
| 2709 | return 0; |
| 2710 | } |
| 2711 | |
Chris Lattner | 29cd5ba | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 2712 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2713 | Instruction *InstCombiner::visitAnd(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 2714 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2715 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2716 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 2717 | if (Value *V = SimplifyAndInst(Op0, Op1, TD)) |
| 2718 | return ReplaceInstUsesWith(I, V); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2719 | |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 2720 | // See if we can simplify any instructions used by the instruction whose sole |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 2721 | // purpose is to compute bits we don't care about. |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2722 | if (SimplifyDemandedInstructionBits(I)) |
Nick Lewycky | 546d631 | 2010-01-02 15:25:44 +0000 | [diff] [blame] | 2723 | return &I; |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2724 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2725 | if (ConstantInt *AndRHS = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 7acdf1d | 2009-10-11 22:00:32 +0000 | [diff] [blame] | 2726 | const APInt &AndRHSMask = AndRHS->getValue(); |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2727 | APInt NotAndRHS(~AndRHSMask); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2728 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2729 | // Optimize a variety of ((val OP C1) & C2) combinations... |
Chris Lattner | 7acdf1d | 2009-10-11 22:00:32 +0000 | [diff] [blame] | 2730 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) { |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2731 | Value *Op0LHS = Op0I->getOperand(0); |
| 2732 | Value *Op0RHS = Op0I->getOperand(1); |
| 2733 | switch (Op0I->getOpcode()) { |
Chris Lattner | 7acdf1d | 2009-10-11 22:00:32 +0000 | [diff] [blame] | 2734 | default: break; |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2735 | case Instruction::Xor: |
| 2736 | case Instruction::Or: |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 2737 | // If the mask is only needed on one incoming arm, push it up. |
Chris Lattner | 7acdf1d | 2009-10-11 22:00:32 +0000 | [diff] [blame] | 2738 | if (!Op0I->hasOneUse()) break; |
| 2739 | |
| 2740 | if (MaskedValueIsZero(Op0LHS, NotAndRHS)) { |
| 2741 | // Not masking anything out for the LHS, move to RHS. |
| 2742 | Value *NewRHS = Builder->CreateAnd(Op0RHS, AndRHS, |
| 2743 | Op0RHS->getName()+".masked"); |
| 2744 | return BinaryOperator::Create(Op0I->getOpcode(), Op0LHS, NewRHS); |
| 2745 | } |
| 2746 | if (!isa<Constant>(Op0RHS) && |
| 2747 | MaskedValueIsZero(Op0RHS, NotAndRHS)) { |
| 2748 | // Not masking anything out for the RHS, move to LHS. |
| 2749 | Value *NewLHS = Builder->CreateAnd(Op0LHS, AndRHS, |
| 2750 | Op0LHS->getName()+".masked"); |
| 2751 | return BinaryOperator::Create(Op0I->getOpcode(), NewLHS, Op0RHS); |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 2752 | } |
| 2753 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2754 | break; |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 2755 | case Instruction::Add: |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 2756 | // ((A & N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == AndRHS. |
| 2757 | // ((A | N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0 |
| 2758 | // ((A ^ N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0 |
| 2759 | if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, false, I)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2760 | return BinaryOperator::CreateAnd(V, AndRHS); |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 2761 | if (Value *V = FoldLogicalPlusAnd(Op0RHS, Op0LHS, AndRHS, false, I)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2762 | return BinaryOperator::CreateAnd(V, AndRHS); // Add commutes |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 2763 | break; |
| 2764 | |
| 2765 | case Instruction::Sub: |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 2766 | // ((A & N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == AndRHS. |
| 2767 | // ((A | N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0 |
| 2768 | // ((A ^ N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0 |
| 2769 | if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, true, I)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2770 | return BinaryOperator::CreateAnd(V, AndRHS); |
Nick Lewycky | b4d1bc9 | 2008-07-09 04:32:37 +0000 | [diff] [blame] | 2771 | |
Nick Lewycky | 5dcc41f | 2008-07-10 05:51:40 +0000 | [diff] [blame] | 2772 | // (A - N) & AndRHS -> -N & AndRHS iff A&AndRHS==0 and AndRHS |
| 2773 | // has 1's for all bits that the subtraction with A might affect. |
| 2774 | if (Op0I->hasOneUse()) { |
| 2775 | uint32_t BitWidth = AndRHSMask.getBitWidth(); |
| 2776 | uint32_t Zeros = AndRHSMask.countLeadingZeros(); |
| 2777 | APInt Mask = APInt::getLowBitsSet(BitWidth, BitWidth - Zeros); |
| 2778 | |
Nick Lewycky | b4d1bc9 | 2008-07-09 04:32:37 +0000 | [diff] [blame] | 2779 | ConstantInt *A = dyn_cast<ConstantInt>(Op0LHS); |
Nick Lewycky | 5dcc41f | 2008-07-10 05:51:40 +0000 | [diff] [blame] | 2780 | if (!(A && A->isZero()) && // avoid infinite recursion. |
| 2781 | MaskedValueIsZero(Op0LHS, Mask)) { |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 2782 | Value *NewNeg = Builder->CreateNeg(Op0RHS); |
Nick Lewycky | b4d1bc9 | 2008-07-09 04:32:37 +0000 | [diff] [blame] | 2783 | return BinaryOperator::CreateAnd(NewNeg, AndRHS); |
| 2784 | } |
| 2785 | } |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 2786 | break; |
Nick Lewycky | d1f77bf | 2008-07-09 05:20:13 +0000 | [diff] [blame] | 2787 | |
| 2788 | case Instruction::Shl: |
| 2789 | case Instruction::LShr: |
| 2790 | // (1 << x) & 1 --> zext(x == 0) |
| 2791 | // (1 >> x) & 1 --> zext(x == 0) |
Nick Lewycky | d8ad492 | 2008-07-09 07:35:26 +0000 | [diff] [blame] | 2792 | if (AndRHSMask == 1 && Op0LHS == AndRHS) { |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 2793 | Value *NewICmp = |
| 2794 | Builder->CreateICmpEQ(Op0RHS, Constant::getNullValue(I.getType())); |
Nick Lewycky | d1f77bf | 2008-07-09 05:20:13 +0000 | [diff] [blame] | 2795 | return new ZExtInst(NewICmp, I.getType()); |
| 2796 | } |
| 2797 | break; |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2798 | } |
| 2799 | |
Chris Lattner | 5840326 | 2003-07-23 19:25:52 +0000 | [diff] [blame] | 2800 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2801 | if (Instruction *Res = OptAndOp(Op0I, Op0CI, AndRHS, I)) |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2802 | return Res; |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2803 | } else if (CastInst *CI = dyn_cast<CastInst>(Op0)) { |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 2804 | // If this is an integer truncation or change from signed-to-unsigned, and |
| 2805 | // if the source is an and/or with immediate, transform it. This |
| 2806 | // frequently occurs for bitfield accesses. |
| 2807 | if (Instruction *CastOp = dyn_cast<Instruction>(CI->getOperand(0))) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2808 | if ((isa<TruncInst>(CI) || isa<BitCastInst>(CI)) && |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 2809 | CastOp->getNumOperands() == 2) |
Chris Lattner | 48b59ec | 2009-10-26 15:40:07 +0000 | [diff] [blame] | 2810 | if (ConstantInt *AndCI =dyn_cast<ConstantInt>(CastOp->getOperand(1))){ |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 2811 | if (CastOp->getOpcode() == Instruction::And) { |
| 2812 | // Change: and (cast (and X, C1) to T), C2 |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2813 | // into : and (cast X to T), trunc_or_bitcast(C1)&C2 |
| 2814 | // This will fold the two constants together, which may allow |
| 2815 | // other simplifications. |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 2816 | Value *NewCast = Builder->CreateTruncOrBitCast( |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 2817 | CastOp->getOperand(0), I.getType(), |
| 2818 | CastOp->getName()+".shrunk"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2819 | // trunc_or_bitcast(C1)&C2 |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 2820 | Constant *C3 = ConstantExpr::getTruncOrBitCast(AndCI,I.getType()); |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2821 | C3 = ConstantExpr::getAnd(C3, AndRHS); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2822 | return BinaryOperator::CreateAnd(NewCast, C3); |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 2823 | } else if (CastOp->getOpcode() == Instruction::Or) { |
| 2824 | // Change: and (cast (or X, C1) to T), C2 |
| 2825 | // into : trunc(C1)&C2 iff trunc(C1)&C2 == C2 |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 2826 | Constant *C3 = ConstantExpr::getTruncOrBitCast(AndCI,I.getType()); |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2827 | if (ConstantExpr::getAnd(C3, AndRHS) == AndRHS) |
Owen Anderson | d672ecb | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2828 | // trunc(C1)&C2 |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 2829 | return ReplaceInstUsesWith(I, AndRHS); |
| 2830 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 2831 | } |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 2832 | } |
Chris Lattner | 06782f8 | 2003-07-23 19:36:21 +0000 | [diff] [blame] | 2833 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2834 | |
| 2835 | // Try to fold constant and into select arguments. |
| 2836 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame^] | 2837 | if (Instruction *R = FoldOpIntoSelect(I, SI)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2838 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2839 | if (isa<PHINode>(Op0)) |
| 2840 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2841 | return NV; |
Chris Lattner | c6a8aff | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 2842 | } |
| 2843 | |
Chris Lattner | 5b62aa7 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 2844 | |
Misha Brukman | cb6267b | 2004-07-30 12:50:08 +0000 | [diff] [blame] | 2845 | // (~A & ~B) == (~(A | B)) - De Morgan's Law |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 2846 | if (Value *Op0NotVal = dyn_castNotVal(Op0)) |
| 2847 | if (Value *Op1NotVal = dyn_castNotVal(Op1)) |
| 2848 | if (Op0->hasOneUse() && Op1->hasOneUse()) { |
| 2849 | Value *Or = Builder->CreateOr(Op0NotVal, Op1NotVal, |
| 2850 | I.getName()+".demorgan"); |
| 2851 | return BinaryOperator::CreateNot(Or); |
| 2852 | } |
| 2853 | |
Chris Lattner | 2082ad9 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 2854 | { |
Chris Lattner | 003b620 | 2007-06-15 05:58:24 +0000 | [diff] [blame] | 2855 | Value *A = 0, *B = 0, *C = 0, *D = 0; |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 2856 | // (A|B) & ~(A&B) -> A^B |
| 2857 | if (match(Op0, m_Or(m_Value(A), m_Value(B))) && |
| 2858 | match(Op1, m_Not(m_And(m_Value(C), m_Value(D)))) && |
| 2859 | ((A == C && B == D) || (A == D && B == C))) |
| 2860 | return BinaryOperator::CreateXor(A, B); |
Chris Lattner | 003b620 | 2007-06-15 05:58:24 +0000 | [diff] [blame] | 2861 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 2862 | // ~(A&B) & (A|B) -> A^B |
| 2863 | if (match(Op1, m_Or(m_Value(A), m_Value(B))) && |
| 2864 | match(Op0, m_Not(m_And(m_Value(C), m_Value(D)))) && |
| 2865 | ((A == C && B == D) || (A == D && B == C))) |
| 2866 | return BinaryOperator::CreateXor(A, B); |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 2867 | |
| 2868 | if (Op0->hasOneUse() && |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 2869 | match(Op0, m_Xor(m_Value(A), m_Value(B)))) { |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 2870 | if (A == Op1) { // (A^B)&A -> A&(A^B) |
| 2871 | I.swapOperands(); // Simplify below |
| 2872 | std::swap(Op0, Op1); |
| 2873 | } else if (B == Op1) { // (A^B)&B -> B&(B^A) |
| 2874 | cast<BinaryOperator>(Op0)->swapOperands(); |
| 2875 | I.swapOperands(); // Simplify below |
| 2876 | std::swap(Op0, Op1); |
| 2877 | } |
| 2878 | } |
Bill Wendling | 7f0ef6b | 2008-11-30 13:08:13 +0000 | [diff] [blame] | 2879 | |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 2880 | if (Op1->hasOneUse() && |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 2881 | match(Op1, m_Xor(m_Value(A), m_Value(B)))) { |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 2882 | if (B == Op0) { // B&(A^B) -> B&(B^A) |
| 2883 | cast<BinaryOperator>(Op1)->swapOperands(); |
| 2884 | std::swap(A, B); |
| 2885 | } |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 2886 | if (A == Op0) // A&(A^B) -> A & ~B |
| 2887 | return BinaryOperator::CreateAnd(A, Builder->CreateNot(B, "tmp")); |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 2888 | } |
Bill Wendling | 7f0ef6b | 2008-11-30 13:08:13 +0000 | [diff] [blame] | 2889 | |
| 2890 | // (A&((~A)|B)) -> A&B |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 2891 | if (match(Op0, m_Or(m_Not(m_Specific(Op1)), m_Value(A))) || |
| 2892 | match(Op0, m_Or(m_Value(A), m_Not(m_Specific(Op1))))) |
Chris Lattner | d8aafcb | 2008-12-01 05:16:26 +0000 | [diff] [blame] | 2893 | return BinaryOperator::CreateAnd(A, Op1); |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 2894 | if (match(Op1, m_Or(m_Not(m_Specific(Op0)), m_Value(A))) || |
| 2895 | match(Op1, m_Or(m_Value(A), m_Not(m_Specific(Op0))))) |
Chris Lattner | d8aafcb | 2008-12-01 05:16:26 +0000 | [diff] [blame] | 2896 | return BinaryOperator::CreateAnd(A, Op0); |
Chris Lattner | 2082ad9 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 2897 | } |
| 2898 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2899 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(Op1)) { |
| 2900 | // (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B) |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 2901 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS))) |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2902 | return R; |
| 2903 | |
Chris Lattner | 29cd5ba | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 2904 | if (ICmpInst *LHS = dyn_cast<ICmpInst>(Op0)) |
| 2905 | if (Instruction *Res = FoldAndOfICmps(I, LHS, RHS)) |
| 2906 | return Res; |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 2907 | } |
| 2908 | |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 2909 | // fold (and (cast A), (cast B)) -> (cast (and A, B)) |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 2910 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) |
| 2911 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) |
| 2912 | if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind ? |
| 2913 | const Type *SrcTy = Op0C->getOperand(0)->getType(); |
Chris Lattner | f98d253 | 2009-07-23 05:32:17 +0000 | [diff] [blame] | 2914 | if (SrcTy == Op1C->getOperand(0)->getType() && |
| 2915 | SrcTy->isIntOrIntVector() && |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 2916 | // Only do this if the casts both really cause code to be generated. |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame^] | 2917 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), |
| 2918 | I.getType()) && |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2919 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame^] | 2920 | I.getType())) { |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 2921 | Value *NewOp = Builder->CreateAnd(Op0C->getOperand(0), |
| 2922 | Op1C->getOperand(0), I.getName()); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2923 | return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType()); |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 2924 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 2925 | } |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 2926 | |
| 2927 | // (X >> Z) & (Y >> Z) -> (X&Y) >> Z for all shifts. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2928 | if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) { |
| 2929 | if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0)) |
| 2930 | if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() && |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 2931 | SI0->getOperand(1) == SI1->getOperand(1) && |
| 2932 | (SI0->hasOneUse() || SI1->hasOneUse())) { |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 2933 | Value *NewOp = |
| 2934 | Builder->CreateAnd(SI0->getOperand(0), SI1->getOperand(0), |
| 2935 | SI0->getName()); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2936 | return BinaryOperator::Create(SI1->getOpcode(), NewOp, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2937 | SI1->getOperand(1)); |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 2938 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 2939 | } |
| 2940 | |
Evan Cheng | 8db9072 | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 2941 | // If and'ing two fcmp, try combine them into one. |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 2942 | if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0))) { |
Chris Lattner | 42d1be0 | 2009-07-23 05:14:02 +0000 | [diff] [blame] | 2943 | if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1))) |
| 2944 | if (Instruction *Res = FoldAndOfFCmps(I, LHS, RHS)) |
| 2945 | return Res; |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 2946 | } |
Nick Lewycky | b4d1bc9 | 2008-07-09 04:32:37 +0000 | [diff] [blame] | 2947 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2948 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2949 | } |
| 2950 | |
Chris Lattner | 8c34cd2 | 2008-10-05 02:13:19 +0000 | [diff] [blame] | 2951 | /// CollectBSwapParts - Analyze the specified subexpression and see if it is |
| 2952 | /// capable of providing pieces of a bswap. The subexpression provides pieces |
| 2953 | /// of a bswap if it is proven that each of the non-zero bytes in the output of |
| 2954 | /// the expression came from the corresponding "byte swapped" byte in some other |
| 2955 | /// value. For example, if the current subexpression is "(shl i32 %X, 24)" then |
| 2956 | /// we know that the expression deposits the low byte of %X into the high byte |
| 2957 | /// of the bswap result and that all other bytes are zero. This expression is |
| 2958 | /// accepted, the high byte of ByteValues is set to X to indicate a correct |
| 2959 | /// match. |
| 2960 | /// |
| 2961 | /// This function returns true if the match was unsuccessful and false if so. |
| 2962 | /// On entry to the function the "OverallLeftShift" is a signed integer value |
| 2963 | /// indicating the number of bytes that the subexpression is later shifted. For |
| 2964 | /// example, if the expression is later right shifted by 16 bits, the |
| 2965 | /// OverallLeftShift value would be -2 on entry. This is used to specify which |
| 2966 | /// byte of ByteValues is actually being set. |
| 2967 | /// |
| 2968 | /// Similarly, ByteMask is a bitmask where a bit is clear if its corresponding |
| 2969 | /// byte is masked to zero by a user. For example, in (X & 255), X will be |
| 2970 | /// processed with a bytemask of 1. Because bytemask is 32-bits, this limits |
| 2971 | /// this function to working on up to 32-byte (256 bit) values. ByteMask is |
| 2972 | /// always in the local (OverallLeftShift) coordinate space. |
| 2973 | /// |
| 2974 | static bool CollectBSwapParts(Value *V, int OverallLeftShift, uint32_t ByteMask, |
| 2975 | SmallVector<Value*, 8> &ByteValues) { |
| 2976 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
| 2977 | // If this is an or instruction, it may be an inner node of the bswap. |
| 2978 | if (I->getOpcode() == Instruction::Or) { |
| 2979 | return CollectBSwapParts(I->getOperand(0), OverallLeftShift, ByteMask, |
| 2980 | ByteValues) || |
| 2981 | CollectBSwapParts(I->getOperand(1), OverallLeftShift, ByteMask, |
| 2982 | ByteValues); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 2983 | } |
Chris Lattner | 8c34cd2 | 2008-10-05 02:13:19 +0000 | [diff] [blame] | 2984 | |
| 2985 | // If this is a logical shift by a constant multiple of 8, recurse with |
| 2986 | // OverallLeftShift and ByteMask adjusted. |
| 2987 | if (I->isLogicalShift() && isa<ConstantInt>(I->getOperand(1))) { |
| 2988 | unsigned ShAmt = |
| 2989 | cast<ConstantInt>(I->getOperand(1))->getLimitedValue(~0U); |
| 2990 | // Ensure the shift amount is defined and of a byte value. |
| 2991 | if ((ShAmt & 7) || (ShAmt > 8*ByteValues.size())) |
| 2992 | return true; |
| 2993 | |
| 2994 | unsigned ByteShift = ShAmt >> 3; |
| 2995 | if (I->getOpcode() == Instruction::Shl) { |
| 2996 | // X << 2 -> collect(X, +2) |
| 2997 | OverallLeftShift += ByteShift; |
| 2998 | ByteMask >>= ByteShift; |
| 2999 | } else { |
| 3000 | // X >>u 2 -> collect(X, -2) |
| 3001 | OverallLeftShift -= ByteShift; |
| 3002 | ByteMask <<= ByteShift; |
Chris Lattner | de17ddc | 2008-10-08 06:42:28 +0000 | [diff] [blame] | 3003 | ByteMask &= (~0U >> (32-ByteValues.size())); |
Chris Lattner | 8c34cd2 | 2008-10-05 02:13:19 +0000 | [diff] [blame] | 3004 | } |
| 3005 | |
| 3006 | if (OverallLeftShift >= (int)ByteValues.size()) return true; |
| 3007 | if (OverallLeftShift <= -(int)ByteValues.size()) return true; |
| 3008 | |
| 3009 | return CollectBSwapParts(I->getOperand(0), OverallLeftShift, ByteMask, |
| 3010 | ByteValues); |
| 3011 | } |
| 3012 | |
| 3013 | // If this is a logical 'and' with a mask that clears bytes, clear the |
| 3014 | // corresponding bytes in ByteMask. |
| 3015 | if (I->getOpcode() == Instruction::And && |
| 3016 | isa<ConstantInt>(I->getOperand(1))) { |
| 3017 | // Scan every byte of the and mask, seeing if the byte is either 0 or 255. |
| 3018 | unsigned NumBytes = ByteValues.size(); |
| 3019 | APInt Byte(I->getType()->getPrimitiveSizeInBits(), 255); |
| 3020 | const APInt &AndMask = cast<ConstantInt>(I->getOperand(1))->getValue(); |
| 3021 | |
| 3022 | for (unsigned i = 0; i != NumBytes; ++i, Byte <<= 8) { |
| 3023 | // If this byte is masked out by a later operation, we don't care what |
| 3024 | // the and mask is. |
| 3025 | if ((ByteMask & (1 << i)) == 0) |
| 3026 | continue; |
| 3027 | |
| 3028 | // If the AndMask is all zeros for this byte, clear the bit. |
| 3029 | APInt MaskB = AndMask & Byte; |
| 3030 | if (MaskB == 0) { |
| 3031 | ByteMask &= ~(1U << i); |
| 3032 | continue; |
| 3033 | } |
| 3034 | |
| 3035 | // If the AndMask is not all ones for this byte, it's not a bytezap. |
| 3036 | if (MaskB != Byte) |
| 3037 | return true; |
| 3038 | |
| 3039 | // Otherwise, this byte is kept. |
| 3040 | } |
| 3041 | |
| 3042 | return CollectBSwapParts(I->getOperand(0), OverallLeftShift, ByteMask, |
| 3043 | ByteValues); |
| 3044 | } |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3045 | } |
| 3046 | |
Chris Lattner | 8c34cd2 | 2008-10-05 02:13:19 +0000 | [diff] [blame] | 3047 | // Okay, we got to something that isn't a shift, 'or' or 'and'. This must be |
| 3048 | // the input value to the bswap. Some observations: 1) if more than one byte |
| 3049 | // is demanded from this input, then it could not be successfully assembled |
| 3050 | // into a byteswap. At least one of the two bytes would not be aligned with |
| 3051 | // their ultimate destination. |
| 3052 | if (!isPowerOf2_32(ByteMask)) return true; |
| 3053 | unsigned InputByteNo = CountTrailingZeros_32(ByteMask); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3054 | |
Chris Lattner | 8c34cd2 | 2008-10-05 02:13:19 +0000 | [diff] [blame] | 3055 | // 2) The input and ultimate destinations must line up: if byte 3 of an i32 |
| 3056 | // is demanded, it needs to go into byte 0 of the result. This means that the |
| 3057 | // byte needs to be shifted until it lands in the right byte bucket. The |
| 3058 | // shift amount depends on the position: if the byte is coming from the high |
| 3059 | // part of the value (e.g. byte 3) then it must be shifted right. If from the |
| 3060 | // low part, it must be shifted left. |
| 3061 | unsigned DestByteNo = InputByteNo + OverallLeftShift; |
| 3062 | if (InputByteNo < ByteValues.size()/2) { |
| 3063 | if (ByteValues.size()-1-DestByteNo != InputByteNo) |
| 3064 | return true; |
| 3065 | } else { |
| 3066 | if (ByteValues.size()-1-DestByteNo != InputByteNo) |
| 3067 | return true; |
| 3068 | } |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3069 | |
| 3070 | // If the destination byte value is already defined, the values are or'd |
| 3071 | // together, which isn't a bswap (unless it's an or of the same bits). |
Chris Lattner | 8c34cd2 | 2008-10-05 02:13:19 +0000 | [diff] [blame] | 3072 | if (ByteValues[DestByteNo] && ByteValues[DestByteNo] != V) |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3073 | return true; |
Chris Lattner | 8c34cd2 | 2008-10-05 02:13:19 +0000 | [diff] [blame] | 3074 | ByteValues[DestByteNo] = V; |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3075 | return false; |
| 3076 | } |
| 3077 | |
| 3078 | /// MatchBSwap - Given an OR instruction, check to see if this is a bswap idiom. |
| 3079 | /// If so, insert the new bswap intrinsic and return it. |
| 3080 | Instruction *InstCombiner::MatchBSwap(BinaryOperator &I) { |
Chris Lattner | 55fc8c4 | 2007-04-01 20:57:36 +0000 | [diff] [blame] | 3081 | const IntegerType *ITy = dyn_cast<IntegerType>(I.getType()); |
Chris Lattner | 8c34cd2 | 2008-10-05 02:13:19 +0000 | [diff] [blame] | 3082 | if (!ITy || ITy->getBitWidth() % 16 || |
| 3083 | // ByteMask only allows up to 32-byte values. |
| 3084 | ITy->getBitWidth() > 32*8) |
Chris Lattner | 55fc8c4 | 2007-04-01 20:57:36 +0000 | [diff] [blame] | 3085 | return 0; // Can only bswap pairs of bytes. Can't do vectors. |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3086 | |
| 3087 | /// ByteValues - For each byte of the result, we keep track of which value |
| 3088 | /// defines each byte. |
Chris Lattner | 535014f | 2007-02-15 22:52:10 +0000 | [diff] [blame] | 3089 | SmallVector<Value*, 8> ByteValues; |
Chris Lattner | 55fc8c4 | 2007-04-01 20:57:36 +0000 | [diff] [blame] | 3090 | ByteValues.resize(ITy->getBitWidth()/8); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3091 | |
| 3092 | // Try to find all the pieces corresponding to the bswap. |
Chris Lattner | 8c34cd2 | 2008-10-05 02:13:19 +0000 | [diff] [blame] | 3093 | uint32_t ByteMask = ~0U >> (32-ByteValues.size()); |
| 3094 | if (CollectBSwapParts(&I, 0, ByteMask, ByteValues)) |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3095 | return 0; |
| 3096 | |
| 3097 | // Check to see if all of the bytes come from the same value. |
| 3098 | Value *V = ByteValues[0]; |
| 3099 | if (V == 0) return 0; // Didn't find a byte? Must be zero. |
| 3100 | |
| 3101 | // Check to make sure that all of the bytes come from the same value. |
| 3102 | for (unsigned i = 1, e = ByteValues.size(); i != e; ++i) |
| 3103 | if (ByteValues[i] != V) |
| 3104 | return 0; |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 3105 | const Type *Tys[] = { ITy }; |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3106 | Module *M = I.getParent()->getParent()->getParent(); |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 3107 | Function *F = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 3108 | return CallInst::Create(F, V); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3109 | } |
| 3110 | |
Chris Lattner | faaf951 | 2008-11-16 04:24:12 +0000 | [diff] [blame] | 3111 | /// MatchSelectFromAndOr - We have an expression of the form (A&C)|(B&D). Check |
| 3112 | /// If A is (cond?-1:0) and either B or D is ~(cond?-1,0) or (cond?0,-1), then |
| 3113 | /// we can simplify this expression to "cond ? C : D or B". |
| 3114 | static Instruction *MatchSelectFromAndOr(Value *A, Value *B, |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 3115 | Value *C, Value *D) { |
Chris Lattner | a6a474d | 2008-11-16 04:26:55 +0000 | [diff] [blame] | 3116 | // If A is not a select of -1/0, this cannot match. |
Chris Lattner | 6046fb7 | 2008-11-16 04:46:19 +0000 | [diff] [blame] | 3117 | Value *Cond = 0; |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 3118 | if (!match(A, m_SelectCst<-1, 0>(m_Value(Cond)))) |
Chris Lattner | faaf951 | 2008-11-16 04:24:12 +0000 | [diff] [blame] | 3119 | return 0; |
| 3120 | |
Chris Lattner | a6a474d | 2008-11-16 04:26:55 +0000 | [diff] [blame] | 3121 | // ((cond?-1:0)&C) | (B&(cond?0:-1)) -> cond ? C : B. |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 3122 | if (match(D, m_SelectCst<0, -1>(m_Specific(Cond)))) |
Chris Lattner | a6a474d | 2008-11-16 04:26:55 +0000 | [diff] [blame] | 3123 | return SelectInst::Create(Cond, C, B); |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 3124 | if (match(D, m_Not(m_SelectCst<-1, 0>(m_Specific(Cond))))) |
Chris Lattner | a6a474d | 2008-11-16 04:26:55 +0000 | [diff] [blame] | 3125 | return SelectInst::Create(Cond, C, B); |
| 3126 | // ((cond?-1:0)&C) | ((cond?0:-1)&D) -> cond ? C : D. |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 3127 | if (match(B, m_SelectCst<0, -1>(m_Specific(Cond)))) |
Chris Lattner | a6a474d | 2008-11-16 04:26:55 +0000 | [diff] [blame] | 3128 | return SelectInst::Create(Cond, C, D); |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 3129 | if (match(B, m_Not(m_SelectCst<-1, 0>(m_Specific(Cond))))) |
Chris Lattner | a6a474d | 2008-11-16 04:26:55 +0000 | [diff] [blame] | 3130 | return SelectInst::Create(Cond, C, D); |
Chris Lattner | faaf951 | 2008-11-16 04:24:12 +0000 | [diff] [blame] | 3131 | return 0; |
| 3132 | } |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3133 | |
Chris Lattner | 69d4ced | 2008-11-16 05:20:07 +0000 | [diff] [blame] | 3134 | /// FoldOrOfICmps - Fold (icmp)|(icmp) if possible. |
| 3135 | Instruction *InstCombiner::FoldOrOfICmps(Instruction &I, |
| 3136 | ICmpInst *LHS, ICmpInst *RHS) { |
| 3137 | Value *Val, *Val2; |
| 3138 | ConstantInt *LHSCst, *RHSCst; |
| 3139 | ICmpInst::Predicate LHSCC, RHSCC; |
| 3140 | |
| 3141 | // This only handles icmp of constants: (icmp1 A, C1) | (icmp2 B, C2). |
Chris Lattner | 3f40e23 | 2009-11-29 00:51:17 +0000 | [diff] [blame] | 3142 | if (!match(LHS, m_ICmp(LHSCC, m_Value(Val), m_ConstantInt(LHSCst))) || |
| 3143 | !match(RHS, m_ICmp(RHSCC, m_Value(Val2), m_ConstantInt(RHSCst)))) |
Chris Lattner | 69d4ced | 2008-11-16 05:20:07 +0000 | [diff] [blame] | 3144 | return 0; |
Chris Lattner | 3f40e23 | 2009-11-29 00:51:17 +0000 | [diff] [blame] | 3145 | |
| 3146 | |
| 3147 | // (icmp ne A, 0) | (icmp ne B, 0) --> (icmp ne (A|B), 0) |
| 3148 | if (LHSCst == RHSCst && LHSCC == RHSCC && |
| 3149 | LHSCC == ICmpInst::ICMP_NE && LHSCst->isZero()) { |
| 3150 | Value *NewOr = Builder->CreateOr(Val, Val2); |
| 3151 | return new ICmpInst(LHSCC, NewOr, LHSCst); |
| 3152 | } |
Chris Lattner | 69d4ced | 2008-11-16 05:20:07 +0000 | [diff] [blame] | 3153 | |
| 3154 | // From here on, we only handle: |
| 3155 | // (icmp1 A, C1) | (icmp2 A, C2) --> something simpler. |
| 3156 | if (Val != Val2) return 0; |
| 3157 | |
| 3158 | // ICMP_[US][GL]E X, CST is folded to ICMP_[US][GL]T elsewhere. |
| 3159 | if (LHSCC == ICmpInst::ICMP_UGE || LHSCC == ICmpInst::ICMP_ULE || |
| 3160 | RHSCC == ICmpInst::ICMP_UGE || RHSCC == ICmpInst::ICMP_ULE || |
| 3161 | LHSCC == ICmpInst::ICMP_SGE || LHSCC == ICmpInst::ICMP_SLE || |
| 3162 | RHSCC == ICmpInst::ICMP_SGE || RHSCC == ICmpInst::ICMP_SLE) |
| 3163 | return 0; |
| 3164 | |
| 3165 | // We can't fold (ugt x, C) | (sgt x, C2). |
| 3166 | if (!PredicatesFoldable(LHSCC, RHSCC)) |
| 3167 | return 0; |
| 3168 | |
| 3169 | // Ensure that the larger constant is on the RHS. |
| 3170 | bool ShouldSwap; |
Nick Lewycky | 4a134af | 2009-10-25 05:20:17 +0000 | [diff] [blame] | 3171 | if (CmpInst::isSigned(LHSCC) || |
Chris Lattner | 69d4ced | 2008-11-16 05:20:07 +0000 | [diff] [blame] | 3172 | (ICmpInst::isEquality(LHSCC) && |
Nick Lewycky | 4a134af | 2009-10-25 05:20:17 +0000 | [diff] [blame] | 3173 | CmpInst::isSigned(RHSCC))) |
Chris Lattner | 69d4ced | 2008-11-16 05:20:07 +0000 | [diff] [blame] | 3174 | ShouldSwap = LHSCst->getValue().sgt(RHSCst->getValue()); |
| 3175 | else |
| 3176 | ShouldSwap = LHSCst->getValue().ugt(RHSCst->getValue()); |
| 3177 | |
| 3178 | if (ShouldSwap) { |
| 3179 | std::swap(LHS, RHS); |
| 3180 | std::swap(LHSCst, RHSCst); |
| 3181 | std::swap(LHSCC, RHSCC); |
| 3182 | } |
| 3183 | |
| 3184 | // At this point, we know we have have two icmp instructions |
| 3185 | // comparing a value against two constants and or'ing the result |
| 3186 | // together. Because of the above check, we know that we only have |
| 3187 | // ICMP_EQ, ICMP_NE, ICMP_LT, and ICMP_GT here. We also know (from the |
| 3188 | // FoldICmpLogical check above), that the two constants are not |
| 3189 | // equal. |
| 3190 | assert(LHSCst != RHSCst && "Compares not folded above?"); |
| 3191 | |
| 3192 | switch (LHSCC) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 3193 | default: llvm_unreachable("Unknown integer condition code!"); |
Chris Lattner | 69d4ced | 2008-11-16 05:20:07 +0000 | [diff] [blame] | 3194 | case ICmpInst::ICMP_EQ: |
| 3195 | switch (RHSCC) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 3196 | default: llvm_unreachable("Unknown integer condition code!"); |
Chris Lattner | 69d4ced | 2008-11-16 05:20:07 +0000 | [diff] [blame] | 3197 | case ICmpInst::ICMP_EQ: |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 3198 | if (LHSCst == SubOne(RHSCst)) { |
Owen Anderson | d672ecb | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 3199 | // (X == 13 | X == 14) -> X-13 <u 2 |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 3200 | Constant *AddCST = ConstantExpr::getNeg(LHSCst); |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 3201 | Value *Add = Builder->CreateAdd(Val, AddCST, Val->getName()+".off"); |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 3202 | AddCST = ConstantExpr::getSub(AddOne(RHSCst), LHSCst); |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 3203 | return new ICmpInst(ICmpInst::ICMP_ULT, Add, AddCST); |
Chris Lattner | 69d4ced | 2008-11-16 05:20:07 +0000 | [diff] [blame] | 3204 | } |
| 3205 | break; // (X == 13 | X == 15) -> no change |
| 3206 | case ICmpInst::ICMP_UGT: // (X == 13 | X u> 14) -> no change |
| 3207 | case ICmpInst::ICMP_SGT: // (X == 13 | X s> 14) -> no change |
| 3208 | break; |
| 3209 | case ICmpInst::ICMP_NE: // (X == 13 | X != 15) -> X != 15 |
| 3210 | case ICmpInst::ICMP_ULT: // (X == 13 | X u< 15) -> X u< 15 |
| 3211 | case ICmpInst::ICMP_SLT: // (X == 13 | X s< 15) -> X s< 15 |
| 3212 | return ReplaceInstUsesWith(I, RHS); |
| 3213 | } |
| 3214 | break; |
| 3215 | case ICmpInst::ICMP_NE: |
| 3216 | switch (RHSCC) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 3217 | default: llvm_unreachable("Unknown integer condition code!"); |
Chris Lattner | 69d4ced | 2008-11-16 05:20:07 +0000 | [diff] [blame] | 3218 | case ICmpInst::ICMP_EQ: // (X != 13 | X == 15) -> X != 13 |
| 3219 | case ICmpInst::ICMP_UGT: // (X != 13 | X u> 15) -> X != 13 |
| 3220 | case ICmpInst::ICMP_SGT: // (X != 13 | X s> 15) -> X != 13 |
| 3221 | return ReplaceInstUsesWith(I, LHS); |
| 3222 | case ICmpInst::ICMP_NE: // (X != 13 | X != 15) -> true |
| 3223 | case ICmpInst::ICMP_ULT: // (X != 13 | X u< 15) -> true |
| 3224 | case ICmpInst::ICMP_SLT: // (X != 13 | X s< 15) -> true |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 3225 | return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext())); |
Chris Lattner | 69d4ced | 2008-11-16 05:20:07 +0000 | [diff] [blame] | 3226 | } |
| 3227 | break; |
| 3228 | case ICmpInst::ICMP_ULT: |
| 3229 | switch (RHSCC) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 3230 | default: llvm_unreachable("Unknown integer condition code!"); |
Chris Lattner | 69d4ced | 2008-11-16 05:20:07 +0000 | [diff] [blame] | 3231 | case ICmpInst::ICMP_EQ: // (X u< 13 | X == 14) -> no change |
| 3232 | break; |
| 3233 | case ICmpInst::ICMP_UGT: // (X u< 13 | X u> 15) -> (X-13) u> 2 |
| 3234 | // If RHSCst is [us]MAXINT, it is always false. Not handling |
| 3235 | // this can cause overflow. |
| 3236 | if (RHSCst->isMaxValue(false)) |
| 3237 | return ReplaceInstUsesWith(I, LHS); |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 3238 | return InsertRangeTest(Val, LHSCst, AddOne(RHSCst), |
Owen Anderson | d672ecb | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 3239 | false, false, I); |
Chris Lattner | 69d4ced | 2008-11-16 05:20:07 +0000 | [diff] [blame] | 3240 | case ICmpInst::ICMP_SGT: // (X u< 13 | X s> 15) -> no change |
| 3241 | break; |
| 3242 | case ICmpInst::ICMP_NE: // (X u< 13 | X != 15) -> X != 15 |
| 3243 | case ICmpInst::ICMP_ULT: // (X u< 13 | X u< 15) -> X u< 15 |
| 3244 | return ReplaceInstUsesWith(I, RHS); |
| 3245 | case ICmpInst::ICMP_SLT: // (X u< 13 | X s< 15) -> no change |
| 3246 | break; |
| 3247 | } |
| 3248 | break; |
| 3249 | case ICmpInst::ICMP_SLT: |
| 3250 | switch (RHSCC) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 3251 | default: llvm_unreachable("Unknown integer condition code!"); |
Chris Lattner | 69d4ced | 2008-11-16 05:20:07 +0000 | [diff] [blame] | 3252 | case ICmpInst::ICMP_EQ: // (X s< 13 | X == 14) -> no change |
| 3253 | break; |
| 3254 | case ICmpInst::ICMP_SGT: // (X s< 13 | X s> 15) -> (X-13) s> 2 |
| 3255 | // If RHSCst is [us]MAXINT, it is always false. Not handling |
| 3256 | // this can cause overflow. |
| 3257 | if (RHSCst->isMaxValue(true)) |
| 3258 | return ReplaceInstUsesWith(I, LHS); |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 3259 | return InsertRangeTest(Val, LHSCst, AddOne(RHSCst), |
Owen Anderson | d672ecb | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 3260 | true, false, I); |
Chris Lattner | 69d4ced | 2008-11-16 05:20:07 +0000 | [diff] [blame] | 3261 | case ICmpInst::ICMP_UGT: // (X s< 13 | X u> 15) -> no change |
| 3262 | break; |
| 3263 | case ICmpInst::ICMP_NE: // (X s< 13 | X != 15) -> X != 15 |
| 3264 | case ICmpInst::ICMP_SLT: // (X s< 13 | X s< 15) -> X s< 15 |
| 3265 | return ReplaceInstUsesWith(I, RHS); |
| 3266 | case ICmpInst::ICMP_ULT: // (X s< 13 | X u< 15) -> no change |
| 3267 | break; |
| 3268 | } |
| 3269 | break; |
| 3270 | case ICmpInst::ICMP_UGT: |
| 3271 | switch (RHSCC) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 3272 | default: llvm_unreachable("Unknown integer condition code!"); |
Chris Lattner | 69d4ced | 2008-11-16 05:20:07 +0000 | [diff] [blame] | 3273 | case ICmpInst::ICMP_EQ: // (X u> 13 | X == 15) -> X u> 13 |
| 3274 | case ICmpInst::ICMP_UGT: // (X u> 13 | X u> 15) -> X u> 13 |
| 3275 | return ReplaceInstUsesWith(I, LHS); |
| 3276 | case ICmpInst::ICMP_SGT: // (X u> 13 | X s> 15) -> no change |
| 3277 | break; |
| 3278 | case ICmpInst::ICMP_NE: // (X u> 13 | X != 15) -> true |
| 3279 | case ICmpInst::ICMP_ULT: // (X u> 13 | X u< 15) -> true |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 3280 | return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext())); |
Chris Lattner | 69d4ced | 2008-11-16 05:20:07 +0000 | [diff] [blame] | 3281 | case ICmpInst::ICMP_SLT: // (X u> 13 | X s< 15) -> no change |
| 3282 | break; |
| 3283 | } |
| 3284 | break; |
| 3285 | case ICmpInst::ICMP_SGT: |
| 3286 | switch (RHSCC) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 3287 | default: llvm_unreachable("Unknown integer condition code!"); |
Chris Lattner | 69d4ced | 2008-11-16 05:20:07 +0000 | [diff] [blame] | 3288 | case ICmpInst::ICMP_EQ: // (X s> 13 | X == 15) -> X > 13 |
| 3289 | case ICmpInst::ICMP_SGT: // (X s> 13 | X s> 15) -> X > 13 |
| 3290 | return ReplaceInstUsesWith(I, LHS); |
| 3291 | case ICmpInst::ICMP_UGT: // (X s> 13 | X u> 15) -> no change |
| 3292 | break; |
| 3293 | case ICmpInst::ICMP_NE: // (X s> 13 | X != 15) -> true |
| 3294 | case ICmpInst::ICMP_SLT: // (X s> 13 | X s< 15) -> true |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 3295 | return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext())); |
Chris Lattner | 69d4ced | 2008-11-16 05:20:07 +0000 | [diff] [blame] | 3296 | case ICmpInst::ICMP_ULT: // (X s> 13 | X u< 15) -> no change |
| 3297 | break; |
| 3298 | } |
| 3299 | break; |
| 3300 | } |
| 3301 | return 0; |
| 3302 | } |
| 3303 | |
Chris Lattner | 5414cc5 | 2009-07-23 05:46:22 +0000 | [diff] [blame] | 3304 | Instruction *InstCombiner::FoldOrOfFCmps(Instruction &I, FCmpInst *LHS, |
| 3305 | FCmpInst *RHS) { |
| 3306 | if (LHS->getPredicate() == FCmpInst::FCMP_UNO && |
| 3307 | RHS->getPredicate() == FCmpInst::FCMP_UNO && |
| 3308 | LHS->getOperand(0)->getType() == RHS->getOperand(0)->getType()) { |
| 3309 | if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1))) |
| 3310 | if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) { |
| 3311 | // If either of the constants are nans, then the whole thing returns |
| 3312 | // true. |
| 3313 | if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN()) |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 3314 | return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext())); |
Chris Lattner | 5414cc5 | 2009-07-23 05:46:22 +0000 | [diff] [blame] | 3315 | |
| 3316 | // Otherwise, no need to compare the two constants, compare the |
| 3317 | // rest. |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 3318 | return new FCmpInst(FCmpInst::FCMP_UNO, |
Chris Lattner | 5414cc5 | 2009-07-23 05:46:22 +0000 | [diff] [blame] | 3319 | LHS->getOperand(0), RHS->getOperand(0)); |
| 3320 | } |
| 3321 | |
| 3322 | // Handle vector zeros. This occurs because the canonical form of |
| 3323 | // "fcmp uno x,x" is "fcmp uno x, 0". |
| 3324 | if (isa<ConstantAggregateZero>(LHS->getOperand(1)) && |
| 3325 | isa<ConstantAggregateZero>(RHS->getOperand(1))) |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 3326 | return new FCmpInst(FCmpInst::FCMP_UNO, |
Chris Lattner | 5414cc5 | 2009-07-23 05:46:22 +0000 | [diff] [blame] | 3327 | LHS->getOperand(0), RHS->getOperand(0)); |
| 3328 | |
| 3329 | return 0; |
| 3330 | } |
| 3331 | |
| 3332 | Value *Op0LHS = LHS->getOperand(0), *Op0RHS = LHS->getOperand(1); |
| 3333 | Value *Op1LHS = RHS->getOperand(0), *Op1RHS = RHS->getOperand(1); |
| 3334 | FCmpInst::Predicate Op0CC = LHS->getPredicate(), Op1CC = RHS->getPredicate(); |
| 3335 | |
| 3336 | if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) { |
| 3337 | // Swap RHS operands to match LHS. |
| 3338 | Op1CC = FCmpInst::getSwappedPredicate(Op1CC); |
| 3339 | std::swap(Op1LHS, Op1RHS); |
| 3340 | } |
| 3341 | if (Op0LHS == Op1LHS && Op0RHS == Op1RHS) { |
| 3342 | // Simplify (fcmp cc0 x, y) | (fcmp cc1 x, y). |
| 3343 | if (Op0CC == Op1CC) |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 3344 | return new FCmpInst((FCmpInst::Predicate)Op0CC, |
Chris Lattner | 5414cc5 | 2009-07-23 05:46:22 +0000 | [diff] [blame] | 3345 | Op0LHS, Op0RHS); |
| 3346 | if (Op0CC == FCmpInst::FCMP_TRUE || Op1CC == FCmpInst::FCMP_TRUE) |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 3347 | return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext())); |
Chris Lattner | 5414cc5 | 2009-07-23 05:46:22 +0000 | [diff] [blame] | 3348 | if (Op0CC == FCmpInst::FCMP_FALSE) |
| 3349 | return ReplaceInstUsesWith(I, RHS); |
| 3350 | if (Op1CC == FCmpInst::FCMP_FALSE) |
| 3351 | return ReplaceInstUsesWith(I, LHS); |
| 3352 | bool Op0Ordered; |
| 3353 | bool Op1Ordered; |
| 3354 | unsigned Op0Pred = getFCmpCode(Op0CC, Op0Ordered); |
| 3355 | unsigned Op1Pred = getFCmpCode(Op1CC, Op1Ordered); |
| 3356 | if (Op0Ordered == Op1Ordered) { |
| 3357 | // If both are ordered or unordered, return a new fcmp with |
| 3358 | // or'ed predicates. |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 3359 | Value *RV = getFCmpValue(Op0Ordered, Op0Pred|Op1Pred, Op0LHS, Op0RHS); |
Chris Lattner | 5414cc5 | 2009-07-23 05:46:22 +0000 | [diff] [blame] | 3360 | if (Instruction *I = dyn_cast<Instruction>(RV)) |
| 3361 | return I; |
| 3362 | // Otherwise, it's a constant boolean value... |
| 3363 | return ReplaceInstUsesWith(I, RV); |
| 3364 | } |
| 3365 | } |
| 3366 | return 0; |
| 3367 | } |
| 3368 | |
Bill Wendling | a698a47 | 2008-12-01 08:23:25 +0000 | [diff] [blame] | 3369 | /// FoldOrWithConstants - This helper function folds: |
| 3370 | /// |
Bill Wendling | a8bb13f | 2008-12-02 05:09:00 +0000 | [diff] [blame] | 3371 | /// ((A | B) & C1) | (B & C2) |
Bill Wendling | a698a47 | 2008-12-01 08:23:25 +0000 | [diff] [blame] | 3372 | /// |
| 3373 | /// into: |
| 3374 | /// |
Bill Wendling | a8bb13f | 2008-12-02 05:09:00 +0000 | [diff] [blame] | 3375 | /// (A & C1) | B |
Bill Wendling | d54d860 | 2008-12-01 08:32:40 +0000 | [diff] [blame] | 3376 | /// |
Bill Wendling | a8bb13f | 2008-12-02 05:09:00 +0000 | [diff] [blame] | 3377 | /// when the XOR of the two constants is "all ones" (-1). |
Bill Wendling | d54d860 | 2008-12-01 08:32:40 +0000 | [diff] [blame] | 3378 | Instruction *InstCombiner::FoldOrWithConstants(BinaryOperator &I, Value *Op, |
Bill Wendling | a698a47 | 2008-12-01 08:23:25 +0000 | [diff] [blame] | 3379 | Value *A, Value *B, Value *C) { |
Bill Wendling | dda74e0 | 2008-12-02 05:06:43 +0000 | [diff] [blame] | 3380 | ConstantInt *CI1 = dyn_cast<ConstantInt>(C); |
| 3381 | if (!CI1) return 0; |
Bill Wendling | a698a47 | 2008-12-01 08:23:25 +0000 | [diff] [blame] | 3382 | |
Bill Wendling | 286a054 | 2008-12-02 06:24:20 +0000 | [diff] [blame] | 3383 | Value *V1 = 0; |
| 3384 | ConstantInt *CI2 = 0; |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 3385 | if (!match(Op, m_And(m_Value(V1), m_ConstantInt(CI2)))) return 0; |
Bill Wendling | a698a47 | 2008-12-01 08:23:25 +0000 | [diff] [blame] | 3386 | |
Bill Wendling | 29976b9 | 2008-12-02 06:18:11 +0000 | [diff] [blame] | 3387 | APInt Xor = CI1->getValue() ^ CI2->getValue(); |
| 3388 | if (!Xor.isAllOnesValue()) return 0; |
| 3389 | |
Bill Wendling | 286a054 | 2008-12-02 06:24:20 +0000 | [diff] [blame] | 3390 | if (V1 == A || V1 == B) { |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 3391 | Value *NewOp = Builder->CreateAnd((V1 == A) ? B : A, CI1); |
Bill Wendling | d16c6e9 | 2008-12-02 06:22:04 +0000 | [diff] [blame] | 3392 | return BinaryOperator::CreateOr(NewOp, V1); |
Bill Wendling | a698a47 | 2008-12-01 08:23:25 +0000 | [diff] [blame] | 3393 | } |
| 3394 | |
| 3395 | return 0; |
| 3396 | } |
| 3397 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3398 | Instruction *InstCombiner::visitOr(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 3399 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3400 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3401 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 3402 | if (Value *V = SimplifyOrInst(Op0, Op1, TD)) |
| 3403 | return ReplaceInstUsesWith(I, V); |
| 3404 | |
| 3405 | |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3406 | // See if we can simplify any instructions used by the instruction whose sole |
| 3407 | // purpose is to compute bits we don't care about. |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 3408 | if (SimplifyDemandedInstructionBits(I)) |
| 3409 | return &I; |
Chris Lattner | 041a6c9 | 2007-06-15 05:26:55 +0000 | [diff] [blame] | 3410 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3411 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 3412 | ConstantInt *C1 = 0; Value *X = 0; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3413 | // (X & C1) | C2 --> (X | C2) & (C1|C2) |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 3414 | if (match(Op0, m_And(m_Value(X), m_ConstantInt(C1))) && |
Owen Anderson | c7d2ce7 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 3415 | isOnlyUse(Op0)) { |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 3416 | Value *Or = Builder->CreateOr(X, RHS); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3417 | Or->takeName(Op0); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3418 | return BinaryOperator::CreateAnd(Or, |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 3419 | ConstantInt::get(I.getContext(), |
| 3420 | RHS->getValue() | C1->getValue())); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3421 | } |
Chris Lattner | ad44ebf | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 3422 | |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3423 | // (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2) |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 3424 | if (match(Op0, m_Xor(m_Value(X), m_ConstantInt(C1))) && |
Owen Anderson | c7d2ce7 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 3425 | isOnlyUse(Op0)) { |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 3426 | Value *Or = Builder->CreateOr(X, RHS); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3427 | Or->takeName(Op0); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3428 | return BinaryOperator::CreateXor(Or, |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 3429 | ConstantInt::get(I.getContext(), |
| 3430 | C1->getValue() & ~RHS->getValue())); |
Chris Lattner | ad44ebf | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 3431 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3432 | |
| 3433 | // Try to fold constant and into select arguments. |
| 3434 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame^] | 3435 | if (Instruction *R = FoldOpIntoSelect(I, SI)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3436 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 3437 | if (isa<PHINode>(Op0)) |
| 3438 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 3439 | return NV; |
Chris Lattner | ad44ebf | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 3440 | } |
| 3441 | |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 3442 | Value *A = 0, *B = 0; |
| 3443 | ConstantInt *C1 = 0, *C2 = 0; |
Chris Lattner | f4d4c87 | 2005-05-07 23:49:08 +0000 | [diff] [blame] | 3444 | |
Chris Lattner | 6423d4c | 2006-07-10 20:25:24 +0000 | [diff] [blame] | 3445 | // (A | B) | C and A | (B | C) -> bswap if possible. |
| 3446 | // (A >> B) | (C << D) and (A << B) | (B >> C) -> bswap if possible. |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 3447 | if (match(Op0, m_Or(m_Value(), m_Value())) || |
| 3448 | match(Op1, m_Or(m_Value(), m_Value())) || |
| 3449 | (match(Op0, m_Shift(m_Value(), m_Value())) && |
| 3450 | match(Op1, m_Shift(m_Value(), m_Value())))) { |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3451 | if (Instruction *BSwap = MatchBSwap(I)) |
| 3452 | return BSwap; |
| 3453 | } |
| 3454 | |
Chris Lattner | 6e4c649 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 3455 | // (X^C)|Y -> (X|Y)^C iff Y&C == 0 |
Owen Anderson | c7d2ce7 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 3456 | if (Op0->hasOneUse() && |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 3457 | match(Op0, m_Xor(m_Value(A), m_ConstantInt(C1))) && |
Reid Spencer | a03d45f | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 3458 | MaskedValueIsZero(Op1, C1->getValue())) { |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 3459 | Value *NOr = Builder->CreateOr(A, Op1); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3460 | NOr->takeName(Op0); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3461 | return BinaryOperator::CreateXor(NOr, C1); |
Chris Lattner | 6e4c649 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 3462 | } |
| 3463 | |
| 3464 | // Y|(X^C) -> (X|Y)^C iff Y&C == 0 |
Owen Anderson | c7d2ce7 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 3465 | if (Op1->hasOneUse() && |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 3466 | match(Op1, m_Xor(m_Value(A), m_ConstantInt(C1))) && |
Reid Spencer | a03d45f | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 3467 | MaskedValueIsZero(Op0, C1->getValue())) { |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 3468 | Value *NOr = Builder->CreateOr(A, Op0); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3469 | NOr->takeName(Op0); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3470 | return BinaryOperator::CreateXor(NOr, C1); |
Chris Lattner | 6e4c649 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 3471 | } |
| 3472 | |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 3473 | // (A & C)|(B & D) |
Chris Lattner | 2384d7b | 2007-06-19 05:43:49 +0000 | [diff] [blame] | 3474 | Value *C = 0, *D = 0; |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 3475 | if (match(Op0, m_And(m_Value(A), m_Value(C))) && |
| 3476 | match(Op1, m_And(m_Value(B), m_Value(D)))) { |
Chris Lattner | 6cae0e0 | 2007-04-08 07:55:22 +0000 | [diff] [blame] | 3477 | Value *V1 = 0, *V2 = 0, *V3 = 0; |
| 3478 | C1 = dyn_cast<ConstantInt>(C); |
| 3479 | C2 = dyn_cast<ConstantInt>(D); |
| 3480 | if (C1 && C2) { // (A & C1)|(B & C2) |
| 3481 | // If we have: ((V + N) & C1) | (V & C2) |
| 3482 | // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0 |
| 3483 | // replace with V+N. |
| 3484 | if (C1->getValue() == ~C2->getValue()) { |
| 3485 | if ((C2->getValue() & (C2->getValue()+1)) == 0 && // C2 == 0+1+ |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 3486 | match(A, m_Add(m_Value(V1), m_Value(V2)))) { |
Chris Lattner | 6cae0e0 | 2007-04-08 07:55:22 +0000 | [diff] [blame] | 3487 | // Add commutes, try both ways. |
| 3488 | if (V1 == B && MaskedValueIsZero(V2, C2->getValue())) |
| 3489 | return ReplaceInstUsesWith(I, A); |
| 3490 | if (V2 == B && MaskedValueIsZero(V1, C2->getValue())) |
| 3491 | return ReplaceInstUsesWith(I, A); |
| 3492 | } |
| 3493 | // Or commutes, try both ways. |
| 3494 | if ((C1->getValue() & (C1->getValue()+1)) == 0 && |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 3495 | match(B, m_Add(m_Value(V1), m_Value(V2)))) { |
Chris Lattner | 6cae0e0 | 2007-04-08 07:55:22 +0000 | [diff] [blame] | 3496 | // Add commutes, try both ways. |
| 3497 | if (V1 == A && MaskedValueIsZero(V2, C1->getValue())) |
| 3498 | return ReplaceInstUsesWith(I, B); |
| 3499 | if (V2 == A && MaskedValueIsZero(V1, C1->getValue())) |
| 3500 | return ReplaceInstUsesWith(I, B); |
| 3501 | } |
| 3502 | } |
Chris Lattner | e4412c1 | 2010-01-04 06:03:59 +0000 | [diff] [blame] | 3503 | |
| 3504 | // ((V | N) & C1) | (V & C2) --> (V|N) & (C1|C2) |
| 3505 | // iff (C1&C2) == 0 and (N&~C1) == 0 |
| 3506 | if ((C1->getValue() & C2->getValue()) == 0) { |
| 3507 | if (match(A, m_Or(m_Value(V1), m_Value(V2))) && |
| 3508 | ((V1 == B && MaskedValueIsZero(V2, ~C1->getValue())) || // (V|N) |
| 3509 | (V2 == B && MaskedValueIsZero(V1, ~C1->getValue())))) // (N|V) |
| 3510 | return BinaryOperator::CreateAnd(A, |
| 3511 | ConstantInt::get(A->getContext(), |
| 3512 | C1->getValue()|C2->getValue())); |
| 3513 | // Or commutes, try both ways. |
| 3514 | if (match(B, m_Or(m_Value(V1), m_Value(V2))) && |
| 3515 | ((V1 == A && MaskedValueIsZero(V2, ~C2->getValue())) || // (V|N) |
| 3516 | (V2 == A && MaskedValueIsZero(V1, ~C2->getValue())))) // (N|V) |
| 3517 | return BinaryOperator::CreateAnd(B, |
| 3518 | ConstantInt::get(B->getContext(), |
| 3519 | C1->getValue()|C2->getValue())); |
| 3520 | } |
Chris Lattner | 6cae0e0 | 2007-04-08 07:55:22 +0000 | [diff] [blame] | 3521 | } |
| 3522 | |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 3523 | // Check to see if we have any common things being and'ed. If so, find the |
| 3524 | // terms for V1 & (V2|V3). |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 3525 | if (isOnlyUse(Op0) || isOnlyUse(Op1)) { |
Chris Lattner | e4412c1 | 2010-01-04 06:03:59 +0000 | [diff] [blame] | 3526 | V1 = 0; |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 3527 | if (A == B) // (A & C)|(A & D) == A & (C|D) |
| 3528 | V1 = A, V2 = C, V3 = D; |
| 3529 | else if (A == D) // (A & C)|(B & A) == A & (B|C) |
| 3530 | V1 = A, V2 = B, V3 = C; |
| 3531 | else if (C == B) // (A & C)|(C & D) == C & (A|D) |
| 3532 | V1 = C, V2 = A, V3 = D; |
| 3533 | else if (C == D) // (A & C)|(B & C) == C & (A|B) |
| 3534 | V1 = C, V2 = A, V3 = B; |
| 3535 | |
| 3536 | if (V1) { |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 3537 | Value *Or = Builder->CreateOr(V2, V3, "tmp"); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3538 | return BinaryOperator::CreateAnd(V1, Or); |
Chris Lattner | 0b7c0bf | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 3539 | } |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 3540 | } |
Dan Gohman | b493b27 | 2008-10-28 22:38:57 +0000 | [diff] [blame] | 3541 | |
Dan Gohman | 1975d03 | 2008-10-30 20:40:10 +0000 | [diff] [blame] | 3542 | // (A & (C0?-1:0)) | (B & ~(C0?-1:0)) -> C0 ? A : B, and commuted variants |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 3543 | if (Instruction *Match = MatchSelectFromAndOr(A, B, C, D)) |
Chris Lattner | faaf951 | 2008-11-16 04:24:12 +0000 | [diff] [blame] | 3544 | return Match; |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 3545 | if (Instruction *Match = MatchSelectFromAndOr(B, A, D, C)) |
Chris Lattner | faaf951 | 2008-11-16 04:24:12 +0000 | [diff] [blame] | 3546 | return Match; |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 3547 | if (Instruction *Match = MatchSelectFromAndOr(C, B, A, D)) |
Chris Lattner | faaf951 | 2008-11-16 04:24:12 +0000 | [diff] [blame] | 3548 | return Match; |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 3549 | if (Instruction *Match = MatchSelectFromAndOr(D, A, B, C)) |
Chris Lattner | faaf951 | 2008-11-16 04:24:12 +0000 | [diff] [blame] | 3550 | return Match; |
Bill Wendling | b01865c | 2008-11-30 13:52:49 +0000 | [diff] [blame] | 3551 | |
Bill Wendling | b01865c | 2008-11-30 13:52:49 +0000 | [diff] [blame] | 3552 | // ((A&~B)|(~A&B)) -> A^B |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 3553 | if ((match(C, m_Not(m_Specific(D))) && |
| 3554 | match(B, m_Not(m_Specific(A))))) |
Bill Wendling | 03aae5f | 2008-12-01 08:09:47 +0000 | [diff] [blame] | 3555 | return BinaryOperator::CreateXor(A, D); |
Bill Wendling | b01865c | 2008-11-30 13:52:49 +0000 | [diff] [blame] | 3556 | // ((~B&A)|(~A&B)) -> A^B |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 3557 | if ((match(A, m_Not(m_Specific(D))) && |
| 3558 | match(B, m_Not(m_Specific(C))))) |
Bill Wendling | 03aae5f | 2008-12-01 08:09:47 +0000 | [diff] [blame] | 3559 | return BinaryOperator::CreateXor(C, D); |
Bill Wendling | b01865c | 2008-11-30 13:52:49 +0000 | [diff] [blame] | 3560 | // ((A&~B)|(B&~A)) -> A^B |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 3561 | if ((match(C, m_Not(m_Specific(B))) && |
| 3562 | match(D, m_Not(m_Specific(A))))) |
Bill Wendling | 03aae5f | 2008-12-01 08:09:47 +0000 | [diff] [blame] | 3563 | return BinaryOperator::CreateXor(A, B); |
Bill Wendling | b01865c | 2008-11-30 13:52:49 +0000 | [diff] [blame] | 3564 | // ((~B&A)|(B&~A)) -> A^B |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 3565 | if ((match(A, m_Not(m_Specific(B))) && |
| 3566 | match(D, m_Not(m_Specific(C))))) |
Bill Wendling | 03aae5f | 2008-12-01 08:09:47 +0000 | [diff] [blame] | 3567 | return BinaryOperator::CreateXor(C, B); |
Chris Lattner | e9bed7d | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 3568 | } |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 3569 | |
| 3570 | // (X >> Z) | (Y >> Z) -> (X|Y) >> Z for all shifts. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3571 | if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) { |
| 3572 | if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0)) |
| 3573 | if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() && |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 3574 | SI0->getOperand(1) == SI1->getOperand(1) && |
| 3575 | (SI0->hasOneUse() || SI1->hasOneUse())) { |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 3576 | Value *NewOp = Builder->CreateOr(SI0->getOperand(0), SI1->getOperand(0), |
| 3577 | SI0->getName()); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3578 | return BinaryOperator::Create(SI1->getOpcode(), NewOp, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3579 | SI1->getOperand(1)); |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 3580 | } |
| 3581 | } |
Chris Lattner | 67ca768 | 2003-08-12 19:11:07 +0000 | [diff] [blame] | 3582 | |
Bill Wendling | b3833d1 | 2008-12-01 01:07:11 +0000 | [diff] [blame] | 3583 | // ((A|B)&1)|(B&-2) -> (A&1) | B |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 3584 | if (match(Op0, m_And(m_Or(m_Value(A), m_Value(B)), m_Value(C))) || |
| 3585 | match(Op0, m_And(m_Value(C), m_Or(m_Value(A), m_Value(B))))) { |
Bill Wendling | d54d860 | 2008-12-01 08:32:40 +0000 | [diff] [blame] | 3586 | Instruction *Ret = FoldOrWithConstants(I, Op1, A, B, C); |
Bill Wendling | a698a47 | 2008-12-01 08:23:25 +0000 | [diff] [blame] | 3587 | if (Ret) return Ret; |
Bill Wendling | b3833d1 | 2008-12-01 01:07:11 +0000 | [diff] [blame] | 3588 | } |
| 3589 | // (B&-2)|((A|B)&1) -> (A&1) | B |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 3590 | if (match(Op1, m_And(m_Or(m_Value(A), m_Value(B)), m_Value(C))) || |
| 3591 | match(Op1, m_And(m_Value(C), m_Or(m_Value(A), m_Value(B))))) { |
Bill Wendling | d54d860 | 2008-12-01 08:32:40 +0000 | [diff] [blame] | 3592 | Instruction *Ret = FoldOrWithConstants(I, Op0, A, B, C); |
Bill Wendling | a698a47 | 2008-12-01 08:23:25 +0000 | [diff] [blame] | 3593 | if (Ret) return Ret; |
Bill Wendling | b3833d1 | 2008-12-01 01:07:11 +0000 | [diff] [blame] | 3594 | } |
| 3595 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 3596 | // (~A | ~B) == (~(A & B)) - De Morgan's Law |
| 3597 | if (Value *Op0NotVal = dyn_castNotVal(Op0)) |
| 3598 | if (Value *Op1NotVal = dyn_castNotVal(Op1)) |
| 3599 | if (Op0->hasOneUse() && Op1->hasOneUse()) { |
| 3600 | Value *And = Builder->CreateAnd(Op0NotVal, Op1NotVal, |
| 3601 | I.getName()+".demorgan"); |
| 3602 | return BinaryOperator::CreateNot(And); |
| 3603 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3604 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3605 | // (icmp1 A, B) | (icmp2 A, B) --> (icmp3 A, B) |
| 3606 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1))) { |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 3607 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS))) |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3608 | return R; |
| 3609 | |
Chris Lattner | 69d4ced | 2008-11-16 05:20:07 +0000 | [diff] [blame] | 3610 | if (ICmpInst *LHS = dyn_cast<ICmpInst>(I.getOperand(0))) |
| 3611 | if (Instruction *Res = FoldOrOfICmps(I, LHS, RHS)) |
| 3612 | return Res; |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3613 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3614 | |
| 3615 | // fold (or (cast A), (cast B)) -> (cast (or A, B)) |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 3616 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) { |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3617 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3618 | if (Op0C->getOpcode() == Op1C->getOpcode()) {// same cast kind ? |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 3619 | if (!isa<ICmpInst>(Op0C->getOperand(0)) || |
| 3620 | !isa<ICmpInst>(Op1C->getOperand(0))) { |
| 3621 | const Type *SrcTy = Op0C->getOperand(0)->getType(); |
Chris Lattner | f98d253 | 2009-07-23 05:32:17 +0000 | [diff] [blame] | 3622 | if (SrcTy == Op1C->getOperand(0)->getType() && |
| 3623 | SrcTy->isIntOrIntVector() && |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 3624 | // Only do this if the casts both really cause code to be |
| 3625 | // generated. |
| 3626 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame^] | 3627 | I.getType()) && |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 3628 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame^] | 3629 | I.getType())) { |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 3630 | Value *NewOp = Builder->CreateOr(Op0C->getOperand(0), |
| 3631 | Op1C->getOperand(0), I.getName()); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3632 | return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType()); |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 3633 | } |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3634 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3635 | } |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 3636 | } |
| 3637 | |
| 3638 | |
| 3639 | // (fcmp uno x, c) | (fcmp uno y, c) -> (fcmp uno x, y) |
| 3640 | if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0))) { |
Chris Lattner | 5414cc5 | 2009-07-23 05:46:22 +0000 | [diff] [blame] | 3641 | if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1))) |
| 3642 | if (Instruction *Res = FoldOrOfFCmps(I, LHS, RHS)) |
| 3643 | return Res; |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 3644 | } |
Chris Lattner | e9bed7d | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 3645 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3646 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3647 | } |
| 3648 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 3649 | namespace { |
| 3650 | |
Chris Lattner | c317d39 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 3651 | // XorSelf - Implements: X ^ X --> 0 |
| 3652 | struct XorSelf { |
| 3653 | Value *RHS; |
| 3654 | XorSelf(Value *rhs) : RHS(rhs) {} |
| 3655 | bool shouldApply(Value *LHS) const { return LHS == RHS; } |
| 3656 | Instruction *apply(BinaryOperator &Xor) const { |
| 3657 | return &Xor; |
| 3658 | } |
| 3659 | }; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3660 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 3661 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3662 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3663 | Instruction *InstCombiner::visitXor(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 3664 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3665 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3666 | |
Evan Cheng | d34af78 | 2008-03-25 20:07:13 +0000 | [diff] [blame] | 3667 | if (isa<UndefValue>(Op1)) { |
| 3668 | if (isa<UndefValue>(Op0)) |
| 3669 | // Handle undef ^ undef -> 0 special case. This is a common |
| 3670 | // idiom (misuse). |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 3671 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3672 | return ReplaceInstUsesWith(I, Op1); // X ^ undef -> undef |
Evan Cheng | d34af78 | 2008-03-25 20:07:13 +0000 | [diff] [blame] | 3673 | } |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3674 | |
Chris Lattner | c317d39 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 3675 | // xor X, X = 0, even if X is nested in a sequence of Xor's. |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 3676 | if (Instruction *Result = AssociativeOpt(I, XorSelf(Op1))) { |
Chris Lattner | a9ff5eb | 2007-08-05 08:47:58 +0000 | [diff] [blame] | 3677 | assert(Result == &I && "AssociativeOpt didn't work?"); Result=Result; |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 3678 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | c317d39 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 3679 | } |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3680 | |
| 3681 | // See if we can simplify any instructions used by the instruction whose sole |
| 3682 | // purpose is to compute bits we don't care about. |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 3683 | if (SimplifyDemandedInstructionBits(I)) |
| 3684 | return &I; |
| 3685 | if (isa<VectorType>(I.getType())) |
| 3686 | if (isa<ConstantAggregateZero>(Op1)) |
| 3687 | return ReplaceInstUsesWith(I, Op0); // X ^ <0,0> -> X |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3688 | |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 3689 | // Is this a ~ operation? |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 3690 | if (Value *NotOp = dyn_castNotVal(&I)) { |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 3691 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(NotOp)) { |
| 3692 | if (Op0I->getOpcode() == Instruction::And || |
| 3693 | Op0I->getOpcode() == Instruction::Or) { |
Chris Lattner | 48b59ec | 2009-10-26 15:40:07 +0000 | [diff] [blame] | 3694 | // ~(~X & Y) --> (X | ~Y) - De Morgan's Law |
| 3695 | // ~(~X | Y) === (X & ~Y) - De Morgan's Law |
| 3696 | if (dyn_castNotVal(Op0I->getOperand(1))) |
| 3697 | Op0I->swapOperands(); |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 3698 | if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0))) { |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 3699 | Value *NotY = |
| 3700 | Builder->CreateNot(Op0I->getOperand(1), |
| 3701 | Op0I->getOperand(1)->getName()+".not"); |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 3702 | if (Op0I->getOpcode() == Instruction::And) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3703 | return BinaryOperator::CreateOr(Op0NotVal, NotY); |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 3704 | return BinaryOperator::CreateAnd(Op0NotVal, NotY); |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 3705 | } |
Chris Lattner | 48b59ec | 2009-10-26 15:40:07 +0000 | [diff] [blame] | 3706 | |
| 3707 | // ~(X & Y) --> (~X | ~Y) - De Morgan's Law |
| 3708 | // ~(X | Y) === (~X & ~Y) - De Morgan's Law |
| 3709 | if (isFreeToInvert(Op0I->getOperand(0)) && |
| 3710 | isFreeToInvert(Op0I->getOperand(1))) { |
| 3711 | Value *NotX = |
| 3712 | Builder->CreateNot(Op0I->getOperand(0), "notlhs"); |
| 3713 | Value *NotY = |
| 3714 | Builder->CreateNot(Op0I->getOperand(1), "notrhs"); |
| 3715 | if (Op0I->getOpcode() == Instruction::And) |
| 3716 | return BinaryOperator::CreateOr(NotX, NotY); |
| 3717 | return BinaryOperator::CreateAnd(NotX, NotY); |
| 3718 | } |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 3719 | } |
| 3720 | } |
| 3721 | } |
| 3722 | |
| 3723 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3724 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 7acdf1d | 2009-10-11 22:00:32 +0000 | [diff] [blame] | 3725 | if (RHS->isOne() && Op0->hasOneUse()) { |
Bill Wendling | 3479be9 | 2009-01-01 01:18:23 +0000 | [diff] [blame] | 3726 | // xor (cmp A, B), true = not (cmp A, B) = !cmp A, B |
Nick Lewycky | f947b3e | 2007-08-06 20:04:16 +0000 | [diff] [blame] | 3727 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(Op0)) |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 3728 | return new ICmpInst(ICI->getInversePredicate(), |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3729 | ICI->getOperand(0), ICI->getOperand(1)); |
Chris Lattner | ad5b4fb | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 3730 | |
Nick Lewycky | f947b3e | 2007-08-06 20:04:16 +0000 | [diff] [blame] | 3731 | if (FCmpInst *FCI = dyn_cast<FCmpInst>(Op0)) |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 3732 | return new FCmpInst(FCI->getInversePredicate(), |
Nick Lewycky | f947b3e | 2007-08-06 20:04:16 +0000 | [diff] [blame] | 3733 | FCI->getOperand(0), FCI->getOperand(1)); |
| 3734 | } |
| 3735 | |
Nick Lewycky | 517e1f5 | 2008-05-31 19:01:33 +0000 | [diff] [blame] | 3736 | // fold (xor(zext(cmp)), 1) and (xor(sext(cmp)), -1) to ext(!cmp). |
| 3737 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) { |
| 3738 | if (CmpInst *CI = dyn_cast<CmpInst>(Op0C->getOperand(0))) { |
| 3739 | if (CI->hasOneUse() && Op0C->hasOneUse()) { |
| 3740 | Instruction::CastOps Opcode = Op0C->getOpcode(); |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 3741 | if ((Opcode == Instruction::ZExt || Opcode == Instruction::SExt) && |
| 3742 | (RHS == ConstantExpr::getCast(Opcode, |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 3743 | ConstantInt::getTrue(I.getContext()), |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 3744 | Op0C->getDestTy()))) { |
| 3745 | CI->setPredicate(CI->getInversePredicate()); |
| 3746 | return CastInst::Create(Opcode, CI, Op0C->getType()); |
Nick Lewycky | 517e1f5 | 2008-05-31 19:01:33 +0000 | [diff] [blame] | 3747 | } |
| 3748 | } |
| 3749 | } |
| 3750 | } |
| 3751 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3752 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) { |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 3753 | // ~(c-X) == X-c-1 == X+(-c-1) |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3754 | if (Op0I->getOpcode() == Instruction::Sub && RHS->isAllOnesValue()) |
| 3755 | if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) { |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 3756 | Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C); |
| 3757 | Constant *ConstantRHS = ConstantExpr::getSub(NegOp0I0C, |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 3758 | ConstantInt::get(I.getType(), 1)); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3759 | return BinaryOperator::CreateAdd(Op0I->getOperand(1), ConstantRHS); |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3760 | } |
Chris Lattner | 5c6e2db | 2007-04-02 05:36:22 +0000 | [diff] [blame] | 3761 | |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 3762 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) { |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3763 | if (Op0I->getOpcode() == Instruction::Add) { |
Chris Lattner | 689d24b | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 3764 | // ~(X-c) --> (-c-1)-X |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3765 | if (RHS->isAllOnesValue()) { |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 3766 | Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3767 | return BinaryOperator::CreateSub( |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 3768 | ConstantExpr::getSub(NegOp0CI, |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 3769 | ConstantInt::get(I.getType(), 1)), |
Owen Anderson | d672ecb | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 3770 | Op0I->getOperand(0)); |
Chris Lattner | acf4e07 | 2007-04-02 05:42:22 +0000 | [diff] [blame] | 3771 | } else if (RHS->getValue().isSignBit()) { |
Chris Lattner | 5c6e2db | 2007-04-02 05:36:22 +0000 | [diff] [blame] | 3772 | // (X + C) ^ signbit -> (X + C + signbit) |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 3773 | Constant *C = ConstantInt::get(I.getContext(), |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 3774 | RHS->getValue() + Op0CI->getValue()); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3775 | return BinaryOperator::CreateAdd(Op0I->getOperand(0), C); |
Chris Lattner | cd1d6d5 | 2007-04-02 05:48:58 +0000 | [diff] [blame] | 3776 | |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3777 | } |
Chris Lattner | 02bd1b3 | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 3778 | } else if (Op0I->getOpcode() == Instruction::Or) { |
| 3779 | // (X|C1)^C2 -> X^(C1|C2) iff X&~C1 == 0 |
Reid Spencer | a03d45f | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 3780 | if (MaskedValueIsZero(Op0I->getOperand(0), Op0CI->getValue())) { |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 3781 | Constant *NewRHS = ConstantExpr::getOr(Op0CI, RHS); |
Chris Lattner | 02bd1b3 | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 3782 | // Anything in both C1 and C2 is known to be zero, remove it from |
| 3783 | // NewRHS. |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 3784 | Constant *CommonBits = ConstantExpr::getAnd(Op0CI, RHS); |
| 3785 | NewRHS = ConstantExpr::getAnd(NewRHS, |
| 3786 | ConstantExpr::getNot(CommonBits)); |
Chris Lattner | 7a1e924 | 2009-08-30 06:13:40 +0000 | [diff] [blame] | 3787 | Worklist.Add(Op0I); |
Chris Lattner | 02bd1b3 | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 3788 | I.setOperand(0, Op0I->getOperand(0)); |
| 3789 | I.setOperand(1, NewRHS); |
| 3790 | return &I; |
| 3791 | } |
Chris Lattner | eca0c5c | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 3792 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 3793 | } |
Chris Lattner | 05bd1b2 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 3794 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3795 | |
| 3796 | // Try to fold constant and into select arguments. |
| 3797 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame^] | 3798 | if (Instruction *R = FoldOpIntoSelect(I, SI)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3799 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 3800 | if (isa<PHINode>(Op0)) |
| 3801 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 3802 | return NV; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3803 | } |
| 3804 | |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 3805 | if (Value *X = dyn_castNotVal(Op0)) // ~A ^ A == -1 |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3806 | if (X == Op1) |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 3807 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3808 | |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 3809 | if (Value *X = dyn_castNotVal(Op1)) // A ^ ~A == -1 |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3810 | if (X == Op0) |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 3811 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3812 | |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 3813 | |
| 3814 | BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1); |
| 3815 | if (Op1I) { |
| 3816 | Value *A, *B; |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 3817 | if (match(Op1I, m_Or(m_Value(A), m_Value(B)))) { |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 3818 | if (A == Op0) { // B^(B|A) == (A|B)^B |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 3819 | Op1I->swapOperands(); |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 3820 | I.swapOperands(); |
| 3821 | std::swap(Op0, Op1); |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 3822 | } else if (B == Op0) { // B^(A|B) == (A|B)^B |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 3823 | I.swapOperands(); // Simplified below. |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 3824 | std::swap(Op0, Op1); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3825 | } |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 3826 | } else if (match(Op1I, m_Xor(m_Specific(Op0), m_Value(B)))) { |
Chris Lattner | cb504b9 | 2008-11-16 05:38:51 +0000 | [diff] [blame] | 3827 | return ReplaceInstUsesWith(I, B); // A^(A^B) == B |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 3828 | } else if (match(Op1I, m_Xor(m_Value(A), m_Specific(Op0)))) { |
Chris Lattner | cb504b9 | 2008-11-16 05:38:51 +0000 | [diff] [blame] | 3829 | return ReplaceInstUsesWith(I, A); // A^(B^A) == B |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 3830 | } else if (match(Op1I, m_And(m_Value(A), m_Value(B))) && |
Owen Anderson | c7d2ce7 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 3831 | Op1I->hasOneUse()){ |
Chris Lattner | 6abbdf9 | 2007-04-01 05:36:37 +0000 | [diff] [blame] | 3832 | if (A == Op0) { // A^(A&B) -> A^(B&A) |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 3833 | Op1I->swapOperands(); |
Chris Lattner | 6abbdf9 | 2007-04-01 05:36:37 +0000 | [diff] [blame] | 3834 | std::swap(A, B); |
| 3835 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 3836 | if (B == Op0) { // A^(B&A) -> (B&A)^A |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 3837 | I.swapOperands(); // Simplified below. |
| 3838 | std::swap(Op0, Op1); |
| 3839 | } |
Chris Lattner | 26ca7e1 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 3840 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 3841 | } |
| 3842 | |
| 3843 | BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0); |
| 3844 | if (Op0I) { |
| 3845 | Value *A, *B; |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 3846 | if (match(Op0I, m_Or(m_Value(A), m_Value(B))) && |
Owen Anderson | c7d2ce7 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 3847 | Op0I->hasOneUse()) { |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 3848 | if (A == Op1) // (B|A)^B == (A|B)^B |
| 3849 | std::swap(A, B); |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 3850 | if (B == Op1) // (A|B)^B == A & ~B |
| 3851 | return BinaryOperator::CreateAnd(A, Builder->CreateNot(Op1, "tmp")); |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 3852 | } else if (match(Op0I, m_Xor(m_Specific(Op1), m_Value(B)))) { |
Chris Lattner | cb504b9 | 2008-11-16 05:38:51 +0000 | [diff] [blame] | 3853 | return ReplaceInstUsesWith(I, B); // (A^B)^A == B |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 3854 | } else if (match(Op0I, m_Xor(m_Value(A), m_Specific(Op1)))) { |
Chris Lattner | cb504b9 | 2008-11-16 05:38:51 +0000 | [diff] [blame] | 3855 | return ReplaceInstUsesWith(I, A); // (B^A)^A == B |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 3856 | } else if (match(Op0I, m_And(m_Value(A), m_Value(B))) && |
Owen Anderson | c7d2ce7 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 3857 | Op0I->hasOneUse()){ |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 3858 | if (A == Op1) // (A&B)^A -> (B&A)^A |
| 3859 | std::swap(A, B); |
| 3860 | if (B == Op1 && // (B&A)^A == ~B & A |
Chris Lattner | ae1ab39 | 2006-04-01 22:05:01 +0000 | [diff] [blame] | 3861 | !isa<ConstantInt>(Op1)) { // Canonical form is (B&C)^C |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 3862 | return BinaryOperator::CreateAnd(Builder->CreateNot(A, "tmp"), Op1); |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 3863 | } |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 3864 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 3865 | } |
| 3866 | |
| 3867 | // (X >> Z) ^ (Y >> Z) -> (X^Y) >> Z for all shifts. |
| 3868 | if (Op0I && Op1I && Op0I->isShift() && |
| 3869 | Op0I->getOpcode() == Op1I->getOpcode() && |
| 3870 | Op0I->getOperand(1) == Op1I->getOperand(1) && |
| 3871 | (Op1I->hasOneUse() || Op1I->hasOneUse())) { |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 3872 | Value *NewOp = |
| 3873 | Builder->CreateXor(Op0I->getOperand(0), Op1I->getOperand(0), |
| 3874 | Op0I->getName()); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3875 | return BinaryOperator::Create(Op1I->getOpcode(), NewOp, |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 3876 | Op1I->getOperand(1)); |
| 3877 | } |
| 3878 | |
| 3879 | if (Op0I && Op1I) { |
| 3880 | Value *A, *B, *C, *D; |
| 3881 | // (A & B)^(A | B) -> A ^ B |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 3882 | if (match(Op0I, m_And(m_Value(A), m_Value(B))) && |
| 3883 | match(Op1I, m_Or(m_Value(C), m_Value(D)))) { |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 3884 | if ((A == C && B == D) || (A == D && B == C)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3885 | return BinaryOperator::CreateXor(A, B); |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 3886 | } |
| 3887 | // (A | B)^(A & B) -> A ^ B |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 3888 | if (match(Op0I, m_Or(m_Value(A), m_Value(B))) && |
| 3889 | match(Op1I, m_And(m_Value(C), m_Value(D)))) { |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 3890 | if ((A == C && B == D) || (A == D && B == C)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3891 | return BinaryOperator::CreateXor(A, B); |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 3892 | } |
| 3893 | |
| 3894 | // (A & B)^(C & D) |
| 3895 | if ((Op0I->hasOneUse() || Op1I->hasOneUse()) && |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 3896 | match(Op0I, m_And(m_Value(A), m_Value(B))) && |
| 3897 | match(Op1I, m_And(m_Value(C), m_Value(D)))) { |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 3898 | // (X & Y)^(X & Y) -> (Y^Z) & X |
| 3899 | Value *X = 0, *Y = 0, *Z = 0; |
| 3900 | if (A == C) |
| 3901 | X = A, Y = B, Z = D; |
| 3902 | else if (A == D) |
| 3903 | X = A, Y = B, Z = C; |
| 3904 | else if (B == C) |
| 3905 | X = B, Y = A, Z = D; |
| 3906 | else if (B == D) |
| 3907 | X = B, Y = A, Z = C; |
| 3908 | |
| 3909 | if (X) { |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 3910 | Value *NewOp = Builder->CreateXor(Y, Z, Op0->getName()); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3911 | return BinaryOperator::CreateAnd(NewOp, X); |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 3912 | } |
| 3913 | } |
| 3914 | } |
| 3915 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3916 | // (icmp1 A, B) ^ (icmp2 A, B) --> (icmp3 A, B) |
| 3917 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1))) |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 3918 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS))) |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3919 | return R; |
| 3920 | |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3921 | // fold (xor (cast A), (cast B)) -> (cast (xor A, B)) |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 3922 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) { |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3923 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3924 | if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind? |
| 3925 | const Type *SrcTy = Op0C->getOperand(0)->getType(); |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 3926 | if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() && |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3927 | // Only do this if the casts both really cause code to be generated. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3928 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame^] | 3929 | I.getType()) && |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3930 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame^] | 3931 | I.getType())) { |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 3932 | Value *NewOp = Builder->CreateXor(Op0C->getOperand(0), |
| 3933 | Op1C->getOperand(0), I.getName()); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3934 | return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType()); |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3935 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3936 | } |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 3937 | } |
Nick Lewycky | 517e1f5 | 2008-05-31 19:01:33 +0000 | [diff] [blame] | 3938 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3939 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3940 | } |
| 3941 | |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3942 | |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3943 | Instruction *InstCombiner::visitShl(BinaryOperator &I) { |
| 3944 | return commonShiftTransforms(I); |
| 3945 | } |
| 3946 | |
| 3947 | Instruction *InstCombiner::visitLShr(BinaryOperator &I) { |
| 3948 | return commonShiftTransforms(I); |
| 3949 | } |
| 3950 | |
| 3951 | Instruction *InstCombiner::visitAShr(BinaryOperator &I) { |
Chris Lattner | 348f665 | 2007-12-06 01:59:46 +0000 | [diff] [blame] | 3952 | if (Instruction *R = commonShiftTransforms(I)) |
| 3953 | return R; |
| 3954 | |
| 3955 | Value *Op0 = I.getOperand(0); |
| 3956 | |
| 3957 | // ashr int -1, X = -1 (for any arithmetic shift rights of ~0) |
| 3958 | if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0)) |
| 3959 | if (CSI->isAllOnesValue()) |
| 3960 | return ReplaceInstUsesWith(I, CSI); |
Dan Gohman | 0001e56 | 2009-02-24 02:00:40 +0000 | [diff] [blame] | 3961 | |
Dan Gohman | c6ac322 | 2009-06-16 19:55:29 +0000 | [diff] [blame] | 3962 | // See if we can turn a signed shr into an unsigned shr. |
| 3963 | if (MaskedValueIsZero(Op0, |
| 3964 | APInt::getSignBit(I.getType()->getScalarSizeInBits()))) |
| 3965 | return BinaryOperator::CreateLShr(Op0, I.getOperand(1)); |
| 3966 | |
| 3967 | // Arithmetic shifting an all-sign-bit value is a no-op. |
| 3968 | unsigned NumSignBits = ComputeNumSignBits(Op0); |
| 3969 | if (NumSignBits == Op0->getType()->getScalarSizeInBits()) |
| 3970 | return ReplaceInstUsesWith(I, Op0); |
Dan Gohman | 0001e56 | 2009-02-24 02:00:40 +0000 | [diff] [blame] | 3971 | |
Chris Lattner | 348f665 | 2007-12-06 01:59:46 +0000 | [diff] [blame] | 3972 | return 0; |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3973 | } |
| 3974 | |
| 3975 | Instruction *InstCombiner::commonShiftTransforms(BinaryOperator &I) { |
| 3976 | assert(I.getOperand(1)->getType() == I.getOperand(0)->getType()); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3977 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3978 | |
| 3979 | // shl X, 0 == X and shr X, 0 == X |
| 3980 | // shl 0, X == 0 and shr 0, X == 0 |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 3981 | if (Op1 == Constant::getNullValue(Op1->getType()) || |
| 3982 | Op0 == Constant::getNullValue(Op0->getType())) |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 3983 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 8d6bbdb | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 3984 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3985 | if (isa<UndefValue>(Op0)) { |
| 3986 | if (I.getOpcode() == Instruction::AShr) // undef >>s X -> undef |
Chris Lattner | 79a564c | 2004-10-16 23:28:04 +0000 | [diff] [blame] | 3987 | return ReplaceInstUsesWith(I, Op0); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3988 | else // undef << X -> 0, undef >>u X -> 0 |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 3989 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3990 | } |
| 3991 | if (isa<UndefValue>(Op1)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3992 | if (I.getOpcode() == Instruction::AShr) // X >>s undef -> X |
| 3993 | return ReplaceInstUsesWith(I, Op0); |
| 3994 | else // X << undef, X >>u undef -> 0 |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 3995 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3996 | } |
| 3997 | |
Dan Gohman | 9004c8a | 2009-05-21 02:28:33 +0000 | [diff] [blame] | 3998 | // See if we can fold away this shift. |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 3999 | if (SimplifyDemandedInstructionBits(I)) |
Dan Gohman | 9004c8a | 2009-05-21 02:28:33 +0000 | [diff] [blame] | 4000 | return &I; |
| 4001 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 4002 | // Try to fold constant and into select arguments. |
| 4003 | if (isa<Constant>(Op0)) |
| 4004 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame^] | 4005 | if (Instruction *R = FoldOpIntoSelect(I, SI)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 4006 | return R; |
| 4007 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4008 | if (ConstantInt *CUI = dyn_cast<ConstantInt>(Op1)) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 4009 | if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I)) |
| 4010 | return Res; |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4011 | return 0; |
| 4012 | } |
| 4013 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4014 | Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4015 | BinaryOperator &I) { |
Chris Lattner | 4598c94 | 2009-01-31 08:24:16 +0000 | [diff] [blame] | 4016 | bool isLeftShift = I.getOpcode() == Instruction::Shl; |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4017 | |
Chris Lattner | 8d6bbdb | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 4018 | // See if we can simplify any instructions used by the instruction whose sole |
| 4019 | // purpose is to compute bits we don't care about. |
Dan Gohman | c6ac322 | 2009-06-16 19:55:29 +0000 | [diff] [blame] | 4020 | uint32_t TypeBits = Op0->getType()->getScalarSizeInBits(); |
Chris Lattner | 8d6bbdb | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 4021 | |
Dan Gohman | a119de8 | 2009-06-14 23:30:43 +0000 | [diff] [blame] | 4022 | // shl i32 X, 32 = 0 and srl i8 Y, 9 = 0, ... just don't eliminate |
| 4023 | // a signed shift. |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4024 | // |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 4025 | if (Op1->uge(TypeBits)) { |
Chris Lattner | 0737c24 | 2007-02-02 05:29:55 +0000 | [diff] [blame] | 4026 | if (I.getOpcode() != Instruction::AShr) |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 4027 | return ReplaceInstUsesWith(I, Constant::getNullValue(Op0->getType())); |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4028 | else { |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 4029 | I.setOperand(1, ConstantInt::get(I.getType(), TypeBits-1)); |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4030 | return &I; |
Chris Lattner | 8adac75 | 2004-02-23 20:30:06 +0000 | [diff] [blame] | 4031 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4032 | } |
| 4033 | |
| 4034 | // ((X*C1) << C2) == (X * (C1 << C2)) |
| 4035 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0)) |
| 4036 | if (BO->getOpcode() == Instruction::Mul && isLeftShift) |
| 4037 | if (Constant *BOOp = dyn_cast<Constant>(BO->getOperand(1))) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4038 | return BinaryOperator::CreateMul(BO->getOperand(0), |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 4039 | ConstantExpr::getShl(BOOp, Op1)); |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4040 | |
| 4041 | // Try to fold constant and into select arguments. |
| 4042 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame^] | 4043 | if (Instruction *R = FoldOpIntoSelect(I, SI)) |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4044 | return R; |
| 4045 | if (isa<PHINode>(Op0)) |
| 4046 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 4047 | return NV; |
| 4048 | |
Chris Lattner | 8999dd3 | 2007-12-22 09:07:47 +0000 | [diff] [blame] | 4049 | // Fold shift2(trunc(shift1(x,c1)), c2) -> trunc(shift2(shift1(x,c1),c2)) |
| 4050 | if (TruncInst *TI = dyn_cast<TruncInst>(Op0)) { |
| 4051 | Instruction *TrOp = dyn_cast<Instruction>(TI->getOperand(0)); |
| 4052 | // If 'shift2' is an ashr, we would have to get the sign bit into a funny |
| 4053 | // place. Don't try to do this transformation in this case. Also, we |
| 4054 | // require that the input operand is a shift-by-constant so that we have |
| 4055 | // confidence that the shifts will get folded together. We could do this |
| 4056 | // xform in more cases, but it is unlikely to be profitable. |
| 4057 | if (TrOp && I.isLogicalShift() && TrOp->isShift() && |
| 4058 | isa<ConstantInt>(TrOp->getOperand(1))) { |
| 4059 | // Okay, we'll do this xform. Make the shift of shift. |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 4060 | Constant *ShAmt = ConstantExpr::getZExt(Op1, TrOp->getType()); |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 4061 | // (shift2 (shift1 & 0x00FF), c2) |
| 4062 | Value *NSh = Builder->CreateBinOp(I.getOpcode(), TrOp, ShAmt,I.getName()); |
Chris Lattner | 8999dd3 | 2007-12-22 09:07:47 +0000 | [diff] [blame] | 4063 | |
| 4064 | // For logical shifts, the truncation has the effect of making the high |
| 4065 | // part of the register be zeros. Emulate this by inserting an AND to |
| 4066 | // clear the top bits as needed. This 'and' will usually be zapped by |
| 4067 | // other xforms later if dead. |
Dan Gohman | c6ac322 | 2009-06-16 19:55:29 +0000 | [diff] [blame] | 4068 | unsigned SrcSize = TrOp->getType()->getScalarSizeInBits(); |
| 4069 | unsigned DstSize = TI->getType()->getScalarSizeInBits(); |
Chris Lattner | 8999dd3 | 2007-12-22 09:07:47 +0000 | [diff] [blame] | 4070 | APInt MaskV(APInt::getLowBitsSet(SrcSize, DstSize)); |
| 4071 | |
| 4072 | // The mask we constructed says what the trunc would do if occurring |
| 4073 | // between the shifts. We want to know the effect *after* the second |
| 4074 | // shift. We know that it is a logical shift by a constant, so adjust the |
| 4075 | // mask as appropriate. |
| 4076 | if (I.getOpcode() == Instruction::Shl) |
| 4077 | MaskV <<= Op1->getZExtValue(); |
| 4078 | else { |
| 4079 | assert(I.getOpcode() == Instruction::LShr && "Unknown logical shift"); |
| 4080 | MaskV = MaskV.lshr(Op1->getZExtValue()); |
| 4081 | } |
| 4082 | |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 4083 | // shift1 & 0x00FF |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 4084 | Value *And = Builder->CreateAnd(NSh, |
| 4085 | ConstantInt::get(I.getContext(), MaskV), |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 4086 | TI->getName()); |
Chris Lattner | 8999dd3 | 2007-12-22 09:07:47 +0000 | [diff] [blame] | 4087 | |
| 4088 | // Return the value truncated to the interesting size. |
| 4089 | return new TruncInst(And, I.getType()); |
| 4090 | } |
| 4091 | } |
| 4092 | |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4093 | if (Op0->hasOneUse()) { |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4094 | if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0)) { |
| 4095 | // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C) |
| 4096 | Value *V1, *V2; |
| 4097 | ConstantInt *CC; |
| 4098 | switch (Op0BO->getOpcode()) { |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 4099 | default: break; |
| 4100 | case Instruction::Add: |
| 4101 | case Instruction::And: |
| 4102 | case Instruction::Or: |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 4103 | case Instruction::Xor: { |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 4104 | // These operators commute. |
| 4105 | // Turn (Y + (X >> C)) << C -> (X + (Y << C)) & (~0 << C) |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 4106 | if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() && |
Owen Anderson | c7d2ce7 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 4107 | match(Op0BO->getOperand(1), m_Shr(m_Value(V1), |
Chris Lattner | f925cbd | 2009-08-30 18:50:58 +0000 | [diff] [blame] | 4108 | m_Specific(Op1)))) { |
| 4109 | Value *YS = // (Y << C) |
| 4110 | Builder->CreateShl(Op0BO->getOperand(0), Op1, Op0BO->getName()); |
| 4111 | // (X + (Y << C)) |
| 4112 | Value *X = Builder->CreateBinOp(Op0BO->getOpcode(), YS, V1, |
| 4113 | Op0BO->getOperand(1)->getName()); |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 4114 | uint32_t Op1Val = Op1->getLimitedValue(TypeBits); |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 4115 | return BinaryOperator::CreateAnd(X, ConstantInt::get(I.getContext(), |
Zhou Sheng | 90b9681 | 2007-03-30 05:45:18 +0000 | [diff] [blame] | 4116 | APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val))); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 4117 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4118 | |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 4119 | // Turn (Y + ((X >> C) & CC)) << C -> ((X & (CC << C)) + (Y << C)) |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 4120 | Value *Op0BOOp1 = Op0BO->getOperand(1); |
Chris Lattner | 3c69849 | 2007-03-05 00:11:19 +0000 | [diff] [blame] | 4121 | if (isLeftShift && Op0BOOp1->hasOneUse() && |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 4122 | match(Op0BOOp1, |
Chris Lattner | cb504b9 | 2008-11-16 05:38:51 +0000 | [diff] [blame] | 4123 | m_And(m_Shr(m_Value(V1), m_Specific(Op1)), |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 4124 | m_ConstantInt(CC))) && |
Chris Lattner | cb504b9 | 2008-11-16 05:38:51 +0000 | [diff] [blame] | 4125 | cast<BinaryOperator>(Op0BOOp1)->getOperand(0)->hasOneUse()) { |
Chris Lattner | f925cbd | 2009-08-30 18:50:58 +0000 | [diff] [blame] | 4126 | Value *YS = // (Y << C) |
| 4127 | Builder->CreateShl(Op0BO->getOperand(0), Op1, |
| 4128 | Op0BO->getName()); |
| 4129 | // X & (CC << C) |
| 4130 | Value *XM = Builder->CreateAnd(V1, ConstantExpr::getShl(CC, Op1), |
| 4131 | V1->getName()+".mask"); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4132 | return BinaryOperator::Create(Op0BO->getOpcode(), YS, XM); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 4133 | } |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 4134 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4135 | |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 4136 | // FALL THROUGH. |
| 4137 | case Instruction::Sub: { |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 4138 | // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C) |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 4139 | if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() && |
Owen Anderson | c7d2ce7 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 4140 | match(Op0BO->getOperand(0), m_Shr(m_Value(V1), |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 4141 | m_Specific(Op1)))) { |
Chris Lattner | f925cbd | 2009-08-30 18:50:58 +0000 | [diff] [blame] | 4142 | Value *YS = // (Y << C) |
| 4143 | Builder->CreateShl(Op0BO->getOperand(1), Op1, Op0BO->getName()); |
| 4144 | // (X + (Y << C)) |
| 4145 | Value *X = Builder->CreateBinOp(Op0BO->getOpcode(), V1, YS, |
| 4146 | Op0BO->getOperand(0)->getName()); |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 4147 | uint32_t Op1Val = Op1->getLimitedValue(TypeBits); |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 4148 | return BinaryOperator::CreateAnd(X, ConstantInt::get(I.getContext(), |
Zhou Sheng | 90b9681 | 2007-03-30 05:45:18 +0000 | [diff] [blame] | 4149 | APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val))); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 4150 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4151 | |
Chris Lattner | 13d4ab4 | 2006-05-31 21:14:00 +0000 | [diff] [blame] | 4152 | // Turn (((X >> C)&CC) + Y) << C -> (X + (Y << C)) & (CC << C) |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 4153 | if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() && |
| 4154 | match(Op0BO->getOperand(0), |
| 4155 | m_And(m_Shr(m_Value(V1), m_Value(V2)), |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 4156 | m_ConstantInt(CC))) && V2 == Op1 && |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 4157 | cast<BinaryOperator>(Op0BO->getOperand(0)) |
| 4158 | ->getOperand(0)->hasOneUse()) { |
Chris Lattner | f925cbd | 2009-08-30 18:50:58 +0000 | [diff] [blame] | 4159 | Value *YS = // (Y << C) |
| 4160 | Builder->CreateShl(Op0BO->getOperand(1), Op1, Op0BO->getName()); |
| 4161 | // X & (CC << C) |
| 4162 | Value *XM = Builder->CreateAnd(V1, ConstantExpr::getShl(CC, Op1), |
| 4163 | V1->getName()+".mask"); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 4164 | |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4165 | return BinaryOperator::Create(Op0BO->getOpcode(), XM, YS); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 4166 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4167 | |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 4168 | break; |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 4169 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4170 | } |
| 4171 | |
| 4172 | |
| 4173 | // If the operand is an bitwise operator with a constant RHS, and the |
| 4174 | // shift is the only use, we can pull it out of the shift. |
| 4175 | if (ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand(1))) { |
| 4176 | bool isValid = true; // Valid only for And, Or, Xor |
| 4177 | bool highBitSet = false; // Transform if high bit of constant set? |
| 4178 | |
| 4179 | switch (Op0BO->getOpcode()) { |
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 4180 | default: isValid = false; break; // Do not perform transform! |
Chris Lattner | 1f7e160 | 2004-10-08 03:46:20 +0000 | [diff] [blame] | 4181 | case Instruction::Add: |
| 4182 | isValid = isLeftShift; |
| 4183 | break; |
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 4184 | case Instruction::Or: |
| 4185 | case Instruction::Xor: |
| 4186 | highBitSet = false; |
| 4187 | break; |
| 4188 | case Instruction::And: |
| 4189 | highBitSet = true; |
| 4190 | break; |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4191 | } |
| 4192 | |
| 4193 | // If this is a signed shift right, and the high bit is modified |
| 4194 | // by the logical operation, do not perform the transformation. |
| 4195 | // The highBitSet boolean indicates the value of the high bit of |
| 4196 | // the constant which would cause it to be modified for this |
| 4197 | // operation. |
| 4198 | // |
Chris Lattner | c95ba44 | 2007-12-06 06:25:04 +0000 | [diff] [blame] | 4199 | if (isValid && I.getOpcode() == Instruction::AShr) |
Zhou Sheng | e9e03f6 | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 4200 | isValid = Op0C->getValue()[TypeBits-1] == highBitSet; |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4201 | |
| 4202 | if (isValid) { |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 4203 | Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, Op1); |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4204 | |
Chris Lattner | f925cbd | 2009-08-30 18:50:58 +0000 | [diff] [blame] | 4205 | Value *NewShift = |
| 4206 | Builder->CreateBinOp(I.getOpcode(), Op0BO->getOperand(0), Op1); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 4207 | NewShift->takeName(Op0BO); |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4208 | |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4209 | return BinaryOperator::Create(Op0BO->getOpcode(), NewShift, |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 4210 | NewRHS); |
| 4211 | } |
| 4212 | } |
| 4213 | } |
| 4214 | } |
| 4215 | |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 4216 | // Find out if this is a shift of a shift by a constant. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4217 | BinaryOperator *ShiftOp = dyn_cast<BinaryOperator>(Op0); |
| 4218 | if (ShiftOp && !ShiftOp->isShift()) |
| 4219 | ShiftOp = 0; |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 4220 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4221 | if (ShiftOp && isa<ConstantInt>(ShiftOp->getOperand(1))) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4222 | ConstantInt *ShiftAmt1C = cast<ConstantInt>(ShiftOp->getOperand(1)); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 4223 | uint32_t ShiftAmt1 = ShiftAmt1C->getLimitedValue(TypeBits); |
| 4224 | uint32_t ShiftAmt2 = Op1->getLimitedValue(TypeBits); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 4225 | assert(ShiftAmt2 != 0 && "Should have been simplified earlier"); |
| 4226 | if (ShiftAmt1 == 0) return 0; // Will be simplified in the future. |
| 4227 | Value *X = ShiftOp->getOperand(0); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 4228 | |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 4229 | uint32_t AmtSum = ShiftAmt1+ShiftAmt2; // Fold into one big shift. |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 4230 | |
| 4231 | const IntegerType *Ty = cast<IntegerType>(I.getType()); |
| 4232 | |
| 4233 | // Check for (X << c1) << c2 and (X >> c1) >> c2 |
Chris Lattner | 7f3da2d | 2007-02-03 23:28:07 +0000 | [diff] [blame] | 4234 | if (I.getOpcode() == ShiftOp->getOpcode()) { |
Chris Lattner | 344c7c5 | 2009-03-20 22:41:15 +0000 | [diff] [blame] | 4235 | // If this is oversized composite shift, then unsigned shifts get 0, ashr |
| 4236 | // saturates. |
| 4237 | if (AmtSum >= TypeBits) { |
| 4238 | if (I.getOpcode() != Instruction::AShr) |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 4239 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | 344c7c5 | 2009-03-20 22:41:15 +0000 | [diff] [blame] | 4240 | AmtSum = TypeBits-1; // Saturate to 31 for i32 ashr. |
| 4241 | } |
| 4242 | |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4243 | return BinaryOperator::Create(I.getOpcode(), X, |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 4244 | ConstantInt::get(Ty, AmtSum)); |
Chris Lattner | f925cbd | 2009-08-30 18:50:58 +0000 | [diff] [blame] | 4245 | } |
| 4246 | |
| 4247 | if (ShiftOp->getOpcode() == Instruction::LShr && |
| 4248 | I.getOpcode() == Instruction::AShr) { |
Chris Lattner | 344c7c5 | 2009-03-20 22:41:15 +0000 | [diff] [blame] | 4249 | if (AmtSum >= TypeBits) |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 4250 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | 344c7c5 | 2009-03-20 22:41:15 +0000 | [diff] [blame] | 4251 | |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 4252 | // ((X >>u C1) >>s C2) -> (X >>u (C1+C2)) since C1 != 0. |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 4253 | return BinaryOperator::CreateLShr(X, ConstantInt::get(Ty, AmtSum)); |
Chris Lattner | f925cbd | 2009-08-30 18:50:58 +0000 | [diff] [blame] | 4254 | } |
| 4255 | |
| 4256 | if (ShiftOp->getOpcode() == Instruction::AShr && |
| 4257 | I.getOpcode() == Instruction::LShr) { |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 4258 | // ((X >>s C1) >>u C2) -> ((X >>s (C1+C2)) & mask) since C1 != 0. |
Chris Lattner | 344c7c5 | 2009-03-20 22:41:15 +0000 | [diff] [blame] | 4259 | if (AmtSum >= TypeBits) |
| 4260 | AmtSum = TypeBits-1; |
| 4261 | |
Chris Lattner | f925cbd | 2009-08-30 18:50:58 +0000 | [diff] [blame] | 4262 | Value *Shift = Builder->CreateAShr(X, ConstantInt::get(Ty, AmtSum)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 4263 | |
Zhou Sheng | e9e03f6 | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 4264 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 4265 | return BinaryOperator::CreateAnd(Shift, |
| 4266 | ConstantInt::get(I.getContext(), Mask)); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 4267 | } |
| 4268 | |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 4269 | // Okay, if we get here, one shift must be left, and the other shift must be |
| 4270 | // right. See if the amounts are equal. |
| 4271 | if (ShiftAmt1 == ShiftAmt2) { |
| 4272 | // If we have ((X >>? C) << C), turn this into X & (-1 << C). |
| 4273 | if (I.getOpcode() == Instruction::Shl) { |
Reid Spencer | 55702aa | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 4274 | APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt1)); |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 4275 | return BinaryOperator::CreateAnd(X, |
| 4276 | ConstantInt::get(I.getContext(),Mask)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 4277 | } |
| 4278 | // If we have ((X << C) >>u C), turn this into X & (-1 >>u C). |
| 4279 | if (I.getOpcode() == Instruction::LShr) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 4280 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt1)); |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 4281 | return BinaryOperator::CreateAnd(X, |
| 4282 | ConstantInt::get(I.getContext(), Mask)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 4283 | } |
| 4284 | // We can simplify ((X << C) >>s C) into a trunc + sext. |
| 4285 | // NOTE: we could do this for any C, but that would make 'unusual' integer |
| 4286 | // types. For now, just stick to ones well-supported by the code |
| 4287 | // generators. |
| 4288 | const Type *SExtType = 0; |
| 4289 | switch (Ty->getBitWidth() - ShiftAmt1) { |
Zhou Sheng | e9e03f6 | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 4290 | case 1 : |
| 4291 | case 8 : |
| 4292 | case 16 : |
| 4293 | case 32 : |
| 4294 | case 64 : |
| 4295 | case 128: |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 4296 | SExtType = IntegerType::get(I.getContext(), |
| 4297 | Ty->getBitWidth() - ShiftAmt1); |
Zhou Sheng | e9e03f6 | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 4298 | break; |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 4299 | default: break; |
| 4300 | } |
Chris Lattner | f925cbd | 2009-08-30 18:50:58 +0000 | [diff] [blame] | 4301 | if (SExtType) |
| 4302 | return new SExtInst(Builder->CreateTrunc(X, SExtType, "sext"), Ty); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 4303 | // Otherwise, we can't handle it yet. |
| 4304 | } else if (ShiftAmt1 < ShiftAmt2) { |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 4305 | uint32_t ShiftDiff = ShiftAmt2-ShiftAmt1; |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 4306 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 4307 | // (X >>? C1) << C2 --> X << (C2-C1) & (-1 << C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 4308 | if (I.getOpcode() == Instruction::Shl) { |
| 4309 | assert(ShiftOp->getOpcode() == Instruction::LShr || |
| 4310 | ShiftOp->getOpcode() == Instruction::AShr); |
Chris Lattner | f925cbd | 2009-08-30 18:50:58 +0000 | [diff] [blame] | 4311 | Value *Shift = Builder->CreateShl(X, ConstantInt::get(Ty, ShiftDiff)); |
Chris Lattner | e8d56c5 | 2006-01-07 01:32:28 +0000 | [diff] [blame] | 4312 | |
Reid Spencer | 55702aa | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 4313 | APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 4314 | return BinaryOperator::CreateAnd(Shift, |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 4315 | ConstantInt::get(I.getContext(),Mask)); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 4316 | } |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 4317 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 4318 | // (X << C1) >>u C2 --> X >>u (C2-C1) & (-1 >> C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 4319 | if (I.getOpcode() == Instruction::LShr) { |
| 4320 | assert(ShiftOp->getOpcode() == Instruction::Shl); |
Chris Lattner | f925cbd | 2009-08-30 18:50:58 +0000 | [diff] [blame] | 4321 | Value *Shift = Builder->CreateLShr(X, ConstantInt::get(Ty, ShiftDiff)); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 4322 | |
Reid Spencer | d5e30f0 | 2007-03-26 17:18:58 +0000 | [diff] [blame] | 4323 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 4324 | return BinaryOperator::CreateAnd(Shift, |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 4325 | ConstantInt::get(I.getContext(),Mask)); |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 4326 | } |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 4327 | |
| 4328 | // We can't handle (X << C1) >>s C2, it shifts arbitrary bits in. |
| 4329 | } else { |
| 4330 | assert(ShiftAmt2 < ShiftAmt1); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 4331 | uint32_t ShiftDiff = ShiftAmt1-ShiftAmt2; |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 4332 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 4333 | // (X >>? C1) << C2 --> X >>? (C1-C2) & (-1 << C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 4334 | if (I.getOpcode() == Instruction::Shl) { |
| 4335 | assert(ShiftOp->getOpcode() == Instruction::LShr || |
| 4336 | ShiftOp->getOpcode() == Instruction::AShr); |
Chris Lattner | f925cbd | 2009-08-30 18:50:58 +0000 | [diff] [blame] | 4337 | Value *Shift = Builder->CreateBinOp(ShiftOp->getOpcode(), X, |
| 4338 | ConstantInt::get(Ty, ShiftDiff)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 4339 | |
Reid Spencer | 55702aa | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 4340 | APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 4341 | return BinaryOperator::CreateAnd(Shift, |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 4342 | ConstantInt::get(I.getContext(),Mask)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 4343 | } |
| 4344 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 4345 | // (X << C1) >>u C2 --> X << (C1-C2) & (-1 >> C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 4346 | if (I.getOpcode() == Instruction::LShr) { |
| 4347 | assert(ShiftOp->getOpcode() == Instruction::Shl); |
Chris Lattner | f925cbd | 2009-08-30 18:50:58 +0000 | [diff] [blame] | 4348 | Value *Shift = Builder->CreateShl(X, ConstantInt::get(Ty, ShiftDiff)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 4349 | |
Reid Spencer | 68d27cf | 2007-03-26 23:45:51 +0000 | [diff] [blame] | 4350 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 4351 | return BinaryOperator::CreateAnd(Shift, |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 4352 | ConstantInt::get(I.getContext(),Mask)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 4353 | } |
| 4354 | |
| 4355 | // We can't handle (X << C1) >>a C2, it shifts arbitrary bits in. |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 4356 | } |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 4357 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4358 | return 0; |
| 4359 | } |
| 4360 | |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 4361 | |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 4362 | /// DecomposeSimpleLinearExpr - Analyze 'Val', seeing if it is a simple linear |
| 4363 | /// expression. If so, decompose it, returning some value X, such that Val is |
| 4364 | /// X*Scale+Offset. |
| 4365 | /// |
| 4366 | static Value *DecomposeSimpleLinearExpr(Value *Val, unsigned &Scale, |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 4367 | int &Offset) { |
| 4368 | assert(Val->getType() == Type::getInt32Ty(Val->getContext()) && |
Chris Lattner | f925cbd | 2009-08-30 18:50:58 +0000 | [diff] [blame] | 4369 | "Unexpected allocation size type!"); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4370 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 4371 | Offset = CI->getZExtValue(); |
Chris Lattner | 6a94de2 | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 4372 | Scale = 0; |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 4373 | return ConstantInt::get(Type::getInt32Ty(Val->getContext()), 0); |
Chris Lattner | 6a94de2 | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 4374 | } else if (BinaryOperator *I = dyn_cast<BinaryOperator>(Val)) { |
| 4375 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 4376 | if (I->getOpcode() == Instruction::Shl) { |
| 4377 | // This is a value scaled by '1 << the shift amt'. |
| 4378 | Scale = 1U << RHS->getZExtValue(); |
| 4379 | Offset = 0; |
| 4380 | return I->getOperand(0); |
| 4381 | } else if (I->getOpcode() == Instruction::Mul) { |
| 4382 | // This value is scaled by 'RHS'. |
| 4383 | Scale = RHS->getZExtValue(); |
| 4384 | Offset = 0; |
| 4385 | return I->getOperand(0); |
| 4386 | } else if (I->getOpcode() == Instruction::Add) { |
| 4387 | // We have X+C. Check to see if we really have (X*C2)+C1, |
| 4388 | // where C1 is divisible by C2. |
| 4389 | unsigned SubScale; |
| 4390 | Value *SubVal = |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 4391 | DecomposeSimpleLinearExpr(I->getOperand(0), SubScale, Offset); |
Chris Lattner | 6a94de2 | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 4392 | Offset += RHS->getZExtValue(); |
| 4393 | Scale = SubScale; |
| 4394 | return SubVal; |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 4395 | } |
| 4396 | } |
| 4397 | } |
| 4398 | |
| 4399 | // Otherwise, we can't look past this. |
| 4400 | Scale = 1; |
| 4401 | Offset = 0; |
| 4402 | return Val; |
| 4403 | } |
| 4404 | |
| 4405 | |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 4406 | /// PromoteCastOfAllocation - If we find a cast of an allocation instruction, |
| 4407 | /// try to eliminate the cast by moving the type information into the alloc. |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 4408 | Instruction *InstCombiner::PromoteCastOfAllocation(BitCastInst &CI, |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 4409 | AllocaInst &AI) { |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 4410 | const PointerType *PTy = cast<PointerType>(CI.getType()); |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 4411 | |
Chris Lattner | f925cbd | 2009-08-30 18:50:58 +0000 | [diff] [blame] | 4412 | BuilderTy AllocaBuilder(*Builder); |
| 4413 | AllocaBuilder.SetInsertPoint(AI.getParent(), &AI); |
| 4414 | |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 4415 | // Remove any uses of AI that are dead. |
| 4416 | assert(!CI.use_empty() && "Dead instructions should be removed earlier!"); |
Chris Lattner | 535014f | 2007-02-15 22:52:10 +0000 | [diff] [blame] | 4417 | |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 4418 | for (Value::use_iterator UI = AI.use_begin(), E = AI.use_end(); UI != E; ) { |
| 4419 | Instruction *User = cast<Instruction>(*UI++); |
| 4420 | if (isInstructionTriviallyDead(User)) { |
| 4421 | while (UI != E && *UI == User) |
| 4422 | ++UI; // If this instruction uses AI more than once, don't break UI. |
| 4423 | |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 4424 | ++NumDeadInst; |
Chris Lattner | bdff548 | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 4425 | DEBUG(errs() << "IC: DCE: " << *User << '\n'); |
Chris Lattner | f22a5c6 | 2007-03-02 19:59:19 +0000 | [diff] [blame] | 4426 | EraseInstFromFunction(*User); |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 4427 | } |
| 4428 | } |
Dan Gohman | ce9fe9f | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 4429 | |
| 4430 | // This requires TargetData to get the alloca alignment and size information. |
| 4431 | if (!TD) return 0; |
| 4432 | |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 4433 | // Get the type really allocated and the type casted to. |
| 4434 | const Type *AllocElTy = AI.getAllocatedType(); |
| 4435 | const Type *CastElTy = PTy->getElementType(); |
| 4436 | if (!AllocElTy->isSized() || !CastElTy->isSized()) return 0; |
Chris Lattner | 18e78bb | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 4437 | |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 4438 | unsigned AllocElTyAlign = TD->getABITypeAlignment(AllocElTy); |
| 4439 | unsigned CastElTyAlign = TD->getABITypeAlignment(CastElTy); |
Chris Lattner | 18e78bb | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 4440 | if (CastElTyAlign < AllocElTyAlign) return 0; |
| 4441 | |
Chris Lattner | 39387a5 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 4442 | // If the allocation has multiple uses, only promote it if we are strictly |
| 4443 | // increasing the alignment of the resultant allocation. If we keep it the |
Dale Johannesen | a0a6637 | 2009-03-05 00:39:02 +0000 | [diff] [blame] | 4444 | // same, we open the door to infinite loops of various kinds. (A reference |
| 4445 | // from a dbg.declare doesn't count as a use for this purpose.) |
| 4446 | if (!AI.hasOneUse() && !hasOneUsePlusDeclare(&AI) && |
| 4447 | CastElTyAlign == AllocElTyAlign) return 0; |
Chris Lattner | 39387a5 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 4448 | |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 4449 | uint64_t AllocElTySize = TD->getTypeAllocSize(AllocElTy); |
| 4450 | uint64_t CastElTySize = TD->getTypeAllocSize(CastElTy); |
Chris Lattner | 0ddac2a | 2005-10-27 05:53:56 +0000 | [diff] [blame] | 4451 | if (CastElTySize == 0 || AllocElTySize == 0) return 0; |
Chris Lattner | 18e78bb | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 4452 | |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 4453 | // See if we can satisfy the modulus by pulling a scale out of the array |
| 4454 | // size argument. |
Jeff Cohen | 86796be | 2007-04-04 16:58:57 +0000 | [diff] [blame] | 4455 | unsigned ArraySizeScale; |
| 4456 | int ArrayOffset; |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 4457 | Value *NumElements = // See if the array size is a decomposable linear expr. |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 4458 | DecomposeSimpleLinearExpr(AI.getOperand(0), ArraySizeScale, ArrayOffset); |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 4459 | |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 4460 | // If we can now satisfy the modulus, by using a non-1 scale, we really can |
| 4461 | // do the xform. |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 4462 | if ((AllocElTySize*ArraySizeScale) % CastElTySize != 0 || |
| 4463 | (AllocElTySize*ArrayOffset ) % CastElTySize != 0) return 0; |
Chris Lattner | 8142b0a | 2005-10-27 06:12:00 +0000 | [diff] [blame] | 4464 | |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 4465 | unsigned Scale = (AllocElTySize*ArraySizeScale)/CastElTySize; |
| 4466 | Value *Amt = 0; |
| 4467 | if (Scale == 1) { |
| 4468 | Amt = NumElements; |
| 4469 | } else { |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 4470 | Amt = ConstantInt::get(Type::getInt32Ty(CI.getContext()), Scale); |
Chris Lattner | f925cbd | 2009-08-30 18:50:58 +0000 | [diff] [blame] | 4471 | // Insert before the alloca, not before the cast. |
| 4472 | Amt = AllocaBuilder.CreateMul(Amt, NumElements, "tmp"); |
Chris Lattner | 0ddac2a | 2005-10-27 05:53:56 +0000 | [diff] [blame] | 4473 | } |
| 4474 | |
Jeff Cohen | 86796be | 2007-04-04 16:58:57 +0000 | [diff] [blame] | 4475 | if (int Offset = (AllocElTySize*ArrayOffset)/CastElTySize) { |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 4476 | Value *Off = ConstantInt::get(Type::getInt32Ty(CI.getContext()), |
| 4477 | Offset, true); |
Chris Lattner | f925cbd | 2009-08-30 18:50:58 +0000 | [diff] [blame] | 4478 | Amt = AllocaBuilder.CreateAdd(Amt, Off, "tmp"); |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 4479 | } |
| 4480 | |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 4481 | AllocaInst *New = AllocaBuilder.CreateAlloca(CastElTy, Amt); |
Chris Lattner | f925cbd | 2009-08-30 18:50:58 +0000 | [diff] [blame] | 4482 | New->setAlignment(AI.getAlignment()); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 4483 | New->takeName(&AI); |
Chris Lattner | 39387a5 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 4484 | |
Dale Johannesen | a0a6637 | 2009-03-05 00:39:02 +0000 | [diff] [blame] | 4485 | // If the allocation has one real use plus a dbg.declare, just remove the |
| 4486 | // declare. |
| 4487 | if (DbgDeclareInst *DI = hasOneUsePlusDeclare(&AI)) { |
| 4488 | EraseInstFromFunction(*DI); |
| 4489 | } |
| 4490 | // If the allocation has multiple real uses, insert a cast and change all |
| 4491 | // things that used it to use the new cast. This will also hack on CI, but it |
| 4492 | // will die soon. |
| 4493 | else if (!AI.hasOneUse()) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 4494 | // New is the allocation instruction, pointer typed. AI is the original |
| 4495 | // allocation instruction, also pointer typed. Thus, cast to use is BitCast. |
Chris Lattner | f925cbd | 2009-08-30 18:50:58 +0000 | [diff] [blame] | 4496 | Value *NewCast = AllocaBuilder.CreateBitCast(New, AI.getType(), "tmpcast"); |
Chris Lattner | 39387a5 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 4497 | AI.replaceAllUsesWith(NewCast); |
| 4498 | } |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 4499 | return ReplaceInstUsesWith(CI, New); |
| 4500 | } |
| 4501 | |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 4502 | /// CanEvaluateInDifferentType - Return true if we can take the specified value |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 4503 | /// and return it as type Ty without inserting any new casts and without |
| 4504 | /// changing the computed value. This is used by code that tries to decide |
| 4505 | /// whether promoting or shrinking integer operations to wider or smaller types |
| 4506 | /// will allow us to eliminate a truncate or extend. |
| 4507 | /// |
| 4508 | /// This is a truncation operation if Ty is smaller than V->getType(), or an |
| 4509 | /// extension operation if Ty is larger. |
Chris Lattner | 8114b71 | 2008-06-18 04:00:49 +0000 | [diff] [blame] | 4510 | /// |
| 4511 | /// If CastOpc is a truncation, then Ty will be a type smaller than V. We |
| 4512 | /// should return true if trunc(V) can be computed by computing V in the smaller |
| 4513 | /// type. If V is an instruction, then trunc(inst(x,y)) can be computed as |
| 4514 | /// inst(trunc(x),trunc(y)), which only makes sense if x and y can be |
| 4515 | /// efficiently truncated. |
| 4516 | /// |
| 4517 | /// If CastOpc is a sext or zext, we are asking if the low bits of the value can |
| 4518 | /// bit computed in a larger type, which is then and'd or sext_in_reg'd to get |
| 4519 | /// the final result. |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 4520 | bool InstCombiner::CanEvaluateInDifferentType(Value *V, const Type *Ty, |
Evan Cheng | 4e56ab2 | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 4521 | unsigned CastOpc, |
| 4522 | int &NumCastsRemoved){ |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 4523 | // We can always evaluate constants in another type. |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 4524 | if (isa<Constant>(V)) |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 4525 | return true; |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 4526 | |
| 4527 | Instruction *I = dyn_cast<Instruction>(V); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 4528 | if (!I) return false; |
| 4529 | |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 4530 | const Type *OrigTy = V->getType(); |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 4531 | |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 4532 | // If this is an extension or truncate, we can often eliminate it. |
| 4533 | if (isa<TruncInst>(I) || isa<ZExtInst>(I) || isa<SExtInst>(I)) { |
| 4534 | // If this is a cast from the destination type, we can trivially eliminate |
| 4535 | // it, and this will remove a cast overall. |
| 4536 | if (I->getOperand(0)->getType() == Ty) { |
| 4537 | // If the first operand is itself a cast, and is eliminable, do not count |
| 4538 | // this as an eliminable cast. We would prefer to eliminate those two |
| 4539 | // casts first. |
Chris Lattner | 8114b71 | 2008-06-18 04:00:49 +0000 | [diff] [blame] | 4540 | if (!isa<CastInst>(I->getOperand(0)) && I->hasOneUse()) |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 4541 | ++NumCastsRemoved; |
| 4542 | return true; |
| 4543 | } |
| 4544 | } |
| 4545 | |
| 4546 | // We can't extend or shrink something that has multiple uses: doing so would |
| 4547 | // require duplicating the instruction in general, which isn't profitable. |
| 4548 | if (!I->hasOneUse()) return false; |
| 4549 | |
Evan Cheng | f35fd54 | 2009-01-15 17:01:23 +0000 | [diff] [blame] | 4550 | unsigned Opc = I->getOpcode(); |
| 4551 | switch (Opc) { |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 4552 | case Instruction::Add: |
| 4553 | case Instruction::Sub: |
Nick Lewycky | b8cd6a4 | 2008-07-05 21:19:34 +0000 | [diff] [blame] | 4554 | case Instruction::Mul: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 4555 | case Instruction::And: |
| 4556 | case Instruction::Or: |
| 4557 | case Instruction::Xor: |
| 4558 | // These operators can all arbitrarily be extended or truncated. |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 4559 | return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc, |
Evan Cheng | 4e56ab2 | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 4560 | NumCastsRemoved) && |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 4561 | CanEvaluateInDifferentType(I->getOperand(1), Ty, CastOpc, |
Evan Cheng | 4e56ab2 | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 4562 | NumCastsRemoved); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 4563 | |
Eli Friedman | 070a981 | 2009-07-13 22:46:01 +0000 | [diff] [blame] | 4564 | case Instruction::UDiv: |
| 4565 | case Instruction::URem: { |
| 4566 | // UDiv and URem can be truncated if all the truncated bits are zero. |
| 4567 | uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits(); |
| 4568 | uint32_t BitWidth = Ty->getScalarSizeInBits(); |
| 4569 | if (BitWidth < OrigBitWidth) { |
| 4570 | APInt Mask = APInt::getHighBitsSet(OrigBitWidth, OrigBitWidth-BitWidth); |
| 4571 | if (MaskedValueIsZero(I->getOperand(0), Mask) && |
| 4572 | MaskedValueIsZero(I->getOperand(1), Mask)) { |
| 4573 | return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc, |
| 4574 | NumCastsRemoved) && |
| 4575 | CanEvaluateInDifferentType(I->getOperand(1), Ty, CastOpc, |
| 4576 | NumCastsRemoved); |
| 4577 | } |
| 4578 | } |
| 4579 | break; |
| 4580 | } |
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 4581 | case Instruction::Shl: |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 4582 | // If we are truncating the result of this SHL, and if it's a shift of a |
| 4583 | // constant amount, we can always perform a SHL in a smaller type. |
| 4584 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 4585 | uint32_t BitWidth = Ty->getScalarSizeInBits(); |
| 4586 | if (BitWidth < OrigTy->getScalarSizeInBits() && |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 4587 | CI->getLimitedValue(BitWidth) < BitWidth) |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 4588 | return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc, |
Evan Cheng | 4e56ab2 | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 4589 | NumCastsRemoved); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 4590 | } |
| 4591 | break; |
| 4592 | case Instruction::LShr: |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 4593 | // If this is a truncate of a logical shr, we can truncate it to a smaller |
| 4594 | // lshr iff we know that the bits we would otherwise be shifting in are |
| 4595 | // already zeros. |
| 4596 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 4597 | uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits(); |
| 4598 | uint32_t BitWidth = Ty->getScalarSizeInBits(); |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 4599 | if (BitWidth < OrigBitWidth && |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 4600 | MaskedValueIsZero(I->getOperand(0), |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 4601 | APInt::getHighBitsSet(OrigBitWidth, OrigBitWidth-BitWidth)) && |
| 4602 | CI->getLimitedValue(BitWidth) < BitWidth) { |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 4603 | return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc, |
Evan Cheng | 4e56ab2 | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 4604 | NumCastsRemoved); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 4605 | } |
| 4606 | } |
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 4607 | break; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 4608 | case Instruction::ZExt: |
| 4609 | case Instruction::SExt: |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 4610 | case Instruction::Trunc: |
| 4611 | // If this is the same kind of case as our original (e.g. zext+zext), we |
Chris Lattner | 5543a85 | 2007-08-02 17:23:38 +0000 | [diff] [blame] | 4612 | // can safely replace it. Note that replacing it does not reduce the number |
| 4613 | // of casts in the input. |
Evan Cheng | f35fd54 | 2009-01-15 17:01:23 +0000 | [diff] [blame] | 4614 | if (Opc == CastOpc) |
| 4615 | return true; |
| 4616 | |
| 4617 | // sext (zext ty1), ty2 -> zext ty2 |
Evan Cheng | 661d9c3 | 2009-01-15 17:09:07 +0000 | [diff] [blame] | 4618 | if (CastOpc == Instruction::SExt && Opc == Instruction::ZExt) |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 4619 | return true; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 4620 | break; |
Nick Lewycky | b8cd6a4 | 2008-07-05 21:19:34 +0000 | [diff] [blame] | 4621 | case Instruction::Select: { |
| 4622 | SelectInst *SI = cast<SelectInst>(I); |
| 4623 | return CanEvaluateInDifferentType(SI->getTrueValue(), Ty, CastOpc, |
Evan Cheng | 4e56ab2 | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 4624 | NumCastsRemoved) && |
Nick Lewycky | b8cd6a4 | 2008-07-05 21:19:34 +0000 | [diff] [blame] | 4625 | CanEvaluateInDifferentType(SI->getFalseValue(), Ty, CastOpc, |
Evan Cheng | 4e56ab2 | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 4626 | NumCastsRemoved); |
Nick Lewycky | b8cd6a4 | 2008-07-05 21:19:34 +0000 | [diff] [blame] | 4627 | } |
Chris Lattner | 8114b71 | 2008-06-18 04:00:49 +0000 | [diff] [blame] | 4628 | case Instruction::PHI: { |
| 4629 | // We can change a phi if we can change all operands. |
| 4630 | PHINode *PN = cast<PHINode>(I); |
| 4631 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 4632 | if (!CanEvaluateInDifferentType(PN->getIncomingValue(i), Ty, CastOpc, |
Evan Cheng | 4e56ab2 | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 4633 | NumCastsRemoved)) |
Chris Lattner | 8114b71 | 2008-06-18 04:00:49 +0000 | [diff] [blame] | 4634 | return false; |
| 4635 | return true; |
| 4636 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 4637 | default: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 4638 | // TODO: Can handle more cases here. |
| 4639 | break; |
| 4640 | } |
| 4641 | |
| 4642 | return false; |
| 4643 | } |
| 4644 | |
| 4645 | /// EvaluateInDifferentType - Given an expression that |
| 4646 | /// CanEvaluateInDifferentType returns true for, actually insert the code to |
| 4647 | /// evaluate the expression. |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 4648 | Value *InstCombiner::EvaluateInDifferentType(Value *V, const Type *Ty, |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 4649 | bool isSigned) { |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 4650 | if (Constant *C = dyn_cast<Constant>(V)) |
Chris Lattner | 9956c05 | 2009-11-08 19:23:30 +0000 | [diff] [blame] | 4651 | return ConstantExpr::getIntegerCast(C, Ty, isSigned /*Sext or ZExt*/); |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 4652 | |
| 4653 | // Otherwise, it must be an instruction. |
| 4654 | Instruction *I = cast<Instruction>(V); |
Chris Lattner | 01859e8 | 2006-05-20 23:14:03 +0000 | [diff] [blame] | 4655 | Instruction *Res = 0; |
Evan Cheng | f35fd54 | 2009-01-15 17:01:23 +0000 | [diff] [blame] | 4656 | unsigned Opc = I->getOpcode(); |
| 4657 | switch (Opc) { |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 4658 | case Instruction::Add: |
| 4659 | case Instruction::Sub: |
Nick Lewycky | e6b0c00 | 2008-01-22 05:08:48 +0000 | [diff] [blame] | 4660 | case Instruction::Mul: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 4661 | case Instruction::And: |
| 4662 | case Instruction::Or: |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 4663 | case Instruction::Xor: |
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 4664 | case Instruction::AShr: |
| 4665 | case Instruction::LShr: |
Eli Friedman | 070a981 | 2009-07-13 22:46:01 +0000 | [diff] [blame] | 4666 | case Instruction::Shl: |
| 4667 | case Instruction::UDiv: |
| 4668 | case Instruction::URem: { |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 4669 | Value *LHS = EvaluateInDifferentType(I->getOperand(0), Ty, isSigned); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 4670 | Value *RHS = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned); |
Evan Cheng | f35fd54 | 2009-01-15 17:01:23 +0000 | [diff] [blame] | 4671 | Res = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS); |
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 4672 | break; |
| 4673 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 4674 | case Instruction::Trunc: |
| 4675 | case Instruction::ZExt: |
| 4676 | case Instruction::SExt: |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 4677 | // If the source type of the cast is the type we're trying for then we can |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 4678 | // just return the source. There's no need to insert it because it is not |
| 4679 | // new. |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 4680 | if (I->getOperand(0)->getType() == Ty) |
| 4681 | return I->getOperand(0); |
| 4682 | |
Chris Lattner | 8114b71 | 2008-06-18 04:00:49 +0000 | [diff] [blame] | 4683 | // Otherwise, must be the same type of cast, so just reinsert a new one. |
Chris Lattner | 9956c05 | 2009-11-08 19:23:30 +0000 | [diff] [blame] | 4684 | Res = CastInst::Create(cast<CastInst>(I)->getOpcode(), I->getOperand(0),Ty); |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 4685 | break; |
Nick Lewycky | b8cd6a4 | 2008-07-05 21:19:34 +0000 | [diff] [blame] | 4686 | case Instruction::Select: { |
| 4687 | Value *True = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned); |
| 4688 | Value *False = EvaluateInDifferentType(I->getOperand(2), Ty, isSigned); |
| 4689 | Res = SelectInst::Create(I->getOperand(0), True, False); |
| 4690 | break; |
| 4691 | } |
Chris Lattner | 8114b71 | 2008-06-18 04:00:49 +0000 | [diff] [blame] | 4692 | case Instruction::PHI: { |
| 4693 | PHINode *OPN = cast<PHINode>(I); |
| 4694 | PHINode *NPN = PHINode::Create(Ty); |
| 4695 | for (unsigned i = 0, e = OPN->getNumIncomingValues(); i != e; ++i) { |
| 4696 | Value *V =EvaluateInDifferentType(OPN->getIncomingValue(i), Ty, isSigned); |
| 4697 | NPN->addIncoming(V, OPN->getIncomingBlock(i)); |
| 4698 | } |
| 4699 | Res = NPN; |
| 4700 | break; |
| 4701 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 4702 | default: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 4703 | // TODO: Can handle more cases here. |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 4704 | llvm_unreachable("Unreachable!"); |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 4705 | break; |
| 4706 | } |
| 4707 | |
Chris Lattner | 8114b71 | 2008-06-18 04:00:49 +0000 | [diff] [blame] | 4708 | Res->takeName(I); |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 4709 | return InsertNewInstBefore(Res, *I); |
| 4710 | } |
| 4711 | |
Chris Lattner | 79d35b3 | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 4712 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 4713 | |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 4714 | /// FindElementAtOffset - Given a type and a constant offset, determine whether |
| 4715 | /// 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] | 4716 | /// the specified offset. If so, fill them into NewIndices and return the |
| 4717 | /// resultant element type, otherwise return null. |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame^] | 4718 | const Type *InstCombiner::FindElementAtOffset(const Type *Ty, int64_t Offset, |
| 4719 | SmallVectorImpl<Value*> &NewIndices) { |
Dan Gohman | ce9fe9f | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 4720 | if (!TD) return 0; |
Chris Lattner | 3914f72 | 2009-01-24 01:00:13 +0000 | [diff] [blame] | 4721 | if (!Ty->isSized()) return 0; |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 4722 | |
| 4723 | // Start with the index over the outer type. Note that the type size |
| 4724 | // might be zero (even if the offset isn't zero) if the indexed type |
| 4725 | // is something like [0 x {int, int}] |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 4726 | const Type *IntPtrTy = TD->getIntPtrType(Ty->getContext()); |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 4727 | int64_t FirstIdx = 0; |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 4728 | if (int64_t TySize = TD->getTypeAllocSize(Ty)) { |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 4729 | FirstIdx = Offset/TySize; |
Chris Lattner | 31a69cb | 2009-01-11 20:41:36 +0000 | [diff] [blame] | 4730 | Offset -= FirstIdx*TySize; |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 4731 | |
Chris Lattner | dbc3bc2 | 2009-01-11 20:15:20 +0000 | [diff] [blame] | 4732 | // Handle hosts where % returns negative instead of values [0..TySize). |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 4733 | if (Offset < 0) { |
| 4734 | --FirstIdx; |
| 4735 | Offset += TySize; |
| 4736 | assert(Offset >= 0); |
| 4737 | } |
| 4738 | assert((uint64_t)Offset < (uint64_t)TySize && "Out of range offset"); |
| 4739 | } |
| 4740 | |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 4741 | NewIndices.push_back(ConstantInt::get(IntPtrTy, FirstIdx)); |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 4742 | |
| 4743 | // Index into the types. If we fail, set OrigBase to null. |
| 4744 | while (Offset) { |
Chris Lattner | dbc3bc2 | 2009-01-11 20:15:20 +0000 | [diff] [blame] | 4745 | // Indexing into tail padding between struct/array elements. |
| 4746 | if (uint64_t(Offset*8) >= TD->getTypeSizeInBits(Ty)) |
Chris Lattner | 3914f72 | 2009-01-24 01:00:13 +0000 | [diff] [blame] | 4747 | return 0; |
Chris Lattner | dbc3bc2 | 2009-01-11 20:15:20 +0000 | [diff] [blame] | 4748 | |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 4749 | if (const StructType *STy = dyn_cast<StructType>(Ty)) { |
| 4750 | const StructLayout *SL = TD->getStructLayout(STy); |
Chris Lattner | dbc3bc2 | 2009-01-11 20:15:20 +0000 | [diff] [blame] | 4751 | assert(Offset < (int64_t)SL->getSizeInBytes() && |
| 4752 | "Offset must stay within the indexed type"); |
| 4753 | |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 4754 | unsigned Elt = SL->getElementContainingOffset(Offset); |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 4755 | NewIndices.push_back(ConstantInt::get(Type::getInt32Ty(Ty->getContext()), |
| 4756 | Elt)); |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 4757 | |
| 4758 | Offset -= SL->getElementOffset(Elt); |
| 4759 | Ty = STy->getElementType(Elt); |
Chris Lattner | 1c412d9 | 2009-01-11 20:23:52 +0000 | [diff] [blame] | 4760 | } else if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) { |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 4761 | uint64_t EltSize = TD->getTypeAllocSize(AT->getElementType()); |
Chris Lattner | dbc3bc2 | 2009-01-11 20:15:20 +0000 | [diff] [blame] | 4762 | assert(EltSize && "Cannot index into a zero-sized array"); |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 4763 | NewIndices.push_back(ConstantInt::get(IntPtrTy,Offset/EltSize)); |
Chris Lattner | dbc3bc2 | 2009-01-11 20:15:20 +0000 | [diff] [blame] | 4764 | Offset %= EltSize; |
Chris Lattner | 1c412d9 | 2009-01-11 20:23:52 +0000 | [diff] [blame] | 4765 | Ty = AT->getElementType(); |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 4766 | } else { |
Chris Lattner | dbc3bc2 | 2009-01-11 20:15:20 +0000 | [diff] [blame] | 4767 | // 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] | 4768 | return 0; |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 4769 | } |
| 4770 | } |
| 4771 | |
Chris Lattner | 3914f72 | 2009-01-24 01:00:13 +0000 | [diff] [blame] | 4772 | return Ty; |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 4773 | } |
| 4774 | |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 4775 | |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 4776 | /// GetSelectFoldableOperands - We want to turn code that looks like this: |
| 4777 | /// %C = or %A, %B |
| 4778 | /// %D = select %cond, %C, %A |
| 4779 | /// into: |
| 4780 | /// %C = select %cond, %B, 0 |
| 4781 | /// %D = or %A, %C |
| 4782 | /// |
| 4783 | /// Assuming that the specified instruction is an operand to the select, return |
| 4784 | /// a bitmask indicating which operands of this instruction are foldable if they |
| 4785 | /// equal the other incoming value of the select. |
| 4786 | /// |
| 4787 | static unsigned GetSelectFoldableOperands(Instruction *I) { |
| 4788 | switch (I->getOpcode()) { |
| 4789 | case Instruction::Add: |
| 4790 | case Instruction::Mul: |
| 4791 | case Instruction::And: |
| 4792 | case Instruction::Or: |
| 4793 | case Instruction::Xor: |
| 4794 | return 3; // Can fold through either operand. |
| 4795 | case Instruction::Sub: // Can only fold on the amount subtracted. |
| 4796 | case Instruction::Shl: // Can only fold on the shift amount. |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 4797 | case Instruction::LShr: |
| 4798 | case Instruction::AShr: |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4799 | return 1; |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 4800 | default: |
| 4801 | return 0; // Cannot fold |
| 4802 | } |
| 4803 | } |
| 4804 | |
| 4805 | /// GetSelectFoldableConstant - For the same transformation as the previous |
| 4806 | /// function, return the identity constant that goes into the select. |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 4807 | static Constant *GetSelectFoldableConstant(Instruction *I) { |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 4808 | switch (I->getOpcode()) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 4809 | default: llvm_unreachable("This cannot happen!"); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 4810 | case Instruction::Add: |
| 4811 | case Instruction::Sub: |
| 4812 | case Instruction::Or: |
| 4813 | case Instruction::Xor: |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 4814 | case Instruction::Shl: |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 4815 | case Instruction::LShr: |
| 4816 | case Instruction::AShr: |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 4817 | return Constant::getNullValue(I->getType()); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 4818 | case Instruction::And: |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 4819 | return Constant::getAllOnesValue(I->getType()); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 4820 | case Instruction::Mul: |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 4821 | return ConstantInt::get(I->getType(), 1); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 4822 | } |
| 4823 | } |
| 4824 | |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 4825 | /// FoldSelectOpOp - Here we have (select c, TI, FI), and we know that TI and FI |
| 4826 | /// have the same opcode and only one use each. Try to simplify this. |
| 4827 | Instruction *InstCombiner::FoldSelectOpOp(SelectInst &SI, Instruction *TI, |
| 4828 | Instruction *FI) { |
| 4829 | if (TI->getNumOperands() == 1) { |
| 4830 | // If this is a non-volatile load or a cast from the same type, |
| 4831 | // merge. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 4832 | if (TI->isCast()) { |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 4833 | if (TI->getOperand(0)->getType() != FI->getOperand(0)->getType()) |
| 4834 | return 0; |
| 4835 | } else { |
| 4836 | return 0; // unknown unary op. |
| 4837 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4838 | |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 4839 | // Fold this by inserting a select from the input values. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 4840 | SelectInst *NewSI = SelectInst::Create(SI.getCondition(), TI->getOperand(0), |
Eric Christopher | a66297a | 2009-07-25 02:45:27 +0000 | [diff] [blame] | 4841 | FI->getOperand(0), SI.getName()+".v"); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 4842 | InsertNewInstBefore(NewSI, SI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4843 | return CastInst::Create(Instruction::CastOps(TI->getOpcode()), NewSI, |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 4844 | TI->getType()); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 4845 | } |
| 4846 | |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4847 | // Only handle binary operators here. |
| 4848 | if (!isa<BinaryOperator>(TI)) |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 4849 | return 0; |
| 4850 | |
| 4851 | // Figure out if the operations have any operands in common. |
| 4852 | Value *MatchOp, *OtherOpT, *OtherOpF; |
| 4853 | bool MatchIsOpZero; |
| 4854 | if (TI->getOperand(0) == FI->getOperand(0)) { |
| 4855 | MatchOp = TI->getOperand(0); |
| 4856 | OtherOpT = TI->getOperand(1); |
| 4857 | OtherOpF = FI->getOperand(1); |
| 4858 | MatchIsOpZero = true; |
| 4859 | } else if (TI->getOperand(1) == FI->getOperand(1)) { |
| 4860 | MatchOp = TI->getOperand(1); |
| 4861 | OtherOpT = TI->getOperand(0); |
| 4862 | OtherOpF = FI->getOperand(0); |
| 4863 | MatchIsOpZero = false; |
| 4864 | } else if (!TI->isCommutative()) { |
| 4865 | return 0; |
| 4866 | } else if (TI->getOperand(0) == FI->getOperand(1)) { |
| 4867 | MatchOp = TI->getOperand(0); |
| 4868 | OtherOpT = TI->getOperand(1); |
| 4869 | OtherOpF = FI->getOperand(0); |
| 4870 | MatchIsOpZero = true; |
| 4871 | } else if (TI->getOperand(1) == FI->getOperand(0)) { |
| 4872 | MatchOp = TI->getOperand(1); |
| 4873 | OtherOpT = TI->getOperand(0); |
| 4874 | OtherOpF = FI->getOperand(1); |
| 4875 | MatchIsOpZero = true; |
| 4876 | } else { |
| 4877 | return 0; |
| 4878 | } |
| 4879 | |
| 4880 | // If we reach here, they do have operations in common. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 4881 | SelectInst *NewSI = SelectInst::Create(SI.getCondition(), OtherOpT, |
| 4882 | OtherOpF, SI.getName()+".v"); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 4883 | InsertNewInstBefore(NewSI, SI); |
| 4884 | |
| 4885 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TI)) { |
| 4886 | if (MatchIsOpZero) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4887 | return BinaryOperator::Create(BO->getOpcode(), MatchOp, NewSI); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 4888 | else |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4889 | return BinaryOperator::Create(BO->getOpcode(), NewSI, MatchOp); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 4890 | } |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 4891 | llvm_unreachable("Shouldn't get here"); |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 4892 | return 0; |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 4893 | } |
| 4894 | |
Evan Cheng | de62192 | 2009-03-31 20:42:45 +0000 | [diff] [blame] | 4895 | static bool isSelect01(Constant *C1, Constant *C2) { |
| 4896 | ConstantInt *C1I = dyn_cast<ConstantInt>(C1); |
| 4897 | if (!C1I) |
| 4898 | return false; |
| 4899 | ConstantInt *C2I = dyn_cast<ConstantInt>(C2); |
| 4900 | if (!C2I) |
| 4901 | return false; |
| 4902 | return (C1I->isZero() || C1I->isOne()) && (C2I->isZero() || C2I->isOne()); |
| 4903 | } |
| 4904 | |
| 4905 | /// FoldSelectIntoOp - Try fold the select into one of the operands to |
| 4906 | /// facilitate further optimization. |
| 4907 | Instruction *InstCombiner::FoldSelectIntoOp(SelectInst &SI, Value *TrueVal, |
| 4908 | Value *FalseVal) { |
| 4909 | // See the comment above GetSelectFoldableOperands for a description of the |
| 4910 | // transformation we are doing here. |
| 4911 | if (Instruction *TVI = dyn_cast<Instruction>(TrueVal)) { |
| 4912 | if (TVI->hasOneUse() && TVI->getNumOperands() == 2 && |
| 4913 | !isa<Constant>(FalseVal)) { |
| 4914 | if (unsigned SFO = GetSelectFoldableOperands(TVI)) { |
| 4915 | unsigned OpToFold = 0; |
| 4916 | if ((SFO & 1) && FalseVal == TVI->getOperand(0)) { |
| 4917 | OpToFold = 1; |
| 4918 | } else if ((SFO & 2) && FalseVal == TVI->getOperand(1)) { |
| 4919 | OpToFold = 2; |
| 4920 | } |
| 4921 | |
| 4922 | if (OpToFold) { |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 4923 | Constant *C = GetSelectFoldableConstant(TVI); |
Evan Cheng | de62192 | 2009-03-31 20:42:45 +0000 | [diff] [blame] | 4924 | Value *OOp = TVI->getOperand(2-OpToFold); |
| 4925 | // Avoid creating select between 2 constants unless it's selecting |
| 4926 | // between 0 and 1. |
| 4927 | if (!isa<Constant>(OOp) || isSelect01(C, cast<Constant>(OOp))) { |
| 4928 | Instruction *NewSel = SelectInst::Create(SI.getCondition(), OOp, C); |
| 4929 | InsertNewInstBefore(NewSel, SI); |
| 4930 | NewSel->takeName(TVI); |
| 4931 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TVI)) |
| 4932 | return BinaryOperator::Create(BO->getOpcode(), FalseVal, NewSel); |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 4933 | llvm_unreachable("Unknown instruction!!"); |
Evan Cheng | de62192 | 2009-03-31 20:42:45 +0000 | [diff] [blame] | 4934 | } |
| 4935 | } |
| 4936 | } |
| 4937 | } |
| 4938 | } |
| 4939 | |
| 4940 | if (Instruction *FVI = dyn_cast<Instruction>(FalseVal)) { |
| 4941 | if (FVI->hasOneUse() && FVI->getNumOperands() == 2 && |
| 4942 | !isa<Constant>(TrueVal)) { |
| 4943 | if (unsigned SFO = GetSelectFoldableOperands(FVI)) { |
| 4944 | unsigned OpToFold = 0; |
| 4945 | if ((SFO & 1) && TrueVal == FVI->getOperand(0)) { |
| 4946 | OpToFold = 1; |
| 4947 | } else if ((SFO & 2) && TrueVal == FVI->getOperand(1)) { |
| 4948 | OpToFold = 2; |
| 4949 | } |
| 4950 | |
| 4951 | if (OpToFold) { |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 4952 | Constant *C = GetSelectFoldableConstant(FVI); |
Evan Cheng | de62192 | 2009-03-31 20:42:45 +0000 | [diff] [blame] | 4953 | Value *OOp = FVI->getOperand(2-OpToFold); |
| 4954 | // Avoid creating select between 2 constants unless it's selecting |
| 4955 | // between 0 and 1. |
| 4956 | if (!isa<Constant>(OOp) || isSelect01(C, cast<Constant>(OOp))) { |
| 4957 | Instruction *NewSel = SelectInst::Create(SI.getCondition(), C, OOp); |
| 4958 | InsertNewInstBefore(NewSel, SI); |
| 4959 | NewSel->takeName(FVI); |
| 4960 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(FVI)) |
| 4961 | return BinaryOperator::Create(BO->getOpcode(), TrueVal, NewSel); |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 4962 | llvm_unreachable("Unknown instruction!!"); |
Evan Cheng | de62192 | 2009-03-31 20:42:45 +0000 | [diff] [blame] | 4963 | } |
| 4964 | } |
| 4965 | } |
| 4966 | } |
| 4967 | } |
| 4968 | |
| 4969 | return 0; |
| 4970 | } |
| 4971 | |
Dan Gohman | 81b28ce | 2008-09-16 18:46:06 +0000 | [diff] [blame] | 4972 | /// visitSelectInstWithICmp - Visit a SelectInst that has an |
| 4973 | /// ICmpInst as its first operand. |
| 4974 | /// |
| 4975 | Instruction *InstCombiner::visitSelectInstWithICmp(SelectInst &SI, |
| 4976 | ICmpInst *ICI) { |
| 4977 | bool Changed = false; |
| 4978 | ICmpInst::Predicate Pred = ICI->getPredicate(); |
| 4979 | Value *CmpLHS = ICI->getOperand(0); |
| 4980 | Value *CmpRHS = ICI->getOperand(1); |
| 4981 | Value *TrueVal = SI.getTrueValue(); |
| 4982 | Value *FalseVal = SI.getFalseValue(); |
| 4983 | |
| 4984 | // Check cases where the comparison is with a constant that |
| 4985 | // can be adjusted to fit the min/max idiom. We may edit ICI in |
| 4986 | // place here, so make sure the select is the only user. |
| 4987 | if (ICI->hasOneUse()) |
Dan Gohman | 1975d03 | 2008-10-30 20:40:10 +0000 | [diff] [blame] | 4988 | if (ConstantInt *CI = dyn_cast<ConstantInt>(CmpRHS)) { |
Dan Gohman | 81b28ce | 2008-09-16 18:46:06 +0000 | [diff] [blame] | 4989 | switch (Pred) { |
| 4990 | default: break; |
| 4991 | case ICmpInst::ICMP_ULT: |
| 4992 | case ICmpInst::ICMP_SLT: { |
| 4993 | // X < MIN ? T : F --> F |
| 4994 | if (CI->isMinValue(Pred == ICmpInst::ICMP_SLT)) |
| 4995 | return ReplaceInstUsesWith(SI, FalseVal); |
| 4996 | // X < C ? X : C-1 --> X > C-1 ? C-1 : X |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 4997 | Constant *AdjustedRHS = SubOne(CI); |
Dan Gohman | 81b28ce | 2008-09-16 18:46:06 +0000 | [diff] [blame] | 4998 | if ((CmpLHS == TrueVal && AdjustedRHS == FalseVal) || |
| 4999 | (CmpLHS == FalseVal && AdjustedRHS == TrueVal)) { |
| 5000 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 5001 | CmpRHS = AdjustedRHS; |
| 5002 | std::swap(FalseVal, TrueVal); |
| 5003 | ICI->setPredicate(Pred); |
| 5004 | ICI->setOperand(1, CmpRHS); |
| 5005 | SI.setOperand(1, TrueVal); |
| 5006 | SI.setOperand(2, FalseVal); |
| 5007 | Changed = true; |
| 5008 | } |
| 5009 | break; |
| 5010 | } |
| 5011 | case ICmpInst::ICMP_UGT: |
| 5012 | case ICmpInst::ICMP_SGT: { |
| 5013 | // X > MAX ? T : F --> F |
| 5014 | if (CI->isMaxValue(Pred == ICmpInst::ICMP_SGT)) |
| 5015 | return ReplaceInstUsesWith(SI, FalseVal); |
| 5016 | // X > C ? X : C+1 --> X < C+1 ? C+1 : X |
Dan Gohman | 186a636 | 2009-08-12 16:04:34 +0000 | [diff] [blame] | 5017 | Constant *AdjustedRHS = AddOne(CI); |
Dan Gohman | 81b28ce | 2008-09-16 18:46:06 +0000 | [diff] [blame] | 5018 | if ((CmpLHS == TrueVal && AdjustedRHS == FalseVal) || |
| 5019 | (CmpLHS == FalseVal && AdjustedRHS == TrueVal)) { |
| 5020 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 5021 | CmpRHS = AdjustedRHS; |
| 5022 | std::swap(FalseVal, TrueVal); |
| 5023 | ICI->setPredicate(Pred); |
| 5024 | ICI->setOperand(1, CmpRHS); |
| 5025 | SI.setOperand(1, TrueVal); |
| 5026 | SI.setOperand(2, FalseVal); |
| 5027 | Changed = true; |
| 5028 | } |
| 5029 | break; |
| 5030 | } |
| 5031 | } |
| 5032 | |
Dan Gohman | 1975d03 | 2008-10-30 20:40:10 +0000 | [diff] [blame] | 5033 | // (x <s 0) ? -1 : 0 -> ashr x, 31 -> all ones if signed |
| 5034 | // (x >s -1) ? -1 : 0 -> ashr x, 31 -> all ones if not signed |
Chris Lattner | cb504b9 | 2008-11-16 05:38:51 +0000 | [diff] [blame] | 5035 | CmpInst::Predicate Pred = CmpInst::BAD_ICMP_PREDICATE; |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 5036 | if (match(TrueVal, m_ConstantInt<-1>()) && |
| 5037 | match(FalseVal, m_ConstantInt<0>())) |
Chris Lattner | cb504b9 | 2008-11-16 05:38:51 +0000 | [diff] [blame] | 5038 | Pred = ICI->getPredicate(); |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 5039 | else if (match(TrueVal, m_ConstantInt<0>()) && |
| 5040 | match(FalseVal, m_ConstantInt<-1>())) |
Chris Lattner | cb504b9 | 2008-11-16 05:38:51 +0000 | [diff] [blame] | 5041 | Pred = CmpInst::getInversePredicate(ICI->getPredicate()); |
| 5042 | |
Dan Gohman | 1975d03 | 2008-10-30 20:40:10 +0000 | [diff] [blame] | 5043 | if (Pred != CmpInst::BAD_ICMP_PREDICATE) { |
| 5044 | // If we are just checking for a icmp eq of a single bit and zext'ing it |
| 5045 | // to an integer, then shift the bit to the appropriate place and then |
| 5046 | // cast to integer to avoid the comparison. |
| 5047 | const APInt &Op1CV = CI->getValue(); |
| 5048 | |
| 5049 | // sext (x <s 0) to i32 --> x>>s31 true if signbit set. |
| 5050 | // sext (x >s -1) to i32 --> (x>>s31)^-1 true if signbit clear. |
| 5051 | if ((Pred == ICmpInst::ICMP_SLT && Op1CV == 0) || |
Chris Lattner | cb504b9 | 2008-11-16 05:38:51 +0000 | [diff] [blame] | 5052 | (Pred == ICmpInst::ICMP_SGT && Op1CV.isAllOnesValue())) { |
Dan Gohman | 1975d03 | 2008-10-30 20:40:10 +0000 | [diff] [blame] | 5053 | Value *In = ICI->getOperand(0); |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 5054 | Value *Sh = ConstantInt::get(In->getType(), |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 5055 | In->getType()->getScalarSizeInBits()-1); |
Dan Gohman | 1975d03 | 2008-10-30 20:40:10 +0000 | [diff] [blame] | 5056 | In = InsertNewInstBefore(BinaryOperator::CreateAShr(In, Sh, |
Eric Christopher | a66297a | 2009-07-25 02:45:27 +0000 | [diff] [blame] | 5057 | In->getName()+".lobit"), |
Dan Gohman | 1975d03 | 2008-10-30 20:40:10 +0000 | [diff] [blame] | 5058 | *ICI); |
Dan Gohman | 21440ac | 2008-11-02 00:17:33 +0000 | [diff] [blame] | 5059 | if (In->getType() != SI.getType()) |
| 5060 | In = CastInst::CreateIntegerCast(In, SI.getType(), |
Dan Gohman | 1975d03 | 2008-10-30 20:40:10 +0000 | [diff] [blame] | 5061 | true/*SExt*/, "tmp", ICI); |
| 5062 | |
| 5063 | if (Pred == ICmpInst::ICMP_SGT) |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 5064 | In = InsertNewInstBefore(BinaryOperator::CreateNot(In, |
Dan Gohman | 1975d03 | 2008-10-30 20:40:10 +0000 | [diff] [blame] | 5065 | In->getName()+".not"), *ICI); |
| 5066 | |
| 5067 | return ReplaceInstUsesWith(SI, In); |
| 5068 | } |
| 5069 | } |
| 5070 | } |
| 5071 | |
Dan Gohman | 81b28ce | 2008-09-16 18:46:06 +0000 | [diff] [blame] | 5072 | if (CmpLHS == TrueVal && CmpRHS == FalseVal) { |
| 5073 | // Transform (X == Y) ? X : Y -> Y |
| 5074 | if (Pred == ICmpInst::ICMP_EQ) |
| 5075 | return ReplaceInstUsesWith(SI, FalseVal); |
| 5076 | // Transform (X != Y) ? X : Y -> X |
| 5077 | if (Pred == ICmpInst::ICMP_NE) |
| 5078 | return ReplaceInstUsesWith(SI, TrueVal); |
| 5079 | /// NOTE: if we wanted to, this is where to detect integer MIN/MAX |
| 5080 | |
| 5081 | } else if (CmpLHS == FalseVal && CmpRHS == TrueVal) { |
| 5082 | // Transform (X == Y) ? Y : X -> X |
| 5083 | if (Pred == ICmpInst::ICMP_EQ) |
| 5084 | return ReplaceInstUsesWith(SI, FalseVal); |
| 5085 | // Transform (X != Y) ? Y : X -> Y |
| 5086 | if (Pred == ICmpInst::ICMP_NE) |
| 5087 | return ReplaceInstUsesWith(SI, TrueVal); |
| 5088 | /// NOTE: if we wanted to, this is where to detect integer MIN/MAX |
| 5089 | } |
Dan Gohman | 81b28ce | 2008-09-16 18:46:06 +0000 | [diff] [blame] | 5090 | return Changed ? &SI : 0; |
| 5091 | } |
| 5092 | |
Chris Lattner | c6df8f4 | 2009-09-27 20:18:49 +0000 | [diff] [blame] | 5093 | |
Chris Lattner | 7f23958 | 2009-10-22 00:17:26 +0000 | [diff] [blame] | 5094 | /// CanSelectOperandBeMappingIntoPredBlock - SI is a select whose condition is a |
| 5095 | /// PHI node (but the two may be in different blocks). See if the true/false |
| 5096 | /// values (V) are live in all of the predecessor blocks of the PHI. For |
| 5097 | /// example, cases like this cannot be mapped: |
| 5098 | /// |
| 5099 | /// X = phi [ C1, BB1], [C2, BB2] |
| 5100 | /// Y = add |
| 5101 | /// Z = select X, Y, 0 |
| 5102 | /// |
| 5103 | /// because Y is not live in BB1/BB2. |
| 5104 | /// |
| 5105 | static bool CanSelectOperandBeMappingIntoPredBlock(const Value *V, |
| 5106 | const SelectInst &SI) { |
| 5107 | // If the value is a non-instruction value like a constant or argument, it |
| 5108 | // can always be mapped. |
| 5109 | const Instruction *I = dyn_cast<Instruction>(V); |
| 5110 | if (I == 0) return true; |
| 5111 | |
| 5112 | // If V is a PHI node defined in the same block as the condition PHI, we can |
| 5113 | // map the arguments. |
| 5114 | const PHINode *CondPHI = cast<PHINode>(SI.getCondition()); |
| 5115 | |
| 5116 | if (const PHINode *VP = dyn_cast<PHINode>(I)) |
| 5117 | if (VP->getParent() == CondPHI->getParent()) |
| 5118 | return true; |
| 5119 | |
| 5120 | // Otherwise, if the PHI and select are defined in the same block and if V is |
| 5121 | // defined in a different block, then we can transform it. |
| 5122 | if (SI.getParent() == CondPHI->getParent() && |
| 5123 | I->getParent() != CondPHI->getParent()) |
| 5124 | return true; |
| 5125 | |
| 5126 | // Otherwise we have a 'hard' case and we can't tell without doing more |
| 5127 | // detailed dominator based analysis, punt. |
| 5128 | return false; |
| 5129 | } |
Chris Lattner | c6df8f4 | 2009-09-27 20:18:49 +0000 | [diff] [blame] | 5130 | |
Chris Lattner | b109b5c | 2009-12-21 06:03:05 +0000 | [diff] [blame] | 5131 | /// FoldSPFofSPF - We have an SPF (e.g. a min or max) of an SPF of the form: |
| 5132 | /// SPF2(SPF1(A, B), C) |
| 5133 | Instruction *InstCombiner::FoldSPFofSPF(Instruction *Inner, |
| 5134 | SelectPatternFlavor SPF1, |
| 5135 | Value *A, Value *B, |
| 5136 | Instruction &Outer, |
| 5137 | SelectPatternFlavor SPF2, Value *C) { |
| 5138 | if (C == A || C == B) { |
| 5139 | // MAX(MAX(A, B), B) -> MAX(A, B) |
| 5140 | // MIN(MIN(a, b), a) -> MIN(a, b) |
| 5141 | if (SPF1 == SPF2) |
| 5142 | return ReplaceInstUsesWith(Outer, Inner); |
| 5143 | |
| 5144 | // MAX(MIN(a, b), a) -> a |
| 5145 | // MIN(MAX(a, b), a) -> a |
Daniel Dunbar | eddfaaf | 2009-12-21 23:27:57 +0000 | [diff] [blame] | 5146 | if ((SPF1 == SPF_SMIN && SPF2 == SPF_SMAX) || |
| 5147 | (SPF1 == SPF_SMAX && SPF2 == SPF_SMIN) || |
| 5148 | (SPF1 == SPF_UMIN && SPF2 == SPF_UMAX) || |
| 5149 | (SPF1 == SPF_UMAX && SPF2 == SPF_UMIN)) |
Chris Lattner | b109b5c | 2009-12-21 06:03:05 +0000 | [diff] [blame] | 5150 | return ReplaceInstUsesWith(Outer, C); |
| 5151 | } |
| 5152 | |
| 5153 | // TODO: MIN(MIN(A, 23), 97) |
| 5154 | return 0; |
| 5155 | } |
| 5156 | |
| 5157 | |
| 5158 | |
| 5159 | |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 5160 | Instruction *InstCombiner::visitSelectInst(SelectInst &SI) { |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 5161 | Value *CondVal = SI.getCondition(); |
| 5162 | Value *TrueVal = SI.getTrueValue(); |
| 5163 | Value *FalseVal = SI.getFalseValue(); |
| 5164 | |
| 5165 | // select true, X, Y -> X |
| 5166 | // select false, X, Y -> Y |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5167 | if (ConstantInt *C = dyn_cast<ConstantInt>(CondVal)) |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 5168 | return ReplaceInstUsesWith(SI, C->getZExtValue() ? TrueVal : FalseVal); |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 5169 | |
| 5170 | // select C, X, X -> X |
| 5171 | if (TrueVal == FalseVal) |
| 5172 | return ReplaceInstUsesWith(SI, TrueVal); |
| 5173 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5174 | if (isa<UndefValue>(TrueVal)) // select C, undef, X -> X |
| 5175 | return ReplaceInstUsesWith(SI, FalseVal); |
| 5176 | if (isa<UndefValue>(FalseVal)) // select C, X, undef -> X |
| 5177 | return ReplaceInstUsesWith(SI, TrueVal); |
| 5178 | if (isa<UndefValue>(CondVal)) { // select undef, X, Y -> X or Y |
| 5179 | if (isa<Constant>(TrueVal)) |
| 5180 | return ReplaceInstUsesWith(SI, TrueVal); |
| 5181 | else |
| 5182 | return ReplaceInstUsesWith(SI, FalseVal); |
| 5183 | } |
| 5184 | |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 5185 | if (SI.getType() == Type::getInt1Ty(SI.getContext())) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 5186 | if (ConstantInt *C = dyn_cast<ConstantInt>(TrueVal)) { |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 5187 | if (C->getZExtValue()) { |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 5188 | // Change: A = select B, true, C --> A = or B, C |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5189 | return BinaryOperator::CreateOr(CondVal, FalseVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 5190 | } else { |
| 5191 | // Change: A = select B, false, C --> A = and !B, C |
| 5192 | Value *NotCond = |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 5193 | InsertNewInstBefore(BinaryOperator::CreateNot(CondVal, |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 5194 | "not."+CondVal->getName()), SI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5195 | return BinaryOperator::CreateAnd(NotCond, FalseVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 5196 | } |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 5197 | } else if (ConstantInt *C = dyn_cast<ConstantInt>(FalseVal)) { |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 5198 | if (C->getZExtValue() == false) { |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 5199 | // Change: A = select B, C, false --> A = and B, C |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5200 | return BinaryOperator::CreateAnd(CondVal, TrueVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 5201 | } else { |
| 5202 | // Change: A = select B, C, true --> A = or !B, C |
| 5203 | Value *NotCond = |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 5204 | InsertNewInstBefore(BinaryOperator::CreateNot(CondVal, |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 5205 | "not."+CondVal->getName()), SI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5206 | return BinaryOperator::CreateOr(NotCond, TrueVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 5207 | } |
| 5208 | } |
Chris Lattner | cfa5975 | 2007-11-25 21:27:53 +0000 | [diff] [blame] | 5209 | |
| 5210 | // select a, b, a -> a&b |
| 5211 | // select a, a, b -> a|b |
| 5212 | if (CondVal == TrueVal) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5213 | return BinaryOperator::CreateOr(CondVal, FalseVal); |
Chris Lattner | cfa5975 | 2007-11-25 21:27:53 +0000 | [diff] [blame] | 5214 | else if (CondVal == FalseVal) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5215 | return BinaryOperator::CreateAnd(CondVal, TrueVal); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5216 | } |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 5217 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 5218 | // Selecting between two integer constants? |
| 5219 | if (ConstantInt *TrueValC = dyn_cast<ConstantInt>(TrueVal)) |
| 5220 | if (ConstantInt *FalseValC = dyn_cast<ConstantInt>(FalseVal)) { |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 5221 | // select C, 1, 0 -> zext C to int |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 5222 | if (FalseValC->isZero() && TrueValC->getValue() == 1) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5223 | return CastInst::Create(Instruction::ZExt, CondVal, SI.getType()); |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 5224 | } else if (TrueValC->isZero() && FalseValC->getValue() == 1) { |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 5225 | // select C, 0, 1 -> zext !C to int |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 5226 | Value *NotCond = |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 5227 | InsertNewInstBefore(BinaryOperator::CreateNot(CondVal, |
Chris Lattner | 82e14fe | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 5228 | "not."+CondVal->getName()), SI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5229 | return CastInst::Create(Instruction::ZExt, NotCond, SI.getType()); |
Chris Lattner | 82e14fe | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 5230 | } |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 5231 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5232 | if (ICmpInst *IC = dyn_cast<ICmpInst>(SI.getCondition())) { |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 5233 | // If one of the constants is zero (we know they can't both be) and we |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 5234 | // have an icmp instruction with zero, and we have an 'and' with the |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 5235 | // non-constant value, eliminate this whole mess. This corresponds to |
| 5236 | // cases like this: ((X & 27) ? 27 : 0) |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 5237 | if (TrueValC->isZero() || FalseValC->isZero()) |
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 5238 | if (IC->isEquality() && isa<ConstantInt>(IC->getOperand(1)) && |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 5239 | cast<Constant>(IC->getOperand(1))->isNullValue()) |
| 5240 | if (Instruction *ICA = dyn_cast<Instruction>(IC->getOperand(0))) |
| 5241 | if (ICA->getOpcode() == Instruction::And && |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5242 | isa<ConstantInt>(ICA->getOperand(1)) && |
| 5243 | (ICA->getOperand(1) == TrueValC || |
| 5244 | ICA->getOperand(1) == FalseValC) && |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 5245 | isOneBitSet(cast<ConstantInt>(ICA->getOperand(1)))) { |
| 5246 | // Okay, now we know that everything is set up, we just don't |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5247 | // know whether we have a icmp_ne or icmp_eq and whether the |
| 5248 | // true or false val is the zero. |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 5249 | bool ShouldNotVal = !TrueValC->isZero(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5250 | ShouldNotVal ^= IC->getPredicate() == ICmpInst::ICMP_NE; |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 5251 | Value *V = ICA; |
| 5252 | if (ShouldNotVal) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5253 | V = InsertNewInstBefore(BinaryOperator::Create( |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 5254 | Instruction::Xor, V, ICA->getOperand(1)), SI); |
| 5255 | return ReplaceInstUsesWith(SI, V); |
| 5256 | } |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 5257 | } |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 5258 | } |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 5259 | |
| 5260 | // See if we are selecting two values based on a comparison of the two values. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5261 | if (FCmpInst *FCI = dyn_cast<FCmpInst>(CondVal)) { |
| 5262 | if (FCI->getOperand(0) == TrueVal && FCI->getOperand(1) == FalseVal) { |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 5263 | // Transform (X == Y) ? X : Y -> Y |
Dale Johannesen | 5a2174f | 2007-10-03 17:45:27 +0000 | [diff] [blame] | 5264 | if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) { |
| 5265 | // This is not safe in general for floating point: |
| 5266 | // consider X== -0, Y== +0. |
| 5267 | // It becomes safe if either operand is a nonzero constant. |
| 5268 | ConstantFP *CFPt, *CFPf; |
| 5269 | if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) && |
| 5270 | !CFPt->getValueAPF().isZero()) || |
| 5271 | ((CFPf = dyn_cast<ConstantFP>(FalseVal)) && |
| 5272 | !CFPf->getValueAPF().isZero())) |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 5273 | return ReplaceInstUsesWith(SI, FalseVal); |
Dale Johannesen | 5a2174f | 2007-10-03 17:45:27 +0000 | [diff] [blame] | 5274 | } |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 5275 | // Transform (X != Y) ? X : Y -> X |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5276 | if (FCI->getPredicate() == FCmpInst::FCMP_ONE) |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 5277 | return ReplaceInstUsesWith(SI, TrueVal); |
Dan Gohman | 81b28ce | 2008-09-16 18:46:06 +0000 | [diff] [blame] | 5278 | // NOTE: if we wanted to, this is where to detect MIN/MAX |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 5279 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5280 | } else if (FCI->getOperand(0) == FalseVal && FCI->getOperand(1) == TrueVal){ |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 5281 | // Transform (X == Y) ? Y : X -> X |
Dale Johannesen | 5a2174f | 2007-10-03 17:45:27 +0000 | [diff] [blame] | 5282 | if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) { |
| 5283 | // This is not safe in general for floating point: |
| 5284 | // consider X== -0, Y== +0. |
| 5285 | // It becomes safe if either operand is a nonzero constant. |
| 5286 | ConstantFP *CFPt, *CFPf; |
| 5287 | if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) && |
| 5288 | !CFPt->getValueAPF().isZero()) || |
| 5289 | ((CFPf = dyn_cast<ConstantFP>(FalseVal)) && |
| 5290 | !CFPf->getValueAPF().isZero())) |
| 5291 | return ReplaceInstUsesWith(SI, FalseVal); |
| 5292 | } |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 5293 | // Transform (X != Y) ? Y : X -> Y |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5294 | if (FCI->getPredicate() == FCmpInst::FCMP_ONE) |
| 5295 | return ReplaceInstUsesWith(SI, TrueVal); |
Dan Gohman | 81b28ce | 2008-09-16 18:46:06 +0000 | [diff] [blame] | 5296 | // NOTE: if we wanted to, this is where to detect MIN/MAX |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5297 | } |
Dan Gohman | 81b28ce | 2008-09-16 18:46:06 +0000 | [diff] [blame] | 5298 | // NOTE: if we wanted to, this is where to detect ABS |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5299 | } |
| 5300 | |
| 5301 | // See if we are selecting two values based on a comparison of the two values. |
Dan Gohman | 81b28ce | 2008-09-16 18:46:06 +0000 | [diff] [blame] | 5302 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(CondVal)) |
| 5303 | if (Instruction *Result = visitSelectInstWithICmp(SI, ICI)) |
| 5304 | return Result; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5305 | |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 5306 | if (Instruction *TI = dyn_cast<Instruction>(TrueVal)) |
| 5307 | if (Instruction *FI = dyn_cast<Instruction>(FalseVal)) |
| 5308 | if (TI->hasOneUse() && FI->hasOneUse()) { |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 5309 | Instruction *AddOp = 0, *SubOp = 0; |
| 5310 | |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 5311 | // Turn (select C, (op X, Y), (op X, Z)) -> (op X, (select C, Y, Z)) |
| 5312 | if (TI->getOpcode() == FI->getOpcode()) |
| 5313 | if (Instruction *IV = FoldSelectOpOp(SI, TI, FI)) |
| 5314 | return IV; |
| 5315 | |
| 5316 | // Turn select C, (X+Y), (X-Y) --> (X+(select C, Y, (-Y))). This is |
| 5317 | // even legal for FP. |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 5318 | if ((TI->getOpcode() == Instruction::Sub && |
| 5319 | FI->getOpcode() == Instruction::Add) || |
| 5320 | (TI->getOpcode() == Instruction::FSub && |
| 5321 | FI->getOpcode() == Instruction::FAdd)) { |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 5322 | AddOp = FI; SubOp = TI; |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 5323 | } else if ((FI->getOpcode() == Instruction::Sub && |
| 5324 | TI->getOpcode() == Instruction::Add) || |
| 5325 | (FI->getOpcode() == Instruction::FSub && |
| 5326 | TI->getOpcode() == Instruction::FAdd)) { |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 5327 | AddOp = TI; SubOp = FI; |
| 5328 | } |
| 5329 | |
| 5330 | if (AddOp) { |
| 5331 | Value *OtherAddOp = 0; |
| 5332 | if (SubOp->getOperand(0) == AddOp->getOperand(0)) { |
| 5333 | OtherAddOp = AddOp->getOperand(1); |
| 5334 | } else if (SubOp->getOperand(0) == AddOp->getOperand(1)) { |
| 5335 | OtherAddOp = AddOp->getOperand(0); |
| 5336 | } |
| 5337 | |
| 5338 | if (OtherAddOp) { |
Chris Lattner | 97f37a4 | 2006-02-24 18:05:58 +0000 | [diff] [blame] | 5339 | // So at this point we know we have (Y -> OtherAddOp): |
| 5340 | // select C, (add X, Y), (sub X, Z) |
| 5341 | Value *NegVal; // Compute -Z |
| 5342 | if (Constant *C = dyn_cast<Constant>(SubOp->getOperand(1))) { |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 5343 | NegVal = ConstantExpr::getNeg(C); |
Chris Lattner | 97f37a4 | 2006-02-24 18:05:58 +0000 | [diff] [blame] | 5344 | } else { |
| 5345 | NegVal = InsertNewInstBefore( |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 5346 | BinaryOperator::CreateNeg(SubOp->getOperand(1), |
Owen Anderson | 0a5372e | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 5347 | "tmp"), SI); |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 5348 | } |
Chris Lattner | 97f37a4 | 2006-02-24 18:05:58 +0000 | [diff] [blame] | 5349 | |
| 5350 | Value *NewTrueOp = OtherAddOp; |
| 5351 | Value *NewFalseOp = NegVal; |
| 5352 | if (AddOp != TI) |
| 5353 | std::swap(NewTrueOp, NewFalseOp); |
| 5354 | Instruction *NewSel = |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 5355 | SelectInst::Create(CondVal, NewTrueOp, |
| 5356 | NewFalseOp, SI.getName() + ".p"); |
Chris Lattner | 97f37a4 | 2006-02-24 18:05:58 +0000 | [diff] [blame] | 5357 | |
| 5358 | NewSel = InsertNewInstBefore(NewSel, SI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5359 | return BinaryOperator::CreateAdd(SubOp->getOperand(0), NewSel); |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 5360 | } |
| 5361 | } |
| 5362 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5363 | |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 5364 | // See if we can fold the select into one of our operands. |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 5365 | if (SI.getType()->isInteger()) { |
Chris Lattner | b109b5c | 2009-12-21 06:03:05 +0000 | [diff] [blame] | 5366 | if (Instruction *FoldI = FoldSelectIntoOp(SI, TrueVal, FalseVal)) |
Evan Cheng | de62192 | 2009-03-31 20:42:45 +0000 | [diff] [blame] | 5367 | return FoldI; |
Chris Lattner | b109b5c | 2009-12-21 06:03:05 +0000 | [diff] [blame] | 5368 | |
| 5369 | // MAX(MAX(a, b), a) -> MAX(a, b) |
| 5370 | // MIN(MIN(a, b), a) -> MIN(a, b) |
| 5371 | // MAX(MIN(a, b), a) -> a |
| 5372 | // MIN(MAX(a, b), a) -> a |
| 5373 | Value *LHS, *RHS, *LHS2, *RHS2; |
| 5374 | if (SelectPatternFlavor SPF = MatchSelectPattern(&SI, LHS, RHS)) { |
| 5375 | if (SelectPatternFlavor SPF2 = MatchSelectPattern(LHS, LHS2, RHS2)) |
| 5376 | if (Instruction *R = FoldSPFofSPF(cast<Instruction>(LHS),SPF2,LHS2,RHS2, |
| 5377 | SI, SPF, RHS)) |
| 5378 | return R; |
| 5379 | if (SelectPatternFlavor SPF2 = MatchSelectPattern(RHS, LHS2, RHS2)) |
| 5380 | if (Instruction *R = FoldSPFofSPF(cast<Instruction>(RHS),SPF2,LHS2,RHS2, |
| 5381 | SI, SPF, LHS)) |
| 5382 | return R; |
| 5383 | } |
| 5384 | |
| 5385 | // TODO. |
| 5386 | // ABS(-X) -> ABS(X) |
| 5387 | // ABS(ABS(X)) -> ABS(X) |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 5388 | } |
Chris Lattner | a1df33c | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 5389 | |
Chris Lattner | 7f23958 | 2009-10-22 00:17:26 +0000 | [diff] [blame] | 5390 | // See if we can fold the select into a phi node if the condition is a select. |
| 5391 | if (isa<PHINode>(SI.getCondition())) |
| 5392 | // The true/false values have to be live in the PHI predecessor's blocks. |
| 5393 | if (CanSelectOperandBeMappingIntoPredBlock(TrueVal, SI) && |
| 5394 | CanSelectOperandBeMappingIntoPredBlock(FalseVal, SI)) |
| 5395 | if (Instruction *NV = FoldOpIntoPhi(SI)) |
| 5396 | return NV; |
Chris Lattner | 5d1704d | 2009-09-27 19:57:57 +0000 | [diff] [blame] | 5397 | |
Chris Lattner | a1df33c | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 5398 | if (BinaryOperator::isNot(CondVal)) { |
| 5399 | SI.setOperand(0, BinaryOperator::getNotArgument(CondVal)); |
| 5400 | SI.setOperand(1, FalseVal); |
| 5401 | SI.setOperand(2, TrueVal); |
| 5402 | return &SI; |
| 5403 | } |
| 5404 | |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 5405 | return 0; |
| 5406 | } |
| 5407 | |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 5408 | /// EnforceKnownAlignment - If the specified pointer points to an object that |
| 5409 | /// we control, modify the object's alignment to PrefAlign. This isn't |
| 5410 | /// often possible though. If alignment is important, a more reliable approach |
| 5411 | /// is to simply align all global variables and allocation instructions to |
| 5412 | /// their preferred alignment from the beginning. |
| 5413 | /// |
| 5414 | static unsigned EnforceKnownAlignment(Value *V, |
| 5415 | unsigned Align, unsigned PrefAlign) { |
Chris Lattner | f2369f2 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 5416 | |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 5417 | User *U = dyn_cast<User>(V); |
| 5418 | if (!U) return Align; |
| 5419 | |
Dan Gohman | ca17890 | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 5420 | switch (Operator::getOpcode(U)) { |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 5421 | default: break; |
| 5422 | case Instruction::BitCast: |
| 5423 | return EnforceKnownAlignment(U->getOperand(0), Align, PrefAlign); |
| 5424 | case Instruction::GetElementPtr: { |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 5425 | // If all indexes are zero, it is just the alignment of the base pointer. |
| 5426 | bool AllZeroOperands = true; |
Gabor Greif | 52ed363 | 2008-06-12 21:51:29 +0000 | [diff] [blame] | 5427 | for (User::op_iterator i = U->op_begin() + 1, e = U->op_end(); i != e; ++i) |
Gabor Greif | 177dd3f | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 5428 | if (!isa<Constant>(*i) || |
| 5429 | !cast<Constant>(*i)->isNullValue()) { |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 5430 | AllZeroOperands = false; |
| 5431 | break; |
| 5432 | } |
Chris Lattner | f2369f2 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 5433 | |
| 5434 | if (AllZeroOperands) { |
| 5435 | // Treat this like a bitcast. |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 5436 | return EnforceKnownAlignment(U->getOperand(0), Align, PrefAlign); |
Chris Lattner | f2369f2 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 5437 | } |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 5438 | break; |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 5439 | } |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 5440 | } |
| 5441 | |
| 5442 | if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
| 5443 | // If there is a large requested alignment and we can, bump up the alignment |
| 5444 | // of the global. |
| 5445 | if (!GV->isDeclaration()) { |
Dan Gohman | ecd0fb5 | 2009-02-16 23:02:21 +0000 | [diff] [blame] | 5446 | if (GV->getAlignment() >= PrefAlign) |
| 5447 | Align = GV->getAlignment(); |
| 5448 | else { |
| 5449 | GV->setAlignment(PrefAlign); |
| 5450 | Align = PrefAlign; |
| 5451 | } |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 5452 | } |
Chris Lattner | 42ebefa | 2009-09-27 21:42:46 +0000 | [diff] [blame] | 5453 | } else if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) { |
| 5454 | // If there is a requested alignment and if this is an alloca, round up. |
| 5455 | if (AI->getAlignment() >= PrefAlign) |
| 5456 | Align = AI->getAlignment(); |
| 5457 | else { |
| 5458 | AI->setAlignment(PrefAlign); |
| 5459 | Align = PrefAlign; |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 5460 | } |
| 5461 | } |
| 5462 | |
| 5463 | return Align; |
| 5464 | } |
| 5465 | |
| 5466 | /// GetOrEnforceKnownAlignment - If the specified pointer has an alignment that |
| 5467 | /// we can determine, return it, otherwise return 0. If PrefAlign is specified, |
| 5468 | /// and it is more than the alignment of the ultimate object, see if we can |
| 5469 | /// increase the alignment of the ultimate object, making this check succeed. |
| 5470 | unsigned InstCombiner::GetOrEnforceKnownAlignment(Value *V, |
| 5471 | unsigned PrefAlign) { |
| 5472 | unsigned BitWidth = TD ? TD->getTypeSizeInBits(V->getType()) : |
| 5473 | sizeof(PrefAlign) * CHAR_BIT; |
| 5474 | APInt Mask = APInt::getAllOnesValue(BitWidth); |
| 5475 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 5476 | ComputeMaskedBits(V, Mask, KnownZero, KnownOne); |
| 5477 | unsigned TrailZ = KnownZero.countTrailingOnes(); |
| 5478 | unsigned Align = 1u << std::min(BitWidth - 1, TrailZ); |
| 5479 | |
| 5480 | if (PrefAlign > Align) |
| 5481 | Align = EnforceKnownAlignment(V, Align, PrefAlign); |
| 5482 | |
| 5483 | // We don't need to make any adjustment. |
| 5484 | return Align; |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 5485 | } |
| 5486 | |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 5487 | Instruction *InstCombiner::SimplifyMemTransfer(MemIntrinsic *MI) { |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 5488 | unsigned DstAlign = GetOrEnforceKnownAlignment(MI->getOperand(1)); |
Dan Gohman | bc989d4 | 2009-02-22 18:06:32 +0000 | [diff] [blame] | 5489 | unsigned SrcAlign = GetOrEnforceKnownAlignment(MI->getOperand(2)); |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 5490 | unsigned MinAlign = std::min(DstAlign, SrcAlign); |
Chris Lattner | dfe964c | 2009-03-08 03:59:00 +0000 | [diff] [blame] | 5491 | unsigned CopyAlign = MI->getAlignment(); |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 5492 | |
| 5493 | if (CopyAlign < MinAlign) { |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 5494 | MI->setAlignment(ConstantInt::get(MI->getAlignmentType(), |
Owen Anderson | a547b47 | 2009-07-09 18:36:20 +0000 | [diff] [blame] | 5495 | MinAlign, false)); |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 5496 | return MI; |
| 5497 | } |
| 5498 | |
| 5499 | // If MemCpyInst length is 1/2/4/8 bytes then replace memcpy with |
| 5500 | // load/store. |
| 5501 | ConstantInt *MemOpLength = dyn_cast<ConstantInt>(MI->getOperand(3)); |
| 5502 | if (MemOpLength == 0) return 0; |
| 5503 | |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 5504 | // Source and destination pointer types are always "i8*" for intrinsic. See |
| 5505 | // if the size is something we can handle with a single primitive load/store. |
| 5506 | // A single load+store correctly handles overlapping memory in the memmove |
| 5507 | // case. |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 5508 | unsigned Size = MemOpLength->getZExtValue(); |
Chris Lattner | 69ea9d2 | 2008-04-30 06:39:11 +0000 | [diff] [blame] | 5509 | if (Size == 0) return MI; // Delete this mem transfer. |
| 5510 | |
| 5511 | if (Size > 8 || (Size&(Size-1))) |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 5512 | return 0; // If not 1/2/4/8 bytes, exit. |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 5513 | |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 5514 | // Use an integer load+store unless we can find something better. |
Owen Anderson | d672ecb | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 5515 | Type *NewPtrTy = |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 5516 | PointerType::getUnqual(IntegerType::get(MI->getContext(), Size<<3)); |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 5517 | |
| 5518 | // Memcpy forces the use of i8* for the source and destination. That means |
| 5519 | // that if you're using memcpy to move one double around, you'll get a cast |
| 5520 | // from double* to i8*. We'd much rather use a double load+store rather than |
| 5521 | // an i64 load+store, here because this improves the odds that the source or |
| 5522 | // dest address will be promotable. See if we can find a better type than the |
| 5523 | // integer datatype. |
| 5524 | if (Value *Op = getBitCastOperand(MI->getOperand(1))) { |
| 5525 | const Type *SrcETy = cast<PointerType>(Op->getType())->getElementType(); |
Dan Gohman | ce9fe9f | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 5526 | if (TD && SrcETy->isSized() && TD->getTypeStoreSize(SrcETy) == Size) { |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 5527 | // The SrcETy might be something like {{{double}}} or [1 x double]. Rip |
| 5528 | // down through these levels if so. |
Dan Gohman | 8f8e269 | 2008-05-23 01:52:21 +0000 | [diff] [blame] | 5529 | while (!SrcETy->isSingleValueType()) { |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 5530 | if (const StructType *STy = dyn_cast<StructType>(SrcETy)) { |
| 5531 | if (STy->getNumElements() == 1) |
| 5532 | SrcETy = STy->getElementType(0); |
| 5533 | else |
| 5534 | break; |
| 5535 | } else if (const ArrayType *ATy = dyn_cast<ArrayType>(SrcETy)) { |
| 5536 | if (ATy->getNumElements() == 1) |
| 5537 | SrcETy = ATy->getElementType(); |
| 5538 | else |
| 5539 | break; |
| 5540 | } else |
| 5541 | break; |
| 5542 | } |
| 5543 | |
Dan Gohman | 8f8e269 | 2008-05-23 01:52:21 +0000 | [diff] [blame] | 5544 | if (SrcETy->isSingleValueType()) |
Owen Anderson | debcb01 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 5545 | NewPtrTy = PointerType::getUnqual(SrcETy); |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 5546 | } |
| 5547 | } |
| 5548 | |
| 5549 | |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 5550 | // If the memcpy/memmove provides better alignment info than we can |
| 5551 | // infer, use it. |
| 5552 | SrcAlign = std::max(SrcAlign, CopyAlign); |
| 5553 | DstAlign = std::max(DstAlign, CopyAlign); |
| 5554 | |
Chris Lattner | 08142f2 | 2009-08-30 19:47:22 +0000 | [diff] [blame] | 5555 | Value *Src = Builder->CreateBitCast(MI->getOperand(2), NewPtrTy); |
| 5556 | Value *Dest = Builder->CreateBitCast(MI->getOperand(1), NewPtrTy); |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 5557 | Instruction *L = new LoadInst(Src, "tmp", false, SrcAlign); |
| 5558 | InsertNewInstBefore(L, *MI); |
| 5559 | InsertNewInstBefore(new StoreInst(L, Dest, false, DstAlign), *MI); |
| 5560 | |
| 5561 | // Set the size of the copy to 0, it will be deleted on the next iteration. |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 5562 | MI->setOperand(3, Constant::getNullValue(MemOpLength->getType())); |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 5563 | return MI; |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 5564 | } |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 5565 | |
Chris Lattner | 69ea9d2 | 2008-04-30 06:39:11 +0000 | [diff] [blame] | 5566 | Instruction *InstCombiner::SimplifyMemSet(MemSetInst *MI) { |
| 5567 | unsigned Alignment = GetOrEnforceKnownAlignment(MI->getDest()); |
Chris Lattner | dfe964c | 2009-03-08 03:59:00 +0000 | [diff] [blame] | 5568 | if (MI->getAlignment() < Alignment) { |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 5569 | MI->setAlignment(ConstantInt::get(MI->getAlignmentType(), |
Owen Anderson | a547b47 | 2009-07-09 18:36:20 +0000 | [diff] [blame] | 5570 | Alignment, false)); |
Chris Lattner | 69ea9d2 | 2008-04-30 06:39:11 +0000 | [diff] [blame] | 5571 | return MI; |
| 5572 | } |
| 5573 | |
| 5574 | // Extract the length and alignment and fill if they are constant. |
| 5575 | ConstantInt *LenC = dyn_cast<ConstantInt>(MI->getLength()); |
| 5576 | ConstantInt *FillC = dyn_cast<ConstantInt>(MI->getValue()); |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 5577 | if (!LenC || !FillC || FillC->getType() != Type::getInt8Ty(MI->getContext())) |
Chris Lattner | 69ea9d2 | 2008-04-30 06:39:11 +0000 | [diff] [blame] | 5578 | return 0; |
| 5579 | uint64_t Len = LenC->getZExtValue(); |
Chris Lattner | dfe964c | 2009-03-08 03:59:00 +0000 | [diff] [blame] | 5580 | Alignment = MI->getAlignment(); |
Chris Lattner | 69ea9d2 | 2008-04-30 06:39:11 +0000 | [diff] [blame] | 5581 | |
| 5582 | // If the length is zero, this is a no-op |
| 5583 | if (Len == 0) return MI; // memset(d,c,0,a) -> noop |
| 5584 | |
| 5585 | // memset(s,c,n) -> store s, c (for n=1,2,4,8) |
| 5586 | if (Len <= 8 && isPowerOf2_32((uint32_t)Len)) { |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 5587 | const Type *ITy = IntegerType::get(MI->getContext(), Len*8); // n=1 -> i8. |
Chris Lattner | 69ea9d2 | 2008-04-30 06:39:11 +0000 | [diff] [blame] | 5588 | |
| 5589 | Value *Dest = MI->getDest(); |
Chris Lattner | 08142f2 | 2009-08-30 19:47:22 +0000 | [diff] [blame] | 5590 | Dest = Builder->CreateBitCast(Dest, PointerType::getUnqual(ITy)); |
Chris Lattner | 69ea9d2 | 2008-04-30 06:39:11 +0000 | [diff] [blame] | 5591 | |
| 5592 | // Alignment 0 is identity for alignment 1 for memset, but not store. |
| 5593 | if (Alignment == 0) Alignment = 1; |
| 5594 | |
| 5595 | // Extract the fill value and store. |
| 5596 | uint64_t Fill = FillC->getZExtValue()*0x0101010101010101ULL; |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 5597 | InsertNewInstBefore(new StoreInst(ConstantInt::get(ITy, Fill), |
Owen Anderson | d672ecb | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 5598 | Dest, false, Alignment), *MI); |
Chris Lattner | 69ea9d2 | 2008-04-30 06:39:11 +0000 | [diff] [blame] | 5599 | |
| 5600 | // Set the size of the copy to 0, it will be deleted on the next iteration. |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 5601 | MI->setLength(Constant::getNullValue(LenC->getType())); |
Chris Lattner | 69ea9d2 | 2008-04-30 06:39:11 +0000 | [diff] [blame] | 5602 | return MI; |
| 5603 | } |
| 5604 | |
| 5605 | return 0; |
| 5606 | } |
| 5607 | |
| 5608 | |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 5609 | /// visitCallInst - CallInst simplification. This mostly only handles folding |
| 5610 | /// of intrinsic instructions. For normal calls, it allows visitCallSite to do |
| 5611 | /// the heavy lifting. |
| 5612 | /// |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 5613 | Instruction *InstCombiner::visitCallInst(CallInst &CI) { |
Victor Hernandez | 66284e0 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 5614 | if (isFreeCall(&CI)) |
| 5615 | return visitFree(CI); |
| 5616 | |
Chris Lattner | aab6ec4 | 2009-05-13 17:39:14 +0000 | [diff] [blame] | 5617 | // If the caller function is nounwind, mark the call as nounwind, even if the |
| 5618 | // callee isn't. |
| 5619 | if (CI.getParent()->getParent()->doesNotThrow() && |
| 5620 | !CI.doesNotThrow()) { |
| 5621 | CI.setDoesNotThrow(); |
| 5622 | return &CI; |
| 5623 | } |
| 5624 | |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 5625 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(&CI); |
| 5626 | if (!II) return visitCallSite(&CI); |
| 5627 | |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 5628 | // Intrinsics cannot occur in an invoke, so handle them here instead of in |
| 5629 | // visitCallSite. |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 5630 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(II)) { |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 5631 | bool Changed = false; |
| 5632 | |
| 5633 | // memmove/cpy/set of zero bytes is a noop. |
| 5634 | if (Constant *NumBytes = dyn_cast<Constant>(MI->getLength())) { |
| 5635 | if (NumBytes->isNullValue()) return EraseInstFromFunction(CI); |
| 5636 | |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 5637 | if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes)) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5638 | if (CI->getZExtValue() == 1) { |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 5639 | // Replace the instruction with just byte operations. We would |
| 5640 | // transform other cases to loads/stores, but we don't know if |
| 5641 | // alignment is sufficient. |
| 5642 | } |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 5643 | } |
| 5644 | |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 5645 | // If we have a memmove and the source operation is a constant global, |
| 5646 | // then the source and dest pointers can't alias, so we can change this |
| 5647 | // into a call to memcpy. |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 5648 | if (MemMoveInst *MMI = dyn_cast<MemMoveInst>(MI)) { |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 5649 | if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource())) |
| 5650 | if (GVSrc->isConstant()) { |
| 5651 | Module *M = CI.getParent()->getParent()->getParent(); |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 5652 | Intrinsic::ID MemCpyID = Intrinsic::memcpy; |
| 5653 | const Type *Tys[1]; |
| 5654 | Tys[0] = CI.getOperand(3)->getType(); |
| 5655 | CI.setOperand(0, |
| 5656 | Intrinsic::getDeclaration(M, MemCpyID, Tys, 1)); |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 5657 | Changed = true; |
| 5658 | } |
Eli Friedman | 0c826d9 | 2009-12-17 21:07:31 +0000 | [diff] [blame] | 5659 | } |
Chris Lattner | a935db8 | 2008-05-28 05:30:41 +0000 | [diff] [blame] | 5660 | |
Eli Friedman | 0c826d9 | 2009-12-17 21:07:31 +0000 | [diff] [blame] | 5661 | if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI)) { |
Chris Lattner | a935db8 | 2008-05-28 05:30:41 +0000 | [diff] [blame] | 5662 | // memmove(x,x,size) -> noop. |
Eli Friedman | 0c826d9 | 2009-12-17 21:07:31 +0000 | [diff] [blame] | 5663 | if (MTI->getSource() == MTI->getDest()) |
Chris Lattner | a935db8 | 2008-05-28 05:30:41 +0000 | [diff] [blame] | 5664 | return EraseInstFromFunction(CI); |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 5665 | } |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 5666 | |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 5667 | // If we can determine a pointer alignment that is bigger than currently |
| 5668 | // set, update the alignment. |
Chris Lattner | 3ce5e88 | 2009-03-08 03:37:16 +0000 | [diff] [blame] | 5669 | if (isa<MemTransferInst>(MI)) { |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 5670 | if (Instruction *I = SimplifyMemTransfer(MI)) |
| 5671 | return I; |
Chris Lattner | 69ea9d2 | 2008-04-30 06:39:11 +0000 | [diff] [blame] | 5672 | } else if (MemSetInst *MSI = dyn_cast<MemSetInst>(MI)) { |
| 5673 | if (Instruction *I = SimplifyMemSet(MSI)) |
| 5674 | return I; |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 5675 | } |
| 5676 | |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 5677 | if (Changed) return II; |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 5678 | } |
| 5679 | |
| 5680 | switch (II->getIntrinsicID()) { |
| 5681 | default: break; |
| 5682 | case Intrinsic::bswap: |
| 5683 | // bswap(bswap(x)) -> x |
| 5684 | if (IntrinsicInst *Operand = dyn_cast<IntrinsicInst>(II->getOperand(1))) |
| 5685 | if (Operand->getIntrinsicID() == Intrinsic::bswap) |
| 5686 | return ReplaceInstUsesWith(CI, Operand->getOperand(1)); |
Chris Lattner | e33d413 | 2010-01-01 18:34:40 +0000 | [diff] [blame] | 5687 | |
| 5688 | // bswap(trunc(bswap(x))) -> trunc(lshr(x, c)) |
| 5689 | if (TruncInst *TI = dyn_cast<TruncInst>(II->getOperand(1))) { |
| 5690 | if (IntrinsicInst *Operand = dyn_cast<IntrinsicInst>(TI->getOperand(0))) |
| 5691 | if (Operand->getIntrinsicID() == Intrinsic::bswap) { |
| 5692 | unsigned C = Operand->getType()->getPrimitiveSizeInBits() - |
| 5693 | TI->getType()->getPrimitiveSizeInBits(); |
| 5694 | Value *CV = ConstantInt::get(Operand->getType(), C); |
| 5695 | Value *V = Builder->CreateLShr(Operand->getOperand(1), CV); |
| 5696 | return new TruncInst(V, TI->getType()); |
| 5697 | } |
| 5698 | } |
| 5699 | |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 5700 | break; |
Chris Lattner | d27f911 | 2010-01-01 01:52:15 +0000 | [diff] [blame] | 5701 | case Intrinsic::powi: |
| 5702 | if (ConstantInt *Power = dyn_cast<ConstantInt>(II->getOperand(2))) { |
| 5703 | // powi(x, 0) -> 1.0 |
| 5704 | if (Power->isZero()) |
| 5705 | return ReplaceInstUsesWith(CI, ConstantFP::get(CI.getType(), 1.0)); |
| 5706 | // powi(x, 1) -> x |
| 5707 | if (Power->isOne()) |
| 5708 | return ReplaceInstUsesWith(CI, II->getOperand(1)); |
| 5709 | // powi(x, -1) -> 1/x |
Chris Lattner | f9ead87 | 2010-01-01 01:54:08 +0000 | [diff] [blame] | 5710 | if (Power->isAllOnesValue()) |
| 5711 | return BinaryOperator::CreateFDiv(ConstantFP::get(CI.getType(), 1.0), |
| 5712 | II->getOperand(1)); |
Chris Lattner | d27f911 | 2010-01-01 01:52:15 +0000 | [diff] [blame] | 5713 | } |
| 5714 | break; |
| 5715 | |
Chris Lattner | 2bbac75 | 2009-11-26 21:42:47 +0000 | [diff] [blame] | 5716 | case Intrinsic::uadd_with_overflow: { |
| 5717 | Value *LHS = II->getOperand(1), *RHS = II->getOperand(2); |
| 5718 | const IntegerType *IT = cast<IntegerType>(II->getOperand(1)->getType()); |
| 5719 | uint32_t BitWidth = IT->getBitWidth(); |
| 5720 | APInt Mask = APInt::getSignBit(BitWidth); |
Chris Lattner | 998e25a | 2009-11-26 22:08:06 +0000 | [diff] [blame] | 5721 | APInt LHSKnownZero(BitWidth, 0); |
| 5722 | APInt LHSKnownOne(BitWidth, 0); |
Chris Lattner | 2bbac75 | 2009-11-26 21:42:47 +0000 | [diff] [blame] | 5723 | ComputeMaskedBits(LHS, Mask, LHSKnownZero, LHSKnownOne); |
| 5724 | bool LHSKnownNegative = LHSKnownOne[BitWidth - 1]; |
| 5725 | bool LHSKnownPositive = LHSKnownZero[BitWidth - 1]; |
| 5726 | |
| 5727 | if (LHSKnownNegative || LHSKnownPositive) { |
Chris Lattner | 998e25a | 2009-11-26 22:08:06 +0000 | [diff] [blame] | 5728 | APInt RHSKnownZero(BitWidth, 0); |
| 5729 | APInt RHSKnownOne(BitWidth, 0); |
Chris Lattner | 2bbac75 | 2009-11-26 21:42:47 +0000 | [diff] [blame] | 5730 | ComputeMaskedBits(RHS, Mask, RHSKnownZero, RHSKnownOne); |
| 5731 | bool RHSKnownNegative = RHSKnownOne[BitWidth - 1]; |
| 5732 | bool RHSKnownPositive = RHSKnownZero[BitWidth - 1]; |
| 5733 | if (LHSKnownNegative && RHSKnownNegative) { |
| 5734 | // The sign bit is set in both cases: this MUST overflow. |
| 5735 | // Create a simple add instruction, and insert it into the struct. |
| 5736 | Instruction *Add = BinaryOperator::CreateAdd(LHS, RHS, "", &CI); |
| 5737 | Worklist.Add(Add); |
Chris Lattner | cd188e9 | 2009-11-29 02:57:29 +0000 | [diff] [blame] | 5738 | Constant *V[] = { |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 5739 | UndefValue::get(LHS->getType()),ConstantInt::getTrue(II->getContext()) |
Chris Lattner | cd188e9 | 2009-11-29 02:57:29 +0000 | [diff] [blame] | 5740 | }; |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 5741 | Constant *Struct = ConstantStruct::get(II->getContext(), V, 2, false); |
Chris Lattner | 2bbac75 | 2009-11-26 21:42:47 +0000 | [diff] [blame] | 5742 | return InsertValueInst::Create(Struct, Add, 0); |
| 5743 | } |
| 5744 | |
| 5745 | if (LHSKnownPositive && RHSKnownPositive) { |
| 5746 | // The sign bit is clear in both cases: this CANNOT overflow. |
| 5747 | // Create a simple add instruction, and insert it into the struct. |
| 5748 | Instruction *Add = BinaryOperator::CreateNUWAdd(LHS, RHS, "", &CI); |
| 5749 | Worklist.Add(Add); |
Chris Lattner | cd188e9 | 2009-11-29 02:57:29 +0000 | [diff] [blame] | 5750 | Constant *V[] = { |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 5751 | UndefValue::get(LHS->getType()), |
| 5752 | ConstantInt::getFalse(II->getContext()) |
Chris Lattner | cd188e9 | 2009-11-29 02:57:29 +0000 | [diff] [blame] | 5753 | }; |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 5754 | Constant *Struct = ConstantStruct::get(II->getContext(), V, 2, false); |
Chris Lattner | 2bbac75 | 2009-11-26 21:42:47 +0000 | [diff] [blame] | 5755 | return InsertValueInst::Create(Struct, Add, 0); |
| 5756 | } |
| 5757 | } |
| 5758 | } |
| 5759 | // FALL THROUGH uadd into sadd |
| 5760 | case Intrinsic::sadd_with_overflow: |
| 5761 | // Canonicalize constants into the RHS. |
| 5762 | if (isa<Constant>(II->getOperand(1)) && |
| 5763 | !isa<Constant>(II->getOperand(2))) { |
| 5764 | Value *LHS = II->getOperand(1); |
| 5765 | II->setOperand(1, II->getOperand(2)); |
| 5766 | II->setOperand(2, LHS); |
| 5767 | return II; |
| 5768 | } |
| 5769 | |
| 5770 | // X + undef -> undef |
| 5771 | if (isa<UndefValue>(II->getOperand(2))) |
| 5772 | return ReplaceInstUsesWith(CI, UndefValue::get(II->getType())); |
| 5773 | |
| 5774 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(II->getOperand(2))) { |
| 5775 | // X + 0 -> {X, false} |
| 5776 | if (RHS->isZero()) { |
| 5777 | Constant *V[] = { |
Chris Lattner | cd188e9 | 2009-11-29 02:57:29 +0000 | [diff] [blame] | 5778 | UndefValue::get(II->getOperand(0)->getType()), |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 5779 | ConstantInt::getFalse(II->getContext()) |
Chris Lattner | 2bbac75 | 2009-11-26 21:42:47 +0000 | [diff] [blame] | 5780 | }; |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 5781 | Constant *Struct = ConstantStruct::get(II->getContext(), V, 2, false); |
Chris Lattner | 2bbac75 | 2009-11-26 21:42:47 +0000 | [diff] [blame] | 5782 | return InsertValueInst::Create(Struct, II->getOperand(1), 0); |
| 5783 | } |
| 5784 | } |
| 5785 | break; |
| 5786 | case Intrinsic::usub_with_overflow: |
| 5787 | case Intrinsic::ssub_with_overflow: |
| 5788 | // undef - X -> undef |
| 5789 | // X - undef -> undef |
| 5790 | if (isa<UndefValue>(II->getOperand(1)) || |
| 5791 | isa<UndefValue>(II->getOperand(2))) |
| 5792 | return ReplaceInstUsesWith(CI, UndefValue::get(II->getType())); |
| 5793 | |
| 5794 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(II->getOperand(2))) { |
| 5795 | // X - 0 -> {X, false} |
| 5796 | if (RHS->isZero()) { |
| 5797 | Constant *V[] = { |
Chris Lattner | cd188e9 | 2009-11-29 02:57:29 +0000 | [diff] [blame] | 5798 | UndefValue::get(II->getOperand(1)->getType()), |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 5799 | ConstantInt::getFalse(II->getContext()) |
Chris Lattner | 2bbac75 | 2009-11-26 21:42:47 +0000 | [diff] [blame] | 5800 | }; |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 5801 | Constant *Struct = ConstantStruct::get(II->getContext(), V, 2, false); |
Chris Lattner | 2bbac75 | 2009-11-26 21:42:47 +0000 | [diff] [blame] | 5802 | return InsertValueInst::Create(Struct, II->getOperand(1), 0); |
| 5803 | } |
| 5804 | } |
| 5805 | break; |
| 5806 | case Intrinsic::umul_with_overflow: |
| 5807 | case Intrinsic::smul_with_overflow: |
| 5808 | // Canonicalize constants into the RHS. |
| 5809 | if (isa<Constant>(II->getOperand(1)) && |
| 5810 | !isa<Constant>(II->getOperand(2))) { |
| 5811 | Value *LHS = II->getOperand(1); |
| 5812 | II->setOperand(1, II->getOperand(2)); |
| 5813 | II->setOperand(2, LHS); |
| 5814 | return II; |
| 5815 | } |
| 5816 | |
| 5817 | // X * undef -> undef |
| 5818 | if (isa<UndefValue>(II->getOperand(2))) |
| 5819 | return ReplaceInstUsesWith(CI, UndefValue::get(II->getType())); |
| 5820 | |
| 5821 | if (ConstantInt *RHSI = dyn_cast<ConstantInt>(II->getOperand(2))) { |
| 5822 | // X*0 -> {0, false} |
| 5823 | if (RHSI->isZero()) |
| 5824 | return ReplaceInstUsesWith(CI, Constant::getNullValue(II->getType())); |
| 5825 | |
| 5826 | // X * 1 -> {X, false} |
| 5827 | if (RHSI->equalsInt(1)) { |
Chris Lattner | cd188e9 | 2009-11-29 02:57:29 +0000 | [diff] [blame] | 5828 | Constant *V[] = { |
| 5829 | UndefValue::get(II->getOperand(1)->getType()), |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 5830 | ConstantInt::getFalse(II->getContext()) |
Chris Lattner | cd188e9 | 2009-11-29 02:57:29 +0000 | [diff] [blame] | 5831 | }; |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 5832 | Constant *Struct = ConstantStruct::get(II->getContext(), V, 2, false); |
Chris Lattner | cd188e9 | 2009-11-29 02:57:29 +0000 | [diff] [blame] | 5833 | return InsertValueInst::Create(Struct, II->getOperand(1), 0); |
Chris Lattner | 2bbac75 | 2009-11-26 21:42:47 +0000 | [diff] [blame] | 5834 | } |
| 5835 | } |
| 5836 | break; |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 5837 | case Intrinsic::ppc_altivec_lvx: |
| 5838 | case Intrinsic::ppc_altivec_lvxl: |
| 5839 | case Intrinsic::x86_sse_loadu_ps: |
| 5840 | case Intrinsic::x86_sse2_loadu_pd: |
| 5841 | case Intrinsic::x86_sse2_loadu_dq: |
| 5842 | // Turn PPC lvx -> load if the pointer is known aligned. |
| 5843 | // Turn X86 loadups -> load if the pointer is known aligned. |
| 5844 | if (GetOrEnforceKnownAlignment(II->getOperand(1), 16) >= 16) { |
Chris Lattner | 08142f2 | 2009-08-30 19:47:22 +0000 | [diff] [blame] | 5845 | Value *Ptr = Builder->CreateBitCast(II->getOperand(1), |
| 5846 | PointerType::getUnqual(II->getType())); |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 5847 | return new LoadInst(Ptr); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 5848 | } |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 5849 | break; |
| 5850 | case Intrinsic::ppc_altivec_stvx: |
| 5851 | case Intrinsic::ppc_altivec_stvxl: |
| 5852 | // Turn stvx -> store if the pointer is known aligned. |
| 5853 | if (GetOrEnforceKnownAlignment(II->getOperand(2), 16) >= 16) { |
| 5854 | const Type *OpPtrTy = |
Owen Anderson | debcb01 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 5855 | PointerType::getUnqual(II->getOperand(1)->getType()); |
Chris Lattner | 08142f2 | 2009-08-30 19:47:22 +0000 | [diff] [blame] | 5856 | Value *Ptr = Builder->CreateBitCast(II->getOperand(2), OpPtrTy); |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 5857 | return new StoreInst(II->getOperand(1), Ptr); |
| 5858 | } |
| 5859 | break; |
| 5860 | case Intrinsic::x86_sse_storeu_ps: |
| 5861 | case Intrinsic::x86_sse2_storeu_pd: |
| 5862 | case Intrinsic::x86_sse2_storeu_dq: |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 5863 | // Turn X86 storeu -> store if the pointer is known aligned. |
| 5864 | if (GetOrEnforceKnownAlignment(II->getOperand(1), 16) >= 16) { |
| 5865 | const Type *OpPtrTy = |
Owen Anderson | debcb01 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 5866 | PointerType::getUnqual(II->getOperand(2)->getType()); |
Chris Lattner | 08142f2 | 2009-08-30 19:47:22 +0000 | [diff] [blame] | 5867 | Value *Ptr = Builder->CreateBitCast(II->getOperand(1), OpPtrTy); |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 5868 | return new StoreInst(II->getOperand(2), Ptr); |
| 5869 | } |
| 5870 | break; |
| 5871 | |
| 5872 | case Intrinsic::x86_sse_cvttss2si: { |
| 5873 | // These intrinsics only demands the 0th element of its input vector. If |
| 5874 | // we can simplify the input based on that, do so now. |
Evan Cheng | 388df62 | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 5875 | unsigned VWidth = |
| 5876 | cast<VectorType>(II->getOperand(1)->getType())->getNumElements(); |
| 5877 | APInt DemandedElts(VWidth, 1); |
| 5878 | APInt UndefElts(VWidth, 0); |
| 5879 | if (Value *V = SimplifyDemandedVectorElts(II->getOperand(1), DemandedElts, |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 5880 | UndefElts)) { |
| 5881 | II->setOperand(1, V); |
| 5882 | return II; |
| 5883 | } |
| 5884 | break; |
| 5885 | } |
| 5886 | |
| 5887 | case Intrinsic::ppc_altivec_vperm: |
| 5888 | // Turn vperm(V1,V2,mask) -> shuffle(V1,V2,mask) if mask is a constant. |
| 5889 | if (ConstantVector *Mask = dyn_cast<ConstantVector>(II->getOperand(3))) { |
| 5890 | assert(Mask->getNumOperands() == 16 && "Bad type for intrinsic!"); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 5891 | |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 5892 | // Check that all of the elements are integer constants or undefs. |
| 5893 | bool AllEltsOk = true; |
| 5894 | for (unsigned i = 0; i != 16; ++i) { |
| 5895 | if (!isa<ConstantInt>(Mask->getOperand(i)) && |
| 5896 | !isa<UndefValue>(Mask->getOperand(i))) { |
| 5897 | AllEltsOk = false; |
| 5898 | break; |
| 5899 | } |
| 5900 | } |
| 5901 | |
| 5902 | if (AllEltsOk) { |
| 5903 | // Cast the input vectors to byte vectors. |
Chris Lattner | 08142f2 | 2009-08-30 19:47:22 +0000 | [diff] [blame] | 5904 | Value *Op0 = Builder->CreateBitCast(II->getOperand(1), Mask->getType()); |
| 5905 | Value *Op1 = Builder->CreateBitCast(II->getOperand(2), Mask->getType()); |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 5906 | Value *Result = UndefValue::get(Op0->getType()); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 5907 | |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 5908 | // Only extract each element once. |
| 5909 | Value *ExtractedElts[32]; |
| 5910 | memset(ExtractedElts, 0, sizeof(ExtractedElts)); |
| 5911 | |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 5912 | for (unsigned i = 0; i != 16; ++i) { |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 5913 | if (isa<UndefValue>(Mask->getOperand(i))) |
| 5914 | continue; |
| 5915 | unsigned Idx=cast<ConstantInt>(Mask->getOperand(i))->getZExtValue(); |
| 5916 | Idx &= 31; // Match the hardware behavior. |
| 5917 | |
| 5918 | if (ExtractedElts[Idx] == 0) { |
Chris Lattner | f925cbd | 2009-08-30 18:50:58 +0000 | [diff] [blame] | 5919 | ExtractedElts[Idx] = |
| 5920 | Builder->CreateExtractElement(Idx < 16 ? Op0 : Op1, |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 5921 | ConstantInt::get(Type::getInt32Ty(II->getContext()), |
| 5922 | Idx&15, false), "tmp"); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 5923 | } |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 5924 | |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 5925 | // Insert this value into the result vector. |
Chris Lattner | f925cbd | 2009-08-30 18:50:58 +0000 | [diff] [blame] | 5926 | Result = Builder->CreateInsertElement(Result, ExtractedElts[Idx], |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 5927 | ConstantInt::get(Type::getInt32Ty(II->getContext()), |
| 5928 | i, false), "tmp"); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 5929 | } |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 5930 | return CastInst::Create(Instruction::BitCast, Result, CI.getType()); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 5931 | } |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 5932 | } |
| 5933 | break; |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 5934 | |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 5935 | case Intrinsic::stackrestore: { |
| 5936 | // If the save is right next to the restore, remove the restore. This can |
| 5937 | // happen when variable allocas are DCE'd. |
| 5938 | if (IntrinsicInst *SS = dyn_cast<IntrinsicInst>(II->getOperand(1))) { |
| 5939 | if (SS->getIntrinsicID() == Intrinsic::stacksave) { |
| 5940 | BasicBlock::iterator BI = SS; |
| 5941 | if (&*++BI == II) |
| 5942 | return EraseInstFromFunction(CI); |
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 5943 | } |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 5944 | } |
| 5945 | |
| 5946 | // Scan down this block to see if there is another stack restore in the |
| 5947 | // same block without an intervening call/alloca. |
| 5948 | BasicBlock::iterator BI = II; |
| 5949 | TerminatorInst *TI = II->getParent()->getTerminator(); |
| 5950 | bool CannotRemove = false; |
| 5951 | for (++BI; &*BI != TI; ++BI) { |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 5952 | if (isa<AllocaInst>(BI) || isMalloc(BI)) { |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 5953 | CannotRemove = true; |
| 5954 | break; |
| 5955 | } |
Chris Lattner | aa0bf52 | 2008-06-25 05:59:28 +0000 | [diff] [blame] | 5956 | if (CallInst *BCI = dyn_cast<CallInst>(BI)) { |
| 5957 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(BCI)) { |
| 5958 | // If there is a stackrestore below this one, remove this one. |
| 5959 | if (II->getIntrinsicID() == Intrinsic::stackrestore) |
| 5960 | return EraseInstFromFunction(CI); |
| 5961 | // Otherwise, ignore the intrinsic. |
| 5962 | } else { |
| 5963 | // If we found a non-intrinsic call, we can't remove the stack |
| 5964 | // restore. |
Chris Lattner | bf1d8a7 | 2008-02-18 06:12:38 +0000 | [diff] [blame] | 5965 | CannotRemove = true; |
| 5966 | break; |
| 5967 | } |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 5968 | } |
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 5969 | } |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 5970 | |
| 5971 | // If the stack restore is in a return/unwind block and if there are no |
| 5972 | // allocas or calls between the restore and the return, nuke the restore. |
| 5973 | if (!CannotRemove && (isa<ReturnInst>(TI) || isa<UnwindInst>(TI))) |
| 5974 | return EraseInstFromFunction(CI); |
| 5975 | break; |
| 5976 | } |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 5977 | } |
| 5978 | |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 5979 | return visitCallSite(II); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 5980 | } |
| 5981 | |
| 5982 | // InvokeInst simplification |
| 5983 | // |
| 5984 | Instruction *InstCombiner::visitInvokeInst(InvokeInst &II) { |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 5985 | return visitCallSite(&II); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 5986 | } |
| 5987 | |
Dale Johannesen | da30ccb | 2008-04-25 21:16:07 +0000 | [diff] [blame] | 5988 | /// isSafeToEliminateVarargsCast - If this cast does not affect the value |
| 5989 | /// passed through the varargs area, we can eliminate the use of the cast. |
Dale Johannesen | 1f530a5 | 2008-04-23 18:34:37 +0000 | [diff] [blame] | 5990 | static bool isSafeToEliminateVarargsCast(const CallSite CS, |
| 5991 | const CastInst * const CI, |
| 5992 | const TargetData * const TD, |
| 5993 | const int ix) { |
| 5994 | if (!CI->isLosslessCast()) |
| 5995 | return false; |
| 5996 | |
| 5997 | // The size of ByVal arguments is derived from the type, so we |
| 5998 | // can't change to a type with a different size. If the size were |
| 5999 | // passed explicitly we could avoid this check. |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 6000 | if (!CS.paramHasAttr(ix, Attribute::ByVal)) |
Dale Johannesen | 1f530a5 | 2008-04-23 18:34:37 +0000 | [diff] [blame] | 6001 | return true; |
| 6002 | |
| 6003 | const Type* SrcTy = |
| 6004 | cast<PointerType>(CI->getOperand(0)->getType())->getElementType(); |
| 6005 | const Type* DstTy = cast<PointerType>(CI->getType())->getElementType(); |
| 6006 | if (!SrcTy->isSized() || !DstTy->isSized()) |
| 6007 | return false; |
Dan Gohman | ce9fe9f | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 6008 | if (!TD || TD->getTypeAllocSize(SrcTy) != TD->getTypeAllocSize(DstTy)) |
Dale Johannesen | 1f530a5 | 2008-04-23 18:34:37 +0000 | [diff] [blame] | 6009 | return false; |
| 6010 | return true; |
| 6011 | } |
| 6012 | |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 6013 | // visitCallSite - Improvements for call and invoke instructions. |
| 6014 | // |
| 6015 | Instruction *InstCombiner::visitCallSite(CallSite CS) { |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 6016 | bool Changed = false; |
| 6017 | |
| 6018 | // If the callee is a constexpr cast of a function, attempt to move the cast |
| 6019 | // to the arguments of the call/invoke. |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 6020 | if (transformConstExprCastCall(CS)) return 0; |
| 6021 | |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 6022 | Value *Callee = CS.getCalledValue(); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 6023 | |
Chris Lattner | 08b22ec | 2005-05-13 07:09:09 +0000 | [diff] [blame] | 6024 | if (Function *CalleeF = dyn_cast<Function>(Callee)) |
| 6025 | if (CalleeF->getCallingConv() != CS.getCallingConv()) { |
| 6026 | Instruction *OldCall = CS.getInstruction(); |
| 6027 | // If the call and callee calling conventions don't match, this call must |
| 6028 | // be unreachable, as the call is undefined. |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 6029 | new StoreInst(ConstantInt::getTrue(Callee->getContext()), |
| 6030 | UndefValue::get(Type::getInt1PtrTy(Callee->getContext())), |
Owen Anderson | d672ecb | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6031 | OldCall); |
Devang Patel | 228ebd0 | 2009-10-13 22:56:32 +0000 | [diff] [blame] | 6032 | // If OldCall dues not return void then replaceAllUsesWith undef. |
| 6033 | // This allows ValueHandlers and custom metadata to adjust itself. |
Devang Patel | 9674d15 | 2009-10-14 17:29:00 +0000 | [diff] [blame] | 6034 | if (!OldCall->getType()->isVoidTy()) |
Devang Patel | 228ebd0 | 2009-10-13 22:56:32 +0000 | [diff] [blame] | 6035 | OldCall->replaceAllUsesWith(UndefValue::get(OldCall->getType())); |
Chris Lattner | 08b22ec | 2005-05-13 07:09:09 +0000 | [diff] [blame] | 6036 | if (isa<CallInst>(OldCall)) // Not worth removing an invoke here. |
| 6037 | return EraseInstFromFunction(*OldCall); |
| 6038 | return 0; |
| 6039 | } |
| 6040 | |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 6041 | if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) { |
| 6042 | // This instruction is not reachable, just remove it. We insert a store to |
| 6043 | // undef so that we know that this code is not reachable, despite the fact |
| 6044 | // that we can't modify the CFG here. |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 6045 | new StoreInst(ConstantInt::getTrue(Callee->getContext()), |
| 6046 | UndefValue::get(Type::getInt1PtrTy(Callee->getContext())), |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 6047 | CS.getInstruction()); |
| 6048 | |
Devang Patel | 228ebd0 | 2009-10-13 22:56:32 +0000 | [diff] [blame] | 6049 | // If CS dues not return void then replaceAllUsesWith undef. |
| 6050 | // This allows ValueHandlers and custom metadata to adjust itself. |
Devang Patel | 9674d15 | 2009-10-14 17:29:00 +0000 | [diff] [blame] | 6051 | if (!CS.getInstruction()->getType()->isVoidTy()) |
Devang Patel | 228ebd0 | 2009-10-13 22:56:32 +0000 | [diff] [blame] | 6052 | CS.getInstruction()-> |
| 6053 | replaceAllUsesWith(UndefValue::get(CS.getInstruction()->getType())); |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 6054 | |
| 6055 | if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) { |
| 6056 | // Don't break the CFG, insert a dummy cond branch. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 6057 | BranchInst::Create(II->getNormalDest(), II->getUnwindDest(), |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 6058 | ConstantInt::getTrue(Callee->getContext()), II); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 6059 | } |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 6060 | return EraseInstFromFunction(*CS.getInstruction()); |
| 6061 | } |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 6062 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 6063 | if (BitCastInst *BC = dyn_cast<BitCastInst>(Callee)) |
| 6064 | if (IntrinsicInst *In = dyn_cast<IntrinsicInst>(BC->getOperand(0))) |
| 6065 | if (In->getIntrinsicID() == Intrinsic::init_trampoline) |
| 6066 | return transformCallThroughTrampoline(CS); |
| 6067 | |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 6068 | const PointerType *PTy = cast<PointerType>(Callee->getType()); |
| 6069 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
| 6070 | if (FTy->isVarArg()) { |
Dale Johannesen | 63e7eb4 | 2008-04-23 01:03:05 +0000 | [diff] [blame] | 6071 | int ix = FTy->getNumParams() + (isa<InvokeInst>(Callee) ? 3 : 1); |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 6072 | // See if we can optimize any arguments passed through the varargs area of |
| 6073 | // the call. |
| 6074 | for (CallSite::arg_iterator I = CS.arg_begin()+FTy->getNumParams(), |
Dale Johannesen | 1f530a5 | 2008-04-23 18:34:37 +0000 | [diff] [blame] | 6075 | E = CS.arg_end(); I != E; ++I, ++ix) { |
| 6076 | CastInst *CI = dyn_cast<CastInst>(*I); |
| 6077 | if (CI && isSafeToEliminateVarargsCast(CS, CI, TD, ix)) { |
| 6078 | *I = CI->getOperand(0); |
| 6079 | Changed = true; |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 6080 | } |
Dale Johannesen | 1f530a5 | 2008-04-23 18:34:37 +0000 | [diff] [blame] | 6081 | } |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 6082 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6083 | |
Duncan Sands | f0c3354 | 2007-12-19 21:13:37 +0000 | [diff] [blame] | 6084 | if (isa<InlineAsm>(Callee) && !CS.doesNotThrow()) { |
Duncan Sands | ece2c04 | 2007-12-16 15:51:49 +0000 | [diff] [blame] | 6085 | // Inline asm calls cannot throw - mark them 'nounwind'. |
Duncan Sands | f0c3354 | 2007-12-19 21:13:37 +0000 | [diff] [blame] | 6086 | CS.setDoesNotThrow(); |
Duncan Sands | ece2c04 | 2007-12-16 15:51:49 +0000 | [diff] [blame] | 6087 | Changed = true; |
| 6088 | } |
| 6089 | |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 6090 | return Changed ? CS.getInstruction() : 0; |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 6091 | } |
| 6092 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6093 | // transformConstExprCastCall - If the callee is a constexpr cast of a function, |
| 6094 | // attempt to move the cast to the arguments of the call/invoke. |
| 6095 | // |
| 6096 | bool InstCombiner::transformConstExprCastCall(CallSite CS) { |
| 6097 | if (!isa<ConstantExpr>(CS.getCalledValue())) return false; |
| 6098 | ConstantExpr *CE = cast<ConstantExpr>(CS.getCalledValue()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6099 | if (CE->getOpcode() != Instruction::BitCast || |
| 6100 | !isa<Function>(CE->getOperand(0))) |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6101 | return false; |
Reid Spencer | 8863f18 | 2004-07-18 00:38:32 +0000 | [diff] [blame] | 6102 | Function *Callee = cast<Function>(CE->getOperand(0)); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6103 | Instruction *Caller = CS.getInstruction(); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 6104 | const AttrListPtr &CallerPAL = CS.getAttributes(); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6105 | |
| 6106 | // Okay, this is a cast from a function to a different type. Unless doing so |
| 6107 | // would cause a type conversion of one of our arguments, change this call to |
| 6108 | // be a direct call with arguments casted to the appropriate types. |
| 6109 | // |
| 6110 | const FunctionType *FT = Callee->getFunctionType(); |
| 6111 | const Type *OldRetTy = Caller->getType(); |
Duncan Sands | f413cdf | 2008-06-01 07:38:42 +0000 | [diff] [blame] | 6112 | const Type *NewRetTy = FT->getReturnType(); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6113 | |
Duncan Sands | f413cdf | 2008-06-01 07:38:42 +0000 | [diff] [blame] | 6114 | if (isa<StructType>(NewRetTy)) |
Devang Patel | 75e6f02 | 2008-03-11 18:04:06 +0000 | [diff] [blame] | 6115 | return false; // TODO: Handle multiple return values. |
| 6116 | |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 6117 | // Check to see if we are changing the return type... |
Duncan Sands | f413cdf | 2008-06-01 07:38:42 +0000 | [diff] [blame] | 6118 | if (OldRetTy != NewRetTy) { |
Bill Wendling | a6c3112 | 2008-05-14 22:45:20 +0000 | [diff] [blame] | 6119 | if (Callee->isDeclaration() && |
Duncan Sands | f413cdf | 2008-06-01 07:38:42 +0000 | [diff] [blame] | 6120 | // Conversion is ok if changing from one pointer type to another or from |
| 6121 | // a pointer to an integer of the same size. |
Dan Gohman | ce9fe9f | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 6122 | !((isa<PointerType>(OldRetTy) || !TD || |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 6123 | OldRetTy == TD->getIntPtrType(Caller->getContext())) && |
Dan Gohman | ce9fe9f | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 6124 | (isa<PointerType>(NewRetTy) || !TD || |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 6125 | NewRetTy == TD->getIntPtrType(Caller->getContext())))) |
Chris Lattner | ec47992 | 2007-01-06 02:09:32 +0000 | [diff] [blame] | 6126 | return false; // Cannot transform this return value. |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 6127 | |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 6128 | if (!Caller->use_empty() && |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 6129 | // void -> non-void is handled specially |
Devang Patel | 9674d15 | 2009-10-14 17:29:00 +0000 | [diff] [blame] | 6130 | !NewRetTy->isVoidTy() && !CastInst::isCastable(NewRetTy, OldRetTy)) |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 6131 | return false; // Cannot transform this return value. |
| 6132 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 6133 | if (!CallerPAL.isEmpty() && !Caller->use_empty()) { |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 6134 | Attributes RAttrs = CallerPAL.getRetAttributes(); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 6135 | if (RAttrs & Attribute::typeIncompatible(NewRetTy)) |
Duncan Sands | 6c3470e | 2008-01-07 17:16:06 +0000 | [diff] [blame] | 6136 | return false; // Attribute not compatible with transformed value. |
| 6137 | } |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 6138 | |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 6139 | // If the callsite is an invoke instruction, and the return value is used by |
| 6140 | // a PHI node in a successor, we cannot change the return type of the call |
| 6141 | // because there is no place to put the cast instruction (without breaking |
| 6142 | // the critical edge). Bail out in this case. |
| 6143 | if (!Caller->use_empty()) |
| 6144 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) |
| 6145 | for (Value::use_iterator UI = II->use_begin(), E = II->use_end(); |
| 6146 | UI != E; ++UI) |
| 6147 | if (PHINode *PN = dyn_cast<PHINode>(*UI)) |
| 6148 | if (PN->getParent() == II->getNormalDest() || |
Chris Lattner | aeb2a1d | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 6149 | PN->getParent() == II->getUnwindDest()) |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 6150 | return false; |
| 6151 | } |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6152 | |
| 6153 | unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin()); |
| 6154 | unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6155 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6156 | CallSite::arg_iterator AI = CS.arg_begin(); |
| 6157 | for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) { |
| 6158 | const Type *ParamTy = FT->getParamType(i); |
Andrew Lenharth | b8e604c | 2006-06-28 01:01:52 +0000 | [diff] [blame] | 6159 | const Type *ActTy = (*AI)->getType(); |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 6160 | |
| 6161 | if (!CastInst::isCastable(ActTy, ParamTy)) |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 6162 | return false; // Cannot transform this parameter value. |
| 6163 | |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 6164 | if (CallerPAL.getParamAttributes(i + 1) |
| 6165 | & Attribute::typeIncompatible(ParamTy)) |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 6166 | return false; // Attribute not compatible with transformed value. |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 6167 | |
Duncan Sands | f413cdf | 2008-06-01 07:38:42 +0000 | [diff] [blame] | 6168 | // Converting from one pointer type to another or between a pointer and an |
| 6169 | // integer of the same size is safe even if we do not have a body. |
Chris Lattner | ec47992 | 2007-01-06 02:09:32 +0000 | [diff] [blame] | 6170 | bool isConvertible = ActTy == ParamTy || |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 6171 | (TD && ((isa<PointerType>(ParamTy) || |
| 6172 | ParamTy == TD->getIntPtrType(Caller->getContext())) && |
| 6173 | (isa<PointerType>(ActTy) || |
| 6174 | ActTy == TD->getIntPtrType(Caller->getContext())))); |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 6175 | if (Callee->isDeclaration() && !isConvertible) return false; |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6176 | } |
| 6177 | |
| 6178 | if (FT->getNumParams() < NumActualArgs && !FT->isVarArg() && |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 6179 | Callee->isDeclaration()) |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 6180 | return false; // Do not delete arguments unless we have a function body. |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6181 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 6182 | if (FT->getNumParams() < NumActualArgs && FT->isVarArg() && |
| 6183 | !CallerPAL.isEmpty()) |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 6184 | // In this case we have more arguments than the new function type, but we |
Duncan Sands | e1e520f | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 6185 | // won't be dropping them. Check that these extra arguments have attributes |
| 6186 | // that are compatible with being a vararg call argument. |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 6187 | for (unsigned i = CallerPAL.getNumSlots(); i; --i) { |
| 6188 | if (CallerPAL.getSlot(i - 1).Index <= FT->getNumParams()) |
Duncan Sands | e1e520f | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 6189 | break; |
Devang Patel | eaf42ab | 2008-09-23 23:03:40 +0000 | [diff] [blame] | 6190 | Attributes PAttrs = CallerPAL.getSlot(i - 1).Attrs; |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 6191 | if (PAttrs & Attribute::VarArgsIncompatible) |
Duncan Sands | e1e520f | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 6192 | return false; |
| 6193 | } |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 6194 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6195 | // Okay, we decided that this is a safe thing to do: go ahead and start |
| 6196 | // inserting cast instructions as necessary... |
| 6197 | std::vector<Value*> Args; |
| 6198 | Args.reserve(NumActualArgs); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 6199 | SmallVector<AttributeWithIndex, 8> attrVec; |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 6200 | attrVec.reserve(NumCommonArgs); |
| 6201 | |
| 6202 | // Get any return attributes. |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 6203 | Attributes RAttrs = CallerPAL.getRetAttributes(); |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 6204 | |
| 6205 | // If the return value is not being used, the type may not be compatible |
| 6206 | // with the existing attributes. Wipe out any problematic attributes. |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 6207 | RAttrs &= ~Attribute::typeIncompatible(NewRetTy); |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 6208 | |
| 6209 | // Add the new return attributes. |
| 6210 | if (RAttrs) |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 6211 | attrVec.push_back(AttributeWithIndex::get(0, RAttrs)); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6212 | |
| 6213 | AI = CS.arg_begin(); |
| 6214 | for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) { |
| 6215 | const Type *ParamTy = FT->getParamType(i); |
| 6216 | if ((*AI)->getType() == ParamTy) { |
| 6217 | Args.push_back(*AI); |
| 6218 | } else { |
Reid Spencer | 8a903db | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 6219 | Instruction::CastOps opcode = CastInst::getCastOpcode(*AI, |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6220 | false, ParamTy, false); |
Chris Lattner | f925cbd | 2009-08-30 18:50:58 +0000 | [diff] [blame] | 6221 | Args.push_back(Builder->CreateCast(opcode, *AI, ParamTy, "tmp")); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6222 | } |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 6223 | |
| 6224 | // Add any parameter attributes. |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 6225 | if (Attributes PAttrs = CallerPAL.getParamAttributes(i + 1)) |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 6226 | attrVec.push_back(AttributeWithIndex::get(i + 1, PAttrs)); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6227 | } |
| 6228 | |
| 6229 | // If the function takes more arguments than the call was taking, add them |
Chris Lattner | f925cbd | 2009-08-30 18:50:58 +0000 | [diff] [blame] | 6230 | // now. |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6231 | for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i) |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 6232 | Args.push_back(Constant::getNullValue(FT->getParamType(i))); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6233 | |
Chris Lattner | f925cbd | 2009-08-30 18:50:58 +0000 | [diff] [blame] | 6234 | // If we are removing arguments to the function, emit an obnoxious warning. |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 6235 | if (FT->getNumParams() < NumActualArgs) { |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6236 | if (!FT->isVarArg()) { |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 6237 | errs() << "WARNING: While resolving call to function '" |
| 6238 | << Callee->getName() << "' arguments were dropped!\n"; |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6239 | } else { |
Chris Lattner | f925cbd | 2009-08-30 18:50:58 +0000 | [diff] [blame] | 6240 | // Add all of the arguments in their promoted form to the arg list. |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6241 | for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) { |
| 6242 | const Type *PTy = getPromotedType((*AI)->getType()); |
| 6243 | if (PTy != (*AI)->getType()) { |
| 6244 | // Must promote to pass through va_arg area! |
Chris Lattner | f925cbd | 2009-08-30 18:50:58 +0000 | [diff] [blame] | 6245 | Instruction::CastOps opcode = |
| 6246 | CastInst::getCastOpcode(*AI, false, PTy, false); |
| 6247 | Args.push_back(Builder->CreateCast(opcode, *AI, PTy, "tmp")); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6248 | } else { |
| 6249 | Args.push_back(*AI); |
| 6250 | } |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 6251 | |
Duncan Sands | e1e520f | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 6252 | // Add any parameter attributes. |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 6253 | if (Attributes PAttrs = CallerPAL.getParamAttributes(i + 1)) |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 6254 | attrVec.push_back(AttributeWithIndex::get(i + 1, PAttrs)); |
Duncan Sands | e1e520f | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 6255 | } |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6256 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 6257 | } |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6258 | |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 6259 | if (Attributes FnAttrs = CallerPAL.getFnAttributes()) |
| 6260 | attrVec.push_back(AttributeWithIndex::get(~0, FnAttrs)); |
| 6261 | |
Devang Patel | 9674d15 | 2009-10-14 17:29:00 +0000 | [diff] [blame] | 6262 | if (NewRetTy->isVoidTy()) |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6263 | Caller->setName(""); // Void type should not have a name. |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6264 | |
Eric Christopher | a66297a | 2009-07-25 02:45:27 +0000 | [diff] [blame] | 6265 | const AttrListPtr &NewCallerPAL = AttrListPtr::get(attrVec.begin(), |
| 6266 | attrVec.end()); |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 6267 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6268 | Instruction *NC; |
| 6269 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 6270 | NC = InvokeInst::Create(Callee, II->getNormalDest(), II->getUnwindDest(), |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 6271 | Args.begin(), Args.end(), |
| 6272 | Caller->getName(), Caller); |
Reid Spencer | ed3fa85 | 2007-07-30 19:53:57 +0000 | [diff] [blame] | 6273 | cast<InvokeInst>(NC)->setCallingConv(II->getCallingConv()); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 6274 | cast<InvokeInst>(NC)->setAttributes(NewCallerPAL); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6275 | } else { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 6276 | NC = CallInst::Create(Callee, Args.begin(), Args.end(), |
| 6277 | Caller->getName(), Caller); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 6278 | CallInst *CI = cast<CallInst>(Caller); |
| 6279 | if (CI->isTailCall()) |
Chris Lattner | a9e9211 | 2005-05-06 06:48:21 +0000 | [diff] [blame] | 6280 | cast<CallInst>(NC)->setTailCall(); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 6281 | cast<CallInst>(NC)->setCallingConv(CI->getCallingConv()); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 6282 | cast<CallInst>(NC)->setAttributes(NewCallerPAL); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6283 | } |
| 6284 | |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6285 | // Insert a cast of the return type as necessary. |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6286 | Value *NV = NC; |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 6287 | if (OldRetTy != NV->getType() && !Caller->use_empty()) { |
Devang Patel | 9674d15 | 2009-10-14 17:29:00 +0000 | [diff] [blame] | 6288 | if (!NV->getType()->isVoidTy()) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6289 | Instruction::CastOps opcode = CastInst::getCastOpcode(NC, false, |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 6290 | OldRetTy, false); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6291 | NV = NC = CastInst::Create(opcode, NC, OldRetTy, "tmp"); |
Chris Lattner | bb60904 | 2003-10-30 00:46:41 +0000 | [diff] [blame] | 6292 | |
| 6293 | // If this is an invoke instruction, we should insert it after the first |
| 6294 | // non-phi, instruction in the normal successor block. |
| 6295 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
Dan Gohman | 02dea8b | 2008-05-23 21:05:58 +0000 | [diff] [blame] | 6296 | BasicBlock::iterator I = II->getNormalDest()->getFirstNonPHI(); |
Chris Lattner | bb60904 | 2003-10-30 00:46:41 +0000 | [diff] [blame] | 6297 | InsertNewInstBefore(NC, *I); |
| 6298 | } else { |
| 6299 | // Otherwise, it's a call, just insert cast right after the call instr |
| 6300 | InsertNewInstBefore(NC, *Caller); |
| 6301 | } |
Chris Lattner | e5ecdb5 | 2009-08-30 06:22:51 +0000 | [diff] [blame] | 6302 | Worklist.AddUsersToWorkList(*Caller); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6303 | } else { |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 6304 | NV = UndefValue::get(Caller->getType()); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6305 | } |
| 6306 | } |
| 6307 | |
Devang Patel | 1bf5ebc | 2009-10-13 21:41:20 +0000 | [diff] [blame] | 6308 | |
Chris Lattner | 931f8f3 | 2009-08-31 05:17:58 +0000 | [diff] [blame] | 6309 | if (!Caller->use_empty()) |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6310 | Caller->replaceAllUsesWith(NV); |
Chris Lattner | 931f8f3 | 2009-08-31 05:17:58 +0000 | [diff] [blame] | 6311 | |
| 6312 | EraseInstFromFunction(*Caller); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6313 | return true; |
| 6314 | } |
| 6315 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 6316 | // transformCallThroughTrampoline - Turn a call to a function created by the |
| 6317 | // init_trampoline intrinsic into a direct call to the underlying function. |
| 6318 | // |
| 6319 | Instruction *InstCombiner::transformCallThroughTrampoline(CallSite CS) { |
| 6320 | Value *Callee = CS.getCalledValue(); |
| 6321 | const PointerType *PTy = cast<PointerType>(Callee->getType()); |
| 6322 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 6323 | const AttrListPtr &Attrs = CS.getAttributes(); |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 6324 | |
| 6325 | // If the call already has the 'nest' attribute somewhere then give up - |
| 6326 | // otherwise 'nest' would occur twice after splicing in the chain. |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 6327 | if (Attrs.hasAttrSomewhere(Attribute::Nest)) |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 6328 | return 0; |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 6329 | |
| 6330 | IntrinsicInst *Tramp = |
| 6331 | cast<IntrinsicInst>(cast<BitCastInst>(Callee)->getOperand(0)); |
| 6332 | |
Anton Korobeynikov | 0b12ecf | 2008-05-07 22:54:15 +0000 | [diff] [blame] | 6333 | Function *NestF = cast<Function>(Tramp->getOperand(2)->stripPointerCasts()); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 6334 | const PointerType *NestFPTy = cast<PointerType>(NestF->getType()); |
| 6335 | const FunctionType *NestFTy = cast<FunctionType>(NestFPTy->getElementType()); |
| 6336 | |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 6337 | const AttrListPtr &NestAttrs = NestF->getAttributes(); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 6338 | if (!NestAttrs.isEmpty()) { |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 6339 | unsigned NestIdx = 1; |
| 6340 | const Type *NestTy = 0; |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 6341 | Attributes NestAttr = Attribute::None; |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 6342 | |
| 6343 | // Look for a parameter marked with the 'nest' attribute. |
| 6344 | for (FunctionType::param_iterator I = NestFTy->param_begin(), |
| 6345 | E = NestFTy->param_end(); I != E; ++NestIdx, ++I) |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 6346 | if (NestAttrs.paramHasAttr(NestIdx, Attribute::Nest)) { |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 6347 | // Record the parameter type and any other attributes. |
| 6348 | NestTy = *I; |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 6349 | NestAttr = NestAttrs.getParamAttributes(NestIdx); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 6350 | break; |
| 6351 | } |
| 6352 | |
| 6353 | if (NestTy) { |
| 6354 | Instruction *Caller = CS.getInstruction(); |
| 6355 | std::vector<Value*> NewArgs; |
| 6356 | NewArgs.reserve(unsigned(CS.arg_end()-CS.arg_begin())+1); |
| 6357 | |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 6358 | SmallVector<AttributeWithIndex, 8> NewAttrs; |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 6359 | NewAttrs.reserve(Attrs.getNumSlots() + 1); |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 6360 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 6361 | // Insert the nest argument into the call argument list, which may |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 6362 | // mean appending it. Likewise for attributes. |
| 6363 | |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 6364 | // Add any result attributes. |
| 6365 | if (Attributes Attr = Attrs.getRetAttributes()) |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 6366 | NewAttrs.push_back(AttributeWithIndex::get(0, Attr)); |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 6367 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 6368 | { |
| 6369 | unsigned Idx = 1; |
| 6370 | CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); |
| 6371 | do { |
| 6372 | if (Idx == NestIdx) { |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 6373 | // Add the chain argument and attributes. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 6374 | Value *NestVal = Tramp->getOperand(3); |
| 6375 | if (NestVal->getType() != NestTy) |
| 6376 | NestVal = new BitCastInst(NestVal, NestTy, "nest", Caller); |
| 6377 | NewArgs.push_back(NestVal); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 6378 | NewAttrs.push_back(AttributeWithIndex::get(NestIdx, NestAttr)); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 6379 | } |
| 6380 | |
| 6381 | if (I == E) |
| 6382 | break; |
| 6383 | |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 6384 | // Add the original argument and attributes. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 6385 | NewArgs.push_back(*I); |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 6386 | if (Attributes Attr = Attrs.getParamAttributes(Idx)) |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 6387 | NewAttrs.push_back |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 6388 | (AttributeWithIndex::get(Idx + (Idx >= NestIdx), Attr)); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 6389 | |
| 6390 | ++Idx, ++I; |
| 6391 | } while (1); |
| 6392 | } |
| 6393 | |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 6394 | // Add any function attributes. |
| 6395 | if (Attributes Attr = Attrs.getFnAttributes()) |
| 6396 | NewAttrs.push_back(AttributeWithIndex::get(~0, Attr)); |
| 6397 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 6398 | // The trampoline may have been bitcast to a bogus type (FTy). |
| 6399 | // Handle this by synthesizing a new function type, equal to FTy |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 6400 | // with the chain parameter inserted. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 6401 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 6402 | std::vector<const Type*> NewTypes; |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 6403 | NewTypes.reserve(FTy->getNumParams()+1); |
| 6404 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 6405 | // Insert the chain's type into the list of parameter types, which may |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 6406 | // mean appending it. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 6407 | { |
| 6408 | unsigned Idx = 1; |
| 6409 | FunctionType::param_iterator I = FTy->param_begin(), |
| 6410 | E = FTy->param_end(); |
| 6411 | |
| 6412 | do { |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 6413 | if (Idx == NestIdx) |
| 6414 | // Add the chain's type. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 6415 | NewTypes.push_back(NestTy); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 6416 | |
| 6417 | if (I == E) |
| 6418 | break; |
| 6419 | |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 6420 | // Add the original type. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 6421 | NewTypes.push_back(*I); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 6422 | |
| 6423 | ++Idx, ++I; |
| 6424 | } while (1); |
| 6425 | } |
| 6426 | |
| 6427 | // Replace the trampoline call with a direct call. Let the generic |
| 6428 | // code sort out any function type mismatches. |
Owen Anderson | debcb01 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 6429 | FunctionType *NewFTy = FunctionType::get(FTy->getReturnType(), NewTypes, |
Owen Anderson | d672ecb | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6430 | FTy->isVarArg()); |
| 6431 | Constant *NewCallee = |
Owen Anderson | debcb01 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 6432 | NestF->getType() == PointerType::getUnqual(NewFTy) ? |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 6433 | NestF : ConstantExpr::getBitCast(NestF, |
Owen Anderson | debcb01 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 6434 | PointerType::getUnqual(NewFTy)); |
Eric Christopher | a66297a | 2009-07-25 02:45:27 +0000 | [diff] [blame] | 6435 | const AttrListPtr &NewPAL = AttrListPtr::get(NewAttrs.begin(), |
| 6436 | NewAttrs.end()); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 6437 | |
| 6438 | Instruction *NewCaller; |
| 6439 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 6440 | NewCaller = InvokeInst::Create(NewCallee, |
| 6441 | II->getNormalDest(), II->getUnwindDest(), |
| 6442 | NewArgs.begin(), NewArgs.end(), |
| 6443 | Caller->getName(), Caller); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 6444 | cast<InvokeInst>(NewCaller)->setCallingConv(II->getCallingConv()); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 6445 | cast<InvokeInst>(NewCaller)->setAttributes(NewPAL); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 6446 | } else { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 6447 | NewCaller = CallInst::Create(NewCallee, NewArgs.begin(), NewArgs.end(), |
| 6448 | Caller->getName(), Caller); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 6449 | if (cast<CallInst>(Caller)->isTailCall()) |
| 6450 | cast<CallInst>(NewCaller)->setTailCall(); |
| 6451 | cast<CallInst>(NewCaller)-> |
| 6452 | setCallingConv(cast<CallInst>(Caller)->getCallingConv()); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 6453 | cast<CallInst>(NewCaller)->setAttributes(NewPAL); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 6454 | } |
Devang Patel | 9674d15 | 2009-10-14 17:29:00 +0000 | [diff] [blame] | 6455 | if (!Caller->getType()->isVoidTy()) |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 6456 | Caller->replaceAllUsesWith(NewCaller); |
| 6457 | Caller->eraseFromParent(); |
Chris Lattner | 7a1e924 | 2009-08-30 06:13:40 +0000 | [diff] [blame] | 6458 | Worklist.Remove(Caller); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 6459 | return 0; |
| 6460 | } |
| 6461 | } |
| 6462 | |
| 6463 | // Replace the trampoline call with a direct call. Since there is no 'nest' |
| 6464 | // parameter, there is no need to adjust the argument list. Let the generic |
| 6465 | // code sort out any function type mismatches. |
| 6466 | Constant *NewCallee = |
Owen Anderson | d672ecb | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6467 | NestF->getType() == PTy ? NestF : |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 6468 | ConstantExpr::getBitCast(NestF, PTy); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 6469 | CS.setCalledFunction(NewCallee); |
| 6470 | return CS.getInstruction(); |
| 6471 | } |
| 6472 | |
Dan Gohman | 9ad2920 | 2009-09-16 16:50:24 +0000 | [diff] [blame] | 6473 | /// FoldPHIArgBinOpIntoPHI - If we have something like phi [add (a,b), add(a,c)] |
| 6474 | /// and if a/b/c and the add's all have a single use, turn this into a phi |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 6475 | /// and a single binop. |
| 6476 | Instruction *InstCombiner::FoldPHIArgBinOpIntoPHI(PHINode &PN) { |
| 6477 | Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0)); |
Chris Lattner | 38b3dcc | 2008-12-01 03:42:51 +0000 | [diff] [blame] | 6478 | assert(isa<BinaryOperator>(FirstInst) || isa<CmpInst>(FirstInst)); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 6479 | unsigned Opc = FirstInst->getOpcode(); |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 6480 | Value *LHSVal = FirstInst->getOperand(0); |
| 6481 | Value *RHSVal = FirstInst->getOperand(1); |
| 6482 | |
| 6483 | const Type *LHSType = LHSVal->getType(); |
| 6484 | const Type *RHSType = RHSVal->getType(); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 6485 | |
Dan Gohman | 9ad2920 | 2009-09-16 16:50:24 +0000 | [diff] [blame] | 6486 | // Scan to see if all operands are the same opcode, and all have one use. |
Chris Lattner | 05f1892 | 2008-12-01 02:34:36 +0000 | [diff] [blame] | 6487 | for (unsigned i = 1; i != PN.getNumIncomingValues(); ++i) { |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 6488 | Instruction *I = dyn_cast<Instruction>(PN.getIncomingValue(i)); |
Chris Lattner | a90a24c | 2006-11-01 04:55:47 +0000 | [diff] [blame] | 6489 | if (!I || I->getOpcode() != Opc || !I->hasOneUse() || |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6490 | // Verify type of the LHS matches so we don't fold cmp's of different |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 6491 | // types or GEP's with different index types. |
| 6492 | I->getOperand(0)->getType() != LHSType || |
| 6493 | I->getOperand(1)->getType() != RHSType) |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 6494 | return 0; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6495 | |
| 6496 | // If they are CmpInst instructions, check their predicates |
| 6497 | if (Opc == Instruction::ICmp || Opc == Instruction::FCmp) |
| 6498 | if (cast<CmpInst>(I)->getPredicate() != |
| 6499 | cast<CmpInst>(FirstInst)->getPredicate()) |
| 6500 | return 0; |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 6501 | |
| 6502 | // Keep track of which operand needs a phi node. |
| 6503 | if (I->getOperand(0) != LHSVal) LHSVal = 0; |
| 6504 | if (I->getOperand(1) != RHSVal) RHSVal = 0; |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 6505 | } |
Dan Gohman | 9ad2920 | 2009-09-16 16:50:24 +0000 | [diff] [blame] | 6506 | |
| 6507 | // If both LHS and RHS would need a PHI, don't do this transformation, |
| 6508 | // because it would increase the number of PHIs entering the block, |
| 6509 | // which leads to higher register pressure. This is especially |
| 6510 | // bad when the PHIs are in the header of a loop. |
| 6511 | if (!LHSVal && !RHSVal) |
| 6512 | return 0; |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 6513 | |
Chris Lattner | 38b3dcc | 2008-12-01 03:42:51 +0000 | [diff] [blame] | 6514 | // Otherwise, this is safe to transform! |
Chris Lattner | 53738a4 | 2006-11-08 19:42:28 +0000 | [diff] [blame] | 6515 | |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 6516 | Value *InLHS = FirstInst->getOperand(0); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 6517 | Value *InRHS = FirstInst->getOperand(1); |
Chris Lattner | 53738a4 | 2006-11-08 19:42:28 +0000 | [diff] [blame] | 6518 | PHINode *NewLHS = 0, *NewRHS = 0; |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 6519 | if (LHSVal == 0) { |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 6520 | NewLHS = PHINode::Create(LHSType, |
| 6521 | FirstInst->getOperand(0)->getName() + ".pn"); |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 6522 | NewLHS->reserveOperandSpace(PN.getNumOperands()/2); |
| 6523 | NewLHS->addIncoming(InLHS, PN.getIncomingBlock(0)); |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 6524 | InsertNewInstBefore(NewLHS, PN); |
| 6525 | LHSVal = NewLHS; |
| 6526 | } |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 6527 | |
| 6528 | if (RHSVal == 0) { |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 6529 | NewRHS = PHINode::Create(RHSType, |
| 6530 | FirstInst->getOperand(1)->getName() + ".pn"); |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 6531 | NewRHS->reserveOperandSpace(PN.getNumOperands()/2); |
| 6532 | NewRHS->addIncoming(InRHS, PN.getIncomingBlock(0)); |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 6533 | InsertNewInstBefore(NewRHS, PN); |
| 6534 | RHSVal = NewRHS; |
| 6535 | } |
| 6536 | |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 6537 | // Add all operands to the new PHIs. |
Chris Lattner | 05f1892 | 2008-12-01 02:34:36 +0000 | [diff] [blame] | 6538 | if (NewLHS || NewRHS) { |
| 6539 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 6540 | Instruction *InInst = cast<Instruction>(PN.getIncomingValue(i)); |
| 6541 | if (NewLHS) { |
| 6542 | Value *NewInLHS = InInst->getOperand(0); |
| 6543 | NewLHS->addIncoming(NewInLHS, PN.getIncomingBlock(i)); |
| 6544 | } |
| 6545 | if (NewRHS) { |
| 6546 | Value *NewInRHS = InInst->getOperand(1); |
| 6547 | NewRHS->addIncoming(NewInRHS, PN.getIncomingBlock(i)); |
| 6548 | } |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 6549 | } |
| 6550 | } |
| 6551 | |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 6552 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6553 | return BinaryOperator::Create(BinOp->getOpcode(), LHSVal, RHSVal); |
Chris Lattner | 38b3dcc | 2008-12-01 03:42:51 +0000 | [diff] [blame] | 6554 | CmpInst *CIOp = cast<CmpInst>(FirstInst); |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 6555 | return CmpInst::Create(CIOp->getOpcode(), CIOp->getPredicate(), |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6556 | LHSVal, RHSVal); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 6557 | } |
| 6558 | |
Chris Lattner | 05f1892 | 2008-12-01 02:34:36 +0000 | [diff] [blame] | 6559 | Instruction *InstCombiner::FoldPHIArgGEPIntoPHI(PHINode &PN) { |
| 6560 | GetElementPtrInst *FirstInst =cast<GetElementPtrInst>(PN.getIncomingValue(0)); |
| 6561 | |
| 6562 | SmallVector<Value*, 16> FixedOperands(FirstInst->op_begin(), |
| 6563 | FirstInst->op_end()); |
Chris Lattner | 36d3e32 | 2009-02-21 00:46:50 +0000 | [diff] [blame] | 6564 | // This is true if all GEP bases are allocas and if all indices into them are |
| 6565 | // constants. |
| 6566 | bool AllBasePointersAreAllocas = true; |
Dan Gohman | b6c3385 | 2009-09-16 02:01:52 +0000 | [diff] [blame] | 6567 | |
| 6568 | // We don't want to replace this phi if the replacement would require |
Dan Gohman | 9ad2920 | 2009-09-16 16:50:24 +0000 | [diff] [blame] | 6569 | // more than one phi, which leads to higher register pressure. This is |
| 6570 | // especially bad when the PHIs are in the header of a loop. |
Dan Gohman | b6c3385 | 2009-09-16 02:01:52 +0000 | [diff] [blame] | 6571 | bool NeededPhi = false; |
Chris Lattner | 05f1892 | 2008-12-01 02:34:36 +0000 | [diff] [blame] | 6572 | |
Dan Gohman | 9ad2920 | 2009-09-16 16:50:24 +0000 | [diff] [blame] | 6573 | // Scan to see if all operands are the same opcode, and all have one use. |
Chris Lattner | 05f1892 | 2008-12-01 02:34:36 +0000 | [diff] [blame] | 6574 | for (unsigned i = 1; i != PN.getNumIncomingValues(); ++i) { |
| 6575 | GetElementPtrInst *GEP= dyn_cast<GetElementPtrInst>(PN.getIncomingValue(i)); |
| 6576 | if (!GEP || !GEP->hasOneUse() || GEP->getType() != FirstInst->getType() || |
| 6577 | GEP->getNumOperands() != FirstInst->getNumOperands()) |
| 6578 | return 0; |
| 6579 | |
Chris Lattner | 36d3e32 | 2009-02-21 00:46:50 +0000 | [diff] [blame] | 6580 | // Keep track of whether or not all GEPs are of alloca pointers. |
| 6581 | if (AllBasePointersAreAllocas && |
| 6582 | (!isa<AllocaInst>(GEP->getOperand(0)) || |
| 6583 | !GEP->hasAllConstantIndices())) |
| 6584 | AllBasePointersAreAllocas = false; |
| 6585 | |
Chris Lattner | 05f1892 | 2008-12-01 02:34:36 +0000 | [diff] [blame] | 6586 | // Compare the operand lists. |
| 6587 | for (unsigned op = 0, e = FirstInst->getNumOperands(); op != e; ++op) { |
| 6588 | if (FirstInst->getOperand(op) == GEP->getOperand(op)) |
| 6589 | continue; |
| 6590 | |
| 6591 | // Don't merge two GEPs when two operands differ (introducing phi nodes) |
| 6592 | // if one of the PHIs has a constant for the index. The index may be |
| 6593 | // substantially cheaper to compute for the constants, so making it a |
| 6594 | // variable index could pessimize the path. This also handles the case |
| 6595 | // for struct indices, which must always be constant. |
| 6596 | if (isa<ConstantInt>(FirstInst->getOperand(op)) || |
| 6597 | isa<ConstantInt>(GEP->getOperand(op))) |
| 6598 | return 0; |
| 6599 | |
| 6600 | if (FirstInst->getOperand(op)->getType() !=GEP->getOperand(op)->getType()) |
| 6601 | return 0; |
Dan Gohman | b6c3385 | 2009-09-16 02:01:52 +0000 | [diff] [blame] | 6602 | |
| 6603 | // If we already needed a PHI for an earlier operand, and another operand |
| 6604 | // also requires a PHI, we'd be introducing more PHIs than we're |
| 6605 | // eliminating, which increases register pressure on entry to the PHI's |
| 6606 | // block. |
| 6607 | if (NeededPhi) |
| 6608 | return 0; |
| 6609 | |
Chris Lattner | 05f1892 | 2008-12-01 02:34:36 +0000 | [diff] [blame] | 6610 | FixedOperands[op] = 0; // Needs a PHI. |
Dan Gohman | b6c3385 | 2009-09-16 02:01:52 +0000 | [diff] [blame] | 6611 | NeededPhi = true; |
Chris Lattner | 05f1892 | 2008-12-01 02:34:36 +0000 | [diff] [blame] | 6612 | } |
| 6613 | } |
| 6614 | |
Chris Lattner | 36d3e32 | 2009-02-21 00:46:50 +0000 | [diff] [blame] | 6615 | // If all of the base pointers of the PHI'd GEPs are from allocas, don't |
Chris Lattner | 2155088 | 2009-02-23 05:56:17 +0000 | [diff] [blame] | 6616 | // bother doing this transformation. At best, this will just save a bit of |
Chris Lattner | 36d3e32 | 2009-02-21 00:46:50 +0000 | [diff] [blame] | 6617 | // offset calculation, but all the predecessors will have to materialize the |
| 6618 | // stack address into a register anyway. We'd actually rather *clone* the |
| 6619 | // load up into the predecessors so that we have a load of a gep of an alloca, |
| 6620 | // which can usually all be folded into the load. |
| 6621 | if (AllBasePointersAreAllocas) |
| 6622 | return 0; |
| 6623 | |
Chris Lattner | 05f1892 | 2008-12-01 02:34:36 +0000 | [diff] [blame] | 6624 | // Otherwise, this is safe to transform. Insert PHI nodes for each operand |
| 6625 | // that is variable. |
| 6626 | SmallVector<PHINode*, 16> OperandPhis(FixedOperands.size()); |
| 6627 | |
| 6628 | bool HasAnyPHIs = false; |
| 6629 | for (unsigned i = 0, e = FixedOperands.size(); i != e; ++i) { |
| 6630 | if (FixedOperands[i]) continue; // operand doesn't need a phi. |
| 6631 | Value *FirstOp = FirstInst->getOperand(i); |
| 6632 | PHINode *NewPN = PHINode::Create(FirstOp->getType(), |
| 6633 | FirstOp->getName()+".pn"); |
| 6634 | InsertNewInstBefore(NewPN, PN); |
| 6635 | |
| 6636 | NewPN->reserveOperandSpace(e); |
| 6637 | NewPN->addIncoming(FirstOp, PN.getIncomingBlock(0)); |
| 6638 | OperandPhis[i] = NewPN; |
| 6639 | FixedOperands[i] = NewPN; |
| 6640 | HasAnyPHIs = true; |
| 6641 | } |
| 6642 | |
| 6643 | |
| 6644 | // Add all operands to the new PHIs. |
| 6645 | if (HasAnyPHIs) { |
| 6646 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 6647 | GetElementPtrInst *InGEP =cast<GetElementPtrInst>(PN.getIncomingValue(i)); |
| 6648 | BasicBlock *InBB = PN.getIncomingBlock(i); |
| 6649 | |
| 6650 | for (unsigned op = 0, e = OperandPhis.size(); op != e; ++op) |
| 6651 | if (PHINode *OpPhi = OperandPhis[op]) |
| 6652 | OpPhi->addIncoming(InGEP->getOperand(op), InBB); |
| 6653 | } |
| 6654 | } |
| 6655 | |
| 6656 | Value *Base = FixedOperands[0]; |
Dan Gohman | f8dbee7 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 6657 | return cast<GEPOperator>(FirstInst)->isInBounds() ? |
| 6658 | GetElementPtrInst::CreateInBounds(Base, FixedOperands.begin()+1, |
| 6659 | FixedOperands.end()) : |
Dan Gohman | d6aa02d | 2009-07-28 01:40:03 +0000 | [diff] [blame] | 6660 | GetElementPtrInst::Create(Base, FixedOperands.begin()+1, |
| 6661 | FixedOperands.end()); |
Chris Lattner | 05f1892 | 2008-12-01 02:34:36 +0000 | [diff] [blame] | 6662 | } |
| 6663 | |
| 6664 | |
Chris Lattner | 2155088 | 2009-02-23 05:56:17 +0000 | [diff] [blame] | 6665 | /// isSafeAndProfitableToSinkLoad - Return true if we know that it is safe to |
| 6666 | /// sink the load out of the block that defines it. This means that it must be |
Chris Lattner | 36d3e32 | 2009-02-21 00:46:50 +0000 | [diff] [blame] | 6667 | /// obvious the value of the load is not changed from the point of the load to |
| 6668 | /// the end of the block it is in. |
Chris Lattner | fd905ca | 2007-02-01 22:30:07 +0000 | [diff] [blame] | 6669 | /// |
| 6670 | /// Finally, it is safe, but not profitable, to sink a load targetting a |
| 6671 | /// non-address-taken alloca. Doing so will cause us to not promote the alloca |
| 6672 | /// to a register. |
Chris Lattner | 36d3e32 | 2009-02-21 00:46:50 +0000 | [diff] [blame] | 6673 | static bool isSafeAndProfitableToSinkLoad(LoadInst *L) { |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 6674 | BasicBlock::iterator BBI = L, E = L->getParent()->end(); |
| 6675 | |
| 6676 | for (++BBI; BBI != E; ++BBI) |
| 6677 | if (BBI->mayWriteToMemory()) |
| 6678 | return false; |
Chris Lattner | fd905ca | 2007-02-01 22:30:07 +0000 | [diff] [blame] | 6679 | |
| 6680 | // Check for non-address taken alloca. If not address-taken already, it isn't |
| 6681 | // profitable to do this xform. |
| 6682 | if (AllocaInst *AI = dyn_cast<AllocaInst>(L->getOperand(0))) { |
| 6683 | bool isAddressTaken = false; |
| 6684 | for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); |
| 6685 | UI != E; ++UI) { |
| 6686 | if (isa<LoadInst>(UI)) continue; |
| 6687 | if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) { |
| 6688 | // If storing TO the alloca, then the address isn't taken. |
| 6689 | if (SI->getOperand(1) == AI) continue; |
| 6690 | } |
| 6691 | isAddressTaken = true; |
| 6692 | break; |
| 6693 | } |
| 6694 | |
Chris Lattner | 36d3e32 | 2009-02-21 00:46:50 +0000 | [diff] [blame] | 6695 | if (!isAddressTaken && AI->isStaticAlloca()) |
Chris Lattner | fd905ca | 2007-02-01 22:30:07 +0000 | [diff] [blame] | 6696 | return false; |
| 6697 | } |
| 6698 | |
Chris Lattner | 36d3e32 | 2009-02-21 00:46:50 +0000 | [diff] [blame] | 6699 | // If this load is a load from a GEP with a constant offset from an alloca, |
| 6700 | // then we don't want to sink it. In its present form, it will be |
| 6701 | // load [constant stack offset]. Sinking it will cause us to have to |
| 6702 | // materialize the stack addresses in each predecessor in a register only to |
| 6703 | // do a shared load from register in the successor. |
| 6704 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(L->getOperand(0))) |
| 6705 | if (AllocaInst *AI = dyn_cast<AllocaInst>(GEP->getOperand(0))) |
| 6706 | if (AI->isStaticAlloca() && GEP->hasAllConstantIndices()) |
| 6707 | return false; |
| 6708 | |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 6709 | return true; |
| 6710 | } |
| 6711 | |
Chris Lattner | 751a362 | 2009-11-01 20:04:24 +0000 | [diff] [blame] | 6712 | Instruction *InstCombiner::FoldPHIArgLoadIntoPHI(PHINode &PN) { |
| 6713 | LoadInst *FirstLI = cast<LoadInst>(PN.getIncomingValue(0)); |
| 6714 | |
| 6715 | // When processing loads, we need to propagate two bits of information to the |
| 6716 | // sunk load: whether it is volatile, and what its alignment is. We currently |
| 6717 | // don't sink loads when some have their alignment specified and some don't. |
| 6718 | // visitLoadInst will propagate an alignment onto the load when TD is around, |
| 6719 | // and if TD isn't around, we can't handle the mixed case. |
| 6720 | bool isVolatile = FirstLI->isVolatile(); |
| 6721 | unsigned LoadAlignment = FirstLI->getAlignment(); |
| 6722 | |
| 6723 | // We can't sink the load if the loaded value could be modified between the |
| 6724 | // load and the PHI. |
| 6725 | if (FirstLI->getParent() != PN.getIncomingBlock(0) || |
| 6726 | !isSafeAndProfitableToSinkLoad(FirstLI)) |
| 6727 | return 0; |
| 6728 | |
| 6729 | // If the PHI is of volatile loads and the load block has multiple |
| 6730 | // successors, sinking it would remove a load of the volatile value from |
| 6731 | // the path through the other successor. |
| 6732 | if (isVolatile && |
| 6733 | FirstLI->getParent()->getTerminator()->getNumSuccessors() != 1) |
| 6734 | return 0; |
| 6735 | |
| 6736 | // Check to see if all arguments are the same operation. |
| 6737 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 6738 | LoadInst *LI = dyn_cast<LoadInst>(PN.getIncomingValue(i)); |
| 6739 | if (!LI || !LI->hasOneUse()) |
| 6740 | return 0; |
| 6741 | |
| 6742 | // We can't sink the load if the loaded value could be modified between |
| 6743 | // the load and the PHI. |
| 6744 | if (LI->isVolatile() != isVolatile || |
| 6745 | LI->getParent() != PN.getIncomingBlock(i) || |
| 6746 | !isSafeAndProfitableToSinkLoad(LI)) |
| 6747 | return 0; |
| 6748 | |
| 6749 | // If some of the loads have an alignment specified but not all of them, |
| 6750 | // we can't do the transformation. |
| 6751 | if ((LoadAlignment != 0) != (LI->getAlignment() != 0)) |
| 6752 | return 0; |
| 6753 | |
Chris Lattner | a664bb7 | 2009-11-01 20:07:07 +0000 | [diff] [blame] | 6754 | LoadAlignment = std::min(LoadAlignment, LI->getAlignment()); |
Chris Lattner | 751a362 | 2009-11-01 20:04:24 +0000 | [diff] [blame] | 6755 | |
| 6756 | // If the PHI is of volatile loads and the load block has multiple |
| 6757 | // successors, sinking it would remove a load of the volatile value from |
| 6758 | // the path through the other successor. |
| 6759 | if (isVolatile && |
| 6760 | LI->getParent()->getTerminator()->getNumSuccessors() != 1) |
| 6761 | return 0; |
| 6762 | } |
| 6763 | |
| 6764 | // Okay, they are all the same operation. Create a new PHI node of the |
| 6765 | // correct type, and PHI together all of the LHS's of the instructions. |
| 6766 | PHINode *NewPN = PHINode::Create(FirstLI->getOperand(0)->getType(), |
| 6767 | PN.getName()+".in"); |
| 6768 | NewPN->reserveOperandSpace(PN.getNumOperands()/2); |
| 6769 | |
| 6770 | Value *InVal = FirstLI->getOperand(0); |
| 6771 | NewPN->addIncoming(InVal, PN.getIncomingBlock(0)); |
| 6772 | |
| 6773 | // Add all operands to the new PHI. |
| 6774 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 6775 | Value *NewInVal = cast<LoadInst>(PN.getIncomingValue(i))->getOperand(0); |
| 6776 | if (NewInVal != InVal) |
| 6777 | InVal = 0; |
| 6778 | NewPN->addIncoming(NewInVal, PN.getIncomingBlock(i)); |
| 6779 | } |
| 6780 | |
| 6781 | Value *PhiVal; |
| 6782 | if (InVal) { |
| 6783 | // The new PHI unions all of the same values together. This is really |
| 6784 | // common, so we handle it intelligently here for compile-time speed. |
| 6785 | PhiVal = InVal; |
| 6786 | delete NewPN; |
| 6787 | } else { |
| 6788 | InsertNewInstBefore(NewPN, PN); |
| 6789 | PhiVal = NewPN; |
| 6790 | } |
| 6791 | |
| 6792 | // If this was a volatile load that we are merging, make sure to loop through |
| 6793 | // and mark all the input loads as non-volatile. If we don't do this, we will |
| 6794 | // insert a new volatile load and the old ones will not be deletable. |
| 6795 | if (isVolatile) |
| 6796 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) |
| 6797 | cast<LoadInst>(PN.getIncomingValue(i))->setVolatile(false); |
| 6798 | |
| 6799 | return new LoadInst(PhiVal, "", isVolatile, LoadAlignment); |
| 6800 | } |
| 6801 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6802 | |
Chris Lattner | c22d4d1 | 2009-11-10 07:23:37 +0000 | [diff] [blame] | 6803 | |
| 6804 | /// FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary" |
| 6805 | /// operator and they all are only used by the PHI, PHI together their |
| 6806 | /// inputs, and do the operation once, to the result of the PHI. |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 6807 | Instruction *InstCombiner::FoldPHIArgOpIntoPHI(PHINode &PN) { |
| 6808 | Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0)); |
| 6809 | |
Chris Lattner | 751a362 | 2009-11-01 20:04:24 +0000 | [diff] [blame] | 6810 | if (isa<GetElementPtrInst>(FirstInst)) |
| 6811 | return FoldPHIArgGEPIntoPHI(PN); |
| 6812 | if (isa<LoadInst>(FirstInst)) |
| 6813 | return FoldPHIArgLoadIntoPHI(PN); |
| 6814 | |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 6815 | // Scan the instruction, looking for input operations that can be folded away. |
| 6816 | // If all input operands to the phi are the same instruction (e.g. a cast from |
| 6817 | // the same type or "+42") we can pull the operation through the PHI, reducing |
| 6818 | // code size and simplifying code. |
| 6819 | Constant *ConstantOp = 0; |
| 6820 | const Type *CastSrcTy = 0; |
Chris Lattner | e3c6281 | 2009-11-01 19:50:13 +0000 | [diff] [blame] | 6821 | |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 6822 | if (isa<CastInst>(FirstInst)) { |
| 6823 | CastSrcTy = FirstInst->getOperand(0)->getType(); |
Chris Lattner | bf382b5 | 2009-11-08 21:20:06 +0000 | [diff] [blame] | 6824 | |
| 6825 | // Be careful about transforming integer PHIs. We don't want to pessimize |
| 6826 | // the code by turning an i32 into an i1293. |
| 6827 | if (isa<IntegerType>(PN.getType()) && isa<IntegerType>(CastSrcTy)) { |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame^] | 6828 | if (!ShouldChangeType(PN.getType(), CastSrcTy)) |
Chris Lattner | bf382b5 | 2009-11-08 21:20:06 +0000 | [diff] [blame] | 6829 | return 0; |
| 6830 | } |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6831 | } else if (isa<BinaryOperator>(FirstInst) || isa<CmpInst>(FirstInst)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6832 | // Can fold binop, compare or shift here if the RHS is a constant, |
| 6833 | // otherwise call FoldPHIArgBinOpIntoPHI. |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 6834 | ConstantOp = dyn_cast<Constant>(FirstInst->getOperand(1)); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 6835 | if (ConstantOp == 0) |
| 6836 | return FoldPHIArgBinOpIntoPHI(PN); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 6837 | } else { |
| 6838 | return 0; // Cannot fold this operation. |
| 6839 | } |
| 6840 | |
| 6841 | // Check to see if all arguments are the same operation. |
| 6842 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
Chris Lattner | 751a362 | 2009-11-01 20:04:24 +0000 | [diff] [blame] | 6843 | Instruction *I = dyn_cast<Instruction>(PN.getIncomingValue(i)); |
| 6844 | if (I == 0 || !I->hasOneUse() || !I->isSameOperationAs(FirstInst)) |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 6845 | return 0; |
| 6846 | if (CastSrcTy) { |
| 6847 | if (I->getOperand(0)->getType() != CastSrcTy) |
| 6848 | return 0; // Cast operation must match. |
| 6849 | } else if (I->getOperand(1) != ConstantOp) { |
| 6850 | return 0; |
| 6851 | } |
| 6852 | } |
| 6853 | |
| 6854 | // Okay, they are all the same operation. Create a new PHI node of the |
| 6855 | // correct type, and PHI together all of the LHS's of the instructions. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 6856 | PHINode *NewPN = PHINode::Create(FirstInst->getOperand(0)->getType(), |
| 6857 | PN.getName()+".in"); |
Chris Lattner | 5551706 | 2005-01-29 00:39:08 +0000 | [diff] [blame] | 6858 | NewPN->reserveOperandSpace(PN.getNumOperands()/2); |
Chris Lattner | b589344 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 6859 | |
| 6860 | Value *InVal = FirstInst->getOperand(0); |
| 6861 | NewPN->addIncoming(InVal, PN.getIncomingBlock(0)); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 6862 | |
| 6863 | // Add all operands to the new PHI. |
Chris Lattner | b589344 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 6864 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 6865 | Value *NewInVal = cast<Instruction>(PN.getIncomingValue(i))->getOperand(0); |
| 6866 | if (NewInVal != InVal) |
| 6867 | InVal = 0; |
| 6868 | NewPN->addIncoming(NewInVal, PN.getIncomingBlock(i)); |
| 6869 | } |
| 6870 | |
| 6871 | Value *PhiVal; |
| 6872 | if (InVal) { |
| 6873 | // The new PHI unions all of the same values together. This is really |
| 6874 | // common, so we handle it intelligently here for compile-time speed. |
| 6875 | PhiVal = InVal; |
| 6876 | delete NewPN; |
| 6877 | } else { |
| 6878 | InsertNewInstBefore(NewPN, PN); |
| 6879 | PhiVal = NewPN; |
| 6880 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6881 | |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 6882 | // Insert and return the new operation. |
Chris Lattner | e3c6281 | 2009-11-01 19:50:13 +0000 | [diff] [blame] | 6883 | if (CastInst *FirstCI = dyn_cast<CastInst>(FirstInst)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6884 | return CastInst::Create(FirstCI->getOpcode(), PhiVal, PN.getType()); |
Chris Lattner | e3c6281 | 2009-11-01 19:50:13 +0000 | [diff] [blame] | 6885 | |
Chris Lattner | 54545ac | 2008-04-29 17:13:43 +0000 | [diff] [blame] | 6886 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6887 | return BinaryOperator::Create(BinOp->getOpcode(), PhiVal, ConstantOp); |
Chris Lattner | e3c6281 | 2009-11-01 19:50:13 +0000 | [diff] [blame] | 6888 | |
Chris Lattner | 751a362 | 2009-11-01 20:04:24 +0000 | [diff] [blame] | 6889 | CmpInst *CIOp = cast<CmpInst>(FirstInst); |
| 6890 | return CmpInst::Create(CIOp->getOpcode(), CIOp->getPredicate(), |
| 6891 | PhiVal, ConstantOp); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 6892 | } |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 6893 | |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 6894 | /// DeadPHICycle - Return true if this PHI node is only used by a PHI node cycle |
| 6895 | /// that is dead. |
Chris Lattner | 0e5444b | 2007-03-26 20:40:50 +0000 | [diff] [blame] | 6896 | static bool DeadPHICycle(PHINode *PN, |
| 6897 | SmallPtrSet<PHINode*, 16> &PotentiallyDeadPHIs) { |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 6898 | if (PN->use_empty()) return true; |
| 6899 | if (!PN->hasOneUse()) return false; |
| 6900 | |
| 6901 | // Remember this node, and if we find the cycle, return. |
Chris Lattner | 0e5444b | 2007-03-26 20:40:50 +0000 | [diff] [blame] | 6902 | if (!PotentiallyDeadPHIs.insert(PN)) |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 6903 | return true; |
Chris Lattner | 92103de | 2007-08-28 04:23:55 +0000 | [diff] [blame] | 6904 | |
| 6905 | // Don't scan crazily complex things. |
| 6906 | if (PotentiallyDeadPHIs.size() == 16) |
| 6907 | return false; |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 6908 | |
| 6909 | if (PHINode *PU = dyn_cast<PHINode>(PN->use_back())) |
| 6910 | return DeadPHICycle(PU, PotentiallyDeadPHIs); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6911 | |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 6912 | return false; |
| 6913 | } |
| 6914 | |
Chris Lattner | cf5008a | 2007-11-06 21:52:06 +0000 | [diff] [blame] | 6915 | /// PHIsEqualValue - Return true if this phi node is always equal to |
| 6916 | /// NonPhiInVal. This happens with mutually cyclic phi nodes like: |
| 6917 | /// z = some value; x = phi (y, z); y = phi (x, z) |
| 6918 | static bool PHIsEqualValue(PHINode *PN, Value *NonPhiInVal, |
| 6919 | SmallPtrSet<PHINode*, 16> &ValueEqualPHIs) { |
| 6920 | // See if we already saw this PHI node. |
| 6921 | if (!ValueEqualPHIs.insert(PN)) |
| 6922 | return true; |
| 6923 | |
| 6924 | // Don't scan crazily complex things. |
| 6925 | if (ValueEqualPHIs.size() == 16) |
| 6926 | return false; |
| 6927 | |
| 6928 | // Scan the operands to see if they are either phi nodes or are equal to |
| 6929 | // the value. |
| 6930 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 6931 | Value *Op = PN->getIncomingValue(i); |
| 6932 | if (PHINode *OpPN = dyn_cast<PHINode>(Op)) { |
| 6933 | if (!PHIsEqualValue(OpPN, NonPhiInVal, ValueEqualPHIs)) |
| 6934 | return false; |
| 6935 | } else if (Op != NonPhiInVal) |
| 6936 | return false; |
| 6937 | } |
| 6938 | |
| 6939 | return true; |
| 6940 | } |
| 6941 | |
| 6942 | |
Chris Lattner | 9956c05 | 2009-11-08 19:23:30 +0000 | [diff] [blame] | 6943 | namespace { |
| 6944 | struct PHIUsageRecord { |
Chris Lattner | dd21a1c | 2009-11-09 01:38:00 +0000 | [diff] [blame] | 6945 | unsigned PHIId; // The ID # of the PHI (something determinstic to sort on) |
Chris Lattner | 9956c05 | 2009-11-08 19:23:30 +0000 | [diff] [blame] | 6946 | unsigned Shift; // The amount shifted. |
| 6947 | Instruction *Inst; // The trunc instruction. |
| 6948 | |
Chris Lattner | dd21a1c | 2009-11-09 01:38:00 +0000 | [diff] [blame] | 6949 | PHIUsageRecord(unsigned pn, unsigned Sh, Instruction *User) |
| 6950 | : PHIId(pn), Shift(Sh), Inst(User) {} |
Chris Lattner | 9956c05 | 2009-11-08 19:23:30 +0000 | [diff] [blame] | 6951 | |
| 6952 | bool operator<(const PHIUsageRecord &RHS) const { |
Chris Lattner | dd21a1c | 2009-11-09 01:38:00 +0000 | [diff] [blame] | 6953 | if (PHIId < RHS.PHIId) return true; |
| 6954 | if (PHIId > RHS.PHIId) return false; |
Chris Lattner | 9956c05 | 2009-11-08 19:23:30 +0000 | [diff] [blame] | 6955 | if (Shift < RHS.Shift) return true; |
Chris Lattner | dd21a1c | 2009-11-09 01:38:00 +0000 | [diff] [blame] | 6956 | if (Shift > RHS.Shift) return false; |
| 6957 | return Inst->getType()->getPrimitiveSizeInBits() < |
Chris Lattner | 9956c05 | 2009-11-08 19:23:30 +0000 | [diff] [blame] | 6958 | RHS.Inst->getType()->getPrimitiveSizeInBits(); |
| 6959 | } |
| 6960 | }; |
Chris Lattner | dd21a1c | 2009-11-09 01:38:00 +0000 | [diff] [blame] | 6961 | |
| 6962 | struct LoweredPHIRecord { |
| 6963 | PHINode *PN; // The PHI that was lowered. |
| 6964 | unsigned Shift; // The amount shifted. |
| 6965 | unsigned Width; // The width extracted. |
| 6966 | |
| 6967 | LoweredPHIRecord(PHINode *pn, unsigned Sh, const Type *Ty) |
| 6968 | : PN(pn), Shift(Sh), Width(Ty->getPrimitiveSizeInBits()) {} |
| 6969 | |
| 6970 | // Ctor form used by DenseMap. |
| 6971 | LoweredPHIRecord(PHINode *pn, unsigned Sh) |
| 6972 | : PN(pn), Shift(Sh), Width(0) {} |
| 6973 | }; |
| 6974 | } |
| 6975 | |
| 6976 | namespace llvm { |
| 6977 | template<> |
| 6978 | struct DenseMapInfo<LoweredPHIRecord> { |
| 6979 | static inline LoweredPHIRecord getEmptyKey() { |
| 6980 | return LoweredPHIRecord(0, 0); |
| 6981 | } |
| 6982 | static inline LoweredPHIRecord getTombstoneKey() { |
| 6983 | return LoweredPHIRecord(0, 1); |
| 6984 | } |
| 6985 | static unsigned getHashValue(const LoweredPHIRecord &Val) { |
| 6986 | return DenseMapInfo<PHINode*>::getHashValue(Val.PN) ^ (Val.Shift>>3) ^ |
| 6987 | (Val.Width>>3); |
| 6988 | } |
| 6989 | static bool isEqual(const LoweredPHIRecord &LHS, |
| 6990 | const LoweredPHIRecord &RHS) { |
| 6991 | return LHS.PN == RHS.PN && LHS.Shift == RHS.Shift && |
| 6992 | LHS.Width == RHS.Width; |
| 6993 | } |
Chris Lattner | dd21a1c | 2009-11-09 01:38:00 +0000 | [diff] [blame] | 6994 | }; |
Chris Lattner | 4bbf4ee | 2009-12-15 07:26:43 +0000 | [diff] [blame] | 6995 | template <> |
| 6996 | struct isPodLike<LoweredPHIRecord> { static const bool value = true; }; |
Chris Lattner | 9956c05 | 2009-11-08 19:23:30 +0000 | [diff] [blame] | 6997 | } |
| 6998 | |
| 6999 | |
| 7000 | /// SliceUpIllegalIntegerPHI - This is an integer PHI and we know that it has an |
| 7001 | /// illegal type: see if it is only used by trunc or trunc(lshr) operations. If |
| 7002 | /// so, we split the PHI into the various pieces being extracted. This sort of |
| 7003 | /// thing is introduced when SROA promotes an aggregate to large integer values. |
| 7004 | /// |
| 7005 | /// TODO: The user of the trunc may be an bitcast to float/double/vector or an |
| 7006 | /// inttoptr. We should produce new PHIs in the right type. |
| 7007 | /// |
Chris Lattner | dd21a1c | 2009-11-09 01:38:00 +0000 | [diff] [blame] | 7008 | Instruction *InstCombiner::SliceUpIllegalIntegerPHI(PHINode &FirstPhi) { |
| 7009 | // PHIUsers - Keep track of all of the truncated values extracted from a set |
| 7010 | // of PHIs, along with their offset. These are the things we want to rewrite. |
Chris Lattner | 9956c05 | 2009-11-08 19:23:30 +0000 | [diff] [blame] | 7011 | SmallVector<PHIUsageRecord, 16> PHIUsers; |
| 7012 | |
Chris Lattner | dd21a1c | 2009-11-09 01:38:00 +0000 | [diff] [blame] | 7013 | // PHIs are often mutually cyclic, so we keep track of a whole set of PHI |
| 7014 | // nodes which are extracted from. PHIsToSlice is a set we use to avoid |
| 7015 | // revisiting PHIs, PHIsInspected is a ordered list of PHIs that we need to |
| 7016 | // check the uses of (to ensure they are all extracts). |
| 7017 | SmallVector<PHINode*, 8> PHIsToSlice; |
| 7018 | SmallPtrSet<PHINode*, 8> PHIsInspected; |
| 7019 | |
| 7020 | PHIsToSlice.push_back(&FirstPhi); |
| 7021 | PHIsInspected.insert(&FirstPhi); |
| 7022 | |
| 7023 | for (unsigned PHIId = 0; PHIId != PHIsToSlice.size(); ++PHIId) { |
| 7024 | PHINode *PN = PHIsToSlice[PHIId]; |
Chris Lattner | 9956c05 | 2009-11-08 19:23:30 +0000 | [diff] [blame] | 7025 | |
Chris Lattner | 0ebc6ce | 2009-12-19 07:01:15 +0000 | [diff] [blame] | 7026 | // Scan the input list of the PHI. If any input is an invoke, and if the |
| 7027 | // input is defined in the predecessor, then we won't be split the critical |
| 7028 | // edge which is required to insert a truncate. Because of this, we have to |
| 7029 | // bail out. |
| 7030 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 7031 | InvokeInst *II = dyn_cast<InvokeInst>(PN->getIncomingValue(i)); |
| 7032 | if (II == 0) continue; |
| 7033 | if (II->getParent() != PN->getIncomingBlock(i)) |
| 7034 | continue; |
| 7035 | |
| 7036 | // If we have a phi, and if it's directly in the predecessor, then we have |
| 7037 | // a critical edge where we need to put the truncate. Since we can't |
| 7038 | // split the edge in instcombine, we have to bail out. |
| 7039 | return 0; |
| 7040 | } |
| 7041 | |
| 7042 | |
Chris Lattner | dd21a1c | 2009-11-09 01:38:00 +0000 | [diff] [blame] | 7043 | for (Value::use_iterator UI = PN->use_begin(), E = PN->use_end(); |
| 7044 | UI != E; ++UI) { |
| 7045 | Instruction *User = cast<Instruction>(*UI); |
| 7046 | |
| 7047 | // If the user is a PHI, inspect its uses recursively. |
| 7048 | if (PHINode *UserPN = dyn_cast<PHINode>(User)) { |
| 7049 | if (PHIsInspected.insert(UserPN)) |
| 7050 | PHIsToSlice.push_back(UserPN); |
| 7051 | continue; |
| 7052 | } |
| 7053 | |
| 7054 | // Truncates are always ok. |
| 7055 | if (isa<TruncInst>(User)) { |
| 7056 | PHIUsers.push_back(PHIUsageRecord(PHIId, 0, User)); |
| 7057 | continue; |
| 7058 | } |
| 7059 | |
| 7060 | // Otherwise it must be a lshr which can only be used by one trunc. |
| 7061 | if (User->getOpcode() != Instruction::LShr || |
| 7062 | !User->hasOneUse() || !isa<TruncInst>(User->use_back()) || |
| 7063 | !isa<ConstantInt>(User->getOperand(1))) |
| 7064 | return 0; |
| 7065 | |
| 7066 | unsigned Shift = cast<ConstantInt>(User->getOperand(1))->getZExtValue(); |
| 7067 | PHIUsers.push_back(PHIUsageRecord(PHIId, Shift, User->use_back())); |
Chris Lattner | 9956c05 | 2009-11-08 19:23:30 +0000 | [diff] [blame] | 7068 | } |
Chris Lattner | 9956c05 | 2009-11-08 19:23:30 +0000 | [diff] [blame] | 7069 | } |
| 7070 | |
| 7071 | // If we have no users, they must be all self uses, just nuke the PHI. |
| 7072 | if (PHIUsers.empty()) |
Chris Lattner | dd21a1c | 2009-11-09 01:38:00 +0000 | [diff] [blame] | 7073 | return ReplaceInstUsesWith(FirstPhi, UndefValue::get(FirstPhi.getType())); |
Chris Lattner | 9956c05 | 2009-11-08 19:23:30 +0000 | [diff] [blame] | 7074 | |
| 7075 | // If this phi node is transformable, create new PHIs for all the pieces |
| 7076 | // extracted out of it. First, sort the users by their offset and size. |
| 7077 | array_pod_sort(PHIUsers.begin(), PHIUsers.end()); |
| 7078 | |
Chris Lattner | dd21a1c | 2009-11-09 01:38:00 +0000 | [diff] [blame] | 7079 | DEBUG(errs() << "SLICING UP PHI: " << FirstPhi << '\n'; |
| 7080 | for (unsigned i = 1, e = PHIsToSlice.size(); i != e; ++i) |
| 7081 | errs() << "AND USER PHI #" << i << ": " << *PHIsToSlice[i] <<'\n'; |
| 7082 | ); |
Chris Lattner | 9956c05 | 2009-11-08 19:23:30 +0000 | [diff] [blame] | 7083 | |
Chris Lattner | dd21a1c | 2009-11-09 01:38:00 +0000 | [diff] [blame] | 7084 | // PredValues - This is a temporary used when rewriting PHI nodes. It is |
| 7085 | // hoisted out here to avoid construction/destruction thrashing. |
Chris Lattner | 9956c05 | 2009-11-08 19:23:30 +0000 | [diff] [blame] | 7086 | DenseMap<BasicBlock*, Value*> PredValues; |
| 7087 | |
Chris Lattner | dd21a1c | 2009-11-09 01:38:00 +0000 | [diff] [blame] | 7088 | // ExtractedVals - Each new PHI we introduce is saved here so we don't |
| 7089 | // introduce redundant PHIs. |
| 7090 | DenseMap<LoweredPHIRecord, PHINode*> ExtractedVals; |
| 7091 | |
| 7092 | for (unsigned UserI = 0, UserE = PHIUsers.size(); UserI != UserE; ++UserI) { |
| 7093 | unsigned PHIId = PHIUsers[UserI].PHIId; |
| 7094 | PHINode *PN = PHIsToSlice[PHIId]; |
Chris Lattner | 9956c05 | 2009-11-08 19:23:30 +0000 | [diff] [blame] | 7095 | unsigned Offset = PHIUsers[UserI].Shift; |
| 7096 | const Type *Ty = PHIUsers[UserI].Inst->getType(); |
Chris Lattner | 9956c05 | 2009-11-08 19:23:30 +0000 | [diff] [blame] | 7097 | |
Chris Lattner | dd21a1c | 2009-11-09 01:38:00 +0000 | [diff] [blame] | 7098 | PHINode *EltPHI; |
| 7099 | |
| 7100 | // If we've already lowered a user like this, reuse the previously lowered |
| 7101 | // value. |
| 7102 | if ((EltPHI = ExtractedVals[LoweredPHIRecord(PN, Offset, Ty)]) == 0) { |
Chris Lattner | 9956c05 | 2009-11-08 19:23:30 +0000 | [diff] [blame] | 7103 | |
Chris Lattner | dd21a1c | 2009-11-09 01:38:00 +0000 | [diff] [blame] | 7104 | // Otherwise, Create the new PHI node for this user. |
| 7105 | EltPHI = PHINode::Create(Ty, PN->getName()+".off"+Twine(Offset), PN); |
| 7106 | assert(EltPHI->getType() != PN->getType() && |
| 7107 | "Truncate didn't shrink phi?"); |
| 7108 | |
| 7109 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 7110 | BasicBlock *Pred = PN->getIncomingBlock(i); |
| 7111 | Value *&PredVal = PredValues[Pred]; |
| 7112 | |
| 7113 | // If we already have a value for this predecessor, reuse it. |
| 7114 | if (PredVal) { |
| 7115 | EltPHI->addIncoming(PredVal, Pred); |
| 7116 | continue; |
| 7117 | } |
Chris Lattner | 9956c05 | 2009-11-08 19:23:30 +0000 | [diff] [blame] | 7118 | |
Chris Lattner | dd21a1c | 2009-11-09 01:38:00 +0000 | [diff] [blame] | 7119 | // Handle the PHI self-reuse case. |
| 7120 | Value *InVal = PN->getIncomingValue(i); |
| 7121 | if (InVal == PN) { |
| 7122 | PredVal = EltPHI; |
| 7123 | EltPHI->addIncoming(PredVal, Pred); |
| 7124 | continue; |
Chris Lattner | 0ebc6ce | 2009-12-19 07:01:15 +0000 | [diff] [blame] | 7125 | } |
| 7126 | |
| 7127 | if (PHINode *InPHI = dyn_cast<PHINode>(PN)) { |
Chris Lattner | dd21a1c | 2009-11-09 01:38:00 +0000 | [diff] [blame] | 7128 | // If the incoming value was a PHI, and if it was one of the PHIs we |
| 7129 | // already rewrote it, just use the lowered value. |
| 7130 | if (Value *Res = ExtractedVals[LoweredPHIRecord(InPHI, Offset, Ty)]) { |
| 7131 | PredVal = Res; |
| 7132 | EltPHI->addIncoming(PredVal, Pred); |
| 7133 | continue; |
| 7134 | } |
| 7135 | } |
| 7136 | |
| 7137 | // Otherwise, do an extract in the predecessor. |
| 7138 | Builder->SetInsertPoint(Pred, Pred->getTerminator()); |
| 7139 | Value *Res = InVal; |
| 7140 | if (Offset) |
| 7141 | Res = Builder->CreateLShr(Res, ConstantInt::get(InVal->getType(), |
| 7142 | Offset), "extract"); |
| 7143 | Res = Builder->CreateTrunc(Res, Ty, "extract.t"); |
| 7144 | PredVal = Res; |
| 7145 | EltPHI->addIncoming(Res, Pred); |
| 7146 | |
| 7147 | // If the incoming value was a PHI, and if it was one of the PHIs we are |
| 7148 | // rewriting, we will ultimately delete the code we inserted. This |
| 7149 | // means we need to revisit that PHI to make sure we extract out the |
| 7150 | // needed piece. |
| 7151 | if (PHINode *OldInVal = dyn_cast<PHINode>(PN->getIncomingValue(i))) |
| 7152 | if (PHIsInspected.count(OldInVal)) { |
| 7153 | unsigned RefPHIId = std::find(PHIsToSlice.begin(),PHIsToSlice.end(), |
| 7154 | OldInVal)-PHIsToSlice.begin(); |
| 7155 | PHIUsers.push_back(PHIUsageRecord(RefPHIId, Offset, |
| 7156 | cast<Instruction>(Res))); |
| 7157 | ++UserE; |
| 7158 | } |
Chris Lattner | 9956c05 | 2009-11-08 19:23:30 +0000 | [diff] [blame] | 7159 | } |
Chris Lattner | dd21a1c | 2009-11-09 01:38:00 +0000 | [diff] [blame] | 7160 | PredValues.clear(); |
Chris Lattner | 9956c05 | 2009-11-08 19:23:30 +0000 | [diff] [blame] | 7161 | |
Chris Lattner | dd21a1c | 2009-11-09 01:38:00 +0000 | [diff] [blame] | 7162 | DEBUG(errs() << " Made element PHI for offset " << Offset << ": " |
| 7163 | << *EltPHI << '\n'); |
| 7164 | ExtractedVals[LoweredPHIRecord(PN, Offset, Ty)] = EltPHI; |
Chris Lattner | 9956c05 | 2009-11-08 19:23:30 +0000 | [diff] [blame] | 7165 | } |
Chris Lattner | 9956c05 | 2009-11-08 19:23:30 +0000 | [diff] [blame] | 7166 | |
Chris Lattner | dd21a1c | 2009-11-09 01:38:00 +0000 | [diff] [blame] | 7167 | // Replace the use of this piece with the PHI node. |
| 7168 | ReplaceInstUsesWith(*PHIUsers[UserI].Inst, EltPHI); |
Chris Lattner | 9956c05 | 2009-11-08 19:23:30 +0000 | [diff] [blame] | 7169 | } |
Chris Lattner | dd21a1c | 2009-11-09 01:38:00 +0000 | [diff] [blame] | 7170 | |
| 7171 | // Replace all the remaining uses of the PHI nodes (self uses and the lshrs) |
| 7172 | // with undefs. |
| 7173 | Value *Undef = UndefValue::get(FirstPhi.getType()); |
| 7174 | for (unsigned i = 1, e = PHIsToSlice.size(); i != e; ++i) |
| 7175 | ReplaceInstUsesWith(*PHIsToSlice[i], Undef); |
| 7176 | return ReplaceInstUsesWith(FirstPhi, Undef); |
Chris Lattner | 9956c05 | 2009-11-08 19:23:30 +0000 | [diff] [blame] | 7177 | } |
| 7178 | |
Chris Lattner | 473945d | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 7179 | // PHINode simplification |
| 7180 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 7181 | Instruction *InstCombiner::visitPHINode(PHINode &PN) { |
Owen Anderson | b64ab87 | 2006-07-10 22:15:25 +0000 | [diff] [blame] | 7182 | // If LCSSA is around, don't mess with Phi nodes |
Chris Lattner | f964f32 | 2007-03-04 04:27:24 +0000 | [diff] [blame] | 7183 | if (MustPreserveLCSSA) return 0; |
Owen Anderson | d1b78a1 | 2006-07-10 19:03:49 +0000 | [diff] [blame] | 7184 | |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 7185 | if (Value *V = PN.hasConstantValue()) |
| 7186 | return ReplaceInstUsesWith(PN, V); |
| 7187 | |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 7188 | // If all PHI operands are the same operation, pull them through the PHI, |
| 7189 | // reducing code size. |
| 7190 | if (isa<Instruction>(PN.getIncomingValue(0)) && |
Chris Lattner | 05f1892 | 2008-12-01 02:34:36 +0000 | [diff] [blame] | 7191 | isa<Instruction>(PN.getIncomingValue(1)) && |
| 7192 | cast<Instruction>(PN.getIncomingValue(0))->getOpcode() == |
| 7193 | cast<Instruction>(PN.getIncomingValue(1))->getOpcode() && |
| 7194 | // FIXME: The hasOneUse check will fail for PHIs that use the value more |
| 7195 | // than themselves more than once. |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 7196 | PN.getIncomingValue(0)->hasOneUse()) |
| 7197 | if (Instruction *Result = FoldPHIArgOpIntoPHI(PN)) |
| 7198 | return Result; |
| 7199 | |
| 7200 | // If this is a trivial cycle in the PHI node graph, remove it. Basically, if |
| 7201 | // this PHI only has a single use (a PHI), and if that PHI only has one use (a |
| 7202 | // PHI)... break the cycle. |
Chris Lattner | ff9f13a | 2007-01-15 07:30:06 +0000 | [diff] [blame] | 7203 | if (PN.hasOneUse()) { |
| 7204 | Instruction *PHIUser = cast<Instruction>(PN.use_back()); |
| 7205 | if (PHINode *PU = dyn_cast<PHINode>(PHIUser)) { |
Chris Lattner | 0e5444b | 2007-03-26 20:40:50 +0000 | [diff] [blame] | 7206 | SmallPtrSet<PHINode*, 16> PotentiallyDeadPHIs; |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 7207 | PotentiallyDeadPHIs.insert(&PN); |
| 7208 | if (DeadPHICycle(PU, PotentiallyDeadPHIs)) |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 7209 | return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType())); |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 7210 | } |
Chris Lattner | ff9f13a | 2007-01-15 07:30:06 +0000 | [diff] [blame] | 7211 | |
| 7212 | // If this phi has a single use, and if that use just computes a value for |
| 7213 | // the next iteration of a loop, delete the phi. This occurs with unused |
| 7214 | // induction variables, e.g. "for (int j = 0; ; ++j);". Detecting this |
| 7215 | // common case here is good because the only other things that catch this |
| 7216 | // are induction variable analysis (sometimes) and ADCE, which is only run |
| 7217 | // late. |
| 7218 | if (PHIUser->hasOneUse() && |
| 7219 | (isa<BinaryOperator>(PHIUser) || isa<GetElementPtrInst>(PHIUser)) && |
| 7220 | PHIUser->use_back() == &PN) { |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 7221 | return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType())); |
Chris Lattner | ff9f13a | 2007-01-15 07:30:06 +0000 | [diff] [blame] | 7222 | } |
| 7223 | } |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 7224 | |
Chris Lattner | cf5008a | 2007-11-06 21:52:06 +0000 | [diff] [blame] | 7225 | // We sometimes end up with phi cycles that non-obviously end up being the |
| 7226 | // same value, for example: |
| 7227 | // z = some value; x = phi (y, z); y = phi (x, z) |
| 7228 | // where the phi nodes don't necessarily need to be in the same block. Do a |
| 7229 | // quick check to see if the PHI node only contains a single non-phi value, if |
| 7230 | // so, scan to see if the phi cycle is actually equal to that value. |
| 7231 | { |
| 7232 | unsigned InValNo = 0, NumOperandVals = PN.getNumIncomingValues(); |
| 7233 | // Scan for the first non-phi operand. |
| 7234 | while (InValNo != NumOperandVals && |
| 7235 | isa<PHINode>(PN.getIncomingValue(InValNo))) |
| 7236 | ++InValNo; |
| 7237 | |
| 7238 | if (InValNo != NumOperandVals) { |
| 7239 | Value *NonPhiInVal = PN.getOperand(InValNo); |
| 7240 | |
| 7241 | // Scan the rest of the operands to see if there are any conflicts, if so |
| 7242 | // there is no need to recursively scan other phis. |
| 7243 | for (++InValNo; InValNo != NumOperandVals; ++InValNo) { |
| 7244 | Value *OpVal = PN.getIncomingValue(InValNo); |
| 7245 | if (OpVal != NonPhiInVal && !isa<PHINode>(OpVal)) |
| 7246 | break; |
| 7247 | } |
| 7248 | |
| 7249 | // If we scanned over all operands, then we have one unique value plus |
| 7250 | // phi values. Scan PHI nodes to see if they all merge in each other or |
| 7251 | // the value. |
| 7252 | if (InValNo == NumOperandVals) { |
| 7253 | SmallPtrSet<PHINode*, 16> ValueEqualPHIs; |
| 7254 | if (PHIsEqualValue(&PN, NonPhiInVal, ValueEqualPHIs)) |
| 7255 | return ReplaceInstUsesWith(PN, NonPhiInVal); |
| 7256 | } |
| 7257 | } |
| 7258 | } |
Dan Gohman | 8e42e4b | 2009-10-30 22:22:22 +0000 | [diff] [blame] | 7259 | |
Dan Gohman | 5b09701 | 2009-10-31 14:22:52 +0000 | [diff] [blame] | 7260 | // If there are multiple PHIs, sort their operands so that they all list |
| 7261 | // the blocks in the same order. This will help identical PHIs be eliminated |
| 7262 | // by other passes. Other passes shouldn't depend on this for correctness |
| 7263 | // however. |
| 7264 | PHINode *FirstPN = cast<PHINode>(PN.getParent()->begin()); |
| 7265 | if (&PN != FirstPN) |
| 7266 | for (unsigned i = 0, e = FirstPN->getNumIncomingValues(); i != e; ++i) { |
Dan Gohman | 8e42e4b | 2009-10-30 22:22:22 +0000 | [diff] [blame] | 7267 | BasicBlock *BBA = PN.getIncomingBlock(i); |
Dan Gohman | 5b09701 | 2009-10-31 14:22:52 +0000 | [diff] [blame] | 7268 | BasicBlock *BBB = FirstPN->getIncomingBlock(i); |
| 7269 | if (BBA != BBB) { |
| 7270 | Value *VA = PN.getIncomingValue(i); |
| 7271 | unsigned j = PN.getBasicBlockIndex(BBB); |
| 7272 | Value *VB = PN.getIncomingValue(j); |
| 7273 | PN.setIncomingBlock(i, BBB); |
| 7274 | PN.setIncomingValue(i, VB); |
| 7275 | PN.setIncomingBlock(j, BBA); |
| 7276 | PN.setIncomingValue(j, VA); |
Chris Lattner | 28f3d34 | 2009-10-31 17:48:31 +0000 | [diff] [blame] | 7277 | // NOTE: Instcombine normally would want us to "return &PN" if we |
| 7278 | // modified any of the operands of an instruction. However, since we |
| 7279 | // aren't adding or removing uses (just rearranging them) we don't do |
| 7280 | // this in this case. |
Dan Gohman | 5b09701 | 2009-10-31 14:22:52 +0000 | [diff] [blame] | 7281 | } |
Dan Gohman | 8e42e4b | 2009-10-30 22:22:22 +0000 | [diff] [blame] | 7282 | } |
| 7283 | |
Chris Lattner | 9956c05 | 2009-11-08 19:23:30 +0000 | [diff] [blame] | 7284 | // If this is an integer PHI and we know that it has an illegal type, see if |
| 7285 | // it is only used by trunc or trunc(lshr) operations. If so, we split the |
| 7286 | // PHI into the various pieces being extracted. This sort of thing is |
| 7287 | // introduced when SROA promotes an aggregate to a single large integer type. |
Chris Lattner | bf382b5 | 2009-11-08 21:20:06 +0000 | [diff] [blame] | 7288 | if (isa<IntegerType>(PN.getType()) && TD && |
Chris Lattner | 9956c05 | 2009-11-08 19:23:30 +0000 | [diff] [blame] | 7289 | !TD->isLegalInteger(PN.getType()->getPrimitiveSizeInBits())) |
| 7290 | if (Instruction *Res = SliceUpIllegalIntegerPHI(PN)) |
| 7291 | return Res; |
| 7292 | |
Chris Lattner | 60921c9 | 2003-12-19 05:58:40 +0000 | [diff] [blame] | 7293 | return 0; |
Chris Lattner | 473945d | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 7294 | } |
| 7295 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 7296 | Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 7297 | SmallVector<Value*, 8> Ops(GEP.op_begin(), GEP.op_end()); |
| 7298 | |
| 7299 | if (Value *V = SimplifyGEPInst(&Ops[0], Ops.size(), TD)) |
| 7300 | return ReplaceInstUsesWith(GEP, V); |
| 7301 | |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7302 | Value *PtrOp = GEP.getOperand(0); |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 7303 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 7304 | if (isa<UndefValue>(GEP.getOperand(0))) |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 7305 | return ReplaceInstUsesWith(GEP, UndefValue::get(GEP.getType())); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 7306 | |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7307 | // Eliminate unneeded casts for indices. |
Chris Lattner | ccf4b34 | 2009-08-30 04:49:01 +0000 | [diff] [blame] | 7308 | if (TD) { |
| 7309 | bool MadeChange = false; |
| 7310 | unsigned PtrSize = TD->getPointerSizeInBits(); |
| 7311 | |
| 7312 | gep_type_iterator GTI = gep_type_begin(GEP); |
| 7313 | for (User::op_iterator I = GEP.op_begin() + 1, E = GEP.op_end(); |
| 7314 | I != E; ++I, ++GTI) { |
| 7315 | if (!isa<SequentialType>(*GTI)) continue; |
| 7316 | |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 7317 | // If we are using a wider index than needed for this platform, shrink it |
Chris Lattner | ccf4b34 | 2009-08-30 04:49:01 +0000 | [diff] [blame] | 7318 | // to what we need. If narrower, sign-extend it to what we need. This |
| 7319 | // explicit cast can make subsequent optimizations more obvious. |
| 7320 | unsigned OpBits = cast<IntegerType>((*I)->getType())->getBitWidth(); |
Chris Lattner | ccf4b34 | 2009-08-30 04:49:01 +0000 | [diff] [blame] | 7321 | if (OpBits == PtrSize) |
| 7322 | continue; |
| 7323 | |
Chris Lattner | 2345d1d | 2009-08-30 20:01:10 +0000 | [diff] [blame] | 7324 | *I = Builder->CreateIntCast(*I, TD->getIntPtrType(GEP.getContext()),true); |
Chris Lattner | ccf4b34 | 2009-08-30 04:49:01 +0000 | [diff] [blame] | 7325 | MadeChange = true; |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7326 | } |
Chris Lattner | ccf4b34 | 2009-08-30 04:49:01 +0000 | [diff] [blame] | 7327 | if (MadeChange) return &GEP; |
Chris Lattner | db9654e | 2007-03-25 20:43:09 +0000 | [diff] [blame] | 7328 | } |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7329 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 7330 | // Combine Indices - If the source pointer to this getelementptr instruction |
| 7331 | // is a getelementptr instruction, combine the indices of the two |
| 7332 | // getelementptr instructions into a single instruction. |
| 7333 | // |
Dan Gohman | d6aa02d | 2009-07-28 01:40:03 +0000 | [diff] [blame] | 7334 | if (GEPOperator *Src = dyn_cast<GEPOperator>(PtrOp)) { |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7335 | // Note that if our source is a gep chain itself that we wait for that |
| 7336 | // chain to be resolved before we perform this transformation. This |
| 7337 | // avoids us creating a TON of code in some cases. |
| 7338 | // |
Chris Lattner | f9b91bb | 2009-08-30 05:08:50 +0000 | [diff] [blame] | 7339 | if (GetElementPtrInst *SrcGEP = |
| 7340 | dyn_cast<GetElementPtrInst>(Src->getOperand(0))) |
| 7341 | if (SrcGEP->getNumOperands() == 2) |
| 7342 | return 0; // Wait until our source is folded to completion. |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7343 | |
Chris Lattner | 72588fc | 2007-02-15 22:48:32 +0000 | [diff] [blame] | 7344 | SmallVector<Value*, 8> Indices; |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7345 | |
| 7346 | // Find out whether the last index in the source GEP is a sequential idx. |
| 7347 | bool EndsWithSequential = false; |
Chris Lattner | ab98484 | 2009-08-30 05:30:55 +0000 | [diff] [blame] | 7348 | for (gep_type_iterator I = gep_type_begin(*Src), E = gep_type_end(*Src); |
| 7349 | I != E; ++I) |
Chris Lattner | be97b4e | 2004-05-08 22:41:42 +0000 | [diff] [blame] | 7350 | EndsWithSequential = !isa<StructType>(*I); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7351 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 7352 | // Can we combine the two pointer arithmetics offsets? |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7353 | if (EndsWithSequential) { |
Chris Lattner | decd081 | 2003-03-05 22:33:14 +0000 | [diff] [blame] | 7354 | // Replace: gep (gep %P, long B), long A, ... |
| 7355 | // With: T = long A+B; gep %P, T, ... |
| 7356 | // |
Chris Lattner | f9b91bb | 2009-08-30 05:08:50 +0000 | [diff] [blame] | 7357 | Value *Sum; |
| 7358 | Value *SO1 = Src->getOperand(Src->getNumOperands()-1); |
| 7359 | Value *GO1 = GEP.getOperand(1); |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 7360 | if (SO1 == Constant::getNullValue(SO1->getType())) { |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7361 | Sum = GO1; |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 7362 | } else if (GO1 == Constant::getNullValue(GO1->getType())) { |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7363 | Sum = SO1; |
| 7364 | } else { |
Chris Lattner | ab98484 | 2009-08-30 05:30:55 +0000 | [diff] [blame] | 7365 | // If they aren't the same type, then the input hasn't been processed |
| 7366 | // by the loop above yet (which canonicalizes sequential index types to |
| 7367 | // intptr_t). Just avoid transforming this until the input has been |
| 7368 | // normalized. |
| 7369 | if (SO1->getType() != GO1->getType()) |
| 7370 | return 0; |
Chris Lattner | f925cbd | 2009-08-30 18:50:58 +0000 | [diff] [blame] | 7371 | Sum = Builder->CreateAdd(SO1, GO1, PtrOp->getName()+".sum"); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7372 | } |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7373 | |
Chris Lattner | ab98484 | 2009-08-30 05:30:55 +0000 | [diff] [blame] | 7374 | // Update the GEP in place if possible. |
Chris Lattner | f9b91bb | 2009-08-30 05:08:50 +0000 | [diff] [blame] | 7375 | if (Src->getNumOperands() == 2) { |
| 7376 | GEP.setOperand(0, Src->getOperand(0)); |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7377 | GEP.setOperand(1, Sum); |
| 7378 | return &GEP; |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7379 | } |
Chris Lattner | ab98484 | 2009-08-30 05:30:55 +0000 | [diff] [blame] | 7380 | Indices.append(Src->op_begin()+1, Src->op_end()-1); |
Chris Lattner | ccf4b34 | 2009-08-30 04:49:01 +0000 | [diff] [blame] | 7381 | Indices.push_back(Sum); |
Chris Lattner | ab98484 | 2009-08-30 05:30:55 +0000 | [diff] [blame] | 7382 | Indices.append(GEP.op_begin()+2, GEP.op_end()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7383 | } else if (isa<Constant>(*GEP.idx_begin()) && |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7384 | cast<Constant>(*GEP.idx_begin())->isNullValue() && |
Chris Lattner | f9b91bb | 2009-08-30 05:08:50 +0000 | [diff] [blame] | 7385 | Src->getNumOperands() != 1) { |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 7386 | // 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] | 7387 | Indices.append(Src->op_begin()+1, Src->op_end()); |
| 7388 | Indices.append(GEP.idx_begin()+1, GEP.idx_end()); |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 7389 | } |
| 7390 | |
Dan Gohman | f8dbee7 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 7391 | if (!Indices.empty()) |
| 7392 | return (cast<GEPOperator>(&GEP)->isInBounds() && |
| 7393 | Src->isInBounds()) ? |
| 7394 | GetElementPtrInst::CreateInBounds(Src->getOperand(0), Indices.begin(), |
| 7395 | Indices.end(), GEP.getName()) : |
Chris Lattner | f9b91bb | 2009-08-30 05:08:50 +0000 | [diff] [blame] | 7396 | GetElementPtrInst::Create(Src->getOperand(0), Indices.begin(), |
Chris Lattner | ccf4b34 | 2009-08-30 04:49:01 +0000 | [diff] [blame] | 7397 | Indices.end(), GEP.getName()); |
Chris Lattner | 6e24d83 | 2009-08-30 05:00:50 +0000 | [diff] [blame] | 7398 | } |
| 7399 | |
Chris Lattner | f9b91bb | 2009-08-30 05:08:50 +0000 | [diff] [blame] | 7400 | // Handle gep(bitcast x) and gep(gep x, 0, 0, 0). |
| 7401 | if (Value *X = getBitCastOperand(PtrOp)) { |
Chris Lattner | 6e24d83 | 2009-08-30 05:00:50 +0000 | [diff] [blame] | 7402 | assert(isa<PointerType>(X->getType()) && "Must be cast from pointer"); |
Chris Lattner | 963f4ba | 2009-08-30 20:36:46 +0000 | [diff] [blame] | 7403 | |
Chris Lattner | 2de2319 | 2009-08-30 20:38:21 +0000 | [diff] [blame] | 7404 | // If the input bitcast is actually "bitcast(bitcast(x))", then we don't |
| 7405 | // want to change the gep until the bitcasts are eliminated. |
| 7406 | if (getBitCastOperand(X)) { |
| 7407 | Worklist.AddValue(PtrOp); |
| 7408 | return 0; |
| 7409 | } |
| 7410 | |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 7411 | bool HasZeroPointerIndex = false; |
| 7412 | if (ConstantInt *C = dyn_cast<ConstantInt>(GEP.getOperand(1))) |
| 7413 | HasZeroPointerIndex = C->isZero(); |
| 7414 | |
Chris Lattner | 963f4ba | 2009-08-30 20:36:46 +0000 | [diff] [blame] | 7415 | // Transform: GEP (bitcast [10 x i8]* X to [0 x i8]*), i32 0, ... |
| 7416 | // into : GEP [10 x i8]* X, i32 0, ... |
| 7417 | // |
| 7418 | // Likewise, transform: GEP (bitcast i8* X to [0 x i8]*), i32 0, ... |
| 7419 | // into : GEP i8* X, ... |
| 7420 | // |
| 7421 | // This occurs when the program declares an array extern like "int X[];" |
Chris Lattner | 6e24d83 | 2009-08-30 05:00:50 +0000 | [diff] [blame] | 7422 | if (HasZeroPointerIndex) { |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 7423 | const PointerType *CPTy = cast<PointerType>(PtrOp->getType()); |
| 7424 | const PointerType *XTy = cast<PointerType>(X->getType()); |
Duncan Sands | 5b7cfb0 | 2009-03-02 09:18:21 +0000 | [diff] [blame] | 7425 | if (const ArrayType *CATy = |
| 7426 | dyn_cast<ArrayType>(CPTy->getElementType())) { |
| 7427 | // GEP (bitcast i8* X to [0 x i8]*), i32 0, ... ? |
| 7428 | if (CATy->getElementType() == XTy->getElementType()) { |
| 7429 | // -> GEP i8* X, ... |
| 7430 | SmallVector<Value*, 8> Indices(GEP.idx_begin()+1, GEP.idx_end()); |
Dan Gohman | f8dbee7 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 7431 | return cast<GEPOperator>(&GEP)->isInBounds() ? |
| 7432 | GetElementPtrInst::CreateInBounds(X, Indices.begin(), Indices.end(), |
| 7433 | GEP.getName()) : |
Dan Gohman | d6aa02d | 2009-07-28 01:40:03 +0000 | [diff] [blame] | 7434 | GetElementPtrInst::Create(X, Indices.begin(), Indices.end(), |
| 7435 | GEP.getName()); |
Chris Lattner | 963f4ba | 2009-08-30 20:36:46 +0000 | [diff] [blame] | 7436 | } |
| 7437 | |
| 7438 | if (const ArrayType *XATy = dyn_cast<ArrayType>(XTy->getElementType())){ |
Duncan Sands | 5b7cfb0 | 2009-03-02 09:18:21 +0000 | [diff] [blame] | 7439 | // GEP (bitcast [10 x i8]* X to [0 x i8]*), i32 0, ... ? |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 7440 | if (CATy->getElementType() == XATy->getElementType()) { |
Duncan Sands | 5b7cfb0 | 2009-03-02 09:18:21 +0000 | [diff] [blame] | 7441 | // -> GEP [10 x i8]* X, i32 0, ... |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 7442 | // At this point, we know that the cast source type is a pointer |
| 7443 | // to an array of the same type as the destination pointer |
| 7444 | // array. Because the array type is never stepped over (there |
| 7445 | // is a leading zero) we can fold the cast into this GEP. |
| 7446 | GEP.setOperand(0, X); |
| 7447 | return &GEP; |
| 7448 | } |
Duncan Sands | 5b7cfb0 | 2009-03-02 09:18:21 +0000 | [diff] [blame] | 7449 | } |
| 7450 | } |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 7451 | } else if (GEP.getNumOperands() == 2) { |
| 7452 | // Transform things like: |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 7453 | // %t = getelementptr i32* bitcast ([2 x i32]* %str to i32*), i32 %V |
| 7454 | // into: %t1 = getelementptr [2 x i32]* %str, i32 0, i32 %V; bitcast |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 7455 | const Type *SrcElTy = cast<PointerType>(X->getType())->getElementType(); |
| 7456 | const Type *ResElTy=cast<PointerType>(PtrOp->getType())->getElementType(); |
Dan Gohman | ce9fe9f | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 7457 | if (TD && isa<ArrayType>(SrcElTy) && |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 7458 | TD->getTypeAllocSize(cast<ArrayType>(SrcElTy)->getElementType()) == |
| 7459 | TD->getTypeAllocSize(ResElTy)) { |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 7460 | Value *Idx[2]; |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 7461 | Idx[0] = Constant::getNullValue(Type::getInt32Ty(GEP.getContext())); |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 7462 | Idx[1] = GEP.getOperand(1); |
Dan Gohman | f8dbee7 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 7463 | Value *NewGEP = cast<GEPOperator>(&GEP)->isInBounds() ? |
| 7464 | Builder->CreateInBoundsGEP(X, Idx, Idx + 2, GEP.getName()) : |
Chris Lattner | f925cbd | 2009-08-30 18:50:58 +0000 | [diff] [blame] | 7465 | Builder->CreateGEP(X, Idx, Idx + 2, GEP.getName()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7466 | // V and GEP are both pointer types --> BitCast |
Chris Lattner | f925cbd | 2009-08-30 18:50:58 +0000 | [diff] [blame] | 7467 | return new BitCastInst(NewGEP, GEP.getType()); |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 7468 | } |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 7469 | |
| 7470 | // Transform things like: |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 7471 | // getelementptr i8* bitcast ([100 x double]* X to i8*), i32 %tmp |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 7472 | // (where tmp = 8*tmp2) into: |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 7473 | // getelementptr [100 x double]* %arr, i32 0, i32 %tmp2; bitcast |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 7474 | |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 7475 | if (TD && isa<ArrayType>(SrcElTy) && |
| 7476 | ResElTy == Type::getInt8Ty(GEP.getContext())) { |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 7477 | uint64_t ArrayEltSize = |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 7478 | TD->getTypeAllocSize(cast<ArrayType>(SrcElTy)->getElementType()); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 7479 | |
| 7480 | // Check to see if "tmp" is a scale by a multiple of ArrayEltSize. We |
| 7481 | // allow either a mul, shift, or constant here. |
| 7482 | Value *NewIdx = 0; |
| 7483 | ConstantInt *Scale = 0; |
| 7484 | if (ArrayEltSize == 1) { |
| 7485 | NewIdx = GEP.getOperand(1); |
Chris Lattner | ab98484 | 2009-08-30 05:30:55 +0000 | [diff] [blame] | 7486 | Scale = ConstantInt::get(cast<IntegerType>(NewIdx->getType()), 1); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 7487 | } else if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP.getOperand(1))) { |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 7488 | NewIdx = ConstantInt::get(CI->getType(), 1); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 7489 | Scale = CI; |
| 7490 | } else if (Instruction *Inst =dyn_cast<Instruction>(GEP.getOperand(1))){ |
| 7491 | if (Inst->getOpcode() == Instruction::Shl && |
| 7492 | isa<ConstantInt>(Inst->getOperand(1))) { |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 7493 | ConstantInt *ShAmt = cast<ConstantInt>(Inst->getOperand(1)); |
| 7494 | uint32_t ShAmtVal = ShAmt->getLimitedValue(64); |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 7495 | Scale = ConstantInt::get(cast<IntegerType>(Inst->getType()), |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 7496 | 1ULL << ShAmtVal); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 7497 | NewIdx = Inst->getOperand(0); |
| 7498 | } else if (Inst->getOpcode() == Instruction::Mul && |
| 7499 | isa<ConstantInt>(Inst->getOperand(1))) { |
| 7500 | Scale = cast<ConstantInt>(Inst->getOperand(1)); |
| 7501 | NewIdx = Inst->getOperand(0); |
| 7502 | } |
| 7503 | } |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 7504 | |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 7505 | // If the index will be to exactly the right offset with the scale taken |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 7506 | // out, perform the transformation. Note, we don't know whether Scale is |
| 7507 | // signed or not. We'll use unsigned version of division/modulo |
| 7508 | // operation after making sure Scale doesn't have the sign bit set. |
Chris Lattner | 58b1ac7 | 2009-02-25 18:20:01 +0000 | [diff] [blame] | 7509 | if (ArrayEltSize && Scale && Scale->getSExtValue() >= 0LL && |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 7510 | Scale->getZExtValue() % ArrayEltSize == 0) { |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 7511 | Scale = ConstantInt::get(Scale->getType(), |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 7512 | Scale->getZExtValue() / ArrayEltSize); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7513 | if (Scale->getZExtValue() != 1) { |
Chris Lattner | 878daed | 2009-08-30 05:56:44 +0000 | [diff] [blame] | 7514 | Constant *C = ConstantExpr::getIntegerCast(Scale, NewIdx->getType(), |
| 7515 | false /*ZExt*/); |
Chris Lattner | f925cbd | 2009-08-30 18:50:58 +0000 | [diff] [blame] | 7516 | NewIdx = Builder->CreateMul(NewIdx, C, "idxscale"); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 7517 | } |
| 7518 | |
| 7519 | // Insert the new GEP instruction. |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 7520 | Value *Idx[2]; |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 7521 | Idx[0] = Constant::getNullValue(Type::getInt32Ty(GEP.getContext())); |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 7522 | Idx[1] = NewIdx; |
Dan Gohman | f8dbee7 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 7523 | Value *NewGEP = cast<GEPOperator>(&GEP)->isInBounds() ? |
| 7524 | Builder->CreateInBoundsGEP(X, Idx, Idx + 2, GEP.getName()) : |
| 7525 | Builder->CreateGEP(X, Idx, Idx + 2, GEP.getName()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7526 | // The NewGEP must be pointer typed, so must the old one -> BitCast |
| 7527 | return new BitCastInst(NewGEP, GEP.getType()); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 7528 | } |
| 7529 | } |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 7530 | } |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 7531 | } |
Chris Lattner | 5840779 | 2009-01-09 04:53:57 +0000 | [diff] [blame] | 7532 | |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 7533 | /// See if we can simplify: |
Chris Lattner | 873ff01 | 2009-08-30 05:55:36 +0000 | [diff] [blame] | 7534 | /// X = bitcast A* to B* |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 7535 | /// Y = gep X, <...constant indices...> |
| 7536 | /// into a gep of the original struct. This is important for SROA and alias |
| 7537 | /// 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] | 7538 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(PtrOp)) { |
Dan Gohman | ce9fe9f | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 7539 | if (TD && |
| 7540 | !isa<BitCastInst>(BCI->getOperand(0)) && GEP.hasAllConstantIndices()) { |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 7541 | // Determine how much the GEP moves the pointer. We are guaranteed to get |
| 7542 | // a constant back from EmitGEPOffset. |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 7543 | ConstantInt *OffsetV = cast<ConstantInt>(EmitGEPOffset(&GEP)); |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 7544 | int64_t Offset = OffsetV->getSExtValue(); |
| 7545 | |
| 7546 | // If this GEP instruction doesn't move the pointer, just replace the GEP |
| 7547 | // with a bitcast of the real input to the dest type. |
| 7548 | if (Offset == 0) { |
| 7549 | // If the bitcast is of an allocation, and the allocation will be |
| 7550 | // converted to match the type of the cast, don't touch this. |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 7551 | if (isa<AllocaInst>(BCI->getOperand(0)) || |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 7552 | isMalloc(BCI->getOperand(0))) { |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 7553 | // See if the bitcast simplifies, if so, don't nuke this GEP yet. |
| 7554 | if (Instruction *I = visitBitCast(*BCI)) { |
| 7555 | if (I != BCI) { |
| 7556 | I->takeName(BCI); |
| 7557 | BCI->getParent()->getInstList().insert(BCI, I); |
| 7558 | ReplaceInstUsesWith(*BCI, I); |
| 7559 | } |
| 7560 | return &GEP; |
Chris Lattner | 5840779 | 2009-01-09 04:53:57 +0000 | [diff] [blame] | 7561 | } |
Chris Lattner | 5840779 | 2009-01-09 04:53:57 +0000 | [diff] [blame] | 7562 | } |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 7563 | return new BitCastInst(BCI->getOperand(0), GEP.getType()); |
Chris Lattner | 5840779 | 2009-01-09 04:53:57 +0000 | [diff] [blame] | 7564 | } |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 7565 | |
| 7566 | // Otherwise, if the offset is non-zero, we need to find out if there is a |
| 7567 | // field at Offset in 'A's type. If so, we can pull the cast through the |
| 7568 | // GEP. |
| 7569 | SmallVector<Value*, 8> NewIndices; |
| 7570 | const Type *InTy = |
| 7571 | cast<PointerType>(BCI->getOperand(0)->getType())->getElementType(); |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame^] | 7572 | if (FindElementAtOffset(InTy, Offset, NewIndices)) { |
Dan Gohman | f8dbee7 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 7573 | Value *NGEP = cast<GEPOperator>(&GEP)->isInBounds() ? |
| 7574 | Builder->CreateInBoundsGEP(BCI->getOperand(0), NewIndices.begin(), |
| 7575 | NewIndices.end()) : |
| 7576 | Builder->CreateGEP(BCI->getOperand(0), NewIndices.begin(), |
| 7577 | NewIndices.end()); |
Chris Lattner | f925cbd | 2009-08-30 18:50:58 +0000 | [diff] [blame] | 7578 | |
| 7579 | if (NGEP->getType() == GEP.getType()) |
| 7580 | return ReplaceInstUsesWith(GEP, NGEP); |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 7581 | NGEP->takeName(&GEP); |
| 7582 | return new BitCastInst(NGEP, GEP.getType()); |
| 7583 | } |
Chris Lattner | 5840779 | 2009-01-09 04:53:57 +0000 | [diff] [blame] | 7584 | } |
| 7585 | } |
| 7586 | |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 7587 | return 0; |
| 7588 | } |
| 7589 | |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 7590 | Instruction *InstCombiner::visitAllocaInst(AllocaInst &AI) { |
Chris Lattner | e3c6281 | 2009-11-01 19:50:13 +0000 | [diff] [blame] | 7591 | // Convert: alloca Ty, C - where C is a constant != 1 into: alloca [C x Ty], 1 |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 7592 | if (AI.isArrayAllocation()) { // Check C != 1 |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7593 | if (const ConstantInt *C = dyn_cast<ConstantInt>(AI.getArraySize())) { |
| 7594 | const Type *NewTy = |
Owen Anderson | debcb01 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 7595 | ArrayType::get(AI.getAllocatedType(), C->getZExtValue()); |
Victor Hernandez | a276c60 | 2009-10-17 01:18:07 +0000 | [diff] [blame] | 7596 | assert(isa<AllocaInst>(AI) && "Unknown type of allocation inst!"); |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 7597 | AllocaInst *New = Builder->CreateAlloca(NewTy, 0, AI.getName()); |
Chris Lattner | f925cbd | 2009-08-30 18:50:58 +0000 | [diff] [blame] | 7598 | New->setAlignment(AI.getAlignment()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7599 | |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 7600 | // Scan to the end of the allocation instructions, to skip over a block of |
Dale Johannesen | a891518 | 2009-03-11 22:19:43 +0000 | [diff] [blame] | 7601 | // allocas if possible...also skip interleaved debug info |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 7602 | // |
| 7603 | BasicBlock::iterator It = New; |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 7604 | while (isa<AllocaInst>(*It) || isa<DbgInfoIntrinsic>(*It)) ++It; |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 7605 | |
| 7606 | // Now that I is pointing to the first non-allocation-inst in the block, |
| 7607 | // insert our getelementptr instruction... |
| 7608 | // |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 7609 | Value *NullIdx =Constant::getNullValue(Type::getInt32Ty(AI.getContext())); |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 7610 | Value *Idx[2]; |
| 7611 | Idx[0] = NullIdx; |
| 7612 | Idx[1] = NullIdx; |
Dan Gohman | f8dbee7 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 7613 | Value *V = GetElementPtrInst::CreateInBounds(New, Idx, Idx + 2, |
| 7614 | New->getName()+".sub", It); |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 7615 | |
| 7616 | // Now make everything use the getelementptr instead of the original |
| 7617 | // allocation. |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 7618 | return ReplaceInstUsesWith(AI, V); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 7619 | } else if (isa<UndefValue>(AI.getArraySize())) { |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 7620 | return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 7621 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 7622 | } |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 7623 | |
Dan Gohman | ce9fe9f | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 7624 | if (TD && isa<AllocaInst>(AI) && AI.getAllocatedType()->isSized()) { |
Dan Gohman | 6893cd7 | 2009-01-13 20:18:38 +0000 | [diff] [blame] | 7625 | // If alloca'ing a zero byte object, replace the alloca with a null pointer. |
Chris Lattner | 46d232d | 2009-03-17 17:55:15 +0000 | [diff] [blame] | 7626 | // Note that we only do this for alloca's, because malloc should allocate |
| 7627 | // and return a unique pointer, even for a zero byte allocation. |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 7628 | if (TD->getTypeAllocSize(AI.getAllocatedType()) == 0) |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 7629 | return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); |
Dan Gohman | 6893cd7 | 2009-01-13 20:18:38 +0000 | [diff] [blame] | 7630 | |
| 7631 | // If the alignment is 0 (unspecified), assign it the preferred alignment. |
| 7632 | if (AI.getAlignment() == 0) |
| 7633 | AI.setAlignment(TD->getPrefTypeAlignment(AI.getAllocatedType())); |
| 7634 | } |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 7635 | |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 7636 | return 0; |
| 7637 | } |
| 7638 | |
Victor Hernandez | 66284e0 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 7639 | Instruction *InstCombiner::visitFree(Instruction &FI) { |
| 7640 | Value *Op = FI.getOperand(1); |
| 7641 | |
| 7642 | // free undef -> unreachable. |
| 7643 | if (isa<UndefValue>(Op)) { |
| 7644 | // Insert a new store to null because we cannot modify the CFG here. |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 7645 | new StoreInst(ConstantInt::getTrue(FI.getContext()), |
| 7646 | UndefValue::get(Type::getInt1PtrTy(FI.getContext())), &FI); |
Victor Hernandez | 66284e0 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 7647 | return EraseInstFromFunction(FI); |
| 7648 | } |
| 7649 | |
| 7650 | // If we have 'free null' delete the instruction. This can happen in stl code |
| 7651 | // when lots of inlining happens. |
| 7652 | if (isa<ConstantPointerNull>(Op)) |
| 7653 | return EraseInstFromFunction(FI); |
| 7654 | |
Victor Hernandez | 046e78c | 2009-10-26 23:43:48 +0000 | [diff] [blame] | 7655 | // If we have a malloc call whose only use is a free call, delete both. |
Dan Gohman | 7f712a1 | 2009-10-27 00:11:02 +0000 | [diff] [blame] | 7656 | if (isMalloc(Op)) { |
Victor Hernandez | 66284e0 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 7657 | if (CallInst* CI = extractMallocCallFromBitCast(Op)) { |
| 7658 | if (Op->hasOneUse() && CI->hasOneUse()) { |
| 7659 | EraseInstFromFunction(FI); |
| 7660 | EraseInstFromFunction(*CI); |
| 7661 | return EraseInstFromFunction(*cast<Instruction>(Op)); |
| 7662 | } |
| 7663 | } else { |
| 7664 | // Op is a call to malloc |
| 7665 | if (Op->hasOneUse()) { |
| 7666 | EraseInstFromFunction(FI); |
| 7667 | return EraseInstFromFunction(*cast<Instruction>(Op)); |
| 7668 | } |
| 7669 | } |
Dan Gohman | 7f712a1 | 2009-10-27 00:11:02 +0000 | [diff] [blame] | 7670 | } |
Victor Hernandez | 66284e0 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 7671 | |
| 7672 | return 0; |
| 7673 | } |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 7674 | |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 7675 | /// InstCombineLoadCast - Fold 'load (cast P)' -> cast (load P)' when possible. |
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 7676 | static Instruction *InstCombineLoadCast(InstCombiner &IC, LoadInst &LI, |
Bill Wendling | 587c01d | 2008-02-26 10:53:30 +0000 | [diff] [blame] | 7677 | const TargetData *TD) { |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 7678 | User *CI = cast<User>(LI.getOperand(0)); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 7679 | Value *CastOp = CI->getOperand(0); |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 7680 | |
Mon P Wang | 6753f95 | 2009-02-07 22:19:29 +0000 | [diff] [blame] | 7681 | const PointerType *DestTy = cast<PointerType>(CI->getType()); |
| 7682 | const Type *DestPTy = DestTy->getElementType(); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 7683 | if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) { |
Mon P Wang | 6753f95 | 2009-02-07 22:19:29 +0000 | [diff] [blame] | 7684 | |
| 7685 | // If the address spaces don't match, don't eliminate the cast. |
| 7686 | if (DestTy->getAddressSpace() != SrcTy->getAddressSpace()) |
| 7687 | return 0; |
| 7688 | |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 7689 | const Type *SrcPTy = SrcTy->getElementType(); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 7690 | |
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 7691 | if (DestPTy->isInteger() || isa<PointerType>(DestPTy) || |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 7692 | isa<VectorType>(DestPTy)) { |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 7693 | // If the source is an array, the code below will not succeed. Check to |
| 7694 | // see if a trivial 'gep P, 0, 0' will help matters. Only do this for |
| 7695 | // constants. |
| 7696 | if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy)) |
| 7697 | if (Constant *CSrc = dyn_cast<Constant>(CastOp)) |
| 7698 | if (ASrcTy->getNumElements() != 0) { |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 7699 | Value *Idxs[2]; |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 7700 | Idxs[0] = Constant::getNullValue(Type::getInt32Ty(LI.getContext())); |
Chris Lattner | e00c43f | 2009-10-22 06:44:07 +0000 | [diff] [blame] | 7701 | Idxs[1] = Idxs[0]; |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 7702 | CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs, 2); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 7703 | SrcTy = cast<PointerType>(CastOp->getType()); |
| 7704 | SrcPTy = SrcTy->getElementType(); |
| 7705 | } |
| 7706 | |
Dan Gohman | ce9fe9f | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 7707 | if (IC.getTargetData() && |
| 7708 | (SrcPTy->isInteger() || isa<PointerType>(SrcPTy) || |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 7709 | isa<VectorType>(SrcPTy)) && |
Chris Lattner | b1515fe | 2005-03-29 06:37:47 +0000 | [diff] [blame] | 7710 | // Do not allow turning this into a load of an integer, which is then |
| 7711 | // casted to a pointer, this pessimizes pointer analysis a lot. |
| 7712 | (isa<PointerType>(SrcPTy) == isa<PointerType>(LI.getType())) && |
Dan Gohman | ce9fe9f | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 7713 | IC.getTargetData()->getTypeSizeInBits(SrcPTy) == |
| 7714 | IC.getTargetData()->getTypeSizeInBits(DestPTy)) { |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7715 | |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 7716 | // Okay, we are casting from one integer or pointer type to another of |
| 7717 | // the same size. Instead of casting the pointer before the load, cast |
| 7718 | // the result of the loaded value. |
Chris Lattner | f925cbd | 2009-08-30 18:50:58 +0000 | [diff] [blame] | 7719 | Value *NewLoad = |
| 7720 | IC.Builder->CreateLoad(CastOp, LI.isVolatile(), CI->getName()); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 7721 | // Now cast the result of the load. |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 7722 | return new BitCastInst(NewLoad, LI.getType()); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 7723 | } |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 7724 | } |
| 7725 | } |
| 7726 | return 0; |
| 7727 | } |
| 7728 | |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 7729 | Instruction *InstCombiner::visitLoadInst(LoadInst &LI) { |
| 7730 | Value *Op = LI.getOperand(0); |
Chris Lattner | 5f16a13 | 2004-01-12 04:13:56 +0000 | [diff] [blame] | 7731 | |
Dan Gohman | 9941f74 | 2007-07-20 16:34:21 +0000 | [diff] [blame] | 7732 | // Attempt to improve the alignment. |
Dan Gohman | ce9fe9f | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 7733 | if (TD) { |
| 7734 | unsigned KnownAlign = |
| 7735 | GetOrEnforceKnownAlignment(Op, TD->getPrefTypeAlignment(LI.getType())); |
| 7736 | if (KnownAlign > |
| 7737 | (LI.getAlignment() == 0 ? TD->getABITypeAlignment(LI.getType()) : |
| 7738 | LI.getAlignment())) |
| 7739 | LI.setAlignment(KnownAlign); |
| 7740 | } |
Dan Gohman | 9941f74 | 2007-07-20 16:34:21 +0000 | [diff] [blame] | 7741 | |
Chris Lattner | 963f4ba | 2009-08-30 20:36:46 +0000 | [diff] [blame] | 7742 | // load (cast X) --> cast (load X) iff safe. |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 7743 | if (isa<CastInst>(Op)) |
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 7744 | if (Instruction *Res = InstCombineLoadCast(*this, LI, TD)) |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 7745 | return Res; |
| 7746 | |
| 7747 | // None of the following transforms are legal for volatile loads. |
| 7748 | if (LI.isVolatile()) return 0; |
Chris Lattner | 62f254d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 7749 | |
Dan Gohman | 2276a7b | 2008-10-15 23:19:35 +0000 | [diff] [blame] | 7750 | // Do really simple store-to-load forwarding and load CSE, to catch cases |
| 7751 | // where there are several consequtive memory accesses to the same location, |
| 7752 | // separated by a few arithmetic operations. |
| 7753 | BasicBlock::iterator BBI = &LI; |
Chris Lattner | 4aebaee | 2008-11-27 08:56:30 +0000 | [diff] [blame] | 7754 | if (Value *AvailableVal = FindAvailableLoadedValue(Op, LI.getParent(), BBI,6)) |
| 7755 | return ReplaceInstUsesWith(LI, AvailableVal); |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 7756 | |
Chris Lattner | 878e494 | 2009-10-22 06:25:11 +0000 | [diff] [blame] | 7757 | // load(gep null, ...) -> unreachable |
Christopher Lamb | b15147e | 2007-12-29 07:56:53 +0000 | [diff] [blame] | 7758 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) { |
| 7759 | const Value *GEPI0 = GEPI->getOperand(0); |
| 7760 | // TODO: Consider a target hook for valid address spaces for this xform. |
Chris Lattner | 8a67ac5 | 2009-08-30 20:06:40 +0000 | [diff] [blame] | 7761 | if (isa<ConstantPointerNull>(GEPI0) && GEPI->getPointerAddressSpace() == 0){ |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 7762 | // Insert a new store to null instruction before the load to indicate |
| 7763 | // that this code is not reachable. We do this instead of inserting |
| 7764 | // an unreachable instruction directly because we cannot modify the |
| 7765 | // CFG. |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 7766 | new StoreInst(UndefValue::get(LI.getType()), |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 7767 | Constant::getNullValue(Op->getType()), &LI); |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 7768 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 7769 | } |
Christopher Lamb | b15147e | 2007-12-29 07:56:53 +0000 | [diff] [blame] | 7770 | } |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 7771 | |
Chris Lattner | 878e494 | 2009-10-22 06:25:11 +0000 | [diff] [blame] | 7772 | // load null/undef -> unreachable |
| 7773 | // TODO: Consider a target hook for valid address spaces for this xform. |
| 7774 | if (isa<UndefValue>(Op) || |
| 7775 | (isa<ConstantPointerNull>(Op) && LI.getPointerAddressSpace() == 0)) { |
| 7776 | // Insert a new store to null instruction before the load to indicate that |
| 7777 | // this code is not reachable. We do this instead of inserting an |
| 7778 | // unreachable instruction directly because we cannot modify the CFG. |
| 7779 | new StoreInst(UndefValue::get(LI.getType()), |
| 7780 | Constant::getNullValue(Op->getType()), &LI); |
| 7781 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 7782 | } |
Chris Lattner | 878e494 | 2009-10-22 06:25:11 +0000 | [diff] [blame] | 7783 | |
| 7784 | // Instcombine load (constantexpr_cast global) -> cast (load global) |
| 7785 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op)) |
| 7786 | if (CE->isCast()) |
| 7787 | if (Instruction *Res = InstCombineLoadCast(*this, LI, TD)) |
| 7788 | return Res; |
| 7789 | |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 7790 | if (Op->hasOneUse()) { |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 7791 | // Change select and PHI nodes to select values instead of addresses: this |
| 7792 | // helps alias analysis out a lot, allows many others simplifications, and |
| 7793 | // exposes redundancy in the code. |
| 7794 | // |
| 7795 | // Note that we cannot do the transformation unless we know that the |
| 7796 | // introduced loads cannot trap! Something like this is valid as long as |
| 7797 | // the condition is always false: load (select bool %C, int* null, int* %G), |
| 7798 | // but it would not be valid if we transformed it to load from null |
| 7799 | // unconditionally. |
| 7800 | // |
| 7801 | if (SelectInst *SI = dyn_cast<SelectInst>(Op)) { |
| 7802 | // load (select (Cond, &V1, &V2)) --> select(Cond, load &V1, load &V2). |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 7803 | if (isSafeToLoadUnconditionally(SI->getOperand(1), SI) && |
| 7804 | isSafeToLoadUnconditionally(SI->getOperand(2), SI)) { |
Chris Lattner | f925cbd | 2009-08-30 18:50:58 +0000 | [diff] [blame] | 7805 | Value *V1 = Builder->CreateLoad(SI->getOperand(1), |
| 7806 | SI->getOperand(1)->getName()+".val"); |
| 7807 | Value *V2 = Builder->CreateLoad(SI->getOperand(2), |
| 7808 | SI->getOperand(2)->getName()+".val"); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 7809 | return SelectInst::Create(SI->getCondition(), V1, V2); |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 7810 | } |
| 7811 | |
Chris Lattner | 684fe21 | 2004-09-23 15:46:00 +0000 | [diff] [blame] | 7812 | // load (select (cond, null, P)) -> load P |
| 7813 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(1))) |
| 7814 | if (C->isNullValue()) { |
| 7815 | LI.setOperand(0, SI->getOperand(2)); |
| 7816 | return &LI; |
| 7817 | } |
| 7818 | |
| 7819 | // load (select (cond, P, null)) -> load P |
| 7820 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(2))) |
| 7821 | if (C->isNullValue()) { |
| 7822 | LI.setOperand(0, SI->getOperand(1)); |
| 7823 | return &LI; |
| 7824 | } |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 7825 | } |
| 7826 | } |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 7827 | return 0; |
| 7828 | } |
| 7829 | |
Reid Spencer | 55af2b5 | 2007-01-19 21:20:31 +0000 | [diff] [blame] | 7830 | /// InstCombineStoreToCast - Fold store V, (cast P) -> store (cast V), P |
Chris Lattner | 3914f72 | 2009-01-24 01:00:13 +0000 | [diff] [blame] | 7831 | /// when possible. This makes it generally easy to do alias analysis and/or |
| 7832 | /// SROA/mem2reg of the memory object. |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 7833 | static Instruction *InstCombineStoreToCast(InstCombiner &IC, StoreInst &SI) { |
| 7834 | User *CI = cast<User>(SI.getOperand(1)); |
| 7835 | Value *CastOp = CI->getOperand(0); |
| 7836 | |
| 7837 | const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType(); |
Chris Lattner | 1b8eaf5 | 2009-01-16 20:08:59 +0000 | [diff] [blame] | 7838 | const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType()); |
| 7839 | if (SrcTy == 0) return 0; |
| 7840 | |
| 7841 | const Type *SrcPTy = SrcTy->getElementType(); |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 7842 | |
Chris Lattner | 1b8eaf5 | 2009-01-16 20:08:59 +0000 | [diff] [blame] | 7843 | if (!DestPTy->isInteger() && !isa<PointerType>(DestPTy)) |
| 7844 | return 0; |
| 7845 | |
Chris Lattner | 3914f72 | 2009-01-24 01:00:13 +0000 | [diff] [blame] | 7846 | /// NewGEPIndices - If SrcPTy is an aggregate type, we can emit a "noop gep" |
| 7847 | /// to its first element. This allows us to handle things like: |
| 7848 | /// store i32 xxx, (bitcast {foo*, float}* %P to i32*) |
| 7849 | /// on 32-bit hosts. |
| 7850 | SmallVector<Value*, 4> NewGEPIndices; |
| 7851 | |
Chris Lattner | 1b8eaf5 | 2009-01-16 20:08:59 +0000 | [diff] [blame] | 7852 | // If the source is an array, the code below will not succeed. Check to |
| 7853 | // see if a trivial 'gep P, 0, 0' will help matters. Only do this for |
| 7854 | // constants. |
Chris Lattner | 3914f72 | 2009-01-24 01:00:13 +0000 | [diff] [blame] | 7855 | if (isa<ArrayType>(SrcPTy) || isa<StructType>(SrcPTy)) { |
| 7856 | // Index through pointer. |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 7857 | Constant *Zero = Constant::getNullValue(Type::getInt32Ty(SI.getContext())); |
Chris Lattner | 3914f72 | 2009-01-24 01:00:13 +0000 | [diff] [blame] | 7858 | NewGEPIndices.push_back(Zero); |
| 7859 | |
| 7860 | while (1) { |
| 7861 | if (const StructType *STy = dyn_cast<StructType>(SrcPTy)) { |
Torok Edwin | 08ffee5 | 2009-01-24 17:16:04 +0000 | [diff] [blame] | 7862 | if (!STy->getNumElements()) /* Struct can be empty {} */ |
Torok Edwin | 629e92b | 2009-01-24 11:30:49 +0000 | [diff] [blame] | 7863 | break; |
Chris Lattner | 3914f72 | 2009-01-24 01:00:13 +0000 | [diff] [blame] | 7864 | NewGEPIndices.push_back(Zero); |
| 7865 | SrcPTy = STy->getElementType(0); |
| 7866 | } else if (const ArrayType *ATy = dyn_cast<ArrayType>(SrcPTy)) { |
| 7867 | NewGEPIndices.push_back(Zero); |
| 7868 | SrcPTy = ATy->getElementType(); |
| 7869 | } else { |
| 7870 | break; |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 7871 | } |
Chris Lattner | 3914f72 | 2009-01-24 01:00:13 +0000 | [diff] [blame] | 7872 | } |
| 7873 | |
Owen Anderson | debcb01 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 7874 | SrcTy = PointerType::get(SrcPTy, SrcTy->getAddressSpace()); |
Chris Lattner | 3914f72 | 2009-01-24 01:00:13 +0000 | [diff] [blame] | 7875 | } |
Chris Lattner | 1b8eaf5 | 2009-01-16 20:08:59 +0000 | [diff] [blame] | 7876 | |
| 7877 | if (!SrcPTy->isInteger() && !isa<PointerType>(SrcPTy)) |
| 7878 | return 0; |
| 7879 | |
Chris Lattner | 71759c4 | 2009-01-16 20:12:52 +0000 | [diff] [blame] | 7880 | // If the pointers point into different address spaces or if they point to |
| 7881 | // values with different sizes, we can't do the transformation. |
Dan Gohman | ce9fe9f | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 7882 | if (!IC.getTargetData() || |
| 7883 | SrcTy->getAddressSpace() != |
Chris Lattner | 71759c4 | 2009-01-16 20:12:52 +0000 | [diff] [blame] | 7884 | cast<PointerType>(CI->getType())->getAddressSpace() || |
Dan Gohman | ce9fe9f | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 7885 | IC.getTargetData()->getTypeSizeInBits(SrcPTy) != |
| 7886 | IC.getTargetData()->getTypeSizeInBits(DestPTy)) |
Chris Lattner | 1b8eaf5 | 2009-01-16 20:08:59 +0000 | [diff] [blame] | 7887 | return 0; |
| 7888 | |
| 7889 | // Okay, we are casting from one integer or pointer type to another of |
| 7890 | // the same size. Instead of casting the pointer before |
| 7891 | // the store, cast the value to be stored. |
| 7892 | Value *NewCast; |
| 7893 | Value *SIOp0 = SI.getOperand(0); |
| 7894 | Instruction::CastOps opcode = Instruction::BitCast; |
| 7895 | const Type* CastSrcTy = SIOp0->getType(); |
| 7896 | const Type* CastDstTy = SrcPTy; |
| 7897 | if (isa<PointerType>(CastDstTy)) { |
| 7898 | if (CastSrcTy->isInteger()) |
| 7899 | opcode = Instruction::IntToPtr; |
| 7900 | } else if (isa<IntegerType>(CastDstTy)) { |
| 7901 | if (isa<PointerType>(SIOp0->getType())) |
| 7902 | opcode = Instruction::PtrToInt; |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 7903 | } |
Chris Lattner | 3914f72 | 2009-01-24 01:00:13 +0000 | [diff] [blame] | 7904 | |
| 7905 | // SIOp0 is a pointer to aggregate and this is a store to the first field, |
| 7906 | // emit a GEP to index into its first field. |
Dan Gohman | f8dbee7 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 7907 | if (!NewGEPIndices.empty()) |
| 7908 | CastOp = IC.Builder->CreateInBoundsGEP(CastOp, NewGEPIndices.begin(), |
| 7909 | NewGEPIndices.end()); |
Chris Lattner | 3914f72 | 2009-01-24 01:00:13 +0000 | [diff] [blame] | 7910 | |
Chris Lattner | f925cbd | 2009-08-30 18:50:58 +0000 | [diff] [blame] | 7911 | NewCast = IC.Builder->CreateCast(opcode, SIOp0, CastDstTy, |
| 7912 | SIOp0->getName()+".c"); |
Chris Lattner | 1b8eaf5 | 2009-01-16 20:08:59 +0000 | [diff] [blame] | 7913 | return new StoreInst(NewCast, CastOp); |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 7914 | } |
| 7915 | |
Chris Lattner | 4aebaee | 2008-11-27 08:56:30 +0000 | [diff] [blame] | 7916 | /// equivalentAddressValues - Test if A and B will obviously have the same |
| 7917 | /// value. This includes recognizing that %t0 and %t1 will have the same |
| 7918 | /// value in code like this: |
Dan Gohman | 0f8b53f | 2009-03-03 02:55:14 +0000 | [diff] [blame] | 7919 | /// %t0 = getelementptr \@a, 0, 3 |
Chris Lattner | 4aebaee | 2008-11-27 08:56:30 +0000 | [diff] [blame] | 7920 | /// store i32 0, i32* %t0 |
Dan Gohman | 0f8b53f | 2009-03-03 02:55:14 +0000 | [diff] [blame] | 7921 | /// %t1 = getelementptr \@a, 0, 3 |
Chris Lattner | 4aebaee | 2008-11-27 08:56:30 +0000 | [diff] [blame] | 7922 | /// %t2 = load i32* %t1 |
| 7923 | /// |
| 7924 | static bool equivalentAddressValues(Value *A, Value *B) { |
| 7925 | // Test if the values are trivially equivalent. |
| 7926 | if (A == B) return true; |
| 7927 | |
| 7928 | // Test if the values come form identical arithmetic instructions. |
Dan Gohman | 58cfa3b | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 7929 | // This uses isIdenticalToWhenDefined instead of isIdenticalTo because |
| 7930 | // its only used to compare two uses within the same basic block, which |
| 7931 | // means that they'll always either have the same value or one of them |
| 7932 | // will have an undefined value. |
Chris Lattner | 4aebaee | 2008-11-27 08:56:30 +0000 | [diff] [blame] | 7933 | if (isa<BinaryOperator>(A) || |
| 7934 | isa<CastInst>(A) || |
| 7935 | isa<PHINode>(A) || |
| 7936 | isa<GetElementPtrInst>(A)) |
| 7937 | if (Instruction *BI = dyn_cast<Instruction>(B)) |
Dan Gohman | 58cfa3b | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 7938 | if (cast<Instruction>(A)->isIdenticalToWhenDefined(BI)) |
Chris Lattner | 4aebaee | 2008-11-27 08:56:30 +0000 | [diff] [blame] | 7939 | return true; |
| 7940 | |
| 7941 | // Otherwise they may not be equivalent. |
| 7942 | return false; |
| 7943 | } |
| 7944 | |
Dale Johannesen | 4945c65 | 2009-03-03 21:26:39 +0000 | [diff] [blame] | 7945 | // If this instruction has two uses, one of which is a llvm.dbg.declare, |
| 7946 | // return the llvm.dbg.declare. |
| 7947 | DbgDeclareInst *InstCombiner::hasOneUsePlusDeclare(Value *V) { |
| 7948 | if (!V->hasNUses(2)) |
| 7949 | return 0; |
| 7950 | for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); |
| 7951 | UI != E; ++UI) { |
| 7952 | if (DbgDeclareInst *DI = dyn_cast<DbgDeclareInst>(UI)) |
| 7953 | return DI; |
| 7954 | if (isa<BitCastInst>(UI) && UI->hasOneUse()) { |
| 7955 | if (DbgDeclareInst *DI = dyn_cast<DbgDeclareInst>(UI->use_begin())) |
| 7956 | return DI; |
| 7957 | } |
| 7958 | } |
| 7959 | return 0; |
| 7960 | } |
| 7961 | |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 7962 | Instruction *InstCombiner::visitStoreInst(StoreInst &SI) { |
| 7963 | Value *Val = SI.getOperand(0); |
| 7964 | Value *Ptr = SI.getOperand(1); |
| 7965 | |
Chris Lattner | 836692d | 2007-01-15 06:51:56 +0000 | [diff] [blame] | 7966 | // If the RHS is an alloca with a single use, zapify the store, making the |
| 7967 | // alloca dead. |
Dale Johannesen | 4945c65 | 2009-03-03 21:26:39 +0000 | [diff] [blame] | 7968 | // If the RHS is an alloca with a two uses, the other one being a |
| 7969 | // llvm.dbg.declare, zapify the store and the declare, making the |
| 7970 | // alloca dead. We must do this to prevent declare's from affecting |
| 7971 | // codegen. |
| 7972 | if (!SI.isVolatile()) { |
| 7973 | if (Ptr->hasOneUse()) { |
| 7974 | if (isa<AllocaInst>(Ptr)) { |
Chris Lattner | 836692d | 2007-01-15 06:51:56 +0000 | [diff] [blame] | 7975 | EraseInstFromFunction(SI); |
| 7976 | ++NumCombined; |
| 7977 | return 0; |
| 7978 | } |
Dale Johannesen | 4945c65 | 2009-03-03 21:26:39 +0000 | [diff] [blame] | 7979 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) { |
| 7980 | if (isa<AllocaInst>(GEP->getOperand(0))) { |
| 7981 | if (GEP->getOperand(0)->hasOneUse()) { |
| 7982 | EraseInstFromFunction(SI); |
| 7983 | ++NumCombined; |
| 7984 | return 0; |
| 7985 | } |
| 7986 | if (DbgDeclareInst *DI = hasOneUsePlusDeclare(GEP->getOperand(0))) { |
| 7987 | EraseInstFromFunction(*DI); |
| 7988 | EraseInstFromFunction(SI); |
| 7989 | ++NumCombined; |
| 7990 | return 0; |
| 7991 | } |
| 7992 | } |
| 7993 | } |
| 7994 | } |
| 7995 | if (DbgDeclareInst *DI = hasOneUsePlusDeclare(Ptr)) { |
| 7996 | EraseInstFromFunction(*DI); |
| 7997 | EraseInstFromFunction(SI); |
| 7998 | ++NumCombined; |
| 7999 | return 0; |
| 8000 | } |
Chris Lattner | 836692d | 2007-01-15 06:51:56 +0000 | [diff] [blame] | 8001 | } |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 8002 | |
Dan Gohman | 9941f74 | 2007-07-20 16:34:21 +0000 | [diff] [blame] | 8003 | // Attempt to improve the alignment. |
Dan Gohman | ce9fe9f | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 8004 | if (TD) { |
| 8005 | unsigned KnownAlign = |
| 8006 | GetOrEnforceKnownAlignment(Ptr, TD->getPrefTypeAlignment(Val->getType())); |
| 8007 | if (KnownAlign > |
| 8008 | (SI.getAlignment() == 0 ? TD->getABITypeAlignment(Val->getType()) : |
| 8009 | SI.getAlignment())) |
| 8010 | SI.setAlignment(KnownAlign); |
| 8011 | } |
Dan Gohman | 9941f74 | 2007-07-20 16:34:21 +0000 | [diff] [blame] | 8012 | |
Dale Johannesen | acb51a3 | 2009-03-03 01:43:03 +0000 | [diff] [blame] | 8013 | // Do really simple DSE, to catch cases where there are several consecutive |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 8014 | // stores to the same location, separated by a few arithmetic operations. This |
| 8015 | // situation often occurs with bitfield accesses. |
| 8016 | BasicBlock::iterator BBI = &SI; |
| 8017 | for (unsigned ScanInsts = 6; BBI != SI.getParent()->begin() && ScanInsts; |
| 8018 | --ScanInsts) { |
Dale Johannesen | 0d6596b | 2009-03-04 01:20:34 +0000 | [diff] [blame] | 8019 | --BBI; |
Dale Johannesen | cdb16aa | 2009-03-04 01:53:05 +0000 | [diff] [blame] | 8020 | // Don't count debug info directives, lest they affect codegen, |
| 8021 | // and we skip pointer-to-pointer bitcasts, which are NOPs. |
| 8022 | // It is necessary for correctness to skip those that feed into a |
| 8023 | // llvm.dbg.declare, as these are not present when debugging is off. |
Dale Johannesen | 4ded40a | 2009-03-03 22:36:47 +0000 | [diff] [blame] | 8024 | if (isa<DbgInfoIntrinsic>(BBI) || |
Dale Johannesen | cdb16aa | 2009-03-04 01:53:05 +0000 | [diff] [blame] | 8025 | (isa<BitCastInst>(BBI) && isa<PointerType>(BBI->getType()))) { |
Dale Johannesen | acb51a3 | 2009-03-03 01:43:03 +0000 | [diff] [blame] | 8026 | ScanInsts++; |
Dale Johannesen | acb51a3 | 2009-03-03 01:43:03 +0000 | [diff] [blame] | 8027 | continue; |
| 8028 | } |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 8029 | |
| 8030 | if (StoreInst *PrevSI = dyn_cast<StoreInst>(BBI)) { |
| 8031 | // Prev store isn't volatile, and stores to the same location? |
Chris Lattner | 4aebaee | 2008-11-27 08:56:30 +0000 | [diff] [blame] | 8032 | if (!PrevSI->isVolatile() &&equivalentAddressValues(PrevSI->getOperand(1), |
| 8033 | SI.getOperand(1))) { |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 8034 | ++NumDeadStore; |
| 8035 | ++BBI; |
| 8036 | EraseInstFromFunction(*PrevSI); |
| 8037 | continue; |
| 8038 | } |
| 8039 | break; |
| 8040 | } |
| 8041 | |
Chris Lattner | b4db97f | 2006-05-26 19:19:20 +0000 | [diff] [blame] | 8042 | // If this is a load, we have to stop. However, if the loaded value is from |
| 8043 | // the pointer we're loading and is producing the pointer we're storing, |
| 8044 | // then *this* store is dead (X = load P; store X -> P). |
| 8045 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { |
Dan Gohman | 2276a7b | 2008-10-15 23:19:35 +0000 | [diff] [blame] | 8046 | if (LI == Val && equivalentAddressValues(LI->getOperand(0), Ptr) && |
| 8047 | !SI.isVolatile()) { |
Chris Lattner | b4db97f | 2006-05-26 19:19:20 +0000 | [diff] [blame] | 8048 | EraseInstFromFunction(SI); |
| 8049 | ++NumCombined; |
| 8050 | return 0; |
| 8051 | } |
| 8052 | // Otherwise, this is a load from some other location. Stores before it |
| 8053 | // may not be dead. |
| 8054 | break; |
| 8055 | } |
| 8056 | |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 8057 | // Don't skip over loads or things that can modify memory. |
Chris Lattner | 0ef546e | 2008-05-08 17:20:30 +0000 | [diff] [blame] | 8058 | if (BBI->mayWriteToMemory() || BBI->mayReadFromMemory()) |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 8059 | break; |
| 8060 | } |
| 8061 | |
| 8062 | |
| 8063 | if (SI.isVolatile()) return 0; // Don't hack volatile stores. |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 8064 | |
| 8065 | // store X, null -> turns into 'unreachable' in SimplifyCFG |
Chris Lattner | 8a67ac5 | 2009-08-30 20:06:40 +0000 | [diff] [blame] | 8066 | if (isa<ConstantPointerNull>(Ptr) && SI.getPointerAddressSpace() == 0) { |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 8067 | if (!isa<UndefValue>(Val)) { |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 8068 | SI.setOperand(0, UndefValue::get(Val->getType())); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 8069 | if (Instruction *U = dyn_cast<Instruction>(Val)) |
Chris Lattner | 7a1e924 | 2009-08-30 06:13:40 +0000 | [diff] [blame] | 8070 | Worklist.Add(U); // Dropped a use. |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 8071 | ++NumCombined; |
| 8072 | } |
| 8073 | return 0; // Do not modify these! |
| 8074 | } |
| 8075 | |
| 8076 | // store undef, Ptr -> noop |
| 8077 | if (isa<UndefValue>(Val)) { |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 8078 | EraseInstFromFunction(SI); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 8079 | ++NumCombined; |
| 8080 | return 0; |
| 8081 | } |
| 8082 | |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8083 | // If the pointer destination is a cast, see if we can fold the cast into the |
| 8084 | // source instead. |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 8085 | if (isa<CastInst>(Ptr)) |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8086 | if (Instruction *Res = InstCombineStoreToCast(*this, SI)) |
| 8087 | return Res; |
| 8088 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8089 | if (CE->isCast()) |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8090 | if (Instruction *Res = InstCombineStoreToCast(*this, SI)) |
| 8091 | return Res; |
| 8092 | |
Chris Lattner | 408902b | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 8093 | |
Dale Johannesen | 4084c4e | 2009-03-05 02:06:48 +0000 | [diff] [blame] | 8094 | // If this store is the last instruction in the basic block (possibly |
| 8095 | // excepting debug info instructions and the pointer bitcasts that feed |
| 8096 | // into them), and if the block ends with an unconditional branch, try |
| 8097 | // to move it to the successor block. |
| 8098 | BBI = &SI; |
| 8099 | do { |
| 8100 | ++BBI; |
| 8101 | } while (isa<DbgInfoIntrinsic>(BBI) || |
| 8102 | (isa<BitCastInst>(BBI) && isa<PointerType>(BBI->getType()))); |
Chris Lattner | 408902b | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 8103 | if (BranchInst *BI = dyn_cast<BranchInst>(BBI)) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 8104 | if (BI->isUnconditional()) |
| 8105 | if (SimplifyStoreAtEndOfBlock(SI)) |
| 8106 | return 0; // xform done! |
Chris Lattner | 408902b | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 8107 | |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 8108 | return 0; |
| 8109 | } |
| 8110 | |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 8111 | /// SimplifyStoreAtEndOfBlock - Turn things like: |
| 8112 | /// if () { *P = v1; } else { *P = v2 } |
| 8113 | /// into a phi node with a store in the successor. |
| 8114 | /// |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 8115 | /// Simplify things like: |
| 8116 | /// *P = v1; if () { *P = v2; } |
| 8117 | /// into a phi node with a store in the successor. |
| 8118 | /// |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 8119 | bool InstCombiner::SimplifyStoreAtEndOfBlock(StoreInst &SI) { |
| 8120 | BasicBlock *StoreBB = SI.getParent(); |
| 8121 | |
| 8122 | // Check to see if the successor block has exactly two incoming edges. If |
| 8123 | // so, see if the other predecessor contains a store to the same location. |
| 8124 | // if so, insert a PHI node (if needed) and move the stores down. |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 8125 | BasicBlock *DestBB = StoreBB->getTerminator()->getSuccessor(0); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 8126 | |
| 8127 | // Determine whether Dest has exactly two predecessors and, if so, compute |
| 8128 | // the other predecessor. |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 8129 | pred_iterator PI = pred_begin(DestBB); |
| 8130 | BasicBlock *OtherBB = 0; |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 8131 | if (*PI != StoreBB) |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 8132 | OtherBB = *PI; |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 8133 | ++PI; |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 8134 | if (PI == pred_end(DestBB)) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 8135 | return false; |
| 8136 | |
| 8137 | if (*PI != StoreBB) { |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 8138 | if (OtherBB) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 8139 | return false; |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 8140 | OtherBB = *PI; |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 8141 | } |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 8142 | if (++PI != pred_end(DestBB)) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 8143 | return false; |
Eli Friedman | 66fe80a | 2008-06-13 21:17:49 +0000 | [diff] [blame] | 8144 | |
| 8145 | // Bail out if all the relevant blocks aren't distinct (this can happen, |
| 8146 | // for example, if SI is in an infinite loop) |
| 8147 | if (StoreBB == DestBB || OtherBB == DestBB) |
| 8148 | return false; |
| 8149 | |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 8150 | // Verify that the other block ends in a branch and is not otherwise empty. |
| 8151 | BasicBlock::iterator BBI = OtherBB->getTerminator(); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 8152 | BranchInst *OtherBr = dyn_cast<BranchInst>(BBI); |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 8153 | if (!OtherBr || BBI == OtherBB->begin()) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 8154 | return false; |
| 8155 | |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 8156 | // If the other block ends in an unconditional branch, check for the 'if then |
| 8157 | // else' case. there is an instruction before the branch. |
| 8158 | StoreInst *OtherStore = 0; |
| 8159 | if (OtherBr->isUnconditional()) { |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 8160 | --BBI; |
Dale Johannesen | 4084c4e | 2009-03-05 02:06:48 +0000 | [diff] [blame] | 8161 | // Skip over debugging info. |
| 8162 | while (isa<DbgInfoIntrinsic>(BBI) || |
| 8163 | (isa<BitCastInst>(BBI) && isa<PointerType>(BBI->getType()))) { |
| 8164 | if (BBI==OtherBB->begin()) |
| 8165 | return false; |
| 8166 | --BBI; |
| 8167 | } |
Chris Lattner | 7ebbabf | 2009-11-02 02:06:37 +0000 | [diff] [blame] | 8168 | // If this isn't a store, isn't a store to the same location, or if the |
| 8169 | // alignments differ, bail out. |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 8170 | OtherStore = dyn_cast<StoreInst>(BBI); |
Chris Lattner | 7ebbabf | 2009-11-02 02:06:37 +0000 | [diff] [blame] | 8171 | if (!OtherStore || OtherStore->getOperand(1) != SI.getOperand(1) || |
| 8172 | OtherStore->getAlignment() != SI.getAlignment()) |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 8173 | return false; |
| 8174 | } else { |
Chris Lattner | d717c18 | 2007-05-05 22:32:24 +0000 | [diff] [blame] | 8175 | // Otherwise, the other block ended with a conditional branch. If one of the |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 8176 | // destinations is StoreBB, then we have the if/then case. |
| 8177 | if (OtherBr->getSuccessor(0) != StoreBB && |
| 8178 | OtherBr->getSuccessor(1) != StoreBB) |
| 8179 | return false; |
| 8180 | |
| 8181 | // Okay, we know that OtherBr now goes to Dest and StoreBB, so this is an |
Chris Lattner | d717c18 | 2007-05-05 22:32:24 +0000 | [diff] [blame] | 8182 | // if/then triangle. See if there is a store to the same ptr as SI that |
| 8183 | // lives in OtherBB. |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 8184 | for (;; --BBI) { |
| 8185 | // Check to see if we find the matching store. |
| 8186 | if ((OtherStore = dyn_cast<StoreInst>(BBI))) { |
Chris Lattner | 7ebbabf | 2009-11-02 02:06:37 +0000 | [diff] [blame] | 8187 | if (OtherStore->getOperand(1) != SI.getOperand(1) || |
| 8188 | OtherStore->getAlignment() != SI.getAlignment()) |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 8189 | return false; |
| 8190 | break; |
| 8191 | } |
Eli Friedman | 6903a24 | 2008-06-13 22:02:12 +0000 | [diff] [blame] | 8192 | // If we find something that may be using or overwriting the stored |
| 8193 | // value, or if we run out of instructions, we can't do the xform. |
| 8194 | if (BBI->mayReadFromMemory() || BBI->mayWriteToMemory() || |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 8195 | BBI == OtherBB->begin()) |
| 8196 | return false; |
| 8197 | } |
| 8198 | |
| 8199 | // In order to eliminate the store in OtherBr, we have to |
Eli Friedman | 6903a24 | 2008-06-13 22:02:12 +0000 | [diff] [blame] | 8200 | // make sure nothing reads or overwrites the stored value in |
| 8201 | // StoreBB. |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 8202 | for (BasicBlock::iterator I = StoreBB->begin(); &*I != &SI; ++I) { |
| 8203 | // FIXME: This should really be AA driven. |
Eli Friedman | 6903a24 | 2008-06-13 22:02:12 +0000 | [diff] [blame] | 8204 | if (I->mayReadFromMemory() || I->mayWriteToMemory()) |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 8205 | return false; |
| 8206 | } |
| 8207 | } |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 8208 | |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 8209 | // Insert a PHI node now if we need it. |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 8210 | Value *MergedVal = OtherStore->getOperand(0); |
| 8211 | if (MergedVal != SI.getOperand(0)) { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 8212 | PHINode *PN = PHINode::Create(MergedVal->getType(), "storemerge"); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 8213 | PN->reserveOperandSpace(2); |
| 8214 | PN->addIncoming(SI.getOperand(0), SI.getParent()); |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 8215 | PN->addIncoming(OtherStore->getOperand(0), OtherBB); |
| 8216 | MergedVal = InsertNewInstBefore(PN, DestBB->front()); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 8217 | } |
| 8218 | |
| 8219 | // Advance to a place where it is safe to insert the new store and |
| 8220 | // insert it. |
Dan Gohman | 02dea8b | 2008-05-23 21:05:58 +0000 | [diff] [blame] | 8221 | BBI = DestBB->getFirstNonPHI(); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 8222 | InsertNewInstBefore(new StoreInst(MergedVal, SI.getOperand(1), |
Chris Lattner | 7ebbabf | 2009-11-02 02:06:37 +0000 | [diff] [blame] | 8223 | OtherStore->isVolatile(), |
| 8224 | SI.getAlignment()), *BBI); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 8225 | |
| 8226 | // Nuke the old stores. |
| 8227 | EraseInstFromFunction(SI); |
| 8228 | EraseInstFromFunction(*OtherStore); |
| 8229 | ++NumCombined; |
| 8230 | return true; |
| 8231 | } |
| 8232 | |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 8233 | |
Chris Lattner | c4d10eb | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 8234 | Instruction *InstCombiner::visitBranchInst(BranchInst &BI) { |
| 8235 | // 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] | 8236 | Value *X = 0; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 8237 | BasicBlock *TrueDest; |
| 8238 | BasicBlock *FalseDest; |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 8239 | if (match(&BI, m_Br(m_Not(m_Value(X)), TrueDest, FalseDest)) && |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 8240 | !isa<Constant>(X)) { |
| 8241 | // Swap Destinations and condition... |
| 8242 | BI.setCondition(X); |
| 8243 | BI.setSuccessor(0, FalseDest); |
| 8244 | BI.setSuccessor(1, TrueDest); |
| 8245 | return &BI; |
| 8246 | } |
| 8247 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8248 | // Cannonicalize fcmp_one -> fcmp_oeq |
| 8249 | FCmpInst::Predicate FPred; Value *Y; |
| 8250 | 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] | 8251 | TrueDest, FalseDest)) && |
| 8252 | BI.getCondition()->hasOneUse()) |
| 8253 | if (FPred == FCmpInst::FCMP_ONE || FPred == FCmpInst::FCMP_OLE || |
| 8254 | FPred == FCmpInst::FCMP_OGE) { |
| 8255 | FCmpInst *Cond = cast<FCmpInst>(BI.getCondition()); |
| 8256 | Cond->setPredicate(FCmpInst::getInversePredicate(FPred)); |
| 8257 | |
| 8258 | // Swap Destinations and condition. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8259 | BI.setSuccessor(0, FalseDest); |
| 8260 | BI.setSuccessor(1, TrueDest); |
Chris Lattner | 7a1e924 | 2009-08-30 06:13:40 +0000 | [diff] [blame] | 8261 | Worklist.Add(Cond); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8262 | return &BI; |
| 8263 | } |
| 8264 | |
| 8265 | // Cannonicalize icmp_ne -> icmp_eq |
| 8266 | ICmpInst::Predicate IPred; |
| 8267 | 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] | 8268 | TrueDest, FalseDest)) && |
| 8269 | BI.getCondition()->hasOneUse()) |
| 8270 | if (IPred == ICmpInst::ICMP_NE || IPred == ICmpInst::ICMP_ULE || |
| 8271 | IPred == ICmpInst::ICMP_SLE || IPred == ICmpInst::ICMP_UGE || |
| 8272 | IPred == ICmpInst::ICMP_SGE) { |
| 8273 | ICmpInst *Cond = cast<ICmpInst>(BI.getCondition()); |
| 8274 | Cond->setPredicate(ICmpInst::getInversePredicate(IPred)); |
| 8275 | // Swap Destinations and condition. |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 8276 | BI.setSuccessor(0, FalseDest); |
| 8277 | BI.setSuccessor(1, TrueDest); |
Chris Lattner | 7a1e924 | 2009-08-30 06:13:40 +0000 | [diff] [blame] | 8278 | Worklist.Add(Cond); |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 8279 | return &BI; |
| 8280 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8281 | |
Chris Lattner | c4d10eb | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 8282 | return 0; |
| 8283 | } |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 8284 | |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 8285 | Instruction *InstCombiner::visitSwitchInst(SwitchInst &SI) { |
| 8286 | Value *Cond = SI.getCondition(); |
| 8287 | if (Instruction *I = dyn_cast<Instruction>(Cond)) { |
| 8288 | if (I->getOpcode() == Instruction::Add) |
| 8289 | if (ConstantInt *AddRHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 8290 | // change 'switch (X+4) case 1:' into 'switch (X) case -3' |
| 8291 | for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2) |
Owen Anderson | d672ecb | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 8292 | SI.setOperand(i, |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 8293 | ConstantExpr::getSub(cast<Constant>(SI.getOperand(i)), |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 8294 | AddRHS)); |
| 8295 | SI.setOperand(0, I->getOperand(0)); |
Chris Lattner | 7a1e924 | 2009-08-30 06:13:40 +0000 | [diff] [blame] | 8296 | Worklist.Add(I); |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 8297 | return &SI; |
| 8298 | } |
| 8299 | } |
| 8300 | return 0; |
| 8301 | } |
| 8302 | |
Matthijs Kooijman | a9012ec | 2008-06-11 14:05:05 +0000 | [diff] [blame] | 8303 | Instruction *InstCombiner::visitExtractValueInst(ExtractValueInst &EV) { |
Matthijs Kooijman | 780ae5e | 2008-07-16 12:55:45 +0000 | [diff] [blame] | 8304 | Value *Agg = EV.getAggregateOperand(); |
Matthijs Kooijman | a9012ec | 2008-06-11 14:05:05 +0000 | [diff] [blame] | 8305 | |
Matthijs Kooijman | 780ae5e | 2008-07-16 12:55:45 +0000 | [diff] [blame] | 8306 | if (!EV.hasIndices()) |
| 8307 | return ReplaceInstUsesWith(EV, Agg); |
| 8308 | |
| 8309 | if (Constant *C = dyn_cast<Constant>(Agg)) { |
| 8310 | if (isa<UndefValue>(C)) |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 8311 | return ReplaceInstUsesWith(EV, UndefValue::get(EV.getType())); |
Matthijs Kooijman | 780ae5e | 2008-07-16 12:55:45 +0000 | [diff] [blame] | 8312 | |
| 8313 | if (isa<ConstantAggregateZero>(C)) |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 8314 | return ReplaceInstUsesWith(EV, Constant::getNullValue(EV.getType())); |
Matthijs Kooijman | 780ae5e | 2008-07-16 12:55:45 +0000 | [diff] [blame] | 8315 | |
| 8316 | if (isa<ConstantArray>(C) || isa<ConstantStruct>(C)) { |
| 8317 | // Extract the element indexed by the first index out of the constant |
| 8318 | Value *V = C->getOperand(*EV.idx_begin()); |
| 8319 | if (EV.getNumIndices() > 1) |
| 8320 | // Extract the remaining indices out of the constant indexed by the |
| 8321 | // first index |
| 8322 | return ExtractValueInst::Create(V, EV.idx_begin() + 1, EV.idx_end()); |
| 8323 | else |
| 8324 | return ReplaceInstUsesWith(EV, V); |
| 8325 | } |
| 8326 | return 0; // Can't handle other constants |
| 8327 | } |
| 8328 | if (InsertValueInst *IV = dyn_cast<InsertValueInst>(Agg)) { |
| 8329 | // We're extracting from an insertvalue instruction, compare the indices |
| 8330 | const unsigned *exti, *exte, *insi, *inse; |
| 8331 | for (exti = EV.idx_begin(), insi = IV->idx_begin(), |
| 8332 | exte = EV.idx_end(), inse = IV->idx_end(); |
| 8333 | exti != exte && insi != inse; |
| 8334 | ++exti, ++insi) { |
| 8335 | if (*insi != *exti) |
| 8336 | // The insert and extract both reference distinctly different elements. |
| 8337 | // This means the extract is not influenced by the insert, and we can |
| 8338 | // replace the aggregate operand of the extract with the aggregate |
| 8339 | // operand of the insert. i.e., replace |
| 8340 | // %I = insertvalue { i32, { i32 } } %A, { i32 } { i32 42 }, 1 |
| 8341 | // %E = extractvalue { i32, { i32 } } %I, 0 |
| 8342 | // with |
| 8343 | // %E = extractvalue { i32, { i32 } } %A, 0 |
| 8344 | return ExtractValueInst::Create(IV->getAggregateOperand(), |
| 8345 | EV.idx_begin(), EV.idx_end()); |
| 8346 | } |
| 8347 | if (exti == exte && insi == inse) |
| 8348 | // Both iterators are at the end: Index lists are identical. Replace |
| 8349 | // %B = insertvalue { i32, { i32 } } %A, i32 42, 1, 0 |
| 8350 | // %C = extractvalue { i32, { i32 } } %B, 1, 0 |
| 8351 | // with "i32 42" |
| 8352 | return ReplaceInstUsesWith(EV, IV->getInsertedValueOperand()); |
| 8353 | if (exti == exte) { |
| 8354 | // The extract list is a prefix of the insert list. i.e. replace |
| 8355 | // %I = insertvalue { i32, { i32 } } %A, i32 42, 1, 0 |
| 8356 | // %E = extractvalue { i32, { i32 } } %I, 1 |
| 8357 | // with |
| 8358 | // %X = extractvalue { i32, { i32 } } %A, 1 |
| 8359 | // %E = insertvalue { i32 } %X, i32 42, 0 |
| 8360 | // by switching the order of the insert and extract (though the |
| 8361 | // insertvalue should be left in, since it may have other uses). |
Chris Lattner | f925cbd | 2009-08-30 18:50:58 +0000 | [diff] [blame] | 8362 | Value *NewEV = Builder->CreateExtractValue(IV->getAggregateOperand(), |
| 8363 | EV.idx_begin(), EV.idx_end()); |
Matthijs Kooijman | 780ae5e | 2008-07-16 12:55:45 +0000 | [diff] [blame] | 8364 | return InsertValueInst::Create(NewEV, IV->getInsertedValueOperand(), |
| 8365 | insi, inse); |
| 8366 | } |
| 8367 | if (insi == inse) |
| 8368 | // The insert list is a prefix of the extract list |
| 8369 | // We can simply remove the common indices from the extract and make it |
| 8370 | // operate on the inserted value instead of the insertvalue result. |
| 8371 | // i.e., replace |
| 8372 | // %I = insertvalue { i32, { i32 } } %A, { i32 } { i32 42 }, 1 |
| 8373 | // %E = extractvalue { i32, { i32 } } %I, 1, 0 |
| 8374 | // with |
| 8375 | // %E extractvalue { i32 } { i32 42 }, 0 |
| 8376 | return ExtractValueInst::Create(IV->getInsertedValueOperand(), |
| 8377 | exti, exte); |
| 8378 | } |
Chris Lattner | 7e606e2 | 2009-11-09 07:07:56 +0000 | [diff] [blame] | 8379 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Agg)) { |
| 8380 | // We're extracting from an intrinsic, see if we're the only user, which |
| 8381 | // allows us to simplify multiple result intrinsics to simpler things that |
| 8382 | // just get one value.. |
| 8383 | if (II->hasOneUse()) { |
| 8384 | // Check if we're grabbing the overflow bit or the result of a 'with |
| 8385 | // overflow' intrinsic. If it's the latter we can remove the intrinsic |
| 8386 | // and replace it with a traditional binary instruction. |
| 8387 | switch (II->getIntrinsicID()) { |
| 8388 | case Intrinsic::uadd_with_overflow: |
| 8389 | case Intrinsic::sadd_with_overflow: |
| 8390 | if (*EV.idx_begin() == 0) { // Normal result. |
| 8391 | Value *LHS = II->getOperand(1), *RHS = II->getOperand(2); |
| 8392 | II->replaceAllUsesWith(UndefValue::get(II->getType())); |
| 8393 | EraseInstFromFunction(*II); |
| 8394 | return BinaryOperator::CreateAdd(LHS, RHS); |
| 8395 | } |
| 8396 | break; |
| 8397 | case Intrinsic::usub_with_overflow: |
| 8398 | case Intrinsic::ssub_with_overflow: |
| 8399 | if (*EV.idx_begin() == 0) { // Normal result. |
| 8400 | Value *LHS = II->getOperand(1), *RHS = II->getOperand(2); |
| 8401 | II->replaceAllUsesWith(UndefValue::get(II->getType())); |
| 8402 | EraseInstFromFunction(*II); |
| 8403 | return BinaryOperator::CreateSub(LHS, RHS); |
| 8404 | } |
| 8405 | break; |
| 8406 | case Intrinsic::umul_with_overflow: |
| 8407 | case Intrinsic::smul_with_overflow: |
| 8408 | if (*EV.idx_begin() == 0) { // Normal result. |
| 8409 | Value *LHS = II->getOperand(1), *RHS = II->getOperand(2); |
| 8410 | II->replaceAllUsesWith(UndefValue::get(II->getType())); |
| 8411 | EraseInstFromFunction(*II); |
| 8412 | return BinaryOperator::CreateMul(LHS, RHS); |
| 8413 | } |
| 8414 | break; |
| 8415 | default: |
| 8416 | break; |
| 8417 | } |
| 8418 | } |
| 8419 | } |
Matthijs Kooijman | 780ae5e | 2008-07-16 12:55:45 +0000 | [diff] [blame] | 8420 | // Can't simplify extracts from other values. Note that nested extracts are |
| 8421 | // already simplified implicitely by the above (extract ( extract (insert) ) |
| 8422 | // will be translated into extract ( insert ( extract ) ) first and then just |
| 8423 | // the value inserted, if appropriate). |
Matthijs Kooijman | a9012ec | 2008-06-11 14:05:05 +0000 | [diff] [blame] | 8424 | return 0; |
| 8425 | } |
| 8426 | |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 8427 | /// CheapToScalarize - Return true if the value is cheaper to scalarize than it |
| 8428 | /// is to leave as a vector operation. |
| 8429 | static bool CheapToScalarize(Value *V, bool isConstant) { |
| 8430 | if (isa<ConstantAggregateZero>(V)) |
| 8431 | return true; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 8432 | if (ConstantVector *C = dyn_cast<ConstantVector>(V)) { |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 8433 | if (isConstant) return true; |
| 8434 | // If all elts are the same, we can extract. |
| 8435 | Constant *Op0 = C->getOperand(0); |
| 8436 | for (unsigned i = 1; i < C->getNumOperands(); ++i) |
| 8437 | if (C->getOperand(i) != Op0) |
| 8438 | return false; |
| 8439 | return true; |
| 8440 | } |
| 8441 | Instruction *I = dyn_cast<Instruction>(V); |
| 8442 | if (!I) return false; |
| 8443 | |
| 8444 | // Insert element gets simplified to the inserted element or is deleted if |
| 8445 | // this is constant idx extract element and its a constant idx insertelt. |
| 8446 | if (I->getOpcode() == Instruction::InsertElement && isConstant && |
| 8447 | isa<ConstantInt>(I->getOperand(2))) |
| 8448 | return true; |
| 8449 | if (I->getOpcode() == Instruction::Load && I->hasOneUse()) |
| 8450 | return true; |
| 8451 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) |
| 8452 | if (BO->hasOneUse() && |
| 8453 | (CheapToScalarize(BO->getOperand(0), isConstant) || |
| 8454 | CheapToScalarize(BO->getOperand(1), isConstant))) |
| 8455 | return true; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8456 | if (CmpInst *CI = dyn_cast<CmpInst>(I)) |
| 8457 | if (CI->hasOneUse() && |
| 8458 | (CheapToScalarize(CI->getOperand(0), isConstant) || |
| 8459 | CheapToScalarize(CI->getOperand(1), isConstant))) |
| 8460 | return true; |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 8461 | |
| 8462 | return false; |
| 8463 | } |
| 8464 | |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 8465 | /// Read and decode a shufflevector mask. |
| 8466 | /// |
| 8467 | /// It turns undef elements into values that are larger than the number of |
| 8468 | /// elements in the input. |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8469 | static std::vector<unsigned> getShuffleMask(const ShuffleVectorInst *SVI) { |
| 8470 | unsigned NElts = SVI->getType()->getNumElements(); |
| 8471 | if (isa<ConstantAggregateZero>(SVI->getOperand(2))) |
| 8472 | return std::vector<unsigned>(NElts, 0); |
| 8473 | if (isa<UndefValue>(SVI->getOperand(2))) |
| 8474 | return std::vector<unsigned>(NElts, 2*NElts); |
| 8475 | |
| 8476 | std::vector<unsigned> Result; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 8477 | const ConstantVector *CP = cast<ConstantVector>(SVI->getOperand(2)); |
Gabor Greif | 177dd3f | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 8478 | for (User::const_op_iterator i = CP->op_begin(), e = CP->op_end(); i!=e; ++i) |
| 8479 | if (isa<UndefValue>(*i)) |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8480 | Result.push_back(NElts*2); // undef -> 8 |
| 8481 | else |
Gabor Greif | 177dd3f | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 8482 | Result.push_back(cast<ConstantInt>(*i)->getZExtValue()); |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8483 | return Result; |
| 8484 | } |
| 8485 | |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8486 | /// FindScalarElement - Given a vector and an element number, see if the scalar |
| 8487 | /// value is already around as a register, for example if it were inserted then |
| 8488 | /// extracted from the vector. |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 8489 | static Value *FindScalarElement(Value *V, unsigned EltNo) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 8490 | assert(isa<VectorType>(V->getType()) && "Not looking at a vector?"); |
| 8491 | const VectorType *PTy = cast<VectorType>(V->getType()); |
Chris Lattner | 389a6f5 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 8492 | unsigned Width = PTy->getNumElements(); |
| 8493 | if (EltNo >= Width) // Out of range access. |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 8494 | return UndefValue::get(PTy->getElementType()); |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8495 | |
| 8496 | if (isa<UndefValue>(V)) |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 8497 | return UndefValue::get(PTy->getElementType()); |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8498 | else if (isa<ConstantAggregateZero>(V)) |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 8499 | return Constant::getNullValue(PTy->getElementType()); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 8500 | else if (ConstantVector *CP = dyn_cast<ConstantVector>(V)) |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8501 | return CP->getOperand(EltNo); |
| 8502 | else if (InsertElementInst *III = dyn_cast<InsertElementInst>(V)) { |
| 8503 | // If this is an insert to a variable element, we don't know what it is. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8504 | if (!isa<ConstantInt>(III->getOperand(2))) |
| 8505 | return 0; |
| 8506 | unsigned IIElt = cast<ConstantInt>(III->getOperand(2))->getZExtValue(); |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8507 | |
| 8508 | // If this is an insert to the element we are looking for, return the |
| 8509 | // inserted value. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8510 | if (EltNo == IIElt) |
| 8511 | return III->getOperand(1); |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8512 | |
| 8513 | // Otherwise, the insertelement doesn't modify the value, recurse on its |
| 8514 | // vector input. |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 8515 | return FindScalarElement(III->getOperand(0), EltNo); |
Chris Lattner | 389a6f5 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 8516 | } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(V)) { |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 8517 | unsigned LHSWidth = |
| 8518 | cast<VectorType>(SVI->getOperand(0)->getType())->getNumElements(); |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8519 | unsigned InEl = getShuffleMask(SVI)[EltNo]; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 8520 | if (InEl < LHSWidth) |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 8521 | return FindScalarElement(SVI->getOperand(0), InEl); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 8522 | else if (InEl < LHSWidth*2) |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 8523 | return FindScalarElement(SVI->getOperand(1), InEl - LHSWidth); |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8524 | else |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 8525 | return UndefValue::get(PTy->getElementType()); |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8526 | } |
| 8527 | |
| 8528 | // Otherwise, we don't know. |
| 8529 | return 0; |
| 8530 | } |
| 8531 | |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 8532 | Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) { |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 8533 | // If vector val is undef, replace extract with scalar undef. |
Chris Lattner | 1f13c88 | 2006-03-31 18:25:14 +0000 | [diff] [blame] | 8534 | if (isa<UndefValue>(EI.getOperand(0))) |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 8535 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); |
Chris Lattner | 1f13c88 | 2006-03-31 18:25:14 +0000 | [diff] [blame] | 8536 | |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 8537 | // If vector val is constant 0, replace extract with scalar 0. |
Chris Lattner | 1f13c88 | 2006-03-31 18:25:14 +0000 | [diff] [blame] | 8538 | if (isa<ConstantAggregateZero>(EI.getOperand(0))) |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 8539 | return ReplaceInstUsesWith(EI, Constant::getNullValue(EI.getType())); |
Chris Lattner | 1f13c88 | 2006-03-31 18:25:14 +0000 | [diff] [blame] | 8540 | |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 8541 | if (ConstantVector *C = dyn_cast<ConstantVector>(EI.getOperand(0))) { |
Matthijs Kooijman | b4d6a5a | 2008-06-11 09:00:12 +0000 | [diff] [blame] | 8542 | // If vector val is constant with all elements the same, replace EI with |
| 8543 | // that element. When the elements are not identical, we cannot replace yet |
| 8544 | // (we do that below, but only when the index is constant). |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 8545 | Constant *op0 = C->getOperand(0); |
Chris Lattner | 4cb81bd | 2009-09-08 03:44:51 +0000 | [diff] [blame] | 8546 | for (unsigned i = 1; i != C->getNumOperands(); ++i) |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 8547 | if (C->getOperand(i) != op0) { |
| 8548 | op0 = 0; |
| 8549 | break; |
| 8550 | } |
| 8551 | if (op0) |
| 8552 | return ReplaceInstUsesWith(EI, op0); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 8553 | } |
Eli Friedman | 76e7ba8 | 2009-07-18 19:04:16 +0000 | [diff] [blame] | 8554 | |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8555 | // If extracting a specified index from the vector, see if we can recursively |
| 8556 | // find a previously computed scalar that was inserted into the vector. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8557 | if (ConstantInt *IdxC = dyn_cast<ConstantInt>(EI.getOperand(1))) { |
Chris Lattner | 8546409 | 2007-04-09 01:37:55 +0000 | [diff] [blame] | 8558 | unsigned IndexVal = IdxC->getZExtValue(); |
Chris Lattner | 4cb81bd | 2009-09-08 03:44:51 +0000 | [diff] [blame] | 8559 | unsigned VectorWidth = EI.getVectorOperandType()->getNumElements(); |
Chris Lattner | 8546409 | 2007-04-09 01:37:55 +0000 | [diff] [blame] | 8560 | |
| 8561 | // If this is extracting an invalid index, turn this into undef, to avoid |
| 8562 | // crashing the code below. |
| 8563 | if (IndexVal >= VectorWidth) |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 8564 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); |
Chris Lattner | 8546409 | 2007-04-09 01:37:55 +0000 | [diff] [blame] | 8565 | |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 8566 | // This instruction only demands the single element from the input vector. |
| 8567 | // If the input vector has a single use, simplify it based on this use |
| 8568 | // property. |
Eli Friedman | 76e7ba8 | 2009-07-18 19:04:16 +0000 | [diff] [blame] | 8569 | if (EI.getOperand(0)->hasOneUse() && VectorWidth != 1) { |
Evan Cheng | 388df62 | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 8570 | APInt UndefElts(VectorWidth, 0); |
| 8571 | APInt DemandedMask(VectorWidth, 1 << IndexVal); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 8572 | if (Value *V = SimplifyDemandedVectorElts(EI.getOperand(0), |
Evan Cheng | 388df62 | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 8573 | DemandedMask, UndefElts)) { |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 8574 | EI.setOperand(0, V); |
| 8575 | return &EI; |
| 8576 | } |
| 8577 | } |
| 8578 | |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 8579 | if (Value *Elt = FindScalarElement(EI.getOperand(0), IndexVal)) |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8580 | return ReplaceInstUsesWith(EI, Elt); |
Chris Lattner | b7300fa | 2007-04-14 23:02:14 +0000 | [diff] [blame] | 8581 | |
| 8582 | // If the this extractelement is directly using a bitcast from a vector of |
| 8583 | // the same number of elements, see if we can find the source element from |
| 8584 | // it. In this case, we will end up needing to bitcast the scalars. |
| 8585 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(EI.getOperand(0))) { |
| 8586 | if (const VectorType *VT = |
| 8587 | dyn_cast<VectorType>(BCI->getOperand(0)->getType())) |
| 8588 | if (VT->getNumElements() == VectorWidth) |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 8589 | if (Value *Elt = FindScalarElement(BCI->getOperand(0), IndexVal)) |
Chris Lattner | b7300fa | 2007-04-14 23:02:14 +0000 | [diff] [blame] | 8590 | return new BitCastInst(Elt, EI.getType()); |
| 8591 | } |
Chris Lattner | 389a6f5 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 8592 | } |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8593 | |
Chris Lattner | 73fa49d | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 8594 | if (Instruction *I = dyn_cast<Instruction>(EI.getOperand(0))) { |
Chris Lattner | 275a6d6 | 2009-09-08 18:48:01 +0000 | [diff] [blame] | 8595 | // Push extractelement into predecessor operation if legal and |
| 8596 | // profitable to do so |
| 8597 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) { |
| 8598 | if (I->hasOneUse() && |
| 8599 | CheapToScalarize(BO, isa<ConstantInt>(EI.getOperand(1)))) { |
| 8600 | Value *newEI0 = |
| 8601 | Builder->CreateExtractElement(BO->getOperand(0), EI.getOperand(1), |
| 8602 | EI.getName()+".lhs"); |
| 8603 | Value *newEI1 = |
| 8604 | Builder->CreateExtractElement(BO->getOperand(1), EI.getOperand(1), |
| 8605 | EI.getName()+".rhs"); |
| 8606 | return BinaryOperator::Create(BO->getOpcode(), newEI0, newEI1); |
Chris Lattner | 73fa49d | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 8607 | } |
Chris Lattner | 275a6d6 | 2009-09-08 18:48:01 +0000 | [diff] [blame] | 8608 | } else if (InsertElementInst *IE = dyn_cast<InsertElementInst>(I)) { |
Chris Lattner | 73fa49d | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 8609 | // Extracting the inserted element? |
| 8610 | if (IE->getOperand(2) == EI.getOperand(1)) |
| 8611 | return ReplaceInstUsesWith(EI, IE->getOperand(1)); |
| 8612 | // If the inserted and extracted elements are constants, they must not |
| 8613 | // be the same value, extract from the pre-inserted value instead. |
Chris Lattner | 08142f2 | 2009-08-30 19:47:22 +0000 | [diff] [blame] | 8614 | if (isa<Constant>(IE->getOperand(2)) && isa<Constant>(EI.getOperand(1))) { |
Chris Lattner | 3c4e38e | 2009-08-30 06:27:41 +0000 | [diff] [blame] | 8615 | Worklist.AddValue(EI.getOperand(0)); |
Chris Lattner | 73fa49d | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 8616 | EI.setOperand(0, IE->getOperand(0)); |
| 8617 | return &EI; |
| 8618 | } |
| 8619 | } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(I)) { |
| 8620 | // If this is extracting an element from a shufflevector, figure out where |
| 8621 | // it came from and extract from the appropriate input element instead. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8622 | if (ConstantInt *Elt = dyn_cast<ConstantInt>(EI.getOperand(1))) { |
| 8623 | unsigned SrcIdx = getShuffleMask(SVI)[Elt->getZExtValue()]; |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8624 | Value *Src; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 8625 | unsigned LHSWidth = |
| 8626 | cast<VectorType>(SVI->getOperand(0)->getType())->getNumElements(); |
| 8627 | |
| 8628 | if (SrcIdx < LHSWidth) |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8629 | Src = SVI->getOperand(0); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 8630 | else if (SrcIdx < LHSWidth*2) { |
| 8631 | SrcIdx -= LHSWidth; |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8632 | Src = SVI->getOperand(1); |
| 8633 | } else { |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 8634 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); |
Chris Lattner | df084ff | 2006-03-30 22:02:40 +0000 | [diff] [blame] | 8635 | } |
Eric Christopher | a3500da | 2009-07-25 02:28:41 +0000 | [diff] [blame] | 8636 | return ExtractElementInst::Create(Src, |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 8637 | ConstantInt::get(Type::getInt32Ty(EI.getContext()), |
| 8638 | SrcIdx, false)); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 8639 | } |
| 8640 | } |
Eli Friedman | 2451a64 | 2009-07-18 23:06:53 +0000 | [diff] [blame] | 8641 | // FIXME: Canonicalize extractelement(bitcast) -> bitcast(extractelement) |
Chris Lattner | 73fa49d | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 8642 | } |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 8643 | return 0; |
| 8644 | } |
| 8645 | |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8646 | /// CollectSingleShuffleElements - If V is a shuffle of values that ONLY returns |
| 8647 | /// elements from either LHS or RHS, return the shuffle mask and true. |
| 8648 | /// Otherwise, return false. |
| 8649 | static bool CollectSingleShuffleElements(Value *V, Value *LHS, Value *RHS, |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 8650 | std::vector<Constant*> &Mask) { |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8651 | assert(V->getType() == LHS->getType() && V->getType() == RHS->getType() && |
| 8652 | "Invalid CollectSingleShuffleElements"); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 8653 | unsigned NumElts = cast<VectorType>(V->getType())->getNumElements(); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8654 | |
| 8655 | if (isa<UndefValue>(V)) { |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 8656 | Mask.assign(NumElts, UndefValue::get(Type::getInt32Ty(V->getContext()))); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8657 | return true; |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 8658 | } |
| 8659 | |
| 8660 | if (V == LHS) { |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8661 | for (unsigned i = 0; i != NumElts; ++i) |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 8662 | Mask.push_back(ConstantInt::get(Type::getInt32Ty(V->getContext()), i)); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8663 | return true; |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 8664 | } |
| 8665 | |
| 8666 | if (V == RHS) { |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8667 | for (unsigned i = 0; i != NumElts; ++i) |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 8668 | Mask.push_back(ConstantInt::get(Type::getInt32Ty(V->getContext()), |
| 8669 | i+NumElts)); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8670 | return true; |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 8671 | } |
| 8672 | |
| 8673 | if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) { |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8674 | // If this is an insert of an extract from some other vector, include it. |
| 8675 | Value *VecOp = IEI->getOperand(0); |
| 8676 | Value *ScalarOp = IEI->getOperand(1); |
| 8677 | Value *IdxOp = IEI->getOperand(2); |
| 8678 | |
Chris Lattner | d929f06 | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 8679 | if (!isa<ConstantInt>(IdxOp)) |
| 8680 | return false; |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8681 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Chris Lattner | d929f06 | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 8682 | |
| 8683 | if (isa<UndefValue>(ScalarOp)) { // inserting undef into vector. |
| 8684 | // Okay, we can handle this if the vector we are insertinting into is |
| 8685 | // transitively ok. |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 8686 | if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) { |
Chris Lattner | d929f06 | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 8687 | // If so, update the mask to reflect the inserted undef. |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 8688 | Mask[InsertedIdx] = UndefValue::get(Type::getInt32Ty(V->getContext())); |
Chris Lattner | d929f06 | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 8689 | return true; |
| 8690 | } |
| 8691 | } else if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)){ |
| 8692 | if (isa<ConstantInt>(EI->getOperand(1)) && |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8693 | EI->getOperand(0)->getType() == V->getType()) { |
| 8694 | unsigned ExtractedIdx = |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8695 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8696 | |
| 8697 | // This must be extracting from either LHS or RHS. |
| 8698 | if (EI->getOperand(0) == LHS || EI->getOperand(0) == RHS) { |
| 8699 | // Okay, we can handle this if the vector we are insertinting into is |
| 8700 | // transitively ok. |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 8701 | if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) { |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8702 | // If so, update the mask to reflect the inserted value. |
| 8703 | if (EI->getOperand(0) == LHS) { |
Mon P Wang | 4f5ca2c | 2008-08-20 02:23:25 +0000 | [diff] [blame] | 8704 | Mask[InsertedIdx % NumElts] = |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 8705 | ConstantInt::get(Type::getInt32Ty(V->getContext()), |
| 8706 | ExtractedIdx); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8707 | } else { |
| 8708 | assert(EI->getOperand(0) == RHS); |
Mon P Wang | 4f5ca2c | 2008-08-20 02:23:25 +0000 | [diff] [blame] | 8709 | Mask[InsertedIdx % NumElts] = |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 8710 | ConstantInt::get(Type::getInt32Ty(V->getContext()), |
| 8711 | ExtractedIdx+NumElts); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8712 | |
| 8713 | } |
| 8714 | return true; |
| 8715 | } |
| 8716 | } |
| 8717 | } |
| 8718 | } |
| 8719 | } |
| 8720 | // TODO: Handle shufflevector here! |
| 8721 | |
| 8722 | return false; |
| 8723 | } |
| 8724 | |
| 8725 | /// CollectShuffleElements - We are building a shuffle of V, using RHS as the |
| 8726 | /// RHS of the shuffle instruction, if it is not null. Return a shuffle mask |
| 8727 | /// that computes V and the LHS value of the shuffle. |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8728 | static Value *CollectShuffleElements(Value *V, std::vector<Constant*> &Mask, |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 8729 | Value *&RHS) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 8730 | assert(isa<VectorType>(V->getType()) && |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8731 | (RHS == 0 || V->getType() == RHS->getType()) && |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8732 | "Invalid shuffle!"); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 8733 | unsigned NumElts = cast<VectorType>(V->getType())->getNumElements(); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8734 | |
| 8735 | if (isa<UndefValue>(V)) { |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 8736 | Mask.assign(NumElts, UndefValue::get(Type::getInt32Ty(V->getContext()))); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8737 | return V; |
| 8738 | } else if (isa<ConstantAggregateZero>(V)) { |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 8739 | Mask.assign(NumElts, ConstantInt::get(Type::getInt32Ty(V->getContext()),0)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8740 | return V; |
| 8741 | } else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) { |
| 8742 | // If this is an insert of an extract from some other vector, include it. |
| 8743 | Value *VecOp = IEI->getOperand(0); |
| 8744 | Value *ScalarOp = IEI->getOperand(1); |
| 8745 | Value *IdxOp = IEI->getOperand(2); |
| 8746 | |
| 8747 | if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) { |
| 8748 | if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) && |
| 8749 | EI->getOperand(0)->getType() == V->getType()) { |
| 8750 | unsigned ExtractedIdx = |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8751 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
| 8752 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8753 | |
| 8754 | // Either the extracted from or inserted into vector must be RHSVec, |
| 8755 | // otherwise we'd end up with a shuffle of three inputs. |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8756 | if (EI->getOperand(0) == RHS || RHS == 0) { |
| 8757 | RHS = EI->getOperand(0); |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 8758 | Value *V = CollectShuffleElements(VecOp, Mask, RHS); |
Mon P Wang | 4f5ca2c | 2008-08-20 02:23:25 +0000 | [diff] [blame] | 8759 | Mask[InsertedIdx % NumElts] = |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 8760 | ConstantInt::get(Type::getInt32Ty(V->getContext()), |
| 8761 | NumElts+ExtractedIdx); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8762 | return V; |
| 8763 | } |
| 8764 | |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8765 | if (VecOp == RHS) { |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 8766 | Value *V = CollectShuffleElements(EI->getOperand(0), Mask, RHS); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8767 | // Everything but the extracted element is replaced with the RHS. |
| 8768 | for (unsigned i = 0; i != NumElts; ++i) { |
| 8769 | if (i != InsertedIdx) |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 8770 | Mask[i] = ConstantInt::get(Type::getInt32Ty(V->getContext()), |
| 8771 | NumElts+i); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8772 | } |
| 8773 | return V; |
| 8774 | } |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8775 | |
| 8776 | // If this insertelement is a chain that comes from exactly these two |
| 8777 | // vectors, return the vector and the effective shuffle. |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 8778 | if (CollectSingleShuffleElements(IEI, EI->getOperand(0), RHS, Mask)) |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8779 | return EI->getOperand(0); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8780 | } |
| 8781 | } |
| 8782 | } |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8783 | // TODO: Handle shufflevector here! |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8784 | |
| 8785 | // Otherwise, can't do anything fancy. Return an identity vector. |
| 8786 | for (unsigned i = 0; i != NumElts; ++i) |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 8787 | Mask.push_back(ConstantInt::get(Type::getInt32Ty(V->getContext()), i)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8788 | return V; |
| 8789 | } |
| 8790 | |
| 8791 | Instruction *InstCombiner::visitInsertElementInst(InsertElementInst &IE) { |
| 8792 | Value *VecOp = IE.getOperand(0); |
| 8793 | Value *ScalarOp = IE.getOperand(1); |
| 8794 | Value *IdxOp = IE.getOperand(2); |
| 8795 | |
Chris Lattner | 599ded1 | 2007-04-09 01:11:16 +0000 | [diff] [blame] | 8796 | // Inserting an undef or into an undefined place, remove this. |
| 8797 | if (isa<UndefValue>(ScalarOp) || isa<UndefValue>(IdxOp)) |
| 8798 | ReplaceInstUsesWith(IE, VecOp); |
Eli Friedman | 76e7ba8 | 2009-07-18 19:04:16 +0000 | [diff] [blame] | 8799 | |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8800 | // If the inserted element was extracted from some other vector, and if the |
| 8801 | // indexes are constant, try to turn this into a shufflevector operation. |
| 8802 | if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) { |
| 8803 | if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) && |
| 8804 | EI->getOperand(0)->getType() == IE.getType()) { |
Eli Friedman | 76e7ba8 | 2009-07-18 19:04:16 +0000 | [diff] [blame] | 8805 | unsigned NumVectorElts = IE.getType()->getNumElements(); |
Chris Lattner | e34e9a2 | 2007-04-14 23:32:02 +0000 | [diff] [blame] | 8806 | unsigned ExtractedIdx = |
| 8807 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8808 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8809 | |
| 8810 | if (ExtractedIdx >= NumVectorElts) // Out of range extract. |
| 8811 | return ReplaceInstUsesWith(IE, VecOp); |
| 8812 | |
| 8813 | if (InsertedIdx >= NumVectorElts) // Out of range insert. |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 8814 | return ReplaceInstUsesWith(IE, UndefValue::get(IE.getType())); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8815 | |
| 8816 | // If we are extracting a value from a vector, then inserting it right |
| 8817 | // back into the same place, just use the input vector. |
| 8818 | if (EI->getOperand(0) == VecOp && ExtractedIdx == InsertedIdx) |
| 8819 | return ReplaceInstUsesWith(IE, VecOp); |
| 8820 | |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8821 | // If this insertelement isn't used by some other insertelement, turn it |
| 8822 | // (and any insertelements it points to), into one big shuffle. |
| 8823 | if (!IE.hasOneUse() || !isa<InsertElementInst>(IE.use_back())) { |
| 8824 | std::vector<Constant*> Mask; |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8825 | Value *RHS = 0; |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 8826 | Value *LHS = CollectShuffleElements(&IE, Mask, RHS); |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 8827 | if (RHS == 0) RHS = UndefValue::get(LHS->getType()); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8828 | // We now have a shuffle of LHS, RHS, Mask. |
Owen Anderson | d672ecb | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 8829 | return new ShuffleVectorInst(LHS, RHS, |
Owen Anderson | af7ec97 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 8830 | ConstantVector::get(Mask)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8831 | } |
| 8832 | } |
| 8833 | } |
| 8834 | |
Eli Friedman | b9a4cac | 2009-06-06 20:08:03 +0000 | [diff] [blame] | 8835 | unsigned VWidth = cast<VectorType>(VecOp->getType())->getNumElements(); |
| 8836 | APInt UndefElts(VWidth, 0); |
| 8837 | APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth)); |
| 8838 | if (SimplifyDemandedVectorElts(&IE, AllOnesEltMask, UndefElts)) |
| 8839 | return &IE; |
| 8840 | |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8841 | return 0; |
| 8842 | } |
| 8843 | |
| 8844 | |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 8845 | Instruction *InstCombiner::visitShuffleVectorInst(ShuffleVectorInst &SVI) { |
| 8846 | Value *LHS = SVI.getOperand(0); |
| 8847 | Value *RHS = SVI.getOperand(1); |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8848 | std::vector<unsigned> Mask = getShuffleMask(&SVI); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 8849 | |
| 8850 | bool MadeChange = false; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 8851 | |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 8852 | // Undefined shuffle mask -> undefined value. |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8853 | if (isa<UndefValue>(SVI.getOperand(2))) |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 8854 | return ReplaceInstUsesWith(SVI, UndefValue::get(SVI.getType())); |
Dan Gohman | 488fbfc | 2008-09-09 18:11:14 +0000 | [diff] [blame] | 8855 | |
Dan Gohman | 488fbfc | 2008-09-09 18:11:14 +0000 | [diff] [blame] | 8856 | unsigned VWidth = cast<VectorType>(SVI.getType())->getNumElements(); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 8857 | |
| 8858 | if (VWidth != cast<VectorType>(LHS->getType())->getNumElements()) |
| 8859 | return 0; |
| 8860 | |
Evan Cheng | 388df62 | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 8861 | APInt UndefElts(VWidth, 0); |
| 8862 | APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth)); |
| 8863 | if (SimplifyDemandedVectorElts(&SVI, AllOnesEltMask, UndefElts)) { |
Dan Gohman | 3139ff8 | 2008-09-11 22:47:57 +0000 | [diff] [blame] | 8864 | LHS = SVI.getOperand(0); |
| 8865 | RHS = SVI.getOperand(1); |
Dan Gohman | 488fbfc | 2008-09-09 18:11:14 +0000 | [diff] [blame] | 8866 | MadeChange = true; |
Dan Gohman | 3139ff8 | 2008-09-11 22:47:57 +0000 | [diff] [blame] | 8867 | } |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8868 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8869 | // Canonicalize shuffle(x ,x,mask) -> shuffle(x, undef,mask') |
| 8870 | // Canonicalize shuffle(undef,x,mask) -> shuffle(x, undef,mask'). |
| 8871 | if (LHS == RHS || isa<UndefValue>(LHS)) { |
| 8872 | if (isa<UndefValue>(LHS) && LHS == RHS) { |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 8873 | // shuffle(undef,undef,mask) -> undef. |
| 8874 | return ReplaceInstUsesWith(SVI, LHS); |
| 8875 | } |
| 8876 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8877 | // Remap any references to RHS to use LHS. |
| 8878 | std::vector<Constant*> Elts; |
| 8879 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 8880 | if (Mask[i] >= 2*e) |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 8881 | Elts.push_back(UndefValue::get(Type::getInt32Ty(SVI.getContext()))); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 8882 | else { |
| 8883 | if ((Mask[i] >= e && isa<UndefValue>(RHS)) || |
Dan Gohman | 4ce9627 | 2008-08-06 18:17:32 +0000 | [diff] [blame] | 8884 | (Mask[i] < e && isa<UndefValue>(LHS))) { |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 8885 | Mask[i] = 2*e; // Turn into undef. |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 8886 | Elts.push_back(UndefValue::get(Type::getInt32Ty(SVI.getContext()))); |
Dan Gohman | 4ce9627 | 2008-08-06 18:17:32 +0000 | [diff] [blame] | 8887 | } else { |
Mon P Wang | 4f5ca2c | 2008-08-20 02:23:25 +0000 | [diff] [blame] | 8888 | Mask[i] = Mask[i] % e; // Force to LHS. |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 8889 | Elts.push_back(ConstantInt::get(Type::getInt32Ty(SVI.getContext()), |
| 8890 | Mask[i])); |
Dan Gohman | 4ce9627 | 2008-08-06 18:17:32 +0000 | [diff] [blame] | 8891 | } |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 8892 | } |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 8893 | } |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8894 | SVI.setOperand(0, SVI.getOperand(1)); |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 8895 | SVI.setOperand(1, UndefValue::get(RHS->getType())); |
Owen Anderson | af7ec97 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 8896 | SVI.setOperand(2, ConstantVector::get(Elts)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 8897 | LHS = SVI.getOperand(0); |
| 8898 | RHS = SVI.getOperand(1); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 8899 | MadeChange = true; |
| 8900 | } |
| 8901 | |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 8902 | // Analyze the shuffle, are the LHS or RHS and identity shuffles? |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8903 | bool isLHSID = true, isRHSID = true; |
Chris Lattner | 706126d | 2006-04-16 00:03:56 +0000 | [diff] [blame] | 8904 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8905 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
| 8906 | if (Mask[i] >= e*2) continue; // Ignore undef values. |
| 8907 | // Is this an identity shuffle of the LHS value? |
| 8908 | isLHSID &= (Mask[i] == i); |
| 8909 | |
| 8910 | // Is this an identity shuffle of the RHS value? |
| 8911 | isRHSID &= (Mask[i]-e == i); |
Chris Lattner | 706126d | 2006-04-16 00:03:56 +0000 | [diff] [blame] | 8912 | } |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 8913 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8914 | // Eliminate identity shuffles. |
| 8915 | if (isLHSID) return ReplaceInstUsesWith(SVI, LHS); |
| 8916 | if (isRHSID) return ReplaceInstUsesWith(SVI, RHS); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 8917 | |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 8918 | // If the LHS is a shufflevector itself, see if we can combine it with this |
| 8919 | // one without producing an unusual shuffle. Here we are really conservative: |
| 8920 | // we are absolutely afraid of producing a shuffle mask not in the input |
| 8921 | // program, because the code gen may not be smart enough to turn a merged |
| 8922 | // shuffle into two specific shuffles: it may produce worse code. As such, |
| 8923 | // we only merge two shuffles if the result is one of the two input shuffle |
| 8924 | // masks. In this case, merging the shuffles just removes one instruction, |
| 8925 | // which we know is safe. This is good for things like turning: |
| 8926 | // (splat(splat)) -> splat. |
| 8927 | if (ShuffleVectorInst *LHSSVI = dyn_cast<ShuffleVectorInst>(LHS)) { |
| 8928 | if (isa<UndefValue>(RHS)) { |
| 8929 | std::vector<unsigned> LHSMask = getShuffleMask(LHSSVI); |
| 8930 | |
David Greene | f941d29 | 2009-11-16 21:52:23 +0000 | [diff] [blame] | 8931 | if (LHSMask.size() == Mask.size()) { |
| 8932 | std::vector<unsigned> NewMask; |
| 8933 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) |
Duncan Sands | 76700ba | 2009-11-20 13:19:51 +0000 | [diff] [blame] | 8934 | if (Mask[i] >= e) |
David Greene | f941d29 | 2009-11-16 21:52:23 +0000 | [diff] [blame] | 8935 | NewMask.push_back(2*e); |
| 8936 | else |
| 8937 | NewMask.push_back(LHSMask[Mask[i]]); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 8938 | |
David Greene | f941d29 | 2009-11-16 21:52:23 +0000 | [diff] [blame] | 8939 | // If the result mask is equal to the src shuffle or this |
| 8940 | // shuffle mask, do the replacement. |
| 8941 | if (NewMask == LHSMask || NewMask == Mask) { |
| 8942 | unsigned LHSInNElts = |
| 8943 | cast<VectorType>(LHSSVI->getOperand(0)->getType())-> |
| 8944 | getNumElements(); |
| 8945 | std::vector<Constant*> Elts; |
| 8946 | for (unsigned i = 0, e = NewMask.size(); i != e; ++i) { |
| 8947 | if (NewMask[i] >= LHSInNElts*2) { |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 8948 | Elts.push_back(UndefValue::get( |
| 8949 | Type::getInt32Ty(SVI.getContext()))); |
David Greene | f941d29 | 2009-11-16 21:52:23 +0000 | [diff] [blame] | 8950 | } else { |
Chris Lattner | 4de8476 | 2010-01-04 07:02:48 +0000 | [diff] [blame] | 8951 | Elts.push_back(ConstantInt::get( |
| 8952 | Type::getInt32Ty(SVI.getContext()), |
David Greene | f941d29 | 2009-11-16 21:52:23 +0000 | [diff] [blame] | 8953 | NewMask[i])); |
| 8954 | } |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 8955 | } |
David Greene | f941d29 | 2009-11-16 21:52:23 +0000 | [diff] [blame] | 8956 | return new ShuffleVectorInst(LHSSVI->getOperand(0), |
| 8957 | LHSSVI->getOperand(1), |
| 8958 | ConstantVector::get(Elts)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 8959 | } |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 8960 | } |
| 8961 | } |
| 8962 | } |
Chris Lattner | c5eff44 | 2007-01-30 22:32:46 +0000 | [diff] [blame] | 8963 | |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 8964 | return MadeChange ? &SVI : 0; |
| 8965 | } |
| 8966 | |
| 8967 | |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 8968 | |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 8969 | |
| 8970 | /// TryToSinkInstruction - Try to move the specified instruction from its |
| 8971 | /// current block into the beginning of DestBlock, which can only happen if it's |
| 8972 | /// safe to move the instruction past all of the instructions between it and the |
| 8973 | /// end of its block. |
| 8974 | static bool TryToSinkInstruction(Instruction *I, BasicBlock *DestBlock) { |
| 8975 | assert(I->hasOneUse() && "Invariants didn't hold!"); |
| 8976 | |
Chris Lattner | 108e902 | 2005-10-27 17:13:11 +0000 | [diff] [blame] | 8977 | // Cannot move control-flow-involving, volatile loads, vaarg, etc. |
Duncan Sands | 7af1c78 | 2009-05-06 06:49:50 +0000 | [diff] [blame] | 8978 | if (isa<PHINode>(I) || I->mayHaveSideEffects() || isa<TerminatorInst>(I)) |
Chris Lattner | bfc538c | 2008-05-09 15:07:33 +0000 | [diff] [blame] | 8979 | return false; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8980 | |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 8981 | // Do not sink alloca instructions out of the entry block. |
Dan Gohman | ecb7a77 | 2007-03-22 16:38:57 +0000 | [diff] [blame] | 8982 | if (isa<AllocaInst>(I) && I->getParent() == |
| 8983 | &DestBlock->getParent()->getEntryBlock()) |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 8984 | return false; |
| 8985 | |
Chris Lattner | 96a52a6 | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 8986 | // We can only sink load instructions if there is nothing between the load and |
| 8987 | // the end of block that could change the value. |
Chris Lattner | 2539e33 | 2008-05-08 17:37:37 +0000 | [diff] [blame] | 8988 | if (I->mayReadFromMemory()) { |
| 8989 | for (BasicBlock::iterator Scan = I, E = I->getParent()->end(); |
Chris Lattner | 96a52a6 | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 8990 | Scan != E; ++Scan) |
| 8991 | if (Scan->mayWriteToMemory()) |
| 8992 | return false; |
Chris Lattner | 96a52a6 | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 8993 | } |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 8994 | |
Dan Gohman | 02dea8b | 2008-05-23 21:05:58 +0000 | [diff] [blame] | 8995 | BasicBlock::iterator InsertPos = DestBlock->getFirstNonPHI(); |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 8996 | |
Dale Johannesen | bd8e650 | 2009-03-03 01:09:07 +0000 | [diff] [blame] | 8997 | CopyPrecedingStopPoint(I, InsertPos); |
Chris Lattner | 4bc5f80 | 2005-08-08 19:11:57 +0000 | [diff] [blame] | 8998 | I->moveBefore(InsertPos); |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 8999 | ++NumSunkInst; |
| 9000 | return true; |
| 9001 | } |
| 9002 | |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9003 | |
| 9004 | /// AddReachableCodeToWorklist - Walk the function in depth-first order, adding |
| 9005 | /// all reachable code to the worklist. |
| 9006 | /// |
| 9007 | /// This has a couple of tricks to make the code faster and more powerful. In |
| 9008 | /// particular, we constant fold and DCE instructions as we go, to avoid adding |
| 9009 | /// them to the worklist (this significantly speeds up instcombine on code where |
| 9010 | /// many instructions are dead or constant). Additionally, if we find a branch |
| 9011 | /// whose condition is a known constant, we only visit the reachable successors. |
| 9012 | /// |
Chris Lattner | 2ee743b | 2009-10-15 04:59:28 +0000 | [diff] [blame] | 9013 | static bool AddReachableCodeToWorklist(BasicBlock *BB, |
Chris Lattner | 1f87a58 | 2007-02-15 19:41:52 +0000 | [diff] [blame] | 9014 | SmallPtrSet<BasicBlock*, 64> &Visited, |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9015 | InstCombiner &IC, |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 9016 | const TargetData *TD) { |
Chris Lattner | 2ee743b | 2009-10-15 04:59:28 +0000 | [diff] [blame] | 9017 | bool MadeIRChange = false; |
Chris Lattner | 2806dff | 2008-08-15 04:03:01 +0000 | [diff] [blame] | 9018 | SmallVector<BasicBlock*, 256> Worklist; |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 9019 | Worklist.push_back(BB); |
Chris Lattner | 67f7d54 | 2009-10-12 03:58:40 +0000 | [diff] [blame] | 9020 | |
| 9021 | std::vector<Instruction*> InstrsForInstCombineWorklist; |
| 9022 | InstrsForInstCombineWorklist.reserve(128); |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9023 | |
Chris Lattner | 2ee743b | 2009-10-15 04:59:28 +0000 | [diff] [blame] | 9024 | SmallPtrSet<ConstantExpr*, 64> FoldedConstants; |
| 9025 | |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 9026 | while (!Worklist.empty()) { |
| 9027 | BB = Worklist.back(); |
| 9028 | Worklist.pop_back(); |
| 9029 | |
| 9030 | // We have now visited this block! If we've already been here, ignore it. |
| 9031 | if (!Visited.insert(BB)) continue; |
Devang Patel | 7fe1dec | 2008-11-19 18:56:50 +0000 | [diff] [blame] | 9032 | |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 9033 | for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ) { |
| 9034 | Instruction *Inst = BBI++; |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9035 | |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 9036 | // DCE instruction if trivially dead. |
| 9037 | if (isInstructionTriviallyDead(Inst)) { |
| 9038 | ++NumDeadInst; |
Chris Lattner | bdff548 | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 9039 | DEBUG(errs() << "IC: DCE: " << *Inst << '\n'); |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 9040 | Inst->eraseFromParent(); |
| 9041 | continue; |
| 9042 | } |
| 9043 | |
| 9044 | // ConstantProp instruction if trivially constant. |
Chris Lattner | e2cc1ad | 2009-10-15 04:13:44 +0000 | [diff] [blame] | 9045 | if (!Inst->use_empty() && isa<Constant>(Inst->getOperand(0))) |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 9046 | if (Constant *C = ConstantFoldInstruction(Inst, TD)) { |
Chris Lattner | e2cc1ad | 2009-10-15 04:13:44 +0000 | [diff] [blame] | 9047 | DEBUG(errs() << "IC: ConstFold to: " << *C << " from: " |
| 9048 | << *Inst << '\n'); |
| 9049 | Inst->replaceAllUsesWith(C); |
| 9050 | ++NumConstProp; |
| 9051 | Inst->eraseFromParent(); |
| 9052 | continue; |
| 9053 | } |
Chris Lattner | 2ee743b | 2009-10-15 04:59:28 +0000 | [diff] [blame] | 9054 | |
| 9055 | |
| 9056 | |
| 9057 | if (TD) { |
| 9058 | // See if we can constant fold its operands. |
| 9059 | for (User::op_iterator i = Inst->op_begin(), e = Inst->op_end(); |
| 9060 | i != e; ++i) { |
| 9061 | ConstantExpr *CE = dyn_cast<ConstantExpr>(i); |
| 9062 | if (CE == 0) continue; |
| 9063 | |
| 9064 | // If we already folded this constant, don't try again. |
| 9065 | if (!FoldedConstants.insert(CE)) |
| 9066 | continue; |
| 9067 | |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 9068 | Constant *NewC = ConstantFoldConstantExpression(CE, TD); |
Chris Lattner | 2ee743b | 2009-10-15 04:59:28 +0000 | [diff] [blame] | 9069 | if (NewC && NewC != CE) { |
| 9070 | *i = NewC; |
| 9071 | MadeIRChange = true; |
| 9072 | } |
| 9073 | } |
| 9074 | } |
| 9075 | |
Devang Patel | 7fe1dec | 2008-11-19 18:56:50 +0000 | [diff] [blame] | 9076 | |
Chris Lattner | 67f7d54 | 2009-10-12 03:58:40 +0000 | [diff] [blame] | 9077 | InstrsForInstCombineWorklist.push_back(Inst); |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9078 | } |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 9079 | |
| 9080 | // Recursively visit successors. If this is a branch or switch on a |
| 9081 | // constant, only visit the reachable successor. |
| 9082 | TerminatorInst *TI = BB->getTerminator(); |
| 9083 | if (BranchInst *BI = dyn_cast<BranchInst>(TI)) { |
| 9084 | if (BI->isConditional() && isa<ConstantInt>(BI->getCondition())) { |
| 9085 | bool CondVal = cast<ConstantInt>(BI->getCondition())->getZExtValue(); |
Nick Lewycky | 9143699 | 2008-03-09 08:50:23 +0000 | [diff] [blame] | 9086 | BasicBlock *ReachableBB = BI->getSuccessor(!CondVal); |
Nick Lewycky | 280a6e6 | 2008-04-25 16:53:59 +0000 | [diff] [blame] | 9087 | Worklist.push_back(ReachableBB); |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 9088 | continue; |
| 9089 | } |
| 9090 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) { |
| 9091 | if (ConstantInt *Cond = dyn_cast<ConstantInt>(SI->getCondition())) { |
| 9092 | // See if this is an explicit destination. |
| 9093 | for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i) |
| 9094 | if (SI->getCaseValue(i) == Cond) { |
Nick Lewycky | 9143699 | 2008-03-09 08:50:23 +0000 | [diff] [blame] | 9095 | BasicBlock *ReachableBB = SI->getSuccessor(i); |
Nick Lewycky | 280a6e6 | 2008-04-25 16:53:59 +0000 | [diff] [blame] | 9096 | Worklist.push_back(ReachableBB); |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 9097 | continue; |
| 9098 | } |
| 9099 | |
| 9100 | // Otherwise it is the default destination. |
| 9101 | Worklist.push_back(SI->getSuccessor(0)); |
| 9102 | continue; |
| 9103 | } |
| 9104 | } |
| 9105 | |
| 9106 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) |
| 9107 | Worklist.push_back(TI->getSuccessor(i)); |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9108 | } |
Chris Lattner | 67f7d54 | 2009-10-12 03:58:40 +0000 | [diff] [blame] | 9109 | |
| 9110 | // Once we've found all of the instructions to add to instcombine's worklist, |
| 9111 | // add them in reverse order. This way instcombine will visit from the top |
| 9112 | // of the function down. This jives well with the way that it adds all uses |
| 9113 | // of instructions to the worklist after doing a transformation, thus avoiding |
| 9114 | // some N^2 behavior in pathological cases. |
| 9115 | IC.Worklist.AddInitialGroup(&InstrsForInstCombineWorklist[0], |
| 9116 | InstrsForInstCombineWorklist.size()); |
Chris Lattner | 2ee743b | 2009-10-15 04:59:28 +0000 | [diff] [blame] | 9117 | |
| 9118 | return MadeIRChange; |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9119 | } |
| 9120 | |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 9121 | bool InstCombiner::DoOneIteration(Function &F, unsigned Iteration) { |
Chris Lattner | b0b822c | 2009-08-31 06:57:37 +0000 | [diff] [blame] | 9122 | MadeIRChange = false; |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 9123 | |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 9124 | DEBUG(errs() << "\n\nINSTCOMBINE ITERATION #" << Iteration << " on " |
| 9125 | << F.getNameStr() << "\n"); |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9126 | |
Chris Lattner | b3d5970 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 9127 | { |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9128 | // Do a depth-first traversal of the function, populate the worklist with |
| 9129 | // the reachable instructions. Ignore blocks that are not reachable. Keep |
| 9130 | // track of which blocks we visit. |
Chris Lattner | 1f87a58 | 2007-02-15 19:41:52 +0000 | [diff] [blame] | 9131 | SmallPtrSet<BasicBlock*, 64> Visited; |
Chris Lattner | 2ee743b | 2009-10-15 04:59:28 +0000 | [diff] [blame] | 9132 | MadeIRChange |= AddReachableCodeToWorklist(F.begin(), Visited, *this, TD); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 9133 | |
Chris Lattner | b3d5970 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 9134 | // Do a quick scan over the function. If we find any blocks that are |
| 9135 | // unreachable, remove any instructions inside of them. This prevents |
| 9136 | // the instcombine code from having to deal with some bad special cases. |
| 9137 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 9138 | if (!Visited.count(BB)) { |
| 9139 | Instruction *Term = BB->getTerminator(); |
| 9140 | while (Term != BB->begin()) { // Remove instrs bottom-up |
| 9141 | BasicBlock::iterator I = Term; --I; |
Chris Lattner | 6ffe551 | 2004-04-27 15:13:33 +0000 | [diff] [blame] | 9142 | |
Chris Lattner | bdff548 | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 9143 | DEBUG(errs() << "IC: DCE: " << *I << '\n'); |
Dale Johannesen | ff278b1 | 2009-03-10 21:19:49 +0000 | [diff] [blame] | 9144 | // A debug intrinsic shouldn't force another iteration if we weren't |
| 9145 | // going to do one without it. |
| 9146 | if (!isa<DbgInfoIntrinsic>(I)) { |
| 9147 | ++NumDeadInst; |
Chris Lattner | b0b822c | 2009-08-31 06:57:37 +0000 | [diff] [blame] | 9148 | MadeIRChange = true; |
Dale Johannesen | ff278b1 | 2009-03-10 21:19:49 +0000 | [diff] [blame] | 9149 | } |
Devang Patel | 228ebd0 | 2009-10-13 22:56:32 +0000 | [diff] [blame] | 9150 | |
Devang Patel | 228ebd0 | 2009-10-13 22:56:32 +0000 | [diff] [blame] | 9151 | // If I is not void type then replaceAllUsesWith undef. |
| 9152 | // This allows ValueHandlers and custom metadata to adjust itself. |
Devang Patel | 9674d15 | 2009-10-14 17:29:00 +0000 | [diff] [blame] | 9153 | if (!I->getType()->isVoidTy()) |
Devang Patel | 228ebd0 | 2009-10-13 22:56:32 +0000 | [diff] [blame] | 9154 | I->replaceAllUsesWith(UndefValue::get(I->getType())); |
Chris Lattner | b3d5970 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 9155 | I->eraseFromParent(); |
| 9156 | } |
| 9157 | } |
| 9158 | } |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9159 | |
Chris Lattner | 873ff01 | 2009-08-30 05:55:36 +0000 | [diff] [blame] | 9160 | while (!Worklist.isEmpty()) { |
| 9161 | Instruction *I = Worklist.RemoveOne(); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9162 | if (I == 0) continue; // skip null values. |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9163 | |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 9164 | // Check to see if we can DCE the instruction. |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 9165 | if (isInstructionTriviallyDead(I)) { |
Chris Lattner | bdff548 | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 9166 | DEBUG(errs() << "IC: DCE: " << *I << '\n'); |
Chris Lattner | 7a1e924 | 2009-08-30 06:13:40 +0000 | [diff] [blame] | 9167 | EraseInstFromFunction(*I); |
| 9168 | ++NumDeadInst; |
Chris Lattner | b0b822c | 2009-08-31 06:57:37 +0000 | [diff] [blame] | 9169 | MadeIRChange = true; |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9170 | continue; |
| 9171 | } |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 9172 | |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 9173 | // Instruction isn't dead, see if we can constant propagate it. |
Chris Lattner | e2cc1ad | 2009-10-15 04:13:44 +0000 | [diff] [blame] | 9174 | if (!I->use_empty() && isa<Constant>(I->getOperand(0))) |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 9175 | if (Constant *C = ConstantFoldInstruction(I, TD)) { |
Chris Lattner | e2cc1ad | 2009-10-15 04:13:44 +0000 | [diff] [blame] | 9176 | DEBUG(errs() << "IC: ConstFold to: " << *C << " from: " << *I << '\n'); |
Chris Lattner | ad5fec1 | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 9177 | |
Chris Lattner | e2cc1ad | 2009-10-15 04:13:44 +0000 | [diff] [blame] | 9178 | // Add operands to the worklist. |
| 9179 | ReplaceInstUsesWith(*I, C); |
| 9180 | ++NumConstProp; |
| 9181 | EraseInstFromFunction(*I); |
| 9182 | MadeIRChange = true; |
| 9183 | continue; |
| 9184 | } |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9185 | |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 9186 | // 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] | 9187 | if (I->hasOneUse()) { |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 9188 | BasicBlock *BB = I->getParent(); |
Chris Lattner | 8db2cd1 | 2009-10-14 15:21:58 +0000 | [diff] [blame] | 9189 | Instruction *UserInst = cast<Instruction>(I->use_back()); |
| 9190 | BasicBlock *UserParent; |
| 9191 | |
| 9192 | // Get the block the use occurs in. |
| 9193 | if (PHINode *PN = dyn_cast<PHINode>(UserInst)) |
| 9194 | UserParent = PN->getIncomingBlock(I->use_begin().getUse()); |
| 9195 | else |
| 9196 | UserParent = UserInst->getParent(); |
| 9197 | |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 9198 | if (UserParent != BB) { |
| 9199 | bool UserIsSuccessor = false; |
| 9200 | // See if the user is one of our successors. |
| 9201 | for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI) |
| 9202 | if (*SI == UserParent) { |
| 9203 | UserIsSuccessor = true; |
| 9204 | break; |
| 9205 | } |
| 9206 | |
| 9207 | // If the user is one of our immediate successors, and if that successor |
| 9208 | // only has us as a predecessors (we'd have to split the critical edge |
| 9209 | // otherwise), we can keep going. |
Chris Lattner | 8db2cd1 | 2009-10-14 15:21:58 +0000 | [diff] [blame] | 9210 | if (UserIsSuccessor && UserParent->getSinglePredecessor()) |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 9211 | // Okay, the CFG is simple enough, try to sink this instruction. |
Chris Lattner | b0b822c | 2009-08-31 06:57:37 +0000 | [diff] [blame] | 9212 | MadeIRChange |= TryToSinkInstruction(I, UserParent); |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 9213 | } |
| 9214 | } |
| 9215 | |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 9216 | // Now that we have an instruction, try combining it to simplify it. |
| 9217 | Builder->SetInsertPoint(I->getParent(), I); |
| 9218 | |
Reid Spencer | a9b8101 | 2007-03-26 17:44:01 +0000 | [diff] [blame] | 9219 | #ifndef NDEBUG |
| 9220 | std::string OrigI; |
| 9221 | #endif |
Chris Lattner | bdff548 | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 9222 | DEBUG(raw_string_ostream SS(OrigI); I->print(SS); OrigI = SS.str();); |
Jeffrey Yasskin | 4306963 | 2009-10-08 00:12:24 +0000 | [diff] [blame] | 9223 | DEBUG(errs() << "IC: Visiting: " << OrigI << '\n'); |
| 9224 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9225 | if (Instruction *Result = visit(*I)) { |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 9226 | ++NumCombined; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 9227 | // Should we replace the old instruction with a new one? |
Chris Lattner | b3bc8fa | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 9228 | if (Result != I) { |
Chris Lattner | bdff548 | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 9229 | DEBUG(errs() << "IC: Old = " << *I << '\n' |
| 9230 | << " New = " << *Result << '\n'); |
Chris Lattner | 0cea42a | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 9231 | |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 9232 | // Everything uses the new instruction now. |
| 9233 | I->replaceAllUsesWith(Result); |
| 9234 | |
| 9235 | // Push the new instruction and any users onto the worklist. |
Chris Lattner | 7a1e924 | 2009-08-30 06:13:40 +0000 | [diff] [blame] | 9236 | Worklist.Add(Result); |
Chris Lattner | e5ecdb5 | 2009-08-30 06:22:51 +0000 | [diff] [blame] | 9237 | Worklist.AddUsersToWorkList(*Result); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9238 | |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 9239 | // Move the name to the new instruction first. |
| 9240 | Result->takeName(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9241 | |
| 9242 | // Insert the new instruction into the basic block... |
| 9243 | BasicBlock *InstParent = I->getParent(); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9244 | BasicBlock::iterator InsertPos = I; |
| 9245 | |
| 9246 | if (!isa<PHINode>(Result)) // If combining a PHI, don't insert |
| 9247 | while (isa<PHINode>(InsertPos)) // middle of a block of PHIs. |
| 9248 | ++InsertPos; |
| 9249 | |
| 9250 | InstParent->getInstList().insert(InsertPos, Result); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9251 | |
Chris Lattner | 7a1e924 | 2009-08-30 06:13:40 +0000 | [diff] [blame] | 9252 | EraseInstFromFunction(*I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 9253 | } else { |
Evan Cheng | c7baf68 | 2007-03-27 16:44:48 +0000 | [diff] [blame] | 9254 | #ifndef NDEBUG |
Chris Lattner | bdff548 | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 9255 | DEBUG(errs() << "IC: Mod = " << OrigI << '\n' |
| 9256 | << " New = " << *I << '\n'); |
Evan Cheng | c7baf68 | 2007-03-27 16:44:48 +0000 | [diff] [blame] | 9257 | #endif |
Chris Lattner | 0cea42a | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 9258 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9259 | // If the instruction was modified, it's possible that it is now dead. |
| 9260 | // if so, remove it. |
Chris Lattner | 00d5131 | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 9261 | if (isInstructionTriviallyDead(I)) { |
Chris Lattner | 7a1e924 | 2009-08-30 06:13:40 +0000 | [diff] [blame] | 9262 | EraseInstFromFunction(*I); |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 9263 | } else { |
Chris Lattner | 7a1e924 | 2009-08-30 06:13:40 +0000 | [diff] [blame] | 9264 | Worklist.Add(I); |
Chris Lattner | e5ecdb5 | 2009-08-30 06:22:51 +0000 | [diff] [blame] | 9265 | Worklist.AddUsersToWorkList(*I); |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9266 | } |
Chris Lattner | b3bc8fa | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 9267 | } |
Chris Lattner | b0b822c | 2009-08-31 06:57:37 +0000 | [diff] [blame] | 9268 | MadeIRChange = true; |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9269 | } |
| 9270 | } |
| 9271 | |
Chris Lattner | 873ff01 | 2009-08-30 05:55:36 +0000 | [diff] [blame] | 9272 | Worklist.Zap(); |
Chris Lattner | b0b822c | 2009-08-31 06:57:37 +0000 | [diff] [blame] | 9273 | return MadeIRChange; |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 9274 | } |
| 9275 | |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 9276 | |
| 9277 | bool InstCombiner::runOnFunction(Function &F) { |
Chris Lattner | f964f32 | 2007-03-04 04:27:24 +0000 | [diff] [blame] | 9278 | MustPreserveLCSSA = mustPreserveAnalysisID(LCSSAID); |
Chris Lattner | e2cc1ad | 2009-10-15 04:13:44 +0000 | [diff] [blame] | 9279 | TD = getAnalysisIfAvailable<TargetData>(); |
| 9280 | |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 9281 | |
| 9282 | /// Builder - This is an IRBuilder that automatically inserts new |
| 9283 | /// instructions into the worklist when they are created. |
Chris Lattner | e2cc1ad | 2009-10-15 04:13:44 +0000 | [diff] [blame] | 9284 | IRBuilder<true, TargetFolder, InstCombineIRInserter> |
Chris Lattner | f55eeb9 | 2009-11-06 05:59:53 +0000 | [diff] [blame] | 9285 | TheBuilder(F.getContext(), TargetFolder(TD), |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 9286 | InstCombineIRInserter(Worklist)); |
| 9287 | Builder = &TheBuilder; |
| 9288 | |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 9289 | bool EverMadeChange = false; |
| 9290 | |
| 9291 | // Iterate while there is work to do. |
| 9292 | unsigned Iteration = 0; |
Bill Wendling | a6c3112 | 2008-05-14 22:45:20 +0000 | [diff] [blame] | 9293 | while (DoOneIteration(F, Iteration++)) |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 9294 | EverMadeChange = true; |
Chris Lattner | 7438106 | 2009-08-30 07:44:24 +0000 | [diff] [blame] | 9295 | |
| 9296 | Builder = 0; |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 9297 | return EverMadeChange; |
| 9298 | } |
| 9299 | |
Brian Gaeke | 96d4bf7 | 2004-07-27 17:43:21 +0000 | [diff] [blame] | 9300 | FunctionPass *llvm::createInstructionCombiningPass() { |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 9301 | return new InstCombiner(); |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 9302 | } |