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 | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 38 | #include "llvm/IntrinsicInst.h" |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 39 | #include "llvm/Pass.h" |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 40 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 41 | #include "llvm/GlobalVariable.h" |
Chris Lattner | 79066fa | 2007-01-30 23:46:24 +0000 | [diff] [blame] | 42 | #include "llvm/Analysis/ConstantFolding.h" |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 43 | #include "llvm/Analysis/ValueTracking.h" |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 44 | #include "llvm/Target/TargetData.h" |
| 45 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 46 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 47 | #include "llvm/Support/CallSite.h" |
Nick Lewycky | 5be2920 | 2008-02-03 16:33:09 +0000 | [diff] [blame] | 48 | #include "llvm/Support/ConstantRange.h" |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 49 | #include "llvm/Support/Debug.h" |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 50 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 51 | #include "llvm/Support/InstVisitor.h" |
Chris Lattner | bcd7db5 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 52 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 53 | #include "llvm/Support/PatternMatch.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 54 | #include "llvm/Support/Compiler.h" |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 55 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 56 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 1f87a58 | 2007-02-15 19:41:52 +0000 | [diff] [blame] | 57 | #include "llvm/ADT/SmallPtrSet.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 58 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 59 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | b3bc8fa | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 60 | #include <algorithm> |
Torok Edwin | 3eaee31 | 2008-04-20 08:33:11 +0000 | [diff] [blame] | 61 | #include <climits> |
Reid Spencer | a9b8101 | 2007-03-26 17:44:01 +0000 | [diff] [blame] | 62 | #include <sstream> |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 63 | using namespace llvm; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 64 | using namespace llvm::PatternMatch; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 65 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 66 | STATISTIC(NumCombined , "Number of insts combined"); |
| 67 | STATISTIC(NumConstProp, "Number of constant folds"); |
| 68 | STATISTIC(NumDeadInst , "Number of dead inst eliminated"); |
| 69 | STATISTIC(NumDeadStore, "Number of dead stores eliminated"); |
| 70 | STATISTIC(NumSunkInst , "Number of instructions sunk"); |
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 71 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 72 | namespace { |
Chris Lattner | f4b5461 | 2006-06-28 22:08:15 +0000 | [diff] [blame] | 73 | class VISIBILITY_HIDDEN InstCombiner |
| 74 | : public FunctionPass, |
| 75 | public InstVisitor<InstCombiner, Instruction*> { |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 76 | // Worklist of all of the instructions that need to be simplified. |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 77 | std::vector<Instruction*> Worklist; |
| 78 | DenseMap<Instruction*, unsigned> WorklistMap; |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 79 | TargetData *TD; |
Chris Lattner | f964f32 | 2007-03-04 04:27:24 +0000 | [diff] [blame] | 80 | bool MustPreserveLCSSA; |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 81 | public: |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 82 | static char ID; // Pass identification, replacement for typeid |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 83 | InstCombiner() : FunctionPass((intptr_t)&ID) {} |
| 84 | |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 85 | /// AddToWorkList - Add the specified instruction to the worklist if it |
| 86 | /// isn't already in it. |
| 87 | void AddToWorkList(Instruction *I) { |
Dan Gohman | 6b345ee | 2008-07-07 17:46:23 +0000 | [diff] [blame] | 88 | if (WorklistMap.insert(std::make_pair(I, Worklist.size())).second) |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 89 | Worklist.push_back(I); |
| 90 | } |
| 91 | |
| 92 | // RemoveFromWorkList - remove I from the worklist if it exists. |
| 93 | void RemoveFromWorkList(Instruction *I) { |
| 94 | DenseMap<Instruction*, unsigned>::iterator It = WorklistMap.find(I); |
| 95 | if (It == WorklistMap.end()) return; // Not in worklist. |
| 96 | |
| 97 | // Don't bother moving everything down, just null out the slot. |
| 98 | Worklist[It->second] = 0; |
| 99 | |
| 100 | WorklistMap.erase(It); |
| 101 | } |
| 102 | |
| 103 | Instruction *RemoveOneFromWorkList() { |
| 104 | Instruction *I = Worklist.back(); |
| 105 | Worklist.pop_back(); |
| 106 | WorklistMap.erase(I); |
| 107 | return I; |
| 108 | } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 109 | |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 110 | |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 111 | /// AddUsersToWorkList - When an instruction is simplified, add all users of |
| 112 | /// the instruction to the work lists because they might get more simplified |
| 113 | /// now. |
| 114 | /// |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 115 | void AddUsersToWorkList(Value &I) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 116 | for (Value::use_iterator UI = I.use_begin(), UE = I.use_end(); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 117 | UI != UE; ++UI) |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 118 | AddToWorkList(cast<Instruction>(*UI)); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 121 | /// AddUsesToWorkList - When an instruction is simplified, add operands to |
| 122 | /// the work lists because they might get more simplified now. |
| 123 | /// |
| 124 | void AddUsesToWorkList(Instruction &I) { |
Gabor Greif | 177dd3f | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 125 | for (User::op_iterator i = I.op_begin(), e = I.op_end(); i != e; ++i) |
| 126 | if (Instruction *Op = dyn_cast<Instruction>(*i)) |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 127 | AddToWorkList(Op); |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 128 | } |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 129 | |
| 130 | /// AddSoonDeadInstToWorklist - The specified instruction is about to become |
| 131 | /// dead. Add all of its operands to the worklist, turning them into |
| 132 | /// undef's to reduce the number of uses of those instructions. |
| 133 | /// |
| 134 | /// Return the specified operand before it is turned into an undef. |
| 135 | /// |
| 136 | Value *AddSoonDeadInstToWorklist(Instruction &I, unsigned op) { |
| 137 | Value *R = I.getOperand(op); |
| 138 | |
Gabor Greif | 177dd3f | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 139 | for (User::op_iterator i = I.op_begin(), e = I.op_end(); i != e; ++i) |
| 140 | if (Instruction *Op = dyn_cast<Instruction>(*i)) { |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 141 | AddToWorkList(Op); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 142 | // Set the operand to undef to drop the use. |
Gabor Greif | 177dd3f | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 143 | *i = UndefValue::get(Op->getType()); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | return R; |
| 147 | } |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 148 | |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 149 | public: |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 150 | virtual bool runOnFunction(Function &F); |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 151 | |
| 152 | bool DoOneIteration(Function &F, unsigned ItNum); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 153 | |
Chris Lattner | 97e52e4 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 154 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 155 | AU.addRequired<TargetData>(); |
Owen Anderson | d1b78a1 | 2006-07-10 19:03:49 +0000 | [diff] [blame] | 156 | AU.addPreservedID(LCSSAID); |
Chris Lattner | cb2610e | 2002-10-21 20:00:28 +0000 | [diff] [blame] | 157 | AU.setPreservesCFG(); |
Chris Lattner | 97e52e4 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 160 | TargetData &getTargetData() const { return *TD; } |
| 161 | |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 162 | // Visitation implementation - Implement instruction combining for different |
| 163 | // instruction types. The semantics are as follows: |
| 164 | // Return Value: |
| 165 | // null - No change was made |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 166 | // I - Change was made, I is still valid, I may be dead though |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 167 | // otherwise - Change was made, replace I with returned instruction |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 168 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 169 | Instruction *visitAdd(BinaryOperator &I); |
| 170 | Instruction *visitSub(BinaryOperator &I); |
| 171 | Instruction *visitMul(BinaryOperator &I); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 172 | Instruction *visitURem(BinaryOperator &I); |
| 173 | Instruction *visitSRem(BinaryOperator &I); |
| 174 | Instruction *visitFRem(BinaryOperator &I); |
| 175 | Instruction *commonRemTransforms(BinaryOperator &I); |
| 176 | Instruction *commonIRemTransforms(BinaryOperator &I); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 177 | Instruction *commonDivTransforms(BinaryOperator &I); |
| 178 | Instruction *commonIDivTransforms(BinaryOperator &I); |
| 179 | Instruction *visitUDiv(BinaryOperator &I); |
| 180 | Instruction *visitSDiv(BinaryOperator &I); |
| 181 | Instruction *visitFDiv(BinaryOperator &I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 182 | Instruction *visitAnd(BinaryOperator &I); |
| 183 | Instruction *visitOr (BinaryOperator &I); |
| 184 | Instruction *visitXor(BinaryOperator &I); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 185 | Instruction *visitShl(BinaryOperator &I); |
| 186 | Instruction *visitAShr(BinaryOperator &I); |
| 187 | Instruction *visitLShr(BinaryOperator &I); |
| 188 | Instruction *commonShiftTransforms(BinaryOperator &I); |
Chris Lattner | a540623 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 189 | Instruction *FoldFCmp_IntToFP_Cst(FCmpInst &I, Instruction *LHSI, |
| 190 | Constant *RHSC); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 191 | Instruction *visitFCmpInst(FCmpInst &I); |
| 192 | Instruction *visitICmpInst(ICmpInst &I); |
| 193 | Instruction *visitICmpInstWithCastAndCast(ICmpInst &ICI); |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 194 | Instruction *visitICmpInstWithInstAndIntCst(ICmpInst &ICI, |
| 195 | Instruction *LHS, |
| 196 | ConstantInt *RHS); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 197 | Instruction *FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI, |
| 198 | ConstantInt *DivRHS); |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 199 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 200 | Instruction *FoldGEPICmp(User *GEPLHS, Value *RHS, |
| 201 | ICmpInst::Predicate Cond, Instruction &I); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 202 | Instruction *FoldShiftByConstant(Value *Op0, ConstantInt *Op1, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 203 | BinaryOperator &I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 204 | Instruction *commonCastTransforms(CastInst &CI); |
| 205 | Instruction *commonIntCastTransforms(CastInst &CI); |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 206 | Instruction *commonPointerCastTransforms(CastInst &CI); |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 207 | Instruction *visitTrunc(TruncInst &CI); |
| 208 | Instruction *visitZExt(ZExtInst &CI); |
| 209 | Instruction *visitSExt(SExtInst &CI); |
Chris Lattner | b753065 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 210 | Instruction *visitFPTrunc(FPTruncInst &CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 211 | Instruction *visitFPExt(CastInst &CI); |
Chris Lattner | 0c7a9a0 | 2008-05-19 20:25:04 +0000 | [diff] [blame] | 212 | Instruction *visitFPToUI(FPToUIInst &FI); |
| 213 | Instruction *visitFPToSI(FPToSIInst &FI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 214 | Instruction *visitUIToFP(CastInst &CI); |
| 215 | Instruction *visitSIToFP(CastInst &CI); |
| 216 | Instruction *visitPtrToInt(CastInst &CI); |
Chris Lattner | f9d9e45 | 2008-01-08 07:23:51 +0000 | [diff] [blame] | 217 | Instruction *visitIntToPtr(IntToPtrInst &CI); |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 218 | Instruction *visitBitCast(BitCastInst &CI); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 219 | Instruction *FoldSelectOpOp(SelectInst &SI, Instruction *TI, |
| 220 | Instruction *FI); |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 221 | Instruction *visitSelectInst(SelectInst &CI); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 222 | Instruction *visitCallInst(CallInst &CI); |
| 223 | Instruction *visitInvokeInst(InvokeInst &II); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 224 | Instruction *visitPHINode(PHINode &PN); |
| 225 | Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP); |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 226 | Instruction *visitAllocationInst(AllocationInst &AI); |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 227 | Instruction *visitFreeInst(FreeInst &FI); |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 228 | Instruction *visitLoadInst(LoadInst &LI); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 229 | Instruction *visitStoreInst(StoreInst &SI); |
Chris Lattner | c4d10eb | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 230 | Instruction *visitBranchInst(BranchInst &BI); |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 231 | Instruction *visitSwitchInst(SwitchInst &SI); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 232 | Instruction *visitInsertElementInst(InsertElementInst &IE); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 233 | Instruction *visitExtractElementInst(ExtractElementInst &EI); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 234 | Instruction *visitShuffleVectorInst(ShuffleVectorInst &SVI); |
Matthijs Kooijman | a9012ec | 2008-06-11 14:05:05 +0000 | [diff] [blame] | 235 | Instruction *visitExtractValueInst(ExtractValueInst &EV); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 236 | |
| 237 | // visitInstruction - Specify what to return for unhandled instructions... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 238 | Instruction *visitInstruction(Instruction &I) { return 0; } |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 239 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 240 | private: |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 241 | Instruction *visitCallSite(CallSite CS); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 242 | bool transformConstExprCastCall(CallSite CS); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 243 | Instruction *transformCallThroughTrampoline(CallSite CS); |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 244 | Instruction *transformZExtICmp(ICmpInst *ICI, Instruction &CI, |
| 245 | bool DoXform = true); |
Chris Lattner | 3d28b1b | 2008-05-20 05:46:13 +0000 | [diff] [blame] | 246 | bool WillNotOverflowSignedAdd(Value *LHS, Value *RHS); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 247 | |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 248 | public: |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 249 | // InsertNewInstBefore - insert an instruction New before instruction Old |
| 250 | // in the program. Add the new instruction to the worklist. |
| 251 | // |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 252 | Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) { |
Chris Lattner | e6f9a91 | 2002-08-23 18:32:43 +0000 | [diff] [blame] | 253 | assert(New && New->getParent() == 0 && |
| 254 | "New instruction already inserted into a basic block!"); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 255 | BasicBlock *BB = Old.getParent(); |
| 256 | BB->getInstList().insert(&Old, New); // Insert inst |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 257 | AddToWorkList(New); |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 258 | return New; |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 261 | /// InsertCastBefore - Insert a cast of V to TY before the instruction POS. |
| 262 | /// This also adds the cast to the worklist. Finally, this returns the |
| 263 | /// cast. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 264 | Value *InsertCastBefore(Instruction::CastOps opc, Value *V, const Type *Ty, |
| 265 | Instruction &Pos) { |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 266 | if (V->getType() == Ty) return V; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 267 | |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 268 | if (Constant *CV = dyn_cast<Constant>(V)) |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 269 | return ConstantExpr::getCast(opc, CV, Ty); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 270 | |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 271 | Instruction *C = CastInst::Create(opc, V, Ty, V->getName(), &Pos); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 272 | AddToWorkList(C); |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 273 | return C; |
| 274 | } |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 275 | |
| 276 | Value *InsertBitCastBefore(Value *V, const Type *Ty, Instruction &Pos) { |
| 277 | return InsertCastBefore(Instruction::BitCast, V, Ty, Pos); |
| 278 | } |
| 279 | |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 280 | |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 281 | // ReplaceInstUsesWith - This method is to be used when an instruction is |
| 282 | // found to be dead, replacable with another preexisting expression. Here |
| 283 | // we add all uses of I to the worklist, replace all uses of I with the new |
| 284 | // value, then return I, so that the inst combiner will know that I was |
| 285 | // modified. |
| 286 | // |
| 287 | Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) { |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 288 | AddUsersToWorkList(I); // Add all modified instrs to worklist |
Chris Lattner | 15a76c0 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 289 | if (&I != V) { |
| 290 | I.replaceAllUsesWith(V); |
| 291 | return &I; |
| 292 | } else { |
| 293 | // If we are replacing the instruction with itself, this must be in a |
| 294 | // segment of unreachable code, so just clobber the instruction. |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 295 | I.replaceAllUsesWith(UndefValue::get(I.getType())); |
Chris Lattner | 15a76c0 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 296 | return &I; |
| 297 | } |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 298 | } |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 299 | |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 300 | // UpdateValueUsesWith - This method is to be used when an value is |
| 301 | // found to be replacable with another preexisting expression or was |
| 302 | // updated. Here we add all uses of I to the worklist, replace all uses of |
| 303 | // I with the new value (unless the instruction was just updated), then |
| 304 | // return true, so that the inst combiner will know that I was modified. |
| 305 | // |
| 306 | bool UpdateValueUsesWith(Value *Old, Value *New) { |
| 307 | AddUsersToWorkList(*Old); // Add all modified instrs to worklist |
| 308 | if (Old != New) |
| 309 | Old->replaceAllUsesWith(New); |
| 310 | if (Instruction *I = dyn_cast<Instruction>(Old)) |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 311 | AddToWorkList(I); |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 312 | if (Instruction *I = dyn_cast<Instruction>(New)) |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 313 | AddToWorkList(I); |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 314 | return true; |
| 315 | } |
| 316 | |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 317 | // EraseInstFromFunction - When dealing with an instruction that has side |
| 318 | // effects or produces a void value, we can't rely on DCE to delete the |
| 319 | // instruction. Instead, visit methods should return the value returned by |
| 320 | // this function. |
| 321 | Instruction *EraseInstFromFunction(Instruction &I) { |
| 322 | assert(I.use_empty() && "Cannot erase instruction that is used!"); |
| 323 | AddUsesToWorkList(I); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 324 | RemoveFromWorkList(&I); |
Chris Lattner | 954f66a | 2004-11-18 21:41:39 +0000 | [diff] [blame] | 325 | I.eraseFromParent(); |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 326 | return 0; // Don't do anything with FI |
| 327 | } |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 328 | |
| 329 | void ComputeMaskedBits(Value *V, const APInt &Mask, APInt &KnownZero, |
| 330 | APInt &KnownOne, unsigned Depth = 0) const { |
| 331 | return llvm::ComputeMaskedBits(V, Mask, KnownZero, KnownOne, TD, Depth); |
| 332 | } |
| 333 | |
| 334 | bool MaskedValueIsZero(Value *V, const APInt &Mask, |
| 335 | unsigned Depth = 0) const { |
| 336 | return llvm::MaskedValueIsZero(V, Mask, TD, Depth); |
| 337 | } |
| 338 | unsigned ComputeNumSignBits(Value *Op, unsigned Depth = 0) const { |
| 339 | return llvm::ComputeNumSignBits(Op, TD, Depth); |
| 340 | } |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 341 | |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 342 | private: |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 343 | /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the |
| 344 | /// InsertBefore instruction. This is specialized a bit to avoid inserting |
| 345 | /// casts that are known to not do anything... |
| 346 | /// |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 347 | Value *InsertOperandCastBefore(Instruction::CastOps opcode, |
| 348 | Value *V, const Type *DestTy, |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 349 | Instruction *InsertBefore); |
| 350 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 351 | /// SimplifyCommutative - This performs a few simplifications for |
| 352 | /// commutative operators. |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 353 | bool SimplifyCommutative(BinaryOperator &I); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 354 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 355 | /// SimplifyCompare - This reorders the operands of a CmpInst to get them in |
| 356 | /// most-complex to least-complex order. |
| 357 | bool SimplifyCompare(CmpInst &I); |
| 358 | |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 359 | /// SimplifyDemandedBits - Attempts to replace V with a simpler value based |
| 360 | /// on the demanded bits. |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 361 | bool SimplifyDemandedBits(Value *V, APInt DemandedMask, |
| 362 | APInt& KnownZero, APInt& KnownOne, |
| 363 | unsigned Depth = 0); |
| 364 | |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 365 | Value *SimplifyDemandedVectorElts(Value *V, uint64_t DemandedElts, |
| 366 | uint64_t &UndefElts, unsigned Depth = 0); |
| 367 | |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 368 | // FoldOpIntoPhi - Given a binary operator or cast instruction which has a |
| 369 | // PHI node as operand #0, see if we can fold the instruction into the PHI |
| 370 | // (which is only possible if all operands to the PHI are constants). |
| 371 | Instruction *FoldOpIntoPhi(Instruction &I); |
| 372 | |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 373 | // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary" |
| 374 | // operator and they all are only used by the PHI, PHI together their |
| 375 | // inputs, and do the operation once, to the result of the PHI. |
| 376 | Instruction *FoldPHIArgOpIntoPHI(PHINode &PN); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 377 | Instruction *FoldPHIArgBinOpIntoPHI(PHINode &PN); |
| 378 | |
| 379 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 380 | Instruction *OptAndOp(Instruction *Op, ConstantInt *OpRHS, |
| 381 | ConstantInt *AndRHS, BinaryOperator &TheAnd); |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 382 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 383 | Value *FoldLogicalPlusAnd(Value *LHS, Value *RHS, ConstantInt *Mask, |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 384 | bool isSub, Instruction &I); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 385 | Instruction *InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 386 | bool isSigned, bool Inside, Instruction &IB); |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 387 | Instruction *PromoteCastOfAllocation(BitCastInst &CI, AllocationInst &AI); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 388 | Instruction *MatchBSwap(BinaryOperator &I); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 389 | bool SimplifyStoreAtEndOfBlock(StoreInst &SI); |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 390 | Instruction *SimplifyMemTransfer(MemIntrinsic *MI); |
Chris Lattner | 69ea9d2 | 2008-04-30 06:39:11 +0000 | [diff] [blame] | 391 | Instruction *SimplifyMemSet(MemSetInst *MI); |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 392 | |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 393 | |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 394 | Value *EvaluateInDifferentType(Value *V, const Type *Ty, bool isSigned); |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 395 | |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 396 | bool CanEvaluateInDifferentType(Value *V, const IntegerType *Ty, |
| 397 | unsigned CastOpc, |
| 398 | int &NumCastsRemoved); |
| 399 | unsigned GetOrEnforceKnownAlignment(Value *V, |
| 400 | unsigned PrefAlign = 0); |
Matthijs Kooijman | a9012ec | 2008-06-11 14:05:05 +0000 | [diff] [blame] | 401 | |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 402 | }; |
| 403 | } |
| 404 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 405 | char InstCombiner::ID = 0; |
| 406 | static RegisterPass<InstCombiner> |
| 407 | X("instcombine", "Combine redundant instructions"); |
| 408 | |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 409 | // getComplexity: Assign a complexity or rank value to LLVM Values... |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 410 | // 0 -> undef, 1 -> Const, 2 -> Other, 3 -> Arg, 3 -> Unary, 4 -> OtherInst |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 411 | static unsigned getComplexity(Value *V) { |
| 412 | if (isa<Instruction>(V)) { |
| 413 | if (BinaryOperator::isNeg(V) || BinaryOperator::isNot(V)) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 414 | return 3; |
| 415 | return 4; |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 416 | } |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 417 | if (isa<Argument>(V)) return 3; |
| 418 | return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2; |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 419 | } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 420 | |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 421 | // isOnlyUse - Return true if this instruction will be deleted if we stop using |
| 422 | // it. |
| 423 | static bool isOnlyUse(Value *V) { |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 424 | return V->hasOneUse() || isa<Constant>(V); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 427 | // getPromotedType - Return the specified type promoted as it would be to pass |
| 428 | // though a va_arg area... |
| 429 | static const Type *getPromotedType(const Type *Ty) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 430 | if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty)) { |
| 431 | if (ITy->getBitWidth() < 32) |
| 432 | return Type::Int32Ty; |
Chris Lattner | 2b7e0ad | 2007-05-23 01:17:04 +0000 | [diff] [blame] | 433 | } |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 434 | return Ty; |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 437 | /// getBitCastOperand - If the specified operand is a CastInst or a constant |
| 438 | /// expression bitcast, return the operand value, otherwise return null. |
| 439 | static Value *getBitCastOperand(Value *V) { |
| 440 | if (BitCastInst *I = dyn_cast<BitCastInst>(V)) |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 441 | return I->getOperand(0); |
| 442 | else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 443 | if (CE->getOpcode() == Instruction::BitCast) |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 444 | return CE->getOperand(0); |
| 445 | return 0; |
| 446 | } |
| 447 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 448 | /// This function is a wrapper around CastInst::isEliminableCastPair. It |
| 449 | /// simply extracts arguments and returns what that function returns. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 450 | static Instruction::CastOps |
| 451 | isEliminableCastPair( |
| 452 | const CastInst *CI, ///< The first cast instruction |
| 453 | unsigned opcode, ///< The opcode of the second cast instruction |
| 454 | const Type *DstTy, ///< The target type for the second cast instruction |
| 455 | TargetData *TD ///< The target data for pointer size |
| 456 | ) { |
| 457 | |
| 458 | const Type *SrcTy = CI->getOperand(0)->getType(); // A from above |
| 459 | const Type *MidTy = CI->getType(); // B from above |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 460 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 461 | // Get the opcodes of the two Cast instructions |
| 462 | Instruction::CastOps firstOp = Instruction::CastOps(CI->getOpcode()); |
| 463 | Instruction::CastOps secondOp = Instruction::CastOps(opcode); |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 464 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 465 | return Instruction::CastOps( |
| 466 | CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, |
| 467 | DstTy, TD->getIntPtrType())); |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | /// ValueRequiresCast - Return true if the cast from "V to Ty" actually results |
| 471 | /// in any code being generated. It does not require codegen if V is simple |
| 472 | /// enough or if the cast can be folded into other casts. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 473 | static bool ValueRequiresCast(Instruction::CastOps opcode, const Value *V, |
| 474 | const Type *Ty, TargetData *TD) { |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 475 | if (V->getType() == Ty || isa<Constant>(V)) return false; |
| 476 | |
Chris Lattner | 01575b7 | 2006-05-25 23:24:33 +0000 | [diff] [blame] | 477 | // If this is another cast that can be eliminated, it isn't codegen either. |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 478 | if (const CastInst *CI = dyn_cast<CastInst>(V)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 479 | if (isEliminableCastPair(CI, opcode, Ty, TD)) |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 480 | return false; |
| 481 | return true; |
| 482 | } |
| 483 | |
| 484 | /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the |
| 485 | /// InsertBefore instruction. This is specialized a bit to avoid inserting |
| 486 | /// casts that are known to not do anything... |
| 487 | /// |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 488 | Value *InstCombiner::InsertOperandCastBefore(Instruction::CastOps opcode, |
| 489 | Value *V, const Type *DestTy, |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 490 | Instruction *InsertBefore) { |
| 491 | if (V->getType() == DestTy) return V; |
| 492 | if (Constant *C = dyn_cast<Constant>(V)) |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 493 | return ConstantExpr::getCast(opcode, C, DestTy); |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 494 | |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 495 | return InsertCastBefore(opcode, V, DestTy, *InsertBefore); |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 498 | // SimplifyCommutative - This performs a few simplifications for commutative |
| 499 | // operators: |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 500 | // |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 501 | // 1. Order operands such that they are listed from right (least complex) to |
| 502 | // left (most complex). This puts constants before unary operators before |
| 503 | // binary operators. |
| 504 | // |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 505 | // 2. Transform: (op (op V, C1), C2) ==> (op V, (op C1, C2)) |
| 506 | // 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] | 507 | // |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 508 | bool InstCombiner::SimplifyCommutative(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 509 | bool Changed = false; |
| 510 | if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1))) |
| 511 | Changed = !I.swapOperands(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 512 | |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 513 | if (!I.isAssociative()) return Changed; |
| 514 | Instruction::BinaryOps Opcode = I.getOpcode(); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 515 | if (BinaryOperator *Op = dyn_cast<BinaryOperator>(I.getOperand(0))) |
| 516 | if (Op->getOpcode() == Opcode && isa<Constant>(Op->getOperand(1))) { |
| 517 | if (isa<Constant>(I.getOperand(1))) { |
Chris Lattner | 2a9c847 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 518 | Constant *Folded = ConstantExpr::get(I.getOpcode(), |
| 519 | cast<Constant>(I.getOperand(1)), |
| 520 | cast<Constant>(Op->getOperand(1))); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 521 | I.setOperand(0, Op->getOperand(0)); |
| 522 | I.setOperand(1, Folded); |
| 523 | return true; |
| 524 | } else if (BinaryOperator *Op1=dyn_cast<BinaryOperator>(I.getOperand(1))) |
| 525 | if (Op1->getOpcode() == Opcode && isa<Constant>(Op1->getOperand(1)) && |
| 526 | isOnlyUse(Op) && isOnlyUse(Op1)) { |
| 527 | Constant *C1 = cast<Constant>(Op->getOperand(1)); |
| 528 | Constant *C2 = cast<Constant>(Op1->getOperand(1)); |
| 529 | |
| 530 | // Fold (op (op V1, C1), (op V2, C2)) ==> (op (op V1, V2), (op C1,C2)) |
Chris Lattner | 2a9c847 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 531 | Constant *Folded = ConstantExpr::get(I.getOpcode(), C1, C2); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 532 | Instruction *New = BinaryOperator::Create(Opcode, Op->getOperand(0), |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 533 | Op1->getOperand(0), |
| 534 | Op1->getName(), &I); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 535 | AddToWorkList(New); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 536 | I.setOperand(0, New); |
| 537 | I.setOperand(1, Folded); |
| 538 | return true; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 539 | } |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 540 | } |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 541 | return Changed; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 542 | } |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 543 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 544 | /// SimplifyCompare - For a CmpInst this function just orders the operands |
| 545 | /// so that theyare listed from right (least complex) to left (most complex). |
| 546 | /// This puts constants before unary operators before binary operators. |
| 547 | bool InstCombiner::SimplifyCompare(CmpInst &I) { |
| 548 | if (getComplexity(I.getOperand(0)) >= getComplexity(I.getOperand(1))) |
| 549 | return false; |
| 550 | I.swapOperands(); |
| 551 | // Compare instructions are not associative so there's nothing else we can do. |
| 552 | return true; |
| 553 | } |
| 554 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 555 | // dyn_castNegVal - Given a 'sub' instruction, return the RHS of the instruction |
| 556 | // if the LHS is a constant zero (which is the 'negate' form). |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 557 | // |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 558 | static inline Value *dyn_castNegVal(Value *V) { |
| 559 | if (BinaryOperator::isNeg(V)) |
Chris Lattner | a1df33c | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 560 | return BinaryOperator::getNegArgument(V); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 561 | |
Chris Lattner | 0ce8580 | 2004-12-14 20:08:06 +0000 | [diff] [blame] | 562 | // Constants can be considered to be negated values if they can be folded. |
| 563 | if (ConstantInt *C = dyn_cast<ConstantInt>(V)) |
| 564 | return ConstantExpr::getNeg(C); |
Nick Lewycky | 18b3da6 | 2008-05-23 04:54:45 +0000 | [diff] [blame] | 565 | |
| 566 | if (ConstantVector *C = dyn_cast<ConstantVector>(V)) |
| 567 | if (C->getType()->getElementType()->isInteger()) |
| 568 | return ConstantExpr::getNeg(C); |
| 569 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 570 | return 0; |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 571 | } |
| 572 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 573 | static inline Value *dyn_castNotVal(Value *V) { |
| 574 | if (BinaryOperator::isNot(V)) |
Chris Lattner | a1df33c | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 575 | return BinaryOperator::getNotArgument(V); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 576 | |
| 577 | // Constants can be considered to be not'ed values... |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 578 | if (ConstantInt *C = dyn_cast<ConstantInt>(V)) |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 579 | return ConstantInt::get(~C->getValue()); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 580 | return 0; |
| 581 | } |
| 582 | |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 583 | // dyn_castFoldableMul - If this value is a multiply that can be folded into |
| 584 | // other computations (because it has a constant operand), return the |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 585 | // non-constant operand of the multiply, and set CST to point to the multiplier. |
| 586 | // Otherwise, return null. |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 587 | // |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 588 | static inline Value *dyn_castFoldableMul(Value *V, ConstantInt *&CST) { |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 589 | if (V->hasOneUse() && V->getType()->isInteger()) |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 590 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 591 | if (I->getOpcode() == Instruction::Mul) |
Chris Lattner | 50e60c7 | 2004-11-15 05:54:07 +0000 | [diff] [blame] | 592 | if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 593 | return I->getOperand(0); |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 594 | if (I->getOpcode() == Instruction::Shl) |
Chris Lattner | 50e60c7 | 2004-11-15 05:54:07 +0000 | [diff] [blame] | 595 | if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) { |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 596 | // The multiplier is really 1 << CST. |
Zhou Sheng | 97b52c2 | 2007-03-29 01:57:21 +0000 | [diff] [blame] | 597 | uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth(); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 598 | uint32_t CSTVal = CST->getLimitedValue(BitWidth); |
Zhou Sheng | 97b52c2 | 2007-03-29 01:57:21 +0000 | [diff] [blame] | 599 | CST = ConstantInt::get(APInt(BitWidth, 1).shl(CSTVal)); |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 600 | return I->getOperand(0); |
| 601 | } |
| 602 | } |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 603 | return 0; |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 604 | } |
Chris Lattner | af2930e | 2002-08-14 17:51:49 +0000 | [diff] [blame] | 605 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 606 | /// dyn_castGetElementPtr - If this is a getelementptr instruction or constant |
| 607 | /// expression, return it. |
| 608 | static User *dyn_castGetElementPtr(Value *V) { |
| 609 | if (isa<GetElementPtrInst>(V)) return cast<User>(V); |
| 610 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
| 611 | if (CE->getOpcode() == Instruction::GetElementPtr) |
| 612 | return cast<User>(V); |
| 613 | return false; |
| 614 | } |
| 615 | |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 616 | /// getOpcode - If this is an Instruction or a ConstantExpr, return the |
| 617 | /// opcode value. Otherwise return UserOp1. |
Dan Gohman | b99e2e2 | 2008-05-29 19:53:46 +0000 | [diff] [blame] | 618 | static unsigned getOpcode(const Value *V) { |
| 619 | if (const Instruction *I = dyn_cast<Instruction>(V)) |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 620 | return I->getOpcode(); |
Dan Gohman | b99e2e2 | 2008-05-29 19:53:46 +0000 | [diff] [blame] | 621 | if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 622 | return CE->getOpcode(); |
| 623 | // Use UserOp1 to mean there's no opcode. |
| 624 | return Instruction::UserOp1; |
| 625 | } |
| 626 | |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 627 | /// AddOne - Add one to a ConstantInt |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 628 | static ConstantInt *AddOne(ConstantInt *C) { |
Reid Spencer | 2149a9d | 2007-03-25 19:55:33 +0000 | [diff] [blame] | 629 | APInt Val(C->getValue()); |
| 630 | return ConstantInt::get(++Val); |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 631 | } |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 632 | /// SubOne - Subtract one from a ConstantInt |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 633 | static ConstantInt *SubOne(ConstantInt *C) { |
Reid Spencer | 2149a9d | 2007-03-25 19:55:33 +0000 | [diff] [blame] | 634 | APInt Val(C->getValue()); |
| 635 | return ConstantInt::get(--Val); |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 636 | } |
| 637 | /// Add - Add two ConstantInts together |
| 638 | static ConstantInt *Add(ConstantInt *C1, ConstantInt *C2) { |
| 639 | return ConstantInt::get(C1->getValue() + C2->getValue()); |
| 640 | } |
| 641 | /// And - Bitwise AND two ConstantInts together |
| 642 | static ConstantInt *And(ConstantInt *C1, ConstantInt *C2) { |
| 643 | return ConstantInt::get(C1->getValue() & C2->getValue()); |
| 644 | } |
| 645 | /// Subtract - Subtract one ConstantInt from another |
| 646 | static ConstantInt *Subtract(ConstantInt *C1, ConstantInt *C2) { |
| 647 | return ConstantInt::get(C1->getValue() - C2->getValue()); |
| 648 | } |
| 649 | /// Multiply - Multiply two ConstantInts together |
| 650 | static ConstantInt *Multiply(ConstantInt *C1, ConstantInt *C2) { |
| 651 | return ConstantInt::get(C1->getValue() * C2->getValue()); |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 652 | } |
Nick Lewycky | e0cfecf | 2008-02-18 22:48:05 +0000 | [diff] [blame] | 653 | /// MultiplyOverflows - True if the multiply can not be expressed in an int |
| 654 | /// this size. |
| 655 | static bool MultiplyOverflows(ConstantInt *C1, ConstantInt *C2, bool sign) { |
| 656 | uint32_t W = C1->getBitWidth(); |
| 657 | APInt LHSExt = C1->getValue(), RHSExt = C2->getValue(); |
| 658 | if (sign) { |
| 659 | LHSExt.sext(W * 2); |
| 660 | RHSExt.sext(W * 2); |
| 661 | } else { |
| 662 | LHSExt.zext(W * 2); |
| 663 | RHSExt.zext(W * 2); |
| 664 | } |
| 665 | |
| 666 | APInt MulExt = LHSExt * RHSExt; |
| 667 | |
| 668 | if (sign) { |
| 669 | APInt Min = APInt::getSignedMinValue(W).sext(W * 2); |
| 670 | APInt Max = APInt::getSignedMaxValue(W).sext(W * 2); |
| 671 | return MulExt.slt(Min) || MulExt.sgt(Max); |
| 672 | } else |
| 673 | return MulExt.ugt(APInt::getLowBitsSet(W * 2, W)); |
| 674 | } |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 675 | |
Reid Spencer | e7816b5 | 2007-03-08 01:52:58 +0000 | [diff] [blame] | 676 | |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 677 | /// ShrinkDemandedConstant - Check to see if the specified operand of the |
| 678 | /// specified instruction is a constant integer. If so, check to see if there |
| 679 | /// are any bits set in the constant that are not demanded. If so, shrink the |
| 680 | /// constant and return true. |
| 681 | static bool ShrinkDemandedConstant(Instruction *I, unsigned OpNo, |
Reid Spencer | 6b79e2d | 2007-03-12 17:15:10 +0000 | [diff] [blame] | 682 | APInt Demanded) { |
| 683 | assert(I && "No instruction?"); |
| 684 | assert(OpNo < I->getNumOperands() && "Operand index too large"); |
| 685 | |
| 686 | // If the operand is not a constant integer, nothing to do. |
| 687 | ConstantInt *OpC = dyn_cast<ConstantInt>(I->getOperand(OpNo)); |
| 688 | if (!OpC) return false; |
| 689 | |
| 690 | // If there are no bits set that aren't demanded, nothing to do. |
| 691 | Demanded.zextOrTrunc(OpC->getValue().getBitWidth()); |
| 692 | if ((~Demanded & OpC->getValue()) == 0) |
| 693 | return false; |
| 694 | |
| 695 | // This instruction is producing bits that are not demanded. Shrink the RHS. |
| 696 | Demanded &= OpC->getValue(); |
| 697 | I->setOperand(OpNo, ConstantInt::get(Demanded)); |
| 698 | return true; |
| 699 | } |
| 700 | |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 701 | // ComputeSignedMinMaxValuesFromKnownBits - Given a signed integer type and a |
| 702 | // set of known zero and one bits, compute the maximum and minimum values that |
| 703 | // could have the specified known zero and known one bits, returning them in |
| 704 | // min/max. |
| 705 | static void ComputeSignedMinMaxValuesFromKnownBits(const Type *Ty, |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 706 | const APInt& KnownZero, |
| 707 | const APInt& KnownOne, |
| 708 | APInt& Min, APInt& Max) { |
| 709 | uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth(); |
| 710 | assert(KnownZero.getBitWidth() == BitWidth && |
| 711 | KnownOne.getBitWidth() == BitWidth && |
| 712 | Min.getBitWidth() == BitWidth && Max.getBitWidth() == BitWidth && |
| 713 | "Ty, KnownZero, KnownOne and Min, Max must have equal bitwidth."); |
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 714 | APInt UnknownBits = ~(KnownZero|KnownOne); |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 715 | |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 716 | // The minimum value is when all unknown bits are zeros, EXCEPT for the sign |
| 717 | // bit if it is unknown. |
| 718 | Min = KnownOne; |
| 719 | Max = KnownOne|UnknownBits; |
| 720 | |
Zhou Sheng | 4acf155 | 2007-03-28 05:15:57 +0000 | [diff] [blame] | 721 | if (UnknownBits[BitWidth-1]) { // Sign bit is unknown |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 722 | Min.set(BitWidth-1); |
| 723 | Max.clear(BitWidth-1); |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 724 | } |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | // ComputeUnsignedMinMaxValuesFromKnownBits - Given an unsigned integer type and |
| 728 | // a set of known zero and one bits, compute the maximum and minimum values that |
| 729 | // could have the specified known zero and known one bits, returning them in |
| 730 | // min/max. |
| 731 | static void ComputeUnsignedMinMaxValuesFromKnownBits(const Type *Ty, |
Chris Lattner | a9ff5eb | 2007-08-05 08:47:58 +0000 | [diff] [blame] | 732 | const APInt &KnownZero, |
| 733 | const APInt &KnownOne, |
| 734 | APInt &Min, APInt &Max) { |
| 735 | uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth(); BitWidth = BitWidth; |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 736 | assert(KnownZero.getBitWidth() == BitWidth && |
| 737 | KnownOne.getBitWidth() == BitWidth && |
| 738 | Min.getBitWidth() == BitWidth && Max.getBitWidth() && |
| 739 | "Ty, KnownZero, KnownOne and Min, Max must have equal bitwidth."); |
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 740 | APInt UnknownBits = ~(KnownZero|KnownOne); |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 741 | |
| 742 | // The minimum value is when the unknown bits are all zeros. |
| 743 | Min = KnownOne; |
| 744 | // The maximum value is when the unknown bits are all ones. |
| 745 | Max = KnownOne|UnknownBits; |
| 746 | } |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 747 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 748 | /// SimplifyDemandedBits - This function attempts to replace V with a simpler |
| 749 | /// value based on the demanded bits. When this function is called, it is known |
| 750 | /// that only the bits set in DemandedMask of the result of V are ever used |
| 751 | /// downstream. Consequently, depending on the mask and V, it may be possible |
| 752 | /// to replace V with a constant or one of its operands. In such cases, this |
| 753 | /// function does the replacement and returns true. In all other cases, it |
| 754 | /// returns false after analyzing the expression and setting KnownOne and known |
| 755 | /// to be one in the expression. KnownZero contains all the bits that are known |
| 756 | /// to be zero in the expression. These are provided to potentially allow the |
| 757 | /// caller (which might recursively be SimplifyDemandedBits itself) to simplify |
| 758 | /// the expression. KnownOne and KnownZero always follow the invariant that |
| 759 | /// KnownOne & KnownZero == 0. That is, a bit can't be both 1 and 0. Note that |
| 760 | /// the bits in KnownOne and KnownZero may only be accurate for those bits set |
| 761 | /// in DemandedMask. Note also that the bitwidth of V, DemandedMask, KnownZero |
| 762 | /// and KnownOne must all be the same. |
| 763 | bool InstCombiner::SimplifyDemandedBits(Value *V, APInt DemandedMask, |
| 764 | APInt& KnownZero, APInt& KnownOne, |
| 765 | unsigned Depth) { |
| 766 | assert(V != 0 && "Null pointer of Value???"); |
| 767 | assert(Depth <= 6 && "Limit Search Depth"); |
| 768 | uint32_t BitWidth = DemandedMask.getBitWidth(); |
| 769 | const IntegerType *VTy = cast<IntegerType>(V->getType()); |
| 770 | assert(VTy->getBitWidth() == BitWidth && |
| 771 | KnownZero.getBitWidth() == BitWidth && |
| 772 | KnownOne.getBitWidth() == BitWidth && |
| 773 | "Value *V, DemandedMask, KnownZero and KnownOne \ |
| 774 | must have same BitWidth"); |
| 775 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
| 776 | // We know all of the bits for a constant! |
| 777 | KnownOne = CI->getValue() & DemandedMask; |
| 778 | KnownZero = ~KnownOne & DemandedMask; |
| 779 | return false; |
| 780 | } |
| 781 | |
Zhou Sheng | 9670445 | 2007-03-14 03:21:24 +0000 | [diff] [blame] | 782 | KnownZero.clear(); |
| 783 | KnownOne.clear(); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 784 | if (!V->hasOneUse()) { // Other users may use these bits. |
| 785 | if (Depth != 0) { // Not at the root. |
| 786 | // Just compute the KnownZero/KnownOne bits to simplify things downstream. |
| 787 | ComputeMaskedBits(V, DemandedMask, KnownZero, KnownOne, Depth); |
| 788 | return false; |
| 789 | } |
| 790 | // If this is the root being simplified, allow it to have multiple uses, |
| 791 | // just set the DemandedMask to all bits. |
| 792 | DemandedMask = APInt::getAllOnesValue(BitWidth); |
| 793 | } else if (DemandedMask == 0) { // Not demanding any bits from V. |
| 794 | if (V != UndefValue::get(VTy)) |
| 795 | return UpdateValueUsesWith(V, UndefValue::get(VTy)); |
| 796 | return false; |
| 797 | } else if (Depth == 6) { // Limit search depth. |
| 798 | return false; |
| 799 | } |
| 800 | |
| 801 | Instruction *I = dyn_cast<Instruction>(V); |
| 802 | if (!I) return false; // Only analyze instructions. |
| 803 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 804 | APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0); |
| 805 | APInt &RHSKnownZero = KnownZero, &RHSKnownOne = KnownOne; |
| 806 | switch (I->getOpcode()) { |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 807 | default: |
| 808 | ComputeMaskedBits(V, DemandedMask, RHSKnownZero, RHSKnownOne, Depth); |
| 809 | break; |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 810 | case Instruction::And: |
| 811 | // If either the LHS or the RHS are Zero, the result is zero. |
| 812 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, |
| 813 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 814 | return true; |
| 815 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 816 | "Bits known to be one AND zero?"); |
| 817 | |
| 818 | // If something is known zero on the RHS, the bits aren't demanded on the |
| 819 | // LHS. |
| 820 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask & ~RHSKnownZero, |
| 821 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 822 | return true; |
| 823 | assert((LHSKnownZero & LHSKnownOne) == 0 && |
| 824 | "Bits known to be one AND zero?"); |
| 825 | |
| 826 | // If all of the demanded bits are known 1 on one side, return the other. |
| 827 | // These bits cannot contribute to the result of the 'and'. |
| 828 | if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) == |
| 829 | (DemandedMask & ~LHSKnownZero)) |
| 830 | return UpdateValueUsesWith(I, I->getOperand(0)); |
| 831 | if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) == |
| 832 | (DemandedMask & ~RHSKnownZero)) |
| 833 | return UpdateValueUsesWith(I, I->getOperand(1)); |
| 834 | |
| 835 | // If all of the demanded bits in the inputs are known zeros, return zero. |
| 836 | if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask) |
| 837 | return UpdateValueUsesWith(I, Constant::getNullValue(VTy)); |
| 838 | |
| 839 | // If the RHS is a constant, see if we can simplify it. |
| 840 | if (ShrinkDemandedConstant(I, 1, DemandedMask & ~LHSKnownZero)) |
| 841 | return UpdateValueUsesWith(I, I); |
| 842 | |
| 843 | // Output known-1 bits are only known if set in both the LHS & RHS. |
| 844 | RHSKnownOne &= LHSKnownOne; |
| 845 | // Output known-0 are known to be clear if zero in either the LHS | RHS. |
| 846 | RHSKnownZero |= LHSKnownZero; |
| 847 | break; |
| 848 | case Instruction::Or: |
| 849 | // If either the LHS or the RHS are One, the result is One. |
| 850 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, |
| 851 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 852 | return true; |
| 853 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 854 | "Bits known to be one AND zero?"); |
| 855 | // If something is known one on the RHS, the bits aren't demanded on the |
| 856 | // LHS. |
| 857 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask & ~RHSKnownOne, |
| 858 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 859 | return true; |
| 860 | assert((LHSKnownZero & LHSKnownOne) == 0 && |
| 861 | "Bits known to be one AND zero?"); |
| 862 | |
| 863 | // If all of the demanded bits are known zero on one side, return the other. |
| 864 | // These bits cannot contribute to the result of the 'or'. |
| 865 | if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) == |
| 866 | (DemandedMask & ~LHSKnownOne)) |
| 867 | return UpdateValueUsesWith(I, I->getOperand(0)); |
| 868 | if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) == |
| 869 | (DemandedMask & ~RHSKnownOne)) |
| 870 | return UpdateValueUsesWith(I, I->getOperand(1)); |
| 871 | |
| 872 | // If all of the potentially set bits on one side are known to be set on |
| 873 | // the other side, just use the 'other' side. |
| 874 | if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) == |
| 875 | (DemandedMask & (~RHSKnownZero))) |
| 876 | return UpdateValueUsesWith(I, I->getOperand(0)); |
| 877 | if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) == |
| 878 | (DemandedMask & (~LHSKnownZero))) |
| 879 | return UpdateValueUsesWith(I, I->getOperand(1)); |
| 880 | |
| 881 | // If the RHS is a constant, see if we can simplify it. |
| 882 | if (ShrinkDemandedConstant(I, 1, DemandedMask)) |
| 883 | return UpdateValueUsesWith(I, I); |
| 884 | |
| 885 | // Output known-0 bits are only known if clear in both the LHS & RHS. |
| 886 | RHSKnownZero &= LHSKnownZero; |
| 887 | // Output known-1 are known to be set if set in either the LHS | RHS. |
| 888 | RHSKnownOne |= LHSKnownOne; |
| 889 | break; |
| 890 | case Instruction::Xor: { |
| 891 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, |
| 892 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 893 | return true; |
| 894 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 895 | "Bits known to be one AND zero?"); |
| 896 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, |
| 897 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 898 | return true; |
| 899 | assert((LHSKnownZero & LHSKnownOne) == 0 && |
| 900 | "Bits known to be one AND zero?"); |
| 901 | |
| 902 | // If all of the demanded bits are known zero on one side, return the other. |
| 903 | // These bits cannot contribute to the result of the 'xor'. |
| 904 | if ((DemandedMask & RHSKnownZero) == DemandedMask) |
| 905 | return UpdateValueUsesWith(I, I->getOperand(0)); |
| 906 | if ((DemandedMask & LHSKnownZero) == DemandedMask) |
| 907 | return UpdateValueUsesWith(I, I->getOperand(1)); |
| 908 | |
| 909 | // Output known-0 bits are known if clear or set in both the LHS & RHS. |
| 910 | APInt KnownZeroOut = (RHSKnownZero & LHSKnownZero) | |
| 911 | (RHSKnownOne & LHSKnownOne); |
| 912 | // Output known-1 are known to be set if set in only one of the LHS, RHS. |
| 913 | APInt KnownOneOut = (RHSKnownZero & LHSKnownOne) | |
| 914 | (RHSKnownOne & LHSKnownZero); |
| 915 | |
| 916 | // If all of the demanded bits are known to be zero on one side or the |
| 917 | // other, turn this into an *inclusive* or. |
| 918 | // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0 |
| 919 | if ((DemandedMask & ~RHSKnownZero & ~LHSKnownZero) == 0) { |
| 920 | Instruction *Or = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 921 | BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1), |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 922 | I->getName()); |
| 923 | InsertNewInstBefore(Or, *I); |
| 924 | return UpdateValueUsesWith(I, Or); |
| 925 | } |
| 926 | |
| 927 | // If all of the demanded bits on one side are known, and all of the set |
| 928 | // bits on that side are also known to be set on the other side, turn this |
| 929 | // into an AND, as we know the bits will be cleared. |
| 930 | // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2 |
| 931 | if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) { |
| 932 | // all known |
| 933 | if ((RHSKnownOne & LHSKnownOne) == RHSKnownOne) { |
| 934 | Constant *AndC = ConstantInt::get(~RHSKnownOne & DemandedMask); |
| 935 | Instruction *And = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 936 | BinaryOperator::CreateAnd(I->getOperand(0), AndC, "tmp"); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 937 | InsertNewInstBefore(And, *I); |
| 938 | return UpdateValueUsesWith(I, And); |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | // If the RHS is a constant, see if we can simplify it. |
| 943 | // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1. |
| 944 | if (ShrinkDemandedConstant(I, 1, DemandedMask)) |
| 945 | return UpdateValueUsesWith(I, I); |
| 946 | |
| 947 | RHSKnownZero = KnownZeroOut; |
| 948 | RHSKnownOne = KnownOneOut; |
| 949 | break; |
| 950 | } |
| 951 | case Instruction::Select: |
| 952 | if (SimplifyDemandedBits(I->getOperand(2), DemandedMask, |
| 953 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 954 | return true; |
| 955 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, |
| 956 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 957 | return true; |
| 958 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 959 | "Bits known to be one AND zero?"); |
| 960 | assert((LHSKnownZero & LHSKnownOne) == 0 && |
| 961 | "Bits known to be one AND zero?"); |
| 962 | |
| 963 | // If the operands are constants, see if we can simplify them. |
| 964 | if (ShrinkDemandedConstant(I, 1, DemandedMask)) |
| 965 | return UpdateValueUsesWith(I, I); |
| 966 | if (ShrinkDemandedConstant(I, 2, DemandedMask)) |
| 967 | return UpdateValueUsesWith(I, I); |
| 968 | |
| 969 | // Only known if known in both the LHS and RHS. |
| 970 | RHSKnownOne &= LHSKnownOne; |
| 971 | RHSKnownZero &= LHSKnownZero; |
| 972 | break; |
| 973 | case Instruction::Trunc: { |
| 974 | uint32_t truncBf = |
| 975 | cast<IntegerType>(I->getOperand(0)->getType())->getBitWidth(); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 976 | DemandedMask.zext(truncBf); |
| 977 | RHSKnownZero.zext(truncBf); |
| 978 | RHSKnownOne.zext(truncBf); |
| 979 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, |
| 980 | RHSKnownZero, RHSKnownOne, Depth+1)) |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 981 | return true; |
| 982 | DemandedMask.trunc(BitWidth); |
| 983 | RHSKnownZero.trunc(BitWidth); |
| 984 | RHSKnownOne.trunc(BitWidth); |
| 985 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 986 | "Bits known to be one AND zero?"); |
| 987 | break; |
| 988 | } |
| 989 | case Instruction::BitCast: |
| 990 | if (!I->getOperand(0)->getType()->isInteger()) |
| 991 | return false; |
| 992 | |
| 993 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, |
| 994 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 995 | return true; |
| 996 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 997 | "Bits known to be one AND zero?"); |
| 998 | break; |
| 999 | case Instruction::ZExt: { |
| 1000 | // Compute the bits in the result that are not present in the input. |
| 1001 | const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType()); |
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 1002 | uint32_t SrcBitWidth = SrcTy->getBitWidth(); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1003 | |
Zhou Sheng | d48653a | 2007-03-29 04:45:55 +0000 | [diff] [blame] | 1004 | DemandedMask.trunc(SrcBitWidth); |
| 1005 | RHSKnownZero.trunc(SrcBitWidth); |
| 1006 | RHSKnownOne.trunc(SrcBitWidth); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1007 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, |
| 1008 | RHSKnownZero, RHSKnownOne, Depth+1)) |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1009 | return true; |
| 1010 | DemandedMask.zext(BitWidth); |
| 1011 | RHSKnownZero.zext(BitWidth); |
| 1012 | RHSKnownOne.zext(BitWidth); |
| 1013 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1014 | "Bits known to be one AND zero?"); |
| 1015 | // The top bits are known to be zero. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1016 | RHSKnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1017 | break; |
| 1018 | } |
| 1019 | case Instruction::SExt: { |
| 1020 | // Compute the bits in the result that are not present in the input. |
| 1021 | const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType()); |
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 1022 | uint32_t SrcBitWidth = SrcTy->getBitWidth(); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1023 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1024 | APInt InputDemandedBits = DemandedMask & |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1025 | APInt::getLowBitsSet(BitWidth, SrcBitWidth); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1026 | |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1027 | APInt NewBits(APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1028 | // If any of the sign extended bits are demanded, we know that the sign |
| 1029 | // bit is demanded. |
| 1030 | if ((NewBits & DemandedMask) != 0) |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 1031 | InputDemandedBits.set(SrcBitWidth-1); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1032 | |
Zhou Sheng | d48653a | 2007-03-29 04:45:55 +0000 | [diff] [blame] | 1033 | InputDemandedBits.trunc(SrcBitWidth); |
| 1034 | RHSKnownZero.trunc(SrcBitWidth); |
| 1035 | RHSKnownOne.trunc(SrcBitWidth); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1036 | if (SimplifyDemandedBits(I->getOperand(0), InputDemandedBits, |
| 1037 | RHSKnownZero, RHSKnownOne, Depth+1)) |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1038 | return true; |
| 1039 | InputDemandedBits.zext(BitWidth); |
| 1040 | RHSKnownZero.zext(BitWidth); |
| 1041 | RHSKnownOne.zext(BitWidth); |
| 1042 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1043 | "Bits known to be one AND zero?"); |
| 1044 | |
| 1045 | // If the sign bit of the input is known set or clear, then we know the |
| 1046 | // top bits of the result. |
| 1047 | |
| 1048 | // If the input sign bit is known zero, or if the NewBits are not demanded |
| 1049 | // convert this into a zero extension. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1050 | if (RHSKnownZero[SrcBitWidth-1] || (NewBits & ~DemandedMask) == NewBits) |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1051 | { |
| 1052 | // Convert to ZExt cast |
| 1053 | CastInst *NewCast = new ZExtInst(I->getOperand(0), VTy, I->getName(), I); |
| 1054 | return UpdateValueUsesWith(I, NewCast); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1055 | } else if (RHSKnownOne[SrcBitWidth-1]) { // Input sign bit known set |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1056 | RHSKnownOne |= NewBits; |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1057 | } |
| 1058 | break; |
| 1059 | } |
| 1060 | case Instruction::Add: { |
| 1061 | // Figure out what the input bits are. If the top bits of the and result |
| 1062 | // are not demanded, then the add doesn't demand them from its input |
| 1063 | // either. |
Reid Spencer | 55702aa | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 1064 | uint32_t NLZ = DemandedMask.countLeadingZeros(); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1065 | |
| 1066 | // If there is a constant on the RHS, there are a variety of xformations |
| 1067 | // we can do. |
| 1068 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 1069 | // If null, this should be simplified elsewhere. Some of the xforms here |
| 1070 | // won't work if the RHS is zero. |
| 1071 | if (RHS->isZero()) |
| 1072 | break; |
| 1073 | |
| 1074 | // If the top bit of the output is demanded, demand everything from the |
| 1075 | // input. Otherwise, we demand all the input bits except NLZ top bits. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1076 | APInt InDemandedBits(APInt::getLowBitsSet(BitWidth, BitWidth - NLZ)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1077 | |
| 1078 | // Find information about known zero/one bits in the input. |
| 1079 | if (SimplifyDemandedBits(I->getOperand(0), InDemandedBits, |
| 1080 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1081 | return true; |
| 1082 | |
| 1083 | // If the RHS of the add has bits set that can't affect the input, reduce |
| 1084 | // the constant. |
| 1085 | if (ShrinkDemandedConstant(I, 1, InDemandedBits)) |
| 1086 | return UpdateValueUsesWith(I, I); |
| 1087 | |
| 1088 | // Avoid excess work. |
| 1089 | if (LHSKnownZero == 0 && LHSKnownOne == 0) |
| 1090 | break; |
| 1091 | |
| 1092 | // Turn it into OR if input bits are zero. |
| 1093 | if ((LHSKnownZero & RHS->getValue()) == RHS->getValue()) { |
| 1094 | Instruction *Or = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1095 | BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1), |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1096 | I->getName()); |
| 1097 | InsertNewInstBefore(Or, *I); |
| 1098 | return UpdateValueUsesWith(I, Or); |
| 1099 | } |
| 1100 | |
| 1101 | // We can say something about the output known-zero and known-one bits, |
| 1102 | // depending on potential carries from the input constant and the |
| 1103 | // unknowns. For example if the LHS is known to have at most the 0x0F0F0 |
| 1104 | // bits set and the RHS constant is 0x01001, then we know we have a known |
| 1105 | // one mask of 0x00001 and a known zero mask of 0xE0F0E. |
| 1106 | |
| 1107 | // To compute this, we first compute the potential carry bits. These are |
| 1108 | // the bits which may be modified. I'm not aware of a better way to do |
| 1109 | // this scan. |
Zhou Sheng | b9cb95f | 2007-03-31 02:38:39 +0000 | [diff] [blame] | 1110 | const APInt& RHSVal = RHS->getValue(); |
| 1111 | APInt CarryBits((~LHSKnownZero + RHSVal) ^ (~LHSKnownZero ^ RHSVal)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1112 | |
| 1113 | // Now that we know which bits have carries, compute the known-1/0 sets. |
| 1114 | |
| 1115 | // Bits are known one if they are known zero in one operand and one in the |
| 1116 | // other, and there is no input carry. |
| 1117 | RHSKnownOne = ((LHSKnownZero & RHSVal) | |
| 1118 | (LHSKnownOne & ~RHSVal)) & ~CarryBits; |
| 1119 | |
| 1120 | // Bits are known zero if they are known zero in both operands and there |
| 1121 | // is no input carry. |
| 1122 | RHSKnownZero = LHSKnownZero & ~RHSVal & ~CarryBits; |
| 1123 | } else { |
| 1124 | // If the high-bits of this ADD are not demanded, then it does not demand |
| 1125 | // the high bits of its LHS or RHS. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1126 | if (DemandedMask[BitWidth-1] == 0) { |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1127 | // Right fill the mask of bits for this ADD to demand the most |
| 1128 | // significant bit and all those below it. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1129 | APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1130 | if (SimplifyDemandedBits(I->getOperand(0), DemandedFromOps, |
| 1131 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1132 | return true; |
| 1133 | if (SimplifyDemandedBits(I->getOperand(1), DemandedFromOps, |
| 1134 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1135 | return true; |
| 1136 | } |
| 1137 | } |
| 1138 | break; |
| 1139 | } |
| 1140 | case Instruction::Sub: |
| 1141 | // If the high-bits of this SUB are not demanded, then it does not demand |
| 1142 | // the high bits of its LHS or RHS. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1143 | if (DemandedMask[BitWidth-1] == 0) { |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1144 | // Right fill the mask of bits for this SUB to demand the most |
| 1145 | // significant bit and all those below it. |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 1146 | uint32_t NLZ = DemandedMask.countLeadingZeros(); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1147 | APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1148 | if (SimplifyDemandedBits(I->getOperand(0), DemandedFromOps, |
| 1149 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1150 | return true; |
| 1151 | if (SimplifyDemandedBits(I->getOperand(1), DemandedFromOps, |
| 1152 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1153 | return true; |
| 1154 | } |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1155 | // Otherwise just hand the sub off to ComputeMaskedBits to fill in |
| 1156 | // the known zeros and ones. |
| 1157 | ComputeMaskedBits(V, DemandedMask, RHSKnownZero, RHSKnownOne, Depth); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1158 | break; |
| 1159 | case Instruction::Shl: |
| 1160 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 1161 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1162 | APInt DemandedMaskIn(DemandedMask.lshr(ShiftAmt)); |
| 1163 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMaskIn, |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1164 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 1165 | return true; |
| 1166 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1167 | "Bits known to be one AND zero?"); |
| 1168 | RHSKnownZero <<= ShiftAmt; |
| 1169 | RHSKnownOne <<= ShiftAmt; |
| 1170 | // low bits known zero. |
Zhou Sheng | adc1495 | 2007-03-14 09:07:33 +0000 | [diff] [blame] | 1171 | if (ShiftAmt) |
Zhou Sheng | e9e03f6 | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 1172 | RHSKnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1173 | } |
| 1174 | break; |
| 1175 | case Instruction::LShr: |
| 1176 | // For a logical shift right |
| 1177 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 1178 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1179 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1180 | // Unsigned shift right. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1181 | APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt)); |
| 1182 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMaskIn, |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1183 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 1184 | return true; |
| 1185 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1186 | "Bits known to be one AND zero?"); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1187 | RHSKnownZero = APIntOps::lshr(RHSKnownZero, ShiftAmt); |
| 1188 | RHSKnownOne = APIntOps::lshr(RHSKnownOne, ShiftAmt); |
Zhou Sheng | adc1495 | 2007-03-14 09:07:33 +0000 | [diff] [blame] | 1189 | if (ShiftAmt) { |
| 1190 | // Compute the new bits that are at the top now. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1191 | APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt)); |
Zhou Sheng | adc1495 | 2007-03-14 09:07:33 +0000 | [diff] [blame] | 1192 | RHSKnownZero |= HighBits; // high bits known zero. |
| 1193 | } |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1194 | } |
| 1195 | break; |
| 1196 | case Instruction::AShr: |
| 1197 | // If this is an arithmetic shift right and only the low-bit is set, we can |
| 1198 | // always convert this into a logical shr, even if the shift amount is |
| 1199 | // variable. The low bit of the shift cannot be an input sign bit unless |
| 1200 | // the shift amount is >= the size of the datatype, which is undefined. |
| 1201 | if (DemandedMask == 1) { |
| 1202 | // Perform the logical shift right. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1203 | Value *NewVal = BinaryOperator::CreateLShr( |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1204 | I->getOperand(0), I->getOperand(1), I->getName()); |
| 1205 | InsertNewInstBefore(cast<Instruction>(NewVal), *I); |
| 1206 | return UpdateValueUsesWith(I, NewVal); |
| 1207 | } |
Chris Lattner | 4241e4d | 2007-07-15 20:54:51 +0000 | [diff] [blame] | 1208 | |
| 1209 | // If the sign bit is the only bit demanded by this ashr, then there is no |
| 1210 | // need to do it, the shift doesn't change the high bit. |
| 1211 | if (DemandedMask.isSignBit()) |
| 1212 | return UpdateValueUsesWith(I, I->getOperand(0)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1213 | |
| 1214 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 1215 | uint32_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1216 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1217 | // Signed shift right. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1218 | APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt)); |
Lauro Ramos Venancio | d0499af | 2007-06-06 17:08:48 +0000 | [diff] [blame] | 1219 | // If any of the "high bits" are demanded, we should set the sign bit as |
| 1220 | // demanded. |
| 1221 | if (DemandedMask.countLeadingZeros() <= ShiftAmt) |
| 1222 | DemandedMaskIn.set(BitWidth-1); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1223 | if (SimplifyDemandedBits(I->getOperand(0), |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1224 | DemandedMaskIn, |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1225 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 1226 | return true; |
| 1227 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1228 | "Bits known to be one AND zero?"); |
| 1229 | // Compute the new bits that are at the top now. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1230 | APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1231 | RHSKnownZero = APIntOps::lshr(RHSKnownZero, ShiftAmt); |
| 1232 | RHSKnownOne = APIntOps::lshr(RHSKnownOne, ShiftAmt); |
| 1233 | |
| 1234 | // Handle the sign bits. |
| 1235 | APInt SignBit(APInt::getSignBit(BitWidth)); |
| 1236 | // Adjust to where it is now in the mask. |
| 1237 | SignBit = APIntOps::lshr(SignBit, ShiftAmt); |
| 1238 | |
| 1239 | // If the input sign bit is known to be zero, or if none of the top bits |
| 1240 | // are demanded, turn this into an unsigned shift right. |
Zhou Sheng | cc41940 | 2008-06-06 08:32:05 +0000 | [diff] [blame] | 1241 | if (BitWidth <= ShiftAmt || RHSKnownZero[BitWidth-ShiftAmt-1] || |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1242 | (HighBits & ~DemandedMask) == HighBits) { |
| 1243 | // Perform the logical shift right. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1244 | Value *NewVal = BinaryOperator::CreateLShr( |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1245 | I->getOperand(0), SA, I->getName()); |
| 1246 | InsertNewInstBefore(cast<Instruction>(NewVal), *I); |
| 1247 | return UpdateValueUsesWith(I, NewVal); |
| 1248 | } else if ((RHSKnownOne & SignBit) != 0) { // New bits are known one. |
| 1249 | RHSKnownOne |= HighBits; |
| 1250 | } |
| 1251 | } |
| 1252 | break; |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 1253 | case Instruction::SRem: |
| 1254 | if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 1255 | APInt RA = Rem->getValue(); |
| 1256 | if (RA.isPowerOf2() || (-RA).isPowerOf2()) { |
Dan Gohman | 23e1df8 | 2008-05-06 00:51:48 +0000 | [diff] [blame] | 1257 | APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) : ~RA; |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 1258 | APInt Mask2 = LowBits | APInt::getSignBit(BitWidth); |
| 1259 | if (SimplifyDemandedBits(I->getOperand(0), Mask2, |
| 1260 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1261 | return true; |
| 1262 | |
| 1263 | if (LHSKnownZero[BitWidth-1] || ((LHSKnownZero & LowBits) == LowBits)) |
| 1264 | LHSKnownZero |= ~LowBits; |
| 1265 | else if (LHSKnownOne[BitWidth-1]) |
| 1266 | LHSKnownOne |= ~LowBits; |
| 1267 | |
| 1268 | KnownZero |= LHSKnownZero & DemandedMask; |
| 1269 | KnownOne |= LHSKnownOne & DemandedMask; |
| 1270 | |
| 1271 | assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); |
| 1272 | } |
| 1273 | } |
| 1274 | break; |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1275 | case Instruction::URem: { |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 1276 | if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 1277 | APInt RA = Rem->getValue(); |
Dan Gohman | 23e1df8 | 2008-05-06 00:51:48 +0000 | [diff] [blame] | 1278 | if (RA.isPowerOf2()) { |
| 1279 | APInt LowBits = (RA - 1); |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 1280 | APInt Mask2 = LowBits & DemandedMask; |
| 1281 | KnownZero |= ~LowBits & DemandedMask; |
| 1282 | if (SimplifyDemandedBits(I->getOperand(0), Mask2, |
| 1283 | KnownZero, KnownOne, Depth+1)) |
| 1284 | return true; |
| 1285 | |
| 1286 | assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1287 | break; |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 1288 | } |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 1289 | } |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1290 | |
| 1291 | APInt KnownZero2(BitWidth, 0), KnownOne2(BitWidth, 0); |
| 1292 | APInt AllOnes = APInt::getAllOnesValue(BitWidth); |
Dan Gohman | e85b758 | 2008-05-01 19:13:24 +0000 | [diff] [blame] | 1293 | if (SimplifyDemandedBits(I->getOperand(0), AllOnes, |
| 1294 | KnownZero2, KnownOne2, Depth+1)) |
| 1295 | return true; |
| 1296 | |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1297 | uint32_t Leaders = KnownZero2.countLeadingOnes(); |
Dan Gohman | e85b758 | 2008-05-01 19:13:24 +0000 | [diff] [blame] | 1298 | if (SimplifyDemandedBits(I->getOperand(1), AllOnes, |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1299 | KnownZero2, KnownOne2, Depth+1)) |
| 1300 | return true; |
| 1301 | |
| 1302 | Leaders = std::max(Leaders, |
| 1303 | KnownZero2.countLeadingOnes()); |
| 1304 | KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & DemandedMask; |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 1305 | break; |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1306 | } |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 1307 | case Instruction::Call: |
| 1308 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { |
| 1309 | switch (II->getIntrinsicID()) { |
| 1310 | default: break; |
| 1311 | case Intrinsic::bswap: { |
| 1312 | // If the only bits demanded come from one byte of the bswap result, |
| 1313 | // just shift the input byte into position to eliminate the bswap. |
| 1314 | unsigned NLZ = DemandedMask.countLeadingZeros(); |
| 1315 | unsigned NTZ = DemandedMask.countTrailingZeros(); |
| 1316 | |
| 1317 | // Round NTZ down to the next byte. If we have 11 trailing zeros, then |
| 1318 | // we need all the bits down to bit 8. Likewise, round NLZ. If we |
| 1319 | // have 14 leading zeros, round to 8. |
| 1320 | NLZ &= ~7; |
| 1321 | NTZ &= ~7; |
| 1322 | // If we need exactly one byte, we can do this transformation. |
| 1323 | if (BitWidth-NLZ-NTZ == 8) { |
| 1324 | unsigned ResultBit = NTZ; |
| 1325 | unsigned InputBit = BitWidth-NTZ-8; |
| 1326 | |
| 1327 | // Replace this with either a left or right shift to get the byte into |
| 1328 | // the right place. |
| 1329 | Instruction *NewVal; |
| 1330 | if (InputBit > ResultBit) |
| 1331 | NewVal = BinaryOperator::CreateLShr(I->getOperand(1), |
| 1332 | ConstantInt::get(I->getType(), InputBit-ResultBit)); |
| 1333 | else |
| 1334 | NewVal = BinaryOperator::CreateShl(I->getOperand(1), |
| 1335 | ConstantInt::get(I->getType(), ResultBit-InputBit)); |
| 1336 | NewVal->takeName(I); |
| 1337 | InsertNewInstBefore(NewVal, *I); |
| 1338 | return UpdateValueUsesWith(I, NewVal); |
| 1339 | } |
| 1340 | |
| 1341 | // TODO: Could compute known zero/one bits based on the input. |
| 1342 | break; |
| 1343 | } |
| 1344 | } |
| 1345 | } |
Chris Lattner | 6c3bfba | 2008-06-18 18:11:55 +0000 | [diff] [blame] | 1346 | ComputeMaskedBits(V, DemandedMask, RHSKnownZero, RHSKnownOne, Depth); |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 1347 | break; |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1348 | } |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1349 | |
| 1350 | // If the client is only demanding bits that we know, return the known |
| 1351 | // constant. |
| 1352 | if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) |
| 1353 | return UpdateValueUsesWith(I, ConstantInt::get(RHSKnownOne)); |
| 1354 | return false; |
| 1355 | } |
| 1356 | |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1357 | |
| 1358 | /// SimplifyDemandedVectorElts - The specified value producecs a vector with |
| 1359 | /// 64 or fewer elements. DemandedElts contains the set of elements that are |
| 1360 | /// actually used by the caller. This method analyzes which elements of the |
| 1361 | /// operand are undef and returns that information in UndefElts. |
| 1362 | /// |
| 1363 | /// If the information about demanded elements can be used to simplify the |
| 1364 | /// operation, the operation is simplified, then the resultant value is |
| 1365 | /// returned. This returns null if no change was made. |
| 1366 | Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, uint64_t DemandedElts, |
| 1367 | uint64_t &UndefElts, |
| 1368 | unsigned Depth) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1369 | unsigned VWidth = cast<VectorType>(V->getType())->getNumElements(); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1370 | assert(VWidth <= 64 && "Vector too wide to analyze!"); |
| 1371 | uint64_t EltMask = ~0ULL >> (64-VWidth); |
| 1372 | assert(DemandedElts != EltMask && (DemandedElts & ~EltMask) == 0 && |
| 1373 | "Invalid DemandedElts!"); |
| 1374 | |
| 1375 | if (isa<UndefValue>(V)) { |
| 1376 | // If the entire vector is undefined, just return this info. |
| 1377 | UndefElts = EltMask; |
| 1378 | return 0; |
| 1379 | } else if (DemandedElts == 0) { // If nothing is demanded, provide undef. |
| 1380 | UndefElts = EltMask; |
| 1381 | return UndefValue::get(V->getType()); |
| 1382 | } |
| 1383 | |
| 1384 | UndefElts = 0; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1385 | if (ConstantVector *CP = dyn_cast<ConstantVector>(V)) { |
| 1386 | const Type *EltTy = cast<VectorType>(V->getType())->getElementType(); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1387 | Constant *Undef = UndefValue::get(EltTy); |
| 1388 | |
| 1389 | std::vector<Constant*> Elts; |
| 1390 | for (unsigned i = 0; i != VWidth; ++i) |
| 1391 | if (!(DemandedElts & (1ULL << i))) { // If not demanded, set to undef. |
| 1392 | Elts.push_back(Undef); |
| 1393 | UndefElts |= (1ULL << i); |
| 1394 | } else if (isa<UndefValue>(CP->getOperand(i))) { // Already undef. |
| 1395 | Elts.push_back(Undef); |
| 1396 | UndefElts |= (1ULL << i); |
| 1397 | } else { // Otherwise, defined. |
| 1398 | Elts.push_back(CP->getOperand(i)); |
| 1399 | } |
| 1400 | |
| 1401 | // If we changed the constant, return it. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1402 | Constant *NewCP = ConstantVector::get(Elts); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1403 | return NewCP != CP ? NewCP : 0; |
| 1404 | } else if (isa<ConstantAggregateZero>(V)) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1405 | // Simplify the CAZ to a ConstantVector where the non-demanded elements are |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1406 | // set to undef. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1407 | const Type *EltTy = cast<VectorType>(V->getType())->getElementType(); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1408 | Constant *Zero = Constant::getNullValue(EltTy); |
| 1409 | Constant *Undef = UndefValue::get(EltTy); |
| 1410 | std::vector<Constant*> Elts; |
| 1411 | for (unsigned i = 0; i != VWidth; ++i) |
| 1412 | Elts.push_back((DemandedElts & (1ULL << i)) ? Zero : Undef); |
| 1413 | UndefElts = DemandedElts ^ EltMask; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1414 | return ConstantVector::get(Elts); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1415 | } |
| 1416 | |
| 1417 | if (!V->hasOneUse()) { // Other users may use these bits. |
| 1418 | if (Depth != 0) { // Not at the root. |
| 1419 | // TODO: Just compute the UndefElts information recursively. |
| 1420 | return false; |
| 1421 | } |
| 1422 | return false; |
| 1423 | } else if (Depth == 10) { // Limit search depth. |
| 1424 | return false; |
| 1425 | } |
| 1426 | |
| 1427 | Instruction *I = dyn_cast<Instruction>(V); |
| 1428 | if (!I) return false; // Only analyze instructions. |
| 1429 | |
| 1430 | bool MadeChange = false; |
| 1431 | uint64_t UndefElts2; |
| 1432 | Value *TmpV; |
| 1433 | switch (I->getOpcode()) { |
| 1434 | default: break; |
| 1435 | |
| 1436 | case Instruction::InsertElement: { |
| 1437 | // If this is a variable index, we don't know which element it overwrites. |
| 1438 | // demand exactly the same input as we produce. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1439 | ConstantInt *Idx = dyn_cast<ConstantInt>(I->getOperand(2)); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1440 | if (Idx == 0) { |
| 1441 | // Note that we can't propagate undef elt info, because we don't know |
| 1442 | // which elt is getting updated. |
| 1443 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, |
| 1444 | UndefElts2, Depth+1); |
| 1445 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } |
| 1446 | break; |
| 1447 | } |
| 1448 | |
| 1449 | // If this is inserting an element that isn't demanded, remove this |
| 1450 | // insertelement. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1451 | unsigned IdxNo = Idx->getZExtValue(); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1452 | if (IdxNo >= VWidth || (DemandedElts & (1ULL << IdxNo)) == 0) |
| 1453 | return AddSoonDeadInstToWorklist(*I, 0); |
| 1454 | |
| 1455 | // Otherwise, the element inserted overwrites whatever was there, so the |
| 1456 | // input demanded set is simpler than the output set. |
| 1457 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), |
| 1458 | DemandedElts & ~(1ULL << IdxNo), |
| 1459 | UndefElts, Depth+1); |
| 1460 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } |
| 1461 | |
| 1462 | // The inserted element is defined. |
| 1463 | UndefElts |= 1ULL << IdxNo; |
| 1464 | break; |
| 1465 | } |
Chris Lattner | 6987833 | 2007-04-14 22:29:23 +0000 | [diff] [blame] | 1466 | case Instruction::BitCast: { |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 1467 | // Vector->vector casts only. |
Chris Lattner | 6987833 | 2007-04-14 22:29:23 +0000 | [diff] [blame] | 1468 | const VectorType *VTy = dyn_cast<VectorType>(I->getOperand(0)->getType()); |
| 1469 | if (!VTy) break; |
| 1470 | unsigned InVWidth = VTy->getNumElements(); |
| 1471 | uint64_t InputDemandedElts = 0; |
| 1472 | unsigned Ratio; |
| 1473 | |
| 1474 | if (VWidth == InVWidth) { |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 1475 | // If we are converting from <4 x i32> -> <4 x f32>, we demand the same |
Chris Lattner | 6987833 | 2007-04-14 22:29:23 +0000 | [diff] [blame] | 1476 | // elements as are demanded of us. |
| 1477 | Ratio = 1; |
| 1478 | InputDemandedElts = DemandedElts; |
| 1479 | } else if (VWidth > InVWidth) { |
| 1480 | // Untested so far. |
| 1481 | break; |
| 1482 | |
| 1483 | // If there are more elements in the result than there are in the source, |
| 1484 | // then an input element is live if any of the corresponding output |
| 1485 | // elements are live. |
| 1486 | Ratio = VWidth/InVWidth; |
| 1487 | for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) { |
| 1488 | if (DemandedElts & (1ULL << OutIdx)) |
| 1489 | InputDemandedElts |= 1ULL << (OutIdx/Ratio); |
| 1490 | } |
| 1491 | } else { |
| 1492 | // Untested so far. |
| 1493 | break; |
| 1494 | |
| 1495 | // If there are more elements in the source than there are in the result, |
| 1496 | // then an input element is live if the corresponding output element is |
| 1497 | // live. |
| 1498 | Ratio = InVWidth/VWidth; |
| 1499 | for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx) |
| 1500 | if (DemandedElts & (1ULL << InIdx/Ratio)) |
| 1501 | InputDemandedElts |= 1ULL << InIdx; |
| 1502 | } |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1503 | |
Chris Lattner | 6987833 | 2007-04-14 22:29:23 +0000 | [diff] [blame] | 1504 | // div/rem demand all inputs, because they don't want divide by zero. |
| 1505 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), InputDemandedElts, |
| 1506 | UndefElts2, Depth+1); |
| 1507 | if (TmpV) { |
| 1508 | I->setOperand(0, TmpV); |
| 1509 | MadeChange = true; |
| 1510 | } |
| 1511 | |
| 1512 | UndefElts = UndefElts2; |
| 1513 | if (VWidth > InVWidth) { |
| 1514 | assert(0 && "Unimp"); |
| 1515 | // If there are more elements in the result than there are in the source, |
| 1516 | // then an output element is undef if the corresponding input element is |
| 1517 | // undef. |
| 1518 | for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) |
| 1519 | if (UndefElts2 & (1ULL << (OutIdx/Ratio))) |
| 1520 | UndefElts |= 1ULL << OutIdx; |
| 1521 | } else if (VWidth < InVWidth) { |
| 1522 | assert(0 && "Unimp"); |
| 1523 | // If there are more elements in the source than there are in the result, |
| 1524 | // then a result element is undef if all of the corresponding input |
| 1525 | // elements are undef. |
| 1526 | UndefElts = ~0ULL >> (64-VWidth); // Start out all undef. |
| 1527 | for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx) |
| 1528 | if ((UndefElts2 & (1ULL << InIdx)) == 0) // Not undef? |
| 1529 | UndefElts &= ~(1ULL << (InIdx/Ratio)); // Clear undef bit. |
| 1530 | } |
| 1531 | break; |
| 1532 | } |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1533 | case Instruction::And: |
| 1534 | case Instruction::Or: |
| 1535 | case Instruction::Xor: |
| 1536 | case Instruction::Add: |
| 1537 | case Instruction::Sub: |
| 1538 | case Instruction::Mul: |
| 1539 | // div/rem demand all inputs, because they don't want divide by zero. |
| 1540 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, |
| 1541 | UndefElts, Depth+1); |
| 1542 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } |
| 1543 | TmpV = SimplifyDemandedVectorElts(I->getOperand(1), DemandedElts, |
| 1544 | UndefElts2, Depth+1); |
| 1545 | if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; } |
| 1546 | |
| 1547 | // Output elements are undefined if both are undefined. Consider things |
| 1548 | // like undef&0. The result is known zero, not undef. |
| 1549 | UndefElts &= UndefElts2; |
| 1550 | break; |
| 1551 | |
| 1552 | case Instruction::Call: { |
| 1553 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(I); |
| 1554 | if (!II) break; |
| 1555 | switch (II->getIntrinsicID()) { |
| 1556 | default: break; |
| 1557 | |
| 1558 | // Binary vector operations that work column-wise. A dest element is a |
| 1559 | // function of the corresponding input elements from the two inputs. |
| 1560 | case Intrinsic::x86_sse_sub_ss: |
| 1561 | case Intrinsic::x86_sse_mul_ss: |
| 1562 | case Intrinsic::x86_sse_min_ss: |
| 1563 | case Intrinsic::x86_sse_max_ss: |
| 1564 | case Intrinsic::x86_sse2_sub_sd: |
| 1565 | case Intrinsic::x86_sse2_mul_sd: |
| 1566 | case Intrinsic::x86_sse2_min_sd: |
| 1567 | case Intrinsic::x86_sse2_max_sd: |
| 1568 | TmpV = SimplifyDemandedVectorElts(II->getOperand(1), DemandedElts, |
| 1569 | UndefElts, Depth+1); |
| 1570 | if (TmpV) { II->setOperand(1, TmpV); MadeChange = true; } |
| 1571 | TmpV = SimplifyDemandedVectorElts(II->getOperand(2), DemandedElts, |
| 1572 | UndefElts2, Depth+1); |
| 1573 | if (TmpV) { II->setOperand(2, TmpV); MadeChange = true; } |
| 1574 | |
| 1575 | // If only the low elt is demanded and this is a scalarizable intrinsic, |
| 1576 | // scalarize it now. |
| 1577 | if (DemandedElts == 1) { |
| 1578 | switch (II->getIntrinsicID()) { |
| 1579 | default: break; |
| 1580 | case Intrinsic::x86_sse_sub_ss: |
| 1581 | case Intrinsic::x86_sse_mul_ss: |
| 1582 | case Intrinsic::x86_sse2_sub_sd: |
| 1583 | case Intrinsic::x86_sse2_mul_sd: |
| 1584 | // TODO: Lower MIN/MAX/ABS/etc |
| 1585 | Value *LHS = II->getOperand(1); |
| 1586 | Value *RHS = II->getOperand(2); |
| 1587 | // Extract the element as scalars. |
| 1588 | LHS = InsertNewInstBefore(new ExtractElementInst(LHS, 0U,"tmp"), *II); |
| 1589 | RHS = InsertNewInstBefore(new ExtractElementInst(RHS, 0U,"tmp"), *II); |
| 1590 | |
| 1591 | switch (II->getIntrinsicID()) { |
| 1592 | default: assert(0 && "Case stmts out of sync!"); |
| 1593 | case Intrinsic::x86_sse_sub_ss: |
| 1594 | case Intrinsic::x86_sse2_sub_sd: |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1595 | TmpV = InsertNewInstBefore(BinaryOperator::CreateSub(LHS, RHS, |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1596 | II->getName()), *II); |
| 1597 | break; |
| 1598 | case Intrinsic::x86_sse_mul_ss: |
| 1599 | case Intrinsic::x86_sse2_mul_sd: |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1600 | TmpV = InsertNewInstBefore(BinaryOperator::CreateMul(LHS, RHS, |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1601 | II->getName()), *II); |
| 1602 | break; |
| 1603 | } |
| 1604 | |
| 1605 | Instruction *New = |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 1606 | InsertElementInst::Create(UndefValue::get(II->getType()), TmpV, 0U, |
| 1607 | II->getName()); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1608 | InsertNewInstBefore(New, *II); |
| 1609 | AddSoonDeadInstToWorklist(*II, 0); |
| 1610 | return New; |
| 1611 | } |
| 1612 | } |
| 1613 | |
| 1614 | // Output elements are undefined if both are undefined. Consider things |
| 1615 | // like undef&0. The result is known zero, not undef. |
| 1616 | UndefElts &= UndefElts2; |
| 1617 | break; |
| 1618 | } |
| 1619 | break; |
| 1620 | } |
| 1621 | } |
| 1622 | return MadeChange ? I : 0; |
| 1623 | } |
| 1624 | |
Dan Gohman | 45b4e48 | 2008-05-19 22:14:15 +0000 | [diff] [blame] | 1625 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1626 | /// AssociativeOpt - Perform an optimization on an associative operator. This |
| 1627 | /// function is designed to check a chain of associative operators for a |
| 1628 | /// potential to apply a certain optimization. Since the optimization may be |
| 1629 | /// applicable if the expression was reassociated, this checks the chain, then |
| 1630 | /// reassociates the expression as necessary to expose the optimization |
| 1631 | /// opportunity. This makes use of a special Functor, which must define |
| 1632 | /// 'shouldApply' and 'apply' methods. |
| 1633 | /// |
| 1634 | template<typename Functor> |
Dan Gohman | 76d402b | 2008-05-20 01:14:05 +0000 | [diff] [blame] | 1635 | static Instruction *AssociativeOpt(BinaryOperator &Root, const Functor &F) { |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1636 | unsigned Opcode = Root.getOpcode(); |
| 1637 | Value *LHS = Root.getOperand(0); |
| 1638 | |
| 1639 | // Quick check, see if the immediate LHS matches... |
| 1640 | if (F.shouldApply(LHS)) |
| 1641 | return F.apply(Root); |
| 1642 | |
| 1643 | // Otherwise, if the LHS is not of the same opcode as the root, return. |
| 1644 | Instruction *LHSI = dyn_cast<Instruction>(LHS); |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 1645 | while (LHSI && LHSI->getOpcode() == Opcode && LHSI->hasOneUse()) { |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1646 | // Should we apply this transform to the RHS? |
| 1647 | bool ShouldApply = F.shouldApply(LHSI->getOperand(1)); |
| 1648 | |
| 1649 | // If not to the RHS, check to see if we should apply to the LHS... |
| 1650 | if (!ShouldApply && F.shouldApply(LHSI->getOperand(0))) { |
| 1651 | cast<BinaryOperator>(LHSI)->swapOperands(); // Make the LHS the RHS |
| 1652 | ShouldApply = true; |
| 1653 | } |
| 1654 | |
| 1655 | // If the functor wants to apply the optimization to the RHS of LHSI, |
| 1656 | // reassociate the expression from ((? op A) op B) to (? op (A op B)) |
| 1657 | if (ShouldApply) { |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1658 | // Now all of the instructions are in the current basic block, go ahead |
| 1659 | // and perform the reassociation. |
| 1660 | Instruction *TmpLHSI = cast<Instruction>(Root.getOperand(0)); |
| 1661 | |
| 1662 | // First move the selected RHS to the LHS of the root... |
| 1663 | Root.setOperand(0, LHSI->getOperand(1)); |
| 1664 | |
| 1665 | // Make what used to be the LHS of the root be the user of the root... |
| 1666 | Value *ExtraOperand = TmpLHSI->getOperand(1); |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1667 | if (&Root == TmpLHSI) { |
Chris Lattner | 15a76c0 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 1668 | Root.replaceAllUsesWith(Constant::getNullValue(TmpLHSI->getType())); |
| 1669 | return 0; |
| 1670 | } |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1671 | Root.replaceAllUsesWith(TmpLHSI); // Users now use TmpLHSI |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1672 | TmpLHSI->setOperand(1, &Root); // TmpLHSI now uses the root |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1673 | BasicBlock::iterator ARI = &Root; ++ARI; |
Dan Gohman | d02d917 | 2008-06-19 17:47:47 +0000 | [diff] [blame] | 1674 | TmpLHSI->moveBefore(ARI); // Move TmpLHSI to after Root |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1675 | ARI = Root; |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1676 | |
| 1677 | // Now propagate the ExtraOperand down the chain of instructions until we |
| 1678 | // get to LHSI. |
| 1679 | while (TmpLHSI != LHSI) { |
| 1680 | Instruction *NextLHSI = cast<Instruction>(TmpLHSI->getOperand(0)); |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1681 | // Move the instruction to immediately before the chain we are |
| 1682 | // constructing to avoid breaking dominance properties. |
Dan Gohman | d02d917 | 2008-06-19 17:47:47 +0000 | [diff] [blame] | 1683 | NextLHSI->moveBefore(ARI); |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1684 | ARI = NextLHSI; |
| 1685 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1686 | Value *NextOp = NextLHSI->getOperand(1); |
| 1687 | NextLHSI->setOperand(1, ExtraOperand); |
| 1688 | TmpLHSI = NextLHSI; |
| 1689 | ExtraOperand = NextOp; |
| 1690 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1691 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1692 | // Now that the instructions are reassociated, have the functor perform |
| 1693 | // the transformation... |
| 1694 | return F.apply(Root); |
| 1695 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1696 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1697 | LHSI = dyn_cast<Instruction>(LHSI->getOperand(0)); |
| 1698 | } |
| 1699 | return 0; |
| 1700 | } |
| 1701 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 1702 | namespace { |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1703 | |
Nick Lewycky | 02d639f | 2008-05-23 04:34:58 +0000 | [diff] [blame] | 1704 | // AddRHS - Implements: X + X --> X << 1 |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1705 | struct AddRHS { |
| 1706 | Value *RHS; |
| 1707 | AddRHS(Value *rhs) : RHS(rhs) {} |
| 1708 | bool shouldApply(Value *LHS) const { return LHS == RHS; } |
| 1709 | Instruction *apply(BinaryOperator &Add) const { |
Nick Lewycky | 02d639f | 2008-05-23 04:34:58 +0000 | [diff] [blame] | 1710 | return BinaryOperator::CreateShl(Add.getOperand(0), |
| 1711 | ConstantInt::get(Add.getType(), 1)); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1712 | } |
| 1713 | }; |
| 1714 | |
| 1715 | // AddMaskingAnd - Implements (A & C1)+(B & C2) --> (A & C1)|(B & C2) |
| 1716 | // iff C1&C2 == 0 |
| 1717 | struct AddMaskingAnd { |
| 1718 | Constant *C2; |
| 1719 | AddMaskingAnd(Constant *c) : C2(c) {} |
| 1720 | bool shouldApply(Value *LHS) const { |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1721 | ConstantInt *C1; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1722 | return match(LHS, m_And(m_Value(), m_ConstantInt(C1))) && |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1723 | ConstantExpr::getAnd(C1, C2)->isNullValue(); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1724 | } |
| 1725 | Instruction *apply(BinaryOperator &Add) const { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1726 | return BinaryOperator::CreateOr(Add.getOperand(0), Add.getOperand(1)); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1727 | } |
| 1728 | }; |
| 1729 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 1730 | } |
| 1731 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1732 | static Value *FoldOperationIntoSelectOperand(Instruction &I, Value *SO, |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1733 | InstCombiner *IC) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1734 | if (CastInst *CI = dyn_cast<CastInst>(&I)) { |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1735 | if (Constant *SOC = dyn_cast<Constant>(SO)) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1736 | return ConstantExpr::getCast(CI->getOpcode(), SOC, I.getType()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1737 | |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1738 | return IC->InsertNewInstBefore(CastInst::Create( |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1739 | CI->getOpcode(), SO, I.getType(), SO->getName() + ".cast"), I); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1740 | } |
| 1741 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1742 | // Figure out if the constant is the left or the right argument. |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1743 | bool ConstIsRHS = isa<Constant>(I.getOperand(1)); |
| 1744 | Constant *ConstOperand = cast<Constant>(I.getOperand(ConstIsRHS)); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1745 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1746 | if (Constant *SOC = dyn_cast<Constant>(SO)) { |
| 1747 | if (ConstIsRHS) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1748 | return ConstantExpr::get(I.getOpcode(), SOC, ConstOperand); |
| 1749 | return ConstantExpr::get(I.getOpcode(), ConstOperand, SOC); |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1750 | } |
| 1751 | |
| 1752 | Value *Op0 = SO, *Op1 = ConstOperand; |
| 1753 | if (!ConstIsRHS) |
| 1754 | std::swap(Op0, Op1); |
| 1755 | Instruction *New; |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1756 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1757 | New = BinaryOperator::Create(BO->getOpcode(), Op0, Op1,SO->getName()+".op"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1758 | else if (CmpInst *CI = dyn_cast<CmpInst>(&I)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1759 | New = CmpInst::Create(CI->getOpcode(), CI->getPredicate(), Op0, Op1, |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1760 | SO->getName()+".cmp"); |
Chris Lattner | 326c0f3 | 2004-04-10 19:15:56 +0000 | [diff] [blame] | 1761 | else { |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1762 | assert(0 && "Unknown binary instruction type!"); |
Chris Lattner | 326c0f3 | 2004-04-10 19:15:56 +0000 | [diff] [blame] | 1763 | abort(); |
| 1764 | } |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1765 | return IC->InsertNewInstBefore(New, I); |
| 1766 | } |
| 1767 | |
| 1768 | // FoldOpIntoSelect - Given an instruction with a select as one operand and a |
| 1769 | // constant as the other operand, try to fold the binary operator into the |
| 1770 | // select arguments. This also works for Cast instructions, which obviously do |
| 1771 | // not have a second operand. |
| 1772 | static Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI, |
| 1773 | InstCombiner *IC) { |
| 1774 | // Don't modify shared select instructions |
| 1775 | if (!SI->hasOneUse()) return 0; |
| 1776 | Value *TV = SI->getOperand(1); |
| 1777 | Value *FV = SI->getOperand(2); |
| 1778 | |
| 1779 | if (isa<Constant>(TV) || isa<Constant>(FV)) { |
Chris Lattner | 956db27 | 2005-04-21 05:43:13 +0000 | [diff] [blame] | 1780 | // Bool selects with constant operands can be folded to logical ops. |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 1781 | if (SI->getType() == Type::Int1Ty) return 0; |
Chris Lattner | 956db27 | 2005-04-21 05:43:13 +0000 | [diff] [blame] | 1782 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1783 | Value *SelectTrueVal = FoldOperationIntoSelectOperand(Op, TV, IC); |
| 1784 | Value *SelectFalseVal = FoldOperationIntoSelectOperand(Op, FV, IC); |
| 1785 | |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 1786 | return SelectInst::Create(SI->getCondition(), SelectTrueVal, |
| 1787 | SelectFalseVal); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1788 | } |
| 1789 | return 0; |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1790 | } |
| 1791 | |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1792 | |
| 1793 | /// FoldOpIntoPhi - Given a binary operator or cast instruction which has a PHI |
| 1794 | /// node as operand #0, see if we can fold the instruction into the PHI (which |
| 1795 | /// is only possible if all operands to the PHI are constants). |
| 1796 | Instruction *InstCombiner::FoldOpIntoPhi(Instruction &I) { |
| 1797 | PHINode *PN = cast<PHINode>(I.getOperand(0)); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 1798 | unsigned NumPHIValues = PN->getNumIncomingValues(); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1799 | if (!PN->hasOneUse() || NumPHIValues == 0) return 0; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1800 | |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1801 | // Check to see if all of the operands of the PHI are constants. If there is |
| 1802 | // one non-constant value, remember the BB it is. If there is more than one |
Chris Lattner | b303668 | 2007-02-24 01:03:45 +0000 | [diff] [blame] | 1803 | // or if *it* is a PHI, bail out. |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1804 | BasicBlock *NonConstBB = 0; |
| 1805 | for (unsigned i = 0; i != NumPHIValues; ++i) |
| 1806 | if (!isa<Constant>(PN->getIncomingValue(i))) { |
| 1807 | if (NonConstBB) return 0; // More than one non-const value. |
Chris Lattner | b303668 | 2007-02-24 01:03:45 +0000 | [diff] [blame] | 1808 | if (isa<PHINode>(PN->getIncomingValue(i))) return 0; // Itself a phi. |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1809 | NonConstBB = PN->getIncomingBlock(i); |
| 1810 | |
| 1811 | // If the incoming non-constant value is in I's block, we have an infinite |
| 1812 | // loop. |
| 1813 | if (NonConstBB == I.getParent()) |
| 1814 | return 0; |
| 1815 | } |
| 1816 | |
| 1817 | // If there is exactly one non-constant value, we can insert a copy of the |
| 1818 | // operation in that block. However, if this is a critical edge, we would be |
| 1819 | // inserting the computation one some other paths (e.g. inside a loop). Only |
| 1820 | // do this if the pred block is unconditionally branching into the phi block. |
| 1821 | if (NonConstBB) { |
| 1822 | BranchInst *BI = dyn_cast<BranchInst>(NonConstBB->getTerminator()); |
| 1823 | if (!BI || !BI->isUnconditional()) return 0; |
| 1824 | } |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1825 | |
| 1826 | // Okay, we can do the transformation: create the new PHI node. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 1827 | PHINode *NewPN = PHINode::Create(I.getType(), ""); |
Chris Lattner | 5551706 | 2005-01-29 00:39:08 +0000 | [diff] [blame] | 1828 | NewPN->reserveOperandSpace(PN->getNumOperands()/2); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1829 | InsertNewInstBefore(NewPN, *PN); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 1830 | NewPN->takeName(PN); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1831 | |
| 1832 | // Next, add all of the operands to the PHI. |
| 1833 | if (I.getNumOperands() == 2) { |
| 1834 | Constant *C = cast<Constant>(I.getOperand(1)); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 1835 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
Chris Lattner | a9ff5eb | 2007-08-05 08:47:58 +0000 | [diff] [blame] | 1836 | Value *InV = 0; |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1837 | if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1838 | if (CmpInst *CI = dyn_cast<CmpInst>(&I)) |
| 1839 | InV = ConstantExpr::getCompare(CI->getPredicate(), InC, C); |
| 1840 | else |
| 1841 | InV = ConstantExpr::get(I.getOpcode(), InC, C); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1842 | } else { |
| 1843 | assert(PN->getIncomingBlock(i) == NonConstBB); |
| 1844 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1845 | InV = BinaryOperator::Create(BO->getOpcode(), |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1846 | PN->getIncomingValue(i), C, "phitmp", |
| 1847 | NonConstBB->getTerminator()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1848 | else if (CmpInst *CI = dyn_cast<CmpInst>(&I)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1849 | InV = CmpInst::Create(CI->getOpcode(), |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1850 | CI->getPredicate(), |
| 1851 | PN->getIncomingValue(i), C, "phitmp", |
| 1852 | NonConstBB->getTerminator()); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1853 | else |
| 1854 | assert(0 && "Unknown binop!"); |
| 1855 | |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 1856 | AddToWorkList(cast<Instruction>(InV)); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1857 | } |
| 1858 | NewPN->addIncoming(InV, PN->getIncomingBlock(i)); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1859 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1860 | } else { |
| 1861 | CastInst *CI = cast<CastInst>(&I); |
| 1862 | const Type *RetTy = CI->getType(); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 1863 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1864 | Value *InV; |
| 1865 | if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1866 | InV = ConstantExpr::getCast(CI->getOpcode(), InC, RetTy); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1867 | } else { |
| 1868 | assert(PN->getIncomingBlock(i) == NonConstBB); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1869 | InV = CastInst::Create(CI->getOpcode(), PN->getIncomingValue(i), |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1870 | I.getType(), "phitmp", |
| 1871 | NonConstBB->getTerminator()); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 1872 | AddToWorkList(cast<Instruction>(InV)); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1873 | } |
| 1874 | NewPN->addIncoming(InV, PN->getIncomingBlock(i)); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1875 | } |
| 1876 | } |
| 1877 | return ReplaceInstUsesWith(I, NewPN); |
| 1878 | } |
| 1879 | |
Chris Lattner | 2454a2e | 2008-01-29 06:52:45 +0000 | [diff] [blame] | 1880 | |
Chris Lattner | 3d28b1b | 2008-05-20 05:46:13 +0000 | [diff] [blame] | 1881 | /// WillNotOverflowSignedAdd - Return true if we can prove that: |
| 1882 | /// (sext (add LHS, RHS)) === (add (sext LHS), (sext RHS)) |
| 1883 | /// This basically requires proving that the add in the original type would not |
| 1884 | /// overflow to change the sign bit or have a carry out. |
| 1885 | bool InstCombiner::WillNotOverflowSignedAdd(Value *LHS, Value *RHS) { |
| 1886 | // There are different heuristics we can use for this. Here are some simple |
| 1887 | // ones. |
| 1888 | |
| 1889 | // Add has the property that adding any two 2's complement numbers can only |
| 1890 | // have one carry bit which can change a sign. As such, if LHS and RHS each |
| 1891 | // have at least two sign bits, we know that the addition of the two values will |
| 1892 | // sign extend fine. |
| 1893 | if (ComputeNumSignBits(LHS) > 1 && ComputeNumSignBits(RHS) > 1) |
| 1894 | return true; |
| 1895 | |
| 1896 | |
| 1897 | // If one of the operands only has one non-zero bit, and if the other operand |
| 1898 | // has a known-zero bit in a more significant place than it (not including the |
| 1899 | // sign bit) the ripple may go up to and fill the zero, but won't change the |
| 1900 | // sign. For example, (X & ~4) + 1. |
| 1901 | |
| 1902 | // TODO: Implement. |
| 1903 | |
| 1904 | return false; |
| 1905 | } |
| 1906 | |
Chris Lattner | 2454a2e | 2008-01-29 06:52:45 +0000 | [diff] [blame] | 1907 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1908 | Instruction *InstCombiner::visitAdd(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 1909 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1910 | Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 1911 | |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 1912 | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 1913 | // X + undef -> undef |
| 1914 | if (isa<UndefValue>(RHS)) |
| 1915 | return ReplaceInstUsesWith(I, RHS); |
| 1916 | |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 1917 | // X + 0 --> X |
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 1918 | if (!I.getType()->isFPOrFPVector()) { // NOTE: -0 + +0 = +0. |
Chris Lattner | 5e678e0 | 2005-10-17 17:56:38 +0000 | [diff] [blame] | 1919 | if (RHSC->isNullValue()) |
| 1920 | return ReplaceInstUsesWith(I, LHS); |
Chris Lattner | 8532cf6 | 2005-10-17 20:18:38 +0000 | [diff] [blame] | 1921 | } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) { |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 1922 | if (CFP->isExactlyValue(ConstantFP::getNegativeZero |
| 1923 | (I.getType())->getValueAPF())) |
Chris Lattner | 8532cf6 | 2005-10-17 20:18:38 +0000 | [diff] [blame] | 1924 | return ReplaceInstUsesWith(I, LHS); |
Chris Lattner | 5e678e0 | 2005-10-17 17:56:38 +0000 | [diff] [blame] | 1925 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1926 | |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 1927 | if (ConstantInt *CI = dyn_cast<ConstantInt>(RHSC)) { |
Chris Lattner | b4a2f05 | 2006-11-09 05:12:27 +0000 | [diff] [blame] | 1928 | // X + (signbit) --> X ^ signbit |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 1929 | const APInt& Val = CI->getValue(); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 1930 | uint32_t BitWidth = Val.getBitWidth(); |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 1931 | if (Val == APInt::getSignBit(BitWidth)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1932 | return BinaryOperator::CreateXor(LHS, RHS); |
Chris Lattner | b4a2f05 | 2006-11-09 05:12:27 +0000 | [diff] [blame] | 1933 | |
| 1934 | // See if SimplifyDemandedBits can simplify this. This handles stuff like |
| 1935 | // (X & 254)+1 -> (X&254)|1 |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 1936 | if (!isa<VectorType>(I.getType())) { |
| 1937 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 1938 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth), |
| 1939 | KnownZero, KnownOne)) |
| 1940 | return &I; |
| 1941 | } |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 1942 | } |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1943 | |
| 1944 | if (isa<PHINode>(LHS)) |
| 1945 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 1946 | return NV; |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1947 | |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 1948 | ConstantInt *XorRHS = 0; |
| 1949 | Value *XorLHS = 0; |
Chris Lattner | c5eff44 | 2007-01-30 22:32:46 +0000 | [diff] [blame] | 1950 | if (isa<ConstantInt>(RHSC) && |
| 1951 | match(LHS, m_Xor(m_Value(XorLHS), m_ConstantInt(XorRHS)))) { |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 1952 | uint32_t TySizeBits = I.getType()->getPrimitiveSizeInBits(); |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 1953 | const APInt& RHSVal = cast<ConstantInt>(RHSC)->getValue(); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1954 | |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 1955 | uint32_t Size = TySizeBits / 2; |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 1956 | APInt C0080Val(APInt(TySizeBits, 1ULL).shl(Size - 1)); |
| 1957 | APInt CFF80Val(-C0080Val); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1958 | do { |
| 1959 | if (TySizeBits > Size) { |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1960 | // If we have ADD(XOR(AND(X, 0xFF), 0x80), 0xF..F80), it's a sext. |
| 1961 | // 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] | 1962 | if ((RHSVal == CFF80Val && XorRHS->getValue() == C0080Val) || |
| 1963 | (RHSVal == C0080Val && XorRHS->getValue() == CFF80Val)) { |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1964 | // This is a sign extend if the top bits are known zero. |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 1965 | if (!MaskedValueIsZero(XorLHS, |
| 1966 | APInt::getHighBitsSet(TySizeBits, TySizeBits - Size))) |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1967 | 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] | 1968 | break; |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1969 | } |
| 1970 | } |
| 1971 | Size >>= 1; |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 1972 | C0080Val = APIntOps::lshr(C0080Val, Size); |
| 1973 | CFF80Val = APIntOps::ashr(CFF80Val, Size); |
| 1974 | } while (Size >= 1); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1975 | |
Reid Spencer | 35c3885 | 2007-03-28 01:36:16 +0000 | [diff] [blame] | 1976 | // FIXME: This shouldn't be necessary. When the backends can handle types |
Chris Lattner | 0c7a9a0 | 2008-05-19 20:25:04 +0000 | [diff] [blame] | 1977 | // with funny bit widths then this switch statement should be removed. It |
| 1978 | // is just here to get the size of the "middle" type back up to something |
| 1979 | // that the back ends can handle. |
Reid Spencer | 35c3885 | 2007-03-28 01:36:16 +0000 | [diff] [blame] | 1980 | const Type *MiddleType = 0; |
| 1981 | switch (Size) { |
| 1982 | default: break; |
| 1983 | case 32: MiddleType = Type::Int32Ty; break; |
| 1984 | case 16: MiddleType = Type::Int16Ty; break; |
| 1985 | case 8: MiddleType = Type::Int8Ty; break; |
| 1986 | } |
| 1987 | if (MiddleType) { |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 1988 | Instruction *NewTrunc = new TruncInst(XorLHS, MiddleType, "sext"); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1989 | InsertNewInstBefore(NewTrunc, I); |
Reid Spencer | 35c3885 | 2007-03-28 01:36:16 +0000 | [diff] [blame] | 1990 | return new SExtInst(NewTrunc, I.getType(), I.getName()); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1991 | } |
| 1992 | } |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 1993 | } |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 1994 | |
Nick Lewycky | 9419ddb | 2008-05-31 17:59:52 +0000 | [diff] [blame] | 1995 | if (I.getType() == Type::Int1Ty) |
| 1996 | return BinaryOperator::CreateXor(LHS, RHS); |
| 1997 | |
Nick Lewycky | 7d26bd8 | 2008-05-23 04:39:38 +0000 | [diff] [blame] | 1998 | // X + X --> X << 1 |
Nick Lewycky | 9419ddb | 2008-05-31 17:59:52 +0000 | [diff] [blame] | 1999 | if (I.getType()->isInteger()) { |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2000 | if (Instruction *Result = AssociativeOpt(I, AddRHS(RHS))) return Result; |
Chris Lattner | 7edc8c2 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 2001 | |
| 2002 | if (Instruction *RHSI = dyn_cast<Instruction>(RHS)) { |
| 2003 | if (RHSI->getOpcode() == Instruction::Sub) |
| 2004 | if (LHS == RHSI->getOperand(1)) // A + (B - A) --> B |
| 2005 | return ReplaceInstUsesWith(I, RHSI->getOperand(0)); |
| 2006 | } |
| 2007 | if (Instruction *LHSI = dyn_cast<Instruction>(LHS)) { |
| 2008 | if (LHSI->getOpcode() == Instruction::Sub) |
| 2009 | if (RHS == LHSI->getOperand(1)) // (B - A) + A --> B |
| 2010 | return ReplaceInstUsesWith(I, LHSI->getOperand(0)); |
| 2011 | } |
Robert Bocchino | 7169828 | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 2012 | } |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 2013 | |
Chris Lattner | 5c4afb9 | 2002-05-08 22:46:53 +0000 | [diff] [blame] | 2014 | // -A + B --> B - A |
Chris Lattner | dd12f96 | 2008-02-17 21:03:36 +0000 | [diff] [blame] | 2015 | // -A + -B --> -(A + B) |
| 2016 | if (Value *LHSV = dyn_castNegVal(LHS)) { |
Chris Lattner | e10c0b9 | 2008-02-18 17:50:16 +0000 | [diff] [blame] | 2017 | if (LHS->getType()->isIntOrIntVector()) { |
| 2018 | if (Value *RHSV = dyn_castNegVal(RHS)) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2019 | Instruction *NewAdd = BinaryOperator::CreateAdd(LHSV, RHSV, "sum"); |
Chris Lattner | e10c0b9 | 2008-02-18 17:50:16 +0000 | [diff] [blame] | 2020 | InsertNewInstBefore(NewAdd, I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2021 | return BinaryOperator::CreateNeg(NewAdd); |
Chris Lattner | e10c0b9 | 2008-02-18 17:50:16 +0000 | [diff] [blame] | 2022 | } |
Chris Lattner | dd12f96 | 2008-02-17 21:03:36 +0000 | [diff] [blame] | 2023 | } |
| 2024 | |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2025 | return BinaryOperator::CreateSub(RHS, LHSV); |
Chris Lattner | dd12f96 | 2008-02-17 21:03:36 +0000 | [diff] [blame] | 2026 | } |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 2027 | |
| 2028 | // A + -B --> A - B |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 2029 | if (!isa<Constant>(RHS)) |
| 2030 | if (Value *V = dyn_castNegVal(RHS)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2031 | return BinaryOperator::CreateSub(LHS, V); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2032 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2033 | |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2034 | ConstantInt *C2; |
| 2035 | if (Value *X = dyn_castFoldableMul(LHS, C2)) { |
| 2036 | if (X == RHS) // X*C + X --> X * (C+1) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2037 | return BinaryOperator::CreateMul(RHS, AddOne(C2)); |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2038 | |
| 2039 | // X*C1 + X*C2 --> X * (C1+C2) |
| 2040 | ConstantInt *C1; |
| 2041 | if (X == dyn_castFoldableMul(RHS, C1)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2042 | return BinaryOperator::CreateMul(X, Add(C1, C2)); |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2043 | } |
| 2044 | |
| 2045 | // X + X*C --> X * (C+1) |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2046 | if (dyn_castFoldableMul(RHS, C2) == LHS) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2047 | return BinaryOperator::CreateMul(LHS, AddOne(C2)); |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2048 | |
Chris Lattner | e617c9e | 2007-01-05 02:17:46 +0000 | [diff] [blame] | 2049 | // X + ~X --> -1 since ~X = -X-1 |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 2050 | if (dyn_castNotVal(LHS) == RHS || dyn_castNotVal(RHS) == LHS) |
| 2051 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | e617c9e | 2007-01-05 02:17:46 +0000 | [diff] [blame] | 2052 | |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2053 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2054 | // (A & C1)+(B & C2) --> (A & C1)|(B & C2) iff C1&C2 == 0 |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2055 | if (match(RHS, m_And(m_Value(), m_ConstantInt(C2)))) |
Chris Lattner | e617c9e | 2007-01-05 02:17:46 +0000 | [diff] [blame] | 2056 | if (Instruction *R = AssociativeOpt(I, AddMaskingAnd(C2))) |
| 2057 | return R; |
Chris Lattner | 5e0d718 | 2008-05-19 20:01:56 +0000 | [diff] [blame] | 2058 | |
| 2059 | // A+B --> A|B iff A and B have no bits set in common. |
| 2060 | if (const IntegerType *IT = dyn_cast<IntegerType>(I.getType())) { |
| 2061 | APInt Mask = APInt::getAllOnesValue(IT->getBitWidth()); |
| 2062 | APInt LHSKnownOne(IT->getBitWidth(), 0); |
| 2063 | APInt LHSKnownZero(IT->getBitWidth(), 0); |
| 2064 | ComputeMaskedBits(LHS, Mask, LHSKnownZero, LHSKnownOne); |
| 2065 | if (LHSKnownZero != 0) { |
| 2066 | APInt RHSKnownOne(IT->getBitWidth(), 0); |
| 2067 | APInt RHSKnownZero(IT->getBitWidth(), 0); |
| 2068 | ComputeMaskedBits(RHS, Mask, RHSKnownZero, RHSKnownOne); |
| 2069 | |
| 2070 | // No bits in common -> bitwise or. |
Chris Lattner | 9d60ba9 | 2008-05-19 20:03:53 +0000 | [diff] [blame] | 2071 | if ((LHSKnownZero|RHSKnownZero).isAllOnesValue()) |
Chris Lattner | 5e0d718 | 2008-05-19 20:01:56 +0000 | [diff] [blame] | 2072 | return BinaryOperator::CreateOr(LHS, RHS); |
Chris Lattner | 5e0d718 | 2008-05-19 20:01:56 +0000 | [diff] [blame] | 2073 | } |
| 2074 | } |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 2075 | |
Nick Lewycky | b6eabff | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 2076 | // W*X + Y*Z --> W * (X+Z) iff W == Y |
Nick Lewycky | 0c2c3f6 | 2008-02-03 08:19:11 +0000 | [diff] [blame] | 2077 | if (I.getType()->isIntOrIntVector()) { |
Nick Lewycky | b6eabff | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 2078 | Value *W, *X, *Y, *Z; |
| 2079 | if (match(LHS, m_Mul(m_Value(W), m_Value(X))) && |
| 2080 | match(RHS, m_Mul(m_Value(Y), m_Value(Z)))) { |
| 2081 | if (W != Y) { |
| 2082 | if (W == Z) { |
Bill Wendling | 587c01d | 2008-02-26 10:53:30 +0000 | [diff] [blame] | 2083 | std::swap(Y, Z); |
Nick Lewycky | b6eabff | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 2084 | } else if (Y == X) { |
Bill Wendling | 587c01d | 2008-02-26 10:53:30 +0000 | [diff] [blame] | 2085 | std::swap(W, X); |
| 2086 | } else if (X == Z) { |
Nick Lewycky | b6eabff | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 2087 | std::swap(Y, Z); |
| 2088 | std::swap(W, X); |
| 2089 | } |
| 2090 | } |
| 2091 | |
| 2092 | if (W == Y) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2093 | Value *NewAdd = InsertNewInstBefore(BinaryOperator::CreateAdd(X, Z, |
Nick Lewycky | b6eabff | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 2094 | LHS->getName()), I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2095 | return BinaryOperator::CreateMul(W, NewAdd); |
Nick Lewycky | b6eabff | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 2096 | } |
| 2097 | } |
| 2098 | } |
| 2099 | |
Chris Lattner | 6b03205 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 2100 | if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) { |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 2101 | Value *X = 0; |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2102 | if (match(LHS, m_Not(m_Value(X)))) // ~X + C --> (C-1) - X |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2103 | return BinaryOperator::CreateSub(SubOne(CRHS), X); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2104 | |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2105 | // (X & FF00) + xx00 -> (X+xx00) & FF00 |
| 2106 | if (LHS->hasOneUse() && match(LHS, m_And(m_Value(X), m_ConstantInt(C2)))) { |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2107 | Constant *Anded = And(CRHS, C2); |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2108 | if (Anded == CRHS) { |
| 2109 | // See if all bits from the first bit set in the Add RHS up are included |
| 2110 | // in the mask. First, get the rightmost bit. |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2111 | const APInt& AddRHSV = CRHS->getValue(); |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2112 | |
| 2113 | // 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] | 2114 | APInt AddRHSHighBits(~((AddRHSV & -AddRHSV)-1)); |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2115 | |
| 2116 | // See if the and mask includes all of these bits. |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2117 | APInt AddRHSHighBitsAnd(AddRHSHighBits & C2->getValue()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2118 | |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2119 | if (AddRHSHighBits == AddRHSHighBitsAnd) { |
| 2120 | // Okay, the xform is safe. Insert the new add pronto. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2121 | Value *NewAdd = InsertNewInstBefore(BinaryOperator::CreateAdd(X, CRHS, |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2122 | LHS->getName()), I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2123 | return BinaryOperator::CreateAnd(NewAdd, C2); |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2124 | } |
| 2125 | } |
| 2126 | } |
| 2127 | |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2128 | // Try to fold constant add into select arguments. |
| 2129 | if (SelectInst *SI = dyn_cast<SelectInst>(LHS)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2130 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2131 | return R; |
Chris Lattner | 6b03205 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 2132 | } |
| 2133 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2134 | // add (cast *A to intptrtype) B -> |
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2135 | // cast (GEP (cast *A to sbyte*) B) --> intptrtype |
Andrew Lenharth | 16d7955 | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 2136 | { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2137 | CastInst *CI = dyn_cast<CastInst>(LHS); |
| 2138 | Value *Other = RHS; |
Andrew Lenharth | 16d7955 | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 2139 | if (!CI) { |
| 2140 | CI = dyn_cast<CastInst>(RHS); |
| 2141 | Other = LHS; |
| 2142 | } |
Andrew Lenharth | 4563326 | 2006-09-20 15:37:57 +0000 | [diff] [blame] | 2143 | if (CI && CI->getType()->isSized() && |
Reid Spencer | abaa8ca | 2007-01-08 16:32:00 +0000 | [diff] [blame] | 2144 | (CI->getType()->getPrimitiveSizeInBits() == |
| 2145 | TD->getIntPtrType()->getPrimitiveSizeInBits()) |
Andrew Lenharth | 4563326 | 2006-09-20 15:37:57 +0000 | [diff] [blame] | 2146 | && isa<PointerType>(CI->getOperand(0)->getType())) { |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 2147 | unsigned AS = |
| 2148 | cast<PointerType>(CI->getOperand(0)->getType())->getAddressSpace(); |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 2149 | Value *I2 = InsertBitCastBefore(CI->getOperand(0), |
| 2150 | PointerType::get(Type::Int8Ty, AS), I); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 2151 | I2 = InsertNewInstBefore(GetElementPtrInst::Create(I2, Other, "ctg2"), I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2152 | return new PtrToIntInst(I2, CI->getType()); |
Andrew Lenharth | 16d7955 | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 2153 | } |
| 2154 | } |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2155 | |
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2156 | // add (select X 0 (sub n A)) A --> select X A n |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2157 | { |
| 2158 | SelectInst *SI = dyn_cast<SelectInst>(LHS); |
| 2159 | Value *Other = RHS; |
| 2160 | if (!SI) { |
| 2161 | SI = dyn_cast<SelectInst>(RHS); |
| 2162 | Other = LHS; |
| 2163 | } |
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2164 | if (SI && SI->hasOneUse()) { |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2165 | Value *TV = SI->getTrueValue(); |
| 2166 | Value *FV = SI->getFalseValue(); |
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2167 | Value *A, *N; |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2168 | |
| 2169 | // Can we fold the add into the argument of the select? |
| 2170 | // We check both true and false select arguments for a matching subtract. |
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2171 | if (match(FV, m_Zero()) && match(TV, m_Sub(m_Value(N), m_Value(A))) && |
| 2172 | A == Other) // Fold the add into the true select value. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 2173 | return SelectInst::Create(SI->getCondition(), N, A); |
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2174 | if (match(TV, m_Zero()) && match(FV, m_Sub(m_Value(N), m_Value(A))) && |
| 2175 | A == Other) // Fold the add into the false select value. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 2176 | return SelectInst::Create(SI->getCondition(), A, N); |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2177 | } |
| 2178 | } |
Chris Lattner | 2454a2e | 2008-01-29 06:52:45 +0000 | [diff] [blame] | 2179 | |
| 2180 | // Check for X+0.0. Simplify it to X if we know X is not -0.0. |
| 2181 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHS)) |
| 2182 | if (CFP->getValueAPF().isPosZero() && CannotBeNegativeZero(LHS)) |
| 2183 | return ReplaceInstUsesWith(I, LHS); |
Andrew Lenharth | 16d7955 | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 2184 | |
Chris Lattner | 3d28b1b | 2008-05-20 05:46:13 +0000 | [diff] [blame] | 2185 | // Check for (add (sext x), y), see if we can merge this into an |
| 2186 | // integer add followed by a sext. |
| 2187 | if (SExtInst *LHSConv = dyn_cast<SExtInst>(LHS)) { |
| 2188 | // (add (sext x), cst) --> (sext (add x, cst')) |
| 2189 | if (ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS)) { |
| 2190 | Constant *CI = |
| 2191 | ConstantExpr::getTrunc(RHSC, LHSConv->getOperand(0)->getType()); |
| 2192 | if (LHSConv->hasOneUse() && |
| 2193 | ConstantExpr::getSExt(CI, I.getType()) == RHSC && |
| 2194 | WillNotOverflowSignedAdd(LHSConv->getOperand(0), CI)) { |
| 2195 | // Insert the new, smaller add. |
| 2196 | Instruction *NewAdd = BinaryOperator::CreateAdd(LHSConv->getOperand(0), |
| 2197 | CI, "addconv"); |
| 2198 | InsertNewInstBefore(NewAdd, I); |
| 2199 | return new SExtInst(NewAdd, I.getType()); |
| 2200 | } |
| 2201 | } |
| 2202 | |
| 2203 | // (add (sext x), (sext y)) --> (sext (add int x, y)) |
| 2204 | if (SExtInst *RHSConv = dyn_cast<SExtInst>(RHS)) { |
| 2205 | // Only do this if x/y have the same type, if at last one of them has a |
| 2206 | // single use (so we don't increase the number of sexts), and if the |
| 2207 | // integer add will not overflow. |
| 2208 | if (LHSConv->getOperand(0)->getType()==RHSConv->getOperand(0)->getType()&& |
| 2209 | (LHSConv->hasOneUse() || RHSConv->hasOneUse()) && |
| 2210 | WillNotOverflowSignedAdd(LHSConv->getOperand(0), |
| 2211 | RHSConv->getOperand(0))) { |
| 2212 | // Insert the new integer add. |
| 2213 | Instruction *NewAdd = BinaryOperator::CreateAdd(LHSConv->getOperand(0), |
| 2214 | RHSConv->getOperand(0), |
| 2215 | "addconv"); |
| 2216 | InsertNewInstBefore(NewAdd, I); |
| 2217 | return new SExtInst(NewAdd, I.getType()); |
| 2218 | } |
| 2219 | } |
| 2220 | } |
| 2221 | |
| 2222 | // Check for (add double (sitofp x), y), see if we can merge this into an |
| 2223 | // integer add followed by a promotion. |
| 2224 | if (SIToFPInst *LHSConv = dyn_cast<SIToFPInst>(LHS)) { |
| 2225 | // (add double (sitofp x), fpcst) --> (sitofp (add int x, intcst)) |
| 2226 | // ... if the constant fits in the integer value. This is useful for things |
| 2227 | // like (double)(x & 1234) + 4.0 -> (double)((X & 1234)+4) which no longer |
| 2228 | // requires a constant pool load, and generally allows the add to be better |
| 2229 | // instcombined. |
| 2230 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHS)) { |
| 2231 | Constant *CI = |
| 2232 | ConstantExpr::getFPToSI(CFP, LHSConv->getOperand(0)->getType()); |
| 2233 | if (LHSConv->hasOneUse() && |
| 2234 | ConstantExpr::getSIToFP(CI, I.getType()) == CFP && |
| 2235 | WillNotOverflowSignedAdd(LHSConv->getOperand(0), CI)) { |
| 2236 | // Insert the new integer add. |
| 2237 | Instruction *NewAdd = BinaryOperator::CreateAdd(LHSConv->getOperand(0), |
| 2238 | CI, "addconv"); |
| 2239 | InsertNewInstBefore(NewAdd, I); |
| 2240 | return new SIToFPInst(NewAdd, I.getType()); |
| 2241 | } |
| 2242 | } |
| 2243 | |
| 2244 | // (add double (sitofp x), (sitofp y)) --> (sitofp (add int x, y)) |
| 2245 | if (SIToFPInst *RHSConv = dyn_cast<SIToFPInst>(RHS)) { |
| 2246 | // Only do this if x/y have the same type, if at last one of them has a |
| 2247 | // single use (so we don't increase the number of int->fp conversions), |
| 2248 | // and if the integer add will not overflow. |
| 2249 | if (LHSConv->getOperand(0)->getType()==RHSConv->getOperand(0)->getType()&& |
| 2250 | (LHSConv->hasOneUse() || RHSConv->hasOneUse()) && |
| 2251 | WillNotOverflowSignedAdd(LHSConv->getOperand(0), |
| 2252 | RHSConv->getOperand(0))) { |
| 2253 | // Insert the new integer add. |
| 2254 | Instruction *NewAdd = BinaryOperator::CreateAdd(LHSConv->getOperand(0), |
| 2255 | RHSConv->getOperand(0), |
| 2256 | "addconv"); |
| 2257 | InsertNewInstBefore(NewAdd, I); |
| 2258 | return new SIToFPInst(NewAdd, I.getType()); |
| 2259 | } |
| 2260 | } |
| 2261 | } |
| 2262 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2263 | return Changed ? &I : 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2264 | } |
| 2265 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2266 | Instruction *InstCombiner::visitSub(BinaryOperator &I) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2267 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2268 | |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2269 | if (Op0 == Op1) // sub X, X -> 0 |
| 2270 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2271 | |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2272 | // If this is a 'B = x-(-A)', change to B = x+A... |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 2273 | if (Value *V = dyn_castNegVal(Op1)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2274 | return BinaryOperator::CreateAdd(Op0, V); |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 2275 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2276 | if (isa<UndefValue>(Op0)) |
| 2277 | return ReplaceInstUsesWith(I, Op0); // undef - X -> undef |
| 2278 | if (isa<UndefValue>(Op1)) |
| 2279 | return ReplaceInstUsesWith(I, Op1); // X - undef -> undef |
| 2280 | |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 2281 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op0)) { |
| 2282 | // Replace (-1 - A) with (~A)... |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2283 | if (C->isAllOnesValue()) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2284 | return BinaryOperator::CreateNot(Op1); |
Chris Lattner | 4037171 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 2285 | |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 2286 | // C - ~X == X + (1+C) |
Reid Spencer | 4b828e6 | 2005-06-18 17:37:34 +0000 | [diff] [blame] | 2287 | Value *X = 0; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2288 | if (match(Op1, m_Not(m_Value(X)))) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2289 | return BinaryOperator::CreateAdd(X, AddOne(C)); |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2290 | |
Chris Lattner | 76b7a06 | 2007-01-15 07:02:54 +0000 | [diff] [blame] | 2291 | // -(X >>u 31) -> (X >>s 31) |
| 2292 | // -(X >>s 31) -> (X >>u 31) |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 2293 | if (C->isZero()) { |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 2294 | if (BinaryOperator *SI = dyn_cast<BinaryOperator>(Op1)) { |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2295 | if (SI->getOpcode() == Instruction::LShr) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2296 | if (ConstantInt *CU = dyn_cast<ConstantInt>(SI->getOperand(1))) { |
Chris Lattner | 9c29067 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 2297 | // 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] | 2298 | if (CU->getLimitedValue(SI->getType()->getPrimitiveSizeInBits()) == |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2299 | SI->getType()->getPrimitiveSizeInBits()-1) { |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2300 | // Ok, the transformation is safe. Insert AShr. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2301 | return BinaryOperator::Create(Instruction::AShr, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2302 | SI->getOperand(0), CU, SI->getName()); |
Chris Lattner | 9c29067 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 2303 | } |
| 2304 | } |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2305 | } |
| 2306 | else if (SI->getOpcode() == Instruction::AShr) { |
| 2307 | if (ConstantInt *CU = dyn_cast<ConstantInt>(SI->getOperand(1))) { |
| 2308 | // 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] | 2309 | if (CU->getLimitedValue(SI->getType()->getPrimitiveSizeInBits()) == |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2310 | SI->getType()->getPrimitiveSizeInBits()-1) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 2311 | // Ok, the transformation is safe. Insert LShr. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2312 | return BinaryOperator::CreateLShr( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2313 | SI->getOperand(0), CU, SI->getName()); |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2314 | } |
| 2315 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 2316 | } |
| 2317 | } |
Chris Lattner | bfe492b | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 2318 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2319 | |
| 2320 | // Try to fold constant sub into select arguments. |
| 2321 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2322 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2323 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2324 | |
| 2325 | if (isa<PHINode>(Op0)) |
| 2326 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2327 | return NV; |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 2328 | } |
| 2329 | |
Nick Lewycky | 9419ddb | 2008-05-31 17:59:52 +0000 | [diff] [blame] | 2330 | if (I.getType() == Type::Int1Ty) |
| 2331 | return BinaryOperator::CreateXor(Op0, Op1); |
| 2332 | |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2333 | if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) { |
| 2334 | if (Op1I->getOpcode() == Instruction::Add && |
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 2335 | !Op0->getType()->isFPOrFPVector()) { |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2336 | if (Op1I->getOperand(0) == Op0) // X-(X+Y) == -Y |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2337 | return BinaryOperator::CreateNeg(Op1I->getOperand(1), I.getName()); |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2338 | else if (Op1I->getOperand(1) == Op0) // X-(Y+X) == -Y |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2339 | return BinaryOperator::CreateNeg(Op1I->getOperand(0), I.getName()); |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2340 | else if (ConstantInt *CI1 = dyn_cast<ConstantInt>(I.getOperand(0))) { |
| 2341 | if (ConstantInt *CI2 = dyn_cast<ConstantInt>(Op1I->getOperand(1))) |
| 2342 | // C1-(X+C2) --> (C1-C2)-X |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2343 | return BinaryOperator::CreateSub(Subtract(CI1, CI2), |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2344 | Op1I->getOperand(0)); |
| 2345 | } |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2346 | } |
| 2347 | |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 2348 | if (Op1I->hasOneUse()) { |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2349 | // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression |
| 2350 | // is not used by anyone else... |
| 2351 | // |
Chris Lattner | 0517e72 | 2004-02-02 20:09:56 +0000 | [diff] [blame] | 2352 | if (Op1I->getOpcode() == Instruction::Sub && |
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 2353 | !Op1I->getType()->isFPOrFPVector()) { |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2354 | // Swap the two operands of the subexpr... |
| 2355 | Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1); |
| 2356 | Op1I->setOperand(0, IIOp1); |
| 2357 | Op1I->setOperand(1, IIOp0); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2358 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2359 | // Create the new top level add instruction... |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2360 | return BinaryOperator::CreateAdd(Op0, Op1); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2361 | } |
| 2362 | |
| 2363 | // Replace (A - (A & B)) with (A & ~B) if this is the only use of (A&B)... |
| 2364 | // |
| 2365 | if (Op1I->getOpcode() == Instruction::And && |
| 2366 | (Op1I->getOperand(0) == Op0 || Op1I->getOperand(1) == Op0)) { |
| 2367 | Value *OtherOp = Op1I->getOperand(Op1I->getOperand(0) == Op0); |
| 2368 | |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 2369 | Value *NewNot = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2370 | InsertNewInstBefore(BinaryOperator::CreateNot(OtherOp, "B.not"), I); |
| 2371 | return BinaryOperator::CreateAnd(Op0, NewNot); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2372 | } |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2373 | |
Reid Spencer | ac5209e | 2006-10-16 23:08:08 +0000 | [diff] [blame] | 2374 | // 0 - (X sdiv C) -> (X sdiv -C) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2375 | if (Op1I->getOpcode() == Instruction::SDiv) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2376 | if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0)) |
Zhou Sheng | 843f0767 | 2007-04-19 05:39:12 +0000 | [diff] [blame] | 2377 | if (CSI->isZero()) |
Chris Lattner | 91ccc15 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 2378 | if (Constant *DivRHS = dyn_cast<Constant>(Op1I->getOperand(1))) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2379 | return BinaryOperator::CreateSDiv(Op1I->getOperand(0), |
Chris Lattner | 91ccc15 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 2380 | ConstantExpr::getNeg(DivRHS)); |
| 2381 | |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2382 | // X - X*C --> X * (1-C) |
Reid Spencer | 4b828e6 | 2005-06-18 17:37:34 +0000 | [diff] [blame] | 2383 | ConstantInt *C2 = 0; |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2384 | if (dyn_castFoldableMul(Op1I, C2) == Op0) { |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2385 | Constant *CP1 = Subtract(ConstantInt::get(I.getType(), 1), C2); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2386 | return BinaryOperator::CreateMul(Op0, CP1); |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2387 | } |
Dan Gohman | 5d066ff | 2007-09-17 17:31:57 +0000 | [diff] [blame] | 2388 | |
| 2389 | // X - ((X / Y) * Y) --> X % Y |
| 2390 | if (Op1I->getOpcode() == Instruction::Mul) |
| 2391 | if (Instruction *I = dyn_cast<Instruction>(Op1I->getOperand(0))) |
| 2392 | if (Op0 == I->getOperand(0) && |
| 2393 | Op1I->getOperand(1) == I->getOperand(1)) { |
| 2394 | if (I->getOpcode() == Instruction::SDiv) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2395 | return BinaryOperator::CreateSRem(Op0, Op1I->getOperand(1)); |
Dan Gohman | 5d066ff | 2007-09-17 17:31:57 +0000 | [diff] [blame] | 2396 | if (I->getOpcode() == Instruction::UDiv) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2397 | return BinaryOperator::CreateURem(Op0, Op1I->getOperand(1)); |
Dan Gohman | 5d066ff | 2007-09-17 17:31:57 +0000 | [diff] [blame] | 2398 | } |
Chris Lattner | 4037171 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 2399 | } |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2400 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2401 | |
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 2402 | if (!Op0->getType()->isFPOrFPVector()) |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 2403 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) { |
Chris Lattner | 7edc8c2 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 2404 | if (Op0I->getOpcode() == Instruction::Add) { |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 2405 | if (Op0I->getOperand(0) == Op1) // (Y+X)-Y == X |
| 2406 | return ReplaceInstUsesWith(I, Op0I->getOperand(1)); |
| 2407 | else if (Op0I->getOperand(1) == Op1) // (X+Y)-Y == X |
| 2408 | return ReplaceInstUsesWith(I, Op0I->getOperand(0)); |
Chris Lattner | 7edc8c2 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 2409 | } else if (Op0I->getOpcode() == Instruction::Sub) { |
| 2410 | if (Op0I->getOperand(0) == Op1) // (X-Y)-X == -Y |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2411 | return BinaryOperator::CreateNeg(Op0I->getOperand(1), I.getName()); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 2412 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 2413 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2414 | |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2415 | ConstantInt *C1; |
| 2416 | if (Value *X = dyn_castFoldableMul(Op0, C1)) { |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2417 | if (X == Op1) // X*C - X --> X * (C-1) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2418 | return BinaryOperator::CreateMul(Op1, SubOne(C1)); |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2419 | |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2420 | ConstantInt *C2; // X*C1 - X*C2 -> X * (C1-C2) |
| 2421 | if (X == dyn_castFoldableMul(Op1, C2)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2422 | return BinaryOperator::CreateMul(X, Subtract(C1, C2)); |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2423 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2424 | return 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2425 | } |
| 2426 | |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2427 | /// isSignBitCheck - Given an exploded icmp instruction, return true if the |
| 2428 | /// comparison only checks the sign bit. If it only checks the sign bit, set |
| 2429 | /// TrueIfSigned if the result of the comparison is true when the input value is |
| 2430 | /// signed. |
| 2431 | static bool isSignBitCheck(ICmpInst::Predicate pred, ConstantInt *RHS, |
| 2432 | bool &TrueIfSigned) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2433 | switch (pred) { |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2434 | case ICmpInst::ICMP_SLT: // True if LHS s< 0 |
| 2435 | TrueIfSigned = true; |
| 2436 | return RHS->isZero(); |
Chris Lattner | cb7122b | 2007-07-16 04:15:34 +0000 | [diff] [blame] | 2437 | case ICmpInst::ICMP_SLE: // True if LHS s<= RHS and RHS == -1 |
| 2438 | TrueIfSigned = true; |
| 2439 | return RHS->isAllOnesValue(); |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2440 | case ICmpInst::ICMP_SGT: // True if LHS s> -1 |
| 2441 | TrueIfSigned = false; |
| 2442 | return RHS->isAllOnesValue(); |
Chris Lattner | cb7122b | 2007-07-16 04:15:34 +0000 | [diff] [blame] | 2443 | case ICmpInst::ICMP_UGT: |
| 2444 | // True if LHS u> RHS and RHS == high-bit-mask - 1 |
| 2445 | TrueIfSigned = true; |
| 2446 | return RHS->getValue() == |
| 2447 | APInt::getSignedMaxValue(RHS->getType()->getPrimitiveSizeInBits()); |
| 2448 | case ICmpInst::ICMP_UGE: |
| 2449 | // True if LHS u>= RHS and RHS == high-bit-mask (2^7, 2^15, 2^31, etc) |
| 2450 | TrueIfSigned = true; |
Chris Lattner | 833f25d | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 2451 | return RHS->getValue().isSignBit(); |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2452 | default: |
| 2453 | return false; |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2454 | } |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2455 | } |
| 2456 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2457 | Instruction *InstCombiner::visitMul(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 2458 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2459 | Value *Op0 = I.getOperand(0); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2460 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2461 | if (isa<UndefValue>(I.getOperand(1))) // undef * X -> 0 |
| 2462 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2463 | |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2464 | // Simplify mul instructions with a constant RHS... |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2465 | if (Constant *Op1 = dyn_cast<Constant>(I.getOperand(1))) { |
| 2466 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 2467 | |
| 2468 | // ((X << C1)*C2) == (X * (C2 << C1)) |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2469 | if (BinaryOperator *SI = dyn_cast<BinaryOperator>(Op0)) |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 2470 | if (SI->getOpcode() == Instruction::Shl) |
| 2471 | if (Constant *ShOp = dyn_cast<Constant>(SI->getOperand(1))) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2472 | return BinaryOperator::CreateMul(SI->getOperand(0), |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2473 | ConstantExpr::getShl(CI, ShOp)); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2474 | |
Zhou Sheng | 843f0767 | 2007-04-19 05:39:12 +0000 | [diff] [blame] | 2475 | if (CI->isZero()) |
Chris Lattner | 515c97c | 2003-09-11 22:24:54 +0000 | [diff] [blame] | 2476 | return ReplaceInstUsesWith(I, Op1); // X * 0 == 0 |
| 2477 | if (CI->equalsInt(1)) // X * 1 == X |
| 2478 | return ReplaceInstUsesWith(I, Op0); |
| 2479 | if (CI->isAllOnesValue()) // X * -1 == 0 - X |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2480 | return BinaryOperator::CreateNeg(Op0, I.getName()); |
Chris Lattner | 6c1ce21 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 2481 | |
Zhou Sheng | 97b52c2 | 2007-03-29 01:57:21 +0000 | [diff] [blame] | 2482 | const APInt& Val = cast<ConstantInt>(CI)->getValue(); |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2483 | if (Val.isPowerOf2()) { // Replace X*(2^C) with X << C |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2484 | return BinaryOperator::CreateShl(Op0, |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2485 | ConstantInt::get(Op0->getType(), Val.logBase2())); |
Chris Lattner | bcd7db5 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 2486 | } |
Robert Bocchino | 7169828 | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 2487 | } else if (ConstantFP *Op1F = dyn_cast<ConstantFP>(Op1)) { |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2488 | if (Op1F->isNullValue()) |
| 2489 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 6c1ce21 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 2490 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2491 | // "In IEEE floating point, x*1 is not equivalent to x for nans. However, |
| 2492 | // ANSI says we can drop signals, so we can do this anyway." (from GCC) |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 2493 | // We need a better interface for long double here. |
| 2494 | if (Op1->getType() == Type::FloatTy || Op1->getType() == Type::DoubleTy) |
| 2495 | if (Op1F->isExactlyValue(1.0)) |
| 2496 | return ReplaceInstUsesWith(I, Op0); // Eliminate 'mul double %X, 1.0' |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2497 | } |
Chris Lattner | ab51f3f | 2006-03-04 06:04:02 +0000 | [diff] [blame] | 2498 | |
| 2499 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) |
| 2500 | if (Op0I->getOpcode() == Instruction::Add && Op0I->hasOneUse() && |
Chris Lattner | 47c9909 | 2008-05-18 04:11:26 +0000 | [diff] [blame] | 2501 | isa<ConstantInt>(Op0I->getOperand(1)) && isa<ConstantInt>(Op1)) { |
Chris Lattner | ab51f3f | 2006-03-04 06:04:02 +0000 | [diff] [blame] | 2502 | // Canonicalize (X+C1)*C2 -> X*C2+C1*C2. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2503 | Instruction *Add = BinaryOperator::CreateMul(Op0I->getOperand(0), |
Chris Lattner | ab51f3f | 2006-03-04 06:04:02 +0000 | [diff] [blame] | 2504 | Op1, "tmp"); |
| 2505 | InsertNewInstBefore(Add, I); |
| 2506 | Value *C1C2 = ConstantExpr::getMul(Op1, |
| 2507 | cast<Constant>(Op0I->getOperand(1))); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2508 | return BinaryOperator::CreateAdd(Add, C1C2); |
Chris Lattner | ab51f3f | 2006-03-04 06:04:02 +0000 | [diff] [blame] | 2509 | |
| 2510 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2511 | |
| 2512 | // Try to fold constant mul into select arguments. |
| 2513 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2514 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2515 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2516 | |
| 2517 | if (isa<PHINode>(Op0)) |
| 2518 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2519 | return NV; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2520 | } |
| 2521 | |
Chris Lattner | a4f445b | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 2522 | if (Value *Op0v = dyn_castNegVal(Op0)) // -X * -Y = X*Y |
| 2523 | if (Value *Op1v = dyn_castNegVal(I.getOperand(1))) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2524 | return BinaryOperator::CreateMul(Op0v, Op1v); |
Chris Lattner | a4f445b | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 2525 | |
Nick Lewycky | 9419ddb | 2008-05-31 17:59:52 +0000 | [diff] [blame] | 2526 | if (I.getType() == Type::Int1Ty) |
| 2527 | return BinaryOperator::CreateAnd(Op0, I.getOperand(1)); |
| 2528 | |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2529 | // If one of the operands of the multiply is a cast from a boolean value, then |
| 2530 | // we know the bool is either zero or one, so this is a 'masking' multiply. |
| 2531 | // See if we can simplify things based on how the boolean was originally |
| 2532 | // formed. |
| 2533 | CastInst *BoolCast = 0; |
Nick Lewycky | 9419ddb | 2008-05-31 17:59:52 +0000 | [diff] [blame] | 2534 | if (ZExtInst *CI = dyn_cast<ZExtInst>(Op0)) |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 2535 | if (CI->getOperand(0)->getType() == Type::Int1Ty) |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2536 | BoolCast = CI; |
| 2537 | if (!BoolCast) |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 2538 | if (ZExtInst *CI = dyn_cast<ZExtInst>(I.getOperand(1))) |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 2539 | if (CI->getOperand(0)->getType() == Type::Int1Ty) |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2540 | BoolCast = CI; |
| 2541 | if (BoolCast) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2542 | if (ICmpInst *SCI = dyn_cast<ICmpInst>(BoolCast->getOperand(0))) { |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2543 | Value *SCIOp0 = SCI->getOperand(0), *SCIOp1 = SCI->getOperand(1); |
| 2544 | const Type *SCOpTy = SCIOp0->getType(); |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2545 | bool TIS = false; |
| 2546 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2547 | // If the icmp is true iff the sign bit of X is set, then convert this |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2548 | // multiply into a shift/and combination. |
| 2549 | if (isa<ConstantInt>(SCIOp1) && |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2550 | isSignBitCheck(SCI->getPredicate(), cast<ConstantInt>(SCIOp1), TIS) && |
| 2551 | TIS) { |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2552 | // Shift the X value right to turn it into "all signbits". |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2553 | Constant *Amt = ConstantInt::get(SCIOp0->getType(), |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 2554 | SCOpTy->getPrimitiveSizeInBits()-1); |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2555 | Value *V = |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2556 | InsertNewInstBefore( |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2557 | BinaryOperator::Create(Instruction::AShr, SCIOp0, Amt, |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2558 | BoolCast->getOperand(0)->getName()+ |
| 2559 | ".mask"), I); |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2560 | |
| 2561 | // If the multiply type is not the same as the source type, sign extend |
| 2562 | // or truncate to the multiply type. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 2563 | if (I.getType() != V->getType()) { |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 2564 | uint32_t SrcBits = V->getType()->getPrimitiveSizeInBits(); |
| 2565 | uint32_t DstBits = I.getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 2566 | Instruction::CastOps opcode = |
| 2567 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2568 | (SrcBits < DstBits ? Instruction::SExt : Instruction::Trunc)); |
| 2569 | V = InsertCastBefore(opcode, V, I.getType(), I); |
| 2570 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2571 | |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2572 | Value *OtherOp = Op0 == BoolCast ? I.getOperand(1) : Op0; |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2573 | return BinaryOperator::CreateAnd(V, OtherOp); |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2574 | } |
| 2575 | } |
| 2576 | } |
| 2577 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2578 | return Changed ? &I : 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2579 | } |
| 2580 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2581 | /// This function implements the transforms on div instructions that work |
| 2582 | /// regardless of the kind of div instruction it is (udiv, sdiv, or fdiv). It is |
| 2583 | /// used by the visitors to those instructions. |
| 2584 | /// @brief Transforms common to all three div instructions |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2585 | Instruction *InstCombiner::commonDivTransforms(BinaryOperator &I) { |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2586 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2587 | |
Chris Lattner | 50b2ca4 | 2008-02-19 06:12:18 +0000 | [diff] [blame] | 2588 | // undef / X -> 0 for integer. |
| 2589 | // undef / X -> undef for FP (the undef could be a snan). |
| 2590 | if (isa<UndefValue>(Op0)) { |
| 2591 | if (Op0->getType()->isFPOrFPVector()) |
| 2592 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2593 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | 50b2ca4 | 2008-02-19 06:12:18 +0000 | [diff] [blame] | 2594 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2595 | |
| 2596 | // X / undef -> undef |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2597 | if (isa<UndefValue>(Op1)) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2598 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2599 | |
Chris Lattner | 25feae5 | 2008-01-28 00:58:18 +0000 | [diff] [blame] | 2600 | // Handle cases involving: [su]div X, (select Cond, Y, Z) |
| 2601 | // This does not apply for fdiv. |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2602 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { |
Chris Lattner | 25feae5 | 2008-01-28 00:58:18 +0000 | [diff] [blame] | 2603 | // [su]div X, (Cond ? 0 : Y) -> div X, Y. If the div and the select are in |
| 2604 | // the same basic block, then we replace the select with Y, and the |
| 2605 | // condition of the select with false (if the cond value is in the same BB). |
| 2606 | // If the select has uses other than the div, this allows them to be |
| 2607 | // simplified also. Note that div X, Y is just as good as div X, 0 (undef) |
| 2608 | if (ConstantInt *ST = dyn_cast<ConstantInt>(SI->getOperand(1))) |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2609 | if (ST->isNullValue()) { |
| 2610 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); |
| 2611 | if (CondI && CondI->getParent() == I.getParent()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2612 | UpdateValueUsesWith(CondI, ConstantInt::getFalse()); |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2613 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
| 2614 | I.setOperand(1, SI->getOperand(2)); |
| 2615 | else |
| 2616 | UpdateValueUsesWith(SI, SI->getOperand(2)); |
| 2617 | return &I; |
| 2618 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2619 | |
Chris Lattner | 25feae5 | 2008-01-28 00:58:18 +0000 | [diff] [blame] | 2620 | // Likewise for: [su]div X, (Cond ? Y : 0) -> div X, Y |
| 2621 | if (ConstantInt *ST = dyn_cast<ConstantInt>(SI->getOperand(2))) |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2622 | if (ST->isNullValue()) { |
| 2623 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); |
| 2624 | if (CondI && CondI->getParent() == I.getParent()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2625 | UpdateValueUsesWith(CondI, ConstantInt::getTrue()); |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2626 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
| 2627 | I.setOperand(1, SI->getOperand(1)); |
| 2628 | else |
| 2629 | UpdateValueUsesWith(SI, SI->getOperand(1)); |
| 2630 | return &I; |
| 2631 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2632 | } |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2633 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2634 | return 0; |
| 2635 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2636 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2637 | /// This function implements the transforms common to both integer division |
| 2638 | /// instructions (udiv and sdiv). It is called by the visitors to those integer |
| 2639 | /// division instructions. |
| 2640 | /// @brief Common integer divide transforms |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2641 | Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2642 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2643 | |
Chris Lattner | b2ae9e3 | 2008-05-16 02:59:42 +0000 | [diff] [blame] | 2644 | // (sdiv X, X) --> 1 (udiv X, X) --> 1 |
Nick Lewycky | 39ac3b5 | 2008-05-23 03:26:47 +0000 | [diff] [blame] | 2645 | if (Op0 == Op1) { |
| 2646 | if (const VectorType *Ty = dyn_cast<VectorType>(I.getType())) { |
| 2647 | ConstantInt *CI = ConstantInt::get(Ty->getElementType(), 1); |
| 2648 | std::vector<Constant*> Elts(Ty->getNumElements(), CI); |
| 2649 | return ReplaceInstUsesWith(I, ConstantVector::get(Elts)); |
| 2650 | } |
| 2651 | |
| 2652 | ConstantInt *CI = ConstantInt::get(I.getType(), 1); |
| 2653 | return ReplaceInstUsesWith(I, CI); |
| 2654 | } |
Chris Lattner | b2ae9e3 | 2008-05-16 02:59:42 +0000 | [diff] [blame] | 2655 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2656 | if (Instruction *Common = commonDivTransforms(I)) |
| 2657 | return Common; |
| 2658 | |
| 2659 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
| 2660 | // div X, 1 == X |
| 2661 | if (RHS->equalsInt(1)) |
| 2662 | return ReplaceInstUsesWith(I, Op0); |
| 2663 | |
| 2664 | // (X / C1) / C2 -> X / (C1*C2) |
| 2665 | if (Instruction *LHS = dyn_cast<Instruction>(Op0)) |
| 2666 | if (Instruction::BinaryOps(LHS->getOpcode()) == I.getOpcode()) |
| 2667 | if (ConstantInt *LHSRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) { |
Nick Lewycky | e0cfecf | 2008-02-18 22:48:05 +0000 | [diff] [blame] | 2668 | if (MultiplyOverflows(RHS, LHSRHS, I.getOpcode()==Instruction::SDiv)) |
| 2669 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2670 | else |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2671 | return BinaryOperator::Create(I.getOpcode(), LHS->getOperand(0), |
Nick Lewycky | e0cfecf | 2008-02-18 22:48:05 +0000 | [diff] [blame] | 2672 | Multiply(RHS, LHSRHS)); |
Chris Lattner | bf70b83 | 2005-04-08 04:03:26 +0000 | [diff] [blame] | 2673 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2674 | |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2675 | if (!RHS->isZero()) { // avoid X udiv 0 |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2676 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 2677 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 2678 | return R; |
| 2679 | if (isa<PHINode>(Op0)) |
| 2680 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2681 | return NV; |
| 2682 | } |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2683 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2684 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2685 | // 0 / X == 0, we don't need to preserve faults! |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2686 | if (ConstantInt *LHS = dyn_cast<ConstantInt>(Op0)) |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2687 | if (LHS->equalsInt(0)) |
| 2688 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2689 | |
Nick Lewycky | 9419ddb | 2008-05-31 17:59:52 +0000 | [diff] [blame] | 2690 | // It can't be division by zero, hence it must be division by one. |
| 2691 | if (I.getType() == Type::Int1Ty) |
| 2692 | return ReplaceInstUsesWith(I, Op0); |
| 2693 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2694 | return 0; |
| 2695 | } |
| 2696 | |
| 2697 | Instruction *InstCombiner::visitUDiv(BinaryOperator &I) { |
| 2698 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2699 | |
| 2700 | // Handle the integer div common cases |
| 2701 | if (Instruction *Common = commonIDivTransforms(I)) |
| 2702 | return Common; |
| 2703 | |
| 2704 | // X udiv C^2 -> X >> C |
| 2705 | // Check to see if this is an unsigned division with an exact power of 2, |
| 2706 | // if so, convert to a right shift. |
| 2707 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op1)) { |
Reid Spencer | 6eb0d99 | 2007-03-26 23:58:26 +0000 | [diff] [blame] | 2708 | if (C->getValue().isPowerOf2()) // 0 not included in isPowerOf2 |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2709 | return BinaryOperator::CreateLShr(Op0, |
Zhou Sheng | 0fc5095 | 2007-03-25 05:01:29 +0000 | [diff] [blame] | 2710 | ConstantInt::get(Op0->getType(), C->getValue().logBase2())); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2711 | } |
| 2712 | |
| 2713 | // X udiv (C1 << N), where C1 is "1<<C2" --> X >> (N+C2) |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2714 | if (BinaryOperator *RHSI = dyn_cast<BinaryOperator>(I.getOperand(1))) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2715 | if (RHSI->getOpcode() == Instruction::Shl && |
| 2716 | isa<ConstantInt>(RHSI->getOperand(0))) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2717 | const APInt& C1 = cast<ConstantInt>(RHSI->getOperand(0))->getValue(); |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2718 | if (C1.isPowerOf2()) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2719 | Value *N = RHSI->getOperand(1); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2720 | const Type *NTy = N->getType(); |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 2721 | if (uint32_t C2 = C1.logBase2()) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2722 | Constant *C2V = ConstantInt::get(NTy, C2); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2723 | N = InsertNewInstBefore(BinaryOperator::CreateAdd(N, C2V, "tmp"), I); |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2724 | } |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2725 | return BinaryOperator::CreateLShr(Op0, N); |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2726 | } |
| 2727 | } |
Chris Lattner | c812e5d | 2005-11-05 07:40:31 +0000 | [diff] [blame] | 2728 | } |
| 2729 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2730 | // udiv X, (Select Cond, C1, C2) --> Select Cond, (shr X, C1), (shr X, C2) |
| 2731 | // where C1&C2 are powers of two. |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2732 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2733 | if (ConstantInt *STO = dyn_cast<ConstantInt>(SI->getOperand(1))) |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2734 | if (ConstantInt *SFO = dyn_cast<ConstantInt>(SI->getOperand(2))) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2735 | const APInt &TVA = STO->getValue(), &FVA = SFO->getValue(); |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2736 | if (TVA.isPowerOf2() && FVA.isPowerOf2()) { |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2737 | // Compute the shift amounts |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2738 | uint32_t TSA = TVA.logBase2(), FSA = FVA.logBase2(); |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2739 | // Construct the "on true" case of the select |
| 2740 | Constant *TC = ConstantInt::get(Op0->getType(), TSA); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2741 | Instruction *TSI = BinaryOperator::CreateLShr( |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2742 | Op0, TC, SI->getName()+".t"); |
| 2743 | TSI = InsertNewInstBefore(TSI, I); |
| 2744 | |
| 2745 | // Construct the "on false" case of the select |
| 2746 | Constant *FC = ConstantInt::get(Op0->getType(), FSA); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2747 | Instruction *FSI = BinaryOperator::CreateLShr( |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2748 | Op0, FC, SI->getName()+".f"); |
| 2749 | FSI = InsertNewInstBefore(FSI, I); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2750 | |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2751 | // construct the select instruction and return it. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 2752 | return SelectInst::Create(SI->getOperand(0), TSI, FSI, SI->getName()); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2753 | } |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2754 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2755 | return 0; |
| 2756 | } |
| 2757 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2758 | Instruction *InstCombiner::visitSDiv(BinaryOperator &I) { |
| 2759 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2760 | |
| 2761 | // Handle the integer div common cases |
| 2762 | if (Instruction *Common = commonIDivTransforms(I)) |
| 2763 | return Common; |
| 2764 | |
| 2765 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
| 2766 | // sdiv X, -1 == -X |
| 2767 | if (RHS->isAllOnesValue()) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2768 | return BinaryOperator::CreateNeg(Op0); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2769 | |
| 2770 | // -X/C -> X/-C |
| 2771 | if (Value *LHSNeg = dyn_castNegVal(Op0)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2772 | return BinaryOperator::CreateSDiv(LHSNeg, ConstantExpr::getNeg(RHS)); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2773 | } |
| 2774 | |
| 2775 | // If the sign bits of both operands are zero (i.e. we can prove they are |
| 2776 | // unsigned inputs), turn this into a udiv. |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 2777 | if (I.getType()->isInteger()) { |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2778 | APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits())); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2779 | if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) { |
Dan Gohman | cff5509 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 2780 | // X sdiv Y -> X udiv Y, iff X and Y don't have sign bit set |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2781 | return BinaryOperator::CreateUDiv(Op0, Op1, I.getName()); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2782 | } |
| 2783 | } |
| 2784 | |
| 2785 | return 0; |
| 2786 | } |
| 2787 | |
| 2788 | Instruction *InstCombiner::visitFDiv(BinaryOperator &I) { |
| 2789 | return commonDivTransforms(I); |
| 2790 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2791 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2792 | /// This function implements the transforms on rem instructions that work |
| 2793 | /// regardless of the kind of rem instruction it is (urem, srem, or frem). It |
| 2794 | /// is used by the visitors to those instructions. |
| 2795 | /// @brief Transforms common to all three rem instructions |
| 2796 | Instruction *InstCombiner::commonRemTransforms(BinaryOperator &I) { |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2797 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2798 | |
Chris Lattner | 50b2ca4 | 2008-02-19 06:12:18 +0000 | [diff] [blame] | 2799 | // 0 % X == 0 for integer, we don't need to preserve faults! |
Chris Lattner | 19ccd5c | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 2800 | if (Constant *LHS = dyn_cast<Constant>(Op0)) |
| 2801 | if (LHS->isNullValue()) |
| 2802 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2803 | |
Chris Lattner | 50b2ca4 | 2008-02-19 06:12:18 +0000 | [diff] [blame] | 2804 | if (isa<UndefValue>(Op0)) { // undef % X -> 0 |
| 2805 | if (I.getType()->isFPOrFPVector()) |
| 2806 | return ReplaceInstUsesWith(I, Op0); // X % undef -> undef (could be SNaN) |
Chris Lattner | 19ccd5c | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 2807 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | 50b2ca4 | 2008-02-19 06:12:18 +0000 | [diff] [blame] | 2808 | } |
Chris Lattner | 19ccd5c | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 2809 | if (isa<UndefValue>(Op1)) |
| 2810 | return ReplaceInstUsesWith(I, Op1); // X % undef -> undef |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2811 | |
| 2812 | // Handle cases involving: rem X, (select Cond, Y, Z) |
| 2813 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { |
| 2814 | // rem X, (Cond ? 0 : Y) -> rem X, Y. If the rem and the select are in |
| 2815 | // the same basic block, then we replace the select with Y, and the |
| 2816 | // condition of the select with false (if the cond value is in the same |
| 2817 | // BB). If the select has uses other than the div, this allows them to be |
| 2818 | // simplified also. |
| 2819 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(1))) |
| 2820 | if (ST->isNullValue()) { |
| 2821 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); |
| 2822 | if (CondI && CondI->getParent() == I.getParent()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2823 | UpdateValueUsesWith(CondI, ConstantInt::getFalse()); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2824 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
| 2825 | I.setOperand(1, SI->getOperand(2)); |
| 2826 | else |
| 2827 | UpdateValueUsesWith(SI, SI->getOperand(2)); |
Chris Lattner | 5b73c08 | 2004-07-06 07:01:22 +0000 | [diff] [blame] | 2828 | return &I; |
| 2829 | } |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2830 | // Likewise for: rem X, (Cond ? Y : 0) -> rem X, Y |
| 2831 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(2))) |
| 2832 | if (ST->isNullValue()) { |
| 2833 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); |
| 2834 | if (CondI && CondI->getParent() == I.getParent()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2835 | UpdateValueUsesWith(CondI, ConstantInt::getTrue()); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2836 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
| 2837 | I.setOperand(1, SI->getOperand(1)); |
| 2838 | else |
| 2839 | UpdateValueUsesWith(SI, SI->getOperand(1)); |
| 2840 | return &I; |
| 2841 | } |
Chris Lattner | 11a49f2 | 2005-11-05 07:28:37 +0000 | [diff] [blame] | 2842 | } |
Chris Lattner | 5b73c08 | 2004-07-06 07:01:22 +0000 | [diff] [blame] | 2843 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2844 | return 0; |
| 2845 | } |
| 2846 | |
| 2847 | /// This function implements the transforms common to both integer remainder |
| 2848 | /// instructions (urem and srem). It is called by the visitors to those integer |
| 2849 | /// remainder instructions. |
| 2850 | /// @brief Common integer remainder transforms |
| 2851 | Instruction *InstCombiner::commonIRemTransforms(BinaryOperator &I) { |
| 2852 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2853 | |
| 2854 | if (Instruction *common = commonRemTransforms(I)) |
| 2855 | return common; |
| 2856 | |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2857 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 19ccd5c | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 2858 | // X % 0 == undef, we don't need to preserve faults! |
| 2859 | if (RHS->equalsInt(0)) |
| 2860 | return ReplaceInstUsesWith(I, UndefValue::get(I.getType())); |
| 2861 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2862 | if (RHS->equalsInt(1)) // X % 1 == 0 |
| 2863 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2864 | |
Chris Lattner | 9794392 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 2865 | if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) { |
| 2866 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0I)) { |
| 2867 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 2868 | return R; |
| 2869 | } else if (isa<PHINode>(Op0I)) { |
| 2870 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2871 | return NV; |
Chris Lattner | 9794392 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 2872 | } |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 2873 | |
| 2874 | // See if we can fold away this rem instruction. |
| 2875 | uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth(); |
| 2876 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 2877 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth), |
| 2878 | KnownZero, KnownOne)) |
| 2879 | return &I; |
Chris Lattner | 9794392 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 2880 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2881 | } |
| 2882 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2883 | return 0; |
| 2884 | } |
| 2885 | |
| 2886 | Instruction *InstCombiner::visitURem(BinaryOperator &I) { |
| 2887 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2888 | |
| 2889 | if (Instruction *common = commonIRemTransforms(I)) |
| 2890 | return common; |
| 2891 | |
| 2892 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
| 2893 | // X urem C^2 -> X and C |
| 2894 | // Check to see if this is an unsigned remainder with an exact power of 2, |
| 2895 | // if so, convert to a bitwise and. |
| 2896 | if (ConstantInt *C = dyn_cast<ConstantInt>(RHS)) |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2897 | if (C->getValue().isPowerOf2()) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2898 | return BinaryOperator::CreateAnd(Op0, SubOne(C)); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2899 | } |
| 2900 | |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2901 | if (Instruction *RHSI = dyn_cast<Instruction>(I.getOperand(1))) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2902 | // Turn A % (C << N), where C is 2^k, into A & ((C << N)-1) |
| 2903 | if (RHSI->getOpcode() == Instruction::Shl && |
| 2904 | isa<ConstantInt>(RHSI->getOperand(0))) { |
Zhou Sheng | 0fc5095 | 2007-03-25 05:01:29 +0000 | [diff] [blame] | 2905 | if (cast<ConstantInt>(RHSI->getOperand(0))->getValue().isPowerOf2()) { |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2906 | Constant *N1 = ConstantInt::getAllOnesValue(I.getType()); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2907 | Value *Add = InsertNewInstBefore(BinaryOperator::CreateAdd(RHSI, N1, |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2908 | "tmp"), I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2909 | return BinaryOperator::CreateAnd(Op0, Add); |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2910 | } |
| 2911 | } |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2912 | } |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2913 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2914 | // urem X, (select Cond, 2^C1, 2^C2) --> select Cond, (and X, C1), (and X, C2) |
| 2915 | // where C1&C2 are powers of two. |
| 2916 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { |
| 2917 | if (ConstantInt *STO = dyn_cast<ConstantInt>(SI->getOperand(1))) |
| 2918 | if (ConstantInt *SFO = dyn_cast<ConstantInt>(SI->getOperand(2))) { |
| 2919 | // STO == 0 and SFO == 0 handled above. |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2920 | if ((STO->getValue().isPowerOf2()) && |
| 2921 | (SFO->getValue().isPowerOf2())) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2922 | Value *TrueAnd = InsertNewInstBefore( |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2923 | BinaryOperator::CreateAnd(Op0, SubOne(STO), SI->getName()+".t"), I); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2924 | Value *FalseAnd = InsertNewInstBefore( |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2925 | BinaryOperator::CreateAnd(Op0, SubOne(SFO), SI->getName()+".f"), I); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 2926 | return SelectInst::Create(SI->getOperand(0), TrueAnd, FalseAnd); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2927 | } |
| 2928 | } |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2929 | } |
| 2930 | |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2931 | return 0; |
| 2932 | } |
| 2933 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2934 | Instruction *InstCombiner::visitSRem(BinaryOperator &I) { |
| 2935 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2936 | |
Dan Gohman | cff5509 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 2937 | // Handle the integer rem common cases |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2938 | if (Instruction *common = commonIRemTransforms(I)) |
| 2939 | return common; |
| 2940 | |
| 2941 | if (Value *RHSNeg = dyn_castNegVal(Op1)) |
| 2942 | if (!isa<ConstantInt>(RHSNeg) || |
Zhou Sheng | 0fc5095 | 2007-03-25 05:01:29 +0000 | [diff] [blame] | 2943 | cast<ConstantInt>(RHSNeg)->getValue().isStrictlyPositive()) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2944 | // X % -Y -> X % Y |
| 2945 | AddUsesToWorkList(I); |
| 2946 | I.setOperand(1, RHSNeg); |
| 2947 | return &I; |
| 2948 | } |
| 2949 | |
Dan Gohman | cff5509 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 2950 | // 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] | 2951 | // unsigned inputs), turn this into a urem. |
Dan Gohman | cff5509 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 2952 | if (I.getType()->isInteger()) { |
| 2953 | APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits())); |
| 2954 | if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) { |
| 2955 | // 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] | 2956 | return BinaryOperator::CreateURem(Op0, Op1, I.getName()); |
Dan Gohman | cff5509 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 2957 | } |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2958 | } |
| 2959 | |
| 2960 | return 0; |
| 2961 | } |
| 2962 | |
| 2963 | Instruction *InstCombiner::visitFRem(BinaryOperator &I) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2964 | return commonRemTransforms(I); |
| 2965 | } |
| 2966 | |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2967 | // isOneBitSet - Return true if there is exactly one bit set in the specified |
| 2968 | // constant. |
| 2969 | static bool isOneBitSet(const ConstantInt *CI) { |
Reid Spencer | 5f6a895 | 2007-03-20 00:16:52 +0000 | [diff] [blame] | 2970 | return CI->getValue().isPowerOf2(); |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2971 | } |
| 2972 | |
Chris Lattner | b20ba0a | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2973 | // isHighOnes - Return true if the constant is of the form 1+0+. |
| 2974 | // This is the same as lowones(~X). |
| 2975 | static bool isHighOnes(const ConstantInt *CI) { |
Zhou Sheng | 2cde46c | 2007-03-20 12:49:06 +0000 | [diff] [blame] | 2976 | return (~CI->getValue() + 1).isPowerOf2(); |
Chris Lattner | b20ba0a | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2977 | } |
| 2978 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2979 | /// getICmpCode - Encode a icmp predicate into a three bit mask. These bits |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2980 | /// are carefully arranged to allow folding of expressions such as: |
| 2981 | /// |
| 2982 | /// (A < B) | (A > B) --> (A != B) |
| 2983 | /// |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2984 | /// Note that this is only valid if the first and second predicates have the |
| 2985 | /// same sign. Is illegal to do: (A u< B) | (A s> B) |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2986 | /// |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2987 | /// Three bits are used to represent the condition, as follows: |
| 2988 | /// 0 A > B |
| 2989 | /// 1 A == B |
| 2990 | /// 2 A < B |
| 2991 | /// |
| 2992 | /// <=> Value Definition |
| 2993 | /// 000 0 Always false |
| 2994 | /// 001 1 A > B |
| 2995 | /// 010 2 A == B |
| 2996 | /// 011 3 A >= B |
| 2997 | /// 100 4 A < B |
| 2998 | /// 101 5 A != B |
| 2999 | /// 110 6 A <= B |
| 3000 | /// 111 7 Always true |
| 3001 | /// |
| 3002 | static unsigned getICmpCode(const ICmpInst *ICI) { |
| 3003 | switch (ICI->getPredicate()) { |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3004 | // False -> 0 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3005 | case ICmpInst::ICMP_UGT: return 1; // 001 |
| 3006 | case ICmpInst::ICMP_SGT: return 1; // 001 |
| 3007 | case ICmpInst::ICMP_EQ: return 2; // 010 |
| 3008 | case ICmpInst::ICMP_UGE: return 3; // 011 |
| 3009 | case ICmpInst::ICMP_SGE: return 3; // 011 |
| 3010 | case ICmpInst::ICMP_ULT: return 4; // 100 |
| 3011 | case ICmpInst::ICMP_SLT: return 4; // 100 |
| 3012 | case ICmpInst::ICMP_NE: return 5; // 101 |
| 3013 | case ICmpInst::ICMP_ULE: return 6; // 110 |
| 3014 | case ICmpInst::ICMP_SLE: return 6; // 110 |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3015 | // True -> 7 |
| 3016 | default: |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3017 | assert(0 && "Invalid ICmp predicate!"); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3018 | return 0; |
| 3019 | } |
| 3020 | } |
| 3021 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3022 | /// getICmpValue - This is the complement of getICmpCode, which turns an |
| 3023 | /// 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] | 3024 | /// new ICmp instruction. The sign is passed in to determine which kind |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3025 | /// of predicate to use in new icmp instructions. |
| 3026 | static Value *getICmpValue(bool sign, unsigned code, Value *LHS, Value *RHS) { |
| 3027 | switch (code) { |
| 3028 | default: assert(0 && "Illegal ICmp code!"); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3029 | case 0: return ConstantInt::getFalse(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3030 | case 1: |
| 3031 | if (sign) |
| 3032 | return new ICmpInst(ICmpInst::ICMP_SGT, LHS, RHS); |
| 3033 | else |
| 3034 | return new ICmpInst(ICmpInst::ICMP_UGT, LHS, RHS); |
| 3035 | case 2: return new ICmpInst(ICmpInst::ICMP_EQ, LHS, RHS); |
| 3036 | case 3: |
| 3037 | if (sign) |
| 3038 | return new ICmpInst(ICmpInst::ICMP_SGE, LHS, RHS); |
| 3039 | else |
| 3040 | return new ICmpInst(ICmpInst::ICMP_UGE, LHS, RHS); |
| 3041 | case 4: |
| 3042 | if (sign) |
| 3043 | return new ICmpInst(ICmpInst::ICMP_SLT, LHS, RHS); |
| 3044 | else |
| 3045 | return new ICmpInst(ICmpInst::ICMP_ULT, LHS, RHS); |
| 3046 | case 5: return new ICmpInst(ICmpInst::ICMP_NE, LHS, RHS); |
| 3047 | case 6: |
| 3048 | if (sign) |
| 3049 | return new ICmpInst(ICmpInst::ICMP_SLE, LHS, RHS); |
| 3050 | else |
| 3051 | return new ICmpInst(ICmpInst::ICMP_ULE, LHS, RHS); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3052 | case 7: return ConstantInt::getTrue(); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3053 | } |
| 3054 | } |
| 3055 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3056 | static bool PredicatesFoldable(ICmpInst::Predicate p1, ICmpInst::Predicate p2) { |
| 3057 | return (ICmpInst::isSignedPredicate(p1) == ICmpInst::isSignedPredicate(p2)) || |
| 3058 | (ICmpInst::isSignedPredicate(p1) && |
| 3059 | (p2 == ICmpInst::ICMP_EQ || p2 == ICmpInst::ICMP_NE)) || |
| 3060 | (ICmpInst::isSignedPredicate(p2) && |
| 3061 | (p1 == ICmpInst::ICMP_EQ || p1 == ICmpInst::ICMP_NE)); |
| 3062 | } |
| 3063 | |
| 3064 | namespace { |
| 3065 | // FoldICmpLogical - Implements (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B) |
| 3066 | struct FoldICmpLogical { |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3067 | InstCombiner &IC; |
| 3068 | Value *LHS, *RHS; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3069 | ICmpInst::Predicate pred; |
| 3070 | FoldICmpLogical(InstCombiner &ic, ICmpInst *ICI) |
| 3071 | : IC(ic), LHS(ICI->getOperand(0)), RHS(ICI->getOperand(1)), |
| 3072 | pred(ICI->getPredicate()) {} |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3073 | bool shouldApply(Value *V) const { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3074 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(V)) |
| 3075 | if (PredicatesFoldable(pred, ICI->getPredicate())) |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 3076 | return ((ICI->getOperand(0) == LHS && ICI->getOperand(1) == RHS) || |
| 3077 | (ICI->getOperand(0) == RHS && ICI->getOperand(1) == LHS)); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3078 | return false; |
| 3079 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3080 | Instruction *apply(Instruction &Log) const { |
| 3081 | ICmpInst *ICI = cast<ICmpInst>(Log.getOperand(0)); |
| 3082 | if (ICI->getOperand(0) != LHS) { |
| 3083 | assert(ICI->getOperand(1) == LHS); |
| 3084 | ICI->swapOperands(); // Swap the LHS and RHS of the ICmp |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3085 | } |
| 3086 | |
Chris Lattner | bc1dbfc | 2007-03-13 14:27:42 +0000 | [diff] [blame] | 3087 | ICmpInst *RHSICI = cast<ICmpInst>(Log.getOperand(1)); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3088 | unsigned LHSCode = getICmpCode(ICI); |
Chris Lattner | bc1dbfc | 2007-03-13 14:27:42 +0000 | [diff] [blame] | 3089 | unsigned RHSCode = getICmpCode(RHSICI); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3090 | unsigned Code; |
| 3091 | switch (Log.getOpcode()) { |
| 3092 | case Instruction::And: Code = LHSCode & RHSCode; break; |
| 3093 | case Instruction::Or: Code = LHSCode | RHSCode; break; |
| 3094 | case Instruction::Xor: Code = LHSCode ^ RHSCode; break; |
Chris Lattner | 021c190 | 2003-09-22 20:33:34 +0000 | [diff] [blame] | 3095 | default: assert(0 && "Illegal logical opcode!"); return 0; |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3096 | } |
| 3097 | |
Chris Lattner | bc1dbfc | 2007-03-13 14:27:42 +0000 | [diff] [blame] | 3098 | bool isSigned = ICmpInst::isSignedPredicate(RHSICI->getPredicate()) || |
| 3099 | ICmpInst::isSignedPredicate(ICI->getPredicate()); |
| 3100 | |
| 3101 | Value *RV = getICmpValue(isSigned, Code, LHS, RHS); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3102 | if (Instruction *I = dyn_cast<Instruction>(RV)) |
| 3103 | return I; |
| 3104 | // Otherwise, it's a constant boolean value... |
| 3105 | return IC.ReplaceInstUsesWith(Log, RV); |
| 3106 | } |
| 3107 | }; |
Chris Lattner | d23b5ba | 2006-11-15 04:53:24 +0000 | [diff] [blame] | 3108 | } // end anonymous namespace |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3109 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3110 | // OptAndOp - This handles expressions of the form ((val OP C1) & C2). Where |
| 3111 | // 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] | 3112 | // guaranteed to be a binary operator. |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3113 | Instruction *InstCombiner::OptAndOp(Instruction *Op, |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3114 | ConstantInt *OpRHS, |
| 3115 | ConstantInt *AndRHS, |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3116 | BinaryOperator &TheAnd) { |
| 3117 | Value *X = Op->getOperand(0); |
Chris Lattner | 76f7fe2 | 2004-01-12 19:47:05 +0000 | [diff] [blame] | 3118 | Constant *Together = 0; |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3119 | if (!Op->isShift()) |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 3120 | Together = And(AndRHS, OpRHS); |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3121 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3122 | switch (Op->getOpcode()) { |
| 3123 | case Instruction::Xor: |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3124 | if (Op->hasOneUse()) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3125 | // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3126 | Instruction *And = BinaryOperator::CreateAnd(X, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3127 | InsertNewInstBefore(And, TheAnd); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3128 | And->takeName(Op); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3129 | return BinaryOperator::CreateXor(And, Together); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3130 | } |
| 3131 | break; |
| 3132 | case Instruction::Or: |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3133 | if (Together == AndRHS) // (X | C) & C --> C |
| 3134 | return ReplaceInstUsesWith(TheAnd, AndRHS); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3135 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3136 | if (Op->hasOneUse() && Together != OpRHS) { |
| 3137 | // (X | C1) & C2 --> (X | (C1&C2)) & C2 |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3138 | Instruction *Or = BinaryOperator::CreateOr(X, Together); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3139 | InsertNewInstBefore(Or, TheAnd); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3140 | Or->takeName(Op); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3141 | return BinaryOperator::CreateAnd(Or, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3142 | } |
| 3143 | break; |
| 3144 | case Instruction::Add: |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 3145 | if (Op->hasOneUse()) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3146 | // Adding a one to a single bit bit-field should be turned into an XOR |
| 3147 | // of the bit. First thing to check is to see if this AND is with a |
| 3148 | // single bit constant. |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3149 | const APInt& AndRHSV = cast<ConstantInt>(AndRHS)->getValue(); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3150 | |
| 3151 | // If there is only one bit set... |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 3152 | if (isOneBitSet(cast<ConstantInt>(AndRHS))) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3153 | // Ok, at this point, we know that we are masking the result of the |
| 3154 | // ADD down to exactly one bit. If the constant we are adding has |
| 3155 | // no bits set below this bit, then we can eliminate the ADD. |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3156 | const APInt& AddRHS = cast<ConstantInt>(OpRHS)->getValue(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3157 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3158 | // Check to see if any bits below the one bit set in AndRHSV are set. |
| 3159 | if ((AddRHS & (AndRHSV-1)) == 0) { |
| 3160 | // If not, the only thing that can effect the output of the AND is |
| 3161 | // the bit specified by AndRHSV. If that bit is set, the effect of |
| 3162 | // the XOR is to toggle the bit. If it is clear, then the ADD has |
| 3163 | // no effect. |
| 3164 | if ((AddRHS & AndRHSV) == 0) { // Bit is not set, noop |
| 3165 | TheAnd.setOperand(0, X); |
| 3166 | return &TheAnd; |
| 3167 | } else { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3168 | // Pull the XOR out of the AND. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3169 | Instruction *NewAnd = BinaryOperator::CreateAnd(X, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3170 | InsertNewInstBefore(NewAnd, TheAnd); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3171 | NewAnd->takeName(Op); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3172 | return BinaryOperator::CreateXor(NewAnd, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3173 | } |
| 3174 | } |
| 3175 | } |
| 3176 | } |
| 3177 | break; |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 3178 | |
| 3179 | case Instruction::Shl: { |
| 3180 | // We know that the AND will not produce any of the bits shifted in, so if |
| 3181 | // the anded constant includes them, clear them now! |
| 3182 | // |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3183 | uint32_t BitWidth = AndRHS->getType()->getBitWidth(); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3184 | uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3185 | APInt ShlMask(APInt::getHighBitsSet(BitWidth, BitWidth-OpRHSVal)); |
| 3186 | ConstantInt *CI = ConstantInt::get(AndRHS->getValue() & ShlMask); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3187 | |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3188 | if (CI->getValue() == ShlMask) { |
| 3189 | // Masking out bits that the shift already masks |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 3190 | return ReplaceInstUsesWith(TheAnd, Op); // No need for the and. |
| 3191 | } else if (CI != AndRHS) { // Reducing bits set in and. |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 3192 | TheAnd.setOperand(1, CI); |
| 3193 | return &TheAnd; |
| 3194 | } |
| 3195 | break; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3196 | } |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 3197 | case Instruction::LShr: |
| 3198 | { |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 3199 | // We know that the AND will not produce any of the bits shifted in, so if |
| 3200 | // the anded constant includes them, clear them now! This only applies to |
| 3201 | // unsigned shifts, because a signed shr may bring in set bits! |
| 3202 | // |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3203 | uint32_t BitWidth = AndRHS->getType()->getBitWidth(); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3204 | uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3205 | APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal)); |
| 3206 | ConstantInt *CI = ConstantInt::get(AndRHS->getValue() & ShrMask); |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 3207 | |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3208 | if (CI->getValue() == ShrMask) { |
| 3209 | // Masking out bits that the shift already masks. |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 3210 | return ReplaceInstUsesWith(TheAnd, Op); |
| 3211 | } else if (CI != AndRHS) { |
| 3212 | TheAnd.setOperand(1, CI); // Reduce bits set in and cst. |
| 3213 | return &TheAnd; |
| 3214 | } |
| 3215 | break; |
| 3216 | } |
| 3217 | case Instruction::AShr: |
| 3218 | // Signed shr. |
| 3219 | // See if this is shifting in some sign extension, then masking it out |
| 3220 | // with an and. |
| 3221 | if (Op->hasOneUse()) { |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3222 | uint32_t BitWidth = AndRHS->getType()->getBitWidth(); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3223 | uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3224 | APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal)); |
| 3225 | Constant *C = ConstantInt::get(AndRHS->getValue() & ShrMask); |
Reid Spencer | 7eb7638 | 2006-12-13 17:19:09 +0000 | [diff] [blame] | 3226 | if (C == AndRHS) { // Masking out bits shifted in. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 3227 | // (Val ashr C1) & C2 -> (Val lshr C1) & C2 |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 3228 | // Make the argument unsigned. |
| 3229 | Value *ShVal = Op->getOperand(0); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3230 | ShVal = InsertNewInstBefore( |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3231 | BinaryOperator::CreateLShr(ShVal, OpRHS, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3232 | Op->getName()), TheAnd); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3233 | return BinaryOperator::CreateAnd(ShVal, AndRHS, TheAnd.getName()); |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 3234 | } |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 3235 | } |
| 3236 | break; |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3237 | } |
| 3238 | return 0; |
| 3239 | } |
| 3240 | |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 3241 | |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3242 | /// InsertRangeTest - Emit a computation of: (V >= Lo && V < Hi) if Inside is |
| 3243 | /// 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] | 3244 | /// (V-Lo) <u Hi-Lo. This method expects that Lo <= Hi. isSigned indicates |
| 3245 | /// 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] | 3246 | /// insert new instructions. |
| 3247 | Instruction *InstCombiner::InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3248 | bool isSigned, bool Inside, |
| 3249 | Instruction &IB) { |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3250 | assert(cast<ConstantInt>(ConstantExpr::getICmp((isSigned ? |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 3251 | ICmpInst::ICMP_SLE:ICmpInst::ICMP_ULE), Lo, Hi))->getZExtValue() && |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3252 | "Lo is not <= Hi in range emission code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3253 | |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3254 | if (Inside) { |
| 3255 | if (Lo == Hi) // Trivially false. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3256 | return new ICmpInst(ICmpInst::ICMP_NE, V, V); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3257 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3258 | // V >= Min && V < Hi --> V < Hi |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3259 | if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) { |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 3260 | ICmpInst::Predicate pred = (isSigned ? |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3261 | ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT); |
| 3262 | return new ICmpInst(pred, V, Hi); |
| 3263 | } |
| 3264 | |
| 3265 | // Emit V-Lo <u Hi-Lo |
| 3266 | Constant *NegLo = ConstantExpr::getNeg(Lo); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3267 | Instruction *Add = BinaryOperator::CreateAdd(V, NegLo, V->getName()+".off"); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3268 | InsertNewInstBefore(Add, IB); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3269 | Constant *UpperBound = ConstantExpr::getAdd(NegLo, Hi); |
| 3270 | return new ICmpInst(ICmpInst::ICMP_ULT, Add, UpperBound); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3271 | } |
| 3272 | |
| 3273 | if (Lo == Hi) // Trivially true. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3274 | return new ICmpInst(ICmpInst::ICMP_EQ, V, V); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3275 | |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 3276 | // V < Min || V >= Hi -> V > Hi-1 |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3277 | Hi = SubOne(cast<ConstantInt>(Hi)); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3278 | if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3279 | ICmpInst::Predicate pred = (isSigned ? |
| 3280 | ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT); |
| 3281 | return new ICmpInst(pred, V, Hi); |
| 3282 | } |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3283 | |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 3284 | // Emit V-Lo >u Hi-1-Lo |
| 3285 | // Note that Hi has already had one subtracted from it, above. |
| 3286 | ConstantInt *NegLo = cast<ConstantInt>(ConstantExpr::getNeg(Lo)); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3287 | Instruction *Add = BinaryOperator::CreateAdd(V, NegLo, V->getName()+".off"); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3288 | InsertNewInstBefore(Add, IB); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3289 | Constant *LowerBound = ConstantExpr::getAdd(NegLo, Hi); |
| 3290 | return new ICmpInst(ICmpInst::ICMP_UGT, Add, LowerBound); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3291 | } |
| 3292 | |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3293 | // isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with |
| 3294 | // any number of 0s on either side. The 1s are allowed to wrap from LSB to |
| 3295 | // MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is |
| 3296 | // not, since all 1s are not contiguous. |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 3297 | static bool isRunOfOnes(ConstantInt *Val, uint32_t &MB, uint32_t &ME) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3298 | const APInt& V = Val->getValue(); |
Reid Spencer | f244252 | 2007-03-24 00:42:08 +0000 | [diff] [blame] | 3299 | uint32_t BitWidth = Val->getType()->getBitWidth(); |
| 3300 | if (!APIntOps::isShiftedMask(BitWidth, V)) return false; |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3301 | |
| 3302 | // look for the first zero bit after the run of ones |
Reid Spencer | f244252 | 2007-03-24 00:42:08 +0000 | [diff] [blame] | 3303 | MB = BitWidth - ((V - 1) ^ V).countLeadingZeros(); |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3304 | // look for the first non-zero bit |
Reid Spencer | f244252 | 2007-03-24 00:42:08 +0000 | [diff] [blame] | 3305 | ME = V.getActiveBits(); |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3306 | return true; |
| 3307 | } |
| 3308 | |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3309 | /// FoldLogicalPlusAnd - This is part of an expression (LHS +/- RHS) & Mask, |
| 3310 | /// where isSub determines whether the operator is a sub. If we can fold one of |
| 3311 | /// the following xforms: |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3312 | /// |
| 3313 | /// ((A & N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == Mask |
| 3314 | /// ((A | N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0 |
| 3315 | /// ((A ^ N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0 |
| 3316 | /// |
| 3317 | /// return (A +/- B). |
| 3318 | /// |
| 3319 | Value *InstCombiner::FoldLogicalPlusAnd(Value *LHS, Value *RHS, |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3320 | ConstantInt *Mask, bool isSub, |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3321 | Instruction &I) { |
| 3322 | Instruction *LHSI = dyn_cast<Instruction>(LHS); |
| 3323 | if (!LHSI || LHSI->getNumOperands() != 2 || |
| 3324 | !isa<ConstantInt>(LHSI->getOperand(1))) return 0; |
| 3325 | |
| 3326 | ConstantInt *N = cast<ConstantInt>(LHSI->getOperand(1)); |
| 3327 | |
| 3328 | switch (LHSI->getOpcode()) { |
| 3329 | default: return 0; |
| 3330 | case Instruction::And: |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 3331 | if (And(N, Mask) == Mask) { |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3332 | // 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] | 3333 | if ((Mask->getValue().countLeadingZeros() + |
| 3334 | Mask->getValue().countPopulation()) == |
| 3335 | Mask->getValue().getBitWidth()) |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3336 | break; |
| 3337 | |
| 3338 | // Otherwise, if Mask is 0+1+0+, and if B is known to have the low 0+ |
| 3339 | // part, we don't need any explicit masks to take them out of A. If that |
| 3340 | // is all N is, ignore it. |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 3341 | uint32_t MB = 0, ME = 0; |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3342 | if (isRunOfOnes(Mask, MB, ME)) { // begin/end bit of run, inclusive |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 3343 | uint32_t BitWidth = cast<IntegerType>(RHS->getType())->getBitWidth(); |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3344 | APInt Mask(APInt::getLowBitsSet(BitWidth, MB-1)); |
Chris Lattner | 3bedbd9 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3345 | if (MaskedValueIsZero(RHS, Mask)) |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3346 | break; |
| 3347 | } |
| 3348 | } |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3349 | return 0; |
| 3350 | case Instruction::Or: |
| 3351 | case Instruction::Xor: |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3352 | // 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] | 3353 | if ((Mask->getValue().countLeadingZeros() + |
| 3354 | Mask->getValue().countPopulation()) == Mask->getValue().getBitWidth() |
Reid Spencer | 6eb0d99 | 2007-03-26 23:58:26 +0000 | [diff] [blame] | 3355 | && And(N, Mask)->isZero()) |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3356 | break; |
| 3357 | return 0; |
| 3358 | } |
| 3359 | |
| 3360 | Instruction *New; |
| 3361 | if (isSub) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3362 | New = BinaryOperator::CreateSub(LHSI->getOperand(0), RHS, "fold"); |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3363 | else |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3364 | New = BinaryOperator::CreateAdd(LHSI->getOperand(0), RHS, "fold"); |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3365 | return InsertNewInstBefore(New, I); |
| 3366 | } |
| 3367 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3368 | Instruction *InstCombiner::visitAnd(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 3369 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3370 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3371 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3372 | if (isa<UndefValue>(Op1)) // X & undef -> 0 |
| 3373 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 3374 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3375 | // and X, X = X |
| 3376 | if (Op0 == Op1) |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 3377 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3378 | |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3379 | // 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] | 3380 | // purpose is to compute bits we don't care about. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 3381 | if (!isa<VectorType>(I.getType())) { |
Reid Spencer | a03d45f | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 3382 | uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth(); |
| 3383 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 3384 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth), |
Chris Lattner | 696ee0a | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3385 | KnownZero, KnownOne)) |
Reid Spencer | 6eb0d99 | 2007-03-26 23:58:26 +0000 | [diff] [blame] | 3386 | return &I; |
Chris Lattner | 696ee0a | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3387 | } else { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 3388 | if (ConstantVector *CP = dyn_cast<ConstantVector>(Op1)) { |
Chris Lattner | 041a6c9 | 2007-06-15 05:26:55 +0000 | [diff] [blame] | 3389 | if (CP->isAllOnesValue()) // X & <-1,-1> -> X |
Chris Lattner | 696ee0a | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3390 | return ReplaceInstUsesWith(I, I.getOperand(0)); |
Chris Lattner | 041a6c9 | 2007-06-15 05:26:55 +0000 | [diff] [blame] | 3391 | } else if (isa<ConstantAggregateZero>(Op1)) { |
| 3392 | return ReplaceInstUsesWith(I, Op1); // X & <0,0> -> <0,0> |
Chris Lattner | 696ee0a | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3393 | } |
| 3394 | } |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 3395 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3396 | if (ConstantInt *AndRHS = dyn_cast<ConstantInt>(Op1)) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3397 | const APInt& AndRHSMask = AndRHS->getValue(); |
| 3398 | APInt NotAndRHS(~AndRHSMask); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3399 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3400 | // Optimize a variety of ((val OP C1) & C2) combinations... |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3401 | if (isa<BinaryOperator>(Op0)) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3402 | Instruction *Op0I = cast<Instruction>(Op0); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3403 | Value *Op0LHS = Op0I->getOperand(0); |
| 3404 | Value *Op0RHS = Op0I->getOperand(1); |
| 3405 | switch (Op0I->getOpcode()) { |
| 3406 | case Instruction::Xor: |
| 3407 | case Instruction::Or: |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 3408 | // If the mask is only needed on one incoming arm, push it up. |
| 3409 | if (Op0I->hasOneUse()) { |
| 3410 | if (MaskedValueIsZero(Op0LHS, NotAndRHS)) { |
| 3411 | // Not masking anything out for the LHS, move to RHS. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3412 | Instruction *NewRHS = BinaryOperator::CreateAnd(Op0RHS, AndRHS, |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 3413 | Op0RHS->getName()+".masked"); |
| 3414 | InsertNewInstBefore(NewRHS, I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3415 | return BinaryOperator::Create( |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 3416 | cast<BinaryOperator>(Op0I)->getOpcode(), Op0LHS, NewRHS); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3417 | } |
Chris Lattner | 3bedbd9 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3418 | if (!isa<Constant>(Op0RHS) && |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 3419 | MaskedValueIsZero(Op0RHS, NotAndRHS)) { |
| 3420 | // Not masking anything out for the RHS, move to LHS. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3421 | Instruction *NewLHS = BinaryOperator::CreateAnd(Op0LHS, AndRHS, |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 3422 | Op0LHS->getName()+".masked"); |
| 3423 | InsertNewInstBefore(NewLHS, I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3424 | return BinaryOperator::Create( |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 3425 | cast<BinaryOperator>(Op0I)->getOpcode(), NewLHS, Op0RHS); |
| 3426 | } |
| 3427 | } |
| 3428 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3429 | break; |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3430 | case Instruction::Add: |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3431 | // ((A & N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == AndRHS. |
| 3432 | // ((A | N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0 |
| 3433 | // ((A ^ N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0 |
| 3434 | if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, false, I)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3435 | return BinaryOperator::CreateAnd(V, AndRHS); |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3436 | if (Value *V = FoldLogicalPlusAnd(Op0RHS, Op0LHS, AndRHS, false, I)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3437 | return BinaryOperator::CreateAnd(V, AndRHS); // Add commutes |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3438 | break; |
| 3439 | |
| 3440 | case Instruction::Sub: |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3441 | // ((A & N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == AndRHS. |
| 3442 | // ((A | N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0 |
| 3443 | // ((A ^ N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0 |
| 3444 | if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, true, I)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3445 | return BinaryOperator::CreateAnd(V, AndRHS); |
Nick Lewycky | b4d1bc9 | 2008-07-09 04:32:37 +0000 | [diff] [blame] | 3446 | |
Nick Lewycky | 5dcc41f | 2008-07-10 05:51:40 +0000 | [diff] [blame] | 3447 | // (A - N) & AndRHS -> -N & AndRHS iff A&AndRHS==0 and AndRHS |
| 3448 | // has 1's for all bits that the subtraction with A might affect. |
| 3449 | if (Op0I->hasOneUse()) { |
| 3450 | uint32_t BitWidth = AndRHSMask.getBitWidth(); |
| 3451 | uint32_t Zeros = AndRHSMask.countLeadingZeros(); |
| 3452 | APInt Mask = APInt::getLowBitsSet(BitWidth, BitWidth - Zeros); |
| 3453 | |
Nick Lewycky | b4d1bc9 | 2008-07-09 04:32:37 +0000 | [diff] [blame] | 3454 | ConstantInt *A = dyn_cast<ConstantInt>(Op0LHS); |
Nick Lewycky | 5dcc41f | 2008-07-10 05:51:40 +0000 | [diff] [blame] | 3455 | if (!(A && A->isZero()) && // avoid infinite recursion. |
| 3456 | MaskedValueIsZero(Op0LHS, Mask)) { |
Nick Lewycky | b4d1bc9 | 2008-07-09 04:32:37 +0000 | [diff] [blame] | 3457 | Instruction *NewNeg = BinaryOperator::CreateNeg(Op0RHS); |
| 3458 | InsertNewInstBefore(NewNeg, I); |
| 3459 | return BinaryOperator::CreateAnd(NewNeg, AndRHS); |
| 3460 | } |
| 3461 | } |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3462 | break; |
Nick Lewycky | d1f77bf | 2008-07-09 05:20:13 +0000 | [diff] [blame] | 3463 | |
| 3464 | case Instruction::Shl: |
| 3465 | case Instruction::LShr: |
| 3466 | // (1 << x) & 1 --> zext(x == 0) |
| 3467 | // (1 >> x) & 1 --> zext(x == 0) |
Nick Lewycky | d8ad492 | 2008-07-09 07:35:26 +0000 | [diff] [blame] | 3468 | if (AndRHSMask == 1 && Op0LHS == AndRHS) { |
Nick Lewycky | d1f77bf | 2008-07-09 05:20:13 +0000 | [diff] [blame] | 3469 | Instruction *NewICmp = new ICmpInst(ICmpInst::ICMP_EQ, Op0RHS, |
| 3470 | Constant::getNullValue(I.getType())); |
| 3471 | InsertNewInstBefore(NewICmp, I); |
| 3472 | return new ZExtInst(NewICmp, I.getType()); |
| 3473 | } |
| 3474 | break; |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3475 | } |
| 3476 | |
Chris Lattner | 5840326 | 2003-07-23 19:25:52 +0000 | [diff] [blame] | 3477 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3478 | if (Instruction *Res = OptAndOp(Op0I, Op0CI, AndRHS, I)) |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3479 | return Res; |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3480 | } else if (CastInst *CI = dyn_cast<CastInst>(Op0)) { |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3481 | // If this is an integer truncation or change from signed-to-unsigned, and |
| 3482 | // if the source is an and/or with immediate, transform it. This |
| 3483 | // frequently occurs for bitfield accesses. |
| 3484 | if (Instruction *CastOp = dyn_cast<Instruction>(CI->getOperand(0))) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3485 | if ((isa<TruncInst>(CI) || isa<BitCastInst>(CI)) && |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3486 | CastOp->getNumOperands() == 2) |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 3487 | if (ConstantInt *AndCI = dyn_cast<ConstantInt>(CastOp->getOperand(1))) { |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3488 | if (CastOp->getOpcode() == Instruction::And) { |
| 3489 | // Change: and (cast (and X, C1) to T), C2 |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3490 | // into : and (cast X to T), trunc_or_bitcast(C1)&C2 |
| 3491 | // This will fold the two constants together, which may allow |
| 3492 | // other simplifications. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3493 | Instruction *NewCast = CastInst::CreateTruncOrBitCast( |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 3494 | CastOp->getOperand(0), I.getType(), |
| 3495 | CastOp->getName()+".shrunk"); |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3496 | NewCast = InsertNewInstBefore(NewCast, I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3497 | // trunc_or_bitcast(C1)&C2 |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 3498 | Constant *C3 = ConstantExpr::getTruncOrBitCast(AndCI,I.getType()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3499 | C3 = ConstantExpr::getAnd(C3, AndRHS); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3500 | return BinaryOperator::CreateAnd(NewCast, C3); |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3501 | } else if (CastOp->getOpcode() == Instruction::Or) { |
| 3502 | // Change: and (cast (or X, C1) to T), C2 |
| 3503 | // into : trunc(C1)&C2 iff trunc(C1)&C2 == C2 |
Chris Lattner | bb4e7b2 | 2006-12-12 19:11:20 +0000 | [diff] [blame] | 3504 | Constant *C3 = ConstantExpr::getTruncOrBitCast(AndCI,I.getType()); |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3505 | if (ConstantExpr::getAnd(C3, AndRHS) == AndRHS) // trunc(C1)&C2 |
| 3506 | return ReplaceInstUsesWith(I, AndRHS); |
| 3507 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 3508 | } |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3509 | } |
Chris Lattner | 06782f8 | 2003-07-23 19:36:21 +0000 | [diff] [blame] | 3510 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3511 | |
| 3512 | // Try to fold constant and into select arguments. |
| 3513 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3514 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3515 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 3516 | if (isa<PHINode>(Op0)) |
| 3517 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 3518 | return NV; |
Chris Lattner | c6a8aff | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 3519 | } |
| 3520 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 3521 | Value *Op0NotVal = dyn_castNotVal(Op0); |
| 3522 | Value *Op1NotVal = dyn_castNotVal(Op1); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3523 | |
Chris Lattner | 5b62aa7 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 3524 | if (Op0NotVal == Op1 || Op1NotVal == Op0) // A & ~A == ~A & A == 0 |
| 3525 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 3526 | |
Misha Brukman | cb6267b | 2004-07-30 12:50:08 +0000 | [diff] [blame] | 3527 | // (~A & ~B) == (~(A | B)) - De Morgan's Law |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 3528 | if (Op0NotVal && Op1NotVal && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3529 | Instruction *Or = BinaryOperator::CreateOr(Op0NotVal, Op1NotVal, |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3530 | I.getName()+".demorgan"); |
Chris Lattner | c6a8aff | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 3531 | InsertNewInstBefore(Or, I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3532 | return BinaryOperator::CreateNot(Or); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3533 | } |
Chris Lattner | 2082ad9 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3534 | |
| 3535 | { |
Chris Lattner | 003b620 | 2007-06-15 05:58:24 +0000 | [diff] [blame] | 3536 | Value *A = 0, *B = 0, *C = 0, *D = 0; |
| 3537 | if (match(Op0, m_Or(m_Value(A), m_Value(B)))) { |
Chris Lattner | 2082ad9 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3538 | if (A == Op1 || B == Op1) // (A | ?) & A --> A |
| 3539 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 003b620 | 2007-06-15 05:58:24 +0000 | [diff] [blame] | 3540 | |
| 3541 | // (A|B) & ~(A&B) -> A^B |
| 3542 | if (match(Op1, m_Not(m_And(m_Value(C), m_Value(D))))) { |
| 3543 | if ((A == C && B == D) || (A == D && B == C)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3544 | return BinaryOperator::CreateXor(A, B); |
Chris Lattner | 003b620 | 2007-06-15 05:58:24 +0000 | [diff] [blame] | 3545 | } |
| 3546 | } |
| 3547 | |
| 3548 | if (match(Op1, m_Or(m_Value(A), m_Value(B)))) { |
Chris Lattner | 2082ad9 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3549 | if (A == Op0 || B == Op0) // A & (A | ?) --> A |
| 3550 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 003b620 | 2007-06-15 05:58:24 +0000 | [diff] [blame] | 3551 | |
| 3552 | // ~(A&B) & (A|B) -> A^B |
| 3553 | if (match(Op0, m_Not(m_And(m_Value(C), m_Value(D))))) { |
| 3554 | if ((A == C && B == D) || (A == D && B == C)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3555 | return BinaryOperator::CreateXor(A, B); |
Chris Lattner | 003b620 | 2007-06-15 05:58:24 +0000 | [diff] [blame] | 3556 | } |
| 3557 | } |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 3558 | |
| 3559 | if (Op0->hasOneUse() && |
| 3560 | match(Op0, m_Xor(m_Value(A), m_Value(B)))) { |
| 3561 | if (A == Op1) { // (A^B)&A -> A&(A^B) |
| 3562 | I.swapOperands(); // Simplify below |
| 3563 | std::swap(Op0, Op1); |
| 3564 | } else if (B == Op1) { // (A^B)&B -> B&(B^A) |
| 3565 | cast<BinaryOperator>(Op0)->swapOperands(); |
| 3566 | I.swapOperands(); // Simplify below |
| 3567 | std::swap(Op0, Op1); |
| 3568 | } |
| 3569 | } |
| 3570 | if (Op1->hasOneUse() && |
| 3571 | match(Op1, m_Xor(m_Value(A), m_Value(B)))) { |
| 3572 | if (B == Op0) { // B&(A^B) -> B&(B^A) |
| 3573 | cast<BinaryOperator>(Op1)->swapOperands(); |
| 3574 | std::swap(A, B); |
| 3575 | } |
| 3576 | if (A == Op0) { // A&(A^B) -> A & ~B |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3577 | Instruction *NotB = BinaryOperator::CreateNot(B, "tmp"); |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 3578 | InsertNewInstBefore(NotB, I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3579 | return BinaryOperator::CreateAnd(A, NotB); |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 3580 | } |
| 3581 | } |
Chris Lattner | 2082ad9 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3582 | } |
| 3583 | |
Nick Lewycky | 9ee863e | 2008-07-09 07:29:11 +0000 | [diff] [blame] | 3584 | |
| 3585 | { // (icmp ugt/ult A, C) & (icmp B, C) --> (icmp (A|B), C) |
| 3586 | // where C is a power of 2 |
| 3587 | Value *A, *B; |
| 3588 | ConstantInt *C1, *C2; |
| 3589 | ICmpInst::Predicate LHSCC, RHSCC; |
| 3590 | if (match(&I, m_And(m_ICmp(LHSCC, m_Value(A), m_ConstantInt(C1)), |
| 3591 | m_ICmp(RHSCC, m_Value(B), m_ConstantInt(C2))))) |
| 3592 | if (C1 == C2 && LHSCC == RHSCC && C1->getValue().isPowerOf2() && |
| 3593 | (LHSCC == ICmpInst::ICMP_ULT || LHSCC == ICmpInst::ICMP_UGT)) { |
| 3594 | Instruction *NewOr = BinaryOperator::CreateOr(A, B); |
| 3595 | InsertNewInstBefore(NewOr, I); |
| 3596 | return new ICmpInst(LHSCC, NewOr, C1); |
| 3597 | } |
| 3598 | } |
| 3599 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3600 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(Op1)) { |
| 3601 | // (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B) |
| 3602 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS))) |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3603 | return R; |
| 3604 | |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3605 | Value *LHSVal, *RHSVal; |
| 3606 | ConstantInt *LHSCst, *RHSCst; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3607 | ICmpInst::Predicate LHSCC, RHSCC; |
| 3608 | if (match(Op0, m_ICmp(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst)))) |
| 3609 | if (match(RHS, m_ICmp(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst)))) |
| 3610 | if (LHSVal == RHSVal && // Found (X icmp C1) & (X icmp C2) |
| 3611 | // ICMP_[GL]E X, CST is folded to ICMP_[GL]T elsewhere. |
| 3612 | LHSCC != ICmpInst::ICMP_UGE && LHSCC != ICmpInst::ICMP_ULE && |
| 3613 | RHSCC != ICmpInst::ICMP_UGE && RHSCC != ICmpInst::ICMP_ULE && |
| 3614 | LHSCC != ICmpInst::ICMP_SGE && LHSCC != ICmpInst::ICMP_SLE && |
Chris Lattner | eec8b9a | 2007-11-22 23:47:13 +0000 | [diff] [blame] | 3615 | RHSCC != ICmpInst::ICMP_SGE && RHSCC != ICmpInst::ICMP_SLE && |
| 3616 | |
| 3617 | // Don't try to fold ICMP_SLT + ICMP_ULT. |
| 3618 | (ICmpInst::isEquality(LHSCC) || ICmpInst::isEquality(RHSCC) || |
| 3619 | ICmpInst::isSignedPredicate(LHSCC) == |
| 3620 | ICmpInst::isSignedPredicate(RHSCC))) { |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3621 | // Ensure that the larger constant is on the RHS. |
Chris Lattner | ee2b7a4 | 2008-01-13 20:59:02 +0000 | [diff] [blame] | 3622 | ICmpInst::Predicate GT; |
| 3623 | if (ICmpInst::isSignedPredicate(LHSCC) || |
| 3624 | (ICmpInst::isEquality(LHSCC) && |
| 3625 | ICmpInst::isSignedPredicate(RHSCC))) |
| 3626 | GT = ICmpInst::ICMP_SGT; |
| 3627 | else |
| 3628 | GT = ICmpInst::ICMP_UGT; |
| 3629 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3630 | Constant *Cmp = ConstantExpr::getICmp(GT, LHSCst, RHSCst); |
| 3631 | ICmpInst *LHS = cast<ICmpInst>(Op0); |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 3632 | if (cast<ConstantInt>(Cmp)->getZExtValue()) { |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3633 | std::swap(LHS, RHS); |
| 3634 | std::swap(LHSCst, RHSCst); |
| 3635 | std::swap(LHSCC, RHSCC); |
| 3636 | } |
| 3637 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3638 | // At this point, we know we have have two icmp instructions |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3639 | // comparing a value against two constants and and'ing the result |
| 3640 | // together. Because of the above check, we know that we only have |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3641 | // icmp eq, icmp ne, icmp [su]lt, and icmp [SU]gt here. We also know |
| 3642 | // (from the FoldICmpLogical check above), that the two constants |
| 3643 | // are not equal and that the larger constant is on the RHS |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3644 | assert(LHSCst != RHSCst && "Compares not folded above?"); |
| 3645 | |
| 3646 | switch (LHSCC) { |
| 3647 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3648 | case ICmpInst::ICMP_EQ: |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3649 | switch (RHSCC) { |
| 3650 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3651 | case ICmpInst::ICMP_EQ: // (X == 13 & X == 15) -> false |
| 3652 | case ICmpInst::ICMP_UGT: // (X == 13 & X > 15) -> false |
| 3653 | case ICmpInst::ICMP_SGT: // (X == 13 & X > 15) -> false |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3654 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3655 | case ICmpInst::ICMP_NE: // (X == 13 & X != 15) -> X == 13 |
| 3656 | case ICmpInst::ICMP_ULT: // (X == 13 & X < 15) -> X == 13 |
| 3657 | case ICmpInst::ICMP_SLT: // (X == 13 & X < 15) -> X == 13 |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3658 | return ReplaceInstUsesWith(I, LHS); |
| 3659 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3660 | case ICmpInst::ICMP_NE: |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3661 | switch (RHSCC) { |
| 3662 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3663 | case ICmpInst::ICMP_ULT: |
| 3664 | if (LHSCst == SubOne(RHSCst)) // (X != 13 & X u< 14) -> X < 13 |
| 3665 | return new ICmpInst(ICmpInst::ICMP_ULT, LHSVal, LHSCst); |
| 3666 | break; // (X != 13 & X u< 15) -> no change |
| 3667 | case ICmpInst::ICMP_SLT: |
| 3668 | if (LHSCst == SubOne(RHSCst)) // (X != 13 & X s< 14) -> X < 13 |
| 3669 | return new ICmpInst(ICmpInst::ICMP_SLT, LHSVal, LHSCst); |
| 3670 | break; // (X != 13 & X s< 15) -> no change |
| 3671 | case ICmpInst::ICMP_EQ: // (X != 13 & X == 15) -> X == 15 |
| 3672 | case ICmpInst::ICMP_UGT: // (X != 13 & X u> 15) -> X u> 15 |
| 3673 | case ICmpInst::ICMP_SGT: // (X != 13 & X s> 15) -> X s> 15 |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3674 | return ReplaceInstUsesWith(I, RHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3675 | case ICmpInst::ICMP_NE: |
| 3676 | if (LHSCst == SubOne(RHSCst)){// (X != 13 & X != 14) -> X-13 >u 1 |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3677 | Constant *AddCST = ConstantExpr::getNeg(LHSCst); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3678 | Instruction *Add = BinaryOperator::CreateAdd(LHSVal, AddCST, |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3679 | LHSVal->getName()+".off"); |
| 3680 | InsertNewInstBefore(Add, I); |
Chris Lattner | 424db02 | 2007-01-27 23:08:34 +0000 | [diff] [blame] | 3681 | return new ICmpInst(ICmpInst::ICMP_UGT, Add, |
| 3682 | ConstantInt::get(Add->getType(), 1)); |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3683 | } |
| 3684 | break; // (X != 13 & X != 15) -> no change |
| 3685 | } |
| 3686 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3687 | case ICmpInst::ICMP_ULT: |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3688 | switch (RHSCC) { |
| 3689 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3690 | case ICmpInst::ICMP_EQ: // (X u< 13 & X == 15) -> false |
| 3691 | case ICmpInst::ICMP_UGT: // (X u< 13 & X u> 15) -> false |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3692 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3693 | case ICmpInst::ICMP_SGT: // (X u< 13 & X s> 15) -> no change |
| 3694 | break; |
| 3695 | case ICmpInst::ICMP_NE: // (X u< 13 & X != 15) -> X u< 13 |
| 3696 | case ICmpInst::ICMP_ULT: // (X u< 13 & X u< 15) -> X u< 13 |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3697 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3698 | case ICmpInst::ICMP_SLT: // (X u< 13 & X s< 15) -> no change |
| 3699 | break; |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3700 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3701 | break; |
| 3702 | case ICmpInst::ICMP_SLT: |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3703 | switch (RHSCC) { |
| 3704 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3705 | case ICmpInst::ICMP_EQ: // (X s< 13 & X == 15) -> false |
| 3706 | case ICmpInst::ICMP_SGT: // (X s< 13 & X s> 15) -> false |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3707 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3708 | case ICmpInst::ICMP_UGT: // (X s< 13 & X u> 15) -> no change |
| 3709 | break; |
| 3710 | case ICmpInst::ICMP_NE: // (X s< 13 & X != 15) -> X < 13 |
| 3711 | case ICmpInst::ICMP_SLT: // (X s< 13 & X s< 15) -> X < 13 |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3712 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3713 | case ICmpInst::ICMP_ULT: // (X s< 13 & X u< 15) -> no change |
| 3714 | break; |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3715 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3716 | break; |
| 3717 | case ICmpInst::ICMP_UGT: |
| 3718 | switch (RHSCC) { |
| 3719 | default: assert(0 && "Unknown integer condition code!"); |
Eli Friedman | 5c1f172 | 2008-06-21 23:36:13 +0000 | [diff] [blame] | 3720 | case ICmpInst::ICMP_EQ: // (X u> 13 & X == 15) -> X == 15 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3721 | case ICmpInst::ICMP_UGT: // (X u> 13 & X u> 15) -> X u> 15 |
| 3722 | return ReplaceInstUsesWith(I, RHS); |
| 3723 | case ICmpInst::ICMP_SGT: // (X u> 13 & X s> 15) -> no change |
| 3724 | break; |
| 3725 | case ICmpInst::ICMP_NE: |
| 3726 | if (RHSCst == AddOne(LHSCst)) // (X u> 13 & X != 14) -> X u> 14 |
| 3727 | return new ICmpInst(LHSCC, LHSVal, RHSCst); |
| 3728 | break; // (X u> 13 & X != 15) -> no change |
| 3729 | case ICmpInst::ICMP_ULT: // (X u> 13 & X u< 15) ->(X-14) <u 1 |
| 3730 | return InsertRangeTest(LHSVal, AddOne(LHSCst), RHSCst, false, |
| 3731 | true, I); |
| 3732 | case ICmpInst::ICMP_SLT: // (X u> 13 & X s< 15) -> no change |
| 3733 | break; |
| 3734 | } |
| 3735 | break; |
| 3736 | case ICmpInst::ICMP_SGT: |
| 3737 | switch (RHSCC) { |
| 3738 | default: assert(0 && "Unknown integer condition code!"); |
Chris Lattner | a7d1ab0 | 2007-11-16 06:04:17 +0000 | [diff] [blame] | 3739 | case ICmpInst::ICMP_EQ: // (X s> 13 & X == 15) -> X == 15 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3740 | case ICmpInst::ICMP_SGT: // (X s> 13 & X s> 15) -> X s> 15 |
| 3741 | return ReplaceInstUsesWith(I, RHS); |
| 3742 | case ICmpInst::ICMP_UGT: // (X s> 13 & X u> 15) -> no change |
| 3743 | break; |
| 3744 | case ICmpInst::ICMP_NE: |
| 3745 | if (RHSCst == AddOne(LHSCst)) // (X s> 13 & X != 14) -> X s> 14 |
| 3746 | return new ICmpInst(LHSCC, LHSVal, RHSCst); |
| 3747 | break; // (X s> 13 & X != 15) -> no change |
| 3748 | case ICmpInst::ICMP_SLT: // (X s> 13 & X s< 15) ->(X-14) s< 1 |
| 3749 | return InsertRangeTest(LHSVal, AddOne(LHSCst), RHSCst, true, |
| 3750 | true, I); |
| 3751 | case ICmpInst::ICMP_ULT: // (X s> 13 & X u< 15) -> no change |
| 3752 | break; |
| 3753 | } |
| 3754 | break; |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3755 | } |
| 3756 | } |
| 3757 | } |
| 3758 | |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3759 | // fold (and (cast A), (cast B)) -> (cast (and A, B)) |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3760 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) |
| 3761 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) |
| 3762 | if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind ? |
| 3763 | const Type *SrcTy = Op0C->getOperand(0)->getType(); |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 3764 | if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() && |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3765 | // 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] | 3766 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), |
| 3767 | I.getType(), TD) && |
| 3768 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), |
| 3769 | I.getType(), TD)) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3770 | Instruction *NewOp = BinaryOperator::CreateAnd(Op0C->getOperand(0), |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3771 | Op1C->getOperand(0), |
| 3772 | I.getName()); |
| 3773 | InsertNewInstBefore(NewOp, I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3774 | return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType()); |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3775 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3776 | } |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 3777 | |
| 3778 | // (X >> Z) & (Y >> Z) -> (X&Y) >> Z for all shifts. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3779 | if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) { |
| 3780 | if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0)) |
| 3781 | if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() && |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 3782 | SI0->getOperand(1) == SI1->getOperand(1) && |
| 3783 | (SI0->hasOneUse() || SI1->hasOneUse())) { |
| 3784 | Instruction *NewOp = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3785 | InsertNewInstBefore(BinaryOperator::CreateAnd(SI0->getOperand(0), |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 3786 | SI1->getOperand(0), |
| 3787 | SI0->getName()), I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3788 | return BinaryOperator::Create(SI1->getOpcode(), NewOp, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3789 | SI1->getOperand(1)); |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 3790 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3791 | } |
| 3792 | |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 3793 | // (fcmp ord x, c) & (fcmp ord y, c) -> (fcmp ord x, y) |
| 3794 | if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0))) { |
| 3795 | if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1))) { |
| 3796 | if (LHS->getPredicate() == FCmpInst::FCMP_ORD && |
| 3797 | RHS->getPredicate() == FCmpInst::FCMP_ORD) |
| 3798 | if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1))) |
| 3799 | if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) { |
| 3800 | // If either of the constants are nans, then the whole thing returns |
| 3801 | // false. |
Chris Lattner | be3e348 | 2007-10-24 18:54:45 +0000 | [diff] [blame] | 3802 | if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN()) |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 3803 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
| 3804 | return new FCmpInst(FCmpInst::FCMP_ORD, LHS->getOperand(0), |
| 3805 | RHS->getOperand(0)); |
| 3806 | } |
| 3807 | } |
| 3808 | } |
Nick Lewycky | b4d1bc9 | 2008-07-09 04:32:37 +0000 | [diff] [blame] | 3809 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3810 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3811 | } |
| 3812 | |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3813 | /// CollectBSwapParts - Look to see if the specified value defines a single byte |
| 3814 | /// in the result. If it does, and if the specified byte hasn't been filled in |
| 3815 | /// yet, fill it in and return false. |
Chris Lattner | 535014f | 2007-02-15 22:52:10 +0000 | [diff] [blame] | 3816 | static bool CollectBSwapParts(Value *V, SmallVector<Value*, 8> &ByteValues) { |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3817 | Instruction *I = dyn_cast<Instruction>(V); |
| 3818 | if (I == 0) return true; |
| 3819 | |
| 3820 | // If this is an or instruction, it is an inner node of the bswap. |
| 3821 | if (I->getOpcode() == Instruction::Or) |
| 3822 | return CollectBSwapParts(I->getOperand(0), ByteValues) || |
| 3823 | CollectBSwapParts(I->getOperand(1), ByteValues); |
| 3824 | |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3825 | uint32_t BitWidth = I->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3826 | // If this is a shift by a constant int, and it is "24", then its operand |
| 3827 | // defines a byte. We only handle unsigned types here. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3828 | if (I->isShift() && isa<ConstantInt>(I->getOperand(1))) { |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3829 | // Not shifting the entire input by N-1 bytes? |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3830 | if (cast<ConstantInt>(I->getOperand(1))->getLimitedValue(BitWidth) != |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3831 | 8*(ByteValues.size()-1)) |
| 3832 | return true; |
| 3833 | |
| 3834 | unsigned DestNo; |
| 3835 | if (I->getOpcode() == Instruction::Shl) { |
| 3836 | // X << 24 defines the top byte with the lowest of the input bytes. |
| 3837 | DestNo = ByteValues.size()-1; |
| 3838 | } else { |
| 3839 | // X >>u 24 defines the low byte with the highest of the input bytes. |
| 3840 | DestNo = 0; |
| 3841 | } |
| 3842 | |
| 3843 | // If the destination byte value is already defined, the values are or'd |
| 3844 | // together, which isn't a bswap (unless it's an or of the same bits). |
| 3845 | if (ByteValues[DestNo] && ByteValues[DestNo] != I->getOperand(0)) |
| 3846 | return true; |
| 3847 | ByteValues[DestNo] = I->getOperand(0); |
| 3848 | return false; |
| 3849 | } |
| 3850 | |
| 3851 | // Otherwise, we can only handle and(shift X, imm), imm). Bail out of if we |
| 3852 | // don't have this. |
| 3853 | Value *Shift = 0, *ShiftLHS = 0; |
| 3854 | ConstantInt *AndAmt = 0, *ShiftAmt = 0; |
| 3855 | if (!match(I, m_And(m_Value(Shift), m_ConstantInt(AndAmt))) || |
| 3856 | !match(Shift, m_Shift(m_Value(ShiftLHS), m_ConstantInt(ShiftAmt)))) |
| 3857 | return true; |
| 3858 | Instruction *SI = cast<Instruction>(Shift); |
| 3859 | |
| 3860 | // Make sure that the shift amount is by a multiple of 8 and isn't too big. |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3861 | if (ShiftAmt->getLimitedValue(BitWidth) & 7 || |
| 3862 | ShiftAmt->getLimitedValue(BitWidth) > 8*ByteValues.size()) |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3863 | return true; |
| 3864 | |
| 3865 | // Turn 0xFF -> 0, 0xFF00 -> 1, 0xFF0000 -> 2, etc. |
| 3866 | unsigned DestByte; |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3867 | if (AndAmt->getValue().getActiveBits() > 64) |
| 3868 | return true; |
| 3869 | uint64_t AndAmtVal = AndAmt->getZExtValue(); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3870 | for (DestByte = 0; DestByte != ByteValues.size(); ++DestByte) |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3871 | if (AndAmtVal == uint64_t(0xFF) << 8*DestByte) |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3872 | break; |
| 3873 | // Unknown mask for bswap. |
| 3874 | if (DestByte == ByteValues.size()) return true; |
| 3875 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3876 | unsigned ShiftBytes = ShiftAmt->getZExtValue()/8; |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3877 | unsigned SrcByte; |
| 3878 | if (SI->getOpcode() == Instruction::Shl) |
| 3879 | SrcByte = DestByte - ShiftBytes; |
| 3880 | else |
| 3881 | SrcByte = DestByte + ShiftBytes; |
| 3882 | |
| 3883 | // If the SrcByte isn't a bswapped value from the DestByte, reject it. |
| 3884 | if (SrcByte != ByteValues.size()-DestByte-1) |
| 3885 | return true; |
| 3886 | |
| 3887 | // If the destination byte value is already defined, the values are or'd |
| 3888 | // together, which isn't a bswap (unless it's an or of the same bits). |
| 3889 | if (ByteValues[DestByte] && ByteValues[DestByte] != SI->getOperand(0)) |
| 3890 | return true; |
| 3891 | ByteValues[DestByte] = SI->getOperand(0); |
| 3892 | return false; |
| 3893 | } |
| 3894 | |
| 3895 | /// MatchBSwap - Given an OR instruction, check to see if this is a bswap idiom. |
| 3896 | /// If so, insert the new bswap intrinsic and return it. |
| 3897 | Instruction *InstCombiner::MatchBSwap(BinaryOperator &I) { |
Chris Lattner | 55fc8c4 | 2007-04-01 20:57:36 +0000 | [diff] [blame] | 3898 | const IntegerType *ITy = dyn_cast<IntegerType>(I.getType()); |
| 3899 | if (!ITy || ITy->getBitWidth() % 16) |
| 3900 | return 0; // Can only bswap pairs of bytes. Can't do vectors. |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3901 | |
| 3902 | /// ByteValues - For each byte of the result, we keep track of which value |
| 3903 | /// defines each byte. |
Chris Lattner | 535014f | 2007-02-15 22:52:10 +0000 | [diff] [blame] | 3904 | SmallVector<Value*, 8> ByteValues; |
Chris Lattner | 55fc8c4 | 2007-04-01 20:57:36 +0000 | [diff] [blame] | 3905 | ByteValues.resize(ITy->getBitWidth()/8); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3906 | |
| 3907 | // Try to find all the pieces corresponding to the bswap. |
| 3908 | if (CollectBSwapParts(I.getOperand(0), ByteValues) || |
| 3909 | CollectBSwapParts(I.getOperand(1), ByteValues)) |
| 3910 | return 0; |
| 3911 | |
| 3912 | // Check to see if all of the bytes come from the same value. |
| 3913 | Value *V = ByteValues[0]; |
| 3914 | if (V == 0) return 0; // Didn't find a byte? Must be zero. |
| 3915 | |
| 3916 | // Check to make sure that all of the bytes come from the same value. |
| 3917 | for (unsigned i = 1, e = ByteValues.size(); i != e; ++i) |
| 3918 | if (ByteValues[i] != V) |
| 3919 | return 0; |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 3920 | const Type *Tys[] = { ITy }; |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3921 | Module *M = I.getParent()->getParent()->getParent(); |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 3922 | Function *F = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 3923 | return CallInst::Create(F, V); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3924 | } |
| 3925 | |
| 3926 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3927 | Instruction *InstCombiner::visitOr(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 3928 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3929 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3930 | |
Chris Lattner | 42593e6 | 2007-03-24 23:56:43 +0000 | [diff] [blame] | 3931 | if (isa<UndefValue>(Op1)) // X | undef -> -1 |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 3932 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3933 | |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3934 | // or X, X = X |
| 3935 | if (Op0 == Op1) |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 3936 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3937 | |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3938 | // See if we can simplify any instructions used by the instruction whose sole |
| 3939 | // purpose is to compute bits we don't care about. |
Chris Lattner | 42593e6 | 2007-03-24 23:56:43 +0000 | [diff] [blame] | 3940 | if (!isa<VectorType>(I.getType())) { |
| 3941 | uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth(); |
| 3942 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 3943 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth), |
| 3944 | KnownZero, KnownOne)) |
| 3945 | return &I; |
Chris Lattner | 041a6c9 | 2007-06-15 05:26:55 +0000 | [diff] [blame] | 3946 | } else if (isa<ConstantAggregateZero>(Op1)) { |
| 3947 | return ReplaceInstUsesWith(I, Op0); // X | <0,0> -> X |
| 3948 | } else if (ConstantVector *CP = dyn_cast<ConstantVector>(Op1)) { |
| 3949 | if (CP->isAllOnesValue()) // X | <-1,-1> -> <-1,-1> |
| 3950 | return ReplaceInstUsesWith(I, I.getOperand(1)); |
Chris Lattner | 42593e6 | 2007-03-24 23:56:43 +0000 | [diff] [blame] | 3951 | } |
Chris Lattner | 041a6c9 | 2007-06-15 05:26:55 +0000 | [diff] [blame] | 3952 | |
| 3953 | |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3954 | |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3955 | // or X, -1 == -1 |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3956 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 3957 | ConstantInt *C1 = 0; Value *X = 0; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3958 | // (X & C1) | C2 --> (X | C2) & (C1|C2) |
| 3959 | if (match(Op0, m_And(m_Value(X), m_ConstantInt(C1))) && isOnlyUse(Op0)) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3960 | Instruction *Or = BinaryOperator::CreateOr(X, RHS); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3961 | InsertNewInstBefore(Or, I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3962 | Or->takeName(Op0); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3963 | return BinaryOperator::CreateAnd(Or, |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 3964 | ConstantInt::get(RHS->getValue() | C1->getValue())); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3965 | } |
Chris Lattner | ad44ebf | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 3966 | |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3967 | // (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2) |
| 3968 | if (match(Op0, m_Xor(m_Value(X), m_ConstantInt(C1))) && isOnlyUse(Op0)) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3969 | Instruction *Or = BinaryOperator::CreateOr(X, RHS); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3970 | InsertNewInstBefore(Or, I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3971 | Or->takeName(Op0); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3972 | return BinaryOperator::CreateXor(Or, |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 3973 | ConstantInt::get(C1->getValue() & ~RHS->getValue())); |
Chris Lattner | ad44ebf | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 3974 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3975 | |
| 3976 | // Try to fold constant and into select arguments. |
| 3977 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3978 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3979 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 3980 | if (isa<PHINode>(Op0)) |
| 3981 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 3982 | return NV; |
Chris Lattner | ad44ebf | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 3983 | } |
| 3984 | |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 3985 | Value *A = 0, *B = 0; |
| 3986 | ConstantInt *C1 = 0, *C2 = 0; |
Chris Lattner | f4d4c87 | 2005-05-07 23:49:08 +0000 | [diff] [blame] | 3987 | |
| 3988 | if (match(Op0, m_And(m_Value(A), m_Value(B)))) |
| 3989 | if (A == Op1 || B == Op1) // (A & ?) | A --> A |
| 3990 | return ReplaceInstUsesWith(I, Op1); |
| 3991 | if (match(Op1, m_And(m_Value(A), m_Value(B)))) |
| 3992 | if (A == Op0 || B == Op0) // A | (A & ?) --> A |
| 3993 | return ReplaceInstUsesWith(I, Op0); |
| 3994 | |
Chris Lattner | 6423d4c | 2006-07-10 20:25:24 +0000 | [diff] [blame] | 3995 | // (A | B) | C and A | (B | C) -> bswap if possible. |
| 3996 | // (A >> B) | (C << D) and (A << B) | (B >> C) -> bswap if possible. |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3997 | if (match(Op0, m_Or(m_Value(), m_Value())) || |
Chris Lattner | 6423d4c | 2006-07-10 20:25:24 +0000 | [diff] [blame] | 3998 | match(Op1, m_Or(m_Value(), m_Value())) || |
| 3999 | (match(Op0, m_Shift(m_Value(), m_Value())) && |
| 4000 | match(Op1, m_Shift(m_Value(), m_Value())))) { |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4001 | if (Instruction *BSwap = MatchBSwap(I)) |
| 4002 | return BSwap; |
| 4003 | } |
| 4004 | |
Chris Lattner | 6e4c649 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 4005 | // (X^C)|Y -> (X|Y)^C iff Y&C == 0 |
| 4006 | if (Op0->hasOneUse() && match(Op0, m_Xor(m_Value(A), m_ConstantInt(C1))) && |
Reid Spencer | a03d45f | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 4007 | MaskedValueIsZero(Op1, C1->getValue())) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4008 | Instruction *NOr = BinaryOperator::CreateOr(A, Op1); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 4009 | InsertNewInstBefore(NOr, I); |
| 4010 | NOr->takeName(Op0); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4011 | return BinaryOperator::CreateXor(NOr, C1); |
Chris Lattner | 6e4c649 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 4012 | } |
| 4013 | |
| 4014 | // Y|(X^C) -> (X|Y)^C iff Y&C == 0 |
| 4015 | if (Op1->hasOneUse() && match(Op1, m_Xor(m_Value(A), m_ConstantInt(C1))) && |
Reid Spencer | a03d45f | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 4016 | MaskedValueIsZero(Op0, C1->getValue())) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4017 | Instruction *NOr = BinaryOperator::CreateOr(A, Op0); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 4018 | InsertNewInstBefore(NOr, I); |
| 4019 | NOr->takeName(Op0); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4020 | return BinaryOperator::CreateXor(NOr, C1); |
Chris Lattner | 6e4c649 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 4021 | } |
| 4022 | |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 4023 | // (A & C)|(B & D) |
Chris Lattner | 2384d7b | 2007-06-19 05:43:49 +0000 | [diff] [blame] | 4024 | Value *C = 0, *D = 0; |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 4025 | if (match(Op0, m_And(m_Value(A), m_Value(C))) && |
| 4026 | match(Op1, m_And(m_Value(B), m_Value(D)))) { |
Chris Lattner | 6cae0e0 | 2007-04-08 07:55:22 +0000 | [diff] [blame] | 4027 | Value *V1 = 0, *V2 = 0, *V3 = 0; |
| 4028 | C1 = dyn_cast<ConstantInt>(C); |
| 4029 | C2 = dyn_cast<ConstantInt>(D); |
| 4030 | if (C1 && C2) { // (A & C1)|(B & C2) |
| 4031 | // If we have: ((V + N) & C1) | (V & C2) |
| 4032 | // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0 |
| 4033 | // replace with V+N. |
| 4034 | if (C1->getValue() == ~C2->getValue()) { |
| 4035 | if ((C2->getValue() & (C2->getValue()+1)) == 0 && // C2 == 0+1+ |
| 4036 | match(A, m_Add(m_Value(V1), m_Value(V2)))) { |
| 4037 | // Add commutes, try both ways. |
| 4038 | if (V1 == B && MaskedValueIsZero(V2, C2->getValue())) |
| 4039 | return ReplaceInstUsesWith(I, A); |
| 4040 | if (V2 == B && MaskedValueIsZero(V1, C2->getValue())) |
| 4041 | return ReplaceInstUsesWith(I, A); |
| 4042 | } |
| 4043 | // Or commutes, try both ways. |
| 4044 | if ((C1->getValue() & (C1->getValue()+1)) == 0 && |
| 4045 | match(B, m_Add(m_Value(V1), m_Value(V2)))) { |
| 4046 | // Add commutes, try both ways. |
| 4047 | if (V1 == A && MaskedValueIsZero(V2, C1->getValue())) |
| 4048 | return ReplaceInstUsesWith(I, B); |
| 4049 | if (V2 == A && MaskedValueIsZero(V1, C1->getValue())) |
| 4050 | return ReplaceInstUsesWith(I, B); |
| 4051 | } |
| 4052 | } |
Chris Lattner | 044e533 | 2007-04-08 08:01:49 +0000 | [diff] [blame] | 4053 | V1 = 0; V2 = 0; V3 = 0; |
Chris Lattner | 6cae0e0 | 2007-04-08 07:55:22 +0000 | [diff] [blame] | 4054 | } |
| 4055 | |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 4056 | // Check to see if we have any common things being and'ed. If so, find the |
| 4057 | // terms for V1 & (V2|V3). |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 4058 | if (isOnlyUse(Op0) || isOnlyUse(Op1)) { |
| 4059 | if (A == B) // (A & C)|(A & D) == A & (C|D) |
| 4060 | V1 = A, V2 = C, V3 = D; |
| 4061 | else if (A == D) // (A & C)|(B & A) == A & (B|C) |
| 4062 | V1 = A, V2 = B, V3 = C; |
| 4063 | else if (C == B) // (A & C)|(C & D) == C & (A|D) |
| 4064 | V1 = C, V2 = A, V3 = D; |
| 4065 | else if (C == D) // (A & C)|(B & C) == C & (A|B) |
| 4066 | V1 = C, V2 = A, V3 = B; |
| 4067 | |
| 4068 | if (V1) { |
| 4069 | Value *Or = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4070 | InsertNewInstBefore(BinaryOperator::CreateOr(V2, V3, "tmp"), I); |
| 4071 | return BinaryOperator::CreateAnd(V1, Or); |
Chris Lattner | 0b7c0bf | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 4072 | } |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 4073 | } |
Chris Lattner | e9bed7d | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 4074 | } |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4075 | |
| 4076 | // (X >> Z) | (Y >> Z) -> (X|Y) >> Z for all shifts. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4077 | if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) { |
| 4078 | if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0)) |
| 4079 | if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() && |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4080 | SI0->getOperand(1) == SI1->getOperand(1) && |
| 4081 | (SI0->hasOneUse() || SI1->hasOneUse())) { |
| 4082 | Instruction *NewOp = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4083 | InsertNewInstBefore(BinaryOperator::CreateOr(SI0->getOperand(0), |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4084 | SI1->getOperand(0), |
| 4085 | SI0->getName()), I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4086 | return BinaryOperator::Create(SI1->getOpcode(), NewOp, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4087 | SI1->getOperand(1)); |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4088 | } |
| 4089 | } |
Chris Lattner | 67ca768 | 2003-08-12 19:11:07 +0000 | [diff] [blame] | 4090 | |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4091 | if (match(Op0, m_Not(m_Value(A)))) { // ~A | Op1 |
| 4092 | if (A == Op1) // ~A | A == -1 |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4093 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4094 | } else { |
| 4095 | A = 0; |
| 4096 | } |
Chris Lattner | f4d4c87 | 2005-05-07 23:49:08 +0000 | [diff] [blame] | 4097 | // Note, A is still live here! |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4098 | if (match(Op1, m_Not(m_Value(B)))) { // Op0 | ~B |
| 4099 | if (Op0 == B) |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4100 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | a27231a | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 4101 | |
Misha Brukman | cb6267b | 2004-07-30 12:50:08 +0000 | [diff] [blame] | 4102 | // (~A | ~B) == (~(A & B)) - De Morgan's Law |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4103 | if (A && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4104 | Value *And = InsertNewInstBefore(BinaryOperator::CreateAnd(A, B, |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4105 | I.getName()+".demorgan"), I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4106 | return BinaryOperator::CreateNot(And); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4107 | } |
Chris Lattner | a27231a | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 4108 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4109 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4110 | // (icmp1 A, B) | (icmp2 A, B) --> (icmp3 A, B) |
| 4111 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1))) { |
| 4112 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS))) |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 4113 | return R; |
| 4114 | |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4115 | Value *LHSVal, *RHSVal; |
| 4116 | ConstantInt *LHSCst, *RHSCst; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4117 | ICmpInst::Predicate LHSCC, RHSCC; |
| 4118 | if (match(Op0, m_ICmp(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst)))) |
| 4119 | if (match(RHS, m_ICmp(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst)))) |
| 4120 | if (LHSVal == RHSVal && // Found (X icmp C1) | (X icmp C2) |
| 4121 | // icmp [us][gl]e x, cst is folded to icmp [us][gl]t elsewhere. |
| 4122 | LHSCC != ICmpInst::ICMP_UGE && LHSCC != ICmpInst::ICMP_ULE && |
| 4123 | RHSCC != ICmpInst::ICMP_UGE && RHSCC != ICmpInst::ICMP_ULE && |
| 4124 | LHSCC != ICmpInst::ICMP_SGE && LHSCC != ICmpInst::ICMP_SLE && |
Chris Lattner | 8885887 | 2007-05-11 05:55:56 +0000 | [diff] [blame] | 4125 | RHSCC != ICmpInst::ICMP_SGE && RHSCC != ICmpInst::ICMP_SLE && |
| 4126 | // We can't fold (ugt x, C) | (sgt x, C2). |
| 4127 | PredicatesFoldable(LHSCC, RHSCC)) { |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4128 | // Ensure that the larger constant is on the RHS. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4129 | ICmpInst *LHS = cast<ICmpInst>(Op0); |
Chris Lattner | 8885887 | 2007-05-11 05:55:56 +0000 | [diff] [blame] | 4130 | bool NeedsSwap; |
| 4131 | if (ICmpInst::isSignedPredicate(LHSCC)) |
Chris Lattner | 3aea1bd | 2007-05-11 16:58:45 +0000 | [diff] [blame] | 4132 | NeedsSwap = LHSCst->getValue().sgt(RHSCst->getValue()); |
Chris Lattner | 8885887 | 2007-05-11 05:55:56 +0000 | [diff] [blame] | 4133 | else |
Chris Lattner | 3aea1bd | 2007-05-11 16:58:45 +0000 | [diff] [blame] | 4134 | NeedsSwap = LHSCst->getValue().ugt(RHSCst->getValue()); |
Chris Lattner | 8885887 | 2007-05-11 05:55:56 +0000 | [diff] [blame] | 4135 | |
| 4136 | if (NeedsSwap) { |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4137 | std::swap(LHS, RHS); |
| 4138 | std::swap(LHSCst, RHSCst); |
| 4139 | std::swap(LHSCC, RHSCC); |
| 4140 | } |
| 4141 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4142 | // At this point, we know we have have two icmp instructions |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4143 | // comparing a value against two constants and or'ing the result |
| 4144 | // together. Because of the above check, we know that we only have |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4145 | // ICMP_EQ, ICMP_NE, ICMP_LT, and ICMP_GT here. We also know (from the |
| 4146 | // FoldICmpLogical check above), that the two constants are not |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4147 | // equal. |
| 4148 | assert(LHSCst != RHSCst && "Compares not folded above?"); |
| 4149 | |
| 4150 | switch (LHSCC) { |
| 4151 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4152 | case ICmpInst::ICMP_EQ: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4153 | switch (RHSCC) { |
| 4154 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4155 | case ICmpInst::ICMP_EQ: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4156 | if (LHSCst == SubOne(RHSCst)) {// (X == 13 | X == 14) -> X-13 <u 2 |
| 4157 | Constant *AddCST = ConstantExpr::getNeg(LHSCst); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4158 | Instruction *Add = BinaryOperator::CreateAdd(LHSVal, AddCST, |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4159 | LHSVal->getName()+".off"); |
| 4160 | InsertNewInstBefore(Add, I); |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 4161 | AddCST = Subtract(AddOne(RHSCst), LHSCst); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4162 | return new ICmpInst(ICmpInst::ICMP_ULT, Add, AddCST); |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4163 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4164 | break; // (X == 13 | X == 15) -> no change |
| 4165 | case ICmpInst::ICMP_UGT: // (X == 13 | X u> 14) -> no change |
| 4166 | case ICmpInst::ICMP_SGT: // (X == 13 | X s> 14) -> no change |
Chris Lattner | 240d6f4 | 2005-04-19 06:04:18 +0000 | [diff] [blame] | 4167 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4168 | case ICmpInst::ICMP_NE: // (X == 13 | X != 15) -> X != 15 |
| 4169 | case ICmpInst::ICMP_ULT: // (X == 13 | X u< 15) -> X u< 15 |
| 4170 | case ICmpInst::ICMP_SLT: // (X == 13 | X s< 15) -> X s< 15 |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4171 | return ReplaceInstUsesWith(I, RHS); |
| 4172 | } |
| 4173 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4174 | case ICmpInst::ICMP_NE: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4175 | switch (RHSCC) { |
| 4176 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4177 | case ICmpInst::ICMP_EQ: // (X != 13 | X == 15) -> X != 13 |
| 4178 | case ICmpInst::ICMP_UGT: // (X != 13 | X u> 15) -> X != 13 |
| 4179 | case ICmpInst::ICMP_SGT: // (X != 13 | X s> 15) -> X != 13 |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4180 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4181 | case ICmpInst::ICMP_NE: // (X != 13 | X != 15) -> true |
| 4182 | case ICmpInst::ICMP_ULT: // (X != 13 | X u< 15) -> true |
| 4183 | case ICmpInst::ICMP_SLT: // (X != 13 | X s< 15) -> true |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4184 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4185 | } |
| 4186 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4187 | case ICmpInst::ICMP_ULT: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4188 | switch (RHSCC) { |
| 4189 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4190 | case ICmpInst::ICMP_EQ: // (X u< 13 | X == 14) -> no change |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4191 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4192 | case ICmpInst::ICMP_UGT: // (X u< 13 | X u> 15) ->(X-13) u> 2 |
Chris Lattner | 74e012a | 2007-11-01 02:18:41 +0000 | [diff] [blame] | 4193 | // If RHSCst is [us]MAXINT, it is always false. Not handling |
| 4194 | // this can cause overflow. |
| 4195 | if (RHSCst->isMaxValue(false)) |
| 4196 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4197 | return InsertRangeTest(LHSVal, LHSCst, AddOne(RHSCst), false, |
| 4198 | false, I); |
| 4199 | case ICmpInst::ICMP_SGT: // (X u< 13 | X s> 15) -> no change |
| 4200 | break; |
| 4201 | case ICmpInst::ICMP_NE: // (X u< 13 | X != 15) -> X != 15 |
| 4202 | case ICmpInst::ICMP_ULT: // (X u< 13 | X u< 15) -> X u< 15 |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4203 | return ReplaceInstUsesWith(I, RHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4204 | case ICmpInst::ICMP_SLT: // (X u< 13 | X s< 15) -> no change |
| 4205 | break; |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4206 | } |
| 4207 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4208 | case ICmpInst::ICMP_SLT: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4209 | switch (RHSCC) { |
| 4210 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4211 | case ICmpInst::ICMP_EQ: // (X s< 13 | X == 14) -> no change |
| 4212 | break; |
| 4213 | case ICmpInst::ICMP_SGT: // (X s< 13 | X s> 15) ->(X-13) s> 2 |
Chris Lattner | 74e012a | 2007-11-01 02:18:41 +0000 | [diff] [blame] | 4214 | // If RHSCst is [us]MAXINT, it is always false. Not handling |
| 4215 | // this can cause overflow. |
| 4216 | if (RHSCst->isMaxValue(true)) |
| 4217 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4218 | return InsertRangeTest(LHSVal, LHSCst, AddOne(RHSCst), true, |
| 4219 | false, I); |
| 4220 | case ICmpInst::ICMP_UGT: // (X s< 13 | X u> 15) -> no change |
| 4221 | break; |
| 4222 | case ICmpInst::ICMP_NE: // (X s< 13 | X != 15) -> X != 15 |
| 4223 | case ICmpInst::ICMP_SLT: // (X s< 13 | X s< 15) -> X s< 15 |
| 4224 | return ReplaceInstUsesWith(I, RHS); |
| 4225 | case ICmpInst::ICMP_ULT: // (X s< 13 | X u< 15) -> no change |
| 4226 | break; |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4227 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4228 | break; |
| 4229 | case ICmpInst::ICMP_UGT: |
| 4230 | switch (RHSCC) { |
| 4231 | default: assert(0 && "Unknown integer condition code!"); |
| 4232 | case ICmpInst::ICMP_EQ: // (X u> 13 | X == 15) -> X u> 13 |
| 4233 | case ICmpInst::ICMP_UGT: // (X u> 13 | X u> 15) -> X u> 13 |
| 4234 | return ReplaceInstUsesWith(I, LHS); |
| 4235 | case ICmpInst::ICMP_SGT: // (X u> 13 | X s> 15) -> no change |
| 4236 | break; |
| 4237 | case ICmpInst::ICMP_NE: // (X u> 13 | X != 15) -> true |
| 4238 | case ICmpInst::ICMP_ULT: // (X u> 13 | X u< 15) -> true |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4239 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4240 | case ICmpInst::ICMP_SLT: // (X u> 13 | X s< 15) -> no change |
| 4241 | break; |
| 4242 | } |
| 4243 | break; |
| 4244 | case ICmpInst::ICMP_SGT: |
| 4245 | switch (RHSCC) { |
| 4246 | default: assert(0 && "Unknown integer condition code!"); |
| 4247 | case ICmpInst::ICMP_EQ: // (X s> 13 | X == 15) -> X > 13 |
| 4248 | case ICmpInst::ICMP_SGT: // (X s> 13 | X s> 15) -> X > 13 |
| 4249 | return ReplaceInstUsesWith(I, LHS); |
| 4250 | case ICmpInst::ICMP_UGT: // (X s> 13 | X u> 15) -> no change |
| 4251 | break; |
| 4252 | case ICmpInst::ICMP_NE: // (X s> 13 | X != 15) -> true |
| 4253 | case ICmpInst::ICMP_SLT: // (X s> 13 | X s< 15) -> true |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4254 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4255 | case ICmpInst::ICMP_ULT: // (X s> 13 | X u< 15) -> no change |
| 4256 | break; |
| 4257 | } |
| 4258 | break; |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4259 | } |
| 4260 | } |
| 4261 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4262 | |
| 4263 | // fold (or (cast A), (cast B)) -> (cast (or A, B)) |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4264 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) { |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4265 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4266 | if (Op0C->getOpcode() == Op1C->getOpcode()) {// same cast kind ? |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 4267 | if (!isa<ICmpInst>(Op0C->getOperand(0)) || |
| 4268 | !isa<ICmpInst>(Op1C->getOperand(0))) { |
| 4269 | const Type *SrcTy = Op0C->getOperand(0)->getType(); |
| 4270 | if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() && |
| 4271 | // Only do this if the casts both really cause code to be |
| 4272 | // generated. |
| 4273 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), |
| 4274 | I.getType(), TD) && |
| 4275 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), |
| 4276 | I.getType(), TD)) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4277 | Instruction *NewOp = BinaryOperator::CreateOr(Op0C->getOperand(0), |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 4278 | Op1C->getOperand(0), |
| 4279 | I.getName()); |
| 4280 | InsertNewInstBefore(NewOp, I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4281 | return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType()); |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 4282 | } |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4283 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4284 | } |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4285 | } |
| 4286 | |
| 4287 | |
| 4288 | // (fcmp uno x, c) | (fcmp uno y, c) -> (fcmp uno x, y) |
| 4289 | if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0))) { |
| 4290 | if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1))) { |
| 4291 | if (LHS->getPredicate() == FCmpInst::FCMP_UNO && |
Chris Lattner | 5ebd936 | 2008-02-29 06:09:11 +0000 | [diff] [blame] | 4292 | RHS->getPredicate() == FCmpInst::FCMP_UNO && |
| 4293 | LHS->getOperand(0)->getType() == RHS->getOperand(0)->getType()) |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4294 | if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1))) |
| 4295 | if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) { |
| 4296 | // If either of the constants are nans, then the whole thing returns |
| 4297 | // true. |
Chris Lattner | be3e348 | 2007-10-24 18:54:45 +0000 | [diff] [blame] | 4298 | if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN()) |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4299 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
| 4300 | |
| 4301 | // Otherwise, no need to compare the two constants, compare the |
| 4302 | // rest. |
| 4303 | return new FCmpInst(FCmpInst::FCMP_UNO, LHS->getOperand(0), |
| 4304 | RHS->getOperand(0)); |
| 4305 | } |
| 4306 | } |
| 4307 | } |
Chris Lattner | e9bed7d | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 4308 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4309 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4310 | } |
| 4311 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 4312 | namespace { |
| 4313 | |
Chris Lattner | c317d39 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 4314 | // XorSelf - Implements: X ^ X --> 0 |
| 4315 | struct XorSelf { |
| 4316 | Value *RHS; |
| 4317 | XorSelf(Value *rhs) : RHS(rhs) {} |
| 4318 | bool shouldApply(Value *LHS) const { return LHS == RHS; } |
| 4319 | Instruction *apply(BinaryOperator &Xor) const { |
| 4320 | return &Xor; |
| 4321 | } |
| 4322 | }; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4323 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 4324 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4325 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4326 | Instruction *InstCombiner::visitXor(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 4327 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4328 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4329 | |
Evan Cheng | d34af78 | 2008-03-25 20:07:13 +0000 | [diff] [blame] | 4330 | if (isa<UndefValue>(Op1)) { |
| 4331 | if (isa<UndefValue>(Op0)) |
| 4332 | // Handle undef ^ undef -> 0 special case. This is a common |
| 4333 | // idiom (misuse). |
| 4334 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4335 | return ReplaceInstUsesWith(I, Op1); // X ^ undef -> undef |
Evan Cheng | d34af78 | 2008-03-25 20:07:13 +0000 | [diff] [blame] | 4336 | } |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4337 | |
Chris Lattner | c317d39 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 4338 | // xor X, X = 0, even if X is nested in a sequence of Xor's. |
| 4339 | if (Instruction *Result = AssociativeOpt(I, XorSelf(Op1))) { |
Chris Lattner | a9ff5eb | 2007-08-05 08:47:58 +0000 | [diff] [blame] | 4340 | assert(Result == &I && "AssociativeOpt didn't work?"); Result=Result; |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 4341 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | c317d39 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 4342 | } |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 4343 | |
| 4344 | // See if we can simplify any instructions used by the instruction whose sole |
| 4345 | // purpose is to compute bits we don't care about. |
Reid Spencer | a03d45f | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 4346 | if (!isa<VectorType>(I.getType())) { |
| 4347 | uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth(); |
| 4348 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 4349 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth), |
| 4350 | KnownZero, KnownOne)) |
| 4351 | return &I; |
Chris Lattner | 041a6c9 | 2007-06-15 05:26:55 +0000 | [diff] [blame] | 4352 | } else if (isa<ConstantAggregateZero>(Op1)) { |
| 4353 | return ReplaceInstUsesWith(I, Op0); // X ^ <0,0> -> X |
Reid Spencer | a03d45f | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 4354 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4355 | |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4356 | // Is this a ~ operation? |
| 4357 | if (Value *NotOp = dyn_castNotVal(&I)) { |
| 4358 | // ~(~X & Y) --> (X | ~Y) - De Morgan's Law |
| 4359 | // ~(~X | Y) === (X & ~Y) - De Morgan's Law |
| 4360 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(NotOp)) { |
| 4361 | if (Op0I->getOpcode() == Instruction::And || |
| 4362 | Op0I->getOpcode() == Instruction::Or) { |
| 4363 | if (dyn_castNotVal(Op0I->getOperand(1))) Op0I->swapOperands(); |
| 4364 | if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0))) { |
| 4365 | Instruction *NotY = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4366 | BinaryOperator::CreateNot(Op0I->getOperand(1), |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4367 | Op0I->getOperand(1)->getName()+".not"); |
| 4368 | InsertNewInstBefore(NotY, I); |
| 4369 | if (Op0I->getOpcode() == Instruction::And) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4370 | return BinaryOperator::CreateOr(Op0NotVal, NotY); |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4371 | else |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4372 | return BinaryOperator::CreateAnd(Op0NotVal, NotY); |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4373 | } |
| 4374 | } |
| 4375 | } |
| 4376 | } |
| 4377 | |
| 4378 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4379 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Nick Lewycky | f947b3e | 2007-08-06 20:04:16 +0000 | [diff] [blame] | 4380 | // xor (cmp A, B), true = not (cmp A, B) = !cmp A, B |
| 4381 | if (RHS == ConstantInt::getTrue() && Op0->hasOneUse()) { |
| 4382 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(Op0)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4383 | return new ICmpInst(ICI->getInversePredicate(), |
| 4384 | ICI->getOperand(0), ICI->getOperand(1)); |
Chris Lattner | ad5b4fb | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 4385 | |
Nick Lewycky | f947b3e | 2007-08-06 20:04:16 +0000 | [diff] [blame] | 4386 | if (FCmpInst *FCI = dyn_cast<FCmpInst>(Op0)) |
| 4387 | return new FCmpInst(FCI->getInversePredicate(), |
| 4388 | FCI->getOperand(0), FCI->getOperand(1)); |
| 4389 | } |
| 4390 | |
Nick Lewycky | 517e1f5 | 2008-05-31 19:01:33 +0000 | [diff] [blame] | 4391 | // fold (xor(zext(cmp)), 1) and (xor(sext(cmp)), -1) to ext(!cmp). |
| 4392 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) { |
| 4393 | if (CmpInst *CI = dyn_cast<CmpInst>(Op0C->getOperand(0))) { |
| 4394 | if (CI->hasOneUse() && Op0C->hasOneUse()) { |
| 4395 | Instruction::CastOps Opcode = Op0C->getOpcode(); |
| 4396 | if (Opcode == Instruction::ZExt || Opcode == Instruction::SExt) { |
| 4397 | if (RHS == ConstantExpr::getCast(Opcode, ConstantInt::getTrue(), |
| 4398 | Op0C->getDestTy())) { |
| 4399 | Instruction *NewCI = InsertNewInstBefore(CmpInst::Create( |
| 4400 | CI->getOpcode(), CI->getInversePredicate(), |
| 4401 | CI->getOperand(0), CI->getOperand(1)), I); |
| 4402 | NewCI->takeName(CI); |
| 4403 | return CastInst::Create(Opcode, NewCI, Op0C->getType()); |
| 4404 | } |
| 4405 | } |
| 4406 | } |
| 4407 | } |
| 4408 | } |
| 4409 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4410 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) { |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 4411 | // ~(c-X) == X-c-1 == X+(-c-1) |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4412 | if (Op0I->getOpcode() == Instruction::Sub && RHS->isAllOnesValue()) |
| 4413 | if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4414 | Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C); |
| 4415 | Constant *ConstantRHS = ConstantExpr::getSub(NegOp0I0C, |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4416 | ConstantInt::get(I.getType(), 1)); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4417 | return BinaryOperator::CreateAdd(Op0I->getOperand(1), ConstantRHS); |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4418 | } |
Chris Lattner | 5c6e2db | 2007-04-02 05:36:22 +0000 | [diff] [blame] | 4419 | |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 4420 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) { |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 4421 | if (Op0I->getOpcode() == Instruction::Add) { |
Chris Lattner | 689d24b | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 4422 | // ~(X-c) --> (-c-1)-X |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4423 | if (RHS->isAllOnesValue()) { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4424 | Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4425 | return BinaryOperator::CreateSub( |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4426 | ConstantExpr::getSub(NegOp0CI, |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4427 | ConstantInt::get(I.getType(), 1)), |
Chris Lattner | 689d24b | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 4428 | Op0I->getOperand(0)); |
Chris Lattner | acf4e07 | 2007-04-02 05:42:22 +0000 | [diff] [blame] | 4429 | } else if (RHS->getValue().isSignBit()) { |
Chris Lattner | 5c6e2db | 2007-04-02 05:36:22 +0000 | [diff] [blame] | 4430 | // (X + C) ^ signbit -> (X + C + signbit) |
| 4431 | Constant *C = ConstantInt::get(RHS->getValue() + Op0CI->getValue()); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4432 | return BinaryOperator::CreateAdd(Op0I->getOperand(0), C); |
Chris Lattner | cd1d6d5 | 2007-04-02 05:48:58 +0000 | [diff] [blame] | 4433 | |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4434 | } |
Chris Lattner | 02bd1b3 | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 4435 | } else if (Op0I->getOpcode() == Instruction::Or) { |
| 4436 | // (X|C1)^C2 -> X^(C1|C2) iff X&~C1 == 0 |
Reid Spencer | a03d45f | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 4437 | if (MaskedValueIsZero(Op0I->getOperand(0), Op0CI->getValue())) { |
Chris Lattner | 02bd1b3 | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 4438 | Constant *NewRHS = ConstantExpr::getOr(Op0CI, RHS); |
| 4439 | // Anything in both C1 and C2 is known to be zero, remove it from |
| 4440 | // NewRHS. |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 4441 | Constant *CommonBits = And(Op0CI, RHS); |
Chris Lattner | 02bd1b3 | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 4442 | NewRHS = ConstantExpr::getAnd(NewRHS, |
| 4443 | ConstantExpr::getNot(CommonBits)); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 4444 | AddToWorkList(Op0I); |
Chris Lattner | 02bd1b3 | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 4445 | I.setOperand(0, Op0I->getOperand(0)); |
| 4446 | I.setOperand(1, NewRHS); |
| 4447 | return &I; |
| 4448 | } |
Chris Lattner | eca0c5c | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 4449 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 4450 | } |
Chris Lattner | 05bd1b2 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 4451 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 4452 | |
| 4453 | // Try to fold constant and into select arguments. |
| 4454 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 4455 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 4456 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 4457 | if (isa<PHINode>(Op0)) |
| 4458 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 4459 | return NV; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4460 | } |
| 4461 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 4462 | if (Value *X = dyn_castNotVal(Op0)) // ~A ^ A == -1 |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4463 | if (X == Op1) |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4464 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4465 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 4466 | if (Value *X = dyn_castNotVal(Op1)) // A ^ ~A == -1 |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4467 | if (X == Op0) |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4468 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4469 | |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4470 | |
| 4471 | BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1); |
| 4472 | if (Op1I) { |
| 4473 | Value *A, *B; |
| 4474 | if (match(Op1I, m_Or(m_Value(A), m_Value(B)))) { |
| 4475 | if (A == Op0) { // B^(B|A) == (A|B)^B |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4476 | Op1I->swapOperands(); |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4477 | I.swapOperands(); |
| 4478 | std::swap(Op0, Op1); |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4479 | } else if (B == Op0) { // B^(A|B) == (A|B)^B |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4480 | I.swapOperands(); // Simplified below. |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4481 | std::swap(Op0, Op1); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4482 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4483 | } else if (match(Op1I, m_Xor(m_Value(A), m_Value(B)))) { |
| 4484 | if (Op0 == A) // A^(A^B) == B |
| 4485 | return ReplaceInstUsesWith(I, B); |
| 4486 | else if (Op0 == B) // A^(B^A) == B |
| 4487 | return ReplaceInstUsesWith(I, A); |
| 4488 | } else if (match(Op1I, m_And(m_Value(A), m_Value(B))) && Op1I->hasOneUse()){ |
Chris Lattner | 6abbdf9 | 2007-04-01 05:36:37 +0000 | [diff] [blame] | 4489 | if (A == Op0) { // A^(A&B) -> A^(B&A) |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4490 | Op1I->swapOperands(); |
Chris Lattner | 6abbdf9 | 2007-04-01 05:36:37 +0000 | [diff] [blame] | 4491 | std::swap(A, B); |
| 4492 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4493 | if (B == Op0) { // A^(B&A) -> (B&A)^A |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4494 | I.swapOperands(); // Simplified below. |
| 4495 | std::swap(Op0, Op1); |
| 4496 | } |
Chris Lattner | 26ca7e1 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 4497 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4498 | } |
| 4499 | |
| 4500 | BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0); |
| 4501 | if (Op0I) { |
| 4502 | Value *A, *B; |
| 4503 | if (match(Op0I, m_Or(m_Value(A), m_Value(B))) && Op0I->hasOneUse()) { |
| 4504 | if (A == Op1) // (B|A)^B == (A|B)^B |
| 4505 | std::swap(A, B); |
| 4506 | if (B == Op1) { // (A|B)^B == A & ~B |
| 4507 | Instruction *NotB = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4508 | InsertNewInstBefore(BinaryOperator::CreateNot(Op1, "tmp"), I); |
| 4509 | return BinaryOperator::CreateAnd(A, NotB); |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4510 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4511 | } else if (match(Op0I, m_Xor(m_Value(A), m_Value(B)))) { |
| 4512 | if (Op1 == A) // (A^B)^A == B |
| 4513 | return ReplaceInstUsesWith(I, B); |
| 4514 | else if (Op1 == B) // (B^A)^A == B |
| 4515 | return ReplaceInstUsesWith(I, A); |
| 4516 | } else if (match(Op0I, m_And(m_Value(A), m_Value(B))) && Op0I->hasOneUse()){ |
| 4517 | if (A == Op1) // (A&B)^A -> (B&A)^A |
| 4518 | std::swap(A, B); |
| 4519 | if (B == Op1 && // (B&A)^A == ~B & A |
Chris Lattner | ae1ab39 | 2006-04-01 22:05:01 +0000 | [diff] [blame] | 4520 | !isa<ConstantInt>(Op1)) { // Canonical form is (B&C)^C |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4521 | Instruction *N = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4522 | InsertNewInstBefore(BinaryOperator::CreateNot(A, "tmp"), I); |
| 4523 | return BinaryOperator::CreateAnd(N, Op1); |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4524 | } |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4525 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4526 | } |
| 4527 | |
| 4528 | // (X >> Z) ^ (Y >> Z) -> (X^Y) >> Z for all shifts. |
| 4529 | if (Op0I && Op1I && Op0I->isShift() && |
| 4530 | Op0I->getOpcode() == Op1I->getOpcode() && |
| 4531 | Op0I->getOperand(1) == Op1I->getOperand(1) && |
| 4532 | (Op1I->hasOneUse() || Op1I->hasOneUse())) { |
| 4533 | Instruction *NewOp = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4534 | InsertNewInstBefore(BinaryOperator::CreateXor(Op0I->getOperand(0), |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4535 | Op1I->getOperand(0), |
| 4536 | Op0I->getName()), I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4537 | return BinaryOperator::Create(Op1I->getOpcode(), NewOp, |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4538 | Op1I->getOperand(1)); |
| 4539 | } |
| 4540 | |
| 4541 | if (Op0I && Op1I) { |
| 4542 | Value *A, *B, *C, *D; |
| 4543 | // (A & B)^(A | B) -> A ^ B |
| 4544 | if (match(Op0I, m_And(m_Value(A), m_Value(B))) && |
| 4545 | match(Op1I, m_Or(m_Value(C), m_Value(D)))) { |
| 4546 | if ((A == C && B == D) || (A == D && B == C)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4547 | return BinaryOperator::CreateXor(A, B); |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4548 | } |
| 4549 | // (A | B)^(A & B) -> A ^ B |
| 4550 | if (match(Op0I, m_Or(m_Value(A), m_Value(B))) && |
| 4551 | match(Op1I, m_And(m_Value(C), m_Value(D)))) { |
| 4552 | if ((A == C && B == D) || (A == D && B == C)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4553 | return BinaryOperator::CreateXor(A, B); |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4554 | } |
| 4555 | |
| 4556 | // (A & B)^(C & D) |
| 4557 | if ((Op0I->hasOneUse() || Op1I->hasOneUse()) && |
| 4558 | match(Op0I, m_And(m_Value(A), m_Value(B))) && |
| 4559 | match(Op1I, m_And(m_Value(C), m_Value(D)))) { |
| 4560 | // (X & Y)^(X & Y) -> (Y^Z) & X |
| 4561 | Value *X = 0, *Y = 0, *Z = 0; |
| 4562 | if (A == C) |
| 4563 | X = A, Y = B, Z = D; |
| 4564 | else if (A == D) |
| 4565 | X = A, Y = B, Z = C; |
| 4566 | else if (B == C) |
| 4567 | X = B, Y = A, Z = D; |
| 4568 | else if (B == D) |
| 4569 | X = B, Y = A, Z = C; |
| 4570 | |
| 4571 | if (X) { |
| 4572 | Instruction *NewOp = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4573 | InsertNewInstBefore(BinaryOperator::CreateXor(Y, Z, Op0->getName()), I); |
| 4574 | return BinaryOperator::CreateAnd(NewOp, X); |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4575 | } |
| 4576 | } |
| 4577 | } |
| 4578 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4579 | // (icmp1 A, B) ^ (icmp2 A, B) --> (icmp3 A, B) |
| 4580 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1))) |
| 4581 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS))) |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 4582 | return R; |
| 4583 | |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4584 | // fold (xor (cast A), (cast B)) -> (cast (xor A, B)) |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4585 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) { |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4586 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4587 | if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind? |
| 4588 | const Type *SrcTy = Op0C->getOperand(0)->getType(); |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 4589 | if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() && |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4590 | // 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] | 4591 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), |
| 4592 | I.getType(), TD) && |
| 4593 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), |
| 4594 | I.getType(), TD)) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4595 | Instruction *NewOp = BinaryOperator::CreateXor(Op0C->getOperand(0), |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4596 | Op1C->getOperand(0), |
| 4597 | I.getName()); |
| 4598 | InsertNewInstBefore(NewOp, I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4599 | return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType()); |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4600 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4601 | } |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4602 | } |
Nick Lewycky | 517e1f5 | 2008-05-31 19:01:33 +0000 | [diff] [blame] | 4603 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4604 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4605 | } |
| 4606 | |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4607 | /// AddWithOverflow - Compute Result = In1+In2, returning true if the result |
| 4608 | /// overflowed for this type. |
| 4609 | static bool AddWithOverflow(ConstantInt *&Result, ConstantInt *In1, |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 4610 | ConstantInt *In2, bool IsSigned = false) { |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 4611 | Result = cast<ConstantInt>(Add(In1, In2)); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4612 | |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 4613 | if (IsSigned) |
| 4614 | if (In2->getValue().isNegative()) |
| 4615 | return Result->getValue().sgt(In1->getValue()); |
| 4616 | else |
| 4617 | return Result->getValue().slt(In1->getValue()); |
| 4618 | else |
| 4619 | return Result->getValue().ult(In1->getValue()); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4620 | } |
| 4621 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4622 | /// EmitGEPOffset - Given a getelementptr instruction/constantexpr, emit the |
| 4623 | /// code necessary to compute the offset from the base pointer (without adding |
| 4624 | /// in the base pointer). Return the result as a signed integer of intptr size. |
| 4625 | static Value *EmitGEPOffset(User *GEP, Instruction &I, InstCombiner &IC) { |
| 4626 | TargetData &TD = IC.getTargetData(); |
| 4627 | gep_type_iterator GTI = gep_type_begin(GEP); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4628 | const Type *IntPtrTy = TD.getIntPtrType(); |
| 4629 | Value *Result = Constant::getNullValue(IntPtrTy); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4630 | |
| 4631 | // Build a mask for high order bits. |
Chris Lattner | 10c0d91 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 4632 | unsigned IntPtrWidth = TD.getPointerSizeInBits(); |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4633 | uint64_t PtrSizeMask = ~0ULL >> (64-IntPtrWidth); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4634 | |
Gabor Greif | 177dd3f | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 4635 | for (User::op_iterator i = GEP->op_begin() + 1, e = GEP->op_end(); i != e; |
| 4636 | ++i, ++GTI) { |
| 4637 | Value *Op = *i; |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 4638 | uint64_t Size = TD.getABITypeSize(GTI.getIndexedType()) & PtrSizeMask; |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4639 | if (ConstantInt *OpC = dyn_cast<ConstantInt>(Op)) { |
| 4640 | if (OpC->isZero()) continue; |
| 4641 | |
| 4642 | // Handle a struct index, which adds its field offset to the pointer. |
| 4643 | if (const StructType *STy = dyn_cast<StructType>(*GTI)) { |
| 4644 | Size = TD.getStructLayout(STy)->getElementOffset(OpC->getZExtValue()); |
| 4645 | |
| 4646 | if (ConstantInt *RC = dyn_cast<ConstantInt>(Result)) |
| 4647 | Result = ConstantInt::get(RC->getValue() + APInt(IntPtrWidth, Size)); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 4648 | else |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4649 | Result = IC.InsertNewInstBefore( |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4650 | BinaryOperator::CreateAdd(Result, |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4651 | ConstantInt::get(IntPtrTy, Size), |
| 4652 | GEP->getName()+".offs"), I); |
| 4653 | continue; |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 4654 | } |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4655 | |
| 4656 | Constant *Scale = ConstantInt::get(IntPtrTy, Size); |
| 4657 | Constant *OC = ConstantExpr::getIntegerCast(OpC, IntPtrTy, true /*SExt*/); |
| 4658 | Scale = ConstantExpr::getMul(OC, Scale); |
| 4659 | if (Constant *RC = dyn_cast<Constant>(Result)) |
| 4660 | Result = ConstantExpr::getAdd(RC, Scale); |
| 4661 | else { |
| 4662 | // Emit an add instruction. |
| 4663 | Result = IC.InsertNewInstBefore( |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4664 | BinaryOperator::CreateAdd(Result, Scale, |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4665 | GEP->getName()+".offs"), I); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 4666 | } |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4667 | continue; |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4668 | } |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4669 | // Convert to correct type. |
| 4670 | if (Op->getType() != IntPtrTy) { |
| 4671 | if (Constant *OpC = dyn_cast<Constant>(Op)) |
| 4672 | Op = ConstantExpr::getSExt(OpC, IntPtrTy); |
| 4673 | else |
| 4674 | Op = IC.InsertNewInstBefore(new SExtInst(Op, IntPtrTy, |
| 4675 | Op->getName()+".c"), I); |
| 4676 | } |
| 4677 | if (Size != 1) { |
| 4678 | Constant *Scale = ConstantInt::get(IntPtrTy, Size); |
| 4679 | if (Constant *OpC = dyn_cast<Constant>(Op)) |
| 4680 | Op = ConstantExpr::getMul(OpC, Scale); |
| 4681 | else // We'll let instcombine(mul) convert this to a shl if possible. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4682 | Op = IC.InsertNewInstBefore(BinaryOperator::CreateMul(Op, Scale, |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4683 | GEP->getName()+".idx"), I); |
| 4684 | } |
| 4685 | |
| 4686 | // Emit an add instruction. |
| 4687 | if (isa<Constant>(Op) && isa<Constant>(Result)) |
| 4688 | Result = ConstantExpr::getAdd(cast<Constant>(Op), |
| 4689 | cast<Constant>(Result)); |
| 4690 | else |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4691 | Result = IC.InsertNewInstBefore(BinaryOperator::CreateAdd(Op, Result, |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4692 | GEP->getName()+".offs"), I); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4693 | } |
| 4694 | return Result; |
| 4695 | } |
| 4696 | |
Chris Lattner | 10c0d91 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 4697 | |
| 4698 | /// EvaluateGEPOffsetExpression - Return an value that can be used to compare of |
| 4699 | /// the *offset* implied by GEP to zero. For example, if we have &A[i], we want |
| 4700 | /// to return 'i' for "icmp ne i, 0". Note that, in general, indices can be |
| 4701 | /// complex, and scales are involved. The above expression would also be legal |
| 4702 | /// to codegen as "icmp ne (i*4), 0" (assuming A is a pointer to i32). This |
| 4703 | /// later form is less amenable to optimization though, and we are allowed to |
| 4704 | /// generate the first by knowing that pointer arithmetic doesn't overflow. |
| 4705 | /// |
| 4706 | /// If we can't emit an optimized form for this expression, this returns null. |
| 4707 | /// |
| 4708 | static Value *EvaluateGEPOffsetExpression(User *GEP, Instruction &I, |
| 4709 | InstCombiner &IC) { |
Chris Lattner | 10c0d91 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 4710 | TargetData &TD = IC.getTargetData(); |
| 4711 | gep_type_iterator GTI = gep_type_begin(GEP); |
| 4712 | |
| 4713 | // Check to see if this gep only has a single variable index. If so, and if |
| 4714 | // any constant indices are a multiple of its scale, then we can compute this |
| 4715 | // in terms of the scale of the variable index. For example, if the GEP |
| 4716 | // implies an offset of "12 + i*4", then we can codegen this as "3 + i", |
| 4717 | // because the expression will cross zero at the same point. |
| 4718 | unsigned i, e = GEP->getNumOperands(); |
| 4719 | int64_t Offset = 0; |
| 4720 | for (i = 1; i != e; ++i, ++GTI) { |
| 4721 | if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i))) { |
| 4722 | // Compute the aggregate offset of constant indices. |
| 4723 | if (CI->isZero()) continue; |
| 4724 | |
| 4725 | // Handle a struct index, which adds its field offset to the pointer. |
| 4726 | if (const StructType *STy = dyn_cast<StructType>(*GTI)) { |
| 4727 | Offset += TD.getStructLayout(STy)->getElementOffset(CI->getZExtValue()); |
| 4728 | } else { |
| 4729 | uint64_t Size = TD.getABITypeSize(GTI.getIndexedType()); |
| 4730 | Offset += Size*CI->getSExtValue(); |
| 4731 | } |
| 4732 | } else { |
| 4733 | // Found our variable index. |
| 4734 | break; |
| 4735 | } |
| 4736 | } |
| 4737 | |
| 4738 | // If there are no variable indices, we must have a constant offset, just |
| 4739 | // evaluate it the general way. |
| 4740 | if (i == e) return 0; |
| 4741 | |
| 4742 | Value *VariableIdx = GEP->getOperand(i); |
| 4743 | // Determine the scale factor of the variable element. For example, this is |
| 4744 | // 4 if the variable index is into an array of i32. |
| 4745 | uint64_t VariableScale = TD.getABITypeSize(GTI.getIndexedType()); |
| 4746 | |
| 4747 | // Verify that there are no other variable indices. If so, emit the hard way. |
| 4748 | for (++i, ++GTI; i != e; ++i, ++GTI) { |
| 4749 | ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i)); |
| 4750 | if (!CI) return 0; |
| 4751 | |
| 4752 | // Compute the aggregate offset of constant indices. |
| 4753 | if (CI->isZero()) continue; |
| 4754 | |
| 4755 | // Handle a struct index, which adds its field offset to the pointer. |
| 4756 | if (const StructType *STy = dyn_cast<StructType>(*GTI)) { |
| 4757 | Offset += TD.getStructLayout(STy)->getElementOffset(CI->getZExtValue()); |
| 4758 | } else { |
| 4759 | uint64_t Size = TD.getABITypeSize(GTI.getIndexedType()); |
| 4760 | Offset += Size*CI->getSExtValue(); |
| 4761 | } |
| 4762 | } |
| 4763 | |
| 4764 | // Okay, we know we have a single variable index, which must be a |
| 4765 | // pointer/array/vector index. If there is no offset, life is simple, return |
| 4766 | // the index. |
| 4767 | unsigned IntPtrWidth = TD.getPointerSizeInBits(); |
| 4768 | if (Offset == 0) { |
| 4769 | // Cast to intptrty in case a truncation occurs. If an extension is needed, |
| 4770 | // we don't need to bother extending: the extension won't affect where the |
| 4771 | // computation crosses zero. |
| 4772 | if (VariableIdx->getType()->getPrimitiveSizeInBits() > IntPtrWidth) |
| 4773 | VariableIdx = new TruncInst(VariableIdx, TD.getIntPtrType(), |
| 4774 | VariableIdx->getNameStart(), &I); |
| 4775 | return VariableIdx; |
| 4776 | } |
| 4777 | |
| 4778 | // Otherwise, there is an index. The computation we will do will be modulo |
| 4779 | // the pointer size, so get it. |
| 4780 | uint64_t PtrSizeMask = ~0ULL >> (64-IntPtrWidth); |
| 4781 | |
| 4782 | Offset &= PtrSizeMask; |
| 4783 | VariableScale &= PtrSizeMask; |
| 4784 | |
| 4785 | // To do this transformation, any constant index must be a multiple of the |
| 4786 | // variable scale factor. For example, we can evaluate "12 + 4*i" as "3 + i", |
| 4787 | // but we can't evaluate "10 + 3*i" in terms of i. Check that the offset is a |
| 4788 | // multiple of the variable scale. |
| 4789 | int64_t NewOffs = Offset / (int64_t)VariableScale; |
| 4790 | if (Offset != NewOffs*(int64_t)VariableScale) |
| 4791 | return 0; |
| 4792 | |
| 4793 | // Okay, we can do this evaluation. Start by converting the index to intptr. |
| 4794 | const Type *IntPtrTy = TD.getIntPtrType(); |
| 4795 | if (VariableIdx->getType() != IntPtrTy) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4796 | VariableIdx = CastInst::CreateIntegerCast(VariableIdx, IntPtrTy, |
Chris Lattner | 10c0d91 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 4797 | true /*SExt*/, |
| 4798 | VariableIdx->getNameStart(), &I); |
| 4799 | Constant *OffsetVal = ConstantInt::get(IntPtrTy, NewOffs); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4800 | return BinaryOperator::CreateAdd(VariableIdx, OffsetVal, "offset", &I); |
Chris Lattner | 10c0d91 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 4801 | } |
| 4802 | |
| 4803 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4804 | /// FoldGEPICmp - Fold comparisons between a GEP instruction and something |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4805 | /// else. At this point we know that the GEP is on the LHS of the comparison. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4806 | Instruction *InstCombiner::FoldGEPICmp(User *GEPLHS, Value *RHS, |
| 4807 | ICmpInst::Predicate Cond, |
| 4808 | Instruction &I) { |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4809 | assert(dyn_castGetElementPtr(GEPLHS) && "LHS is not a getelementptr!"); |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4810 | |
Chris Lattner | 10c0d91 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 4811 | // Look through bitcasts. |
| 4812 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(RHS)) |
| 4813 | RHS = BCI->getOperand(0); |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4814 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4815 | Value *PtrBase = GEPLHS->getOperand(0); |
| 4816 | if (PtrBase == RHS) { |
Chris Lattner | 7c95deb | 2008-02-05 04:45:32 +0000 | [diff] [blame] | 4817 | // ((gep Ptr, OFFSET) cmp Ptr) ---> (OFFSET cmp 0). |
Chris Lattner | 10c0d91 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 4818 | // This transformation (ignoring the base and scales) is valid because we |
| 4819 | // know pointers can't overflow. See if we can output an optimized form. |
| 4820 | Value *Offset = EvaluateGEPOffsetExpression(GEPLHS, I, *this); |
| 4821 | |
| 4822 | // If not, synthesize the offset the hard way. |
| 4823 | if (Offset == 0) |
| 4824 | Offset = EmitGEPOffset(GEPLHS, I, *this); |
Chris Lattner | 7c95deb | 2008-02-05 04:45:32 +0000 | [diff] [blame] | 4825 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), Offset, |
| 4826 | Constant::getNullValue(Offset->getType())); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4827 | } else if (User *GEPRHS = dyn_castGetElementPtr(RHS)) { |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 4828 | // If the base pointers are different, but the indices are the same, just |
| 4829 | // compare the base pointer. |
| 4830 | if (PtrBase != GEPRHS->getOperand(0)) { |
| 4831 | bool IndicesTheSame = GEPLHS->getNumOperands()==GEPRHS->getNumOperands(); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 4832 | IndicesTheSame &= GEPLHS->getOperand(0)->getType() == |
Chris Lattner | 93b94a6 | 2005-04-26 14:40:41 +0000 | [diff] [blame] | 4833 | GEPRHS->getOperand(0)->getType(); |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 4834 | if (IndicesTheSame) |
| 4835 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i) |
| 4836 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { |
| 4837 | IndicesTheSame = false; |
| 4838 | break; |
| 4839 | } |
| 4840 | |
| 4841 | // If all indices are the same, just compare the base pointers. |
| 4842 | if (IndicesTheSame) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4843 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), |
| 4844 | GEPLHS->getOperand(0), GEPRHS->getOperand(0)); |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 4845 | |
| 4846 | // Otherwise, the base pointers are different and the indices are |
| 4847 | // different, bail out. |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4848 | return 0; |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 4849 | } |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4850 | |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4851 | // If one of the GEPs has all zero indices, recurse. |
| 4852 | bool AllZeros = true; |
| 4853 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i) |
| 4854 | if (!isa<Constant>(GEPLHS->getOperand(i)) || |
| 4855 | !cast<Constant>(GEPLHS->getOperand(i))->isNullValue()) { |
| 4856 | AllZeros = false; |
| 4857 | break; |
| 4858 | } |
| 4859 | if (AllZeros) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4860 | return FoldGEPICmp(GEPRHS, GEPLHS->getOperand(0), |
| 4861 | ICmpInst::getSwappedPredicate(Cond), I); |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 4862 | |
| 4863 | // If the other GEP has all zero indices, recurse. |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4864 | AllZeros = true; |
| 4865 | for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i) |
| 4866 | if (!isa<Constant>(GEPRHS->getOperand(i)) || |
| 4867 | !cast<Constant>(GEPRHS->getOperand(i))->isNullValue()) { |
| 4868 | AllZeros = false; |
| 4869 | break; |
| 4870 | } |
| 4871 | if (AllZeros) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4872 | return FoldGEPICmp(GEPLHS, GEPRHS->getOperand(0), Cond, I); |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4873 | |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 4874 | if (GEPLHS->getNumOperands() == GEPRHS->getNumOperands()) { |
| 4875 | // If the GEPs only differ by one index, compare it. |
| 4876 | unsigned NumDifferences = 0; // Keep track of # differences. |
| 4877 | unsigned DiffOperand = 0; // The operand that differs. |
| 4878 | for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i) |
| 4879 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4880 | if (GEPLHS->getOperand(i)->getType()->getPrimitiveSizeInBits() != |
| 4881 | GEPRHS->getOperand(i)->getType()->getPrimitiveSizeInBits()) { |
Chris Lattner | 45f57b8 | 2005-01-21 23:06:49 +0000 | [diff] [blame] | 4882 | // Irreconcilable differences. |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 4883 | NumDifferences = 2; |
| 4884 | break; |
| 4885 | } else { |
| 4886 | if (NumDifferences++) break; |
| 4887 | DiffOperand = i; |
| 4888 | } |
| 4889 | } |
| 4890 | |
| 4891 | if (NumDifferences == 0) // SAME GEP? |
| 4892 | return ReplaceInstUsesWith(I, // No comparison is needed here. |
Nick Lewycky | 455e176 | 2007-09-06 02:40:25 +0000 | [diff] [blame] | 4893 | ConstantInt::get(Type::Int1Ty, |
Nick Lewycky | fc1efbb | 2008-05-17 07:33:39 +0000 | [diff] [blame] | 4894 | ICmpInst::isTrueWhenEqual(Cond))); |
Nick Lewycky | 455e176 | 2007-09-06 02:40:25 +0000 | [diff] [blame] | 4895 | |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 4896 | else if (NumDifferences == 1) { |
Chris Lattner | 45f57b8 | 2005-01-21 23:06:49 +0000 | [diff] [blame] | 4897 | Value *LHSV = GEPLHS->getOperand(DiffOperand); |
| 4898 | Value *RHSV = GEPRHS->getOperand(DiffOperand); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4899 | // Make sure we do a signed comparison here. |
| 4900 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), LHSV, RHSV); |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 4901 | } |
| 4902 | } |
| 4903 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4904 | // Only lower this if the icmp is the only user of the GEP or if we expect |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4905 | // the result to fold to a constant! |
| 4906 | if ((isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) && |
| 4907 | (isa<ConstantExpr>(GEPRHS) || GEPRHS->hasOneUse())) { |
| 4908 | // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) ---> (OFFSET1 cmp OFFSET2) |
| 4909 | Value *L = EmitGEPOffset(GEPLHS, I, *this); |
| 4910 | Value *R = EmitGEPOffset(GEPRHS, I, *this); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4911 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), L, R); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4912 | } |
| 4913 | } |
| 4914 | return 0; |
| 4915 | } |
| 4916 | |
Chris Lattner | a540623 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 4917 | /// FoldFCmp_IntToFP_Cst - Fold fcmp ([us]itofp x, cst) if possible. |
| 4918 | /// |
| 4919 | Instruction *InstCombiner::FoldFCmp_IntToFP_Cst(FCmpInst &I, |
| 4920 | Instruction *LHSI, |
| 4921 | Constant *RHSC) { |
| 4922 | if (!isa<ConstantFP>(RHSC)) return 0; |
| 4923 | const APFloat &RHS = cast<ConstantFP>(RHSC)->getValueAPF(); |
| 4924 | |
| 4925 | // Get the width of the mantissa. We don't want to hack on conversions that |
| 4926 | // might lose information from the integer, e.g. "i64 -> float" |
Chris Lattner | 7be1c45 | 2008-05-19 21:17:23 +0000 | [diff] [blame] | 4927 | int MantissaWidth = LHSI->getType()->getFPMantissaWidth(); |
Chris Lattner | a540623 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 4928 | if (MantissaWidth == -1) return 0; // Unknown. |
| 4929 | |
| 4930 | // Check to see that the input is converted from an integer type that is small |
| 4931 | // enough that preserves all bits. TODO: check here for "known" sign bits. |
| 4932 | // This would allow us to handle (fptosi (x >>s 62) to float) if x is i64 f.e. |
| 4933 | unsigned InputSize = LHSI->getOperand(0)->getType()->getPrimitiveSizeInBits(); |
| 4934 | |
| 4935 | // If this is a uitofp instruction, we need an extra bit to hold the sign. |
| 4936 | if (isa<UIToFPInst>(LHSI)) |
| 4937 | ++InputSize; |
| 4938 | |
| 4939 | // If the conversion would lose info, don't hack on this. |
| 4940 | if ((int)InputSize > MantissaWidth) |
| 4941 | return 0; |
| 4942 | |
| 4943 | // Otherwise, we can potentially simplify the comparison. We know that it |
| 4944 | // will always come through as an integer value and we know the constant is |
| 4945 | // not a NAN (it would have been previously simplified). |
| 4946 | assert(!RHS.isNaN() && "NaN comparison not already folded!"); |
| 4947 | |
| 4948 | ICmpInst::Predicate Pred; |
| 4949 | switch (I.getPredicate()) { |
| 4950 | default: assert(0 && "Unexpected predicate!"); |
| 4951 | case FCmpInst::FCMP_UEQ: |
| 4952 | case FCmpInst::FCMP_OEQ: Pred = ICmpInst::ICMP_EQ; break; |
| 4953 | case FCmpInst::FCMP_UGT: |
| 4954 | case FCmpInst::FCMP_OGT: Pred = ICmpInst::ICMP_SGT; break; |
| 4955 | case FCmpInst::FCMP_UGE: |
| 4956 | case FCmpInst::FCMP_OGE: Pred = ICmpInst::ICMP_SGE; break; |
| 4957 | case FCmpInst::FCMP_ULT: |
| 4958 | case FCmpInst::FCMP_OLT: Pred = ICmpInst::ICMP_SLT; break; |
| 4959 | case FCmpInst::FCMP_ULE: |
| 4960 | case FCmpInst::FCMP_OLE: Pred = ICmpInst::ICMP_SLE; break; |
| 4961 | case FCmpInst::FCMP_UNE: |
| 4962 | case FCmpInst::FCMP_ONE: Pred = ICmpInst::ICMP_NE; break; |
| 4963 | case FCmpInst::FCMP_ORD: |
| 4964 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 1)); |
| 4965 | case FCmpInst::FCMP_UNO: |
| 4966 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 0)); |
| 4967 | } |
| 4968 | |
| 4969 | const IntegerType *IntTy = cast<IntegerType>(LHSI->getOperand(0)->getType()); |
| 4970 | |
| 4971 | // Now we know that the APFloat is a normal number, zero or inf. |
| 4972 | |
Chris Lattner | 8516278 | 2008-05-20 03:50:52 +0000 | [diff] [blame] | 4973 | // See if the FP constant is too large for the integer. For example, |
Chris Lattner | a540623 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 4974 | // comparing an i8 to 300.0. |
| 4975 | unsigned IntWidth = IntTy->getPrimitiveSizeInBits(); |
| 4976 | |
| 4977 | // If the RHS value is > SignedMax, fold the comparison. This handles +INF |
| 4978 | // and large values. |
| 4979 | APFloat SMax(RHS.getSemantics(), APFloat::fcZero, false); |
| 4980 | SMax.convertFromAPInt(APInt::getSignedMaxValue(IntWidth), true, |
| 4981 | APFloat::rmNearestTiesToEven); |
| 4982 | if (SMax.compare(RHS) == APFloat::cmpLessThan) { // smax < 13123.0 |
Chris Lattner | 393f7eb | 2008-05-24 04:06:28 +0000 | [diff] [blame] | 4983 | if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SLT || |
| 4984 | Pred == ICmpInst::ICMP_SLE) |
Chris Lattner | a540623 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 4985 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 1)); |
| 4986 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 0)); |
| 4987 | } |
| 4988 | |
| 4989 | // See if the RHS value is < SignedMin. |
| 4990 | APFloat SMin(RHS.getSemantics(), APFloat::fcZero, false); |
| 4991 | SMin.convertFromAPInt(APInt::getSignedMinValue(IntWidth), true, |
| 4992 | APFloat::rmNearestTiesToEven); |
| 4993 | if (SMin.compare(RHS) == APFloat::cmpGreaterThan) { // smin > 12312.0 |
Chris Lattner | 393f7eb | 2008-05-24 04:06:28 +0000 | [diff] [blame] | 4994 | if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SGT || |
| 4995 | Pred == ICmpInst::ICMP_SGE) |
Chris Lattner | a540623 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 4996 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 1)); |
| 4997 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 0)); |
| 4998 | } |
| 4999 | |
| 5000 | // Okay, now we know that the FP constant fits in the range [SMIN, SMAX] but |
| 5001 | // it may still be fractional. See if it is fractional by casting the FP |
| 5002 | // value to the integer value and back, checking for equality. Don't do this |
| 5003 | // for zero, because -0.0 is not fractional. |
| 5004 | Constant *RHSInt = ConstantExpr::getFPToSI(RHSC, IntTy); |
| 5005 | if (!RHS.isZero() && |
| 5006 | ConstantExpr::getSIToFP(RHSInt, RHSC->getType()) != RHSC) { |
| 5007 | // If we had a comparison against a fractional value, we have to adjust |
| 5008 | // the compare predicate and sometimes the value. RHSC is rounded towards |
| 5009 | // zero at this point. |
| 5010 | switch (Pred) { |
| 5011 | default: assert(0 && "Unexpected integer comparison!"); |
| 5012 | case ICmpInst::ICMP_NE: // (float)int != 4.4 --> true |
| 5013 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 1)); |
| 5014 | case ICmpInst::ICMP_EQ: // (float)int == 4.4 --> false |
| 5015 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 0)); |
| 5016 | case ICmpInst::ICMP_SLE: |
| 5017 | // (float)int <= 4.4 --> int <= 4 |
| 5018 | // (float)int <= -4.4 --> int < -4 |
| 5019 | if (RHS.isNegative()) |
| 5020 | Pred = ICmpInst::ICMP_SLT; |
| 5021 | break; |
| 5022 | case ICmpInst::ICMP_SLT: |
| 5023 | // (float)int < -4.4 --> int < -4 |
| 5024 | // (float)int < 4.4 --> int <= 4 |
| 5025 | if (!RHS.isNegative()) |
| 5026 | Pred = ICmpInst::ICMP_SLE; |
| 5027 | break; |
| 5028 | case ICmpInst::ICMP_SGT: |
| 5029 | // (float)int > 4.4 --> int > 4 |
| 5030 | // (float)int > -4.4 --> int >= -4 |
| 5031 | if (RHS.isNegative()) |
| 5032 | Pred = ICmpInst::ICMP_SGE; |
| 5033 | break; |
| 5034 | case ICmpInst::ICMP_SGE: |
| 5035 | // (float)int >= -4.4 --> int >= -4 |
| 5036 | // (float)int >= 4.4 --> int > 4 |
| 5037 | if (!RHS.isNegative()) |
| 5038 | Pred = ICmpInst::ICMP_SGT; |
| 5039 | break; |
| 5040 | } |
| 5041 | } |
| 5042 | |
| 5043 | // Lower this FP comparison into an appropriate integer version of the |
| 5044 | // comparison. |
| 5045 | return new ICmpInst(Pred, LHSI->getOperand(0), RHSInt); |
| 5046 | } |
| 5047 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5048 | Instruction *InstCombiner::visitFCmpInst(FCmpInst &I) { |
| 5049 | bool Changed = SimplifyCompare(I); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5050 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5051 | |
Chris Lattner | 58e9746 | 2007-01-14 19:42:17 +0000 | [diff] [blame] | 5052 | // Fold trivial predicates. |
| 5053 | if (I.getPredicate() == FCmpInst::FCMP_FALSE) |
| 5054 | return ReplaceInstUsesWith(I, Constant::getNullValue(Type::Int1Ty)); |
| 5055 | if (I.getPredicate() == FCmpInst::FCMP_TRUE) |
| 5056 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 1)); |
| 5057 | |
| 5058 | // Simplify 'fcmp pred X, X' |
| 5059 | if (Op0 == Op1) { |
| 5060 | switch (I.getPredicate()) { |
| 5061 | default: assert(0 && "Unknown predicate!"); |
| 5062 | case FCmpInst::FCMP_UEQ: // True if unordered or equal |
| 5063 | case FCmpInst::FCMP_UGE: // True if unordered, greater than, or equal |
| 5064 | case FCmpInst::FCMP_ULE: // True if unordered, less than, or equal |
| 5065 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 1)); |
| 5066 | case FCmpInst::FCMP_OGT: // True if ordered and greater than |
| 5067 | case FCmpInst::FCMP_OLT: // True if ordered and less than |
| 5068 | case FCmpInst::FCMP_ONE: // True if ordered and operands are unequal |
| 5069 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 0)); |
| 5070 | |
| 5071 | case FCmpInst::FCMP_UNO: // True if unordered: isnan(X) | isnan(Y) |
| 5072 | case FCmpInst::FCMP_ULT: // True if unordered or less than |
| 5073 | case FCmpInst::FCMP_UGT: // True if unordered or greater than |
| 5074 | case FCmpInst::FCMP_UNE: // True if unordered or not equal |
| 5075 | // Canonicalize these to be 'fcmp uno %X, 0.0'. |
| 5076 | I.setPredicate(FCmpInst::FCMP_UNO); |
| 5077 | I.setOperand(1, Constant::getNullValue(Op0->getType())); |
| 5078 | return &I; |
| 5079 | |
| 5080 | case FCmpInst::FCMP_ORD: // True if ordered (no nans) |
| 5081 | case FCmpInst::FCMP_OEQ: // True if ordered and equal |
| 5082 | case FCmpInst::FCMP_OGE: // True if ordered and greater than or equal |
| 5083 | case FCmpInst::FCMP_OLE: // True if ordered and less than or equal |
| 5084 | // Canonicalize these to be 'fcmp ord %X, 0.0'. |
| 5085 | I.setPredicate(FCmpInst::FCMP_ORD); |
| 5086 | I.setOperand(1, Constant::getNullValue(Op0->getType())); |
| 5087 | return &I; |
| 5088 | } |
| 5089 | } |
| 5090 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5091 | if (isa<UndefValue>(Op1)) // fcmp pred X, undef -> undef |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 5092 | return ReplaceInstUsesWith(I, UndefValue::get(Type::Int1Ty)); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5093 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5094 | // Handle fcmp with constant RHS |
| 5095 | if (Constant *RHSC = dyn_cast<Constant>(Op1)) { |
Chris Lattner | a540623 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5096 | // If the constant is a nan, see if we can fold the comparison based on it. |
| 5097 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) { |
| 5098 | if (CFP->getValueAPF().isNaN()) { |
| 5099 | if (FCmpInst::isOrdered(I.getPredicate())) // True if ordered and... |
| 5100 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 0)); |
Chris Lattner | 8516278 | 2008-05-20 03:50:52 +0000 | [diff] [blame] | 5101 | assert(FCmpInst::isUnordered(I.getPredicate()) && |
| 5102 | "Comparison must be either ordered or unordered!"); |
| 5103 | // True if unordered. |
| 5104 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 1)); |
Chris Lattner | a540623 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5105 | } |
| 5106 | } |
| 5107 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5108 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
| 5109 | switch (LHSI->getOpcode()) { |
| 5110 | case Instruction::PHI: |
Chris Lattner | 7d8ab4e | 2008-06-08 20:52:11 +0000 | [diff] [blame] | 5111 | // Only fold fcmp into the PHI if the phi and fcmp are in the same |
| 5112 | // block. If in the same block, we're encouraging jump threading. If |
| 5113 | // not, we are just pessimizing the code by making an i1 phi. |
| 5114 | if (LHSI->getParent() == I.getParent()) |
| 5115 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 5116 | return NV; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5117 | break; |
Chris Lattner | a540623 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5118 | case Instruction::SIToFP: |
| 5119 | case Instruction::UIToFP: |
| 5120 | if (Instruction *NV = FoldFCmp_IntToFP_Cst(I, LHSI, RHSC)) |
| 5121 | return NV; |
| 5122 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5123 | case Instruction::Select: |
| 5124 | // If either operand of the select is a constant, we can fold the |
| 5125 | // comparison into the select arms, which will cause one to be |
| 5126 | // constant folded and the select turned into a bitwise or. |
| 5127 | Value *Op1 = 0, *Op2 = 0; |
| 5128 | if (LHSI->hasOneUse()) { |
| 5129 | if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) { |
| 5130 | // Fold the known value into the constant operand. |
| 5131 | Op1 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC); |
| 5132 | // Insert a new FCmp of the other select operand. |
| 5133 | Op2 = InsertNewInstBefore(new FCmpInst(I.getPredicate(), |
| 5134 | LHSI->getOperand(2), RHSC, |
| 5135 | I.getName()), I); |
| 5136 | } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) { |
| 5137 | // Fold the known value into the constant operand. |
| 5138 | Op2 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC); |
| 5139 | // Insert a new FCmp of the other select operand. |
| 5140 | Op1 = InsertNewInstBefore(new FCmpInst(I.getPredicate(), |
| 5141 | LHSI->getOperand(1), RHSC, |
| 5142 | I.getName()), I); |
| 5143 | } |
| 5144 | } |
| 5145 | |
| 5146 | if (Op1) |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 5147 | return SelectInst::Create(LHSI->getOperand(0), Op1, Op2); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5148 | break; |
| 5149 | } |
| 5150 | } |
| 5151 | |
| 5152 | return Changed ? &I : 0; |
| 5153 | } |
| 5154 | |
| 5155 | Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { |
| 5156 | bool Changed = SimplifyCompare(I); |
| 5157 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 5158 | const Type *Ty = Op0->getType(); |
| 5159 | |
| 5160 | // icmp X, X |
| 5161 | if (Op0 == Op1) |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 5162 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
Nick Lewycky | fc1efbb | 2008-05-17 07:33:39 +0000 | [diff] [blame] | 5163 | I.isTrueWhenEqual())); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5164 | |
| 5165 | if (isa<UndefValue>(Op1)) // X icmp undef -> undef |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 5166 | return ReplaceInstUsesWith(I, UndefValue::get(Type::Int1Ty)); |
Christopher Lamb | 7a0678c | 2007-12-18 21:32:20 +0000 | [diff] [blame] | 5167 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5168 | // icmp <global/alloca*/null>, <global/alloca*/null> - Global/Stack value |
Chris Lattner | 711b340 | 2004-11-14 07:33:16 +0000 | [diff] [blame] | 5169 | // addresses never equal each other! We already know that Op0 != Op1. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5170 | if ((isa<GlobalValue>(Op0) || isa<AllocaInst>(Op0) || |
| 5171 | isa<ConstantPointerNull>(Op0)) && |
| 5172 | (isa<GlobalValue>(Op1) || isa<AllocaInst>(Op1) || |
Chris Lattner | 711b340 | 2004-11-14 07:33:16 +0000 | [diff] [blame] | 5173 | isa<ConstantPointerNull>(Op1))) |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 5174 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
Nick Lewycky | fc1efbb | 2008-05-17 07:33:39 +0000 | [diff] [blame] | 5175 | !I.isTrueWhenEqual())); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5176 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5177 | // icmp's with boolean values can always be turned into bitwise operations |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 5178 | if (Ty == Type::Int1Ty) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5179 | switch (I.getPredicate()) { |
| 5180 | default: assert(0 && "Invalid icmp instruction!"); |
Chris Lattner | 85b5eb0 | 2008-07-11 04:20:58 +0000 | [diff] [blame] | 5181 | case ICmpInst::ICMP_EQ: { // icmp eq i1 A, B -> ~(A^B) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5182 | Instruction *Xor = BinaryOperator::CreateXor(Op0, Op1, I.getName()+"tmp"); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5183 | InsertNewInstBefore(Xor, I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5184 | return BinaryOperator::CreateNot(Xor); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5185 | } |
Chris Lattner | 85b5eb0 | 2008-07-11 04:20:58 +0000 | [diff] [blame] | 5186 | case ICmpInst::ICMP_NE: // icmp eq i1 A, B -> A^B |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5187 | return BinaryOperator::CreateXor(Op0, Op1); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5188 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5189 | case ICmpInst::ICMP_UGT: |
Chris Lattner | 85b5eb0 | 2008-07-11 04:20:58 +0000 | [diff] [blame] | 5190 | std::swap(Op0, Op1); // Change icmp ugt -> icmp ult |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 5191 | // FALL THROUGH |
Chris Lattner | 85b5eb0 | 2008-07-11 04:20:58 +0000 | [diff] [blame] | 5192 | case ICmpInst::ICMP_ULT:{ // icmp ult i1 A, B -> ~A & B |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5193 | Instruction *Not = BinaryOperator::CreateNot(Op0, I.getName()+"tmp"); |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 5194 | InsertNewInstBefore(Not, I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5195 | return BinaryOperator::CreateAnd(Not, Op1); |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 5196 | } |
Chris Lattner | 85b5eb0 | 2008-07-11 04:20:58 +0000 | [diff] [blame] | 5197 | case ICmpInst::ICMP_SGT: |
| 5198 | std::swap(Op0, Op1); // Change icmp sgt -> icmp slt |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 5199 | // FALL THROUGH |
Chris Lattner | 85b5eb0 | 2008-07-11 04:20:58 +0000 | [diff] [blame] | 5200 | case ICmpInst::ICMP_SLT: { // icmp slt i1 A, B -> A & ~B |
| 5201 | Instruction *Not = BinaryOperator::CreateNot(Op1, I.getName()+"tmp"); |
| 5202 | InsertNewInstBefore(Not, I); |
| 5203 | return BinaryOperator::CreateAnd(Not, Op0); |
| 5204 | } |
| 5205 | case ICmpInst::ICMP_UGE: |
| 5206 | std::swap(Op0, Op1); // Change icmp uge -> icmp ule |
| 5207 | // FALL THROUGH |
| 5208 | case ICmpInst::ICMP_ULE: { // icmp ule i1 A, B -> ~A | B |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5209 | Instruction *Not = BinaryOperator::CreateNot(Op0, I.getName()+"tmp"); |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 5210 | InsertNewInstBefore(Not, I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5211 | return BinaryOperator::CreateOr(Not, Op1); |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 5212 | } |
Chris Lattner | 85b5eb0 | 2008-07-11 04:20:58 +0000 | [diff] [blame] | 5213 | case ICmpInst::ICMP_SGE: |
| 5214 | std::swap(Op0, Op1); // Change icmp sge -> icmp sle |
| 5215 | // FALL THROUGH |
| 5216 | case ICmpInst::ICMP_SLE: { // icmp sle i1 A, B -> A | ~B |
| 5217 | Instruction *Not = BinaryOperator::CreateNot(Op1, I.getName()+"tmp"); |
| 5218 | InsertNewInstBefore(Not, I); |
| 5219 | return BinaryOperator::CreateOr(Not, Op0); |
| 5220 | } |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 5221 | } |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5222 | } |
| 5223 | |
Chris Lattner | 2be51ae | 2004-06-09 04:24:29 +0000 | [diff] [blame] | 5224 | // See if we are doing a comparison between a constant and an instruction that |
| 5225 | // can be folded into the comparison. |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5226 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | f299184 | 2008-07-11 04:09:09 +0000 | [diff] [blame] | 5227 | Value *A, *B; |
Christopher Lamb | 103e1a3 | 2007-12-20 07:21:11 +0000 | [diff] [blame] | 5228 | |
Chris Lattner | b656601 | 2008-01-05 01:18:20 +0000 | [diff] [blame] | 5229 | // (icmp ne/eq (sub A B) 0) -> (icmp ne/eq A, B) |
| 5230 | if (I.isEquality() && CI->isNullValue() && |
| 5231 | match(Op0, m_Sub(m_Value(A), m_Value(B)))) { |
| 5232 | // (icmp cond A B) if cond is equality |
| 5233 | return new ICmpInst(I.getPredicate(), A, B); |
Owen Anderson | f5783f8 | 2007-12-28 07:42:12 +0000 | [diff] [blame] | 5234 | } |
Christopher Lamb | 103e1a3 | 2007-12-20 07:21:11 +0000 | [diff] [blame] | 5235 | |
Chris Lattner | 84dff67 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 5236 | // If we have a icmp le or icmp ge instruction, turn it into the appropriate |
| 5237 | // icmp lt or icmp gt instruction. This allows us to rely on them being |
| 5238 | // folded in the code below. |
| 5239 | switch (I.getPredicate()) { |
| 5240 | default: break; |
| 5241 | case ICmpInst::ICMP_ULE: |
| 5242 | if (CI->isMaxValue(false)) // A <=u MAX -> TRUE |
| 5243 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
| 5244 | return new ICmpInst(ICmpInst::ICMP_ULT, Op0, AddOne(CI)); |
| 5245 | case ICmpInst::ICMP_SLE: |
| 5246 | if (CI->isMaxValue(true)) // A <=s MAX -> TRUE |
| 5247 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
| 5248 | return new ICmpInst(ICmpInst::ICMP_SLT, Op0, AddOne(CI)); |
| 5249 | case ICmpInst::ICMP_UGE: |
| 5250 | if (CI->isMinValue(false)) // A >=u MIN -> TRUE |
| 5251 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
| 5252 | return new ICmpInst( ICmpInst::ICMP_UGT, Op0, SubOne(CI)); |
| 5253 | case ICmpInst::ICMP_SGE: |
| 5254 | if (CI->isMinValue(true)) // A >=s MIN -> TRUE |
| 5255 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
| 5256 | return new ICmpInst(ICmpInst::ICMP_SGT, Op0, SubOne(CI)); |
| 5257 | } |
| 5258 | |
Chris Lattner | 183661e | 2008-07-11 05:40:05 +0000 | [diff] [blame^] | 5259 | // See if we can fold the comparison based on range information we can get |
| 5260 | // by checking whether bits are known to be zero or one in the input. |
| 5261 | uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth(); |
| 5262 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 5263 | |
| 5264 | // If this comparison is a normal comparison, it demands all |
Chris Lattner | 4241e4d | 2007-07-15 20:54:51 +0000 | [diff] [blame] | 5265 | // bits, if it is a sign bit comparison, it only demands the sign bit. |
Chris Lattner | 4241e4d | 2007-07-15 20:54:51 +0000 | [diff] [blame] | 5266 | bool UnusedBit; |
| 5267 | bool isSignBit = isSignBitCheck(I.getPredicate(), CI, UnusedBit); |
| 5268 | |
Chris Lattner | 4241e4d | 2007-07-15 20:54:51 +0000 | [diff] [blame] | 5269 | if (SimplifyDemandedBits(Op0, |
| 5270 | isSignBit ? APInt::getSignBit(BitWidth) |
| 5271 | : APInt::getAllOnesValue(BitWidth), |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 5272 | KnownZero, KnownOne, 0)) |
| 5273 | return &I; |
| 5274 | |
| 5275 | // Given the known and unknown bits, compute a range that the LHS could be |
Chris Lattner | 84dff67 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 5276 | // in. Compute the Min, Max and RHS values based on the known bits. For the |
| 5277 | // EQ and NE we use unsigned values. |
| 5278 | APInt Min(BitWidth, 0), Max(BitWidth, 0); |
Chris Lattner | 84dff67 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 5279 | if (ICmpInst::isSignedPredicate(I.getPredicate())) |
| 5280 | ComputeSignedMinMaxValuesFromKnownBits(Ty, KnownZero, KnownOne, Min, Max); |
| 5281 | else |
| 5282 | ComputeUnsignedMinMaxValuesFromKnownBits(Ty, KnownZero, KnownOne,Min,Max); |
| 5283 | |
Chris Lattner | 183661e | 2008-07-11 05:40:05 +0000 | [diff] [blame^] | 5284 | // If Min and Max are known to be the same, then SimplifyDemandedBits |
| 5285 | // figured out that the LHS is a constant. Just constant fold this now so |
| 5286 | // that code below can assume that Min != Max. |
| 5287 | if (Min == Max) |
| 5288 | return ReplaceInstUsesWith(I, ConstantExpr::getICmp(I.getPredicate(), |
| 5289 | ConstantInt::get(Min), |
| 5290 | CI)); |
| 5291 | |
| 5292 | // Based on the range information we know about the LHS, see if we can |
| 5293 | // simplify this comparison. For example, (x&4) < 8 is always true. |
| 5294 | const APInt &RHSVal = CI->getValue(); |
Chris Lattner | 84dff67 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 5295 | switch (I.getPredicate()) { // LE/GE have been folded already. |
| 5296 | default: assert(0 && "Unknown icmp opcode!"); |
| 5297 | case ICmpInst::ICMP_EQ: |
| 5298 | if (Max.ult(RHSVal) || Min.ugt(RHSVal)) |
| 5299 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
| 5300 | break; |
| 5301 | case ICmpInst::ICMP_NE: |
| 5302 | if (Max.ult(RHSVal) || Min.ugt(RHSVal)) |
| 5303 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
| 5304 | break; |
| 5305 | case ICmpInst::ICMP_ULT: |
Chris Lattner | 183661e | 2008-07-11 05:40:05 +0000 | [diff] [blame^] | 5306 | if (Max.ult(RHSVal)) // A <u C -> true iff max(A) < C |
Chris Lattner | 84dff67 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 5307 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | 183661e | 2008-07-11 05:40:05 +0000 | [diff] [blame^] | 5308 | if (Min.uge(RHSVal)) // A <u C -> false iff min(A) >= C |
Chris Lattner | 84dff67 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 5309 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Chris Lattner | 183661e | 2008-07-11 05:40:05 +0000 | [diff] [blame^] | 5310 | if (RHSVal == Max) // A <u MAX -> A != MAX |
| 5311 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
| 5312 | if (RHSVal == Min+1) // A <u MIN+1 -> A == MIN |
| 5313 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, SubOne(CI)); |
| 5314 | |
| 5315 | // (x <u 2147483648) -> (x >s -1) -> true if sign bit clear |
| 5316 | if (CI->isMinValue(true)) |
| 5317 | return new ICmpInst(ICmpInst::ICMP_SGT, Op0, |
| 5318 | ConstantInt::getAllOnesValue(Op0->getType())); |
Chris Lattner | 84dff67 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 5319 | break; |
| 5320 | case ICmpInst::ICMP_UGT: |
Chris Lattner | 183661e | 2008-07-11 05:40:05 +0000 | [diff] [blame^] | 5321 | if (Min.ugt(RHSVal)) // A >u C -> true iff min(A) > C |
Chris Lattner | 84dff67 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 5322 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | 183661e | 2008-07-11 05:40:05 +0000 | [diff] [blame^] | 5323 | if (Max.ule(RHSVal)) // A >u C -> false iff max(A) <= C |
Chris Lattner | 84dff67 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 5324 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Chris Lattner | 183661e | 2008-07-11 05:40:05 +0000 | [diff] [blame^] | 5325 | |
| 5326 | if (RHSVal == Min) // A >u MIN -> A != MIN |
| 5327 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
| 5328 | if (RHSVal == Max-1) // A >u MAX-1 -> A == MAX |
| 5329 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, AddOne(CI)); |
| 5330 | |
| 5331 | // (x >u 2147483647) -> (x <s 0) -> true if sign bit set |
| 5332 | if (CI->isMaxValue(true)) |
| 5333 | return new ICmpInst(ICmpInst::ICMP_SLT, Op0, |
| 5334 | ConstantInt::getNullValue(Op0->getType())); |
Chris Lattner | 84dff67 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 5335 | break; |
| 5336 | case ICmpInst::ICMP_SLT: |
Chris Lattner | 183661e | 2008-07-11 05:40:05 +0000 | [diff] [blame^] | 5337 | if (Max.slt(RHSVal)) // A <s C -> true iff max(A) < C |
Chris Lattner | 84dff67 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 5338 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | 183661e | 2008-07-11 05:40:05 +0000 | [diff] [blame^] | 5339 | if (Min.sgt(RHSVal)) // A <s C -> false iff min(A) >= C |
Chris Lattner | 84dff67 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 5340 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Chris Lattner | 183661e | 2008-07-11 05:40:05 +0000 | [diff] [blame^] | 5341 | if (RHSVal == Max) // A <s MAX -> A != MAX |
| 5342 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
| 5343 | if (RHSVal == Min-1) // A <s MIN+1 -> A == MIN |
Chris Lattner | 84dff67 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 5344 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
| 5345 | break; |
| 5346 | case ICmpInst::ICMP_SGT: |
Chris Lattner | 183661e | 2008-07-11 05:40:05 +0000 | [diff] [blame^] | 5347 | if (Min.sgt(RHSVal)) // A >s C -> true iff min(A) > C |
Chris Lattner | 84dff67 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 5348 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | 183661e | 2008-07-11 05:40:05 +0000 | [diff] [blame^] | 5349 | if (Max.sle(RHSVal)) // A >s C -> false iff max(A) <= C |
Chris Lattner | 84dff67 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 5350 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Chris Lattner | 183661e | 2008-07-11 05:40:05 +0000 | [diff] [blame^] | 5351 | |
| 5352 | if (RHSVal == Min) // A >s MIN -> A != MIN |
| 5353 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
| 5354 | if (RHSVal == Max-1) // A >s MAX-1 -> A == MAX |
| 5355 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, AddOne(CI)); |
Chris Lattner | 84dff67 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 5356 | break; |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 5357 | } |
| 5358 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5359 | // Since the RHS is a ConstantInt (CI), if the left hand side is an |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 5360 | // instruction, see if that instruction also has constants so that the |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5361 | // instruction can be folded into the icmp |
Chris Lattner | 3c6a0d4 | 2004-05-25 06:32:08 +0000 | [diff] [blame] | 5362 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5363 | if (Instruction *Res = visitICmpInstWithInstAndIntCst(I, LHSI, CI)) |
| 5364 | return Res; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5365 | } |
| 5366 | |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5367 | // Handle icmp with constant (but not simple integer constant) RHS |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5368 | if (Constant *RHSC = dyn_cast<Constant>(Op1)) { |
| 5369 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
| 5370 | switch (LHSI->getOpcode()) { |
Chris Lattner | 9fb25db | 2005-05-01 04:42:15 +0000 | [diff] [blame] | 5371 | case Instruction::GetElementPtr: |
| 5372 | if (RHSC->isNullValue()) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5373 | // icmp pred GEP (P, int 0, int 0, int 0), null -> icmp pred P, null |
Chris Lattner | 9fb25db | 2005-05-01 04:42:15 +0000 | [diff] [blame] | 5374 | bool isAllZeros = true; |
| 5375 | for (unsigned i = 1, e = LHSI->getNumOperands(); i != e; ++i) |
| 5376 | if (!isa<Constant>(LHSI->getOperand(i)) || |
| 5377 | !cast<Constant>(LHSI->getOperand(i))->isNullValue()) { |
| 5378 | isAllZeros = false; |
| 5379 | break; |
| 5380 | } |
| 5381 | if (isAllZeros) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5382 | return new ICmpInst(I.getPredicate(), LHSI->getOperand(0), |
Chris Lattner | 9fb25db | 2005-05-01 04:42:15 +0000 | [diff] [blame] | 5383 | Constant::getNullValue(LHSI->getOperand(0)->getType())); |
| 5384 | } |
| 5385 | break; |
| 5386 | |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5387 | case Instruction::PHI: |
Chris Lattner | 7d8ab4e | 2008-06-08 20:52:11 +0000 | [diff] [blame] | 5388 | // Only fold icmp into the PHI if the phi and fcmp are in the same |
| 5389 | // block. If in the same block, we're encouraging jump threading. If |
| 5390 | // not, we are just pessimizing the code by making an i1 phi. |
| 5391 | if (LHSI->getParent() == I.getParent()) |
| 5392 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 5393 | return NV; |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5394 | break; |
Chris Lattner | 4802d90 | 2007-04-06 18:57:34 +0000 | [diff] [blame] | 5395 | case Instruction::Select: { |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5396 | // If either operand of the select is a constant, we can fold the |
| 5397 | // comparison into the select arms, which will cause one to be |
| 5398 | // constant folded and the select turned into a bitwise or. |
| 5399 | Value *Op1 = 0, *Op2 = 0; |
| 5400 | if (LHSI->hasOneUse()) { |
| 5401 | if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) { |
| 5402 | // Fold the known value into the constant operand. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5403 | Op1 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC); |
| 5404 | // Insert a new ICmp of the other select operand. |
| 5405 | Op2 = InsertNewInstBefore(new ICmpInst(I.getPredicate(), |
| 5406 | LHSI->getOperand(2), RHSC, |
| 5407 | I.getName()), I); |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5408 | } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) { |
| 5409 | // Fold the known value into the constant operand. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5410 | Op2 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC); |
| 5411 | // Insert a new ICmp of the other select operand. |
| 5412 | Op1 = InsertNewInstBefore(new ICmpInst(I.getPredicate(), |
| 5413 | LHSI->getOperand(1), RHSC, |
| 5414 | I.getName()), I); |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5415 | } |
| 5416 | } |
Jeff Cohen | 9d80930 | 2005-04-23 21:38:35 +0000 | [diff] [blame] | 5417 | |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5418 | if (Op1) |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 5419 | return SelectInst::Create(LHSI->getOperand(0), Op1, Op2); |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5420 | break; |
| 5421 | } |
Chris Lattner | 4802d90 | 2007-04-06 18:57:34 +0000 | [diff] [blame] | 5422 | case Instruction::Malloc: |
| 5423 | // If we have (malloc != null), and if the malloc has a single use, we |
| 5424 | // can assume it is successful and remove the malloc. |
| 5425 | if (LHSI->hasOneUse() && isa<ConstantPointerNull>(RHSC)) { |
| 5426 | AddToWorkList(LHSI); |
| 5427 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
Nick Lewycky | fc1efbb | 2008-05-17 07:33:39 +0000 | [diff] [blame] | 5428 | !I.isTrueWhenEqual())); |
Chris Lattner | 4802d90 | 2007-04-06 18:57:34 +0000 | [diff] [blame] | 5429 | } |
| 5430 | break; |
| 5431 | } |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5432 | } |
| 5433 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5434 | // If we can optimize a 'icmp GEP, P' or 'icmp P, GEP', do so now. |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5435 | if (User *GEP = dyn_castGetElementPtr(Op0)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5436 | if (Instruction *NI = FoldGEPICmp(GEP, Op1, I.getPredicate(), I)) |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5437 | return NI; |
| 5438 | if (User *GEP = dyn_castGetElementPtr(Op1)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5439 | if (Instruction *NI = FoldGEPICmp(GEP, Op0, |
| 5440 | ICmpInst::getSwappedPredicate(I.getPredicate()), I)) |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5441 | return NI; |
| 5442 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5443 | // Test to see if the operands of the icmp are casted versions of other |
Chris Lattner | 57d8637 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 5444 | // values. If the ptr->ptr cast can be stripped off both arguments, we do so |
| 5445 | // now. |
| 5446 | if (BitCastInst *CI = dyn_cast<BitCastInst>(Op0)) { |
| 5447 | if (isa<PointerType>(Op0->getType()) && |
| 5448 | (isa<Constant>(Op1) || isa<BitCastInst>(Op1))) { |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5449 | // We keep moving the cast from the left operand over to the right |
| 5450 | // operand, where it can often be eliminated completely. |
Chris Lattner | 57d8637 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 5451 | Op0 = CI->getOperand(0); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5452 | |
Chris Lattner | 57d8637 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 5453 | // If operand #1 is a bitcast instruction, it must also be a ptr->ptr cast |
| 5454 | // so eliminate it as well. |
| 5455 | if (BitCastInst *CI2 = dyn_cast<BitCastInst>(Op1)) |
| 5456 | Op1 = CI2->getOperand(0); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5457 | |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5458 | // If Op1 is a constant, we can fold the cast into the constant. |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 5459 | if (Op0->getType() != Op1->getType()) { |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5460 | if (Constant *Op1C = dyn_cast<Constant>(Op1)) { |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 5461 | Op1 = ConstantExpr::getBitCast(Op1C, Op0->getType()); |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5462 | } else { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5463 | // Otherwise, cast the RHS right before the icmp |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 5464 | Op1 = InsertBitCastBefore(Op1, Op0->getType(), I); |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5465 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 5466 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5467 | return new ICmpInst(I.getPredicate(), Op0, Op1); |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5468 | } |
Chris Lattner | 57d8637 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 5469 | } |
| 5470 | |
| 5471 | if (isa<CastInst>(Op0)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5472 | // Handle the special case of: icmp (cast bool to X), <cst> |
Chris Lattner | 6870805 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 5473 | // This comes up when you have code like |
| 5474 | // int X = A < B; |
| 5475 | // if (X) ... |
| 5476 | // For generality, we handle any zero-extension of any operand comparison |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5477 | // with a constant or another cast from the same type. |
| 5478 | if (isa<ConstantInt>(Op1) || isa<CastInst>(Op1)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5479 | if (Instruction *R = visitICmpInstWithCastAndCast(I)) |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5480 | return R; |
Chris Lattner | 6870805 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 5481 | } |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5482 | |
Chris Lattner | 7d2cbd2 | 2008-05-09 05:19:28 +0000 | [diff] [blame] | 5483 | // ~x < ~y --> y < x |
| 5484 | { Value *A, *B; |
| 5485 | if (match(Op0, m_Not(m_Value(A))) && |
| 5486 | match(Op1, m_Not(m_Value(B)))) |
| 5487 | return new ICmpInst(I.getPredicate(), B, A); |
| 5488 | } |
| 5489 | |
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 5490 | if (I.isEquality()) { |
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5491 | Value *A, *B, *C, *D; |
Chris Lattner | 7d2cbd2 | 2008-05-09 05:19:28 +0000 | [diff] [blame] | 5492 | |
| 5493 | // -x == -y --> x == y |
| 5494 | if (match(Op0, m_Neg(m_Value(A))) && |
| 5495 | match(Op1, m_Neg(m_Value(B)))) |
| 5496 | return new ICmpInst(I.getPredicate(), A, B); |
| 5497 | |
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5498 | if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) { |
| 5499 | if (A == Op1 || B == Op1) { // (A^B) == A -> B == 0 |
| 5500 | Value *OtherVal = A == Op1 ? B : A; |
| 5501 | return new ICmpInst(I.getPredicate(), OtherVal, |
| 5502 | Constant::getNullValue(A->getType())); |
| 5503 | } |
| 5504 | |
| 5505 | if (match(Op1, m_Xor(m_Value(C), m_Value(D)))) { |
| 5506 | // A^c1 == C^c2 --> A == C^(c1^c2) |
| 5507 | if (ConstantInt *C1 = dyn_cast<ConstantInt>(B)) |
| 5508 | if (ConstantInt *C2 = dyn_cast<ConstantInt>(D)) |
| 5509 | if (Op1->hasOneUse()) { |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 5510 | Constant *NC = ConstantInt::get(C1->getValue() ^ C2->getValue()); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5511 | Instruction *Xor = BinaryOperator::CreateXor(C, NC, "tmp"); |
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5512 | return new ICmpInst(I.getPredicate(), A, |
| 5513 | InsertNewInstBefore(Xor, I)); |
| 5514 | } |
| 5515 | |
| 5516 | // A^B == A^D -> B == D |
| 5517 | if (A == C) return new ICmpInst(I.getPredicate(), B, D); |
| 5518 | if (A == D) return new ICmpInst(I.getPredicate(), B, C); |
| 5519 | if (B == C) return new ICmpInst(I.getPredicate(), A, D); |
| 5520 | if (B == D) return new ICmpInst(I.getPredicate(), A, C); |
| 5521 | } |
| 5522 | } |
| 5523 | |
| 5524 | if (match(Op1, m_Xor(m_Value(A), m_Value(B))) && |
| 5525 | (A == Op0 || B == Op0)) { |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5526 | // A == (A^B) -> B == 0 |
| 5527 | Value *OtherVal = A == Op0 ? B : A; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5528 | return new ICmpInst(I.getPredicate(), OtherVal, |
| 5529 | Constant::getNullValue(A->getType())); |
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5530 | } |
| 5531 | if (match(Op0, m_Sub(m_Value(A), m_Value(B))) && A == Op1) { |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5532 | // (A-B) == A -> B == 0 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5533 | return new ICmpInst(I.getPredicate(), B, |
| 5534 | Constant::getNullValue(B->getType())); |
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5535 | } |
| 5536 | if (match(Op1, m_Sub(m_Value(A), m_Value(B))) && A == Op0) { |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5537 | // A == (A-B) -> B == 0 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5538 | return new ICmpInst(I.getPredicate(), B, |
| 5539 | Constant::getNullValue(B->getType())); |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5540 | } |
Chris Lattner | 9c2328e | 2006-11-14 06:06:06 +0000 | [diff] [blame] | 5541 | |
Chris Lattner | 9c2328e | 2006-11-14 06:06:06 +0000 | [diff] [blame] | 5542 | // (X&Z) == (Y&Z) -> (X^Y) & Z == 0 |
| 5543 | if (Op0->hasOneUse() && Op1->hasOneUse() && |
| 5544 | match(Op0, m_And(m_Value(A), m_Value(B))) && |
| 5545 | match(Op1, m_And(m_Value(C), m_Value(D)))) { |
| 5546 | Value *X = 0, *Y = 0, *Z = 0; |
| 5547 | |
| 5548 | if (A == C) { |
| 5549 | X = B; Y = D; Z = A; |
| 5550 | } else if (A == D) { |
| 5551 | X = B; Y = C; Z = A; |
| 5552 | } else if (B == C) { |
| 5553 | X = A; Y = D; Z = B; |
| 5554 | } else if (B == D) { |
| 5555 | X = A; Y = C; Z = B; |
| 5556 | } |
| 5557 | |
| 5558 | if (X) { // Build (X^Y) & Z |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5559 | Op1 = InsertNewInstBefore(BinaryOperator::CreateXor(X, Y, "tmp"), I); |
| 5560 | Op1 = InsertNewInstBefore(BinaryOperator::CreateAnd(Op1, Z, "tmp"), I); |
Chris Lattner | 9c2328e | 2006-11-14 06:06:06 +0000 | [diff] [blame] | 5561 | I.setOperand(0, Op1); |
| 5562 | I.setOperand(1, Constant::getNullValue(Op1->getType())); |
| 5563 | return &I; |
| 5564 | } |
| 5565 | } |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5566 | } |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 5567 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5568 | } |
| 5569 | |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5570 | |
| 5571 | /// FoldICmpDivCst - Fold "icmp pred, ([su]div X, DivRHS), CmpRHS" where DivRHS |
| 5572 | /// and CmpRHS are both known to be integer constants. |
| 5573 | Instruction *InstCombiner::FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI, |
| 5574 | ConstantInt *DivRHS) { |
| 5575 | ConstantInt *CmpRHS = cast<ConstantInt>(ICI.getOperand(1)); |
| 5576 | const APInt &CmpRHSV = CmpRHS->getValue(); |
| 5577 | |
| 5578 | // FIXME: If the operand types don't match the type of the divide |
| 5579 | // then don't attempt this transform. The code below doesn't have the |
| 5580 | // logic to deal with a signed divide and an unsigned compare (and |
| 5581 | // vice versa). This is because (x /s C1) <s C2 produces different |
| 5582 | // results than (x /s C1) <u C2 or (x /u C1) <s C2 or even |
| 5583 | // (x /u C1) <u C2. Simply casting the operands and result won't |
| 5584 | // work. :( The if statement below tests that condition and bails |
| 5585 | // if it finds it. |
| 5586 | bool DivIsSigned = DivI->getOpcode() == Instruction::SDiv; |
| 5587 | if (!ICI.isEquality() && DivIsSigned != ICI.isSignedPredicate()) |
| 5588 | return 0; |
| 5589 | if (DivRHS->isZero()) |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5590 | return 0; // The ProdOV computation fails on divide by zero. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5591 | |
| 5592 | // Compute Prod = CI * DivRHS. We are essentially solving an equation |
| 5593 | // of form X/C1=C2. We solve for X by multiplying C1 (DivRHS) and |
| 5594 | // C2 (CI). By solving for X we can turn this into a range check |
| 5595 | // instead of computing a divide. |
| 5596 | ConstantInt *Prod = Multiply(CmpRHS, DivRHS); |
| 5597 | |
| 5598 | // Determine if the product overflows by seeing if the product is |
| 5599 | // not equal to the divide. Make sure we do the same kind of divide |
| 5600 | // as in the LHS instruction that we're folding. |
| 5601 | bool ProdOV = (DivIsSigned ? ConstantExpr::getSDiv(Prod, DivRHS) : |
| 5602 | ConstantExpr::getUDiv(Prod, DivRHS)) != CmpRHS; |
| 5603 | |
| 5604 | // Get the ICmp opcode |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5605 | ICmpInst::Predicate Pred = ICI.getPredicate(); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5606 | |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5607 | // Figure out the interval that is being checked. For example, a comparison |
| 5608 | // like "X /u 5 == 0" is really checking that X is in the interval [0, 5). |
| 5609 | // Compute this interval based on the constants involved and the signedness of |
| 5610 | // the compare/divide. This computes a half-open interval, keeping track of |
| 5611 | // whether either value in the interval overflows. After analysis each |
| 5612 | // overflow variable is set to 0 if it's corresponding bound variable is valid |
| 5613 | // -1 if overflowed off the bottom end, or +1 if overflowed off the top end. |
| 5614 | int LoOverflow = 0, HiOverflow = 0; |
| 5615 | ConstantInt *LoBound = 0, *HiBound = 0; |
| 5616 | |
| 5617 | |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5618 | if (!DivIsSigned) { // udiv |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5619 | // e.g. X/5 op 3 --> [15, 20) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5620 | LoBound = Prod; |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5621 | HiOverflow = LoOverflow = ProdOV; |
| 5622 | if (!HiOverflow) |
| 5623 | HiOverflow = AddWithOverflow(HiBound, LoBound, DivRHS, false); |
Dan Gohman | 7649127 | 2008-02-13 22:09:18 +0000 | [diff] [blame] | 5624 | } else if (DivRHS->getValue().isStrictlyPositive()) { // Divisor is > 0. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5625 | if (CmpRHSV == 0) { // (X / pos) op 0 |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5626 | // Can't overflow. e.g. X/2 op 0 --> [-1, 2) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5627 | LoBound = cast<ConstantInt>(ConstantExpr::getNeg(SubOne(DivRHS))); |
| 5628 | HiBound = DivRHS; |
Dan Gohman | 7649127 | 2008-02-13 22:09:18 +0000 | [diff] [blame] | 5629 | } else if (CmpRHSV.isStrictlyPositive()) { // (X / pos) op pos |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5630 | LoBound = Prod; // e.g. X/5 op 3 --> [15, 20) |
| 5631 | HiOverflow = LoOverflow = ProdOV; |
| 5632 | if (!HiOverflow) |
| 5633 | HiOverflow = AddWithOverflow(HiBound, Prod, DivRHS, true); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5634 | } else { // (X / pos) op neg |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5635 | // e.g. X/5 op -3 --> [-15-4, -15+1) --> [-19, -14) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5636 | Constant *DivRHSH = ConstantExpr::getNeg(SubOne(DivRHS)); |
| 5637 | LoOverflow = AddWithOverflow(LoBound, Prod, |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5638 | cast<ConstantInt>(DivRHSH), true) ? -1 : 0; |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5639 | HiBound = AddOne(Prod); |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5640 | HiOverflow = ProdOV ? -1 : 0; |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5641 | } |
Dan Gohman | 7649127 | 2008-02-13 22:09:18 +0000 | [diff] [blame] | 5642 | } else if (DivRHS->getValue().isNegative()) { // Divisor is < 0. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5643 | if (CmpRHSV == 0) { // (X / neg) op 0 |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5644 | // e.g. X/-5 op 0 --> [-4, 5) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5645 | LoBound = AddOne(DivRHS); |
| 5646 | HiBound = cast<ConstantInt>(ConstantExpr::getNeg(DivRHS)); |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5647 | if (HiBound == DivRHS) { // -INTMIN = INTMIN |
| 5648 | HiOverflow = 1; // [INTMIN+1, overflow) |
| 5649 | HiBound = 0; // e.g. X/INTMIN = 0 --> X > INTMIN |
| 5650 | } |
Dan Gohman | 7649127 | 2008-02-13 22:09:18 +0000 | [diff] [blame] | 5651 | } else if (CmpRHSV.isStrictlyPositive()) { // (X / neg) op pos |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5652 | // e.g. X/-5 op 3 --> [-19, -14) |
| 5653 | HiOverflow = LoOverflow = ProdOV ? -1 : 0; |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5654 | if (!LoOverflow) |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5655 | LoOverflow = AddWithOverflow(LoBound, Prod, AddOne(DivRHS), true) ?-1:0; |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5656 | HiBound = AddOne(Prod); |
| 5657 | } else { // (X / neg) op neg |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5658 | // e.g. X/-5 op -3 --> [15, 20) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5659 | LoBound = Prod; |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5660 | LoOverflow = HiOverflow = ProdOV ? 1 : 0; |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5661 | HiBound = Subtract(Prod, DivRHS); |
| 5662 | } |
| 5663 | |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5664 | // Dividing by a negative swaps the condition. LT <-> GT |
| 5665 | Pred = ICmpInst::getSwappedPredicate(Pred); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5666 | } |
| 5667 | |
| 5668 | Value *X = DivI->getOperand(0); |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5669 | switch (Pred) { |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5670 | default: assert(0 && "Unhandled icmp opcode!"); |
| 5671 | case ICmpInst::ICMP_EQ: |
| 5672 | if (LoOverflow && HiOverflow) |
| 5673 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
| 5674 | else if (HiOverflow) |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5675 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5676 | ICmpInst::ICMP_UGE, X, LoBound); |
| 5677 | else if (LoOverflow) |
| 5678 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : |
| 5679 | ICmpInst::ICMP_ULT, X, HiBound); |
| 5680 | else |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5681 | return InsertRangeTest(X, LoBound, HiBound, DivIsSigned, true, ICI); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5682 | case ICmpInst::ICMP_NE: |
| 5683 | if (LoOverflow && HiOverflow) |
| 5684 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); |
| 5685 | else if (HiOverflow) |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5686 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5687 | ICmpInst::ICMP_ULT, X, LoBound); |
| 5688 | else if (LoOverflow) |
| 5689 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : |
| 5690 | ICmpInst::ICMP_UGE, X, HiBound); |
| 5691 | else |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5692 | return InsertRangeTest(X, LoBound, HiBound, DivIsSigned, false, ICI); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5693 | case ICmpInst::ICMP_ULT: |
| 5694 | case ICmpInst::ICMP_SLT: |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5695 | if (LoOverflow == +1) // Low bound is greater than input range. |
| 5696 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); |
| 5697 | if (LoOverflow == -1) // Low bound is less than input range. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5698 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5699 | return new ICmpInst(Pred, X, LoBound); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5700 | case ICmpInst::ICMP_UGT: |
| 5701 | case ICmpInst::ICMP_SGT: |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5702 | if (HiOverflow == +1) // High bound greater than input range. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5703 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5704 | else if (HiOverflow == -1) // High bound less than input range. |
| 5705 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); |
| 5706 | if (Pred == ICmpInst::ICMP_UGT) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5707 | return new ICmpInst(ICmpInst::ICMP_UGE, X, HiBound); |
| 5708 | else |
| 5709 | return new ICmpInst(ICmpInst::ICMP_SGE, X, HiBound); |
| 5710 | } |
| 5711 | } |
| 5712 | |
| 5713 | |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5714 | /// visitICmpInstWithInstAndIntCst - Handle "icmp (instr, intcst)". |
| 5715 | /// |
| 5716 | Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI, |
| 5717 | Instruction *LHSI, |
| 5718 | ConstantInt *RHS) { |
| 5719 | const APInt &RHSV = RHS->getValue(); |
| 5720 | |
| 5721 | switch (LHSI->getOpcode()) { |
Duncan Sands | 0091bf2 | 2007-04-04 06:42:45 +0000 | [diff] [blame] | 5722 | case Instruction::Xor: // (icmp pred (xor X, XorCST), CI) |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5723 | if (ConstantInt *XorCST = dyn_cast<ConstantInt>(LHSI->getOperand(1))) { |
| 5724 | // If this is a comparison that tests the signbit (X < 0) or (x > -1), |
| 5725 | // fold the xor. |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 5726 | if ((ICI.getPredicate() == ICmpInst::ICMP_SLT && RHSV == 0) || |
| 5727 | (ICI.getPredicate() == ICmpInst::ICMP_SGT && RHSV.isAllOnesValue())) { |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5728 | Value *CompareVal = LHSI->getOperand(0); |
| 5729 | |
| 5730 | // If the sign bit of the XorCST is not set, there is no change to |
| 5731 | // the operation, just stop using the Xor. |
| 5732 | if (!XorCST->getValue().isNegative()) { |
| 5733 | ICI.setOperand(0, CompareVal); |
| 5734 | AddToWorkList(LHSI); |
| 5735 | return &ICI; |
| 5736 | } |
| 5737 | |
| 5738 | // Was the old condition true if the operand is positive? |
| 5739 | bool isTrueIfPositive = ICI.getPredicate() == ICmpInst::ICMP_SGT; |
| 5740 | |
| 5741 | // If so, the new one isn't. |
| 5742 | isTrueIfPositive ^= true; |
| 5743 | |
| 5744 | if (isTrueIfPositive) |
| 5745 | return new ICmpInst(ICmpInst::ICMP_SGT, CompareVal, SubOne(RHS)); |
| 5746 | else |
| 5747 | return new ICmpInst(ICmpInst::ICMP_SLT, CompareVal, AddOne(RHS)); |
| 5748 | } |
| 5749 | } |
| 5750 | break; |
| 5751 | case Instruction::And: // (icmp pred (and X, AndCST), RHS) |
| 5752 | if (LHSI->hasOneUse() && isa<ConstantInt>(LHSI->getOperand(1)) && |
| 5753 | LHSI->getOperand(0)->hasOneUse()) { |
| 5754 | ConstantInt *AndCST = cast<ConstantInt>(LHSI->getOperand(1)); |
| 5755 | |
| 5756 | // If the LHS is an AND of a truncating cast, we can widen the |
| 5757 | // and/compare to be the input width without changing the value |
| 5758 | // produced, eliminating a cast. |
| 5759 | if (TruncInst *Cast = dyn_cast<TruncInst>(LHSI->getOperand(0))) { |
| 5760 | // We can do this transformation if either the AND constant does not |
| 5761 | // have its sign bit set or if it is an equality comparison. |
| 5762 | // Extending a relational comparison when we're checking the sign |
| 5763 | // bit would not work. |
| 5764 | if (Cast->hasOneUse() && |
Anton Korobeynikov | 4aefd6b | 2008-02-20 12:07:57 +0000 | [diff] [blame] | 5765 | (ICI.isEquality() || |
| 5766 | (AndCST->getValue().isNonNegative() && RHSV.isNonNegative()))) { |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5767 | uint32_t BitWidth = |
| 5768 | cast<IntegerType>(Cast->getOperand(0)->getType())->getBitWidth(); |
| 5769 | APInt NewCST = AndCST->getValue(); |
| 5770 | NewCST.zext(BitWidth); |
| 5771 | APInt NewCI = RHSV; |
| 5772 | NewCI.zext(BitWidth); |
| 5773 | Instruction *NewAnd = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5774 | BinaryOperator::CreateAnd(Cast->getOperand(0), |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5775 | ConstantInt::get(NewCST),LHSI->getName()); |
| 5776 | InsertNewInstBefore(NewAnd, ICI); |
| 5777 | return new ICmpInst(ICI.getPredicate(), NewAnd, |
| 5778 | ConstantInt::get(NewCI)); |
| 5779 | } |
| 5780 | } |
| 5781 | |
| 5782 | // If this is: (X >> C1) & C2 != C3 (where any shift and any compare |
| 5783 | // could exist), turn it into (X & (C2 << C1)) != (C3 << C1). This |
| 5784 | // happens a LOT in code produced by the C front-end, for bitfield |
| 5785 | // access. |
| 5786 | BinaryOperator *Shift = dyn_cast<BinaryOperator>(LHSI->getOperand(0)); |
| 5787 | if (Shift && !Shift->isShift()) |
| 5788 | Shift = 0; |
| 5789 | |
| 5790 | ConstantInt *ShAmt; |
| 5791 | ShAmt = Shift ? dyn_cast<ConstantInt>(Shift->getOperand(1)) : 0; |
| 5792 | const Type *Ty = Shift ? Shift->getType() : 0; // Type of the shift. |
| 5793 | const Type *AndTy = AndCST->getType(); // Type of the and. |
| 5794 | |
| 5795 | // We can fold this as long as we can't shift unknown bits |
| 5796 | // into the mask. This can only happen with signed shift |
| 5797 | // rights, as they sign-extend. |
| 5798 | if (ShAmt) { |
| 5799 | bool CanFold = Shift->isLogicalShift(); |
| 5800 | if (!CanFold) { |
| 5801 | // To test for the bad case of the signed shr, see if any |
| 5802 | // of the bits shifted in could be tested after the mask. |
| 5803 | uint32_t TyBits = Ty->getPrimitiveSizeInBits(); |
| 5804 | int ShAmtVal = TyBits - ShAmt->getLimitedValue(TyBits); |
| 5805 | |
| 5806 | uint32_t BitWidth = AndTy->getPrimitiveSizeInBits(); |
| 5807 | if ((APInt::getHighBitsSet(BitWidth, BitWidth-ShAmtVal) & |
| 5808 | AndCST->getValue()) == 0) |
| 5809 | CanFold = true; |
| 5810 | } |
| 5811 | |
| 5812 | if (CanFold) { |
| 5813 | Constant *NewCst; |
| 5814 | if (Shift->getOpcode() == Instruction::Shl) |
| 5815 | NewCst = ConstantExpr::getLShr(RHS, ShAmt); |
| 5816 | else |
| 5817 | NewCst = ConstantExpr::getShl(RHS, ShAmt); |
| 5818 | |
| 5819 | // Check to see if we are shifting out any of the bits being |
| 5820 | // compared. |
| 5821 | if (ConstantExpr::get(Shift->getOpcode(), NewCst, ShAmt) != RHS) { |
| 5822 | // If we shifted bits out, the fold is not going to work out. |
| 5823 | // As a special case, check to see if this means that the |
| 5824 | // result is always true or false now. |
| 5825 | if (ICI.getPredicate() == ICmpInst::ICMP_EQ) |
| 5826 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
| 5827 | if (ICI.getPredicate() == ICmpInst::ICMP_NE) |
| 5828 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); |
| 5829 | } else { |
| 5830 | ICI.setOperand(1, NewCst); |
| 5831 | Constant *NewAndCST; |
| 5832 | if (Shift->getOpcode() == Instruction::Shl) |
| 5833 | NewAndCST = ConstantExpr::getLShr(AndCST, ShAmt); |
| 5834 | else |
| 5835 | NewAndCST = ConstantExpr::getShl(AndCST, ShAmt); |
| 5836 | LHSI->setOperand(1, NewAndCST); |
| 5837 | LHSI->setOperand(0, Shift->getOperand(0)); |
| 5838 | AddToWorkList(Shift); // Shift is dead. |
| 5839 | AddUsesToWorkList(ICI); |
| 5840 | return &ICI; |
| 5841 | } |
| 5842 | } |
| 5843 | } |
| 5844 | |
| 5845 | // Turn ((X >> Y) & C) == 0 into (X & (C << Y)) == 0. The later is |
| 5846 | // preferable because it allows the C<<Y expression to be hoisted out |
| 5847 | // of a loop if Y is invariant and X is not. |
| 5848 | if (Shift && Shift->hasOneUse() && RHSV == 0 && |
| 5849 | ICI.isEquality() && !Shift->isArithmeticShift() && |
| 5850 | isa<Instruction>(Shift->getOperand(0))) { |
| 5851 | // Compute C << Y. |
| 5852 | Value *NS; |
| 5853 | if (Shift->getOpcode() == Instruction::LShr) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5854 | NS = BinaryOperator::CreateShl(AndCST, |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5855 | Shift->getOperand(1), "tmp"); |
| 5856 | } else { |
| 5857 | // Insert a logical shift. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5858 | NS = BinaryOperator::CreateLShr(AndCST, |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5859 | Shift->getOperand(1), "tmp"); |
| 5860 | } |
| 5861 | InsertNewInstBefore(cast<Instruction>(NS), ICI); |
| 5862 | |
| 5863 | // Compute X & (C << Y). |
| 5864 | Instruction *NewAnd = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5865 | BinaryOperator::CreateAnd(Shift->getOperand(0), NS, LHSI->getName()); |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5866 | InsertNewInstBefore(NewAnd, ICI); |
| 5867 | |
| 5868 | ICI.setOperand(0, NewAnd); |
| 5869 | return &ICI; |
| 5870 | } |
| 5871 | } |
| 5872 | break; |
| 5873 | |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 5874 | case Instruction::Shl: { // (icmp pred (shl X, ShAmt), CI) |
| 5875 | ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1)); |
| 5876 | if (!ShAmt) break; |
| 5877 | |
| 5878 | uint32_t TypeBits = RHSV.getBitWidth(); |
| 5879 | |
| 5880 | // Check that the shift amount is in range. If not, don't perform |
| 5881 | // undefined shifts. When the shift is visited it will be |
| 5882 | // simplified. |
| 5883 | if (ShAmt->uge(TypeBits)) |
| 5884 | break; |
| 5885 | |
| 5886 | if (ICI.isEquality()) { |
| 5887 | // If we are comparing against bits always shifted out, the |
| 5888 | // comparison cannot succeed. |
| 5889 | Constant *Comp = |
| 5890 | ConstantExpr::getShl(ConstantExpr::getLShr(RHS, ShAmt), ShAmt); |
| 5891 | if (Comp != RHS) {// Comparing against a bit that we know is zero. |
| 5892 | bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE; |
| 5893 | Constant *Cst = ConstantInt::get(Type::Int1Ty, IsICMP_NE); |
| 5894 | return ReplaceInstUsesWith(ICI, Cst); |
| 5895 | } |
| 5896 | |
| 5897 | if (LHSI->hasOneUse()) { |
| 5898 | // Otherwise strength reduce the shift into an and. |
| 5899 | uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits); |
| 5900 | Constant *Mask = |
| 5901 | ConstantInt::get(APInt::getLowBitsSet(TypeBits, TypeBits-ShAmtVal)); |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5902 | |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 5903 | Instruction *AndI = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5904 | BinaryOperator::CreateAnd(LHSI->getOperand(0), |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 5905 | Mask, LHSI->getName()+".mask"); |
| 5906 | Value *And = InsertNewInstBefore(AndI, ICI); |
| 5907 | return new ICmpInst(ICI.getPredicate(), And, |
| 5908 | ConstantInt::get(RHSV.lshr(ShAmtVal))); |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5909 | } |
| 5910 | } |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 5911 | |
| 5912 | // Otherwise, if this is a comparison of the sign bit, simplify to and/test. |
| 5913 | bool TrueIfSigned = false; |
| 5914 | if (LHSI->hasOneUse() && |
| 5915 | isSignBitCheck(ICI.getPredicate(), RHS, TrueIfSigned)) { |
| 5916 | // (X << 31) <s 0 --> (X&1) != 0 |
| 5917 | Constant *Mask = ConstantInt::get(APInt(TypeBits, 1) << |
| 5918 | (TypeBits-ShAmt->getZExtValue()-1)); |
| 5919 | Instruction *AndI = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5920 | BinaryOperator::CreateAnd(LHSI->getOperand(0), |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 5921 | Mask, LHSI->getName()+".mask"); |
| 5922 | Value *And = InsertNewInstBefore(AndI, ICI); |
| 5923 | |
| 5924 | return new ICmpInst(TrueIfSigned ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ, |
| 5925 | And, Constant::getNullValue(And->getType())); |
| 5926 | } |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5927 | break; |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 5928 | } |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5929 | |
| 5930 | case Instruction::LShr: // (icmp pred (shr X, ShAmt), CI) |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 5931 | case Instruction::AShr: { |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 5932 | // Only handle equality comparisons of shift-by-constant. |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 5933 | ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1)); |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 5934 | if (!ShAmt || !ICI.isEquality()) break; |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 5935 | |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 5936 | // Check that the shift amount is in range. If not, don't perform |
| 5937 | // undefined shifts. When the shift is visited it will be |
| 5938 | // simplified. |
| 5939 | uint32_t TypeBits = RHSV.getBitWidth(); |
| 5940 | if (ShAmt->uge(TypeBits)) |
| 5941 | break; |
| 5942 | |
| 5943 | uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits); |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 5944 | |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 5945 | // If we are comparing against bits always shifted out, the |
| 5946 | // comparison cannot succeed. |
| 5947 | APInt Comp = RHSV << ShAmtVal; |
| 5948 | if (LHSI->getOpcode() == Instruction::LShr) |
| 5949 | Comp = Comp.lshr(ShAmtVal); |
| 5950 | else |
| 5951 | Comp = Comp.ashr(ShAmtVal); |
| 5952 | |
| 5953 | if (Comp != RHSV) { // Comparing against a bit that we know is zero. |
| 5954 | bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE; |
| 5955 | Constant *Cst = ConstantInt::get(Type::Int1Ty, IsICMP_NE); |
| 5956 | return ReplaceInstUsesWith(ICI, Cst); |
| 5957 | } |
| 5958 | |
| 5959 | // Otherwise, check to see if the bits shifted out are known to be zero. |
| 5960 | // If so, we can compare against the unshifted value: |
| 5961 | // (X & 4) >> 1 == 2 --> (X & 4) == 4. |
Evan Cheng | f30752c | 2008-04-23 00:38:06 +0000 | [diff] [blame] | 5962 | if (LHSI->hasOneUse() && |
| 5963 | MaskedValueIsZero(LHSI->getOperand(0), |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 5964 | APInt::getLowBitsSet(Comp.getBitWidth(), ShAmtVal))) { |
| 5965 | return new ICmpInst(ICI.getPredicate(), LHSI->getOperand(0), |
| 5966 | ConstantExpr::getShl(RHS, ShAmt)); |
| 5967 | } |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 5968 | |
Evan Cheng | f30752c | 2008-04-23 00:38:06 +0000 | [diff] [blame] | 5969 | if (LHSI->hasOneUse()) { |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 5970 | // Otherwise strength reduce the shift into an and. |
| 5971 | APInt Val(APInt::getHighBitsSet(TypeBits, TypeBits - ShAmtVal)); |
| 5972 | Constant *Mask = ConstantInt::get(Val); |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 5973 | |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 5974 | Instruction *AndI = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5975 | BinaryOperator::CreateAnd(LHSI->getOperand(0), |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 5976 | Mask, LHSI->getName()+".mask"); |
| 5977 | Value *And = InsertNewInstBefore(AndI, ICI); |
| 5978 | return new ICmpInst(ICI.getPredicate(), And, |
| 5979 | ConstantExpr::getShl(RHS, ShAmt)); |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5980 | } |
| 5981 | break; |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 5982 | } |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5983 | |
| 5984 | case Instruction::SDiv: |
| 5985 | case Instruction::UDiv: |
| 5986 | // Fold: icmp pred ([us]div X, C1), C2 -> range test |
| 5987 | // Fold this div into the comparison, producing a range check. |
| 5988 | // Determine, based on the divide type, what the range is being |
| 5989 | // checked. If there is an overflow on the low or high side, remember |
| 5990 | // it, otherwise compute the range [low, hi) bounding the new value. |
| 5991 | // See: InsertRangeTest above for the kinds of replacements possible. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5992 | if (ConstantInt *DivRHS = dyn_cast<ConstantInt>(LHSI->getOperand(1))) |
| 5993 | if (Instruction *R = FoldICmpDivCst(ICI, cast<BinaryOperator>(LHSI), |
| 5994 | DivRHS)) |
| 5995 | return R; |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5996 | break; |
Nick Lewycky | 5be2920 | 2008-02-03 16:33:09 +0000 | [diff] [blame] | 5997 | |
| 5998 | case Instruction::Add: |
| 5999 | // Fold: icmp pred (add, X, C1), C2 |
| 6000 | |
| 6001 | if (!ICI.isEquality()) { |
| 6002 | ConstantInt *LHSC = dyn_cast<ConstantInt>(LHSI->getOperand(1)); |
| 6003 | if (!LHSC) break; |
| 6004 | const APInt &LHSV = LHSC->getValue(); |
| 6005 | |
| 6006 | ConstantRange CR = ICI.makeConstantRange(ICI.getPredicate(), RHSV) |
| 6007 | .subtract(LHSV); |
| 6008 | |
| 6009 | if (ICI.isSignedPredicate()) { |
| 6010 | if (CR.getLower().isSignBit()) { |
| 6011 | return new ICmpInst(ICmpInst::ICMP_SLT, LHSI->getOperand(0), |
| 6012 | ConstantInt::get(CR.getUpper())); |
| 6013 | } else if (CR.getUpper().isSignBit()) { |
| 6014 | return new ICmpInst(ICmpInst::ICMP_SGE, LHSI->getOperand(0), |
| 6015 | ConstantInt::get(CR.getLower())); |
| 6016 | } |
| 6017 | } else { |
| 6018 | if (CR.getLower().isMinValue()) { |
| 6019 | return new ICmpInst(ICmpInst::ICMP_ULT, LHSI->getOperand(0), |
| 6020 | ConstantInt::get(CR.getUpper())); |
| 6021 | } else if (CR.getUpper().isMinValue()) { |
| 6022 | return new ICmpInst(ICmpInst::ICMP_UGE, LHSI->getOperand(0), |
| 6023 | ConstantInt::get(CR.getLower())); |
| 6024 | } |
| 6025 | } |
| 6026 | } |
| 6027 | break; |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6028 | } |
| 6029 | |
| 6030 | // Simplify icmp_eq and icmp_ne instructions with integer constant RHS. |
| 6031 | if (ICI.isEquality()) { |
| 6032 | bool isICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE; |
| 6033 | |
| 6034 | // If the first operand is (add|sub|and|or|xor|rem) with a constant, and |
| 6035 | // the second operand is a constant, simplify a bit. |
| 6036 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(LHSI)) { |
| 6037 | switch (BO->getOpcode()) { |
| 6038 | case Instruction::SRem: |
| 6039 | // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one. |
| 6040 | if (RHSV == 0 && isa<ConstantInt>(BO->getOperand(1)) &&BO->hasOneUse()){ |
| 6041 | const APInt &V = cast<ConstantInt>(BO->getOperand(1))->getValue(); |
| 6042 | if (V.sgt(APInt(V.getBitWidth(), 1)) && V.isPowerOf2()) { |
| 6043 | Instruction *NewRem = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6044 | BinaryOperator::CreateURem(BO->getOperand(0), BO->getOperand(1), |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6045 | BO->getName()); |
| 6046 | InsertNewInstBefore(NewRem, ICI); |
| 6047 | return new ICmpInst(ICI.getPredicate(), NewRem, |
| 6048 | Constant::getNullValue(BO->getType())); |
| 6049 | } |
| 6050 | } |
| 6051 | break; |
| 6052 | case Instruction::Add: |
| 6053 | // Replace ((add A, B) != C) with (A != C-B) if B & C are constants. |
| 6054 | if (ConstantInt *BOp1C = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
| 6055 | if (BO->hasOneUse()) |
| 6056 | return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), |
| 6057 | Subtract(RHS, BOp1C)); |
| 6058 | } else if (RHSV == 0) { |
| 6059 | // Replace ((add A, B) != 0) with (A != -B) if A or B is |
| 6060 | // efficiently invertible, or if the add has just this one use. |
| 6061 | Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1); |
| 6062 | |
| 6063 | if (Value *NegVal = dyn_castNegVal(BOp1)) |
| 6064 | return new ICmpInst(ICI.getPredicate(), BOp0, NegVal); |
| 6065 | else if (Value *NegVal = dyn_castNegVal(BOp0)) |
| 6066 | return new ICmpInst(ICI.getPredicate(), NegVal, BOp1); |
| 6067 | else if (BO->hasOneUse()) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6068 | Instruction *Neg = BinaryOperator::CreateNeg(BOp1); |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6069 | InsertNewInstBefore(Neg, ICI); |
| 6070 | Neg->takeName(BO); |
| 6071 | return new ICmpInst(ICI.getPredicate(), BOp0, Neg); |
| 6072 | } |
| 6073 | } |
| 6074 | break; |
| 6075 | case Instruction::Xor: |
| 6076 | // For the xor case, we can xor two constants together, eliminating |
| 6077 | // the explicit xor. |
| 6078 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) |
| 6079 | return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), |
| 6080 | ConstantExpr::getXor(RHS, BOC)); |
| 6081 | |
| 6082 | // FALLTHROUGH |
| 6083 | case Instruction::Sub: |
| 6084 | // Replace (([sub|xor] A, B) != 0) with (A != B) |
| 6085 | if (RHSV == 0) |
| 6086 | return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), |
| 6087 | BO->getOperand(1)); |
| 6088 | break; |
| 6089 | |
| 6090 | case Instruction::Or: |
| 6091 | // If bits are being or'd in that are not present in the constant we |
| 6092 | // are comparing against, then the comparison could never succeed! |
| 6093 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) { |
| 6094 | Constant *NotCI = ConstantExpr::getNot(RHS); |
| 6095 | if (!ConstantExpr::getAnd(BOC, NotCI)->isNullValue()) |
| 6096 | return ReplaceInstUsesWith(ICI, ConstantInt::get(Type::Int1Ty, |
| 6097 | isICMP_NE)); |
| 6098 | } |
| 6099 | break; |
| 6100 | |
| 6101 | case Instruction::And: |
| 6102 | if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
| 6103 | // If bits are being compared against that are and'd out, then the |
| 6104 | // comparison can never succeed! |
| 6105 | if ((RHSV & ~BOC->getValue()) != 0) |
| 6106 | return ReplaceInstUsesWith(ICI, ConstantInt::get(Type::Int1Ty, |
| 6107 | isICMP_NE)); |
| 6108 | |
| 6109 | // If we have ((X & C) == C), turn it into ((X & C) != 0). |
| 6110 | if (RHS == BOC && RHSV.isPowerOf2()) |
| 6111 | return new ICmpInst(isICMP_NE ? ICmpInst::ICMP_EQ : |
| 6112 | ICmpInst::ICMP_NE, LHSI, |
| 6113 | Constant::getNullValue(RHS->getType())); |
| 6114 | |
| 6115 | // Replace (and X, (1 << size(X)-1) != 0) with x s< 0 |
Chris Lattner | 833f25d | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 6116 | if (BOC->getValue().isSignBit()) { |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6117 | Value *X = BO->getOperand(0); |
| 6118 | Constant *Zero = Constant::getNullValue(X->getType()); |
| 6119 | ICmpInst::Predicate pred = isICMP_NE ? |
| 6120 | ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGE; |
| 6121 | return new ICmpInst(pred, X, Zero); |
| 6122 | } |
| 6123 | |
| 6124 | // ((X & ~7) == 0) --> X < 8 |
| 6125 | if (RHSV == 0 && isHighOnes(BOC)) { |
| 6126 | Value *X = BO->getOperand(0); |
| 6127 | Constant *NegX = ConstantExpr::getNeg(BOC); |
| 6128 | ICmpInst::Predicate pred = isICMP_NE ? |
| 6129 | ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT; |
| 6130 | return new ICmpInst(pred, X, NegX); |
| 6131 | } |
| 6132 | } |
| 6133 | default: break; |
| 6134 | } |
| 6135 | } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(LHSI)) { |
| 6136 | // Handle icmp {eq|ne} <intrinsic>, intcst. |
| 6137 | if (II->getIntrinsicID() == Intrinsic::bswap) { |
| 6138 | AddToWorkList(II); |
| 6139 | ICI.setOperand(0, II->getOperand(1)); |
| 6140 | ICI.setOperand(1, ConstantInt::get(RHSV.byteSwap())); |
| 6141 | return &ICI; |
| 6142 | } |
| 6143 | } |
| 6144 | } else { // Not a ICMP_EQ/ICMP_NE |
Chris Lattner | e34e9a2 | 2007-04-14 23:32:02 +0000 | [diff] [blame] | 6145 | // If the LHS is a cast from an integral value of the same size, |
| 6146 | // then since we know the RHS is a constant, try to simlify. |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6147 | if (CastInst *Cast = dyn_cast<CastInst>(LHSI)) { |
| 6148 | Value *CastOp = Cast->getOperand(0); |
| 6149 | const Type *SrcTy = CastOp->getType(); |
| 6150 | uint32_t SrcTySize = SrcTy->getPrimitiveSizeInBits(); |
| 6151 | if (SrcTy->isInteger() && |
| 6152 | SrcTySize == Cast->getType()->getPrimitiveSizeInBits()) { |
| 6153 | // If this is an unsigned comparison, try to make the comparison use |
| 6154 | // smaller constant values. |
| 6155 | if (ICI.getPredicate() == ICmpInst::ICMP_ULT && RHSV.isSignBit()) { |
| 6156 | // X u< 128 => X s> -1 |
| 6157 | return new ICmpInst(ICmpInst::ICMP_SGT, CastOp, |
| 6158 | ConstantInt::get(APInt::getAllOnesValue(SrcTySize))); |
| 6159 | } else if (ICI.getPredicate() == ICmpInst::ICMP_UGT && |
| 6160 | RHSV == APInt::getSignedMaxValue(SrcTySize)) { |
| 6161 | // X u> 127 => X s< 0 |
| 6162 | return new ICmpInst(ICmpInst::ICMP_SLT, CastOp, |
| 6163 | Constant::getNullValue(SrcTy)); |
| 6164 | } |
| 6165 | } |
| 6166 | } |
| 6167 | } |
| 6168 | return 0; |
| 6169 | } |
| 6170 | |
| 6171 | /// visitICmpInstWithCastAndCast - Handle icmp (cast x to y), (cast/cst). |
| 6172 | /// We only handle extending casts so far. |
| 6173 | /// |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6174 | Instruction *InstCombiner::visitICmpInstWithCastAndCast(ICmpInst &ICI) { |
| 6175 | const CastInst *LHSCI = cast<CastInst>(ICI.getOperand(0)); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6176 | Value *LHSCIOp = LHSCI->getOperand(0); |
| 6177 | const Type *SrcTy = LHSCIOp->getType(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6178 | const Type *DestTy = LHSCI->getType(); |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 6179 | Value *RHSCIOp; |
| 6180 | |
Chris Lattner | 8c756c1 | 2007-05-05 22:41:33 +0000 | [diff] [blame] | 6181 | // Turn icmp (ptrtoint x), (ptrtoint/c) into a compare of the input if the |
| 6182 | // integer type is the same size as the pointer type. |
| 6183 | if (LHSCI->getOpcode() == Instruction::PtrToInt && |
| 6184 | getTargetData().getPointerSizeInBits() == |
| 6185 | cast<IntegerType>(DestTy)->getBitWidth()) { |
| 6186 | Value *RHSOp = 0; |
| 6187 | if (Constant *RHSC = dyn_cast<Constant>(ICI.getOperand(1))) { |
Chris Lattner | 6f6f512 | 2007-05-06 07:24:03 +0000 | [diff] [blame] | 6188 | RHSOp = ConstantExpr::getIntToPtr(RHSC, SrcTy); |
Chris Lattner | 8c756c1 | 2007-05-05 22:41:33 +0000 | [diff] [blame] | 6189 | } else if (PtrToIntInst *RHSC = dyn_cast<PtrToIntInst>(ICI.getOperand(1))) { |
| 6190 | RHSOp = RHSC->getOperand(0); |
| 6191 | // If the pointer types don't match, insert a bitcast. |
| 6192 | if (LHSCIOp->getType() != RHSOp->getType()) |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 6193 | RHSOp = InsertBitCastBefore(RHSOp, LHSCIOp->getType(), ICI); |
Chris Lattner | 8c756c1 | 2007-05-05 22:41:33 +0000 | [diff] [blame] | 6194 | } |
| 6195 | |
| 6196 | if (RHSOp) |
| 6197 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSOp); |
| 6198 | } |
| 6199 | |
| 6200 | // The code below only handles extension cast instructions, so far. |
| 6201 | // Enforce this. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6202 | if (LHSCI->getOpcode() != Instruction::ZExt && |
| 6203 | LHSCI->getOpcode() != Instruction::SExt) |
Chris Lattner | b352fa5 | 2005-01-17 03:20:02 +0000 | [diff] [blame] | 6204 | return 0; |
| 6205 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6206 | bool isSignedExt = LHSCI->getOpcode() == Instruction::SExt; |
| 6207 | bool isSignedCmp = ICI.isSignedPredicate(); |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 6208 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6209 | if (CastInst *CI = dyn_cast<CastInst>(ICI.getOperand(1))) { |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 6210 | // Not an extension from the same type? |
| 6211 | RHSCIOp = CI->getOperand(0); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6212 | if (RHSCIOp->getType() != LHSCIOp->getType()) |
| 6213 | return 0; |
Chris Lattner | a5c5e77 | 2007-01-13 23:11:38 +0000 | [diff] [blame] | 6214 | |
Nick Lewycky | 4189a53 | 2008-01-28 03:48:02 +0000 | [diff] [blame] | 6215 | // If the signedness of the two casts doesn't agree (i.e. one is a sext |
Chris Lattner | a5c5e77 | 2007-01-13 23:11:38 +0000 | [diff] [blame] | 6216 | // and the other is a zext), then we can't handle this. |
| 6217 | if (CI->getOpcode() != LHSCI->getOpcode()) |
| 6218 | return 0; |
| 6219 | |
Nick Lewycky | 4189a53 | 2008-01-28 03:48:02 +0000 | [diff] [blame] | 6220 | // Deal with equality cases early. |
| 6221 | if (ICI.isEquality()) |
| 6222 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSCIOp); |
| 6223 | |
| 6224 | // A signed comparison of sign extended values simplifies into a |
| 6225 | // signed comparison. |
| 6226 | if (isSignedCmp && isSignedExt) |
| 6227 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSCIOp); |
| 6228 | |
| 6229 | // The other three cases all fold into an unsigned comparison. |
| 6230 | return new ICmpInst(ICI.getUnsignedPredicate(), LHSCIOp, RHSCIOp); |
Reid Spencer | 6731d5c | 2004-11-28 21:31:15 +0000 | [diff] [blame] | 6231 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 6232 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6233 | // If we aren't dealing with a constant on the RHS, exit early |
| 6234 | ConstantInt *CI = dyn_cast<ConstantInt>(ICI.getOperand(1)); |
| 6235 | if (!CI) |
| 6236 | return 0; |
| 6237 | |
| 6238 | // Compute the constant that would happen if we truncated to SrcTy then |
| 6239 | // reextended to DestTy. |
| 6240 | Constant *Res1 = ConstantExpr::getTrunc(CI, SrcTy); |
| 6241 | Constant *Res2 = ConstantExpr::getCast(LHSCI->getOpcode(), Res1, DestTy); |
| 6242 | |
| 6243 | // If the re-extended constant didn't change... |
| 6244 | if (Res2 == CI) { |
| 6245 | // Make sure that sign of the Cmp and the sign of the Cast are the same. |
| 6246 | // For example, we might have: |
| 6247 | // %A = sext short %X to uint |
| 6248 | // %B = icmp ugt uint %A, 1330 |
| 6249 | // It is incorrect to transform this into |
| 6250 | // %B = icmp ugt short %X, 1330 |
| 6251 | // because %A may have negative value. |
| 6252 | // |
Chris Lattner | f299184 | 2008-07-11 04:09:09 +0000 | [diff] [blame] | 6253 | // However, we allow this when the compare is EQ/NE, because they are |
| 6254 | // signless. |
| 6255 | if (isSignedExt == isSignedCmp || ICI.isEquality()) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6256 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, Res1); |
Chris Lattner | f299184 | 2008-07-11 04:09:09 +0000 | [diff] [blame] | 6257 | return 0; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6258 | } |
| 6259 | |
| 6260 | // The re-extended constant changed so the constant cannot be represented |
| 6261 | // in the shorter type. Consequently, we cannot emit a simple comparison. |
| 6262 | |
| 6263 | // First, handle some easy cases. We know the result cannot be equal at this |
| 6264 | // point so handle the ICI.isEquality() cases |
| 6265 | if (ICI.getPredicate() == ICmpInst::ICMP_EQ) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6266 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6267 | if (ICI.getPredicate() == ICmpInst::ICMP_NE) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6268 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6269 | |
| 6270 | // Evaluate the comparison for LT (we invert for GT below). LE and GE cases |
| 6271 | // should have been folded away previously and not enter in here. |
| 6272 | Value *Result; |
| 6273 | if (isSignedCmp) { |
| 6274 | // We're performing a signed comparison. |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 6275 | if (cast<ConstantInt>(CI)->getValue().isNegative()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6276 | Result = ConstantInt::getFalse(); // X < (small) --> false |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6277 | else |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6278 | Result = ConstantInt::getTrue(); // X < (large) --> true |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6279 | } else { |
| 6280 | // We're performing an unsigned comparison. |
| 6281 | if (isSignedExt) { |
| 6282 | // We're performing an unsigned comp with a sign extended value. |
| 6283 | // This is true if the input is >= 0. [aka >s -1] |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6284 | Constant *NegOne = ConstantInt::getAllOnesValue(SrcTy); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6285 | Result = InsertNewInstBefore(new ICmpInst(ICmpInst::ICMP_SGT, LHSCIOp, |
| 6286 | NegOne, ICI.getName()), ICI); |
| 6287 | } else { |
| 6288 | // Unsigned extend & unsigned compare -> always true. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6289 | Result = ConstantInt::getTrue(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6290 | } |
| 6291 | } |
| 6292 | |
| 6293 | // Finally, return the value computed. |
| 6294 | if (ICI.getPredicate() == ICmpInst::ICMP_ULT || |
Chris Lattner | f299184 | 2008-07-11 04:09:09 +0000 | [diff] [blame] | 6295 | ICI.getPredicate() == ICmpInst::ICMP_SLT) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6296 | return ReplaceInstUsesWith(ICI, Result); |
Chris Lattner | f299184 | 2008-07-11 04:09:09 +0000 | [diff] [blame] | 6297 | |
| 6298 | assert((ICI.getPredicate()==ICmpInst::ICMP_UGT || |
| 6299 | ICI.getPredicate()==ICmpInst::ICMP_SGT) && |
| 6300 | "ICmp should be folded!"); |
| 6301 | if (Constant *CI = dyn_cast<Constant>(Result)) |
| 6302 | return ReplaceInstUsesWith(ICI, ConstantExpr::getNot(CI)); |
| 6303 | return BinaryOperator::CreateNot(Result); |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 6304 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 6305 | |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6306 | Instruction *InstCombiner::visitShl(BinaryOperator &I) { |
| 6307 | return commonShiftTransforms(I); |
| 6308 | } |
| 6309 | |
| 6310 | Instruction *InstCombiner::visitLShr(BinaryOperator &I) { |
| 6311 | return commonShiftTransforms(I); |
| 6312 | } |
| 6313 | |
| 6314 | Instruction *InstCombiner::visitAShr(BinaryOperator &I) { |
Chris Lattner | 348f665 | 2007-12-06 01:59:46 +0000 | [diff] [blame] | 6315 | if (Instruction *R = commonShiftTransforms(I)) |
| 6316 | return R; |
| 6317 | |
| 6318 | Value *Op0 = I.getOperand(0); |
| 6319 | |
| 6320 | // ashr int -1, X = -1 (for any arithmetic shift rights of ~0) |
| 6321 | if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0)) |
| 6322 | if (CSI->isAllOnesValue()) |
| 6323 | return ReplaceInstUsesWith(I, CSI); |
| 6324 | |
| 6325 | // See if we can turn a signed shr into an unsigned shr. |
| 6326 | if (MaskedValueIsZero(Op0, |
| 6327 | APInt::getSignBit(I.getType()->getPrimitiveSizeInBits()))) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6328 | return BinaryOperator::CreateLShr(Op0, I.getOperand(1)); |
Chris Lattner | 348f665 | 2007-12-06 01:59:46 +0000 | [diff] [blame] | 6329 | |
| 6330 | return 0; |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6331 | } |
| 6332 | |
| 6333 | Instruction *InstCombiner::commonShiftTransforms(BinaryOperator &I) { |
| 6334 | assert(I.getOperand(1)->getType() == I.getOperand(0)->getType()); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 6335 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 6336 | |
| 6337 | // shl X, 0 == X and shr X, 0 == X |
| 6338 | // shl 0, X == 0 and shr 0, X == 0 |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6339 | if (Op1 == Constant::getNullValue(Op1->getType()) || |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 6340 | Op0 == Constant::getNullValue(Op0->getType())) |
| 6341 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 8d6bbdb | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 6342 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6343 | if (isa<UndefValue>(Op0)) { |
| 6344 | if (I.getOpcode() == Instruction::AShr) // undef >>s X -> undef |
Chris Lattner | 79a564c | 2004-10-16 23:28:04 +0000 | [diff] [blame] | 6345 | return ReplaceInstUsesWith(I, Op0); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6346 | else // undef << X -> 0, undef >>u X -> 0 |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 6347 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 6348 | } |
| 6349 | if (isa<UndefValue>(Op1)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6350 | if (I.getOpcode() == Instruction::AShr) // X >>s undef -> X |
| 6351 | return ReplaceInstUsesWith(I, Op0); |
| 6352 | else // X << undef, X >>u undef -> 0 |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 6353 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 6354 | } |
| 6355 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 6356 | // Try to fold constant and into select arguments. |
| 6357 | if (isa<Constant>(Op0)) |
| 6358 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 6359 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 6360 | return R; |
| 6361 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6362 | if (ConstantInt *CUI = dyn_cast<ConstantInt>(Op1)) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6363 | if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I)) |
| 6364 | return Res; |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6365 | return 0; |
| 6366 | } |
| 6367 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6368 | Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6369 | BinaryOperator &I) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6370 | bool isLeftShift = I.getOpcode() == Instruction::Shl; |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6371 | |
Chris Lattner | 8d6bbdb | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 6372 | // See if we can simplify any instructions used by the instruction whose sole |
| 6373 | // purpose is to compute bits we don't care about. |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6374 | uint32_t TypeBits = Op0->getType()->getPrimitiveSizeInBits(); |
| 6375 | APInt KnownZero(TypeBits, 0), KnownOne(TypeBits, 0); |
| 6376 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(TypeBits), |
Chris Lattner | 8d6bbdb | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 6377 | KnownZero, KnownOne)) |
| 6378 | return &I; |
| 6379 | |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6380 | // shl uint X, 32 = 0 and shr ubyte Y, 9 = 0, ... just don't eliminate shr |
| 6381 | // of a signed value. |
| 6382 | // |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 6383 | if (Op1->uge(TypeBits)) { |
Chris Lattner | 0737c24 | 2007-02-02 05:29:55 +0000 | [diff] [blame] | 6384 | if (I.getOpcode() != Instruction::AShr) |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6385 | return ReplaceInstUsesWith(I, Constant::getNullValue(Op0->getType())); |
| 6386 | else { |
Chris Lattner | 0737c24 | 2007-02-02 05:29:55 +0000 | [diff] [blame] | 6387 | I.setOperand(1, ConstantInt::get(I.getType(), TypeBits-1)); |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6388 | return &I; |
Chris Lattner | 8adac75 | 2004-02-23 20:30:06 +0000 | [diff] [blame] | 6389 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6390 | } |
| 6391 | |
| 6392 | // ((X*C1) << C2) == (X * (C1 << C2)) |
| 6393 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0)) |
| 6394 | if (BO->getOpcode() == Instruction::Mul && isLeftShift) |
| 6395 | if (Constant *BOOp = dyn_cast<Constant>(BO->getOperand(1))) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6396 | return BinaryOperator::CreateMul(BO->getOperand(0), |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6397 | ConstantExpr::getShl(BOOp, Op1)); |
| 6398 | |
| 6399 | // Try to fold constant and into select arguments. |
| 6400 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 6401 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 6402 | return R; |
| 6403 | if (isa<PHINode>(Op0)) |
| 6404 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 6405 | return NV; |
| 6406 | |
Chris Lattner | 8999dd3 | 2007-12-22 09:07:47 +0000 | [diff] [blame] | 6407 | // Fold shift2(trunc(shift1(x,c1)), c2) -> trunc(shift2(shift1(x,c1),c2)) |
| 6408 | if (TruncInst *TI = dyn_cast<TruncInst>(Op0)) { |
| 6409 | Instruction *TrOp = dyn_cast<Instruction>(TI->getOperand(0)); |
| 6410 | // If 'shift2' is an ashr, we would have to get the sign bit into a funny |
| 6411 | // place. Don't try to do this transformation in this case. Also, we |
| 6412 | // require that the input operand is a shift-by-constant so that we have |
| 6413 | // confidence that the shifts will get folded together. We could do this |
| 6414 | // xform in more cases, but it is unlikely to be profitable. |
| 6415 | if (TrOp && I.isLogicalShift() && TrOp->isShift() && |
| 6416 | isa<ConstantInt>(TrOp->getOperand(1))) { |
| 6417 | // Okay, we'll do this xform. Make the shift of shift. |
| 6418 | Constant *ShAmt = ConstantExpr::getZExt(Op1, TrOp->getType()); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6419 | Instruction *NSh = BinaryOperator::Create(I.getOpcode(), TrOp, ShAmt, |
Chris Lattner | 8999dd3 | 2007-12-22 09:07:47 +0000 | [diff] [blame] | 6420 | I.getName()); |
| 6421 | InsertNewInstBefore(NSh, I); // (shift2 (shift1 & 0x00FF), c2) |
| 6422 | |
| 6423 | // For logical shifts, the truncation has the effect of making the high |
| 6424 | // part of the register be zeros. Emulate this by inserting an AND to |
| 6425 | // clear the top bits as needed. This 'and' will usually be zapped by |
| 6426 | // other xforms later if dead. |
| 6427 | unsigned SrcSize = TrOp->getType()->getPrimitiveSizeInBits(); |
| 6428 | unsigned DstSize = TI->getType()->getPrimitiveSizeInBits(); |
| 6429 | APInt MaskV(APInt::getLowBitsSet(SrcSize, DstSize)); |
| 6430 | |
| 6431 | // The mask we constructed says what the trunc would do if occurring |
| 6432 | // between the shifts. We want to know the effect *after* the second |
| 6433 | // shift. We know that it is a logical shift by a constant, so adjust the |
| 6434 | // mask as appropriate. |
| 6435 | if (I.getOpcode() == Instruction::Shl) |
| 6436 | MaskV <<= Op1->getZExtValue(); |
| 6437 | else { |
| 6438 | assert(I.getOpcode() == Instruction::LShr && "Unknown logical shift"); |
| 6439 | MaskV = MaskV.lshr(Op1->getZExtValue()); |
| 6440 | } |
| 6441 | |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6442 | Instruction *And = BinaryOperator::CreateAnd(NSh, ConstantInt::get(MaskV), |
Chris Lattner | 8999dd3 | 2007-12-22 09:07:47 +0000 | [diff] [blame] | 6443 | TI->getName()); |
| 6444 | InsertNewInstBefore(And, I); // shift1 & 0x00FF |
| 6445 | |
| 6446 | // Return the value truncated to the interesting size. |
| 6447 | return new TruncInst(And, I.getType()); |
| 6448 | } |
| 6449 | } |
| 6450 | |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6451 | if (Op0->hasOneUse()) { |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6452 | if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0)) { |
| 6453 | // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C) |
| 6454 | Value *V1, *V2; |
| 6455 | ConstantInt *CC; |
| 6456 | switch (Op0BO->getOpcode()) { |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 6457 | default: break; |
| 6458 | case Instruction::Add: |
| 6459 | case Instruction::And: |
| 6460 | case Instruction::Or: |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6461 | case Instruction::Xor: { |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 6462 | // These operators commute. |
| 6463 | // Turn (Y + (X >> C)) << C -> (X + (Y << C)) & (~0 << C) |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6464 | if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() && |
| 6465 | match(Op0BO->getOperand(1), |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6466 | m_Shr(m_Value(V1), m_ConstantInt(CC))) && CC == Op1) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6467 | Instruction *YS = BinaryOperator::CreateShl( |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6468 | Op0BO->getOperand(0), Op1, |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6469 | Op0BO->getName()); |
| 6470 | InsertNewInstBefore(YS, I); // (Y << C) |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 6471 | Instruction *X = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6472 | BinaryOperator::Create(Op0BO->getOpcode(), YS, V1, |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 6473 | Op0BO->getOperand(1)->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6474 | InsertNewInstBefore(X, I); // (X + (Y << C)) |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 6475 | uint32_t Op1Val = Op1->getLimitedValue(TypeBits); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6476 | return BinaryOperator::CreateAnd(X, ConstantInt::get( |
Zhou Sheng | 90b9681 | 2007-03-30 05:45:18 +0000 | [diff] [blame] | 6477 | APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val))); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6478 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6479 | |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6480 | // Turn (Y + ((X >> C) & CC)) << C -> ((X & (CC << C)) + (Y << C)) |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6481 | Value *Op0BOOp1 = Op0BO->getOperand(1); |
Chris Lattner | 3c69849 | 2007-03-05 00:11:19 +0000 | [diff] [blame] | 6482 | if (isLeftShift && Op0BOOp1->hasOneUse() && |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6483 | match(Op0BOOp1, |
| 6484 | m_And(m_Shr(m_Value(V1), m_Value(V2)),m_ConstantInt(CC))) && |
Chris Lattner | 3c69849 | 2007-03-05 00:11:19 +0000 | [diff] [blame] | 6485 | cast<BinaryOperator>(Op0BOOp1)->getOperand(0)->hasOneUse() && |
| 6486 | V2 == Op1) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6487 | Instruction *YS = BinaryOperator::CreateShl( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6488 | Op0BO->getOperand(0), Op1, |
| 6489 | Op0BO->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6490 | InsertNewInstBefore(YS, I); // (Y << C) |
| 6491 | Instruction *XM = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6492 | BinaryOperator::CreateAnd(V1, ConstantExpr::getShl(CC, Op1), |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6493 | V1->getName()+".mask"); |
| 6494 | InsertNewInstBefore(XM, I); // X & (CC << C) |
| 6495 | |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6496 | return BinaryOperator::Create(Op0BO->getOpcode(), YS, XM); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6497 | } |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6498 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6499 | |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6500 | // FALL THROUGH. |
| 6501 | case Instruction::Sub: { |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 6502 | // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C) |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6503 | if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() && |
| 6504 | match(Op0BO->getOperand(0), |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6505 | m_Shr(m_Value(V1), m_ConstantInt(CC))) && CC == Op1) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6506 | Instruction *YS = BinaryOperator::CreateShl( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6507 | Op0BO->getOperand(1), Op1, |
| 6508 | Op0BO->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6509 | InsertNewInstBefore(YS, I); // (Y << C) |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 6510 | Instruction *X = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6511 | BinaryOperator::Create(Op0BO->getOpcode(), V1, YS, |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 6512 | Op0BO->getOperand(0)->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6513 | InsertNewInstBefore(X, I); // (X + (Y << C)) |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 6514 | uint32_t Op1Val = Op1->getLimitedValue(TypeBits); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6515 | return BinaryOperator::CreateAnd(X, ConstantInt::get( |
Zhou Sheng | 90b9681 | 2007-03-30 05:45:18 +0000 | [diff] [blame] | 6516 | APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val))); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6517 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6518 | |
Chris Lattner | 13d4ab4 | 2006-05-31 21:14:00 +0000 | [diff] [blame] | 6519 | // Turn (((X >> C)&CC) + Y) << C -> (X + (Y << C)) & (CC << C) |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6520 | if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() && |
| 6521 | match(Op0BO->getOperand(0), |
| 6522 | m_And(m_Shr(m_Value(V1), m_Value(V2)), |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6523 | m_ConstantInt(CC))) && V2 == Op1 && |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 6524 | cast<BinaryOperator>(Op0BO->getOperand(0)) |
| 6525 | ->getOperand(0)->hasOneUse()) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6526 | Instruction *YS = BinaryOperator::CreateShl( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6527 | Op0BO->getOperand(1), Op1, |
| 6528 | Op0BO->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6529 | InsertNewInstBefore(YS, I); // (Y << C) |
| 6530 | Instruction *XM = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6531 | BinaryOperator::CreateAnd(V1, ConstantExpr::getShl(CC, Op1), |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6532 | V1->getName()+".mask"); |
| 6533 | InsertNewInstBefore(XM, I); // X & (CC << C) |
| 6534 | |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6535 | return BinaryOperator::Create(Op0BO->getOpcode(), XM, YS); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6536 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6537 | |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 6538 | break; |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6539 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6540 | } |
| 6541 | |
| 6542 | |
| 6543 | // If the operand is an bitwise operator with a constant RHS, and the |
| 6544 | // shift is the only use, we can pull it out of the shift. |
| 6545 | if (ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand(1))) { |
| 6546 | bool isValid = true; // Valid only for And, Or, Xor |
| 6547 | bool highBitSet = false; // Transform if high bit of constant set? |
| 6548 | |
| 6549 | switch (Op0BO->getOpcode()) { |
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 6550 | default: isValid = false; break; // Do not perform transform! |
Chris Lattner | 1f7e160 | 2004-10-08 03:46:20 +0000 | [diff] [blame] | 6551 | case Instruction::Add: |
| 6552 | isValid = isLeftShift; |
| 6553 | break; |
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 6554 | case Instruction::Or: |
| 6555 | case Instruction::Xor: |
| 6556 | highBitSet = false; |
| 6557 | break; |
| 6558 | case Instruction::And: |
| 6559 | highBitSet = true; |
| 6560 | break; |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6561 | } |
| 6562 | |
| 6563 | // If this is a signed shift right, and the high bit is modified |
| 6564 | // by the logical operation, do not perform the transformation. |
| 6565 | // The highBitSet boolean indicates the value of the high bit of |
| 6566 | // the constant which would cause it to be modified for this |
| 6567 | // operation. |
| 6568 | // |
Chris Lattner | c95ba44 | 2007-12-06 06:25:04 +0000 | [diff] [blame] | 6569 | if (isValid && I.getOpcode() == Instruction::AShr) |
Zhou Sheng | e9e03f6 | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 6570 | isValid = Op0C->getValue()[TypeBits-1] == highBitSet; |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6571 | |
| 6572 | if (isValid) { |
| 6573 | Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, Op1); |
| 6574 | |
| 6575 | Instruction *NewShift = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6576 | BinaryOperator::Create(I.getOpcode(), Op0BO->getOperand(0), Op1); |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6577 | InsertNewInstBefore(NewShift, I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6578 | NewShift->takeName(Op0BO); |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6579 | |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6580 | return BinaryOperator::Create(Op0BO->getOpcode(), NewShift, |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6581 | NewRHS); |
| 6582 | } |
| 6583 | } |
| 6584 | } |
| 6585 | } |
| 6586 | |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6587 | // 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] | 6588 | BinaryOperator *ShiftOp = dyn_cast<BinaryOperator>(Op0); |
| 6589 | if (ShiftOp && !ShiftOp->isShift()) |
| 6590 | ShiftOp = 0; |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6591 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6592 | if (ShiftOp && isa<ConstantInt>(ShiftOp->getOperand(1))) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6593 | ConstantInt *ShiftAmt1C = cast<ConstantInt>(ShiftOp->getOperand(1)); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 6594 | uint32_t ShiftAmt1 = ShiftAmt1C->getLimitedValue(TypeBits); |
| 6595 | uint32_t ShiftAmt2 = Op1->getLimitedValue(TypeBits); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6596 | assert(ShiftAmt2 != 0 && "Should have been simplified earlier"); |
| 6597 | if (ShiftAmt1 == 0) return 0; // Will be simplified in the future. |
| 6598 | Value *X = ShiftOp->getOperand(0); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6599 | |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 6600 | uint32_t AmtSum = ShiftAmt1+ShiftAmt2; // Fold into one big shift. |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6601 | if (AmtSum > TypeBits) |
| 6602 | AmtSum = TypeBits; |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6603 | |
| 6604 | const IntegerType *Ty = cast<IntegerType>(I.getType()); |
| 6605 | |
| 6606 | // Check for (X << c1) << c2 and (X >> c1) >> c2 |
Chris Lattner | 7f3da2d | 2007-02-03 23:28:07 +0000 | [diff] [blame] | 6607 | if (I.getOpcode() == ShiftOp->getOpcode()) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6608 | return BinaryOperator::Create(I.getOpcode(), X, |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6609 | ConstantInt::get(Ty, AmtSum)); |
| 6610 | } else if (ShiftOp->getOpcode() == Instruction::LShr && |
| 6611 | I.getOpcode() == Instruction::AShr) { |
| 6612 | // ((X >>u C1) >>s C2) -> (X >>u (C1+C2)) since C1 != 0. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6613 | return BinaryOperator::CreateLShr(X, ConstantInt::get(Ty, AmtSum)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6614 | } else if (ShiftOp->getOpcode() == Instruction::AShr && |
| 6615 | I.getOpcode() == Instruction::LShr) { |
| 6616 | // ((X >>s C1) >>u C2) -> ((X >>s (C1+C2)) & mask) since C1 != 0. |
| 6617 | Instruction *Shift = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6618 | BinaryOperator::CreateAShr(X, ConstantInt::get(Ty, AmtSum)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6619 | InsertNewInstBefore(Shift, I); |
| 6620 | |
Zhou Sheng | e9e03f6 | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 6621 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6622 | return BinaryOperator::CreateAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6623 | } |
| 6624 | |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6625 | // Okay, if we get here, one shift must be left, and the other shift must be |
| 6626 | // right. See if the amounts are equal. |
| 6627 | if (ShiftAmt1 == ShiftAmt2) { |
| 6628 | // If we have ((X >>? C) << C), turn this into X & (-1 << C). |
| 6629 | if (I.getOpcode() == Instruction::Shl) { |
Reid Spencer | 55702aa | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 6630 | APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt1)); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6631 | return BinaryOperator::CreateAnd(X, ConstantInt::get(Mask)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6632 | } |
| 6633 | // If we have ((X << C) >>u C), turn this into X & (-1 >>u C). |
| 6634 | if (I.getOpcode() == Instruction::LShr) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 6635 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt1)); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6636 | return BinaryOperator::CreateAnd(X, ConstantInt::get(Mask)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6637 | } |
| 6638 | // We can simplify ((X << C) >>s C) into a trunc + sext. |
| 6639 | // NOTE: we could do this for any C, but that would make 'unusual' integer |
| 6640 | // types. For now, just stick to ones well-supported by the code |
| 6641 | // generators. |
| 6642 | const Type *SExtType = 0; |
| 6643 | switch (Ty->getBitWidth() - ShiftAmt1) { |
Zhou Sheng | e9e03f6 | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 6644 | case 1 : |
| 6645 | case 8 : |
| 6646 | case 16 : |
| 6647 | case 32 : |
| 6648 | case 64 : |
| 6649 | case 128: |
| 6650 | SExtType = IntegerType::get(Ty->getBitWidth() - ShiftAmt1); |
| 6651 | break; |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6652 | default: break; |
| 6653 | } |
| 6654 | if (SExtType) { |
| 6655 | Instruction *NewTrunc = new TruncInst(X, SExtType, "sext"); |
| 6656 | InsertNewInstBefore(NewTrunc, I); |
| 6657 | return new SExtInst(NewTrunc, Ty); |
| 6658 | } |
| 6659 | // Otherwise, we can't handle it yet. |
| 6660 | } else if (ShiftAmt1 < ShiftAmt2) { |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 6661 | uint32_t ShiftDiff = ShiftAmt2-ShiftAmt1; |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6662 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 6663 | // (X >>? C1) << C2 --> X << (C2-C1) & (-1 << C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6664 | if (I.getOpcode() == Instruction::Shl) { |
| 6665 | assert(ShiftOp->getOpcode() == Instruction::LShr || |
| 6666 | ShiftOp->getOpcode() == Instruction::AShr); |
Chris Lattner | e8d56c5 | 2006-01-07 01:32:28 +0000 | [diff] [blame] | 6667 | Instruction *Shift = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6668 | BinaryOperator::CreateShl(X, ConstantInt::get(Ty, ShiftDiff)); |
Chris Lattner | e8d56c5 | 2006-01-07 01:32:28 +0000 | [diff] [blame] | 6669 | InsertNewInstBefore(Shift, I); |
| 6670 | |
Reid Spencer | 55702aa | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 6671 | APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6672 | return BinaryOperator::CreateAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6673 | } |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6674 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 6675 | // (X << C1) >>u C2 --> X >>u (C2-C1) & (-1 >> C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6676 | if (I.getOpcode() == Instruction::LShr) { |
| 6677 | assert(ShiftOp->getOpcode() == Instruction::Shl); |
| 6678 | Instruction *Shift = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6679 | BinaryOperator::CreateLShr(X, ConstantInt::get(Ty, ShiftDiff)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6680 | InsertNewInstBefore(Shift, I); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6681 | |
Reid Spencer | d5e30f0 | 2007-03-26 17:18:58 +0000 | [diff] [blame] | 6682 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6683 | return BinaryOperator::CreateAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 6684 | } |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6685 | |
| 6686 | // We can't handle (X << C1) >>s C2, it shifts arbitrary bits in. |
| 6687 | } else { |
| 6688 | assert(ShiftAmt2 < ShiftAmt1); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 6689 | uint32_t ShiftDiff = ShiftAmt1-ShiftAmt2; |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6690 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 6691 | // (X >>? C1) << C2 --> X >>? (C1-C2) & (-1 << C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6692 | if (I.getOpcode() == Instruction::Shl) { |
| 6693 | assert(ShiftOp->getOpcode() == Instruction::LShr || |
| 6694 | ShiftOp->getOpcode() == Instruction::AShr); |
| 6695 | Instruction *Shift = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6696 | BinaryOperator::Create(ShiftOp->getOpcode(), X, |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6697 | ConstantInt::get(Ty, ShiftDiff)); |
| 6698 | InsertNewInstBefore(Shift, I); |
| 6699 | |
Reid Spencer | 55702aa | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 6700 | APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6701 | return BinaryOperator::CreateAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6702 | } |
| 6703 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 6704 | // (X << C1) >>u C2 --> X << (C1-C2) & (-1 >> C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6705 | if (I.getOpcode() == Instruction::LShr) { |
| 6706 | assert(ShiftOp->getOpcode() == Instruction::Shl); |
| 6707 | Instruction *Shift = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6708 | BinaryOperator::CreateShl(X, ConstantInt::get(Ty, ShiftDiff)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6709 | InsertNewInstBefore(Shift, I); |
| 6710 | |
Reid Spencer | 68d27cf | 2007-03-26 23:45:51 +0000 | [diff] [blame] | 6711 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6712 | return BinaryOperator::CreateAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6713 | } |
| 6714 | |
| 6715 | // 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] | 6716 | } |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6717 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 6718 | return 0; |
| 6719 | } |
| 6720 | |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 6721 | |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 6722 | /// DecomposeSimpleLinearExpr - Analyze 'Val', seeing if it is a simple linear |
| 6723 | /// expression. If so, decompose it, returning some value X, such that Val is |
| 6724 | /// X*Scale+Offset. |
| 6725 | /// |
| 6726 | static Value *DecomposeSimpleLinearExpr(Value *Val, unsigned &Scale, |
Jeff Cohen | 86796be | 2007-04-04 16:58:57 +0000 | [diff] [blame] | 6727 | int &Offset) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6728 | assert(Val->getType() == Type::Int32Ty && "Unexpected allocation size type!"); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6729 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6730 | Offset = CI->getZExtValue(); |
Chris Lattner | 6a94de2 | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 6731 | Scale = 0; |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6732 | return ConstantInt::get(Type::Int32Ty, 0); |
Chris Lattner | 6a94de2 | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 6733 | } else if (BinaryOperator *I = dyn_cast<BinaryOperator>(Val)) { |
| 6734 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 6735 | if (I->getOpcode() == Instruction::Shl) { |
| 6736 | // This is a value scaled by '1 << the shift amt'. |
| 6737 | Scale = 1U << RHS->getZExtValue(); |
| 6738 | Offset = 0; |
| 6739 | return I->getOperand(0); |
| 6740 | } else if (I->getOpcode() == Instruction::Mul) { |
| 6741 | // This value is scaled by 'RHS'. |
| 6742 | Scale = RHS->getZExtValue(); |
| 6743 | Offset = 0; |
| 6744 | return I->getOperand(0); |
| 6745 | } else if (I->getOpcode() == Instruction::Add) { |
| 6746 | // We have X+C. Check to see if we really have (X*C2)+C1, |
| 6747 | // where C1 is divisible by C2. |
| 6748 | unsigned SubScale; |
| 6749 | Value *SubVal = |
| 6750 | DecomposeSimpleLinearExpr(I->getOperand(0), SubScale, Offset); |
| 6751 | Offset += RHS->getZExtValue(); |
| 6752 | Scale = SubScale; |
| 6753 | return SubVal; |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 6754 | } |
| 6755 | } |
| 6756 | } |
| 6757 | |
| 6758 | // Otherwise, we can't look past this. |
| 6759 | Scale = 1; |
| 6760 | Offset = 0; |
| 6761 | return Val; |
| 6762 | } |
| 6763 | |
| 6764 | |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6765 | /// PromoteCastOfAllocation - If we find a cast of an allocation instruction, |
| 6766 | /// 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] | 6767 | Instruction *InstCombiner::PromoteCastOfAllocation(BitCastInst &CI, |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6768 | AllocationInst &AI) { |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 6769 | const PointerType *PTy = cast<PointerType>(CI.getType()); |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6770 | |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 6771 | // Remove any uses of AI that are dead. |
| 6772 | assert(!CI.use_empty() && "Dead instructions should be removed earlier!"); |
Chris Lattner | 535014f | 2007-02-15 22:52:10 +0000 | [diff] [blame] | 6773 | |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 6774 | for (Value::use_iterator UI = AI.use_begin(), E = AI.use_end(); UI != E; ) { |
| 6775 | Instruction *User = cast<Instruction>(*UI++); |
| 6776 | if (isInstructionTriviallyDead(User)) { |
| 6777 | while (UI != E && *UI == User) |
| 6778 | ++UI; // If this instruction uses AI more than once, don't break UI. |
| 6779 | |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 6780 | ++NumDeadInst; |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 6781 | DOUT << "IC: DCE: " << *User; |
Chris Lattner | f22a5c6 | 2007-03-02 19:59:19 +0000 | [diff] [blame] | 6782 | EraseInstFromFunction(*User); |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 6783 | } |
| 6784 | } |
| 6785 | |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6786 | // Get the type really allocated and the type casted to. |
| 6787 | const Type *AllocElTy = AI.getAllocatedType(); |
| 6788 | const Type *CastElTy = PTy->getElementType(); |
| 6789 | if (!AllocElTy->isSized() || !CastElTy->isSized()) return 0; |
Chris Lattner | 18e78bb | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 6790 | |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 6791 | unsigned AllocElTyAlign = TD->getABITypeAlignment(AllocElTy); |
| 6792 | unsigned CastElTyAlign = TD->getABITypeAlignment(CastElTy); |
Chris Lattner | 18e78bb | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 6793 | if (CastElTyAlign < AllocElTyAlign) return 0; |
| 6794 | |
Chris Lattner | 39387a5 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 6795 | // If the allocation has multiple uses, only promote it if we are strictly |
| 6796 | // increasing the alignment of the resultant allocation. If we keep it the |
| 6797 | // same, we open the door to infinite loops of various kinds. |
| 6798 | if (!AI.hasOneUse() && CastElTyAlign == AllocElTyAlign) return 0; |
| 6799 | |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 6800 | uint64_t AllocElTySize = TD->getABITypeSize(AllocElTy); |
| 6801 | uint64_t CastElTySize = TD->getABITypeSize(CastElTy); |
Chris Lattner | 0ddac2a | 2005-10-27 05:53:56 +0000 | [diff] [blame] | 6802 | if (CastElTySize == 0 || AllocElTySize == 0) return 0; |
Chris Lattner | 18e78bb | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 6803 | |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 6804 | // See if we can satisfy the modulus by pulling a scale out of the array |
| 6805 | // size argument. |
Jeff Cohen | 86796be | 2007-04-04 16:58:57 +0000 | [diff] [blame] | 6806 | unsigned ArraySizeScale; |
| 6807 | int ArrayOffset; |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 6808 | Value *NumElements = // See if the array size is a decomposable linear expr. |
| 6809 | DecomposeSimpleLinearExpr(AI.getOperand(0), ArraySizeScale, ArrayOffset); |
| 6810 | |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 6811 | // If we can now satisfy the modulus, by using a non-1 scale, we really can |
| 6812 | // do the xform. |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 6813 | if ((AllocElTySize*ArraySizeScale) % CastElTySize != 0 || |
| 6814 | (AllocElTySize*ArrayOffset ) % CastElTySize != 0) return 0; |
Chris Lattner | 8142b0a | 2005-10-27 06:12:00 +0000 | [diff] [blame] | 6815 | |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 6816 | unsigned Scale = (AllocElTySize*ArraySizeScale)/CastElTySize; |
| 6817 | Value *Amt = 0; |
| 6818 | if (Scale == 1) { |
| 6819 | Amt = NumElements; |
| 6820 | } else { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6821 | // If the allocation size is constant, form a constant mul expression |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6822 | Amt = ConstantInt::get(Type::Int32Ty, Scale); |
| 6823 | if (isa<ConstantInt>(NumElements)) |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 6824 | Amt = Multiply(cast<ConstantInt>(NumElements), cast<ConstantInt>(Amt)); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6825 | // otherwise multiply the amount and the number of elements |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 6826 | else if (Scale != 1) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6827 | Instruction *Tmp = BinaryOperator::CreateMul(Amt, NumElements, "tmp"); |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 6828 | Amt = InsertNewInstBefore(Tmp, AI); |
Chris Lattner | 8142b0a | 2005-10-27 06:12:00 +0000 | [diff] [blame] | 6829 | } |
Chris Lattner | 0ddac2a | 2005-10-27 05:53:56 +0000 | [diff] [blame] | 6830 | } |
| 6831 | |
Jeff Cohen | 86796be | 2007-04-04 16:58:57 +0000 | [diff] [blame] | 6832 | if (int Offset = (AllocElTySize*ArrayOffset)/CastElTySize) { |
| 6833 | Value *Off = ConstantInt::get(Type::Int32Ty, Offset, true); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6834 | Instruction *Tmp = BinaryOperator::CreateAdd(Amt, Off, "tmp"); |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 6835 | Amt = InsertNewInstBefore(Tmp, AI); |
| 6836 | } |
| 6837 | |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6838 | AllocationInst *New; |
| 6839 | if (isa<MallocInst>(AI)) |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6840 | New = new MallocInst(CastElTy, Amt, AI.getAlignment()); |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6841 | else |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6842 | New = new AllocaInst(CastElTy, Amt, AI.getAlignment()); |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6843 | InsertNewInstBefore(New, AI); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6844 | New->takeName(&AI); |
Chris Lattner | 39387a5 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 6845 | |
| 6846 | // If the allocation has multiple uses, insert a cast and change all things |
| 6847 | // that used it to use the new cast. This will also hack on CI, but it will |
| 6848 | // die soon. |
| 6849 | if (!AI.hasOneUse()) { |
| 6850 | AddUsesToWorkList(AI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6851 | // New is the allocation instruction, pointer typed. AI is the original |
| 6852 | // allocation instruction, also pointer typed. Thus, cast to use is BitCast. |
| 6853 | CastInst *NewCast = new BitCastInst(New, AI.getType(), "tmpcast"); |
Chris Lattner | 39387a5 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 6854 | InsertNewInstBefore(NewCast, AI); |
| 6855 | AI.replaceAllUsesWith(NewCast); |
| 6856 | } |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6857 | return ReplaceInstUsesWith(CI, New); |
| 6858 | } |
| 6859 | |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6860 | /// CanEvaluateInDifferentType - Return true if we can take the specified value |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6861 | /// and return it as type Ty without inserting any new casts and without |
| 6862 | /// changing the computed value. This is used by code that tries to decide |
| 6863 | /// whether promoting or shrinking integer operations to wider or smaller types |
| 6864 | /// will allow us to eliminate a truncate or extend. |
| 6865 | /// |
| 6866 | /// This is a truncation operation if Ty is smaller than V->getType(), or an |
| 6867 | /// extension operation if Ty is larger. |
Chris Lattner | 8114b71 | 2008-06-18 04:00:49 +0000 | [diff] [blame] | 6868 | /// |
| 6869 | /// If CastOpc is a truncation, then Ty will be a type smaller than V. We |
| 6870 | /// should return true if trunc(V) can be computed by computing V in the smaller |
| 6871 | /// type. If V is an instruction, then trunc(inst(x,y)) can be computed as |
| 6872 | /// inst(trunc(x),trunc(y)), which only makes sense if x and y can be |
| 6873 | /// efficiently truncated. |
| 6874 | /// |
| 6875 | /// If CastOpc is a sext or zext, we are asking if the low bits of the value can |
| 6876 | /// bit computed in a larger type, which is then and'd or sext_in_reg'd to get |
| 6877 | /// the final result. |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 6878 | bool InstCombiner::CanEvaluateInDifferentType(Value *V, const IntegerType *Ty, |
| 6879 | unsigned CastOpc, |
| 6880 | int &NumCastsRemoved) { |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6881 | // We can always evaluate constants in another type. |
| 6882 | if (isa<ConstantInt>(V)) |
| 6883 | return true; |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6884 | |
| 6885 | Instruction *I = dyn_cast<Instruction>(V); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6886 | if (!I) return false; |
| 6887 | |
| 6888 | const IntegerType *OrigTy = cast<IntegerType>(V->getType()); |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6889 | |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 6890 | // If this is an extension or truncate, we can often eliminate it. |
| 6891 | if (isa<TruncInst>(I) || isa<ZExtInst>(I) || isa<SExtInst>(I)) { |
| 6892 | // If this is a cast from the destination type, we can trivially eliminate |
| 6893 | // it, and this will remove a cast overall. |
| 6894 | if (I->getOperand(0)->getType() == Ty) { |
| 6895 | // If the first operand is itself a cast, and is eliminable, do not count |
| 6896 | // this as an eliminable cast. We would prefer to eliminate those two |
| 6897 | // casts first. |
Chris Lattner | 8114b71 | 2008-06-18 04:00:49 +0000 | [diff] [blame] | 6898 | if (!isa<CastInst>(I->getOperand(0)) && I->hasOneUse()) |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 6899 | ++NumCastsRemoved; |
| 6900 | return true; |
| 6901 | } |
| 6902 | } |
| 6903 | |
| 6904 | // We can't extend or shrink something that has multiple uses: doing so would |
| 6905 | // require duplicating the instruction in general, which isn't profitable. |
| 6906 | if (!I->hasOneUse()) return false; |
| 6907 | |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6908 | switch (I->getOpcode()) { |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6909 | case Instruction::Add: |
| 6910 | case Instruction::Sub: |
Nick Lewycky | b8cd6a4 | 2008-07-05 21:19:34 +0000 | [diff] [blame] | 6911 | case Instruction::Mul: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6912 | case Instruction::And: |
| 6913 | case Instruction::Or: |
| 6914 | case Instruction::Xor: |
| 6915 | // These operators can all arbitrarily be extended or truncated. |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 6916 | return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc, |
| 6917 | NumCastsRemoved) && |
| 6918 | CanEvaluateInDifferentType(I->getOperand(1), Ty, CastOpc, |
| 6919 | NumCastsRemoved); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6920 | |
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 6921 | case Instruction::Shl: |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6922 | // If we are truncating the result of this SHL, and if it's a shift of a |
| 6923 | // constant amount, we can always perform a SHL in a smaller type. |
| 6924 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 6925 | uint32_t BitWidth = Ty->getBitWidth(); |
| 6926 | if (BitWidth < OrigTy->getBitWidth() && |
| 6927 | CI->getLimitedValue(BitWidth) < BitWidth) |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 6928 | return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc, |
| 6929 | NumCastsRemoved); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6930 | } |
| 6931 | break; |
| 6932 | case Instruction::LShr: |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6933 | // If this is a truncate of a logical shr, we can truncate it to a smaller |
| 6934 | // lshr iff we know that the bits we would otherwise be shifting in are |
| 6935 | // already zeros. |
| 6936 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 6937 | uint32_t OrigBitWidth = OrigTy->getBitWidth(); |
| 6938 | uint32_t BitWidth = Ty->getBitWidth(); |
| 6939 | if (BitWidth < OrigBitWidth && |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6940 | MaskedValueIsZero(I->getOperand(0), |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 6941 | APInt::getHighBitsSet(OrigBitWidth, OrigBitWidth-BitWidth)) && |
| 6942 | CI->getLimitedValue(BitWidth) < BitWidth) { |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 6943 | return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc, |
| 6944 | NumCastsRemoved); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6945 | } |
| 6946 | } |
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 6947 | break; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6948 | case Instruction::ZExt: |
| 6949 | case Instruction::SExt: |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 6950 | case Instruction::Trunc: |
| 6951 | // 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] | 6952 | // can safely replace it. Note that replacing it does not reduce the number |
| 6953 | // of casts in the input. |
| 6954 | if (I->getOpcode() == CastOpc) |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6955 | return true; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6956 | break; |
Nick Lewycky | b8cd6a4 | 2008-07-05 21:19:34 +0000 | [diff] [blame] | 6957 | case Instruction::Select: { |
| 6958 | SelectInst *SI = cast<SelectInst>(I); |
| 6959 | return CanEvaluateInDifferentType(SI->getTrueValue(), Ty, CastOpc, |
| 6960 | NumCastsRemoved) && |
| 6961 | CanEvaluateInDifferentType(SI->getFalseValue(), Ty, CastOpc, |
| 6962 | NumCastsRemoved); |
| 6963 | } |
Chris Lattner | 8114b71 | 2008-06-18 04:00:49 +0000 | [diff] [blame] | 6964 | case Instruction::PHI: { |
| 6965 | // We can change a phi if we can change all operands. |
| 6966 | PHINode *PN = cast<PHINode>(I); |
| 6967 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 6968 | if (!CanEvaluateInDifferentType(PN->getIncomingValue(i), Ty, CastOpc, |
| 6969 | NumCastsRemoved)) |
| 6970 | return false; |
| 6971 | return true; |
| 6972 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6973 | default: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6974 | // TODO: Can handle more cases here. |
| 6975 | break; |
| 6976 | } |
| 6977 | |
| 6978 | return false; |
| 6979 | } |
| 6980 | |
| 6981 | /// EvaluateInDifferentType - Given an expression that |
| 6982 | /// CanEvaluateInDifferentType returns true for, actually insert the code to |
| 6983 | /// evaluate the expression. |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 6984 | Value *InstCombiner::EvaluateInDifferentType(Value *V, const Type *Ty, |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6985 | bool isSigned) { |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6986 | if (Constant *C = dyn_cast<Constant>(V)) |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 6987 | return ConstantExpr::getIntegerCast(C, Ty, isSigned /*Sext or ZExt*/); |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6988 | |
| 6989 | // Otherwise, it must be an instruction. |
| 6990 | Instruction *I = cast<Instruction>(V); |
Chris Lattner | 01859e8 | 2006-05-20 23:14:03 +0000 | [diff] [blame] | 6991 | Instruction *Res = 0; |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6992 | switch (I->getOpcode()) { |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6993 | case Instruction::Add: |
| 6994 | case Instruction::Sub: |
Nick Lewycky | e6b0c00 | 2008-01-22 05:08:48 +0000 | [diff] [blame] | 6995 | case Instruction::Mul: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6996 | case Instruction::And: |
| 6997 | case Instruction::Or: |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6998 | case Instruction::Xor: |
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 6999 | case Instruction::AShr: |
| 7000 | case Instruction::LShr: |
| 7001 | case Instruction::Shl: { |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 7002 | Value *LHS = EvaluateInDifferentType(I->getOperand(0), Ty, isSigned); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7003 | Value *RHS = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7004 | Res = BinaryOperator::Create((Instruction::BinaryOps)I->getOpcode(), |
Chris Lattner | 8114b71 | 2008-06-18 04:00:49 +0000 | [diff] [blame] | 7005 | LHS, RHS); |
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 7006 | break; |
| 7007 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7008 | case Instruction::Trunc: |
| 7009 | case Instruction::ZExt: |
| 7010 | case Instruction::SExt: |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7011 | // 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] | 7012 | // just return the source. There's no need to insert it because it is not |
| 7013 | // new. |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7014 | if (I->getOperand(0)->getType() == Ty) |
| 7015 | return I->getOperand(0); |
| 7016 | |
Chris Lattner | 8114b71 | 2008-06-18 04:00:49 +0000 | [diff] [blame] | 7017 | // Otherwise, must be the same type of cast, so just reinsert a new one. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7018 | Res = CastInst::Create(cast<CastInst>(I)->getOpcode(), I->getOperand(0), |
Chris Lattner | 8114b71 | 2008-06-18 04:00:49 +0000 | [diff] [blame] | 7019 | Ty); |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7020 | break; |
Nick Lewycky | b8cd6a4 | 2008-07-05 21:19:34 +0000 | [diff] [blame] | 7021 | case Instruction::Select: { |
| 7022 | Value *True = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned); |
| 7023 | Value *False = EvaluateInDifferentType(I->getOperand(2), Ty, isSigned); |
| 7024 | Res = SelectInst::Create(I->getOperand(0), True, False); |
| 7025 | break; |
| 7026 | } |
Chris Lattner | 8114b71 | 2008-06-18 04:00:49 +0000 | [diff] [blame] | 7027 | case Instruction::PHI: { |
| 7028 | PHINode *OPN = cast<PHINode>(I); |
| 7029 | PHINode *NPN = PHINode::Create(Ty); |
| 7030 | for (unsigned i = 0, e = OPN->getNumIncomingValues(); i != e; ++i) { |
| 7031 | Value *V =EvaluateInDifferentType(OPN->getIncomingValue(i), Ty, isSigned); |
| 7032 | NPN->addIncoming(V, OPN->getIncomingBlock(i)); |
| 7033 | } |
| 7034 | Res = NPN; |
| 7035 | break; |
| 7036 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7037 | default: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7038 | // TODO: Can handle more cases here. |
| 7039 | assert(0 && "Unreachable!"); |
| 7040 | break; |
| 7041 | } |
| 7042 | |
Chris Lattner | 8114b71 | 2008-06-18 04:00:49 +0000 | [diff] [blame] | 7043 | Res->takeName(I); |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7044 | return InsertNewInstBefore(Res, *I); |
| 7045 | } |
| 7046 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7047 | /// @brief Implement the transforms common to all CastInst visitors. |
| 7048 | Instruction *InstCombiner::commonCastTransforms(CastInst &CI) { |
Chris Lattner | 79d35b3 | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 7049 | Value *Src = CI.getOperand(0); |
| 7050 | |
Dan Gohman | 23d9d27 | 2007-05-11 21:10:54 +0000 | [diff] [blame] | 7051 | // Many cases of "cast of a cast" are eliminable. If it's eliminable we just |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7052 | // eliminate it now. |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 7053 | if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7054 | if (Instruction::CastOps opc = |
| 7055 | isEliminableCastPair(CSrc, CI.getOpcode(), CI.getType(), TD)) { |
| 7056 | // The first cast (CSrc) is eliminable so we need to fix up or replace |
| 7057 | // the second cast (CI). CSrc will then have a good chance of being dead. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7058 | return CastInst::Create(opc, CSrc->getOperand(0), CI.getType()); |
Chris Lattner | 8fd217c | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 7059 | } |
| 7060 | } |
Chris Lattner | a710ddc | 2004-05-25 04:29:21 +0000 | [diff] [blame] | 7061 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7062 | // If we are casting a select then fold the cast into the select |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 7063 | if (SelectInst *SI = dyn_cast<SelectInst>(Src)) |
| 7064 | if (Instruction *NV = FoldOpIntoSelect(CI, SI, this)) |
| 7065 | return NV; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7066 | |
| 7067 | // If we are casting a PHI then fold the cast into the PHI |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 7068 | if (isa<PHINode>(Src)) |
| 7069 | if (Instruction *NV = FoldOpIntoPhi(CI)) |
| 7070 | return NV; |
Chris Lattner | 9fb9213 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 7071 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7072 | return 0; |
| 7073 | } |
| 7074 | |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7075 | /// @brief Implement the transforms for cast of pointer (bitcast/ptrtoint) |
| 7076 | Instruction *InstCombiner::commonPointerCastTransforms(CastInst &CI) { |
| 7077 | Value *Src = CI.getOperand(0); |
| 7078 | |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7079 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Src)) { |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7080 | // If casting the result of a getelementptr instruction with no offset, turn |
| 7081 | // this into a cast of the original pointer! |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7082 | if (GEP->hasAllZeroIndices()) { |
| 7083 | // Changing the cast operand is usually not a good idea but it is safe |
| 7084 | // here because the pointer operand is being replaced with another |
| 7085 | // pointer operand so the opcode doesn't need to change. |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7086 | AddToWorkList(GEP); |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7087 | CI.setOperand(0, GEP->getOperand(0)); |
| 7088 | return &CI; |
| 7089 | } |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7090 | |
| 7091 | // If the GEP has a single use, and the base pointer is a bitcast, and the |
| 7092 | // GEP computes a constant offset, see if we can convert these three |
| 7093 | // instructions into fewer. This typically happens with unions and other |
| 7094 | // non-type-safe code. |
| 7095 | if (GEP->hasOneUse() && isa<BitCastInst>(GEP->getOperand(0))) { |
| 7096 | if (GEP->hasAllConstantIndices()) { |
| 7097 | // We are guaranteed to get a constant from EmitGEPOffset. |
| 7098 | ConstantInt *OffsetV = cast<ConstantInt>(EmitGEPOffset(GEP, CI, *this)); |
| 7099 | int64_t Offset = OffsetV->getSExtValue(); |
| 7100 | |
| 7101 | // Get the base pointer input of the bitcast, and the type it points to. |
| 7102 | Value *OrigBase = cast<BitCastInst>(GEP->getOperand(0))->getOperand(0); |
| 7103 | const Type *GEPIdxTy = |
| 7104 | cast<PointerType>(OrigBase->getType())->getElementType(); |
| 7105 | if (GEPIdxTy->isSized()) { |
| 7106 | SmallVector<Value*, 8> NewIndices; |
| 7107 | |
Chris Lattner | c42e226 | 2007-05-05 01:59:31 +0000 | [diff] [blame] | 7108 | // Start with the index over the outer type. Note that the type size |
| 7109 | // might be zero (even if the offset isn't zero) if the indexed type |
| 7110 | // is something like [0 x {int, int}] |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7111 | const Type *IntPtrTy = TD->getIntPtrType(); |
Chris Lattner | c42e226 | 2007-05-05 01:59:31 +0000 | [diff] [blame] | 7112 | int64_t FirstIdx = 0; |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 7113 | if (int64_t TySize = TD->getABITypeSize(GEPIdxTy)) { |
Chris Lattner | c42e226 | 2007-05-05 01:59:31 +0000 | [diff] [blame] | 7114 | FirstIdx = Offset/TySize; |
| 7115 | Offset %= TySize; |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7116 | |
Chris Lattner | c42e226 | 2007-05-05 01:59:31 +0000 | [diff] [blame] | 7117 | // Handle silly modulus not returning values values [0..TySize). |
| 7118 | if (Offset < 0) { |
| 7119 | --FirstIdx; |
| 7120 | Offset += TySize; |
| 7121 | assert(Offset >= 0); |
| 7122 | } |
Chris Lattner | d717c18 | 2007-05-05 22:32:24 +0000 | [diff] [blame] | 7123 | assert((uint64_t)Offset < (uint64_t)TySize &&"Out of range offset"); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7124 | } |
| 7125 | |
| 7126 | NewIndices.push_back(ConstantInt::get(IntPtrTy, FirstIdx)); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7127 | |
| 7128 | // Index into the types. If we fail, set OrigBase to null. |
| 7129 | while (Offset) { |
| 7130 | if (const StructType *STy = dyn_cast<StructType>(GEPIdxTy)) { |
| 7131 | const StructLayout *SL = TD->getStructLayout(STy); |
Chris Lattner | 6b6aef8 | 2007-05-15 00:16:00 +0000 | [diff] [blame] | 7132 | if (Offset < (int64_t)SL->getSizeInBytes()) { |
| 7133 | unsigned Elt = SL->getElementContainingOffset(Offset); |
| 7134 | NewIndices.push_back(ConstantInt::get(Type::Int32Ty, Elt)); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7135 | |
Chris Lattner | 6b6aef8 | 2007-05-15 00:16:00 +0000 | [diff] [blame] | 7136 | Offset -= SL->getElementOffset(Elt); |
| 7137 | GEPIdxTy = STy->getElementType(Elt); |
| 7138 | } else { |
| 7139 | // Otherwise, we can't index into this, bail out. |
| 7140 | Offset = 0; |
| 7141 | OrigBase = 0; |
| 7142 | } |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7143 | } else if (isa<ArrayType>(GEPIdxTy) || isa<VectorType>(GEPIdxTy)) { |
| 7144 | const SequentialType *STy = cast<SequentialType>(GEPIdxTy); |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 7145 | if (uint64_t EltSize = TD->getABITypeSize(STy->getElementType())){ |
Chris Lattner | 6b6aef8 | 2007-05-15 00:16:00 +0000 | [diff] [blame] | 7146 | NewIndices.push_back(ConstantInt::get(IntPtrTy,Offset/EltSize)); |
| 7147 | Offset %= EltSize; |
| 7148 | } else { |
| 7149 | NewIndices.push_back(ConstantInt::get(IntPtrTy, 0)); |
| 7150 | } |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7151 | GEPIdxTy = STy->getElementType(); |
| 7152 | } else { |
| 7153 | // Otherwise, we can't index into this, bail out. |
| 7154 | Offset = 0; |
| 7155 | OrigBase = 0; |
| 7156 | } |
| 7157 | } |
| 7158 | if (OrigBase) { |
| 7159 | // If we were able to index down into an element, create the GEP |
| 7160 | // and bitcast the result. This eliminates one bitcast, potentially |
| 7161 | // two. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 7162 | Instruction *NGEP = GetElementPtrInst::Create(OrigBase, |
| 7163 | NewIndices.begin(), |
| 7164 | NewIndices.end(), ""); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7165 | InsertNewInstBefore(NGEP, CI); |
| 7166 | NGEP->takeName(GEP); |
| 7167 | |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7168 | if (isa<BitCastInst>(CI)) |
| 7169 | return new BitCastInst(NGEP, CI.getType()); |
| 7170 | assert(isa<PtrToIntInst>(CI)); |
| 7171 | return new PtrToIntInst(NGEP, CI.getType()); |
| 7172 | } |
| 7173 | } |
| 7174 | } |
| 7175 | } |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7176 | } |
| 7177 | |
| 7178 | return commonCastTransforms(CI); |
| 7179 | } |
| 7180 | |
| 7181 | |
| 7182 | |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7183 | /// Only the TRUNC, ZEXT, SEXT, and BITCAST can both operand and result as |
| 7184 | /// integer types. This function implements the common transforms for all those |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7185 | /// cases. |
| 7186 | /// @brief Implement the transforms common to CastInst with integer operands |
| 7187 | Instruction *InstCombiner::commonIntCastTransforms(CastInst &CI) { |
| 7188 | if (Instruction *Result = commonCastTransforms(CI)) |
| 7189 | return Result; |
| 7190 | |
| 7191 | Value *Src = CI.getOperand(0); |
| 7192 | const Type *SrcTy = Src->getType(); |
| 7193 | const Type *DestTy = CI.getType(); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 7194 | uint32_t SrcBitSize = SrcTy->getPrimitiveSizeInBits(); |
| 7195 | uint32_t DestBitSize = DestTy->getPrimitiveSizeInBits(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7196 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7197 | // See if we can simplify any instructions used by the LHS whose sole |
| 7198 | // purpose is to compute bits we don't care about. |
Reid Spencer | ad6676e | 2007-03-22 20:56:53 +0000 | [diff] [blame] | 7199 | APInt KnownZero(DestBitSize, 0), KnownOne(DestBitSize, 0); |
| 7200 | if (SimplifyDemandedBits(&CI, APInt::getAllOnesValue(DestBitSize), |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7201 | KnownZero, KnownOne)) |
| 7202 | return &CI; |
| 7203 | |
| 7204 | // If the source isn't an instruction or has more than one use then we |
| 7205 | // can't do anything more. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7206 | Instruction *SrcI = dyn_cast<Instruction>(Src); |
| 7207 | if (!SrcI || !Src->hasOneUse()) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7208 | return 0; |
| 7209 | |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7210 | // Attempt to propagate the cast into the instruction for int->int casts. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7211 | int NumCastsRemoved = 0; |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7212 | if (!isa<BitCastInst>(CI) && |
| 7213 | CanEvaluateInDifferentType(SrcI, cast<IntegerType>(DestTy), |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7214 | CI.getOpcode(), NumCastsRemoved)) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7215 | // If this cast is a truncate, evaluting in a different type always |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7216 | // eliminates the cast, so it is always a win. If this is a zero-extension, |
| 7217 | // we need to do an AND to maintain the clear top-part of the computation, |
| 7218 | // so we require that the input have eliminated at least one cast. If this |
| 7219 | // is a sign extension, we insert two new casts (to do the extension) so we |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7220 | // require that two casts have been eliminated. |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7221 | bool DoXForm; |
| 7222 | switch (CI.getOpcode()) { |
| 7223 | default: |
| 7224 | // All the others use floating point so we shouldn't actually |
| 7225 | // get here because of the check above. |
| 7226 | assert(0 && "Unknown cast type"); |
| 7227 | case Instruction::Trunc: |
| 7228 | DoXForm = true; |
| 7229 | break; |
| 7230 | case Instruction::ZExt: |
| 7231 | DoXForm = NumCastsRemoved >= 1; |
| 7232 | break; |
| 7233 | case Instruction::SExt: |
| 7234 | DoXForm = NumCastsRemoved >= 2; |
| 7235 | break; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7236 | } |
| 7237 | |
| 7238 | if (DoXForm) { |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 7239 | Value *Res = EvaluateInDifferentType(SrcI, DestTy, |
| 7240 | CI.getOpcode() == Instruction::SExt); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7241 | assert(Res->getType() == DestTy); |
| 7242 | switch (CI.getOpcode()) { |
| 7243 | default: assert(0 && "Unknown cast type!"); |
| 7244 | case Instruction::Trunc: |
| 7245 | case Instruction::BitCast: |
| 7246 | // Just replace this cast with the result. |
| 7247 | return ReplaceInstUsesWith(CI, Res); |
| 7248 | case Instruction::ZExt: { |
| 7249 | // We need to emit an AND to clear the high bits. |
| 7250 | assert(SrcBitSize < DestBitSize && "Not a zext?"); |
Chris Lattner | cd1d6d5 | 2007-04-02 05:48:58 +0000 | [diff] [blame] | 7251 | Constant *C = ConstantInt::get(APInt::getLowBitsSet(DestBitSize, |
| 7252 | SrcBitSize)); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7253 | return BinaryOperator::CreateAnd(Res, C); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7254 | } |
| 7255 | case Instruction::SExt: |
| 7256 | // We need to emit a cast to truncate, then a cast to sext. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7257 | return CastInst::Create(Instruction::SExt, |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7258 | InsertCastBefore(Instruction::Trunc, Res, Src->getType(), |
| 7259 | CI), DestTy); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7260 | } |
| 7261 | } |
| 7262 | } |
| 7263 | |
| 7264 | Value *Op0 = SrcI->getNumOperands() > 0 ? SrcI->getOperand(0) : 0; |
| 7265 | Value *Op1 = SrcI->getNumOperands() > 1 ? SrcI->getOperand(1) : 0; |
| 7266 | |
| 7267 | switch (SrcI->getOpcode()) { |
| 7268 | case Instruction::Add: |
| 7269 | case Instruction::Mul: |
| 7270 | case Instruction::And: |
| 7271 | case Instruction::Or: |
| 7272 | case Instruction::Xor: |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 7273 | // If we are discarding information, rewrite. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7274 | if (DestBitSize <= SrcBitSize && DestBitSize != 1) { |
| 7275 | // Don't insert two casts if they cannot be eliminated. We allow |
| 7276 | // two casts to be inserted if the sizes are the same. This could |
| 7277 | // only be converting signedness, which is a noop. |
| 7278 | if (DestBitSize == SrcBitSize || |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7279 | !ValueRequiresCast(CI.getOpcode(), Op1, DestTy,TD) || |
| 7280 | !ValueRequiresCast(CI.getOpcode(), Op0, DestTy, TD)) { |
Reid Spencer | 7eb7638 | 2006-12-13 17:19:09 +0000 | [diff] [blame] | 7281 | Instruction::CastOps opcode = CI.getOpcode(); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7282 | Value *Op0c = InsertOperandCastBefore(opcode, Op0, DestTy, SrcI); |
| 7283 | Value *Op1c = InsertOperandCastBefore(opcode, Op1, DestTy, SrcI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7284 | return BinaryOperator::Create( |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7285 | cast<BinaryOperator>(SrcI)->getOpcode(), Op0c, Op1c); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7286 | } |
| 7287 | } |
| 7288 | |
| 7289 | // cast (xor bool X, true) to int --> xor (cast bool X to int), 1 |
| 7290 | if (isa<ZExtInst>(CI) && SrcBitSize == 1 && |
| 7291 | SrcI->getOpcode() == Instruction::Xor && |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 7292 | Op1 == ConstantInt::getTrue() && |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7293 | (!Op0->hasOneUse() || !isa<CmpInst>(Op0))) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7294 | Value *New = InsertOperandCastBefore(Instruction::ZExt, Op0, DestTy, &CI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7295 | return BinaryOperator::CreateXor(New, ConstantInt::get(CI.getType(), 1)); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7296 | } |
| 7297 | break; |
| 7298 | case Instruction::SDiv: |
| 7299 | case Instruction::UDiv: |
| 7300 | case Instruction::SRem: |
| 7301 | case Instruction::URem: |
| 7302 | // If we are just changing the sign, rewrite. |
| 7303 | if (DestBitSize == SrcBitSize) { |
| 7304 | // Don't insert two casts if they cannot be eliminated. We allow |
| 7305 | // two casts to be inserted if the sizes are the same. This could |
| 7306 | // only be converting signedness, which is a noop. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7307 | if (!ValueRequiresCast(CI.getOpcode(), Op1, DestTy, TD) || |
| 7308 | !ValueRequiresCast(CI.getOpcode(), Op0, DestTy, TD)) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7309 | Value *Op0c = InsertOperandCastBefore(Instruction::BitCast, |
| 7310 | Op0, DestTy, SrcI); |
| 7311 | Value *Op1c = InsertOperandCastBefore(Instruction::BitCast, |
| 7312 | Op1, DestTy, SrcI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7313 | return BinaryOperator::Create( |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7314 | cast<BinaryOperator>(SrcI)->getOpcode(), Op0c, Op1c); |
| 7315 | } |
| 7316 | } |
| 7317 | break; |
| 7318 | |
| 7319 | case Instruction::Shl: |
| 7320 | // Allow changing the sign of the source operand. Do not allow |
| 7321 | // changing the size of the shift, UNLESS the shift amount is a |
| 7322 | // constant. We must not change variable sized shifts to a smaller |
| 7323 | // size, because it is undefined to shift more bits out than exist |
| 7324 | // in the value. |
| 7325 | if (DestBitSize == SrcBitSize || |
| 7326 | (DestBitSize < SrcBitSize && isa<Constant>(Op1))) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7327 | Instruction::CastOps opcode = (DestBitSize == SrcBitSize ? |
| 7328 | Instruction::BitCast : Instruction::Trunc); |
| 7329 | Value *Op0c = InsertOperandCastBefore(opcode, Op0, DestTy, SrcI); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7330 | Value *Op1c = InsertOperandCastBefore(opcode, Op1, DestTy, SrcI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7331 | return BinaryOperator::CreateShl(Op0c, Op1c); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7332 | } |
| 7333 | break; |
| 7334 | case Instruction::AShr: |
| 7335 | // If this is a signed shr, and if all bits shifted in are about to be |
| 7336 | // truncated off, turn it into an unsigned shr to allow greater |
| 7337 | // simplifications. |
| 7338 | if (DestBitSize < SrcBitSize && |
| 7339 | isa<ConstantInt>(Op1)) { |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 7340 | uint32_t ShiftAmt = cast<ConstantInt>(Op1)->getLimitedValue(SrcBitSize); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7341 | if (SrcBitSize > ShiftAmt && SrcBitSize-ShiftAmt >= DestBitSize) { |
| 7342 | // Insert the new logical shift right. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7343 | return BinaryOperator::CreateLShr(Op0, Op1); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7344 | } |
| 7345 | } |
| 7346 | break; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7347 | } |
| 7348 | return 0; |
| 7349 | } |
| 7350 | |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 7351 | Instruction *InstCombiner::visitTrunc(TruncInst &CI) { |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 7352 | if (Instruction *Result = commonIntCastTransforms(CI)) |
| 7353 | return Result; |
| 7354 | |
| 7355 | Value *Src = CI.getOperand(0); |
| 7356 | const Type *Ty = CI.getType(); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 7357 | uint32_t DestBitWidth = Ty->getPrimitiveSizeInBits(); |
| 7358 | uint32_t SrcBitWidth = cast<IntegerType>(Src->getType())->getBitWidth(); |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 7359 | |
| 7360 | if (Instruction *SrcI = dyn_cast<Instruction>(Src)) { |
| 7361 | switch (SrcI->getOpcode()) { |
| 7362 | default: break; |
| 7363 | case Instruction::LShr: |
| 7364 | // We can shrink lshr to something smaller if we know the bits shifted in |
| 7365 | // are already zeros. |
| 7366 | if (ConstantInt *ShAmtV = dyn_cast<ConstantInt>(SrcI->getOperand(1))) { |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 7367 | uint32_t ShAmt = ShAmtV->getLimitedValue(SrcBitWidth); |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 7368 | |
| 7369 | // Get a mask for the bits shifting in. |
Zhou Sheng | e82fca0 | 2007-03-28 09:19:01 +0000 | [diff] [blame] | 7370 | APInt Mask(APInt::getLowBitsSet(SrcBitWidth, ShAmt).shl(DestBitWidth)); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7371 | Value* SrcIOp0 = SrcI->getOperand(0); |
| 7372 | if (SrcI->hasOneUse() && MaskedValueIsZero(SrcIOp0, Mask)) { |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 7373 | if (ShAmt >= DestBitWidth) // All zeros. |
| 7374 | return ReplaceInstUsesWith(CI, Constant::getNullValue(Ty)); |
| 7375 | |
| 7376 | // Okay, we can shrink this. Truncate the input, then return a new |
| 7377 | // shift. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7378 | Value *V1 = InsertCastBefore(Instruction::Trunc, SrcIOp0, Ty, CI); |
| 7379 | Value *V2 = InsertCastBefore(Instruction::Trunc, SrcI->getOperand(1), |
| 7380 | Ty, CI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7381 | return BinaryOperator::CreateLShr(V1, V2); |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 7382 | } |
Chris Lattner | e13ab2a | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 7383 | } else { // This is a variable shr. |
| 7384 | |
| 7385 | // Turn 'trunc (lshr X, Y) to bool' into '(X & (1 << Y)) != 0'. This is |
| 7386 | // more LLVM instructions, but allows '1 << Y' to be hoisted if |
| 7387 | // loop-invariant and CSE'd. |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 7388 | if (CI.getType() == Type::Int1Ty && SrcI->hasOneUse()) { |
Chris Lattner | e13ab2a | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 7389 | Value *One = ConstantInt::get(SrcI->getType(), 1); |
| 7390 | |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7391 | Value *V = InsertNewInstBefore( |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7392 | BinaryOperator::CreateShl(One, SrcI->getOperand(1), |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7393 | "tmp"), CI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7394 | V = InsertNewInstBefore(BinaryOperator::CreateAnd(V, |
Chris Lattner | e13ab2a | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 7395 | SrcI->getOperand(0), |
| 7396 | "tmp"), CI); |
| 7397 | Value *Zero = Constant::getNullValue(V->getType()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7398 | return new ICmpInst(ICmpInst::ICMP_NE, V, Zero); |
Chris Lattner | e13ab2a | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 7399 | } |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 7400 | } |
| 7401 | break; |
| 7402 | } |
| 7403 | } |
| 7404 | |
| 7405 | return 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7406 | } |
| 7407 | |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 7408 | /// transformZExtICmp - Transform (zext icmp) to bitwise / integer operations |
| 7409 | /// in order to eliminate the icmp. |
| 7410 | Instruction *InstCombiner::transformZExtICmp(ICmpInst *ICI, Instruction &CI, |
| 7411 | bool DoXform) { |
| 7412 | // If we are just checking for a icmp eq of a single bit and zext'ing it |
| 7413 | // to an integer, then shift the bit to the appropriate place and then |
| 7414 | // cast to integer to avoid the comparison. |
| 7415 | if (ConstantInt *Op1C = dyn_cast<ConstantInt>(ICI->getOperand(1))) { |
| 7416 | const APInt &Op1CV = Op1C->getValue(); |
| 7417 | |
| 7418 | // zext (x <s 0) to i32 --> x>>u31 true if signbit set. |
| 7419 | // zext (x >s -1) to i32 --> (x>>u31)^1 true if signbit clear. |
| 7420 | if ((ICI->getPredicate() == ICmpInst::ICMP_SLT && Op1CV == 0) || |
| 7421 | (ICI->getPredicate() == ICmpInst::ICMP_SGT &&Op1CV.isAllOnesValue())) { |
| 7422 | if (!DoXform) return ICI; |
| 7423 | |
| 7424 | Value *In = ICI->getOperand(0); |
| 7425 | Value *Sh = ConstantInt::get(In->getType(), |
| 7426 | In->getType()->getPrimitiveSizeInBits()-1); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7427 | In = InsertNewInstBefore(BinaryOperator::CreateLShr(In, Sh, |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 7428 | In->getName()+".lobit"), |
| 7429 | CI); |
| 7430 | if (In->getType() != CI.getType()) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7431 | In = CastInst::CreateIntegerCast(In, CI.getType(), |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 7432 | false/*ZExt*/, "tmp", &CI); |
| 7433 | |
| 7434 | if (ICI->getPredicate() == ICmpInst::ICMP_SGT) { |
| 7435 | Constant *One = ConstantInt::get(In->getType(), 1); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7436 | In = InsertNewInstBefore(BinaryOperator::CreateXor(In, One, |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 7437 | In->getName()+".not"), |
| 7438 | CI); |
| 7439 | } |
| 7440 | |
| 7441 | return ReplaceInstUsesWith(CI, In); |
| 7442 | } |
| 7443 | |
| 7444 | |
| 7445 | |
| 7446 | // zext (X == 0) to i32 --> X^1 iff X has only the low bit set. |
| 7447 | // zext (X == 0) to i32 --> (X>>1)^1 iff X has only the 2nd bit set. |
| 7448 | // zext (X == 1) to i32 --> X iff X has only the low bit set. |
| 7449 | // zext (X == 2) to i32 --> X>>1 iff X has only the 2nd bit set. |
| 7450 | // zext (X != 0) to i32 --> X iff X has only the low bit set. |
| 7451 | // zext (X != 0) to i32 --> X>>1 iff X has only the 2nd bit set. |
| 7452 | // zext (X != 1) to i32 --> X^1 iff X has only the low bit set. |
| 7453 | // zext (X != 2) to i32 --> (X>>1)^1 iff X has only the 2nd bit set. |
| 7454 | if ((Op1CV == 0 || Op1CV.isPowerOf2()) && |
| 7455 | // This only works for EQ and NE |
| 7456 | ICI->isEquality()) { |
| 7457 | // If Op1C some other power of two, convert: |
| 7458 | uint32_t BitWidth = Op1C->getType()->getBitWidth(); |
| 7459 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 7460 | APInt TypeMask(APInt::getAllOnesValue(BitWidth)); |
| 7461 | ComputeMaskedBits(ICI->getOperand(0), TypeMask, KnownZero, KnownOne); |
| 7462 | |
| 7463 | APInt KnownZeroMask(~KnownZero); |
| 7464 | if (KnownZeroMask.isPowerOf2()) { // Exactly 1 possible 1? |
| 7465 | if (!DoXform) return ICI; |
| 7466 | |
| 7467 | bool isNE = ICI->getPredicate() == ICmpInst::ICMP_NE; |
| 7468 | if (Op1CV != 0 && (Op1CV != KnownZeroMask)) { |
| 7469 | // (X&4) == 2 --> false |
| 7470 | // (X&4) != 2 --> true |
| 7471 | Constant *Res = ConstantInt::get(Type::Int1Ty, isNE); |
| 7472 | Res = ConstantExpr::getZExt(Res, CI.getType()); |
| 7473 | return ReplaceInstUsesWith(CI, Res); |
| 7474 | } |
| 7475 | |
| 7476 | uint32_t ShiftAmt = KnownZeroMask.logBase2(); |
| 7477 | Value *In = ICI->getOperand(0); |
| 7478 | if (ShiftAmt) { |
| 7479 | // Perform a logical shr by shiftamt. |
| 7480 | // Insert the shift to put the result in the low bit. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7481 | In = InsertNewInstBefore(BinaryOperator::CreateLShr(In, |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 7482 | ConstantInt::get(In->getType(), ShiftAmt), |
| 7483 | In->getName()+".lobit"), CI); |
| 7484 | } |
| 7485 | |
| 7486 | if ((Op1CV != 0) == isNE) { // Toggle the low bit. |
| 7487 | Constant *One = ConstantInt::get(In->getType(), 1); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7488 | In = BinaryOperator::CreateXor(In, One, "tmp"); |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 7489 | InsertNewInstBefore(cast<Instruction>(In), CI); |
| 7490 | } |
| 7491 | |
| 7492 | if (CI.getType() == In->getType()) |
| 7493 | return ReplaceInstUsesWith(CI, In); |
| 7494 | else |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7495 | return CastInst::CreateIntegerCast(In, CI.getType(), false/*ZExt*/); |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 7496 | } |
| 7497 | } |
| 7498 | } |
| 7499 | |
| 7500 | return 0; |
| 7501 | } |
| 7502 | |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 7503 | Instruction *InstCombiner::visitZExt(ZExtInst &CI) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7504 | // If one of the common conversion will work .. |
| 7505 | if (Instruction *Result = commonIntCastTransforms(CI)) |
| 7506 | return Result; |
| 7507 | |
| 7508 | Value *Src = CI.getOperand(0); |
| 7509 | |
| 7510 | // If this is a cast of a cast |
| 7511 | if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7512 | // If this is a TRUNC followed by a ZEXT then we are dealing with integral |
| 7513 | // types and if the sizes are just right we can convert this into a logical |
| 7514 | // 'and' which will be much cheaper than the pair of casts. |
| 7515 | if (isa<TruncInst>(CSrc)) { |
| 7516 | // Get the sizes of the types involved |
| 7517 | Value *A = CSrc->getOperand(0); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 7518 | uint32_t SrcSize = A->getType()->getPrimitiveSizeInBits(); |
| 7519 | uint32_t MidSize = CSrc->getType()->getPrimitiveSizeInBits(); |
| 7520 | uint32_t DstSize = CI.getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7521 | // If we're actually extending zero bits and the trunc is a no-op |
| 7522 | if (MidSize < DstSize && SrcSize == DstSize) { |
| 7523 | // Replace both of the casts with an And of the type mask. |
Zhou Sheng | e82fca0 | 2007-03-28 09:19:01 +0000 | [diff] [blame] | 7524 | APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize)); |
Reid Spencer | ad6676e | 2007-03-22 20:56:53 +0000 | [diff] [blame] | 7525 | Constant *AndConst = ConstantInt::get(AndValue); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7526 | Instruction *And = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7527 | BinaryOperator::CreateAnd(CSrc->getOperand(0), AndConst); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7528 | // Unfortunately, if the type changed, we need to cast it back. |
| 7529 | if (And->getType() != CI.getType()) { |
| 7530 | And->setName(CSrc->getName()+".mask"); |
| 7531 | InsertNewInstBefore(And, CI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7532 | And = CastInst::CreateIntegerCast(And, CI.getType(), false/*ZExt*/); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7533 | } |
| 7534 | return And; |
| 7535 | } |
| 7536 | } |
| 7537 | } |
| 7538 | |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 7539 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(Src)) |
| 7540 | return transformZExtICmp(ICI, CI); |
Chris Lattner | a2e2c9b | 2007-04-11 06:53:04 +0000 | [diff] [blame] | 7541 | |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 7542 | BinaryOperator *SrcI = dyn_cast<BinaryOperator>(Src); |
| 7543 | if (SrcI && SrcI->getOpcode() == Instruction::Or) { |
| 7544 | // zext (or icmp, icmp) --> or (zext icmp), (zext icmp) if at least one |
| 7545 | // of the (zext icmp) will be transformed. |
| 7546 | ICmpInst *LHS = dyn_cast<ICmpInst>(SrcI->getOperand(0)); |
| 7547 | ICmpInst *RHS = dyn_cast<ICmpInst>(SrcI->getOperand(1)); |
| 7548 | if (LHS && RHS && LHS->hasOneUse() && RHS->hasOneUse() && |
| 7549 | (transformZExtICmp(LHS, CI, false) || |
| 7550 | transformZExtICmp(RHS, CI, false))) { |
| 7551 | Value *LCast = InsertCastBefore(Instruction::ZExt, LHS, CI.getType(), CI); |
| 7552 | Value *RCast = InsertCastBefore(Instruction::ZExt, RHS, CI.getType(), CI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7553 | return BinaryOperator::Create(Instruction::Or, LCast, RCast); |
Chris Lattner | 66bc325 | 2007-04-11 05:45:39 +0000 | [diff] [blame] | 7554 | } |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 7555 | } |
| 7556 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7557 | return 0; |
| 7558 | } |
| 7559 | |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 7560 | Instruction *InstCombiner::visitSExt(SExtInst &CI) { |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 7561 | if (Instruction *I = commonIntCastTransforms(CI)) |
| 7562 | return I; |
| 7563 | |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 7564 | Value *Src = CI.getOperand(0); |
| 7565 | |
| 7566 | // sext (x <s 0) -> ashr x, 31 -> all ones if signed |
| 7567 | // sext (x >s -1) -> ashr x, 31 -> all ones if not signed |
| 7568 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(Src)) { |
| 7569 | // If we are just checking for a icmp eq of a single bit and zext'ing it |
| 7570 | // to an integer, then shift the bit to the appropriate place and then |
| 7571 | // cast to integer to avoid the comparison. |
| 7572 | if (ConstantInt *Op1C = dyn_cast<ConstantInt>(ICI->getOperand(1))) { |
| 7573 | const APInt &Op1CV = Op1C->getValue(); |
| 7574 | |
| 7575 | // sext (x <s 0) to i32 --> x>>s31 true if signbit set. |
| 7576 | // sext (x >s -1) to i32 --> (x>>s31)^-1 true if signbit clear. |
| 7577 | if ((ICI->getPredicate() == ICmpInst::ICMP_SLT && Op1CV == 0) || |
| 7578 | (ICI->getPredicate() == ICmpInst::ICMP_SGT &&Op1CV.isAllOnesValue())){ |
| 7579 | Value *In = ICI->getOperand(0); |
| 7580 | Value *Sh = ConstantInt::get(In->getType(), |
| 7581 | In->getType()->getPrimitiveSizeInBits()-1); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7582 | In = InsertNewInstBefore(BinaryOperator::CreateAShr(In, Sh, |
Chris Lattner | e34e9a2 | 2007-04-14 23:32:02 +0000 | [diff] [blame] | 7583 | In->getName()+".lobit"), |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 7584 | CI); |
| 7585 | if (In->getType() != CI.getType()) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7586 | In = CastInst::CreateIntegerCast(In, CI.getType(), |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 7587 | true/*SExt*/, "tmp", &CI); |
| 7588 | |
| 7589 | if (ICI->getPredicate() == ICmpInst::ICMP_SGT) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7590 | In = InsertNewInstBefore(BinaryOperator::CreateNot(In, |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 7591 | In->getName()+".not"), CI); |
| 7592 | |
| 7593 | return ReplaceInstUsesWith(CI, In); |
| 7594 | } |
| 7595 | } |
| 7596 | } |
Dan Gohman | f35c882 | 2008-05-20 21:01:12 +0000 | [diff] [blame] | 7597 | |
| 7598 | // See if the value being truncated is already sign extended. If so, just |
| 7599 | // eliminate the trunc/sext pair. |
| 7600 | if (getOpcode(Src) == Instruction::Trunc) { |
| 7601 | Value *Op = cast<User>(Src)->getOperand(0); |
| 7602 | unsigned OpBits = cast<IntegerType>(Op->getType())->getBitWidth(); |
| 7603 | unsigned MidBits = cast<IntegerType>(Src->getType())->getBitWidth(); |
| 7604 | unsigned DestBits = cast<IntegerType>(CI.getType())->getBitWidth(); |
| 7605 | unsigned NumSignBits = ComputeNumSignBits(Op); |
| 7606 | |
| 7607 | if (OpBits == DestBits) { |
| 7608 | // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign |
| 7609 | // bits, it is already ready. |
| 7610 | if (NumSignBits > DestBits-MidBits) |
| 7611 | return ReplaceInstUsesWith(CI, Op); |
| 7612 | } else if (OpBits < DestBits) { |
| 7613 | // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign |
| 7614 | // bits, just sext from i32. |
| 7615 | if (NumSignBits > OpBits-MidBits) |
| 7616 | return new SExtInst(Op, CI.getType(), "tmp"); |
| 7617 | } else { |
| 7618 | // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign |
| 7619 | // bits, just truncate to i32. |
| 7620 | if (NumSignBits > OpBits-MidBits) |
| 7621 | return new TruncInst(Op, CI.getType(), "tmp"); |
| 7622 | } |
| 7623 | } |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 7624 | |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 7625 | return 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7626 | } |
| 7627 | |
Chris Lattner | b753065 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 7628 | /// FitsInFPType - Return a Constant* for the specified FP constant if it fits |
| 7629 | /// in the specified FP type without changing its value. |
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame] | 7630 | static Constant *FitsInFPType(ConstantFP *CFP, const fltSemantics &Sem) { |
Chris Lattner | b753065 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 7631 | APFloat F = CFP->getValueAPF(); |
| 7632 | if (F.convert(Sem, APFloat::rmNearestTiesToEven) == APFloat::opOK) |
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame] | 7633 | return ConstantFP::get(F); |
Chris Lattner | b753065 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 7634 | return 0; |
| 7635 | } |
| 7636 | |
| 7637 | /// LookThroughFPExtensions - If this is an fp extension instruction, look |
| 7638 | /// through it until we get the source value. |
| 7639 | static Value *LookThroughFPExtensions(Value *V) { |
| 7640 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 7641 | if (I->getOpcode() == Instruction::FPExt) |
| 7642 | return LookThroughFPExtensions(I->getOperand(0)); |
| 7643 | |
| 7644 | // If this value is a constant, return the constant in the smallest FP type |
| 7645 | // that can accurately represent it. This allows us to turn |
| 7646 | // (float)((double)X+2.0) into x+2.0f. |
| 7647 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) { |
| 7648 | if (CFP->getType() == Type::PPC_FP128Ty) |
| 7649 | return V; // No constant folding of this. |
| 7650 | // See if the value can be truncated to float and then reextended. |
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame] | 7651 | if (Value *V = FitsInFPType(CFP, APFloat::IEEEsingle)) |
Chris Lattner | b753065 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 7652 | return V; |
| 7653 | if (CFP->getType() == Type::DoubleTy) |
| 7654 | return V; // Won't shrink. |
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame] | 7655 | if (Value *V = FitsInFPType(CFP, APFloat::IEEEdouble)) |
Chris Lattner | b753065 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 7656 | return V; |
| 7657 | // Don't try to shrink to various long double types. |
| 7658 | } |
| 7659 | |
| 7660 | return V; |
| 7661 | } |
| 7662 | |
| 7663 | Instruction *InstCombiner::visitFPTrunc(FPTruncInst &CI) { |
| 7664 | if (Instruction *I = commonCastTransforms(CI)) |
| 7665 | return I; |
| 7666 | |
| 7667 | // If we have fptrunc(add (fpextend x), (fpextend y)), where x and y are |
| 7668 | // smaller than the destination type, we can eliminate the truncate by doing |
| 7669 | // the add as the smaller type. This applies to add/sub/mul/div as well as |
| 7670 | // many builtins (sqrt, etc). |
| 7671 | BinaryOperator *OpI = dyn_cast<BinaryOperator>(CI.getOperand(0)); |
| 7672 | if (OpI && OpI->hasOneUse()) { |
| 7673 | switch (OpI->getOpcode()) { |
| 7674 | default: break; |
| 7675 | case Instruction::Add: |
| 7676 | case Instruction::Sub: |
| 7677 | case Instruction::Mul: |
| 7678 | case Instruction::FDiv: |
| 7679 | case Instruction::FRem: |
| 7680 | const Type *SrcTy = OpI->getType(); |
| 7681 | Value *LHSTrunc = LookThroughFPExtensions(OpI->getOperand(0)); |
| 7682 | Value *RHSTrunc = LookThroughFPExtensions(OpI->getOperand(1)); |
| 7683 | if (LHSTrunc->getType() != SrcTy && |
| 7684 | RHSTrunc->getType() != SrcTy) { |
| 7685 | unsigned DstSize = CI.getType()->getPrimitiveSizeInBits(); |
| 7686 | // If the source types were both smaller than the destination type of |
| 7687 | // the cast, do this xform. |
| 7688 | if (LHSTrunc->getType()->getPrimitiveSizeInBits() <= DstSize && |
| 7689 | RHSTrunc->getType()->getPrimitiveSizeInBits() <= DstSize) { |
| 7690 | LHSTrunc = InsertCastBefore(Instruction::FPExt, LHSTrunc, |
| 7691 | CI.getType(), CI); |
| 7692 | RHSTrunc = InsertCastBefore(Instruction::FPExt, RHSTrunc, |
| 7693 | CI.getType(), CI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7694 | return BinaryOperator::Create(OpI->getOpcode(), LHSTrunc, RHSTrunc); |
Chris Lattner | b753065 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 7695 | } |
| 7696 | } |
| 7697 | break; |
| 7698 | } |
| 7699 | } |
| 7700 | return 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7701 | } |
| 7702 | |
| 7703 | Instruction *InstCombiner::visitFPExt(CastInst &CI) { |
| 7704 | return commonCastTransforms(CI); |
| 7705 | } |
| 7706 | |
Chris Lattner | 0c7a9a0 | 2008-05-19 20:25:04 +0000 | [diff] [blame] | 7707 | Instruction *InstCombiner::visitFPToUI(FPToUIInst &FI) { |
| 7708 | // fptoui(uitofp(X)) --> X if the intermediate type has enough bits in its |
| 7709 | // mantissa to accurately represent all values of X. For example, do not |
| 7710 | // do this with i64->float->i64. |
| 7711 | if (UIToFPInst *SrcI = dyn_cast<UIToFPInst>(FI.getOperand(0))) |
| 7712 | if (SrcI->getOperand(0)->getType() == FI.getType() && |
| 7713 | (int)FI.getType()->getPrimitiveSizeInBits() < /*extra bit for sign */ |
Chris Lattner | 7be1c45 | 2008-05-19 21:17:23 +0000 | [diff] [blame] | 7714 | SrcI->getType()->getFPMantissaWidth()) |
Chris Lattner | 0c7a9a0 | 2008-05-19 20:25:04 +0000 | [diff] [blame] | 7715 | return ReplaceInstUsesWith(FI, SrcI->getOperand(0)); |
| 7716 | |
| 7717 | return commonCastTransforms(FI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7718 | } |
| 7719 | |
Chris Lattner | 0c7a9a0 | 2008-05-19 20:25:04 +0000 | [diff] [blame] | 7720 | Instruction *InstCombiner::visitFPToSI(FPToSIInst &FI) { |
| 7721 | // fptosi(sitofp(X)) --> X if the intermediate type has enough bits in its |
| 7722 | // mantissa to accurately represent all values of X. For example, do not |
| 7723 | // do this with i64->float->i64. |
| 7724 | if (SIToFPInst *SrcI = dyn_cast<SIToFPInst>(FI.getOperand(0))) |
| 7725 | if (SrcI->getOperand(0)->getType() == FI.getType() && |
| 7726 | (int)FI.getType()->getPrimitiveSizeInBits() <= |
Chris Lattner | 7be1c45 | 2008-05-19 21:17:23 +0000 | [diff] [blame] | 7727 | SrcI->getType()->getFPMantissaWidth()) |
Chris Lattner | 0c7a9a0 | 2008-05-19 20:25:04 +0000 | [diff] [blame] | 7728 | return ReplaceInstUsesWith(FI, SrcI->getOperand(0)); |
| 7729 | |
| 7730 | return commonCastTransforms(FI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7731 | } |
| 7732 | |
| 7733 | Instruction *InstCombiner::visitUIToFP(CastInst &CI) { |
| 7734 | return commonCastTransforms(CI); |
| 7735 | } |
| 7736 | |
| 7737 | Instruction *InstCombiner::visitSIToFP(CastInst &CI) { |
| 7738 | return commonCastTransforms(CI); |
| 7739 | } |
| 7740 | |
| 7741 | Instruction *InstCombiner::visitPtrToInt(CastInst &CI) { |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7742 | return commonPointerCastTransforms(CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7743 | } |
| 7744 | |
Chris Lattner | f9d9e45 | 2008-01-08 07:23:51 +0000 | [diff] [blame] | 7745 | Instruction *InstCombiner::visitIntToPtr(IntToPtrInst &CI) { |
| 7746 | if (Instruction *I = commonCastTransforms(CI)) |
| 7747 | return I; |
| 7748 | |
| 7749 | const Type *DestPointee = cast<PointerType>(CI.getType())->getElementType(); |
| 7750 | if (!DestPointee->isSized()) return 0; |
| 7751 | |
| 7752 | // If this is inttoptr(add (ptrtoint x), cst), try to turn this into a GEP. |
| 7753 | ConstantInt *Cst; |
| 7754 | Value *X; |
| 7755 | if (match(CI.getOperand(0), m_Add(m_Cast<PtrToIntInst>(m_Value(X)), |
| 7756 | m_ConstantInt(Cst)))) { |
| 7757 | // If the source and destination operands have the same type, see if this |
| 7758 | // is a single-index GEP. |
| 7759 | if (X->getType() == CI.getType()) { |
| 7760 | // Get the size of the pointee type. |
Bill Wendling | b9d4f8d | 2008-03-14 05:12:19 +0000 | [diff] [blame] | 7761 | uint64_t Size = TD->getABITypeSize(DestPointee); |
Chris Lattner | f9d9e45 | 2008-01-08 07:23:51 +0000 | [diff] [blame] | 7762 | |
| 7763 | // Convert the constant to intptr type. |
| 7764 | APInt Offset = Cst->getValue(); |
| 7765 | Offset.sextOrTrunc(TD->getPointerSizeInBits()); |
| 7766 | |
| 7767 | // If Offset is evenly divisible by Size, we can do this xform. |
| 7768 | if (Size && !APIntOps::srem(Offset, APInt(Offset.getBitWidth(), Size))){ |
| 7769 | Offset = APIntOps::sdiv(Offset, APInt(Offset.getBitWidth(), Size)); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 7770 | return GetElementPtrInst::Create(X, ConstantInt::get(Offset)); |
Chris Lattner | f9d9e45 | 2008-01-08 07:23:51 +0000 | [diff] [blame] | 7771 | } |
| 7772 | } |
| 7773 | // TODO: Could handle other cases, e.g. where add is indexing into field of |
| 7774 | // struct etc. |
| 7775 | } else if (CI.getOperand(0)->hasOneUse() && |
| 7776 | match(CI.getOperand(0), m_Add(m_Value(X), m_ConstantInt(Cst)))) { |
| 7777 | // Otherwise, if this is inttoptr(add x, cst), try to turn this into an |
| 7778 | // "inttoptr+GEP" instead of "add+intptr". |
| 7779 | |
| 7780 | // Get the size of the pointee type. |
| 7781 | uint64_t Size = TD->getABITypeSize(DestPointee); |
| 7782 | |
| 7783 | // Convert the constant to intptr type. |
| 7784 | APInt Offset = Cst->getValue(); |
| 7785 | Offset.sextOrTrunc(TD->getPointerSizeInBits()); |
| 7786 | |
| 7787 | // If Offset is evenly divisible by Size, we can do this xform. |
| 7788 | if (Size && !APIntOps::srem(Offset, APInt(Offset.getBitWidth(), Size))){ |
| 7789 | Offset = APIntOps::sdiv(Offset, APInt(Offset.getBitWidth(), Size)); |
| 7790 | |
| 7791 | Instruction *P = InsertNewInstBefore(new IntToPtrInst(X, CI.getType(), |
| 7792 | "tmp"), CI); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 7793 | return GetElementPtrInst::Create(P, ConstantInt::get(Offset), "tmp"); |
Chris Lattner | f9d9e45 | 2008-01-08 07:23:51 +0000 | [diff] [blame] | 7794 | } |
| 7795 | } |
| 7796 | return 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7797 | } |
| 7798 | |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7799 | Instruction *InstCombiner::visitBitCast(BitCastInst &CI) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7800 | // If the operands are integer typed then apply the integer transforms, |
| 7801 | // otherwise just apply the common ones. |
| 7802 | Value *Src = CI.getOperand(0); |
| 7803 | const Type *SrcTy = Src->getType(); |
| 7804 | const Type *DestTy = CI.getType(); |
| 7805 | |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 7806 | if (SrcTy->isInteger() && DestTy->isInteger()) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7807 | if (Instruction *Result = commonIntCastTransforms(CI)) |
| 7808 | return Result; |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7809 | } else if (isa<PointerType>(SrcTy)) { |
| 7810 | if (Instruction *I = commonPointerCastTransforms(CI)) |
| 7811 | return I; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7812 | } else { |
| 7813 | if (Instruction *Result = commonCastTransforms(CI)) |
| 7814 | return Result; |
| 7815 | } |
| 7816 | |
| 7817 | |
| 7818 | // Get rid of casts from one type to the same type. These are useless and can |
| 7819 | // be replaced by the operand. |
| 7820 | if (DestTy == Src->getType()) |
| 7821 | return ReplaceInstUsesWith(CI, Src); |
| 7822 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7823 | if (const PointerType *DstPTy = dyn_cast<PointerType>(DestTy)) { |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7824 | const PointerType *SrcPTy = cast<PointerType>(SrcTy); |
| 7825 | const Type *DstElTy = DstPTy->getElementType(); |
| 7826 | const Type *SrcElTy = SrcPTy->getElementType(); |
| 7827 | |
Nate Begeman | 83ad90a | 2008-03-31 00:22:16 +0000 | [diff] [blame] | 7828 | // If the address spaces don't match, don't eliminate the bitcast, which is |
| 7829 | // required for changing types. |
| 7830 | if (SrcPTy->getAddressSpace() != DstPTy->getAddressSpace()) |
| 7831 | return 0; |
| 7832 | |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7833 | // If we are casting a malloc or alloca to a pointer to a type of the same |
| 7834 | // size, rewrite the allocation instruction to allocate the "right" type. |
| 7835 | if (AllocationInst *AI = dyn_cast<AllocationInst>(Src)) |
| 7836 | if (Instruction *V = PromoteCastOfAllocation(CI, *AI)) |
| 7837 | return V; |
| 7838 | |
Chris Lattner | d717c18 | 2007-05-05 22:32:24 +0000 | [diff] [blame] | 7839 | // If the source and destination are pointers, and this cast is equivalent |
| 7840 | // to a getelementptr X, 0, 0, 0... turn it into the appropriate gep. |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7841 | // This can enhance SROA and other transforms that want type-safe pointers. |
| 7842 | Constant *ZeroUInt = Constant::getNullValue(Type::Int32Ty); |
| 7843 | unsigned NumZeros = 0; |
| 7844 | while (SrcElTy != DstElTy && |
| 7845 | isa<CompositeType>(SrcElTy) && !isa<PointerType>(SrcElTy) && |
| 7846 | SrcElTy->getNumContainedTypes() /* not "{}" */) { |
| 7847 | SrcElTy = cast<CompositeType>(SrcElTy)->getTypeAtIndex(ZeroUInt); |
| 7848 | ++NumZeros; |
| 7849 | } |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 7850 | |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7851 | // If we found a path from the src to dest, create the getelementptr now. |
| 7852 | if (SrcElTy == DstElTy) { |
| 7853 | SmallVector<Value*, 8> Idxs(NumZeros+1, ZeroUInt); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 7854 | return GetElementPtrInst::Create(Src, Idxs.begin(), Idxs.end(), "", |
| 7855 | ((Instruction*) NULL)); |
Chris Lattner | 9fb9213 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 7856 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7857 | } |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 7858 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7859 | if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(Src)) { |
| 7860 | if (SVI->hasOneUse()) { |
| 7861 | // Okay, we have (bitconvert (shuffle ..)). Check to see if this is |
| 7862 | // a bitconvert to a vector with the same # elts. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 7863 | if (isa<VectorType>(DestTy) && |
| 7864 | cast<VectorType>(DestTy)->getNumElements() == |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7865 | SVI->getType()->getNumElements()) { |
| 7866 | CastInst *Tmp; |
| 7867 | // If either of the operands is a cast from CI.getType(), then |
| 7868 | // evaluating the shuffle in the casted destination's type will allow |
| 7869 | // us to eliminate at least one cast. |
| 7870 | if (((Tmp = dyn_cast<CastInst>(SVI->getOperand(0))) && |
| 7871 | Tmp->getOperand(0)->getType() == DestTy) || |
| 7872 | ((Tmp = dyn_cast<CastInst>(SVI->getOperand(1))) && |
| 7873 | Tmp->getOperand(0)->getType() == DestTy)) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7874 | Value *LHS = InsertOperandCastBefore(Instruction::BitCast, |
| 7875 | SVI->getOperand(0), DestTy, &CI); |
| 7876 | Value *RHS = InsertOperandCastBefore(Instruction::BitCast, |
| 7877 | SVI->getOperand(1), DestTy, &CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7878 | // Return a new shuffle vector. Use the same element ID's, as we |
| 7879 | // know the vector types match #elts. |
| 7880 | return new ShuffleVectorInst(LHS, RHS, SVI->getOperand(2)); |
Chris Lattner | 01575b7 | 2006-05-25 23:24:33 +0000 | [diff] [blame] | 7881 | } |
| 7882 | } |
| 7883 | } |
| 7884 | } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 7885 | return 0; |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 7886 | } |
| 7887 | |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7888 | /// GetSelectFoldableOperands - We want to turn code that looks like this: |
| 7889 | /// %C = or %A, %B |
| 7890 | /// %D = select %cond, %C, %A |
| 7891 | /// into: |
| 7892 | /// %C = select %cond, %B, 0 |
| 7893 | /// %D = or %A, %C |
| 7894 | /// |
| 7895 | /// Assuming that the specified instruction is an operand to the select, return |
| 7896 | /// a bitmask indicating which operands of this instruction are foldable if they |
| 7897 | /// equal the other incoming value of the select. |
| 7898 | /// |
| 7899 | static unsigned GetSelectFoldableOperands(Instruction *I) { |
| 7900 | switch (I->getOpcode()) { |
| 7901 | case Instruction::Add: |
| 7902 | case Instruction::Mul: |
| 7903 | case Instruction::And: |
| 7904 | case Instruction::Or: |
| 7905 | case Instruction::Xor: |
| 7906 | return 3; // Can fold through either operand. |
| 7907 | case Instruction::Sub: // Can only fold on the amount subtracted. |
| 7908 | case Instruction::Shl: // Can only fold on the shift amount. |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 7909 | case Instruction::LShr: |
| 7910 | case Instruction::AShr: |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7911 | return 1; |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7912 | default: |
| 7913 | return 0; // Cannot fold |
| 7914 | } |
| 7915 | } |
| 7916 | |
| 7917 | /// GetSelectFoldableConstant - For the same transformation as the previous |
| 7918 | /// function, return the identity constant that goes into the select. |
| 7919 | static Constant *GetSelectFoldableConstant(Instruction *I) { |
| 7920 | switch (I->getOpcode()) { |
| 7921 | default: assert(0 && "This cannot happen!"); abort(); |
| 7922 | case Instruction::Add: |
| 7923 | case Instruction::Sub: |
| 7924 | case Instruction::Or: |
| 7925 | case Instruction::Xor: |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7926 | case Instruction::Shl: |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 7927 | case Instruction::LShr: |
| 7928 | case Instruction::AShr: |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7929 | return Constant::getNullValue(I->getType()); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7930 | case Instruction::And: |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 7931 | return Constant::getAllOnesValue(I->getType()); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7932 | case Instruction::Mul: |
| 7933 | return ConstantInt::get(I->getType(), 1); |
| 7934 | } |
| 7935 | } |
| 7936 | |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 7937 | /// FoldSelectOpOp - Here we have (select c, TI, FI), and we know that TI and FI |
| 7938 | /// have the same opcode and only one use each. Try to simplify this. |
| 7939 | Instruction *InstCombiner::FoldSelectOpOp(SelectInst &SI, Instruction *TI, |
| 7940 | Instruction *FI) { |
| 7941 | if (TI->getNumOperands() == 1) { |
| 7942 | // If this is a non-volatile load or a cast from the same type, |
| 7943 | // merge. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7944 | if (TI->isCast()) { |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 7945 | if (TI->getOperand(0)->getType() != FI->getOperand(0)->getType()) |
| 7946 | return 0; |
| 7947 | } else { |
| 7948 | return 0; // unknown unary op. |
| 7949 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7950 | |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 7951 | // Fold this by inserting a select from the input values. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 7952 | SelectInst *NewSI = SelectInst::Create(SI.getCondition(), TI->getOperand(0), |
| 7953 | FI->getOperand(0), SI.getName()+".v"); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 7954 | InsertNewInstBefore(NewSI, SI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7955 | return CastInst::Create(Instruction::CastOps(TI->getOpcode()), NewSI, |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7956 | TI->getType()); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 7957 | } |
| 7958 | |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7959 | // Only handle binary operators here. |
| 7960 | if (!isa<BinaryOperator>(TI)) |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 7961 | return 0; |
| 7962 | |
| 7963 | // Figure out if the operations have any operands in common. |
| 7964 | Value *MatchOp, *OtherOpT, *OtherOpF; |
| 7965 | bool MatchIsOpZero; |
| 7966 | if (TI->getOperand(0) == FI->getOperand(0)) { |
| 7967 | MatchOp = TI->getOperand(0); |
| 7968 | OtherOpT = TI->getOperand(1); |
| 7969 | OtherOpF = FI->getOperand(1); |
| 7970 | MatchIsOpZero = true; |
| 7971 | } else if (TI->getOperand(1) == FI->getOperand(1)) { |
| 7972 | MatchOp = TI->getOperand(1); |
| 7973 | OtherOpT = TI->getOperand(0); |
| 7974 | OtherOpF = FI->getOperand(0); |
| 7975 | MatchIsOpZero = false; |
| 7976 | } else if (!TI->isCommutative()) { |
| 7977 | return 0; |
| 7978 | } else if (TI->getOperand(0) == FI->getOperand(1)) { |
| 7979 | MatchOp = TI->getOperand(0); |
| 7980 | OtherOpT = TI->getOperand(1); |
| 7981 | OtherOpF = FI->getOperand(0); |
| 7982 | MatchIsOpZero = true; |
| 7983 | } else if (TI->getOperand(1) == FI->getOperand(0)) { |
| 7984 | MatchOp = TI->getOperand(1); |
| 7985 | OtherOpT = TI->getOperand(0); |
| 7986 | OtherOpF = FI->getOperand(1); |
| 7987 | MatchIsOpZero = true; |
| 7988 | } else { |
| 7989 | return 0; |
| 7990 | } |
| 7991 | |
| 7992 | // If we reach here, they do have operations in common. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 7993 | SelectInst *NewSI = SelectInst::Create(SI.getCondition(), OtherOpT, |
| 7994 | OtherOpF, SI.getName()+".v"); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 7995 | InsertNewInstBefore(NewSI, SI); |
| 7996 | |
| 7997 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TI)) { |
| 7998 | if (MatchIsOpZero) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7999 | return BinaryOperator::Create(BO->getOpcode(), MatchOp, NewSI); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8000 | else |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8001 | return BinaryOperator::Create(BO->getOpcode(), NewSI, MatchOp); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8002 | } |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 8003 | assert(0 && "Shouldn't get here"); |
| 8004 | return 0; |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8005 | } |
| 8006 | |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 8007 | Instruction *InstCombiner::visitSelectInst(SelectInst &SI) { |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 8008 | Value *CondVal = SI.getCondition(); |
| 8009 | Value *TrueVal = SI.getTrueValue(); |
| 8010 | Value *FalseVal = SI.getFalseValue(); |
| 8011 | |
| 8012 | // select true, X, Y -> X |
| 8013 | // select false, X, Y -> Y |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 8014 | if (ConstantInt *C = dyn_cast<ConstantInt>(CondVal)) |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 8015 | return ReplaceInstUsesWith(SI, C->getZExtValue() ? TrueVal : FalseVal); |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 8016 | |
| 8017 | // select C, X, X -> X |
| 8018 | if (TrueVal == FalseVal) |
| 8019 | return ReplaceInstUsesWith(SI, TrueVal); |
| 8020 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8021 | if (isa<UndefValue>(TrueVal)) // select C, undef, X -> X |
| 8022 | return ReplaceInstUsesWith(SI, FalseVal); |
| 8023 | if (isa<UndefValue>(FalseVal)) // select C, X, undef -> X |
| 8024 | return ReplaceInstUsesWith(SI, TrueVal); |
| 8025 | if (isa<UndefValue>(CondVal)) { // select undef, X, Y -> X or Y |
| 8026 | if (isa<Constant>(TrueVal)) |
| 8027 | return ReplaceInstUsesWith(SI, TrueVal); |
| 8028 | else |
| 8029 | return ReplaceInstUsesWith(SI, FalseVal); |
| 8030 | } |
| 8031 | |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 8032 | if (SI.getType() == Type::Int1Ty) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 8033 | if (ConstantInt *C = dyn_cast<ConstantInt>(TrueVal)) { |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 8034 | if (C->getZExtValue()) { |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 8035 | // Change: A = select B, true, C --> A = or B, C |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8036 | return BinaryOperator::CreateOr(CondVal, FalseVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 8037 | } else { |
| 8038 | // Change: A = select B, false, C --> A = and !B, C |
| 8039 | Value *NotCond = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8040 | InsertNewInstBefore(BinaryOperator::CreateNot(CondVal, |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 8041 | "not."+CondVal->getName()), SI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8042 | return BinaryOperator::CreateAnd(NotCond, FalseVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 8043 | } |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 8044 | } else if (ConstantInt *C = dyn_cast<ConstantInt>(FalseVal)) { |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 8045 | if (C->getZExtValue() == false) { |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 8046 | // Change: A = select B, C, false --> A = and B, C |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8047 | return BinaryOperator::CreateAnd(CondVal, TrueVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 8048 | } else { |
| 8049 | // Change: A = select B, C, true --> A = or !B, C |
| 8050 | Value *NotCond = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8051 | InsertNewInstBefore(BinaryOperator::CreateNot(CondVal, |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 8052 | "not."+CondVal->getName()), SI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8053 | return BinaryOperator::CreateOr(NotCond, TrueVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 8054 | } |
| 8055 | } |
Chris Lattner | cfa5975 | 2007-11-25 21:27:53 +0000 | [diff] [blame] | 8056 | |
| 8057 | // select a, b, a -> a&b |
| 8058 | // select a, a, b -> a|b |
| 8059 | if (CondVal == TrueVal) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8060 | return BinaryOperator::CreateOr(CondVal, FalseVal); |
Chris Lattner | cfa5975 | 2007-11-25 21:27:53 +0000 | [diff] [blame] | 8061 | else if (CondVal == FalseVal) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8062 | return BinaryOperator::CreateAnd(CondVal, TrueVal); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 8063 | } |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 8064 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 8065 | // Selecting between two integer constants? |
| 8066 | if (ConstantInt *TrueValC = dyn_cast<ConstantInt>(TrueVal)) |
| 8067 | if (ConstantInt *FalseValC = dyn_cast<ConstantInt>(FalseVal)) { |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 8068 | // select C, 1, 0 -> zext C to int |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 8069 | if (FalseValC->isZero() && TrueValC->getValue() == 1) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8070 | return CastInst::Create(Instruction::ZExt, CondVal, SI.getType()); |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 8071 | } else if (TrueValC->isZero() && FalseValC->getValue() == 1) { |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 8072 | // select C, 0, 1 -> zext !C to int |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 8073 | Value *NotCond = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8074 | InsertNewInstBefore(BinaryOperator::CreateNot(CondVal, |
Chris Lattner | 82e14fe | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 8075 | "not."+CondVal->getName()), SI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8076 | return CastInst::Create(Instruction::ZExt, NotCond, SI.getType()); |
Chris Lattner | 82e14fe | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 8077 | } |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 8078 | |
| 8079 | // FIXME: Turn select 0/-1 and -1/0 into sext from condition! |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 8080 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8081 | if (ICmpInst *IC = dyn_cast<ICmpInst>(SI.getCondition())) { |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 8082 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8083 | // (x <s 0) ? -1 : 0 -> ashr x, 31 |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 8084 | if (TrueValC->isAllOnesValue() && FalseValC->isZero()) |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 8085 | if (ConstantInt *CmpCst = dyn_cast<ConstantInt>(IC->getOperand(1))) { |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 8086 | if (IC->getPredicate() == ICmpInst::ICMP_SLT && CmpCst->isZero()) { |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 8087 | // The comparison constant and the result are not neccessarily the |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8088 | // same width. Make an all-ones value by inserting a AShr. |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 8089 | Value *X = IC->getOperand(0); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 8090 | uint32_t Bits = X->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 8091 | Constant *ShAmt = ConstantInt::get(X->getType(), Bits-1); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8092 | Instruction *SRA = BinaryOperator::Create(Instruction::AShr, X, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 8093 | ShAmt, "ones"); |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 8094 | InsertNewInstBefore(SRA, SI); |
| 8095 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8096 | // Finally, convert to the type of the select RHS. We figure out |
| 8097 | // if this requires a SExt, Trunc or BitCast based on the sizes. |
| 8098 | Instruction::CastOps opc = Instruction::BitCast; |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 8099 | uint32_t SRASize = SRA->getType()->getPrimitiveSizeInBits(); |
| 8100 | uint32_t SISize = SI.getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8101 | if (SRASize < SISize) |
| 8102 | opc = Instruction::SExt; |
| 8103 | else if (SRASize > SISize) |
| 8104 | opc = Instruction::Trunc; |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8105 | return CastInst::Create(opc, SRA, SI.getType()); |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 8106 | } |
| 8107 | } |
| 8108 | |
| 8109 | |
| 8110 | // 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] | 8111 | // 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] | 8112 | // non-constant value, eliminate this whole mess. This corresponds to |
| 8113 | // cases like this: ((X & 27) ? 27 : 0) |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 8114 | if (TrueValC->isZero() || FalseValC->isZero()) |
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 8115 | if (IC->isEquality() && isa<ConstantInt>(IC->getOperand(1)) && |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 8116 | cast<Constant>(IC->getOperand(1))->isNullValue()) |
| 8117 | if (Instruction *ICA = dyn_cast<Instruction>(IC->getOperand(0))) |
| 8118 | if (ICA->getOpcode() == Instruction::And && |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8119 | isa<ConstantInt>(ICA->getOperand(1)) && |
| 8120 | (ICA->getOperand(1) == TrueValC || |
| 8121 | ICA->getOperand(1) == FalseValC) && |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 8122 | isOneBitSet(cast<ConstantInt>(ICA->getOperand(1)))) { |
| 8123 | // 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] | 8124 | // know whether we have a icmp_ne or icmp_eq and whether the |
| 8125 | // true or false val is the zero. |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 8126 | bool ShouldNotVal = !TrueValC->isZero(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8127 | ShouldNotVal ^= IC->getPredicate() == ICmpInst::ICMP_NE; |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 8128 | Value *V = ICA; |
| 8129 | if (ShouldNotVal) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8130 | V = InsertNewInstBefore(BinaryOperator::Create( |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 8131 | Instruction::Xor, V, ICA->getOperand(1)), SI); |
| 8132 | return ReplaceInstUsesWith(SI, V); |
| 8133 | } |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 8134 | } |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 8135 | } |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8136 | |
| 8137 | // 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] | 8138 | if (FCmpInst *FCI = dyn_cast<FCmpInst>(CondVal)) { |
| 8139 | if (FCI->getOperand(0) == TrueVal && FCI->getOperand(1) == FalseVal) { |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8140 | // Transform (X == Y) ? X : Y -> Y |
Dale Johannesen | 5a2174f | 2007-10-03 17:45:27 +0000 | [diff] [blame] | 8141 | if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) { |
| 8142 | // This is not safe in general for floating point: |
| 8143 | // consider X== -0, Y== +0. |
| 8144 | // It becomes safe if either operand is a nonzero constant. |
| 8145 | ConstantFP *CFPt, *CFPf; |
| 8146 | if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) && |
| 8147 | !CFPt->getValueAPF().isZero()) || |
| 8148 | ((CFPf = dyn_cast<ConstantFP>(FalseVal)) && |
| 8149 | !CFPf->getValueAPF().isZero())) |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8150 | return ReplaceInstUsesWith(SI, FalseVal); |
Dale Johannesen | 5a2174f | 2007-10-03 17:45:27 +0000 | [diff] [blame] | 8151 | } |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8152 | // Transform (X != Y) ? X : Y -> X |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8153 | if (FCI->getPredicate() == FCmpInst::FCMP_ONE) |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8154 | return ReplaceInstUsesWith(SI, TrueVal); |
| 8155 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 8156 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8157 | } else if (FCI->getOperand(0) == FalseVal && FCI->getOperand(1) == TrueVal){ |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8158 | // Transform (X == Y) ? Y : X -> X |
Dale Johannesen | 5a2174f | 2007-10-03 17:45:27 +0000 | [diff] [blame] | 8159 | if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) { |
| 8160 | // This is not safe in general for floating point: |
| 8161 | // consider X== -0, Y== +0. |
| 8162 | // It becomes safe if either operand is a nonzero constant. |
| 8163 | ConstantFP *CFPt, *CFPf; |
| 8164 | if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) && |
| 8165 | !CFPt->getValueAPF().isZero()) || |
| 8166 | ((CFPf = dyn_cast<ConstantFP>(FalseVal)) && |
| 8167 | !CFPf->getValueAPF().isZero())) |
| 8168 | return ReplaceInstUsesWith(SI, FalseVal); |
| 8169 | } |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8170 | // Transform (X != Y) ? Y : X -> Y |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8171 | if (FCI->getPredicate() == FCmpInst::FCMP_ONE) |
| 8172 | return ReplaceInstUsesWith(SI, TrueVal); |
| 8173 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 8174 | } |
| 8175 | } |
| 8176 | |
| 8177 | // See if we are selecting two values based on a comparison of the two values. |
| 8178 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(CondVal)) { |
| 8179 | if (ICI->getOperand(0) == TrueVal && ICI->getOperand(1) == FalseVal) { |
| 8180 | // Transform (X == Y) ? X : Y -> Y |
| 8181 | if (ICI->getPredicate() == ICmpInst::ICMP_EQ) |
| 8182 | return ReplaceInstUsesWith(SI, FalseVal); |
| 8183 | // Transform (X != Y) ? X : Y -> X |
| 8184 | if (ICI->getPredicate() == ICmpInst::ICMP_NE) |
| 8185 | return ReplaceInstUsesWith(SI, TrueVal); |
| 8186 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 8187 | |
| 8188 | } else if (ICI->getOperand(0) == FalseVal && ICI->getOperand(1) == TrueVal){ |
| 8189 | // Transform (X == Y) ? Y : X -> X |
| 8190 | if (ICI->getPredicate() == ICmpInst::ICMP_EQ) |
| 8191 | return ReplaceInstUsesWith(SI, FalseVal); |
| 8192 | // Transform (X != Y) ? Y : X -> Y |
| 8193 | if (ICI->getPredicate() == ICmpInst::ICMP_NE) |
Chris Lattner | fbede52 | 2004-04-11 01:39:19 +0000 | [diff] [blame] | 8194 | return ReplaceInstUsesWith(SI, TrueVal); |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8195 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 8196 | } |
| 8197 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8198 | |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 8199 | if (Instruction *TI = dyn_cast<Instruction>(TrueVal)) |
| 8200 | if (Instruction *FI = dyn_cast<Instruction>(FalseVal)) |
| 8201 | if (TI->hasOneUse() && FI->hasOneUse()) { |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 8202 | Instruction *AddOp = 0, *SubOp = 0; |
| 8203 | |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8204 | // Turn (select C, (op X, Y), (op X, Z)) -> (op X, (select C, Y, Z)) |
| 8205 | if (TI->getOpcode() == FI->getOpcode()) |
| 8206 | if (Instruction *IV = FoldSelectOpOp(SI, TI, FI)) |
| 8207 | return IV; |
| 8208 | |
| 8209 | // Turn select C, (X+Y), (X-Y) --> (X+(select C, Y, (-Y))). This is |
| 8210 | // even legal for FP. |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 8211 | if (TI->getOpcode() == Instruction::Sub && |
| 8212 | FI->getOpcode() == Instruction::Add) { |
| 8213 | AddOp = FI; SubOp = TI; |
| 8214 | } else if (FI->getOpcode() == Instruction::Sub && |
| 8215 | TI->getOpcode() == Instruction::Add) { |
| 8216 | AddOp = TI; SubOp = FI; |
| 8217 | } |
| 8218 | |
| 8219 | if (AddOp) { |
| 8220 | Value *OtherAddOp = 0; |
| 8221 | if (SubOp->getOperand(0) == AddOp->getOperand(0)) { |
| 8222 | OtherAddOp = AddOp->getOperand(1); |
| 8223 | } else if (SubOp->getOperand(0) == AddOp->getOperand(1)) { |
| 8224 | OtherAddOp = AddOp->getOperand(0); |
| 8225 | } |
| 8226 | |
| 8227 | if (OtherAddOp) { |
Chris Lattner | 97f37a4 | 2006-02-24 18:05:58 +0000 | [diff] [blame] | 8228 | // So at this point we know we have (Y -> OtherAddOp): |
| 8229 | // select C, (add X, Y), (sub X, Z) |
| 8230 | Value *NegVal; // Compute -Z |
| 8231 | if (Constant *C = dyn_cast<Constant>(SubOp->getOperand(1))) { |
| 8232 | NegVal = ConstantExpr::getNeg(C); |
| 8233 | } else { |
| 8234 | NegVal = InsertNewInstBefore( |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8235 | BinaryOperator::CreateNeg(SubOp->getOperand(1), "tmp"), SI); |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 8236 | } |
Chris Lattner | 97f37a4 | 2006-02-24 18:05:58 +0000 | [diff] [blame] | 8237 | |
| 8238 | Value *NewTrueOp = OtherAddOp; |
| 8239 | Value *NewFalseOp = NegVal; |
| 8240 | if (AddOp != TI) |
| 8241 | std::swap(NewTrueOp, NewFalseOp); |
| 8242 | Instruction *NewSel = |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 8243 | SelectInst::Create(CondVal, NewTrueOp, |
| 8244 | NewFalseOp, SI.getName() + ".p"); |
Chris Lattner | 97f37a4 | 2006-02-24 18:05:58 +0000 | [diff] [blame] | 8245 | |
| 8246 | NewSel = InsertNewInstBefore(NewSel, SI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8247 | return BinaryOperator::CreateAdd(SubOp->getOperand(0), NewSel); |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 8248 | } |
| 8249 | } |
| 8250 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8251 | |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8252 | // See if we can fold the select into one of our operands. |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 8253 | if (SI.getType()->isInteger()) { |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8254 | // See the comment above GetSelectFoldableOperands for a description of the |
| 8255 | // transformation we are doing here. |
| 8256 | if (Instruction *TVI = dyn_cast<Instruction>(TrueVal)) |
| 8257 | if (TVI->hasOneUse() && TVI->getNumOperands() == 2 && |
| 8258 | !isa<Constant>(FalseVal)) |
| 8259 | if (unsigned SFO = GetSelectFoldableOperands(TVI)) { |
| 8260 | unsigned OpToFold = 0; |
| 8261 | if ((SFO & 1) && FalseVal == TVI->getOperand(0)) { |
| 8262 | OpToFold = 1; |
| 8263 | } else if ((SFO & 2) && FalseVal == TVI->getOperand(1)) { |
| 8264 | OpToFold = 2; |
| 8265 | } |
| 8266 | |
| 8267 | if (OpToFold) { |
| 8268 | Constant *C = GetSelectFoldableConstant(TVI); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8269 | Instruction *NewSel = |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 8270 | SelectInst::Create(SI.getCondition(), |
| 8271 | TVI->getOperand(2-OpToFold), C); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8272 | InsertNewInstBefore(NewSel, SI); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 8273 | NewSel->takeName(TVI); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8274 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TVI)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8275 | return BinaryOperator::Create(BO->getOpcode(), FalseVal, NewSel); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8276 | else { |
| 8277 | assert(0 && "Unknown instruction!!"); |
| 8278 | } |
| 8279 | } |
| 8280 | } |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 8281 | |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8282 | if (Instruction *FVI = dyn_cast<Instruction>(FalseVal)) |
| 8283 | if (FVI->hasOneUse() && FVI->getNumOperands() == 2 && |
| 8284 | !isa<Constant>(TrueVal)) |
| 8285 | if (unsigned SFO = GetSelectFoldableOperands(FVI)) { |
| 8286 | unsigned OpToFold = 0; |
| 8287 | if ((SFO & 1) && TrueVal == FVI->getOperand(0)) { |
| 8288 | OpToFold = 1; |
| 8289 | } else if ((SFO & 2) && TrueVal == FVI->getOperand(1)) { |
| 8290 | OpToFold = 2; |
| 8291 | } |
| 8292 | |
| 8293 | if (OpToFold) { |
| 8294 | Constant *C = GetSelectFoldableConstant(FVI); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8295 | Instruction *NewSel = |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 8296 | SelectInst::Create(SI.getCondition(), C, |
| 8297 | FVI->getOperand(2-OpToFold)); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8298 | InsertNewInstBefore(NewSel, SI); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 8299 | NewSel->takeName(FVI); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8300 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(FVI)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8301 | return BinaryOperator::Create(BO->getOpcode(), TrueVal, NewSel); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 8302 | else |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8303 | assert(0 && "Unknown instruction!!"); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8304 | } |
| 8305 | } |
| 8306 | } |
Chris Lattner | a1df33c | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 8307 | |
| 8308 | if (BinaryOperator::isNot(CondVal)) { |
| 8309 | SI.setOperand(0, BinaryOperator::getNotArgument(CondVal)); |
| 8310 | SI.setOperand(1, FalseVal); |
| 8311 | SI.setOperand(2, TrueVal); |
| 8312 | return &SI; |
| 8313 | } |
| 8314 | |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 8315 | return 0; |
| 8316 | } |
| 8317 | |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8318 | /// EnforceKnownAlignment - If the specified pointer points to an object that |
| 8319 | /// we control, modify the object's alignment to PrefAlign. This isn't |
| 8320 | /// often possible though. If alignment is important, a more reliable approach |
| 8321 | /// is to simply align all global variables and allocation instructions to |
| 8322 | /// their preferred alignment from the beginning. |
| 8323 | /// |
| 8324 | static unsigned EnforceKnownAlignment(Value *V, |
| 8325 | unsigned Align, unsigned PrefAlign) { |
Chris Lattner | f2369f2 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 8326 | |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8327 | User *U = dyn_cast<User>(V); |
| 8328 | if (!U) return Align; |
| 8329 | |
| 8330 | switch (getOpcode(U)) { |
| 8331 | default: break; |
| 8332 | case Instruction::BitCast: |
| 8333 | return EnforceKnownAlignment(U->getOperand(0), Align, PrefAlign); |
| 8334 | case Instruction::GetElementPtr: { |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8335 | // If all indexes are zero, it is just the alignment of the base pointer. |
| 8336 | bool AllZeroOperands = true; |
Gabor Greif | 52ed363 | 2008-06-12 21:51:29 +0000 | [diff] [blame] | 8337 | 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] | 8338 | if (!isa<Constant>(*i) || |
| 8339 | !cast<Constant>(*i)->isNullValue()) { |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8340 | AllZeroOperands = false; |
| 8341 | break; |
| 8342 | } |
Chris Lattner | f2369f2 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 8343 | |
| 8344 | if (AllZeroOperands) { |
| 8345 | // Treat this like a bitcast. |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8346 | return EnforceKnownAlignment(U->getOperand(0), Align, PrefAlign); |
Chris Lattner | f2369f2 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 8347 | } |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8348 | break; |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8349 | } |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8350 | } |
| 8351 | |
| 8352 | if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
| 8353 | // If there is a large requested alignment and we can, bump up the alignment |
| 8354 | // of the global. |
| 8355 | if (!GV->isDeclaration()) { |
| 8356 | GV->setAlignment(PrefAlign); |
| 8357 | Align = PrefAlign; |
| 8358 | } |
| 8359 | } else if (AllocationInst *AI = dyn_cast<AllocationInst>(V)) { |
| 8360 | // If there is a requested alignment and if this is an alloca, round up. We |
| 8361 | // don't do this for malloc, because some systems can't respect the request. |
| 8362 | if (isa<AllocaInst>(AI)) { |
| 8363 | AI->setAlignment(PrefAlign); |
| 8364 | Align = PrefAlign; |
| 8365 | } |
| 8366 | } |
| 8367 | |
| 8368 | return Align; |
| 8369 | } |
| 8370 | |
| 8371 | /// GetOrEnforceKnownAlignment - If the specified pointer has an alignment that |
| 8372 | /// we can determine, return it, otherwise return 0. If PrefAlign is specified, |
| 8373 | /// and it is more than the alignment of the ultimate object, see if we can |
| 8374 | /// increase the alignment of the ultimate object, making this check succeed. |
| 8375 | unsigned InstCombiner::GetOrEnforceKnownAlignment(Value *V, |
| 8376 | unsigned PrefAlign) { |
| 8377 | unsigned BitWidth = TD ? TD->getTypeSizeInBits(V->getType()) : |
| 8378 | sizeof(PrefAlign) * CHAR_BIT; |
| 8379 | APInt Mask = APInt::getAllOnesValue(BitWidth); |
| 8380 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 8381 | ComputeMaskedBits(V, Mask, KnownZero, KnownOne); |
| 8382 | unsigned TrailZ = KnownZero.countTrailingOnes(); |
| 8383 | unsigned Align = 1u << std::min(BitWidth - 1, TrailZ); |
| 8384 | |
| 8385 | if (PrefAlign > Align) |
| 8386 | Align = EnforceKnownAlignment(V, Align, PrefAlign); |
| 8387 | |
| 8388 | // We don't need to make any adjustment. |
| 8389 | return Align; |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8390 | } |
| 8391 | |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8392 | Instruction *InstCombiner::SimplifyMemTransfer(MemIntrinsic *MI) { |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8393 | unsigned DstAlign = GetOrEnforceKnownAlignment(MI->getOperand(1)); |
| 8394 | unsigned SrcAlign = GetOrEnforceKnownAlignment(MI->getOperand(2)); |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8395 | unsigned MinAlign = std::min(DstAlign, SrcAlign); |
| 8396 | unsigned CopyAlign = MI->getAlignment()->getZExtValue(); |
| 8397 | |
| 8398 | if (CopyAlign < MinAlign) { |
| 8399 | MI->setAlignment(ConstantInt::get(Type::Int32Ty, MinAlign)); |
| 8400 | return MI; |
| 8401 | } |
| 8402 | |
| 8403 | // If MemCpyInst length is 1/2/4/8 bytes then replace memcpy with |
| 8404 | // load/store. |
| 8405 | ConstantInt *MemOpLength = dyn_cast<ConstantInt>(MI->getOperand(3)); |
| 8406 | if (MemOpLength == 0) return 0; |
| 8407 | |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 8408 | // Source and destination pointer types are always "i8*" for intrinsic. See |
| 8409 | // if the size is something we can handle with a single primitive load/store. |
| 8410 | // A single load+store correctly handles overlapping memory in the memmove |
| 8411 | // case. |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8412 | unsigned Size = MemOpLength->getZExtValue(); |
Chris Lattner | 69ea9d2 | 2008-04-30 06:39:11 +0000 | [diff] [blame] | 8413 | if (Size == 0) return MI; // Delete this mem transfer. |
| 8414 | |
| 8415 | if (Size > 8 || (Size&(Size-1))) |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 8416 | return 0; // If not 1/2/4/8 bytes, exit. |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8417 | |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 8418 | // Use an integer load+store unless we can find something better. |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8419 | Type *NewPtrTy = PointerType::getUnqual(IntegerType::get(Size<<3)); |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 8420 | |
| 8421 | // Memcpy forces the use of i8* for the source and destination. That means |
| 8422 | // that if you're using memcpy to move one double around, you'll get a cast |
| 8423 | // from double* to i8*. We'd much rather use a double load+store rather than |
| 8424 | // an i64 load+store, here because this improves the odds that the source or |
| 8425 | // dest address will be promotable. See if we can find a better type than the |
| 8426 | // integer datatype. |
| 8427 | if (Value *Op = getBitCastOperand(MI->getOperand(1))) { |
| 8428 | const Type *SrcETy = cast<PointerType>(Op->getType())->getElementType(); |
| 8429 | if (SrcETy->isSized() && TD->getTypeStoreSize(SrcETy) == Size) { |
| 8430 | // The SrcETy might be something like {{{double}}} or [1 x double]. Rip |
| 8431 | // down through these levels if so. |
Dan Gohman | 8f8e269 | 2008-05-23 01:52:21 +0000 | [diff] [blame] | 8432 | while (!SrcETy->isSingleValueType()) { |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 8433 | if (const StructType *STy = dyn_cast<StructType>(SrcETy)) { |
| 8434 | if (STy->getNumElements() == 1) |
| 8435 | SrcETy = STy->getElementType(0); |
| 8436 | else |
| 8437 | break; |
| 8438 | } else if (const ArrayType *ATy = dyn_cast<ArrayType>(SrcETy)) { |
| 8439 | if (ATy->getNumElements() == 1) |
| 8440 | SrcETy = ATy->getElementType(); |
| 8441 | else |
| 8442 | break; |
| 8443 | } else |
| 8444 | break; |
| 8445 | } |
| 8446 | |
Dan Gohman | 8f8e269 | 2008-05-23 01:52:21 +0000 | [diff] [blame] | 8447 | if (SrcETy->isSingleValueType()) |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 8448 | NewPtrTy = PointerType::getUnqual(SrcETy); |
| 8449 | } |
| 8450 | } |
| 8451 | |
| 8452 | |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8453 | // If the memcpy/memmove provides better alignment info than we can |
| 8454 | // infer, use it. |
| 8455 | SrcAlign = std::max(SrcAlign, CopyAlign); |
| 8456 | DstAlign = std::max(DstAlign, CopyAlign); |
| 8457 | |
| 8458 | Value *Src = InsertBitCastBefore(MI->getOperand(2), NewPtrTy, *MI); |
| 8459 | Value *Dest = InsertBitCastBefore(MI->getOperand(1), NewPtrTy, *MI); |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 8460 | Instruction *L = new LoadInst(Src, "tmp", false, SrcAlign); |
| 8461 | InsertNewInstBefore(L, *MI); |
| 8462 | InsertNewInstBefore(new StoreInst(L, Dest, false, DstAlign), *MI); |
| 8463 | |
| 8464 | // Set the size of the copy to 0, it will be deleted on the next iteration. |
| 8465 | MI->setOperand(3, Constant::getNullValue(MemOpLength->getType())); |
| 8466 | return MI; |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8467 | } |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 8468 | |
Chris Lattner | 69ea9d2 | 2008-04-30 06:39:11 +0000 | [diff] [blame] | 8469 | Instruction *InstCombiner::SimplifyMemSet(MemSetInst *MI) { |
| 8470 | unsigned Alignment = GetOrEnforceKnownAlignment(MI->getDest()); |
| 8471 | if (MI->getAlignment()->getZExtValue() < Alignment) { |
| 8472 | MI->setAlignment(ConstantInt::get(Type::Int32Ty, Alignment)); |
| 8473 | return MI; |
| 8474 | } |
| 8475 | |
| 8476 | // Extract the length and alignment and fill if they are constant. |
| 8477 | ConstantInt *LenC = dyn_cast<ConstantInt>(MI->getLength()); |
| 8478 | ConstantInt *FillC = dyn_cast<ConstantInt>(MI->getValue()); |
| 8479 | if (!LenC || !FillC || FillC->getType() != Type::Int8Ty) |
| 8480 | return 0; |
| 8481 | uint64_t Len = LenC->getZExtValue(); |
| 8482 | Alignment = MI->getAlignment()->getZExtValue(); |
| 8483 | |
| 8484 | // If the length is zero, this is a no-op |
| 8485 | if (Len == 0) return MI; // memset(d,c,0,a) -> noop |
| 8486 | |
| 8487 | // memset(s,c,n) -> store s, c (for n=1,2,4,8) |
| 8488 | if (Len <= 8 && isPowerOf2_32((uint32_t)Len)) { |
| 8489 | const Type *ITy = IntegerType::get(Len*8); // n=1 -> i8. |
| 8490 | |
| 8491 | Value *Dest = MI->getDest(); |
| 8492 | Dest = InsertBitCastBefore(Dest, PointerType::getUnqual(ITy), *MI); |
| 8493 | |
| 8494 | // Alignment 0 is identity for alignment 1 for memset, but not store. |
| 8495 | if (Alignment == 0) Alignment = 1; |
| 8496 | |
| 8497 | // Extract the fill value and store. |
| 8498 | uint64_t Fill = FillC->getZExtValue()*0x0101010101010101ULL; |
| 8499 | InsertNewInstBefore(new StoreInst(ConstantInt::get(ITy, Fill), Dest, false, |
| 8500 | Alignment), *MI); |
| 8501 | |
| 8502 | // Set the size of the copy to 0, it will be deleted on the next iteration. |
| 8503 | MI->setLength(Constant::getNullValue(LenC->getType())); |
| 8504 | return MI; |
| 8505 | } |
| 8506 | |
| 8507 | return 0; |
| 8508 | } |
| 8509 | |
| 8510 | |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 8511 | /// visitCallInst - CallInst simplification. This mostly only handles folding |
| 8512 | /// of intrinsic instructions. For normal calls, it allows visitCallSite to do |
| 8513 | /// the heavy lifting. |
| 8514 | /// |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8515 | Instruction *InstCombiner::visitCallInst(CallInst &CI) { |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 8516 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(&CI); |
| 8517 | if (!II) return visitCallSite(&CI); |
| 8518 | |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 8519 | // Intrinsics cannot occur in an invoke, so handle them here instead of in |
| 8520 | // visitCallSite. |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 8521 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(II)) { |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8522 | bool Changed = false; |
| 8523 | |
| 8524 | // memmove/cpy/set of zero bytes is a noop. |
| 8525 | if (Constant *NumBytes = dyn_cast<Constant>(MI->getLength())) { |
| 8526 | if (NumBytes->isNullValue()) return EraseInstFromFunction(CI); |
| 8527 | |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8528 | if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes)) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8529 | if (CI->getZExtValue() == 1) { |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8530 | // Replace the instruction with just byte operations. We would |
| 8531 | // transform other cases to loads/stores, but we don't know if |
| 8532 | // alignment is sufficient. |
| 8533 | } |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 8534 | } |
| 8535 | |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8536 | // If we have a memmove and the source operation is a constant global, |
| 8537 | // then the source and dest pointers can't alias, so we can change this |
| 8538 | // into a call to memcpy. |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8539 | if (MemMoveInst *MMI = dyn_cast<MemMoveInst>(MI)) { |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8540 | if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource())) |
| 8541 | if (GVSrc->isConstant()) { |
| 8542 | Module *M = CI.getParent()->getParent()->getParent(); |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 8543 | Intrinsic::ID MemCpyID; |
| 8544 | if (CI.getOperand(3)->getType() == Type::Int32Ty) |
| 8545 | MemCpyID = Intrinsic::memcpy_i32; |
Chris Lattner | 2195939 | 2006-03-03 01:34:17 +0000 | [diff] [blame] | 8546 | else |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 8547 | MemCpyID = Intrinsic::memcpy_i64; |
| 8548 | CI.setOperand(0, Intrinsic::getDeclaration(M, MemCpyID)); |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8549 | Changed = true; |
| 8550 | } |
Chris Lattner | a935db8 | 2008-05-28 05:30:41 +0000 | [diff] [blame] | 8551 | |
| 8552 | // memmove(x,x,size) -> noop. |
| 8553 | if (MMI->getSource() == MMI->getDest()) |
| 8554 | return EraseInstFromFunction(CI); |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8555 | } |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8556 | |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8557 | // If we can determine a pointer alignment that is bigger than currently |
| 8558 | // set, update the alignment. |
| 8559 | if (isa<MemCpyInst>(MI) || isa<MemMoveInst>(MI)) { |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8560 | if (Instruction *I = SimplifyMemTransfer(MI)) |
| 8561 | return I; |
Chris Lattner | 69ea9d2 | 2008-04-30 06:39:11 +0000 | [diff] [blame] | 8562 | } else if (MemSetInst *MSI = dyn_cast<MemSetInst>(MI)) { |
| 8563 | if (Instruction *I = SimplifyMemSet(MSI)) |
| 8564 | return I; |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8565 | } |
| 8566 | |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 8567 | if (Changed) return II; |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 8568 | } |
| 8569 | |
| 8570 | switch (II->getIntrinsicID()) { |
| 8571 | default: break; |
| 8572 | case Intrinsic::bswap: |
| 8573 | // bswap(bswap(x)) -> x |
| 8574 | if (IntrinsicInst *Operand = dyn_cast<IntrinsicInst>(II->getOperand(1))) |
| 8575 | if (Operand->getIntrinsicID() == Intrinsic::bswap) |
| 8576 | return ReplaceInstUsesWith(CI, Operand->getOperand(1)); |
| 8577 | break; |
| 8578 | case Intrinsic::ppc_altivec_lvx: |
| 8579 | case Intrinsic::ppc_altivec_lvxl: |
| 8580 | case Intrinsic::x86_sse_loadu_ps: |
| 8581 | case Intrinsic::x86_sse2_loadu_pd: |
| 8582 | case Intrinsic::x86_sse2_loadu_dq: |
| 8583 | // Turn PPC lvx -> load if the pointer is known aligned. |
| 8584 | // Turn X86 loadups -> load if the pointer is known aligned. |
| 8585 | if (GetOrEnforceKnownAlignment(II->getOperand(1), 16) >= 16) { |
| 8586 | Value *Ptr = InsertBitCastBefore(II->getOperand(1), |
| 8587 | PointerType::getUnqual(II->getType()), |
| 8588 | CI); |
| 8589 | return new LoadInst(Ptr); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 8590 | } |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 8591 | break; |
| 8592 | case Intrinsic::ppc_altivec_stvx: |
| 8593 | case Intrinsic::ppc_altivec_stvxl: |
| 8594 | // Turn stvx -> store if the pointer is known aligned. |
| 8595 | if (GetOrEnforceKnownAlignment(II->getOperand(2), 16) >= 16) { |
| 8596 | const Type *OpPtrTy = |
| 8597 | PointerType::getUnqual(II->getOperand(1)->getType()); |
| 8598 | Value *Ptr = InsertBitCastBefore(II->getOperand(2), OpPtrTy, CI); |
| 8599 | return new StoreInst(II->getOperand(1), Ptr); |
| 8600 | } |
| 8601 | break; |
| 8602 | case Intrinsic::x86_sse_storeu_ps: |
| 8603 | case Intrinsic::x86_sse2_storeu_pd: |
| 8604 | case Intrinsic::x86_sse2_storeu_dq: |
| 8605 | case Intrinsic::x86_sse2_storel_dq: |
| 8606 | // Turn X86 storeu -> store if the pointer is known aligned. |
| 8607 | if (GetOrEnforceKnownAlignment(II->getOperand(1), 16) >= 16) { |
| 8608 | const Type *OpPtrTy = |
| 8609 | PointerType::getUnqual(II->getOperand(2)->getType()); |
| 8610 | Value *Ptr = InsertBitCastBefore(II->getOperand(1), OpPtrTy, CI); |
| 8611 | return new StoreInst(II->getOperand(2), Ptr); |
| 8612 | } |
| 8613 | break; |
| 8614 | |
| 8615 | case Intrinsic::x86_sse_cvttss2si: { |
| 8616 | // These intrinsics only demands the 0th element of its input vector. If |
| 8617 | // we can simplify the input based on that, do so now. |
| 8618 | uint64_t UndefElts; |
| 8619 | if (Value *V = SimplifyDemandedVectorElts(II->getOperand(1), 1, |
| 8620 | UndefElts)) { |
| 8621 | II->setOperand(1, V); |
| 8622 | return II; |
| 8623 | } |
| 8624 | break; |
| 8625 | } |
| 8626 | |
| 8627 | case Intrinsic::ppc_altivec_vperm: |
| 8628 | // Turn vperm(V1,V2,mask) -> shuffle(V1,V2,mask) if mask is a constant. |
| 8629 | if (ConstantVector *Mask = dyn_cast<ConstantVector>(II->getOperand(3))) { |
| 8630 | assert(Mask->getNumOperands() == 16 && "Bad type for intrinsic!"); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 8631 | |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 8632 | // Check that all of the elements are integer constants or undefs. |
| 8633 | bool AllEltsOk = true; |
| 8634 | for (unsigned i = 0; i != 16; ++i) { |
| 8635 | if (!isa<ConstantInt>(Mask->getOperand(i)) && |
| 8636 | !isa<UndefValue>(Mask->getOperand(i))) { |
| 8637 | AllEltsOk = false; |
| 8638 | break; |
| 8639 | } |
| 8640 | } |
| 8641 | |
| 8642 | if (AllEltsOk) { |
| 8643 | // Cast the input vectors to byte vectors. |
| 8644 | Value *Op0 =InsertBitCastBefore(II->getOperand(1),Mask->getType(),CI); |
| 8645 | Value *Op1 =InsertBitCastBefore(II->getOperand(2),Mask->getType(),CI); |
| 8646 | Value *Result = UndefValue::get(Op0->getType()); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8647 | |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 8648 | // Only extract each element once. |
| 8649 | Value *ExtractedElts[32]; |
| 8650 | memset(ExtractedElts, 0, sizeof(ExtractedElts)); |
| 8651 | |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8652 | for (unsigned i = 0; i != 16; ++i) { |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 8653 | if (isa<UndefValue>(Mask->getOperand(i))) |
| 8654 | continue; |
| 8655 | unsigned Idx=cast<ConstantInt>(Mask->getOperand(i))->getZExtValue(); |
| 8656 | Idx &= 31; // Match the hardware behavior. |
| 8657 | |
| 8658 | if (ExtractedElts[Idx] == 0) { |
| 8659 | Instruction *Elt = |
| 8660 | new ExtractElementInst(Idx < 16 ? Op0 : Op1, Idx&15, "tmp"); |
| 8661 | InsertNewInstBefore(Elt, CI); |
| 8662 | ExtractedElts[Idx] = Elt; |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8663 | } |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8664 | |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 8665 | // Insert this value into the result vector. |
| 8666 | Result = InsertElementInst::Create(Result, ExtractedElts[Idx], |
| 8667 | i, "tmp"); |
| 8668 | InsertNewInstBefore(cast<Instruction>(Result), CI); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8669 | } |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 8670 | return CastInst::Create(Instruction::BitCast, Result, CI.getType()); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8671 | } |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 8672 | } |
| 8673 | break; |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8674 | |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 8675 | case Intrinsic::stackrestore: { |
| 8676 | // If the save is right next to the restore, remove the restore. This can |
| 8677 | // happen when variable allocas are DCE'd. |
| 8678 | if (IntrinsicInst *SS = dyn_cast<IntrinsicInst>(II->getOperand(1))) { |
| 8679 | if (SS->getIntrinsicID() == Intrinsic::stacksave) { |
| 8680 | BasicBlock::iterator BI = SS; |
| 8681 | if (&*++BI == II) |
| 8682 | return EraseInstFromFunction(CI); |
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 8683 | } |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 8684 | } |
| 8685 | |
| 8686 | // Scan down this block to see if there is another stack restore in the |
| 8687 | // same block without an intervening call/alloca. |
| 8688 | BasicBlock::iterator BI = II; |
| 8689 | TerminatorInst *TI = II->getParent()->getTerminator(); |
| 8690 | bool CannotRemove = false; |
| 8691 | for (++BI; &*BI != TI; ++BI) { |
| 8692 | if (isa<AllocaInst>(BI)) { |
| 8693 | CannotRemove = true; |
| 8694 | break; |
| 8695 | } |
Chris Lattner | aa0bf52 | 2008-06-25 05:59:28 +0000 | [diff] [blame] | 8696 | if (CallInst *BCI = dyn_cast<CallInst>(BI)) { |
| 8697 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(BCI)) { |
| 8698 | // If there is a stackrestore below this one, remove this one. |
| 8699 | if (II->getIntrinsicID() == Intrinsic::stackrestore) |
| 8700 | return EraseInstFromFunction(CI); |
| 8701 | // Otherwise, ignore the intrinsic. |
| 8702 | } else { |
| 8703 | // If we found a non-intrinsic call, we can't remove the stack |
| 8704 | // restore. |
Chris Lattner | bf1d8a7 | 2008-02-18 06:12:38 +0000 | [diff] [blame] | 8705 | CannotRemove = true; |
| 8706 | break; |
| 8707 | } |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 8708 | } |
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 8709 | } |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 8710 | |
| 8711 | // If the stack restore is in a return/unwind block and if there are no |
| 8712 | // allocas or calls between the restore and the return, nuke the restore. |
| 8713 | if (!CannotRemove && (isa<ReturnInst>(TI) || isa<UnwindInst>(TI))) |
| 8714 | return EraseInstFromFunction(CI); |
| 8715 | break; |
| 8716 | } |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8717 | } |
| 8718 | |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 8719 | return visitCallSite(II); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8720 | } |
| 8721 | |
| 8722 | // InvokeInst simplification |
| 8723 | // |
| 8724 | Instruction *InstCombiner::visitInvokeInst(InvokeInst &II) { |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 8725 | return visitCallSite(&II); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8726 | } |
| 8727 | |
Dale Johannesen | da30ccb | 2008-04-25 21:16:07 +0000 | [diff] [blame] | 8728 | /// isSafeToEliminateVarargsCast - If this cast does not affect the value |
| 8729 | /// 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] | 8730 | static bool isSafeToEliminateVarargsCast(const CallSite CS, |
| 8731 | const CastInst * const CI, |
| 8732 | const TargetData * const TD, |
| 8733 | const int ix) { |
| 8734 | if (!CI->isLosslessCast()) |
| 8735 | return false; |
| 8736 | |
| 8737 | // The size of ByVal arguments is derived from the type, so we |
| 8738 | // can't change to a type with a different size. If the size were |
| 8739 | // passed explicitly we could avoid this check. |
| 8740 | if (!CS.paramHasAttr(ix, ParamAttr::ByVal)) |
| 8741 | return true; |
| 8742 | |
| 8743 | const Type* SrcTy = |
| 8744 | cast<PointerType>(CI->getOperand(0)->getType())->getElementType(); |
| 8745 | const Type* DstTy = cast<PointerType>(CI->getType())->getElementType(); |
| 8746 | if (!SrcTy->isSized() || !DstTy->isSized()) |
| 8747 | return false; |
| 8748 | if (TD->getABITypeSize(SrcTy) != TD->getABITypeSize(DstTy)) |
| 8749 | return false; |
| 8750 | return true; |
| 8751 | } |
| 8752 | |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 8753 | // visitCallSite - Improvements for call and invoke instructions. |
| 8754 | // |
| 8755 | Instruction *InstCombiner::visitCallSite(CallSite CS) { |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8756 | bool Changed = false; |
| 8757 | |
| 8758 | // If the callee is a constexpr cast of a function, attempt to move the cast |
| 8759 | // to the arguments of the call/invoke. |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 8760 | if (transformConstExprCastCall(CS)) return 0; |
| 8761 | |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8762 | Value *Callee = CS.getCalledValue(); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8763 | |
Chris Lattner | 08b22ec | 2005-05-13 07:09:09 +0000 | [diff] [blame] | 8764 | if (Function *CalleeF = dyn_cast<Function>(Callee)) |
| 8765 | if (CalleeF->getCallingConv() != CS.getCallingConv()) { |
| 8766 | Instruction *OldCall = CS.getInstruction(); |
| 8767 | // If the call and callee calling conventions don't match, this call must |
| 8768 | // be unreachable, as the call is undefined. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 8769 | new StoreInst(ConstantInt::getTrue(), |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 8770 | UndefValue::get(PointerType::getUnqual(Type::Int1Ty)), |
| 8771 | OldCall); |
Chris Lattner | 08b22ec | 2005-05-13 07:09:09 +0000 | [diff] [blame] | 8772 | if (!OldCall->use_empty()) |
| 8773 | OldCall->replaceAllUsesWith(UndefValue::get(OldCall->getType())); |
| 8774 | if (isa<CallInst>(OldCall)) // Not worth removing an invoke here. |
| 8775 | return EraseInstFromFunction(*OldCall); |
| 8776 | return 0; |
| 8777 | } |
| 8778 | |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8779 | if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) { |
| 8780 | // This instruction is not reachable, just remove it. We insert a store to |
| 8781 | // undef so that we know that this code is not reachable, despite the fact |
| 8782 | // that we can't modify the CFG here. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 8783 | new StoreInst(ConstantInt::getTrue(), |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 8784 | UndefValue::get(PointerType::getUnqual(Type::Int1Ty)), |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8785 | CS.getInstruction()); |
| 8786 | |
| 8787 | if (!CS.getInstruction()->use_empty()) |
| 8788 | CS.getInstruction()-> |
| 8789 | replaceAllUsesWith(UndefValue::get(CS.getInstruction()->getType())); |
| 8790 | |
| 8791 | if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) { |
| 8792 | // Don't break the CFG, insert a dummy cond branch. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 8793 | BranchInst::Create(II->getNormalDest(), II->getUnwindDest(), |
| 8794 | ConstantInt::getTrue(), II); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8795 | } |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8796 | return EraseInstFromFunction(*CS.getInstruction()); |
| 8797 | } |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8798 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8799 | if (BitCastInst *BC = dyn_cast<BitCastInst>(Callee)) |
| 8800 | if (IntrinsicInst *In = dyn_cast<IntrinsicInst>(BC->getOperand(0))) |
| 8801 | if (In->getIntrinsicID() == Intrinsic::init_trampoline) |
| 8802 | return transformCallThroughTrampoline(CS); |
| 8803 | |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8804 | const PointerType *PTy = cast<PointerType>(Callee->getType()); |
| 8805 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
| 8806 | if (FTy->isVarArg()) { |
Dale Johannesen | 63e7eb4 | 2008-04-23 01:03:05 +0000 | [diff] [blame] | 8807 | int ix = FTy->getNumParams() + (isa<InvokeInst>(Callee) ? 3 : 1); |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8808 | // See if we can optimize any arguments passed through the varargs area of |
| 8809 | // the call. |
| 8810 | for (CallSite::arg_iterator I = CS.arg_begin()+FTy->getNumParams(), |
Dale Johannesen | 1f530a5 | 2008-04-23 18:34:37 +0000 | [diff] [blame] | 8811 | E = CS.arg_end(); I != E; ++I, ++ix) { |
| 8812 | CastInst *CI = dyn_cast<CastInst>(*I); |
| 8813 | if (CI && isSafeToEliminateVarargsCast(CS, CI, TD, ix)) { |
| 8814 | *I = CI->getOperand(0); |
| 8815 | Changed = true; |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8816 | } |
Dale Johannesen | 1f530a5 | 2008-04-23 18:34:37 +0000 | [diff] [blame] | 8817 | } |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8818 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8819 | |
Duncan Sands | f0c3354 | 2007-12-19 21:13:37 +0000 | [diff] [blame] | 8820 | if (isa<InlineAsm>(Callee) && !CS.doesNotThrow()) { |
Duncan Sands | ece2c04 | 2007-12-16 15:51:49 +0000 | [diff] [blame] | 8821 | // Inline asm calls cannot throw - mark them 'nounwind'. |
Duncan Sands | f0c3354 | 2007-12-19 21:13:37 +0000 | [diff] [blame] | 8822 | CS.setDoesNotThrow(); |
Duncan Sands | ece2c04 | 2007-12-16 15:51:49 +0000 | [diff] [blame] | 8823 | Changed = true; |
| 8824 | } |
| 8825 | |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8826 | return Changed ? CS.getInstruction() : 0; |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 8827 | } |
| 8828 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8829 | // transformConstExprCastCall - If the callee is a constexpr cast of a function, |
| 8830 | // attempt to move the cast to the arguments of the call/invoke. |
| 8831 | // |
| 8832 | bool InstCombiner::transformConstExprCastCall(CallSite CS) { |
| 8833 | if (!isa<ConstantExpr>(CS.getCalledValue())) return false; |
| 8834 | ConstantExpr *CE = cast<ConstantExpr>(CS.getCalledValue()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8835 | if (CE->getOpcode() != Instruction::BitCast || |
| 8836 | !isa<Function>(CE->getOperand(0))) |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8837 | return false; |
Reid Spencer | 8863f18 | 2004-07-18 00:38:32 +0000 | [diff] [blame] | 8838 | Function *Callee = cast<Function>(CE->getOperand(0)); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8839 | Instruction *Caller = CS.getInstruction(); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8840 | const PAListPtr &CallerPAL = CS.getParamAttrs(); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8841 | |
| 8842 | // Okay, this is a cast from a function to a different type. Unless doing so |
| 8843 | // would cause a type conversion of one of our arguments, change this call to |
| 8844 | // be a direct call with arguments casted to the appropriate types. |
| 8845 | // |
| 8846 | const FunctionType *FT = Callee->getFunctionType(); |
| 8847 | const Type *OldRetTy = Caller->getType(); |
Duncan Sands | f413cdf | 2008-06-01 07:38:42 +0000 | [diff] [blame] | 8848 | const Type *NewRetTy = FT->getReturnType(); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8849 | |
Duncan Sands | f413cdf | 2008-06-01 07:38:42 +0000 | [diff] [blame] | 8850 | if (isa<StructType>(NewRetTy)) |
Devang Patel | 75e6f02 | 2008-03-11 18:04:06 +0000 | [diff] [blame] | 8851 | return false; // TODO: Handle multiple return values. |
| 8852 | |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 8853 | // Check to see if we are changing the return type... |
Duncan Sands | f413cdf | 2008-06-01 07:38:42 +0000 | [diff] [blame] | 8854 | if (OldRetTy != NewRetTy) { |
Bill Wendling | a6c3112 | 2008-05-14 22:45:20 +0000 | [diff] [blame] | 8855 | if (Callee->isDeclaration() && |
Duncan Sands | f413cdf | 2008-06-01 07:38:42 +0000 | [diff] [blame] | 8856 | // Conversion is ok if changing from one pointer type to another or from |
| 8857 | // a pointer to an integer of the same size. |
| 8858 | !((isa<PointerType>(OldRetTy) || OldRetTy == TD->getIntPtrType()) && |
Duncan Sands | 34b176a | 2008-06-17 15:55:30 +0000 | [diff] [blame] | 8859 | (isa<PointerType>(NewRetTy) || NewRetTy == TD->getIntPtrType()))) |
Chris Lattner | ec47992 | 2007-01-06 02:09:32 +0000 | [diff] [blame] | 8860 | return false; // Cannot transform this return value. |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 8861 | |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8862 | if (!Caller->use_empty() && |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8863 | // void -> non-void is handled specially |
Duncan Sands | f413cdf | 2008-06-01 07:38:42 +0000 | [diff] [blame] | 8864 | NewRetTy != Type::VoidTy && !CastInst::isCastable(NewRetTy, OldRetTy)) |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8865 | return false; // Cannot transform this return value. |
| 8866 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8867 | if (!CallerPAL.isEmpty() && !Caller->use_empty()) { |
| 8868 | ParameterAttributes RAttrs = CallerPAL.getParamAttrs(0); |
Duncan Sands | f413cdf | 2008-06-01 07:38:42 +0000 | [diff] [blame] | 8869 | if (RAttrs & ParamAttr::typeIncompatible(NewRetTy)) |
Duncan Sands | 6c3470e | 2008-01-07 17:16:06 +0000 | [diff] [blame] | 8870 | return false; // Attribute not compatible with transformed value. |
| 8871 | } |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8872 | |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 8873 | // If the callsite is an invoke instruction, and the return value is used by |
| 8874 | // a PHI node in a successor, we cannot change the return type of the call |
| 8875 | // because there is no place to put the cast instruction (without breaking |
| 8876 | // the critical edge). Bail out in this case. |
| 8877 | if (!Caller->use_empty()) |
| 8878 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) |
| 8879 | for (Value::use_iterator UI = II->use_begin(), E = II->use_end(); |
| 8880 | UI != E; ++UI) |
| 8881 | if (PHINode *PN = dyn_cast<PHINode>(*UI)) |
| 8882 | if (PN->getParent() == II->getNormalDest() || |
Chris Lattner | aeb2a1d | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 8883 | PN->getParent() == II->getUnwindDest()) |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 8884 | return false; |
| 8885 | } |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8886 | |
| 8887 | unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin()); |
| 8888 | unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8889 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8890 | CallSite::arg_iterator AI = CS.arg_begin(); |
| 8891 | for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) { |
| 8892 | const Type *ParamTy = FT->getParamType(i); |
Andrew Lenharth | b8e604c | 2006-06-28 01:01:52 +0000 | [diff] [blame] | 8893 | const Type *ActTy = (*AI)->getType(); |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8894 | |
| 8895 | if (!CastInst::isCastable(ActTy, ParamTy)) |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8896 | return false; // Cannot transform this parameter value. |
| 8897 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8898 | if (CallerPAL.getParamAttrs(i + 1) & ParamAttr::typeIncompatible(ParamTy)) |
| 8899 | return false; // Attribute not compatible with transformed value. |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8900 | |
Duncan Sands | f413cdf | 2008-06-01 07:38:42 +0000 | [diff] [blame] | 8901 | // Converting from one pointer type to another or between a pointer and an |
| 8902 | // 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] | 8903 | bool isConvertible = ActTy == ParamTy || |
Duncan Sands | f413cdf | 2008-06-01 07:38:42 +0000 | [diff] [blame] | 8904 | ((isa<PointerType>(ParamTy) || ParamTy == TD->getIntPtrType()) && |
| 8905 | (isa<PointerType>(ActTy) || ActTy == TD->getIntPtrType())); |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 8906 | if (Callee->isDeclaration() && !isConvertible) return false; |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8907 | } |
| 8908 | |
| 8909 | if (FT->getNumParams() < NumActualArgs && !FT->isVarArg() && |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 8910 | Callee->isDeclaration()) |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8911 | return false; // Do not delete arguments unless we have a function body. |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8912 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8913 | if (FT->getNumParams() < NumActualArgs && FT->isVarArg() && |
| 8914 | !CallerPAL.isEmpty()) |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8915 | // 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] | 8916 | // won't be dropping them. Check that these extra arguments have attributes |
| 8917 | // that are compatible with being a vararg call argument. |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8918 | for (unsigned i = CallerPAL.getNumSlots(); i; --i) { |
| 8919 | if (CallerPAL.getSlot(i - 1).Index <= FT->getNumParams()) |
Duncan Sands | e1e520f | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 8920 | break; |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8921 | ParameterAttributes PAttrs = CallerPAL.getSlot(i - 1).Attrs; |
Duncan Sands | e1e520f | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 8922 | if (PAttrs & ParamAttr::VarArgsIncompatible) |
| 8923 | return false; |
| 8924 | } |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8925 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8926 | // Okay, we decided that this is a safe thing to do: go ahead and start |
| 8927 | // inserting cast instructions as necessary... |
| 8928 | std::vector<Value*> Args; |
| 8929 | Args.reserve(NumActualArgs); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8930 | SmallVector<ParamAttrsWithIndex, 8> attrVec; |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8931 | attrVec.reserve(NumCommonArgs); |
| 8932 | |
| 8933 | // Get any return attributes. |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8934 | ParameterAttributes RAttrs = CallerPAL.getParamAttrs(0); |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8935 | |
| 8936 | // If the return value is not being used, the type may not be compatible |
| 8937 | // with the existing attributes. Wipe out any problematic attributes. |
Duncan Sands | f413cdf | 2008-06-01 07:38:42 +0000 | [diff] [blame] | 8938 | RAttrs &= ~ParamAttr::typeIncompatible(NewRetTy); |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8939 | |
| 8940 | // Add the new return attributes. |
| 8941 | if (RAttrs) |
| 8942 | attrVec.push_back(ParamAttrsWithIndex::get(0, RAttrs)); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8943 | |
| 8944 | AI = CS.arg_begin(); |
| 8945 | for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) { |
| 8946 | const Type *ParamTy = FT->getParamType(i); |
| 8947 | if ((*AI)->getType() == ParamTy) { |
| 8948 | Args.push_back(*AI); |
| 8949 | } else { |
Reid Spencer | 8a903db | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 8950 | Instruction::CastOps opcode = CastInst::getCastOpcode(*AI, |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8951 | false, ParamTy, false); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8952 | CastInst *NewCast = CastInst::Create(opcode, *AI, ParamTy, "tmp"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8953 | Args.push_back(InsertNewInstBefore(NewCast, *Caller)); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8954 | } |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8955 | |
| 8956 | // Add any parameter attributes. |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8957 | if (ParameterAttributes PAttrs = CallerPAL.getParamAttrs(i + 1)) |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8958 | attrVec.push_back(ParamAttrsWithIndex::get(i + 1, PAttrs)); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8959 | } |
| 8960 | |
| 8961 | // If the function takes more arguments than the call was taking, add them |
| 8962 | // now... |
| 8963 | for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i) |
| 8964 | Args.push_back(Constant::getNullValue(FT->getParamType(i))); |
| 8965 | |
| 8966 | // If we are removing arguments to the function, emit an obnoxious warning... |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 8967 | if (FT->getNumParams() < NumActualArgs) { |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8968 | if (!FT->isVarArg()) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 8969 | cerr << "WARNING: While resolving call to function '" |
| 8970 | << Callee->getName() << "' arguments were dropped!\n"; |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8971 | } else { |
| 8972 | // Add all of the arguments in their promoted form to the arg list... |
| 8973 | for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) { |
| 8974 | const Type *PTy = getPromotedType((*AI)->getType()); |
| 8975 | if (PTy != (*AI)->getType()) { |
| 8976 | // Must promote to pass through va_arg area! |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8977 | Instruction::CastOps opcode = CastInst::getCastOpcode(*AI, false, |
| 8978 | PTy, false); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8979 | Instruction *Cast = CastInst::Create(opcode, *AI, PTy, "tmp"); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8980 | InsertNewInstBefore(Cast, *Caller); |
| 8981 | Args.push_back(Cast); |
| 8982 | } else { |
| 8983 | Args.push_back(*AI); |
| 8984 | } |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8985 | |
Duncan Sands | e1e520f | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 8986 | // Add any parameter attributes. |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8987 | if (ParameterAttributes PAttrs = CallerPAL.getParamAttrs(i + 1)) |
Duncan Sands | e1e520f | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 8988 | attrVec.push_back(ParamAttrsWithIndex::get(i + 1, PAttrs)); |
| 8989 | } |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8990 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 8991 | } |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8992 | |
Duncan Sands | f413cdf | 2008-06-01 07:38:42 +0000 | [diff] [blame] | 8993 | if (NewRetTy == Type::VoidTy) |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 8994 | Caller->setName(""); // Void type should not have a name. |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8995 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8996 | const PAListPtr &NewCallerPAL = PAListPtr::get(attrVec.begin(),attrVec.end()); |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8997 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8998 | Instruction *NC; |
| 8999 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9000 | NC = InvokeInst::Create(Callee, II->getNormalDest(), II->getUnwindDest(), |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 9001 | Args.begin(), Args.end(), |
| 9002 | Caller->getName(), Caller); |
Reid Spencer | ed3fa85 | 2007-07-30 19:53:57 +0000 | [diff] [blame] | 9003 | cast<InvokeInst>(NC)->setCallingConv(II->getCallingConv()); |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 9004 | cast<InvokeInst>(NC)->setParamAttrs(NewCallerPAL); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9005 | } else { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9006 | NC = CallInst::Create(Callee, Args.begin(), Args.end(), |
| 9007 | Caller->getName(), Caller); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 9008 | CallInst *CI = cast<CallInst>(Caller); |
| 9009 | if (CI->isTailCall()) |
Chris Lattner | a9e9211 | 2005-05-06 06:48:21 +0000 | [diff] [blame] | 9010 | cast<CallInst>(NC)->setTailCall(); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 9011 | cast<CallInst>(NC)->setCallingConv(CI->getCallingConv()); |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 9012 | cast<CallInst>(NC)->setParamAttrs(NewCallerPAL); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9013 | } |
| 9014 | |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 9015 | // Insert a cast of the return type as necessary. |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9016 | Value *NV = NC; |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 9017 | if (OldRetTy != NV->getType() && !Caller->use_empty()) { |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9018 | if (NV->getType() != Type::VoidTy) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9019 | Instruction::CastOps opcode = CastInst::getCastOpcode(NC, false, |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 9020 | OldRetTy, false); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9021 | NV = NC = CastInst::Create(opcode, NC, OldRetTy, "tmp"); |
Chris Lattner | bb60904 | 2003-10-30 00:46:41 +0000 | [diff] [blame] | 9022 | |
| 9023 | // If this is an invoke instruction, we should insert it after the first |
| 9024 | // non-phi, instruction in the normal successor block. |
| 9025 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
Dan Gohman | 02dea8b | 2008-05-23 21:05:58 +0000 | [diff] [blame] | 9026 | BasicBlock::iterator I = II->getNormalDest()->getFirstNonPHI(); |
Chris Lattner | bb60904 | 2003-10-30 00:46:41 +0000 | [diff] [blame] | 9027 | InsertNewInstBefore(NC, *I); |
| 9028 | } else { |
| 9029 | // Otherwise, it's a call, just insert cast right after the call instr |
| 9030 | InsertNewInstBefore(NC, *Caller); |
| 9031 | } |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 9032 | AddUsersToWorkList(*Caller); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9033 | } else { |
Chris Lattner | c30bda7 | 2004-10-17 21:22:38 +0000 | [diff] [blame] | 9034 | NV = UndefValue::get(Caller->getType()); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9035 | } |
| 9036 | } |
| 9037 | |
| 9038 | if (Caller->getType() != Type::VoidTy && !Caller->use_empty()) |
| 9039 | Caller->replaceAllUsesWith(NV); |
Chris Lattner | f22a5c6 | 2007-03-02 19:59:19 +0000 | [diff] [blame] | 9040 | Caller->eraseFromParent(); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9041 | RemoveFromWorkList(Caller); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9042 | return true; |
| 9043 | } |
| 9044 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9045 | // transformCallThroughTrampoline - Turn a call to a function created by the |
| 9046 | // init_trampoline intrinsic into a direct call to the underlying function. |
| 9047 | // |
| 9048 | Instruction *InstCombiner::transformCallThroughTrampoline(CallSite CS) { |
| 9049 | Value *Callee = CS.getCalledValue(); |
| 9050 | const PointerType *PTy = cast<PointerType>(Callee->getType()); |
| 9051 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9052 | const PAListPtr &Attrs = CS.getParamAttrs(); |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9053 | |
| 9054 | // If the call already has the 'nest' attribute somewhere then give up - |
| 9055 | // otherwise 'nest' would occur twice after splicing in the chain. |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9056 | if (Attrs.hasAttrSomewhere(ParamAttr::Nest)) |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9057 | return 0; |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9058 | |
| 9059 | IntrinsicInst *Tramp = |
| 9060 | cast<IntrinsicInst>(cast<BitCastInst>(Callee)->getOperand(0)); |
| 9061 | |
Anton Korobeynikov | 0b12ecf | 2008-05-07 22:54:15 +0000 | [diff] [blame] | 9062 | Function *NestF = cast<Function>(Tramp->getOperand(2)->stripPointerCasts()); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9063 | const PointerType *NestFPTy = cast<PointerType>(NestF->getType()); |
| 9064 | const FunctionType *NestFTy = cast<FunctionType>(NestFPTy->getElementType()); |
| 9065 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9066 | const PAListPtr &NestAttrs = NestF->getParamAttrs(); |
| 9067 | if (!NestAttrs.isEmpty()) { |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9068 | unsigned NestIdx = 1; |
| 9069 | const Type *NestTy = 0; |
Dale Johannesen | 0d51e7e | 2008-02-19 21:38:47 +0000 | [diff] [blame] | 9070 | ParameterAttributes NestAttr = ParamAttr::None; |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9071 | |
| 9072 | // Look for a parameter marked with the 'nest' attribute. |
| 9073 | for (FunctionType::param_iterator I = NestFTy->param_begin(), |
| 9074 | E = NestFTy->param_end(); I != E; ++NestIdx, ++I) |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9075 | if (NestAttrs.paramHasAttr(NestIdx, ParamAttr::Nest)) { |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9076 | // Record the parameter type and any other attributes. |
| 9077 | NestTy = *I; |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9078 | NestAttr = NestAttrs.getParamAttrs(NestIdx); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9079 | break; |
| 9080 | } |
| 9081 | |
| 9082 | if (NestTy) { |
| 9083 | Instruction *Caller = CS.getInstruction(); |
| 9084 | std::vector<Value*> NewArgs; |
| 9085 | NewArgs.reserve(unsigned(CS.arg_end()-CS.arg_begin())+1); |
| 9086 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9087 | SmallVector<ParamAttrsWithIndex, 8> NewAttrs; |
| 9088 | NewAttrs.reserve(Attrs.getNumSlots() + 1); |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9089 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9090 | // Insert the nest argument into the call argument list, which may |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9091 | // mean appending it. Likewise for attributes. |
| 9092 | |
| 9093 | // Add any function result attributes. |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9094 | if (ParameterAttributes Attr = Attrs.getParamAttrs(0)) |
| 9095 | NewAttrs.push_back(ParamAttrsWithIndex::get(0, Attr)); |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9096 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9097 | { |
| 9098 | unsigned Idx = 1; |
| 9099 | CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); |
| 9100 | do { |
| 9101 | if (Idx == NestIdx) { |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9102 | // Add the chain argument and attributes. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9103 | Value *NestVal = Tramp->getOperand(3); |
| 9104 | if (NestVal->getType() != NestTy) |
| 9105 | NestVal = new BitCastInst(NestVal, NestTy, "nest", Caller); |
| 9106 | NewArgs.push_back(NestVal); |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9107 | NewAttrs.push_back(ParamAttrsWithIndex::get(NestIdx, NestAttr)); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9108 | } |
| 9109 | |
| 9110 | if (I == E) |
| 9111 | break; |
| 9112 | |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9113 | // Add the original argument and attributes. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9114 | NewArgs.push_back(*I); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9115 | if (ParameterAttributes Attr = Attrs.getParamAttrs(Idx)) |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9116 | NewAttrs.push_back |
| 9117 | (ParamAttrsWithIndex::get(Idx + (Idx >= NestIdx), Attr)); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9118 | |
| 9119 | ++Idx, ++I; |
| 9120 | } while (1); |
| 9121 | } |
| 9122 | |
| 9123 | // The trampoline may have been bitcast to a bogus type (FTy). |
| 9124 | // Handle this by synthesizing a new function type, equal to FTy |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9125 | // with the chain parameter inserted. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9126 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9127 | std::vector<const Type*> NewTypes; |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9128 | NewTypes.reserve(FTy->getNumParams()+1); |
| 9129 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9130 | // 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] | 9131 | // mean appending it. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9132 | { |
| 9133 | unsigned Idx = 1; |
| 9134 | FunctionType::param_iterator I = FTy->param_begin(), |
| 9135 | E = FTy->param_end(); |
| 9136 | |
| 9137 | do { |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9138 | if (Idx == NestIdx) |
| 9139 | // Add the chain's type. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9140 | NewTypes.push_back(NestTy); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9141 | |
| 9142 | if (I == E) |
| 9143 | break; |
| 9144 | |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9145 | // Add the original type. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9146 | NewTypes.push_back(*I); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9147 | |
| 9148 | ++Idx, ++I; |
| 9149 | } while (1); |
| 9150 | } |
| 9151 | |
| 9152 | // Replace the trampoline call with a direct call. Let the generic |
| 9153 | // code sort out any function type mismatches. |
| 9154 | FunctionType *NewFTy = |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 9155 | FunctionType::get(FTy->getReturnType(), NewTypes, FTy->isVarArg()); |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 9156 | Constant *NewCallee = NestF->getType() == PointerType::getUnqual(NewFTy) ? |
| 9157 | NestF : ConstantExpr::getBitCast(NestF, PointerType::getUnqual(NewFTy)); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9158 | const PAListPtr &NewPAL = PAListPtr::get(NewAttrs.begin(),NewAttrs.end()); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9159 | |
| 9160 | Instruction *NewCaller; |
| 9161 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9162 | NewCaller = InvokeInst::Create(NewCallee, |
| 9163 | II->getNormalDest(), II->getUnwindDest(), |
| 9164 | NewArgs.begin(), NewArgs.end(), |
| 9165 | Caller->getName(), Caller); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9166 | cast<InvokeInst>(NewCaller)->setCallingConv(II->getCallingConv()); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 9167 | cast<InvokeInst>(NewCaller)->setParamAttrs(NewPAL); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9168 | } else { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9169 | NewCaller = CallInst::Create(NewCallee, NewArgs.begin(), NewArgs.end(), |
| 9170 | Caller->getName(), Caller); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9171 | if (cast<CallInst>(Caller)->isTailCall()) |
| 9172 | cast<CallInst>(NewCaller)->setTailCall(); |
| 9173 | cast<CallInst>(NewCaller)-> |
| 9174 | setCallingConv(cast<CallInst>(Caller)->getCallingConv()); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 9175 | cast<CallInst>(NewCaller)->setParamAttrs(NewPAL); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9176 | } |
| 9177 | if (Caller->getType() != Type::VoidTy && !Caller->use_empty()) |
| 9178 | Caller->replaceAllUsesWith(NewCaller); |
| 9179 | Caller->eraseFromParent(); |
| 9180 | RemoveFromWorkList(Caller); |
| 9181 | return 0; |
| 9182 | } |
| 9183 | } |
| 9184 | |
| 9185 | // Replace the trampoline call with a direct call. Since there is no 'nest' |
| 9186 | // parameter, there is no need to adjust the argument list. Let the generic |
| 9187 | // code sort out any function type mismatches. |
| 9188 | Constant *NewCallee = |
| 9189 | NestF->getType() == PTy ? NestF : ConstantExpr::getBitCast(NestF, PTy); |
| 9190 | CS.setCalledFunction(NewCallee); |
| 9191 | return CS.getInstruction(); |
| 9192 | } |
| 9193 | |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9194 | /// FoldPHIArgBinOpIntoPHI - If we have something like phi [add (a,b), add(c,d)] |
| 9195 | /// and if a/b/c/d and the add's all have a single use, turn this into two phi's |
| 9196 | /// and a single binop. |
| 9197 | Instruction *InstCombiner::FoldPHIArgBinOpIntoPHI(PHINode &PN) { |
| 9198 | Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0)); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 9199 | assert(isa<BinaryOperator>(FirstInst) || isa<GetElementPtrInst>(FirstInst) || |
| 9200 | isa<CmpInst>(FirstInst)); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9201 | unsigned Opc = FirstInst->getOpcode(); |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 9202 | Value *LHSVal = FirstInst->getOperand(0); |
| 9203 | Value *RHSVal = FirstInst->getOperand(1); |
| 9204 | |
| 9205 | const Type *LHSType = LHSVal->getType(); |
| 9206 | const Type *RHSType = RHSVal->getType(); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9207 | |
| 9208 | // Scan to see if all operands are the same opcode, all have one use, and all |
| 9209 | // kill their operands (i.e. the operands have one use). |
Chris Lattner | a90a24c | 2006-11-01 04:55:47 +0000 | [diff] [blame] | 9210 | for (unsigned i = 0; i != PN.getNumIncomingValues(); ++i) { |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9211 | Instruction *I = dyn_cast<Instruction>(PN.getIncomingValue(i)); |
Chris Lattner | a90a24c | 2006-11-01 04:55:47 +0000 | [diff] [blame] | 9212 | if (!I || I->getOpcode() != Opc || !I->hasOneUse() || |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9213 | // 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] | 9214 | // types or GEP's with different index types. |
| 9215 | I->getOperand(0)->getType() != LHSType || |
| 9216 | I->getOperand(1)->getType() != RHSType) |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9217 | return 0; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9218 | |
| 9219 | // If they are CmpInst instructions, check their predicates |
| 9220 | if (Opc == Instruction::ICmp || Opc == Instruction::FCmp) |
| 9221 | if (cast<CmpInst>(I)->getPredicate() != |
| 9222 | cast<CmpInst>(FirstInst)->getPredicate()) |
| 9223 | return 0; |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 9224 | |
| 9225 | // Keep track of which operand needs a phi node. |
| 9226 | if (I->getOperand(0) != LHSVal) LHSVal = 0; |
| 9227 | if (I->getOperand(1) != RHSVal) RHSVal = 0; |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9228 | } |
| 9229 | |
Chris Lattner | 53738a4 | 2006-11-08 19:42:28 +0000 | [diff] [blame] | 9230 | // Otherwise, this is safe to transform, determine if it is profitable. |
| 9231 | |
| 9232 | // If this is a GEP, and if the index (not the pointer) needs a PHI, bail out. |
| 9233 | // Indexes are often folded into load/store instructions, so we don't want to |
| 9234 | // hide them behind a phi. |
| 9235 | if (isa<GetElementPtrInst>(FirstInst) && RHSVal == 0) |
| 9236 | return 0; |
| 9237 | |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9238 | Value *InLHS = FirstInst->getOperand(0); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9239 | Value *InRHS = FirstInst->getOperand(1); |
Chris Lattner | 53738a4 | 2006-11-08 19:42:28 +0000 | [diff] [blame] | 9240 | PHINode *NewLHS = 0, *NewRHS = 0; |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 9241 | if (LHSVal == 0) { |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 9242 | NewLHS = PHINode::Create(LHSType, |
| 9243 | FirstInst->getOperand(0)->getName() + ".pn"); |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 9244 | NewLHS->reserveOperandSpace(PN.getNumOperands()/2); |
| 9245 | NewLHS->addIncoming(InLHS, PN.getIncomingBlock(0)); |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 9246 | InsertNewInstBefore(NewLHS, PN); |
| 9247 | LHSVal = NewLHS; |
| 9248 | } |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 9249 | |
| 9250 | if (RHSVal == 0) { |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 9251 | NewRHS = PHINode::Create(RHSType, |
| 9252 | FirstInst->getOperand(1)->getName() + ".pn"); |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 9253 | NewRHS->reserveOperandSpace(PN.getNumOperands()/2); |
| 9254 | NewRHS->addIncoming(InRHS, PN.getIncomingBlock(0)); |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 9255 | InsertNewInstBefore(NewRHS, PN); |
| 9256 | RHSVal = NewRHS; |
| 9257 | } |
| 9258 | |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 9259 | // Add all operands to the new PHIs. |
| 9260 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 9261 | if (NewLHS) { |
| 9262 | Value *NewInLHS =cast<Instruction>(PN.getIncomingValue(i))->getOperand(0); |
| 9263 | NewLHS->addIncoming(NewInLHS, PN.getIncomingBlock(i)); |
| 9264 | } |
| 9265 | if (NewRHS) { |
| 9266 | Value *NewInRHS =cast<Instruction>(PN.getIncomingValue(i))->getOperand(1); |
| 9267 | NewRHS->addIncoming(NewInRHS, PN.getIncomingBlock(i)); |
| 9268 | } |
| 9269 | } |
| 9270 | |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9271 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9272 | return BinaryOperator::Create(BinOp->getOpcode(), LHSVal, RHSVal); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9273 | else if (CmpInst *CIOp = dyn_cast<CmpInst>(FirstInst)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9274 | return CmpInst::Create(CIOp->getOpcode(), CIOp->getPredicate(), LHSVal, |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9275 | RHSVal); |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 9276 | else { |
| 9277 | assert(isa<GetElementPtrInst>(FirstInst)); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9278 | return GetElementPtrInst::Create(LHSVal, RHSVal); |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 9279 | } |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9280 | } |
| 9281 | |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 9282 | /// isSafeToSinkLoad - Return true if we know that it is safe sink the load out |
| 9283 | /// of the block that defines it. This means that it must be obvious the value |
| 9284 | /// of the load is not changed from the point of the load to the end of the |
| 9285 | /// block it is in. |
Chris Lattner | fd905ca | 2007-02-01 22:30:07 +0000 | [diff] [blame] | 9286 | /// |
| 9287 | /// Finally, it is safe, but not profitable, to sink a load targetting a |
| 9288 | /// non-address-taken alloca. Doing so will cause us to not promote the alloca |
| 9289 | /// to a register. |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 9290 | static bool isSafeToSinkLoad(LoadInst *L) { |
| 9291 | BasicBlock::iterator BBI = L, E = L->getParent()->end(); |
| 9292 | |
| 9293 | for (++BBI; BBI != E; ++BBI) |
| 9294 | if (BBI->mayWriteToMemory()) |
| 9295 | return false; |
Chris Lattner | fd905ca | 2007-02-01 22:30:07 +0000 | [diff] [blame] | 9296 | |
| 9297 | // Check for non-address taken alloca. If not address-taken already, it isn't |
| 9298 | // profitable to do this xform. |
| 9299 | if (AllocaInst *AI = dyn_cast<AllocaInst>(L->getOperand(0))) { |
| 9300 | bool isAddressTaken = false; |
| 9301 | for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); |
| 9302 | UI != E; ++UI) { |
| 9303 | if (isa<LoadInst>(UI)) continue; |
| 9304 | if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) { |
| 9305 | // If storing TO the alloca, then the address isn't taken. |
| 9306 | if (SI->getOperand(1) == AI) continue; |
| 9307 | } |
| 9308 | isAddressTaken = true; |
| 9309 | break; |
| 9310 | } |
| 9311 | |
| 9312 | if (!isAddressTaken) |
| 9313 | return false; |
| 9314 | } |
| 9315 | |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 9316 | return true; |
| 9317 | } |
| 9318 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9319 | |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9320 | // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary" |
| 9321 | // operator and they all are only used by the PHI, PHI together their |
| 9322 | // inputs, and do the operation once, to the result of the PHI. |
| 9323 | Instruction *InstCombiner::FoldPHIArgOpIntoPHI(PHINode &PN) { |
| 9324 | Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0)); |
| 9325 | |
| 9326 | // Scan the instruction, looking for input operations that can be folded away. |
| 9327 | // If all input operands to the phi are the same instruction (e.g. a cast from |
| 9328 | // the same type or "+42") we can pull the operation through the PHI, reducing |
| 9329 | // code size and simplifying code. |
| 9330 | Constant *ConstantOp = 0; |
| 9331 | const Type *CastSrcTy = 0; |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 9332 | bool isVolatile = false; |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9333 | if (isa<CastInst>(FirstInst)) { |
| 9334 | CastSrcTy = FirstInst->getOperand(0)->getType(); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 9335 | } else if (isa<BinaryOperator>(FirstInst) || isa<CmpInst>(FirstInst)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9336 | // Can fold binop, compare or shift here if the RHS is a constant, |
| 9337 | // otherwise call FoldPHIArgBinOpIntoPHI. |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9338 | ConstantOp = dyn_cast<Constant>(FirstInst->getOperand(1)); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9339 | if (ConstantOp == 0) |
| 9340 | return FoldPHIArgBinOpIntoPHI(PN); |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 9341 | } else if (LoadInst *LI = dyn_cast<LoadInst>(FirstInst)) { |
| 9342 | isVolatile = LI->isVolatile(); |
| 9343 | // We can't sink the load if the loaded value could be modified between the |
| 9344 | // load and the PHI. |
| 9345 | if (LI->getParent() != PN.getIncomingBlock(0) || |
| 9346 | !isSafeToSinkLoad(LI)) |
| 9347 | return 0; |
Chris Lattner | 7104296 | 2008-07-08 17:18:32 +0000 | [diff] [blame] | 9348 | |
| 9349 | // If the PHI is of volatile loads and the load block has multiple |
| 9350 | // successors, sinking it would remove a load of the volatile value from |
| 9351 | // the path through the other successor. |
| 9352 | if (isVolatile && |
| 9353 | LI->getParent()->getTerminator()->getNumSuccessors() != 1) |
| 9354 | return 0; |
| 9355 | |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 9356 | } else if (isa<GetElementPtrInst>(FirstInst)) { |
Chris Lattner | 53738a4 | 2006-11-08 19:42:28 +0000 | [diff] [blame] | 9357 | if (FirstInst->getNumOperands() == 2) |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 9358 | return FoldPHIArgBinOpIntoPHI(PN); |
| 9359 | // Can't handle general GEPs yet. |
| 9360 | return 0; |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9361 | } else { |
| 9362 | return 0; // Cannot fold this operation. |
| 9363 | } |
| 9364 | |
| 9365 | // Check to see if all arguments are the same operation. |
| 9366 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 9367 | if (!isa<Instruction>(PN.getIncomingValue(i))) return 0; |
| 9368 | Instruction *I = cast<Instruction>(PN.getIncomingValue(i)); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9369 | if (!I->hasOneUse() || !I->isSameOperationAs(FirstInst)) |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9370 | return 0; |
| 9371 | if (CastSrcTy) { |
| 9372 | if (I->getOperand(0)->getType() != CastSrcTy) |
| 9373 | return 0; // Cast operation must match. |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 9374 | } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9375 | // We can't sink the load if the loaded value could be modified between |
| 9376 | // the load and the PHI. |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 9377 | if (LI->isVolatile() != isVolatile || |
| 9378 | LI->getParent() != PN.getIncomingBlock(i) || |
| 9379 | !isSafeToSinkLoad(LI)) |
| 9380 | return 0; |
Chris Lattner | 40700fe | 2008-04-29 17:28:22 +0000 | [diff] [blame] | 9381 | |
Chris Lattner | 7104296 | 2008-07-08 17:18:32 +0000 | [diff] [blame] | 9382 | // If the PHI is of volatile loads and the load block has multiple |
| 9383 | // successors, sinking it would remove a load of the volatile value from |
| 9384 | // the path through the other successor. |
Chris Lattner | 40700fe | 2008-04-29 17:28:22 +0000 | [diff] [blame] | 9385 | if (isVolatile && |
| 9386 | LI->getParent()->getTerminator()->getNumSuccessors() != 1) |
| 9387 | return 0; |
| 9388 | |
| 9389 | |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9390 | } else if (I->getOperand(1) != ConstantOp) { |
| 9391 | return 0; |
| 9392 | } |
| 9393 | } |
| 9394 | |
| 9395 | // Okay, they are all the same operation. Create a new PHI node of the |
| 9396 | // 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] | 9397 | PHINode *NewPN = PHINode::Create(FirstInst->getOperand(0)->getType(), |
| 9398 | PN.getName()+".in"); |
Chris Lattner | 5551706 | 2005-01-29 00:39:08 +0000 | [diff] [blame] | 9399 | NewPN->reserveOperandSpace(PN.getNumOperands()/2); |
Chris Lattner | b589344 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 9400 | |
| 9401 | Value *InVal = FirstInst->getOperand(0); |
| 9402 | NewPN->addIncoming(InVal, PN.getIncomingBlock(0)); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9403 | |
| 9404 | // Add all operands to the new PHI. |
Chris Lattner | b589344 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 9405 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 9406 | Value *NewInVal = cast<Instruction>(PN.getIncomingValue(i))->getOperand(0); |
| 9407 | if (NewInVal != InVal) |
| 9408 | InVal = 0; |
| 9409 | NewPN->addIncoming(NewInVal, PN.getIncomingBlock(i)); |
| 9410 | } |
| 9411 | |
| 9412 | Value *PhiVal; |
| 9413 | if (InVal) { |
| 9414 | // The new PHI unions all of the same values together. This is really |
| 9415 | // common, so we handle it intelligently here for compile-time speed. |
| 9416 | PhiVal = InVal; |
| 9417 | delete NewPN; |
| 9418 | } else { |
| 9419 | InsertNewInstBefore(NewPN, PN); |
| 9420 | PhiVal = NewPN; |
| 9421 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9422 | |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9423 | // Insert and return the new operation. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9424 | if (CastInst* FirstCI = dyn_cast<CastInst>(FirstInst)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9425 | return CastInst::Create(FirstCI->getOpcode(), PhiVal, PN.getType()); |
Chris Lattner | 54545ac | 2008-04-29 17:13:43 +0000 | [diff] [blame] | 9426 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9427 | return BinaryOperator::Create(BinOp->getOpcode(), PhiVal, ConstantOp); |
Chris Lattner | 54545ac | 2008-04-29 17:13:43 +0000 | [diff] [blame] | 9428 | if (CmpInst *CIOp = dyn_cast<CmpInst>(FirstInst)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9429 | return CmpInst::Create(CIOp->getOpcode(), CIOp->getPredicate(), |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9430 | PhiVal, ConstantOp); |
Chris Lattner | 54545ac | 2008-04-29 17:13:43 +0000 | [diff] [blame] | 9431 | assert(isa<LoadInst>(FirstInst) && "Unknown operation"); |
| 9432 | |
| 9433 | // If this was a volatile load that we are merging, make sure to loop through |
| 9434 | // and mark all the input loads as non-volatile. If we don't do this, we will |
| 9435 | // insert a new volatile load and the old ones will not be deletable. |
| 9436 | if (isVolatile) |
| 9437 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) |
| 9438 | cast<LoadInst>(PN.getIncomingValue(i))->setVolatile(false); |
| 9439 | |
| 9440 | return new LoadInst(PhiVal, "", isVolatile); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9441 | } |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 9442 | |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 9443 | /// DeadPHICycle - Return true if this PHI node is only used by a PHI node cycle |
| 9444 | /// that is dead. |
Chris Lattner | 0e5444b | 2007-03-26 20:40:50 +0000 | [diff] [blame] | 9445 | static bool DeadPHICycle(PHINode *PN, |
| 9446 | SmallPtrSet<PHINode*, 16> &PotentiallyDeadPHIs) { |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 9447 | if (PN->use_empty()) return true; |
| 9448 | if (!PN->hasOneUse()) return false; |
| 9449 | |
| 9450 | // Remember this node, and if we find the cycle, return. |
Chris Lattner | 0e5444b | 2007-03-26 20:40:50 +0000 | [diff] [blame] | 9451 | if (!PotentiallyDeadPHIs.insert(PN)) |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 9452 | return true; |
Chris Lattner | 92103de | 2007-08-28 04:23:55 +0000 | [diff] [blame] | 9453 | |
| 9454 | // Don't scan crazily complex things. |
| 9455 | if (PotentiallyDeadPHIs.size() == 16) |
| 9456 | return false; |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 9457 | |
| 9458 | if (PHINode *PU = dyn_cast<PHINode>(PN->use_back())) |
| 9459 | return DeadPHICycle(PU, PotentiallyDeadPHIs); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9460 | |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 9461 | return false; |
| 9462 | } |
| 9463 | |
Chris Lattner | cf5008a | 2007-11-06 21:52:06 +0000 | [diff] [blame] | 9464 | /// PHIsEqualValue - Return true if this phi node is always equal to |
| 9465 | /// NonPhiInVal. This happens with mutually cyclic phi nodes like: |
| 9466 | /// z = some value; x = phi (y, z); y = phi (x, z) |
| 9467 | static bool PHIsEqualValue(PHINode *PN, Value *NonPhiInVal, |
| 9468 | SmallPtrSet<PHINode*, 16> &ValueEqualPHIs) { |
| 9469 | // See if we already saw this PHI node. |
| 9470 | if (!ValueEqualPHIs.insert(PN)) |
| 9471 | return true; |
| 9472 | |
| 9473 | // Don't scan crazily complex things. |
| 9474 | if (ValueEqualPHIs.size() == 16) |
| 9475 | return false; |
| 9476 | |
| 9477 | // Scan the operands to see if they are either phi nodes or are equal to |
| 9478 | // the value. |
| 9479 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 9480 | Value *Op = PN->getIncomingValue(i); |
| 9481 | if (PHINode *OpPN = dyn_cast<PHINode>(Op)) { |
| 9482 | if (!PHIsEqualValue(OpPN, NonPhiInVal, ValueEqualPHIs)) |
| 9483 | return false; |
| 9484 | } else if (Op != NonPhiInVal) |
| 9485 | return false; |
| 9486 | } |
| 9487 | |
| 9488 | return true; |
| 9489 | } |
| 9490 | |
| 9491 | |
Chris Lattner | 473945d | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 9492 | // PHINode simplification |
| 9493 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 9494 | Instruction *InstCombiner::visitPHINode(PHINode &PN) { |
Owen Anderson | b64ab87 | 2006-07-10 22:15:25 +0000 | [diff] [blame] | 9495 | // If LCSSA is around, don't mess with Phi nodes |
Chris Lattner | f964f32 | 2007-03-04 04:27:24 +0000 | [diff] [blame] | 9496 | if (MustPreserveLCSSA) return 0; |
Owen Anderson | d1b78a1 | 2006-07-10 19:03:49 +0000 | [diff] [blame] | 9497 | |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 9498 | if (Value *V = PN.hasConstantValue()) |
| 9499 | return ReplaceInstUsesWith(PN, V); |
| 9500 | |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 9501 | // If all PHI operands are the same operation, pull them through the PHI, |
| 9502 | // reducing code size. |
| 9503 | if (isa<Instruction>(PN.getIncomingValue(0)) && |
| 9504 | PN.getIncomingValue(0)->hasOneUse()) |
| 9505 | if (Instruction *Result = FoldPHIArgOpIntoPHI(PN)) |
| 9506 | return Result; |
| 9507 | |
| 9508 | // If this is a trivial cycle in the PHI node graph, remove it. Basically, if |
| 9509 | // this PHI only has a single use (a PHI), and if that PHI only has one use (a |
| 9510 | // PHI)... break the cycle. |
Chris Lattner | ff9f13a | 2007-01-15 07:30:06 +0000 | [diff] [blame] | 9511 | if (PN.hasOneUse()) { |
| 9512 | Instruction *PHIUser = cast<Instruction>(PN.use_back()); |
| 9513 | if (PHINode *PU = dyn_cast<PHINode>(PHIUser)) { |
Chris Lattner | 0e5444b | 2007-03-26 20:40:50 +0000 | [diff] [blame] | 9514 | SmallPtrSet<PHINode*, 16> PotentiallyDeadPHIs; |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 9515 | PotentiallyDeadPHIs.insert(&PN); |
| 9516 | if (DeadPHICycle(PU, PotentiallyDeadPHIs)) |
| 9517 | return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType())); |
| 9518 | } |
Chris Lattner | ff9f13a | 2007-01-15 07:30:06 +0000 | [diff] [blame] | 9519 | |
| 9520 | // If this phi has a single use, and if that use just computes a value for |
| 9521 | // the next iteration of a loop, delete the phi. This occurs with unused |
| 9522 | // induction variables, e.g. "for (int j = 0; ; ++j);". Detecting this |
| 9523 | // common case here is good because the only other things that catch this |
| 9524 | // are induction variable analysis (sometimes) and ADCE, which is only run |
| 9525 | // late. |
| 9526 | if (PHIUser->hasOneUse() && |
| 9527 | (isa<BinaryOperator>(PHIUser) || isa<GetElementPtrInst>(PHIUser)) && |
| 9528 | PHIUser->use_back() == &PN) { |
| 9529 | return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType())); |
| 9530 | } |
| 9531 | } |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 9532 | |
Chris Lattner | cf5008a | 2007-11-06 21:52:06 +0000 | [diff] [blame] | 9533 | // We sometimes end up with phi cycles that non-obviously end up being the |
| 9534 | // same value, for example: |
| 9535 | // z = some value; x = phi (y, z); y = phi (x, z) |
| 9536 | // where the phi nodes don't necessarily need to be in the same block. Do a |
| 9537 | // quick check to see if the PHI node only contains a single non-phi value, if |
| 9538 | // so, scan to see if the phi cycle is actually equal to that value. |
| 9539 | { |
| 9540 | unsigned InValNo = 0, NumOperandVals = PN.getNumIncomingValues(); |
| 9541 | // Scan for the first non-phi operand. |
| 9542 | while (InValNo != NumOperandVals && |
| 9543 | isa<PHINode>(PN.getIncomingValue(InValNo))) |
| 9544 | ++InValNo; |
| 9545 | |
| 9546 | if (InValNo != NumOperandVals) { |
| 9547 | Value *NonPhiInVal = PN.getOperand(InValNo); |
| 9548 | |
| 9549 | // Scan the rest of the operands to see if there are any conflicts, if so |
| 9550 | // there is no need to recursively scan other phis. |
| 9551 | for (++InValNo; InValNo != NumOperandVals; ++InValNo) { |
| 9552 | Value *OpVal = PN.getIncomingValue(InValNo); |
| 9553 | if (OpVal != NonPhiInVal && !isa<PHINode>(OpVal)) |
| 9554 | break; |
| 9555 | } |
| 9556 | |
| 9557 | // If we scanned over all operands, then we have one unique value plus |
| 9558 | // phi values. Scan PHI nodes to see if they all merge in each other or |
| 9559 | // the value. |
| 9560 | if (InValNo == NumOperandVals) { |
| 9561 | SmallPtrSet<PHINode*, 16> ValueEqualPHIs; |
| 9562 | if (PHIsEqualValue(&PN, NonPhiInVal, ValueEqualPHIs)) |
| 9563 | return ReplaceInstUsesWith(PN, NonPhiInVal); |
| 9564 | } |
| 9565 | } |
| 9566 | } |
Chris Lattner | 60921c9 | 2003-12-19 05:58:40 +0000 | [diff] [blame] | 9567 | return 0; |
Chris Lattner | 473945d | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 9568 | } |
| 9569 | |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9570 | static Value *InsertCastToIntPtrTy(Value *V, const Type *DTy, |
| 9571 | Instruction *InsertPoint, |
| 9572 | InstCombiner *IC) { |
Reid Spencer | abaa8ca | 2007-01-08 16:32:00 +0000 | [diff] [blame] | 9573 | unsigned PtrSize = DTy->getPrimitiveSizeInBits(); |
| 9574 | unsigned VTySize = V->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9575 | // We must cast correctly to the pointer type. Ensure that we |
| 9576 | // sign extend the integer value if it is smaller as this is |
| 9577 | // used for address computation. |
| 9578 | Instruction::CastOps opcode = |
| 9579 | (VTySize < PtrSize ? Instruction::SExt : |
| 9580 | (VTySize == PtrSize ? Instruction::BitCast : Instruction::Trunc)); |
| 9581 | return IC->InsertCastBefore(opcode, V, DTy, *InsertPoint); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9582 | } |
| 9583 | |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 9584 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 9585 | Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9586 | Value *PtrOp = GEP.getOperand(0); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 9587 | // Is it 'getelementptr %P, i32 0' or 'getelementptr %P' |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 9588 | // If so, eliminate the noop. |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 9589 | if (GEP.getNumOperands() == 1) |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9590 | return ReplaceInstUsesWith(GEP, PtrOp); |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 9591 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 9592 | if (isa<UndefValue>(GEP.getOperand(0))) |
| 9593 | return ReplaceInstUsesWith(GEP, UndefValue::get(GEP.getType())); |
| 9594 | |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 9595 | bool HasZeroPointerIndex = false; |
| 9596 | if (Constant *C = dyn_cast<Constant>(GEP.getOperand(1))) |
| 9597 | HasZeroPointerIndex = C->isNullValue(); |
| 9598 | |
| 9599 | if (GEP.getNumOperands() == 2 && HasZeroPointerIndex) |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9600 | return ReplaceInstUsesWith(GEP, PtrOp); |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 9601 | |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9602 | // Eliminate unneeded casts for indices. |
| 9603 | bool MadeChange = false; |
Chris Lattner | db9654e | 2007-03-25 20:43:09 +0000 | [diff] [blame] | 9604 | |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 9605 | gep_type_iterator GTI = gep_type_begin(GEP); |
Gabor Greif | 177dd3f | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 9606 | for (User::op_iterator i = GEP.op_begin() + 1, e = GEP.op_end(); |
| 9607 | i != e; ++i, ++GTI) { |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 9608 | if (isa<SequentialType>(*GTI)) { |
Gabor Greif | 177dd3f | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 9609 | if (CastInst *CI = dyn_cast<CastInst>(*i)) { |
Chris Lattner | 76b7a06 | 2007-01-15 07:02:54 +0000 | [diff] [blame] | 9610 | if (CI->getOpcode() == Instruction::ZExt || |
| 9611 | CI->getOpcode() == Instruction::SExt) { |
| 9612 | const Type *SrcTy = CI->getOperand(0)->getType(); |
| 9613 | // We can eliminate a cast from i32 to i64 iff the target |
| 9614 | // is a 32-bit pointer target. |
| 9615 | if (SrcTy->getPrimitiveSizeInBits() >= TD->getPointerSizeInBits()) { |
| 9616 | MadeChange = true; |
Gabor Greif | 177dd3f | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 9617 | *i = CI->getOperand(0); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9618 | } |
| 9619 | } |
| 9620 | } |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 9621 | // If we are using a wider index than needed for this platform, shrink it |
| 9622 | // to what we need. If the incoming value needs a cast instruction, |
| 9623 | // insert it. This explicit cast can make subsequent optimizations more |
| 9624 | // obvious. |
Gabor Greif | 177dd3f | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 9625 | Value *Op = *i; |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 9626 | if (TD->getTypeSizeInBits(Op->getType()) > TD->getPointerSizeInBits()) { |
Chris Lattner | 4f1134e | 2004-04-17 18:16:10 +0000 | [diff] [blame] | 9627 | if (Constant *C = dyn_cast<Constant>(Op)) { |
Gabor Greif | 177dd3f | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 9628 | *i = ConstantExpr::getTrunc(C, TD->getIntPtrType()); |
Chris Lattner | 4f1134e | 2004-04-17 18:16:10 +0000 | [diff] [blame] | 9629 | MadeChange = true; |
| 9630 | } else { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9631 | Op = InsertCastBefore(Instruction::Trunc, Op, TD->getIntPtrType(), |
| 9632 | GEP); |
Gabor Greif | 177dd3f | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 9633 | *i = Op; |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 9634 | MadeChange = true; |
| 9635 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 9636 | } |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9637 | } |
Chris Lattner | db9654e | 2007-03-25 20:43:09 +0000 | [diff] [blame] | 9638 | } |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9639 | if (MadeChange) return &GEP; |
| 9640 | |
Chris Lattner | db9654e | 2007-03-25 20:43:09 +0000 | [diff] [blame] | 9641 | // If this GEP instruction doesn't move the pointer, and if the input operand |
| 9642 | // is a bitcast of another pointer, just replace the GEP with a bitcast of the |
| 9643 | // real input to the dest type. |
Chris Lattner | 6a94de2 | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 9644 | if (GEP.hasAllZeroIndices()) { |
| 9645 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(GEP.getOperand(0))) { |
| 9646 | // If the bitcast is of an allocation, and the allocation will be |
| 9647 | // converted to match the type of the cast, don't touch this. |
| 9648 | if (isa<AllocationInst>(BCI->getOperand(0))) { |
| 9649 | // See if the bitcast simplifies, if so, don't nuke this GEP yet. |
Chris Lattner | a79dd43 | 2007-10-12 18:05:47 +0000 | [diff] [blame] | 9650 | if (Instruction *I = visitBitCast(*BCI)) { |
| 9651 | if (I != BCI) { |
| 9652 | I->takeName(BCI); |
| 9653 | BCI->getParent()->getInstList().insert(BCI, I); |
| 9654 | ReplaceInstUsesWith(*BCI, I); |
| 9655 | } |
Chris Lattner | 6a94de2 | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 9656 | return &GEP; |
Chris Lattner | a79dd43 | 2007-10-12 18:05:47 +0000 | [diff] [blame] | 9657 | } |
Chris Lattner | 6a94de2 | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 9658 | } |
| 9659 | return new BitCastInst(BCI->getOperand(0), GEP.getType()); |
| 9660 | } |
| 9661 | } |
| 9662 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9663 | // Combine Indices - If the source pointer to this getelementptr instruction |
| 9664 | // is a getelementptr instruction, combine the indices of the two |
| 9665 | // getelementptr instructions into a single instruction. |
| 9666 | // |
Chris Lattner | 72588fc | 2007-02-15 22:48:32 +0000 | [diff] [blame] | 9667 | SmallVector<Value*, 8> SrcGEPOperands; |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 9668 | if (User *Src = dyn_castGetElementPtr(PtrOp)) |
Chris Lattner | 72588fc | 2007-02-15 22:48:32 +0000 | [diff] [blame] | 9669 | SrcGEPOperands.append(Src->op_begin(), Src->op_end()); |
Chris Lattner | ebd985c | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 9670 | |
| 9671 | if (!SrcGEPOperands.empty()) { |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9672 | // Note that if our source is a gep chain itself that we wait for that |
| 9673 | // chain to be resolved before we perform this transformation. This |
| 9674 | // avoids us creating a TON of code in some cases. |
| 9675 | // |
| 9676 | if (isa<GetElementPtrInst>(SrcGEPOperands[0]) && |
| 9677 | cast<Instruction>(SrcGEPOperands[0])->getNumOperands() == 2) |
| 9678 | return 0; // Wait until our source is folded to completion. |
| 9679 | |
Chris Lattner | 72588fc | 2007-02-15 22:48:32 +0000 | [diff] [blame] | 9680 | SmallVector<Value*, 8> Indices; |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9681 | |
| 9682 | // Find out whether the last index in the source GEP is a sequential idx. |
| 9683 | bool EndsWithSequential = false; |
| 9684 | for (gep_type_iterator I = gep_type_begin(*cast<User>(PtrOp)), |
| 9685 | E = gep_type_end(*cast<User>(PtrOp)); I != E; ++I) |
Chris Lattner | be97b4e | 2004-05-08 22:41:42 +0000 | [diff] [blame] | 9686 | EndsWithSequential = !isa<StructType>(*I); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9687 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9688 | // Can we combine the two pointer arithmetics offsets? |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9689 | if (EndsWithSequential) { |
Chris Lattner | decd081 | 2003-03-05 22:33:14 +0000 | [diff] [blame] | 9690 | // Replace: gep (gep %P, long B), long A, ... |
| 9691 | // With: T = long A+B; gep %P, T, ... |
| 9692 | // |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9693 | Value *Sum, *SO1 = SrcGEPOperands.back(), *GO1 = GEP.getOperand(1); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9694 | if (SO1 == Constant::getNullValue(SO1->getType())) { |
| 9695 | Sum = GO1; |
| 9696 | } else if (GO1 == Constant::getNullValue(GO1->getType())) { |
| 9697 | Sum = SO1; |
| 9698 | } else { |
| 9699 | // If they aren't the same type, convert both to an integer of the |
| 9700 | // target's pointer size. |
| 9701 | if (SO1->getType() != GO1->getType()) { |
| 9702 | if (Constant *SO1C = dyn_cast<Constant>(SO1)) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9703 | SO1 = ConstantExpr::getIntegerCast(SO1C, GO1->getType(), true); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9704 | } else if (Constant *GO1C = dyn_cast<Constant>(GO1)) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9705 | GO1 = ConstantExpr::getIntegerCast(GO1C, SO1->getType(), true); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9706 | } else { |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 9707 | unsigned PS = TD->getPointerSizeInBits(); |
| 9708 | if (TD->getTypeSizeInBits(SO1->getType()) == PS) { |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9709 | // Convert GO1 to SO1's type. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9710 | GO1 = InsertCastToIntPtrTy(GO1, SO1->getType(), &GEP, this); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9711 | |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 9712 | } else if (TD->getTypeSizeInBits(GO1->getType()) == PS) { |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9713 | // Convert SO1 to GO1's type. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9714 | SO1 = InsertCastToIntPtrTy(SO1, GO1->getType(), &GEP, this); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9715 | } else { |
| 9716 | const Type *PT = TD->getIntPtrType(); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9717 | SO1 = InsertCastToIntPtrTy(SO1, PT, &GEP, this); |
| 9718 | GO1 = InsertCastToIntPtrTy(GO1, PT, &GEP, this); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9719 | } |
| 9720 | } |
| 9721 | } |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9722 | if (isa<Constant>(SO1) && isa<Constant>(GO1)) |
| 9723 | Sum = ConstantExpr::getAdd(cast<Constant>(SO1), cast<Constant>(GO1)); |
| 9724 | else { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9725 | Sum = BinaryOperator::CreateAdd(SO1, GO1, PtrOp->getName()+".sum"); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 9726 | InsertNewInstBefore(cast<Instruction>(Sum), GEP); |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9727 | } |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9728 | } |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9729 | |
| 9730 | // Recycle the GEP we already have if possible. |
| 9731 | if (SrcGEPOperands.size() == 2) { |
| 9732 | GEP.setOperand(0, SrcGEPOperands[0]); |
| 9733 | GEP.setOperand(1, Sum); |
| 9734 | return &GEP; |
| 9735 | } else { |
| 9736 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, |
| 9737 | SrcGEPOperands.end()-1); |
| 9738 | Indices.push_back(Sum); |
| 9739 | Indices.insert(Indices.end(), GEP.op_begin()+2, GEP.op_end()); |
| 9740 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9741 | } else if (isa<Constant>(*GEP.idx_begin()) && |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9742 | cast<Constant>(*GEP.idx_begin())->isNullValue() && |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9743 | SrcGEPOperands.size() != 1) { |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9744 | // Otherwise we can do the fold if the first index of the GEP is a zero |
Chris Lattner | ebd985c | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 9745 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, |
| 9746 | SrcGEPOperands.end()); |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9747 | Indices.insert(Indices.end(), GEP.idx_begin()+1, GEP.idx_end()); |
| 9748 | } |
| 9749 | |
| 9750 | if (!Indices.empty()) |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9751 | return GetElementPtrInst::Create(SrcGEPOperands[0], Indices.begin(), |
| 9752 | Indices.end(), GEP.getName()); |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 9753 | |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9754 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(PtrOp)) { |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 9755 | // GEP of global variable. If all of the indices for this GEP are |
| 9756 | // constants, we can promote this to a constexpr instead of an instruction. |
| 9757 | |
| 9758 | // Scan for nonconstants... |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 9759 | SmallVector<Constant*, 8> Indices; |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 9760 | User::op_iterator I = GEP.idx_begin(), E = GEP.idx_end(); |
| 9761 | for (; I != E && isa<Constant>(*I); ++I) |
| 9762 | Indices.push_back(cast<Constant>(*I)); |
| 9763 | |
| 9764 | if (I == E) { // If they are all constants... |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 9765 | Constant *CE = ConstantExpr::getGetElementPtr(GV, |
| 9766 | &Indices[0],Indices.size()); |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 9767 | |
| 9768 | // Replace all uses of the GEP with the new constexpr... |
| 9769 | return ReplaceInstUsesWith(GEP, CE); |
| 9770 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9771 | } else if (Value *X = getBitCastOperand(PtrOp)) { // Is the operand a cast? |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 9772 | if (!isa<PointerType>(X->getType())) { |
| 9773 | // Not interesting. Source pointer must be a cast from pointer. |
| 9774 | } else if (HasZeroPointerIndex) { |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9775 | // transform: GEP (bitcast [10 x i8]* X to [0 x i8]*), i32 0, ... |
| 9776 | // into : GEP [10 x i8]* X, i32 0, ... |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 9777 | // |
| 9778 | // This occurs when the program declares an array extern like "int X[];" |
| 9779 | // |
| 9780 | const PointerType *CPTy = cast<PointerType>(PtrOp->getType()); |
| 9781 | const PointerType *XTy = cast<PointerType>(X->getType()); |
| 9782 | if (const ArrayType *XATy = |
| 9783 | dyn_cast<ArrayType>(XTy->getElementType())) |
| 9784 | if (const ArrayType *CATy = |
| 9785 | dyn_cast<ArrayType>(CPTy->getElementType())) |
| 9786 | if (CATy->getElementType() == XATy->getElementType()) { |
| 9787 | // At this point, we know that the cast source type is a pointer |
| 9788 | // to an array of the same type as the destination pointer |
| 9789 | // array. Because the array type is never stepped over (there |
| 9790 | // is a leading zero) we can fold the cast into this GEP. |
| 9791 | GEP.setOperand(0, X); |
| 9792 | return &GEP; |
| 9793 | } |
| 9794 | } else if (GEP.getNumOperands() == 2) { |
| 9795 | // Transform things like: |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9796 | // %t = getelementptr i32* bitcast ([2 x i32]* %str to i32*), i32 %V |
| 9797 | // into: %t1 = getelementptr [2 x i32]* %str, i32 0, i32 %V; bitcast |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 9798 | const Type *SrcElTy = cast<PointerType>(X->getType())->getElementType(); |
| 9799 | const Type *ResElTy=cast<PointerType>(PtrOp->getType())->getElementType(); |
| 9800 | if (isa<ArrayType>(SrcElTy) && |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 9801 | TD->getABITypeSize(cast<ArrayType>(SrcElTy)->getElementType()) == |
| 9802 | TD->getABITypeSize(ResElTy)) { |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 9803 | Value *Idx[2]; |
| 9804 | Idx[0] = Constant::getNullValue(Type::Int32Ty); |
| 9805 | Idx[1] = GEP.getOperand(1); |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 9806 | Value *V = InsertNewInstBefore( |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9807 | GetElementPtrInst::Create(X, Idx, Idx + 2, GEP.getName()), GEP); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9808 | // V and GEP are both pointer types --> BitCast |
| 9809 | return new BitCastInst(V, GEP.getType()); |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 9810 | } |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9811 | |
| 9812 | // Transform things like: |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9813 | // getelementptr i8* bitcast ([100 x double]* X to i8*), i32 %tmp |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9814 | // (where tmp = 8*tmp2) into: |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9815 | // getelementptr [100 x double]* %arr, i32 0, i32 %tmp2; bitcast |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9816 | |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9817 | if (isa<ArrayType>(SrcElTy) && ResElTy == Type::Int8Ty) { |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9818 | uint64_t ArrayEltSize = |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 9819 | TD->getABITypeSize(cast<ArrayType>(SrcElTy)->getElementType()); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9820 | |
| 9821 | // Check to see if "tmp" is a scale by a multiple of ArrayEltSize. We |
| 9822 | // allow either a mul, shift, or constant here. |
| 9823 | Value *NewIdx = 0; |
| 9824 | ConstantInt *Scale = 0; |
| 9825 | if (ArrayEltSize == 1) { |
| 9826 | NewIdx = GEP.getOperand(1); |
| 9827 | Scale = ConstantInt::get(NewIdx->getType(), 1); |
| 9828 | } else if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP.getOperand(1))) { |
Chris Lattner | 6e2f843 | 2005-09-14 17:32:56 +0000 | [diff] [blame] | 9829 | NewIdx = ConstantInt::get(CI->getType(), 1); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9830 | Scale = CI; |
| 9831 | } else if (Instruction *Inst =dyn_cast<Instruction>(GEP.getOperand(1))){ |
| 9832 | if (Inst->getOpcode() == Instruction::Shl && |
| 9833 | isa<ConstantInt>(Inst->getOperand(1))) { |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 9834 | ConstantInt *ShAmt = cast<ConstantInt>(Inst->getOperand(1)); |
| 9835 | uint32_t ShAmtVal = ShAmt->getLimitedValue(64); |
| 9836 | Scale = ConstantInt::get(Inst->getType(), 1ULL << ShAmtVal); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9837 | NewIdx = Inst->getOperand(0); |
| 9838 | } else if (Inst->getOpcode() == Instruction::Mul && |
| 9839 | isa<ConstantInt>(Inst->getOperand(1))) { |
| 9840 | Scale = cast<ConstantInt>(Inst->getOperand(1)); |
| 9841 | NewIdx = Inst->getOperand(0); |
| 9842 | } |
| 9843 | } |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9844 | |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9845 | // 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] | 9846 | // out, perform the transformation. Note, we don't know whether Scale is |
| 9847 | // signed or not. We'll use unsigned version of division/modulo |
| 9848 | // operation after making sure Scale doesn't have the sign bit set. |
| 9849 | if (Scale && Scale->getSExtValue() >= 0LL && |
| 9850 | Scale->getZExtValue() % ArrayEltSize == 0) { |
| 9851 | Scale = ConstantInt::get(Scale->getType(), |
| 9852 | Scale->getZExtValue() / ArrayEltSize); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 9853 | if (Scale->getZExtValue() != 1) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9854 | Constant *C = ConstantExpr::getIntegerCast(Scale, NewIdx->getType(), |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9855 | false /*ZExt*/); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9856 | Instruction *Sc = BinaryOperator::CreateMul(NewIdx, C, "idxscale"); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9857 | NewIdx = InsertNewInstBefore(Sc, GEP); |
| 9858 | } |
| 9859 | |
| 9860 | // Insert the new GEP instruction. |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 9861 | Value *Idx[2]; |
| 9862 | Idx[0] = Constant::getNullValue(Type::Int32Ty); |
| 9863 | Idx[1] = NewIdx; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9864 | Instruction *NewGEP = |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9865 | GetElementPtrInst::Create(X, Idx, Idx + 2, GEP.getName()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9866 | NewGEP = InsertNewInstBefore(NewGEP, GEP); |
| 9867 | // The NewGEP must be pointer typed, so must the old one -> BitCast |
| 9868 | return new BitCastInst(NewGEP, GEP.getType()); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9869 | } |
| 9870 | } |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 9871 | } |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9872 | } |
| 9873 | |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9874 | return 0; |
| 9875 | } |
| 9876 | |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 9877 | Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) { |
| 9878 | // Convert: malloc Ty, C - where C is a constant != 1 into: malloc [C x Ty], 1 |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 9879 | if (AI.isArrayAllocation()) { // Check C != 1 |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 9880 | if (const ConstantInt *C = dyn_cast<ConstantInt>(AI.getArraySize())) { |
| 9881 | const Type *NewTy = |
| 9882 | ArrayType::get(AI.getAllocatedType(), C->getZExtValue()); |
Chris Lattner | 0006bd7 | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 9883 | AllocationInst *New = 0; |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 9884 | |
| 9885 | // Create and insert the replacement instruction... |
| 9886 | if (isa<MallocInst>(AI)) |
Nate Begeman | 14b0529 | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 9887 | New = new MallocInst(NewTy, 0, AI.getAlignment(), AI.getName()); |
Chris Lattner | 0006bd7 | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 9888 | else { |
| 9889 | assert(isa<AllocaInst>(AI) && "Unknown type of allocation inst!"); |
Nate Begeman | 14b0529 | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 9890 | New = new AllocaInst(NewTy, 0, AI.getAlignment(), AI.getName()); |
Chris Lattner | 0006bd7 | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 9891 | } |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 9892 | |
| 9893 | InsertNewInstBefore(New, AI); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9894 | |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 9895 | // Scan to the end of the allocation instructions, to skip over a block of |
| 9896 | // allocas if possible... |
| 9897 | // |
| 9898 | BasicBlock::iterator It = New; |
| 9899 | while (isa<AllocationInst>(*It)) ++It; |
| 9900 | |
| 9901 | // Now that I is pointing to the first non-allocation-inst in the block, |
| 9902 | // insert our getelementptr instruction... |
| 9903 | // |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9904 | Value *NullIdx = Constant::getNullValue(Type::Int32Ty); |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 9905 | Value *Idx[2]; |
| 9906 | Idx[0] = NullIdx; |
| 9907 | Idx[1] = NullIdx; |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9908 | Value *V = GetElementPtrInst::Create(New, Idx, Idx + 2, |
| 9909 | New->getName()+".sub", It); |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 9910 | |
| 9911 | // Now make everything use the getelementptr instead of the original |
| 9912 | // allocation. |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 9913 | return ReplaceInstUsesWith(AI, V); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 9914 | } else if (isa<UndefValue>(AI.getArraySize())) { |
| 9915 | return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 9916 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 9917 | } |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 9918 | |
| 9919 | // If alloca'ing a zero byte object, replace the alloca with a null pointer. |
| 9920 | // Note that we only do this for alloca's, because malloc should allocate and |
| 9921 | // return a unique pointer, even for a zero byte allocation. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9922 | if (isa<AllocaInst>(AI) && AI.getAllocatedType()->isSized() && |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 9923 | TD->getABITypeSize(AI.getAllocatedType()) == 0) |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 9924 | return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); |
| 9925 | |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 9926 | return 0; |
| 9927 | } |
| 9928 | |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 9929 | Instruction *InstCombiner::visitFreeInst(FreeInst &FI) { |
| 9930 | Value *Op = FI.getOperand(0); |
| 9931 | |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 9932 | // free undef -> unreachable. |
| 9933 | if (isa<UndefValue>(Op)) { |
| 9934 | // Insert a new store to null because we cannot modify the CFG here. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 9935 | new StoreInst(ConstantInt::getTrue(), |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 9936 | UndefValue::get(PointerType::getUnqual(Type::Int1Ty)), &FI); |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 9937 | return EraseInstFromFunction(FI); |
| 9938 | } |
Chris Lattner | 6fe5541 | 2007-04-14 00:20:02 +0000 | [diff] [blame] | 9939 | |
Chris Lattner | 6160e85 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 9940 | // If we have 'free null' delete the instruction. This can happen in stl code |
| 9941 | // when lots of inlining happens. |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 9942 | if (isa<ConstantPointerNull>(Op)) |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 9943 | return EraseInstFromFunction(FI); |
Chris Lattner | 6fe5541 | 2007-04-14 00:20:02 +0000 | [diff] [blame] | 9944 | |
| 9945 | // Change free <ty>* (cast <ty2>* X to <ty>*) into free <ty2>* X |
| 9946 | if (BitCastInst *CI = dyn_cast<BitCastInst>(Op)) { |
| 9947 | FI.setOperand(0, CI->getOperand(0)); |
| 9948 | return &FI; |
| 9949 | } |
| 9950 | |
| 9951 | // Change free (gep X, 0,0,0,0) into free(X) |
| 9952 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) { |
| 9953 | if (GEPI->hasAllZeroIndices()) { |
| 9954 | AddToWorkList(GEPI); |
| 9955 | FI.setOperand(0, GEPI->getOperand(0)); |
| 9956 | return &FI; |
| 9957 | } |
| 9958 | } |
| 9959 | |
| 9960 | // Change free(malloc) into nothing, if the malloc has a single use. |
| 9961 | if (MallocInst *MI = dyn_cast<MallocInst>(Op)) |
| 9962 | if (MI->hasOneUse()) { |
| 9963 | EraseInstFromFunction(FI); |
| 9964 | return EraseInstFromFunction(*MI); |
| 9965 | } |
Chris Lattner | 6160e85 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 9966 | |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 9967 | return 0; |
| 9968 | } |
| 9969 | |
| 9970 | |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 9971 | /// InstCombineLoadCast - Fold 'load (cast P)' -> cast (load P)' when possible. |
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 9972 | static Instruction *InstCombineLoadCast(InstCombiner &IC, LoadInst &LI, |
Bill Wendling | 587c01d | 2008-02-26 10:53:30 +0000 | [diff] [blame] | 9973 | const TargetData *TD) { |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 9974 | User *CI = cast<User>(LI.getOperand(0)); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 9975 | Value *CastOp = CI->getOperand(0); |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 9976 | |
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 9977 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(CI)) { |
| 9978 | // Instead of loading constant c string, use corresponding integer value |
| 9979 | // directly if string length is small enough. |
Evan Cheng | 0ff39b3 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 9980 | std::string Str; |
| 9981 | if (GetConstantStringInfo(CE->getOperand(0), Str) && !Str.empty()) { |
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 9982 | unsigned len = Str.length(); |
| 9983 | const Type *Ty = cast<PointerType>(CE->getType())->getElementType(); |
| 9984 | unsigned numBits = Ty->getPrimitiveSizeInBits(); |
| 9985 | // Replace LI with immediate integer store. |
| 9986 | if ((numBits >> 3) == len + 1) { |
Bill Wendling | 587c01d | 2008-02-26 10:53:30 +0000 | [diff] [blame] | 9987 | APInt StrVal(numBits, 0); |
| 9988 | APInt SingleChar(numBits, 0); |
| 9989 | if (TD->isLittleEndian()) { |
| 9990 | for (signed i = len-1; i >= 0; i--) { |
| 9991 | SingleChar = (uint64_t) Str[i]; |
| 9992 | StrVal = (StrVal << 8) | SingleChar; |
| 9993 | } |
| 9994 | } else { |
| 9995 | for (unsigned i = 0; i < len; i++) { |
| 9996 | SingleChar = (uint64_t) Str[i]; |
| 9997 | StrVal = (StrVal << 8) | SingleChar; |
| 9998 | } |
| 9999 | // Append NULL at the end. |
| 10000 | SingleChar = 0; |
| 10001 | StrVal = (StrVal << 8) | SingleChar; |
| 10002 | } |
| 10003 | Value *NL = ConstantInt::get(StrVal); |
| 10004 | return IC.ReplaceInstUsesWith(LI, NL); |
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 10005 | } |
| 10006 | } |
| 10007 | } |
| 10008 | |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 10009 | const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType(); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 10010 | if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) { |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 10011 | const Type *SrcPTy = SrcTy->getElementType(); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 10012 | |
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 10013 | if (DestPTy->isInteger() || isa<PointerType>(DestPTy) || |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10014 | isa<VectorType>(DestPTy)) { |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 10015 | // If the source is an array, the code below will not succeed. Check to |
| 10016 | // see if a trivial 'gep P, 0, 0' will help matters. Only do this for |
| 10017 | // constants. |
| 10018 | if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy)) |
| 10019 | if (Constant *CSrc = dyn_cast<Constant>(CastOp)) |
| 10020 | if (ASrcTy->getNumElements() != 0) { |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 10021 | Value *Idxs[2]; |
| 10022 | Idxs[0] = Idxs[1] = Constant::getNullValue(Type::Int32Ty); |
| 10023 | CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs, 2); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 10024 | SrcTy = cast<PointerType>(CastOp->getType()); |
| 10025 | SrcPTy = SrcTy->getElementType(); |
| 10026 | } |
| 10027 | |
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 10028 | if ((SrcPTy->isInteger() || isa<PointerType>(SrcPTy) || |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10029 | isa<VectorType>(SrcPTy)) && |
Chris Lattner | b1515fe | 2005-03-29 06:37:47 +0000 | [diff] [blame] | 10030 | // Do not allow turning this into a load of an integer, which is then |
| 10031 | // casted to a pointer, this pessimizes pointer analysis a lot. |
| 10032 | (isa<PointerType>(SrcPTy) == isa<PointerType>(LI.getType())) && |
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 10033 | IC.getTargetData().getTypeSizeInBits(SrcPTy) == |
| 10034 | IC.getTargetData().getTypeSizeInBits(DestPTy)) { |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 10035 | |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 10036 | // Okay, we are casting from one integer or pointer type to another of |
| 10037 | // the same size. Instead of casting the pointer before the load, cast |
| 10038 | // the result of the loaded value. |
| 10039 | Value *NewLoad = IC.InsertNewInstBefore(new LoadInst(CastOp, |
| 10040 | CI->getName(), |
| 10041 | LI.isVolatile()),LI); |
| 10042 | // Now cast the result of the load. |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 10043 | return new BitCastInst(NewLoad, LI.getType()); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 10044 | } |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 10045 | } |
| 10046 | } |
| 10047 | return 0; |
| 10048 | } |
| 10049 | |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 10050 | /// isSafeToLoadUnconditionally - Return true if we know that executing a load |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 10051 | /// from this value cannot trap. If it is not obviously safe to load from the |
| 10052 | /// specified pointer, we do a quick local scan of the basic block containing |
| 10053 | /// ScanFrom, to determine if the address is already accessed. |
| 10054 | static bool isSafeToLoadUnconditionally(Value *V, Instruction *ScanFrom) { |
Duncan Sands | 892c7e4 | 2007-09-19 10:10:31 +0000 | [diff] [blame] | 10055 | // If it is an alloca it is always safe to load from. |
| 10056 | if (isa<AllocaInst>(V)) return true; |
| 10057 | |
Duncan Sands | 46318cd | 2007-09-19 10:25:38 +0000 | [diff] [blame] | 10058 | // If it is a global variable it is mostly safe to load from. |
Duncan Sands | 892c7e4 | 2007-09-19 10:10:31 +0000 | [diff] [blame] | 10059 | if (const GlobalValue *GV = dyn_cast<GlobalVariable>(V)) |
Duncan Sands | 46318cd | 2007-09-19 10:25:38 +0000 | [diff] [blame] | 10060 | // Don't try to evaluate aliases. External weak GV can be null. |
Duncan Sands | 892c7e4 | 2007-09-19 10:10:31 +0000 | [diff] [blame] | 10061 | return !isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage(); |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 10062 | |
| 10063 | // Otherwise, be a little bit agressive by scanning the local block where we |
| 10064 | // want to check to see if the pointer is already being loaded or stored |
Alkis Evlogimenos | 7b6ec60 | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 10065 | // from/to. If so, the previous load or store would have already trapped, |
| 10066 | // so there is no harm doing an extra load (also, CSE will later eliminate |
| 10067 | // the load entirely). |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 10068 | BasicBlock::iterator BBI = ScanFrom, E = ScanFrom->getParent()->begin(); |
| 10069 | |
Alkis Evlogimenos | 7b6ec60 | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 10070 | while (BBI != E) { |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 10071 | --BBI; |
| 10072 | |
Chris Lattner | 2de3fec | 2008-06-20 05:12:56 +0000 | [diff] [blame] | 10073 | // If we see a free or a call (which might do a free) the pointer could be |
| 10074 | // marked invalid. |
| 10075 | if (isa<FreeInst>(BBI) || isa<CallInst>(BBI)) |
| 10076 | return false; |
| 10077 | |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 10078 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { |
| 10079 | if (LI->getOperand(0) == V) return true; |
Chris Lattner | 2de3fec | 2008-06-20 05:12:56 +0000 | [diff] [blame] | 10080 | } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) { |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 10081 | if (SI->getOperand(1) == V) return true; |
Chris Lattner | 2de3fec | 2008-06-20 05:12:56 +0000 | [diff] [blame] | 10082 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 10083 | |
Alkis Evlogimenos | 7b6ec60 | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 10084 | } |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 10085 | return false; |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 10086 | } |
| 10087 | |
Chris Lattner | 8d2e888 | 2007-08-11 18:48:48 +0000 | [diff] [blame] | 10088 | /// GetUnderlyingObject - Trace through a series of getelementptrs and bitcasts |
| 10089 | /// until we find the underlying object a pointer is referring to or something |
| 10090 | /// we don't understand. Note that the returned pointer may be offset from the |
| 10091 | /// input, because we ignore GEP indices. |
| 10092 | static Value *GetUnderlyingObject(Value *Ptr) { |
| 10093 | while (1) { |
| 10094 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) { |
| 10095 | if (CE->getOpcode() == Instruction::BitCast || |
| 10096 | CE->getOpcode() == Instruction::GetElementPtr) |
| 10097 | Ptr = CE->getOperand(0); |
| 10098 | else |
| 10099 | return Ptr; |
| 10100 | } else if (BitCastInst *BCI = dyn_cast<BitCastInst>(Ptr)) { |
| 10101 | Ptr = BCI->getOperand(0); |
| 10102 | } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) { |
| 10103 | Ptr = GEP->getOperand(0); |
| 10104 | } else { |
| 10105 | return Ptr; |
| 10106 | } |
| 10107 | } |
| 10108 | } |
| 10109 | |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 10110 | Instruction *InstCombiner::visitLoadInst(LoadInst &LI) { |
| 10111 | Value *Op = LI.getOperand(0); |
Chris Lattner | 5f16a13 | 2004-01-12 04:13:56 +0000 | [diff] [blame] | 10112 | |
Dan Gohman | 9941f74 | 2007-07-20 16:34:21 +0000 | [diff] [blame] | 10113 | // Attempt to improve the alignment. |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 10114 | unsigned KnownAlign = GetOrEnforceKnownAlignment(Op); |
| 10115 | if (KnownAlign > |
| 10116 | (LI.getAlignment() == 0 ? TD->getABITypeAlignment(LI.getType()) : |
| 10117 | LI.getAlignment())) |
Dan Gohman | 9941f74 | 2007-07-20 16:34:21 +0000 | [diff] [blame] | 10118 | LI.setAlignment(KnownAlign); |
| 10119 | |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 10120 | // load (cast X) --> cast (load X) iff safe |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 10121 | if (isa<CastInst>(Op)) |
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 10122 | if (Instruction *Res = InstCombineLoadCast(*this, LI, TD)) |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 10123 | return Res; |
| 10124 | |
| 10125 | // None of the following transforms are legal for volatile loads. |
| 10126 | if (LI.isVolatile()) return 0; |
Chris Lattner | 62f254d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 10127 | |
Chris Lattner | 62f254d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 10128 | if (&LI.getParent()->front() != &LI) { |
| 10129 | BasicBlock::iterator BBI = &LI; --BBI; |
Chris Lattner | 9c1f0fd | 2005-09-12 22:21:03 +0000 | [diff] [blame] | 10130 | // If the instruction immediately before this is a store to the same |
| 10131 | // address, do a simple form of store->load forwarding. |
Chris Lattner | 62f254d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 10132 | if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) |
| 10133 | if (SI->getOperand(1) == LI.getOperand(0)) |
| 10134 | return ReplaceInstUsesWith(LI, SI->getOperand(0)); |
Chris Lattner | 9c1f0fd | 2005-09-12 22:21:03 +0000 | [diff] [blame] | 10135 | if (LoadInst *LIB = dyn_cast<LoadInst>(BBI)) |
| 10136 | if (LIB->getOperand(0) == LI.getOperand(0)) |
| 10137 | return ReplaceInstUsesWith(LI, LIB); |
Chris Lattner | 62f254d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 10138 | } |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 10139 | |
Christopher Lamb | b15147e | 2007-12-29 07:56:53 +0000 | [diff] [blame] | 10140 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) { |
| 10141 | const Value *GEPI0 = GEPI->getOperand(0); |
| 10142 | // TODO: Consider a target hook for valid address spaces for this xform. |
| 10143 | if (isa<ConstantPointerNull>(GEPI0) && |
| 10144 | cast<PointerType>(GEPI0->getType())->getAddressSpace() == 0) { |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 10145 | // Insert a new store to null instruction before the load to indicate |
| 10146 | // that this code is not reachable. We do this instead of inserting |
| 10147 | // an unreachable instruction directly because we cannot modify the |
| 10148 | // CFG. |
| 10149 | new StoreInst(UndefValue::get(LI.getType()), |
| 10150 | Constant::getNullValue(Op->getType()), &LI); |
| 10151 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
| 10152 | } |
Christopher Lamb | b15147e | 2007-12-29 07:56:53 +0000 | [diff] [blame] | 10153 | } |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 10154 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10155 | if (Constant *C = dyn_cast<Constant>(Op)) { |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 10156 | // load null/undef -> undef |
Christopher Lamb | b15147e | 2007-12-29 07:56:53 +0000 | [diff] [blame] | 10157 | // TODO: Consider a target hook for valid address spaces for this xform. |
| 10158 | if (isa<UndefValue>(C) || (C->isNullValue() && |
| 10159 | cast<PointerType>(Op->getType())->getAddressSpace() == 0)) { |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 10160 | // Insert a new store to null instruction before the load to indicate that |
| 10161 | // this code is not reachable. We do this instead of inserting an |
| 10162 | // unreachable instruction directly because we cannot modify the CFG. |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 10163 | new StoreInst(UndefValue::get(LI.getType()), |
| 10164 | Constant::getNullValue(Op->getType()), &LI); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10165 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 10166 | } |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 10167 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10168 | // Instcombine load (constant global) into the value loaded. |
| 10169 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op)) |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 10170 | if (GV->isConstant() && !GV->isDeclaration()) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10171 | return ReplaceInstUsesWith(LI, GV->getInitializer()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 10172 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10173 | // Instcombine load (constantexpr_GEP global, 0, ...) into the value loaded. |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 10174 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op)) { |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10175 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 10176 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0))) |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 10177 | if (GV->isConstant() && !GV->isDeclaration()) |
Chris Lattner | 363f2a2 | 2005-09-26 05:28:06 +0000 | [diff] [blame] | 10178 | if (Constant *V = |
| 10179 | ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE)) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10180 | return ReplaceInstUsesWith(LI, V); |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 10181 | if (CE->getOperand(0)->isNullValue()) { |
| 10182 | // Insert a new store to null instruction before the load to indicate |
| 10183 | // that this code is not reachable. We do this instead of inserting |
| 10184 | // an unreachable instruction directly because we cannot modify the |
| 10185 | // CFG. |
| 10186 | new StoreInst(UndefValue::get(LI.getType()), |
| 10187 | Constant::getNullValue(Op->getType()), &LI); |
| 10188 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
| 10189 | } |
| 10190 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 10191 | } else if (CE->isCast()) { |
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 10192 | if (Instruction *Res = InstCombineLoadCast(*this, LI, TD)) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10193 | return Res; |
| 10194 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 10195 | } |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10196 | } |
Chris Lattner | 8d2e888 | 2007-08-11 18:48:48 +0000 | [diff] [blame] | 10197 | |
| 10198 | // If this load comes from anywhere in a constant global, and if the global |
| 10199 | // is all undef or zero, we know what it loads. |
| 10200 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GetUnderlyingObject(Op))) { |
| 10201 | if (GV->isConstant() && GV->hasInitializer()) { |
| 10202 | if (GV->getInitializer()->isNullValue()) |
| 10203 | return ReplaceInstUsesWith(LI, Constant::getNullValue(LI.getType())); |
| 10204 | else if (isa<UndefValue>(GV->getInitializer())) |
| 10205 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
| 10206 | } |
| 10207 | } |
Chris Lattner | f499eac | 2004-04-08 20:39:49 +0000 | [diff] [blame] | 10208 | |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 10209 | if (Op->hasOneUse()) { |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 10210 | // Change select and PHI nodes to select values instead of addresses: this |
| 10211 | // helps alias analysis out a lot, allows many others simplifications, and |
| 10212 | // exposes redundancy in the code. |
| 10213 | // |
| 10214 | // Note that we cannot do the transformation unless we know that the |
| 10215 | // introduced loads cannot trap! Something like this is valid as long as |
| 10216 | // the condition is always false: load (select bool %C, int* null, int* %G), |
| 10217 | // but it would not be valid if we transformed it to load from null |
| 10218 | // unconditionally. |
| 10219 | // |
| 10220 | if (SelectInst *SI = dyn_cast<SelectInst>(Op)) { |
| 10221 | // load (select (Cond, &V1, &V2)) --> select(Cond, load &V1, load &V2). |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 10222 | if (isSafeToLoadUnconditionally(SI->getOperand(1), SI) && |
| 10223 | isSafeToLoadUnconditionally(SI->getOperand(2), SI)) { |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 10224 | Value *V1 = InsertNewInstBefore(new LoadInst(SI->getOperand(1), |
Chris Lattner | 79f0c8e | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 10225 | SI->getOperand(1)->getName()+".val"), LI); |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 10226 | Value *V2 = InsertNewInstBefore(new LoadInst(SI->getOperand(2), |
Chris Lattner | 79f0c8e | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 10227 | SI->getOperand(2)->getName()+".val"), LI); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 10228 | return SelectInst::Create(SI->getCondition(), V1, V2); |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 10229 | } |
| 10230 | |
Chris Lattner | 684fe21 | 2004-09-23 15:46:00 +0000 | [diff] [blame] | 10231 | // load (select (cond, null, P)) -> load P |
| 10232 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(1))) |
| 10233 | if (C->isNullValue()) { |
| 10234 | LI.setOperand(0, SI->getOperand(2)); |
| 10235 | return &LI; |
| 10236 | } |
| 10237 | |
| 10238 | // load (select (cond, P, null)) -> load P |
| 10239 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(2))) |
| 10240 | if (C->isNullValue()) { |
| 10241 | LI.setOperand(0, SI->getOperand(1)); |
| 10242 | return &LI; |
| 10243 | } |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 10244 | } |
| 10245 | } |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 10246 | return 0; |
| 10247 | } |
| 10248 | |
Reid Spencer | 55af2b5 | 2007-01-19 21:20:31 +0000 | [diff] [blame] | 10249 | /// InstCombineStoreToCast - Fold store V, (cast P) -> store (cast V), P |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10250 | /// when possible. |
| 10251 | static Instruction *InstCombineStoreToCast(InstCombiner &IC, StoreInst &SI) { |
| 10252 | User *CI = cast<User>(SI.getOperand(1)); |
| 10253 | Value *CastOp = CI->getOperand(0); |
| 10254 | |
| 10255 | const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType(); |
| 10256 | if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) { |
| 10257 | const Type *SrcPTy = SrcTy->getElementType(); |
| 10258 | |
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 10259 | if (DestPTy->isInteger() || isa<PointerType>(DestPTy)) { |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10260 | // If the source is an array, the code below will not succeed. Check to |
| 10261 | // see if a trivial 'gep P, 0, 0' will help matters. Only do this for |
| 10262 | // constants. |
| 10263 | if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy)) |
| 10264 | if (Constant *CSrc = dyn_cast<Constant>(CastOp)) |
| 10265 | if (ASrcTy->getNumElements() != 0) { |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 10266 | Value* Idxs[2]; |
| 10267 | Idxs[0] = Idxs[1] = Constant::getNullValue(Type::Int32Ty); |
| 10268 | CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs, 2); |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10269 | SrcTy = cast<PointerType>(CastOp->getType()); |
| 10270 | SrcPTy = SrcTy->getElementType(); |
| 10271 | } |
| 10272 | |
Reid Spencer | 67f827c | 2007-01-20 23:35:48 +0000 | [diff] [blame] | 10273 | if ((SrcPTy->isInteger() || isa<PointerType>(SrcPTy)) && |
| 10274 | IC.getTargetData().getTypeSizeInBits(SrcPTy) == |
| 10275 | IC.getTargetData().getTypeSizeInBits(DestPTy)) { |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10276 | |
| 10277 | // Okay, we are casting from one integer or pointer type to another of |
Reid Spencer | 7515396 | 2007-01-18 18:54:33 +0000 | [diff] [blame] | 10278 | // the same size. Instead of casting the pointer before |
| 10279 | // the store, cast the value to be stored. |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10280 | Value *NewCast; |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 10281 | Value *SIOp0 = SI.getOperand(0); |
Reid Spencer | 7515396 | 2007-01-18 18:54:33 +0000 | [diff] [blame] | 10282 | Instruction::CastOps opcode = Instruction::BitCast; |
| 10283 | const Type* CastSrcTy = SIOp0->getType(); |
| 10284 | const Type* CastDstTy = SrcPTy; |
| 10285 | if (isa<PointerType>(CastDstTy)) { |
| 10286 | if (CastSrcTy->isInteger()) |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 10287 | opcode = Instruction::IntToPtr; |
Reid Spencer | 67f827c | 2007-01-20 23:35:48 +0000 | [diff] [blame] | 10288 | } else if (isa<IntegerType>(CastDstTy)) { |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 10289 | if (isa<PointerType>(SIOp0->getType())) |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 10290 | opcode = Instruction::PtrToInt; |
| 10291 | } |
| 10292 | if (Constant *C = dyn_cast<Constant>(SIOp0)) |
Reid Spencer | 7515396 | 2007-01-18 18:54:33 +0000 | [diff] [blame] | 10293 | NewCast = ConstantExpr::getCast(opcode, C, CastDstTy); |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10294 | else |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 10295 | NewCast = IC.InsertNewInstBefore( |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 10296 | CastInst::Create(opcode, SIOp0, CastDstTy, SIOp0->getName()+".c"), |
Reid Spencer | 7515396 | 2007-01-18 18:54:33 +0000 | [diff] [blame] | 10297 | SI); |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10298 | return new StoreInst(NewCast, CastOp); |
| 10299 | } |
| 10300 | } |
| 10301 | } |
| 10302 | return 0; |
| 10303 | } |
| 10304 | |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10305 | Instruction *InstCombiner::visitStoreInst(StoreInst &SI) { |
| 10306 | Value *Val = SI.getOperand(0); |
| 10307 | Value *Ptr = SI.getOperand(1); |
| 10308 | |
| 10309 | if (isa<UndefValue>(Ptr)) { // store X, undef -> noop (even if volatile) |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 10310 | EraseInstFromFunction(SI); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10311 | ++NumCombined; |
| 10312 | return 0; |
| 10313 | } |
Chris Lattner | 836692d | 2007-01-15 06:51:56 +0000 | [diff] [blame] | 10314 | |
| 10315 | // If the RHS is an alloca with a single use, zapify the store, making the |
| 10316 | // alloca dead. |
Chris Lattner | cea1fdd | 2008-04-29 04:58:38 +0000 | [diff] [blame] | 10317 | if (Ptr->hasOneUse() && !SI.isVolatile()) { |
Chris Lattner | 836692d | 2007-01-15 06:51:56 +0000 | [diff] [blame] | 10318 | if (isa<AllocaInst>(Ptr)) { |
| 10319 | EraseInstFromFunction(SI); |
| 10320 | ++NumCombined; |
| 10321 | return 0; |
| 10322 | } |
| 10323 | |
| 10324 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) |
| 10325 | if (isa<AllocaInst>(GEP->getOperand(0)) && |
| 10326 | GEP->getOperand(0)->hasOneUse()) { |
| 10327 | EraseInstFromFunction(SI); |
| 10328 | ++NumCombined; |
| 10329 | return 0; |
| 10330 | } |
| 10331 | } |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10332 | |
Dan Gohman | 9941f74 | 2007-07-20 16:34:21 +0000 | [diff] [blame] | 10333 | // Attempt to improve the alignment. |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 10334 | unsigned KnownAlign = GetOrEnforceKnownAlignment(Ptr); |
| 10335 | if (KnownAlign > |
| 10336 | (SI.getAlignment() == 0 ? TD->getABITypeAlignment(Val->getType()) : |
| 10337 | SI.getAlignment())) |
Dan Gohman | 9941f74 | 2007-07-20 16:34:21 +0000 | [diff] [blame] | 10338 | SI.setAlignment(KnownAlign); |
| 10339 | |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 10340 | // Do really simple DSE, to catch cases where there are several consequtive |
| 10341 | // stores to the same location, separated by a few arithmetic operations. This |
| 10342 | // situation often occurs with bitfield accesses. |
| 10343 | BasicBlock::iterator BBI = &SI; |
| 10344 | for (unsigned ScanInsts = 6; BBI != SI.getParent()->begin() && ScanInsts; |
| 10345 | --ScanInsts) { |
| 10346 | --BBI; |
| 10347 | |
| 10348 | if (StoreInst *PrevSI = dyn_cast<StoreInst>(BBI)) { |
| 10349 | // Prev store isn't volatile, and stores to the same location? |
| 10350 | if (!PrevSI->isVolatile() && PrevSI->getOperand(1) == SI.getOperand(1)) { |
| 10351 | ++NumDeadStore; |
| 10352 | ++BBI; |
| 10353 | EraseInstFromFunction(*PrevSI); |
| 10354 | continue; |
| 10355 | } |
| 10356 | break; |
| 10357 | } |
| 10358 | |
Chris Lattner | b4db97f | 2006-05-26 19:19:20 +0000 | [diff] [blame] | 10359 | // If this is a load, we have to stop. However, if the loaded value is from |
| 10360 | // the pointer we're loading and is producing the pointer we're storing, |
| 10361 | // then *this* store is dead (X = load P; store X -> P). |
| 10362 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { |
Chris Lattner | a54c7eb | 2007-09-07 05:33:03 +0000 | [diff] [blame] | 10363 | if (LI == Val && LI->getOperand(0) == Ptr && !SI.isVolatile()) { |
Chris Lattner | b4db97f | 2006-05-26 19:19:20 +0000 | [diff] [blame] | 10364 | EraseInstFromFunction(SI); |
| 10365 | ++NumCombined; |
| 10366 | return 0; |
| 10367 | } |
| 10368 | // Otherwise, this is a load from some other location. Stores before it |
| 10369 | // may not be dead. |
| 10370 | break; |
| 10371 | } |
| 10372 | |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 10373 | // Don't skip over loads or things that can modify memory. |
Chris Lattner | 0ef546e | 2008-05-08 17:20:30 +0000 | [diff] [blame] | 10374 | if (BBI->mayWriteToMemory() || BBI->mayReadFromMemory()) |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 10375 | break; |
| 10376 | } |
| 10377 | |
| 10378 | |
| 10379 | if (SI.isVolatile()) return 0; // Don't hack volatile stores. |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10380 | |
| 10381 | // store X, null -> turns into 'unreachable' in SimplifyCFG |
| 10382 | if (isa<ConstantPointerNull>(Ptr)) { |
| 10383 | if (!isa<UndefValue>(Val)) { |
| 10384 | SI.setOperand(0, UndefValue::get(Val->getType())); |
| 10385 | if (Instruction *U = dyn_cast<Instruction>(Val)) |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10386 | AddToWorkList(U); // Dropped a use. |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10387 | ++NumCombined; |
| 10388 | } |
| 10389 | return 0; // Do not modify these! |
| 10390 | } |
| 10391 | |
| 10392 | // store undef, Ptr -> noop |
| 10393 | if (isa<UndefValue>(Val)) { |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 10394 | EraseInstFromFunction(SI); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10395 | ++NumCombined; |
| 10396 | return 0; |
| 10397 | } |
| 10398 | |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10399 | // If the pointer destination is a cast, see if we can fold the cast into the |
| 10400 | // source instead. |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 10401 | if (isa<CastInst>(Ptr)) |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10402 | if (Instruction *Res = InstCombineStoreToCast(*this, SI)) |
| 10403 | return Res; |
| 10404 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 10405 | if (CE->isCast()) |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10406 | if (Instruction *Res = InstCombineStoreToCast(*this, SI)) |
| 10407 | return Res; |
| 10408 | |
Chris Lattner | 408902b | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 10409 | |
| 10410 | // If this store is the last instruction in the basic block, and if the block |
| 10411 | // ends with an unconditional branch, try to move it to the successor block. |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 10412 | BBI = &SI; ++BBI; |
Chris Lattner | 408902b | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 10413 | if (BranchInst *BI = dyn_cast<BranchInst>(BBI)) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10414 | if (BI->isUnconditional()) |
| 10415 | if (SimplifyStoreAtEndOfBlock(SI)) |
| 10416 | return 0; // xform done! |
Chris Lattner | 408902b | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 10417 | |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10418 | return 0; |
| 10419 | } |
| 10420 | |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10421 | /// SimplifyStoreAtEndOfBlock - Turn things like: |
| 10422 | /// if () { *P = v1; } else { *P = v2 } |
| 10423 | /// into a phi node with a store in the successor. |
| 10424 | /// |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10425 | /// Simplify things like: |
| 10426 | /// *P = v1; if () { *P = v2; } |
| 10427 | /// into a phi node with a store in the successor. |
| 10428 | /// |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10429 | bool InstCombiner::SimplifyStoreAtEndOfBlock(StoreInst &SI) { |
| 10430 | BasicBlock *StoreBB = SI.getParent(); |
| 10431 | |
| 10432 | // Check to see if the successor block has exactly two incoming edges. If |
| 10433 | // so, see if the other predecessor contains a store to the same location. |
| 10434 | // 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] | 10435 | BasicBlock *DestBB = StoreBB->getTerminator()->getSuccessor(0); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10436 | |
| 10437 | // Determine whether Dest has exactly two predecessors and, if so, compute |
| 10438 | // the other predecessor. |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10439 | pred_iterator PI = pred_begin(DestBB); |
| 10440 | BasicBlock *OtherBB = 0; |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10441 | if (*PI != StoreBB) |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10442 | OtherBB = *PI; |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10443 | ++PI; |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10444 | if (PI == pred_end(DestBB)) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10445 | return false; |
| 10446 | |
| 10447 | if (*PI != StoreBB) { |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10448 | if (OtherBB) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10449 | return false; |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10450 | OtherBB = *PI; |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10451 | } |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10452 | if (++PI != pred_end(DestBB)) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10453 | return false; |
Eli Friedman | 66fe80a | 2008-06-13 21:17:49 +0000 | [diff] [blame] | 10454 | |
| 10455 | // Bail out if all the relevant blocks aren't distinct (this can happen, |
| 10456 | // for example, if SI is in an infinite loop) |
| 10457 | if (StoreBB == DestBB || OtherBB == DestBB) |
| 10458 | return false; |
| 10459 | |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10460 | // Verify that the other block ends in a branch and is not otherwise empty. |
| 10461 | BasicBlock::iterator BBI = OtherBB->getTerminator(); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10462 | BranchInst *OtherBr = dyn_cast<BranchInst>(BBI); |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10463 | if (!OtherBr || BBI == OtherBB->begin()) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10464 | return false; |
| 10465 | |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10466 | // If the other block ends in an unconditional branch, check for the 'if then |
| 10467 | // else' case. there is an instruction before the branch. |
| 10468 | StoreInst *OtherStore = 0; |
| 10469 | if (OtherBr->isUnconditional()) { |
| 10470 | // If this isn't a store, or isn't a store to the same location, bail out. |
| 10471 | --BBI; |
| 10472 | OtherStore = dyn_cast<StoreInst>(BBI); |
| 10473 | if (!OtherStore || OtherStore->getOperand(1) != SI.getOperand(1)) |
| 10474 | return false; |
| 10475 | } else { |
Chris Lattner | d717c18 | 2007-05-05 22:32:24 +0000 | [diff] [blame] | 10476 | // 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] | 10477 | // destinations is StoreBB, then we have the if/then case. |
| 10478 | if (OtherBr->getSuccessor(0) != StoreBB && |
| 10479 | OtherBr->getSuccessor(1) != StoreBB) |
| 10480 | return false; |
| 10481 | |
| 10482 | // 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] | 10483 | // if/then triangle. See if there is a store to the same ptr as SI that |
| 10484 | // lives in OtherBB. |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10485 | for (;; --BBI) { |
| 10486 | // Check to see if we find the matching store. |
| 10487 | if ((OtherStore = dyn_cast<StoreInst>(BBI))) { |
| 10488 | if (OtherStore->getOperand(1) != SI.getOperand(1)) |
| 10489 | return false; |
| 10490 | break; |
| 10491 | } |
Eli Friedman | 6903a24 | 2008-06-13 22:02:12 +0000 | [diff] [blame] | 10492 | // If we find something that may be using or overwriting the stored |
| 10493 | // value, or if we run out of instructions, we can't do the xform. |
| 10494 | if (BBI->mayReadFromMemory() || BBI->mayWriteToMemory() || |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10495 | BBI == OtherBB->begin()) |
| 10496 | return false; |
| 10497 | } |
| 10498 | |
| 10499 | // In order to eliminate the store in OtherBr, we have to |
Eli Friedman | 6903a24 | 2008-06-13 22:02:12 +0000 | [diff] [blame] | 10500 | // make sure nothing reads or overwrites the stored value in |
| 10501 | // StoreBB. |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10502 | for (BasicBlock::iterator I = StoreBB->begin(); &*I != &SI; ++I) { |
| 10503 | // FIXME: This should really be AA driven. |
Eli Friedman | 6903a24 | 2008-06-13 22:02:12 +0000 | [diff] [blame] | 10504 | if (I->mayReadFromMemory() || I->mayWriteToMemory()) |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10505 | return false; |
| 10506 | } |
| 10507 | } |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10508 | |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10509 | // Insert a PHI node now if we need it. |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10510 | Value *MergedVal = OtherStore->getOperand(0); |
| 10511 | if (MergedVal != SI.getOperand(0)) { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 10512 | PHINode *PN = PHINode::Create(MergedVal->getType(), "storemerge"); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10513 | PN->reserveOperandSpace(2); |
| 10514 | PN->addIncoming(SI.getOperand(0), SI.getParent()); |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10515 | PN->addIncoming(OtherStore->getOperand(0), OtherBB); |
| 10516 | MergedVal = InsertNewInstBefore(PN, DestBB->front()); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10517 | } |
| 10518 | |
| 10519 | // Advance to a place where it is safe to insert the new store and |
| 10520 | // insert it. |
Dan Gohman | 02dea8b | 2008-05-23 21:05:58 +0000 | [diff] [blame] | 10521 | BBI = DestBB->getFirstNonPHI(); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10522 | InsertNewInstBefore(new StoreInst(MergedVal, SI.getOperand(1), |
| 10523 | OtherStore->isVolatile()), *BBI); |
| 10524 | |
| 10525 | // Nuke the old stores. |
| 10526 | EraseInstFromFunction(SI); |
| 10527 | EraseInstFromFunction(*OtherStore); |
| 10528 | ++NumCombined; |
| 10529 | return true; |
| 10530 | } |
| 10531 | |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10532 | |
Chris Lattner | c4d10eb | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 10533 | Instruction *InstCombiner::visitBranchInst(BranchInst &BI) { |
| 10534 | // 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] | 10535 | Value *X = 0; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 10536 | BasicBlock *TrueDest; |
| 10537 | BasicBlock *FalseDest; |
| 10538 | if (match(&BI, m_Br(m_Not(m_Value(X)), TrueDest, FalseDest)) && |
| 10539 | !isa<Constant>(X)) { |
| 10540 | // Swap Destinations and condition... |
| 10541 | BI.setCondition(X); |
| 10542 | BI.setSuccessor(0, FalseDest); |
| 10543 | BI.setSuccessor(1, TrueDest); |
| 10544 | return &BI; |
| 10545 | } |
| 10546 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10547 | // Cannonicalize fcmp_one -> fcmp_oeq |
| 10548 | FCmpInst::Predicate FPred; Value *Y; |
| 10549 | if (match(&BI, m_Br(m_FCmp(FPred, m_Value(X), m_Value(Y)), |
| 10550 | TrueDest, FalseDest))) |
| 10551 | if ((FPred == FCmpInst::FCMP_ONE || FPred == FCmpInst::FCMP_OLE || |
| 10552 | FPred == FCmpInst::FCMP_OGE) && BI.getCondition()->hasOneUse()) { |
| 10553 | FCmpInst *I = cast<FCmpInst>(BI.getCondition()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10554 | FCmpInst::Predicate NewPred = FCmpInst::getInversePredicate(FPred); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 10555 | Instruction *NewSCC = new FCmpInst(NewPred, X, Y, "", I); |
| 10556 | NewSCC->takeName(I); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10557 | // Swap Destinations and condition... |
| 10558 | BI.setCondition(NewSCC); |
| 10559 | BI.setSuccessor(0, FalseDest); |
| 10560 | BI.setSuccessor(1, TrueDest); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10561 | RemoveFromWorkList(I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 10562 | I->eraseFromParent(); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10563 | AddToWorkList(NewSCC); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10564 | return &BI; |
| 10565 | } |
| 10566 | |
| 10567 | // Cannonicalize icmp_ne -> icmp_eq |
| 10568 | ICmpInst::Predicate IPred; |
| 10569 | if (match(&BI, m_Br(m_ICmp(IPred, m_Value(X), m_Value(Y)), |
| 10570 | TrueDest, FalseDest))) |
| 10571 | if ((IPred == ICmpInst::ICMP_NE || IPred == ICmpInst::ICMP_ULE || |
| 10572 | IPred == ICmpInst::ICMP_SLE || IPred == ICmpInst::ICMP_UGE || |
| 10573 | IPred == ICmpInst::ICMP_SGE) && BI.getCondition()->hasOneUse()) { |
| 10574 | ICmpInst *I = cast<ICmpInst>(BI.getCondition()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10575 | ICmpInst::Predicate NewPred = ICmpInst::getInversePredicate(IPred); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 10576 | Instruction *NewSCC = new ICmpInst(NewPred, X, Y, "", I); |
| 10577 | NewSCC->takeName(I); |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 10578 | // Swap Destinations and condition... |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 10579 | BI.setCondition(NewSCC); |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 10580 | BI.setSuccessor(0, FalseDest); |
| 10581 | BI.setSuccessor(1, TrueDest); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10582 | RemoveFromWorkList(I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 10583 | I->eraseFromParent();; |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10584 | AddToWorkList(NewSCC); |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 10585 | return &BI; |
| 10586 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 10587 | |
Chris Lattner | c4d10eb | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 10588 | return 0; |
| 10589 | } |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 10590 | |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 10591 | Instruction *InstCombiner::visitSwitchInst(SwitchInst &SI) { |
| 10592 | Value *Cond = SI.getCondition(); |
| 10593 | if (Instruction *I = dyn_cast<Instruction>(Cond)) { |
| 10594 | if (I->getOpcode() == Instruction::Add) |
| 10595 | if (ConstantInt *AddRHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 10596 | // change 'switch (X+4) case 1:' into 'switch (X) case -3' |
| 10597 | for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10598 | SI.setOperand(i,ConstantExpr::getSub(cast<Constant>(SI.getOperand(i)), |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 10599 | AddRHS)); |
| 10600 | SI.setOperand(0, I->getOperand(0)); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10601 | AddToWorkList(I); |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 10602 | return &SI; |
| 10603 | } |
| 10604 | } |
| 10605 | return 0; |
| 10606 | } |
| 10607 | |
Matthijs Kooijman | a9012ec | 2008-06-11 14:05:05 +0000 | [diff] [blame] | 10608 | Instruction *InstCombiner::visitExtractValueInst(ExtractValueInst &EV) { |
| 10609 | // See if we are trying to extract a known value. If so, use that instead. |
Matthijs Kooijman | 710eb23 | 2008-06-16 12:57:37 +0000 | [diff] [blame] | 10610 | if (Value *Elt = FindInsertedValue(EV.getOperand(0), EV.idx_begin(), |
Matthijs Kooijman | 0a7413d | 2008-06-16 13:13:08 +0000 | [diff] [blame] | 10611 | EV.idx_end(), &EV)) |
Matthijs Kooijman | a9012ec | 2008-06-11 14:05:05 +0000 | [diff] [blame] | 10612 | return ReplaceInstUsesWith(EV, Elt); |
| 10613 | |
| 10614 | // No changes |
| 10615 | return 0; |
| 10616 | } |
| 10617 | |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10618 | /// CheapToScalarize - Return true if the value is cheaper to scalarize than it |
| 10619 | /// is to leave as a vector operation. |
| 10620 | static bool CheapToScalarize(Value *V, bool isConstant) { |
| 10621 | if (isa<ConstantAggregateZero>(V)) |
| 10622 | return true; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10623 | if (ConstantVector *C = dyn_cast<ConstantVector>(V)) { |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10624 | if (isConstant) return true; |
| 10625 | // If all elts are the same, we can extract. |
| 10626 | Constant *Op0 = C->getOperand(0); |
| 10627 | for (unsigned i = 1; i < C->getNumOperands(); ++i) |
| 10628 | if (C->getOperand(i) != Op0) |
| 10629 | return false; |
| 10630 | return true; |
| 10631 | } |
| 10632 | Instruction *I = dyn_cast<Instruction>(V); |
| 10633 | if (!I) return false; |
| 10634 | |
| 10635 | // Insert element gets simplified to the inserted element or is deleted if |
| 10636 | // this is constant idx extract element and its a constant idx insertelt. |
| 10637 | if (I->getOpcode() == Instruction::InsertElement && isConstant && |
| 10638 | isa<ConstantInt>(I->getOperand(2))) |
| 10639 | return true; |
| 10640 | if (I->getOpcode() == Instruction::Load && I->hasOneUse()) |
| 10641 | return true; |
| 10642 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) |
| 10643 | if (BO->hasOneUse() && |
| 10644 | (CheapToScalarize(BO->getOperand(0), isConstant) || |
| 10645 | CheapToScalarize(BO->getOperand(1), isConstant))) |
| 10646 | return true; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10647 | if (CmpInst *CI = dyn_cast<CmpInst>(I)) |
| 10648 | if (CI->hasOneUse() && |
| 10649 | (CheapToScalarize(CI->getOperand(0), isConstant) || |
| 10650 | CheapToScalarize(CI->getOperand(1), isConstant))) |
| 10651 | return true; |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10652 | |
| 10653 | return false; |
| 10654 | } |
| 10655 | |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 10656 | /// Read and decode a shufflevector mask. |
| 10657 | /// |
| 10658 | /// It turns undef elements into values that are larger than the number of |
| 10659 | /// elements in the input. |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10660 | static std::vector<unsigned> getShuffleMask(const ShuffleVectorInst *SVI) { |
| 10661 | unsigned NElts = SVI->getType()->getNumElements(); |
| 10662 | if (isa<ConstantAggregateZero>(SVI->getOperand(2))) |
| 10663 | return std::vector<unsigned>(NElts, 0); |
| 10664 | if (isa<UndefValue>(SVI->getOperand(2))) |
| 10665 | return std::vector<unsigned>(NElts, 2*NElts); |
| 10666 | |
| 10667 | std::vector<unsigned> Result; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10668 | const ConstantVector *CP = cast<ConstantVector>(SVI->getOperand(2)); |
Gabor Greif | 177dd3f | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 10669 | for (User::const_op_iterator i = CP->op_begin(), e = CP->op_end(); i!=e; ++i) |
| 10670 | if (isa<UndefValue>(*i)) |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10671 | Result.push_back(NElts*2); // undef -> 8 |
| 10672 | else |
Gabor Greif | 177dd3f | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 10673 | Result.push_back(cast<ConstantInt>(*i)->getZExtValue()); |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10674 | return Result; |
| 10675 | } |
| 10676 | |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10677 | /// FindScalarElement - Given a vector and an element number, see if the scalar |
| 10678 | /// value is already around as a register, for example if it were inserted then |
| 10679 | /// extracted from the vector. |
| 10680 | static Value *FindScalarElement(Value *V, unsigned EltNo) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10681 | assert(isa<VectorType>(V->getType()) && "Not looking at a vector?"); |
| 10682 | const VectorType *PTy = cast<VectorType>(V->getType()); |
Chris Lattner | 389a6f5 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 10683 | unsigned Width = PTy->getNumElements(); |
| 10684 | if (EltNo >= Width) // Out of range access. |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10685 | return UndefValue::get(PTy->getElementType()); |
| 10686 | |
| 10687 | if (isa<UndefValue>(V)) |
| 10688 | return UndefValue::get(PTy->getElementType()); |
| 10689 | else if (isa<ConstantAggregateZero>(V)) |
| 10690 | return Constant::getNullValue(PTy->getElementType()); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10691 | else if (ConstantVector *CP = dyn_cast<ConstantVector>(V)) |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10692 | return CP->getOperand(EltNo); |
| 10693 | else if (InsertElementInst *III = dyn_cast<InsertElementInst>(V)) { |
| 10694 | // 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] | 10695 | if (!isa<ConstantInt>(III->getOperand(2))) |
| 10696 | return 0; |
| 10697 | unsigned IIElt = cast<ConstantInt>(III->getOperand(2))->getZExtValue(); |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10698 | |
| 10699 | // If this is an insert to the element we are looking for, return the |
| 10700 | // inserted value. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10701 | if (EltNo == IIElt) |
| 10702 | return III->getOperand(1); |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10703 | |
| 10704 | // Otherwise, the insertelement doesn't modify the value, recurse on its |
| 10705 | // vector input. |
| 10706 | return FindScalarElement(III->getOperand(0), EltNo); |
Chris Lattner | 389a6f5 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 10707 | } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(V)) { |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10708 | unsigned InEl = getShuffleMask(SVI)[EltNo]; |
| 10709 | if (InEl < Width) |
| 10710 | return FindScalarElement(SVI->getOperand(0), InEl); |
| 10711 | else if (InEl < Width*2) |
| 10712 | return FindScalarElement(SVI->getOperand(1), InEl - Width); |
| 10713 | else |
| 10714 | return UndefValue::get(PTy->getElementType()); |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10715 | } |
| 10716 | |
| 10717 | // Otherwise, we don't know. |
| 10718 | return 0; |
| 10719 | } |
| 10720 | |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10721 | Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) { |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 10722 | // If vector val is undef, replace extract with scalar undef. |
Chris Lattner | 1f13c88 | 2006-03-31 18:25:14 +0000 | [diff] [blame] | 10723 | if (isa<UndefValue>(EI.getOperand(0))) |
| 10724 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); |
| 10725 | |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 10726 | // If vector val is constant 0, replace extract with scalar 0. |
Chris Lattner | 1f13c88 | 2006-03-31 18:25:14 +0000 | [diff] [blame] | 10727 | if (isa<ConstantAggregateZero>(EI.getOperand(0))) |
| 10728 | return ReplaceInstUsesWith(EI, Constant::getNullValue(EI.getType())); |
| 10729 | |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10730 | if (ConstantVector *C = dyn_cast<ConstantVector>(EI.getOperand(0))) { |
Matthijs Kooijman | b4d6a5a | 2008-06-11 09:00:12 +0000 | [diff] [blame] | 10731 | // If vector val is constant with all elements the same, replace EI with |
| 10732 | // that element. When the elements are not identical, we cannot replace yet |
| 10733 | // (we do that below, but only when the index is constant). |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10734 | Constant *op0 = C->getOperand(0); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10735 | for (unsigned i = 1; i < C->getNumOperands(); ++i) |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10736 | if (C->getOperand(i) != op0) { |
| 10737 | op0 = 0; |
| 10738 | break; |
| 10739 | } |
| 10740 | if (op0) |
| 10741 | return ReplaceInstUsesWith(EI, op0); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10742 | } |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10743 | |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10744 | // If extracting a specified index from the vector, see if we can recursively |
| 10745 | // find a previously computed scalar that was inserted into the vector. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10746 | if (ConstantInt *IdxC = dyn_cast<ConstantInt>(EI.getOperand(1))) { |
Chris Lattner | 8546409 | 2007-04-09 01:37:55 +0000 | [diff] [blame] | 10747 | unsigned IndexVal = IdxC->getZExtValue(); |
| 10748 | unsigned VectorWidth = |
| 10749 | cast<VectorType>(EI.getOperand(0)->getType())->getNumElements(); |
| 10750 | |
| 10751 | // If this is extracting an invalid index, turn this into undef, to avoid |
| 10752 | // crashing the code below. |
| 10753 | if (IndexVal >= VectorWidth) |
| 10754 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); |
| 10755 | |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 10756 | // This instruction only demands the single element from the input vector. |
| 10757 | // If the input vector has a single use, simplify it based on this use |
| 10758 | // property. |
Chris Lattner | 8546409 | 2007-04-09 01:37:55 +0000 | [diff] [blame] | 10759 | if (EI.getOperand(0)->hasOneUse() && VectorWidth != 1) { |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 10760 | uint64_t UndefElts; |
| 10761 | if (Value *V = SimplifyDemandedVectorElts(EI.getOperand(0), |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10762 | 1 << IndexVal, |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 10763 | UndefElts)) { |
| 10764 | EI.setOperand(0, V); |
| 10765 | return &EI; |
| 10766 | } |
| 10767 | } |
| 10768 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10769 | if (Value *Elt = FindScalarElement(EI.getOperand(0), IndexVal)) |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10770 | return ReplaceInstUsesWith(EI, Elt); |
Chris Lattner | b7300fa | 2007-04-14 23:02:14 +0000 | [diff] [blame] | 10771 | |
| 10772 | // If the this extractelement is directly using a bitcast from a vector of |
| 10773 | // the same number of elements, see if we can find the source element from |
| 10774 | // it. In this case, we will end up needing to bitcast the scalars. |
| 10775 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(EI.getOperand(0))) { |
| 10776 | if (const VectorType *VT = |
| 10777 | dyn_cast<VectorType>(BCI->getOperand(0)->getType())) |
| 10778 | if (VT->getNumElements() == VectorWidth) |
| 10779 | if (Value *Elt = FindScalarElement(BCI->getOperand(0), IndexVal)) |
| 10780 | return new BitCastInst(Elt, EI.getType()); |
| 10781 | } |
Chris Lattner | 389a6f5 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 10782 | } |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10783 | |
Chris Lattner | 73fa49d | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 10784 | if (Instruction *I = dyn_cast<Instruction>(EI.getOperand(0))) { |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10785 | if (I->hasOneUse()) { |
| 10786 | // Push extractelement into predecessor operation if legal and |
| 10787 | // profitable to do so |
| 10788 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) { |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10789 | bool isConstantElt = isa<ConstantInt>(EI.getOperand(1)); |
| 10790 | if (CheapToScalarize(BO, isConstantElt)) { |
| 10791 | ExtractElementInst *newEI0 = |
| 10792 | new ExtractElementInst(BO->getOperand(0), EI.getOperand(1), |
| 10793 | EI.getName()+".lhs"); |
| 10794 | ExtractElementInst *newEI1 = |
| 10795 | new ExtractElementInst(BO->getOperand(1), EI.getOperand(1), |
| 10796 | EI.getName()+".rhs"); |
| 10797 | InsertNewInstBefore(newEI0, EI); |
| 10798 | InsertNewInstBefore(newEI1, EI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 10799 | return BinaryOperator::Create(BO->getOpcode(), newEI0, newEI1); |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10800 | } |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 10801 | } else if (isa<LoadInst>(I)) { |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 10802 | unsigned AS = |
| 10803 | cast<PointerType>(I->getOperand(0)->getType())->getAddressSpace(); |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 10804 | Value *Ptr = InsertBitCastBefore(I->getOperand(0), |
| 10805 | PointerType::get(EI.getType(), AS),EI); |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 10806 | GetElementPtrInst *GEP = |
| 10807 | GetElementPtrInst::Create(Ptr, EI.getOperand(1), I->getName()+".gep"); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10808 | InsertNewInstBefore(GEP, EI); |
| 10809 | return new LoadInst(GEP); |
Chris Lattner | 73fa49d | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 10810 | } |
| 10811 | } |
| 10812 | if (InsertElementInst *IE = dyn_cast<InsertElementInst>(I)) { |
| 10813 | // Extracting the inserted element? |
| 10814 | if (IE->getOperand(2) == EI.getOperand(1)) |
| 10815 | return ReplaceInstUsesWith(EI, IE->getOperand(1)); |
| 10816 | // If the inserted and extracted elements are constants, they must not |
| 10817 | // be the same value, extract from the pre-inserted value instead. |
| 10818 | if (isa<Constant>(IE->getOperand(2)) && |
| 10819 | isa<Constant>(EI.getOperand(1))) { |
| 10820 | AddUsesToWorkList(EI); |
| 10821 | EI.setOperand(0, IE->getOperand(0)); |
| 10822 | return &EI; |
| 10823 | } |
| 10824 | } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(I)) { |
| 10825 | // If this is extracting an element from a shufflevector, figure out where |
| 10826 | // it came from and extract from the appropriate input element instead. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10827 | if (ConstantInt *Elt = dyn_cast<ConstantInt>(EI.getOperand(1))) { |
| 10828 | unsigned SrcIdx = getShuffleMask(SVI)[Elt->getZExtValue()]; |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10829 | Value *Src; |
| 10830 | if (SrcIdx < SVI->getType()->getNumElements()) |
| 10831 | Src = SVI->getOperand(0); |
| 10832 | else if (SrcIdx < SVI->getType()->getNumElements()*2) { |
| 10833 | SrcIdx -= SVI->getType()->getNumElements(); |
| 10834 | Src = SVI->getOperand(1); |
| 10835 | } else { |
| 10836 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); |
Chris Lattner | df084ff | 2006-03-30 22:02:40 +0000 | [diff] [blame] | 10837 | } |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 10838 | return new ExtractElementInst(Src, SrcIdx); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10839 | } |
| 10840 | } |
Chris Lattner | 73fa49d | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 10841 | } |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10842 | return 0; |
| 10843 | } |
| 10844 | |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10845 | /// CollectSingleShuffleElements - If V is a shuffle of values that ONLY returns |
| 10846 | /// elements from either LHS or RHS, return the shuffle mask and true. |
| 10847 | /// Otherwise, return false. |
| 10848 | static bool CollectSingleShuffleElements(Value *V, Value *LHS, Value *RHS, |
| 10849 | std::vector<Constant*> &Mask) { |
| 10850 | assert(V->getType() == LHS->getType() && V->getType() == RHS->getType() && |
| 10851 | "Invalid CollectSingleShuffleElements"); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10852 | unsigned NumElts = cast<VectorType>(V->getType())->getNumElements(); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10853 | |
| 10854 | if (isa<UndefValue>(V)) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10855 | Mask.assign(NumElts, UndefValue::get(Type::Int32Ty)); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10856 | return true; |
| 10857 | } else if (V == LHS) { |
| 10858 | for (unsigned i = 0; i != NumElts; ++i) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10859 | Mask.push_back(ConstantInt::get(Type::Int32Ty, i)); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10860 | return true; |
| 10861 | } else if (V == RHS) { |
| 10862 | for (unsigned i = 0; i != NumElts; ++i) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10863 | Mask.push_back(ConstantInt::get(Type::Int32Ty, i+NumElts)); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10864 | return true; |
| 10865 | } else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) { |
| 10866 | // If this is an insert of an extract from some other vector, include it. |
| 10867 | Value *VecOp = IEI->getOperand(0); |
| 10868 | Value *ScalarOp = IEI->getOperand(1); |
| 10869 | Value *IdxOp = IEI->getOperand(2); |
| 10870 | |
Chris Lattner | d929f06 | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 10871 | if (!isa<ConstantInt>(IdxOp)) |
| 10872 | return false; |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10873 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Chris Lattner | d929f06 | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 10874 | |
| 10875 | if (isa<UndefValue>(ScalarOp)) { // inserting undef into vector. |
| 10876 | // Okay, we can handle this if the vector we are insertinting into is |
| 10877 | // transitively ok. |
| 10878 | if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) { |
| 10879 | // If so, update the mask to reflect the inserted undef. |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10880 | Mask[InsertedIdx] = UndefValue::get(Type::Int32Ty); |
Chris Lattner | d929f06 | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 10881 | return true; |
| 10882 | } |
| 10883 | } else if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)){ |
| 10884 | if (isa<ConstantInt>(EI->getOperand(1)) && |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10885 | EI->getOperand(0)->getType() == V->getType()) { |
| 10886 | unsigned ExtractedIdx = |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10887 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10888 | |
| 10889 | // This must be extracting from either LHS or RHS. |
| 10890 | if (EI->getOperand(0) == LHS || EI->getOperand(0) == RHS) { |
| 10891 | // Okay, we can handle this if the vector we are insertinting into is |
| 10892 | // transitively ok. |
| 10893 | if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) { |
| 10894 | // If so, update the mask to reflect the inserted value. |
| 10895 | if (EI->getOperand(0) == LHS) { |
| 10896 | Mask[InsertedIdx & (NumElts-1)] = |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10897 | ConstantInt::get(Type::Int32Ty, ExtractedIdx); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10898 | } else { |
| 10899 | assert(EI->getOperand(0) == RHS); |
| 10900 | Mask[InsertedIdx & (NumElts-1)] = |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10901 | ConstantInt::get(Type::Int32Ty, ExtractedIdx+NumElts); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10902 | |
| 10903 | } |
| 10904 | return true; |
| 10905 | } |
| 10906 | } |
| 10907 | } |
| 10908 | } |
| 10909 | } |
| 10910 | // TODO: Handle shufflevector here! |
| 10911 | |
| 10912 | return false; |
| 10913 | } |
| 10914 | |
| 10915 | /// CollectShuffleElements - We are building a shuffle of V, using RHS as the |
| 10916 | /// RHS of the shuffle instruction, if it is not null. Return a shuffle mask |
| 10917 | /// that computes V and the LHS value of the shuffle. |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10918 | static Value *CollectShuffleElements(Value *V, std::vector<Constant*> &Mask, |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10919 | Value *&RHS) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10920 | assert(isa<VectorType>(V->getType()) && |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10921 | (RHS == 0 || V->getType() == RHS->getType()) && |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10922 | "Invalid shuffle!"); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10923 | unsigned NumElts = cast<VectorType>(V->getType())->getNumElements(); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10924 | |
| 10925 | if (isa<UndefValue>(V)) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10926 | Mask.assign(NumElts, UndefValue::get(Type::Int32Ty)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10927 | return V; |
| 10928 | } else if (isa<ConstantAggregateZero>(V)) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10929 | Mask.assign(NumElts, ConstantInt::get(Type::Int32Ty, 0)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10930 | return V; |
| 10931 | } else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) { |
| 10932 | // If this is an insert of an extract from some other vector, include it. |
| 10933 | Value *VecOp = IEI->getOperand(0); |
| 10934 | Value *ScalarOp = IEI->getOperand(1); |
| 10935 | Value *IdxOp = IEI->getOperand(2); |
| 10936 | |
| 10937 | if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) { |
| 10938 | if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) && |
| 10939 | EI->getOperand(0)->getType() == V->getType()) { |
| 10940 | unsigned ExtractedIdx = |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10941 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
| 10942 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10943 | |
| 10944 | // Either the extracted from or inserted into vector must be RHSVec, |
| 10945 | // otherwise we'd end up with a shuffle of three inputs. |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10946 | if (EI->getOperand(0) == RHS || RHS == 0) { |
| 10947 | RHS = EI->getOperand(0); |
| 10948 | Value *V = CollectShuffleElements(VecOp, Mask, RHS); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10949 | Mask[InsertedIdx & (NumElts-1)] = |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10950 | ConstantInt::get(Type::Int32Ty, NumElts+ExtractedIdx); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10951 | return V; |
| 10952 | } |
| 10953 | |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10954 | if (VecOp == RHS) { |
| 10955 | Value *V = CollectShuffleElements(EI->getOperand(0), Mask, RHS); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10956 | // Everything but the extracted element is replaced with the RHS. |
| 10957 | for (unsigned i = 0; i != NumElts; ++i) { |
| 10958 | if (i != InsertedIdx) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10959 | Mask[i] = ConstantInt::get(Type::Int32Ty, NumElts+i); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10960 | } |
| 10961 | return V; |
| 10962 | } |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10963 | |
| 10964 | // If this insertelement is a chain that comes from exactly these two |
| 10965 | // vectors, return the vector and the effective shuffle. |
| 10966 | if (CollectSingleShuffleElements(IEI, EI->getOperand(0), RHS, Mask)) |
| 10967 | return EI->getOperand(0); |
| 10968 | |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10969 | } |
| 10970 | } |
| 10971 | } |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10972 | // TODO: Handle shufflevector here! |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10973 | |
| 10974 | // Otherwise, can't do anything fancy. Return an identity vector. |
| 10975 | for (unsigned i = 0; i != NumElts; ++i) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10976 | Mask.push_back(ConstantInt::get(Type::Int32Ty, i)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10977 | return V; |
| 10978 | } |
| 10979 | |
| 10980 | Instruction *InstCombiner::visitInsertElementInst(InsertElementInst &IE) { |
| 10981 | Value *VecOp = IE.getOperand(0); |
| 10982 | Value *ScalarOp = IE.getOperand(1); |
| 10983 | Value *IdxOp = IE.getOperand(2); |
| 10984 | |
Chris Lattner | 599ded1 | 2007-04-09 01:11:16 +0000 | [diff] [blame] | 10985 | // Inserting an undef or into an undefined place, remove this. |
| 10986 | if (isa<UndefValue>(ScalarOp) || isa<UndefValue>(IdxOp)) |
| 10987 | ReplaceInstUsesWith(IE, VecOp); |
| 10988 | |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10989 | // If the inserted element was extracted from some other vector, and if the |
| 10990 | // indexes are constant, try to turn this into a shufflevector operation. |
| 10991 | if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) { |
| 10992 | if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) && |
| 10993 | EI->getOperand(0)->getType() == IE.getType()) { |
| 10994 | unsigned NumVectorElts = IE.getType()->getNumElements(); |
Chris Lattner | e34e9a2 | 2007-04-14 23:32:02 +0000 | [diff] [blame] | 10995 | unsigned ExtractedIdx = |
| 10996 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10997 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10998 | |
| 10999 | if (ExtractedIdx >= NumVectorElts) // Out of range extract. |
| 11000 | return ReplaceInstUsesWith(IE, VecOp); |
| 11001 | |
| 11002 | if (InsertedIdx >= NumVectorElts) // Out of range insert. |
| 11003 | return ReplaceInstUsesWith(IE, UndefValue::get(IE.getType())); |
| 11004 | |
| 11005 | // If we are extracting a value from a vector, then inserting it right |
| 11006 | // back into the same place, just use the input vector. |
| 11007 | if (EI->getOperand(0) == VecOp && ExtractedIdx == InsertedIdx) |
| 11008 | return ReplaceInstUsesWith(IE, VecOp); |
| 11009 | |
| 11010 | // We could theoretically do this for ANY input. However, doing so could |
| 11011 | // turn chains of insertelement instructions into a chain of shufflevector |
| 11012 | // instructions, and right now we do not merge shufflevectors. As such, |
| 11013 | // only do this in a situation where it is clear that there is benefit. |
| 11014 | if (isa<UndefValue>(VecOp) || isa<ConstantAggregateZero>(VecOp)) { |
| 11015 | // Turn this into shuffle(EIOp0, VecOp, Mask). The result has all of |
| 11016 | // the values of VecOp, except then one read from EIOp0. |
| 11017 | // Build a new shuffle mask. |
| 11018 | std::vector<Constant*> Mask; |
| 11019 | if (isa<UndefValue>(VecOp)) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 11020 | Mask.assign(NumVectorElts, UndefValue::get(Type::Int32Ty)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11021 | else { |
| 11022 | assert(isa<ConstantAggregateZero>(VecOp) && "Unknown thing"); |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 11023 | Mask.assign(NumVectorElts, ConstantInt::get(Type::Int32Ty, |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11024 | NumVectorElts)); |
| 11025 | } |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 11026 | Mask[InsertedIdx] = ConstantInt::get(Type::Int32Ty, ExtractedIdx); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11027 | return new ShuffleVectorInst(EI->getOperand(0), VecOp, |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 11028 | ConstantVector::get(Mask)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11029 | } |
| 11030 | |
| 11031 | // If this insertelement isn't used by some other insertelement, turn it |
| 11032 | // (and any insertelements it points to), into one big shuffle. |
| 11033 | if (!IE.hasOneUse() || !isa<InsertElementInst>(IE.use_back())) { |
| 11034 | std::vector<Constant*> Mask; |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 11035 | Value *RHS = 0; |
| 11036 | Value *LHS = CollectShuffleElements(&IE, Mask, RHS); |
| 11037 | if (RHS == 0) RHS = UndefValue::get(LHS->getType()); |
| 11038 | // We now have a shuffle of LHS, RHS, Mask. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 11039 | return new ShuffleVectorInst(LHS, RHS, ConstantVector::get(Mask)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11040 | } |
| 11041 | } |
| 11042 | } |
| 11043 | |
| 11044 | return 0; |
| 11045 | } |
| 11046 | |
| 11047 | |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 11048 | Instruction *InstCombiner::visitShuffleVectorInst(ShuffleVectorInst &SVI) { |
| 11049 | Value *LHS = SVI.getOperand(0); |
| 11050 | Value *RHS = SVI.getOperand(1); |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 11051 | std::vector<unsigned> Mask = getShuffleMask(&SVI); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 11052 | |
| 11053 | bool MadeChange = false; |
| 11054 | |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 11055 | // Undefined shuffle mask -> undefined value. |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 11056 | if (isa<UndefValue>(SVI.getOperand(2))) |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 11057 | return ReplaceInstUsesWith(SVI, UndefValue::get(SVI.getType())); |
| 11058 | |
Chris Lattner | e4929dd | 2007-01-05 07:36:08 +0000 | [diff] [blame] | 11059 | // If we have shuffle(x, undef, mask) and any elements of mask refer to |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11060 | // the undef, change them to undefs. |
Chris Lattner | e4929dd | 2007-01-05 07:36:08 +0000 | [diff] [blame] | 11061 | if (isa<UndefValue>(SVI.getOperand(1))) { |
| 11062 | // Scan to see if there are any references to the RHS. If so, replace them |
| 11063 | // with undef element refs and set MadeChange to true. |
| 11064 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
| 11065 | if (Mask[i] >= e && Mask[i] != 2*e) { |
| 11066 | Mask[i] = 2*e; |
| 11067 | MadeChange = true; |
| 11068 | } |
| 11069 | } |
| 11070 | |
| 11071 | if (MadeChange) { |
| 11072 | // Remap any references to RHS to use LHS. |
| 11073 | std::vector<Constant*> Elts; |
| 11074 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
| 11075 | if (Mask[i] == 2*e) |
| 11076 | Elts.push_back(UndefValue::get(Type::Int32Ty)); |
| 11077 | else |
| 11078 | Elts.push_back(ConstantInt::get(Type::Int32Ty, Mask[i])); |
| 11079 | } |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 11080 | SVI.setOperand(2, ConstantVector::get(Elts)); |
Chris Lattner | e4929dd | 2007-01-05 07:36:08 +0000 | [diff] [blame] | 11081 | } |
| 11082 | } |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11083 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 11084 | // Canonicalize shuffle(x ,x,mask) -> shuffle(x, undef,mask') |
| 11085 | // Canonicalize shuffle(undef,x,mask) -> shuffle(x, undef,mask'). |
| 11086 | if (LHS == RHS || isa<UndefValue>(LHS)) { |
| 11087 | if (isa<UndefValue>(LHS) && LHS == RHS) { |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 11088 | // shuffle(undef,undef,mask) -> undef. |
| 11089 | return ReplaceInstUsesWith(SVI, LHS); |
| 11090 | } |
| 11091 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 11092 | // Remap any references to RHS to use LHS. |
| 11093 | std::vector<Constant*> Elts; |
| 11094 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 11095 | if (Mask[i] >= 2*e) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 11096 | Elts.push_back(UndefValue::get(Type::Int32Ty)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 11097 | else { |
| 11098 | if ((Mask[i] >= e && isa<UndefValue>(RHS)) || |
| 11099 | (Mask[i] < e && isa<UndefValue>(LHS))) |
| 11100 | Mask[i] = 2*e; // Turn into undef. |
| 11101 | else |
| 11102 | Mask[i] &= (e-1); // Force to LHS. |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 11103 | Elts.push_back(ConstantInt::get(Type::Int32Ty, Mask[i])); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 11104 | } |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 11105 | } |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 11106 | SVI.setOperand(0, SVI.getOperand(1)); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 11107 | SVI.setOperand(1, UndefValue::get(RHS->getType())); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 11108 | SVI.setOperand(2, ConstantVector::get(Elts)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 11109 | LHS = SVI.getOperand(0); |
| 11110 | RHS = SVI.getOperand(1); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 11111 | MadeChange = true; |
| 11112 | } |
| 11113 | |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 11114 | // Analyze the shuffle, are the LHS or RHS and identity shuffles? |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 11115 | bool isLHSID = true, isRHSID = true; |
Chris Lattner | 706126d | 2006-04-16 00:03:56 +0000 | [diff] [blame] | 11116 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 11117 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
| 11118 | if (Mask[i] >= e*2) continue; // Ignore undef values. |
| 11119 | // Is this an identity shuffle of the LHS value? |
| 11120 | isLHSID &= (Mask[i] == i); |
| 11121 | |
| 11122 | // Is this an identity shuffle of the RHS value? |
| 11123 | isRHSID &= (Mask[i]-e == i); |
Chris Lattner | 706126d | 2006-04-16 00:03:56 +0000 | [diff] [blame] | 11124 | } |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 11125 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 11126 | // Eliminate identity shuffles. |
| 11127 | if (isLHSID) return ReplaceInstUsesWith(SVI, LHS); |
| 11128 | if (isRHSID) return ReplaceInstUsesWith(SVI, RHS); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 11129 | |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 11130 | // If the LHS is a shufflevector itself, see if we can combine it with this |
| 11131 | // one without producing an unusual shuffle. Here we are really conservative: |
| 11132 | // we are absolutely afraid of producing a shuffle mask not in the input |
| 11133 | // program, because the code gen may not be smart enough to turn a merged |
| 11134 | // shuffle into two specific shuffles: it may produce worse code. As such, |
| 11135 | // we only merge two shuffles if the result is one of the two input shuffle |
| 11136 | // masks. In this case, merging the shuffles just removes one instruction, |
| 11137 | // which we know is safe. This is good for things like turning: |
| 11138 | // (splat(splat)) -> splat. |
| 11139 | if (ShuffleVectorInst *LHSSVI = dyn_cast<ShuffleVectorInst>(LHS)) { |
| 11140 | if (isa<UndefValue>(RHS)) { |
| 11141 | std::vector<unsigned> LHSMask = getShuffleMask(LHSSVI); |
| 11142 | |
| 11143 | std::vector<unsigned> NewMask; |
| 11144 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) |
| 11145 | if (Mask[i] >= 2*e) |
| 11146 | NewMask.push_back(2*e); |
| 11147 | else |
| 11148 | NewMask.push_back(LHSMask[Mask[i]]); |
| 11149 | |
| 11150 | // If the result mask is equal to the src shuffle or this shuffle mask, do |
| 11151 | // the replacement. |
| 11152 | if (NewMask == LHSMask || NewMask == Mask) { |
| 11153 | std::vector<Constant*> Elts; |
| 11154 | for (unsigned i = 0, e = NewMask.size(); i != e; ++i) { |
| 11155 | if (NewMask[i] >= e*2) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 11156 | Elts.push_back(UndefValue::get(Type::Int32Ty)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 11157 | } else { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 11158 | Elts.push_back(ConstantInt::get(Type::Int32Ty, NewMask[i])); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 11159 | } |
| 11160 | } |
| 11161 | return new ShuffleVectorInst(LHSSVI->getOperand(0), |
| 11162 | LHSSVI->getOperand(1), |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 11163 | ConstantVector::get(Elts)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 11164 | } |
| 11165 | } |
| 11166 | } |
Chris Lattner | c5eff44 | 2007-01-30 22:32:46 +0000 | [diff] [blame] | 11167 | |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 11168 | return MadeChange ? &SVI : 0; |
| 11169 | } |
| 11170 | |
| 11171 | |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 11172 | |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 11173 | |
| 11174 | /// TryToSinkInstruction - Try to move the specified instruction from its |
| 11175 | /// current block into the beginning of DestBlock, which can only happen if it's |
| 11176 | /// safe to move the instruction past all of the instructions between it and the |
| 11177 | /// end of its block. |
| 11178 | static bool TryToSinkInstruction(Instruction *I, BasicBlock *DestBlock) { |
| 11179 | assert(I->hasOneUse() && "Invariants didn't hold!"); |
| 11180 | |
Chris Lattner | 108e902 | 2005-10-27 17:13:11 +0000 | [diff] [blame] | 11181 | // Cannot move control-flow-involving, volatile loads, vaarg, etc. |
Chris Lattner | bfc538c | 2008-05-09 15:07:33 +0000 | [diff] [blame] | 11182 | if (isa<PHINode>(I) || I->mayWriteToMemory() || isa<TerminatorInst>(I)) |
| 11183 | return false; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 11184 | |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 11185 | // Do not sink alloca instructions out of the entry block. |
Dan Gohman | ecb7a77 | 2007-03-22 16:38:57 +0000 | [diff] [blame] | 11186 | if (isa<AllocaInst>(I) && I->getParent() == |
| 11187 | &DestBlock->getParent()->getEntryBlock()) |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 11188 | return false; |
| 11189 | |
Chris Lattner | 96a52a6 | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 11190 | // We can only sink load instructions if there is nothing between the load and |
| 11191 | // the end of block that could change the value. |
Chris Lattner | 2539e33 | 2008-05-08 17:37:37 +0000 | [diff] [blame] | 11192 | if (I->mayReadFromMemory()) { |
| 11193 | for (BasicBlock::iterator Scan = I, E = I->getParent()->end(); |
Chris Lattner | 96a52a6 | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 11194 | Scan != E; ++Scan) |
| 11195 | if (Scan->mayWriteToMemory()) |
| 11196 | return false; |
Chris Lattner | 96a52a6 | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 11197 | } |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 11198 | |
Dan Gohman | 02dea8b | 2008-05-23 21:05:58 +0000 | [diff] [blame] | 11199 | BasicBlock::iterator InsertPos = DestBlock->getFirstNonPHI(); |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 11200 | |
Chris Lattner | 4bc5f80 | 2005-08-08 19:11:57 +0000 | [diff] [blame] | 11201 | I->moveBefore(InsertPos); |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 11202 | ++NumSunkInst; |
| 11203 | return true; |
| 11204 | } |
| 11205 | |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 11206 | |
| 11207 | /// AddReachableCodeToWorklist - Walk the function in depth-first order, adding |
| 11208 | /// all reachable code to the worklist. |
| 11209 | /// |
| 11210 | /// This has a couple of tricks to make the code faster and more powerful. In |
| 11211 | /// particular, we constant fold and DCE instructions as we go, to avoid adding |
| 11212 | /// them to the worklist (this significantly speeds up instcombine on code where |
| 11213 | /// many instructions are dead or constant). Additionally, if we find a branch |
| 11214 | /// whose condition is a known constant, we only visit the reachable successors. |
| 11215 | /// |
| 11216 | static void AddReachableCodeToWorklist(BasicBlock *BB, |
Chris Lattner | 1f87a58 | 2007-02-15 19:41:52 +0000 | [diff] [blame] | 11217 | SmallPtrSet<BasicBlock*, 64> &Visited, |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11218 | InstCombiner &IC, |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 11219 | const TargetData *TD) { |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 11220 | std::vector<BasicBlock*> Worklist; |
| 11221 | Worklist.push_back(BB); |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 11222 | |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 11223 | while (!Worklist.empty()) { |
| 11224 | BB = Worklist.back(); |
| 11225 | Worklist.pop_back(); |
| 11226 | |
| 11227 | // We have now visited this block! If we've already been here, ignore it. |
| 11228 | if (!Visited.insert(BB)) continue; |
| 11229 | |
| 11230 | for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ) { |
| 11231 | Instruction *Inst = BBI++; |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 11232 | |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 11233 | // DCE instruction if trivially dead. |
| 11234 | if (isInstructionTriviallyDead(Inst)) { |
| 11235 | ++NumDeadInst; |
| 11236 | DOUT << "IC: DCE: " << *Inst; |
| 11237 | Inst->eraseFromParent(); |
| 11238 | continue; |
| 11239 | } |
| 11240 | |
| 11241 | // ConstantProp instruction if trivially constant. |
| 11242 | if (Constant *C = ConstantFoldInstruction(Inst, TD)) { |
| 11243 | DOUT << "IC: ConstFold to: " << *C << " from: " << *Inst; |
| 11244 | Inst->replaceAllUsesWith(C); |
| 11245 | ++NumConstProp; |
| 11246 | Inst->eraseFromParent(); |
| 11247 | continue; |
| 11248 | } |
Chris Lattner | 3ccc6bc | 2007-07-20 22:06:41 +0000 | [diff] [blame] | 11249 | |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 11250 | IC.AddToWorkList(Inst); |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 11251 | } |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 11252 | |
| 11253 | // Recursively visit successors. If this is a branch or switch on a |
| 11254 | // constant, only visit the reachable successor. |
| 11255 | TerminatorInst *TI = BB->getTerminator(); |
| 11256 | if (BranchInst *BI = dyn_cast<BranchInst>(TI)) { |
| 11257 | if (BI->isConditional() && isa<ConstantInt>(BI->getCondition())) { |
| 11258 | bool CondVal = cast<ConstantInt>(BI->getCondition())->getZExtValue(); |
Nick Lewycky | 9143699 | 2008-03-09 08:50:23 +0000 | [diff] [blame] | 11259 | BasicBlock *ReachableBB = BI->getSuccessor(!CondVal); |
Nick Lewycky | 280a6e6 | 2008-04-25 16:53:59 +0000 | [diff] [blame] | 11260 | Worklist.push_back(ReachableBB); |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 11261 | continue; |
| 11262 | } |
| 11263 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) { |
| 11264 | if (ConstantInt *Cond = dyn_cast<ConstantInt>(SI->getCondition())) { |
| 11265 | // See if this is an explicit destination. |
| 11266 | for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i) |
| 11267 | if (SI->getCaseValue(i) == Cond) { |
Nick Lewycky | 9143699 | 2008-03-09 08:50:23 +0000 | [diff] [blame] | 11268 | BasicBlock *ReachableBB = SI->getSuccessor(i); |
Nick Lewycky | 280a6e6 | 2008-04-25 16:53:59 +0000 | [diff] [blame] | 11269 | Worklist.push_back(ReachableBB); |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 11270 | continue; |
| 11271 | } |
| 11272 | |
| 11273 | // Otherwise it is the default destination. |
| 11274 | Worklist.push_back(SI->getSuccessor(0)); |
| 11275 | continue; |
| 11276 | } |
| 11277 | } |
| 11278 | |
| 11279 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) |
| 11280 | Worklist.push_back(TI->getSuccessor(i)); |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 11281 | } |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 11282 | } |
| 11283 | |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 11284 | bool InstCombiner::DoOneIteration(Function &F, unsigned Iteration) { |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 11285 | bool Changed = false; |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 11286 | TD = &getAnalysis<TargetData>(); |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 11287 | |
| 11288 | DEBUG(DOUT << "\n\nINSTCOMBINE ITERATION #" << Iteration << " on " |
| 11289 | << F.getNameStr() << "\n"); |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 11290 | |
Chris Lattner | b3d5970 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 11291 | { |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 11292 | // Do a depth-first traversal of the function, populate the worklist with |
| 11293 | // the reachable instructions. Ignore blocks that are not reachable. Keep |
| 11294 | // track of which blocks we visit. |
Chris Lattner | 1f87a58 | 2007-02-15 19:41:52 +0000 | [diff] [blame] | 11295 | SmallPtrSet<BasicBlock*, 64> Visited; |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11296 | AddReachableCodeToWorklist(F.begin(), Visited, *this, TD); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 11297 | |
Chris Lattner | b3d5970 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 11298 | // Do a quick scan over the function. If we find any blocks that are |
| 11299 | // unreachable, remove any instructions inside of them. This prevents |
| 11300 | // the instcombine code from having to deal with some bad special cases. |
| 11301 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 11302 | if (!Visited.count(BB)) { |
| 11303 | Instruction *Term = BB->getTerminator(); |
| 11304 | while (Term != BB->begin()) { // Remove instrs bottom-up |
| 11305 | BasicBlock::iterator I = Term; --I; |
Chris Lattner | 6ffe551 | 2004-04-27 15:13:33 +0000 | [diff] [blame] | 11306 | |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 11307 | DOUT << "IC: DCE: " << *I; |
Chris Lattner | b3d5970 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 11308 | ++NumDeadInst; |
| 11309 | |
| 11310 | if (!I->use_empty()) |
| 11311 | I->replaceAllUsesWith(UndefValue::get(I->getType())); |
| 11312 | I->eraseFromParent(); |
| 11313 | } |
| 11314 | } |
| 11315 | } |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 11316 | |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11317 | while (!Worklist.empty()) { |
| 11318 | Instruction *I = RemoveOneFromWorkList(); |
| 11319 | if (I == 0) continue; // skip null values. |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 11320 | |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 11321 | // Check to see if we can DCE the instruction. |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 11322 | if (isInstructionTriviallyDead(I)) { |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 11323 | // Add operands to the worklist. |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11324 | if (I->getNumOperands() < 4) |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 11325 | AddUsesToWorkList(*I); |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 11326 | ++NumDeadInst; |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11327 | |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 11328 | DOUT << "IC: DCE: " << *I; |
Chris Lattner | ad5fec1 | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 11329 | |
| 11330 | I->eraseFromParent(); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11331 | RemoveFromWorkList(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11332 | continue; |
| 11333 | } |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 11334 | |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 11335 | // Instruction isn't dead, see if we can constant propagate it. |
Chris Lattner | 0a19ffa | 2007-01-30 23:16:15 +0000 | [diff] [blame] | 11336 | if (Constant *C = ConstantFoldInstruction(I, TD)) { |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 11337 | DOUT << "IC: ConstFold to: " << *C << " from: " << *I; |
Chris Lattner | ad5fec1 | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 11338 | |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 11339 | // Add operands to the worklist. |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 11340 | AddUsesToWorkList(*I); |
Chris Lattner | c736d56 | 2002-12-05 22:41:53 +0000 | [diff] [blame] | 11341 | ReplaceInstUsesWith(*I, C); |
| 11342 | |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 11343 | ++NumConstProp; |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 11344 | I->eraseFromParent(); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11345 | RemoveFromWorkList(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11346 | continue; |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 11347 | } |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11348 | |
Nick Lewycky | 3dfd7bf | 2008-05-25 20:56:15 +0000 | [diff] [blame] | 11349 | if (TD && I->getType()->getTypeID() == Type::VoidTyID) { |
| 11350 | // See if we can constant fold its operands. |
| 11351 | for (User::op_iterator i = I->op_begin(), e = I->op_end(); i != e; ++i) { |
| 11352 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(i)) { |
| 11353 | if (Constant *NewC = ConstantFoldConstantExpression(CE, TD)) |
| 11354 | i->set(NewC); |
| 11355 | } |
| 11356 | } |
| 11357 | } |
| 11358 | |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 11359 | // See if we can trivially sink this instruction to a successor basic block. |
Chris Lattner | 2539e33 | 2008-05-08 17:37:37 +0000 | [diff] [blame] | 11360 | // FIXME: Remove GetResultInst test when first class support for aggregates |
| 11361 | // is implemented. |
Devang Patel | f944c9a | 2008-05-03 00:36:30 +0000 | [diff] [blame] | 11362 | if (I->hasOneUse() && !isa<GetResultInst>(I)) { |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 11363 | BasicBlock *BB = I->getParent(); |
| 11364 | BasicBlock *UserParent = cast<Instruction>(I->use_back())->getParent(); |
| 11365 | if (UserParent != BB) { |
| 11366 | bool UserIsSuccessor = false; |
| 11367 | // See if the user is one of our successors. |
| 11368 | for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI) |
| 11369 | if (*SI == UserParent) { |
| 11370 | UserIsSuccessor = true; |
| 11371 | break; |
| 11372 | } |
| 11373 | |
| 11374 | // If the user is one of our immediate successors, and if that successor |
| 11375 | // only has us as a predecessors (we'd have to split the critical edge |
| 11376 | // otherwise), we can keep going. |
| 11377 | if (UserIsSuccessor && !isa<PHINode>(I->use_back()) && |
| 11378 | next(pred_begin(UserParent)) == pred_end(UserParent)) |
| 11379 | // Okay, the CFG is simple enough, try to sink this instruction. |
| 11380 | Changed |= TryToSinkInstruction(I, UserParent); |
| 11381 | } |
| 11382 | } |
| 11383 | |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 11384 | // Now that we have an instruction, try combining it to simplify it... |
Reid Spencer | a9b8101 | 2007-03-26 17:44:01 +0000 | [diff] [blame] | 11385 | #ifndef NDEBUG |
| 11386 | std::string OrigI; |
| 11387 | #endif |
| 11388 | DEBUG(std::ostringstream SS; I->print(SS); OrigI = SS.str();); |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 11389 | if (Instruction *Result = visit(*I)) { |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 11390 | ++NumCombined; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 11391 | // Should we replace the old instruction with a new one? |
Chris Lattner | b3bc8fa | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 11392 | if (Result != I) { |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 11393 | DOUT << "IC: Old = " << *I |
| 11394 | << " New = " << *Result; |
Chris Lattner | 0cea42a | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 11395 | |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 11396 | // Everything uses the new instruction now. |
| 11397 | I->replaceAllUsesWith(Result); |
| 11398 | |
| 11399 | // Push the new instruction and any users onto the worklist. |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11400 | AddToWorkList(Result); |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 11401 | AddUsersToWorkList(*Result); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11402 | |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 11403 | // Move the name to the new instruction first. |
| 11404 | Result->takeName(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11405 | |
| 11406 | // Insert the new instruction into the basic block... |
| 11407 | BasicBlock *InstParent = I->getParent(); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 11408 | BasicBlock::iterator InsertPos = I; |
| 11409 | |
| 11410 | if (!isa<PHINode>(Result)) // If combining a PHI, don't insert |
| 11411 | while (isa<PHINode>(InsertPos)) // middle of a block of PHIs. |
| 11412 | ++InsertPos; |
| 11413 | |
| 11414 | InstParent->getInstList().insert(InsertPos, Result); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11415 | |
Chris Lattner | 00d5131 | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 11416 | // Make sure that we reprocess all operands now that we reduced their |
| 11417 | // use counts. |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11418 | AddUsesToWorkList(*I); |
Chris Lattner | 216d4d8 | 2004-05-01 23:19:52 +0000 | [diff] [blame] | 11419 | |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 11420 | // Instructions can end up on the worklist more than once. Make sure |
| 11421 | // we do not process an instruction that has been deleted. |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11422 | RemoveFromWorkList(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11423 | |
| 11424 | // Erase the old instruction. |
| 11425 | InstParent->getInstList().erase(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 11426 | } else { |
Evan Cheng | c7baf68 | 2007-03-27 16:44:48 +0000 | [diff] [blame] | 11427 | #ifndef NDEBUG |
Reid Spencer | a9b8101 | 2007-03-26 17:44:01 +0000 | [diff] [blame] | 11428 | DOUT << "IC: Mod = " << OrigI |
| 11429 | << " New = " << *I; |
Evan Cheng | c7baf68 | 2007-03-27 16:44:48 +0000 | [diff] [blame] | 11430 | #endif |
Chris Lattner | 0cea42a | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 11431 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 11432 | // If the instruction was modified, it's possible that it is now dead. |
| 11433 | // if so, remove it. |
Chris Lattner | 00d5131 | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 11434 | if (isInstructionTriviallyDead(I)) { |
| 11435 | // Make sure we process all operands now that we are reducing their |
| 11436 | // use counts. |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 11437 | AddUsesToWorkList(*I); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 11438 | |
Chris Lattner | 00d5131 | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 11439 | // Instructions may end up in the worklist more than once. Erase all |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 11440 | // occurrences of this instruction. |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11441 | RemoveFromWorkList(I); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 11442 | I->eraseFromParent(); |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 11443 | } else { |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 11444 | AddToWorkList(I); |
| 11445 | AddUsersToWorkList(*I); |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 11446 | } |
Chris Lattner | b3bc8fa | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 11447 | } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 11448 | Changed = true; |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 11449 | } |
| 11450 | } |
| 11451 | |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 11452 | assert(WorklistMap.empty() && "Worklist empty, but map not?"); |
Chris Lattner | a9ff5eb | 2007-08-05 08:47:58 +0000 | [diff] [blame] | 11453 | |
| 11454 | // Do an explicit clear, this shrinks the map if needed. |
| 11455 | WorklistMap.clear(); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 11456 | return Changed; |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 11457 | } |
| 11458 | |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 11459 | |
| 11460 | bool InstCombiner::runOnFunction(Function &F) { |
Chris Lattner | f964f32 | 2007-03-04 04:27:24 +0000 | [diff] [blame] | 11461 | MustPreserveLCSSA = mustPreserveAnalysisID(LCSSAID); |
| 11462 | |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 11463 | bool EverMadeChange = false; |
| 11464 | |
| 11465 | // Iterate while there is work to do. |
| 11466 | unsigned Iteration = 0; |
Bill Wendling | a6c3112 | 2008-05-14 22:45:20 +0000 | [diff] [blame] | 11467 | while (DoOneIteration(F, Iteration++)) |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 11468 | EverMadeChange = true; |
| 11469 | return EverMadeChange; |
| 11470 | } |
| 11471 | |
Brian Gaeke | 96d4bf7 | 2004-07-27 17:43:21 +0000 | [diff] [blame] | 11472 | FunctionPass *llvm::createInstructionCombiningPass() { |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 11473 | return new InstCombiner(); |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 11474 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 11475 | |