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 | 2806dff | 2008-08-15 04:03:01 +0000 | [diff] [blame] | 77 | SmallVector<Instruction*, 256> Worklist; |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 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 |
Dan Gohman | ae73dc1 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 83 | InstCombiner() : FunctionPass(&ID) {} |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 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); |
Chris Lattner | fdb19e5 | 2008-07-14 00:15:52 +0000 | [diff] [blame] | 175 | bool SimplifyDivRemOfSelect(BinaryOperator &I); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 176 | Instruction *commonRemTransforms(BinaryOperator &I); |
| 177 | Instruction *commonIRemTransforms(BinaryOperator &I); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 178 | Instruction *commonDivTransforms(BinaryOperator &I); |
| 179 | Instruction *commonIDivTransforms(BinaryOperator &I); |
| 180 | Instruction *visitUDiv(BinaryOperator &I); |
| 181 | Instruction *visitSDiv(BinaryOperator &I); |
| 182 | Instruction *visitFDiv(BinaryOperator &I); |
Chris Lattner | 29cd5ba | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 183 | Instruction *FoldAndOfICmps(Instruction &I, ICmpInst *LHS, ICmpInst *RHS); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 184 | Instruction *visitAnd(BinaryOperator &I); |
Chris Lattner | 69d4ced | 2008-11-16 05:20:07 +0000 | [diff] [blame] | 185 | Instruction *FoldOrOfICmps(Instruction &I, ICmpInst *LHS, ICmpInst *RHS); |
Bill Wendling | d54d860 | 2008-12-01 08:32:40 +0000 | [diff] [blame] | 186 | Instruction *FoldOrWithConstants(BinaryOperator &I, Value *Op, |
Bill Wendling | a698a47 | 2008-12-01 08:23:25 +0000 | [diff] [blame] | 187 | Value *A, Value *B, Value *C); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 188 | Instruction *visitOr (BinaryOperator &I); |
| 189 | Instruction *visitXor(BinaryOperator &I); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 190 | Instruction *visitShl(BinaryOperator &I); |
| 191 | Instruction *visitAShr(BinaryOperator &I); |
| 192 | Instruction *visitLShr(BinaryOperator &I); |
| 193 | Instruction *commonShiftTransforms(BinaryOperator &I); |
Chris Lattner | a540623 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 194 | Instruction *FoldFCmp_IntToFP_Cst(FCmpInst &I, Instruction *LHSI, |
| 195 | Constant *RHSC); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 196 | Instruction *visitFCmpInst(FCmpInst &I); |
| 197 | Instruction *visitICmpInst(ICmpInst &I); |
| 198 | Instruction *visitICmpInstWithCastAndCast(ICmpInst &ICI); |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 199 | Instruction *visitICmpInstWithInstAndIntCst(ICmpInst &ICI, |
| 200 | Instruction *LHS, |
| 201 | ConstantInt *RHS); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 202 | Instruction *FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI, |
| 203 | ConstantInt *DivRHS); |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 204 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 205 | Instruction *FoldGEPICmp(User *GEPLHS, Value *RHS, |
| 206 | ICmpInst::Predicate Cond, Instruction &I); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 207 | Instruction *FoldShiftByConstant(Value *Op0, ConstantInt *Op1, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 208 | BinaryOperator &I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 209 | Instruction *commonCastTransforms(CastInst &CI); |
| 210 | Instruction *commonIntCastTransforms(CastInst &CI); |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 211 | Instruction *commonPointerCastTransforms(CastInst &CI); |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 212 | Instruction *visitTrunc(TruncInst &CI); |
| 213 | Instruction *visitZExt(ZExtInst &CI); |
| 214 | Instruction *visitSExt(SExtInst &CI); |
Chris Lattner | b753065 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 215 | Instruction *visitFPTrunc(FPTruncInst &CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 216 | Instruction *visitFPExt(CastInst &CI); |
Chris Lattner | 0c7a9a0 | 2008-05-19 20:25:04 +0000 | [diff] [blame] | 217 | Instruction *visitFPToUI(FPToUIInst &FI); |
| 218 | Instruction *visitFPToSI(FPToSIInst &FI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 219 | Instruction *visitUIToFP(CastInst &CI); |
| 220 | Instruction *visitSIToFP(CastInst &CI); |
Chris Lattner | a0e6969 | 2009-03-24 18:35:40 +0000 | [diff] [blame] | 221 | Instruction *visitPtrToInt(PtrToIntInst &CI); |
Chris Lattner | f9d9e45 | 2008-01-08 07:23:51 +0000 | [diff] [blame] | 222 | Instruction *visitIntToPtr(IntToPtrInst &CI); |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 223 | Instruction *visitBitCast(BitCastInst &CI); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 224 | Instruction *FoldSelectOpOp(SelectInst &SI, Instruction *TI, |
| 225 | Instruction *FI); |
Evan Cheng | de62192 | 2009-03-31 20:42:45 +0000 | [diff] [blame] | 226 | Instruction *FoldSelectIntoOp(SelectInst &SI, Value*, Value*); |
Dan Gohman | 81b28ce | 2008-09-16 18:46:06 +0000 | [diff] [blame] | 227 | Instruction *visitSelectInst(SelectInst &SI); |
| 228 | Instruction *visitSelectInstWithICmp(SelectInst &SI, ICmpInst *ICI); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 229 | Instruction *visitCallInst(CallInst &CI); |
| 230 | Instruction *visitInvokeInst(InvokeInst &II); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 231 | Instruction *visitPHINode(PHINode &PN); |
| 232 | Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP); |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 233 | Instruction *visitAllocationInst(AllocationInst &AI); |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 234 | Instruction *visitFreeInst(FreeInst &FI); |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 235 | Instruction *visitLoadInst(LoadInst &LI); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 236 | Instruction *visitStoreInst(StoreInst &SI); |
Chris Lattner | c4d10eb | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 237 | Instruction *visitBranchInst(BranchInst &BI); |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 238 | Instruction *visitSwitchInst(SwitchInst &SI); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 239 | Instruction *visitInsertElementInst(InsertElementInst &IE); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 240 | Instruction *visitExtractElementInst(ExtractElementInst &EI); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 241 | Instruction *visitShuffleVectorInst(ShuffleVectorInst &SVI); |
Matthijs Kooijman | a9012ec | 2008-06-11 14:05:05 +0000 | [diff] [blame] | 242 | Instruction *visitExtractValueInst(ExtractValueInst &EV); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 243 | |
| 244 | // visitInstruction - Specify what to return for unhandled instructions... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 245 | Instruction *visitInstruction(Instruction &I) { return 0; } |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 246 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 247 | private: |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 248 | Instruction *visitCallSite(CallSite CS); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 249 | bool transformConstExprCastCall(CallSite CS); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 250 | Instruction *transformCallThroughTrampoline(CallSite CS); |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 251 | Instruction *transformZExtICmp(ICmpInst *ICI, Instruction &CI, |
| 252 | bool DoXform = true); |
Chris Lattner | 3d28b1b | 2008-05-20 05:46:13 +0000 | [diff] [blame] | 253 | bool WillNotOverflowSignedAdd(Value *LHS, Value *RHS); |
Dale Johannesen | 4945c65 | 2009-03-03 21:26:39 +0000 | [diff] [blame] | 254 | DbgDeclareInst *hasOneUsePlusDeclare(Value *V); |
| 255 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 256 | |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 257 | public: |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 258 | // InsertNewInstBefore - insert an instruction New before instruction Old |
| 259 | // in the program. Add the new instruction to the worklist. |
| 260 | // |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 261 | Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) { |
Chris Lattner | e6f9a91 | 2002-08-23 18:32:43 +0000 | [diff] [blame] | 262 | assert(New && New->getParent() == 0 && |
| 263 | "New instruction already inserted into a basic block!"); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 264 | BasicBlock *BB = Old.getParent(); |
| 265 | BB->getInstList().insert(&Old, New); // Insert inst |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 266 | AddToWorkList(New); |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 267 | return New; |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 270 | /// InsertCastBefore - Insert a cast of V to TY before the instruction POS. |
| 271 | /// This also adds the cast to the worklist. Finally, this returns the |
| 272 | /// cast. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 273 | Value *InsertCastBefore(Instruction::CastOps opc, Value *V, const Type *Ty, |
| 274 | Instruction &Pos) { |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 275 | if (V->getType() == Ty) return V; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 276 | |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 277 | if (Constant *CV = dyn_cast<Constant>(V)) |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 278 | return ConstantExpr::getCast(opc, CV, Ty); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 279 | |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 280 | Instruction *C = CastInst::Create(opc, V, Ty, V->getName(), &Pos); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 281 | AddToWorkList(C); |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 282 | return C; |
| 283 | } |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 284 | |
| 285 | Value *InsertBitCastBefore(Value *V, const Type *Ty, Instruction &Pos) { |
| 286 | return InsertCastBefore(Instruction::BitCast, V, Ty, Pos); |
| 287 | } |
| 288 | |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 289 | |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 290 | // ReplaceInstUsesWith - This method is to be used when an instruction is |
| 291 | // found to be dead, replacable with another preexisting expression. Here |
| 292 | // we add all uses of I to the worklist, replace all uses of I with the new |
| 293 | // value, then return I, so that the inst combiner will know that I was |
| 294 | // modified. |
| 295 | // |
| 296 | Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) { |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 297 | AddUsersToWorkList(I); // Add all modified instrs to worklist |
Chris Lattner | 15a76c0 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 298 | if (&I != V) { |
| 299 | I.replaceAllUsesWith(V); |
| 300 | return &I; |
| 301 | } else { |
| 302 | // If we are replacing the instruction with itself, this must be in a |
| 303 | // segment of unreachable code, so just clobber the instruction. |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 304 | I.replaceAllUsesWith(UndefValue::get(I.getType())); |
Chris Lattner | 15a76c0 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 305 | return &I; |
| 306 | } |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 307 | } |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 308 | |
| 309 | // EraseInstFromFunction - When dealing with an instruction that has side |
| 310 | // effects or produces a void value, we can't rely on DCE to delete the |
| 311 | // instruction. Instead, visit methods should return the value returned by |
| 312 | // this function. |
| 313 | Instruction *EraseInstFromFunction(Instruction &I) { |
| 314 | assert(I.use_empty() && "Cannot erase instruction that is used!"); |
| 315 | AddUsesToWorkList(I); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 316 | RemoveFromWorkList(&I); |
Chris Lattner | 954f66a | 2004-11-18 21:41:39 +0000 | [diff] [blame] | 317 | I.eraseFromParent(); |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 318 | return 0; // Don't do anything with FI |
| 319 | } |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 320 | |
| 321 | void ComputeMaskedBits(Value *V, const APInt &Mask, APInt &KnownZero, |
| 322 | APInt &KnownOne, unsigned Depth = 0) const { |
| 323 | return llvm::ComputeMaskedBits(V, Mask, KnownZero, KnownOne, TD, Depth); |
| 324 | } |
| 325 | |
| 326 | bool MaskedValueIsZero(Value *V, const APInt &Mask, |
| 327 | unsigned Depth = 0) const { |
| 328 | return llvm::MaskedValueIsZero(V, Mask, TD, Depth); |
| 329 | } |
| 330 | unsigned ComputeNumSignBits(Value *Op, unsigned Depth = 0) const { |
| 331 | return llvm::ComputeNumSignBits(Op, TD, Depth); |
| 332 | } |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 333 | |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 334 | private: |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 335 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 336 | /// SimplifyCommutative - This performs a few simplifications for |
| 337 | /// commutative operators. |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 338 | bool SimplifyCommutative(BinaryOperator &I); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 339 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 340 | /// SimplifyCompare - This reorders the operands of a CmpInst to get them in |
| 341 | /// most-complex to least-complex order. |
| 342 | bool SimplifyCompare(CmpInst &I); |
| 343 | |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 344 | /// SimplifyDemandedUseBits - Attempts to replace V with a simpler value |
| 345 | /// based on the demanded bits. |
| 346 | Value *SimplifyDemandedUseBits(Value *V, APInt DemandedMask, |
| 347 | APInt& KnownZero, APInt& KnownOne, |
| 348 | unsigned Depth); |
| 349 | bool SimplifyDemandedBits(Use &U, APInt DemandedMask, |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 350 | APInt& KnownZero, APInt& KnownOne, |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 351 | unsigned Depth=0); |
| 352 | |
| 353 | /// SimplifyDemandedInstructionBits - Inst is an integer instruction that |
| 354 | /// SimplifyDemandedBits knows about. See if the instruction has any |
| 355 | /// properties that allow us to simplify its operands. |
| 356 | bool SimplifyDemandedInstructionBits(Instruction &Inst); |
| 357 | |
Evan Cheng | 388df62 | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 358 | Value *SimplifyDemandedVectorElts(Value *V, APInt DemandedElts, |
| 359 | APInt& UndefElts, unsigned Depth = 0); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 360 | |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 361 | // FoldOpIntoPhi - Given a binary operator or cast instruction which has a |
| 362 | // PHI node as operand #0, see if we can fold the instruction into the PHI |
| 363 | // (which is only possible if all operands to the PHI are constants). |
| 364 | Instruction *FoldOpIntoPhi(Instruction &I); |
| 365 | |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 366 | // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary" |
| 367 | // operator and they all are only used by the PHI, PHI together their |
| 368 | // inputs, and do the operation once, to the result of the PHI. |
| 369 | Instruction *FoldPHIArgOpIntoPHI(PHINode &PN); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 370 | Instruction *FoldPHIArgBinOpIntoPHI(PHINode &PN); |
Chris Lattner | 05f1892 | 2008-12-01 02:34:36 +0000 | [diff] [blame] | 371 | Instruction *FoldPHIArgGEPIntoPHI(PHINode &PN); |
| 372 | |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 373 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 374 | Instruction *OptAndOp(Instruction *Op, ConstantInt *OpRHS, |
| 375 | ConstantInt *AndRHS, BinaryOperator &TheAnd); |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 376 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 377 | Value *FoldLogicalPlusAnd(Value *LHS, Value *RHS, ConstantInt *Mask, |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 378 | bool isSub, Instruction &I); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 379 | Instruction *InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 380 | bool isSigned, bool Inside, Instruction &IB); |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 381 | Instruction *PromoteCastOfAllocation(BitCastInst &CI, AllocationInst &AI); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 382 | Instruction *MatchBSwap(BinaryOperator &I); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 383 | bool SimplifyStoreAtEndOfBlock(StoreInst &SI); |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 384 | Instruction *SimplifyMemTransfer(MemIntrinsic *MI); |
Chris Lattner | 69ea9d2 | 2008-04-30 06:39:11 +0000 | [diff] [blame] | 385 | Instruction *SimplifyMemSet(MemSetInst *MI); |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 386 | |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 387 | |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 388 | Value *EvaluateInDifferentType(Value *V, const Type *Ty, bool isSigned); |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 389 | |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 390 | bool CanEvaluateInDifferentType(Value *V, const IntegerType *Ty, |
Evan Cheng | 4e56ab2 | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 391 | unsigned CastOpc, int &NumCastsRemoved); |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 392 | unsigned GetOrEnforceKnownAlignment(Value *V, |
| 393 | unsigned PrefAlign = 0); |
Matthijs Kooijman | a9012ec | 2008-06-11 14:05:05 +0000 | [diff] [blame] | 394 | |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 395 | }; |
| 396 | } |
| 397 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 398 | char InstCombiner::ID = 0; |
| 399 | static RegisterPass<InstCombiner> |
| 400 | X("instcombine", "Combine redundant instructions"); |
| 401 | |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 402 | // getComplexity: Assign a complexity or rank value to LLVM Values... |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 403 | // 0 -> undef, 1 -> Const, 2 -> Other, 3 -> Arg, 3 -> Unary, 4 -> OtherInst |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 404 | static unsigned getComplexity(Value *V) { |
| 405 | if (isa<Instruction>(V)) { |
| 406 | if (BinaryOperator::isNeg(V) || BinaryOperator::isNot(V)) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 407 | return 3; |
| 408 | return 4; |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 409 | } |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 410 | if (isa<Argument>(V)) return 3; |
| 411 | return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2; |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 412 | } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 413 | |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 414 | // isOnlyUse - Return true if this instruction will be deleted if we stop using |
| 415 | // it. |
| 416 | static bool isOnlyUse(Value *V) { |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 417 | return V->hasOneUse() || isa<Constant>(V); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 420 | // getPromotedType - Return the specified type promoted as it would be to pass |
| 421 | // though a va_arg area... |
| 422 | static const Type *getPromotedType(const Type *Ty) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 423 | if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty)) { |
| 424 | if (ITy->getBitWidth() < 32) |
| 425 | return Type::Int32Ty; |
Chris Lattner | 2b7e0ad | 2007-05-23 01:17:04 +0000 | [diff] [blame] | 426 | } |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 427 | return Ty; |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Matthijs Kooijman | 7e6d9b9 | 2008-10-13 15:17:01 +0000 | [diff] [blame] | 430 | /// getBitCastOperand - If the specified operand is a CastInst, a constant |
| 431 | /// expression bitcast, or a GetElementPtrInst with all zero indices, return the |
| 432 | /// operand value, otherwise return null. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 433 | static Value *getBitCastOperand(Value *V) { |
| 434 | if (BitCastInst *I = dyn_cast<BitCastInst>(V)) |
Matthijs Kooijman | 7e6d9b9 | 2008-10-13 15:17:01 +0000 | [diff] [blame] | 435 | // BitCastInst? |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 436 | return I->getOperand(0); |
Matthijs Kooijman | 7e6d9b9 | 2008-10-13 15:17:01 +0000 | [diff] [blame] | 437 | else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(V)) { |
| 438 | // GetElementPtrInst? |
| 439 | if (GEP->hasAllZeroIndices()) |
| 440 | return GEP->getOperand(0); |
| 441 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 442 | if (CE->getOpcode() == Instruction::BitCast) |
Matthijs Kooijman | 7e6d9b9 | 2008-10-13 15:17:01 +0000 | [diff] [blame] | 443 | // BitCast ConstantExp? |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 444 | return CE->getOperand(0); |
Matthijs Kooijman | 7e6d9b9 | 2008-10-13 15:17:01 +0000 | [diff] [blame] | 445 | else if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 446 | // GetElementPtr ConstantExp? |
| 447 | for (User::op_iterator I = CE->op_begin() + 1, E = CE->op_end(); |
| 448 | I != E; ++I) { |
| 449 | ConstantInt *CI = dyn_cast<ConstantInt>(I); |
| 450 | if (!CI || !CI->isZero()) |
| 451 | // Any non-zero indices? Not cast-like. |
| 452 | return 0; |
| 453 | } |
| 454 | // All-zero indices? This is just like casting. |
| 455 | return CE->getOperand(0); |
| 456 | } |
| 457 | } |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 458 | return 0; |
| 459 | } |
| 460 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 461 | /// This function is a wrapper around CastInst::isEliminableCastPair. It |
| 462 | /// simply extracts arguments and returns what that function returns. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 463 | static Instruction::CastOps |
| 464 | isEliminableCastPair( |
| 465 | const CastInst *CI, ///< The first cast instruction |
| 466 | unsigned opcode, ///< The opcode of the second cast instruction |
| 467 | const Type *DstTy, ///< The target type for the second cast instruction |
| 468 | TargetData *TD ///< The target data for pointer size |
| 469 | ) { |
| 470 | |
| 471 | const Type *SrcTy = CI->getOperand(0)->getType(); // A from above |
| 472 | const Type *MidTy = CI->getType(); // B from above |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 473 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 474 | // Get the opcodes of the two Cast instructions |
| 475 | Instruction::CastOps firstOp = Instruction::CastOps(CI->getOpcode()); |
| 476 | Instruction::CastOps secondOp = Instruction::CastOps(opcode); |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 477 | |
Chris Lattner | a0e6969 | 2009-03-24 18:35:40 +0000 | [diff] [blame] | 478 | unsigned Res = CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, |
| 479 | DstTy, TD->getIntPtrType()); |
| 480 | |
| 481 | // We don't want to form an inttoptr or ptrtoint that converts to an integer |
| 482 | // type that differs from the pointer size. |
| 483 | if ((Res == Instruction::IntToPtr && SrcTy != TD->getIntPtrType()) || |
| 484 | (Res == Instruction::PtrToInt && DstTy != TD->getIntPtrType())) |
| 485 | Res = 0; |
| 486 | |
| 487 | return Instruction::CastOps(Res); |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | /// ValueRequiresCast - Return true if the cast from "V to Ty" actually results |
| 491 | /// in any code being generated. It does not require codegen if V is simple |
| 492 | /// enough or if the cast can be folded into other casts. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 493 | static bool ValueRequiresCast(Instruction::CastOps opcode, const Value *V, |
| 494 | const Type *Ty, TargetData *TD) { |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 495 | if (V->getType() == Ty || isa<Constant>(V)) return false; |
| 496 | |
Chris Lattner | 01575b7 | 2006-05-25 23:24:33 +0000 | [diff] [blame] | 497 | // 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] | 498 | if (const CastInst *CI = dyn_cast<CastInst>(V)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 499 | if (isEliminableCastPair(CI, opcode, Ty, TD)) |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 500 | return false; |
| 501 | return true; |
| 502 | } |
| 503 | |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 504 | // SimplifyCommutative - This performs a few simplifications for commutative |
| 505 | // operators: |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 506 | // |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 507 | // 1. Order operands such that they are listed from right (least complex) to |
| 508 | // left (most complex). This puts constants before unary operators before |
| 509 | // binary operators. |
| 510 | // |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 511 | // 2. Transform: (op (op V, C1), C2) ==> (op V, (op C1, C2)) |
| 512 | // 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] | 513 | // |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 514 | bool InstCombiner::SimplifyCommutative(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 515 | bool Changed = false; |
| 516 | if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1))) |
| 517 | Changed = !I.swapOperands(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 518 | |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 519 | if (!I.isAssociative()) return Changed; |
| 520 | Instruction::BinaryOps Opcode = I.getOpcode(); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 521 | if (BinaryOperator *Op = dyn_cast<BinaryOperator>(I.getOperand(0))) |
| 522 | if (Op->getOpcode() == Opcode && isa<Constant>(Op->getOperand(1))) { |
| 523 | if (isa<Constant>(I.getOperand(1))) { |
Chris Lattner | 2a9c847 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 524 | Constant *Folded = ConstantExpr::get(I.getOpcode(), |
| 525 | cast<Constant>(I.getOperand(1)), |
| 526 | cast<Constant>(Op->getOperand(1))); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 527 | I.setOperand(0, Op->getOperand(0)); |
| 528 | I.setOperand(1, Folded); |
| 529 | return true; |
| 530 | } else if (BinaryOperator *Op1=dyn_cast<BinaryOperator>(I.getOperand(1))) |
| 531 | if (Op1->getOpcode() == Opcode && isa<Constant>(Op1->getOperand(1)) && |
| 532 | isOnlyUse(Op) && isOnlyUse(Op1)) { |
| 533 | Constant *C1 = cast<Constant>(Op->getOperand(1)); |
| 534 | Constant *C2 = cast<Constant>(Op1->getOperand(1)); |
| 535 | |
| 536 | // 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] | 537 | Constant *Folded = ConstantExpr::get(I.getOpcode(), C1, C2); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 538 | Instruction *New = BinaryOperator::Create(Opcode, Op->getOperand(0), |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 539 | Op1->getOperand(0), |
| 540 | Op1->getName(), &I); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 541 | AddToWorkList(New); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 542 | I.setOperand(0, New); |
| 543 | I.setOperand(1, Folded); |
| 544 | return true; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 545 | } |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 546 | } |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 547 | return Changed; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 548 | } |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 549 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 550 | /// SimplifyCompare - For a CmpInst this function just orders the operands |
| 551 | /// so that theyare listed from right (least complex) to left (most complex). |
| 552 | /// This puts constants before unary operators before binary operators. |
| 553 | bool InstCombiner::SimplifyCompare(CmpInst &I) { |
| 554 | if (getComplexity(I.getOperand(0)) >= getComplexity(I.getOperand(1))) |
| 555 | return false; |
| 556 | I.swapOperands(); |
| 557 | // Compare instructions are not associative so there's nothing else we can do. |
| 558 | return true; |
| 559 | } |
| 560 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 561 | // dyn_castNegVal - Given a 'sub' instruction, return the RHS of the instruction |
| 562 | // if the LHS is a constant zero (which is the 'negate' form). |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 563 | // |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 564 | static inline Value *dyn_castNegVal(Value *V) { |
| 565 | if (BinaryOperator::isNeg(V)) |
Chris Lattner | a1df33c | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 566 | return BinaryOperator::getNegArgument(V); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 567 | |
Chris Lattner | 0ce8580 | 2004-12-14 20:08:06 +0000 | [diff] [blame] | 568 | // Constants can be considered to be negated values if they can be folded. |
| 569 | if (ConstantInt *C = dyn_cast<ConstantInt>(V)) |
| 570 | return ConstantExpr::getNeg(C); |
Nick Lewycky | 18b3da6 | 2008-05-23 04:54:45 +0000 | [diff] [blame] | 571 | |
| 572 | if (ConstantVector *C = dyn_cast<ConstantVector>(V)) |
| 573 | if (C->getType()->getElementType()->isInteger()) |
| 574 | return ConstantExpr::getNeg(C); |
| 575 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 576 | return 0; |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 579 | static inline Value *dyn_castNotVal(Value *V) { |
| 580 | if (BinaryOperator::isNot(V)) |
Chris Lattner | a1df33c | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 581 | return BinaryOperator::getNotArgument(V); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 582 | |
| 583 | // Constants can be considered to be not'ed values... |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 584 | if (ConstantInt *C = dyn_cast<ConstantInt>(V)) |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 585 | return ConstantInt::get(~C->getValue()); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 586 | return 0; |
| 587 | } |
| 588 | |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 589 | // dyn_castFoldableMul - If this value is a multiply that can be folded into |
| 590 | // other computations (because it has a constant operand), return the |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 591 | // non-constant operand of the multiply, and set CST to point to the multiplier. |
| 592 | // Otherwise, return null. |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 593 | // |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 594 | static inline Value *dyn_castFoldableMul(Value *V, ConstantInt *&CST) { |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 595 | if (V->hasOneUse() && V->getType()->isInteger()) |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 596 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 597 | if (I->getOpcode() == Instruction::Mul) |
Chris Lattner | 50e60c7 | 2004-11-15 05:54:07 +0000 | [diff] [blame] | 598 | if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 599 | return I->getOperand(0); |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 600 | if (I->getOpcode() == Instruction::Shl) |
Chris Lattner | 50e60c7 | 2004-11-15 05:54:07 +0000 | [diff] [blame] | 601 | if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) { |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 602 | // The multiplier is really 1 << CST. |
Zhou Sheng | 97b52c2 | 2007-03-29 01:57:21 +0000 | [diff] [blame] | 603 | uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth(); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 604 | uint32_t CSTVal = CST->getLimitedValue(BitWidth); |
Zhou Sheng | 97b52c2 | 2007-03-29 01:57:21 +0000 | [diff] [blame] | 605 | CST = ConstantInt::get(APInt(BitWidth, 1).shl(CSTVal)); |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 606 | return I->getOperand(0); |
| 607 | } |
| 608 | } |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 609 | return 0; |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 610 | } |
Chris Lattner | af2930e | 2002-08-14 17:51:49 +0000 | [diff] [blame] | 611 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 612 | /// dyn_castGetElementPtr - If this is a getelementptr instruction or constant |
| 613 | /// expression, return it. |
| 614 | static User *dyn_castGetElementPtr(Value *V) { |
| 615 | if (isa<GetElementPtrInst>(V)) return cast<User>(V); |
| 616 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
| 617 | if (CE->getOpcode() == Instruction::GetElementPtr) |
| 618 | return cast<User>(V); |
| 619 | return false; |
| 620 | } |
| 621 | |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 622 | /// getOpcode - If this is an Instruction or a ConstantExpr, return the |
| 623 | /// opcode value. Otherwise return UserOp1. |
Dan Gohman | b99e2e2 | 2008-05-29 19:53:46 +0000 | [diff] [blame] | 624 | static unsigned getOpcode(const Value *V) { |
| 625 | if (const Instruction *I = dyn_cast<Instruction>(V)) |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 626 | return I->getOpcode(); |
Dan Gohman | b99e2e2 | 2008-05-29 19:53:46 +0000 | [diff] [blame] | 627 | if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 628 | return CE->getOpcode(); |
| 629 | // Use UserOp1 to mean there's no opcode. |
| 630 | return Instruction::UserOp1; |
| 631 | } |
| 632 | |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 633 | /// AddOne - Add one to a ConstantInt |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 634 | static ConstantInt *AddOne(ConstantInt *C) { |
Reid Spencer | 2149a9d | 2007-03-25 19:55:33 +0000 | [diff] [blame] | 635 | APInt Val(C->getValue()); |
| 636 | return ConstantInt::get(++Val); |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 637 | } |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 638 | /// SubOne - Subtract one from a ConstantInt |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 639 | static ConstantInt *SubOne(ConstantInt *C) { |
Reid Spencer | 2149a9d | 2007-03-25 19:55:33 +0000 | [diff] [blame] | 640 | APInt Val(C->getValue()); |
| 641 | return ConstantInt::get(--Val); |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 642 | } |
| 643 | /// Add - Add two ConstantInts together |
| 644 | static ConstantInt *Add(ConstantInt *C1, ConstantInt *C2) { |
| 645 | return ConstantInt::get(C1->getValue() + C2->getValue()); |
| 646 | } |
| 647 | /// And - Bitwise AND two ConstantInts together |
| 648 | static ConstantInt *And(ConstantInt *C1, ConstantInt *C2) { |
| 649 | return ConstantInt::get(C1->getValue() & C2->getValue()); |
| 650 | } |
| 651 | /// Subtract - Subtract one ConstantInt from another |
| 652 | static ConstantInt *Subtract(ConstantInt *C1, ConstantInt *C2) { |
| 653 | return ConstantInt::get(C1->getValue() - C2->getValue()); |
| 654 | } |
| 655 | /// Multiply - Multiply two ConstantInts together |
| 656 | static ConstantInt *Multiply(ConstantInt *C1, ConstantInt *C2) { |
| 657 | return ConstantInt::get(C1->getValue() * C2->getValue()); |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 658 | } |
Nick Lewycky | e0cfecf | 2008-02-18 22:48:05 +0000 | [diff] [blame] | 659 | /// MultiplyOverflows - True if the multiply can not be expressed in an int |
| 660 | /// this size. |
| 661 | static bool MultiplyOverflows(ConstantInt *C1, ConstantInt *C2, bool sign) { |
| 662 | uint32_t W = C1->getBitWidth(); |
| 663 | APInt LHSExt = C1->getValue(), RHSExt = C2->getValue(); |
| 664 | if (sign) { |
| 665 | LHSExt.sext(W * 2); |
| 666 | RHSExt.sext(W * 2); |
| 667 | } else { |
| 668 | LHSExt.zext(W * 2); |
| 669 | RHSExt.zext(W * 2); |
| 670 | } |
| 671 | |
| 672 | APInt MulExt = LHSExt * RHSExt; |
| 673 | |
| 674 | if (sign) { |
| 675 | APInt Min = APInt::getSignedMinValue(W).sext(W * 2); |
| 676 | APInt Max = APInt::getSignedMaxValue(W).sext(W * 2); |
| 677 | return MulExt.slt(Min) || MulExt.sgt(Max); |
| 678 | } else |
| 679 | return MulExt.ugt(APInt::getLowBitsSet(W * 2, W)); |
| 680 | } |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 681 | |
Reid Spencer | e7816b5 | 2007-03-08 01:52:58 +0000 | [diff] [blame] | 682 | |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 683 | /// ShrinkDemandedConstant - Check to see if the specified operand of the |
| 684 | /// specified instruction is a constant integer. If so, check to see if there |
| 685 | /// are any bits set in the constant that are not demanded. If so, shrink the |
| 686 | /// constant and return true. |
| 687 | static bool ShrinkDemandedConstant(Instruction *I, unsigned OpNo, |
Reid Spencer | 6b79e2d | 2007-03-12 17:15:10 +0000 | [diff] [blame] | 688 | APInt Demanded) { |
| 689 | assert(I && "No instruction?"); |
| 690 | assert(OpNo < I->getNumOperands() && "Operand index too large"); |
| 691 | |
| 692 | // If the operand is not a constant integer, nothing to do. |
| 693 | ConstantInt *OpC = dyn_cast<ConstantInt>(I->getOperand(OpNo)); |
| 694 | if (!OpC) return false; |
| 695 | |
| 696 | // If there are no bits set that aren't demanded, nothing to do. |
| 697 | Demanded.zextOrTrunc(OpC->getValue().getBitWidth()); |
| 698 | if ((~Demanded & OpC->getValue()) == 0) |
| 699 | return false; |
| 700 | |
| 701 | // This instruction is producing bits that are not demanded. Shrink the RHS. |
| 702 | Demanded &= OpC->getValue(); |
| 703 | I->setOperand(OpNo, ConstantInt::get(Demanded)); |
| 704 | return true; |
| 705 | } |
| 706 | |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 707 | // ComputeSignedMinMaxValuesFromKnownBits - Given a signed integer type and a |
| 708 | // set of known zero and one bits, compute the maximum and minimum values that |
| 709 | // could have the specified known zero and known one bits, returning them in |
| 710 | // min/max. |
| 711 | static void ComputeSignedMinMaxValuesFromKnownBits(const Type *Ty, |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 712 | const APInt& KnownZero, |
| 713 | const APInt& KnownOne, |
| 714 | APInt& Min, APInt& Max) { |
| 715 | uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth(); |
| 716 | assert(KnownZero.getBitWidth() == BitWidth && |
| 717 | KnownOne.getBitWidth() == BitWidth && |
| 718 | Min.getBitWidth() == BitWidth && Max.getBitWidth() == BitWidth && |
| 719 | "Ty, KnownZero, KnownOne and Min, Max must have equal bitwidth."); |
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 720 | APInt UnknownBits = ~(KnownZero|KnownOne); |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 721 | |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 722 | // The minimum value is when all unknown bits are zeros, EXCEPT for the sign |
| 723 | // bit if it is unknown. |
| 724 | Min = KnownOne; |
| 725 | Max = KnownOne|UnknownBits; |
| 726 | |
Zhou Sheng | 4acf155 | 2007-03-28 05:15:57 +0000 | [diff] [blame] | 727 | if (UnknownBits[BitWidth-1]) { // Sign bit is unknown |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 728 | Min.set(BitWidth-1); |
| 729 | Max.clear(BitWidth-1); |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 730 | } |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | // ComputeUnsignedMinMaxValuesFromKnownBits - Given an unsigned integer type and |
| 734 | // a set of known zero and one bits, compute the maximum and minimum values that |
| 735 | // could have the specified known zero and known one bits, returning them in |
| 736 | // min/max. |
| 737 | static void ComputeUnsignedMinMaxValuesFromKnownBits(const Type *Ty, |
Chris Lattner | a9ff5eb | 2007-08-05 08:47:58 +0000 | [diff] [blame] | 738 | const APInt &KnownZero, |
| 739 | const APInt &KnownOne, |
| 740 | APInt &Min, APInt &Max) { |
| 741 | uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth(); BitWidth = BitWidth; |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 742 | assert(KnownZero.getBitWidth() == BitWidth && |
| 743 | KnownOne.getBitWidth() == BitWidth && |
| 744 | Min.getBitWidth() == BitWidth && Max.getBitWidth() && |
| 745 | "Ty, KnownZero, KnownOne and Min, Max must have equal bitwidth."); |
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 746 | APInt UnknownBits = ~(KnownZero|KnownOne); |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 747 | |
| 748 | // The minimum value is when the unknown bits are all zeros. |
| 749 | Min = KnownOne; |
| 750 | // The maximum value is when the unknown bits are all ones. |
| 751 | Max = KnownOne|UnknownBits; |
| 752 | } |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 753 | |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 754 | /// SimplifyDemandedInstructionBits - Inst is an integer instruction that |
| 755 | /// SimplifyDemandedBits knows about. See if the instruction has any |
| 756 | /// properties that allow us to simplify its operands. |
| 757 | bool InstCombiner::SimplifyDemandedInstructionBits(Instruction &Inst) { |
| 758 | unsigned BitWidth = cast<IntegerType>(Inst.getType())->getBitWidth(); |
| 759 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 760 | APInt DemandedMask(APInt::getAllOnesValue(BitWidth)); |
| 761 | |
| 762 | Value *V = SimplifyDemandedUseBits(&Inst, DemandedMask, |
| 763 | KnownZero, KnownOne, 0); |
| 764 | if (V == 0) return false; |
| 765 | if (V == &Inst) return true; |
| 766 | ReplaceInstUsesWith(Inst, V); |
| 767 | return true; |
| 768 | } |
| 769 | |
| 770 | /// SimplifyDemandedBits - This form of SimplifyDemandedBits simplifies the |
| 771 | /// specified instruction operand if possible, updating it in place. It returns |
| 772 | /// true if it made any change and false otherwise. |
| 773 | bool InstCombiner::SimplifyDemandedBits(Use &U, APInt DemandedMask, |
| 774 | APInt &KnownZero, APInt &KnownOne, |
| 775 | unsigned Depth) { |
| 776 | Value *NewVal = SimplifyDemandedUseBits(U.get(), DemandedMask, |
| 777 | KnownZero, KnownOne, Depth); |
| 778 | if (NewVal == 0) return false; |
| 779 | U.set(NewVal); |
| 780 | return true; |
| 781 | } |
| 782 | |
| 783 | |
| 784 | /// SimplifyDemandedUseBits - This function attempts to replace V with a simpler |
| 785 | /// value based on the demanded bits. When this function is called, it is known |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 786 | /// that only the bits set in DemandedMask of the result of V are ever used |
| 787 | /// downstream. Consequently, depending on the mask and V, it may be possible |
| 788 | /// to replace V with a constant or one of its operands. In such cases, this |
| 789 | /// function does the replacement and returns true. In all other cases, it |
| 790 | /// returns false after analyzing the expression and setting KnownOne and known |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 791 | /// to be one in the expression. KnownZero contains all the bits that are known |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 792 | /// to be zero in the expression. These are provided to potentially allow the |
| 793 | /// caller (which might recursively be SimplifyDemandedBits itself) to simplify |
| 794 | /// the expression. KnownOne and KnownZero always follow the invariant that |
| 795 | /// KnownOne & KnownZero == 0. That is, a bit can't be both 1 and 0. Note that |
| 796 | /// the bits in KnownOne and KnownZero may only be accurate for those bits set |
| 797 | /// in DemandedMask. Note also that the bitwidth of V, DemandedMask, KnownZero |
| 798 | /// and KnownOne must all be the same. |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 799 | /// |
| 800 | /// This returns null if it did not change anything and it permits no |
| 801 | /// simplification. This returns V itself if it did some simplification of V's |
| 802 | /// operands based on the information about what bits are demanded. This returns |
| 803 | /// some other non-null value if it found out that V is equal to another value |
| 804 | /// in the context where the specified bits are demanded, but not for all users. |
| 805 | Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask, |
| 806 | APInt &KnownZero, APInt &KnownOne, |
| 807 | unsigned Depth) { |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 808 | assert(V != 0 && "Null pointer of Value???"); |
| 809 | assert(Depth <= 6 && "Limit Search Depth"); |
| 810 | uint32_t BitWidth = DemandedMask.getBitWidth(); |
| 811 | const IntegerType *VTy = cast<IntegerType>(V->getType()); |
| 812 | assert(VTy->getBitWidth() == BitWidth && |
| 813 | KnownZero.getBitWidth() == BitWidth && |
| 814 | KnownOne.getBitWidth() == BitWidth && |
| 815 | "Value *V, DemandedMask, KnownZero and KnownOne \ |
| 816 | must have same BitWidth"); |
| 817 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
| 818 | // We know all of the bits for a constant! |
| 819 | KnownOne = CI->getValue() & DemandedMask; |
| 820 | KnownZero = ~KnownOne & DemandedMask; |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 821 | return 0; |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 822 | } |
| 823 | |
Chris Lattner | 08d2cc7 | 2009-01-31 07:26:06 +0000 | [diff] [blame] | 824 | KnownZero.clear(); |
Zhou Sheng | 9670445 | 2007-03-14 03:21:24 +0000 | [diff] [blame] | 825 | KnownOne.clear(); |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 826 | if (DemandedMask == 0) { // Not demanding any bits from V. |
| 827 | if (isa<UndefValue>(V)) |
| 828 | return 0; |
| 829 | return UndefValue::get(VTy); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 830 | } |
| 831 | |
Chris Lattner | 4598c94 | 2009-01-31 08:24:16 +0000 | [diff] [blame] | 832 | if (Depth == 6) // Limit search depth. |
| 833 | return 0; |
| 834 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 835 | Instruction *I = dyn_cast<Instruction>(V); |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 836 | if (!I) return 0; // Only analyze instructions. |
Chris Lattner | 4598c94 | 2009-01-31 08:24:16 +0000 | [diff] [blame] | 837 | |
Chris Lattner | d1b5e3f | 2009-01-31 08:40:03 +0000 | [diff] [blame] | 838 | APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0); |
| 839 | APInt &RHSKnownZero = KnownZero, &RHSKnownOne = KnownOne; |
| 840 | |
Chris Lattner | 4598c94 | 2009-01-31 08:24:16 +0000 | [diff] [blame] | 841 | // If there are multiple uses of this value and we aren't at the root, then |
| 842 | // we can't do any simplifications of the operands, because DemandedMask |
| 843 | // only reflects the bits demanded by *one* of the users. |
| 844 | if (Depth != 0 && !I->hasOneUse()) { |
Chris Lattner | d1b5e3f | 2009-01-31 08:40:03 +0000 | [diff] [blame] | 845 | // Despite the fact that we can't simplify this instruction in all User's |
| 846 | // context, we can at least compute the knownzero/knownone bits, and we can |
| 847 | // do simplifications that apply to *just* the one user if we know that |
| 848 | // this instruction has a simpler value in that context. |
| 849 | if (I->getOpcode() == Instruction::And) { |
| 850 | // If either the LHS or the RHS are Zero, the result is zero. |
| 851 | ComputeMaskedBits(I->getOperand(1), DemandedMask, |
| 852 | RHSKnownZero, RHSKnownOne, Depth+1); |
| 853 | ComputeMaskedBits(I->getOperand(0), DemandedMask & ~RHSKnownZero, |
| 854 | LHSKnownZero, LHSKnownOne, Depth+1); |
| 855 | |
| 856 | // If all of the demanded bits are known 1 on one side, return the other. |
| 857 | // These bits cannot contribute to the result of the 'and' in this |
| 858 | // context. |
| 859 | if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) == |
| 860 | (DemandedMask & ~LHSKnownZero)) |
| 861 | return I->getOperand(0); |
| 862 | if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) == |
| 863 | (DemandedMask & ~RHSKnownZero)) |
| 864 | return I->getOperand(1); |
| 865 | |
| 866 | // If all of the demanded bits in the inputs are known zeros, return zero. |
| 867 | if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask) |
| 868 | return Constant::getNullValue(VTy); |
| 869 | |
| 870 | } else if (I->getOpcode() == Instruction::Or) { |
| 871 | // We can simplify (X|Y) -> X or Y in the user's context if we know that |
| 872 | // only bits from X or Y are demanded. |
| 873 | |
| 874 | // If either the LHS or the RHS are One, the result is One. |
| 875 | ComputeMaskedBits(I->getOperand(1), DemandedMask, |
| 876 | RHSKnownZero, RHSKnownOne, Depth+1); |
| 877 | ComputeMaskedBits(I->getOperand(0), DemandedMask & ~RHSKnownOne, |
| 878 | LHSKnownZero, LHSKnownOne, Depth+1); |
| 879 | |
| 880 | // If all of the demanded bits are known zero on one side, return the |
| 881 | // other. These bits cannot contribute to the result of the 'or' in this |
| 882 | // context. |
| 883 | if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) == |
| 884 | (DemandedMask & ~LHSKnownOne)) |
| 885 | return I->getOperand(0); |
| 886 | if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) == |
| 887 | (DemandedMask & ~RHSKnownOne)) |
| 888 | return I->getOperand(1); |
| 889 | |
| 890 | // If all of the potentially set bits on one side are known to be set on |
| 891 | // the other side, just use the 'other' side. |
| 892 | if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) == |
| 893 | (DemandedMask & (~RHSKnownZero))) |
| 894 | return I->getOperand(0); |
| 895 | if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) == |
| 896 | (DemandedMask & (~LHSKnownZero))) |
| 897 | return I->getOperand(1); |
| 898 | } |
| 899 | |
Chris Lattner | 4598c94 | 2009-01-31 08:24:16 +0000 | [diff] [blame] | 900 | // Compute the KnownZero/KnownOne bits to simplify things downstream. |
| 901 | ComputeMaskedBits(I, DemandedMask, KnownZero, KnownOne, Depth); |
| 902 | return 0; |
| 903 | } |
| 904 | |
| 905 | // If this is the root being simplified, allow it to have multiple uses, |
| 906 | // just set the DemandedMask to all bits so that we can try to simplify the |
| 907 | // operands. This allows visitTruncInst (for example) to simplify the |
| 908 | // operand of a trunc without duplicating all the logic below. |
| 909 | if (Depth == 0 && !V->hasOneUse()) |
| 910 | DemandedMask = APInt::getAllOnesValue(BitWidth); |
| 911 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 912 | switch (I->getOpcode()) { |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 913 | default: |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 914 | ComputeMaskedBits(I, DemandedMask, RHSKnownZero, RHSKnownOne, Depth); |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 915 | break; |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 916 | case Instruction::And: |
| 917 | // If either the LHS or the RHS are Zero, the result is zero. |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 918 | if (SimplifyDemandedBits(I->getOperandUse(1), DemandedMask, |
| 919 | RHSKnownZero, RHSKnownOne, Depth+1) || |
| 920 | SimplifyDemandedBits(I->getOperandUse(0), DemandedMask & ~RHSKnownZero, |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 921 | LHSKnownZero, LHSKnownOne, Depth+1)) |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 922 | return I; |
| 923 | assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?"); |
| 924 | assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?"); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 925 | |
| 926 | // If all of the demanded bits are known 1 on one side, return the other. |
| 927 | // These bits cannot contribute to the result of the 'and'. |
| 928 | if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) == |
| 929 | (DemandedMask & ~LHSKnownZero)) |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 930 | return I->getOperand(0); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 931 | if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) == |
| 932 | (DemandedMask & ~RHSKnownZero)) |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 933 | return I->getOperand(1); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 934 | |
| 935 | // If all of the demanded bits in the inputs are known zeros, return zero. |
| 936 | if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask) |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 937 | return Constant::getNullValue(VTy); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 938 | |
| 939 | // If the RHS is a constant, see if we can simplify it. |
| 940 | if (ShrinkDemandedConstant(I, 1, DemandedMask & ~LHSKnownZero)) |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 941 | return I; |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 942 | |
| 943 | // Output known-1 bits are only known if set in both the LHS & RHS. |
| 944 | RHSKnownOne &= LHSKnownOne; |
| 945 | // Output known-0 are known to be clear if zero in either the LHS | RHS. |
| 946 | RHSKnownZero |= LHSKnownZero; |
| 947 | break; |
| 948 | case Instruction::Or: |
| 949 | // If either the LHS or the RHS are One, the result is One. |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 950 | if (SimplifyDemandedBits(I->getOperandUse(1), DemandedMask, |
| 951 | RHSKnownZero, RHSKnownOne, Depth+1) || |
| 952 | SimplifyDemandedBits(I->getOperandUse(0), DemandedMask & ~RHSKnownOne, |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 953 | LHSKnownZero, LHSKnownOne, Depth+1)) |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 954 | return I; |
| 955 | assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?"); |
| 956 | assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?"); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 957 | |
| 958 | // If all of the demanded bits are known zero on one side, return the other. |
| 959 | // These bits cannot contribute to the result of the 'or'. |
| 960 | if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) == |
| 961 | (DemandedMask & ~LHSKnownOne)) |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 962 | return I->getOperand(0); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 963 | if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) == |
| 964 | (DemandedMask & ~RHSKnownOne)) |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 965 | return I->getOperand(1); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 966 | |
| 967 | // If all of the potentially set bits on one side are known to be set on |
| 968 | // the other side, just use the 'other' side. |
| 969 | if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) == |
| 970 | (DemandedMask & (~RHSKnownZero))) |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 971 | return I->getOperand(0); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 972 | if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) == |
| 973 | (DemandedMask & (~LHSKnownZero))) |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 974 | return I->getOperand(1); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 975 | |
| 976 | // If the RHS is a constant, see if we can simplify it. |
| 977 | if (ShrinkDemandedConstant(I, 1, DemandedMask)) |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 978 | return I; |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 979 | |
| 980 | // Output known-0 bits are only known if clear in both the LHS & RHS. |
| 981 | RHSKnownZero &= LHSKnownZero; |
| 982 | // Output known-1 are known to be set if set in either the LHS | RHS. |
| 983 | RHSKnownOne |= LHSKnownOne; |
| 984 | break; |
| 985 | case Instruction::Xor: { |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 986 | if (SimplifyDemandedBits(I->getOperandUse(1), DemandedMask, |
| 987 | RHSKnownZero, RHSKnownOne, Depth+1) || |
| 988 | SimplifyDemandedBits(I->getOperandUse(0), DemandedMask, |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 989 | LHSKnownZero, LHSKnownOne, Depth+1)) |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 990 | return I; |
| 991 | assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?"); |
| 992 | assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?"); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 993 | |
| 994 | // If all of the demanded bits are known zero on one side, return the other. |
| 995 | // These bits cannot contribute to the result of the 'xor'. |
| 996 | if ((DemandedMask & RHSKnownZero) == DemandedMask) |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 997 | return I->getOperand(0); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 998 | if ((DemandedMask & LHSKnownZero) == DemandedMask) |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 999 | return I->getOperand(1); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1000 | |
| 1001 | // Output known-0 bits are known if clear or set in both the LHS & RHS. |
| 1002 | APInt KnownZeroOut = (RHSKnownZero & LHSKnownZero) | |
| 1003 | (RHSKnownOne & LHSKnownOne); |
| 1004 | // Output known-1 are known to be set if set in only one of the LHS, RHS. |
| 1005 | APInt KnownOneOut = (RHSKnownZero & LHSKnownOne) | |
| 1006 | (RHSKnownOne & LHSKnownZero); |
| 1007 | |
| 1008 | // If all of the demanded bits are known to be zero on one side or the |
| 1009 | // other, turn this into an *inclusive* or. |
| 1010 | // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0 |
| 1011 | if ((DemandedMask & ~RHSKnownZero & ~LHSKnownZero) == 0) { |
| 1012 | Instruction *Or = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1013 | BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1), |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1014 | I->getName()); |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1015 | return InsertNewInstBefore(Or, *I); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | // If all of the demanded bits on one side are known, and all of the set |
| 1019 | // bits on that side are also known to be set on the other side, turn this |
| 1020 | // into an AND, as we know the bits will be cleared. |
| 1021 | // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2 |
| 1022 | if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) { |
| 1023 | // all known |
| 1024 | if ((RHSKnownOne & LHSKnownOne) == RHSKnownOne) { |
| 1025 | Constant *AndC = ConstantInt::get(~RHSKnownOne & DemandedMask); |
| 1026 | Instruction *And = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1027 | BinaryOperator::CreateAnd(I->getOperand(0), AndC, "tmp"); |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1028 | return InsertNewInstBefore(And, *I); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | // If the RHS is a constant, see if we can simplify it. |
| 1033 | // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1. |
| 1034 | if (ShrinkDemandedConstant(I, 1, DemandedMask)) |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1035 | return I; |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1036 | |
| 1037 | RHSKnownZero = KnownZeroOut; |
| 1038 | RHSKnownOne = KnownOneOut; |
| 1039 | break; |
| 1040 | } |
| 1041 | case Instruction::Select: |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1042 | if (SimplifyDemandedBits(I->getOperandUse(2), DemandedMask, |
| 1043 | RHSKnownZero, RHSKnownOne, Depth+1) || |
| 1044 | SimplifyDemandedBits(I->getOperandUse(1), DemandedMask, |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1045 | LHSKnownZero, LHSKnownOne, Depth+1)) |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1046 | return I; |
| 1047 | assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?"); |
| 1048 | assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?"); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1049 | |
| 1050 | // If the operands are constants, see if we can simplify them. |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1051 | if (ShrinkDemandedConstant(I, 1, DemandedMask) || |
| 1052 | ShrinkDemandedConstant(I, 2, DemandedMask)) |
| 1053 | return I; |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1054 | |
| 1055 | // Only known if known in both the LHS and RHS. |
| 1056 | RHSKnownOne &= LHSKnownOne; |
| 1057 | RHSKnownZero &= LHSKnownZero; |
| 1058 | break; |
| 1059 | case Instruction::Trunc: { |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1060 | unsigned truncBf = I->getOperand(0)->getType()->getPrimitiveSizeInBits(); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1061 | DemandedMask.zext(truncBf); |
| 1062 | RHSKnownZero.zext(truncBf); |
| 1063 | RHSKnownOne.zext(truncBf); |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1064 | if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMask, |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1065 | RHSKnownZero, RHSKnownOne, Depth+1)) |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1066 | return I; |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1067 | DemandedMask.trunc(BitWidth); |
| 1068 | RHSKnownZero.trunc(BitWidth); |
| 1069 | RHSKnownOne.trunc(BitWidth); |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1070 | assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?"); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1071 | break; |
| 1072 | } |
| 1073 | case Instruction::BitCast: |
| 1074 | if (!I->getOperand(0)->getType()->isInteger()) |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1075 | return false; // vector->int or fp->int? |
| 1076 | if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMask, |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1077 | RHSKnownZero, RHSKnownOne, Depth+1)) |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1078 | return I; |
| 1079 | assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?"); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1080 | break; |
| 1081 | case Instruction::ZExt: { |
| 1082 | // Compute the bits in the result that are not present in the input. |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1083 | unsigned SrcBitWidth =I->getOperand(0)->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1084 | |
Zhou Sheng | d48653a | 2007-03-29 04:45:55 +0000 | [diff] [blame] | 1085 | DemandedMask.trunc(SrcBitWidth); |
| 1086 | RHSKnownZero.trunc(SrcBitWidth); |
| 1087 | RHSKnownOne.trunc(SrcBitWidth); |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1088 | if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMask, |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1089 | RHSKnownZero, RHSKnownOne, Depth+1)) |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1090 | return I; |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1091 | DemandedMask.zext(BitWidth); |
| 1092 | RHSKnownZero.zext(BitWidth); |
| 1093 | RHSKnownOne.zext(BitWidth); |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1094 | assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?"); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1095 | // The top bits are known to be zero. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1096 | RHSKnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1097 | break; |
| 1098 | } |
| 1099 | case Instruction::SExt: { |
| 1100 | // Compute the bits in the result that are not present in the input. |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1101 | unsigned SrcBitWidth =I->getOperand(0)->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1102 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1103 | APInt InputDemandedBits = DemandedMask & |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1104 | APInt::getLowBitsSet(BitWidth, SrcBitWidth); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1105 | |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1106 | APInt NewBits(APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1107 | // If any of the sign extended bits are demanded, we know that the sign |
| 1108 | // bit is demanded. |
| 1109 | if ((NewBits & DemandedMask) != 0) |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 1110 | InputDemandedBits.set(SrcBitWidth-1); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1111 | |
Zhou Sheng | d48653a | 2007-03-29 04:45:55 +0000 | [diff] [blame] | 1112 | InputDemandedBits.trunc(SrcBitWidth); |
| 1113 | RHSKnownZero.trunc(SrcBitWidth); |
| 1114 | RHSKnownOne.trunc(SrcBitWidth); |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1115 | if (SimplifyDemandedBits(I->getOperandUse(0), InputDemandedBits, |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1116 | RHSKnownZero, RHSKnownOne, Depth+1)) |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1117 | return I; |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1118 | InputDemandedBits.zext(BitWidth); |
| 1119 | RHSKnownZero.zext(BitWidth); |
| 1120 | RHSKnownOne.zext(BitWidth); |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1121 | assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?"); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1122 | |
| 1123 | // If the sign bit of the input is known set or clear, then we know the |
| 1124 | // top bits of the result. |
| 1125 | |
| 1126 | // If the input sign bit is known zero, or if the NewBits are not demanded |
| 1127 | // convert this into a zero extension. |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1128 | if (RHSKnownZero[SrcBitWidth-1] || (NewBits & ~DemandedMask) == NewBits) { |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1129 | // Convert to ZExt cast |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1130 | CastInst *NewCast = new ZExtInst(I->getOperand(0), VTy, I->getName()); |
| 1131 | return InsertNewInstBefore(NewCast, *I); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1132 | } else if (RHSKnownOne[SrcBitWidth-1]) { // Input sign bit known set |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1133 | RHSKnownOne |= NewBits; |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1134 | } |
| 1135 | break; |
| 1136 | } |
| 1137 | case Instruction::Add: { |
| 1138 | // Figure out what the input bits are. If the top bits of the and result |
| 1139 | // are not demanded, then the add doesn't demand them from its input |
| 1140 | // either. |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1141 | unsigned NLZ = DemandedMask.countLeadingZeros(); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1142 | |
| 1143 | // If there is a constant on the RHS, there are a variety of xformations |
| 1144 | // we can do. |
| 1145 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 1146 | // If null, this should be simplified elsewhere. Some of the xforms here |
| 1147 | // won't work if the RHS is zero. |
| 1148 | if (RHS->isZero()) |
| 1149 | break; |
| 1150 | |
| 1151 | // If the top bit of the output is demanded, demand everything from the |
| 1152 | // input. Otherwise, we demand all the input bits except NLZ top bits. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1153 | APInt InDemandedBits(APInt::getLowBitsSet(BitWidth, BitWidth - NLZ)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1154 | |
| 1155 | // Find information about known zero/one bits in the input. |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1156 | if (SimplifyDemandedBits(I->getOperandUse(0), InDemandedBits, |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1157 | LHSKnownZero, LHSKnownOne, Depth+1)) |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1158 | return I; |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1159 | |
| 1160 | // If the RHS of the add has bits set that can't affect the input, reduce |
| 1161 | // the constant. |
| 1162 | if (ShrinkDemandedConstant(I, 1, InDemandedBits)) |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1163 | return I; |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1164 | |
| 1165 | // Avoid excess work. |
| 1166 | if (LHSKnownZero == 0 && LHSKnownOne == 0) |
| 1167 | break; |
| 1168 | |
| 1169 | // Turn it into OR if input bits are zero. |
| 1170 | if ((LHSKnownZero & RHS->getValue()) == RHS->getValue()) { |
| 1171 | Instruction *Or = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1172 | BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1), |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1173 | I->getName()); |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1174 | return InsertNewInstBefore(Or, *I); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
| 1177 | // We can say something about the output known-zero and known-one bits, |
| 1178 | // depending on potential carries from the input constant and the |
| 1179 | // unknowns. For example if the LHS is known to have at most the 0x0F0F0 |
| 1180 | // bits set and the RHS constant is 0x01001, then we know we have a known |
| 1181 | // one mask of 0x00001 and a known zero mask of 0xE0F0E. |
| 1182 | |
| 1183 | // To compute this, we first compute the potential carry bits. These are |
| 1184 | // the bits which may be modified. I'm not aware of a better way to do |
| 1185 | // this scan. |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1186 | const APInt &RHSVal = RHS->getValue(); |
Zhou Sheng | b9cb95f | 2007-03-31 02:38:39 +0000 | [diff] [blame] | 1187 | APInt CarryBits((~LHSKnownZero + RHSVal) ^ (~LHSKnownZero ^ RHSVal)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1188 | |
| 1189 | // Now that we know which bits have carries, compute the known-1/0 sets. |
| 1190 | |
| 1191 | // Bits are known one if they are known zero in one operand and one in the |
| 1192 | // other, and there is no input carry. |
| 1193 | RHSKnownOne = ((LHSKnownZero & RHSVal) | |
| 1194 | (LHSKnownOne & ~RHSVal)) & ~CarryBits; |
| 1195 | |
| 1196 | // Bits are known zero if they are known zero in both operands and there |
| 1197 | // is no input carry. |
| 1198 | RHSKnownZero = LHSKnownZero & ~RHSVal & ~CarryBits; |
| 1199 | } else { |
| 1200 | // If the high-bits of this ADD are not demanded, then it does not demand |
| 1201 | // the high bits of its LHS or RHS. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1202 | if (DemandedMask[BitWidth-1] == 0) { |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1203 | // Right fill the mask of bits for this ADD to demand the most |
| 1204 | // significant bit and all those below it. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1205 | APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ)); |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1206 | if (SimplifyDemandedBits(I->getOperandUse(0), DemandedFromOps, |
| 1207 | LHSKnownZero, LHSKnownOne, Depth+1) || |
| 1208 | SimplifyDemandedBits(I->getOperandUse(1), DemandedFromOps, |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1209 | LHSKnownZero, LHSKnownOne, Depth+1)) |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1210 | return I; |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1211 | } |
| 1212 | } |
| 1213 | break; |
| 1214 | } |
| 1215 | case Instruction::Sub: |
| 1216 | // If the high-bits of this SUB are not demanded, then it does not demand |
| 1217 | // the high bits of its LHS or RHS. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1218 | if (DemandedMask[BitWidth-1] == 0) { |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1219 | // Right fill the mask of bits for this SUB to demand the most |
| 1220 | // significant bit and all those below it. |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 1221 | uint32_t NLZ = DemandedMask.countLeadingZeros(); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1222 | APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ)); |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1223 | if (SimplifyDemandedBits(I->getOperandUse(0), DemandedFromOps, |
| 1224 | LHSKnownZero, LHSKnownOne, Depth+1) || |
| 1225 | SimplifyDemandedBits(I->getOperandUse(1), DemandedFromOps, |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1226 | LHSKnownZero, LHSKnownOne, Depth+1)) |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1227 | return I; |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1228 | } |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1229 | // Otherwise just hand the sub off to ComputeMaskedBits to fill in |
| 1230 | // the known zeros and ones. |
| 1231 | ComputeMaskedBits(V, DemandedMask, RHSKnownZero, RHSKnownOne, Depth); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1232 | break; |
| 1233 | case Instruction::Shl: |
| 1234 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 1235 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1236 | APInt DemandedMaskIn(DemandedMask.lshr(ShiftAmt)); |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1237 | if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMaskIn, |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1238 | RHSKnownZero, RHSKnownOne, Depth+1)) |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1239 | return I; |
| 1240 | assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?"); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1241 | RHSKnownZero <<= ShiftAmt; |
| 1242 | RHSKnownOne <<= ShiftAmt; |
| 1243 | // low bits known zero. |
Zhou Sheng | adc1495 | 2007-03-14 09:07:33 +0000 | [diff] [blame] | 1244 | if (ShiftAmt) |
Zhou Sheng | e9e03f6 | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 1245 | RHSKnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1246 | } |
| 1247 | break; |
| 1248 | case Instruction::LShr: |
| 1249 | // For a logical shift right |
| 1250 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 1251 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1252 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1253 | // Unsigned shift right. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1254 | APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt)); |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1255 | if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMaskIn, |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1256 | RHSKnownZero, RHSKnownOne, Depth+1)) |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1257 | return I; |
| 1258 | assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?"); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1259 | RHSKnownZero = APIntOps::lshr(RHSKnownZero, ShiftAmt); |
| 1260 | RHSKnownOne = APIntOps::lshr(RHSKnownOne, ShiftAmt); |
Zhou Sheng | adc1495 | 2007-03-14 09:07:33 +0000 | [diff] [blame] | 1261 | if (ShiftAmt) { |
| 1262 | // Compute the new bits that are at the top now. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1263 | APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt)); |
Zhou Sheng | adc1495 | 2007-03-14 09:07:33 +0000 | [diff] [blame] | 1264 | RHSKnownZero |= HighBits; // high bits known zero. |
| 1265 | } |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1266 | } |
| 1267 | break; |
| 1268 | case Instruction::AShr: |
| 1269 | // If this is an arithmetic shift right and only the low-bit is set, we can |
| 1270 | // always convert this into a logical shr, even if the shift amount is |
| 1271 | // variable. The low bit of the shift cannot be an input sign bit unless |
| 1272 | // the shift amount is >= the size of the datatype, which is undefined. |
| 1273 | if (DemandedMask == 1) { |
| 1274 | // Perform the logical shift right. |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1275 | Instruction *NewVal = BinaryOperator::CreateLShr( |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1276 | I->getOperand(0), I->getOperand(1), I->getName()); |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1277 | return InsertNewInstBefore(NewVal, *I); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1278 | } |
Chris Lattner | 4241e4d | 2007-07-15 20:54:51 +0000 | [diff] [blame] | 1279 | |
| 1280 | // If the sign bit is the only bit demanded by this ashr, then there is no |
| 1281 | // need to do it, the shift doesn't change the high bit. |
| 1282 | if (DemandedMask.isSignBit()) |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1283 | return I->getOperand(0); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1284 | |
| 1285 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 1286 | uint32_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1287 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1288 | // Signed shift right. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1289 | APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt)); |
Lauro Ramos Venancio | d0499af | 2007-06-06 17:08:48 +0000 | [diff] [blame] | 1290 | // If any of the "high bits" are demanded, we should set the sign bit as |
| 1291 | // demanded. |
| 1292 | if (DemandedMask.countLeadingZeros() <= ShiftAmt) |
| 1293 | DemandedMaskIn.set(BitWidth-1); |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1294 | if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMaskIn, |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1295 | RHSKnownZero, RHSKnownOne, Depth+1)) |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1296 | return I; |
| 1297 | assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?"); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1298 | // Compute the new bits that are at the top now. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1299 | APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1300 | RHSKnownZero = APIntOps::lshr(RHSKnownZero, ShiftAmt); |
| 1301 | RHSKnownOne = APIntOps::lshr(RHSKnownOne, ShiftAmt); |
| 1302 | |
| 1303 | // Handle the sign bits. |
| 1304 | APInt SignBit(APInt::getSignBit(BitWidth)); |
| 1305 | // Adjust to where it is now in the mask. |
| 1306 | SignBit = APIntOps::lshr(SignBit, ShiftAmt); |
| 1307 | |
| 1308 | // If the input sign bit is known to be zero, or if none of the top bits |
| 1309 | // are demanded, turn this into an unsigned shift right. |
Zhou Sheng | cc41940 | 2008-06-06 08:32:05 +0000 | [diff] [blame] | 1310 | if (BitWidth <= ShiftAmt || RHSKnownZero[BitWidth-ShiftAmt-1] || |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1311 | (HighBits & ~DemandedMask) == HighBits) { |
| 1312 | // Perform the logical shift right. |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1313 | Instruction *NewVal = BinaryOperator::CreateLShr( |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1314 | I->getOperand(0), SA, I->getName()); |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1315 | return InsertNewInstBefore(NewVal, *I); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1316 | } else if ((RHSKnownOne & SignBit) != 0) { // New bits are known one. |
| 1317 | RHSKnownOne |= HighBits; |
| 1318 | } |
| 1319 | } |
| 1320 | break; |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 1321 | case Instruction::SRem: |
| 1322 | if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Nick Lewycky | 8e39432 | 2008-11-02 02:41:50 +0000 | [diff] [blame] | 1323 | APInt RA = Rem->getValue().abs(); |
| 1324 | if (RA.isPowerOf2()) { |
Nick Lewycky | 3ac9e10 | 2008-07-12 05:04:38 +0000 | [diff] [blame] | 1325 | if (DemandedMask.ule(RA)) // srem won't affect demanded bits |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1326 | return I->getOperand(0); |
Nick Lewycky | 3ac9e10 | 2008-07-12 05:04:38 +0000 | [diff] [blame] | 1327 | |
Nick Lewycky | 8e39432 | 2008-11-02 02:41:50 +0000 | [diff] [blame] | 1328 | APInt LowBits = RA - 1; |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 1329 | APInt Mask2 = LowBits | APInt::getSignBit(BitWidth); |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1330 | if (SimplifyDemandedBits(I->getOperandUse(0), Mask2, |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 1331 | LHSKnownZero, LHSKnownOne, Depth+1)) |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1332 | return I; |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 1333 | |
| 1334 | if (LHSKnownZero[BitWidth-1] || ((LHSKnownZero & LowBits) == LowBits)) |
| 1335 | LHSKnownZero |= ~LowBits; |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 1336 | |
| 1337 | KnownZero |= LHSKnownZero & DemandedMask; |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 1338 | |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1339 | assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?"); |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 1340 | } |
| 1341 | } |
| 1342 | break; |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1343 | case Instruction::URem: { |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1344 | APInt KnownZero2(BitWidth, 0), KnownOne2(BitWidth, 0); |
| 1345 | APInt AllOnes = APInt::getAllOnesValue(BitWidth); |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1346 | if (SimplifyDemandedBits(I->getOperandUse(0), AllOnes, |
| 1347 | KnownZero2, KnownOne2, Depth+1) || |
| 1348 | SimplifyDemandedBits(I->getOperandUse(1), AllOnes, |
Dan Gohman | e85b758 | 2008-05-01 19:13:24 +0000 | [diff] [blame] | 1349 | KnownZero2, KnownOne2, Depth+1)) |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1350 | return I; |
Dan Gohman | e85b758 | 2008-05-01 19:13:24 +0000 | [diff] [blame] | 1351 | |
Chris Lattner | 455e9ab | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1352 | unsigned Leaders = KnownZero2.countLeadingOnes(); |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1353 | Leaders = std::max(Leaders, |
| 1354 | KnownZero2.countLeadingOnes()); |
| 1355 | KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & DemandedMask; |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 1356 | break; |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1357 | } |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 1358 | case Instruction::Call: |
| 1359 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { |
| 1360 | switch (II->getIntrinsicID()) { |
| 1361 | default: break; |
| 1362 | case Intrinsic::bswap: { |
| 1363 | // If the only bits demanded come from one byte of the bswap result, |
| 1364 | // just shift the input byte into position to eliminate the bswap. |
| 1365 | unsigned NLZ = DemandedMask.countLeadingZeros(); |
| 1366 | unsigned NTZ = DemandedMask.countTrailingZeros(); |
| 1367 | |
| 1368 | // Round NTZ down to the next byte. If we have 11 trailing zeros, then |
| 1369 | // we need all the bits down to bit 8. Likewise, round NLZ. If we |
| 1370 | // have 14 leading zeros, round to 8. |
| 1371 | NLZ &= ~7; |
| 1372 | NTZ &= ~7; |
| 1373 | // If we need exactly one byte, we can do this transformation. |
| 1374 | if (BitWidth-NLZ-NTZ == 8) { |
| 1375 | unsigned ResultBit = NTZ; |
| 1376 | unsigned InputBit = BitWidth-NTZ-8; |
| 1377 | |
| 1378 | // Replace this with either a left or right shift to get the byte into |
| 1379 | // the right place. |
| 1380 | Instruction *NewVal; |
| 1381 | if (InputBit > ResultBit) |
| 1382 | NewVal = BinaryOperator::CreateLShr(I->getOperand(1), |
| 1383 | ConstantInt::get(I->getType(), InputBit-ResultBit)); |
| 1384 | else |
| 1385 | NewVal = BinaryOperator::CreateShl(I->getOperand(1), |
| 1386 | ConstantInt::get(I->getType(), ResultBit-InputBit)); |
| 1387 | NewVal->takeName(I); |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1388 | return InsertNewInstBefore(NewVal, *I); |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 1389 | } |
| 1390 | |
| 1391 | // TODO: Could compute known zero/one bits based on the input. |
| 1392 | break; |
| 1393 | } |
| 1394 | } |
| 1395 | } |
Chris Lattner | 6c3bfba | 2008-06-18 18:11:55 +0000 | [diff] [blame] | 1396 | ComputeMaskedBits(V, DemandedMask, RHSKnownZero, RHSKnownOne, Depth); |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 1397 | break; |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1398 | } |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1399 | |
| 1400 | // If the client is only demanding bits that we know, return the known |
| 1401 | // constant. |
| 1402 | if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1403 | return ConstantInt::get(RHSKnownOne); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1404 | return false; |
| 1405 | } |
| 1406 | |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1407 | |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 1408 | /// SimplifyDemandedVectorElts - The specified value produces a vector with |
Evan Cheng | 388df62 | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1409 | /// any number of elements. DemandedElts contains the set of elements that are |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1410 | /// actually used by the caller. This method analyzes which elements of the |
| 1411 | /// operand are undef and returns that information in UndefElts. |
| 1412 | /// |
| 1413 | /// If the information about demanded elements can be used to simplify the |
| 1414 | /// operation, the operation is simplified, then the resultant value is |
| 1415 | /// returned. This returns null if no change was made. |
Evan Cheng | 388df62 | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1416 | Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, APInt DemandedElts, |
| 1417 | APInt& UndefElts, |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1418 | unsigned Depth) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1419 | unsigned VWidth = cast<VectorType>(V->getType())->getNumElements(); |
Evan Cheng | 388df62 | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1420 | APInt EltMask(APInt::getAllOnesValue(VWidth)); |
Dan Gohman | 488fbfc | 2008-09-09 18:11:14 +0000 | [diff] [blame] | 1421 | assert((DemandedElts & ~EltMask) == 0 && "Invalid DemandedElts!"); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1422 | |
| 1423 | if (isa<UndefValue>(V)) { |
| 1424 | // If the entire vector is undefined, just return this info. |
| 1425 | UndefElts = EltMask; |
| 1426 | return 0; |
| 1427 | } else if (DemandedElts == 0) { // If nothing is demanded, provide undef. |
| 1428 | UndefElts = EltMask; |
| 1429 | return UndefValue::get(V->getType()); |
| 1430 | } |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 1431 | |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1432 | UndefElts = 0; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1433 | if (ConstantVector *CP = dyn_cast<ConstantVector>(V)) { |
| 1434 | const Type *EltTy = cast<VectorType>(V->getType())->getElementType(); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1435 | Constant *Undef = UndefValue::get(EltTy); |
| 1436 | |
| 1437 | std::vector<Constant*> Elts; |
| 1438 | for (unsigned i = 0; i != VWidth; ++i) |
Evan Cheng | 388df62 | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1439 | if (!DemandedElts[i]) { // If not demanded, set to undef. |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1440 | Elts.push_back(Undef); |
Evan Cheng | 388df62 | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1441 | UndefElts.set(i); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1442 | } else if (isa<UndefValue>(CP->getOperand(i))) { // Already undef. |
| 1443 | Elts.push_back(Undef); |
Evan Cheng | 388df62 | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1444 | UndefElts.set(i); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1445 | } else { // Otherwise, defined. |
| 1446 | Elts.push_back(CP->getOperand(i)); |
| 1447 | } |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 1448 | |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1449 | // If we changed the constant, return it. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1450 | Constant *NewCP = ConstantVector::get(Elts); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1451 | return NewCP != CP ? NewCP : 0; |
| 1452 | } else if (isa<ConstantAggregateZero>(V)) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1453 | // Simplify the CAZ to a ConstantVector where the non-demanded elements are |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1454 | // set to undef. |
Mon P Wang | e0b436a | 2008-11-06 22:52:21 +0000 | [diff] [blame] | 1455 | |
| 1456 | // Check if this is identity. If so, return 0 since we are not simplifying |
| 1457 | // anything. |
| 1458 | if (DemandedElts == ((1ULL << VWidth) -1)) |
| 1459 | return 0; |
| 1460 | |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1461 | const Type *EltTy = cast<VectorType>(V->getType())->getElementType(); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1462 | Constant *Zero = Constant::getNullValue(EltTy); |
| 1463 | Constant *Undef = UndefValue::get(EltTy); |
| 1464 | std::vector<Constant*> Elts; |
Evan Cheng | 388df62 | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1465 | for (unsigned i = 0; i != VWidth; ++i) { |
| 1466 | Constant *Elt = DemandedElts[i] ? Zero : Undef; |
| 1467 | Elts.push_back(Elt); |
| 1468 | } |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1469 | UndefElts = DemandedElts ^ EltMask; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1470 | return ConstantVector::get(Elts); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1471 | } |
| 1472 | |
Dan Gohman | 488fbfc | 2008-09-09 18:11:14 +0000 | [diff] [blame] | 1473 | // Limit search depth. |
| 1474 | if (Depth == 10) |
| 1475 | return false; |
| 1476 | |
| 1477 | // If multiple users are using the root value, procede with |
| 1478 | // simplification conservatively assuming that all elements |
| 1479 | // are needed. |
| 1480 | if (!V->hasOneUse()) { |
| 1481 | // Quit if we find multiple users of a non-root value though. |
| 1482 | // They'll be handled when it's their turn to be visited by |
| 1483 | // the main instcombine process. |
| 1484 | if (Depth != 0) |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1485 | // TODO: Just compute the UndefElts information recursively. |
| 1486 | return false; |
Dan Gohman | 488fbfc | 2008-09-09 18:11:14 +0000 | [diff] [blame] | 1487 | |
| 1488 | // Conservatively assume that all elements are needed. |
| 1489 | DemandedElts = EltMask; |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1490 | } |
| 1491 | |
| 1492 | Instruction *I = dyn_cast<Instruction>(V); |
| 1493 | if (!I) return false; // Only analyze instructions. |
| 1494 | |
| 1495 | bool MadeChange = false; |
Evan Cheng | 388df62 | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1496 | APInt UndefElts2(VWidth, 0); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1497 | Value *TmpV; |
| 1498 | switch (I->getOpcode()) { |
| 1499 | default: break; |
| 1500 | |
| 1501 | case Instruction::InsertElement: { |
| 1502 | // If this is a variable index, we don't know which element it overwrites. |
| 1503 | // demand exactly the same input as we produce. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1504 | ConstantInt *Idx = dyn_cast<ConstantInt>(I->getOperand(2)); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1505 | if (Idx == 0) { |
| 1506 | // Note that we can't propagate undef elt info, because we don't know |
| 1507 | // which elt is getting updated. |
| 1508 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, |
| 1509 | UndefElts2, Depth+1); |
| 1510 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } |
| 1511 | break; |
| 1512 | } |
| 1513 | |
| 1514 | // If this is inserting an element that isn't demanded, remove this |
| 1515 | // insertelement. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1516 | unsigned IdxNo = Idx->getZExtValue(); |
Evan Cheng | 388df62 | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1517 | if (IdxNo >= VWidth || !DemandedElts[IdxNo]) |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1518 | return AddSoonDeadInstToWorklist(*I, 0); |
| 1519 | |
| 1520 | // Otherwise, the element inserted overwrites whatever was there, so the |
| 1521 | // input demanded set is simpler than the output set. |
Evan Cheng | 388df62 | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1522 | APInt DemandedElts2 = DemandedElts; |
| 1523 | DemandedElts2.clear(IdxNo); |
| 1524 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts2, |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1525 | UndefElts, Depth+1); |
| 1526 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } |
| 1527 | |
| 1528 | // The inserted element is defined. |
Evan Cheng | 388df62 | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1529 | UndefElts.clear(IdxNo); |
Dan Gohman | 488fbfc | 2008-09-09 18:11:14 +0000 | [diff] [blame] | 1530 | break; |
| 1531 | } |
| 1532 | case Instruction::ShuffleVector: { |
| 1533 | ShuffleVectorInst *Shuffle = cast<ShuffleVectorInst>(I); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 1534 | uint64_t LHSVWidth = |
| 1535 | cast<VectorType>(Shuffle->getOperand(0)->getType())->getNumElements(); |
Evan Cheng | 388df62 | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1536 | APInt LeftDemanded(LHSVWidth, 0), RightDemanded(LHSVWidth, 0); |
Dan Gohman | 488fbfc | 2008-09-09 18:11:14 +0000 | [diff] [blame] | 1537 | for (unsigned i = 0; i < VWidth; i++) { |
Evan Cheng | 388df62 | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1538 | if (DemandedElts[i]) { |
Dan Gohman | 488fbfc | 2008-09-09 18:11:14 +0000 | [diff] [blame] | 1539 | unsigned MaskVal = Shuffle->getMaskValue(i); |
| 1540 | if (MaskVal != -1u) { |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 1541 | assert(MaskVal < LHSVWidth * 2 && |
Dan Gohman | 488fbfc | 2008-09-09 18:11:14 +0000 | [diff] [blame] | 1542 | "shufflevector mask index out of range!"); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 1543 | if (MaskVal < LHSVWidth) |
Evan Cheng | 388df62 | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1544 | LeftDemanded.set(MaskVal); |
Dan Gohman | 488fbfc | 2008-09-09 18:11:14 +0000 | [diff] [blame] | 1545 | else |
Evan Cheng | 388df62 | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1546 | RightDemanded.set(MaskVal - LHSVWidth); |
Dan Gohman | 488fbfc | 2008-09-09 18:11:14 +0000 | [diff] [blame] | 1547 | } |
| 1548 | } |
| 1549 | } |
| 1550 | |
Nate Begeman | 7b25467 | 2009-02-11 22:36:25 +0000 | [diff] [blame] | 1551 | APInt UndefElts4(LHSVWidth, 0); |
Dan Gohman | 488fbfc | 2008-09-09 18:11:14 +0000 | [diff] [blame] | 1552 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), LeftDemanded, |
Nate Begeman | 7b25467 | 2009-02-11 22:36:25 +0000 | [diff] [blame] | 1553 | UndefElts4, Depth+1); |
Dan Gohman | 488fbfc | 2008-09-09 18:11:14 +0000 | [diff] [blame] | 1554 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } |
| 1555 | |
Nate Begeman | 7b25467 | 2009-02-11 22:36:25 +0000 | [diff] [blame] | 1556 | APInt UndefElts3(LHSVWidth, 0); |
Dan Gohman | 488fbfc | 2008-09-09 18:11:14 +0000 | [diff] [blame] | 1557 | TmpV = SimplifyDemandedVectorElts(I->getOperand(1), RightDemanded, |
| 1558 | UndefElts3, Depth+1); |
| 1559 | if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; } |
| 1560 | |
| 1561 | bool NewUndefElts = false; |
| 1562 | for (unsigned i = 0; i < VWidth; i++) { |
| 1563 | unsigned MaskVal = Shuffle->getMaskValue(i); |
Dan Gohman | cb89309 | 2008-09-10 01:09:32 +0000 | [diff] [blame] | 1564 | if (MaskVal == -1u) { |
Evan Cheng | 388df62 | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1565 | UndefElts.set(i); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 1566 | } else if (MaskVal < LHSVWidth) { |
Nate Begeman | 7b25467 | 2009-02-11 22:36:25 +0000 | [diff] [blame] | 1567 | if (UndefElts4[MaskVal]) { |
Evan Cheng | 388df62 | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1568 | NewUndefElts = true; |
| 1569 | UndefElts.set(i); |
| 1570 | } |
Dan Gohman | 488fbfc | 2008-09-09 18:11:14 +0000 | [diff] [blame] | 1571 | } else { |
Evan Cheng | 388df62 | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1572 | if (UndefElts3[MaskVal - LHSVWidth]) { |
| 1573 | NewUndefElts = true; |
| 1574 | UndefElts.set(i); |
| 1575 | } |
Dan Gohman | 488fbfc | 2008-09-09 18:11:14 +0000 | [diff] [blame] | 1576 | } |
| 1577 | } |
| 1578 | |
| 1579 | if (NewUndefElts) { |
| 1580 | // Add additional discovered undefs. |
| 1581 | std::vector<Constant*> Elts; |
| 1582 | for (unsigned i = 0; i < VWidth; ++i) { |
Evan Cheng | 388df62 | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1583 | if (UndefElts[i]) |
Dan Gohman | 488fbfc | 2008-09-09 18:11:14 +0000 | [diff] [blame] | 1584 | Elts.push_back(UndefValue::get(Type::Int32Ty)); |
| 1585 | else |
| 1586 | Elts.push_back(ConstantInt::get(Type::Int32Ty, |
| 1587 | Shuffle->getMaskValue(i))); |
| 1588 | } |
| 1589 | I->setOperand(2, ConstantVector::get(Elts)); |
| 1590 | MadeChange = true; |
| 1591 | } |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1592 | break; |
| 1593 | } |
Chris Lattner | 6987833 | 2007-04-14 22:29:23 +0000 | [diff] [blame] | 1594 | case Instruction::BitCast: { |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 1595 | // Vector->vector casts only. |
Chris Lattner | 6987833 | 2007-04-14 22:29:23 +0000 | [diff] [blame] | 1596 | const VectorType *VTy = dyn_cast<VectorType>(I->getOperand(0)->getType()); |
| 1597 | if (!VTy) break; |
| 1598 | unsigned InVWidth = VTy->getNumElements(); |
Evan Cheng | 388df62 | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1599 | APInt InputDemandedElts(InVWidth, 0); |
Chris Lattner | 6987833 | 2007-04-14 22:29:23 +0000 | [diff] [blame] | 1600 | unsigned Ratio; |
| 1601 | |
| 1602 | if (VWidth == InVWidth) { |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 1603 | // 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] | 1604 | // elements as are demanded of us. |
| 1605 | Ratio = 1; |
| 1606 | InputDemandedElts = DemandedElts; |
| 1607 | } else if (VWidth > InVWidth) { |
| 1608 | // Untested so far. |
| 1609 | break; |
| 1610 | |
| 1611 | // If there are more elements in the result than there are in the source, |
| 1612 | // then an input element is live if any of the corresponding output |
| 1613 | // elements are live. |
| 1614 | Ratio = VWidth/InVWidth; |
| 1615 | for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) { |
Evan Cheng | 388df62 | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1616 | if (DemandedElts[OutIdx]) |
| 1617 | InputDemandedElts.set(OutIdx/Ratio); |
Chris Lattner | 6987833 | 2007-04-14 22:29:23 +0000 | [diff] [blame] | 1618 | } |
| 1619 | } else { |
| 1620 | // Untested so far. |
| 1621 | break; |
| 1622 | |
| 1623 | // If there are more elements in the source than there are in the result, |
| 1624 | // then an input element is live if the corresponding output element is |
| 1625 | // live. |
| 1626 | Ratio = InVWidth/VWidth; |
| 1627 | for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx) |
Evan Cheng | 388df62 | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1628 | if (DemandedElts[InIdx/Ratio]) |
| 1629 | InputDemandedElts.set(InIdx); |
Chris Lattner | 6987833 | 2007-04-14 22:29:23 +0000 | [diff] [blame] | 1630 | } |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1631 | |
Chris Lattner | 6987833 | 2007-04-14 22:29:23 +0000 | [diff] [blame] | 1632 | // div/rem demand all inputs, because they don't want divide by zero. |
| 1633 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), InputDemandedElts, |
| 1634 | UndefElts2, Depth+1); |
| 1635 | if (TmpV) { |
| 1636 | I->setOperand(0, TmpV); |
| 1637 | MadeChange = true; |
| 1638 | } |
| 1639 | |
| 1640 | UndefElts = UndefElts2; |
| 1641 | if (VWidth > InVWidth) { |
| 1642 | assert(0 && "Unimp"); |
| 1643 | // If there are more elements in the result than there are in the source, |
| 1644 | // then an output element is undef if the corresponding input element is |
| 1645 | // undef. |
| 1646 | for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) |
Evan Cheng | 388df62 | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1647 | if (UndefElts2[OutIdx/Ratio]) |
| 1648 | UndefElts.set(OutIdx); |
Chris Lattner | 6987833 | 2007-04-14 22:29:23 +0000 | [diff] [blame] | 1649 | } else if (VWidth < InVWidth) { |
| 1650 | assert(0 && "Unimp"); |
| 1651 | // If there are more elements in the source than there are in the result, |
| 1652 | // then a result element is undef if all of the corresponding input |
| 1653 | // elements are undef. |
| 1654 | UndefElts = ~0ULL >> (64-VWidth); // Start out all undef. |
| 1655 | for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx) |
Evan Cheng | 388df62 | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1656 | if (!UndefElts2[InIdx]) // Not undef? |
| 1657 | UndefElts.clear(InIdx/Ratio); // Clear undef bit. |
Chris Lattner | 6987833 | 2007-04-14 22:29:23 +0000 | [diff] [blame] | 1658 | } |
| 1659 | break; |
| 1660 | } |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1661 | case Instruction::And: |
| 1662 | case Instruction::Or: |
| 1663 | case Instruction::Xor: |
| 1664 | case Instruction::Add: |
| 1665 | case Instruction::Sub: |
| 1666 | case Instruction::Mul: |
| 1667 | // div/rem demand all inputs, because they don't want divide by zero. |
| 1668 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, |
| 1669 | UndefElts, Depth+1); |
| 1670 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } |
| 1671 | TmpV = SimplifyDemandedVectorElts(I->getOperand(1), DemandedElts, |
| 1672 | UndefElts2, Depth+1); |
| 1673 | if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; } |
| 1674 | |
| 1675 | // Output elements are undefined if both are undefined. Consider things |
| 1676 | // like undef&0. The result is known zero, not undef. |
| 1677 | UndefElts &= UndefElts2; |
| 1678 | break; |
| 1679 | |
| 1680 | case Instruction::Call: { |
| 1681 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(I); |
| 1682 | if (!II) break; |
| 1683 | switch (II->getIntrinsicID()) { |
| 1684 | default: break; |
| 1685 | |
| 1686 | // Binary vector operations that work column-wise. A dest element is a |
| 1687 | // function of the corresponding input elements from the two inputs. |
| 1688 | case Intrinsic::x86_sse_sub_ss: |
| 1689 | case Intrinsic::x86_sse_mul_ss: |
| 1690 | case Intrinsic::x86_sse_min_ss: |
| 1691 | case Intrinsic::x86_sse_max_ss: |
| 1692 | case Intrinsic::x86_sse2_sub_sd: |
| 1693 | case Intrinsic::x86_sse2_mul_sd: |
| 1694 | case Intrinsic::x86_sse2_min_sd: |
| 1695 | case Intrinsic::x86_sse2_max_sd: |
| 1696 | TmpV = SimplifyDemandedVectorElts(II->getOperand(1), DemandedElts, |
| 1697 | UndefElts, Depth+1); |
| 1698 | if (TmpV) { II->setOperand(1, TmpV); MadeChange = true; } |
| 1699 | TmpV = SimplifyDemandedVectorElts(II->getOperand(2), DemandedElts, |
| 1700 | UndefElts2, Depth+1); |
| 1701 | if (TmpV) { II->setOperand(2, TmpV); MadeChange = true; } |
| 1702 | |
| 1703 | // If only the low elt is demanded and this is a scalarizable intrinsic, |
| 1704 | // scalarize it now. |
| 1705 | if (DemandedElts == 1) { |
| 1706 | switch (II->getIntrinsicID()) { |
| 1707 | default: break; |
| 1708 | case Intrinsic::x86_sse_sub_ss: |
| 1709 | case Intrinsic::x86_sse_mul_ss: |
| 1710 | case Intrinsic::x86_sse2_sub_sd: |
| 1711 | case Intrinsic::x86_sse2_mul_sd: |
| 1712 | // TODO: Lower MIN/MAX/ABS/etc |
| 1713 | Value *LHS = II->getOperand(1); |
| 1714 | Value *RHS = II->getOperand(2); |
| 1715 | // Extract the element as scalars. |
| 1716 | LHS = InsertNewInstBefore(new ExtractElementInst(LHS, 0U,"tmp"), *II); |
| 1717 | RHS = InsertNewInstBefore(new ExtractElementInst(RHS, 0U,"tmp"), *II); |
| 1718 | |
| 1719 | switch (II->getIntrinsicID()) { |
| 1720 | default: assert(0 && "Case stmts out of sync!"); |
| 1721 | case Intrinsic::x86_sse_sub_ss: |
| 1722 | case Intrinsic::x86_sse2_sub_sd: |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1723 | TmpV = InsertNewInstBefore(BinaryOperator::CreateSub(LHS, RHS, |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1724 | II->getName()), *II); |
| 1725 | break; |
| 1726 | case Intrinsic::x86_sse_mul_ss: |
| 1727 | case Intrinsic::x86_sse2_mul_sd: |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1728 | TmpV = InsertNewInstBefore(BinaryOperator::CreateMul(LHS, RHS, |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1729 | II->getName()), *II); |
| 1730 | break; |
| 1731 | } |
| 1732 | |
| 1733 | Instruction *New = |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 1734 | InsertElementInst::Create(UndefValue::get(II->getType()), TmpV, 0U, |
| 1735 | II->getName()); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1736 | InsertNewInstBefore(New, *II); |
| 1737 | AddSoonDeadInstToWorklist(*II, 0); |
| 1738 | return New; |
| 1739 | } |
| 1740 | } |
| 1741 | |
| 1742 | // Output elements are undefined if both are undefined. Consider things |
| 1743 | // like undef&0. The result is known zero, not undef. |
| 1744 | UndefElts &= UndefElts2; |
| 1745 | break; |
| 1746 | } |
| 1747 | break; |
| 1748 | } |
| 1749 | } |
| 1750 | return MadeChange ? I : 0; |
| 1751 | } |
| 1752 | |
Dan Gohman | 45b4e48 | 2008-05-19 22:14:15 +0000 | [diff] [blame] | 1753 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1754 | /// AssociativeOpt - Perform an optimization on an associative operator. This |
| 1755 | /// function is designed to check a chain of associative operators for a |
| 1756 | /// potential to apply a certain optimization. Since the optimization may be |
| 1757 | /// applicable if the expression was reassociated, this checks the chain, then |
| 1758 | /// reassociates the expression as necessary to expose the optimization |
| 1759 | /// opportunity. This makes use of a special Functor, which must define |
| 1760 | /// 'shouldApply' and 'apply' methods. |
| 1761 | /// |
| 1762 | template<typename Functor> |
Dan Gohman | 76d402b | 2008-05-20 01:14:05 +0000 | [diff] [blame] | 1763 | static Instruction *AssociativeOpt(BinaryOperator &Root, const Functor &F) { |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1764 | unsigned Opcode = Root.getOpcode(); |
| 1765 | Value *LHS = Root.getOperand(0); |
| 1766 | |
| 1767 | // Quick check, see if the immediate LHS matches... |
| 1768 | if (F.shouldApply(LHS)) |
| 1769 | return F.apply(Root); |
| 1770 | |
| 1771 | // Otherwise, if the LHS is not of the same opcode as the root, return. |
| 1772 | Instruction *LHSI = dyn_cast<Instruction>(LHS); |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 1773 | while (LHSI && LHSI->getOpcode() == Opcode && LHSI->hasOneUse()) { |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1774 | // Should we apply this transform to the RHS? |
| 1775 | bool ShouldApply = F.shouldApply(LHSI->getOperand(1)); |
| 1776 | |
| 1777 | // If not to the RHS, check to see if we should apply to the LHS... |
| 1778 | if (!ShouldApply && F.shouldApply(LHSI->getOperand(0))) { |
| 1779 | cast<BinaryOperator>(LHSI)->swapOperands(); // Make the LHS the RHS |
| 1780 | ShouldApply = true; |
| 1781 | } |
| 1782 | |
| 1783 | // If the functor wants to apply the optimization to the RHS of LHSI, |
| 1784 | // reassociate the expression from ((? op A) op B) to (? op (A op B)) |
| 1785 | if (ShouldApply) { |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1786 | // Now all of the instructions are in the current basic block, go ahead |
| 1787 | // and perform the reassociation. |
| 1788 | Instruction *TmpLHSI = cast<Instruction>(Root.getOperand(0)); |
| 1789 | |
| 1790 | // First move the selected RHS to the LHS of the root... |
| 1791 | Root.setOperand(0, LHSI->getOperand(1)); |
| 1792 | |
| 1793 | // Make what used to be the LHS of the root be the user of the root... |
| 1794 | Value *ExtraOperand = TmpLHSI->getOperand(1); |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1795 | if (&Root == TmpLHSI) { |
Chris Lattner | 15a76c0 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 1796 | Root.replaceAllUsesWith(Constant::getNullValue(TmpLHSI->getType())); |
| 1797 | return 0; |
| 1798 | } |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1799 | Root.replaceAllUsesWith(TmpLHSI); // Users now use TmpLHSI |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1800 | TmpLHSI->setOperand(1, &Root); // TmpLHSI now uses the root |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1801 | BasicBlock::iterator ARI = &Root; ++ARI; |
Dan Gohman | d02d917 | 2008-06-19 17:47:47 +0000 | [diff] [blame] | 1802 | TmpLHSI->moveBefore(ARI); // Move TmpLHSI to after Root |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1803 | ARI = Root; |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1804 | |
| 1805 | // Now propagate the ExtraOperand down the chain of instructions until we |
| 1806 | // get to LHSI. |
| 1807 | while (TmpLHSI != LHSI) { |
| 1808 | Instruction *NextLHSI = cast<Instruction>(TmpLHSI->getOperand(0)); |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1809 | // Move the instruction to immediately before the chain we are |
| 1810 | // constructing to avoid breaking dominance properties. |
Dan Gohman | d02d917 | 2008-06-19 17:47:47 +0000 | [diff] [blame] | 1811 | NextLHSI->moveBefore(ARI); |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1812 | ARI = NextLHSI; |
| 1813 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1814 | Value *NextOp = NextLHSI->getOperand(1); |
| 1815 | NextLHSI->setOperand(1, ExtraOperand); |
| 1816 | TmpLHSI = NextLHSI; |
| 1817 | ExtraOperand = NextOp; |
| 1818 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1819 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1820 | // Now that the instructions are reassociated, have the functor perform |
| 1821 | // the transformation... |
| 1822 | return F.apply(Root); |
| 1823 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1824 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1825 | LHSI = dyn_cast<Instruction>(LHSI->getOperand(0)); |
| 1826 | } |
| 1827 | return 0; |
| 1828 | } |
| 1829 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 1830 | namespace { |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1831 | |
Nick Lewycky | 02d639f | 2008-05-23 04:34:58 +0000 | [diff] [blame] | 1832 | // AddRHS - Implements: X + X --> X << 1 |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1833 | struct AddRHS { |
| 1834 | Value *RHS; |
| 1835 | AddRHS(Value *rhs) : RHS(rhs) {} |
| 1836 | bool shouldApply(Value *LHS) const { return LHS == RHS; } |
| 1837 | Instruction *apply(BinaryOperator &Add) const { |
Nick Lewycky | 02d639f | 2008-05-23 04:34:58 +0000 | [diff] [blame] | 1838 | return BinaryOperator::CreateShl(Add.getOperand(0), |
| 1839 | ConstantInt::get(Add.getType(), 1)); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1840 | } |
| 1841 | }; |
| 1842 | |
| 1843 | // AddMaskingAnd - Implements (A & C1)+(B & C2) --> (A & C1)|(B & C2) |
| 1844 | // iff C1&C2 == 0 |
| 1845 | struct AddMaskingAnd { |
| 1846 | Constant *C2; |
| 1847 | AddMaskingAnd(Constant *c) : C2(c) {} |
| 1848 | bool shouldApply(Value *LHS) const { |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1849 | ConstantInt *C1; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1850 | return match(LHS, m_And(m_Value(), m_ConstantInt(C1))) && |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1851 | ConstantExpr::getAnd(C1, C2)->isNullValue(); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1852 | } |
| 1853 | Instruction *apply(BinaryOperator &Add) const { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1854 | return BinaryOperator::CreateOr(Add.getOperand(0), Add.getOperand(1)); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1855 | } |
| 1856 | }; |
| 1857 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 1858 | } |
| 1859 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1860 | static Value *FoldOperationIntoSelectOperand(Instruction &I, Value *SO, |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1861 | InstCombiner *IC) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1862 | if (CastInst *CI = dyn_cast<CastInst>(&I)) { |
Eli Friedman | d1fd1da | 2008-11-30 21:09:11 +0000 | [diff] [blame] | 1863 | return IC->InsertCastBefore(CI->getOpcode(), SO, I.getType(), I); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1864 | } |
| 1865 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1866 | // Figure out if the constant is the left or the right argument. |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1867 | bool ConstIsRHS = isa<Constant>(I.getOperand(1)); |
| 1868 | Constant *ConstOperand = cast<Constant>(I.getOperand(ConstIsRHS)); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1869 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1870 | if (Constant *SOC = dyn_cast<Constant>(SO)) { |
| 1871 | if (ConstIsRHS) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1872 | return ConstantExpr::get(I.getOpcode(), SOC, ConstOperand); |
| 1873 | return ConstantExpr::get(I.getOpcode(), ConstOperand, SOC); |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1874 | } |
| 1875 | |
| 1876 | Value *Op0 = SO, *Op1 = ConstOperand; |
| 1877 | if (!ConstIsRHS) |
| 1878 | std::swap(Op0, Op1); |
| 1879 | Instruction *New; |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1880 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1881 | New = BinaryOperator::Create(BO->getOpcode(), Op0, Op1,SO->getName()+".op"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1882 | else if (CmpInst *CI = dyn_cast<CmpInst>(&I)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1883 | New = CmpInst::Create(CI->getOpcode(), CI->getPredicate(), Op0, Op1, |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1884 | SO->getName()+".cmp"); |
Chris Lattner | 326c0f3 | 2004-04-10 19:15:56 +0000 | [diff] [blame] | 1885 | else { |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1886 | assert(0 && "Unknown binary instruction type!"); |
Chris Lattner | 326c0f3 | 2004-04-10 19:15:56 +0000 | [diff] [blame] | 1887 | abort(); |
| 1888 | } |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1889 | return IC->InsertNewInstBefore(New, I); |
| 1890 | } |
| 1891 | |
| 1892 | // FoldOpIntoSelect - Given an instruction with a select as one operand and a |
| 1893 | // constant as the other operand, try to fold the binary operator into the |
| 1894 | // select arguments. This also works for Cast instructions, which obviously do |
| 1895 | // not have a second operand. |
| 1896 | static Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI, |
| 1897 | InstCombiner *IC) { |
| 1898 | // Don't modify shared select instructions |
| 1899 | if (!SI->hasOneUse()) return 0; |
| 1900 | Value *TV = SI->getOperand(1); |
| 1901 | Value *FV = SI->getOperand(2); |
| 1902 | |
| 1903 | if (isa<Constant>(TV) || isa<Constant>(FV)) { |
Chris Lattner | 956db27 | 2005-04-21 05:43:13 +0000 | [diff] [blame] | 1904 | // Bool selects with constant operands can be folded to logical ops. |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 1905 | if (SI->getType() == Type::Int1Ty) return 0; |
Chris Lattner | 956db27 | 2005-04-21 05:43:13 +0000 | [diff] [blame] | 1906 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1907 | Value *SelectTrueVal = FoldOperationIntoSelectOperand(Op, TV, IC); |
| 1908 | Value *SelectFalseVal = FoldOperationIntoSelectOperand(Op, FV, IC); |
| 1909 | |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 1910 | return SelectInst::Create(SI->getCondition(), SelectTrueVal, |
| 1911 | SelectFalseVal); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1912 | } |
| 1913 | return 0; |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1914 | } |
| 1915 | |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1916 | |
| 1917 | /// FoldOpIntoPhi - Given a binary operator or cast instruction which has a PHI |
| 1918 | /// node as operand #0, see if we can fold the instruction into the PHI (which |
| 1919 | /// is only possible if all operands to the PHI are constants). |
| 1920 | Instruction *InstCombiner::FoldOpIntoPhi(Instruction &I) { |
| 1921 | PHINode *PN = cast<PHINode>(I.getOperand(0)); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 1922 | unsigned NumPHIValues = PN->getNumIncomingValues(); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1923 | if (!PN->hasOneUse() || NumPHIValues == 0) return 0; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1924 | |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1925 | // Check to see if all of the operands of the PHI are constants. If there is |
| 1926 | // 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] | 1927 | // or if *it* is a PHI, bail out. |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1928 | BasicBlock *NonConstBB = 0; |
| 1929 | for (unsigned i = 0; i != NumPHIValues; ++i) |
| 1930 | if (!isa<Constant>(PN->getIncomingValue(i))) { |
| 1931 | if (NonConstBB) return 0; // More than one non-const value. |
Chris Lattner | b303668 | 2007-02-24 01:03:45 +0000 | [diff] [blame] | 1932 | if (isa<PHINode>(PN->getIncomingValue(i))) return 0; // Itself a phi. |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1933 | NonConstBB = PN->getIncomingBlock(i); |
| 1934 | |
| 1935 | // If the incoming non-constant value is in I's block, we have an infinite |
| 1936 | // loop. |
| 1937 | if (NonConstBB == I.getParent()) |
| 1938 | return 0; |
| 1939 | } |
| 1940 | |
| 1941 | // If there is exactly one non-constant value, we can insert a copy of the |
| 1942 | // operation in that block. However, if this is a critical edge, we would be |
| 1943 | // inserting the computation one some other paths (e.g. inside a loop). Only |
| 1944 | // do this if the pred block is unconditionally branching into the phi block. |
| 1945 | if (NonConstBB) { |
| 1946 | BranchInst *BI = dyn_cast<BranchInst>(NonConstBB->getTerminator()); |
| 1947 | if (!BI || !BI->isUnconditional()) return 0; |
| 1948 | } |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1949 | |
| 1950 | // Okay, we can do the transformation: create the new PHI node. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 1951 | PHINode *NewPN = PHINode::Create(I.getType(), ""); |
Chris Lattner | 5551706 | 2005-01-29 00:39:08 +0000 | [diff] [blame] | 1952 | NewPN->reserveOperandSpace(PN->getNumOperands()/2); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1953 | InsertNewInstBefore(NewPN, *PN); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 1954 | NewPN->takeName(PN); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1955 | |
| 1956 | // Next, add all of the operands to the PHI. |
| 1957 | if (I.getNumOperands() == 2) { |
| 1958 | Constant *C = cast<Constant>(I.getOperand(1)); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 1959 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
Chris Lattner | a9ff5eb | 2007-08-05 08:47:58 +0000 | [diff] [blame] | 1960 | Value *InV = 0; |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1961 | if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1962 | if (CmpInst *CI = dyn_cast<CmpInst>(&I)) |
| 1963 | InV = ConstantExpr::getCompare(CI->getPredicate(), InC, C); |
| 1964 | else |
| 1965 | InV = ConstantExpr::get(I.getOpcode(), InC, C); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1966 | } else { |
| 1967 | assert(PN->getIncomingBlock(i) == NonConstBB); |
| 1968 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1969 | InV = BinaryOperator::Create(BO->getOpcode(), |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1970 | PN->getIncomingValue(i), C, "phitmp", |
| 1971 | NonConstBB->getTerminator()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1972 | else if (CmpInst *CI = dyn_cast<CmpInst>(&I)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1973 | InV = CmpInst::Create(CI->getOpcode(), |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1974 | CI->getPredicate(), |
| 1975 | PN->getIncomingValue(i), C, "phitmp", |
| 1976 | NonConstBB->getTerminator()); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1977 | else |
| 1978 | assert(0 && "Unknown binop!"); |
| 1979 | |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 1980 | AddToWorkList(cast<Instruction>(InV)); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1981 | } |
| 1982 | NewPN->addIncoming(InV, PN->getIncomingBlock(i)); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1983 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1984 | } else { |
| 1985 | CastInst *CI = cast<CastInst>(&I); |
| 1986 | const Type *RetTy = CI->getType(); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 1987 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1988 | Value *InV; |
| 1989 | if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1990 | InV = ConstantExpr::getCast(CI->getOpcode(), InC, RetTy); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1991 | } else { |
| 1992 | assert(PN->getIncomingBlock(i) == NonConstBB); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1993 | InV = CastInst::Create(CI->getOpcode(), PN->getIncomingValue(i), |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1994 | I.getType(), "phitmp", |
| 1995 | NonConstBB->getTerminator()); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 1996 | AddToWorkList(cast<Instruction>(InV)); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1997 | } |
| 1998 | NewPN->addIncoming(InV, PN->getIncomingBlock(i)); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1999 | } |
| 2000 | } |
| 2001 | return ReplaceInstUsesWith(I, NewPN); |
| 2002 | } |
| 2003 | |
Chris Lattner | 2454a2e | 2008-01-29 06:52:45 +0000 | [diff] [blame] | 2004 | |
Chris Lattner | 3d28b1b | 2008-05-20 05:46:13 +0000 | [diff] [blame] | 2005 | /// WillNotOverflowSignedAdd - Return true if we can prove that: |
| 2006 | /// (sext (add LHS, RHS)) === (add (sext LHS), (sext RHS)) |
| 2007 | /// This basically requires proving that the add in the original type would not |
| 2008 | /// overflow to change the sign bit or have a carry out. |
| 2009 | bool InstCombiner::WillNotOverflowSignedAdd(Value *LHS, Value *RHS) { |
| 2010 | // There are different heuristics we can use for this. Here are some simple |
| 2011 | // ones. |
| 2012 | |
| 2013 | // Add has the property that adding any two 2's complement numbers can only |
| 2014 | // have one carry bit which can change a sign. As such, if LHS and RHS each |
| 2015 | // have at least two sign bits, we know that the addition of the two values will |
| 2016 | // sign extend fine. |
| 2017 | if (ComputeNumSignBits(LHS) > 1 && ComputeNumSignBits(RHS) > 1) |
| 2018 | return true; |
| 2019 | |
| 2020 | |
| 2021 | // If one of the operands only has one non-zero bit, and if the other operand |
| 2022 | // has a known-zero bit in a more significant place than it (not including the |
| 2023 | // sign bit) the ripple may go up to and fill the zero, but won't change the |
| 2024 | // sign. For example, (X & ~4) + 1. |
| 2025 | |
| 2026 | // TODO: Implement. |
| 2027 | |
| 2028 | return false; |
| 2029 | } |
| 2030 | |
Chris Lattner | 2454a2e | 2008-01-29 06:52:45 +0000 | [diff] [blame] | 2031 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2032 | Instruction *InstCombiner::visitAdd(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 2033 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2034 | Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 2035 | |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 2036 | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2037 | // X + undef -> undef |
| 2038 | if (isa<UndefValue>(RHS)) |
| 2039 | return ReplaceInstUsesWith(I, RHS); |
| 2040 | |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 2041 | // X + 0 --> X |
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 2042 | if (!I.getType()->isFPOrFPVector()) { // NOTE: -0 + +0 = +0. |
Chris Lattner | 5e678e0 | 2005-10-17 17:56:38 +0000 | [diff] [blame] | 2043 | if (RHSC->isNullValue()) |
| 2044 | return ReplaceInstUsesWith(I, LHS); |
Chris Lattner | 8532cf6 | 2005-10-17 20:18:38 +0000 | [diff] [blame] | 2045 | } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) { |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 2046 | if (CFP->isExactlyValue(ConstantFP::getNegativeZero |
| 2047 | (I.getType())->getValueAPF())) |
Chris Lattner | 8532cf6 | 2005-10-17 20:18:38 +0000 | [diff] [blame] | 2048 | return ReplaceInstUsesWith(I, LHS); |
Chris Lattner | 5e678e0 | 2005-10-17 17:56:38 +0000 | [diff] [blame] | 2049 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2050 | |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 2051 | if (ConstantInt *CI = dyn_cast<ConstantInt>(RHSC)) { |
Chris Lattner | b4a2f05 | 2006-11-09 05:12:27 +0000 | [diff] [blame] | 2052 | // X + (signbit) --> X ^ signbit |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2053 | const APInt& Val = CI->getValue(); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 2054 | uint32_t BitWidth = Val.getBitWidth(); |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 2055 | if (Val == APInt::getSignBit(BitWidth)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2056 | return BinaryOperator::CreateXor(LHS, RHS); |
Chris Lattner | b4a2f05 | 2006-11-09 05:12:27 +0000 | [diff] [blame] | 2057 | |
| 2058 | // See if SimplifyDemandedBits can simplify this. This handles stuff like |
| 2059 | // (X & 254)+1 -> (X&254)|1 |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 2060 | if (!isa<VectorType>(I.getType()) && SimplifyDemandedInstructionBits(I)) |
| 2061 | return &I; |
Dan Gohman | 1975d03 | 2008-10-30 20:40:10 +0000 | [diff] [blame] | 2062 | |
| 2063 | // zext(i1) - 1 -> select i1, 0, -1 |
| 2064 | if (ZExtInst *ZI = dyn_cast<ZExtInst>(LHS)) |
| 2065 | if (CI->isAllOnesValue() && |
| 2066 | ZI->getOperand(0)->getType() == Type::Int1Ty) |
| 2067 | return SelectInst::Create(ZI->getOperand(0), |
| 2068 | Constant::getNullValue(I.getType()), |
| 2069 | ConstantInt::getAllOnesValue(I.getType())); |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 2070 | } |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2071 | |
| 2072 | if (isa<PHINode>(LHS)) |
| 2073 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2074 | return NV; |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2075 | |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 2076 | ConstantInt *XorRHS = 0; |
| 2077 | Value *XorLHS = 0; |
Chris Lattner | c5eff44 | 2007-01-30 22:32:46 +0000 | [diff] [blame] | 2078 | if (isa<ConstantInt>(RHSC) && |
| 2079 | match(LHS, m_Xor(m_Value(XorLHS), m_ConstantInt(XorRHS)))) { |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 2080 | uint32_t TySizeBits = I.getType()->getPrimitiveSizeInBits(); |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2081 | const APInt& RHSVal = cast<ConstantInt>(RHSC)->getValue(); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2082 | |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 2083 | uint32_t Size = TySizeBits / 2; |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 2084 | APInt C0080Val(APInt(TySizeBits, 1ULL).shl(Size - 1)); |
| 2085 | APInt CFF80Val(-C0080Val); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2086 | do { |
| 2087 | if (TySizeBits > Size) { |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2088 | // If we have ADD(XOR(AND(X, 0xFF), 0x80), 0xF..F80), it's a sext. |
| 2089 | // 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] | 2090 | if ((RHSVal == CFF80Val && XorRHS->getValue() == C0080Val) || |
| 2091 | (RHSVal == C0080Val && XorRHS->getValue() == CFF80Val)) { |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2092 | // This is a sign extend if the top bits are known zero. |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 2093 | if (!MaskedValueIsZero(XorLHS, |
| 2094 | APInt::getHighBitsSet(TySizeBits, TySizeBits - Size))) |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2095 | 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] | 2096 | break; |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2097 | } |
| 2098 | } |
| 2099 | Size >>= 1; |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 2100 | C0080Val = APIntOps::lshr(C0080Val, Size); |
| 2101 | CFF80Val = APIntOps::ashr(CFF80Val, Size); |
| 2102 | } while (Size >= 1); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2103 | |
Reid Spencer | 35c3885 | 2007-03-28 01:36:16 +0000 | [diff] [blame] | 2104 | // FIXME: This shouldn't be necessary. When the backends can handle types |
Chris Lattner | 0c7a9a0 | 2008-05-19 20:25:04 +0000 | [diff] [blame] | 2105 | // with funny bit widths then this switch statement should be removed. It |
| 2106 | // is just here to get the size of the "middle" type back up to something |
| 2107 | // that the back ends can handle. |
Reid Spencer | 35c3885 | 2007-03-28 01:36:16 +0000 | [diff] [blame] | 2108 | const Type *MiddleType = 0; |
| 2109 | switch (Size) { |
| 2110 | default: break; |
| 2111 | case 32: MiddleType = Type::Int32Ty; break; |
| 2112 | case 16: MiddleType = Type::Int16Ty; break; |
| 2113 | case 8: MiddleType = Type::Int8Ty; break; |
| 2114 | } |
| 2115 | if (MiddleType) { |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 2116 | Instruction *NewTrunc = new TruncInst(XorLHS, MiddleType, "sext"); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2117 | InsertNewInstBefore(NewTrunc, I); |
Reid Spencer | 35c3885 | 2007-03-28 01:36:16 +0000 | [diff] [blame] | 2118 | return new SExtInst(NewTrunc, I.getType(), I.getName()); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2119 | } |
| 2120 | } |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 2121 | } |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 2122 | |
Nick Lewycky | 9419ddb | 2008-05-31 17:59:52 +0000 | [diff] [blame] | 2123 | if (I.getType() == Type::Int1Ty) |
| 2124 | return BinaryOperator::CreateXor(LHS, RHS); |
| 2125 | |
Nick Lewycky | 7d26bd8 | 2008-05-23 04:39:38 +0000 | [diff] [blame] | 2126 | // X + X --> X << 1 |
Nick Lewycky | 9419ddb | 2008-05-31 17:59:52 +0000 | [diff] [blame] | 2127 | if (I.getType()->isInteger()) { |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2128 | if (Instruction *Result = AssociativeOpt(I, AddRHS(RHS))) return Result; |
Chris Lattner | 7edc8c2 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 2129 | |
| 2130 | if (Instruction *RHSI = dyn_cast<Instruction>(RHS)) { |
| 2131 | if (RHSI->getOpcode() == Instruction::Sub) |
| 2132 | if (LHS == RHSI->getOperand(1)) // A + (B - A) --> B |
| 2133 | return ReplaceInstUsesWith(I, RHSI->getOperand(0)); |
| 2134 | } |
| 2135 | if (Instruction *LHSI = dyn_cast<Instruction>(LHS)) { |
| 2136 | if (LHSI->getOpcode() == Instruction::Sub) |
| 2137 | if (RHS == LHSI->getOperand(1)) // (B - A) + A --> B |
| 2138 | return ReplaceInstUsesWith(I, LHSI->getOperand(0)); |
| 2139 | } |
Robert Bocchino | 7169828 | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 2140 | } |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 2141 | |
Chris Lattner | 5c4afb9 | 2002-05-08 22:46:53 +0000 | [diff] [blame] | 2142 | // -A + B --> B - A |
Chris Lattner | dd12f96 | 2008-02-17 21:03:36 +0000 | [diff] [blame] | 2143 | // -A + -B --> -(A + B) |
| 2144 | if (Value *LHSV = dyn_castNegVal(LHS)) { |
Chris Lattner | e10c0b9 | 2008-02-18 17:50:16 +0000 | [diff] [blame] | 2145 | if (LHS->getType()->isIntOrIntVector()) { |
| 2146 | if (Value *RHSV = dyn_castNegVal(RHS)) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2147 | Instruction *NewAdd = BinaryOperator::CreateAdd(LHSV, RHSV, "sum"); |
Chris Lattner | e10c0b9 | 2008-02-18 17:50:16 +0000 | [diff] [blame] | 2148 | InsertNewInstBefore(NewAdd, I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2149 | return BinaryOperator::CreateNeg(NewAdd); |
Chris Lattner | e10c0b9 | 2008-02-18 17:50:16 +0000 | [diff] [blame] | 2150 | } |
Chris Lattner | dd12f96 | 2008-02-17 21:03:36 +0000 | [diff] [blame] | 2151 | } |
| 2152 | |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2153 | return BinaryOperator::CreateSub(RHS, LHSV); |
Chris Lattner | dd12f96 | 2008-02-17 21:03:36 +0000 | [diff] [blame] | 2154 | } |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 2155 | |
| 2156 | // A + -B --> A - B |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 2157 | if (!isa<Constant>(RHS)) |
| 2158 | if (Value *V = dyn_castNegVal(RHS)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2159 | return BinaryOperator::CreateSub(LHS, V); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2160 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2161 | |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2162 | ConstantInt *C2; |
| 2163 | if (Value *X = dyn_castFoldableMul(LHS, C2)) { |
| 2164 | if (X == RHS) // X*C + X --> X * (C+1) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2165 | return BinaryOperator::CreateMul(RHS, AddOne(C2)); |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2166 | |
| 2167 | // X*C1 + X*C2 --> X * (C1+C2) |
| 2168 | ConstantInt *C1; |
| 2169 | if (X == dyn_castFoldableMul(RHS, C1)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2170 | return BinaryOperator::CreateMul(X, Add(C1, C2)); |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2171 | } |
| 2172 | |
| 2173 | // X + X*C --> X * (C+1) |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2174 | if (dyn_castFoldableMul(RHS, C2) == LHS) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2175 | return BinaryOperator::CreateMul(LHS, AddOne(C2)); |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2176 | |
Chris Lattner | e617c9e | 2007-01-05 02:17:46 +0000 | [diff] [blame] | 2177 | // X + ~X --> -1 since ~X = -X-1 |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 2178 | if (dyn_castNotVal(LHS) == RHS || dyn_castNotVal(RHS) == LHS) |
| 2179 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | e617c9e | 2007-01-05 02:17:46 +0000 | [diff] [blame] | 2180 | |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2181 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2182 | // (A & C1)+(B & C2) --> (A & C1)|(B & C2) iff C1&C2 == 0 |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2183 | if (match(RHS, m_And(m_Value(), m_ConstantInt(C2)))) |
Chris Lattner | e617c9e | 2007-01-05 02:17:46 +0000 | [diff] [blame] | 2184 | if (Instruction *R = AssociativeOpt(I, AddMaskingAnd(C2))) |
| 2185 | return R; |
Chris Lattner | 5e0d718 | 2008-05-19 20:01:56 +0000 | [diff] [blame] | 2186 | |
| 2187 | // A+B --> A|B iff A and B have no bits set in common. |
| 2188 | if (const IntegerType *IT = dyn_cast<IntegerType>(I.getType())) { |
| 2189 | APInt Mask = APInt::getAllOnesValue(IT->getBitWidth()); |
| 2190 | APInt LHSKnownOne(IT->getBitWidth(), 0); |
| 2191 | APInt LHSKnownZero(IT->getBitWidth(), 0); |
| 2192 | ComputeMaskedBits(LHS, Mask, LHSKnownZero, LHSKnownOne); |
| 2193 | if (LHSKnownZero != 0) { |
| 2194 | APInt RHSKnownOne(IT->getBitWidth(), 0); |
| 2195 | APInt RHSKnownZero(IT->getBitWidth(), 0); |
| 2196 | ComputeMaskedBits(RHS, Mask, RHSKnownZero, RHSKnownOne); |
| 2197 | |
| 2198 | // No bits in common -> bitwise or. |
Chris Lattner | 9d60ba9 | 2008-05-19 20:03:53 +0000 | [diff] [blame] | 2199 | if ((LHSKnownZero|RHSKnownZero).isAllOnesValue()) |
Chris Lattner | 5e0d718 | 2008-05-19 20:01:56 +0000 | [diff] [blame] | 2200 | return BinaryOperator::CreateOr(LHS, RHS); |
Chris Lattner | 5e0d718 | 2008-05-19 20:01:56 +0000 | [diff] [blame] | 2201 | } |
| 2202 | } |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 2203 | |
Nick Lewycky | b6eabff | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 2204 | // W*X + Y*Z --> W * (X+Z) iff W == Y |
Nick Lewycky | 0c2c3f6 | 2008-02-03 08:19:11 +0000 | [diff] [blame] | 2205 | if (I.getType()->isIntOrIntVector()) { |
Nick Lewycky | b6eabff | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 2206 | Value *W, *X, *Y, *Z; |
| 2207 | if (match(LHS, m_Mul(m_Value(W), m_Value(X))) && |
| 2208 | match(RHS, m_Mul(m_Value(Y), m_Value(Z)))) { |
| 2209 | if (W != Y) { |
| 2210 | if (W == Z) { |
Bill Wendling | 587c01d | 2008-02-26 10:53:30 +0000 | [diff] [blame] | 2211 | std::swap(Y, Z); |
Nick Lewycky | b6eabff | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 2212 | } else if (Y == X) { |
Bill Wendling | 587c01d | 2008-02-26 10:53:30 +0000 | [diff] [blame] | 2213 | std::swap(W, X); |
| 2214 | } else if (X == Z) { |
Nick Lewycky | b6eabff | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 2215 | std::swap(Y, Z); |
| 2216 | std::swap(W, X); |
| 2217 | } |
| 2218 | } |
| 2219 | |
| 2220 | if (W == Y) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2221 | Value *NewAdd = InsertNewInstBefore(BinaryOperator::CreateAdd(X, Z, |
Nick Lewycky | b6eabff | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 2222 | LHS->getName()), I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2223 | return BinaryOperator::CreateMul(W, NewAdd); |
Nick Lewycky | b6eabff | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 2224 | } |
| 2225 | } |
| 2226 | } |
| 2227 | |
Chris Lattner | 6b03205 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 2228 | if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) { |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 2229 | Value *X = 0; |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2230 | 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] | 2231 | return BinaryOperator::CreateSub(SubOne(CRHS), X); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2232 | |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2233 | // (X & FF00) + xx00 -> (X+xx00) & FF00 |
| 2234 | 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] | 2235 | Constant *Anded = And(CRHS, C2); |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2236 | if (Anded == CRHS) { |
| 2237 | // See if all bits from the first bit set in the Add RHS up are included |
| 2238 | // in the mask. First, get the rightmost bit. |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2239 | const APInt& AddRHSV = CRHS->getValue(); |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2240 | |
| 2241 | // 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] | 2242 | APInt AddRHSHighBits(~((AddRHSV & -AddRHSV)-1)); |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2243 | |
| 2244 | // See if the and mask includes all of these bits. |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2245 | APInt AddRHSHighBitsAnd(AddRHSHighBits & C2->getValue()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2246 | |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2247 | if (AddRHSHighBits == AddRHSHighBitsAnd) { |
| 2248 | // Okay, the xform is safe. Insert the new add pronto. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2249 | Value *NewAdd = InsertNewInstBefore(BinaryOperator::CreateAdd(X, CRHS, |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2250 | LHS->getName()), I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2251 | return BinaryOperator::CreateAnd(NewAdd, C2); |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2252 | } |
| 2253 | } |
| 2254 | } |
| 2255 | |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2256 | // Try to fold constant add into select arguments. |
| 2257 | if (SelectInst *SI = dyn_cast<SelectInst>(LHS)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2258 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2259 | return R; |
Chris Lattner | 6b03205 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 2260 | } |
| 2261 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2262 | // add (cast *A to intptrtype) B -> |
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2263 | // cast (GEP (cast *A to sbyte*) B) --> intptrtype |
Andrew Lenharth | 16d7955 | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 2264 | { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2265 | CastInst *CI = dyn_cast<CastInst>(LHS); |
| 2266 | Value *Other = RHS; |
Andrew Lenharth | 16d7955 | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 2267 | if (!CI) { |
| 2268 | CI = dyn_cast<CastInst>(RHS); |
| 2269 | Other = LHS; |
| 2270 | } |
Andrew Lenharth | 4563326 | 2006-09-20 15:37:57 +0000 | [diff] [blame] | 2271 | if (CI && CI->getType()->isSized() && |
Reid Spencer | abaa8ca | 2007-01-08 16:32:00 +0000 | [diff] [blame] | 2272 | (CI->getType()->getPrimitiveSizeInBits() == |
| 2273 | TD->getIntPtrType()->getPrimitiveSizeInBits()) |
Andrew Lenharth | 4563326 | 2006-09-20 15:37:57 +0000 | [diff] [blame] | 2274 | && isa<PointerType>(CI->getOperand(0)->getType())) { |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 2275 | unsigned AS = |
| 2276 | cast<PointerType>(CI->getOperand(0)->getType())->getAddressSpace(); |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 2277 | Value *I2 = InsertBitCastBefore(CI->getOperand(0), |
| 2278 | PointerType::get(Type::Int8Ty, AS), I); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 2279 | I2 = InsertNewInstBefore(GetElementPtrInst::Create(I2, Other, "ctg2"), I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2280 | return new PtrToIntInst(I2, CI->getType()); |
Andrew Lenharth | 16d7955 | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 2281 | } |
| 2282 | } |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2283 | |
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2284 | // add (select X 0 (sub n A)) A --> select X A n |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2285 | { |
| 2286 | SelectInst *SI = dyn_cast<SelectInst>(LHS); |
Chris Lattner | 6046fb7 | 2008-11-16 04:46:19 +0000 | [diff] [blame] | 2287 | Value *A = RHS; |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2288 | if (!SI) { |
| 2289 | SI = dyn_cast<SelectInst>(RHS); |
Chris Lattner | 6046fb7 | 2008-11-16 04:46:19 +0000 | [diff] [blame] | 2290 | A = LHS; |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2291 | } |
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2292 | if (SI && SI->hasOneUse()) { |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2293 | Value *TV = SI->getTrueValue(); |
| 2294 | Value *FV = SI->getFalseValue(); |
Chris Lattner | 6046fb7 | 2008-11-16 04:46:19 +0000 | [diff] [blame] | 2295 | Value *N; |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2296 | |
| 2297 | // Can we fold the add into the argument of the select? |
| 2298 | // We check both true and false select arguments for a matching subtract. |
Chris Lattner | 6046fb7 | 2008-11-16 04:46:19 +0000 | [diff] [blame] | 2299 | if (match(FV, m_Zero()) && match(TV, m_Sub(m_Value(N), m_Specific(A)))) |
| 2300 | // Fold the add into the true select value. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 2301 | return SelectInst::Create(SI->getCondition(), N, A); |
Chris Lattner | 6046fb7 | 2008-11-16 04:46:19 +0000 | [diff] [blame] | 2302 | if (match(TV, m_Zero()) && match(FV, m_Sub(m_Value(N), m_Specific(A)))) |
| 2303 | // Fold the add into the false select value. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 2304 | return SelectInst::Create(SI->getCondition(), A, N); |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2305 | } |
| 2306 | } |
Chris Lattner | 2454a2e | 2008-01-29 06:52:45 +0000 | [diff] [blame] | 2307 | |
| 2308 | // Check for X+0.0. Simplify it to X if we know X is not -0.0. |
| 2309 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHS)) |
| 2310 | if (CFP->getValueAPF().isPosZero() && CannotBeNegativeZero(LHS)) |
| 2311 | return ReplaceInstUsesWith(I, LHS); |
Andrew Lenharth | 16d7955 | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 2312 | |
Chris Lattner | 3d28b1b | 2008-05-20 05:46:13 +0000 | [diff] [blame] | 2313 | // Check for (add (sext x), y), see if we can merge this into an |
| 2314 | // integer add followed by a sext. |
| 2315 | if (SExtInst *LHSConv = dyn_cast<SExtInst>(LHS)) { |
| 2316 | // (add (sext x), cst) --> (sext (add x, cst')) |
| 2317 | if (ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS)) { |
| 2318 | Constant *CI = |
| 2319 | ConstantExpr::getTrunc(RHSC, LHSConv->getOperand(0)->getType()); |
| 2320 | if (LHSConv->hasOneUse() && |
| 2321 | ConstantExpr::getSExt(CI, I.getType()) == RHSC && |
| 2322 | WillNotOverflowSignedAdd(LHSConv->getOperand(0), CI)) { |
| 2323 | // Insert the new, smaller add. |
| 2324 | Instruction *NewAdd = BinaryOperator::CreateAdd(LHSConv->getOperand(0), |
| 2325 | CI, "addconv"); |
| 2326 | InsertNewInstBefore(NewAdd, I); |
| 2327 | return new SExtInst(NewAdd, I.getType()); |
| 2328 | } |
| 2329 | } |
| 2330 | |
| 2331 | // (add (sext x), (sext y)) --> (sext (add int x, y)) |
| 2332 | if (SExtInst *RHSConv = dyn_cast<SExtInst>(RHS)) { |
| 2333 | // Only do this if x/y have the same type, if at last one of them has a |
| 2334 | // single use (so we don't increase the number of sexts), and if the |
| 2335 | // integer add will not overflow. |
| 2336 | if (LHSConv->getOperand(0)->getType()==RHSConv->getOperand(0)->getType()&& |
| 2337 | (LHSConv->hasOneUse() || RHSConv->hasOneUse()) && |
| 2338 | WillNotOverflowSignedAdd(LHSConv->getOperand(0), |
| 2339 | RHSConv->getOperand(0))) { |
| 2340 | // Insert the new integer add. |
| 2341 | Instruction *NewAdd = BinaryOperator::CreateAdd(LHSConv->getOperand(0), |
| 2342 | RHSConv->getOperand(0), |
| 2343 | "addconv"); |
| 2344 | InsertNewInstBefore(NewAdd, I); |
| 2345 | return new SExtInst(NewAdd, I.getType()); |
| 2346 | } |
| 2347 | } |
| 2348 | } |
| 2349 | |
| 2350 | // Check for (add double (sitofp x), y), see if we can merge this into an |
| 2351 | // integer add followed by a promotion. |
| 2352 | if (SIToFPInst *LHSConv = dyn_cast<SIToFPInst>(LHS)) { |
| 2353 | // (add double (sitofp x), fpcst) --> (sitofp (add int x, intcst)) |
| 2354 | // ... if the constant fits in the integer value. This is useful for things |
| 2355 | // like (double)(x & 1234) + 4.0 -> (double)((X & 1234)+4) which no longer |
| 2356 | // requires a constant pool load, and generally allows the add to be better |
| 2357 | // instcombined. |
| 2358 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHS)) { |
| 2359 | Constant *CI = |
| 2360 | ConstantExpr::getFPToSI(CFP, LHSConv->getOperand(0)->getType()); |
| 2361 | if (LHSConv->hasOneUse() && |
| 2362 | ConstantExpr::getSIToFP(CI, I.getType()) == CFP && |
| 2363 | WillNotOverflowSignedAdd(LHSConv->getOperand(0), CI)) { |
| 2364 | // Insert the new integer add. |
| 2365 | Instruction *NewAdd = BinaryOperator::CreateAdd(LHSConv->getOperand(0), |
| 2366 | CI, "addconv"); |
| 2367 | InsertNewInstBefore(NewAdd, I); |
| 2368 | return new SIToFPInst(NewAdd, I.getType()); |
| 2369 | } |
| 2370 | } |
| 2371 | |
| 2372 | // (add double (sitofp x), (sitofp y)) --> (sitofp (add int x, y)) |
| 2373 | if (SIToFPInst *RHSConv = dyn_cast<SIToFPInst>(RHS)) { |
| 2374 | // Only do this if x/y have the same type, if at last one of them has a |
| 2375 | // single use (so we don't increase the number of int->fp conversions), |
| 2376 | // and if the integer add will not overflow. |
| 2377 | if (LHSConv->getOperand(0)->getType()==RHSConv->getOperand(0)->getType()&& |
| 2378 | (LHSConv->hasOneUse() || RHSConv->hasOneUse()) && |
| 2379 | WillNotOverflowSignedAdd(LHSConv->getOperand(0), |
| 2380 | RHSConv->getOperand(0))) { |
| 2381 | // Insert the new integer add. |
| 2382 | Instruction *NewAdd = BinaryOperator::CreateAdd(LHSConv->getOperand(0), |
| 2383 | RHSConv->getOperand(0), |
| 2384 | "addconv"); |
| 2385 | InsertNewInstBefore(NewAdd, I); |
| 2386 | return new SIToFPInst(NewAdd, I.getType()); |
| 2387 | } |
| 2388 | } |
| 2389 | } |
| 2390 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2391 | return Changed ? &I : 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2392 | } |
| 2393 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2394 | Instruction *InstCombiner::visitSub(BinaryOperator &I) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2395 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2396 | |
Chris Lattner | d137ab4 | 2008-07-17 06:07:20 +0000 | [diff] [blame] | 2397 | if (Op0 == Op1 && // sub X, X -> 0 |
| 2398 | !I.getType()->isFPOrFPVector()) |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2399 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2400 | |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2401 | // If this is a 'B = x-(-A)', change to B = x+A... |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 2402 | if (Value *V = dyn_castNegVal(Op1)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2403 | return BinaryOperator::CreateAdd(Op0, V); |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 2404 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2405 | if (isa<UndefValue>(Op0)) |
| 2406 | return ReplaceInstUsesWith(I, Op0); // undef - X -> undef |
| 2407 | if (isa<UndefValue>(Op1)) |
| 2408 | return ReplaceInstUsesWith(I, Op1); // X - undef -> undef |
| 2409 | |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 2410 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op0)) { |
| 2411 | // Replace (-1 - A) with (~A)... |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2412 | if (C->isAllOnesValue()) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2413 | return BinaryOperator::CreateNot(Op1); |
Chris Lattner | 4037171 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 2414 | |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 2415 | // C - ~X == X + (1+C) |
Reid Spencer | 4b828e6 | 2005-06-18 17:37:34 +0000 | [diff] [blame] | 2416 | Value *X = 0; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2417 | if (match(Op1, m_Not(m_Value(X)))) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2418 | return BinaryOperator::CreateAdd(X, AddOne(C)); |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2419 | |
Chris Lattner | 76b7a06 | 2007-01-15 07:02:54 +0000 | [diff] [blame] | 2420 | // -(X >>u 31) -> (X >>s 31) |
| 2421 | // -(X >>s 31) -> (X >>u 31) |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 2422 | if (C->isZero()) { |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 2423 | if (BinaryOperator *SI = dyn_cast<BinaryOperator>(Op1)) { |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2424 | if (SI->getOpcode() == Instruction::LShr) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2425 | if (ConstantInt *CU = dyn_cast<ConstantInt>(SI->getOperand(1))) { |
Chris Lattner | 9c29067 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 2426 | // 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] | 2427 | if (CU->getLimitedValue(SI->getType()->getPrimitiveSizeInBits()) == |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2428 | SI->getType()->getPrimitiveSizeInBits()-1) { |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2429 | // Ok, the transformation is safe. Insert AShr. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2430 | return BinaryOperator::Create(Instruction::AShr, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2431 | SI->getOperand(0), CU, SI->getName()); |
Chris Lattner | 9c29067 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 2432 | } |
| 2433 | } |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2434 | } |
| 2435 | else if (SI->getOpcode() == Instruction::AShr) { |
| 2436 | if (ConstantInt *CU = dyn_cast<ConstantInt>(SI->getOperand(1))) { |
| 2437 | // 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] | 2438 | if (CU->getLimitedValue(SI->getType()->getPrimitiveSizeInBits()) == |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2439 | SI->getType()->getPrimitiveSizeInBits()-1) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 2440 | // Ok, the transformation is safe. Insert LShr. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2441 | return BinaryOperator::CreateLShr( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2442 | SI->getOperand(0), CU, SI->getName()); |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2443 | } |
| 2444 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 2445 | } |
| 2446 | } |
Chris Lattner | bfe492b | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 2447 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2448 | |
| 2449 | // Try to fold constant sub into select arguments. |
| 2450 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2451 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2452 | return R; |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 2453 | } |
| 2454 | |
Nick Lewycky | 9419ddb | 2008-05-31 17:59:52 +0000 | [diff] [blame] | 2455 | if (I.getType() == Type::Int1Ty) |
| 2456 | return BinaryOperator::CreateXor(Op0, Op1); |
| 2457 | |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2458 | if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) { |
| 2459 | if (Op1I->getOpcode() == Instruction::Add && |
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 2460 | !Op0->getType()->isFPOrFPVector()) { |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2461 | if (Op1I->getOperand(0) == Op0) // X-(X+Y) == -Y |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2462 | return BinaryOperator::CreateNeg(Op1I->getOperand(1), I.getName()); |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2463 | else if (Op1I->getOperand(1) == Op0) // X-(Y+X) == -Y |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2464 | return BinaryOperator::CreateNeg(Op1I->getOperand(0), I.getName()); |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2465 | else if (ConstantInt *CI1 = dyn_cast<ConstantInt>(I.getOperand(0))) { |
| 2466 | if (ConstantInt *CI2 = dyn_cast<ConstantInt>(Op1I->getOperand(1))) |
| 2467 | // C1-(X+C2) --> (C1-C2)-X |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2468 | return BinaryOperator::CreateSub(Subtract(CI1, CI2), |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2469 | Op1I->getOperand(0)); |
| 2470 | } |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2471 | } |
| 2472 | |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 2473 | if (Op1I->hasOneUse()) { |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2474 | // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression |
| 2475 | // is not used by anyone else... |
| 2476 | // |
Chris Lattner | 0517e72 | 2004-02-02 20:09:56 +0000 | [diff] [blame] | 2477 | if (Op1I->getOpcode() == Instruction::Sub && |
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 2478 | !Op1I->getType()->isFPOrFPVector()) { |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2479 | // Swap the two operands of the subexpr... |
| 2480 | Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1); |
| 2481 | Op1I->setOperand(0, IIOp1); |
| 2482 | Op1I->setOperand(1, IIOp0); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2483 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2484 | // Create the new top level add instruction... |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2485 | return BinaryOperator::CreateAdd(Op0, Op1); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2486 | } |
| 2487 | |
| 2488 | // Replace (A - (A & B)) with (A & ~B) if this is the only use of (A&B)... |
| 2489 | // |
| 2490 | if (Op1I->getOpcode() == Instruction::And && |
| 2491 | (Op1I->getOperand(0) == Op0 || Op1I->getOperand(1) == Op0)) { |
| 2492 | Value *OtherOp = Op1I->getOperand(Op1I->getOperand(0) == Op0); |
| 2493 | |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 2494 | Value *NewNot = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2495 | InsertNewInstBefore(BinaryOperator::CreateNot(OtherOp, "B.not"), I); |
| 2496 | return BinaryOperator::CreateAnd(Op0, NewNot); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2497 | } |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2498 | |
Reid Spencer | ac5209e | 2006-10-16 23:08:08 +0000 | [diff] [blame] | 2499 | // 0 - (X sdiv C) -> (X sdiv -C) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2500 | if (Op1I->getOpcode() == Instruction::SDiv) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2501 | if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0)) |
Zhou Sheng | 843f0767 | 2007-04-19 05:39:12 +0000 | [diff] [blame] | 2502 | if (CSI->isZero()) |
Chris Lattner | 91ccc15 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 2503 | if (Constant *DivRHS = dyn_cast<Constant>(Op1I->getOperand(1))) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2504 | return BinaryOperator::CreateSDiv(Op1I->getOperand(0), |
Chris Lattner | 91ccc15 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 2505 | ConstantExpr::getNeg(DivRHS)); |
| 2506 | |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2507 | // X - X*C --> X * (1-C) |
Reid Spencer | 4b828e6 | 2005-06-18 17:37:34 +0000 | [diff] [blame] | 2508 | ConstantInt *C2 = 0; |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2509 | if (dyn_castFoldableMul(Op1I, C2) == Op0) { |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2510 | Constant *CP1 = Subtract(ConstantInt::get(I.getType(), 1), C2); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2511 | return BinaryOperator::CreateMul(Op0, CP1); |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2512 | } |
Chris Lattner | 4037171 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 2513 | } |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2514 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2515 | |
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 2516 | if (!Op0->getType()->isFPOrFPVector()) |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 2517 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) { |
Chris Lattner | 7edc8c2 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 2518 | if (Op0I->getOpcode() == Instruction::Add) { |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 2519 | if (Op0I->getOperand(0) == Op1) // (Y+X)-Y == X |
| 2520 | return ReplaceInstUsesWith(I, Op0I->getOperand(1)); |
| 2521 | else if (Op0I->getOperand(1) == Op1) // (X+Y)-Y == X |
| 2522 | return ReplaceInstUsesWith(I, Op0I->getOperand(0)); |
Chris Lattner | 7edc8c2 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 2523 | } else if (Op0I->getOpcode() == Instruction::Sub) { |
| 2524 | if (Op0I->getOperand(0) == Op1) // (X-Y)-X == -Y |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2525 | return BinaryOperator::CreateNeg(Op0I->getOperand(1), I.getName()); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 2526 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 2527 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2528 | |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2529 | ConstantInt *C1; |
| 2530 | if (Value *X = dyn_castFoldableMul(Op0, C1)) { |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2531 | if (X == Op1) // X*C - X --> X * (C-1) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2532 | return BinaryOperator::CreateMul(Op1, SubOne(C1)); |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2533 | |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2534 | ConstantInt *C2; // X*C1 - X*C2 -> X * (C1-C2) |
| 2535 | if (X == dyn_castFoldableMul(Op1, C2)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2536 | return BinaryOperator::CreateMul(X, Subtract(C1, C2)); |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2537 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2538 | return 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2539 | } |
| 2540 | |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2541 | /// isSignBitCheck - Given an exploded icmp instruction, return true if the |
| 2542 | /// comparison only checks the sign bit. If it only checks the sign bit, set |
| 2543 | /// TrueIfSigned if the result of the comparison is true when the input value is |
| 2544 | /// signed. |
| 2545 | static bool isSignBitCheck(ICmpInst::Predicate pred, ConstantInt *RHS, |
| 2546 | bool &TrueIfSigned) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2547 | switch (pred) { |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2548 | case ICmpInst::ICMP_SLT: // True if LHS s< 0 |
| 2549 | TrueIfSigned = true; |
| 2550 | return RHS->isZero(); |
Chris Lattner | cb7122b | 2007-07-16 04:15:34 +0000 | [diff] [blame] | 2551 | case ICmpInst::ICMP_SLE: // True if LHS s<= RHS and RHS == -1 |
| 2552 | TrueIfSigned = true; |
| 2553 | return RHS->isAllOnesValue(); |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2554 | case ICmpInst::ICMP_SGT: // True if LHS s> -1 |
| 2555 | TrueIfSigned = false; |
| 2556 | return RHS->isAllOnesValue(); |
Chris Lattner | cb7122b | 2007-07-16 04:15:34 +0000 | [diff] [blame] | 2557 | case ICmpInst::ICMP_UGT: |
| 2558 | // True if LHS u> RHS and RHS == high-bit-mask - 1 |
| 2559 | TrueIfSigned = true; |
| 2560 | return RHS->getValue() == |
| 2561 | APInt::getSignedMaxValue(RHS->getType()->getPrimitiveSizeInBits()); |
| 2562 | case ICmpInst::ICMP_UGE: |
| 2563 | // True if LHS u>= RHS and RHS == high-bit-mask (2^7, 2^15, 2^31, etc) |
| 2564 | TrueIfSigned = true; |
Chris Lattner | 833f25d | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 2565 | return RHS->getValue().isSignBit(); |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2566 | default: |
| 2567 | return false; |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2568 | } |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2569 | } |
| 2570 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2571 | Instruction *InstCombiner::visitMul(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 2572 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2573 | Value *Op0 = I.getOperand(0); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2574 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2575 | if (isa<UndefValue>(I.getOperand(1))) // undef * X -> 0 |
| 2576 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2577 | |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2578 | // Simplify mul instructions with a constant RHS... |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2579 | if (Constant *Op1 = dyn_cast<Constant>(I.getOperand(1))) { |
| 2580 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 2581 | |
| 2582 | // ((X << C1)*C2) == (X * (C2 << C1)) |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2583 | if (BinaryOperator *SI = dyn_cast<BinaryOperator>(Op0)) |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 2584 | if (SI->getOpcode() == Instruction::Shl) |
| 2585 | if (Constant *ShOp = dyn_cast<Constant>(SI->getOperand(1))) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2586 | return BinaryOperator::CreateMul(SI->getOperand(0), |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2587 | ConstantExpr::getShl(CI, ShOp)); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2588 | |
Zhou Sheng | 843f0767 | 2007-04-19 05:39:12 +0000 | [diff] [blame] | 2589 | if (CI->isZero()) |
Chris Lattner | 515c97c | 2003-09-11 22:24:54 +0000 | [diff] [blame] | 2590 | return ReplaceInstUsesWith(I, Op1); // X * 0 == 0 |
| 2591 | if (CI->equalsInt(1)) // X * 1 == X |
| 2592 | return ReplaceInstUsesWith(I, Op0); |
| 2593 | if (CI->isAllOnesValue()) // X * -1 == 0 - X |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2594 | return BinaryOperator::CreateNeg(Op0, I.getName()); |
Chris Lattner | 6c1ce21 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 2595 | |
Zhou Sheng | 97b52c2 | 2007-03-29 01:57:21 +0000 | [diff] [blame] | 2596 | const APInt& Val = cast<ConstantInt>(CI)->getValue(); |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2597 | if (Val.isPowerOf2()) { // Replace X*(2^C) with X << C |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2598 | return BinaryOperator::CreateShl(Op0, |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2599 | ConstantInt::get(Op0->getType(), Val.logBase2())); |
Chris Lattner | bcd7db5 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 2600 | } |
Robert Bocchino | 7169828 | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 2601 | } else if (ConstantFP *Op1F = dyn_cast<ConstantFP>(Op1)) { |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2602 | if (Op1F->isNullValue()) |
| 2603 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 6c1ce21 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 2604 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2605 | // "In IEEE floating point, x*1 is not equivalent to x for nans. However, |
| 2606 | // ANSI says we can drop signals, so we can do this anyway." (from GCC) |
Chris Lattner | b8cd4d3 | 2008-08-11 22:06:05 +0000 | [diff] [blame] | 2607 | if (Op1F->isExactlyValue(1.0)) |
| 2608 | return ReplaceInstUsesWith(I, Op0); // Eliminate 'mul double %X, 1.0' |
| 2609 | } else if (isa<VectorType>(Op1->getType())) { |
| 2610 | if (isa<ConstantAggregateZero>(Op1)) |
| 2611 | return ReplaceInstUsesWith(I, Op1); |
Nick Lewycky | 895f085 | 2008-11-27 20:21:08 +0000 | [diff] [blame] | 2612 | |
| 2613 | if (ConstantVector *Op1V = dyn_cast<ConstantVector>(Op1)) { |
| 2614 | if (Op1V->isAllOnesValue()) // X * -1 == 0 - X |
| 2615 | return BinaryOperator::CreateNeg(Op0, I.getName()); |
| 2616 | |
| 2617 | // As above, vector X*splat(1.0) -> X in all defined cases. |
| 2618 | if (Constant *Splat = Op1V->getSplatValue()) { |
| 2619 | if (ConstantFP *F = dyn_cast<ConstantFP>(Splat)) |
| 2620 | if (F->isExactlyValue(1.0)) |
| 2621 | return ReplaceInstUsesWith(I, Op0); |
| 2622 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Splat)) |
| 2623 | if (CI->equalsInt(1)) |
| 2624 | return ReplaceInstUsesWith(I, Op0); |
| 2625 | } |
| 2626 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2627 | } |
Chris Lattner | ab51f3f | 2006-03-04 06:04:02 +0000 | [diff] [blame] | 2628 | |
| 2629 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) |
| 2630 | if (Op0I->getOpcode() == Instruction::Add && Op0I->hasOneUse() && |
Chris Lattner | 47c9909 | 2008-05-18 04:11:26 +0000 | [diff] [blame] | 2631 | isa<ConstantInt>(Op0I->getOperand(1)) && isa<ConstantInt>(Op1)) { |
Chris Lattner | ab51f3f | 2006-03-04 06:04:02 +0000 | [diff] [blame] | 2632 | // Canonicalize (X+C1)*C2 -> X*C2+C1*C2. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2633 | Instruction *Add = BinaryOperator::CreateMul(Op0I->getOperand(0), |
Chris Lattner | ab51f3f | 2006-03-04 06:04:02 +0000 | [diff] [blame] | 2634 | Op1, "tmp"); |
| 2635 | InsertNewInstBefore(Add, I); |
| 2636 | Value *C1C2 = ConstantExpr::getMul(Op1, |
| 2637 | cast<Constant>(Op0I->getOperand(1))); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2638 | return BinaryOperator::CreateAdd(Add, C1C2); |
Chris Lattner | ab51f3f | 2006-03-04 06:04:02 +0000 | [diff] [blame] | 2639 | |
| 2640 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2641 | |
| 2642 | // Try to fold constant mul into select arguments. |
| 2643 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2644 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2645 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2646 | |
| 2647 | if (isa<PHINode>(Op0)) |
| 2648 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2649 | return NV; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2650 | } |
| 2651 | |
Chris Lattner | a4f445b | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 2652 | if (Value *Op0v = dyn_castNegVal(Op0)) // -X * -Y = X*Y |
| 2653 | if (Value *Op1v = dyn_castNegVal(I.getOperand(1))) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2654 | return BinaryOperator::CreateMul(Op0v, Op1v); |
Chris Lattner | a4f445b | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 2655 | |
Nick Lewycky | 0c73079 | 2008-11-21 07:33:58 +0000 | [diff] [blame] | 2656 | // (X / Y) * Y = X - (X % Y) |
| 2657 | // (X / Y) * -Y = (X % Y) - X |
| 2658 | { |
| 2659 | Value *Op1 = I.getOperand(1); |
| 2660 | BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0); |
| 2661 | if (!BO || |
| 2662 | (BO->getOpcode() != Instruction::UDiv && |
| 2663 | BO->getOpcode() != Instruction::SDiv)) { |
| 2664 | Op1 = Op0; |
| 2665 | BO = dyn_cast<BinaryOperator>(I.getOperand(1)); |
| 2666 | } |
| 2667 | Value *Neg = dyn_castNegVal(Op1); |
| 2668 | if (BO && BO->hasOneUse() && |
| 2669 | (BO->getOperand(1) == Op1 || BO->getOperand(1) == Neg) && |
| 2670 | (BO->getOpcode() == Instruction::UDiv || |
| 2671 | BO->getOpcode() == Instruction::SDiv)) { |
| 2672 | Value *Op0BO = BO->getOperand(0), *Op1BO = BO->getOperand(1); |
| 2673 | |
| 2674 | Instruction *Rem; |
| 2675 | if (BO->getOpcode() == Instruction::UDiv) |
| 2676 | Rem = BinaryOperator::CreateURem(Op0BO, Op1BO); |
| 2677 | else |
| 2678 | Rem = BinaryOperator::CreateSRem(Op0BO, Op1BO); |
| 2679 | |
| 2680 | InsertNewInstBefore(Rem, I); |
| 2681 | Rem->takeName(BO); |
| 2682 | |
| 2683 | if (Op1BO == Op1) |
| 2684 | return BinaryOperator::CreateSub(Op0BO, Rem); |
| 2685 | else |
| 2686 | return BinaryOperator::CreateSub(Rem, Op0BO); |
| 2687 | } |
| 2688 | } |
| 2689 | |
Nick Lewycky | 9419ddb | 2008-05-31 17:59:52 +0000 | [diff] [blame] | 2690 | if (I.getType() == Type::Int1Ty) |
| 2691 | return BinaryOperator::CreateAnd(Op0, I.getOperand(1)); |
| 2692 | |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2693 | // If one of the operands of the multiply is a cast from a boolean value, then |
| 2694 | // we know the bool is either zero or one, so this is a 'masking' multiply. |
| 2695 | // See if we can simplify things based on how the boolean was originally |
| 2696 | // formed. |
| 2697 | CastInst *BoolCast = 0; |
Nick Lewycky | 9419ddb | 2008-05-31 17:59:52 +0000 | [diff] [blame] | 2698 | if (ZExtInst *CI = dyn_cast<ZExtInst>(Op0)) |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 2699 | if (CI->getOperand(0)->getType() == Type::Int1Ty) |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2700 | BoolCast = CI; |
| 2701 | if (!BoolCast) |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 2702 | if (ZExtInst *CI = dyn_cast<ZExtInst>(I.getOperand(1))) |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 2703 | if (CI->getOperand(0)->getType() == Type::Int1Ty) |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2704 | BoolCast = CI; |
| 2705 | if (BoolCast) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2706 | if (ICmpInst *SCI = dyn_cast<ICmpInst>(BoolCast->getOperand(0))) { |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2707 | Value *SCIOp0 = SCI->getOperand(0), *SCIOp1 = SCI->getOperand(1); |
| 2708 | const Type *SCOpTy = SCIOp0->getType(); |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2709 | bool TIS = false; |
| 2710 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2711 | // 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] | 2712 | // multiply into a shift/and combination. |
| 2713 | if (isa<ConstantInt>(SCIOp1) && |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2714 | isSignBitCheck(SCI->getPredicate(), cast<ConstantInt>(SCIOp1), TIS) && |
| 2715 | TIS) { |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2716 | // Shift the X value right to turn it into "all signbits". |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2717 | Constant *Amt = ConstantInt::get(SCIOp0->getType(), |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 2718 | SCOpTy->getPrimitiveSizeInBits()-1); |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2719 | Value *V = |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2720 | InsertNewInstBefore( |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2721 | BinaryOperator::Create(Instruction::AShr, SCIOp0, Amt, |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2722 | BoolCast->getOperand(0)->getName()+ |
| 2723 | ".mask"), I); |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2724 | |
| 2725 | // If the multiply type is not the same as the source type, sign extend |
| 2726 | // or truncate to the multiply type. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 2727 | if (I.getType() != V->getType()) { |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 2728 | uint32_t SrcBits = V->getType()->getPrimitiveSizeInBits(); |
| 2729 | uint32_t DstBits = I.getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 2730 | Instruction::CastOps opcode = |
| 2731 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2732 | (SrcBits < DstBits ? Instruction::SExt : Instruction::Trunc)); |
| 2733 | V = InsertCastBefore(opcode, V, I.getType(), I); |
| 2734 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2735 | |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2736 | Value *OtherOp = Op0 == BoolCast ? I.getOperand(1) : Op0; |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2737 | return BinaryOperator::CreateAnd(V, OtherOp); |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2738 | } |
| 2739 | } |
| 2740 | } |
| 2741 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2742 | return Changed ? &I : 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2743 | } |
| 2744 | |
Chris Lattner | fdb19e5 | 2008-07-14 00:15:52 +0000 | [diff] [blame] | 2745 | /// SimplifyDivRemOfSelect - Try to fold a divide or remainder of a select |
| 2746 | /// instruction. |
| 2747 | bool InstCombiner::SimplifyDivRemOfSelect(BinaryOperator &I) { |
| 2748 | SelectInst *SI = cast<SelectInst>(I.getOperand(1)); |
| 2749 | |
| 2750 | // div/rem X, (Cond ? 0 : Y) -> div/rem X, Y |
| 2751 | int NonNullOperand = -1; |
| 2752 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(1))) |
| 2753 | if (ST->isNullValue()) |
| 2754 | NonNullOperand = 2; |
| 2755 | // div/rem X, (Cond ? Y : 0) -> div/rem X, Y |
| 2756 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(2))) |
| 2757 | if (ST->isNullValue()) |
| 2758 | NonNullOperand = 1; |
| 2759 | |
| 2760 | if (NonNullOperand == -1) |
| 2761 | return false; |
| 2762 | |
| 2763 | Value *SelectCond = SI->getOperand(0); |
| 2764 | |
| 2765 | // Change the div/rem to use 'Y' instead of the select. |
| 2766 | I.setOperand(1, SI->getOperand(NonNullOperand)); |
| 2767 | |
| 2768 | // Okay, we know we replace the operand of the div/rem with 'Y' with no |
| 2769 | // problem. However, the select, or the condition of the select may have |
| 2770 | // multiple uses. Based on our knowledge that the operand must be non-zero, |
| 2771 | // propagate the known value for the select into other uses of it, and |
| 2772 | // propagate a known value of the condition into its other users. |
| 2773 | |
| 2774 | // If the select and condition only have a single use, don't bother with this, |
| 2775 | // early exit. |
| 2776 | if (SI->use_empty() && SelectCond->hasOneUse()) |
| 2777 | return true; |
| 2778 | |
| 2779 | // Scan the current block backward, looking for other uses of SI. |
| 2780 | BasicBlock::iterator BBI = &I, BBFront = I.getParent()->begin(); |
| 2781 | |
| 2782 | while (BBI != BBFront) { |
| 2783 | --BBI; |
| 2784 | // If we found a call to a function, we can't assume it will return, so |
| 2785 | // information from below it cannot be propagated above it. |
| 2786 | if (isa<CallInst>(BBI) && !isa<IntrinsicInst>(BBI)) |
| 2787 | break; |
| 2788 | |
| 2789 | // Replace uses of the select or its condition with the known values. |
| 2790 | for (Instruction::op_iterator I = BBI->op_begin(), E = BBI->op_end(); |
| 2791 | I != E; ++I) { |
| 2792 | if (*I == SI) { |
| 2793 | *I = SI->getOperand(NonNullOperand); |
| 2794 | AddToWorkList(BBI); |
| 2795 | } else if (*I == SelectCond) { |
| 2796 | *I = NonNullOperand == 1 ? ConstantInt::getTrue() : |
| 2797 | ConstantInt::getFalse(); |
| 2798 | AddToWorkList(BBI); |
| 2799 | } |
| 2800 | } |
| 2801 | |
| 2802 | // If we past the instruction, quit looking for it. |
| 2803 | if (&*BBI == SI) |
| 2804 | SI = 0; |
| 2805 | if (&*BBI == SelectCond) |
| 2806 | SelectCond = 0; |
| 2807 | |
| 2808 | // If we ran out of things to eliminate, break out of the loop. |
| 2809 | if (SelectCond == 0 && SI == 0) |
| 2810 | break; |
| 2811 | |
| 2812 | } |
| 2813 | return true; |
| 2814 | } |
| 2815 | |
| 2816 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2817 | /// This function implements the transforms on div instructions that work |
| 2818 | /// regardless of the kind of div instruction it is (udiv, sdiv, or fdiv). It is |
| 2819 | /// used by the visitors to those instructions. |
| 2820 | /// @brief Transforms common to all three div instructions |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2821 | Instruction *InstCombiner::commonDivTransforms(BinaryOperator &I) { |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2822 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2823 | |
Chris Lattner | 50b2ca4 | 2008-02-19 06:12:18 +0000 | [diff] [blame] | 2824 | // undef / X -> 0 for integer. |
| 2825 | // undef / X -> undef for FP (the undef could be a snan). |
| 2826 | if (isa<UndefValue>(Op0)) { |
| 2827 | if (Op0->getType()->isFPOrFPVector()) |
| 2828 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2829 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | 50b2ca4 | 2008-02-19 06:12:18 +0000 | [diff] [blame] | 2830 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2831 | |
| 2832 | // X / undef -> undef |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2833 | if (isa<UndefValue>(Op1)) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2834 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2835 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2836 | return 0; |
| 2837 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2838 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2839 | /// This function implements the transforms common to both integer division |
| 2840 | /// instructions (udiv and sdiv). It is called by the visitors to those integer |
| 2841 | /// division instructions. |
| 2842 | /// @brief Common integer divide transforms |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2843 | Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2844 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2845 | |
Chris Lattner | b2ae9e3 | 2008-05-16 02:59:42 +0000 | [diff] [blame] | 2846 | // (sdiv X, X) --> 1 (udiv X, X) --> 1 |
Nick Lewycky | 39ac3b5 | 2008-05-23 03:26:47 +0000 | [diff] [blame] | 2847 | if (Op0 == Op1) { |
| 2848 | if (const VectorType *Ty = dyn_cast<VectorType>(I.getType())) { |
| 2849 | ConstantInt *CI = ConstantInt::get(Ty->getElementType(), 1); |
| 2850 | std::vector<Constant*> Elts(Ty->getNumElements(), CI); |
| 2851 | return ReplaceInstUsesWith(I, ConstantVector::get(Elts)); |
| 2852 | } |
| 2853 | |
| 2854 | ConstantInt *CI = ConstantInt::get(I.getType(), 1); |
| 2855 | return ReplaceInstUsesWith(I, CI); |
| 2856 | } |
Chris Lattner | b2ae9e3 | 2008-05-16 02:59:42 +0000 | [diff] [blame] | 2857 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2858 | if (Instruction *Common = commonDivTransforms(I)) |
| 2859 | return Common; |
Chris Lattner | fdb19e5 | 2008-07-14 00:15:52 +0000 | [diff] [blame] | 2860 | |
| 2861 | // Handle cases involving: [su]div X, (select Cond, Y, Z) |
| 2862 | // This does not apply for fdiv. |
| 2863 | if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I)) |
| 2864 | return &I; |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2865 | |
| 2866 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
| 2867 | // div X, 1 == X |
| 2868 | if (RHS->equalsInt(1)) |
| 2869 | return ReplaceInstUsesWith(I, Op0); |
| 2870 | |
| 2871 | // (X / C1) / C2 -> X / (C1*C2) |
| 2872 | if (Instruction *LHS = dyn_cast<Instruction>(Op0)) |
| 2873 | if (Instruction::BinaryOps(LHS->getOpcode()) == I.getOpcode()) |
| 2874 | if (ConstantInt *LHSRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) { |
Nick Lewycky | e0cfecf | 2008-02-18 22:48:05 +0000 | [diff] [blame] | 2875 | if (MultiplyOverflows(RHS, LHSRHS, I.getOpcode()==Instruction::SDiv)) |
| 2876 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2877 | else |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2878 | return BinaryOperator::Create(I.getOpcode(), LHS->getOperand(0), |
Nick Lewycky | e0cfecf | 2008-02-18 22:48:05 +0000 | [diff] [blame] | 2879 | Multiply(RHS, LHSRHS)); |
Chris Lattner | bf70b83 | 2005-04-08 04:03:26 +0000 | [diff] [blame] | 2880 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2881 | |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2882 | if (!RHS->isZero()) { // avoid X udiv 0 |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2883 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 2884 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 2885 | return R; |
| 2886 | if (isa<PHINode>(Op0)) |
| 2887 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2888 | return NV; |
| 2889 | } |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2890 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2891 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2892 | // 0 / X == 0, we don't need to preserve faults! |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2893 | if (ConstantInt *LHS = dyn_cast<ConstantInt>(Op0)) |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2894 | if (LHS->equalsInt(0)) |
| 2895 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2896 | |
Nick Lewycky | 9419ddb | 2008-05-31 17:59:52 +0000 | [diff] [blame] | 2897 | // It can't be division by zero, hence it must be division by one. |
| 2898 | if (I.getType() == Type::Int1Ty) |
| 2899 | return ReplaceInstUsesWith(I, Op0); |
| 2900 | |
Nick Lewycky | 895f085 | 2008-11-27 20:21:08 +0000 | [diff] [blame] | 2901 | if (ConstantVector *Op1V = dyn_cast<ConstantVector>(Op1)) { |
| 2902 | if (ConstantInt *X = cast_or_null<ConstantInt>(Op1V->getSplatValue())) |
| 2903 | // div X, 1 == X |
| 2904 | if (X->isOne()) |
| 2905 | return ReplaceInstUsesWith(I, Op0); |
| 2906 | } |
| 2907 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2908 | return 0; |
| 2909 | } |
| 2910 | |
| 2911 | Instruction *InstCombiner::visitUDiv(BinaryOperator &I) { |
| 2912 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2913 | |
| 2914 | // Handle the integer div common cases |
| 2915 | if (Instruction *Common = commonIDivTransforms(I)) |
| 2916 | return Common; |
| 2917 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2918 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op1)) { |
Nick Lewycky | 8ca5248 | 2008-11-27 22:41:10 +0000 | [diff] [blame] | 2919 | // X udiv C^2 -> X >> C |
| 2920 | // Check to see if this is an unsigned division with an exact power of 2, |
| 2921 | // if so, convert to a right shift. |
Reid Spencer | 6eb0d99 | 2007-03-26 23:58:26 +0000 | [diff] [blame] | 2922 | if (C->getValue().isPowerOf2()) // 0 not included in isPowerOf2 |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2923 | return BinaryOperator::CreateLShr(Op0, |
Zhou Sheng | 0fc5095 | 2007-03-25 05:01:29 +0000 | [diff] [blame] | 2924 | ConstantInt::get(Op0->getType(), C->getValue().logBase2())); |
Nick Lewycky | 8ca5248 | 2008-11-27 22:41:10 +0000 | [diff] [blame] | 2925 | |
| 2926 | // X udiv C, where C >= signbit |
| 2927 | if (C->getValue().isNegative()) { |
| 2928 | Value *IC = InsertNewInstBefore(new ICmpInst(ICmpInst::ICMP_ULT, Op0, C), |
| 2929 | I); |
| 2930 | return SelectInst::Create(IC, Constant::getNullValue(I.getType()), |
| 2931 | ConstantInt::get(I.getType(), 1)); |
| 2932 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2933 | } |
| 2934 | |
| 2935 | // X udiv (C1 << N), where C1 is "1<<C2" --> X >> (N+C2) |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2936 | if (BinaryOperator *RHSI = dyn_cast<BinaryOperator>(I.getOperand(1))) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2937 | if (RHSI->getOpcode() == Instruction::Shl && |
| 2938 | isa<ConstantInt>(RHSI->getOperand(0))) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2939 | const APInt& C1 = cast<ConstantInt>(RHSI->getOperand(0))->getValue(); |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2940 | if (C1.isPowerOf2()) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2941 | Value *N = RHSI->getOperand(1); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2942 | const Type *NTy = N->getType(); |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 2943 | if (uint32_t C2 = C1.logBase2()) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2944 | Constant *C2V = ConstantInt::get(NTy, C2); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2945 | N = InsertNewInstBefore(BinaryOperator::CreateAdd(N, C2V, "tmp"), I); |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2946 | } |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2947 | return BinaryOperator::CreateLShr(Op0, N); |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2948 | } |
| 2949 | } |
Chris Lattner | c812e5d | 2005-11-05 07:40:31 +0000 | [diff] [blame] | 2950 | } |
| 2951 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2952 | // udiv X, (Select Cond, C1, C2) --> Select Cond, (shr X, C1), (shr X, C2) |
| 2953 | // where C1&C2 are powers of two. |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2954 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2955 | if (ConstantInt *STO = dyn_cast<ConstantInt>(SI->getOperand(1))) |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2956 | if (ConstantInt *SFO = dyn_cast<ConstantInt>(SI->getOperand(2))) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2957 | const APInt &TVA = STO->getValue(), &FVA = SFO->getValue(); |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2958 | if (TVA.isPowerOf2() && FVA.isPowerOf2()) { |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2959 | // Compute the shift amounts |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2960 | uint32_t TSA = TVA.logBase2(), FSA = FVA.logBase2(); |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2961 | // Construct the "on true" case of the select |
| 2962 | Constant *TC = ConstantInt::get(Op0->getType(), TSA); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2963 | Instruction *TSI = BinaryOperator::CreateLShr( |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2964 | Op0, TC, SI->getName()+".t"); |
| 2965 | TSI = InsertNewInstBefore(TSI, I); |
| 2966 | |
| 2967 | // Construct the "on false" case of the select |
| 2968 | Constant *FC = ConstantInt::get(Op0->getType(), FSA); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2969 | Instruction *FSI = BinaryOperator::CreateLShr( |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2970 | Op0, FC, SI->getName()+".f"); |
| 2971 | FSI = InsertNewInstBefore(FSI, I); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2972 | |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2973 | // construct the select instruction and return it. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 2974 | return SelectInst::Create(SI->getOperand(0), TSI, FSI, SI->getName()); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2975 | } |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2976 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2977 | return 0; |
| 2978 | } |
| 2979 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2980 | Instruction *InstCombiner::visitSDiv(BinaryOperator &I) { |
| 2981 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2982 | |
| 2983 | // Handle the integer div common cases |
| 2984 | if (Instruction *Common = commonIDivTransforms(I)) |
| 2985 | return Common; |
| 2986 | |
| 2987 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
| 2988 | // sdiv X, -1 == -X |
| 2989 | if (RHS->isAllOnesValue()) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2990 | return BinaryOperator::CreateNeg(Op0); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2991 | } |
| 2992 | |
| 2993 | // If the sign bits of both operands are zero (i.e. we can prove they are |
| 2994 | // unsigned inputs), turn this into a udiv. |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 2995 | if (I.getType()->isInteger()) { |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2996 | APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits())); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2997 | if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) { |
Dan Gohman | cff5509 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 2998 | // 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] | 2999 | return BinaryOperator::CreateUDiv(Op0, Op1, I.getName()); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3000 | } |
| 3001 | } |
| 3002 | |
| 3003 | return 0; |
| 3004 | } |
| 3005 | |
| 3006 | Instruction *InstCombiner::visitFDiv(BinaryOperator &I) { |
| 3007 | return commonDivTransforms(I); |
| 3008 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3009 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3010 | /// This function implements the transforms on rem instructions that work |
| 3011 | /// regardless of the kind of rem instruction it is (urem, srem, or frem). It |
| 3012 | /// is used by the visitors to those instructions. |
| 3013 | /// @brief Transforms common to all three rem instructions |
| 3014 | Instruction *InstCombiner::commonRemTransforms(BinaryOperator &I) { |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 3015 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3016 | |
Chris Lattner | 50b2ca4 | 2008-02-19 06:12:18 +0000 | [diff] [blame] | 3017 | if (isa<UndefValue>(Op0)) { // undef % X -> 0 |
| 3018 | if (I.getType()->isFPOrFPVector()) |
| 3019 | return ReplaceInstUsesWith(I, Op0); // X % undef -> undef (could be SNaN) |
Chris Lattner | 19ccd5c | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 3020 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | 50b2ca4 | 2008-02-19 06:12:18 +0000 | [diff] [blame] | 3021 | } |
Chris Lattner | 19ccd5c | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 3022 | if (isa<UndefValue>(Op1)) |
| 3023 | return ReplaceInstUsesWith(I, Op1); // X % undef -> undef |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3024 | |
| 3025 | // Handle cases involving: rem X, (select Cond, Y, Z) |
Chris Lattner | fdb19e5 | 2008-07-14 00:15:52 +0000 | [diff] [blame] | 3026 | if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I)) |
| 3027 | return &I; |
Chris Lattner | 5b73c08 | 2004-07-06 07:01:22 +0000 | [diff] [blame] | 3028 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3029 | return 0; |
| 3030 | } |
| 3031 | |
| 3032 | /// This function implements the transforms common to both integer remainder |
| 3033 | /// instructions (urem and srem). It is called by the visitors to those integer |
| 3034 | /// remainder instructions. |
| 3035 | /// @brief Common integer remainder transforms |
| 3036 | Instruction *InstCombiner::commonIRemTransforms(BinaryOperator &I) { |
| 3037 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 3038 | |
| 3039 | if (Instruction *common = commonRemTransforms(I)) |
| 3040 | return common; |
| 3041 | |
Dale Johannesen | ed6af24 | 2009-01-21 00:35:19 +0000 | [diff] [blame] | 3042 | // 0 % X == 0 for integer, we don't need to preserve faults! |
| 3043 | if (Constant *LHS = dyn_cast<Constant>(Op0)) |
| 3044 | if (LHS->isNullValue()) |
| 3045 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 3046 | |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 3047 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 19ccd5c | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 3048 | // X % 0 == undef, we don't need to preserve faults! |
| 3049 | if (RHS->equalsInt(0)) |
| 3050 | return ReplaceInstUsesWith(I, UndefValue::get(I.getType())); |
| 3051 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3052 | if (RHS->equalsInt(1)) // X % 1 == 0 |
| 3053 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 3054 | |
Chris Lattner | 9794392 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 3055 | if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) { |
| 3056 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0I)) { |
| 3057 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 3058 | return R; |
| 3059 | } else if (isa<PHINode>(Op0I)) { |
| 3060 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 3061 | return NV; |
Chris Lattner | 9794392 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 3062 | } |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 3063 | |
| 3064 | // See if we can fold away this rem instruction. |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 3065 | if (SimplifyDemandedInstructionBits(I)) |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 3066 | return &I; |
Chris Lattner | 9794392 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 3067 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3068 | } |
| 3069 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3070 | return 0; |
| 3071 | } |
| 3072 | |
| 3073 | Instruction *InstCombiner::visitURem(BinaryOperator &I) { |
| 3074 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 3075 | |
| 3076 | if (Instruction *common = commonIRemTransforms(I)) |
| 3077 | return common; |
| 3078 | |
| 3079 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
| 3080 | // X urem C^2 -> X and C |
| 3081 | // Check to see if this is an unsigned remainder with an exact power of 2, |
| 3082 | // if so, convert to a bitwise and. |
| 3083 | if (ConstantInt *C = dyn_cast<ConstantInt>(RHS)) |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 3084 | if (C->getValue().isPowerOf2()) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3085 | return BinaryOperator::CreateAnd(Op0, SubOne(C)); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3086 | } |
| 3087 | |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 3088 | if (Instruction *RHSI = dyn_cast<Instruction>(I.getOperand(1))) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3089 | // Turn A % (C << N), where C is 2^k, into A & ((C << N)-1) |
| 3090 | if (RHSI->getOpcode() == Instruction::Shl && |
| 3091 | isa<ConstantInt>(RHSI->getOperand(0))) { |
Zhou Sheng | 0fc5095 | 2007-03-25 05:01:29 +0000 | [diff] [blame] | 3092 | if (cast<ConstantInt>(RHSI->getOperand(0))->getValue().isPowerOf2()) { |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 3093 | Constant *N1 = ConstantInt::getAllOnesValue(I.getType()); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3094 | Value *Add = InsertNewInstBefore(BinaryOperator::CreateAdd(RHSI, N1, |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 3095 | "tmp"), I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3096 | return BinaryOperator::CreateAnd(Op0, Add); |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 3097 | } |
| 3098 | } |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3099 | } |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 3100 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3101 | // urem X, (select Cond, 2^C1, 2^C2) --> select Cond, (and X, C1), (and X, C2) |
| 3102 | // where C1&C2 are powers of two. |
| 3103 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { |
| 3104 | if (ConstantInt *STO = dyn_cast<ConstantInt>(SI->getOperand(1))) |
| 3105 | if (ConstantInt *SFO = dyn_cast<ConstantInt>(SI->getOperand(2))) { |
| 3106 | // STO == 0 and SFO == 0 handled above. |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 3107 | if ((STO->getValue().isPowerOf2()) && |
| 3108 | (SFO->getValue().isPowerOf2())) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3109 | Value *TrueAnd = InsertNewInstBefore( |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3110 | BinaryOperator::CreateAnd(Op0, SubOne(STO), SI->getName()+".t"), I); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3111 | Value *FalseAnd = InsertNewInstBefore( |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3112 | BinaryOperator::CreateAnd(Op0, SubOne(SFO), SI->getName()+".f"), I); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 3113 | return SelectInst::Create(SI->getOperand(0), TrueAnd, FalseAnd); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3114 | } |
| 3115 | } |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 3116 | } |
| 3117 | |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3118 | return 0; |
| 3119 | } |
| 3120 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3121 | Instruction *InstCombiner::visitSRem(BinaryOperator &I) { |
| 3122 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 3123 | |
Dan Gohman | cff5509 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 3124 | // Handle the integer rem common cases |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3125 | if (Instruction *common = commonIRemTransforms(I)) |
| 3126 | return common; |
| 3127 | |
| 3128 | if (Value *RHSNeg = dyn_castNegVal(Op1)) |
Nick Lewycky | 23c0430 | 2008-09-03 06:24:21 +0000 | [diff] [blame] | 3129 | if (!isa<Constant>(RHSNeg) || |
| 3130 | (isa<ConstantInt>(RHSNeg) && |
| 3131 | cast<ConstantInt>(RHSNeg)->getValue().isStrictlyPositive())) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3132 | // X % -Y -> X % Y |
| 3133 | AddUsesToWorkList(I); |
| 3134 | I.setOperand(1, RHSNeg); |
| 3135 | return &I; |
| 3136 | } |
Nick Lewycky | a06cf82 | 2008-09-30 06:08:34 +0000 | [diff] [blame] | 3137 | |
Dan Gohman | cff5509 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 3138 | // 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] | 3139 | // unsigned inputs), turn this into a urem. |
Dan Gohman | cff5509 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 3140 | if (I.getType()->isInteger()) { |
| 3141 | APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits())); |
| 3142 | if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) { |
| 3143 | // 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] | 3144 | return BinaryOperator::CreateURem(Op0, Op1, I.getName()); |
Dan Gohman | cff5509 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 3145 | } |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3146 | } |
| 3147 | |
Nick Lewycky | 2a8f659 | 2008-12-18 06:31:11 +0000 | [diff] [blame] | 3148 | // If it's a constant vector, flip any negative values positive. |
Nick Lewycky | 9dce873 | 2008-12-20 16:48:00 +0000 | [diff] [blame] | 3149 | if (ConstantVector *RHSV = dyn_cast<ConstantVector>(Op1)) { |
| 3150 | unsigned VWidth = RHSV->getNumOperands(); |
Nick Lewycky | 2a8f659 | 2008-12-18 06:31:11 +0000 | [diff] [blame] | 3151 | |
Nick Lewycky | 9dce873 | 2008-12-20 16:48:00 +0000 | [diff] [blame] | 3152 | bool hasNegative = false; |
| 3153 | for (unsigned i = 0; !hasNegative && i != VWidth; ++i) |
| 3154 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(RHSV->getOperand(i))) |
| 3155 | if (RHS->getValue().isNegative()) |
| 3156 | hasNegative = true; |
| 3157 | |
| 3158 | if (hasNegative) { |
| 3159 | std::vector<Constant *> Elts(VWidth); |
Nick Lewycky | 2a8f659 | 2008-12-18 06:31:11 +0000 | [diff] [blame] | 3160 | for (unsigned i = 0; i != VWidth; ++i) { |
| 3161 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(RHSV->getOperand(i))) { |
| 3162 | if (RHS->getValue().isNegative()) |
| 3163 | Elts[i] = cast<ConstantInt>(ConstantExpr::getNeg(RHS)); |
| 3164 | else |
| 3165 | Elts[i] = RHS; |
| 3166 | } |
| 3167 | } |
| 3168 | |
| 3169 | Constant *NewRHSV = ConstantVector::get(Elts); |
| 3170 | if (NewRHSV != RHSV) { |
Nick Lewycky | 19c2892 | 2008-12-18 06:42:28 +0000 | [diff] [blame] | 3171 | AddUsesToWorkList(I); |
Nick Lewycky | 2a8f659 | 2008-12-18 06:31:11 +0000 | [diff] [blame] | 3172 | I.setOperand(1, NewRHSV); |
| 3173 | return &I; |
| 3174 | } |
| 3175 | } |
| 3176 | } |
| 3177 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3178 | return 0; |
| 3179 | } |
| 3180 | |
| 3181 | Instruction *InstCombiner::visitFRem(BinaryOperator &I) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3182 | return commonRemTransforms(I); |
| 3183 | } |
| 3184 | |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 3185 | // isOneBitSet - Return true if there is exactly one bit set in the specified |
| 3186 | // constant. |
| 3187 | static bool isOneBitSet(const ConstantInt *CI) { |
Reid Spencer | 5f6a895 | 2007-03-20 00:16:52 +0000 | [diff] [blame] | 3188 | return CI->getValue().isPowerOf2(); |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 3189 | } |
| 3190 | |
Chris Lattner | b20ba0a | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 3191 | // isHighOnes - Return true if the constant is of the form 1+0+. |
| 3192 | // This is the same as lowones(~X). |
| 3193 | static bool isHighOnes(const ConstantInt *CI) { |
Zhou Sheng | 2cde46c | 2007-03-20 12:49:06 +0000 | [diff] [blame] | 3194 | return (~CI->getValue() + 1).isPowerOf2(); |
Chris Lattner | b20ba0a | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 3195 | } |
| 3196 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3197 | /// getICmpCode - Encode a icmp predicate into a three bit mask. These bits |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3198 | /// are carefully arranged to allow folding of expressions such as: |
| 3199 | /// |
| 3200 | /// (A < B) | (A > B) --> (A != B) |
| 3201 | /// |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3202 | /// Note that this is only valid if the first and second predicates have the |
| 3203 | /// same sign. Is illegal to do: (A u< B) | (A s> B) |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3204 | /// |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3205 | /// Three bits are used to represent the condition, as follows: |
| 3206 | /// 0 A > B |
| 3207 | /// 1 A == B |
| 3208 | /// 2 A < B |
| 3209 | /// |
| 3210 | /// <=> Value Definition |
| 3211 | /// 000 0 Always false |
| 3212 | /// 001 1 A > B |
| 3213 | /// 010 2 A == B |
| 3214 | /// 011 3 A >= B |
| 3215 | /// 100 4 A < B |
| 3216 | /// 101 5 A != B |
| 3217 | /// 110 6 A <= B |
| 3218 | /// 111 7 Always true |
| 3219 | /// |
| 3220 | static unsigned getICmpCode(const ICmpInst *ICI) { |
| 3221 | switch (ICI->getPredicate()) { |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3222 | // False -> 0 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3223 | case ICmpInst::ICMP_UGT: return 1; // 001 |
| 3224 | case ICmpInst::ICMP_SGT: return 1; // 001 |
| 3225 | case ICmpInst::ICMP_EQ: return 2; // 010 |
| 3226 | case ICmpInst::ICMP_UGE: return 3; // 011 |
| 3227 | case ICmpInst::ICMP_SGE: return 3; // 011 |
| 3228 | case ICmpInst::ICMP_ULT: return 4; // 100 |
| 3229 | case ICmpInst::ICMP_SLT: return 4; // 100 |
| 3230 | case ICmpInst::ICMP_NE: return 5; // 101 |
| 3231 | case ICmpInst::ICMP_ULE: return 6; // 110 |
| 3232 | case ICmpInst::ICMP_SLE: return 6; // 110 |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3233 | // True -> 7 |
| 3234 | default: |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3235 | assert(0 && "Invalid ICmp predicate!"); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3236 | return 0; |
| 3237 | } |
| 3238 | } |
| 3239 | |
Evan Cheng | 8db9072 | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 3240 | /// getFCmpCode - Similar to getICmpCode but for FCmpInst. This encodes a fcmp |
| 3241 | /// predicate into a three bit mask. It also returns whether it is an ordered |
| 3242 | /// predicate by reference. |
| 3243 | static unsigned getFCmpCode(FCmpInst::Predicate CC, bool &isOrdered) { |
| 3244 | isOrdered = false; |
| 3245 | switch (CC) { |
| 3246 | case FCmpInst::FCMP_ORD: isOrdered = true; return 0; // 000 |
| 3247 | case FCmpInst::FCMP_UNO: return 0; // 000 |
Evan Cheng | 4990b25 | 2008-10-14 18:13:38 +0000 | [diff] [blame] | 3248 | case FCmpInst::FCMP_OGT: isOrdered = true; return 1; // 001 |
| 3249 | case FCmpInst::FCMP_UGT: return 1; // 001 |
| 3250 | case FCmpInst::FCMP_OEQ: isOrdered = true; return 2; // 010 |
| 3251 | case FCmpInst::FCMP_UEQ: return 2; // 010 |
Evan Cheng | 8db9072 | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 3252 | case FCmpInst::FCMP_OGE: isOrdered = true; return 3; // 011 |
| 3253 | case FCmpInst::FCMP_UGE: return 3; // 011 |
| 3254 | case FCmpInst::FCMP_OLT: isOrdered = true; return 4; // 100 |
| 3255 | case FCmpInst::FCMP_ULT: return 4; // 100 |
Evan Cheng | 4990b25 | 2008-10-14 18:13:38 +0000 | [diff] [blame] | 3256 | case FCmpInst::FCMP_ONE: isOrdered = true; return 5; // 101 |
| 3257 | case FCmpInst::FCMP_UNE: return 5; // 101 |
Evan Cheng | 8db9072 | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 3258 | case FCmpInst::FCMP_OLE: isOrdered = true; return 6; // 110 |
| 3259 | case FCmpInst::FCMP_ULE: return 6; // 110 |
Evan Cheng | 4030062 | 2008-10-14 18:44:08 +0000 | [diff] [blame] | 3260 | // True -> 7 |
Evan Cheng | 8db9072 | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 3261 | default: |
| 3262 | // Not expecting FCMP_FALSE and FCMP_TRUE; |
| 3263 | assert(0 && "Unexpected FCmp predicate!"); |
| 3264 | return 0; |
| 3265 | } |
| 3266 | } |
| 3267 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3268 | /// getICmpValue - This is the complement of getICmpCode, which turns an |
| 3269 | /// 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] | 3270 | /// new ICmp instruction. The sign is passed in to determine which kind |
Evan Cheng | 8db9072 | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 3271 | /// of predicate to use in the new icmp instruction. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3272 | static Value *getICmpValue(bool sign, unsigned code, Value *LHS, Value *RHS) { |
| 3273 | switch (code) { |
| 3274 | default: assert(0 && "Illegal ICmp code!"); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3275 | case 0: return ConstantInt::getFalse(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3276 | case 1: |
| 3277 | if (sign) |
| 3278 | return new ICmpInst(ICmpInst::ICMP_SGT, LHS, RHS); |
| 3279 | else |
| 3280 | return new ICmpInst(ICmpInst::ICMP_UGT, LHS, RHS); |
| 3281 | case 2: return new ICmpInst(ICmpInst::ICMP_EQ, LHS, RHS); |
| 3282 | case 3: |
| 3283 | if (sign) |
| 3284 | return new ICmpInst(ICmpInst::ICMP_SGE, LHS, RHS); |
| 3285 | else |
| 3286 | return new ICmpInst(ICmpInst::ICMP_UGE, LHS, RHS); |
| 3287 | case 4: |
| 3288 | if (sign) |
| 3289 | return new ICmpInst(ICmpInst::ICMP_SLT, LHS, RHS); |
| 3290 | else |
| 3291 | return new ICmpInst(ICmpInst::ICMP_ULT, LHS, RHS); |
| 3292 | case 5: return new ICmpInst(ICmpInst::ICMP_NE, LHS, RHS); |
| 3293 | case 6: |
| 3294 | if (sign) |
| 3295 | return new ICmpInst(ICmpInst::ICMP_SLE, LHS, RHS); |
| 3296 | else |
| 3297 | return new ICmpInst(ICmpInst::ICMP_ULE, LHS, RHS); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3298 | case 7: return ConstantInt::getTrue(); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3299 | } |
| 3300 | } |
| 3301 | |
Evan Cheng | 8db9072 | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 3302 | /// getFCmpValue - This is the complement of getFCmpCode, which turns an |
| 3303 | /// opcode and two operands into either a FCmp instruction. isordered is passed |
| 3304 | /// in to determine which kind of predicate to use in the new fcmp instruction. |
| 3305 | static Value *getFCmpValue(bool isordered, unsigned code, |
| 3306 | Value *LHS, Value *RHS) { |
| 3307 | switch (code) { |
Evan Cheng | 4990b25 | 2008-10-14 18:13:38 +0000 | [diff] [blame] | 3308 | default: assert(0 && "Illegal FCmp code!"); |
Evan Cheng | 8db9072 | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 3309 | case 0: |
| 3310 | if (isordered) |
| 3311 | return new FCmpInst(FCmpInst::FCMP_ORD, LHS, RHS); |
| 3312 | else |
| 3313 | return new FCmpInst(FCmpInst::FCMP_UNO, LHS, RHS); |
| 3314 | case 1: |
| 3315 | if (isordered) |
Evan Cheng | 8db9072 | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 3316 | return new FCmpInst(FCmpInst::FCMP_OGT, LHS, RHS); |
| 3317 | else |
| 3318 | return new FCmpInst(FCmpInst::FCMP_UGT, LHS, RHS); |
Evan Cheng | 4990b25 | 2008-10-14 18:13:38 +0000 | [diff] [blame] | 3319 | case 2: |
| 3320 | if (isordered) |
| 3321 | return new FCmpInst(FCmpInst::FCMP_OEQ, LHS, RHS); |
| 3322 | else |
| 3323 | return new FCmpInst(FCmpInst::FCMP_UEQ, LHS, RHS); |
Evan Cheng | 8db9072 | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 3324 | case 3: |
| 3325 | if (isordered) |
| 3326 | return new FCmpInst(FCmpInst::FCMP_OGE, LHS, RHS); |
| 3327 | else |
| 3328 | return new FCmpInst(FCmpInst::FCMP_UGE, LHS, RHS); |
| 3329 | case 4: |
| 3330 | if (isordered) |
| 3331 | return new FCmpInst(FCmpInst::FCMP_OLT, LHS, RHS); |
| 3332 | else |
| 3333 | return new FCmpInst(FCmpInst::FCMP_ULT, LHS, RHS); |
| 3334 | case 5: |
| 3335 | if (isordered) |
Evan Cheng | 4990b25 | 2008-10-14 18:13:38 +0000 | [diff] [blame] | 3336 | return new FCmpInst(FCmpInst::FCMP_ONE, LHS, RHS); |
| 3337 | else |
| 3338 | return new FCmpInst(FCmpInst::FCMP_UNE, LHS, RHS); |
| 3339 | case 6: |
| 3340 | if (isordered) |
Evan Cheng | 8db9072 | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 3341 | return new FCmpInst(FCmpInst::FCMP_OLE, LHS, RHS); |
| 3342 | else |
| 3343 | return new FCmpInst(FCmpInst::FCMP_ULE, LHS, RHS); |
Evan Cheng | 4030062 | 2008-10-14 18:44:08 +0000 | [diff] [blame] | 3344 | case 7: return ConstantInt::getTrue(); |
Evan Cheng | 8db9072 | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 3345 | } |
| 3346 | } |
| 3347 | |
Chris Lattner | b9553d6 | 2008-11-16 04:55:20 +0000 | [diff] [blame] | 3348 | /// PredicatesFoldable - Return true if both predicates match sign or if at |
| 3349 | /// least one of them is an equality comparison (which is signless). |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3350 | static bool PredicatesFoldable(ICmpInst::Predicate p1, ICmpInst::Predicate p2) { |
| 3351 | return (ICmpInst::isSignedPredicate(p1) == ICmpInst::isSignedPredicate(p2)) || |
Chris Lattner | b9553d6 | 2008-11-16 04:55:20 +0000 | [diff] [blame] | 3352 | (ICmpInst::isSignedPredicate(p1) && ICmpInst::isEquality(p2)) || |
| 3353 | (ICmpInst::isSignedPredicate(p2) && ICmpInst::isEquality(p1)); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3354 | } |
| 3355 | |
| 3356 | namespace { |
| 3357 | // FoldICmpLogical - Implements (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B) |
| 3358 | struct FoldICmpLogical { |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3359 | InstCombiner &IC; |
| 3360 | Value *LHS, *RHS; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3361 | ICmpInst::Predicate pred; |
| 3362 | FoldICmpLogical(InstCombiner &ic, ICmpInst *ICI) |
| 3363 | : IC(ic), LHS(ICI->getOperand(0)), RHS(ICI->getOperand(1)), |
| 3364 | pred(ICI->getPredicate()) {} |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3365 | bool shouldApply(Value *V) const { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3366 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(V)) |
| 3367 | if (PredicatesFoldable(pred, ICI->getPredicate())) |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 3368 | return ((ICI->getOperand(0) == LHS && ICI->getOperand(1) == RHS) || |
| 3369 | (ICI->getOperand(0) == RHS && ICI->getOperand(1) == LHS)); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3370 | return false; |
| 3371 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3372 | Instruction *apply(Instruction &Log) const { |
| 3373 | ICmpInst *ICI = cast<ICmpInst>(Log.getOperand(0)); |
| 3374 | if (ICI->getOperand(0) != LHS) { |
| 3375 | assert(ICI->getOperand(1) == LHS); |
| 3376 | ICI->swapOperands(); // Swap the LHS and RHS of the ICmp |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3377 | } |
| 3378 | |
Chris Lattner | bc1dbfc | 2007-03-13 14:27:42 +0000 | [diff] [blame] | 3379 | ICmpInst *RHSICI = cast<ICmpInst>(Log.getOperand(1)); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3380 | unsigned LHSCode = getICmpCode(ICI); |
Chris Lattner | bc1dbfc | 2007-03-13 14:27:42 +0000 | [diff] [blame] | 3381 | unsigned RHSCode = getICmpCode(RHSICI); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3382 | unsigned Code; |
| 3383 | switch (Log.getOpcode()) { |
| 3384 | case Instruction::And: Code = LHSCode & RHSCode; break; |
| 3385 | case Instruction::Or: Code = LHSCode | RHSCode; break; |
| 3386 | case Instruction::Xor: Code = LHSCode ^ RHSCode; break; |
Chris Lattner | 021c190 | 2003-09-22 20:33:34 +0000 | [diff] [blame] | 3387 | default: assert(0 && "Illegal logical opcode!"); return 0; |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3388 | } |
| 3389 | |
Chris Lattner | bc1dbfc | 2007-03-13 14:27:42 +0000 | [diff] [blame] | 3390 | bool isSigned = ICmpInst::isSignedPredicate(RHSICI->getPredicate()) || |
| 3391 | ICmpInst::isSignedPredicate(ICI->getPredicate()); |
| 3392 | |
| 3393 | Value *RV = getICmpValue(isSigned, Code, LHS, RHS); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3394 | if (Instruction *I = dyn_cast<Instruction>(RV)) |
| 3395 | return I; |
| 3396 | // Otherwise, it's a constant boolean value... |
| 3397 | return IC.ReplaceInstUsesWith(Log, RV); |
| 3398 | } |
| 3399 | }; |
Chris Lattner | d23b5ba | 2006-11-15 04:53:24 +0000 | [diff] [blame] | 3400 | } // end anonymous namespace |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3401 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3402 | // OptAndOp - This handles expressions of the form ((val OP C1) & C2). Where |
| 3403 | // 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] | 3404 | // guaranteed to be a binary operator. |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3405 | Instruction *InstCombiner::OptAndOp(Instruction *Op, |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3406 | ConstantInt *OpRHS, |
| 3407 | ConstantInt *AndRHS, |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3408 | BinaryOperator &TheAnd) { |
| 3409 | Value *X = Op->getOperand(0); |
Chris Lattner | 76f7fe2 | 2004-01-12 19:47:05 +0000 | [diff] [blame] | 3410 | Constant *Together = 0; |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3411 | if (!Op->isShift()) |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 3412 | Together = And(AndRHS, OpRHS); |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3413 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3414 | switch (Op->getOpcode()) { |
| 3415 | case Instruction::Xor: |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3416 | if (Op->hasOneUse()) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3417 | // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3418 | Instruction *And = BinaryOperator::CreateAnd(X, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3419 | InsertNewInstBefore(And, TheAnd); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3420 | And->takeName(Op); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3421 | return BinaryOperator::CreateXor(And, Together); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3422 | } |
| 3423 | break; |
| 3424 | case Instruction::Or: |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3425 | if (Together == AndRHS) // (X | C) & C --> C |
| 3426 | return ReplaceInstUsesWith(TheAnd, AndRHS); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3427 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3428 | if (Op->hasOneUse() && Together != OpRHS) { |
| 3429 | // (X | C1) & C2 --> (X | (C1&C2)) & C2 |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3430 | Instruction *Or = BinaryOperator::CreateOr(X, Together); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3431 | InsertNewInstBefore(Or, TheAnd); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3432 | Or->takeName(Op); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3433 | return BinaryOperator::CreateAnd(Or, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3434 | } |
| 3435 | break; |
| 3436 | case Instruction::Add: |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 3437 | if (Op->hasOneUse()) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3438 | // Adding a one to a single bit bit-field should be turned into an XOR |
| 3439 | // of the bit. First thing to check is to see if this AND is with a |
| 3440 | // single bit constant. |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3441 | const APInt& AndRHSV = cast<ConstantInt>(AndRHS)->getValue(); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3442 | |
| 3443 | // If there is only one bit set... |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 3444 | if (isOneBitSet(cast<ConstantInt>(AndRHS))) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3445 | // Ok, at this point, we know that we are masking the result of the |
| 3446 | // ADD down to exactly one bit. If the constant we are adding has |
| 3447 | // no bits set below this bit, then we can eliminate the ADD. |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3448 | const APInt& AddRHS = cast<ConstantInt>(OpRHS)->getValue(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3449 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3450 | // Check to see if any bits below the one bit set in AndRHSV are set. |
| 3451 | if ((AddRHS & (AndRHSV-1)) == 0) { |
| 3452 | // If not, the only thing that can effect the output of the AND is |
| 3453 | // the bit specified by AndRHSV. If that bit is set, the effect of |
| 3454 | // the XOR is to toggle the bit. If it is clear, then the ADD has |
| 3455 | // no effect. |
| 3456 | if ((AddRHS & AndRHSV) == 0) { // Bit is not set, noop |
| 3457 | TheAnd.setOperand(0, X); |
| 3458 | return &TheAnd; |
| 3459 | } else { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3460 | // Pull the XOR out of the AND. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3461 | Instruction *NewAnd = BinaryOperator::CreateAnd(X, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3462 | InsertNewInstBefore(NewAnd, TheAnd); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3463 | NewAnd->takeName(Op); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3464 | return BinaryOperator::CreateXor(NewAnd, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3465 | } |
| 3466 | } |
| 3467 | } |
| 3468 | } |
| 3469 | break; |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 3470 | |
| 3471 | case Instruction::Shl: { |
| 3472 | // We know that the AND will not produce any of the bits shifted in, so if |
| 3473 | // the anded constant includes them, clear them now! |
| 3474 | // |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3475 | uint32_t BitWidth = AndRHS->getType()->getBitWidth(); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3476 | uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3477 | APInt ShlMask(APInt::getHighBitsSet(BitWidth, BitWidth-OpRHSVal)); |
| 3478 | ConstantInt *CI = ConstantInt::get(AndRHS->getValue() & ShlMask); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3479 | |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3480 | if (CI->getValue() == ShlMask) { |
| 3481 | // Masking out bits that the shift already masks |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 3482 | return ReplaceInstUsesWith(TheAnd, Op); // No need for the and. |
| 3483 | } else if (CI != AndRHS) { // Reducing bits set in and. |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 3484 | TheAnd.setOperand(1, CI); |
| 3485 | return &TheAnd; |
| 3486 | } |
| 3487 | break; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3488 | } |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 3489 | case Instruction::LShr: |
| 3490 | { |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 3491 | // We know that the AND will not produce any of the bits shifted in, so if |
| 3492 | // the anded constant includes them, clear them now! This only applies to |
| 3493 | // unsigned shifts, because a signed shr may bring in set bits! |
| 3494 | // |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3495 | uint32_t BitWidth = AndRHS->getType()->getBitWidth(); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3496 | uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3497 | APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal)); |
| 3498 | ConstantInt *CI = ConstantInt::get(AndRHS->getValue() & ShrMask); |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 3499 | |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3500 | if (CI->getValue() == ShrMask) { |
| 3501 | // Masking out bits that the shift already masks. |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 3502 | return ReplaceInstUsesWith(TheAnd, Op); |
| 3503 | } else if (CI != AndRHS) { |
| 3504 | TheAnd.setOperand(1, CI); // Reduce bits set in and cst. |
| 3505 | return &TheAnd; |
| 3506 | } |
| 3507 | break; |
| 3508 | } |
| 3509 | case Instruction::AShr: |
| 3510 | // Signed shr. |
| 3511 | // See if this is shifting in some sign extension, then masking it out |
| 3512 | // with an and. |
| 3513 | if (Op->hasOneUse()) { |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3514 | uint32_t BitWidth = AndRHS->getType()->getBitWidth(); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3515 | uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3516 | APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal)); |
| 3517 | Constant *C = ConstantInt::get(AndRHS->getValue() & ShrMask); |
Reid Spencer | 7eb7638 | 2006-12-13 17:19:09 +0000 | [diff] [blame] | 3518 | if (C == AndRHS) { // Masking out bits shifted in. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 3519 | // (Val ashr C1) & C2 -> (Val lshr C1) & C2 |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 3520 | // Make the argument unsigned. |
| 3521 | Value *ShVal = Op->getOperand(0); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3522 | ShVal = InsertNewInstBefore( |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3523 | BinaryOperator::CreateLShr(ShVal, OpRHS, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3524 | Op->getName()), TheAnd); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3525 | return BinaryOperator::CreateAnd(ShVal, AndRHS, TheAnd.getName()); |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 3526 | } |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 3527 | } |
| 3528 | break; |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3529 | } |
| 3530 | return 0; |
| 3531 | } |
| 3532 | |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 3533 | |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3534 | /// InsertRangeTest - Emit a computation of: (V >= Lo && V < Hi) if Inside is |
| 3535 | /// 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] | 3536 | /// (V-Lo) <u Hi-Lo. This method expects that Lo <= Hi. isSigned indicates |
| 3537 | /// 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] | 3538 | /// insert new instructions. |
| 3539 | Instruction *InstCombiner::InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3540 | bool isSigned, bool Inside, |
| 3541 | Instruction &IB) { |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3542 | assert(cast<ConstantInt>(ConstantExpr::getICmp((isSigned ? |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 3543 | ICmpInst::ICMP_SLE:ICmpInst::ICMP_ULE), Lo, Hi))->getZExtValue() && |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3544 | "Lo is not <= Hi in range emission code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3545 | |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3546 | if (Inside) { |
| 3547 | if (Lo == Hi) // Trivially false. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3548 | return new ICmpInst(ICmpInst::ICMP_NE, V, V); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3549 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3550 | // V >= Min && V < Hi --> V < Hi |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3551 | if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) { |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 3552 | ICmpInst::Predicate pred = (isSigned ? |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3553 | ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT); |
| 3554 | return new ICmpInst(pred, V, Hi); |
| 3555 | } |
| 3556 | |
| 3557 | // Emit V-Lo <u Hi-Lo |
| 3558 | Constant *NegLo = ConstantExpr::getNeg(Lo); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3559 | Instruction *Add = BinaryOperator::CreateAdd(V, NegLo, V->getName()+".off"); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3560 | InsertNewInstBefore(Add, IB); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3561 | Constant *UpperBound = ConstantExpr::getAdd(NegLo, Hi); |
| 3562 | return new ICmpInst(ICmpInst::ICMP_ULT, Add, UpperBound); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3563 | } |
| 3564 | |
| 3565 | if (Lo == Hi) // Trivially true. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3566 | return new ICmpInst(ICmpInst::ICMP_EQ, V, V); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3567 | |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 3568 | // V < Min || V >= Hi -> V > Hi-1 |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3569 | Hi = SubOne(cast<ConstantInt>(Hi)); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3570 | if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3571 | ICmpInst::Predicate pred = (isSigned ? |
| 3572 | ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT); |
| 3573 | return new ICmpInst(pred, V, Hi); |
| 3574 | } |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3575 | |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 3576 | // Emit V-Lo >u Hi-1-Lo |
| 3577 | // Note that Hi has already had one subtracted from it, above. |
| 3578 | ConstantInt *NegLo = cast<ConstantInt>(ConstantExpr::getNeg(Lo)); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3579 | Instruction *Add = BinaryOperator::CreateAdd(V, NegLo, V->getName()+".off"); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3580 | InsertNewInstBefore(Add, IB); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3581 | Constant *LowerBound = ConstantExpr::getAdd(NegLo, Hi); |
| 3582 | return new ICmpInst(ICmpInst::ICMP_UGT, Add, LowerBound); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3583 | } |
| 3584 | |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3585 | // isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with |
| 3586 | // any number of 0s on either side. The 1s are allowed to wrap from LSB to |
| 3587 | // MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is |
| 3588 | // not, since all 1s are not contiguous. |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 3589 | static bool isRunOfOnes(ConstantInt *Val, uint32_t &MB, uint32_t &ME) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3590 | const APInt& V = Val->getValue(); |
Reid Spencer | f244252 | 2007-03-24 00:42:08 +0000 | [diff] [blame] | 3591 | uint32_t BitWidth = Val->getType()->getBitWidth(); |
| 3592 | if (!APIntOps::isShiftedMask(BitWidth, V)) return false; |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3593 | |
| 3594 | // look for the first zero bit after the run of ones |
Reid Spencer | f244252 | 2007-03-24 00:42:08 +0000 | [diff] [blame] | 3595 | MB = BitWidth - ((V - 1) ^ V).countLeadingZeros(); |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3596 | // look for the first non-zero bit |
Reid Spencer | f244252 | 2007-03-24 00:42:08 +0000 | [diff] [blame] | 3597 | ME = V.getActiveBits(); |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3598 | return true; |
| 3599 | } |
| 3600 | |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3601 | /// FoldLogicalPlusAnd - This is part of an expression (LHS +/- RHS) & Mask, |
| 3602 | /// where isSub determines whether the operator is a sub. If we can fold one of |
| 3603 | /// the following xforms: |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3604 | /// |
| 3605 | /// ((A & N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == Mask |
| 3606 | /// ((A | N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0 |
| 3607 | /// ((A ^ N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0 |
| 3608 | /// |
| 3609 | /// return (A +/- B). |
| 3610 | /// |
| 3611 | Value *InstCombiner::FoldLogicalPlusAnd(Value *LHS, Value *RHS, |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3612 | ConstantInt *Mask, bool isSub, |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3613 | Instruction &I) { |
| 3614 | Instruction *LHSI = dyn_cast<Instruction>(LHS); |
| 3615 | if (!LHSI || LHSI->getNumOperands() != 2 || |
| 3616 | !isa<ConstantInt>(LHSI->getOperand(1))) return 0; |
| 3617 | |
| 3618 | ConstantInt *N = cast<ConstantInt>(LHSI->getOperand(1)); |
| 3619 | |
| 3620 | switch (LHSI->getOpcode()) { |
| 3621 | default: return 0; |
| 3622 | case Instruction::And: |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 3623 | if (And(N, Mask) == Mask) { |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3624 | // 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] | 3625 | if ((Mask->getValue().countLeadingZeros() + |
| 3626 | Mask->getValue().countPopulation()) == |
| 3627 | Mask->getValue().getBitWidth()) |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3628 | break; |
| 3629 | |
| 3630 | // Otherwise, if Mask is 0+1+0+, and if B is known to have the low 0+ |
| 3631 | // part, we don't need any explicit masks to take them out of A. If that |
| 3632 | // is all N is, ignore it. |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 3633 | uint32_t MB = 0, ME = 0; |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3634 | if (isRunOfOnes(Mask, MB, ME)) { // begin/end bit of run, inclusive |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 3635 | uint32_t BitWidth = cast<IntegerType>(RHS->getType())->getBitWidth(); |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3636 | APInt Mask(APInt::getLowBitsSet(BitWidth, MB-1)); |
Chris Lattner | 3bedbd9 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3637 | if (MaskedValueIsZero(RHS, Mask)) |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3638 | break; |
| 3639 | } |
| 3640 | } |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3641 | return 0; |
| 3642 | case Instruction::Or: |
| 3643 | case Instruction::Xor: |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3644 | // 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] | 3645 | if ((Mask->getValue().countLeadingZeros() + |
| 3646 | Mask->getValue().countPopulation()) == Mask->getValue().getBitWidth() |
Reid Spencer | 6eb0d99 | 2007-03-26 23:58:26 +0000 | [diff] [blame] | 3647 | && And(N, Mask)->isZero()) |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3648 | break; |
| 3649 | return 0; |
| 3650 | } |
| 3651 | |
| 3652 | Instruction *New; |
| 3653 | if (isSub) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3654 | New = BinaryOperator::CreateSub(LHSI->getOperand(0), RHS, "fold"); |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3655 | else |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3656 | New = BinaryOperator::CreateAdd(LHSI->getOperand(0), RHS, "fold"); |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3657 | return InsertNewInstBefore(New, I); |
| 3658 | } |
| 3659 | |
Chris Lattner | 29cd5ba | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 3660 | /// FoldAndOfICmps - Fold (icmp)&(icmp) if possible. |
| 3661 | Instruction *InstCombiner::FoldAndOfICmps(Instruction &I, |
| 3662 | ICmpInst *LHS, ICmpInst *RHS) { |
Chris Lattner | ea065fb | 2008-11-16 05:10:52 +0000 | [diff] [blame] | 3663 | Value *Val, *Val2; |
Chris Lattner | 29cd5ba | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 3664 | ConstantInt *LHSCst, *RHSCst; |
| 3665 | ICmpInst::Predicate LHSCC, RHSCC; |
| 3666 | |
Chris Lattner | ea065fb | 2008-11-16 05:10:52 +0000 | [diff] [blame] | 3667 | // This only handles icmp of constants: (icmp1 A, C1) & (icmp2 B, C2). |
Chris Lattner | 29cd5ba | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 3668 | if (!match(LHS, m_ICmp(LHSCC, m_Value(Val), m_ConstantInt(LHSCst))) || |
Chris Lattner | ea065fb | 2008-11-16 05:10:52 +0000 | [diff] [blame] | 3669 | !match(RHS, m_ICmp(RHSCC, m_Value(Val2), m_ConstantInt(RHSCst)))) |
Chris Lattner | 29cd5ba | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 3670 | return 0; |
Chris Lattner | ea065fb | 2008-11-16 05:10:52 +0000 | [diff] [blame] | 3671 | |
| 3672 | // (icmp ult A, C) & (icmp ult B, C) --> (icmp ult (A|B), C) |
| 3673 | // where C is a power of 2 |
| 3674 | if (LHSCst == RHSCst && LHSCC == RHSCC && LHSCC == ICmpInst::ICMP_ULT && |
| 3675 | LHSCst->getValue().isPowerOf2()) { |
| 3676 | Instruction *NewOr = BinaryOperator::CreateOr(Val, Val2); |
| 3677 | InsertNewInstBefore(NewOr, I); |
| 3678 | return new ICmpInst(LHSCC, NewOr, LHSCst); |
| 3679 | } |
| 3680 | |
| 3681 | // From here on, we only handle: |
| 3682 | // (icmp1 A, C1) & (icmp2 A, C2) --> something simpler. |
| 3683 | if (Val != Val2) return 0; |
| 3684 | |
Chris Lattner | 29cd5ba | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 3685 | // ICMP_[US][GL]E X, CST is folded to ICMP_[US][GL]T elsewhere. |
| 3686 | if (LHSCC == ICmpInst::ICMP_UGE || LHSCC == ICmpInst::ICMP_ULE || |
| 3687 | RHSCC == ICmpInst::ICMP_UGE || RHSCC == ICmpInst::ICMP_ULE || |
| 3688 | LHSCC == ICmpInst::ICMP_SGE || LHSCC == ICmpInst::ICMP_SLE || |
| 3689 | RHSCC == ICmpInst::ICMP_SGE || RHSCC == ICmpInst::ICMP_SLE) |
| 3690 | return 0; |
| 3691 | |
| 3692 | // We can't fold (ugt x, C) & (sgt x, C2). |
| 3693 | if (!PredicatesFoldable(LHSCC, RHSCC)) |
| 3694 | return 0; |
| 3695 | |
| 3696 | // Ensure that the larger constant is on the RHS. |
Chris Lattner | aa3e157 | 2008-11-16 05:14:43 +0000 | [diff] [blame] | 3697 | bool ShouldSwap; |
Chris Lattner | 29cd5ba | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 3698 | if (ICmpInst::isSignedPredicate(LHSCC) || |
| 3699 | (ICmpInst::isEquality(LHSCC) && |
| 3700 | ICmpInst::isSignedPredicate(RHSCC))) |
Chris Lattner | aa3e157 | 2008-11-16 05:14:43 +0000 | [diff] [blame] | 3701 | ShouldSwap = LHSCst->getValue().sgt(RHSCst->getValue()); |
Chris Lattner | 29cd5ba | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 3702 | else |
Chris Lattner | aa3e157 | 2008-11-16 05:14:43 +0000 | [diff] [blame] | 3703 | ShouldSwap = LHSCst->getValue().ugt(RHSCst->getValue()); |
| 3704 | |
| 3705 | if (ShouldSwap) { |
Chris Lattner | 29cd5ba | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 3706 | std::swap(LHS, RHS); |
| 3707 | std::swap(LHSCst, RHSCst); |
| 3708 | std::swap(LHSCC, RHSCC); |
| 3709 | } |
| 3710 | |
| 3711 | // At this point, we know we have have two icmp instructions |
| 3712 | // comparing a value against two constants and and'ing the result |
| 3713 | // together. Because of the above check, we know that we only have |
| 3714 | // icmp eq, icmp ne, icmp [su]lt, and icmp [SU]gt here. We also know |
| 3715 | // (from the FoldICmpLogical check above), that the two constants |
| 3716 | // are not equal and that the larger constant is on the RHS |
| 3717 | assert(LHSCst != RHSCst && "Compares not folded above?"); |
| 3718 | |
| 3719 | switch (LHSCC) { |
| 3720 | default: assert(0 && "Unknown integer condition code!"); |
| 3721 | case ICmpInst::ICMP_EQ: |
| 3722 | switch (RHSCC) { |
| 3723 | default: assert(0 && "Unknown integer condition code!"); |
| 3724 | case ICmpInst::ICMP_EQ: // (X == 13 & X == 15) -> false |
| 3725 | case ICmpInst::ICMP_UGT: // (X == 13 & X > 15) -> false |
| 3726 | case ICmpInst::ICMP_SGT: // (X == 13 & X > 15) -> false |
| 3727 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
| 3728 | case ICmpInst::ICMP_NE: // (X == 13 & X != 15) -> X == 13 |
| 3729 | case ICmpInst::ICMP_ULT: // (X == 13 & X < 15) -> X == 13 |
| 3730 | case ICmpInst::ICMP_SLT: // (X == 13 & X < 15) -> X == 13 |
| 3731 | return ReplaceInstUsesWith(I, LHS); |
| 3732 | } |
| 3733 | case ICmpInst::ICMP_NE: |
| 3734 | switch (RHSCC) { |
| 3735 | default: assert(0 && "Unknown integer condition code!"); |
| 3736 | case ICmpInst::ICMP_ULT: |
| 3737 | if (LHSCst == SubOne(RHSCst)) // (X != 13 & X u< 14) -> X < 13 |
| 3738 | return new ICmpInst(ICmpInst::ICMP_ULT, Val, LHSCst); |
| 3739 | break; // (X != 13 & X u< 15) -> no change |
| 3740 | case ICmpInst::ICMP_SLT: |
| 3741 | if (LHSCst == SubOne(RHSCst)) // (X != 13 & X s< 14) -> X < 13 |
| 3742 | return new ICmpInst(ICmpInst::ICMP_SLT, Val, LHSCst); |
| 3743 | break; // (X != 13 & X s< 15) -> no change |
| 3744 | case ICmpInst::ICMP_EQ: // (X != 13 & X == 15) -> X == 15 |
| 3745 | case ICmpInst::ICMP_UGT: // (X != 13 & X u> 15) -> X u> 15 |
| 3746 | case ICmpInst::ICMP_SGT: // (X != 13 & X s> 15) -> X s> 15 |
| 3747 | return ReplaceInstUsesWith(I, RHS); |
| 3748 | case ICmpInst::ICMP_NE: |
| 3749 | if (LHSCst == SubOne(RHSCst)){// (X != 13 & X != 14) -> X-13 >u 1 |
| 3750 | Constant *AddCST = ConstantExpr::getNeg(LHSCst); |
| 3751 | Instruction *Add = BinaryOperator::CreateAdd(Val, AddCST, |
| 3752 | Val->getName()+".off"); |
| 3753 | InsertNewInstBefore(Add, I); |
| 3754 | return new ICmpInst(ICmpInst::ICMP_UGT, Add, |
| 3755 | ConstantInt::get(Add->getType(), 1)); |
| 3756 | } |
| 3757 | break; // (X != 13 & X != 15) -> no change |
| 3758 | } |
| 3759 | break; |
| 3760 | case ICmpInst::ICMP_ULT: |
| 3761 | switch (RHSCC) { |
| 3762 | default: assert(0 && "Unknown integer condition code!"); |
| 3763 | case ICmpInst::ICMP_EQ: // (X u< 13 & X == 15) -> false |
| 3764 | case ICmpInst::ICMP_UGT: // (X u< 13 & X u> 15) -> false |
| 3765 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
| 3766 | case ICmpInst::ICMP_SGT: // (X u< 13 & X s> 15) -> no change |
| 3767 | break; |
| 3768 | case ICmpInst::ICMP_NE: // (X u< 13 & X != 15) -> X u< 13 |
| 3769 | case ICmpInst::ICMP_ULT: // (X u< 13 & X u< 15) -> X u< 13 |
| 3770 | return ReplaceInstUsesWith(I, LHS); |
| 3771 | case ICmpInst::ICMP_SLT: // (X u< 13 & X s< 15) -> no change |
| 3772 | break; |
| 3773 | } |
| 3774 | break; |
| 3775 | case ICmpInst::ICMP_SLT: |
| 3776 | switch (RHSCC) { |
| 3777 | default: assert(0 && "Unknown integer condition code!"); |
| 3778 | case ICmpInst::ICMP_EQ: // (X s< 13 & X == 15) -> false |
| 3779 | case ICmpInst::ICMP_SGT: // (X s< 13 & X s> 15) -> false |
| 3780 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
| 3781 | case ICmpInst::ICMP_UGT: // (X s< 13 & X u> 15) -> no change |
| 3782 | break; |
| 3783 | case ICmpInst::ICMP_NE: // (X s< 13 & X != 15) -> X < 13 |
| 3784 | case ICmpInst::ICMP_SLT: // (X s< 13 & X s< 15) -> X < 13 |
| 3785 | return ReplaceInstUsesWith(I, LHS); |
| 3786 | case ICmpInst::ICMP_ULT: // (X s< 13 & X u< 15) -> no change |
| 3787 | break; |
| 3788 | } |
| 3789 | break; |
| 3790 | case ICmpInst::ICMP_UGT: |
| 3791 | switch (RHSCC) { |
| 3792 | default: assert(0 && "Unknown integer condition code!"); |
| 3793 | case ICmpInst::ICMP_EQ: // (X u> 13 & X == 15) -> X == 15 |
| 3794 | case ICmpInst::ICMP_UGT: // (X u> 13 & X u> 15) -> X u> 15 |
| 3795 | return ReplaceInstUsesWith(I, RHS); |
| 3796 | case ICmpInst::ICMP_SGT: // (X u> 13 & X s> 15) -> no change |
| 3797 | break; |
| 3798 | case ICmpInst::ICMP_NE: |
| 3799 | if (RHSCst == AddOne(LHSCst)) // (X u> 13 & X != 14) -> X u> 14 |
| 3800 | return new ICmpInst(LHSCC, Val, RHSCst); |
| 3801 | break; // (X u> 13 & X != 15) -> no change |
Chris Lattner | 69d4ced | 2008-11-16 05:20:07 +0000 | [diff] [blame] | 3802 | case ICmpInst::ICMP_ULT: // (X u> 13 & X u< 15) -> (X-14) <u 1 |
Chris Lattner | 29cd5ba | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 3803 | return InsertRangeTest(Val, AddOne(LHSCst), RHSCst, false, true, I); |
| 3804 | case ICmpInst::ICMP_SLT: // (X u> 13 & X s< 15) -> no change |
| 3805 | break; |
| 3806 | } |
| 3807 | break; |
| 3808 | case ICmpInst::ICMP_SGT: |
| 3809 | switch (RHSCC) { |
| 3810 | default: assert(0 && "Unknown integer condition code!"); |
| 3811 | case ICmpInst::ICMP_EQ: // (X s> 13 & X == 15) -> X == 15 |
| 3812 | case ICmpInst::ICMP_SGT: // (X s> 13 & X s> 15) -> X s> 15 |
| 3813 | return ReplaceInstUsesWith(I, RHS); |
| 3814 | case ICmpInst::ICMP_UGT: // (X s> 13 & X u> 15) -> no change |
| 3815 | break; |
| 3816 | case ICmpInst::ICMP_NE: |
| 3817 | if (RHSCst == AddOne(LHSCst)) // (X s> 13 & X != 14) -> X s> 14 |
| 3818 | return new ICmpInst(LHSCC, Val, RHSCst); |
| 3819 | break; // (X s> 13 & X != 15) -> no change |
Chris Lattner | 69d4ced | 2008-11-16 05:20:07 +0000 | [diff] [blame] | 3820 | case ICmpInst::ICMP_SLT: // (X s> 13 & X s< 15) -> (X-14) s< 1 |
Chris Lattner | 29cd5ba | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 3821 | return InsertRangeTest(Val, AddOne(LHSCst), RHSCst, true, true, I); |
| 3822 | case ICmpInst::ICMP_ULT: // (X s> 13 & X u< 15) -> no change |
| 3823 | break; |
| 3824 | } |
| 3825 | break; |
| 3826 | } |
Chris Lattner | 29cd5ba | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 3827 | |
| 3828 | return 0; |
| 3829 | } |
| 3830 | |
| 3831 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3832 | Instruction *InstCombiner::visitAnd(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 3833 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3834 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3835 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3836 | if (isa<UndefValue>(Op1)) // X & undef -> 0 |
| 3837 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 3838 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3839 | // and X, X = X |
| 3840 | if (Op0 == Op1) |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 3841 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3842 | |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3843 | // 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] | 3844 | // purpose is to compute bits we don't care about. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 3845 | if (!isa<VectorType>(I.getType())) { |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 3846 | if (SimplifyDemandedInstructionBits(I)) |
Reid Spencer | 6eb0d99 | 2007-03-26 23:58:26 +0000 | [diff] [blame] | 3847 | return &I; |
Chris Lattner | 696ee0a | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3848 | } else { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 3849 | if (ConstantVector *CP = dyn_cast<ConstantVector>(Op1)) { |
Chris Lattner | 041a6c9 | 2007-06-15 05:26:55 +0000 | [diff] [blame] | 3850 | if (CP->isAllOnesValue()) // X & <-1,-1> -> X |
Chris Lattner | 696ee0a | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3851 | return ReplaceInstUsesWith(I, I.getOperand(0)); |
Chris Lattner | 041a6c9 | 2007-06-15 05:26:55 +0000 | [diff] [blame] | 3852 | } else if (isa<ConstantAggregateZero>(Op1)) { |
| 3853 | return ReplaceInstUsesWith(I, Op1); // X & <0,0> -> <0,0> |
Chris Lattner | 696ee0a | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3854 | } |
| 3855 | } |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 3856 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3857 | if (ConstantInt *AndRHS = dyn_cast<ConstantInt>(Op1)) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3858 | const APInt& AndRHSMask = AndRHS->getValue(); |
| 3859 | APInt NotAndRHS(~AndRHSMask); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3860 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3861 | // Optimize a variety of ((val OP C1) & C2) combinations... |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3862 | if (isa<BinaryOperator>(Op0)) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3863 | Instruction *Op0I = cast<Instruction>(Op0); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3864 | Value *Op0LHS = Op0I->getOperand(0); |
| 3865 | Value *Op0RHS = Op0I->getOperand(1); |
| 3866 | switch (Op0I->getOpcode()) { |
| 3867 | case Instruction::Xor: |
| 3868 | case Instruction::Or: |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 3869 | // If the mask is only needed on one incoming arm, push it up. |
| 3870 | if (Op0I->hasOneUse()) { |
| 3871 | if (MaskedValueIsZero(Op0LHS, NotAndRHS)) { |
| 3872 | // Not masking anything out for the LHS, move to RHS. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3873 | Instruction *NewRHS = BinaryOperator::CreateAnd(Op0RHS, AndRHS, |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 3874 | Op0RHS->getName()+".masked"); |
| 3875 | InsertNewInstBefore(NewRHS, I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3876 | return BinaryOperator::Create( |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 3877 | cast<BinaryOperator>(Op0I)->getOpcode(), Op0LHS, NewRHS); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3878 | } |
Chris Lattner | 3bedbd9 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3879 | if (!isa<Constant>(Op0RHS) && |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 3880 | MaskedValueIsZero(Op0RHS, NotAndRHS)) { |
| 3881 | // Not masking anything out for the RHS, move to LHS. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3882 | Instruction *NewLHS = BinaryOperator::CreateAnd(Op0LHS, AndRHS, |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 3883 | Op0LHS->getName()+".masked"); |
| 3884 | InsertNewInstBefore(NewLHS, I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3885 | return BinaryOperator::Create( |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 3886 | cast<BinaryOperator>(Op0I)->getOpcode(), NewLHS, Op0RHS); |
| 3887 | } |
| 3888 | } |
| 3889 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3890 | break; |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3891 | case Instruction::Add: |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3892 | // ((A & N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == AndRHS. |
| 3893 | // ((A | N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0 |
| 3894 | // ((A ^ N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0 |
| 3895 | if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, false, I)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3896 | return BinaryOperator::CreateAnd(V, AndRHS); |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3897 | if (Value *V = FoldLogicalPlusAnd(Op0RHS, Op0LHS, AndRHS, false, I)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3898 | return BinaryOperator::CreateAnd(V, AndRHS); // Add commutes |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3899 | break; |
| 3900 | |
| 3901 | case Instruction::Sub: |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3902 | // ((A & N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == AndRHS. |
| 3903 | // ((A | N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0 |
| 3904 | // ((A ^ N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0 |
| 3905 | if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, true, I)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3906 | return BinaryOperator::CreateAnd(V, AndRHS); |
Nick Lewycky | b4d1bc9 | 2008-07-09 04:32:37 +0000 | [diff] [blame] | 3907 | |
Nick Lewycky | 5dcc41f | 2008-07-10 05:51:40 +0000 | [diff] [blame] | 3908 | // (A - N) & AndRHS -> -N & AndRHS iff A&AndRHS==0 and AndRHS |
| 3909 | // has 1's for all bits that the subtraction with A might affect. |
| 3910 | if (Op0I->hasOneUse()) { |
| 3911 | uint32_t BitWidth = AndRHSMask.getBitWidth(); |
| 3912 | uint32_t Zeros = AndRHSMask.countLeadingZeros(); |
| 3913 | APInt Mask = APInt::getLowBitsSet(BitWidth, BitWidth - Zeros); |
| 3914 | |
Nick Lewycky | b4d1bc9 | 2008-07-09 04:32:37 +0000 | [diff] [blame] | 3915 | ConstantInt *A = dyn_cast<ConstantInt>(Op0LHS); |
Nick Lewycky | 5dcc41f | 2008-07-10 05:51:40 +0000 | [diff] [blame] | 3916 | if (!(A && A->isZero()) && // avoid infinite recursion. |
| 3917 | MaskedValueIsZero(Op0LHS, Mask)) { |
Nick Lewycky | b4d1bc9 | 2008-07-09 04:32:37 +0000 | [diff] [blame] | 3918 | Instruction *NewNeg = BinaryOperator::CreateNeg(Op0RHS); |
| 3919 | InsertNewInstBefore(NewNeg, I); |
| 3920 | return BinaryOperator::CreateAnd(NewNeg, AndRHS); |
| 3921 | } |
| 3922 | } |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3923 | break; |
Nick Lewycky | d1f77bf | 2008-07-09 05:20:13 +0000 | [diff] [blame] | 3924 | |
| 3925 | case Instruction::Shl: |
| 3926 | case Instruction::LShr: |
| 3927 | // (1 << x) & 1 --> zext(x == 0) |
| 3928 | // (1 >> x) & 1 --> zext(x == 0) |
Nick Lewycky | d8ad492 | 2008-07-09 07:35:26 +0000 | [diff] [blame] | 3929 | if (AndRHSMask == 1 && Op0LHS == AndRHS) { |
Nick Lewycky | d1f77bf | 2008-07-09 05:20:13 +0000 | [diff] [blame] | 3930 | Instruction *NewICmp = new ICmpInst(ICmpInst::ICMP_EQ, Op0RHS, |
| 3931 | Constant::getNullValue(I.getType())); |
| 3932 | InsertNewInstBefore(NewICmp, I); |
| 3933 | return new ZExtInst(NewICmp, I.getType()); |
| 3934 | } |
| 3935 | break; |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3936 | } |
| 3937 | |
Chris Lattner | 5840326 | 2003-07-23 19:25:52 +0000 | [diff] [blame] | 3938 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3939 | if (Instruction *Res = OptAndOp(Op0I, Op0CI, AndRHS, I)) |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3940 | return Res; |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3941 | } else if (CastInst *CI = dyn_cast<CastInst>(Op0)) { |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3942 | // If this is an integer truncation or change from signed-to-unsigned, and |
| 3943 | // if the source is an and/or with immediate, transform it. This |
| 3944 | // frequently occurs for bitfield accesses. |
| 3945 | if (Instruction *CastOp = dyn_cast<Instruction>(CI->getOperand(0))) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3946 | if ((isa<TruncInst>(CI) || isa<BitCastInst>(CI)) && |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3947 | CastOp->getNumOperands() == 2) |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 3948 | if (ConstantInt *AndCI = dyn_cast<ConstantInt>(CastOp->getOperand(1))) { |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3949 | if (CastOp->getOpcode() == Instruction::And) { |
| 3950 | // Change: and (cast (and X, C1) to T), C2 |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3951 | // into : and (cast X to T), trunc_or_bitcast(C1)&C2 |
| 3952 | // This will fold the two constants together, which may allow |
| 3953 | // other simplifications. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3954 | Instruction *NewCast = CastInst::CreateTruncOrBitCast( |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 3955 | CastOp->getOperand(0), I.getType(), |
| 3956 | CastOp->getName()+".shrunk"); |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3957 | NewCast = InsertNewInstBefore(NewCast, I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3958 | // trunc_or_bitcast(C1)&C2 |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 3959 | Constant *C3 = ConstantExpr::getTruncOrBitCast(AndCI,I.getType()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3960 | C3 = ConstantExpr::getAnd(C3, AndRHS); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3961 | return BinaryOperator::CreateAnd(NewCast, C3); |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3962 | } else if (CastOp->getOpcode() == Instruction::Or) { |
| 3963 | // Change: and (cast (or X, C1) to T), C2 |
| 3964 | // into : trunc(C1)&C2 iff trunc(C1)&C2 == C2 |
Chris Lattner | bb4e7b2 | 2006-12-12 19:11:20 +0000 | [diff] [blame] | 3965 | Constant *C3 = ConstantExpr::getTruncOrBitCast(AndCI,I.getType()); |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3966 | if (ConstantExpr::getAnd(C3, AndRHS) == AndRHS) // trunc(C1)&C2 |
| 3967 | return ReplaceInstUsesWith(I, AndRHS); |
| 3968 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 3969 | } |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3970 | } |
Chris Lattner | 06782f8 | 2003-07-23 19:36:21 +0000 | [diff] [blame] | 3971 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3972 | |
| 3973 | // Try to fold constant and into select arguments. |
| 3974 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3975 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3976 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 3977 | if (isa<PHINode>(Op0)) |
| 3978 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 3979 | return NV; |
Chris Lattner | c6a8aff | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 3980 | } |
| 3981 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 3982 | Value *Op0NotVal = dyn_castNotVal(Op0); |
| 3983 | Value *Op1NotVal = dyn_castNotVal(Op1); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3984 | |
Chris Lattner | 5b62aa7 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 3985 | if (Op0NotVal == Op1 || Op1NotVal == Op0) // A & ~A == ~A & A == 0 |
| 3986 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 3987 | |
Misha Brukman | cb6267b | 2004-07-30 12:50:08 +0000 | [diff] [blame] | 3988 | // (~A & ~B) == (~(A | B)) - De Morgan's Law |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 3989 | if (Op0NotVal && Op1NotVal && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3990 | Instruction *Or = BinaryOperator::CreateOr(Op0NotVal, Op1NotVal, |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3991 | I.getName()+".demorgan"); |
Chris Lattner | c6a8aff | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 3992 | InsertNewInstBefore(Or, I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3993 | return BinaryOperator::CreateNot(Or); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3994 | } |
Chris Lattner | 2082ad9 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3995 | |
| 3996 | { |
Chris Lattner | 003b620 | 2007-06-15 05:58:24 +0000 | [diff] [blame] | 3997 | Value *A = 0, *B = 0, *C = 0, *D = 0; |
| 3998 | if (match(Op0, m_Or(m_Value(A), m_Value(B)))) { |
Chris Lattner | 2082ad9 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3999 | if (A == Op1 || B == Op1) // (A | ?) & A --> A |
| 4000 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 003b620 | 2007-06-15 05:58:24 +0000 | [diff] [blame] | 4001 | |
| 4002 | // (A|B) & ~(A&B) -> A^B |
| 4003 | if (match(Op1, m_Not(m_And(m_Value(C), m_Value(D))))) { |
| 4004 | if ((A == C && B == D) || (A == D && B == C)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4005 | return BinaryOperator::CreateXor(A, B); |
Chris Lattner | 003b620 | 2007-06-15 05:58:24 +0000 | [diff] [blame] | 4006 | } |
| 4007 | } |
| 4008 | |
| 4009 | if (match(Op1, m_Or(m_Value(A), m_Value(B)))) { |
Chris Lattner | 2082ad9 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 4010 | if (A == Op0 || B == Op0) // A & (A | ?) --> A |
| 4011 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 003b620 | 2007-06-15 05:58:24 +0000 | [diff] [blame] | 4012 | |
| 4013 | // ~(A&B) & (A|B) -> A^B |
| 4014 | if (match(Op0, m_Not(m_And(m_Value(C), m_Value(D))))) { |
| 4015 | if ((A == C && B == D) || (A == D && B == C)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4016 | return BinaryOperator::CreateXor(A, B); |
Chris Lattner | 003b620 | 2007-06-15 05:58:24 +0000 | [diff] [blame] | 4017 | } |
| 4018 | } |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4019 | |
| 4020 | if (Op0->hasOneUse() && |
| 4021 | match(Op0, m_Xor(m_Value(A), m_Value(B)))) { |
| 4022 | if (A == Op1) { // (A^B)&A -> A&(A^B) |
| 4023 | I.swapOperands(); // Simplify below |
| 4024 | std::swap(Op0, Op1); |
| 4025 | } else if (B == Op1) { // (A^B)&B -> B&(B^A) |
| 4026 | cast<BinaryOperator>(Op0)->swapOperands(); |
| 4027 | I.swapOperands(); // Simplify below |
| 4028 | std::swap(Op0, Op1); |
| 4029 | } |
| 4030 | } |
Bill Wendling | 7f0ef6b | 2008-11-30 13:08:13 +0000 | [diff] [blame] | 4031 | |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4032 | if (Op1->hasOneUse() && |
| 4033 | match(Op1, m_Xor(m_Value(A), m_Value(B)))) { |
| 4034 | if (B == Op0) { // B&(A^B) -> B&(B^A) |
| 4035 | cast<BinaryOperator>(Op1)->swapOperands(); |
| 4036 | std::swap(A, B); |
| 4037 | } |
| 4038 | if (A == Op0) { // A&(A^B) -> A & ~B |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4039 | Instruction *NotB = BinaryOperator::CreateNot(B, "tmp"); |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4040 | InsertNewInstBefore(NotB, I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4041 | return BinaryOperator::CreateAnd(A, NotB); |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4042 | } |
| 4043 | } |
Bill Wendling | 7f0ef6b | 2008-11-30 13:08:13 +0000 | [diff] [blame] | 4044 | |
| 4045 | // (A&((~A)|B)) -> A&B |
Chris Lattner | d8aafcb | 2008-12-01 05:16:26 +0000 | [diff] [blame] | 4046 | if (match(Op0, m_Or(m_Not(m_Specific(Op1)), m_Value(A))) || |
| 4047 | match(Op0, m_Or(m_Value(A), m_Not(m_Specific(Op1))))) |
| 4048 | return BinaryOperator::CreateAnd(A, Op1); |
| 4049 | if (match(Op1, m_Or(m_Not(m_Specific(Op0)), m_Value(A))) || |
| 4050 | match(Op1, m_Or(m_Value(A), m_Not(m_Specific(Op0))))) |
| 4051 | return BinaryOperator::CreateAnd(A, Op0); |
Chris Lattner | 2082ad9 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 4052 | } |
| 4053 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4054 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(Op1)) { |
| 4055 | // (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B) |
| 4056 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS))) |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 4057 | return R; |
| 4058 | |
Chris Lattner | 29cd5ba | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 4059 | if (ICmpInst *LHS = dyn_cast<ICmpInst>(Op0)) |
| 4060 | if (Instruction *Res = FoldAndOfICmps(I, LHS, RHS)) |
| 4061 | return Res; |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 4062 | } |
| 4063 | |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4064 | // fold (and (cast A), (cast B)) -> (cast (and A, B)) |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4065 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) |
| 4066 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) |
| 4067 | if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind ? |
| 4068 | const Type *SrcTy = Op0C->getOperand(0)->getType(); |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 4069 | if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() && |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4070 | // 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] | 4071 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), |
| 4072 | I.getType(), TD) && |
| 4073 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), |
| 4074 | I.getType(), TD)) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4075 | Instruction *NewOp = BinaryOperator::CreateAnd(Op0C->getOperand(0), |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4076 | Op1C->getOperand(0), |
| 4077 | I.getName()); |
| 4078 | InsertNewInstBefore(NewOp, I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4079 | return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType()); |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4080 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4081 | } |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4082 | |
| 4083 | // (X >> Z) & (Y >> Z) -> (X&Y) >> Z for all shifts. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4084 | if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) { |
| 4085 | if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0)) |
| 4086 | if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() && |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4087 | SI0->getOperand(1) == SI1->getOperand(1) && |
| 4088 | (SI0->hasOneUse() || SI1->hasOneUse())) { |
| 4089 | Instruction *NewOp = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4090 | InsertNewInstBefore(BinaryOperator::CreateAnd(SI0->getOperand(0), |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4091 | SI1->getOperand(0), |
| 4092 | SI0->getName()), I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4093 | return BinaryOperator::Create(SI1->getOpcode(), NewOp, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4094 | SI1->getOperand(1)); |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4095 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4096 | } |
| 4097 | |
Evan Cheng | 8db9072 | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 4098 | // If and'ing two fcmp, try combine them into one. |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4099 | if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0))) { |
| 4100 | if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1))) { |
| 4101 | if (LHS->getPredicate() == FCmpInst::FCMP_ORD && |
Evan Cheng | 8db9072 | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 4102 | RHS->getPredicate() == FCmpInst::FCMP_ORD) { |
| 4103 | // (fcmp ord x, c) & (fcmp ord y, c) -> (fcmp ord x, y) |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4104 | if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1))) |
| 4105 | if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) { |
| 4106 | // If either of the constants are nans, then the whole thing returns |
| 4107 | // false. |
Chris Lattner | be3e348 | 2007-10-24 18:54:45 +0000 | [diff] [blame] | 4108 | if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN()) |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4109 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
| 4110 | return new FCmpInst(FCmpInst::FCMP_ORD, LHS->getOperand(0), |
| 4111 | RHS->getOperand(0)); |
| 4112 | } |
Evan Cheng | 8db9072 | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 4113 | } else { |
| 4114 | Value *Op0LHS, *Op0RHS, *Op1LHS, *Op1RHS; |
| 4115 | FCmpInst::Predicate Op0CC, Op1CC; |
| 4116 | if (match(Op0, m_FCmp(Op0CC, m_Value(Op0LHS), m_Value(Op0RHS))) && |
| 4117 | match(Op1, m_FCmp(Op1CC, m_Value(Op1LHS), m_Value(Op1RHS)))) { |
Evan Cheng | 4990b25 | 2008-10-14 18:13:38 +0000 | [diff] [blame] | 4118 | if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) { |
| 4119 | // Swap RHS operands to match LHS. |
| 4120 | Op1CC = FCmpInst::getSwappedPredicate(Op1CC); |
| 4121 | std::swap(Op1LHS, Op1RHS); |
| 4122 | } |
Evan Cheng | 8db9072 | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 4123 | if (Op0LHS == Op1LHS && Op0RHS == Op1RHS) { |
| 4124 | // Simplify (fcmp cc0 x, y) & (fcmp cc1 x, y). |
| 4125 | if (Op0CC == Op1CC) |
| 4126 | return new FCmpInst((FCmpInst::Predicate)Op0CC, Op0LHS, Op0RHS); |
| 4127 | else if (Op0CC == FCmpInst::FCMP_FALSE || |
| 4128 | Op1CC == FCmpInst::FCMP_FALSE) |
| 4129 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
| 4130 | else if (Op0CC == FCmpInst::FCMP_TRUE) |
| 4131 | return ReplaceInstUsesWith(I, Op1); |
| 4132 | else if (Op1CC == FCmpInst::FCMP_TRUE) |
| 4133 | return ReplaceInstUsesWith(I, Op0); |
| 4134 | bool Op0Ordered; |
| 4135 | bool Op1Ordered; |
| 4136 | unsigned Op0Pred = getFCmpCode(Op0CC, Op0Ordered); |
| 4137 | unsigned Op1Pred = getFCmpCode(Op1CC, Op1Ordered); |
| 4138 | if (Op1Pred == 0) { |
| 4139 | std::swap(Op0, Op1); |
| 4140 | std::swap(Op0Pred, Op1Pred); |
| 4141 | std::swap(Op0Ordered, Op1Ordered); |
| 4142 | } |
| 4143 | if (Op0Pred == 0) { |
| 4144 | // uno && ueq -> uno && (uno || eq) -> ueq |
| 4145 | // ord && olt -> ord && (ord && lt) -> olt |
| 4146 | if (Op0Ordered == Op1Ordered) |
| 4147 | return ReplaceInstUsesWith(I, Op1); |
| 4148 | // uno && oeq -> uno && (ord && eq) -> false |
| 4149 | // uno && ord -> false |
| 4150 | if (!Op0Ordered) |
| 4151 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
| 4152 | // ord && ueq -> ord && (uno || eq) -> oeq |
| 4153 | return cast<Instruction>(getFCmpValue(true, Op1Pred, |
| 4154 | Op0LHS, Op0RHS)); |
| 4155 | } |
| 4156 | } |
| 4157 | } |
| 4158 | } |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4159 | } |
| 4160 | } |
Nick Lewycky | b4d1bc9 | 2008-07-09 04:32:37 +0000 | [diff] [blame] | 4161 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4162 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4163 | } |
| 4164 | |
Chris Lattner | 8c34cd2 | 2008-10-05 02:13:19 +0000 | [diff] [blame] | 4165 | /// CollectBSwapParts - Analyze the specified subexpression and see if it is |
| 4166 | /// capable of providing pieces of a bswap. The subexpression provides pieces |
| 4167 | /// of a bswap if it is proven that each of the non-zero bytes in the output of |
| 4168 | /// the expression came from the corresponding "byte swapped" byte in some other |
| 4169 | /// value. For example, if the current subexpression is "(shl i32 %X, 24)" then |
| 4170 | /// we know that the expression deposits the low byte of %X into the high byte |
| 4171 | /// of the bswap result and that all other bytes are zero. This expression is |
| 4172 | /// accepted, the high byte of ByteValues is set to X to indicate a correct |
| 4173 | /// match. |
| 4174 | /// |
| 4175 | /// This function returns true if the match was unsuccessful and false if so. |
| 4176 | /// On entry to the function the "OverallLeftShift" is a signed integer value |
| 4177 | /// indicating the number of bytes that the subexpression is later shifted. For |
| 4178 | /// example, if the expression is later right shifted by 16 bits, the |
| 4179 | /// OverallLeftShift value would be -2 on entry. This is used to specify which |
| 4180 | /// byte of ByteValues is actually being set. |
| 4181 | /// |
| 4182 | /// Similarly, ByteMask is a bitmask where a bit is clear if its corresponding |
| 4183 | /// byte is masked to zero by a user. For example, in (X & 255), X will be |
| 4184 | /// processed with a bytemask of 1. Because bytemask is 32-bits, this limits |
| 4185 | /// this function to working on up to 32-byte (256 bit) values. ByteMask is |
| 4186 | /// always in the local (OverallLeftShift) coordinate space. |
| 4187 | /// |
| 4188 | static bool CollectBSwapParts(Value *V, int OverallLeftShift, uint32_t ByteMask, |
| 4189 | SmallVector<Value*, 8> &ByteValues) { |
| 4190 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
| 4191 | // If this is an or instruction, it may be an inner node of the bswap. |
| 4192 | if (I->getOpcode() == Instruction::Or) { |
| 4193 | return CollectBSwapParts(I->getOperand(0), OverallLeftShift, ByteMask, |
| 4194 | ByteValues) || |
| 4195 | CollectBSwapParts(I->getOperand(1), OverallLeftShift, ByteMask, |
| 4196 | ByteValues); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4197 | } |
Chris Lattner | 8c34cd2 | 2008-10-05 02:13:19 +0000 | [diff] [blame] | 4198 | |
| 4199 | // If this is a logical shift by a constant multiple of 8, recurse with |
| 4200 | // OverallLeftShift and ByteMask adjusted. |
| 4201 | if (I->isLogicalShift() && isa<ConstantInt>(I->getOperand(1))) { |
| 4202 | unsigned ShAmt = |
| 4203 | cast<ConstantInt>(I->getOperand(1))->getLimitedValue(~0U); |
| 4204 | // Ensure the shift amount is defined and of a byte value. |
| 4205 | if ((ShAmt & 7) || (ShAmt > 8*ByteValues.size())) |
| 4206 | return true; |
| 4207 | |
| 4208 | unsigned ByteShift = ShAmt >> 3; |
| 4209 | if (I->getOpcode() == Instruction::Shl) { |
| 4210 | // X << 2 -> collect(X, +2) |
| 4211 | OverallLeftShift += ByteShift; |
| 4212 | ByteMask >>= ByteShift; |
| 4213 | } else { |
| 4214 | // X >>u 2 -> collect(X, -2) |
| 4215 | OverallLeftShift -= ByteShift; |
| 4216 | ByteMask <<= ByteShift; |
Chris Lattner | de17ddc | 2008-10-08 06:42:28 +0000 | [diff] [blame] | 4217 | ByteMask &= (~0U >> (32-ByteValues.size())); |
Chris Lattner | 8c34cd2 | 2008-10-05 02:13:19 +0000 | [diff] [blame] | 4218 | } |
| 4219 | |
| 4220 | if (OverallLeftShift >= (int)ByteValues.size()) return true; |
| 4221 | if (OverallLeftShift <= -(int)ByteValues.size()) return true; |
| 4222 | |
| 4223 | return CollectBSwapParts(I->getOperand(0), OverallLeftShift, ByteMask, |
| 4224 | ByteValues); |
| 4225 | } |
| 4226 | |
| 4227 | // If this is a logical 'and' with a mask that clears bytes, clear the |
| 4228 | // corresponding bytes in ByteMask. |
| 4229 | if (I->getOpcode() == Instruction::And && |
| 4230 | isa<ConstantInt>(I->getOperand(1))) { |
| 4231 | // Scan every byte of the and mask, seeing if the byte is either 0 or 255. |
| 4232 | unsigned NumBytes = ByteValues.size(); |
| 4233 | APInt Byte(I->getType()->getPrimitiveSizeInBits(), 255); |
| 4234 | const APInt &AndMask = cast<ConstantInt>(I->getOperand(1))->getValue(); |
| 4235 | |
| 4236 | for (unsigned i = 0; i != NumBytes; ++i, Byte <<= 8) { |
| 4237 | // If this byte is masked out by a later operation, we don't care what |
| 4238 | // the and mask is. |
| 4239 | if ((ByteMask & (1 << i)) == 0) |
| 4240 | continue; |
| 4241 | |
| 4242 | // If the AndMask is all zeros for this byte, clear the bit. |
| 4243 | APInt MaskB = AndMask & Byte; |
| 4244 | if (MaskB == 0) { |
| 4245 | ByteMask &= ~(1U << i); |
| 4246 | continue; |
| 4247 | } |
| 4248 | |
| 4249 | // If the AndMask is not all ones for this byte, it's not a bytezap. |
| 4250 | if (MaskB != Byte) |
| 4251 | return true; |
| 4252 | |
| 4253 | // Otherwise, this byte is kept. |
| 4254 | } |
| 4255 | |
| 4256 | return CollectBSwapParts(I->getOperand(0), OverallLeftShift, ByteMask, |
| 4257 | ByteValues); |
| 4258 | } |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4259 | } |
| 4260 | |
Chris Lattner | 8c34cd2 | 2008-10-05 02:13:19 +0000 | [diff] [blame] | 4261 | // Okay, we got to something that isn't a shift, 'or' or 'and'. This must be |
| 4262 | // the input value to the bswap. Some observations: 1) if more than one byte |
| 4263 | // is demanded from this input, then it could not be successfully assembled |
| 4264 | // into a byteswap. At least one of the two bytes would not be aligned with |
| 4265 | // their ultimate destination. |
| 4266 | if (!isPowerOf2_32(ByteMask)) return true; |
| 4267 | unsigned InputByteNo = CountTrailingZeros_32(ByteMask); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4268 | |
Chris Lattner | 8c34cd2 | 2008-10-05 02:13:19 +0000 | [diff] [blame] | 4269 | // 2) The input and ultimate destinations must line up: if byte 3 of an i32 |
| 4270 | // is demanded, it needs to go into byte 0 of the result. This means that the |
| 4271 | // byte needs to be shifted until it lands in the right byte bucket. The |
| 4272 | // shift amount depends on the position: if the byte is coming from the high |
| 4273 | // part of the value (e.g. byte 3) then it must be shifted right. If from the |
| 4274 | // low part, it must be shifted left. |
| 4275 | unsigned DestByteNo = InputByteNo + OverallLeftShift; |
| 4276 | if (InputByteNo < ByteValues.size()/2) { |
| 4277 | if (ByteValues.size()-1-DestByteNo != InputByteNo) |
| 4278 | return true; |
| 4279 | } else { |
| 4280 | if (ByteValues.size()-1-DestByteNo != InputByteNo) |
| 4281 | return true; |
| 4282 | } |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4283 | |
| 4284 | // If the destination byte value is already defined, the values are or'd |
| 4285 | // together, which isn't a bswap (unless it's an or of the same bits). |
Chris Lattner | 8c34cd2 | 2008-10-05 02:13:19 +0000 | [diff] [blame] | 4286 | if (ByteValues[DestByteNo] && ByteValues[DestByteNo] != V) |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4287 | return true; |
Chris Lattner | 8c34cd2 | 2008-10-05 02:13:19 +0000 | [diff] [blame] | 4288 | ByteValues[DestByteNo] = V; |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4289 | return false; |
| 4290 | } |
| 4291 | |
| 4292 | /// MatchBSwap - Given an OR instruction, check to see if this is a bswap idiom. |
| 4293 | /// If so, insert the new bswap intrinsic and return it. |
| 4294 | Instruction *InstCombiner::MatchBSwap(BinaryOperator &I) { |
Chris Lattner | 55fc8c4 | 2007-04-01 20:57:36 +0000 | [diff] [blame] | 4295 | const IntegerType *ITy = dyn_cast<IntegerType>(I.getType()); |
Chris Lattner | 8c34cd2 | 2008-10-05 02:13:19 +0000 | [diff] [blame] | 4296 | if (!ITy || ITy->getBitWidth() % 16 || |
| 4297 | // ByteMask only allows up to 32-byte values. |
| 4298 | ITy->getBitWidth() > 32*8) |
Chris Lattner | 55fc8c4 | 2007-04-01 20:57:36 +0000 | [diff] [blame] | 4299 | return 0; // Can only bswap pairs of bytes. Can't do vectors. |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4300 | |
| 4301 | /// ByteValues - For each byte of the result, we keep track of which value |
| 4302 | /// defines each byte. |
Chris Lattner | 535014f | 2007-02-15 22:52:10 +0000 | [diff] [blame] | 4303 | SmallVector<Value*, 8> ByteValues; |
Chris Lattner | 55fc8c4 | 2007-04-01 20:57:36 +0000 | [diff] [blame] | 4304 | ByteValues.resize(ITy->getBitWidth()/8); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4305 | |
| 4306 | // Try to find all the pieces corresponding to the bswap. |
Chris Lattner | 8c34cd2 | 2008-10-05 02:13:19 +0000 | [diff] [blame] | 4307 | uint32_t ByteMask = ~0U >> (32-ByteValues.size()); |
| 4308 | if (CollectBSwapParts(&I, 0, ByteMask, ByteValues)) |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4309 | return 0; |
| 4310 | |
| 4311 | // Check to see if all of the bytes come from the same value. |
| 4312 | Value *V = ByteValues[0]; |
| 4313 | if (V == 0) return 0; // Didn't find a byte? Must be zero. |
| 4314 | |
| 4315 | // Check to make sure that all of the bytes come from the same value. |
| 4316 | for (unsigned i = 1, e = ByteValues.size(); i != e; ++i) |
| 4317 | if (ByteValues[i] != V) |
| 4318 | return 0; |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 4319 | const Type *Tys[] = { ITy }; |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4320 | Module *M = I.getParent()->getParent()->getParent(); |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 4321 | Function *F = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 4322 | return CallInst::Create(F, V); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4323 | } |
| 4324 | |
Chris Lattner | faaf951 | 2008-11-16 04:24:12 +0000 | [diff] [blame] | 4325 | /// MatchSelectFromAndOr - We have an expression of the form (A&C)|(B&D). Check |
| 4326 | /// If A is (cond?-1:0) and either B or D is ~(cond?-1,0) or (cond?0,-1), then |
| 4327 | /// we can simplify this expression to "cond ? C : D or B". |
| 4328 | static Instruction *MatchSelectFromAndOr(Value *A, Value *B, |
| 4329 | Value *C, Value *D) { |
Chris Lattner | a6a474d | 2008-11-16 04:26:55 +0000 | [diff] [blame] | 4330 | // If A is not a select of -1/0, this cannot match. |
Chris Lattner | 6046fb7 | 2008-11-16 04:46:19 +0000 | [diff] [blame] | 4331 | Value *Cond = 0; |
Chris Lattner | 159c35b | 2009-01-05 23:53:12 +0000 | [diff] [blame] | 4332 | if (!match(A, m_SelectCst<-1, 0>(m_Value(Cond)))) |
Chris Lattner | faaf951 | 2008-11-16 04:24:12 +0000 | [diff] [blame] | 4333 | return 0; |
| 4334 | |
Chris Lattner | a6a474d | 2008-11-16 04:26:55 +0000 | [diff] [blame] | 4335 | // ((cond?-1:0)&C) | (B&(cond?0:-1)) -> cond ? C : B. |
Chris Lattner | 159c35b | 2009-01-05 23:53:12 +0000 | [diff] [blame] | 4336 | if (match(D, m_SelectCst<0, -1>(m_Specific(Cond)))) |
Chris Lattner | a6a474d | 2008-11-16 04:26:55 +0000 | [diff] [blame] | 4337 | return SelectInst::Create(Cond, C, B); |
Chris Lattner | 159c35b | 2009-01-05 23:53:12 +0000 | [diff] [blame] | 4338 | if (match(D, m_Not(m_SelectCst<-1, 0>(m_Specific(Cond))))) |
Chris Lattner | a6a474d | 2008-11-16 04:26:55 +0000 | [diff] [blame] | 4339 | return SelectInst::Create(Cond, C, B); |
| 4340 | // ((cond?-1:0)&C) | ((cond?0:-1)&D) -> cond ? C : D. |
Chris Lattner | 159c35b | 2009-01-05 23:53:12 +0000 | [diff] [blame] | 4341 | if (match(B, m_SelectCst<0, -1>(m_Specific(Cond)))) |
Chris Lattner | a6a474d | 2008-11-16 04:26:55 +0000 | [diff] [blame] | 4342 | return SelectInst::Create(Cond, C, D); |
Chris Lattner | 159c35b | 2009-01-05 23:53:12 +0000 | [diff] [blame] | 4343 | if (match(B, m_Not(m_SelectCst<-1, 0>(m_Specific(Cond))))) |
Chris Lattner | a6a474d | 2008-11-16 04:26:55 +0000 | [diff] [blame] | 4344 | return SelectInst::Create(Cond, C, D); |
Chris Lattner | faaf951 | 2008-11-16 04:24:12 +0000 | [diff] [blame] | 4345 | return 0; |
| 4346 | } |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4347 | |
Chris Lattner | 69d4ced | 2008-11-16 05:20:07 +0000 | [diff] [blame] | 4348 | /// FoldOrOfICmps - Fold (icmp)|(icmp) if possible. |
| 4349 | Instruction *InstCombiner::FoldOrOfICmps(Instruction &I, |
| 4350 | ICmpInst *LHS, ICmpInst *RHS) { |
| 4351 | Value *Val, *Val2; |
| 4352 | ConstantInt *LHSCst, *RHSCst; |
| 4353 | ICmpInst::Predicate LHSCC, RHSCC; |
| 4354 | |
| 4355 | // This only handles icmp of constants: (icmp1 A, C1) | (icmp2 B, C2). |
| 4356 | if (!match(LHS, m_ICmp(LHSCC, m_Value(Val), m_ConstantInt(LHSCst))) || |
| 4357 | !match(RHS, m_ICmp(RHSCC, m_Value(Val2), m_ConstantInt(RHSCst)))) |
| 4358 | return 0; |
| 4359 | |
| 4360 | // From here on, we only handle: |
| 4361 | // (icmp1 A, C1) | (icmp2 A, C2) --> something simpler. |
| 4362 | if (Val != Val2) return 0; |
| 4363 | |
| 4364 | // ICMP_[US][GL]E X, CST is folded to ICMP_[US][GL]T elsewhere. |
| 4365 | if (LHSCC == ICmpInst::ICMP_UGE || LHSCC == ICmpInst::ICMP_ULE || |
| 4366 | RHSCC == ICmpInst::ICMP_UGE || RHSCC == ICmpInst::ICMP_ULE || |
| 4367 | LHSCC == ICmpInst::ICMP_SGE || LHSCC == ICmpInst::ICMP_SLE || |
| 4368 | RHSCC == ICmpInst::ICMP_SGE || RHSCC == ICmpInst::ICMP_SLE) |
| 4369 | return 0; |
| 4370 | |
| 4371 | // We can't fold (ugt x, C) | (sgt x, C2). |
| 4372 | if (!PredicatesFoldable(LHSCC, RHSCC)) |
| 4373 | return 0; |
| 4374 | |
| 4375 | // Ensure that the larger constant is on the RHS. |
| 4376 | bool ShouldSwap; |
| 4377 | if (ICmpInst::isSignedPredicate(LHSCC) || |
| 4378 | (ICmpInst::isEquality(LHSCC) && |
| 4379 | ICmpInst::isSignedPredicate(RHSCC))) |
| 4380 | ShouldSwap = LHSCst->getValue().sgt(RHSCst->getValue()); |
| 4381 | else |
| 4382 | ShouldSwap = LHSCst->getValue().ugt(RHSCst->getValue()); |
| 4383 | |
| 4384 | if (ShouldSwap) { |
| 4385 | std::swap(LHS, RHS); |
| 4386 | std::swap(LHSCst, RHSCst); |
| 4387 | std::swap(LHSCC, RHSCC); |
| 4388 | } |
| 4389 | |
| 4390 | // At this point, we know we have have two icmp instructions |
| 4391 | // comparing a value against two constants and or'ing the result |
| 4392 | // together. Because of the above check, we know that we only have |
| 4393 | // ICMP_EQ, ICMP_NE, ICMP_LT, and ICMP_GT here. We also know (from the |
| 4394 | // FoldICmpLogical check above), that the two constants are not |
| 4395 | // equal. |
| 4396 | assert(LHSCst != RHSCst && "Compares not folded above?"); |
| 4397 | |
| 4398 | switch (LHSCC) { |
| 4399 | default: assert(0 && "Unknown integer condition code!"); |
| 4400 | case ICmpInst::ICMP_EQ: |
| 4401 | switch (RHSCC) { |
| 4402 | default: assert(0 && "Unknown integer condition code!"); |
| 4403 | case ICmpInst::ICMP_EQ: |
| 4404 | if (LHSCst == SubOne(RHSCst)) { // (X == 13 | X == 14) -> X-13 <u 2 |
| 4405 | Constant *AddCST = ConstantExpr::getNeg(LHSCst); |
| 4406 | Instruction *Add = BinaryOperator::CreateAdd(Val, AddCST, |
| 4407 | Val->getName()+".off"); |
| 4408 | InsertNewInstBefore(Add, I); |
| 4409 | AddCST = Subtract(AddOne(RHSCst), LHSCst); |
| 4410 | return new ICmpInst(ICmpInst::ICMP_ULT, Add, AddCST); |
| 4411 | } |
| 4412 | break; // (X == 13 | X == 15) -> no change |
| 4413 | case ICmpInst::ICMP_UGT: // (X == 13 | X u> 14) -> no change |
| 4414 | case ICmpInst::ICMP_SGT: // (X == 13 | X s> 14) -> no change |
| 4415 | break; |
| 4416 | case ICmpInst::ICMP_NE: // (X == 13 | X != 15) -> X != 15 |
| 4417 | case ICmpInst::ICMP_ULT: // (X == 13 | X u< 15) -> X u< 15 |
| 4418 | case ICmpInst::ICMP_SLT: // (X == 13 | X s< 15) -> X s< 15 |
| 4419 | return ReplaceInstUsesWith(I, RHS); |
| 4420 | } |
| 4421 | break; |
| 4422 | case ICmpInst::ICMP_NE: |
| 4423 | switch (RHSCC) { |
| 4424 | default: assert(0 && "Unknown integer condition code!"); |
| 4425 | case ICmpInst::ICMP_EQ: // (X != 13 | X == 15) -> X != 13 |
| 4426 | case ICmpInst::ICMP_UGT: // (X != 13 | X u> 15) -> X != 13 |
| 4427 | case ICmpInst::ICMP_SGT: // (X != 13 | X s> 15) -> X != 13 |
| 4428 | return ReplaceInstUsesWith(I, LHS); |
| 4429 | case ICmpInst::ICMP_NE: // (X != 13 | X != 15) -> true |
| 4430 | case ICmpInst::ICMP_ULT: // (X != 13 | X u< 15) -> true |
| 4431 | case ICmpInst::ICMP_SLT: // (X != 13 | X s< 15) -> true |
| 4432 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
| 4433 | } |
| 4434 | break; |
| 4435 | case ICmpInst::ICMP_ULT: |
| 4436 | switch (RHSCC) { |
| 4437 | default: assert(0 && "Unknown integer condition code!"); |
| 4438 | case ICmpInst::ICMP_EQ: // (X u< 13 | X == 14) -> no change |
| 4439 | break; |
| 4440 | case ICmpInst::ICMP_UGT: // (X u< 13 | X u> 15) -> (X-13) u> 2 |
| 4441 | // If RHSCst is [us]MAXINT, it is always false. Not handling |
| 4442 | // this can cause overflow. |
| 4443 | if (RHSCst->isMaxValue(false)) |
| 4444 | return ReplaceInstUsesWith(I, LHS); |
| 4445 | return InsertRangeTest(Val, LHSCst, AddOne(RHSCst), false, false, I); |
| 4446 | case ICmpInst::ICMP_SGT: // (X u< 13 | X s> 15) -> no change |
| 4447 | break; |
| 4448 | case ICmpInst::ICMP_NE: // (X u< 13 | X != 15) -> X != 15 |
| 4449 | case ICmpInst::ICMP_ULT: // (X u< 13 | X u< 15) -> X u< 15 |
| 4450 | return ReplaceInstUsesWith(I, RHS); |
| 4451 | case ICmpInst::ICMP_SLT: // (X u< 13 | X s< 15) -> no change |
| 4452 | break; |
| 4453 | } |
| 4454 | break; |
| 4455 | case ICmpInst::ICMP_SLT: |
| 4456 | switch (RHSCC) { |
| 4457 | default: assert(0 && "Unknown integer condition code!"); |
| 4458 | case ICmpInst::ICMP_EQ: // (X s< 13 | X == 14) -> no change |
| 4459 | break; |
| 4460 | case ICmpInst::ICMP_SGT: // (X s< 13 | X s> 15) -> (X-13) s> 2 |
| 4461 | // If RHSCst is [us]MAXINT, it is always false. Not handling |
| 4462 | // this can cause overflow. |
| 4463 | if (RHSCst->isMaxValue(true)) |
| 4464 | return ReplaceInstUsesWith(I, LHS); |
| 4465 | return InsertRangeTest(Val, LHSCst, AddOne(RHSCst), true, false, I); |
| 4466 | case ICmpInst::ICMP_UGT: // (X s< 13 | X u> 15) -> no change |
| 4467 | break; |
| 4468 | case ICmpInst::ICMP_NE: // (X s< 13 | X != 15) -> X != 15 |
| 4469 | case ICmpInst::ICMP_SLT: // (X s< 13 | X s< 15) -> X s< 15 |
| 4470 | return ReplaceInstUsesWith(I, RHS); |
| 4471 | case ICmpInst::ICMP_ULT: // (X s< 13 | X u< 15) -> no change |
| 4472 | break; |
| 4473 | } |
| 4474 | break; |
| 4475 | case ICmpInst::ICMP_UGT: |
| 4476 | switch (RHSCC) { |
| 4477 | default: assert(0 && "Unknown integer condition code!"); |
| 4478 | case ICmpInst::ICMP_EQ: // (X u> 13 | X == 15) -> X u> 13 |
| 4479 | case ICmpInst::ICMP_UGT: // (X u> 13 | X u> 15) -> X u> 13 |
| 4480 | return ReplaceInstUsesWith(I, LHS); |
| 4481 | case ICmpInst::ICMP_SGT: // (X u> 13 | X s> 15) -> no change |
| 4482 | break; |
| 4483 | case ICmpInst::ICMP_NE: // (X u> 13 | X != 15) -> true |
| 4484 | case ICmpInst::ICMP_ULT: // (X u> 13 | X u< 15) -> true |
| 4485 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
| 4486 | case ICmpInst::ICMP_SLT: // (X u> 13 | X s< 15) -> no change |
| 4487 | break; |
| 4488 | } |
| 4489 | break; |
| 4490 | case ICmpInst::ICMP_SGT: |
| 4491 | switch (RHSCC) { |
| 4492 | default: assert(0 && "Unknown integer condition code!"); |
| 4493 | case ICmpInst::ICMP_EQ: // (X s> 13 | X == 15) -> X > 13 |
| 4494 | case ICmpInst::ICMP_SGT: // (X s> 13 | X s> 15) -> X > 13 |
| 4495 | return ReplaceInstUsesWith(I, LHS); |
| 4496 | case ICmpInst::ICMP_UGT: // (X s> 13 | X u> 15) -> no change |
| 4497 | break; |
| 4498 | case ICmpInst::ICMP_NE: // (X s> 13 | X != 15) -> true |
| 4499 | case ICmpInst::ICMP_SLT: // (X s> 13 | X s< 15) -> true |
| 4500 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
| 4501 | case ICmpInst::ICMP_ULT: // (X s> 13 | X u< 15) -> no change |
| 4502 | break; |
| 4503 | } |
| 4504 | break; |
| 4505 | } |
| 4506 | return 0; |
| 4507 | } |
| 4508 | |
Bill Wendling | a698a47 | 2008-12-01 08:23:25 +0000 | [diff] [blame] | 4509 | /// FoldOrWithConstants - This helper function folds: |
| 4510 | /// |
Bill Wendling | a8bb13f | 2008-12-02 05:09:00 +0000 | [diff] [blame] | 4511 | /// ((A | B) & C1) | (B & C2) |
Bill Wendling | a698a47 | 2008-12-01 08:23:25 +0000 | [diff] [blame] | 4512 | /// |
| 4513 | /// into: |
| 4514 | /// |
Bill Wendling | a8bb13f | 2008-12-02 05:09:00 +0000 | [diff] [blame] | 4515 | /// (A & C1) | B |
Bill Wendling | d54d860 | 2008-12-01 08:32:40 +0000 | [diff] [blame] | 4516 | /// |
Bill Wendling | a8bb13f | 2008-12-02 05:09:00 +0000 | [diff] [blame] | 4517 | /// when the XOR of the two constants is "all ones" (-1). |
Bill Wendling | d54d860 | 2008-12-01 08:32:40 +0000 | [diff] [blame] | 4518 | Instruction *InstCombiner::FoldOrWithConstants(BinaryOperator &I, Value *Op, |
Bill Wendling | a698a47 | 2008-12-01 08:23:25 +0000 | [diff] [blame] | 4519 | Value *A, Value *B, Value *C) { |
Bill Wendling | dda74e0 | 2008-12-02 05:06:43 +0000 | [diff] [blame] | 4520 | ConstantInt *CI1 = dyn_cast<ConstantInt>(C); |
| 4521 | if (!CI1) return 0; |
Bill Wendling | a698a47 | 2008-12-01 08:23:25 +0000 | [diff] [blame] | 4522 | |
Bill Wendling | 286a054 | 2008-12-02 06:24:20 +0000 | [diff] [blame] | 4523 | Value *V1 = 0; |
| 4524 | ConstantInt *CI2 = 0; |
| 4525 | if (!match(Op, m_And(m_Value(V1), m_ConstantInt(CI2)))) return 0; |
Bill Wendling | a698a47 | 2008-12-01 08:23:25 +0000 | [diff] [blame] | 4526 | |
Bill Wendling | 29976b9 | 2008-12-02 06:18:11 +0000 | [diff] [blame] | 4527 | APInt Xor = CI1->getValue() ^ CI2->getValue(); |
| 4528 | if (!Xor.isAllOnesValue()) return 0; |
| 4529 | |
Bill Wendling | 286a054 | 2008-12-02 06:24:20 +0000 | [diff] [blame] | 4530 | if (V1 == A || V1 == B) { |
Bill Wendling | 29976b9 | 2008-12-02 06:18:11 +0000 | [diff] [blame] | 4531 | Instruction *NewOp = |
Bill Wendling | d16c6e9 | 2008-12-02 06:22:04 +0000 | [diff] [blame] | 4532 | InsertNewInstBefore(BinaryOperator::CreateAnd((V1 == A) ? B : A, CI1), I); |
| 4533 | return BinaryOperator::CreateOr(NewOp, V1); |
Bill Wendling | a698a47 | 2008-12-01 08:23:25 +0000 | [diff] [blame] | 4534 | } |
| 4535 | |
| 4536 | return 0; |
| 4537 | } |
| 4538 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4539 | Instruction *InstCombiner::visitOr(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 4540 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4541 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4542 | |
Chris Lattner | 42593e6 | 2007-03-24 23:56:43 +0000 | [diff] [blame] | 4543 | if (isa<UndefValue>(Op1)) // X | undef -> -1 |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4544 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4545 | |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 4546 | // or X, X = X |
| 4547 | if (Op0 == Op1) |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 4548 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4549 | |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 4550 | // See if we can simplify any instructions used by the instruction whose sole |
| 4551 | // purpose is to compute bits we don't care about. |
Chris Lattner | 42593e6 | 2007-03-24 23:56:43 +0000 | [diff] [blame] | 4552 | if (!isa<VectorType>(I.getType())) { |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 4553 | if (SimplifyDemandedInstructionBits(I)) |
Chris Lattner | 42593e6 | 2007-03-24 23:56:43 +0000 | [diff] [blame] | 4554 | return &I; |
Chris Lattner | 041a6c9 | 2007-06-15 05:26:55 +0000 | [diff] [blame] | 4555 | } else if (isa<ConstantAggregateZero>(Op1)) { |
| 4556 | return ReplaceInstUsesWith(I, Op0); // X | <0,0> -> X |
| 4557 | } else if (ConstantVector *CP = dyn_cast<ConstantVector>(Op1)) { |
| 4558 | if (CP->isAllOnesValue()) // X | <-1,-1> -> <-1,-1> |
| 4559 | return ReplaceInstUsesWith(I, I.getOperand(1)); |
Chris Lattner | 42593e6 | 2007-03-24 23:56:43 +0000 | [diff] [blame] | 4560 | } |
Chris Lattner | 041a6c9 | 2007-06-15 05:26:55 +0000 | [diff] [blame] | 4561 | |
| 4562 | |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 4563 | |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4564 | // or X, -1 == -1 |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4565 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 4566 | ConstantInt *C1 = 0; Value *X = 0; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4567 | // (X & C1) | C2 --> (X | C2) & (C1|C2) |
| 4568 | 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] | 4569 | Instruction *Or = BinaryOperator::CreateOr(X, RHS); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4570 | InsertNewInstBefore(Or, I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 4571 | Or->takeName(Op0); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4572 | return BinaryOperator::CreateAnd(Or, |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 4573 | ConstantInt::get(RHS->getValue() | C1->getValue())); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4574 | } |
Chris Lattner | ad44ebf | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 4575 | |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4576 | // (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2) |
| 4577 | 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] | 4578 | Instruction *Or = BinaryOperator::CreateOr(X, RHS); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4579 | InsertNewInstBefore(Or, I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 4580 | Or->takeName(Op0); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4581 | return BinaryOperator::CreateXor(Or, |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 4582 | ConstantInt::get(C1->getValue() & ~RHS->getValue())); |
Chris Lattner | ad44ebf | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 4583 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 4584 | |
| 4585 | // Try to fold constant and into select arguments. |
| 4586 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 4587 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 4588 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 4589 | if (isa<PHINode>(Op0)) |
| 4590 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 4591 | return NV; |
Chris Lattner | ad44ebf | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 4592 | } |
| 4593 | |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 4594 | Value *A = 0, *B = 0; |
| 4595 | ConstantInt *C1 = 0, *C2 = 0; |
Chris Lattner | f4d4c87 | 2005-05-07 23:49:08 +0000 | [diff] [blame] | 4596 | |
| 4597 | if (match(Op0, m_And(m_Value(A), m_Value(B)))) |
| 4598 | if (A == Op1 || B == Op1) // (A & ?) | A --> A |
| 4599 | return ReplaceInstUsesWith(I, Op1); |
| 4600 | if (match(Op1, m_And(m_Value(A), m_Value(B)))) |
| 4601 | if (A == Op0 || B == Op0) // A | (A & ?) --> A |
| 4602 | return ReplaceInstUsesWith(I, Op0); |
| 4603 | |
Chris Lattner | 6423d4c | 2006-07-10 20:25:24 +0000 | [diff] [blame] | 4604 | // (A | B) | C and A | (B | C) -> bswap if possible. |
| 4605 | // (A >> B) | (C << D) and (A << B) | (B >> C) -> bswap if possible. |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4606 | if (match(Op0, m_Or(m_Value(), m_Value())) || |
Chris Lattner | 6423d4c | 2006-07-10 20:25:24 +0000 | [diff] [blame] | 4607 | match(Op1, m_Or(m_Value(), m_Value())) || |
| 4608 | (match(Op0, m_Shift(m_Value(), m_Value())) && |
| 4609 | match(Op1, m_Shift(m_Value(), m_Value())))) { |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4610 | if (Instruction *BSwap = MatchBSwap(I)) |
| 4611 | return BSwap; |
| 4612 | } |
| 4613 | |
Chris Lattner | 6e4c649 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 4614 | // (X^C)|Y -> (X|Y)^C iff Y&C == 0 |
| 4615 | 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] | 4616 | MaskedValueIsZero(Op1, C1->getValue())) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4617 | Instruction *NOr = BinaryOperator::CreateOr(A, Op1); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 4618 | InsertNewInstBefore(NOr, I); |
| 4619 | NOr->takeName(Op0); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4620 | return BinaryOperator::CreateXor(NOr, C1); |
Chris Lattner | 6e4c649 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 4621 | } |
| 4622 | |
| 4623 | // Y|(X^C) -> (X|Y)^C iff Y&C == 0 |
| 4624 | 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] | 4625 | MaskedValueIsZero(Op0, C1->getValue())) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4626 | Instruction *NOr = BinaryOperator::CreateOr(A, Op0); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 4627 | InsertNewInstBefore(NOr, I); |
| 4628 | NOr->takeName(Op0); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4629 | return BinaryOperator::CreateXor(NOr, C1); |
Chris Lattner | 6e4c649 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 4630 | } |
| 4631 | |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 4632 | // (A & C)|(B & D) |
Chris Lattner | 2384d7b | 2007-06-19 05:43:49 +0000 | [diff] [blame] | 4633 | Value *C = 0, *D = 0; |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 4634 | if (match(Op0, m_And(m_Value(A), m_Value(C))) && |
| 4635 | match(Op1, m_And(m_Value(B), m_Value(D)))) { |
Chris Lattner | 6cae0e0 | 2007-04-08 07:55:22 +0000 | [diff] [blame] | 4636 | Value *V1 = 0, *V2 = 0, *V3 = 0; |
| 4637 | C1 = dyn_cast<ConstantInt>(C); |
| 4638 | C2 = dyn_cast<ConstantInt>(D); |
| 4639 | if (C1 && C2) { // (A & C1)|(B & C2) |
| 4640 | // If we have: ((V + N) & C1) | (V & C2) |
| 4641 | // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0 |
| 4642 | // replace with V+N. |
| 4643 | if (C1->getValue() == ~C2->getValue()) { |
| 4644 | if ((C2->getValue() & (C2->getValue()+1)) == 0 && // C2 == 0+1+ |
| 4645 | match(A, m_Add(m_Value(V1), m_Value(V2)))) { |
| 4646 | // Add commutes, try both ways. |
| 4647 | if (V1 == B && MaskedValueIsZero(V2, C2->getValue())) |
| 4648 | return ReplaceInstUsesWith(I, A); |
| 4649 | if (V2 == B && MaskedValueIsZero(V1, C2->getValue())) |
| 4650 | return ReplaceInstUsesWith(I, A); |
| 4651 | } |
| 4652 | // Or commutes, try both ways. |
| 4653 | if ((C1->getValue() & (C1->getValue()+1)) == 0 && |
| 4654 | match(B, m_Add(m_Value(V1), m_Value(V2)))) { |
| 4655 | // Add commutes, try both ways. |
| 4656 | if (V1 == A && MaskedValueIsZero(V2, C1->getValue())) |
| 4657 | return ReplaceInstUsesWith(I, B); |
| 4658 | if (V2 == A && MaskedValueIsZero(V1, C1->getValue())) |
| 4659 | return ReplaceInstUsesWith(I, B); |
| 4660 | } |
| 4661 | } |
Chris Lattner | 044e533 | 2007-04-08 08:01:49 +0000 | [diff] [blame] | 4662 | V1 = 0; V2 = 0; V3 = 0; |
Chris Lattner | 6cae0e0 | 2007-04-08 07:55:22 +0000 | [diff] [blame] | 4663 | } |
| 4664 | |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 4665 | // Check to see if we have any common things being and'ed. If so, find the |
| 4666 | // terms for V1 & (V2|V3). |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 4667 | if (isOnlyUse(Op0) || isOnlyUse(Op1)) { |
| 4668 | if (A == B) // (A & C)|(A & D) == A & (C|D) |
| 4669 | V1 = A, V2 = C, V3 = D; |
| 4670 | else if (A == D) // (A & C)|(B & A) == A & (B|C) |
| 4671 | V1 = A, V2 = B, V3 = C; |
| 4672 | else if (C == B) // (A & C)|(C & D) == C & (A|D) |
| 4673 | V1 = C, V2 = A, V3 = D; |
| 4674 | else if (C == D) // (A & C)|(B & C) == C & (A|B) |
| 4675 | V1 = C, V2 = A, V3 = B; |
| 4676 | |
| 4677 | if (V1) { |
| 4678 | Value *Or = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4679 | InsertNewInstBefore(BinaryOperator::CreateOr(V2, V3, "tmp"), I); |
| 4680 | return BinaryOperator::CreateAnd(V1, Or); |
Chris Lattner | 0b7c0bf | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 4681 | } |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 4682 | } |
Dan Gohman | b493b27 | 2008-10-28 22:38:57 +0000 | [diff] [blame] | 4683 | |
Dan Gohman | 1975d03 | 2008-10-30 20:40:10 +0000 | [diff] [blame] | 4684 | // (A & (C0?-1:0)) | (B & ~(C0?-1:0)) -> C0 ? A : B, and commuted variants |
Chris Lattner | faaf951 | 2008-11-16 04:24:12 +0000 | [diff] [blame] | 4685 | if (Instruction *Match = MatchSelectFromAndOr(A, B, C, D)) |
| 4686 | return Match; |
| 4687 | if (Instruction *Match = MatchSelectFromAndOr(B, A, D, C)) |
| 4688 | return Match; |
| 4689 | if (Instruction *Match = MatchSelectFromAndOr(C, B, A, D)) |
| 4690 | return Match; |
| 4691 | if (Instruction *Match = MatchSelectFromAndOr(D, A, B, C)) |
| 4692 | return Match; |
Bill Wendling | b01865c | 2008-11-30 13:52:49 +0000 | [diff] [blame] | 4693 | |
Bill Wendling | b01865c | 2008-11-30 13:52:49 +0000 | [diff] [blame] | 4694 | // ((A&~B)|(~A&B)) -> A^B |
Bill Wendling | 03aae5f | 2008-12-01 08:09:47 +0000 | [diff] [blame] | 4695 | if ((match(C, m_Not(m_Specific(D))) && |
| 4696 | match(B, m_Not(m_Specific(A))))) |
| 4697 | return BinaryOperator::CreateXor(A, D); |
Bill Wendling | b01865c | 2008-11-30 13:52:49 +0000 | [diff] [blame] | 4698 | // ((~B&A)|(~A&B)) -> A^B |
Bill Wendling | 03aae5f | 2008-12-01 08:09:47 +0000 | [diff] [blame] | 4699 | if ((match(A, m_Not(m_Specific(D))) && |
| 4700 | match(B, m_Not(m_Specific(C))))) |
| 4701 | return BinaryOperator::CreateXor(C, D); |
Bill Wendling | b01865c | 2008-11-30 13:52:49 +0000 | [diff] [blame] | 4702 | // ((A&~B)|(B&~A)) -> A^B |
Bill Wendling | 03aae5f | 2008-12-01 08:09:47 +0000 | [diff] [blame] | 4703 | if ((match(C, m_Not(m_Specific(B))) && |
| 4704 | match(D, m_Not(m_Specific(A))))) |
| 4705 | return BinaryOperator::CreateXor(A, B); |
Bill Wendling | b01865c | 2008-11-30 13:52:49 +0000 | [diff] [blame] | 4706 | // ((~B&A)|(B&~A)) -> A^B |
Bill Wendling | 03aae5f | 2008-12-01 08:09:47 +0000 | [diff] [blame] | 4707 | if ((match(A, m_Not(m_Specific(B))) && |
| 4708 | match(D, m_Not(m_Specific(C))))) |
| 4709 | return BinaryOperator::CreateXor(C, B); |
Chris Lattner | e9bed7d | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 4710 | } |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4711 | |
| 4712 | // (X >> Z) | (Y >> Z) -> (X|Y) >> Z for all shifts. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4713 | if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) { |
| 4714 | if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0)) |
| 4715 | if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() && |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4716 | SI0->getOperand(1) == SI1->getOperand(1) && |
| 4717 | (SI0->hasOneUse() || SI1->hasOneUse())) { |
| 4718 | Instruction *NewOp = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4719 | InsertNewInstBefore(BinaryOperator::CreateOr(SI0->getOperand(0), |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4720 | SI1->getOperand(0), |
| 4721 | SI0->getName()), I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4722 | return BinaryOperator::Create(SI1->getOpcode(), NewOp, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4723 | SI1->getOperand(1)); |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4724 | } |
| 4725 | } |
Chris Lattner | 67ca768 | 2003-08-12 19:11:07 +0000 | [diff] [blame] | 4726 | |
Bill Wendling | b3833d1 | 2008-12-01 01:07:11 +0000 | [diff] [blame] | 4727 | // ((A|B)&1)|(B&-2) -> (A&1) | B |
| 4728 | if (match(Op0, m_And(m_Or(m_Value(A), m_Value(B)), m_Value(C))) || |
| 4729 | match(Op0, m_And(m_Value(C), m_Or(m_Value(A), m_Value(B))))) { |
Bill Wendling | d54d860 | 2008-12-01 08:32:40 +0000 | [diff] [blame] | 4730 | Instruction *Ret = FoldOrWithConstants(I, Op1, A, B, C); |
Bill Wendling | a698a47 | 2008-12-01 08:23:25 +0000 | [diff] [blame] | 4731 | if (Ret) return Ret; |
Bill Wendling | b3833d1 | 2008-12-01 01:07:11 +0000 | [diff] [blame] | 4732 | } |
| 4733 | // (B&-2)|((A|B)&1) -> (A&1) | B |
| 4734 | if (match(Op1, m_And(m_Or(m_Value(A), m_Value(B)), m_Value(C))) || |
| 4735 | match(Op1, m_And(m_Value(C), m_Or(m_Value(A), m_Value(B))))) { |
Bill Wendling | d54d860 | 2008-12-01 08:32:40 +0000 | [diff] [blame] | 4736 | Instruction *Ret = FoldOrWithConstants(I, Op0, A, B, C); |
Bill Wendling | a698a47 | 2008-12-01 08:23:25 +0000 | [diff] [blame] | 4737 | if (Ret) return Ret; |
Bill Wendling | b3833d1 | 2008-12-01 01:07:11 +0000 | [diff] [blame] | 4738 | } |
| 4739 | |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4740 | if (match(Op0, m_Not(m_Value(A)))) { // ~A | Op1 |
| 4741 | if (A == Op1) // ~A | A == -1 |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4742 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4743 | } else { |
| 4744 | A = 0; |
| 4745 | } |
Chris Lattner | f4d4c87 | 2005-05-07 23:49:08 +0000 | [diff] [blame] | 4746 | // Note, A is still live here! |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4747 | if (match(Op1, m_Not(m_Value(B)))) { // Op0 | ~B |
| 4748 | if (Op0 == B) |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4749 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | a27231a | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 4750 | |
Misha Brukman | cb6267b | 2004-07-30 12:50:08 +0000 | [diff] [blame] | 4751 | // (~A | ~B) == (~(A & B)) - De Morgan's Law |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4752 | if (A && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4753 | Value *And = InsertNewInstBefore(BinaryOperator::CreateAnd(A, B, |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4754 | I.getName()+".demorgan"), I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4755 | return BinaryOperator::CreateNot(And); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4756 | } |
Chris Lattner | a27231a | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 4757 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4758 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4759 | // (icmp1 A, B) | (icmp2 A, B) --> (icmp3 A, B) |
| 4760 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1))) { |
| 4761 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS))) |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 4762 | return R; |
| 4763 | |
Chris Lattner | 69d4ced | 2008-11-16 05:20:07 +0000 | [diff] [blame] | 4764 | if (ICmpInst *LHS = dyn_cast<ICmpInst>(I.getOperand(0))) |
| 4765 | if (Instruction *Res = FoldOrOfICmps(I, LHS, RHS)) |
| 4766 | return Res; |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4767 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4768 | |
| 4769 | // fold (or (cast A), (cast B)) -> (cast (or A, B)) |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4770 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) { |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4771 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4772 | if (Op0C->getOpcode() == Op1C->getOpcode()) {// same cast kind ? |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 4773 | if (!isa<ICmpInst>(Op0C->getOperand(0)) || |
| 4774 | !isa<ICmpInst>(Op1C->getOperand(0))) { |
| 4775 | const Type *SrcTy = Op0C->getOperand(0)->getType(); |
| 4776 | if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() && |
| 4777 | // Only do this if the casts both really cause code to be |
| 4778 | // generated. |
| 4779 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), |
| 4780 | I.getType(), TD) && |
| 4781 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), |
| 4782 | I.getType(), TD)) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4783 | Instruction *NewOp = BinaryOperator::CreateOr(Op0C->getOperand(0), |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 4784 | Op1C->getOperand(0), |
| 4785 | I.getName()); |
| 4786 | InsertNewInstBefore(NewOp, I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4787 | return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType()); |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 4788 | } |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4789 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4790 | } |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4791 | } |
| 4792 | |
| 4793 | |
| 4794 | // (fcmp uno x, c) | (fcmp uno y, c) -> (fcmp uno x, y) |
| 4795 | if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0))) { |
| 4796 | if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1))) { |
| 4797 | if (LHS->getPredicate() == FCmpInst::FCMP_UNO && |
Chris Lattner | 5ebd936 | 2008-02-29 06:09:11 +0000 | [diff] [blame] | 4798 | RHS->getPredicate() == FCmpInst::FCMP_UNO && |
Evan Cheng | 4030062 | 2008-10-14 18:44:08 +0000 | [diff] [blame] | 4799 | LHS->getOperand(0)->getType() == RHS->getOperand(0)->getType()) { |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4800 | if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1))) |
| 4801 | if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) { |
| 4802 | // If either of the constants are nans, then the whole thing returns |
| 4803 | // true. |
Chris Lattner | be3e348 | 2007-10-24 18:54:45 +0000 | [diff] [blame] | 4804 | if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN()) |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4805 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
| 4806 | |
| 4807 | // Otherwise, no need to compare the two constants, compare the |
| 4808 | // rest. |
| 4809 | return new FCmpInst(FCmpInst::FCMP_UNO, LHS->getOperand(0), |
| 4810 | RHS->getOperand(0)); |
| 4811 | } |
Evan Cheng | 4030062 | 2008-10-14 18:44:08 +0000 | [diff] [blame] | 4812 | } else { |
| 4813 | Value *Op0LHS, *Op0RHS, *Op1LHS, *Op1RHS; |
| 4814 | FCmpInst::Predicate Op0CC, Op1CC; |
| 4815 | if (match(Op0, m_FCmp(Op0CC, m_Value(Op0LHS), m_Value(Op0RHS))) && |
| 4816 | match(Op1, m_FCmp(Op1CC, m_Value(Op1LHS), m_Value(Op1RHS)))) { |
| 4817 | if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) { |
| 4818 | // Swap RHS operands to match LHS. |
| 4819 | Op1CC = FCmpInst::getSwappedPredicate(Op1CC); |
| 4820 | std::swap(Op1LHS, Op1RHS); |
| 4821 | } |
| 4822 | if (Op0LHS == Op1LHS && Op0RHS == Op1RHS) { |
| 4823 | // Simplify (fcmp cc0 x, y) | (fcmp cc1 x, y). |
| 4824 | if (Op0CC == Op1CC) |
| 4825 | return new FCmpInst((FCmpInst::Predicate)Op0CC, Op0LHS, Op0RHS); |
| 4826 | else if (Op0CC == FCmpInst::FCMP_TRUE || |
| 4827 | Op1CC == FCmpInst::FCMP_TRUE) |
| 4828 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
| 4829 | else if (Op0CC == FCmpInst::FCMP_FALSE) |
| 4830 | return ReplaceInstUsesWith(I, Op1); |
| 4831 | else if (Op1CC == FCmpInst::FCMP_FALSE) |
| 4832 | return ReplaceInstUsesWith(I, Op0); |
| 4833 | bool Op0Ordered; |
| 4834 | bool Op1Ordered; |
| 4835 | unsigned Op0Pred = getFCmpCode(Op0CC, Op0Ordered); |
| 4836 | unsigned Op1Pred = getFCmpCode(Op1CC, Op1Ordered); |
| 4837 | if (Op0Ordered == Op1Ordered) { |
| 4838 | // If both are ordered or unordered, return a new fcmp with |
| 4839 | // or'ed predicates. |
| 4840 | Value *RV = getFCmpValue(Op0Ordered, Op0Pred|Op1Pred, |
| 4841 | Op0LHS, Op0RHS); |
| 4842 | if (Instruction *I = dyn_cast<Instruction>(RV)) |
| 4843 | return I; |
| 4844 | // Otherwise, it's a constant boolean value... |
| 4845 | return ReplaceInstUsesWith(I, RV); |
| 4846 | } |
| 4847 | } |
| 4848 | } |
| 4849 | } |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4850 | } |
| 4851 | } |
Chris Lattner | e9bed7d | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 4852 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4853 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4854 | } |
| 4855 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 4856 | namespace { |
| 4857 | |
Chris Lattner | c317d39 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 4858 | // XorSelf - Implements: X ^ X --> 0 |
| 4859 | struct XorSelf { |
| 4860 | Value *RHS; |
| 4861 | XorSelf(Value *rhs) : RHS(rhs) {} |
| 4862 | bool shouldApply(Value *LHS) const { return LHS == RHS; } |
| 4863 | Instruction *apply(BinaryOperator &Xor) const { |
| 4864 | return &Xor; |
| 4865 | } |
| 4866 | }; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4867 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 4868 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4869 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4870 | Instruction *InstCombiner::visitXor(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 4871 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4872 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4873 | |
Evan Cheng | d34af78 | 2008-03-25 20:07:13 +0000 | [diff] [blame] | 4874 | if (isa<UndefValue>(Op1)) { |
| 4875 | if (isa<UndefValue>(Op0)) |
| 4876 | // Handle undef ^ undef -> 0 special case. This is a common |
| 4877 | // idiom (misuse). |
| 4878 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4879 | return ReplaceInstUsesWith(I, Op1); // X ^ undef -> undef |
Evan Cheng | d34af78 | 2008-03-25 20:07:13 +0000 | [diff] [blame] | 4880 | } |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4881 | |
Chris Lattner | c317d39 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 4882 | // xor X, X = 0, even if X is nested in a sequence of Xor's. |
| 4883 | if (Instruction *Result = AssociativeOpt(I, XorSelf(Op1))) { |
Chris Lattner | a9ff5eb | 2007-08-05 08:47:58 +0000 | [diff] [blame] | 4884 | assert(Result == &I && "AssociativeOpt didn't work?"); Result=Result; |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 4885 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | c317d39 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 4886 | } |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 4887 | |
| 4888 | // See if we can simplify any instructions used by the instruction whose sole |
| 4889 | // purpose is to compute bits we don't care about. |
Reid Spencer | a03d45f | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 4890 | if (!isa<VectorType>(I.getType())) { |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 4891 | if (SimplifyDemandedInstructionBits(I)) |
Reid Spencer | a03d45f | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 4892 | return &I; |
Chris Lattner | 041a6c9 | 2007-06-15 05:26:55 +0000 | [diff] [blame] | 4893 | } else if (isa<ConstantAggregateZero>(Op1)) { |
| 4894 | return ReplaceInstUsesWith(I, Op0); // X ^ <0,0> -> X |
Reid Spencer | a03d45f | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 4895 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4896 | |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4897 | // Is this a ~ operation? |
| 4898 | if (Value *NotOp = dyn_castNotVal(&I)) { |
| 4899 | // ~(~X & Y) --> (X | ~Y) - De Morgan's Law |
| 4900 | // ~(~X | Y) === (X & ~Y) - De Morgan's Law |
| 4901 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(NotOp)) { |
| 4902 | if (Op0I->getOpcode() == Instruction::And || |
| 4903 | Op0I->getOpcode() == Instruction::Or) { |
| 4904 | if (dyn_castNotVal(Op0I->getOperand(1))) Op0I->swapOperands(); |
| 4905 | if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0))) { |
| 4906 | Instruction *NotY = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4907 | BinaryOperator::CreateNot(Op0I->getOperand(1), |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4908 | Op0I->getOperand(1)->getName()+".not"); |
| 4909 | InsertNewInstBefore(NotY, I); |
| 4910 | if (Op0I->getOpcode() == Instruction::And) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4911 | return BinaryOperator::CreateOr(Op0NotVal, NotY); |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4912 | else |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4913 | return BinaryOperator::CreateAnd(Op0NotVal, NotY); |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4914 | } |
| 4915 | } |
| 4916 | } |
| 4917 | } |
| 4918 | |
| 4919 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4920 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Nick Lewycky | f947b3e | 2007-08-06 20:04:16 +0000 | [diff] [blame] | 4921 | if (RHS == ConstantInt::getTrue() && Op0->hasOneUse()) { |
Bill Wendling | 3479be9 | 2009-01-01 01:18:23 +0000 | [diff] [blame] | 4922 | // xor (cmp A, B), true = not (cmp A, B) = !cmp A, B |
Nick Lewycky | f947b3e | 2007-08-06 20:04:16 +0000 | [diff] [blame] | 4923 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(Op0)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4924 | return new ICmpInst(ICI->getInversePredicate(), |
| 4925 | ICI->getOperand(0), ICI->getOperand(1)); |
Chris Lattner | ad5b4fb | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 4926 | |
Nick Lewycky | f947b3e | 2007-08-06 20:04:16 +0000 | [diff] [blame] | 4927 | if (FCmpInst *FCI = dyn_cast<FCmpInst>(Op0)) |
| 4928 | return new FCmpInst(FCI->getInversePredicate(), |
| 4929 | FCI->getOperand(0), FCI->getOperand(1)); |
| 4930 | } |
| 4931 | |
Nick Lewycky | 517e1f5 | 2008-05-31 19:01:33 +0000 | [diff] [blame] | 4932 | // fold (xor(zext(cmp)), 1) and (xor(sext(cmp)), -1) to ext(!cmp). |
| 4933 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) { |
| 4934 | if (CmpInst *CI = dyn_cast<CmpInst>(Op0C->getOperand(0))) { |
| 4935 | if (CI->hasOneUse() && Op0C->hasOneUse()) { |
| 4936 | Instruction::CastOps Opcode = Op0C->getOpcode(); |
| 4937 | if (Opcode == Instruction::ZExt || Opcode == Instruction::SExt) { |
| 4938 | if (RHS == ConstantExpr::getCast(Opcode, ConstantInt::getTrue(), |
| 4939 | Op0C->getDestTy())) { |
| 4940 | Instruction *NewCI = InsertNewInstBefore(CmpInst::Create( |
| 4941 | CI->getOpcode(), CI->getInversePredicate(), |
| 4942 | CI->getOperand(0), CI->getOperand(1)), I); |
| 4943 | NewCI->takeName(CI); |
| 4944 | return CastInst::Create(Opcode, NewCI, Op0C->getType()); |
| 4945 | } |
| 4946 | } |
| 4947 | } |
| 4948 | } |
| 4949 | } |
| 4950 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4951 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) { |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 4952 | // ~(c-X) == X-c-1 == X+(-c-1) |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4953 | if (Op0I->getOpcode() == Instruction::Sub && RHS->isAllOnesValue()) |
| 4954 | if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4955 | Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C); |
| 4956 | Constant *ConstantRHS = ConstantExpr::getSub(NegOp0I0C, |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4957 | ConstantInt::get(I.getType(), 1)); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4958 | return BinaryOperator::CreateAdd(Op0I->getOperand(1), ConstantRHS); |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4959 | } |
Chris Lattner | 5c6e2db | 2007-04-02 05:36:22 +0000 | [diff] [blame] | 4960 | |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 4961 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) { |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 4962 | if (Op0I->getOpcode() == Instruction::Add) { |
Chris Lattner | 689d24b | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 4963 | // ~(X-c) --> (-c-1)-X |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4964 | if (RHS->isAllOnesValue()) { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4965 | Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4966 | return BinaryOperator::CreateSub( |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4967 | ConstantExpr::getSub(NegOp0CI, |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4968 | ConstantInt::get(I.getType(), 1)), |
Chris Lattner | 689d24b | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 4969 | Op0I->getOperand(0)); |
Chris Lattner | acf4e07 | 2007-04-02 05:42:22 +0000 | [diff] [blame] | 4970 | } else if (RHS->getValue().isSignBit()) { |
Chris Lattner | 5c6e2db | 2007-04-02 05:36:22 +0000 | [diff] [blame] | 4971 | // (X + C) ^ signbit -> (X + C + signbit) |
| 4972 | Constant *C = ConstantInt::get(RHS->getValue() + Op0CI->getValue()); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4973 | return BinaryOperator::CreateAdd(Op0I->getOperand(0), C); |
Chris Lattner | cd1d6d5 | 2007-04-02 05:48:58 +0000 | [diff] [blame] | 4974 | |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4975 | } |
Chris Lattner | 02bd1b3 | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 4976 | } else if (Op0I->getOpcode() == Instruction::Or) { |
| 4977 | // (X|C1)^C2 -> X^(C1|C2) iff X&~C1 == 0 |
Reid Spencer | a03d45f | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 4978 | if (MaskedValueIsZero(Op0I->getOperand(0), Op0CI->getValue())) { |
Chris Lattner | 02bd1b3 | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 4979 | Constant *NewRHS = ConstantExpr::getOr(Op0CI, RHS); |
| 4980 | // Anything in both C1 and C2 is known to be zero, remove it from |
| 4981 | // NewRHS. |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 4982 | Constant *CommonBits = And(Op0CI, RHS); |
Chris Lattner | 02bd1b3 | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 4983 | NewRHS = ConstantExpr::getAnd(NewRHS, |
| 4984 | ConstantExpr::getNot(CommonBits)); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 4985 | AddToWorkList(Op0I); |
Chris Lattner | 02bd1b3 | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 4986 | I.setOperand(0, Op0I->getOperand(0)); |
| 4987 | I.setOperand(1, NewRHS); |
| 4988 | return &I; |
| 4989 | } |
Chris Lattner | eca0c5c | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 4990 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 4991 | } |
Chris Lattner | 05bd1b2 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 4992 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 4993 | |
| 4994 | // Try to fold constant and into select arguments. |
| 4995 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 4996 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 4997 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 4998 | if (isa<PHINode>(Op0)) |
| 4999 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 5000 | return NV; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5001 | } |
| 5002 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 5003 | if (Value *X = dyn_castNotVal(Op0)) // ~A ^ A == -1 |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 5004 | if (X == Op1) |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 5005 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 5006 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 5007 | if (Value *X = dyn_castNotVal(Op1)) // A ^ ~A == -1 |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 5008 | if (X == Op0) |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 5009 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 5010 | |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 5011 | |
| 5012 | BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1); |
| 5013 | if (Op1I) { |
| 5014 | Value *A, *B; |
| 5015 | if (match(Op1I, m_Or(m_Value(A), m_Value(B)))) { |
| 5016 | if (A == Op0) { // B^(B|A) == (A|B)^B |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 5017 | Op1I->swapOperands(); |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 5018 | I.swapOperands(); |
| 5019 | std::swap(Op0, Op1); |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 5020 | } else if (B == Op0) { // B^(A|B) == (A|B)^B |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 5021 | I.swapOperands(); // Simplified below. |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 5022 | std::swap(Op0, Op1); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5023 | } |
Chris Lattner | cb504b9 | 2008-11-16 05:38:51 +0000 | [diff] [blame] | 5024 | } else if (match(Op1I, m_Xor(m_Specific(Op0), m_Value(B)))) { |
| 5025 | return ReplaceInstUsesWith(I, B); // A^(A^B) == B |
| 5026 | } else if (match(Op1I, m_Xor(m_Value(A), m_Specific(Op0)))) { |
| 5027 | return ReplaceInstUsesWith(I, A); // A^(B^A) == B |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 5028 | } 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] | 5029 | if (A == Op0) { // A^(A&B) -> A^(B&A) |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 5030 | Op1I->swapOperands(); |
Chris Lattner | 6abbdf9 | 2007-04-01 05:36:37 +0000 | [diff] [blame] | 5031 | std::swap(A, B); |
| 5032 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 5033 | if (B == Op0) { // A^(B&A) -> (B&A)^A |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 5034 | I.swapOperands(); // Simplified below. |
| 5035 | std::swap(Op0, Op1); |
| 5036 | } |
Chris Lattner | 26ca7e1 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 5037 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 5038 | } |
| 5039 | |
| 5040 | BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0); |
| 5041 | if (Op0I) { |
| 5042 | Value *A, *B; |
| 5043 | if (match(Op0I, m_Or(m_Value(A), m_Value(B))) && Op0I->hasOneUse()) { |
| 5044 | if (A == Op1) // (B|A)^B == (A|B)^B |
| 5045 | std::swap(A, B); |
| 5046 | if (B == Op1) { // (A|B)^B == A & ~B |
| 5047 | Instruction *NotB = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5048 | InsertNewInstBefore(BinaryOperator::CreateNot(Op1, "tmp"), I); |
| 5049 | return BinaryOperator::CreateAnd(A, NotB); |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 5050 | } |
Chris Lattner | cb504b9 | 2008-11-16 05:38:51 +0000 | [diff] [blame] | 5051 | } else if (match(Op0I, m_Xor(m_Specific(Op1), m_Value(B)))) { |
| 5052 | return ReplaceInstUsesWith(I, B); // (A^B)^A == B |
| 5053 | } else if (match(Op0I, m_Xor(m_Value(A), m_Specific(Op1)))) { |
| 5054 | return ReplaceInstUsesWith(I, A); // (B^A)^A == B |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 5055 | } else if (match(Op0I, m_And(m_Value(A), m_Value(B))) && Op0I->hasOneUse()){ |
| 5056 | if (A == Op1) // (A&B)^A -> (B&A)^A |
| 5057 | std::swap(A, B); |
| 5058 | if (B == Op1 && // (B&A)^A == ~B & A |
Chris Lattner | ae1ab39 | 2006-04-01 22:05:01 +0000 | [diff] [blame] | 5059 | !isa<ConstantInt>(Op1)) { // Canonical form is (B&C)^C |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 5060 | Instruction *N = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5061 | InsertNewInstBefore(BinaryOperator::CreateNot(A, "tmp"), I); |
| 5062 | return BinaryOperator::CreateAnd(N, Op1); |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 5063 | } |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 5064 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 5065 | } |
| 5066 | |
| 5067 | // (X >> Z) ^ (Y >> Z) -> (X^Y) >> Z for all shifts. |
| 5068 | if (Op0I && Op1I && Op0I->isShift() && |
| 5069 | Op0I->getOpcode() == Op1I->getOpcode() && |
| 5070 | Op0I->getOperand(1) == Op1I->getOperand(1) && |
| 5071 | (Op1I->hasOneUse() || Op1I->hasOneUse())) { |
| 5072 | Instruction *NewOp = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5073 | InsertNewInstBefore(BinaryOperator::CreateXor(Op0I->getOperand(0), |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 5074 | Op1I->getOperand(0), |
| 5075 | Op0I->getName()), I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5076 | return BinaryOperator::Create(Op1I->getOpcode(), NewOp, |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 5077 | Op1I->getOperand(1)); |
| 5078 | } |
| 5079 | |
| 5080 | if (Op0I && Op1I) { |
| 5081 | Value *A, *B, *C, *D; |
| 5082 | // (A & B)^(A | B) -> A ^ B |
| 5083 | if (match(Op0I, m_And(m_Value(A), m_Value(B))) && |
| 5084 | match(Op1I, m_Or(m_Value(C), m_Value(D)))) { |
| 5085 | if ((A == C && B == D) || (A == D && B == C)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5086 | return BinaryOperator::CreateXor(A, B); |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 5087 | } |
| 5088 | // (A | B)^(A & B) -> A ^ B |
| 5089 | if (match(Op0I, m_Or(m_Value(A), m_Value(B))) && |
| 5090 | match(Op1I, m_And(m_Value(C), m_Value(D)))) { |
| 5091 | if ((A == C && B == D) || (A == D && B == C)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5092 | return BinaryOperator::CreateXor(A, B); |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 5093 | } |
| 5094 | |
| 5095 | // (A & B)^(C & D) |
| 5096 | if ((Op0I->hasOneUse() || Op1I->hasOneUse()) && |
| 5097 | match(Op0I, m_And(m_Value(A), m_Value(B))) && |
| 5098 | match(Op1I, m_And(m_Value(C), m_Value(D)))) { |
| 5099 | // (X & Y)^(X & Y) -> (Y^Z) & X |
| 5100 | Value *X = 0, *Y = 0, *Z = 0; |
| 5101 | if (A == C) |
| 5102 | X = A, Y = B, Z = D; |
| 5103 | else if (A == D) |
| 5104 | X = A, Y = B, Z = C; |
| 5105 | else if (B == C) |
| 5106 | X = B, Y = A, Z = D; |
| 5107 | else if (B == D) |
| 5108 | X = B, Y = A, Z = C; |
| 5109 | |
| 5110 | if (X) { |
| 5111 | Instruction *NewOp = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5112 | InsertNewInstBefore(BinaryOperator::CreateXor(Y, Z, Op0->getName()), I); |
| 5113 | return BinaryOperator::CreateAnd(NewOp, X); |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 5114 | } |
| 5115 | } |
| 5116 | } |
| 5117 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5118 | // (icmp1 A, B) ^ (icmp2 A, B) --> (icmp3 A, B) |
| 5119 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1))) |
| 5120 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS))) |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 5121 | return R; |
| 5122 | |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 5123 | // fold (xor (cast A), (cast B)) -> (cast (xor A, B)) |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 5124 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) { |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 5125 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 5126 | if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind? |
| 5127 | const Type *SrcTy = Op0C->getOperand(0)->getType(); |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 5128 | if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() && |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 5129 | // 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] | 5130 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), |
| 5131 | I.getType(), TD) && |
| 5132 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), |
| 5133 | I.getType(), TD)) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5134 | Instruction *NewOp = BinaryOperator::CreateXor(Op0C->getOperand(0), |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 5135 | Op1C->getOperand(0), |
| 5136 | I.getName()); |
| 5137 | InsertNewInstBefore(NewOp, I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5138 | return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType()); |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 5139 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 5140 | } |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 5141 | } |
Nick Lewycky | 517e1f5 | 2008-05-31 19:01:33 +0000 | [diff] [blame] | 5142 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 5143 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5144 | } |
| 5145 | |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 5146 | /// AddWithOverflow - Compute Result = In1+In2, returning true if the result |
| 5147 | /// overflowed for this type. |
| 5148 | static bool AddWithOverflow(ConstantInt *&Result, ConstantInt *In1, |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 5149 | ConstantInt *In2, bool IsSigned = false) { |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 5150 | Result = cast<ConstantInt>(Add(In1, In2)); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 5151 | |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 5152 | if (IsSigned) |
| 5153 | if (In2->getValue().isNegative()) |
| 5154 | return Result->getValue().sgt(In1->getValue()); |
| 5155 | else |
| 5156 | return Result->getValue().slt(In1->getValue()); |
| 5157 | else |
| 5158 | return Result->getValue().ult(In1->getValue()); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 5159 | } |
| 5160 | |
Dan Gohman | 1df3fd6 | 2008-09-10 23:30:57 +0000 | [diff] [blame] | 5161 | /// SubWithOverflow - Compute Result = In1-In2, returning true if the result |
| 5162 | /// overflowed for this type. |
| 5163 | static bool SubWithOverflow(ConstantInt *&Result, ConstantInt *In1, |
| 5164 | ConstantInt *In2, bool IsSigned = false) { |
Dan Gohman | bcb37fd | 2008-09-11 18:53:02 +0000 | [diff] [blame] | 5165 | Result = cast<ConstantInt>(Subtract(In1, In2)); |
Dan Gohman | 1df3fd6 | 2008-09-10 23:30:57 +0000 | [diff] [blame] | 5166 | |
| 5167 | if (IsSigned) |
| 5168 | if (In2->getValue().isNegative()) |
| 5169 | return Result->getValue().slt(In1->getValue()); |
| 5170 | else |
| 5171 | return Result->getValue().sgt(In1->getValue()); |
| 5172 | else |
| 5173 | return Result->getValue().ugt(In1->getValue()); |
| 5174 | } |
| 5175 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5176 | /// EmitGEPOffset - Given a getelementptr instruction/constantexpr, emit the |
| 5177 | /// code necessary to compute the offset from the base pointer (without adding |
| 5178 | /// in the base pointer). Return the result as a signed integer of intptr size. |
| 5179 | static Value *EmitGEPOffset(User *GEP, Instruction &I, InstCombiner &IC) { |
| 5180 | TargetData &TD = IC.getTargetData(); |
| 5181 | gep_type_iterator GTI = gep_type_begin(GEP); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5182 | const Type *IntPtrTy = TD.getIntPtrType(); |
| 5183 | Value *Result = Constant::getNullValue(IntPtrTy); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5184 | |
| 5185 | // Build a mask for high order bits. |
Chris Lattner | 10c0d91 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 5186 | unsigned IntPtrWidth = TD.getPointerSizeInBits(); |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 5187 | uint64_t PtrSizeMask = ~0ULL >> (64-IntPtrWidth); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5188 | |
Gabor Greif | 177dd3f | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 5189 | for (User::op_iterator i = GEP->op_begin() + 1, e = GEP->op_end(); i != e; |
| 5190 | ++i, ++GTI) { |
| 5191 | Value *Op = *i; |
Duncan Sands | ceb4d1a | 2009-01-12 20:38:59 +0000 | [diff] [blame] | 5192 | uint64_t Size = TD.getTypePaddedSize(GTI.getIndexedType()) & PtrSizeMask; |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 5193 | if (ConstantInt *OpC = dyn_cast<ConstantInt>(Op)) { |
| 5194 | if (OpC->isZero()) continue; |
| 5195 | |
| 5196 | // Handle a struct index, which adds its field offset to the pointer. |
| 5197 | if (const StructType *STy = dyn_cast<StructType>(*GTI)) { |
| 5198 | Size = TD.getStructLayout(STy)->getElementOffset(OpC->getZExtValue()); |
| 5199 | |
| 5200 | if (ConstantInt *RC = dyn_cast<ConstantInt>(Result)) |
| 5201 | Result = ConstantInt::get(RC->getValue() + APInt(IntPtrWidth, Size)); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 5202 | else |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 5203 | Result = IC.InsertNewInstBefore( |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5204 | BinaryOperator::CreateAdd(Result, |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 5205 | ConstantInt::get(IntPtrTy, Size), |
| 5206 | GEP->getName()+".offs"), I); |
| 5207 | continue; |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 5208 | } |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 5209 | |
| 5210 | Constant *Scale = ConstantInt::get(IntPtrTy, Size); |
| 5211 | Constant *OC = ConstantExpr::getIntegerCast(OpC, IntPtrTy, true /*SExt*/); |
| 5212 | Scale = ConstantExpr::getMul(OC, Scale); |
| 5213 | if (Constant *RC = dyn_cast<Constant>(Result)) |
| 5214 | Result = ConstantExpr::getAdd(RC, Scale); |
| 5215 | else { |
| 5216 | // Emit an add instruction. |
| 5217 | Result = IC.InsertNewInstBefore( |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5218 | BinaryOperator::CreateAdd(Result, Scale, |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 5219 | GEP->getName()+".offs"), I); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 5220 | } |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 5221 | continue; |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5222 | } |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 5223 | // Convert to correct type. |
| 5224 | if (Op->getType() != IntPtrTy) { |
| 5225 | if (Constant *OpC = dyn_cast<Constant>(Op)) |
Chris Lattner | 62ce3b3 | 2009-04-07 05:03:34 +0000 | [diff] [blame^] | 5226 | Op = ConstantExpr::getIntegerCast(OpC, IntPtrTy, true); |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 5227 | else |
Chris Lattner | 62ce3b3 | 2009-04-07 05:03:34 +0000 | [diff] [blame^] | 5228 | Op = IC.InsertNewInstBefore(CastInst::CreateIntegerCast(Op, IntPtrTy, |
| 5229 | true, |
| 5230 | Op->getName()+".c"), I); |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 5231 | } |
| 5232 | if (Size != 1) { |
| 5233 | Constant *Scale = ConstantInt::get(IntPtrTy, Size); |
| 5234 | if (Constant *OpC = dyn_cast<Constant>(Op)) |
| 5235 | Op = ConstantExpr::getMul(OpC, Scale); |
| 5236 | 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] | 5237 | Op = IC.InsertNewInstBefore(BinaryOperator::CreateMul(Op, Scale, |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 5238 | GEP->getName()+".idx"), I); |
| 5239 | } |
| 5240 | |
| 5241 | // Emit an add instruction. |
| 5242 | if (isa<Constant>(Op) && isa<Constant>(Result)) |
| 5243 | Result = ConstantExpr::getAdd(cast<Constant>(Op), |
| 5244 | cast<Constant>(Result)); |
| 5245 | else |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5246 | Result = IC.InsertNewInstBefore(BinaryOperator::CreateAdd(Op, Result, |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 5247 | GEP->getName()+".offs"), I); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5248 | } |
| 5249 | return Result; |
| 5250 | } |
| 5251 | |
Chris Lattner | 10c0d91 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 5252 | |
| 5253 | /// EvaluateGEPOffsetExpression - Return an value that can be used to compare of |
| 5254 | /// the *offset* implied by GEP to zero. For example, if we have &A[i], we want |
| 5255 | /// to return 'i' for "icmp ne i, 0". Note that, in general, indices can be |
| 5256 | /// complex, and scales are involved. The above expression would also be legal |
| 5257 | /// to codegen as "icmp ne (i*4), 0" (assuming A is a pointer to i32). This |
| 5258 | /// later form is less amenable to optimization though, and we are allowed to |
| 5259 | /// generate the first by knowing that pointer arithmetic doesn't overflow. |
| 5260 | /// |
| 5261 | /// If we can't emit an optimized form for this expression, this returns null. |
| 5262 | /// |
| 5263 | static Value *EvaluateGEPOffsetExpression(User *GEP, Instruction &I, |
| 5264 | InstCombiner &IC) { |
Chris Lattner | 10c0d91 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 5265 | TargetData &TD = IC.getTargetData(); |
| 5266 | gep_type_iterator GTI = gep_type_begin(GEP); |
| 5267 | |
| 5268 | // Check to see if this gep only has a single variable index. If so, and if |
| 5269 | // any constant indices are a multiple of its scale, then we can compute this |
| 5270 | // in terms of the scale of the variable index. For example, if the GEP |
| 5271 | // implies an offset of "12 + i*4", then we can codegen this as "3 + i", |
| 5272 | // because the expression will cross zero at the same point. |
| 5273 | unsigned i, e = GEP->getNumOperands(); |
| 5274 | int64_t Offset = 0; |
| 5275 | for (i = 1; i != e; ++i, ++GTI) { |
| 5276 | if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i))) { |
| 5277 | // Compute the aggregate offset of constant indices. |
| 5278 | if (CI->isZero()) continue; |
| 5279 | |
| 5280 | // Handle a struct index, which adds its field offset to the pointer. |
| 5281 | if (const StructType *STy = dyn_cast<StructType>(*GTI)) { |
| 5282 | Offset += TD.getStructLayout(STy)->getElementOffset(CI->getZExtValue()); |
| 5283 | } else { |
Duncan Sands | ceb4d1a | 2009-01-12 20:38:59 +0000 | [diff] [blame] | 5284 | uint64_t Size = TD.getTypePaddedSize(GTI.getIndexedType()); |
Chris Lattner | 10c0d91 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 5285 | Offset += Size*CI->getSExtValue(); |
| 5286 | } |
| 5287 | } else { |
| 5288 | // Found our variable index. |
| 5289 | break; |
| 5290 | } |
| 5291 | } |
| 5292 | |
| 5293 | // If there are no variable indices, we must have a constant offset, just |
| 5294 | // evaluate it the general way. |
| 5295 | if (i == e) return 0; |
| 5296 | |
| 5297 | Value *VariableIdx = GEP->getOperand(i); |
| 5298 | // Determine the scale factor of the variable element. For example, this is |
| 5299 | // 4 if the variable index is into an array of i32. |
Duncan Sands | ceb4d1a | 2009-01-12 20:38:59 +0000 | [diff] [blame] | 5300 | uint64_t VariableScale = TD.getTypePaddedSize(GTI.getIndexedType()); |
Chris Lattner | 10c0d91 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 5301 | |
| 5302 | // Verify that there are no other variable indices. If so, emit the hard way. |
| 5303 | for (++i, ++GTI; i != e; ++i, ++GTI) { |
| 5304 | ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i)); |
| 5305 | if (!CI) return 0; |
| 5306 | |
| 5307 | // Compute the aggregate offset of constant indices. |
| 5308 | if (CI->isZero()) continue; |
| 5309 | |
| 5310 | // Handle a struct index, which adds its field offset to the pointer. |
| 5311 | if (const StructType *STy = dyn_cast<StructType>(*GTI)) { |
| 5312 | Offset += TD.getStructLayout(STy)->getElementOffset(CI->getZExtValue()); |
| 5313 | } else { |
Duncan Sands | ceb4d1a | 2009-01-12 20:38:59 +0000 | [diff] [blame] | 5314 | uint64_t Size = TD.getTypePaddedSize(GTI.getIndexedType()); |
Chris Lattner | 10c0d91 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 5315 | Offset += Size*CI->getSExtValue(); |
| 5316 | } |
| 5317 | } |
| 5318 | |
| 5319 | // Okay, we know we have a single variable index, which must be a |
| 5320 | // pointer/array/vector index. If there is no offset, life is simple, return |
| 5321 | // the index. |
| 5322 | unsigned IntPtrWidth = TD.getPointerSizeInBits(); |
| 5323 | if (Offset == 0) { |
| 5324 | // Cast to intptrty in case a truncation occurs. If an extension is needed, |
| 5325 | // we don't need to bother extending: the extension won't affect where the |
| 5326 | // computation crosses zero. |
| 5327 | if (VariableIdx->getType()->getPrimitiveSizeInBits() > IntPtrWidth) |
| 5328 | VariableIdx = new TruncInst(VariableIdx, TD.getIntPtrType(), |
| 5329 | VariableIdx->getNameStart(), &I); |
| 5330 | return VariableIdx; |
| 5331 | } |
| 5332 | |
| 5333 | // Otherwise, there is an index. The computation we will do will be modulo |
| 5334 | // the pointer size, so get it. |
| 5335 | uint64_t PtrSizeMask = ~0ULL >> (64-IntPtrWidth); |
| 5336 | |
| 5337 | Offset &= PtrSizeMask; |
| 5338 | VariableScale &= PtrSizeMask; |
| 5339 | |
| 5340 | // To do this transformation, any constant index must be a multiple of the |
| 5341 | // variable scale factor. For example, we can evaluate "12 + 4*i" as "3 + i", |
| 5342 | // but we can't evaluate "10 + 3*i" in terms of i. Check that the offset is a |
| 5343 | // multiple of the variable scale. |
| 5344 | int64_t NewOffs = Offset / (int64_t)VariableScale; |
| 5345 | if (Offset != NewOffs*(int64_t)VariableScale) |
| 5346 | return 0; |
| 5347 | |
| 5348 | // Okay, we can do this evaluation. Start by converting the index to intptr. |
| 5349 | const Type *IntPtrTy = TD.getIntPtrType(); |
| 5350 | if (VariableIdx->getType() != IntPtrTy) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5351 | VariableIdx = CastInst::CreateIntegerCast(VariableIdx, IntPtrTy, |
Chris Lattner | 10c0d91 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 5352 | true /*SExt*/, |
| 5353 | VariableIdx->getNameStart(), &I); |
| 5354 | Constant *OffsetVal = ConstantInt::get(IntPtrTy, NewOffs); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5355 | return BinaryOperator::CreateAdd(VariableIdx, OffsetVal, "offset", &I); |
Chris Lattner | 10c0d91 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 5356 | } |
| 5357 | |
| 5358 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5359 | /// FoldGEPICmp - Fold comparisons between a GEP instruction and something |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5360 | /// 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] | 5361 | Instruction *InstCombiner::FoldGEPICmp(User *GEPLHS, Value *RHS, |
| 5362 | ICmpInst::Predicate Cond, |
| 5363 | Instruction &I) { |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5364 | assert(dyn_castGetElementPtr(GEPLHS) && "LHS is not a getelementptr!"); |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 5365 | |
Chris Lattner | 10c0d91 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 5366 | // Look through bitcasts. |
| 5367 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(RHS)) |
| 5368 | RHS = BCI->getOperand(0); |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 5369 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5370 | Value *PtrBase = GEPLHS->getOperand(0); |
| 5371 | if (PtrBase == RHS) { |
Chris Lattner | 7c95deb | 2008-02-05 04:45:32 +0000 | [diff] [blame] | 5372 | // ((gep Ptr, OFFSET) cmp Ptr) ---> (OFFSET cmp 0). |
Chris Lattner | 10c0d91 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 5373 | // This transformation (ignoring the base and scales) is valid because we |
| 5374 | // know pointers can't overflow. See if we can output an optimized form. |
| 5375 | Value *Offset = EvaluateGEPOffsetExpression(GEPLHS, I, *this); |
| 5376 | |
| 5377 | // If not, synthesize the offset the hard way. |
| 5378 | if (Offset == 0) |
| 5379 | Offset = EmitGEPOffset(GEPLHS, I, *this); |
Chris Lattner | 7c95deb | 2008-02-05 04:45:32 +0000 | [diff] [blame] | 5380 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), Offset, |
| 5381 | Constant::getNullValue(Offset->getType())); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5382 | } else if (User *GEPRHS = dyn_castGetElementPtr(RHS)) { |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 5383 | // If the base pointers are different, but the indices are the same, just |
| 5384 | // compare the base pointer. |
| 5385 | if (PtrBase != GEPRHS->getOperand(0)) { |
| 5386 | bool IndicesTheSame = GEPLHS->getNumOperands()==GEPRHS->getNumOperands(); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 5387 | IndicesTheSame &= GEPLHS->getOperand(0)->getType() == |
Chris Lattner | 93b94a6 | 2005-04-26 14:40:41 +0000 | [diff] [blame] | 5388 | GEPRHS->getOperand(0)->getType(); |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 5389 | if (IndicesTheSame) |
| 5390 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i) |
| 5391 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { |
| 5392 | IndicesTheSame = false; |
| 5393 | break; |
| 5394 | } |
| 5395 | |
| 5396 | // If all indices are the same, just compare the base pointers. |
| 5397 | if (IndicesTheSame) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5398 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), |
| 5399 | GEPLHS->getOperand(0), GEPRHS->getOperand(0)); |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 5400 | |
| 5401 | // Otherwise, the base pointers are different and the indices are |
| 5402 | // different, bail out. |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5403 | return 0; |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 5404 | } |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5405 | |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 5406 | // If one of the GEPs has all zero indices, recurse. |
| 5407 | bool AllZeros = true; |
| 5408 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i) |
| 5409 | if (!isa<Constant>(GEPLHS->getOperand(i)) || |
| 5410 | !cast<Constant>(GEPLHS->getOperand(i))->isNullValue()) { |
| 5411 | AllZeros = false; |
| 5412 | break; |
| 5413 | } |
| 5414 | if (AllZeros) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5415 | return FoldGEPICmp(GEPRHS, GEPLHS->getOperand(0), |
| 5416 | ICmpInst::getSwappedPredicate(Cond), I); |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 5417 | |
| 5418 | // If the other GEP has all zero indices, recurse. |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 5419 | AllZeros = true; |
| 5420 | for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i) |
| 5421 | if (!isa<Constant>(GEPRHS->getOperand(i)) || |
| 5422 | !cast<Constant>(GEPRHS->getOperand(i))->isNullValue()) { |
| 5423 | AllZeros = false; |
| 5424 | break; |
| 5425 | } |
| 5426 | if (AllZeros) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5427 | return FoldGEPICmp(GEPLHS, GEPRHS->getOperand(0), Cond, I); |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 5428 | |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 5429 | if (GEPLHS->getNumOperands() == GEPRHS->getNumOperands()) { |
| 5430 | // If the GEPs only differ by one index, compare it. |
| 5431 | unsigned NumDifferences = 0; // Keep track of # differences. |
| 5432 | unsigned DiffOperand = 0; // The operand that differs. |
| 5433 | for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i) |
| 5434 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5435 | if (GEPLHS->getOperand(i)->getType()->getPrimitiveSizeInBits() != |
| 5436 | GEPRHS->getOperand(i)->getType()->getPrimitiveSizeInBits()) { |
Chris Lattner | 45f57b8 | 2005-01-21 23:06:49 +0000 | [diff] [blame] | 5437 | // Irreconcilable differences. |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 5438 | NumDifferences = 2; |
| 5439 | break; |
| 5440 | } else { |
| 5441 | if (NumDifferences++) break; |
| 5442 | DiffOperand = i; |
| 5443 | } |
| 5444 | } |
| 5445 | |
| 5446 | if (NumDifferences == 0) // SAME GEP? |
| 5447 | return ReplaceInstUsesWith(I, // No comparison is needed here. |
Nick Lewycky | 455e176 | 2007-09-06 02:40:25 +0000 | [diff] [blame] | 5448 | ConstantInt::get(Type::Int1Ty, |
Nick Lewycky | fc1efbb | 2008-05-17 07:33:39 +0000 | [diff] [blame] | 5449 | ICmpInst::isTrueWhenEqual(Cond))); |
Nick Lewycky | 455e176 | 2007-09-06 02:40:25 +0000 | [diff] [blame] | 5450 | |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 5451 | else if (NumDifferences == 1) { |
Chris Lattner | 45f57b8 | 2005-01-21 23:06:49 +0000 | [diff] [blame] | 5452 | Value *LHSV = GEPLHS->getOperand(DiffOperand); |
| 5453 | Value *RHSV = GEPRHS->getOperand(DiffOperand); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5454 | // Make sure we do a signed comparison here. |
| 5455 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), LHSV, RHSV); |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 5456 | } |
| 5457 | } |
| 5458 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5459 | // 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] | 5460 | // the result to fold to a constant! |
| 5461 | if ((isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) && |
| 5462 | (isa<ConstantExpr>(GEPRHS) || GEPRHS->hasOneUse())) { |
| 5463 | // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) ---> (OFFSET1 cmp OFFSET2) |
| 5464 | Value *L = EmitGEPOffset(GEPLHS, I, *this); |
| 5465 | Value *R = EmitGEPOffset(GEPRHS, I, *this); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5466 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), L, R); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5467 | } |
| 5468 | } |
| 5469 | return 0; |
| 5470 | } |
| 5471 | |
Chris Lattner | a540623 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5472 | /// FoldFCmp_IntToFP_Cst - Fold fcmp ([us]itofp x, cst) if possible. |
| 5473 | /// |
| 5474 | Instruction *InstCombiner::FoldFCmp_IntToFP_Cst(FCmpInst &I, |
| 5475 | Instruction *LHSI, |
| 5476 | Constant *RHSC) { |
| 5477 | if (!isa<ConstantFP>(RHSC)) return 0; |
| 5478 | const APFloat &RHS = cast<ConstantFP>(RHSC)->getValueAPF(); |
| 5479 | |
| 5480 | // Get the width of the mantissa. We don't want to hack on conversions that |
| 5481 | // might lose information from the integer, e.g. "i64 -> float" |
Chris Lattner | 7be1c45 | 2008-05-19 21:17:23 +0000 | [diff] [blame] | 5482 | int MantissaWidth = LHSI->getType()->getFPMantissaWidth(); |
Chris Lattner | a540623 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5483 | if (MantissaWidth == -1) return 0; // Unknown. |
| 5484 | |
| 5485 | // Check to see that the input is converted from an integer type that is small |
| 5486 | // enough that preserves all bits. TODO: check here for "known" sign bits. |
| 5487 | // This would allow us to handle (fptosi (x >>s 62) to float) if x is i64 f.e. |
| 5488 | unsigned InputSize = LHSI->getOperand(0)->getType()->getPrimitiveSizeInBits(); |
| 5489 | |
| 5490 | // If this is a uitofp instruction, we need an extra bit to hold the sign. |
Bill Wendling | c143bcf | 2008-11-09 04:26:50 +0000 | [diff] [blame] | 5491 | bool LHSUnsigned = isa<UIToFPInst>(LHSI); |
| 5492 | if (LHSUnsigned) |
Chris Lattner | a540623 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5493 | ++InputSize; |
| 5494 | |
| 5495 | // If the conversion would lose info, don't hack on this. |
| 5496 | if ((int)InputSize > MantissaWidth) |
| 5497 | return 0; |
| 5498 | |
| 5499 | // Otherwise, we can potentially simplify the comparison. We know that it |
| 5500 | // will always come through as an integer value and we know the constant is |
| 5501 | // not a NAN (it would have been previously simplified). |
| 5502 | assert(!RHS.isNaN() && "NaN comparison not already folded!"); |
| 5503 | |
| 5504 | ICmpInst::Predicate Pred; |
| 5505 | switch (I.getPredicate()) { |
| 5506 | default: assert(0 && "Unexpected predicate!"); |
| 5507 | case FCmpInst::FCMP_UEQ: |
Bill Wendling | c143bcf | 2008-11-09 04:26:50 +0000 | [diff] [blame] | 5508 | case FCmpInst::FCMP_OEQ: |
| 5509 | Pred = ICmpInst::ICMP_EQ; |
| 5510 | break; |
Chris Lattner | a540623 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5511 | case FCmpInst::FCMP_UGT: |
Bill Wendling | c143bcf | 2008-11-09 04:26:50 +0000 | [diff] [blame] | 5512 | case FCmpInst::FCMP_OGT: |
| 5513 | Pred = LHSUnsigned ? ICmpInst::ICMP_UGT : ICmpInst::ICMP_SGT; |
| 5514 | break; |
Chris Lattner | a540623 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5515 | case FCmpInst::FCMP_UGE: |
Bill Wendling | c143bcf | 2008-11-09 04:26:50 +0000 | [diff] [blame] | 5516 | case FCmpInst::FCMP_OGE: |
| 5517 | Pred = LHSUnsigned ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_SGE; |
| 5518 | break; |
Chris Lattner | a540623 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5519 | case FCmpInst::FCMP_ULT: |
Bill Wendling | c143bcf | 2008-11-09 04:26:50 +0000 | [diff] [blame] | 5520 | case FCmpInst::FCMP_OLT: |
| 5521 | Pred = LHSUnsigned ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_SLT; |
| 5522 | break; |
Chris Lattner | a540623 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5523 | case FCmpInst::FCMP_ULE: |
Bill Wendling | c143bcf | 2008-11-09 04:26:50 +0000 | [diff] [blame] | 5524 | case FCmpInst::FCMP_OLE: |
| 5525 | Pred = LHSUnsigned ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_SLE; |
| 5526 | break; |
Chris Lattner | a540623 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5527 | case FCmpInst::FCMP_UNE: |
Bill Wendling | c143bcf | 2008-11-09 04:26:50 +0000 | [diff] [blame] | 5528 | case FCmpInst::FCMP_ONE: |
| 5529 | Pred = ICmpInst::ICMP_NE; |
| 5530 | break; |
Chris Lattner | a540623 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5531 | case FCmpInst::FCMP_ORD: |
Eli Friedman | 8b019c8 | 2008-11-30 22:48:49 +0000 | [diff] [blame] | 5532 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | a540623 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5533 | case FCmpInst::FCMP_UNO: |
Eli Friedman | 8b019c8 | 2008-11-30 22:48:49 +0000 | [diff] [blame] | 5534 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Chris Lattner | a540623 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5535 | } |
| 5536 | |
| 5537 | const IntegerType *IntTy = cast<IntegerType>(LHSI->getOperand(0)->getType()); |
| 5538 | |
| 5539 | // Now we know that the APFloat is a normal number, zero or inf. |
| 5540 | |
Chris Lattner | 8516278 | 2008-05-20 03:50:52 +0000 | [diff] [blame] | 5541 | // 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] | 5542 | // comparing an i8 to 300.0. |
| 5543 | unsigned IntWidth = IntTy->getPrimitiveSizeInBits(); |
| 5544 | |
Bill Wendling | c143bcf | 2008-11-09 04:26:50 +0000 | [diff] [blame] | 5545 | if (!LHSUnsigned) { |
| 5546 | // If the RHS value is > SignedMax, fold the comparison. This handles +INF |
| 5547 | // and large values. |
| 5548 | APFloat SMax(RHS.getSemantics(), APFloat::fcZero, false); |
| 5549 | SMax.convertFromAPInt(APInt::getSignedMaxValue(IntWidth), true, |
| 5550 | APFloat::rmNearestTiesToEven); |
| 5551 | if (SMax.compare(RHS) == APFloat::cmpLessThan) { // smax < 13123.0 |
| 5552 | if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SLT || |
| 5553 | Pred == ICmpInst::ICMP_SLE) |
Eli Friedman | 8b019c8 | 2008-11-30 22:48:49 +0000 | [diff] [blame] | 5554 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
| 5555 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Bill Wendling | c143bcf | 2008-11-09 04:26:50 +0000 | [diff] [blame] | 5556 | } |
| 5557 | } else { |
| 5558 | // If the RHS value is > UnsignedMax, fold the comparison. This handles |
| 5559 | // +INF and large values. |
| 5560 | APFloat UMax(RHS.getSemantics(), APFloat::fcZero, false); |
| 5561 | UMax.convertFromAPInt(APInt::getMaxValue(IntWidth), false, |
| 5562 | APFloat::rmNearestTiesToEven); |
| 5563 | if (UMax.compare(RHS) == APFloat::cmpLessThan) { // umax < 13123.0 |
| 5564 | if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_ULT || |
| 5565 | Pred == ICmpInst::ICMP_ULE) |
Eli Friedman | 8b019c8 | 2008-11-30 22:48:49 +0000 | [diff] [blame] | 5566 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
| 5567 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Bill Wendling | c143bcf | 2008-11-09 04:26:50 +0000 | [diff] [blame] | 5568 | } |
Chris Lattner | a540623 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5569 | } |
| 5570 | |
Bill Wendling | c143bcf | 2008-11-09 04:26:50 +0000 | [diff] [blame] | 5571 | if (!LHSUnsigned) { |
| 5572 | // See if the RHS value is < SignedMin. |
| 5573 | APFloat SMin(RHS.getSemantics(), APFloat::fcZero, false); |
| 5574 | SMin.convertFromAPInt(APInt::getSignedMinValue(IntWidth), true, |
| 5575 | APFloat::rmNearestTiesToEven); |
| 5576 | if (SMin.compare(RHS) == APFloat::cmpGreaterThan) { // smin > 12312.0 |
| 5577 | if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SGT || |
| 5578 | Pred == ICmpInst::ICMP_SGE) |
Eli Friedman | 8b019c8 | 2008-11-30 22:48:49 +0000 | [diff] [blame] | 5579 | return ReplaceInstUsesWith(I,ConstantInt::getTrue()); |
| 5580 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Bill Wendling | c143bcf | 2008-11-09 04:26:50 +0000 | [diff] [blame] | 5581 | } |
Chris Lattner | a540623 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5582 | } |
| 5583 | |
Bill Wendling | c143bcf | 2008-11-09 04:26:50 +0000 | [diff] [blame] | 5584 | // Okay, now we know that the FP constant fits in the range [SMIN, SMAX] or |
| 5585 | // [0, UMAX], but it may still be fractional. See if it is fractional by |
| 5586 | // casting the FP value to the integer value and back, checking for equality. |
| 5587 | // Don't do this for zero, because -0.0 is not fractional. |
Chris Lattner | a540623 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5588 | Constant *RHSInt = ConstantExpr::getFPToSI(RHSC, IntTy); |
| 5589 | if (!RHS.isZero() && |
| 5590 | ConstantExpr::getSIToFP(RHSInt, RHSC->getType()) != RHSC) { |
Bill Wendling | c143bcf | 2008-11-09 04:26:50 +0000 | [diff] [blame] | 5591 | // If we had a comparison against a fractional value, we have to adjust the |
| 5592 | // compare predicate and sometimes the value. RHSC is rounded towards zero |
| 5593 | // at this point. |
Chris Lattner | a540623 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5594 | switch (Pred) { |
| 5595 | default: assert(0 && "Unexpected integer comparison!"); |
| 5596 | case ICmpInst::ICMP_NE: // (float)int != 4.4 --> true |
Eli Friedman | 8b019c8 | 2008-11-30 22:48:49 +0000 | [diff] [blame] | 5597 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | a540623 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5598 | case ICmpInst::ICMP_EQ: // (float)int == 4.4 --> false |
Eli Friedman | 8b019c8 | 2008-11-30 22:48:49 +0000 | [diff] [blame] | 5599 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Bill Wendling | c143bcf | 2008-11-09 04:26:50 +0000 | [diff] [blame] | 5600 | case ICmpInst::ICMP_ULE: |
| 5601 | // (float)int <= 4.4 --> int <= 4 |
| 5602 | // (float)int <= -4.4 --> false |
| 5603 | if (RHS.isNegative()) |
Eli Friedman | 8b019c8 | 2008-11-30 22:48:49 +0000 | [diff] [blame] | 5604 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Bill Wendling | c143bcf | 2008-11-09 04:26:50 +0000 | [diff] [blame] | 5605 | break; |
Chris Lattner | a540623 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5606 | case ICmpInst::ICMP_SLE: |
| 5607 | // (float)int <= 4.4 --> int <= 4 |
| 5608 | // (float)int <= -4.4 --> int < -4 |
| 5609 | if (RHS.isNegative()) |
| 5610 | Pred = ICmpInst::ICMP_SLT; |
| 5611 | break; |
Bill Wendling | c143bcf | 2008-11-09 04:26:50 +0000 | [diff] [blame] | 5612 | case ICmpInst::ICMP_ULT: |
| 5613 | // (float)int < -4.4 --> false |
| 5614 | // (float)int < 4.4 --> int <= 4 |
| 5615 | if (RHS.isNegative()) |
Eli Friedman | 8b019c8 | 2008-11-30 22:48:49 +0000 | [diff] [blame] | 5616 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Bill Wendling | c143bcf | 2008-11-09 04:26:50 +0000 | [diff] [blame] | 5617 | Pred = ICmpInst::ICMP_ULE; |
| 5618 | break; |
Chris Lattner | a540623 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5619 | case ICmpInst::ICMP_SLT: |
| 5620 | // (float)int < -4.4 --> int < -4 |
| 5621 | // (float)int < 4.4 --> int <= 4 |
| 5622 | if (!RHS.isNegative()) |
| 5623 | Pred = ICmpInst::ICMP_SLE; |
| 5624 | break; |
Bill Wendling | c143bcf | 2008-11-09 04:26:50 +0000 | [diff] [blame] | 5625 | case ICmpInst::ICMP_UGT: |
| 5626 | // (float)int > 4.4 --> int > 4 |
| 5627 | // (float)int > -4.4 --> true |
| 5628 | if (RHS.isNegative()) |
Eli Friedman | 8b019c8 | 2008-11-30 22:48:49 +0000 | [diff] [blame] | 5629 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Bill Wendling | c143bcf | 2008-11-09 04:26:50 +0000 | [diff] [blame] | 5630 | break; |
Chris Lattner | a540623 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5631 | case ICmpInst::ICMP_SGT: |
| 5632 | // (float)int > 4.4 --> int > 4 |
| 5633 | // (float)int > -4.4 --> int >= -4 |
| 5634 | if (RHS.isNegative()) |
| 5635 | Pred = ICmpInst::ICMP_SGE; |
| 5636 | break; |
Bill Wendling | c143bcf | 2008-11-09 04:26:50 +0000 | [diff] [blame] | 5637 | case ICmpInst::ICMP_UGE: |
| 5638 | // (float)int >= -4.4 --> true |
| 5639 | // (float)int >= 4.4 --> int > 4 |
| 5640 | if (!RHS.isNegative()) |
Eli Friedman | 8b019c8 | 2008-11-30 22:48:49 +0000 | [diff] [blame] | 5641 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Bill Wendling | c143bcf | 2008-11-09 04:26:50 +0000 | [diff] [blame] | 5642 | Pred = ICmpInst::ICMP_UGT; |
| 5643 | break; |
Chris Lattner | a540623 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5644 | case ICmpInst::ICMP_SGE: |
| 5645 | // (float)int >= -4.4 --> int >= -4 |
| 5646 | // (float)int >= 4.4 --> int > 4 |
| 5647 | if (!RHS.isNegative()) |
| 5648 | Pred = ICmpInst::ICMP_SGT; |
| 5649 | break; |
| 5650 | } |
| 5651 | } |
| 5652 | |
| 5653 | // Lower this FP comparison into an appropriate integer version of the |
| 5654 | // comparison. |
| 5655 | return new ICmpInst(Pred, LHSI->getOperand(0), RHSInt); |
| 5656 | } |
| 5657 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5658 | Instruction *InstCombiner::visitFCmpInst(FCmpInst &I) { |
| 5659 | bool Changed = SimplifyCompare(I); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5660 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5661 | |
Chris Lattner | 58e9746 | 2007-01-14 19:42:17 +0000 | [diff] [blame] | 5662 | // Fold trivial predicates. |
| 5663 | if (I.getPredicate() == FCmpInst::FCMP_FALSE) |
Eli Friedman | 8b019c8 | 2008-11-30 22:48:49 +0000 | [diff] [blame] | 5664 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Chris Lattner | 58e9746 | 2007-01-14 19:42:17 +0000 | [diff] [blame] | 5665 | if (I.getPredicate() == FCmpInst::FCMP_TRUE) |
Eli Friedman | 8b019c8 | 2008-11-30 22:48:49 +0000 | [diff] [blame] | 5666 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | 58e9746 | 2007-01-14 19:42:17 +0000 | [diff] [blame] | 5667 | |
| 5668 | // Simplify 'fcmp pred X, X' |
| 5669 | if (Op0 == Op1) { |
| 5670 | switch (I.getPredicate()) { |
| 5671 | default: assert(0 && "Unknown predicate!"); |
| 5672 | case FCmpInst::FCMP_UEQ: // True if unordered or equal |
| 5673 | case FCmpInst::FCMP_UGE: // True if unordered, greater than, or equal |
| 5674 | case FCmpInst::FCMP_ULE: // True if unordered, less than, or equal |
Eli Friedman | 8b019c8 | 2008-11-30 22:48:49 +0000 | [diff] [blame] | 5675 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | 58e9746 | 2007-01-14 19:42:17 +0000 | [diff] [blame] | 5676 | case FCmpInst::FCMP_OGT: // True if ordered and greater than |
| 5677 | case FCmpInst::FCMP_OLT: // True if ordered and less than |
| 5678 | case FCmpInst::FCMP_ONE: // True if ordered and operands are unequal |
Eli Friedman | 8b019c8 | 2008-11-30 22:48:49 +0000 | [diff] [blame] | 5679 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Chris Lattner | 58e9746 | 2007-01-14 19:42:17 +0000 | [diff] [blame] | 5680 | |
| 5681 | case FCmpInst::FCMP_UNO: // True if unordered: isnan(X) | isnan(Y) |
| 5682 | case FCmpInst::FCMP_ULT: // True if unordered or less than |
| 5683 | case FCmpInst::FCMP_UGT: // True if unordered or greater than |
| 5684 | case FCmpInst::FCMP_UNE: // True if unordered or not equal |
| 5685 | // Canonicalize these to be 'fcmp uno %X, 0.0'. |
| 5686 | I.setPredicate(FCmpInst::FCMP_UNO); |
| 5687 | I.setOperand(1, Constant::getNullValue(Op0->getType())); |
| 5688 | return &I; |
| 5689 | |
| 5690 | case FCmpInst::FCMP_ORD: // True if ordered (no nans) |
| 5691 | case FCmpInst::FCMP_OEQ: // True if ordered and equal |
| 5692 | case FCmpInst::FCMP_OGE: // True if ordered and greater than or equal |
| 5693 | case FCmpInst::FCMP_OLE: // True if ordered and less than or equal |
| 5694 | // Canonicalize these to be 'fcmp ord %X, 0.0'. |
| 5695 | I.setPredicate(FCmpInst::FCMP_ORD); |
| 5696 | I.setOperand(1, Constant::getNullValue(Op0->getType())); |
| 5697 | return &I; |
| 5698 | } |
| 5699 | } |
| 5700 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5701 | if (isa<UndefValue>(Op1)) // fcmp pred X, undef -> undef |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 5702 | return ReplaceInstUsesWith(I, UndefValue::get(Type::Int1Ty)); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5703 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5704 | // Handle fcmp with constant RHS |
| 5705 | if (Constant *RHSC = dyn_cast<Constant>(Op1)) { |
Chris Lattner | a540623 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5706 | // If the constant is a nan, see if we can fold the comparison based on it. |
| 5707 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) { |
| 5708 | if (CFP->getValueAPF().isNaN()) { |
| 5709 | if (FCmpInst::isOrdered(I.getPredicate())) // True if ordered and... |
Eli Friedman | 8b019c8 | 2008-11-30 22:48:49 +0000 | [diff] [blame] | 5710 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Chris Lattner | 8516278 | 2008-05-20 03:50:52 +0000 | [diff] [blame] | 5711 | assert(FCmpInst::isUnordered(I.getPredicate()) && |
| 5712 | "Comparison must be either ordered or unordered!"); |
| 5713 | // True if unordered. |
Eli Friedman | 8b019c8 | 2008-11-30 22:48:49 +0000 | [diff] [blame] | 5714 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | a540623 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5715 | } |
| 5716 | } |
| 5717 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5718 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
| 5719 | switch (LHSI->getOpcode()) { |
| 5720 | case Instruction::PHI: |
Chris Lattner | 7d8ab4e | 2008-06-08 20:52:11 +0000 | [diff] [blame] | 5721 | // Only fold fcmp into the PHI if the phi and fcmp are in the same |
| 5722 | // block. If in the same block, we're encouraging jump threading. If |
| 5723 | // not, we are just pessimizing the code by making an i1 phi. |
| 5724 | if (LHSI->getParent() == I.getParent()) |
| 5725 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 5726 | return NV; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5727 | break; |
Chris Lattner | a540623 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5728 | case Instruction::SIToFP: |
| 5729 | case Instruction::UIToFP: |
| 5730 | if (Instruction *NV = FoldFCmp_IntToFP_Cst(I, LHSI, RHSC)) |
| 5731 | return NV; |
| 5732 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5733 | case Instruction::Select: |
| 5734 | // If either operand of the select is a constant, we can fold the |
| 5735 | // comparison into the select arms, which will cause one to be |
| 5736 | // constant folded and the select turned into a bitwise or. |
| 5737 | Value *Op1 = 0, *Op2 = 0; |
| 5738 | if (LHSI->hasOneUse()) { |
| 5739 | if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) { |
| 5740 | // Fold the known value into the constant operand. |
| 5741 | Op1 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC); |
| 5742 | // Insert a new FCmp of the other select operand. |
| 5743 | Op2 = InsertNewInstBefore(new FCmpInst(I.getPredicate(), |
| 5744 | LHSI->getOperand(2), RHSC, |
| 5745 | I.getName()), I); |
| 5746 | } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) { |
| 5747 | // Fold the known value into the constant operand. |
| 5748 | Op2 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC); |
| 5749 | // Insert a new FCmp of the other select operand. |
| 5750 | Op1 = InsertNewInstBefore(new FCmpInst(I.getPredicate(), |
| 5751 | LHSI->getOperand(1), RHSC, |
| 5752 | I.getName()), I); |
| 5753 | } |
| 5754 | } |
| 5755 | |
| 5756 | if (Op1) |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 5757 | return SelectInst::Create(LHSI->getOperand(0), Op1, Op2); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5758 | break; |
| 5759 | } |
| 5760 | } |
| 5761 | |
| 5762 | return Changed ? &I : 0; |
| 5763 | } |
| 5764 | |
| 5765 | Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { |
| 5766 | bool Changed = SimplifyCompare(I); |
| 5767 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 5768 | const Type *Ty = Op0->getType(); |
| 5769 | |
| 5770 | // icmp X, X |
| 5771 | if (Op0 == Op1) |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 5772 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
Nick Lewycky | fc1efbb | 2008-05-17 07:33:39 +0000 | [diff] [blame] | 5773 | I.isTrueWhenEqual())); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5774 | |
| 5775 | if (isa<UndefValue>(Op1)) // X icmp undef -> undef |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 5776 | return ReplaceInstUsesWith(I, UndefValue::get(Type::Int1Ty)); |
Christopher Lamb | 7a0678c | 2007-12-18 21:32:20 +0000 | [diff] [blame] | 5777 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5778 | // icmp <global/alloca*/null>, <global/alloca*/null> - Global/Stack value |
Chris Lattner | 711b340 | 2004-11-14 07:33:16 +0000 | [diff] [blame] | 5779 | // addresses never equal each other! We already know that Op0 != Op1. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5780 | if ((isa<GlobalValue>(Op0) || isa<AllocaInst>(Op0) || |
| 5781 | isa<ConstantPointerNull>(Op0)) && |
| 5782 | (isa<GlobalValue>(Op1) || isa<AllocaInst>(Op1) || |
Chris Lattner | 711b340 | 2004-11-14 07:33:16 +0000 | [diff] [blame] | 5783 | isa<ConstantPointerNull>(Op1))) |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 5784 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
Nick Lewycky | fc1efbb | 2008-05-17 07:33:39 +0000 | [diff] [blame] | 5785 | !I.isTrueWhenEqual())); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5786 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5787 | // icmp's with boolean values can always be turned into bitwise operations |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 5788 | if (Ty == Type::Int1Ty) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5789 | switch (I.getPredicate()) { |
| 5790 | default: assert(0 && "Invalid icmp instruction!"); |
Chris Lattner | 85b5eb0 | 2008-07-11 04:20:58 +0000 | [diff] [blame] | 5791 | case ICmpInst::ICMP_EQ: { // icmp eq i1 A, B -> ~(A^B) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5792 | Instruction *Xor = BinaryOperator::CreateXor(Op0, Op1, I.getName()+"tmp"); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5793 | InsertNewInstBefore(Xor, I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5794 | return BinaryOperator::CreateNot(Xor); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5795 | } |
Chris Lattner | 85b5eb0 | 2008-07-11 04:20:58 +0000 | [diff] [blame] | 5796 | case ICmpInst::ICMP_NE: // icmp eq i1 A, B -> A^B |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5797 | return BinaryOperator::CreateXor(Op0, Op1); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5798 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5799 | case ICmpInst::ICMP_UGT: |
Chris Lattner | 85b5eb0 | 2008-07-11 04:20:58 +0000 | [diff] [blame] | 5800 | std::swap(Op0, Op1); // Change icmp ugt -> icmp ult |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 5801 | // FALL THROUGH |
Chris Lattner | 85b5eb0 | 2008-07-11 04:20:58 +0000 | [diff] [blame] | 5802 | case ICmpInst::ICMP_ULT:{ // icmp ult i1 A, B -> ~A & B |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5803 | Instruction *Not = BinaryOperator::CreateNot(Op0, I.getName()+"tmp"); |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 5804 | InsertNewInstBefore(Not, I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5805 | return BinaryOperator::CreateAnd(Not, Op1); |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 5806 | } |
Chris Lattner | 85b5eb0 | 2008-07-11 04:20:58 +0000 | [diff] [blame] | 5807 | case ICmpInst::ICMP_SGT: |
| 5808 | std::swap(Op0, Op1); // Change icmp sgt -> icmp slt |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 5809 | // FALL THROUGH |
Chris Lattner | 85b5eb0 | 2008-07-11 04:20:58 +0000 | [diff] [blame] | 5810 | case ICmpInst::ICMP_SLT: { // icmp slt i1 A, B -> A & ~B |
| 5811 | Instruction *Not = BinaryOperator::CreateNot(Op1, I.getName()+"tmp"); |
| 5812 | InsertNewInstBefore(Not, I); |
| 5813 | return BinaryOperator::CreateAnd(Not, Op0); |
| 5814 | } |
| 5815 | case ICmpInst::ICMP_UGE: |
| 5816 | std::swap(Op0, Op1); // Change icmp uge -> icmp ule |
| 5817 | // FALL THROUGH |
| 5818 | case ICmpInst::ICMP_ULE: { // icmp ule i1 A, B -> ~A | B |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5819 | Instruction *Not = BinaryOperator::CreateNot(Op0, I.getName()+"tmp"); |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 5820 | InsertNewInstBefore(Not, I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5821 | return BinaryOperator::CreateOr(Not, Op1); |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 5822 | } |
Chris Lattner | 85b5eb0 | 2008-07-11 04:20:58 +0000 | [diff] [blame] | 5823 | case ICmpInst::ICMP_SGE: |
| 5824 | std::swap(Op0, Op1); // Change icmp sge -> icmp sle |
| 5825 | // FALL THROUGH |
| 5826 | case ICmpInst::ICMP_SLE: { // icmp sle i1 A, B -> A | ~B |
| 5827 | Instruction *Not = BinaryOperator::CreateNot(Op1, I.getName()+"tmp"); |
| 5828 | InsertNewInstBefore(Not, I); |
| 5829 | return BinaryOperator::CreateOr(Not, Op0); |
| 5830 | } |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 5831 | } |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5832 | } |
| 5833 | |
Dan Gohman | 81b28ce | 2008-09-16 18:46:06 +0000 | [diff] [blame] | 5834 | // See if we are doing a comparison with a constant. |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5835 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Nick Lewycky | 579214a | 2009-02-27 06:37:39 +0000 | [diff] [blame] | 5836 | Value *A = 0, *B = 0; |
Christopher Lamb | 103e1a3 | 2007-12-20 07:21:11 +0000 | [diff] [blame] | 5837 | |
Chris Lattner | b656601 | 2008-01-05 01:18:20 +0000 | [diff] [blame] | 5838 | // (icmp ne/eq (sub A B) 0) -> (icmp ne/eq A, B) |
| 5839 | if (I.isEquality() && CI->isNullValue() && |
| 5840 | match(Op0, m_Sub(m_Value(A), m_Value(B)))) { |
| 5841 | // (icmp cond A B) if cond is equality |
| 5842 | return new ICmpInst(I.getPredicate(), A, B); |
Owen Anderson | f5783f8 | 2007-12-28 07:42:12 +0000 | [diff] [blame] | 5843 | } |
Christopher Lamb | 103e1a3 | 2007-12-20 07:21:11 +0000 | [diff] [blame] | 5844 | |
Dan Gohman | 81b28ce | 2008-09-16 18:46:06 +0000 | [diff] [blame] | 5845 | // If we have an icmp le or icmp ge instruction, turn it into the |
| 5846 | // appropriate icmp lt or icmp gt instruction. This allows us to rely on |
| 5847 | // them being folded in the code below. |
Chris Lattner | 84dff67 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 5848 | switch (I.getPredicate()) { |
| 5849 | default: break; |
| 5850 | case ICmpInst::ICMP_ULE: |
| 5851 | if (CI->isMaxValue(false)) // A <=u MAX -> TRUE |
| 5852 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
| 5853 | return new ICmpInst(ICmpInst::ICMP_ULT, Op0, AddOne(CI)); |
| 5854 | case ICmpInst::ICMP_SLE: |
| 5855 | if (CI->isMaxValue(true)) // A <=s MAX -> TRUE |
| 5856 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
| 5857 | return new ICmpInst(ICmpInst::ICMP_SLT, Op0, AddOne(CI)); |
| 5858 | case ICmpInst::ICMP_UGE: |
| 5859 | if (CI->isMinValue(false)) // A >=u MIN -> TRUE |
| 5860 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
| 5861 | return new ICmpInst( ICmpInst::ICMP_UGT, Op0, SubOne(CI)); |
| 5862 | case ICmpInst::ICMP_SGE: |
| 5863 | if (CI->isMinValue(true)) // A >=s MIN -> TRUE |
| 5864 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
| 5865 | return new ICmpInst(ICmpInst::ICMP_SGT, Op0, SubOne(CI)); |
| 5866 | } |
| 5867 | |
Chris Lattner | 183661e | 2008-07-11 05:40:05 +0000 | [diff] [blame] | 5868 | // See if we can fold the comparison based on range information we can get |
| 5869 | // by checking whether bits are known to be zero or one in the input. |
| 5870 | uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth(); |
| 5871 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 5872 | |
| 5873 | // If this comparison is a normal comparison, it demands all |
Chris Lattner | 4241e4d | 2007-07-15 20:54:51 +0000 | [diff] [blame] | 5874 | // 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] | 5875 | bool UnusedBit; |
| 5876 | bool isSignBit = isSignBitCheck(I.getPredicate(), CI, UnusedBit); |
| 5877 | |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 5878 | if (SimplifyDemandedBits(I.getOperandUse(0), |
Chris Lattner | 4241e4d | 2007-07-15 20:54:51 +0000 | [diff] [blame] | 5879 | isSignBit ? APInt::getSignBit(BitWidth) |
| 5880 | : APInt::getAllOnesValue(BitWidth), |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 5881 | KnownZero, KnownOne, 0)) |
| 5882 | return &I; |
| 5883 | |
| 5884 | // 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] | 5885 | // in. Compute the Min, Max and RHS values based on the known bits. For the |
| 5886 | // EQ and NE we use unsigned values. |
| 5887 | APInt Min(BitWidth, 0), Max(BitWidth, 0); |
Chris Lattner | 84dff67 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 5888 | if (ICmpInst::isSignedPredicate(I.getPredicate())) |
| 5889 | ComputeSignedMinMaxValuesFromKnownBits(Ty, KnownZero, KnownOne, Min, Max); |
| 5890 | else |
| 5891 | ComputeUnsignedMinMaxValuesFromKnownBits(Ty, KnownZero, KnownOne,Min,Max); |
| 5892 | |
Chris Lattner | 183661e | 2008-07-11 05:40:05 +0000 | [diff] [blame] | 5893 | // If Min and Max are known to be the same, then SimplifyDemandedBits |
| 5894 | // figured out that the LHS is a constant. Just constant fold this now so |
| 5895 | // that code below can assume that Min != Max. |
| 5896 | if (Min == Max) |
| 5897 | return ReplaceInstUsesWith(I, ConstantExpr::getICmp(I.getPredicate(), |
| 5898 | ConstantInt::get(Min), |
| 5899 | CI)); |
| 5900 | |
| 5901 | // Based on the range information we know about the LHS, see if we can |
| 5902 | // simplify this comparison. For example, (x&4) < 8 is always true. |
| 5903 | const APInt &RHSVal = CI->getValue(); |
Chris Lattner | 84dff67 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 5904 | switch (I.getPredicate()) { // LE/GE have been folded already. |
| 5905 | default: assert(0 && "Unknown icmp opcode!"); |
| 5906 | case ICmpInst::ICMP_EQ: |
| 5907 | if (Max.ult(RHSVal) || Min.ugt(RHSVal)) |
| 5908 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
| 5909 | break; |
| 5910 | case ICmpInst::ICMP_NE: |
| 5911 | if (Max.ult(RHSVal) || Min.ugt(RHSVal)) |
| 5912 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
| 5913 | break; |
| 5914 | case ICmpInst::ICMP_ULT: |
Chris Lattner | 183661e | 2008-07-11 05:40:05 +0000 | [diff] [blame] | 5915 | if (Max.ult(RHSVal)) // A <u C -> true iff max(A) < C |
Chris Lattner | 84dff67 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 5916 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | 183661e | 2008-07-11 05:40:05 +0000 | [diff] [blame] | 5917 | if (Min.uge(RHSVal)) // A <u C -> false iff min(A) >= C |
Chris Lattner | 84dff67 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 5918 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Chris Lattner | 183661e | 2008-07-11 05:40:05 +0000 | [diff] [blame] | 5919 | if (RHSVal == Max) // A <u MAX -> A != MAX |
| 5920 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
| 5921 | if (RHSVal == Min+1) // A <u MIN+1 -> A == MIN |
| 5922 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, SubOne(CI)); |
| 5923 | |
| 5924 | // (x <u 2147483648) -> (x >s -1) -> true if sign bit clear |
| 5925 | if (CI->isMinValue(true)) |
| 5926 | return new ICmpInst(ICmpInst::ICMP_SGT, Op0, |
| 5927 | ConstantInt::getAllOnesValue(Op0->getType())); |
Chris Lattner | 84dff67 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 5928 | break; |
| 5929 | case ICmpInst::ICMP_UGT: |
Chris Lattner | 183661e | 2008-07-11 05:40:05 +0000 | [diff] [blame] | 5930 | if (Min.ugt(RHSVal)) // A >u C -> true iff min(A) > C |
Chris Lattner | 84dff67 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 5931 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | 183661e | 2008-07-11 05:40:05 +0000 | [diff] [blame] | 5932 | if (Max.ule(RHSVal)) // A >u C -> false iff max(A) <= C |
Chris Lattner | 84dff67 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 5933 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Chris Lattner | 183661e | 2008-07-11 05:40:05 +0000 | [diff] [blame] | 5934 | |
| 5935 | if (RHSVal == Min) // A >u MIN -> A != MIN |
| 5936 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
| 5937 | if (RHSVal == Max-1) // A >u MAX-1 -> A == MAX |
| 5938 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, AddOne(CI)); |
| 5939 | |
| 5940 | // (x >u 2147483647) -> (x <s 0) -> true if sign bit set |
| 5941 | if (CI->isMaxValue(true)) |
| 5942 | return new ICmpInst(ICmpInst::ICMP_SLT, Op0, |
| 5943 | ConstantInt::getNullValue(Op0->getType())); |
Chris Lattner | 84dff67 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 5944 | break; |
| 5945 | case ICmpInst::ICMP_SLT: |
Chris Lattner | 183661e | 2008-07-11 05:40:05 +0000 | [diff] [blame] | 5946 | if (Max.slt(RHSVal)) // A <s C -> true iff max(A) < C |
Chris Lattner | 84dff67 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 5947 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | d01bee7 | 2008-07-11 06:40:29 +0000 | [diff] [blame] | 5948 | if (Min.sge(RHSVal)) // A <s C -> false iff min(A) >= C |
Chris Lattner | 84dff67 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 5949 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Chris Lattner | 183661e | 2008-07-11 05:40:05 +0000 | [diff] [blame] | 5950 | if (RHSVal == Max) // A <s MAX -> A != MAX |
| 5951 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
Chris Lattner | a8ff4a8 | 2008-07-11 06:36:01 +0000 | [diff] [blame] | 5952 | if (RHSVal == Min+1) // A <s MIN+1 -> A == MIN |
Chris Lattner | f9685ac | 2008-07-11 06:38:16 +0000 | [diff] [blame] | 5953 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, SubOne(CI)); |
Chris Lattner | 84dff67 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 5954 | break; |
| 5955 | case ICmpInst::ICMP_SGT: |
Chris Lattner | 183661e | 2008-07-11 05:40:05 +0000 | [diff] [blame] | 5956 | if (Min.sgt(RHSVal)) // A >s C -> true iff min(A) > C |
Chris Lattner | 84dff67 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 5957 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | 183661e | 2008-07-11 05:40:05 +0000 | [diff] [blame] | 5958 | if (Max.sle(RHSVal)) // A >s C -> false iff max(A) <= C |
Chris Lattner | 84dff67 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 5959 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Chris Lattner | 183661e | 2008-07-11 05:40:05 +0000 | [diff] [blame] | 5960 | |
| 5961 | if (RHSVal == Min) // A >s MIN -> A != MIN |
| 5962 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
| 5963 | if (RHSVal == Max-1) // A >s MAX-1 -> A == MAX |
| 5964 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, AddOne(CI)); |
Chris Lattner | 84dff67 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 5965 | break; |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 5966 | } |
Dan Gohman | 81b28ce | 2008-09-16 18:46:06 +0000 | [diff] [blame] | 5967 | } |
| 5968 | |
| 5969 | // Test if the ICmpInst instruction is used exclusively by a select as |
| 5970 | // part of a minimum or maximum operation. If so, refrain from doing |
| 5971 | // any other folding. This helps out other analyses which understand |
| 5972 | // non-obfuscated minimum and maximum idioms, such as ScalarEvolution |
| 5973 | // and CodeGen. And in this case, at least one of the comparison |
| 5974 | // operands has at least one user besides the compare (the select), |
| 5975 | // which would often largely negate the benefit of folding anyway. |
| 5976 | if (I.hasOneUse()) |
| 5977 | if (SelectInst *SI = dyn_cast<SelectInst>(*I.use_begin())) |
| 5978 | if ((SI->getOperand(1) == Op0 && SI->getOperand(2) == Op1) || |
| 5979 | (SI->getOperand(2) == Op0 && SI->getOperand(1) == Op1)) |
| 5980 | return 0; |
| 5981 | |
| 5982 | // See if we are doing a comparison between a constant and an instruction that |
| 5983 | // can be folded into the comparison. |
| 5984 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5985 | // 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] | 5986 | // instruction, see if that instruction also has constants so that the |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5987 | // instruction can be folded into the icmp |
Chris Lattner | 3c6a0d4 | 2004-05-25 06:32:08 +0000 | [diff] [blame] | 5988 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5989 | if (Instruction *Res = visitICmpInstWithInstAndIntCst(I, LHSI, CI)) |
| 5990 | return Res; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5991 | } |
| 5992 | |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5993 | // Handle icmp with constant (but not simple integer constant) RHS |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5994 | if (Constant *RHSC = dyn_cast<Constant>(Op1)) { |
| 5995 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
| 5996 | switch (LHSI->getOpcode()) { |
Chris Lattner | 9fb25db | 2005-05-01 04:42:15 +0000 | [diff] [blame] | 5997 | case Instruction::GetElementPtr: |
| 5998 | if (RHSC->isNullValue()) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5999 | // 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] | 6000 | bool isAllZeros = true; |
| 6001 | for (unsigned i = 1, e = LHSI->getNumOperands(); i != e; ++i) |
| 6002 | if (!isa<Constant>(LHSI->getOperand(i)) || |
| 6003 | !cast<Constant>(LHSI->getOperand(i))->isNullValue()) { |
| 6004 | isAllZeros = false; |
| 6005 | break; |
| 6006 | } |
| 6007 | if (isAllZeros) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6008 | return new ICmpInst(I.getPredicate(), LHSI->getOperand(0), |
Chris Lattner | 9fb25db | 2005-05-01 04:42:15 +0000 | [diff] [blame] | 6009 | Constant::getNullValue(LHSI->getOperand(0)->getType())); |
| 6010 | } |
| 6011 | break; |
| 6012 | |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 6013 | case Instruction::PHI: |
Chris Lattner | 7d8ab4e | 2008-06-08 20:52:11 +0000 | [diff] [blame] | 6014 | // Only fold icmp into the PHI if the phi and fcmp are in the same |
| 6015 | // block. If in the same block, we're encouraging jump threading. If |
| 6016 | // not, we are just pessimizing the code by making an i1 phi. |
| 6017 | if (LHSI->getParent() == I.getParent()) |
| 6018 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 6019 | return NV; |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 6020 | break; |
Chris Lattner | 4802d90 | 2007-04-06 18:57:34 +0000 | [diff] [blame] | 6021 | case Instruction::Select: { |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 6022 | // If either operand of the select is a constant, we can fold the |
| 6023 | // comparison into the select arms, which will cause one to be |
| 6024 | // constant folded and the select turned into a bitwise or. |
| 6025 | Value *Op1 = 0, *Op2 = 0; |
| 6026 | if (LHSI->hasOneUse()) { |
| 6027 | if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) { |
| 6028 | // Fold the known value into the constant operand. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6029 | Op1 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC); |
| 6030 | // Insert a new ICmp of the other select operand. |
| 6031 | Op2 = InsertNewInstBefore(new ICmpInst(I.getPredicate(), |
| 6032 | LHSI->getOperand(2), RHSC, |
| 6033 | I.getName()), I); |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 6034 | } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) { |
| 6035 | // Fold the known value into the constant operand. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6036 | Op2 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC); |
| 6037 | // Insert a new ICmp of the other select operand. |
| 6038 | Op1 = InsertNewInstBefore(new ICmpInst(I.getPredicate(), |
| 6039 | LHSI->getOperand(1), RHSC, |
| 6040 | I.getName()), I); |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 6041 | } |
| 6042 | } |
Jeff Cohen | 9d80930 | 2005-04-23 21:38:35 +0000 | [diff] [blame] | 6043 | |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 6044 | if (Op1) |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 6045 | return SelectInst::Create(LHSI->getOperand(0), Op1, Op2); |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 6046 | break; |
| 6047 | } |
Chris Lattner | 4802d90 | 2007-04-06 18:57:34 +0000 | [diff] [blame] | 6048 | case Instruction::Malloc: |
| 6049 | // If we have (malloc != null), and if the malloc has a single use, we |
| 6050 | // can assume it is successful and remove the malloc. |
| 6051 | if (LHSI->hasOneUse() && isa<ConstantPointerNull>(RHSC)) { |
| 6052 | AddToWorkList(LHSI); |
| 6053 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
Nick Lewycky | fc1efbb | 2008-05-17 07:33:39 +0000 | [diff] [blame] | 6054 | !I.isTrueWhenEqual())); |
Chris Lattner | 4802d90 | 2007-04-06 18:57:34 +0000 | [diff] [blame] | 6055 | } |
| 6056 | break; |
| 6057 | } |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 6058 | } |
| 6059 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6060 | // 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] | 6061 | if (User *GEP = dyn_castGetElementPtr(Op0)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6062 | if (Instruction *NI = FoldGEPICmp(GEP, Op1, I.getPredicate(), I)) |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 6063 | return NI; |
| 6064 | if (User *GEP = dyn_castGetElementPtr(Op1)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6065 | if (Instruction *NI = FoldGEPICmp(GEP, Op0, |
| 6066 | ICmpInst::getSwappedPredicate(I.getPredicate()), I)) |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 6067 | return NI; |
| 6068 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6069 | // 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] | 6070 | // values. If the ptr->ptr cast can be stripped off both arguments, we do so |
| 6071 | // now. |
| 6072 | if (BitCastInst *CI = dyn_cast<BitCastInst>(Op0)) { |
| 6073 | if (isa<PointerType>(Op0->getType()) && |
| 6074 | (isa<Constant>(Op1) || isa<BitCastInst>(Op1))) { |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 6075 | // We keep moving the cast from the left operand over to the right |
| 6076 | // operand, where it can often be eliminated completely. |
Chris Lattner | 57d8637 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 6077 | Op0 = CI->getOperand(0); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6078 | |
Chris Lattner | 57d8637 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 6079 | // If operand #1 is a bitcast instruction, it must also be a ptr->ptr cast |
| 6080 | // so eliminate it as well. |
| 6081 | if (BitCastInst *CI2 = dyn_cast<BitCastInst>(Op1)) |
| 6082 | Op1 = CI2->getOperand(0); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6083 | |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 6084 | // 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] | 6085 | if (Op0->getType() != Op1->getType()) { |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 6086 | if (Constant *Op1C = dyn_cast<Constant>(Op1)) { |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 6087 | Op1 = ConstantExpr::getBitCast(Op1C, Op0->getType()); |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 6088 | } else { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6089 | // Otherwise, cast the RHS right before the icmp |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 6090 | Op1 = InsertBitCastBefore(Op1, Op0->getType(), I); |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 6091 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 6092 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6093 | return new ICmpInst(I.getPredicate(), Op0, Op1); |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 6094 | } |
Chris Lattner | 57d8637 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 6095 | } |
| 6096 | |
| 6097 | if (isa<CastInst>(Op0)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6098 | // Handle the special case of: icmp (cast bool to X), <cst> |
Chris Lattner | 6870805 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 6099 | // This comes up when you have code like |
| 6100 | // int X = A < B; |
| 6101 | // if (X) ... |
| 6102 | // For generality, we handle any zero-extension of any operand comparison |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 6103 | // with a constant or another cast from the same type. |
| 6104 | if (isa<ConstantInt>(Op1) || isa<CastInst>(Op1)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6105 | if (Instruction *R = visitICmpInstWithCastAndCast(I)) |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 6106 | return R; |
Chris Lattner | 6870805 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 6107 | } |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 6108 | |
Nick Lewycky | 4bf1e59 | 2008-07-11 07:20:53 +0000 | [diff] [blame] | 6109 | // See if it's the same type of instruction on the left and right. |
| 6110 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) { |
| 6111 | if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) { |
Nick Lewycky | 5d52c45 | 2008-08-21 05:56:10 +0000 | [diff] [blame] | 6112 | if (Op0I->getOpcode() == Op1I->getOpcode() && Op0I->hasOneUse() && |
Nick Lewycky | 4333f49 | 2009-01-31 21:30:05 +0000 | [diff] [blame] | 6113 | Op1I->hasOneUse() && Op0I->getOperand(1) == Op1I->getOperand(1)) { |
Nick Lewycky | 23c0430 | 2008-09-03 06:24:21 +0000 | [diff] [blame] | 6114 | switch (Op0I->getOpcode()) { |
Nick Lewycky | 4bf1e59 | 2008-07-11 07:20:53 +0000 | [diff] [blame] | 6115 | default: break; |
| 6116 | case Instruction::Add: |
| 6117 | case Instruction::Sub: |
| 6118 | case Instruction::Xor: |
Chris Lattner | f5db1fb | 2009-02-02 07:15:30 +0000 | [diff] [blame] | 6119 | if (I.isEquality()) // a+x icmp eq/ne b+x --> a icmp b |
Nick Lewycky | 4333f49 | 2009-01-31 21:30:05 +0000 | [diff] [blame] | 6120 | return new ICmpInst(I.getPredicate(), Op0I->getOperand(0), |
| 6121 | Op1I->getOperand(0)); |
Chris Lattner | f5db1fb | 2009-02-02 07:15:30 +0000 | [diff] [blame] | 6122 | // icmp u/s (a ^ signbit), (b ^ signbit) --> icmp s/u a, b |
| 6123 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) { |
| 6124 | if (CI->getValue().isSignBit()) { |
| 6125 | ICmpInst::Predicate Pred = I.isSignedPredicate() |
| 6126 | ? I.getUnsignedPredicate() |
| 6127 | : I.getSignedPredicate(); |
| 6128 | return new ICmpInst(Pred, Op0I->getOperand(0), |
| 6129 | Op1I->getOperand(0)); |
| 6130 | } |
| 6131 | |
| 6132 | if (CI->getValue().isMaxSignedValue()) { |
| 6133 | ICmpInst::Predicate Pred = I.isSignedPredicate() |
| 6134 | ? I.getUnsignedPredicate() |
| 6135 | : I.getSignedPredicate(); |
| 6136 | Pred = I.getSwappedPredicate(Pred); |
| 6137 | return new ICmpInst(Pred, Op0I->getOperand(0), |
| 6138 | Op1I->getOperand(0)); |
Nick Lewycky | 4333f49 | 2009-01-31 21:30:05 +0000 | [diff] [blame] | 6139 | } |
| 6140 | } |
Nick Lewycky | 4bf1e59 | 2008-07-11 07:20:53 +0000 | [diff] [blame] | 6141 | break; |
| 6142 | case Instruction::Mul: |
Nick Lewycky | 4333f49 | 2009-01-31 21:30:05 +0000 | [diff] [blame] | 6143 | if (!I.isEquality()) |
| 6144 | break; |
| 6145 | |
Nick Lewycky | 5d52c45 | 2008-08-21 05:56:10 +0000 | [diff] [blame] | 6146 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) { |
| 6147 | // a * Cst icmp eq/ne b * Cst --> a & Mask icmp b & Mask |
| 6148 | // Mask = -1 >> count-trailing-zeros(Cst). |
| 6149 | if (!CI->isZero() && !CI->isOne()) { |
| 6150 | const APInt &AP = CI->getValue(); |
| 6151 | ConstantInt *Mask = ConstantInt::get( |
| 6152 | APInt::getLowBitsSet(AP.getBitWidth(), |
| 6153 | AP.getBitWidth() - |
Nick Lewycky | 4bf1e59 | 2008-07-11 07:20:53 +0000 | [diff] [blame] | 6154 | AP.countTrailingZeros())); |
Nick Lewycky | 5d52c45 | 2008-08-21 05:56:10 +0000 | [diff] [blame] | 6155 | Instruction *And1 = BinaryOperator::CreateAnd(Op0I->getOperand(0), |
| 6156 | Mask); |
| 6157 | Instruction *And2 = BinaryOperator::CreateAnd(Op1I->getOperand(0), |
| 6158 | Mask); |
| 6159 | InsertNewInstBefore(And1, I); |
| 6160 | InsertNewInstBefore(And2, I); |
| 6161 | return new ICmpInst(I.getPredicate(), And1, And2); |
Nick Lewycky | 4bf1e59 | 2008-07-11 07:20:53 +0000 | [diff] [blame] | 6162 | } |
| 6163 | } |
| 6164 | break; |
| 6165 | } |
| 6166 | } |
| 6167 | } |
| 6168 | } |
| 6169 | |
Chris Lattner | 7d2cbd2 | 2008-05-09 05:19:28 +0000 | [diff] [blame] | 6170 | // ~x < ~y --> y < x |
| 6171 | { Value *A, *B; |
| 6172 | if (match(Op0, m_Not(m_Value(A))) && |
| 6173 | match(Op1, m_Not(m_Value(B)))) |
| 6174 | return new ICmpInst(I.getPredicate(), B, A); |
| 6175 | } |
| 6176 | |
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 6177 | if (I.isEquality()) { |
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 6178 | Value *A, *B, *C, *D; |
Chris Lattner | 7d2cbd2 | 2008-05-09 05:19:28 +0000 | [diff] [blame] | 6179 | |
| 6180 | // -x == -y --> x == y |
| 6181 | if (match(Op0, m_Neg(m_Value(A))) && |
| 6182 | match(Op1, m_Neg(m_Value(B)))) |
| 6183 | return new ICmpInst(I.getPredicate(), A, B); |
| 6184 | |
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 6185 | if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) { |
| 6186 | if (A == Op1 || B == Op1) { // (A^B) == A -> B == 0 |
| 6187 | Value *OtherVal = A == Op1 ? B : A; |
| 6188 | return new ICmpInst(I.getPredicate(), OtherVal, |
| 6189 | Constant::getNullValue(A->getType())); |
| 6190 | } |
| 6191 | |
| 6192 | if (match(Op1, m_Xor(m_Value(C), m_Value(D)))) { |
| 6193 | // A^c1 == C^c2 --> A == C^(c1^c2) |
Chris Lattner | cb504b9 | 2008-11-16 05:38:51 +0000 | [diff] [blame] | 6194 | ConstantInt *C1, *C2; |
| 6195 | if (match(B, m_ConstantInt(C1)) && |
| 6196 | match(D, m_ConstantInt(C2)) && Op1->hasOneUse()) { |
| 6197 | Constant *NC = ConstantInt::get(C1->getValue() ^ C2->getValue()); |
| 6198 | Instruction *Xor = BinaryOperator::CreateXor(C, NC, "tmp"); |
| 6199 | return new ICmpInst(I.getPredicate(), A, |
| 6200 | InsertNewInstBefore(Xor, I)); |
| 6201 | } |
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 6202 | |
| 6203 | // A^B == A^D -> B == D |
| 6204 | if (A == C) return new ICmpInst(I.getPredicate(), B, D); |
| 6205 | if (A == D) return new ICmpInst(I.getPredicate(), B, C); |
| 6206 | if (B == C) return new ICmpInst(I.getPredicate(), A, D); |
| 6207 | if (B == D) return new ICmpInst(I.getPredicate(), A, C); |
| 6208 | } |
| 6209 | } |
| 6210 | |
| 6211 | if (match(Op1, m_Xor(m_Value(A), m_Value(B))) && |
| 6212 | (A == Op0 || B == Op0)) { |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 6213 | // A == (A^B) -> B == 0 |
| 6214 | Value *OtherVal = A == Op0 ? B : A; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6215 | return new ICmpInst(I.getPredicate(), OtherVal, |
| 6216 | Constant::getNullValue(A->getType())); |
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 6217 | } |
Chris Lattner | cb504b9 | 2008-11-16 05:38:51 +0000 | [diff] [blame] | 6218 | |
| 6219 | // (A-B) == A -> B == 0 |
| 6220 | if (match(Op0, m_Sub(m_Specific(Op1), m_Value(B)))) |
| 6221 | return new ICmpInst(I.getPredicate(), B, |
| 6222 | Constant::getNullValue(B->getType())); |
| 6223 | |
| 6224 | // A == (A-B) -> B == 0 |
| 6225 | if (match(Op1, m_Sub(m_Specific(Op0), m_Value(B)))) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6226 | return new ICmpInst(I.getPredicate(), B, |
| 6227 | Constant::getNullValue(B->getType())); |
Chris Lattner | 9c2328e | 2006-11-14 06:06:06 +0000 | [diff] [blame] | 6228 | |
Chris Lattner | 9c2328e | 2006-11-14 06:06:06 +0000 | [diff] [blame] | 6229 | // (X&Z) == (Y&Z) -> (X^Y) & Z == 0 |
| 6230 | if (Op0->hasOneUse() && Op1->hasOneUse() && |
| 6231 | match(Op0, m_And(m_Value(A), m_Value(B))) && |
| 6232 | match(Op1, m_And(m_Value(C), m_Value(D)))) { |
| 6233 | Value *X = 0, *Y = 0, *Z = 0; |
| 6234 | |
| 6235 | if (A == C) { |
| 6236 | X = B; Y = D; Z = A; |
| 6237 | } else if (A == D) { |
| 6238 | X = B; Y = C; Z = A; |
| 6239 | } else if (B == C) { |
| 6240 | X = A; Y = D; Z = B; |
| 6241 | } else if (B == D) { |
| 6242 | X = A; Y = C; Z = B; |
| 6243 | } |
| 6244 | |
| 6245 | if (X) { // Build (X^Y) & Z |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6246 | Op1 = InsertNewInstBefore(BinaryOperator::CreateXor(X, Y, "tmp"), I); |
| 6247 | Op1 = InsertNewInstBefore(BinaryOperator::CreateAnd(Op1, Z, "tmp"), I); |
Chris Lattner | 9c2328e | 2006-11-14 06:06:06 +0000 | [diff] [blame] | 6248 | I.setOperand(0, Op1); |
| 6249 | I.setOperand(1, Constant::getNullValue(Op1->getType())); |
| 6250 | return &I; |
| 6251 | } |
| 6252 | } |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 6253 | } |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 6254 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 6255 | } |
| 6256 | |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 6257 | |
| 6258 | /// FoldICmpDivCst - Fold "icmp pred, ([su]div X, DivRHS), CmpRHS" where DivRHS |
| 6259 | /// and CmpRHS are both known to be integer constants. |
| 6260 | Instruction *InstCombiner::FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI, |
| 6261 | ConstantInt *DivRHS) { |
| 6262 | ConstantInt *CmpRHS = cast<ConstantInt>(ICI.getOperand(1)); |
| 6263 | const APInt &CmpRHSV = CmpRHS->getValue(); |
| 6264 | |
| 6265 | // FIXME: If the operand types don't match the type of the divide |
| 6266 | // then don't attempt this transform. The code below doesn't have the |
| 6267 | // logic to deal with a signed divide and an unsigned compare (and |
| 6268 | // vice versa). This is because (x /s C1) <s C2 produces different |
| 6269 | // results than (x /s C1) <u C2 or (x /u C1) <s C2 or even |
| 6270 | // (x /u C1) <u C2. Simply casting the operands and result won't |
| 6271 | // work. :( The if statement below tests that condition and bails |
| 6272 | // if it finds it. |
| 6273 | bool DivIsSigned = DivI->getOpcode() == Instruction::SDiv; |
| 6274 | if (!ICI.isEquality() && DivIsSigned != ICI.isSignedPredicate()) |
| 6275 | return 0; |
| 6276 | if (DivRHS->isZero()) |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 6277 | return 0; // The ProdOV computation fails on divide by zero. |
Chris Lattner | a6321b4 | 2008-10-11 22:55:00 +0000 | [diff] [blame] | 6278 | if (DivIsSigned && DivRHS->isAllOnesValue()) |
| 6279 | return 0; // The overflow computation also screws up here |
| 6280 | if (DivRHS->isOne()) |
| 6281 | return 0; // Not worth bothering, and eliminates some funny cases |
| 6282 | // with INT_MIN. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 6283 | |
| 6284 | // Compute Prod = CI * DivRHS. We are essentially solving an equation |
| 6285 | // of form X/C1=C2. We solve for X by multiplying C1 (DivRHS) and |
| 6286 | // C2 (CI). By solving for X we can turn this into a range check |
| 6287 | // instead of computing a divide. |
| 6288 | ConstantInt *Prod = Multiply(CmpRHS, DivRHS); |
| 6289 | |
| 6290 | // Determine if the product overflows by seeing if the product is |
| 6291 | // not equal to the divide. Make sure we do the same kind of divide |
| 6292 | // as in the LHS instruction that we're folding. |
| 6293 | bool ProdOV = (DivIsSigned ? ConstantExpr::getSDiv(Prod, DivRHS) : |
| 6294 | ConstantExpr::getUDiv(Prod, DivRHS)) != CmpRHS; |
| 6295 | |
| 6296 | // Get the ICmp opcode |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 6297 | ICmpInst::Predicate Pred = ICI.getPredicate(); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 6298 | |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 6299 | // Figure out the interval that is being checked. For example, a comparison |
| 6300 | // like "X /u 5 == 0" is really checking that X is in the interval [0, 5). |
| 6301 | // Compute this interval based on the constants involved and the signedness of |
| 6302 | // the compare/divide. This computes a half-open interval, keeping track of |
| 6303 | // whether either value in the interval overflows. After analysis each |
| 6304 | // overflow variable is set to 0 if it's corresponding bound variable is valid |
| 6305 | // -1 if overflowed off the bottom end, or +1 if overflowed off the top end. |
| 6306 | int LoOverflow = 0, HiOverflow = 0; |
| 6307 | ConstantInt *LoBound = 0, *HiBound = 0; |
| 6308 | |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 6309 | if (!DivIsSigned) { // udiv |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 6310 | // e.g. X/5 op 3 --> [15, 20) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 6311 | LoBound = Prod; |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 6312 | HiOverflow = LoOverflow = ProdOV; |
| 6313 | if (!HiOverflow) |
| 6314 | HiOverflow = AddWithOverflow(HiBound, LoBound, DivRHS, false); |
Dan Gohman | 7649127 | 2008-02-13 22:09:18 +0000 | [diff] [blame] | 6315 | } else if (DivRHS->getValue().isStrictlyPositive()) { // Divisor is > 0. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 6316 | if (CmpRHSV == 0) { // (X / pos) op 0 |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 6317 | // Can't overflow. e.g. X/2 op 0 --> [-1, 2) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 6318 | LoBound = cast<ConstantInt>(ConstantExpr::getNeg(SubOne(DivRHS))); |
| 6319 | HiBound = DivRHS; |
Dan Gohman | 7649127 | 2008-02-13 22:09:18 +0000 | [diff] [blame] | 6320 | } else if (CmpRHSV.isStrictlyPositive()) { // (X / pos) op pos |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 6321 | LoBound = Prod; // e.g. X/5 op 3 --> [15, 20) |
| 6322 | HiOverflow = LoOverflow = ProdOV; |
| 6323 | if (!HiOverflow) |
| 6324 | HiOverflow = AddWithOverflow(HiBound, Prod, DivRHS, true); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 6325 | } else { // (X / pos) op neg |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 6326 | // e.g. X/5 op -3 --> [-15-4, -15+1) --> [-19, -14) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 6327 | HiBound = AddOne(Prod); |
Chris Lattner | a6321b4 | 2008-10-11 22:55:00 +0000 | [diff] [blame] | 6328 | LoOverflow = HiOverflow = ProdOV ? -1 : 0; |
| 6329 | if (!LoOverflow) { |
| 6330 | ConstantInt* DivNeg = cast<ConstantInt>(ConstantExpr::getNeg(DivRHS)); |
| 6331 | LoOverflow = AddWithOverflow(LoBound, HiBound, DivNeg, |
| 6332 | true) ? -1 : 0; |
| 6333 | } |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 6334 | } |
Dan Gohman | 7649127 | 2008-02-13 22:09:18 +0000 | [diff] [blame] | 6335 | } else if (DivRHS->getValue().isNegative()) { // Divisor is < 0. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 6336 | if (CmpRHSV == 0) { // (X / neg) op 0 |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 6337 | // e.g. X/-5 op 0 --> [-4, 5) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 6338 | LoBound = AddOne(DivRHS); |
| 6339 | HiBound = cast<ConstantInt>(ConstantExpr::getNeg(DivRHS)); |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 6340 | if (HiBound == DivRHS) { // -INTMIN = INTMIN |
| 6341 | HiOverflow = 1; // [INTMIN+1, overflow) |
| 6342 | HiBound = 0; // e.g. X/INTMIN = 0 --> X > INTMIN |
| 6343 | } |
Dan Gohman | 7649127 | 2008-02-13 22:09:18 +0000 | [diff] [blame] | 6344 | } else if (CmpRHSV.isStrictlyPositive()) { // (X / neg) op pos |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 6345 | // e.g. X/-5 op 3 --> [-19, -14) |
Chris Lattner | a6321b4 | 2008-10-11 22:55:00 +0000 | [diff] [blame] | 6346 | HiBound = AddOne(Prod); |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 6347 | HiOverflow = LoOverflow = ProdOV ? -1 : 0; |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 6348 | if (!LoOverflow) |
Chris Lattner | a6321b4 | 2008-10-11 22:55:00 +0000 | [diff] [blame] | 6349 | LoOverflow = AddWithOverflow(LoBound, HiBound, DivRHS, true) ? -1 : 0; |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 6350 | } else { // (X / neg) op neg |
Chris Lattner | a6321b4 | 2008-10-11 22:55:00 +0000 | [diff] [blame] | 6351 | LoBound = Prod; // e.g. X/-5 op -3 --> [15, 20) |
| 6352 | LoOverflow = HiOverflow = ProdOV; |
Dan Gohman | 7f85fbd | 2008-09-11 00:25:00 +0000 | [diff] [blame] | 6353 | if (!HiOverflow) |
| 6354 | HiOverflow = SubWithOverflow(HiBound, Prod, DivRHS, true); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 6355 | } |
| 6356 | |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 6357 | // Dividing by a negative swaps the condition. LT <-> GT |
| 6358 | Pred = ICmpInst::getSwappedPredicate(Pred); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 6359 | } |
| 6360 | |
| 6361 | Value *X = DivI->getOperand(0); |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 6362 | switch (Pred) { |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 6363 | default: assert(0 && "Unhandled icmp opcode!"); |
| 6364 | case ICmpInst::ICMP_EQ: |
| 6365 | if (LoOverflow && HiOverflow) |
| 6366 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
| 6367 | else if (HiOverflow) |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 6368 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 6369 | ICmpInst::ICMP_UGE, X, LoBound); |
| 6370 | else if (LoOverflow) |
| 6371 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : |
| 6372 | ICmpInst::ICMP_ULT, X, HiBound); |
| 6373 | else |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 6374 | return InsertRangeTest(X, LoBound, HiBound, DivIsSigned, true, ICI); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 6375 | case ICmpInst::ICMP_NE: |
| 6376 | if (LoOverflow && HiOverflow) |
| 6377 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); |
| 6378 | else if (HiOverflow) |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 6379 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 6380 | ICmpInst::ICMP_ULT, X, LoBound); |
| 6381 | else if (LoOverflow) |
| 6382 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : |
| 6383 | ICmpInst::ICMP_UGE, X, HiBound); |
| 6384 | else |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 6385 | return InsertRangeTest(X, LoBound, HiBound, DivIsSigned, false, ICI); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 6386 | case ICmpInst::ICMP_ULT: |
| 6387 | case ICmpInst::ICMP_SLT: |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 6388 | if (LoOverflow == +1) // Low bound is greater than input range. |
| 6389 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); |
| 6390 | if (LoOverflow == -1) // Low bound is less than input range. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 6391 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 6392 | return new ICmpInst(Pred, X, LoBound); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 6393 | case ICmpInst::ICMP_UGT: |
| 6394 | case ICmpInst::ICMP_SGT: |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 6395 | if (HiOverflow == +1) // High bound greater than input range. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 6396 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 6397 | else if (HiOverflow == -1) // High bound less than input range. |
| 6398 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); |
| 6399 | if (Pred == ICmpInst::ICMP_UGT) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 6400 | return new ICmpInst(ICmpInst::ICMP_UGE, X, HiBound); |
| 6401 | else |
| 6402 | return new ICmpInst(ICmpInst::ICMP_SGE, X, HiBound); |
| 6403 | } |
| 6404 | } |
| 6405 | |
| 6406 | |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6407 | /// visitICmpInstWithInstAndIntCst - Handle "icmp (instr, intcst)". |
| 6408 | /// |
| 6409 | Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI, |
| 6410 | Instruction *LHSI, |
| 6411 | ConstantInt *RHS) { |
| 6412 | const APInt &RHSV = RHS->getValue(); |
| 6413 | |
| 6414 | switch (LHSI->getOpcode()) { |
Chris Lattner | a80d668 | 2009-01-09 07:47:06 +0000 | [diff] [blame] | 6415 | case Instruction::Trunc: |
| 6416 | if (ICI.isEquality() && LHSI->hasOneUse()) { |
| 6417 | // Simplify icmp eq (trunc x to i8), 42 -> icmp eq x, 42|highbits if all |
| 6418 | // of the high bits truncated out of x are known. |
| 6419 | unsigned DstBits = LHSI->getType()->getPrimitiveSizeInBits(), |
| 6420 | SrcBits = LHSI->getOperand(0)->getType()->getPrimitiveSizeInBits(); |
| 6421 | APInt Mask(APInt::getHighBitsSet(SrcBits, SrcBits-DstBits)); |
| 6422 | APInt KnownZero(SrcBits, 0), KnownOne(SrcBits, 0); |
| 6423 | ComputeMaskedBits(LHSI->getOperand(0), Mask, KnownZero, KnownOne); |
| 6424 | |
| 6425 | // If all the high bits are known, we can do this xform. |
| 6426 | if ((KnownZero|KnownOne).countLeadingOnes() >= SrcBits-DstBits) { |
| 6427 | // Pull in the high bits from known-ones set. |
| 6428 | APInt NewRHS(RHS->getValue()); |
| 6429 | NewRHS.zext(SrcBits); |
| 6430 | NewRHS |= KnownOne; |
| 6431 | return new ICmpInst(ICI.getPredicate(), LHSI->getOperand(0), |
| 6432 | ConstantInt::get(NewRHS)); |
| 6433 | } |
| 6434 | } |
| 6435 | break; |
| 6436 | |
Duncan Sands | 0091bf2 | 2007-04-04 06:42:45 +0000 | [diff] [blame] | 6437 | case Instruction::Xor: // (icmp pred (xor X, XorCST), CI) |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6438 | if (ConstantInt *XorCST = dyn_cast<ConstantInt>(LHSI->getOperand(1))) { |
| 6439 | // If this is a comparison that tests the signbit (X < 0) or (x > -1), |
| 6440 | // fold the xor. |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 6441 | if ((ICI.getPredicate() == ICmpInst::ICMP_SLT && RHSV == 0) || |
| 6442 | (ICI.getPredicate() == ICmpInst::ICMP_SGT && RHSV.isAllOnesValue())) { |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6443 | Value *CompareVal = LHSI->getOperand(0); |
| 6444 | |
| 6445 | // If the sign bit of the XorCST is not set, there is no change to |
| 6446 | // the operation, just stop using the Xor. |
| 6447 | if (!XorCST->getValue().isNegative()) { |
| 6448 | ICI.setOperand(0, CompareVal); |
| 6449 | AddToWorkList(LHSI); |
| 6450 | return &ICI; |
| 6451 | } |
| 6452 | |
| 6453 | // Was the old condition true if the operand is positive? |
| 6454 | bool isTrueIfPositive = ICI.getPredicate() == ICmpInst::ICMP_SGT; |
| 6455 | |
| 6456 | // If so, the new one isn't. |
| 6457 | isTrueIfPositive ^= true; |
| 6458 | |
| 6459 | if (isTrueIfPositive) |
| 6460 | return new ICmpInst(ICmpInst::ICMP_SGT, CompareVal, SubOne(RHS)); |
| 6461 | else |
| 6462 | return new ICmpInst(ICmpInst::ICMP_SLT, CompareVal, AddOne(RHS)); |
| 6463 | } |
Nick Lewycky | 4333f49 | 2009-01-31 21:30:05 +0000 | [diff] [blame] | 6464 | |
| 6465 | if (LHSI->hasOneUse()) { |
| 6466 | // (icmp u/s (xor A SignBit), C) -> (icmp s/u A, (xor C SignBit)) |
| 6467 | if (!ICI.isEquality() && XorCST->getValue().isSignBit()) { |
| 6468 | const APInt &SignBit = XorCST->getValue(); |
| 6469 | ICmpInst::Predicate Pred = ICI.isSignedPredicate() |
| 6470 | ? ICI.getUnsignedPredicate() |
| 6471 | : ICI.getSignedPredicate(); |
| 6472 | return new ICmpInst(Pred, LHSI->getOperand(0), |
| 6473 | ConstantInt::get(RHSV ^ SignBit)); |
| 6474 | } |
| 6475 | |
| 6476 | // (icmp u/s (xor A ~SignBit), C) -> (icmp s/u (xor C ~SignBit), A) |
Chris Lattner | f5db1fb | 2009-02-02 07:15:30 +0000 | [diff] [blame] | 6477 | if (!ICI.isEquality() && XorCST->getValue().isMaxSignedValue()) { |
Nick Lewycky | 4333f49 | 2009-01-31 21:30:05 +0000 | [diff] [blame] | 6478 | const APInt &NotSignBit = XorCST->getValue(); |
| 6479 | ICmpInst::Predicate Pred = ICI.isSignedPredicate() |
| 6480 | ? ICI.getUnsignedPredicate() |
| 6481 | : ICI.getSignedPredicate(); |
| 6482 | Pred = ICI.getSwappedPredicate(Pred); |
| 6483 | return new ICmpInst(Pred, LHSI->getOperand(0), |
| 6484 | ConstantInt::get(RHSV ^ NotSignBit)); |
| 6485 | } |
| 6486 | } |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6487 | } |
| 6488 | break; |
| 6489 | case Instruction::And: // (icmp pred (and X, AndCST), RHS) |
| 6490 | if (LHSI->hasOneUse() && isa<ConstantInt>(LHSI->getOperand(1)) && |
| 6491 | LHSI->getOperand(0)->hasOneUse()) { |
| 6492 | ConstantInt *AndCST = cast<ConstantInt>(LHSI->getOperand(1)); |
| 6493 | |
| 6494 | // If the LHS is an AND of a truncating cast, we can widen the |
| 6495 | // and/compare to be the input width without changing the value |
| 6496 | // produced, eliminating a cast. |
| 6497 | if (TruncInst *Cast = dyn_cast<TruncInst>(LHSI->getOperand(0))) { |
| 6498 | // We can do this transformation if either the AND constant does not |
| 6499 | // have its sign bit set or if it is an equality comparison. |
| 6500 | // Extending a relational comparison when we're checking the sign |
| 6501 | // bit would not work. |
| 6502 | if (Cast->hasOneUse() && |
Anton Korobeynikov | 4aefd6b | 2008-02-20 12:07:57 +0000 | [diff] [blame] | 6503 | (ICI.isEquality() || |
| 6504 | (AndCST->getValue().isNonNegative() && RHSV.isNonNegative()))) { |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6505 | uint32_t BitWidth = |
| 6506 | cast<IntegerType>(Cast->getOperand(0)->getType())->getBitWidth(); |
| 6507 | APInt NewCST = AndCST->getValue(); |
| 6508 | NewCST.zext(BitWidth); |
| 6509 | APInt NewCI = RHSV; |
| 6510 | NewCI.zext(BitWidth); |
| 6511 | Instruction *NewAnd = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6512 | BinaryOperator::CreateAnd(Cast->getOperand(0), |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6513 | ConstantInt::get(NewCST),LHSI->getName()); |
| 6514 | InsertNewInstBefore(NewAnd, ICI); |
| 6515 | return new ICmpInst(ICI.getPredicate(), NewAnd, |
| 6516 | ConstantInt::get(NewCI)); |
| 6517 | } |
| 6518 | } |
| 6519 | |
| 6520 | // If this is: (X >> C1) & C2 != C3 (where any shift and any compare |
| 6521 | // could exist), turn it into (X & (C2 << C1)) != (C3 << C1). This |
| 6522 | // happens a LOT in code produced by the C front-end, for bitfield |
| 6523 | // access. |
| 6524 | BinaryOperator *Shift = dyn_cast<BinaryOperator>(LHSI->getOperand(0)); |
| 6525 | if (Shift && !Shift->isShift()) |
| 6526 | Shift = 0; |
| 6527 | |
| 6528 | ConstantInt *ShAmt; |
| 6529 | ShAmt = Shift ? dyn_cast<ConstantInt>(Shift->getOperand(1)) : 0; |
| 6530 | const Type *Ty = Shift ? Shift->getType() : 0; // Type of the shift. |
| 6531 | const Type *AndTy = AndCST->getType(); // Type of the and. |
| 6532 | |
| 6533 | // We can fold this as long as we can't shift unknown bits |
| 6534 | // into the mask. This can only happen with signed shift |
| 6535 | // rights, as they sign-extend. |
| 6536 | if (ShAmt) { |
| 6537 | bool CanFold = Shift->isLogicalShift(); |
| 6538 | if (!CanFold) { |
| 6539 | // To test for the bad case of the signed shr, see if any |
| 6540 | // of the bits shifted in could be tested after the mask. |
| 6541 | uint32_t TyBits = Ty->getPrimitiveSizeInBits(); |
| 6542 | int ShAmtVal = TyBits - ShAmt->getLimitedValue(TyBits); |
| 6543 | |
| 6544 | uint32_t BitWidth = AndTy->getPrimitiveSizeInBits(); |
| 6545 | if ((APInt::getHighBitsSet(BitWidth, BitWidth-ShAmtVal) & |
| 6546 | AndCST->getValue()) == 0) |
| 6547 | CanFold = true; |
| 6548 | } |
| 6549 | |
| 6550 | if (CanFold) { |
| 6551 | Constant *NewCst; |
| 6552 | if (Shift->getOpcode() == Instruction::Shl) |
| 6553 | NewCst = ConstantExpr::getLShr(RHS, ShAmt); |
| 6554 | else |
| 6555 | NewCst = ConstantExpr::getShl(RHS, ShAmt); |
| 6556 | |
| 6557 | // Check to see if we are shifting out any of the bits being |
| 6558 | // compared. |
| 6559 | if (ConstantExpr::get(Shift->getOpcode(), NewCst, ShAmt) != RHS) { |
| 6560 | // If we shifted bits out, the fold is not going to work out. |
| 6561 | // As a special case, check to see if this means that the |
| 6562 | // result is always true or false now. |
| 6563 | if (ICI.getPredicate() == ICmpInst::ICMP_EQ) |
| 6564 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
| 6565 | if (ICI.getPredicate() == ICmpInst::ICMP_NE) |
| 6566 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); |
| 6567 | } else { |
| 6568 | ICI.setOperand(1, NewCst); |
| 6569 | Constant *NewAndCST; |
| 6570 | if (Shift->getOpcode() == Instruction::Shl) |
| 6571 | NewAndCST = ConstantExpr::getLShr(AndCST, ShAmt); |
| 6572 | else |
| 6573 | NewAndCST = ConstantExpr::getShl(AndCST, ShAmt); |
| 6574 | LHSI->setOperand(1, NewAndCST); |
| 6575 | LHSI->setOperand(0, Shift->getOperand(0)); |
| 6576 | AddToWorkList(Shift); // Shift is dead. |
| 6577 | AddUsesToWorkList(ICI); |
| 6578 | return &ICI; |
| 6579 | } |
| 6580 | } |
| 6581 | } |
| 6582 | |
| 6583 | // Turn ((X >> Y) & C) == 0 into (X & (C << Y)) == 0. The later is |
| 6584 | // preferable because it allows the C<<Y expression to be hoisted out |
| 6585 | // of a loop if Y is invariant and X is not. |
| 6586 | if (Shift && Shift->hasOneUse() && RHSV == 0 && |
Chris Lattner | e8e4921 | 2009-03-25 00:28:58 +0000 | [diff] [blame] | 6587 | ICI.isEquality() && !Shift->isArithmeticShift() && |
| 6588 | !isa<Constant>(Shift->getOperand(0))) { |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6589 | // Compute C << Y. |
| 6590 | Value *NS; |
| 6591 | if (Shift->getOpcode() == Instruction::LShr) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6592 | NS = BinaryOperator::CreateShl(AndCST, |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6593 | Shift->getOperand(1), "tmp"); |
| 6594 | } else { |
| 6595 | // Insert a logical shift. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6596 | NS = BinaryOperator::CreateLShr(AndCST, |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6597 | Shift->getOperand(1), "tmp"); |
| 6598 | } |
| 6599 | InsertNewInstBefore(cast<Instruction>(NS), ICI); |
| 6600 | |
| 6601 | // Compute X & (C << Y). |
| 6602 | Instruction *NewAnd = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6603 | BinaryOperator::CreateAnd(Shift->getOperand(0), NS, LHSI->getName()); |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6604 | InsertNewInstBefore(NewAnd, ICI); |
| 6605 | |
| 6606 | ICI.setOperand(0, NewAnd); |
| 6607 | return &ICI; |
| 6608 | } |
| 6609 | } |
| 6610 | break; |
| 6611 | |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6612 | case Instruction::Shl: { // (icmp pred (shl X, ShAmt), CI) |
| 6613 | ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1)); |
| 6614 | if (!ShAmt) break; |
| 6615 | |
| 6616 | uint32_t TypeBits = RHSV.getBitWidth(); |
| 6617 | |
| 6618 | // Check that the shift amount is in range. If not, don't perform |
| 6619 | // undefined shifts. When the shift is visited it will be |
| 6620 | // simplified. |
| 6621 | if (ShAmt->uge(TypeBits)) |
| 6622 | break; |
| 6623 | |
| 6624 | if (ICI.isEquality()) { |
| 6625 | // If we are comparing against bits always shifted out, the |
| 6626 | // comparison cannot succeed. |
| 6627 | Constant *Comp = |
| 6628 | ConstantExpr::getShl(ConstantExpr::getLShr(RHS, ShAmt), ShAmt); |
| 6629 | if (Comp != RHS) {// Comparing against a bit that we know is zero. |
| 6630 | bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE; |
| 6631 | Constant *Cst = ConstantInt::get(Type::Int1Ty, IsICMP_NE); |
| 6632 | return ReplaceInstUsesWith(ICI, Cst); |
| 6633 | } |
| 6634 | |
| 6635 | if (LHSI->hasOneUse()) { |
| 6636 | // Otherwise strength reduce the shift into an and. |
| 6637 | uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits); |
| 6638 | Constant *Mask = |
| 6639 | ConstantInt::get(APInt::getLowBitsSet(TypeBits, TypeBits-ShAmtVal)); |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6640 | |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6641 | Instruction *AndI = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6642 | BinaryOperator::CreateAnd(LHSI->getOperand(0), |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6643 | Mask, LHSI->getName()+".mask"); |
| 6644 | Value *And = InsertNewInstBefore(AndI, ICI); |
| 6645 | return new ICmpInst(ICI.getPredicate(), And, |
| 6646 | ConstantInt::get(RHSV.lshr(ShAmtVal))); |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6647 | } |
| 6648 | } |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6649 | |
| 6650 | // Otherwise, if this is a comparison of the sign bit, simplify to and/test. |
| 6651 | bool TrueIfSigned = false; |
| 6652 | if (LHSI->hasOneUse() && |
| 6653 | isSignBitCheck(ICI.getPredicate(), RHS, TrueIfSigned)) { |
| 6654 | // (X << 31) <s 0 --> (X&1) != 0 |
| 6655 | Constant *Mask = ConstantInt::get(APInt(TypeBits, 1) << |
| 6656 | (TypeBits-ShAmt->getZExtValue()-1)); |
| 6657 | Instruction *AndI = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6658 | BinaryOperator::CreateAnd(LHSI->getOperand(0), |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6659 | Mask, LHSI->getName()+".mask"); |
| 6660 | Value *And = InsertNewInstBefore(AndI, ICI); |
| 6661 | |
| 6662 | return new ICmpInst(TrueIfSigned ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ, |
| 6663 | And, Constant::getNullValue(And->getType())); |
| 6664 | } |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6665 | break; |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6666 | } |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6667 | |
| 6668 | case Instruction::LShr: // (icmp pred (shr X, ShAmt), CI) |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6669 | case Instruction::AShr: { |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 6670 | // Only handle equality comparisons of shift-by-constant. |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6671 | ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1)); |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 6672 | if (!ShAmt || !ICI.isEquality()) break; |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6673 | |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 6674 | // Check that the shift amount is in range. If not, don't perform |
| 6675 | // undefined shifts. When the shift is visited it will be |
| 6676 | // simplified. |
| 6677 | uint32_t TypeBits = RHSV.getBitWidth(); |
| 6678 | if (ShAmt->uge(TypeBits)) |
| 6679 | break; |
| 6680 | |
| 6681 | uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits); |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6682 | |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 6683 | // If we are comparing against bits always shifted out, the |
| 6684 | // comparison cannot succeed. |
| 6685 | APInt Comp = RHSV << ShAmtVal; |
| 6686 | if (LHSI->getOpcode() == Instruction::LShr) |
| 6687 | Comp = Comp.lshr(ShAmtVal); |
| 6688 | else |
| 6689 | Comp = Comp.ashr(ShAmtVal); |
| 6690 | |
| 6691 | if (Comp != RHSV) { // Comparing against a bit that we know is zero. |
| 6692 | bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE; |
| 6693 | Constant *Cst = ConstantInt::get(Type::Int1Ty, IsICMP_NE); |
| 6694 | return ReplaceInstUsesWith(ICI, Cst); |
| 6695 | } |
| 6696 | |
| 6697 | // Otherwise, check to see if the bits shifted out are known to be zero. |
| 6698 | // If so, we can compare against the unshifted value: |
| 6699 | // (X & 4) >> 1 == 2 --> (X & 4) == 4. |
Evan Cheng | f30752c | 2008-04-23 00:38:06 +0000 | [diff] [blame] | 6700 | if (LHSI->hasOneUse() && |
| 6701 | MaskedValueIsZero(LHSI->getOperand(0), |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 6702 | APInt::getLowBitsSet(Comp.getBitWidth(), ShAmtVal))) { |
| 6703 | return new ICmpInst(ICI.getPredicate(), LHSI->getOperand(0), |
| 6704 | ConstantExpr::getShl(RHS, ShAmt)); |
| 6705 | } |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6706 | |
Evan Cheng | f30752c | 2008-04-23 00:38:06 +0000 | [diff] [blame] | 6707 | if (LHSI->hasOneUse()) { |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 6708 | // Otherwise strength reduce the shift into an and. |
| 6709 | APInt Val(APInt::getHighBitsSet(TypeBits, TypeBits - ShAmtVal)); |
| 6710 | Constant *Mask = ConstantInt::get(Val); |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6711 | |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 6712 | Instruction *AndI = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6713 | BinaryOperator::CreateAnd(LHSI->getOperand(0), |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 6714 | Mask, LHSI->getName()+".mask"); |
| 6715 | Value *And = InsertNewInstBefore(AndI, ICI); |
| 6716 | return new ICmpInst(ICI.getPredicate(), And, |
| 6717 | ConstantExpr::getShl(RHS, ShAmt)); |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6718 | } |
| 6719 | break; |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6720 | } |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6721 | |
| 6722 | case Instruction::SDiv: |
| 6723 | case Instruction::UDiv: |
| 6724 | // Fold: icmp pred ([us]div X, C1), C2 -> range test |
| 6725 | // Fold this div into the comparison, producing a range check. |
| 6726 | // Determine, based on the divide type, what the range is being |
| 6727 | // checked. If there is an overflow on the low or high side, remember |
| 6728 | // it, otherwise compute the range [low, hi) bounding the new value. |
| 6729 | // See: InsertRangeTest above for the kinds of replacements possible. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 6730 | if (ConstantInt *DivRHS = dyn_cast<ConstantInt>(LHSI->getOperand(1))) |
| 6731 | if (Instruction *R = FoldICmpDivCst(ICI, cast<BinaryOperator>(LHSI), |
| 6732 | DivRHS)) |
| 6733 | return R; |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6734 | break; |
Nick Lewycky | 5be2920 | 2008-02-03 16:33:09 +0000 | [diff] [blame] | 6735 | |
| 6736 | case Instruction::Add: |
| 6737 | // Fold: icmp pred (add, X, C1), C2 |
| 6738 | |
| 6739 | if (!ICI.isEquality()) { |
| 6740 | ConstantInt *LHSC = dyn_cast<ConstantInt>(LHSI->getOperand(1)); |
| 6741 | if (!LHSC) break; |
| 6742 | const APInt &LHSV = LHSC->getValue(); |
| 6743 | |
| 6744 | ConstantRange CR = ICI.makeConstantRange(ICI.getPredicate(), RHSV) |
| 6745 | .subtract(LHSV); |
| 6746 | |
| 6747 | if (ICI.isSignedPredicate()) { |
| 6748 | if (CR.getLower().isSignBit()) { |
| 6749 | return new ICmpInst(ICmpInst::ICMP_SLT, LHSI->getOperand(0), |
| 6750 | ConstantInt::get(CR.getUpper())); |
| 6751 | } else if (CR.getUpper().isSignBit()) { |
| 6752 | return new ICmpInst(ICmpInst::ICMP_SGE, LHSI->getOperand(0), |
| 6753 | ConstantInt::get(CR.getLower())); |
| 6754 | } |
| 6755 | } else { |
| 6756 | if (CR.getLower().isMinValue()) { |
| 6757 | return new ICmpInst(ICmpInst::ICMP_ULT, LHSI->getOperand(0), |
| 6758 | ConstantInt::get(CR.getUpper())); |
| 6759 | } else if (CR.getUpper().isMinValue()) { |
| 6760 | return new ICmpInst(ICmpInst::ICMP_UGE, LHSI->getOperand(0), |
| 6761 | ConstantInt::get(CR.getLower())); |
| 6762 | } |
| 6763 | } |
| 6764 | } |
| 6765 | break; |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6766 | } |
| 6767 | |
| 6768 | // Simplify icmp_eq and icmp_ne instructions with integer constant RHS. |
| 6769 | if (ICI.isEquality()) { |
| 6770 | bool isICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE; |
| 6771 | |
| 6772 | // If the first operand is (add|sub|and|or|xor|rem) with a constant, and |
| 6773 | // the second operand is a constant, simplify a bit. |
| 6774 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(LHSI)) { |
| 6775 | switch (BO->getOpcode()) { |
| 6776 | case Instruction::SRem: |
| 6777 | // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one. |
| 6778 | if (RHSV == 0 && isa<ConstantInt>(BO->getOperand(1)) &&BO->hasOneUse()){ |
| 6779 | const APInt &V = cast<ConstantInt>(BO->getOperand(1))->getValue(); |
| 6780 | if (V.sgt(APInt(V.getBitWidth(), 1)) && V.isPowerOf2()) { |
| 6781 | Instruction *NewRem = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6782 | BinaryOperator::CreateURem(BO->getOperand(0), BO->getOperand(1), |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6783 | BO->getName()); |
| 6784 | InsertNewInstBefore(NewRem, ICI); |
| 6785 | return new ICmpInst(ICI.getPredicate(), NewRem, |
| 6786 | Constant::getNullValue(BO->getType())); |
| 6787 | } |
| 6788 | } |
| 6789 | break; |
| 6790 | case Instruction::Add: |
| 6791 | // Replace ((add A, B) != C) with (A != C-B) if B & C are constants. |
| 6792 | if (ConstantInt *BOp1C = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
| 6793 | if (BO->hasOneUse()) |
| 6794 | return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), |
| 6795 | Subtract(RHS, BOp1C)); |
| 6796 | } else if (RHSV == 0) { |
| 6797 | // Replace ((add A, B) != 0) with (A != -B) if A or B is |
| 6798 | // efficiently invertible, or if the add has just this one use. |
| 6799 | Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1); |
| 6800 | |
| 6801 | if (Value *NegVal = dyn_castNegVal(BOp1)) |
| 6802 | return new ICmpInst(ICI.getPredicate(), BOp0, NegVal); |
| 6803 | else if (Value *NegVal = dyn_castNegVal(BOp0)) |
| 6804 | return new ICmpInst(ICI.getPredicate(), NegVal, BOp1); |
| 6805 | else if (BO->hasOneUse()) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6806 | Instruction *Neg = BinaryOperator::CreateNeg(BOp1); |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6807 | InsertNewInstBefore(Neg, ICI); |
| 6808 | Neg->takeName(BO); |
| 6809 | return new ICmpInst(ICI.getPredicate(), BOp0, Neg); |
| 6810 | } |
| 6811 | } |
| 6812 | break; |
| 6813 | case Instruction::Xor: |
| 6814 | // For the xor case, we can xor two constants together, eliminating |
| 6815 | // the explicit xor. |
| 6816 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) |
| 6817 | return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), |
| 6818 | ConstantExpr::getXor(RHS, BOC)); |
| 6819 | |
| 6820 | // FALLTHROUGH |
| 6821 | case Instruction::Sub: |
| 6822 | // Replace (([sub|xor] A, B) != 0) with (A != B) |
| 6823 | if (RHSV == 0) |
| 6824 | return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), |
| 6825 | BO->getOperand(1)); |
| 6826 | break; |
| 6827 | |
| 6828 | case Instruction::Or: |
| 6829 | // If bits are being or'd in that are not present in the constant we |
| 6830 | // are comparing against, then the comparison could never succeed! |
| 6831 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) { |
| 6832 | Constant *NotCI = ConstantExpr::getNot(RHS); |
| 6833 | if (!ConstantExpr::getAnd(BOC, NotCI)->isNullValue()) |
| 6834 | return ReplaceInstUsesWith(ICI, ConstantInt::get(Type::Int1Ty, |
| 6835 | isICMP_NE)); |
| 6836 | } |
| 6837 | break; |
| 6838 | |
| 6839 | case Instruction::And: |
| 6840 | if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
| 6841 | // If bits are being compared against that are and'd out, then the |
| 6842 | // comparison can never succeed! |
| 6843 | if ((RHSV & ~BOC->getValue()) != 0) |
| 6844 | return ReplaceInstUsesWith(ICI, ConstantInt::get(Type::Int1Ty, |
| 6845 | isICMP_NE)); |
| 6846 | |
| 6847 | // If we have ((X & C) == C), turn it into ((X & C) != 0). |
| 6848 | if (RHS == BOC && RHSV.isPowerOf2()) |
| 6849 | return new ICmpInst(isICMP_NE ? ICmpInst::ICMP_EQ : |
| 6850 | ICmpInst::ICMP_NE, LHSI, |
| 6851 | Constant::getNullValue(RHS->getType())); |
| 6852 | |
| 6853 | // Replace (and X, (1 << size(X)-1) != 0) with x s< 0 |
Chris Lattner | 833f25d | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 6854 | if (BOC->getValue().isSignBit()) { |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6855 | Value *X = BO->getOperand(0); |
| 6856 | Constant *Zero = Constant::getNullValue(X->getType()); |
| 6857 | ICmpInst::Predicate pred = isICMP_NE ? |
| 6858 | ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGE; |
| 6859 | return new ICmpInst(pred, X, Zero); |
| 6860 | } |
| 6861 | |
| 6862 | // ((X & ~7) == 0) --> X < 8 |
| 6863 | if (RHSV == 0 && isHighOnes(BOC)) { |
| 6864 | Value *X = BO->getOperand(0); |
| 6865 | Constant *NegX = ConstantExpr::getNeg(BOC); |
| 6866 | ICmpInst::Predicate pred = isICMP_NE ? |
| 6867 | ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT; |
| 6868 | return new ICmpInst(pred, X, NegX); |
| 6869 | } |
| 6870 | } |
| 6871 | default: break; |
| 6872 | } |
| 6873 | } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(LHSI)) { |
| 6874 | // Handle icmp {eq|ne} <intrinsic>, intcst. |
| 6875 | if (II->getIntrinsicID() == Intrinsic::bswap) { |
| 6876 | AddToWorkList(II); |
| 6877 | ICI.setOperand(0, II->getOperand(1)); |
| 6878 | ICI.setOperand(1, ConstantInt::get(RHSV.byteSwap())); |
| 6879 | return &ICI; |
| 6880 | } |
| 6881 | } |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6882 | } |
| 6883 | return 0; |
| 6884 | } |
| 6885 | |
| 6886 | /// visitICmpInstWithCastAndCast - Handle icmp (cast x to y), (cast/cst). |
| 6887 | /// We only handle extending casts so far. |
| 6888 | /// |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6889 | Instruction *InstCombiner::visitICmpInstWithCastAndCast(ICmpInst &ICI) { |
| 6890 | const CastInst *LHSCI = cast<CastInst>(ICI.getOperand(0)); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6891 | Value *LHSCIOp = LHSCI->getOperand(0); |
| 6892 | const Type *SrcTy = LHSCIOp->getType(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6893 | const Type *DestTy = LHSCI->getType(); |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 6894 | Value *RHSCIOp; |
| 6895 | |
Chris Lattner | 8c756c1 | 2007-05-05 22:41:33 +0000 | [diff] [blame] | 6896 | // Turn icmp (ptrtoint x), (ptrtoint/c) into a compare of the input if the |
| 6897 | // integer type is the same size as the pointer type. |
| 6898 | if (LHSCI->getOpcode() == Instruction::PtrToInt && |
| 6899 | getTargetData().getPointerSizeInBits() == |
| 6900 | cast<IntegerType>(DestTy)->getBitWidth()) { |
| 6901 | Value *RHSOp = 0; |
| 6902 | if (Constant *RHSC = dyn_cast<Constant>(ICI.getOperand(1))) { |
Chris Lattner | 6f6f512 | 2007-05-06 07:24:03 +0000 | [diff] [blame] | 6903 | RHSOp = ConstantExpr::getIntToPtr(RHSC, SrcTy); |
Chris Lattner | 8c756c1 | 2007-05-05 22:41:33 +0000 | [diff] [blame] | 6904 | } else if (PtrToIntInst *RHSC = dyn_cast<PtrToIntInst>(ICI.getOperand(1))) { |
| 6905 | RHSOp = RHSC->getOperand(0); |
| 6906 | // If the pointer types don't match, insert a bitcast. |
| 6907 | if (LHSCIOp->getType() != RHSOp->getType()) |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 6908 | RHSOp = InsertBitCastBefore(RHSOp, LHSCIOp->getType(), ICI); |
Chris Lattner | 8c756c1 | 2007-05-05 22:41:33 +0000 | [diff] [blame] | 6909 | } |
| 6910 | |
| 6911 | if (RHSOp) |
| 6912 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSOp); |
| 6913 | } |
| 6914 | |
| 6915 | // The code below only handles extension cast instructions, so far. |
| 6916 | // Enforce this. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6917 | if (LHSCI->getOpcode() != Instruction::ZExt && |
| 6918 | LHSCI->getOpcode() != Instruction::SExt) |
Chris Lattner | b352fa5 | 2005-01-17 03:20:02 +0000 | [diff] [blame] | 6919 | return 0; |
| 6920 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6921 | bool isSignedExt = LHSCI->getOpcode() == Instruction::SExt; |
| 6922 | bool isSignedCmp = ICI.isSignedPredicate(); |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 6923 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6924 | if (CastInst *CI = dyn_cast<CastInst>(ICI.getOperand(1))) { |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 6925 | // Not an extension from the same type? |
| 6926 | RHSCIOp = CI->getOperand(0); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6927 | if (RHSCIOp->getType() != LHSCIOp->getType()) |
| 6928 | return 0; |
Chris Lattner | a5c5e77 | 2007-01-13 23:11:38 +0000 | [diff] [blame] | 6929 | |
Nick Lewycky | 4189a53 | 2008-01-28 03:48:02 +0000 | [diff] [blame] | 6930 | // 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] | 6931 | // and the other is a zext), then we can't handle this. |
| 6932 | if (CI->getOpcode() != LHSCI->getOpcode()) |
| 6933 | return 0; |
| 6934 | |
Nick Lewycky | 4189a53 | 2008-01-28 03:48:02 +0000 | [diff] [blame] | 6935 | // Deal with equality cases early. |
| 6936 | if (ICI.isEquality()) |
| 6937 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSCIOp); |
| 6938 | |
| 6939 | // A signed comparison of sign extended values simplifies into a |
| 6940 | // signed comparison. |
| 6941 | if (isSignedCmp && isSignedExt) |
| 6942 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSCIOp); |
| 6943 | |
| 6944 | // The other three cases all fold into an unsigned comparison. |
| 6945 | return new ICmpInst(ICI.getUnsignedPredicate(), LHSCIOp, RHSCIOp); |
Reid Spencer | 6731d5c | 2004-11-28 21:31:15 +0000 | [diff] [blame] | 6946 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 6947 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6948 | // If we aren't dealing with a constant on the RHS, exit early |
| 6949 | ConstantInt *CI = dyn_cast<ConstantInt>(ICI.getOperand(1)); |
| 6950 | if (!CI) |
| 6951 | return 0; |
| 6952 | |
| 6953 | // Compute the constant that would happen if we truncated to SrcTy then |
| 6954 | // reextended to DestTy. |
| 6955 | Constant *Res1 = ConstantExpr::getTrunc(CI, SrcTy); |
| 6956 | Constant *Res2 = ConstantExpr::getCast(LHSCI->getOpcode(), Res1, DestTy); |
| 6957 | |
| 6958 | // If the re-extended constant didn't change... |
| 6959 | if (Res2 == CI) { |
| 6960 | // Make sure that sign of the Cmp and the sign of the Cast are the same. |
| 6961 | // For example, we might have: |
| 6962 | // %A = sext short %X to uint |
| 6963 | // %B = icmp ugt uint %A, 1330 |
| 6964 | // It is incorrect to transform this into |
| 6965 | // %B = icmp ugt short %X, 1330 |
| 6966 | // because %A may have negative value. |
| 6967 | // |
Chris Lattner | f299184 | 2008-07-11 04:09:09 +0000 | [diff] [blame] | 6968 | // However, we allow this when the compare is EQ/NE, because they are |
| 6969 | // signless. |
| 6970 | if (isSignedExt == isSignedCmp || ICI.isEquality()) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6971 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, Res1); |
Chris Lattner | f299184 | 2008-07-11 04:09:09 +0000 | [diff] [blame] | 6972 | return 0; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6973 | } |
| 6974 | |
| 6975 | // The re-extended constant changed so the constant cannot be represented |
| 6976 | // in the shorter type. Consequently, we cannot emit a simple comparison. |
| 6977 | |
| 6978 | // First, handle some easy cases. We know the result cannot be equal at this |
| 6979 | // point so handle the ICI.isEquality() cases |
| 6980 | if (ICI.getPredicate() == ICmpInst::ICMP_EQ) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6981 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6982 | if (ICI.getPredicate() == ICmpInst::ICMP_NE) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6983 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6984 | |
| 6985 | // Evaluate the comparison for LT (we invert for GT below). LE and GE cases |
| 6986 | // should have been folded away previously and not enter in here. |
| 6987 | Value *Result; |
| 6988 | if (isSignedCmp) { |
| 6989 | // We're performing a signed comparison. |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 6990 | if (cast<ConstantInt>(CI)->getValue().isNegative()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6991 | Result = ConstantInt::getFalse(); // X < (small) --> false |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6992 | else |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6993 | Result = ConstantInt::getTrue(); // X < (large) --> true |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6994 | } else { |
| 6995 | // We're performing an unsigned comparison. |
| 6996 | if (isSignedExt) { |
| 6997 | // We're performing an unsigned comp with a sign extended value. |
| 6998 | // This is true if the input is >= 0. [aka >s -1] |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6999 | Constant *NegOne = ConstantInt::getAllOnesValue(SrcTy); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7000 | Result = InsertNewInstBefore(new ICmpInst(ICmpInst::ICMP_SGT, LHSCIOp, |
| 7001 | NegOne, ICI.getName()), ICI); |
| 7002 | } else { |
| 7003 | // Unsigned extend & unsigned compare -> always true. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 7004 | Result = ConstantInt::getTrue(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7005 | } |
| 7006 | } |
| 7007 | |
| 7008 | // Finally, return the value computed. |
| 7009 | if (ICI.getPredicate() == ICmpInst::ICMP_ULT || |
Chris Lattner | f299184 | 2008-07-11 04:09:09 +0000 | [diff] [blame] | 7010 | ICI.getPredicate() == ICmpInst::ICMP_SLT) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7011 | return ReplaceInstUsesWith(ICI, Result); |
Chris Lattner | f299184 | 2008-07-11 04:09:09 +0000 | [diff] [blame] | 7012 | |
| 7013 | assert((ICI.getPredicate()==ICmpInst::ICMP_UGT || |
| 7014 | ICI.getPredicate()==ICmpInst::ICMP_SGT) && |
| 7015 | "ICmp should be folded!"); |
| 7016 | if (Constant *CI = dyn_cast<Constant>(Result)) |
| 7017 | return ReplaceInstUsesWith(ICI, ConstantExpr::getNot(CI)); |
| 7018 | return BinaryOperator::CreateNot(Result); |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 7019 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 7020 | |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7021 | Instruction *InstCombiner::visitShl(BinaryOperator &I) { |
| 7022 | return commonShiftTransforms(I); |
| 7023 | } |
| 7024 | |
| 7025 | Instruction *InstCombiner::visitLShr(BinaryOperator &I) { |
| 7026 | return commonShiftTransforms(I); |
| 7027 | } |
| 7028 | |
| 7029 | Instruction *InstCombiner::visitAShr(BinaryOperator &I) { |
Chris Lattner | 348f665 | 2007-12-06 01:59:46 +0000 | [diff] [blame] | 7030 | if (Instruction *R = commonShiftTransforms(I)) |
| 7031 | return R; |
| 7032 | |
| 7033 | Value *Op0 = I.getOperand(0); |
| 7034 | |
| 7035 | // ashr int -1, X = -1 (for any arithmetic shift rights of ~0) |
| 7036 | if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0)) |
| 7037 | if (CSI->isAllOnesValue()) |
| 7038 | return ReplaceInstUsesWith(I, CSI); |
| 7039 | |
| 7040 | // See if we can turn a signed shr into an unsigned shr. |
Chris Lattner | b44b366 | 2009-03-18 16:32:19 +0000 | [diff] [blame] | 7041 | if (!isa<VectorType>(I.getType())) { |
| 7042 | if (MaskedValueIsZero(Op0, |
Chris Lattner | 348f665 | 2007-12-06 01:59:46 +0000 | [diff] [blame] | 7043 | APInt::getSignBit(I.getType()->getPrimitiveSizeInBits()))) |
Chris Lattner | b44b366 | 2009-03-18 16:32:19 +0000 | [diff] [blame] | 7044 | return BinaryOperator::CreateLShr(Op0, I.getOperand(1)); |
Dan Gohman | 0001e56 | 2009-02-24 02:00:40 +0000 | [diff] [blame] | 7045 | |
Chris Lattner | b44b366 | 2009-03-18 16:32:19 +0000 | [diff] [blame] | 7046 | // Arithmetic shifting an all-sign-bit value is a no-op. |
| 7047 | unsigned NumSignBits = ComputeNumSignBits(Op0); |
| 7048 | if (NumSignBits == Op0->getType()->getPrimitiveSizeInBits()) |
| 7049 | return ReplaceInstUsesWith(I, Op0); |
| 7050 | } |
Dan Gohman | 0001e56 | 2009-02-24 02:00:40 +0000 | [diff] [blame] | 7051 | |
Chris Lattner | 348f665 | 2007-12-06 01:59:46 +0000 | [diff] [blame] | 7052 | return 0; |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7053 | } |
| 7054 | |
| 7055 | Instruction *InstCombiner::commonShiftTransforms(BinaryOperator &I) { |
| 7056 | assert(I.getOperand(1)->getType() == I.getOperand(0)->getType()); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 7057 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 7058 | |
| 7059 | // shl X, 0 == X and shr X, 0 == X |
| 7060 | // shl 0, X == 0 and shr 0, X == 0 |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7061 | if (Op1 == Constant::getNullValue(Op1->getType()) || |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 7062 | Op0 == Constant::getNullValue(Op0->getType())) |
| 7063 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 8d6bbdb | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 7064 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7065 | if (isa<UndefValue>(Op0)) { |
| 7066 | if (I.getOpcode() == Instruction::AShr) // undef >>s X -> undef |
Chris Lattner | 79a564c | 2004-10-16 23:28:04 +0000 | [diff] [blame] | 7067 | return ReplaceInstUsesWith(I, Op0); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7068 | else // undef << X -> 0, undef >>u X -> 0 |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 7069 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 7070 | } |
| 7071 | if (isa<UndefValue>(Op1)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7072 | if (I.getOpcode() == Instruction::AShr) // X >>s undef -> X |
| 7073 | return ReplaceInstUsesWith(I, Op0); |
| 7074 | else // X << undef, X >>u undef -> 0 |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 7075 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 7076 | } |
| 7077 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 7078 | // Try to fold constant and into select arguments. |
| 7079 | if (isa<Constant>(Op0)) |
| 7080 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 7081 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 7082 | return R; |
| 7083 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7084 | if (ConstantInt *CUI = dyn_cast<ConstantInt>(Op1)) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 7085 | if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I)) |
| 7086 | return Res; |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 7087 | return 0; |
| 7088 | } |
| 7089 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7090 | Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7091 | BinaryOperator &I) { |
Chris Lattner | 4598c94 | 2009-01-31 08:24:16 +0000 | [diff] [blame] | 7092 | bool isLeftShift = I.getOpcode() == Instruction::Shl; |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 7093 | |
Chris Lattner | 8d6bbdb | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 7094 | // See if we can simplify any instructions used by the instruction whose sole |
| 7095 | // purpose is to compute bits we don't care about. |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 7096 | uint32_t TypeBits = Op0->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 7097 | if (SimplifyDemandedInstructionBits(I)) |
Chris Lattner | 8d6bbdb | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 7098 | return &I; |
| 7099 | |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 7100 | // shl uint X, 32 = 0 and shr ubyte Y, 9 = 0, ... just don't eliminate shr |
| 7101 | // of a signed value. |
| 7102 | // |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 7103 | if (Op1->uge(TypeBits)) { |
Chris Lattner | 0737c24 | 2007-02-02 05:29:55 +0000 | [diff] [blame] | 7104 | if (I.getOpcode() != Instruction::AShr) |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 7105 | return ReplaceInstUsesWith(I, Constant::getNullValue(Op0->getType())); |
| 7106 | else { |
Chris Lattner | 0737c24 | 2007-02-02 05:29:55 +0000 | [diff] [blame] | 7107 | I.setOperand(1, ConstantInt::get(I.getType(), TypeBits-1)); |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 7108 | return &I; |
Chris Lattner | 8adac75 | 2004-02-23 20:30:06 +0000 | [diff] [blame] | 7109 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 7110 | } |
| 7111 | |
| 7112 | // ((X*C1) << C2) == (X * (C1 << C2)) |
| 7113 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0)) |
| 7114 | if (BO->getOpcode() == Instruction::Mul && isLeftShift) |
| 7115 | if (Constant *BOOp = dyn_cast<Constant>(BO->getOperand(1))) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7116 | return BinaryOperator::CreateMul(BO->getOperand(0), |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 7117 | ConstantExpr::getShl(BOOp, Op1)); |
| 7118 | |
| 7119 | // Try to fold constant and into select arguments. |
| 7120 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 7121 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 7122 | return R; |
| 7123 | if (isa<PHINode>(Op0)) |
| 7124 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 7125 | return NV; |
| 7126 | |
Chris Lattner | 8999dd3 | 2007-12-22 09:07:47 +0000 | [diff] [blame] | 7127 | // Fold shift2(trunc(shift1(x,c1)), c2) -> trunc(shift2(shift1(x,c1),c2)) |
| 7128 | if (TruncInst *TI = dyn_cast<TruncInst>(Op0)) { |
| 7129 | Instruction *TrOp = dyn_cast<Instruction>(TI->getOperand(0)); |
| 7130 | // If 'shift2' is an ashr, we would have to get the sign bit into a funny |
| 7131 | // place. Don't try to do this transformation in this case. Also, we |
| 7132 | // require that the input operand is a shift-by-constant so that we have |
| 7133 | // confidence that the shifts will get folded together. We could do this |
| 7134 | // xform in more cases, but it is unlikely to be profitable. |
| 7135 | if (TrOp && I.isLogicalShift() && TrOp->isShift() && |
| 7136 | isa<ConstantInt>(TrOp->getOperand(1))) { |
| 7137 | // Okay, we'll do this xform. Make the shift of shift. |
| 7138 | Constant *ShAmt = ConstantExpr::getZExt(Op1, TrOp->getType()); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7139 | Instruction *NSh = BinaryOperator::Create(I.getOpcode(), TrOp, ShAmt, |
Chris Lattner | 8999dd3 | 2007-12-22 09:07:47 +0000 | [diff] [blame] | 7140 | I.getName()); |
| 7141 | InsertNewInstBefore(NSh, I); // (shift2 (shift1 & 0x00FF), c2) |
| 7142 | |
| 7143 | // For logical shifts, the truncation has the effect of making the high |
| 7144 | // part of the register be zeros. Emulate this by inserting an AND to |
| 7145 | // clear the top bits as needed. This 'and' will usually be zapped by |
| 7146 | // other xforms later if dead. |
| 7147 | unsigned SrcSize = TrOp->getType()->getPrimitiveSizeInBits(); |
| 7148 | unsigned DstSize = TI->getType()->getPrimitiveSizeInBits(); |
| 7149 | APInt MaskV(APInt::getLowBitsSet(SrcSize, DstSize)); |
| 7150 | |
| 7151 | // The mask we constructed says what the trunc would do if occurring |
| 7152 | // between the shifts. We want to know the effect *after* the second |
| 7153 | // shift. We know that it is a logical shift by a constant, so adjust the |
| 7154 | // mask as appropriate. |
| 7155 | if (I.getOpcode() == Instruction::Shl) |
| 7156 | MaskV <<= Op1->getZExtValue(); |
| 7157 | else { |
| 7158 | assert(I.getOpcode() == Instruction::LShr && "Unknown logical shift"); |
| 7159 | MaskV = MaskV.lshr(Op1->getZExtValue()); |
| 7160 | } |
| 7161 | |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7162 | Instruction *And = BinaryOperator::CreateAnd(NSh, ConstantInt::get(MaskV), |
Chris Lattner | 8999dd3 | 2007-12-22 09:07:47 +0000 | [diff] [blame] | 7163 | TI->getName()); |
| 7164 | InsertNewInstBefore(And, I); // shift1 & 0x00FF |
| 7165 | |
| 7166 | // Return the value truncated to the interesting size. |
| 7167 | return new TruncInst(And, I.getType()); |
| 7168 | } |
| 7169 | } |
| 7170 | |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 7171 | if (Op0->hasOneUse()) { |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 7172 | if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0)) { |
| 7173 | // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C) |
| 7174 | Value *V1, *V2; |
| 7175 | ConstantInt *CC; |
| 7176 | switch (Op0BO->getOpcode()) { |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 7177 | default: break; |
| 7178 | case Instruction::Add: |
| 7179 | case Instruction::And: |
| 7180 | case Instruction::Or: |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 7181 | case Instruction::Xor: { |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 7182 | // These operators commute. |
| 7183 | // Turn (Y + (X >> C)) << C -> (X + (Y << C)) & (~0 << C) |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 7184 | if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() && |
Chris Lattner | cb504b9 | 2008-11-16 05:38:51 +0000 | [diff] [blame] | 7185 | match(Op0BO->getOperand(1), m_Shr(m_Value(V1), m_Specific(Op1)))){ |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7186 | Instruction *YS = BinaryOperator::CreateShl( |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 7187 | Op0BO->getOperand(0), Op1, |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 7188 | Op0BO->getName()); |
| 7189 | InsertNewInstBefore(YS, I); // (Y << C) |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 7190 | Instruction *X = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7191 | BinaryOperator::Create(Op0BO->getOpcode(), YS, V1, |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 7192 | Op0BO->getOperand(1)->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 7193 | InsertNewInstBefore(X, I); // (X + (Y << C)) |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 7194 | uint32_t Op1Val = Op1->getLimitedValue(TypeBits); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7195 | return BinaryOperator::CreateAnd(X, ConstantInt::get( |
Zhou Sheng | 90b9681 | 2007-03-30 05:45:18 +0000 | [diff] [blame] | 7196 | APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val))); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 7197 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 7198 | |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 7199 | // Turn (Y + ((X >> C) & CC)) << C -> ((X & (CC << C)) + (Y << C)) |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 7200 | Value *Op0BOOp1 = Op0BO->getOperand(1); |
Chris Lattner | 3c69849 | 2007-03-05 00:11:19 +0000 | [diff] [blame] | 7201 | if (isLeftShift && Op0BOOp1->hasOneUse() && |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 7202 | match(Op0BOOp1, |
Chris Lattner | cb504b9 | 2008-11-16 05:38:51 +0000 | [diff] [blame] | 7203 | m_And(m_Shr(m_Value(V1), m_Specific(Op1)), |
| 7204 | m_ConstantInt(CC))) && |
| 7205 | cast<BinaryOperator>(Op0BOOp1)->getOperand(0)->hasOneUse()) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7206 | Instruction *YS = BinaryOperator::CreateShl( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7207 | Op0BO->getOperand(0), Op1, |
| 7208 | Op0BO->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 7209 | InsertNewInstBefore(YS, I); // (Y << C) |
| 7210 | Instruction *XM = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7211 | BinaryOperator::CreateAnd(V1, ConstantExpr::getShl(CC, Op1), |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 7212 | V1->getName()+".mask"); |
| 7213 | InsertNewInstBefore(XM, I); // X & (CC << C) |
| 7214 | |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7215 | return BinaryOperator::Create(Op0BO->getOpcode(), YS, XM); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 7216 | } |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 7217 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 7218 | |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 7219 | // FALL THROUGH. |
| 7220 | case Instruction::Sub: { |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 7221 | // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C) |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 7222 | if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() && |
Chris Lattner | cb504b9 | 2008-11-16 05:38:51 +0000 | [diff] [blame] | 7223 | match(Op0BO->getOperand(0), m_Shr(m_Value(V1), m_Specific(Op1)))){ |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7224 | Instruction *YS = BinaryOperator::CreateShl( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7225 | Op0BO->getOperand(1), Op1, |
| 7226 | Op0BO->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 7227 | InsertNewInstBefore(YS, I); // (Y << C) |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 7228 | Instruction *X = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7229 | BinaryOperator::Create(Op0BO->getOpcode(), V1, YS, |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 7230 | Op0BO->getOperand(0)->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 7231 | InsertNewInstBefore(X, I); // (X + (Y << C)) |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 7232 | uint32_t Op1Val = Op1->getLimitedValue(TypeBits); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7233 | return BinaryOperator::CreateAnd(X, ConstantInt::get( |
Zhou Sheng | 90b9681 | 2007-03-30 05:45:18 +0000 | [diff] [blame] | 7234 | APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val))); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 7235 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 7236 | |
Chris Lattner | 13d4ab4 | 2006-05-31 21:14:00 +0000 | [diff] [blame] | 7237 | // Turn (((X >> C)&CC) + Y) << C -> (X + (Y << C)) & (CC << C) |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 7238 | if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() && |
| 7239 | match(Op0BO->getOperand(0), |
| 7240 | m_And(m_Shr(m_Value(V1), m_Value(V2)), |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 7241 | m_ConstantInt(CC))) && V2 == Op1 && |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 7242 | cast<BinaryOperator>(Op0BO->getOperand(0)) |
| 7243 | ->getOperand(0)->hasOneUse()) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7244 | Instruction *YS = BinaryOperator::CreateShl( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7245 | Op0BO->getOperand(1), Op1, |
| 7246 | Op0BO->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 7247 | InsertNewInstBefore(YS, I); // (Y << C) |
| 7248 | Instruction *XM = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7249 | BinaryOperator::CreateAnd(V1, ConstantExpr::getShl(CC, Op1), |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 7250 | V1->getName()+".mask"); |
| 7251 | InsertNewInstBefore(XM, I); // X & (CC << C) |
| 7252 | |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7253 | return BinaryOperator::Create(Op0BO->getOpcode(), XM, YS); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 7254 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 7255 | |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 7256 | break; |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 7257 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 7258 | } |
| 7259 | |
| 7260 | |
| 7261 | // If the operand is an bitwise operator with a constant RHS, and the |
| 7262 | // shift is the only use, we can pull it out of the shift. |
| 7263 | if (ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand(1))) { |
| 7264 | bool isValid = true; // Valid only for And, Or, Xor |
| 7265 | bool highBitSet = false; // Transform if high bit of constant set? |
| 7266 | |
| 7267 | switch (Op0BO->getOpcode()) { |
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 7268 | default: isValid = false; break; // Do not perform transform! |
Chris Lattner | 1f7e160 | 2004-10-08 03:46:20 +0000 | [diff] [blame] | 7269 | case Instruction::Add: |
| 7270 | isValid = isLeftShift; |
| 7271 | break; |
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 7272 | case Instruction::Or: |
| 7273 | case Instruction::Xor: |
| 7274 | highBitSet = false; |
| 7275 | break; |
| 7276 | case Instruction::And: |
| 7277 | highBitSet = true; |
| 7278 | break; |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 7279 | } |
| 7280 | |
| 7281 | // If this is a signed shift right, and the high bit is modified |
| 7282 | // by the logical operation, do not perform the transformation. |
| 7283 | // The highBitSet boolean indicates the value of the high bit of |
| 7284 | // the constant which would cause it to be modified for this |
| 7285 | // operation. |
| 7286 | // |
Chris Lattner | c95ba44 | 2007-12-06 06:25:04 +0000 | [diff] [blame] | 7287 | if (isValid && I.getOpcode() == Instruction::AShr) |
Zhou Sheng | e9e03f6 | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 7288 | isValid = Op0C->getValue()[TypeBits-1] == highBitSet; |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 7289 | |
| 7290 | if (isValid) { |
| 7291 | Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, Op1); |
| 7292 | |
| 7293 | Instruction *NewShift = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7294 | BinaryOperator::Create(I.getOpcode(), Op0BO->getOperand(0), Op1); |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 7295 | InsertNewInstBefore(NewShift, I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 7296 | NewShift->takeName(Op0BO); |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 7297 | |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7298 | return BinaryOperator::Create(Op0BO->getOpcode(), NewShift, |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 7299 | NewRHS); |
| 7300 | } |
| 7301 | } |
| 7302 | } |
| 7303 | } |
| 7304 | |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 7305 | // 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] | 7306 | BinaryOperator *ShiftOp = dyn_cast<BinaryOperator>(Op0); |
| 7307 | if (ShiftOp && !ShiftOp->isShift()) |
| 7308 | ShiftOp = 0; |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 7309 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7310 | if (ShiftOp && isa<ConstantInt>(ShiftOp->getOperand(1))) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7311 | ConstantInt *ShiftAmt1C = cast<ConstantInt>(ShiftOp->getOperand(1)); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 7312 | uint32_t ShiftAmt1 = ShiftAmt1C->getLimitedValue(TypeBits); |
| 7313 | uint32_t ShiftAmt2 = Op1->getLimitedValue(TypeBits); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 7314 | assert(ShiftAmt2 != 0 && "Should have been simplified earlier"); |
| 7315 | if (ShiftAmt1 == 0) return 0; // Will be simplified in the future. |
| 7316 | Value *X = ShiftOp->getOperand(0); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 7317 | |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 7318 | uint32_t AmtSum = ShiftAmt1+ShiftAmt2; // Fold into one big shift. |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 7319 | |
| 7320 | const IntegerType *Ty = cast<IntegerType>(I.getType()); |
| 7321 | |
| 7322 | // Check for (X << c1) << c2 and (X >> c1) >> c2 |
Chris Lattner | 7f3da2d | 2007-02-03 23:28:07 +0000 | [diff] [blame] | 7323 | if (I.getOpcode() == ShiftOp->getOpcode()) { |
Chris Lattner | 344c7c5 | 2009-03-20 22:41:15 +0000 | [diff] [blame] | 7324 | // If this is oversized composite shift, then unsigned shifts get 0, ashr |
| 7325 | // saturates. |
| 7326 | if (AmtSum >= TypeBits) { |
| 7327 | if (I.getOpcode() != Instruction::AShr) |
| 7328 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 7329 | AmtSum = TypeBits-1; // Saturate to 31 for i32 ashr. |
| 7330 | } |
| 7331 | |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7332 | return BinaryOperator::Create(I.getOpcode(), X, |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 7333 | ConstantInt::get(Ty, AmtSum)); |
| 7334 | } else if (ShiftOp->getOpcode() == Instruction::LShr && |
| 7335 | I.getOpcode() == Instruction::AShr) { |
Chris Lattner | 344c7c5 | 2009-03-20 22:41:15 +0000 | [diff] [blame] | 7336 | if (AmtSum >= TypeBits) |
| 7337 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 7338 | |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 7339 | // ((X >>u C1) >>s C2) -> (X >>u (C1+C2)) since C1 != 0. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7340 | return BinaryOperator::CreateLShr(X, ConstantInt::get(Ty, AmtSum)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 7341 | } else if (ShiftOp->getOpcode() == Instruction::AShr && |
| 7342 | I.getOpcode() == Instruction::LShr) { |
| 7343 | // ((X >>s C1) >>u C2) -> ((X >>s (C1+C2)) & mask) since C1 != 0. |
Chris Lattner | 344c7c5 | 2009-03-20 22:41:15 +0000 | [diff] [blame] | 7344 | if (AmtSum >= TypeBits) |
| 7345 | AmtSum = TypeBits-1; |
| 7346 | |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 7347 | Instruction *Shift = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7348 | BinaryOperator::CreateAShr(X, ConstantInt::get(Ty, AmtSum)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 7349 | InsertNewInstBefore(Shift, I); |
| 7350 | |
Zhou Sheng | e9e03f6 | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 7351 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7352 | return BinaryOperator::CreateAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 7353 | } |
| 7354 | |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 7355 | // Okay, if we get here, one shift must be left, and the other shift must be |
| 7356 | // right. See if the amounts are equal. |
| 7357 | if (ShiftAmt1 == ShiftAmt2) { |
| 7358 | // If we have ((X >>? C) << C), turn this into X & (-1 << C). |
| 7359 | if (I.getOpcode() == Instruction::Shl) { |
Reid Spencer | 55702aa | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 7360 | APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt1)); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7361 | return BinaryOperator::CreateAnd(X, ConstantInt::get(Mask)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 7362 | } |
| 7363 | // If we have ((X << C) >>u C), turn this into X & (-1 >>u C). |
| 7364 | if (I.getOpcode() == Instruction::LShr) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 7365 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt1)); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7366 | return BinaryOperator::CreateAnd(X, ConstantInt::get(Mask)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 7367 | } |
| 7368 | // We can simplify ((X << C) >>s C) into a trunc + sext. |
| 7369 | // NOTE: we could do this for any C, but that would make 'unusual' integer |
| 7370 | // types. For now, just stick to ones well-supported by the code |
| 7371 | // generators. |
| 7372 | const Type *SExtType = 0; |
| 7373 | switch (Ty->getBitWidth() - ShiftAmt1) { |
Zhou Sheng | e9e03f6 | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 7374 | case 1 : |
| 7375 | case 8 : |
| 7376 | case 16 : |
| 7377 | case 32 : |
| 7378 | case 64 : |
| 7379 | case 128: |
| 7380 | SExtType = IntegerType::get(Ty->getBitWidth() - ShiftAmt1); |
| 7381 | break; |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 7382 | default: break; |
| 7383 | } |
| 7384 | if (SExtType) { |
| 7385 | Instruction *NewTrunc = new TruncInst(X, SExtType, "sext"); |
| 7386 | InsertNewInstBefore(NewTrunc, I); |
| 7387 | return new SExtInst(NewTrunc, Ty); |
| 7388 | } |
| 7389 | // Otherwise, we can't handle it yet. |
| 7390 | } else if (ShiftAmt1 < ShiftAmt2) { |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 7391 | uint32_t ShiftDiff = ShiftAmt2-ShiftAmt1; |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 7392 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 7393 | // (X >>? C1) << C2 --> X << (C2-C1) & (-1 << C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 7394 | if (I.getOpcode() == Instruction::Shl) { |
| 7395 | assert(ShiftOp->getOpcode() == Instruction::LShr || |
| 7396 | ShiftOp->getOpcode() == Instruction::AShr); |
Chris Lattner | e8d56c5 | 2006-01-07 01:32:28 +0000 | [diff] [blame] | 7397 | Instruction *Shift = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7398 | BinaryOperator::CreateShl(X, ConstantInt::get(Ty, ShiftDiff)); |
Chris Lattner | e8d56c5 | 2006-01-07 01:32:28 +0000 | [diff] [blame] | 7399 | InsertNewInstBefore(Shift, I); |
| 7400 | |
Reid Spencer | 55702aa | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 7401 | APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7402 | return BinaryOperator::CreateAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 7403 | } |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 7404 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 7405 | // (X << C1) >>u C2 --> X >>u (C2-C1) & (-1 >> C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 7406 | if (I.getOpcode() == Instruction::LShr) { |
| 7407 | assert(ShiftOp->getOpcode() == Instruction::Shl); |
| 7408 | Instruction *Shift = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7409 | BinaryOperator::CreateLShr(X, ConstantInt::get(Ty, ShiftDiff)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 7410 | InsertNewInstBefore(Shift, I); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 7411 | |
Reid Spencer | d5e30f0 | 2007-03-26 17:18:58 +0000 | [diff] [blame] | 7412 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7413 | return BinaryOperator::CreateAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 7414 | } |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 7415 | |
| 7416 | // We can't handle (X << C1) >>s C2, it shifts arbitrary bits in. |
| 7417 | } else { |
| 7418 | assert(ShiftAmt2 < ShiftAmt1); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 7419 | uint32_t ShiftDiff = ShiftAmt1-ShiftAmt2; |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 7420 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 7421 | // (X >>? C1) << C2 --> X >>? (C1-C2) & (-1 << C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 7422 | if (I.getOpcode() == Instruction::Shl) { |
| 7423 | assert(ShiftOp->getOpcode() == Instruction::LShr || |
| 7424 | ShiftOp->getOpcode() == Instruction::AShr); |
| 7425 | Instruction *Shift = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7426 | BinaryOperator::Create(ShiftOp->getOpcode(), X, |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 7427 | ConstantInt::get(Ty, ShiftDiff)); |
| 7428 | InsertNewInstBefore(Shift, I); |
| 7429 | |
Reid Spencer | 55702aa | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 7430 | APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7431 | return BinaryOperator::CreateAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 7432 | } |
| 7433 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 7434 | // (X << C1) >>u C2 --> X << (C1-C2) & (-1 >> C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 7435 | if (I.getOpcode() == Instruction::LShr) { |
| 7436 | assert(ShiftOp->getOpcode() == Instruction::Shl); |
| 7437 | Instruction *Shift = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7438 | BinaryOperator::CreateShl(X, ConstantInt::get(Ty, ShiftDiff)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 7439 | InsertNewInstBefore(Shift, I); |
| 7440 | |
Reid Spencer | 68d27cf | 2007-03-26 23:45:51 +0000 | [diff] [blame] | 7441 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7442 | return BinaryOperator::CreateAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 7443 | } |
| 7444 | |
| 7445 | // 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] | 7446 | } |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 7447 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 7448 | return 0; |
| 7449 | } |
| 7450 | |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 7451 | |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 7452 | /// DecomposeSimpleLinearExpr - Analyze 'Val', seeing if it is a simple linear |
| 7453 | /// expression. If so, decompose it, returning some value X, such that Val is |
| 7454 | /// X*Scale+Offset. |
| 7455 | /// |
| 7456 | static Value *DecomposeSimpleLinearExpr(Value *Val, unsigned &Scale, |
Jeff Cohen | 86796be | 2007-04-04 16:58:57 +0000 | [diff] [blame] | 7457 | int &Offset) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 7458 | assert(Val->getType() == Type::Int32Ty && "Unexpected allocation size type!"); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7459 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 7460 | Offset = CI->getZExtValue(); |
Chris Lattner | 6a94de2 | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 7461 | Scale = 0; |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 7462 | return ConstantInt::get(Type::Int32Ty, 0); |
Chris Lattner | 6a94de2 | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 7463 | } else if (BinaryOperator *I = dyn_cast<BinaryOperator>(Val)) { |
| 7464 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 7465 | if (I->getOpcode() == Instruction::Shl) { |
| 7466 | // This is a value scaled by '1 << the shift amt'. |
| 7467 | Scale = 1U << RHS->getZExtValue(); |
| 7468 | Offset = 0; |
| 7469 | return I->getOperand(0); |
| 7470 | } else if (I->getOpcode() == Instruction::Mul) { |
| 7471 | // This value is scaled by 'RHS'. |
| 7472 | Scale = RHS->getZExtValue(); |
| 7473 | Offset = 0; |
| 7474 | return I->getOperand(0); |
| 7475 | } else if (I->getOpcode() == Instruction::Add) { |
| 7476 | // We have X+C. Check to see if we really have (X*C2)+C1, |
| 7477 | // where C1 is divisible by C2. |
| 7478 | unsigned SubScale; |
| 7479 | Value *SubVal = |
| 7480 | DecomposeSimpleLinearExpr(I->getOperand(0), SubScale, Offset); |
| 7481 | Offset += RHS->getZExtValue(); |
| 7482 | Scale = SubScale; |
| 7483 | return SubVal; |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 7484 | } |
| 7485 | } |
| 7486 | } |
| 7487 | |
| 7488 | // Otherwise, we can't look past this. |
| 7489 | Scale = 1; |
| 7490 | Offset = 0; |
| 7491 | return Val; |
| 7492 | } |
| 7493 | |
| 7494 | |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 7495 | /// PromoteCastOfAllocation - If we find a cast of an allocation instruction, |
| 7496 | /// 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] | 7497 | Instruction *InstCombiner::PromoteCastOfAllocation(BitCastInst &CI, |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 7498 | AllocationInst &AI) { |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7499 | const PointerType *PTy = cast<PointerType>(CI.getType()); |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 7500 | |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 7501 | // Remove any uses of AI that are dead. |
| 7502 | assert(!CI.use_empty() && "Dead instructions should be removed earlier!"); |
Chris Lattner | 535014f | 2007-02-15 22:52:10 +0000 | [diff] [blame] | 7503 | |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 7504 | for (Value::use_iterator UI = AI.use_begin(), E = AI.use_end(); UI != E; ) { |
| 7505 | Instruction *User = cast<Instruction>(*UI++); |
| 7506 | if (isInstructionTriviallyDead(User)) { |
| 7507 | while (UI != E && *UI == User) |
| 7508 | ++UI; // If this instruction uses AI more than once, don't break UI. |
| 7509 | |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 7510 | ++NumDeadInst; |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 7511 | DOUT << "IC: DCE: " << *User; |
Chris Lattner | f22a5c6 | 2007-03-02 19:59:19 +0000 | [diff] [blame] | 7512 | EraseInstFromFunction(*User); |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 7513 | } |
| 7514 | } |
| 7515 | |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 7516 | // Get the type really allocated and the type casted to. |
| 7517 | const Type *AllocElTy = AI.getAllocatedType(); |
| 7518 | const Type *CastElTy = PTy->getElementType(); |
| 7519 | if (!AllocElTy->isSized() || !CastElTy->isSized()) return 0; |
Chris Lattner | 18e78bb | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 7520 | |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 7521 | unsigned AllocElTyAlign = TD->getABITypeAlignment(AllocElTy); |
| 7522 | unsigned CastElTyAlign = TD->getABITypeAlignment(CastElTy); |
Chris Lattner | 18e78bb | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 7523 | if (CastElTyAlign < AllocElTyAlign) return 0; |
| 7524 | |
Chris Lattner | 39387a5 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 7525 | // If the allocation has multiple uses, only promote it if we are strictly |
| 7526 | // increasing the alignment of the resultant allocation. If we keep it the |
Dale Johannesen | a0a6637 | 2009-03-05 00:39:02 +0000 | [diff] [blame] | 7527 | // same, we open the door to infinite loops of various kinds. (A reference |
| 7528 | // from a dbg.declare doesn't count as a use for this purpose.) |
| 7529 | if (!AI.hasOneUse() && !hasOneUsePlusDeclare(&AI) && |
| 7530 | CastElTyAlign == AllocElTyAlign) return 0; |
Chris Lattner | 39387a5 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 7531 | |
Duncan Sands | ceb4d1a | 2009-01-12 20:38:59 +0000 | [diff] [blame] | 7532 | uint64_t AllocElTySize = TD->getTypePaddedSize(AllocElTy); |
| 7533 | uint64_t CastElTySize = TD->getTypePaddedSize(CastElTy); |
Chris Lattner | 0ddac2a | 2005-10-27 05:53:56 +0000 | [diff] [blame] | 7534 | if (CastElTySize == 0 || AllocElTySize == 0) return 0; |
Chris Lattner | 18e78bb | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 7535 | |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 7536 | // See if we can satisfy the modulus by pulling a scale out of the array |
| 7537 | // size argument. |
Jeff Cohen | 86796be | 2007-04-04 16:58:57 +0000 | [diff] [blame] | 7538 | unsigned ArraySizeScale; |
| 7539 | int ArrayOffset; |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 7540 | Value *NumElements = // See if the array size is a decomposable linear expr. |
| 7541 | DecomposeSimpleLinearExpr(AI.getOperand(0), ArraySizeScale, ArrayOffset); |
| 7542 | |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 7543 | // If we can now satisfy the modulus, by using a non-1 scale, we really can |
| 7544 | // do the xform. |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 7545 | if ((AllocElTySize*ArraySizeScale) % CastElTySize != 0 || |
| 7546 | (AllocElTySize*ArrayOffset ) % CastElTySize != 0) return 0; |
Chris Lattner | 8142b0a | 2005-10-27 06:12:00 +0000 | [diff] [blame] | 7547 | |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 7548 | unsigned Scale = (AllocElTySize*ArraySizeScale)/CastElTySize; |
| 7549 | Value *Amt = 0; |
| 7550 | if (Scale == 1) { |
| 7551 | Amt = NumElements; |
| 7552 | } else { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7553 | // If the allocation size is constant, form a constant mul expression |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 7554 | Amt = ConstantInt::get(Type::Int32Ty, Scale); |
| 7555 | if (isa<ConstantInt>(NumElements)) |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 7556 | Amt = Multiply(cast<ConstantInt>(NumElements), cast<ConstantInt>(Amt)); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7557 | // otherwise multiply the amount and the number of elements |
Chris Lattner | 46d232d | 2009-03-17 17:55:15 +0000 | [diff] [blame] | 7558 | else { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7559 | Instruction *Tmp = BinaryOperator::CreateMul(Amt, NumElements, "tmp"); |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 7560 | Amt = InsertNewInstBefore(Tmp, AI); |
Chris Lattner | 8142b0a | 2005-10-27 06:12:00 +0000 | [diff] [blame] | 7561 | } |
Chris Lattner | 0ddac2a | 2005-10-27 05:53:56 +0000 | [diff] [blame] | 7562 | } |
| 7563 | |
Jeff Cohen | 86796be | 2007-04-04 16:58:57 +0000 | [diff] [blame] | 7564 | if (int Offset = (AllocElTySize*ArrayOffset)/CastElTySize) { |
| 7565 | Value *Off = ConstantInt::get(Type::Int32Ty, Offset, true); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7566 | Instruction *Tmp = BinaryOperator::CreateAdd(Amt, Off, "tmp"); |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 7567 | Amt = InsertNewInstBefore(Tmp, AI); |
| 7568 | } |
| 7569 | |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 7570 | AllocationInst *New; |
| 7571 | if (isa<MallocInst>(AI)) |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 7572 | New = new MallocInst(CastElTy, Amt, AI.getAlignment()); |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 7573 | else |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 7574 | New = new AllocaInst(CastElTy, Amt, AI.getAlignment()); |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 7575 | InsertNewInstBefore(New, AI); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 7576 | New->takeName(&AI); |
Chris Lattner | 39387a5 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 7577 | |
Dale Johannesen | a0a6637 | 2009-03-05 00:39:02 +0000 | [diff] [blame] | 7578 | // If the allocation has one real use plus a dbg.declare, just remove the |
| 7579 | // declare. |
| 7580 | if (DbgDeclareInst *DI = hasOneUsePlusDeclare(&AI)) { |
| 7581 | EraseInstFromFunction(*DI); |
| 7582 | } |
| 7583 | // If the allocation has multiple real uses, insert a cast and change all |
| 7584 | // things that used it to use the new cast. This will also hack on CI, but it |
| 7585 | // will die soon. |
| 7586 | else if (!AI.hasOneUse()) { |
Chris Lattner | 39387a5 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 7587 | AddUsesToWorkList(AI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7588 | // New is the allocation instruction, pointer typed. AI is the original |
| 7589 | // allocation instruction, also pointer typed. Thus, cast to use is BitCast. |
| 7590 | CastInst *NewCast = new BitCastInst(New, AI.getType(), "tmpcast"); |
Chris Lattner | 39387a5 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 7591 | InsertNewInstBefore(NewCast, AI); |
| 7592 | AI.replaceAllUsesWith(NewCast); |
| 7593 | } |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 7594 | return ReplaceInstUsesWith(CI, New); |
| 7595 | } |
| 7596 | |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7597 | /// CanEvaluateInDifferentType - Return true if we can take the specified value |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7598 | /// and return it as type Ty without inserting any new casts and without |
| 7599 | /// changing the computed value. This is used by code that tries to decide |
| 7600 | /// whether promoting or shrinking integer operations to wider or smaller types |
| 7601 | /// will allow us to eliminate a truncate or extend. |
| 7602 | /// |
| 7603 | /// This is a truncation operation if Ty is smaller than V->getType(), or an |
| 7604 | /// extension operation if Ty is larger. |
Chris Lattner | 8114b71 | 2008-06-18 04:00:49 +0000 | [diff] [blame] | 7605 | /// |
| 7606 | /// If CastOpc is a truncation, then Ty will be a type smaller than V. We |
| 7607 | /// should return true if trunc(V) can be computed by computing V in the smaller |
| 7608 | /// type. If V is an instruction, then trunc(inst(x,y)) can be computed as |
| 7609 | /// inst(trunc(x),trunc(y)), which only makes sense if x and y can be |
| 7610 | /// efficiently truncated. |
| 7611 | /// |
| 7612 | /// If CastOpc is a sext or zext, we are asking if the low bits of the value can |
| 7613 | /// bit computed in a larger type, which is then and'd or sext_in_reg'd to get |
| 7614 | /// the final result. |
Evan Cheng | 4e56ab2 | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 7615 | bool InstCombiner::CanEvaluateInDifferentType(Value *V, const IntegerType *Ty, |
| 7616 | unsigned CastOpc, |
| 7617 | int &NumCastsRemoved){ |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7618 | // We can always evaluate constants in another type. |
| 7619 | if (isa<ConstantInt>(V)) |
| 7620 | return true; |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7621 | |
| 7622 | Instruction *I = dyn_cast<Instruction>(V); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7623 | if (!I) return false; |
| 7624 | |
| 7625 | const IntegerType *OrigTy = cast<IntegerType>(V->getType()); |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7626 | |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7627 | // If this is an extension or truncate, we can often eliminate it. |
| 7628 | if (isa<TruncInst>(I) || isa<ZExtInst>(I) || isa<SExtInst>(I)) { |
| 7629 | // If this is a cast from the destination type, we can trivially eliminate |
| 7630 | // it, and this will remove a cast overall. |
| 7631 | if (I->getOperand(0)->getType() == Ty) { |
| 7632 | // If the first operand is itself a cast, and is eliminable, do not count |
| 7633 | // this as an eliminable cast. We would prefer to eliminate those two |
| 7634 | // casts first. |
Chris Lattner | 8114b71 | 2008-06-18 04:00:49 +0000 | [diff] [blame] | 7635 | if (!isa<CastInst>(I->getOperand(0)) && I->hasOneUse()) |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7636 | ++NumCastsRemoved; |
| 7637 | return true; |
| 7638 | } |
| 7639 | } |
| 7640 | |
| 7641 | // We can't extend or shrink something that has multiple uses: doing so would |
| 7642 | // require duplicating the instruction in general, which isn't profitable. |
| 7643 | if (!I->hasOneUse()) return false; |
| 7644 | |
Evan Cheng | f35fd54 | 2009-01-15 17:01:23 +0000 | [diff] [blame] | 7645 | unsigned Opc = I->getOpcode(); |
| 7646 | switch (Opc) { |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7647 | case Instruction::Add: |
| 7648 | case Instruction::Sub: |
Nick Lewycky | b8cd6a4 | 2008-07-05 21:19:34 +0000 | [diff] [blame] | 7649 | case Instruction::Mul: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7650 | case Instruction::And: |
| 7651 | case Instruction::Or: |
| 7652 | case Instruction::Xor: |
| 7653 | // These operators can all arbitrarily be extended or truncated. |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7654 | return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc, |
Evan Cheng | 4e56ab2 | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 7655 | NumCastsRemoved) && |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7656 | CanEvaluateInDifferentType(I->getOperand(1), Ty, CastOpc, |
Evan Cheng | 4e56ab2 | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 7657 | NumCastsRemoved); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7658 | |
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 7659 | case Instruction::Shl: |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7660 | // If we are truncating the result of this SHL, and if it's a shift of a |
| 7661 | // constant amount, we can always perform a SHL in a smaller type. |
| 7662 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 7663 | uint32_t BitWidth = Ty->getBitWidth(); |
| 7664 | if (BitWidth < OrigTy->getBitWidth() && |
| 7665 | CI->getLimitedValue(BitWidth) < BitWidth) |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7666 | return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc, |
Evan Cheng | 4e56ab2 | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 7667 | NumCastsRemoved); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7668 | } |
| 7669 | break; |
| 7670 | case Instruction::LShr: |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7671 | // If this is a truncate of a logical shr, we can truncate it to a smaller |
| 7672 | // lshr iff we know that the bits we would otherwise be shifting in are |
| 7673 | // already zeros. |
| 7674 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 7675 | uint32_t OrigBitWidth = OrigTy->getBitWidth(); |
| 7676 | uint32_t BitWidth = Ty->getBitWidth(); |
| 7677 | if (BitWidth < OrigBitWidth && |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7678 | MaskedValueIsZero(I->getOperand(0), |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 7679 | APInt::getHighBitsSet(OrigBitWidth, OrigBitWidth-BitWidth)) && |
| 7680 | CI->getLimitedValue(BitWidth) < BitWidth) { |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7681 | return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc, |
Evan Cheng | 4e56ab2 | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 7682 | NumCastsRemoved); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7683 | } |
| 7684 | } |
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 7685 | break; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7686 | case Instruction::ZExt: |
| 7687 | case Instruction::SExt: |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7688 | case Instruction::Trunc: |
| 7689 | // 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] | 7690 | // can safely replace it. Note that replacing it does not reduce the number |
| 7691 | // of casts in the input. |
Evan Cheng | f35fd54 | 2009-01-15 17:01:23 +0000 | [diff] [blame] | 7692 | if (Opc == CastOpc) |
| 7693 | return true; |
| 7694 | |
| 7695 | // sext (zext ty1), ty2 -> zext ty2 |
Evan Cheng | 661d9c3 | 2009-01-15 17:09:07 +0000 | [diff] [blame] | 7696 | if (CastOpc == Instruction::SExt && Opc == Instruction::ZExt) |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7697 | return true; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7698 | break; |
Nick Lewycky | b8cd6a4 | 2008-07-05 21:19:34 +0000 | [diff] [blame] | 7699 | case Instruction::Select: { |
| 7700 | SelectInst *SI = cast<SelectInst>(I); |
| 7701 | return CanEvaluateInDifferentType(SI->getTrueValue(), Ty, CastOpc, |
Evan Cheng | 4e56ab2 | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 7702 | NumCastsRemoved) && |
Nick Lewycky | b8cd6a4 | 2008-07-05 21:19:34 +0000 | [diff] [blame] | 7703 | CanEvaluateInDifferentType(SI->getFalseValue(), Ty, CastOpc, |
Evan Cheng | 4e56ab2 | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 7704 | NumCastsRemoved); |
Nick Lewycky | b8cd6a4 | 2008-07-05 21:19:34 +0000 | [diff] [blame] | 7705 | } |
Chris Lattner | 8114b71 | 2008-06-18 04:00:49 +0000 | [diff] [blame] | 7706 | case Instruction::PHI: { |
| 7707 | // We can change a phi if we can change all operands. |
| 7708 | PHINode *PN = cast<PHINode>(I); |
| 7709 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 7710 | if (!CanEvaluateInDifferentType(PN->getIncomingValue(i), Ty, CastOpc, |
Evan Cheng | 4e56ab2 | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 7711 | NumCastsRemoved)) |
Chris Lattner | 8114b71 | 2008-06-18 04:00:49 +0000 | [diff] [blame] | 7712 | return false; |
| 7713 | return true; |
| 7714 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7715 | default: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7716 | // TODO: Can handle more cases here. |
| 7717 | break; |
| 7718 | } |
| 7719 | |
| 7720 | return false; |
| 7721 | } |
| 7722 | |
| 7723 | /// EvaluateInDifferentType - Given an expression that |
| 7724 | /// CanEvaluateInDifferentType returns true for, actually insert the code to |
| 7725 | /// evaluate the expression. |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 7726 | Value *InstCombiner::EvaluateInDifferentType(Value *V, const Type *Ty, |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7727 | bool isSigned) { |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7728 | if (Constant *C = dyn_cast<Constant>(V)) |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 7729 | return ConstantExpr::getIntegerCast(C, Ty, isSigned /*Sext or ZExt*/); |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7730 | |
| 7731 | // Otherwise, it must be an instruction. |
| 7732 | Instruction *I = cast<Instruction>(V); |
Chris Lattner | 01859e8 | 2006-05-20 23:14:03 +0000 | [diff] [blame] | 7733 | Instruction *Res = 0; |
Evan Cheng | f35fd54 | 2009-01-15 17:01:23 +0000 | [diff] [blame] | 7734 | unsigned Opc = I->getOpcode(); |
| 7735 | switch (Opc) { |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7736 | case Instruction::Add: |
| 7737 | case Instruction::Sub: |
Nick Lewycky | e6b0c00 | 2008-01-22 05:08:48 +0000 | [diff] [blame] | 7738 | case Instruction::Mul: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7739 | case Instruction::And: |
| 7740 | case Instruction::Or: |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7741 | case Instruction::Xor: |
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 7742 | case Instruction::AShr: |
| 7743 | case Instruction::LShr: |
| 7744 | case Instruction::Shl: { |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 7745 | Value *LHS = EvaluateInDifferentType(I->getOperand(0), Ty, isSigned); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7746 | Value *RHS = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned); |
Evan Cheng | f35fd54 | 2009-01-15 17:01:23 +0000 | [diff] [blame] | 7747 | Res = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS); |
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 7748 | break; |
| 7749 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7750 | case Instruction::Trunc: |
| 7751 | case Instruction::ZExt: |
| 7752 | case Instruction::SExt: |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7753 | // 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] | 7754 | // just return the source. There's no need to insert it because it is not |
| 7755 | // new. |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7756 | if (I->getOperand(0)->getType() == Ty) |
| 7757 | return I->getOperand(0); |
| 7758 | |
Chris Lattner | 8114b71 | 2008-06-18 04:00:49 +0000 | [diff] [blame] | 7759 | // 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] | 7760 | Res = CastInst::Create(cast<CastInst>(I)->getOpcode(), I->getOperand(0), |
Chris Lattner | 8114b71 | 2008-06-18 04:00:49 +0000 | [diff] [blame] | 7761 | Ty); |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7762 | break; |
Nick Lewycky | b8cd6a4 | 2008-07-05 21:19:34 +0000 | [diff] [blame] | 7763 | case Instruction::Select: { |
| 7764 | Value *True = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned); |
| 7765 | Value *False = EvaluateInDifferentType(I->getOperand(2), Ty, isSigned); |
| 7766 | Res = SelectInst::Create(I->getOperand(0), True, False); |
| 7767 | break; |
| 7768 | } |
Chris Lattner | 8114b71 | 2008-06-18 04:00:49 +0000 | [diff] [blame] | 7769 | case Instruction::PHI: { |
| 7770 | PHINode *OPN = cast<PHINode>(I); |
| 7771 | PHINode *NPN = PHINode::Create(Ty); |
| 7772 | for (unsigned i = 0, e = OPN->getNumIncomingValues(); i != e; ++i) { |
| 7773 | Value *V =EvaluateInDifferentType(OPN->getIncomingValue(i), Ty, isSigned); |
| 7774 | NPN->addIncoming(V, OPN->getIncomingBlock(i)); |
| 7775 | } |
| 7776 | Res = NPN; |
| 7777 | break; |
| 7778 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7779 | default: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7780 | // TODO: Can handle more cases here. |
| 7781 | assert(0 && "Unreachable!"); |
| 7782 | break; |
| 7783 | } |
| 7784 | |
Chris Lattner | 8114b71 | 2008-06-18 04:00:49 +0000 | [diff] [blame] | 7785 | Res->takeName(I); |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7786 | return InsertNewInstBefore(Res, *I); |
| 7787 | } |
| 7788 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7789 | /// @brief Implement the transforms common to all CastInst visitors. |
| 7790 | Instruction *InstCombiner::commonCastTransforms(CastInst &CI) { |
Chris Lattner | 79d35b3 | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 7791 | Value *Src = CI.getOperand(0); |
| 7792 | |
Dan Gohman | 23d9d27 | 2007-05-11 21:10:54 +0000 | [diff] [blame] | 7793 | // 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] | 7794 | // eliminate it now. |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 7795 | if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7796 | if (Instruction::CastOps opc = |
| 7797 | isEliminableCastPair(CSrc, CI.getOpcode(), CI.getType(), TD)) { |
| 7798 | // The first cast (CSrc) is eliminable so we need to fix up or replace |
| 7799 | // 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] | 7800 | return CastInst::Create(opc, CSrc->getOperand(0), CI.getType()); |
Chris Lattner | 8fd217c | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 7801 | } |
| 7802 | } |
Chris Lattner | a710ddc | 2004-05-25 04:29:21 +0000 | [diff] [blame] | 7803 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7804 | // 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] | 7805 | if (SelectInst *SI = dyn_cast<SelectInst>(Src)) |
| 7806 | if (Instruction *NV = FoldOpIntoSelect(CI, SI, this)) |
| 7807 | return NV; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7808 | |
| 7809 | // 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] | 7810 | if (isa<PHINode>(Src)) |
| 7811 | if (Instruction *NV = FoldOpIntoPhi(CI)) |
| 7812 | return NV; |
Chris Lattner | 9fb9213 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 7813 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7814 | return 0; |
| 7815 | } |
| 7816 | |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 7817 | /// FindElementAtOffset - Given a type and a constant offset, determine whether |
| 7818 | /// or not there is a sequence of GEP indices into the type that will land us at |
Chris Lattner | 3914f72 | 2009-01-24 01:00:13 +0000 | [diff] [blame] | 7819 | /// the specified offset. If so, fill them into NewIndices and return the |
| 7820 | /// resultant element type, otherwise return null. |
| 7821 | static const Type *FindElementAtOffset(const Type *Ty, int64_t Offset, |
| 7822 | SmallVectorImpl<Value*> &NewIndices, |
| 7823 | const TargetData *TD) { |
| 7824 | if (!Ty->isSized()) return 0; |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 7825 | |
| 7826 | // Start with the index over the outer type. Note that the type size |
| 7827 | // might be zero (even if the offset isn't zero) if the indexed type |
| 7828 | // is something like [0 x {int, int}] |
| 7829 | const Type *IntPtrTy = TD->getIntPtrType(); |
| 7830 | int64_t FirstIdx = 0; |
Duncan Sands | ceb4d1a | 2009-01-12 20:38:59 +0000 | [diff] [blame] | 7831 | if (int64_t TySize = TD->getTypePaddedSize(Ty)) { |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 7832 | FirstIdx = Offset/TySize; |
Chris Lattner | 31a69cb | 2009-01-11 20:41:36 +0000 | [diff] [blame] | 7833 | Offset -= FirstIdx*TySize; |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 7834 | |
Chris Lattner | dbc3bc2 | 2009-01-11 20:15:20 +0000 | [diff] [blame] | 7835 | // Handle hosts where % returns negative instead of values [0..TySize). |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 7836 | if (Offset < 0) { |
| 7837 | --FirstIdx; |
| 7838 | Offset += TySize; |
| 7839 | assert(Offset >= 0); |
| 7840 | } |
| 7841 | assert((uint64_t)Offset < (uint64_t)TySize && "Out of range offset"); |
| 7842 | } |
| 7843 | |
| 7844 | NewIndices.push_back(ConstantInt::get(IntPtrTy, FirstIdx)); |
| 7845 | |
| 7846 | // Index into the types. If we fail, set OrigBase to null. |
| 7847 | while (Offset) { |
Chris Lattner | dbc3bc2 | 2009-01-11 20:15:20 +0000 | [diff] [blame] | 7848 | // Indexing into tail padding between struct/array elements. |
| 7849 | if (uint64_t(Offset*8) >= TD->getTypeSizeInBits(Ty)) |
Chris Lattner | 3914f72 | 2009-01-24 01:00:13 +0000 | [diff] [blame] | 7850 | return 0; |
Chris Lattner | dbc3bc2 | 2009-01-11 20:15:20 +0000 | [diff] [blame] | 7851 | |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 7852 | if (const StructType *STy = dyn_cast<StructType>(Ty)) { |
| 7853 | const StructLayout *SL = TD->getStructLayout(STy); |
Chris Lattner | dbc3bc2 | 2009-01-11 20:15:20 +0000 | [diff] [blame] | 7854 | assert(Offset < (int64_t)SL->getSizeInBytes() && |
| 7855 | "Offset must stay within the indexed type"); |
| 7856 | |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 7857 | unsigned Elt = SL->getElementContainingOffset(Offset); |
| 7858 | NewIndices.push_back(ConstantInt::get(Type::Int32Ty, Elt)); |
| 7859 | |
| 7860 | Offset -= SL->getElementOffset(Elt); |
| 7861 | Ty = STy->getElementType(Elt); |
Chris Lattner | 1c412d9 | 2009-01-11 20:23:52 +0000 | [diff] [blame] | 7862 | } else if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) { |
Duncan Sands | ceb4d1a | 2009-01-12 20:38:59 +0000 | [diff] [blame] | 7863 | uint64_t EltSize = TD->getTypePaddedSize(AT->getElementType()); |
Chris Lattner | dbc3bc2 | 2009-01-11 20:15:20 +0000 | [diff] [blame] | 7864 | assert(EltSize && "Cannot index into a zero-sized array"); |
| 7865 | NewIndices.push_back(ConstantInt::get(IntPtrTy,Offset/EltSize)); |
| 7866 | Offset %= EltSize; |
Chris Lattner | 1c412d9 | 2009-01-11 20:23:52 +0000 | [diff] [blame] | 7867 | Ty = AT->getElementType(); |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 7868 | } else { |
Chris Lattner | dbc3bc2 | 2009-01-11 20:15:20 +0000 | [diff] [blame] | 7869 | // Otherwise, we can't index into the middle of this atomic type, bail. |
Chris Lattner | 3914f72 | 2009-01-24 01:00:13 +0000 | [diff] [blame] | 7870 | return 0; |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 7871 | } |
| 7872 | } |
| 7873 | |
Chris Lattner | 3914f72 | 2009-01-24 01:00:13 +0000 | [diff] [blame] | 7874 | return Ty; |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 7875 | } |
| 7876 | |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7877 | /// @brief Implement the transforms for cast of pointer (bitcast/ptrtoint) |
| 7878 | Instruction *InstCombiner::commonPointerCastTransforms(CastInst &CI) { |
| 7879 | Value *Src = CI.getOperand(0); |
| 7880 | |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7881 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Src)) { |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7882 | // If casting the result of a getelementptr instruction with no offset, turn |
| 7883 | // this into a cast of the original pointer! |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7884 | if (GEP->hasAllZeroIndices()) { |
| 7885 | // Changing the cast operand is usually not a good idea but it is safe |
| 7886 | // here because the pointer operand is being replaced with another |
| 7887 | // pointer operand so the opcode doesn't need to change. |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7888 | AddToWorkList(GEP); |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7889 | CI.setOperand(0, GEP->getOperand(0)); |
| 7890 | return &CI; |
| 7891 | } |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7892 | |
| 7893 | // If the GEP has a single use, and the base pointer is a bitcast, and the |
| 7894 | // GEP computes a constant offset, see if we can convert these three |
| 7895 | // instructions into fewer. This typically happens with unions and other |
| 7896 | // non-type-safe code. |
| 7897 | if (GEP->hasOneUse() && isa<BitCastInst>(GEP->getOperand(0))) { |
| 7898 | if (GEP->hasAllConstantIndices()) { |
| 7899 | // We are guaranteed to get a constant from EmitGEPOffset. |
| 7900 | ConstantInt *OffsetV = cast<ConstantInt>(EmitGEPOffset(GEP, CI, *this)); |
| 7901 | int64_t Offset = OffsetV->getSExtValue(); |
| 7902 | |
| 7903 | // Get the base pointer input of the bitcast, and the type it points to. |
| 7904 | Value *OrigBase = cast<BitCastInst>(GEP->getOperand(0))->getOperand(0); |
| 7905 | const Type *GEPIdxTy = |
| 7906 | cast<PointerType>(OrigBase->getType())->getElementType(); |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 7907 | SmallVector<Value*, 8> NewIndices; |
| 7908 | if (FindElementAtOffset(GEPIdxTy, Offset, NewIndices, TD)) { |
| 7909 | // If we were able to index down into an element, create the GEP |
| 7910 | // and bitcast the result. This eliminates one bitcast, potentially |
| 7911 | // two. |
| 7912 | Instruction *NGEP = GetElementPtrInst::Create(OrigBase, |
| 7913 | NewIndices.begin(), |
| 7914 | NewIndices.end(), ""); |
| 7915 | InsertNewInstBefore(NGEP, CI); |
| 7916 | NGEP->takeName(GEP); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7917 | |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 7918 | if (isa<BitCastInst>(CI)) |
| 7919 | return new BitCastInst(NGEP, CI.getType()); |
| 7920 | assert(isa<PtrToIntInst>(CI)); |
| 7921 | return new PtrToIntInst(NGEP, CI.getType()); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7922 | } |
| 7923 | } |
| 7924 | } |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7925 | } |
| 7926 | |
| 7927 | return commonCastTransforms(CI); |
| 7928 | } |
| 7929 | |
| 7930 | |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7931 | /// Only the TRUNC, ZEXT, SEXT, and BITCAST can both operand and result as |
| 7932 | /// integer types. This function implements the common transforms for all those |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7933 | /// cases. |
| 7934 | /// @brief Implement the transforms common to CastInst with integer operands |
| 7935 | Instruction *InstCombiner::commonIntCastTransforms(CastInst &CI) { |
| 7936 | if (Instruction *Result = commonCastTransforms(CI)) |
| 7937 | return Result; |
| 7938 | |
| 7939 | Value *Src = CI.getOperand(0); |
| 7940 | const Type *SrcTy = Src->getType(); |
| 7941 | const Type *DestTy = CI.getType(); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 7942 | uint32_t SrcBitSize = SrcTy->getPrimitiveSizeInBits(); |
| 7943 | uint32_t DestBitSize = DestTy->getPrimitiveSizeInBits(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7944 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7945 | // See if we can simplify any instructions used by the LHS whose sole |
| 7946 | // purpose is to compute bits we don't care about. |
Chris Lattner | 886ab6c | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 7947 | if (SimplifyDemandedInstructionBits(CI)) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7948 | return &CI; |
| 7949 | |
| 7950 | // If the source isn't an instruction or has more than one use then we |
| 7951 | // can't do anything more. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7952 | Instruction *SrcI = dyn_cast<Instruction>(Src); |
| 7953 | if (!SrcI || !Src->hasOneUse()) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7954 | return 0; |
| 7955 | |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7956 | // Attempt to propagate the cast into the instruction for int->int casts. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7957 | int NumCastsRemoved = 0; |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7958 | if (!isa<BitCastInst>(CI) && |
| 7959 | CanEvaluateInDifferentType(SrcI, cast<IntegerType>(DestTy), |
Evan Cheng | 4e56ab2 | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 7960 | CI.getOpcode(), NumCastsRemoved)) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7961 | // If this cast is a truncate, evaluting in a different type always |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7962 | // eliminates the cast, so it is always a win. If this is a zero-extension, |
| 7963 | // we need to do an AND to maintain the clear top-part of the computation, |
| 7964 | // so we require that the input have eliminated at least one cast. If this |
| 7965 | // 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] | 7966 | // require that two casts have been eliminated. |
Evan Cheng | f35fd54 | 2009-01-15 17:01:23 +0000 | [diff] [blame] | 7967 | bool DoXForm = false; |
| 7968 | bool JustReplace = false; |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7969 | switch (CI.getOpcode()) { |
| 7970 | default: |
| 7971 | // All the others use floating point so we shouldn't actually |
| 7972 | // get here because of the check above. |
| 7973 | assert(0 && "Unknown cast type"); |
| 7974 | case Instruction::Trunc: |
| 7975 | DoXForm = true; |
| 7976 | break; |
Evan Cheng | 4e56ab2 | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 7977 | case Instruction::ZExt: { |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7978 | DoXForm = NumCastsRemoved >= 1; |
Chris Lattner | 39c27ed | 2009-01-31 19:05:27 +0000 | [diff] [blame] | 7979 | if (!DoXForm && 0) { |
Evan Cheng | 4e56ab2 | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 7980 | // If it's unnecessary to issue an AND to clear the high bits, it's |
| 7981 | // always profitable to do this xform. |
Chris Lattner | 39c27ed | 2009-01-31 19:05:27 +0000 | [diff] [blame] | 7982 | Value *TryRes = EvaluateInDifferentType(SrcI, DestTy, false); |
Evan Cheng | 4e56ab2 | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 7983 | APInt Mask(APInt::getBitsSet(DestBitSize, SrcBitSize, DestBitSize)); |
| 7984 | if (MaskedValueIsZero(TryRes, Mask)) |
| 7985 | return ReplaceInstUsesWith(CI, TryRes); |
Chris Lattner | 39c27ed | 2009-01-31 19:05:27 +0000 | [diff] [blame] | 7986 | |
| 7987 | if (Instruction *TryI = dyn_cast<Instruction>(TryRes)) |
Evan Cheng | 4e56ab2 | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 7988 | if (TryI->use_empty()) |
| 7989 | EraseInstFromFunction(*TryI); |
| 7990 | } |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7991 | break; |
Evan Cheng | 4e56ab2 | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 7992 | } |
Evan Cheng | f35fd54 | 2009-01-15 17:01:23 +0000 | [diff] [blame] | 7993 | case Instruction::SExt: { |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7994 | DoXForm = NumCastsRemoved >= 2; |
Chris Lattner | 39c27ed | 2009-01-31 19:05:27 +0000 | [diff] [blame] | 7995 | if (!DoXForm && !isa<TruncInst>(SrcI) && 0) { |
Evan Cheng | 4e56ab2 | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 7996 | // If we do not have to emit the truncate + sext pair, then it's always |
| 7997 | // profitable to do this xform. |
Evan Cheng | f35fd54 | 2009-01-15 17:01:23 +0000 | [diff] [blame] | 7998 | // |
| 7999 | // It's not safe to eliminate the trunc + sext pair if one of the |
| 8000 | // eliminated cast is a truncate. e.g. |
| 8001 | // t2 = trunc i32 t1 to i16 |
| 8002 | // t3 = sext i16 t2 to i32 |
| 8003 | // != |
| 8004 | // i32 t1 |
Chris Lattner | 39c27ed | 2009-01-31 19:05:27 +0000 | [diff] [blame] | 8005 | Value *TryRes = EvaluateInDifferentType(SrcI, DestTy, true); |
Evan Cheng | 4e56ab2 | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 8006 | unsigned NumSignBits = ComputeNumSignBits(TryRes); |
| 8007 | if (NumSignBits > (DestBitSize - SrcBitSize)) |
| 8008 | return ReplaceInstUsesWith(CI, TryRes); |
Chris Lattner | 39c27ed | 2009-01-31 19:05:27 +0000 | [diff] [blame] | 8009 | |
| 8010 | if (Instruction *TryI = dyn_cast<Instruction>(TryRes)) |
Evan Cheng | 4e56ab2 | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 8011 | if (TryI->use_empty()) |
| 8012 | EraseInstFromFunction(*TryI); |
Evan Cheng | f35fd54 | 2009-01-15 17:01:23 +0000 | [diff] [blame] | 8013 | } |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 8014 | break; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8015 | } |
Evan Cheng | f35fd54 | 2009-01-15 17:01:23 +0000 | [diff] [blame] | 8016 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8017 | |
| 8018 | if (DoXForm) { |
Chris Lattner | 39c27ed | 2009-01-31 19:05:27 +0000 | [diff] [blame] | 8019 | DOUT << "ICE: EvaluateInDifferentType converting expression type to avoid" |
| 8020 | << " cast: " << CI; |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 8021 | Value *Res = EvaluateInDifferentType(SrcI, DestTy, |
| 8022 | CI.getOpcode() == Instruction::SExt); |
Evan Cheng | 4e56ab2 | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 8023 | if (JustReplace) |
Chris Lattner | 39c27ed | 2009-01-31 19:05:27 +0000 | [diff] [blame] | 8024 | // Just replace this cast with the result. |
| 8025 | return ReplaceInstUsesWith(CI, Res); |
Evan Cheng | 4e56ab2 | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 8026 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8027 | assert(Res->getType() == DestTy); |
| 8028 | switch (CI.getOpcode()) { |
| 8029 | default: assert(0 && "Unknown cast type!"); |
| 8030 | case Instruction::Trunc: |
| 8031 | case Instruction::BitCast: |
| 8032 | // Just replace this cast with the result. |
| 8033 | return ReplaceInstUsesWith(CI, Res); |
| 8034 | case Instruction::ZExt: { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8035 | assert(SrcBitSize < DestBitSize && "Not a zext?"); |
Evan Cheng | 4e56ab2 | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 8036 | |
| 8037 | // If the high bits are already zero, just replace this cast with the |
| 8038 | // result. |
| 8039 | APInt Mask(APInt::getBitsSet(DestBitSize, SrcBitSize, DestBitSize)); |
| 8040 | if (MaskedValueIsZero(Res, Mask)) |
| 8041 | return ReplaceInstUsesWith(CI, Res); |
| 8042 | |
| 8043 | // We need to emit an AND to clear the high bits. |
Chris Lattner | cd1d6d5 | 2007-04-02 05:48:58 +0000 | [diff] [blame] | 8044 | Constant *C = ConstantInt::get(APInt::getLowBitsSet(DestBitSize, |
| 8045 | SrcBitSize)); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8046 | return BinaryOperator::CreateAnd(Res, C); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8047 | } |
Evan Cheng | 4e56ab2 | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 8048 | case Instruction::SExt: { |
| 8049 | // If the high bits are already filled with sign bit, just replace this |
| 8050 | // cast with the result. |
| 8051 | unsigned NumSignBits = ComputeNumSignBits(Res); |
| 8052 | if (NumSignBits > (DestBitSize - SrcBitSize)) |
Evan Cheng | f35fd54 | 2009-01-15 17:01:23 +0000 | [diff] [blame] | 8053 | return ReplaceInstUsesWith(CI, Res); |
| 8054 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8055 | // 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] | 8056 | return CastInst::Create(Instruction::SExt, |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 8057 | InsertCastBefore(Instruction::Trunc, Res, Src->getType(), |
| 8058 | CI), DestTy); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8059 | } |
Evan Cheng | 4e56ab2 | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 8060 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8061 | } |
| 8062 | } |
| 8063 | |
| 8064 | Value *Op0 = SrcI->getNumOperands() > 0 ? SrcI->getOperand(0) : 0; |
| 8065 | Value *Op1 = SrcI->getNumOperands() > 1 ? SrcI->getOperand(1) : 0; |
| 8066 | |
| 8067 | switch (SrcI->getOpcode()) { |
| 8068 | case Instruction::Add: |
| 8069 | case Instruction::Mul: |
| 8070 | case Instruction::And: |
| 8071 | case Instruction::Or: |
| 8072 | case Instruction::Xor: |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 8073 | // If we are discarding information, rewrite. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8074 | if (DestBitSize <= SrcBitSize && DestBitSize != 1) { |
| 8075 | // Don't insert two casts if they cannot be eliminated. We allow |
| 8076 | // two casts to be inserted if the sizes are the same. This could |
| 8077 | // only be converting signedness, which is a noop. |
| 8078 | if (DestBitSize == SrcBitSize || |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8079 | !ValueRequiresCast(CI.getOpcode(), Op1, DestTy,TD) || |
| 8080 | !ValueRequiresCast(CI.getOpcode(), Op0, DestTy, TD)) { |
Reid Spencer | 7eb7638 | 2006-12-13 17:19:09 +0000 | [diff] [blame] | 8081 | Instruction::CastOps opcode = CI.getOpcode(); |
Eli Friedman | d1fd1da | 2008-11-30 21:09:11 +0000 | [diff] [blame] | 8082 | Value *Op0c = InsertCastBefore(opcode, Op0, DestTy, *SrcI); |
| 8083 | Value *Op1c = InsertCastBefore(opcode, Op1, DestTy, *SrcI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8084 | return BinaryOperator::Create( |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 8085 | cast<BinaryOperator>(SrcI)->getOpcode(), Op0c, Op1c); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8086 | } |
| 8087 | } |
| 8088 | |
| 8089 | // cast (xor bool X, true) to int --> xor (cast bool X to int), 1 |
| 8090 | if (isa<ZExtInst>(CI) && SrcBitSize == 1 && |
| 8091 | SrcI->getOpcode() == Instruction::Xor && |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 8092 | Op1 == ConstantInt::getTrue() && |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8093 | (!Op0->hasOneUse() || !isa<CmpInst>(Op0))) { |
Eli Friedman | d1fd1da | 2008-11-30 21:09:11 +0000 | [diff] [blame] | 8094 | Value *New = InsertCastBefore(Instruction::ZExt, Op0, DestTy, CI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8095 | return BinaryOperator::CreateXor(New, ConstantInt::get(CI.getType(), 1)); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8096 | } |
| 8097 | break; |
| 8098 | case Instruction::SDiv: |
| 8099 | case Instruction::UDiv: |
| 8100 | case Instruction::SRem: |
| 8101 | case Instruction::URem: |
| 8102 | // If we are just changing the sign, rewrite. |
| 8103 | if (DestBitSize == SrcBitSize) { |
| 8104 | // Don't insert two casts if they cannot be eliminated. We allow |
| 8105 | // two casts to be inserted if the sizes are the same. This could |
| 8106 | // only be converting signedness, which is a noop. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8107 | if (!ValueRequiresCast(CI.getOpcode(), Op1, DestTy, TD) || |
| 8108 | !ValueRequiresCast(CI.getOpcode(), Op0, DestTy, TD)) { |
Eli Friedman | d1fd1da | 2008-11-30 21:09:11 +0000 | [diff] [blame] | 8109 | Value *Op0c = InsertCastBefore(Instruction::BitCast, |
| 8110 | Op0, DestTy, *SrcI); |
| 8111 | Value *Op1c = InsertCastBefore(Instruction::BitCast, |
| 8112 | Op1, DestTy, *SrcI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8113 | return BinaryOperator::Create( |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8114 | cast<BinaryOperator>(SrcI)->getOpcode(), Op0c, Op1c); |
| 8115 | } |
| 8116 | } |
| 8117 | break; |
| 8118 | |
| 8119 | case Instruction::Shl: |
| 8120 | // Allow changing the sign of the source operand. Do not allow |
| 8121 | // changing the size of the shift, UNLESS the shift amount is a |
| 8122 | // constant. We must not change variable sized shifts to a smaller |
| 8123 | // size, because it is undefined to shift more bits out than exist |
| 8124 | // in the value. |
| 8125 | if (DestBitSize == SrcBitSize || |
| 8126 | (DestBitSize < SrcBitSize && isa<Constant>(Op1))) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 8127 | Instruction::CastOps opcode = (DestBitSize == SrcBitSize ? |
| 8128 | Instruction::BitCast : Instruction::Trunc); |
Eli Friedman | d1fd1da | 2008-11-30 21:09:11 +0000 | [diff] [blame] | 8129 | Value *Op0c = InsertCastBefore(opcode, Op0, DestTy, *SrcI); |
| 8130 | Value *Op1c = InsertCastBefore(opcode, Op1, DestTy, *SrcI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8131 | return BinaryOperator::CreateShl(Op0c, Op1c); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8132 | } |
| 8133 | break; |
| 8134 | case Instruction::AShr: |
| 8135 | // If this is a signed shr, and if all bits shifted in are about to be |
| 8136 | // truncated off, turn it into an unsigned shr to allow greater |
| 8137 | // simplifications. |
| 8138 | if (DestBitSize < SrcBitSize && |
| 8139 | isa<ConstantInt>(Op1)) { |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 8140 | uint32_t ShiftAmt = cast<ConstantInt>(Op1)->getLimitedValue(SrcBitSize); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8141 | if (SrcBitSize > ShiftAmt && SrcBitSize-ShiftAmt >= DestBitSize) { |
| 8142 | // Insert the new logical shift right. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8143 | return BinaryOperator::CreateLShr(Op0, Op1); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8144 | } |
| 8145 | } |
| 8146 | break; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8147 | } |
| 8148 | return 0; |
| 8149 | } |
| 8150 | |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 8151 | Instruction *InstCombiner::visitTrunc(TruncInst &CI) { |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 8152 | if (Instruction *Result = commonIntCastTransforms(CI)) |
| 8153 | return Result; |
| 8154 | |
| 8155 | Value *Src = CI.getOperand(0); |
| 8156 | const Type *Ty = CI.getType(); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 8157 | uint32_t DestBitWidth = Ty->getPrimitiveSizeInBits(); |
| 8158 | uint32_t SrcBitWidth = cast<IntegerType>(Src->getType())->getBitWidth(); |
Chris Lattner | 4f9797d | 2009-03-24 18:15:30 +0000 | [diff] [blame] | 8159 | |
| 8160 | // Canonicalize trunc x to i1 -> (icmp ne (and x, 1), 0) |
| 8161 | if (DestBitWidth == 1) { |
| 8162 | Constant *One = ConstantInt::get(Src->getType(), 1); |
| 8163 | Src = InsertNewInstBefore(BinaryOperator::CreateAnd(Src, One, "tmp"), CI); |
| 8164 | Value *Zero = Constant::getNullValue(Src->getType()); |
| 8165 | return new ICmpInst(ICmpInst::ICMP_NE, Src, Zero); |
| 8166 | } |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 8167 | |
Chris Lattner | 4f9797d | 2009-03-24 18:15:30 +0000 | [diff] [blame] | 8168 | // Optimize trunc(lshr(), c) to pull the shift through the truncate. |
| 8169 | ConstantInt *ShAmtV = 0; |
| 8170 | Value *ShiftOp = 0; |
| 8171 | if (Src->hasOneUse() && |
| 8172 | match(Src, m_LShr(m_Value(ShiftOp), m_ConstantInt(ShAmtV)))) { |
| 8173 | uint32_t ShAmt = ShAmtV->getLimitedValue(SrcBitWidth); |
| 8174 | |
| 8175 | // Get a mask for the bits shifting in. |
| 8176 | APInt Mask(APInt::getLowBitsSet(SrcBitWidth, ShAmt).shl(DestBitWidth)); |
| 8177 | if (MaskedValueIsZero(ShiftOp, Mask)) { |
| 8178 | if (ShAmt >= DestBitWidth) // All zeros. |
| 8179 | return ReplaceInstUsesWith(CI, Constant::getNullValue(Ty)); |
| 8180 | |
| 8181 | // Okay, we can shrink this. Truncate the input, then return a new |
| 8182 | // shift. |
| 8183 | Value *V1 = InsertCastBefore(Instruction::Trunc, ShiftOp, Ty, CI); |
| 8184 | Value *V2 = ConstantExpr::getTrunc(ShAmtV, Ty); |
| 8185 | return BinaryOperator::CreateLShr(V1, V2); |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 8186 | } |
| 8187 | } |
| 8188 | |
| 8189 | return 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8190 | } |
| 8191 | |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 8192 | /// transformZExtICmp - Transform (zext icmp) to bitwise / integer operations |
| 8193 | /// in order to eliminate the icmp. |
| 8194 | Instruction *InstCombiner::transformZExtICmp(ICmpInst *ICI, Instruction &CI, |
| 8195 | bool DoXform) { |
| 8196 | // If we are just checking for a icmp eq of a single bit and zext'ing it |
| 8197 | // to an integer, then shift the bit to the appropriate place and then |
| 8198 | // cast to integer to avoid the comparison. |
| 8199 | if (ConstantInt *Op1C = dyn_cast<ConstantInt>(ICI->getOperand(1))) { |
| 8200 | const APInt &Op1CV = Op1C->getValue(); |
| 8201 | |
| 8202 | // zext (x <s 0) to i32 --> x>>u31 true if signbit set. |
| 8203 | // zext (x >s -1) to i32 --> (x>>u31)^1 true if signbit clear. |
| 8204 | if ((ICI->getPredicate() == ICmpInst::ICMP_SLT && Op1CV == 0) || |
| 8205 | (ICI->getPredicate() == ICmpInst::ICMP_SGT &&Op1CV.isAllOnesValue())) { |
| 8206 | if (!DoXform) return ICI; |
| 8207 | |
| 8208 | Value *In = ICI->getOperand(0); |
| 8209 | Value *Sh = ConstantInt::get(In->getType(), |
| 8210 | In->getType()->getPrimitiveSizeInBits()-1); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8211 | In = InsertNewInstBefore(BinaryOperator::CreateLShr(In, Sh, |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 8212 | In->getName()+".lobit"), |
| 8213 | CI); |
| 8214 | if (In->getType() != CI.getType()) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8215 | In = CastInst::CreateIntegerCast(In, CI.getType(), |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 8216 | false/*ZExt*/, "tmp", &CI); |
| 8217 | |
| 8218 | if (ICI->getPredicate() == ICmpInst::ICMP_SGT) { |
| 8219 | Constant *One = ConstantInt::get(In->getType(), 1); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8220 | In = InsertNewInstBefore(BinaryOperator::CreateXor(In, One, |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 8221 | In->getName()+".not"), |
| 8222 | CI); |
| 8223 | } |
| 8224 | |
| 8225 | return ReplaceInstUsesWith(CI, In); |
| 8226 | } |
| 8227 | |
| 8228 | |
| 8229 | |
| 8230 | // zext (X == 0) to i32 --> X^1 iff X has only the low bit set. |
| 8231 | // zext (X == 0) to i32 --> (X>>1)^1 iff X has only the 2nd bit set. |
| 8232 | // zext (X == 1) to i32 --> X iff X has only the low bit set. |
| 8233 | // zext (X == 2) to i32 --> X>>1 iff X has only the 2nd bit set. |
| 8234 | // zext (X != 0) to i32 --> X iff X has only the low bit set. |
| 8235 | // zext (X != 0) to i32 --> X>>1 iff X has only the 2nd bit set. |
| 8236 | // zext (X != 1) to i32 --> X^1 iff X has only the low bit set. |
| 8237 | // zext (X != 2) to i32 --> (X>>1)^1 iff X has only the 2nd bit set. |
| 8238 | if ((Op1CV == 0 || Op1CV.isPowerOf2()) && |
| 8239 | // This only works for EQ and NE |
| 8240 | ICI->isEquality()) { |
| 8241 | // If Op1C some other power of two, convert: |
| 8242 | uint32_t BitWidth = Op1C->getType()->getBitWidth(); |
| 8243 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 8244 | APInt TypeMask(APInt::getAllOnesValue(BitWidth)); |
| 8245 | ComputeMaskedBits(ICI->getOperand(0), TypeMask, KnownZero, KnownOne); |
| 8246 | |
| 8247 | APInt KnownZeroMask(~KnownZero); |
| 8248 | if (KnownZeroMask.isPowerOf2()) { // Exactly 1 possible 1? |
| 8249 | if (!DoXform) return ICI; |
| 8250 | |
| 8251 | bool isNE = ICI->getPredicate() == ICmpInst::ICMP_NE; |
| 8252 | if (Op1CV != 0 && (Op1CV != KnownZeroMask)) { |
| 8253 | // (X&4) == 2 --> false |
| 8254 | // (X&4) != 2 --> true |
| 8255 | Constant *Res = ConstantInt::get(Type::Int1Ty, isNE); |
| 8256 | Res = ConstantExpr::getZExt(Res, CI.getType()); |
| 8257 | return ReplaceInstUsesWith(CI, Res); |
| 8258 | } |
| 8259 | |
| 8260 | uint32_t ShiftAmt = KnownZeroMask.logBase2(); |
| 8261 | Value *In = ICI->getOperand(0); |
| 8262 | if (ShiftAmt) { |
| 8263 | // Perform a logical shr by shiftamt. |
| 8264 | // Insert the shift to put the result in the low bit. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8265 | In = InsertNewInstBefore(BinaryOperator::CreateLShr(In, |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 8266 | ConstantInt::get(In->getType(), ShiftAmt), |
| 8267 | In->getName()+".lobit"), CI); |
| 8268 | } |
| 8269 | |
| 8270 | if ((Op1CV != 0) == isNE) { // Toggle the low bit. |
| 8271 | Constant *One = ConstantInt::get(In->getType(), 1); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8272 | In = BinaryOperator::CreateXor(In, One, "tmp"); |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 8273 | InsertNewInstBefore(cast<Instruction>(In), CI); |
| 8274 | } |
| 8275 | |
| 8276 | if (CI.getType() == In->getType()) |
| 8277 | return ReplaceInstUsesWith(CI, In); |
| 8278 | else |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8279 | return CastInst::CreateIntegerCast(In, CI.getType(), false/*ZExt*/); |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 8280 | } |
| 8281 | } |
| 8282 | } |
| 8283 | |
| 8284 | return 0; |
| 8285 | } |
| 8286 | |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 8287 | Instruction *InstCombiner::visitZExt(ZExtInst &CI) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8288 | // If one of the common conversion will work .. |
| 8289 | if (Instruction *Result = commonIntCastTransforms(CI)) |
| 8290 | return Result; |
| 8291 | |
| 8292 | Value *Src = CI.getOperand(0); |
| 8293 | |
Chris Lattner | a84f47c | 2009-02-17 20:47:23 +0000 | [diff] [blame] | 8294 | // If this is a TRUNC followed by a ZEXT then we are dealing with integral |
| 8295 | // types and if the sizes are just right we can convert this into a logical |
| 8296 | // 'and' which will be much cheaper than the pair of casts. |
| 8297 | if (TruncInst *CSrc = dyn_cast<TruncInst>(Src)) { // A->B->C cast |
| 8298 | // Get the sizes of the types involved. We know that the intermediate type |
| 8299 | // will be smaller than A or C, but don't know the relation between A and C. |
| 8300 | Value *A = CSrc->getOperand(0); |
| 8301 | unsigned SrcSize = A->getType()->getPrimitiveSizeInBits(); |
| 8302 | unsigned MidSize = CSrc->getType()->getPrimitiveSizeInBits(); |
| 8303 | unsigned DstSize = CI.getType()->getPrimitiveSizeInBits(); |
| 8304 | // If we're actually extending zero bits, then if |
| 8305 | // SrcSize < DstSize: zext(a & mask) |
| 8306 | // SrcSize == DstSize: a & mask |
| 8307 | // SrcSize > DstSize: trunc(a) & mask |
| 8308 | if (SrcSize < DstSize) { |
| 8309 | APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize)); |
| 8310 | Constant *AndConst = ConstantInt::get(AndValue); |
| 8311 | Instruction *And = |
| 8312 | BinaryOperator::CreateAnd(A, AndConst, CSrc->getName()+".mask"); |
| 8313 | InsertNewInstBefore(And, CI); |
| 8314 | return new ZExtInst(And, CI.getType()); |
| 8315 | } else if (SrcSize == DstSize) { |
| 8316 | APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize)); |
| 8317 | return BinaryOperator::CreateAnd(A, ConstantInt::get(AndValue)); |
| 8318 | } else if (SrcSize > DstSize) { |
| 8319 | Instruction *Trunc = new TruncInst(A, CI.getType(), "tmp"); |
| 8320 | InsertNewInstBefore(Trunc, CI); |
| 8321 | APInt AndValue(APInt::getLowBitsSet(DstSize, MidSize)); |
| 8322 | return BinaryOperator::CreateAnd(Trunc, ConstantInt::get(AndValue)); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8323 | } |
| 8324 | } |
| 8325 | |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 8326 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(Src)) |
| 8327 | return transformZExtICmp(ICI, CI); |
Chris Lattner | a2e2c9b | 2007-04-11 06:53:04 +0000 | [diff] [blame] | 8328 | |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 8329 | BinaryOperator *SrcI = dyn_cast<BinaryOperator>(Src); |
| 8330 | if (SrcI && SrcI->getOpcode() == Instruction::Or) { |
| 8331 | // zext (or icmp, icmp) --> or (zext icmp), (zext icmp) if at least one |
| 8332 | // of the (zext icmp) will be transformed. |
| 8333 | ICmpInst *LHS = dyn_cast<ICmpInst>(SrcI->getOperand(0)); |
| 8334 | ICmpInst *RHS = dyn_cast<ICmpInst>(SrcI->getOperand(1)); |
| 8335 | if (LHS && RHS && LHS->hasOneUse() && RHS->hasOneUse() && |
| 8336 | (transformZExtICmp(LHS, CI, false) || |
| 8337 | transformZExtICmp(RHS, CI, false))) { |
| 8338 | Value *LCast = InsertCastBefore(Instruction::ZExt, LHS, CI.getType(), CI); |
| 8339 | Value *RCast = InsertCastBefore(Instruction::ZExt, RHS, CI.getType(), CI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8340 | return BinaryOperator::Create(Instruction::Or, LCast, RCast); |
Chris Lattner | 66bc325 | 2007-04-11 05:45:39 +0000 | [diff] [blame] | 8341 | } |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 8342 | } |
| 8343 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8344 | return 0; |
| 8345 | } |
| 8346 | |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 8347 | Instruction *InstCombiner::visitSExt(SExtInst &CI) { |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 8348 | if (Instruction *I = commonIntCastTransforms(CI)) |
| 8349 | return I; |
| 8350 | |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 8351 | Value *Src = CI.getOperand(0); |
| 8352 | |
Dan Gohman | 1975d03 | 2008-10-30 20:40:10 +0000 | [diff] [blame] | 8353 | // Canonicalize sign-extend from i1 to a select. |
| 8354 | if (Src->getType() == Type::Int1Ty) |
| 8355 | return SelectInst::Create(Src, |
| 8356 | ConstantInt::getAllOnesValue(CI.getType()), |
| 8357 | Constant::getNullValue(CI.getType())); |
Dan Gohman | f35c882 | 2008-05-20 21:01:12 +0000 | [diff] [blame] | 8358 | |
| 8359 | // See if the value being truncated is already sign extended. If so, just |
| 8360 | // eliminate the trunc/sext pair. |
| 8361 | if (getOpcode(Src) == Instruction::Trunc) { |
| 8362 | Value *Op = cast<User>(Src)->getOperand(0); |
| 8363 | unsigned OpBits = cast<IntegerType>(Op->getType())->getBitWidth(); |
| 8364 | unsigned MidBits = cast<IntegerType>(Src->getType())->getBitWidth(); |
| 8365 | unsigned DestBits = cast<IntegerType>(CI.getType())->getBitWidth(); |
| 8366 | unsigned NumSignBits = ComputeNumSignBits(Op); |
| 8367 | |
| 8368 | if (OpBits == DestBits) { |
| 8369 | // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign |
| 8370 | // bits, it is already ready. |
| 8371 | if (NumSignBits > DestBits-MidBits) |
| 8372 | return ReplaceInstUsesWith(CI, Op); |
| 8373 | } else if (OpBits < DestBits) { |
| 8374 | // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign |
| 8375 | // bits, just sext from i32. |
| 8376 | if (NumSignBits > OpBits-MidBits) |
| 8377 | return new SExtInst(Op, CI.getType(), "tmp"); |
| 8378 | } else { |
| 8379 | // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign |
| 8380 | // bits, just truncate to i32. |
| 8381 | if (NumSignBits > OpBits-MidBits) |
| 8382 | return new TruncInst(Op, CI.getType(), "tmp"); |
| 8383 | } |
| 8384 | } |
Chris Lattner | 46bbad2 | 2008-08-06 07:35:52 +0000 | [diff] [blame] | 8385 | |
| 8386 | // If the input is a shl/ashr pair of a same constant, then this is a sign |
| 8387 | // extension from a smaller value. If we could trust arbitrary bitwidth |
| 8388 | // integers, we could turn this into a truncate to the smaller bit and then |
| 8389 | // use a sext for the whole extension. Since we don't, look deeper and check |
| 8390 | // for a truncate. If the source and dest are the same type, eliminate the |
| 8391 | // trunc and extend and just do shifts. For example, turn: |
| 8392 | // %a = trunc i32 %i to i8 |
| 8393 | // %b = shl i8 %a, 6 |
| 8394 | // %c = ashr i8 %b, 6 |
| 8395 | // %d = sext i8 %c to i32 |
| 8396 | // into: |
| 8397 | // %a = shl i32 %i, 30 |
| 8398 | // %d = ashr i32 %a, 30 |
| 8399 | Value *A = 0; |
| 8400 | ConstantInt *BA = 0, *CA = 0; |
| 8401 | if (match(Src, m_AShr(m_Shl(m_Value(A), m_ConstantInt(BA)), |
| 8402 | m_ConstantInt(CA))) && |
| 8403 | BA == CA && isa<TruncInst>(A)) { |
| 8404 | Value *I = cast<TruncInst>(A)->getOperand(0); |
| 8405 | if (I->getType() == CI.getType()) { |
| 8406 | unsigned MidSize = Src->getType()->getPrimitiveSizeInBits(); |
| 8407 | unsigned SrcDstSize = CI.getType()->getPrimitiveSizeInBits(); |
| 8408 | unsigned ShAmt = CA->getZExtValue()+SrcDstSize-MidSize; |
| 8409 | Constant *ShAmtV = ConstantInt::get(CI.getType(), ShAmt); |
| 8410 | I = InsertNewInstBefore(BinaryOperator::CreateShl(I, ShAmtV, |
| 8411 | CI.getName()), CI); |
| 8412 | return BinaryOperator::CreateAShr(I, ShAmtV); |
| 8413 | } |
| 8414 | } |
| 8415 | |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 8416 | return 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8417 | } |
| 8418 | |
Chris Lattner | b753065 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 8419 | /// FitsInFPType - Return a Constant* for the specified FP constant if it fits |
| 8420 | /// in the specified FP type without changing its value. |
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame] | 8421 | static Constant *FitsInFPType(ConstantFP *CFP, const fltSemantics &Sem) { |
Dale Johannesen | 23a9855 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 8422 | bool losesInfo; |
Chris Lattner | b753065 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 8423 | APFloat F = CFP->getValueAPF(); |
Dale Johannesen | 23a9855 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 8424 | (void)F.convert(Sem, APFloat::rmNearestTiesToEven, &losesInfo); |
| 8425 | if (!losesInfo) |
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame] | 8426 | return ConstantFP::get(F); |
Chris Lattner | b753065 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 8427 | return 0; |
| 8428 | } |
| 8429 | |
| 8430 | /// LookThroughFPExtensions - If this is an fp extension instruction, look |
| 8431 | /// through it until we get the source value. |
| 8432 | static Value *LookThroughFPExtensions(Value *V) { |
| 8433 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 8434 | if (I->getOpcode() == Instruction::FPExt) |
| 8435 | return LookThroughFPExtensions(I->getOperand(0)); |
| 8436 | |
| 8437 | // If this value is a constant, return the constant in the smallest FP type |
| 8438 | // that can accurately represent it. This allows us to turn |
| 8439 | // (float)((double)X+2.0) into x+2.0f. |
| 8440 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) { |
| 8441 | if (CFP->getType() == Type::PPC_FP128Ty) |
| 8442 | return V; // No constant folding of this. |
| 8443 | // See if the value can be truncated to float and then reextended. |
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame] | 8444 | if (Value *V = FitsInFPType(CFP, APFloat::IEEEsingle)) |
Chris Lattner | b753065 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 8445 | return V; |
| 8446 | if (CFP->getType() == Type::DoubleTy) |
| 8447 | return V; // Won't shrink. |
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame] | 8448 | if (Value *V = FitsInFPType(CFP, APFloat::IEEEdouble)) |
Chris Lattner | b753065 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 8449 | return V; |
| 8450 | // Don't try to shrink to various long double types. |
| 8451 | } |
| 8452 | |
| 8453 | return V; |
| 8454 | } |
| 8455 | |
| 8456 | Instruction *InstCombiner::visitFPTrunc(FPTruncInst &CI) { |
| 8457 | if (Instruction *I = commonCastTransforms(CI)) |
| 8458 | return I; |
| 8459 | |
| 8460 | // If we have fptrunc(add (fpextend x), (fpextend y)), where x and y are |
| 8461 | // smaller than the destination type, we can eliminate the truncate by doing |
| 8462 | // the add as the smaller type. This applies to add/sub/mul/div as well as |
| 8463 | // many builtins (sqrt, etc). |
| 8464 | BinaryOperator *OpI = dyn_cast<BinaryOperator>(CI.getOperand(0)); |
| 8465 | if (OpI && OpI->hasOneUse()) { |
| 8466 | switch (OpI->getOpcode()) { |
| 8467 | default: break; |
| 8468 | case Instruction::Add: |
| 8469 | case Instruction::Sub: |
| 8470 | case Instruction::Mul: |
| 8471 | case Instruction::FDiv: |
| 8472 | case Instruction::FRem: |
| 8473 | const Type *SrcTy = OpI->getType(); |
| 8474 | Value *LHSTrunc = LookThroughFPExtensions(OpI->getOperand(0)); |
| 8475 | Value *RHSTrunc = LookThroughFPExtensions(OpI->getOperand(1)); |
| 8476 | if (LHSTrunc->getType() != SrcTy && |
| 8477 | RHSTrunc->getType() != SrcTy) { |
| 8478 | unsigned DstSize = CI.getType()->getPrimitiveSizeInBits(); |
| 8479 | // If the source types were both smaller than the destination type of |
| 8480 | // the cast, do this xform. |
| 8481 | if (LHSTrunc->getType()->getPrimitiveSizeInBits() <= DstSize && |
| 8482 | RHSTrunc->getType()->getPrimitiveSizeInBits() <= DstSize) { |
| 8483 | LHSTrunc = InsertCastBefore(Instruction::FPExt, LHSTrunc, |
| 8484 | CI.getType(), CI); |
| 8485 | RHSTrunc = InsertCastBefore(Instruction::FPExt, RHSTrunc, |
| 8486 | CI.getType(), CI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8487 | return BinaryOperator::Create(OpI->getOpcode(), LHSTrunc, RHSTrunc); |
Chris Lattner | b753065 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 8488 | } |
| 8489 | } |
| 8490 | break; |
| 8491 | } |
| 8492 | } |
| 8493 | return 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8494 | } |
| 8495 | |
| 8496 | Instruction *InstCombiner::visitFPExt(CastInst &CI) { |
| 8497 | return commonCastTransforms(CI); |
| 8498 | } |
| 8499 | |
Chris Lattner | 0c7a9a0 | 2008-05-19 20:25:04 +0000 | [diff] [blame] | 8500 | Instruction *InstCombiner::visitFPToUI(FPToUIInst &FI) { |
Chris Lattner | 5af5f46 | 2008-08-06 05:13:06 +0000 | [diff] [blame] | 8501 | Instruction *OpI = dyn_cast<Instruction>(FI.getOperand(0)); |
| 8502 | if (OpI == 0) |
| 8503 | return commonCastTransforms(FI); |
| 8504 | |
| 8505 | // fptoui(uitofp(X)) --> X |
| 8506 | // fptoui(sitofp(X)) --> X |
| 8507 | // This is safe if the intermediate type has enough bits in its mantissa to |
| 8508 | // accurately represent all values of X. For example, do not do this with |
| 8509 | // i64->float->i64. This is also safe for sitofp case, because any negative |
| 8510 | // 'X' value would cause an undefined result for the fptoui. |
| 8511 | if ((isa<UIToFPInst>(OpI) || isa<SIToFPInst>(OpI)) && |
| 8512 | OpI->getOperand(0)->getType() == FI.getType() && |
| 8513 | (int)FI.getType()->getPrimitiveSizeInBits() < /*extra bit for sign */ |
| 8514 | OpI->getType()->getFPMantissaWidth()) |
| 8515 | return ReplaceInstUsesWith(FI, OpI->getOperand(0)); |
Chris Lattner | 0c7a9a0 | 2008-05-19 20:25:04 +0000 | [diff] [blame] | 8516 | |
| 8517 | return commonCastTransforms(FI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8518 | } |
| 8519 | |
Chris Lattner | 0c7a9a0 | 2008-05-19 20:25:04 +0000 | [diff] [blame] | 8520 | Instruction *InstCombiner::visitFPToSI(FPToSIInst &FI) { |
Chris Lattner | 5af5f46 | 2008-08-06 05:13:06 +0000 | [diff] [blame] | 8521 | Instruction *OpI = dyn_cast<Instruction>(FI.getOperand(0)); |
| 8522 | if (OpI == 0) |
| 8523 | return commonCastTransforms(FI); |
| 8524 | |
| 8525 | // fptosi(sitofp(X)) --> X |
| 8526 | // fptosi(uitofp(X)) --> X |
| 8527 | // This is safe if the intermediate type has enough bits in its mantissa to |
| 8528 | // accurately represent all values of X. For example, do not do this with |
| 8529 | // i64->float->i64. This is also safe for sitofp case, because any negative |
| 8530 | // 'X' value would cause an undefined result for the fptoui. |
| 8531 | if ((isa<UIToFPInst>(OpI) || isa<SIToFPInst>(OpI)) && |
| 8532 | OpI->getOperand(0)->getType() == FI.getType() && |
| 8533 | (int)FI.getType()->getPrimitiveSizeInBits() <= |
| 8534 | OpI->getType()->getFPMantissaWidth()) |
| 8535 | return ReplaceInstUsesWith(FI, OpI->getOperand(0)); |
Chris Lattner | 0c7a9a0 | 2008-05-19 20:25:04 +0000 | [diff] [blame] | 8536 | |
| 8537 | return commonCastTransforms(FI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8538 | } |
| 8539 | |
| 8540 | Instruction *InstCombiner::visitUIToFP(CastInst &CI) { |
| 8541 | return commonCastTransforms(CI); |
| 8542 | } |
| 8543 | |
| 8544 | Instruction *InstCombiner::visitSIToFP(CastInst &CI) { |
| 8545 | return commonCastTransforms(CI); |
| 8546 | } |
| 8547 | |
Chris Lattner | a0e6969 | 2009-03-24 18:35:40 +0000 | [diff] [blame] | 8548 | Instruction *InstCombiner::visitPtrToInt(PtrToIntInst &CI) { |
| 8549 | // If the destination integer type is smaller than the intptr_t type for |
| 8550 | // this target, do a ptrtoint to intptr_t then do a trunc. This allows the |
| 8551 | // trunc to be exposed to other transforms. Don't do this for extending |
| 8552 | // ptrtoint's, because we don't know if the target sign or zero extends its |
| 8553 | // pointers. |
| 8554 | if (CI.getType()->getPrimitiveSizeInBits() < TD->getPointerSizeInBits()) { |
| 8555 | Value *P = InsertNewInstBefore(new PtrToIntInst(CI.getOperand(0), |
| 8556 | TD->getIntPtrType(), |
| 8557 | "tmp"), CI); |
| 8558 | return new TruncInst(P, CI.getType()); |
| 8559 | } |
| 8560 | |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 8561 | return commonPointerCastTransforms(CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8562 | } |
| 8563 | |
Chris Lattner | f9d9e45 | 2008-01-08 07:23:51 +0000 | [diff] [blame] | 8564 | Instruction *InstCombiner::visitIntToPtr(IntToPtrInst &CI) { |
Chris Lattner | a0e6969 | 2009-03-24 18:35:40 +0000 | [diff] [blame] | 8565 | // If the source integer type is larger than the intptr_t type for |
| 8566 | // this target, do a trunc to the intptr_t type, then inttoptr of it. This |
| 8567 | // allows the trunc to be exposed to other transforms. Don't do this for |
| 8568 | // extending inttoptr's, because we don't know if the target sign or zero |
| 8569 | // extends to pointers. |
| 8570 | if (CI.getOperand(0)->getType()->getPrimitiveSizeInBits() > |
| 8571 | TD->getPointerSizeInBits()) { |
| 8572 | Value *P = InsertNewInstBefore(new TruncInst(CI.getOperand(0), |
| 8573 | TD->getIntPtrType(), |
| 8574 | "tmp"), CI); |
| 8575 | return new IntToPtrInst(P, CI.getType()); |
| 8576 | } |
| 8577 | |
Chris Lattner | f9d9e45 | 2008-01-08 07:23:51 +0000 | [diff] [blame] | 8578 | if (Instruction *I = commonCastTransforms(CI)) |
| 8579 | return I; |
| 8580 | |
| 8581 | const Type *DestPointee = cast<PointerType>(CI.getType())->getElementType(); |
| 8582 | if (!DestPointee->isSized()) return 0; |
| 8583 | |
| 8584 | // If this is inttoptr(add (ptrtoint x), cst), try to turn this into a GEP. |
| 8585 | ConstantInt *Cst; |
| 8586 | Value *X; |
| 8587 | if (match(CI.getOperand(0), m_Add(m_Cast<PtrToIntInst>(m_Value(X)), |
| 8588 | m_ConstantInt(Cst)))) { |
| 8589 | // If the source and destination operands have the same type, see if this |
| 8590 | // is a single-index GEP. |
| 8591 | if (X->getType() == CI.getType()) { |
| 8592 | // Get the size of the pointee type. |
Duncan Sands | ceb4d1a | 2009-01-12 20:38:59 +0000 | [diff] [blame] | 8593 | uint64_t Size = TD->getTypePaddedSize(DestPointee); |
Chris Lattner | f9d9e45 | 2008-01-08 07:23:51 +0000 | [diff] [blame] | 8594 | |
| 8595 | // Convert the constant to intptr type. |
| 8596 | APInt Offset = Cst->getValue(); |
| 8597 | Offset.sextOrTrunc(TD->getPointerSizeInBits()); |
| 8598 | |
| 8599 | // If Offset is evenly divisible by Size, we can do this xform. |
| 8600 | if (Size && !APIntOps::srem(Offset, APInt(Offset.getBitWidth(), Size))){ |
| 8601 | Offset = APIntOps::sdiv(Offset, APInt(Offset.getBitWidth(), Size)); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 8602 | return GetElementPtrInst::Create(X, ConstantInt::get(Offset)); |
Chris Lattner | f9d9e45 | 2008-01-08 07:23:51 +0000 | [diff] [blame] | 8603 | } |
| 8604 | } |
| 8605 | // TODO: Could handle other cases, e.g. where add is indexing into field of |
| 8606 | // struct etc. |
| 8607 | } else if (CI.getOperand(0)->hasOneUse() && |
| 8608 | match(CI.getOperand(0), m_Add(m_Value(X), m_ConstantInt(Cst)))) { |
| 8609 | // Otherwise, if this is inttoptr(add x, cst), try to turn this into an |
| 8610 | // "inttoptr+GEP" instead of "add+intptr". |
| 8611 | |
| 8612 | // Get the size of the pointee type. |
Duncan Sands | ceb4d1a | 2009-01-12 20:38:59 +0000 | [diff] [blame] | 8613 | uint64_t Size = TD->getTypePaddedSize(DestPointee); |
Chris Lattner | f9d9e45 | 2008-01-08 07:23:51 +0000 | [diff] [blame] | 8614 | |
| 8615 | // Convert the constant to intptr type. |
| 8616 | APInt Offset = Cst->getValue(); |
| 8617 | Offset.sextOrTrunc(TD->getPointerSizeInBits()); |
| 8618 | |
| 8619 | // If Offset is evenly divisible by Size, we can do this xform. |
| 8620 | if (Size && !APIntOps::srem(Offset, APInt(Offset.getBitWidth(), Size))){ |
| 8621 | Offset = APIntOps::sdiv(Offset, APInt(Offset.getBitWidth(), Size)); |
| 8622 | |
| 8623 | Instruction *P = InsertNewInstBefore(new IntToPtrInst(X, CI.getType(), |
| 8624 | "tmp"), CI); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 8625 | return GetElementPtrInst::Create(P, ConstantInt::get(Offset), "tmp"); |
Chris Lattner | f9d9e45 | 2008-01-08 07:23:51 +0000 | [diff] [blame] | 8626 | } |
| 8627 | } |
| 8628 | return 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8629 | } |
| 8630 | |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 8631 | Instruction *InstCombiner::visitBitCast(BitCastInst &CI) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8632 | // If the operands are integer typed then apply the integer transforms, |
| 8633 | // otherwise just apply the common ones. |
| 8634 | Value *Src = CI.getOperand(0); |
| 8635 | const Type *SrcTy = Src->getType(); |
| 8636 | const Type *DestTy = CI.getType(); |
| 8637 | |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 8638 | if (SrcTy->isInteger() && DestTy->isInteger()) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8639 | if (Instruction *Result = commonIntCastTransforms(CI)) |
| 8640 | return Result; |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 8641 | } else if (isa<PointerType>(SrcTy)) { |
| 8642 | if (Instruction *I = commonPointerCastTransforms(CI)) |
| 8643 | return I; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8644 | } else { |
| 8645 | if (Instruction *Result = commonCastTransforms(CI)) |
| 8646 | return Result; |
| 8647 | } |
| 8648 | |
| 8649 | |
| 8650 | // Get rid of casts from one type to the same type. These are useless and can |
| 8651 | // be replaced by the operand. |
| 8652 | if (DestTy == Src->getType()) |
| 8653 | return ReplaceInstUsesWith(CI, Src); |
| 8654 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8655 | if (const PointerType *DstPTy = dyn_cast<PointerType>(DestTy)) { |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 8656 | const PointerType *SrcPTy = cast<PointerType>(SrcTy); |
| 8657 | const Type *DstElTy = DstPTy->getElementType(); |
| 8658 | const Type *SrcElTy = SrcPTy->getElementType(); |
| 8659 | |
Nate Begeman | 83ad90a | 2008-03-31 00:22:16 +0000 | [diff] [blame] | 8660 | // If the address spaces don't match, don't eliminate the bitcast, which is |
| 8661 | // required for changing types. |
| 8662 | if (SrcPTy->getAddressSpace() != DstPTy->getAddressSpace()) |
| 8663 | return 0; |
| 8664 | |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 8665 | // If we are casting a malloc or alloca to a pointer to a type of the same |
| 8666 | // size, rewrite the allocation instruction to allocate the "right" type. |
| 8667 | if (AllocationInst *AI = dyn_cast<AllocationInst>(Src)) |
| 8668 | if (Instruction *V = PromoteCastOfAllocation(CI, *AI)) |
| 8669 | return V; |
| 8670 | |
Chris Lattner | d717c18 | 2007-05-05 22:32:24 +0000 | [diff] [blame] | 8671 | // If the source and destination are pointers, and this cast is equivalent |
| 8672 | // 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] | 8673 | // This can enhance SROA and other transforms that want type-safe pointers. |
| 8674 | Constant *ZeroUInt = Constant::getNullValue(Type::Int32Ty); |
| 8675 | unsigned NumZeros = 0; |
| 8676 | while (SrcElTy != DstElTy && |
| 8677 | isa<CompositeType>(SrcElTy) && !isa<PointerType>(SrcElTy) && |
| 8678 | SrcElTy->getNumContainedTypes() /* not "{}" */) { |
| 8679 | SrcElTy = cast<CompositeType>(SrcElTy)->getTypeAtIndex(ZeroUInt); |
| 8680 | ++NumZeros; |
| 8681 | } |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 8682 | |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 8683 | // If we found a path from the src to dest, create the getelementptr now. |
| 8684 | if (SrcElTy == DstElTy) { |
| 8685 | SmallVector<Value*, 8> Idxs(NumZeros+1, ZeroUInt); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 8686 | return GetElementPtrInst::Create(Src, Idxs.begin(), Idxs.end(), "", |
| 8687 | ((Instruction*) NULL)); |
Chris Lattner | 9fb9213 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 8688 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8689 | } |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 8690 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8691 | if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(Src)) { |
| 8692 | if (SVI->hasOneUse()) { |
| 8693 | // Okay, we have (bitconvert (shuffle ..)). Check to see if this is |
| 8694 | // a bitconvert to a vector with the same # elts. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 8695 | if (isa<VectorType>(DestTy) && |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 8696 | cast<VectorType>(DestTy)->getNumElements() == |
| 8697 | SVI->getType()->getNumElements() && |
| 8698 | SVI->getType()->getNumElements() == |
| 8699 | cast<VectorType>(SVI->getOperand(0)->getType())->getNumElements()) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8700 | CastInst *Tmp; |
| 8701 | // If either of the operands is a cast from CI.getType(), then |
| 8702 | // evaluating the shuffle in the casted destination's type will allow |
| 8703 | // us to eliminate at least one cast. |
| 8704 | if (((Tmp = dyn_cast<CastInst>(SVI->getOperand(0))) && |
| 8705 | Tmp->getOperand(0)->getType() == DestTy) || |
| 8706 | ((Tmp = dyn_cast<CastInst>(SVI->getOperand(1))) && |
| 8707 | Tmp->getOperand(0)->getType() == DestTy)) { |
Eli Friedman | d1fd1da | 2008-11-30 21:09:11 +0000 | [diff] [blame] | 8708 | Value *LHS = InsertCastBefore(Instruction::BitCast, |
| 8709 | SVI->getOperand(0), DestTy, CI); |
| 8710 | Value *RHS = InsertCastBefore(Instruction::BitCast, |
| 8711 | SVI->getOperand(1), DestTy, CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8712 | // Return a new shuffle vector. Use the same element ID's, as we |
| 8713 | // know the vector types match #elts. |
| 8714 | return new ShuffleVectorInst(LHS, RHS, SVI->getOperand(2)); |
Chris Lattner | 01575b7 | 2006-05-25 23:24:33 +0000 | [diff] [blame] | 8715 | } |
| 8716 | } |
| 8717 | } |
| 8718 | } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 8719 | return 0; |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 8720 | } |
| 8721 | |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8722 | /// GetSelectFoldableOperands - We want to turn code that looks like this: |
| 8723 | /// %C = or %A, %B |
| 8724 | /// %D = select %cond, %C, %A |
| 8725 | /// into: |
| 8726 | /// %C = select %cond, %B, 0 |
| 8727 | /// %D = or %A, %C |
| 8728 | /// |
| 8729 | /// Assuming that the specified instruction is an operand to the select, return |
| 8730 | /// a bitmask indicating which operands of this instruction are foldable if they |
| 8731 | /// equal the other incoming value of the select. |
| 8732 | /// |
| 8733 | static unsigned GetSelectFoldableOperands(Instruction *I) { |
| 8734 | switch (I->getOpcode()) { |
| 8735 | case Instruction::Add: |
| 8736 | case Instruction::Mul: |
| 8737 | case Instruction::And: |
| 8738 | case Instruction::Or: |
| 8739 | case Instruction::Xor: |
| 8740 | return 3; // Can fold through either operand. |
| 8741 | case Instruction::Sub: // Can only fold on the amount subtracted. |
| 8742 | case Instruction::Shl: // Can only fold on the shift amount. |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 8743 | case Instruction::LShr: |
| 8744 | case Instruction::AShr: |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8745 | return 1; |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8746 | default: |
| 8747 | return 0; // Cannot fold |
| 8748 | } |
| 8749 | } |
| 8750 | |
| 8751 | /// GetSelectFoldableConstant - For the same transformation as the previous |
| 8752 | /// function, return the identity constant that goes into the select. |
| 8753 | static Constant *GetSelectFoldableConstant(Instruction *I) { |
| 8754 | switch (I->getOpcode()) { |
| 8755 | default: assert(0 && "This cannot happen!"); abort(); |
| 8756 | case Instruction::Add: |
| 8757 | case Instruction::Sub: |
| 8758 | case Instruction::Or: |
| 8759 | case Instruction::Xor: |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8760 | case Instruction::Shl: |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 8761 | case Instruction::LShr: |
| 8762 | case Instruction::AShr: |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 8763 | return Constant::getNullValue(I->getType()); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8764 | case Instruction::And: |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 8765 | return Constant::getAllOnesValue(I->getType()); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8766 | case Instruction::Mul: |
| 8767 | return ConstantInt::get(I->getType(), 1); |
| 8768 | } |
| 8769 | } |
| 8770 | |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8771 | /// FoldSelectOpOp - Here we have (select c, TI, FI), and we know that TI and FI |
| 8772 | /// have the same opcode and only one use each. Try to simplify this. |
| 8773 | Instruction *InstCombiner::FoldSelectOpOp(SelectInst &SI, Instruction *TI, |
| 8774 | Instruction *FI) { |
| 8775 | if (TI->getNumOperands() == 1) { |
| 8776 | // If this is a non-volatile load or a cast from the same type, |
| 8777 | // merge. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8778 | if (TI->isCast()) { |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8779 | if (TI->getOperand(0)->getType() != FI->getOperand(0)->getType()) |
| 8780 | return 0; |
| 8781 | } else { |
| 8782 | return 0; // unknown unary op. |
| 8783 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8784 | |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8785 | // Fold this by inserting a select from the input values. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 8786 | SelectInst *NewSI = SelectInst::Create(SI.getCondition(), TI->getOperand(0), |
| 8787 | FI->getOperand(0), SI.getName()+".v"); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8788 | InsertNewInstBefore(NewSI, SI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8789 | return CastInst::Create(Instruction::CastOps(TI->getOpcode()), NewSI, |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8790 | TI->getType()); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8791 | } |
| 8792 | |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 8793 | // Only handle binary operators here. |
| 8794 | if (!isa<BinaryOperator>(TI)) |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8795 | return 0; |
| 8796 | |
| 8797 | // Figure out if the operations have any operands in common. |
| 8798 | Value *MatchOp, *OtherOpT, *OtherOpF; |
| 8799 | bool MatchIsOpZero; |
| 8800 | if (TI->getOperand(0) == FI->getOperand(0)) { |
| 8801 | MatchOp = TI->getOperand(0); |
| 8802 | OtherOpT = TI->getOperand(1); |
| 8803 | OtherOpF = FI->getOperand(1); |
| 8804 | MatchIsOpZero = true; |
| 8805 | } else if (TI->getOperand(1) == FI->getOperand(1)) { |
| 8806 | MatchOp = TI->getOperand(1); |
| 8807 | OtherOpT = TI->getOperand(0); |
| 8808 | OtherOpF = FI->getOperand(0); |
| 8809 | MatchIsOpZero = false; |
| 8810 | } else if (!TI->isCommutative()) { |
| 8811 | return 0; |
| 8812 | } else if (TI->getOperand(0) == FI->getOperand(1)) { |
| 8813 | MatchOp = TI->getOperand(0); |
| 8814 | OtherOpT = TI->getOperand(1); |
| 8815 | OtherOpF = FI->getOperand(0); |
| 8816 | MatchIsOpZero = true; |
| 8817 | } else if (TI->getOperand(1) == FI->getOperand(0)) { |
| 8818 | MatchOp = TI->getOperand(1); |
| 8819 | OtherOpT = TI->getOperand(0); |
| 8820 | OtherOpF = FI->getOperand(1); |
| 8821 | MatchIsOpZero = true; |
| 8822 | } else { |
| 8823 | return 0; |
| 8824 | } |
| 8825 | |
| 8826 | // If we reach here, they do have operations in common. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 8827 | SelectInst *NewSI = SelectInst::Create(SI.getCondition(), OtherOpT, |
| 8828 | OtherOpF, SI.getName()+".v"); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8829 | InsertNewInstBefore(NewSI, SI); |
| 8830 | |
| 8831 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TI)) { |
| 8832 | if (MatchIsOpZero) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8833 | return BinaryOperator::Create(BO->getOpcode(), MatchOp, NewSI); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8834 | else |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8835 | return BinaryOperator::Create(BO->getOpcode(), NewSI, MatchOp); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8836 | } |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 8837 | assert(0 && "Shouldn't get here"); |
| 8838 | return 0; |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8839 | } |
| 8840 | |
Evan Cheng | de62192 | 2009-03-31 20:42:45 +0000 | [diff] [blame] | 8841 | static bool isSelect01(Constant *C1, Constant *C2) { |
| 8842 | ConstantInt *C1I = dyn_cast<ConstantInt>(C1); |
| 8843 | if (!C1I) |
| 8844 | return false; |
| 8845 | ConstantInt *C2I = dyn_cast<ConstantInt>(C2); |
| 8846 | if (!C2I) |
| 8847 | return false; |
| 8848 | return (C1I->isZero() || C1I->isOne()) && (C2I->isZero() || C2I->isOne()); |
| 8849 | } |
| 8850 | |
| 8851 | /// FoldSelectIntoOp - Try fold the select into one of the operands to |
| 8852 | /// facilitate further optimization. |
| 8853 | Instruction *InstCombiner::FoldSelectIntoOp(SelectInst &SI, Value *TrueVal, |
| 8854 | Value *FalseVal) { |
| 8855 | // See the comment above GetSelectFoldableOperands for a description of the |
| 8856 | // transformation we are doing here. |
| 8857 | if (Instruction *TVI = dyn_cast<Instruction>(TrueVal)) { |
| 8858 | if (TVI->hasOneUse() && TVI->getNumOperands() == 2 && |
| 8859 | !isa<Constant>(FalseVal)) { |
| 8860 | if (unsigned SFO = GetSelectFoldableOperands(TVI)) { |
| 8861 | unsigned OpToFold = 0; |
| 8862 | if ((SFO & 1) && FalseVal == TVI->getOperand(0)) { |
| 8863 | OpToFold = 1; |
| 8864 | } else if ((SFO & 2) && FalseVal == TVI->getOperand(1)) { |
| 8865 | OpToFold = 2; |
| 8866 | } |
| 8867 | |
| 8868 | if (OpToFold) { |
| 8869 | Constant *C = GetSelectFoldableConstant(TVI); |
| 8870 | Value *OOp = TVI->getOperand(2-OpToFold); |
| 8871 | // Avoid creating select between 2 constants unless it's selecting |
| 8872 | // between 0 and 1. |
| 8873 | if (!isa<Constant>(OOp) || isSelect01(C, cast<Constant>(OOp))) { |
| 8874 | Instruction *NewSel = SelectInst::Create(SI.getCondition(), OOp, C); |
| 8875 | InsertNewInstBefore(NewSel, SI); |
| 8876 | NewSel->takeName(TVI); |
| 8877 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TVI)) |
| 8878 | return BinaryOperator::Create(BO->getOpcode(), FalseVal, NewSel); |
| 8879 | assert(0 && "Unknown instruction!!"); |
| 8880 | } |
| 8881 | } |
| 8882 | } |
| 8883 | } |
| 8884 | } |
| 8885 | |
| 8886 | if (Instruction *FVI = dyn_cast<Instruction>(FalseVal)) { |
| 8887 | if (FVI->hasOneUse() && FVI->getNumOperands() == 2 && |
| 8888 | !isa<Constant>(TrueVal)) { |
| 8889 | if (unsigned SFO = GetSelectFoldableOperands(FVI)) { |
| 8890 | unsigned OpToFold = 0; |
| 8891 | if ((SFO & 1) && TrueVal == FVI->getOperand(0)) { |
| 8892 | OpToFold = 1; |
| 8893 | } else if ((SFO & 2) && TrueVal == FVI->getOperand(1)) { |
| 8894 | OpToFold = 2; |
| 8895 | } |
| 8896 | |
| 8897 | if (OpToFold) { |
| 8898 | Constant *C = GetSelectFoldableConstant(FVI); |
| 8899 | Value *OOp = FVI->getOperand(2-OpToFold); |
| 8900 | // Avoid creating select between 2 constants unless it's selecting |
| 8901 | // between 0 and 1. |
| 8902 | if (!isa<Constant>(OOp) || isSelect01(C, cast<Constant>(OOp))) { |
| 8903 | Instruction *NewSel = SelectInst::Create(SI.getCondition(), C, OOp); |
| 8904 | InsertNewInstBefore(NewSel, SI); |
| 8905 | NewSel->takeName(FVI); |
| 8906 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(FVI)) |
| 8907 | return BinaryOperator::Create(BO->getOpcode(), TrueVal, NewSel); |
| 8908 | assert(0 && "Unknown instruction!!"); |
| 8909 | } |
| 8910 | } |
| 8911 | } |
| 8912 | } |
| 8913 | } |
| 8914 | |
| 8915 | return 0; |
| 8916 | } |
| 8917 | |
Dan Gohman | 81b28ce | 2008-09-16 18:46:06 +0000 | [diff] [blame] | 8918 | /// visitSelectInstWithICmp - Visit a SelectInst that has an |
| 8919 | /// ICmpInst as its first operand. |
| 8920 | /// |
| 8921 | Instruction *InstCombiner::visitSelectInstWithICmp(SelectInst &SI, |
| 8922 | ICmpInst *ICI) { |
| 8923 | bool Changed = false; |
| 8924 | ICmpInst::Predicate Pred = ICI->getPredicate(); |
| 8925 | Value *CmpLHS = ICI->getOperand(0); |
| 8926 | Value *CmpRHS = ICI->getOperand(1); |
| 8927 | Value *TrueVal = SI.getTrueValue(); |
| 8928 | Value *FalseVal = SI.getFalseValue(); |
| 8929 | |
| 8930 | // Check cases where the comparison is with a constant that |
| 8931 | // can be adjusted to fit the min/max idiom. We may edit ICI in |
| 8932 | // place here, so make sure the select is the only user. |
| 8933 | if (ICI->hasOneUse()) |
Dan Gohman | 1975d03 | 2008-10-30 20:40:10 +0000 | [diff] [blame] | 8934 | if (ConstantInt *CI = dyn_cast<ConstantInt>(CmpRHS)) { |
Dan Gohman | 81b28ce | 2008-09-16 18:46:06 +0000 | [diff] [blame] | 8935 | switch (Pred) { |
| 8936 | default: break; |
| 8937 | case ICmpInst::ICMP_ULT: |
| 8938 | case ICmpInst::ICMP_SLT: { |
| 8939 | // X < MIN ? T : F --> F |
| 8940 | if (CI->isMinValue(Pred == ICmpInst::ICMP_SLT)) |
| 8941 | return ReplaceInstUsesWith(SI, FalseVal); |
| 8942 | // X < C ? X : C-1 --> X > C-1 ? C-1 : X |
| 8943 | Constant *AdjustedRHS = SubOne(CI); |
| 8944 | if ((CmpLHS == TrueVal && AdjustedRHS == FalseVal) || |
| 8945 | (CmpLHS == FalseVal && AdjustedRHS == TrueVal)) { |
| 8946 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 8947 | CmpRHS = AdjustedRHS; |
| 8948 | std::swap(FalseVal, TrueVal); |
| 8949 | ICI->setPredicate(Pred); |
| 8950 | ICI->setOperand(1, CmpRHS); |
| 8951 | SI.setOperand(1, TrueVal); |
| 8952 | SI.setOperand(2, FalseVal); |
| 8953 | Changed = true; |
| 8954 | } |
| 8955 | break; |
| 8956 | } |
| 8957 | case ICmpInst::ICMP_UGT: |
| 8958 | case ICmpInst::ICMP_SGT: { |
| 8959 | // X > MAX ? T : F --> F |
| 8960 | if (CI->isMaxValue(Pred == ICmpInst::ICMP_SGT)) |
| 8961 | return ReplaceInstUsesWith(SI, FalseVal); |
| 8962 | // X > C ? X : C+1 --> X < C+1 ? C+1 : X |
| 8963 | Constant *AdjustedRHS = AddOne(CI); |
| 8964 | if ((CmpLHS == TrueVal && AdjustedRHS == FalseVal) || |
| 8965 | (CmpLHS == FalseVal && AdjustedRHS == TrueVal)) { |
| 8966 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 8967 | CmpRHS = AdjustedRHS; |
| 8968 | std::swap(FalseVal, TrueVal); |
| 8969 | ICI->setPredicate(Pred); |
| 8970 | ICI->setOperand(1, CmpRHS); |
| 8971 | SI.setOperand(1, TrueVal); |
| 8972 | SI.setOperand(2, FalseVal); |
| 8973 | Changed = true; |
| 8974 | } |
| 8975 | break; |
| 8976 | } |
| 8977 | } |
| 8978 | |
Dan Gohman | 1975d03 | 2008-10-30 20:40:10 +0000 | [diff] [blame] | 8979 | // (x <s 0) ? -1 : 0 -> ashr x, 31 -> all ones if signed |
| 8980 | // (x >s -1) ? -1 : 0 -> ashr x, 31 -> all ones if not signed |
Chris Lattner | cb504b9 | 2008-11-16 05:38:51 +0000 | [diff] [blame] | 8981 | CmpInst::Predicate Pred = CmpInst::BAD_ICMP_PREDICATE; |
Chris Lattner | 159c35b | 2009-01-05 23:53:12 +0000 | [diff] [blame] | 8982 | if (match(TrueVal, m_ConstantInt<-1>()) && |
| 8983 | match(FalseVal, m_ConstantInt<0>())) |
Chris Lattner | cb504b9 | 2008-11-16 05:38:51 +0000 | [diff] [blame] | 8984 | Pred = ICI->getPredicate(); |
Chris Lattner | 159c35b | 2009-01-05 23:53:12 +0000 | [diff] [blame] | 8985 | else if (match(TrueVal, m_ConstantInt<0>()) && |
| 8986 | match(FalseVal, m_ConstantInt<-1>())) |
Chris Lattner | cb504b9 | 2008-11-16 05:38:51 +0000 | [diff] [blame] | 8987 | Pred = CmpInst::getInversePredicate(ICI->getPredicate()); |
| 8988 | |
Dan Gohman | 1975d03 | 2008-10-30 20:40:10 +0000 | [diff] [blame] | 8989 | if (Pred != CmpInst::BAD_ICMP_PREDICATE) { |
| 8990 | // If we are just checking for a icmp eq of a single bit and zext'ing it |
| 8991 | // to an integer, then shift the bit to the appropriate place and then |
| 8992 | // cast to integer to avoid the comparison. |
| 8993 | const APInt &Op1CV = CI->getValue(); |
| 8994 | |
| 8995 | // sext (x <s 0) to i32 --> x>>s31 true if signbit set. |
| 8996 | // sext (x >s -1) to i32 --> (x>>s31)^-1 true if signbit clear. |
| 8997 | if ((Pred == ICmpInst::ICMP_SLT && Op1CV == 0) || |
Chris Lattner | cb504b9 | 2008-11-16 05:38:51 +0000 | [diff] [blame] | 8998 | (Pred == ICmpInst::ICMP_SGT && Op1CV.isAllOnesValue())) { |
Dan Gohman | 1975d03 | 2008-10-30 20:40:10 +0000 | [diff] [blame] | 8999 | Value *In = ICI->getOperand(0); |
| 9000 | Value *Sh = ConstantInt::get(In->getType(), |
| 9001 | In->getType()->getPrimitiveSizeInBits()-1); |
| 9002 | In = InsertNewInstBefore(BinaryOperator::CreateAShr(In, Sh, |
| 9003 | In->getName()+".lobit"), |
| 9004 | *ICI); |
Dan Gohman | 21440ac | 2008-11-02 00:17:33 +0000 | [diff] [blame] | 9005 | if (In->getType() != SI.getType()) |
| 9006 | In = CastInst::CreateIntegerCast(In, SI.getType(), |
Dan Gohman | 1975d03 | 2008-10-30 20:40:10 +0000 | [diff] [blame] | 9007 | true/*SExt*/, "tmp", ICI); |
| 9008 | |
| 9009 | if (Pred == ICmpInst::ICMP_SGT) |
| 9010 | In = InsertNewInstBefore(BinaryOperator::CreateNot(In, |
| 9011 | In->getName()+".not"), *ICI); |
| 9012 | |
| 9013 | return ReplaceInstUsesWith(SI, In); |
| 9014 | } |
| 9015 | } |
| 9016 | } |
| 9017 | |
Dan Gohman | 81b28ce | 2008-09-16 18:46:06 +0000 | [diff] [blame] | 9018 | if (CmpLHS == TrueVal && CmpRHS == FalseVal) { |
| 9019 | // Transform (X == Y) ? X : Y -> Y |
| 9020 | if (Pred == ICmpInst::ICMP_EQ) |
| 9021 | return ReplaceInstUsesWith(SI, FalseVal); |
| 9022 | // Transform (X != Y) ? X : Y -> X |
| 9023 | if (Pred == ICmpInst::ICMP_NE) |
| 9024 | return ReplaceInstUsesWith(SI, TrueVal); |
| 9025 | /// NOTE: if we wanted to, this is where to detect integer MIN/MAX |
| 9026 | |
| 9027 | } else if (CmpLHS == FalseVal && CmpRHS == TrueVal) { |
| 9028 | // Transform (X == Y) ? Y : X -> X |
| 9029 | if (Pred == ICmpInst::ICMP_EQ) |
| 9030 | return ReplaceInstUsesWith(SI, FalseVal); |
| 9031 | // Transform (X != Y) ? Y : X -> Y |
| 9032 | if (Pred == ICmpInst::ICMP_NE) |
| 9033 | return ReplaceInstUsesWith(SI, TrueVal); |
| 9034 | /// NOTE: if we wanted to, this is where to detect integer MIN/MAX |
| 9035 | } |
| 9036 | |
| 9037 | /// NOTE: if we wanted to, this is where to detect integer ABS |
| 9038 | |
| 9039 | return Changed ? &SI : 0; |
| 9040 | } |
| 9041 | |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 9042 | Instruction *InstCombiner::visitSelectInst(SelectInst &SI) { |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 9043 | Value *CondVal = SI.getCondition(); |
| 9044 | Value *TrueVal = SI.getTrueValue(); |
| 9045 | Value *FalseVal = SI.getFalseValue(); |
| 9046 | |
| 9047 | // select true, X, Y -> X |
| 9048 | // select false, X, Y -> Y |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 9049 | if (ConstantInt *C = dyn_cast<ConstantInt>(CondVal)) |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 9050 | return ReplaceInstUsesWith(SI, C->getZExtValue() ? TrueVal : FalseVal); |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 9051 | |
| 9052 | // select C, X, X -> X |
| 9053 | if (TrueVal == FalseVal) |
| 9054 | return ReplaceInstUsesWith(SI, TrueVal); |
| 9055 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 9056 | if (isa<UndefValue>(TrueVal)) // select C, undef, X -> X |
| 9057 | return ReplaceInstUsesWith(SI, FalseVal); |
| 9058 | if (isa<UndefValue>(FalseVal)) // select C, X, undef -> X |
| 9059 | return ReplaceInstUsesWith(SI, TrueVal); |
| 9060 | if (isa<UndefValue>(CondVal)) { // select undef, X, Y -> X or Y |
| 9061 | if (isa<Constant>(TrueVal)) |
| 9062 | return ReplaceInstUsesWith(SI, TrueVal); |
| 9063 | else |
| 9064 | return ReplaceInstUsesWith(SI, FalseVal); |
| 9065 | } |
| 9066 | |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 9067 | if (SI.getType() == Type::Int1Ty) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 9068 | if (ConstantInt *C = dyn_cast<ConstantInt>(TrueVal)) { |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 9069 | if (C->getZExtValue()) { |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 9070 | // Change: A = select B, true, C --> A = or B, C |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9071 | return BinaryOperator::CreateOr(CondVal, FalseVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 9072 | } else { |
| 9073 | // Change: A = select B, false, C --> A = and !B, C |
| 9074 | Value *NotCond = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9075 | InsertNewInstBefore(BinaryOperator::CreateNot(CondVal, |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 9076 | "not."+CondVal->getName()), SI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9077 | return BinaryOperator::CreateAnd(NotCond, FalseVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 9078 | } |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 9079 | } else if (ConstantInt *C = dyn_cast<ConstantInt>(FalseVal)) { |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 9080 | if (C->getZExtValue() == false) { |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 9081 | // Change: A = select B, C, false --> A = and B, C |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9082 | return BinaryOperator::CreateAnd(CondVal, TrueVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 9083 | } else { |
| 9084 | // Change: A = select B, C, true --> A = or !B, C |
| 9085 | Value *NotCond = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9086 | InsertNewInstBefore(BinaryOperator::CreateNot(CondVal, |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 9087 | "not."+CondVal->getName()), SI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9088 | return BinaryOperator::CreateOr(NotCond, TrueVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 9089 | } |
| 9090 | } |
Chris Lattner | cfa5975 | 2007-11-25 21:27:53 +0000 | [diff] [blame] | 9091 | |
| 9092 | // select a, b, a -> a&b |
| 9093 | // select a, a, b -> a|b |
| 9094 | if (CondVal == TrueVal) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9095 | return BinaryOperator::CreateOr(CondVal, FalseVal); |
Chris Lattner | cfa5975 | 2007-11-25 21:27:53 +0000 | [diff] [blame] | 9096 | else if (CondVal == FalseVal) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9097 | return BinaryOperator::CreateAnd(CondVal, TrueVal); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 9098 | } |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 9099 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 9100 | // Selecting between two integer constants? |
| 9101 | if (ConstantInt *TrueValC = dyn_cast<ConstantInt>(TrueVal)) |
| 9102 | if (ConstantInt *FalseValC = dyn_cast<ConstantInt>(FalseVal)) { |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 9103 | // select C, 1, 0 -> zext C to int |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 9104 | if (FalseValC->isZero() && TrueValC->getValue() == 1) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9105 | return CastInst::Create(Instruction::ZExt, CondVal, SI.getType()); |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 9106 | } else if (TrueValC->isZero() && FalseValC->getValue() == 1) { |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 9107 | // select C, 0, 1 -> zext !C to int |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 9108 | Value *NotCond = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9109 | InsertNewInstBefore(BinaryOperator::CreateNot(CondVal, |
Chris Lattner | 82e14fe | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 9110 | "not."+CondVal->getName()), SI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9111 | return CastInst::Create(Instruction::ZExt, NotCond, SI.getType()); |
Chris Lattner | 82e14fe | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 9112 | } |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 9113 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9114 | if (ICmpInst *IC = dyn_cast<ICmpInst>(SI.getCondition())) { |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 9115 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9116 | // (x <s 0) ? -1 : 0 -> ashr x, 31 |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 9117 | if (TrueValC->isAllOnesValue() && FalseValC->isZero()) |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 9118 | if (ConstantInt *CmpCst = dyn_cast<ConstantInt>(IC->getOperand(1))) { |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 9119 | if (IC->getPredicate() == ICmpInst::ICMP_SLT && CmpCst->isZero()) { |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 9120 | // The comparison constant and the result are not neccessarily the |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9121 | // same width. Make an all-ones value by inserting a AShr. |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 9122 | Value *X = IC->getOperand(0); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 9123 | uint32_t Bits = X->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 9124 | Constant *ShAmt = ConstantInt::get(X->getType(), Bits-1); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9125 | Instruction *SRA = BinaryOperator::Create(Instruction::AShr, X, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 9126 | ShAmt, "ones"); |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 9127 | InsertNewInstBefore(SRA, SI); |
Eli Friedman | d1fd1da | 2008-11-30 21:09:11 +0000 | [diff] [blame] | 9128 | |
| 9129 | // Then cast to the appropriate width. |
| 9130 | return CastInst::CreateIntegerCast(SRA, SI.getType(), true); |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 9131 | } |
| 9132 | } |
| 9133 | |
| 9134 | |
| 9135 | // 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] | 9136 | // 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] | 9137 | // non-constant value, eliminate this whole mess. This corresponds to |
| 9138 | // cases like this: ((X & 27) ? 27 : 0) |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 9139 | if (TrueValC->isZero() || FalseValC->isZero()) |
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 9140 | if (IC->isEquality() && isa<ConstantInt>(IC->getOperand(1)) && |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 9141 | cast<Constant>(IC->getOperand(1))->isNullValue()) |
| 9142 | if (Instruction *ICA = dyn_cast<Instruction>(IC->getOperand(0))) |
| 9143 | if (ICA->getOpcode() == Instruction::And && |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9144 | isa<ConstantInt>(ICA->getOperand(1)) && |
| 9145 | (ICA->getOperand(1) == TrueValC || |
| 9146 | ICA->getOperand(1) == FalseValC) && |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 9147 | isOneBitSet(cast<ConstantInt>(ICA->getOperand(1)))) { |
| 9148 | // 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] | 9149 | // know whether we have a icmp_ne or icmp_eq and whether the |
| 9150 | // true or false val is the zero. |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 9151 | bool ShouldNotVal = !TrueValC->isZero(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9152 | ShouldNotVal ^= IC->getPredicate() == ICmpInst::ICMP_NE; |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 9153 | Value *V = ICA; |
| 9154 | if (ShouldNotVal) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9155 | V = InsertNewInstBefore(BinaryOperator::Create( |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 9156 | Instruction::Xor, V, ICA->getOperand(1)), SI); |
| 9157 | return ReplaceInstUsesWith(SI, V); |
| 9158 | } |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 9159 | } |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 9160 | } |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 9161 | |
| 9162 | // 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] | 9163 | if (FCmpInst *FCI = dyn_cast<FCmpInst>(CondVal)) { |
| 9164 | if (FCI->getOperand(0) == TrueVal && FCI->getOperand(1) == FalseVal) { |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 9165 | // Transform (X == Y) ? X : Y -> Y |
Dale Johannesen | 5a2174f | 2007-10-03 17:45:27 +0000 | [diff] [blame] | 9166 | if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) { |
| 9167 | // This is not safe in general for floating point: |
| 9168 | // consider X== -0, Y== +0. |
| 9169 | // It becomes safe if either operand is a nonzero constant. |
| 9170 | ConstantFP *CFPt, *CFPf; |
| 9171 | if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) && |
| 9172 | !CFPt->getValueAPF().isZero()) || |
| 9173 | ((CFPf = dyn_cast<ConstantFP>(FalseVal)) && |
| 9174 | !CFPf->getValueAPF().isZero())) |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 9175 | return ReplaceInstUsesWith(SI, FalseVal); |
Dale Johannesen | 5a2174f | 2007-10-03 17:45:27 +0000 | [diff] [blame] | 9176 | } |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 9177 | // Transform (X != Y) ? X : Y -> X |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9178 | if (FCI->getPredicate() == FCmpInst::FCMP_ONE) |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 9179 | return ReplaceInstUsesWith(SI, TrueVal); |
Dan Gohman | 81b28ce | 2008-09-16 18:46:06 +0000 | [diff] [blame] | 9180 | // NOTE: if we wanted to, this is where to detect MIN/MAX |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 9181 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9182 | } else if (FCI->getOperand(0) == FalseVal && FCI->getOperand(1) == TrueVal){ |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 9183 | // Transform (X == Y) ? Y : X -> X |
Dale Johannesen | 5a2174f | 2007-10-03 17:45:27 +0000 | [diff] [blame] | 9184 | if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) { |
| 9185 | // This is not safe in general for floating point: |
| 9186 | // consider X== -0, Y== +0. |
| 9187 | // It becomes safe if either operand is a nonzero constant. |
| 9188 | ConstantFP *CFPt, *CFPf; |
| 9189 | if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) && |
| 9190 | !CFPt->getValueAPF().isZero()) || |
| 9191 | ((CFPf = dyn_cast<ConstantFP>(FalseVal)) && |
| 9192 | !CFPf->getValueAPF().isZero())) |
| 9193 | return ReplaceInstUsesWith(SI, FalseVal); |
| 9194 | } |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 9195 | // Transform (X != Y) ? Y : X -> Y |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9196 | if (FCI->getPredicate() == FCmpInst::FCMP_ONE) |
| 9197 | return ReplaceInstUsesWith(SI, TrueVal); |
Dan Gohman | 81b28ce | 2008-09-16 18:46:06 +0000 | [diff] [blame] | 9198 | // NOTE: if we wanted to, this is where to detect MIN/MAX |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9199 | } |
Dan Gohman | 81b28ce | 2008-09-16 18:46:06 +0000 | [diff] [blame] | 9200 | // NOTE: if we wanted to, this is where to detect ABS |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9201 | } |
| 9202 | |
| 9203 | // See if we are selecting two values based on a comparison of the two values. |
Dan Gohman | 81b28ce | 2008-09-16 18:46:06 +0000 | [diff] [blame] | 9204 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(CondVal)) |
| 9205 | if (Instruction *Result = visitSelectInstWithICmp(SI, ICI)) |
| 9206 | return Result; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9207 | |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 9208 | if (Instruction *TI = dyn_cast<Instruction>(TrueVal)) |
| 9209 | if (Instruction *FI = dyn_cast<Instruction>(FalseVal)) |
| 9210 | if (TI->hasOneUse() && FI->hasOneUse()) { |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 9211 | Instruction *AddOp = 0, *SubOp = 0; |
| 9212 | |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 9213 | // Turn (select C, (op X, Y), (op X, Z)) -> (op X, (select C, Y, Z)) |
| 9214 | if (TI->getOpcode() == FI->getOpcode()) |
| 9215 | if (Instruction *IV = FoldSelectOpOp(SI, TI, FI)) |
| 9216 | return IV; |
| 9217 | |
| 9218 | // Turn select C, (X+Y), (X-Y) --> (X+(select C, Y, (-Y))). This is |
| 9219 | // even legal for FP. |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 9220 | if (TI->getOpcode() == Instruction::Sub && |
| 9221 | FI->getOpcode() == Instruction::Add) { |
| 9222 | AddOp = FI; SubOp = TI; |
| 9223 | } else if (FI->getOpcode() == Instruction::Sub && |
| 9224 | TI->getOpcode() == Instruction::Add) { |
| 9225 | AddOp = TI; SubOp = FI; |
| 9226 | } |
| 9227 | |
| 9228 | if (AddOp) { |
| 9229 | Value *OtherAddOp = 0; |
| 9230 | if (SubOp->getOperand(0) == AddOp->getOperand(0)) { |
| 9231 | OtherAddOp = AddOp->getOperand(1); |
| 9232 | } else if (SubOp->getOperand(0) == AddOp->getOperand(1)) { |
| 9233 | OtherAddOp = AddOp->getOperand(0); |
| 9234 | } |
| 9235 | |
| 9236 | if (OtherAddOp) { |
Chris Lattner | 97f37a4 | 2006-02-24 18:05:58 +0000 | [diff] [blame] | 9237 | // So at this point we know we have (Y -> OtherAddOp): |
| 9238 | // select C, (add X, Y), (sub X, Z) |
| 9239 | Value *NegVal; // Compute -Z |
| 9240 | if (Constant *C = dyn_cast<Constant>(SubOp->getOperand(1))) { |
| 9241 | NegVal = ConstantExpr::getNeg(C); |
| 9242 | } else { |
| 9243 | NegVal = InsertNewInstBefore( |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9244 | BinaryOperator::CreateNeg(SubOp->getOperand(1), "tmp"), SI); |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 9245 | } |
Chris Lattner | 97f37a4 | 2006-02-24 18:05:58 +0000 | [diff] [blame] | 9246 | |
| 9247 | Value *NewTrueOp = OtherAddOp; |
| 9248 | Value *NewFalseOp = NegVal; |
| 9249 | if (AddOp != TI) |
| 9250 | std::swap(NewTrueOp, NewFalseOp); |
| 9251 | Instruction *NewSel = |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 9252 | SelectInst::Create(CondVal, NewTrueOp, |
| 9253 | NewFalseOp, SI.getName() + ".p"); |
Chris Lattner | 97f37a4 | 2006-02-24 18:05:58 +0000 | [diff] [blame] | 9254 | |
| 9255 | NewSel = InsertNewInstBefore(NewSel, SI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9256 | return BinaryOperator::CreateAdd(SubOp->getOperand(0), NewSel); |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 9257 | } |
| 9258 | } |
| 9259 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9260 | |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 9261 | // See if we can fold the select into one of our operands. |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 9262 | if (SI.getType()->isInteger()) { |
Evan Cheng | de62192 | 2009-03-31 20:42:45 +0000 | [diff] [blame] | 9263 | Instruction *FoldI = FoldSelectIntoOp(SI, TrueVal, FalseVal); |
| 9264 | if (FoldI) |
| 9265 | return FoldI; |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 9266 | } |
Chris Lattner | a1df33c | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 9267 | |
| 9268 | if (BinaryOperator::isNot(CondVal)) { |
| 9269 | SI.setOperand(0, BinaryOperator::getNotArgument(CondVal)); |
| 9270 | SI.setOperand(1, FalseVal); |
| 9271 | SI.setOperand(2, TrueVal); |
| 9272 | return &SI; |
| 9273 | } |
| 9274 | |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 9275 | return 0; |
| 9276 | } |
| 9277 | |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 9278 | /// EnforceKnownAlignment - If the specified pointer points to an object that |
| 9279 | /// we control, modify the object's alignment to PrefAlign. This isn't |
| 9280 | /// often possible though. If alignment is important, a more reliable approach |
| 9281 | /// is to simply align all global variables and allocation instructions to |
| 9282 | /// their preferred alignment from the beginning. |
| 9283 | /// |
| 9284 | static unsigned EnforceKnownAlignment(Value *V, |
| 9285 | unsigned Align, unsigned PrefAlign) { |
Chris Lattner | f2369f2 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 9286 | |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 9287 | User *U = dyn_cast<User>(V); |
| 9288 | if (!U) return Align; |
| 9289 | |
| 9290 | switch (getOpcode(U)) { |
| 9291 | default: break; |
| 9292 | case Instruction::BitCast: |
| 9293 | return EnforceKnownAlignment(U->getOperand(0), Align, PrefAlign); |
| 9294 | case Instruction::GetElementPtr: { |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 9295 | // If all indexes are zero, it is just the alignment of the base pointer. |
| 9296 | bool AllZeroOperands = true; |
Gabor Greif | 52ed363 | 2008-06-12 21:51:29 +0000 | [diff] [blame] | 9297 | 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] | 9298 | if (!isa<Constant>(*i) || |
| 9299 | !cast<Constant>(*i)->isNullValue()) { |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 9300 | AllZeroOperands = false; |
| 9301 | break; |
| 9302 | } |
Chris Lattner | f2369f2 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 9303 | |
| 9304 | if (AllZeroOperands) { |
| 9305 | // Treat this like a bitcast. |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 9306 | return EnforceKnownAlignment(U->getOperand(0), Align, PrefAlign); |
Chris Lattner | f2369f2 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 9307 | } |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 9308 | break; |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 9309 | } |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 9310 | } |
| 9311 | |
| 9312 | if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
| 9313 | // If there is a large requested alignment and we can, bump up the alignment |
| 9314 | // of the global. |
| 9315 | if (!GV->isDeclaration()) { |
Dan Gohman | ecd0fb5 | 2009-02-16 23:02:21 +0000 | [diff] [blame] | 9316 | if (GV->getAlignment() >= PrefAlign) |
| 9317 | Align = GV->getAlignment(); |
| 9318 | else { |
| 9319 | GV->setAlignment(PrefAlign); |
| 9320 | Align = PrefAlign; |
| 9321 | } |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 9322 | } |
| 9323 | } else if (AllocationInst *AI = dyn_cast<AllocationInst>(V)) { |
| 9324 | // If there is a requested alignment and if this is an alloca, round up. We |
| 9325 | // don't do this for malloc, because some systems can't respect the request. |
| 9326 | if (isa<AllocaInst>(AI)) { |
Dan Gohman | ecd0fb5 | 2009-02-16 23:02:21 +0000 | [diff] [blame] | 9327 | if (AI->getAlignment() >= PrefAlign) |
| 9328 | Align = AI->getAlignment(); |
| 9329 | else { |
| 9330 | AI->setAlignment(PrefAlign); |
| 9331 | Align = PrefAlign; |
| 9332 | } |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 9333 | } |
| 9334 | } |
| 9335 | |
| 9336 | return Align; |
| 9337 | } |
| 9338 | |
| 9339 | /// GetOrEnforceKnownAlignment - If the specified pointer has an alignment that |
| 9340 | /// we can determine, return it, otherwise return 0. If PrefAlign is specified, |
| 9341 | /// and it is more than the alignment of the ultimate object, see if we can |
| 9342 | /// increase the alignment of the ultimate object, making this check succeed. |
| 9343 | unsigned InstCombiner::GetOrEnforceKnownAlignment(Value *V, |
| 9344 | unsigned PrefAlign) { |
| 9345 | unsigned BitWidth = TD ? TD->getTypeSizeInBits(V->getType()) : |
| 9346 | sizeof(PrefAlign) * CHAR_BIT; |
| 9347 | APInt Mask = APInt::getAllOnesValue(BitWidth); |
| 9348 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 9349 | ComputeMaskedBits(V, Mask, KnownZero, KnownOne); |
| 9350 | unsigned TrailZ = KnownZero.countTrailingOnes(); |
| 9351 | unsigned Align = 1u << std::min(BitWidth - 1, TrailZ); |
| 9352 | |
| 9353 | if (PrefAlign > Align) |
| 9354 | Align = EnforceKnownAlignment(V, Align, PrefAlign); |
| 9355 | |
| 9356 | // We don't need to make any adjustment. |
| 9357 | return Align; |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 9358 | } |
| 9359 | |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 9360 | Instruction *InstCombiner::SimplifyMemTransfer(MemIntrinsic *MI) { |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 9361 | unsigned DstAlign = GetOrEnforceKnownAlignment(MI->getOperand(1)); |
Dan Gohman | bc989d4 | 2009-02-22 18:06:32 +0000 | [diff] [blame] | 9362 | unsigned SrcAlign = GetOrEnforceKnownAlignment(MI->getOperand(2)); |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 9363 | unsigned MinAlign = std::min(DstAlign, SrcAlign); |
Chris Lattner | dfe964c | 2009-03-08 03:59:00 +0000 | [diff] [blame] | 9364 | unsigned CopyAlign = MI->getAlignment(); |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 9365 | |
| 9366 | if (CopyAlign < MinAlign) { |
Chris Lattner | dfe964c | 2009-03-08 03:59:00 +0000 | [diff] [blame] | 9367 | MI->setAlignment(MinAlign); |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 9368 | return MI; |
| 9369 | } |
| 9370 | |
| 9371 | // If MemCpyInst length is 1/2/4/8 bytes then replace memcpy with |
| 9372 | // load/store. |
| 9373 | ConstantInt *MemOpLength = dyn_cast<ConstantInt>(MI->getOperand(3)); |
| 9374 | if (MemOpLength == 0) return 0; |
| 9375 | |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 9376 | // Source and destination pointer types are always "i8*" for intrinsic. See |
| 9377 | // if the size is something we can handle with a single primitive load/store. |
| 9378 | // A single load+store correctly handles overlapping memory in the memmove |
| 9379 | // case. |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 9380 | unsigned Size = MemOpLength->getZExtValue(); |
Chris Lattner | 69ea9d2 | 2008-04-30 06:39:11 +0000 | [diff] [blame] | 9381 | if (Size == 0) return MI; // Delete this mem transfer. |
| 9382 | |
| 9383 | if (Size > 8 || (Size&(Size-1))) |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 9384 | return 0; // If not 1/2/4/8 bytes, exit. |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 9385 | |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 9386 | // Use an integer load+store unless we can find something better. |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 9387 | Type *NewPtrTy = PointerType::getUnqual(IntegerType::get(Size<<3)); |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 9388 | |
| 9389 | // Memcpy forces the use of i8* for the source and destination. That means |
| 9390 | // that if you're using memcpy to move one double around, you'll get a cast |
| 9391 | // from double* to i8*. We'd much rather use a double load+store rather than |
| 9392 | // an i64 load+store, here because this improves the odds that the source or |
| 9393 | // dest address will be promotable. See if we can find a better type than the |
| 9394 | // integer datatype. |
| 9395 | if (Value *Op = getBitCastOperand(MI->getOperand(1))) { |
| 9396 | const Type *SrcETy = cast<PointerType>(Op->getType())->getElementType(); |
| 9397 | if (SrcETy->isSized() && TD->getTypeStoreSize(SrcETy) == Size) { |
| 9398 | // The SrcETy might be something like {{{double}}} or [1 x double]. Rip |
| 9399 | // down through these levels if so. |
Dan Gohman | 8f8e269 | 2008-05-23 01:52:21 +0000 | [diff] [blame] | 9400 | while (!SrcETy->isSingleValueType()) { |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 9401 | if (const StructType *STy = dyn_cast<StructType>(SrcETy)) { |
| 9402 | if (STy->getNumElements() == 1) |
| 9403 | SrcETy = STy->getElementType(0); |
| 9404 | else |
| 9405 | break; |
| 9406 | } else if (const ArrayType *ATy = dyn_cast<ArrayType>(SrcETy)) { |
| 9407 | if (ATy->getNumElements() == 1) |
| 9408 | SrcETy = ATy->getElementType(); |
| 9409 | else |
| 9410 | break; |
| 9411 | } else |
| 9412 | break; |
| 9413 | } |
| 9414 | |
Dan Gohman | 8f8e269 | 2008-05-23 01:52:21 +0000 | [diff] [blame] | 9415 | if (SrcETy->isSingleValueType()) |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 9416 | NewPtrTy = PointerType::getUnqual(SrcETy); |
| 9417 | } |
| 9418 | } |
| 9419 | |
| 9420 | |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 9421 | // If the memcpy/memmove provides better alignment info than we can |
| 9422 | // infer, use it. |
| 9423 | SrcAlign = std::max(SrcAlign, CopyAlign); |
| 9424 | DstAlign = std::max(DstAlign, CopyAlign); |
| 9425 | |
| 9426 | Value *Src = InsertBitCastBefore(MI->getOperand(2), NewPtrTy, *MI); |
| 9427 | Value *Dest = InsertBitCastBefore(MI->getOperand(1), NewPtrTy, *MI); |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 9428 | Instruction *L = new LoadInst(Src, "tmp", false, SrcAlign); |
| 9429 | InsertNewInstBefore(L, *MI); |
| 9430 | InsertNewInstBefore(new StoreInst(L, Dest, false, DstAlign), *MI); |
| 9431 | |
| 9432 | // Set the size of the copy to 0, it will be deleted on the next iteration. |
| 9433 | MI->setOperand(3, Constant::getNullValue(MemOpLength->getType())); |
| 9434 | return MI; |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 9435 | } |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 9436 | |
Chris Lattner | 69ea9d2 | 2008-04-30 06:39:11 +0000 | [diff] [blame] | 9437 | Instruction *InstCombiner::SimplifyMemSet(MemSetInst *MI) { |
| 9438 | unsigned Alignment = GetOrEnforceKnownAlignment(MI->getDest()); |
Chris Lattner | dfe964c | 2009-03-08 03:59:00 +0000 | [diff] [blame] | 9439 | if (MI->getAlignment() < Alignment) { |
| 9440 | MI->setAlignment(Alignment); |
Chris Lattner | 69ea9d2 | 2008-04-30 06:39:11 +0000 | [diff] [blame] | 9441 | return MI; |
| 9442 | } |
| 9443 | |
| 9444 | // Extract the length and alignment and fill if they are constant. |
| 9445 | ConstantInt *LenC = dyn_cast<ConstantInt>(MI->getLength()); |
| 9446 | ConstantInt *FillC = dyn_cast<ConstantInt>(MI->getValue()); |
| 9447 | if (!LenC || !FillC || FillC->getType() != Type::Int8Ty) |
| 9448 | return 0; |
| 9449 | uint64_t Len = LenC->getZExtValue(); |
Chris Lattner | dfe964c | 2009-03-08 03:59:00 +0000 | [diff] [blame] | 9450 | Alignment = MI->getAlignment(); |
Chris Lattner | 69ea9d2 | 2008-04-30 06:39:11 +0000 | [diff] [blame] | 9451 | |
| 9452 | // If the length is zero, this is a no-op |
| 9453 | if (Len == 0) return MI; // memset(d,c,0,a) -> noop |
| 9454 | |
| 9455 | // memset(s,c,n) -> store s, c (for n=1,2,4,8) |
| 9456 | if (Len <= 8 && isPowerOf2_32((uint32_t)Len)) { |
| 9457 | const Type *ITy = IntegerType::get(Len*8); // n=1 -> i8. |
| 9458 | |
| 9459 | Value *Dest = MI->getDest(); |
| 9460 | Dest = InsertBitCastBefore(Dest, PointerType::getUnqual(ITy), *MI); |
| 9461 | |
| 9462 | // Alignment 0 is identity for alignment 1 for memset, but not store. |
| 9463 | if (Alignment == 0) Alignment = 1; |
| 9464 | |
| 9465 | // Extract the fill value and store. |
| 9466 | uint64_t Fill = FillC->getZExtValue()*0x0101010101010101ULL; |
| 9467 | InsertNewInstBefore(new StoreInst(ConstantInt::get(ITy, Fill), Dest, false, |
| 9468 | Alignment), *MI); |
| 9469 | |
| 9470 | // Set the size of the copy to 0, it will be deleted on the next iteration. |
| 9471 | MI->setLength(Constant::getNullValue(LenC->getType())); |
| 9472 | return MI; |
| 9473 | } |
| 9474 | |
| 9475 | return 0; |
| 9476 | } |
| 9477 | |
| 9478 | |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 9479 | /// visitCallInst - CallInst simplification. This mostly only handles folding |
| 9480 | /// of intrinsic instructions. For normal calls, it allows visitCallSite to do |
| 9481 | /// the heavy lifting. |
| 9482 | /// |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9483 | Instruction *InstCombiner::visitCallInst(CallInst &CI) { |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 9484 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(&CI); |
| 9485 | if (!II) return visitCallSite(&CI); |
| 9486 | |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 9487 | // Intrinsics cannot occur in an invoke, so handle them here instead of in |
| 9488 | // visitCallSite. |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 9489 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(II)) { |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 9490 | bool Changed = false; |
| 9491 | |
| 9492 | // memmove/cpy/set of zero bytes is a noop. |
| 9493 | if (Constant *NumBytes = dyn_cast<Constant>(MI->getLength())) { |
| 9494 | if (NumBytes->isNullValue()) return EraseInstFromFunction(CI); |
| 9495 | |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 9496 | if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes)) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 9497 | if (CI->getZExtValue() == 1) { |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 9498 | // Replace the instruction with just byte operations. We would |
| 9499 | // transform other cases to loads/stores, but we don't know if |
| 9500 | // alignment is sufficient. |
| 9501 | } |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 9502 | } |
| 9503 | |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 9504 | // If we have a memmove and the source operation is a constant global, |
| 9505 | // then the source and dest pointers can't alias, so we can change this |
| 9506 | // into a call to memcpy. |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 9507 | if (MemMoveInst *MMI = dyn_cast<MemMoveInst>(MI)) { |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 9508 | if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource())) |
| 9509 | if (GVSrc->isConstant()) { |
| 9510 | Module *M = CI.getParent()->getParent()->getParent(); |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 9511 | Intrinsic::ID MemCpyID = Intrinsic::memcpy; |
| 9512 | const Type *Tys[1]; |
| 9513 | Tys[0] = CI.getOperand(3)->getType(); |
| 9514 | CI.setOperand(0, |
| 9515 | Intrinsic::getDeclaration(M, MemCpyID, Tys, 1)); |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 9516 | Changed = true; |
| 9517 | } |
Chris Lattner | a935db8 | 2008-05-28 05:30:41 +0000 | [diff] [blame] | 9518 | |
| 9519 | // memmove(x,x,size) -> noop. |
| 9520 | if (MMI->getSource() == MMI->getDest()) |
| 9521 | return EraseInstFromFunction(CI); |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 9522 | } |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 9523 | |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 9524 | // If we can determine a pointer alignment that is bigger than currently |
| 9525 | // set, update the alignment. |
Chris Lattner | 3ce5e88 | 2009-03-08 03:37:16 +0000 | [diff] [blame] | 9526 | if (isa<MemTransferInst>(MI)) { |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 9527 | if (Instruction *I = SimplifyMemTransfer(MI)) |
| 9528 | return I; |
Chris Lattner | 69ea9d2 | 2008-04-30 06:39:11 +0000 | [diff] [blame] | 9529 | } else if (MemSetInst *MSI = dyn_cast<MemSetInst>(MI)) { |
| 9530 | if (Instruction *I = SimplifyMemSet(MSI)) |
| 9531 | return I; |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 9532 | } |
| 9533 | |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 9534 | if (Changed) return II; |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 9535 | } |
| 9536 | |
| 9537 | switch (II->getIntrinsicID()) { |
| 9538 | default: break; |
| 9539 | case Intrinsic::bswap: |
| 9540 | // bswap(bswap(x)) -> x |
| 9541 | if (IntrinsicInst *Operand = dyn_cast<IntrinsicInst>(II->getOperand(1))) |
| 9542 | if (Operand->getIntrinsicID() == Intrinsic::bswap) |
| 9543 | return ReplaceInstUsesWith(CI, Operand->getOperand(1)); |
| 9544 | break; |
| 9545 | case Intrinsic::ppc_altivec_lvx: |
| 9546 | case Intrinsic::ppc_altivec_lvxl: |
| 9547 | case Intrinsic::x86_sse_loadu_ps: |
| 9548 | case Intrinsic::x86_sse2_loadu_pd: |
| 9549 | case Intrinsic::x86_sse2_loadu_dq: |
| 9550 | // Turn PPC lvx -> load if the pointer is known aligned. |
| 9551 | // Turn X86 loadups -> load if the pointer is known aligned. |
| 9552 | if (GetOrEnforceKnownAlignment(II->getOperand(1), 16) >= 16) { |
| 9553 | Value *Ptr = InsertBitCastBefore(II->getOperand(1), |
| 9554 | PointerType::getUnqual(II->getType()), |
| 9555 | CI); |
| 9556 | return new LoadInst(Ptr); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 9557 | } |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 9558 | break; |
| 9559 | case Intrinsic::ppc_altivec_stvx: |
| 9560 | case Intrinsic::ppc_altivec_stvxl: |
| 9561 | // Turn stvx -> store if the pointer is known aligned. |
| 9562 | if (GetOrEnforceKnownAlignment(II->getOperand(2), 16) >= 16) { |
| 9563 | const Type *OpPtrTy = |
| 9564 | PointerType::getUnqual(II->getOperand(1)->getType()); |
| 9565 | Value *Ptr = InsertBitCastBefore(II->getOperand(2), OpPtrTy, CI); |
| 9566 | return new StoreInst(II->getOperand(1), Ptr); |
| 9567 | } |
| 9568 | break; |
| 9569 | case Intrinsic::x86_sse_storeu_ps: |
| 9570 | case Intrinsic::x86_sse2_storeu_pd: |
| 9571 | case Intrinsic::x86_sse2_storeu_dq: |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 9572 | // Turn X86 storeu -> store if the pointer is known aligned. |
| 9573 | if (GetOrEnforceKnownAlignment(II->getOperand(1), 16) >= 16) { |
| 9574 | const Type *OpPtrTy = |
| 9575 | PointerType::getUnqual(II->getOperand(2)->getType()); |
| 9576 | Value *Ptr = InsertBitCastBefore(II->getOperand(1), OpPtrTy, CI); |
| 9577 | return new StoreInst(II->getOperand(2), Ptr); |
| 9578 | } |
| 9579 | break; |
| 9580 | |
| 9581 | case Intrinsic::x86_sse_cvttss2si: { |
| 9582 | // These intrinsics only demands the 0th element of its input vector. If |
| 9583 | // we can simplify the input based on that, do so now. |
Evan Cheng | 388df62 | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 9584 | unsigned VWidth = |
| 9585 | cast<VectorType>(II->getOperand(1)->getType())->getNumElements(); |
| 9586 | APInt DemandedElts(VWidth, 1); |
| 9587 | APInt UndefElts(VWidth, 0); |
| 9588 | if (Value *V = SimplifyDemandedVectorElts(II->getOperand(1), DemandedElts, |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 9589 | UndefElts)) { |
| 9590 | II->setOperand(1, V); |
| 9591 | return II; |
| 9592 | } |
| 9593 | break; |
| 9594 | } |
| 9595 | |
| 9596 | case Intrinsic::ppc_altivec_vperm: |
| 9597 | // Turn vperm(V1,V2,mask) -> shuffle(V1,V2,mask) if mask is a constant. |
| 9598 | if (ConstantVector *Mask = dyn_cast<ConstantVector>(II->getOperand(3))) { |
| 9599 | assert(Mask->getNumOperands() == 16 && "Bad type for intrinsic!"); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 9600 | |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 9601 | // Check that all of the elements are integer constants or undefs. |
| 9602 | bool AllEltsOk = true; |
| 9603 | for (unsigned i = 0; i != 16; ++i) { |
| 9604 | if (!isa<ConstantInt>(Mask->getOperand(i)) && |
| 9605 | !isa<UndefValue>(Mask->getOperand(i))) { |
| 9606 | AllEltsOk = false; |
| 9607 | break; |
| 9608 | } |
| 9609 | } |
| 9610 | |
| 9611 | if (AllEltsOk) { |
| 9612 | // Cast the input vectors to byte vectors. |
| 9613 | Value *Op0 =InsertBitCastBefore(II->getOperand(1),Mask->getType(),CI); |
| 9614 | Value *Op1 =InsertBitCastBefore(II->getOperand(2),Mask->getType(),CI); |
| 9615 | Value *Result = UndefValue::get(Op0->getType()); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 9616 | |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 9617 | // Only extract each element once. |
| 9618 | Value *ExtractedElts[32]; |
| 9619 | memset(ExtractedElts, 0, sizeof(ExtractedElts)); |
| 9620 | |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 9621 | for (unsigned i = 0; i != 16; ++i) { |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 9622 | if (isa<UndefValue>(Mask->getOperand(i))) |
| 9623 | continue; |
| 9624 | unsigned Idx=cast<ConstantInt>(Mask->getOperand(i))->getZExtValue(); |
| 9625 | Idx &= 31; // Match the hardware behavior. |
| 9626 | |
| 9627 | if (ExtractedElts[Idx] == 0) { |
| 9628 | Instruction *Elt = |
| 9629 | new ExtractElementInst(Idx < 16 ? Op0 : Op1, Idx&15, "tmp"); |
| 9630 | InsertNewInstBefore(Elt, CI); |
| 9631 | ExtractedElts[Idx] = Elt; |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 9632 | } |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 9633 | |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 9634 | // Insert this value into the result vector. |
| 9635 | Result = InsertElementInst::Create(Result, ExtractedElts[Idx], |
| 9636 | i, "tmp"); |
| 9637 | InsertNewInstBefore(cast<Instruction>(Result), CI); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 9638 | } |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 9639 | return CastInst::Create(Instruction::BitCast, Result, CI.getType()); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 9640 | } |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 9641 | } |
| 9642 | break; |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 9643 | |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 9644 | case Intrinsic::stackrestore: { |
| 9645 | // If the save is right next to the restore, remove the restore. This can |
| 9646 | // happen when variable allocas are DCE'd. |
| 9647 | if (IntrinsicInst *SS = dyn_cast<IntrinsicInst>(II->getOperand(1))) { |
| 9648 | if (SS->getIntrinsicID() == Intrinsic::stacksave) { |
| 9649 | BasicBlock::iterator BI = SS; |
| 9650 | if (&*++BI == II) |
| 9651 | return EraseInstFromFunction(CI); |
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 9652 | } |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 9653 | } |
| 9654 | |
| 9655 | // Scan down this block to see if there is another stack restore in the |
| 9656 | // same block without an intervening call/alloca. |
| 9657 | BasicBlock::iterator BI = II; |
| 9658 | TerminatorInst *TI = II->getParent()->getTerminator(); |
| 9659 | bool CannotRemove = false; |
| 9660 | for (++BI; &*BI != TI; ++BI) { |
| 9661 | if (isa<AllocaInst>(BI)) { |
| 9662 | CannotRemove = true; |
| 9663 | break; |
| 9664 | } |
Chris Lattner | aa0bf52 | 2008-06-25 05:59:28 +0000 | [diff] [blame] | 9665 | if (CallInst *BCI = dyn_cast<CallInst>(BI)) { |
| 9666 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(BCI)) { |
| 9667 | // If there is a stackrestore below this one, remove this one. |
| 9668 | if (II->getIntrinsicID() == Intrinsic::stackrestore) |
| 9669 | return EraseInstFromFunction(CI); |
| 9670 | // Otherwise, ignore the intrinsic. |
| 9671 | } else { |
| 9672 | // If we found a non-intrinsic call, we can't remove the stack |
| 9673 | // restore. |
Chris Lattner | bf1d8a7 | 2008-02-18 06:12:38 +0000 | [diff] [blame] | 9674 | CannotRemove = true; |
| 9675 | break; |
| 9676 | } |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 9677 | } |
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 9678 | } |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 9679 | |
| 9680 | // If the stack restore is in a return/unwind block and if there are no |
| 9681 | // allocas or calls between the restore and the return, nuke the restore. |
| 9682 | if (!CannotRemove && (isa<ReturnInst>(TI) || isa<UnwindInst>(TI))) |
| 9683 | return EraseInstFromFunction(CI); |
| 9684 | break; |
| 9685 | } |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 9686 | } |
| 9687 | |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 9688 | return visitCallSite(II); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9689 | } |
| 9690 | |
| 9691 | // InvokeInst simplification |
| 9692 | // |
| 9693 | Instruction *InstCombiner::visitInvokeInst(InvokeInst &II) { |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 9694 | return visitCallSite(&II); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9695 | } |
| 9696 | |
Dale Johannesen | da30ccb | 2008-04-25 21:16:07 +0000 | [diff] [blame] | 9697 | /// isSafeToEliminateVarargsCast - If this cast does not affect the value |
| 9698 | /// 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] | 9699 | static bool isSafeToEliminateVarargsCast(const CallSite CS, |
| 9700 | const CastInst * const CI, |
| 9701 | const TargetData * const TD, |
| 9702 | const int ix) { |
| 9703 | if (!CI->isLosslessCast()) |
| 9704 | return false; |
| 9705 | |
| 9706 | // The size of ByVal arguments is derived from the type, so we |
| 9707 | // can't change to a type with a different size. If the size were |
| 9708 | // passed explicitly we could avoid this check. |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 9709 | if (!CS.paramHasAttr(ix, Attribute::ByVal)) |
Dale Johannesen | 1f530a5 | 2008-04-23 18:34:37 +0000 | [diff] [blame] | 9710 | return true; |
| 9711 | |
| 9712 | const Type* SrcTy = |
| 9713 | cast<PointerType>(CI->getOperand(0)->getType())->getElementType(); |
| 9714 | const Type* DstTy = cast<PointerType>(CI->getType())->getElementType(); |
| 9715 | if (!SrcTy->isSized() || !DstTy->isSized()) |
| 9716 | return false; |
Duncan Sands | ceb4d1a | 2009-01-12 20:38:59 +0000 | [diff] [blame] | 9717 | if (TD->getTypePaddedSize(SrcTy) != TD->getTypePaddedSize(DstTy)) |
Dale Johannesen | 1f530a5 | 2008-04-23 18:34:37 +0000 | [diff] [blame] | 9718 | return false; |
| 9719 | return true; |
| 9720 | } |
| 9721 | |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 9722 | // visitCallSite - Improvements for call and invoke instructions. |
| 9723 | // |
| 9724 | Instruction *InstCombiner::visitCallSite(CallSite CS) { |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 9725 | bool Changed = false; |
| 9726 | |
| 9727 | // If the callee is a constexpr cast of a function, attempt to move the cast |
| 9728 | // to the arguments of the call/invoke. |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 9729 | if (transformConstExprCastCall(CS)) return 0; |
| 9730 | |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 9731 | Value *Callee = CS.getCalledValue(); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 9732 | |
Chris Lattner | 08b22ec | 2005-05-13 07:09:09 +0000 | [diff] [blame] | 9733 | if (Function *CalleeF = dyn_cast<Function>(Callee)) |
| 9734 | if (CalleeF->getCallingConv() != CS.getCallingConv()) { |
| 9735 | Instruction *OldCall = CS.getInstruction(); |
| 9736 | // If the call and callee calling conventions don't match, this call must |
| 9737 | // be unreachable, as the call is undefined. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 9738 | new StoreInst(ConstantInt::getTrue(), |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 9739 | UndefValue::get(PointerType::getUnqual(Type::Int1Ty)), |
| 9740 | OldCall); |
Chris Lattner | 08b22ec | 2005-05-13 07:09:09 +0000 | [diff] [blame] | 9741 | if (!OldCall->use_empty()) |
| 9742 | OldCall->replaceAllUsesWith(UndefValue::get(OldCall->getType())); |
| 9743 | if (isa<CallInst>(OldCall)) // Not worth removing an invoke here. |
| 9744 | return EraseInstFromFunction(*OldCall); |
| 9745 | return 0; |
| 9746 | } |
| 9747 | |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 9748 | if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) { |
| 9749 | // This instruction is not reachable, just remove it. We insert a store to |
| 9750 | // undef so that we know that this code is not reachable, despite the fact |
| 9751 | // that we can't modify the CFG here. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 9752 | new StoreInst(ConstantInt::getTrue(), |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 9753 | UndefValue::get(PointerType::getUnqual(Type::Int1Ty)), |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 9754 | CS.getInstruction()); |
| 9755 | |
| 9756 | if (!CS.getInstruction()->use_empty()) |
| 9757 | CS.getInstruction()-> |
| 9758 | replaceAllUsesWith(UndefValue::get(CS.getInstruction()->getType())); |
| 9759 | |
| 9760 | if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) { |
| 9761 | // Don't break the CFG, insert a dummy cond branch. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9762 | BranchInst::Create(II->getNormalDest(), II->getUnwindDest(), |
| 9763 | ConstantInt::getTrue(), II); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 9764 | } |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 9765 | return EraseInstFromFunction(*CS.getInstruction()); |
| 9766 | } |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 9767 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9768 | if (BitCastInst *BC = dyn_cast<BitCastInst>(Callee)) |
| 9769 | if (IntrinsicInst *In = dyn_cast<IntrinsicInst>(BC->getOperand(0))) |
| 9770 | if (In->getIntrinsicID() == Intrinsic::init_trampoline) |
| 9771 | return transformCallThroughTrampoline(CS); |
| 9772 | |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 9773 | const PointerType *PTy = cast<PointerType>(Callee->getType()); |
| 9774 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
| 9775 | if (FTy->isVarArg()) { |
Dale Johannesen | 63e7eb4 | 2008-04-23 01:03:05 +0000 | [diff] [blame] | 9776 | int ix = FTy->getNumParams() + (isa<InvokeInst>(Callee) ? 3 : 1); |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 9777 | // See if we can optimize any arguments passed through the varargs area of |
| 9778 | // the call. |
| 9779 | for (CallSite::arg_iterator I = CS.arg_begin()+FTy->getNumParams(), |
Dale Johannesen | 1f530a5 | 2008-04-23 18:34:37 +0000 | [diff] [blame] | 9780 | E = CS.arg_end(); I != E; ++I, ++ix) { |
| 9781 | CastInst *CI = dyn_cast<CastInst>(*I); |
| 9782 | if (CI && isSafeToEliminateVarargsCast(CS, CI, TD, ix)) { |
| 9783 | *I = CI->getOperand(0); |
| 9784 | Changed = true; |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 9785 | } |
Dale Johannesen | 1f530a5 | 2008-04-23 18:34:37 +0000 | [diff] [blame] | 9786 | } |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 9787 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9788 | |
Duncan Sands | f0c3354 | 2007-12-19 21:13:37 +0000 | [diff] [blame] | 9789 | if (isa<InlineAsm>(Callee) && !CS.doesNotThrow()) { |
Duncan Sands | ece2c04 | 2007-12-16 15:51:49 +0000 | [diff] [blame] | 9790 | // Inline asm calls cannot throw - mark them 'nounwind'. |
Duncan Sands | f0c3354 | 2007-12-19 21:13:37 +0000 | [diff] [blame] | 9791 | CS.setDoesNotThrow(); |
Duncan Sands | ece2c04 | 2007-12-16 15:51:49 +0000 | [diff] [blame] | 9792 | Changed = true; |
| 9793 | } |
| 9794 | |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 9795 | return Changed ? CS.getInstruction() : 0; |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 9796 | } |
| 9797 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9798 | // transformConstExprCastCall - If the callee is a constexpr cast of a function, |
| 9799 | // attempt to move the cast to the arguments of the call/invoke. |
| 9800 | // |
| 9801 | bool InstCombiner::transformConstExprCastCall(CallSite CS) { |
| 9802 | if (!isa<ConstantExpr>(CS.getCalledValue())) return false; |
| 9803 | ConstantExpr *CE = cast<ConstantExpr>(CS.getCalledValue()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9804 | if (CE->getOpcode() != Instruction::BitCast || |
| 9805 | !isa<Function>(CE->getOperand(0))) |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9806 | return false; |
Reid Spencer | 8863f18 | 2004-07-18 00:38:32 +0000 | [diff] [blame] | 9807 | Function *Callee = cast<Function>(CE->getOperand(0)); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9808 | Instruction *Caller = CS.getInstruction(); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 9809 | const AttrListPtr &CallerPAL = CS.getAttributes(); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9810 | |
| 9811 | // Okay, this is a cast from a function to a different type. Unless doing so |
| 9812 | // would cause a type conversion of one of our arguments, change this call to |
| 9813 | // be a direct call with arguments casted to the appropriate types. |
| 9814 | // |
| 9815 | const FunctionType *FT = Callee->getFunctionType(); |
| 9816 | const Type *OldRetTy = Caller->getType(); |
Duncan Sands | f413cdf | 2008-06-01 07:38:42 +0000 | [diff] [blame] | 9817 | const Type *NewRetTy = FT->getReturnType(); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9818 | |
Duncan Sands | f413cdf | 2008-06-01 07:38:42 +0000 | [diff] [blame] | 9819 | if (isa<StructType>(NewRetTy)) |
Devang Patel | 75e6f02 | 2008-03-11 18:04:06 +0000 | [diff] [blame] | 9820 | return false; // TODO: Handle multiple return values. |
| 9821 | |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 9822 | // Check to see if we are changing the return type... |
Duncan Sands | f413cdf | 2008-06-01 07:38:42 +0000 | [diff] [blame] | 9823 | if (OldRetTy != NewRetTy) { |
Bill Wendling | a6c3112 | 2008-05-14 22:45:20 +0000 | [diff] [blame] | 9824 | if (Callee->isDeclaration() && |
Duncan Sands | f413cdf | 2008-06-01 07:38:42 +0000 | [diff] [blame] | 9825 | // Conversion is ok if changing from one pointer type to another or from |
| 9826 | // a pointer to an integer of the same size. |
| 9827 | !((isa<PointerType>(OldRetTy) || OldRetTy == TD->getIntPtrType()) && |
Duncan Sands | 34b176a | 2008-06-17 15:55:30 +0000 | [diff] [blame] | 9828 | (isa<PointerType>(NewRetTy) || NewRetTy == TD->getIntPtrType()))) |
Chris Lattner | ec47992 | 2007-01-06 02:09:32 +0000 | [diff] [blame] | 9829 | return false; // Cannot transform this return value. |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 9830 | |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 9831 | if (!Caller->use_empty() && |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 9832 | // void -> non-void is handled specially |
Duncan Sands | f413cdf | 2008-06-01 07:38:42 +0000 | [diff] [blame] | 9833 | NewRetTy != Type::VoidTy && !CastInst::isCastable(NewRetTy, OldRetTy)) |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 9834 | return false; // Cannot transform this return value. |
| 9835 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9836 | if (!CallerPAL.isEmpty() && !Caller->use_empty()) { |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 9837 | Attributes RAttrs = CallerPAL.getRetAttributes(); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 9838 | if (RAttrs & Attribute::typeIncompatible(NewRetTy)) |
Duncan Sands | 6c3470e | 2008-01-07 17:16:06 +0000 | [diff] [blame] | 9839 | return false; // Attribute not compatible with transformed value. |
| 9840 | } |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 9841 | |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 9842 | // If the callsite is an invoke instruction, and the return value is used by |
| 9843 | // a PHI node in a successor, we cannot change the return type of the call |
| 9844 | // because there is no place to put the cast instruction (without breaking |
| 9845 | // the critical edge). Bail out in this case. |
| 9846 | if (!Caller->use_empty()) |
| 9847 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) |
| 9848 | for (Value::use_iterator UI = II->use_begin(), E = II->use_end(); |
| 9849 | UI != E; ++UI) |
| 9850 | if (PHINode *PN = dyn_cast<PHINode>(*UI)) |
| 9851 | if (PN->getParent() == II->getNormalDest() || |
Chris Lattner | aeb2a1d | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 9852 | PN->getParent() == II->getUnwindDest()) |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 9853 | return false; |
| 9854 | } |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9855 | |
| 9856 | unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin()); |
| 9857 | unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9858 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9859 | CallSite::arg_iterator AI = CS.arg_begin(); |
| 9860 | for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) { |
| 9861 | const Type *ParamTy = FT->getParamType(i); |
Andrew Lenharth | b8e604c | 2006-06-28 01:01:52 +0000 | [diff] [blame] | 9862 | const Type *ActTy = (*AI)->getType(); |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 9863 | |
| 9864 | if (!CastInst::isCastable(ActTy, ParamTy)) |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 9865 | return false; // Cannot transform this parameter value. |
| 9866 | |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 9867 | if (CallerPAL.getParamAttributes(i + 1) |
| 9868 | & Attribute::typeIncompatible(ParamTy)) |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9869 | return false; // Attribute not compatible with transformed value. |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 9870 | |
Duncan Sands | f413cdf | 2008-06-01 07:38:42 +0000 | [diff] [blame] | 9871 | // Converting from one pointer type to another or between a pointer and an |
| 9872 | // 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] | 9873 | bool isConvertible = ActTy == ParamTy || |
Duncan Sands | f413cdf | 2008-06-01 07:38:42 +0000 | [diff] [blame] | 9874 | ((isa<PointerType>(ParamTy) || ParamTy == TD->getIntPtrType()) && |
| 9875 | (isa<PointerType>(ActTy) || ActTy == TD->getIntPtrType())); |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 9876 | if (Callee->isDeclaration() && !isConvertible) return false; |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9877 | } |
| 9878 | |
| 9879 | if (FT->getNumParams() < NumActualArgs && !FT->isVarArg() && |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 9880 | Callee->isDeclaration()) |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9881 | return false; // Do not delete arguments unless we have a function body. |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9882 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9883 | if (FT->getNumParams() < NumActualArgs && FT->isVarArg() && |
| 9884 | !CallerPAL.isEmpty()) |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 9885 | // 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] | 9886 | // won't be dropping them. Check that these extra arguments have attributes |
| 9887 | // that are compatible with being a vararg call argument. |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9888 | for (unsigned i = CallerPAL.getNumSlots(); i; --i) { |
| 9889 | if (CallerPAL.getSlot(i - 1).Index <= FT->getNumParams()) |
Duncan Sands | e1e520f | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 9890 | break; |
Devang Patel | eaf42ab | 2008-09-23 23:03:40 +0000 | [diff] [blame] | 9891 | Attributes PAttrs = CallerPAL.getSlot(i - 1).Attrs; |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 9892 | if (PAttrs & Attribute::VarArgsIncompatible) |
Duncan Sands | e1e520f | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 9893 | return false; |
| 9894 | } |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 9895 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9896 | // Okay, we decided that this is a safe thing to do: go ahead and start |
| 9897 | // inserting cast instructions as necessary... |
| 9898 | std::vector<Value*> Args; |
| 9899 | Args.reserve(NumActualArgs); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 9900 | SmallVector<AttributeWithIndex, 8> attrVec; |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 9901 | attrVec.reserve(NumCommonArgs); |
| 9902 | |
| 9903 | // Get any return attributes. |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 9904 | Attributes RAttrs = CallerPAL.getRetAttributes(); |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 9905 | |
| 9906 | // If the return value is not being used, the type may not be compatible |
| 9907 | // with the existing attributes. Wipe out any problematic attributes. |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 9908 | RAttrs &= ~Attribute::typeIncompatible(NewRetTy); |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 9909 | |
| 9910 | // Add the new return attributes. |
| 9911 | if (RAttrs) |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 9912 | attrVec.push_back(AttributeWithIndex::get(0, RAttrs)); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9913 | |
| 9914 | AI = CS.arg_begin(); |
| 9915 | for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) { |
| 9916 | const Type *ParamTy = FT->getParamType(i); |
| 9917 | if ((*AI)->getType() == ParamTy) { |
| 9918 | Args.push_back(*AI); |
| 9919 | } else { |
Reid Spencer | 8a903db | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 9920 | Instruction::CastOps opcode = CastInst::getCastOpcode(*AI, |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9921 | false, ParamTy, false); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9922 | CastInst *NewCast = CastInst::Create(opcode, *AI, ParamTy, "tmp"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9923 | Args.push_back(InsertNewInstBefore(NewCast, *Caller)); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9924 | } |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 9925 | |
| 9926 | // Add any parameter attributes. |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 9927 | if (Attributes PAttrs = CallerPAL.getParamAttributes(i + 1)) |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 9928 | attrVec.push_back(AttributeWithIndex::get(i + 1, PAttrs)); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9929 | } |
| 9930 | |
| 9931 | // If the function takes more arguments than the call was taking, add them |
| 9932 | // now... |
| 9933 | for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i) |
| 9934 | Args.push_back(Constant::getNullValue(FT->getParamType(i))); |
| 9935 | |
| 9936 | // If we are removing arguments to the function, emit an obnoxious warning... |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 9937 | if (FT->getNumParams() < NumActualArgs) { |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9938 | if (!FT->isVarArg()) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 9939 | cerr << "WARNING: While resolving call to function '" |
| 9940 | << Callee->getName() << "' arguments were dropped!\n"; |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9941 | } else { |
| 9942 | // Add all of the arguments in their promoted form to the arg list... |
| 9943 | for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) { |
| 9944 | const Type *PTy = getPromotedType((*AI)->getType()); |
| 9945 | if (PTy != (*AI)->getType()) { |
| 9946 | // Must promote to pass through va_arg area! |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9947 | Instruction::CastOps opcode = CastInst::getCastOpcode(*AI, false, |
| 9948 | PTy, false); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9949 | Instruction *Cast = CastInst::Create(opcode, *AI, PTy, "tmp"); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9950 | InsertNewInstBefore(Cast, *Caller); |
| 9951 | Args.push_back(Cast); |
| 9952 | } else { |
| 9953 | Args.push_back(*AI); |
| 9954 | } |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 9955 | |
Duncan Sands | e1e520f | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 9956 | // Add any parameter attributes. |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 9957 | if (Attributes PAttrs = CallerPAL.getParamAttributes(i + 1)) |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 9958 | attrVec.push_back(AttributeWithIndex::get(i + 1, PAttrs)); |
Duncan Sands | e1e520f | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 9959 | } |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9960 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 9961 | } |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9962 | |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 9963 | if (Attributes FnAttrs = CallerPAL.getFnAttributes()) |
| 9964 | attrVec.push_back(AttributeWithIndex::get(~0, FnAttrs)); |
| 9965 | |
Duncan Sands | f413cdf | 2008-06-01 07:38:42 +0000 | [diff] [blame] | 9966 | if (NewRetTy == Type::VoidTy) |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 9967 | Caller->setName(""); // Void type should not have a name. |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9968 | |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 9969 | const AttrListPtr &NewCallerPAL = AttrListPtr::get(attrVec.begin(),attrVec.end()); |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 9970 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9971 | Instruction *NC; |
| 9972 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9973 | NC = InvokeInst::Create(Callee, II->getNormalDest(), II->getUnwindDest(), |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 9974 | Args.begin(), Args.end(), |
| 9975 | Caller->getName(), Caller); |
Reid Spencer | ed3fa85 | 2007-07-30 19:53:57 +0000 | [diff] [blame] | 9976 | cast<InvokeInst>(NC)->setCallingConv(II->getCallingConv()); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 9977 | cast<InvokeInst>(NC)->setAttributes(NewCallerPAL); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9978 | } else { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9979 | NC = CallInst::Create(Callee, Args.begin(), Args.end(), |
| 9980 | Caller->getName(), Caller); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 9981 | CallInst *CI = cast<CallInst>(Caller); |
| 9982 | if (CI->isTailCall()) |
Chris Lattner | a9e9211 | 2005-05-06 06:48:21 +0000 | [diff] [blame] | 9983 | cast<CallInst>(NC)->setTailCall(); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 9984 | cast<CallInst>(NC)->setCallingConv(CI->getCallingConv()); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 9985 | cast<CallInst>(NC)->setAttributes(NewCallerPAL); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9986 | } |
| 9987 | |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 9988 | // Insert a cast of the return type as necessary. |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9989 | Value *NV = NC; |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 9990 | if (OldRetTy != NV->getType() && !Caller->use_empty()) { |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9991 | if (NV->getType() != Type::VoidTy) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9992 | Instruction::CastOps opcode = CastInst::getCastOpcode(NC, false, |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 9993 | OldRetTy, false); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9994 | NV = NC = CastInst::Create(opcode, NC, OldRetTy, "tmp"); |
Chris Lattner | bb60904 | 2003-10-30 00:46:41 +0000 | [diff] [blame] | 9995 | |
| 9996 | // If this is an invoke instruction, we should insert it after the first |
| 9997 | // non-phi, instruction in the normal successor block. |
| 9998 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
Dan Gohman | 02dea8b | 2008-05-23 21:05:58 +0000 | [diff] [blame] | 9999 | BasicBlock::iterator I = II->getNormalDest()->getFirstNonPHI(); |
Chris Lattner | bb60904 | 2003-10-30 00:46:41 +0000 | [diff] [blame] | 10000 | InsertNewInstBefore(NC, *I); |
| 10001 | } else { |
| 10002 | // Otherwise, it's a call, just insert cast right after the call instr |
| 10003 | InsertNewInstBefore(NC, *Caller); |
| 10004 | } |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 10005 | AddUsersToWorkList(*Caller); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 10006 | } else { |
Chris Lattner | c30bda7 | 2004-10-17 21:22:38 +0000 | [diff] [blame] | 10007 | NV = UndefValue::get(Caller->getType()); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 10008 | } |
| 10009 | } |
| 10010 | |
| 10011 | if (Caller->getType() != Type::VoidTy && !Caller->use_empty()) |
| 10012 | Caller->replaceAllUsesWith(NV); |
Chris Lattner | f22a5c6 | 2007-03-02 19:59:19 +0000 | [diff] [blame] | 10013 | Caller->eraseFromParent(); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10014 | RemoveFromWorkList(Caller); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 10015 | return true; |
| 10016 | } |
| 10017 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10018 | // transformCallThroughTrampoline - Turn a call to a function created by the |
| 10019 | // init_trampoline intrinsic into a direct call to the underlying function. |
| 10020 | // |
| 10021 | Instruction *InstCombiner::transformCallThroughTrampoline(CallSite CS) { |
| 10022 | Value *Callee = CS.getCalledValue(); |
| 10023 | const PointerType *PTy = cast<PointerType>(Callee->getType()); |
| 10024 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 10025 | const AttrListPtr &Attrs = CS.getAttributes(); |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 10026 | |
| 10027 | // If the call already has the 'nest' attribute somewhere then give up - |
| 10028 | // otherwise 'nest' would occur twice after splicing in the chain. |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 10029 | if (Attrs.hasAttrSomewhere(Attribute::Nest)) |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 10030 | return 0; |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10031 | |
| 10032 | IntrinsicInst *Tramp = |
| 10033 | cast<IntrinsicInst>(cast<BitCastInst>(Callee)->getOperand(0)); |
| 10034 | |
Anton Korobeynikov | 0b12ecf | 2008-05-07 22:54:15 +0000 | [diff] [blame] | 10035 | Function *NestF = cast<Function>(Tramp->getOperand(2)->stripPointerCasts()); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10036 | const PointerType *NestFPTy = cast<PointerType>(NestF->getType()); |
| 10037 | const FunctionType *NestFTy = cast<FunctionType>(NestFPTy->getElementType()); |
| 10038 | |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 10039 | const AttrListPtr &NestAttrs = NestF->getAttributes(); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 10040 | if (!NestAttrs.isEmpty()) { |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10041 | unsigned NestIdx = 1; |
| 10042 | const Type *NestTy = 0; |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 10043 | Attributes NestAttr = Attribute::None; |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10044 | |
| 10045 | // Look for a parameter marked with the 'nest' attribute. |
| 10046 | for (FunctionType::param_iterator I = NestFTy->param_begin(), |
| 10047 | E = NestFTy->param_end(); I != E; ++NestIdx, ++I) |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 10048 | if (NestAttrs.paramHasAttr(NestIdx, Attribute::Nest)) { |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10049 | // Record the parameter type and any other attributes. |
| 10050 | NestTy = *I; |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 10051 | NestAttr = NestAttrs.getParamAttributes(NestIdx); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10052 | break; |
| 10053 | } |
| 10054 | |
| 10055 | if (NestTy) { |
| 10056 | Instruction *Caller = CS.getInstruction(); |
| 10057 | std::vector<Value*> NewArgs; |
| 10058 | NewArgs.reserve(unsigned(CS.arg_end()-CS.arg_begin())+1); |
| 10059 | |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 10060 | SmallVector<AttributeWithIndex, 8> NewAttrs; |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 10061 | NewAttrs.reserve(Attrs.getNumSlots() + 1); |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 10062 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10063 | // Insert the nest argument into the call argument list, which may |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 10064 | // mean appending it. Likewise for attributes. |
| 10065 | |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 10066 | // Add any result attributes. |
| 10067 | if (Attributes Attr = Attrs.getRetAttributes()) |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 10068 | NewAttrs.push_back(AttributeWithIndex::get(0, Attr)); |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 10069 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10070 | { |
| 10071 | unsigned Idx = 1; |
| 10072 | CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); |
| 10073 | do { |
| 10074 | if (Idx == NestIdx) { |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 10075 | // Add the chain argument and attributes. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10076 | Value *NestVal = Tramp->getOperand(3); |
| 10077 | if (NestVal->getType() != NestTy) |
| 10078 | NestVal = new BitCastInst(NestVal, NestTy, "nest", Caller); |
| 10079 | NewArgs.push_back(NestVal); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 10080 | NewAttrs.push_back(AttributeWithIndex::get(NestIdx, NestAttr)); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10081 | } |
| 10082 | |
| 10083 | if (I == E) |
| 10084 | break; |
| 10085 | |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 10086 | // Add the original argument and attributes. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10087 | NewArgs.push_back(*I); |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 10088 | if (Attributes Attr = Attrs.getParamAttributes(Idx)) |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 10089 | NewAttrs.push_back |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 10090 | (AttributeWithIndex::get(Idx + (Idx >= NestIdx), Attr)); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10091 | |
| 10092 | ++Idx, ++I; |
| 10093 | } while (1); |
| 10094 | } |
| 10095 | |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 10096 | // Add any function attributes. |
| 10097 | if (Attributes Attr = Attrs.getFnAttributes()) |
| 10098 | NewAttrs.push_back(AttributeWithIndex::get(~0, Attr)); |
| 10099 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10100 | // The trampoline may have been bitcast to a bogus type (FTy). |
| 10101 | // Handle this by synthesizing a new function type, equal to FTy |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 10102 | // with the chain parameter inserted. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10103 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10104 | std::vector<const Type*> NewTypes; |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10105 | NewTypes.reserve(FTy->getNumParams()+1); |
| 10106 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10107 | // 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] | 10108 | // mean appending it. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10109 | { |
| 10110 | unsigned Idx = 1; |
| 10111 | FunctionType::param_iterator I = FTy->param_begin(), |
| 10112 | E = FTy->param_end(); |
| 10113 | |
| 10114 | do { |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 10115 | if (Idx == NestIdx) |
| 10116 | // Add the chain's type. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10117 | NewTypes.push_back(NestTy); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10118 | |
| 10119 | if (I == E) |
| 10120 | break; |
| 10121 | |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 10122 | // Add the original type. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10123 | NewTypes.push_back(*I); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10124 | |
| 10125 | ++Idx, ++I; |
| 10126 | } while (1); |
| 10127 | } |
| 10128 | |
| 10129 | // Replace the trampoline call with a direct call. Let the generic |
| 10130 | // code sort out any function type mismatches. |
| 10131 | FunctionType *NewFTy = |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 10132 | FunctionType::get(FTy->getReturnType(), NewTypes, FTy->isVarArg()); |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 10133 | Constant *NewCallee = NestF->getType() == PointerType::getUnqual(NewFTy) ? |
| 10134 | NestF : ConstantExpr::getBitCast(NestF, PointerType::getUnqual(NewFTy)); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 10135 | const AttrListPtr &NewPAL = AttrListPtr::get(NewAttrs.begin(),NewAttrs.end()); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10136 | |
| 10137 | Instruction *NewCaller; |
| 10138 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 10139 | NewCaller = InvokeInst::Create(NewCallee, |
| 10140 | II->getNormalDest(), II->getUnwindDest(), |
| 10141 | NewArgs.begin(), NewArgs.end(), |
| 10142 | Caller->getName(), Caller); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10143 | cast<InvokeInst>(NewCaller)->setCallingConv(II->getCallingConv()); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 10144 | cast<InvokeInst>(NewCaller)->setAttributes(NewPAL); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10145 | } else { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 10146 | NewCaller = CallInst::Create(NewCallee, NewArgs.begin(), NewArgs.end(), |
| 10147 | Caller->getName(), Caller); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10148 | if (cast<CallInst>(Caller)->isTailCall()) |
| 10149 | cast<CallInst>(NewCaller)->setTailCall(); |
| 10150 | cast<CallInst>(NewCaller)-> |
| 10151 | setCallingConv(cast<CallInst>(Caller)->getCallingConv()); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 10152 | cast<CallInst>(NewCaller)->setAttributes(NewPAL); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10153 | } |
| 10154 | if (Caller->getType() != Type::VoidTy && !Caller->use_empty()) |
| 10155 | Caller->replaceAllUsesWith(NewCaller); |
| 10156 | Caller->eraseFromParent(); |
| 10157 | RemoveFromWorkList(Caller); |
| 10158 | return 0; |
| 10159 | } |
| 10160 | } |
| 10161 | |
| 10162 | // Replace the trampoline call with a direct call. Since there is no 'nest' |
| 10163 | // parameter, there is no need to adjust the argument list. Let the generic |
| 10164 | // code sort out any function type mismatches. |
| 10165 | Constant *NewCallee = |
| 10166 | NestF->getType() == PTy ? NestF : ConstantExpr::getBitCast(NestF, PTy); |
| 10167 | CS.setCalledFunction(NewCallee); |
| 10168 | return CS.getInstruction(); |
| 10169 | } |
| 10170 | |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 10171 | /// FoldPHIArgBinOpIntoPHI - If we have something like phi [add (a,b), add(c,d)] |
| 10172 | /// and if a/b/c/d and the add's all have a single use, turn this into two phi's |
| 10173 | /// and a single binop. |
| 10174 | Instruction *InstCombiner::FoldPHIArgBinOpIntoPHI(PHINode &PN) { |
| 10175 | Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0)); |
Chris Lattner | 38b3dcc | 2008-12-01 03:42:51 +0000 | [diff] [blame] | 10176 | assert(isa<BinaryOperator>(FirstInst) || isa<CmpInst>(FirstInst)); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 10177 | unsigned Opc = FirstInst->getOpcode(); |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 10178 | Value *LHSVal = FirstInst->getOperand(0); |
| 10179 | Value *RHSVal = FirstInst->getOperand(1); |
| 10180 | |
| 10181 | const Type *LHSType = LHSVal->getType(); |
| 10182 | const Type *RHSType = RHSVal->getType(); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 10183 | |
| 10184 | // Scan to see if all operands are the same opcode, all have one use, and all |
| 10185 | // kill their operands (i.e. the operands have one use). |
Chris Lattner | 05f1892 | 2008-12-01 02:34:36 +0000 | [diff] [blame] | 10186 | for (unsigned i = 1; i != PN.getNumIncomingValues(); ++i) { |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 10187 | Instruction *I = dyn_cast<Instruction>(PN.getIncomingValue(i)); |
Chris Lattner | a90a24c | 2006-11-01 04:55:47 +0000 | [diff] [blame] | 10188 | if (!I || I->getOpcode() != Opc || !I->hasOneUse() || |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10189 | // 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] | 10190 | // types or GEP's with different index types. |
| 10191 | I->getOperand(0)->getType() != LHSType || |
| 10192 | I->getOperand(1)->getType() != RHSType) |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 10193 | return 0; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10194 | |
| 10195 | // If they are CmpInst instructions, check their predicates |
| 10196 | if (Opc == Instruction::ICmp || Opc == Instruction::FCmp) |
| 10197 | if (cast<CmpInst>(I)->getPredicate() != |
| 10198 | cast<CmpInst>(FirstInst)->getPredicate()) |
| 10199 | return 0; |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 10200 | |
| 10201 | // Keep track of which operand needs a phi node. |
| 10202 | if (I->getOperand(0) != LHSVal) LHSVal = 0; |
| 10203 | if (I->getOperand(1) != RHSVal) RHSVal = 0; |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 10204 | } |
| 10205 | |
Chris Lattner | 38b3dcc | 2008-12-01 03:42:51 +0000 | [diff] [blame] | 10206 | // Otherwise, this is safe to transform! |
Chris Lattner | 53738a4 | 2006-11-08 19:42:28 +0000 | [diff] [blame] | 10207 | |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 10208 | Value *InLHS = FirstInst->getOperand(0); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 10209 | Value *InRHS = FirstInst->getOperand(1); |
Chris Lattner | 53738a4 | 2006-11-08 19:42:28 +0000 | [diff] [blame] | 10210 | PHINode *NewLHS = 0, *NewRHS = 0; |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 10211 | if (LHSVal == 0) { |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 10212 | NewLHS = PHINode::Create(LHSType, |
| 10213 | FirstInst->getOperand(0)->getName() + ".pn"); |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 10214 | NewLHS->reserveOperandSpace(PN.getNumOperands()/2); |
| 10215 | NewLHS->addIncoming(InLHS, PN.getIncomingBlock(0)); |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 10216 | InsertNewInstBefore(NewLHS, PN); |
| 10217 | LHSVal = NewLHS; |
| 10218 | } |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 10219 | |
| 10220 | if (RHSVal == 0) { |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 10221 | NewRHS = PHINode::Create(RHSType, |
| 10222 | FirstInst->getOperand(1)->getName() + ".pn"); |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 10223 | NewRHS->reserveOperandSpace(PN.getNumOperands()/2); |
| 10224 | NewRHS->addIncoming(InRHS, PN.getIncomingBlock(0)); |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 10225 | InsertNewInstBefore(NewRHS, PN); |
| 10226 | RHSVal = NewRHS; |
| 10227 | } |
| 10228 | |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 10229 | // Add all operands to the new PHIs. |
Chris Lattner | 05f1892 | 2008-12-01 02:34:36 +0000 | [diff] [blame] | 10230 | if (NewLHS || NewRHS) { |
| 10231 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 10232 | Instruction *InInst = cast<Instruction>(PN.getIncomingValue(i)); |
| 10233 | if (NewLHS) { |
| 10234 | Value *NewInLHS = InInst->getOperand(0); |
| 10235 | NewLHS->addIncoming(NewInLHS, PN.getIncomingBlock(i)); |
| 10236 | } |
| 10237 | if (NewRHS) { |
| 10238 | Value *NewInRHS = InInst->getOperand(1); |
| 10239 | NewRHS->addIncoming(NewInRHS, PN.getIncomingBlock(i)); |
| 10240 | } |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 10241 | } |
| 10242 | } |
| 10243 | |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 10244 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 10245 | return BinaryOperator::Create(BinOp->getOpcode(), LHSVal, RHSVal); |
Chris Lattner | 38b3dcc | 2008-12-01 03:42:51 +0000 | [diff] [blame] | 10246 | CmpInst *CIOp = cast<CmpInst>(FirstInst); |
| 10247 | return CmpInst::Create(CIOp->getOpcode(), CIOp->getPredicate(), LHSVal, |
| 10248 | RHSVal); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 10249 | } |
| 10250 | |
Chris Lattner | 05f1892 | 2008-12-01 02:34:36 +0000 | [diff] [blame] | 10251 | Instruction *InstCombiner::FoldPHIArgGEPIntoPHI(PHINode &PN) { |
| 10252 | GetElementPtrInst *FirstInst =cast<GetElementPtrInst>(PN.getIncomingValue(0)); |
| 10253 | |
| 10254 | SmallVector<Value*, 16> FixedOperands(FirstInst->op_begin(), |
| 10255 | FirstInst->op_end()); |
Chris Lattner | 36d3e32 | 2009-02-21 00:46:50 +0000 | [diff] [blame] | 10256 | // This is true if all GEP bases are allocas and if all indices into them are |
| 10257 | // constants. |
| 10258 | bool AllBasePointersAreAllocas = true; |
Chris Lattner | 05f1892 | 2008-12-01 02:34:36 +0000 | [diff] [blame] | 10259 | |
| 10260 | // Scan to see if all operands are the same opcode, all have one use, and all |
| 10261 | // kill their operands (i.e. the operands have one use). |
| 10262 | for (unsigned i = 1; i != PN.getNumIncomingValues(); ++i) { |
| 10263 | GetElementPtrInst *GEP= dyn_cast<GetElementPtrInst>(PN.getIncomingValue(i)); |
| 10264 | if (!GEP || !GEP->hasOneUse() || GEP->getType() != FirstInst->getType() || |
| 10265 | GEP->getNumOperands() != FirstInst->getNumOperands()) |
| 10266 | return 0; |
| 10267 | |
Chris Lattner | 36d3e32 | 2009-02-21 00:46:50 +0000 | [diff] [blame] | 10268 | // Keep track of whether or not all GEPs are of alloca pointers. |
| 10269 | if (AllBasePointersAreAllocas && |
| 10270 | (!isa<AllocaInst>(GEP->getOperand(0)) || |
| 10271 | !GEP->hasAllConstantIndices())) |
| 10272 | AllBasePointersAreAllocas = false; |
| 10273 | |
Chris Lattner | 05f1892 | 2008-12-01 02:34:36 +0000 | [diff] [blame] | 10274 | // Compare the operand lists. |
| 10275 | for (unsigned op = 0, e = FirstInst->getNumOperands(); op != e; ++op) { |
| 10276 | if (FirstInst->getOperand(op) == GEP->getOperand(op)) |
| 10277 | continue; |
| 10278 | |
| 10279 | // Don't merge two GEPs when two operands differ (introducing phi nodes) |
| 10280 | // if one of the PHIs has a constant for the index. The index may be |
| 10281 | // substantially cheaper to compute for the constants, so making it a |
| 10282 | // variable index could pessimize the path. This also handles the case |
| 10283 | // for struct indices, which must always be constant. |
| 10284 | if (isa<ConstantInt>(FirstInst->getOperand(op)) || |
| 10285 | isa<ConstantInt>(GEP->getOperand(op))) |
| 10286 | return 0; |
| 10287 | |
| 10288 | if (FirstInst->getOperand(op)->getType() !=GEP->getOperand(op)->getType()) |
| 10289 | return 0; |
| 10290 | FixedOperands[op] = 0; // Needs a PHI. |
| 10291 | } |
| 10292 | } |
| 10293 | |
Chris Lattner | 36d3e32 | 2009-02-21 00:46:50 +0000 | [diff] [blame] | 10294 | // If all of the base pointers of the PHI'd GEPs are from allocas, don't |
Chris Lattner | 2155088 | 2009-02-23 05:56:17 +0000 | [diff] [blame] | 10295 | // bother doing this transformation. At best, this will just save a bit of |
Chris Lattner | 36d3e32 | 2009-02-21 00:46:50 +0000 | [diff] [blame] | 10296 | // offset calculation, but all the predecessors will have to materialize the |
| 10297 | // stack address into a register anyway. We'd actually rather *clone* the |
| 10298 | // load up into the predecessors so that we have a load of a gep of an alloca, |
| 10299 | // which can usually all be folded into the load. |
| 10300 | if (AllBasePointersAreAllocas) |
| 10301 | return 0; |
| 10302 | |
Chris Lattner | 05f1892 | 2008-12-01 02:34:36 +0000 | [diff] [blame] | 10303 | // Otherwise, this is safe to transform. Insert PHI nodes for each operand |
| 10304 | // that is variable. |
| 10305 | SmallVector<PHINode*, 16> OperandPhis(FixedOperands.size()); |
| 10306 | |
| 10307 | bool HasAnyPHIs = false; |
| 10308 | for (unsigned i = 0, e = FixedOperands.size(); i != e; ++i) { |
| 10309 | if (FixedOperands[i]) continue; // operand doesn't need a phi. |
| 10310 | Value *FirstOp = FirstInst->getOperand(i); |
| 10311 | PHINode *NewPN = PHINode::Create(FirstOp->getType(), |
| 10312 | FirstOp->getName()+".pn"); |
| 10313 | InsertNewInstBefore(NewPN, PN); |
| 10314 | |
| 10315 | NewPN->reserveOperandSpace(e); |
| 10316 | NewPN->addIncoming(FirstOp, PN.getIncomingBlock(0)); |
| 10317 | OperandPhis[i] = NewPN; |
| 10318 | FixedOperands[i] = NewPN; |
| 10319 | HasAnyPHIs = true; |
| 10320 | } |
| 10321 | |
| 10322 | |
| 10323 | // Add all operands to the new PHIs. |
| 10324 | if (HasAnyPHIs) { |
| 10325 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 10326 | GetElementPtrInst *InGEP =cast<GetElementPtrInst>(PN.getIncomingValue(i)); |
| 10327 | BasicBlock *InBB = PN.getIncomingBlock(i); |
| 10328 | |
| 10329 | for (unsigned op = 0, e = OperandPhis.size(); op != e; ++op) |
| 10330 | if (PHINode *OpPhi = OperandPhis[op]) |
| 10331 | OpPhi->addIncoming(InGEP->getOperand(op), InBB); |
| 10332 | } |
| 10333 | } |
| 10334 | |
| 10335 | Value *Base = FixedOperands[0]; |
| 10336 | return GetElementPtrInst::Create(Base, FixedOperands.begin()+1, |
| 10337 | FixedOperands.end()); |
| 10338 | } |
| 10339 | |
| 10340 | |
Chris Lattner | 2155088 | 2009-02-23 05:56:17 +0000 | [diff] [blame] | 10341 | /// isSafeAndProfitableToSinkLoad - Return true if we know that it is safe to |
| 10342 | /// sink the load out of the block that defines it. This means that it must be |
Chris Lattner | 36d3e32 | 2009-02-21 00:46:50 +0000 | [diff] [blame] | 10343 | /// obvious the value of the load is not changed from the point of the load to |
| 10344 | /// the end of the block it is in. |
Chris Lattner | fd905ca | 2007-02-01 22:30:07 +0000 | [diff] [blame] | 10345 | /// |
| 10346 | /// Finally, it is safe, but not profitable, to sink a load targetting a |
| 10347 | /// non-address-taken alloca. Doing so will cause us to not promote the alloca |
| 10348 | /// to a register. |
Chris Lattner | 36d3e32 | 2009-02-21 00:46:50 +0000 | [diff] [blame] | 10349 | static bool isSafeAndProfitableToSinkLoad(LoadInst *L) { |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 10350 | BasicBlock::iterator BBI = L, E = L->getParent()->end(); |
| 10351 | |
| 10352 | for (++BBI; BBI != E; ++BBI) |
| 10353 | if (BBI->mayWriteToMemory()) |
| 10354 | return false; |
Chris Lattner | fd905ca | 2007-02-01 22:30:07 +0000 | [diff] [blame] | 10355 | |
| 10356 | // Check for non-address taken alloca. If not address-taken already, it isn't |
| 10357 | // profitable to do this xform. |
| 10358 | if (AllocaInst *AI = dyn_cast<AllocaInst>(L->getOperand(0))) { |
| 10359 | bool isAddressTaken = false; |
| 10360 | for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); |
| 10361 | UI != E; ++UI) { |
| 10362 | if (isa<LoadInst>(UI)) continue; |
| 10363 | if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) { |
| 10364 | // If storing TO the alloca, then the address isn't taken. |
| 10365 | if (SI->getOperand(1) == AI) continue; |
| 10366 | } |
| 10367 | isAddressTaken = true; |
| 10368 | break; |
| 10369 | } |
| 10370 | |
Chris Lattner | 36d3e32 | 2009-02-21 00:46:50 +0000 | [diff] [blame] | 10371 | if (!isAddressTaken && AI->isStaticAlloca()) |
Chris Lattner | fd905ca | 2007-02-01 22:30:07 +0000 | [diff] [blame] | 10372 | return false; |
| 10373 | } |
| 10374 | |
Chris Lattner | 36d3e32 | 2009-02-21 00:46:50 +0000 | [diff] [blame] | 10375 | // If this load is a load from a GEP with a constant offset from an alloca, |
| 10376 | // then we don't want to sink it. In its present form, it will be |
| 10377 | // load [constant stack offset]. Sinking it will cause us to have to |
| 10378 | // materialize the stack addresses in each predecessor in a register only to |
| 10379 | // do a shared load from register in the successor. |
| 10380 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(L->getOperand(0))) |
| 10381 | if (AllocaInst *AI = dyn_cast<AllocaInst>(GEP->getOperand(0))) |
| 10382 | if (AI->isStaticAlloca() && GEP->hasAllConstantIndices()) |
| 10383 | return false; |
| 10384 | |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 10385 | return true; |
| 10386 | } |
| 10387 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 10388 | |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 10389 | // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary" |
| 10390 | // operator and they all are only used by the PHI, PHI together their |
| 10391 | // inputs, and do the operation once, to the result of the PHI. |
| 10392 | Instruction *InstCombiner::FoldPHIArgOpIntoPHI(PHINode &PN) { |
| 10393 | Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0)); |
| 10394 | |
| 10395 | // Scan the instruction, looking for input operations that can be folded away. |
| 10396 | // If all input operands to the phi are the same instruction (e.g. a cast from |
| 10397 | // the same type or "+42") we can pull the operation through the PHI, reducing |
| 10398 | // code size and simplifying code. |
| 10399 | Constant *ConstantOp = 0; |
| 10400 | const Type *CastSrcTy = 0; |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 10401 | bool isVolatile = false; |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 10402 | if (isa<CastInst>(FirstInst)) { |
| 10403 | CastSrcTy = FirstInst->getOperand(0)->getType(); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 10404 | } else if (isa<BinaryOperator>(FirstInst) || isa<CmpInst>(FirstInst)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10405 | // Can fold binop, compare or shift here if the RHS is a constant, |
| 10406 | // otherwise call FoldPHIArgBinOpIntoPHI. |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 10407 | ConstantOp = dyn_cast<Constant>(FirstInst->getOperand(1)); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 10408 | if (ConstantOp == 0) |
| 10409 | return FoldPHIArgBinOpIntoPHI(PN); |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 10410 | } else if (LoadInst *LI = dyn_cast<LoadInst>(FirstInst)) { |
| 10411 | isVolatile = LI->isVolatile(); |
| 10412 | // We can't sink the load if the loaded value could be modified between the |
| 10413 | // load and the PHI. |
| 10414 | if (LI->getParent() != PN.getIncomingBlock(0) || |
Chris Lattner | 36d3e32 | 2009-02-21 00:46:50 +0000 | [diff] [blame] | 10415 | !isSafeAndProfitableToSinkLoad(LI)) |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 10416 | return 0; |
Chris Lattner | 7104296 | 2008-07-08 17:18:32 +0000 | [diff] [blame] | 10417 | |
| 10418 | // If the PHI is of volatile loads and the load block has multiple |
| 10419 | // successors, sinking it would remove a load of the volatile value from |
| 10420 | // the path through the other successor. |
| 10421 | if (isVolatile && |
| 10422 | LI->getParent()->getTerminator()->getNumSuccessors() != 1) |
| 10423 | return 0; |
| 10424 | |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 10425 | } else if (isa<GetElementPtrInst>(FirstInst)) { |
Chris Lattner | 05f1892 | 2008-12-01 02:34:36 +0000 | [diff] [blame] | 10426 | return FoldPHIArgGEPIntoPHI(PN); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 10427 | } else { |
| 10428 | return 0; // Cannot fold this operation. |
| 10429 | } |
| 10430 | |
| 10431 | // Check to see if all arguments are the same operation. |
| 10432 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 10433 | if (!isa<Instruction>(PN.getIncomingValue(i))) return 0; |
| 10434 | Instruction *I = cast<Instruction>(PN.getIncomingValue(i)); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10435 | if (!I->hasOneUse() || !I->isSameOperationAs(FirstInst)) |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 10436 | return 0; |
| 10437 | if (CastSrcTy) { |
| 10438 | if (I->getOperand(0)->getType() != CastSrcTy) |
| 10439 | return 0; // Cast operation must match. |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 10440 | } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10441 | // We can't sink the load if the loaded value could be modified between |
| 10442 | // the load and the PHI. |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 10443 | if (LI->isVolatile() != isVolatile || |
| 10444 | LI->getParent() != PN.getIncomingBlock(i) || |
Chris Lattner | 36d3e32 | 2009-02-21 00:46:50 +0000 | [diff] [blame] | 10445 | !isSafeAndProfitableToSinkLoad(LI)) |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 10446 | return 0; |
Chris Lattner | 40700fe | 2008-04-29 17:28:22 +0000 | [diff] [blame] | 10447 | |
Chris Lattner | 7104296 | 2008-07-08 17:18:32 +0000 | [diff] [blame] | 10448 | // If the PHI is of volatile loads and the load block has multiple |
| 10449 | // successors, sinking it would remove a load of the volatile value from |
| 10450 | // the path through the other successor. |
Chris Lattner | 40700fe | 2008-04-29 17:28:22 +0000 | [diff] [blame] | 10451 | if (isVolatile && |
| 10452 | LI->getParent()->getTerminator()->getNumSuccessors() != 1) |
| 10453 | return 0; |
Chris Lattner | 40700fe | 2008-04-29 17:28:22 +0000 | [diff] [blame] | 10454 | |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 10455 | } else if (I->getOperand(1) != ConstantOp) { |
| 10456 | return 0; |
| 10457 | } |
| 10458 | } |
| 10459 | |
| 10460 | // Okay, they are all the same operation. Create a new PHI node of the |
| 10461 | // 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] | 10462 | PHINode *NewPN = PHINode::Create(FirstInst->getOperand(0)->getType(), |
| 10463 | PN.getName()+".in"); |
Chris Lattner | 5551706 | 2005-01-29 00:39:08 +0000 | [diff] [blame] | 10464 | NewPN->reserveOperandSpace(PN.getNumOperands()/2); |
Chris Lattner | b589344 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 10465 | |
| 10466 | Value *InVal = FirstInst->getOperand(0); |
| 10467 | NewPN->addIncoming(InVal, PN.getIncomingBlock(0)); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 10468 | |
| 10469 | // Add all operands to the new PHI. |
Chris Lattner | b589344 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 10470 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 10471 | Value *NewInVal = cast<Instruction>(PN.getIncomingValue(i))->getOperand(0); |
| 10472 | if (NewInVal != InVal) |
| 10473 | InVal = 0; |
| 10474 | NewPN->addIncoming(NewInVal, PN.getIncomingBlock(i)); |
| 10475 | } |
| 10476 | |
| 10477 | Value *PhiVal; |
| 10478 | if (InVal) { |
| 10479 | // The new PHI unions all of the same values together. This is really |
| 10480 | // common, so we handle it intelligently here for compile-time speed. |
| 10481 | PhiVal = InVal; |
| 10482 | delete NewPN; |
| 10483 | } else { |
| 10484 | InsertNewInstBefore(NewPN, PN); |
| 10485 | PhiVal = NewPN; |
| 10486 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 10487 | |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 10488 | // Insert and return the new operation. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 10489 | if (CastInst* FirstCI = dyn_cast<CastInst>(FirstInst)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 10490 | return CastInst::Create(FirstCI->getOpcode(), PhiVal, PN.getType()); |
Chris Lattner | 54545ac | 2008-04-29 17:13:43 +0000 | [diff] [blame] | 10491 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 10492 | return BinaryOperator::Create(BinOp->getOpcode(), PhiVal, ConstantOp); |
Chris Lattner | 54545ac | 2008-04-29 17:13:43 +0000 | [diff] [blame] | 10493 | if (CmpInst *CIOp = dyn_cast<CmpInst>(FirstInst)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 10494 | return CmpInst::Create(CIOp->getOpcode(), CIOp->getPredicate(), |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10495 | PhiVal, ConstantOp); |
Chris Lattner | 54545ac | 2008-04-29 17:13:43 +0000 | [diff] [blame] | 10496 | assert(isa<LoadInst>(FirstInst) && "Unknown operation"); |
| 10497 | |
| 10498 | // If this was a volatile load that we are merging, make sure to loop through |
| 10499 | // and mark all the input loads as non-volatile. If we don't do this, we will |
| 10500 | // insert a new volatile load and the old ones will not be deletable. |
| 10501 | if (isVolatile) |
| 10502 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) |
| 10503 | cast<LoadInst>(PN.getIncomingValue(i))->setVolatile(false); |
| 10504 | |
| 10505 | return new LoadInst(PhiVal, "", isVolatile); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 10506 | } |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 10507 | |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 10508 | /// DeadPHICycle - Return true if this PHI node is only used by a PHI node cycle |
| 10509 | /// that is dead. |
Chris Lattner | 0e5444b | 2007-03-26 20:40:50 +0000 | [diff] [blame] | 10510 | static bool DeadPHICycle(PHINode *PN, |
| 10511 | SmallPtrSet<PHINode*, 16> &PotentiallyDeadPHIs) { |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 10512 | if (PN->use_empty()) return true; |
| 10513 | if (!PN->hasOneUse()) return false; |
| 10514 | |
| 10515 | // Remember this node, and if we find the cycle, return. |
Chris Lattner | 0e5444b | 2007-03-26 20:40:50 +0000 | [diff] [blame] | 10516 | if (!PotentiallyDeadPHIs.insert(PN)) |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 10517 | return true; |
Chris Lattner | 92103de | 2007-08-28 04:23:55 +0000 | [diff] [blame] | 10518 | |
| 10519 | // Don't scan crazily complex things. |
| 10520 | if (PotentiallyDeadPHIs.size() == 16) |
| 10521 | return false; |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 10522 | |
| 10523 | if (PHINode *PU = dyn_cast<PHINode>(PN->use_back())) |
| 10524 | return DeadPHICycle(PU, PotentiallyDeadPHIs); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 10525 | |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 10526 | return false; |
| 10527 | } |
| 10528 | |
Chris Lattner | cf5008a | 2007-11-06 21:52:06 +0000 | [diff] [blame] | 10529 | /// PHIsEqualValue - Return true if this phi node is always equal to |
| 10530 | /// NonPhiInVal. This happens with mutually cyclic phi nodes like: |
| 10531 | /// z = some value; x = phi (y, z); y = phi (x, z) |
| 10532 | static bool PHIsEqualValue(PHINode *PN, Value *NonPhiInVal, |
| 10533 | SmallPtrSet<PHINode*, 16> &ValueEqualPHIs) { |
| 10534 | // See if we already saw this PHI node. |
| 10535 | if (!ValueEqualPHIs.insert(PN)) |
| 10536 | return true; |
| 10537 | |
| 10538 | // Don't scan crazily complex things. |
| 10539 | if (ValueEqualPHIs.size() == 16) |
| 10540 | return false; |
| 10541 | |
| 10542 | // Scan the operands to see if they are either phi nodes or are equal to |
| 10543 | // the value. |
| 10544 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 10545 | Value *Op = PN->getIncomingValue(i); |
| 10546 | if (PHINode *OpPN = dyn_cast<PHINode>(Op)) { |
| 10547 | if (!PHIsEqualValue(OpPN, NonPhiInVal, ValueEqualPHIs)) |
| 10548 | return false; |
| 10549 | } else if (Op != NonPhiInVal) |
| 10550 | return false; |
| 10551 | } |
| 10552 | |
| 10553 | return true; |
| 10554 | } |
| 10555 | |
| 10556 | |
Chris Lattner | 473945d | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 10557 | // PHINode simplification |
| 10558 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 10559 | Instruction *InstCombiner::visitPHINode(PHINode &PN) { |
Owen Anderson | b64ab87 | 2006-07-10 22:15:25 +0000 | [diff] [blame] | 10560 | // If LCSSA is around, don't mess with Phi nodes |
Chris Lattner | f964f32 | 2007-03-04 04:27:24 +0000 | [diff] [blame] | 10561 | if (MustPreserveLCSSA) return 0; |
Owen Anderson | d1b78a1 | 2006-07-10 19:03:49 +0000 | [diff] [blame] | 10562 | |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 10563 | if (Value *V = PN.hasConstantValue()) |
| 10564 | return ReplaceInstUsesWith(PN, V); |
| 10565 | |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 10566 | // If all PHI operands are the same operation, pull them through the PHI, |
| 10567 | // reducing code size. |
| 10568 | if (isa<Instruction>(PN.getIncomingValue(0)) && |
Chris Lattner | 05f1892 | 2008-12-01 02:34:36 +0000 | [diff] [blame] | 10569 | isa<Instruction>(PN.getIncomingValue(1)) && |
| 10570 | cast<Instruction>(PN.getIncomingValue(0))->getOpcode() == |
| 10571 | cast<Instruction>(PN.getIncomingValue(1))->getOpcode() && |
| 10572 | // FIXME: The hasOneUse check will fail for PHIs that use the value more |
| 10573 | // than themselves more than once. |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 10574 | PN.getIncomingValue(0)->hasOneUse()) |
| 10575 | if (Instruction *Result = FoldPHIArgOpIntoPHI(PN)) |
| 10576 | return Result; |
| 10577 | |
| 10578 | // If this is a trivial cycle in the PHI node graph, remove it. Basically, if |
| 10579 | // this PHI only has a single use (a PHI), and if that PHI only has one use (a |
| 10580 | // PHI)... break the cycle. |
Chris Lattner | ff9f13a | 2007-01-15 07:30:06 +0000 | [diff] [blame] | 10581 | if (PN.hasOneUse()) { |
| 10582 | Instruction *PHIUser = cast<Instruction>(PN.use_back()); |
| 10583 | if (PHINode *PU = dyn_cast<PHINode>(PHIUser)) { |
Chris Lattner | 0e5444b | 2007-03-26 20:40:50 +0000 | [diff] [blame] | 10584 | SmallPtrSet<PHINode*, 16> PotentiallyDeadPHIs; |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 10585 | PotentiallyDeadPHIs.insert(&PN); |
| 10586 | if (DeadPHICycle(PU, PotentiallyDeadPHIs)) |
| 10587 | return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType())); |
| 10588 | } |
Chris Lattner | ff9f13a | 2007-01-15 07:30:06 +0000 | [diff] [blame] | 10589 | |
| 10590 | // If this phi has a single use, and if that use just computes a value for |
| 10591 | // the next iteration of a loop, delete the phi. This occurs with unused |
| 10592 | // induction variables, e.g. "for (int j = 0; ; ++j);". Detecting this |
| 10593 | // common case here is good because the only other things that catch this |
| 10594 | // are induction variable analysis (sometimes) and ADCE, which is only run |
| 10595 | // late. |
| 10596 | if (PHIUser->hasOneUse() && |
| 10597 | (isa<BinaryOperator>(PHIUser) || isa<GetElementPtrInst>(PHIUser)) && |
| 10598 | PHIUser->use_back() == &PN) { |
| 10599 | return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType())); |
| 10600 | } |
| 10601 | } |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 10602 | |
Chris Lattner | cf5008a | 2007-11-06 21:52:06 +0000 | [diff] [blame] | 10603 | // We sometimes end up with phi cycles that non-obviously end up being the |
| 10604 | // same value, for example: |
| 10605 | // z = some value; x = phi (y, z); y = phi (x, z) |
| 10606 | // where the phi nodes don't necessarily need to be in the same block. Do a |
| 10607 | // quick check to see if the PHI node only contains a single non-phi value, if |
| 10608 | // so, scan to see if the phi cycle is actually equal to that value. |
| 10609 | { |
| 10610 | unsigned InValNo = 0, NumOperandVals = PN.getNumIncomingValues(); |
| 10611 | // Scan for the first non-phi operand. |
| 10612 | while (InValNo != NumOperandVals && |
| 10613 | isa<PHINode>(PN.getIncomingValue(InValNo))) |
| 10614 | ++InValNo; |
| 10615 | |
| 10616 | if (InValNo != NumOperandVals) { |
| 10617 | Value *NonPhiInVal = PN.getOperand(InValNo); |
| 10618 | |
| 10619 | // Scan the rest of the operands to see if there are any conflicts, if so |
| 10620 | // there is no need to recursively scan other phis. |
| 10621 | for (++InValNo; InValNo != NumOperandVals; ++InValNo) { |
| 10622 | Value *OpVal = PN.getIncomingValue(InValNo); |
| 10623 | if (OpVal != NonPhiInVal && !isa<PHINode>(OpVal)) |
| 10624 | break; |
| 10625 | } |
| 10626 | |
| 10627 | // If we scanned over all operands, then we have one unique value plus |
| 10628 | // phi values. Scan PHI nodes to see if they all merge in each other or |
| 10629 | // the value. |
| 10630 | if (InValNo == NumOperandVals) { |
| 10631 | SmallPtrSet<PHINode*, 16> ValueEqualPHIs; |
| 10632 | if (PHIsEqualValue(&PN, NonPhiInVal, ValueEqualPHIs)) |
| 10633 | return ReplaceInstUsesWith(PN, NonPhiInVal); |
| 10634 | } |
| 10635 | } |
| 10636 | } |
Chris Lattner | 60921c9 | 2003-12-19 05:58:40 +0000 | [diff] [blame] | 10637 | return 0; |
Chris Lattner | 473945d | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 10638 | } |
| 10639 | |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 10640 | static Value *InsertCastToIntPtrTy(Value *V, const Type *DTy, |
| 10641 | Instruction *InsertPoint, |
| 10642 | InstCombiner *IC) { |
Reid Spencer | abaa8ca | 2007-01-08 16:32:00 +0000 | [diff] [blame] | 10643 | unsigned PtrSize = DTy->getPrimitiveSizeInBits(); |
| 10644 | unsigned VTySize = V->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 10645 | // We must cast correctly to the pointer type. Ensure that we |
| 10646 | // sign extend the integer value if it is smaller as this is |
| 10647 | // used for address computation. |
| 10648 | Instruction::CastOps opcode = |
| 10649 | (VTySize < PtrSize ? Instruction::SExt : |
| 10650 | (VTySize == PtrSize ? Instruction::BitCast : Instruction::Trunc)); |
| 10651 | return IC->InsertCastBefore(opcode, V, DTy, *InsertPoint); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 10652 | } |
| 10653 | |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 10654 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 10655 | Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 10656 | Value *PtrOp = GEP.getOperand(0); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 10657 | // Is it 'getelementptr %P, i32 0' or 'getelementptr %P' |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 10658 | // If so, eliminate the noop. |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 10659 | if (GEP.getNumOperands() == 1) |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 10660 | return ReplaceInstUsesWith(GEP, PtrOp); |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 10661 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10662 | if (isa<UndefValue>(GEP.getOperand(0))) |
| 10663 | return ReplaceInstUsesWith(GEP, UndefValue::get(GEP.getType())); |
| 10664 | |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 10665 | bool HasZeroPointerIndex = false; |
| 10666 | if (Constant *C = dyn_cast<Constant>(GEP.getOperand(1))) |
| 10667 | HasZeroPointerIndex = C->isNullValue(); |
| 10668 | |
| 10669 | if (GEP.getNumOperands() == 2 && HasZeroPointerIndex) |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 10670 | return ReplaceInstUsesWith(GEP, PtrOp); |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 10671 | |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 10672 | // Eliminate unneeded casts for indices. |
| 10673 | bool MadeChange = false; |
Chris Lattner | db9654e | 2007-03-25 20:43:09 +0000 | [diff] [blame] | 10674 | |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 10675 | gep_type_iterator GTI = gep_type_begin(GEP); |
Gabor Greif | 177dd3f | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 10676 | for (User::op_iterator i = GEP.op_begin() + 1, e = GEP.op_end(); |
| 10677 | i != e; ++i, ++GTI) { |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 10678 | if (isa<SequentialType>(*GTI)) { |
Gabor Greif | 177dd3f | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 10679 | if (CastInst *CI = dyn_cast<CastInst>(*i)) { |
Chris Lattner | 76b7a06 | 2007-01-15 07:02:54 +0000 | [diff] [blame] | 10680 | if (CI->getOpcode() == Instruction::ZExt || |
| 10681 | CI->getOpcode() == Instruction::SExt) { |
| 10682 | const Type *SrcTy = CI->getOperand(0)->getType(); |
| 10683 | // We can eliminate a cast from i32 to i64 iff the target |
| 10684 | // is a 32-bit pointer target. |
| 10685 | if (SrcTy->getPrimitiveSizeInBits() >= TD->getPointerSizeInBits()) { |
| 10686 | MadeChange = true; |
Gabor Greif | 177dd3f | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 10687 | *i = CI->getOperand(0); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 10688 | } |
| 10689 | } |
| 10690 | } |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 10691 | // If we are using a wider index than needed for this platform, shrink it |
Dan Gohman | 4f833d4 | 2008-09-11 23:06:38 +0000 | [diff] [blame] | 10692 | // to what we need. If narrower, sign-extend it to what we need. |
| 10693 | // If the incoming value needs a cast instruction, |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 10694 | // insert it. This explicit cast can make subsequent optimizations more |
| 10695 | // obvious. |
Gabor Greif | 177dd3f | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 10696 | Value *Op = *i; |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 10697 | if (TD->getTypeSizeInBits(Op->getType()) > TD->getPointerSizeInBits()) { |
Chris Lattner | 4f1134e | 2004-04-17 18:16:10 +0000 | [diff] [blame] | 10698 | if (Constant *C = dyn_cast<Constant>(Op)) { |
Gabor Greif | 177dd3f | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 10699 | *i = ConstantExpr::getTrunc(C, TD->getIntPtrType()); |
Chris Lattner | 4f1134e | 2004-04-17 18:16:10 +0000 | [diff] [blame] | 10700 | MadeChange = true; |
| 10701 | } else { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 10702 | Op = InsertCastBefore(Instruction::Trunc, Op, TD->getIntPtrType(), |
| 10703 | GEP); |
Gabor Greif | 177dd3f | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 10704 | *i = Op; |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 10705 | MadeChange = true; |
| 10706 | } |
Dan Gohman | 4f833d4 | 2008-09-11 23:06:38 +0000 | [diff] [blame] | 10707 | } else if (TD->getTypeSizeInBits(Op->getType()) < TD->getPointerSizeInBits()) { |
| 10708 | if (Constant *C = dyn_cast<Constant>(Op)) { |
| 10709 | *i = ConstantExpr::getSExt(C, TD->getIntPtrType()); |
| 10710 | MadeChange = true; |
| 10711 | } else { |
| 10712 | Op = InsertCastBefore(Instruction::SExt, Op, TD->getIntPtrType(), |
| 10713 | GEP); |
| 10714 | *i = Op; |
| 10715 | MadeChange = true; |
| 10716 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 10717 | } |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 10718 | } |
Chris Lattner | db9654e | 2007-03-25 20:43:09 +0000 | [diff] [blame] | 10719 | } |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 10720 | if (MadeChange) return &GEP; |
| 10721 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 10722 | // Combine Indices - If the source pointer to this getelementptr instruction |
| 10723 | // is a getelementptr instruction, combine the indices of the two |
| 10724 | // getelementptr instructions into a single instruction. |
| 10725 | // |
Chris Lattner | 72588fc | 2007-02-15 22:48:32 +0000 | [diff] [blame] | 10726 | SmallVector<Value*, 8> SrcGEPOperands; |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 10727 | if (User *Src = dyn_castGetElementPtr(PtrOp)) |
Chris Lattner | 72588fc | 2007-02-15 22:48:32 +0000 | [diff] [blame] | 10728 | SrcGEPOperands.append(Src->op_begin(), Src->op_end()); |
Chris Lattner | ebd985c | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 10729 | |
| 10730 | if (!SrcGEPOperands.empty()) { |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 10731 | // Note that if our source is a gep chain itself that we wait for that |
| 10732 | // chain to be resolved before we perform this transformation. This |
| 10733 | // avoids us creating a TON of code in some cases. |
| 10734 | // |
| 10735 | if (isa<GetElementPtrInst>(SrcGEPOperands[0]) && |
| 10736 | cast<Instruction>(SrcGEPOperands[0])->getNumOperands() == 2) |
| 10737 | return 0; // Wait until our source is folded to completion. |
| 10738 | |
Chris Lattner | 72588fc | 2007-02-15 22:48:32 +0000 | [diff] [blame] | 10739 | SmallVector<Value*, 8> Indices; |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 10740 | |
| 10741 | // Find out whether the last index in the source GEP is a sequential idx. |
| 10742 | bool EndsWithSequential = false; |
| 10743 | for (gep_type_iterator I = gep_type_begin(*cast<User>(PtrOp)), |
| 10744 | E = gep_type_end(*cast<User>(PtrOp)); I != E; ++I) |
Chris Lattner | be97b4e | 2004-05-08 22:41:42 +0000 | [diff] [blame] | 10745 | EndsWithSequential = !isa<StructType>(*I); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 10746 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 10747 | // Can we combine the two pointer arithmetics offsets? |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 10748 | if (EndsWithSequential) { |
Chris Lattner | decd081 | 2003-03-05 22:33:14 +0000 | [diff] [blame] | 10749 | // Replace: gep (gep %P, long B), long A, ... |
| 10750 | // With: T = long A+B; gep %P, T, ... |
| 10751 | // |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 10752 | Value *Sum, *SO1 = SrcGEPOperands.back(), *GO1 = GEP.getOperand(1); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 10753 | if (SO1 == Constant::getNullValue(SO1->getType())) { |
| 10754 | Sum = GO1; |
| 10755 | } else if (GO1 == Constant::getNullValue(GO1->getType())) { |
| 10756 | Sum = SO1; |
| 10757 | } else { |
| 10758 | // If they aren't the same type, convert both to an integer of the |
| 10759 | // target's pointer size. |
| 10760 | if (SO1->getType() != GO1->getType()) { |
| 10761 | if (Constant *SO1C = dyn_cast<Constant>(SO1)) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 10762 | SO1 = ConstantExpr::getIntegerCast(SO1C, GO1->getType(), true); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 10763 | } else if (Constant *GO1C = dyn_cast<Constant>(GO1)) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 10764 | GO1 = ConstantExpr::getIntegerCast(GO1C, SO1->getType(), true); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 10765 | } else { |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 10766 | unsigned PS = TD->getPointerSizeInBits(); |
| 10767 | if (TD->getTypeSizeInBits(SO1->getType()) == PS) { |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 10768 | // Convert GO1 to SO1's type. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 10769 | GO1 = InsertCastToIntPtrTy(GO1, SO1->getType(), &GEP, this); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 10770 | |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 10771 | } else if (TD->getTypeSizeInBits(GO1->getType()) == PS) { |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 10772 | // Convert SO1 to GO1's type. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 10773 | SO1 = InsertCastToIntPtrTy(SO1, GO1->getType(), &GEP, this); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 10774 | } else { |
| 10775 | const Type *PT = TD->getIntPtrType(); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 10776 | SO1 = InsertCastToIntPtrTy(SO1, PT, &GEP, this); |
| 10777 | GO1 = InsertCastToIntPtrTy(GO1, PT, &GEP, this); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 10778 | } |
| 10779 | } |
| 10780 | } |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 10781 | if (isa<Constant>(SO1) && isa<Constant>(GO1)) |
| 10782 | Sum = ConstantExpr::getAdd(cast<Constant>(SO1), cast<Constant>(GO1)); |
| 10783 | else { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 10784 | Sum = BinaryOperator::CreateAdd(SO1, GO1, PtrOp->getName()+".sum"); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 10785 | InsertNewInstBefore(cast<Instruction>(Sum), GEP); |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 10786 | } |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 10787 | } |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 10788 | |
| 10789 | // Recycle the GEP we already have if possible. |
| 10790 | if (SrcGEPOperands.size() == 2) { |
| 10791 | GEP.setOperand(0, SrcGEPOperands[0]); |
| 10792 | GEP.setOperand(1, Sum); |
| 10793 | return &GEP; |
| 10794 | } else { |
| 10795 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, |
| 10796 | SrcGEPOperands.end()-1); |
| 10797 | Indices.push_back(Sum); |
| 10798 | Indices.insert(Indices.end(), GEP.op_begin()+2, GEP.op_end()); |
| 10799 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 10800 | } else if (isa<Constant>(*GEP.idx_begin()) && |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 10801 | cast<Constant>(*GEP.idx_begin())->isNullValue() && |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 10802 | SrcGEPOperands.size() != 1) { |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 10803 | // 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] | 10804 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, |
| 10805 | SrcGEPOperands.end()); |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 10806 | Indices.insert(Indices.end(), GEP.idx_begin()+1, GEP.idx_end()); |
| 10807 | } |
| 10808 | |
| 10809 | if (!Indices.empty()) |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 10810 | return GetElementPtrInst::Create(SrcGEPOperands[0], Indices.begin(), |
| 10811 | Indices.end(), GEP.getName()); |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 10812 | |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 10813 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(PtrOp)) { |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 10814 | // GEP of global variable. If all of the indices for this GEP are |
| 10815 | // constants, we can promote this to a constexpr instead of an instruction. |
| 10816 | |
| 10817 | // Scan for nonconstants... |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 10818 | SmallVector<Constant*, 8> Indices; |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 10819 | User::op_iterator I = GEP.idx_begin(), E = GEP.idx_end(); |
| 10820 | for (; I != E && isa<Constant>(*I); ++I) |
| 10821 | Indices.push_back(cast<Constant>(*I)); |
| 10822 | |
| 10823 | if (I == E) { // If they are all constants... |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 10824 | Constant *CE = ConstantExpr::getGetElementPtr(GV, |
| 10825 | &Indices[0],Indices.size()); |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 10826 | |
| 10827 | // Replace all uses of the GEP with the new constexpr... |
| 10828 | return ReplaceInstUsesWith(GEP, CE); |
| 10829 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 10830 | } else if (Value *X = getBitCastOperand(PtrOp)) { // Is the operand a cast? |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 10831 | if (!isa<PointerType>(X->getType())) { |
| 10832 | // Not interesting. Source pointer must be a cast from pointer. |
| 10833 | } else if (HasZeroPointerIndex) { |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 10834 | // transform: GEP (bitcast [10 x i8]* X to [0 x i8]*), i32 0, ... |
| 10835 | // into : GEP [10 x i8]* X, i32 0, ... |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 10836 | // |
Duncan Sands | 5b7cfb0 | 2009-03-02 09:18:21 +0000 | [diff] [blame] | 10837 | // Likewise, transform: GEP (bitcast i8* X to [0 x i8]*), i32 0, ... |
| 10838 | // into : GEP i8* X, ... |
| 10839 | // |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 10840 | // This occurs when the program declares an array extern like "int X[];" |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 10841 | const PointerType *CPTy = cast<PointerType>(PtrOp->getType()); |
| 10842 | const PointerType *XTy = cast<PointerType>(X->getType()); |
Duncan Sands | 5b7cfb0 | 2009-03-02 09:18:21 +0000 | [diff] [blame] | 10843 | if (const ArrayType *CATy = |
| 10844 | dyn_cast<ArrayType>(CPTy->getElementType())) { |
| 10845 | // GEP (bitcast i8* X to [0 x i8]*), i32 0, ... ? |
| 10846 | if (CATy->getElementType() == XTy->getElementType()) { |
| 10847 | // -> GEP i8* X, ... |
| 10848 | SmallVector<Value*, 8> Indices(GEP.idx_begin()+1, GEP.idx_end()); |
| 10849 | return GetElementPtrInst::Create(X, Indices.begin(), Indices.end(), |
| 10850 | GEP.getName()); |
| 10851 | } else if (const ArrayType *XATy = |
| 10852 | dyn_cast<ArrayType>(XTy->getElementType())) { |
| 10853 | // GEP (bitcast [10 x i8]* X to [0 x i8]*), i32 0, ... ? |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 10854 | if (CATy->getElementType() == XATy->getElementType()) { |
Duncan Sands | 5b7cfb0 | 2009-03-02 09:18:21 +0000 | [diff] [blame] | 10855 | // -> GEP [10 x i8]* X, i32 0, ... |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 10856 | // At this point, we know that the cast source type is a pointer |
| 10857 | // to an array of the same type as the destination pointer |
| 10858 | // array. Because the array type is never stepped over (there |
| 10859 | // is a leading zero) we can fold the cast into this GEP. |
| 10860 | GEP.setOperand(0, X); |
| 10861 | return &GEP; |
| 10862 | } |
Duncan Sands | 5b7cfb0 | 2009-03-02 09:18:21 +0000 | [diff] [blame] | 10863 | } |
| 10864 | } |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 10865 | } else if (GEP.getNumOperands() == 2) { |
| 10866 | // Transform things like: |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 10867 | // %t = getelementptr i32* bitcast ([2 x i32]* %str to i32*), i32 %V |
| 10868 | // into: %t1 = getelementptr [2 x i32]* %str, i32 0, i32 %V; bitcast |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 10869 | const Type *SrcElTy = cast<PointerType>(X->getType())->getElementType(); |
| 10870 | const Type *ResElTy=cast<PointerType>(PtrOp->getType())->getElementType(); |
| 10871 | if (isa<ArrayType>(SrcElTy) && |
Duncan Sands | ceb4d1a | 2009-01-12 20:38:59 +0000 | [diff] [blame] | 10872 | TD->getTypePaddedSize(cast<ArrayType>(SrcElTy)->getElementType()) == |
| 10873 | TD->getTypePaddedSize(ResElTy)) { |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 10874 | Value *Idx[2]; |
| 10875 | Idx[0] = Constant::getNullValue(Type::Int32Ty); |
| 10876 | Idx[1] = GEP.getOperand(1); |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 10877 | Value *V = InsertNewInstBefore( |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 10878 | GetElementPtrInst::Create(X, Idx, Idx + 2, GEP.getName()), GEP); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 10879 | // V and GEP are both pointer types --> BitCast |
| 10880 | return new BitCastInst(V, GEP.getType()); |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 10881 | } |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 10882 | |
| 10883 | // Transform things like: |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 10884 | // getelementptr i8* bitcast ([100 x double]* X to i8*), i32 %tmp |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 10885 | // (where tmp = 8*tmp2) into: |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 10886 | // getelementptr [100 x double]* %arr, i32 0, i32 %tmp2; bitcast |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 10887 | |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 10888 | if (isa<ArrayType>(SrcElTy) && ResElTy == Type::Int8Ty) { |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 10889 | uint64_t ArrayEltSize = |
Duncan Sands | ceb4d1a | 2009-01-12 20:38:59 +0000 | [diff] [blame] | 10890 | TD->getTypePaddedSize(cast<ArrayType>(SrcElTy)->getElementType()); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 10891 | |
| 10892 | // Check to see if "tmp" is a scale by a multiple of ArrayEltSize. We |
| 10893 | // allow either a mul, shift, or constant here. |
| 10894 | Value *NewIdx = 0; |
| 10895 | ConstantInt *Scale = 0; |
| 10896 | if (ArrayEltSize == 1) { |
| 10897 | NewIdx = GEP.getOperand(1); |
| 10898 | Scale = ConstantInt::get(NewIdx->getType(), 1); |
| 10899 | } else if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP.getOperand(1))) { |
Chris Lattner | 6e2f843 | 2005-09-14 17:32:56 +0000 | [diff] [blame] | 10900 | NewIdx = ConstantInt::get(CI->getType(), 1); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 10901 | Scale = CI; |
| 10902 | } else if (Instruction *Inst =dyn_cast<Instruction>(GEP.getOperand(1))){ |
| 10903 | if (Inst->getOpcode() == Instruction::Shl && |
| 10904 | isa<ConstantInt>(Inst->getOperand(1))) { |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 10905 | ConstantInt *ShAmt = cast<ConstantInt>(Inst->getOperand(1)); |
| 10906 | uint32_t ShAmtVal = ShAmt->getLimitedValue(64); |
| 10907 | Scale = ConstantInt::get(Inst->getType(), 1ULL << ShAmtVal); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 10908 | NewIdx = Inst->getOperand(0); |
| 10909 | } else if (Inst->getOpcode() == Instruction::Mul && |
| 10910 | isa<ConstantInt>(Inst->getOperand(1))) { |
| 10911 | Scale = cast<ConstantInt>(Inst->getOperand(1)); |
| 10912 | NewIdx = Inst->getOperand(0); |
| 10913 | } |
| 10914 | } |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 10915 | |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 10916 | // 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] | 10917 | // out, perform the transformation. Note, we don't know whether Scale is |
| 10918 | // signed or not. We'll use unsigned version of division/modulo |
| 10919 | // operation after making sure Scale doesn't have the sign bit set. |
Chris Lattner | 58b1ac7 | 2009-02-25 18:20:01 +0000 | [diff] [blame] | 10920 | if (ArrayEltSize && Scale && Scale->getSExtValue() >= 0LL && |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 10921 | Scale->getZExtValue() % ArrayEltSize == 0) { |
| 10922 | Scale = ConstantInt::get(Scale->getType(), |
| 10923 | Scale->getZExtValue() / ArrayEltSize); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10924 | if (Scale->getZExtValue() != 1) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 10925 | Constant *C = ConstantExpr::getIntegerCast(Scale, NewIdx->getType(), |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 10926 | false /*ZExt*/); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 10927 | Instruction *Sc = BinaryOperator::CreateMul(NewIdx, C, "idxscale"); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 10928 | NewIdx = InsertNewInstBefore(Sc, GEP); |
| 10929 | } |
| 10930 | |
| 10931 | // Insert the new GEP instruction. |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 10932 | Value *Idx[2]; |
| 10933 | Idx[0] = Constant::getNullValue(Type::Int32Ty); |
| 10934 | Idx[1] = NewIdx; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 10935 | Instruction *NewGEP = |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 10936 | GetElementPtrInst::Create(X, Idx, Idx + 2, GEP.getName()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 10937 | NewGEP = InsertNewInstBefore(NewGEP, GEP); |
| 10938 | // The NewGEP must be pointer typed, so must the old one -> BitCast |
| 10939 | return new BitCastInst(NewGEP, GEP.getType()); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 10940 | } |
| 10941 | } |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 10942 | } |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 10943 | } |
Chris Lattner | 5840779 | 2009-01-09 04:53:57 +0000 | [diff] [blame] | 10944 | |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 10945 | /// See if we can simplify: |
| 10946 | /// X = bitcast A to B* |
| 10947 | /// Y = gep X, <...constant indices...> |
| 10948 | /// into a gep of the original struct. This is important for SROA and alias |
| 10949 | /// analysis of unions. If "A" is also a bitcast, wait for A/X to be merged. |
Chris Lattner | 5840779 | 2009-01-09 04:53:57 +0000 | [diff] [blame] | 10950 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(PtrOp)) { |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 10951 | if (!isa<BitCastInst>(BCI->getOperand(0)) && GEP.hasAllConstantIndices()) { |
| 10952 | // Determine how much the GEP moves the pointer. We are guaranteed to get |
| 10953 | // a constant back from EmitGEPOffset. |
| 10954 | ConstantInt *OffsetV = cast<ConstantInt>(EmitGEPOffset(&GEP, GEP, *this)); |
| 10955 | int64_t Offset = OffsetV->getSExtValue(); |
| 10956 | |
| 10957 | // If this GEP instruction doesn't move the pointer, just replace the GEP |
| 10958 | // with a bitcast of the real input to the dest type. |
| 10959 | if (Offset == 0) { |
| 10960 | // If the bitcast is of an allocation, and the allocation will be |
| 10961 | // converted to match the type of the cast, don't touch this. |
| 10962 | if (isa<AllocationInst>(BCI->getOperand(0))) { |
| 10963 | // See if the bitcast simplifies, if so, don't nuke this GEP yet. |
| 10964 | if (Instruction *I = visitBitCast(*BCI)) { |
| 10965 | if (I != BCI) { |
| 10966 | I->takeName(BCI); |
| 10967 | BCI->getParent()->getInstList().insert(BCI, I); |
| 10968 | ReplaceInstUsesWith(*BCI, I); |
| 10969 | } |
| 10970 | return &GEP; |
Chris Lattner | 5840779 | 2009-01-09 04:53:57 +0000 | [diff] [blame] | 10971 | } |
Chris Lattner | 5840779 | 2009-01-09 04:53:57 +0000 | [diff] [blame] | 10972 | } |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 10973 | return new BitCastInst(BCI->getOperand(0), GEP.getType()); |
Chris Lattner | 5840779 | 2009-01-09 04:53:57 +0000 | [diff] [blame] | 10974 | } |
Chris Lattner | 46cd5a1 | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 10975 | |
| 10976 | // Otherwise, if the offset is non-zero, we need to find out if there is a |
| 10977 | // field at Offset in 'A's type. If so, we can pull the cast through the |
| 10978 | // GEP. |
| 10979 | SmallVector<Value*, 8> NewIndices; |
| 10980 | const Type *InTy = |
| 10981 | cast<PointerType>(BCI->getOperand(0)->getType())->getElementType(); |
| 10982 | if (FindElementAtOffset(InTy, Offset, NewIndices, TD)) { |
| 10983 | Instruction *NGEP = |
| 10984 | GetElementPtrInst::Create(BCI->getOperand(0), NewIndices.begin(), |
| 10985 | NewIndices.end()); |
| 10986 | if (NGEP->getType() == GEP.getType()) return NGEP; |
| 10987 | InsertNewInstBefore(NGEP, GEP); |
| 10988 | NGEP->takeName(&GEP); |
| 10989 | return new BitCastInst(NGEP, GEP.getType()); |
| 10990 | } |
Chris Lattner | 5840779 | 2009-01-09 04:53:57 +0000 | [diff] [blame] | 10991 | } |
| 10992 | } |
| 10993 | |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 10994 | return 0; |
| 10995 | } |
| 10996 | |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 10997 | Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) { |
| 10998 | // 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] | 10999 | if (AI.isArrayAllocation()) { // Check C != 1 |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 11000 | if (const ConstantInt *C = dyn_cast<ConstantInt>(AI.getArraySize())) { |
| 11001 | const Type *NewTy = |
| 11002 | ArrayType::get(AI.getAllocatedType(), C->getZExtValue()); |
Chris Lattner | 0006bd7 | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 11003 | AllocationInst *New = 0; |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 11004 | |
| 11005 | // Create and insert the replacement instruction... |
| 11006 | if (isa<MallocInst>(AI)) |
Nate Begeman | 14b0529 | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 11007 | New = new MallocInst(NewTy, 0, AI.getAlignment(), AI.getName()); |
Chris Lattner | 0006bd7 | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 11008 | else { |
| 11009 | assert(isa<AllocaInst>(AI) && "Unknown type of allocation inst!"); |
Nate Begeman | 14b0529 | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 11010 | New = new AllocaInst(NewTy, 0, AI.getAlignment(), AI.getName()); |
Chris Lattner | 0006bd7 | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 11011 | } |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 11012 | |
| 11013 | InsertNewInstBefore(New, AI); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 11014 | |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 11015 | // Scan to the end of the allocation instructions, to skip over a block of |
Dale Johannesen | a891518 | 2009-03-11 22:19:43 +0000 | [diff] [blame] | 11016 | // allocas if possible...also skip interleaved debug info |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 11017 | // |
| 11018 | BasicBlock::iterator It = New; |
Dale Johannesen | a891518 | 2009-03-11 22:19:43 +0000 | [diff] [blame] | 11019 | while (isa<AllocationInst>(*It) || isa<DbgInfoIntrinsic>(*It)) ++It; |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 11020 | |
| 11021 | // Now that I is pointing to the first non-allocation-inst in the block, |
| 11022 | // insert our getelementptr instruction... |
| 11023 | // |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 11024 | Value *NullIdx = Constant::getNullValue(Type::Int32Ty); |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 11025 | Value *Idx[2]; |
| 11026 | Idx[0] = NullIdx; |
| 11027 | Idx[1] = NullIdx; |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 11028 | Value *V = GetElementPtrInst::Create(New, Idx, Idx + 2, |
| 11029 | New->getName()+".sub", It); |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 11030 | |
| 11031 | // Now make everything use the getelementptr instead of the original |
| 11032 | // allocation. |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 11033 | return ReplaceInstUsesWith(AI, V); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 11034 | } else if (isa<UndefValue>(AI.getArraySize())) { |
| 11035 | return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 11036 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 11037 | } |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 11038 | |
Dan Gohman | 6893cd7 | 2009-01-13 20:18:38 +0000 | [diff] [blame] | 11039 | if (isa<AllocaInst>(AI) && AI.getAllocatedType()->isSized()) { |
| 11040 | // If alloca'ing a zero byte object, replace the alloca with a null pointer. |
Chris Lattner | 46d232d | 2009-03-17 17:55:15 +0000 | [diff] [blame] | 11041 | // Note that we only do this for alloca's, because malloc should allocate |
| 11042 | // and return a unique pointer, even for a zero byte allocation. |
Dan Gohman | 6893cd7 | 2009-01-13 20:18:38 +0000 | [diff] [blame] | 11043 | if (TD->getTypePaddedSize(AI.getAllocatedType()) == 0) |
| 11044 | return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); |
| 11045 | |
| 11046 | // If the alignment is 0 (unspecified), assign it the preferred alignment. |
| 11047 | if (AI.getAlignment() == 0) |
| 11048 | AI.setAlignment(TD->getPrefTypeAlignment(AI.getAllocatedType())); |
| 11049 | } |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 11050 | |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 11051 | return 0; |
| 11052 | } |
| 11053 | |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 11054 | Instruction *InstCombiner::visitFreeInst(FreeInst &FI) { |
| 11055 | Value *Op = FI.getOperand(0); |
| 11056 | |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 11057 | // free undef -> unreachable. |
| 11058 | if (isa<UndefValue>(Op)) { |
| 11059 | // 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] | 11060 | new StoreInst(ConstantInt::getTrue(), |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 11061 | UndefValue::get(PointerType::getUnqual(Type::Int1Ty)), &FI); |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 11062 | return EraseInstFromFunction(FI); |
| 11063 | } |
Chris Lattner | 6fe5541 | 2007-04-14 00:20:02 +0000 | [diff] [blame] | 11064 | |
Chris Lattner | 6160e85 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 11065 | // If we have 'free null' delete the instruction. This can happen in stl code |
| 11066 | // when lots of inlining happens. |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 11067 | if (isa<ConstantPointerNull>(Op)) |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 11068 | return EraseInstFromFunction(FI); |
Chris Lattner | 6fe5541 | 2007-04-14 00:20:02 +0000 | [diff] [blame] | 11069 | |
| 11070 | // Change free <ty>* (cast <ty2>* X to <ty>*) into free <ty2>* X |
| 11071 | if (BitCastInst *CI = dyn_cast<BitCastInst>(Op)) { |
| 11072 | FI.setOperand(0, CI->getOperand(0)); |
| 11073 | return &FI; |
| 11074 | } |
| 11075 | |
| 11076 | // Change free (gep X, 0,0,0,0) into free(X) |
| 11077 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) { |
| 11078 | if (GEPI->hasAllZeroIndices()) { |
| 11079 | AddToWorkList(GEPI); |
| 11080 | FI.setOperand(0, GEPI->getOperand(0)); |
| 11081 | return &FI; |
| 11082 | } |
| 11083 | } |
| 11084 | |
| 11085 | // Change free(malloc) into nothing, if the malloc has a single use. |
| 11086 | if (MallocInst *MI = dyn_cast<MallocInst>(Op)) |
| 11087 | if (MI->hasOneUse()) { |
| 11088 | EraseInstFromFunction(FI); |
| 11089 | return EraseInstFromFunction(*MI); |
| 11090 | } |
Chris Lattner | 6160e85 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 11091 | |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 11092 | return 0; |
| 11093 | } |
| 11094 | |
| 11095 | |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 11096 | /// InstCombineLoadCast - Fold 'load (cast P)' -> cast (load P)' when possible. |
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 11097 | static Instruction *InstCombineLoadCast(InstCombiner &IC, LoadInst &LI, |
Bill Wendling | 587c01d | 2008-02-26 10:53:30 +0000 | [diff] [blame] | 11098 | const TargetData *TD) { |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 11099 | User *CI = cast<User>(LI.getOperand(0)); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 11100 | Value *CastOp = CI->getOperand(0); |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 11101 | |
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 11102 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(CI)) { |
| 11103 | // Instead of loading constant c string, use corresponding integer value |
| 11104 | // directly if string length is small enough. |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 11105 | std::string Str; |
| 11106 | if (GetConstantStringInfo(CE->getOperand(0), Str) && !Str.empty()) { |
| 11107 | unsigned len = Str.length(); |
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 11108 | const Type *Ty = cast<PointerType>(CE->getType())->getElementType(); |
| 11109 | unsigned numBits = Ty->getPrimitiveSizeInBits(); |
| 11110 | // Replace LI with immediate integer store. |
| 11111 | if ((numBits >> 3) == len + 1) { |
Bill Wendling | 587c01d | 2008-02-26 10:53:30 +0000 | [diff] [blame] | 11112 | APInt StrVal(numBits, 0); |
| 11113 | APInt SingleChar(numBits, 0); |
| 11114 | if (TD->isLittleEndian()) { |
| 11115 | for (signed i = len-1; i >= 0; i--) { |
Nick Lewycky | 2ec0dbf | 2009-02-21 20:50:42 +0000 | [diff] [blame] | 11116 | SingleChar = (uint64_t) Str[i] & UCHAR_MAX; |
Bill Wendling | 587c01d | 2008-02-26 10:53:30 +0000 | [diff] [blame] | 11117 | StrVal = (StrVal << 8) | SingleChar; |
| 11118 | } |
| 11119 | } else { |
| 11120 | for (unsigned i = 0; i < len; i++) { |
Nick Lewycky | 2ec0dbf | 2009-02-21 20:50:42 +0000 | [diff] [blame] | 11121 | SingleChar = (uint64_t) Str[i] & UCHAR_MAX; |
Bill Wendling | 587c01d | 2008-02-26 10:53:30 +0000 | [diff] [blame] | 11122 | StrVal = (StrVal << 8) | SingleChar; |
| 11123 | } |
| 11124 | // Append NULL at the end. |
| 11125 | SingleChar = 0; |
| 11126 | StrVal = (StrVal << 8) | SingleChar; |
| 11127 | } |
| 11128 | Value *NL = ConstantInt::get(StrVal); |
| 11129 | return IC.ReplaceInstUsesWith(LI, NL); |
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 11130 | } |
| 11131 | } |
| 11132 | } |
| 11133 | |
Mon P Wang | 6753f95 | 2009-02-07 22:19:29 +0000 | [diff] [blame] | 11134 | const PointerType *DestTy = cast<PointerType>(CI->getType()); |
| 11135 | const Type *DestPTy = DestTy->getElementType(); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 11136 | if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) { |
Mon P Wang | 6753f95 | 2009-02-07 22:19:29 +0000 | [diff] [blame] | 11137 | |
| 11138 | // If the address spaces don't match, don't eliminate the cast. |
| 11139 | if (DestTy->getAddressSpace() != SrcTy->getAddressSpace()) |
| 11140 | return 0; |
| 11141 | |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 11142 | const Type *SrcPTy = SrcTy->getElementType(); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 11143 | |
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 11144 | if (DestPTy->isInteger() || isa<PointerType>(DestPTy) || |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 11145 | isa<VectorType>(DestPTy)) { |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 11146 | // If the source is an array, the code below will not succeed. Check to |
| 11147 | // see if a trivial 'gep P, 0, 0' will help matters. Only do this for |
| 11148 | // constants. |
| 11149 | if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy)) |
| 11150 | if (Constant *CSrc = dyn_cast<Constant>(CastOp)) |
| 11151 | if (ASrcTy->getNumElements() != 0) { |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 11152 | Value *Idxs[2]; |
| 11153 | Idxs[0] = Idxs[1] = Constant::getNullValue(Type::Int32Ty); |
| 11154 | CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs, 2); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 11155 | SrcTy = cast<PointerType>(CastOp->getType()); |
| 11156 | SrcPTy = SrcTy->getElementType(); |
| 11157 | } |
| 11158 | |
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 11159 | if ((SrcPTy->isInteger() || isa<PointerType>(SrcPTy) || |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 11160 | isa<VectorType>(SrcPTy)) && |
Chris Lattner | b1515fe | 2005-03-29 06:37:47 +0000 | [diff] [blame] | 11161 | // Do not allow turning this into a load of an integer, which is then |
| 11162 | // casted to a pointer, this pessimizes pointer analysis a lot. |
| 11163 | (isa<PointerType>(SrcPTy) == isa<PointerType>(LI.getType())) && |
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 11164 | IC.getTargetData().getTypeSizeInBits(SrcPTy) == |
| 11165 | IC.getTargetData().getTypeSizeInBits(DestPTy)) { |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 11166 | |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 11167 | // Okay, we are casting from one integer or pointer type to another of |
| 11168 | // the same size. Instead of casting the pointer before the load, cast |
| 11169 | // the result of the loaded value. |
| 11170 | Value *NewLoad = IC.InsertNewInstBefore(new LoadInst(CastOp, |
| 11171 | CI->getName(), |
| 11172 | LI.isVolatile()),LI); |
| 11173 | // Now cast the result of the load. |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 11174 | return new BitCastInst(NewLoad, LI.getType()); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 11175 | } |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 11176 | } |
| 11177 | } |
| 11178 | return 0; |
| 11179 | } |
| 11180 | |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 11181 | /// isSafeToLoadUnconditionally - Return true if we know that executing a load |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 11182 | /// from this value cannot trap. If it is not obviously safe to load from the |
| 11183 | /// specified pointer, we do a quick local scan of the basic block containing |
| 11184 | /// ScanFrom, to determine if the address is already accessed. |
| 11185 | static bool isSafeToLoadUnconditionally(Value *V, Instruction *ScanFrom) { |
Duncan Sands | 892c7e4 | 2007-09-19 10:10:31 +0000 | [diff] [blame] | 11186 | // If it is an alloca it is always safe to load from. |
| 11187 | if (isa<AllocaInst>(V)) return true; |
| 11188 | |
Duncan Sands | 46318cd | 2007-09-19 10:25:38 +0000 | [diff] [blame] | 11189 | // 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] | 11190 | if (const GlobalValue *GV = dyn_cast<GlobalVariable>(V)) |
Duncan Sands | 46318cd | 2007-09-19 10:25:38 +0000 | [diff] [blame] | 11191 | // Don't try to evaluate aliases. External weak GV can be null. |
Duncan Sands | 892c7e4 | 2007-09-19 10:10:31 +0000 | [diff] [blame] | 11192 | return !isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage(); |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 11193 | |
| 11194 | // Otherwise, be a little bit agressive by scanning the local block where we |
| 11195 | // 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] | 11196 | // from/to. If so, the previous load or store would have already trapped, |
| 11197 | // so there is no harm doing an extra load (also, CSE will later eliminate |
| 11198 | // the load entirely). |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 11199 | BasicBlock::iterator BBI = ScanFrom, E = ScanFrom->getParent()->begin(); |
| 11200 | |
Alkis Evlogimenos | 7b6ec60 | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 11201 | while (BBI != E) { |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 11202 | --BBI; |
| 11203 | |
Chris Lattner | 2de3fec | 2008-06-20 05:12:56 +0000 | [diff] [blame] | 11204 | // If we see a free or a call (which might do a free) the pointer could be |
| 11205 | // marked invalid. |
Dale Johannesen | d1c135c | 2009-03-13 19:23:20 +0000 | [diff] [blame] | 11206 | if (isa<FreeInst>(BBI) || |
| 11207 | (isa<CallInst>(BBI) && !isa<DbgInfoIntrinsic>(BBI))) |
Chris Lattner | 2de3fec | 2008-06-20 05:12:56 +0000 | [diff] [blame] | 11208 | return false; |
| 11209 | |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 11210 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { |
| 11211 | if (LI->getOperand(0) == V) return true; |
Chris Lattner | 2de3fec | 2008-06-20 05:12:56 +0000 | [diff] [blame] | 11212 | } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) { |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 11213 | if (SI->getOperand(1) == V) return true; |
Chris Lattner | 2de3fec | 2008-06-20 05:12:56 +0000 | [diff] [blame] | 11214 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 11215 | |
Alkis Evlogimenos | 7b6ec60 | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 11216 | } |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 11217 | return false; |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 11218 | } |
| 11219 | |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 11220 | Instruction *InstCombiner::visitLoadInst(LoadInst &LI) { |
| 11221 | Value *Op = LI.getOperand(0); |
Chris Lattner | 5f16a13 | 2004-01-12 04:13:56 +0000 | [diff] [blame] | 11222 | |
Dan Gohman | 9941f74 | 2007-07-20 16:34:21 +0000 | [diff] [blame] | 11223 | // Attempt to improve the alignment. |
Dan Gohman | 926b0a2 | 2009-02-16 00:44:23 +0000 | [diff] [blame] | 11224 | unsigned KnownAlign = |
| 11225 | GetOrEnforceKnownAlignment(Op, TD->getPrefTypeAlignment(LI.getType())); |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 11226 | if (KnownAlign > |
| 11227 | (LI.getAlignment() == 0 ? TD->getABITypeAlignment(LI.getType()) : |
| 11228 | LI.getAlignment())) |
Dan Gohman | 9941f74 | 2007-07-20 16:34:21 +0000 | [diff] [blame] | 11229 | LI.setAlignment(KnownAlign); |
| 11230 | |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 11231 | // load (cast X) --> cast (load X) iff safe |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 11232 | if (isa<CastInst>(Op)) |
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 11233 | if (Instruction *Res = InstCombineLoadCast(*this, LI, TD)) |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 11234 | return Res; |
| 11235 | |
| 11236 | // None of the following transforms are legal for volatile loads. |
| 11237 | if (LI.isVolatile()) return 0; |
Chris Lattner | 62f254d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 11238 | |
Dan Gohman | 2276a7b | 2008-10-15 23:19:35 +0000 | [diff] [blame] | 11239 | // Do really simple store-to-load forwarding and load CSE, to catch cases |
| 11240 | // where there are several consequtive memory accesses to the same location, |
| 11241 | // separated by a few arithmetic operations. |
| 11242 | BasicBlock::iterator BBI = &LI; |
Chris Lattner | 4aebaee | 2008-11-27 08:56:30 +0000 | [diff] [blame] | 11243 | if (Value *AvailableVal = FindAvailableLoadedValue(Op, LI.getParent(), BBI,6)) |
| 11244 | return ReplaceInstUsesWith(LI, AvailableVal); |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 11245 | |
Christopher Lamb | b15147e | 2007-12-29 07:56:53 +0000 | [diff] [blame] | 11246 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) { |
| 11247 | const Value *GEPI0 = GEPI->getOperand(0); |
| 11248 | // TODO: Consider a target hook for valid address spaces for this xform. |
| 11249 | if (isa<ConstantPointerNull>(GEPI0) && |
| 11250 | cast<PointerType>(GEPI0->getType())->getAddressSpace() == 0) { |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 11251 | // Insert a new store to null instruction before the load to indicate |
| 11252 | // that this code is not reachable. We do this instead of inserting |
| 11253 | // an unreachable instruction directly because we cannot modify the |
| 11254 | // CFG. |
| 11255 | new StoreInst(UndefValue::get(LI.getType()), |
| 11256 | Constant::getNullValue(Op->getType()), &LI); |
| 11257 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
| 11258 | } |
Christopher Lamb | b15147e | 2007-12-29 07:56:53 +0000 | [diff] [blame] | 11259 | } |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 11260 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 11261 | if (Constant *C = dyn_cast<Constant>(Op)) { |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 11262 | // load null/undef -> undef |
Christopher Lamb | b15147e | 2007-12-29 07:56:53 +0000 | [diff] [blame] | 11263 | // TODO: Consider a target hook for valid address spaces for this xform. |
| 11264 | if (isa<UndefValue>(C) || (C->isNullValue() && |
| 11265 | cast<PointerType>(Op->getType())->getAddressSpace() == 0)) { |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 11266 | // Insert a new store to null instruction before the load to indicate that |
| 11267 | // this code is not reachable. We do this instead of inserting an |
| 11268 | // unreachable instruction directly because we cannot modify the CFG. |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 11269 | new StoreInst(UndefValue::get(LI.getType()), |
| 11270 | Constant::getNullValue(Op->getType()), &LI); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 11271 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 11272 | } |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 11273 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 11274 | // Instcombine load (constant global) into the value loaded. |
| 11275 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op)) |
Duncan Sands | 64da940 | 2009-03-21 21:27:31 +0000 | [diff] [blame] | 11276 | if (GV->isConstant() && GV->hasDefinitiveInitializer()) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 11277 | return ReplaceInstUsesWith(LI, GV->getInitializer()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 11278 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 11279 | // Instcombine load (constantexpr_GEP global, 0, ...) into the value loaded. |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 11280 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op)) { |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 11281 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 11282 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0))) |
Duncan Sands | 64da940 | 2009-03-21 21:27:31 +0000 | [diff] [blame] | 11283 | if (GV->isConstant() && GV->hasDefinitiveInitializer()) |
Chris Lattner | 363f2a2 | 2005-09-26 05:28:06 +0000 | [diff] [blame] | 11284 | if (Constant *V = |
| 11285 | ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE)) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 11286 | return ReplaceInstUsesWith(LI, V); |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 11287 | if (CE->getOperand(0)->isNullValue()) { |
| 11288 | // Insert a new store to null instruction before the load to indicate |
| 11289 | // that this code is not reachable. We do this instead of inserting |
| 11290 | // an unreachable instruction directly because we cannot modify the |
| 11291 | // CFG. |
| 11292 | new StoreInst(UndefValue::get(LI.getType()), |
| 11293 | Constant::getNullValue(Op->getType()), &LI); |
| 11294 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
| 11295 | } |
| 11296 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 11297 | } else if (CE->isCast()) { |
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 11298 | if (Instruction *Res = InstCombineLoadCast(*this, LI, TD)) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 11299 | return Res; |
| 11300 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 11301 | } |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 11302 | } |
Chris Lattner | 8d2e888 | 2007-08-11 18:48:48 +0000 | [diff] [blame] | 11303 | |
| 11304 | // If this load comes from anywhere in a constant global, and if the global |
| 11305 | // is all undef or zero, we know what it loads. |
Duncan Sands | 5d0392c | 2008-10-01 15:25:41 +0000 | [diff] [blame] | 11306 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op->getUnderlyingObject())){ |
Duncan Sands | 64da940 | 2009-03-21 21:27:31 +0000 | [diff] [blame] | 11307 | if (GV->isConstant() && GV->hasDefinitiveInitializer()) { |
Chris Lattner | 8d2e888 | 2007-08-11 18:48:48 +0000 | [diff] [blame] | 11308 | if (GV->getInitializer()->isNullValue()) |
| 11309 | return ReplaceInstUsesWith(LI, Constant::getNullValue(LI.getType())); |
| 11310 | else if (isa<UndefValue>(GV->getInitializer())) |
| 11311 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
| 11312 | } |
| 11313 | } |
Chris Lattner | f499eac | 2004-04-08 20:39:49 +0000 | [diff] [blame] | 11314 | |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 11315 | if (Op->hasOneUse()) { |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 11316 | // Change select and PHI nodes to select values instead of addresses: this |
| 11317 | // helps alias analysis out a lot, allows many others simplifications, and |
| 11318 | // exposes redundancy in the code. |
| 11319 | // |
| 11320 | // Note that we cannot do the transformation unless we know that the |
| 11321 | // introduced loads cannot trap! Something like this is valid as long as |
| 11322 | // the condition is always false: load (select bool %C, int* null, int* %G), |
| 11323 | // but it would not be valid if we transformed it to load from null |
| 11324 | // unconditionally. |
| 11325 | // |
| 11326 | if (SelectInst *SI = dyn_cast<SelectInst>(Op)) { |
| 11327 | // load (select (Cond, &V1, &V2)) --> select(Cond, load &V1, load &V2). |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 11328 | if (isSafeToLoadUnconditionally(SI->getOperand(1), SI) && |
| 11329 | isSafeToLoadUnconditionally(SI->getOperand(2), SI)) { |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 11330 | Value *V1 = InsertNewInstBefore(new LoadInst(SI->getOperand(1), |
Chris Lattner | 79f0c8e | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 11331 | SI->getOperand(1)->getName()+".val"), LI); |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 11332 | Value *V2 = InsertNewInstBefore(new LoadInst(SI->getOperand(2), |
Chris Lattner | 79f0c8e | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 11333 | SI->getOperand(2)->getName()+".val"), LI); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 11334 | return SelectInst::Create(SI->getCondition(), V1, V2); |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 11335 | } |
| 11336 | |
Chris Lattner | 684fe21 | 2004-09-23 15:46:00 +0000 | [diff] [blame] | 11337 | // load (select (cond, null, P)) -> load P |
| 11338 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(1))) |
| 11339 | if (C->isNullValue()) { |
| 11340 | LI.setOperand(0, SI->getOperand(2)); |
| 11341 | return &LI; |
| 11342 | } |
| 11343 | |
| 11344 | // load (select (cond, P, null)) -> load P |
| 11345 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(2))) |
| 11346 | if (C->isNullValue()) { |
| 11347 | LI.setOperand(0, SI->getOperand(1)); |
| 11348 | return &LI; |
| 11349 | } |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 11350 | } |
| 11351 | } |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 11352 | return 0; |
| 11353 | } |
| 11354 | |
Reid Spencer | 55af2b5 | 2007-01-19 21:20:31 +0000 | [diff] [blame] | 11355 | /// InstCombineStoreToCast - Fold store V, (cast P) -> store (cast V), P |
Chris Lattner | 3914f72 | 2009-01-24 01:00:13 +0000 | [diff] [blame] | 11356 | /// when possible. This makes it generally easy to do alias analysis and/or |
| 11357 | /// SROA/mem2reg of the memory object. |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 11358 | static Instruction *InstCombineStoreToCast(InstCombiner &IC, StoreInst &SI) { |
| 11359 | User *CI = cast<User>(SI.getOperand(1)); |
| 11360 | Value *CastOp = CI->getOperand(0); |
| 11361 | |
| 11362 | const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType(); |
Chris Lattner | 1b8eaf5 | 2009-01-16 20:08:59 +0000 | [diff] [blame] | 11363 | const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType()); |
| 11364 | if (SrcTy == 0) return 0; |
| 11365 | |
| 11366 | const Type *SrcPTy = SrcTy->getElementType(); |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 11367 | |
Chris Lattner | 1b8eaf5 | 2009-01-16 20:08:59 +0000 | [diff] [blame] | 11368 | if (!DestPTy->isInteger() && !isa<PointerType>(DestPTy)) |
| 11369 | return 0; |
| 11370 | |
Chris Lattner | 3914f72 | 2009-01-24 01:00:13 +0000 | [diff] [blame] | 11371 | /// NewGEPIndices - If SrcPTy is an aggregate type, we can emit a "noop gep" |
| 11372 | /// to its first element. This allows us to handle things like: |
| 11373 | /// store i32 xxx, (bitcast {foo*, float}* %P to i32*) |
| 11374 | /// on 32-bit hosts. |
| 11375 | SmallVector<Value*, 4> NewGEPIndices; |
| 11376 | |
Chris Lattner | 1b8eaf5 | 2009-01-16 20:08:59 +0000 | [diff] [blame] | 11377 | // If the source is an array, the code below will not succeed. Check to |
| 11378 | // see if a trivial 'gep P, 0, 0' will help matters. Only do this for |
| 11379 | // constants. |
Chris Lattner | 3914f72 | 2009-01-24 01:00:13 +0000 | [diff] [blame] | 11380 | if (isa<ArrayType>(SrcPTy) || isa<StructType>(SrcPTy)) { |
| 11381 | // Index through pointer. |
| 11382 | Constant *Zero = Constant::getNullValue(Type::Int32Ty); |
| 11383 | NewGEPIndices.push_back(Zero); |
| 11384 | |
| 11385 | while (1) { |
| 11386 | if (const StructType *STy = dyn_cast<StructType>(SrcPTy)) { |
Torok Edwin | 08ffee5 | 2009-01-24 17:16:04 +0000 | [diff] [blame] | 11387 | if (!STy->getNumElements()) /* Struct can be empty {} */ |
Torok Edwin | 629e92b | 2009-01-24 11:30:49 +0000 | [diff] [blame] | 11388 | break; |
Chris Lattner | 3914f72 | 2009-01-24 01:00:13 +0000 | [diff] [blame] | 11389 | NewGEPIndices.push_back(Zero); |
| 11390 | SrcPTy = STy->getElementType(0); |
| 11391 | } else if (const ArrayType *ATy = dyn_cast<ArrayType>(SrcPTy)) { |
| 11392 | NewGEPIndices.push_back(Zero); |
| 11393 | SrcPTy = ATy->getElementType(); |
| 11394 | } else { |
| 11395 | break; |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 11396 | } |
Chris Lattner | 3914f72 | 2009-01-24 01:00:13 +0000 | [diff] [blame] | 11397 | } |
| 11398 | |
| 11399 | SrcTy = PointerType::get(SrcPTy, SrcTy->getAddressSpace()); |
| 11400 | } |
Chris Lattner | 1b8eaf5 | 2009-01-16 20:08:59 +0000 | [diff] [blame] | 11401 | |
| 11402 | if (!SrcPTy->isInteger() && !isa<PointerType>(SrcPTy)) |
| 11403 | return 0; |
| 11404 | |
Chris Lattner | 71759c4 | 2009-01-16 20:12:52 +0000 | [diff] [blame] | 11405 | // If the pointers point into different address spaces or if they point to |
| 11406 | // values with different sizes, we can't do the transformation. |
| 11407 | if (SrcTy->getAddressSpace() != |
| 11408 | cast<PointerType>(CI->getType())->getAddressSpace() || |
| 11409 | IC.getTargetData().getTypeSizeInBits(SrcPTy) != |
Chris Lattner | 1b8eaf5 | 2009-01-16 20:08:59 +0000 | [diff] [blame] | 11410 | IC.getTargetData().getTypeSizeInBits(DestPTy)) |
| 11411 | return 0; |
| 11412 | |
| 11413 | // Okay, we are casting from one integer or pointer type to another of |
| 11414 | // the same size. Instead of casting the pointer before |
| 11415 | // the store, cast the value to be stored. |
| 11416 | Value *NewCast; |
| 11417 | Value *SIOp0 = SI.getOperand(0); |
| 11418 | Instruction::CastOps opcode = Instruction::BitCast; |
| 11419 | const Type* CastSrcTy = SIOp0->getType(); |
| 11420 | const Type* CastDstTy = SrcPTy; |
| 11421 | if (isa<PointerType>(CastDstTy)) { |
| 11422 | if (CastSrcTy->isInteger()) |
| 11423 | opcode = Instruction::IntToPtr; |
| 11424 | } else if (isa<IntegerType>(CastDstTy)) { |
| 11425 | if (isa<PointerType>(SIOp0->getType())) |
| 11426 | opcode = Instruction::PtrToInt; |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 11427 | } |
Chris Lattner | 3914f72 | 2009-01-24 01:00:13 +0000 | [diff] [blame] | 11428 | |
| 11429 | // SIOp0 is a pointer to aggregate and this is a store to the first field, |
| 11430 | // emit a GEP to index into its first field. |
| 11431 | if (!NewGEPIndices.empty()) { |
| 11432 | if (Constant *C = dyn_cast<Constant>(CastOp)) |
| 11433 | CastOp = ConstantExpr::getGetElementPtr(C, &NewGEPIndices[0], |
| 11434 | NewGEPIndices.size()); |
| 11435 | else |
| 11436 | CastOp = IC.InsertNewInstBefore( |
| 11437 | GetElementPtrInst::Create(CastOp, NewGEPIndices.begin(), |
| 11438 | NewGEPIndices.end()), SI); |
| 11439 | } |
| 11440 | |
Chris Lattner | 1b8eaf5 | 2009-01-16 20:08:59 +0000 | [diff] [blame] | 11441 | if (Constant *C = dyn_cast<Constant>(SIOp0)) |
| 11442 | NewCast = ConstantExpr::getCast(opcode, C, CastDstTy); |
| 11443 | else |
| 11444 | NewCast = IC.InsertNewInstBefore( |
| 11445 | CastInst::Create(opcode, SIOp0, CastDstTy, SIOp0->getName()+".c"), |
| 11446 | SI); |
| 11447 | return new StoreInst(NewCast, CastOp); |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 11448 | } |
| 11449 | |
Chris Lattner | 4aebaee | 2008-11-27 08:56:30 +0000 | [diff] [blame] | 11450 | /// equivalentAddressValues - Test if A and B will obviously have the same |
| 11451 | /// value. This includes recognizing that %t0 and %t1 will have the same |
| 11452 | /// value in code like this: |
Dan Gohman | 0f8b53f | 2009-03-03 02:55:14 +0000 | [diff] [blame] | 11453 | /// %t0 = getelementptr \@a, 0, 3 |
Chris Lattner | 4aebaee | 2008-11-27 08:56:30 +0000 | [diff] [blame] | 11454 | /// store i32 0, i32* %t0 |
Dan Gohman | 0f8b53f | 2009-03-03 02:55:14 +0000 | [diff] [blame] | 11455 | /// %t1 = getelementptr \@a, 0, 3 |
Chris Lattner | 4aebaee | 2008-11-27 08:56:30 +0000 | [diff] [blame] | 11456 | /// %t2 = load i32* %t1 |
| 11457 | /// |
| 11458 | static bool equivalentAddressValues(Value *A, Value *B) { |
| 11459 | // Test if the values are trivially equivalent. |
| 11460 | if (A == B) return true; |
| 11461 | |
| 11462 | // Test if the values come form identical arithmetic instructions. |
| 11463 | if (isa<BinaryOperator>(A) || |
| 11464 | isa<CastInst>(A) || |
| 11465 | isa<PHINode>(A) || |
| 11466 | isa<GetElementPtrInst>(A)) |
| 11467 | if (Instruction *BI = dyn_cast<Instruction>(B)) |
| 11468 | if (cast<Instruction>(A)->isIdenticalTo(BI)) |
| 11469 | return true; |
| 11470 | |
| 11471 | // Otherwise they may not be equivalent. |
| 11472 | return false; |
| 11473 | } |
| 11474 | |
Dale Johannesen | 4945c65 | 2009-03-03 21:26:39 +0000 | [diff] [blame] | 11475 | // If this instruction has two uses, one of which is a llvm.dbg.declare, |
| 11476 | // return the llvm.dbg.declare. |
| 11477 | DbgDeclareInst *InstCombiner::hasOneUsePlusDeclare(Value *V) { |
| 11478 | if (!V->hasNUses(2)) |
| 11479 | return 0; |
| 11480 | for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); |
| 11481 | UI != E; ++UI) { |
| 11482 | if (DbgDeclareInst *DI = dyn_cast<DbgDeclareInst>(UI)) |
| 11483 | return DI; |
| 11484 | if (isa<BitCastInst>(UI) && UI->hasOneUse()) { |
| 11485 | if (DbgDeclareInst *DI = dyn_cast<DbgDeclareInst>(UI->use_begin())) |
| 11486 | return DI; |
| 11487 | } |
| 11488 | } |
| 11489 | return 0; |
| 11490 | } |
| 11491 | |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 11492 | Instruction *InstCombiner::visitStoreInst(StoreInst &SI) { |
| 11493 | Value *Val = SI.getOperand(0); |
| 11494 | Value *Ptr = SI.getOperand(1); |
| 11495 | |
| 11496 | if (isa<UndefValue>(Ptr)) { // store X, undef -> noop (even if volatile) |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 11497 | EraseInstFromFunction(SI); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 11498 | ++NumCombined; |
| 11499 | return 0; |
| 11500 | } |
Chris Lattner | 836692d | 2007-01-15 06:51:56 +0000 | [diff] [blame] | 11501 | |
| 11502 | // If the RHS is an alloca with a single use, zapify the store, making the |
| 11503 | // alloca dead. |
Dale Johannesen | 4945c65 | 2009-03-03 21:26:39 +0000 | [diff] [blame] | 11504 | // If the RHS is an alloca with a two uses, the other one being a |
| 11505 | // llvm.dbg.declare, zapify the store and the declare, making the |
| 11506 | // alloca dead. We must do this to prevent declare's from affecting |
| 11507 | // codegen. |
| 11508 | if (!SI.isVolatile()) { |
| 11509 | if (Ptr->hasOneUse()) { |
| 11510 | if (isa<AllocaInst>(Ptr)) { |
Chris Lattner | 836692d | 2007-01-15 06:51:56 +0000 | [diff] [blame] | 11511 | EraseInstFromFunction(SI); |
| 11512 | ++NumCombined; |
| 11513 | return 0; |
| 11514 | } |
Dale Johannesen | 4945c65 | 2009-03-03 21:26:39 +0000 | [diff] [blame] | 11515 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) { |
| 11516 | if (isa<AllocaInst>(GEP->getOperand(0))) { |
| 11517 | if (GEP->getOperand(0)->hasOneUse()) { |
| 11518 | EraseInstFromFunction(SI); |
| 11519 | ++NumCombined; |
| 11520 | return 0; |
| 11521 | } |
| 11522 | if (DbgDeclareInst *DI = hasOneUsePlusDeclare(GEP->getOperand(0))) { |
| 11523 | EraseInstFromFunction(*DI); |
| 11524 | EraseInstFromFunction(SI); |
| 11525 | ++NumCombined; |
| 11526 | return 0; |
| 11527 | } |
| 11528 | } |
| 11529 | } |
| 11530 | } |
| 11531 | if (DbgDeclareInst *DI = hasOneUsePlusDeclare(Ptr)) { |
| 11532 | EraseInstFromFunction(*DI); |
| 11533 | EraseInstFromFunction(SI); |
| 11534 | ++NumCombined; |
| 11535 | return 0; |
| 11536 | } |
Chris Lattner | 836692d | 2007-01-15 06:51:56 +0000 | [diff] [blame] | 11537 | } |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 11538 | |
Dan Gohman | 9941f74 | 2007-07-20 16:34:21 +0000 | [diff] [blame] | 11539 | // Attempt to improve the alignment. |
Dan Gohman | 926b0a2 | 2009-02-16 00:44:23 +0000 | [diff] [blame] | 11540 | unsigned KnownAlign = |
| 11541 | GetOrEnforceKnownAlignment(Ptr, TD->getPrefTypeAlignment(Val->getType())); |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 11542 | if (KnownAlign > |
| 11543 | (SI.getAlignment() == 0 ? TD->getABITypeAlignment(Val->getType()) : |
| 11544 | SI.getAlignment())) |
Dan Gohman | 9941f74 | 2007-07-20 16:34:21 +0000 | [diff] [blame] | 11545 | SI.setAlignment(KnownAlign); |
| 11546 | |
Dale Johannesen | acb51a3 | 2009-03-03 01:43:03 +0000 | [diff] [blame] | 11547 | // Do really simple DSE, to catch cases where there are several consecutive |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 11548 | // stores to the same location, separated by a few arithmetic operations. This |
| 11549 | // situation often occurs with bitfield accesses. |
| 11550 | BasicBlock::iterator BBI = &SI; |
| 11551 | for (unsigned ScanInsts = 6; BBI != SI.getParent()->begin() && ScanInsts; |
| 11552 | --ScanInsts) { |
Dale Johannesen | 0d6596b | 2009-03-04 01:20:34 +0000 | [diff] [blame] | 11553 | --BBI; |
Dale Johannesen | cdb16aa | 2009-03-04 01:53:05 +0000 | [diff] [blame] | 11554 | // Don't count debug info directives, lest they affect codegen, |
| 11555 | // and we skip pointer-to-pointer bitcasts, which are NOPs. |
| 11556 | // It is necessary for correctness to skip those that feed into a |
| 11557 | // llvm.dbg.declare, as these are not present when debugging is off. |
Dale Johannesen | 4ded40a | 2009-03-03 22:36:47 +0000 | [diff] [blame] | 11558 | if (isa<DbgInfoIntrinsic>(BBI) || |
Dale Johannesen | cdb16aa | 2009-03-04 01:53:05 +0000 | [diff] [blame] | 11559 | (isa<BitCastInst>(BBI) && isa<PointerType>(BBI->getType()))) { |
Dale Johannesen | acb51a3 | 2009-03-03 01:43:03 +0000 | [diff] [blame] | 11560 | ScanInsts++; |
Dale Johannesen | acb51a3 | 2009-03-03 01:43:03 +0000 | [diff] [blame] | 11561 | continue; |
| 11562 | } |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 11563 | |
| 11564 | if (StoreInst *PrevSI = dyn_cast<StoreInst>(BBI)) { |
| 11565 | // Prev store isn't volatile, and stores to the same location? |
Chris Lattner | 4aebaee | 2008-11-27 08:56:30 +0000 | [diff] [blame] | 11566 | if (!PrevSI->isVolatile() &&equivalentAddressValues(PrevSI->getOperand(1), |
| 11567 | SI.getOperand(1))) { |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 11568 | ++NumDeadStore; |
| 11569 | ++BBI; |
| 11570 | EraseInstFromFunction(*PrevSI); |
| 11571 | continue; |
| 11572 | } |
| 11573 | break; |
| 11574 | } |
| 11575 | |
Chris Lattner | b4db97f | 2006-05-26 19:19:20 +0000 | [diff] [blame] | 11576 | // If this is a load, we have to stop. However, if the loaded value is from |
| 11577 | // the pointer we're loading and is producing the pointer we're storing, |
| 11578 | // then *this* store is dead (X = load P; store X -> P). |
| 11579 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { |
Dan Gohman | 2276a7b | 2008-10-15 23:19:35 +0000 | [diff] [blame] | 11580 | if (LI == Val && equivalentAddressValues(LI->getOperand(0), Ptr) && |
| 11581 | !SI.isVolatile()) { |
Chris Lattner | b4db97f | 2006-05-26 19:19:20 +0000 | [diff] [blame] | 11582 | EraseInstFromFunction(SI); |
| 11583 | ++NumCombined; |
| 11584 | return 0; |
| 11585 | } |
| 11586 | // Otherwise, this is a load from some other location. Stores before it |
| 11587 | // may not be dead. |
| 11588 | break; |
| 11589 | } |
| 11590 | |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 11591 | // Don't skip over loads or things that can modify memory. |
Chris Lattner | 0ef546e | 2008-05-08 17:20:30 +0000 | [diff] [blame] | 11592 | if (BBI->mayWriteToMemory() || BBI->mayReadFromMemory()) |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 11593 | break; |
| 11594 | } |
| 11595 | |
| 11596 | |
| 11597 | if (SI.isVolatile()) return 0; // Don't hack volatile stores. |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 11598 | |
| 11599 | // store X, null -> turns into 'unreachable' in SimplifyCFG |
| 11600 | if (isa<ConstantPointerNull>(Ptr)) { |
| 11601 | if (!isa<UndefValue>(Val)) { |
| 11602 | SI.setOperand(0, UndefValue::get(Val->getType())); |
| 11603 | if (Instruction *U = dyn_cast<Instruction>(Val)) |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11604 | AddToWorkList(U); // Dropped a use. |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 11605 | ++NumCombined; |
| 11606 | } |
| 11607 | return 0; // Do not modify these! |
| 11608 | } |
| 11609 | |
| 11610 | // store undef, Ptr -> noop |
| 11611 | if (isa<UndefValue>(Val)) { |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 11612 | EraseInstFromFunction(SI); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 11613 | ++NumCombined; |
| 11614 | return 0; |
| 11615 | } |
| 11616 | |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 11617 | // If the pointer destination is a cast, see if we can fold the cast into the |
| 11618 | // source instead. |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 11619 | if (isa<CastInst>(Ptr)) |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 11620 | if (Instruction *Res = InstCombineStoreToCast(*this, SI)) |
| 11621 | return Res; |
| 11622 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 11623 | if (CE->isCast()) |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 11624 | if (Instruction *Res = InstCombineStoreToCast(*this, SI)) |
| 11625 | return Res; |
| 11626 | |
Chris Lattner | 408902b | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 11627 | |
Dale Johannesen | 4084c4e | 2009-03-05 02:06:48 +0000 | [diff] [blame] | 11628 | // If this store is the last instruction in the basic block (possibly |
| 11629 | // excepting debug info instructions and the pointer bitcasts that feed |
| 11630 | // into them), and if the block ends with an unconditional branch, try |
| 11631 | // to move it to the successor block. |
| 11632 | BBI = &SI; |
| 11633 | do { |
| 11634 | ++BBI; |
| 11635 | } while (isa<DbgInfoIntrinsic>(BBI) || |
| 11636 | (isa<BitCastInst>(BBI) && isa<PointerType>(BBI->getType()))); |
Chris Lattner | 408902b | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 11637 | if (BranchInst *BI = dyn_cast<BranchInst>(BBI)) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 11638 | if (BI->isUnconditional()) |
| 11639 | if (SimplifyStoreAtEndOfBlock(SI)) |
| 11640 | return 0; // xform done! |
Chris Lattner | 408902b | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 11641 | |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 11642 | return 0; |
| 11643 | } |
| 11644 | |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 11645 | /// SimplifyStoreAtEndOfBlock - Turn things like: |
| 11646 | /// if () { *P = v1; } else { *P = v2 } |
| 11647 | /// into a phi node with a store in the successor. |
| 11648 | /// |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 11649 | /// Simplify things like: |
| 11650 | /// *P = v1; if () { *P = v2; } |
| 11651 | /// into a phi node with a store in the successor. |
| 11652 | /// |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 11653 | bool InstCombiner::SimplifyStoreAtEndOfBlock(StoreInst &SI) { |
| 11654 | BasicBlock *StoreBB = SI.getParent(); |
| 11655 | |
| 11656 | // Check to see if the successor block has exactly two incoming edges. If |
| 11657 | // so, see if the other predecessor contains a store to the same location. |
| 11658 | // 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] | 11659 | BasicBlock *DestBB = StoreBB->getTerminator()->getSuccessor(0); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 11660 | |
| 11661 | // Determine whether Dest has exactly two predecessors and, if so, compute |
| 11662 | // the other predecessor. |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 11663 | pred_iterator PI = pred_begin(DestBB); |
| 11664 | BasicBlock *OtherBB = 0; |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 11665 | if (*PI != StoreBB) |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 11666 | OtherBB = *PI; |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 11667 | ++PI; |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 11668 | if (PI == pred_end(DestBB)) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 11669 | return false; |
| 11670 | |
| 11671 | if (*PI != StoreBB) { |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 11672 | if (OtherBB) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 11673 | return false; |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 11674 | OtherBB = *PI; |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 11675 | } |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 11676 | if (++PI != pred_end(DestBB)) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 11677 | return false; |
Eli Friedman | 66fe80a | 2008-06-13 21:17:49 +0000 | [diff] [blame] | 11678 | |
| 11679 | // Bail out if all the relevant blocks aren't distinct (this can happen, |
| 11680 | // for example, if SI is in an infinite loop) |
| 11681 | if (StoreBB == DestBB || OtherBB == DestBB) |
| 11682 | return false; |
| 11683 | |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 11684 | // Verify that the other block ends in a branch and is not otherwise empty. |
| 11685 | BasicBlock::iterator BBI = OtherBB->getTerminator(); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 11686 | BranchInst *OtherBr = dyn_cast<BranchInst>(BBI); |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 11687 | if (!OtherBr || BBI == OtherBB->begin()) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 11688 | return false; |
| 11689 | |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 11690 | // If the other block ends in an unconditional branch, check for the 'if then |
| 11691 | // else' case. there is an instruction before the branch. |
| 11692 | StoreInst *OtherStore = 0; |
| 11693 | if (OtherBr->isUnconditional()) { |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 11694 | --BBI; |
Dale Johannesen | 4084c4e | 2009-03-05 02:06:48 +0000 | [diff] [blame] | 11695 | // Skip over debugging info. |
| 11696 | while (isa<DbgInfoIntrinsic>(BBI) || |
| 11697 | (isa<BitCastInst>(BBI) && isa<PointerType>(BBI->getType()))) { |
| 11698 | if (BBI==OtherBB->begin()) |
| 11699 | return false; |
| 11700 | --BBI; |
| 11701 | } |
| 11702 | // If this isn't a store, or isn't a store to the same location, bail out. |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 11703 | OtherStore = dyn_cast<StoreInst>(BBI); |
| 11704 | if (!OtherStore || OtherStore->getOperand(1) != SI.getOperand(1)) |
| 11705 | return false; |
| 11706 | } else { |
Chris Lattner | d717c18 | 2007-05-05 22:32:24 +0000 | [diff] [blame] | 11707 | // 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] | 11708 | // destinations is StoreBB, then we have the if/then case. |
| 11709 | if (OtherBr->getSuccessor(0) != StoreBB && |
| 11710 | OtherBr->getSuccessor(1) != StoreBB) |
| 11711 | return false; |
| 11712 | |
| 11713 | // 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] | 11714 | // if/then triangle. See if there is a store to the same ptr as SI that |
| 11715 | // lives in OtherBB. |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 11716 | for (;; --BBI) { |
| 11717 | // Check to see if we find the matching store. |
| 11718 | if ((OtherStore = dyn_cast<StoreInst>(BBI))) { |
| 11719 | if (OtherStore->getOperand(1) != SI.getOperand(1)) |
| 11720 | return false; |
| 11721 | break; |
| 11722 | } |
Eli Friedman | 6903a24 | 2008-06-13 22:02:12 +0000 | [diff] [blame] | 11723 | // If we find something that may be using or overwriting the stored |
| 11724 | // value, or if we run out of instructions, we can't do the xform. |
| 11725 | if (BBI->mayReadFromMemory() || BBI->mayWriteToMemory() || |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 11726 | BBI == OtherBB->begin()) |
| 11727 | return false; |
| 11728 | } |
| 11729 | |
| 11730 | // In order to eliminate the store in OtherBr, we have to |
Eli Friedman | 6903a24 | 2008-06-13 22:02:12 +0000 | [diff] [blame] | 11731 | // make sure nothing reads or overwrites the stored value in |
| 11732 | // StoreBB. |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 11733 | for (BasicBlock::iterator I = StoreBB->begin(); &*I != &SI; ++I) { |
| 11734 | // FIXME: This should really be AA driven. |
Eli Friedman | 6903a24 | 2008-06-13 22:02:12 +0000 | [diff] [blame] | 11735 | if (I->mayReadFromMemory() || I->mayWriteToMemory()) |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 11736 | return false; |
| 11737 | } |
| 11738 | } |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 11739 | |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 11740 | // Insert a PHI node now if we need it. |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 11741 | Value *MergedVal = OtherStore->getOperand(0); |
| 11742 | if (MergedVal != SI.getOperand(0)) { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 11743 | PHINode *PN = PHINode::Create(MergedVal->getType(), "storemerge"); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 11744 | PN->reserveOperandSpace(2); |
| 11745 | PN->addIncoming(SI.getOperand(0), SI.getParent()); |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 11746 | PN->addIncoming(OtherStore->getOperand(0), OtherBB); |
| 11747 | MergedVal = InsertNewInstBefore(PN, DestBB->front()); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 11748 | } |
| 11749 | |
| 11750 | // Advance to a place where it is safe to insert the new store and |
| 11751 | // insert it. |
Dan Gohman | 02dea8b | 2008-05-23 21:05:58 +0000 | [diff] [blame] | 11752 | BBI = DestBB->getFirstNonPHI(); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 11753 | InsertNewInstBefore(new StoreInst(MergedVal, SI.getOperand(1), |
| 11754 | OtherStore->isVolatile()), *BBI); |
| 11755 | |
| 11756 | // Nuke the old stores. |
| 11757 | EraseInstFromFunction(SI); |
| 11758 | EraseInstFromFunction(*OtherStore); |
| 11759 | ++NumCombined; |
| 11760 | return true; |
| 11761 | } |
| 11762 | |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 11763 | |
Chris Lattner | c4d10eb | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 11764 | Instruction *InstCombiner::visitBranchInst(BranchInst &BI) { |
| 11765 | // 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] | 11766 | Value *X = 0; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 11767 | BasicBlock *TrueDest; |
| 11768 | BasicBlock *FalseDest; |
| 11769 | if (match(&BI, m_Br(m_Not(m_Value(X)), TrueDest, FalseDest)) && |
| 11770 | !isa<Constant>(X)) { |
| 11771 | // Swap Destinations and condition... |
| 11772 | BI.setCondition(X); |
| 11773 | BI.setSuccessor(0, FalseDest); |
| 11774 | BI.setSuccessor(1, TrueDest); |
| 11775 | return &BI; |
| 11776 | } |
| 11777 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 11778 | // Cannonicalize fcmp_one -> fcmp_oeq |
| 11779 | FCmpInst::Predicate FPred; Value *Y; |
| 11780 | if (match(&BI, m_Br(m_FCmp(FPred, m_Value(X), m_Value(Y)), |
| 11781 | TrueDest, FalseDest))) |
| 11782 | if ((FPred == FCmpInst::FCMP_ONE || FPred == FCmpInst::FCMP_OLE || |
| 11783 | FPred == FCmpInst::FCMP_OGE) && BI.getCondition()->hasOneUse()) { |
| 11784 | FCmpInst *I = cast<FCmpInst>(BI.getCondition()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 11785 | FCmpInst::Predicate NewPred = FCmpInst::getInversePredicate(FPred); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 11786 | Instruction *NewSCC = new FCmpInst(NewPred, X, Y, "", I); |
| 11787 | NewSCC->takeName(I); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 11788 | // Swap Destinations and condition... |
| 11789 | BI.setCondition(NewSCC); |
| 11790 | BI.setSuccessor(0, FalseDest); |
| 11791 | BI.setSuccessor(1, TrueDest); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11792 | RemoveFromWorkList(I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 11793 | I->eraseFromParent(); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11794 | AddToWorkList(NewSCC); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 11795 | return &BI; |
| 11796 | } |
| 11797 | |
| 11798 | // Cannonicalize icmp_ne -> icmp_eq |
| 11799 | ICmpInst::Predicate IPred; |
| 11800 | if (match(&BI, m_Br(m_ICmp(IPred, m_Value(X), m_Value(Y)), |
| 11801 | TrueDest, FalseDest))) |
| 11802 | if ((IPred == ICmpInst::ICMP_NE || IPred == ICmpInst::ICMP_ULE || |
| 11803 | IPred == ICmpInst::ICMP_SLE || IPred == ICmpInst::ICMP_UGE || |
| 11804 | IPred == ICmpInst::ICMP_SGE) && BI.getCondition()->hasOneUse()) { |
| 11805 | ICmpInst *I = cast<ICmpInst>(BI.getCondition()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 11806 | ICmpInst::Predicate NewPred = ICmpInst::getInversePredicate(IPred); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 11807 | Instruction *NewSCC = new ICmpInst(NewPred, X, Y, "", I); |
| 11808 | NewSCC->takeName(I); |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 11809 | // Swap Destinations and condition... |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 11810 | BI.setCondition(NewSCC); |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 11811 | BI.setSuccessor(0, FalseDest); |
| 11812 | BI.setSuccessor(1, TrueDest); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11813 | RemoveFromWorkList(I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 11814 | I->eraseFromParent();; |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11815 | AddToWorkList(NewSCC); |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 11816 | return &BI; |
| 11817 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 11818 | |
Chris Lattner | c4d10eb | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 11819 | return 0; |
| 11820 | } |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 11821 | |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 11822 | Instruction *InstCombiner::visitSwitchInst(SwitchInst &SI) { |
| 11823 | Value *Cond = SI.getCondition(); |
| 11824 | if (Instruction *I = dyn_cast<Instruction>(Cond)) { |
| 11825 | if (I->getOpcode() == Instruction::Add) |
| 11826 | if (ConstantInt *AddRHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 11827 | // change 'switch (X+4) case 1:' into 'switch (X) case -3' |
| 11828 | for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 11829 | SI.setOperand(i,ConstantExpr::getSub(cast<Constant>(SI.getOperand(i)), |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 11830 | AddRHS)); |
| 11831 | SI.setOperand(0, I->getOperand(0)); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11832 | AddToWorkList(I); |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 11833 | return &SI; |
| 11834 | } |
| 11835 | } |
| 11836 | return 0; |
| 11837 | } |
| 11838 | |
Matthijs Kooijman | a9012ec | 2008-06-11 14:05:05 +0000 | [diff] [blame] | 11839 | Instruction *InstCombiner::visitExtractValueInst(ExtractValueInst &EV) { |
Matthijs Kooijman | 780ae5e | 2008-07-16 12:55:45 +0000 | [diff] [blame] | 11840 | Value *Agg = EV.getAggregateOperand(); |
Matthijs Kooijman | a9012ec | 2008-06-11 14:05:05 +0000 | [diff] [blame] | 11841 | |
Matthijs Kooijman | 780ae5e | 2008-07-16 12:55:45 +0000 | [diff] [blame] | 11842 | if (!EV.hasIndices()) |
| 11843 | return ReplaceInstUsesWith(EV, Agg); |
| 11844 | |
| 11845 | if (Constant *C = dyn_cast<Constant>(Agg)) { |
| 11846 | if (isa<UndefValue>(C)) |
| 11847 | return ReplaceInstUsesWith(EV, UndefValue::get(EV.getType())); |
| 11848 | |
| 11849 | if (isa<ConstantAggregateZero>(C)) |
| 11850 | return ReplaceInstUsesWith(EV, Constant::getNullValue(EV.getType())); |
| 11851 | |
| 11852 | if (isa<ConstantArray>(C) || isa<ConstantStruct>(C)) { |
| 11853 | // Extract the element indexed by the first index out of the constant |
| 11854 | Value *V = C->getOperand(*EV.idx_begin()); |
| 11855 | if (EV.getNumIndices() > 1) |
| 11856 | // Extract the remaining indices out of the constant indexed by the |
| 11857 | // first index |
| 11858 | return ExtractValueInst::Create(V, EV.idx_begin() + 1, EV.idx_end()); |
| 11859 | else |
| 11860 | return ReplaceInstUsesWith(EV, V); |
| 11861 | } |
| 11862 | return 0; // Can't handle other constants |
| 11863 | } |
| 11864 | if (InsertValueInst *IV = dyn_cast<InsertValueInst>(Agg)) { |
| 11865 | // We're extracting from an insertvalue instruction, compare the indices |
| 11866 | const unsigned *exti, *exte, *insi, *inse; |
| 11867 | for (exti = EV.idx_begin(), insi = IV->idx_begin(), |
| 11868 | exte = EV.idx_end(), inse = IV->idx_end(); |
| 11869 | exti != exte && insi != inse; |
| 11870 | ++exti, ++insi) { |
| 11871 | if (*insi != *exti) |
| 11872 | // The insert and extract both reference distinctly different elements. |
| 11873 | // This means the extract is not influenced by the insert, and we can |
| 11874 | // replace the aggregate operand of the extract with the aggregate |
| 11875 | // operand of the insert. i.e., replace |
| 11876 | // %I = insertvalue { i32, { i32 } } %A, { i32 } { i32 42 }, 1 |
| 11877 | // %E = extractvalue { i32, { i32 } } %I, 0 |
| 11878 | // with |
| 11879 | // %E = extractvalue { i32, { i32 } } %A, 0 |
| 11880 | return ExtractValueInst::Create(IV->getAggregateOperand(), |
| 11881 | EV.idx_begin(), EV.idx_end()); |
| 11882 | } |
| 11883 | if (exti == exte && insi == inse) |
| 11884 | // Both iterators are at the end: Index lists are identical. Replace |
| 11885 | // %B = insertvalue { i32, { i32 } } %A, i32 42, 1, 0 |
| 11886 | // %C = extractvalue { i32, { i32 } } %B, 1, 0 |
| 11887 | // with "i32 42" |
| 11888 | return ReplaceInstUsesWith(EV, IV->getInsertedValueOperand()); |
| 11889 | if (exti == exte) { |
| 11890 | // The extract list is a prefix of the insert list. i.e. replace |
| 11891 | // %I = insertvalue { i32, { i32 } } %A, i32 42, 1, 0 |
| 11892 | // %E = extractvalue { i32, { i32 } } %I, 1 |
| 11893 | // with |
| 11894 | // %X = extractvalue { i32, { i32 } } %A, 1 |
| 11895 | // %E = insertvalue { i32 } %X, i32 42, 0 |
| 11896 | // by switching the order of the insert and extract (though the |
| 11897 | // insertvalue should be left in, since it may have other uses). |
| 11898 | Value *NewEV = InsertNewInstBefore( |
| 11899 | ExtractValueInst::Create(IV->getAggregateOperand(), |
| 11900 | EV.idx_begin(), EV.idx_end()), |
| 11901 | EV); |
| 11902 | return InsertValueInst::Create(NewEV, IV->getInsertedValueOperand(), |
| 11903 | insi, inse); |
| 11904 | } |
| 11905 | if (insi == inse) |
| 11906 | // The insert list is a prefix of the extract list |
| 11907 | // We can simply remove the common indices from the extract and make it |
| 11908 | // operate on the inserted value instead of the insertvalue result. |
| 11909 | // i.e., replace |
| 11910 | // %I = insertvalue { i32, { i32 } } %A, { i32 } { i32 42 }, 1 |
| 11911 | // %E = extractvalue { i32, { i32 } } %I, 1, 0 |
| 11912 | // with |
| 11913 | // %E extractvalue { i32 } { i32 42 }, 0 |
| 11914 | return ExtractValueInst::Create(IV->getInsertedValueOperand(), |
| 11915 | exti, exte); |
| 11916 | } |
| 11917 | // Can't simplify extracts from other values. Note that nested extracts are |
| 11918 | // already simplified implicitely by the above (extract ( extract (insert) ) |
| 11919 | // will be translated into extract ( insert ( extract ) ) first and then just |
| 11920 | // the value inserted, if appropriate). |
Matthijs Kooijman | a9012ec | 2008-06-11 14:05:05 +0000 | [diff] [blame] | 11921 | return 0; |
| 11922 | } |
| 11923 | |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 11924 | /// CheapToScalarize - Return true if the value is cheaper to scalarize than it |
| 11925 | /// is to leave as a vector operation. |
| 11926 | static bool CheapToScalarize(Value *V, bool isConstant) { |
| 11927 | if (isa<ConstantAggregateZero>(V)) |
| 11928 | return true; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 11929 | if (ConstantVector *C = dyn_cast<ConstantVector>(V)) { |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 11930 | if (isConstant) return true; |
| 11931 | // If all elts are the same, we can extract. |
| 11932 | Constant *Op0 = C->getOperand(0); |
| 11933 | for (unsigned i = 1; i < C->getNumOperands(); ++i) |
| 11934 | if (C->getOperand(i) != Op0) |
| 11935 | return false; |
| 11936 | return true; |
| 11937 | } |
| 11938 | Instruction *I = dyn_cast<Instruction>(V); |
| 11939 | if (!I) return false; |
| 11940 | |
| 11941 | // Insert element gets simplified to the inserted element or is deleted if |
| 11942 | // this is constant idx extract element and its a constant idx insertelt. |
| 11943 | if (I->getOpcode() == Instruction::InsertElement && isConstant && |
| 11944 | isa<ConstantInt>(I->getOperand(2))) |
| 11945 | return true; |
| 11946 | if (I->getOpcode() == Instruction::Load && I->hasOneUse()) |
| 11947 | return true; |
| 11948 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) |
| 11949 | if (BO->hasOneUse() && |
| 11950 | (CheapToScalarize(BO->getOperand(0), isConstant) || |
| 11951 | CheapToScalarize(BO->getOperand(1), isConstant))) |
| 11952 | return true; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 11953 | if (CmpInst *CI = dyn_cast<CmpInst>(I)) |
| 11954 | if (CI->hasOneUse() && |
| 11955 | (CheapToScalarize(CI->getOperand(0), isConstant) || |
| 11956 | CheapToScalarize(CI->getOperand(1), isConstant))) |
| 11957 | return true; |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 11958 | |
| 11959 | return false; |
| 11960 | } |
| 11961 | |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 11962 | /// Read and decode a shufflevector mask. |
| 11963 | /// |
| 11964 | /// It turns undef elements into values that are larger than the number of |
| 11965 | /// elements in the input. |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 11966 | static std::vector<unsigned> getShuffleMask(const ShuffleVectorInst *SVI) { |
| 11967 | unsigned NElts = SVI->getType()->getNumElements(); |
| 11968 | if (isa<ConstantAggregateZero>(SVI->getOperand(2))) |
| 11969 | return std::vector<unsigned>(NElts, 0); |
| 11970 | if (isa<UndefValue>(SVI->getOperand(2))) |
| 11971 | return std::vector<unsigned>(NElts, 2*NElts); |
| 11972 | |
| 11973 | std::vector<unsigned> Result; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 11974 | const ConstantVector *CP = cast<ConstantVector>(SVI->getOperand(2)); |
Gabor Greif | 177dd3f | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 11975 | for (User::const_op_iterator i = CP->op_begin(), e = CP->op_end(); i!=e; ++i) |
| 11976 | if (isa<UndefValue>(*i)) |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 11977 | Result.push_back(NElts*2); // undef -> 8 |
| 11978 | else |
Gabor Greif | 177dd3f | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 11979 | Result.push_back(cast<ConstantInt>(*i)->getZExtValue()); |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 11980 | return Result; |
| 11981 | } |
| 11982 | |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 11983 | /// FindScalarElement - Given a vector and an element number, see if the scalar |
| 11984 | /// value is already around as a register, for example if it were inserted then |
| 11985 | /// extracted from the vector. |
| 11986 | static Value *FindScalarElement(Value *V, unsigned EltNo) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 11987 | assert(isa<VectorType>(V->getType()) && "Not looking at a vector?"); |
| 11988 | const VectorType *PTy = cast<VectorType>(V->getType()); |
Chris Lattner | 389a6f5 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 11989 | unsigned Width = PTy->getNumElements(); |
| 11990 | if (EltNo >= Width) // Out of range access. |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 11991 | return UndefValue::get(PTy->getElementType()); |
| 11992 | |
| 11993 | if (isa<UndefValue>(V)) |
| 11994 | return UndefValue::get(PTy->getElementType()); |
| 11995 | else if (isa<ConstantAggregateZero>(V)) |
| 11996 | return Constant::getNullValue(PTy->getElementType()); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 11997 | else if (ConstantVector *CP = dyn_cast<ConstantVector>(V)) |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 11998 | return CP->getOperand(EltNo); |
| 11999 | else if (InsertElementInst *III = dyn_cast<InsertElementInst>(V)) { |
| 12000 | // 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] | 12001 | if (!isa<ConstantInt>(III->getOperand(2))) |
| 12002 | return 0; |
| 12003 | unsigned IIElt = cast<ConstantInt>(III->getOperand(2))->getZExtValue(); |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 12004 | |
| 12005 | // If this is an insert to the element we are looking for, return the |
| 12006 | // inserted value. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 12007 | if (EltNo == IIElt) |
| 12008 | return III->getOperand(1); |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 12009 | |
| 12010 | // Otherwise, the insertelement doesn't modify the value, recurse on its |
| 12011 | // vector input. |
| 12012 | return FindScalarElement(III->getOperand(0), EltNo); |
Chris Lattner | 389a6f5 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 12013 | } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(V)) { |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 12014 | unsigned LHSWidth = |
| 12015 | cast<VectorType>(SVI->getOperand(0)->getType())->getNumElements(); |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 12016 | unsigned InEl = getShuffleMask(SVI)[EltNo]; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 12017 | if (InEl < LHSWidth) |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 12018 | return FindScalarElement(SVI->getOperand(0), InEl); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 12019 | else if (InEl < LHSWidth*2) |
| 12020 | return FindScalarElement(SVI->getOperand(1), InEl - LHSWidth); |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 12021 | else |
| 12022 | return UndefValue::get(PTy->getElementType()); |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 12023 | } |
| 12024 | |
| 12025 | // Otherwise, we don't know. |
| 12026 | return 0; |
| 12027 | } |
| 12028 | |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 12029 | Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) { |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 12030 | // If vector val is undef, replace extract with scalar undef. |
Chris Lattner | 1f13c88 | 2006-03-31 18:25:14 +0000 | [diff] [blame] | 12031 | if (isa<UndefValue>(EI.getOperand(0))) |
| 12032 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); |
| 12033 | |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 12034 | // If vector val is constant 0, replace extract with scalar 0. |
Chris Lattner | 1f13c88 | 2006-03-31 18:25:14 +0000 | [diff] [blame] | 12035 | if (isa<ConstantAggregateZero>(EI.getOperand(0))) |
| 12036 | return ReplaceInstUsesWith(EI, Constant::getNullValue(EI.getType())); |
| 12037 | |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 12038 | if (ConstantVector *C = dyn_cast<ConstantVector>(EI.getOperand(0))) { |
Matthijs Kooijman | b4d6a5a | 2008-06-11 09:00:12 +0000 | [diff] [blame] | 12039 | // If vector val is constant with all elements the same, replace EI with |
| 12040 | // that element. When the elements are not identical, we cannot replace yet |
| 12041 | // (we do that below, but only when the index is constant). |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 12042 | Constant *op0 = C->getOperand(0); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 12043 | for (unsigned i = 1; i < C->getNumOperands(); ++i) |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 12044 | if (C->getOperand(i) != op0) { |
| 12045 | op0 = 0; |
| 12046 | break; |
| 12047 | } |
| 12048 | if (op0) |
| 12049 | return ReplaceInstUsesWith(EI, op0); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 12050 | } |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 12051 | |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 12052 | // If extracting a specified index from the vector, see if we can recursively |
| 12053 | // find a previously computed scalar that was inserted into the vector. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 12054 | if (ConstantInt *IdxC = dyn_cast<ConstantInt>(EI.getOperand(1))) { |
Chris Lattner | 8546409 | 2007-04-09 01:37:55 +0000 | [diff] [blame] | 12055 | unsigned IndexVal = IdxC->getZExtValue(); |
| 12056 | unsigned VectorWidth = |
| 12057 | cast<VectorType>(EI.getOperand(0)->getType())->getNumElements(); |
| 12058 | |
| 12059 | // If this is extracting an invalid index, turn this into undef, to avoid |
| 12060 | // crashing the code below. |
| 12061 | if (IndexVal >= VectorWidth) |
| 12062 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); |
| 12063 | |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 12064 | // This instruction only demands the single element from the input vector. |
| 12065 | // If the input vector has a single use, simplify it based on this use |
| 12066 | // property. |
Chris Lattner | 8546409 | 2007-04-09 01:37:55 +0000 | [diff] [blame] | 12067 | if (EI.getOperand(0)->hasOneUse() && VectorWidth != 1) { |
Evan Cheng | 388df62 | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 12068 | APInt UndefElts(VectorWidth, 0); |
| 12069 | APInt DemandedMask(VectorWidth, 1 << IndexVal); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 12070 | if (Value *V = SimplifyDemandedVectorElts(EI.getOperand(0), |
Evan Cheng | 388df62 | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 12071 | DemandedMask, UndefElts)) { |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 12072 | EI.setOperand(0, V); |
| 12073 | return &EI; |
| 12074 | } |
| 12075 | } |
| 12076 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 12077 | if (Value *Elt = FindScalarElement(EI.getOperand(0), IndexVal)) |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 12078 | return ReplaceInstUsesWith(EI, Elt); |
Chris Lattner | b7300fa | 2007-04-14 23:02:14 +0000 | [diff] [blame] | 12079 | |
| 12080 | // If the this extractelement is directly using a bitcast from a vector of |
| 12081 | // the same number of elements, see if we can find the source element from |
| 12082 | // it. In this case, we will end up needing to bitcast the scalars. |
| 12083 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(EI.getOperand(0))) { |
| 12084 | if (const VectorType *VT = |
| 12085 | dyn_cast<VectorType>(BCI->getOperand(0)->getType())) |
| 12086 | if (VT->getNumElements() == VectorWidth) |
| 12087 | if (Value *Elt = FindScalarElement(BCI->getOperand(0), IndexVal)) |
| 12088 | return new BitCastInst(Elt, EI.getType()); |
| 12089 | } |
Chris Lattner | 389a6f5 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 12090 | } |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 12091 | |
Chris Lattner | 73fa49d | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 12092 | if (Instruction *I = dyn_cast<Instruction>(EI.getOperand(0))) { |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 12093 | if (I->hasOneUse()) { |
| 12094 | // Push extractelement into predecessor operation if legal and |
| 12095 | // profitable to do so |
| 12096 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) { |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 12097 | bool isConstantElt = isa<ConstantInt>(EI.getOperand(1)); |
| 12098 | if (CheapToScalarize(BO, isConstantElt)) { |
| 12099 | ExtractElementInst *newEI0 = |
| 12100 | new ExtractElementInst(BO->getOperand(0), EI.getOperand(1), |
| 12101 | EI.getName()+".lhs"); |
| 12102 | ExtractElementInst *newEI1 = |
| 12103 | new ExtractElementInst(BO->getOperand(1), EI.getOperand(1), |
| 12104 | EI.getName()+".rhs"); |
| 12105 | InsertNewInstBefore(newEI0, EI); |
| 12106 | InsertNewInstBefore(newEI1, EI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 12107 | return BinaryOperator::Create(BO->getOpcode(), newEI0, newEI1); |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 12108 | } |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 12109 | } else if (isa<LoadInst>(I)) { |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 12110 | unsigned AS = |
| 12111 | cast<PointerType>(I->getOperand(0)->getType())->getAddressSpace(); |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 12112 | Value *Ptr = InsertBitCastBefore(I->getOperand(0), |
| 12113 | PointerType::get(EI.getType(), AS),EI); |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 12114 | GetElementPtrInst *GEP = |
| 12115 | GetElementPtrInst::Create(Ptr, EI.getOperand(1), I->getName()+".gep"); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 12116 | InsertNewInstBefore(GEP, EI); |
| 12117 | return new LoadInst(GEP); |
Chris Lattner | 73fa49d | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 12118 | } |
| 12119 | } |
| 12120 | if (InsertElementInst *IE = dyn_cast<InsertElementInst>(I)) { |
| 12121 | // Extracting the inserted element? |
| 12122 | if (IE->getOperand(2) == EI.getOperand(1)) |
| 12123 | return ReplaceInstUsesWith(EI, IE->getOperand(1)); |
| 12124 | // If the inserted and extracted elements are constants, they must not |
| 12125 | // be the same value, extract from the pre-inserted value instead. |
| 12126 | if (isa<Constant>(IE->getOperand(2)) && |
| 12127 | isa<Constant>(EI.getOperand(1))) { |
| 12128 | AddUsesToWorkList(EI); |
| 12129 | EI.setOperand(0, IE->getOperand(0)); |
| 12130 | return &EI; |
| 12131 | } |
| 12132 | } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(I)) { |
| 12133 | // If this is extracting an element from a shufflevector, figure out where |
| 12134 | // it came from and extract from the appropriate input element instead. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 12135 | if (ConstantInt *Elt = dyn_cast<ConstantInt>(EI.getOperand(1))) { |
| 12136 | unsigned SrcIdx = getShuffleMask(SVI)[Elt->getZExtValue()]; |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 12137 | Value *Src; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 12138 | unsigned LHSWidth = |
| 12139 | cast<VectorType>(SVI->getOperand(0)->getType())->getNumElements(); |
| 12140 | |
| 12141 | if (SrcIdx < LHSWidth) |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 12142 | Src = SVI->getOperand(0); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 12143 | else if (SrcIdx < LHSWidth*2) { |
| 12144 | SrcIdx -= LHSWidth; |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 12145 | Src = SVI->getOperand(1); |
| 12146 | } else { |
| 12147 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); |
Chris Lattner | df084ff | 2006-03-30 22:02:40 +0000 | [diff] [blame] | 12148 | } |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 12149 | return new ExtractElementInst(Src, SrcIdx); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 12150 | } |
| 12151 | } |
Chris Lattner | 73fa49d | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 12152 | } |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 12153 | return 0; |
| 12154 | } |
| 12155 | |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 12156 | /// CollectSingleShuffleElements - If V is a shuffle of values that ONLY returns |
| 12157 | /// elements from either LHS or RHS, return the shuffle mask and true. |
| 12158 | /// Otherwise, return false. |
| 12159 | static bool CollectSingleShuffleElements(Value *V, Value *LHS, Value *RHS, |
| 12160 | std::vector<Constant*> &Mask) { |
| 12161 | assert(V->getType() == LHS->getType() && V->getType() == RHS->getType() && |
| 12162 | "Invalid CollectSingleShuffleElements"); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 12163 | unsigned NumElts = cast<VectorType>(V->getType())->getNumElements(); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 12164 | |
| 12165 | if (isa<UndefValue>(V)) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 12166 | Mask.assign(NumElts, UndefValue::get(Type::Int32Ty)); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 12167 | return true; |
| 12168 | } else if (V == LHS) { |
| 12169 | for (unsigned i = 0; i != NumElts; ++i) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 12170 | Mask.push_back(ConstantInt::get(Type::Int32Ty, i)); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 12171 | return true; |
| 12172 | } else if (V == RHS) { |
| 12173 | for (unsigned i = 0; i != NumElts; ++i) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 12174 | Mask.push_back(ConstantInt::get(Type::Int32Ty, i+NumElts)); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 12175 | return true; |
| 12176 | } else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) { |
| 12177 | // If this is an insert of an extract from some other vector, include it. |
| 12178 | Value *VecOp = IEI->getOperand(0); |
| 12179 | Value *ScalarOp = IEI->getOperand(1); |
| 12180 | Value *IdxOp = IEI->getOperand(2); |
| 12181 | |
Chris Lattner | d929f06 | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 12182 | if (!isa<ConstantInt>(IdxOp)) |
| 12183 | return false; |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 12184 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Chris Lattner | d929f06 | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 12185 | |
| 12186 | if (isa<UndefValue>(ScalarOp)) { // inserting undef into vector. |
| 12187 | // Okay, we can handle this if the vector we are insertinting into is |
| 12188 | // transitively ok. |
| 12189 | if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) { |
| 12190 | // If so, update the mask to reflect the inserted undef. |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 12191 | Mask[InsertedIdx] = UndefValue::get(Type::Int32Ty); |
Chris Lattner | d929f06 | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 12192 | return true; |
| 12193 | } |
| 12194 | } else if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)){ |
| 12195 | if (isa<ConstantInt>(EI->getOperand(1)) && |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 12196 | EI->getOperand(0)->getType() == V->getType()) { |
| 12197 | unsigned ExtractedIdx = |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 12198 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 12199 | |
| 12200 | // This must be extracting from either LHS or RHS. |
| 12201 | if (EI->getOperand(0) == LHS || EI->getOperand(0) == RHS) { |
| 12202 | // Okay, we can handle this if the vector we are insertinting into is |
| 12203 | // transitively ok. |
| 12204 | if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) { |
| 12205 | // If so, update the mask to reflect the inserted value. |
| 12206 | if (EI->getOperand(0) == LHS) { |
Mon P Wang | 4f5ca2c | 2008-08-20 02:23:25 +0000 | [diff] [blame] | 12207 | Mask[InsertedIdx % NumElts] = |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 12208 | ConstantInt::get(Type::Int32Ty, ExtractedIdx); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 12209 | } else { |
| 12210 | assert(EI->getOperand(0) == RHS); |
Mon P Wang | 4f5ca2c | 2008-08-20 02:23:25 +0000 | [diff] [blame] | 12211 | Mask[InsertedIdx % NumElts] = |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 12212 | ConstantInt::get(Type::Int32Ty, ExtractedIdx+NumElts); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 12213 | |
| 12214 | } |
| 12215 | return true; |
| 12216 | } |
| 12217 | } |
| 12218 | } |
| 12219 | } |
| 12220 | } |
| 12221 | // TODO: Handle shufflevector here! |
| 12222 | |
| 12223 | return false; |
| 12224 | } |
| 12225 | |
| 12226 | /// CollectShuffleElements - We are building a shuffle of V, using RHS as the |
| 12227 | /// RHS of the shuffle instruction, if it is not null. Return a shuffle mask |
| 12228 | /// that computes V and the LHS value of the shuffle. |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 12229 | static Value *CollectShuffleElements(Value *V, std::vector<Constant*> &Mask, |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 12230 | Value *&RHS) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 12231 | assert(isa<VectorType>(V->getType()) && |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 12232 | (RHS == 0 || V->getType() == RHS->getType()) && |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 12233 | "Invalid shuffle!"); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 12234 | unsigned NumElts = cast<VectorType>(V->getType())->getNumElements(); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 12235 | |
| 12236 | if (isa<UndefValue>(V)) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 12237 | Mask.assign(NumElts, UndefValue::get(Type::Int32Ty)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 12238 | return V; |
| 12239 | } else if (isa<ConstantAggregateZero>(V)) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 12240 | Mask.assign(NumElts, ConstantInt::get(Type::Int32Ty, 0)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 12241 | return V; |
| 12242 | } else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) { |
| 12243 | // If this is an insert of an extract from some other vector, include it. |
| 12244 | Value *VecOp = IEI->getOperand(0); |
| 12245 | Value *ScalarOp = IEI->getOperand(1); |
| 12246 | Value *IdxOp = IEI->getOperand(2); |
| 12247 | |
| 12248 | if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) { |
| 12249 | if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) && |
| 12250 | EI->getOperand(0)->getType() == V->getType()) { |
| 12251 | unsigned ExtractedIdx = |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 12252 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
| 12253 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 12254 | |
| 12255 | // Either the extracted from or inserted into vector must be RHSVec, |
| 12256 | // otherwise we'd end up with a shuffle of three inputs. |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 12257 | if (EI->getOperand(0) == RHS || RHS == 0) { |
| 12258 | RHS = EI->getOperand(0); |
| 12259 | Value *V = CollectShuffleElements(VecOp, Mask, RHS); |
Mon P Wang | 4f5ca2c | 2008-08-20 02:23:25 +0000 | [diff] [blame] | 12260 | Mask[InsertedIdx % NumElts] = |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 12261 | ConstantInt::get(Type::Int32Ty, NumElts+ExtractedIdx); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 12262 | return V; |
| 12263 | } |
| 12264 | |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 12265 | if (VecOp == RHS) { |
| 12266 | Value *V = CollectShuffleElements(EI->getOperand(0), Mask, RHS); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 12267 | // Everything but the extracted element is replaced with the RHS. |
| 12268 | for (unsigned i = 0; i != NumElts; ++i) { |
| 12269 | if (i != InsertedIdx) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 12270 | Mask[i] = ConstantInt::get(Type::Int32Ty, NumElts+i); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 12271 | } |
| 12272 | return V; |
| 12273 | } |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 12274 | |
| 12275 | // If this insertelement is a chain that comes from exactly these two |
| 12276 | // vectors, return the vector and the effective shuffle. |
| 12277 | if (CollectSingleShuffleElements(IEI, EI->getOperand(0), RHS, Mask)) |
| 12278 | return EI->getOperand(0); |
| 12279 | |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 12280 | } |
| 12281 | } |
| 12282 | } |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 12283 | // TODO: Handle shufflevector here! |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 12284 | |
| 12285 | // Otherwise, can't do anything fancy. Return an identity vector. |
| 12286 | for (unsigned i = 0; i != NumElts; ++i) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 12287 | Mask.push_back(ConstantInt::get(Type::Int32Ty, i)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 12288 | return V; |
| 12289 | } |
| 12290 | |
| 12291 | Instruction *InstCombiner::visitInsertElementInst(InsertElementInst &IE) { |
| 12292 | Value *VecOp = IE.getOperand(0); |
| 12293 | Value *ScalarOp = IE.getOperand(1); |
| 12294 | Value *IdxOp = IE.getOperand(2); |
| 12295 | |
Chris Lattner | 599ded1 | 2007-04-09 01:11:16 +0000 | [diff] [blame] | 12296 | // Inserting an undef or into an undefined place, remove this. |
| 12297 | if (isa<UndefValue>(ScalarOp) || isa<UndefValue>(IdxOp)) |
| 12298 | ReplaceInstUsesWith(IE, VecOp); |
| 12299 | |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 12300 | // If the inserted element was extracted from some other vector, and if the |
| 12301 | // indexes are constant, try to turn this into a shufflevector operation. |
| 12302 | if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) { |
| 12303 | if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) && |
| 12304 | EI->getOperand(0)->getType() == IE.getType()) { |
| 12305 | unsigned NumVectorElts = IE.getType()->getNumElements(); |
Chris Lattner | e34e9a2 | 2007-04-14 23:32:02 +0000 | [diff] [blame] | 12306 | unsigned ExtractedIdx = |
| 12307 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 12308 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 12309 | |
| 12310 | if (ExtractedIdx >= NumVectorElts) // Out of range extract. |
| 12311 | return ReplaceInstUsesWith(IE, VecOp); |
| 12312 | |
| 12313 | if (InsertedIdx >= NumVectorElts) // Out of range insert. |
| 12314 | return ReplaceInstUsesWith(IE, UndefValue::get(IE.getType())); |
| 12315 | |
| 12316 | // If we are extracting a value from a vector, then inserting it right |
| 12317 | // back into the same place, just use the input vector. |
| 12318 | if (EI->getOperand(0) == VecOp && ExtractedIdx == InsertedIdx) |
| 12319 | return ReplaceInstUsesWith(IE, VecOp); |
| 12320 | |
| 12321 | // We could theoretically do this for ANY input. However, doing so could |
| 12322 | // turn chains of insertelement instructions into a chain of shufflevector |
| 12323 | // instructions, and right now we do not merge shufflevectors. As such, |
| 12324 | // only do this in a situation where it is clear that there is benefit. |
| 12325 | if (isa<UndefValue>(VecOp) || isa<ConstantAggregateZero>(VecOp)) { |
| 12326 | // Turn this into shuffle(EIOp0, VecOp, Mask). The result has all of |
| 12327 | // the values of VecOp, except then one read from EIOp0. |
| 12328 | // Build a new shuffle mask. |
| 12329 | std::vector<Constant*> Mask; |
| 12330 | if (isa<UndefValue>(VecOp)) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 12331 | Mask.assign(NumVectorElts, UndefValue::get(Type::Int32Ty)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 12332 | else { |
| 12333 | assert(isa<ConstantAggregateZero>(VecOp) && "Unknown thing"); |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 12334 | Mask.assign(NumVectorElts, ConstantInt::get(Type::Int32Ty, |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 12335 | NumVectorElts)); |
| 12336 | } |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 12337 | Mask[InsertedIdx] = ConstantInt::get(Type::Int32Ty, ExtractedIdx); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 12338 | return new ShuffleVectorInst(EI->getOperand(0), VecOp, |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 12339 | ConstantVector::get(Mask)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 12340 | } |
| 12341 | |
| 12342 | // If this insertelement isn't used by some other insertelement, turn it |
| 12343 | // (and any insertelements it points to), into one big shuffle. |
| 12344 | if (!IE.hasOneUse() || !isa<InsertElementInst>(IE.use_back())) { |
| 12345 | std::vector<Constant*> Mask; |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 12346 | Value *RHS = 0; |
| 12347 | Value *LHS = CollectShuffleElements(&IE, Mask, RHS); |
| 12348 | if (RHS == 0) RHS = UndefValue::get(LHS->getType()); |
| 12349 | // We now have a shuffle of LHS, RHS, Mask. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 12350 | return new ShuffleVectorInst(LHS, RHS, ConstantVector::get(Mask)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 12351 | } |
| 12352 | } |
| 12353 | } |
| 12354 | |
| 12355 | return 0; |
| 12356 | } |
| 12357 | |
| 12358 | |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 12359 | Instruction *InstCombiner::visitShuffleVectorInst(ShuffleVectorInst &SVI) { |
| 12360 | Value *LHS = SVI.getOperand(0); |
| 12361 | Value *RHS = SVI.getOperand(1); |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 12362 | std::vector<unsigned> Mask = getShuffleMask(&SVI); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 12363 | |
| 12364 | bool MadeChange = false; |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 12365 | |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 12366 | // Undefined shuffle mask -> undefined value. |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 12367 | if (isa<UndefValue>(SVI.getOperand(2))) |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 12368 | return ReplaceInstUsesWith(SVI, UndefValue::get(SVI.getType())); |
Dan Gohman | 488fbfc | 2008-09-09 18:11:14 +0000 | [diff] [blame] | 12369 | |
Dan Gohman | 488fbfc | 2008-09-09 18:11:14 +0000 | [diff] [blame] | 12370 | unsigned VWidth = cast<VectorType>(SVI.getType())->getNumElements(); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 12371 | |
| 12372 | if (VWidth != cast<VectorType>(LHS->getType())->getNumElements()) |
| 12373 | return 0; |
| 12374 | |
Evan Cheng | 388df62 | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 12375 | APInt UndefElts(VWidth, 0); |
| 12376 | APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth)); |
| 12377 | if (SimplifyDemandedVectorElts(&SVI, AllOnesEltMask, UndefElts)) { |
Dan Gohman | 3139ff8 | 2008-09-11 22:47:57 +0000 | [diff] [blame] | 12378 | LHS = SVI.getOperand(0); |
| 12379 | RHS = SVI.getOperand(1); |
Dan Gohman | 488fbfc | 2008-09-09 18:11:14 +0000 | [diff] [blame] | 12380 | MadeChange = true; |
Dan Gohman | 3139ff8 | 2008-09-11 22:47:57 +0000 | [diff] [blame] | 12381 | } |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 12382 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 12383 | // Canonicalize shuffle(x ,x,mask) -> shuffle(x, undef,mask') |
| 12384 | // Canonicalize shuffle(undef,x,mask) -> shuffle(x, undef,mask'). |
| 12385 | if (LHS == RHS || isa<UndefValue>(LHS)) { |
| 12386 | if (isa<UndefValue>(LHS) && LHS == RHS) { |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 12387 | // shuffle(undef,undef,mask) -> undef. |
| 12388 | return ReplaceInstUsesWith(SVI, LHS); |
| 12389 | } |
| 12390 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 12391 | // Remap any references to RHS to use LHS. |
| 12392 | std::vector<Constant*> Elts; |
| 12393 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 12394 | if (Mask[i] >= 2*e) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 12395 | Elts.push_back(UndefValue::get(Type::Int32Ty)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 12396 | else { |
| 12397 | if ((Mask[i] >= e && isa<UndefValue>(RHS)) || |
Dan Gohman | 4ce9627 | 2008-08-06 18:17:32 +0000 | [diff] [blame] | 12398 | (Mask[i] < e && isa<UndefValue>(LHS))) { |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 12399 | Mask[i] = 2*e; // Turn into undef. |
Dan Gohman | 4ce9627 | 2008-08-06 18:17:32 +0000 | [diff] [blame] | 12400 | Elts.push_back(UndefValue::get(Type::Int32Ty)); |
| 12401 | } else { |
Mon P Wang | 4f5ca2c | 2008-08-20 02:23:25 +0000 | [diff] [blame] | 12402 | Mask[i] = Mask[i] % e; // Force to LHS. |
Dan Gohman | 4ce9627 | 2008-08-06 18:17:32 +0000 | [diff] [blame] | 12403 | Elts.push_back(ConstantInt::get(Type::Int32Ty, Mask[i])); |
| 12404 | } |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 12405 | } |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 12406 | } |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 12407 | SVI.setOperand(0, SVI.getOperand(1)); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 12408 | SVI.setOperand(1, UndefValue::get(RHS->getType())); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 12409 | SVI.setOperand(2, ConstantVector::get(Elts)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 12410 | LHS = SVI.getOperand(0); |
| 12411 | RHS = SVI.getOperand(1); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 12412 | MadeChange = true; |
| 12413 | } |
| 12414 | |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 12415 | // Analyze the shuffle, are the LHS or RHS and identity shuffles? |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 12416 | bool isLHSID = true, isRHSID = true; |
Chris Lattner | 706126d | 2006-04-16 00:03:56 +0000 | [diff] [blame] | 12417 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 12418 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
| 12419 | if (Mask[i] >= e*2) continue; // Ignore undef values. |
| 12420 | // Is this an identity shuffle of the LHS value? |
| 12421 | isLHSID &= (Mask[i] == i); |
| 12422 | |
| 12423 | // Is this an identity shuffle of the RHS value? |
| 12424 | isRHSID &= (Mask[i]-e == i); |
Chris Lattner | 706126d | 2006-04-16 00:03:56 +0000 | [diff] [blame] | 12425 | } |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 12426 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 12427 | // Eliminate identity shuffles. |
| 12428 | if (isLHSID) return ReplaceInstUsesWith(SVI, LHS); |
| 12429 | if (isRHSID) return ReplaceInstUsesWith(SVI, RHS); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 12430 | |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 12431 | // If the LHS is a shufflevector itself, see if we can combine it with this |
| 12432 | // one without producing an unusual shuffle. Here we are really conservative: |
| 12433 | // we are absolutely afraid of producing a shuffle mask not in the input |
| 12434 | // program, because the code gen may not be smart enough to turn a merged |
| 12435 | // shuffle into two specific shuffles: it may produce worse code. As such, |
| 12436 | // we only merge two shuffles if the result is one of the two input shuffle |
| 12437 | // masks. In this case, merging the shuffles just removes one instruction, |
| 12438 | // which we know is safe. This is good for things like turning: |
| 12439 | // (splat(splat)) -> splat. |
| 12440 | if (ShuffleVectorInst *LHSSVI = dyn_cast<ShuffleVectorInst>(LHS)) { |
| 12441 | if (isa<UndefValue>(RHS)) { |
| 12442 | std::vector<unsigned> LHSMask = getShuffleMask(LHSSVI); |
| 12443 | |
| 12444 | std::vector<unsigned> NewMask; |
| 12445 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) |
| 12446 | if (Mask[i] >= 2*e) |
| 12447 | NewMask.push_back(2*e); |
| 12448 | else |
| 12449 | NewMask.push_back(LHSMask[Mask[i]]); |
| 12450 | |
| 12451 | // If the result mask is equal to the src shuffle or this shuffle mask, do |
| 12452 | // the replacement. |
| 12453 | if (NewMask == LHSMask || NewMask == Mask) { |
Mon P Wang | fe6d2cd | 2009-01-26 04:39:00 +0000 | [diff] [blame] | 12454 | unsigned LHSInNElts = |
| 12455 | cast<VectorType>(LHSSVI->getOperand(0)->getType())->getNumElements(); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 12456 | std::vector<Constant*> Elts; |
| 12457 | for (unsigned i = 0, e = NewMask.size(); i != e; ++i) { |
Mon P Wang | fe6d2cd | 2009-01-26 04:39:00 +0000 | [diff] [blame] | 12458 | if (NewMask[i] >= LHSInNElts*2) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 12459 | Elts.push_back(UndefValue::get(Type::Int32Ty)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 12460 | } else { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 12461 | Elts.push_back(ConstantInt::get(Type::Int32Ty, NewMask[i])); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 12462 | } |
| 12463 | } |
| 12464 | return new ShuffleVectorInst(LHSSVI->getOperand(0), |
| 12465 | LHSSVI->getOperand(1), |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 12466 | ConstantVector::get(Elts)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 12467 | } |
| 12468 | } |
| 12469 | } |
Chris Lattner | c5eff44 | 2007-01-30 22:32:46 +0000 | [diff] [blame] | 12470 | |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 12471 | return MadeChange ? &SVI : 0; |
| 12472 | } |
| 12473 | |
| 12474 | |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 12475 | |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 12476 | |
| 12477 | /// TryToSinkInstruction - Try to move the specified instruction from its |
| 12478 | /// current block into the beginning of DestBlock, which can only happen if it's |
| 12479 | /// safe to move the instruction past all of the instructions between it and the |
| 12480 | /// end of its block. |
| 12481 | static bool TryToSinkInstruction(Instruction *I, BasicBlock *DestBlock) { |
| 12482 | assert(I->hasOneUse() && "Invariants didn't hold!"); |
| 12483 | |
Chris Lattner | 108e902 | 2005-10-27 17:13:11 +0000 | [diff] [blame] | 12484 | // Cannot move control-flow-involving, volatile loads, vaarg, etc. |
Chris Lattner | bfc538c | 2008-05-09 15:07:33 +0000 | [diff] [blame] | 12485 | if (isa<PHINode>(I) || I->mayWriteToMemory() || isa<TerminatorInst>(I)) |
| 12486 | return false; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 12487 | |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 12488 | // Do not sink alloca instructions out of the entry block. |
Dan Gohman | ecb7a77 | 2007-03-22 16:38:57 +0000 | [diff] [blame] | 12489 | if (isa<AllocaInst>(I) && I->getParent() == |
| 12490 | &DestBlock->getParent()->getEntryBlock()) |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 12491 | return false; |
| 12492 | |
Chris Lattner | 96a52a6 | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 12493 | // We can only sink load instructions if there is nothing between the load and |
| 12494 | // the end of block that could change the value. |
Chris Lattner | 2539e33 | 2008-05-08 17:37:37 +0000 | [diff] [blame] | 12495 | if (I->mayReadFromMemory()) { |
| 12496 | for (BasicBlock::iterator Scan = I, E = I->getParent()->end(); |
Chris Lattner | 96a52a6 | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 12497 | Scan != E; ++Scan) |
| 12498 | if (Scan->mayWriteToMemory()) |
| 12499 | return false; |
Chris Lattner | 96a52a6 | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 12500 | } |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 12501 | |
Dan Gohman | 02dea8b | 2008-05-23 21:05:58 +0000 | [diff] [blame] | 12502 | BasicBlock::iterator InsertPos = DestBlock->getFirstNonPHI(); |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 12503 | |
Dale Johannesen | bd8e650 | 2009-03-03 01:09:07 +0000 | [diff] [blame] | 12504 | CopyPrecedingStopPoint(I, InsertPos); |
Chris Lattner | 4bc5f80 | 2005-08-08 19:11:57 +0000 | [diff] [blame] | 12505 | I->moveBefore(InsertPos); |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 12506 | ++NumSunkInst; |
| 12507 | return true; |
| 12508 | } |
| 12509 | |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 12510 | |
| 12511 | /// AddReachableCodeToWorklist - Walk the function in depth-first order, adding |
| 12512 | /// all reachable code to the worklist. |
| 12513 | /// |
| 12514 | /// This has a couple of tricks to make the code faster and more powerful. In |
| 12515 | /// particular, we constant fold and DCE instructions as we go, to avoid adding |
| 12516 | /// them to the worklist (this significantly speeds up instcombine on code where |
| 12517 | /// many instructions are dead or constant). Additionally, if we find a branch |
| 12518 | /// whose condition is a known constant, we only visit the reachable successors. |
| 12519 | /// |
| 12520 | static void AddReachableCodeToWorklist(BasicBlock *BB, |
Chris Lattner | 1f87a58 | 2007-02-15 19:41:52 +0000 | [diff] [blame] | 12521 | SmallPtrSet<BasicBlock*, 64> &Visited, |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 12522 | InstCombiner &IC, |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 12523 | const TargetData *TD) { |
Chris Lattner | 2806dff | 2008-08-15 04:03:01 +0000 | [diff] [blame] | 12524 | SmallVector<BasicBlock*, 256> Worklist; |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 12525 | Worklist.push_back(BB); |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 12526 | |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 12527 | while (!Worklist.empty()) { |
| 12528 | BB = Worklist.back(); |
| 12529 | Worklist.pop_back(); |
| 12530 | |
| 12531 | // We have now visited this block! If we've already been here, ignore it. |
| 12532 | if (!Visited.insert(BB)) continue; |
Devang Patel | 7fe1dec | 2008-11-19 18:56:50 +0000 | [diff] [blame] | 12533 | |
| 12534 | DbgInfoIntrinsic *DBI_Prev = NULL; |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 12535 | for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ) { |
| 12536 | Instruction *Inst = BBI++; |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 12537 | |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 12538 | // DCE instruction if trivially dead. |
| 12539 | if (isInstructionTriviallyDead(Inst)) { |
| 12540 | ++NumDeadInst; |
| 12541 | DOUT << "IC: DCE: " << *Inst; |
| 12542 | Inst->eraseFromParent(); |
| 12543 | continue; |
| 12544 | } |
| 12545 | |
| 12546 | // ConstantProp instruction if trivially constant. |
| 12547 | if (Constant *C = ConstantFoldInstruction(Inst, TD)) { |
| 12548 | DOUT << "IC: ConstFold to: " << *C << " from: " << *Inst; |
| 12549 | Inst->replaceAllUsesWith(C); |
| 12550 | ++NumConstProp; |
| 12551 | Inst->eraseFromParent(); |
| 12552 | continue; |
| 12553 | } |
Chris Lattner | 3ccc6bc | 2007-07-20 22:06:41 +0000 | [diff] [blame] | 12554 | |
Devang Patel | 7fe1dec | 2008-11-19 18:56:50 +0000 | [diff] [blame] | 12555 | // If there are two consecutive llvm.dbg.stoppoint calls then |
| 12556 | // it is likely that the optimizer deleted code in between these |
| 12557 | // two intrinsics. |
| 12558 | DbgInfoIntrinsic *DBI_Next = dyn_cast<DbgInfoIntrinsic>(Inst); |
| 12559 | if (DBI_Next) { |
| 12560 | if (DBI_Prev |
| 12561 | && DBI_Prev->getIntrinsicID() == llvm::Intrinsic::dbg_stoppoint |
| 12562 | && DBI_Next->getIntrinsicID() == llvm::Intrinsic::dbg_stoppoint) { |
| 12563 | IC.RemoveFromWorkList(DBI_Prev); |
| 12564 | DBI_Prev->eraseFromParent(); |
| 12565 | } |
| 12566 | DBI_Prev = DBI_Next; |
Zhou Sheng | 8313ef4 | 2009-02-23 10:14:11 +0000 | [diff] [blame] | 12567 | } else { |
| 12568 | DBI_Prev = 0; |
Devang Patel | 7fe1dec | 2008-11-19 18:56:50 +0000 | [diff] [blame] | 12569 | } |
| 12570 | |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 12571 | IC.AddToWorkList(Inst); |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 12572 | } |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 12573 | |
| 12574 | // Recursively visit successors. If this is a branch or switch on a |
| 12575 | // constant, only visit the reachable successor. |
| 12576 | TerminatorInst *TI = BB->getTerminator(); |
| 12577 | if (BranchInst *BI = dyn_cast<BranchInst>(TI)) { |
| 12578 | if (BI->isConditional() && isa<ConstantInt>(BI->getCondition())) { |
| 12579 | bool CondVal = cast<ConstantInt>(BI->getCondition())->getZExtValue(); |
Nick Lewycky | 9143699 | 2008-03-09 08:50:23 +0000 | [diff] [blame] | 12580 | BasicBlock *ReachableBB = BI->getSuccessor(!CondVal); |
Nick Lewycky | 280a6e6 | 2008-04-25 16:53:59 +0000 | [diff] [blame] | 12581 | Worklist.push_back(ReachableBB); |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 12582 | continue; |
| 12583 | } |
| 12584 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) { |
| 12585 | if (ConstantInt *Cond = dyn_cast<ConstantInt>(SI->getCondition())) { |
| 12586 | // See if this is an explicit destination. |
| 12587 | for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i) |
| 12588 | if (SI->getCaseValue(i) == Cond) { |
Nick Lewycky | 9143699 | 2008-03-09 08:50:23 +0000 | [diff] [blame] | 12589 | BasicBlock *ReachableBB = SI->getSuccessor(i); |
Nick Lewycky | 280a6e6 | 2008-04-25 16:53:59 +0000 | [diff] [blame] | 12590 | Worklist.push_back(ReachableBB); |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 12591 | continue; |
| 12592 | } |
| 12593 | |
| 12594 | // Otherwise it is the default destination. |
| 12595 | Worklist.push_back(SI->getSuccessor(0)); |
| 12596 | continue; |
| 12597 | } |
| 12598 | } |
| 12599 | |
| 12600 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) |
| 12601 | Worklist.push_back(TI->getSuccessor(i)); |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 12602 | } |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 12603 | } |
| 12604 | |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 12605 | bool InstCombiner::DoOneIteration(Function &F, unsigned Iteration) { |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 12606 | bool Changed = false; |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 12607 | TD = &getAnalysis<TargetData>(); |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 12608 | |
| 12609 | DEBUG(DOUT << "\n\nINSTCOMBINE ITERATION #" << Iteration << " on " |
| 12610 | << F.getNameStr() << "\n"); |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 12611 | |
Chris Lattner | b3d5970 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 12612 | { |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 12613 | // Do a depth-first traversal of the function, populate the worklist with |
| 12614 | // the reachable instructions. Ignore blocks that are not reachable. Keep |
| 12615 | // track of which blocks we visit. |
Chris Lattner | 1f87a58 | 2007-02-15 19:41:52 +0000 | [diff] [blame] | 12616 | SmallPtrSet<BasicBlock*, 64> Visited; |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 12617 | AddReachableCodeToWorklist(F.begin(), Visited, *this, TD); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 12618 | |
Chris Lattner | b3d5970 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 12619 | // Do a quick scan over the function. If we find any blocks that are |
| 12620 | // unreachable, remove any instructions inside of them. This prevents |
| 12621 | // the instcombine code from having to deal with some bad special cases. |
| 12622 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 12623 | if (!Visited.count(BB)) { |
| 12624 | Instruction *Term = BB->getTerminator(); |
| 12625 | while (Term != BB->begin()) { // Remove instrs bottom-up |
| 12626 | BasicBlock::iterator I = Term; --I; |
Chris Lattner | 6ffe551 | 2004-04-27 15:13:33 +0000 | [diff] [blame] | 12627 | |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 12628 | DOUT << "IC: DCE: " << *I; |
Dale Johannesen | ff278b1 | 2009-03-10 21:19:49 +0000 | [diff] [blame] | 12629 | // A debug intrinsic shouldn't force another iteration if we weren't |
| 12630 | // going to do one without it. |
| 12631 | if (!isa<DbgInfoIntrinsic>(I)) { |
| 12632 | ++NumDeadInst; |
| 12633 | Changed = true; |
| 12634 | } |
Chris Lattner | b3d5970 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 12635 | if (!I->use_empty()) |
| 12636 | I->replaceAllUsesWith(UndefValue::get(I->getType())); |
| 12637 | I->eraseFromParent(); |
| 12638 | } |
| 12639 | } |
| 12640 | } |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 12641 | |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 12642 | while (!Worklist.empty()) { |
| 12643 | Instruction *I = RemoveOneFromWorkList(); |
| 12644 | if (I == 0) continue; // skip null values. |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 12645 | |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 12646 | // Check to see if we can DCE the instruction. |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 12647 | if (isInstructionTriviallyDead(I)) { |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 12648 | // Add operands to the worklist. |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 12649 | if (I->getNumOperands() < 4) |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 12650 | AddUsesToWorkList(*I); |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 12651 | ++NumDeadInst; |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 12652 | |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 12653 | DOUT << "IC: DCE: " << *I; |
Chris Lattner | ad5fec1 | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 12654 | |
| 12655 | I->eraseFromParent(); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 12656 | RemoveFromWorkList(I); |
Chris Lattner | 1e19d60 | 2009-01-31 07:04:22 +0000 | [diff] [blame] | 12657 | Changed = true; |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 12658 | continue; |
| 12659 | } |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 12660 | |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 12661 | // Instruction isn't dead, see if we can constant propagate it. |
Chris Lattner | 0a19ffa | 2007-01-30 23:16:15 +0000 | [diff] [blame] | 12662 | if (Constant *C = ConstantFoldInstruction(I, TD)) { |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 12663 | DOUT << "IC: ConstFold to: " << *C << " from: " << *I; |
Chris Lattner | ad5fec1 | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 12664 | |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 12665 | // Add operands to the worklist. |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 12666 | AddUsesToWorkList(*I); |
Chris Lattner | c736d56 | 2002-12-05 22:41:53 +0000 | [diff] [blame] | 12667 | ReplaceInstUsesWith(*I, C); |
| 12668 | |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 12669 | ++NumConstProp; |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 12670 | I->eraseFromParent(); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 12671 | RemoveFromWorkList(I); |
Chris Lattner | 1e19d60 | 2009-01-31 07:04:22 +0000 | [diff] [blame] | 12672 | Changed = true; |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 12673 | continue; |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 12674 | } |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 12675 | |
Nick Lewycky | 3dfd7bf | 2008-05-25 20:56:15 +0000 | [diff] [blame] | 12676 | if (TD && I->getType()->getTypeID() == Type::VoidTyID) { |
| 12677 | // See if we can constant fold its operands. |
Chris Lattner | 1e19d60 | 2009-01-31 07:04:22 +0000 | [diff] [blame] | 12678 | for (User::op_iterator i = I->op_begin(), e = I->op_end(); i != e; ++i) |
| 12679 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(i)) |
Nick Lewycky | 3dfd7bf | 2008-05-25 20:56:15 +0000 | [diff] [blame] | 12680 | if (Constant *NewC = ConstantFoldConstantExpression(CE, TD)) |
Chris Lattner | 1e19d60 | 2009-01-31 07:04:22 +0000 | [diff] [blame] | 12681 | if (NewC != CE) { |
| 12682 | i->set(NewC); |
| 12683 | Changed = true; |
| 12684 | } |
Nick Lewycky | 3dfd7bf | 2008-05-25 20:56:15 +0000 | [diff] [blame] | 12685 | } |
| 12686 | |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 12687 | // See if we can trivially sink this instruction to a successor basic block. |
Dan Gohman | fc74abf | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 12688 | if (I->hasOneUse()) { |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 12689 | BasicBlock *BB = I->getParent(); |
| 12690 | BasicBlock *UserParent = cast<Instruction>(I->use_back())->getParent(); |
| 12691 | if (UserParent != BB) { |
| 12692 | bool UserIsSuccessor = false; |
| 12693 | // See if the user is one of our successors. |
| 12694 | for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI) |
| 12695 | if (*SI == UserParent) { |
| 12696 | UserIsSuccessor = true; |
| 12697 | break; |
| 12698 | } |
| 12699 | |
| 12700 | // If the user is one of our immediate successors, and if that successor |
| 12701 | // only has us as a predecessors (we'd have to split the critical edge |
| 12702 | // otherwise), we can keep going. |
| 12703 | if (UserIsSuccessor && !isa<PHINode>(I->use_back()) && |
| 12704 | next(pred_begin(UserParent)) == pred_end(UserParent)) |
| 12705 | // Okay, the CFG is simple enough, try to sink this instruction. |
| 12706 | Changed |= TryToSinkInstruction(I, UserParent); |
| 12707 | } |
| 12708 | } |
| 12709 | |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 12710 | // Now that we have an instruction, try combining it to simplify it... |
Reid Spencer | a9b8101 | 2007-03-26 17:44:01 +0000 | [diff] [blame] | 12711 | #ifndef NDEBUG |
| 12712 | std::string OrigI; |
| 12713 | #endif |
| 12714 | DEBUG(std::ostringstream SS; I->print(SS); OrigI = SS.str();); |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 12715 | if (Instruction *Result = visit(*I)) { |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 12716 | ++NumCombined; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 12717 | // Should we replace the old instruction with a new one? |
Chris Lattner | b3bc8fa | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 12718 | if (Result != I) { |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 12719 | DOUT << "IC: Old = " << *I |
| 12720 | << " New = " << *Result; |
Chris Lattner | 0cea42a | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 12721 | |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 12722 | // Everything uses the new instruction now. |
| 12723 | I->replaceAllUsesWith(Result); |
| 12724 | |
| 12725 | // Push the new instruction and any users onto the worklist. |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 12726 | AddToWorkList(Result); |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 12727 | AddUsersToWorkList(*Result); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 12728 | |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 12729 | // Move the name to the new instruction first. |
| 12730 | Result->takeName(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 12731 | |
| 12732 | // Insert the new instruction into the basic block... |
| 12733 | BasicBlock *InstParent = I->getParent(); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 12734 | BasicBlock::iterator InsertPos = I; |
| 12735 | |
| 12736 | if (!isa<PHINode>(Result)) // If combining a PHI, don't insert |
| 12737 | while (isa<PHINode>(InsertPos)) // middle of a block of PHIs. |
| 12738 | ++InsertPos; |
| 12739 | |
| 12740 | InstParent->getInstList().insert(InsertPos, Result); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 12741 | |
Chris Lattner | 00d5131 | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 12742 | // Make sure that we reprocess all operands now that we reduced their |
| 12743 | // use counts. |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 12744 | AddUsesToWorkList(*I); |
Chris Lattner | 216d4d8 | 2004-05-01 23:19:52 +0000 | [diff] [blame] | 12745 | |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 12746 | // Instructions can end up on the worklist more than once. Make sure |
| 12747 | // we do not process an instruction that has been deleted. |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 12748 | RemoveFromWorkList(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 12749 | |
| 12750 | // Erase the old instruction. |
| 12751 | InstParent->getInstList().erase(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 12752 | } else { |
Evan Cheng | c7baf68 | 2007-03-27 16:44:48 +0000 | [diff] [blame] | 12753 | #ifndef NDEBUG |
Reid Spencer | a9b8101 | 2007-03-26 17:44:01 +0000 | [diff] [blame] | 12754 | DOUT << "IC: Mod = " << OrigI |
| 12755 | << " New = " << *I; |
Evan Cheng | c7baf68 | 2007-03-27 16:44:48 +0000 | [diff] [blame] | 12756 | #endif |
Chris Lattner | 0cea42a | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 12757 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 12758 | // If the instruction was modified, it's possible that it is now dead. |
| 12759 | // if so, remove it. |
Chris Lattner | 00d5131 | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 12760 | if (isInstructionTriviallyDead(I)) { |
| 12761 | // Make sure we process all operands now that we are reducing their |
| 12762 | // use counts. |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 12763 | AddUsesToWorkList(*I); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 12764 | |
Chris Lattner | 00d5131 | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 12765 | // Instructions may end up in the worklist more than once. Erase all |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 12766 | // occurrences of this instruction. |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 12767 | RemoveFromWorkList(I); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 12768 | I->eraseFromParent(); |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 12769 | } else { |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 12770 | AddToWorkList(I); |
| 12771 | AddUsersToWorkList(*I); |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 12772 | } |
Chris Lattner | b3bc8fa | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 12773 | } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 12774 | Changed = true; |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 12775 | } |
| 12776 | } |
| 12777 | |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 12778 | assert(WorklistMap.empty() && "Worklist empty, but map not?"); |
Chris Lattner | a9ff5eb | 2007-08-05 08:47:58 +0000 | [diff] [blame] | 12779 | |
| 12780 | // Do an explicit clear, this shrinks the map if needed. |
| 12781 | WorklistMap.clear(); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 12782 | return Changed; |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 12783 | } |
| 12784 | |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 12785 | |
| 12786 | bool InstCombiner::runOnFunction(Function &F) { |
Chris Lattner | f964f32 | 2007-03-04 04:27:24 +0000 | [diff] [blame] | 12787 | MustPreserveLCSSA = mustPreserveAnalysisID(LCSSAID); |
| 12788 | |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 12789 | bool EverMadeChange = false; |
| 12790 | |
| 12791 | // Iterate while there is work to do. |
| 12792 | unsigned Iteration = 0; |
Bill Wendling | a6c3112 | 2008-05-14 22:45:20 +0000 | [diff] [blame] | 12793 | while (DoOneIteration(F, Iteration++)) |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 12794 | EverMadeChange = true; |
| 12795 | return EverMadeChange; |
| 12796 | } |
| 12797 | |
Brian Gaeke | 96d4bf7 | 2004-07-27 17:43:21 +0000 | [diff] [blame] | 12798 | FunctionPass *llvm::createInstructionCombiningPass() { |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 12799 | return new InstCombiner(); |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 12800 | } |