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 |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 11 | // instructions. This pass does not modify the CFG This pass is where algebraic |
| 12 | // 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 | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 43 | #include "llvm/Target/TargetData.h" |
| 44 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 45 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 46 | #include "llvm/Support/CallSite.h" |
Nick Lewycky | 5be2920 | 2008-02-03 16:33:09 +0000 | [diff] [blame] | 47 | #include "llvm/Support/ConstantRange.h" |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 48 | #include "llvm/Support/Debug.h" |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 49 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 50 | #include "llvm/Support/InstVisitor.h" |
Chris Lattner | bcd7db5 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 51 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 52 | #include "llvm/Support/PatternMatch.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 53 | #include "llvm/Support/Compiler.h" |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 54 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 55 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 1f87a58 | 2007-02-15 19:41:52 +0000 | [diff] [blame] | 56 | #include "llvm/ADT/SmallPtrSet.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 57 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 58 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | b3bc8fa | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 59 | #include <algorithm> |
Torok Edwin | 3eaee31 | 2008-04-20 08:33:11 +0000 | [diff] [blame] | 60 | #include <climits> |
Reid Spencer | a9b8101 | 2007-03-26 17:44:01 +0000 | [diff] [blame] | 61 | #include <sstream> |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 62 | using namespace llvm; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 63 | using namespace llvm::PatternMatch; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 64 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 65 | STATISTIC(NumCombined , "Number of insts combined"); |
| 66 | STATISTIC(NumConstProp, "Number of constant folds"); |
| 67 | STATISTIC(NumDeadInst , "Number of dead inst eliminated"); |
| 68 | STATISTIC(NumDeadStore, "Number of dead stores eliminated"); |
| 69 | STATISTIC(NumSunkInst , "Number of instructions sunk"); |
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 70 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 71 | namespace { |
Chris Lattner | f4b5461 | 2006-06-28 22:08:15 +0000 | [diff] [blame] | 72 | class VISIBILITY_HIDDEN InstCombiner |
| 73 | : public FunctionPass, |
| 74 | public InstVisitor<InstCombiner, Instruction*> { |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 75 | // Worklist of all of the instructions that need to be simplified. |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 76 | std::vector<Instruction*> Worklist; |
| 77 | DenseMap<Instruction*, unsigned> WorklistMap; |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 78 | TargetData *TD; |
Chris Lattner | f964f32 | 2007-03-04 04:27:24 +0000 | [diff] [blame] | 79 | bool MustPreserveLCSSA; |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 80 | public: |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 81 | static char ID; // Pass identification, replacement for typeid |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 82 | InstCombiner() : FunctionPass((intptr_t)&ID) {} |
| 83 | |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 84 | /// AddToWorkList - Add the specified instruction to the worklist if it |
| 85 | /// isn't already in it. |
| 86 | void AddToWorkList(Instruction *I) { |
| 87 | if (WorklistMap.insert(std::make_pair(I, Worklist.size()))) |
| 88 | Worklist.push_back(I); |
| 89 | } |
| 90 | |
| 91 | // RemoveFromWorkList - remove I from the worklist if it exists. |
| 92 | void RemoveFromWorkList(Instruction *I) { |
| 93 | DenseMap<Instruction*, unsigned>::iterator It = WorklistMap.find(I); |
| 94 | if (It == WorklistMap.end()) return; // Not in worklist. |
| 95 | |
| 96 | // Don't bother moving everything down, just null out the slot. |
| 97 | Worklist[It->second] = 0; |
| 98 | |
| 99 | WorklistMap.erase(It); |
| 100 | } |
| 101 | |
| 102 | Instruction *RemoveOneFromWorkList() { |
| 103 | Instruction *I = Worklist.back(); |
| 104 | Worklist.pop_back(); |
| 105 | WorklistMap.erase(I); |
| 106 | return I; |
| 107 | } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 108 | |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 109 | |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 110 | /// AddUsersToWorkList - When an instruction is simplified, add all users of |
| 111 | /// the instruction to the work lists because they might get more simplified |
| 112 | /// now. |
| 113 | /// |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 114 | void AddUsersToWorkList(Value &I) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 115 | for (Value::use_iterator UI = I.use_begin(), UE = I.use_end(); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 116 | UI != UE; ++UI) |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 117 | AddToWorkList(cast<Instruction>(*UI)); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 120 | /// AddUsesToWorkList - When an instruction is simplified, add operands to |
| 121 | /// the work lists because they might get more simplified now. |
| 122 | /// |
| 123 | void AddUsesToWorkList(Instruction &I) { |
| 124 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) |
| 125 | if (Instruction *Op = dyn_cast<Instruction>(I.getOperand(i))) |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 126 | AddToWorkList(Op); |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 127 | } |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 128 | |
| 129 | /// AddSoonDeadInstToWorklist - The specified instruction is about to become |
| 130 | /// dead. Add all of its operands to the worklist, turning them into |
| 131 | /// undef's to reduce the number of uses of those instructions. |
| 132 | /// |
| 133 | /// Return the specified operand before it is turned into an undef. |
| 134 | /// |
| 135 | Value *AddSoonDeadInstToWorklist(Instruction &I, unsigned op) { |
| 136 | Value *R = I.getOperand(op); |
| 137 | |
| 138 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) |
| 139 | if (Instruction *Op = dyn_cast<Instruction>(I.getOperand(i))) { |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 140 | AddToWorkList(Op); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 141 | // Set the operand to undef to drop the use. |
| 142 | I.setOperand(i, UndefValue::get(Op->getType())); |
| 143 | } |
| 144 | |
| 145 | return R; |
| 146 | } |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 147 | |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 148 | public: |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 149 | virtual bool runOnFunction(Function &F); |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 150 | |
| 151 | bool DoOneIteration(Function &F, unsigned ItNum); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 152 | |
Chris Lattner | 97e52e4 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 153 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 154 | AU.addRequired<TargetData>(); |
Owen Anderson | d1b78a1 | 2006-07-10 19:03:49 +0000 | [diff] [blame] | 155 | AU.addPreservedID(LCSSAID); |
Chris Lattner | cb2610e | 2002-10-21 20:00:28 +0000 | [diff] [blame] | 156 | AU.setPreservesCFG(); |
Chris Lattner | 97e52e4 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 159 | TargetData &getTargetData() const { return *TD; } |
| 160 | |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 161 | // Visitation implementation - Implement instruction combining for different |
| 162 | // instruction types. The semantics are as follows: |
| 163 | // Return Value: |
| 164 | // null - No change was made |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 165 | // 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] | 166 | // otherwise - Change was made, replace I with returned instruction |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 167 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 168 | Instruction *visitAdd(BinaryOperator &I); |
| 169 | Instruction *visitSub(BinaryOperator &I); |
| 170 | Instruction *visitMul(BinaryOperator &I); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 171 | Instruction *visitURem(BinaryOperator &I); |
| 172 | Instruction *visitSRem(BinaryOperator &I); |
| 173 | Instruction *visitFRem(BinaryOperator &I); |
| 174 | Instruction *commonRemTransforms(BinaryOperator &I); |
| 175 | Instruction *commonIRemTransforms(BinaryOperator &I); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 176 | Instruction *commonDivTransforms(BinaryOperator &I); |
| 177 | Instruction *commonIDivTransforms(BinaryOperator &I); |
| 178 | Instruction *visitUDiv(BinaryOperator &I); |
| 179 | Instruction *visitSDiv(BinaryOperator &I); |
| 180 | Instruction *visitFDiv(BinaryOperator &I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 181 | Instruction *visitAnd(BinaryOperator &I); |
| 182 | Instruction *visitOr (BinaryOperator &I); |
| 183 | Instruction *visitXor(BinaryOperator &I); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 184 | Instruction *visitShl(BinaryOperator &I); |
| 185 | Instruction *visitAShr(BinaryOperator &I); |
| 186 | Instruction *visitLShr(BinaryOperator &I); |
| 187 | Instruction *commonShiftTransforms(BinaryOperator &I); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 188 | Instruction *visitFCmpInst(FCmpInst &I); |
| 189 | Instruction *visitICmpInst(ICmpInst &I); |
| 190 | Instruction *visitICmpInstWithCastAndCast(ICmpInst &ICI); |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 191 | Instruction *visitICmpInstWithInstAndIntCst(ICmpInst &ICI, |
| 192 | Instruction *LHS, |
| 193 | ConstantInt *RHS); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 194 | Instruction *FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI, |
| 195 | ConstantInt *DivRHS); |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 196 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 197 | Instruction *FoldGEPICmp(User *GEPLHS, Value *RHS, |
| 198 | ICmpInst::Predicate Cond, Instruction &I); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 199 | Instruction *FoldShiftByConstant(Value *Op0, ConstantInt *Op1, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 200 | BinaryOperator &I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 201 | Instruction *commonCastTransforms(CastInst &CI); |
| 202 | Instruction *commonIntCastTransforms(CastInst &CI); |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 203 | Instruction *commonPointerCastTransforms(CastInst &CI); |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 204 | Instruction *visitTrunc(TruncInst &CI); |
| 205 | Instruction *visitZExt(ZExtInst &CI); |
| 206 | Instruction *visitSExt(SExtInst &CI); |
Chris Lattner | b753065 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 207 | Instruction *visitFPTrunc(FPTruncInst &CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 208 | Instruction *visitFPExt(CastInst &CI); |
| 209 | Instruction *visitFPToUI(CastInst &CI); |
| 210 | Instruction *visitFPToSI(CastInst &CI); |
| 211 | Instruction *visitUIToFP(CastInst &CI); |
| 212 | Instruction *visitSIToFP(CastInst &CI); |
| 213 | Instruction *visitPtrToInt(CastInst &CI); |
Chris Lattner | f9d9e45 | 2008-01-08 07:23:51 +0000 | [diff] [blame] | 214 | Instruction *visitIntToPtr(IntToPtrInst &CI); |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 215 | Instruction *visitBitCast(BitCastInst &CI); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 216 | Instruction *FoldSelectOpOp(SelectInst &SI, Instruction *TI, |
| 217 | Instruction *FI); |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 218 | Instruction *visitSelectInst(SelectInst &CI); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 219 | Instruction *visitCallInst(CallInst &CI); |
| 220 | Instruction *visitInvokeInst(InvokeInst &II); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 221 | Instruction *visitPHINode(PHINode &PN); |
| 222 | Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP); |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 223 | Instruction *visitAllocationInst(AllocationInst &AI); |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 224 | Instruction *visitFreeInst(FreeInst &FI); |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 225 | Instruction *visitLoadInst(LoadInst &LI); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 226 | Instruction *visitStoreInst(StoreInst &SI); |
Chris Lattner | c4d10eb | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 227 | Instruction *visitBranchInst(BranchInst &BI); |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 228 | Instruction *visitSwitchInst(SwitchInst &SI); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 229 | Instruction *visitInsertElementInst(InsertElementInst &IE); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 230 | Instruction *visitExtractElementInst(ExtractElementInst &EI); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 231 | Instruction *visitShuffleVectorInst(ShuffleVectorInst &SVI); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 232 | |
| 233 | // visitInstruction - Specify what to return for unhandled instructions... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 234 | Instruction *visitInstruction(Instruction &I) { return 0; } |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 235 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 236 | private: |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 237 | Instruction *visitCallSite(CallSite CS); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 238 | bool transformConstExprCastCall(CallSite CS); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 239 | Instruction *transformCallThroughTrampoline(CallSite CS); |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 240 | Instruction *transformZExtICmp(ICmpInst *ICI, Instruction &CI, |
| 241 | bool DoXform = true); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 242 | |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 243 | public: |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 244 | // InsertNewInstBefore - insert an instruction New before instruction Old |
| 245 | // in the program. Add the new instruction to the worklist. |
| 246 | // |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 247 | Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) { |
Chris Lattner | e6f9a91 | 2002-08-23 18:32:43 +0000 | [diff] [blame] | 248 | assert(New && New->getParent() == 0 && |
| 249 | "New instruction already inserted into a basic block!"); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 250 | BasicBlock *BB = Old.getParent(); |
| 251 | BB->getInstList().insert(&Old, New); // Insert inst |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 252 | AddToWorkList(New); |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 253 | return New; |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 256 | /// InsertCastBefore - Insert a cast of V to TY before the instruction POS. |
| 257 | /// This also adds the cast to the worklist. Finally, this returns the |
| 258 | /// cast. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 259 | Value *InsertCastBefore(Instruction::CastOps opc, Value *V, const Type *Ty, |
| 260 | Instruction &Pos) { |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 261 | if (V->getType() == Ty) return V; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 262 | |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 263 | if (Constant *CV = dyn_cast<Constant>(V)) |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 264 | return ConstantExpr::getCast(opc, CV, Ty); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 265 | |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 266 | Instruction *C = CastInst::create(opc, V, Ty, V->getName(), &Pos); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 267 | AddToWorkList(C); |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 268 | return C; |
| 269 | } |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 270 | |
| 271 | Value *InsertBitCastBefore(Value *V, const Type *Ty, Instruction &Pos) { |
| 272 | return InsertCastBefore(Instruction::BitCast, V, Ty, Pos); |
| 273 | } |
| 274 | |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 275 | |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 276 | // ReplaceInstUsesWith - This method is to be used when an instruction is |
| 277 | // found to be dead, replacable with another preexisting expression. Here |
| 278 | // we add all uses of I to the worklist, replace all uses of I with the new |
| 279 | // value, then return I, so that the inst combiner will know that I was |
| 280 | // modified. |
| 281 | // |
| 282 | Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) { |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 283 | AddUsersToWorkList(I); // Add all modified instrs to worklist |
Chris Lattner | 15a76c0 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 284 | if (&I != V) { |
| 285 | I.replaceAllUsesWith(V); |
| 286 | return &I; |
| 287 | } else { |
| 288 | // If we are replacing the instruction with itself, this must be in a |
| 289 | // segment of unreachable code, so just clobber the instruction. |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 290 | I.replaceAllUsesWith(UndefValue::get(I.getType())); |
Chris Lattner | 15a76c0 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 291 | return &I; |
| 292 | } |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 293 | } |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 294 | |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 295 | // UpdateValueUsesWith - This method is to be used when an value is |
| 296 | // found to be replacable with another preexisting expression or was |
| 297 | // updated. Here we add all uses of I to the worklist, replace all uses of |
| 298 | // I with the new value (unless the instruction was just updated), then |
| 299 | // return true, so that the inst combiner will know that I was modified. |
| 300 | // |
| 301 | bool UpdateValueUsesWith(Value *Old, Value *New) { |
| 302 | AddUsersToWorkList(*Old); // Add all modified instrs to worklist |
| 303 | if (Old != New) |
| 304 | Old->replaceAllUsesWith(New); |
| 305 | if (Instruction *I = dyn_cast<Instruction>(Old)) |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 306 | AddToWorkList(I); |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 307 | if (Instruction *I = dyn_cast<Instruction>(New)) |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 308 | AddToWorkList(I); |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 309 | return true; |
| 310 | } |
| 311 | |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 312 | // EraseInstFromFunction - When dealing with an instruction that has side |
| 313 | // effects or produces a void value, we can't rely on DCE to delete the |
| 314 | // instruction. Instead, visit methods should return the value returned by |
| 315 | // this function. |
| 316 | Instruction *EraseInstFromFunction(Instruction &I) { |
| 317 | assert(I.use_empty() && "Cannot erase instruction that is used!"); |
| 318 | AddUsesToWorkList(I); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 319 | RemoveFromWorkList(&I); |
Chris Lattner | 954f66a | 2004-11-18 21:41:39 +0000 | [diff] [blame] | 320 | I.eraseFromParent(); |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 321 | return 0; // Don't do anything with FI |
| 322 | } |
| 323 | |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 324 | private: |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 325 | /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the |
| 326 | /// InsertBefore instruction. This is specialized a bit to avoid inserting |
| 327 | /// casts that are known to not do anything... |
| 328 | /// |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 329 | Value *InsertOperandCastBefore(Instruction::CastOps opcode, |
| 330 | Value *V, const Type *DestTy, |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 331 | Instruction *InsertBefore); |
| 332 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 333 | /// SimplifyCommutative - This performs a few simplifications for |
| 334 | /// commutative operators. |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 335 | bool SimplifyCommutative(BinaryOperator &I); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 336 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 337 | /// SimplifyCompare - This reorders the operands of a CmpInst to get them in |
| 338 | /// most-complex to least-complex order. |
| 339 | bool SimplifyCompare(CmpInst &I); |
| 340 | |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 341 | /// SimplifyDemandedBits - Attempts to replace V with a simpler value based |
| 342 | /// on the demanded bits. |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 343 | bool SimplifyDemandedBits(Value *V, APInt DemandedMask, |
| 344 | APInt& KnownZero, APInt& KnownOne, |
| 345 | unsigned Depth = 0); |
| 346 | |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 347 | Value *SimplifyDemandedVectorElts(Value *V, uint64_t DemandedElts, |
| 348 | uint64_t &UndefElts, unsigned Depth = 0); |
| 349 | |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 350 | // FoldOpIntoPhi - Given a binary operator or cast instruction which has a |
| 351 | // PHI node as operand #0, see if we can fold the instruction into the PHI |
| 352 | // (which is only possible if all operands to the PHI are constants). |
| 353 | Instruction *FoldOpIntoPhi(Instruction &I); |
| 354 | |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 355 | // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary" |
| 356 | // operator and they all are only used by the PHI, PHI together their |
| 357 | // inputs, and do the operation once, to the result of the PHI. |
| 358 | Instruction *FoldPHIArgOpIntoPHI(PHINode &PN); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 359 | Instruction *FoldPHIArgBinOpIntoPHI(PHINode &PN); |
| 360 | |
| 361 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 362 | Instruction *OptAndOp(Instruction *Op, ConstantInt *OpRHS, |
| 363 | ConstantInt *AndRHS, BinaryOperator &TheAnd); |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 364 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 365 | Value *FoldLogicalPlusAnd(Value *LHS, Value *RHS, ConstantInt *Mask, |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 366 | bool isSub, Instruction &I); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 367 | Instruction *InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 368 | bool isSigned, bool Inside, Instruction &IB); |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 369 | Instruction *PromoteCastOfAllocation(BitCastInst &CI, AllocationInst &AI); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 370 | Instruction *MatchBSwap(BinaryOperator &I); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 371 | bool SimplifyStoreAtEndOfBlock(StoreInst &SI); |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 372 | Instruction *SimplifyMemTransfer(MemIntrinsic *MI); |
| 373 | |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 374 | |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 375 | Value *EvaluateInDifferentType(Value *V, const Type *Ty, bool isSigned); |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 376 | |
| 377 | void ComputeMaskedBits(Value *V, const APInt &Mask, APInt& KnownZero, |
| 378 | APInt& KnownOne, unsigned Depth = 0); |
| 379 | bool MaskedValueIsZero(Value *V, const APInt& Mask, unsigned Depth = 0); |
| 380 | bool CanEvaluateInDifferentType(Value *V, const IntegerType *Ty, |
| 381 | unsigned CastOpc, |
| 382 | int &NumCastsRemoved); |
| 383 | unsigned GetOrEnforceKnownAlignment(Value *V, |
| 384 | unsigned PrefAlign = 0); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 385 | }; |
Chris Lattner | f629309 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 386 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 387 | char InstCombiner::ID = 0; |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 388 | RegisterPass<InstCombiner> X("instcombine", "Combine redundant instructions"); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 391 | // getComplexity: Assign a complexity or rank value to LLVM Values... |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 392 | // 0 -> undef, 1 -> Const, 2 -> Other, 3 -> Arg, 3 -> Unary, 4 -> OtherInst |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 393 | static unsigned getComplexity(Value *V) { |
| 394 | if (isa<Instruction>(V)) { |
| 395 | if (BinaryOperator::isNeg(V) || BinaryOperator::isNot(V)) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 396 | return 3; |
| 397 | return 4; |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 398 | } |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 399 | if (isa<Argument>(V)) return 3; |
| 400 | return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2; |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 401 | } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 402 | |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 403 | // isOnlyUse - Return true if this instruction will be deleted if we stop using |
| 404 | // it. |
| 405 | static bool isOnlyUse(Value *V) { |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 406 | return V->hasOneUse() || isa<Constant>(V); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 409 | // getPromotedType - Return the specified type promoted as it would be to pass |
| 410 | // though a va_arg area... |
| 411 | static const Type *getPromotedType(const Type *Ty) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 412 | if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty)) { |
| 413 | if (ITy->getBitWidth() < 32) |
| 414 | return Type::Int32Ty; |
Chris Lattner | 2b7e0ad | 2007-05-23 01:17:04 +0000 | [diff] [blame] | 415 | } |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 416 | return Ty; |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 419 | /// getBitCastOperand - If the specified operand is a CastInst or a constant |
| 420 | /// expression bitcast, return the operand value, otherwise return null. |
| 421 | static Value *getBitCastOperand(Value *V) { |
| 422 | if (BitCastInst *I = dyn_cast<BitCastInst>(V)) |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 423 | return I->getOperand(0); |
| 424 | else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 425 | if (CE->getOpcode() == Instruction::BitCast) |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 426 | return CE->getOperand(0); |
| 427 | return 0; |
| 428 | } |
| 429 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 430 | /// This function is a wrapper around CastInst::isEliminableCastPair. It |
| 431 | /// simply extracts arguments and returns what that function returns. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 432 | static Instruction::CastOps |
| 433 | isEliminableCastPair( |
| 434 | const CastInst *CI, ///< The first cast instruction |
| 435 | unsigned opcode, ///< The opcode of the second cast instruction |
| 436 | const Type *DstTy, ///< The target type for the second cast instruction |
| 437 | TargetData *TD ///< The target data for pointer size |
| 438 | ) { |
| 439 | |
| 440 | const Type *SrcTy = CI->getOperand(0)->getType(); // A from above |
| 441 | const Type *MidTy = CI->getType(); // B from above |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 442 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 443 | // Get the opcodes of the two Cast instructions |
| 444 | Instruction::CastOps firstOp = Instruction::CastOps(CI->getOpcode()); |
| 445 | Instruction::CastOps secondOp = Instruction::CastOps(opcode); |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 446 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 447 | return Instruction::CastOps( |
| 448 | CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, |
| 449 | DstTy, TD->getIntPtrType())); |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | /// ValueRequiresCast - Return true if the cast from "V to Ty" actually results |
| 453 | /// in any code being generated. It does not require codegen if V is simple |
| 454 | /// enough or if the cast can be folded into other casts. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 455 | static bool ValueRequiresCast(Instruction::CastOps opcode, const Value *V, |
| 456 | const Type *Ty, TargetData *TD) { |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 457 | if (V->getType() == Ty || isa<Constant>(V)) return false; |
| 458 | |
Chris Lattner | 01575b7 | 2006-05-25 23:24:33 +0000 | [diff] [blame] | 459 | // 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] | 460 | if (const CastInst *CI = dyn_cast<CastInst>(V)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 461 | if (isEliminableCastPair(CI, opcode, Ty, TD)) |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 462 | return false; |
| 463 | return true; |
| 464 | } |
| 465 | |
| 466 | /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the |
| 467 | /// InsertBefore instruction. This is specialized a bit to avoid inserting |
| 468 | /// casts that are known to not do anything... |
| 469 | /// |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 470 | Value *InstCombiner::InsertOperandCastBefore(Instruction::CastOps opcode, |
| 471 | Value *V, const Type *DestTy, |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 472 | Instruction *InsertBefore) { |
| 473 | if (V->getType() == DestTy) return V; |
| 474 | if (Constant *C = dyn_cast<Constant>(V)) |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 475 | return ConstantExpr::getCast(opcode, C, DestTy); |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 476 | |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 477 | return InsertCastBefore(opcode, V, DestTy, *InsertBefore); |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 478 | } |
| 479 | |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 480 | // SimplifyCommutative - This performs a few simplifications for commutative |
| 481 | // operators: |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 482 | // |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 483 | // 1. Order operands such that they are listed from right (least complex) to |
| 484 | // left (most complex). This puts constants before unary operators before |
| 485 | // binary operators. |
| 486 | // |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 487 | // 2. Transform: (op (op V, C1), C2) ==> (op V, (op C1, C2)) |
| 488 | // 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] | 489 | // |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 490 | bool InstCombiner::SimplifyCommutative(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 491 | bool Changed = false; |
| 492 | if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1))) |
| 493 | Changed = !I.swapOperands(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 494 | |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 495 | if (!I.isAssociative()) return Changed; |
| 496 | Instruction::BinaryOps Opcode = I.getOpcode(); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 497 | if (BinaryOperator *Op = dyn_cast<BinaryOperator>(I.getOperand(0))) |
| 498 | if (Op->getOpcode() == Opcode && isa<Constant>(Op->getOperand(1))) { |
| 499 | if (isa<Constant>(I.getOperand(1))) { |
Chris Lattner | 2a9c847 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 500 | Constant *Folded = ConstantExpr::get(I.getOpcode(), |
| 501 | cast<Constant>(I.getOperand(1)), |
| 502 | cast<Constant>(Op->getOperand(1))); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 503 | I.setOperand(0, Op->getOperand(0)); |
| 504 | I.setOperand(1, Folded); |
| 505 | return true; |
| 506 | } else if (BinaryOperator *Op1=dyn_cast<BinaryOperator>(I.getOperand(1))) |
| 507 | if (Op1->getOpcode() == Opcode && isa<Constant>(Op1->getOperand(1)) && |
| 508 | isOnlyUse(Op) && isOnlyUse(Op1)) { |
| 509 | Constant *C1 = cast<Constant>(Op->getOperand(1)); |
| 510 | Constant *C2 = cast<Constant>(Op1->getOperand(1)); |
| 511 | |
| 512 | // 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] | 513 | Constant *Folded = ConstantExpr::get(I.getOpcode(), C1, C2); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 514 | Instruction *New = BinaryOperator::create(Opcode, Op->getOperand(0), |
| 515 | Op1->getOperand(0), |
| 516 | Op1->getName(), &I); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 517 | AddToWorkList(New); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 518 | I.setOperand(0, New); |
| 519 | I.setOperand(1, Folded); |
| 520 | return true; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 521 | } |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 522 | } |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 523 | return Changed; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 524 | } |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 525 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 526 | /// SimplifyCompare - For a CmpInst this function just orders the operands |
| 527 | /// so that theyare listed from right (least complex) to left (most complex). |
| 528 | /// This puts constants before unary operators before binary operators. |
| 529 | bool InstCombiner::SimplifyCompare(CmpInst &I) { |
| 530 | if (getComplexity(I.getOperand(0)) >= getComplexity(I.getOperand(1))) |
| 531 | return false; |
| 532 | I.swapOperands(); |
| 533 | // Compare instructions are not associative so there's nothing else we can do. |
| 534 | return true; |
| 535 | } |
| 536 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 537 | // dyn_castNegVal - Given a 'sub' instruction, return the RHS of the instruction |
| 538 | // if the LHS is a constant zero (which is the 'negate' form). |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 539 | // |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 540 | static inline Value *dyn_castNegVal(Value *V) { |
| 541 | if (BinaryOperator::isNeg(V)) |
Chris Lattner | a1df33c | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 542 | return BinaryOperator::getNegArgument(V); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 543 | |
Chris Lattner | 0ce8580 | 2004-12-14 20:08:06 +0000 | [diff] [blame] | 544 | // Constants can be considered to be negated values if they can be folded. |
| 545 | if (ConstantInt *C = dyn_cast<ConstantInt>(V)) |
| 546 | return ConstantExpr::getNeg(C); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 547 | return 0; |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 550 | static inline Value *dyn_castNotVal(Value *V) { |
| 551 | if (BinaryOperator::isNot(V)) |
Chris Lattner | a1df33c | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 552 | return BinaryOperator::getNotArgument(V); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 553 | |
| 554 | // Constants can be considered to be not'ed values... |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 555 | if (ConstantInt *C = dyn_cast<ConstantInt>(V)) |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 556 | return ConstantInt::get(~C->getValue()); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 557 | return 0; |
| 558 | } |
| 559 | |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 560 | // dyn_castFoldableMul - If this value is a multiply that can be folded into |
| 561 | // other computations (because it has a constant operand), return the |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 562 | // non-constant operand of the multiply, and set CST to point to the multiplier. |
| 563 | // Otherwise, return null. |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 564 | // |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 565 | static inline Value *dyn_castFoldableMul(Value *V, ConstantInt *&CST) { |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 566 | if (V->hasOneUse() && V->getType()->isInteger()) |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 567 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 568 | if (I->getOpcode() == Instruction::Mul) |
Chris Lattner | 50e60c7 | 2004-11-15 05:54:07 +0000 | [diff] [blame] | 569 | if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 570 | return I->getOperand(0); |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 571 | if (I->getOpcode() == Instruction::Shl) |
Chris Lattner | 50e60c7 | 2004-11-15 05:54:07 +0000 | [diff] [blame] | 572 | if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) { |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 573 | // The multiplier is really 1 << CST. |
Zhou Sheng | 97b52c2 | 2007-03-29 01:57:21 +0000 | [diff] [blame] | 574 | uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth(); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 575 | uint32_t CSTVal = CST->getLimitedValue(BitWidth); |
Zhou Sheng | 97b52c2 | 2007-03-29 01:57:21 +0000 | [diff] [blame] | 576 | CST = ConstantInt::get(APInt(BitWidth, 1).shl(CSTVal)); |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 577 | return I->getOperand(0); |
| 578 | } |
| 579 | } |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 580 | return 0; |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 581 | } |
Chris Lattner | af2930e | 2002-08-14 17:51:49 +0000 | [diff] [blame] | 582 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 583 | /// dyn_castGetElementPtr - If this is a getelementptr instruction or constant |
| 584 | /// expression, return it. |
| 585 | static User *dyn_castGetElementPtr(Value *V) { |
| 586 | if (isa<GetElementPtrInst>(V)) return cast<User>(V); |
| 587 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
| 588 | if (CE->getOpcode() == Instruction::GetElementPtr) |
| 589 | return cast<User>(V); |
| 590 | return false; |
| 591 | } |
| 592 | |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 593 | /// getOpcode - If this is an Instruction or a ConstantExpr, return the |
| 594 | /// opcode value. Otherwise return UserOp1. |
| 595 | static unsigned getOpcode(User *U) { |
| 596 | if (Instruction *I = dyn_cast<Instruction>(U)) |
| 597 | return I->getOpcode(); |
| 598 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) |
| 599 | return CE->getOpcode(); |
| 600 | // Use UserOp1 to mean there's no opcode. |
| 601 | return Instruction::UserOp1; |
| 602 | } |
| 603 | |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 604 | /// AddOne - Add one to a ConstantInt |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 605 | static ConstantInt *AddOne(ConstantInt *C) { |
Reid Spencer | 2149a9d | 2007-03-25 19:55:33 +0000 | [diff] [blame] | 606 | APInt Val(C->getValue()); |
| 607 | return ConstantInt::get(++Val); |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 608 | } |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 609 | /// SubOne - Subtract one from a ConstantInt |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 610 | static ConstantInt *SubOne(ConstantInt *C) { |
Reid Spencer | 2149a9d | 2007-03-25 19:55:33 +0000 | [diff] [blame] | 611 | APInt Val(C->getValue()); |
| 612 | return ConstantInt::get(--Val); |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 613 | } |
| 614 | /// Add - Add two ConstantInts together |
| 615 | static ConstantInt *Add(ConstantInt *C1, ConstantInt *C2) { |
| 616 | return ConstantInt::get(C1->getValue() + C2->getValue()); |
| 617 | } |
| 618 | /// And - Bitwise AND two ConstantInts together |
| 619 | static ConstantInt *And(ConstantInt *C1, ConstantInt *C2) { |
| 620 | return ConstantInt::get(C1->getValue() & C2->getValue()); |
| 621 | } |
| 622 | /// Subtract - Subtract one ConstantInt from another |
| 623 | static ConstantInt *Subtract(ConstantInt *C1, ConstantInt *C2) { |
| 624 | return ConstantInt::get(C1->getValue() - C2->getValue()); |
| 625 | } |
| 626 | /// Multiply - Multiply two ConstantInts together |
| 627 | static ConstantInt *Multiply(ConstantInt *C1, ConstantInt *C2) { |
| 628 | return ConstantInt::get(C1->getValue() * C2->getValue()); |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 629 | } |
Nick Lewycky | e0cfecf | 2008-02-18 22:48:05 +0000 | [diff] [blame] | 630 | /// MultiplyOverflows - True if the multiply can not be expressed in an int |
| 631 | /// this size. |
| 632 | static bool MultiplyOverflows(ConstantInt *C1, ConstantInt *C2, bool sign) { |
| 633 | uint32_t W = C1->getBitWidth(); |
| 634 | APInt LHSExt = C1->getValue(), RHSExt = C2->getValue(); |
| 635 | if (sign) { |
| 636 | LHSExt.sext(W * 2); |
| 637 | RHSExt.sext(W * 2); |
| 638 | } else { |
| 639 | LHSExt.zext(W * 2); |
| 640 | RHSExt.zext(W * 2); |
| 641 | } |
| 642 | |
| 643 | APInt MulExt = LHSExt * RHSExt; |
| 644 | |
| 645 | if (sign) { |
| 646 | APInt Min = APInt::getSignedMinValue(W).sext(W * 2); |
| 647 | APInt Max = APInt::getSignedMaxValue(W).sext(W * 2); |
| 648 | return MulExt.slt(Min) || MulExt.sgt(Max); |
| 649 | } else |
| 650 | return MulExt.ugt(APInt::getLowBitsSet(W * 2, W)); |
| 651 | } |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 652 | |
Chris Lattner | 68d5ff2 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 653 | /// ComputeMaskedBits - Determine which of the bits specified in Mask are |
| 654 | /// known to be either zero or one and return them in the KnownZero/KnownOne |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 655 | /// bit sets. This code only analyzes bits in Mask, in order to short-circuit |
| 656 | /// processing. |
| 657 | /// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that |
| 658 | /// we cannot optimize based on the assumption that it is zero without changing |
| 659 | /// it to be an explicit zero. If we don't change it to zero, other code could |
| 660 | /// optimized based on the contradictory assumption that it is non-zero. |
| 661 | /// Because instcombine aggressively folds operations with undef args anyway, |
| 662 | /// this won't lose us code quality. |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 663 | void InstCombiner::ComputeMaskedBits(Value *V, const APInt &Mask, |
| 664 | APInt& KnownZero, APInt& KnownOne, |
| 665 | unsigned Depth) { |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 666 | assert(V && "No Value?"); |
| 667 | assert(Depth <= 6 && "Limit Search Depth"); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 668 | uint32_t BitWidth = Mask.getBitWidth(); |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 669 | assert((V->getType()->isInteger() || isa<PointerType>(V->getType())) && |
| 670 | "Not integer or pointer type!"); |
| 671 | assert((!TD || TD->getTypeSizeInBits(V->getType()) == BitWidth) && |
| 672 | (!isa<IntegerType>(V->getType()) || |
| 673 | V->getType()->getPrimitiveSizeInBits() == BitWidth) && |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 674 | KnownZero.getBitWidth() == BitWidth && |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 675 | KnownOne.getBitWidth() == BitWidth && |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 676 | "V, Mask, KnownOne and KnownZero should have same BitWidth"); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 677 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
| 678 | // We know all of the bits for a constant! |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 679 | KnownOne = CI->getValue() & Mask; |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 680 | KnownZero = ~KnownOne & Mask; |
| 681 | return; |
| 682 | } |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 683 | // Null is all-zeros. |
| 684 | if (isa<ConstantPointerNull>(V)) { |
| 685 | KnownOne.clear(); |
| 686 | KnownZero = Mask; |
| 687 | return; |
| 688 | } |
| 689 | // The address of an aligned GlobalValue has trailing zeros. |
| 690 | if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
| 691 | unsigned Align = GV->getAlignment(); |
| 692 | if (Align == 0 && TD && GV->getType()->getElementType()->isSized()) |
| 693 | Align = TD->getPrefTypeAlignment(GV->getType()->getElementType()); |
| 694 | if (Align > 0) |
| 695 | KnownZero = Mask & APInt::getLowBitsSet(BitWidth, |
| 696 | CountTrailingZeros_32(Align)); |
| 697 | else |
| 698 | KnownZero.clear(); |
| 699 | KnownOne.clear(); |
| 700 | return; |
| 701 | } |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 702 | |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 703 | if (Depth == 6 || Mask == 0) |
| 704 | return; // Limit search depth. |
| 705 | |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 706 | User *I = dyn_cast<User>(V); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 707 | if (!I) return; |
| 708 | |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 709 | KnownZero.clear(); KnownOne.clear(); // Don't know anything. |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 710 | APInt KnownZero2(KnownZero), KnownOne2(KnownOne); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 711 | |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 712 | switch (getOpcode(I)) { |
| 713 | default: break; |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 714 | case Instruction::And: { |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 715 | // If either the LHS or the RHS are Zero, the result is zero. |
| 716 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, Depth+1); |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 717 | APInt Mask2(Mask & ~KnownZero); |
| 718 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 719 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 720 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 721 | |
| 722 | // Output known-1 bits are only known if set in both the LHS & RHS. |
| 723 | KnownOne &= KnownOne2; |
| 724 | // Output known-0 are known to be clear if zero in either the LHS | RHS. |
| 725 | KnownZero |= KnownZero2; |
| 726 | return; |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 727 | } |
| 728 | case Instruction::Or: { |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 729 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, Depth+1); |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 730 | APInt Mask2(Mask & ~KnownOne); |
| 731 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 732 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 733 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 734 | |
| 735 | // Output known-0 bits are only known if clear in both the LHS & RHS. |
| 736 | KnownZero &= KnownZero2; |
| 737 | // Output known-1 are known to be set if set in either the LHS | RHS. |
| 738 | KnownOne |= KnownOne2; |
| 739 | return; |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 740 | } |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 741 | case Instruction::Xor: { |
| 742 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, Depth+1); |
| 743 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1); |
| 744 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 745 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 746 | |
| 747 | // Output known-0 bits are known if clear or set in both the LHS & RHS. |
| 748 | APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2); |
| 749 | // Output known-1 are known to be set if set in only one of the LHS, RHS. |
| 750 | KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2); |
| 751 | KnownZero = KnownZeroOut; |
| 752 | return; |
| 753 | } |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 754 | case Instruction::Mul: { |
| 755 | APInt Mask2 = APInt::getAllOnesValue(BitWidth); |
| 756 | ComputeMaskedBits(I->getOperand(1), Mask2, KnownZero, KnownOne, Depth+1); |
| 757 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1); |
| 758 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 759 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 760 | |
| 761 | // If low bits are zero in either operand, output low known-0 bits. |
| 762 | // More trickiness is possible, but this is sufficient for the |
| 763 | // interesting case of alignment computation. |
| 764 | KnownOne.clear(); |
| 765 | unsigned TrailZ = KnownZero.countTrailingOnes() + |
| 766 | KnownZero2.countTrailingOnes(); |
| 767 | TrailZ = std::min(TrailZ, BitWidth); |
| 768 | KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ); |
| 769 | KnownZero &= Mask; |
| 770 | return; |
| 771 | } |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 772 | case Instruction::Select: |
| 773 | ComputeMaskedBits(I->getOperand(2), Mask, KnownZero, KnownOne, Depth+1); |
| 774 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1); |
| 775 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 776 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 777 | |
| 778 | // Only known if known in both the LHS and RHS. |
| 779 | KnownOne &= KnownOne2; |
| 780 | KnownZero &= KnownZero2; |
| 781 | return; |
| 782 | case Instruction::FPTrunc: |
| 783 | case Instruction::FPExt: |
| 784 | case Instruction::FPToUI: |
| 785 | case Instruction::FPToSI: |
| 786 | case Instruction::SIToFP: |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 787 | case Instruction::UIToFP: |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 788 | return; // Can't work with floating point. |
| 789 | case Instruction::PtrToInt: |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 790 | case Instruction::IntToPtr: |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 791 | // We can't handle these if we don't know the pointer size. |
| 792 | if (!TD) return; |
| 793 | // Fall through and handle them the same as zext/trunc. |
| 794 | case Instruction::ZExt: |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 795 | case Instruction::Trunc: { |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 796 | // All these have integer operands |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 797 | const Type *SrcTy = I->getOperand(0)->getType(); |
| 798 | uint32_t SrcBitWidth = TD ? |
| 799 | TD->getTypeSizeInBits(SrcTy) : |
| 800 | SrcTy->getPrimitiveSizeInBits(); |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 801 | APInt MaskIn(Mask); |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 802 | MaskIn.zextOrTrunc(SrcBitWidth); |
| 803 | KnownZero.zextOrTrunc(SrcBitWidth); |
| 804 | KnownOne.zextOrTrunc(SrcBitWidth); |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 805 | ComputeMaskedBits(I->getOperand(0), MaskIn, KnownZero, KnownOne, Depth+1); |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 806 | KnownZero.zextOrTrunc(BitWidth); |
| 807 | KnownOne.zextOrTrunc(BitWidth); |
| 808 | // Any top bits are known to be zero. |
| 809 | if (BitWidth > SrcBitWidth) |
| 810 | KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 811 | return; |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 812 | } |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 813 | case Instruction::BitCast: { |
| 814 | const Type *SrcTy = I->getOperand(0)->getType(); |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 815 | if (SrcTy->isInteger() || isa<PointerType>(SrcTy)) { |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 816 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero, KnownOne, Depth+1); |
| 817 | return; |
| 818 | } |
| 819 | break; |
| 820 | } |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 821 | case Instruction::SExt: { |
| 822 | // Compute the bits in the result that are not present in the input. |
| 823 | const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType()); |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 824 | uint32_t SrcBitWidth = SrcTy->getBitWidth(); |
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 825 | |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 826 | APInt MaskIn(Mask); |
| 827 | MaskIn.trunc(SrcBitWidth); |
| 828 | KnownZero.trunc(SrcBitWidth); |
| 829 | KnownOne.trunc(SrcBitWidth); |
| 830 | ComputeMaskedBits(I->getOperand(0), MaskIn, KnownZero, KnownOne, Depth+1); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 831 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 832 | KnownZero.zext(BitWidth); |
| 833 | KnownOne.zext(BitWidth); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 834 | |
| 835 | // If the sign bit of the input is known set or clear, then we know the |
| 836 | // top bits of the result. |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 837 | if (KnownZero[SrcBitWidth-1]) // Input sign bit known zero |
Zhou Sheng | 34a4b38 | 2007-03-28 17:38:21 +0000 | [diff] [blame] | 838 | KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth); |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 839 | else if (KnownOne[SrcBitWidth-1]) // Input sign bit known set |
Zhou Sheng | 34a4b38 | 2007-03-28 17:38:21 +0000 | [diff] [blame] | 840 | KnownOne |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 841 | return; |
| 842 | } |
| 843 | case Instruction::Shl: |
| 844 | // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0 |
| 845 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 846 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 847 | APInt Mask2(Mask.lshr(ShiftAmt)); |
| 848 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero, KnownOne, Depth+1); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 849 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
Zhou Sheng | 430f626 | 2007-03-12 05:44:52 +0000 | [diff] [blame] | 850 | KnownZero <<= ShiftAmt; |
| 851 | KnownOne <<= ShiftAmt; |
Reid Spencer | 2149a9d | 2007-03-25 19:55:33 +0000 | [diff] [blame] | 852 | KnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt); // low bits known 0 |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 853 | return; |
| 854 | } |
| 855 | break; |
| 856 | case Instruction::LShr: |
| 857 | // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0 |
| 858 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 859 | // Compute the new bits that are at the top now. |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 860 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 861 | |
| 862 | // Unsigned shift right. |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 863 | APInt Mask2(Mask.shl(ShiftAmt)); |
| 864 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero,KnownOne,Depth+1); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 865 | assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); |
| 866 | KnownZero = APIntOps::lshr(KnownZero, ShiftAmt); |
| 867 | KnownOne = APIntOps::lshr(KnownOne, ShiftAmt); |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 868 | // high bits known zero. |
| 869 | KnownZero |= APInt::getHighBitsSet(BitWidth, ShiftAmt); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 870 | return; |
| 871 | } |
| 872 | break; |
| 873 | case Instruction::AShr: |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 874 | // (ashr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0 |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 875 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 876 | // Compute the new bits that are at the top now. |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 877 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 878 | |
| 879 | // Signed shift right. |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 880 | APInt Mask2(Mask.shl(ShiftAmt)); |
| 881 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero,KnownOne,Depth+1); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 882 | assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); |
| 883 | KnownZero = APIntOps::lshr(KnownZero, ShiftAmt); |
| 884 | KnownOne = APIntOps::lshr(KnownOne, ShiftAmt); |
| 885 | |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 886 | APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt)); |
| 887 | if (KnownZero[BitWidth-ShiftAmt-1]) // New bits are known zero. |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 888 | KnownZero |= HighBits; |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 889 | else if (KnownOne[BitWidth-ShiftAmt-1]) // New bits are known one. |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 890 | KnownOne |= HighBits; |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 891 | return; |
| 892 | } |
| 893 | break; |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 894 | case Instruction::Sub: { |
| 895 | if (ConstantInt *CLHS = dyn_cast<ConstantInt>(I->getOperand(0))) { |
| 896 | // We know that the top bits of C-X are clear if X contains less bits |
| 897 | // than C (i.e. no wrap-around can happen). For example, 20-X is |
| 898 | // positive if we can prove that X is >= 0 and < 16. |
| 899 | if (!CLHS->getValue().isNegative()) { |
| 900 | unsigned NLZ = (CLHS->getValue()+1).countLeadingZeros(); |
| 901 | // NLZ can't be BitWidth with no sign bit |
| 902 | APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1); |
| 903 | ComputeMaskedBits(I->getOperand(1), MaskV, KnownZero, KnownOne, Depth+1); |
| 904 | |
| 905 | // If all of the MaskV bits are known to be zero, then we know the output |
| 906 | // top bits are zero, because we now know that the output is from [0-C]. |
| 907 | if ((KnownZero & MaskV) == MaskV) { |
| 908 | unsigned NLZ2 = CLHS->getValue().countLeadingZeros(); |
| 909 | // Top bits known zero. |
| 910 | KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask; |
| 911 | KnownOne = APInt(BitWidth, 0); // No one bits known. |
| 912 | } else { |
| 913 | KnownZero = KnownOne = APInt(BitWidth, 0); // Otherwise, nothing known. |
| 914 | } |
| 915 | return; |
| 916 | } |
| 917 | } |
| 918 | } |
| 919 | // fall through |
Duncan Sands | 1d57a75 | 2008-03-21 08:32:17 +0000 | [diff] [blame] | 920 | case Instruction::Add: { |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 921 | // If either the LHS or the RHS are Zero, the result is zero. |
| 922 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, Depth+1); |
| 923 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1); |
| 924 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 925 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 926 | |
| 927 | // Output known-0 bits are known if clear or set in both the low clear bits |
| 928 | // common to both LHS & RHS. For example, 8+(X<<3) is known to have the |
| 929 | // low 3 bits clear. |
| 930 | unsigned KnownZeroOut = std::min(KnownZero.countTrailingOnes(), |
| 931 | KnownZero2.countTrailingOnes()); |
| 932 | |
| 933 | KnownZero = APInt::getLowBitsSet(BitWidth, KnownZeroOut); |
| 934 | KnownOne = APInt(BitWidth, 0); |
| 935 | return; |
Duncan Sands | 1d57a75 | 2008-03-21 08:32:17 +0000 | [diff] [blame] | 936 | } |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 937 | case Instruction::SRem: |
| 938 | if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 939 | APInt RA = Rem->getValue(); |
| 940 | if (RA.isPowerOf2() || (-RA).isPowerOf2()) { |
| 941 | APInt LowBits = RA.isStrictlyPositive() ? ((RA - 1) | RA) : ~RA; |
| 942 | APInt Mask2 = LowBits | APInt::getSignBit(BitWidth); |
| 943 | ComputeMaskedBits(I->getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1); |
| 944 | |
| 945 | // The sign of a remainder is equal to the sign of the first |
| 946 | // operand (zero being positive). |
| 947 | if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits)) |
| 948 | KnownZero2 |= ~LowBits; |
| 949 | else if (KnownOne2[BitWidth-1]) |
| 950 | KnownOne2 |= ~LowBits; |
| 951 | |
| 952 | KnownZero |= KnownZero2 & Mask; |
| 953 | KnownOne |= KnownOne2 & Mask; |
| 954 | |
| 955 | assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); |
| 956 | } |
| 957 | } |
| 958 | break; |
| 959 | case Instruction::URem: |
| 960 | if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 961 | APInt RA = Rem->getValue(); |
| 962 | if (RA.isStrictlyPositive() && RA.isPowerOf2()) { |
| 963 | APInt LowBits = (RA - 1) | RA; |
| 964 | APInt Mask2 = LowBits & Mask; |
| 965 | KnownZero |= ~LowBits & Mask; |
| 966 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero, KnownOne,Depth+1); |
| 967 | assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); |
| 968 | } |
| 969 | } else { |
| 970 | // Since the result is less than or equal to RHS, any leading zero bits |
| 971 | // in RHS must also exist in the result. |
| 972 | APInt AllOnes = APInt::getAllOnesValue(BitWidth); |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 973 | ComputeMaskedBits(I->getOperand(1), AllOnes, KnownZero2, KnownOne2, |
| 974 | Depth+1); |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 975 | |
| 976 | uint32_t Leaders = KnownZero2.countLeadingOnes(); |
| 977 | KnownZero |= APInt::getHighBitsSet(BitWidth, Leaders) & Mask; |
| 978 | assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); |
| 979 | } |
| 980 | break; |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 981 | |
| 982 | case Instruction::Alloca: |
| 983 | case Instruction::Malloc: { |
| 984 | AllocationInst *AI = cast<AllocationInst>(V); |
| 985 | unsigned Align = AI->getAlignment(); |
| 986 | if (Align == 0 && TD) { |
| 987 | if (isa<AllocaInst>(AI)) |
| 988 | Align = TD->getPrefTypeAlignment(AI->getType()->getElementType()); |
| 989 | else if (isa<MallocInst>(AI)) { |
| 990 | // Malloc returns maximally aligned memory. |
| 991 | Align = TD->getABITypeAlignment(AI->getType()->getElementType()); |
| 992 | Align = |
| 993 | std::max(Align, |
| 994 | (unsigned)TD->getABITypeAlignment(Type::DoubleTy)); |
| 995 | Align = |
| 996 | std::max(Align, |
| 997 | (unsigned)TD->getABITypeAlignment(Type::Int64Ty)); |
| 998 | } |
| 999 | } |
| 1000 | |
| 1001 | if (Align > 0) |
| 1002 | KnownZero = Mask & APInt::getLowBitsSet(BitWidth, |
| 1003 | CountTrailingZeros_32(Align)); |
| 1004 | break; |
| 1005 | } |
| 1006 | case Instruction::GetElementPtr: { |
| 1007 | // Analyze all of the subscripts of this getelementptr instruction |
| 1008 | // to determine if we can prove known low zero bits. |
| 1009 | APInt LocalMask = APInt::getAllOnesValue(BitWidth); |
| 1010 | APInt LocalKnownZero(BitWidth, 0), LocalKnownOne(BitWidth, 0); |
| 1011 | ComputeMaskedBits(I->getOperand(0), LocalMask, |
| 1012 | LocalKnownZero, LocalKnownOne, Depth+1); |
| 1013 | unsigned TrailZ = LocalKnownZero.countTrailingOnes(); |
| 1014 | |
| 1015 | gep_type_iterator GTI = gep_type_begin(I); |
| 1016 | for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) { |
| 1017 | Value *Index = I->getOperand(i); |
| 1018 | if (const StructType *STy = dyn_cast<StructType>(*GTI)) { |
| 1019 | // Handle struct member offset arithmetic. |
| 1020 | if (!TD) return; |
| 1021 | const StructLayout *SL = TD->getStructLayout(STy); |
| 1022 | unsigned Idx = cast<ConstantInt>(Index)->getZExtValue(); |
| 1023 | uint64_t Offset = SL->getElementOffset(Idx); |
| 1024 | TrailZ = std::min(TrailZ, |
| 1025 | CountTrailingZeros_64(Offset)); |
| 1026 | } else { |
| 1027 | // Handle array index arithmetic. |
| 1028 | const Type *IndexedTy = GTI.getIndexedType(); |
| 1029 | if (!IndexedTy->isSized()) return; |
| 1030 | unsigned GEPOpiBits = Index->getType()->getPrimitiveSizeInBits(); |
| 1031 | uint64_t TypeSize = TD ? TD->getABITypeSize(IndexedTy) : 1; |
| 1032 | LocalMask = APInt::getAllOnesValue(GEPOpiBits); |
| 1033 | LocalKnownZero = LocalKnownOne = APInt(GEPOpiBits, 0); |
| 1034 | ComputeMaskedBits(Index, LocalMask, |
| 1035 | LocalKnownZero, LocalKnownOne, Depth+1); |
| 1036 | TrailZ = std::min(TrailZ, |
| 1037 | CountTrailingZeros_64(TypeSize) + |
| 1038 | LocalKnownZero.countTrailingOnes()); |
| 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) & Mask; |
| 1043 | break; |
| 1044 | } |
| 1045 | case Instruction::PHI: { |
| 1046 | PHINode *P = cast<PHINode>(I); |
| 1047 | // Handle the case of a simple two-predecessor recurrence PHI. |
| 1048 | // There's a lot more that could theoretically be done here, but |
| 1049 | // this is sufficient to catch some interesting cases. |
| 1050 | if (P->getNumIncomingValues() == 2) { |
| 1051 | for (unsigned i = 0; i != 2; ++i) { |
| 1052 | Value *L = P->getIncomingValue(i); |
| 1053 | Value *R = P->getIncomingValue(!i); |
| 1054 | User *LU = dyn_cast<User>(L); |
| 1055 | unsigned Opcode = LU ? getOpcode(LU) : (unsigned)Instruction::UserOp1; |
| 1056 | // Check for operations that have the property that if |
| 1057 | // both their operands have low zero bits, the result |
| 1058 | // will have low zero bits. |
| 1059 | if (Opcode == Instruction::Add || |
| 1060 | Opcode == Instruction::Sub || |
| 1061 | Opcode == Instruction::And || |
| 1062 | Opcode == Instruction::Or || |
| 1063 | Opcode == Instruction::Mul) { |
| 1064 | Value *LL = LU->getOperand(0); |
| 1065 | Value *LR = LU->getOperand(1); |
| 1066 | // Find a recurrence. |
| 1067 | if (LL == I) |
| 1068 | L = LR; |
| 1069 | else if (LR == I) |
| 1070 | L = LL; |
| 1071 | else |
| 1072 | break; |
| 1073 | // Ok, we have a PHI of the form L op= R. Check for low |
| 1074 | // zero bits. |
| 1075 | APInt Mask2 = APInt::getAllOnesValue(BitWidth); |
| 1076 | ComputeMaskedBits(R, Mask2, KnownZero2, KnownOne2, Depth+1); |
| 1077 | Mask2 = APInt::getLowBitsSet(BitWidth, |
| 1078 | KnownZero2.countTrailingOnes()); |
| 1079 | KnownOne2.clear(); |
| 1080 | KnownZero2.clear(); |
| 1081 | ComputeMaskedBits(L, Mask2, KnownZero2, KnownOne2, Depth+1); |
| 1082 | KnownZero = Mask & |
| 1083 | APInt::getLowBitsSet(BitWidth, |
| 1084 | KnownZero2.countTrailingOnes()); |
| 1085 | break; |
| 1086 | } |
| 1087 | } |
| 1088 | } |
| 1089 | break; |
| 1090 | } |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 1091 | } |
| 1092 | } |
| 1093 | |
Reid Spencer | e7816b5 | 2007-03-08 01:52:58 +0000 | [diff] [blame] | 1094 | /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use |
| 1095 | /// this predicate to simplify operations downstream. Mask is known to be zero |
| 1096 | /// for bits that V cannot have. |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 1097 | bool InstCombiner::MaskedValueIsZero(Value *V, const APInt& Mask, |
| 1098 | unsigned Depth) { |
Zhou Sheng | edd089c | 2007-03-12 16:54:56 +0000 | [diff] [blame] | 1099 | APInt KnownZero(Mask.getBitWidth(), 0), KnownOne(Mask.getBitWidth(), 0); |
Reid Spencer | e7816b5 | 2007-03-08 01:52:58 +0000 | [diff] [blame] | 1100 | ComputeMaskedBits(V, Mask, KnownZero, KnownOne, Depth); |
| 1101 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 1102 | return (KnownZero & Mask) == Mask; |
| 1103 | } |
| 1104 | |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1105 | /// ShrinkDemandedConstant - Check to see if the specified operand of the |
| 1106 | /// specified instruction is a constant integer. If so, check to see if there |
| 1107 | /// are any bits set in the constant that are not demanded. If so, shrink the |
| 1108 | /// constant and return true. |
| 1109 | static bool ShrinkDemandedConstant(Instruction *I, unsigned OpNo, |
Reid Spencer | 6b79e2d | 2007-03-12 17:15:10 +0000 | [diff] [blame] | 1110 | APInt Demanded) { |
| 1111 | assert(I && "No instruction?"); |
| 1112 | assert(OpNo < I->getNumOperands() && "Operand index too large"); |
| 1113 | |
| 1114 | // If the operand is not a constant integer, nothing to do. |
| 1115 | ConstantInt *OpC = dyn_cast<ConstantInt>(I->getOperand(OpNo)); |
| 1116 | if (!OpC) return false; |
| 1117 | |
| 1118 | // If there are no bits set that aren't demanded, nothing to do. |
| 1119 | Demanded.zextOrTrunc(OpC->getValue().getBitWidth()); |
| 1120 | if ((~Demanded & OpC->getValue()) == 0) |
| 1121 | return false; |
| 1122 | |
| 1123 | // This instruction is producing bits that are not demanded. Shrink the RHS. |
| 1124 | Demanded &= OpC->getValue(); |
| 1125 | I->setOperand(OpNo, ConstantInt::get(Demanded)); |
| 1126 | return true; |
| 1127 | } |
| 1128 | |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 1129 | // ComputeSignedMinMaxValuesFromKnownBits - Given a signed integer type and a |
| 1130 | // set of known zero and one bits, compute the maximum and minimum values that |
| 1131 | // could have the specified known zero and known one bits, returning them in |
| 1132 | // min/max. |
| 1133 | static void ComputeSignedMinMaxValuesFromKnownBits(const Type *Ty, |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 1134 | const APInt& KnownZero, |
| 1135 | const APInt& KnownOne, |
| 1136 | APInt& Min, APInt& Max) { |
| 1137 | uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth(); |
| 1138 | assert(KnownZero.getBitWidth() == BitWidth && |
| 1139 | KnownOne.getBitWidth() == BitWidth && |
| 1140 | Min.getBitWidth() == BitWidth && Max.getBitWidth() == BitWidth && |
| 1141 | "Ty, KnownZero, KnownOne and Min, Max must have equal bitwidth."); |
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 1142 | APInt UnknownBits = ~(KnownZero|KnownOne); |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 1143 | |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 1144 | // The minimum value is when all unknown bits are zeros, EXCEPT for the sign |
| 1145 | // bit if it is unknown. |
| 1146 | Min = KnownOne; |
| 1147 | Max = KnownOne|UnknownBits; |
| 1148 | |
Zhou Sheng | 4acf155 | 2007-03-28 05:15:57 +0000 | [diff] [blame] | 1149 | if (UnknownBits[BitWidth-1]) { // Sign bit is unknown |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 1150 | Min.set(BitWidth-1); |
| 1151 | Max.clear(BitWidth-1); |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 1152 | } |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | // ComputeUnsignedMinMaxValuesFromKnownBits - Given an unsigned integer type and |
| 1156 | // a set of known zero and one bits, compute the maximum and minimum values that |
| 1157 | // could have the specified known zero and known one bits, returning them in |
| 1158 | // min/max. |
| 1159 | static void ComputeUnsignedMinMaxValuesFromKnownBits(const Type *Ty, |
Chris Lattner | a9ff5eb | 2007-08-05 08:47:58 +0000 | [diff] [blame] | 1160 | const APInt &KnownZero, |
| 1161 | const APInt &KnownOne, |
| 1162 | APInt &Min, APInt &Max) { |
| 1163 | uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth(); BitWidth = BitWidth; |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 1164 | assert(KnownZero.getBitWidth() == BitWidth && |
| 1165 | KnownOne.getBitWidth() == BitWidth && |
| 1166 | Min.getBitWidth() == BitWidth && Max.getBitWidth() && |
| 1167 | "Ty, KnownZero, KnownOne and Min, Max must have equal bitwidth."); |
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 1168 | APInt UnknownBits = ~(KnownZero|KnownOne); |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 1169 | |
| 1170 | // The minimum value is when the unknown bits are all zeros. |
| 1171 | Min = KnownOne; |
| 1172 | // The maximum value is when the unknown bits are all ones. |
| 1173 | Max = KnownOne|UnknownBits; |
| 1174 | } |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1175 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1176 | /// SimplifyDemandedBits - This function attempts to replace V with a simpler |
| 1177 | /// value based on the demanded bits. When this function is called, it is known |
| 1178 | /// that only the bits set in DemandedMask of the result of V are ever used |
| 1179 | /// downstream. Consequently, depending on the mask and V, it may be possible |
| 1180 | /// to replace V with a constant or one of its operands. In such cases, this |
| 1181 | /// function does the replacement and returns true. In all other cases, it |
| 1182 | /// returns false after analyzing the expression and setting KnownOne and known |
| 1183 | /// to be one in the expression. KnownZero contains all the bits that are known |
| 1184 | /// to be zero in the expression. These are provided to potentially allow the |
| 1185 | /// caller (which might recursively be SimplifyDemandedBits itself) to simplify |
| 1186 | /// the expression. KnownOne and KnownZero always follow the invariant that |
| 1187 | /// KnownOne & KnownZero == 0. That is, a bit can't be both 1 and 0. Note that |
| 1188 | /// the bits in KnownOne and KnownZero may only be accurate for those bits set |
| 1189 | /// in DemandedMask. Note also that the bitwidth of V, DemandedMask, KnownZero |
| 1190 | /// and KnownOne must all be the same. |
| 1191 | bool InstCombiner::SimplifyDemandedBits(Value *V, APInt DemandedMask, |
| 1192 | APInt& KnownZero, APInt& KnownOne, |
| 1193 | unsigned Depth) { |
| 1194 | assert(V != 0 && "Null pointer of Value???"); |
| 1195 | assert(Depth <= 6 && "Limit Search Depth"); |
| 1196 | uint32_t BitWidth = DemandedMask.getBitWidth(); |
| 1197 | const IntegerType *VTy = cast<IntegerType>(V->getType()); |
| 1198 | assert(VTy->getBitWidth() == BitWidth && |
| 1199 | KnownZero.getBitWidth() == BitWidth && |
| 1200 | KnownOne.getBitWidth() == BitWidth && |
| 1201 | "Value *V, DemandedMask, KnownZero and KnownOne \ |
| 1202 | must have same BitWidth"); |
| 1203 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
| 1204 | // We know all of the bits for a constant! |
| 1205 | KnownOne = CI->getValue() & DemandedMask; |
| 1206 | KnownZero = ~KnownOne & DemandedMask; |
| 1207 | return false; |
| 1208 | } |
| 1209 | |
Zhou Sheng | 9670445 | 2007-03-14 03:21:24 +0000 | [diff] [blame] | 1210 | KnownZero.clear(); |
| 1211 | KnownOne.clear(); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1212 | if (!V->hasOneUse()) { // Other users may use these bits. |
| 1213 | if (Depth != 0) { // Not at the root. |
| 1214 | // Just compute the KnownZero/KnownOne bits to simplify things downstream. |
| 1215 | ComputeMaskedBits(V, DemandedMask, KnownZero, KnownOne, Depth); |
| 1216 | return false; |
| 1217 | } |
| 1218 | // If this is the root being simplified, allow it to have multiple uses, |
| 1219 | // just set the DemandedMask to all bits. |
| 1220 | DemandedMask = APInt::getAllOnesValue(BitWidth); |
| 1221 | } else if (DemandedMask == 0) { // Not demanding any bits from V. |
| 1222 | if (V != UndefValue::get(VTy)) |
| 1223 | return UpdateValueUsesWith(V, UndefValue::get(VTy)); |
| 1224 | return false; |
| 1225 | } else if (Depth == 6) { // Limit search depth. |
| 1226 | return false; |
| 1227 | } |
| 1228 | |
| 1229 | Instruction *I = dyn_cast<Instruction>(V); |
| 1230 | if (!I) return false; // Only analyze instructions. |
| 1231 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1232 | APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0); |
| 1233 | APInt &RHSKnownZero = KnownZero, &RHSKnownOne = KnownOne; |
| 1234 | switch (I->getOpcode()) { |
| 1235 | default: break; |
| 1236 | case Instruction::And: |
| 1237 | // If either the LHS or the RHS are Zero, the result is zero. |
| 1238 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, |
| 1239 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 1240 | return true; |
| 1241 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1242 | "Bits known to be one AND zero?"); |
| 1243 | |
| 1244 | // If something is known zero on the RHS, the bits aren't demanded on the |
| 1245 | // LHS. |
| 1246 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask & ~RHSKnownZero, |
| 1247 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1248 | return true; |
| 1249 | assert((LHSKnownZero & LHSKnownOne) == 0 && |
| 1250 | "Bits known to be one AND zero?"); |
| 1251 | |
| 1252 | // If all of the demanded bits are known 1 on one side, return the other. |
| 1253 | // These bits cannot contribute to the result of the 'and'. |
| 1254 | if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) == |
| 1255 | (DemandedMask & ~LHSKnownZero)) |
| 1256 | return UpdateValueUsesWith(I, I->getOperand(0)); |
| 1257 | if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) == |
| 1258 | (DemandedMask & ~RHSKnownZero)) |
| 1259 | return UpdateValueUsesWith(I, I->getOperand(1)); |
| 1260 | |
| 1261 | // If all of the demanded bits in the inputs are known zeros, return zero. |
| 1262 | if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask) |
| 1263 | return UpdateValueUsesWith(I, Constant::getNullValue(VTy)); |
| 1264 | |
| 1265 | // If the RHS is a constant, see if we can simplify it. |
| 1266 | if (ShrinkDemandedConstant(I, 1, DemandedMask & ~LHSKnownZero)) |
| 1267 | return UpdateValueUsesWith(I, I); |
| 1268 | |
| 1269 | // Output known-1 bits are only known if set in both the LHS & RHS. |
| 1270 | RHSKnownOne &= LHSKnownOne; |
| 1271 | // Output known-0 are known to be clear if zero in either the LHS | RHS. |
| 1272 | RHSKnownZero |= LHSKnownZero; |
| 1273 | break; |
| 1274 | case Instruction::Or: |
| 1275 | // If either the LHS or the RHS are One, the result is One. |
| 1276 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, |
| 1277 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 1278 | return true; |
| 1279 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1280 | "Bits known to be one AND zero?"); |
| 1281 | // If something is known one on the RHS, the bits aren't demanded on the |
| 1282 | // LHS. |
| 1283 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask & ~RHSKnownOne, |
| 1284 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1285 | return true; |
| 1286 | assert((LHSKnownZero & LHSKnownOne) == 0 && |
| 1287 | "Bits known to be one AND zero?"); |
| 1288 | |
| 1289 | // If all of the demanded bits are known zero on one side, return the other. |
| 1290 | // These bits cannot contribute to the result of the 'or'. |
| 1291 | if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) == |
| 1292 | (DemandedMask & ~LHSKnownOne)) |
| 1293 | return UpdateValueUsesWith(I, I->getOperand(0)); |
| 1294 | if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) == |
| 1295 | (DemandedMask & ~RHSKnownOne)) |
| 1296 | return UpdateValueUsesWith(I, I->getOperand(1)); |
| 1297 | |
| 1298 | // If all of the potentially set bits on one side are known to be set on |
| 1299 | // the other side, just use the 'other' side. |
| 1300 | if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) == |
| 1301 | (DemandedMask & (~RHSKnownZero))) |
| 1302 | return UpdateValueUsesWith(I, I->getOperand(0)); |
| 1303 | if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) == |
| 1304 | (DemandedMask & (~LHSKnownZero))) |
| 1305 | return UpdateValueUsesWith(I, I->getOperand(1)); |
| 1306 | |
| 1307 | // If the RHS is a constant, see if we can simplify it. |
| 1308 | if (ShrinkDemandedConstant(I, 1, DemandedMask)) |
| 1309 | return UpdateValueUsesWith(I, I); |
| 1310 | |
| 1311 | // Output known-0 bits are only known if clear in both the LHS & RHS. |
| 1312 | RHSKnownZero &= LHSKnownZero; |
| 1313 | // Output known-1 are known to be set if set in either the LHS | RHS. |
| 1314 | RHSKnownOne |= LHSKnownOne; |
| 1315 | break; |
| 1316 | case Instruction::Xor: { |
| 1317 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, |
| 1318 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 1319 | return true; |
| 1320 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1321 | "Bits known to be one AND zero?"); |
| 1322 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, |
| 1323 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1324 | return true; |
| 1325 | assert((LHSKnownZero & LHSKnownOne) == 0 && |
| 1326 | "Bits known to be one AND zero?"); |
| 1327 | |
| 1328 | // If all of the demanded bits are known zero on one side, return the other. |
| 1329 | // These bits cannot contribute to the result of the 'xor'. |
| 1330 | if ((DemandedMask & RHSKnownZero) == DemandedMask) |
| 1331 | return UpdateValueUsesWith(I, I->getOperand(0)); |
| 1332 | if ((DemandedMask & LHSKnownZero) == DemandedMask) |
| 1333 | return UpdateValueUsesWith(I, I->getOperand(1)); |
| 1334 | |
| 1335 | // Output known-0 bits are known if clear or set in both the LHS & RHS. |
| 1336 | APInt KnownZeroOut = (RHSKnownZero & LHSKnownZero) | |
| 1337 | (RHSKnownOne & LHSKnownOne); |
| 1338 | // Output known-1 are known to be set if set in only one of the LHS, RHS. |
| 1339 | APInt KnownOneOut = (RHSKnownZero & LHSKnownOne) | |
| 1340 | (RHSKnownOne & LHSKnownZero); |
| 1341 | |
| 1342 | // If all of the demanded bits are known to be zero on one side or the |
| 1343 | // other, turn this into an *inclusive* or. |
| 1344 | // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0 |
| 1345 | if ((DemandedMask & ~RHSKnownZero & ~LHSKnownZero) == 0) { |
| 1346 | Instruction *Or = |
| 1347 | BinaryOperator::createOr(I->getOperand(0), I->getOperand(1), |
| 1348 | I->getName()); |
| 1349 | InsertNewInstBefore(Or, *I); |
| 1350 | return UpdateValueUsesWith(I, Or); |
| 1351 | } |
| 1352 | |
| 1353 | // If all of the demanded bits on one side are known, and all of the set |
| 1354 | // bits on that side are also known to be set on the other side, turn this |
| 1355 | // into an AND, as we know the bits will be cleared. |
| 1356 | // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2 |
| 1357 | if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) { |
| 1358 | // all known |
| 1359 | if ((RHSKnownOne & LHSKnownOne) == RHSKnownOne) { |
| 1360 | Constant *AndC = ConstantInt::get(~RHSKnownOne & DemandedMask); |
| 1361 | Instruction *And = |
| 1362 | BinaryOperator::createAnd(I->getOperand(0), AndC, "tmp"); |
| 1363 | InsertNewInstBefore(And, *I); |
| 1364 | return UpdateValueUsesWith(I, And); |
| 1365 | } |
| 1366 | } |
| 1367 | |
| 1368 | // If the RHS is a constant, see if we can simplify it. |
| 1369 | // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1. |
| 1370 | if (ShrinkDemandedConstant(I, 1, DemandedMask)) |
| 1371 | return UpdateValueUsesWith(I, I); |
| 1372 | |
| 1373 | RHSKnownZero = KnownZeroOut; |
| 1374 | RHSKnownOne = KnownOneOut; |
| 1375 | break; |
| 1376 | } |
| 1377 | case Instruction::Select: |
| 1378 | if (SimplifyDemandedBits(I->getOperand(2), DemandedMask, |
| 1379 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 1380 | return true; |
| 1381 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, |
| 1382 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1383 | return true; |
| 1384 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1385 | "Bits known to be one AND zero?"); |
| 1386 | assert((LHSKnownZero & LHSKnownOne) == 0 && |
| 1387 | "Bits known to be one AND zero?"); |
| 1388 | |
| 1389 | // If the operands are constants, see if we can simplify them. |
| 1390 | if (ShrinkDemandedConstant(I, 1, DemandedMask)) |
| 1391 | return UpdateValueUsesWith(I, I); |
| 1392 | if (ShrinkDemandedConstant(I, 2, DemandedMask)) |
| 1393 | return UpdateValueUsesWith(I, I); |
| 1394 | |
| 1395 | // Only known if known in both the LHS and RHS. |
| 1396 | RHSKnownOne &= LHSKnownOne; |
| 1397 | RHSKnownZero &= LHSKnownZero; |
| 1398 | break; |
| 1399 | case Instruction::Trunc: { |
| 1400 | uint32_t truncBf = |
| 1401 | cast<IntegerType>(I->getOperand(0)->getType())->getBitWidth(); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1402 | DemandedMask.zext(truncBf); |
| 1403 | RHSKnownZero.zext(truncBf); |
| 1404 | RHSKnownOne.zext(truncBf); |
| 1405 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, |
| 1406 | RHSKnownZero, RHSKnownOne, Depth+1)) |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1407 | return true; |
| 1408 | DemandedMask.trunc(BitWidth); |
| 1409 | RHSKnownZero.trunc(BitWidth); |
| 1410 | RHSKnownOne.trunc(BitWidth); |
| 1411 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1412 | "Bits known to be one AND zero?"); |
| 1413 | break; |
| 1414 | } |
| 1415 | case Instruction::BitCast: |
| 1416 | if (!I->getOperand(0)->getType()->isInteger()) |
| 1417 | return false; |
| 1418 | |
| 1419 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, |
| 1420 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 1421 | return true; |
| 1422 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1423 | "Bits known to be one AND zero?"); |
| 1424 | break; |
| 1425 | case Instruction::ZExt: { |
| 1426 | // Compute the bits in the result that are not present in the input. |
| 1427 | const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType()); |
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 1428 | uint32_t SrcBitWidth = SrcTy->getBitWidth(); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1429 | |
Zhou Sheng | d48653a | 2007-03-29 04:45:55 +0000 | [diff] [blame] | 1430 | DemandedMask.trunc(SrcBitWidth); |
| 1431 | RHSKnownZero.trunc(SrcBitWidth); |
| 1432 | RHSKnownOne.trunc(SrcBitWidth); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1433 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, |
| 1434 | RHSKnownZero, RHSKnownOne, Depth+1)) |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1435 | return true; |
| 1436 | DemandedMask.zext(BitWidth); |
| 1437 | RHSKnownZero.zext(BitWidth); |
| 1438 | RHSKnownOne.zext(BitWidth); |
| 1439 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1440 | "Bits known to be one AND zero?"); |
| 1441 | // The top bits are known to be zero. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1442 | RHSKnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1443 | break; |
| 1444 | } |
| 1445 | case Instruction::SExt: { |
| 1446 | // Compute the bits in the result that are not present in the input. |
| 1447 | const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType()); |
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 1448 | uint32_t SrcBitWidth = SrcTy->getBitWidth(); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1449 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1450 | APInt InputDemandedBits = DemandedMask & |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1451 | APInt::getLowBitsSet(BitWidth, SrcBitWidth); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1452 | |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1453 | APInt NewBits(APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1454 | // If any of the sign extended bits are demanded, we know that the sign |
| 1455 | // bit is demanded. |
| 1456 | if ((NewBits & DemandedMask) != 0) |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 1457 | InputDemandedBits.set(SrcBitWidth-1); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1458 | |
Zhou Sheng | d48653a | 2007-03-29 04:45:55 +0000 | [diff] [blame] | 1459 | InputDemandedBits.trunc(SrcBitWidth); |
| 1460 | RHSKnownZero.trunc(SrcBitWidth); |
| 1461 | RHSKnownOne.trunc(SrcBitWidth); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1462 | if (SimplifyDemandedBits(I->getOperand(0), InputDemandedBits, |
| 1463 | RHSKnownZero, RHSKnownOne, Depth+1)) |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1464 | return true; |
| 1465 | InputDemandedBits.zext(BitWidth); |
| 1466 | RHSKnownZero.zext(BitWidth); |
| 1467 | RHSKnownOne.zext(BitWidth); |
| 1468 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1469 | "Bits known to be one AND zero?"); |
| 1470 | |
| 1471 | // If the sign bit of the input is known set or clear, then we know the |
| 1472 | // top bits of the result. |
| 1473 | |
| 1474 | // If the input sign bit is known zero, or if the NewBits are not demanded |
| 1475 | // convert this into a zero extension. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1476 | if (RHSKnownZero[SrcBitWidth-1] || (NewBits & ~DemandedMask) == NewBits) |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1477 | { |
| 1478 | // Convert to ZExt cast |
| 1479 | CastInst *NewCast = new ZExtInst(I->getOperand(0), VTy, I->getName(), I); |
| 1480 | return UpdateValueUsesWith(I, NewCast); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1481 | } else if (RHSKnownOne[SrcBitWidth-1]) { // Input sign bit known set |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1482 | RHSKnownOne |= NewBits; |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1483 | } |
| 1484 | break; |
| 1485 | } |
| 1486 | case Instruction::Add: { |
| 1487 | // Figure out what the input bits are. If the top bits of the and result |
| 1488 | // are not demanded, then the add doesn't demand them from its input |
| 1489 | // either. |
Reid Spencer | 55702aa | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 1490 | uint32_t NLZ = DemandedMask.countLeadingZeros(); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1491 | |
| 1492 | // If there is a constant on the RHS, there are a variety of xformations |
| 1493 | // we can do. |
| 1494 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 1495 | // If null, this should be simplified elsewhere. Some of the xforms here |
| 1496 | // won't work if the RHS is zero. |
| 1497 | if (RHS->isZero()) |
| 1498 | break; |
| 1499 | |
| 1500 | // If the top bit of the output is demanded, demand everything from the |
| 1501 | // input. Otherwise, we demand all the input bits except NLZ top bits. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1502 | APInt InDemandedBits(APInt::getLowBitsSet(BitWidth, BitWidth - NLZ)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1503 | |
| 1504 | // Find information about known zero/one bits in the input. |
| 1505 | if (SimplifyDemandedBits(I->getOperand(0), InDemandedBits, |
| 1506 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1507 | return true; |
| 1508 | |
| 1509 | // If the RHS of the add has bits set that can't affect the input, reduce |
| 1510 | // the constant. |
| 1511 | if (ShrinkDemandedConstant(I, 1, InDemandedBits)) |
| 1512 | return UpdateValueUsesWith(I, I); |
| 1513 | |
| 1514 | // Avoid excess work. |
| 1515 | if (LHSKnownZero == 0 && LHSKnownOne == 0) |
| 1516 | break; |
| 1517 | |
| 1518 | // Turn it into OR if input bits are zero. |
| 1519 | if ((LHSKnownZero & RHS->getValue()) == RHS->getValue()) { |
| 1520 | Instruction *Or = |
| 1521 | BinaryOperator::createOr(I->getOperand(0), I->getOperand(1), |
| 1522 | I->getName()); |
| 1523 | InsertNewInstBefore(Or, *I); |
| 1524 | return UpdateValueUsesWith(I, Or); |
| 1525 | } |
| 1526 | |
| 1527 | // We can say something about the output known-zero and known-one bits, |
| 1528 | // depending on potential carries from the input constant and the |
| 1529 | // unknowns. For example if the LHS is known to have at most the 0x0F0F0 |
| 1530 | // bits set and the RHS constant is 0x01001, then we know we have a known |
| 1531 | // one mask of 0x00001 and a known zero mask of 0xE0F0E. |
| 1532 | |
| 1533 | // To compute this, we first compute the potential carry bits. These are |
| 1534 | // the bits which may be modified. I'm not aware of a better way to do |
| 1535 | // this scan. |
Zhou Sheng | b9cb95f | 2007-03-31 02:38:39 +0000 | [diff] [blame] | 1536 | const APInt& RHSVal = RHS->getValue(); |
| 1537 | APInt CarryBits((~LHSKnownZero + RHSVal) ^ (~LHSKnownZero ^ RHSVal)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1538 | |
| 1539 | // Now that we know which bits have carries, compute the known-1/0 sets. |
| 1540 | |
| 1541 | // Bits are known one if they are known zero in one operand and one in the |
| 1542 | // other, and there is no input carry. |
| 1543 | RHSKnownOne = ((LHSKnownZero & RHSVal) | |
| 1544 | (LHSKnownOne & ~RHSVal)) & ~CarryBits; |
| 1545 | |
| 1546 | // Bits are known zero if they are known zero in both operands and there |
| 1547 | // is no input carry. |
| 1548 | RHSKnownZero = LHSKnownZero & ~RHSVal & ~CarryBits; |
| 1549 | } else { |
| 1550 | // If the high-bits of this ADD are not demanded, then it does not demand |
| 1551 | // the high bits of its LHS or RHS. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1552 | if (DemandedMask[BitWidth-1] == 0) { |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1553 | // Right fill the mask of bits for this ADD to demand the most |
| 1554 | // significant bit and all those below it. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1555 | APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1556 | if (SimplifyDemandedBits(I->getOperand(0), DemandedFromOps, |
| 1557 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1558 | return true; |
| 1559 | if (SimplifyDemandedBits(I->getOperand(1), DemandedFromOps, |
| 1560 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1561 | return true; |
| 1562 | } |
| 1563 | } |
| 1564 | break; |
| 1565 | } |
| 1566 | case Instruction::Sub: |
| 1567 | // If the high-bits of this SUB are not demanded, then it does not demand |
| 1568 | // the high bits of its LHS or RHS. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1569 | if (DemandedMask[BitWidth-1] == 0) { |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1570 | // Right fill the mask of bits for this SUB to demand the most |
| 1571 | // significant bit and all those below it. |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 1572 | uint32_t NLZ = DemandedMask.countLeadingZeros(); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1573 | APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1574 | if (SimplifyDemandedBits(I->getOperand(0), DemandedFromOps, |
| 1575 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1576 | return true; |
| 1577 | if (SimplifyDemandedBits(I->getOperand(1), DemandedFromOps, |
| 1578 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1579 | return true; |
| 1580 | } |
| 1581 | break; |
| 1582 | case Instruction::Shl: |
| 1583 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 1584 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1585 | APInt DemandedMaskIn(DemandedMask.lshr(ShiftAmt)); |
| 1586 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMaskIn, |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1587 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 1588 | return true; |
| 1589 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1590 | "Bits known to be one AND zero?"); |
| 1591 | RHSKnownZero <<= ShiftAmt; |
| 1592 | RHSKnownOne <<= ShiftAmt; |
| 1593 | // low bits known zero. |
Zhou Sheng | adc1495 | 2007-03-14 09:07:33 +0000 | [diff] [blame] | 1594 | if (ShiftAmt) |
Zhou Sheng | e9e03f6 | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 1595 | RHSKnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1596 | } |
| 1597 | break; |
| 1598 | case Instruction::LShr: |
| 1599 | // For a logical shift right |
| 1600 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 1601 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1602 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1603 | // Unsigned shift right. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1604 | APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt)); |
| 1605 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMaskIn, |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1606 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 1607 | return true; |
| 1608 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1609 | "Bits known to be one AND zero?"); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1610 | RHSKnownZero = APIntOps::lshr(RHSKnownZero, ShiftAmt); |
| 1611 | RHSKnownOne = APIntOps::lshr(RHSKnownOne, ShiftAmt); |
Zhou Sheng | adc1495 | 2007-03-14 09:07:33 +0000 | [diff] [blame] | 1612 | if (ShiftAmt) { |
| 1613 | // Compute the new bits that are at the top now. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1614 | APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt)); |
Zhou Sheng | adc1495 | 2007-03-14 09:07:33 +0000 | [diff] [blame] | 1615 | RHSKnownZero |= HighBits; // high bits known zero. |
| 1616 | } |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1617 | } |
| 1618 | break; |
| 1619 | case Instruction::AShr: |
| 1620 | // If this is an arithmetic shift right and only the low-bit is set, we can |
| 1621 | // always convert this into a logical shr, even if the shift amount is |
| 1622 | // variable. The low bit of the shift cannot be an input sign bit unless |
| 1623 | // the shift amount is >= the size of the datatype, which is undefined. |
| 1624 | if (DemandedMask == 1) { |
| 1625 | // Perform the logical shift right. |
| 1626 | Value *NewVal = BinaryOperator::createLShr( |
| 1627 | I->getOperand(0), I->getOperand(1), I->getName()); |
| 1628 | InsertNewInstBefore(cast<Instruction>(NewVal), *I); |
| 1629 | return UpdateValueUsesWith(I, NewVal); |
| 1630 | } |
Chris Lattner | 4241e4d | 2007-07-15 20:54:51 +0000 | [diff] [blame] | 1631 | |
| 1632 | // If the sign bit is the only bit demanded by this ashr, then there is no |
| 1633 | // need to do it, the shift doesn't change the high bit. |
| 1634 | if (DemandedMask.isSignBit()) |
| 1635 | return UpdateValueUsesWith(I, I->getOperand(0)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1636 | |
| 1637 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 1638 | uint32_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1639 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1640 | // Signed shift right. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1641 | APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt)); |
Lauro Ramos Venancio | d0499af | 2007-06-06 17:08:48 +0000 | [diff] [blame] | 1642 | // If any of the "high bits" are demanded, we should set the sign bit as |
| 1643 | // demanded. |
| 1644 | if (DemandedMask.countLeadingZeros() <= ShiftAmt) |
| 1645 | DemandedMaskIn.set(BitWidth-1); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1646 | if (SimplifyDemandedBits(I->getOperand(0), |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1647 | DemandedMaskIn, |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1648 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 1649 | return true; |
| 1650 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1651 | "Bits known to be one AND zero?"); |
| 1652 | // Compute the new bits that are at the top now. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1653 | APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1654 | RHSKnownZero = APIntOps::lshr(RHSKnownZero, ShiftAmt); |
| 1655 | RHSKnownOne = APIntOps::lshr(RHSKnownOne, ShiftAmt); |
| 1656 | |
| 1657 | // Handle the sign bits. |
| 1658 | APInt SignBit(APInt::getSignBit(BitWidth)); |
| 1659 | // Adjust to where it is now in the mask. |
| 1660 | SignBit = APIntOps::lshr(SignBit, ShiftAmt); |
| 1661 | |
| 1662 | // If the input sign bit is known to be zero, or if none of the top bits |
| 1663 | // are demanded, turn this into an unsigned shift right. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1664 | if (RHSKnownZero[BitWidth-ShiftAmt-1] || |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1665 | (HighBits & ~DemandedMask) == HighBits) { |
| 1666 | // Perform the logical shift right. |
| 1667 | Value *NewVal = BinaryOperator::createLShr( |
| 1668 | I->getOperand(0), SA, I->getName()); |
| 1669 | InsertNewInstBefore(cast<Instruction>(NewVal), *I); |
| 1670 | return UpdateValueUsesWith(I, NewVal); |
| 1671 | } else if ((RHSKnownOne & SignBit) != 0) { // New bits are known one. |
| 1672 | RHSKnownOne |= HighBits; |
| 1673 | } |
| 1674 | } |
| 1675 | break; |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 1676 | case Instruction::SRem: |
| 1677 | if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 1678 | APInt RA = Rem->getValue(); |
| 1679 | if (RA.isPowerOf2() || (-RA).isPowerOf2()) { |
| 1680 | APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) | RA : ~RA; |
| 1681 | APInt Mask2 = LowBits | APInt::getSignBit(BitWidth); |
| 1682 | if (SimplifyDemandedBits(I->getOperand(0), Mask2, |
| 1683 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1684 | return true; |
| 1685 | |
| 1686 | if (LHSKnownZero[BitWidth-1] || ((LHSKnownZero & LowBits) == LowBits)) |
| 1687 | LHSKnownZero |= ~LowBits; |
| 1688 | else if (LHSKnownOne[BitWidth-1]) |
| 1689 | LHSKnownOne |= ~LowBits; |
| 1690 | |
| 1691 | KnownZero |= LHSKnownZero & DemandedMask; |
| 1692 | KnownOne |= LHSKnownOne & DemandedMask; |
| 1693 | |
| 1694 | assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); |
| 1695 | } |
| 1696 | } |
| 1697 | break; |
| 1698 | case Instruction::URem: |
| 1699 | if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 1700 | APInt RA = Rem->getValue(); |
| 1701 | if (RA.isPowerOf2()) { |
| 1702 | APInt LowBits = (RA - 1) | RA; |
| 1703 | APInt Mask2 = LowBits & DemandedMask; |
| 1704 | KnownZero |= ~LowBits & DemandedMask; |
| 1705 | if (SimplifyDemandedBits(I->getOperand(0), Mask2, |
| 1706 | KnownZero, KnownOne, Depth+1)) |
| 1707 | return true; |
| 1708 | |
| 1709 | assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); |
| 1710 | } |
| 1711 | } else { |
| 1712 | APInt KnownZero2(BitWidth, 0), KnownOne2(BitWidth, 0); |
| 1713 | APInt AllOnes = APInt::getAllOnesValue(BitWidth); |
| 1714 | if (SimplifyDemandedBits(I->getOperand(1), AllOnes, |
| 1715 | KnownZero2, KnownOne2, Depth+1)) |
| 1716 | return true; |
| 1717 | |
| 1718 | uint32_t Leaders = KnownZero2.countLeadingOnes(); |
| 1719 | KnownZero |= APInt::getHighBitsSet(BitWidth, Leaders) & DemandedMask; |
| 1720 | } |
| 1721 | break; |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1722 | } |
| 1723 | |
| 1724 | // If the client is only demanding bits that we know, return the known |
| 1725 | // constant. |
| 1726 | if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) |
| 1727 | return UpdateValueUsesWith(I, ConstantInt::get(RHSKnownOne)); |
| 1728 | return false; |
| 1729 | } |
| 1730 | |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1731 | |
| 1732 | /// SimplifyDemandedVectorElts - The specified value producecs a vector with |
| 1733 | /// 64 or fewer elements. DemandedElts contains the set of elements that are |
| 1734 | /// actually used by the caller. This method analyzes which elements of the |
| 1735 | /// operand are undef and returns that information in UndefElts. |
| 1736 | /// |
| 1737 | /// If the information about demanded elements can be used to simplify the |
| 1738 | /// operation, the operation is simplified, then the resultant value is |
| 1739 | /// returned. This returns null if no change was made. |
| 1740 | Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, uint64_t DemandedElts, |
| 1741 | uint64_t &UndefElts, |
| 1742 | unsigned Depth) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1743 | unsigned VWidth = cast<VectorType>(V->getType())->getNumElements(); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1744 | assert(VWidth <= 64 && "Vector too wide to analyze!"); |
| 1745 | uint64_t EltMask = ~0ULL >> (64-VWidth); |
| 1746 | assert(DemandedElts != EltMask && (DemandedElts & ~EltMask) == 0 && |
| 1747 | "Invalid DemandedElts!"); |
| 1748 | |
| 1749 | if (isa<UndefValue>(V)) { |
| 1750 | // If the entire vector is undefined, just return this info. |
| 1751 | UndefElts = EltMask; |
| 1752 | return 0; |
| 1753 | } else if (DemandedElts == 0) { // If nothing is demanded, provide undef. |
| 1754 | UndefElts = EltMask; |
| 1755 | return UndefValue::get(V->getType()); |
| 1756 | } |
| 1757 | |
| 1758 | UndefElts = 0; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1759 | if (ConstantVector *CP = dyn_cast<ConstantVector>(V)) { |
| 1760 | const Type *EltTy = cast<VectorType>(V->getType())->getElementType(); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1761 | Constant *Undef = UndefValue::get(EltTy); |
| 1762 | |
| 1763 | std::vector<Constant*> Elts; |
| 1764 | for (unsigned i = 0; i != VWidth; ++i) |
| 1765 | if (!(DemandedElts & (1ULL << i))) { // If not demanded, set to undef. |
| 1766 | Elts.push_back(Undef); |
| 1767 | UndefElts |= (1ULL << i); |
| 1768 | } else if (isa<UndefValue>(CP->getOperand(i))) { // Already undef. |
| 1769 | Elts.push_back(Undef); |
| 1770 | UndefElts |= (1ULL << i); |
| 1771 | } else { // Otherwise, defined. |
| 1772 | Elts.push_back(CP->getOperand(i)); |
| 1773 | } |
| 1774 | |
| 1775 | // If we changed the constant, return it. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1776 | Constant *NewCP = ConstantVector::get(Elts); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1777 | return NewCP != CP ? NewCP : 0; |
| 1778 | } else if (isa<ConstantAggregateZero>(V)) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1779 | // Simplify the CAZ to a ConstantVector where the non-demanded elements are |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1780 | // set to undef. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1781 | const Type *EltTy = cast<VectorType>(V->getType())->getElementType(); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1782 | Constant *Zero = Constant::getNullValue(EltTy); |
| 1783 | Constant *Undef = UndefValue::get(EltTy); |
| 1784 | std::vector<Constant*> Elts; |
| 1785 | for (unsigned i = 0; i != VWidth; ++i) |
| 1786 | Elts.push_back((DemandedElts & (1ULL << i)) ? Zero : Undef); |
| 1787 | UndefElts = DemandedElts ^ EltMask; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1788 | return ConstantVector::get(Elts); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1789 | } |
| 1790 | |
| 1791 | if (!V->hasOneUse()) { // Other users may use these bits. |
| 1792 | if (Depth != 0) { // Not at the root. |
| 1793 | // TODO: Just compute the UndefElts information recursively. |
| 1794 | return false; |
| 1795 | } |
| 1796 | return false; |
| 1797 | } else if (Depth == 10) { // Limit search depth. |
| 1798 | return false; |
| 1799 | } |
| 1800 | |
| 1801 | Instruction *I = dyn_cast<Instruction>(V); |
| 1802 | if (!I) return false; // Only analyze instructions. |
| 1803 | |
| 1804 | bool MadeChange = false; |
| 1805 | uint64_t UndefElts2; |
| 1806 | Value *TmpV; |
| 1807 | switch (I->getOpcode()) { |
| 1808 | default: break; |
| 1809 | |
| 1810 | case Instruction::InsertElement: { |
| 1811 | // If this is a variable index, we don't know which element it overwrites. |
| 1812 | // demand exactly the same input as we produce. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1813 | ConstantInt *Idx = dyn_cast<ConstantInt>(I->getOperand(2)); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1814 | if (Idx == 0) { |
| 1815 | // Note that we can't propagate undef elt info, because we don't know |
| 1816 | // which elt is getting updated. |
| 1817 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, |
| 1818 | UndefElts2, Depth+1); |
| 1819 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } |
| 1820 | break; |
| 1821 | } |
| 1822 | |
| 1823 | // If this is inserting an element that isn't demanded, remove this |
| 1824 | // insertelement. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1825 | unsigned IdxNo = Idx->getZExtValue(); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1826 | if (IdxNo >= VWidth || (DemandedElts & (1ULL << IdxNo)) == 0) |
| 1827 | return AddSoonDeadInstToWorklist(*I, 0); |
| 1828 | |
| 1829 | // Otherwise, the element inserted overwrites whatever was there, so the |
| 1830 | // input demanded set is simpler than the output set. |
| 1831 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), |
| 1832 | DemandedElts & ~(1ULL << IdxNo), |
| 1833 | UndefElts, Depth+1); |
| 1834 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } |
| 1835 | |
| 1836 | // The inserted element is defined. |
| 1837 | UndefElts |= 1ULL << IdxNo; |
| 1838 | break; |
| 1839 | } |
Chris Lattner | 6987833 | 2007-04-14 22:29:23 +0000 | [diff] [blame] | 1840 | case Instruction::BitCast: { |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 1841 | // Vector->vector casts only. |
Chris Lattner | 6987833 | 2007-04-14 22:29:23 +0000 | [diff] [blame] | 1842 | const VectorType *VTy = dyn_cast<VectorType>(I->getOperand(0)->getType()); |
| 1843 | if (!VTy) break; |
| 1844 | unsigned InVWidth = VTy->getNumElements(); |
| 1845 | uint64_t InputDemandedElts = 0; |
| 1846 | unsigned Ratio; |
| 1847 | |
| 1848 | if (VWidth == InVWidth) { |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 1849 | // 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] | 1850 | // elements as are demanded of us. |
| 1851 | Ratio = 1; |
| 1852 | InputDemandedElts = DemandedElts; |
| 1853 | } else if (VWidth > InVWidth) { |
| 1854 | // Untested so far. |
| 1855 | break; |
| 1856 | |
| 1857 | // If there are more elements in the result than there are in the source, |
| 1858 | // then an input element is live if any of the corresponding output |
| 1859 | // elements are live. |
| 1860 | Ratio = VWidth/InVWidth; |
| 1861 | for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) { |
| 1862 | if (DemandedElts & (1ULL << OutIdx)) |
| 1863 | InputDemandedElts |= 1ULL << (OutIdx/Ratio); |
| 1864 | } |
| 1865 | } else { |
| 1866 | // Untested so far. |
| 1867 | break; |
| 1868 | |
| 1869 | // If there are more elements in the source than there are in the result, |
| 1870 | // then an input element is live if the corresponding output element is |
| 1871 | // live. |
| 1872 | Ratio = InVWidth/VWidth; |
| 1873 | for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx) |
| 1874 | if (DemandedElts & (1ULL << InIdx/Ratio)) |
| 1875 | InputDemandedElts |= 1ULL << InIdx; |
| 1876 | } |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1877 | |
Chris Lattner | 6987833 | 2007-04-14 22:29:23 +0000 | [diff] [blame] | 1878 | // div/rem demand all inputs, because they don't want divide by zero. |
| 1879 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), InputDemandedElts, |
| 1880 | UndefElts2, Depth+1); |
| 1881 | if (TmpV) { |
| 1882 | I->setOperand(0, TmpV); |
| 1883 | MadeChange = true; |
| 1884 | } |
| 1885 | |
| 1886 | UndefElts = UndefElts2; |
| 1887 | if (VWidth > InVWidth) { |
| 1888 | assert(0 && "Unimp"); |
| 1889 | // If there are more elements in the result than there are in the source, |
| 1890 | // then an output element is undef if the corresponding input element is |
| 1891 | // undef. |
| 1892 | for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) |
| 1893 | if (UndefElts2 & (1ULL << (OutIdx/Ratio))) |
| 1894 | UndefElts |= 1ULL << OutIdx; |
| 1895 | } else if (VWidth < InVWidth) { |
| 1896 | assert(0 && "Unimp"); |
| 1897 | // If there are more elements in the source than there are in the result, |
| 1898 | // then a result element is undef if all of the corresponding input |
| 1899 | // elements are undef. |
| 1900 | UndefElts = ~0ULL >> (64-VWidth); // Start out all undef. |
| 1901 | for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx) |
| 1902 | if ((UndefElts2 & (1ULL << InIdx)) == 0) // Not undef? |
| 1903 | UndefElts &= ~(1ULL << (InIdx/Ratio)); // Clear undef bit. |
| 1904 | } |
| 1905 | break; |
| 1906 | } |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1907 | case Instruction::And: |
| 1908 | case Instruction::Or: |
| 1909 | case Instruction::Xor: |
| 1910 | case Instruction::Add: |
| 1911 | case Instruction::Sub: |
| 1912 | case Instruction::Mul: |
| 1913 | // div/rem demand all inputs, because they don't want divide by zero. |
| 1914 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, |
| 1915 | UndefElts, Depth+1); |
| 1916 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } |
| 1917 | TmpV = SimplifyDemandedVectorElts(I->getOperand(1), DemandedElts, |
| 1918 | UndefElts2, Depth+1); |
| 1919 | if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; } |
| 1920 | |
| 1921 | // Output elements are undefined if both are undefined. Consider things |
| 1922 | // like undef&0. The result is known zero, not undef. |
| 1923 | UndefElts &= UndefElts2; |
| 1924 | break; |
| 1925 | |
| 1926 | case Instruction::Call: { |
| 1927 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(I); |
| 1928 | if (!II) break; |
| 1929 | switch (II->getIntrinsicID()) { |
| 1930 | default: break; |
| 1931 | |
| 1932 | // Binary vector operations that work column-wise. A dest element is a |
| 1933 | // function of the corresponding input elements from the two inputs. |
| 1934 | case Intrinsic::x86_sse_sub_ss: |
| 1935 | case Intrinsic::x86_sse_mul_ss: |
| 1936 | case Intrinsic::x86_sse_min_ss: |
| 1937 | case Intrinsic::x86_sse_max_ss: |
| 1938 | case Intrinsic::x86_sse2_sub_sd: |
| 1939 | case Intrinsic::x86_sse2_mul_sd: |
| 1940 | case Intrinsic::x86_sse2_min_sd: |
| 1941 | case Intrinsic::x86_sse2_max_sd: |
| 1942 | TmpV = SimplifyDemandedVectorElts(II->getOperand(1), DemandedElts, |
| 1943 | UndefElts, Depth+1); |
| 1944 | if (TmpV) { II->setOperand(1, TmpV); MadeChange = true; } |
| 1945 | TmpV = SimplifyDemandedVectorElts(II->getOperand(2), DemandedElts, |
| 1946 | UndefElts2, Depth+1); |
| 1947 | if (TmpV) { II->setOperand(2, TmpV); MadeChange = true; } |
| 1948 | |
| 1949 | // If only the low elt is demanded and this is a scalarizable intrinsic, |
| 1950 | // scalarize it now. |
| 1951 | if (DemandedElts == 1) { |
| 1952 | switch (II->getIntrinsicID()) { |
| 1953 | default: break; |
| 1954 | case Intrinsic::x86_sse_sub_ss: |
| 1955 | case Intrinsic::x86_sse_mul_ss: |
| 1956 | case Intrinsic::x86_sse2_sub_sd: |
| 1957 | case Intrinsic::x86_sse2_mul_sd: |
| 1958 | // TODO: Lower MIN/MAX/ABS/etc |
| 1959 | Value *LHS = II->getOperand(1); |
| 1960 | Value *RHS = II->getOperand(2); |
| 1961 | // Extract the element as scalars. |
| 1962 | LHS = InsertNewInstBefore(new ExtractElementInst(LHS, 0U,"tmp"), *II); |
| 1963 | RHS = InsertNewInstBefore(new ExtractElementInst(RHS, 0U,"tmp"), *II); |
| 1964 | |
| 1965 | switch (II->getIntrinsicID()) { |
| 1966 | default: assert(0 && "Case stmts out of sync!"); |
| 1967 | case Intrinsic::x86_sse_sub_ss: |
| 1968 | case Intrinsic::x86_sse2_sub_sd: |
| 1969 | TmpV = InsertNewInstBefore(BinaryOperator::createSub(LHS, RHS, |
| 1970 | II->getName()), *II); |
| 1971 | break; |
| 1972 | case Intrinsic::x86_sse_mul_ss: |
| 1973 | case Intrinsic::x86_sse2_mul_sd: |
| 1974 | TmpV = InsertNewInstBefore(BinaryOperator::createMul(LHS, RHS, |
| 1975 | II->getName()), *II); |
| 1976 | break; |
| 1977 | } |
| 1978 | |
| 1979 | Instruction *New = |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 1980 | InsertElementInst::Create(UndefValue::get(II->getType()), TmpV, 0U, |
| 1981 | II->getName()); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1982 | InsertNewInstBefore(New, *II); |
| 1983 | AddSoonDeadInstToWorklist(*II, 0); |
| 1984 | return New; |
| 1985 | } |
| 1986 | } |
| 1987 | |
| 1988 | // Output elements are undefined if both are undefined. Consider things |
| 1989 | // like undef&0. The result is known zero, not undef. |
| 1990 | UndefElts &= UndefElts2; |
| 1991 | break; |
| 1992 | } |
| 1993 | break; |
| 1994 | } |
| 1995 | } |
| 1996 | return MadeChange ? I : 0; |
| 1997 | } |
| 1998 | |
Nick Lewycky | 455e176 | 2007-09-06 02:40:25 +0000 | [diff] [blame] | 1999 | /// @returns true if the specified compare predicate is |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2000 | /// true when both operands are equal... |
Nick Lewycky | 455e176 | 2007-09-06 02:40:25 +0000 | [diff] [blame] | 2001 | /// @brief Determine if the icmp Predicate is true when both operands are equal |
| 2002 | static bool isTrueWhenEqual(ICmpInst::Predicate pred) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2003 | return pred == ICmpInst::ICMP_EQ || pred == ICmpInst::ICMP_UGE || |
| 2004 | pred == ICmpInst::ICMP_SGE || pred == ICmpInst::ICMP_ULE || |
| 2005 | pred == ICmpInst::ICMP_SLE; |
| 2006 | } |
| 2007 | |
Nick Lewycky | 455e176 | 2007-09-06 02:40:25 +0000 | [diff] [blame] | 2008 | /// @returns true if the specified compare instruction is |
| 2009 | /// true when both operands are equal... |
| 2010 | /// @brief Determine if the ICmpInst returns true when both operands are equal |
| 2011 | static bool isTrueWhenEqual(ICmpInst &ICI) { |
| 2012 | return isTrueWhenEqual(ICI.getPredicate()); |
| 2013 | } |
| 2014 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2015 | /// AssociativeOpt - Perform an optimization on an associative operator. This |
| 2016 | /// function is designed to check a chain of associative operators for a |
| 2017 | /// potential to apply a certain optimization. Since the optimization may be |
| 2018 | /// applicable if the expression was reassociated, this checks the chain, then |
| 2019 | /// reassociates the expression as necessary to expose the optimization |
| 2020 | /// opportunity. This makes use of a special Functor, which must define |
| 2021 | /// 'shouldApply' and 'apply' methods. |
| 2022 | /// |
| 2023 | template<typename Functor> |
| 2024 | Instruction *AssociativeOpt(BinaryOperator &Root, const Functor &F) { |
| 2025 | unsigned Opcode = Root.getOpcode(); |
| 2026 | Value *LHS = Root.getOperand(0); |
| 2027 | |
| 2028 | // Quick check, see if the immediate LHS matches... |
| 2029 | if (F.shouldApply(LHS)) |
| 2030 | return F.apply(Root); |
| 2031 | |
| 2032 | // Otherwise, if the LHS is not of the same opcode as the root, return. |
| 2033 | Instruction *LHSI = dyn_cast<Instruction>(LHS); |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 2034 | while (LHSI && LHSI->getOpcode() == Opcode && LHSI->hasOneUse()) { |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2035 | // Should we apply this transform to the RHS? |
| 2036 | bool ShouldApply = F.shouldApply(LHSI->getOperand(1)); |
| 2037 | |
| 2038 | // If not to the RHS, check to see if we should apply to the LHS... |
| 2039 | if (!ShouldApply && F.shouldApply(LHSI->getOperand(0))) { |
| 2040 | cast<BinaryOperator>(LHSI)->swapOperands(); // Make the LHS the RHS |
| 2041 | ShouldApply = true; |
| 2042 | } |
| 2043 | |
| 2044 | // If the functor wants to apply the optimization to the RHS of LHSI, |
| 2045 | // reassociate the expression from ((? op A) op B) to (? op (A op B)) |
| 2046 | if (ShouldApply) { |
| 2047 | BasicBlock *BB = Root.getParent(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2048 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2049 | // Now all of the instructions are in the current basic block, go ahead |
| 2050 | // and perform the reassociation. |
| 2051 | Instruction *TmpLHSI = cast<Instruction>(Root.getOperand(0)); |
| 2052 | |
| 2053 | // First move the selected RHS to the LHS of the root... |
| 2054 | Root.setOperand(0, LHSI->getOperand(1)); |
| 2055 | |
| 2056 | // Make what used to be the LHS of the root be the user of the root... |
| 2057 | Value *ExtraOperand = TmpLHSI->getOperand(1); |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 2058 | if (&Root == TmpLHSI) { |
Chris Lattner | 15a76c0 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 2059 | Root.replaceAllUsesWith(Constant::getNullValue(TmpLHSI->getType())); |
| 2060 | return 0; |
| 2061 | } |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 2062 | Root.replaceAllUsesWith(TmpLHSI); // Users now use TmpLHSI |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2063 | TmpLHSI->setOperand(1, &Root); // TmpLHSI now uses the root |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 2064 | TmpLHSI->getParent()->getInstList().remove(TmpLHSI); |
| 2065 | BasicBlock::iterator ARI = &Root; ++ARI; |
| 2066 | BB->getInstList().insert(ARI, TmpLHSI); // Move TmpLHSI to after Root |
| 2067 | ARI = Root; |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2068 | |
| 2069 | // Now propagate the ExtraOperand down the chain of instructions until we |
| 2070 | // get to LHSI. |
| 2071 | while (TmpLHSI != LHSI) { |
| 2072 | Instruction *NextLHSI = cast<Instruction>(TmpLHSI->getOperand(0)); |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 2073 | // Move the instruction to immediately before the chain we are |
| 2074 | // constructing to avoid breaking dominance properties. |
| 2075 | NextLHSI->getParent()->getInstList().remove(NextLHSI); |
| 2076 | BB->getInstList().insert(ARI, NextLHSI); |
| 2077 | ARI = NextLHSI; |
| 2078 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2079 | Value *NextOp = NextLHSI->getOperand(1); |
| 2080 | NextLHSI->setOperand(1, ExtraOperand); |
| 2081 | TmpLHSI = NextLHSI; |
| 2082 | ExtraOperand = NextOp; |
| 2083 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2084 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2085 | // Now that the instructions are reassociated, have the functor perform |
| 2086 | // the transformation... |
| 2087 | return F.apply(Root); |
| 2088 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2089 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2090 | LHSI = dyn_cast<Instruction>(LHSI->getOperand(0)); |
| 2091 | } |
| 2092 | return 0; |
| 2093 | } |
| 2094 | |
| 2095 | |
| 2096 | // AddRHS - Implements: X + X --> X << 1 |
| 2097 | struct AddRHS { |
| 2098 | Value *RHS; |
| 2099 | AddRHS(Value *rhs) : RHS(rhs) {} |
| 2100 | bool shouldApply(Value *LHS) const { return LHS == RHS; } |
| 2101 | Instruction *apply(BinaryOperator &Add) const { |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 2102 | return BinaryOperator::createShl(Add.getOperand(0), |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2103 | ConstantInt::get(Add.getType(), 1)); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2104 | } |
| 2105 | }; |
| 2106 | |
| 2107 | // AddMaskingAnd - Implements (A & C1)+(B & C2) --> (A & C1)|(B & C2) |
| 2108 | // iff C1&C2 == 0 |
| 2109 | struct AddMaskingAnd { |
| 2110 | Constant *C2; |
| 2111 | AddMaskingAnd(Constant *c) : C2(c) {} |
| 2112 | bool shouldApply(Value *LHS) const { |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2113 | ConstantInt *C1; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2114 | return match(LHS, m_And(m_Value(), m_ConstantInt(C1))) && |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2115 | ConstantExpr::getAnd(C1, C2)->isNullValue(); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2116 | } |
| 2117 | Instruction *apply(BinaryOperator &Add) const { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2118 | return BinaryOperator::createOr(Add.getOperand(0), Add.getOperand(1)); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2119 | } |
| 2120 | }; |
| 2121 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2122 | static Value *FoldOperationIntoSelectOperand(Instruction &I, Value *SO, |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2123 | InstCombiner *IC) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2124 | if (CastInst *CI = dyn_cast<CastInst>(&I)) { |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2125 | if (Constant *SOC = dyn_cast<Constant>(SO)) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2126 | return ConstantExpr::getCast(CI->getOpcode(), SOC, I.getType()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2127 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2128 | return IC->InsertNewInstBefore(CastInst::create( |
| 2129 | CI->getOpcode(), SO, I.getType(), SO->getName() + ".cast"), I); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2130 | } |
| 2131 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2132 | // Figure out if the constant is the left or the right argument. |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2133 | bool ConstIsRHS = isa<Constant>(I.getOperand(1)); |
| 2134 | Constant *ConstOperand = cast<Constant>(I.getOperand(ConstIsRHS)); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2135 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2136 | if (Constant *SOC = dyn_cast<Constant>(SO)) { |
| 2137 | if (ConstIsRHS) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2138 | return ConstantExpr::get(I.getOpcode(), SOC, ConstOperand); |
| 2139 | return ConstantExpr::get(I.getOpcode(), ConstOperand, SOC); |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2140 | } |
| 2141 | |
| 2142 | Value *Op0 = SO, *Op1 = ConstOperand; |
| 2143 | if (!ConstIsRHS) |
| 2144 | std::swap(Op0, Op1); |
| 2145 | Instruction *New; |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2146 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I)) |
| 2147 | New = BinaryOperator::create(BO->getOpcode(), Op0, Op1,SO->getName()+".op"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2148 | else if (CmpInst *CI = dyn_cast<CmpInst>(&I)) |
| 2149 | New = CmpInst::create(CI->getOpcode(), CI->getPredicate(), Op0, Op1, |
| 2150 | SO->getName()+".cmp"); |
Chris Lattner | 326c0f3 | 2004-04-10 19:15:56 +0000 | [diff] [blame] | 2151 | else { |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2152 | assert(0 && "Unknown binary instruction type!"); |
Chris Lattner | 326c0f3 | 2004-04-10 19:15:56 +0000 | [diff] [blame] | 2153 | abort(); |
| 2154 | } |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2155 | return IC->InsertNewInstBefore(New, I); |
| 2156 | } |
| 2157 | |
| 2158 | // FoldOpIntoSelect - Given an instruction with a select as one operand and a |
| 2159 | // constant as the other operand, try to fold the binary operator into the |
| 2160 | // select arguments. This also works for Cast instructions, which obviously do |
| 2161 | // not have a second operand. |
| 2162 | static Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI, |
| 2163 | InstCombiner *IC) { |
| 2164 | // Don't modify shared select instructions |
| 2165 | if (!SI->hasOneUse()) return 0; |
| 2166 | Value *TV = SI->getOperand(1); |
| 2167 | Value *FV = SI->getOperand(2); |
| 2168 | |
| 2169 | if (isa<Constant>(TV) || isa<Constant>(FV)) { |
Chris Lattner | 956db27 | 2005-04-21 05:43:13 +0000 | [diff] [blame] | 2170 | // Bool selects with constant operands can be folded to logical ops. |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 2171 | if (SI->getType() == Type::Int1Ty) return 0; |
Chris Lattner | 956db27 | 2005-04-21 05:43:13 +0000 | [diff] [blame] | 2172 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2173 | Value *SelectTrueVal = FoldOperationIntoSelectOperand(Op, TV, IC); |
| 2174 | Value *SelectFalseVal = FoldOperationIntoSelectOperand(Op, FV, IC); |
| 2175 | |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 2176 | return SelectInst::Create(SI->getCondition(), SelectTrueVal, |
| 2177 | SelectFalseVal); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2178 | } |
| 2179 | return 0; |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2180 | } |
| 2181 | |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2182 | |
| 2183 | /// FoldOpIntoPhi - Given a binary operator or cast instruction which has a PHI |
| 2184 | /// node as operand #0, see if we can fold the instruction into the PHI (which |
| 2185 | /// is only possible if all operands to the PHI are constants). |
| 2186 | Instruction *InstCombiner::FoldOpIntoPhi(Instruction &I) { |
| 2187 | PHINode *PN = cast<PHINode>(I.getOperand(0)); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 2188 | unsigned NumPHIValues = PN->getNumIncomingValues(); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 2189 | if (!PN->hasOneUse() || NumPHIValues == 0) return 0; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2190 | |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 2191 | // Check to see if all of the operands of the PHI are constants. If there is |
| 2192 | // 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] | 2193 | // or if *it* is a PHI, bail out. |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 2194 | BasicBlock *NonConstBB = 0; |
| 2195 | for (unsigned i = 0; i != NumPHIValues; ++i) |
| 2196 | if (!isa<Constant>(PN->getIncomingValue(i))) { |
| 2197 | if (NonConstBB) return 0; // More than one non-const value. |
Chris Lattner | b303668 | 2007-02-24 01:03:45 +0000 | [diff] [blame] | 2198 | if (isa<PHINode>(PN->getIncomingValue(i))) return 0; // Itself a phi. |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 2199 | NonConstBB = PN->getIncomingBlock(i); |
| 2200 | |
| 2201 | // If the incoming non-constant value is in I's block, we have an infinite |
| 2202 | // loop. |
| 2203 | if (NonConstBB == I.getParent()) |
| 2204 | return 0; |
| 2205 | } |
| 2206 | |
| 2207 | // If there is exactly one non-constant value, we can insert a copy of the |
| 2208 | // operation in that block. However, if this is a critical edge, we would be |
| 2209 | // inserting the computation one some other paths (e.g. inside a loop). Only |
| 2210 | // do this if the pred block is unconditionally branching into the phi block. |
| 2211 | if (NonConstBB) { |
| 2212 | BranchInst *BI = dyn_cast<BranchInst>(NonConstBB->getTerminator()); |
| 2213 | if (!BI || !BI->isUnconditional()) return 0; |
| 2214 | } |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2215 | |
| 2216 | // Okay, we can do the transformation: create the new PHI node. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 2217 | PHINode *NewPN = PHINode::Create(I.getType(), ""); |
Chris Lattner | 5551706 | 2005-01-29 00:39:08 +0000 | [diff] [blame] | 2218 | NewPN->reserveOperandSpace(PN->getNumOperands()/2); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2219 | InsertNewInstBefore(NewPN, *PN); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 2220 | NewPN->takeName(PN); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2221 | |
| 2222 | // Next, add all of the operands to the PHI. |
| 2223 | if (I.getNumOperands() == 2) { |
| 2224 | Constant *C = cast<Constant>(I.getOperand(1)); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 2225 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
Chris Lattner | a9ff5eb | 2007-08-05 08:47:58 +0000 | [diff] [blame] | 2226 | Value *InV = 0; |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 2227 | if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2228 | if (CmpInst *CI = dyn_cast<CmpInst>(&I)) |
| 2229 | InV = ConstantExpr::getCompare(CI->getPredicate(), InC, C); |
| 2230 | else |
| 2231 | InV = ConstantExpr::get(I.getOpcode(), InC, C); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 2232 | } else { |
| 2233 | assert(PN->getIncomingBlock(i) == NonConstBB); |
| 2234 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I)) |
| 2235 | InV = BinaryOperator::create(BO->getOpcode(), |
| 2236 | PN->getIncomingValue(i), C, "phitmp", |
| 2237 | NonConstBB->getTerminator()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2238 | else if (CmpInst *CI = dyn_cast<CmpInst>(&I)) |
| 2239 | InV = CmpInst::create(CI->getOpcode(), |
| 2240 | CI->getPredicate(), |
| 2241 | PN->getIncomingValue(i), C, "phitmp", |
| 2242 | NonConstBB->getTerminator()); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 2243 | else |
| 2244 | assert(0 && "Unknown binop!"); |
| 2245 | |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 2246 | AddToWorkList(cast<Instruction>(InV)); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 2247 | } |
| 2248 | NewPN->addIncoming(InV, PN->getIncomingBlock(i)); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2249 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2250 | } else { |
| 2251 | CastInst *CI = cast<CastInst>(&I); |
| 2252 | const Type *RetTy = CI->getType(); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 2253 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 2254 | Value *InV; |
| 2255 | if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2256 | InV = ConstantExpr::getCast(CI->getOpcode(), InC, RetTy); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 2257 | } else { |
| 2258 | assert(PN->getIncomingBlock(i) == NonConstBB); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2259 | InV = CastInst::create(CI->getOpcode(), PN->getIncomingValue(i), |
| 2260 | I.getType(), "phitmp", |
| 2261 | NonConstBB->getTerminator()); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 2262 | AddToWorkList(cast<Instruction>(InV)); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 2263 | } |
| 2264 | NewPN->addIncoming(InV, PN->getIncomingBlock(i)); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2265 | } |
| 2266 | } |
| 2267 | return ReplaceInstUsesWith(I, NewPN); |
| 2268 | } |
| 2269 | |
Chris Lattner | 2454a2e | 2008-01-29 06:52:45 +0000 | [diff] [blame] | 2270 | |
| 2271 | /// CannotBeNegativeZero - Return true if we can prove that the specified FP |
| 2272 | /// value is never equal to -0.0. |
| 2273 | /// |
| 2274 | /// Note that this function will need to be revisited when we support nondefault |
| 2275 | /// rounding modes! |
| 2276 | /// |
| 2277 | static bool CannotBeNegativeZero(const Value *V) { |
| 2278 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V)) |
| 2279 | return !CFP->getValueAPF().isNegZero(); |
| 2280 | |
| 2281 | // (add x, 0.0) is guaranteed to return +0.0, not -0.0. |
| 2282 | if (const Instruction *I = dyn_cast<Instruction>(V)) { |
| 2283 | if (I->getOpcode() == Instruction::Add && |
| 2284 | isa<ConstantFP>(I->getOperand(1)) && |
| 2285 | cast<ConstantFP>(I->getOperand(1))->isNullValue()) |
| 2286 | return true; |
| 2287 | |
| 2288 | if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) |
| 2289 | if (II->getIntrinsicID() == Intrinsic::sqrt) |
| 2290 | return CannotBeNegativeZero(II->getOperand(1)); |
| 2291 | |
| 2292 | if (const CallInst *CI = dyn_cast<CallInst>(I)) |
| 2293 | if (const Function *F = CI->getCalledFunction()) { |
| 2294 | if (F->isDeclaration()) { |
| 2295 | switch (F->getNameLen()) { |
| 2296 | case 3: // abs(x) != -0.0 |
| 2297 | if (!strcmp(F->getNameStart(), "abs")) return true; |
| 2298 | break; |
| 2299 | case 4: // abs[lf](x) != -0.0 |
| 2300 | if (!strcmp(F->getNameStart(), "absf")) return true; |
| 2301 | if (!strcmp(F->getNameStart(), "absl")) return true; |
| 2302 | break; |
| 2303 | } |
| 2304 | } |
| 2305 | } |
| 2306 | } |
| 2307 | |
| 2308 | return false; |
| 2309 | } |
| 2310 | |
| 2311 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2312 | Instruction *InstCombiner::visitAdd(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 2313 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2314 | Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 2315 | |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 2316 | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2317 | // X + undef -> undef |
| 2318 | if (isa<UndefValue>(RHS)) |
| 2319 | return ReplaceInstUsesWith(I, RHS); |
| 2320 | |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 2321 | // X + 0 --> X |
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 2322 | if (!I.getType()->isFPOrFPVector()) { // NOTE: -0 + +0 = +0. |
Chris Lattner | 5e678e0 | 2005-10-17 17:56:38 +0000 | [diff] [blame] | 2323 | if (RHSC->isNullValue()) |
| 2324 | return ReplaceInstUsesWith(I, LHS); |
Chris Lattner | 8532cf6 | 2005-10-17 20:18:38 +0000 | [diff] [blame] | 2325 | } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) { |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 2326 | if (CFP->isExactlyValue(ConstantFP::getNegativeZero |
| 2327 | (I.getType())->getValueAPF())) |
Chris Lattner | 8532cf6 | 2005-10-17 20:18:38 +0000 | [diff] [blame] | 2328 | return ReplaceInstUsesWith(I, LHS); |
Chris Lattner | 5e678e0 | 2005-10-17 17:56:38 +0000 | [diff] [blame] | 2329 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2330 | |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 2331 | if (ConstantInt *CI = dyn_cast<ConstantInt>(RHSC)) { |
Chris Lattner | b4a2f05 | 2006-11-09 05:12:27 +0000 | [diff] [blame] | 2332 | // X + (signbit) --> X ^ signbit |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2333 | const APInt& Val = CI->getValue(); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 2334 | uint32_t BitWidth = Val.getBitWidth(); |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 2335 | if (Val == APInt::getSignBit(BitWidth)) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2336 | return BinaryOperator::createXor(LHS, RHS); |
Chris Lattner | b4a2f05 | 2006-11-09 05:12:27 +0000 | [diff] [blame] | 2337 | |
| 2338 | // See if SimplifyDemandedBits can simplify this. This handles stuff like |
| 2339 | // (X & 254)+1 -> (X&254)|1 |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 2340 | if (!isa<VectorType>(I.getType())) { |
| 2341 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 2342 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth), |
| 2343 | KnownZero, KnownOne)) |
| 2344 | return &I; |
| 2345 | } |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 2346 | } |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2347 | |
| 2348 | if (isa<PHINode>(LHS)) |
| 2349 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2350 | return NV; |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2351 | |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 2352 | ConstantInt *XorRHS = 0; |
| 2353 | Value *XorLHS = 0; |
Chris Lattner | c5eff44 | 2007-01-30 22:32:46 +0000 | [diff] [blame] | 2354 | if (isa<ConstantInt>(RHSC) && |
| 2355 | match(LHS, m_Xor(m_Value(XorLHS), m_ConstantInt(XorRHS)))) { |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 2356 | uint32_t TySizeBits = I.getType()->getPrimitiveSizeInBits(); |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2357 | const APInt& RHSVal = cast<ConstantInt>(RHSC)->getValue(); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2358 | |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 2359 | uint32_t Size = TySizeBits / 2; |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 2360 | APInt C0080Val(APInt(TySizeBits, 1ULL).shl(Size - 1)); |
| 2361 | APInt CFF80Val(-C0080Val); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2362 | do { |
| 2363 | if (TySizeBits > Size) { |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2364 | // If we have ADD(XOR(AND(X, 0xFF), 0x80), 0xF..F80), it's a sext. |
| 2365 | // 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] | 2366 | if ((RHSVal == CFF80Val && XorRHS->getValue() == C0080Val) || |
| 2367 | (RHSVal == C0080Val && XorRHS->getValue() == CFF80Val)) { |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2368 | // This is a sign extend if the top bits are known zero. |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 2369 | if (!MaskedValueIsZero(XorLHS, |
| 2370 | APInt::getHighBitsSet(TySizeBits, TySizeBits - Size))) |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2371 | 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] | 2372 | break; |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2373 | } |
| 2374 | } |
| 2375 | Size >>= 1; |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 2376 | C0080Val = APIntOps::lshr(C0080Val, Size); |
| 2377 | CFF80Val = APIntOps::ashr(CFF80Val, Size); |
| 2378 | } while (Size >= 1); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2379 | |
Reid Spencer | 35c3885 | 2007-03-28 01:36:16 +0000 | [diff] [blame] | 2380 | // FIXME: This shouldn't be necessary. When the backends can handle types |
| 2381 | // with funny bit widths then this whole cascade of if statements should |
| 2382 | // be removed. It is just here to get the size of the "middle" type back |
| 2383 | // up to something that the back ends can handle. |
| 2384 | const Type *MiddleType = 0; |
| 2385 | switch (Size) { |
| 2386 | default: break; |
| 2387 | case 32: MiddleType = Type::Int32Ty; break; |
| 2388 | case 16: MiddleType = Type::Int16Ty; break; |
| 2389 | case 8: MiddleType = Type::Int8Ty; break; |
| 2390 | } |
| 2391 | if (MiddleType) { |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 2392 | Instruction *NewTrunc = new TruncInst(XorLHS, MiddleType, "sext"); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2393 | InsertNewInstBefore(NewTrunc, I); |
Reid Spencer | 35c3885 | 2007-03-28 01:36:16 +0000 | [diff] [blame] | 2394 | return new SExtInst(NewTrunc, I.getType(), I.getName()); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2395 | } |
| 2396 | } |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 2397 | } |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 2398 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2399 | // X + X --> X << 1 |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 2400 | if (I.getType()->isInteger() && I.getType() != Type::Int1Ty) { |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2401 | if (Instruction *Result = AssociativeOpt(I, AddRHS(RHS))) return Result; |
Chris Lattner | 7edc8c2 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 2402 | |
| 2403 | if (Instruction *RHSI = dyn_cast<Instruction>(RHS)) { |
| 2404 | if (RHSI->getOpcode() == Instruction::Sub) |
| 2405 | if (LHS == RHSI->getOperand(1)) // A + (B - A) --> B |
| 2406 | return ReplaceInstUsesWith(I, RHSI->getOperand(0)); |
| 2407 | } |
| 2408 | if (Instruction *LHSI = dyn_cast<Instruction>(LHS)) { |
| 2409 | if (LHSI->getOpcode() == Instruction::Sub) |
| 2410 | if (RHS == LHSI->getOperand(1)) // (B - A) + A --> B |
| 2411 | return ReplaceInstUsesWith(I, LHSI->getOperand(0)); |
| 2412 | } |
Robert Bocchino | 7169828 | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 2413 | } |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 2414 | |
Chris Lattner | 5c4afb9 | 2002-05-08 22:46:53 +0000 | [diff] [blame] | 2415 | // -A + B --> B - A |
Chris Lattner | dd12f96 | 2008-02-17 21:03:36 +0000 | [diff] [blame] | 2416 | // -A + -B --> -(A + B) |
| 2417 | if (Value *LHSV = dyn_castNegVal(LHS)) { |
Chris Lattner | e10c0b9 | 2008-02-18 17:50:16 +0000 | [diff] [blame] | 2418 | if (LHS->getType()->isIntOrIntVector()) { |
| 2419 | if (Value *RHSV = dyn_castNegVal(RHS)) { |
| 2420 | Instruction *NewAdd = BinaryOperator::createAdd(LHSV, RHSV, "sum"); |
| 2421 | InsertNewInstBefore(NewAdd, I); |
| 2422 | return BinaryOperator::createNeg(NewAdd); |
| 2423 | } |
Chris Lattner | dd12f96 | 2008-02-17 21:03:36 +0000 | [diff] [blame] | 2424 | } |
| 2425 | |
| 2426 | return BinaryOperator::createSub(RHS, LHSV); |
| 2427 | } |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 2428 | |
| 2429 | // A + -B --> A - B |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 2430 | if (!isa<Constant>(RHS)) |
| 2431 | if (Value *V = dyn_castNegVal(RHS)) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2432 | return BinaryOperator::createSub(LHS, V); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2433 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2434 | |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2435 | ConstantInt *C2; |
| 2436 | if (Value *X = dyn_castFoldableMul(LHS, C2)) { |
| 2437 | if (X == RHS) // X*C + X --> X * (C+1) |
| 2438 | return BinaryOperator::createMul(RHS, AddOne(C2)); |
| 2439 | |
| 2440 | // X*C1 + X*C2 --> X * (C1+C2) |
| 2441 | ConstantInt *C1; |
| 2442 | if (X == dyn_castFoldableMul(RHS, C1)) |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2443 | return BinaryOperator::createMul(X, Add(C1, C2)); |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2444 | } |
| 2445 | |
| 2446 | // X + X*C --> X * (C+1) |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2447 | if (dyn_castFoldableMul(RHS, C2) == LHS) |
| 2448 | return BinaryOperator::createMul(LHS, AddOne(C2)); |
| 2449 | |
Chris Lattner | e617c9e | 2007-01-05 02:17:46 +0000 | [diff] [blame] | 2450 | // X + ~X --> -1 since ~X = -X-1 |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 2451 | if (dyn_castNotVal(LHS) == RHS || dyn_castNotVal(RHS) == LHS) |
| 2452 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | e617c9e | 2007-01-05 02:17:46 +0000 | [diff] [blame] | 2453 | |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2454 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2455 | // (A & C1)+(B & C2) --> (A & C1)|(B & C2) iff C1&C2 == 0 |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2456 | if (match(RHS, m_And(m_Value(), m_ConstantInt(C2)))) |
Chris Lattner | e617c9e | 2007-01-05 02:17:46 +0000 | [diff] [blame] | 2457 | if (Instruction *R = AssociativeOpt(I, AddMaskingAnd(C2))) |
| 2458 | return R; |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 2459 | |
Nick Lewycky | b6eabff | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 2460 | // W*X + Y*Z --> W * (X+Z) iff W == Y |
Nick Lewycky | 0c2c3f6 | 2008-02-03 08:19:11 +0000 | [diff] [blame] | 2461 | if (I.getType()->isIntOrIntVector()) { |
Nick Lewycky | b6eabff | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 2462 | Value *W, *X, *Y, *Z; |
| 2463 | if (match(LHS, m_Mul(m_Value(W), m_Value(X))) && |
| 2464 | match(RHS, m_Mul(m_Value(Y), m_Value(Z)))) { |
| 2465 | if (W != Y) { |
| 2466 | if (W == Z) { |
Bill Wendling | 587c01d | 2008-02-26 10:53:30 +0000 | [diff] [blame] | 2467 | std::swap(Y, Z); |
Nick Lewycky | b6eabff | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 2468 | } else if (Y == X) { |
Bill Wendling | 587c01d | 2008-02-26 10:53:30 +0000 | [diff] [blame] | 2469 | std::swap(W, X); |
| 2470 | } else if (X == Z) { |
Nick Lewycky | b6eabff | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 2471 | std::swap(Y, Z); |
| 2472 | std::swap(W, X); |
| 2473 | } |
| 2474 | } |
| 2475 | |
| 2476 | if (W == Y) { |
| 2477 | Value *NewAdd = InsertNewInstBefore(BinaryOperator::createAdd(X, Z, |
| 2478 | LHS->getName()), I); |
| 2479 | return BinaryOperator::createMul(W, NewAdd); |
| 2480 | } |
| 2481 | } |
| 2482 | } |
| 2483 | |
Chris Lattner | 6b03205 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 2484 | if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) { |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 2485 | Value *X = 0; |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2486 | if (match(LHS, m_Not(m_Value(X)))) // ~X + C --> (C-1) - X |
| 2487 | return BinaryOperator::createSub(SubOne(CRHS), X); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2488 | |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2489 | // (X & FF00) + xx00 -> (X+xx00) & FF00 |
| 2490 | 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] | 2491 | Constant *Anded = And(CRHS, C2); |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2492 | if (Anded == CRHS) { |
| 2493 | // See if all bits from the first bit set in the Add RHS up are included |
| 2494 | // in the mask. First, get the rightmost bit. |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2495 | const APInt& AddRHSV = CRHS->getValue(); |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2496 | |
| 2497 | // 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] | 2498 | APInt AddRHSHighBits(~((AddRHSV & -AddRHSV)-1)); |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2499 | |
| 2500 | // See if the and mask includes all of these bits. |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2501 | APInt AddRHSHighBitsAnd(AddRHSHighBits & C2->getValue()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2502 | |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2503 | if (AddRHSHighBits == AddRHSHighBitsAnd) { |
| 2504 | // Okay, the xform is safe. Insert the new add pronto. |
| 2505 | Value *NewAdd = InsertNewInstBefore(BinaryOperator::createAdd(X, CRHS, |
| 2506 | LHS->getName()), I); |
| 2507 | return BinaryOperator::createAnd(NewAdd, C2); |
| 2508 | } |
| 2509 | } |
| 2510 | } |
| 2511 | |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2512 | // Try to fold constant add into select arguments. |
| 2513 | if (SelectInst *SI = dyn_cast<SelectInst>(LHS)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2514 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2515 | return R; |
Chris Lattner | 6b03205 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 2516 | } |
| 2517 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2518 | // add (cast *A to intptrtype) B -> |
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2519 | // cast (GEP (cast *A to sbyte*) B) --> intptrtype |
Andrew Lenharth | 16d7955 | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 2520 | { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2521 | CastInst *CI = dyn_cast<CastInst>(LHS); |
| 2522 | Value *Other = RHS; |
Andrew Lenharth | 16d7955 | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 2523 | if (!CI) { |
| 2524 | CI = dyn_cast<CastInst>(RHS); |
| 2525 | Other = LHS; |
| 2526 | } |
Andrew Lenharth | 4563326 | 2006-09-20 15:37:57 +0000 | [diff] [blame] | 2527 | if (CI && CI->getType()->isSized() && |
Reid Spencer | abaa8ca | 2007-01-08 16:32:00 +0000 | [diff] [blame] | 2528 | (CI->getType()->getPrimitiveSizeInBits() == |
| 2529 | TD->getIntPtrType()->getPrimitiveSizeInBits()) |
Andrew Lenharth | 4563326 | 2006-09-20 15:37:57 +0000 | [diff] [blame] | 2530 | && isa<PointerType>(CI->getOperand(0)->getType())) { |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 2531 | unsigned AS = |
| 2532 | cast<PointerType>(CI->getOperand(0)->getType())->getAddressSpace(); |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 2533 | Value *I2 = InsertBitCastBefore(CI->getOperand(0), |
| 2534 | PointerType::get(Type::Int8Ty, AS), I); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 2535 | I2 = InsertNewInstBefore(GetElementPtrInst::Create(I2, Other, "ctg2"), I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2536 | return new PtrToIntInst(I2, CI->getType()); |
Andrew Lenharth | 16d7955 | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 2537 | } |
| 2538 | } |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2539 | |
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2540 | // add (select X 0 (sub n A)) A --> select X A n |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2541 | { |
| 2542 | SelectInst *SI = dyn_cast<SelectInst>(LHS); |
| 2543 | Value *Other = RHS; |
| 2544 | if (!SI) { |
| 2545 | SI = dyn_cast<SelectInst>(RHS); |
| 2546 | Other = LHS; |
| 2547 | } |
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2548 | if (SI && SI->hasOneUse()) { |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2549 | Value *TV = SI->getTrueValue(); |
| 2550 | Value *FV = SI->getFalseValue(); |
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2551 | Value *A, *N; |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2552 | |
| 2553 | // Can we fold the add into the argument of the select? |
| 2554 | // We check both true and false select arguments for a matching subtract. |
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2555 | if (match(FV, m_Zero()) && match(TV, m_Sub(m_Value(N), m_Value(A))) && |
| 2556 | A == Other) // Fold the add into the true select value. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 2557 | return SelectInst::Create(SI->getCondition(), N, A); |
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2558 | if (match(TV, m_Zero()) && match(FV, m_Sub(m_Value(N), m_Value(A))) && |
| 2559 | A == Other) // Fold the add into the false select value. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 2560 | return SelectInst::Create(SI->getCondition(), A, N); |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2561 | } |
| 2562 | } |
Chris Lattner | 2454a2e | 2008-01-29 06:52:45 +0000 | [diff] [blame] | 2563 | |
| 2564 | // Check for X+0.0. Simplify it to X if we know X is not -0.0. |
| 2565 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHS)) |
| 2566 | if (CFP->getValueAPF().isPosZero() && CannotBeNegativeZero(LHS)) |
| 2567 | return ReplaceInstUsesWith(I, LHS); |
Andrew Lenharth | 16d7955 | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 2568 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2569 | return Changed ? &I : 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2570 | } |
| 2571 | |
Chris Lattner | 1ba5bcd | 2003-07-22 21:46:59 +0000 | [diff] [blame] | 2572 | // isSignBit - Return true if the value represented by the constant only has the |
| 2573 | // highest order bit set. |
| 2574 | static bool isSignBit(ConstantInt *CI) { |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 2575 | uint32_t NumBits = CI->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 5a1e3e1 | 2007-03-19 20:58:18 +0000 | [diff] [blame] | 2576 | return CI->getValue() == APInt::getSignBit(NumBits); |
Chris Lattner | 1ba5bcd | 2003-07-22 21:46:59 +0000 | [diff] [blame] | 2577 | } |
| 2578 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2579 | Instruction *InstCombiner::visitSub(BinaryOperator &I) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2580 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2581 | |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2582 | if (Op0 == Op1) // sub X, X -> 0 |
| 2583 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2584 | |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2585 | // If this is a 'B = x-(-A)', change to B = x+A... |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 2586 | if (Value *V = dyn_castNegVal(Op1)) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2587 | return BinaryOperator::createAdd(Op0, V); |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 2588 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2589 | if (isa<UndefValue>(Op0)) |
| 2590 | return ReplaceInstUsesWith(I, Op0); // undef - X -> undef |
| 2591 | if (isa<UndefValue>(Op1)) |
| 2592 | return ReplaceInstUsesWith(I, Op1); // X - undef -> undef |
| 2593 | |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 2594 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op0)) { |
| 2595 | // Replace (-1 - A) with (~A)... |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2596 | if (C->isAllOnesValue()) |
| 2597 | return BinaryOperator::createNot(Op1); |
Chris Lattner | 4037171 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 2598 | |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 2599 | // C - ~X == X + (1+C) |
Reid Spencer | 4b828e6 | 2005-06-18 17:37:34 +0000 | [diff] [blame] | 2600 | Value *X = 0; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2601 | if (match(Op1, m_Not(m_Value(X)))) |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2602 | return BinaryOperator::createAdd(X, AddOne(C)); |
| 2603 | |
Chris Lattner | 76b7a06 | 2007-01-15 07:02:54 +0000 | [diff] [blame] | 2604 | // -(X >>u 31) -> (X >>s 31) |
| 2605 | // -(X >>s 31) -> (X >>u 31) |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 2606 | if (C->isZero()) { |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 2607 | if (BinaryOperator *SI = dyn_cast<BinaryOperator>(Op1)) { |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2608 | if (SI->getOpcode() == Instruction::LShr) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2609 | if (ConstantInt *CU = dyn_cast<ConstantInt>(SI->getOperand(1))) { |
Chris Lattner | 9c29067 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 2610 | // 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] | 2611 | if (CU->getLimitedValue(SI->getType()->getPrimitiveSizeInBits()) == |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2612 | SI->getType()->getPrimitiveSizeInBits()-1) { |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2613 | // Ok, the transformation is safe. Insert AShr. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2614 | return BinaryOperator::create(Instruction::AShr, |
| 2615 | SI->getOperand(0), CU, SI->getName()); |
Chris Lattner | 9c29067 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 2616 | } |
| 2617 | } |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2618 | } |
| 2619 | else if (SI->getOpcode() == Instruction::AShr) { |
| 2620 | if (ConstantInt *CU = dyn_cast<ConstantInt>(SI->getOperand(1))) { |
| 2621 | // 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] | 2622 | if (CU->getLimitedValue(SI->getType()->getPrimitiveSizeInBits()) == |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2623 | SI->getType()->getPrimitiveSizeInBits()-1) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 2624 | // Ok, the transformation is safe. Insert LShr. |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 2625 | return BinaryOperator::createLShr( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2626 | SI->getOperand(0), CU, SI->getName()); |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2627 | } |
| 2628 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 2629 | } |
| 2630 | } |
Chris Lattner | bfe492b | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 2631 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2632 | |
| 2633 | // Try to fold constant sub into select arguments. |
| 2634 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2635 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2636 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2637 | |
| 2638 | if (isa<PHINode>(Op0)) |
| 2639 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2640 | return NV; |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 2641 | } |
| 2642 | |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2643 | if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) { |
| 2644 | if (Op1I->getOpcode() == Instruction::Add && |
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 2645 | !Op0->getType()->isFPOrFPVector()) { |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2646 | if (Op1I->getOperand(0) == Op0) // X-(X+Y) == -Y |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2647 | return BinaryOperator::createNeg(Op1I->getOperand(1), I.getName()); |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2648 | else if (Op1I->getOperand(1) == Op0) // X-(Y+X) == -Y |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2649 | return BinaryOperator::createNeg(Op1I->getOperand(0), I.getName()); |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2650 | else if (ConstantInt *CI1 = dyn_cast<ConstantInt>(I.getOperand(0))) { |
| 2651 | if (ConstantInt *CI2 = dyn_cast<ConstantInt>(Op1I->getOperand(1))) |
| 2652 | // C1-(X+C2) --> (C1-C2)-X |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2653 | return BinaryOperator::createSub(Subtract(CI1, CI2), |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2654 | Op1I->getOperand(0)); |
| 2655 | } |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2656 | } |
| 2657 | |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 2658 | if (Op1I->hasOneUse()) { |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2659 | // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression |
| 2660 | // is not used by anyone else... |
| 2661 | // |
Chris Lattner | 0517e72 | 2004-02-02 20:09:56 +0000 | [diff] [blame] | 2662 | if (Op1I->getOpcode() == Instruction::Sub && |
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 2663 | !Op1I->getType()->isFPOrFPVector()) { |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2664 | // Swap the two operands of the subexpr... |
| 2665 | Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1); |
| 2666 | Op1I->setOperand(0, IIOp1); |
| 2667 | Op1I->setOperand(1, IIOp0); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2668 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2669 | // Create the new top level add instruction... |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2670 | return BinaryOperator::createAdd(Op0, Op1); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2671 | } |
| 2672 | |
| 2673 | // Replace (A - (A & B)) with (A & ~B) if this is the only use of (A&B)... |
| 2674 | // |
| 2675 | if (Op1I->getOpcode() == Instruction::And && |
| 2676 | (Op1I->getOperand(0) == Op0 || Op1I->getOperand(1) == Op0)) { |
| 2677 | Value *OtherOp = Op1I->getOperand(Op1I->getOperand(0) == Op0); |
| 2678 | |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 2679 | Value *NewNot = |
| 2680 | InsertNewInstBefore(BinaryOperator::createNot(OtherOp, "B.not"), I); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2681 | return BinaryOperator::createAnd(Op0, NewNot); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2682 | } |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2683 | |
Reid Spencer | ac5209e | 2006-10-16 23:08:08 +0000 | [diff] [blame] | 2684 | // 0 - (X sdiv C) -> (X sdiv -C) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2685 | if (Op1I->getOpcode() == Instruction::SDiv) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2686 | if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0)) |
Zhou Sheng | 843f0767 | 2007-04-19 05:39:12 +0000 | [diff] [blame] | 2687 | if (CSI->isZero()) |
Chris Lattner | 91ccc15 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 2688 | if (Constant *DivRHS = dyn_cast<Constant>(Op1I->getOperand(1))) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2689 | return BinaryOperator::createSDiv(Op1I->getOperand(0), |
Chris Lattner | 91ccc15 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 2690 | ConstantExpr::getNeg(DivRHS)); |
| 2691 | |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2692 | // X - X*C --> X * (1-C) |
Reid Spencer | 4b828e6 | 2005-06-18 17:37:34 +0000 | [diff] [blame] | 2693 | ConstantInt *C2 = 0; |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2694 | if (dyn_castFoldableMul(Op1I, C2) == Op0) { |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2695 | Constant *CP1 = Subtract(ConstantInt::get(I.getType(), 1), C2); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2696 | return BinaryOperator::createMul(Op0, CP1); |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2697 | } |
Dan Gohman | 5d066ff | 2007-09-17 17:31:57 +0000 | [diff] [blame] | 2698 | |
| 2699 | // X - ((X / Y) * Y) --> X % Y |
| 2700 | if (Op1I->getOpcode() == Instruction::Mul) |
| 2701 | if (Instruction *I = dyn_cast<Instruction>(Op1I->getOperand(0))) |
| 2702 | if (Op0 == I->getOperand(0) && |
| 2703 | Op1I->getOperand(1) == I->getOperand(1)) { |
| 2704 | if (I->getOpcode() == Instruction::SDiv) |
| 2705 | return BinaryOperator::createSRem(Op0, Op1I->getOperand(1)); |
| 2706 | if (I->getOpcode() == Instruction::UDiv) |
| 2707 | return BinaryOperator::createURem(Op0, Op1I->getOperand(1)); |
| 2708 | } |
Chris Lattner | 4037171 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 2709 | } |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2710 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2711 | |
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 2712 | if (!Op0->getType()->isFPOrFPVector()) |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 2713 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) { |
Chris Lattner | 7edc8c2 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 2714 | if (Op0I->getOpcode() == Instruction::Add) { |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 2715 | if (Op0I->getOperand(0) == Op1) // (Y+X)-Y == X |
| 2716 | return ReplaceInstUsesWith(I, Op0I->getOperand(1)); |
| 2717 | else if (Op0I->getOperand(1) == Op1) // (X+Y)-Y == X |
| 2718 | return ReplaceInstUsesWith(I, Op0I->getOperand(0)); |
Chris Lattner | 7edc8c2 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 2719 | } else if (Op0I->getOpcode() == Instruction::Sub) { |
| 2720 | if (Op0I->getOperand(0) == Op1) // (X-Y)-X == -Y |
| 2721 | return BinaryOperator::createNeg(Op0I->getOperand(1), I.getName()); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 2722 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 2723 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2724 | |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2725 | ConstantInt *C1; |
| 2726 | if (Value *X = dyn_castFoldableMul(Op0, C1)) { |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2727 | if (X == Op1) // X*C - X --> X * (C-1) |
| 2728 | return BinaryOperator::createMul(Op1, SubOne(C1)); |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2729 | |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2730 | ConstantInt *C2; // X*C1 - X*C2 -> X * (C1-C2) |
| 2731 | if (X == dyn_castFoldableMul(Op1, C2)) |
Zhou Sheng | 58d13af | 2008-02-22 10:00:35 +0000 | [diff] [blame] | 2732 | return BinaryOperator::createMul(X, Subtract(C1, C2)); |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2733 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2734 | return 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2735 | } |
| 2736 | |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2737 | /// isSignBitCheck - Given an exploded icmp instruction, return true if the |
| 2738 | /// comparison only checks the sign bit. If it only checks the sign bit, set |
| 2739 | /// TrueIfSigned if the result of the comparison is true when the input value is |
| 2740 | /// signed. |
| 2741 | static bool isSignBitCheck(ICmpInst::Predicate pred, ConstantInt *RHS, |
| 2742 | bool &TrueIfSigned) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2743 | switch (pred) { |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2744 | case ICmpInst::ICMP_SLT: // True if LHS s< 0 |
| 2745 | TrueIfSigned = true; |
| 2746 | return RHS->isZero(); |
Chris Lattner | cb7122b | 2007-07-16 04:15:34 +0000 | [diff] [blame] | 2747 | case ICmpInst::ICMP_SLE: // True if LHS s<= RHS and RHS == -1 |
| 2748 | TrueIfSigned = true; |
| 2749 | return RHS->isAllOnesValue(); |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2750 | case ICmpInst::ICMP_SGT: // True if LHS s> -1 |
| 2751 | TrueIfSigned = false; |
| 2752 | return RHS->isAllOnesValue(); |
Chris Lattner | cb7122b | 2007-07-16 04:15:34 +0000 | [diff] [blame] | 2753 | case ICmpInst::ICMP_UGT: |
| 2754 | // True if LHS u> RHS and RHS == high-bit-mask - 1 |
| 2755 | TrueIfSigned = true; |
| 2756 | return RHS->getValue() == |
| 2757 | APInt::getSignedMaxValue(RHS->getType()->getPrimitiveSizeInBits()); |
| 2758 | case ICmpInst::ICMP_UGE: |
| 2759 | // True if LHS u>= RHS and RHS == high-bit-mask (2^7, 2^15, 2^31, etc) |
| 2760 | TrueIfSigned = true; |
| 2761 | return RHS->getValue() == |
| 2762 | APInt::getSignBit(RHS->getType()->getPrimitiveSizeInBits()); |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2763 | default: |
| 2764 | return false; |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2765 | } |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2766 | } |
| 2767 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2768 | Instruction *InstCombiner::visitMul(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 2769 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2770 | Value *Op0 = I.getOperand(0); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2771 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2772 | if (isa<UndefValue>(I.getOperand(1))) // undef * X -> 0 |
| 2773 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2774 | |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2775 | // Simplify mul instructions with a constant RHS... |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2776 | if (Constant *Op1 = dyn_cast<Constant>(I.getOperand(1))) { |
| 2777 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 2778 | |
| 2779 | // ((X << C1)*C2) == (X * (C2 << C1)) |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2780 | if (BinaryOperator *SI = dyn_cast<BinaryOperator>(Op0)) |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 2781 | if (SI->getOpcode() == Instruction::Shl) |
| 2782 | if (Constant *ShOp = dyn_cast<Constant>(SI->getOperand(1))) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2783 | return BinaryOperator::createMul(SI->getOperand(0), |
| 2784 | ConstantExpr::getShl(CI, ShOp)); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2785 | |
Zhou Sheng | 843f0767 | 2007-04-19 05:39:12 +0000 | [diff] [blame] | 2786 | if (CI->isZero()) |
Chris Lattner | 515c97c | 2003-09-11 22:24:54 +0000 | [diff] [blame] | 2787 | return ReplaceInstUsesWith(I, Op1); // X * 0 == 0 |
| 2788 | if (CI->equalsInt(1)) // X * 1 == X |
| 2789 | return ReplaceInstUsesWith(I, Op0); |
| 2790 | if (CI->isAllOnesValue()) // X * -1 == 0 - X |
Chris Lattner | 0af1fab | 2003-06-25 17:09:20 +0000 | [diff] [blame] | 2791 | return BinaryOperator::createNeg(Op0, I.getName()); |
Chris Lattner | 6c1ce21 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 2792 | |
Zhou Sheng | 97b52c2 | 2007-03-29 01:57:21 +0000 | [diff] [blame] | 2793 | const APInt& Val = cast<ConstantInt>(CI)->getValue(); |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2794 | if (Val.isPowerOf2()) { // Replace X*(2^C) with X << C |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 2795 | return BinaryOperator::createShl(Op0, |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2796 | ConstantInt::get(Op0->getType(), Val.logBase2())); |
Chris Lattner | bcd7db5 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 2797 | } |
Robert Bocchino | 7169828 | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 2798 | } else if (ConstantFP *Op1F = dyn_cast<ConstantFP>(Op1)) { |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2799 | if (Op1F->isNullValue()) |
| 2800 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 6c1ce21 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 2801 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2802 | // "In IEEE floating point, x*1 is not equivalent to x for nans. However, |
| 2803 | // ANSI says we can drop signals, so we can do this anyway." (from GCC) |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 2804 | // We need a better interface for long double here. |
| 2805 | if (Op1->getType() == Type::FloatTy || Op1->getType() == Type::DoubleTy) |
| 2806 | if (Op1F->isExactlyValue(1.0)) |
| 2807 | return ReplaceInstUsesWith(I, Op0); // Eliminate 'mul double %X, 1.0' |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2808 | } |
Chris Lattner | ab51f3f | 2006-03-04 06:04:02 +0000 | [diff] [blame] | 2809 | |
| 2810 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) |
| 2811 | if (Op0I->getOpcode() == Instruction::Add && Op0I->hasOneUse() && |
| 2812 | isa<ConstantInt>(Op0I->getOperand(1))) { |
| 2813 | // Canonicalize (X+C1)*C2 -> X*C2+C1*C2. |
| 2814 | Instruction *Add = BinaryOperator::createMul(Op0I->getOperand(0), |
| 2815 | Op1, "tmp"); |
| 2816 | InsertNewInstBefore(Add, I); |
| 2817 | Value *C1C2 = ConstantExpr::getMul(Op1, |
| 2818 | cast<Constant>(Op0I->getOperand(1))); |
| 2819 | return BinaryOperator::createAdd(Add, C1C2); |
| 2820 | |
| 2821 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2822 | |
| 2823 | // Try to fold constant mul into select arguments. |
| 2824 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2825 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2826 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2827 | |
| 2828 | if (isa<PHINode>(Op0)) |
| 2829 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2830 | return NV; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2831 | } |
| 2832 | |
Chris Lattner | a4f445b | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 2833 | if (Value *Op0v = dyn_castNegVal(Op0)) // -X * -Y = X*Y |
| 2834 | if (Value *Op1v = dyn_castNegVal(I.getOperand(1))) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2835 | return BinaryOperator::createMul(Op0v, Op1v); |
Chris Lattner | a4f445b | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 2836 | |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2837 | // If one of the operands of the multiply is a cast from a boolean value, then |
| 2838 | // we know the bool is either zero or one, so this is a 'masking' multiply. |
| 2839 | // See if we can simplify things based on how the boolean was originally |
| 2840 | // formed. |
| 2841 | CastInst *BoolCast = 0; |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 2842 | if (ZExtInst *CI = dyn_cast<ZExtInst>(I.getOperand(0))) |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 2843 | if (CI->getOperand(0)->getType() == Type::Int1Ty) |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2844 | BoolCast = CI; |
| 2845 | if (!BoolCast) |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 2846 | if (ZExtInst *CI = dyn_cast<ZExtInst>(I.getOperand(1))) |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 2847 | if (CI->getOperand(0)->getType() == Type::Int1Ty) |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2848 | BoolCast = CI; |
| 2849 | if (BoolCast) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2850 | if (ICmpInst *SCI = dyn_cast<ICmpInst>(BoolCast->getOperand(0))) { |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2851 | Value *SCIOp0 = SCI->getOperand(0), *SCIOp1 = SCI->getOperand(1); |
| 2852 | const Type *SCOpTy = SCIOp0->getType(); |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2853 | bool TIS = false; |
| 2854 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2855 | // 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] | 2856 | // multiply into a shift/and combination. |
| 2857 | if (isa<ConstantInt>(SCIOp1) && |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2858 | isSignBitCheck(SCI->getPredicate(), cast<ConstantInt>(SCIOp1), TIS) && |
| 2859 | TIS) { |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2860 | // Shift the X value right to turn it into "all signbits". |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2861 | Constant *Amt = ConstantInt::get(SCIOp0->getType(), |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 2862 | SCOpTy->getPrimitiveSizeInBits()-1); |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2863 | Value *V = |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2864 | InsertNewInstBefore( |
| 2865 | BinaryOperator::create(Instruction::AShr, SCIOp0, Amt, |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2866 | BoolCast->getOperand(0)->getName()+ |
| 2867 | ".mask"), I); |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2868 | |
| 2869 | // If the multiply type is not the same as the source type, sign extend |
| 2870 | // or truncate to the multiply type. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 2871 | if (I.getType() != V->getType()) { |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 2872 | uint32_t SrcBits = V->getType()->getPrimitiveSizeInBits(); |
| 2873 | uint32_t DstBits = I.getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 2874 | Instruction::CastOps opcode = |
| 2875 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2876 | (SrcBits < DstBits ? Instruction::SExt : Instruction::Trunc)); |
| 2877 | V = InsertCastBefore(opcode, V, I.getType(), I); |
| 2878 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2879 | |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2880 | Value *OtherOp = Op0 == BoolCast ? I.getOperand(1) : Op0; |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2881 | return BinaryOperator::createAnd(V, OtherOp); |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2882 | } |
| 2883 | } |
| 2884 | } |
| 2885 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2886 | return Changed ? &I : 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2887 | } |
| 2888 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2889 | /// This function implements the transforms on div instructions that work |
| 2890 | /// regardless of the kind of div instruction it is (udiv, sdiv, or fdiv). It is |
| 2891 | /// used by the visitors to those instructions. |
| 2892 | /// @brief Transforms common to all three div instructions |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2893 | Instruction *InstCombiner::commonDivTransforms(BinaryOperator &I) { |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2894 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2895 | |
Chris Lattner | 50b2ca4 | 2008-02-19 06:12:18 +0000 | [diff] [blame] | 2896 | // undef / X -> 0 for integer. |
| 2897 | // undef / X -> undef for FP (the undef could be a snan). |
| 2898 | if (isa<UndefValue>(Op0)) { |
| 2899 | if (Op0->getType()->isFPOrFPVector()) |
| 2900 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2901 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | 50b2ca4 | 2008-02-19 06:12:18 +0000 | [diff] [blame] | 2902 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2903 | |
| 2904 | // X / undef -> undef |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2905 | if (isa<UndefValue>(Op1)) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2906 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2907 | |
Chris Lattner | 25feae5 | 2008-01-28 00:58:18 +0000 | [diff] [blame] | 2908 | // Handle cases involving: [su]div X, (select Cond, Y, Z) |
| 2909 | // This does not apply for fdiv. |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2910 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { |
Chris Lattner | 25feae5 | 2008-01-28 00:58:18 +0000 | [diff] [blame] | 2911 | // [su]div X, (Cond ? 0 : Y) -> div X, Y. If the div and the select are in |
| 2912 | // the same basic block, then we replace the select with Y, and the |
| 2913 | // condition of the select with false (if the cond value is in the same BB). |
| 2914 | // If the select has uses other than the div, this allows them to be |
| 2915 | // simplified also. Note that div X, Y is just as good as div X, 0 (undef) |
| 2916 | if (ConstantInt *ST = dyn_cast<ConstantInt>(SI->getOperand(1))) |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2917 | if (ST->isNullValue()) { |
| 2918 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); |
| 2919 | if (CondI && CondI->getParent() == I.getParent()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2920 | UpdateValueUsesWith(CondI, ConstantInt::getFalse()); |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2921 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
| 2922 | I.setOperand(1, SI->getOperand(2)); |
| 2923 | else |
| 2924 | UpdateValueUsesWith(SI, SI->getOperand(2)); |
| 2925 | return &I; |
| 2926 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2927 | |
Chris Lattner | 25feae5 | 2008-01-28 00:58:18 +0000 | [diff] [blame] | 2928 | // Likewise for: [su]div X, (Cond ? Y : 0) -> div X, Y |
| 2929 | if (ConstantInt *ST = dyn_cast<ConstantInt>(SI->getOperand(2))) |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2930 | if (ST->isNullValue()) { |
| 2931 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); |
| 2932 | if (CondI && CondI->getParent() == I.getParent()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2933 | UpdateValueUsesWith(CondI, ConstantInt::getTrue()); |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2934 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
| 2935 | I.setOperand(1, SI->getOperand(1)); |
| 2936 | else |
| 2937 | UpdateValueUsesWith(SI, SI->getOperand(1)); |
| 2938 | return &I; |
| 2939 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2940 | } |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2941 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2942 | return 0; |
| 2943 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2944 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2945 | /// This function implements the transforms common to both integer division |
| 2946 | /// instructions (udiv and sdiv). It is called by the visitors to those integer |
| 2947 | /// division instructions. |
| 2948 | /// @brief Common integer divide transforms |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2949 | Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2950 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2951 | |
| 2952 | if (Instruction *Common = commonDivTransforms(I)) |
| 2953 | return Common; |
| 2954 | |
| 2955 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
| 2956 | // div X, 1 == X |
| 2957 | if (RHS->equalsInt(1)) |
| 2958 | return ReplaceInstUsesWith(I, Op0); |
| 2959 | |
| 2960 | // (X / C1) / C2 -> X / (C1*C2) |
| 2961 | if (Instruction *LHS = dyn_cast<Instruction>(Op0)) |
| 2962 | if (Instruction::BinaryOps(LHS->getOpcode()) == I.getOpcode()) |
| 2963 | if (ConstantInt *LHSRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) { |
Nick Lewycky | e0cfecf | 2008-02-18 22:48:05 +0000 | [diff] [blame] | 2964 | if (MultiplyOverflows(RHS, LHSRHS, I.getOpcode()==Instruction::SDiv)) |
| 2965 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2966 | else |
| 2967 | return BinaryOperator::create(I.getOpcode(), LHS->getOperand(0), |
| 2968 | Multiply(RHS, LHSRHS)); |
Chris Lattner | bf70b83 | 2005-04-08 04:03:26 +0000 | [diff] [blame] | 2969 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2970 | |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2971 | if (!RHS->isZero()) { // avoid X udiv 0 |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2972 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 2973 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 2974 | return R; |
| 2975 | if (isa<PHINode>(Op0)) |
| 2976 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2977 | return NV; |
| 2978 | } |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2979 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2980 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2981 | // 0 / X == 0, we don't need to preserve faults! |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2982 | if (ConstantInt *LHS = dyn_cast<ConstantInt>(Op0)) |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2983 | if (LHS->equalsInt(0)) |
| 2984 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2985 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2986 | return 0; |
| 2987 | } |
| 2988 | |
| 2989 | Instruction *InstCombiner::visitUDiv(BinaryOperator &I) { |
| 2990 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2991 | |
| 2992 | // Handle the integer div common cases |
| 2993 | if (Instruction *Common = commonIDivTransforms(I)) |
| 2994 | return Common; |
| 2995 | |
| 2996 | // X udiv C^2 -> X >> C |
| 2997 | // Check to see if this is an unsigned division with an exact power of 2, |
| 2998 | // if so, convert to a right shift. |
| 2999 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op1)) { |
Reid Spencer | 6eb0d99 | 2007-03-26 23:58:26 +0000 | [diff] [blame] | 3000 | if (C->getValue().isPowerOf2()) // 0 not included in isPowerOf2 |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 3001 | return BinaryOperator::createLShr(Op0, |
Zhou Sheng | 0fc5095 | 2007-03-25 05:01:29 +0000 | [diff] [blame] | 3002 | ConstantInt::get(Op0->getType(), C->getValue().logBase2())); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3003 | } |
| 3004 | |
| 3005 | // X udiv (C1 << N), where C1 is "1<<C2" --> X >> (N+C2) |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3006 | if (BinaryOperator *RHSI = dyn_cast<BinaryOperator>(I.getOperand(1))) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3007 | if (RHSI->getOpcode() == Instruction::Shl && |
| 3008 | isa<ConstantInt>(RHSI->getOperand(0))) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3009 | const APInt& C1 = cast<ConstantInt>(RHSI->getOperand(0))->getValue(); |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 3010 | if (C1.isPowerOf2()) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3011 | Value *N = RHSI->getOperand(1); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3012 | const Type *NTy = N->getType(); |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 3013 | if (uint32_t C2 = C1.logBase2()) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3014 | Constant *C2V = ConstantInt::get(NTy, C2); |
| 3015 | N = InsertNewInstBefore(BinaryOperator::createAdd(N, C2V, "tmp"), I); |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 3016 | } |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 3017 | return BinaryOperator::createLShr(Op0, N); |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 3018 | } |
| 3019 | } |
Chris Lattner | c812e5d | 2005-11-05 07:40:31 +0000 | [diff] [blame] | 3020 | } |
| 3021 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3022 | // udiv X, (Select Cond, C1, C2) --> Select Cond, (shr X, C1), (shr X, C2) |
| 3023 | // where C1&C2 are powers of two. |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 3024 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3025 | if (ConstantInt *STO = dyn_cast<ConstantInt>(SI->getOperand(1))) |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 3026 | if (ConstantInt *SFO = dyn_cast<ConstantInt>(SI->getOperand(2))) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3027 | const APInt &TVA = STO->getValue(), &FVA = SFO->getValue(); |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 3028 | if (TVA.isPowerOf2() && FVA.isPowerOf2()) { |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 3029 | // Compute the shift amounts |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 3030 | uint32_t TSA = TVA.logBase2(), FSA = FVA.logBase2(); |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 3031 | // Construct the "on true" case of the select |
| 3032 | Constant *TC = ConstantInt::get(Op0->getType(), TSA); |
| 3033 | Instruction *TSI = BinaryOperator::createLShr( |
| 3034 | Op0, TC, SI->getName()+".t"); |
| 3035 | TSI = InsertNewInstBefore(TSI, I); |
| 3036 | |
| 3037 | // Construct the "on false" case of the select |
| 3038 | Constant *FC = ConstantInt::get(Op0->getType(), FSA); |
| 3039 | Instruction *FSI = BinaryOperator::createLShr( |
| 3040 | Op0, FC, SI->getName()+".f"); |
| 3041 | FSI = InsertNewInstBefore(FSI, I); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3042 | |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 3043 | // construct the select instruction and return it. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 3044 | return SelectInst::Create(SI->getOperand(0), TSI, FSI, SI->getName()); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3045 | } |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 3046 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3047 | return 0; |
| 3048 | } |
| 3049 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3050 | Instruction *InstCombiner::visitSDiv(BinaryOperator &I) { |
| 3051 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 3052 | |
| 3053 | // Handle the integer div common cases |
| 3054 | if (Instruction *Common = commonIDivTransforms(I)) |
| 3055 | return Common; |
| 3056 | |
| 3057 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
| 3058 | // sdiv X, -1 == -X |
| 3059 | if (RHS->isAllOnesValue()) |
| 3060 | return BinaryOperator::createNeg(Op0); |
| 3061 | |
| 3062 | // -X/C -> X/-C |
| 3063 | if (Value *LHSNeg = dyn_castNegVal(Op0)) |
| 3064 | return BinaryOperator::createSDiv(LHSNeg, ConstantExpr::getNeg(RHS)); |
| 3065 | } |
| 3066 | |
| 3067 | // If the sign bits of both operands are zero (i.e. we can prove they are |
| 3068 | // unsigned inputs), turn this into a udiv. |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 3069 | if (I.getType()->isInteger()) { |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 3070 | APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits())); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3071 | if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) { |
Dan Gohman | cff5509 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 3072 | // X sdiv Y -> X udiv Y, iff X and Y don't have sign bit set |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3073 | return BinaryOperator::createUDiv(Op0, Op1, I.getName()); |
| 3074 | } |
| 3075 | } |
| 3076 | |
| 3077 | return 0; |
| 3078 | } |
| 3079 | |
| 3080 | Instruction *InstCombiner::visitFDiv(BinaryOperator &I) { |
| 3081 | return commonDivTransforms(I); |
| 3082 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3083 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3084 | /// This function implements the transforms on rem instructions that work |
| 3085 | /// regardless of the kind of rem instruction it is (urem, srem, or frem). It |
| 3086 | /// is used by the visitors to those instructions. |
| 3087 | /// @brief Transforms common to all three rem instructions |
| 3088 | Instruction *InstCombiner::commonRemTransforms(BinaryOperator &I) { |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 3089 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3090 | |
Chris Lattner | 50b2ca4 | 2008-02-19 06:12:18 +0000 | [diff] [blame] | 3091 | // 0 % X == 0 for integer, we don't need to preserve faults! |
Chris Lattner | 19ccd5c | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 3092 | if (Constant *LHS = dyn_cast<Constant>(Op0)) |
| 3093 | if (LHS->isNullValue()) |
| 3094 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 3095 | |
Chris Lattner | 50b2ca4 | 2008-02-19 06:12:18 +0000 | [diff] [blame] | 3096 | if (isa<UndefValue>(Op0)) { // undef % X -> 0 |
| 3097 | if (I.getType()->isFPOrFPVector()) |
| 3098 | return ReplaceInstUsesWith(I, Op0); // X % undef -> undef (could be SNaN) |
Chris Lattner | 19ccd5c | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 3099 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | 50b2ca4 | 2008-02-19 06:12:18 +0000 | [diff] [blame] | 3100 | } |
Chris Lattner | 19ccd5c | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 3101 | if (isa<UndefValue>(Op1)) |
| 3102 | return ReplaceInstUsesWith(I, Op1); // X % undef -> undef |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3103 | |
| 3104 | // Handle cases involving: rem X, (select Cond, Y, Z) |
| 3105 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { |
| 3106 | // rem X, (Cond ? 0 : Y) -> rem X, Y. If the rem and the select are in |
| 3107 | // the same basic block, then we replace the select with Y, and the |
| 3108 | // condition of the select with false (if the cond value is in the same |
| 3109 | // BB). If the select has uses other than the div, this allows them to be |
| 3110 | // simplified also. |
| 3111 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(1))) |
| 3112 | if (ST->isNullValue()) { |
| 3113 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); |
| 3114 | if (CondI && CondI->getParent() == I.getParent()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3115 | UpdateValueUsesWith(CondI, ConstantInt::getFalse()); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3116 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
| 3117 | I.setOperand(1, SI->getOperand(2)); |
| 3118 | else |
| 3119 | UpdateValueUsesWith(SI, SI->getOperand(2)); |
Chris Lattner | 5b73c08 | 2004-07-06 07:01:22 +0000 | [diff] [blame] | 3120 | return &I; |
| 3121 | } |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3122 | // Likewise for: rem X, (Cond ? Y : 0) -> rem X, Y |
| 3123 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(2))) |
| 3124 | if (ST->isNullValue()) { |
| 3125 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); |
| 3126 | if (CondI && CondI->getParent() == I.getParent()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3127 | UpdateValueUsesWith(CondI, ConstantInt::getTrue()); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3128 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
| 3129 | I.setOperand(1, SI->getOperand(1)); |
| 3130 | else |
| 3131 | UpdateValueUsesWith(SI, SI->getOperand(1)); |
| 3132 | return &I; |
| 3133 | } |
Chris Lattner | 11a49f2 | 2005-11-05 07:28:37 +0000 | [diff] [blame] | 3134 | } |
Chris Lattner | 5b73c08 | 2004-07-06 07:01:22 +0000 | [diff] [blame] | 3135 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3136 | return 0; |
| 3137 | } |
| 3138 | |
| 3139 | /// This function implements the transforms common to both integer remainder |
| 3140 | /// instructions (urem and srem). It is called by the visitors to those integer |
| 3141 | /// remainder instructions. |
| 3142 | /// @brief Common integer remainder transforms |
| 3143 | Instruction *InstCombiner::commonIRemTransforms(BinaryOperator &I) { |
| 3144 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 3145 | |
| 3146 | if (Instruction *common = commonRemTransforms(I)) |
| 3147 | return common; |
| 3148 | |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 3149 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 19ccd5c | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 3150 | // X % 0 == undef, we don't need to preserve faults! |
| 3151 | if (RHS->equalsInt(0)) |
| 3152 | return ReplaceInstUsesWith(I, UndefValue::get(I.getType())); |
| 3153 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3154 | if (RHS->equalsInt(1)) // X % 1 == 0 |
| 3155 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 3156 | |
Chris Lattner | 9794392 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 3157 | if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) { |
| 3158 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0I)) { |
| 3159 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 3160 | return R; |
| 3161 | } else if (isa<PHINode>(Op0I)) { |
| 3162 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 3163 | return NV; |
Chris Lattner | 9794392 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 3164 | } |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 3165 | |
| 3166 | // See if we can fold away this rem instruction. |
| 3167 | uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth(); |
| 3168 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 3169 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth), |
| 3170 | KnownZero, KnownOne)) |
| 3171 | return &I; |
Chris Lattner | 9794392 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 3172 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3173 | } |
| 3174 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3175 | return 0; |
| 3176 | } |
| 3177 | |
| 3178 | Instruction *InstCombiner::visitURem(BinaryOperator &I) { |
| 3179 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 3180 | |
| 3181 | if (Instruction *common = commonIRemTransforms(I)) |
| 3182 | return common; |
| 3183 | |
| 3184 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
| 3185 | // X urem C^2 -> X and C |
| 3186 | // Check to see if this is an unsigned remainder with an exact power of 2, |
| 3187 | // if so, convert to a bitwise and. |
| 3188 | if (ConstantInt *C = dyn_cast<ConstantInt>(RHS)) |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 3189 | if (C->getValue().isPowerOf2()) |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3190 | return BinaryOperator::createAnd(Op0, SubOne(C)); |
| 3191 | } |
| 3192 | |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 3193 | if (Instruction *RHSI = dyn_cast<Instruction>(I.getOperand(1))) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3194 | // Turn A % (C << N), where C is 2^k, into A & ((C << N)-1) |
| 3195 | if (RHSI->getOpcode() == Instruction::Shl && |
| 3196 | isa<ConstantInt>(RHSI->getOperand(0))) { |
Zhou Sheng | 0fc5095 | 2007-03-25 05:01:29 +0000 | [diff] [blame] | 3197 | if (cast<ConstantInt>(RHSI->getOperand(0))->getValue().isPowerOf2()) { |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 3198 | Constant *N1 = ConstantInt::getAllOnesValue(I.getType()); |
| 3199 | Value *Add = InsertNewInstBefore(BinaryOperator::createAdd(RHSI, N1, |
| 3200 | "tmp"), I); |
| 3201 | return BinaryOperator::createAnd(Op0, Add); |
| 3202 | } |
| 3203 | } |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3204 | } |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 3205 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3206 | // urem X, (select Cond, 2^C1, 2^C2) --> select Cond, (and X, C1), (and X, C2) |
| 3207 | // where C1&C2 are powers of two. |
| 3208 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { |
| 3209 | if (ConstantInt *STO = dyn_cast<ConstantInt>(SI->getOperand(1))) |
| 3210 | if (ConstantInt *SFO = dyn_cast<ConstantInt>(SI->getOperand(2))) { |
| 3211 | // STO == 0 and SFO == 0 handled above. |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 3212 | if ((STO->getValue().isPowerOf2()) && |
| 3213 | (SFO->getValue().isPowerOf2())) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3214 | Value *TrueAnd = InsertNewInstBefore( |
| 3215 | BinaryOperator::createAnd(Op0, SubOne(STO), SI->getName()+".t"), I); |
| 3216 | Value *FalseAnd = InsertNewInstBefore( |
| 3217 | BinaryOperator::createAnd(Op0, SubOne(SFO), SI->getName()+".f"), I); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 3218 | return SelectInst::Create(SI->getOperand(0), TrueAnd, FalseAnd); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3219 | } |
| 3220 | } |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 3221 | } |
| 3222 | |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3223 | return 0; |
| 3224 | } |
| 3225 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3226 | Instruction *InstCombiner::visitSRem(BinaryOperator &I) { |
| 3227 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 3228 | |
Dan Gohman | cff5509 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 3229 | // Handle the integer rem common cases |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3230 | if (Instruction *common = commonIRemTransforms(I)) |
| 3231 | return common; |
| 3232 | |
| 3233 | if (Value *RHSNeg = dyn_castNegVal(Op1)) |
| 3234 | if (!isa<ConstantInt>(RHSNeg) || |
Zhou Sheng | 0fc5095 | 2007-03-25 05:01:29 +0000 | [diff] [blame] | 3235 | cast<ConstantInt>(RHSNeg)->getValue().isStrictlyPositive()) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3236 | // X % -Y -> X % Y |
| 3237 | AddUsesToWorkList(I); |
| 3238 | I.setOperand(1, RHSNeg); |
| 3239 | return &I; |
| 3240 | } |
| 3241 | |
Dan Gohman | cff5509 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 3242 | // 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] | 3243 | // unsigned inputs), turn this into a urem. |
Dan Gohman | cff5509 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 3244 | if (I.getType()->isInteger()) { |
| 3245 | APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits())); |
| 3246 | if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) { |
| 3247 | // X srem Y -> X urem Y, iff X and Y don't have sign bit set |
| 3248 | return BinaryOperator::createURem(Op0, Op1, I.getName()); |
| 3249 | } |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3250 | } |
| 3251 | |
| 3252 | return 0; |
| 3253 | } |
| 3254 | |
| 3255 | Instruction *InstCombiner::visitFRem(BinaryOperator &I) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3256 | return commonRemTransforms(I); |
| 3257 | } |
| 3258 | |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 3259 | // isMaxValueMinusOne - return true if this is Max-1 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3260 | static bool isMaxValueMinusOne(const ConstantInt *C, bool isSigned) { |
Reid Spencer | 3a2a9fb | 2007-03-19 21:10:28 +0000 | [diff] [blame] | 3261 | uint32_t TypeBits = C->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 3262 | if (!isSigned) |
| 3263 | return C->getValue() == APInt::getAllOnesValue(TypeBits) - 1; |
| 3264 | return C->getValue() == APInt::getSignedMaxValue(TypeBits)-1; |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 3265 | } |
| 3266 | |
| 3267 | // isMinValuePlusOne - return true if this is Min+1 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3268 | static bool isMinValuePlusOne(const ConstantInt *C, bool isSigned) { |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 3269 | if (!isSigned) |
| 3270 | return C->getValue() == 1; // unsigned |
| 3271 | |
| 3272 | // Calculate 1111111111000000000000 |
| 3273 | uint32_t TypeBits = C->getType()->getPrimitiveSizeInBits(); |
| 3274 | return C->getValue() == APInt::getSignedMinValue(TypeBits)+1; |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 3275 | } |
| 3276 | |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 3277 | // isOneBitSet - Return true if there is exactly one bit set in the specified |
| 3278 | // constant. |
| 3279 | static bool isOneBitSet(const ConstantInt *CI) { |
Reid Spencer | 5f6a895 | 2007-03-20 00:16:52 +0000 | [diff] [blame] | 3280 | return CI->getValue().isPowerOf2(); |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 3281 | } |
| 3282 | |
Chris Lattner | b20ba0a | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 3283 | // isHighOnes - Return true if the constant is of the form 1+0+. |
| 3284 | // This is the same as lowones(~X). |
| 3285 | static bool isHighOnes(const ConstantInt *CI) { |
Zhou Sheng | 2cde46c | 2007-03-20 12:49:06 +0000 | [diff] [blame] | 3286 | return (~CI->getValue() + 1).isPowerOf2(); |
Chris Lattner | b20ba0a | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 3287 | } |
| 3288 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3289 | /// getICmpCode - Encode a icmp predicate into a three bit mask. These bits |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3290 | /// are carefully arranged to allow folding of expressions such as: |
| 3291 | /// |
| 3292 | /// (A < B) | (A > B) --> (A != B) |
| 3293 | /// |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3294 | /// Note that this is only valid if the first and second predicates have the |
| 3295 | /// same sign. Is illegal to do: (A u< B) | (A s> B) |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3296 | /// |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3297 | /// Three bits are used to represent the condition, as follows: |
| 3298 | /// 0 A > B |
| 3299 | /// 1 A == B |
| 3300 | /// 2 A < B |
| 3301 | /// |
| 3302 | /// <=> Value Definition |
| 3303 | /// 000 0 Always false |
| 3304 | /// 001 1 A > B |
| 3305 | /// 010 2 A == B |
| 3306 | /// 011 3 A >= B |
| 3307 | /// 100 4 A < B |
| 3308 | /// 101 5 A != B |
| 3309 | /// 110 6 A <= B |
| 3310 | /// 111 7 Always true |
| 3311 | /// |
| 3312 | static unsigned getICmpCode(const ICmpInst *ICI) { |
| 3313 | switch (ICI->getPredicate()) { |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3314 | // False -> 0 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3315 | case ICmpInst::ICMP_UGT: return 1; // 001 |
| 3316 | case ICmpInst::ICMP_SGT: return 1; // 001 |
| 3317 | case ICmpInst::ICMP_EQ: return 2; // 010 |
| 3318 | case ICmpInst::ICMP_UGE: return 3; // 011 |
| 3319 | case ICmpInst::ICMP_SGE: return 3; // 011 |
| 3320 | case ICmpInst::ICMP_ULT: return 4; // 100 |
| 3321 | case ICmpInst::ICMP_SLT: return 4; // 100 |
| 3322 | case ICmpInst::ICMP_NE: return 5; // 101 |
| 3323 | case ICmpInst::ICMP_ULE: return 6; // 110 |
| 3324 | case ICmpInst::ICMP_SLE: return 6; // 110 |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3325 | // True -> 7 |
| 3326 | default: |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3327 | assert(0 && "Invalid ICmp predicate!"); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3328 | return 0; |
| 3329 | } |
| 3330 | } |
| 3331 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3332 | /// getICmpValue - This is the complement of getICmpCode, which turns an |
| 3333 | /// 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] | 3334 | /// new ICmp instruction. The sign is passed in to determine which kind |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3335 | /// of predicate to use in new icmp instructions. |
| 3336 | static Value *getICmpValue(bool sign, unsigned code, Value *LHS, Value *RHS) { |
| 3337 | switch (code) { |
| 3338 | default: assert(0 && "Illegal ICmp code!"); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3339 | case 0: return ConstantInt::getFalse(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3340 | case 1: |
| 3341 | if (sign) |
| 3342 | return new ICmpInst(ICmpInst::ICMP_SGT, LHS, RHS); |
| 3343 | else |
| 3344 | return new ICmpInst(ICmpInst::ICMP_UGT, LHS, RHS); |
| 3345 | case 2: return new ICmpInst(ICmpInst::ICMP_EQ, LHS, RHS); |
| 3346 | case 3: |
| 3347 | if (sign) |
| 3348 | return new ICmpInst(ICmpInst::ICMP_SGE, LHS, RHS); |
| 3349 | else |
| 3350 | return new ICmpInst(ICmpInst::ICMP_UGE, LHS, RHS); |
| 3351 | case 4: |
| 3352 | if (sign) |
| 3353 | return new ICmpInst(ICmpInst::ICMP_SLT, LHS, RHS); |
| 3354 | else |
| 3355 | return new ICmpInst(ICmpInst::ICMP_ULT, LHS, RHS); |
| 3356 | case 5: return new ICmpInst(ICmpInst::ICMP_NE, LHS, RHS); |
| 3357 | case 6: |
| 3358 | if (sign) |
| 3359 | return new ICmpInst(ICmpInst::ICMP_SLE, LHS, RHS); |
| 3360 | else |
| 3361 | return new ICmpInst(ICmpInst::ICMP_ULE, LHS, RHS); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3362 | case 7: return ConstantInt::getTrue(); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3363 | } |
| 3364 | } |
| 3365 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3366 | static bool PredicatesFoldable(ICmpInst::Predicate p1, ICmpInst::Predicate p2) { |
| 3367 | return (ICmpInst::isSignedPredicate(p1) == ICmpInst::isSignedPredicate(p2)) || |
| 3368 | (ICmpInst::isSignedPredicate(p1) && |
| 3369 | (p2 == ICmpInst::ICMP_EQ || p2 == ICmpInst::ICMP_NE)) || |
| 3370 | (ICmpInst::isSignedPredicate(p2) && |
| 3371 | (p1 == ICmpInst::ICMP_EQ || p1 == ICmpInst::ICMP_NE)); |
| 3372 | } |
| 3373 | |
| 3374 | namespace { |
| 3375 | // FoldICmpLogical - Implements (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B) |
| 3376 | struct FoldICmpLogical { |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3377 | InstCombiner &IC; |
| 3378 | Value *LHS, *RHS; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3379 | ICmpInst::Predicate pred; |
| 3380 | FoldICmpLogical(InstCombiner &ic, ICmpInst *ICI) |
| 3381 | : IC(ic), LHS(ICI->getOperand(0)), RHS(ICI->getOperand(1)), |
| 3382 | pred(ICI->getPredicate()) {} |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3383 | bool shouldApply(Value *V) const { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3384 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(V)) |
| 3385 | if (PredicatesFoldable(pred, ICI->getPredicate())) |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 3386 | return ((ICI->getOperand(0) == LHS && ICI->getOperand(1) == RHS) || |
| 3387 | (ICI->getOperand(0) == RHS && ICI->getOperand(1) == LHS)); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3388 | return false; |
| 3389 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3390 | Instruction *apply(Instruction &Log) const { |
| 3391 | ICmpInst *ICI = cast<ICmpInst>(Log.getOperand(0)); |
| 3392 | if (ICI->getOperand(0) != LHS) { |
| 3393 | assert(ICI->getOperand(1) == LHS); |
| 3394 | ICI->swapOperands(); // Swap the LHS and RHS of the ICmp |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3395 | } |
| 3396 | |
Chris Lattner | bc1dbfc | 2007-03-13 14:27:42 +0000 | [diff] [blame] | 3397 | ICmpInst *RHSICI = cast<ICmpInst>(Log.getOperand(1)); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3398 | unsigned LHSCode = getICmpCode(ICI); |
Chris Lattner | bc1dbfc | 2007-03-13 14:27:42 +0000 | [diff] [blame] | 3399 | unsigned RHSCode = getICmpCode(RHSICI); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3400 | unsigned Code; |
| 3401 | switch (Log.getOpcode()) { |
| 3402 | case Instruction::And: Code = LHSCode & RHSCode; break; |
| 3403 | case Instruction::Or: Code = LHSCode | RHSCode; break; |
| 3404 | case Instruction::Xor: Code = LHSCode ^ RHSCode; break; |
Chris Lattner | 021c190 | 2003-09-22 20:33:34 +0000 | [diff] [blame] | 3405 | default: assert(0 && "Illegal logical opcode!"); return 0; |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3406 | } |
| 3407 | |
Chris Lattner | bc1dbfc | 2007-03-13 14:27:42 +0000 | [diff] [blame] | 3408 | bool isSigned = ICmpInst::isSignedPredicate(RHSICI->getPredicate()) || |
| 3409 | ICmpInst::isSignedPredicate(ICI->getPredicate()); |
| 3410 | |
| 3411 | Value *RV = getICmpValue(isSigned, Code, LHS, RHS); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3412 | if (Instruction *I = dyn_cast<Instruction>(RV)) |
| 3413 | return I; |
| 3414 | // Otherwise, it's a constant boolean value... |
| 3415 | return IC.ReplaceInstUsesWith(Log, RV); |
| 3416 | } |
| 3417 | }; |
Chris Lattner | d23b5ba | 2006-11-15 04:53:24 +0000 | [diff] [blame] | 3418 | } // end anonymous namespace |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3419 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3420 | // OptAndOp - This handles expressions of the form ((val OP C1) & C2). Where |
| 3421 | // 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] | 3422 | // guaranteed to be a binary operator. |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3423 | Instruction *InstCombiner::OptAndOp(Instruction *Op, |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3424 | ConstantInt *OpRHS, |
| 3425 | ConstantInt *AndRHS, |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3426 | BinaryOperator &TheAnd) { |
| 3427 | Value *X = Op->getOperand(0); |
Chris Lattner | 76f7fe2 | 2004-01-12 19:47:05 +0000 | [diff] [blame] | 3428 | Constant *Together = 0; |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3429 | if (!Op->isShift()) |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 3430 | Together = And(AndRHS, OpRHS); |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3431 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3432 | switch (Op->getOpcode()) { |
| 3433 | case Instruction::Xor: |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3434 | if (Op->hasOneUse()) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3435 | // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2) |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3436 | Instruction *And = BinaryOperator::createAnd(X, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3437 | InsertNewInstBefore(And, TheAnd); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3438 | And->takeName(Op); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3439 | return BinaryOperator::createXor(And, Together); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3440 | } |
| 3441 | break; |
| 3442 | case Instruction::Or: |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3443 | if (Together == AndRHS) // (X | C) & C --> C |
| 3444 | return ReplaceInstUsesWith(TheAnd, AndRHS); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3445 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3446 | if (Op->hasOneUse() && Together != OpRHS) { |
| 3447 | // (X | C1) & C2 --> (X | (C1&C2)) & C2 |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3448 | Instruction *Or = BinaryOperator::createOr(X, Together); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3449 | InsertNewInstBefore(Or, TheAnd); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3450 | Or->takeName(Op); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3451 | return BinaryOperator::createAnd(Or, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3452 | } |
| 3453 | break; |
| 3454 | case Instruction::Add: |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 3455 | if (Op->hasOneUse()) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3456 | // Adding a one to a single bit bit-field should be turned into an XOR |
| 3457 | // of the bit. First thing to check is to see if this AND is with a |
| 3458 | // single bit constant. |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3459 | const APInt& AndRHSV = cast<ConstantInt>(AndRHS)->getValue(); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3460 | |
| 3461 | // If there is only one bit set... |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 3462 | if (isOneBitSet(cast<ConstantInt>(AndRHS))) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3463 | // Ok, at this point, we know that we are masking the result of the |
| 3464 | // ADD down to exactly one bit. If the constant we are adding has |
| 3465 | // no bits set below this bit, then we can eliminate the ADD. |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3466 | const APInt& AddRHS = cast<ConstantInt>(OpRHS)->getValue(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3467 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3468 | // Check to see if any bits below the one bit set in AndRHSV are set. |
| 3469 | if ((AddRHS & (AndRHSV-1)) == 0) { |
| 3470 | // If not, the only thing that can effect the output of the AND is |
| 3471 | // the bit specified by AndRHSV. If that bit is set, the effect of |
| 3472 | // the XOR is to toggle the bit. If it is clear, then the ADD has |
| 3473 | // no effect. |
| 3474 | if ((AddRHS & AndRHSV) == 0) { // Bit is not set, noop |
| 3475 | TheAnd.setOperand(0, X); |
| 3476 | return &TheAnd; |
| 3477 | } else { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3478 | // Pull the XOR out of the AND. |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3479 | Instruction *NewAnd = BinaryOperator::createAnd(X, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3480 | InsertNewInstBefore(NewAnd, TheAnd); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3481 | NewAnd->takeName(Op); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3482 | return BinaryOperator::createXor(NewAnd, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3483 | } |
| 3484 | } |
| 3485 | } |
| 3486 | } |
| 3487 | break; |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 3488 | |
| 3489 | case Instruction::Shl: { |
| 3490 | // We know that the AND will not produce any of the bits shifted in, so if |
| 3491 | // the anded constant includes them, clear them now! |
| 3492 | // |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3493 | uint32_t BitWidth = AndRHS->getType()->getBitWidth(); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3494 | uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3495 | APInt ShlMask(APInt::getHighBitsSet(BitWidth, BitWidth-OpRHSVal)); |
| 3496 | ConstantInt *CI = ConstantInt::get(AndRHS->getValue() & ShlMask); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3497 | |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3498 | if (CI->getValue() == ShlMask) { |
| 3499 | // Masking out bits that the shift already masks |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 3500 | return ReplaceInstUsesWith(TheAnd, Op); // No need for the and. |
| 3501 | } else if (CI != AndRHS) { // Reducing bits set in and. |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 3502 | TheAnd.setOperand(1, CI); |
| 3503 | return &TheAnd; |
| 3504 | } |
| 3505 | break; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3506 | } |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 3507 | case Instruction::LShr: |
| 3508 | { |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 3509 | // We know that the AND will not produce any of the bits shifted in, so if |
| 3510 | // the anded constant includes them, clear them now! This only applies to |
| 3511 | // unsigned shifts, because a signed shr may bring in set bits! |
| 3512 | // |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3513 | uint32_t BitWidth = AndRHS->getType()->getBitWidth(); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3514 | uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3515 | APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal)); |
| 3516 | ConstantInt *CI = ConstantInt::get(AndRHS->getValue() & ShrMask); |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 3517 | |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3518 | if (CI->getValue() == ShrMask) { |
| 3519 | // Masking out bits that the shift already masks. |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 3520 | return ReplaceInstUsesWith(TheAnd, Op); |
| 3521 | } else if (CI != AndRHS) { |
| 3522 | TheAnd.setOperand(1, CI); // Reduce bits set in and cst. |
| 3523 | return &TheAnd; |
| 3524 | } |
| 3525 | break; |
| 3526 | } |
| 3527 | case Instruction::AShr: |
| 3528 | // Signed shr. |
| 3529 | // See if this is shifting in some sign extension, then masking it out |
| 3530 | // with an and. |
| 3531 | if (Op->hasOneUse()) { |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3532 | uint32_t BitWidth = AndRHS->getType()->getBitWidth(); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3533 | uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3534 | APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal)); |
| 3535 | Constant *C = ConstantInt::get(AndRHS->getValue() & ShrMask); |
Reid Spencer | 7eb7638 | 2006-12-13 17:19:09 +0000 | [diff] [blame] | 3536 | if (C == AndRHS) { // Masking out bits shifted in. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 3537 | // (Val ashr C1) & C2 -> (Val lshr C1) & C2 |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 3538 | // Make the argument unsigned. |
| 3539 | Value *ShVal = Op->getOperand(0); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3540 | ShVal = InsertNewInstBefore( |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 3541 | BinaryOperator::createLShr(ShVal, OpRHS, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3542 | Op->getName()), TheAnd); |
Reid Spencer | 7eb7638 | 2006-12-13 17:19:09 +0000 | [diff] [blame] | 3543 | return BinaryOperator::createAnd(ShVal, AndRHS, TheAnd.getName()); |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 3544 | } |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 3545 | } |
| 3546 | break; |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3547 | } |
| 3548 | return 0; |
| 3549 | } |
| 3550 | |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 3551 | |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3552 | /// InsertRangeTest - Emit a computation of: (V >= Lo && V < Hi) if Inside is |
| 3553 | /// 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] | 3554 | /// (V-Lo) <u Hi-Lo. This method expects that Lo <= Hi. isSigned indicates |
| 3555 | /// 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] | 3556 | /// insert new instructions. |
| 3557 | Instruction *InstCombiner::InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3558 | bool isSigned, bool Inside, |
| 3559 | Instruction &IB) { |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3560 | assert(cast<ConstantInt>(ConstantExpr::getICmp((isSigned ? |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 3561 | ICmpInst::ICMP_SLE:ICmpInst::ICMP_ULE), Lo, Hi))->getZExtValue() && |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3562 | "Lo is not <= Hi in range emission code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3563 | |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3564 | if (Inside) { |
| 3565 | if (Lo == Hi) // Trivially false. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3566 | return new ICmpInst(ICmpInst::ICMP_NE, V, V); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3567 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3568 | // V >= Min && V < Hi --> V < Hi |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3569 | if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) { |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 3570 | ICmpInst::Predicate pred = (isSigned ? |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3571 | ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT); |
| 3572 | return new ICmpInst(pred, V, Hi); |
| 3573 | } |
| 3574 | |
| 3575 | // Emit V-Lo <u Hi-Lo |
| 3576 | Constant *NegLo = ConstantExpr::getNeg(Lo); |
| 3577 | Instruction *Add = BinaryOperator::createAdd(V, NegLo, V->getName()+".off"); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3578 | InsertNewInstBefore(Add, IB); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3579 | Constant *UpperBound = ConstantExpr::getAdd(NegLo, Hi); |
| 3580 | return new ICmpInst(ICmpInst::ICMP_ULT, Add, UpperBound); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3581 | } |
| 3582 | |
| 3583 | if (Lo == Hi) // Trivially true. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3584 | return new ICmpInst(ICmpInst::ICMP_EQ, V, V); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3585 | |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 3586 | // V < Min || V >= Hi -> V > Hi-1 |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3587 | Hi = SubOne(cast<ConstantInt>(Hi)); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3588 | if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3589 | ICmpInst::Predicate pred = (isSigned ? |
| 3590 | ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT); |
| 3591 | return new ICmpInst(pred, V, Hi); |
| 3592 | } |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3593 | |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 3594 | // Emit V-Lo >u Hi-1-Lo |
| 3595 | // Note that Hi has already had one subtracted from it, above. |
| 3596 | ConstantInt *NegLo = cast<ConstantInt>(ConstantExpr::getNeg(Lo)); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3597 | Instruction *Add = BinaryOperator::createAdd(V, NegLo, V->getName()+".off"); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3598 | InsertNewInstBefore(Add, IB); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3599 | Constant *LowerBound = ConstantExpr::getAdd(NegLo, Hi); |
| 3600 | return new ICmpInst(ICmpInst::ICMP_UGT, Add, LowerBound); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3601 | } |
| 3602 | |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3603 | // isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with |
| 3604 | // any number of 0s on either side. The 1s are allowed to wrap from LSB to |
| 3605 | // MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is |
| 3606 | // not, since all 1s are not contiguous. |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 3607 | static bool isRunOfOnes(ConstantInt *Val, uint32_t &MB, uint32_t &ME) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3608 | const APInt& V = Val->getValue(); |
Reid Spencer | f244252 | 2007-03-24 00:42:08 +0000 | [diff] [blame] | 3609 | uint32_t BitWidth = Val->getType()->getBitWidth(); |
| 3610 | if (!APIntOps::isShiftedMask(BitWidth, V)) return false; |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3611 | |
| 3612 | // look for the first zero bit after the run of ones |
Reid Spencer | f244252 | 2007-03-24 00:42:08 +0000 | [diff] [blame] | 3613 | MB = BitWidth - ((V - 1) ^ V).countLeadingZeros(); |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3614 | // look for the first non-zero bit |
Reid Spencer | f244252 | 2007-03-24 00:42:08 +0000 | [diff] [blame] | 3615 | ME = V.getActiveBits(); |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3616 | return true; |
| 3617 | } |
| 3618 | |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3619 | /// FoldLogicalPlusAnd - This is part of an expression (LHS +/- RHS) & Mask, |
| 3620 | /// where isSub determines whether the operator is a sub. If we can fold one of |
| 3621 | /// the following xforms: |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3622 | /// |
| 3623 | /// ((A & N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == Mask |
| 3624 | /// ((A | N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0 |
| 3625 | /// ((A ^ N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0 |
| 3626 | /// |
| 3627 | /// return (A +/- B). |
| 3628 | /// |
| 3629 | Value *InstCombiner::FoldLogicalPlusAnd(Value *LHS, Value *RHS, |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3630 | ConstantInt *Mask, bool isSub, |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3631 | Instruction &I) { |
| 3632 | Instruction *LHSI = dyn_cast<Instruction>(LHS); |
| 3633 | if (!LHSI || LHSI->getNumOperands() != 2 || |
| 3634 | !isa<ConstantInt>(LHSI->getOperand(1))) return 0; |
| 3635 | |
| 3636 | ConstantInt *N = cast<ConstantInt>(LHSI->getOperand(1)); |
| 3637 | |
| 3638 | switch (LHSI->getOpcode()) { |
| 3639 | default: return 0; |
| 3640 | case Instruction::And: |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 3641 | if (And(N, Mask) == Mask) { |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3642 | // 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] | 3643 | if ((Mask->getValue().countLeadingZeros() + |
| 3644 | Mask->getValue().countPopulation()) == |
| 3645 | Mask->getValue().getBitWidth()) |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3646 | break; |
| 3647 | |
| 3648 | // Otherwise, if Mask is 0+1+0+, and if B is known to have the low 0+ |
| 3649 | // part, we don't need any explicit masks to take them out of A. If that |
| 3650 | // is all N is, ignore it. |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 3651 | uint32_t MB = 0, ME = 0; |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3652 | if (isRunOfOnes(Mask, MB, ME)) { // begin/end bit of run, inclusive |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 3653 | uint32_t BitWidth = cast<IntegerType>(RHS->getType())->getBitWidth(); |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3654 | APInt Mask(APInt::getLowBitsSet(BitWidth, MB-1)); |
Chris Lattner | 3bedbd9 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3655 | if (MaskedValueIsZero(RHS, Mask)) |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3656 | break; |
| 3657 | } |
| 3658 | } |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3659 | return 0; |
| 3660 | case Instruction::Or: |
| 3661 | case Instruction::Xor: |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3662 | // 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] | 3663 | if ((Mask->getValue().countLeadingZeros() + |
| 3664 | Mask->getValue().countPopulation()) == Mask->getValue().getBitWidth() |
Reid Spencer | 6eb0d99 | 2007-03-26 23:58:26 +0000 | [diff] [blame] | 3665 | && And(N, Mask)->isZero()) |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3666 | break; |
| 3667 | return 0; |
| 3668 | } |
| 3669 | |
| 3670 | Instruction *New; |
| 3671 | if (isSub) |
| 3672 | New = BinaryOperator::createSub(LHSI->getOperand(0), RHS, "fold"); |
| 3673 | else |
| 3674 | New = BinaryOperator::createAdd(LHSI->getOperand(0), RHS, "fold"); |
| 3675 | return InsertNewInstBefore(New, I); |
| 3676 | } |
| 3677 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3678 | Instruction *InstCombiner::visitAnd(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 3679 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3680 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3681 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3682 | if (isa<UndefValue>(Op1)) // X & undef -> 0 |
| 3683 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 3684 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3685 | // and X, X = X |
| 3686 | if (Op0 == Op1) |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 3687 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3688 | |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3689 | // 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] | 3690 | // purpose is to compute bits we don't care about. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 3691 | if (!isa<VectorType>(I.getType())) { |
Reid Spencer | a03d45f | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 3692 | uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth(); |
| 3693 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 3694 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth), |
Chris Lattner | 696ee0a | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3695 | KnownZero, KnownOne)) |
Reid Spencer | 6eb0d99 | 2007-03-26 23:58:26 +0000 | [diff] [blame] | 3696 | return &I; |
Chris Lattner | 696ee0a | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3697 | } else { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 3698 | if (ConstantVector *CP = dyn_cast<ConstantVector>(Op1)) { |
Chris Lattner | 041a6c9 | 2007-06-15 05:26:55 +0000 | [diff] [blame] | 3699 | if (CP->isAllOnesValue()) // X & <-1,-1> -> X |
Chris Lattner | 696ee0a | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3700 | return ReplaceInstUsesWith(I, I.getOperand(0)); |
Chris Lattner | 041a6c9 | 2007-06-15 05:26:55 +0000 | [diff] [blame] | 3701 | } else if (isa<ConstantAggregateZero>(Op1)) { |
| 3702 | return ReplaceInstUsesWith(I, Op1); // X & <0,0> -> <0,0> |
Chris Lattner | 696ee0a | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3703 | } |
| 3704 | } |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 3705 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3706 | if (ConstantInt *AndRHS = dyn_cast<ConstantInt>(Op1)) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3707 | const APInt& AndRHSMask = AndRHS->getValue(); |
| 3708 | APInt NotAndRHS(~AndRHSMask); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3709 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3710 | // Optimize a variety of ((val OP C1) & C2) combinations... |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3711 | if (isa<BinaryOperator>(Op0)) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3712 | Instruction *Op0I = cast<Instruction>(Op0); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3713 | Value *Op0LHS = Op0I->getOperand(0); |
| 3714 | Value *Op0RHS = Op0I->getOperand(1); |
| 3715 | switch (Op0I->getOpcode()) { |
| 3716 | case Instruction::Xor: |
| 3717 | case Instruction::Or: |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 3718 | // If the mask is only needed on one incoming arm, push it up. |
| 3719 | if (Op0I->hasOneUse()) { |
| 3720 | if (MaskedValueIsZero(Op0LHS, NotAndRHS)) { |
| 3721 | // Not masking anything out for the LHS, move to RHS. |
| 3722 | Instruction *NewRHS = BinaryOperator::createAnd(Op0RHS, AndRHS, |
| 3723 | Op0RHS->getName()+".masked"); |
| 3724 | InsertNewInstBefore(NewRHS, I); |
| 3725 | return BinaryOperator::create( |
| 3726 | cast<BinaryOperator>(Op0I)->getOpcode(), Op0LHS, NewRHS); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3727 | } |
Chris Lattner | 3bedbd9 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3728 | if (!isa<Constant>(Op0RHS) && |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 3729 | MaskedValueIsZero(Op0RHS, NotAndRHS)) { |
| 3730 | // Not masking anything out for the RHS, move to LHS. |
| 3731 | Instruction *NewLHS = BinaryOperator::createAnd(Op0LHS, AndRHS, |
| 3732 | Op0LHS->getName()+".masked"); |
| 3733 | InsertNewInstBefore(NewLHS, I); |
| 3734 | return BinaryOperator::create( |
| 3735 | cast<BinaryOperator>(Op0I)->getOpcode(), NewLHS, Op0RHS); |
| 3736 | } |
| 3737 | } |
| 3738 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3739 | break; |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3740 | case Instruction::Add: |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3741 | // ((A & N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == AndRHS. |
| 3742 | // ((A | N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0 |
| 3743 | // ((A ^ N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0 |
| 3744 | if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, false, I)) |
| 3745 | return BinaryOperator::createAnd(V, AndRHS); |
| 3746 | if (Value *V = FoldLogicalPlusAnd(Op0RHS, Op0LHS, AndRHS, false, I)) |
| 3747 | return BinaryOperator::createAnd(V, AndRHS); // Add commutes |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3748 | break; |
| 3749 | |
| 3750 | case Instruction::Sub: |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3751 | // ((A & N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == AndRHS. |
| 3752 | // ((A | N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0 |
| 3753 | // ((A ^ N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0 |
| 3754 | if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, true, I)) |
| 3755 | return BinaryOperator::createAnd(V, AndRHS); |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3756 | break; |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3757 | } |
| 3758 | |
Chris Lattner | 5840326 | 2003-07-23 19:25:52 +0000 | [diff] [blame] | 3759 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3760 | if (Instruction *Res = OptAndOp(Op0I, Op0CI, AndRHS, I)) |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3761 | return Res; |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3762 | } else if (CastInst *CI = dyn_cast<CastInst>(Op0)) { |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3763 | // If this is an integer truncation or change from signed-to-unsigned, and |
| 3764 | // if the source is an and/or with immediate, transform it. This |
| 3765 | // frequently occurs for bitfield accesses. |
| 3766 | if (Instruction *CastOp = dyn_cast<Instruction>(CI->getOperand(0))) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3767 | if ((isa<TruncInst>(CI) || isa<BitCastInst>(CI)) && |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3768 | CastOp->getNumOperands() == 2) |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 3769 | if (ConstantInt *AndCI = dyn_cast<ConstantInt>(CastOp->getOperand(1))) { |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3770 | if (CastOp->getOpcode() == Instruction::And) { |
| 3771 | // Change: and (cast (and X, C1) to T), C2 |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3772 | // into : and (cast X to T), trunc_or_bitcast(C1)&C2 |
| 3773 | // This will fold the two constants together, which may allow |
| 3774 | // other simplifications. |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 3775 | Instruction *NewCast = CastInst::createTruncOrBitCast( |
| 3776 | CastOp->getOperand(0), I.getType(), |
| 3777 | CastOp->getName()+".shrunk"); |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3778 | NewCast = InsertNewInstBefore(NewCast, I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3779 | // trunc_or_bitcast(C1)&C2 |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 3780 | Constant *C3 = ConstantExpr::getTruncOrBitCast(AndCI,I.getType()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3781 | C3 = ConstantExpr::getAnd(C3, AndRHS); |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3782 | return BinaryOperator::createAnd(NewCast, C3); |
| 3783 | } else if (CastOp->getOpcode() == Instruction::Or) { |
| 3784 | // Change: and (cast (or X, C1) to T), C2 |
| 3785 | // into : trunc(C1)&C2 iff trunc(C1)&C2 == C2 |
Chris Lattner | bb4e7b2 | 2006-12-12 19:11:20 +0000 | [diff] [blame] | 3786 | Constant *C3 = ConstantExpr::getTruncOrBitCast(AndCI,I.getType()); |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3787 | if (ConstantExpr::getAnd(C3, AndRHS) == AndRHS) // trunc(C1)&C2 |
| 3788 | return ReplaceInstUsesWith(I, AndRHS); |
| 3789 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 3790 | } |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3791 | } |
Chris Lattner | 06782f8 | 2003-07-23 19:36:21 +0000 | [diff] [blame] | 3792 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3793 | |
| 3794 | // Try to fold constant and into select arguments. |
| 3795 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3796 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3797 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 3798 | if (isa<PHINode>(Op0)) |
| 3799 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 3800 | return NV; |
Chris Lattner | c6a8aff | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 3801 | } |
| 3802 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 3803 | Value *Op0NotVal = dyn_castNotVal(Op0); |
| 3804 | Value *Op1NotVal = dyn_castNotVal(Op1); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3805 | |
Chris Lattner | 5b62aa7 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 3806 | if (Op0NotVal == Op1 || Op1NotVal == Op0) // A & ~A == ~A & A == 0 |
| 3807 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 3808 | |
Misha Brukman | cb6267b | 2004-07-30 12:50:08 +0000 | [diff] [blame] | 3809 | // (~A & ~B) == (~(A | B)) - De Morgan's Law |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 3810 | if (Op0NotVal && Op1NotVal && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3811 | Instruction *Or = BinaryOperator::createOr(Op0NotVal, Op1NotVal, |
| 3812 | I.getName()+".demorgan"); |
Chris Lattner | c6a8aff | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 3813 | InsertNewInstBefore(Or, I); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3814 | return BinaryOperator::createNot(Or); |
| 3815 | } |
Chris Lattner | 2082ad9 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3816 | |
| 3817 | { |
Chris Lattner | 003b620 | 2007-06-15 05:58:24 +0000 | [diff] [blame] | 3818 | Value *A = 0, *B = 0, *C = 0, *D = 0; |
| 3819 | if (match(Op0, m_Or(m_Value(A), m_Value(B)))) { |
Chris Lattner | 2082ad9 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3820 | if (A == Op1 || B == Op1) // (A | ?) & A --> A |
| 3821 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 003b620 | 2007-06-15 05:58:24 +0000 | [diff] [blame] | 3822 | |
| 3823 | // (A|B) & ~(A&B) -> A^B |
| 3824 | if (match(Op1, m_Not(m_And(m_Value(C), m_Value(D))))) { |
| 3825 | if ((A == C && B == D) || (A == D && B == C)) |
| 3826 | return BinaryOperator::createXor(A, B); |
| 3827 | } |
| 3828 | } |
| 3829 | |
| 3830 | if (match(Op1, m_Or(m_Value(A), m_Value(B)))) { |
Chris Lattner | 2082ad9 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3831 | if (A == Op0 || B == Op0) // A & (A | ?) --> A |
| 3832 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 003b620 | 2007-06-15 05:58:24 +0000 | [diff] [blame] | 3833 | |
| 3834 | // ~(A&B) & (A|B) -> A^B |
| 3835 | if (match(Op0, m_Not(m_And(m_Value(C), m_Value(D))))) { |
| 3836 | if ((A == C && B == D) || (A == D && B == C)) |
| 3837 | return BinaryOperator::createXor(A, B); |
| 3838 | } |
| 3839 | } |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 3840 | |
| 3841 | if (Op0->hasOneUse() && |
| 3842 | match(Op0, m_Xor(m_Value(A), m_Value(B)))) { |
| 3843 | if (A == Op1) { // (A^B)&A -> A&(A^B) |
| 3844 | I.swapOperands(); // Simplify below |
| 3845 | std::swap(Op0, Op1); |
| 3846 | } else if (B == Op1) { // (A^B)&B -> B&(B^A) |
| 3847 | cast<BinaryOperator>(Op0)->swapOperands(); |
| 3848 | I.swapOperands(); // Simplify below |
| 3849 | std::swap(Op0, Op1); |
| 3850 | } |
| 3851 | } |
| 3852 | if (Op1->hasOneUse() && |
| 3853 | match(Op1, m_Xor(m_Value(A), m_Value(B)))) { |
| 3854 | if (B == Op0) { // B&(A^B) -> B&(B^A) |
| 3855 | cast<BinaryOperator>(Op1)->swapOperands(); |
| 3856 | std::swap(A, B); |
| 3857 | } |
| 3858 | if (A == Op0) { // A&(A^B) -> A & ~B |
| 3859 | Instruction *NotB = BinaryOperator::createNot(B, "tmp"); |
| 3860 | InsertNewInstBefore(NotB, I); |
| 3861 | return BinaryOperator::createAnd(A, NotB); |
| 3862 | } |
| 3863 | } |
Chris Lattner | 2082ad9 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3864 | } |
| 3865 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3866 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(Op1)) { |
| 3867 | // (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B) |
| 3868 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS))) |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3869 | return R; |
| 3870 | |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3871 | Value *LHSVal, *RHSVal; |
| 3872 | ConstantInt *LHSCst, *RHSCst; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3873 | ICmpInst::Predicate LHSCC, RHSCC; |
| 3874 | if (match(Op0, m_ICmp(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst)))) |
| 3875 | if (match(RHS, m_ICmp(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst)))) |
| 3876 | if (LHSVal == RHSVal && // Found (X icmp C1) & (X icmp C2) |
| 3877 | // ICMP_[GL]E X, CST is folded to ICMP_[GL]T elsewhere. |
| 3878 | LHSCC != ICmpInst::ICMP_UGE && LHSCC != ICmpInst::ICMP_ULE && |
| 3879 | RHSCC != ICmpInst::ICMP_UGE && RHSCC != ICmpInst::ICMP_ULE && |
| 3880 | LHSCC != ICmpInst::ICMP_SGE && LHSCC != ICmpInst::ICMP_SLE && |
Chris Lattner | eec8b9a | 2007-11-22 23:47:13 +0000 | [diff] [blame] | 3881 | RHSCC != ICmpInst::ICMP_SGE && RHSCC != ICmpInst::ICMP_SLE && |
| 3882 | |
| 3883 | // Don't try to fold ICMP_SLT + ICMP_ULT. |
| 3884 | (ICmpInst::isEquality(LHSCC) || ICmpInst::isEquality(RHSCC) || |
| 3885 | ICmpInst::isSignedPredicate(LHSCC) == |
| 3886 | ICmpInst::isSignedPredicate(RHSCC))) { |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3887 | // Ensure that the larger constant is on the RHS. |
Chris Lattner | ee2b7a4 | 2008-01-13 20:59:02 +0000 | [diff] [blame] | 3888 | ICmpInst::Predicate GT; |
| 3889 | if (ICmpInst::isSignedPredicate(LHSCC) || |
| 3890 | (ICmpInst::isEquality(LHSCC) && |
| 3891 | ICmpInst::isSignedPredicate(RHSCC))) |
| 3892 | GT = ICmpInst::ICMP_SGT; |
| 3893 | else |
| 3894 | GT = ICmpInst::ICMP_UGT; |
| 3895 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3896 | Constant *Cmp = ConstantExpr::getICmp(GT, LHSCst, RHSCst); |
| 3897 | ICmpInst *LHS = cast<ICmpInst>(Op0); |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 3898 | if (cast<ConstantInt>(Cmp)->getZExtValue()) { |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3899 | std::swap(LHS, RHS); |
| 3900 | std::swap(LHSCst, RHSCst); |
| 3901 | std::swap(LHSCC, RHSCC); |
| 3902 | } |
| 3903 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3904 | // At this point, we know we have have two icmp instructions |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3905 | // comparing a value against two constants and and'ing the result |
| 3906 | // together. Because of the above check, we know that we only have |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3907 | // icmp eq, icmp ne, icmp [su]lt, and icmp [SU]gt here. We also know |
| 3908 | // (from the FoldICmpLogical check above), that the two constants |
| 3909 | // are not equal and that the larger constant is on the RHS |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3910 | assert(LHSCst != RHSCst && "Compares not folded above?"); |
| 3911 | |
| 3912 | switch (LHSCC) { |
| 3913 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3914 | case ICmpInst::ICMP_EQ: |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3915 | switch (RHSCC) { |
| 3916 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3917 | case ICmpInst::ICMP_EQ: // (X == 13 & X == 15) -> false |
| 3918 | case ICmpInst::ICMP_UGT: // (X == 13 & X > 15) -> false |
| 3919 | case ICmpInst::ICMP_SGT: // (X == 13 & X > 15) -> false |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3920 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3921 | case ICmpInst::ICMP_NE: // (X == 13 & X != 15) -> X == 13 |
| 3922 | case ICmpInst::ICMP_ULT: // (X == 13 & X < 15) -> X == 13 |
| 3923 | case ICmpInst::ICMP_SLT: // (X == 13 & X < 15) -> X == 13 |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3924 | return ReplaceInstUsesWith(I, LHS); |
| 3925 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3926 | case ICmpInst::ICMP_NE: |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3927 | switch (RHSCC) { |
| 3928 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3929 | case ICmpInst::ICMP_ULT: |
| 3930 | if (LHSCst == SubOne(RHSCst)) // (X != 13 & X u< 14) -> X < 13 |
| 3931 | return new ICmpInst(ICmpInst::ICMP_ULT, LHSVal, LHSCst); |
| 3932 | break; // (X != 13 & X u< 15) -> no change |
| 3933 | case ICmpInst::ICMP_SLT: |
| 3934 | if (LHSCst == SubOne(RHSCst)) // (X != 13 & X s< 14) -> X < 13 |
| 3935 | return new ICmpInst(ICmpInst::ICMP_SLT, LHSVal, LHSCst); |
| 3936 | break; // (X != 13 & X s< 15) -> no change |
| 3937 | case ICmpInst::ICMP_EQ: // (X != 13 & X == 15) -> X == 15 |
| 3938 | case ICmpInst::ICMP_UGT: // (X != 13 & X u> 15) -> X u> 15 |
| 3939 | case ICmpInst::ICMP_SGT: // (X != 13 & X s> 15) -> X s> 15 |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3940 | return ReplaceInstUsesWith(I, RHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3941 | case ICmpInst::ICMP_NE: |
| 3942 | if (LHSCst == SubOne(RHSCst)){// (X != 13 & X != 14) -> X-13 >u 1 |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3943 | Constant *AddCST = ConstantExpr::getNeg(LHSCst); |
| 3944 | Instruction *Add = BinaryOperator::createAdd(LHSVal, AddCST, |
| 3945 | LHSVal->getName()+".off"); |
| 3946 | InsertNewInstBefore(Add, I); |
Chris Lattner | 424db02 | 2007-01-27 23:08:34 +0000 | [diff] [blame] | 3947 | return new ICmpInst(ICmpInst::ICMP_UGT, Add, |
| 3948 | ConstantInt::get(Add->getType(), 1)); |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3949 | } |
| 3950 | break; // (X != 13 & X != 15) -> no change |
| 3951 | } |
| 3952 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3953 | case ICmpInst::ICMP_ULT: |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3954 | switch (RHSCC) { |
| 3955 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3956 | case ICmpInst::ICMP_EQ: // (X u< 13 & X == 15) -> false |
| 3957 | case ICmpInst::ICMP_UGT: // (X u< 13 & X u> 15) -> false |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3958 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3959 | case ICmpInst::ICMP_SGT: // (X u< 13 & X s> 15) -> no change |
| 3960 | break; |
| 3961 | case ICmpInst::ICMP_NE: // (X u< 13 & X != 15) -> X u< 13 |
| 3962 | case ICmpInst::ICMP_ULT: // (X u< 13 & X u< 15) -> X u< 13 |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3963 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3964 | case ICmpInst::ICMP_SLT: // (X u< 13 & X s< 15) -> no change |
| 3965 | break; |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3966 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3967 | break; |
| 3968 | case ICmpInst::ICMP_SLT: |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3969 | switch (RHSCC) { |
| 3970 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3971 | case ICmpInst::ICMP_EQ: // (X s< 13 & X == 15) -> false |
| 3972 | case ICmpInst::ICMP_SGT: // (X s< 13 & X s> 15) -> false |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3973 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3974 | case ICmpInst::ICMP_UGT: // (X s< 13 & X u> 15) -> no change |
| 3975 | break; |
| 3976 | case ICmpInst::ICMP_NE: // (X s< 13 & X != 15) -> X < 13 |
| 3977 | case ICmpInst::ICMP_SLT: // (X s< 13 & X s< 15) -> X < 13 |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3978 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3979 | case ICmpInst::ICMP_ULT: // (X s< 13 & X u< 15) -> no change |
| 3980 | break; |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3981 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3982 | break; |
| 3983 | case ICmpInst::ICMP_UGT: |
| 3984 | switch (RHSCC) { |
| 3985 | default: assert(0 && "Unknown integer condition code!"); |
| 3986 | case ICmpInst::ICMP_EQ: // (X u> 13 & X == 15) -> X > 13 |
| 3987 | return ReplaceInstUsesWith(I, LHS); |
| 3988 | case ICmpInst::ICMP_UGT: // (X u> 13 & X u> 15) -> X u> 15 |
| 3989 | return ReplaceInstUsesWith(I, RHS); |
| 3990 | case ICmpInst::ICMP_SGT: // (X u> 13 & X s> 15) -> no change |
| 3991 | break; |
| 3992 | case ICmpInst::ICMP_NE: |
| 3993 | if (RHSCst == AddOne(LHSCst)) // (X u> 13 & X != 14) -> X u> 14 |
| 3994 | return new ICmpInst(LHSCC, LHSVal, RHSCst); |
| 3995 | break; // (X u> 13 & X != 15) -> no change |
| 3996 | case ICmpInst::ICMP_ULT: // (X u> 13 & X u< 15) ->(X-14) <u 1 |
| 3997 | return InsertRangeTest(LHSVal, AddOne(LHSCst), RHSCst, false, |
| 3998 | true, I); |
| 3999 | case ICmpInst::ICMP_SLT: // (X u> 13 & X s< 15) -> no change |
| 4000 | break; |
| 4001 | } |
| 4002 | break; |
| 4003 | case ICmpInst::ICMP_SGT: |
| 4004 | switch (RHSCC) { |
| 4005 | default: assert(0 && "Unknown integer condition code!"); |
Chris Lattner | a7d1ab0 | 2007-11-16 06:04:17 +0000 | [diff] [blame] | 4006 | case ICmpInst::ICMP_EQ: // (X s> 13 & X == 15) -> X == 15 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4007 | case ICmpInst::ICMP_SGT: // (X s> 13 & X s> 15) -> X s> 15 |
| 4008 | return ReplaceInstUsesWith(I, RHS); |
| 4009 | case ICmpInst::ICMP_UGT: // (X s> 13 & X u> 15) -> no change |
| 4010 | break; |
| 4011 | case ICmpInst::ICMP_NE: |
| 4012 | if (RHSCst == AddOne(LHSCst)) // (X s> 13 & X != 14) -> X s> 14 |
| 4013 | return new ICmpInst(LHSCC, LHSVal, RHSCst); |
| 4014 | break; // (X s> 13 & X != 15) -> no change |
| 4015 | case ICmpInst::ICMP_SLT: // (X s> 13 & X s< 15) ->(X-14) s< 1 |
| 4016 | return InsertRangeTest(LHSVal, AddOne(LHSCst), RHSCst, true, |
| 4017 | true, I); |
| 4018 | case ICmpInst::ICMP_ULT: // (X s> 13 & X u< 15) -> no change |
| 4019 | break; |
| 4020 | } |
| 4021 | break; |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 4022 | } |
| 4023 | } |
| 4024 | } |
| 4025 | |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4026 | // fold (and (cast A), (cast B)) -> (cast (and A, B)) |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4027 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) |
| 4028 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) |
| 4029 | if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind ? |
| 4030 | const Type *SrcTy = Op0C->getOperand(0)->getType(); |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 4031 | if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() && |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4032 | // 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] | 4033 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), |
| 4034 | I.getType(), TD) && |
| 4035 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), |
| 4036 | I.getType(), TD)) { |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4037 | Instruction *NewOp = BinaryOperator::createAnd(Op0C->getOperand(0), |
| 4038 | Op1C->getOperand(0), |
| 4039 | I.getName()); |
| 4040 | InsertNewInstBefore(NewOp, I); |
| 4041 | return CastInst::create(Op0C->getOpcode(), NewOp, I.getType()); |
| 4042 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4043 | } |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4044 | |
| 4045 | // (X >> Z) & (Y >> Z) -> (X&Y) >> Z for all shifts. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4046 | if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) { |
| 4047 | if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0)) |
| 4048 | if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() && |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4049 | SI0->getOperand(1) == SI1->getOperand(1) && |
| 4050 | (SI0->hasOneUse() || SI1->hasOneUse())) { |
| 4051 | Instruction *NewOp = |
| 4052 | InsertNewInstBefore(BinaryOperator::createAnd(SI0->getOperand(0), |
| 4053 | SI1->getOperand(0), |
| 4054 | SI0->getName()), I); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4055 | return BinaryOperator::create(SI1->getOpcode(), NewOp, |
| 4056 | SI1->getOperand(1)); |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4057 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4058 | } |
| 4059 | |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4060 | // (fcmp ord x, c) & (fcmp ord y, c) -> (fcmp ord x, y) |
| 4061 | if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0))) { |
| 4062 | if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1))) { |
| 4063 | if (LHS->getPredicate() == FCmpInst::FCMP_ORD && |
| 4064 | RHS->getPredicate() == FCmpInst::FCMP_ORD) |
| 4065 | if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1))) |
| 4066 | if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) { |
| 4067 | // If either of the constants are nans, then the whole thing returns |
| 4068 | // false. |
Chris Lattner | be3e348 | 2007-10-24 18:54:45 +0000 | [diff] [blame] | 4069 | if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN()) |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4070 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
| 4071 | return new FCmpInst(FCmpInst::FCMP_ORD, LHS->getOperand(0), |
| 4072 | RHS->getOperand(0)); |
| 4073 | } |
| 4074 | } |
| 4075 | } |
| 4076 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4077 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4078 | } |
| 4079 | |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4080 | /// CollectBSwapParts - Look to see if the specified value defines a single byte |
| 4081 | /// in the result. If it does, and if the specified byte hasn't been filled in |
| 4082 | /// yet, fill it in and return false. |
Chris Lattner | 535014f | 2007-02-15 22:52:10 +0000 | [diff] [blame] | 4083 | static bool CollectBSwapParts(Value *V, SmallVector<Value*, 8> &ByteValues) { |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4084 | Instruction *I = dyn_cast<Instruction>(V); |
| 4085 | if (I == 0) return true; |
| 4086 | |
| 4087 | // If this is an or instruction, it is an inner node of the bswap. |
| 4088 | if (I->getOpcode() == Instruction::Or) |
| 4089 | return CollectBSwapParts(I->getOperand(0), ByteValues) || |
| 4090 | CollectBSwapParts(I->getOperand(1), ByteValues); |
| 4091 | |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 4092 | uint32_t BitWidth = I->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4093 | // If this is a shift by a constant int, and it is "24", then its operand |
| 4094 | // defines a byte. We only handle unsigned types here. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4095 | if (I->isShift() && isa<ConstantInt>(I->getOperand(1))) { |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4096 | // Not shifting the entire input by N-1 bytes? |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 4097 | if (cast<ConstantInt>(I->getOperand(1))->getLimitedValue(BitWidth) != |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4098 | 8*(ByteValues.size()-1)) |
| 4099 | return true; |
| 4100 | |
| 4101 | unsigned DestNo; |
| 4102 | if (I->getOpcode() == Instruction::Shl) { |
| 4103 | // X << 24 defines the top byte with the lowest of the input bytes. |
| 4104 | DestNo = ByteValues.size()-1; |
| 4105 | } else { |
| 4106 | // X >>u 24 defines the low byte with the highest of the input bytes. |
| 4107 | DestNo = 0; |
| 4108 | } |
| 4109 | |
| 4110 | // If the destination byte value is already defined, the values are or'd |
| 4111 | // together, which isn't a bswap (unless it's an or of the same bits). |
| 4112 | if (ByteValues[DestNo] && ByteValues[DestNo] != I->getOperand(0)) |
| 4113 | return true; |
| 4114 | ByteValues[DestNo] = I->getOperand(0); |
| 4115 | return false; |
| 4116 | } |
| 4117 | |
| 4118 | // Otherwise, we can only handle and(shift X, imm), imm). Bail out of if we |
| 4119 | // don't have this. |
| 4120 | Value *Shift = 0, *ShiftLHS = 0; |
| 4121 | ConstantInt *AndAmt = 0, *ShiftAmt = 0; |
| 4122 | if (!match(I, m_And(m_Value(Shift), m_ConstantInt(AndAmt))) || |
| 4123 | !match(Shift, m_Shift(m_Value(ShiftLHS), m_ConstantInt(ShiftAmt)))) |
| 4124 | return true; |
| 4125 | Instruction *SI = cast<Instruction>(Shift); |
| 4126 | |
| 4127 | // Make sure that the shift amount is by a multiple of 8 and isn't too big. |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 4128 | if (ShiftAmt->getLimitedValue(BitWidth) & 7 || |
| 4129 | ShiftAmt->getLimitedValue(BitWidth) > 8*ByteValues.size()) |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4130 | return true; |
| 4131 | |
| 4132 | // Turn 0xFF -> 0, 0xFF00 -> 1, 0xFF0000 -> 2, etc. |
| 4133 | unsigned DestByte; |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 4134 | if (AndAmt->getValue().getActiveBits() > 64) |
| 4135 | return true; |
| 4136 | uint64_t AndAmtVal = AndAmt->getZExtValue(); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4137 | for (DestByte = 0; DestByte != ByteValues.size(); ++DestByte) |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 4138 | if (AndAmtVal == uint64_t(0xFF) << 8*DestByte) |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4139 | break; |
| 4140 | // Unknown mask for bswap. |
| 4141 | if (DestByte == ByteValues.size()) return true; |
| 4142 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4143 | unsigned ShiftBytes = ShiftAmt->getZExtValue()/8; |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4144 | unsigned SrcByte; |
| 4145 | if (SI->getOpcode() == Instruction::Shl) |
| 4146 | SrcByte = DestByte - ShiftBytes; |
| 4147 | else |
| 4148 | SrcByte = DestByte + ShiftBytes; |
| 4149 | |
| 4150 | // If the SrcByte isn't a bswapped value from the DestByte, reject it. |
| 4151 | if (SrcByte != ByteValues.size()-DestByte-1) |
| 4152 | return true; |
| 4153 | |
| 4154 | // If the destination byte value is already defined, the values are or'd |
| 4155 | // together, which isn't a bswap (unless it's an or of the same bits). |
| 4156 | if (ByteValues[DestByte] && ByteValues[DestByte] != SI->getOperand(0)) |
| 4157 | return true; |
| 4158 | ByteValues[DestByte] = SI->getOperand(0); |
| 4159 | return false; |
| 4160 | } |
| 4161 | |
| 4162 | /// MatchBSwap - Given an OR instruction, check to see if this is a bswap idiom. |
| 4163 | /// If so, insert the new bswap intrinsic and return it. |
| 4164 | Instruction *InstCombiner::MatchBSwap(BinaryOperator &I) { |
Chris Lattner | 55fc8c4 | 2007-04-01 20:57:36 +0000 | [diff] [blame] | 4165 | const IntegerType *ITy = dyn_cast<IntegerType>(I.getType()); |
| 4166 | if (!ITy || ITy->getBitWidth() % 16) |
| 4167 | return 0; // Can only bswap pairs of bytes. Can't do vectors. |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4168 | |
| 4169 | /// ByteValues - For each byte of the result, we keep track of which value |
| 4170 | /// defines each byte. |
Chris Lattner | 535014f | 2007-02-15 22:52:10 +0000 | [diff] [blame] | 4171 | SmallVector<Value*, 8> ByteValues; |
Chris Lattner | 55fc8c4 | 2007-04-01 20:57:36 +0000 | [diff] [blame] | 4172 | ByteValues.resize(ITy->getBitWidth()/8); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4173 | |
| 4174 | // Try to find all the pieces corresponding to the bswap. |
| 4175 | if (CollectBSwapParts(I.getOperand(0), ByteValues) || |
| 4176 | CollectBSwapParts(I.getOperand(1), ByteValues)) |
| 4177 | return 0; |
| 4178 | |
| 4179 | // Check to see if all of the bytes come from the same value. |
| 4180 | Value *V = ByteValues[0]; |
| 4181 | if (V == 0) return 0; // Didn't find a byte? Must be zero. |
| 4182 | |
| 4183 | // Check to make sure that all of the bytes come from the same value. |
| 4184 | for (unsigned i = 1, e = ByteValues.size(); i != e; ++i) |
| 4185 | if (ByteValues[i] != V) |
| 4186 | return 0; |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 4187 | const Type *Tys[] = { ITy }; |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4188 | Module *M = I.getParent()->getParent()->getParent(); |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 4189 | Function *F = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 4190 | return CallInst::Create(F, V); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4191 | } |
| 4192 | |
| 4193 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4194 | Instruction *InstCombiner::visitOr(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 4195 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4196 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4197 | |
Chris Lattner | 42593e6 | 2007-03-24 23:56:43 +0000 | [diff] [blame] | 4198 | if (isa<UndefValue>(Op1)) // X | undef -> -1 |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4199 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4200 | |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 4201 | // or X, X = X |
| 4202 | if (Op0 == Op1) |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 4203 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4204 | |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 4205 | // See if we can simplify any instructions used by the instruction whose sole |
| 4206 | // purpose is to compute bits we don't care about. |
Chris Lattner | 42593e6 | 2007-03-24 23:56:43 +0000 | [diff] [blame] | 4207 | if (!isa<VectorType>(I.getType())) { |
| 4208 | uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth(); |
| 4209 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 4210 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth), |
| 4211 | KnownZero, KnownOne)) |
| 4212 | return &I; |
Chris Lattner | 041a6c9 | 2007-06-15 05:26:55 +0000 | [diff] [blame] | 4213 | } else if (isa<ConstantAggregateZero>(Op1)) { |
| 4214 | return ReplaceInstUsesWith(I, Op0); // X | <0,0> -> X |
| 4215 | } else if (ConstantVector *CP = dyn_cast<ConstantVector>(Op1)) { |
| 4216 | if (CP->isAllOnesValue()) // X | <-1,-1> -> <-1,-1> |
| 4217 | return ReplaceInstUsesWith(I, I.getOperand(1)); |
Chris Lattner | 42593e6 | 2007-03-24 23:56:43 +0000 | [diff] [blame] | 4218 | } |
Chris Lattner | 041a6c9 | 2007-06-15 05:26:55 +0000 | [diff] [blame] | 4219 | |
| 4220 | |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 4221 | |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4222 | // or X, -1 == -1 |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4223 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 4224 | ConstantInt *C1 = 0; Value *X = 0; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4225 | // (X & C1) | C2 --> (X | C2) & (C1|C2) |
| 4226 | if (match(Op0, m_And(m_Value(X), m_ConstantInt(C1))) && isOnlyUse(Op0)) { |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 4227 | Instruction *Or = BinaryOperator::createOr(X, RHS); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4228 | InsertNewInstBefore(Or, I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 4229 | Or->takeName(Op0); |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 4230 | return BinaryOperator::createAnd(Or, |
| 4231 | ConstantInt::get(RHS->getValue() | C1->getValue())); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4232 | } |
Chris Lattner | ad44ebf | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 4233 | |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4234 | // (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2) |
| 4235 | if (match(Op0, m_Xor(m_Value(X), m_ConstantInt(C1))) && isOnlyUse(Op0)) { |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 4236 | Instruction *Or = BinaryOperator::createOr(X, RHS); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4237 | InsertNewInstBefore(Or, I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 4238 | Or->takeName(Op0); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4239 | return BinaryOperator::createXor(Or, |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 4240 | ConstantInt::get(C1->getValue() & ~RHS->getValue())); |
Chris Lattner | ad44ebf | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 4241 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 4242 | |
| 4243 | // Try to fold constant and into select arguments. |
| 4244 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 4245 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 4246 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 4247 | if (isa<PHINode>(Op0)) |
| 4248 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 4249 | return NV; |
Chris Lattner | ad44ebf | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 4250 | } |
| 4251 | |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 4252 | Value *A = 0, *B = 0; |
| 4253 | ConstantInt *C1 = 0, *C2 = 0; |
Chris Lattner | f4d4c87 | 2005-05-07 23:49:08 +0000 | [diff] [blame] | 4254 | |
| 4255 | if (match(Op0, m_And(m_Value(A), m_Value(B)))) |
| 4256 | if (A == Op1 || B == Op1) // (A & ?) | A --> A |
| 4257 | return ReplaceInstUsesWith(I, Op1); |
| 4258 | if (match(Op1, m_And(m_Value(A), m_Value(B)))) |
| 4259 | if (A == Op0 || B == Op0) // A | (A & ?) --> A |
| 4260 | return ReplaceInstUsesWith(I, Op0); |
| 4261 | |
Chris Lattner | 6423d4c | 2006-07-10 20:25:24 +0000 | [diff] [blame] | 4262 | // (A | B) | C and A | (B | C) -> bswap if possible. |
| 4263 | // (A >> B) | (C << D) and (A << B) | (B >> C) -> bswap if possible. |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4264 | if (match(Op0, m_Or(m_Value(), m_Value())) || |
Chris Lattner | 6423d4c | 2006-07-10 20:25:24 +0000 | [diff] [blame] | 4265 | match(Op1, m_Or(m_Value(), m_Value())) || |
| 4266 | (match(Op0, m_Shift(m_Value(), m_Value())) && |
| 4267 | match(Op1, m_Shift(m_Value(), m_Value())))) { |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4268 | if (Instruction *BSwap = MatchBSwap(I)) |
| 4269 | return BSwap; |
| 4270 | } |
| 4271 | |
Chris Lattner | 6e4c649 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 4272 | // (X^C)|Y -> (X|Y)^C iff Y&C == 0 |
| 4273 | 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] | 4274 | MaskedValueIsZero(Op1, C1->getValue())) { |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 4275 | Instruction *NOr = BinaryOperator::createOr(A, Op1); |
| 4276 | InsertNewInstBefore(NOr, I); |
| 4277 | NOr->takeName(Op0); |
| 4278 | return BinaryOperator::createXor(NOr, C1); |
Chris Lattner | 6e4c649 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 4279 | } |
| 4280 | |
| 4281 | // Y|(X^C) -> (X|Y)^C iff Y&C == 0 |
| 4282 | 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] | 4283 | MaskedValueIsZero(Op0, C1->getValue())) { |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 4284 | Instruction *NOr = BinaryOperator::createOr(A, Op0); |
| 4285 | InsertNewInstBefore(NOr, I); |
| 4286 | NOr->takeName(Op0); |
| 4287 | return BinaryOperator::createXor(NOr, C1); |
Chris Lattner | 6e4c649 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 4288 | } |
| 4289 | |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 4290 | // (A & C)|(B & D) |
Chris Lattner | 2384d7b | 2007-06-19 05:43:49 +0000 | [diff] [blame] | 4291 | Value *C = 0, *D = 0; |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 4292 | if (match(Op0, m_And(m_Value(A), m_Value(C))) && |
| 4293 | match(Op1, m_And(m_Value(B), m_Value(D)))) { |
Chris Lattner | 6cae0e0 | 2007-04-08 07:55:22 +0000 | [diff] [blame] | 4294 | Value *V1 = 0, *V2 = 0, *V3 = 0; |
| 4295 | C1 = dyn_cast<ConstantInt>(C); |
| 4296 | C2 = dyn_cast<ConstantInt>(D); |
| 4297 | if (C1 && C2) { // (A & C1)|(B & C2) |
| 4298 | // If we have: ((V + N) & C1) | (V & C2) |
| 4299 | // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0 |
| 4300 | // replace with V+N. |
| 4301 | if (C1->getValue() == ~C2->getValue()) { |
| 4302 | if ((C2->getValue() & (C2->getValue()+1)) == 0 && // C2 == 0+1+ |
| 4303 | match(A, m_Add(m_Value(V1), m_Value(V2)))) { |
| 4304 | // Add commutes, try both ways. |
| 4305 | if (V1 == B && MaskedValueIsZero(V2, C2->getValue())) |
| 4306 | return ReplaceInstUsesWith(I, A); |
| 4307 | if (V2 == B && MaskedValueIsZero(V1, C2->getValue())) |
| 4308 | return ReplaceInstUsesWith(I, A); |
| 4309 | } |
| 4310 | // Or commutes, try both ways. |
| 4311 | if ((C1->getValue() & (C1->getValue()+1)) == 0 && |
| 4312 | match(B, m_Add(m_Value(V1), m_Value(V2)))) { |
| 4313 | // Add commutes, try both ways. |
| 4314 | if (V1 == A && MaskedValueIsZero(V2, C1->getValue())) |
| 4315 | return ReplaceInstUsesWith(I, B); |
| 4316 | if (V2 == A && MaskedValueIsZero(V1, C1->getValue())) |
| 4317 | return ReplaceInstUsesWith(I, B); |
| 4318 | } |
| 4319 | } |
Chris Lattner | 044e533 | 2007-04-08 08:01:49 +0000 | [diff] [blame] | 4320 | V1 = 0; V2 = 0; V3 = 0; |
Chris Lattner | 6cae0e0 | 2007-04-08 07:55:22 +0000 | [diff] [blame] | 4321 | } |
| 4322 | |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 4323 | // Check to see if we have any common things being and'ed. If so, find the |
| 4324 | // terms for V1 & (V2|V3). |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 4325 | if (isOnlyUse(Op0) || isOnlyUse(Op1)) { |
| 4326 | if (A == B) // (A & C)|(A & D) == A & (C|D) |
| 4327 | V1 = A, V2 = C, V3 = D; |
| 4328 | else if (A == D) // (A & C)|(B & A) == A & (B|C) |
| 4329 | V1 = A, V2 = B, V3 = C; |
| 4330 | else if (C == B) // (A & C)|(C & D) == C & (A|D) |
| 4331 | V1 = C, V2 = A, V3 = D; |
| 4332 | else if (C == D) // (A & C)|(B & C) == C & (A|B) |
| 4333 | V1 = C, V2 = A, V3 = B; |
| 4334 | |
| 4335 | if (V1) { |
| 4336 | Value *Or = |
| 4337 | InsertNewInstBefore(BinaryOperator::createOr(V2, V3, "tmp"), I); |
| 4338 | return BinaryOperator::createAnd(V1, Or); |
Chris Lattner | 0b7c0bf | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 4339 | } |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 4340 | } |
Chris Lattner | e9bed7d | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 4341 | } |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4342 | |
| 4343 | // (X >> Z) | (Y >> Z) -> (X|Y) >> Z for all shifts. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4344 | if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) { |
| 4345 | if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0)) |
| 4346 | if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() && |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4347 | SI0->getOperand(1) == SI1->getOperand(1) && |
| 4348 | (SI0->hasOneUse() || SI1->hasOneUse())) { |
| 4349 | Instruction *NewOp = |
| 4350 | InsertNewInstBefore(BinaryOperator::createOr(SI0->getOperand(0), |
| 4351 | SI1->getOperand(0), |
| 4352 | SI0->getName()), I); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4353 | return BinaryOperator::create(SI1->getOpcode(), NewOp, |
| 4354 | SI1->getOperand(1)); |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4355 | } |
| 4356 | } |
Chris Lattner | 67ca768 | 2003-08-12 19:11:07 +0000 | [diff] [blame] | 4357 | |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4358 | if (match(Op0, m_Not(m_Value(A)))) { // ~A | Op1 |
| 4359 | if (A == Op1) // ~A | A == -1 |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4360 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4361 | } else { |
| 4362 | A = 0; |
| 4363 | } |
Chris Lattner | f4d4c87 | 2005-05-07 23:49:08 +0000 | [diff] [blame] | 4364 | // Note, A is still live here! |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4365 | if (match(Op1, m_Not(m_Value(B)))) { // Op0 | ~B |
| 4366 | if (Op0 == B) |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4367 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | a27231a | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 4368 | |
Misha Brukman | cb6267b | 2004-07-30 12:50:08 +0000 | [diff] [blame] | 4369 | // (~A | ~B) == (~(A & B)) - De Morgan's Law |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4370 | if (A && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
| 4371 | Value *And = InsertNewInstBefore(BinaryOperator::createAnd(A, B, |
| 4372 | I.getName()+".demorgan"), I); |
| 4373 | return BinaryOperator::createNot(And); |
| 4374 | } |
Chris Lattner | a27231a | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 4375 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4376 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4377 | // (icmp1 A, B) | (icmp2 A, B) --> (icmp3 A, B) |
| 4378 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1))) { |
| 4379 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS))) |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 4380 | return R; |
| 4381 | |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4382 | Value *LHSVal, *RHSVal; |
| 4383 | ConstantInt *LHSCst, *RHSCst; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4384 | ICmpInst::Predicate LHSCC, RHSCC; |
| 4385 | if (match(Op0, m_ICmp(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst)))) |
| 4386 | if (match(RHS, m_ICmp(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst)))) |
| 4387 | if (LHSVal == RHSVal && // Found (X icmp C1) | (X icmp C2) |
| 4388 | // icmp [us][gl]e x, cst is folded to icmp [us][gl]t elsewhere. |
| 4389 | LHSCC != ICmpInst::ICMP_UGE && LHSCC != ICmpInst::ICMP_ULE && |
| 4390 | RHSCC != ICmpInst::ICMP_UGE && RHSCC != ICmpInst::ICMP_ULE && |
| 4391 | LHSCC != ICmpInst::ICMP_SGE && LHSCC != ICmpInst::ICMP_SLE && |
Chris Lattner | 8885887 | 2007-05-11 05:55:56 +0000 | [diff] [blame] | 4392 | RHSCC != ICmpInst::ICMP_SGE && RHSCC != ICmpInst::ICMP_SLE && |
| 4393 | // We can't fold (ugt x, C) | (sgt x, C2). |
| 4394 | PredicatesFoldable(LHSCC, RHSCC)) { |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4395 | // Ensure that the larger constant is on the RHS. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4396 | ICmpInst *LHS = cast<ICmpInst>(Op0); |
Chris Lattner | 8885887 | 2007-05-11 05:55:56 +0000 | [diff] [blame] | 4397 | bool NeedsSwap; |
| 4398 | if (ICmpInst::isSignedPredicate(LHSCC)) |
Chris Lattner | 3aea1bd | 2007-05-11 16:58:45 +0000 | [diff] [blame] | 4399 | NeedsSwap = LHSCst->getValue().sgt(RHSCst->getValue()); |
Chris Lattner | 8885887 | 2007-05-11 05:55:56 +0000 | [diff] [blame] | 4400 | else |
Chris Lattner | 3aea1bd | 2007-05-11 16:58:45 +0000 | [diff] [blame] | 4401 | NeedsSwap = LHSCst->getValue().ugt(RHSCst->getValue()); |
Chris Lattner | 8885887 | 2007-05-11 05:55:56 +0000 | [diff] [blame] | 4402 | |
| 4403 | if (NeedsSwap) { |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4404 | std::swap(LHS, RHS); |
| 4405 | std::swap(LHSCst, RHSCst); |
| 4406 | std::swap(LHSCC, RHSCC); |
| 4407 | } |
| 4408 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4409 | // At this point, we know we have have two icmp instructions |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4410 | // comparing a value against two constants and or'ing the result |
| 4411 | // together. Because of the above check, we know that we only have |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4412 | // ICMP_EQ, ICMP_NE, ICMP_LT, and ICMP_GT here. We also know (from the |
| 4413 | // FoldICmpLogical check above), that the two constants are not |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4414 | // equal. |
| 4415 | assert(LHSCst != RHSCst && "Compares not folded above?"); |
| 4416 | |
| 4417 | switch (LHSCC) { |
| 4418 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4419 | case ICmpInst::ICMP_EQ: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4420 | switch (RHSCC) { |
| 4421 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4422 | case ICmpInst::ICMP_EQ: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4423 | if (LHSCst == SubOne(RHSCst)) {// (X == 13 | X == 14) -> X-13 <u 2 |
| 4424 | Constant *AddCST = ConstantExpr::getNeg(LHSCst); |
| 4425 | Instruction *Add = BinaryOperator::createAdd(LHSVal, AddCST, |
| 4426 | LHSVal->getName()+".off"); |
| 4427 | InsertNewInstBefore(Add, I); |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 4428 | AddCST = Subtract(AddOne(RHSCst), LHSCst); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4429 | return new ICmpInst(ICmpInst::ICMP_ULT, Add, AddCST); |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4430 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4431 | break; // (X == 13 | X == 15) -> no change |
| 4432 | case ICmpInst::ICMP_UGT: // (X == 13 | X u> 14) -> no change |
| 4433 | case ICmpInst::ICMP_SGT: // (X == 13 | X s> 14) -> no change |
Chris Lattner | 240d6f4 | 2005-04-19 06:04:18 +0000 | [diff] [blame] | 4434 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4435 | case ICmpInst::ICMP_NE: // (X == 13 | X != 15) -> X != 15 |
| 4436 | case ICmpInst::ICMP_ULT: // (X == 13 | X u< 15) -> X u< 15 |
| 4437 | case ICmpInst::ICMP_SLT: // (X == 13 | X s< 15) -> X s< 15 |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4438 | return ReplaceInstUsesWith(I, RHS); |
| 4439 | } |
| 4440 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4441 | case ICmpInst::ICMP_NE: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4442 | switch (RHSCC) { |
| 4443 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4444 | case ICmpInst::ICMP_EQ: // (X != 13 | X == 15) -> X != 13 |
| 4445 | case ICmpInst::ICMP_UGT: // (X != 13 | X u> 15) -> X != 13 |
| 4446 | case ICmpInst::ICMP_SGT: // (X != 13 | X s> 15) -> X != 13 |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4447 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4448 | case ICmpInst::ICMP_NE: // (X != 13 | X != 15) -> true |
| 4449 | case ICmpInst::ICMP_ULT: // (X != 13 | X u< 15) -> true |
| 4450 | case ICmpInst::ICMP_SLT: // (X != 13 | X s< 15) -> true |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4451 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4452 | } |
| 4453 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4454 | case ICmpInst::ICMP_ULT: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4455 | switch (RHSCC) { |
| 4456 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4457 | case ICmpInst::ICMP_EQ: // (X u< 13 | X == 14) -> no change |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4458 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4459 | case ICmpInst::ICMP_UGT: // (X u< 13 | X u> 15) ->(X-13) u> 2 |
Chris Lattner | 74e012a | 2007-11-01 02:18:41 +0000 | [diff] [blame] | 4460 | // If RHSCst is [us]MAXINT, it is always false. Not handling |
| 4461 | // this can cause overflow. |
| 4462 | if (RHSCst->isMaxValue(false)) |
| 4463 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4464 | return InsertRangeTest(LHSVal, LHSCst, AddOne(RHSCst), false, |
| 4465 | false, I); |
| 4466 | case ICmpInst::ICMP_SGT: // (X u< 13 | X s> 15) -> no change |
| 4467 | break; |
| 4468 | case ICmpInst::ICMP_NE: // (X u< 13 | X != 15) -> X != 15 |
| 4469 | case ICmpInst::ICMP_ULT: // (X u< 13 | X u< 15) -> X u< 15 |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4470 | return ReplaceInstUsesWith(I, RHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4471 | case ICmpInst::ICMP_SLT: // (X u< 13 | X s< 15) -> no change |
| 4472 | break; |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4473 | } |
| 4474 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4475 | case ICmpInst::ICMP_SLT: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4476 | switch (RHSCC) { |
| 4477 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4478 | case ICmpInst::ICMP_EQ: // (X s< 13 | X == 14) -> no change |
| 4479 | break; |
| 4480 | case ICmpInst::ICMP_SGT: // (X s< 13 | X s> 15) ->(X-13) s> 2 |
Chris Lattner | 74e012a | 2007-11-01 02:18:41 +0000 | [diff] [blame] | 4481 | // If RHSCst is [us]MAXINT, it is always false. Not handling |
| 4482 | // this can cause overflow. |
| 4483 | if (RHSCst->isMaxValue(true)) |
| 4484 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4485 | return InsertRangeTest(LHSVal, LHSCst, AddOne(RHSCst), true, |
| 4486 | false, I); |
| 4487 | case ICmpInst::ICMP_UGT: // (X s< 13 | X u> 15) -> no change |
| 4488 | break; |
| 4489 | case ICmpInst::ICMP_NE: // (X s< 13 | X != 15) -> X != 15 |
| 4490 | case ICmpInst::ICMP_SLT: // (X s< 13 | X s< 15) -> X s< 15 |
| 4491 | return ReplaceInstUsesWith(I, RHS); |
| 4492 | case ICmpInst::ICMP_ULT: // (X s< 13 | X u< 15) -> no change |
| 4493 | break; |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4494 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4495 | break; |
| 4496 | case ICmpInst::ICMP_UGT: |
| 4497 | switch (RHSCC) { |
| 4498 | default: assert(0 && "Unknown integer condition code!"); |
| 4499 | case ICmpInst::ICMP_EQ: // (X u> 13 | X == 15) -> X u> 13 |
| 4500 | case ICmpInst::ICMP_UGT: // (X u> 13 | X u> 15) -> X u> 13 |
| 4501 | return ReplaceInstUsesWith(I, LHS); |
| 4502 | case ICmpInst::ICMP_SGT: // (X u> 13 | X s> 15) -> no change |
| 4503 | break; |
| 4504 | case ICmpInst::ICMP_NE: // (X u> 13 | X != 15) -> true |
| 4505 | case ICmpInst::ICMP_ULT: // (X u> 13 | X u< 15) -> true |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4506 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4507 | case ICmpInst::ICMP_SLT: // (X u> 13 | X s< 15) -> no change |
| 4508 | break; |
| 4509 | } |
| 4510 | break; |
| 4511 | case ICmpInst::ICMP_SGT: |
| 4512 | switch (RHSCC) { |
| 4513 | default: assert(0 && "Unknown integer condition code!"); |
| 4514 | case ICmpInst::ICMP_EQ: // (X s> 13 | X == 15) -> X > 13 |
| 4515 | case ICmpInst::ICMP_SGT: // (X s> 13 | X s> 15) -> X > 13 |
| 4516 | return ReplaceInstUsesWith(I, LHS); |
| 4517 | case ICmpInst::ICMP_UGT: // (X s> 13 | X u> 15) -> no change |
| 4518 | break; |
| 4519 | case ICmpInst::ICMP_NE: // (X s> 13 | X != 15) -> true |
| 4520 | case ICmpInst::ICMP_SLT: // (X s> 13 | X s< 15) -> true |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4521 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4522 | case ICmpInst::ICMP_ULT: // (X s> 13 | X u< 15) -> no change |
| 4523 | break; |
| 4524 | } |
| 4525 | break; |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4526 | } |
| 4527 | } |
| 4528 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4529 | |
| 4530 | // fold (or (cast A), (cast B)) -> (cast (or A, B)) |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4531 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) { |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4532 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4533 | if (Op0C->getOpcode() == Op1C->getOpcode()) {// same cast kind ? |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 4534 | if (!isa<ICmpInst>(Op0C->getOperand(0)) || |
| 4535 | !isa<ICmpInst>(Op1C->getOperand(0))) { |
| 4536 | const Type *SrcTy = Op0C->getOperand(0)->getType(); |
| 4537 | if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() && |
| 4538 | // Only do this if the casts both really cause code to be |
| 4539 | // generated. |
| 4540 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), |
| 4541 | I.getType(), TD) && |
| 4542 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), |
| 4543 | I.getType(), TD)) { |
| 4544 | Instruction *NewOp = BinaryOperator::createOr(Op0C->getOperand(0), |
| 4545 | Op1C->getOperand(0), |
| 4546 | I.getName()); |
| 4547 | InsertNewInstBefore(NewOp, I); |
| 4548 | return CastInst::create(Op0C->getOpcode(), NewOp, I.getType()); |
| 4549 | } |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4550 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4551 | } |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4552 | } |
| 4553 | |
| 4554 | |
| 4555 | // (fcmp uno x, c) | (fcmp uno y, c) -> (fcmp uno x, y) |
| 4556 | if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0))) { |
| 4557 | if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1))) { |
| 4558 | if (LHS->getPredicate() == FCmpInst::FCMP_UNO && |
Chris Lattner | 5ebd936 | 2008-02-29 06:09:11 +0000 | [diff] [blame] | 4559 | RHS->getPredicate() == FCmpInst::FCMP_UNO && |
| 4560 | LHS->getOperand(0)->getType() == RHS->getOperand(0)->getType()) |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4561 | if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1))) |
| 4562 | if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) { |
| 4563 | // If either of the constants are nans, then the whole thing returns |
| 4564 | // true. |
Chris Lattner | be3e348 | 2007-10-24 18:54:45 +0000 | [diff] [blame] | 4565 | if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN()) |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4566 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
| 4567 | |
| 4568 | // Otherwise, no need to compare the two constants, compare the |
| 4569 | // rest. |
| 4570 | return new FCmpInst(FCmpInst::FCMP_UNO, LHS->getOperand(0), |
| 4571 | RHS->getOperand(0)); |
| 4572 | } |
| 4573 | } |
| 4574 | } |
Chris Lattner | e9bed7d | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 4575 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4576 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4577 | } |
| 4578 | |
Chris Lattner | c317d39 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 4579 | // XorSelf - Implements: X ^ X --> 0 |
| 4580 | struct XorSelf { |
| 4581 | Value *RHS; |
| 4582 | XorSelf(Value *rhs) : RHS(rhs) {} |
| 4583 | bool shouldApply(Value *LHS) const { return LHS == RHS; } |
| 4584 | Instruction *apply(BinaryOperator &Xor) const { |
| 4585 | return &Xor; |
| 4586 | } |
| 4587 | }; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4588 | |
| 4589 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4590 | Instruction *InstCombiner::visitXor(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 4591 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4592 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4593 | |
Evan Cheng | d34af78 | 2008-03-25 20:07:13 +0000 | [diff] [blame] | 4594 | if (isa<UndefValue>(Op1)) { |
| 4595 | if (isa<UndefValue>(Op0)) |
| 4596 | // Handle undef ^ undef -> 0 special case. This is a common |
| 4597 | // idiom (misuse). |
| 4598 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4599 | return ReplaceInstUsesWith(I, Op1); // X ^ undef -> undef |
Evan Cheng | d34af78 | 2008-03-25 20:07:13 +0000 | [diff] [blame] | 4600 | } |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4601 | |
Chris Lattner | c317d39 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 4602 | // xor X, X = 0, even if X is nested in a sequence of Xor's. |
| 4603 | if (Instruction *Result = AssociativeOpt(I, XorSelf(Op1))) { |
Chris Lattner | a9ff5eb | 2007-08-05 08:47:58 +0000 | [diff] [blame] | 4604 | assert(Result == &I && "AssociativeOpt didn't work?"); Result=Result; |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 4605 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | c317d39 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 4606 | } |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 4607 | |
| 4608 | // See if we can simplify any instructions used by the instruction whose sole |
| 4609 | // purpose is to compute bits we don't care about. |
Reid Spencer | a03d45f | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 4610 | if (!isa<VectorType>(I.getType())) { |
| 4611 | uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth(); |
| 4612 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 4613 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth), |
| 4614 | KnownZero, KnownOne)) |
| 4615 | return &I; |
Chris Lattner | 041a6c9 | 2007-06-15 05:26:55 +0000 | [diff] [blame] | 4616 | } else if (isa<ConstantAggregateZero>(Op1)) { |
| 4617 | return ReplaceInstUsesWith(I, Op0); // X ^ <0,0> -> X |
Reid Spencer | a03d45f | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 4618 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4619 | |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4620 | // Is this a ~ operation? |
| 4621 | if (Value *NotOp = dyn_castNotVal(&I)) { |
| 4622 | // ~(~X & Y) --> (X | ~Y) - De Morgan's Law |
| 4623 | // ~(~X | Y) === (X & ~Y) - De Morgan's Law |
| 4624 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(NotOp)) { |
| 4625 | if (Op0I->getOpcode() == Instruction::And || |
| 4626 | Op0I->getOpcode() == Instruction::Or) { |
| 4627 | if (dyn_castNotVal(Op0I->getOperand(1))) Op0I->swapOperands(); |
| 4628 | if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0))) { |
| 4629 | Instruction *NotY = |
| 4630 | BinaryOperator::createNot(Op0I->getOperand(1), |
| 4631 | Op0I->getOperand(1)->getName()+".not"); |
| 4632 | InsertNewInstBefore(NotY, I); |
| 4633 | if (Op0I->getOpcode() == Instruction::And) |
| 4634 | return BinaryOperator::createOr(Op0NotVal, NotY); |
| 4635 | else |
| 4636 | return BinaryOperator::createAnd(Op0NotVal, NotY); |
| 4637 | } |
| 4638 | } |
| 4639 | } |
| 4640 | } |
| 4641 | |
| 4642 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4643 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Nick Lewycky | f947b3e | 2007-08-06 20:04:16 +0000 | [diff] [blame] | 4644 | // xor (cmp A, B), true = not (cmp A, B) = !cmp A, B |
| 4645 | if (RHS == ConstantInt::getTrue() && Op0->hasOneUse()) { |
| 4646 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(Op0)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4647 | return new ICmpInst(ICI->getInversePredicate(), |
| 4648 | ICI->getOperand(0), ICI->getOperand(1)); |
Chris Lattner | ad5b4fb | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 4649 | |
Nick Lewycky | f947b3e | 2007-08-06 20:04:16 +0000 | [diff] [blame] | 4650 | if (FCmpInst *FCI = dyn_cast<FCmpInst>(Op0)) |
| 4651 | return new FCmpInst(FCI->getInversePredicate(), |
| 4652 | FCI->getOperand(0), FCI->getOperand(1)); |
| 4653 | } |
| 4654 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4655 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) { |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 4656 | // ~(c-X) == X-c-1 == X+(-c-1) |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4657 | if (Op0I->getOpcode() == Instruction::Sub && RHS->isAllOnesValue()) |
| 4658 | if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4659 | Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C); |
| 4660 | Constant *ConstantRHS = ConstantExpr::getSub(NegOp0I0C, |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4661 | ConstantInt::get(I.getType(), 1)); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4662 | return BinaryOperator::createAdd(Op0I->getOperand(1), ConstantRHS); |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4663 | } |
Chris Lattner | 5c6e2db | 2007-04-02 05:36:22 +0000 | [diff] [blame] | 4664 | |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 4665 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) { |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 4666 | if (Op0I->getOpcode() == Instruction::Add) { |
Chris Lattner | 689d24b | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 4667 | // ~(X-c) --> (-c-1)-X |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4668 | if (RHS->isAllOnesValue()) { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4669 | Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI); |
| 4670 | return BinaryOperator::createSub( |
| 4671 | ConstantExpr::getSub(NegOp0CI, |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4672 | ConstantInt::get(I.getType(), 1)), |
Chris Lattner | 689d24b | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 4673 | Op0I->getOperand(0)); |
Chris Lattner | acf4e07 | 2007-04-02 05:42:22 +0000 | [diff] [blame] | 4674 | } else if (RHS->getValue().isSignBit()) { |
Chris Lattner | 5c6e2db | 2007-04-02 05:36:22 +0000 | [diff] [blame] | 4675 | // (X + C) ^ signbit -> (X + C + signbit) |
| 4676 | Constant *C = ConstantInt::get(RHS->getValue() + Op0CI->getValue()); |
| 4677 | return BinaryOperator::createAdd(Op0I->getOperand(0), C); |
Chris Lattner | cd1d6d5 | 2007-04-02 05:48:58 +0000 | [diff] [blame] | 4678 | |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4679 | } |
Chris Lattner | 02bd1b3 | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 4680 | } else if (Op0I->getOpcode() == Instruction::Or) { |
| 4681 | // (X|C1)^C2 -> X^(C1|C2) iff X&~C1 == 0 |
Reid Spencer | a03d45f | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 4682 | if (MaskedValueIsZero(Op0I->getOperand(0), Op0CI->getValue())) { |
Chris Lattner | 02bd1b3 | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 4683 | Constant *NewRHS = ConstantExpr::getOr(Op0CI, RHS); |
| 4684 | // Anything in both C1 and C2 is known to be zero, remove it from |
| 4685 | // NewRHS. |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 4686 | Constant *CommonBits = And(Op0CI, RHS); |
Chris Lattner | 02bd1b3 | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 4687 | NewRHS = ConstantExpr::getAnd(NewRHS, |
| 4688 | ConstantExpr::getNot(CommonBits)); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 4689 | AddToWorkList(Op0I); |
Chris Lattner | 02bd1b3 | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 4690 | I.setOperand(0, Op0I->getOperand(0)); |
| 4691 | I.setOperand(1, NewRHS); |
| 4692 | return &I; |
| 4693 | } |
Chris Lattner | eca0c5c | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 4694 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 4695 | } |
Chris Lattner | 05bd1b2 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 4696 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 4697 | |
| 4698 | // Try to fold constant and into select arguments. |
| 4699 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 4700 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 4701 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 4702 | if (isa<PHINode>(Op0)) |
| 4703 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 4704 | return NV; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4705 | } |
| 4706 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 4707 | if (Value *X = dyn_castNotVal(Op0)) // ~A ^ A == -1 |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4708 | if (X == Op1) |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4709 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4710 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 4711 | if (Value *X = dyn_castNotVal(Op1)) // A ^ ~A == -1 |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4712 | if (X == Op0) |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4713 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4714 | |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4715 | |
| 4716 | BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1); |
| 4717 | if (Op1I) { |
| 4718 | Value *A, *B; |
| 4719 | if (match(Op1I, m_Or(m_Value(A), m_Value(B)))) { |
| 4720 | if (A == Op0) { // B^(B|A) == (A|B)^B |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4721 | Op1I->swapOperands(); |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4722 | I.swapOperands(); |
| 4723 | std::swap(Op0, Op1); |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4724 | } else if (B == Op0) { // B^(A|B) == (A|B)^B |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4725 | I.swapOperands(); // Simplified below. |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4726 | std::swap(Op0, Op1); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4727 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4728 | } else if (match(Op1I, m_Xor(m_Value(A), m_Value(B)))) { |
| 4729 | if (Op0 == A) // A^(A^B) == B |
| 4730 | return ReplaceInstUsesWith(I, B); |
| 4731 | else if (Op0 == B) // A^(B^A) == B |
| 4732 | return ReplaceInstUsesWith(I, A); |
| 4733 | } 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] | 4734 | if (A == Op0) { // A^(A&B) -> A^(B&A) |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4735 | Op1I->swapOperands(); |
Chris Lattner | 6abbdf9 | 2007-04-01 05:36:37 +0000 | [diff] [blame] | 4736 | std::swap(A, B); |
| 4737 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4738 | if (B == Op0) { // A^(B&A) -> (B&A)^A |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4739 | I.swapOperands(); // Simplified below. |
| 4740 | std::swap(Op0, Op1); |
| 4741 | } |
Chris Lattner | 26ca7e1 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 4742 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4743 | } |
| 4744 | |
| 4745 | BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0); |
| 4746 | if (Op0I) { |
| 4747 | Value *A, *B; |
| 4748 | if (match(Op0I, m_Or(m_Value(A), m_Value(B))) && Op0I->hasOneUse()) { |
| 4749 | if (A == Op1) // (B|A)^B == (A|B)^B |
| 4750 | std::swap(A, B); |
| 4751 | if (B == Op1) { // (A|B)^B == A & ~B |
| 4752 | Instruction *NotB = |
| 4753 | InsertNewInstBefore(BinaryOperator::createNot(Op1, "tmp"), I); |
| 4754 | return BinaryOperator::createAnd(A, NotB); |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4755 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4756 | } else if (match(Op0I, m_Xor(m_Value(A), m_Value(B)))) { |
| 4757 | if (Op1 == A) // (A^B)^A == B |
| 4758 | return ReplaceInstUsesWith(I, B); |
| 4759 | else if (Op1 == B) // (B^A)^A == B |
| 4760 | return ReplaceInstUsesWith(I, A); |
| 4761 | } else if (match(Op0I, m_And(m_Value(A), m_Value(B))) && Op0I->hasOneUse()){ |
| 4762 | if (A == Op1) // (A&B)^A -> (B&A)^A |
| 4763 | std::swap(A, B); |
| 4764 | if (B == Op1 && // (B&A)^A == ~B & A |
Chris Lattner | ae1ab39 | 2006-04-01 22:05:01 +0000 | [diff] [blame] | 4765 | !isa<ConstantInt>(Op1)) { // Canonical form is (B&C)^C |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4766 | Instruction *N = |
| 4767 | InsertNewInstBefore(BinaryOperator::createNot(A, "tmp"), I); |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4768 | return BinaryOperator::createAnd(N, Op1); |
| 4769 | } |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4770 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4771 | } |
| 4772 | |
| 4773 | // (X >> Z) ^ (Y >> Z) -> (X^Y) >> Z for all shifts. |
| 4774 | if (Op0I && Op1I && Op0I->isShift() && |
| 4775 | Op0I->getOpcode() == Op1I->getOpcode() && |
| 4776 | Op0I->getOperand(1) == Op1I->getOperand(1) && |
| 4777 | (Op1I->hasOneUse() || Op1I->hasOneUse())) { |
| 4778 | Instruction *NewOp = |
| 4779 | InsertNewInstBefore(BinaryOperator::createXor(Op0I->getOperand(0), |
| 4780 | Op1I->getOperand(0), |
| 4781 | Op0I->getName()), I); |
| 4782 | return BinaryOperator::create(Op1I->getOpcode(), NewOp, |
| 4783 | Op1I->getOperand(1)); |
| 4784 | } |
| 4785 | |
| 4786 | if (Op0I && Op1I) { |
| 4787 | Value *A, *B, *C, *D; |
| 4788 | // (A & B)^(A | B) -> A ^ B |
| 4789 | if (match(Op0I, m_And(m_Value(A), m_Value(B))) && |
| 4790 | match(Op1I, m_Or(m_Value(C), m_Value(D)))) { |
| 4791 | if ((A == C && B == D) || (A == D && B == C)) |
| 4792 | return BinaryOperator::createXor(A, B); |
| 4793 | } |
| 4794 | // (A | B)^(A & B) -> A ^ B |
| 4795 | if (match(Op0I, m_Or(m_Value(A), m_Value(B))) && |
| 4796 | match(Op1I, m_And(m_Value(C), m_Value(D)))) { |
| 4797 | if ((A == C && B == D) || (A == D && B == C)) |
| 4798 | return BinaryOperator::createXor(A, B); |
| 4799 | } |
| 4800 | |
| 4801 | // (A & B)^(C & D) |
| 4802 | if ((Op0I->hasOneUse() || Op1I->hasOneUse()) && |
| 4803 | match(Op0I, m_And(m_Value(A), m_Value(B))) && |
| 4804 | match(Op1I, m_And(m_Value(C), m_Value(D)))) { |
| 4805 | // (X & Y)^(X & Y) -> (Y^Z) & X |
| 4806 | Value *X = 0, *Y = 0, *Z = 0; |
| 4807 | if (A == C) |
| 4808 | X = A, Y = B, Z = D; |
| 4809 | else if (A == D) |
| 4810 | X = A, Y = B, Z = C; |
| 4811 | else if (B == C) |
| 4812 | X = B, Y = A, Z = D; |
| 4813 | else if (B == D) |
| 4814 | X = B, Y = A, Z = C; |
| 4815 | |
| 4816 | if (X) { |
| 4817 | Instruction *NewOp = |
| 4818 | InsertNewInstBefore(BinaryOperator::createXor(Y, Z, Op0->getName()), I); |
| 4819 | return BinaryOperator::createAnd(NewOp, X); |
| 4820 | } |
| 4821 | } |
| 4822 | } |
| 4823 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4824 | // (icmp1 A, B) ^ (icmp2 A, B) --> (icmp3 A, B) |
| 4825 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1))) |
| 4826 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS))) |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 4827 | return R; |
| 4828 | |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4829 | // fold (xor (cast A), (cast B)) -> (cast (xor A, B)) |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4830 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) { |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4831 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4832 | if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind? |
| 4833 | const Type *SrcTy = Op0C->getOperand(0)->getType(); |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 4834 | if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() && |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4835 | // 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] | 4836 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), |
| 4837 | I.getType(), TD) && |
| 4838 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), |
| 4839 | I.getType(), TD)) { |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4840 | Instruction *NewOp = BinaryOperator::createXor(Op0C->getOperand(0), |
| 4841 | Op1C->getOperand(0), |
| 4842 | I.getName()); |
| 4843 | InsertNewInstBefore(NewOp, I); |
| 4844 | return CastInst::create(Op0C->getOpcode(), NewOp, I.getType()); |
| 4845 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4846 | } |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4847 | } |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4848 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4849 | } |
| 4850 | |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4851 | /// AddWithOverflow - Compute Result = In1+In2, returning true if the result |
| 4852 | /// overflowed for this type. |
| 4853 | static bool AddWithOverflow(ConstantInt *&Result, ConstantInt *In1, |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 4854 | ConstantInt *In2, bool IsSigned = false) { |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 4855 | Result = cast<ConstantInt>(Add(In1, In2)); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4856 | |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 4857 | if (IsSigned) |
| 4858 | if (In2->getValue().isNegative()) |
| 4859 | return Result->getValue().sgt(In1->getValue()); |
| 4860 | else |
| 4861 | return Result->getValue().slt(In1->getValue()); |
| 4862 | else |
| 4863 | return Result->getValue().ult(In1->getValue()); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4864 | } |
| 4865 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4866 | /// EmitGEPOffset - Given a getelementptr instruction/constantexpr, emit the |
| 4867 | /// code necessary to compute the offset from the base pointer (without adding |
| 4868 | /// in the base pointer). Return the result as a signed integer of intptr size. |
| 4869 | static Value *EmitGEPOffset(User *GEP, Instruction &I, InstCombiner &IC) { |
| 4870 | TargetData &TD = IC.getTargetData(); |
| 4871 | gep_type_iterator GTI = gep_type_begin(GEP); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4872 | const Type *IntPtrTy = TD.getIntPtrType(); |
| 4873 | Value *Result = Constant::getNullValue(IntPtrTy); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4874 | |
| 4875 | // Build a mask for high order bits. |
Chris Lattner | 10c0d91 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 4876 | unsigned IntPtrWidth = TD.getPointerSizeInBits(); |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4877 | uint64_t PtrSizeMask = ~0ULL >> (64-IntPtrWidth); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4878 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4879 | for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i, ++GTI) { |
| 4880 | Value *Op = GEP->getOperand(i); |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 4881 | uint64_t Size = TD.getABITypeSize(GTI.getIndexedType()) & PtrSizeMask; |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4882 | if (ConstantInt *OpC = dyn_cast<ConstantInt>(Op)) { |
| 4883 | if (OpC->isZero()) continue; |
| 4884 | |
| 4885 | // Handle a struct index, which adds its field offset to the pointer. |
| 4886 | if (const StructType *STy = dyn_cast<StructType>(*GTI)) { |
| 4887 | Size = TD.getStructLayout(STy)->getElementOffset(OpC->getZExtValue()); |
| 4888 | |
| 4889 | if (ConstantInt *RC = dyn_cast<ConstantInt>(Result)) |
| 4890 | Result = ConstantInt::get(RC->getValue() + APInt(IntPtrWidth, Size)); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 4891 | else |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4892 | Result = IC.InsertNewInstBefore( |
| 4893 | BinaryOperator::createAdd(Result, |
| 4894 | ConstantInt::get(IntPtrTy, Size), |
| 4895 | GEP->getName()+".offs"), I); |
| 4896 | continue; |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 4897 | } |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4898 | |
| 4899 | Constant *Scale = ConstantInt::get(IntPtrTy, Size); |
| 4900 | Constant *OC = ConstantExpr::getIntegerCast(OpC, IntPtrTy, true /*SExt*/); |
| 4901 | Scale = ConstantExpr::getMul(OC, Scale); |
| 4902 | if (Constant *RC = dyn_cast<Constant>(Result)) |
| 4903 | Result = ConstantExpr::getAdd(RC, Scale); |
| 4904 | else { |
| 4905 | // Emit an add instruction. |
| 4906 | Result = IC.InsertNewInstBefore( |
| 4907 | BinaryOperator::createAdd(Result, Scale, |
| 4908 | GEP->getName()+".offs"), I); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 4909 | } |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4910 | continue; |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4911 | } |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4912 | // Convert to correct type. |
| 4913 | if (Op->getType() != IntPtrTy) { |
| 4914 | if (Constant *OpC = dyn_cast<Constant>(Op)) |
| 4915 | Op = ConstantExpr::getSExt(OpC, IntPtrTy); |
| 4916 | else |
| 4917 | Op = IC.InsertNewInstBefore(new SExtInst(Op, IntPtrTy, |
| 4918 | Op->getName()+".c"), I); |
| 4919 | } |
| 4920 | if (Size != 1) { |
| 4921 | Constant *Scale = ConstantInt::get(IntPtrTy, Size); |
| 4922 | if (Constant *OpC = dyn_cast<Constant>(Op)) |
| 4923 | Op = ConstantExpr::getMul(OpC, Scale); |
| 4924 | else // We'll let instcombine(mul) convert this to a shl if possible. |
| 4925 | Op = IC.InsertNewInstBefore(BinaryOperator::createMul(Op, Scale, |
| 4926 | GEP->getName()+".idx"), I); |
| 4927 | } |
| 4928 | |
| 4929 | // Emit an add instruction. |
| 4930 | if (isa<Constant>(Op) && isa<Constant>(Result)) |
| 4931 | Result = ConstantExpr::getAdd(cast<Constant>(Op), |
| 4932 | cast<Constant>(Result)); |
| 4933 | else |
| 4934 | Result = IC.InsertNewInstBefore(BinaryOperator::createAdd(Op, Result, |
| 4935 | GEP->getName()+".offs"), I); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4936 | } |
| 4937 | return Result; |
| 4938 | } |
| 4939 | |
Chris Lattner | 10c0d91 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 4940 | |
| 4941 | /// EvaluateGEPOffsetExpression - Return an value that can be used to compare of |
| 4942 | /// the *offset* implied by GEP to zero. For example, if we have &A[i], we want |
| 4943 | /// to return 'i' for "icmp ne i, 0". Note that, in general, indices can be |
| 4944 | /// complex, and scales are involved. The above expression would also be legal |
| 4945 | /// to codegen as "icmp ne (i*4), 0" (assuming A is a pointer to i32). This |
| 4946 | /// later form is less amenable to optimization though, and we are allowed to |
| 4947 | /// generate the first by knowing that pointer arithmetic doesn't overflow. |
| 4948 | /// |
| 4949 | /// If we can't emit an optimized form for this expression, this returns null. |
| 4950 | /// |
| 4951 | static Value *EvaluateGEPOffsetExpression(User *GEP, Instruction &I, |
| 4952 | InstCombiner &IC) { |
Chris Lattner | 10c0d91 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 4953 | TargetData &TD = IC.getTargetData(); |
| 4954 | gep_type_iterator GTI = gep_type_begin(GEP); |
| 4955 | |
| 4956 | // Check to see if this gep only has a single variable index. If so, and if |
| 4957 | // any constant indices are a multiple of its scale, then we can compute this |
| 4958 | // in terms of the scale of the variable index. For example, if the GEP |
| 4959 | // implies an offset of "12 + i*4", then we can codegen this as "3 + i", |
| 4960 | // because the expression will cross zero at the same point. |
| 4961 | unsigned i, e = GEP->getNumOperands(); |
| 4962 | int64_t Offset = 0; |
| 4963 | for (i = 1; i != e; ++i, ++GTI) { |
| 4964 | if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i))) { |
| 4965 | // Compute the aggregate offset of constant indices. |
| 4966 | if (CI->isZero()) continue; |
| 4967 | |
| 4968 | // Handle a struct index, which adds its field offset to the pointer. |
| 4969 | if (const StructType *STy = dyn_cast<StructType>(*GTI)) { |
| 4970 | Offset += TD.getStructLayout(STy)->getElementOffset(CI->getZExtValue()); |
| 4971 | } else { |
| 4972 | uint64_t Size = TD.getABITypeSize(GTI.getIndexedType()); |
| 4973 | Offset += Size*CI->getSExtValue(); |
| 4974 | } |
| 4975 | } else { |
| 4976 | // Found our variable index. |
| 4977 | break; |
| 4978 | } |
| 4979 | } |
| 4980 | |
| 4981 | // If there are no variable indices, we must have a constant offset, just |
| 4982 | // evaluate it the general way. |
| 4983 | if (i == e) return 0; |
| 4984 | |
| 4985 | Value *VariableIdx = GEP->getOperand(i); |
| 4986 | // Determine the scale factor of the variable element. For example, this is |
| 4987 | // 4 if the variable index is into an array of i32. |
| 4988 | uint64_t VariableScale = TD.getABITypeSize(GTI.getIndexedType()); |
| 4989 | |
| 4990 | // Verify that there are no other variable indices. If so, emit the hard way. |
| 4991 | for (++i, ++GTI; i != e; ++i, ++GTI) { |
| 4992 | ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i)); |
| 4993 | if (!CI) return 0; |
| 4994 | |
| 4995 | // Compute the aggregate offset of constant indices. |
| 4996 | if (CI->isZero()) continue; |
| 4997 | |
| 4998 | // Handle a struct index, which adds its field offset to the pointer. |
| 4999 | if (const StructType *STy = dyn_cast<StructType>(*GTI)) { |
| 5000 | Offset += TD.getStructLayout(STy)->getElementOffset(CI->getZExtValue()); |
| 5001 | } else { |
| 5002 | uint64_t Size = TD.getABITypeSize(GTI.getIndexedType()); |
| 5003 | Offset += Size*CI->getSExtValue(); |
| 5004 | } |
| 5005 | } |
| 5006 | |
| 5007 | // Okay, we know we have a single variable index, which must be a |
| 5008 | // pointer/array/vector index. If there is no offset, life is simple, return |
| 5009 | // the index. |
| 5010 | unsigned IntPtrWidth = TD.getPointerSizeInBits(); |
| 5011 | if (Offset == 0) { |
| 5012 | // Cast to intptrty in case a truncation occurs. If an extension is needed, |
| 5013 | // we don't need to bother extending: the extension won't affect where the |
| 5014 | // computation crosses zero. |
| 5015 | if (VariableIdx->getType()->getPrimitiveSizeInBits() > IntPtrWidth) |
| 5016 | VariableIdx = new TruncInst(VariableIdx, TD.getIntPtrType(), |
| 5017 | VariableIdx->getNameStart(), &I); |
| 5018 | return VariableIdx; |
| 5019 | } |
| 5020 | |
| 5021 | // Otherwise, there is an index. The computation we will do will be modulo |
| 5022 | // the pointer size, so get it. |
| 5023 | uint64_t PtrSizeMask = ~0ULL >> (64-IntPtrWidth); |
| 5024 | |
| 5025 | Offset &= PtrSizeMask; |
| 5026 | VariableScale &= PtrSizeMask; |
| 5027 | |
| 5028 | // To do this transformation, any constant index must be a multiple of the |
| 5029 | // variable scale factor. For example, we can evaluate "12 + 4*i" as "3 + i", |
| 5030 | // but we can't evaluate "10 + 3*i" in terms of i. Check that the offset is a |
| 5031 | // multiple of the variable scale. |
| 5032 | int64_t NewOffs = Offset / (int64_t)VariableScale; |
| 5033 | if (Offset != NewOffs*(int64_t)VariableScale) |
| 5034 | return 0; |
| 5035 | |
| 5036 | // Okay, we can do this evaluation. Start by converting the index to intptr. |
| 5037 | const Type *IntPtrTy = TD.getIntPtrType(); |
| 5038 | if (VariableIdx->getType() != IntPtrTy) |
| 5039 | VariableIdx = CastInst::createIntegerCast(VariableIdx, IntPtrTy, |
| 5040 | true /*SExt*/, |
| 5041 | VariableIdx->getNameStart(), &I); |
| 5042 | Constant *OffsetVal = ConstantInt::get(IntPtrTy, NewOffs); |
| 5043 | return BinaryOperator::createAdd(VariableIdx, OffsetVal, "offset", &I); |
| 5044 | } |
| 5045 | |
| 5046 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5047 | /// FoldGEPICmp - Fold comparisons between a GEP instruction and something |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5048 | /// 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] | 5049 | Instruction *InstCombiner::FoldGEPICmp(User *GEPLHS, Value *RHS, |
| 5050 | ICmpInst::Predicate Cond, |
| 5051 | Instruction &I) { |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5052 | assert(dyn_castGetElementPtr(GEPLHS) && "LHS is not a getelementptr!"); |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 5053 | |
Chris Lattner | 10c0d91 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 5054 | // Look through bitcasts. |
| 5055 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(RHS)) |
| 5056 | RHS = BCI->getOperand(0); |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 5057 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5058 | Value *PtrBase = GEPLHS->getOperand(0); |
| 5059 | if (PtrBase == RHS) { |
Chris Lattner | 7c95deb | 2008-02-05 04:45:32 +0000 | [diff] [blame] | 5060 | // ((gep Ptr, OFFSET) cmp Ptr) ---> (OFFSET cmp 0). |
Chris Lattner | 10c0d91 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 5061 | // This transformation (ignoring the base and scales) is valid because we |
| 5062 | // know pointers can't overflow. See if we can output an optimized form. |
| 5063 | Value *Offset = EvaluateGEPOffsetExpression(GEPLHS, I, *this); |
| 5064 | |
| 5065 | // If not, synthesize the offset the hard way. |
| 5066 | if (Offset == 0) |
| 5067 | Offset = EmitGEPOffset(GEPLHS, I, *this); |
Chris Lattner | 7c95deb | 2008-02-05 04:45:32 +0000 | [diff] [blame] | 5068 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), Offset, |
| 5069 | Constant::getNullValue(Offset->getType())); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5070 | } else if (User *GEPRHS = dyn_castGetElementPtr(RHS)) { |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 5071 | // If the base pointers are different, but the indices are the same, just |
| 5072 | // compare the base pointer. |
| 5073 | if (PtrBase != GEPRHS->getOperand(0)) { |
| 5074 | bool IndicesTheSame = GEPLHS->getNumOperands()==GEPRHS->getNumOperands(); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 5075 | IndicesTheSame &= GEPLHS->getOperand(0)->getType() == |
Chris Lattner | 93b94a6 | 2005-04-26 14:40:41 +0000 | [diff] [blame] | 5076 | GEPRHS->getOperand(0)->getType(); |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 5077 | if (IndicesTheSame) |
| 5078 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i) |
| 5079 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { |
| 5080 | IndicesTheSame = false; |
| 5081 | break; |
| 5082 | } |
| 5083 | |
| 5084 | // If all indices are the same, just compare the base pointers. |
| 5085 | if (IndicesTheSame) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5086 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), |
| 5087 | GEPLHS->getOperand(0), GEPRHS->getOperand(0)); |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 5088 | |
| 5089 | // Otherwise, the base pointers are different and the indices are |
| 5090 | // different, bail out. |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5091 | return 0; |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 5092 | } |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5093 | |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 5094 | // If one of the GEPs has all zero indices, recurse. |
| 5095 | bool AllZeros = true; |
| 5096 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i) |
| 5097 | if (!isa<Constant>(GEPLHS->getOperand(i)) || |
| 5098 | !cast<Constant>(GEPLHS->getOperand(i))->isNullValue()) { |
| 5099 | AllZeros = false; |
| 5100 | break; |
| 5101 | } |
| 5102 | if (AllZeros) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5103 | return FoldGEPICmp(GEPRHS, GEPLHS->getOperand(0), |
| 5104 | ICmpInst::getSwappedPredicate(Cond), I); |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 5105 | |
| 5106 | // If the other GEP has all zero indices, recurse. |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 5107 | AllZeros = true; |
| 5108 | for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i) |
| 5109 | if (!isa<Constant>(GEPRHS->getOperand(i)) || |
| 5110 | !cast<Constant>(GEPRHS->getOperand(i))->isNullValue()) { |
| 5111 | AllZeros = false; |
| 5112 | break; |
| 5113 | } |
| 5114 | if (AllZeros) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5115 | return FoldGEPICmp(GEPLHS, GEPRHS->getOperand(0), Cond, I); |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 5116 | |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 5117 | if (GEPLHS->getNumOperands() == GEPRHS->getNumOperands()) { |
| 5118 | // If the GEPs only differ by one index, compare it. |
| 5119 | unsigned NumDifferences = 0; // Keep track of # differences. |
| 5120 | unsigned DiffOperand = 0; // The operand that differs. |
| 5121 | for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i) |
| 5122 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5123 | if (GEPLHS->getOperand(i)->getType()->getPrimitiveSizeInBits() != |
| 5124 | GEPRHS->getOperand(i)->getType()->getPrimitiveSizeInBits()) { |
Chris Lattner | 45f57b8 | 2005-01-21 23:06:49 +0000 | [diff] [blame] | 5125 | // Irreconcilable differences. |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 5126 | NumDifferences = 2; |
| 5127 | break; |
| 5128 | } else { |
| 5129 | if (NumDifferences++) break; |
| 5130 | DiffOperand = i; |
| 5131 | } |
| 5132 | } |
| 5133 | |
| 5134 | if (NumDifferences == 0) // SAME GEP? |
| 5135 | return ReplaceInstUsesWith(I, // No comparison is needed here. |
Nick Lewycky | 455e176 | 2007-09-06 02:40:25 +0000 | [diff] [blame] | 5136 | ConstantInt::get(Type::Int1Ty, |
| 5137 | isTrueWhenEqual(Cond))); |
| 5138 | |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 5139 | else if (NumDifferences == 1) { |
Chris Lattner | 45f57b8 | 2005-01-21 23:06:49 +0000 | [diff] [blame] | 5140 | Value *LHSV = GEPLHS->getOperand(DiffOperand); |
| 5141 | Value *RHSV = GEPRHS->getOperand(DiffOperand); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5142 | // Make sure we do a signed comparison here. |
| 5143 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), LHSV, RHSV); |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 5144 | } |
| 5145 | } |
| 5146 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5147 | // 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] | 5148 | // the result to fold to a constant! |
| 5149 | if ((isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) && |
| 5150 | (isa<ConstantExpr>(GEPRHS) || GEPRHS->hasOneUse())) { |
| 5151 | // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) ---> (OFFSET1 cmp OFFSET2) |
| 5152 | Value *L = EmitGEPOffset(GEPLHS, I, *this); |
| 5153 | Value *R = EmitGEPOffset(GEPRHS, I, *this); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5154 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), L, R); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5155 | } |
| 5156 | } |
| 5157 | return 0; |
| 5158 | } |
| 5159 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5160 | Instruction *InstCombiner::visitFCmpInst(FCmpInst &I) { |
| 5161 | bool Changed = SimplifyCompare(I); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5162 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5163 | |
Chris Lattner | 58e9746 | 2007-01-14 19:42:17 +0000 | [diff] [blame] | 5164 | // Fold trivial predicates. |
| 5165 | if (I.getPredicate() == FCmpInst::FCMP_FALSE) |
| 5166 | return ReplaceInstUsesWith(I, Constant::getNullValue(Type::Int1Ty)); |
| 5167 | if (I.getPredicate() == FCmpInst::FCMP_TRUE) |
| 5168 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 1)); |
| 5169 | |
| 5170 | // Simplify 'fcmp pred X, X' |
| 5171 | if (Op0 == Op1) { |
| 5172 | switch (I.getPredicate()) { |
| 5173 | default: assert(0 && "Unknown predicate!"); |
| 5174 | case FCmpInst::FCMP_UEQ: // True if unordered or equal |
| 5175 | case FCmpInst::FCMP_UGE: // True if unordered, greater than, or equal |
| 5176 | case FCmpInst::FCMP_ULE: // True if unordered, less than, or equal |
| 5177 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 1)); |
| 5178 | case FCmpInst::FCMP_OGT: // True if ordered and greater than |
| 5179 | case FCmpInst::FCMP_OLT: // True if ordered and less than |
| 5180 | case FCmpInst::FCMP_ONE: // True if ordered and operands are unequal |
| 5181 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 0)); |
| 5182 | |
| 5183 | case FCmpInst::FCMP_UNO: // True if unordered: isnan(X) | isnan(Y) |
| 5184 | case FCmpInst::FCMP_ULT: // True if unordered or less than |
| 5185 | case FCmpInst::FCMP_UGT: // True if unordered or greater than |
| 5186 | case FCmpInst::FCMP_UNE: // True if unordered or not equal |
| 5187 | // Canonicalize these to be 'fcmp uno %X, 0.0'. |
| 5188 | I.setPredicate(FCmpInst::FCMP_UNO); |
| 5189 | I.setOperand(1, Constant::getNullValue(Op0->getType())); |
| 5190 | return &I; |
| 5191 | |
| 5192 | case FCmpInst::FCMP_ORD: // True if ordered (no nans) |
| 5193 | case FCmpInst::FCMP_OEQ: // True if ordered and equal |
| 5194 | case FCmpInst::FCMP_OGE: // True if ordered and greater than or equal |
| 5195 | case FCmpInst::FCMP_OLE: // True if ordered and less than or equal |
| 5196 | // Canonicalize these to be 'fcmp ord %X, 0.0'. |
| 5197 | I.setPredicate(FCmpInst::FCMP_ORD); |
| 5198 | I.setOperand(1, Constant::getNullValue(Op0->getType())); |
| 5199 | return &I; |
| 5200 | } |
| 5201 | } |
| 5202 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5203 | if (isa<UndefValue>(Op1)) // fcmp pred X, undef -> undef |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 5204 | return ReplaceInstUsesWith(I, UndefValue::get(Type::Int1Ty)); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5205 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5206 | // Handle fcmp with constant RHS |
| 5207 | if (Constant *RHSC = dyn_cast<Constant>(Op1)) { |
| 5208 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
| 5209 | switch (LHSI->getOpcode()) { |
| 5210 | case Instruction::PHI: |
| 5211 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 5212 | return NV; |
| 5213 | break; |
| 5214 | case Instruction::Select: |
| 5215 | // If either operand of the select is a constant, we can fold the |
| 5216 | // comparison into the select arms, which will cause one to be |
| 5217 | // constant folded and the select turned into a bitwise or. |
| 5218 | Value *Op1 = 0, *Op2 = 0; |
| 5219 | if (LHSI->hasOneUse()) { |
| 5220 | if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) { |
| 5221 | // Fold the known value into the constant operand. |
| 5222 | Op1 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC); |
| 5223 | // Insert a new FCmp of the other select operand. |
| 5224 | Op2 = InsertNewInstBefore(new FCmpInst(I.getPredicate(), |
| 5225 | LHSI->getOperand(2), RHSC, |
| 5226 | I.getName()), I); |
| 5227 | } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) { |
| 5228 | // Fold the known value into the constant operand. |
| 5229 | Op2 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC); |
| 5230 | // Insert a new FCmp of the other select operand. |
| 5231 | Op1 = InsertNewInstBefore(new FCmpInst(I.getPredicate(), |
| 5232 | LHSI->getOperand(1), RHSC, |
| 5233 | I.getName()), I); |
| 5234 | } |
| 5235 | } |
| 5236 | |
| 5237 | if (Op1) |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 5238 | return SelectInst::Create(LHSI->getOperand(0), Op1, Op2); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5239 | break; |
| 5240 | } |
| 5241 | } |
| 5242 | |
| 5243 | return Changed ? &I : 0; |
| 5244 | } |
| 5245 | |
| 5246 | Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { |
| 5247 | bool Changed = SimplifyCompare(I); |
| 5248 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 5249 | const Type *Ty = Op0->getType(); |
| 5250 | |
| 5251 | // icmp X, X |
| 5252 | if (Op0 == Op1) |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 5253 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
| 5254 | isTrueWhenEqual(I))); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5255 | |
| 5256 | if (isa<UndefValue>(Op1)) // X icmp undef -> undef |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 5257 | return ReplaceInstUsesWith(I, UndefValue::get(Type::Int1Ty)); |
Christopher Lamb | 7a0678c | 2007-12-18 21:32:20 +0000 | [diff] [blame] | 5258 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5259 | // icmp <global/alloca*/null>, <global/alloca*/null> - Global/Stack value |
Chris Lattner | 711b340 | 2004-11-14 07:33:16 +0000 | [diff] [blame] | 5260 | // addresses never equal each other! We already know that Op0 != Op1. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5261 | if ((isa<GlobalValue>(Op0) || isa<AllocaInst>(Op0) || |
| 5262 | isa<ConstantPointerNull>(Op0)) && |
| 5263 | (isa<GlobalValue>(Op1) || isa<AllocaInst>(Op1) || |
Chris Lattner | 711b340 | 2004-11-14 07:33:16 +0000 | [diff] [blame] | 5264 | isa<ConstantPointerNull>(Op1))) |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 5265 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
| 5266 | !isTrueWhenEqual(I))); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5267 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5268 | // icmp's with boolean values can always be turned into bitwise operations |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 5269 | if (Ty == Type::Int1Ty) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5270 | switch (I.getPredicate()) { |
| 5271 | default: assert(0 && "Invalid icmp instruction!"); |
| 5272 | case ICmpInst::ICMP_EQ: { // icmp eq bool %A, %B -> ~(A^B) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 5273 | Instruction *Xor = BinaryOperator::createXor(Op0, Op1, I.getName()+"tmp"); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5274 | InsertNewInstBefore(Xor, I); |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5275 | return BinaryOperator::createNot(Xor); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5276 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5277 | case ICmpInst::ICMP_NE: // icmp eq bool %A, %B -> A^B |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 5278 | return BinaryOperator::createXor(Op0, Op1); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5279 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5280 | case ICmpInst::ICMP_UGT: |
| 5281 | case ICmpInst::ICMP_SGT: |
| 5282 | std::swap(Op0, Op1); // Change icmp gt -> icmp lt |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 5283 | // FALL THROUGH |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5284 | case ICmpInst::ICMP_ULT: |
| 5285 | case ICmpInst::ICMP_SLT: { // icmp lt bool A, B -> ~X & Y |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 5286 | Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp"); |
| 5287 | InsertNewInstBefore(Not, I); |
| 5288 | return BinaryOperator::createAnd(Not, Op1); |
| 5289 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5290 | case ICmpInst::ICMP_UGE: |
| 5291 | case ICmpInst::ICMP_SGE: |
| 5292 | std::swap(Op0, Op1); // Change icmp ge -> icmp le |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 5293 | // FALL THROUGH |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5294 | case ICmpInst::ICMP_ULE: |
| 5295 | case ICmpInst::ICMP_SLE: { // icmp le bool %A, %B -> ~A | B |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 5296 | Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp"); |
| 5297 | InsertNewInstBefore(Not, I); |
| 5298 | return BinaryOperator::createOr(Not, Op1); |
| 5299 | } |
| 5300 | } |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5301 | } |
| 5302 | |
Chris Lattner | 2be51ae | 2004-06-09 04:24:29 +0000 | [diff] [blame] | 5303 | // See if we are doing a comparison between a constant and an instruction that |
| 5304 | // can be folded into the comparison. |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5305 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Christopher Lamb | 103e1a3 | 2007-12-20 07:21:11 +0000 | [diff] [blame] | 5306 | Value *A, *B; |
| 5307 | |
Chris Lattner | b656601 | 2008-01-05 01:18:20 +0000 | [diff] [blame] | 5308 | // (icmp ne/eq (sub A B) 0) -> (icmp ne/eq A, B) |
| 5309 | if (I.isEquality() && CI->isNullValue() && |
| 5310 | match(Op0, m_Sub(m_Value(A), m_Value(B)))) { |
| 5311 | // (icmp cond A B) if cond is equality |
| 5312 | return new ICmpInst(I.getPredicate(), A, B); |
Owen Anderson | f5783f8 | 2007-12-28 07:42:12 +0000 | [diff] [blame] | 5313 | } |
Christopher Lamb | 103e1a3 | 2007-12-20 07:21:11 +0000 | [diff] [blame] | 5314 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5315 | switch (I.getPredicate()) { |
| 5316 | default: break; |
| 5317 | case ICmpInst::ICMP_ULT: // A <u MIN -> FALSE |
| 5318 | if (CI->isMinValue(false)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5319 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5320 | if (CI->isMaxValue(false)) // A <u MAX -> A != MAX |
| 5321 | return new ICmpInst(ICmpInst::ICMP_NE, Op0,Op1); |
| 5322 | if (isMinValuePlusOne(CI,false)) // A <u MIN+1 -> A == MIN |
| 5323 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, SubOne(CI)); |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 5324 | // (x <u 2147483648) -> (x >s -1) -> true if sign bit clear |
| 5325 | if (CI->isMinValue(true)) |
| 5326 | return new ICmpInst(ICmpInst::ICMP_SGT, Op0, |
| 5327 | ConstantInt::getAllOnesValue(Op0->getType())); |
| 5328 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5329 | break; |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 5330 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5331 | case ICmpInst::ICMP_SLT: |
| 5332 | if (CI->isMinValue(true)) // A <s MIN -> FALSE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5333 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5334 | if (CI->isMaxValue(true)) // A <s MAX -> A != MAX |
| 5335 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
| 5336 | if (isMinValuePlusOne(CI,true)) // A <s MIN+1 -> A == MIN |
| 5337 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, SubOne(CI)); |
| 5338 | break; |
| 5339 | |
| 5340 | case ICmpInst::ICMP_UGT: |
| 5341 | if (CI->isMaxValue(false)) // A >u MAX -> FALSE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5342 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5343 | if (CI->isMinValue(false)) // A >u MIN -> A != MIN |
| 5344 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
| 5345 | if (isMaxValueMinusOne(CI, false)) // A >u MAX-1 -> A == MAX |
| 5346 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, AddOne(CI)); |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 5347 | |
| 5348 | // (x >u 2147483647) -> (x <s 0) -> true if sign bit set |
| 5349 | if (CI->isMaxValue(true)) |
| 5350 | return new ICmpInst(ICmpInst::ICMP_SLT, Op0, |
| 5351 | ConstantInt::getNullValue(Op0->getType())); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5352 | break; |
| 5353 | |
| 5354 | case ICmpInst::ICMP_SGT: |
| 5355 | if (CI->isMaxValue(true)) // A >s MAX -> FALSE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5356 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5357 | if (CI->isMinValue(true)) // A >s MIN -> A != MIN |
| 5358 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
| 5359 | if (isMaxValueMinusOne(CI, true)) // A >s MAX-1 -> A == MAX |
| 5360 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, AddOne(CI)); |
| 5361 | break; |
| 5362 | |
| 5363 | case ICmpInst::ICMP_ULE: |
| 5364 | if (CI->isMaxValue(false)) // A <=u MAX -> TRUE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5365 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5366 | if (CI->isMinValue(false)) // A <=u MIN -> A == MIN |
| 5367 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); |
| 5368 | if (isMaxValueMinusOne(CI,false)) // A <=u MAX-1 -> A != MAX |
| 5369 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, AddOne(CI)); |
| 5370 | break; |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 5371 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5372 | case ICmpInst::ICMP_SLE: |
| 5373 | if (CI->isMaxValue(true)) // A <=s MAX -> TRUE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5374 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5375 | if (CI->isMinValue(true)) // A <=s MIN -> A == MIN |
| 5376 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); |
| 5377 | if (isMaxValueMinusOne(CI,true)) // A <=s MAX-1 -> A != MAX |
| 5378 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, AddOne(CI)); |
| 5379 | break; |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 5380 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5381 | case ICmpInst::ICMP_UGE: |
| 5382 | if (CI->isMinValue(false)) // A >=u MIN -> TRUE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5383 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5384 | if (CI->isMaxValue(false)) // A >=u MAX -> A == MAX |
| 5385 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); |
| 5386 | if (isMinValuePlusOne(CI,false)) // A >=u MIN-1 -> A != MIN |
| 5387 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, SubOne(CI)); |
| 5388 | break; |
| 5389 | |
| 5390 | case ICmpInst::ICMP_SGE: |
| 5391 | if (CI->isMinValue(true)) // A >=s MIN -> TRUE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5392 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5393 | if (CI->isMaxValue(true)) // A >=s MAX -> A == MAX |
| 5394 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); |
| 5395 | if (isMinValuePlusOne(CI,true)) // A >=s MIN-1 -> A != MIN |
| 5396 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, SubOne(CI)); |
| 5397 | break; |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 5398 | } |
| 5399 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5400 | // If we still have a icmp le or icmp ge instruction, turn it into the |
| 5401 | // appropriate icmp lt or icmp gt instruction. Since the border cases have |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 5402 | // already been handled above, this requires little checking. |
| 5403 | // |
Reid Spencer | 2149a9d | 2007-03-25 19:55:33 +0000 | [diff] [blame] | 5404 | switch (I.getPredicate()) { |
Chris Lattner | 4241e4d | 2007-07-15 20:54:51 +0000 | [diff] [blame] | 5405 | default: break; |
| 5406 | case ICmpInst::ICMP_ULE: |
| 5407 | return new ICmpInst(ICmpInst::ICMP_ULT, Op0, AddOne(CI)); |
| 5408 | case ICmpInst::ICMP_SLE: |
| 5409 | return new ICmpInst(ICmpInst::ICMP_SLT, Op0, AddOne(CI)); |
| 5410 | case ICmpInst::ICMP_UGE: |
| 5411 | return new ICmpInst( ICmpInst::ICMP_UGT, Op0, SubOne(CI)); |
| 5412 | case ICmpInst::ICMP_SGE: |
| 5413 | return new ICmpInst(ICmpInst::ICMP_SGT, Op0, SubOne(CI)); |
Reid Spencer | 2149a9d | 2007-03-25 19:55:33 +0000 | [diff] [blame] | 5414 | } |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 5415 | |
| 5416 | // See if we can fold the comparison based on bits known to be zero or one |
Chris Lattner | 4241e4d | 2007-07-15 20:54:51 +0000 | [diff] [blame] | 5417 | // in the input. If this comparison is a normal comparison, it demands all |
| 5418 | // bits, if it is a sign bit comparison, it only demands the sign bit. |
| 5419 | |
| 5420 | bool UnusedBit; |
| 5421 | bool isSignBit = isSignBitCheck(I.getPredicate(), CI, UnusedBit); |
| 5422 | |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5423 | uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth(); |
| 5424 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
Chris Lattner | 4241e4d | 2007-07-15 20:54:51 +0000 | [diff] [blame] | 5425 | if (SimplifyDemandedBits(Op0, |
| 5426 | isSignBit ? APInt::getSignBit(BitWidth) |
| 5427 | : APInt::getAllOnesValue(BitWidth), |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 5428 | KnownZero, KnownOne, 0)) |
| 5429 | return &I; |
| 5430 | |
| 5431 | // Given the known and unknown bits, compute a range that the LHS could be |
| 5432 | // in. |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5433 | if ((KnownOne | KnownZero) != 0) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5434 | // Compute the Min, Max and RHS values based on the known bits. For the |
| 5435 | // EQ and NE we use unsigned values. |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 5436 | APInt Min(BitWidth, 0), Max(BitWidth, 0); |
| 5437 | const APInt& RHSVal = CI->getValue(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5438 | if (ICmpInst::isSignedPredicate(I.getPredicate())) { |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5439 | ComputeSignedMinMaxValuesFromKnownBits(Ty, KnownZero, KnownOne, Min, |
| 5440 | Max); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5441 | } else { |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5442 | ComputeUnsignedMinMaxValuesFromKnownBits(Ty, KnownZero, KnownOne, Min, |
| 5443 | Max); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5444 | } |
| 5445 | switch (I.getPredicate()) { // LE/GE have been folded already. |
| 5446 | default: assert(0 && "Unknown icmp opcode!"); |
| 5447 | case ICmpInst::ICMP_EQ: |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5448 | if (Max.ult(RHSVal) || Min.ugt(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5449 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5450 | break; |
| 5451 | case ICmpInst::ICMP_NE: |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5452 | if (Max.ult(RHSVal) || Min.ugt(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5453 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5454 | break; |
| 5455 | case ICmpInst::ICMP_ULT: |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5456 | if (Max.ult(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5457 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | 81973ef | 2007-04-09 23:52:13 +0000 | [diff] [blame] | 5458 | if (Min.uge(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5459 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5460 | break; |
| 5461 | case ICmpInst::ICMP_UGT: |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5462 | if (Min.ugt(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5463 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | 81973ef | 2007-04-09 23:52:13 +0000 | [diff] [blame] | 5464 | if (Max.ule(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5465 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5466 | break; |
| 5467 | case ICmpInst::ICMP_SLT: |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5468 | if (Max.slt(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5469 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5470 | if (Min.sgt(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5471 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5472 | break; |
| 5473 | case ICmpInst::ICMP_SGT: |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5474 | if (Min.sgt(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5475 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | 81973ef | 2007-04-09 23:52:13 +0000 | [diff] [blame] | 5476 | if (Max.sle(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5477 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5478 | break; |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 5479 | } |
| 5480 | } |
| 5481 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5482 | // 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] | 5483 | // instruction, see if that instruction also has constants so that the |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5484 | // instruction can be folded into the icmp |
Chris Lattner | 3c6a0d4 | 2004-05-25 06:32:08 +0000 | [diff] [blame] | 5485 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5486 | if (Instruction *Res = visitICmpInstWithInstAndIntCst(I, LHSI, CI)) |
| 5487 | return Res; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5488 | } |
| 5489 | |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5490 | // Handle icmp with constant (but not simple integer constant) RHS |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5491 | if (Constant *RHSC = dyn_cast<Constant>(Op1)) { |
| 5492 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
| 5493 | switch (LHSI->getOpcode()) { |
Chris Lattner | 9fb25db | 2005-05-01 04:42:15 +0000 | [diff] [blame] | 5494 | case Instruction::GetElementPtr: |
| 5495 | if (RHSC->isNullValue()) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5496 | // 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] | 5497 | bool isAllZeros = true; |
| 5498 | for (unsigned i = 1, e = LHSI->getNumOperands(); i != e; ++i) |
| 5499 | if (!isa<Constant>(LHSI->getOperand(i)) || |
| 5500 | !cast<Constant>(LHSI->getOperand(i))->isNullValue()) { |
| 5501 | isAllZeros = false; |
| 5502 | break; |
| 5503 | } |
| 5504 | if (isAllZeros) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5505 | return new ICmpInst(I.getPredicate(), LHSI->getOperand(0), |
Chris Lattner | 9fb25db | 2005-05-01 04:42:15 +0000 | [diff] [blame] | 5506 | Constant::getNullValue(LHSI->getOperand(0)->getType())); |
| 5507 | } |
| 5508 | break; |
| 5509 | |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5510 | case Instruction::PHI: |
| 5511 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 5512 | return NV; |
| 5513 | break; |
Chris Lattner | 4802d90 | 2007-04-06 18:57:34 +0000 | [diff] [blame] | 5514 | case Instruction::Select: { |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5515 | // If either operand of the select is a constant, we can fold the |
| 5516 | // comparison into the select arms, which will cause one to be |
| 5517 | // constant folded and the select turned into a bitwise or. |
| 5518 | Value *Op1 = 0, *Op2 = 0; |
| 5519 | if (LHSI->hasOneUse()) { |
| 5520 | if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) { |
| 5521 | // Fold the known value into the constant operand. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5522 | Op1 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC); |
| 5523 | // Insert a new ICmp of the other select operand. |
| 5524 | Op2 = InsertNewInstBefore(new ICmpInst(I.getPredicate(), |
| 5525 | LHSI->getOperand(2), RHSC, |
| 5526 | I.getName()), I); |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5527 | } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) { |
| 5528 | // Fold the known value into the constant operand. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5529 | Op2 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC); |
| 5530 | // Insert a new ICmp of the other select operand. |
| 5531 | Op1 = InsertNewInstBefore(new ICmpInst(I.getPredicate(), |
| 5532 | LHSI->getOperand(1), RHSC, |
| 5533 | I.getName()), I); |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5534 | } |
| 5535 | } |
Jeff Cohen | 9d80930 | 2005-04-23 21:38:35 +0000 | [diff] [blame] | 5536 | |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5537 | if (Op1) |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 5538 | return SelectInst::Create(LHSI->getOperand(0), Op1, Op2); |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5539 | break; |
| 5540 | } |
Chris Lattner | 4802d90 | 2007-04-06 18:57:34 +0000 | [diff] [blame] | 5541 | case Instruction::Malloc: |
| 5542 | // If we have (malloc != null), and if the malloc has a single use, we |
| 5543 | // can assume it is successful and remove the malloc. |
| 5544 | if (LHSI->hasOneUse() && isa<ConstantPointerNull>(RHSC)) { |
| 5545 | AddToWorkList(LHSI); |
| 5546 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
| 5547 | !isTrueWhenEqual(I))); |
| 5548 | } |
| 5549 | break; |
| 5550 | } |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5551 | } |
| 5552 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5553 | // 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] | 5554 | if (User *GEP = dyn_castGetElementPtr(Op0)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5555 | if (Instruction *NI = FoldGEPICmp(GEP, Op1, I.getPredicate(), I)) |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5556 | return NI; |
| 5557 | if (User *GEP = dyn_castGetElementPtr(Op1)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5558 | if (Instruction *NI = FoldGEPICmp(GEP, Op0, |
| 5559 | ICmpInst::getSwappedPredicate(I.getPredicate()), I)) |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5560 | return NI; |
| 5561 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5562 | // 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] | 5563 | // values. If the ptr->ptr cast can be stripped off both arguments, we do so |
| 5564 | // now. |
| 5565 | if (BitCastInst *CI = dyn_cast<BitCastInst>(Op0)) { |
| 5566 | if (isa<PointerType>(Op0->getType()) && |
| 5567 | (isa<Constant>(Op1) || isa<BitCastInst>(Op1))) { |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5568 | // We keep moving the cast from the left operand over to the right |
| 5569 | // operand, where it can often be eliminated completely. |
Chris Lattner | 57d8637 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 5570 | Op0 = CI->getOperand(0); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5571 | |
Chris Lattner | 57d8637 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 5572 | // If operand #1 is a bitcast instruction, it must also be a ptr->ptr cast |
| 5573 | // so eliminate it as well. |
| 5574 | if (BitCastInst *CI2 = dyn_cast<BitCastInst>(Op1)) |
| 5575 | Op1 = CI2->getOperand(0); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5576 | |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5577 | // 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] | 5578 | if (Op0->getType() != Op1->getType()) { |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5579 | if (Constant *Op1C = dyn_cast<Constant>(Op1)) { |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 5580 | Op1 = ConstantExpr::getBitCast(Op1C, Op0->getType()); |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5581 | } else { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5582 | // Otherwise, cast the RHS right before the icmp |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 5583 | Op1 = InsertBitCastBefore(Op1, Op0->getType(), I); |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5584 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 5585 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5586 | return new ICmpInst(I.getPredicate(), Op0, Op1); |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5587 | } |
Chris Lattner | 57d8637 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 5588 | } |
| 5589 | |
| 5590 | if (isa<CastInst>(Op0)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5591 | // Handle the special case of: icmp (cast bool to X), <cst> |
Chris Lattner | 6870805 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 5592 | // This comes up when you have code like |
| 5593 | // int X = A < B; |
| 5594 | // if (X) ... |
| 5595 | // For generality, we handle any zero-extension of any operand comparison |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5596 | // with a constant or another cast from the same type. |
| 5597 | if (isa<ConstantInt>(Op1) || isa<CastInst>(Op1)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5598 | if (Instruction *R = visitICmpInstWithCastAndCast(I)) |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5599 | return R; |
Chris Lattner | 6870805 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 5600 | } |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5601 | |
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 5602 | if (I.isEquality()) { |
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5603 | Value *A, *B, *C, *D; |
| 5604 | if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) { |
| 5605 | if (A == Op1 || B == Op1) { // (A^B) == A -> B == 0 |
| 5606 | Value *OtherVal = A == Op1 ? B : A; |
| 5607 | return new ICmpInst(I.getPredicate(), OtherVal, |
| 5608 | Constant::getNullValue(A->getType())); |
| 5609 | } |
| 5610 | |
| 5611 | if (match(Op1, m_Xor(m_Value(C), m_Value(D)))) { |
| 5612 | // A^c1 == C^c2 --> A == C^(c1^c2) |
| 5613 | if (ConstantInt *C1 = dyn_cast<ConstantInt>(B)) |
| 5614 | if (ConstantInt *C2 = dyn_cast<ConstantInt>(D)) |
| 5615 | if (Op1->hasOneUse()) { |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 5616 | Constant *NC = ConstantInt::get(C1->getValue() ^ C2->getValue()); |
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5617 | Instruction *Xor = BinaryOperator::createXor(C, NC, "tmp"); |
| 5618 | return new ICmpInst(I.getPredicate(), A, |
| 5619 | InsertNewInstBefore(Xor, I)); |
| 5620 | } |
| 5621 | |
| 5622 | // A^B == A^D -> B == D |
| 5623 | if (A == C) return new ICmpInst(I.getPredicate(), B, D); |
| 5624 | if (A == D) return new ICmpInst(I.getPredicate(), B, C); |
| 5625 | if (B == C) return new ICmpInst(I.getPredicate(), A, D); |
| 5626 | if (B == D) return new ICmpInst(I.getPredicate(), A, C); |
| 5627 | } |
| 5628 | } |
| 5629 | |
| 5630 | if (match(Op1, m_Xor(m_Value(A), m_Value(B))) && |
| 5631 | (A == Op0 || B == Op0)) { |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5632 | // A == (A^B) -> B == 0 |
| 5633 | Value *OtherVal = A == Op0 ? B : A; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5634 | return new ICmpInst(I.getPredicate(), OtherVal, |
| 5635 | Constant::getNullValue(A->getType())); |
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5636 | } |
| 5637 | if (match(Op0, m_Sub(m_Value(A), m_Value(B))) && A == Op1) { |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5638 | // (A-B) == A -> B == 0 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5639 | return new ICmpInst(I.getPredicate(), B, |
| 5640 | Constant::getNullValue(B->getType())); |
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5641 | } |
| 5642 | if (match(Op1, m_Sub(m_Value(A), m_Value(B))) && A == Op0) { |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5643 | // A == (A-B) -> B == 0 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5644 | return new ICmpInst(I.getPredicate(), B, |
| 5645 | Constant::getNullValue(B->getType())); |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5646 | } |
Chris Lattner | 9c2328e | 2006-11-14 06:06:06 +0000 | [diff] [blame] | 5647 | |
Chris Lattner | 9c2328e | 2006-11-14 06:06:06 +0000 | [diff] [blame] | 5648 | // (X&Z) == (Y&Z) -> (X^Y) & Z == 0 |
| 5649 | if (Op0->hasOneUse() && Op1->hasOneUse() && |
| 5650 | match(Op0, m_And(m_Value(A), m_Value(B))) && |
| 5651 | match(Op1, m_And(m_Value(C), m_Value(D)))) { |
| 5652 | Value *X = 0, *Y = 0, *Z = 0; |
| 5653 | |
| 5654 | if (A == C) { |
| 5655 | X = B; Y = D; Z = A; |
| 5656 | } else if (A == D) { |
| 5657 | X = B; Y = C; Z = A; |
| 5658 | } else if (B == C) { |
| 5659 | X = A; Y = D; Z = B; |
| 5660 | } else if (B == D) { |
| 5661 | X = A; Y = C; Z = B; |
| 5662 | } |
| 5663 | |
| 5664 | if (X) { // Build (X^Y) & Z |
| 5665 | Op1 = InsertNewInstBefore(BinaryOperator::createXor(X, Y, "tmp"), I); |
| 5666 | Op1 = InsertNewInstBefore(BinaryOperator::createAnd(Op1, Z, "tmp"), I); |
| 5667 | I.setOperand(0, Op1); |
| 5668 | I.setOperand(1, Constant::getNullValue(Op1->getType())); |
| 5669 | return &I; |
| 5670 | } |
| 5671 | } |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5672 | } |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 5673 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5674 | } |
| 5675 | |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5676 | |
| 5677 | /// FoldICmpDivCst - Fold "icmp pred, ([su]div X, DivRHS), CmpRHS" where DivRHS |
| 5678 | /// and CmpRHS are both known to be integer constants. |
| 5679 | Instruction *InstCombiner::FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI, |
| 5680 | ConstantInt *DivRHS) { |
| 5681 | ConstantInt *CmpRHS = cast<ConstantInt>(ICI.getOperand(1)); |
| 5682 | const APInt &CmpRHSV = CmpRHS->getValue(); |
| 5683 | |
| 5684 | // FIXME: If the operand types don't match the type of the divide |
| 5685 | // then don't attempt this transform. The code below doesn't have the |
| 5686 | // logic to deal with a signed divide and an unsigned compare (and |
| 5687 | // vice versa). This is because (x /s C1) <s C2 produces different |
| 5688 | // results than (x /s C1) <u C2 or (x /u C1) <s C2 or even |
| 5689 | // (x /u C1) <u C2. Simply casting the operands and result won't |
| 5690 | // work. :( The if statement below tests that condition and bails |
| 5691 | // if it finds it. |
| 5692 | bool DivIsSigned = DivI->getOpcode() == Instruction::SDiv; |
| 5693 | if (!ICI.isEquality() && DivIsSigned != ICI.isSignedPredicate()) |
| 5694 | return 0; |
| 5695 | if (DivRHS->isZero()) |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5696 | return 0; // The ProdOV computation fails on divide by zero. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5697 | |
| 5698 | // Compute Prod = CI * DivRHS. We are essentially solving an equation |
| 5699 | // of form X/C1=C2. We solve for X by multiplying C1 (DivRHS) and |
| 5700 | // C2 (CI). By solving for X we can turn this into a range check |
| 5701 | // instead of computing a divide. |
| 5702 | ConstantInt *Prod = Multiply(CmpRHS, DivRHS); |
| 5703 | |
| 5704 | // Determine if the product overflows by seeing if the product is |
| 5705 | // not equal to the divide. Make sure we do the same kind of divide |
| 5706 | // as in the LHS instruction that we're folding. |
| 5707 | bool ProdOV = (DivIsSigned ? ConstantExpr::getSDiv(Prod, DivRHS) : |
| 5708 | ConstantExpr::getUDiv(Prod, DivRHS)) != CmpRHS; |
| 5709 | |
| 5710 | // Get the ICmp opcode |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5711 | ICmpInst::Predicate Pred = ICI.getPredicate(); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5712 | |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5713 | // Figure out the interval that is being checked. For example, a comparison |
| 5714 | // like "X /u 5 == 0" is really checking that X is in the interval [0, 5). |
| 5715 | // Compute this interval based on the constants involved and the signedness of |
| 5716 | // the compare/divide. This computes a half-open interval, keeping track of |
| 5717 | // whether either value in the interval overflows. After analysis each |
| 5718 | // overflow variable is set to 0 if it's corresponding bound variable is valid |
| 5719 | // -1 if overflowed off the bottom end, or +1 if overflowed off the top end. |
| 5720 | int LoOverflow = 0, HiOverflow = 0; |
| 5721 | ConstantInt *LoBound = 0, *HiBound = 0; |
| 5722 | |
| 5723 | |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5724 | if (!DivIsSigned) { // udiv |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5725 | // e.g. X/5 op 3 --> [15, 20) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5726 | LoBound = Prod; |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5727 | HiOverflow = LoOverflow = ProdOV; |
| 5728 | if (!HiOverflow) |
| 5729 | HiOverflow = AddWithOverflow(HiBound, LoBound, DivRHS, false); |
Dan Gohman | 7649127 | 2008-02-13 22:09:18 +0000 | [diff] [blame] | 5730 | } else if (DivRHS->getValue().isStrictlyPositive()) { // Divisor is > 0. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5731 | if (CmpRHSV == 0) { // (X / pos) op 0 |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5732 | // Can't overflow. e.g. X/2 op 0 --> [-1, 2) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5733 | LoBound = cast<ConstantInt>(ConstantExpr::getNeg(SubOne(DivRHS))); |
| 5734 | HiBound = DivRHS; |
Dan Gohman | 7649127 | 2008-02-13 22:09:18 +0000 | [diff] [blame] | 5735 | } else if (CmpRHSV.isStrictlyPositive()) { // (X / pos) op pos |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5736 | LoBound = Prod; // e.g. X/5 op 3 --> [15, 20) |
| 5737 | HiOverflow = LoOverflow = ProdOV; |
| 5738 | if (!HiOverflow) |
| 5739 | HiOverflow = AddWithOverflow(HiBound, Prod, DivRHS, true); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5740 | } else { // (X / pos) op neg |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5741 | // e.g. X/5 op -3 --> [-15-4, -15+1) --> [-19, -14) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5742 | Constant *DivRHSH = ConstantExpr::getNeg(SubOne(DivRHS)); |
| 5743 | LoOverflow = AddWithOverflow(LoBound, Prod, |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5744 | cast<ConstantInt>(DivRHSH), true) ? -1 : 0; |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5745 | HiBound = AddOne(Prod); |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5746 | HiOverflow = ProdOV ? -1 : 0; |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5747 | } |
Dan Gohman | 7649127 | 2008-02-13 22:09:18 +0000 | [diff] [blame] | 5748 | } else if (DivRHS->getValue().isNegative()) { // Divisor is < 0. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5749 | if (CmpRHSV == 0) { // (X / neg) op 0 |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5750 | // e.g. X/-5 op 0 --> [-4, 5) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5751 | LoBound = AddOne(DivRHS); |
| 5752 | HiBound = cast<ConstantInt>(ConstantExpr::getNeg(DivRHS)); |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5753 | if (HiBound == DivRHS) { // -INTMIN = INTMIN |
| 5754 | HiOverflow = 1; // [INTMIN+1, overflow) |
| 5755 | HiBound = 0; // e.g. X/INTMIN = 0 --> X > INTMIN |
| 5756 | } |
Dan Gohman | 7649127 | 2008-02-13 22:09:18 +0000 | [diff] [blame] | 5757 | } else if (CmpRHSV.isStrictlyPositive()) { // (X / neg) op pos |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5758 | // e.g. X/-5 op 3 --> [-19, -14) |
| 5759 | HiOverflow = LoOverflow = ProdOV ? -1 : 0; |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5760 | if (!LoOverflow) |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5761 | LoOverflow = AddWithOverflow(LoBound, Prod, AddOne(DivRHS), true) ?-1:0; |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5762 | HiBound = AddOne(Prod); |
| 5763 | } else { // (X / neg) op neg |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5764 | // e.g. X/-5 op -3 --> [15, 20) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5765 | LoBound = Prod; |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5766 | LoOverflow = HiOverflow = ProdOV ? 1 : 0; |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5767 | HiBound = Subtract(Prod, DivRHS); |
| 5768 | } |
| 5769 | |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5770 | // Dividing by a negative swaps the condition. LT <-> GT |
| 5771 | Pred = ICmpInst::getSwappedPredicate(Pred); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5772 | } |
| 5773 | |
| 5774 | Value *X = DivI->getOperand(0); |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5775 | switch (Pred) { |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5776 | default: assert(0 && "Unhandled icmp opcode!"); |
| 5777 | case ICmpInst::ICMP_EQ: |
| 5778 | if (LoOverflow && HiOverflow) |
| 5779 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
| 5780 | else if (HiOverflow) |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5781 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5782 | ICmpInst::ICMP_UGE, X, LoBound); |
| 5783 | else if (LoOverflow) |
| 5784 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : |
| 5785 | ICmpInst::ICMP_ULT, X, HiBound); |
| 5786 | else |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5787 | return InsertRangeTest(X, LoBound, HiBound, DivIsSigned, true, ICI); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5788 | case ICmpInst::ICMP_NE: |
| 5789 | if (LoOverflow && HiOverflow) |
| 5790 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); |
| 5791 | else if (HiOverflow) |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5792 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5793 | ICmpInst::ICMP_ULT, X, LoBound); |
| 5794 | else if (LoOverflow) |
| 5795 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : |
| 5796 | ICmpInst::ICMP_UGE, X, HiBound); |
| 5797 | else |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5798 | return InsertRangeTest(X, LoBound, HiBound, DivIsSigned, false, ICI); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5799 | case ICmpInst::ICMP_ULT: |
| 5800 | case ICmpInst::ICMP_SLT: |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5801 | if (LoOverflow == +1) // Low bound is greater than input range. |
| 5802 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); |
| 5803 | if (LoOverflow == -1) // Low bound is less than input range. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5804 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5805 | return new ICmpInst(Pred, X, LoBound); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5806 | case ICmpInst::ICMP_UGT: |
| 5807 | case ICmpInst::ICMP_SGT: |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5808 | if (HiOverflow == +1) // High bound greater than input range. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5809 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5810 | else if (HiOverflow == -1) // High bound less than input range. |
| 5811 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); |
| 5812 | if (Pred == ICmpInst::ICMP_UGT) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5813 | return new ICmpInst(ICmpInst::ICMP_UGE, X, HiBound); |
| 5814 | else |
| 5815 | return new ICmpInst(ICmpInst::ICMP_SGE, X, HiBound); |
| 5816 | } |
| 5817 | } |
| 5818 | |
| 5819 | |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5820 | /// visitICmpInstWithInstAndIntCst - Handle "icmp (instr, intcst)". |
| 5821 | /// |
| 5822 | Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI, |
| 5823 | Instruction *LHSI, |
| 5824 | ConstantInt *RHS) { |
| 5825 | const APInt &RHSV = RHS->getValue(); |
| 5826 | |
| 5827 | switch (LHSI->getOpcode()) { |
Duncan Sands | 0091bf2 | 2007-04-04 06:42:45 +0000 | [diff] [blame] | 5828 | case Instruction::Xor: // (icmp pred (xor X, XorCST), CI) |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5829 | if (ConstantInt *XorCST = dyn_cast<ConstantInt>(LHSI->getOperand(1))) { |
| 5830 | // If this is a comparison that tests the signbit (X < 0) or (x > -1), |
| 5831 | // fold the xor. |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 5832 | if ((ICI.getPredicate() == ICmpInst::ICMP_SLT && RHSV == 0) || |
| 5833 | (ICI.getPredicate() == ICmpInst::ICMP_SGT && RHSV.isAllOnesValue())) { |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5834 | Value *CompareVal = LHSI->getOperand(0); |
| 5835 | |
| 5836 | // If the sign bit of the XorCST is not set, there is no change to |
| 5837 | // the operation, just stop using the Xor. |
| 5838 | if (!XorCST->getValue().isNegative()) { |
| 5839 | ICI.setOperand(0, CompareVal); |
| 5840 | AddToWorkList(LHSI); |
| 5841 | return &ICI; |
| 5842 | } |
| 5843 | |
| 5844 | // Was the old condition true if the operand is positive? |
| 5845 | bool isTrueIfPositive = ICI.getPredicate() == ICmpInst::ICMP_SGT; |
| 5846 | |
| 5847 | // If so, the new one isn't. |
| 5848 | isTrueIfPositive ^= true; |
| 5849 | |
| 5850 | if (isTrueIfPositive) |
| 5851 | return new ICmpInst(ICmpInst::ICMP_SGT, CompareVal, SubOne(RHS)); |
| 5852 | else |
| 5853 | return new ICmpInst(ICmpInst::ICMP_SLT, CompareVal, AddOne(RHS)); |
| 5854 | } |
| 5855 | } |
| 5856 | break; |
| 5857 | case Instruction::And: // (icmp pred (and X, AndCST), RHS) |
| 5858 | if (LHSI->hasOneUse() && isa<ConstantInt>(LHSI->getOperand(1)) && |
| 5859 | LHSI->getOperand(0)->hasOneUse()) { |
| 5860 | ConstantInt *AndCST = cast<ConstantInt>(LHSI->getOperand(1)); |
| 5861 | |
| 5862 | // If the LHS is an AND of a truncating cast, we can widen the |
| 5863 | // and/compare to be the input width without changing the value |
| 5864 | // produced, eliminating a cast. |
| 5865 | if (TruncInst *Cast = dyn_cast<TruncInst>(LHSI->getOperand(0))) { |
| 5866 | // We can do this transformation if either the AND constant does not |
| 5867 | // have its sign bit set or if it is an equality comparison. |
| 5868 | // Extending a relational comparison when we're checking the sign |
| 5869 | // bit would not work. |
| 5870 | if (Cast->hasOneUse() && |
Anton Korobeynikov | 4aefd6b | 2008-02-20 12:07:57 +0000 | [diff] [blame] | 5871 | (ICI.isEquality() || |
| 5872 | (AndCST->getValue().isNonNegative() && RHSV.isNonNegative()))) { |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5873 | uint32_t BitWidth = |
| 5874 | cast<IntegerType>(Cast->getOperand(0)->getType())->getBitWidth(); |
| 5875 | APInt NewCST = AndCST->getValue(); |
| 5876 | NewCST.zext(BitWidth); |
| 5877 | APInt NewCI = RHSV; |
| 5878 | NewCI.zext(BitWidth); |
| 5879 | Instruction *NewAnd = |
| 5880 | BinaryOperator::createAnd(Cast->getOperand(0), |
| 5881 | ConstantInt::get(NewCST),LHSI->getName()); |
| 5882 | InsertNewInstBefore(NewAnd, ICI); |
| 5883 | return new ICmpInst(ICI.getPredicate(), NewAnd, |
| 5884 | ConstantInt::get(NewCI)); |
| 5885 | } |
| 5886 | } |
| 5887 | |
| 5888 | // If this is: (X >> C1) & C2 != C3 (where any shift and any compare |
| 5889 | // could exist), turn it into (X & (C2 << C1)) != (C3 << C1). This |
| 5890 | // happens a LOT in code produced by the C front-end, for bitfield |
| 5891 | // access. |
| 5892 | BinaryOperator *Shift = dyn_cast<BinaryOperator>(LHSI->getOperand(0)); |
| 5893 | if (Shift && !Shift->isShift()) |
| 5894 | Shift = 0; |
| 5895 | |
| 5896 | ConstantInt *ShAmt; |
| 5897 | ShAmt = Shift ? dyn_cast<ConstantInt>(Shift->getOperand(1)) : 0; |
| 5898 | const Type *Ty = Shift ? Shift->getType() : 0; // Type of the shift. |
| 5899 | const Type *AndTy = AndCST->getType(); // Type of the and. |
| 5900 | |
| 5901 | // We can fold this as long as we can't shift unknown bits |
| 5902 | // into the mask. This can only happen with signed shift |
| 5903 | // rights, as they sign-extend. |
| 5904 | if (ShAmt) { |
| 5905 | bool CanFold = Shift->isLogicalShift(); |
| 5906 | if (!CanFold) { |
| 5907 | // To test for the bad case of the signed shr, see if any |
| 5908 | // of the bits shifted in could be tested after the mask. |
| 5909 | uint32_t TyBits = Ty->getPrimitiveSizeInBits(); |
| 5910 | int ShAmtVal = TyBits - ShAmt->getLimitedValue(TyBits); |
| 5911 | |
| 5912 | uint32_t BitWidth = AndTy->getPrimitiveSizeInBits(); |
| 5913 | if ((APInt::getHighBitsSet(BitWidth, BitWidth-ShAmtVal) & |
| 5914 | AndCST->getValue()) == 0) |
| 5915 | CanFold = true; |
| 5916 | } |
| 5917 | |
| 5918 | if (CanFold) { |
| 5919 | Constant *NewCst; |
| 5920 | if (Shift->getOpcode() == Instruction::Shl) |
| 5921 | NewCst = ConstantExpr::getLShr(RHS, ShAmt); |
| 5922 | else |
| 5923 | NewCst = ConstantExpr::getShl(RHS, ShAmt); |
| 5924 | |
| 5925 | // Check to see if we are shifting out any of the bits being |
| 5926 | // compared. |
| 5927 | if (ConstantExpr::get(Shift->getOpcode(), NewCst, ShAmt) != RHS) { |
| 5928 | // If we shifted bits out, the fold is not going to work out. |
| 5929 | // As a special case, check to see if this means that the |
| 5930 | // result is always true or false now. |
| 5931 | if (ICI.getPredicate() == ICmpInst::ICMP_EQ) |
| 5932 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
| 5933 | if (ICI.getPredicate() == ICmpInst::ICMP_NE) |
| 5934 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); |
| 5935 | } else { |
| 5936 | ICI.setOperand(1, NewCst); |
| 5937 | Constant *NewAndCST; |
| 5938 | if (Shift->getOpcode() == Instruction::Shl) |
| 5939 | NewAndCST = ConstantExpr::getLShr(AndCST, ShAmt); |
| 5940 | else |
| 5941 | NewAndCST = ConstantExpr::getShl(AndCST, ShAmt); |
| 5942 | LHSI->setOperand(1, NewAndCST); |
| 5943 | LHSI->setOperand(0, Shift->getOperand(0)); |
| 5944 | AddToWorkList(Shift); // Shift is dead. |
| 5945 | AddUsesToWorkList(ICI); |
| 5946 | return &ICI; |
| 5947 | } |
| 5948 | } |
| 5949 | } |
| 5950 | |
| 5951 | // Turn ((X >> Y) & C) == 0 into (X & (C << Y)) == 0. The later is |
| 5952 | // preferable because it allows the C<<Y expression to be hoisted out |
| 5953 | // of a loop if Y is invariant and X is not. |
| 5954 | if (Shift && Shift->hasOneUse() && RHSV == 0 && |
| 5955 | ICI.isEquality() && !Shift->isArithmeticShift() && |
| 5956 | isa<Instruction>(Shift->getOperand(0))) { |
| 5957 | // Compute C << Y. |
| 5958 | Value *NS; |
| 5959 | if (Shift->getOpcode() == Instruction::LShr) { |
| 5960 | NS = BinaryOperator::createShl(AndCST, |
| 5961 | Shift->getOperand(1), "tmp"); |
| 5962 | } else { |
| 5963 | // Insert a logical shift. |
| 5964 | NS = BinaryOperator::createLShr(AndCST, |
| 5965 | Shift->getOperand(1), "tmp"); |
| 5966 | } |
| 5967 | InsertNewInstBefore(cast<Instruction>(NS), ICI); |
| 5968 | |
| 5969 | // Compute X & (C << Y). |
| 5970 | Instruction *NewAnd = |
| 5971 | BinaryOperator::createAnd(Shift->getOperand(0), NS, LHSI->getName()); |
| 5972 | InsertNewInstBefore(NewAnd, ICI); |
| 5973 | |
| 5974 | ICI.setOperand(0, NewAnd); |
| 5975 | return &ICI; |
| 5976 | } |
| 5977 | } |
| 5978 | break; |
| 5979 | |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 5980 | case Instruction::Shl: { // (icmp pred (shl X, ShAmt), CI) |
| 5981 | ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1)); |
| 5982 | if (!ShAmt) break; |
| 5983 | |
| 5984 | uint32_t TypeBits = RHSV.getBitWidth(); |
| 5985 | |
| 5986 | // Check that the shift amount is in range. If not, don't perform |
| 5987 | // undefined shifts. When the shift is visited it will be |
| 5988 | // simplified. |
| 5989 | if (ShAmt->uge(TypeBits)) |
| 5990 | break; |
| 5991 | |
| 5992 | if (ICI.isEquality()) { |
| 5993 | // If we are comparing against bits always shifted out, the |
| 5994 | // comparison cannot succeed. |
| 5995 | Constant *Comp = |
| 5996 | ConstantExpr::getShl(ConstantExpr::getLShr(RHS, ShAmt), ShAmt); |
| 5997 | if (Comp != RHS) {// Comparing against a bit that we know is zero. |
| 5998 | bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE; |
| 5999 | Constant *Cst = ConstantInt::get(Type::Int1Ty, IsICMP_NE); |
| 6000 | return ReplaceInstUsesWith(ICI, Cst); |
| 6001 | } |
| 6002 | |
| 6003 | if (LHSI->hasOneUse()) { |
| 6004 | // Otherwise strength reduce the shift into an and. |
| 6005 | uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits); |
| 6006 | Constant *Mask = |
| 6007 | ConstantInt::get(APInt::getLowBitsSet(TypeBits, TypeBits-ShAmtVal)); |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6008 | |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6009 | Instruction *AndI = |
| 6010 | BinaryOperator::createAnd(LHSI->getOperand(0), |
| 6011 | Mask, LHSI->getName()+".mask"); |
| 6012 | Value *And = InsertNewInstBefore(AndI, ICI); |
| 6013 | return new ICmpInst(ICI.getPredicate(), And, |
| 6014 | ConstantInt::get(RHSV.lshr(ShAmtVal))); |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6015 | } |
| 6016 | } |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6017 | |
| 6018 | // Otherwise, if this is a comparison of the sign bit, simplify to and/test. |
| 6019 | bool TrueIfSigned = false; |
| 6020 | if (LHSI->hasOneUse() && |
| 6021 | isSignBitCheck(ICI.getPredicate(), RHS, TrueIfSigned)) { |
| 6022 | // (X << 31) <s 0 --> (X&1) != 0 |
| 6023 | Constant *Mask = ConstantInt::get(APInt(TypeBits, 1) << |
| 6024 | (TypeBits-ShAmt->getZExtValue()-1)); |
| 6025 | Instruction *AndI = |
| 6026 | BinaryOperator::createAnd(LHSI->getOperand(0), |
| 6027 | Mask, LHSI->getName()+".mask"); |
| 6028 | Value *And = InsertNewInstBefore(AndI, ICI); |
| 6029 | |
| 6030 | return new ICmpInst(TrueIfSigned ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ, |
| 6031 | And, Constant::getNullValue(And->getType())); |
| 6032 | } |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6033 | break; |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6034 | } |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6035 | |
| 6036 | case Instruction::LShr: // (icmp pred (shr X, ShAmt), CI) |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6037 | case Instruction::AShr: { |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 6038 | // Only handle equality comparisons of shift-by-constant. |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6039 | ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1)); |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 6040 | if (!ShAmt || !ICI.isEquality()) break; |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6041 | |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 6042 | // Check that the shift amount is in range. If not, don't perform |
| 6043 | // undefined shifts. When the shift is visited it will be |
| 6044 | // simplified. |
| 6045 | uint32_t TypeBits = RHSV.getBitWidth(); |
| 6046 | if (ShAmt->uge(TypeBits)) |
| 6047 | break; |
| 6048 | |
| 6049 | uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits); |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6050 | |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 6051 | // If we are comparing against bits always shifted out, the |
| 6052 | // comparison cannot succeed. |
| 6053 | APInt Comp = RHSV << ShAmtVal; |
| 6054 | if (LHSI->getOpcode() == Instruction::LShr) |
| 6055 | Comp = Comp.lshr(ShAmtVal); |
| 6056 | else |
| 6057 | Comp = Comp.ashr(ShAmtVal); |
| 6058 | |
| 6059 | if (Comp != RHSV) { // Comparing against a bit that we know is zero. |
| 6060 | bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE; |
| 6061 | Constant *Cst = ConstantInt::get(Type::Int1Ty, IsICMP_NE); |
| 6062 | return ReplaceInstUsesWith(ICI, Cst); |
| 6063 | } |
| 6064 | |
| 6065 | // Otherwise, check to see if the bits shifted out are known to be zero. |
| 6066 | // If so, we can compare against the unshifted value: |
| 6067 | // (X & 4) >> 1 == 2 --> (X & 4) == 4. |
Evan Cheng | f30752c | 2008-04-23 00:38:06 +0000 | [diff] [blame] | 6068 | if (LHSI->hasOneUse() && |
| 6069 | MaskedValueIsZero(LHSI->getOperand(0), |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 6070 | APInt::getLowBitsSet(Comp.getBitWidth(), ShAmtVal))) { |
| 6071 | return new ICmpInst(ICI.getPredicate(), LHSI->getOperand(0), |
| 6072 | ConstantExpr::getShl(RHS, ShAmt)); |
| 6073 | } |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6074 | |
Evan Cheng | f30752c | 2008-04-23 00:38:06 +0000 | [diff] [blame] | 6075 | if (LHSI->hasOneUse()) { |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 6076 | // Otherwise strength reduce the shift into an and. |
| 6077 | APInt Val(APInt::getHighBitsSet(TypeBits, TypeBits - ShAmtVal)); |
| 6078 | Constant *Mask = ConstantInt::get(Val); |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6079 | |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 6080 | Instruction *AndI = |
| 6081 | BinaryOperator::createAnd(LHSI->getOperand(0), |
| 6082 | Mask, LHSI->getName()+".mask"); |
| 6083 | Value *And = InsertNewInstBefore(AndI, ICI); |
| 6084 | return new ICmpInst(ICI.getPredicate(), And, |
| 6085 | ConstantExpr::getShl(RHS, ShAmt)); |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6086 | } |
| 6087 | break; |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6088 | } |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6089 | |
| 6090 | case Instruction::SDiv: |
| 6091 | case Instruction::UDiv: |
| 6092 | // Fold: icmp pred ([us]div X, C1), C2 -> range test |
| 6093 | // Fold this div into the comparison, producing a range check. |
| 6094 | // Determine, based on the divide type, what the range is being |
| 6095 | // checked. If there is an overflow on the low or high side, remember |
| 6096 | // it, otherwise compute the range [low, hi) bounding the new value. |
| 6097 | // See: InsertRangeTest above for the kinds of replacements possible. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 6098 | if (ConstantInt *DivRHS = dyn_cast<ConstantInt>(LHSI->getOperand(1))) |
| 6099 | if (Instruction *R = FoldICmpDivCst(ICI, cast<BinaryOperator>(LHSI), |
| 6100 | DivRHS)) |
| 6101 | return R; |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6102 | break; |
Nick Lewycky | 5be2920 | 2008-02-03 16:33:09 +0000 | [diff] [blame] | 6103 | |
| 6104 | case Instruction::Add: |
| 6105 | // Fold: icmp pred (add, X, C1), C2 |
| 6106 | |
| 6107 | if (!ICI.isEquality()) { |
| 6108 | ConstantInt *LHSC = dyn_cast<ConstantInt>(LHSI->getOperand(1)); |
| 6109 | if (!LHSC) break; |
| 6110 | const APInt &LHSV = LHSC->getValue(); |
| 6111 | |
| 6112 | ConstantRange CR = ICI.makeConstantRange(ICI.getPredicate(), RHSV) |
| 6113 | .subtract(LHSV); |
| 6114 | |
| 6115 | if (ICI.isSignedPredicate()) { |
| 6116 | if (CR.getLower().isSignBit()) { |
| 6117 | return new ICmpInst(ICmpInst::ICMP_SLT, LHSI->getOperand(0), |
| 6118 | ConstantInt::get(CR.getUpper())); |
| 6119 | } else if (CR.getUpper().isSignBit()) { |
| 6120 | return new ICmpInst(ICmpInst::ICMP_SGE, LHSI->getOperand(0), |
| 6121 | ConstantInt::get(CR.getLower())); |
| 6122 | } |
| 6123 | } else { |
| 6124 | if (CR.getLower().isMinValue()) { |
| 6125 | return new ICmpInst(ICmpInst::ICMP_ULT, LHSI->getOperand(0), |
| 6126 | ConstantInt::get(CR.getUpper())); |
| 6127 | } else if (CR.getUpper().isMinValue()) { |
| 6128 | return new ICmpInst(ICmpInst::ICMP_UGE, LHSI->getOperand(0), |
| 6129 | ConstantInt::get(CR.getLower())); |
| 6130 | } |
| 6131 | } |
| 6132 | } |
| 6133 | break; |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6134 | } |
| 6135 | |
| 6136 | // Simplify icmp_eq and icmp_ne instructions with integer constant RHS. |
| 6137 | if (ICI.isEquality()) { |
| 6138 | bool isICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE; |
| 6139 | |
| 6140 | // If the first operand is (add|sub|and|or|xor|rem) with a constant, and |
| 6141 | // the second operand is a constant, simplify a bit. |
| 6142 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(LHSI)) { |
| 6143 | switch (BO->getOpcode()) { |
| 6144 | case Instruction::SRem: |
| 6145 | // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one. |
| 6146 | if (RHSV == 0 && isa<ConstantInt>(BO->getOperand(1)) &&BO->hasOneUse()){ |
| 6147 | const APInt &V = cast<ConstantInt>(BO->getOperand(1))->getValue(); |
| 6148 | if (V.sgt(APInt(V.getBitWidth(), 1)) && V.isPowerOf2()) { |
| 6149 | Instruction *NewRem = |
| 6150 | BinaryOperator::createURem(BO->getOperand(0), BO->getOperand(1), |
| 6151 | BO->getName()); |
| 6152 | InsertNewInstBefore(NewRem, ICI); |
| 6153 | return new ICmpInst(ICI.getPredicate(), NewRem, |
| 6154 | Constant::getNullValue(BO->getType())); |
| 6155 | } |
| 6156 | } |
| 6157 | break; |
| 6158 | case Instruction::Add: |
| 6159 | // Replace ((add A, B) != C) with (A != C-B) if B & C are constants. |
| 6160 | if (ConstantInt *BOp1C = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
| 6161 | if (BO->hasOneUse()) |
| 6162 | return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), |
| 6163 | Subtract(RHS, BOp1C)); |
| 6164 | } else if (RHSV == 0) { |
| 6165 | // Replace ((add A, B) != 0) with (A != -B) if A or B is |
| 6166 | // efficiently invertible, or if the add has just this one use. |
| 6167 | Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1); |
| 6168 | |
| 6169 | if (Value *NegVal = dyn_castNegVal(BOp1)) |
| 6170 | return new ICmpInst(ICI.getPredicate(), BOp0, NegVal); |
| 6171 | else if (Value *NegVal = dyn_castNegVal(BOp0)) |
| 6172 | return new ICmpInst(ICI.getPredicate(), NegVal, BOp1); |
| 6173 | else if (BO->hasOneUse()) { |
| 6174 | Instruction *Neg = BinaryOperator::createNeg(BOp1); |
| 6175 | InsertNewInstBefore(Neg, ICI); |
| 6176 | Neg->takeName(BO); |
| 6177 | return new ICmpInst(ICI.getPredicate(), BOp0, Neg); |
| 6178 | } |
| 6179 | } |
| 6180 | break; |
| 6181 | case Instruction::Xor: |
| 6182 | // For the xor case, we can xor two constants together, eliminating |
| 6183 | // the explicit xor. |
| 6184 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) |
| 6185 | return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), |
| 6186 | ConstantExpr::getXor(RHS, BOC)); |
| 6187 | |
| 6188 | // FALLTHROUGH |
| 6189 | case Instruction::Sub: |
| 6190 | // Replace (([sub|xor] A, B) != 0) with (A != B) |
| 6191 | if (RHSV == 0) |
| 6192 | return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), |
| 6193 | BO->getOperand(1)); |
| 6194 | break; |
| 6195 | |
| 6196 | case Instruction::Or: |
| 6197 | // If bits are being or'd in that are not present in the constant we |
| 6198 | // are comparing against, then the comparison could never succeed! |
| 6199 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) { |
| 6200 | Constant *NotCI = ConstantExpr::getNot(RHS); |
| 6201 | if (!ConstantExpr::getAnd(BOC, NotCI)->isNullValue()) |
| 6202 | return ReplaceInstUsesWith(ICI, ConstantInt::get(Type::Int1Ty, |
| 6203 | isICMP_NE)); |
| 6204 | } |
| 6205 | break; |
| 6206 | |
| 6207 | case Instruction::And: |
| 6208 | if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
| 6209 | // If bits are being compared against that are and'd out, then the |
| 6210 | // comparison can never succeed! |
| 6211 | if ((RHSV & ~BOC->getValue()) != 0) |
| 6212 | return ReplaceInstUsesWith(ICI, ConstantInt::get(Type::Int1Ty, |
| 6213 | isICMP_NE)); |
| 6214 | |
| 6215 | // If we have ((X & C) == C), turn it into ((X & C) != 0). |
| 6216 | if (RHS == BOC && RHSV.isPowerOf2()) |
| 6217 | return new ICmpInst(isICMP_NE ? ICmpInst::ICMP_EQ : |
| 6218 | ICmpInst::ICMP_NE, LHSI, |
| 6219 | Constant::getNullValue(RHS->getType())); |
| 6220 | |
| 6221 | // Replace (and X, (1 << size(X)-1) != 0) with x s< 0 |
| 6222 | if (isSignBit(BOC)) { |
| 6223 | Value *X = BO->getOperand(0); |
| 6224 | Constant *Zero = Constant::getNullValue(X->getType()); |
| 6225 | ICmpInst::Predicate pred = isICMP_NE ? |
| 6226 | ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGE; |
| 6227 | return new ICmpInst(pred, X, Zero); |
| 6228 | } |
| 6229 | |
| 6230 | // ((X & ~7) == 0) --> X < 8 |
| 6231 | if (RHSV == 0 && isHighOnes(BOC)) { |
| 6232 | Value *X = BO->getOperand(0); |
| 6233 | Constant *NegX = ConstantExpr::getNeg(BOC); |
| 6234 | ICmpInst::Predicate pred = isICMP_NE ? |
| 6235 | ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT; |
| 6236 | return new ICmpInst(pred, X, NegX); |
| 6237 | } |
| 6238 | } |
| 6239 | default: break; |
| 6240 | } |
| 6241 | } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(LHSI)) { |
| 6242 | // Handle icmp {eq|ne} <intrinsic>, intcst. |
| 6243 | if (II->getIntrinsicID() == Intrinsic::bswap) { |
| 6244 | AddToWorkList(II); |
| 6245 | ICI.setOperand(0, II->getOperand(1)); |
| 6246 | ICI.setOperand(1, ConstantInt::get(RHSV.byteSwap())); |
| 6247 | return &ICI; |
| 6248 | } |
| 6249 | } |
| 6250 | } else { // Not a ICMP_EQ/ICMP_NE |
Chris Lattner | e34e9a2 | 2007-04-14 23:32:02 +0000 | [diff] [blame] | 6251 | // If the LHS is a cast from an integral value of the same size, |
| 6252 | // then since we know the RHS is a constant, try to simlify. |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6253 | if (CastInst *Cast = dyn_cast<CastInst>(LHSI)) { |
| 6254 | Value *CastOp = Cast->getOperand(0); |
| 6255 | const Type *SrcTy = CastOp->getType(); |
| 6256 | uint32_t SrcTySize = SrcTy->getPrimitiveSizeInBits(); |
| 6257 | if (SrcTy->isInteger() && |
| 6258 | SrcTySize == Cast->getType()->getPrimitiveSizeInBits()) { |
| 6259 | // If this is an unsigned comparison, try to make the comparison use |
| 6260 | // smaller constant values. |
| 6261 | if (ICI.getPredicate() == ICmpInst::ICMP_ULT && RHSV.isSignBit()) { |
| 6262 | // X u< 128 => X s> -1 |
| 6263 | return new ICmpInst(ICmpInst::ICMP_SGT, CastOp, |
| 6264 | ConstantInt::get(APInt::getAllOnesValue(SrcTySize))); |
| 6265 | } else if (ICI.getPredicate() == ICmpInst::ICMP_UGT && |
| 6266 | RHSV == APInt::getSignedMaxValue(SrcTySize)) { |
| 6267 | // X u> 127 => X s< 0 |
| 6268 | return new ICmpInst(ICmpInst::ICMP_SLT, CastOp, |
| 6269 | Constant::getNullValue(SrcTy)); |
| 6270 | } |
| 6271 | } |
| 6272 | } |
| 6273 | } |
| 6274 | return 0; |
| 6275 | } |
| 6276 | |
| 6277 | /// visitICmpInstWithCastAndCast - Handle icmp (cast x to y), (cast/cst). |
| 6278 | /// We only handle extending casts so far. |
| 6279 | /// |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6280 | Instruction *InstCombiner::visitICmpInstWithCastAndCast(ICmpInst &ICI) { |
| 6281 | const CastInst *LHSCI = cast<CastInst>(ICI.getOperand(0)); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6282 | Value *LHSCIOp = LHSCI->getOperand(0); |
| 6283 | const Type *SrcTy = LHSCIOp->getType(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6284 | const Type *DestTy = LHSCI->getType(); |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 6285 | Value *RHSCIOp; |
| 6286 | |
Chris Lattner | 8c756c1 | 2007-05-05 22:41:33 +0000 | [diff] [blame] | 6287 | // Turn icmp (ptrtoint x), (ptrtoint/c) into a compare of the input if the |
| 6288 | // integer type is the same size as the pointer type. |
| 6289 | if (LHSCI->getOpcode() == Instruction::PtrToInt && |
| 6290 | getTargetData().getPointerSizeInBits() == |
| 6291 | cast<IntegerType>(DestTy)->getBitWidth()) { |
| 6292 | Value *RHSOp = 0; |
| 6293 | if (Constant *RHSC = dyn_cast<Constant>(ICI.getOperand(1))) { |
Chris Lattner | 6f6f512 | 2007-05-06 07:24:03 +0000 | [diff] [blame] | 6294 | RHSOp = ConstantExpr::getIntToPtr(RHSC, SrcTy); |
Chris Lattner | 8c756c1 | 2007-05-05 22:41:33 +0000 | [diff] [blame] | 6295 | } else if (PtrToIntInst *RHSC = dyn_cast<PtrToIntInst>(ICI.getOperand(1))) { |
| 6296 | RHSOp = RHSC->getOperand(0); |
| 6297 | // If the pointer types don't match, insert a bitcast. |
| 6298 | if (LHSCIOp->getType() != RHSOp->getType()) |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 6299 | RHSOp = InsertBitCastBefore(RHSOp, LHSCIOp->getType(), ICI); |
Chris Lattner | 8c756c1 | 2007-05-05 22:41:33 +0000 | [diff] [blame] | 6300 | } |
| 6301 | |
| 6302 | if (RHSOp) |
| 6303 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSOp); |
| 6304 | } |
| 6305 | |
| 6306 | // The code below only handles extension cast instructions, so far. |
| 6307 | // Enforce this. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6308 | if (LHSCI->getOpcode() != Instruction::ZExt && |
| 6309 | LHSCI->getOpcode() != Instruction::SExt) |
Chris Lattner | b352fa5 | 2005-01-17 03:20:02 +0000 | [diff] [blame] | 6310 | return 0; |
| 6311 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6312 | bool isSignedExt = LHSCI->getOpcode() == Instruction::SExt; |
| 6313 | bool isSignedCmp = ICI.isSignedPredicate(); |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 6314 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6315 | if (CastInst *CI = dyn_cast<CastInst>(ICI.getOperand(1))) { |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 6316 | // Not an extension from the same type? |
| 6317 | RHSCIOp = CI->getOperand(0); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6318 | if (RHSCIOp->getType() != LHSCIOp->getType()) |
| 6319 | return 0; |
Chris Lattner | a5c5e77 | 2007-01-13 23:11:38 +0000 | [diff] [blame] | 6320 | |
Nick Lewycky | 4189a53 | 2008-01-28 03:48:02 +0000 | [diff] [blame] | 6321 | // 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] | 6322 | // and the other is a zext), then we can't handle this. |
| 6323 | if (CI->getOpcode() != LHSCI->getOpcode()) |
| 6324 | return 0; |
| 6325 | |
Nick Lewycky | 4189a53 | 2008-01-28 03:48:02 +0000 | [diff] [blame] | 6326 | // Deal with equality cases early. |
| 6327 | if (ICI.isEquality()) |
| 6328 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSCIOp); |
| 6329 | |
| 6330 | // A signed comparison of sign extended values simplifies into a |
| 6331 | // signed comparison. |
| 6332 | if (isSignedCmp && isSignedExt) |
| 6333 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSCIOp); |
| 6334 | |
| 6335 | // The other three cases all fold into an unsigned comparison. |
| 6336 | return new ICmpInst(ICI.getUnsignedPredicate(), LHSCIOp, RHSCIOp); |
Reid Spencer | 6731d5c | 2004-11-28 21:31:15 +0000 | [diff] [blame] | 6337 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 6338 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6339 | // If we aren't dealing with a constant on the RHS, exit early |
| 6340 | ConstantInt *CI = dyn_cast<ConstantInt>(ICI.getOperand(1)); |
| 6341 | if (!CI) |
| 6342 | return 0; |
| 6343 | |
| 6344 | // Compute the constant that would happen if we truncated to SrcTy then |
| 6345 | // reextended to DestTy. |
| 6346 | Constant *Res1 = ConstantExpr::getTrunc(CI, SrcTy); |
| 6347 | Constant *Res2 = ConstantExpr::getCast(LHSCI->getOpcode(), Res1, DestTy); |
| 6348 | |
| 6349 | // If the re-extended constant didn't change... |
| 6350 | if (Res2 == CI) { |
| 6351 | // Make sure that sign of the Cmp and the sign of the Cast are the same. |
| 6352 | // For example, we might have: |
| 6353 | // %A = sext short %X to uint |
| 6354 | // %B = icmp ugt uint %A, 1330 |
| 6355 | // It is incorrect to transform this into |
| 6356 | // %B = icmp ugt short %X, 1330 |
| 6357 | // because %A may have negative value. |
| 6358 | // |
| 6359 | // However, it is OK if SrcTy is bool (See cast-set.ll testcase) |
| 6360 | // OR operation is EQ/NE. |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 6361 | if (isSignedExt == isSignedCmp || SrcTy == Type::Int1Ty || ICI.isEquality()) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6362 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, Res1); |
| 6363 | else |
| 6364 | return 0; |
| 6365 | } |
| 6366 | |
| 6367 | // The re-extended constant changed so the constant cannot be represented |
| 6368 | // in the shorter type. Consequently, we cannot emit a simple comparison. |
| 6369 | |
| 6370 | // First, handle some easy cases. We know the result cannot be equal at this |
| 6371 | // point so handle the ICI.isEquality() cases |
| 6372 | if (ICI.getPredicate() == ICmpInst::ICMP_EQ) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6373 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6374 | if (ICI.getPredicate() == ICmpInst::ICMP_NE) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6375 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6376 | |
| 6377 | // Evaluate the comparison for LT (we invert for GT below). LE and GE cases |
| 6378 | // should have been folded away previously and not enter in here. |
| 6379 | Value *Result; |
| 6380 | if (isSignedCmp) { |
| 6381 | // We're performing a signed comparison. |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 6382 | if (cast<ConstantInt>(CI)->getValue().isNegative()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6383 | Result = ConstantInt::getFalse(); // X < (small) --> false |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6384 | else |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6385 | Result = ConstantInt::getTrue(); // X < (large) --> true |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6386 | } else { |
| 6387 | // We're performing an unsigned comparison. |
| 6388 | if (isSignedExt) { |
| 6389 | // We're performing an unsigned comp with a sign extended value. |
| 6390 | // This is true if the input is >= 0. [aka >s -1] |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6391 | Constant *NegOne = ConstantInt::getAllOnesValue(SrcTy); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6392 | Result = InsertNewInstBefore(new ICmpInst(ICmpInst::ICMP_SGT, LHSCIOp, |
| 6393 | NegOne, ICI.getName()), ICI); |
| 6394 | } else { |
| 6395 | // Unsigned extend & unsigned compare -> always true. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6396 | Result = ConstantInt::getTrue(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6397 | } |
| 6398 | } |
| 6399 | |
| 6400 | // Finally, return the value computed. |
| 6401 | if (ICI.getPredicate() == ICmpInst::ICMP_ULT || |
| 6402 | ICI.getPredicate() == ICmpInst::ICMP_SLT) { |
| 6403 | return ReplaceInstUsesWith(ICI, Result); |
| 6404 | } else { |
| 6405 | assert((ICI.getPredicate()==ICmpInst::ICMP_UGT || |
| 6406 | ICI.getPredicate()==ICmpInst::ICMP_SGT) && |
| 6407 | "ICmp should be folded!"); |
| 6408 | if (Constant *CI = dyn_cast<Constant>(Result)) |
| 6409 | return ReplaceInstUsesWith(ICI, ConstantExpr::getNot(CI)); |
| 6410 | else |
| 6411 | return BinaryOperator::createNot(Result); |
| 6412 | } |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 6413 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 6414 | |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6415 | Instruction *InstCombiner::visitShl(BinaryOperator &I) { |
| 6416 | return commonShiftTransforms(I); |
| 6417 | } |
| 6418 | |
| 6419 | Instruction *InstCombiner::visitLShr(BinaryOperator &I) { |
| 6420 | return commonShiftTransforms(I); |
| 6421 | } |
| 6422 | |
| 6423 | Instruction *InstCombiner::visitAShr(BinaryOperator &I) { |
Chris Lattner | 348f665 | 2007-12-06 01:59:46 +0000 | [diff] [blame] | 6424 | if (Instruction *R = commonShiftTransforms(I)) |
| 6425 | return R; |
| 6426 | |
| 6427 | Value *Op0 = I.getOperand(0); |
| 6428 | |
| 6429 | // ashr int -1, X = -1 (for any arithmetic shift rights of ~0) |
| 6430 | if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0)) |
| 6431 | if (CSI->isAllOnesValue()) |
| 6432 | return ReplaceInstUsesWith(I, CSI); |
| 6433 | |
| 6434 | // See if we can turn a signed shr into an unsigned shr. |
| 6435 | if (MaskedValueIsZero(Op0, |
| 6436 | APInt::getSignBit(I.getType()->getPrimitiveSizeInBits()))) |
| 6437 | return BinaryOperator::createLShr(Op0, I.getOperand(1)); |
| 6438 | |
| 6439 | return 0; |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6440 | } |
| 6441 | |
| 6442 | Instruction *InstCombiner::commonShiftTransforms(BinaryOperator &I) { |
| 6443 | assert(I.getOperand(1)->getType() == I.getOperand(0)->getType()); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 6444 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 6445 | |
| 6446 | // shl X, 0 == X and shr X, 0 == X |
| 6447 | // shl 0, X == 0 and shr 0, X == 0 |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6448 | if (Op1 == Constant::getNullValue(Op1->getType()) || |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 6449 | Op0 == Constant::getNullValue(Op0->getType())) |
| 6450 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 8d6bbdb | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 6451 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6452 | if (isa<UndefValue>(Op0)) { |
| 6453 | if (I.getOpcode() == Instruction::AShr) // undef >>s X -> undef |
Chris Lattner | 79a564c | 2004-10-16 23:28:04 +0000 | [diff] [blame] | 6454 | return ReplaceInstUsesWith(I, Op0); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6455 | else // undef << X -> 0, undef >>u X -> 0 |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 6456 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 6457 | } |
| 6458 | if (isa<UndefValue>(Op1)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6459 | if (I.getOpcode() == Instruction::AShr) // X >>s undef -> X |
| 6460 | return ReplaceInstUsesWith(I, Op0); |
| 6461 | else // X << undef, X >>u undef -> 0 |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 6462 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 6463 | } |
| 6464 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 6465 | // Try to fold constant and into select arguments. |
| 6466 | if (isa<Constant>(Op0)) |
| 6467 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 6468 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 6469 | return R; |
| 6470 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6471 | if (ConstantInt *CUI = dyn_cast<ConstantInt>(Op1)) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6472 | if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I)) |
| 6473 | return Res; |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6474 | return 0; |
| 6475 | } |
| 6476 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6477 | Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6478 | BinaryOperator &I) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6479 | bool isLeftShift = I.getOpcode() == Instruction::Shl; |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6480 | |
Chris Lattner | 8d6bbdb | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 6481 | // See if we can simplify any instructions used by the instruction whose sole |
| 6482 | // purpose is to compute bits we don't care about. |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6483 | uint32_t TypeBits = Op0->getType()->getPrimitiveSizeInBits(); |
| 6484 | APInt KnownZero(TypeBits, 0), KnownOne(TypeBits, 0); |
| 6485 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(TypeBits), |
Chris Lattner | 8d6bbdb | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 6486 | KnownZero, KnownOne)) |
| 6487 | return &I; |
| 6488 | |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6489 | // shl uint X, 32 = 0 and shr ubyte Y, 9 = 0, ... just don't eliminate shr |
| 6490 | // of a signed value. |
| 6491 | // |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 6492 | if (Op1->uge(TypeBits)) { |
Chris Lattner | 0737c24 | 2007-02-02 05:29:55 +0000 | [diff] [blame] | 6493 | if (I.getOpcode() != Instruction::AShr) |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6494 | return ReplaceInstUsesWith(I, Constant::getNullValue(Op0->getType())); |
| 6495 | else { |
Chris Lattner | 0737c24 | 2007-02-02 05:29:55 +0000 | [diff] [blame] | 6496 | I.setOperand(1, ConstantInt::get(I.getType(), TypeBits-1)); |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6497 | return &I; |
Chris Lattner | 8adac75 | 2004-02-23 20:30:06 +0000 | [diff] [blame] | 6498 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6499 | } |
| 6500 | |
| 6501 | // ((X*C1) << C2) == (X * (C1 << C2)) |
| 6502 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0)) |
| 6503 | if (BO->getOpcode() == Instruction::Mul && isLeftShift) |
| 6504 | if (Constant *BOOp = dyn_cast<Constant>(BO->getOperand(1))) |
| 6505 | return BinaryOperator::createMul(BO->getOperand(0), |
| 6506 | ConstantExpr::getShl(BOOp, Op1)); |
| 6507 | |
| 6508 | // Try to fold constant and into select arguments. |
| 6509 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 6510 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 6511 | return R; |
| 6512 | if (isa<PHINode>(Op0)) |
| 6513 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 6514 | return NV; |
| 6515 | |
Chris Lattner | 8999dd3 | 2007-12-22 09:07:47 +0000 | [diff] [blame] | 6516 | // Fold shift2(trunc(shift1(x,c1)), c2) -> trunc(shift2(shift1(x,c1),c2)) |
| 6517 | if (TruncInst *TI = dyn_cast<TruncInst>(Op0)) { |
| 6518 | Instruction *TrOp = dyn_cast<Instruction>(TI->getOperand(0)); |
| 6519 | // If 'shift2' is an ashr, we would have to get the sign bit into a funny |
| 6520 | // place. Don't try to do this transformation in this case. Also, we |
| 6521 | // require that the input operand is a shift-by-constant so that we have |
| 6522 | // confidence that the shifts will get folded together. We could do this |
| 6523 | // xform in more cases, but it is unlikely to be profitable. |
| 6524 | if (TrOp && I.isLogicalShift() && TrOp->isShift() && |
| 6525 | isa<ConstantInt>(TrOp->getOperand(1))) { |
| 6526 | // Okay, we'll do this xform. Make the shift of shift. |
| 6527 | Constant *ShAmt = ConstantExpr::getZExt(Op1, TrOp->getType()); |
| 6528 | Instruction *NSh = BinaryOperator::create(I.getOpcode(), TrOp, ShAmt, |
| 6529 | I.getName()); |
| 6530 | InsertNewInstBefore(NSh, I); // (shift2 (shift1 & 0x00FF), c2) |
| 6531 | |
| 6532 | // For logical shifts, the truncation has the effect of making the high |
| 6533 | // part of the register be zeros. Emulate this by inserting an AND to |
| 6534 | // clear the top bits as needed. This 'and' will usually be zapped by |
| 6535 | // other xforms later if dead. |
| 6536 | unsigned SrcSize = TrOp->getType()->getPrimitiveSizeInBits(); |
| 6537 | unsigned DstSize = TI->getType()->getPrimitiveSizeInBits(); |
| 6538 | APInt MaskV(APInt::getLowBitsSet(SrcSize, DstSize)); |
| 6539 | |
| 6540 | // The mask we constructed says what the trunc would do if occurring |
| 6541 | // between the shifts. We want to know the effect *after* the second |
| 6542 | // shift. We know that it is a logical shift by a constant, so adjust the |
| 6543 | // mask as appropriate. |
| 6544 | if (I.getOpcode() == Instruction::Shl) |
| 6545 | MaskV <<= Op1->getZExtValue(); |
| 6546 | else { |
| 6547 | assert(I.getOpcode() == Instruction::LShr && "Unknown logical shift"); |
| 6548 | MaskV = MaskV.lshr(Op1->getZExtValue()); |
| 6549 | } |
| 6550 | |
| 6551 | Instruction *And = BinaryOperator::createAnd(NSh, ConstantInt::get(MaskV), |
| 6552 | TI->getName()); |
| 6553 | InsertNewInstBefore(And, I); // shift1 & 0x00FF |
| 6554 | |
| 6555 | // Return the value truncated to the interesting size. |
| 6556 | return new TruncInst(And, I.getType()); |
| 6557 | } |
| 6558 | } |
| 6559 | |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6560 | if (Op0->hasOneUse()) { |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6561 | if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0)) { |
| 6562 | // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C) |
| 6563 | Value *V1, *V2; |
| 6564 | ConstantInt *CC; |
| 6565 | switch (Op0BO->getOpcode()) { |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 6566 | default: break; |
| 6567 | case Instruction::Add: |
| 6568 | case Instruction::And: |
| 6569 | case Instruction::Or: |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6570 | case Instruction::Xor: { |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 6571 | // These operators commute. |
| 6572 | // Turn (Y + (X >> C)) << C -> (X + (Y << C)) & (~0 << C) |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6573 | if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() && |
| 6574 | match(Op0BO->getOperand(1), |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6575 | m_Shr(m_Value(V1), m_ConstantInt(CC))) && CC == Op1) { |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 6576 | Instruction *YS = BinaryOperator::createShl( |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6577 | Op0BO->getOperand(0), Op1, |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6578 | Op0BO->getName()); |
| 6579 | InsertNewInstBefore(YS, I); // (Y << C) |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 6580 | Instruction *X = |
| 6581 | BinaryOperator::create(Op0BO->getOpcode(), YS, V1, |
| 6582 | Op0BO->getOperand(1)->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6583 | InsertNewInstBefore(X, I); // (X + (Y << C)) |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 6584 | uint32_t Op1Val = Op1->getLimitedValue(TypeBits); |
Zhou Sheng | 90b9681 | 2007-03-30 05:45:18 +0000 | [diff] [blame] | 6585 | return BinaryOperator::createAnd(X, ConstantInt::get( |
| 6586 | APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val))); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6587 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6588 | |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6589 | // Turn (Y + ((X >> C) & CC)) << C -> ((X & (CC << C)) + (Y << C)) |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6590 | Value *Op0BOOp1 = Op0BO->getOperand(1); |
Chris Lattner | 3c69849 | 2007-03-05 00:11:19 +0000 | [diff] [blame] | 6591 | if (isLeftShift && Op0BOOp1->hasOneUse() && |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6592 | match(Op0BOOp1, |
| 6593 | m_And(m_Shr(m_Value(V1), m_Value(V2)),m_ConstantInt(CC))) && |
Chris Lattner | 3c69849 | 2007-03-05 00:11:19 +0000 | [diff] [blame] | 6594 | cast<BinaryOperator>(Op0BOOp1)->getOperand(0)->hasOneUse() && |
| 6595 | V2 == Op1) { |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 6596 | Instruction *YS = BinaryOperator::createShl( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6597 | Op0BO->getOperand(0), Op1, |
| 6598 | Op0BO->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6599 | InsertNewInstBefore(YS, I); // (Y << C) |
| 6600 | Instruction *XM = |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6601 | BinaryOperator::createAnd(V1, ConstantExpr::getShl(CC, Op1), |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6602 | V1->getName()+".mask"); |
| 6603 | InsertNewInstBefore(XM, I); // X & (CC << C) |
| 6604 | |
| 6605 | return BinaryOperator::create(Op0BO->getOpcode(), YS, XM); |
| 6606 | } |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6607 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6608 | |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6609 | // FALL THROUGH. |
| 6610 | case Instruction::Sub: { |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 6611 | // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C) |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6612 | if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() && |
| 6613 | match(Op0BO->getOperand(0), |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6614 | m_Shr(m_Value(V1), m_ConstantInt(CC))) && CC == Op1) { |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 6615 | Instruction *YS = BinaryOperator::createShl( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6616 | Op0BO->getOperand(1), Op1, |
| 6617 | Op0BO->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6618 | InsertNewInstBefore(YS, I); // (Y << C) |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 6619 | Instruction *X = |
Chris Lattner | 13d4ab4 | 2006-05-31 21:14:00 +0000 | [diff] [blame] | 6620 | BinaryOperator::create(Op0BO->getOpcode(), V1, YS, |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 6621 | Op0BO->getOperand(0)->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6622 | InsertNewInstBefore(X, I); // (X + (Y << C)) |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 6623 | uint32_t Op1Val = Op1->getLimitedValue(TypeBits); |
Zhou Sheng | 90b9681 | 2007-03-30 05:45:18 +0000 | [diff] [blame] | 6624 | return BinaryOperator::createAnd(X, ConstantInt::get( |
| 6625 | APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val))); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6626 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6627 | |
Chris Lattner | 13d4ab4 | 2006-05-31 21:14:00 +0000 | [diff] [blame] | 6628 | // Turn (((X >> C)&CC) + Y) << C -> (X + (Y << C)) & (CC << C) |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6629 | if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() && |
| 6630 | match(Op0BO->getOperand(0), |
| 6631 | m_And(m_Shr(m_Value(V1), m_Value(V2)), |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6632 | m_ConstantInt(CC))) && V2 == Op1 && |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 6633 | cast<BinaryOperator>(Op0BO->getOperand(0)) |
| 6634 | ->getOperand(0)->hasOneUse()) { |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 6635 | Instruction *YS = BinaryOperator::createShl( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6636 | Op0BO->getOperand(1), Op1, |
| 6637 | Op0BO->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6638 | InsertNewInstBefore(YS, I); // (Y << C) |
| 6639 | Instruction *XM = |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6640 | BinaryOperator::createAnd(V1, ConstantExpr::getShl(CC, Op1), |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6641 | V1->getName()+".mask"); |
| 6642 | InsertNewInstBefore(XM, I); // X & (CC << C) |
| 6643 | |
Chris Lattner | 13d4ab4 | 2006-05-31 21:14:00 +0000 | [diff] [blame] | 6644 | return BinaryOperator::create(Op0BO->getOpcode(), XM, YS); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6645 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6646 | |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 6647 | break; |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6648 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6649 | } |
| 6650 | |
| 6651 | |
| 6652 | // If the operand is an bitwise operator with a constant RHS, and the |
| 6653 | // shift is the only use, we can pull it out of the shift. |
| 6654 | if (ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand(1))) { |
| 6655 | bool isValid = true; // Valid only for And, Or, Xor |
| 6656 | bool highBitSet = false; // Transform if high bit of constant set? |
| 6657 | |
| 6658 | switch (Op0BO->getOpcode()) { |
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 6659 | default: isValid = false; break; // Do not perform transform! |
Chris Lattner | 1f7e160 | 2004-10-08 03:46:20 +0000 | [diff] [blame] | 6660 | case Instruction::Add: |
| 6661 | isValid = isLeftShift; |
| 6662 | break; |
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 6663 | case Instruction::Or: |
| 6664 | case Instruction::Xor: |
| 6665 | highBitSet = false; |
| 6666 | break; |
| 6667 | case Instruction::And: |
| 6668 | highBitSet = true; |
| 6669 | break; |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6670 | } |
| 6671 | |
| 6672 | // If this is a signed shift right, and the high bit is modified |
| 6673 | // by the logical operation, do not perform the transformation. |
| 6674 | // The highBitSet boolean indicates the value of the high bit of |
| 6675 | // the constant which would cause it to be modified for this |
| 6676 | // operation. |
| 6677 | // |
Chris Lattner | c95ba44 | 2007-12-06 06:25:04 +0000 | [diff] [blame] | 6678 | if (isValid && I.getOpcode() == Instruction::AShr) |
Zhou Sheng | e9e03f6 | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 6679 | isValid = Op0C->getValue()[TypeBits-1] == highBitSet; |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6680 | |
| 6681 | if (isValid) { |
| 6682 | Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, Op1); |
| 6683 | |
| 6684 | Instruction *NewShift = |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6685 | BinaryOperator::create(I.getOpcode(), Op0BO->getOperand(0), Op1); |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6686 | InsertNewInstBefore(NewShift, I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6687 | NewShift->takeName(Op0BO); |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6688 | |
| 6689 | return BinaryOperator::create(Op0BO->getOpcode(), NewShift, |
| 6690 | NewRHS); |
| 6691 | } |
| 6692 | } |
| 6693 | } |
| 6694 | } |
| 6695 | |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6696 | // 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] | 6697 | BinaryOperator *ShiftOp = dyn_cast<BinaryOperator>(Op0); |
| 6698 | if (ShiftOp && !ShiftOp->isShift()) |
| 6699 | ShiftOp = 0; |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6700 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6701 | if (ShiftOp && isa<ConstantInt>(ShiftOp->getOperand(1))) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6702 | ConstantInt *ShiftAmt1C = cast<ConstantInt>(ShiftOp->getOperand(1)); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 6703 | uint32_t ShiftAmt1 = ShiftAmt1C->getLimitedValue(TypeBits); |
| 6704 | uint32_t ShiftAmt2 = Op1->getLimitedValue(TypeBits); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6705 | assert(ShiftAmt2 != 0 && "Should have been simplified earlier"); |
| 6706 | if (ShiftAmt1 == 0) return 0; // Will be simplified in the future. |
| 6707 | Value *X = ShiftOp->getOperand(0); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6708 | |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 6709 | uint32_t AmtSum = ShiftAmt1+ShiftAmt2; // Fold into one big shift. |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6710 | if (AmtSum > TypeBits) |
| 6711 | AmtSum = TypeBits; |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6712 | |
| 6713 | const IntegerType *Ty = cast<IntegerType>(I.getType()); |
| 6714 | |
| 6715 | // Check for (X << c1) << c2 and (X >> c1) >> c2 |
Chris Lattner | 7f3da2d | 2007-02-03 23:28:07 +0000 | [diff] [blame] | 6716 | if (I.getOpcode() == ShiftOp->getOpcode()) { |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6717 | return BinaryOperator::create(I.getOpcode(), X, |
| 6718 | ConstantInt::get(Ty, AmtSum)); |
| 6719 | } else if (ShiftOp->getOpcode() == Instruction::LShr && |
| 6720 | I.getOpcode() == Instruction::AShr) { |
| 6721 | // ((X >>u C1) >>s C2) -> (X >>u (C1+C2)) since C1 != 0. |
| 6722 | return BinaryOperator::createLShr(X, ConstantInt::get(Ty, AmtSum)); |
| 6723 | } else if (ShiftOp->getOpcode() == Instruction::AShr && |
| 6724 | I.getOpcode() == Instruction::LShr) { |
| 6725 | // ((X >>s C1) >>u C2) -> ((X >>s (C1+C2)) & mask) since C1 != 0. |
| 6726 | Instruction *Shift = |
| 6727 | BinaryOperator::createAShr(X, ConstantInt::get(Ty, AmtSum)); |
| 6728 | InsertNewInstBefore(Shift, I); |
| 6729 | |
Zhou Sheng | e9e03f6 | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 6730 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6731 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6732 | } |
| 6733 | |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6734 | // Okay, if we get here, one shift must be left, and the other shift must be |
| 6735 | // right. See if the amounts are equal. |
| 6736 | if (ShiftAmt1 == ShiftAmt2) { |
| 6737 | // If we have ((X >>? C) << C), turn this into X & (-1 << C). |
| 6738 | if (I.getOpcode() == Instruction::Shl) { |
Reid Spencer | 55702aa | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 6739 | APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt1)); |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6740 | return BinaryOperator::createAnd(X, ConstantInt::get(Mask)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6741 | } |
| 6742 | // If we have ((X << C) >>u C), turn this into X & (-1 >>u C). |
| 6743 | if (I.getOpcode() == Instruction::LShr) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 6744 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt1)); |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6745 | return BinaryOperator::createAnd(X, ConstantInt::get(Mask)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6746 | } |
| 6747 | // We can simplify ((X << C) >>s C) into a trunc + sext. |
| 6748 | // NOTE: we could do this for any C, but that would make 'unusual' integer |
| 6749 | // types. For now, just stick to ones well-supported by the code |
| 6750 | // generators. |
| 6751 | const Type *SExtType = 0; |
| 6752 | switch (Ty->getBitWidth() - ShiftAmt1) { |
Zhou Sheng | e9e03f6 | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 6753 | case 1 : |
| 6754 | case 8 : |
| 6755 | case 16 : |
| 6756 | case 32 : |
| 6757 | case 64 : |
| 6758 | case 128: |
| 6759 | SExtType = IntegerType::get(Ty->getBitWidth() - ShiftAmt1); |
| 6760 | break; |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6761 | default: break; |
| 6762 | } |
| 6763 | if (SExtType) { |
| 6764 | Instruction *NewTrunc = new TruncInst(X, SExtType, "sext"); |
| 6765 | InsertNewInstBefore(NewTrunc, I); |
| 6766 | return new SExtInst(NewTrunc, Ty); |
| 6767 | } |
| 6768 | // Otherwise, we can't handle it yet. |
| 6769 | } else if (ShiftAmt1 < ShiftAmt2) { |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 6770 | uint32_t ShiftDiff = ShiftAmt2-ShiftAmt1; |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6771 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 6772 | // (X >>? C1) << C2 --> X << (C2-C1) & (-1 << C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6773 | if (I.getOpcode() == Instruction::Shl) { |
| 6774 | assert(ShiftOp->getOpcode() == Instruction::LShr || |
| 6775 | ShiftOp->getOpcode() == Instruction::AShr); |
Chris Lattner | e8d56c5 | 2006-01-07 01:32:28 +0000 | [diff] [blame] | 6776 | Instruction *Shift = |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6777 | BinaryOperator::createShl(X, ConstantInt::get(Ty, ShiftDiff)); |
Chris Lattner | e8d56c5 | 2006-01-07 01:32:28 +0000 | [diff] [blame] | 6778 | InsertNewInstBefore(Shift, I); |
| 6779 | |
Reid Spencer | 55702aa | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 6780 | APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
| 6781 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6782 | } |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6783 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 6784 | // (X << C1) >>u C2 --> X >>u (C2-C1) & (-1 >> C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6785 | if (I.getOpcode() == Instruction::LShr) { |
| 6786 | assert(ShiftOp->getOpcode() == Instruction::Shl); |
| 6787 | Instruction *Shift = |
| 6788 | BinaryOperator::createLShr(X, ConstantInt::get(Ty, ShiftDiff)); |
| 6789 | InsertNewInstBefore(Shift, I); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6790 | |
Reid Spencer | d5e30f0 | 2007-03-26 17:18:58 +0000 | [diff] [blame] | 6791 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6792 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 6793 | } |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6794 | |
| 6795 | // We can't handle (X << C1) >>s C2, it shifts arbitrary bits in. |
| 6796 | } else { |
| 6797 | assert(ShiftAmt2 < ShiftAmt1); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 6798 | uint32_t ShiftDiff = ShiftAmt1-ShiftAmt2; |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6799 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 6800 | // (X >>? C1) << C2 --> X >>? (C1-C2) & (-1 << C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6801 | if (I.getOpcode() == Instruction::Shl) { |
| 6802 | assert(ShiftOp->getOpcode() == Instruction::LShr || |
| 6803 | ShiftOp->getOpcode() == Instruction::AShr); |
| 6804 | Instruction *Shift = |
| 6805 | BinaryOperator::create(ShiftOp->getOpcode(), X, |
| 6806 | ConstantInt::get(Ty, ShiftDiff)); |
| 6807 | InsertNewInstBefore(Shift, I); |
| 6808 | |
Reid Spencer | 55702aa | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 6809 | APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6810 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6811 | } |
| 6812 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 6813 | // (X << C1) >>u C2 --> X << (C1-C2) & (-1 >> C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6814 | if (I.getOpcode() == Instruction::LShr) { |
| 6815 | assert(ShiftOp->getOpcode() == Instruction::Shl); |
| 6816 | Instruction *Shift = |
| 6817 | BinaryOperator::createShl(X, ConstantInt::get(Ty, ShiftDiff)); |
| 6818 | InsertNewInstBefore(Shift, I); |
| 6819 | |
Reid Spencer | 68d27cf | 2007-03-26 23:45:51 +0000 | [diff] [blame] | 6820 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6821 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6822 | } |
| 6823 | |
| 6824 | // 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] | 6825 | } |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6826 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 6827 | return 0; |
| 6828 | } |
| 6829 | |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 6830 | |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 6831 | /// DecomposeSimpleLinearExpr - Analyze 'Val', seeing if it is a simple linear |
| 6832 | /// expression. If so, decompose it, returning some value X, such that Val is |
| 6833 | /// X*Scale+Offset. |
| 6834 | /// |
| 6835 | static Value *DecomposeSimpleLinearExpr(Value *Val, unsigned &Scale, |
Jeff Cohen | 86796be | 2007-04-04 16:58:57 +0000 | [diff] [blame] | 6836 | int &Offset) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6837 | assert(Val->getType() == Type::Int32Ty && "Unexpected allocation size type!"); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6838 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6839 | Offset = CI->getZExtValue(); |
Chris Lattner | 6a94de2 | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 6840 | Scale = 0; |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6841 | return ConstantInt::get(Type::Int32Ty, 0); |
Chris Lattner | 6a94de2 | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 6842 | } else if (BinaryOperator *I = dyn_cast<BinaryOperator>(Val)) { |
| 6843 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 6844 | if (I->getOpcode() == Instruction::Shl) { |
| 6845 | // This is a value scaled by '1 << the shift amt'. |
| 6846 | Scale = 1U << RHS->getZExtValue(); |
| 6847 | Offset = 0; |
| 6848 | return I->getOperand(0); |
| 6849 | } else if (I->getOpcode() == Instruction::Mul) { |
| 6850 | // This value is scaled by 'RHS'. |
| 6851 | Scale = RHS->getZExtValue(); |
| 6852 | Offset = 0; |
| 6853 | return I->getOperand(0); |
| 6854 | } else if (I->getOpcode() == Instruction::Add) { |
| 6855 | // We have X+C. Check to see if we really have (X*C2)+C1, |
| 6856 | // where C1 is divisible by C2. |
| 6857 | unsigned SubScale; |
| 6858 | Value *SubVal = |
| 6859 | DecomposeSimpleLinearExpr(I->getOperand(0), SubScale, Offset); |
| 6860 | Offset += RHS->getZExtValue(); |
| 6861 | Scale = SubScale; |
| 6862 | return SubVal; |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 6863 | } |
| 6864 | } |
| 6865 | } |
| 6866 | |
| 6867 | // Otherwise, we can't look past this. |
| 6868 | Scale = 1; |
| 6869 | Offset = 0; |
| 6870 | return Val; |
| 6871 | } |
| 6872 | |
| 6873 | |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6874 | /// PromoteCastOfAllocation - If we find a cast of an allocation instruction, |
| 6875 | /// 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] | 6876 | Instruction *InstCombiner::PromoteCastOfAllocation(BitCastInst &CI, |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6877 | AllocationInst &AI) { |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 6878 | const PointerType *PTy = cast<PointerType>(CI.getType()); |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6879 | |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 6880 | // Remove any uses of AI that are dead. |
| 6881 | assert(!CI.use_empty() && "Dead instructions should be removed earlier!"); |
Chris Lattner | 535014f | 2007-02-15 22:52:10 +0000 | [diff] [blame] | 6882 | |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 6883 | for (Value::use_iterator UI = AI.use_begin(), E = AI.use_end(); UI != E; ) { |
| 6884 | Instruction *User = cast<Instruction>(*UI++); |
| 6885 | if (isInstructionTriviallyDead(User)) { |
| 6886 | while (UI != E && *UI == User) |
| 6887 | ++UI; // If this instruction uses AI more than once, don't break UI. |
| 6888 | |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 6889 | ++NumDeadInst; |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 6890 | DOUT << "IC: DCE: " << *User; |
Chris Lattner | f22a5c6 | 2007-03-02 19:59:19 +0000 | [diff] [blame] | 6891 | EraseInstFromFunction(*User); |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 6892 | } |
| 6893 | } |
| 6894 | |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6895 | // Get the type really allocated and the type casted to. |
| 6896 | const Type *AllocElTy = AI.getAllocatedType(); |
| 6897 | const Type *CastElTy = PTy->getElementType(); |
| 6898 | if (!AllocElTy->isSized() || !CastElTy->isSized()) return 0; |
Chris Lattner | 18e78bb | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 6899 | |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 6900 | unsigned AllocElTyAlign = TD->getABITypeAlignment(AllocElTy); |
| 6901 | unsigned CastElTyAlign = TD->getABITypeAlignment(CastElTy); |
Chris Lattner | 18e78bb | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 6902 | if (CastElTyAlign < AllocElTyAlign) return 0; |
| 6903 | |
Chris Lattner | 39387a5 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 6904 | // If the allocation has multiple uses, only promote it if we are strictly |
| 6905 | // increasing the alignment of the resultant allocation. If we keep it the |
| 6906 | // same, we open the door to infinite loops of various kinds. |
| 6907 | if (!AI.hasOneUse() && CastElTyAlign == AllocElTyAlign) return 0; |
| 6908 | |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 6909 | uint64_t AllocElTySize = TD->getABITypeSize(AllocElTy); |
| 6910 | uint64_t CastElTySize = TD->getABITypeSize(CastElTy); |
Chris Lattner | 0ddac2a | 2005-10-27 05:53:56 +0000 | [diff] [blame] | 6911 | if (CastElTySize == 0 || AllocElTySize == 0) return 0; |
Chris Lattner | 18e78bb | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 6912 | |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 6913 | // See if we can satisfy the modulus by pulling a scale out of the array |
| 6914 | // size argument. |
Jeff Cohen | 86796be | 2007-04-04 16:58:57 +0000 | [diff] [blame] | 6915 | unsigned ArraySizeScale; |
| 6916 | int ArrayOffset; |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 6917 | Value *NumElements = // See if the array size is a decomposable linear expr. |
| 6918 | DecomposeSimpleLinearExpr(AI.getOperand(0), ArraySizeScale, ArrayOffset); |
| 6919 | |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 6920 | // If we can now satisfy the modulus, by using a non-1 scale, we really can |
| 6921 | // do the xform. |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 6922 | if ((AllocElTySize*ArraySizeScale) % CastElTySize != 0 || |
| 6923 | (AllocElTySize*ArrayOffset ) % CastElTySize != 0) return 0; |
Chris Lattner | 8142b0a | 2005-10-27 06:12:00 +0000 | [diff] [blame] | 6924 | |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 6925 | unsigned Scale = (AllocElTySize*ArraySizeScale)/CastElTySize; |
| 6926 | Value *Amt = 0; |
| 6927 | if (Scale == 1) { |
| 6928 | Amt = NumElements; |
| 6929 | } else { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6930 | // If the allocation size is constant, form a constant mul expression |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6931 | Amt = ConstantInt::get(Type::Int32Ty, Scale); |
| 6932 | if (isa<ConstantInt>(NumElements)) |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 6933 | Amt = Multiply(cast<ConstantInt>(NumElements), cast<ConstantInt>(Amt)); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6934 | // otherwise multiply the amount and the number of elements |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 6935 | else if (Scale != 1) { |
| 6936 | Instruction *Tmp = BinaryOperator::createMul(Amt, NumElements, "tmp"); |
| 6937 | Amt = InsertNewInstBefore(Tmp, AI); |
Chris Lattner | 8142b0a | 2005-10-27 06:12:00 +0000 | [diff] [blame] | 6938 | } |
Chris Lattner | 0ddac2a | 2005-10-27 05:53:56 +0000 | [diff] [blame] | 6939 | } |
| 6940 | |
Jeff Cohen | 86796be | 2007-04-04 16:58:57 +0000 | [diff] [blame] | 6941 | if (int Offset = (AllocElTySize*ArrayOffset)/CastElTySize) { |
| 6942 | Value *Off = ConstantInt::get(Type::Int32Ty, Offset, true); |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 6943 | Instruction *Tmp = BinaryOperator::createAdd(Amt, Off, "tmp"); |
| 6944 | Amt = InsertNewInstBefore(Tmp, AI); |
| 6945 | } |
| 6946 | |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6947 | AllocationInst *New; |
| 6948 | if (isa<MallocInst>(AI)) |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6949 | New = new MallocInst(CastElTy, Amt, AI.getAlignment()); |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6950 | else |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6951 | New = new AllocaInst(CastElTy, Amt, AI.getAlignment()); |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6952 | InsertNewInstBefore(New, AI); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6953 | New->takeName(&AI); |
Chris Lattner | 39387a5 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 6954 | |
| 6955 | // If the allocation has multiple uses, insert a cast and change all things |
| 6956 | // that used it to use the new cast. This will also hack on CI, but it will |
| 6957 | // die soon. |
| 6958 | if (!AI.hasOneUse()) { |
| 6959 | AddUsesToWorkList(AI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6960 | // New is the allocation instruction, pointer typed. AI is the original |
| 6961 | // allocation instruction, also pointer typed. Thus, cast to use is BitCast. |
| 6962 | CastInst *NewCast = new BitCastInst(New, AI.getType(), "tmpcast"); |
Chris Lattner | 39387a5 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 6963 | InsertNewInstBefore(NewCast, AI); |
| 6964 | AI.replaceAllUsesWith(NewCast); |
| 6965 | } |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6966 | return ReplaceInstUsesWith(CI, New); |
| 6967 | } |
| 6968 | |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6969 | /// CanEvaluateInDifferentType - Return true if we can take the specified value |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6970 | /// and return it as type Ty without inserting any new casts and without |
| 6971 | /// changing the computed value. This is used by code that tries to decide |
| 6972 | /// whether promoting or shrinking integer operations to wider or smaller types |
| 6973 | /// will allow us to eliminate a truncate or extend. |
| 6974 | /// |
| 6975 | /// This is a truncation operation if Ty is smaller than V->getType(), or an |
| 6976 | /// extension operation if Ty is larger. |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 6977 | bool InstCombiner::CanEvaluateInDifferentType(Value *V, const IntegerType *Ty, |
| 6978 | unsigned CastOpc, |
| 6979 | int &NumCastsRemoved) { |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6980 | // We can always evaluate constants in another type. |
| 6981 | if (isa<ConstantInt>(V)) |
| 6982 | return true; |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6983 | |
| 6984 | Instruction *I = dyn_cast<Instruction>(V); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6985 | if (!I) return false; |
| 6986 | |
| 6987 | const IntegerType *OrigTy = cast<IntegerType>(V->getType()); |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6988 | |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 6989 | // If this is an extension or truncate, we can often eliminate it. |
| 6990 | if (isa<TruncInst>(I) || isa<ZExtInst>(I) || isa<SExtInst>(I)) { |
| 6991 | // If this is a cast from the destination type, we can trivially eliminate |
| 6992 | // it, and this will remove a cast overall. |
| 6993 | if (I->getOperand(0)->getType() == Ty) { |
| 6994 | // If the first operand is itself a cast, and is eliminable, do not count |
| 6995 | // this as an eliminable cast. We would prefer to eliminate those two |
| 6996 | // casts first. |
| 6997 | if (!isa<CastInst>(I->getOperand(0))) |
| 6998 | ++NumCastsRemoved; |
| 6999 | return true; |
| 7000 | } |
| 7001 | } |
| 7002 | |
| 7003 | // We can't extend or shrink something that has multiple uses: doing so would |
| 7004 | // require duplicating the instruction in general, which isn't profitable. |
| 7005 | if (!I->hasOneUse()) return false; |
| 7006 | |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7007 | switch (I->getOpcode()) { |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7008 | case Instruction::Add: |
| 7009 | case Instruction::Sub: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7010 | case Instruction::And: |
| 7011 | case Instruction::Or: |
| 7012 | case Instruction::Xor: |
| 7013 | // These operators can all arbitrarily be extended or truncated. |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7014 | return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc, |
| 7015 | NumCastsRemoved) && |
| 7016 | CanEvaluateInDifferentType(I->getOperand(1), Ty, CastOpc, |
| 7017 | NumCastsRemoved); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7018 | |
Nick Lewycky | e6b0c00 | 2008-01-22 05:08:48 +0000 | [diff] [blame] | 7019 | case Instruction::Mul: |
Nick Lewycky | e6b0c00 | 2008-01-22 05:08:48 +0000 | [diff] [blame] | 7020 | // A multiply can be truncated by truncating its operands. |
| 7021 | return Ty->getBitWidth() < OrigTy->getBitWidth() && |
| 7022 | CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc, |
| 7023 | NumCastsRemoved) && |
| 7024 | CanEvaluateInDifferentType(I->getOperand(1), Ty, CastOpc, |
| 7025 | NumCastsRemoved); |
| 7026 | |
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 7027 | case Instruction::Shl: |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7028 | // If we are truncating the result of this SHL, and if it's a shift of a |
| 7029 | // constant amount, we can always perform a SHL in a smaller type. |
| 7030 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 7031 | uint32_t BitWidth = Ty->getBitWidth(); |
| 7032 | if (BitWidth < OrigTy->getBitWidth() && |
| 7033 | CI->getLimitedValue(BitWidth) < BitWidth) |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7034 | return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc, |
| 7035 | NumCastsRemoved); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7036 | } |
| 7037 | break; |
| 7038 | case Instruction::LShr: |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7039 | // If this is a truncate of a logical shr, we can truncate it to a smaller |
| 7040 | // lshr iff we know that the bits we would otherwise be shifting in are |
| 7041 | // already zeros. |
| 7042 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 7043 | uint32_t OrigBitWidth = OrigTy->getBitWidth(); |
| 7044 | uint32_t BitWidth = Ty->getBitWidth(); |
| 7045 | if (BitWidth < OrigBitWidth && |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7046 | MaskedValueIsZero(I->getOperand(0), |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 7047 | APInt::getHighBitsSet(OrigBitWidth, OrigBitWidth-BitWidth)) && |
| 7048 | CI->getLimitedValue(BitWidth) < BitWidth) { |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7049 | return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc, |
| 7050 | NumCastsRemoved); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7051 | } |
| 7052 | } |
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 7053 | break; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7054 | case Instruction::ZExt: |
| 7055 | case Instruction::SExt: |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7056 | case Instruction::Trunc: |
| 7057 | // 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] | 7058 | // can safely replace it. Note that replacing it does not reduce the number |
| 7059 | // of casts in the input. |
| 7060 | if (I->getOpcode() == CastOpc) |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7061 | return true; |
Chris Lattner | 50d9d77 | 2007-09-10 23:46:29 +0000 | [diff] [blame] | 7062 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7063 | break; |
| 7064 | default: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7065 | // TODO: Can handle more cases here. |
| 7066 | break; |
| 7067 | } |
| 7068 | |
| 7069 | return false; |
| 7070 | } |
| 7071 | |
| 7072 | /// EvaluateInDifferentType - Given an expression that |
| 7073 | /// CanEvaluateInDifferentType returns true for, actually insert the code to |
| 7074 | /// evaluate the expression. |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 7075 | Value *InstCombiner::EvaluateInDifferentType(Value *V, const Type *Ty, |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7076 | bool isSigned) { |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7077 | if (Constant *C = dyn_cast<Constant>(V)) |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 7078 | return ConstantExpr::getIntegerCast(C, Ty, isSigned /*Sext or ZExt*/); |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7079 | |
| 7080 | // Otherwise, it must be an instruction. |
| 7081 | Instruction *I = cast<Instruction>(V); |
Chris Lattner | 01859e8 | 2006-05-20 23:14:03 +0000 | [diff] [blame] | 7082 | Instruction *Res = 0; |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7083 | switch (I->getOpcode()) { |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7084 | case Instruction::Add: |
| 7085 | case Instruction::Sub: |
Nick Lewycky | e6b0c00 | 2008-01-22 05:08:48 +0000 | [diff] [blame] | 7086 | case Instruction::Mul: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7087 | case Instruction::And: |
| 7088 | case Instruction::Or: |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7089 | case Instruction::Xor: |
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 7090 | case Instruction::AShr: |
| 7091 | case Instruction::LShr: |
| 7092 | case Instruction::Shl: { |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 7093 | Value *LHS = EvaluateInDifferentType(I->getOperand(0), Ty, isSigned); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7094 | Value *RHS = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned); |
| 7095 | Res = BinaryOperator::create((Instruction::BinaryOps)I->getOpcode(), |
| 7096 | LHS, RHS, I->getName()); |
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 7097 | break; |
| 7098 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7099 | case Instruction::Trunc: |
| 7100 | case Instruction::ZExt: |
| 7101 | case Instruction::SExt: |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7102 | // 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] | 7103 | // just return the source. There's no need to insert it because it is not |
| 7104 | // new. |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7105 | if (I->getOperand(0)->getType() == Ty) |
| 7106 | return I->getOperand(0); |
| 7107 | |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7108 | // Otherwise, must be the same type of case, so just reinsert a new one. |
| 7109 | Res = CastInst::create(cast<CastInst>(I)->getOpcode(), I->getOperand(0), |
| 7110 | Ty, I->getName()); |
| 7111 | break; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7112 | default: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7113 | // TODO: Can handle more cases here. |
| 7114 | assert(0 && "Unreachable!"); |
| 7115 | break; |
| 7116 | } |
| 7117 | |
| 7118 | return InsertNewInstBefore(Res, *I); |
| 7119 | } |
| 7120 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7121 | /// @brief Implement the transforms common to all CastInst visitors. |
| 7122 | Instruction *InstCombiner::commonCastTransforms(CastInst &CI) { |
Chris Lattner | 79d35b3 | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 7123 | Value *Src = CI.getOperand(0); |
| 7124 | |
Dan Gohman | 23d9d27 | 2007-05-11 21:10:54 +0000 | [diff] [blame] | 7125 | // 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] | 7126 | // eliminate it now. |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 7127 | if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7128 | if (Instruction::CastOps opc = |
| 7129 | isEliminableCastPair(CSrc, CI.getOpcode(), CI.getType(), TD)) { |
| 7130 | // The first cast (CSrc) is eliminable so we need to fix up or replace |
| 7131 | // the second cast (CI). CSrc will then have a good chance of being dead. |
| 7132 | return CastInst::create(opc, CSrc->getOperand(0), CI.getType()); |
Chris Lattner | 8fd217c | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 7133 | } |
| 7134 | } |
Chris Lattner | a710ddc | 2004-05-25 04:29:21 +0000 | [diff] [blame] | 7135 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7136 | // 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] | 7137 | if (SelectInst *SI = dyn_cast<SelectInst>(Src)) |
| 7138 | if (Instruction *NV = FoldOpIntoSelect(CI, SI, this)) |
| 7139 | return NV; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7140 | |
| 7141 | // 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] | 7142 | if (isa<PHINode>(Src)) |
| 7143 | if (Instruction *NV = FoldOpIntoPhi(CI)) |
| 7144 | return NV; |
Chris Lattner | 9fb9213 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 7145 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7146 | return 0; |
| 7147 | } |
| 7148 | |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7149 | /// @brief Implement the transforms for cast of pointer (bitcast/ptrtoint) |
| 7150 | Instruction *InstCombiner::commonPointerCastTransforms(CastInst &CI) { |
| 7151 | Value *Src = CI.getOperand(0); |
| 7152 | |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7153 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Src)) { |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7154 | // If casting the result of a getelementptr instruction with no offset, turn |
| 7155 | // this into a cast of the original pointer! |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7156 | if (GEP->hasAllZeroIndices()) { |
| 7157 | // Changing the cast operand is usually not a good idea but it is safe |
| 7158 | // here because the pointer operand is being replaced with another |
| 7159 | // pointer operand so the opcode doesn't need to change. |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7160 | AddToWorkList(GEP); |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7161 | CI.setOperand(0, GEP->getOperand(0)); |
| 7162 | return &CI; |
| 7163 | } |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7164 | |
| 7165 | // If the GEP has a single use, and the base pointer is a bitcast, and the |
| 7166 | // GEP computes a constant offset, see if we can convert these three |
| 7167 | // instructions into fewer. This typically happens with unions and other |
| 7168 | // non-type-safe code. |
| 7169 | if (GEP->hasOneUse() && isa<BitCastInst>(GEP->getOperand(0))) { |
| 7170 | if (GEP->hasAllConstantIndices()) { |
| 7171 | // We are guaranteed to get a constant from EmitGEPOffset. |
| 7172 | ConstantInt *OffsetV = cast<ConstantInt>(EmitGEPOffset(GEP, CI, *this)); |
| 7173 | int64_t Offset = OffsetV->getSExtValue(); |
| 7174 | |
| 7175 | // Get the base pointer input of the bitcast, and the type it points to. |
| 7176 | Value *OrigBase = cast<BitCastInst>(GEP->getOperand(0))->getOperand(0); |
| 7177 | const Type *GEPIdxTy = |
| 7178 | cast<PointerType>(OrigBase->getType())->getElementType(); |
| 7179 | if (GEPIdxTy->isSized()) { |
| 7180 | SmallVector<Value*, 8> NewIndices; |
| 7181 | |
Chris Lattner | c42e226 | 2007-05-05 01:59:31 +0000 | [diff] [blame] | 7182 | // Start with the index over the outer type. Note that the type size |
| 7183 | // might be zero (even if the offset isn't zero) if the indexed type |
| 7184 | // is something like [0 x {int, int}] |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7185 | const Type *IntPtrTy = TD->getIntPtrType(); |
Chris Lattner | c42e226 | 2007-05-05 01:59:31 +0000 | [diff] [blame] | 7186 | int64_t FirstIdx = 0; |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 7187 | if (int64_t TySize = TD->getABITypeSize(GEPIdxTy)) { |
Chris Lattner | c42e226 | 2007-05-05 01:59:31 +0000 | [diff] [blame] | 7188 | FirstIdx = Offset/TySize; |
| 7189 | Offset %= TySize; |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7190 | |
Chris Lattner | c42e226 | 2007-05-05 01:59:31 +0000 | [diff] [blame] | 7191 | // Handle silly modulus not returning values values [0..TySize). |
| 7192 | if (Offset < 0) { |
| 7193 | --FirstIdx; |
| 7194 | Offset += TySize; |
| 7195 | assert(Offset >= 0); |
| 7196 | } |
Chris Lattner | d717c18 | 2007-05-05 22:32:24 +0000 | [diff] [blame] | 7197 | assert((uint64_t)Offset < (uint64_t)TySize &&"Out of range offset"); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7198 | } |
| 7199 | |
| 7200 | NewIndices.push_back(ConstantInt::get(IntPtrTy, FirstIdx)); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7201 | |
| 7202 | // Index into the types. If we fail, set OrigBase to null. |
| 7203 | while (Offset) { |
| 7204 | if (const StructType *STy = dyn_cast<StructType>(GEPIdxTy)) { |
| 7205 | const StructLayout *SL = TD->getStructLayout(STy); |
Chris Lattner | 6b6aef8 | 2007-05-15 00:16:00 +0000 | [diff] [blame] | 7206 | if (Offset < (int64_t)SL->getSizeInBytes()) { |
| 7207 | unsigned Elt = SL->getElementContainingOffset(Offset); |
| 7208 | NewIndices.push_back(ConstantInt::get(Type::Int32Ty, Elt)); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7209 | |
Chris Lattner | 6b6aef8 | 2007-05-15 00:16:00 +0000 | [diff] [blame] | 7210 | Offset -= SL->getElementOffset(Elt); |
| 7211 | GEPIdxTy = STy->getElementType(Elt); |
| 7212 | } else { |
| 7213 | // Otherwise, we can't index into this, bail out. |
| 7214 | Offset = 0; |
| 7215 | OrigBase = 0; |
| 7216 | } |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7217 | } else if (isa<ArrayType>(GEPIdxTy) || isa<VectorType>(GEPIdxTy)) { |
| 7218 | const SequentialType *STy = cast<SequentialType>(GEPIdxTy); |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 7219 | if (uint64_t EltSize = TD->getABITypeSize(STy->getElementType())){ |
Chris Lattner | 6b6aef8 | 2007-05-15 00:16:00 +0000 | [diff] [blame] | 7220 | NewIndices.push_back(ConstantInt::get(IntPtrTy,Offset/EltSize)); |
| 7221 | Offset %= EltSize; |
| 7222 | } else { |
| 7223 | NewIndices.push_back(ConstantInt::get(IntPtrTy, 0)); |
| 7224 | } |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7225 | GEPIdxTy = STy->getElementType(); |
| 7226 | } else { |
| 7227 | // Otherwise, we can't index into this, bail out. |
| 7228 | Offset = 0; |
| 7229 | OrigBase = 0; |
| 7230 | } |
| 7231 | } |
| 7232 | if (OrigBase) { |
| 7233 | // If we were able to index down into an element, create the GEP |
| 7234 | // and bitcast the result. This eliminates one bitcast, potentially |
| 7235 | // two. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 7236 | Instruction *NGEP = GetElementPtrInst::Create(OrigBase, |
| 7237 | NewIndices.begin(), |
| 7238 | NewIndices.end(), ""); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7239 | InsertNewInstBefore(NGEP, CI); |
| 7240 | NGEP->takeName(GEP); |
| 7241 | |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7242 | if (isa<BitCastInst>(CI)) |
| 7243 | return new BitCastInst(NGEP, CI.getType()); |
| 7244 | assert(isa<PtrToIntInst>(CI)); |
| 7245 | return new PtrToIntInst(NGEP, CI.getType()); |
| 7246 | } |
| 7247 | } |
| 7248 | } |
| 7249 | } |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7250 | } |
| 7251 | |
| 7252 | return commonCastTransforms(CI); |
| 7253 | } |
| 7254 | |
| 7255 | |
| 7256 | |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7257 | /// Only the TRUNC, ZEXT, SEXT, and BITCAST can both operand and result as |
| 7258 | /// integer types. This function implements the common transforms for all those |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7259 | /// cases. |
| 7260 | /// @brief Implement the transforms common to CastInst with integer operands |
| 7261 | Instruction *InstCombiner::commonIntCastTransforms(CastInst &CI) { |
| 7262 | if (Instruction *Result = commonCastTransforms(CI)) |
| 7263 | return Result; |
| 7264 | |
| 7265 | Value *Src = CI.getOperand(0); |
| 7266 | const Type *SrcTy = Src->getType(); |
| 7267 | const Type *DestTy = CI.getType(); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 7268 | uint32_t SrcBitSize = SrcTy->getPrimitiveSizeInBits(); |
| 7269 | uint32_t DestBitSize = DestTy->getPrimitiveSizeInBits(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7270 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7271 | // See if we can simplify any instructions used by the LHS whose sole |
| 7272 | // purpose is to compute bits we don't care about. |
Reid Spencer | ad6676e | 2007-03-22 20:56:53 +0000 | [diff] [blame] | 7273 | APInt KnownZero(DestBitSize, 0), KnownOne(DestBitSize, 0); |
| 7274 | if (SimplifyDemandedBits(&CI, APInt::getAllOnesValue(DestBitSize), |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7275 | KnownZero, KnownOne)) |
| 7276 | return &CI; |
| 7277 | |
| 7278 | // If the source isn't an instruction or has more than one use then we |
| 7279 | // can't do anything more. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7280 | Instruction *SrcI = dyn_cast<Instruction>(Src); |
| 7281 | if (!SrcI || !Src->hasOneUse()) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7282 | return 0; |
| 7283 | |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7284 | // Attempt to propagate the cast into the instruction for int->int casts. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7285 | int NumCastsRemoved = 0; |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7286 | if (!isa<BitCastInst>(CI) && |
| 7287 | CanEvaluateInDifferentType(SrcI, cast<IntegerType>(DestTy), |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7288 | CI.getOpcode(), NumCastsRemoved)) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7289 | // If this cast is a truncate, evaluting in a different type always |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7290 | // eliminates the cast, so it is always a win. If this is a zero-extension, |
| 7291 | // we need to do an AND to maintain the clear top-part of the computation, |
| 7292 | // so we require that the input have eliminated at least one cast. If this |
| 7293 | // 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] | 7294 | // require that two casts have been eliminated. |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7295 | bool DoXForm; |
| 7296 | switch (CI.getOpcode()) { |
| 7297 | default: |
| 7298 | // All the others use floating point so we shouldn't actually |
| 7299 | // get here because of the check above. |
| 7300 | assert(0 && "Unknown cast type"); |
| 7301 | case Instruction::Trunc: |
| 7302 | DoXForm = true; |
| 7303 | break; |
| 7304 | case Instruction::ZExt: |
| 7305 | DoXForm = NumCastsRemoved >= 1; |
| 7306 | break; |
| 7307 | case Instruction::SExt: |
| 7308 | DoXForm = NumCastsRemoved >= 2; |
| 7309 | break; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7310 | } |
| 7311 | |
| 7312 | if (DoXForm) { |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 7313 | Value *Res = EvaluateInDifferentType(SrcI, DestTy, |
| 7314 | CI.getOpcode() == Instruction::SExt); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7315 | assert(Res->getType() == DestTy); |
| 7316 | switch (CI.getOpcode()) { |
| 7317 | default: assert(0 && "Unknown cast type!"); |
| 7318 | case Instruction::Trunc: |
| 7319 | case Instruction::BitCast: |
| 7320 | // Just replace this cast with the result. |
| 7321 | return ReplaceInstUsesWith(CI, Res); |
| 7322 | case Instruction::ZExt: { |
| 7323 | // We need to emit an AND to clear the high bits. |
| 7324 | assert(SrcBitSize < DestBitSize && "Not a zext?"); |
Chris Lattner | cd1d6d5 | 2007-04-02 05:48:58 +0000 | [diff] [blame] | 7325 | Constant *C = ConstantInt::get(APInt::getLowBitsSet(DestBitSize, |
| 7326 | SrcBitSize)); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7327 | return BinaryOperator::createAnd(Res, C); |
| 7328 | } |
| 7329 | case Instruction::SExt: |
| 7330 | // We need to emit a cast to truncate, then a cast to sext. |
| 7331 | return CastInst::create(Instruction::SExt, |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7332 | InsertCastBefore(Instruction::Trunc, Res, Src->getType(), |
| 7333 | CI), DestTy); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7334 | } |
| 7335 | } |
| 7336 | } |
| 7337 | |
| 7338 | Value *Op0 = SrcI->getNumOperands() > 0 ? SrcI->getOperand(0) : 0; |
| 7339 | Value *Op1 = SrcI->getNumOperands() > 1 ? SrcI->getOperand(1) : 0; |
| 7340 | |
| 7341 | switch (SrcI->getOpcode()) { |
| 7342 | case Instruction::Add: |
| 7343 | case Instruction::Mul: |
| 7344 | case Instruction::And: |
| 7345 | case Instruction::Or: |
| 7346 | case Instruction::Xor: |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 7347 | // If we are discarding information, rewrite. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7348 | if (DestBitSize <= SrcBitSize && DestBitSize != 1) { |
| 7349 | // Don't insert two casts if they cannot be eliminated. We allow |
| 7350 | // two casts to be inserted if the sizes are the same. This could |
| 7351 | // only be converting signedness, which is a noop. |
| 7352 | if (DestBitSize == SrcBitSize || |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7353 | !ValueRequiresCast(CI.getOpcode(), Op1, DestTy,TD) || |
| 7354 | !ValueRequiresCast(CI.getOpcode(), Op0, DestTy, TD)) { |
Reid Spencer | 7eb7638 | 2006-12-13 17:19:09 +0000 | [diff] [blame] | 7355 | Instruction::CastOps opcode = CI.getOpcode(); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7356 | Value *Op0c = InsertOperandCastBefore(opcode, Op0, DestTy, SrcI); |
| 7357 | Value *Op1c = InsertOperandCastBefore(opcode, Op1, DestTy, SrcI); |
| 7358 | return BinaryOperator::create( |
| 7359 | cast<BinaryOperator>(SrcI)->getOpcode(), Op0c, Op1c); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7360 | } |
| 7361 | } |
| 7362 | |
| 7363 | // cast (xor bool X, true) to int --> xor (cast bool X to int), 1 |
| 7364 | if (isa<ZExtInst>(CI) && SrcBitSize == 1 && |
| 7365 | SrcI->getOpcode() == Instruction::Xor && |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 7366 | Op1 == ConstantInt::getTrue() && |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7367 | (!Op0->hasOneUse() || !isa<CmpInst>(Op0))) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7368 | Value *New = InsertOperandCastBefore(Instruction::ZExt, Op0, DestTy, &CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7369 | return BinaryOperator::createXor(New, ConstantInt::get(CI.getType(), 1)); |
| 7370 | } |
| 7371 | break; |
| 7372 | case Instruction::SDiv: |
| 7373 | case Instruction::UDiv: |
| 7374 | case Instruction::SRem: |
| 7375 | case Instruction::URem: |
| 7376 | // If we are just changing the sign, rewrite. |
| 7377 | if (DestBitSize == SrcBitSize) { |
| 7378 | // Don't insert two casts if they cannot be eliminated. We allow |
| 7379 | // two casts to be inserted if the sizes are the same. This could |
| 7380 | // only be converting signedness, which is a noop. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7381 | if (!ValueRequiresCast(CI.getOpcode(), Op1, DestTy, TD) || |
| 7382 | !ValueRequiresCast(CI.getOpcode(), Op0, DestTy, TD)) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7383 | Value *Op0c = InsertOperandCastBefore(Instruction::BitCast, |
| 7384 | Op0, DestTy, SrcI); |
| 7385 | Value *Op1c = InsertOperandCastBefore(Instruction::BitCast, |
| 7386 | Op1, DestTy, SrcI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7387 | return BinaryOperator::create( |
| 7388 | cast<BinaryOperator>(SrcI)->getOpcode(), Op0c, Op1c); |
| 7389 | } |
| 7390 | } |
| 7391 | break; |
| 7392 | |
| 7393 | case Instruction::Shl: |
| 7394 | // Allow changing the sign of the source operand. Do not allow |
| 7395 | // changing the size of the shift, UNLESS the shift amount is a |
| 7396 | // constant. We must not change variable sized shifts to a smaller |
| 7397 | // size, because it is undefined to shift more bits out than exist |
| 7398 | // in the value. |
| 7399 | if (DestBitSize == SrcBitSize || |
| 7400 | (DestBitSize < SrcBitSize && isa<Constant>(Op1))) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7401 | Instruction::CastOps opcode = (DestBitSize == SrcBitSize ? |
| 7402 | Instruction::BitCast : Instruction::Trunc); |
| 7403 | Value *Op0c = InsertOperandCastBefore(opcode, Op0, DestTy, SrcI); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7404 | Value *Op1c = InsertOperandCastBefore(opcode, Op1, DestTy, SrcI); |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 7405 | return BinaryOperator::createShl(Op0c, Op1c); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7406 | } |
| 7407 | break; |
| 7408 | case Instruction::AShr: |
| 7409 | // If this is a signed shr, and if all bits shifted in are about to be |
| 7410 | // truncated off, turn it into an unsigned shr to allow greater |
| 7411 | // simplifications. |
| 7412 | if (DestBitSize < SrcBitSize && |
| 7413 | isa<ConstantInt>(Op1)) { |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 7414 | uint32_t ShiftAmt = cast<ConstantInt>(Op1)->getLimitedValue(SrcBitSize); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7415 | if (SrcBitSize > ShiftAmt && SrcBitSize-ShiftAmt >= DestBitSize) { |
| 7416 | // Insert the new logical shift right. |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 7417 | return BinaryOperator::createLShr(Op0, Op1); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7418 | } |
| 7419 | } |
| 7420 | break; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7421 | } |
| 7422 | return 0; |
| 7423 | } |
| 7424 | |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 7425 | Instruction *InstCombiner::visitTrunc(TruncInst &CI) { |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 7426 | if (Instruction *Result = commonIntCastTransforms(CI)) |
| 7427 | return Result; |
| 7428 | |
| 7429 | Value *Src = CI.getOperand(0); |
| 7430 | const Type *Ty = CI.getType(); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 7431 | uint32_t DestBitWidth = Ty->getPrimitiveSizeInBits(); |
| 7432 | uint32_t SrcBitWidth = cast<IntegerType>(Src->getType())->getBitWidth(); |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 7433 | |
| 7434 | if (Instruction *SrcI = dyn_cast<Instruction>(Src)) { |
| 7435 | switch (SrcI->getOpcode()) { |
| 7436 | default: break; |
| 7437 | case Instruction::LShr: |
| 7438 | // We can shrink lshr to something smaller if we know the bits shifted in |
| 7439 | // are already zeros. |
| 7440 | if (ConstantInt *ShAmtV = dyn_cast<ConstantInt>(SrcI->getOperand(1))) { |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 7441 | uint32_t ShAmt = ShAmtV->getLimitedValue(SrcBitWidth); |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 7442 | |
| 7443 | // Get a mask for the bits shifting in. |
Zhou Sheng | e82fca0 | 2007-03-28 09:19:01 +0000 | [diff] [blame] | 7444 | APInt Mask(APInt::getLowBitsSet(SrcBitWidth, ShAmt).shl(DestBitWidth)); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7445 | Value* SrcIOp0 = SrcI->getOperand(0); |
| 7446 | if (SrcI->hasOneUse() && MaskedValueIsZero(SrcIOp0, Mask)) { |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 7447 | if (ShAmt >= DestBitWidth) // All zeros. |
| 7448 | return ReplaceInstUsesWith(CI, Constant::getNullValue(Ty)); |
| 7449 | |
| 7450 | // Okay, we can shrink this. Truncate the input, then return a new |
| 7451 | // shift. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7452 | Value *V1 = InsertCastBefore(Instruction::Trunc, SrcIOp0, Ty, CI); |
| 7453 | Value *V2 = InsertCastBefore(Instruction::Trunc, SrcI->getOperand(1), |
| 7454 | Ty, CI); |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 7455 | return BinaryOperator::createLShr(V1, V2); |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 7456 | } |
Chris Lattner | e13ab2a | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 7457 | } else { // This is a variable shr. |
| 7458 | |
| 7459 | // Turn 'trunc (lshr X, Y) to bool' into '(X & (1 << Y)) != 0'. This is |
| 7460 | // more LLVM instructions, but allows '1 << Y' to be hoisted if |
| 7461 | // loop-invariant and CSE'd. |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 7462 | if (CI.getType() == Type::Int1Ty && SrcI->hasOneUse()) { |
Chris Lattner | e13ab2a | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 7463 | Value *One = ConstantInt::get(SrcI->getType(), 1); |
| 7464 | |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7465 | Value *V = InsertNewInstBefore( |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 7466 | BinaryOperator::createShl(One, SrcI->getOperand(1), |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7467 | "tmp"), CI); |
Chris Lattner | e13ab2a | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 7468 | V = InsertNewInstBefore(BinaryOperator::createAnd(V, |
| 7469 | SrcI->getOperand(0), |
| 7470 | "tmp"), CI); |
| 7471 | Value *Zero = Constant::getNullValue(V->getType()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7472 | return new ICmpInst(ICmpInst::ICMP_NE, V, Zero); |
Chris Lattner | e13ab2a | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 7473 | } |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 7474 | } |
| 7475 | break; |
| 7476 | } |
| 7477 | } |
| 7478 | |
| 7479 | return 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7480 | } |
| 7481 | |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 7482 | /// transformZExtICmp - Transform (zext icmp) to bitwise / integer operations |
| 7483 | /// in order to eliminate the icmp. |
| 7484 | Instruction *InstCombiner::transformZExtICmp(ICmpInst *ICI, Instruction &CI, |
| 7485 | bool DoXform) { |
| 7486 | // If we are just checking for a icmp eq of a single bit and zext'ing it |
| 7487 | // to an integer, then shift the bit to the appropriate place and then |
| 7488 | // cast to integer to avoid the comparison. |
| 7489 | if (ConstantInt *Op1C = dyn_cast<ConstantInt>(ICI->getOperand(1))) { |
| 7490 | const APInt &Op1CV = Op1C->getValue(); |
| 7491 | |
| 7492 | // zext (x <s 0) to i32 --> x>>u31 true if signbit set. |
| 7493 | // zext (x >s -1) to i32 --> (x>>u31)^1 true if signbit clear. |
| 7494 | if ((ICI->getPredicate() == ICmpInst::ICMP_SLT && Op1CV == 0) || |
| 7495 | (ICI->getPredicate() == ICmpInst::ICMP_SGT &&Op1CV.isAllOnesValue())) { |
| 7496 | if (!DoXform) return ICI; |
| 7497 | |
| 7498 | Value *In = ICI->getOperand(0); |
| 7499 | Value *Sh = ConstantInt::get(In->getType(), |
| 7500 | In->getType()->getPrimitiveSizeInBits()-1); |
| 7501 | In = InsertNewInstBefore(BinaryOperator::createLShr(In, Sh, |
| 7502 | In->getName()+".lobit"), |
| 7503 | CI); |
| 7504 | if (In->getType() != CI.getType()) |
| 7505 | In = CastInst::createIntegerCast(In, CI.getType(), |
| 7506 | false/*ZExt*/, "tmp", &CI); |
| 7507 | |
| 7508 | if (ICI->getPredicate() == ICmpInst::ICMP_SGT) { |
| 7509 | Constant *One = ConstantInt::get(In->getType(), 1); |
| 7510 | In = InsertNewInstBefore(BinaryOperator::createXor(In, One, |
| 7511 | In->getName()+".not"), |
| 7512 | CI); |
| 7513 | } |
| 7514 | |
| 7515 | return ReplaceInstUsesWith(CI, In); |
| 7516 | } |
| 7517 | |
| 7518 | |
| 7519 | |
| 7520 | // zext (X == 0) to i32 --> X^1 iff X has only the low bit set. |
| 7521 | // zext (X == 0) to i32 --> (X>>1)^1 iff X has only the 2nd bit set. |
| 7522 | // zext (X == 1) to i32 --> X iff X has only the low bit set. |
| 7523 | // zext (X == 2) to i32 --> X>>1 iff X has only the 2nd bit set. |
| 7524 | // zext (X != 0) to i32 --> X iff X has only the low bit set. |
| 7525 | // zext (X != 0) to i32 --> X>>1 iff X has only the 2nd bit set. |
| 7526 | // zext (X != 1) to i32 --> X^1 iff X has only the low bit set. |
| 7527 | // zext (X != 2) to i32 --> (X>>1)^1 iff X has only the 2nd bit set. |
| 7528 | if ((Op1CV == 0 || Op1CV.isPowerOf2()) && |
| 7529 | // This only works for EQ and NE |
| 7530 | ICI->isEquality()) { |
| 7531 | // If Op1C some other power of two, convert: |
| 7532 | uint32_t BitWidth = Op1C->getType()->getBitWidth(); |
| 7533 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 7534 | APInt TypeMask(APInt::getAllOnesValue(BitWidth)); |
| 7535 | ComputeMaskedBits(ICI->getOperand(0), TypeMask, KnownZero, KnownOne); |
| 7536 | |
| 7537 | APInt KnownZeroMask(~KnownZero); |
| 7538 | if (KnownZeroMask.isPowerOf2()) { // Exactly 1 possible 1? |
| 7539 | if (!DoXform) return ICI; |
| 7540 | |
| 7541 | bool isNE = ICI->getPredicate() == ICmpInst::ICMP_NE; |
| 7542 | if (Op1CV != 0 && (Op1CV != KnownZeroMask)) { |
| 7543 | // (X&4) == 2 --> false |
| 7544 | // (X&4) != 2 --> true |
| 7545 | Constant *Res = ConstantInt::get(Type::Int1Ty, isNE); |
| 7546 | Res = ConstantExpr::getZExt(Res, CI.getType()); |
| 7547 | return ReplaceInstUsesWith(CI, Res); |
| 7548 | } |
| 7549 | |
| 7550 | uint32_t ShiftAmt = KnownZeroMask.logBase2(); |
| 7551 | Value *In = ICI->getOperand(0); |
| 7552 | if (ShiftAmt) { |
| 7553 | // Perform a logical shr by shiftamt. |
| 7554 | // Insert the shift to put the result in the low bit. |
| 7555 | In = InsertNewInstBefore(BinaryOperator::createLShr(In, |
| 7556 | ConstantInt::get(In->getType(), ShiftAmt), |
| 7557 | In->getName()+".lobit"), CI); |
| 7558 | } |
| 7559 | |
| 7560 | if ((Op1CV != 0) == isNE) { // Toggle the low bit. |
| 7561 | Constant *One = ConstantInt::get(In->getType(), 1); |
| 7562 | In = BinaryOperator::createXor(In, One, "tmp"); |
| 7563 | InsertNewInstBefore(cast<Instruction>(In), CI); |
| 7564 | } |
| 7565 | |
| 7566 | if (CI.getType() == In->getType()) |
| 7567 | return ReplaceInstUsesWith(CI, In); |
| 7568 | else |
| 7569 | return CastInst::createIntegerCast(In, CI.getType(), false/*ZExt*/); |
| 7570 | } |
| 7571 | } |
| 7572 | } |
| 7573 | |
| 7574 | return 0; |
| 7575 | } |
| 7576 | |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 7577 | Instruction *InstCombiner::visitZExt(ZExtInst &CI) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7578 | // If one of the common conversion will work .. |
| 7579 | if (Instruction *Result = commonIntCastTransforms(CI)) |
| 7580 | return Result; |
| 7581 | |
| 7582 | Value *Src = CI.getOperand(0); |
| 7583 | |
| 7584 | // If this is a cast of a cast |
| 7585 | if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7586 | // If this is a TRUNC followed by a ZEXT then we are dealing with integral |
| 7587 | // types and if the sizes are just right we can convert this into a logical |
| 7588 | // 'and' which will be much cheaper than the pair of casts. |
| 7589 | if (isa<TruncInst>(CSrc)) { |
| 7590 | // Get the sizes of the types involved |
| 7591 | Value *A = CSrc->getOperand(0); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 7592 | uint32_t SrcSize = A->getType()->getPrimitiveSizeInBits(); |
| 7593 | uint32_t MidSize = CSrc->getType()->getPrimitiveSizeInBits(); |
| 7594 | uint32_t DstSize = CI.getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7595 | // If we're actually extending zero bits and the trunc is a no-op |
| 7596 | if (MidSize < DstSize && SrcSize == DstSize) { |
| 7597 | // Replace both of the casts with an And of the type mask. |
Zhou Sheng | e82fca0 | 2007-03-28 09:19:01 +0000 | [diff] [blame] | 7598 | APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize)); |
Reid Spencer | ad6676e | 2007-03-22 20:56:53 +0000 | [diff] [blame] | 7599 | Constant *AndConst = ConstantInt::get(AndValue); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7600 | Instruction *And = |
| 7601 | BinaryOperator::createAnd(CSrc->getOperand(0), AndConst); |
| 7602 | // Unfortunately, if the type changed, we need to cast it back. |
| 7603 | if (And->getType() != CI.getType()) { |
| 7604 | And->setName(CSrc->getName()+".mask"); |
| 7605 | InsertNewInstBefore(And, CI); |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 7606 | And = CastInst::createIntegerCast(And, CI.getType(), false/*ZExt*/); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7607 | } |
| 7608 | return And; |
| 7609 | } |
| 7610 | } |
| 7611 | } |
| 7612 | |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 7613 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(Src)) |
| 7614 | return transformZExtICmp(ICI, CI); |
Chris Lattner | a2e2c9b | 2007-04-11 06:53:04 +0000 | [diff] [blame] | 7615 | |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 7616 | BinaryOperator *SrcI = dyn_cast<BinaryOperator>(Src); |
| 7617 | if (SrcI && SrcI->getOpcode() == Instruction::Or) { |
| 7618 | // zext (or icmp, icmp) --> or (zext icmp), (zext icmp) if at least one |
| 7619 | // of the (zext icmp) will be transformed. |
| 7620 | ICmpInst *LHS = dyn_cast<ICmpInst>(SrcI->getOperand(0)); |
| 7621 | ICmpInst *RHS = dyn_cast<ICmpInst>(SrcI->getOperand(1)); |
| 7622 | if (LHS && RHS && LHS->hasOneUse() && RHS->hasOneUse() && |
| 7623 | (transformZExtICmp(LHS, CI, false) || |
| 7624 | transformZExtICmp(RHS, CI, false))) { |
| 7625 | Value *LCast = InsertCastBefore(Instruction::ZExt, LHS, CI.getType(), CI); |
| 7626 | Value *RCast = InsertCastBefore(Instruction::ZExt, RHS, CI.getType(), CI); |
| 7627 | return BinaryOperator::create(Instruction::Or, LCast, RCast); |
Chris Lattner | 66bc325 | 2007-04-11 05:45:39 +0000 | [diff] [blame] | 7628 | } |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 7629 | } |
| 7630 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7631 | return 0; |
| 7632 | } |
| 7633 | |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 7634 | Instruction *InstCombiner::visitSExt(SExtInst &CI) { |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 7635 | if (Instruction *I = commonIntCastTransforms(CI)) |
| 7636 | return I; |
| 7637 | |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 7638 | Value *Src = CI.getOperand(0); |
| 7639 | |
| 7640 | // sext (x <s 0) -> ashr x, 31 -> all ones if signed |
| 7641 | // sext (x >s -1) -> ashr x, 31 -> all ones if not signed |
| 7642 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(Src)) { |
| 7643 | // If we are just checking for a icmp eq of a single bit and zext'ing it |
| 7644 | // to an integer, then shift the bit to the appropriate place and then |
| 7645 | // cast to integer to avoid the comparison. |
| 7646 | if (ConstantInt *Op1C = dyn_cast<ConstantInt>(ICI->getOperand(1))) { |
| 7647 | const APInt &Op1CV = Op1C->getValue(); |
| 7648 | |
| 7649 | // sext (x <s 0) to i32 --> x>>s31 true if signbit set. |
| 7650 | // sext (x >s -1) to i32 --> (x>>s31)^-1 true if signbit clear. |
| 7651 | if ((ICI->getPredicate() == ICmpInst::ICMP_SLT && Op1CV == 0) || |
| 7652 | (ICI->getPredicate() == ICmpInst::ICMP_SGT &&Op1CV.isAllOnesValue())){ |
| 7653 | Value *In = ICI->getOperand(0); |
| 7654 | Value *Sh = ConstantInt::get(In->getType(), |
| 7655 | In->getType()->getPrimitiveSizeInBits()-1); |
| 7656 | In = InsertNewInstBefore(BinaryOperator::createAShr(In, Sh, |
Chris Lattner | e34e9a2 | 2007-04-14 23:32:02 +0000 | [diff] [blame] | 7657 | In->getName()+".lobit"), |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 7658 | CI); |
| 7659 | if (In->getType() != CI.getType()) |
| 7660 | In = CastInst::createIntegerCast(In, CI.getType(), |
| 7661 | true/*SExt*/, "tmp", &CI); |
| 7662 | |
| 7663 | if (ICI->getPredicate() == ICmpInst::ICMP_SGT) |
| 7664 | In = InsertNewInstBefore(BinaryOperator::createNot(In, |
| 7665 | In->getName()+".not"), CI); |
| 7666 | |
| 7667 | return ReplaceInstUsesWith(CI, In); |
| 7668 | } |
| 7669 | } |
| 7670 | } |
| 7671 | |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 7672 | return 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7673 | } |
| 7674 | |
Chris Lattner | b753065 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 7675 | /// FitsInFPType - Return a Constant* for the specified FP constant if it fits |
| 7676 | /// in the specified FP type without changing its value. |
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame] | 7677 | static Constant *FitsInFPType(ConstantFP *CFP, const fltSemantics &Sem) { |
Chris Lattner | b753065 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 7678 | APFloat F = CFP->getValueAPF(); |
| 7679 | if (F.convert(Sem, APFloat::rmNearestTiesToEven) == APFloat::opOK) |
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame] | 7680 | return ConstantFP::get(F); |
Chris Lattner | b753065 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 7681 | return 0; |
| 7682 | } |
| 7683 | |
| 7684 | /// LookThroughFPExtensions - If this is an fp extension instruction, look |
| 7685 | /// through it until we get the source value. |
| 7686 | static Value *LookThroughFPExtensions(Value *V) { |
| 7687 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 7688 | if (I->getOpcode() == Instruction::FPExt) |
| 7689 | return LookThroughFPExtensions(I->getOperand(0)); |
| 7690 | |
| 7691 | // If this value is a constant, return the constant in the smallest FP type |
| 7692 | // that can accurately represent it. This allows us to turn |
| 7693 | // (float)((double)X+2.0) into x+2.0f. |
| 7694 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) { |
| 7695 | if (CFP->getType() == Type::PPC_FP128Ty) |
| 7696 | return V; // No constant folding of this. |
| 7697 | // See if the value can be truncated to float and then reextended. |
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame] | 7698 | if (Value *V = FitsInFPType(CFP, APFloat::IEEEsingle)) |
Chris Lattner | b753065 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 7699 | return V; |
| 7700 | if (CFP->getType() == Type::DoubleTy) |
| 7701 | return V; // Won't shrink. |
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame] | 7702 | if (Value *V = FitsInFPType(CFP, APFloat::IEEEdouble)) |
Chris Lattner | b753065 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 7703 | return V; |
| 7704 | // Don't try to shrink to various long double types. |
| 7705 | } |
| 7706 | |
| 7707 | return V; |
| 7708 | } |
| 7709 | |
| 7710 | Instruction *InstCombiner::visitFPTrunc(FPTruncInst &CI) { |
| 7711 | if (Instruction *I = commonCastTransforms(CI)) |
| 7712 | return I; |
| 7713 | |
| 7714 | // If we have fptrunc(add (fpextend x), (fpextend y)), where x and y are |
| 7715 | // smaller than the destination type, we can eliminate the truncate by doing |
| 7716 | // the add as the smaller type. This applies to add/sub/mul/div as well as |
| 7717 | // many builtins (sqrt, etc). |
| 7718 | BinaryOperator *OpI = dyn_cast<BinaryOperator>(CI.getOperand(0)); |
| 7719 | if (OpI && OpI->hasOneUse()) { |
| 7720 | switch (OpI->getOpcode()) { |
| 7721 | default: break; |
| 7722 | case Instruction::Add: |
| 7723 | case Instruction::Sub: |
| 7724 | case Instruction::Mul: |
| 7725 | case Instruction::FDiv: |
| 7726 | case Instruction::FRem: |
| 7727 | const Type *SrcTy = OpI->getType(); |
| 7728 | Value *LHSTrunc = LookThroughFPExtensions(OpI->getOperand(0)); |
| 7729 | Value *RHSTrunc = LookThroughFPExtensions(OpI->getOperand(1)); |
| 7730 | if (LHSTrunc->getType() != SrcTy && |
| 7731 | RHSTrunc->getType() != SrcTy) { |
| 7732 | unsigned DstSize = CI.getType()->getPrimitiveSizeInBits(); |
| 7733 | // If the source types were both smaller than the destination type of |
| 7734 | // the cast, do this xform. |
| 7735 | if (LHSTrunc->getType()->getPrimitiveSizeInBits() <= DstSize && |
| 7736 | RHSTrunc->getType()->getPrimitiveSizeInBits() <= DstSize) { |
| 7737 | LHSTrunc = InsertCastBefore(Instruction::FPExt, LHSTrunc, |
| 7738 | CI.getType(), CI); |
| 7739 | RHSTrunc = InsertCastBefore(Instruction::FPExt, RHSTrunc, |
| 7740 | CI.getType(), CI); |
| 7741 | return BinaryOperator::create(OpI->getOpcode(), LHSTrunc, RHSTrunc); |
| 7742 | } |
| 7743 | } |
| 7744 | break; |
| 7745 | } |
| 7746 | } |
| 7747 | return 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7748 | } |
| 7749 | |
| 7750 | Instruction *InstCombiner::visitFPExt(CastInst &CI) { |
| 7751 | return commonCastTransforms(CI); |
| 7752 | } |
| 7753 | |
| 7754 | Instruction *InstCombiner::visitFPToUI(CastInst &CI) { |
Reid Spencer | 44c030a | 2006-11-30 23:13:36 +0000 | [diff] [blame] | 7755 | return commonCastTransforms(CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7756 | } |
| 7757 | |
| 7758 | Instruction *InstCombiner::visitFPToSI(CastInst &CI) { |
Reid Spencer | 44c030a | 2006-11-30 23:13:36 +0000 | [diff] [blame] | 7759 | return commonCastTransforms(CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7760 | } |
| 7761 | |
| 7762 | Instruction *InstCombiner::visitUIToFP(CastInst &CI) { |
| 7763 | return commonCastTransforms(CI); |
| 7764 | } |
| 7765 | |
| 7766 | Instruction *InstCombiner::visitSIToFP(CastInst &CI) { |
| 7767 | return commonCastTransforms(CI); |
| 7768 | } |
| 7769 | |
| 7770 | Instruction *InstCombiner::visitPtrToInt(CastInst &CI) { |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7771 | return commonPointerCastTransforms(CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7772 | } |
| 7773 | |
Chris Lattner | f9d9e45 | 2008-01-08 07:23:51 +0000 | [diff] [blame] | 7774 | Instruction *InstCombiner::visitIntToPtr(IntToPtrInst &CI) { |
| 7775 | if (Instruction *I = commonCastTransforms(CI)) |
| 7776 | return I; |
| 7777 | |
| 7778 | const Type *DestPointee = cast<PointerType>(CI.getType())->getElementType(); |
| 7779 | if (!DestPointee->isSized()) return 0; |
| 7780 | |
| 7781 | // If this is inttoptr(add (ptrtoint x), cst), try to turn this into a GEP. |
| 7782 | ConstantInt *Cst; |
| 7783 | Value *X; |
| 7784 | if (match(CI.getOperand(0), m_Add(m_Cast<PtrToIntInst>(m_Value(X)), |
| 7785 | m_ConstantInt(Cst)))) { |
| 7786 | // If the source and destination operands have the same type, see if this |
| 7787 | // is a single-index GEP. |
| 7788 | if (X->getType() == CI.getType()) { |
| 7789 | // Get the size of the pointee type. |
Bill Wendling | b9d4f8d | 2008-03-14 05:12:19 +0000 | [diff] [blame] | 7790 | uint64_t Size = TD->getABITypeSize(DestPointee); |
Chris Lattner | f9d9e45 | 2008-01-08 07:23:51 +0000 | [diff] [blame] | 7791 | |
| 7792 | // Convert the constant to intptr type. |
| 7793 | APInt Offset = Cst->getValue(); |
| 7794 | Offset.sextOrTrunc(TD->getPointerSizeInBits()); |
| 7795 | |
| 7796 | // If Offset is evenly divisible by Size, we can do this xform. |
| 7797 | if (Size && !APIntOps::srem(Offset, APInt(Offset.getBitWidth(), Size))){ |
| 7798 | Offset = APIntOps::sdiv(Offset, APInt(Offset.getBitWidth(), Size)); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 7799 | return GetElementPtrInst::Create(X, ConstantInt::get(Offset)); |
Chris Lattner | f9d9e45 | 2008-01-08 07:23:51 +0000 | [diff] [blame] | 7800 | } |
| 7801 | } |
| 7802 | // TODO: Could handle other cases, e.g. where add is indexing into field of |
| 7803 | // struct etc. |
| 7804 | } else if (CI.getOperand(0)->hasOneUse() && |
| 7805 | match(CI.getOperand(0), m_Add(m_Value(X), m_ConstantInt(Cst)))) { |
| 7806 | // Otherwise, if this is inttoptr(add x, cst), try to turn this into an |
| 7807 | // "inttoptr+GEP" instead of "add+intptr". |
| 7808 | |
| 7809 | // Get the size of the pointee type. |
| 7810 | uint64_t Size = TD->getABITypeSize(DestPointee); |
| 7811 | |
| 7812 | // Convert the constant to intptr type. |
| 7813 | APInt Offset = Cst->getValue(); |
| 7814 | Offset.sextOrTrunc(TD->getPointerSizeInBits()); |
| 7815 | |
| 7816 | // If Offset is evenly divisible by Size, we can do this xform. |
| 7817 | if (Size && !APIntOps::srem(Offset, APInt(Offset.getBitWidth(), Size))){ |
| 7818 | Offset = APIntOps::sdiv(Offset, APInt(Offset.getBitWidth(), Size)); |
| 7819 | |
| 7820 | Instruction *P = InsertNewInstBefore(new IntToPtrInst(X, CI.getType(), |
| 7821 | "tmp"), CI); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 7822 | return GetElementPtrInst::Create(P, ConstantInt::get(Offset), "tmp"); |
Chris Lattner | f9d9e45 | 2008-01-08 07:23:51 +0000 | [diff] [blame] | 7823 | } |
| 7824 | } |
| 7825 | return 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7826 | } |
| 7827 | |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7828 | Instruction *InstCombiner::visitBitCast(BitCastInst &CI) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7829 | // If the operands are integer typed then apply the integer transforms, |
| 7830 | // otherwise just apply the common ones. |
| 7831 | Value *Src = CI.getOperand(0); |
| 7832 | const Type *SrcTy = Src->getType(); |
| 7833 | const Type *DestTy = CI.getType(); |
| 7834 | |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 7835 | if (SrcTy->isInteger() && DestTy->isInteger()) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7836 | if (Instruction *Result = commonIntCastTransforms(CI)) |
| 7837 | return Result; |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7838 | } else if (isa<PointerType>(SrcTy)) { |
| 7839 | if (Instruction *I = commonPointerCastTransforms(CI)) |
| 7840 | return I; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7841 | } else { |
| 7842 | if (Instruction *Result = commonCastTransforms(CI)) |
| 7843 | return Result; |
| 7844 | } |
| 7845 | |
| 7846 | |
| 7847 | // Get rid of casts from one type to the same type. These are useless and can |
| 7848 | // be replaced by the operand. |
| 7849 | if (DestTy == Src->getType()) |
| 7850 | return ReplaceInstUsesWith(CI, Src); |
| 7851 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7852 | if (const PointerType *DstPTy = dyn_cast<PointerType>(DestTy)) { |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7853 | const PointerType *SrcPTy = cast<PointerType>(SrcTy); |
| 7854 | const Type *DstElTy = DstPTy->getElementType(); |
| 7855 | const Type *SrcElTy = SrcPTy->getElementType(); |
| 7856 | |
Nate Begeman | 83ad90a | 2008-03-31 00:22:16 +0000 | [diff] [blame] | 7857 | // If the address spaces don't match, don't eliminate the bitcast, which is |
| 7858 | // required for changing types. |
| 7859 | if (SrcPTy->getAddressSpace() != DstPTy->getAddressSpace()) |
| 7860 | return 0; |
| 7861 | |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7862 | // If we are casting a malloc or alloca to a pointer to a type of the same |
| 7863 | // size, rewrite the allocation instruction to allocate the "right" type. |
| 7864 | if (AllocationInst *AI = dyn_cast<AllocationInst>(Src)) |
| 7865 | if (Instruction *V = PromoteCastOfAllocation(CI, *AI)) |
| 7866 | return V; |
| 7867 | |
Chris Lattner | d717c18 | 2007-05-05 22:32:24 +0000 | [diff] [blame] | 7868 | // If the source and destination are pointers, and this cast is equivalent |
| 7869 | // 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] | 7870 | // This can enhance SROA and other transforms that want type-safe pointers. |
| 7871 | Constant *ZeroUInt = Constant::getNullValue(Type::Int32Ty); |
| 7872 | unsigned NumZeros = 0; |
| 7873 | while (SrcElTy != DstElTy && |
| 7874 | isa<CompositeType>(SrcElTy) && !isa<PointerType>(SrcElTy) && |
| 7875 | SrcElTy->getNumContainedTypes() /* not "{}" */) { |
| 7876 | SrcElTy = cast<CompositeType>(SrcElTy)->getTypeAtIndex(ZeroUInt); |
| 7877 | ++NumZeros; |
| 7878 | } |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 7879 | |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7880 | // If we found a path from the src to dest, create the getelementptr now. |
| 7881 | if (SrcElTy == DstElTy) { |
| 7882 | SmallVector<Value*, 8> Idxs(NumZeros+1, ZeroUInt); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 7883 | return GetElementPtrInst::Create(Src, Idxs.begin(), Idxs.end(), "", |
| 7884 | ((Instruction*) NULL)); |
Chris Lattner | 9fb9213 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 7885 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7886 | } |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 7887 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7888 | if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(Src)) { |
| 7889 | if (SVI->hasOneUse()) { |
| 7890 | // Okay, we have (bitconvert (shuffle ..)). Check to see if this is |
| 7891 | // a bitconvert to a vector with the same # elts. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 7892 | if (isa<VectorType>(DestTy) && |
| 7893 | cast<VectorType>(DestTy)->getNumElements() == |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7894 | SVI->getType()->getNumElements()) { |
| 7895 | CastInst *Tmp; |
| 7896 | // If either of the operands is a cast from CI.getType(), then |
| 7897 | // evaluating the shuffle in the casted destination's type will allow |
| 7898 | // us to eliminate at least one cast. |
| 7899 | if (((Tmp = dyn_cast<CastInst>(SVI->getOperand(0))) && |
| 7900 | Tmp->getOperand(0)->getType() == DestTy) || |
| 7901 | ((Tmp = dyn_cast<CastInst>(SVI->getOperand(1))) && |
| 7902 | Tmp->getOperand(0)->getType() == DestTy)) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7903 | Value *LHS = InsertOperandCastBefore(Instruction::BitCast, |
| 7904 | SVI->getOperand(0), DestTy, &CI); |
| 7905 | Value *RHS = InsertOperandCastBefore(Instruction::BitCast, |
| 7906 | SVI->getOperand(1), DestTy, &CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7907 | // Return a new shuffle vector. Use the same element ID's, as we |
| 7908 | // know the vector types match #elts. |
| 7909 | return new ShuffleVectorInst(LHS, RHS, SVI->getOperand(2)); |
Chris Lattner | 01575b7 | 2006-05-25 23:24:33 +0000 | [diff] [blame] | 7910 | } |
| 7911 | } |
| 7912 | } |
| 7913 | } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 7914 | return 0; |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 7915 | } |
| 7916 | |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7917 | /// GetSelectFoldableOperands - We want to turn code that looks like this: |
| 7918 | /// %C = or %A, %B |
| 7919 | /// %D = select %cond, %C, %A |
| 7920 | /// into: |
| 7921 | /// %C = select %cond, %B, 0 |
| 7922 | /// %D = or %A, %C |
| 7923 | /// |
| 7924 | /// Assuming that the specified instruction is an operand to the select, return |
| 7925 | /// a bitmask indicating which operands of this instruction are foldable if they |
| 7926 | /// equal the other incoming value of the select. |
| 7927 | /// |
| 7928 | static unsigned GetSelectFoldableOperands(Instruction *I) { |
| 7929 | switch (I->getOpcode()) { |
| 7930 | case Instruction::Add: |
| 7931 | case Instruction::Mul: |
| 7932 | case Instruction::And: |
| 7933 | case Instruction::Or: |
| 7934 | case Instruction::Xor: |
| 7935 | return 3; // Can fold through either operand. |
| 7936 | case Instruction::Sub: // Can only fold on the amount subtracted. |
| 7937 | case Instruction::Shl: // Can only fold on the shift amount. |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 7938 | case Instruction::LShr: |
| 7939 | case Instruction::AShr: |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7940 | return 1; |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7941 | default: |
| 7942 | return 0; // Cannot fold |
| 7943 | } |
| 7944 | } |
| 7945 | |
| 7946 | /// GetSelectFoldableConstant - For the same transformation as the previous |
| 7947 | /// function, return the identity constant that goes into the select. |
| 7948 | static Constant *GetSelectFoldableConstant(Instruction *I) { |
| 7949 | switch (I->getOpcode()) { |
| 7950 | default: assert(0 && "This cannot happen!"); abort(); |
| 7951 | case Instruction::Add: |
| 7952 | case Instruction::Sub: |
| 7953 | case Instruction::Or: |
| 7954 | case Instruction::Xor: |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7955 | case Instruction::Shl: |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 7956 | case Instruction::LShr: |
| 7957 | case Instruction::AShr: |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7958 | return Constant::getNullValue(I->getType()); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7959 | case Instruction::And: |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 7960 | return Constant::getAllOnesValue(I->getType()); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7961 | case Instruction::Mul: |
| 7962 | return ConstantInt::get(I->getType(), 1); |
| 7963 | } |
| 7964 | } |
| 7965 | |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 7966 | /// FoldSelectOpOp - Here we have (select c, TI, FI), and we know that TI and FI |
| 7967 | /// have the same opcode and only one use each. Try to simplify this. |
| 7968 | Instruction *InstCombiner::FoldSelectOpOp(SelectInst &SI, Instruction *TI, |
| 7969 | Instruction *FI) { |
| 7970 | if (TI->getNumOperands() == 1) { |
| 7971 | // If this is a non-volatile load or a cast from the same type, |
| 7972 | // merge. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7973 | if (TI->isCast()) { |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 7974 | if (TI->getOperand(0)->getType() != FI->getOperand(0)->getType()) |
| 7975 | return 0; |
| 7976 | } else { |
| 7977 | return 0; // unknown unary op. |
| 7978 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7979 | |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 7980 | // Fold this by inserting a select from the input values. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 7981 | SelectInst *NewSI = SelectInst::Create(SI.getCondition(), TI->getOperand(0), |
| 7982 | FI->getOperand(0), SI.getName()+".v"); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 7983 | InsertNewInstBefore(NewSI, SI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7984 | return CastInst::create(Instruction::CastOps(TI->getOpcode()), NewSI, |
| 7985 | TI->getType()); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 7986 | } |
| 7987 | |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7988 | // Only handle binary operators here. |
| 7989 | if (!isa<BinaryOperator>(TI)) |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 7990 | return 0; |
| 7991 | |
| 7992 | // Figure out if the operations have any operands in common. |
| 7993 | Value *MatchOp, *OtherOpT, *OtherOpF; |
| 7994 | bool MatchIsOpZero; |
| 7995 | if (TI->getOperand(0) == FI->getOperand(0)) { |
| 7996 | MatchOp = TI->getOperand(0); |
| 7997 | OtherOpT = TI->getOperand(1); |
| 7998 | OtherOpF = FI->getOperand(1); |
| 7999 | MatchIsOpZero = true; |
| 8000 | } else if (TI->getOperand(1) == FI->getOperand(1)) { |
| 8001 | MatchOp = TI->getOperand(1); |
| 8002 | OtherOpT = TI->getOperand(0); |
| 8003 | OtherOpF = FI->getOperand(0); |
| 8004 | MatchIsOpZero = false; |
| 8005 | } else if (!TI->isCommutative()) { |
| 8006 | return 0; |
| 8007 | } else if (TI->getOperand(0) == FI->getOperand(1)) { |
| 8008 | MatchOp = TI->getOperand(0); |
| 8009 | OtherOpT = TI->getOperand(1); |
| 8010 | OtherOpF = FI->getOperand(0); |
| 8011 | MatchIsOpZero = true; |
| 8012 | } else if (TI->getOperand(1) == FI->getOperand(0)) { |
| 8013 | MatchOp = TI->getOperand(1); |
| 8014 | OtherOpT = TI->getOperand(0); |
| 8015 | OtherOpF = FI->getOperand(1); |
| 8016 | MatchIsOpZero = true; |
| 8017 | } else { |
| 8018 | return 0; |
| 8019 | } |
| 8020 | |
| 8021 | // If we reach here, they do have operations in common. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 8022 | SelectInst *NewSI = SelectInst::Create(SI.getCondition(), OtherOpT, |
| 8023 | OtherOpF, SI.getName()+".v"); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8024 | InsertNewInstBefore(NewSI, SI); |
| 8025 | |
| 8026 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TI)) { |
| 8027 | if (MatchIsOpZero) |
| 8028 | return BinaryOperator::create(BO->getOpcode(), MatchOp, NewSI); |
| 8029 | else |
| 8030 | return BinaryOperator::create(BO->getOpcode(), NewSI, MatchOp); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8031 | } |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 8032 | assert(0 && "Shouldn't get here"); |
| 8033 | return 0; |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8034 | } |
| 8035 | |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 8036 | Instruction *InstCombiner::visitSelectInst(SelectInst &SI) { |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 8037 | Value *CondVal = SI.getCondition(); |
| 8038 | Value *TrueVal = SI.getTrueValue(); |
| 8039 | Value *FalseVal = SI.getFalseValue(); |
| 8040 | |
| 8041 | // select true, X, Y -> X |
| 8042 | // select false, X, Y -> Y |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 8043 | if (ConstantInt *C = dyn_cast<ConstantInt>(CondVal)) |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 8044 | return ReplaceInstUsesWith(SI, C->getZExtValue() ? TrueVal : FalseVal); |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 8045 | |
| 8046 | // select C, X, X -> X |
| 8047 | if (TrueVal == FalseVal) |
| 8048 | return ReplaceInstUsesWith(SI, TrueVal); |
| 8049 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8050 | if (isa<UndefValue>(TrueVal)) // select C, undef, X -> X |
| 8051 | return ReplaceInstUsesWith(SI, FalseVal); |
| 8052 | if (isa<UndefValue>(FalseVal)) // select C, X, undef -> X |
| 8053 | return ReplaceInstUsesWith(SI, TrueVal); |
| 8054 | if (isa<UndefValue>(CondVal)) { // select undef, X, Y -> X or Y |
| 8055 | if (isa<Constant>(TrueVal)) |
| 8056 | return ReplaceInstUsesWith(SI, TrueVal); |
| 8057 | else |
| 8058 | return ReplaceInstUsesWith(SI, FalseVal); |
| 8059 | } |
| 8060 | |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 8061 | if (SI.getType() == Type::Int1Ty) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 8062 | if (ConstantInt *C = dyn_cast<ConstantInt>(TrueVal)) { |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 8063 | if (C->getZExtValue()) { |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 8064 | // Change: A = select B, true, C --> A = or B, C |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 8065 | return BinaryOperator::createOr(CondVal, FalseVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 8066 | } else { |
| 8067 | // Change: A = select B, false, C --> A = and !B, C |
| 8068 | Value *NotCond = |
| 8069 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
| 8070 | "not."+CondVal->getName()), SI); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 8071 | return BinaryOperator::createAnd(NotCond, FalseVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 8072 | } |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 8073 | } else if (ConstantInt *C = dyn_cast<ConstantInt>(FalseVal)) { |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 8074 | if (C->getZExtValue() == false) { |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 8075 | // Change: A = select B, C, false --> A = and B, C |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 8076 | return BinaryOperator::createAnd(CondVal, TrueVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 8077 | } else { |
| 8078 | // Change: A = select B, C, true --> A = or !B, C |
| 8079 | Value *NotCond = |
| 8080 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
| 8081 | "not."+CondVal->getName()), SI); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 8082 | return BinaryOperator::createOr(NotCond, TrueVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 8083 | } |
| 8084 | } |
Chris Lattner | cfa5975 | 2007-11-25 21:27:53 +0000 | [diff] [blame] | 8085 | |
| 8086 | // select a, b, a -> a&b |
| 8087 | // select a, a, b -> a|b |
| 8088 | if (CondVal == TrueVal) |
| 8089 | return BinaryOperator::createOr(CondVal, FalseVal); |
| 8090 | else if (CondVal == FalseVal) |
| 8091 | return BinaryOperator::createAnd(CondVal, TrueVal); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 8092 | } |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 8093 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 8094 | // Selecting between two integer constants? |
| 8095 | if (ConstantInt *TrueValC = dyn_cast<ConstantInt>(TrueVal)) |
| 8096 | if (ConstantInt *FalseValC = dyn_cast<ConstantInt>(FalseVal)) { |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 8097 | // select C, 1, 0 -> zext C to int |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 8098 | if (FalseValC->isZero() && TrueValC->getValue() == 1) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8099 | return CastInst::create(Instruction::ZExt, CondVal, SI.getType()); |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 8100 | } else if (TrueValC->isZero() && FalseValC->getValue() == 1) { |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 8101 | // select C, 0, 1 -> zext !C to int |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 8102 | Value *NotCond = |
| 8103 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
Chris Lattner | 82e14fe | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 8104 | "not."+CondVal->getName()), SI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8105 | return CastInst::create(Instruction::ZExt, NotCond, SI.getType()); |
Chris Lattner | 82e14fe | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 8106 | } |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 8107 | |
| 8108 | // FIXME: Turn select 0/-1 and -1/0 into sext from condition! |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 8109 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8110 | if (ICmpInst *IC = dyn_cast<ICmpInst>(SI.getCondition())) { |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 8111 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8112 | // (x <s 0) ? -1 : 0 -> ashr x, 31 |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 8113 | if (TrueValC->isAllOnesValue() && FalseValC->isZero()) |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 8114 | if (ConstantInt *CmpCst = dyn_cast<ConstantInt>(IC->getOperand(1))) { |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 8115 | if (IC->getPredicate() == ICmpInst::ICMP_SLT && CmpCst->isZero()) { |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 8116 | // The comparison constant and the result are not neccessarily the |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8117 | // same width. Make an all-ones value by inserting a AShr. |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 8118 | Value *X = IC->getOperand(0); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 8119 | uint32_t Bits = X->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 8120 | Constant *ShAmt = ConstantInt::get(X->getType(), Bits-1); |
| 8121 | Instruction *SRA = BinaryOperator::create(Instruction::AShr, X, |
| 8122 | ShAmt, "ones"); |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 8123 | InsertNewInstBefore(SRA, SI); |
| 8124 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8125 | // Finally, convert to the type of the select RHS. We figure out |
| 8126 | // if this requires a SExt, Trunc or BitCast based on the sizes. |
| 8127 | Instruction::CastOps opc = Instruction::BitCast; |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 8128 | uint32_t SRASize = SRA->getType()->getPrimitiveSizeInBits(); |
| 8129 | uint32_t SISize = SI.getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8130 | if (SRASize < SISize) |
| 8131 | opc = Instruction::SExt; |
| 8132 | else if (SRASize > SISize) |
| 8133 | opc = Instruction::Trunc; |
| 8134 | return CastInst::create(opc, SRA, SI.getType()); |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 8135 | } |
| 8136 | } |
| 8137 | |
| 8138 | |
| 8139 | // 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] | 8140 | // 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] | 8141 | // non-constant value, eliminate this whole mess. This corresponds to |
| 8142 | // cases like this: ((X & 27) ? 27 : 0) |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 8143 | if (TrueValC->isZero() || FalseValC->isZero()) |
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 8144 | if (IC->isEquality() && isa<ConstantInt>(IC->getOperand(1)) && |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 8145 | cast<Constant>(IC->getOperand(1))->isNullValue()) |
| 8146 | if (Instruction *ICA = dyn_cast<Instruction>(IC->getOperand(0))) |
| 8147 | if (ICA->getOpcode() == Instruction::And && |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8148 | isa<ConstantInt>(ICA->getOperand(1)) && |
| 8149 | (ICA->getOperand(1) == TrueValC || |
| 8150 | ICA->getOperand(1) == FalseValC) && |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 8151 | isOneBitSet(cast<ConstantInt>(ICA->getOperand(1)))) { |
| 8152 | // 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] | 8153 | // know whether we have a icmp_ne or icmp_eq and whether the |
| 8154 | // true or false val is the zero. |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 8155 | bool ShouldNotVal = !TrueValC->isZero(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8156 | ShouldNotVal ^= IC->getPredicate() == ICmpInst::ICMP_NE; |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 8157 | Value *V = ICA; |
| 8158 | if (ShouldNotVal) |
| 8159 | V = InsertNewInstBefore(BinaryOperator::create( |
| 8160 | Instruction::Xor, V, ICA->getOperand(1)), SI); |
| 8161 | return ReplaceInstUsesWith(SI, V); |
| 8162 | } |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 8163 | } |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 8164 | } |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8165 | |
| 8166 | // 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] | 8167 | if (FCmpInst *FCI = dyn_cast<FCmpInst>(CondVal)) { |
| 8168 | if (FCI->getOperand(0) == TrueVal && FCI->getOperand(1) == FalseVal) { |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8169 | // Transform (X == Y) ? X : Y -> Y |
Dale Johannesen | 5a2174f | 2007-10-03 17:45:27 +0000 | [diff] [blame] | 8170 | if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) { |
| 8171 | // This is not safe in general for floating point: |
| 8172 | // consider X== -0, Y== +0. |
| 8173 | // It becomes safe if either operand is a nonzero constant. |
| 8174 | ConstantFP *CFPt, *CFPf; |
| 8175 | if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) && |
| 8176 | !CFPt->getValueAPF().isZero()) || |
| 8177 | ((CFPf = dyn_cast<ConstantFP>(FalseVal)) && |
| 8178 | !CFPf->getValueAPF().isZero())) |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8179 | return ReplaceInstUsesWith(SI, FalseVal); |
Dale Johannesen | 5a2174f | 2007-10-03 17:45:27 +0000 | [diff] [blame] | 8180 | } |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8181 | // Transform (X != Y) ? X : Y -> X |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8182 | if (FCI->getPredicate() == FCmpInst::FCMP_ONE) |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8183 | return ReplaceInstUsesWith(SI, TrueVal); |
| 8184 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 8185 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8186 | } else if (FCI->getOperand(0) == FalseVal && FCI->getOperand(1) == TrueVal){ |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8187 | // Transform (X == Y) ? Y : X -> X |
Dale Johannesen | 5a2174f | 2007-10-03 17:45:27 +0000 | [diff] [blame] | 8188 | if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) { |
| 8189 | // This is not safe in general for floating point: |
| 8190 | // consider X== -0, Y== +0. |
| 8191 | // It becomes safe if either operand is a nonzero constant. |
| 8192 | ConstantFP *CFPt, *CFPf; |
| 8193 | if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) && |
| 8194 | !CFPt->getValueAPF().isZero()) || |
| 8195 | ((CFPf = dyn_cast<ConstantFP>(FalseVal)) && |
| 8196 | !CFPf->getValueAPF().isZero())) |
| 8197 | return ReplaceInstUsesWith(SI, FalseVal); |
| 8198 | } |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8199 | // Transform (X != Y) ? Y : X -> Y |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8200 | if (FCI->getPredicate() == FCmpInst::FCMP_ONE) |
| 8201 | return ReplaceInstUsesWith(SI, TrueVal); |
| 8202 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 8203 | } |
| 8204 | } |
| 8205 | |
| 8206 | // See if we are selecting two values based on a comparison of the two values. |
| 8207 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(CondVal)) { |
| 8208 | if (ICI->getOperand(0) == TrueVal && ICI->getOperand(1) == FalseVal) { |
| 8209 | // Transform (X == Y) ? X : Y -> Y |
| 8210 | if (ICI->getPredicate() == ICmpInst::ICMP_EQ) |
| 8211 | return ReplaceInstUsesWith(SI, FalseVal); |
| 8212 | // Transform (X != Y) ? X : Y -> X |
| 8213 | if (ICI->getPredicate() == ICmpInst::ICMP_NE) |
| 8214 | return ReplaceInstUsesWith(SI, TrueVal); |
| 8215 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 8216 | |
| 8217 | } else if (ICI->getOperand(0) == FalseVal && ICI->getOperand(1) == TrueVal){ |
| 8218 | // Transform (X == Y) ? Y : X -> X |
| 8219 | if (ICI->getPredicate() == ICmpInst::ICMP_EQ) |
| 8220 | return ReplaceInstUsesWith(SI, FalseVal); |
| 8221 | // Transform (X != Y) ? Y : X -> Y |
| 8222 | if (ICI->getPredicate() == ICmpInst::ICMP_NE) |
Chris Lattner | fbede52 | 2004-04-11 01:39:19 +0000 | [diff] [blame] | 8223 | return ReplaceInstUsesWith(SI, TrueVal); |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8224 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 8225 | } |
| 8226 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8227 | |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 8228 | if (Instruction *TI = dyn_cast<Instruction>(TrueVal)) |
| 8229 | if (Instruction *FI = dyn_cast<Instruction>(FalseVal)) |
| 8230 | if (TI->hasOneUse() && FI->hasOneUse()) { |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 8231 | Instruction *AddOp = 0, *SubOp = 0; |
| 8232 | |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8233 | // Turn (select C, (op X, Y), (op X, Z)) -> (op X, (select C, Y, Z)) |
| 8234 | if (TI->getOpcode() == FI->getOpcode()) |
| 8235 | if (Instruction *IV = FoldSelectOpOp(SI, TI, FI)) |
| 8236 | return IV; |
| 8237 | |
| 8238 | // Turn select C, (X+Y), (X-Y) --> (X+(select C, Y, (-Y))). This is |
| 8239 | // even legal for FP. |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 8240 | if (TI->getOpcode() == Instruction::Sub && |
| 8241 | FI->getOpcode() == Instruction::Add) { |
| 8242 | AddOp = FI; SubOp = TI; |
| 8243 | } else if (FI->getOpcode() == Instruction::Sub && |
| 8244 | TI->getOpcode() == Instruction::Add) { |
| 8245 | AddOp = TI; SubOp = FI; |
| 8246 | } |
| 8247 | |
| 8248 | if (AddOp) { |
| 8249 | Value *OtherAddOp = 0; |
| 8250 | if (SubOp->getOperand(0) == AddOp->getOperand(0)) { |
| 8251 | OtherAddOp = AddOp->getOperand(1); |
| 8252 | } else if (SubOp->getOperand(0) == AddOp->getOperand(1)) { |
| 8253 | OtherAddOp = AddOp->getOperand(0); |
| 8254 | } |
| 8255 | |
| 8256 | if (OtherAddOp) { |
Chris Lattner | 97f37a4 | 2006-02-24 18:05:58 +0000 | [diff] [blame] | 8257 | // So at this point we know we have (Y -> OtherAddOp): |
| 8258 | // select C, (add X, Y), (sub X, Z) |
| 8259 | Value *NegVal; // Compute -Z |
| 8260 | if (Constant *C = dyn_cast<Constant>(SubOp->getOperand(1))) { |
| 8261 | NegVal = ConstantExpr::getNeg(C); |
| 8262 | } else { |
| 8263 | NegVal = InsertNewInstBefore( |
| 8264 | BinaryOperator::createNeg(SubOp->getOperand(1), "tmp"), SI); |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 8265 | } |
Chris Lattner | 97f37a4 | 2006-02-24 18:05:58 +0000 | [diff] [blame] | 8266 | |
| 8267 | Value *NewTrueOp = OtherAddOp; |
| 8268 | Value *NewFalseOp = NegVal; |
| 8269 | if (AddOp != TI) |
| 8270 | std::swap(NewTrueOp, NewFalseOp); |
| 8271 | Instruction *NewSel = |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 8272 | SelectInst::Create(CondVal, NewTrueOp,NewFalseOp,SI.getName()+".p"); |
Chris Lattner | 97f37a4 | 2006-02-24 18:05:58 +0000 | [diff] [blame] | 8273 | |
| 8274 | NewSel = InsertNewInstBefore(NewSel, SI); |
| 8275 | return BinaryOperator::createAdd(SubOp->getOperand(0), NewSel); |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 8276 | } |
| 8277 | } |
| 8278 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8279 | |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8280 | // See if we can fold the select into one of our operands. |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 8281 | if (SI.getType()->isInteger()) { |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8282 | // See the comment above GetSelectFoldableOperands for a description of the |
| 8283 | // transformation we are doing here. |
| 8284 | if (Instruction *TVI = dyn_cast<Instruction>(TrueVal)) |
| 8285 | if (TVI->hasOneUse() && TVI->getNumOperands() == 2 && |
| 8286 | !isa<Constant>(FalseVal)) |
| 8287 | if (unsigned SFO = GetSelectFoldableOperands(TVI)) { |
| 8288 | unsigned OpToFold = 0; |
| 8289 | if ((SFO & 1) && FalseVal == TVI->getOperand(0)) { |
| 8290 | OpToFold = 1; |
| 8291 | } else if ((SFO & 2) && FalseVal == TVI->getOperand(1)) { |
| 8292 | OpToFold = 2; |
| 8293 | } |
| 8294 | |
| 8295 | if (OpToFold) { |
| 8296 | Constant *C = GetSelectFoldableConstant(TVI); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8297 | Instruction *NewSel = |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 8298 | SelectInst::Create(SI.getCondition(), TVI->getOperand(2-OpToFold), C); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8299 | InsertNewInstBefore(NewSel, SI); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 8300 | NewSel->takeName(TVI); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8301 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TVI)) |
| 8302 | return BinaryOperator::create(BO->getOpcode(), FalseVal, NewSel); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8303 | else { |
| 8304 | assert(0 && "Unknown instruction!!"); |
| 8305 | } |
| 8306 | } |
| 8307 | } |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 8308 | |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8309 | if (Instruction *FVI = dyn_cast<Instruction>(FalseVal)) |
| 8310 | if (FVI->hasOneUse() && FVI->getNumOperands() == 2 && |
| 8311 | !isa<Constant>(TrueVal)) |
| 8312 | if (unsigned SFO = GetSelectFoldableOperands(FVI)) { |
| 8313 | unsigned OpToFold = 0; |
| 8314 | if ((SFO & 1) && TrueVal == FVI->getOperand(0)) { |
| 8315 | OpToFold = 1; |
| 8316 | } else if ((SFO & 2) && TrueVal == FVI->getOperand(1)) { |
| 8317 | OpToFold = 2; |
| 8318 | } |
| 8319 | |
| 8320 | if (OpToFold) { |
| 8321 | Constant *C = GetSelectFoldableConstant(FVI); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8322 | Instruction *NewSel = |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 8323 | SelectInst::Create(SI.getCondition(), C, FVI->getOperand(2-OpToFold)); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8324 | InsertNewInstBefore(NewSel, SI); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 8325 | NewSel->takeName(FVI); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8326 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(FVI)) |
| 8327 | return BinaryOperator::create(BO->getOpcode(), TrueVal, NewSel); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 8328 | else |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8329 | assert(0 && "Unknown instruction!!"); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8330 | } |
| 8331 | } |
| 8332 | } |
Chris Lattner | a1df33c | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 8333 | |
| 8334 | if (BinaryOperator::isNot(CondVal)) { |
| 8335 | SI.setOperand(0, BinaryOperator::getNotArgument(CondVal)); |
| 8336 | SI.setOperand(1, FalseVal); |
| 8337 | SI.setOperand(2, TrueVal); |
| 8338 | return &SI; |
| 8339 | } |
| 8340 | |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 8341 | return 0; |
| 8342 | } |
| 8343 | |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8344 | /// EnforceKnownAlignment - If the specified pointer points to an object that |
| 8345 | /// we control, modify the object's alignment to PrefAlign. This isn't |
| 8346 | /// often possible though. If alignment is important, a more reliable approach |
| 8347 | /// is to simply align all global variables and allocation instructions to |
| 8348 | /// their preferred alignment from the beginning. |
| 8349 | /// |
| 8350 | static unsigned EnforceKnownAlignment(Value *V, |
| 8351 | unsigned Align, unsigned PrefAlign) { |
Chris Lattner | f2369f2 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 8352 | |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8353 | User *U = dyn_cast<User>(V); |
| 8354 | if (!U) return Align; |
| 8355 | |
| 8356 | switch (getOpcode(U)) { |
| 8357 | default: break; |
| 8358 | case Instruction::BitCast: |
| 8359 | return EnforceKnownAlignment(U->getOperand(0), Align, PrefAlign); |
| 8360 | case Instruction::GetElementPtr: { |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8361 | // If all indexes are zero, it is just the alignment of the base pointer. |
| 8362 | bool AllZeroOperands = true; |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8363 | for (unsigned i = 1, e = U->getNumOperands(); i != e; ++i) |
| 8364 | if (!isa<Constant>(U->getOperand(i)) || |
| 8365 | !cast<Constant>(U->getOperand(i))->isNullValue()) { |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8366 | AllZeroOperands = false; |
| 8367 | break; |
| 8368 | } |
Chris Lattner | f2369f2 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 8369 | |
| 8370 | if (AllZeroOperands) { |
| 8371 | // Treat this like a bitcast. |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8372 | return EnforceKnownAlignment(U->getOperand(0), Align, PrefAlign); |
Chris Lattner | f2369f2 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 8373 | } |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8374 | break; |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8375 | } |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8376 | } |
| 8377 | |
| 8378 | if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
| 8379 | // If there is a large requested alignment and we can, bump up the alignment |
| 8380 | // of the global. |
| 8381 | if (!GV->isDeclaration()) { |
| 8382 | GV->setAlignment(PrefAlign); |
| 8383 | Align = PrefAlign; |
| 8384 | } |
| 8385 | } else if (AllocationInst *AI = dyn_cast<AllocationInst>(V)) { |
| 8386 | // If there is a requested alignment and if this is an alloca, round up. We |
| 8387 | // don't do this for malloc, because some systems can't respect the request. |
| 8388 | if (isa<AllocaInst>(AI)) { |
| 8389 | AI->setAlignment(PrefAlign); |
| 8390 | Align = PrefAlign; |
| 8391 | } |
| 8392 | } |
| 8393 | |
| 8394 | return Align; |
| 8395 | } |
| 8396 | |
| 8397 | /// GetOrEnforceKnownAlignment - If the specified pointer has an alignment that |
| 8398 | /// we can determine, return it, otherwise return 0. If PrefAlign is specified, |
| 8399 | /// and it is more than the alignment of the ultimate object, see if we can |
| 8400 | /// increase the alignment of the ultimate object, making this check succeed. |
| 8401 | unsigned InstCombiner::GetOrEnforceKnownAlignment(Value *V, |
| 8402 | unsigned PrefAlign) { |
| 8403 | unsigned BitWidth = TD ? TD->getTypeSizeInBits(V->getType()) : |
| 8404 | sizeof(PrefAlign) * CHAR_BIT; |
| 8405 | APInt Mask = APInt::getAllOnesValue(BitWidth); |
| 8406 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 8407 | ComputeMaskedBits(V, Mask, KnownZero, KnownOne); |
| 8408 | unsigned TrailZ = KnownZero.countTrailingOnes(); |
| 8409 | unsigned Align = 1u << std::min(BitWidth - 1, TrailZ); |
| 8410 | |
| 8411 | if (PrefAlign > Align) |
| 8412 | Align = EnforceKnownAlignment(V, Align, PrefAlign); |
| 8413 | |
| 8414 | // We don't need to make any adjustment. |
| 8415 | return Align; |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8416 | } |
| 8417 | |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8418 | Instruction *InstCombiner::SimplifyMemTransfer(MemIntrinsic *MI) { |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8419 | unsigned DstAlign = GetOrEnforceKnownAlignment(MI->getOperand(1)); |
| 8420 | unsigned SrcAlign = GetOrEnforceKnownAlignment(MI->getOperand(2)); |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8421 | unsigned MinAlign = std::min(DstAlign, SrcAlign); |
| 8422 | unsigned CopyAlign = MI->getAlignment()->getZExtValue(); |
| 8423 | |
| 8424 | if (CopyAlign < MinAlign) { |
| 8425 | MI->setAlignment(ConstantInt::get(Type::Int32Ty, MinAlign)); |
| 8426 | return MI; |
| 8427 | } |
| 8428 | |
| 8429 | // If MemCpyInst length is 1/2/4/8 bytes then replace memcpy with |
| 8430 | // load/store. |
| 8431 | ConstantInt *MemOpLength = dyn_cast<ConstantInt>(MI->getOperand(3)); |
| 8432 | if (MemOpLength == 0) return 0; |
| 8433 | |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 8434 | // Source and destination pointer types are always "i8*" for intrinsic. See |
| 8435 | // if the size is something we can handle with a single primitive load/store. |
| 8436 | // A single load+store correctly handles overlapping memory in the memmove |
| 8437 | // case. |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8438 | unsigned Size = MemOpLength->getZExtValue(); |
| 8439 | if (Size == 0 || Size > 8 || (Size&(Size-1))) |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 8440 | return 0; // If not 1/2/4/8 bytes, exit. |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8441 | |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 8442 | // Use an integer load+store unless we can find something better. |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8443 | Type *NewPtrTy = PointerType::getUnqual(IntegerType::get(Size<<3)); |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 8444 | |
| 8445 | // Memcpy forces the use of i8* for the source and destination. That means |
| 8446 | // that if you're using memcpy to move one double around, you'll get a cast |
| 8447 | // from double* to i8*. We'd much rather use a double load+store rather than |
| 8448 | // an i64 load+store, here because this improves the odds that the source or |
| 8449 | // dest address will be promotable. See if we can find a better type than the |
| 8450 | // integer datatype. |
| 8451 | if (Value *Op = getBitCastOperand(MI->getOperand(1))) { |
| 8452 | const Type *SrcETy = cast<PointerType>(Op->getType())->getElementType(); |
| 8453 | if (SrcETy->isSized() && TD->getTypeStoreSize(SrcETy) == Size) { |
| 8454 | // The SrcETy might be something like {{{double}}} or [1 x double]. Rip |
| 8455 | // down through these levels if so. |
| 8456 | while (!SrcETy->isFirstClassType()) { |
| 8457 | if (const StructType *STy = dyn_cast<StructType>(SrcETy)) { |
| 8458 | if (STy->getNumElements() == 1) |
| 8459 | SrcETy = STy->getElementType(0); |
| 8460 | else |
| 8461 | break; |
| 8462 | } else if (const ArrayType *ATy = dyn_cast<ArrayType>(SrcETy)) { |
| 8463 | if (ATy->getNumElements() == 1) |
| 8464 | SrcETy = ATy->getElementType(); |
| 8465 | else |
| 8466 | break; |
| 8467 | } else |
| 8468 | break; |
| 8469 | } |
| 8470 | |
| 8471 | if (SrcETy->isFirstClassType()) |
| 8472 | NewPtrTy = PointerType::getUnqual(SrcETy); |
| 8473 | } |
| 8474 | } |
| 8475 | |
| 8476 | |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8477 | // If the memcpy/memmove provides better alignment info than we can |
| 8478 | // infer, use it. |
| 8479 | SrcAlign = std::max(SrcAlign, CopyAlign); |
| 8480 | DstAlign = std::max(DstAlign, CopyAlign); |
| 8481 | |
| 8482 | Value *Src = InsertBitCastBefore(MI->getOperand(2), NewPtrTy, *MI); |
| 8483 | Value *Dest = InsertBitCastBefore(MI->getOperand(1), NewPtrTy, *MI); |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 8484 | Instruction *L = new LoadInst(Src, "tmp", false, SrcAlign); |
| 8485 | InsertNewInstBefore(L, *MI); |
| 8486 | InsertNewInstBefore(new StoreInst(L, Dest, false, DstAlign), *MI); |
| 8487 | |
| 8488 | // Set the size of the copy to 0, it will be deleted on the next iteration. |
| 8489 | MI->setOperand(3, Constant::getNullValue(MemOpLength->getType())); |
| 8490 | return MI; |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8491 | } |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 8492 | |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 8493 | /// visitCallInst - CallInst simplification. This mostly only handles folding |
| 8494 | /// of intrinsic instructions. For normal calls, it allows visitCallSite to do |
| 8495 | /// the heavy lifting. |
| 8496 | /// |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8497 | Instruction *InstCombiner::visitCallInst(CallInst &CI) { |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 8498 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(&CI); |
| 8499 | if (!II) return visitCallSite(&CI); |
| 8500 | |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 8501 | // Intrinsics cannot occur in an invoke, so handle them here instead of in |
| 8502 | // visitCallSite. |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 8503 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(II)) { |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8504 | bool Changed = false; |
| 8505 | |
| 8506 | // memmove/cpy/set of zero bytes is a noop. |
| 8507 | if (Constant *NumBytes = dyn_cast<Constant>(MI->getLength())) { |
| 8508 | if (NumBytes->isNullValue()) return EraseInstFromFunction(CI); |
| 8509 | |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8510 | if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes)) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8511 | if (CI->getZExtValue() == 1) { |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8512 | // Replace the instruction with just byte operations. We would |
| 8513 | // transform other cases to loads/stores, but we don't know if |
| 8514 | // alignment is sufficient. |
| 8515 | } |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 8516 | } |
| 8517 | |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8518 | // If we have a memmove and the source operation is a constant global, |
| 8519 | // then the source and dest pointers can't alias, so we can change this |
| 8520 | // into a call to memcpy. |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8521 | if (MemMoveInst *MMI = dyn_cast<MemMoveInst>(MI)) { |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8522 | if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource())) |
| 8523 | if (GVSrc->isConstant()) { |
| 8524 | Module *M = CI.getParent()->getParent()->getParent(); |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 8525 | Intrinsic::ID MemCpyID; |
| 8526 | if (CI.getOperand(3)->getType() == Type::Int32Ty) |
| 8527 | MemCpyID = Intrinsic::memcpy_i32; |
Chris Lattner | 2195939 | 2006-03-03 01:34:17 +0000 | [diff] [blame] | 8528 | else |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 8529 | MemCpyID = Intrinsic::memcpy_i64; |
| 8530 | CI.setOperand(0, Intrinsic::getDeclaration(M, MemCpyID)); |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8531 | Changed = true; |
| 8532 | } |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8533 | } |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8534 | |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8535 | // If we can determine a pointer alignment that is bigger than currently |
| 8536 | // set, update the alignment. |
| 8537 | if (isa<MemCpyInst>(MI) || isa<MemMoveInst>(MI)) { |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8538 | if (Instruction *I = SimplifyMemTransfer(MI)) |
| 8539 | return I; |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8540 | } else if (isa<MemSetInst>(MI)) { |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8541 | unsigned Alignment = GetOrEnforceKnownAlignment(MI->getDest()); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8542 | if (MI->getAlignment()->getZExtValue() < Alignment) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8543 | MI->setAlignment(ConstantInt::get(Type::Int32Ty, Alignment)); |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8544 | Changed = true; |
| 8545 | } |
| 8546 | } |
| 8547 | |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 8548 | if (Changed) return II; |
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 8549 | } else { |
| 8550 | switch (II->getIntrinsicID()) { |
| 8551 | default: break; |
Chris Lattner | 82ed58f | 2006-04-02 05:30:25 +0000 | [diff] [blame] | 8552 | case Intrinsic::ppc_altivec_lvx: |
| 8553 | case Intrinsic::ppc_altivec_lvxl: |
Chris Lattner | fd6bdf0 | 2006-04-17 22:26:56 +0000 | [diff] [blame] | 8554 | case Intrinsic::x86_sse_loadu_ps: |
| 8555 | case Intrinsic::x86_sse2_loadu_pd: |
| 8556 | case Intrinsic::x86_sse2_loadu_dq: |
| 8557 | // Turn PPC lvx -> load if the pointer is known aligned. |
| 8558 | // Turn X86 loadups -> load if the pointer is known aligned. |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8559 | if (GetOrEnforceKnownAlignment(II->getOperand(1), 16) >= 16) { |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 8560 | Value *Ptr = InsertBitCastBefore(II->getOperand(1), |
| 8561 | PointerType::getUnqual(II->getType()), |
| 8562 | CI); |
Chris Lattner | 82ed58f | 2006-04-02 05:30:25 +0000 | [diff] [blame] | 8563 | return new LoadInst(Ptr); |
| 8564 | } |
| 8565 | break; |
| 8566 | case Intrinsic::ppc_altivec_stvx: |
| 8567 | case Intrinsic::ppc_altivec_stvxl: |
| 8568 | // Turn stvx -> store if the pointer is known aligned. |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8569 | if (GetOrEnforceKnownAlignment(II->getOperand(2), 16) >= 16) { |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 8570 | const Type *OpPtrTy = |
| 8571 | PointerType::getUnqual(II->getOperand(1)->getType()); |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 8572 | Value *Ptr = InsertBitCastBefore(II->getOperand(2), OpPtrTy, CI); |
Chris Lattner | 82ed58f | 2006-04-02 05:30:25 +0000 | [diff] [blame] | 8573 | return new StoreInst(II->getOperand(1), Ptr); |
| 8574 | } |
| 8575 | break; |
Chris Lattner | fd6bdf0 | 2006-04-17 22:26:56 +0000 | [diff] [blame] | 8576 | case Intrinsic::x86_sse_storeu_ps: |
| 8577 | case Intrinsic::x86_sse2_storeu_pd: |
| 8578 | case Intrinsic::x86_sse2_storeu_dq: |
| 8579 | case Intrinsic::x86_sse2_storel_dq: |
| 8580 | // Turn X86 storeu -> store if the pointer is known aligned. |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8581 | if (GetOrEnforceKnownAlignment(II->getOperand(1), 16) >= 16) { |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 8582 | const Type *OpPtrTy = |
| 8583 | PointerType::getUnqual(II->getOperand(2)->getType()); |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 8584 | Value *Ptr = InsertBitCastBefore(II->getOperand(1), OpPtrTy, CI); |
Chris Lattner | fd6bdf0 | 2006-04-17 22:26:56 +0000 | [diff] [blame] | 8585 | return new StoreInst(II->getOperand(2), Ptr); |
| 8586 | } |
| 8587 | break; |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 8588 | |
| 8589 | case Intrinsic::x86_sse_cvttss2si: { |
| 8590 | // These intrinsics only demands the 0th element of its input vector. If |
| 8591 | // we can simplify the input based on that, do so now. |
| 8592 | uint64_t UndefElts; |
| 8593 | if (Value *V = SimplifyDemandedVectorElts(II->getOperand(1), 1, |
| 8594 | UndefElts)) { |
| 8595 | II->setOperand(1, V); |
| 8596 | return II; |
| 8597 | } |
| 8598 | break; |
| 8599 | } |
| 8600 | |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8601 | case Intrinsic::ppc_altivec_vperm: |
| 8602 | // Turn vperm(V1,V2,mask) -> shuffle(V1,V2,mask) if mask is a constant. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 8603 | if (ConstantVector *Mask = dyn_cast<ConstantVector>(II->getOperand(3))) { |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8604 | assert(Mask->getNumOperands() == 16 && "Bad type for intrinsic!"); |
| 8605 | |
| 8606 | // Check that all of the elements are integer constants or undefs. |
| 8607 | bool AllEltsOk = true; |
| 8608 | for (unsigned i = 0; i != 16; ++i) { |
| 8609 | if (!isa<ConstantInt>(Mask->getOperand(i)) && |
| 8610 | !isa<UndefValue>(Mask->getOperand(i))) { |
| 8611 | AllEltsOk = false; |
| 8612 | break; |
| 8613 | } |
| 8614 | } |
| 8615 | |
| 8616 | if (AllEltsOk) { |
| 8617 | // Cast the input vectors to byte vectors. |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 8618 | Value *Op0 =InsertBitCastBefore(II->getOperand(1),Mask->getType(),CI); |
| 8619 | Value *Op1 =InsertBitCastBefore(II->getOperand(2),Mask->getType(),CI); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8620 | Value *Result = UndefValue::get(Op0->getType()); |
| 8621 | |
| 8622 | // Only extract each element once. |
| 8623 | Value *ExtractedElts[32]; |
| 8624 | memset(ExtractedElts, 0, sizeof(ExtractedElts)); |
| 8625 | |
| 8626 | for (unsigned i = 0; i != 16; ++i) { |
| 8627 | if (isa<UndefValue>(Mask->getOperand(i))) |
| 8628 | continue; |
Chris Lattner | e34e9a2 | 2007-04-14 23:32:02 +0000 | [diff] [blame] | 8629 | unsigned Idx=cast<ConstantInt>(Mask->getOperand(i))->getZExtValue(); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8630 | Idx &= 31; // Match the hardware behavior. |
| 8631 | |
| 8632 | if (ExtractedElts[Idx] == 0) { |
| 8633 | Instruction *Elt = |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 8634 | new ExtractElementInst(Idx < 16 ? Op0 : Op1, Idx&15, "tmp"); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8635 | InsertNewInstBefore(Elt, CI); |
| 8636 | ExtractedElts[Idx] = Elt; |
| 8637 | } |
| 8638 | |
| 8639 | // Insert this value into the result vector. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 8640 | Result = InsertElementInst::Create(Result, ExtractedElts[Idx], i, "tmp"); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8641 | InsertNewInstBefore(cast<Instruction>(Result), CI); |
| 8642 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8643 | return CastInst::create(Instruction::BitCast, Result, CI.getType()); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8644 | } |
| 8645 | } |
| 8646 | break; |
| 8647 | |
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 8648 | case Intrinsic::stackrestore: { |
| 8649 | // If the save is right next to the restore, remove the restore. This can |
| 8650 | // happen when variable allocas are DCE'd. |
| 8651 | if (IntrinsicInst *SS = dyn_cast<IntrinsicInst>(II->getOperand(1))) { |
| 8652 | if (SS->getIntrinsicID() == Intrinsic::stacksave) { |
| 8653 | BasicBlock::iterator BI = SS; |
| 8654 | if (&*++BI == II) |
| 8655 | return EraseInstFromFunction(CI); |
| 8656 | } |
| 8657 | } |
| 8658 | |
Chris Lattner | bf1d8a7 | 2008-02-18 06:12:38 +0000 | [diff] [blame] | 8659 | // Scan down this block to see if there is another stack restore in the |
| 8660 | // same block without an intervening call/alloca. |
| 8661 | BasicBlock::iterator BI = II; |
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 8662 | TerminatorInst *TI = II->getParent()->getTerminator(); |
Chris Lattner | bf1d8a7 | 2008-02-18 06:12:38 +0000 | [diff] [blame] | 8663 | bool CannotRemove = false; |
| 8664 | for (++BI; &*BI != TI; ++BI) { |
| 8665 | if (isa<AllocaInst>(BI)) { |
| 8666 | CannotRemove = true; |
| 8667 | break; |
| 8668 | } |
| 8669 | if (isa<CallInst>(BI)) { |
| 8670 | if (!isa<IntrinsicInst>(BI)) { |
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 8671 | CannotRemove = true; |
| 8672 | break; |
| 8673 | } |
Chris Lattner | bf1d8a7 | 2008-02-18 06:12:38 +0000 | [diff] [blame] | 8674 | // If there is a stackrestore below this one, remove this one. |
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 8675 | return EraseInstFromFunction(CI); |
Chris Lattner | bf1d8a7 | 2008-02-18 06:12:38 +0000 | [diff] [blame] | 8676 | } |
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 8677 | } |
Chris Lattner | bf1d8a7 | 2008-02-18 06:12:38 +0000 | [diff] [blame] | 8678 | |
| 8679 | // If the stack restore is in a return/unwind block and if there are no |
| 8680 | // allocas or calls between the restore and the return, nuke the restore. |
| 8681 | if (!CannotRemove && (isa<ReturnInst>(TI) || isa<UnwindInst>(TI))) |
| 8682 | return EraseInstFromFunction(CI); |
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 8683 | break; |
| 8684 | } |
| 8685 | } |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8686 | } |
| 8687 | |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 8688 | return visitCallSite(II); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8689 | } |
| 8690 | |
| 8691 | // InvokeInst simplification |
| 8692 | // |
| 8693 | Instruction *InstCombiner::visitInvokeInst(InvokeInst &II) { |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 8694 | return visitCallSite(&II); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8695 | } |
| 8696 | |
Dale Johannesen | 1f530a5 | 2008-04-23 18:34:37 +0000 | [diff] [blame] | 8697 | // If this cast does not affect the value passed through the varargs |
| 8698 | // area, we can eliminate the use of the cast. |
| 8699 | static bool isSafeToEliminateVarargsCast(const CallSite CS, |
| 8700 | const CastInst * const CI, |
| 8701 | const TargetData * const TD, |
| 8702 | const int ix) { |
| 8703 | if (!CI->isLosslessCast()) |
| 8704 | return false; |
| 8705 | |
| 8706 | // The size of ByVal arguments is derived from the type, so we |
| 8707 | // can't change to a type with a different size. If the size were |
| 8708 | // passed explicitly we could avoid this check. |
| 8709 | if (!CS.paramHasAttr(ix, ParamAttr::ByVal)) |
| 8710 | return true; |
| 8711 | |
| 8712 | const Type* SrcTy = |
| 8713 | cast<PointerType>(CI->getOperand(0)->getType())->getElementType(); |
| 8714 | const Type* DstTy = cast<PointerType>(CI->getType())->getElementType(); |
| 8715 | if (!SrcTy->isSized() || !DstTy->isSized()) |
| 8716 | return false; |
| 8717 | if (TD->getABITypeSize(SrcTy) != TD->getABITypeSize(DstTy)) |
| 8718 | return false; |
| 8719 | return true; |
| 8720 | } |
| 8721 | |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 8722 | // visitCallSite - Improvements for call and invoke instructions. |
| 8723 | // |
| 8724 | Instruction *InstCombiner::visitCallSite(CallSite CS) { |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8725 | bool Changed = false; |
| 8726 | |
| 8727 | // If the callee is a constexpr cast of a function, attempt to move the cast |
| 8728 | // to the arguments of the call/invoke. |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 8729 | if (transformConstExprCastCall(CS)) return 0; |
| 8730 | |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8731 | Value *Callee = CS.getCalledValue(); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8732 | |
Chris Lattner | 08b22ec | 2005-05-13 07:09:09 +0000 | [diff] [blame] | 8733 | if (Function *CalleeF = dyn_cast<Function>(Callee)) |
| 8734 | if (CalleeF->getCallingConv() != CS.getCallingConv()) { |
| 8735 | Instruction *OldCall = CS.getInstruction(); |
| 8736 | // If the call and callee calling conventions don't match, this call must |
| 8737 | // be unreachable, as the call is undefined. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 8738 | new StoreInst(ConstantInt::getTrue(), |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 8739 | UndefValue::get(PointerType::getUnqual(Type::Int1Ty)), |
| 8740 | OldCall); |
Chris Lattner | 08b22ec | 2005-05-13 07:09:09 +0000 | [diff] [blame] | 8741 | if (!OldCall->use_empty()) |
| 8742 | OldCall->replaceAllUsesWith(UndefValue::get(OldCall->getType())); |
| 8743 | if (isa<CallInst>(OldCall)) // Not worth removing an invoke here. |
| 8744 | return EraseInstFromFunction(*OldCall); |
| 8745 | return 0; |
| 8746 | } |
| 8747 | |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8748 | if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) { |
| 8749 | // This instruction is not reachable, just remove it. We insert a store to |
| 8750 | // undef so that we know that this code is not reachable, despite the fact |
| 8751 | // that we can't modify the CFG here. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 8752 | new StoreInst(ConstantInt::getTrue(), |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 8753 | UndefValue::get(PointerType::getUnqual(Type::Int1Ty)), |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8754 | CS.getInstruction()); |
| 8755 | |
| 8756 | if (!CS.getInstruction()->use_empty()) |
| 8757 | CS.getInstruction()-> |
| 8758 | replaceAllUsesWith(UndefValue::get(CS.getInstruction()->getType())); |
| 8759 | |
| 8760 | if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) { |
| 8761 | // Don't break the CFG, insert a dummy cond branch. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 8762 | BranchInst::Create(II->getNormalDest(), II->getUnwindDest(), |
| 8763 | ConstantInt::getTrue(), II); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8764 | } |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8765 | return EraseInstFromFunction(*CS.getInstruction()); |
| 8766 | } |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8767 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8768 | if (BitCastInst *BC = dyn_cast<BitCastInst>(Callee)) |
| 8769 | if (IntrinsicInst *In = dyn_cast<IntrinsicInst>(BC->getOperand(0))) |
| 8770 | if (In->getIntrinsicID() == Intrinsic::init_trampoline) |
| 8771 | return transformCallThroughTrampoline(CS); |
| 8772 | |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8773 | const PointerType *PTy = cast<PointerType>(Callee->getType()); |
| 8774 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
| 8775 | if (FTy->isVarArg()) { |
Dale Johannesen | 63e7eb4 | 2008-04-23 01:03:05 +0000 | [diff] [blame] | 8776 | int ix = FTy->getNumParams() + (isa<InvokeInst>(Callee) ? 3 : 1); |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8777 | // See if we can optimize any arguments passed through the varargs area of |
| 8778 | // the call. |
| 8779 | for (CallSite::arg_iterator I = CS.arg_begin()+FTy->getNumParams(), |
Dale Johannesen | 1f530a5 | 2008-04-23 18:34:37 +0000 | [diff] [blame] | 8780 | E = CS.arg_end(); I != E; ++I, ++ix) { |
| 8781 | CastInst *CI = dyn_cast<CastInst>(*I); |
| 8782 | if (CI && isSafeToEliminateVarargsCast(CS, CI, TD, ix)) { |
| 8783 | *I = CI->getOperand(0); |
| 8784 | Changed = true; |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8785 | } |
Dale Johannesen | 1f530a5 | 2008-04-23 18:34:37 +0000 | [diff] [blame] | 8786 | } |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8787 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8788 | |
Duncan Sands | f0c3354 | 2007-12-19 21:13:37 +0000 | [diff] [blame] | 8789 | if (isa<InlineAsm>(Callee) && !CS.doesNotThrow()) { |
Duncan Sands | ece2c04 | 2007-12-16 15:51:49 +0000 | [diff] [blame] | 8790 | // Inline asm calls cannot throw - mark them 'nounwind'. |
Duncan Sands | f0c3354 | 2007-12-19 21:13:37 +0000 | [diff] [blame] | 8791 | CS.setDoesNotThrow(); |
Duncan Sands | ece2c04 | 2007-12-16 15:51:49 +0000 | [diff] [blame] | 8792 | Changed = true; |
| 8793 | } |
| 8794 | |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8795 | return Changed ? CS.getInstruction() : 0; |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 8796 | } |
| 8797 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8798 | // transformConstExprCastCall - If the callee is a constexpr cast of a function, |
| 8799 | // attempt to move the cast to the arguments of the call/invoke. |
| 8800 | // |
| 8801 | bool InstCombiner::transformConstExprCastCall(CallSite CS) { |
| 8802 | if (!isa<ConstantExpr>(CS.getCalledValue())) return false; |
| 8803 | ConstantExpr *CE = cast<ConstantExpr>(CS.getCalledValue()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8804 | if (CE->getOpcode() != Instruction::BitCast || |
| 8805 | !isa<Function>(CE->getOperand(0))) |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8806 | return false; |
Reid Spencer | 8863f18 | 2004-07-18 00:38:32 +0000 | [diff] [blame] | 8807 | Function *Callee = cast<Function>(CE->getOperand(0)); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8808 | Instruction *Caller = CS.getInstruction(); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8809 | const PAListPtr &CallerPAL = CS.getParamAttrs(); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8810 | |
| 8811 | // Okay, this is a cast from a function to a different type. Unless doing so |
| 8812 | // would cause a type conversion of one of our arguments, change this call to |
| 8813 | // be a direct call with arguments casted to the appropriate types. |
| 8814 | // |
| 8815 | const FunctionType *FT = Callee->getFunctionType(); |
| 8816 | const Type *OldRetTy = Caller->getType(); |
| 8817 | |
Devang Patel | 75e6f02 | 2008-03-11 18:04:06 +0000 | [diff] [blame] | 8818 | if (isa<StructType>(FT->getReturnType())) |
| 8819 | return false; // TODO: Handle multiple return values. |
| 8820 | |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 8821 | // Check to see if we are changing the return type... |
| 8822 | if (OldRetTy != FT->getReturnType()) { |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 8823 | if (Callee->isDeclaration() && !Caller->use_empty() && |
Chris Lattner | 46013f4 | 2007-01-06 19:53:32 +0000 | [diff] [blame] | 8824 | // Conversion is ok if changing from pointer to int of same size. |
| 8825 | !(isa<PointerType>(FT->getReturnType()) && |
| 8826 | TD->getIntPtrType() == OldRetTy)) |
Chris Lattner | ec47992 | 2007-01-06 02:09:32 +0000 | [diff] [blame] | 8827 | return false; // Cannot transform this return value. |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 8828 | |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8829 | if (!Caller->use_empty() && |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8830 | // void -> non-void is handled specially |
Duncan Sands | e1e520f | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 8831 | FT->getReturnType() != Type::VoidTy && |
| 8832 | !CastInst::isCastable(FT->getReturnType(), OldRetTy)) |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8833 | return false; // Cannot transform this return value. |
| 8834 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8835 | if (!CallerPAL.isEmpty() && !Caller->use_empty()) { |
| 8836 | ParameterAttributes RAttrs = CallerPAL.getParamAttrs(0); |
Duncan Sands | 6c3470e | 2008-01-07 17:16:06 +0000 | [diff] [blame] | 8837 | if (RAttrs & ParamAttr::typeIncompatible(FT->getReturnType())) |
| 8838 | return false; // Attribute not compatible with transformed value. |
| 8839 | } |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8840 | |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 8841 | // If the callsite is an invoke instruction, and the return value is used by |
| 8842 | // a PHI node in a successor, we cannot change the return type of the call |
| 8843 | // because there is no place to put the cast instruction (without breaking |
| 8844 | // the critical edge). Bail out in this case. |
| 8845 | if (!Caller->use_empty()) |
| 8846 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) |
| 8847 | for (Value::use_iterator UI = II->use_begin(), E = II->use_end(); |
| 8848 | UI != E; ++UI) |
| 8849 | if (PHINode *PN = dyn_cast<PHINode>(*UI)) |
| 8850 | if (PN->getParent() == II->getNormalDest() || |
Chris Lattner | aeb2a1d | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 8851 | PN->getParent() == II->getUnwindDest()) |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 8852 | return false; |
| 8853 | } |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8854 | |
| 8855 | unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin()); |
| 8856 | unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8857 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8858 | CallSite::arg_iterator AI = CS.arg_begin(); |
| 8859 | for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) { |
| 8860 | const Type *ParamTy = FT->getParamType(i); |
Andrew Lenharth | b8e604c | 2006-06-28 01:01:52 +0000 | [diff] [blame] | 8861 | const Type *ActTy = (*AI)->getType(); |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8862 | |
| 8863 | if (!CastInst::isCastable(ActTy, ParamTy)) |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8864 | return false; // Cannot transform this parameter value. |
| 8865 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8866 | if (CallerPAL.getParamAttrs(i + 1) & ParamAttr::typeIncompatible(ParamTy)) |
| 8867 | return false; // Attribute not compatible with transformed value. |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8868 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8869 | ConstantInt *c = dyn_cast<ConstantInt>(*AI); |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8870 | // Some conversions are safe even if we do not have a body. |
| 8871 | // Either we can cast directly, or we can upconvert the argument |
Chris Lattner | ec47992 | 2007-01-06 02:09:32 +0000 | [diff] [blame] | 8872 | bool isConvertible = ActTy == ParamTy || |
Chris Lattner | 46013f4 | 2007-01-06 19:53:32 +0000 | [diff] [blame] | 8873 | (isa<PointerType>(ParamTy) && isa<PointerType>(ActTy)) || |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 8874 | (ParamTy->isInteger() && ActTy->isInteger() && |
Reid Spencer | abaa8ca | 2007-01-08 16:32:00 +0000 | [diff] [blame] | 8875 | ParamTy->getPrimitiveSizeInBits() >= ActTy->getPrimitiveSizeInBits()) || |
| 8876 | (c && ParamTy->getPrimitiveSizeInBits() >= ActTy->getPrimitiveSizeInBits() |
Zhou Sheng | 0fc5095 | 2007-03-25 05:01:29 +0000 | [diff] [blame] | 8877 | && c->getValue().isStrictlyPositive()); |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 8878 | if (Callee->isDeclaration() && !isConvertible) return false; |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8879 | } |
| 8880 | |
| 8881 | if (FT->getNumParams() < NumActualArgs && !FT->isVarArg() && |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 8882 | Callee->isDeclaration()) |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8883 | return false; // Do not delete arguments unless we have a function body. |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8884 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8885 | if (FT->getNumParams() < NumActualArgs && FT->isVarArg() && |
| 8886 | !CallerPAL.isEmpty()) |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8887 | // 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] | 8888 | // won't be dropping them. Check that these extra arguments have attributes |
| 8889 | // that are compatible with being a vararg call argument. |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8890 | for (unsigned i = CallerPAL.getNumSlots(); i; --i) { |
| 8891 | if (CallerPAL.getSlot(i - 1).Index <= FT->getNumParams()) |
Duncan Sands | e1e520f | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 8892 | break; |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8893 | ParameterAttributes PAttrs = CallerPAL.getSlot(i - 1).Attrs; |
Duncan Sands | e1e520f | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 8894 | if (PAttrs & ParamAttr::VarArgsIncompatible) |
| 8895 | return false; |
| 8896 | } |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8897 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8898 | // Okay, we decided that this is a safe thing to do: go ahead and start |
| 8899 | // inserting cast instructions as necessary... |
| 8900 | std::vector<Value*> Args; |
| 8901 | Args.reserve(NumActualArgs); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8902 | SmallVector<ParamAttrsWithIndex, 8> attrVec; |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8903 | attrVec.reserve(NumCommonArgs); |
| 8904 | |
| 8905 | // Get any return attributes. |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8906 | ParameterAttributes RAttrs = CallerPAL.getParamAttrs(0); |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8907 | |
| 8908 | // If the return value is not being used, the type may not be compatible |
| 8909 | // with the existing attributes. Wipe out any problematic attributes. |
Duncan Sands | 6c3470e | 2008-01-07 17:16:06 +0000 | [diff] [blame] | 8910 | RAttrs &= ~ParamAttr::typeIncompatible(FT->getReturnType()); |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8911 | |
| 8912 | // Add the new return attributes. |
| 8913 | if (RAttrs) |
| 8914 | attrVec.push_back(ParamAttrsWithIndex::get(0, RAttrs)); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8915 | |
| 8916 | AI = CS.arg_begin(); |
| 8917 | for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) { |
| 8918 | const Type *ParamTy = FT->getParamType(i); |
| 8919 | if ((*AI)->getType() == ParamTy) { |
| 8920 | Args.push_back(*AI); |
| 8921 | } else { |
Reid Spencer | 8a903db | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 8922 | Instruction::CastOps opcode = CastInst::getCastOpcode(*AI, |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8923 | false, ParamTy, false); |
Reid Spencer | 8a903db | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 8924 | CastInst *NewCast = CastInst::create(opcode, *AI, ParamTy, "tmp"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8925 | Args.push_back(InsertNewInstBefore(NewCast, *Caller)); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8926 | } |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8927 | |
| 8928 | // Add any parameter attributes. |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8929 | if (ParameterAttributes PAttrs = CallerPAL.getParamAttrs(i + 1)) |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8930 | attrVec.push_back(ParamAttrsWithIndex::get(i + 1, PAttrs)); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8931 | } |
| 8932 | |
| 8933 | // If the function takes more arguments than the call was taking, add them |
| 8934 | // now... |
| 8935 | for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i) |
| 8936 | Args.push_back(Constant::getNullValue(FT->getParamType(i))); |
| 8937 | |
| 8938 | // If we are removing arguments to the function, emit an obnoxious warning... |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 8939 | if (FT->getNumParams() < NumActualArgs) { |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8940 | if (!FT->isVarArg()) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 8941 | cerr << "WARNING: While resolving call to function '" |
| 8942 | << Callee->getName() << "' arguments were dropped!\n"; |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8943 | } else { |
| 8944 | // Add all of the arguments in their promoted form to the arg list... |
| 8945 | for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) { |
| 8946 | const Type *PTy = getPromotedType((*AI)->getType()); |
| 8947 | if (PTy != (*AI)->getType()) { |
| 8948 | // Must promote to pass through va_arg area! |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8949 | Instruction::CastOps opcode = CastInst::getCastOpcode(*AI, false, |
| 8950 | PTy, false); |
Reid Spencer | 8a903db | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 8951 | Instruction *Cast = CastInst::create(opcode, *AI, PTy, "tmp"); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8952 | InsertNewInstBefore(Cast, *Caller); |
| 8953 | Args.push_back(Cast); |
| 8954 | } else { |
| 8955 | Args.push_back(*AI); |
| 8956 | } |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8957 | |
Duncan Sands | e1e520f | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 8958 | // Add any parameter attributes. |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8959 | if (ParameterAttributes PAttrs = CallerPAL.getParamAttrs(i + 1)) |
Duncan Sands | e1e520f | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 8960 | attrVec.push_back(ParamAttrsWithIndex::get(i + 1, PAttrs)); |
| 8961 | } |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8962 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 8963 | } |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8964 | |
| 8965 | if (FT->getReturnType() == Type::VoidTy) |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 8966 | Caller->setName(""); // Void type should not have a name. |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8967 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8968 | const PAListPtr &NewCallerPAL = PAListPtr::get(attrVec.begin(),attrVec.end()); |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8969 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8970 | Instruction *NC; |
| 8971 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 8972 | NC = InvokeInst::Create(Callee, II->getNormalDest(), II->getUnwindDest(), |
| 8973 | Args.begin(), Args.end(), Caller->getName(), Caller); |
Reid Spencer | ed3fa85 | 2007-07-30 19:53:57 +0000 | [diff] [blame] | 8974 | cast<InvokeInst>(NC)->setCallingConv(II->getCallingConv()); |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8975 | cast<InvokeInst>(NC)->setParamAttrs(NewCallerPAL); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8976 | } else { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 8977 | NC = CallInst::Create(Callee, Args.begin(), Args.end(), |
| 8978 | Caller->getName(), Caller); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 8979 | CallInst *CI = cast<CallInst>(Caller); |
| 8980 | if (CI->isTailCall()) |
Chris Lattner | a9e9211 | 2005-05-06 06:48:21 +0000 | [diff] [blame] | 8981 | cast<CallInst>(NC)->setTailCall(); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 8982 | cast<CallInst>(NC)->setCallingConv(CI->getCallingConv()); |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8983 | cast<CallInst>(NC)->setParamAttrs(NewCallerPAL); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8984 | } |
| 8985 | |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 8986 | // Insert a cast of the return type as necessary. |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8987 | Value *NV = NC; |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8988 | if (OldRetTy != NV->getType() && !Caller->use_empty()) { |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8989 | if (NV->getType() != Type::VoidTy) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8990 | Instruction::CastOps opcode = CastInst::getCastOpcode(NC, false, |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8991 | OldRetTy, false); |
| 8992 | NV = NC = CastInst::create(opcode, NC, OldRetTy, "tmp"); |
Chris Lattner | bb60904 | 2003-10-30 00:46:41 +0000 | [diff] [blame] | 8993 | |
| 8994 | // If this is an invoke instruction, we should insert it after the first |
| 8995 | // non-phi, instruction in the normal successor block. |
| 8996 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
| 8997 | BasicBlock::iterator I = II->getNormalDest()->begin(); |
| 8998 | while (isa<PHINode>(I)) ++I; |
| 8999 | InsertNewInstBefore(NC, *I); |
| 9000 | } else { |
| 9001 | // Otherwise, it's a call, just insert cast right after the call instr |
| 9002 | InsertNewInstBefore(NC, *Caller); |
| 9003 | } |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 9004 | AddUsersToWorkList(*Caller); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9005 | } else { |
Chris Lattner | c30bda7 | 2004-10-17 21:22:38 +0000 | [diff] [blame] | 9006 | NV = UndefValue::get(Caller->getType()); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9007 | } |
| 9008 | } |
| 9009 | |
| 9010 | if (Caller->getType() != Type::VoidTy && !Caller->use_empty()) |
| 9011 | Caller->replaceAllUsesWith(NV); |
Chris Lattner | f22a5c6 | 2007-03-02 19:59:19 +0000 | [diff] [blame] | 9012 | Caller->eraseFromParent(); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9013 | RemoveFromWorkList(Caller); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9014 | return true; |
| 9015 | } |
| 9016 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9017 | // transformCallThroughTrampoline - Turn a call to a function created by the |
| 9018 | // init_trampoline intrinsic into a direct call to the underlying function. |
| 9019 | // |
| 9020 | Instruction *InstCombiner::transformCallThroughTrampoline(CallSite CS) { |
| 9021 | Value *Callee = CS.getCalledValue(); |
| 9022 | const PointerType *PTy = cast<PointerType>(Callee->getType()); |
| 9023 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9024 | const PAListPtr &Attrs = CS.getParamAttrs(); |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9025 | |
| 9026 | // If the call already has the 'nest' attribute somewhere then give up - |
| 9027 | // otherwise 'nest' would occur twice after splicing in the chain. |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9028 | if (Attrs.hasAttrSomewhere(ParamAttr::Nest)) |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9029 | return 0; |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9030 | |
| 9031 | IntrinsicInst *Tramp = |
| 9032 | cast<IntrinsicInst>(cast<BitCastInst>(Callee)->getOperand(0)); |
| 9033 | |
| 9034 | Function *NestF = |
| 9035 | cast<Function>(IntrinsicInst::StripPointerCasts(Tramp->getOperand(2))); |
| 9036 | const PointerType *NestFPTy = cast<PointerType>(NestF->getType()); |
| 9037 | const FunctionType *NestFTy = cast<FunctionType>(NestFPTy->getElementType()); |
| 9038 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9039 | const PAListPtr &NestAttrs = NestF->getParamAttrs(); |
| 9040 | if (!NestAttrs.isEmpty()) { |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9041 | unsigned NestIdx = 1; |
| 9042 | const Type *NestTy = 0; |
Dale Johannesen | 0d51e7e | 2008-02-19 21:38:47 +0000 | [diff] [blame] | 9043 | ParameterAttributes NestAttr = ParamAttr::None; |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9044 | |
| 9045 | // Look for a parameter marked with the 'nest' attribute. |
| 9046 | for (FunctionType::param_iterator I = NestFTy->param_begin(), |
| 9047 | E = NestFTy->param_end(); I != E; ++NestIdx, ++I) |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9048 | if (NestAttrs.paramHasAttr(NestIdx, ParamAttr::Nest)) { |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9049 | // Record the parameter type and any other attributes. |
| 9050 | NestTy = *I; |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9051 | NestAttr = NestAttrs.getParamAttrs(NestIdx); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9052 | break; |
| 9053 | } |
| 9054 | |
| 9055 | if (NestTy) { |
| 9056 | Instruction *Caller = CS.getInstruction(); |
| 9057 | std::vector<Value*> NewArgs; |
| 9058 | NewArgs.reserve(unsigned(CS.arg_end()-CS.arg_begin())+1); |
| 9059 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9060 | SmallVector<ParamAttrsWithIndex, 8> NewAttrs; |
| 9061 | NewAttrs.reserve(Attrs.getNumSlots() + 1); |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9062 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9063 | // Insert the nest argument into the call argument list, which may |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9064 | // mean appending it. Likewise for attributes. |
| 9065 | |
| 9066 | // Add any function result attributes. |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9067 | if (ParameterAttributes Attr = Attrs.getParamAttrs(0)) |
| 9068 | NewAttrs.push_back(ParamAttrsWithIndex::get(0, Attr)); |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9069 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9070 | { |
| 9071 | unsigned Idx = 1; |
| 9072 | CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); |
| 9073 | do { |
| 9074 | if (Idx == NestIdx) { |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9075 | // Add the chain argument and attributes. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9076 | Value *NestVal = Tramp->getOperand(3); |
| 9077 | if (NestVal->getType() != NestTy) |
| 9078 | NestVal = new BitCastInst(NestVal, NestTy, "nest", Caller); |
| 9079 | NewArgs.push_back(NestVal); |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9080 | NewAttrs.push_back(ParamAttrsWithIndex::get(NestIdx, NestAttr)); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9081 | } |
| 9082 | |
| 9083 | if (I == E) |
| 9084 | break; |
| 9085 | |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9086 | // Add the original argument and attributes. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9087 | NewArgs.push_back(*I); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9088 | if (ParameterAttributes Attr = Attrs.getParamAttrs(Idx)) |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9089 | NewAttrs.push_back |
| 9090 | (ParamAttrsWithIndex::get(Idx + (Idx >= NestIdx), Attr)); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9091 | |
| 9092 | ++Idx, ++I; |
| 9093 | } while (1); |
| 9094 | } |
| 9095 | |
| 9096 | // The trampoline may have been bitcast to a bogus type (FTy). |
| 9097 | // Handle this by synthesizing a new function type, equal to FTy |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9098 | // with the chain parameter inserted. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9099 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9100 | std::vector<const Type*> NewTypes; |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9101 | NewTypes.reserve(FTy->getNumParams()+1); |
| 9102 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9103 | // 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] | 9104 | // mean appending it. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9105 | { |
| 9106 | unsigned Idx = 1; |
| 9107 | FunctionType::param_iterator I = FTy->param_begin(), |
| 9108 | E = FTy->param_end(); |
| 9109 | |
| 9110 | do { |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9111 | if (Idx == NestIdx) |
| 9112 | // Add the chain's type. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9113 | NewTypes.push_back(NestTy); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9114 | |
| 9115 | if (I == E) |
| 9116 | break; |
| 9117 | |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9118 | // Add the original type. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9119 | NewTypes.push_back(*I); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9120 | |
| 9121 | ++Idx, ++I; |
| 9122 | } while (1); |
| 9123 | } |
| 9124 | |
| 9125 | // Replace the trampoline call with a direct call. Let the generic |
| 9126 | // code sort out any function type mismatches. |
| 9127 | FunctionType *NewFTy = |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 9128 | FunctionType::get(FTy->getReturnType(), NewTypes, FTy->isVarArg()); |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 9129 | Constant *NewCallee = NestF->getType() == PointerType::getUnqual(NewFTy) ? |
| 9130 | NestF : ConstantExpr::getBitCast(NestF, PointerType::getUnqual(NewFTy)); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9131 | const PAListPtr &NewPAL = PAListPtr::get(NewAttrs.begin(),NewAttrs.end()); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9132 | |
| 9133 | Instruction *NewCaller; |
| 9134 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9135 | NewCaller = InvokeInst::Create(NewCallee, |
| 9136 | II->getNormalDest(), II->getUnwindDest(), |
| 9137 | NewArgs.begin(), NewArgs.end(), |
| 9138 | Caller->getName(), Caller); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9139 | cast<InvokeInst>(NewCaller)->setCallingConv(II->getCallingConv()); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 9140 | cast<InvokeInst>(NewCaller)->setParamAttrs(NewPAL); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9141 | } else { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9142 | NewCaller = CallInst::Create(NewCallee, NewArgs.begin(), NewArgs.end(), |
| 9143 | Caller->getName(), Caller); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9144 | if (cast<CallInst>(Caller)->isTailCall()) |
| 9145 | cast<CallInst>(NewCaller)->setTailCall(); |
| 9146 | cast<CallInst>(NewCaller)-> |
| 9147 | setCallingConv(cast<CallInst>(Caller)->getCallingConv()); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 9148 | cast<CallInst>(NewCaller)->setParamAttrs(NewPAL); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9149 | } |
| 9150 | if (Caller->getType() != Type::VoidTy && !Caller->use_empty()) |
| 9151 | Caller->replaceAllUsesWith(NewCaller); |
| 9152 | Caller->eraseFromParent(); |
| 9153 | RemoveFromWorkList(Caller); |
| 9154 | return 0; |
| 9155 | } |
| 9156 | } |
| 9157 | |
| 9158 | // Replace the trampoline call with a direct call. Since there is no 'nest' |
| 9159 | // parameter, there is no need to adjust the argument list. Let the generic |
| 9160 | // code sort out any function type mismatches. |
| 9161 | Constant *NewCallee = |
| 9162 | NestF->getType() == PTy ? NestF : ConstantExpr::getBitCast(NestF, PTy); |
| 9163 | CS.setCalledFunction(NewCallee); |
| 9164 | return CS.getInstruction(); |
| 9165 | } |
| 9166 | |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9167 | /// FoldPHIArgBinOpIntoPHI - If we have something like phi [add (a,b), add(c,d)] |
| 9168 | /// and if a/b/c/d and the add's all have a single use, turn this into two phi's |
| 9169 | /// and a single binop. |
| 9170 | Instruction *InstCombiner::FoldPHIArgBinOpIntoPHI(PHINode &PN) { |
| 9171 | Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0)); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 9172 | assert(isa<BinaryOperator>(FirstInst) || isa<GetElementPtrInst>(FirstInst) || |
| 9173 | isa<CmpInst>(FirstInst)); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9174 | unsigned Opc = FirstInst->getOpcode(); |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 9175 | Value *LHSVal = FirstInst->getOperand(0); |
| 9176 | Value *RHSVal = FirstInst->getOperand(1); |
| 9177 | |
| 9178 | const Type *LHSType = LHSVal->getType(); |
| 9179 | const Type *RHSType = RHSVal->getType(); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9180 | |
| 9181 | // Scan to see if all operands are the same opcode, all have one use, and all |
| 9182 | // kill their operands (i.e. the operands have one use). |
Chris Lattner | a90a24c | 2006-11-01 04:55:47 +0000 | [diff] [blame] | 9183 | for (unsigned i = 0; i != PN.getNumIncomingValues(); ++i) { |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9184 | Instruction *I = dyn_cast<Instruction>(PN.getIncomingValue(i)); |
Chris Lattner | a90a24c | 2006-11-01 04:55:47 +0000 | [diff] [blame] | 9185 | if (!I || I->getOpcode() != Opc || !I->hasOneUse() || |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9186 | // 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] | 9187 | // types or GEP's with different index types. |
| 9188 | I->getOperand(0)->getType() != LHSType || |
| 9189 | I->getOperand(1)->getType() != RHSType) |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9190 | return 0; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9191 | |
| 9192 | // If they are CmpInst instructions, check their predicates |
| 9193 | if (Opc == Instruction::ICmp || Opc == Instruction::FCmp) |
| 9194 | if (cast<CmpInst>(I)->getPredicate() != |
| 9195 | cast<CmpInst>(FirstInst)->getPredicate()) |
| 9196 | return 0; |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 9197 | |
| 9198 | // Keep track of which operand needs a phi node. |
| 9199 | if (I->getOperand(0) != LHSVal) LHSVal = 0; |
| 9200 | if (I->getOperand(1) != RHSVal) RHSVal = 0; |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9201 | } |
| 9202 | |
Chris Lattner | 53738a4 | 2006-11-08 19:42:28 +0000 | [diff] [blame] | 9203 | // Otherwise, this is safe to transform, determine if it is profitable. |
| 9204 | |
| 9205 | // If this is a GEP, and if the index (not the pointer) needs a PHI, bail out. |
| 9206 | // Indexes are often folded into load/store instructions, so we don't want to |
| 9207 | // hide them behind a phi. |
| 9208 | if (isa<GetElementPtrInst>(FirstInst) && RHSVal == 0) |
| 9209 | return 0; |
| 9210 | |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9211 | Value *InLHS = FirstInst->getOperand(0); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9212 | Value *InRHS = FirstInst->getOperand(1); |
Chris Lattner | 53738a4 | 2006-11-08 19:42:28 +0000 | [diff] [blame] | 9213 | PHINode *NewLHS = 0, *NewRHS = 0; |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 9214 | if (LHSVal == 0) { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9215 | NewLHS = PHINode::Create(LHSType, FirstInst->getOperand(0)->getName()+".pn"); |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 9216 | NewLHS->reserveOperandSpace(PN.getNumOperands()/2); |
| 9217 | NewLHS->addIncoming(InLHS, PN.getIncomingBlock(0)); |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 9218 | InsertNewInstBefore(NewLHS, PN); |
| 9219 | LHSVal = NewLHS; |
| 9220 | } |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 9221 | |
| 9222 | if (RHSVal == 0) { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9223 | NewRHS = PHINode::Create(RHSType, FirstInst->getOperand(1)->getName()+".pn"); |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 9224 | NewRHS->reserveOperandSpace(PN.getNumOperands()/2); |
| 9225 | NewRHS->addIncoming(InRHS, PN.getIncomingBlock(0)); |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 9226 | InsertNewInstBefore(NewRHS, PN); |
| 9227 | RHSVal = NewRHS; |
| 9228 | } |
| 9229 | |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 9230 | // Add all operands to the new PHIs. |
| 9231 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 9232 | if (NewLHS) { |
| 9233 | Value *NewInLHS =cast<Instruction>(PN.getIncomingValue(i))->getOperand(0); |
| 9234 | NewLHS->addIncoming(NewInLHS, PN.getIncomingBlock(i)); |
| 9235 | } |
| 9236 | if (NewRHS) { |
| 9237 | Value *NewInRHS =cast<Instruction>(PN.getIncomingValue(i))->getOperand(1); |
| 9238 | NewRHS->addIncoming(NewInRHS, PN.getIncomingBlock(i)); |
| 9239 | } |
| 9240 | } |
| 9241 | |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9242 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 9243 | return BinaryOperator::create(BinOp->getOpcode(), LHSVal, RHSVal); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9244 | else if (CmpInst *CIOp = dyn_cast<CmpInst>(FirstInst)) |
| 9245 | return CmpInst::create(CIOp->getOpcode(), CIOp->getPredicate(), LHSVal, |
| 9246 | RHSVal); |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 9247 | else { |
| 9248 | assert(isa<GetElementPtrInst>(FirstInst)); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9249 | return GetElementPtrInst::Create(LHSVal, RHSVal); |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 9250 | } |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9251 | } |
| 9252 | |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 9253 | /// isSafeToSinkLoad - Return true if we know that it is safe sink the load out |
| 9254 | /// of the block that defines it. This means that it must be obvious the value |
| 9255 | /// of the load is not changed from the point of the load to the end of the |
| 9256 | /// block it is in. |
Chris Lattner | fd905ca | 2007-02-01 22:30:07 +0000 | [diff] [blame] | 9257 | /// |
| 9258 | /// Finally, it is safe, but not profitable, to sink a load targetting a |
| 9259 | /// non-address-taken alloca. Doing so will cause us to not promote the alloca |
| 9260 | /// to a register. |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 9261 | static bool isSafeToSinkLoad(LoadInst *L) { |
| 9262 | BasicBlock::iterator BBI = L, E = L->getParent()->end(); |
| 9263 | |
| 9264 | for (++BBI; BBI != E; ++BBI) |
| 9265 | if (BBI->mayWriteToMemory()) |
| 9266 | return false; |
Chris Lattner | fd905ca | 2007-02-01 22:30:07 +0000 | [diff] [blame] | 9267 | |
| 9268 | // Check for non-address taken alloca. If not address-taken already, it isn't |
| 9269 | // profitable to do this xform. |
| 9270 | if (AllocaInst *AI = dyn_cast<AllocaInst>(L->getOperand(0))) { |
| 9271 | bool isAddressTaken = false; |
| 9272 | for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); |
| 9273 | UI != E; ++UI) { |
| 9274 | if (isa<LoadInst>(UI)) continue; |
| 9275 | if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) { |
| 9276 | // If storing TO the alloca, then the address isn't taken. |
| 9277 | if (SI->getOperand(1) == AI) continue; |
| 9278 | } |
| 9279 | isAddressTaken = true; |
| 9280 | break; |
| 9281 | } |
| 9282 | |
| 9283 | if (!isAddressTaken) |
| 9284 | return false; |
| 9285 | } |
| 9286 | |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 9287 | return true; |
| 9288 | } |
| 9289 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9290 | |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9291 | // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary" |
| 9292 | // operator and they all are only used by the PHI, PHI together their |
| 9293 | // inputs, and do the operation once, to the result of the PHI. |
| 9294 | Instruction *InstCombiner::FoldPHIArgOpIntoPHI(PHINode &PN) { |
| 9295 | Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0)); |
| 9296 | |
| 9297 | // Scan the instruction, looking for input operations that can be folded away. |
| 9298 | // If all input operands to the phi are the same instruction (e.g. a cast from |
| 9299 | // the same type or "+42") we can pull the operation through the PHI, reducing |
| 9300 | // code size and simplifying code. |
| 9301 | Constant *ConstantOp = 0; |
| 9302 | const Type *CastSrcTy = 0; |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 9303 | bool isVolatile = false; |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9304 | if (isa<CastInst>(FirstInst)) { |
| 9305 | CastSrcTy = FirstInst->getOperand(0)->getType(); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 9306 | } else if (isa<BinaryOperator>(FirstInst) || isa<CmpInst>(FirstInst)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9307 | // Can fold binop, compare or shift here if the RHS is a constant, |
| 9308 | // otherwise call FoldPHIArgBinOpIntoPHI. |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9309 | ConstantOp = dyn_cast<Constant>(FirstInst->getOperand(1)); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9310 | if (ConstantOp == 0) |
| 9311 | return FoldPHIArgBinOpIntoPHI(PN); |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 9312 | } else if (LoadInst *LI = dyn_cast<LoadInst>(FirstInst)) { |
| 9313 | isVolatile = LI->isVolatile(); |
| 9314 | // We can't sink the load if the loaded value could be modified between the |
| 9315 | // load and the PHI. |
| 9316 | if (LI->getParent() != PN.getIncomingBlock(0) || |
| 9317 | !isSafeToSinkLoad(LI)) |
| 9318 | return 0; |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 9319 | } else if (isa<GetElementPtrInst>(FirstInst)) { |
Chris Lattner | 53738a4 | 2006-11-08 19:42:28 +0000 | [diff] [blame] | 9320 | if (FirstInst->getNumOperands() == 2) |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 9321 | return FoldPHIArgBinOpIntoPHI(PN); |
| 9322 | // Can't handle general GEPs yet. |
| 9323 | return 0; |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9324 | } else { |
| 9325 | return 0; // Cannot fold this operation. |
| 9326 | } |
| 9327 | |
| 9328 | // Check to see if all arguments are the same operation. |
| 9329 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 9330 | if (!isa<Instruction>(PN.getIncomingValue(i))) return 0; |
| 9331 | Instruction *I = cast<Instruction>(PN.getIncomingValue(i)); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9332 | if (!I->hasOneUse() || !I->isSameOperationAs(FirstInst)) |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9333 | return 0; |
| 9334 | if (CastSrcTy) { |
| 9335 | if (I->getOperand(0)->getType() != CastSrcTy) |
| 9336 | return 0; // Cast operation must match. |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 9337 | } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9338 | // We can't sink the load if the loaded value could be modified between |
| 9339 | // the load and the PHI. |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 9340 | if (LI->isVolatile() != isVolatile || |
| 9341 | LI->getParent() != PN.getIncomingBlock(i) || |
| 9342 | !isSafeToSinkLoad(LI)) |
| 9343 | return 0; |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9344 | } else if (I->getOperand(1) != ConstantOp) { |
| 9345 | return 0; |
| 9346 | } |
| 9347 | } |
| 9348 | |
| 9349 | // Okay, they are all the same operation. Create a new PHI node of the |
| 9350 | // 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] | 9351 | PHINode *NewPN = PHINode::Create(FirstInst->getOperand(0)->getType(), |
| 9352 | PN.getName()+".in"); |
Chris Lattner | 5551706 | 2005-01-29 00:39:08 +0000 | [diff] [blame] | 9353 | NewPN->reserveOperandSpace(PN.getNumOperands()/2); |
Chris Lattner | b589344 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 9354 | |
| 9355 | Value *InVal = FirstInst->getOperand(0); |
| 9356 | NewPN->addIncoming(InVal, PN.getIncomingBlock(0)); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9357 | |
| 9358 | // Add all operands to the new PHI. |
Chris Lattner | b589344 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 9359 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 9360 | Value *NewInVal = cast<Instruction>(PN.getIncomingValue(i))->getOperand(0); |
| 9361 | if (NewInVal != InVal) |
| 9362 | InVal = 0; |
| 9363 | NewPN->addIncoming(NewInVal, PN.getIncomingBlock(i)); |
| 9364 | } |
| 9365 | |
| 9366 | Value *PhiVal; |
| 9367 | if (InVal) { |
| 9368 | // The new PHI unions all of the same values together. This is really |
| 9369 | // common, so we handle it intelligently here for compile-time speed. |
| 9370 | PhiVal = InVal; |
| 9371 | delete NewPN; |
| 9372 | } else { |
| 9373 | InsertNewInstBefore(NewPN, PN); |
| 9374 | PhiVal = NewPN; |
| 9375 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9376 | |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9377 | // Insert and return the new operation. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9378 | if (CastInst* FirstCI = dyn_cast<CastInst>(FirstInst)) |
| 9379 | return CastInst::create(FirstCI->getOpcode(), PhiVal, PN.getType()); |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 9380 | else if (isa<LoadInst>(FirstInst)) |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 9381 | return new LoadInst(PhiVal, "", isVolatile); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9382 | else if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) |
Chris Lattner | b589344 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 9383 | return BinaryOperator::create(BinOp->getOpcode(), PhiVal, ConstantOp); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9384 | else if (CmpInst *CIOp = dyn_cast<CmpInst>(FirstInst)) |
| 9385 | return CmpInst::create(CIOp->getOpcode(), CIOp->getPredicate(), |
| 9386 | PhiVal, ConstantOp); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9387 | else |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 9388 | assert(0 && "Unknown operation"); |
Jeff Cohen | ca5183d | 2007-03-05 00:00:42 +0000 | [diff] [blame] | 9389 | return 0; |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9390 | } |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 9391 | |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 9392 | /// DeadPHICycle - Return true if this PHI node is only used by a PHI node cycle |
| 9393 | /// that is dead. |
Chris Lattner | 0e5444b | 2007-03-26 20:40:50 +0000 | [diff] [blame] | 9394 | static bool DeadPHICycle(PHINode *PN, |
| 9395 | SmallPtrSet<PHINode*, 16> &PotentiallyDeadPHIs) { |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 9396 | if (PN->use_empty()) return true; |
| 9397 | if (!PN->hasOneUse()) return false; |
| 9398 | |
| 9399 | // Remember this node, and if we find the cycle, return. |
Chris Lattner | 0e5444b | 2007-03-26 20:40:50 +0000 | [diff] [blame] | 9400 | if (!PotentiallyDeadPHIs.insert(PN)) |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 9401 | return true; |
Chris Lattner | 92103de | 2007-08-28 04:23:55 +0000 | [diff] [blame] | 9402 | |
| 9403 | // Don't scan crazily complex things. |
| 9404 | if (PotentiallyDeadPHIs.size() == 16) |
| 9405 | return false; |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 9406 | |
| 9407 | if (PHINode *PU = dyn_cast<PHINode>(PN->use_back())) |
| 9408 | return DeadPHICycle(PU, PotentiallyDeadPHIs); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9409 | |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 9410 | return false; |
| 9411 | } |
| 9412 | |
Chris Lattner | cf5008a | 2007-11-06 21:52:06 +0000 | [diff] [blame] | 9413 | /// PHIsEqualValue - Return true if this phi node is always equal to |
| 9414 | /// NonPhiInVal. This happens with mutually cyclic phi nodes like: |
| 9415 | /// z = some value; x = phi (y, z); y = phi (x, z) |
| 9416 | static bool PHIsEqualValue(PHINode *PN, Value *NonPhiInVal, |
| 9417 | SmallPtrSet<PHINode*, 16> &ValueEqualPHIs) { |
| 9418 | // See if we already saw this PHI node. |
| 9419 | if (!ValueEqualPHIs.insert(PN)) |
| 9420 | return true; |
| 9421 | |
| 9422 | // Don't scan crazily complex things. |
| 9423 | if (ValueEqualPHIs.size() == 16) |
| 9424 | return false; |
| 9425 | |
| 9426 | // Scan the operands to see if they are either phi nodes or are equal to |
| 9427 | // the value. |
| 9428 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 9429 | Value *Op = PN->getIncomingValue(i); |
| 9430 | if (PHINode *OpPN = dyn_cast<PHINode>(Op)) { |
| 9431 | if (!PHIsEqualValue(OpPN, NonPhiInVal, ValueEqualPHIs)) |
| 9432 | return false; |
| 9433 | } else if (Op != NonPhiInVal) |
| 9434 | return false; |
| 9435 | } |
| 9436 | |
| 9437 | return true; |
| 9438 | } |
| 9439 | |
| 9440 | |
Chris Lattner | 473945d | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 9441 | // PHINode simplification |
| 9442 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 9443 | Instruction *InstCombiner::visitPHINode(PHINode &PN) { |
Owen Anderson | b64ab87 | 2006-07-10 22:15:25 +0000 | [diff] [blame] | 9444 | // If LCSSA is around, don't mess with Phi nodes |
Chris Lattner | f964f32 | 2007-03-04 04:27:24 +0000 | [diff] [blame] | 9445 | if (MustPreserveLCSSA) return 0; |
Owen Anderson | d1b78a1 | 2006-07-10 19:03:49 +0000 | [diff] [blame] | 9446 | |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 9447 | if (Value *V = PN.hasConstantValue()) |
| 9448 | return ReplaceInstUsesWith(PN, V); |
| 9449 | |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 9450 | // If all PHI operands are the same operation, pull them through the PHI, |
| 9451 | // reducing code size. |
| 9452 | if (isa<Instruction>(PN.getIncomingValue(0)) && |
| 9453 | PN.getIncomingValue(0)->hasOneUse()) |
| 9454 | if (Instruction *Result = FoldPHIArgOpIntoPHI(PN)) |
| 9455 | return Result; |
| 9456 | |
| 9457 | // If this is a trivial cycle in the PHI node graph, remove it. Basically, if |
| 9458 | // this PHI only has a single use (a PHI), and if that PHI only has one use (a |
| 9459 | // PHI)... break the cycle. |
Chris Lattner | ff9f13a | 2007-01-15 07:30:06 +0000 | [diff] [blame] | 9460 | if (PN.hasOneUse()) { |
| 9461 | Instruction *PHIUser = cast<Instruction>(PN.use_back()); |
| 9462 | if (PHINode *PU = dyn_cast<PHINode>(PHIUser)) { |
Chris Lattner | 0e5444b | 2007-03-26 20:40:50 +0000 | [diff] [blame] | 9463 | SmallPtrSet<PHINode*, 16> PotentiallyDeadPHIs; |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 9464 | PotentiallyDeadPHIs.insert(&PN); |
| 9465 | if (DeadPHICycle(PU, PotentiallyDeadPHIs)) |
| 9466 | return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType())); |
| 9467 | } |
Chris Lattner | ff9f13a | 2007-01-15 07:30:06 +0000 | [diff] [blame] | 9468 | |
| 9469 | // If this phi has a single use, and if that use just computes a value for |
| 9470 | // the next iteration of a loop, delete the phi. This occurs with unused |
| 9471 | // induction variables, e.g. "for (int j = 0; ; ++j);". Detecting this |
| 9472 | // common case here is good because the only other things that catch this |
| 9473 | // are induction variable analysis (sometimes) and ADCE, which is only run |
| 9474 | // late. |
| 9475 | if (PHIUser->hasOneUse() && |
| 9476 | (isa<BinaryOperator>(PHIUser) || isa<GetElementPtrInst>(PHIUser)) && |
| 9477 | PHIUser->use_back() == &PN) { |
| 9478 | return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType())); |
| 9479 | } |
| 9480 | } |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 9481 | |
Chris Lattner | cf5008a | 2007-11-06 21:52:06 +0000 | [diff] [blame] | 9482 | // We sometimes end up with phi cycles that non-obviously end up being the |
| 9483 | // same value, for example: |
| 9484 | // z = some value; x = phi (y, z); y = phi (x, z) |
| 9485 | // where the phi nodes don't necessarily need to be in the same block. Do a |
| 9486 | // quick check to see if the PHI node only contains a single non-phi value, if |
| 9487 | // so, scan to see if the phi cycle is actually equal to that value. |
| 9488 | { |
| 9489 | unsigned InValNo = 0, NumOperandVals = PN.getNumIncomingValues(); |
| 9490 | // Scan for the first non-phi operand. |
| 9491 | while (InValNo != NumOperandVals && |
| 9492 | isa<PHINode>(PN.getIncomingValue(InValNo))) |
| 9493 | ++InValNo; |
| 9494 | |
| 9495 | if (InValNo != NumOperandVals) { |
| 9496 | Value *NonPhiInVal = PN.getOperand(InValNo); |
| 9497 | |
| 9498 | // Scan the rest of the operands to see if there are any conflicts, if so |
| 9499 | // there is no need to recursively scan other phis. |
| 9500 | for (++InValNo; InValNo != NumOperandVals; ++InValNo) { |
| 9501 | Value *OpVal = PN.getIncomingValue(InValNo); |
| 9502 | if (OpVal != NonPhiInVal && !isa<PHINode>(OpVal)) |
| 9503 | break; |
| 9504 | } |
| 9505 | |
| 9506 | // If we scanned over all operands, then we have one unique value plus |
| 9507 | // phi values. Scan PHI nodes to see if they all merge in each other or |
| 9508 | // the value. |
| 9509 | if (InValNo == NumOperandVals) { |
| 9510 | SmallPtrSet<PHINode*, 16> ValueEqualPHIs; |
| 9511 | if (PHIsEqualValue(&PN, NonPhiInVal, ValueEqualPHIs)) |
| 9512 | return ReplaceInstUsesWith(PN, NonPhiInVal); |
| 9513 | } |
| 9514 | } |
| 9515 | } |
Chris Lattner | 60921c9 | 2003-12-19 05:58:40 +0000 | [diff] [blame] | 9516 | return 0; |
Chris Lattner | 473945d | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 9517 | } |
| 9518 | |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9519 | static Value *InsertCastToIntPtrTy(Value *V, const Type *DTy, |
| 9520 | Instruction *InsertPoint, |
| 9521 | InstCombiner *IC) { |
Reid Spencer | abaa8ca | 2007-01-08 16:32:00 +0000 | [diff] [blame] | 9522 | unsigned PtrSize = DTy->getPrimitiveSizeInBits(); |
| 9523 | unsigned VTySize = V->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9524 | // We must cast correctly to the pointer type. Ensure that we |
| 9525 | // sign extend the integer value if it is smaller as this is |
| 9526 | // used for address computation. |
| 9527 | Instruction::CastOps opcode = |
| 9528 | (VTySize < PtrSize ? Instruction::SExt : |
| 9529 | (VTySize == PtrSize ? Instruction::BitCast : Instruction::Trunc)); |
| 9530 | return IC->InsertCastBefore(opcode, V, DTy, *InsertPoint); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9531 | } |
| 9532 | |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 9533 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 9534 | Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9535 | Value *PtrOp = GEP.getOperand(0); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 9536 | // Is it 'getelementptr %P, i32 0' or 'getelementptr %P' |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 9537 | // If so, eliminate the noop. |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 9538 | if (GEP.getNumOperands() == 1) |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9539 | return ReplaceInstUsesWith(GEP, PtrOp); |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 9540 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 9541 | if (isa<UndefValue>(GEP.getOperand(0))) |
| 9542 | return ReplaceInstUsesWith(GEP, UndefValue::get(GEP.getType())); |
| 9543 | |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 9544 | bool HasZeroPointerIndex = false; |
| 9545 | if (Constant *C = dyn_cast<Constant>(GEP.getOperand(1))) |
| 9546 | HasZeroPointerIndex = C->isNullValue(); |
| 9547 | |
| 9548 | if (GEP.getNumOperands() == 2 && HasZeroPointerIndex) |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9549 | return ReplaceInstUsesWith(GEP, PtrOp); |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 9550 | |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9551 | // Eliminate unneeded casts for indices. |
| 9552 | bool MadeChange = false; |
Chris Lattner | db9654e | 2007-03-25 20:43:09 +0000 | [diff] [blame] | 9553 | |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 9554 | gep_type_iterator GTI = gep_type_begin(GEP); |
Chris Lattner | db9654e | 2007-03-25 20:43:09 +0000 | [diff] [blame] | 9555 | for (unsigned i = 1, e = GEP.getNumOperands(); i != e; ++i, ++GTI) { |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 9556 | if (isa<SequentialType>(*GTI)) { |
| 9557 | if (CastInst *CI = dyn_cast<CastInst>(GEP.getOperand(i))) { |
Chris Lattner | 76b7a06 | 2007-01-15 07:02:54 +0000 | [diff] [blame] | 9558 | if (CI->getOpcode() == Instruction::ZExt || |
| 9559 | CI->getOpcode() == Instruction::SExt) { |
| 9560 | const Type *SrcTy = CI->getOperand(0)->getType(); |
| 9561 | // We can eliminate a cast from i32 to i64 iff the target |
| 9562 | // is a 32-bit pointer target. |
| 9563 | if (SrcTy->getPrimitiveSizeInBits() >= TD->getPointerSizeInBits()) { |
| 9564 | MadeChange = true; |
| 9565 | GEP.setOperand(i, CI->getOperand(0)); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9566 | } |
| 9567 | } |
| 9568 | } |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 9569 | // If we are using a wider index than needed for this platform, shrink it |
| 9570 | // to what we need. If the incoming value needs a cast instruction, |
| 9571 | // insert it. This explicit cast can make subsequent optimizations more |
| 9572 | // obvious. |
| 9573 | Value *Op = GEP.getOperand(i); |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 9574 | if (TD->getTypeSizeInBits(Op->getType()) > TD->getPointerSizeInBits()) { |
Chris Lattner | 4f1134e | 2004-04-17 18:16:10 +0000 | [diff] [blame] | 9575 | if (Constant *C = dyn_cast<Constant>(Op)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9576 | GEP.setOperand(i, ConstantExpr::getTrunc(C, TD->getIntPtrType())); |
Chris Lattner | 4f1134e | 2004-04-17 18:16:10 +0000 | [diff] [blame] | 9577 | MadeChange = true; |
| 9578 | } else { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9579 | Op = InsertCastBefore(Instruction::Trunc, Op, TD->getIntPtrType(), |
| 9580 | GEP); |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 9581 | GEP.setOperand(i, Op); |
| 9582 | MadeChange = true; |
| 9583 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 9584 | } |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9585 | } |
Chris Lattner | db9654e | 2007-03-25 20:43:09 +0000 | [diff] [blame] | 9586 | } |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9587 | if (MadeChange) return &GEP; |
| 9588 | |
Chris Lattner | db9654e | 2007-03-25 20:43:09 +0000 | [diff] [blame] | 9589 | // If this GEP instruction doesn't move the pointer, and if the input operand |
| 9590 | // is a bitcast of another pointer, just replace the GEP with a bitcast of the |
| 9591 | // real input to the dest type. |
Chris Lattner | 6a94de2 | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 9592 | if (GEP.hasAllZeroIndices()) { |
| 9593 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(GEP.getOperand(0))) { |
| 9594 | // If the bitcast is of an allocation, and the allocation will be |
| 9595 | // converted to match the type of the cast, don't touch this. |
| 9596 | if (isa<AllocationInst>(BCI->getOperand(0))) { |
| 9597 | // See if the bitcast simplifies, if so, don't nuke this GEP yet. |
Chris Lattner | a79dd43 | 2007-10-12 18:05:47 +0000 | [diff] [blame] | 9598 | if (Instruction *I = visitBitCast(*BCI)) { |
| 9599 | if (I != BCI) { |
| 9600 | I->takeName(BCI); |
| 9601 | BCI->getParent()->getInstList().insert(BCI, I); |
| 9602 | ReplaceInstUsesWith(*BCI, I); |
| 9603 | } |
Chris Lattner | 6a94de2 | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 9604 | return &GEP; |
Chris Lattner | a79dd43 | 2007-10-12 18:05:47 +0000 | [diff] [blame] | 9605 | } |
Chris Lattner | 6a94de2 | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 9606 | } |
| 9607 | return new BitCastInst(BCI->getOperand(0), GEP.getType()); |
| 9608 | } |
| 9609 | } |
| 9610 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9611 | // Combine Indices - If the source pointer to this getelementptr instruction |
| 9612 | // is a getelementptr instruction, combine the indices of the two |
| 9613 | // getelementptr instructions into a single instruction. |
| 9614 | // |
Chris Lattner | 72588fc | 2007-02-15 22:48:32 +0000 | [diff] [blame] | 9615 | SmallVector<Value*, 8> SrcGEPOperands; |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 9616 | if (User *Src = dyn_castGetElementPtr(PtrOp)) |
Chris Lattner | 72588fc | 2007-02-15 22:48:32 +0000 | [diff] [blame] | 9617 | SrcGEPOperands.append(Src->op_begin(), Src->op_end()); |
Chris Lattner | ebd985c | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 9618 | |
| 9619 | if (!SrcGEPOperands.empty()) { |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9620 | // Note that if our source is a gep chain itself that we wait for that |
| 9621 | // chain to be resolved before we perform this transformation. This |
| 9622 | // avoids us creating a TON of code in some cases. |
| 9623 | // |
| 9624 | if (isa<GetElementPtrInst>(SrcGEPOperands[0]) && |
| 9625 | cast<Instruction>(SrcGEPOperands[0])->getNumOperands() == 2) |
| 9626 | return 0; // Wait until our source is folded to completion. |
| 9627 | |
Chris Lattner | 72588fc | 2007-02-15 22:48:32 +0000 | [diff] [blame] | 9628 | SmallVector<Value*, 8> Indices; |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9629 | |
| 9630 | // Find out whether the last index in the source GEP is a sequential idx. |
| 9631 | bool EndsWithSequential = false; |
| 9632 | for (gep_type_iterator I = gep_type_begin(*cast<User>(PtrOp)), |
| 9633 | E = gep_type_end(*cast<User>(PtrOp)); I != E; ++I) |
Chris Lattner | be97b4e | 2004-05-08 22:41:42 +0000 | [diff] [blame] | 9634 | EndsWithSequential = !isa<StructType>(*I); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9635 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9636 | // Can we combine the two pointer arithmetics offsets? |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9637 | if (EndsWithSequential) { |
Chris Lattner | decd081 | 2003-03-05 22:33:14 +0000 | [diff] [blame] | 9638 | // Replace: gep (gep %P, long B), long A, ... |
| 9639 | // With: T = long A+B; gep %P, T, ... |
| 9640 | // |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9641 | Value *Sum, *SO1 = SrcGEPOperands.back(), *GO1 = GEP.getOperand(1); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9642 | if (SO1 == Constant::getNullValue(SO1->getType())) { |
| 9643 | Sum = GO1; |
| 9644 | } else if (GO1 == Constant::getNullValue(GO1->getType())) { |
| 9645 | Sum = SO1; |
| 9646 | } else { |
| 9647 | // If they aren't the same type, convert both to an integer of the |
| 9648 | // target's pointer size. |
| 9649 | if (SO1->getType() != GO1->getType()) { |
| 9650 | if (Constant *SO1C = dyn_cast<Constant>(SO1)) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9651 | SO1 = ConstantExpr::getIntegerCast(SO1C, GO1->getType(), true); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9652 | } else if (Constant *GO1C = dyn_cast<Constant>(GO1)) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9653 | GO1 = ConstantExpr::getIntegerCast(GO1C, SO1->getType(), true); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9654 | } else { |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 9655 | unsigned PS = TD->getPointerSizeInBits(); |
| 9656 | if (TD->getTypeSizeInBits(SO1->getType()) == PS) { |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9657 | // Convert GO1 to SO1's type. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9658 | GO1 = InsertCastToIntPtrTy(GO1, SO1->getType(), &GEP, this); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9659 | |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 9660 | } else if (TD->getTypeSizeInBits(GO1->getType()) == PS) { |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9661 | // Convert SO1 to GO1's type. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9662 | SO1 = InsertCastToIntPtrTy(SO1, GO1->getType(), &GEP, this); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9663 | } else { |
| 9664 | const Type *PT = TD->getIntPtrType(); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9665 | SO1 = InsertCastToIntPtrTy(SO1, PT, &GEP, this); |
| 9666 | GO1 = InsertCastToIntPtrTy(GO1, PT, &GEP, this); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9667 | } |
| 9668 | } |
| 9669 | } |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9670 | if (isa<Constant>(SO1) && isa<Constant>(GO1)) |
| 9671 | Sum = ConstantExpr::getAdd(cast<Constant>(SO1), cast<Constant>(GO1)); |
| 9672 | else { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 9673 | Sum = BinaryOperator::createAdd(SO1, GO1, PtrOp->getName()+".sum"); |
| 9674 | InsertNewInstBefore(cast<Instruction>(Sum), GEP); |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9675 | } |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9676 | } |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9677 | |
| 9678 | // Recycle the GEP we already have if possible. |
| 9679 | if (SrcGEPOperands.size() == 2) { |
| 9680 | GEP.setOperand(0, SrcGEPOperands[0]); |
| 9681 | GEP.setOperand(1, Sum); |
| 9682 | return &GEP; |
| 9683 | } else { |
| 9684 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, |
| 9685 | SrcGEPOperands.end()-1); |
| 9686 | Indices.push_back(Sum); |
| 9687 | Indices.insert(Indices.end(), GEP.op_begin()+2, GEP.op_end()); |
| 9688 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9689 | } else if (isa<Constant>(*GEP.idx_begin()) && |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9690 | cast<Constant>(*GEP.idx_begin())->isNullValue() && |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9691 | SrcGEPOperands.size() != 1) { |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9692 | // 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] | 9693 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, |
| 9694 | SrcGEPOperands.end()); |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9695 | Indices.insert(Indices.end(), GEP.idx_begin()+1, GEP.idx_end()); |
| 9696 | } |
| 9697 | |
| 9698 | if (!Indices.empty()) |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9699 | return GetElementPtrInst::Create(SrcGEPOperands[0], Indices.begin(), |
| 9700 | Indices.end(), GEP.getName()); |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 9701 | |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9702 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(PtrOp)) { |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 9703 | // GEP of global variable. If all of the indices for this GEP are |
| 9704 | // constants, we can promote this to a constexpr instead of an instruction. |
| 9705 | |
| 9706 | // Scan for nonconstants... |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 9707 | SmallVector<Constant*, 8> Indices; |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 9708 | User::op_iterator I = GEP.idx_begin(), E = GEP.idx_end(); |
| 9709 | for (; I != E && isa<Constant>(*I); ++I) |
| 9710 | Indices.push_back(cast<Constant>(*I)); |
| 9711 | |
| 9712 | if (I == E) { // If they are all constants... |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 9713 | Constant *CE = ConstantExpr::getGetElementPtr(GV, |
| 9714 | &Indices[0],Indices.size()); |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 9715 | |
| 9716 | // Replace all uses of the GEP with the new constexpr... |
| 9717 | return ReplaceInstUsesWith(GEP, CE); |
| 9718 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9719 | } else if (Value *X = getBitCastOperand(PtrOp)) { // Is the operand a cast? |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 9720 | if (!isa<PointerType>(X->getType())) { |
| 9721 | // Not interesting. Source pointer must be a cast from pointer. |
| 9722 | } else if (HasZeroPointerIndex) { |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9723 | // transform: GEP (bitcast [10 x i8]* X to [0 x i8]*), i32 0, ... |
| 9724 | // into : GEP [10 x i8]* X, i32 0, ... |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 9725 | // |
| 9726 | // This occurs when the program declares an array extern like "int X[];" |
| 9727 | // |
| 9728 | const PointerType *CPTy = cast<PointerType>(PtrOp->getType()); |
| 9729 | const PointerType *XTy = cast<PointerType>(X->getType()); |
| 9730 | if (const ArrayType *XATy = |
| 9731 | dyn_cast<ArrayType>(XTy->getElementType())) |
| 9732 | if (const ArrayType *CATy = |
| 9733 | dyn_cast<ArrayType>(CPTy->getElementType())) |
| 9734 | if (CATy->getElementType() == XATy->getElementType()) { |
| 9735 | // At this point, we know that the cast source type is a pointer |
| 9736 | // to an array of the same type as the destination pointer |
| 9737 | // array. Because the array type is never stepped over (there |
| 9738 | // is a leading zero) we can fold the cast into this GEP. |
| 9739 | GEP.setOperand(0, X); |
| 9740 | return &GEP; |
| 9741 | } |
| 9742 | } else if (GEP.getNumOperands() == 2) { |
| 9743 | // Transform things like: |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9744 | // %t = getelementptr i32* bitcast ([2 x i32]* %str to i32*), i32 %V |
| 9745 | // into: %t1 = getelementptr [2 x i32]* %str, i32 0, i32 %V; bitcast |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 9746 | const Type *SrcElTy = cast<PointerType>(X->getType())->getElementType(); |
| 9747 | const Type *ResElTy=cast<PointerType>(PtrOp->getType())->getElementType(); |
| 9748 | if (isa<ArrayType>(SrcElTy) && |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 9749 | TD->getABITypeSize(cast<ArrayType>(SrcElTy)->getElementType()) == |
| 9750 | TD->getABITypeSize(ResElTy)) { |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 9751 | Value *Idx[2]; |
| 9752 | Idx[0] = Constant::getNullValue(Type::Int32Ty); |
| 9753 | Idx[1] = GEP.getOperand(1); |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 9754 | Value *V = InsertNewInstBefore( |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9755 | GetElementPtrInst::Create(X, Idx, Idx + 2, GEP.getName()), GEP); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9756 | // V and GEP are both pointer types --> BitCast |
| 9757 | return new BitCastInst(V, GEP.getType()); |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 9758 | } |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9759 | |
| 9760 | // Transform things like: |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9761 | // getelementptr i8* bitcast ([100 x double]* X to i8*), i32 %tmp |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9762 | // (where tmp = 8*tmp2) into: |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9763 | // getelementptr [100 x double]* %arr, i32 0, i32 %tmp2; bitcast |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9764 | |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9765 | if (isa<ArrayType>(SrcElTy) && ResElTy == Type::Int8Ty) { |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9766 | uint64_t ArrayEltSize = |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 9767 | TD->getABITypeSize(cast<ArrayType>(SrcElTy)->getElementType()); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9768 | |
| 9769 | // Check to see if "tmp" is a scale by a multiple of ArrayEltSize. We |
| 9770 | // allow either a mul, shift, or constant here. |
| 9771 | Value *NewIdx = 0; |
| 9772 | ConstantInt *Scale = 0; |
| 9773 | if (ArrayEltSize == 1) { |
| 9774 | NewIdx = GEP.getOperand(1); |
| 9775 | Scale = ConstantInt::get(NewIdx->getType(), 1); |
| 9776 | } else if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP.getOperand(1))) { |
Chris Lattner | 6e2f843 | 2005-09-14 17:32:56 +0000 | [diff] [blame] | 9777 | NewIdx = ConstantInt::get(CI->getType(), 1); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9778 | Scale = CI; |
| 9779 | } else if (Instruction *Inst =dyn_cast<Instruction>(GEP.getOperand(1))){ |
| 9780 | if (Inst->getOpcode() == Instruction::Shl && |
| 9781 | isa<ConstantInt>(Inst->getOperand(1))) { |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 9782 | ConstantInt *ShAmt = cast<ConstantInt>(Inst->getOperand(1)); |
| 9783 | uint32_t ShAmtVal = ShAmt->getLimitedValue(64); |
| 9784 | Scale = ConstantInt::get(Inst->getType(), 1ULL << ShAmtVal); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9785 | NewIdx = Inst->getOperand(0); |
| 9786 | } else if (Inst->getOpcode() == Instruction::Mul && |
| 9787 | isa<ConstantInt>(Inst->getOperand(1))) { |
| 9788 | Scale = cast<ConstantInt>(Inst->getOperand(1)); |
| 9789 | NewIdx = Inst->getOperand(0); |
| 9790 | } |
| 9791 | } |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9792 | |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9793 | // 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] | 9794 | // out, perform the transformation. Note, we don't know whether Scale is |
| 9795 | // signed or not. We'll use unsigned version of division/modulo |
| 9796 | // operation after making sure Scale doesn't have the sign bit set. |
| 9797 | if (Scale && Scale->getSExtValue() >= 0LL && |
| 9798 | Scale->getZExtValue() % ArrayEltSize == 0) { |
| 9799 | Scale = ConstantInt::get(Scale->getType(), |
| 9800 | Scale->getZExtValue() / ArrayEltSize); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 9801 | if (Scale->getZExtValue() != 1) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9802 | Constant *C = ConstantExpr::getIntegerCast(Scale, NewIdx->getType(), |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9803 | false /*ZExt*/); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9804 | Instruction *Sc = BinaryOperator::createMul(NewIdx, C, "idxscale"); |
| 9805 | NewIdx = InsertNewInstBefore(Sc, GEP); |
| 9806 | } |
| 9807 | |
| 9808 | // Insert the new GEP instruction. |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 9809 | Value *Idx[2]; |
| 9810 | Idx[0] = Constant::getNullValue(Type::Int32Ty); |
| 9811 | Idx[1] = NewIdx; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9812 | Instruction *NewGEP = |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9813 | GetElementPtrInst::Create(X, Idx, Idx + 2, GEP.getName()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9814 | NewGEP = InsertNewInstBefore(NewGEP, GEP); |
| 9815 | // The NewGEP must be pointer typed, so must the old one -> BitCast |
| 9816 | return new BitCastInst(NewGEP, GEP.getType()); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9817 | } |
| 9818 | } |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 9819 | } |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9820 | } |
| 9821 | |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9822 | return 0; |
| 9823 | } |
| 9824 | |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 9825 | Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) { |
| 9826 | // 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] | 9827 | if (AI.isArrayAllocation()) { // Check C != 1 |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 9828 | if (const ConstantInt *C = dyn_cast<ConstantInt>(AI.getArraySize())) { |
| 9829 | const Type *NewTy = |
| 9830 | ArrayType::get(AI.getAllocatedType(), C->getZExtValue()); |
Chris Lattner | 0006bd7 | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 9831 | AllocationInst *New = 0; |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 9832 | |
| 9833 | // Create and insert the replacement instruction... |
| 9834 | if (isa<MallocInst>(AI)) |
Nate Begeman | 14b0529 | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 9835 | New = new MallocInst(NewTy, 0, AI.getAlignment(), AI.getName()); |
Chris Lattner | 0006bd7 | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 9836 | else { |
| 9837 | assert(isa<AllocaInst>(AI) && "Unknown type of allocation inst!"); |
Nate Begeman | 14b0529 | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 9838 | New = new AllocaInst(NewTy, 0, AI.getAlignment(), AI.getName()); |
Chris Lattner | 0006bd7 | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 9839 | } |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 9840 | |
| 9841 | InsertNewInstBefore(New, AI); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9842 | |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 9843 | // Scan to the end of the allocation instructions, to skip over a block of |
| 9844 | // allocas if possible... |
| 9845 | // |
| 9846 | BasicBlock::iterator It = New; |
| 9847 | while (isa<AllocationInst>(*It)) ++It; |
| 9848 | |
| 9849 | // Now that I is pointing to the first non-allocation-inst in the block, |
| 9850 | // insert our getelementptr instruction... |
| 9851 | // |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9852 | Value *NullIdx = Constant::getNullValue(Type::Int32Ty); |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 9853 | Value *Idx[2]; |
| 9854 | Idx[0] = NullIdx; |
| 9855 | Idx[1] = NullIdx; |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9856 | Value *V = GetElementPtrInst::Create(New, Idx, Idx + 2, |
| 9857 | New->getName()+".sub", It); |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 9858 | |
| 9859 | // Now make everything use the getelementptr instead of the original |
| 9860 | // allocation. |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 9861 | return ReplaceInstUsesWith(AI, V); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 9862 | } else if (isa<UndefValue>(AI.getArraySize())) { |
| 9863 | return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 9864 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 9865 | } |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 9866 | |
| 9867 | // If alloca'ing a zero byte object, replace the alloca with a null pointer. |
| 9868 | // Note that we only do this for alloca's, because malloc should allocate and |
| 9869 | // return a unique pointer, even for a zero byte allocation. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9870 | if (isa<AllocaInst>(AI) && AI.getAllocatedType()->isSized() && |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 9871 | TD->getABITypeSize(AI.getAllocatedType()) == 0) |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 9872 | return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); |
| 9873 | |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 9874 | return 0; |
| 9875 | } |
| 9876 | |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 9877 | Instruction *InstCombiner::visitFreeInst(FreeInst &FI) { |
| 9878 | Value *Op = FI.getOperand(0); |
| 9879 | |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 9880 | // free undef -> unreachable. |
| 9881 | if (isa<UndefValue>(Op)) { |
| 9882 | // 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] | 9883 | new StoreInst(ConstantInt::getTrue(), |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 9884 | UndefValue::get(PointerType::getUnqual(Type::Int1Ty)), &FI); |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 9885 | return EraseInstFromFunction(FI); |
| 9886 | } |
Chris Lattner | 6fe5541 | 2007-04-14 00:20:02 +0000 | [diff] [blame] | 9887 | |
Chris Lattner | 6160e85 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 9888 | // If we have 'free null' delete the instruction. This can happen in stl code |
| 9889 | // when lots of inlining happens. |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 9890 | if (isa<ConstantPointerNull>(Op)) |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 9891 | return EraseInstFromFunction(FI); |
Chris Lattner | 6fe5541 | 2007-04-14 00:20:02 +0000 | [diff] [blame] | 9892 | |
| 9893 | // Change free <ty>* (cast <ty2>* X to <ty>*) into free <ty2>* X |
| 9894 | if (BitCastInst *CI = dyn_cast<BitCastInst>(Op)) { |
| 9895 | FI.setOperand(0, CI->getOperand(0)); |
| 9896 | return &FI; |
| 9897 | } |
| 9898 | |
| 9899 | // Change free (gep X, 0,0,0,0) into free(X) |
| 9900 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) { |
| 9901 | if (GEPI->hasAllZeroIndices()) { |
| 9902 | AddToWorkList(GEPI); |
| 9903 | FI.setOperand(0, GEPI->getOperand(0)); |
| 9904 | return &FI; |
| 9905 | } |
| 9906 | } |
| 9907 | |
| 9908 | // Change free(malloc) into nothing, if the malloc has a single use. |
| 9909 | if (MallocInst *MI = dyn_cast<MallocInst>(Op)) |
| 9910 | if (MI->hasOneUse()) { |
| 9911 | EraseInstFromFunction(FI); |
| 9912 | return EraseInstFromFunction(*MI); |
| 9913 | } |
Chris Lattner | 6160e85 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 9914 | |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 9915 | return 0; |
| 9916 | } |
| 9917 | |
| 9918 | |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 9919 | /// InstCombineLoadCast - Fold 'load (cast P)' -> cast (load P)' when possible. |
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 9920 | static Instruction *InstCombineLoadCast(InstCombiner &IC, LoadInst &LI, |
Bill Wendling | 587c01d | 2008-02-26 10:53:30 +0000 | [diff] [blame] | 9921 | const TargetData *TD) { |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 9922 | User *CI = cast<User>(LI.getOperand(0)); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 9923 | Value *CastOp = CI->getOperand(0); |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 9924 | |
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 9925 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(CI)) { |
| 9926 | // Instead of loading constant c string, use corresponding integer value |
| 9927 | // directly if string length is small enough. |
| 9928 | const std::string &Str = CE->getOperand(0)->getStringValue(); |
| 9929 | if (!Str.empty()) { |
| 9930 | unsigned len = Str.length(); |
| 9931 | const Type *Ty = cast<PointerType>(CE->getType())->getElementType(); |
| 9932 | unsigned numBits = Ty->getPrimitiveSizeInBits(); |
| 9933 | // Replace LI with immediate integer store. |
| 9934 | if ((numBits >> 3) == len + 1) { |
Bill Wendling | 587c01d | 2008-02-26 10:53:30 +0000 | [diff] [blame] | 9935 | APInt StrVal(numBits, 0); |
| 9936 | APInt SingleChar(numBits, 0); |
| 9937 | if (TD->isLittleEndian()) { |
| 9938 | for (signed i = len-1; i >= 0; i--) { |
| 9939 | SingleChar = (uint64_t) Str[i]; |
| 9940 | StrVal = (StrVal << 8) | SingleChar; |
| 9941 | } |
| 9942 | } else { |
| 9943 | for (unsigned i = 0; i < len; i++) { |
| 9944 | SingleChar = (uint64_t) Str[i]; |
| 9945 | StrVal = (StrVal << 8) | SingleChar; |
| 9946 | } |
| 9947 | // Append NULL at the end. |
| 9948 | SingleChar = 0; |
| 9949 | StrVal = (StrVal << 8) | SingleChar; |
| 9950 | } |
| 9951 | Value *NL = ConstantInt::get(StrVal); |
| 9952 | return IC.ReplaceInstUsesWith(LI, NL); |
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 9953 | } |
| 9954 | } |
| 9955 | } |
| 9956 | |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 9957 | const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType(); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 9958 | if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) { |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 9959 | const Type *SrcPTy = SrcTy->getElementType(); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 9960 | |
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 9961 | if (DestPTy->isInteger() || isa<PointerType>(DestPTy) || |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 9962 | isa<VectorType>(DestPTy)) { |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 9963 | // If the source is an array, the code below will not succeed. Check to |
| 9964 | // see if a trivial 'gep P, 0, 0' will help matters. Only do this for |
| 9965 | // constants. |
| 9966 | if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy)) |
| 9967 | if (Constant *CSrc = dyn_cast<Constant>(CastOp)) |
| 9968 | if (ASrcTy->getNumElements() != 0) { |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 9969 | Value *Idxs[2]; |
| 9970 | Idxs[0] = Idxs[1] = Constant::getNullValue(Type::Int32Ty); |
| 9971 | CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs, 2); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 9972 | SrcTy = cast<PointerType>(CastOp->getType()); |
| 9973 | SrcPTy = SrcTy->getElementType(); |
| 9974 | } |
| 9975 | |
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 9976 | if ((SrcPTy->isInteger() || isa<PointerType>(SrcPTy) || |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 9977 | isa<VectorType>(SrcPTy)) && |
Chris Lattner | b1515fe | 2005-03-29 06:37:47 +0000 | [diff] [blame] | 9978 | // Do not allow turning this into a load of an integer, which is then |
| 9979 | // casted to a pointer, this pessimizes pointer analysis a lot. |
| 9980 | (isa<PointerType>(SrcPTy) == isa<PointerType>(LI.getType())) && |
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 9981 | IC.getTargetData().getTypeSizeInBits(SrcPTy) == |
| 9982 | IC.getTargetData().getTypeSizeInBits(DestPTy)) { |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9983 | |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 9984 | // Okay, we are casting from one integer or pointer type to another of |
| 9985 | // the same size. Instead of casting the pointer before the load, cast |
| 9986 | // the result of the loaded value. |
| 9987 | Value *NewLoad = IC.InsertNewInstBefore(new LoadInst(CastOp, |
| 9988 | CI->getName(), |
| 9989 | LI.isVolatile()),LI); |
| 9990 | // Now cast the result of the load. |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 9991 | return new BitCastInst(NewLoad, LI.getType()); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 9992 | } |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 9993 | } |
| 9994 | } |
| 9995 | return 0; |
| 9996 | } |
| 9997 | |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 9998 | /// isSafeToLoadUnconditionally - Return true if we know that executing a load |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 9999 | /// from this value cannot trap. If it is not obviously safe to load from the |
| 10000 | /// specified pointer, we do a quick local scan of the basic block containing |
| 10001 | /// ScanFrom, to determine if the address is already accessed. |
| 10002 | static bool isSafeToLoadUnconditionally(Value *V, Instruction *ScanFrom) { |
Duncan Sands | 892c7e4 | 2007-09-19 10:10:31 +0000 | [diff] [blame] | 10003 | // If it is an alloca it is always safe to load from. |
| 10004 | if (isa<AllocaInst>(V)) return true; |
| 10005 | |
Duncan Sands | 46318cd | 2007-09-19 10:25:38 +0000 | [diff] [blame] | 10006 | // 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] | 10007 | if (const GlobalValue *GV = dyn_cast<GlobalVariable>(V)) |
Duncan Sands | 46318cd | 2007-09-19 10:25:38 +0000 | [diff] [blame] | 10008 | // Don't try to evaluate aliases. External weak GV can be null. |
Duncan Sands | 892c7e4 | 2007-09-19 10:10:31 +0000 | [diff] [blame] | 10009 | return !isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage(); |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 10010 | |
| 10011 | // Otherwise, be a little bit agressive by scanning the local block where we |
| 10012 | // 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] | 10013 | // from/to. If so, the previous load or store would have already trapped, |
| 10014 | // so there is no harm doing an extra load (also, CSE will later eliminate |
| 10015 | // the load entirely). |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 10016 | BasicBlock::iterator BBI = ScanFrom, E = ScanFrom->getParent()->begin(); |
| 10017 | |
Alkis Evlogimenos | 7b6ec60 | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 10018 | while (BBI != E) { |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 10019 | --BBI; |
| 10020 | |
| 10021 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { |
| 10022 | if (LI->getOperand(0) == V) return true; |
| 10023 | } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) |
| 10024 | if (SI->getOperand(1) == V) return true; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 10025 | |
Alkis Evlogimenos | 7b6ec60 | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 10026 | } |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 10027 | return false; |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 10028 | } |
| 10029 | |
Chris Lattner | 8d2e888 | 2007-08-11 18:48:48 +0000 | [diff] [blame] | 10030 | /// GetUnderlyingObject - Trace through a series of getelementptrs and bitcasts |
| 10031 | /// until we find the underlying object a pointer is referring to or something |
| 10032 | /// we don't understand. Note that the returned pointer may be offset from the |
| 10033 | /// input, because we ignore GEP indices. |
| 10034 | static Value *GetUnderlyingObject(Value *Ptr) { |
| 10035 | while (1) { |
| 10036 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) { |
| 10037 | if (CE->getOpcode() == Instruction::BitCast || |
| 10038 | CE->getOpcode() == Instruction::GetElementPtr) |
| 10039 | Ptr = CE->getOperand(0); |
| 10040 | else |
| 10041 | return Ptr; |
| 10042 | } else if (BitCastInst *BCI = dyn_cast<BitCastInst>(Ptr)) { |
| 10043 | Ptr = BCI->getOperand(0); |
| 10044 | } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) { |
| 10045 | Ptr = GEP->getOperand(0); |
| 10046 | } else { |
| 10047 | return Ptr; |
| 10048 | } |
| 10049 | } |
| 10050 | } |
| 10051 | |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 10052 | Instruction *InstCombiner::visitLoadInst(LoadInst &LI) { |
| 10053 | Value *Op = LI.getOperand(0); |
Chris Lattner | 5f16a13 | 2004-01-12 04:13:56 +0000 | [diff] [blame] | 10054 | |
Dan Gohman | 9941f74 | 2007-07-20 16:34:21 +0000 | [diff] [blame] | 10055 | // Attempt to improve the alignment. |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 10056 | unsigned KnownAlign = GetOrEnforceKnownAlignment(Op); |
| 10057 | if (KnownAlign > |
| 10058 | (LI.getAlignment() == 0 ? TD->getABITypeAlignment(LI.getType()) : |
| 10059 | LI.getAlignment())) |
Dan Gohman | 9941f74 | 2007-07-20 16:34:21 +0000 | [diff] [blame] | 10060 | LI.setAlignment(KnownAlign); |
| 10061 | |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 10062 | // load (cast X) --> cast (load X) iff safe |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 10063 | if (isa<CastInst>(Op)) |
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 10064 | if (Instruction *Res = InstCombineLoadCast(*this, LI, TD)) |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 10065 | return Res; |
| 10066 | |
| 10067 | // None of the following transforms are legal for volatile loads. |
| 10068 | if (LI.isVolatile()) return 0; |
Chris Lattner | 62f254d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 10069 | |
Chris Lattner | 62f254d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 10070 | if (&LI.getParent()->front() != &LI) { |
| 10071 | BasicBlock::iterator BBI = &LI; --BBI; |
Chris Lattner | 9c1f0fd | 2005-09-12 22:21:03 +0000 | [diff] [blame] | 10072 | // If the instruction immediately before this is a store to the same |
| 10073 | // address, do a simple form of store->load forwarding. |
Chris Lattner | 62f254d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 10074 | if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) |
| 10075 | if (SI->getOperand(1) == LI.getOperand(0)) |
| 10076 | return ReplaceInstUsesWith(LI, SI->getOperand(0)); |
Chris Lattner | 9c1f0fd | 2005-09-12 22:21:03 +0000 | [diff] [blame] | 10077 | if (LoadInst *LIB = dyn_cast<LoadInst>(BBI)) |
| 10078 | if (LIB->getOperand(0) == LI.getOperand(0)) |
| 10079 | return ReplaceInstUsesWith(LI, LIB); |
Chris Lattner | 62f254d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 10080 | } |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 10081 | |
Christopher Lamb | b15147e | 2007-12-29 07:56:53 +0000 | [diff] [blame] | 10082 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) { |
| 10083 | const Value *GEPI0 = GEPI->getOperand(0); |
| 10084 | // TODO: Consider a target hook for valid address spaces for this xform. |
| 10085 | if (isa<ConstantPointerNull>(GEPI0) && |
| 10086 | cast<PointerType>(GEPI0->getType())->getAddressSpace() == 0) { |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 10087 | // Insert a new store to null instruction before the load to indicate |
| 10088 | // that this code is not reachable. We do this instead of inserting |
| 10089 | // an unreachable instruction directly because we cannot modify the |
| 10090 | // CFG. |
| 10091 | new StoreInst(UndefValue::get(LI.getType()), |
| 10092 | Constant::getNullValue(Op->getType()), &LI); |
| 10093 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
| 10094 | } |
Christopher Lamb | b15147e | 2007-12-29 07:56:53 +0000 | [diff] [blame] | 10095 | } |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 10096 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10097 | if (Constant *C = dyn_cast<Constant>(Op)) { |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 10098 | // load null/undef -> undef |
Christopher Lamb | b15147e | 2007-12-29 07:56:53 +0000 | [diff] [blame] | 10099 | // TODO: Consider a target hook for valid address spaces for this xform. |
| 10100 | if (isa<UndefValue>(C) || (C->isNullValue() && |
| 10101 | cast<PointerType>(Op->getType())->getAddressSpace() == 0)) { |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 10102 | // Insert a new store to null instruction before the load to indicate that |
| 10103 | // this code is not reachable. We do this instead of inserting an |
| 10104 | // unreachable instruction directly because we cannot modify the CFG. |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 10105 | new StoreInst(UndefValue::get(LI.getType()), |
| 10106 | Constant::getNullValue(Op->getType()), &LI); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10107 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 10108 | } |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 10109 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10110 | // Instcombine load (constant global) into the value loaded. |
| 10111 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op)) |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 10112 | if (GV->isConstant() && !GV->isDeclaration()) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10113 | return ReplaceInstUsesWith(LI, GV->getInitializer()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 10114 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10115 | // Instcombine load (constantexpr_GEP global, 0, ...) into the value loaded. |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 10116 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op)) { |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10117 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 10118 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0))) |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 10119 | if (GV->isConstant() && !GV->isDeclaration()) |
Chris Lattner | 363f2a2 | 2005-09-26 05:28:06 +0000 | [diff] [blame] | 10120 | if (Constant *V = |
| 10121 | ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE)) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10122 | return ReplaceInstUsesWith(LI, V); |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 10123 | if (CE->getOperand(0)->isNullValue()) { |
| 10124 | // Insert a new store to null instruction before the load to indicate |
| 10125 | // that this code is not reachable. We do this instead of inserting |
| 10126 | // an unreachable instruction directly because we cannot modify the |
| 10127 | // CFG. |
| 10128 | new StoreInst(UndefValue::get(LI.getType()), |
| 10129 | Constant::getNullValue(Op->getType()), &LI); |
| 10130 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
| 10131 | } |
| 10132 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 10133 | } else if (CE->isCast()) { |
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 10134 | if (Instruction *Res = InstCombineLoadCast(*this, LI, TD)) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10135 | return Res; |
| 10136 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 10137 | } |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10138 | } |
Chris Lattner | 8d2e888 | 2007-08-11 18:48:48 +0000 | [diff] [blame] | 10139 | |
| 10140 | // If this load comes from anywhere in a constant global, and if the global |
| 10141 | // is all undef or zero, we know what it loads. |
| 10142 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GetUnderlyingObject(Op))) { |
| 10143 | if (GV->isConstant() && GV->hasInitializer()) { |
| 10144 | if (GV->getInitializer()->isNullValue()) |
| 10145 | return ReplaceInstUsesWith(LI, Constant::getNullValue(LI.getType())); |
| 10146 | else if (isa<UndefValue>(GV->getInitializer())) |
| 10147 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
| 10148 | } |
| 10149 | } |
Chris Lattner | f499eac | 2004-04-08 20:39:49 +0000 | [diff] [blame] | 10150 | |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 10151 | if (Op->hasOneUse()) { |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 10152 | // Change select and PHI nodes to select values instead of addresses: this |
| 10153 | // helps alias analysis out a lot, allows many others simplifications, and |
| 10154 | // exposes redundancy in the code. |
| 10155 | // |
| 10156 | // Note that we cannot do the transformation unless we know that the |
| 10157 | // introduced loads cannot trap! Something like this is valid as long as |
| 10158 | // the condition is always false: load (select bool %C, int* null, int* %G), |
| 10159 | // but it would not be valid if we transformed it to load from null |
| 10160 | // unconditionally. |
| 10161 | // |
| 10162 | if (SelectInst *SI = dyn_cast<SelectInst>(Op)) { |
| 10163 | // load (select (Cond, &V1, &V2)) --> select(Cond, load &V1, load &V2). |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 10164 | if (isSafeToLoadUnconditionally(SI->getOperand(1), SI) && |
| 10165 | isSafeToLoadUnconditionally(SI->getOperand(2), SI)) { |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 10166 | Value *V1 = InsertNewInstBefore(new LoadInst(SI->getOperand(1), |
Chris Lattner | 79f0c8e | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 10167 | SI->getOperand(1)->getName()+".val"), LI); |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 10168 | Value *V2 = InsertNewInstBefore(new LoadInst(SI->getOperand(2), |
Chris Lattner | 79f0c8e | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 10169 | SI->getOperand(2)->getName()+".val"), LI); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 10170 | return SelectInst::Create(SI->getCondition(), V1, V2); |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 10171 | } |
| 10172 | |
Chris Lattner | 684fe21 | 2004-09-23 15:46:00 +0000 | [diff] [blame] | 10173 | // load (select (cond, null, P)) -> load P |
| 10174 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(1))) |
| 10175 | if (C->isNullValue()) { |
| 10176 | LI.setOperand(0, SI->getOperand(2)); |
| 10177 | return &LI; |
| 10178 | } |
| 10179 | |
| 10180 | // load (select (cond, P, null)) -> load P |
| 10181 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(2))) |
| 10182 | if (C->isNullValue()) { |
| 10183 | LI.setOperand(0, SI->getOperand(1)); |
| 10184 | return &LI; |
| 10185 | } |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 10186 | } |
| 10187 | } |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 10188 | return 0; |
| 10189 | } |
| 10190 | |
Reid Spencer | 55af2b5 | 2007-01-19 21:20:31 +0000 | [diff] [blame] | 10191 | /// InstCombineStoreToCast - Fold store V, (cast P) -> store (cast V), P |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10192 | /// when possible. |
| 10193 | static Instruction *InstCombineStoreToCast(InstCombiner &IC, StoreInst &SI) { |
| 10194 | User *CI = cast<User>(SI.getOperand(1)); |
| 10195 | Value *CastOp = CI->getOperand(0); |
| 10196 | |
| 10197 | const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType(); |
| 10198 | if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) { |
| 10199 | const Type *SrcPTy = SrcTy->getElementType(); |
| 10200 | |
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 10201 | if (DestPTy->isInteger() || isa<PointerType>(DestPTy)) { |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10202 | // If the source is an array, the code below will not succeed. Check to |
| 10203 | // see if a trivial 'gep P, 0, 0' will help matters. Only do this for |
| 10204 | // constants. |
| 10205 | if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy)) |
| 10206 | if (Constant *CSrc = dyn_cast<Constant>(CastOp)) |
| 10207 | if (ASrcTy->getNumElements() != 0) { |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 10208 | Value* Idxs[2]; |
| 10209 | Idxs[0] = Idxs[1] = Constant::getNullValue(Type::Int32Ty); |
| 10210 | CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs, 2); |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10211 | SrcTy = cast<PointerType>(CastOp->getType()); |
| 10212 | SrcPTy = SrcTy->getElementType(); |
| 10213 | } |
| 10214 | |
Reid Spencer | 67f827c | 2007-01-20 23:35:48 +0000 | [diff] [blame] | 10215 | if ((SrcPTy->isInteger() || isa<PointerType>(SrcPTy)) && |
| 10216 | IC.getTargetData().getTypeSizeInBits(SrcPTy) == |
| 10217 | IC.getTargetData().getTypeSizeInBits(DestPTy)) { |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10218 | |
| 10219 | // Okay, we are casting from one integer or pointer type to another of |
Reid Spencer | 7515396 | 2007-01-18 18:54:33 +0000 | [diff] [blame] | 10220 | // the same size. Instead of casting the pointer before |
| 10221 | // the store, cast the value to be stored. |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10222 | Value *NewCast; |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 10223 | Value *SIOp0 = SI.getOperand(0); |
Reid Spencer | 7515396 | 2007-01-18 18:54:33 +0000 | [diff] [blame] | 10224 | Instruction::CastOps opcode = Instruction::BitCast; |
| 10225 | const Type* CastSrcTy = SIOp0->getType(); |
| 10226 | const Type* CastDstTy = SrcPTy; |
| 10227 | if (isa<PointerType>(CastDstTy)) { |
| 10228 | if (CastSrcTy->isInteger()) |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 10229 | opcode = Instruction::IntToPtr; |
Reid Spencer | 67f827c | 2007-01-20 23:35:48 +0000 | [diff] [blame] | 10230 | } else if (isa<IntegerType>(CastDstTy)) { |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 10231 | if (isa<PointerType>(SIOp0->getType())) |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 10232 | opcode = Instruction::PtrToInt; |
| 10233 | } |
| 10234 | if (Constant *C = dyn_cast<Constant>(SIOp0)) |
Reid Spencer | 7515396 | 2007-01-18 18:54:33 +0000 | [diff] [blame] | 10235 | NewCast = ConstantExpr::getCast(opcode, C, CastDstTy); |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10236 | else |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 10237 | NewCast = IC.InsertNewInstBefore( |
Reid Spencer | 7515396 | 2007-01-18 18:54:33 +0000 | [diff] [blame] | 10238 | CastInst::create(opcode, SIOp0, CastDstTy, SIOp0->getName()+".c"), |
| 10239 | SI); |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10240 | return new StoreInst(NewCast, CastOp); |
| 10241 | } |
| 10242 | } |
| 10243 | } |
| 10244 | return 0; |
| 10245 | } |
| 10246 | |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10247 | Instruction *InstCombiner::visitStoreInst(StoreInst &SI) { |
| 10248 | Value *Val = SI.getOperand(0); |
| 10249 | Value *Ptr = SI.getOperand(1); |
| 10250 | |
| 10251 | if (isa<UndefValue>(Ptr)) { // store X, undef -> noop (even if volatile) |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 10252 | EraseInstFromFunction(SI); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10253 | ++NumCombined; |
| 10254 | return 0; |
| 10255 | } |
Chris Lattner | 836692d | 2007-01-15 06:51:56 +0000 | [diff] [blame] | 10256 | |
| 10257 | // If the RHS is an alloca with a single use, zapify the store, making the |
| 10258 | // alloca dead. |
| 10259 | if (Ptr->hasOneUse()) { |
| 10260 | if (isa<AllocaInst>(Ptr)) { |
| 10261 | EraseInstFromFunction(SI); |
| 10262 | ++NumCombined; |
| 10263 | return 0; |
| 10264 | } |
| 10265 | |
| 10266 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) |
| 10267 | if (isa<AllocaInst>(GEP->getOperand(0)) && |
| 10268 | GEP->getOperand(0)->hasOneUse()) { |
| 10269 | EraseInstFromFunction(SI); |
| 10270 | ++NumCombined; |
| 10271 | return 0; |
| 10272 | } |
| 10273 | } |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10274 | |
Dan Gohman | 9941f74 | 2007-07-20 16:34:21 +0000 | [diff] [blame] | 10275 | // Attempt to improve the alignment. |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 10276 | unsigned KnownAlign = GetOrEnforceKnownAlignment(Ptr); |
| 10277 | if (KnownAlign > |
| 10278 | (SI.getAlignment() == 0 ? TD->getABITypeAlignment(Val->getType()) : |
| 10279 | SI.getAlignment())) |
Dan Gohman | 9941f74 | 2007-07-20 16:34:21 +0000 | [diff] [blame] | 10280 | SI.setAlignment(KnownAlign); |
| 10281 | |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 10282 | // Do really simple DSE, to catch cases where there are several consequtive |
| 10283 | // stores to the same location, separated by a few arithmetic operations. This |
| 10284 | // situation often occurs with bitfield accesses. |
| 10285 | BasicBlock::iterator BBI = &SI; |
| 10286 | for (unsigned ScanInsts = 6; BBI != SI.getParent()->begin() && ScanInsts; |
| 10287 | --ScanInsts) { |
| 10288 | --BBI; |
| 10289 | |
| 10290 | if (StoreInst *PrevSI = dyn_cast<StoreInst>(BBI)) { |
| 10291 | // Prev store isn't volatile, and stores to the same location? |
| 10292 | if (!PrevSI->isVolatile() && PrevSI->getOperand(1) == SI.getOperand(1)) { |
| 10293 | ++NumDeadStore; |
| 10294 | ++BBI; |
| 10295 | EraseInstFromFunction(*PrevSI); |
| 10296 | continue; |
| 10297 | } |
| 10298 | break; |
| 10299 | } |
| 10300 | |
Chris Lattner | b4db97f | 2006-05-26 19:19:20 +0000 | [diff] [blame] | 10301 | // If this is a load, we have to stop. However, if the loaded value is from |
| 10302 | // the pointer we're loading and is producing the pointer we're storing, |
| 10303 | // then *this* store is dead (X = load P; store X -> P). |
| 10304 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { |
Chris Lattner | a54c7eb | 2007-09-07 05:33:03 +0000 | [diff] [blame] | 10305 | if (LI == Val && LI->getOperand(0) == Ptr && !SI.isVolatile()) { |
Chris Lattner | b4db97f | 2006-05-26 19:19:20 +0000 | [diff] [blame] | 10306 | EraseInstFromFunction(SI); |
| 10307 | ++NumCombined; |
| 10308 | return 0; |
| 10309 | } |
| 10310 | // Otherwise, this is a load from some other location. Stores before it |
| 10311 | // may not be dead. |
| 10312 | break; |
| 10313 | } |
| 10314 | |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 10315 | // Don't skip over loads or things that can modify memory. |
Chris Lattner | b4db97f | 2006-05-26 19:19:20 +0000 | [diff] [blame] | 10316 | if (BBI->mayWriteToMemory()) |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 10317 | break; |
| 10318 | } |
| 10319 | |
| 10320 | |
| 10321 | if (SI.isVolatile()) return 0; // Don't hack volatile stores. |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10322 | |
| 10323 | // store X, null -> turns into 'unreachable' in SimplifyCFG |
| 10324 | if (isa<ConstantPointerNull>(Ptr)) { |
| 10325 | if (!isa<UndefValue>(Val)) { |
| 10326 | SI.setOperand(0, UndefValue::get(Val->getType())); |
| 10327 | if (Instruction *U = dyn_cast<Instruction>(Val)) |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10328 | AddToWorkList(U); // Dropped a use. |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10329 | ++NumCombined; |
| 10330 | } |
| 10331 | return 0; // Do not modify these! |
| 10332 | } |
| 10333 | |
| 10334 | // store undef, Ptr -> noop |
| 10335 | if (isa<UndefValue>(Val)) { |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 10336 | EraseInstFromFunction(SI); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10337 | ++NumCombined; |
| 10338 | return 0; |
| 10339 | } |
| 10340 | |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10341 | // If the pointer destination is a cast, see if we can fold the cast into the |
| 10342 | // source instead. |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 10343 | if (isa<CastInst>(Ptr)) |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10344 | if (Instruction *Res = InstCombineStoreToCast(*this, SI)) |
| 10345 | return Res; |
| 10346 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 10347 | if (CE->isCast()) |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10348 | if (Instruction *Res = InstCombineStoreToCast(*this, SI)) |
| 10349 | return Res; |
| 10350 | |
Chris Lattner | 408902b | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 10351 | |
| 10352 | // If this store is the last instruction in the basic block, and if the block |
| 10353 | // ends with an unconditional branch, try to move it to the successor block. |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 10354 | BBI = &SI; ++BBI; |
Chris Lattner | 408902b | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 10355 | if (BranchInst *BI = dyn_cast<BranchInst>(BBI)) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10356 | if (BI->isUnconditional()) |
| 10357 | if (SimplifyStoreAtEndOfBlock(SI)) |
| 10358 | return 0; // xform done! |
Chris Lattner | 408902b | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 10359 | |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10360 | return 0; |
| 10361 | } |
| 10362 | |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10363 | /// SimplifyStoreAtEndOfBlock - Turn things like: |
| 10364 | /// if () { *P = v1; } else { *P = v2 } |
| 10365 | /// into a phi node with a store in the successor. |
| 10366 | /// |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10367 | /// Simplify things like: |
| 10368 | /// *P = v1; if () { *P = v2; } |
| 10369 | /// into a phi node with a store in the successor. |
| 10370 | /// |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10371 | bool InstCombiner::SimplifyStoreAtEndOfBlock(StoreInst &SI) { |
| 10372 | BasicBlock *StoreBB = SI.getParent(); |
| 10373 | |
| 10374 | // Check to see if the successor block has exactly two incoming edges. If |
| 10375 | // so, see if the other predecessor contains a store to the same location. |
| 10376 | // 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] | 10377 | BasicBlock *DestBB = StoreBB->getTerminator()->getSuccessor(0); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10378 | |
| 10379 | // Determine whether Dest has exactly two predecessors and, if so, compute |
| 10380 | // the other predecessor. |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10381 | pred_iterator PI = pred_begin(DestBB); |
| 10382 | BasicBlock *OtherBB = 0; |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10383 | if (*PI != StoreBB) |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10384 | OtherBB = *PI; |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10385 | ++PI; |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10386 | if (PI == pred_end(DestBB)) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10387 | return false; |
| 10388 | |
| 10389 | if (*PI != StoreBB) { |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10390 | if (OtherBB) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10391 | return false; |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10392 | OtherBB = *PI; |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10393 | } |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10394 | if (++PI != pred_end(DestBB)) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10395 | return false; |
| 10396 | |
| 10397 | |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10398 | // Verify that the other block ends in a branch and is not otherwise empty. |
| 10399 | BasicBlock::iterator BBI = OtherBB->getTerminator(); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10400 | BranchInst *OtherBr = dyn_cast<BranchInst>(BBI); |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10401 | if (!OtherBr || BBI == OtherBB->begin()) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10402 | return false; |
| 10403 | |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10404 | // If the other block ends in an unconditional branch, check for the 'if then |
| 10405 | // else' case. there is an instruction before the branch. |
| 10406 | StoreInst *OtherStore = 0; |
| 10407 | if (OtherBr->isUnconditional()) { |
| 10408 | // If this isn't a store, or isn't a store to the same location, bail out. |
| 10409 | --BBI; |
| 10410 | OtherStore = dyn_cast<StoreInst>(BBI); |
| 10411 | if (!OtherStore || OtherStore->getOperand(1) != SI.getOperand(1)) |
| 10412 | return false; |
| 10413 | } else { |
Chris Lattner | d717c18 | 2007-05-05 22:32:24 +0000 | [diff] [blame] | 10414 | // 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] | 10415 | // destinations is StoreBB, then we have the if/then case. |
| 10416 | if (OtherBr->getSuccessor(0) != StoreBB && |
| 10417 | OtherBr->getSuccessor(1) != StoreBB) |
| 10418 | return false; |
| 10419 | |
| 10420 | // 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] | 10421 | // if/then triangle. See if there is a store to the same ptr as SI that |
| 10422 | // lives in OtherBB. |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10423 | for (;; --BBI) { |
| 10424 | // Check to see if we find the matching store. |
| 10425 | if ((OtherStore = dyn_cast<StoreInst>(BBI))) { |
| 10426 | if (OtherStore->getOperand(1) != SI.getOperand(1)) |
| 10427 | return false; |
| 10428 | break; |
| 10429 | } |
Chris Lattner | d717c18 | 2007-05-05 22:32:24 +0000 | [diff] [blame] | 10430 | // If we find something that may be using the stored value, or if we run |
| 10431 | // out of instructions, we can't do the xform. |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10432 | if (isa<LoadInst>(BBI) || BBI->mayWriteToMemory() || |
| 10433 | BBI == OtherBB->begin()) |
| 10434 | return false; |
| 10435 | } |
| 10436 | |
| 10437 | // In order to eliminate the store in OtherBr, we have to |
| 10438 | // make sure nothing reads the stored value in StoreBB. |
| 10439 | for (BasicBlock::iterator I = StoreBB->begin(); &*I != &SI; ++I) { |
| 10440 | // FIXME: This should really be AA driven. |
| 10441 | if (isa<LoadInst>(I) || I->mayWriteToMemory()) |
| 10442 | return false; |
| 10443 | } |
| 10444 | } |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10445 | |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10446 | // Insert a PHI node now if we need it. |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10447 | Value *MergedVal = OtherStore->getOperand(0); |
| 10448 | if (MergedVal != SI.getOperand(0)) { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 10449 | PHINode *PN = PHINode::Create(MergedVal->getType(), "storemerge"); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10450 | PN->reserveOperandSpace(2); |
| 10451 | PN->addIncoming(SI.getOperand(0), SI.getParent()); |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10452 | PN->addIncoming(OtherStore->getOperand(0), OtherBB); |
| 10453 | MergedVal = InsertNewInstBefore(PN, DestBB->front()); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10454 | } |
| 10455 | |
| 10456 | // Advance to a place where it is safe to insert the new store and |
| 10457 | // insert it. |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10458 | BBI = DestBB->begin(); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10459 | while (isa<PHINode>(BBI)) ++BBI; |
| 10460 | InsertNewInstBefore(new StoreInst(MergedVal, SI.getOperand(1), |
| 10461 | OtherStore->isVolatile()), *BBI); |
| 10462 | |
| 10463 | // Nuke the old stores. |
| 10464 | EraseInstFromFunction(SI); |
| 10465 | EraseInstFromFunction(*OtherStore); |
| 10466 | ++NumCombined; |
| 10467 | return true; |
| 10468 | } |
| 10469 | |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10470 | |
Chris Lattner | c4d10eb | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 10471 | Instruction *InstCombiner::visitBranchInst(BranchInst &BI) { |
| 10472 | // 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] | 10473 | Value *X = 0; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 10474 | BasicBlock *TrueDest; |
| 10475 | BasicBlock *FalseDest; |
| 10476 | if (match(&BI, m_Br(m_Not(m_Value(X)), TrueDest, FalseDest)) && |
| 10477 | !isa<Constant>(X)) { |
| 10478 | // Swap Destinations and condition... |
| 10479 | BI.setCondition(X); |
| 10480 | BI.setSuccessor(0, FalseDest); |
| 10481 | BI.setSuccessor(1, TrueDest); |
| 10482 | return &BI; |
| 10483 | } |
| 10484 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10485 | // Cannonicalize fcmp_one -> fcmp_oeq |
| 10486 | FCmpInst::Predicate FPred; Value *Y; |
| 10487 | if (match(&BI, m_Br(m_FCmp(FPred, m_Value(X), m_Value(Y)), |
| 10488 | TrueDest, FalseDest))) |
| 10489 | if ((FPred == FCmpInst::FCMP_ONE || FPred == FCmpInst::FCMP_OLE || |
| 10490 | FPred == FCmpInst::FCMP_OGE) && BI.getCondition()->hasOneUse()) { |
| 10491 | FCmpInst *I = cast<FCmpInst>(BI.getCondition()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10492 | FCmpInst::Predicate NewPred = FCmpInst::getInversePredicate(FPred); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 10493 | Instruction *NewSCC = new FCmpInst(NewPred, X, Y, "", I); |
| 10494 | NewSCC->takeName(I); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10495 | // Swap Destinations and condition... |
| 10496 | BI.setCondition(NewSCC); |
| 10497 | BI.setSuccessor(0, FalseDest); |
| 10498 | BI.setSuccessor(1, TrueDest); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10499 | RemoveFromWorkList(I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 10500 | I->eraseFromParent(); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10501 | AddToWorkList(NewSCC); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10502 | return &BI; |
| 10503 | } |
| 10504 | |
| 10505 | // Cannonicalize icmp_ne -> icmp_eq |
| 10506 | ICmpInst::Predicate IPred; |
| 10507 | if (match(&BI, m_Br(m_ICmp(IPred, m_Value(X), m_Value(Y)), |
| 10508 | TrueDest, FalseDest))) |
| 10509 | if ((IPred == ICmpInst::ICMP_NE || IPred == ICmpInst::ICMP_ULE || |
| 10510 | IPred == ICmpInst::ICMP_SLE || IPred == ICmpInst::ICMP_UGE || |
| 10511 | IPred == ICmpInst::ICMP_SGE) && BI.getCondition()->hasOneUse()) { |
| 10512 | ICmpInst *I = cast<ICmpInst>(BI.getCondition()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10513 | ICmpInst::Predicate NewPred = ICmpInst::getInversePredicate(IPred); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 10514 | Instruction *NewSCC = new ICmpInst(NewPred, X, Y, "", I); |
| 10515 | NewSCC->takeName(I); |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 10516 | // Swap Destinations and condition... |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 10517 | BI.setCondition(NewSCC); |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 10518 | BI.setSuccessor(0, FalseDest); |
| 10519 | BI.setSuccessor(1, TrueDest); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10520 | RemoveFromWorkList(I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 10521 | I->eraseFromParent();; |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10522 | AddToWorkList(NewSCC); |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 10523 | return &BI; |
| 10524 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 10525 | |
Chris Lattner | c4d10eb | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 10526 | return 0; |
| 10527 | } |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 10528 | |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 10529 | Instruction *InstCombiner::visitSwitchInst(SwitchInst &SI) { |
| 10530 | Value *Cond = SI.getCondition(); |
| 10531 | if (Instruction *I = dyn_cast<Instruction>(Cond)) { |
| 10532 | if (I->getOpcode() == Instruction::Add) |
| 10533 | if (ConstantInt *AddRHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 10534 | // change 'switch (X+4) case 1:' into 'switch (X) case -3' |
| 10535 | for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10536 | SI.setOperand(i,ConstantExpr::getSub(cast<Constant>(SI.getOperand(i)), |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 10537 | AddRHS)); |
| 10538 | SI.setOperand(0, I->getOperand(0)); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10539 | AddToWorkList(I); |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 10540 | return &SI; |
| 10541 | } |
| 10542 | } |
| 10543 | return 0; |
| 10544 | } |
| 10545 | |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10546 | /// CheapToScalarize - Return true if the value is cheaper to scalarize than it |
| 10547 | /// is to leave as a vector operation. |
| 10548 | static bool CheapToScalarize(Value *V, bool isConstant) { |
| 10549 | if (isa<ConstantAggregateZero>(V)) |
| 10550 | return true; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10551 | if (ConstantVector *C = dyn_cast<ConstantVector>(V)) { |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10552 | if (isConstant) return true; |
| 10553 | // If all elts are the same, we can extract. |
| 10554 | Constant *Op0 = C->getOperand(0); |
| 10555 | for (unsigned i = 1; i < C->getNumOperands(); ++i) |
| 10556 | if (C->getOperand(i) != Op0) |
| 10557 | return false; |
| 10558 | return true; |
| 10559 | } |
| 10560 | Instruction *I = dyn_cast<Instruction>(V); |
| 10561 | if (!I) return false; |
| 10562 | |
| 10563 | // Insert element gets simplified to the inserted element or is deleted if |
| 10564 | // this is constant idx extract element and its a constant idx insertelt. |
| 10565 | if (I->getOpcode() == Instruction::InsertElement && isConstant && |
| 10566 | isa<ConstantInt>(I->getOperand(2))) |
| 10567 | return true; |
| 10568 | if (I->getOpcode() == Instruction::Load && I->hasOneUse()) |
| 10569 | return true; |
| 10570 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) |
| 10571 | if (BO->hasOneUse() && |
| 10572 | (CheapToScalarize(BO->getOperand(0), isConstant) || |
| 10573 | CheapToScalarize(BO->getOperand(1), isConstant))) |
| 10574 | return true; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10575 | if (CmpInst *CI = dyn_cast<CmpInst>(I)) |
| 10576 | if (CI->hasOneUse() && |
| 10577 | (CheapToScalarize(CI->getOperand(0), isConstant) || |
| 10578 | CheapToScalarize(CI->getOperand(1), isConstant))) |
| 10579 | return true; |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10580 | |
| 10581 | return false; |
| 10582 | } |
| 10583 | |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 10584 | /// Read and decode a shufflevector mask. |
| 10585 | /// |
| 10586 | /// It turns undef elements into values that are larger than the number of |
| 10587 | /// elements in the input. |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10588 | static std::vector<unsigned> getShuffleMask(const ShuffleVectorInst *SVI) { |
| 10589 | unsigned NElts = SVI->getType()->getNumElements(); |
| 10590 | if (isa<ConstantAggregateZero>(SVI->getOperand(2))) |
| 10591 | return std::vector<unsigned>(NElts, 0); |
| 10592 | if (isa<UndefValue>(SVI->getOperand(2))) |
| 10593 | return std::vector<unsigned>(NElts, 2*NElts); |
| 10594 | |
| 10595 | std::vector<unsigned> Result; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10596 | const ConstantVector *CP = cast<ConstantVector>(SVI->getOperand(2)); |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10597 | for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) |
| 10598 | if (isa<UndefValue>(CP->getOperand(i))) |
| 10599 | Result.push_back(NElts*2); // undef -> 8 |
| 10600 | else |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10601 | Result.push_back(cast<ConstantInt>(CP->getOperand(i))->getZExtValue()); |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10602 | return Result; |
| 10603 | } |
| 10604 | |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10605 | /// FindScalarElement - Given a vector and an element number, see if the scalar |
| 10606 | /// value is already around as a register, for example if it were inserted then |
| 10607 | /// extracted from the vector. |
| 10608 | static Value *FindScalarElement(Value *V, unsigned EltNo) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10609 | assert(isa<VectorType>(V->getType()) && "Not looking at a vector?"); |
| 10610 | const VectorType *PTy = cast<VectorType>(V->getType()); |
Chris Lattner | 389a6f5 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 10611 | unsigned Width = PTy->getNumElements(); |
| 10612 | if (EltNo >= Width) // Out of range access. |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10613 | return UndefValue::get(PTy->getElementType()); |
| 10614 | |
| 10615 | if (isa<UndefValue>(V)) |
| 10616 | return UndefValue::get(PTy->getElementType()); |
| 10617 | else if (isa<ConstantAggregateZero>(V)) |
| 10618 | return Constant::getNullValue(PTy->getElementType()); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10619 | else if (ConstantVector *CP = dyn_cast<ConstantVector>(V)) |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10620 | return CP->getOperand(EltNo); |
| 10621 | else if (InsertElementInst *III = dyn_cast<InsertElementInst>(V)) { |
| 10622 | // 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] | 10623 | if (!isa<ConstantInt>(III->getOperand(2))) |
| 10624 | return 0; |
| 10625 | unsigned IIElt = cast<ConstantInt>(III->getOperand(2))->getZExtValue(); |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10626 | |
| 10627 | // If this is an insert to the element we are looking for, return the |
| 10628 | // inserted value. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10629 | if (EltNo == IIElt) |
| 10630 | return III->getOperand(1); |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10631 | |
| 10632 | // Otherwise, the insertelement doesn't modify the value, recurse on its |
| 10633 | // vector input. |
| 10634 | return FindScalarElement(III->getOperand(0), EltNo); |
Chris Lattner | 389a6f5 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 10635 | } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(V)) { |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10636 | unsigned InEl = getShuffleMask(SVI)[EltNo]; |
| 10637 | if (InEl < Width) |
| 10638 | return FindScalarElement(SVI->getOperand(0), InEl); |
| 10639 | else if (InEl < Width*2) |
| 10640 | return FindScalarElement(SVI->getOperand(1), InEl - Width); |
| 10641 | else |
| 10642 | return UndefValue::get(PTy->getElementType()); |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10643 | } |
| 10644 | |
| 10645 | // Otherwise, we don't know. |
| 10646 | return 0; |
| 10647 | } |
| 10648 | |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10649 | Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) { |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10650 | |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 10651 | // If vector val is undef, replace extract with scalar undef. |
Chris Lattner | 1f13c88 | 2006-03-31 18:25:14 +0000 | [diff] [blame] | 10652 | if (isa<UndefValue>(EI.getOperand(0))) |
| 10653 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); |
| 10654 | |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 10655 | // If vector val is constant 0, replace extract with scalar 0. |
Chris Lattner | 1f13c88 | 2006-03-31 18:25:14 +0000 | [diff] [blame] | 10656 | if (isa<ConstantAggregateZero>(EI.getOperand(0))) |
| 10657 | return ReplaceInstUsesWith(EI, Constant::getNullValue(EI.getType())); |
| 10658 | |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10659 | if (ConstantVector *C = dyn_cast<ConstantVector>(EI.getOperand(0))) { |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 10660 | // If vector val is constant with uniform operands, replace EI |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10661 | // with that operand |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10662 | Constant *op0 = C->getOperand(0); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10663 | for (unsigned i = 1; i < C->getNumOperands(); ++i) |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10664 | if (C->getOperand(i) != op0) { |
| 10665 | op0 = 0; |
| 10666 | break; |
| 10667 | } |
| 10668 | if (op0) |
| 10669 | return ReplaceInstUsesWith(EI, op0); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10670 | } |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10671 | |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10672 | // If extracting a specified index from the vector, see if we can recursively |
| 10673 | // find a previously computed scalar that was inserted into the vector. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10674 | if (ConstantInt *IdxC = dyn_cast<ConstantInt>(EI.getOperand(1))) { |
Chris Lattner | 8546409 | 2007-04-09 01:37:55 +0000 | [diff] [blame] | 10675 | unsigned IndexVal = IdxC->getZExtValue(); |
| 10676 | unsigned VectorWidth = |
| 10677 | cast<VectorType>(EI.getOperand(0)->getType())->getNumElements(); |
| 10678 | |
| 10679 | // If this is extracting an invalid index, turn this into undef, to avoid |
| 10680 | // crashing the code below. |
| 10681 | if (IndexVal >= VectorWidth) |
| 10682 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); |
| 10683 | |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 10684 | // This instruction only demands the single element from the input vector. |
| 10685 | // If the input vector has a single use, simplify it based on this use |
| 10686 | // property. |
Chris Lattner | 8546409 | 2007-04-09 01:37:55 +0000 | [diff] [blame] | 10687 | if (EI.getOperand(0)->hasOneUse() && VectorWidth != 1) { |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 10688 | uint64_t UndefElts; |
| 10689 | if (Value *V = SimplifyDemandedVectorElts(EI.getOperand(0), |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10690 | 1 << IndexVal, |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 10691 | UndefElts)) { |
| 10692 | EI.setOperand(0, V); |
| 10693 | return &EI; |
| 10694 | } |
| 10695 | } |
| 10696 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10697 | if (Value *Elt = FindScalarElement(EI.getOperand(0), IndexVal)) |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10698 | return ReplaceInstUsesWith(EI, Elt); |
Chris Lattner | b7300fa | 2007-04-14 23:02:14 +0000 | [diff] [blame] | 10699 | |
| 10700 | // If the this extractelement is directly using a bitcast from a vector of |
| 10701 | // the same number of elements, see if we can find the source element from |
| 10702 | // it. In this case, we will end up needing to bitcast the scalars. |
| 10703 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(EI.getOperand(0))) { |
| 10704 | if (const VectorType *VT = |
| 10705 | dyn_cast<VectorType>(BCI->getOperand(0)->getType())) |
| 10706 | if (VT->getNumElements() == VectorWidth) |
| 10707 | if (Value *Elt = FindScalarElement(BCI->getOperand(0), IndexVal)) |
| 10708 | return new BitCastInst(Elt, EI.getType()); |
| 10709 | } |
Chris Lattner | 389a6f5 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 10710 | } |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10711 | |
Chris Lattner | 73fa49d | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 10712 | if (Instruction *I = dyn_cast<Instruction>(EI.getOperand(0))) { |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10713 | if (I->hasOneUse()) { |
| 10714 | // Push extractelement into predecessor operation if legal and |
| 10715 | // profitable to do so |
| 10716 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) { |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10717 | bool isConstantElt = isa<ConstantInt>(EI.getOperand(1)); |
| 10718 | if (CheapToScalarize(BO, isConstantElt)) { |
| 10719 | ExtractElementInst *newEI0 = |
| 10720 | new ExtractElementInst(BO->getOperand(0), EI.getOperand(1), |
| 10721 | EI.getName()+".lhs"); |
| 10722 | ExtractElementInst *newEI1 = |
| 10723 | new ExtractElementInst(BO->getOperand(1), EI.getOperand(1), |
| 10724 | EI.getName()+".rhs"); |
| 10725 | InsertNewInstBefore(newEI0, EI); |
| 10726 | InsertNewInstBefore(newEI1, EI); |
| 10727 | return BinaryOperator::create(BO->getOpcode(), newEI0, newEI1); |
| 10728 | } |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 10729 | } else if (isa<LoadInst>(I)) { |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 10730 | unsigned AS = |
| 10731 | cast<PointerType>(I->getOperand(0)->getType())->getAddressSpace(); |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 10732 | Value *Ptr = InsertBitCastBefore(I->getOperand(0), |
| 10733 | PointerType::get(EI.getType(), AS),EI); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10734 | GetElementPtrInst *GEP = |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 10735 | GetElementPtrInst::Create(Ptr, EI.getOperand(1), I->getName() + ".gep"); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10736 | InsertNewInstBefore(GEP, EI); |
| 10737 | return new LoadInst(GEP); |
Chris Lattner | 73fa49d | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 10738 | } |
| 10739 | } |
| 10740 | if (InsertElementInst *IE = dyn_cast<InsertElementInst>(I)) { |
| 10741 | // Extracting the inserted element? |
| 10742 | if (IE->getOperand(2) == EI.getOperand(1)) |
| 10743 | return ReplaceInstUsesWith(EI, IE->getOperand(1)); |
| 10744 | // If the inserted and extracted elements are constants, they must not |
| 10745 | // be the same value, extract from the pre-inserted value instead. |
| 10746 | if (isa<Constant>(IE->getOperand(2)) && |
| 10747 | isa<Constant>(EI.getOperand(1))) { |
| 10748 | AddUsesToWorkList(EI); |
| 10749 | EI.setOperand(0, IE->getOperand(0)); |
| 10750 | return &EI; |
| 10751 | } |
| 10752 | } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(I)) { |
| 10753 | // If this is extracting an element from a shufflevector, figure out where |
| 10754 | // it came from and extract from the appropriate input element instead. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10755 | if (ConstantInt *Elt = dyn_cast<ConstantInt>(EI.getOperand(1))) { |
| 10756 | unsigned SrcIdx = getShuffleMask(SVI)[Elt->getZExtValue()]; |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10757 | Value *Src; |
| 10758 | if (SrcIdx < SVI->getType()->getNumElements()) |
| 10759 | Src = SVI->getOperand(0); |
| 10760 | else if (SrcIdx < SVI->getType()->getNumElements()*2) { |
| 10761 | SrcIdx -= SVI->getType()->getNumElements(); |
| 10762 | Src = SVI->getOperand(1); |
| 10763 | } else { |
| 10764 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); |
Chris Lattner | df084ff | 2006-03-30 22:02:40 +0000 | [diff] [blame] | 10765 | } |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 10766 | return new ExtractElementInst(Src, SrcIdx); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10767 | } |
| 10768 | } |
Chris Lattner | 73fa49d | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 10769 | } |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10770 | return 0; |
| 10771 | } |
| 10772 | |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10773 | /// CollectSingleShuffleElements - If V is a shuffle of values that ONLY returns |
| 10774 | /// elements from either LHS or RHS, return the shuffle mask and true. |
| 10775 | /// Otherwise, return false. |
| 10776 | static bool CollectSingleShuffleElements(Value *V, Value *LHS, Value *RHS, |
| 10777 | std::vector<Constant*> &Mask) { |
| 10778 | assert(V->getType() == LHS->getType() && V->getType() == RHS->getType() && |
| 10779 | "Invalid CollectSingleShuffleElements"); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10780 | unsigned NumElts = cast<VectorType>(V->getType())->getNumElements(); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10781 | |
| 10782 | if (isa<UndefValue>(V)) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10783 | Mask.assign(NumElts, UndefValue::get(Type::Int32Ty)); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10784 | return true; |
| 10785 | } else if (V == LHS) { |
| 10786 | for (unsigned i = 0; i != NumElts; ++i) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10787 | Mask.push_back(ConstantInt::get(Type::Int32Ty, i)); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10788 | return true; |
| 10789 | } else if (V == RHS) { |
| 10790 | for (unsigned i = 0; i != NumElts; ++i) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10791 | Mask.push_back(ConstantInt::get(Type::Int32Ty, i+NumElts)); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10792 | return true; |
| 10793 | } else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) { |
| 10794 | // If this is an insert of an extract from some other vector, include it. |
| 10795 | Value *VecOp = IEI->getOperand(0); |
| 10796 | Value *ScalarOp = IEI->getOperand(1); |
| 10797 | Value *IdxOp = IEI->getOperand(2); |
| 10798 | |
Chris Lattner | d929f06 | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 10799 | if (!isa<ConstantInt>(IdxOp)) |
| 10800 | return false; |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10801 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Chris Lattner | d929f06 | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 10802 | |
| 10803 | if (isa<UndefValue>(ScalarOp)) { // inserting undef into vector. |
| 10804 | // Okay, we can handle this if the vector we are insertinting into is |
| 10805 | // transitively ok. |
| 10806 | if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) { |
| 10807 | // If so, update the mask to reflect the inserted undef. |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10808 | Mask[InsertedIdx] = UndefValue::get(Type::Int32Ty); |
Chris Lattner | d929f06 | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 10809 | return true; |
| 10810 | } |
| 10811 | } else if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)){ |
| 10812 | if (isa<ConstantInt>(EI->getOperand(1)) && |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10813 | EI->getOperand(0)->getType() == V->getType()) { |
| 10814 | unsigned ExtractedIdx = |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10815 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10816 | |
| 10817 | // This must be extracting from either LHS or RHS. |
| 10818 | if (EI->getOperand(0) == LHS || EI->getOperand(0) == RHS) { |
| 10819 | // Okay, we can handle this if the vector we are insertinting into is |
| 10820 | // transitively ok. |
| 10821 | if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) { |
| 10822 | // If so, update the mask to reflect the inserted value. |
| 10823 | if (EI->getOperand(0) == LHS) { |
| 10824 | Mask[InsertedIdx & (NumElts-1)] = |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10825 | ConstantInt::get(Type::Int32Ty, ExtractedIdx); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10826 | } else { |
| 10827 | assert(EI->getOperand(0) == RHS); |
| 10828 | Mask[InsertedIdx & (NumElts-1)] = |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10829 | ConstantInt::get(Type::Int32Ty, ExtractedIdx+NumElts); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10830 | |
| 10831 | } |
| 10832 | return true; |
| 10833 | } |
| 10834 | } |
| 10835 | } |
| 10836 | } |
| 10837 | } |
| 10838 | // TODO: Handle shufflevector here! |
| 10839 | |
| 10840 | return false; |
| 10841 | } |
| 10842 | |
| 10843 | /// CollectShuffleElements - We are building a shuffle of V, using RHS as the |
| 10844 | /// RHS of the shuffle instruction, if it is not null. Return a shuffle mask |
| 10845 | /// that computes V and the LHS value of the shuffle. |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10846 | static Value *CollectShuffleElements(Value *V, std::vector<Constant*> &Mask, |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10847 | Value *&RHS) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10848 | assert(isa<VectorType>(V->getType()) && |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10849 | (RHS == 0 || V->getType() == RHS->getType()) && |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10850 | "Invalid shuffle!"); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10851 | unsigned NumElts = cast<VectorType>(V->getType())->getNumElements(); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10852 | |
| 10853 | if (isa<UndefValue>(V)) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10854 | Mask.assign(NumElts, UndefValue::get(Type::Int32Ty)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10855 | return V; |
| 10856 | } else if (isa<ConstantAggregateZero>(V)) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10857 | Mask.assign(NumElts, ConstantInt::get(Type::Int32Ty, 0)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10858 | return V; |
| 10859 | } else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) { |
| 10860 | // If this is an insert of an extract from some other vector, include it. |
| 10861 | Value *VecOp = IEI->getOperand(0); |
| 10862 | Value *ScalarOp = IEI->getOperand(1); |
| 10863 | Value *IdxOp = IEI->getOperand(2); |
| 10864 | |
| 10865 | if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) { |
| 10866 | if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) && |
| 10867 | EI->getOperand(0)->getType() == V->getType()) { |
| 10868 | unsigned ExtractedIdx = |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10869 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
| 10870 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10871 | |
| 10872 | // Either the extracted from or inserted into vector must be RHSVec, |
| 10873 | // otherwise we'd end up with a shuffle of three inputs. |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10874 | if (EI->getOperand(0) == RHS || RHS == 0) { |
| 10875 | RHS = EI->getOperand(0); |
| 10876 | Value *V = CollectShuffleElements(VecOp, Mask, RHS); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10877 | Mask[InsertedIdx & (NumElts-1)] = |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10878 | ConstantInt::get(Type::Int32Ty, NumElts+ExtractedIdx); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10879 | return V; |
| 10880 | } |
| 10881 | |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10882 | if (VecOp == RHS) { |
| 10883 | Value *V = CollectShuffleElements(EI->getOperand(0), Mask, RHS); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10884 | // Everything but the extracted element is replaced with the RHS. |
| 10885 | for (unsigned i = 0; i != NumElts; ++i) { |
| 10886 | if (i != InsertedIdx) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10887 | Mask[i] = ConstantInt::get(Type::Int32Ty, NumElts+i); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10888 | } |
| 10889 | return V; |
| 10890 | } |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10891 | |
| 10892 | // If this insertelement is a chain that comes from exactly these two |
| 10893 | // vectors, return the vector and the effective shuffle. |
| 10894 | if (CollectSingleShuffleElements(IEI, EI->getOperand(0), RHS, Mask)) |
| 10895 | return EI->getOperand(0); |
| 10896 | |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10897 | } |
| 10898 | } |
| 10899 | } |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10900 | // TODO: Handle shufflevector here! |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10901 | |
| 10902 | // Otherwise, can't do anything fancy. Return an identity vector. |
| 10903 | for (unsigned i = 0; i != NumElts; ++i) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10904 | Mask.push_back(ConstantInt::get(Type::Int32Ty, i)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10905 | return V; |
| 10906 | } |
| 10907 | |
| 10908 | Instruction *InstCombiner::visitInsertElementInst(InsertElementInst &IE) { |
| 10909 | Value *VecOp = IE.getOperand(0); |
| 10910 | Value *ScalarOp = IE.getOperand(1); |
| 10911 | Value *IdxOp = IE.getOperand(2); |
| 10912 | |
Chris Lattner | 599ded1 | 2007-04-09 01:11:16 +0000 | [diff] [blame] | 10913 | // Inserting an undef or into an undefined place, remove this. |
| 10914 | if (isa<UndefValue>(ScalarOp) || isa<UndefValue>(IdxOp)) |
| 10915 | ReplaceInstUsesWith(IE, VecOp); |
| 10916 | |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10917 | // If the inserted element was extracted from some other vector, and if the |
| 10918 | // indexes are constant, try to turn this into a shufflevector operation. |
| 10919 | if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) { |
| 10920 | if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) && |
| 10921 | EI->getOperand(0)->getType() == IE.getType()) { |
| 10922 | unsigned NumVectorElts = IE.getType()->getNumElements(); |
Chris Lattner | e34e9a2 | 2007-04-14 23:32:02 +0000 | [diff] [blame] | 10923 | unsigned ExtractedIdx = |
| 10924 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10925 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10926 | |
| 10927 | if (ExtractedIdx >= NumVectorElts) // Out of range extract. |
| 10928 | return ReplaceInstUsesWith(IE, VecOp); |
| 10929 | |
| 10930 | if (InsertedIdx >= NumVectorElts) // Out of range insert. |
| 10931 | return ReplaceInstUsesWith(IE, UndefValue::get(IE.getType())); |
| 10932 | |
| 10933 | // If we are extracting a value from a vector, then inserting it right |
| 10934 | // back into the same place, just use the input vector. |
| 10935 | if (EI->getOperand(0) == VecOp && ExtractedIdx == InsertedIdx) |
| 10936 | return ReplaceInstUsesWith(IE, VecOp); |
| 10937 | |
| 10938 | // We could theoretically do this for ANY input. However, doing so could |
| 10939 | // turn chains of insertelement instructions into a chain of shufflevector |
| 10940 | // instructions, and right now we do not merge shufflevectors. As such, |
| 10941 | // only do this in a situation where it is clear that there is benefit. |
| 10942 | if (isa<UndefValue>(VecOp) || isa<ConstantAggregateZero>(VecOp)) { |
| 10943 | // Turn this into shuffle(EIOp0, VecOp, Mask). The result has all of |
| 10944 | // the values of VecOp, except then one read from EIOp0. |
| 10945 | // Build a new shuffle mask. |
| 10946 | std::vector<Constant*> Mask; |
| 10947 | if (isa<UndefValue>(VecOp)) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10948 | Mask.assign(NumVectorElts, UndefValue::get(Type::Int32Ty)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10949 | else { |
| 10950 | assert(isa<ConstantAggregateZero>(VecOp) && "Unknown thing"); |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10951 | Mask.assign(NumVectorElts, ConstantInt::get(Type::Int32Ty, |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10952 | NumVectorElts)); |
| 10953 | } |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10954 | Mask[InsertedIdx] = ConstantInt::get(Type::Int32Ty, ExtractedIdx); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10955 | return new ShuffleVectorInst(EI->getOperand(0), VecOp, |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10956 | ConstantVector::get(Mask)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10957 | } |
| 10958 | |
| 10959 | // If this insertelement isn't used by some other insertelement, turn it |
| 10960 | // (and any insertelements it points to), into one big shuffle. |
| 10961 | if (!IE.hasOneUse() || !isa<InsertElementInst>(IE.use_back())) { |
| 10962 | std::vector<Constant*> Mask; |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10963 | Value *RHS = 0; |
| 10964 | Value *LHS = CollectShuffleElements(&IE, Mask, RHS); |
| 10965 | if (RHS == 0) RHS = UndefValue::get(LHS->getType()); |
| 10966 | // We now have a shuffle of LHS, RHS, Mask. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10967 | return new ShuffleVectorInst(LHS, RHS, ConstantVector::get(Mask)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10968 | } |
| 10969 | } |
| 10970 | } |
| 10971 | |
| 10972 | return 0; |
| 10973 | } |
| 10974 | |
| 10975 | |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 10976 | Instruction *InstCombiner::visitShuffleVectorInst(ShuffleVectorInst &SVI) { |
| 10977 | Value *LHS = SVI.getOperand(0); |
| 10978 | Value *RHS = SVI.getOperand(1); |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10979 | std::vector<unsigned> Mask = getShuffleMask(&SVI); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 10980 | |
| 10981 | bool MadeChange = false; |
| 10982 | |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 10983 | // Undefined shuffle mask -> undefined value. |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10984 | if (isa<UndefValue>(SVI.getOperand(2))) |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 10985 | return ReplaceInstUsesWith(SVI, UndefValue::get(SVI.getType())); |
| 10986 | |
Chris Lattner | e4929dd | 2007-01-05 07:36:08 +0000 | [diff] [blame] | 10987 | // If we have shuffle(x, undef, mask) and any elements of mask refer to |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10988 | // the undef, change them to undefs. |
Chris Lattner | e4929dd | 2007-01-05 07:36:08 +0000 | [diff] [blame] | 10989 | if (isa<UndefValue>(SVI.getOperand(1))) { |
| 10990 | // Scan to see if there are any references to the RHS. If so, replace them |
| 10991 | // with undef element refs and set MadeChange to true. |
| 10992 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
| 10993 | if (Mask[i] >= e && Mask[i] != 2*e) { |
| 10994 | Mask[i] = 2*e; |
| 10995 | MadeChange = true; |
| 10996 | } |
| 10997 | } |
| 10998 | |
| 10999 | if (MadeChange) { |
| 11000 | // Remap any references to RHS to use LHS. |
| 11001 | std::vector<Constant*> Elts; |
| 11002 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
| 11003 | if (Mask[i] == 2*e) |
| 11004 | Elts.push_back(UndefValue::get(Type::Int32Ty)); |
| 11005 | else |
| 11006 | Elts.push_back(ConstantInt::get(Type::Int32Ty, Mask[i])); |
| 11007 | } |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 11008 | SVI.setOperand(2, ConstantVector::get(Elts)); |
Chris Lattner | e4929dd | 2007-01-05 07:36:08 +0000 | [diff] [blame] | 11009 | } |
| 11010 | } |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11011 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 11012 | // Canonicalize shuffle(x ,x,mask) -> shuffle(x, undef,mask') |
| 11013 | // Canonicalize shuffle(undef,x,mask) -> shuffle(x, undef,mask'). |
| 11014 | if (LHS == RHS || isa<UndefValue>(LHS)) { |
| 11015 | if (isa<UndefValue>(LHS) && LHS == RHS) { |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 11016 | // shuffle(undef,undef,mask) -> undef. |
| 11017 | return ReplaceInstUsesWith(SVI, LHS); |
| 11018 | } |
| 11019 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 11020 | // Remap any references to RHS to use LHS. |
| 11021 | std::vector<Constant*> Elts; |
| 11022 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 11023 | if (Mask[i] >= 2*e) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 11024 | Elts.push_back(UndefValue::get(Type::Int32Ty)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 11025 | else { |
| 11026 | if ((Mask[i] >= e && isa<UndefValue>(RHS)) || |
| 11027 | (Mask[i] < e && isa<UndefValue>(LHS))) |
| 11028 | Mask[i] = 2*e; // Turn into undef. |
| 11029 | else |
| 11030 | Mask[i] &= (e-1); // Force to LHS. |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 11031 | Elts.push_back(ConstantInt::get(Type::Int32Ty, Mask[i])); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 11032 | } |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 11033 | } |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 11034 | SVI.setOperand(0, SVI.getOperand(1)); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 11035 | SVI.setOperand(1, UndefValue::get(RHS->getType())); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 11036 | SVI.setOperand(2, ConstantVector::get(Elts)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 11037 | LHS = SVI.getOperand(0); |
| 11038 | RHS = SVI.getOperand(1); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 11039 | MadeChange = true; |
| 11040 | } |
| 11041 | |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 11042 | // Analyze the shuffle, are the LHS or RHS and identity shuffles? |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 11043 | bool isLHSID = true, isRHSID = true; |
Chris Lattner | 706126d | 2006-04-16 00:03:56 +0000 | [diff] [blame] | 11044 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 11045 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
| 11046 | if (Mask[i] >= e*2) continue; // Ignore undef values. |
| 11047 | // Is this an identity shuffle of the LHS value? |
| 11048 | isLHSID &= (Mask[i] == i); |
| 11049 | |
| 11050 | // Is this an identity shuffle of the RHS value? |
| 11051 | isRHSID &= (Mask[i]-e == i); |
Chris Lattner | 706126d | 2006-04-16 00:03:56 +0000 | [diff] [blame] | 11052 | } |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 11053 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 11054 | // Eliminate identity shuffles. |
| 11055 | if (isLHSID) return ReplaceInstUsesWith(SVI, LHS); |
| 11056 | if (isRHSID) return ReplaceInstUsesWith(SVI, RHS); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 11057 | |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 11058 | // If the LHS is a shufflevector itself, see if we can combine it with this |
| 11059 | // one without producing an unusual shuffle. Here we are really conservative: |
| 11060 | // we are absolutely afraid of producing a shuffle mask not in the input |
| 11061 | // program, because the code gen may not be smart enough to turn a merged |
| 11062 | // shuffle into two specific shuffles: it may produce worse code. As such, |
| 11063 | // we only merge two shuffles if the result is one of the two input shuffle |
| 11064 | // masks. In this case, merging the shuffles just removes one instruction, |
| 11065 | // which we know is safe. This is good for things like turning: |
| 11066 | // (splat(splat)) -> splat. |
| 11067 | if (ShuffleVectorInst *LHSSVI = dyn_cast<ShuffleVectorInst>(LHS)) { |
| 11068 | if (isa<UndefValue>(RHS)) { |
| 11069 | std::vector<unsigned> LHSMask = getShuffleMask(LHSSVI); |
| 11070 | |
| 11071 | std::vector<unsigned> NewMask; |
| 11072 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) |
| 11073 | if (Mask[i] >= 2*e) |
| 11074 | NewMask.push_back(2*e); |
| 11075 | else |
| 11076 | NewMask.push_back(LHSMask[Mask[i]]); |
| 11077 | |
| 11078 | // If the result mask is equal to the src shuffle or this shuffle mask, do |
| 11079 | // the replacement. |
| 11080 | if (NewMask == LHSMask || NewMask == Mask) { |
| 11081 | std::vector<Constant*> Elts; |
| 11082 | for (unsigned i = 0, e = NewMask.size(); i != e; ++i) { |
| 11083 | if (NewMask[i] >= e*2) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 11084 | Elts.push_back(UndefValue::get(Type::Int32Ty)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 11085 | } else { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 11086 | Elts.push_back(ConstantInt::get(Type::Int32Ty, NewMask[i])); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 11087 | } |
| 11088 | } |
| 11089 | return new ShuffleVectorInst(LHSSVI->getOperand(0), |
| 11090 | LHSSVI->getOperand(1), |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 11091 | ConstantVector::get(Elts)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 11092 | } |
| 11093 | } |
| 11094 | } |
Chris Lattner | c5eff44 | 2007-01-30 22:32:46 +0000 | [diff] [blame] | 11095 | |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 11096 | return MadeChange ? &SVI : 0; |
| 11097 | } |
| 11098 | |
| 11099 | |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 11100 | |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 11101 | |
| 11102 | /// TryToSinkInstruction - Try to move the specified instruction from its |
| 11103 | /// current block into the beginning of DestBlock, which can only happen if it's |
| 11104 | /// safe to move the instruction past all of the instructions between it and the |
| 11105 | /// end of its block. |
| 11106 | static bool TryToSinkInstruction(Instruction *I, BasicBlock *DestBlock) { |
| 11107 | assert(I->hasOneUse() && "Invariants didn't hold!"); |
| 11108 | |
Chris Lattner | 108e902 | 2005-10-27 17:13:11 +0000 | [diff] [blame] | 11109 | // Cannot move control-flow-involving, volatile loads, vaarg, etc. |
| 11110 | if (isa<PHINode>(I) || I->mayWriteToMemory()) return false; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 11111 | |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 11112 | // Do not sink alloca instructions out of the entry block. |
Dan Gohman | ecb7a77 | 2007-03-22 16:38:57 +0000 | [diff] [blame] | 11113 | if (isa<AllocaInst>(I) && I->getParent() == |
| 11114 | &DestBlock->getParent()->getEntryBlock()) |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 11115 | return false; |
| 11116 | |
Chris Lattner | 96a52a6 | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 11117 | // We can only sink load instructions if there is nothing between the load and |
| 11118 | // the end of block that could change the value. |
| 11119 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Chris Lattner | 96a52a6 | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 11120 | for (BasicBlock::iterator Scan = LI, E = LI->getParent()->end(); |
| 11121 | Scan != E; ++Scan) |
| 11122 | if (Scan->mayWriteToMemory()) |
| 11123 | return false; |
Chris Lattner | 96a52a6 | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 11124 | } |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 11125 | |
| 11126 | BasicBlock::iterator InsertPos = DestBlock->begin(); |
| 11127 | while (isa<PHINode>(InsertPos)) ++InsertPos; |
| 11128 | |
Chris Lattner | 4bc5f80 | 2005-08-08 19:11:57 +0000 | [diff] [blame] | 11129 | I->moveBefore(InsertPos); |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 11130 | ++NumSunkInst; |
| 11131 | return true; |
| 11132 | } |
| 11133 | |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 11134 | |
| 11135 | /// AddReachableCodeToWorklist - Walk the function in depth-first order, adding |
| 11136 | /// all reachable code to the worklist. |
| 11137 | /// |
| 11138 | /// This has a couple of tricks to make the code faster and more powerful. In |
| 11139 | /// particular, we constant fold and DCE instructions as we go, to avoid adding |
| 11140 | /// them to the worklist (this significantly speeds up instcombine on code where |
| 11141 | /// many instructions are dead or constant). Additionally, if we find a branch |
| 11142 | /// whose condition is a known constant, we only visit the reachable successors. |
| 11143 | /// |
| 11144 | static void AddReachableCodeToWorklist(BasicBlock *BB, |
Chris Lattner | 1f87a58 | 2007-02-15 19:41:52 +0000 | [diff] [blame] | 11145 | SmallPtrSet<BasicBlock*, 64> &Visited, |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11146 | InstCombiner &IC, |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 11147 | const TargetData *TD) { |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 11148 | std::vector<BasicBlock*> Worklist; |
| 11149 | Worklist.push_back(BB); |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 11150 | |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 11151 | while (!Worklist.empty()) { |
| 11152 | BB = Worklist.back(); |
| 11153 | Worklist.pop_back(); |
| 11154 | |
| 11155 | // We have now visited this block! If we've already been here, ignore it. |
| 11156 | if (!Visited.insert(BB)) continue; |
| 11157 | |
| 11158 | for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ) { |
| 11159 | Instruction *Inst = BBI++; |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 11160 | |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 11161 | // DCE instruction if trivially dead. |
| 11162 | if (isInstructionTriviallyDead(Inst)) { |
| 11163 | ++NumDeadInst; |
| 11164 | DOUT << "IC: DCE: " << *Inst; |
| 11165 | Inst->eraseFromParent(); |
| 11166 | continue; |
| 11167 | } |
| 11168 | |
| 11169 | // ConstantProp instruction if trivially constant. |
| 11170 | if (Constant *C = ConstantFoldInstruction(Inst, TD)) { |
| 11171 | DOUT << "IC: ConstFold to: " << *C << " from: " << *Inst; |
| 11172 | Inst->replaceAllUsesWith(C); |
| 11173 | ++NumConstProp; |
| 11174 | Inst->eraseFromParent(); |
| 11175 | continue; |
| 11176 | } |
Chris Lattner | 3ccc6bc | 2007-07-20 22:06:41 +0000 | [diff] [blame] | 11177 | |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 11178 | IC.AddToWorkList(Inst); |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 11179 | } |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 11180 | |
| 11181 | // Recursively visit successors. If this is a branch or switch on a |
| 11182 | // constant, only visit the reachable successor. |
Nick Lewycky | 9143699 | 2008-03-09 08:50:23 +0000 | [diff] [blame] | 11183 | if (BB->getUnwindDest()) |
| 11184 | Worklist.push_back(BB->getUnwindDest()); |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 11185 | TerminatorInst *TI = BB->getTerminator(); |
| 11186 | if (BranchInst *BI = dyn_cast<BranchInst>(TI)) { |
| 11187 | if (BI->isConditional() && isa<ConstantInt>(BI->getCondition())) { |
| 11188 | bool CondVal = cast<ConstantInt>(BI->getCondition())->getZExtValue(); |
Nick Lewycky | 9143699 | 2008-03-09 08:50:23 +0000 | [diff] [blame] | 11189 | BasicBlock *ReachableBB = BI->getSuccessor(!CondVal); |
| 11190 | if (ReachableBB != BB->getUnwindDest()) |
| 11191 | Worklist.push_back(ReachableBB); |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 11192 | continue; |
| 11193 | } |
| 11194 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) { |
| 11195 | if (ConstantInt *Cond = dyn_cast<ConstantInt>(SI->getCondition())) { |
| 11196 | // See if this is an explicit destination. |
| 11197 | for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i) |
| 11198 | if (SI->getCaseValue(i) == Cond) { |
Nick Lewycky | 9143699 | 2008-03-09 08:50:23 +0000 | [diff] [blame] | 11199 | BasicBlock *ReachableBB = SI->getSuccessor(i); |
| 11200 | if (ReachableBB != BB->getUnwindDest()) |
| 11201 | Worklist.push_back(ReachableBB); |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 11202 | continue; |
| 11203 | } |
| 11204 | |
| 11205 | // Otherwise it is the default destination. |
| 11206 | Worklist.push_back(SI->getSuccessor(0)); |
| 11207 | continue; |
| 11208 | } |
| 11209 | } |
| 11210 | |
| 11211 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) |
| 11212 | Worklist.push_back(TI->getSuccessor(i)); |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 11213 | } |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 11214 | } |
| 11215 | |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 11216 | bool InstCombiner::DoOneIteration(Function &F, unsigned Iteration) { |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 11217 | bool Changed = false; |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 11218 | TD = &getAnalysis<TargetData>(); |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 11219 | |
| 11220 | DEBUG(DOUT << "\n\nINSTCOMBINE ITERATION #" << Iteration << " on " |
| 11221 | << F.getNameStr() << "\n"); |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 11222 | |
Chris Lattner | b3d5970 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 11223 | { |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 11224 | // Do a depth-first traversal of the function, populate the worklist with |
| 11225 | // the reachable instructions. Ignore blocks that are not reachable. Keep |
| 11226 | // track of which blocks we visit. |
Chris Lattner | 1f87a58 | 2007-02-15 19:41:52 +0000 | [diff] [blame] | 11227 | SmallPtrSet<BasicBlock*, 64> Visited; |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11228 | AddReachableCodeToWorklist(F.begin(), Visited, *this, TD); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 11229 | |
Chris Lattner | b3d5970 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 11230 | // Do a quick scan over the function. If we find any blocks that are |
| 11231 | // unreachable, remove any instructions inside of them. This prevents |
| 11232 | // the instcombine code from having to deal with some bad special cases. |
| 11233 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 11234 | if (!Visited.count(BB)) { |
| 11235 | Instruction *Term = BB->getTerminator(); |
| 11236 | while (Term != BB->begin()) { // Remove instrs bottom-up |
| 11237 | BasicBlock::iterator I = Term; --I; |
Chris Lattner | 6ffe551 | 2004-04-27 15:13:33 +0000 | [diff] [blame] | 11238 | |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 11239 | DOUT << "IC: DCE: " << *I; |
Chris Lattner | b3d5970 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 11240 | ++NumDeadInst; |
| 11241 | |
| 11242 | if (!I->use_empty()) |
| 11243 | I->replaceAllUsesWith(UndefValue::get(I->getType())); |
| 11244 | I->eraseFromParent(); |
| 11245 | } |
| 11246 | } |
| 11247 | } |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 11248 | |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11249 | while (!Worklist.empty()) { |
| 11250 | Instruction *I = RemoveOneFromWorkList(); |
| 11251 | if (I == 0) continue; // skip null values. |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 11252 | |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 11253 | // Check to see if we can DCE the instruction. |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 11254 | if (isInstructionTriviallyDead(I)) { |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 11255 | // Add operands to the worklist. |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11256 | if (I->getNumOperands() < 4) |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 11257 | AddUsesToWorkList(*I); |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 11258 | ++NumDeadInst; |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11259 | |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 11260 | DOUT << "IC: DCE: " << *I; |
Chris Lattner | ad5fec1 | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 11261 | |
| 11262 | I->eraseFromParent(); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11263 | RemoveFromWorkList(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11264 | continue; |
| 11265 | } |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 11266 | |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 11267 | // Instruction isn't dead, see if we can constant propagate it. |
Chris Lattner | 0a19ffa | 2007-01-30 23:16:15 +0000 | [diff] [blame] | 11268 | if (Constant *C = ConstantFoldInstruction(I, TD)) { |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 11269 | DOUT << "IC: ConstFold to: " << *C << " from: " << *I; |
Chris Lattner | ad5fec1 | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 11270 | |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 11271 | // Add operands to the worklist. |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 11272 | AddUsesToWorkList(*I); |
Chris Lattner | c736d56 | 2002-12-05 22:41:53 +0000 | [diff] [blame] | 11273 | ReplaceInstUsesWith(*I, C); |
| 11274 | |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 11275 | ++NumConstProp; |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 11276 | I->eraseFromParent(); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11277 | RemoveFromWorkList(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11278 | continue; |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 11279 | } |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11280 | |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 11281 | // See if we can trivially sink this instruction to a successor basic block. |
| 11282 | if (I->hasOneUse()) { |
| 11283 | BasicBlock *BB = I->getParent(); |
| 11284 | BasicBlock *UserParent = cast<Instruction>(I->use_back())->getParent(); |
| 11285 | if (UserParent != BB) { |
| 11286 | bool UserIsSuccessor = false; |
| 11287 | // See if the user is one of our successors. |
| 11288 | for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI) |
| 11289 | if (*SI == UserParent) { |
| 11290 | UserIsSuccessor = true; |
| 11291 | break; |
| 11292 | } |
| 11293 | |
| 11294 | // If the user is one of our immediate successors, and if that successor |
| 11295 | // only has us as a predecessors (we'd have to split the critical edge |
| 11296 | // otherwise), we can keep going. |
| 11297 | if (UserIsSuccessor && !isa<PHINode>(I->use_back()) && |
| 11298 | next(pred_begin(UserParent)) == pred_end(UserParent)) |
| 11299 | // Okay, the CFG is simple enough, try to sink this instruction. |
| 11300 | Changed |= TryToSinkInstruction(I, UserParent); |
| 11301 | } |
| 11302 | } |
| 11303 | |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 11304 | // Now that we have an instruction, try combining it to simplify it... |
Reid Spencer | a9b8101 | 2007-03-26 17:44:01 +0000 | [diff] [blame] | 11305 | #ifndef NDEBUG |
| 11306 | std::string OrigI; |
| 11307 | #endif |
| 11308 | DEBUG(std::ostringstream SS; I->print(SS); OrigI = SS.str();); |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 11309 | if (Instruction *Result = visit(*I)) { |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 11310 | ++NumCombined; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 11311 | // Should we replace the old instruction with a new one? |
Chris Lattner | b3bc8fa | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 11312 | if (Result != I) { |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 11313 | DOUT << "IC: Old = " << *I |
| 11314 | << " New = " << *Result; |
Chris Lattner | 0cea42a | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 11315 | |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 11316 | // Everything uses the new instruction now. |
| 11317 | I->replaceAllUsesWith(Result); |
| 11318 | |
| 11319 | // Push the new instruction and any users onto the worklist. |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11320 | AddToWorkList(Result); |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 11321 | AddUsersToWorkList(*Result); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11322 | |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 11323 | // Move the name to the new instruction first. |
| 11324 | Result->takeName(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11325 | |
| 11326 | // Insert the new instruction into the basic block... |
| 11327 | BasicBlock *InstParent = I->getParent(); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 11328 | BasicBlock::iterator InsertPos = I; |
| 11329 | |
| 11330 | if (!isa<PHINode>(Result)) // If combining a PHI, don't insert |
| 11331 | while (isa<PHINode>(InsertPos)) // middle of a block of PHIs. |
| 11332 | ++InsertPos; |
| 11333 | |
| 11334 | InstParent->getInstList().insert(InsertPos, Result); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11335 | |
Chris Lattner | 00d5131 | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 11336 | // Make sure that we reprocess all operands now that we reduced their |
| 11337 | // use counts. |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11338 | AddUsesToWorkList(*I); |
Chris Lattner | 216d4d8 | 2004-05-01 23:19:52 +0000 | [diff] [blame] | 11339 | |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 11340 | // Instructions can end up on the worklist more than once. Make sure |
| 11341 | // we do not process an instruction that has been deleted. |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11342 | RemoveFromWorkList(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11343 | |
| 11344 | // Erase the old instruction. |
| 11345 | InstParent->getInstList().erase(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 11346 | } else { |
Evan Cheng | c7baf68 | 2007-03-27 16:44:48 +0000 | [diff] [blame] | 11347 | #ifndef NDEBUG |
Reid Spencer | a9b8101 | 2007-03-26 17:44:01 +0000 | [diff] [blame] | 11348 | DOUT << "IC: Mod = " << OrigI |
| 11349 | << " New = " << *I; |
Evan Cheng | c7baf68 | 2007-03-27 16:44:48 +0000 | [diff] [blame] | 11350 | #endif |
Chris Lattner | 0cea42a | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 11351 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 11352 | // If the instruction was modified, it's possible that it is now dead. |
| 11353 | // if so, remove it. |
Chris Lattner | 00d5131 | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 11354 | if (isInstructionTriviallyDead(I)) { |
| 11355 | // Make sure we process all operands now that we are reducing their |
| 11356 | // use counts. |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 11357 | AddUsesToWorkList(*I); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 11358 | |
Chris Lattner | 00d5131 | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 11359 | // Instructions may end up in the worklist more than once. Erase all |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 11360 | // occurrences of this instruction. |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11361 | RemoveFromWorkList(I); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 11362 | I->eraseFromParent(); |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 11363 | } else { |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 11364 | AddToWorkList(I); |
| 11365 | AddUsersToWorkList(*I); |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 11366 | } |
Chris Lattner | b3bc8fa | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 11367 | } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 11368 | Changed = true; |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 11369 | } |
| 11370 | } |
| 11371 | |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 11372 | assert(WorklistMap.empty() && "Worklist empty, but map not?"); |
Chris Lattner | a9ff5eb | 2007-08-05 08:47:58 +0000 | [diff] [blame] | 11373 | |
| 11374 | // Do an explicit clear, this shrinks the map if needed. |
| 11375 | WorklistMap.clear(); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 11376 | return Changed; |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 11377 | } |
| 11378 | |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 11379 | |
| 11380 | bool InstCombiner::runOnFunction(Function &F) { |
Chris Lattner | f964f32 | 2007-03-04 04:27:24 +0000 | [diff] [blame] | 11381 | MustPreserveLCSSA = mustPreserveAnalysisID(LCSSAID); |
| 11382 | |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 11383 | bool EverMadeChange = false; |
| 11384 | |
| 11385 | // Iterate while there is work to do. |
| 11386 | unsigned Iteration = 0; |
| 11387 | while (DoOneIteration(F, Iteration++)) |
| 11388 | EverMadeChange = true; |
| 11389 | return EverMadeChange; |
| 11390 | } |
| 11391 | |
Brian Gaeke | 96d4bf7 | 2004-07-27 17:43:21 +0000 | [diff] [blame] | 11392 | FunctionPass *llvm::createInstructionCombiningPass() { |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 11393 | return new InstCombiner(); |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 11394 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 11395 | |