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" |
Duncan Sands | b84abcd | 2007-09-11 14:35:41 +0000 | [diff] [blame] | 42 | #include "llvm/ParameterAttributes.h" |
Chris Lattner | 79066fa | 2007-01-30 23:46:24 +0000 | [diff] [blame] | 43 | #include "llvm/Analysis/ConstantFolding.h" |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 44 | #include "llvm/Target/TargetData.h" |
| 45 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 46 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 47 | #include "llvm/Support/CallSite.h" |
Nick Lewycky | 5be2920 | 2008-02-03 16:33:09 +0000 | [diff] [blame] | 48 | #include "llvm/Support/ConstantRange.h" |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 49 | #include "llvm/Support/Debug.h" |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 50 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 51 | #include "llvm/Support/InstVisitor.h" |
Chris Lattner | bcd7db5 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 52 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 53 | #include "llvm/Support/PatternMatch.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 54 | #include "llvm/Support/Compiler.h" |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 55 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 56 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 1f87a58 | 2007-02-15 19:41:52 +0000 | [diff] [blame] | 57 | #include "llvm/ADT/SmallPtrSet.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 58 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 59 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | b3bc8fa | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 60 | #include <algorithm> |
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); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 240 | |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 241 | public: |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 242 | // InsertNewInstBefore - insert an instruction New before instruction Old |
| 243 | // in the program. Add the new instruction to the worklist. |
| 244 | // |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 245 | Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) { |
Chris Lattner | e6f9a91 | 2002-08-23 18:32:43 +0000 | [diff] [blame] | 246 | assert(New && New->getParent() == 0 && |
| 247 | "New instruction already inserted into a basic block!"); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 248 | BasicBlock *BB = Old.getParent(); |
| 249 | BB->getInstList().insert(&Old, New); // Insert inst |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 250 | AddToWorkList(New); |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 251 | return New; |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 254 | /// InsertCastBefore - Insert a cast of V to TY before the instruction POS. |
| 255 | /// This also adds the cast to the worklist. Finally, this returns the |
| 256 | /// cast. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 257 | Value *InsertCastBefore(Instruction::CastOps opc, Value *V, const Type *Ty, |
| 258 | Instruction &Pos) { |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 259 | if (V->getType() == Ty) return V; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 260 | |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 261 | if (Constant *CV = dyn_cast<Constant>(V)) |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 262 | return ConstantExpr::getCast(opc, CV, Ty); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 263 | |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 264 | Instruction *C = CastInst::create(opc, V, Ty, V->getName(), &Pos); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 265 | AddToWorkList(C); |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 266 | return C; |
| 267 | } |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 268 | |
| 269 | Value *InsertBitCastBefore(Value *V, const Type *Ty, Instruction &Pos) { |
| 270 | return InsertCastBefore(Instruction::BitCast, V, Ty, Pos); |
| 271 | } |
| 272 | |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 273 | |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 274 | // ReplaceInstUsesWith - This method is to be used when an instruction is |
| 275 | // found to be dead, replacable with another preexisting expression. Here |
| 276 | // we add all uses of I to the worklist, replace all uses of I with the new |
| 277 | // value, then return I, so that the inst combiner will know that I was |
| 278 | // modified. |
| 279 | // |
| 280 | Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) { |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 281 | AddUsersToWorkList(I); // Add all modified instrs to worklist |
Chris Lattner | 15a76c0 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 282 | if (&I != V) { |
| 283 | I.replaceAllUsesWith(V); |
| 284 | return &I; |
| 285 | } else { |
| 286 | // If we are replacing the instruction with itself, this must be in a |
| 287 | // segment of unreachable code, so just clobber the instruction. |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 288 | I.replaceAllUsesWith(UndefValue::get(I.getType())); |
Chris Lattner | 15a76c0 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 289 | return &I; |
| 290 | } |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 291 | } |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 292 | |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 293 | // UpdateValueUsesWith - This method is to be used when an value is |
| 294 | // found to be replacable with another preexisting expression or was |
| 295 | // updated. Here we add all uses of I to the worklist, replace all uses of |
| 296 | // I with the new value (unless the instruction was just updated), then |
| 297 | // return true, so that the inst combiner will know that I was modified. |
| 298 | // |
| 299 | bool UpdateValueUsesWith(Value *Old, Value *New) { |
| 300 | AddUsersToWorkList(*Old); // Add all modified instrs to worklist |
| 301 | if (Old != New) |
| 302 | Old->replaceAllUsesWith(New); |
| 303 | if (Instruction *I = dyn_cast<Instruction>(Old)) |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 304 | AddToWorkList(I); |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 305 | if (Instruction *I = dyn_cast<Instruction>(New)) |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 306 | AddToWorkList(I); |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 307 | return true; |
| 308 | } |
| 309 | |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 310 | // EraseInstFromFunction - When dealing with an instruction that has side |
| 311 | // effects or produces a void value, we can't rely on DCE to delete the |
| 312 | // instruction. Instead, visit methods should return the value returned by |
| 313 | // this function. |
| 314 | Instruction *EraseInstFromFunction(Instruction &I) { |
| 315 | assert(I.use_empty() && "Cannot erase instruction that is used!"); |
| 316 | AddUsesToWorkList(I); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 317 | RemoveFromWorkList(&I); |
Chris Lattner | 954f66a | 2004-11-18 21:41:39 +0000 | [diff] [blame] | 318 | I.eraseFromParent(); |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 319 | return 0; // Don't do anything with FI |
| 320 | } |
| 321 | |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 322 | private: |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 323 | /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the |
| 324 | /// InsertBefore instruction. This is specialized a bit to avoid inserting |
| 325 | /// casts that are known to not do anything... |
| 326 | /// |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 327 | Value *InsertOperandCastBefore(Instruction::CastOps opcode, |
| 328 | Value *V, const Type *DestTy, |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 329 | Instruction *InsertBefore); |
| 330 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 331 | /// SimplifyCommutative - This performs a few simplifications for |
| 332 | /// commutative operators. |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 333 | bool SimplifyCommutative(BinaryOperator &I); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 334 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 335 | /// SimplifyCompare - This reorders the operands of a CmpInst to get them in |
| 336 | /// most-complex to least-complex order. |
| 337 | bool SimplifyCompare(CmpInst &I); |
| 338 | |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 339 | /// SimplifyDemandedBits - Attempts to replace V with a simpler value based |
| 340 | /// on the demanded bits. |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 341 | bool SimplifyDemandedBits(Value *V, APInt DemandedMask, |
| 342 | APInt& KnownZero, APInt& KnownOne, |
| 343 | unsigned Depth = 0); |
| 344 | |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 345 | Value *SimplifyDemandedVectorElts(Value *V, uint64_t DemandedElts, |
| 346 | uint64_t &UndefElts, unsigned Depth = 0); |
| 347 | |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 348 | // FoldOpIntoPhi - Given a binary operator or cast instruction which has a |
| 349 | // PHI node as operand #0, see if we can fold the instruction into the PHI |
| 350 | // (which is only possible if all operands to the PHI are constants). |
| 351 | Instruction *FoldOpIntoPhi(Instruction &I); |
| 352 | |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 353 | // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary" |
| 354 | // operator and they all are only used by the PHI, PHI together their |
| 355 | // inputs, and do the operation once, to the result of the PHI. |
| 356 | Instruction *FoldPHIArgOpIntoPHI(PHINode &PN); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 357 | Instruction *FoldPHIArgBinOpIntoPHI(PHINode &PN); |
| 358 | |
| 359 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 360 | Instruction *OptAndOp(Instruction *Op, ConstantInt *OpRHS, |
| 361 | ConstantInt *AndRHS, BinaryOperator &TheAnd); |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 362 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 363 | Value *FoldLogicalPlusAnd(Value *LHS, Value *RHS, ConstantInt *Mask, |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 364 | bool isSub, Instruction &I); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 365 | Instruction *InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 366 | bool isSigned, bool Inside, Instruction &IB); |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 367 | Instruction *PromoteCastOfAllocation(BitCastInst &CI, AllocationInst &AI); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 368 | Instruction *MatchBSwap(BinaryOperator &I); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 369 | bool SimplifyStoreAtEndOfBlock(StoreInst &SI); |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 370 | Instruction *SimplifyMemTransfer(MemIntrinsic *MI); |
| 371 | |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 372 | |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 373 | Value *EvaluateInDifferentType(Value *V, const Type *Ty, bool isSigned); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 374 | }; |
Chris Lattner | f629309 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 375 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 376 | char InstCombiner::ID = 0; |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 377 | RegisterPass<InstCombiner> X("instcombine", "Combine redundant instructions"); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 380 | // getComplexity: Assign a complexity or rank value to LLVM Values... |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 381 | // 0 -> undef, 1 -> Const, 2 -> Other, 3 -> Arg, 3 -> Unary, 4 -> OtherInst |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 382 | static unsigned getComplexity(Value *V) { |
| 383 | if (isa<Instruction>(V)) { |
| 384 | if (BinaryOperator::isNeg(V) || BinaryOperator::isNot(V)) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 385 | return 3; |
| 386 | return 4; |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 387 | } |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 388 | if (isa<Argument>(V)) return 3; |
| 389 | return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2; |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 390 | } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 391 | |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 392 | // isOnlyUse - Return true if this instruction will be deleted if we stop using |
| 393 | // it. |
| 394 | static bool isOnlyUse(Value *V) { |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 395 | return V->hasOneUse() || isa<Constant>(V); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 396 | } |
| 397 | |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 398 | // getPromotedType - Return the specified type promoted as it would be to pass |
| 399 | // though a va_arg area... |
| 400 | static const Type *getPromotedType(const Type *Ty) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 401 | if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty)) { |
| 402 | if (ITy->getBitWidth() < 32) |
| 403 | return Type::Int32Ty; |
Chris Lattner | 2b7e0ad | 2007-05-23 01:17:04 +0000 | [diff] [blame] | 404 | } |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 405 | return Ty; |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 408 | /// getBitCastOperand - If the specified operand is a CastInst or a constant |
| 409 | /// expression bitcast, return the operand value, otherwise return null. |
| 410 | static Value *getBitCastOperand(Value *V) { |
| 411 | if (BitCastInst *I = dyn_cast<BitCastInst>(V)) |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 412 | return I->getOperand(0); |
| 413 | else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 414 | if (CE->getOpcode() == Instruction::BitCast) |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 415 | return CE->getOperand(0); |
| 416 | return 0; |
| 417 | } |
| 418 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 419 | /// This function is a wrapper around CastInst::isEliminableCastPair. It |
| 420 | /// simply extracts arguments and returns what that function returns. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 421 | static Instruction::CastOps |
| 422 | isEliminableCastPair( |
| 423 | const CastInst *CI, ///< The first cast instruction |
| 424 | unsigned opcode, ///< The opcode of the second cast instruction |
| 425 | const Type *DstTy, ///< The target type for the second cast instruction |
| 426 | TargetData *TD ///< The target data for pointer size |
| 427 | ) { |
| 428 | |
| 429 | const Type *SrcTy = CI->getOperand(0)->getType(); // A from above |
| 430 | const Type *MidTy = CI->getType(); // B from above |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 431 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 432 | // Get the opcodes of the two Cast instructions |
| 433 | Instruction::CastOps firstOp = Instruction::CastOps(CI->getOpcode()); |
| 434 | Instruction::CastOps secondOp = Instruction::CastOps(opcode); |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 435 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 436 | return Instruction::CastOps( |
| 437 | CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, |
| 438 | DstTy, TD->getIntPtrType())); |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | /// ValueRequiresCast - Return true if the cast from "V to Ty" actually results |
| 442 | /// in any code being generated. It does not require codegen if V is simple |
| 443 | /// enough or if the cast can be folded into other casts. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 444 | static bool ValueRequiresCast(Instruction::CastOps opcode, const Value *V, |
| 445 | const Type *Ty, TargetData *TD) { |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 446 | if (V->getType() == Ty || isa<Constant>(V)) return false; |
| 447 | |
Chris Lattner | 01575b7 | 2006-05-25 23:24:33 +0000 | [diff] [blame] | 448 | // 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] | 449 | if (const CastInst *CI = dyn_cast<CastInst>(V)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 450 | if (isEliminableCastPair(CI, opcode, Ty, TD)) |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 451 | return false; |
| 452 | return true; |
| 453 | } |
| 454 | |
| 455 | /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the |
| 456 | /// InsertBefore instruction. This is specialized a bit to avoid inserting |
| 457 | /// casts that are known to not do anything... |
| 458 | /// |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 459 | Value *InstCombiner::InsertOperandCastBefore(Instruction::CastOps opcode, |
| 460 | Value *V, const Type *DestTy, |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 461 | Instruction *InsertBefore) { |
| 462 | if (V->getType() == DestTy) return V; |
| 463 | if (Constant *C = dyn_cast<Constant>(V)) |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 464 | return ConstantExpr::getCast(opcode, C, DestTy); |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 465 | |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 466 | return InsertCastBefore(opcode, V, DestTy, *InsertBefore); |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 469 | // SimplifyCommutative - This performs a few simplifications for commutative |
| 470 | // operators: |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 471 | // |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 472 | // 1. Order operands such that they are listed from right (least complex) to |
| 473 | // left (most complex). This puts constants before unary operators before |
| 474 | // binary operators. |
| 475 | // |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 476 | // 2. Transform: (op (op V, C1), C2) ==> (op V, (op C1, C2)) |
| 477 | // 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] | 478 | // |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 479 | bool InstCombiner::SimplifyCommutative(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 480 | bool Changed = false; |
| 481 | if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1))) |
| 482 | Changed = !I.swapOperands(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 483 | |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 484 | if (!I.isAssociative()) return Changed; |
| 485 | Instruction::BinaryOps Opcode = I.getOpcode(); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 486 | if (BinaryOperator *Op = dyn_cast<BinaryOperator>(I.getOperand(0))) |
| 487 | if (Op->getOpcode() == Opcode && isa<Constant>(Op->getOperand(1))) { |
| 488 | if (isa<Constant>(I.getOperand(1))) { |
Chris Lattner | 2a9c847 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 489 | Constant *Folded = ConstantExpr::get(I.getOpcode(), |
| 490 | cast<Constant>(I.getOperand(1)), |
| 491 | cast<Constant>(Op->getOperand(1))); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 492 | I.setOperand(0, Op->getOperand(0)); |
| 493 | I.setOperand(1, Folded); |
| 494 | return true; |
| 495 | } else if (BinaryOperator *Op1=dyn_cast<BinaryOperator>(I.getOperand(1))) |
| 496 | if (Op1->getOpcode() == Opcode && isa<Constant>(Op1->getOperand(1)) && |
| 497 | isOnlyUse(Op) && isOnlyUse(Op1)) { |
| 498 | Constant *C1 = cast<Constant>(Op->getOperand(1)); |
| 499 | Constant *C2 = cast<Constant>(Op1->getOperand(1)); |
| 500 | |
| 501 | // 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] | 502 | Constant *Folded = ConstantExpr::get(I.getOpcode(), C1, C2); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 503 | Instruction *New = BinaryOperator::create(Opcode, Op->getOperand(0), |
| 504 | Op1->getOperand(0), |
| 505 | Op1->getName(), &I); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 506 | AddToWorkList(New); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 507 | I.setOperand(0, New); |
| 508 | I.setOperand(1, Folded); |
| 509 | return true; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 510 | } |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 511 | } |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 512 | return Changed; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 513 | } |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 514 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 515 | /// SimplifyCompare - For a CmpInst this function just orders the operands |
| 516 | /// so that theyare listed from right (least complex) to left (most complex). |
| 517 | /// This puts constants before unary operators before binary operators. |
| 518 | bool InstCombiner::SimplifyCompare(CmpInst &I) { |
| 519 | if (getComplexity(I.getOperand(0)) >= getComplexity(I.getOperand(1))) |
| 520 | return false; |
| 521 | I.swapOperands(); |
| 522 | // Compare instructions are not associative so there's nothing else we can do. |
| 523 | return true; |
| 524 | } |
| 525 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 526 | // dyn_castNegVal - Given a 'sub' instruction, return the RHS of the instruction |
| 527 | // if the LHS is a constant zero (which is the 'negate' form). |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 528 | // |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 529 | static inline Value *dyn_castNegVal(Value *V) { |
| 530 | if (BinaryOperator::isNeg(V)) |
Chris Lattner | a1df33c | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 531 | return BinaryOperator::getNegArgument(V); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 532 | |
Chris Lattner | 0ce8580 | 2004-12-14 20:08:06 +0000 | [diff] [blame] | 533 | // Constants can be considered to be negated values if they can be folded. |
| 534 | if (ConstantInt *C = dyn_cast<ConstantInt>(V)) |
| 535 | return ConstantExpr::getNeg(C); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 536 | return 0; |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 539 | static inline Value *dyn_castNotVal(Value *V) { |
| 540 | if (BinaryOperator::isNot(V)) |
Chris Lattner | a1df33c | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 541 | return BinaryOperator::getNotArgument(V); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 542 | |
| 543 | // Constants can be considered to be not'ed values... |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 544 | if (ConstantInt *C = dyn_cast<ConstantInt>(V)) |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 545 | return ConstantInt::get(~C->getValue()); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 546 | return 0; |
| 547 | } |
| 548 | |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 549 | // dyn_castFoldableMul - If this value is a multiply that can be folded into |
| 550 | // other computations (because it has a constant operand), return the |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 551 | // non-constant operand of the multiply, and set CST to point to the multiplier. |
| 552 | // Otherwise, return null. |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 553 | // |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 554 | static inline Value *dyn_castFoldableMul(Value *V, ConstantInt *&CST) { |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 555 | if (V->hasOneUse() && V->getType()->isInteger()) |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 556 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 557 | if (I->getOpcode() == Instruction::Mul) |
Chris Lattner | 50e60c7 | 2004-11-15 05:54:07 +0000 | [diff] [blame] | 558 | if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 559 | return I->getOperand(0); |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 560 | if (I->getOpcode() == Instruction::Shl) |
Chris Lattner | 50e60c7 | 2004-11-15 05:54:07 +0000 | [diff] [blame] | 561 | if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) { |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 562 | // The multiplier is really 1 << CST. |
Zhou Sheng | 97b52c2 | 2007-03-29 01:57:21 +0000 | [diff] [blame] | 563 | uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth(); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 564 | uint32_t CSTVal = CST->getLimitedValue(BitWidth); |
Zhou Sheng | 97b52c2 | 2007-03-29 01:57:21 +0000 | [diff] [blame] | 565 | CST = ConstantInt::get(APInt(BitWidth, 1).shl(CSTVal)); |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 566 | return I->getOperand(0); |
| 567 | } |
| 568 | } |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 569 | return 0; |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 570 | } |
Chris Lattner | af2930e | 2002-08-14 17:51:49 +0000 | [diff] [blame] | 571 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 572 | /// dyn_castGetElementPtr - If this is a getelementptr instruction or constant |
| 573 | /// expression, return it. |
| 574 | static User *dyn_castGetElementPtr(Value *V) { |
| 575 | if (isa<GetElementPtrInst>(V)) return cast<User>(V); |
| 576 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
| 577 | if (CE->getOpcode() == Instruction::GetElementPtr) |
| 578 | return cast<User>(V); |
| 579 | return false; |
| 580 | } |
| 581 | |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 582 | /// AddOne - Add one to a ConstantInt |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 583 | static ConstantInt *AddOne(ConstantInt *C) { |
Reid Spencer | 2149a9d | 2007-03-25 19:55:33 +0000 | [diff] [blame] | 584 | APInt Val(C->getValue()); |
| 585 | return ConstantInt::get(++Val); |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 586 | } |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 587 | /// SubOne - Subtract one from a ConstantInt |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 588 | static ConstantInt *SubOne(ConstantInt *C) { |
Reid Spencer | 2149a9d | 2007-03-25 19:55:33 +0000 | [diff] [blame] | 589 | APInt Val(C->getValue()); |
| 590 | return ConstantInt::get(--Val); |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 591 | } |
| 592 | /// Add - Add two ConstantInts together |
| 593 | static ConstantInt *Add(ConstantInt *C1, ConstantInt *C2) { |
| 594 | return ConstantInt::get(C1->getValue() + C2->getValue()); |
| 595 | } |
| 596 | /// And - Bitwise AND two ConstantInts together |
| 597 | static ConstantInt *And(ConstantInt *C1, ConstantInt *C2) { |
| 598 | return ConstantInt::get(C1->getValue() & C2->getValue()); |
| 599 | } |
| 600 | /// Subtract - Subtract one ConstantInt from another |
| 601 | static ConstantInt *Subtract(ConstantInt *C1, ConstantInt *C2) { |
| 602 | return ConstantInt::get(C1->getValue() - C2->getValue()); |
| 603 | } |
| 604 | /// Multiply - Multiply two ConstantInts together |
| 605 | static ConstantInt *Multiply(ConstantInt *C1, ConstantInt *C2) { |
| 606 | return ConstantInt::get(C1->getValue() * C2->getValue()); |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 607 | } |
Nick Lewycky | e0cfecf | 2008-02-18 22:48:05 +0000 | [diff] [blame] | 608 | /// MultiplyOverflows - True if the multiply can not be expressed in an int |
| 609 | /// this size. |
| 610 | static bool MultiplyOverflows(ConstantInt *C1, ConstantInt *C2, bool sign) { |
| 611 | uint32_t W = C1->getBitWidth(); |
| 612 | APInt LHSExt = C1->getValue(), RHSExt = C2->getValue(); |
| 613 | if (sign) { |
| 614 | LHSExt.sext(W * 2); |
| 615 | RHSExt.sext(W * 2); |
| 616 | } else { |
| 617 | LHSExt.zext(W * 2); |
| 618 | RHSExt.zext(W * 2); |
| 619 | } |
| 620 | |
| 621 | APInt MulExt = LHSExt * RHSExt; |
| 622 | |
| 623 | if (sign) { |
| 624 | APInt Min = APInt::getSignedMinValue(W).sext(W * 2); |
| 625 | APInt Max = APInt::getSignedMaxValue(W).sext(W * 2); |
| 626 | return MulExt.slt(Min) || MulExt.sgt(Max); |
| 627 | } else |
| 628 | return MulExt.ugt(APInt::getLowBitsSet(W * 2, W)); |
| 629 | } |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 630 | |
Chris Lattner | 68d5ff2 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 631 | /// ComputeMaskedBits - Determine which of the bits specified in Mask are |
| 632 | /// 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] | 633 | /// bit sets. This code only analyzes bits in Mask, in order to short-circuit |
| 634 | /// processing. |
| 635 | /// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that |
| 636 | /// we cannot optimize based on the assumption that it is zero without changing |
| 637 | /// it to be an explicit zero. If we don't change it to zero, other code could |
| 638 | /// optimized based on the contradictory assumption that it is non-zero. |
| 639 | /// Because instcombine aggressively folds operations with undef args anyway, |
| 640 | /// this won't lose us code quality. |
Reid Spencer | 55702aa | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 641 | static void ComputeMaskedBits(Value *V, const APInt &Mask, APInt& KnownZero, |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 642 | APInt& KnownOne, unsigned Depth = 0) { |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 643 | assert(V && "No Value?"); |
| 644 | assert(Depth <= 6 && "Limit Search Depth"); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 645 | uint32_t BitWidth = Mask.getBitWidth(); |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 646 | assert(cast<IntegerType>(V->getType())->getBitWidth() == BitWidth && |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 647 | KnownZero.getBitWidth() == BitWidth && |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 648 | KnownOne.getBitWidth() == BitWidth && |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 649 | "V, Mask, KnownOne and KnownZero should have same BitWidth"); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 650 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
| 651 | // We know all of the bits for a constant! |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 652 | KnownOne = CI->getValue() & Mask; |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 653 | KnownZero = ~KnownOne & Mask; |
| 654 | return; |
| 655 | } |
| 656 | |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 657 | if (Depth == 6 || Mask == 0) |
| 658 | return; // Limit search depth. |
| 659 | |
| 660 | Instruction *I = dyn_cast<Instruction>(V); |
| 661 | if (!I) return; |
| 662 | |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 663 | KnownZero.clear(); KnownOne.clear(); // Don't know anything. |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 664 | APInt KnownZero2(KnownZero), KnownOne2(KnownOne); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 665 | |
| 666 | switch (I->getOpcode()) { |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 667 | case Instruction::And: { |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 668 | // If either the LHS or the RHS are Zero, the result is zero. |
| 669 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, Depth+1); |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 670 | APInt Mask2(Mask & ~KnownZero); |
| 671 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 672 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 673 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 674 | |
| 675 | // Output known-1 bits are only known if set in both the LHS & RHS. |
| 676 | KnownOne &= KnownOne2; |
| 677 | // Output known-0 are known to be clear if zero in either the LHS | RHS. |
| 678 | KnownZero |= KnownZero2; |
| 679 | return; |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 680 | } |
| 681 | case Instruction::Or: { |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 682 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, Depth+1); |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 683 | APInt Mask2(Mask & ~KnownOne); |
| 684 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 685 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 686 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 687 | |
| 688 | // Output known-0 bits are only known if clear in both the LHS & RHS. |
| 689 | KnownZero &= KnownZero2; |
| 690 | // Output known-1 are known to be set if set in either the LHS | RHS. |
| 691 | KnownOne |= KnownOne2; |
| 692 | return; |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 693 | } |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 694 | case Instruction::Xor: { |
| 695 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, Depth+1); |
| 696 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1); |
| 697 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 698 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 699 | |
| 700 | // Output known-0 bits are known if clear or set in both the LHS & RHS. |
| 701 | APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2); |
| 702 | // Output known-1 are known to be set if set in only one of the LHS, RHS. |
| 703 | KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2); |
| 704 | KnownZero = KnownZeroOut; |
| 705 | return; |
| 706 | } |
| 707 | case Instruction::Select: |
| 708 | ComputeMaskedBits(I->getOperand(2), Mask, KnownZero, KnownOne, Depth+1); |
| 709 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1); |
| 710 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 711 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 712 | |
| 713 | // Only known if known in both the LHS and RHS. |
| 714 | KnownOne &= KnownOne2; |
| 715 | KnownZero &= KnownZero2; |
| 716 | return; |
| 717 | case Instruction::FPTrunc: |
| 718 | case Instruction::FPExt: |
| 719 | case Instruction::FPToUI: |
| 720 | case Instruction::FPToSI: |
| 721 | case Instruction::SIToFP: |
| 722 | case Instruction::PtrToInt: |
| 723 | case Instruction::UIToFP: |
| 724 | case Instruction::IntToPtr: |
| 725 | return; // Can't work with floating point or pointers |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 726 | case Instruction::Trunc: { |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 727 | // All these have integer operands |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 728 | uint32_t SrcBitWidth = |
| 729 | cast<IntegerType>(I->getOperand(0)->getType())->getBitWidth(); |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 730 | APInt MaskIn(Mask); |
| 731 | MaskIn.zext(SrcBitWidth); |
| 732 | KnownZero.zext(SrcBitWidth); |
| 733 | KnownOne.zext(SrcBitWidth); |
| 734 | ComputeMaskedBits(I->getOperand(0), MaskIn, KnownZero, KnownOne, Depth+1); |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 735 | KnownZero.trunc(BitWidth); |
| 736 | KnownOne.trunc(BitWidth); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 737 | return; |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 738 | } |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 739 | case Instruction::BitCast: { |
| 740 | const Type *SrcTy = I->getOperand(0)->getType(); |
| 741 | if (SrcTy->isInteger()) { |
| 742 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero, KnownOne, Depth+1); |
| 743 | return; |
| 744 | } |
| 745 | break; |
| 746 | } |
| 747 | case Instruction::ZExt: { |
| 748 | // Compute the bits in the result that are not present in the input. |
| 749 | const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType()); |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 750 | uint32_t SrcBitWidth = SrcTy->getBitWidth(); |
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 751 | |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 752 | APInt MaskIn(Mask); |
| 753 | MaskIn.trunc(SrcBitWidth); |
| 754 | KnownZero.trunc(SrcBitWidth); |
| 755 | KnownOne.trunc(SrcBitWidth); |
| 756 | ComputeMaskedBits(I->getOperand(0), MaskIn, KnownZero, KnownOne, Depth+1); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 757 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 758 | // The top bits are known to be zero. |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 759 | KnownZero.zext(BitWidth); |
| 760 | KnownOne.zext(BitWidth); |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 761 | KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 762 | return; |
| 763 | } |
| 764 | case Instruction::SExt: { |
| 765 | // Compute the bits in the result that are not present in the input. |
| 766 | const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType()); |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 767 | uint32_t SrcBitWidth = SrcTy->getBitWidth(); |
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 768 | |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 769 | APInt MaskIn(Mask); |
| 770 | MaskIn.trunc(SrcBitWidth); |
| 771 | KnownZero.trunc(SrcBitWidth); |
| 772 | KnownOne.trunc(SrcBitWidth); |
| 773 | ComputeMaskedBits(I->getOperand(0), MaskIn, KnownZero, KnownOne, Depth+1); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 774 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 775 | KnownZero.zext(BitWidth); |
| 776 | KnownOne.zext(BitWidth); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 777 | |
| 778 | // If the sign bit of the input is known set or clear, then we know the |
| 779 | // top bits of the result. |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 780 | if (KnownZero[SrcBitWidth-1]) // Input sign bit known zero |
Zhou Sheng | 34a4b38 | 2007-03-28 17:38:21 +0000 | [diff] [blame] | 781 | KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth); |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 782 | else if (KnownOne[SrcBitWidth-1]) // Input sign bit known set |
Zhou Sheng | 34a4b38 | 2007-03-28 17:38:21 +0000 | [diff] [blame] | 783 | KnownOne |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 784 | return; |
| 785 | } |
| 786 | case Instruction::Shl: |
| 787 | // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0 |
| 788 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 789 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 790 | APInt Mask2(Mask.lshr(ShiftAmt)); |
| 791 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero, KnownOne, Depth+1); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 792 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
Zhou Sheng | 430f626 | 2007-03-12 05:44:52 +0000 | [diff] [blame] | 793 | KnownZero <<= ShiftAmt; |
| 794 | KnownOne <<= ShiftAmt; |
Reid Spencer | 2149a9d | 2007-03-25 19:55:33 +0000 | [diff] [blame] | 795 | KnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt); // low bits known 0 |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 796 | return; |
| 797 | } |
| 798 | break; |
| 799 | case Instruction::LShr: |
| 800 | // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0 |
| 801 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 802 | // Compute the new bits that are at the top now. |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 803 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 804 | |
| 805 | // Unsigned shift right. |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 806 | APInt Mask2(Mask.shl(ShiftAmt)); |
| 807 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero,KnownOne,Depth+1); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 808 | assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); |
| 809 | KnownZero = APIntOps::lshr(KnownZero, ShiftAmt); |
| 810 | KnownOne = APIntOps::lshr(KnownOne, ShiftAmt); |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 811 | // high bits known zero. |
| 812 | KnownZero |= APInt::getHighBitsSet(BitWidth, ShiftAmt); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 813 | return; |
| 814 | } |
| 815 | break; |
| 816 | case Instruction::AShr: |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 817 | // (ashr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0 |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 818 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 819 | // Compute the new bits that are at the top now. |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 820 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 821 | |
| 822 | // Signed shift right. |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 823 | APInt Mask2(Mask.shl(ShiftAmt)); |
| 824 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero,KnownOne,Depth+1); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 825 | assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); |
| 826 | KnownZero = APIntOps::lshr(KnownZero, ShiftAmt); |
| 827 | KnownOne = APIntOps::lshr(KnownOne, ShiftAmt); |
| 828 | |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 829 | APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt)); |
| 830 | if (KnownZero[BitWidth-ShiftAmt-1]) // New bits are known zero. |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 831 | KnownZero |= HighBits; |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 832 | else if (KnownOne[BitWidth-ShiftAmt-1]) // New bits are known one. |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 833 | KnownOne |= HighBits; |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 834 | return; |
| 835 | } |
| 836 | break; |
| 837 | } |
| 838 | } |
| 839 | |
Reid Spencer | e7816b5 | 2007-03-08 01:52:58 +0000 | [diff] [blame] | 840 | /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use |
| 841 | /// this predicate to simplify operations downstream. Mask is known to be zero |
| 842 | /// for bits that V cannot have. |
| 843 | static bool MaskedValueIsZero(Value *V, const APInt& Mask, unsigned Depth = 0) { |
Zhou Sheng | edd089c | 2007-03-12 16:54:56 +0000 | [diff] [blame] | 844 | APInt KnownZero(Mask.getBitWidth(), 0), KnownOne(Mask.getBitWidth(), 0); |
Reid Spencer | e7816b5 | 2007-03-08 01:52:58 +0000 | [diff] [blame] | 845 | ComputeMaskedBits(V, Mask, KnownZero, KnownOne, Depth); |
| 846 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 847 | return (KnownZero & Mask) == Mask; |
| 848 | } |
| 849 | |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 850 | /// ShrinkDemandedConstant - Check to see if the specified operand of the |
| 851 | /// specified instruction is a constant integer. If so, check to see if there |
| 852 | /// are any bits set in the constant that are not demanded. If so, shrink the |
| 853 | /// constant and return true. |
| 854 | static bool ShrinkDemandedConstant(Instruction *I, unsigned OpNo, |
Reid Spencer | 6b79e2d | 2007-03-12 17:15:10 +0000 | [diff] [blame] | 855 | APInt Demanded) { |
| 856 | assert(I && "No instruction?"); |
| 857 | assert(OpNo < I->getNumOperands() && "Operand index too large"); |
| 858 | |
| 859 | // If the operand is not a constant integer, nothing to do. |
| 860 | ConstantInt *OpC = dyn_cast<ConstantInt>(I->getOperand(OpNo)); |
| 861 | if (!OpC) return false; |
| 862 | |
| 863 | // If there are no bits set that aren't demanded, nothing to do. |
| 864 | Demanded.zextOrTrunc(OpC->getValue().getBitWidth()); |
| 865 | if ((~Demanded & OpC->getValue()) == 0) |
| 866 | return false; |
| 867 | |
| 868 | // This instruction is producing bits that are not demanded. Shrink the RHS. |
| 869 | Demanded &= OpC->getValue(); |
| 870 | I->setOperand(OpNo, ConstantInt::get(Demanded)); |
| 871 | return true; |
| 872 | } |
| 873 | |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 874 | // ComputeSignedMinMaxValuesFromKnownBits - Given a signed integer type and a |
| 875 | // set of known zero and one bits, compute the maximum and minimum values that |
| 876 | // could have the specified known zero and known one bits, returning them in |
| 877 | // min/max. |
| 878 | static void ComputeSignedMinMaxValuesFromKnownBits(const Type *Ty, |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 879 | const APInt& KnownZero, |
| 880 | const APInt& KnownOne, |
| 881 | APInt& Min, APInt& Max) { |
| 882 | uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth(); |
| 883 | assert(KnownZero.getBitWidth() == BitWidth && |
| 884 | KnownOne.getBitWidth() == BitWidth && |
| 885 | Min.getBitWidth() == BitWidth && Max.getBitWidth() == BitWidth && |
| 886 | "Ty, KnownZero, KnownOne and Min, Max must have equal bitwidth."); |
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 887 | APInt UnknownBits = ~(KnownZero|KnownOne); |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 888 | |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 889 | // The minimum value is when all unknown bits are zeros, EXCEPT for the sign |
| 890 | // bit if it is unknown. |
| 891 | Min = KnownOne; |
| 892 | Max = KnownOne|UnknownBits; |
| 893 | |
Zhou Sheng | 4acf155 | 2007-03-28 05:15:57 +0000 | [diff] [blame] | 894 | if (UnknownBits[BitWidth-1]) { // Sign bit is unknown |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 895 | Min.set(BitWidth-1); |
| 896 | Max.clear(BitWidth-1); |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 897 | } |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | // ComputeUnsignedMinMaxValuesFromKnownBits - Given an unsigned integer type and |
| 901 | // a set of known zero and one bits, compute the maximum and minimum values that |
| 902 | // could have the specified known zero and known one bits, returning them in |
| 903 | // min/max. |
| 904 | static void ComputeUnsignedMinMaxValuesFromKnownBits(const Type *Ty, |
Chris Lattner | a9ff5eb | 2007-08-05 08:47:58 +0000 | [diff] [blame] | 905 | const APInt &KnownZero, |
| 906 | const APInt &KnownOne, |
| 907 | APInt &Min, APInt &Max) { |
| 908 | uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth(); BitWidth = BitWidth; |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 909 | assert(KnownZero.getBitWidth() == BitWidth && |
| 910 | KnownOne.getBitWidth() == BitWidth && |
| 911 | Min.getBitWidth() == BitWidth && Max.getBitWidth() && |
| 912 | "Ty, KnownZero, KnownOne and Min, Max must have equal bitwidth."); |
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 913 | APInt UnknownBits = ~(KnownZero|KnownOne); |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 914 | |
| 915 | // The minimum value is when the unknown bits are all zeros. |
| 916 | Min = KnownOne; |
| 917 | // The maximum value is when the unknown bits are all ones. |
| 918 | Max = KnownOne|UnknownBits; |
| 919 | } |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 920 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 921 | /// SimplifyDemandedBits - This function attempts to replace V with a simpler |
| 922 | /// value based on the demanded bits. When this function is called, it is known |
| 923 | /// that only the bits set in DemandedMask of the result of V are ever used |
| 924 | /// downstream. Consequently, depending on the mask and V, it may be possible |
| 925 | /// to replace V with a constant or one of its operands. In such cases, this |
| 926 | /// function does the replacement and returns true. In all other cases, it |
| 927 | /// returns false after analyzing the expression and setting KnownOne and known |
| 928 | /// to be one in the expression. KnownZero contains all the bits that are known |
| 929 | /// to be zero in the expression. These are provided to potentially allow the |
| 930 | /// caller (which might recursively be SimplifyDemandedBits itself) to simplify |
| 931 | /// the expression. KnownOne and KnownZero always follow the invariant that |
| 932 | /// KnownOne & KnownZero == 0. That is, a bit can't be both 1 and 0. Note that |
| 933 | /// the bits in KnownOne and KnownZero may only be accurate for those bits set |
| 934 | /// in DemandedMask. Note also that the bitwidth of V, DemandedMask, KnownZero |
| 935 | /// and KnownOne must all be the same. |
| 936 | bool InstCombiner::SimplifyDemandedBits(Value *V, APInt DemandedMask, |
| 937 | APInt& KnownZero, APInt& KnownOne, |
| 938 | unsigned Depth) { |
| 939 | assert(V != 0 && "Null pointer of Value???"); |
| 940 | assert(Depth <= 6 && "Limit Search Depth"); |
| 941 | uint32_t BitWidth = DemandedMask.getBitWidth(); |
| 942 | const IntegerType *VTy = cast<IntegerType>(V->getType()); |
| 943 | assert(VTy->getBitWidth() == BitWidth && |
| 944 | KnownZero.getBitWidth() == BitWidth && |
| 945 | KnownOne.getBitWidth() == BitWidth && |
| 946 | "Value *V, DemandedMask, KnownZero and KnownOne \ |
| 947 | must have same BitWidth"); |
| 948 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
| 949 | // We know all of the bits for a constant! |
| 950 | KnownOne = CI->getValue() & DemandedMask; |
| 951 | KnownZero = ~KnownOne & DemandedMask; |
| 952 | return false; |
| 953 | } |
| 954 | |
Zhou Sheng | 9670445 | 2007-03-14 03:21:24 +0000 | [diff] [blame] | 955 | KnownZero.clear(); |
| 956 | KnownOne.clear(); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 957 | if (!V->hasOneUse()) { // Other users may use these bits. |
| 958 | if (Depth != 0) { // Not at the root. |
| 959 | // Just compute the KnownZero/KnownOne bits to simplify things downstream. |
| 960 | ComputeMaskedBits(V, DemandedMask, KnownZero, KnownOne, Depth); |
| 961 | return false; |
| 962 | } |
| 963 | // If this is the root being simplified, allow it to have multiple uses, |
| 964 | // just set the DemandedMask to all bits. |
| 965 | DemandedMask = APInt::getAllOnesValue(BitWidth); |
| 966 | } else if (DemandedMask == 0) { // Not demanding any bits from V. |
| 967 | if (V != UndefValue::get(VTy)) |
| 968 | return UpdateValueUsesWith(V, UndefValue::get(VTy)); |
| 969 | return false; |
| 970 | } else if (Depth == 6) { // Limit search depth. |
| 971 | return false; |
| 972 | } |
| 973 | |
| 974 | Instruction *I = dyn_cast<Instruction>(V); |
| 975 | if (!I) return false; // Only analyze instructions. |
| 976 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 977 | APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0); |
| 978 | APInt &RHSKnownZero = KnownZero, &RHSKnownOne = KnownOne; |
| 979 | switch (I->getOpcode()) { |
| 980 | default: break; |
| 981 | case Instruction::And: |
| 982 | // If either the LHS or the RHS are Zero, the result is zero. |
| 983 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, |
| 984 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 985 | return true; |
| 986 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 987 | "Bits known to be one AND zero?"); |
| 988 | |
| 989 | // If something is known zero on the RHS, the bits aren't demanded on the |
| 990 | // LHS. |
| 991 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask & ~RHSKnownZero, |
| 992 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 993 | return true; |
| 994 | assert((LHSKnownZero & LHSKnownOne) == 0 && |
| 995 | "Bits known to be one AND zero?"); |
| 996 | |
| 997 | // If all of the demanded bits are known 1 on one side, return the other. |
| 998 | // These bits cannot contribute to the result of the 'and'. |
| 999 | if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) == |
| 1000 | (DemandedMask & ~LHSKnownZero)) |
| 1001 | return UpdateValueUsesWith(I, I->getOperand(0)); |
| 1002 | if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) == |
| 1003 | (DemandedMask & ~RHSKnownZero)) |
| 1004 | return UpdateValueUsesWith(I, I->getOperand(1)); |
| 1005 | |
| 1006 | // If all of the demanded bits in the inputs are known zeros, return zero. |
| 1007 | if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask) |
| 1008 | return UpdateValueUsesWith(I, Constant::getNullValue(VTy)); |
| 1009 | |
| 1010 | // If the RHS is a constant, see if we can simplify it. |
| 1011 | if (ShrinkDemandedConstant(I, 1, DemandedMask & ~LHSKnownZero)) |
| 1012 | return UpdateValueUsesWith(I, I); |
| 1013 | |
| 1014 | // Output known-1 bits are only known if set in both the LHS & RHS. |
| 1015 | RHSKnownOne &= LHSKnownOne; |
| 1016 | // Output known-0 are known to be clear if zero in either the LHS | RHS. |
| 1017 | RHSKnownZero |= LHSKnownZero; |
| 1018 | break; |
| 1019 | case Instruction::Or: |
| 1020 | // If either the LHS or the RHS are One, the result is One. |
| 1021 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, |
| 1022 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 1023 | return true; |
| 1024 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1025 | "Bits known to be one AND zero?"); |
| 1026 | // If something is known one on the RHS, the bits aren't demanded on the |
| 1027 | // LHS. |
| 1028 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask & ~RHSKnownOne, |
| 1029 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1030 | return true; |
| 1031 | assert((LHSKnownZero & LHSKnownOne) == 0 && |
| 1032 | "Bits known to be one AND zero?"); |
| 1033 | |
| 1034 | // If all of the demanded bits are known zero on one side, return the other. |
| 1035 | // These bits cannot contribute to the result of the 'or'. |
| 1036 | if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) == |
| 1037 | (DemandedMask & ~LHSKnownOne)) |
| 1038 | return UpdateValueUsesWith(I, I->getOperand(0)); |
| 1039 | if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) == |
| 1040 | (DemandedMask & ~RHSKnownOne)) |
| 1041 | return UpdateValueUsesWith(I, I->getOperand(1)); |
| 1042 | |
| 1043 | // If all of the potentially set bits on one side are known to be set on |
| 1044 | // the other side, just use the 'other' side. |
| 1045 | if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) == |
| 1046 | (DemandedMask & (~RHSKnownZero))) |
| 1047 | return UpdateValueUsesWith(I, I->getOperand(0)); |
| 1048 | if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) == |
| 1049 | (DemandedMask & (~LHSKnownZero))) |
| 1050 | return UpdateValueUsesWith(I, I->getOperand(1)); |
| 1051 | |
| 1052 | // If the RHS is a constant, see if we can simplify it. |
| 1053 | if (ShrinkDemandedConstant(I, 1, DemandedMask)) |
| 1054 | return UpdateValueUsesWith(I, I); |
| 1055 | |
| 1056 | // Output known-0 bits are only known if clear in both the LHS & RHS. |
| 1057 | RHSKnownZero &= LHSKnownZero; |
| 1058 | // Output known-1 are known to be set if set in either the LHS | RHS. |
| 1059 | RHSKnownOne |= LHSKnownOne; |
| 1060 | break; |
| 1061 | case Instruction::Xor: { |
| 1062 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, |
| 1063 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 1064 | return true; |
| 1065 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1066 | "Bits known to be one AND zero?"); |
| 1067 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, |
| 1068 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1069 | return true; |
| 1070 | assert((LHSKnownZero & LHSKnownOne) == 0 && |
| 1071 | "Bits known to be one AND zero?"); |
| 1072 | |
| 1073 | // If all of the demanded bits are known zero on one side, return the other. |
| 1074 | // These bits cannot contribute to the result of the 'xor'. |
| 1075 | if ((DemandedMask & RHSKnownZero) == DemandedMask) |
| 1076 | return UpdateValueUsesWith(I, I->getOperand(0)); |
| 1077 | if ((DemandedMask & LHSKnownZero) == DemandedMask) |
| 1078 | return UpdateValueUsesWith(I, I->getOperand(1)); |
| 1079 | |
| 1080 | // Output known-0 bits are known if clear or set in both the LHS & RHS. |
| 1081 | APInt KnownZeroOut = (RHSKnownZero & LHSKnownZero) | |
| 1082 | (RHSKnownOne & LHSKnownOne); |
| 1083 | // Output known-1 are known to be set if set in only one of the LHS, RHS. |
| 1084 | APInt KnownOneOut = (RHSKnownZero & LHSKnownOne) | |
| 1085 | (RHSKnownOne & LHSKnownZero); |
| 1086 | |
| 1087 | // If all of the demanded bits are known to be zero on one side or the |
| 1088 | // other, turn this into an *inclusive* or. |
| 1089 | // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0 |
| 1090 | if ((DemandedMask & ~RHSKnownZero & ~LHSKnownZero) == 0) { |
| 1091 | Instruction *Or = |
| 1092 | BinaryOperator::createOr(I->getOperand(0), I->getOperand(1), |
| 1093 | I->getName()); |
| 1094 | InsertNewInstBefore(Or, *I); |
| 1095 | return UpdateValueUsesWith(I, Or); |
| 1096 | } |
| 1097 | |
| 1098 | // If all of the demanded bits on one side are known, and all of the set |
| 1099 | // bits on that side are also known to be set on the other side, turn this |
| 1100 | // into an AND, as we know the bits will be cleared. |
| 1101 | // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2 |
| 1102 | if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) { |
| 1103 | // all known |
| 1104 | if ((RHSKnownOne & LHSKnownOne) == RHSKnownOne) { |
| 1105 | Constant *AndC = ConstantInt::get(~RHSKnownOne & DemandedMask); |
| 1106 | Instruction *And = |
| 1107 | BinaryOperator::createAnd(I->getOperand(0), AndC, "tmp"); |
| 1108 | InsertNewInstBefore(And, *I); |
| 1109 | return UpdateValueUsesWith(I, And); |
| 1110 | } |
| 1111 | } |
| 1112 | |
| 1113 | // If the RHS is a constant, see if we can simplify it. |
| 1114 | // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1. |
| 1115 | if (ShrinkDemandedConstant(I, 1, DemandedMask)) |
| 1116 | return UpdateValueUsesWith(I, I); |
| 1117 | |
| 1118 | RHSKnownZero = KnownZeroOut; |
| 1119 | RHSKnownOne = KnownOneOut; |
| 1120 | break; |
| 1121 | } |
| 1122 | case Instruction::Select: |
| 1123 | if (SimplifyDemandedBits(I->getOperand(2), DemandedMask, |
| 1124 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 1125 | return true; |
| 1126 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, |
| 1127 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1128 | return true; |
| 1129 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1130 | "Bits known to be one AND zero?"); |
| 1131 | assert((LHSKnownZero & LHSKnownOne) == 0 && |
| 1132 | "Bits known to be one AND zero?"); |
| 1133 | |
| 1134 | // If the operands are constants, see if we can simplify them. |
| 1135 | if (ShrinkDemandedConstant(I, 1, DemandedMask)) |
| 1136 | return UpdateValueUsesWith(I, I); |
| 1137 | if (ShrinkDemandedConstant(I, 2, DemandedMask)) |
| 1138 | return UpdateValueUsesWith(I, I); |
| 1139 | |
| 1140 | // Only known if known in both the LHS and RHS. |
| 1141 | RHSKnownOne &= LHSKnownOne; |
| 1142 | RHSKnownZero &= LHSKnownZero; |
| 1143 | break; |
| 1144 | case Instruction::Trunc: { |
| 1145 | uint32_t truncBf = |
| 1146 | cast<IntegerType>(I->getOperand(0)->getType())->getBitWidth(); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1147 | DemandedMask.zext(truncBf); |
| 1148 | RHSKnownZero.zext(truncBf); |
| 1149 | RHSKnownOne.zext(truncBf); |
| 1150 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, |
| 1151 | RHSKnownZero, RHSKnownOne, Depth+1)) |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1152 | return true; |
| 1153 | DemandedMask.trunc(BitWidth); |
| 1154 | RHSKnownZero.trunc(BitWidth); |
| 1155 | RHSKnownOne.trunc(BitWidth); |
| 1156 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1157 | "Bits known to be one AND zero?"); |
| 1158 | break; |
| 1159 | } |
| 1160 | case Instruction::BitCast: |
| 1161 | if (!I->getOperand(0)->getType()->isInteger()) |
| 1162 | return false; |
| 1163 | |
| 1164 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, |
| 1165 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 1166 | return true; |
| 1167 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1168 | "Bits known to be one AND zero?"); |
| 1169 | break; |
| 1170 | case Instruction::ZExt: { |
| 1171 | // Compute the bits in the result that are not present in the input. |
| 1172 | const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType()); |
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 1173 | uint32_t SrcBitWidth = SrcTy->getBitWidth(); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1174 | |
Zhou Sheng | d48653a | 2007-03-29 04:45:55 +0000 | [diff] [blame] | 1175 | DemandedMask.trunc(SrcBitWidth); |
| 1176 | RHSKnownZero.trunc(SrcBitWidth); |
| 1177 | RHSKnownOne.trunc(SrcBitWidth); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1178 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, |
| 1179 | RHSKnownZero, RHSKnownOne, Depth+1)) |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1180 | return true; |
| 1181 | DemandedMask.zext(BitWidth); |
| 1182 | RHSKnownZero.zext(BitWidth); |
| 1183 | RHSKnownOne.zext(BitWidth); |
| 1184 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1185 | "Bits known to be one AND zero?"); |
| 1186 | // The top bits are known to be zero. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1187 | RHSKnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1188 | break; |
| 1189 | } |
| 1190 | case Instruction::SExt: { |
| 1191 | // Compute the bits in the result that are not present in the input. |
| 1192 | const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType()); |
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 1193 | uint32_t SrcBitWidth = SrcTy->getBitWidth(); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1194 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1195 | APInt InputDemandedBits = DemandedMask & |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1196 | APInt::getLowBitsSet(BitWidth, SrcBitWidth); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1197 | |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1198 | APInt NewBits(APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1199 | // If any of the sign extended bits are demanded, we know that the sign |
| 1200 | // bit is demanded. |
| 1201 | if ((NewBits & DemandedMask) != 0) |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 1202 | InputDemandedBits.set(SrcBitWidth-1); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1203 | |
Zhou Sheng | d48653a | 2007-03-29 04:45:55 +0000 | [diff] [blame] | 1204 | InputDemandedBits.trunc(SrcBitWidth); |
| 1205 | RHSKnownZero.trunc(SrcBitWidth); |
| 1206 | RHSKnownOne.trunc(SrcBitWidth); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1207 | if (SimplifyDemandedBits(I->getOperand(0), InputDemandedBits, |
| 1208 | RHSKnownZero, RHSKnownOne, Depth+1)) |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1209 | return true; |
| 1210 | InputDemandedBits.zext(BitWidth); |
| 1211 | RHSKnownZero.zext(BitWidth); |
| 1212 | RHSKnownOne.zext(BitWidth); |
| 1213 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1214 | "Bits known to be one AND zero?"); |
| 1215 | |
| 1216 | // If the sign bit of the input is known set or clear, then we know the |
| 1217 | // top bits of the result. |
| 1218 | |
| 1219 | // If the input sign bit is known zero, or if the NewBits are not demanded |
| 1220 | // convert this into a zero extension. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1221 | if (RHSKnownZero[SrcBitWidth-1] || (NewBits & ~DemandedMask) == NewBits) |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1222 | { |
| 1223 | // Convert to ZExt cast |
| 1224 | CastInst *NewCast = new ZExtInst(I->getOperand(0), VTy, I->getName(), I); |
| 1225 | return UpdateValueUsesWith(I, NewCast); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1226 | } else if (RHSKnownOne[SrcBitWidth-1]) { // Input sign bit known set |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1227 | RHSKnownOne |= NewBits; |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1228 | } |
| 1229 | break; |
| 1230 | } |
| 1231 | case Instruction::Add: { |
| 1232 | // Figure out what the input bits are. If the top bits of the and result |
| 1233 | // are not demanded, then the add doesn't demand them from its input |
| 1234 | // either. |
Reid Spencer | 55702aa | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 1235 | uint32_t NLZ = DemandedMask.countLeadingZeros(); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1236 | |
| 1237 | // If there is a constant on the RHS, there are a variety of xformations |
| 1238 | // we can do. |
| 1239 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 1240 | // If null, this should be simplified elsewhere. Some of the xforms here |
| 1241 | // won't work if the RHS is zero. |
| 1242 | if (RHS->isZero()) |
| 1243 | break; |
| 1244 | |
| 1245 | // If the top bit of the output is demanded, demand everything from the |
| 1246 | // input. Otherwise, we demand all the input bits except NLZ top bits. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1247 | APInt InDemandedBits(APInt::getLowBitsSet(BitWidth, BitWidth - NLZ)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1248 | |
| 1249 | // Find information about known zero/one bits in the input. |
| 1250 | if (SimplifyDemandedBits(I->getOperand(0), InDemandedBits, |
| 1251 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1252 | return true; |
| 1253 | |
| 1254 | // If the RHS of the add has bits set that can't affect the input, reduce |
| 1255 | // the constant. |
| 1256 | if (ShrinkDemandedConstant(I, 1, InDemandedBits)) |
| 1257 | return UpdateValueUsesWith(I, I); |
| 1258 | |
| 1259 | // Avoid excess work. |
| 1260 | if (LHSKnownZero == 0 && LHSKnownOne == 0) |
| 1261 | break; |
| 1262 | |
| 1263 | // Turn it into OR if input bits are zero. |
| 1264 | if ((LHSKnownZero & RHS->getValue()) == RHS->getValue()) { |
| 1265 | Instruction *Or = |
| 1266 | BinaryOperator::createOr(I->getOperand(0), I->getOperand(1), |
| 1267 | I->getName()); |
| 1268 | InsertNewInstBefore(Or, *I); |
| 1269 | return UpdateValueUsesWith(I, Or); |
| 1270 | } |
| 1271 | |
| 1272 | // We can say something about the output known-zero and known-one bits, |
| 1273 | // depending on potential carries from the input constant and the |
| 1274 | // unknowns. For example if the LHS is known to have at most the 0x0F0F0 |
| 1275 | // bits set and the RHS constant is 0x01001, then we know we have a known |
| 1276 | // one mask of 0x00001 and a known zero mask of 0xE0F0E. |
| 1277 | |
| 1278 | // To compute this, we first compute the potential carry bits. These are |
| 1279 | // the bits which may be modified. I'm not aware of a better way to do |
| 1280 | // this scan. |
Zhou Sheng | b9cb95f | 2007-03-31 02:38:39 +0000 | [diff] [blame] | 1281 | const APInt& RHSVal = RHS->getValue(); |
| 1282 | APInt CarryBits((~LHSKnownZero + RHSVal) ^ (~LHSKnownZero ^ RHSVal)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1283 | |
| 1284 | // Now that we know which bits have carries, compute the known-1/0 sets. |
| 1285 | |
| 1286 | // Bits are known one if they are known zero in one operand and one in the |
| 1287 | // other, and there is no input carry. |
| 1288 | RHSKnownOne = ((LHSKnownZero & RHSVal) | |
| 1289 | (LHSKnownOne & ~RHSVal)) & ~CarryBits; |
| 1290 | |
| 1291 | // Bits are known zero if they are known zero in both operands and there |
| 1292 | // is no input carry. |
| 1293 | RHSKnownZero = LHSKnownZero & ~RHSVal & ~CarryBits; |
| 1294 | } else { |
| 1295 | // If the high-bits of this ADD are not demanded, then it does not demand |
| 1296 | // the high bits of its LHS or RHS. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1297 | if (DemandedMask[BitWidth-1] == 0) { |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1298 | // Right fill the mask of bits for this ADD to demand the most |
| 1299 | // significant bit and all those below it. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1300 | APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1301 | if (SimplifyDemandedBits(I->getOperand(0), DemandedFromOps, |
| 1302 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1303 | return true; |
| 1304 | if (SimplifyDemandedBits(I->getOperand(1), DemandedFromOps, |
| 1305 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1306 | return true; |
| 1307 | } |
| 1308 | } |
| 1309 | break; |
| 1310 | } |
| 1311 | case Instruction::Sub: |
| 1312 | // If the high-bits of this SUB are not demanded, then it does not demand |
| 1313 | // the high bits of its LHS or RHS. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1314 | if (DemandedMask[BitWidth-1] == 0) { |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1315 | // Right fill the mask of bits for this SUB to demand the most |
| 1316 | // significant bit and all those below it. |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 1317 | uint32_t NLZ = DemandedMask.countLeadingZeros(); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1318 | APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1319 | if (SimplifyDemandedBits(I->getOperand(0), DemandedFromOps, |
| 1320 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1321 | return true; |
| 1322 | if (SimplifyDemandedBits(I->getOperand(1), DemandedFromOps, |
| 1323 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1324 | return true; |
| 1325 | } |
| 1326 | break; |
| 1327 | case Instruction::Shl: |
| 1328 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 1329 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1330 | APInt DemandedMaskIn(DemandedMask.lshr(ShiftAmt)); |
| 1331 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMaskIn, |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1332 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 1333 | return true; |
| 1334 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1335 | "Bits known to be one AND zero?"); |
| 1336 | RHSKnownZero <<= ShiftAmt; |
| 1337 | RHSKnownOne <<= ShiftAmt; |
| 1338 | // low bits known zero. |
Zhou Sheng | adc1495 | 2007-03-14 09:07:33 +0000 | [diff] [blame] | 1339 | if (ShiftAmt) |
Zhou Sheng | e9e03f6 | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 1340 | RHSKnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1341 | } |
| 1342 | break; |
| 1343 | case Instruction::LShr: |
| 1344 | // For a logical shift right |
| 1345 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 1346 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1347 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1348 | // Unsigned shift right. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1349 | APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt)); |
| 1350 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMaskIn, |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1351 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 1352 | return true; |
| 1353 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1354 | "Bits known to be one AND zero?"); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1355 | RHSKnownZero = APIntOps::lshr(RHSKnownZero, ShiftAmt); |
| 1356 | RHSKnownOne = APIntOps::lshr(RHSKnownOne, ShiftAmt); |
Zhou Sheng | adc1495 | 2007-03-14 09:07:33 +0000 | [diff] [blame] | 1357 | if (ShiftAmt) { |
| 1358 | // Compute the new bits that are at the top now. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1359 | APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt)); |
Zhou Sheng | adc1495 | 2007-03-14 09:07:33 +0000 | [diff] [blame] | 1360 | RHSKnownZero |= HighBits; // high bits known zero. |
| 1361 | } |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1362 | } |
| 1363 | break; |
| 1364 | case Instruction::AShr: |
| 1365 | // If this is an arithmetic shift right and only the low-bit is set, we can |
| 1366 | // always convert this into a logical shr, even if the shift amount is |
| 1367 | // variable. The low bit of the shift cannot be an input sign bit unless |
| 1368 | // the shift amount is >= the size of the datatype, which is undefined. |
| 1369 | if (DemandedMask == 1) { |
| 1370 | // Perform the logical shift right. |
| 1371 | Value *NewVal = BinaryOperator::createLShr( |
| 1372 | I->getOperand(0), I->getOperand(1), I->getName()); |
| 1373 | InsertNewInstBefore(cast<Instruction>(NewVal), *I); |
| 1374 | return UpdateValueUsesWith(I, NewVal); |
| 1375 | } |
Chris Lattner | 4241e4d | 2007-07-15 20:54:51 +0000 | [diff] [blame] | 1376 | |
| 1377 | // If the sign bit is the only bit demanded by this ashr, then there is no |
| 1378 | // need to do it, the shift doesn't change the high bit. |
| 1379 | if (DemandedMask.isSignBit()) |
| 1380 | return UpdateValueUsesWith(I, I->getOperand(0)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1381 | |
| 1382 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 1383 | uint32_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1384 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1385 | // Signed shift right. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1386 | APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt)); |
Lauro Ramos Venancio | d0499af | 2007-06-06 17:08:48 +0000 | [diff] [blame] | 1387 | // If any of the "high bits" are demanded, we should set the sign bit as |
| 1388 | // demanded. |
| 1389 | if (DemandedMask.countLeadingZeros() <= ShiftAmt) |
| 1390 | DemandedMaskIn.set(BitWidth-1); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1391 | if (SimplifyDemandedBits(I->getOperand(0), |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1392 | DemandedMaskIn, |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1393 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 1394 | return true; |
| 1395 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1396 | "Bits known to be one AND zero?"); |
| 1397 | // Compute the new bits that are at the top now. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1398 | APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1399 | RHSKnownZero = APIntOps::lshr(RHSKnownZero, ShiftAmt); |
| 1400 | RHSKnownOne = APIntOps::lshr(RHSKnownOne, ShiftAmt); |
| 1401 | |
| 1402 | // Handle the sign bits. |
| 1403 | APInt SignBit(APInt::getSignBit(BitWidth)); |
| 1404 | // Adjust to where it is now in the mask. |
| 1405 | SignBit = APIntOps::lshr(SignBit, ShiftAmt); |
| 1406 | |
| 1407 | // If the input sign bit is known to be zero, or if none of the top bits |
| 1408 | // are demanded, turn this into an unsigned shift right. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1409 | if (RHSKnownZero[BitWidth-ShiftAmt-1] || |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1410 | (HighBits & ~DemandedMask) == HighBits) { |
| 1411 | // Perform the logical shift right. |
| 1412 | Value *NewVal = BinaryOperator::createLShr( |
| 1413 | I->getOperand(0), SA, I->getName()); |
| 1414 | InsertNewInstBefore(cast<Instruction>(NewVal), *I); |
| 1415 | return UpdateValueUsesWith(I, NewVal); |
| 1416 | } else if ((RHSKnownOne & SignBit) != 0) { // New bits are known one. |
| 1417 | RHSKnownOne |= HighBits; |
| 1418 | } |
| 1419 | } |
| 1420 | break; |
| 1421 | } |
| 1422 | |
| 1423 | // If the client is only demanding bits that we know, return the known |
| 1424 | // constant. |
| 1425 | if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) |
| 1426 | return UpdateValueUsesWith(I, ConstantInt::get(RHSKnownOne)); |
| 1427 | return false; |
| 1428 | } |
| 1429 | |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1430 | |
| 1431 | /// SimplifyDemandedVectorElts - The specified value producecs a vector with |
| 1432 | /// 64 or fewer elements. DemandedElts contains the set of elements that are |
| 1433 | /// actually used by the caller. This method analyzes which elements of the |
| 1434 | /// operand are undef and returns that information in UndefElts. |
| 1435 | /// |
| 1436 | /// If the information about demanded elements can be used to simplify the |
| 1437 | /// operation, the operation is simplified, then the resultant value is |
| 1438 | /// returned. This returns null if no change was made. |
| 1439 | Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, uint64_t DemandedElts, |
| 1440 | uint64_t &UndefElts, |
| 1441 | unsigned Depth) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1442 | unsigned VWidth = cast<VectorType>(V->getType())->getNumElements(); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1443 | assert(VWidth <= 64 && "Vector too wide to analyze!"); |
| 1444 | uint64_t EltMask = ~0ULL >> (64-VWidth); |
| 1445 | assert(DemandedElts != EltMask && (DemandedElts & ~EltMask) == 0 && |
| 1446 | "Invalid DemandedElts!"); |
| 1447 | |
| 1448 | if (isa<UndefValue>(V)) { |
| 1449 | // If the entire vector is undefined, just return this info. |
| 1450 | UndefElts = EltMask; |
| 1451 | return 0; |
| 1452 | } else if (DemandedElts == 0) { // If nothing is demanded, provide undef. |
| 1453 | UndefElts = EltMask; |
| 1454 | return UndefValue::get(V->getType()); |
| 1455 | } |
| 1456 | |
| 1457 | UndefElts = 0; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1458 | if (ConstantVector *CP = dyn_cast<ConstantVector>(V)) { |
| 1459 | const Type *EltTy = cast<VectorType>(V->getType())->getElementType(); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1460 | Constant *Undef = UndefValue::get(EltTy); |
| 1461 | |
| 1462 | std::vector<Constant*> Elts; |
| 1463 | for (unsigned i = 0; i != VWidth; ++i) |
| 1464 | if (!(DemandedElts & (1ULL << i))) { // If not demanded, set to undef. |
| 1465 | Elts.push_back(Undef); |
| 1466 | UndefElts |= (1ULL << i); |
| 1467 | } else if (isa<UndefValue>(CP->getOperand(i))) { // Already undef. |
| 1468 | Elts.push_back(Undef); |
| 1469 | UndefElts |= (1ULL << i); |
| 1470 | } else { // Otherwise, defined. |
| 1471 | Elts.push_back(CP->getOperand(i)); |
| 1472 | } |
| 1473 | |
| 1474 | // If we changed the constant, return it. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1475 | Constant *NewCP = ConstantVector::get(Elts); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1476 | return NewCP != CP ? NewCP : 0; |
| 1477 | } else if (isa<ConstantAggregateZero>(V)) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1478 | // Simplify the CAZ to a ConstantVector where the non-demanded elements are |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1479 | // set to undef. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1480 | const Type *EltTy = cast<VectorType>(V->getType())->getElementType(); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1481 | Constant *Zero = Constant::getNullValue(EltTy); |
| 1482 | Constant *Undef = UndefValue::get(EltTy); |
| 1483 | std::vector<Constant*> Elts; |
| 1484 | for (unsigned i = 0; i != VWidth; ++i) |
| 1485 | Elts.push_back((DemandedElts & (1ULL << i)) ? Zero : Undef); |
| 1486 | UndefElts = DemandedElts ^ EltMask; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1487 | return ConstantVector::get(Elts); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1488 | } |
| 1489 | |
| 1490 | if (!V->hasOneUse()) { // Other users may use these bits. |
| 1491 | if (Depth != 0) { // Not at the root. |
| 1492 | // TODO: Just compute the UndefElts information recursively. |
| 1493 | return false; |
| 1494 | } |
| 1495 | return false; |
| 1496 | } else if (Depth == 10) { // Limit search depth. |
| 1497 | return false; |
| 1498 | } |
| 1499 | |
| 1500 | Instruction *I = dyn_cast<Instruction>(V); |
| 1501 | if (!I) return false; // Only analyze instructions. |
| 1502 | |
| 1503 | bool MadeChange = false; |
| 1504 | uint64_t UndefElts2; |
| 1505 | Value *TmpV; |
| 1506 | switch (I->getOpcode()) { |
| 1507 | default: break; |
| 1508 | |
| 1509 | case Instruction::InsertElement: { |
| 1510 | // If this is a variable index, we don't know which element it overwrites. |
| 1511 | // demand exactly the same input as we produce. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1512 | ConstantInt *Idx = dyn_cast<ConstantInt>(I->getOperand(2)); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1513 | if (Idx == 0) { |
| 1514 | // Note that we can't propagate undef elt info, because we don't know |
| 1515 | // which elt is getting updated. |
| 1516 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, |
| 1517 | UndefElts2, Depth+1); |
| 1518 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } |
| 1519 | break; |
| 1520 | } |
| 1521 | |
| 1522 | // If this is inserting an element that isn't demanded, remove this |
| 1523 | // insertelement. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1524 | unsigned IdxNo = Idx->getZExtValue(); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1525 | if (IdxNo >= VWidth || (DemandedElts & (1ULL << IdxNo)) == 0) |
| 1526 | return AddSoonDeadInstToWorklist(*I, 0); |
| 1527 | |
| 1528 | // Otherwise, the element inserted overwrites whatever was there, so the |
| 1529 | // input demanded set is simpler than the output set. |
| 1530 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), |
| 1531 | DemandedElts & ~(1ULL << IdxNo), |
| 1532 | UndefElts, Depth+1); |
| 1533 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } |
| 1534 | |
| 1535 | // The inserted element is defined. |
| 1536 | UndefElts |= 1ULL << IdxNo; |
| 1537 | break; |
| 1538 | } |
Chris Lattner | 6987833 | 2007-04-14 22:29:23 +0000 | [diff] [blame] | 1539 | case Instruction::BitCast: { |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 1540 | // Vector->vector casts only. |
Chris Lattner | 6987833 | 2007-04-14 22:29:23 +0000 | [diff] [blame] | 1541 | const VectorType *VTy = dyn_cast<VectorType>(I->getOperand(0)->getType()); |
| 1542 | if (!VTy) break; |
| 1543 | unsigned InVWidth = VTy->getNumElements(); |
| 1544 | uint64_t InputDemandedElts = 0; |
| 1545 | unsigned Ratio; |
| 1546 | |
| 1547 | if (VWidth == InVWidth) { |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 1548 | // 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] | 1549 | // elements as are demanded of us. |
| 1550 | Ratio = 1; |
| 1551 | InputDemandedElts = DemandedElts; |
| 1552 | } else if (VWidth > InVWidth) { |
| 1553 | // Untested so far. |
| 1554 | break; |
| 1555 | |
| 1556 | // If there are more elements in the result than there are in the source, |
| 1557 | // then an input element is live if any of the corresponding output |
| 1558 | // elements are live. |
| 1559 | Ratio = VWidth/InVWidth; |
| 1560 | for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) { |
| 1561 | if (DemandedElts & (1ULL << OutIdx)) |
| 1562 | InputDemandedElts |= 1ULL << (OutIdx/Ratio); |
| 1563 | } |
| 1564 | } else { |
| 1565 | // Untested so far. |
| 1566 | break; |
| 1567 | |
| 1568 | // If there are more elements in the source than there are in the result, |
| 1569 | // then an input element is live if the corresponding output element is |
| 1570 | // live. |
| 1571 | Ratio = InVWidth/VWidth; |
| 1572 | for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx) |
| 1573 | if (DemandedElts & (1ULL << InIdx/Ratio)) |
| 1574 | InputDemandedElts |= 1ULL << InIdx; |
| 1575 | } |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1576 | |
Chris Lattner | 6987833 | 2007-04-14 22:29:23 +0000 | [diff] [blame] | 1577 | // div/rem demand all inputs, because they don't want divide by zero. |
| 1578 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), InputDemandedElts, |
| 1579 | UndefElts2, Depth+1); |
| 1580 | if (TmpV) { |
| 1581 | I->setOperand(0, TmpV); |
| 1582 | MadeChange = true; |
| 1583 | } |
| 1584 | |
| 1585 | UndefElts = UndefElts2; |
| 1586 | if (VWidth > InVWidth) { |
| 1587 | assert(0 && "Unimp"); |
| 1588 | // If there are more elements in the result than there are in the source, |
| 1589 | // then an output element is undef if the corresponding input element is |
| 1590 | // undef. |
| 1591 | for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) |
| 1592 | if (UndefElts2 & (1ULL << (OutIdx/Ratio))) |
| 1593 | UndefElts |= 1ULL << OutIdx; |
| 1594 | } else if (VWidth < InVWidth) { |
| 1595 | assert(0 && "Unimp"); |
| 1596 | // If there are more elements in the source than there are in the result, |
| 1597 | // then a result element is undef if all of the corresponding input |
| 1598 | // elements are undef. |
| 1599 | UndefElts = ~0ULL >> (64-VWidth); // Start out all undef. |
| 1600 | for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx) |
| 1601 | if ((UndefElts2 & (1ULL << InIdx)) == 0) // Not undef? |
| 1602 | UndefElts &= ~(1ULL << (InIdx/Ratio)); // Clear undef bit. |
| 1603 | } |
| 1604 | break; |
| 1605 | } |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1606 | case Instruction::And: |
| 1607 | case Instruction::Or: |
| 1608 | case Instruction::Xor: |
| 1609 | case Instruction::Add: |
| 1610 | case Instruction::Sub: |
| 1611 | case Instruction::Mul: |
| 1612 | // div/rem demand all inputs, because they don't want divide by zero. |
| 1613 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, |
| 1614 | UndefElts, Depth+1); |
| 1615 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } |
| 1616 | TmpV = SimplifyDemandedVectorElts(I->getOperand(1), DemandedElts, |
| 1617 | UndefElts2, Depth+1); |
| 1618 | if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; } |
| 1619 | |
| 1620 | // Output elements are undefined if both are undefined. Consider things |
| 1621 | // like undef&0. The result is known zero, not undef. |
| 1622 | UndefElts &= UndefElts2; |
| 1623 | break; |
| 1624 | |
| 1625 | case Instruction::Call: { |
| 1626 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(I); |
| 1627 | if (!II) break; |
| 1628 | switch (II->getIntrinsicID()) { |
| 1629 | default: break; |
| 1630 | |
| 1631 | // Binary vector operations that work column-wise. A dest element is a |
| 1632 | // function of the corresponding input elements from the two inputs. |
| 1633 | case Intrinsic::x86_sse_sub_ss: |
| 1634 | case Intrinsic::x86_sse_mul_ss: |
| 1635 | case Intrinsic::x86_sse_min_ss: |
| 1636 | case Intrinsic::x86_sse_max_ss: |
| 1637 | case Intrinsic::x86_sse2_sub_sd: |
| 1638 | case Intrinsic::x86_sse2_mul_sd: |
| 1639 | case Intrinsic::x86_sse2_min_sd: |
| 1640 | case Intrinsic::x86_sse2_max_sd: |
| 1641 | TmpV = SimplifyDemandedVectorElts(II->getOperand(1), DemandedElts, |
| 1642 | UndefElts, Depth+1); |
| 1643 | if (TmpV) { II->setOperand(1, TmpV); MadeChange = true; } |
| 1644 | TmpV = SimplifyDemandedVectorElts(II->getOperand(2), DemandedElts, |
| 1645 | UndefElts2, Depth+1); |
| 1646 | if (TmpV) { II->setOperand(2, TmpV); MadeChange = true; } |
| 1647 | |
| 1648 | // If only the low elt is demanded and this is a scalarizable intrinsic, |
| 1649 | // scalarize it now. |
| 1650 | if (DemandedElts == 1) { |
| 1651 | switch (II->getIntrinsicID()) { |
| 1652 | default: break; |
| 1653 | case Intrinsic::x86_sse_sub_ss: |
| 1654 | case Intrinsic::x86_sse_mul_ss: |
| 1655 | case Intrinsic::x86_sse2_sub_sd: |
| 1656 | case Intrinsic::x86_sse2_mul_sd: |
| 1657 | // TODO: Lower MIN/MAX/ABS/etc |
| 1658 | Value *LHS = II->getOperand(1); |
| 1659 | Value *RHS = II->getOperand(2); |
| 1660 | // Extract the element as scalars. |
| 1661 | LHS = InsertNewInstBefore(new ExtractElementInst(LHS, 0U,"tmp"), *II); |
| 1662 | RHS = InsertNewInstBefore(new ExtractElementInst(RHS, 0U,"tmp"), *II); |
| 1663 | |
| 1664 | switch (II->getIntrinsicID()) { |
| 1665 | default: assert(0 && "Case stmts out of sync!"); |
| 1666 | case Intrinsic::x86_sse_sub_ss: |
| 1667 | case Intrinsic::x86_sse2_sub_sd: |
| 1668 | TmpV = InsertNewInstBefore(BinaryOperator::createSub(LHS, RHS, |
| 1669 | II->getName()), *II); |
| 1670 | break; |
| 1671 | case Intrinsic::x86_sse_mul_ss: |
| 1672 | case Intrinsic::x86_sse2_mul_sd: |
| 1673 | TmpV = InsertNewInstBefore(BinaryOperator::createMul(LHS, RHS, |
| 1674 | II->getName()), *II); |
| 1675 | break; |
| 1676 | } |
| 1677 | |
| 1678 | Instruction *New = |
| 1679 | new InsertElementInst(UndefValue::get(II->getType()), TmpV, 0U, |
| 1680 | II->getName()); |
| 1681 | InsertNewInstBefore(New, *II); |
| 1682 | AddSoonDeadInstToWorklist(*II, 0); |
| 1683 | return New; |
| 1684 | } |
| 1685 | } |
| 1686 | |
| 1687 | // Output elements are undefined if both are undefined. Consider things |
| 1688 | // like undef&0. The result is known zero, not undef. |
| 1689 | UndefElts &= UndefElts2; |
| 1690 | break; |
| 1691 | } |
| 1692 | break; |
| 1693 | } |
| 1694 | } |
| 1695 | return MadeChange ? I : 0; |
| 1696 | } |
| 1697 | |
Nick Lewycky | 455e176 | 2007-09-06 02:40:25 +0000 | [diff] [blame] | 1698 | /// @returns true if the specified compare predicate is |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1699 | /// true when both operands are equal... |
Nick Lewycky | 455e176 | 2007-09-06 02:40:25 +0000 | [diff] [blame] | 1700 | /// @brief Determine if the icmp Predicate is true when both operands are equal |
| 1701 | static bool isTrueWhenEqual(ICmpInst::Predicate pred) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1702 | return pred == ICmpInst::ICMP_EQ || pred == ICmpInst::ICMP_UGE || |
| 1703 | pred == ICmpInst::ICMP_SGE || pred == ICmpInst::ICMP_ULE || |
| 1704 | pred == ICmpInst::ICMP_SLE; |
| 1705 | } |
| 1706 | |
Nick Lewycky | 455e176 | 2007-09-06 02:40:25 +0000 | [diff] [blame] | 1707 | /// @returns true if the specified compare instruction is |
| 1708 | /// true when both operands are equal... |
| 1709 | /// @brief Determine if the ICmpInst returns true when both operands are equal |
| 1710 | static bool isTrueWhenEqual(ICmpInst &ICI) { |
| 1711 | return isTrueWhenEqual(ICI.getPredicate()); |
| 1712 | } |
| 1713 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1714 | /// AssociativeOpt - Perform an optimization on an associative operator. This |
| 1715 | /// function is designed to check a chain of associative operators for a |
| 1716 | /// potential to apply a certain optimization. Since the optimization may be |
| 1717 | /// applicable if the expression was reassociated, this checks the chain, then |
| 1718 | /// reassociates the expression as necessary to expose the optimization |
| 1719 | /// opportunity. This makes use of a special Functor, which must define |
| 1720 | /// 'shouldApply' and 'apply' methods. |
| 1721 | /// |
| 1722 | template<typename Functor> |
| 1723 | Instruction *AssociativeOpt(BinaryOperator &Root, const Functor &F) { |
| 1724 | unsigned Opcode = Root.getOpcode(); |
| 1725 | Value *LHS = Root.getOperand(0); |
| 1726 | |
| 1727 | // Quick check, see if the immediate LHS matches... |
| 1728 | if (F.shouldApply(LHS)) |
| 1729 | return F.apply(Root); |
| 1730 | |
| 1731 | // Otherwise, if the LHS is not of the same opcode as the root, return. |
| 1732 | Instruction *LHSI = dyn_cast<Instruction>(LHS); |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 1733 | while (LHSI && LHSI->getOpcode() == Opcode && LHSI->hasOneUse()) { |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1734 | // Should we apply this transform to the RHS? |
| 1735 | bool ShouldApply = F.shouldApply(LHSI->getOperand(1)); |
| 1736 | |
| 1737 | // If not to the RHS, check to see if we should apply to the LHS... |
| 1738 | if (!ShouldApply && F.shouldApply(LHSI->getOperand(0))) { |
| 1739 | cast<BinaryOperator>(LHSI)->swapOperands(); // Make the LHS the RHS |
| 1740 | ShouldApply = true; |
| 1741 | } |
| 1742 | |
| 1743 | // If the functor wants to apply the optimization to the RHS of LHSI, |
| 1744 | // reassociate the expression from ((? op A) op B) to (? op (A op B)) |
| 1745 | if (ShouldApply) { |
| 1746 | BasicBlock *BB = Root.getParent(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1747 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1748 | // Now all of the instructions are in the current basic block, go ahead |
| 1749 | // and perform the reassociation. |
| 1750 | Instruction *TmpLHSI = cast<Instruction>(Root.getOperand(0)); |
| 1751 | |
| 1752 | // First move the selected RHS to the LHS of the root... |
| 1753 | Root.setOperand(0, LHSI->getOperand(1)); |
| 1754 | |
| 1755 | // Make what used to be the LHS of the root be the user of the root... |
| 1756 | Value *ExtraOperand = TmpLHSI->getOperand(1); |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1757 | if (&Root == TmpLHSI) { |
Chris Lattner | 15a76c0 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 1758 | Root.replaceAllUsesWith(Constant::getNullValue(TmpLHSI->getType())); |
| 1759 | return 0; |
| 1760 | } |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1761 | Root.replaceAllUsesWith(TmpLHSI); // Users now use TmpLHSI |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1762 | TmpLHSI->setOperand(1, &Root); // TmpLHSI now uses the root |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1763 | TmpLHSI->getParent()->getInstList().remove(TmpLHSI); |
| 1764 | BasicBlock::iterator ARI = &Root; ++ARI; |
| 1765 | BB->getInstList().insert(ARI, TmpLHSI); // Move TmpLHSI to after Root |
| 1766 | ARI = Root; |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1767 | |
| 1768 | // Now propagate the ExtraOperand down the chain of instructions until we |
| 1769 | // get to LHSI. |
| 1770 | while (TmpLHSI != LHSI) { |
| 1771 | Instruction *NextLHSI = cast<Instruction>(TmpLHSI->getOperand(0)); |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1772 | // Move the instruction to immediately before the chain we are |
| 1773 | // constructing to avoid breaking dominance properties. |
| 1774 | NextLHSI->getParent()->getInstList().remove(NextLHSI); |
| 1775 | BB->getInstList().insert(ARI, NextLHSI); |
| 1776 | ARI = NextLHSI; |
| 1777 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1778 | Value *NextOp = NextLHSI->getOperand(1); |
| 1779 | NextLHSI->setOperand(1, ExtraOperand); |
| 1780 | TmpLHSI = NextLHSI; |
| 1781 | ExtraOperand = NextOp; |
| 1782 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1783 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1784 | // Now that the instructions are reassociated, have the functor perform |
| 1785 | // the transformation... |
| 1786 | return F.apply(Root); |
| 1787 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1788 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1789 | LHSI = dyn_cast<Instruction>(LHSI->getOperand(0)); |
| 1790 | } |
| 1791 | return 0; |
| 1792 | } |
| 1793 | |
| 1794 | |
| 1795 | // AddRHS - Implements: X + X --> X << 1 |
| 1796 | struct AddRHS { |
| 1797 | Value *RHS; |
| 1798 | AddRHS(Value *rhs) : RHS(rhs) {} |
| 1799 | bool shouldApply(Value *LHS) const { return LHS == RHS; } |
| 1800 | Instruction *apply(BinaryOperator &Add) const { |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 1801 | return BinaryOperator::createShl(Add.getOperand(0), |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1802 | ConstantInt::get(Add.getType(), 1)); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1803 | } |
| 1804 | }; |
| 1805 | |
| 1806 | // AddMaskingAnd - Implements (A & C1)+(B & C2) --> (A & C1)|(B & C2) |
| 1807 | // iff C1&C2 == 0 |
| 1808 | struct AddMaskingAnd { |
| 1809 | Constant *C2; |
| 1810 | AddMaskingAnd(Constant *c) : C2(c) {} |
| 1811 | bool shouldApply(Value *LHS) const { |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1812 | ConstantInt *C1; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1813 | return match(LHS, m_And(m_Value(), m_ConstantInt(C1))) && |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1814 | ConstantExpr::getAnd(C1, C2)->isNullValue(); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1815 | } |
| 1816 | Instruction *apply(BinaryOperator &Add) const { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1817 | return BinaryOperator::createOr(Add.getOperand(0), Add.getOperand(1)); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1818 | } |
| 1819 | }; |
| 1820 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1821 | static Value *FoldOperationIntoSelectOperand(Instruction &I, Value *SO, |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1822 | InstCombiner *IC) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1823 | if (CastInst *CI = dyn_cast<CastInst>(&I)) { |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1824 | if (Constant *SOC = dyn_cast<Constant>(SO)) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1825 | return ConstantExpr::getCast(CI->getOpcode(), SOC, I.getType()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1826 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1827 | return IC->InsertNewInstBefore(CastInst::create( |
| 1828 | CI->getOpcode(), SO, I.getType(), SO->getName() + ".cast"), I); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1829 | } |
| 1830 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1831 | // Figure out if the constant is the left or the right argument. |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1832 | bool ConstIsRHS = isa<Constant>(I.getOperand(1)); |
| 1833 | Constant *ConstOperand = cast<Constant>(I.getOperand(ConstIsRHS)); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1834 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1835 | if (Constant *SOC = dyn_cast<Constant>(SO)) { |
| 1836 | if (ConstIsRHS) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1837 | return ConstantExpr::get(I.getOpcode(), SOC, ConstOperand); |
| 1838 | return ConstantExpr::get(I.getOpcode(), ConstOperand, SOC); |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1839 | } |
| 1840 | |
| 1841 | Value *Op0 = SO, *Op1 = ConstOperand; |
| 1842 | if (!ConstIsRHS) |
| 1843 | std::swap(Op0, Op1); |
| 1844 | Instruction *New; |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1845 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I)) |
| 1846 | New = BinaryOperator::create(BO->getOpcode(), Op0, Op1,SO->getName()+".op"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1847 | else if (CmpInst *CI = dyn_cast<CmpInst>(&I)) |
| 1848 | New = CmpInst::create(CI->getOpcode(), CI->getPredicate(), Op0, Op1, |
| 1849 | SO->getName()+".cmp"); |
Chris Lattner | 326c0f3 | 2004-04-10 19:15:56 +0000 | [diff] [blame] | 1850 | else { |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1851 | assert(0 && "Unknown binary instruction type!"); |
Chris Lattner | 326c0f3 | 2004-04-10 19:15:56 +0000 | [diff] [blame] | 1852 | abort(); |
| 1853 | } |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1854 | return IC->InsertNewInstBefore(New, I); |
| 1855 | } |
| 1856 | |
| 1857 | // FoldOpIntoSelect - Given an instruction with a select as one operand and a |
| 1858 | // constant as the other operand, try to fold the binary operator into the |
| 1859 | // select arguments. This also works for Cast instructions, which obviously do |
| 1860 | // not have a second operand. |
| 1861 | static Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI, |
| 1862 | InstCombiner *IC) { |
| 1863 | // Don't modify shared select instructions |
| 1864 | if (!SI->hasOneUse()) return 0; |
| 1865 | Value *TV = SI->getOperand(1); |
| 1866 | Value *FV = SI->getOperand(2); |
| 1867 | |
| 1868 | if (isa<Constant>(TV) || isa<Constant>(FV)) { |
Chris Lattner | 956db27 | 2005-04-21 05:43:13 +0000 | [diff] [blame] | 1869 | // Bool selects with constant operands can be folded to logical ops. |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 1870 | if (SI->getType() == Type::Int1Ty) return 0; |
Chris Lattner | 956db27 | 2005-04-21 05:43:13 +0000 | [diff] [blame] | 1871 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1872 | Value *SelectTrueVal = FoldOperationIntoSelectOperand(Op, TV, IC); |
| 1873 | Value *SelectFalseVal = FoldOperationIntoSelectOperand(Op, FV, IC); |
| 1874 | |
| 1875 | return new SelectInst(SI->getCondition(), SelectTrueVal, |
| 1876 | SelectFalseVal); |
| 1877 | } |
| 1878 | return 0; |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1879 | } |
| 1880 | |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1881 | |
| 1882 | /// FoldOpIntoPhi - Given a binary operator or cast instruction which has a PHI |
| 1883 | /// node as operand #0, see if we can fold the instruction into the PHI (which |
| 1884 | /// is only possible if all operands to the PHI are constants). |
| 1885 | Instruction *InstCombiner::FoldOpIntoPhi(Instruction &I) { |
| 1886 | PHINode *PN = cast<PHINode>(I.getOperand(0)); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 1887 | unsigned NumPHIValues = PN->getNumIncomingValues(); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1888 | if (!PN->hasOneUse() || NumPHIValues == 0) return 0; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1889 | |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1890 | // Check to see if all of the operands of the PHI are constants. If there is |
| 1891 | // 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] | 1892 | // or if *it* is a PHI, bail out. |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1893 | BasicBlock *NonConstBB = 0; |
| 1894 | for (unsigned i = 0; i != NumPHIValues; ++i) |
| 1895 | if (!isa<Constant>(PN->getIncomingValue(i))) { |
| 1896 | if (NonConstBB) return 0; // More than one non-const value. |
Chris Lattner | b303668 | 2007-02-24 01:03:45 +0000 | [diff] [blame] | 1897 | if (isa<PHINode>(PN->getIncomingValue(i))) return 0; // Itself a phi. |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1898 | NonConstBB = PN->getIncomingBlock(i); |
| 1899 | |
| 1900 | // If the incoming non-constant value is in I's block, we have an infinite |
| 1901 | // loop. |
| 1902 | if (NonConstBB == I.getParent()) |
| 1903 | return 0; |
| 1904 | } |
| 1905 | |
| 1906 | // If there is exactly one non-constant value, we can insert a copy of the |
| 1907 | // operation in that block. However, if this is a critical edge, we would be |
| 1908 | // inserting the computation one some other paths (e.g. inside a loop). Only |
| 1909 | // do this if the pred block is unconditionally branching into the phi block. |
| 1910 | if (NonConstBB) { |
| 1911 | BranchInst *BI = dyn_cast<BranchInst>(NonConstBB->getTerminator()); |
| 1912 | if (!BI || !BI->isUnconditional()) return 0; |
| 1913 | } |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1914 | |
| 1915 | // Okay, we can do the transformation: create the new PHI node. |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 1916 | PHINode *NewPN = new PHINode(I.getType(), ""); |
Chris Lattner | 5551706 | 2005-01-29 00:39:08 +0000 | [diff] [blame] | 1917 | NewPN->reserveOperandSpace(PN->getNumOperands()/2); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1918 | InsertNewInstBefore(NewPN, *PN); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 1919 | NewPN->takeName(PN); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1920 | |
| 1921 | // Next, add all of the operands to the PHI. |
| 1922 | if (I.getNumOperands() == 2) { |
| 1923 | Constant *C = cast<Constant>(I.getOperand(1)); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 1924 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
Chris Lattner | a9ff5eb | 2007-08-05 08:47:58 +0000 | [diff] [blame] | 1925 | Value *InV = 0; |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1926 | if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1927 | if (CmpInst *CI = dyn_cast<CmpInst>(&I)) |
| 1928 | InV = ConstantExpr::getCompare(CI->getPredicate(), InC, C); |
| 1929 | else |
| 1930 | InV = ConstantExpr::get(I.getOpcode(), InC, C); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1931 | } else { |
| 1932 | assert(PN->getIncomingBlock(i) == NonConstBB); |
| 1933 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I)) |
| 1934 | InV = BinaryOperator::create(BO->getOpcode(), |
| 1935 | PN->getIncomingValue(i), C, "phitmp", |
| 1936 | NonConstBB->getTerminator()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1937 | else if (CmpInst *CI = dyn_cast<CmpInst>(&I)) |
| 1938 | InV = CmpInst::create(CI->getOpcode(), |
| 1939 | CI->getPredicate(), |
| 1940 | PN->getIncomingValue(i), C, "phitmp", |
| 1941 | NonConstBB->getTerminator()); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1942 | else |
| 1943 | assert(0 && "Unknown binop!"); |
| 1944 | |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 1945 | AddToWorkList(cast<Instruction>(InV)); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1946 | } |
| 1947 | NewPN->addIncoming(InV, PN->getIncomingBlock(i)); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1948 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1949 | } else { |
| 1950 | CastInst *CI = cast<CastInst>(&I); |
| 1951 | const Type *RetTy = CI->getType(); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 1952 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1953 | Value *InV; |
| 1954 | if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1955 | InV = ConstantExpr::getCast(CI->getOpcode(), InC, RetTy); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1956 | } else { |
| 1957 | assert(PN->getIncomingBlock(i) == NonConstBB); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1958 | InV = CastInst::create(CI->getOpcode(), PN->getIncomingValue(i), |
| 1959 | I.getType(), "phitmp", |
| 1960 | NonConstBB->getTerminator()); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 1961 | AddToWorkList(cast<Instruction>(InV)); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1962 | } |
| 1963 | NewPN->addIncoming(InV, PN->getIncomingBlock(i)); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1964 | } |
| 1965 | } |
| 1966 | return ReplaceInstUsesWith(I, NewPN); |
| 1967 | } |
| 1968 | |
Chris Lattner | 2454a2e | 2008-01-29 06:52:45 +0000 | [diff] [blame] | 1969 | |
| 1970 | /// CannotBeNegativeZero - Return true if we can prove that the specified FP |
| 1971 | /// value is never equal to -0.0. |
| 1972 | /// |
| 1973 | /// Note that this function will need to be revisited when we support nondefault |
| 1974 | /// rounding modes! |
| 1975 | /// |
| 1976 | static bool CannotBeNegativeZero(const Value *V) { |
| 1977 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V)) |
| 1978 | return !CFP->getValueAPF().isNegZero(); |
| 1979 | |
| 1980 | // (add x, 0.0) is guaranteed to return +0.0, not -0.0. |
| 1981 | if (const Instruction *I = dyn_cast<Instruction>(V)) { |
| 1982 | if (I->getOpcode() == Instruction::Add && |
| 1983 | isa<ConstantFP>(I->getOperand(1)) && |
| 1984 | cast<ConstantFP>(I->getOperand(1))->isNullValue()) |
| 1985 | return true; |
| 1986 | |
| 1987 | if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) |
| 1988 | if (II->getIntrinsicID() == Intrinsic::sqrt) |
| 1989 | return CannotBeNegativeZero(II->getOperand(1)); |
| 1990 | |
| 1991 | if (const CallInst *CI = dyn_cast<CallInst>(I)) |
| 1992 | if (const Function *F = CI->getCalledFunction()) { |
| 1993 | if (F->isDeclaration()) { |
| 1994 | switch (F->getNameLen()) { |
| 1995 | case 3: // abs(x) != -0.0 |
| 1996 | if (!strcmp(F->getNameStart(), "abs")) return true; |
| 1997 | break; |
| 1998 | case 4: // abs[lf](x) != -0.0 |
| 1999 | if (!strcmp(F->getNameStart(), "absf")) return true; |
| 2000 | if (!strcmp(F->getNameStart(), "absl")) return true; |
| 2001 | break; |
| 2002 | } |
| 2003 | } |
| 2004 | } |
| 2005 | } |
| 2006 | |
| 2007 | return false; |
| 2008 | } |
| 2009 | |
| 2010 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2011 | Instruction *InstCombiner::visitAdd(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 2012 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2013 | Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 2014 | |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 2015 | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2016 | // X + undef -> undef |
| 2017 | if (isa<UndefValue>(RHS)) |
| 2018 | return ReplaceInstUsesWith(I, RHS); |
| 2019 | |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 2020 | // X + 0 --> X |
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 2021 | if (!I.getType()->isFPOrFPVector()) { // NOTE: -0 + +0 = +0. |
Chris Lattner | 5e678e0 | 2005-10-17 17:56:38 +0000 | [diff] [blame] | 2022 | if (RHSC->isNullValue()) |
| 2023 | return ReplaceInstUsesWith(I, LHS); |
Chris Lattner | 8532cf6 | 2005-10-17 20:18:38 +0000 | [diff] [blame] | 2024 | } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) { |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 2025 | if (CFP->isExactlyValue(ConstantFP::getNegativeZero |
| 2026 | (I.getType())->getValueAPF())) |
Chris Lattner | 8532cf6 | 2005-10-17 20:18:38 +0000 | [diff] [blame] | 2027 | return ReplaceInstUsesWith(I, LHS); |
Chris Lattner | 5e678e0 | 2005-10-17 17:56:38 +0000 | [diff] [blame] | 2028 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2029 | |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 2030 | if (ConstantInt *CI = dyn_cast<ConstantInt>(RHSC)) { |
Chris Lattner | b4a2f05 | 2006-11-09 05:12:27 +0000 | [diff] [blame] | 2031 | // X + (signbit) --> X ^ signbit |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2032 | const APInt& Val = CI->getValue(); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 2033 | uint32_t BitWidth = Val.getBitWidth(); |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 2034 | if (Val == APInt::getSignBit(BitWidth)) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2035 | return BinaryOperator::createXor(LHS, RHS); |
Chris Lattner | b4a2f05 | 2006-11-09 05:12:27 +0000 | [diff] [blame] | 2036 | |
| 2037 | // See if SimplifyDemandedBits can simplify this. This handles stuff like |
| 2038 | // (X & 254)+1 -> (X&254)|1 |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 2039 | if (!isa<VectorType>(I.getType())) { |
| 2040 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 2041 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth), |
| 2042 | KnownZero, KnownOne)) |
| 2043 | return &I; |
| 2044 | } |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 2045 | } |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2046 | |
| 2047 | if (isa<PHINode>(LHS)) |
| 2048 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2049 | return NV; |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2050 | |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 2051 | ConstantInt *XorRHS = 0; |
| 2052 | Value *XorLHS = 0; |
Chris Lattner | c5eff44 | 2007-01-30 22:32:46 +0000 | [diff] [blame] | 2053 | if (isa<ConstantInt>(RHSC) && |
| 2054 | match(LHS, m_Xor(m_Value(XorLHS), m_ConstantInt(XorRHS)))) { |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 2055 | uint32_t TySizeBits = I.getType()->getPrimitiveSizeInBits(); |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2056 | const APInt& RHSVal = cast<ConstantInt>(RHSC)->getValue(); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2057 | |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 2058 | uint32_t Size = TySizeBits / 2; |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 2059 | APInt C0080Val(APInt(TySizeBits, 1ULL).shl(Size - 1)); |
| 2060 | APInt CFF80Val(-C0080Val); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2061 | do { |
| 2062 | if (TySizeBits > Size) { |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2063 | // If we have ADD(XOR(AND(X, 0xFF), 0x80), 0xF..F80), it's a sext. |
| 2064 | // 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] | 2065 | if ((RHSVal == CFF80Val && XorRHS->getValue() == C0080Val) || |
| 2066 | (RHSVal == C0080Val && XorRHS->getValue() == CFF80Val)) { |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2067 | // This is a sign extend if the top bits are known zero. |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 2068 | if (!MaskedValueIsZero(XorLHS, |
| 2069 | APInt::getHighBitsSet(TySizeBits, TySizeBits - Size))) |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2070 | 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] | 2071 | break; |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2072 | } |
| 2073 | } |
| 2074 | Size >>= 1; |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 2075 | C0080Val = APIntOps::lshr(C0080Val, Size); |
| 2076 | CFF80Val = APIntOps::ashr(CFF80Val, Size); |
| 2077 | } while (Size >= 1); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2078 | |
Reid Spencer | 35c3885 | 2007-03-28 01:36:16 +0000 | [diff] [blame] | 2079 | // FIXME: This shouldn't be necessary. When the backends can handle types |
| 2080 | // with funny bit widths then this whole cascade of if statements should |
| 2081 | // be removed. It is just here to get the size of the "middle" type back |
| 2082 | // up to something that the back ends can handle. |
| 2083 | const Type *MiddleType = 0; |
| 2084 | switch (Size) { |
| 2085 | default: break; |
| 2086 | case 32: MiddleType = Type::Int32Ty; break; |
| 2087 | case 16: MiddleType = Type::Int16Ty; break; |
| 2088 | case 8: MiddleType = Type::Int8Ty; break; |
| 2089 | } |
| 2090 | if (MiddleType) { |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 2091 | Instruction *NewTrunc = new TruncInst(XorLHS, MiddleType, "sext"); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2092 | InsertNewInstBefore(NewTrunc, I); |
Reid Spencer | 35c3885 | 2007-03-28 01:36:16 +0000 | [diff] [blame] | 2093 | return new SExtInst(NewTrunc, I.getType(), I.getName()); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2094 | } |
| 2095 | } |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 2096 | } |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 2097 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2098 | // X + X --> X << 1 |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 2099 | if (I.getType()->isInteger() && I.getType() != Type::Int1Ty) { |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2100 | if (Instruction *Result = AssociativeOpt(I, AddRHS(RHS))) return Result; |
Chris Lattner | 7edc8c2 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 2101 | |
| 2102 | if (Instruction *RHSI = dyn_cast<Instruction>(RHS)) { |
| 2103 | if (RHSI->getOpcode() == Instruction::Sub) |
| 2104 | if (LHS == RHSI->getOperand(1)) // A + (B - A) --> B |
| 2105 | return ReplaceInstUsesWith(I, RHSI->getOperand(0)); |
| 2106 | } |
| 2107 | if (Instruction *LHSI = dyn_cast<Instruction>(LHS)) { |
| 2108 | if (LHSI->getOpcode() == Instruction::Sub) |
| 2109 | if (RHS == LHSI->getOperand(1)) // (B - A) + A --> B |
| 2110 | return ReplaceInstUsesWith(I, LHSI->getOperand(0)); |
| 2111 | } |
Robert Bocchino | 7169828 | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 2112 | } |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 2113 | |
Chris Lattner | 5c4afb9 | 2002-05-08 22:46:53 +0000 | [diff] [blame] | 2114 | // -A + B --> B - A |
Chris Lattner | dd12f96 | 2008-02-17 21:03:36 +0000 | [diff] [blame] | 2115 | // -A + -B --> -(A + B) |
| 2116 | if (Value *LHSV = dyn_castNegVal(LHS)) { |
Chris Lattner | e10c0b9 | 2008-02-18 17:50:16 +0000 | [diff] [blame] | 2117 | if (LHS->getType()->isIntOrIntVector()) { |
| 2118 | if (Value *RHSV = dyn_castNegVal(RHS)) { |
| 2119 | Instruction *NewAdd = BinaryOperator::createAdd(LHSV, RHSV, "sum"); |
| 2120 | InsertNewInstBefore(NewAdd, I); |
| 2121 | return BinaryOperator::createNeg(NewAdd); |
| 2122 | } |
Chris Lattner | dd12f96 | 2008-02-17 21:03:36 +0000 | [diff] [blame] | 2123 | } |
| 2124 | |
| 2125 | return BinaryOperator::createSub(RHS, LHSV); |
| 2126 | } |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 2127 | |
| 2128 | // A + -B --> A - B |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 2129 | if (!isa<Constant>(RHS)) |
| 2130 | if (Value *V = dyn_castNegVal(RHS)) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2131 | return BinaryOperator::createSub(LHS, V); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2132 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2133 | |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2134 | ConstantInt *C2; |
| 2135 | if (Value *X = dyn_castFoldableMul(LHS, C2)) { |
| 2136 | if (X == RHS) // X*C + X --> X * (C+1) |
| 2137 | return BinaryOperator::createMul(RHS, AddOne(C2)); |
| 2138 | |
| 2139 | // X*C1 + X*C2 --> X * (C1+C2) |
| 2140 | ConstantInt *C1; |
| 2141 | if (X == dyn_castFoldableMul(RHS, C1)) |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2142 | return BinaryOperator::createMul(X, Add(C1, C2)); |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2143 | } |
| 2144 | |
| 2145 | // X + X*C --> X * (C+1) |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2146 | if (dyn_castFoldableMul(RHS, C2) == LHS) |
| 2147 | return BinaryOperator::createMul(LHS, AddOne(C2)); |
| 2148 | |
Chris Lattner | e617c9e | 2007-01-05 02:17:46 +0000 | [diff] [blame] | 2149 | // X + ~X --> -1 since ~X = -X-1 |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 2150 | if (dyn_castNotVal(LHS) == RHS || dyn_castNotVal(RHS) == LHS) |
| 2151 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | e617c9e | 2007-01-05 02:17:46 +0000 | [diff] [blame] | 2152 | |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2153 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2154 | // (A & C1)+(B & C2) --> (A & C1)|(B & C2) iff C1&C2 == 0 |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2155 | if (match(RHS, m_And(m_Value(), m_ConstantInt(C2)))) |
Chris Lattner | e617c9e | 2007-01-05 02:17:46 +0000 | [diff] [blame] | 2156 | if (Instruction *R = AssociativeOpt(I, AddMaskingAnd(C2))) |
| 2157 | return R; |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 2158 | |
Nick Lewycky | b6eabff | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 2159 | // W*X + Y*Z --> W * (X+Z) iff W == Y |
Nick Lewycky | 0c2c3f6 | 2008-02-03 08:19:11 +0000 | [diff] [blame] | 2160 | if (I.getType()->isIntOrIntVector()) { |
Nick Lewycky | b6eabff | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 2161 | Value *W, *X, *Y, *Z; |
| 2162 | if (match(LHS, m_Mul(m_Value(W), m_Value(X))) && |
| 2163 | match(RHS, m_Mul(m_Value(Y), m_Value(Z)))) { |
| 2164 | if (W != Y) { |
| 2165 | if (W == Z) { |
| 2166 | std::swap(Y, Z); |
| 2167 | } else if (Y == X) { |
| 2168 | std::swap(W, X); |
| 2169 | } else if (X == Z) { |
| 2170 | std::swap(Y, Z); |
| 2171 | std::swap(W, X); |
| 2172 | } |
| 2173 | } |
| 2174 | |
| 2175 | if (W == Y) { |
| 2176 | Value *NewAdd = InsertNewInstBefore(BinaryOperator::createAdd(X, Z, |
| 2177 | LHS->getName()), I); |
| 2178 | return BinaryOperator::createMul(W, NewAdd); |
| 2179 | } |
| 2180 | } |
| 2181 | } |
| 2182 | |
Chris Lattner | 6b03205 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 2183 | if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) { |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 2184 | Value *X = 0; |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2185 | if (match(LHS, m_Not(m_Value(X)))) // ~X + C --> (C-1) - X |
| 2186 | return BinaryOperator::createSub(SubOne(CRHS), X); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2187 | |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2188 | // (X & FF00) + xx00 -> (X+xx00) & FF00 |
| 2189 | 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] | 2190 | Constant *Anded = And(CRHS, C2); |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2191 | if (Anded == CRHS) { |
| 2192 | // See if all bits from the first bit set in the Add RHS up are included |
| 2193 | // in the mask. First, get the rightmost bit. |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2194 | const APInt& AddRHSV = CRHS->getValue(); |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2195 | |
| 2196 | // 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] | 2197 | APInt AddRHSHighBits(~((AddRHSV & -AddRHSV)-1)); |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2198 | |
| 2199 | // See if the and mask includes all of these bits. |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2200 | APInt AddRHSHighBitsAnd(AddRHSHighBits & C2->getValue()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2201 | |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2202 | if (AddRHSHighBits == AddRHSHighBitsAnd) { |
| 2203 | // Okay, the xform is safe. Insert the new add pronto. |
| 2204 | Value *NewAdd = InsertNewInstBefore(BinaryOperator::createAdd(X, CRHS, |
| 2205 | LHS->getName()), I); |
| 2206 | return BinaryOperator::createAnd(NewAdd, C2); |
| 2207 | } |
| 2208 | } |
| 2209 | } |
| 2210 | |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2211 | // Try to fold constant add into select arguments. |
| 2212 | if (SelectInst *SI = dyn_cast<SelectInst>(LHS)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2213 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2214 | return R; |
Chris Lattner | 6b03205 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 2215 | } |
| 2216 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2217 | // add (cast *A to intptrtype) B -> |
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2218 | // cast (GEP (cast *A to sbyte*) B) --> intptrtype |
Andrew Lenharth | 16d7955 | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 2219 | { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2220 | CastInst *CI = dyn_cast<CastInst>(LHS); |
| 2221 | Value *Other = RHS; |
Andrew Lenharth | 16d7955 | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 2222 | if (!CI) { |
| 2223 | CI = dyn_cast<CastInst>(RHS); |
| 2224 | Other = LHS; |
| 2225 | } |
Andrew Lenharth | 4563326 | 2006-09-20 15:37:57 +0000 | [diff] [blame] | 2226 | if (CI && CI->getType()->isSized() && |
Reid Spencer | abaa8ca | 2007-01-08 16:32:00 +0000 | [diff] [blame] | 2227 | (CI->getType()->getPrimitiveSizeInBits() == |
| 2228 | TD->getIntPtrType()->getPrimitiveSizeInBits()) |
Andrew Lenharth | 4563326 | 2006-09-20 15:37:57 +0000 | [diff] [blame] | 2229 | && isa<PointerType>(CI->getOperand(0)->getType())) { |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 2230 | unsigned AS = |
| 2231 | cast<PointerType>(CI->getOperand(0)->getType())->getAddressSpace(); |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 2232 | Value *I2 = InsertBitCastBefore(CI->getOperand(0), |
| 2233 | PointerType::get(Type::Int8Ty, AS), I); |
Andrew Lenharth | 4563326 | 2006-09-20 15:37:57 +0000 | [diff] [blame] | 2234 | I2 = InsertNewInstBefore(new GetElementPtrInst(I2, Other, "ctg2"), I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2235 | return new PtrToIntInst(I2, CI->getType()); |
Andrew Lenharth | 16d7955 | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 2236 | } |
| 2237 | } |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2238 | |
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2239 | // add (select X 0 (sub n A)) A --> select X A n |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2240 | { |
| 2241 | SelectInst *SI = dyn_cast<SelectInst>(LHS); |
| 2242 | Value *Other = RHS; |
| 2243 | if (!SI) { |
| 2244 | SI = dyn_cast<SelectInst>(RHS); |
| 2245 | Other = LHS; |
| 2246 | } |
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2247 | if (SI && SI->hasOneUse()) { |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2248 | Value *TV = SI->getTrueValue(); |
| 2249 | Value *FV = SI->getFalseValue(); |
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2250 | Value *A, *N; |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2251 | |
| 2252 | // Can we fold the add into the argument of the select? |
| 2253 | // We check both true and false select arguments for a matching subtract. |
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2254 | if (match(FV, m_Zero()) && match(TV, m_Sub(m_Value(N), m_Value(A))) && |
| 2255 | A == Other) // Fold the add into the true select value. |
| 2256 | return new SelectInst(SI->getCondition(), N, A); |
| 2257 | if (match(TV, m_Zero()) && match(FV, m_Sub(m_Value(N), m_Value(A))) && |
| 2258 | A == Other) // Fold the add into the false select value. |
| 2259 | return new SelectInst(SI->getCondition(), A, N); |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2260 | } |
| 2261 | } |
Chris Lattner | 2454a2e | 2008-01-29 06:52:45 +0000 | [diff] [blame] | 2262 | |
| 2263 | // Check for X+0.0. Simplify it to X if we know X is not -0.0. |
| 2264 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHS)) |
| 2265 | if (CFP->getValueAPF().isPosZero() && CannotBeNegativeZero(LHS)) |
| 2266 | return ReplaceInstUsesWith(I, LHS); |
Andrew Lenharth | 16d7955 | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 2267 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2268 | return Changed ? &I : 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2269 | } |
| 2270 | |
Chris Lattner | 1ba5bcd | 2003-07-22 21:46:59 +0000 | [diff] [blame] | 2271 | // isSignBit - Return true if the value represented by the constant only has the |
| 2272 | // highest order bit set. |
| 2273 | static bool isSignBit(ConstantInt *CI) { |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 2274 | uint32_t NumBits = CI->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 5a1e3e1 | 2007-03-19 20:58:18 +0000 | [diff] [blame] | 2275 | return CI->getValue() == APInt::getSignBit(NumBits); |
Chris Lattner | 1ba5bcd | 2003-07-22 21:46:59 +0000 | [diff] [blame] | 2276 | } |
| 2277 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2278 | Instruction *InstCombiner::visitSub(BinaryOperator &I) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2279 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2280 | |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2281 | if (Op0 == Op1) // sub X, X -> 0 |
| 2282 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2283 | |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2284 | // If this is a 'B = x-(-A)', change to B = x+A... |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 2285 | if (Value *V = dyn_castNegVal(Op1)) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2286 | return BinaryOperator::createAdd(Op0, V); |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 2287 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2288 | if (isa<UndefValue>(Op0)) |
| 2289 | return ReplaceInstUsesWith(I, Op0); // undef - X -> undef |
| 2290 | if (isa<UndefValue>(Op1)) |
| 2291 | return ReplaceInstUsesWith(I, Op1); // X - undef -> undef |
| 2292 | |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 2293 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op0)) { |
| 2294 | // Replace (-1 - A) with (~A)... |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2295 | if (C->isAllOnesValue()) |
| 2296 | return BinaryOperator::createNot(Op1); |
Chris Lattner | 4037171 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 2297 | |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 2298 | // C - ~X == X + (1+C) |
Reid Spencer | 4b828e6 | 2005-06-18 17:37:34 +0000 | [diff] [blame] | 2299 | Value *X = 0; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2300 | if (match(Op1, m_Not(m_Value(X)))) |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2301 | return BinaryOperator::createAdd(X, AddOne(C)); |
| 2302 | |
Chris Lattner | 76b7a06 | 2007-01-15 07:02:54 +0000 | [diff] [blame] | 2303 | // -(X >>u 31) -> (X >>s 31) |
| 2304 | // -(X >>s 31) -> (X >>u 31) |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 2305 | if (C->isZero()) { |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 2306 | if (BinaryOperator *SI = dyn_cast<BinaryOperator>(Op1)) { |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2307 | if (SI->getOpcode() == Instruction::LShr) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2308 | if (ConstantInt *CU = dyn_cast<ConstantInt>(SI->getOperand(1))) { |
Chris Lattner | 9c29067 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 2309 | // 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] | 2310 | if (CU->getLimitedValue(SI->getType()->getPrimitiveSizeInBits()) == |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2311 | SI->getType()->getPrimitiveSizeInBits()-1) { |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2312 | // Ok, the transformation is safe. Insert AShr. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2313 | return BinaryOperator::create(Instruction::AShr, |
| 2314 | SI->getOperand(0), CU, SI->getName()); |
Chris Lattner | 9c29067 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 2315 | } |
| 2316 | } |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2317 | } |
| 2318 | else if (SI->getOpcode() == Instruction::AShr) { |
| 2319 | if (ConstantInt *CU = dyn_cast<ConstantInt>(SI->getOperand(1))) { |
| 2320 | // 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] | 2321 | if (CU->getLimitedValue(SI->getType()->getPrimitiveSizeInBits()) == |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2322 | SI->getType()->getPrimitiveSizeInBits()-1) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 2323 | // Ok, the transformation is safe. Insert LShr. |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 2324 | return BinaryOperator::createLShr( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2325 | SI->getOperand(0), CU, SI->getName()); |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2326 | } |
| 2327 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 2328 | } |
| 2329 | } |
Chris Lattner | bfe492b | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 2330 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2331 | |
| 2332 | // Try to fold constant sub into select arguments. |
| 2333 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2334 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2335 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2336 | |
| 2337 | if (isa<PHINode>(Op0)) |
| 2338 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2339 | return NV; |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 2340 | } |
| 2341 | |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2342 | if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) { |
| 2343 | if (Op1I->getOpcode() == Instruction::Add && |
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 2344 | !Op0->getType()->isFPOrFPVector()) { |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2345 | if (Op1I->getOperand(0) == Op0) // X-(X+Y) == -Y |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2346 | return BinaryOperator::createNeg(Op1I->getOperand(1), I.getName()); |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2347 | else if (Op1I->getOperand(1) == Op0) // X-(Y+X) == -Y |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2348 | return BinaryOperator::createNeg(Op1I->getOperand(0), I.getName()); |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2349 | else if (ConstantInt *CI1 = dyn_cast<ConstantInt>(I.getOperand(0))) { |
| 2350 | if (ConstantInt *CI2 = dyn_cast<ConstantInt>(Op1I->getOperand(1))) |
| 2351 | // C1-(X+C2) --> (C1-C2)-X |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2352 | return BinaryOperator::createSub(Subtract(CI1, CI2), |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2353 | Op1I->getOperand(0)); |
| 2354 | } |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2355 | } |
| 2356 | |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 2357 | if (Op1I->hasOneUse()) { |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2358 | // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression |
| 2359 | // is not used by anyone else... |
| 2360 | // |
Chris Lattner | 0517e72 | 2004-02-02 20:09:56 +0000 | [diff] [blame] | 2361 | if (Op1I->getOpcode() == Instruction::Sub && |
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 2362 | !Op1I->getType()->isFPOrFPVector()) { |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2363 | // Swap the two operands of the subexpr... |
| 2364 | Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1); |
| 2365 | Op1I->setOperand(0, IIOp1); |
| 2366 | Op1I->setOperand(1, IIOp0); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2367 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2368 | // Create the new top level add instruction... |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2369 | return BinaryOperator::createAdd(Op0, Op1); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2370 | } |
| 2371 | |
| 2372 | // Replace (A - (A & B)) with (A & ~B) if this is the only use of (A&B)... |
| 2373 | // |
| 2374 | if (Op1I->getOpcode() == Instruction::And && |
| 2375 | (Op1I->getOperand(0) == Op0 || Op1I->getOperand(1) == Op0)) { |
| 2376 | Value *OtherOp = Op1I->getOperand(Op1I->getOperand(0) == Op0); |
| 2377 | |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 2378 | Value *NewNot = |
| 2379 | InsertNewInstBefore(BinaryOperator::createNot(OtherOp, "B.not"), I); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2380 | return BinaryOperator::createAnd(Op0, NewNot); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2381 | } |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2382 | |
Reid Spencer | ac5209e | 2006-10-16 23:08:08 +0000 | [diff] [blame] | 2383 | // 0 - (X sdiv C) -> (X sdiv -C) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2384 | if (Op1I->getOpcode() == Instruction::SDiv) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2385 | if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0)) |
Zhou Sheng | 843f0767 | 2007-04-19 05:39:12 +0000 | [diff] [blame] | 2386 | if (CSI->isZero()) |
Chris Lattner | 91ccc15 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 2387 | if (Constant *DivRHS = dyn_cast<Constant>(Op1I->getOperand(1))) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2388 | return BinaryOperator::createSDiv(Op1I->getOperand(0), |
Chris Lattner | 91ccc15 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 2389 | ConstantExpr::getNeg(DivRHS)); |
| 2390 | |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2391 | // X - X*C --> X * (1-C) |
Reid Spencer | 4b828e6 | 2005-06-18 17:37:34 +0000 | [diff] [blame] | 2392 | ConstantInt *C2 = 0; |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2393 | if (dyn_castFoldableMul(Op1I, C2) == Op0) { |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2394 | Constant *CP1 = Subtract(ConstantInt::get(I.getType(), 1), C2); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2395 | return BinaryOperator::createMul(Op0, CP1); |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2396 | } |
Dan Gohman | 5d066ff | 2007-09-17 17:31:57 +0000 | [diff] [blame] | 2397 | |
| 2398 | // X - ((X / Y) * Y) --> X % Y |
| 2399 | if (Op1I->getOpcode() == Instruction::Mul) |
| 2400 | if (Instruction *I = dyn_cast<Instruction>(Op1I->getOperand(0))) |
| 2401 | if (Op0 == I->getOperand(0) && |
| 2402 | Op1I->getOperand(1) == I->getOperand(1)) { |
| 2403 | if (I->getOpcode() == Instruction::SDiv) |
| 2404 | return BinaryOperator::createSRem(Op0, Op1I->getOperand(1)); |
| 2405 | if (I->getOpcode() == Instruction::UDiv) |
| 2406 | return BinaryOperator::createURem(Op0, Op1I->getOperand(1)); |
| 2407 | } |
Chris Lattner | 4037171 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 2408 | } |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2409 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2410 | |
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 2411 | if (!Op0->getType()->isFPOrFPVector()) |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 2412 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) { |
Chris Lattner | 7edc8c2 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 2413 | if (Op0I->getOpcode() == Instruction::Add) { |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 2414 | if (Op0I->getOperand(0) == Op1) // (Y+X)-Y == X |
| 2415 | return ReplaceInstUsesWith(I, Op0I->getOperand(1)); |
| 2416 | else if (Op0I->getOperand(1) == Op1) // (X+Y)-Y == X |
| 2417 | return ReplaceInstUsesWith(I, Op0I->getOperand(0)); |
Chris Lattner | 7edc8c2 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 2418 | } else if (Op0I->getOpcode() == Instruction::Sub) { |
| 2419 | if (Op0I->getOperand(0) == Op1) // (X-Y)-X == -Y |
| 2420 | return BinaryOperator::createNeg(Op0I->getOperand(1), I.getName()); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 2421 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 2422 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2423 | |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2424 | ConstantInt *C1; |
| 2425 | if (Value *X = dyn_castFoldableMul(Op0, C1)) { |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2426 | if (X == Op1) // X*C - X --> X * (C-1) |
| 2427 | return BinaryOperator::createMul(Op1, SubOne(C1)); |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2428 | |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2429 | ConstantInt *C2; // X*C1 - X*C2 -> X * (C1-C2) |
| 2430 | if (X == dyn_castFoldableMul(Op1, C2)) |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2431 | return BinaryOperator::createMul(Op1, Subtract(C1, C2)); |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2432 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2433 | return 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2434 | } |
| 2435 | |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2436 | /// isSignBitCheck - Given an exploded icmp instruction, return true if the |
| 2437 | /// comparison only checks the sign bit. If it only checks the sign bit, set |
| 2438 | /// TrueIfSigned if the result of the comparison is true when the input value is |
| 2439 | /// signed. |
| 2440 | static bool isSignBitCheck(ICmpInst::Predicate pred, ConstantInt *RHS, |
| 2441 | bool &TrueIfSigned) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2442 | switch (pred) { |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2443 | case ICmpInst::ICMP_SLT: // True if LHS s< 0 |
| 2444 | TrueIfSigned = true; |
| 2445 | return RHS->isZero(); |
Chris Lattner | cb7122b | 2007-07-16 04:15:34 +0000 | [diff] [blame] | 2446 | case ICmpInst::ICMP_SLE: // True if LHS s<= RHS and RHS == -1 |
| 2447 | TrueIfSigned = true; |
| 2448 | return RHS->isAllOnesValue(); |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2449 | case ICmpInst::ICMP_SGT: // True if LHS s> -1 |
| 2450 | TrueIfSigned = false; |
| 2451 | return RHS->isAllOnesValue(); |
Chris Lattner | cb7122b | 2007-07-16 04:15:34 +0000 | [diff] [blame] | 2452 | case ICmpInst::ICMP_UGT: |
| 2453 | // True if LHS u> RHS and RHS == high-bit-mask - 1 |
| 2454 | TrueIfSigned = true; |
| 2455 | return RHS->getValue() == |
| 2456 | APInt::getSignedMaxValue(RHS->getType()->getPrimitiveSizeInBits()); |
| 2457 | case ICmpInst::ICMP_UGE: |
| 2458 | // True if LHS u>= RHS and RHS == high-bit-mask (2^7, 2^15, 2^31, etc) |
| 2459 | TrueIfSigned = true; |
| 2460 | return RHS->getValue() == |
| 2461 | APInt::getSignBit(RHS->getType()->getPrimitiveSizeInBits()); |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2462 | default: |
| 2463 | return false; |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2464 | } |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2465 | } |
| 2466 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2467 | Instruction *InstCombiner::visitMul(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 2468 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2469 | Value *Op0 = I.getOperand(0); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2470 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2471 | if (isa<UndefValue>(I.getOperand(1))) // undef * X -> 0 |
| 2472 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2473 | |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2474 | // Simplify mul instructions with a constant RHS... |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2475 | if (Constant *Op1 = dyn_cast<Constant>(I.getOperand(1))) { |
| 2476 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 2477 | |
| 2478 | // ((X << C1)*C2) == (X * (C2 << C1)) |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2479 | if (BinaryOperator *SI = dyn_cast<BinaryOperator>(Op0)) |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 2480 | if (SI->getOpcode() == Instruction::Shl) |
| 2481 | if (Constant *ShOp = dyn_cast<Constant>(SI->getOperand(1))) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2482 | return BinaryOperator::createMul(SI->getOperand(0), |
| 2483 | ConstantExpr::getShl(CI, ShOp)); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2484 | |
Zhou Sheng | 843f0767 | 2007-04-19 05:39:12 +0000 | [diff] [blame] | 2485 | if (CI->isZero()) |
Chris Lattner | 515c97c | 2003-09-11 22:24:54 +0000 | [diff] [blame] | 2486 | return ReplaceInstUsesWith(I, Op1); // X * 0 == 0 |
| 2487 | if (CI->equalsInt(1)) // X * 1 == X |
| 2488 | return ReplaceInstUsesWith(I, Op0); |
| 2489 | if (CI->isAllOnesValue()) // X * -1 == 0 - X |
Chris Lattner | 0af1fab | 2003-06-25 17:09:20 +0000 | [diff] [blame] | 2490 | return BinaryOperator::createNeg(Op0, I.getName()); |
Chris Lattner | 6c1ce21 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 2491 | |
Zhou Sheng | 97b52c2 | 2007-03-29 01:57:21 +0000 | [diff] [blame] | 2492 | const APInt& Val = cast<ConstantInt>(CI)->getValue(); |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2493 | if (Val.isPowerOf2()) { // Replace X*(2^C) with X << C |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 2494 | return BinaryOperator::createShl(Op0, |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2495 | ConstantInt::get(Op0->getType(), Val.logBase2())); |
Chris Lattner | bcd7db5 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 2496 | } |
Robert Bocchino | 7169828 | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 2497 | } else if (ConstantFP *Op1F = dyn_cast<ConstantFP>(Op1)) { |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2498 | if (Op1F->isNullValue()) |
| 2499 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 6c1ce21 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 2500 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2501 | // "In IEEE floating point, x*1 is not equivalent to x for nans. However, |
| 2502 | // 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] | 2503 | // We need a better interface for long double here. |
| 2504 | if (Op1->getType() == Type::FloatTy || Op1->getType() == Type::DoubleTy) |
| 2505 | if (Op1F->isExactlyValue(1.0)) |
| 2506 | return ReplaceInstUsesWith(I, Op0); // Eliminate 'mul double %X, 1.0' |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2507 | } |
Chris Lattner | ab51f3f | 2006-03-04 06:04:02 +0000 | [diff] [blame] | 2508 | |
| 2509 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) |
| 2510 | if (Op0I->getOpcode() == Instruction::Add && Op0I->hasOneUse() && |
| 2511 | isa<ConstantInt>(Op0I->getOperand(1))) { |
| 2512 | // Canonicalize (X+C1)*C2 -> X*C2+C1*C2. |
| 2513 | Instruction *Add = BinaryOperator::createMul(Op0I->getOperand(0), |
| 2514 | Op1, "tmp"); |
| 2515 | InsertNewInstBefore(Add, I); |
| 2516 | Value *C1C2 = ConstantExpr::getMul(Op1, |
| 2517 | cast<Constant>(Op0I->getOperand(1))); |
| 2518 | return BinaryOperator::createAdd(Add, C1C2); |
| 2519 | |
| 2520 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2521 | |
| 2522 | // Try to fold constant mul into select arguments. |
| 2523 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2524 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2525 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2526 | |
| 2527 | if (isa<PHINode>(Op0)) |
| 2528 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2529 | return NV; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2530 | } |
| 2531 | |
Chris Lattner | a4f445b | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 2532 | if (Value *Op0v = dyn_castNegVal(Op0)) // -X * -Y = X*Y |
| 2533 | if (Value *Op1v = dyn_castNegVal(I.getOperand(1))) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2534 | return BinaryOperator::createMul(Op0v, Op1v); |
Chris Lattner | a4f445b | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 2535 | |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2536 | // If one of the operands of the multiply is a cast from a boolean value, then |
| 2537 | // we know the bool is either zero or one, so this is a 'masking' multiply. |
| 2538 | // See if we can simplify things based on how the boolean was originally |
| 2539 | // formed. |
| 2540 | CastInst *BoolCast = 0; |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 2541 | if (ZExtInst *CI = dyn_cast<ZExtInst>(I.getOperand(0))) |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 2542 | if (CI->getOperand(0)->getType() == Type::Int1Ty) |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2543 | BoolCast = CI; |
| 2544 | if (!BoolCast) |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 2545 | if (ZExtInst *CI = dyn_cast<ZExtInst>(I.getOperand(1))) |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 2546 | if (CI->getOperand(0)->getType() == Type::Int1Ty) |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2547 | BoolCast = CI; |
| 2548 | if (BoolCast) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2549 | if (ICmpInst *SCI = dyn_cast<ICmpInst>(BoolCast->getOperand(0))) { |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2550 | Value *SCIOp0 = SCI->getOperand(0), *SCIOp1 = SCI->getOperand(1); |
| 2551 | const Type *SCOpTy = SCIOp0->getType(); |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2552 | bool TIS = false; |
| 2553 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2554 | // 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] | 2555 | // multiply into a shift/and combination. |
| 2556 | if (isa<ConstantInt>(SCIOp1) && |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2557 | isSignBitCheck(SCI->getPredicate(), cast<ConstantInt>(SCIOp1), TIS) && |
| 2558 | TIS) { |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2559 | // Shift the X value right to turn it into "all signbits". |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2560 | Constant *Amt = ConstantInt::get(SCIOp0->getType(), |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 2561 | SCOpTy->getPrimitiveSizeInBits()-1); |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2562 | Value *V = |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2563 | InsertNewInstBefore( |
| 2564 | BinaryOperator::create(Instruction::AShr, SCIOp0, Amt, |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2565 | BoolCast->getOperand(0)->getName()+ |
| 2566 | ".mask"), I); |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2567 | |
| 2568 | // If the multiply type is not the same as the source type, sign extend |
| 2569 | // or truncate to the multiply type. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 2570 | if (I.getType() != V->getType()) { |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 2571 | uint32_t SrcBits = V->getType()->getPrimitiveSizeInBits(); |
| 2572 | uint32_t DstBits = I.getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 2573 | Instruction::CastOps opcode = |
| 2574 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2575 | (SrcBits < DstBits ? Instruction::SExt : Instruction::Trunc)); |
| 2576 | V = InsertCastBefore(opcode, V, I.getType(), I); |
| 2577 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2578 | |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2579 | Value *OtherOp = Op0 == BoolCast ? I.getOperand(1) : Op0; |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2580 | return BinaryOperator::createAnd(V, OtherOp); |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2581 | } |
| 2582 | } |
| 2583 | } |
| 2584 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2585 | return Changed ? &I : 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2586 | } |
| 2587 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2588 | /// This function implements the transforms on div instructions that work |
| 2589 | /// regardless of the kind of div instruction it is (udiv, sdiv, or fdiv). It is |
| 2590 | /// used by the visitors to those instructions. |
| 2591 | /// @brief Transforms common to all three div instructions |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2592 | Instruction *InstCombiner::commonDivTransforms(BinaryOperator &I) { |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2593 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2594 | |
Chris Lattner | 50b2ca4 | 2008-02-19 06:12:18 +0000 | [diff] [blame] | 2595 | // undef / X -> 0 for integer. |
| 2596 | // undef / X -> undef for FP (the undef could be a snan). |
| 2597 | if (isa<UndefValue>(Op0)) { |
| 2598 | if (Op0->getType()->isFPOrFPVector()) |
| 2599 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2600 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | 50b2ca4 | 2008-02-19 06:12:18 +0000 | [diff] [blame] | 2601 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2602 | |
| 2603 | // X / undef -> undef |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2604 | if (isa<UndefValue>(Op1)) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2605 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2606 | |
Chris Lattner | 25feae5 | 2008-01-28 00:58:18 +0000 | [diff] [blame] | 2607 | // Handle cases involving: [su]div X, (select Cond, Y, Z) |
| 2608 | // This does not apply for fdiv. |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2609 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { |
Chris Lattner | 25feae5 | 2008-01-28 00:58:18 +0000 | [diff] [blame] | 2610 | // [su]div X, (Cond ? 0 : Y) -> div X, Y. If the div and the select are in |
| 2611 | // the same basic block, then we replace the select with Y, and the |
| 2612 | // condition of the select with false (if the cond value is in the same BB). |
| 2613 | // If the select has uses other than the div, this allows them to be |
| 2614 | // simplified also. Note that div X, Y is just as good as div X, 0 (undef) |
| 2615 | if (ConstantInt *ST = dyn_cast<ConstantInt>(SI->getOperand(1))) |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2616 | if (ST->isNullValue()) { |
| 2617 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); |
| 2618 | if (CondI && CondI->getParent() == I.getParent()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2619 | UpdateValueUsesWith(CondI, ConstantInt::getFalse()); |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2620 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
| 2621 | I.setOperand(1, SI->getOperand(2)); |
| 2622 | else |
| 2623 | UpdateValueUsesWith(SI, SI->getOperand(2)); |
| 2624 | return &I; |
| 2625 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2626 | |
Chris Lattner | 25feae5 | 2008-01-28 00:58:18 +0000 | [diff] [blame] | 2627 | // Likewise for: [su]div X, (Cond ? Y : 0) -> div X, Y |
| 2628 | if (ConstantInt *ST = dyn_cast<ConstantInt>(SI->getOperand(2))) |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2629 | if (ST->isNullValue()) { |
| 2630 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); |
| 2631 | if (CondI && CondI->getParent() == I.getParent()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2632 | UpdateValueUsesWith(CondI, ConstantInt::getTrue()); |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2633 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
| 2634 | I.setOperand(1, SI->getOperand(1)); |
| 2635 | else |
| 2636 | UpdateValueUsesWith(SI, SI->getOperand(1)); |
| 2637 | return &I; |
| 2638 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2639 | } |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2640 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2641 | return 0; |
| 2642 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2643 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2644 | /// This function implements the transforms common to both integer division |
| 2645 | /// instructions (udiv and sdiv). It is called by the visitors to those integer |
| 2646 | /// division instructions. |
| 2647 | /// @brief Common integer divide transforms |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2648 | Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2649 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2650 | |
| 2651 | if (Instruction *Common = commonDivTransforms(I)) |
| 2652 | return Common; |
| 2653 | |
| 2654 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
| 2655 | // div X, 1 == X |
| 2656 | if (RHS->equalsInt(1)) |
| 2657 | return ReplaceInstUsesWith(I, Op0); |
| 2658 | |
| 2659 | // (X / C1) / C2 -> X / (C1*C2) |
| 2660 | if (Instruction *LHS = dyn_cast<Instruction>(Op0)) |
| 2661 | if (Instruction::BinaryOps(LHS->getOpcode()) == I.getOpcode()) |
| 2662 | if (ConstantInt *LHSRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) { |
Nick Lewycky | e0cfecf | 2008-02-18 22:48:05 +0000 | [diff] [blame] | 2663 | if (MultiplyOverflows(RHS, LHSRHS, I.getOpcode()==Instruction::SDiv)) |
| 2664 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2665 | else |
| 2666 | return BinaryOperator::create(I.getOpcode(), LHS->getOperand(0), |
| 2667 | Multiply(RHS, LHSRHS)); |
Chris Lattner | bf70b83 | 2005-04-08 04:03:26 +0000 | [diff] [blame] | 2668 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2669 | |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2670 | if (!RHS->isZero()) { // avoid X udiv 0 |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2671 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 2672 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 2673 | return R; |
| 2674 | if (isa<PHINode>(Op0)) |
| 2675 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2676 | return NV; |
| 2677 | } |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2678 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2679 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2680 | // 0 / X == 0, we don't need to preserve faults! |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2681 | if (ConstantInt *LHS = dyn_cast<ConstantInt>(Op0)) |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2682 | if (LHS->equalsInt(0)) |
| 2683 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2684 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2685 | return 0; |
| 2686 | } |
| 2687 | |
| 2688 | Instruction *InstCombiner::visitUDiv(BinaryOperator &I) { |
| 2689 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2690 | |
| 2691 | // Handle the integer div common cases |
| 2692 | if (Instruction *Common = commonIDivTransforms(I)) |
| 2693 | return Common; |
| 2694 | |
| 2695 | // X udiv C^2 -> X >> C |
| 2696 | // Check to see if this is an unsigned division with an exact power of 2, |
| 2697 | // if so, convert to a right shift. |
| 2698 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op1)) { |
Reid Spencer | 6eb0d99 | 2007-03-26 23:58:26 +0000 | [diff] [blame] | 2699 | if (C->getValue().isPowerOf2()) // 0 not included in isPowerOf2 |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2700 | return BinaryOperator::createLShr(Op0, |
Zhou Sheng | 0fc5095 | 2007-03-25 05:01:29 +0000 | [diff] [blame] | 2701 | ConstantInt::get(Op0->getType(), C->getValue().logBase2())); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2702 | } |
| 2703 | |
| 2704 | // X udiv (C1 << N), where C1 is "1<<C2" --> X >> (N+C2) |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2705 | if (BinaryOperator *RHSI = dyn_cast<BinaryOperator>(I.getOperand(1))) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2706 | if (RHSI->getOpcode() == Instruction::Shl && |
| 2707 | isa<ConstantInt>(RHSI->getOperand(0))) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2708 | const APInt& C1 = cast<ConstantInt>(RHSI->getOperand(0))->getValue(); |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2709 | if (C1.isPowerOf2()) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2710 | Value *N = RHSI->getOperand(1); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2711 | const Type *NTy = N->getType(); |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 2712 | if (uint32_t C2 = C1.logBase2()) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2713 | Constant *C2V = ConstantInt::get(NTy, C2); |
| 2714 | N = InsertNewInstBefore(BinaryOperator::createAdd(N, C2V, "tmp"), I); |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2715 | } |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 2716 | return BinaryOperator::createLShr(Op0, N); |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2717 | } |
| 2718 | } |
Chris Lattner | c812e5d | 2005-11-05 07:40:31 +0000 | [diff] [blame] | 2719 | } |
| 2720 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2721 | // udiv X, (Select Cond, C1, C2) --> Select Cond, (shr X, C1), (shr X, C2) |
| 2722 | // where C1&C2 are powers of two. |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2723 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2724 | if (ConstantInt *STO = dyn_cast<ConstantInt>(SI->getOperand(1))) |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2725 | if (ConstantInt *SFO = dyn_cast<ConstantInt>(SI->getOperand(2))) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2726 | const APInt &TVA = STO->getValue(), &FVA = SFO->getValue(); |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2727 | if (TVA.isPowerOf2() && FVA.isPowerOf2()) { |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2728 | // Compute the shift amounts |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2729 | uint32_t TSA = TVA.logBase2(), FSA = FVA.logBase2(); |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2730 | // Construct the "on true" case of the select |
| 2731 | Constant *TC = ConstantInt::get(Op0->getType(), TSA); |
| 2732 | Instruction *TSI = BinaryOperator::createLShr( |
| 2733 | Op0, TC, SI->getName()+".t"); |
| 2734 | TSI = InsertNewInstBefore(TSI, I); |
| 2735 | |
| 2736 | // Construct the "on false" case of the select |
| 2737 | Constant *FC = ConstantInt::get(Op0->getType(), FSA); |
| 2738 | Instruction *FSI = BinaryOperator::createLShr( |
| 2739 | Op0, FC, SI->getName()+".f"); |
| 2740 | FSI = InsertNewInstBefore(FSI, I); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2741 | |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2742 | // construct the select instruction and return it. |
| 2743 | return new SelectInst(SI->getOperand(0), TSI, FSI, SI->getName()); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2744 | } |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2745 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2746 | return 0; |
| 2747 | } |
| 2748 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2749 | Instruction *InstCombiner::visitSDiv(BinaryOperator &I) { |
| 2750 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2751 | |
| 2752 | // Handle the integer div common cases |
| 2753 | if (Instruction *Common = commonIDivTransforms(I)) |
| 2754 | return Common; |
| 2755 | |
| 2756 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
| 2757 | // sdiv X, -1 == -X |
| 2758 | if (RHS->isAllOnesValue()) |
| 2759 | return BinaryOperator::createNeg(Op0); |
| 2760 | |
| 2761 | // -X/C -> X/-C |
| 2762 | if (Value *LHSNeg = dyn_castNegVal(Op0)) |
| 2763 | return BinaryOperator::createSDiv(LHSNeg, ConstantExpr::getNeg(RHS)); |
| 2764 | } |
| 2765 | |
| 2766 | // If the sign bits of both operands are zero (i.e. we can prove they are |
| 2767 | // unsigned inputs), turn this into a udiv. |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 2768 | if (I.getType()->isInteger()) { |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2769 | APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits())); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2770 | if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) { |
Dan Gohman | cff5509 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 2771 | // 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] | 2772 | return BinaryOperator::createUDiv(Op0, Op1, I.getName()); |
| 2773 | } |
| 2774 | } |
| 2775 | |
| 2776 | return 0; |
| 2777 | } |
| 2778 | |
| 2779 | Instruction *InstCombiner::visitFDiv(BinaryOperator &I) { |
| 2780 | return commonDivTransforms(I); |
| 2781 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2782 | |
Chris Lattner | db3f873 | 2006-03-02 06:50:58 +0000 | [diff] [blame] | 2783 | /// GetFactor - If we can prove that the specified value is at least a multiple |
| 2784 | /// of some factor, return that factor. |
| 2785 | static Constant *GetFactor(Value *V) { |
| 2786 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) |
| 2787 | return CI; |
| 2788 | |
| 2789 | // Unless we can be tricky, we know this is a multiple of 1. |
| 2790 | Constant *Result = ConstantInt::get(V->getType(), 1); |
| 2791 | |
| 2792 | Instruction *I = dyn_cast<Instruction>(V); |
| 2793 | if (!I) return Result; |
| 2794 | |
| 2795 | if (I->getOpcode() == Instruction::Mul) { |
| 2796 | // Handle multiplies by a constant, etc. |
| 2797 | return ConstantExpr::getMul(GetFactor(I->getOperand(0)), |
| 2798 | GetFactor(I->getOperand(1))); |
| 2799 | } else if (I->getOpcode() == Instruction::Shl) { |
| 2800 | // (X<<C) -> X * (1 << C) |
| 2801 | if (Constant *ShRHS = dyn_cast<Constant>(I->getOperand(1))) { |
| 2802 | ShRHS = ConstantExpr::getShl(Result, ShRHS); |
| 2803 | return ConstantExpr::getMul(GetFactor(I->getOperand(0)), ShRHS); |
| 2804 | } |
| 2805 | } else if (I->getOpcode() == Instruction::And) { |
| 2806 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 2807 | // X & 0xFFF0 is known to be a multiple of 16. |
Reid Spencer | f244252 | 2007-03-24 00:42:08 +0000 | [diff] [blame] | 2808 | uint32_t Zeros = RHS->getValue().countTrailingZeros(); |
Chris Lattner | 148083a | 2007-11-23 22:35:18 +0000 | [diff] [blame] | 2809 | if (Zeros != V->getType()->getPrimitiveSizeInBits())// don't shift by "32" |
Chris Lattner | db3f873 | 2006-03-02 06:50:58 +0000 | [diff] [blame] | 2810 | return ConstantExpr::getShl(Result, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2811 | ConstantInt::get(Result->getType(), Zeros)); |
Chris Lattner | db3f873 | 2006-03-02 06:50:58 +0000 | [diff] [blame] | 2812 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2813 | } else if (CastInst *CI = dyn_cast<CastInst>(I)) { |
Chris Lattner | db3f873 | 2006-03-02 06:50:58 +0000 | [diff] [blame] | 2814 | // Only handle int->int casts. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2815 | if (!CI->isIntegerCast()) |
| 2816 | return Result; |
| 2817 | Value *Op = CI->getOperand(0); |
| 2818 | return ConstantExpr::getCast(CI->getOpcode(), GetFactor(Op), V->getType()); |
Chris Lattner | db3f873 | 2006-03-02 06:50:58 +0000 | [diff] [blame] | 2819 | } |
| 2820 | return Result; |
| 2821 | } |
| 2822 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2823 | /// This function implements the transforms on rem instructions that work |
| 2824 | /// regardless of the kind of rem instruction it is (urem, srem, or frem). It |
| 2825 | /// is used by the visitors to those instructions. |
| 2826 | /// @brief Transforms common to all three rem instructions |
| 2827 | Instruction *InstCombiner::commonRemTransforms(BinaryOperator &I) { |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2828 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2829 | |
Chris Lattner | 50b2ca4 | 2008-02-19 06:12:18 +0000 | [diff] [blame] | 2830 | // 0 % X == 0 for integer, we don't need to preserve faults! |
Chris Lattner | 19ccd5c | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 2831 | if (Constant *LHS = dyn_cast<Constant>(Op0)) |
| 2832 | if (LHS->isNullValue()) |
| 2833 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2834 | |
Chris Lattner | 50b2ca4 | 2008-02-19 06:12:18 +0000 | [diff] [blame] | 2835 | if (isa<UndefValue>(Op0)) { // undef % X -> 0 |
| 2836 | if (I.getType()->isFPOrFPVector()) |
| 2837 | return ReplaceInstUsesWith(I, Op0); // X % undef -> undef (could be SNaN) |
Chris Lattner | 19ccd5c | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 2838 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | 50b2ca4 | 2008-02-19 06:12:18 +0000 | [diff] [blame] | 2839 | } |
Chris Lattner | 19ccd5c | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 2840 | if (isa<UndefValue>(Op1)) |
| 2841 | return ReplaceInstUsesWith(I, Op1); // X % undef -> undef |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2842 | |
| 2843 | // Handle cases involving: rem X, (select Cond, Y, Z) |
| 2844 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { |
| 2845 | // rem X, (Cond ? 0 : Y) -> rem X, Y. If the rem and the select are in |
| 2846 | // the same basic block, then we replace the select with Y, and the |
| 2847 | // condition of the select with false (if the cond value is in the same |
| 2848 | // BB). If the select has uses other than the div, this allows them to be |
| 2849 | // simplified also. |
| 2850 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(1))) |
| 2851 | if (ST->isNullValue()) { |
| 2852 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); |
| 2853 | if (CondI && CondI->getParent() == I.getParent()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2854 | UpdateValueUsesWith(CondI, ConstantInt::getFalse()); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2855 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
| 2856 | I.setOperand(1, SI->getOperand(2)); |
| 2857 | else |
| 2858 | UpdateValueUsesWith(SI, SI->getOperand(2)); |
Chris Lattner | 5b73c08 | 2004-07-06 07:01:22 +0000 | [diff] [blame] | 2859 | return &I; |
| 2860 | } |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2861 | // Likewise for: rem X, (Cond ? Y : 0) -> rem X, Y |
| 2862 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(2))) |
| 2863 | if (ST->isNullValue()) { |
| 2864 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); |
| 2865 | if (CondI && CondI->getParent() == I.getParent()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2866 | UpdateValueUsesWith(CondI, ConstantInt::getTrue()); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2867 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
| 2868 | I.setOperand(1, SI->getOperand(1)); |
| 2869 | else |
| 2870 | UpdateValueUsesWith(SI, SI->getOperand(1)); |
| 2871 | return &I; |
| 2872 | } |
Chris Lattner | 11a49f2 | 2005-11-05 07:28:37 +0000 | [diff] [blame] | 2873 | } |
Chris Lattner | 5b73c08 | 2004-07-06 07:01:22 +0000 | [diff] [blame] | 2874 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2875 | return 0; |
| 2876 | } |
| 2877 | |
| 2878 | /// This function implements the transforms common to both integer remainder |
| 2879 | /// instructions (urem and srem). It is called by the visitors to those integer |
| 2880 | /// remainder instructions. |
| 2881 | /// @brief Common integer remainder transforms |
| 2882 | Instruction *InstCombiner::commonIRemTransforms(BinaryOperator &I) { |
| 2883 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2884 | |
| 2885 | if (Instruction *common = commonRemTransforms(I)) |
| 2886 | return common; |
| 2887 | |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2888 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 19ccd5c | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 2889 | // X % 0 == undef, we don't need to preserve faults! |
| 2890 | if (RHS->equalsInt(0)) |
| 2891 | return ReplaceInstUsesWith(I, UndefValue::get(I.getType())); |
| 2892 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2893 | if (RHS->equalsInt(1)) // X % 1 == 0 |
| 2894 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2895 | |
Chris Lattner | 9794392 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 2896 | if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) { |
| 2897 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0I)) { |
| 2898 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 2899 | return R; |
| 2900 | } else if (isa<PHINode>(Op0I)) { |
| 2901 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2902 | return NV; |
Chris Lattner | 9794392 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 2903 | } |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2904 | // (X * C1) % C2 --> 0 iff C1 % C2 == 0 |
| 2905 | if (ConstantExpr::getSRem(GetFactor(Op0I), RHS)->isNullValue()) |
Chris Lattner | db3f873 | 2006-03-02 06:50:58 +0000 | [diff] [blame] | 2906 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | 9794392 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 2907 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2908 | } |
| 2909 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2910 | return 0; |
| 2911 | } |
| 2912 | |
| 2913 | Instruction *InstCombiner::visitURem(BinaryOperator &I) { |
| 2914 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2915 | |
| 2916 | if (Instruction *common = commonIRemTransforms(I)) |
| 2917 | return common; |
| 2918 | |
| 2919 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
| 2920 | // X urem C^2 -> X and C |
| 2921 | // Check to see if this is an unsigned remainder with an exact power of 2, |
| 2922 | // if so, convert to a bitwise and. |
| 2923 | if (ConstantInt *C = dyn_cast<ConstantInt>(RHS)) |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2924 | if (C->getValue().isPowerOf2()) |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2925 | return BinaryOperator::createAnd(Op0, SubOne(C)); |
| 2926 | } |
| 2927 | |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2928 | if (Instruction *RHSI = dyn_cast<Instruction>(I.getOperand(1))) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2929 | // Turn A % (C << N), where C is 2^k, into A & ((C << N)-1) |
| 2930 | if (RHSI->getOpcode() == Instruction::Shl && |
| 2931 | isa<ConstantInt>(RHSI->getOperand(0))) { |
Zhou Sheng | 0fc5095 | 2007-03-25 05:01:29 +0000 | [diff] [blame] | 2932 | if (cast<ConstantInt>(RHSI->getOperand(0))->getValue().isPowerOf2()) { |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2933 | Constant *N1 = ConstantInt::getAllOnesValue(I.getType()); |
| 2934 | Value *Add = InsertNewInstBefore(BinaryOperator::createAdd(RHSI, N1, |
| 2935 | "tmp"), I); |
| 2936 | return BinaryOperator::createAnd(Op0, Add); |
| 2937 | } |
| 2938 | } |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2939 | } |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2940 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2941 | // urem X, (select Cond, 2^C1, 2^C2) --> select Cond, (and X, C1), (and X, C2) |
| 2942 | // where C1&C2 are powers of two. |
| 2943 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { |
| 2944 | if (ConstantInt *STO = dyn_cast<ConstantInt>(SI->getOperand(1))) |
| 2945 | if (ConstantInt *SFO = dyn_cast<ConstantInt>(SI->getOperand(2))) { |
| 2946 | // STO == 0 and SFO == 0 handled above. |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2947 | if ((STO->getValue().isPowerOf2()) && |
| 2948 | (SFO->getValue().isPowerOf2())) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2949 | Value *TrueAnd = InsertNewInstBefore( |
| 2950 | BinaryOperator::createAnd(Op0, SubOne(STO), SI->getName()+".t"), I); |
| 2951 | Value *FalseAnd = InsertNewInstBefore( |
| 2952 | BinaryOperator::createAnd(Op0, SubOne(SFO), SI->getName()+".f"), I); |
| 2953 | return new SelectInst(SI->getOperand(0), TrueAnd, FalseAnd); |
| 2954 | } |
| 2955 | } |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2956 | } |
| 2957 | |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2958 | return 0; |
| 2959 | } |
| 2960 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2961 | Instruction *InstCombiner::visitSRem(BinaryOperator &I) { |
| 2962 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2963 | |
Dan Gohman | cff5509 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 2964 | // Handle the integer rem common cases |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2965 | if (Instruction *common = commonIRemTransforms(I)) |
| 2966 | return common; |
| 2967 | |
| 2968 | if (Value *RHSNeg = dyn_castNegVal(Op1)) |
| 2969 | if (!isa<ConstantInt>(RHSNeg) || |
Zhou Sheng | 0fc5095 | 2007-03-25 05:01:29 +0000 | [diff] [blame] | 2970 | cast<ConstantInt>(RHSNeg)->getValue().isStrictlyPositive()) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2971 | // X % -Y -> X % Y |
| 2972 | AddUsesToWorkList(I); |
| 2973 | I.setOperand(1, RHSNeg); |
| 2974 | return &I; |
| 2975 | } |
| 2976 | |
Dan Gohman | cff5509 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 2977 | // 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] | 2978 | // unsigned inputs), turn this into a urem. |
Dan Gohman | cff5509 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 2979 | if (I.getType()->isInteger()) { |
| 2980 | APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits())); |
| 2981 | if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) { |
| 2982 | // X srem Y -> X urem Y, iff X and Y don't have sign bit set |
| 2983 | return BinaryOperator::createURem(Op0, Op1, I.getName()); |
| 2984 | } |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2985 | } |
| 2986 | |
| 2987 | return 0; |
| 2988 | } |
| 2989 | |
| 2990 | Instruction *InstCombiner::visitFRem(BinaryOperator &I) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2991 | return commonRemTransforms(I); |
| 2992 | } |
| 2993 | |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2994 | // isMaxValueMinusOne - return true if this is Max-1 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2995 | static bool isMaxValueMinusOne(const ConstantInt *C, bool isSigned) { |
Reid Spencer | 3a2a9fb | 2007-03-19 21:10:28 +0000 | [diff] [blame] | 2996 | uint32_t TypeBits = C->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2997 | if (!isSigned) |
| 2998 | return C->getValue() == APInt::getAllOnesValue(TypeBits) - 1; |
| 2999 | return C->getValue() == APInt::getSignedMaxValue(TypeBits)-1; |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 3000 | } |
| 3001 | |
| 3002 | // isMinValuePlusOne - return true if this is Min+1 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3003 | static bool isMinValuePlusOne(const ConstantInt *C, bool isSigned) { |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 3004 | if (!isSigned) |
| 3005 | return C->getValue() == 1; // unsigned |
| 3006 | |
| 3007 | // Calculate 1111111111000000000000 |
| 3008 | uint32_t TypeBits = C->getType()->getPrimitiveSizeInBits(); |
| 3009 | return C->getValue() == APInt::getSignedMinValue(TypeBits)+1; |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 3010 | } |
| 3011 | |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 3012 | // isOneBitSet - Return true if there is exactly one bit set in the specified |
| 3013 | // constant. |
| 3014 | static bool isOneBitSet(const ConstantInt *CI) { |
Reid Spencer | 5f6a895 | 2007-03-20 00:16:52 +0000 | [diff] [blame] | 3015 | return CI->getValue().isPowerOf2(); |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 3016 | } |
| 3017 | |
Chris Lattner | b20ba0a | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 3018 | // isHighOnes - Return true if the constant is of the form 1+0+. |
| 3019 | // This is the same as lowones(~X). |
| 3020 | static bool isHighOnes(const ConstantInt *CI) { |
Zhou Sheng | 2cde46c | 2007-03-20 12:49:06 +0000 | [diff] [blame] | 3021 | return (~CI->getValue() + 1).isPowerOf2(); |
Chris Lattner | b20ba0a | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 3022 | } |
| 3023 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3024 | /// getICmpCode - Encode a icmp predicate into a three bit mask. These bits |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3025 | /// are carefully arranged to allow folding of expressions such as: |
| 3026 | /// |
| 3027 | /// (A < B) | (A > B) --> (A != B) |
| 3028 | /// |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3029 | /// Note that this is only valid if the first and second predicates have the |
| 3030 | /// same sign. Is illegal to do: (A u< B) | (A s> B) |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3031 | /// |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3032 | /// Three bits are used to represent the condition, as follows: |
| 3033 | /// 0 A > B |
| 3034 | /// 1 A == B |
| 3035 | /// 2 A < B |
| 3036 | /// |
| 3037 | /// <=> Value Definition |
| 3038 | /// 000 0 Always false |
| 3039 | /// 001 1 A > B |
| 3040 | /// 010 2 A == B |
| 3041 | /// 011 3 A >= B |
| 3042 | /// 100 4 A < B |
| 3043 | /// 101 5 A != B |
| 3044 | /// 110 6 A <= B |
| 3045 | /// 111 7 Always true |
| 3046 | /// |
| 3047 | static unsigned getICmpCode(const ICmpInst *ICI) { |
| 3048 | switch (ICI->getPredicate()) { |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3049 | // False -> 0 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3050 | case ICmpInst::ICMP_UGT: return 1; // 001 |
| 3051 | case ICmpInst::ICMP_SGT: return 1; // 001 |
| 3052 | case ICmpInst::ICMP_EQ: return 2; // 010 |
| 3053 | case ICmpInst::ICMP_UGE: return 3; // 011 |
| 3054 | case ICmpInst::ICMP_SGE: return 3; // 011 |
| 3055 | case ICmpInst::ICMP_ULT: return 4; // 100 |
| 3056 | case ICmpInst::ICMP_SLT: return 4; // 100 |
| 3057 | case ICmpInst::ICMP_NE: return 5; // 101 |
| 3058 | case ICmpInst::ICMP_ULE: return 6; // 110 |
| 3059 | case ICmpInst::ICMP_SLE: return 6; // 110 |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3060 | // True -> 7 |
| 3061 | default: |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3062 | assert(0 && "Invalid ICmp predicate!"); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3063 | return 0; |
| 3064 | } |
| 3065 | } |
| 3066 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3067 | /// getICmpValue - This is the complement of getICmpCode, which turns an |
| 3068 | /// 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] | 3069 | /// new ICmp instruction. The sign is passed in to determine which kind |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3070 | /// of predicate to use in new icmp instructions. |
| 3071 | static Value *getICmpValue(bool sign, unsigned code, Value *LHS, Value *RHS) { |
| 3072 | switch (code) { |
| 3073 | default: assert(0 && "Illegal ICmp code!"); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3074 | case 0: return ConstantInt::getFalse(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3075 | case 1: |
| 3076 | if (sign) |
| 3077 | return new ICmpInst(ICmpInst::ICMP_SGT, LHS, RHS); |
| 3078 | else |
| 3079 | return new ICmpInst(ICmpInst::ICMP_UGT, LHS, RHS); |
| 3080 | case 2: return new ICmpInst(ICmpInst::ICMP_EQ, LHS, RHS); |
| 3081 | case 3: |
| 3082 | if (sign) |
| 3083 | return new ICmpInst(ICmpInst::ICMP_SGE, LHS, RHS); |
| 3084 | else |
| 3085 | return new ICmpInst(ICmpInst::ICMP_UGE, LHS, RHS); |
| 3086 | case 4: |
| 3087 | if (sign) |
| 3088 | return new ICmpInst(ICmpInst::ICMP_SLT, LHS, RHS); |
| 3089 | else |
| 3090 | return new ICmpInst(ICmpInst::ICMP_ULT, LHS, RHS); |
| 3091 | case 5: return new ICmpInst(ICmpInst::ICMP_NE, LHS, RHS); |
| 3092 | case 6: |
| 3093 | if (sign) |
| 3094 | return new ICmpInst(ICmpInst::ICMP_SLE, LHS, RHS); |
| 3095 | else |
| 3096 | return new ICmpInst(ICmpInst::ICMP_ULE, LHS, RHS); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3097 | case 7: return ConstantInt::getTrue(); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3098 | } |
| 3099 | } |
| 3100 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3101 | static bool PredicatesFoldable(ICmpInst::Predicate p1, ICmpInst::Predicate p2) { |
| 3102 | return (ICmpInst::isSignedPredicate(p1) == ICmpInst::isSignedPredicate(p2)) || |
| 3103 | (ICmpInst::isSignedPredicate(p1) && |
| 3104 | (p2 == ICmpInst::ICMP_EQ || p2 == ICmpInst::ICMP_NE)) || |
| 3105 | (ICmpInst::isSignedPredicate(p2) && |
| 3106 | (p1 == ICmpInst::ICMP_EQ || p1 == ICmpInst::ICMP_NE)); |
| 3107 | } |
| 3108 | |
| 3109 | namespace { |
| 3110 | // FoldICmpLogical - Implements (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B) |
| 3111 | struct FoldICmpLogical { |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3112 | InstCombiner &IC; |
| 3113 | Value *LHS, *RHS; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3114 | ICmpInst::Predicate pred; |
| 3115 | FoldICmpLogical(InstCombiner &ic, ICmpInst *ICI) |
| 3116 | : IC(ic), LHS(ICI->getOperand(0)), RHS(ICI->getOperand(1)), |
| 3117 | pred(ICI->getPredicate()) {} |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3118 | bool shouldApply(Value *V) const { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3119 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(V)) |
| 3120 | if (PredicatesFoldable(pred, ICI->getPredicate())) |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 3121 | return ((ICI->getOperand(0) == LHS && ICI->getOperand(1) == RHS) || |
| 3122 | (ICI->getOperand(0) == RHS && ICI->getOperand(1) == LHS)); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3123 | return false; |
| 3124 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3125 | Instruction *apply(Instruction &Log) const { |
| 3126 | ICmpInst *ICI = cast<ICmpInst>(Log.getOperand(0)); |
| 3127 | if (ICI->getOperand(0) != LHS) { |
| 3128 | assert(ICI->getOperand(1) == LHS); |
| 3129 | ICI->swapOperands(); // Swap the LHS and RHS of the ICmp |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3130 | } |
| 3131 | |
Chris Lattner | bc1dbfc | 2007-03-13 14:27:42 +0000 | [diff] [blame] | 3132 | ICmpInst *RHSICI = cast<ICmpInst>(Log.getOperand(1)); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3133 | unsigned LHSCode = getICmpCode(ICI); |
Chris Lattner | bc1dbfc | 2007-03-13 14:27:42 +0000 | [diff] [blame] | 3134 | unsigned RHSCode = getICmpCode(RHSICI); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3135 | unsigned Code; |
| 3136 | switch (Log.getOpcode()) { |
| 3137 | case Instruction::And: Code = LHSCode & RHSCode; break; |
| 3138 | case Instruction::Or: Code = LHSCode | RHSCode; break; |
| 3139 | case Instruction::Xor: Code = LHSCode ^ RHSCode; break; |
Chris Lattner | 021c190 | 2003-09-22 20:33:34 +0000 | [diff] [blame] | 3140 | default: assert(0 && "Illegal logical opcode!"); return 0; |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3141 | } |
| 3142 | |
Chris Lattner | bc1dbfc | 2007-03-13 14:27:42 +0000 | [diff] [blame] | 3143 | bool isSigned = ICmpInst::isSignedPredicate(RHSICI->getPredicate()) || |
| 3144 | ICmpInst::isSignedPredicate(ICI->getPredicate()); |
| 3145 | |
| 3146 | Value *RV = getICmpValue(isSigned, Code, LHS, RHS); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3147 | if (Instruction *I = dyn_cast<Instruction>(RV)) |
| 3148 | return I; |
| 3149 | // Otherwise, it's a constant boolean value... |
| 3150 | return IC.ReplaceInstUsesWith(Log, RV); |
| 3151 | } |
| 3152 | }; |
Chris Lattner | d23b5ba | 2006-11-15 04:53:24 +0000 | [diff] [blame] | 3153 | } // end anonymous namespace |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3154 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3155 | // OptAndOp - This handles expressions of the form ((val OP C1) & C2). Where |
| 3156 | // 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] | 3157 | // guaranteed to be a binary operator. |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3158 | Instruction *InstCombiner::OptAndOp(Instruction *Op, |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3159 | ConstantInt *OpRHS, |
| 3160 | ConstantInt *AndRHS, |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3161 | BinaryOperator &TheAnd) { |
| 3162 | Value *X = Op->getOperand(0); |
Chris Lattner | 76f7fe2 | 2004-01-12 19:47:05 +0000 | [diff] [blame] | 3163 | Constant *Together = 0; |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3164 | if (!Op->isShift()) |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 3165 | Together = And(AndRHS, OpRHS); |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3166 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3167 | switch (Op->getOpcode()) { |
| 3168 | case Instruction::Xor: |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3169 | if (Op->hasOneUse()) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3170 | // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2) |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3171 | Instruction *And = BinaryOperator::createAnd(X, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3172 | InsertNewInstBefore(And, TheAnd); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3173 | And->takeName(Op); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3174 | return BinaryOperator::createXor(And, Together); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3175 | } |
| 3176 | break; |
| 3177 | case Instruction::Or: |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3178 | if (Together == AndRHS) // (X | C) & C --> C |
| 3179 | return ReplaceInstUsesWith(TheAnd, AndRHS); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3180 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3181 | if (Op->hasOneUse() && Together != OpRHS) { |
| 3182 | // (X | C1) & C2 --> (X | (C1&C2)) & C2 |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3183 | Instruction *Or = BinaryOperator::createOr(X, Together); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3184 | InsertNewInstBefore(Or, TheAnd); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3185 | Or->takeName(Op); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3186 | return BinaryOperator::createAnd(Or, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3187 | } |
| 3188 | break; |
| 3189 | case Instruction::Add: |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 3190 | if (Op->hasOneUse()) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3191 | // Adding a one to a single bit bit-field should be turned into an XOR |
| 3192 | // of the bit. First thing to check is to see if this AND is with a |
| 3193 | // single bit constant. |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3194 | const APInt& AndRHSV = cast<ConstantInt>(AndRHS)->getValue(); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3195 | |
| 3196 | // If there is only one bit set... |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 3197 | if (isOneBitSet(cast<ConstantInt>(AndRHS))) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3198 | // Ok, at this point, we know that we are masking the result of the |
| 3199 | // ADD down to exactly one bit. If the constant we are adding has |
| 3200 | // no bits set below this bit, then we can eliminate the ADD. |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3201 | const APInt& AddRHS = cast<ConstantInt>(OpRHS)->getValue(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3202 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3203 | // Check to see if any bits below the one bit set in AndRHSV are set. |
| 3204 | if ((AddRHS & (AndRHSV-1)) == 0) { |
| 3205 | // If not, the only thing that can effect the output of the AND is |
| 3206 | // the bit specified by AndRHSV. If that bit is set, the effect of |
| 3207 | // the XOR is to toggle the bit. If it is clear, then the ADD has |
| 3208 | // no effect. |
| 3209 | if ((AddRHS & AndRHSV) == 0) { // Bit is not set, noop |
| 3210 | TheAnd.setOperand(0, X); |
| 3211 | return &TheAnd; |
| 3212 | } else { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3213 | // Pull the XOR out of the AND. |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3214 | Instruction *NewAnd = BinaryOperator::createAnd(X, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3215 | InsertNewInstBefore(NewAnd, TheAnd); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3216 | NewAnd->takeName(Op); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3217 | return BinaryOperator::createXor(NewAnd, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3218 | } |
| 3219 | } |
| 3220 | } |
| 3221 | } |
| 3222 | break; |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 3223 | |
| 3224 | case Instruction::Shl: { |
| 3225 | // We know that the AND will not produce any of the bits shifted in, so if |
| 3226 | // the anded constant includes them, clear them now! |
| 3227 | // |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3228 | uint32_t BitWidth = AndRHS->getType()->getBitWidth(); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3229 | uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3230 | APInt ShlMask(APInt::getHighBitsSet(BitWidth, BitWidth-OpRHSVal)); |
| 3231 | ConstantInt *CI = ConstantInt::get(AndRHS->getValue() & ShlMask); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3232 | |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3233 | if (CI->getValue() == ShlMask) { |
| 3234 | // Masking out bits that the shift already masks |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 3235 | return ReplaceInstUsesWith(TheAnd, Op); // No need for the and. |
| 3236 | } else if (CI != AndRHS) { // Reducing bits set in and. |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 3237 | TheAnd.setOperand(1, CI); |
| 3238 | return &TheAnd; |
| 3239 | } |
| 3240 | break; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3241 | } |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 3242 | case Instruction::LShr: |
| 3243 | { |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 3244 | // We know that the AND will not produce any of the bits shifted in, so if |
| 3245 | // the anded constant includes them, clear them now! This only applies to |
| 3246 | // unsigned shifts, because a signed shr may bring in set bits! |
| 3247 | // |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3248 | uint32_t BitWidth = AndRHS->getType()->getBitWidth(); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3249 | uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3250 | APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal)); |
| 3251 | ConstantInt *CI = ConstantInt::get(AndRHS->getValue() & ShrMask); |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 3252 | |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3253 | if (CI->getValue() == ShrMask) { |
| 3254 | // Masking out bits that the shift already masks. |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 3255 | return ReplaceInstUsesWith(TheAnd, Op); |
| 3256 | } else if (CI != AndRHS) { |
| 3257 | TheAnd.setOperand(1, CI); // Reduce bits set in and cst. |
| 3258 | return &TheAnd; |
| 3259 | } |
| 3260 | break; |
| 3261 | } |
| 3262 | case Instruction::AShr: |
| 3263 | // Signed shr. |
| 3264 | // See if this is shifting in some sign extension, then masking it out |
| 3265 | // with an and. |
| 3266 | if (Op->hasOneUse()) { |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3267 | uint32_t BitWidth = AndRHS->getType()->getBitWidth(); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3268 | uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3269 | APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal)); |
| 3270 | Constant *C = ConstantInt::get(AndRHS->getValue() & ShrMask); |
Reid Spencer | 7eb7638 | 2006-12-13 17:19:09 +0000 | [diff] [blame] | 3271 | if (C == AndRHS) { // Masking out bits shifted in. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 3272 | // (Val ashr C1) & C2 -> (Val lshr C1) & C2 |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 3273 | // Make the argument unsigned. |
| 3274 | Value *ShVal = Op->getOperand(0); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3275 | ShVal = InsertNewInstBefore( |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 3276 | BinaryOperator::createLShr(ShVal, OpRHS, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3277 | Op->getName()), TheAnd); |
Reid Spencer | 7eb7638 | 2006-12-13 17:19:09 +0000 | [diff] [blame] | 3278 | return BinaryOperator::createAnd(ShVal, AndRHS, TheAnd.getName()); |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 3279 | } |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 3280 | } |
| 3281 | break; |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3282 | } |
| 3283 | return 0; |
| 3284 | } |
| 3285 | |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 3286 | |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3287 | /// InsertRangeTest - Emit a computation of: (V >= Lo && V < Hi) if Inside is |
| 3288 | /// 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] | 3289 | /// (V-Lo) <u Hi-Lo. This method expects that Lo <= Hi. isSigned indicates |
| 3290 | /// 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] | 3291 | /// insert new instructions. |
| 3292 | Instruction *InstCombiner::InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3293 | bool isSigned, bool Inside, |
| 3294 | Instruction &IB) { |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3295 | assert(cast<ConstantInt>(ConstantExpr::getICmp((isSigned ? |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 3296 | ICmpInst::ICMP_SLE:ICmpInst::ICMP_ULE), Lo, Hi))->getZExtValue() && |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3297 | "Lo is not <= Hi in range emission code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3298 | |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3299 | if (Inside) { |
| 3300 | if (Lo == Hi) // Trivially false. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3301 | return new ICmpInst(ICmpInst::ICMP_NE, V, V); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3302 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3303 | // V >= Min && V < Hi --> V < Hi |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3304 | if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) { |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 3305 | ICmpInst::Predicate pred = (isSigned ? |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3306 | ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT); |
| 3307 | return new ICmpInst(pred, V, Hi); |
| 3308 | } |
| 3309 | |
| 3310 | // Emit V-Lo <u Hi-Lo |
| 3311 | Constant *NegLo = ConstantExpr::getNeg(Lo); |
| 3312 | Instruction *Add = BinaryOperator::createAdd(V, NegLo, V->getName()+".off"); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3313 | InsertNewInstBefore(Add, IB); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3314 | Constant *UpperBound = ConstantExpr::getAdd(NegLo, Hi); |
| 3315 | return new ICmpInst(ICmpInst::ICMP_ULT, Add, UpperBound); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3316 | } |
| 3317 | |
| 3318 | if (Lo == Hi) // Trivially true. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3319 | return new ICmpInst(ICmpInst::ICMP_EQ, V, V); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3320 | |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 3321 | // V < Min || V >= Hi -> V > Hi-1 |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3322 | Hi = SubOne(cast<ConstantInt>(Hi)); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3323 | if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3324 | ICmpInst::Predicate pred = (isSigned ? |
| 3325 | ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT); |
| 3326 | return new ICmpInst(pred, V, Hi); |
| 3327 | } |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3328 | |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 3329 | // Emit V-Lo >u Hi-1-Lo |
| 3330 | // Note that Hi has already had one subtracted from it, above. |
| 3331 | ConstantInt *NegLo = cast<ConstantInt>(ConstantExpr::getNeg(Lo)); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3332 | Instruction *Add = BinaryOperator::createAdd(V, NegLo, V->getName()+".off"); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3333 | InsertNewInstBefore(Add, IB); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3334 | Constant *LowerBound = ConstantExpr::getAdd(NegLo, Hi); |
| 3335 | return new ICmpInst(ICmpInst::ICMP_UGT, Add, LowerBound); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3336 | } |
| 3337 | |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3338 | // isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with |
| 3339 | // any number of 0s on either side. The 1s are allowed to wrap from LSB to |
| 3340 | // MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is |
| 3341 | // not, since all 1s are not contiguous. |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 3342 | static bool isRunOfOnes(ConstantInt *Val, uint32_t &MB, uint32_t &ME) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3343 | const APInt& V = Val->getValue(); |
Reid Spencer | f244252 | 2007-03-24 00:42:08 +0000 | [diff] [blame] | 3344 | uint32_t BitWidth = Val->getType()->getBitWidth(); |
| 3345 | if (!APIntOps::isShiftedMask(BitWidth, V)) return false; |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3346 | |
| 3347 | // look for the first zero bit after the run of ones |
Reid Spencer | f244252 | 2007-03-24 00:42:08 +0000 | [diff] [blame] | 3348 | MB = BitWidth - ((V - 1) ^ V).countLeadingZeros(); |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3349 | // look for the first non-zero bit |
Reid Spencer | f244252 | 2007-03-24 00:42:08 +0000 | [diff] [blame] | 3350 | ME = V.getActiveBits(); |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3351 | return true; |
| 3352 | } |
| 3353 | |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3354 | /// FoldLogicalPlusAnd - This is part of an expression (LHS +/- RHS) & Mask, |
| 3355 | /// where isSub determines whether the operator is a sub. If we can fold one of |
| 3356 | /// the following xforms: |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3357 | /// |
| 3358 | /// ((A & N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == Mask |
| 3359 | /// ((A | N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0 |
| 3360 | /// ((A ^ N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0 |
| 3361 | /// |
| 3362 | /// return (A +/- B). |
| 3363 | /// |
| 3364 | Value *InstCombiner::FoldLogicalPlusAnd(Value *LHS, Value *RHS, |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3365 | ConstantInt *Mask, bool isSub, |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3366 | Instruction &I) { |
| 3367 | Instruction *LHSI = dyn_cast<Instruction>(LHS); |
| 3368 | if (!LHSI || LHSI->getNumOperands() != 2 || |
| 3369 | !isa<ConstantInt>(LHSI->getOperand(1))) return 0; |
| 3370 | |
| 3371 | ConstantInt *N = cast<ConstantInt>(LHSI->getOperand(1)); |
| 3372 | |
| 3373 | switch (LHSI->getOpcode()) { |
| 3374 | default: return 0; |
| 3375 | case Instruction::And: |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 3376 | if (And(N, Mask) == Mask) { |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3377 | // 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] | 3378 | if ((Mask->getValue().countLeadingZeros() + |
| 3379 | Mask->getValue().countPopulation()) == |
| 3380 | Mask->getValue().getBitWidth()) |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3381 | break; |
| 3382 | |
| 3383 | // Otherwise, if Mask is 0+1+0+, and if B is known to have the low 0+ |
| 3384 | // part, we don't need any explicit masks to take them out of A. If that |
| 3385 | // is all N is, ignore it. |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 3386 | uint32_t MB = 0, ME = 0; |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3387 | if (isRunOfOnes(Mask, MB, ME)) { // begin/end bit of run, inclusive |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 3388 | uint32_t BitWidth = cast<IntegerType>(RHS->getType())->getBitWidth(); |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3389 | APInt Mask(APInt::getLowBitsSet(BitWidth, MB-1)); |
Chris Lattner | 3bedbd9 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3390 | if (MaskedValueIsZero(RHS, Mask)) |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3391 | break; |
| 3392 | } |
| 3393 | } |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3394 | return 0; |
| 3395 | case Instruction::Or: |
| 3396 | case Instruction::Xor: |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3397 | // 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] | 3398 | if ((Mask->getValue().countLeadingZeros() + |
| 3399 | Mask->getValue().countPopulation()) == Mask->getValue().getBitWidth() |
Reid Spencer | 6eb0d99 | 2007-03-26 23:58:26 +0000 | [diff] [blame] | 3400 | && And(N, Mask)->isZero()) |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3401 | break; |
| 3402 | return 0; |
| 3403 | } |
| 3404 | |
| 3405 | Instruction *New; |
| 3406 | if (isSub) |
| 3407 | New = BinaryOperator::createSub(LHSI->getOperand(0), RHS, "fold"); |
| 3408 | else |
| 3409 | New = BinaryOperator::createAdd(LHSI->getOperand(0), RHS, "fold"); |
| 3410 | return InsertNewInstBefore(New, I); |
| 3411 | } |
| 3412 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3413 | Instruction *InstCombiner::visitAnd(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 3414 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3415 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3416 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3417 | if (isa<UndefValue>(Op1)) // X & undef -> 0 |
| 3418 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 3419 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3420 | // and X, X = X |
| 3421 | if (Op0 == Op1) |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 3422 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3423 | |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3424 | // 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] | 3425 | // purpose is to compute bits we don't care about. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 3426 | if (!isa<VectorType>(I.getType())) { |
Reid Spencer | a03d45f | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 3427 | uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth(); |
| 3428 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 3429 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth), |
Chris Lattner | 696ee0a | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3430 | KnownZero, KnownOne)) |
Reid Spencer | 6eb0d99 | 2007-03-26 23:58:26 +0000 | [diff] [blame] | 3431 | return &I; |
Chris Lattner | 696ee0a | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3432 | } else { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 3433 | if (ConstantVector *CP = dyn_cast<ConstantVector>(Op1)) { |
Chris Lattner | 041a6c9 | 2007-06-15 05:26:55 +0000 | [diff] [blame] | 3434 | if (CP->isAllOnesValue()) // X & <-1,-1> -> X |
Chris Lattner | 696ee0a | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3435 | return ReplaceInstUsesWith(I, I.getOperand(0)); |
Chris Lattner | 041a6c9 | 2007-06-15 05:26:55 +0000 | [diff] [blame] | 3436 | } else if (isa<ConstantAggregateZero>(Op1)) { |
| 3437 | return ReplaceInstUsesWith(I, Op1); // X & <0,0> -> <0,0> |
Chris Lattner | 696ee0a | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3438 | } |
| 3439 | } |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 3440 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3441 | if (ConstantInt *AndRHS = dyn_cast<ConstantInt>(Op1)) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3442 | const APInt& AndRHSMask = AndRHS->getValue(); |
| 3443 | APInt NotAndRHS(~AndRHSMask); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3444 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3445 | // Optimize a variety of ((val OP C1) & C2) combinations... |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3446 | if (isa<BinaryOperator>(Op0)) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3447 | Instruction *Op0I = cast<Instruction>(Op0); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3448 | Value *Op0LHS = Op0I->getOperand(0); |
| 3449 | Value *Op0RHS = Op0I->getOperand(1); |
| 3450 | switch (Op0I->getOpcode()) { |
| 3451 | case Instruction::Xor: |
| 3452 | case Instruction::Or: |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 3453 | // If the mask is only needed on one incoming arm, push it up. |
| 3454 | if (Op0I->hasOneUse()) { |
| 3455 | if (MaskedValueIsZero(Op0LHS, NotAndRHS)) { |
| 3456 | // Not masking anything out for the LHS, move to RHS. |
| 3457 | Instruction *NewRHS = BinaryOperator::createAnd(Op0RHS, AndRHS, |
| 3458 | Op0RHS->getName()+".masked"); |
| 3459 | InsertNewInstBefore(NewRHS, I); |
| 3460 | return BinaryOperator::create( |
| 3461 | cast<BinaryOperator>(Op0I)->getOpcode(), Op0LHS, NewRHS); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3462 | } |
Chris Lattner | 3bedbd9 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3463 | if (!isa<Constant>(Op0RHS) && |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 3464 | MaskedValueIsZero(Op0RHS, NotAndRHS)) { |
| 3465 | // Not masking anything out for the RHS, move to LHS. |
| 3466 | Instruction *NewLHS = BinaryOperator::createAnd(Op0LHS, AndRHS, |
| 3467 | Op0LHS->getName()+".masked"); |
| 3468 | InsertNewInstBefore(NewLHS, I); |
| 3469 | return BinaryOperator::create( |
| 3470 | cast<BinaryOperator>(Op0I)->getOpcode(), NewLHS, Op0RHS); |
| 3471 | } |
| 3472 | } |
| 3473 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3474 | break; |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3475 | case Instruction::Add: |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3476 | // ((A & N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == AndRHS. |
| 3477 | // ((A | N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0 |
| 3478 | // ((A ^ N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0 |
| 3479 | if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, false, I)) |
| 3480 | return BinaryOperator::createAnd(V, AndRHS); |
| 3481 | if (Value *V = FoldLogicalPlusAnd(Op0RHS, Op0LHS, AndRHS, false, I)) |
| 3482 | return BinaryOperator::createAnd(V, AndRHS); // Add commutes |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3483 | break; |
| 3484 | |
| 3485 | case Instruction::Sub: |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3486 | // ((A & N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == AndRHS. |
| 3487 | // ((A | N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0 |
| 3488 | // ((A ^ N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0 |
| 3489 | if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, true, I)) |
| 3490 | return BinaryOperator::createAnd(V, AndRHS); |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3491 | break; |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3492 | } |
| 3493 | |
Chris Lattner | 5840326 | 2003-07-23 19:25:52 +0000 | [diff] [blame] | 3494 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3495 | if (Instruction *Res = OptAndOp(Op0I, Op0CI, AndRHS, I)) |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3496 | return Res; |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3497 | } else if (CastInst *CI = dyn_cast<CastInst>(Op0)) { |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3498 | // If this is an integer truncation or change from signed-to-unsigned, and |
| 3499 | // if the source is an and/or with immediate, transform it. This |
| 3500 | // frequently occurs for bitfield accesses. |
| 3501 | if (Instruction *CastOp = dyn_cast<Instruction>(CI->getOperand(0))) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3502 | if ((isa<TruncInst>(CI) || isa<BitCastInst>(CI)) && |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3503 | CastOp->getNumOperands() == 2) |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 3504 | if (ConstantInt *AndCI = dyn_cast<ConstantInt>(CastOp->getOperand(1))) { |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3505 | if (CastOp->getOpcode() == Instruction::And) { |
| 3506 | // Change: and (cast (and X, C1) to T), C2 |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3507 | // into : and (cast X to T), trunc_or_bitcast(C1)&C2 |
| 3508 | // This will fold the two constants together, which may allow |
| 3509 | // other simplifications. |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 3510 | Instruction *NewCast = CastInst::createTruncOrBitCast( |
| 3511 | CastOp->getOperand(0), I.getType(), |
| 3512 | CastOp->getName()+".shrunk"); |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3513 | NewCast = InsertNewInstBefore(NewCast, I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3514 | // trunc_or_bitcast(C1)&C2 |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 3515 | Constant *C3 = ConstantExpr::getTruncOrBitCast(AndCI,I.getType()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3516 | C3 = ConstantExpr::getAnd(C3, AndRHS); |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3517 | return BinaryOperator::createAnd(NewCast, C3); |
| 3518 | } else if (CastOp->getOpcode() == Instruction::Or) { |
| 3519 | // Change: and (cast (or X, C1) to T), C2 |
| 3520 | // into : trunc(C1)&C2 iff trunc(C1)&C2 == C2 |
Chris Lattner | bb4e7b2 | 2006-12-12 19:11:20 +0000 | [diff] [blame] | 3521 | Constant *C3 = ConstantExpr::getTruncOrBitCast(AndCI,I.getType()); |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3522 | if (ConstantExpr::getAnd(C3, AndRHS) == AndRHS) // trunc(C1)&C2 |
| 3523 | return ReplaceInstUsesWith(I, AndRHS); |
| 3524 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 3525 | } |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3526 | } |
Chris Lattner | 06782f8 | 2003-07-23 19:36:21 +0000 | [diff] [blame] | 3527 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3528 | |
| 3529 | // Try to fold constant and into select arguments. |
| 3530 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3531 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3532 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 3533 | if (isa<PHINode>(Op0)) |
| 3534 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 3535 | return NV; |
Chris Lattner | c6a8aff | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 3536 | } |
| 3537 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 3538 | Value *Op0NotVal = dyn_castNotVal(Op0); |
| 3539 | Value *Op1NotVal = dyn_castNotVal(Op1); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3540 | |
Chris Lattner | 5b62aa7 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 3541 | if (Op0NotVal == Op1 || Op1NotVal == Op0) // A & ~A == ~A & A == 0 |
| 3542 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 3543 | |
Misha Brukman | cb6267b | 2004-07-30 12:50:08 +0000 | [diff] [blame] | 3544 | // (~A & ~B) == (~(A | B)) - De Morgan's Law |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 3545 | if (Op0NotVal && Op1NotVal && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3546 | Instruction *Or = BinaryOperator::createOr(Op0NotVal, Op1NotVal, |
| 3547 | I.getName()+".demorgan"); |
Chris Lattner | c6a8aff | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 3548 | InsertNewInstBefore(Or, I); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3549 | return BinaryOperator::createNot(Or); |
| 3550 | } |
Chris Lattner | 2082ad9 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3551 | |
| 3552 | { |
Chris Lattner | 003b620 | 2007-06-15 05:58:24 +0000 | [diff] [blame] | 3553 | Value *A = 0, *B = 0, *C = 0, *D = 0; |
| 3554 | if (match(Op0, m_Or(m_Value(A), m_Value(B)))) { |
Chris Lattner | 2082ad9 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3555 | if (A == Op1 || B == Op1) // (A | ?) & A --> A |
| 3556 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 003b620 | 2007-06-15 05:58:24 +0000 | [diff] [blame] | 3557 | |
| 3558 | // (A|B) & ~(A&B) -> A^B |
| 3559 | if (match(Op1, m_Not(m_And(m_Value(C), m_Value(D))))) { |
| 3560 | if ((A == C && B == D) || (A == D && B == C)) |
| 3561 | return BinaryOperator::createXor(A, B); |
| 3562 | } |
| 3563 | } |
| 3564 | |
| 3565 | if (match(Op1, m_Or(m_Value(A), m_Value(B)))) { |
Chris Lattner | 2082ad9 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3566 | if (A == Op0 || B == Op0) // A & (A | ?) --> A |
| 3567 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 003b620 | 2007-06-15 05:58:24 +0000 | [diff] [blame] | 3568 | |
| 3569 | // ~(A&B) & (A|B) -> A^B |
| 3570 | if (match(Op0, m_Not(m_And(m_Value(C), m_Value(D))))) { |
| 3571 | if ((A == C && B == D) || (A == D && B == C)) |
| 3572 | return BinaryOperator::createXor(A, B); |
| 3573 | } |
| 3574 | } |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 3575 | |
| 3576 | if (Op0->hasOneUse() && |
| 3577 | match(Op0, m_Xor(m_Value(A), m_Value(B)))) { |
| 3578 | if (A == Op1) { // (A^B)&A -> A&(A^B) |
| 3579 | I.swapOperands(); // Simplify below |
| 3580 | std::swap(Op0, Op1); |
| 3581 | } else if (B == Op1) { // (A^B)&B -> B&(B^A) |
| 3582 | cast<BinaryOperator>(Op0)->swapOperands(); |
| 3583 | I.swapOperands(); // Simplify below |
| 3584 | std::swap(Op0, Op1); |
| 3585 | } |
| 3586 | } |
| 3587 | if (Op1->hasOneUse() && |
| 3588 | match(Op1, m_Xor(m_Value(A), m_Value(B)))) { |
| 3589 | if (B == Op0) { // B&(A^B) -> B&(B^A) |
| 3590 | cast<BinaryOperator>(Op1)->swapOperands(); |
| 3591 | std::swap(A, B); |
| 3592 | } |
| 3593 | if (A == Op0) { // A&(A^B) -> A & ~B |
| 3594 | Instruction *NotB = BinaryOperator::createNot(B, "tmp"); |
| 3595 | InsertNewInstBefore(NotB, I); |
| 3596 | return BinaryOperator::createAnd(A, NotB); |
| 3597 | } |
| 3598 | } |
Chris Lattner | 2082ad9 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3599 | } |
| 3600 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3601 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(Op1)) { |
| 3602 | // (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B) |
| 3603 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS))) |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3604 | return R; |
| 3605 | |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3606 | Value *LHSVal, *RHSVal; |
| 3607 | ConstantInt *LHSCst, *RHSCst; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3608 | ICmpInst::Predicate LHSCC, RHSCC; |
| 3609 | if (match(Op0, m_ICmp(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst)))) |
| 3610 | if (match(RHS, m_ICmp(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst)))) |
| 3611 | if (LHSVal == RHSVal && // Found (X icmp C1) & (X icmp C2) |
| 3612 | // ICMP_[GL]E X, CST is folded to ICMP_[GL]T elsewhere. |
| 3613 | LHSCC != ICmpInst::ICMP_UGE && LHSCC != ICmpInst::ICMP_ULE && |
| 3614 | RHSCC != ICmpInst::ICMP_UGE && RHSCC != ICmpInst::ICMP_ULE && |
| 3615 | LHSCC != ICmpInst::ICMP_SGE && LHSCC != ICmpInst::ICMP_SLE && |
Chris Lattner | eec8b9a | 2007-11-22 23:47:13 +0000 | [diff] [blame] | 3616 | RHSCC != ICmpInst::ICMP_SGE && RHSCC != ICmpInst::ICMP_SLE && |
| 3617 | |
| 3618 | // Don't try to fold ICMP_SLT + ICMP_ULT. |
| 3619 | (ICmpInst::isEquality(LHSCC) || ICmpInst::isEquality(RHSCC) || |
| 3620 | ICmpInst::isSignedPredicate(LHSCC) == |
| 3621 | ICmpInst::isSignedPredicate(RHSCC))) { |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3622 | // Ensure that the larger constant is on the RHS. |
Chris Lattner | ee2b7a4 | 2008-01-13 20:59:02 +0000 | [diff] [blame] | 3623 | ICmpInst::Predicate GT; |
| 3624 | if (ICmpInst::isSignedPredicate(LHSCC) || |
| 3625 | (ICmpInst::isEquality(LHSCC) && |
| 3626 | ICmpInst::isSignedPredicate(RHSCC))) |
| 3627 | GT = ICmpInst::ICMP_SGT; |
| 3628 | else |
| 3629 | GT = ICmpInst::ICMP_UGT; |
| 3630 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3631 | Constant *Cmp = ConstantExpr::getICmp(GT, LHSCst, RHSCst); |
| 3632 | ICmpInst *LHS = cast<ICmpInst>(Op0); |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 3633 | if (cast<ConstantInt>(Cmp)->getZExtValue()) { |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3634 | std::swap(LHS, RHS); |
| 3635 | std::swap(LHSCst, RHSCst); |
| 3636 | std::swap(LHSCC, RHSCC); |
| 3637 | } |
| 3638 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3639 | // At this point, we know we have have two icmp instructions |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3640 | // comparing a value against two constants and and'ing the result |
| 3641 | // together. Because of the above check, we know that we only have |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3642 | // icmp eq, icmp ne, icmp [su]lt, and icmp [SU]gt here. We also know |
| 3643 | // (from the FoldICmpLogical check above), that the two constants |
| 3644 | // are not equal and that the larger constant is on the RHS |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3645 | assert(LHSCst != RHSCst && "Compares not folded above?"); |
| 3646 | |
| 3647 | switch (LHSCC) { |
| 3648 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3649 | case ICmpInst::ICMP_EQ: |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3650 | switch (RHSCC) { |
| 3651 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3652 | case ICmpInst::ICMP_EQ: // (X == 13 & X == 15) -> false |
| 3653 | case ICmpInst::ICMP_UGT: // (X == 13 & X > 15) -> false |
| 3654 | case ICmpInst::ICMP_SGT: // (X == 13 & X > 15) -> false |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3655 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3656 | case ICmpInst::ICMP_NE: // (X == 13 & X != 15) -> X == 13 |
| 3657 | case ICmpInst::ICMP_ULT: // (X == 13 & X < 15) -> X == 13 |
| 3658 | case ICmpInst::ICMP_SLT: // (X == 13 & X < 15) -> X == 13 |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3659 | return ReplaceInstUsesWith(I, LHS); |
| 3660 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3661 | case ICmpInst::ICMP_NE: |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3662 | switch (RHSCC) { |
| 3663 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3664 | case ICmpInst::ICMP_ULT: |
| 3665 | if (LHSCst == SubOne(RHSCst)) // (X != 13 & X u< 14) -> X < 13 |
| 3666 | return new ICmpInst(ICmpInst::ICMP_ULT, LHSVal, LHSCst); |
| 3667 | break; // (X != 13 & X u< 15) -> no change |
| 3668 | case ICmpInst::ICMP_SLT: |
| 3669 | if (LHSCst == SubOne(RHSCst)) // (X != 13 & X s< 14) -> X < 13 |
| 3670 | return new ICmpInst(ICmpInst::ICMP_SLT, LHSVal, LHSCst); |
| 3671 | break; // (X != 13 & X s< 15) -> no change |
| 3672 | case ICmpInst::ICMP_EQ: // (X != 13 & X == 15) -> X == 15 |
| 3673 | case ICmpInst::ICMP_UGT: // (X != 13 & X u> 15) -> X u> 15 |
| 3674 | case ICmpInst::ICMP_SGT: // (X != 13 & X s> 15) -> X s> 15 |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3675 | return ReplaceInstUsesWith(I, RHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3676 | case ICmpInst::ICMP_NE: |
| 3677 | if (LHSCst == SubOne(RHSCst)){// (X != 13 & X != 14) -> X-13 >u 1 |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3678 | Constant *AddCST = ConstantExpr::getNeg(LHSCst); |
| 3679 | Instruction *Add = BinaryOperator::createAdd(LHSVal, AddCST, |
| 3680 | LHSVal->getName()+".off"); |
| 3681 | InsertNewInstBefore(Add, I); |
Chris Lattner | 424db02 | 2007-01-27 23:08:34 +0000 | [diff] [blame] | 3682 | return new ICmpInst(ICmpInst::ICMP_UGT, Add, |
| 3683 | ConstantInt::get(Add->getType(), 1)); |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3684 | } |
| 3685 | break; // (X != 13 & X != 15) -> no change |
| 3686 | } |
| 3687 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3688 | case ICmpInst::ICMP_ULT: |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3689 | switch (RHSCC) { |
| 3690 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3691 | case ICmpInst::ICMP_EQ: // (X u< 13 & X == 15) -> false |
| 3692 | case ICmpInst::ICMP_UGT: // (X u< 13 & X u> 15) -> false |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3693 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3694 | case ICmpInst::ICMP_SGT: // (X u< 13 & X s> 15) -> no change |
| 3695 | break; |
| 3696 | case ICmpInst::ICMP_NE: // (X u< 13 & X != 15) -> X u< 13 |
| 3697 | 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] | 3698 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3699 | case ICmpInst::ICMP_SLT: // (X u< 13 & X s< 15) -> no change |
| 3700 | break; |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3701 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3702 | break; |
| 3703 | case ICmpInst::ICMP_SLT: |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3704 | switch (RHSCC) { |
| 3705 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3706 | case ICmpInst::ICMP_EQ: // (X s< 13 & X == 15) -> false |
| 3707 | case ICmpInst::ICMP_SGT: // (X s< 13 & X s> 15) -> false |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3708 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3709 | case ICmpInst::ICMP_UGT: // (X s< 13 & X u> 15) -> no change |
| 3710 | break; |
| 3711 | case ICmpInst::ICMP_NE: // (X s< 13 & X != 15) -> X < 13 |
| 3712 | case ICmpInst::ICMP_SLT: // (X s< 13 & X s< 15) -> X < 13 |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3713 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3714 | case ICmpInst::ICMP_ULT: // (X s< 13 & X u< 15) -> no change |
| 3715 | break; |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3716 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3717 | break; |
| 3718 | case ICmpInst::ICMP_UGT: |
| 3719 | switch (RHSCC) { |
| 3720 | default: assert(0 && "Unknown integer condition code!"); |
| 3721 | case ICmpInst::ICMP_EQ: // (X u> 13 & X == 15) -> X > 13 |
| 3722 | return ReplaceInstUsesWith(I, LHS); |
| 3723 | case ICmpInst::ICMP_UGT: // (X u> 13 & X u> 15) -> X u> 15 |
| 3724 | return ReplaceInstUsesWith(I, RHS); |
| 3725 | case ICmpInst::ICMP_SGT: // (X u> 13 & X s> 15) -> no change |
| 3726 | break; |
| 3727 | case ICmpInst::ICMP_NE: |
| 3728 | if (RHSCst == AddOne(LHSCst)) // (X u> 13 & X != 14) -> X u> 14 |
| 3729 | return new ICmpInst(LHSCC, LHSVal, RHSCst); |
| 3730 | break; // (X u> 13 & X != 15) -> no change |
| 3731 | case ICmpInst::ICMP_ULT: // (X u> 13 & X u< 15) ->(X-14) <u 1 |
| 3732 | return InsertRangeTest(LHSVal, AddOne(LHSCst), RHSCst, false, |
| 3733 | true, I); |
| 3734 | case ICmpInst::ICMP_SLT: // (X u> 13 & X s< 15) -> no change |
| 3735 | break; |
| 3736 | } |
| 3737 | break; |
| 3738 | case ICmpInst::ICMP_SGT: |
| 3739 | switch (RHSCC) { |
| 3740 | default: assert(0 && "Unknown integer condition code!"); |
Chris Lattner | a7d1ab0 | 2007-11-16 06:04:17 +0000 | [diff] [blame] | 3741 | case ICmpInst::ICMP_EQ: // (X s> 13 & X == 15) -> X == 15 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3742 | case ICmpInst::ICMP_SGT: // (X s> 13 & X s> 15) -> X s> 15 |
| 3743 | return ReplaceInstUsesWith(I, RHS); |
| 3744 | case ICmpInst::ICMP_UGT: // (X s> 13 & X u> 15) -> no change |
| 3745 | break; |
| 3746 | case ICmpInst::ICMP_NE: |
| 3747 | if (RHSCst == AddOne(LHSCst)) // (X s> 13 & X != 14) -> X s> 14 |
| 3748 | return new ICmpInst(LHSCC, LHSVal, RHSCst); |
| 3749 | break; // (X s> 13 & X != 15) -> no change |
| 3750 | case ICmpInst::ICMP_SLT: // (X s> 13 & X s< 15) ->(X-14) s< 1 |
| 3751 | return InsertRangeTest(LHSVal, AddOne(LHSCst), RHSCst, true, |
| 3752 | true, I); |
| 3753 | case ICmpInst::ICMP_ULT: // (X s> 13 & X u< 15) -> no change |
| 3754 | break; |
| 3755 | } |
| 3756 | break; |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3757 | } |
| 3758 | } |
| 3759 | } |
| 3760 | |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3761 | // fold (and (cast A), (cast B)) -> (cast (and A, B)) |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3762 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) |
| 3763 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) |
| 3764 | if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind ? |
| 3765 | const Type *SrcTy = Op0C->getOperand(0)->getType(); |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 3766 | if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() && |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3767 | // 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] | 3768 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), |
| 3769 | I.getType(), TD) && |
| 3770 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), |
| 3771 | I.getType(), TD)) { |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3772 | Instruction *NewOp = BinaryOperator::createAnd(Op0C->getOperand(0), |
| 3773 | Op1C->getOperand(0), |
| 3774 | I.getName()); |
| 3775 | InsertNewInstBefore(NewOp, I); |
| 3776 | return CastInst::create(Op0C->getOpcode(), NewOp, I.getType()); |
| 3777 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3778 | } |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 3779 | |
| 3780 | // (X >> Z) & (Y >> Z) -> (X&Y) >> Z for all shifts. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3781 | if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) { |
| 3782 | if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0)) |
| 3783 | if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() && |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 3784 | SI0->getOperand(1) == SI1->getOperand(1) && |
| 3785 | (SI0->hasOneUse() || SI1->hasOneUse())) { |
| 3786 | Instruction *NewOp = |
| 3787 | InsertNewInstBefore(BinaryOperator::createAnd(SI0->getOperand(0), |
| 3788 | SI1->getOperand(0), |
| 3789 | SI0->getName()), I); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3790 | return BinaryOperator::create(SI1->getOpcode(), NewOp, |
| 3791 | SI1->getOperand(1)); |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 3792 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3793 | } |
| 3794 | |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 3795 | // (fcmp ord x, c) & (fcmp ord y, c) -> (fcmp ord x, y) |
| 3796 | if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0))) { |
| 3797 | if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1))) { |
| 3798 | if (LHS->getPredicate() == FCmpInst::FCMP_ORD && |
| 3799 | RHS->getPredicate() == FCmpInst::FCMP_ORD) |
| 3800 | if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1))) |
| 3801 | if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) { |
| 3802 | // If either of the constants are nans, then the whole thing returns |
| 3803 | // false. |
Chris Lattner | be3e348 | 2007-10-24 18:54:45 +0000 | [diff] [blame] | 3804 | if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN()) |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 3805 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
| 3806 | return new FCmpInst(FCmpInst::FCMP_ORD, LHS->getOperand(0), |
| 3807 | RHS->getOperand(0)); |
| 3808 | } |
| 3809 | } |
| 3810 | } |
| 3811 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3812 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3813 | } |
| 3814 | |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3815 | /// CollectBSwapParts - Look to see if the specified value defines a single byte |
| 3816 | /// in the result. If it does, and if the specified byte hasn't been filled in |
| 3817 | /// yet, fill it in and return false. |
Chris Lattner | 535014f | 2007-02-15 22:52:10 +0000 | [diff] [blame] | 3818 | static bool CollectBSwapParts(Value *V, SmallVector<Value*, 8> &ByteValues) { |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3819 | Instruction *I = dyn_cast<Instruction>(V); |
| 3820 | if (I == 0) return true; |
| 3821 | |
| 3822 | // If this is an or instruction, it is an inner node of the bswap. |
| 3823 | if (I->getOpcode() == Instruction::Or) |
| 3824 | return CollectBSwapParts(I->getOperand(0), ByteValues) || |
| 3825 | CollectBSwapParts(I->getOperand(1), ByteValues); |
| 3826 | |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3827 | uint32_t BitWidth = I->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3828 | // If this is a shift by a constant int, and it is "24", then its operand |
| 3829 | // defines a byte. We only handle unsigned types here. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3830 | if (I->isShift() && isa<ConstantInt>(I->getOperand(1))) { |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3831 | // Not shifting the entire input by N-1 bytes? |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3832 | if (cast<ConstantInt>(I->getOperand(1))->getLimitedValue(BitWidth) != |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3833 | 8*(ByteValues.size()-1)) |
| 3834 | return true; |
| 3835 | |
| 3836 | unsigned DestNo; |
| 3837 | if (I->getOpcode() == Instruction::Shl) { |
| 3838 | // X << 24 defines the top byte with the lowest of the input bytes. |
| 3839 | DestNo = ByteValues.size()-1; |
| 3840 | } else { |
| 3841 | // X >>u 24 defines the low byte with the highest of the input bytes. |
| 3842 | DestNo = 0; |
| 3843 | } |
| 3844 | |
| 3845 | // If the destination byte value is already defined, the values are or'd |
| 3846 | // together, which isn't a bswap (unless it's an or of the same bits). |
| 3847 | if (ByteValues[DestNo] && ByteValues[DestNo] != I->getOperand(0)) |
| 3848 | return true; |
| 3849 | ByteValues[DestNo] = I->getOperand(0); |
| 3850 | return false; |
| 3851 | } |
| 3852 | |
| 3853 | // Otherwise, we can only handle and(shift X, imm), imm). Bail out of if we |
| 3854 | // don't have this. |
| 3855 | Value *Shift = 0, *ShiftLHS = 0; |
| 3856 | ConstantInt *AndAmt = 0, *ShiftAmt = 0; |
| 3857 | if (!match(I, m_And(m_Value(Shift), m_ConstantInt(AndAmt))) || |
| 3858 | !match(Shift, m_Shift(m_Value(ShiftLHS), m_ConstantInt(ShiftAmt)))) |
| 3859 | return true; |
| 3860 | Instruction *SI = cast<Instruction>(Shift); |
| 3861 | |
| 3862 | // 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] | 3863 | if (ShiftAmt->getLimitedValue(BitWidth) & 7 || |
| 3864 | ShiftAmt->getLimitedValue(BitWidth) > 8*ByteValues.size()) |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3865 | return true; |
| 3866 | |
| 3867 | // Turn 0xFF -> 0, 0xFF00 -> 1, 0xFF0000 -> 2, etc. |
| 3868 | unsigned DestByte; |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3869 | if (AndAmt->getValue().getActiveBits() > 64) |
| 3870 | return true; |
| 3871 | uint64_t AndAmtVal = AndAmt->getZExtValue(); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3872 | for (DestByte = 0; DestByte != ByteValues.size(); ++DestByte) |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3873 | if (AndAmtVal == uint64_t(0xFF) << 8*DestByte) |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3874 | break; |
| 3875 | // Unknown mask for bswap. |
| 3876 | if (DestByte == ByteValues.size()) return true; |
| 3877 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3878 | unsigned ShiftBytes = ShiftAmt->getZExtValue()/8; |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3879 | unsigned SrcByte; |
| 3880 | if (SI->getOpcode() == Instruction::Shl) |
| 3881 | SrcByte = DestByte - ShiftBytes; |
| 3882 | else |
| 3883 | SrcByte = DestByte + ShiftBytes; |
| 3884 | |
| 3885 | // If the SrcByte isn't a bswapped value from the DestByte, reject it. |
| 3886 | if (SrcByte != ByteValues.size()-DestByte-1) |
| 3887 | return true; |
| 3888 | |
| 3889 | // If the destination byte value is already defined, the values are or'd |
| 3890 | // together, which isn't a bswap (unless it's an or of the same bits). |
| 3891 | if (ByteValues[DestByte] && ByteValues[DestByte] != SI->getOperand(0)) |
| 3892 | return true; |
| 3893 | ByteValues[DestByte] = SI->getOperand(0); |
| 3894 | return false; |
| 3895 | } |
| 3896 | |
| 3897 | /// MatchBSwap - Given an OR instruction, check to see if this is a bswap idiom. |
| 3898 | /// If so, insert the new bswap intrinsic and return it. |
| 3899 | Instruction *InstCombiner::MatchBSwap(BinaryOperator &I) { |
Chris Lattner | 55fc8c4 | 2007-04-01 20:57:36 +0000 | [diff] [blame] | 3900 | const IntegerType *ITy = dyn_cast<IntegerType>(I.getType()); |
| 3901 | if (!ITy || ITy->getBitWidth() % 16) |
| 3902 | return 0; // Can only bswap pairs of bytes. Can't do vectors. |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3903 | |
| 3904 | /// ByteValues - For each byte of the result, we keep track of which value |
| 3905 | /// defines each byte. |
Chris Lattner | 535014f | 2007-02-15 22:52:10 +0000 | [diff] [blame] | 3906 | SmallVector<Value*, 8> ByteValues; |
Chris Lattner | 55fc8c4 | 2007-04-01 20:57:36 +0000 | [diff] [blame] | 3907 | ByteValues.resize(ITy->getBitWidth()/8); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3908 | |
| 3909 | // Try to find all the pieces corresponding to the bswap. |
| 3910 | if (CollectBSwapParts(I.getOperand(0), ByteValues) || |
| 3911 | CollectBSwapParts(I.getOperand(1), ByteValues)) |
| 3912 | return 0; |
| 3913 | |
| 3914 | // Check to see if all of the bytes come from the same value. |
| 3915 | Value *V = ByteValues[0]; |
| 3916 | if (V == 0) return 0; // Didn't find a byte? Must be zero. |
| 3917 | |
| 3918 | // Check to make sure that all of the bytes come from the same value. |
| 3919 | for (unsigned i = 1, e = ByteValues.size(); i != e; ++i) |
| 3920 | if (ByteValues[i] != V) |
| 3921 | return 0; |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 3922 | const Type *Tys[] = { ITy }; |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3923 | Module *M = I.getParent()->getParent()->getParent(); |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 3924 | Function *F = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3925 | return new CallInst(F, V); |
| 3926 | } |
| 3927 | |
| 3928 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3929 | Instruction *InstCombiner::visitOr(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 3930 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3931 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3932 | |
Chris Lattner | 42593e6 | 2007-03-24 23:56:43 +0000 | [diff] [blame] | 3933 | if (isa<UndefValue>(Op1)) // X | undef -> -1 |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 3934 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3935 | |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3936 | // or X, X = X |
| 3937 | if (Op0 == Op1) |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 3938 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3939 | |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3940 | // See if we can simplify any instructions used by the instruction whose sole |
| 3941 | // purpose is to compute bits we don't care about. |
Chris Lattner | 42593e6 | 2007-03-24 23:56:43 +0000 | [diff] [blame] | 3942 | if (!isa<VectorType>(I.getType())) { |
| 3943 | uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth(); |
| 3944 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 3945 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth), |
| 3946 | KnownZero, KnownOne)) |
| 3947 | return &I; |
Chris Lattner | 041a6c9 | 2007-06-15 05:26:55 +0000 | [diff] [blame] | 3948 | } else if (isa<ConstantAggregateZero>(Op1)) { |
| 3949 | return ReplaceInstUsesWith(I, Op0); // X | <0,0> -> X |
| 3950 | } else if (ConstantVector *CP = dyn_cast<ConstantVector>(Op1)) { |
| 3951 | if (CP->isAllOnesValue()) // X | <-1,-1> -> <-1,-1> |
| 3952 | return ReplaceInstUsesWith(I, I.getOperand(1)); |
Chris Lattner | 42593e6 | 2007-03-24 23:56:43 +0000 | [diff] [blame] | 3953 | } |
Chris Lattner | 041a6c9 | 2007-06-15 05:26:55 +0000 | [diff] [blame] | 3954 | |
| 3955 | |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3956 | |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3957 | // or X, -1 == -1 |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3958 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 3959 | ConstantInt *C1 = 0; Value *X = 0; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3960 | // (X & C1) | C2 --> (X | C2) & (C1|C2) |
| 3961 | 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] | 3962 | Instruction *Or = BinaryOperator::createOr(X, RHS); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3963 | InsertNewInstBefore(Or, I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3964 | Or->takeName(Op0); |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 3965 | return BinaryOperator::createAnd(Or, |
| 3966 | ConstantInt::get(RHS->getValue() | C1->getValue())); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3967 | } |
Chris Lattner | ad44ebf | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 3968 | |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3969 | // (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2) |
| 3970 | 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] | 3971 | Instruction *Or = BinaryOperator::createOr(X, RHS); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3972 | InsertNewInstBefore(Or, I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3973 | Or->takeName(Op0); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3974 | return BinaryOperator::createXor(Or, |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 3975 | ConstantInt::get(C1->getValue() & ~RHS->getValue())); |
Chris Lattner | ad44ebf | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 3976 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3977 | |
| 3978 | // Try to fold constant and into select arguments. |
| 3979 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3980 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3981 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 3982 | if (isa<PHINode>(Op0)) |
| 3983 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 3984 | return NV; |
Chris Lattner | ad44ebf | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 3985 | } |
| 3986 | |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 3987 | Value *A = 0, *B = 0; |
| 3988 | ConstantInt *C1 = 0, *C2 = 0; |
Chris Lattner | f4d4c87 | 2005-05-07 23:49:08 +0000 | [diff] [blame] | 3989 | |
| 3990 | if (match(Op0, m_And(m_Value(A), m_Value(B)))) |
| 3991 | if (A == Op1 || B == Op1) // (A & ?) | A --> A |
| 3992 | return ReplaceInstUsesWith(I, Op1); |
| 3993 | if (match(Op1, m_And(m_Value(A), m_Value(B)))) |
| 3994 | if (A == Op0 || B == Op0) // A | (A & ?) --> A |
| 3995 | return ReplaceInstUsesWith(I, Op0); |
| 3996 | |
Chris Lattner | 6423d4c | 2006-07-10 20:25:24 +0000 | [diff] [blame] | 3997 | // (A | B) | C and A | (B | C) -> bswap if possible. |
| 3998 | // (A >> B) | (C << D) and (A << B) | (B >> C) -> bswap if possible. |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3999 | if (match(Op0, m_Or(m_Value(), m_Value())) || |
Chris Lattner | 6423d4c | 2006-07-10 20:25:24 +0000 | [diff] [blame] | 4000 | match(Op1, m_Or(m_Value(), m_Value())) || |
| 4001 | (match(Op0, m_Shift(m_Value(), m_Value())) && |
| 4002 | match(Op1, m_Shift(m_Value(), m_Value())))) { |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4003 | if (Instruction *BSwap = MatchBSwap(I)) |
| 4004 | return BSwap; |
| 4005 | } |
| 4006 | |
Chris Lattner | 6e4c649 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 4007 | // (X^C)|Y -> (X|Y)^C iff Y&C == 0 |
| 4008 | 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] | 4009 | MaskedValueIsZero(Op1, C1->getValue())) { |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 4010 | Instruction *NOr = BinaryOperator::createOr(A, Op1); |
| 4011 | InsertNewInstBefore(NOr, I); |
| 4012 | NOr->takeName(Op0); |
| 4013 | return BinaryOperator::createXor(NOr, C1); |
Chris Lattner | 6e4c649 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 4014 | } |
| 4015 | |
| 4016 | // Y|(X^C) -> (X|Y)^C iff Y&C == 0 |
| 4017 | 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] | 4018 | MaskedValueIsZero(Op0, C1->getValue())) { |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 4019 | Instruction *NOr = BinaryOperator::createOr(A, Op0); |
| 4020 | InsertNewInstBefore(NOr, I); |
| 4021 | NOr->takeName(Op0); |
| 4022 | return BinaryOperator::createXor(NOr, C1); |
Chris Lattner | 6e4c649 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 4023 | } |
| 4024 | |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 4025 | // (A & C)|(B & D) |
Chris Lattner | 2384d7b | 2007-06-19 05:43:49 +0000 | [diff] [blame] | 4026 | Value *C = 0, *D = 0; |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 4027 | if (match(Op0, m_And(m_Value(A), m_Value(C))) && |
| 4028 | match(Op1, m_And(m_Value(B), m_Value(D)))) { |
Chris Lattner | 6cae0e0 | 2007-04-08 07:55:22 +0000 | [diff] [blame] | 4029 | Value *V1 = 0, *V2 = 0, *V3 = 0; |
| 4030 | C1 = dyn_cast<ConstantInt>(C); |
| 4031 | C2 = dyn_cast<ConstantInt>(D); |
| 4032 | if (C1 && C2) { // (A & C1)|(B & C2) |
| 4033 | // If we have: ((V + N) & C1) | (V & C2) |
| 4034 | // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0 |
| 4035 | // replace with V+N. |
| 4036 | if (C1->getValue() == ~C2->getValue()) { |
| 4037 | if ((C2->getValue() & (C2->getValue()+1)) == 0 && // C2 == 0+1+ |
| 4038 | match(A, m_Add(m_Value(V1), m_Value(V2)))) { |
| 4039 | // Add commutes, try both ways. |
| 4040 | if (V1 == B && MaskedValueIsZero(V2, C2->getValue())) |
| 4041 | return ReplaceInstUsesWith(I, A); |
| 4042 | if (V2 == B && MaskedValueIsZero(V1, C2->getValue())) |
| 4043 | return ReplaceInstUsesWith(I, A); |
| 4044 | } |
| 4045 | // Or commutes, try both ways. |
| 4046 | if ((C1->getValue() & (C1->getValue()+1)) == 0 && |
| 4047 | match(B, m_Add(m_Value(V1), m_Value(V2)))) { |
| 4048 | // Add commutes, try both ways. |
| 4049 | if (V1 == A && MaskedValueIsZero(V2, C1->getValue())) |
| 4050 | return ReplaceInstUsesWith(I, B); |
| 4051 | if (V2 == A && MaskedValueIsZero(V1, C1->getValue())) |
| 4052 | return ReplaceInstUsesWith(I, B); |
| 4053 | } |
| 4054 | } |
Chris Lattner | 044e533 | 2007-04-08 08:01:49 +0000 | [diff] [blame] | 4055 | V1 = 0; V2 = 0; V3 = 0; |
Chris Lattner | 6cae0e0 | 2007-04-08 07:55:22 +0000 | [diff] [blame] | 4056 | } |
| 4057 | |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 4058 | // Check to see if we have any common things being and'ed. If so, find the |
| 4059 | // terms for V1 & (V2|V3). |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 4060 | if (isOnlyUse(Op0) || isOnlyUse(Op1)) { |
| 4061 | if (A == B) // (A & C)|(A & D) == A & (C|D) |
| 4062 | V1 = A, V2 = C, V3 = D; |
| 4063 | else if (A == D) // (A & C)|(B & A) == A & (B|C) |
| 4064 | V1 = A, V2 = B, V3 = C; |
| 4065 | else if (C == B) // (A & C)|(C & D) == C & (A|D) |
| 4066 | V1 = C, V2 = A, V3 = D; |
| 4067 | else if (C == D) // (A & C)|(B & C) == C & (A|B) |
| 4068 | V1 = C, V2 = A, V3 = B; |
| 4069 | |
| 4070 | if (V1) { |
| 4071 | Value *Or = |
| 4072 | InsertNewInstBefore(BinaryOperator::createOr(V2, V3, "tmp"), I); |
| 4073 | return BinaryOperator::createAnd(V1, Or); |
Chris Lattner | 0b7c0bf | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 4074 | } |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 4075 | } |
Chris Lattner | e9bed7d | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 4076 | } |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4077 | |
| 4078 | // (X >> Z) | (Y >> Z) -> (X|Y) >> Z for all shifts. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4079 | if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) { |
| 4080 | if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0)) |
| 4081 | if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() && |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4082 | SI0->getOperand(1) == SI1->getOperand(1) && |
| 4083 | (SI0->hasOneUse() || SI1->hasOneUse())) { |
| 4084 | Instruction *NewOp = |
| 4085 | InsertNewInstBefore(BinaryOperator::createOr(SI0->getOperand(0), |
| 4086 | SI1->getOperand(0), |
| 4087 | SI0->getName()), I); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4088 | return BinaryOperator::create(SI1->getOpcode(), NewOp, |
| 4089 | SI1->getOperand(1)); |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4090 | } |
| 4091 | } |
Chris Lattner | 67ca768 | 2003-08-12 19:11:07 +0000 | [diff] [blame] | 4092 | |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4093 | if (match(Op0, m_Not(m_Value(A)))) { // ~A | Op1 |
| 4094 | if (A == Op1) // ~A | A == -1 |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4095 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4096 | } else { |
| 4097 | A = 0; |
| 4098 | } |
Chris Lattner | f4d4c87 | 2005-05-07 23:49:08 +0000 | [diff] [blame] | 4099 | // Note, A is still live here! |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4100 | if (match(Op1, m_Not(m_Value(B)))) { // Op0 | ~B |
| 4101 | if (Op0 == B) |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4102 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | a27231a | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 4103 | |
Misha Brukman | cb6267b | 2004-07-30 12:50:08 +0000 | [diff] [blame] | 4104 | // (~A | ~B) == (~(A & B)) - De Morgan's Law |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4105 | if (A && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
| 4106 | Value *And = InsertNewInstBefore(BinaryOperator::createAnd(A, B, |
| 4107 | I.getName()+".demorgan"), I); |
| 4108 | return BinaryOperator::createNot(And); |
| 4109 | } |
Chris Lattner | a27231a | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 4110 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4111 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4112 | // (icmp1 A, B) | (icmp2 A, B) --> (icmp3 A, B) |
| 4113 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1))) { |
| 4114 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS))) |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 4115 | return R; |
| 4116 | |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4117 | Value *LHSVal, *RHSVal; |
| 4118 | ConstantInt *LHSCst, *RHSCst; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4119 | ICmpInst::Predicate LHSCC, RHSCC; |
| 4120 | if (match(Op0, m_ICmp(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst)))) |
| 4121 | if (match(RHS, m_ICmp(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst)))) |
| 4122 | if (LHSVal == RHSVal && // Found (X icmp C1) | (X icmp C2) |
| 4123 | // icmp [us][gl]e x, cst is folded to icmp [us][gl]t elsewhere. |
| 4124 | LHSCC != ICmpInst::ICMP_UGE && LHSCC != ICmpInst::ICMP_ULE && |
| 4125 | RHSCC != ICmpInst::ICMP_UGE && RHSCC != ICmpInst::ICMP_ULE && |
| 4126 | LHSCC != ICmpInst::ICMP_SGE && LHSCC != ICmpInst::ICMP_SLE && |
Chris Lattner | 8885887 | 2007-05-11 05:55:56 +0000 | [diff] [blame] | 4127 | RHSCC != ICmpInst::ICMP_SGE && RHSCC != ICmpInst::ICMP_SLE && |
| 4128 | // We can't fold (ugt x, C) | (sgt x, C2). |
| 4129 | PredicatesFoldable(LHSCC, RHSCC)) { |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4130 | // Ensure that the larger constant is on the RHS. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4131 | ICmpInst *LHS = cast<ICmpInst>(Op0); |
Chris Lattner | 8885887 | 2007-05-11 05:55:56 +0000 | [diff] [blame] | 4132 | bool NeedsSwap; |
| 4133 | if (ICmpInst::isSignedPredicate(LHSCC)) |
Chris Lattner | 3aea1bd | 2007-05-11 16:58:45 +0000 | [diff] [blame] | 4134 | NeedsSwap = LHSCst->getValue().sgt(RHSCst->getValue()); |
Chris Lattner | 8885887 | 2007-05-11 05:55:56 +0000 | [diff] [blame] | 4135 | else |
Chris Lattner | 3aea1bd | 2007-05-11 16:58:45 +0000 | [diff] [blame] | 4136 | NeedsSwap = LHSCst->getValue().ugt(RHSCst->getValue()); |
Chris Lattner | 8885887 | 2007-05-11 05:55:56 +0000 | [diff] [blame] | 4137 | |
| 4138 | if (NeedsSwap) { |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4139 | std::swap(LHS, RHS); |
| 4140 | std::swap(LHSCst, RHSCst); |
| 4141 | std::swap(LHSCC, RHSCC); |
| 4142 | } |
| 4143 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4144 | // At this point, we know we have have two icmp instructions |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4145 | // comparing a value against two constants and or'ing the result |
| 4146 | // together. Because of the above check, we know that we only have |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4147 | // ICMP_EQ, ICMP_NE, ICMP_LT, and ICMP_GT here. We also know (from the |
| 4148 | // FoldICmpLogical check above), that the two constants are not |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4149 | // equal. |
| 4150 | assert(LHSCst != RHSCst && "Compares not folded above?"); |
| 4151 | |
| 4152 | switch (LHSCC) { |
| 4153 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4154 | case ICmpInst::ICMP_EQ: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4155 | switch (RHSCC) { |
| 4156 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4157 | case ICmpInst::ICMP_EQ: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4158 | if (LHSCst == SubOne(RHSCst)) {// (X == 13 | X == 14) -> X-13 <u 2 |
| 4159 | Constant *AddCST = ConstantExpr::getNeg(LHSCst); |
| 4160 | Instruction *Add = BinaryOperator::createAdd(LHSVal, AddCST, |
| 4161 | LHSVal->getName()+".off"); |
| 4162 | InsertNewInstBefore(Add, I); |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 4163 | AddCST = Subtract(AddOne(RHSCst), LHSCst); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4164 | return new ICmpInst(ICmpInst::ICMP_ULT, Add, AddCST); |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4165 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4166 | break; // (X == 13 | X == 15) -> no change |
| 4167 | case ICmpInst::ICMP_UGT: // (X == 13 | X u> 14) -> no change |
| 4168 | case ICmpInst::ICMP_SGT: // (X == 13 | X s> 14) -> no change |
Chris Lattner | 240d6f4 | 2005-04-19 06:04:18 +0000 | [diff] [blame] | 4169 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4170 | case ICmpInst::ICMP_NE: // (X == 13 | X != 15) -> X != 15 |
| 4171 | case ICmpInst::ICMP_ULT: // (X == 13 | X u< 15) -> X u< 15 |
| 4172 | case ICmpInst::ICMP_SLT: // (X == 13 | X s< 15) -> X s< 15 |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4173 | return ReplaceInstUsesWith(I, RHS); |
| 4174 | } |
| 4175 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4176 | case ICmpInst::ICMP_NE: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4177 | switch (RHSCC) { |
| 4178 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4179 | case ICmpInst::ICMP_EQ: // (X != 13 | X == 15) -> X != 13 |
| 4180 | case ICmpInst::ICMP_UGT: // (X != 13 | X u> 15) -> X != 13 |
| 4181 | case ICmpInst::ICMP_SGT: // (X != 13 | X s> 15) -> X != 13 |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4182 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4183 | case ICmpInst::ICMP_NE: // (X != 13 | X != 15) -> true |
| 4184 | case ICmpInst::ICMP_ULT: // (X != 13 | X u< 15) -> true |
| 4185 | case ICmpInst::ICMP_SLT: // (X != 13 | X s< 15) -> true |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4186 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4187 | } |
| 4188 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4189 | case ICmpInst::ICMP_ULT: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4190 | switch (RHSCC) { |
| 4191 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4192 | case ICmpInst::ICMP_EQ: // (X u< 13 | X == 14) -> no change |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4193 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4194 | 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] | 4195 | // If RHSCst is [us]MAXINT, it is always false. Not handling |
| 4196 | // this can cause overflow. |
| 4197 | if (RHSCst->isMaxValue(false)) |
| 4198 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4199 | return InsertRangeTest(LHSVal, LHSCst, AddOne(RHSCst), false, |
| 4200 | false, I); |
| 4201 | case ICmpInst::ICMP_SGT: // (X u< 13 | X s> 15) -> no change |
| 4202 | break; |
| 4203 | case ICmpInst::ICMP_NE: // (X u< 13 | X != 15) -> X != 15 |
| 4204 | 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] | 4205 | return ReplaceInstUsesWith(I, RHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4206 | case ICmpInst::ICMP_SLT: // (X u< 13 | X s< 15) -> no change |
| 4207 | break; |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4208 | } |
| 4209 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4210 | case ICmpInst::ICMP_SLT: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4211 | switch (RHSCC) { |
| 4212 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4213 | case ICmpInst::ICMP_EQ: // (X s< 13 | X == 14) -> no change |
| 4214 | break; |
| 4215 | 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] | 4216 | // If RHSCst is [us]MAXINT, it is always false. Not handling |
| 4217 | // this can cause overflow. |
| 4218 | if (RHSCst->isMaxValue(true)) |
| 4219 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4220 | return InsertRangeTest(LHSVal, LHSCst, AddOne(RHSCst), true, |
| 4221 | false, I); |
| 4222 | case ICmpInst::ICMP_UGT: // (X s< 13 | X u> 15) -> no change |
| 4223 | break; |
| 4224 | case ICmpInst::ICMP_NE: // (X s< 13 | X != 15) -> X != 15 |
| 4225 | case ICmpInst::ICMP_SLT: // (X s< 13 | X s< 15) -> X s< 15 |
| 4226 | return ReplaceInstUsesWith(I, RHS); |
| 4227 | case ICmpInst::ICMP_ULT: // (X s< 13 | X u< 15) -> no change |
| 4228 | break; |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4229 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4230 | break; |
| 4231 | case ICmpInst::ICMP_UGT: |
| 4232 | switch (RHSCC) { |
| 4233 | default: assert(0 && "Unknown integer condition code!"); |
| 4234 | case ICmpInst::ICMP_EQ: // (X u> 13 | X == 15) -> X u> 13 |
| 4235 | case ICmpInst::ICMP_UGT: // (X u> 13 | X u> 15) -> X u> 13 |
| 4236 | return ReplaceInstUsesWith(I, LHS); |
| 4237 | case ICmpInst::ICMP_SGT: // (X u> 13 | X s> 15) -> no change |
| 4238 | break; |
| 4239 | case ICmpInst::ICMP_NE: // (X u> 13 | X != 15) -> true |
| 4240 | case ICmpInst::ICMP_ULT: // (X u> 13 | X u< 15) -> true |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4241 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4242 | case ICmpInst::ICMP_SLT: // (X u> 13 | X s< 15) -> no change |
| 4243 | break; |
| 4244 | } |
| 4245 | break; |
| 4246 | case ICmpInst::ICMP_SGT: |
| 4247 | switch (RHSCC) { |
| 4248 | default: assert(0 && "Unknown integer condition code!"); |
| 4249 | case ICmpInst::ICMP_EQ: // (X s> 13 | X == 15) -> X > 13 |
| 4250 | case ICmpInst::ICMP_SGT: // (X s> 13 | X s> 15) -> X > 13 |
| 4251 | return ReplaceInstUsesWith(I, LHS); |
| 4252 | case ICmpInst::ICMP_UGT: // (X s> 13 | X u> 15) -> no change |
| 4253 | break; |
| 4254 | case ICmpInst::ICMP_NE: // (X s> 13 | X != 15) -> true |
| 4255 | case ICmpInst::ICMP_SLT: // (X s> 13 | X s< 15) -> true |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4256 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4257 | case ICmpInst::ICMP_ULT: // (X s> 13 | X u< 15) -> no change |
| 4258 | break; |
| 4259 | } |
| 4260 | break; |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4261 | } |
| 4262 | } |
| 4263 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4264 | |
| 4265 | // fold (or (cast A), (cast B)) -> (cast (or A, B)) |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4266 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) { |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4267 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4268 | if (Op0C->getOpcode() == Op1C->getOpcode()) {// same cast kind ? |
| 4269 | const Type *SrcTy = Op0C->getOperand(0)->getType(); |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 4270 | if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() && |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4271 | // 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] | 4272 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), |
| 4273 | I.getType(), TD) && |
| 4274 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), |
| 4275 | I.getType(), TD)) { |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4276 | Instruction *NewOp = BinaryOperator::createOr(Op0C->getOperand(0), |
| 4277 | Op1C->getOperand(0), |
| 4278 | I.getName()); |
| 4279 | InsertNewInstBefore(NewOp, I); |
| 4280 | return CastInst::create(Op0C->getOpcode(), NewOp, I.getType()); |
| 4281 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4282 | } |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4283 | } |
| 4284 | |
| 4285 | |
| 4286 | // (fcmp uno x, c) | (fcmp uno y, c) -> (fcmp uno x, y) |
| 4287 | if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0))) { |
| 4288 | if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1))) { |
| 4289 | if (LHS->getPredicate() == FCmpInst::FCMP_UNO && |
| 4290 | RHS->getPredicate() == FCmpInst::FCMP_UNO) |
| 4291 | if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1))) |
| 4292 | if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) { |
| 4293 | // If either of the constants are nans, then the whole thing returns |
| 4294 | // true. |
Chris Lattner | be3e348 | 2007-10-24 18:54:45 +0000 | [diff] [blame] | 4295 | if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN()) |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4296 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
| 4297 | |
| 4298 | // Otherwise, no need to compare the two constants, compare the |
| 4299 | // rest. |
| 4300 | return new FCmpInst(FCmpInst::FCMP_UNO, LHS->getOperand(0), |
| 4301 | RHS->getOperand(0)); |
| 4302 | } |
| 4303 | } |
| 4304 | } |
Chris Lattner | e9bed7d | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 4305 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4306 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4307 | } |
| 4308 | |
Chris Lattner | c317d39 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 4309 | // XorSelf - Implements: X ^ X --> 0 |
| 4310 | struct XorSelf { |
| 4311 | Value *RHS; |
| 4312 | XorSelf(Value *rhs) : RHS(rhs) {} |
| 4313 | bool shouldApply(Value *LHS) const { return LHS == RHS; } |
| 4314 | Instruction *apply(BinaryOperator &Xor) const { |
| 4315 | return &Xor; |
| 4316 | } |
| 4317 | }; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4318 | |
| 4319 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4320 | Instruction *InstCombiner::visitXor(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 4321 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4322 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4323 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4324 | if (isa<UndefValue>(Op1)) |
| 4325 | return ReplaceInstUsesWith(I, Op1); // X ^ undef -> undef |
| 4326 | |
Chris Lattner | c317d39 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 4327 | // xor X, X = 0, even if X is nested in a sequence of Xor's. |
| 4328 | if (Instruction *Result = AssociativeOpt(I, XorSelf(Op1))) { |
Chris Lattner | a9ff5eb | 2007-08-05 08:47:58 +0000 | [diff] [blame] | 4329 | assert(Result == &I && "AssociativeOpt didn't work?"); Result=Result; |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 4330 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | c317d39 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 4331 | } |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 4332 | |
| 4333 | // See if we can simplify any instructions used by the instruction whose sole |
| 4334 | // purpose is to compute bits we don't care about. |
Reid Spencer | a03d45f | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 4335 | if (!isa<VectorType>(I.getType())) { |
| 4336 | uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth(); |
| 4337 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 4338 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth), |
| 4339 | KnownZero, KnownOne)) |
| 4340 | return &I; |
Chris Lattner | 041a6c9 | 2007-06-15 05:26:55 +0000 | [diff] [blame] | 4341 | } else if (isa<ConstantAggregateZero>(Op1)) { |
| 4342 | return ReplaceInstUsesWith(I, Op0); // X ^ <0,0> -> X |
Reid Spencer | a03d45f | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 4343 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4344 | |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4345 | // Is this a ~ operation? |
| 4346 | if (Value *NotOp = dyn_castNotVal(&I)) { |
| 4347 | // ~(~X & Y) --> (X | ~Y) - De Morgan's Law |
| 4348 | // ~(~X | Y) === (X & ~Y) - De Morgan's Law |
| 4349 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(NotOp)) { |
| 4350 | if (Op0I->getOpcode() == Instruction::And || |
| 4351 | Op0I->getOpcode() == Instruction::Or) { |
| 4352 | if (dyn_castNotVal(Op0I->getOperand(1))) Op0I->swapOperands(); |
| 4353 | if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0))) { |
| 4354 | Instruction *NotY = |
| 4355 | BinaryOperator::createNot(Op0I->getOperand(1), |
| 4356 | Op0I->getOperand(1)->getName()+".not"); |
| 4357 | InsertNewInstBefore(NotY, I); |
| 4358 | if (Op0I->getOpcode() == Instruction::And) |
| 4359 | return BinaryOperator::createOr(Op0NotVal, NotY); |
| 4360 | else |
| 4361 | return BinaryOperator::createAnd(Op0NotVal, NotY); |
| 4362 | } |
| 4363 | } |
| 4364 | } |
| 4365 | } |
| 4366 | |
| 4367 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4368 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Nick Lewycky | f947b3e | 2007-08-06 20:04:16 +0000 | [diff] [blame] | 4369 | // xor (cmp A, B), true = not (cmp A, B) = !cmp A, B |
| 4370 | if (RHS == ConstantInt::getTrue() && Op0->hasOneUse()) { |
| 4371 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(Op0)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4372 | return new ICmpInst(ICI->getInversePredicate(), |
| 4373 | ICI->getOperand(0), ICI->getOperand(1)); |
Chris Lattner | ad5b4fb | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 4374 | |
Nick Lewycky | f947b3e | 2007-08-06 20:04:16 +0000 | [diff] [blame] | 4375 | if (FCmpInst *FCI = dyn_cast<FCmpInst>(Op0)) |
| 4376 | return new FCmpInst(FCI->getInversePredicate(), |
| 4377 | FCI->getOperand(0), FCI->getOperand(1)); |
| 4378 | } |
| 4379 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4380 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) { |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 4381 | // ~(c-X) == X-c-1 == X+(-c-1) |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4382 | if (Op0I->getOpcode() == Instruction::Sub && RHS->isAllOnesValue()) |
| 4383 | if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4384 | Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C); |
| 4385 | Constant *ConstantRHS = ConstantExpr::getSub(NegOp0I0C, |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4386 | ConstantInt::get(I.getType(), 1)); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4387 | return BinaryOperator::createAdd(Op0I->getOperand(1), ConstantRHS); |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4388 | } |
Chris Lattner | 5c6e2db | 2007-04-02 05:36:22 +0000 | [diff] [blame] | 4389 | |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 4390 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) { |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 4391 | if (Op0I->getOpcode() == Instruction::Add) { |
Chris Lattner | 689d24b | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 4392 | // ~(X-c) --> (-c-1)-X |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4393 | if (RHS->isAllOnesValue()) { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4394 | Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI); |
| 4395 | return BinaryOperator::createSub( |
| 4396 | ConstantExpr::getSub(NegOp0CI, |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4397 | ConstantInt::get(I.getType(), 1)), |
Chris Lattner | 689d24b | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 4398 | Op0I->getOperand(0)); |
Chris Lattner | acf4e07 | 2007-04-02 05:42:22 +0000 | [diff] [blame] | 4399 | } else if (RHS->getValue().isSignBit()) { |
Chris Lattner | 5c6e2db | 2007-04-02 05:36:22 +0000 | [diff] [blame] | 4400 | // (X + C) ^ signbit -> (X + C + signbit) |
| 4401 | Constant *C = ConstantInt::get(RHS->getValue() + Op0CI->getValue()); |
| 4402 | return BinaryOperator::createAdd(Op0I->getOperand(0), C); |
Chris Lattner | cd1d6d5 | 2007-04-02 05:48:58 +0000 | [diff] [blame] | 4403 | |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4404 | } |
Chris Lattner | 02bd1b3 | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 4405 | } else if (Op0I->getOpcode() == Instruction::Or) { |
| 4406 | // (X|C1)^C2 -> X^(C1|C2) iff X&~C1 == 0 |
Reid Spencer | a03d45f | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 4407 | if (MaskedValueIsZero(Op0I->getOperand(0), Op0CI->getValue())) { |
Chris Lattner | 02bd1b3 | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 4408 | Constant *NewRHS = ConstantExpr::getOr(Op0CI, RHS); |
| 4409 | // Anything in both C1 and C2 is known to be zero, remove it from |
| 4410 | // NewRHS. |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 4411 | Constant *CommonBits = And(Op0CI, RHS); |
Chris Lattner | 02bd1b3 | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 4412 | NewRHS = ConstantExpr::getAnd(NewRHS, |
| 4413 | ConstantExpr::getNot(CommonBits)); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 4414 | AddToWorkList(Op0I); |
Chris Lattner | 02bd1b3 | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 4415 | I.setOperand(0, Op0I->getOperand(0)); |
| 4416 | I.setOperand(1, NewRHS); |
| 4417 | return &I; |
| 4418 | } |
Chris Lattner | eca0c5c | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 4419 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 4420 | } |
Chris Lattner | 05bd1b2 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 4421 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 4422 | |
| 4423 | // Try to fold constant and into select arguments. |
| 4424 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 4425 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 4426 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 4427 | if (isa<PHINode>(Op0)) |
| 4428 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 4429 | return NV; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4430 | } |
| 4431 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 4432 | if (Value *X = dyn_castNotVal(Op0)) // ~A ^ A == -1 |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4433 | if (X == Op1) |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4434 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4435 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 4436 | if (Value *X = dyn_castNotVal(Op1)) // A ^ ~A == -1 |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4437 | if (X == Op0) |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4438 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4439 | |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4440 | |
| 4441 | BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1); |
| 4442 | if (Op1I) { |
| 4443 | Value *A, *B; |
| 4444 | if (match(Op1I, m_Or(m_Value(A), m_Value(B)))) { |
| 4445 | if (A == Op0) { // B^(B|A) == (A|B)^B |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4446 | Op1I->swapOperands(); |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4447 | I.swapOperands(); |
| 4448 | std::swap(Op0, Op1); |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4449 | } else if (B == Op0) { // B^(A|B) == (A|B)^B |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4450 | I.swapOperands(); // Simplified below. |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4451 | std::swap(Op0, Op1); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4452 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4453 | } else if (match(Op1I, m_Xor(m_Value(A), m_Value(B)))) { |
| 4454 | if (Op0 == A) // A^(A^B) == B |
| 4455 | return ReplaceInstUsesWith(I, B); |
| 4456 | else if (Op0 == B) // A^(B^A) == B |
| 4457 | return ReplaceInstUsesWith(I, A); |
| 4458 | } 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] | 4459 | if (A == Op0) { // A^(A&B) -> A^(B&A) |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4460 | Op1I->swapOperands(); |
Chris Lattner | 6abbdf9 | 2007-04-01 05:36:37 +0000 | [diff] [blame] | 4461 | std::swap(A, B); |
| 4462 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4463 | if (B == Op0) { // A^(B&A) -> (B&A)^A |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4464 | I.swapOperands(); // Simplified below. |
| 4465 | std::swap(Op0, Op1); |
| 4466 | } |
Chris Lattner | 26ca7e1 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 4467 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4468 | } |
| 4469 | |
| 4470 | BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0); |
| 4471 | if (Op0I) { |
| 4472 | Value *A, *B; |
| 4473 | if (match(Op0I, m_Or(m_Value(A), m_Value(B))) && Op0I->hasOneUse()) { |
| 4474 | if (A == Op1) // (B|A)^B == (A|B)^B |
| 4475 | std::swap(A, B); |
| 4476 | if (B == Op1) { // (A|B)^B == A & ~B |
| 4477 | Instruction *NotB = |
| 4478 | InsertNewInstBefore(BinaryOperator::createNot(Op1, "tmp"), I); |
| 4479 | return BinaryOperator::createAnd(A, NotB); |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4480 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4481 | } else if (match(Op0I, m_Xor(m_Value(A), m_Value(B)))) { |
| 4482 | if (Op1 == A) // (A^B)^A == B |
| 4483 | return ReplaceInstUsesWith(I, B); |
| 4484 | else if (Op1 == B) // (B^A)^A == B |
| 4485 | return ReplaceInstUsesWith(I, A); |
| 4486 | } else if (match(Op0I, m_And(m_Value(A), m_Value(B))) && Op0I->hasOneUse()){ |
| 4487 | if (A == Op1) // (A&B)^A -> (B&A)^A |
| 4488 | std::swap(A, B); |
| 4489 | if (B == Op1 && // (B&A)^A == ~B & A |
Chris Lattner | ae1ab39 | 2006-04-01 22:05:01 +0000 | [diff] [blame] | 4490 | !isa<ConstantInt>(Op1)) { // Canonical form is (B&C)^C |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4491 | Instruction *N = |
| 4492 | InsertNewInstBefore(BinaryOperator::createNot(A, "tmp"), I); |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4493 | return BinaryOperator::createAnd(N, Op1); |
| 4494 | } |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4495 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4496 | } |
| 4497 | |
| 4498 | // (X >> Z) ^ (Y >> Z) -> (X^Y) >> Z for all shifts. |
| 4499 | if (Op0I && Op1I && Op0I->isShift() && |
| 4500 | Op0I->getOpcode() == Op1I->getOpcode() && |
| 4501 | Op0I->getOperand(1) == Op1I->getOperand(1) && |
| 4502 | (Op1I->hasOneUse() || Op1I->hasOneUse())) { |
| 4503 | Instruction *NewOp = |
| 4504 | InsertNewInstBefore(BinaryOperator::createXor(Op0I->getOperand(0), |
| 4505 | Op1I->getOperand(0), |
| 4506 | Op0I->getName()), I); |
| 4507 | return BinaryOperator::create(Op1I->getOpcode(), NewOp, |
| 4508 | Op1I->getOperand(1)); |
| 4509 | } |
| 4510 | |
| 4511 | if (Op0I && Op1I) { |
| 4512 | Value *A, *B, *C, *D; |
| 4513 | // (A & B)^(A | B) -> A ^ B |
| 4514 | if (match(Op0I, m_And(m_Value(A), m_Value(B))) && |
| 4515 | match(Op1I, m_Or(m_Value(C), m_Value(D)))) { |
| 4516 | if ((A == C && B == D) || (A == D && B == C)) |
| 4517 | return BinaryOperator::createXor(A, B); |
| 4518 | } |
| 4519 | // (A | B)^(A & B) -> A ^ B |
| 4520 | if (match(Op0I, m_Or(m_Value(A), m_Value(B))) && |
| 4521 | match(Op1I, m_And(m_Value(C), m_Value(D)))) { |
| 4522 | if ((A == C && B == D) || (A == D && B == C)) |
| 4523 | return BinaryOperator::createXor(A, B); |
| 4524 | } |
| 4525 | |
| 4526 | // (A & B)^(C & D) |
| 4527 | if ((Op0I->hasOneUse() || Op1I->hasOneUse()) && |
| 4528 | match(Op0I, m_And(m_Value(A), m_Value(B))) && |
| 4529 | match(Op1I, m_And(m_Value(C), m_Value(D)))) { |
| 4530 | // (X & Y)^(X & Y) -> (Y^Z) & X |
| 4531 | Value *X = 0, *Y = 0, *Z = 0; |
| 4532 | if (A == C) |
| 4533 | X = A, Y = B, Z = D; |
| 4534 | else if (A == D) |
| 4535 | X = A, Y = B, Z = C; |
| 4536 | else if (B == C) |
| 4537 | X = B, Y = A, Z = D; |
| 4538 | else if (B == D) |
| 4539 | X = B, Y = A, Z = C; |
| 4540 | |
| 4541 | if (X) { |
| 4542 | Instruction *NewOp = |
| 4543 | InsertNewInstBefore(BinaryOperator::createXor(Y, Z, Op0->getName()), I); |
| 4544 | return BinaryOperator::createAnd(NewOp, X); |
| 4545 | } |
| 4546 | } |
| 4547 | } |
| 4548 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4549 | // (icmp1 A, B) ^ (icmp2 A, B) --> (icmp3 A, B) |
| 4550 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1))) |
| 4551 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS))) |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 4552 | return R; |
| 4553 | |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4554 | // fold (xor (cast A), (cast B)) -> (cast (xor A, B)) |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4555 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) { |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4556 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4557 | if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind? |
| 4558 | const Type *SrcTy = Op0C->getOperand(0)->getType(); |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 4559 | if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() && |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4560 | // 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] | 4561 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), |
| 4562 | I.getType(), TD) && |
| 4563 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), |
| 4564 | I.getType(), TD)) { |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4565 | Instruction *NewOp = BinaryOperator::createXor(Op0C->getOperand(0), |
| 4566 | Op1C->getOperand(0), |
| 4567 | I.getName()); |
| 4568 | InsertNewInstBefore(NewOp, I); |
| 4569 | return CastInst::create(Op0C->getOpcode(), NewOp, I.getType()); |
| 4570 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4571 | } |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4572 | } |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4573 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4574 | } |
| 4575 | |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4576 | /// AddWithOverflow - Compute Result = In1+In2, returning true if the result |
| 4577 | /// overflowed for this type. |
| 4578 | static bool AddWithOverflow(ConstantInt *&Result, ConstantInt *In1, |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 4579 | ConstantInt *In2, bool IsSigned = false) { |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 4580 | Result = cast<ConstantInt>(Add(In1, In2)); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4581 | |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 4582 | if (IsSigned) |
| 4583 | if (In2->getValue().isNegative()) |
| 4584 | return Result->getValue().sgt(In1->getValue()); |
| 4585 | else |
| 4586 | return Result->getValue().slt(In1->getValue()); |
| 4587 | else |
| 4588 | return Result->getValue().ult(In1->getValue()); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4589 | } |
| 4590 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4591 | /// EmitGEPOffset - Given a getelementptr instruction/constantexpr, emit the |
| 4592 | /// code necessary to compute the offset from the base pointer (without adding |
| 4593 | /// in the base pointer). Return the result as a signed integer of intptr size. |
| 4594 | static Value *EmitGEPOffset(User *GEP, Instruction &I, InstCombiner &IC) { |
| 4595 | TargetData &TD = IC.getTargetData(); |
| 4596 | gep_type_iterator GTI = gep_type_begin(GEP); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4597 | const Type *IntPtrTy = TD.getIntPtrType(); |
| 4598 | Value *Result = Constant::getNullValue(IntPtrTy); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4599 | |
| 4600 | // Build a mask for high order bits. |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4601 | unsigned IntPtrWidth = TD.getPointerSize()*8; |
| 4602 | uint64_t PtrSizeMask = ~0ULL >> (64-IntPtrWidth); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4603 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4604 | for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i, ++GTI) { |
| 4605 | Value *Op = GEP->getOperand(i); |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 4606 | uint64_t Size = TD.getABITypeSize(GTI.getIndexedType()) & PtrSizeMask; |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4607 | if (ConstantInt *OpC = dyn_cast<ConstantInt>(Op)) { |
| 4608 | if (OpC->isZero()) continue; |
| 4609 | |
| 4610 | // Handle a struct index, which adds its field offset to the pointer. |
| 4611 | if (const StructType *STy = dyn_cast<StructType>(*GTI)) { |
| 4612 | Size = TD.getStructLayout(STy)->getElementOffset(OpC->getZExtValue()); |
| 4613 | |
| 4614 | if (ConstantInt *RC = dyn_cast<ConstantInt>(Result)) |
| 4615 | Result = ConstantInt::get(RC->getValue() + APInt(IntPtrWidth, Size)); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 4616 | else |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4617 | Result = IC.InsertNewInstBefore( |
| 4618 | BinaryOperator::createAdd(Result, |
| 4619 | ConstantInt::get(IntPtrTy, Size), |
| 4620 | GEP->getName()+".offs"), I); |
| 4621 | continue; |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 4622 | } |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4623 | |
| 4624 | Constant *Scale = ConstantInt::get(IntPtrTy, Size); |
| 4625 | Constant *OC = ConstantExpr::getIntegerCast(OpC, IntPtrTy, true /*SExt*/); |
| 4626 | Scale = ConstantExpr::getMul(OC, Scale); |
| 4627 | if (Constant *RC = dyn_cast<Constant>(Result)) |
| 4628 | Result = ConstantExpr::getAdd(RC, Scale); |
| 4629 | else { |
| 4630 | // Emit an add instruction. |
| 4631 | Result = IC.InsertNewInstBefore( |
| 4632 | BinaryOperator::createAdd(Result, Scale, |
| 4633 | GEP->getName()+".offs"), I); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 4634 | } |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4635 | continue; |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4636 | } |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4637 | // Convert to correct type. |
| 4638 | if (Op->getType() != IntPtrTy) { |
| 4639 | if (Constant *OpC = dyn_cast<Constant>(Op)) |
| 4640 | Op = ConstantExpr::getSExt(OpC, IntPtrTy); |
| 4641 | else |
| 4642 | Op = IC.InsertNewInstBefore(new SExtInst(Op, IntPtrTy, |
| 4643 | Op->getName()+".c"), I); |
| 4644 | } |
| 4645 | if (Size != 1) { |
| 4646 | Constant *Scale = ConstantInt::get(IntPtrTy, Size); |
| 4647 | if (Constant *OpC = dyn_cast<Constant>(Op)) |
| 4648 | Op = ConstantExpr::getMul(OpC, Scale); |
| 4649 | else // We'll let instcombine(mul) convert this to a shl if possible. |
| 4650 | Op = IC.InsertNewInstBefore(BinaryOperator::createMul(Op, Scale, |
| 4651 | GEP->getName()+".idx"), I); |
| 4652 | } |
| 4653 | |
| 4654 | // Emit an add instruction. |
| 4655 | if (isa<Constant>(Op) && isa<Constant>(Result)) |
| 4656 | Result = ConstantExpr::getAdd(cast<Constant>(Op), |
| 4657 | cast<Constant>(Result)); |
| 4658 | else |
| 4659 | Result = IC.InsertNewInstBefore(BinaryOperator::createAdd(Op, Result, |
| 4660 | GEP->getName()+".offs"), I); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4661 | } |
| 4662 | return Result; |
| 4663 | } |
| 4664 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4665 | /// FoldGEPICmp - Fold comparisons between a GEP instruction and something |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4666 | /// 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] | 4667 | Instruction *InstCombiner::FoldGEPICmp(User *GEPLHS, Value *RHS, |
| 4668 | ICmpInst::Predicate Cond, |
| 4669 | Instruction &I) { |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4670 | assert(dyn_castGetElementPtr(GEPLHS) && "LHS is not a getelementptr!"); |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4671 | |
| 4672 | if (CastInst *CI = dyn_cast<CastInst>(RHS)) |
| 4673 | if (isa<PointerType>(CI->getOperand(0)->getType())) |
| 4674 | RHS = CI->getOperand(0); |
| 4675 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4676 | Value *PtrBase = GEPLHS->getOperand(0); |
| 4677 | if (PtrBase == RHS) { |
Chris Lattner | 7c95deb | 2008-02-05 04:45:32 +0000 | [diff] [blame] | 4678 | // ((gep Ptr, OFFSET) cmp Ptr) ---> (OFFSET cmp 0). |
| 4679 | // This transformation is valid because we know pointers can't overflow. |
| 4680 | Value *Offset = EmitGEPOffset(GEPLHS, I, *this); |
| 4681 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), Offset, |
| 4682 | Constant::getNullValue(Offset->getType())); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4683 | } else if (User *GEPRHS = dyn_castGetElementPtr(RHS)) { |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 4684 | // If the base pointers are different, but the indices are the same, just |
| 4685 | // compare the base pointer. |
| 4686 | if (PtrBase != GEPRHS->getOperand(0)) { |
| 4687 | bool IndicesTheSame = GEPLHS->getNumOperands()==GEPRHS->getNumOperands(); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 4688 | IndicesTheSame &= GEPLHS->getOperand(0)->getType() == |
Chris Lattner | 93b94a6 | 2005-04-26 14:40:41 +0000 | [diff] [blame] | 4689 | GEPRHS->getOperand(0)->getType(); |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 4690 | if (IndicesTheSame) |
| 4691 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i) |
| 4692 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { |
| 4693 | IndicesTheSame = false; |
| 4694 | break; |
| 4695 | } |
| 4696 | |
| 4697 | // If all indices are the same, just compare the base pointers. |
| 4698 | if (IndicesTheSame) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4699 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), |
| 4700 | GEPLHS->getOperand(0), GEPRHS->getOperand(0)); |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 4701 | |
| 4702 | // Otherwise, the base pointers are different and the indices are |
| 4703 | // different, bail out. |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4704 | return 0; |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 4705 | } |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4706 | |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4707 | // If one of the GEPs has all zero indices, recurse. |
| 4708 | bool AllZeros = true; |
| 4709 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i) |
| 4710 | if (!isa<Constant>(GEPLHS->getOperand(i)) || |
| 4711 | !cast<Constant>(GEPLHS->getOperand(i))->isNullValue()) { |
| 4712 | AllZeros = false; |
| 4713 | break; |
| 4714 | } |
| 4715 | if (AllZeros) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4716 | return FoldGEPICmp(GEPRHS, GEPLHS->getOperand(0), |
| 4717 | ICmpInst::getSwappedPredicate(Cond), I); |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 4718 | |
| 4719 | // If the other GEP has all zero indices, recurse. |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4720 | AllZeros = true; |
| 4721 | for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i) |
| 4722 | if (!isa<Constant>(GEPRHS->getOperand(i)) || |
| 4723 | !cast<Constant>(GEPRHS->getOperand(i))->isNullValue()) { |
| 4724 | AllZeros = false; |
| 4725 | break; |
| 4726 | } |
| 4727 | if (AllZeros) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4728 | return FoldGEPICmp(GEPLHS, GEPRHS->getOperand(0), Cond, I); |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4729 | |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 4730 | if (GEPLHS->getNumOperands() == GEPRHS->getNumOperands()) { |
| 4731 | // If the GEPs only differ by one index, compare it. |
| 4732 | unsigned NumDifferences = 0; // Keep track of # differences. |
| 4733 | unsigned DiffOperand = 0; // The operand that differs. |
| 4734 | for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i) |
| 4735 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4736 | if (GEPLHS->getOperand(i)->getType()->getPrimitiveSizeInBits() != |
| 4737 | GEPRHS->getOperand(i)->getType()->getPrimitiveSizeInBits()) { |
Chris Lattner | 45f57b8 | 2005-01-21 23:06:49 +0000 | [diff] [blame] | 4738 | // Irreconcilable differences. |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 4739 | NumDifferences = 2; |
| 4740 | break; |
| 4741 | } else { |
| 4742 | if (NumDifferences++) break; |
| 4743 | DiffOperand = i; |
| 4744 | } |
| 4745 | } |
| 4746 | |
| 4747 | if (NumDifferences == 0) // SAME GEP? |
| 4748 | return ReplaceInstUsesWith(I, // No comparison is needed here. |
Nick Lewycky | 455e176 | 2007-09-06 02:40:25 +0000 | [diff] [blame] | 4749 | ConstantInt::get(Type::Int1Ty, |
| 4750 | isTrueWhenEqual(Cond))); |
| 4751 | |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 4752 | else if (NumDifferences == 1) { |
Chris Lattner | 45f57b8 | 2005-01-21 23:06:49 +0000 | [diff] [blame] | 4753 | Value *LHSV = GEPLHS->getOperand(DiffOperand); |
| 4754 | Value *RHSV = GEPRHS->getOperand(DiffOperand); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4755 | // Make sure we do a signed comparison here. |
| 4756 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), LHSV, RHSV); |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 4757 | } |
| 4758 | } |
| 4759 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4760 | // 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] | 4761 | // the result to fold to a constant! |
| 4762 | if ((isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) && |
| 4763 | (isa<ConstantExpr>(GEPRHS) || GEPRHS->hasOneUse())) { |
| 4764 | // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) ---> (OFFSET1 cmp OFFSET2) |
| 4765 | Value *L = EmitGEPOffset(GEPLHS, I, *this); |
| 4766 | Value *R = EmitGEPOffset(GEPRHS, I, *this); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4767 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), L, R); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4768 | } |
| 4769 | } |
| 4770 | return 0; |
| 4771 | } |
| 4772 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4773 | Instruction *InstCombiner::visitFCmpInst(FCmpInst &I) { |
| 4774 | bool Changed = SimplifyCompare(I); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4775 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4776 | |
Chris Lattner | 58e9746 | 2007-01-14 19:42:17 +0000 | [diff] [blame] | 4777 | // Fold trivial predicates. |
| 4778 | if (I.getPredicate() == FCmpInst::FCMP_FALSE) |
| 4779 | return ReplaceInstUsesWith(I, Constant::getNullValue(Type::Int1Ty)); |
| 4780 | if (I.getPredicate() == FCmpInst::FCMP_TRUE) |
| 4781 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 1)); |
| 4782 | |
| 4783 | // Simplify 'fcmp pred X, X' |
| 4784 | if (Op0 == Op1) { |
| 4785 | switch (I.getPredicate()) { |
| 4786 | default: assert(0 && "Unknown predicate!"); |
| 4787 | case FCmpInst::FCMP_UEQ: // True if unordered or equal |
| 4788 | case FCmpInst::FCMP_UGE: // True if unordered, greater than, or equal |
| 4789 | case FCmpInst::FCMP_ULE: // True if unordered, less than, or equal |
| 4790 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 1)); |
| 4791 | case FCmpInst::FCMP_OGT: // True if ordered and greater than |
| 4792 | case FCmpInst::FCMP_OLT: // True if ordered and less than |
| 4793 | case FCmpInst::FCMP_ONE: // True if ordered and operands are unequal |
| 4794 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 0)); |
| 4795 | |
| 4796 | case FCmpInst::FCMP_UNO: // True if unordered: isnan(X) | isnan(Y) |
| 4797 | case FCmpInst::FCMP_ULT: // True if unordered or less than |
| 4798 | case FCmpInst::FCMP_UGT: // True if unordered or greater than |
| 4799 | case FCmpInst::FCMP_UNE: // True if unordered or not equal |
| 4800 | // Canonicalize these to be 'fcmp uno %X, 0.0'. |
| 4801 | I.setPredicate(FCmpInst::FCMP_UNO); |
| 4802 | I.setOperand(1, Constant::getNullValue(Op0->getType())); |
| 4803 | return &I; |
| 4804 | |
| 4805 | case FCmpInst::FCMP_ORD: // True if ordered (no nans) |
| 4806 | case FCmpInst::FCMP_OEQ: // True if ordered and equal |
| 4807 | case FCmpInst::FCMP_OGE: // True if ordered and greater than or equal |
| 4808 | case FCmpInst::FCMP_OLE: // True if ordered and less than or equal |
| 4809 | // Canonicalize these to be 'fcmp ord %X, 0.0'. |
| 4810 | I.setPredicate(FCmpInst::FCMP_ORD); |
| 4811 | I.setOperand(1, Constant::getNullValue(Op0->getType())); |
| 4812 | return &I; |
| 4813 | } |
| 4814 | } |
| 4815 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4816 | if (isa<UndefValue>(Op1)) // fcmp pred X, undef -> undef |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 4817 | return ReplaceInstUsesWith(I, UndefValue::get(Type::Int1Ty)); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4818 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4819 | // Handle fcmp with constant RHS |
| 4820 | if (Constant *RHSC = dyn_cast<Constant>(Op1)) { |
| 4821 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
| 4822 | switch (LHSI->getOpcode()) { |
| 4823 | case Instruction::PHI: |
| 4824 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 4825 | return NV; |
| 4826 | break; |
| 4827 | case Instruction::Select: |
| 4828 | // If either operand of the select is a constant, we can fold the |
| 4829 | // comparison into the select arms, which will cause one to be |
| 4830 | // constant folded and the select turned into a bitwise or. |
| 4831 | Value *Op1 = 0, *Op2 = 0; |
| 4832 | if (LHSI->hasOneUse()) { |
| 4833 | if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) { |
| 4834 | // Fold the known value into the constant operand. |
| 4835 | Op1 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC); |
| 4836 | // Insert a new FCmp of the other select operand. |
| 4837 | Op2 = InsertNewInstBefore(new FCmpInst(I.getPredicate(), |
| 4838 | LHSI->getOperand(2), RHSC, |
| 4839 | I.getName()), I); |
| 4840 | } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) { |
| 4841 | // Fold the known value into the constant operand. |
| 4842 | Op2 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC); |
| 4843 | // Insert a new FCmp of the other select operand. |
| 4844 | Op1 = InsertNewInstBefore(new FCmpInst(I.getPredicate(), |
| 4845 | LHSI->getOperand(1), RHSC, |
| 4846 | I.getName()), I); |
| 4847 | } |
| 4848 | } |
| 4849 | |
| 4850 | if (Op1) |
| 4851 | return new SelectInst(LHSI->getOperand(0), Op1, Op2); |
| 4852 | break; |
| 4853 | } |
| 4854 | } |
| 4855 | |
| 4856 | return Changed ? &I : 0; |
| 4857 | } |
| 4858 | |
| 4859 | Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { |
| 4860 | bool Changed = SimplifyCompare(I); |
| 4861 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 4862 | const Type *Ty = Op0->getType(); |
| 4863 | |
| 4864 | // icmp X, X |
| 4865 | if (Op0 == Op1) |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4866 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
| 4867 | isTrueWhenEqual(I))); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4868 | |
| 4869 | if (isa<UndefValue>(Op1)) // X icmp undef -> undef |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 4870 | return ReplaceInstUsesWith(I, UndefValue::get(Type::Int1Ty)); |
Christopher Lamb | 7a0678c | 2007-12-18 21:32:20 +0000 | [diff] [blame] | 4871 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4872 | // icmp <global/alloca*/null>, <global/alloca*/null> - Global/Stack value |
Chris Lattner | 711b340 | 2004-11-14 07:33:16 +0000 | [diff] [blame] | 4873 | // addresses never equal each other! We already know that Op0 != Op1. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4874 | if ((isa<GlobalValue>(Op0) || isa<AllocaInst>(Op0) || |
| 4875 | isa<ConstantPointerNull>(Op0)) && |
| 4876 | (isa<GlobalValue>(Op1) || isa<AllocaInst>(Op1) || |
Chris Lattner | 711b340 | 2004-11-14 07:33:16 +0000 | [diff] [blame] | 4877 | isa<ConstantPointerNull>(Op1))) |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4878 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
| 4879 | !isTrueWhenEqual(I))); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4880 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4881 | // icmp's with boolean values can always be turned into bitwise operations |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 4882 | if (Ty == Type::Int1Ty) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4883 | switch (I.getPredicate()) { |
| 4884 | default: assert(0 && "Invalid icmp instruction!"); |
| 4885 | case ICmpInst::ICMP_EQ: { // icmp eq bool %A, %B -> ~(A^B) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4886 | Instruction *Xor = BinaryOperator::createXor(Op0, Op1, I.getName()+"tmp"); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4887 | InsertNewInstBefore(Xor, I); |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 4888 | return BinaryOperator::createNot(Xor); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4889 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4890 | case ICmpInst::ICMP_NE: // icmp eq bool %A, %B -> A^B |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 4891 | return BinaryOperator::createXor(Op0, Op1); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4892 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4893 | case ICmpInst::ICMP_UGT: |
| 4894 | case ICmpInst::ICMP_SGT: |
| 4895 | std::swap(Op0, Op1); // Change icmp gt -> icmp lt |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 4896 | // FALL THROUGH |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4897 | case ICmpInst::ICMP_ULT: |
| 4898 | case ICmpInst::ICMP_SLT: { // icmp lt bool A, B -> ~X & Y |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 4899 | Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp"); |
| 4900 | InsertNewInstBefore(Not, I); |
| 4901 | return BinaryOperator::createAnd(Not, Op1); |
| 4902 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4903 | case ICmpInst::ICMP_UGE: |
| 4904 | case ICmpInst::ICMP_SGE: |
| 4905 | std::swap(Op0, Op1); // Change icmp ge -> icmp le |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 4906 | // FALL THROUGH |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4907 | case ICmpInst::ICMP_ULE: |
| 4908 | case ICmpInst::ICMP_SLE: { // icmp le bool %A, %B -> ~A | B |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 4909 | Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp"); |
| 4910 | InsertNewInstBefore(Not, I); |
| 4911 | return BinaryOperator::createOr(Not, Op1); |
| 4912 | } |
| 4913 | } |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4914 | } |
| 4915 | |
Chris Lattner | 2be51ae | 2004-06-09 04:24:29 +0000 | [diff] [blame] | 4916 | // See if we are doing a comparison between a constant and an instruction that |
| 4917 | // can be folded into the comparison. |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4918 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Christopher Lamb | 103e1a3 | 2007-12-20 07:21:11 +0000 | [diff] [blame] | 4919 | Value *A, *B; |
| 4920 | |
Chris Lattner | b656601 | 2008-01-05 01:18:20 +0000 | [diff] [blame] | 4921 | // (icmp ne/eq (sub A B) 0) -> (icmp ne/eq A, B) |
| 4922 | if (I.isEquality() && CI->isNullValue() && |
| 4923 | match(Op0, m_Sub(m_Value(A), m_Value(B)))) { |
| 4924 | // (icmp cond A B) if cond is equality |
| 4925 | return new ICmpInst(I.getPredicate(), A, B); |
Owen Anderson | f5783f8 | 2007-12-28 07:42:12 +0000 | [diff] [blame] | 4926 | } |
Christopher Lamb | 103e1a3 | 2007-12-20 07:21:11 +0000 | [diff] [blame] | 4927 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4928 | switch (I.getPredicate()) { |
| 4929 | default: break; |
| 4930 | case ICmpInst::ICMP_ULT: // A <u MIN -> FALSE |
| 4931 | if (CI->isMinValue(false)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4932 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4933 | if (CI->isMaxValue(false)) // A <u MAX -> A != MAX |
| 4934 | return new ICmpInst(ICmpInst::ICMP_NE, Op0,Op1); |
| 4935 | if (isMinValuePlusOne(CI,false)) // A <u MIN+1 -> A == MIN |
| 4936 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, SubOne(CI)); |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 4937 | // (x <u 2147483648) -> (x >s -1) -> true if sign bit clear |
| 4938 | if (CI->isMinValue(true)) |
| 4939 | return new ICmpInst(ICmpInst::ICMP_SGT, Op0, |
| 4940 | ConstantInt::getAllOnesValue(Op0->getType())); |
| 4941 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4942 | break; |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4943 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4944 | case ICmpInst::ICMP_SLT: |
| 4945 | if (CI->isMinValue(true)) // A <s MIN -> FALSE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4946 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4947 | if (CI->isMaxValue(true)) // A <s MAX -> A != MAX |
| 4948 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
| 4949 | if (isMinValuePlusOne(CI,true)) // A <s MIN+1 -> A == MIN |
| 4950 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, SubOne(CI)); |
| 4951 | break; |
| 4952 | |
| 4953 | case ICmpInst::ICMP_UGT: |
| 4954 | if (CI->isMaxValue(false)) // A >u MAX -> FALSE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4955 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4956 | if (CI->isMinValue(false)) // A >u MIN -> A != MIN |
| 4957 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
| 4958 | if (isMaxValueMinusOne(CI, false)) // A >u MAX-1 -> A == MAX |
| 4959 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, AddOne(CI)); |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 4960 | |
| 4961 | // (x >u 2147483647) -> (x <s 0) -> true if sign bit set |
| 4962 | if (CI->isMaxValue(true)) |
| 4963 | return new ICmpInst(ICmpInst::ICMP_SLT, Op0, |
| 4964 | ConstantInt::getNullValue(Op0->getType())); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4965 | break; |
| 4966 | |
| 4967 | case ICmpInst::ICMP_SGT: |
| 4968 | if (CI->isMaxValue(true)) // A >s MAX -> FALSE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4969 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4970 | if (CI->isMinValue(true)) // A >s MIN -> A != MIN |
| 4971 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
| 4972 | if (isMaxValueMinusOne(CI, true)) // A >s MAX-1 -> A == MAX |
| 4973 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, AddOne(CI)); |
| 4974 | break; |
| 4975 | |
| 4976 | case ICmpInst::ICMP_ULE: |
| 4977 | if (CI->isMaxValue(false)) // A <=u MAX -> TRUE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4978 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4979 | if (CI->isMinValue(false)) // A <=u MIN -> A == MIN |
| 4980 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); |
| 4981 | if (isMaxValueMinusOne(CI,false)) // A <=u MAX-1 -> A != MAX |
| 4982 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, AddOne(CI)); |
| 4983 | break; |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4984 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4985 | case ICmpInst::ICMP_SLE: |
| 4986 | if (CI->isMaxValue(true)) // A <=s MAX -> TRUE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4987 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4988 | if (CI->isMinValue(true)) // A <=s MIN -> A == MIN |
| 4989 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); |
| 4990 | if (isMaxValueMinusOne(CI,true)) // A <=s MAX-1 -> A != MAX |
| 4991 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, AddOne(CI)); |
| 4992 | break; |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4993 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4994 | case ICmpInst::ICMP_UGE: |
| 4995 | if (CI->isMinValue(false)) // A >=u MIN -> TRUE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4996 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4997 | if (CI->isMaxValue(false)) // A >=u MAX -> A == MAX |
| 4998 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); |
| 4999 | if (isMinValuePlusOne(CI,false)) // A >=u MIN-1 -> A != MIN |
| 5000 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, SubOne(CI)); |
| 5001 | break; |
| 5002 | |
| 5003 | case ICmpInst::ICMP_SGE: |
| 5004 | if (CI->isMinValue(true)) // A >=s MIN -> TRUE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5005 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5006 | if (CI->isMaxValue(true)) // A >=s MAX -> A == MAX |
| 5007 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); |
| 5008 | if (isMinValuePlusOne(CI,true)) // A >=s MIN-1 -> A != MIN |
| 5009 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, SubOne(CI)); |
| 5010 | break; |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 5011 | } |
| 5012 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5013 | // If we still have a icmp le or icmp ge instruction, turn it into the |
| 5014 | // appropriate icmp lt or icmp gt instruction. Since the border cases have |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 5015 | // already been handled above, this requires little checking. |
| 5016 | // |
Reid Spencer | 2149a9d | 2007-03-25 19:55:33 +0000 | [diff] [blame] | 5017 | switch (I.getPredicate()) { |
Chris Lattner | 4241e4d | 2007-07-15 20:54:51 +0000 | [diff] [blame] | 5018 | default: break; |
| 5019 | case ICmpInst::ICMP_ULE: |
| 5020 | return new ICmpInst(ICmpInst::ICMP_ULT, Op0, AddOne(CI)); |
| 5021 | case ICmpInst::ICMP_SLE: |
| 5022 | return new ICmpInst(ICmpInst::ICMP_SLT, Op0, AddOne(CI)); |
| 5023 | case ICmpInst::ICMP_UGE: |
| 5024 | return new ICmpInst( ICmpInst::ICMP_UGT, Op0, SubOne(CI)); |
| 5025 | case ICmpInst::ICMP_SGE: |
| 5026 | return new ICmpInst(ICmpInst::ICMP_SGT, Op0, SubOne(CI)); |
Reid Spencer | 2149a9d | 2007-03-25 19:55:33 +0000 | [diff] [blame] | 5027 | } |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 5028 | |
| 5029 | // 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] | 5030 | // in the input. If this comparison is a normal comparison, it demands all |
| 5031 | // bits, if it is a sign bit comparison, it only demands the sign bit. |
| 5032 | |
| 5033 | bool UnusedBit; |
| 5034 | bool isSignBit = isSignBitCheck(I.getPredicate(), CI, UnusedBit); |
| 5035 | |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5036 | uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth(); |
| 5037 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
Chris Lattner | 4241e4d | 2007-07-15 20:54:51 +0000 | [diff] [blame] | 5038 | if (SimplifyDemandedBits(Op0, |
| 5039 | isSignBit ? APInt::getSignBit(BitWidth) |
| 5040 | : APInt::getAllOnesValue(BitWidth), |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 5041 | KnownZero, KnownOne, 0)) |
| 5042 | return &I; |
| 5043 | |
| 5044 | // Given the known and unknown bits, compute a range that the LHS could be |
| 5045 | // in. |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5046 | if ((KnownOne | KnownZero) != 0) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5047 | // Compute the Min, Max and RHS values based on the known bits. For the |
| 5048 | // EQ and NE we use unsigned values. |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 5049 | APInt Min(BitWidth, 0), Max(BitWidth, 0); |
| 5050 | const APInt& RHSVal = CI->getValue(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5051 | if (ICmpInst::isSignedPredicate(I.getPredicate())) { |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5052 | ComputeSignedMinMaxValuesFromKnownBits(Ty, KnownZero, KnownOne, Min, |
| 5053 | Max); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5054 | } else { |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5055 | ComputeUnsignedMinMaxValuesFromKnownBits(Ty, KnownZero, KnownOne, Min, |
| 5056 | Max); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5057 | } |
| 5058 | switch (I.getPredicate()) { // LE/GE have been folded already. |
| 5059 | default: assert(0 && "Unknown icmp opcode!"); |
| 5060 | case ICmpInst::ICMP_EQ: |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5061 | if (Max.ult(RHSVal) || Min.ugt(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5062 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5063 | break; |
| 5064 | case ICmpInst::ICMP_NE: |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5065 | if (Max.ult(RHSVal) || Min.ugt(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5066 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5067 | break; |
| 5068 | case ICmpInst::ICMP_ULT: |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5069 | if (Max.ult(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5070 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | 81973ef | 2007-04-09 23:52:13 +0000 | [diff] [blame] | 5071 | if (Min.uge(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5072 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5073 | break; |
| 5074 | case ICmpInst::ICMP_UGT: |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5075 | if (Min.ugt(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5076 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | 81973ef | 2007-04-09 23:52:13 +0000 | [diff] [blame] | 5077 | if (Max.ule(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5078 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5079 | break; |
| 5080 | case ICmpInst::ICMP_SLT: |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5081 | if (Max.slt(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5082 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5083 | if (Min.sgt(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5084 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5085 | break; |
| 5086 | case ICmpInst::ICMP_SGT: |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5087 | if (Min.sgt(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5088 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | 81973ef | 2007-04-09 23:52:13 +0000 | [diff] [blame] | 5089 | if (Max.sle(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5090 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5091 | break; |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 5092 | } |
| 5093 | } |
| 5094 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5095 | // 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] | 5096 | // instruction, see if that instruction also has constants so that the |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5097 | // instruction can be folded into the icmp |
Chris Lattner | 3c6a0d4 | 2004-05-25 06:32:08 +0000 | [diff] [blame] | 5098 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5099 | if (Instruction *Res = visitICmpInstWithInstAndIntCst(I, LHSI, CI)) |
| 5100 | return Res; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5101 | } |
| 5102 | |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5103 | // Handle icmp with constant (but not simple integer constant) RHS |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5104 | if (Constant *RHSC = dyn_cast<Constant>(Op1)) { |
| 5105 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
| 5106 | switch (LHSI->getOpcode()) { |
Chris Lattner | 9fb25db | 2005-05-01 04:42:15 +0000 | [diff] [blame] | 5107 | case Instruction::GetElementPtr: |
| 5108 | if (RHSC->isNullValue()) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5109 | // 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] | 5110 | bool isAllZeros = true; |
| 5111 | for (unsigned i = 1, e = LHSI->getNumOperands(); i != e; ++i) |
| 5112 | if (!isa<Constant>(LHSI->getOperand(i)) || |
| 5113 | !cast<Constant>(LHSI->getOperand(i))->isNullValue()) { |
| 5114 | isAllZeros = false; |
| 5115 | break; |
| 5116 | } |
| 5117 | if (isAllZeros) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5118 | return new ICmpInst(I.getPredicate(), LHSI->getOperand(0), |
Chris Lattner | 9fb25db | 2005-05-01 04:42:15 +0000 | [diff] [blame] | 5119 | Constant::getNullValue(LHSI->getOperand(0)->getType())); |
| 5120 | } |
| 5121 | break; |
| 5122 | |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5123 | case Instruction::PHI: |
| 5124 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 5125 | return NV; |
| 5126 | break; |
Chris Lattner | 4802d90 | 2007-04-06 18:57:34 +0000 | [diff] [blame] | 5127 | case Instruction::Select: { |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5128 | // If either operand of the select is a constant, we can fold the |
| 5129 | // comparison into the select arms, which will cause one to be |
| 5130 | // constant folded and the select turned into a bitwise or. |
| 5131 | Value *Op1 = 0, *Op2 = 0; |
| 5132 | if (LHSI->hasOneUse()) { |
| 5133 | if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) { |
| 5134 | // Fold the known value into the constant operand. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5135 | Op1 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC); |
| 5136 | // Insert a new ICmp of the other select operand. |
| 5137 | Op2 = InsertNewInstBefore(new ICmpInst(I.getPredicate(), |
| 5138 | LHSI->getOperand(2), RHSC, |
| 5139 | I.getName()), I); |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5140 | } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) { |
| 5141 | // Fold the known value into the constant operand. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5142 | Op2 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC); |
| 5143 | // Insert a new ICmp of the other select operand. |
| 5144 | Op1 = InsertNewInstBefore(new ICmpInst(I.getPredicate(), |
| 5145 | LHSI->getOperand(1), RHSC, |
| 5146 | I.getName()), I); |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5147 | } |
| 5148 | } |
Jeff Cohen | 9d80930 | 2005-04-23 21:38:35 +0000 | [diff] [blame] | 5149 | |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5150 | if (Op1) |
| 5151 | return new SelectInst(LHSI->getOperand(0), Op1, Op2); |
| 5152 | break; |
| 5153 | } |
Chris Lattner | 4802d90 | 2007-04-06 18:57:34 +0000 | [diff] [blame] | 5154 | case Instruction::Malloc: |
| 5155 | // If we have (malloc != null), and if the malloc has a single use, we |
| 5156 | // can assume it is successful and remove the malloc. |
| 5157 | if (LHSI->hasOneUse() && isa<ConstantPointerNull>(RHSC)) { |
| 5158 | AddToWorkList(LHSI); |
| 5159 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
| 5160 | !isTrueWhenEqual(I))); |
| 5161 | } |
| 5162 | break; |
| 5163 | } |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5164 | } |
| 5165 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5166 | // 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] | 5167 | if (User *GEP = dyn_castGetElementPtr(Op0)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5168 | if (Instruction *NI = FoldGEPICmp(GEP, Op1, I.getPredicate(), I)) |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5169 | return NI; |
| 5170 | if (User *GEP = dyn_castGetElementPtr(Op1)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5171 | if (Instruction *NI = FoldGEPICmp(GEP, Op0, |
| 5172 | ICmpInst::getSwappedPredicate(I.getPredicate()), I)) |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5173 | return NI; |
| 5174 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5175 | // 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] | 5176 | // values. If the ptr->ptr cast can be stripped off both arguments, we do so |
| 5177 | // now. |
| 5178 | if (BitCastInst *CI = dyn_cast<BitCastInst>(Op0)) { |
| 5179 | if (isa<PointerType>(Op0->getType()) && |
| 5180 | (isa<Constant>(Op1) || isa<BitCastInst>(Op1))) { |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5181 | // We keep moving the cast from the left operand over to the right |
| 5182 | // operand, where it can often be eliminated completely. |
Chris Lattner | 57d8637 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 5183 | Op0 = CI->getOperand(0); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5184 | |
Chris Lattner | 57d8637 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 5185 | // If operand #1 is a bitcast instruction, it must also be a ptr->ptr cast |
| 5186 | // so eliminate it as well. |
| 5187 | if (BitCastInst *CI2 = dyn_cast<BitCastInst>(Op1)) |
| 5188 | Op1 = CI2->getOperand(0); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5189 | |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5190 | // 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] | 5191 | if (Op0->getType() != Op1->getType()) { |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5192 | if (Constant *Op1C = dyn_cast<Constant>(Op1)) { |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 5193 | Op1 = ConstantExpr::getBitCast(Op1C, Op0->getType()); |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5194 | } else { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5195 | // Otherwise, cast the RHS right before the icmp |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 5196 | Op1 = InsertBitCastBefore(Op1, Op0->getType(), I); |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5197 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 5198 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5199 | return new ICmpInst(I.getPredicate(), Op0, Op1); |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5200 | } |
Chris Lattner | 57d8637 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 5201 | } |
| 5202 | |
| 5203 | if (isa<CastInst>(Op0)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5204 | // Handle the special case of: icmp (cast bool to X), <cst> |
Chris Lattner | 6870805 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 5205 | // This comes up when you have code like |
| 5206 | // int X = A < B; |
| 5207 | // if (X) ... |
| 5208 | // For generality, we handle any zero-extension of any operand comparison |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5209 | // with a constant or another cast from the same type. |
| 5210 | if (isa<ConstantInt>(Op1) || isa<CastInst>(Op1)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5211 | if (Instruction *R = visitICmpInstWithCastAndCast(I)) |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5212 | return R; |
Chris Lattner | 6870805 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 5213 | } |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5214 | |
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 5215 | if (I.isEquality()) { |
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5216 | Value *A, *B, *C, *D; |
| 5217 | if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) { |
| 5218 | if (A == Op1 || B == Op1) { // (A^B) == A -> B == 0 |
| 5219 | Value *OtherVal = A == Op1 ? B : A; |
| 5220 | return new ICmpInst(I.getPredicate(), OtherVal, |
| 5221 | Constant::getNullValue(A->getType())); |
| 5222 | } |
| 5223 | |
| 5224 | if (match(Op1, m_Xor(m_Value(C), m_Value(D)))) { |
| 5225 | // A^c1 == C^c2 --> A == C^(c1^c2) |
| 5226 | if (ConstantInt *C1 = dyn_cast<ConstantInt>(B)) |
| 5227 | if (ConstantInt *C2 = dyn_cast<ConstantInt>(D)) |
| 5228 | if (Op1->hasOneUse()) { |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 5229 | Constant *NC = ConstantInt::get(C1->getValue() ^ C2->getValue()); |
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5230 | Instruction *Xor = BinaryOperator::createXor(C, NC, "tmp"); |
| 5231 | return new ICmpInst(I.getPredicate(), A, |
| 5232 | InsertNewInstBefore(Xor, I)); |
| 5233 | } |
| 5234 | |
| 5235 | // A^B == A^D -> B == D |
| 5236 | if (A == C) return new ICmpInst(I.getPredicate(), B, D); |
| 5237 | if (A == D) return new ICmpInst(I.getPredicate(), B, C); |
| 5238 | if (B == C) return new ICmpInst(I.getPredicate(), A, D); |
| 5239 | if (B == D) return new ICmpInst(I.getPredicate(), A, C); |
| 5240 | } |
| 5241 | } |
| 5242 | |
| 5243 | if (match(Op1, m_Xor(m_Value(A), m_Value(B))) && |
| 5244 | (A == Op0 || B == Op0)) { |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5245 | // A == (A^B) -> B == 0 |
| 5246 | Value *OtherVal = A == Op0 ? B : A; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5247 | return new ICmpInst(I.getPredicate(), OtherVal, |
| 5248 | Constant::getNullValue(A->getType())); |
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5249 | } |
| 5250 | 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] | 5251 | // (A-B) == A -> B == 0 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5252 | return new ICmpInst(I.getPredicate(), B, |
| 5253 | Constant::getNullValue(B->getType())); |
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5254 | } |
| 5255 | 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] | 5256 | // A == (A-B) -> B == 0 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5257 | return new ICmpInst(I.getPredicate(), B, |
| 5258 | Constant::getNullValue(B->getType())); |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5259 | } |
Chris Lattner | 9c2328e | 2006-11-14 06:06:06 +0000 | [diff] [blame] | 5260 | |
Chris Lattner | 9c2328e | 2006-11-14 06:06:06 +0000 | [diff] [blame] | 5261 | // (X&Z) == (Y&Z) -> (X^Y) & Z == 0 |
| 5262 | if (Op0->hasOneUse() && Op1->hasOneUse() && |
| 5263 | match(Op0, m_And(m_Value(A), m_Value(B))) && |
| 5264 | match(Op1, m_And(m_Value(C), m_Value(D)))) { |
| 5265 | Value *X = 0, *Y = 0, *Z = 0; |
| 5266 | |
| 5267 | if (A == C) { |
| 5268 | X = B; Y = D; Z = A; |
| 5269 | } else if (A == D) { |
| 5270 | X = B; Y = C; Z = A; |
| 5271 | } else if (B == C) { |
| 5272 | X = A; Y = D; Z = B; |
| 5273 | } else if (B == D) { |
| 5274 | X = A; Y = C; Z = B; |
| 5275 | } |
| 5276 | |
| 5277 | if (X) { // Build (X^Y) & Z |
| 5278 | Op1 = InsertNewInstBefore(BinaryOperator::createXor(X, Y, "tmp"), I); |
| 5279 | Op1 = InsertNewInstBefore(BinaryOperator::createAnd(Op1, Z, "tmp"), I); |
| 5280 | I.setOperand(0, Op1); |
| 5281 | I.setOperand(1, Constant::getNullValue(Op1->getType())); |
| 5282 | return &I; |
| 5283 | } |
| 5284 | } |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5285 | } |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 5286 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5287 | } |
| 5288 | |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5289 | |
| 5290 | /// FoldICmpDivCst - Fold "icmp pred, ([su]div X, DivRHS), CmpRHS" where DivRHS |
| 5291 | /// and CmpRHS are both known to be integer constants. |
| 5292 | Instruction *InstCombiner::FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI, |
| 5293 | ConstantInt *DivRHS) { |
| 5294 | ConstantInt *CmpRHS = cast<ConstantInt>(ICI.getOperand(1)); |
| 5295 | const APInt &CmpRHSV = CmpRHS->getValue(); |
| 5296 | |
| 5297 | // FIXME: If the operand types don't match the type of the divide |
| 5298 | // then don't attempt this transform. The code below doesn't have the |
| 5299 | // logic to deal with a signed divide and an unsigned compare (and |
| 5300 | // vice versa). This is because (x /s C1) <s C2 produces different |
| 5301 | // results than (x /s C1) <u C2 or (x /u C1) <s C2 or even |
| 5302 | // (x /u C1) <u C2. Simply casting the operands and result won't |
| 5303 | // work. :( The if statement below tests that condition and bails |
| 5304 | // if it finds it. |
| 5305 | bool DivIsSigned = DivI->getOpcode() == Instruction::SDiv; |
| 5306 | if (!ICI.isEquality() && DivIsSigned != ICI.isSignedPredicate()) |
| 5307 | return 0; |
| 5308 | if (DivRHS->isZero()) |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5309 | return 0; // The ProdOV computation fails on divide by zero. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5310 | |
| 5311 | // Compute Prod = CI * DivRHS. We are essentially solving an equation |
| 5312 | // of form X/C1=C2. We solve for X by multiplying C1 (DivRHS) and |
| 5313 | // C2 (CI). By solving for X we can turn this into a range check |
| 5314 | // instead of computing a divide. |
| 5315 | ConstantInt *Prod = Multiply(CmpRHS, DivRHS); |
| 5316 | |
| 5317 | // Determine if the product overflows by seeing if the product is |
| 5318 | // not equal to the divide. Make sure we do the same kind of divide |
| 5319 | // as in the LHS instruction that we're folding. |
| 5320 | bool ProdOV = (DivIsSigned ? ConstantExpr::getSDiv(Prod, DivRHS) : |
| 5321 | ConstantExpr::getUDiv(Prod, DivRHS)) != CmpRHS; |
| 5322 | |
| 5323 | // Get the ICmp opcode |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5324 | ICmpInst::Predicate Pred = ICI.getPredicate(); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5325 | |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5326 | // Figure out the interval that is being checked. For example, a comparison |
| 5327 | // like "X /u 5 == 0" is really checking that X is in the interval [0, 5). |
| 5328 | // Compute this interval based on the constants involved and the signedness of |
| 5329 | // the compare/divide. This computes a half-open interval, keeping track of |
| 5330 | // whether either value in the interval overflows. After analysis each |
| 5331 | // overflow variable is set to 0 if it's corresponding bound variable is valid |
| 5332 | // -1 if overflowed off the bottom end, or +1 if overflowed off the top end. |
| 5333 | int LoOverflow = 0, HiOverflow = 0; |
| 5334 | ConstantInt *LoBound = 0, *HiBound = 0; |
| 5335 | |
| 5336 | |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5337 | if (!DivIsSigned) { // udiv |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5338 | // e.g. X/5 op 3 --> [15, 20) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5339 | LoBound = Prod; |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5340 | HiOverflow = LoOverflow = ProdOV; |
| 5341 | if (!HiOverflow) |
| 5342 | HiOverflow = AddWithOverflow(HiBound, LoBound, DivRHS, false); |
Dan Gohman | 7649127 | 2008-02-13 22:09:18 +0000 | [diff] [blame] | 5343 | } else if (DivRHS->getValue().isStrictlyPositive()) { // Divisor is > 0. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5344 | if (CmpRHSV == 0) { // (X / pos) op 0 |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5345 | // Can't overflow. e.g. X/2 op 0 --> [-1, 2) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5346 | LoBound = cast<ConstantInt>(ConstantExpr::getNeg(SubOne(DivRHS))); |
| 5347 | HiBound = DivRHS; |
Dan Gohman | 7649127 | 2008-02-13 22:09:18 +0000 | [diff] [blame] | 5348 | } else if (CmpRHSV.isStrictlyPositive()) { // (X / pos) op pos |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5349 | LoBound = Prod; // e.g. X/5 op 3 --> [15, 20) |
| 5350 | HiOverflow = LoOverflow = ProdOV; |
| 5351 | if (!HiOverflow) |
| 5352 | HiOverflow = AddWithOverflow(HiBound, Prod, DivRHS, true); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5353 | } else { // (X / pos) op neg |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5354 | // e.g. X/5 op -3 --> [-15-4, -15+1) --> [-19, -14) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5355 | Constant *DivRHSH = ConstantExpr::getNeg(SubOne(DivRHS)); |
| 5356 | LoOverflow = AddWithOverflow(LoBound, Prod, |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5357 | cast<ConstantInt>(DivRHSH), true) ? -1 : 0; |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5358 | HiBound = AddOne(Prod); |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5359 | HiOverflow = ProdOV ? -1 : 0; |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5360 | } |
Dan Gohman | 7649127 | 2008-02-13 22:09:18 +0000 | [diff] [blame] | 5361 | } else if (DivRHS->getValue().isNegative()) { // Divisor is < 0. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5362 | if (CmpRHSV == 0) { // (X / neg) op 0 |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5363 | // e.g. X/-5 op 0 --> [-4, 5) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5364 | LoBound = AddOne(DivRHS); |
| 5365 | HiBound = cast<ConstantInt>(ConstantExpr::getNeg(DivRHS)); |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5366 | if (HiBound == DivRHS) { // -INTMIN = INTMIN |
| 5367 | HiOverflow = 1; // [INTMIN+1, overflow) |
| 5368 | HiBound = 0; // e.g. X/INTMIN = 0 --> X > INTMIN |
| 5369 | } |
Dan Gohman | 7649127 | 2008-02-13 22:09:18 +0000 | [diff] [blame] | 5370 | } else if (CmpRHSV.isStrictlyPositive()) { // (X / neg) op pos |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5371 | // e.g. X/-5 op 3 --> [-19, -14) |
| 5372 | HiOverflow = LoOverflow = ProdOV ? -1 : 0; |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5373 | if (!LoOverflow) |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5374 | LoOverflow = AddWithOverflow(LoBound, Prod, AddOne(DivRHS), true) ?-1:0; |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5375 | HiBound = AddOne(Prod); |
| 5376 | } else { // (X / neg) op neg |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5377 | // e.g. X/-5 op -3 --> [15, 20) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5378 | LoBound = Prod; |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5379 | LoOverflow = HiOverflow = ProdOV ? 1 : 0; |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5380 | HiBound = Subtract(Prod, DivRHS); |
| 5381 | } |
| 5382 | |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5383 | // Dividing by a negative swaps the condition. LT <-> GT |
| 5384 | Pred = ICmpInst::getSwappedPredicate(Pred); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5385 | } |
| 5386 | |
| 5387 | Value *X = DivI->getOperand(0); |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5388 | switch (Pred) { |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5389 | default: assert(0 && "Unhandled icmp opcode!"); |
| 5390 | case ICmpInst::ICMP_EQ: |
| 5391 | if (LoOverflow && HiOverflow) |
| 5392 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
| 5393 | else if (HiOverflow) |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5394 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5395 | ICmpInst::ICMP_UGE, X, LoBound); |
| 5396 | else if (LoOverflow) |
| 5397 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : |
| 5398 | ICmpInst::ICMP_ULT, X, HiBound); |
| 5399 | else |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5400 | return InsertRangeTest(X, LoBound, HiBound, DivIsSigned, true, ICI); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5401 | case ICmpInst::ICMP_NE: |
| 5402 | if (LoOverflow && HiOverflow) |
| 5403 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); |
| 5404 | else if (HiOverflow) |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5405 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5406 | ICmpInst::ICMP_ULT, X, LoBound); |
| 5407 | else if (LoOverflow) |
| 5408 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : |
| 5409 | ICmpInst::ICMP_UGE, X, HiBound); |
| 5410 | else |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5411 | return InsertRangeTest(X, LoBound, HiBound, DivIsSigned, false, ICI); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5412 | case ICmpInst::ICMP_ULT: |
| 5413 | case ICmpInst::ICMP_SLT: |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5414 | if (LoOverflow == +1) // Low bound is greater than input range. |
| 5415 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); |
| 5416 | if (LoOverflow == -1) // Low bound is less than input range. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5417 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5418 | return new ICmpInst(Pred, X, LoBound); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5419 | case ICmpInst::ICMP_UGT: |
| 5420 | case ICmpInst::ICMP_SGT: |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5421 | if (HiOverflow == +1) // High bound greater than input range. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5422 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5423 | else if (HiOverflow == -1) // High bound less than input range. |
| 5424 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); |
| 5425 | if (Pred == ICmpInst::ICMP_UGT) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5426 | return new ICmpInst(ICmpInst::ICMP_UGE, X, HiBound); |
| 5427 | else |
| 5428 | return new ICmpInst(ICmpInst::ICMP_SGE, X, HiBound); |
| 5429 | } |
| 5430 | } |
| 5431 | |
| 5432 | |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5433 | /// visitICmpInstWithInstAndIntCst - Handle "icmp (instr, intcst)". |
| 5434 | /// |
| 5435 | Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI, |
| 5436 | Instruction *LHSI, |
| 5437 | ConstantInt *RHS) { |
| 5438 | const APInt &RHSV = RHS->getValue(); |
| 5439 | |
| 5440 | switch (LHSI->getOpcode()) { |
Duncan Sands | 0091bf2 | 2007-04-04 06:42:45 +0000 | [diff] [blame] | 5441 | case Instruction::Xor: // (icmp pred (xor X, XorCST), CI) |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5442 | if (ConstantInt *XorCST = dyn_cast<ConstantInt>(LHSI->getOperand(1))) { |
| 5443 | // If this is a comparison that tests the signbit (X < 0) or (x > -1), |
| 5444 | // fold the xor. |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 5445 | if ((ICI.getPredicate() == ICmpInst::ICMP_SLT && RHSV == 0) || |
| 5446 | (ICI.getPredicate() == ICmpInst::ICMP_SGT && RHSV.isAllOnesValue())) { |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5447 | Value *CompareVal = LHSI->getOperand(0); |
| 5448 | |
| 5449 | // If the sign bit of the XorCST is not set, there is no change to |
| 5450 | // the operation, just stop using the Xor. |
| 5451 | if (!XorCST->getValue().isNegative()) { |
| 5452 | ICI.setOperand(0, CompareVal); |
| 5453 | AddToWorkList(LHSI); |
| 5454 | return &ICI; |
| 5455 | } |
| 5456 | |
| 5457 | // Was the old condition true if the operand is positive? |
| 5458 | bool isTrueIfPositive = ICI.getPredicate() == ICmpInst::ICMP_SGT; |
| 5459 | |
| 5460 | // If so, the new one isn't. |
| 5461 | isTrueIfPositive ^= true; |
| 5462 | |
| 5463 | if (isTrueIfPositive) |
| 5464 | return new ICmpInst(ICmpInst::ICMP_SGT, CompareVal, SubOne(RHS)); |
| 5465 | else |
| 5466 | return new ICmpInst(ICmpInst::ICMP_SLT, CompareVal, AddOne(RHS)); |
| 5467 | } |
| 5468 | } |
| 5469 | break; |
| 5470 | case Instruction::And: // (icmp pred (and X, AndCST), RHS) |
| 5471 | if (LHSI->hasOneUse() && isa<ConstantInt>(LHSI->getOperand(1)) && |
| 5472 | LHSI->getOperand(0)->hasOneUse()) { |
| 5473 | ConstantInt *AndCST = cast<ConstantInt>(LHSI->getOperand(1)); |
| 5474 | |
| 5475 | // If the LHS is an AND of a truncating cast, we can widen the |
| 5476 | // and/compare to be the input width without changing the value |
| 5477 | // produced, eliminating a cast. |
| 5478 | if (TruncInst *Cast = dyn_cast<TruncInst>(LHSI->getOperand(0))) { |
| 5479 | // We can do this transformation if either the AND constant does not |
| 5480 | // have its sign bit set or if it is an equality comparison. |
| 5481 | // Extending a relational comparison when we're checking the sign |
| 5482 | // bit would not work. |
| 5483 | if (Cast->hasOneUse() && |
Anton Korobeynikov | 4aefd6b | 2008-02-20 12:07:57 +0000 | [diff] [blame] | 5484 | (ICI.isEquality() || |
| 5485 | (AndCST->getValue().isNonNegative() && RHSV.isNonNegative()))) { |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5486 | uint32_t BitWidth = |
| 5487 | cast<IntegerType>(Cast->getOperand(0)->getType())->getBitWidth(); |
| 5488 | APInt NewCST = AndCST->getValue(); |
| 5489 | NewCST.zext(BitWidth); |
| 5490 | APInt NewCI = RHSV; |
| 5491 | NewCI.zext(BitWidth); |
| 5492 | Instruction *NewAnd = |
| 5493 | BinaryOperator::createAnd(Cast->getOperand(0), |
| 5494 | ConstantInt::get(NewCST),LHSI->getName()); |
| 5495 | InsertNewInstBefore(NewAnd, ICI); |
| 5496 | return new ICmpInst(ICI.getPredicate(), NewAnd, |
| 5497 | ConstantInt::get(NewCI)); |
| 5498 | } |
| 5499 | } |
| 5500 | |
| 5501 | // If this is: (X >> C1) & C2 != C3 (where any shift and any compare |
| 5502 | // could exist), turn it into (X & (C2 << C1)) != (C3 << C1). This |
| 5503 | // happens a LOT in code produced by the C front-end, for bitfield |
| 5504 | // access. |
| 5505 | BinaryOperator *Shift = dyn_cast<BinaryOperator>(LHSI->getOperand(0)); |
| 5506 | if (Shift && !Shift->isShift()) |
| 5507 | Shift = 0; |
| 5508 | |
| 5509 | ConstantInt *ShAmt; |
| 5510 | ShAmt = Shift ? dyn_cast<ConstantInt>(Shift->getOperand(1)) : 0; |
| 5511 | const Type *Ty = Shift ? Shift->getType() : 0; // Type of the shift. |
| 5512 | const Type *AndTy = AndCST->getType(); // Type of the and. |
| 5513 | |
| 5514 | // We can fold this as long as we can't shift unknown bits |
| 5515 | // into the mask. This can only happen with signed shift |
| 5516 | // rights, as they sign-extend. |
| 5517 | if (ShAmt) { |
| 5518 | bool CanFold = Shift->isLogicalShift(); |
| 5519 | if (!CanFold) { |
| 5520 | // To test for the bad case of the signed shr, see if any |
| 5521 | // of the bits shifted in could be tested after the mask. |
| 5522 | uint32_t TyBits = Ty->getPrimitiveSizeInBits(); |
| 5523 | int ShAmtVal = TyBits - ShAmt->getLimitedValue(TyBits); |
| 5524 | |
| 5525 | uint32_t BitWidth = AndTy->getPrimitiveSizeInBits(); |
| 5526 | if ((APInt::getHighBitsSet(BitWidth, BitWidth-ShAmtVal) & |
| 5527 | AndCST->getValue()) == 0) |
| 5528 | CanFold = true; |
| 5529 | } |
| 5530 | |
| 5531 | if (CanFold) { |
| 5532 | Constant *NewCst; |
| 5533 | if (Shift->getOpcode() == Instruction::Shl) |
| 5534 | NewCst = ConstantExpr::getLShr(RHS, ShAmt); |
| 5535 | else |
| 5536 | NewCst = ConstantExpr::getShl(RHS, ShAmt); |
| 5537 | |
| 5538 | // Check to see if we are shifting out any of the bits being |
| 5539 | // compared. |
| 5540 | if (ConstantExpr::get(Shift->getOpcode(), NewCst, ShAmt) != RHS) { |
| 5541 | // If we shifted bits out, the fold is not going to work out. |
| 5542 | // As a special case, check to see if this means that the |
| 5543 | // result is always true or false now. |
| 5544 | if (ICI.getPredicate() == ICmpInst::ICMP_EQ) |
| 5545 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
| 5546 | if (ICI.getPredicate() == ICmpInst::ICMP_NE) |
| 5547 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); |
| 5548 | } else { |
| 5549 | ICI.setOperand(1, NewCst); |
| 5550 | Constant *NewAndCST; |
| 5551 | if (Shift->getOpcode() == Instruction::Shl) |
| 5552 | NewAndCST = ConstantExpr::getLShr(AndCST, ShAmt); |
| 5553 | else |
| 5554 | NewAndCST = ConstantExpr::getShl(AndCST, ShAmt); |
| 5555 | LHSI->setOperand(1, NewAndCST); |
| 5556 | LHSI->setOperand(0, Shift->getOperand(0)); |
| 5557 | AddToWorkList(Shift); // Shift is dead. |
| 5558 | AddUsesToWorkList(ICI); |
| 5559 | return &ICI; |
| 5560 | } |
| 5561 | } |
| 5562 | } |
| 5563 | |
| 5564 | // Turn ((X >> Y) & C) == 0 into (X & (C << Y)) == 0. The later is |
| 5565 | // preferable because it allows the C<<Y expression to be hoisted out |
| 5566 | // of a loop if Y is invariant and X is not. |
| 5567 | if (Shift && Shift->hasOneUse() && RHSV == 0 && |
| 5568 | ICI.isEquality() && !Shift->isArithmeticShift() && |
| 5569 | isa<Instruction>(Shift->getOperand(0))) { |
| 5570 | // Compute C << Y. |
| 5571 | Value *NS; |
| 5572 | if (Shift->getOpcode() == Instruction::LShr) { |
| 5573 | NS = BinaryOperator::createShl(AndCST, |
| 5574 | Shift->getOperand(1), "tmp"); |
| 5575 | } else { |
| 5576 | // Insert a logical shift. |
| 5577 | NS = BinaryOperator::createLShr(AndCST, |
| 5578 | Shift->getOperand(1), "tmp"); |
| 5579 | } |
| 5580 | InsertNewInstBefore(cast<Instruction>(NS), ICI); |
| 5581 | |
| 5582 | // Compute X & (C << Y). |
| 5583 | Instruction *NewAnd = |
| 5584 | BinaryOperator::createAnd(Shift->getOperand(0), NS, LHSI->getName()); |
| 5585 | InsertNewInstBefore(NewAnd, ICI); |
| 5586 | |
| 5587 | ICI.setOperand(0, NewAnd); |
| 5588 | return &ICI; |
| 5589 | } |
| 5590 | } |
| 5591 | break; |
| 5592 | |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 5593 | case Instruction::Shl: { // (icmp pred (shl X, ShAmt), CI) |
| 5594 | ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1)); |
| 5595 | if (!ShAmt) break; |
| 5596 | |
| 5597 | uint32_t TypeBits = RHSV.getBitWidth(); |
| 5598 | |
| 5599 | // Check that the shift amount is in range. If not, don't perform |
| 5600 | // undefined shifts. When the shift is visited it will be |
| 5601 | // simplified. |
| 5602 | if (ShAmt->uge(TypeBits)) |
| 5603 | break; |
| 5604 | |
| 5605 | if (ICI.isEquality()) { |
| 5606 | // If we are comparing against bits always shifted out, the |
| 5607 | // comparison cannot succeed. |
| 5608 | Constant *Comp = |
| 5609 | ConstantExpr::getShl(ConstantExpr::getLShr(RHS, ShAmt), ShAmt); |
| 5610 | if (Comp != RHS) {// Comparing against a bit that we know is zero. |
| 5611 | bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE; |
| 5612 | Constant *Cst = ConstantInt::get(Type::Int1Ty, IsICMP_NE); |
| 5613 | return ReplaceInstUsesWith(ICI, Cst); |
| 5614 | } |
| 5615 | |
| 5616 | if (LHSI->hasOneUse()) { |
| 5617 | // Otherwise strength reduce the shift into an and. |
| 5618 | uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits); |
| 5619 | Constant *Mask = |
| 5620 | ConstantInt::get(APInt::getLowBitsSet(TypeBits, TypeBits-ShAmtVal)); |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5621 | |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 5622 | Instruction *AndI = |
| 5623 | BinaryOperator::createAnd(LHSI->getOperand(0), |
| 5624 | Mask, LHSI->getName()+".mask"); |
| 5625 | Value *And = InsertNewInstBefore(AndI, ICI); |
| 5626 | return new ICmpInst(ICI.getPredicate(), And, |
| 5627 | ConstantInt::get(RHSV.lshr(ShAmtVal))); |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5628 | } |
| 5629 | } |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 5630 | |
| 5631 | // Otherwise, if this is a comparison of the sign bit, simplify to and/test. |
| 5632 | bool TrueIfSigned = false; |
| 5633 | if (LHSI->hasOneUse() && |
| 5634 | isSignBitCheck(ICI.getPredicate(), RHS, TrueIfSigned)) { |
| 5635 | // (X << 31) <s 0 --> (X&1) != 0 |
| 5636 | Constant *Mask = ConstantInt::get(APInt(TypeBits, 1) << |
| 5637 | (TypeBits-ShAmt->getZExtValue()-1)); |
| 5638 | Instruction *AndI = |
| 5639 | BinaryOperator::createAnd(LHSI->getOperand(0), |
| 5640 | Mask, LHSI->getName()+".mask"); |
| 5641 | Value *And = InsertNewInstBefore(AndI, ICI); |
| 5642 | |
| 5643 | return new ICmpInst(TrueIfSigned ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ, |
| 5644 | And, Constant::getNullValue(And->getType())); |
| 5645 | } |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5646 | break; |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 5647 | } |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5648 | |
| 5649 | case Instruction::LShr: // (icmp pred (shr X, ShAmt), CI) |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 5650 | case Instruction::AShr: { |
| 5651 | ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1)); |
| 5652 | if (!ShAmt) break; |
| 5653 | |
| 5654 | if (ICI.isEquality()) { |
| 5655 | // Check that the shift amount is in range. If not, don't perform |
| 5656 | // undefined shifts. When the shift is visited it will be |
| 5657 | // simplified. |
| 5658 | uint32_t TypeBits = RHSV.getBitWidth(); |
| 5659 | if (ShAmt->uge(TypeBits)) |
| 5660 | break; |
| 5661 | uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits); |
| 5662 | |
| 5663 | // If we are comparing against bits always shifted out, the |
| 5664 | // comparison cannot succeed. |
| 5665 | APInt Comp = RHSV << ShAmtVal; |
| 5666 | if (LHSI->getOpcode() == Instruction::LShr) |
| 5667 | Comp = Comp.lshr(ShAmtVal); |
| 5668 | else |
| 5669 | Comp = Comp.ashr(ShAmtVal); |
| 5670 | |
| 5671 | if (Comp != RHSV) { // Comparing against a bit that we know is zero. |
| 5672 | bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE; |
| 5673 | Constant *Cst = ConstantInt::get(Type::Int1Ty, IsICMP_NE); |
| 5674 | return ReplaceInstUsesWith(ICI, Cst); |
| 5675 | } |
| 5676 | |
| 5677 | if (LHSI->hasOneUse() || RHSV == 0) { |
| 5678 | // Otherwise strength reduce the shift into an and. |
| 5679 | APInt Val(APInt::getHighBitsSet(TypeBits, TypeBits - ShAmtVal)); |
| 5680 | Constant *Mask = ConstantInt::get(Val); |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5681 | |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 5682 | Instruction *AndI = |
| 5683 | BinaryOperator::createAnd(LHSI->getOperand(0), |
| 5684 | Mask, LHSI->getName()+".mask"); |
| 5685 | Value *And = InsertNewInstBefore(AndI, ICI); |
| 5686 | return new ICmpInst(ICI.getPredicate(), And, |
| 5687 | ConstantExpr::getShl(RHS, ShAmt)); |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5688 | } |
| 5689 | } |
| 5690 | break; |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 5691 | } |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5692 | |
| 5693 | case Instruction::SDiv: |
| 5694 | case Instruction::UDiv: |
| 5695 | // Fold: icmp pred ([us]div X, C1), C2 -> range test |
| 5696 | // Fold this div into the comparison, producing a range check. |
| 5697 | // Determine, based on the divide type, what the range is being |
| 5698 | // checked. If there is an overflow on the low or high side, remember |
| 5699 | // it, otherwise compute the range [low, hi) bounding the new value. |
| 5700 | // See: InsertRangeTest above for the kinds of replacements possible. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5701 | if (ConstantInt *DivRHS = dyn_cast<ConstantInt>(LHSI->getOperand(1))) |
| 5702 | if (Instruction *R = FoldICmpDivCst(ICI, cast<BinaryOperator>(LHSI), |
| 5703 | DivRHS)) |
| 5704 | return R; |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5705 | break; |
Nick Lewycky | 5be2920 | 2008-02-03 16:33:09 +0000 | [diff] [blame] | 5706 | |
| 5707 | case Instruction::Add: |
| 5708 | // Fold: icmp pred (add, X, C1), C2 |
| 5709 | |
| 5710 | if (!ICI.isEquality()) { |
| 5711 | ConstantInt *LHSC = dyn_cast<ConstantInt>(LHSI->getOperand(1)); |
| 5712 | if (!LHSC) break; |
| 5713 | const APInt &LHSV = LHSC->getValue(); |
| 5714 | |
| 5715 | ConstantRange CR = ICI.makeConstantRange(ICI.getPredicate(), RHSV) |
| 5716 | .subtract(LHSV); |
| 5717 | |
| 5718 | if (ICI.isSignedPredicate()) { |
| 5719 | if (CR.getLower().isSignBit()) { |
| 5720 | return new ICmpInst(ICmpInst::ICMP_SLT, LHSI->getOperand(0), |
| 5721 | ConstantInt::get(CR.getUpper())); |
| 5722 | } else if (CR.getUpper().isSignBit()) { |
| 5723 | return new ICmpInst(ICmpInst::ICMP_SGE, LHSI->getOperand(0), |
| 5724 | ConstantInt::get(CR.getLower())); |
| 5725 | } |
| 5726 | } else { |
| 5727 | if (CR.getLower().isMinValue()) { |
| 5728 | return new ICmpInst(ICmpInst::ICMP_ULT, LHSI->getOperand(0), |
| 5729 | ConstantInt::get(CR.getUpper())); |
| 5730 | } else if (CR.getUpper().isMinValue()) { |
| 5731 | return new ICmpInst(ICmpInst::ICMP_UGE, LHSI->getOperand(0), |
| 5732 | ConstantInt::get(CR.getLower())); |
| 5733 | } |
| 5734 | } |
| 5735 | } |
| 5736 | break; |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5737 | } |
| 5738 | |
| 5739 | // Simplify icmp_eq and icmp_ne instructions with integer constant RHS. |
| 5740 | if (ICI.isEquality()) { |
| 5741 | bool isICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE; |
| 5742 | |
| 5743 | // If the first operand is (add|sub|and|or|xor|rem) with a constant, and |
| 5744 | // the second operand is a constant, simplify a bit. |
| 5745 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(LHSI)) { |
| 5746 | switch (BO->getOpcode()) { |
| 5747 | case Instruction::SRem: |
| 5748 | // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one. |
| 5749 | if (RHSV == 0 && isa<ConstantInt>(BO->getOperand(1)) &&BO->hasOneUse()){ |
| 5750 | const APInt &V = cast<ConstantInt>(BO->getOperand(1))->getValue(); |
| 5751 | if (V.sgt(APInt(V.getBitWidth(), 1)) && V.isPowerOf2()) { |
| 5752 | Instruction *NewRem = |
| 5753 | BinaryOperator::createURem(BO->getOperand(0), BO->getOperand(1), |
| 5754 | BO->getName()); |
| 5755 | InsertNewInstBefore(NewRem, ICI); |
| 5756 | return new ICmpInst(ICI.getPredicate(), NewRem, |
| 5757 | Constant::getNullValue(BO->getType())); |
| 5758 | } |
| 5759 | } |
| 5760 | break; |
| 5761 | case Instruction::Add: |
| 5762 | // Replace ((add A, B) != C) with (A != C-B) if B & C are constants. |
| 5763 | if (ConstantInt *BOp1C = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
| 5764 | if (BO->hasOneUse()) |
| 5765 | return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), |
| 5766 | Subtract(RHS, BOp1C)); |
| 5767 | } else if (RHSV == 0) { |
| 5768 | // Replace ((add A, B) != 0) with (A != -B) if A or B is |
| 5769 | // efficiently invertible, or if the add has just this one use. |
| 5770 | Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1); |
| 5771 | |
| 5772 | if (Value *NegVal = dyn_castNegVal(BOp1)) |
| 5773 | return new ICmpInst(ICI.getPredicate(), BOp0, NegVal); |
| 5774 | else if (Value *NegVal = dyn_castNegVal(BOp0)) |
| 5775 | return new ICmpInst(ICI.getPredicate(), NegVal, BOp1); |
| 5776 | else if (BO->hasOneUse()) { |
| 5777 | Instruction *Neg = BinaryOperator::createNeg(BOp1); |
| 5778 | InsertNewInstBefore(Neg, ICI); |
| 5779 | Neg->takeName(BO); |
| 5780 | return new ICmpInst(ICI.getPredicate(), BOp0, Neg); |
| 5781 | } |
| 5782 | } |
| 5783 | break; |
| 5784 | case Instruction::Xor: |
| 5785 | // For the xor case, we can xor two constants together, eliminating |
| 5786 | // the explicit xor. |
| 5787 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) |
| 5788 | return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), |
| 5789 | ConstantExpr::getXor(RHS, BOC)); |
| 5790 | |
| 5791 | // FALLTHROUGH |
| 5792 | case Instruction::Sub: |
| 5793 | // Replace (([sub|xor] A, B) != 0) with (A != B) |
| 5794 | if (RHSV == 0) |
| 5795 | return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), |
| 5796 | BO->getOperand(1)); |
| 5797 | break; |
| 5798 | |
| 5799 | case Instruction::Or: |
| 5800 | // If bits are being or'd in that are not present in the constant we |
| 5801 | // are comparing against, then the comparison could never succeed! |
| 5802 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) { |
| 5803 | Constant *NotCI = ConstantExpr::getNot(RHS); |
| 5804 | if (!ConstantExpr::getAnd(BOC, NotCI)->isNullValue()) |
| 5805 | return ReplaceInstUsesWith(ICI, ConstantInt::get(Type::Int1Ty, |
| 5806 | isICMP_NE)); |
| 5807 | } |
| 5808 | break; |
| 5809 | |
| 5810 | case Instruction::And: |
| 5811 | if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
| 5812 | // If bits are being compared against that are and'd out, then the |
| 5813 | // comparison can never succeed! |
| 5814 | if ((RHSV & ~BOC->getValue()) != 0) |
| 5815 | return ReplaceInstUsesWith(ICI, ConstantInt::get(Type::Int1Ty, |
| 5816 | isICMP_NE)); |
| 5817 | |
| 5818 | // If we have ((X & C) == C), turn it into ((X & C) != 0). |
| 5819 | if (RHS == BOC && RHSV.isPowerOf2()) |
| 5820 | return new ICmpInst(isICMP_NE ? ICmpInst::ICMP_EQ : |
| 5821 | ICmpInst::ICMP_NE, LHSI, |
| 5822 | Constant::getNullValue(RHS->getType())); |
| 5823 | |
| 5824 | // Replace (and X, (1 << size(X)-1) != 0) with x s< 0 |
| 5825 | if (isSignBit(BOC)) { |
| 5826 | Value *X = BO->getOperand(0); |
| 5827 | Constant *Zero = Constant::getNullValue(X->getType()); |
| 5828 | ICmpInst::Predicate pred = isICMP_NE ? |
| 5829 | ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGE; |
| 5830 | return new ICmpInst(pred, X, Zero); |
| 5831 | } |
| 5832 | |
| 5833 | // ((X & ~7) == 0) --> X < 8 |
| 5834 | if (RHSV == 0 && isHighOnes(BOC)) { |
| 5835 | Value *X = BO->getOperand(0); |
| 5836 | Constant *NegX = ConstantExpr::getNeg(BOC); |
| 5837 | ICmpInst::Predicate pred = isICMP_NE ? |
| 5838 | ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT; |
| 5839 | return new ICmpInst(pred, X, NegX); |
| 5840 | } |
| 5841 | } |
| 5842 | default: break; |
| 5843 | } |
| 5844 | } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(LHSI)) { |
| 5845 | // Handle icmp {eq|ne} <intrinsic>, intcst. |
| 5846 | if (II->getIntrinsicID() == Intrinsic::bswap) { |
| 5847 | AddToWorkList(II); |
| 5848 | ICI.setOperand(0, II->getOperand(1)); |
| 5849 | ICI.setOperand(1, ConstantInt::get(RHSV.byteSwap())); |
| 5850 | return &ICI; |
| 5851 | } |
| 5852 | } |
| 5853 | } else { // Not a ICMP_EQ/ICMP_NE |
Chris Lattner | e34e9a2 | 2007-04-14 23:32:02 +0000 | [diff] [blame] | 5854 | // If the LHS is a cast from an integral value of the same size, |
| 5855 | // then since we know the RHS is a constant, try to simlify. |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5856 | if (CastInst *Cast = dyn_cast<CastInst>(LHSI)) { |
| 5857 | Value *CastOp = Cast->getOperand(0); |
| 5858 | const Type *SrcTy = CastOp->getType(); |
| 5859 | uint32_t SrcTySize = SrcTy->getPrimitiveSizeInBits(); |
| 5860 | if (SrcTy->isInteger() && |
| 5861 | SrcTySize == Cast->getType()->getPrimitiveSizeInBits()) { |
| 5862 | // If this is an unsigned comparison, try to make the comparison use |
| 5863 | // smaller constant values. |
| 5864 | if (ICI.getPredicate() == ICmpInst::ICMP_ULT && RHSV.isSignBit()) { |
| 5865 | // X u< 128 => X s> -1 |
| 5866 | return new ICmpInst(ICmpInst::ICMP_SGT, CastOp, |
| 5867 | ConstantInt::get(APInt::getAllOnesValue(SrcTySize))); |
| 5868 | } else if (ICI.getPredicate() == ICmpInst::ICMP_UGT && |
| 5869 | RHSV == APInt::getSignedMaxValue(SrcTySize)) { |
| 5870 | // X u> 127 => X s< 0 |
| 5871 | return new ICmpInst(ICmpInst::ICMP_SLT, CastOp, |
| 5872 | Constant::getNullValue(SrcTy)); |
| 5873 | } |
| 5874 | } |
| 5875 | } |
| 5876 | } |
| 5877 | return 0; |
| 5878 | } |
| 5879 | |
| 5880 | /// visitICmpInstWithCastAndCast - Handle icmp (cast x to y), (cast/cst). |
| 5881 | /// We only handle extending casts so far. |
| 5882 | /// |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5883 | Instruction *InstCombiner::visitICmpInstWithCastAndCast(ICmpInst &ICI) { |
| 5884 | const CastInst *LHSCI = cast<CastInst>(ICI.getOperand(0)); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 5885 | Value *LHSCIOp = LHSCI->getOperand(0); |
| 5886 | const Type *SrcTy = LHSCIOp->getType(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5887 | const Type *DestTy = LHSCI->getType(); |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5888 | Value *RHSCIOp; |
| 5889 | |
Chris Lattner | 8c756c1 | 2007-05-05 22:41:33 +0000 | [diff] [blame] | 5890 | // Turn icmp (ptrtoint x), (ptrtoint/c) into a compare of the input if the |
| 5891 | // integer type is the same size as the pointer type. |
| 5892 | if (LHSCI->getOpcode() == Instruction::PtrToInt && |
| 5893 | getTargetData().getPointerSizeInBits() == |
| 5894 | cast<IntegerType>(DestTy)->getBitWidth()) { |
| 5895 | Value *RHSOp = 0; |
| 5896 | if (Constant *RHSC = dyn_cast<Constant>(ICI.getOperand(1))) { |
Chris Lattner | 6f6f512 | 2007-05-06 07:24:03 +0000 | [diff] [blame] | 5897 | RHSOp = ConstantExpr::getIntToPtr(RHSC, SrcTy); |
Chris Lattner | 8c756c1 | 2007-05-05 22:41:33 +0000 | [diff] [blame] | 5898 | } else if (PtrToIntInst *RHSC = dyn_cast<PtrToIntInst>(ICI.getOperand(1))) { |
| 5899 | RHSOp = RHSC->getOperand(0); |
| 5900 | // If the pointer types don't match, insert a bitcast. |
| 5901 | if (LHSCIOp->getType() != RHSOp->getType()) |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 5902 | RHSOp = InsertBitCastBefore(RHSOp, LHSCIOp->getType(), ICI); |
Chris Lattner | 8c756c1 | 2007-05-05 22:41:33 +0000 | [diff] [blame] | 5903 | } |
| 5904 | |
| 5905 | if (RHSOp) |
| 5906 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSOp); |
| 5907 | } |
| 5908 | |
| 5909 | // The code below only handles extension cast instructions, so far. |
| 5910 | // Enforce this. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5911 | if (LHSCI->getOpcode() != Instruction::ZExt && |
| 5912 | LHSCI->getOpcode() != Instruction::SExt) |
Chris Lattner | b352fa5 | 2005-01-17 03:20:02 +0000 | [diff] [blame] | 5913 | return 0; |
| 5914 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5915 | bool isSignedExt = LHSCI->getOpcode() == Instruction::SExt; |
| 5916 | bool isSignedCmp = ICI.isSignedPredicate(); |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5917 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5918 | if (CastInst *CI = dyn_cast<CastInst>(ICI.getOperand(1))) { |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5919 | // Not an extension from the same type? |
| 5920 | RHSCIOp = CI->getOperand(0); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5921 | if (RHSCIOp->getType() != LHSCIOp->getType()) |
| 5922 | return 0; |
Chris Lattner | a5c5e77 | 2007-01-13 23:11:38 +0000 | [diff] [blame] | 5923 | |
Nick Lewycky | 4189a53 | 2008-01-28 03:48:02 +0000 | [diff] [blame] | 5924 | // 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] | 5925 | // and the other is a zext), then we can't handle this. |
| 5926 | if (CI->getOpcode() != LHSCI->getOpcode()) |
| 5927 | return 0; |
| 5928 | |
Nick Lewycky | 4189a53 | 2008-01-28 03:48:02 +0000 | [diff] [blame] | 5929 | // Deal with equality cases early. |
| 5930 | if (ICI.isEquality()) |
| 5931 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSCIOp); |
| 5932 | |
| 5933 | // A signed comparison of sign extended values simplifies into a |
| 5934 | // signed comparison. |
| 5935 | if (isSignedCmp && isSignedExt) |
| 5936 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSCIOp); |
| 5937 | |
| 5938 | // The other three cases all fold into an unsigned comparison. |
| 5939 | return new ICmpInst(ICI.getUnsignedPredicate(), LHSCIOp, RHSCIOp); |
Reid Spencer | 6731d5c | 2004-11-28 21:31:15 +0000 | [diff] [blame] | 5940 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5941 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5942 | // If we aren't dealing with a constant on the RHS, exit early |
| 5943 | ConstantInt *CI = dyn_cast<ConstantInt>(ICI.getOperand(1)); |
| 5944 | if (!CI) |
| 5945 | return 0; |
| 5946 | |
| 5947 | // Compute the constant that would happen if we truncated to SrcTy then |
| 5948 | // reextended to DestTy. |
| 5949 | Constant *Res1 = ConstantExpr::getTrunc(CI, SrcTy); |
| 5950 | Constant *Res2 = ConstantExpr::getCast(LHSCI->getOpcode(), Res1, DestTy); |
| 5951 | |
| 5952 | // If the re-extended constant didn't change... |
| 5953 | if (Res2 == CI) { |
| 5954 | // Make sure that sign of the Cmp and the sign of the Cast are the same. |
| 5955 | // For example, we might have: |
| 5956 | // %A = sext short %X to uint |
| 5957 | // %B = icmp ugt uint %A, 1330 |
| 5958 | // It is incorrect to transform this into |
| 5959 | // %B = icmp ugt short %X, 1330 |
| 5960 | // because %A may have negative value. |
| 5961 | // |
| 5962 | // However, it is OK if SrcTy is bool (See cast-set.ll testcase) |
| 5963 | // OR operation is EQ/NE. |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 5964 | if (isSignedExt == isSignedCmp || SrcTy == Type::Int1Ty || ICI.isEquality()) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5965 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, Res1); |
| 5966 | else |
| 5967 | return 0; |
| 5968 | } |
| 5969 | |
| 5970 | // The re-extended constant changed so the constant cannot be represented |
| 5971 | // in the shorter type. Consequently, we cannot emit a simple comparison. |
| 5972 | |
| 5973 | // First, handle some easy cases. We know the result cannot be equal at this |
| 5974 | // point so handle the ICI.isEquality() cases |
| 5975 | if (ICI.getPredicate() == ICmpInst::ICMP_EQ) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5976 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5977 | if (ICI.getPredicate() == ICmpInst::ICMP_NE) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5978 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5979 | |
| 5980 | // Evaluate the comparison for LT (we invert for GT below). LE and GE cases |
| 5981 | // should have been folded away previously and not enter in here. |
| 5982 | Value *Result; |
| 5983 | if (isSignedCmp) { |
| 5984 | // We're performing a signed comparison. |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5985 | if (cast<ConstantInt>(CI)->getValue().isNegative()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5986 | Result = ConstantInt::getFalse(); // X < (small) --> false |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5987 | else |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5988 | Result = ConstantInt::getTrue(); // X < (large) --> true |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5989 | } else { |
| 5990 | // We're performing an unsigned comparison. |
| 5991 | if (isSignedExt) { |
| 5992 | // We're performing an unsigned comp with a sign extended value. |
| 5993 | // This is true if the input is >= 0. [aka >s -1] |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5994 | Constant *NegOne = ConstantInt::getAllOnesValue(SrcTy); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5995 | Result = InsertNewInstBefore(new ICmpInst(ICmpInst::ICMP_SGT, LHSCIOp, |
| 5996 | NegOne, ICI.getName()), ICI); |
| 5997 | } else { |
| 5998 | // Unsigned extend & unsigned compare -> always true. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5999 | Result = ConstantInt::getTrue(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6000 | } |
| 6001 | } |
| 6002 | |
| 6003 | // Finally, return the value computed. |
| 6004 | if (ICI.getPredicate() == ICmpInst::ICMP_ULT || |
| 6005 | ICI.getPredicate() == ICmpInst::ICMP_SLT) { |
| 6006 | return ReplaceInstUsesWith(ICI, Result); |
| 6007 | } else { |
| 6008 | assert((ICI.getPredicate()==ICmpInst::ICMP_UGT || |
| 6009 | ICI.getPredicate()==ICmpInst::ICMP_SGT) && |
| 6010 | "ICmp should be folded!"); |
| 6011 | if (Constant *CI = dyn_cast<Constant>(Result)) |
| 6012 | return ReplaceInstUsesWith(ICI, ConstantExpr::getNot(CI)); |
| 6013 | else |
| 6014 | return BinaryOperator::createNot(Result); |
| 6015 | } |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 6016 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 6017 | |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6018 | Instruction *InstCombiner::visitShl(BinaryOperator &I) { |
| 6019 | return commonShiftTransforms(I); |
| 6020 | } |
| 6021 | |
| 6022 | Instruction *InstCombiner::visitLShr(BinaryOperator &I) { |
| 6023 | return commonShiftTransforms(I); |
| 6024 | } |
| 6025 | |
| 6026 | Instruction *InstCombiner::visitAShr(BinaryOperator &I) { |
Chris Lattner | 348f665 | 2007-12-06 01:59:46 +0000 | [diff] [blame] | 6027 | if (Instruction *R = commonShiftTransforms(I)) |
| 6028 | return R; |
| 6029 | |
| 6030 | Value *Op0 = I.getOperand(0); |
| 6031 | |
| 6032 | // ashr int -1, X = -1 (for any arithmetic shift rights of ~0) |
| 6033 | if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0)) |
| 6034 | if (CSI->isAllOnesValue()) |
| 6035 | return ReplaceInstUsesWith(I, CSI); |
| 6036 | |
| 6037 | // See if we can turn a signed shr into an unsigned shr. |
| 6038 | if (MaskedValueIsZero(Op0, |
| 6039 | APInt::getSignBit(I.getType()->getPrimitiveSizeInBits()))) |
| 6040 | return BinaryOperator::createLShr(Op0, I.getOperand(1)); |
| 6041 | |
| 6042 | return 0; |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6043 | } |
| 6044 | |
| 6045 | Instruction *InstCombiner::commonShiftTransforms(BinaryOperator &I) { |
| 6046 | assert(I.getOperand(1)->getType() == I.getOperand(0)->getType()); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 6047 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 6048 | |
| 6049 | // shl X, 0 == X and shr X, 0 == X |
| 6050 | // shl 0, X == 0 and shr 0, X == 0 |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6051 | if (Op1 == Constant::getNullValue(Op1->getType()) || |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 6052 | Op0 == Constant::getNullValue(Op0->getType())) |
| 6053 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 8d6bbdb | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 6054 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6055 | if (isa<UndefValue>(Op0)) { |
| 6056 | if (I.getOpcode() == Instruction::AShr) // undef >>s X -> undef |
Chris Lattner | 79a564c | 2004-10-16 23:28:04 +0000 | [diff] [blame] | 6057 | return ReplaceInstUsesWith(I, Op0); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6058 | else // undef << X -> 0, undef >>u X -> 0 |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 6059 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 6060 | } |
| 6061 | if (isa<UndefValue>(Op1)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6062 | if (I.getOpcode() == Instruction::AShr) // X >>s undef -> X |
| 6063 | return ReplaceInstUsesWith(I, Op0); |
| 6064 | else // X << undef, X >>u undef -> 0 |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 6065 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 6066 | } |
| 6067 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 6068 | // Try to fold constant and into select arguments. |
| 6069 | if (isa<Constant>(Op0)) |
| 6070 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 6071 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 6072 | return R; |
| 6073 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6074 | if (ConstantInt *CUI = dyn_cast<ConstantInt>(Op1)) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6075 | if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I)) |
| 6076 | return Res; |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6077 | return 0; |
| 6078 | } |
| 6079 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6080 | Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6081 | BinaryOperator &I) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6082 | bool isLeftShift = I.getOpcode() == Instruction::Shl; |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6083 | |
Chris Lattner | 8d6bbdb | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 6084 | // See if we can simplify any instructions used by the instruction whose sole |
| 6085 | // purpose is to compute bits we don't care about. |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6086 | uint32_t TypeBits = Op0->getType()->getPrimitiveSizeInBits(); |
| 6087 | APInt KnownZero(TypeBits, 0), KnownOne(TypeBits, 0); |
| 6088 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(TypeBits), |
Chris Lattner | 8d6bbdb | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 6089 | KnownZero, KnownOne)) |
| 6090 | return &I; |
| 6091 | |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6092 | // shl uint X, 32 = 0 and shr ubyte Y, 9 = 0, ... just don't eliminate shr |
| 6093 | // of a signed value. |
| 6094 | // |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 6095 | if (Op1->uge(TypeBits)) { |
Chris Lattner | 0737c24 | 2007-02-02 05:29:55 +0000 | [diff] [blame] | 6096 | if (I.getOpcode() != Instruction::AShr) |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6097 | return ReplaceInstUsesWith(I, Constant::getNullValue(Op0->getType())); |
| 6098 | else { |
Chris Lattner | 0737c24 | 2007-02-02 05:29:55 +0000 | [diff] [blame] | 6099 | I.setOperand(1, ConstantInt::get(I.getType(), TypeBits-1)); |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6100 | return &I; |
Chris Lattner | 8adac75 | 2004-02-23 20:30:06 +0000 | [diff] [blame] | 6101 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6102 | } |
| 6103 | |
| 6104 | // ((X*C1) << C2) == (X * (C1 << C2)) |
| 6105 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0)) |
| 6106 | if (BO->getOpcode() == Instruction::Mul && isLeftShift) |
| 6107 | if (Constant *BOOp = dyn_cast<Constant>(BO->getOperand(1))) |
| 6108 | return BinaryOperator::createMul(BO->getOperand(0), |
| 6109 | ConstantExpr::getShl(BOOp, Op1)); |
| 6110 | |
| 6111 | // Try to fold constant and into select arguments. |
| 6112 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 6113 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 6114 | return R; |
| 6115 | if (isa<PHINode>(Op0)) |
| 6116 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 6117 | return NV; |
| 6118 | |
Chris Lattner | 8999dd3 | 2007-12-22 09:07:47 +0000 | [diff] [blame] | 6119 | // Fold shift2(trunc(shift1(x,c1)), c2) -> trunc(shift2(shift1(x,c1),c2)) |
| 6120 | if (TruncInst *TI = dyn_cast<TruncInst>(Op0)) { |
| 6121 | Instruction *TrOp = dyn_cast<Instruction>(TI->getOperand(0)); |
| 6122 | // If 'shift2' is an ashr, we would have to get the sign bit into a funny |
| 6123 | // place. Don't try to do this transformation in this case. Also, we |
| 6124 | // require that the input operand is a shift-by-constant so that we have |
| 6125 | // confidence that the shifts will get folded together. We could do this |
| 6126 | // xform in more cases, but it is unlikely to be profitable. |
| 6127 | if (TrOp && I.isLogicalShift() && TrOp->isShift() && |
| 6128 | isa<ConstantInt>(TrOp->getOperand(1))) { |
| 6129 | // Okay, we'll do this xform. Make the shift of shift. |
| 6130 | Constant *ShAmt = ConstantExpr::getZExt(Op1, TrOp->getType()); |
| 6131 | Instruction *NSh = BinaryOperator::create(I.getOpcode(), TrOp, ShAmt, |
| 6132 | I.getName()); |
| 6133 | InsertNewInstBefore(NSh, I); // (shift2 (shift1 & 0x00FF), c2) |
| 6134 | |
| 6135 | // For logical shifts, the truncation has the effect of making the high |
| 6136 | // part of the register be zeros. Emulate this by inserting an AND to |
| 6137 | // clear the top bits as needed. This 'and' will usually be zapped by |
| 6138 | // other xforms later if dead. |
| 6139 | unsigned SrcSize = TrOp->getType()->getPrimitiveSizeInBits(); |
| 6140 | unsigned DstSize = TI->getType()->getPrimitiveSizeInBits(); |
| 6141 | APInt MaskV(APInt::getLowBitsSet(SrcSize, DstSize)); |
| 6142 | |
| 6143 | // The mask we constructed says what the trunc would do if occurring |
| 6144 | // between the shifts. We want to know the effect *after* the second |
| 6145 | // shift. We know that it is a logical shift by a constant, so adjust the |
| 6146 | // mask as appropriate. |
| 6147 | if (I.getOpcode() == Instruction::Shl) |
| 6148 | MaskV <<= Op1->getZExtValue(); |
| 6149 | else { |
| 6150 | assert(I.getOpcode() == Instruction::LShr && "Unknown logical shift"); |
| 6151 | MaskV = MaskV.lshr(Op1->getZExtValue()); |
| 6152 | } |
| 6153 | |
| 6154 | Instruction *And = BinaryOperator::createAnd(NSh, ConstantInt::get(MaskV), |
| 6155 | TI->getName()); |
| 6156 | InsertNewInstBefore(And, I); // shift1 & 0x00FF |
| 6157 | |
| 6158 | // Return the value truncated to the interesting size. |
| 6159 | return new TruncInst(And, I.getType()); |
| 6160 | } |
| 6161 | } |
| 6162 | |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6163 | if (Op0->hasOneUse()) { |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6164 | if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0)) { |
| 6165 | // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C) |
| 6166 | Value *V1, *V2; |
| 6167 | ConstantInt *CC; |
| 6168 | switch (Op0BO->getOpcode()) { |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 6169 | default: break; |
| 6170 | case Instruction::Add: |
| 6171 | case Instruction::And: |
| 6172 | case Instruction::Or: |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6173 | case Instruction::Xor: { |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 6174 | // These operators commute. |
| 6175 | // Turn (Y + (X >> C)) << C -> (X + (Y << C)) & (~0 << C) |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6176 | if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() && |
| 6177 | match(Op0BO->getOperand(1), |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6178 | m_Shr(m_Value(V1), m_ConstantInt(CC))) && CC == Op1) { |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 6179 | Instruction *YS = BinaryOperator::createShl( |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6180 | Op0BO->getOperand(0), Op1, |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6181 | Op0BO->getName()); |
| 6182 | InsertNewInstBefore(YS, I); // (Y << C) |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 6183 | Instruction *X = |
| 6184 | BinaryOperator::create(Op0BO->getOpcode(), YS, V1, |
| 6185 | Op0BO->getOperand(1)->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6186 | InsertNewInstBefore(X, I); // (X + (Y << C)) |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 6187 | uint32_t Op1Val = Op1->getLimitedValue(TypeBits); |
Zhou Sheng | 90b9681 | 2007-03-30 05:45:18 +0000 | [diff] [blame] | 6188 | return BinaryOperator::createAnd(X, ConstantInt::get( |
| 6189 | APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val))); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6190 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6191 | |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6192 | // Turn (Y + ((X >> C) & CC)) << C -> ((X & (CC << C)) + (Y << C)) |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6193 | Value *Op0BOOp1 = Op0BO->getOperand(1); |
Chris Lattner | 3c69849 | 2007-03-05 00:11:19 +0000 | [diff] [blame] | 6194 | if (isLeftShift && Op0BOOp1->hasOneUse() && |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6195 | match(Op0BOOp1, |
| 6196 | 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] | 6197 | cast<BinaryOperator>(Op0BOOp1)->getOperand(0)->hasOneUse() && |
| 6198 | V2 == Op1) { |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 6199 | Instruction *YS = BinaryOperator::createShl( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6200 | Op0BO->getOperand(0), Op1, |
| 6201 | Op0BO->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6202 | InsertNewInstBefore(YS, I); // (Y << C) |
| 6203 | Instruction *XM = |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6204 | BinaryOperator::createAnd(V1, ConstantExpr::getShl(CC, Op1), |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6205 | V1->getName()+".mask"); |
| 6206 | InsertNewInstBefore(XM, I); // X & (CC << C) |
| 6207 | |
| 6208 | return BinaryOperator::create(Op0BO->getOpcode(), YS, XM); |
| 6209 | } |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6210 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6211 | |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6212 | // FALL THROUGH. |
| 6213 | case Instruction::Sub: { |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 6214 | // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C) |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6215 | if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() && |
| 6216 | match(Op0BO->getOperand(0), |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6217 | m_Shr(m_Value(V1), m_ConstantInt(CC))) && CC == Op1) { |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 6218 | Instruction *YS = BinaryOperator::createShl( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6219 | Op0BO->getOperand(1), Op1, |
| 6220 | Op0BO->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6221 | InsertNewInstBefore(YS, I); // (Y << C) |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 6222 | Instruction *X = |
Chris Lattner | 13d4ab4 | 2006-05-31 21:14:00 +0000 | [diff] [blame] | 6223 | BinaryOperator::create(Op0BO->getOpcode(), V1, YS, |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 6224 | Op0BO->getOperand(0)->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6225 | InsertNewInstBefore(X, I); // (X + (Y << C)) |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 6226 | uint32_t Op1Val = Op1->getLimitedValue(TypeBits); |
Zhou Sheng | 90b9681 | 2007-03-30 05:45:18 +0000 | [diff] [blame] | 6227 | return BinaryOperator::createAnd(X, ConstantInt::get( |
| 6228 | APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val))); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6229 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6230 | |
Chris Lattner | 13d4ab4 | 2006-05-31 21:14:00 +0000 | [diff] [blame] | 6231 | // Turn (((X >> C)&CC) + Y) << C -> (X + (Y << C)) & (CC << C) |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6232 | if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() && |
| 6233 | match(Op0BO->getOperand(0), |
| 6234 | m_And(m_Shr(m_Value(V1), m_Value(V2)), |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6235 | m_ConstantInt(CC))) && V2 == Op1 && |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 6236 | cast<BinaryOperator>(Op0BO->getOperand(0)) |
| 6237 | ->getOperand(0)->hasOneUse()) { |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 6238 | Instruction *YS = BinaryOperator::createShl( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6239 | Op0BO->getOperand(1), Op1, |
| 6240 | Op0BO->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6241 | InsertNewInstBefore(YS, I); // (Y << C) |
| 6242 | Instruction *XM = |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6243 | BinaryOperator::createAnd(V1, ConstantExpr::getShl(CC, Op1), |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6244 | V1->getName()+".mask"); |
| 6245 | InsertNewInstBefore(XM, I); // X & (CC << C) |
| 6246 | |
Chris Lattner | 13d4ab4 | 2006-05-31 21:14:00 +0000 | [diff] [blame] | 6247 | return BinaryOperator::create(Op0BO->getOpcode(), XM, YS); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6248 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6249 | |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 6250 | break; |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6251 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6252 | } |
| 6253 | |
| 6254 | |
| 6255 | // If the operand is an bitwise operator with a constant RHS, and the |
| 6256 | // shift is the only use, we can pull it out of the shift. |
| 6257 | if (ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand(1))) { |
| 6258 | bool isValid = true; // Valid only for And, Or, Xor |
| 6259 | bool highBitSet = false; // Transform if high bit of constant set? |
| 6260 | |
| 6261 | switch (Op0BO->getOpcode()) { |
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 6262 | default: isValid = false; break; // Do not perform transform! |
Chris Lattner | 1f7e160 | 2004-10-08 03:46:20 +0000 | [diff] [blame] | 6263 | case Instruction::Add: |
| 6264 | isValid = isLeftShift; |
| 6265 | break; |
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 6266 | case Instruction::Or: |
| 6267 | case Instruction::Xor: |
| 6268 | highBitSet = false; |
| 6269 | break; |
| 6270 | case Instruction::And: |
| 6271 | highBitSet = true; |
| 6272 | break; |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6273 | } |
| 6274 | |
| 6275 | // If this is a signed shift right, and the high bit is modified |
| 6276 | // by the logical operation, do not perform the transformation. |
| 6277 | // The highBitSet boolean indicates the value of the high bit of |
| 6278 | // the constant which would cause it to be modified for this |
| 6279 | // operation. |
| 6280 | // |
Chris Lattner | c95ba44 | 2007-12-06 06:25:04 +0000 | [diff] [blame] | 6281 | if (isValid && I.getOpcode() == Instruction::AShr) |
Zhou Sheng | e9e03f6 | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 6282 | isValid = Op0C->getValue()[TypeBits-1] == highBitSet; |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6283 | |
| 6284 | if (isValid) { |
| 6285 | Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, Op1); |
| 6286 | |
| 6287 | Instruction *NewShift = |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6288 | BinaryOperator::create(I.getOpcode(), Op0BO->getOperand(0), Op1); |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6289 | InsertNewInstBefore(NewShift, I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6290 | NewShift->takeName(Op0BO); |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6291 | |
| 6292 | return BinaryOperator::create(Op0BO->getOpcode(), NewShift, |
| 6293 | NewRHS); |
| 6294 | } |
| 6295 | } |
| 6296 | } |
| 6297 | } |
| 6298 | |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6299 | // 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] | 6300 | BinaryOperator *ShiftOp = dyn_cast<BinaryOperator>(Op0); |
| 6301 | if (ShiftOp && !ShiftOp->isShift()) |
| 6302 | ShiftOp = 0; |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6303 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6304 | if (ShiftOp && isa<ConstantInt>(ShiftOp->getOperand(1))) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6305 | ConstantInt *ShiftAmt1C = cast<ConstantInt>(ShiftOp->getOperand(1)); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 6306 | uint32_t ShiftAmt1 = ShiftAmt1C->getLimitedValue(TypeBits); |
| 6307 | uint32_t ShiftAmt2 = Op1->getLimitedValue(TypeBits); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6308 | assert(ShiftAmt2 != 0 && "Should have been simplified earlier"); |
| 6309 | if (ShiftAmt1 == 0) return 0; // Will be simplified in the future. |
| 6310 | Value *X = ShiftOp->getOperand(0); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6311 | |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 6312 | uint32_t AmtSum = ShiftAmt1+ShiftAmt2; // Fold into one big shift. |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6313 | if (AmtSum > TypeBits) |
| 6314 | AmtSum = TypeBits; |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6315 | |
| 6316 | const IntegerType *Ty = cast<IntegerType>(I.getType()); |
| 6317 | |
| 6318 | // Check for (X << c1) << c2 and (X >> c1) >> c2 |
Chris Lattner | 7f3da2d | 2007-02-03 23:28:07 +0000 | [diff] [blame] | 6319 | if (I.getOpcode() == ShiftOp->getOpcode()) { |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6320 | return BinaryOperator::create(I.getOpcode(), X, |
| 6321 | ConstantInt::get(Ty, AmtSum)); |
| 6322 | } else if (ShiftOp->getOpcode() == Instruction::LShr && |
| 6323 | I.getOpcode() == Instruction::AShr) { |
| 6324 | // ((X >>u C1) >>s C2) -> (X >>u (C1+C2)) since C1 != 0. |
| 6325 | return BinaryOperator::createLShr(X, ConstantInt::get(Ty, AmtSum)); |
| 6326 | } else if (ShiftOp->getOpcode() == Instruction::AShr && |
| 6327 | I.getOpcode() == Instruction::LShr) { |
| 6328 | // ((X >>s C1) >>u C2) -> ((X >>s (C1+C2)) & mask) since C1 != 0. |
| 6329 | Instruction *Shift = |
| 6330 | BinaryOperator::createAShr(X, ConstantInt::get(Ty, AmtSum)); |
| 6331 | InsertNewInstBefore(Shift, I); |
| 6332 | |
Zhou Sheng | e9e03f6 | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 6333 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6334 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6335 | } |
| 6336 | |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6337 | // Okay, if we get here, one shift must be left, and the other shift must be |
| 6338 | // right. See if the amounts are equal. |
| 6339 | if (ShiftAmt1 == ShiftAmt2) { |
| 6340 | // If we have ((X >>? C) << C), turn this into X & (-1 << C). |
| 6341 | if (I.getOpcode() == Instruction::Shl) { |
Reid Spencer | 55702aa | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 6342 | APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt1)); |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6343 | return BinaryOperator::createAnd(X, ConstantInt::get(Mask)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6344 | } |
| 6345 | // If we have ((X << C) >>u C), turn this into X & (-1 >>u C). |
| 6346 | if (I.getOpcode() == Instruction::LShr) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 6347 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt1)); |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6348 | return BinaryOperator::createAnd(X, ConstantInt::get(Mask)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6349 | } |
| 6350 | // We can simplify ((X << C) >>s C) into a trunc + sext. |
| 6351 | // NOTE: we could do this for any C, but that would make 'unusual' integer |
| 6352 | // types. For now, just stick to ones well-supported by the code |
| 6353 | // generators. |
| 6354 | const Type *SExtType = 0; |
| 6355 | switch (Ty->getBitWidth() - ShiftAmt1) { |
Zhou Sheng | e9e03f6 | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 6356 | case 1 : |
| 6357 | case 8 : |
| 6358 | case 16 : |
| 6359 | case 32 : |
| 6360 | case 64 : |
| 6361 | case 128: |
| 6362 | SExtType = IntegerType::get(Ty->getBitWidth() - ShiftAmt1); |
| 6363 | break; |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6364 | default: break; |
| 6365 | } |
| 6366 | if (SExtType) { |
| 6367 | Instruction *NewTrunc = new TruncInst(X, SExtType, "sext"); |
| 6368 | InsertNewInstBefore(NewTrunc, I); |
| 6369 | return new SExtInst(NewTrunc, Ty); |
| 6370 | } |
| 6371 | // Otherwise, we can't handle it yet. |
| 6372 | } else if (ShiftAmt1 < ShiftAmt2) { |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 6373 | uint32_t ShiftDiff = ShiftAmt2-ShiftAmt1; |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6374 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 6375 | // (X >>? C1) << C2 --> X << (C2-C1) & (-1 << C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6376 | if (I.getOpcode() == Instruction::Shl) { |
| 6377 | assert(ShiftOp->getOpcode() == Instruction::LShr || |
| 6378 | ShiftOp->getOpcode() == Instruction::AShr); |
Chris Lattner | e8d56c5 | 2006-01-07 01:32:28 +0000 | [diff] [blame] | 6379 | Instruction *Shift = |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6380 | BinaryOperator::createShl(X, ConstantInt::get(Ty, ShiftDiff)); |
Chris Lattner | e8d56c5 | 2006-01-07 01:32:28 +0000 | [diff] [blame] | 6381 | InsertNewInstBefore(Shift, I); |
| 6382 | |
Reid Spencer | 55702aa | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 6383 | APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
| 6384 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6385 | } |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6386 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 6387 | // (X << C1) >>u C2 --> X >>u (C2-C1) & (-1 >> C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6388 | if (I.getOpcode() == Instruction::LShr) { |
| 6389 | assert(ShiftOp->getOpcode() == Instruction::Shl); |
| 6390 | Instruction *Shift = |
| 6391 | BinaryOperator::createLShr(X, ConstantInt::get(Ty, ShiftDiff)); |
| 6392 | InsertNewInstBefore(Shift, I); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6393 | |
Reid Spencer | d5e30f0 | 2007-03-26 17:18:58 +0000 | [diff] [blame] | 6394 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6395 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 6396 | } |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6397 | |
| 6398 | // We can't handle (X << C1) >>s C2, it shifts arbitrary bits in. |
| 6399 | } else { |
| 6400 | assert(ShiftAmt2 < ShiftAmt1); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 6401 | uint32_t ShiftDiff = ShiftAmt1-ShiftAmt2; |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6402 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 6403 | // (X >>? C1) << C2 --> X >>? (C1-C2) & (-1 << C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6404 | if (I.getOpcode() == Instruction::Shl) { |
| 6405 | assert(ShiftOp->getOpcode() == Instruction::LShr || |
| 6406 | ShiftOp->getOpcode() == Instruction::AShr); |
| 6407 | Instruction *Shift = |
| 6408 | BinaryOperator::create(ShiftOp->getOpcode(), X, |
| 6409 | ConstantInt::get(Ty, ShiftDiff)); |
| 6410 | InsertNewInstBefore(Shift, I); |
| 6411 | |
Reid Spencer | 55702aa | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 6412 | APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6413 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6414 | } |
| 6415 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 6416 | // (X << C1) >>u C2 --> X << (C1-C2) & (-1 >> C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6417 | if (I.getOpcode() == Instruction::LShr) { |
| 6418 | assert(ShiftOp->getOpcode() == Instruction::Shl); |
| 6419 | Instruction *Shift = |
| 6420 | BinaryOperator::createShl(X, ConstantInt::get(Ty, ShiftDiff)); |
| 6421 | InsertNewInstBefore(Shift, I); |
| 6422 | |
Reid Spencer | 68d27cf | 2007-03-26 23:45:51 +0000 | [diff] [blame] | 6423 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6424 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6425 | } |
| 6426 | |
| 6427 | // 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] | 6428 | } |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6429 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 6430 | return 0; |
| 6431 | } |
| 6432 | |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 6433 | |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 6434 | /// DecomposeSimpleLinearExpr - Analyze 'Val', seeing if it is a simple linear |
| 6435 | /// expression. If so, decompose it, returning some value X, such that Val is |
| 6436 | /// X*Scale+Offset. |
| 6437 | /// |
| 6438 | static Value *DecomposeSimpleLinearExpr(Value *Val, unsigned &Scale, |
Jeff Cohen | 86796be | 2007-04-04 16:58:57 +0000 | [diff] [blame] | 6439 | int &Offset) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6440 | assert(Val->getType() == Type::Int32Ty && "Unexpected allocation size type!"); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6441 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6442 | Offset = CI->getZExtValue(); |
Chris Lattner | 6a94de2 | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 6443 | Scale = 0; |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6444 | return ConstantInt::get(Type::Int32Ty, 0); |
Chris Lattner | 6a94de2 | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 6445 | } else if (BinaryOperator *I = dyn_cast<BinaryOperator>(Val)) { |
| 6446 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 6447 | if (I->getOpcode() == Instruction::Shl) { |
| 6448 | // This is a value scaled by '1 << the shift amt'. |
| 6449 | Scale = 1U << RHS->getZExtValue(); |
| 6450 | Offset = 0; |
| 6451 | return I->getOperand(0); |
| 6452 | } else if (I->getOpcode() == Instruction::Mul) { |
| 6453 | // This value is scaled by 'RHS'. |
| 6454 | Scale = RHS->getZExtValue(); |
| 6455 | Offset = 0; |
| 6456 | return I->getOperand(0); |
| 6457 | } else if (I->getOpcode() == Instruction::Add) { |
| 6458 | // We have X+C. Check to see if we really have (X*C2)+C1, |
| 6459 | // where C1 is divisible by C2. |
| 6460 | unsigned SubScale; |
| 6461 | Value *SubVal = |
| 6462 | DecomposeSimpleLinearExpr(I->getOperand(0), SubScale, Offset); |
| 6463 | Offset += RHS->getZExtValue(); |
| 6464 | Scale = SubScale; |
| 6465 | return SubVal; |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 6466 | } |
| 6467 | } |
| 6468 | } |
| 6469 | |
| 6470 | // Otherwise, we can't look past this. |
| 6471 | Scale = 1; |
| 6472 | Offset = 0; |
| 6473 | return Val; |
| 6474 | } |
| 6475 | |
| 6476 | |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6477 | /// PromoteCastOfAllocation - If we find a cast of an allocation instruction, |
| 6478 | /// 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] | 6479 | Instruction *InstCombiner::PromoteCastOfAllocation(BitCastInst &CI, |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6480 | AllocationInst &AI) { |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 6481 | const PointerType *PTy = cast<PointerType>(CI.getType()); |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6482 | |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 6483 | // Remove any uses of AI that are dead. |
| 6484 | assert(!CI.use_empty() && "Dead instructions should be removed earlier!"); |
Chris Lattner | 535014f | 2007-02-15 22:52:10 +0000 | [diff] [blame] | 6485 | |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 6486 | for (Value::use_iterator UI = AI.use_begin(), E = AI.use_end(); UI != E; ) { |
| 6487 | Instruction *User = cast<Instruction>(*UI++); |
| 6488 | if (isInstructionTriviallyDead(User)) { |
| 6489 | while (UI != E && *UI == User) |
| 6490 | ++UI; // If this instruction uses AI more than once, don't break UI. |
| 6491 | |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 6492 | ++NumDeadInst; |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 6493 | DOUT << "IC: DCE: " << *User; |
Chris Lattner | f22a5c6 | 2007-03-02 19:59:19 +0000 | [diff] [blame] | 6494 | EraseInstFromFunction(*User); |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 6495 | } |
| 6496 | } |
| 6497 | |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6498 | // Get the type really allocated and the type casted to. |
| 6499 | const Type *AllocElTy = AI.getAllocatedType(); |
| 6500 | const Type *CastElTy = PTy->getElementType(); |
| 6501 | if (!AllocElTy->isSized() || !CastElTy->isSized()) return 0; |
Chris Lattner | 18e78bb | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 6502 | |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 6503 | unsigned AllocElTyAlign = TD->getABITypeAlignment(AllocElTy); |
| 6504 | unsigned CastElTyAlign = TD->getABITypeAlignment(CastElTy); |
Chris Lattner | 18e78bb | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 6505 | if (CastElTyAlign < AllocElTyAlign) return 0; |
| 6506 | |
Chris Lattner | 39387a5 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 6507 | // If the allocation has multiple uses, only promote it if we are strictly |
| 6508 | // increasing the alignment of the resultant allocation. If we keep it the |
| 6509 | // same, we open the door to infinite loops of various kinds. |
| 6510 | if (!AI.hasOneUse() && CastElTyAlign == AllocElTyAlign) return 0; |
| 6511 | |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 6512 | uint64_t AllocElTySize = TD->getABITypeSize(AllocElTy); |
| 6513 | uint64_t CastElTySize = TD->getABITypeSize(CastElTy); |
Chris Lattner | 0ddac2a | 2005-10-27 05:53:56 +0000 | [diff] [blame] | 6514 | if (CastElTySize == 0 || AllocElTySize == 0) return 0; |
Chris Lattner | 18e78bb | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 6515 | |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 6516 | // See if we can satisfy the modulus by pulling a scale out of the array |
| 6517 | // size argument. |
Jeff Cohen | 86796be | 2007-04-04 16:58:57 +0000 | [diff] [blame] | 6518 | unsigned ArraySizeScale; |
| 6519 | int ArrayOffset; |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 6520 | Value *NumElements = // See if the array size is a decomposable linear expr. |
| 6521 | DecomposeSimpleLinearExpr(AI.getOperand(0), ArraySizeScale, ArrayOffset); |
| 6522 | |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 6523 | // If we can now satisfy the modulus, by using a non-1 scale, we really can |
| 6524 | // do the xform. |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 6525 | if ((AllocElTySize*ArraySizeScale) % CastElTySize != 0 || |
| 6526 | (AllocElTySize*ArrayOffset ) % CastElTySize != 0) return 0; |
Chris Lattner | 8142b0a | 2005-10-27 06:12:00 +0000 | [diff] [blame] | 6527 | |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 6528 | unsigned Scale = (AllocElTySize*ArraySizeScale)/CastElTySize; |
| 6529 | Value *Amt = 0; |
| 6530 | if (Scale == 1) { |
| 6531 | Amt = NumElements; |
| 6532 | } else { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6533 | // If the allocation size is constant, form a constant mul expression |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6534 | Amt = ConstantInt::get(Type::Int32Ty, Scale); |
| 6535 | if (isa<ConstantInt>(NumElements)) |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 6536 | Amt = Multiply(cast<ConstantInt>(NumElements), cast<ConstantInt>(Amt)); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6537 | // otherwise multiply the amount and the number of elements |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 6538 | else if (Scale != 1) { |
| 6539 | Instruction *Tmp = BinaryOperator::createMul(Amt, NumElements, "tmp"); |
| 6540 | Amt = InsertNewInstBefore(Tmp, AI); |
Chris Lattner | 8142b0a | 2005-10-27 06:12:00 +0000 | [diff] [blame] | 6541 | } |
Chris Lattner | 0ddac2a | 2005-10-27 05:53:56 +0000 | [diff] [blame] | 6542 | } |
| 6543 | |
Jeff Cohen | 86796be | 2007-04-04 16:58:57 +0000 | [diff] [blame] | 6544 | if (int Offset = (AllocElTySize*ArrayOffset)/CastElTySize) { |
| 6545 | Value *Off = ConstantInt::get(Type::Int32Ty, Offset, true); |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 6546 | Instruction *Tmp = BinaryOperator::createAdd(Amt, Off, "tmp"); |
| 6547 | Amt = InsertNewInstBefore(Tmp, AI); |
| 6548 | } |
| 6549 | |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6550 | AllocationInst *New; |
| 6551 | if (isa<MallocInst>(AI)) |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6552 | New = new MallocInst(CastElTy, Amt, AI.getAlignment()); |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6553 | else |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6554 | New = new AllocaInst(CastElTy, Amt, AI.getAlignment()); |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6555 | InsertNewInstBefore(New, AI); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6556 | New->takeName(&AI); |
Chris Lattner | 39387a5 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 6557 | |
| 6558 | // If the allocation has multiple uses, insert a cast and change all things |
| 6559 | // that used it to use the new cast. This will also hack on CI, but it will |
| 6560 | // die soon. |
| 6561 | if (!AI.hasOneUse()) { |
| 6562 | AddUsesToWorkList(AI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6563 | // New is the allocation instruction, pointer typed. AI is the original |
| 6564 | // allocation instruction, also pointer typed. Thus, cast to use is BitCast. |
| 6565 | CastInst *NewCast = new BitCastInst(New, AI.getType(), "tmpcast"); |
Chris Lattner | 39387a5 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 6566 | InsertNewInstBefore(NewCast, AI); |
| 6567 | AI.replaceAllUsesWith(NewCast); |
| 6568 | } |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6569 | return ReplaceInstUsesWith(CI, New); |
| 6570 | } |
| 6571 | |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6572 | /// CanEvaluateInDifferentType - Return true if we can take the specified value |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6573 | /// and return it as type Ty without inserting any new casts and without |
| 6574 | /// changing the computed value. This is used by code that tries to decide |
| 6575 | /// whether promoting or shrinking integer operations to wider or smaller types |
| 6576 | /// will allow us to eliminate a truncate or extend. |
| 6577 | /// |
| 6578 | /// This is a truncation operation if Ty is smaller than V->getType(), or an |
| 6579 | /// extension operation if Ty is larger. |
| 6580 | static bool CanEvaluateInDifferentType(Value *V, const IntegerType *Ty, |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 6581 | unsigned CastOpc, int &NumCastsRemoved) { |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6582 | // We can always evaluate constants in another type. |
| 6583 | if (isa<ConstantInt>(V)) |
| 6584 | return true; |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6585 | |
| 6586 | Instruction *I = dyn_cast<Instruction>(V); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6587 | if (!I) return false; |
| 6588 | |
| 6589 | const IntegerType *OrigTy = cast<IntegerType>(V->getType()); |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6590 | |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 6591 | // If this is an extension or truncate, we can often eliminate it. |
| 6592 | if (isa<TruncInst>(I) || isa<ZExtInst>(I) || isa<SExtInst>(I)) { |
| 6593 | // If this is a cast from the destination type, we can trivially eliminate |
| 6594 | // it, and this will remove a cast overall. |
| 6595 | if (I->getOperand(0)->getType() == Ty) { |
| 6596 | // If the first operand is itself a cast, and is eliminable, do not count |
| 6597 | // this as an eliminable cast. We would prefer to eliminate those two |
| 6598 | // casts first. |
| 6599 | if (!isa<CastInst>(I->getOperand(0))) |
| 6600 | ++NumCastsRemoved; |
| 6601 | return true; |
| 6602 | } |
| 6603 | } |
| 6604 | |
| 6605 | // We can't extend or shrink something that has multiple uses: doing so would |
| 6606 | // require duplicating the instruction in general, which isn't profitable. |
| 6607 | if (!I->hasOneUse()) return false; |
| 6608 | |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6609 | switch (I->getOpcode()) { |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6610 | case Instruction::Add: |
| 6611 | case Instruction::Sub: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6612 | case Instruction::And: |
| 6613 | case Instruction::Or: |
| 6614 | case Instruction::Xor: |
| 6615 | // These operators can all arbitrarily be extended or truncated. |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 6616 | return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc, |
| 6617 | NumCastsRemoved) && |
| 6618 | CanEvaluateInDifferentType(I->getOperand(1), Ty, CastOpc, |
| 6619 | NumCastsRemoved); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6620 | |
Nick Lewycky | e6b0c00 | 2008-01-22 05:08:48 +0000 | [diff] [blame] | 6621 | case Instruction::Mul: |
Nick Lewycky | e6b0c00 | 2008-01-22 05:08:48 +0000 | [diff] [blame] | 6622 | // A multiply can be truncated by truncating its operands. |
| 6623 | return Ty->getBitWidth() < OrigTy->getBitWidth() && |
| 6624 | CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc, |
| 6625 | NumCastsRemoved) && |
| 6626 | CanEvaluateInDifferentType(I->getOperand(1), Ty, CastOpc, |
| 6627 | NumCastsRemoved); |
| 6628 | |
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 6629 | case Instruction::Shl: |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6630 | // If we are truncating the result of this SHL, and if it's a shift of a |
| 6631 | // constant amount, we can always perform a SHL in a smaller type. |
| 6632 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 6633 | uint32_t BitWidth = Ty->getBitWidth(); |
| 6634 | if (BitWidth < OrigTy->getBitWidth() && |
| 6635 | CI->getLimitedValue(BitWidth) < BitWidth) |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 6636 | return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc, |
| 6637 | NumCastsRemoved); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6638 | } |
| 6639 | break; |
| 6640 | case Instruction::LShr: |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6641 | // If this is a truncate of a logical shr, we can truncate it to a smaller |
| 6642 | // lshr iff we know that the bits we would otherwise be shifting in are |
| 6643 | // already zeros. |
| 6644 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 6645 | uint32_t OrigBitWidth = OrigTy->getBitWidth(); |
| 6646 | uint32_t BitWidth = Ty->getBitWidth(); |
| 6647 | if (BitWidth < OrigBitWidth && |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6648 | MaskedValueIsZero(I->getOperand(0), |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 6649 | APInt::getHighBitsSet(OrigBitWidth, OrigBitWidth-BitWidth)) && |
| 6650 | CI->getLimitedValue(BitWidth) < BitWidth) { |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 6651 | return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc, |
| 6652 | NumCastsRemoved); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6653 | } |
| 6654 | } |
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 6655 | break; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6656 | case Instruction::ZExt: |
| 6657 | case Instruction::SExt: |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 6658 | case Instruction::Trunc: |
| 6659 | // 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] | 6660 | // can safely replace it. Note that replacing it does not reduce the number |
| 6661 | // of casts in the input. |
| 6662 | if (I->getOpcode() == CastOpc) |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6663 | return true; |
Chris Lattner | 50d9d77 | 2007-09-10 23:46:29 +0000 | [diff] [blame] | 6664 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6665 | break; |
| 6666 | default: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6667 | // TODO: Can handle more cases here. |
| 6668 | break; |
| 6669 | } |
| 6670 | |
| 6671 | return false; |
| 6672 | } |
| 6673 | |
| 6674 | /// EvaluateInDifferentType - Given an expression that |
| 6675 | /// CanEvaluateInDifferentType returns true for, actually insert the code to |
| 6676 | /// evaluate the expression. |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 6677 | Value *InstCombiner::EvaluateInDifferentType(Value *V, const Type *Ty, |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6678 | bool isSigned) { |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6679 | if (Constant *C = dyn_cast<Constant>(V)) |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 6680 | return ConstantExpr::getIntegerCast(C, Ty, isSigned /*Sext or ZExt*/); |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6681 | |
| 6682 | // Otherwise, it must be an instruction. |
| 6683 | Instruction *I = cast<Instruction>(V); |
Chris Lattner | 01859e8 | 2006-05-20 23:14:03 +0000 | [diff] [blame] | 6684 | Instruction *Res = 0; |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6685 | switch (I->getOpcode()) { |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6686 | case Instruction::Add: |
| 6687 | case Instruction::Sub: |
Nick Lewycky | e6b0c00 | 2008-01-22 05:08:48 +0000 | [diff] [blame] | 6688 | case Instruction::Mul: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6689 | case Instruction::And: |
| 6690 | case Instruction::Or: |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6691 | case Instruction::Xor: |
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 6692 | case Instruction::AShr: |
| 6693 | case Instruction::LShr: |
| 6694 | case Instruction::Shl: { |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 6695 | Value *LHS = EvaluateInDifferentType(I->getOperand(0), Ty, isSigned); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6696 | Value *RHS = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned); |
| 6697 | Res = BinaryOperator::create((Instruction::BinaryOps)I->getOpcode(), |
| 6698 | LHS, RHS, I->getName()); |
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 6699 | break; |
| 6700 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6701 | case Instruction::Trunc: |
| 6702 | case Instruction::ZExt: |
| 6703 | case Instruction::SExt: |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6704 | // 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] | 6705 | // just return the source. There's no need to insert it because it is not |
| 6706 | // new. |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6707 | if (I->getOperand(0)->getType() == Ty) |
| 6708 | return I->getOperand(0); |
| 6709 | |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 6710 | // Otherwise, must be the same type of case, so just reinsert a new one. |
| 6711 | Res = CastInst::create(cast<CastInst>(I)->getOpcode(), I->getOperand(0), |
| 6712 | Ty, I->getName()); |
| 6713 | break; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6714 | default: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6715 | // TODO: Can handle more cases here. |
| 6716 | assert(0 && "Unreachable!"); |
| 6717 | break; |
| 6718 | } |
| 6719 | |
| 6720 | return InsertNewInstBefore(Res, *I); |
| 6721 | } |
| 6722 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6723 | /// @brief Implement the transforms common to all CastInst visitors. |
| 6724 | Instruction *InstCombiner::commonCastTransforms(CastInst &CI) { |
Chris Lattner | 79d35b3 | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 6725 | Value *Src = CI.getOperand(0); |
| 6726 | |
Dan Gohman | 23d9d27 | 2007-05-11 21:10:54 +0000 | [diff] [blame] | 6727 | // 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] | 6728 | // eliminate it now. |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 6729 | if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6730 | if (Instruction::CastOps opc = |
| 6731 | isEliminableCastPair(CSrc, CI.getOpcode(), CI.getType(), TD)) { |
| 6732 | // The first cast (CSrc) is eliminable so we need to fix up or replace |
| 6733 | // the second cast (CI). CSrc will then have a good chance of being dead. |
| 6734 | return CastInst::create(opc, CSrc->getOperand(0), CI.getType()); |
Chris Lattner | 8fd217c | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 6735 | } |
| 6736 | } |
Chris Lattner | a710ddc | 2004-05-25 04:29:21 +0000 | [diff] [blame] | 6737 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6738 | // 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] | 6739 | if (SelectInst *SI = dyn_cast<SelectInst>(Src)) |
| 6740 | if (Instruction *NV = FoldOpIntoSelect(CI, SI, this)) |
| 6741 | return NV; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6742 | |
| 6743 | // 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] | 6744 | if (isa<PHINode>(Src)) |
| 6745 | if (Instruction *NV = FoldOpIntoPhi(CI)) |
| 6746 | return NV; |
Chris Lattner | 9fb9213 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 6747 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6748 | return 0; |
| 6749 | } |
| 6750 | |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 6751 | /// @brief Implement the transforms for cast of pointer (bitcast/ptrtoint) |
| 6752 | Instruction *InstCombiner::commonPointerCastTransforms(CastInst &CI) { |
| 6753 | Value *Src = CI.getOperand(0); |
| 6754 | |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 6755 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Src)) { |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 6756 | // If casting the result of a getelementptr instruction with no offset, turn |
| 6757 | // this into a cast of the original pointer! |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 6758 | if (GEP->hasAllZeroIndices()) { |
| 6759 | // Changing the cast operand is usually not a good idea but it is safe |
| 6760 | // here because the pointer operand is being replaced with another |
| 6761 | // pointer operand so the opcode doesn't need to change. |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 6762 | AddToWorkList(GEP); |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 6763 | CI.setOperand(0, GEP->getOperand(0)); |
| 6764 | return &CI; |
| 6765 | } |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 6766 | |
| 6767 | // If the GEP has a single use, and the base pointer is a bitcast, and the |
| 6768 | // GEP computes a constant offset, see if we can convert these three |
| 6769 | // instructions into fewer. This typically happens with unions and other |
| 6770 | // non-type-safe code. |
| 6771 | if (GEP->hasOneUse() && isa<BitCastInst>(GEP->getOperand(0))) { |
| 6772 | if (GEP->hasAllConstantIndices()) { |
| 6773 | // We are guaranteed to get a constant from EmitGEPOffset. |
| 6774 | ConstantInt *OffsetV = cast<ConstantInt>(EmitGEPOffset(GEP, CI, *this)); |
| 6775 | int64_t Offset = OffsetV->getSExtValue(); |
| 6776 | |
| 6777 | // Get the base pointer input of the bitcast, and the type it points to. |
| 6778 | Value *OrigBase = cast<BitCastInst>(GEP->getOperand(0))->getOperand(0); |
| 6779 | const Type *GEPIdxTy = |
| 6780 | cast<PointerType>(OrigBase->getType())->getElementType(); |
| 6781 | if (GEPIdxTy->isSized()) { |
| 6782 | SmallVector<Value*, 8> NewIndices; |
| 6783 | |
Chris Lattner | c42e226 | 2007-05-05 01:59:31 +0000 | [diff] [blame] | 6784 | // Start with the index over the outer type. Note that the type size |
| 6785 | // might be zero (even if the offset isn't zero) if the indexed type |
| 6786 | // is something like [0 x {int, int}] |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 6787 | const Type *IntPtrTy = TD->getIntPtrType(); |
Chris Lattner | c42e226 | 2007-05-05 01:59:31 +0000 | [diff] [blame] | 6788 | int64_t FirstIdx = 0; |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 6789 | if (int64_t TySize = TD->getABITypeSize(GEPIdxTy)) { |
Chris Lattner | c42e226 | 2007-05-05 01:59:31 +0000 | [diff] [blame] | 6790 | FirstIdx = Offset/TySize; |
| 6791 | Offset %= TySize; |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 6792 | |
Chris Lattner | c42e226 | 2007-05-05 01:59:31 +0000 | [diff] [blame] | 6793 | // Handle silly modulus not returning values values [0..TySize). |
| 6794 | if (Offset < 0) { |
| 6795 | --FirstIdx; |
| 6796 | Offset += TySize; |
| 6797 | assert(Offset >= 0); |
| 6798 | } |
Chris Lattner | d717c18 | 2007-05-05 22:32:24 +0000 | [diff] [blame] | 6799 | assert((uint64_t)Offset < (uint64_t)TySize &&"Out of range offset"); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 6800 | } |
| 6801 | |
| 6802 | NewIndices.push_back(ConstantInt::get(IntPtrTy, FirstIdx)); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 6803 | |
| 6804 | // Index into the types. If we fail, set OrigBase to null. |
| 6805 | while (Offset) { |
| 6806 | if (const StructType *STy = dyn_cast<StructType>(GEPIdxTy)) { |
| 6807 | const StructLayout *SL = TD->getStructLayout(STy); |
Chris Lattner | 6b6aef8 | 2007-05-15 00:16:00 +0000 | [diff] [blame] | 6808 | if (Offset < (int64_t)SL->getSizeInBytes()) { |
| 6809 | unsigned Elt = SL->getElementContainingOffset(Offset); |
| 6810 | NewIndices.push_back(ConstantInt::get(Type::Int32Ty, Elt)); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 6811 | |
Chris Lattner | 6b6aef8 | 2007-05-15 00:16:00 +0000 | [diff] [blame] | 6812 | Offset -= SL->getElementOffset(Elt); |
| 6813 | GEPIdxTy = STy->getElementType(Elt); |
| 6814 | } else { |
| 6815 | // Otherwise, we can't index into this, bail out. |
| 6816 | Offset = 0; |
| 6817 | OrigBase = 0; |
| 6818 | } |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 6819 | } else if (isa<ArrayType>(GEPIdxTy) || isa<VectorType>(GEPIdxTy)) { |
| 6820 | const SequentialType *STy = cast<SequentialType>(GEPIdxTy); |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 6821 | if (uint64_t EltSize = TD->getABITypeSize(STy->getElementType())){ |
Chris Lattner | 6b6aef8 | 2007-05-15 00:16:00 +0000 | [diff] [blame] | 6822 | NewIndices.push_back(ConstantInt::get(IntPtrTy,Offset/EltSize)); |
| 6823 | Offset %= EltSize; |
| 6824 | } else { |
| 6825 | NewIndices.push_back(ConstantInt::get(IntPtrTy, 0)); |
| 6826 | } |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 6827 | GEPIdxTy = STy->getElementType(); |
| 6828 | } else { |
| 6829 | // Otherwise, we can't index into this, bail out. |
| 6830 | Offset = 0; |
| 6831 | OrigBase = 0; |
| 6832 | } |
| 6833 | } |
| 6834 | if (OrigBase) { |
| 6835 | // If we were able to index down into an element, create the GEP |
| 6836 | // and bitcast the result. This eliminates one bitcast, potentially |
| 6837 | // two. |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 6838 | Instruction *NGEP = new GetElementPtrInst(OrigBase, |
| 6839 | NewIndices.begin(), |
| 6840 | NewIndices.end(), ""); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 6841 | InsertNewInstBefore(NGEP, CI); |
| 6842 | NGEP->takeName(GEP); |
| 6843 | |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 6844 | if (isa<BitCastInst>(CI)) |
| 6845 | return new BitCastInst(NGEP, CI.getType()); |
| 6846 | assert(isa<PtrToIntInst>(CI)); |
| 6847 | return new PtrToIntInst(NGEP, CI.getType()); |
| 6848 | } |
| 6849 | } |
| 6850 | } |
| 6851 | } |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 6852 | } |
| 6853 | |
| 6854 | return commonCastTransforms(CI); |
| 6855 | } |
| 6856 | |
| 6857 | |
| 6858 | |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6859 | /// Only the TRUNC, ZEXT, SEXT, and BITCAST can both operand and result as |
| 6860 | /// integer types. This function implements the common transforms for all those |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6861 | /// cases. |
| 6862 | /// @brief Implement the transforms common to CastInst with integer operands |
| 6863 | Instruction *InstCombiner::commonIntCastTransforms(CastInst &CI) { |
| 6864 | if (Instruction *Result = commonCastTransforms(CI)) |
| 6865 | return Result; |
| 6866 | |
| 6867 | Value *Src = CI.getOperand(0); |
| 6868 | const Type *SrcTy = Src->getType(); |
| 6869 | const Type *DestTy = CI.getType(); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 6870 | uint32_t SrcBitSize = SrcTy->getPrimitiveSizeInBits(); |
| 6871 | uint32_t DestBitSize = DestTy->getPrimitiveSizeInBits(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6872 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6873 | // See if we can simplify any instructions used by the LHS whose sole |
| 6874 | // purpose is to compute bits we don't care about. |
Reid Spencer | ad6676e | 2007-03-22 20:56:53 +0000 | [diff] [blame] | 6875 | APInt KnownZero(DestBitSize, 0), KnownOne(DestBitSize, 0); |
| 6876 | if (SimplifyDemandedBits(&CI, APInt::getAllOnesValue(DestBitSize), |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6877 | KnownZero, KnownOne)) |
| 6878 | return &CI; |
| 6879 | |
| 6880 | // If the source isn't an instruction or has more than one use then we |
| 6881 | // can't do anything more. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6882 | Instruction *SrcI = dyn_cast<Instruction>(Src); |
| 6883 | if (!SrcI || !Src->hasOneUse()) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6884 | return 0; |
| 6885 | |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6886 | // Attempt to propagate the cast into the instruction for int->int casts. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6887 | int NumCastsRemoved = 0; |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6888 | if (!isa<BitCastInst>(CI) && |
| 6889 | CanEvaluateInDifferentType(SrcI, cast<IntegerType>(DestTy), |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 6890 | CI.getOpcode(), NumCastsRemoved)) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6891 | // If this cast is a truncate, evaluting in a different type always |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 6892 | // eliminates the cast, so it is always a win. If this is a zero-extension, |
| 6893 | // we need to do an AND to maintain the clear top-part of the computation, |
| 6894 | // so we require that the input have eliminated at least one cast. If this |
| 6895 | // 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] | 6896 | // require that two casts have been eliminated. |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6897 | bool DoXForm; |
| 6898 | switch (CI.getOpcode()) { |
| 6899 | default: |
| 6900 | // All the others use floating point so we shouldn't actually |
| 6901 | // get here because of the check above. |
| 6902 | assert(0 && "Unknown cast type"); |
| 6903 | case Instruction::Trunc: |
| 6904 | DoXForm = true; |
| 6905 | break; |
| 6906 | case Instruction::ZExt: |
| 6907 | DoXForm = NumCastsRemoved >= 1; |
| 6908 | break; |
| 6909 | case Instruction::SExt: |
| 6910 | DoXForm = NumCastsRemoved >= 2; |
| 6911 | break; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6912 | } |
| 6913 | |
| 6914 | if (DoXForm) { |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 6915 | Value *Res = EvaluateInDifferentType(SrcI, DestTy, |
| 6916 | CI.getOpcode() == Instruction::SExt); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6917 | assert(Res->getType() == DestTy); |
| 6918 | switch (CI.getOpcode()) { |
| 6919 | default: assert(0 && "Unknown cast type!"); |
| 6920 | case Instruction::Trunc: |
| 6921 | case Instruction::BitCast: |
| 6922 | // Just replace this cast with the result. |
| 6923 | return ReplaceInstUsesWith(CI, Res); |
| 6924 | case Instruction::ZExt: { |
| 6925 | // We need to emit an AND to clear the high bits. |
| 6926 | assert(SrcBitSize < DestBitSize && "Not a zext?"); |
Chris Lattner | cd1d6d5 | 2007-04-02 05:48:58 +0000 | [diff] [blame] | 6927 | Constant *C = ConstantInt::get(APInt::getLowBitsSet(DestBitSize, |
| 6928 | SrcBitSize)); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6929 | return BinaryOperator::createAnd(Res, C); |
| 6930 | } |
| 6931 | case Instruction::SExt: |
| 6932 | // We need to emit a cast to truncate, then a cast to sext. |
| 6933 | return CastInst::create(Instruction::SExt, |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 6934 | InsertCastBefore(Instruction::Trunc, Res, Src->getType(), |
| 6935 | CI), DestTy); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6936 | } |
| 6937 | } |
| 6938 | } |
| 6939 | |
| 6940 | Value *Op0 = SrcI->getNumOperands() > 0 ? SrcI->getOperand(0) : 0; |
| 6941 | Value *Op1 = SrcI->getNumOperands() > 1 ? SrcI->getOperand(1) : 0; |
| 6942 | |
| 6943 | switch (SrcI->getOpcode()) { |
| 6944 | case Instruction::Add: |
| 6945 | case Instruction::Mul: |
| 6946 | case Instruction::And: |
| 6947 | case Instruction::Or: |
| 6948 | case Instruction::Xor: |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6949 | // If we are discarding information, rewrite. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6950 | if (DestBitSize <= SrcBitSize && DestBitSize != 1) { |
| 6951 | // Don't insert two casts if they cannot be eliminated. We allow |
| 6952 | // two casts to be inserted if the sizes are the same. This could |
| 6953 | // only be converting signedness, which is a noop. |
| 6954 | if (DestBitSize == SrcBitSize || |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6955 | !ValueRequiresCast(CI.getOpcode(), Op1, DestTy,TD) || |
| 6956 | !ValueRequiresCast(CI.getOpcode(), Op0, DestTy, TD)) { |
Reid Spencer | 7eb7638 | 2006-12-13 17:19:09 +0000 | [diff] [blame] | 6957 | Instruction::CastOps opcode = CI.getOpcode(); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 6958 | Value *Op0c = InsertOperandCastBefore(opcode, Op0, DestTy, SrcI); |
| 6959 | Value *Op1c = InsertOperandCastBefore(opcode, Op1, DestTy, SrcI); |
| 6960 | return BinaryOperator::create( |
| 6961 | cast<BinaryOperator>(SrcI)->getOpcode(), Op0c, Op1c); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6962 | } |
| 6963 | } |
| 6964 | |
| 6965 | // cast (xor bool X, true) to int --> xor (cast bool X to int), 1 |
| 6966 | if (isa<ZExtInst>(CI) && SrcBitSize == 1 && |
| 6967 | SrcI->getOpcode() == Instruction::Xor && |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6968 | Op1 == ConstantInt::getTrue() && |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6969 | (!Op0->hasOneUse() || !isa<CmpInst>(Op0))) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 6970 | Value *New = InsertOperandCastBefore(Instruction::ZExt, Op0, DestTy, &CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6971 | return BinaryOperator::createXor(New, ConstantInt::get(CI.getType(), 1)); |
| 6972 | } |
| 6973 | break; |
| 6974 | case Instruction::SDiv: |
| 6975 | case Instruction::UDiv: |
| 6976 | case Instruction::SRem: |
| 6977 | case Instruction::URem: |
| 6978 | // If we are just changing the sign, rewrite. |
| 6979 | if (DestBitSize == SrcBitSize) { |
| 6980 | // Don't insert two casts if they cannot be eliminated. We allow |
| 6981 | // two casts to be inserted if the sizes are the same. This could |
| 6982 | // only be converting signedness, which is a noop. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6983 | if (!ValueRequiresCast(CI.getOpcode(), Op1, DestTy, TD) || |
| 6984 | !ValueRequiresCast(CI.getOpcode(), Op0, DestTy, TD)) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 6985 | Value *Op0c = InsertOperandCastBefore(Instruction::BitCast, |
| 6986 | Op0, DestTy, SrcI); |
| 6987 | Value *Op1c = InsertOperandCastBefore(Instruction::BitCast, |
| 6988 | Op1, DestTy, SrcI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6989 | return BinaryOperator::create( |
| 6990 | cast<BinaryOperator>(SrcI)->getOpcode(), Op0c, Op1c); |
| 6991 | } |
| 6992 | } |
| 6993 | break; |
| 6994 | |
| 6995 | case Instruction::Shl: |
| 6996 | // Allow changing the sign of the source operand. Do not allow |
| 6997 | // changing the size of the shift, UNLESS the shift amount is a |
| 6998 | // constant. We must not change variable sized shifts to a smaller |
| 6999 | // size, because it is undefined to shift more bits out than exist |
| 7000 | // in the value. |
| 7001 | if (DestBitSize == SrcBitSize || |
| 7002 | (DestBitSize < SrcBitSize && isa<Constant>(Op1))) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7003 | Instruction::CastOps opcode = (DestBitSize == SrcBitSize ? |
| 7004 | Instruction::BitCast : Instruction::Trunc); |
| 7005 | Value *Op0c = InsertOperandCastBefore(opcode, Op0, DestTy, SrcI); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7006 | Value *Op1c = InsertOperandCastBefore(opcode, Op1, DestTy, SrcI); |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 7007 | return BinaryOperator::createShl(Op0c, Op1c); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7008 | } |
| 7009 | break; |
| 7010 | case Instruction::AShr: |
| 7011 | // If this is a signed shr, and if all bits shifted in are about to be |
| 7012 | // truncated off, turn it into an unsigned shr to allow greater |
| 7013 | // simplifications. |
| 7014 | if (DestBitSize < SrcBitSize && |
| 7015 | isa<ConstantInt>(Op1)) { |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 7016 | uint32_t ShiftAmt = cast<ConstantInt>(Op1)->getLimitedValue(SrcBitSize); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7017 | if (SrcBitSize > ShiftAmt && SrcBitSize-ShiftAmt >= DestBitSize) { |
| 7018 | // Insert the new logical shift right. |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 7019 | return BinaryOperator::createLShr(Op0, Op1); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7020 | } |
| 7021 | } |
| 7022 | break; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7023 | } |
| 7024 | return 0; |
| 7025 | } |
| 7026 | |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 7027 | Instruction *InstCombiner::visitTrunc(TruncInst &CI) { |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 7028 | if (Instruction *Result = commonIntCastTransforms(CI)) |
| 7029 | return Result; |
| 7030 | |
| 7031 | Value *Src = CI.getOperand(0); |
| 7032 | const Type *Ty = CI.getType(); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 7033 | uint32_t DestBitWidth = Ty->getPrimitiveSizeInBits(); |
| 7034 | uint32_t SrcBitWidth = cast<IntegerType>(Src->getType())->getBitWidth(); |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 7035 | |
| 7036 | if (Instruction *SrcI = dyn_cast<Instruction>(Src)) { |
| 7037 | switch (SrcI->getOpcode()) { |
| 7038 | default: break; |
| 7039 | case Instruction::LShr: |
| 7040 | // We can shrink lshr to something smaller if we know the bits shifted in |
| 7041 | // are already zeros. |
| 7042 | if (ConstantInt *ShAmtV = dyn_cast<ConstantInt>(SrcI->getOperand(1))) { |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 7043 | uint32_t ShAmt = ShAmtV->getLimitedValue(SrcBitWidth); |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 7044 | |
| 7045 | // Get a mask for the bits shifting in. |
Zhou Sheng | e82fca0 | 2007-03-28 09:19:01 +0000 | [diff] [blame] | 7046 | APInt Mask(APInt::getLowBitsSet(SrcBitWidth, ShAmt).shl(DestBitWidth)); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7047 | Value* SrcIOp0 = SrcI->getOperand(0); |
| 7048 | if (SrcI->hasOneUse() && MaskedValueIsZero(SrcIOp0, Mask)) { |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 7049 | if (ShAmt >= DestBitWidth) // All zeros. |
| 7050 | return ReplaceInstUsesWith(CI, Constant::getNullValue(Ty)); |
| 7051 | |
| 7052 | // Okay, we can shrink this. Truncate the input, then return a new |
| 7053 | // shift. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7054 | Value *V1 = InsertCastBefore(Instruction::Trunc, SrcIOp0, Ty, CI); |
| 7055 | Value *V2 = InsertCastBefore(Instruction::Trunc, SrcI->getOperand(1), |
| 7056 | Ty, CI); |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 7057 | return BinaryOperator::createLShr(V1, V2); |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 7058 | } |
Chris Lattner | e13ab2a | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 7059 | } else { // This is a variable shr. |
| 7060 | |
| 7061 | // Turn 'trunc (lshr X, Y) to bool' into '(X & (1 << Y)) != 0'. This is |
| 7062 | // more LLVM instructions, but allows '1 << Y' to be hoisted if |
| 7063 | // loop-invariant and CSE'd. |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 7064 | if (CI.getType() == Type::Int1Ty && SrcI->hasOneUse()) { |
Chris Lattner | e13ab2a | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 7065 | Value *One = ConstantInt::get(SrcI->getType(), 1); |
| 7066 | |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7067 | Value *V = InsertNewInstBefore( |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 7068 | BinaryOperator::createShl(One, SrcI->getOperand(1), |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7069 | "tmp"), CI); |
Chris Lattner | e13ab2a | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 7070 | V = InsertNewInstBefore(BinaryOperator::createAnd(V, |
| 7071 | SrcI->getOperand(0), |
| 7072 | "tmp"), CI); |
| 7073 | Value *Zero = Constant::getNullValue(V->getType()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7074 | return new ICmpInst(ICmpInst::ICMP_NE, V, Zero); |
Chris Lattner | e13ab2a | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 7075 | } |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 7076 | } |
| 7077 | break; |
| 7078 | } |
| 7079 | } |
| 7080 | |
| 7081 | return 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7082 | } |
| 7083 | |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 7084 | Instruction *InstCombiner::visitZExt(ZExtInst &CI) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7085 | // If one of the common conversion will work .. |
| 7086 | if (Instruction *Result = commonIntCastTransforms(CI)) |
| 7087 | return Result; |
| 7088 | |
| 7089 | Value *Src = CI.getOperand(0); |
| 7090 | |
| 7091 | // If this is a cast of a cast |
| 7092 | if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7093 | // If this is a TRUNC followed by a ZEXT then we are dealing with integral |
| 7094 | // types and if the sizes are just right we can convert this into a logical |
| 7095 | // 'and' which will be much cheaper than the pair of casts. |
| 7096 | if (isa<TruncInst>(CSrc)) { |
| 7097 | // Get the sizes of the types involved |
| 7098 | Value *A = CSrc->getOperand(0); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 7099 | uint32_t SrcSize = A->getType()->getPrimitiveSizeInBits(); |
| 7100 | uint32_t MidSize = CSrc->getType()->getPrimitiveSizeInBits(); |
| 7101 | uint32_t DstSize = CI.getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7102 | // If we're actually extending zero bits and the trunc is a no-op |
| 7103 | if (MidSize < DstSize && SrcSize == DstSize) { |
| 7104 | // Replace both of the casts with an And of the type mask. |
Zhou Sheng | e82fca0 | 2007-03-28 09:19:01 +0000 | [diff] [blame] | 7105 | APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize)); |
Reid Spencer | ad6676e | 2007-03-22 20:56:53 +0000 | [diff] [blame] | 7106 | Constant *AndConst = ConstantInt::get(AndValue); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7107 | Instruction *And = |
| 7108 | BinaryOperator::createAnd(CSrc->getOperand(0), AndConst); |
| 7109 | // Unfortunately, if the type changed, we need to cast it back. |
| 7110 | if (And->getType() != CI.getType()) { |
| 7111 | And->setName(CSrc->getName()+".mask"); |
| 7112 | InsertNewInstBefore(And, CI); |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 7113 | And = CastInst::createIntegerCast(And, CI.getType(), false/*ZExt*/); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7114 | } |
| 7115 | return And; |
| 7116 | } |
| 7117 | } |
| 7118 | } |
| 7119 | |
Chris Lattner | 66bc325 | 2007-04-11 05:45:39 +0000 | [diff] [blame] | 7120 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(Src)) { |
| 7121 | // If we are just checking for a icmp eq of a single bit and zext'ing it |
| 7122 | // to an integer, then shift the bit to the appropriate place and then |
| 7123 | // cast to integer to avoid the comparison. |
| 7124 | if (ConstantInt *Op1C = dyn_cast<ConstantInt>(ICI->getOperand(1))) { |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 7125 | const APInt &Op1CV = Op1C->getValue(); |
Chris Lattner | a2e2c9b | 2007-04-11 06:53:04 +0000 | [diff] [blame] | 7126 | |
| 7127 | // zext (x <s 0) to i32 --> x>>u31 true if signbit set. |
| 7128 | // zext (x >s -1) to i32 --> (x>>u31)^1 true if signbit clear. |
| 7129 | if ((ICI->getPredicate() == ICmpInst::ICMP_SLT && Op1CV == 0) || |
| 7130 | (ICI->getPredicate() == ICmpInst::ICMP_SGT &&Op1CV.isAllOnesValue())){ |
| 7131 | Value *In = ICI->getOperand(0); |
| 7132 | Value *Sh = ConstantInt::get(In->getType(), |
| 7133 | In->getType()->getPrimitiveSizeInBits()-1); |
| 7134 | In = InsertNewInstBefore(BinaryOperator::createLShr(In, Sh, |
Chris Lattner | e34e9a2 | 2007-04-14 23:32:02 +0000 | [diff] [blame] | 7135 | In->getName()+".lobit"), |
Chris Lattner | a2e2c9b | 2007-04-11 06:53:04 +0000 | [diff] [blame] | 7136 | CI); |
| 7137 | if (In->getType() != CI.getType()) |
| 7138 | In = CastInst::createIntegerCast(In, CI.getType(), |
| 7139 | false/*ZExt*/, "tmp", &CI); |
| 7140 | |
| 7141 | if (ICI->getPredicate() == ICmpInst::ICMP_SGT) { |
| 7142 | Constant *One = ConstantInt::get(In->getType(), 1); |
| 7143 | In = InsertNewInstBefore(BinaryOperator::createXor(In, One, |
Chris Lattner | e34e9a2 | 2007-04-14 23:32:02 +0000 | [diff] [blame] | 7144 | In->getName()+".not"), |
Chris Lattner | a2e2c9b | 2007-04-11 06:53:04 +0000 | [diff] [blame] | 7145 | CI); |
| 7146 | } |
| 7147 | |
| 7148 | return ReplaceInstUsesWith(CI, In); |
| 7149 | } |
| 7150 | |
| 7151 | |
| 7152 | |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 7153 | // zext (X == 0) to i32 --> X^1 iff X has only the low bit set. |
| 7154 | // zext (X == 0) to i32 --> (X>>1)^1 iff X has only the 2nd bit set. |
| 7155 | // zext (X == 1) to i32 --> X iff X has only the low bit set. |
| 7156 | // zext (X == 2) to i32 --> X>>1 iff X has only the 2nd bit set. |
| 7157 | // zext (X != 0) to i32 --> X iff X has only the low bit set. |
| 7158 | // zext (X != 0) to i32 --> X>>1 iff X has only the 2nd bit set. |
| 7159 | // zext (X != 1) to i32 --> X^1 iff X has only the low bit set. |
| 7160 | // zext (X != 2) to i32 --> (X>>1)^1 iff X has only the 2nd bit set. |
Chris Lattner | 66bc325 | 2007-04-11 05:45:39 +0000 | [diff] [blame] | 7161 | if ((Op1CV == 0 || Op1CV.isPowerOf2()) && |
| 7162 | // This only works for EQ and NE |
| 7163 | ICI->isEquality()) { |
| 7164 | // If Op1C some other power of two, convert: |
| 7165 | uint32_t BitWidth = Op1C->getType()->getBitWidth(); |
| 7166 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 7167 | APInt TypeMask(APInt::getAllOnesValue(BitWidth)); |
| 7168 | ComputeMaskedBits(ICI->getOperand(0), TypeMask, KnownZero, KnownOne); |
| 7169 | |
| 7170 | APInt KnownZeroMask(~KnownZero); |
| 7171 | if (KnownZeroMask.isPowerOf2()) { // Exactly 1 possible 1? |
| 7172 | bool isNE = ICI->getPredicate() == ICmpInst::ICMP_NE; |
| 7173 | if (Op1CV != 0 && (Op1CV != KnownZeroMask)) { |
| 7174 | // (X&4) == 2 --> false |
| 7175 | // (X&4) != 2 --> true |
| 7176 | Constant *Res = ConstantInt::get(Type::Int1Ty, isNE); |
| 7177 | Res = ConstantExpr::getZExt(Res, CI.getType()); |
| 7178 | return ReplaceInstUsesWith(CI, Res); |
| 7179 | } |
| 7180 | |
| 7181 | uint32_t ShiftAmt = KnownZeroMask.logBase2(); |
| 7182 | Value *In = ICI->getOperand(0); |
| 7183 | if (ShiftAmt) { |
| 7184 | // Perform a logical shr by shiftamt. |
| 7185 | // Insert the shift to put the result in the low bit. |
| 7186 | In = InsertNewInstBefore( |
| 7187 | BinaryOperator::createLShr(In, |
| 7188 | ConstantInt::get(In->getType(), ShiftAmt), |
| 7189 | In->getName()+".lobit"), CI); |
| 7190 | } |
| 7191 | |
| 7192 | if ((Op1CV != 0) == isNE) { // Toggle the low bit. |
| 7193 | Constant *One = ConstantInt::get(In->getType(), 1); |
| 7194 | In = BinaryOperator::createXor(In, One, "tmp"); |
| 7195 | InsertNewInstBefore(cast<Instruction>(In), CI); |
| 7196 | } |
| 7197 | |
| 7198 | if (CI.getType() == In->getType()) |
| 7199 | return ReplaceInstUsesWith(CI, In); |
| 7200 | else |
| 7201 | return CastInst::createIntegerCast(In, CI.getType(), false/*ZExt*/); |
| 7202 | } |
| 7203 | } |
| 7204 | } |
| 7205 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7206 | return 0; |
| 7207 | } |
| 7208 | |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 7209 | Instruction *InstCombiner::visitSExt(SExtInst &CI) { |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 7210 | if (Instruction *I = commonIntCastTransforms(CI)) |
| 7211 | return I; |
| 7212 | |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 7213 | Value *Src = CI.getOperand(0); |
| 7214 | |
| 7215 | // sext (x <s 0) -> ashr x, 31 -> all ones if signed |
| 7216 | // sext (x >s -1) -> ashr x, 31 -> all ones if not signed |
| 7217 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(Src)) { |
| 7218 | // If we are just checking for a icmp eq of a single bit and zext'ing it |
| 7219 | // to an integer, then shift the bit to the appropriate place and then |
| 7220 | // cast to integer to avoid the comparison. |
| 7221 | if (ConstantInt *Op1C = dyn_cast<ConstantInt>(ICI->getOperand(1))) { |
| 7222 | const APInt &Op1CV = Op1C->getValue(); |
| 7223 | |
| 7224 | // sext (x <s 0) to i32 --> x>>s31 true if signbit set. |
| 7225 | // sext (x >s -1) to i32 --> (x>>s31)^-1 true if signbit clear. |
| 7226 | if ((ICI->getPredicate() == ICmpInst::ICMP_SLT && Op1CV == 0) || |
| 7227 | (ICI->getPredicate() == ICmpInst::ICMP_SGT &&Op1CV.isAllOnesValue())){ |
| 7228 | Value *In = ICI->getOperand(0); |
| 7229 | Value *Sh = ConstantInt::get(In->getType(), |
| 7230 | In->getType()->getPrimitiveSizeInBits()-1); |
| 7231 | In = InsertNewInstBefore(BinaryOperator::createAShr(In, Sh, |
Chris Lattner | e34e9a2 | 2007-04-14 23:32:02 +0000 | [diff] [blame] | 7232 | In->getName()+".lobit"), |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 7233 | CI); |
| 7234 | if (In->getType() != CI.getType()) |
| 7235 | In = CastInst::createIntegerCast(In, CI.getType(), |
| 7236 | true/*SExt*/, "tmp", &CI); |
| 7237 | |
| 7238 | if (ICI->getPredicate() == ICmpInst::ICMP_SGT) |
| 7239 | In = InsertNewInstBefore(BinaryOperator::createNot(In, |
| 7240 | In->getName()+".not"), CI); |
| 7241 | |
| 7242 | return ReplaceInstUsesWith(CI, In); |
| 7243 | } |
| 7244 | } |
| 7245 | } |
| 7246 | |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 7247 | return 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7248 | } |
| 7249 | |
Chris Lattner | b753065 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 7250 | /// FitsInFPType - Return a Constant* for the specified FP constant if it fits |
| 7251 | /// in the specified FP type without changing its value. |
| 7252 | static Constant *FitsInFPType(ConstantFP *CFP, const Type *FPTy, |
| 7253 | const fltSemantics &Sem) { |
| 7254 | APFloat F = CFP->getValueAPF(); |
| 7255 | if (F.convert(Sem, APFloat::rmNearestTiesToEven) == APFloat::opOK) |
| 7256 | return ConstantFP::get(FPTy, F); |
| 7257 | return 0; |
| 7258 | } |
| 7259 | |
| 7260 | /// LookThroughFPExtensions - If this is an fp extension instruction, look |
| 7261 | /// through it until we get the source value. |
| 7262 | static Value *LookThroughFPExtensions(Value *V) { |
| 7263 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 7264 | if (I->getOpcode() == Instruction::FPExt) |
| 7265 | return LookThroughFPExtensions(I->getOperand(0)); |
| 7266 | |
| 7267 | // If this value is a constant, return the constant in the smallest FP type |
| 7268 | // that can accurately represent it. This allows us to turn |
| 7269 | // (float)((double)X+2.0) into x+2.0f. |
| 7270 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) { |
| 7271 | if (CFP->getType() == Type::PPC_FP128Ty) |
| 7272 | return V; // No constant folding of this. |
| 7273 | // See if the value can be truncated to float and then reextended. |
| 7274 | if (Value *V = FitsInFPType(CFP, Type::FloatTy, APFloat::IEEEsingle)) |
| 7275 | return V; |
| 7276 | if (CFP->getType() == Type::DoubleTy) |
| 7277 | return V; // Won't shrink. |
| 7278 | if (Value *V = FitsInFPType(CFP, Type::DoubleTy, APFloat::IEEEdouble)) |
| 7279 | return V; |
| 7280 | // Don't try to shrink to various long double types. |
| 7281 | } |
| 7282 | |
| 7283 | return V; |
| 7284 | } |
| 7285 | |
| 7286 | Instruction *InstCombiner::visitFPTrunc(FPTruncInst &CI) { |
| 7287 | if (Instruction *I = commonCastTransforms(CI)) |
| 7288 | return I; |
| 7289 | |
| 7290 | // If we have fptrunc(add (fpextend x), (fpextend y)), where x and y are |
| 7291 | // smaller than the destination type, we can eliminate the truncate by doing |
| 7292 | // the add as the smaller type. This applies to add/sub/mul/div as well as |
| 7293 | // many builtins (sqrt, etc). |
| 7294 | BinaryOperator *OpI = dyn_cast<BinaryOperator>(CI.getOperand(0)); |
| 7295 | if (OpI && OpI->hasOneUse()) { |
| 7296 | switch (OpI->getOpcode()) { |
| 7297 | default: break; |
| 7298 | case Instruction::Add: |
| 7299 | case Instruction::Sub: |
| 7300 | case Instruction::Mul: |
| 7301 | case Instruction::FDiv: |
| 7302 | case Instruction::FRem: |
| 7303 | const Type *SrcTy = OpI->getType(); |
| 7304 | Value *LHSTrunc = LookThroughFPExtensions(OpI->getOperand(0)); |
| 7305 | Value *RHSTrunc = LookThroughFPExtensions(OpI->getOperand(1)); |
| 7306 | if (LHSTrunc->getType() != SrcTy && |
| 7307 | RHSTrunc->getType() != SrcTy) { |
| 7308 | unsigned DstSize = CI.getType()->getPrimitiveSizeInBits(); |
| 7309 | // If the source types were both smaller than the destination type of |
| 7310 | // the cast, do this xform. |
| 7311 | if (LHSTrunc->getType()->getPrimitiveSizeInBits() <= DstSize && |
| 7312 | RHSTrunc->getType()->getPrimitiveSizeInBits() <= DstSize) { |
| 7313 | LHSTrunc = InsertCastBefore(Instruction::FPExt, LHSTrunc, |
| 7314 | CI.getType(), CI); |
| 7315 | RHSTrunc = InsertCastBefore(Instruction::FPExt, RHSTrunc, |
| 7316 | CI.getType(), CI); |
| 7317 | return BinaryOperator::create(OpI->getOpcode(), LHSTrunc, RHSTrunc); |
| 7318 | } |
| 7319 | } |
| 7320 | break; |
| 7321 | } |
| 7322 | } |
| 7323 | return 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7324 | } |
| 7325 | |
| 7326 | Instruction *InstCombiner::visitFPExt(CastInst &CI) { |
| 7327 | return commonCastTransforms(CI); |
| 7328 | } |
| 7329 | |
| 7330 | Instruction *InstCombiner::visitFPToUI(CastInst &CI) { |
Reid Spencer | 44c030a | 2006-11-30 23:13:36 +0000 | [diff] [blame] | 7331 | return commonCastTransforms(CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7332 | } |
| 7333 | |
| 7334 | Instruction *InstCombiner::visitFPToSI(CastInst &CI) { |
Reid Spencer | 44c030a | 2006-11-30 23:13:36 +0000 | [diff] [blame] | 7335 | return commonCastTransforms(CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7336 | } |
| 7337 | |
| 7338 | Instruction *InstCombiner::visitUIToFP(CastInst &CI) { |
| 7339 | return commonCastTransforms(CI); |
| 7340 | } |
| 7341 | |
| 7342 | Instruction *InstCombiner::visitSIToFP(CastInst &CI) { |
| 7343 | return commonCastTransforms(CI); |
| 7344 | } |
| 7345 | |
| 7346 | Instruction *InstCombiner::visitPtrToInt(CastInst &CI) { |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7347 | return commonPointerCastTransforms(CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7348 | } |
| 7349 | |
Chris Lattner | f9d9e45 | 2008-01-08 07:23:51 +0000 | [diff] [blame] | 7350 | Instruction *InstCombiner::visitIntToPtr(IntToPtrInst &CI) { |
| 7351 | if (Instruction *I = commonCastTransforms(CI)) |
| 7352 | return I; |
| 7353 | |
| 7354 | const Type *DestPointee = cast<PointerType>(CI.getType())->getElementType(); |
| 7355 | if (!DestPointee->isSized()) return 0; |
| 7356 | |
| 7357 | // If this is inttoptr(add (ptrtoint x), cst), try to turn this into a GEP. |
| 7358 | ConstantInt *Cst; |
| 7359 | Value *X; |
| 7360 | if (match(CI.getOperand(0), m_Add(m_Cast<PtrToIntInst>(m_Value(X)), |
| 7361 | m_ConstantInt(Cst)))) { |
| 7362 | // If the source and destination operands have the same type, see if this |
| 7363 | // is a single-index GEP. |
| 7364 | if (X->getType() == CI.getType()) { |
| 7365 | // Get the size of the pointee type. |
| 7366 | uint64_t Size = TD->getABITypeSizeInBits(DestPointee); |
| 7367 | |
| 7368 | // Convert the constant to intptr type. |
| 7369 | APInt Offset = Cst->getValue(); |
| 7370 | Offset.sextOrTrunc(TD->getPointerSizeInBits()); |
| 7371 | |
| 7372 | // If Offset is evenly divisible by Size, we can do this xform. |
| 7373 | if (Size && !APIntOps::srem(Offset, APInt(Offset.getBitWidth(), Size))){ |
| 7374 | Offset = APIntOps::sdiv(Offset, APInt(Offset.getBitWidth(), Size)); |
| 7375 | return new GetElementPtrInst(X, ConstantInt::get(Offset)); |
| 7376 | } |
| 7377 | } |
| 7378 | // TODO: Could handle other cases, e.g. where add is indexing into field of |
| 7379 | // struct etc. |
| 7380 | } else if (CI.getOperand(0)->hasOneUse() && |
| 7381 | match(CI.getOperand(0), m_Add(m_Value(X), m_ConstantInt(Cst)))) { |
| 7382 | // Otherwise, if this is inttoptr(add x, cst), try to turn this into an |
| 7383 | // "inttoptr+GEP" instead of "add+intptr". |
| 7384 | |
| 7385 | // Get the size of the pointee type. |
| 7386 | uint64_t Size = TD->getABITypeSize(DestPointee); |
| 7387 | |
| 7388 | // Convert the constant to intptr type. |
| 7389 | APInt Offset = Cst->getValue(); |
| 7390 | Offset.sextOrTrunc(TD->getPointerSizeInBits()); |
| 7391 | |
| 7392 | // If Offset is evenly divisible by Size, we can do this xform. |
| 7393 | if (Size && !APIntOps::srem(Offset, APInt(Offset.getBitWidth(), Size))){ |
| 7394 | Offset = APIntOps::sdiv(Offset, APInt(Offset.getBitWidth(), Size)); |
| 7395 | |
| 7396 | Instruction *P = InsertNewInstBefore(new IntToPtrInst(X, CI.getType(), |
| 7397 | "tmp"), CI); |
| 7398 | return new GetElementPtrInst(P, ConstantInt::get(Offset), "tmp"); |
| 7399 | } |
| 7400 | } |
| 7401 | return 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7402 | } |
| 7403 | |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7404 | Instruction *InstCombiner::visitBitCast(BitCastInst &CI) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7405 | // If the operands are integer typed then apply the integer transforms, |
| 7406 | // otherwise just apply the common ones. |
| 7407 | Value *Src = CI.getOperand(0); |
| 7408 | const Type *SrcTy = Src->getType(); |
| 7409 | const Type *DestTy = CI.getType(); |
| 7410 | |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 7411 | if (SrcTy->isInteger() && DestTy->isInteger()) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7412 | if (Instruction *Result = commonIntCastTransforms(CI)) |
| 7413 | return Result; |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7414 | } else if (isa<PointerType>(SrcTy)) { |
| 7415 | if (Instruction *I = commonPointerCastTransforms(CI)) |
| 7416 | return I; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7417 | } else { |
| 7418 | if (Instruction *Result = commonCastTransforms(CI)) |
| 7419 | return Result; |
| 7420 | } |
| 7421 | |
| 7422 | |
| 7423 | // Get rid of casts from one type to the same type. These are useless and can |
| 7424 | // be replaced by the operand. |
| 7425 | if (DestTy == Src->getType()) |
| 7426 | return ReplaceInstUsesWith(CI, Src); |
| 7427 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7428 | if (const PointerType *DstPTy = dyn_cast<PointerType>(DestTy)) { |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7429 | const PointerType *SrcPTy = cast<PointerType>(SrcTy); |
| 7430 | const Type *DstElTy = DstPTy->getElementType(); |
| 7431 | const Type *SrcElTy = SrcPTy->getElementType(); |
| 7432 | |
| 7433 | // If we are casting a malloc or alloca to a pointer to a type of the same |
| 7434 | // size, rewrite the allocation instruction to allocate the "right" type. |
| 7435 | if (AllocationInst *AI = dyn_cast<AllocationInst>(Src)) |
| 7436 | if (Instruction *V = PromoteCastOfAllocation(CI, *AI)) |
| 7437 | return V; |
| 7438 | |
Chris Lattner | d717c18 | 2007-05-05 22:32:24 +0000 | [diff] [blame] | 7439 | // If the source and destination are pointers, and this cast is equivalent |
| 7440 | // 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] | 7441 | // This can enhance SROA and other transforms that want type-safe pointers. |
| 7442 | Constant *ZeroUInt = Constant::getNullValue(Type::Int32Ty); |
| 7443 | unsigned NumZeros = 0; |
| 7444 | while (SrcElTy != DstElTy && |
| 7445 | isa<CompositeType>(SrcElTy) && !isa<PointerType>(SrcElTy) && |
| 7446 | SrcElTy->getNumContainedTypes() /* not "{}" */) { |
| 7447 | SrcElTy = cast<CompositeType>(SrcElTy)->getTypeAtIndex(ZeroUInt); |
| 7448 | ++NumZeros; |
| 7449 | } |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 7450 | |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7451 | // If we found a path from the src to dest, create the getelementptr now. |
| 7452 | if (SrcElTy == DstElTy) { |
| 7453 | SmallVector<Value*, 8> Idxs(NumZeros+1, ZeroUInt); |
Chuck Rose III | c331d30 | 2007-09-05 20:36:41 +0000 | [diff] [blame] | 7454 | return new GetElementPtrInst(Src, Idxs.begin(), Idxs.end(), "", |
| 7455 | ((Instruction*) NULL)); |
Chris Lattner | 9fb9213 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 7456 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7457 | } |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 7458 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7459 | if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(Src)) { |
| 7460 | if (SVI->hasOneUse()) { |
| 7461 | // Okay, we have (bitconvert (shuffle ..)). Check to see if this is |
| 7462 | // a bitconvert to a vector with the same # elts. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 7463 | if (isa<VectorType>(DestTy) && |
| 7464 | cast<VectorType>(DestTy)->getNumElements() == |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7465 | SVI->getType()->getNumElements()) { |
| 7466 | CastInst *Tmp; |
| 7467 | // If either of the operands is a cast from CI.getType(), then |
| 7468 | // evaluating the shuffle in the casted destination's type will allow |
| 7469 | // us to eliminate at least one cast. |
| 7470 | if (((Tmp = dyn_cast<CastInst>(SVI->getOperand(0))) && |
| 7471 | Tmp->getOperand(0)->getType() == DestTy) || |
| 7472 | ((Tmp = dyn_cast<CastInst>(SVI->getOperand(1))) && |
| 7473 | Tmp->getOperand(0)->getType() == DestTy)) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7474 | Value *LHS = InsertOperandCastBefore(Instruction::BitCast, |
| 7475 | SVI->getOperand(0), DestTy, &CI); |
| 7476 | Value *RHS = InsertOperandCastBefore(Instruction::BitCast, |
| 7477 | SVI->getOperand(1), DestTy, &CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7478 | // Return a new shuffle vector. Use the same element ID's, as we |
| 7479 | // know the vector types match #elts. |
| 7480 | return new ShuffleVectorInst(LHS, RHS, SVI->getOperand(2)); |
Chris Lattner | 01575b7 | 2006-05-25 23:24:33 +0000 | [diff] [blame] | 7481 | } |
| 7482 | } |
| 7483 | } |
| 7484 | } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 7485 | return 0; |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 7486 | } |
| 7487 | |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7488 | /// GetSelectFoldableOperands - We want to turn code that looks like this: |
| 7489 | /// %C = or %A, %B |
| 7490 | /// %D = select %cond, %C, %A |
| 7491 | /// into: |
| 7492 | /// %C = select %cond, %B, 0 |
| 7493 | /// %D = or %A, %C |
| 7494 | /// |
| 7495 | /// Assuming that the specified instruction is an operand to the select, return |
| 7496 | /// a bitmask indicating which operands of this instruction are foldable if they |
| 7497 | /// equal the other incoming value of the select. |
| 7498 | /// |
| 7499 | static unsigned GetSelectFoldableOperands(Instruction *I) { |
| 7500 | switch (I->getOpcode()) { |
| 7501 | case Instruction::Add: |
| 7502 | case Instruction::Mul: |
| 7503 | case Instruction::And: |
| 7504 | case Instruction::Or: |
| 7505 | case Instruction::Xor: |
| 7506 | return 3; // Can fold through either operand. |
| 7507 | case Instruction::Sub: // Can only fold on the amount subtracted. |
| 7508 | case Instruction::Shl: // Can only fold on the shift amount. |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 7509 | case Instruction::LShr: |
| 7510 | case Instruction::AShr: |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7511 | return 1; |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7512 | default: |
| 7513 | return 0; // Cannot fold |
| 7514 | } |
| 7515 | } |
| 7516 | |
| 7517 | /// GetSelectFoldableConstant - For the same transformation as the previous |
| 7518 | /// function, return the identity constant that goes into the select. |
| 7519 | static Constant *GetSelectFoldableConstant(Instruction *I) { |
| 7520 | switch (I->getOpcode()) { |
| 7521 | default: assert(0 && "This cannot happen!"); abort(); |
| 7522 | case Instruction::Add: |
| 7523 | case Instruction::Sub: |
| 7524 | case Instruction::Or: |
| 7525 | case Instruction::Xor: |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7526 | case Instruction::Shl: |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 7527 | case Instruction::LShr: |
| 7528 | case Instruction::AShr: |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7529 | return Constant::getNullValue(I->getType()); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7530 | case Instruction::And: |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 7531 | return Constant::getAllOnesValue(I->getType()); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7532 | case Instruction::Mul: |
| 7533 | return ConstantInt::get(I->getType(), 1); |
| 7534 | } |
| 7535 | } |
| 7536 | |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 7537 | /// FoldSelectOpOp - Here we have (select c, TI, FI), and we know that TI and FI |
| 7538 | /// have the same opcode and only one use each. Try to simplify this. |
| 7539 | Instruction *InstCombiner::FoldSelectOpOp(SelectInst &SI, Instruction *TI, |
| 7540 | Instruction *FI) { |
| 7541 | if (TI->getNumOperands() == 1) { |
| 7542 | // If this is a non-volatile load or a cast from the same type, |
| 7543 | // merge. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7544 | if (TI->isCast()) { |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 7545 | if (TI->getOperand(0)->getType() != FI->getOperand(0)->getType()) |
| 7546 | return 0; |
| 7547 | } else { |
| 7548 | return 0; // unknown unary op. |
| 7549 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7550 | |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 7551 | // Fold this by inserting a select from the input values. |
| 7552 | SelectInst *NewSI = new SelectInst(SI.getCondition(), TI->getOperand(0), |
| 7553 | FI->getOperand(0), SI.getName()+".v"); |
| 7554 | InsertNewInstBefore(NewSI, SI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7555 | return CastInst::create(Instruction::CastOps(TI->getOpcode()), NewSI, |
| 7556 | TI->getType()); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 7557 | } |
| 7558 | |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7559 | // Only handle binary operators here. |
| 7560 | if (!isa<BinaryOperator>(TI)) |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 7561 | return 0; |
| 7562 | |
| 7563 | // Figure out if the operations have any operands in common. |
| 7564 | Value *MatchOp, *OtherOpT, *OtherOpF; |
| 7565 | bool MatchIsOpZero; |
| 7566 | if (TI->getOperand(0) == FI->getOperand(0)) { |
| 7567 | MatchOp = TI->getOperand(0); |
| 7568 | OtherOpT = TI->getOperand(1); |
| 7569 | OtherOpF = FI->getOperand(1); |
| 7570 | MatchIsOpZero = true; |
| 7571 | } else if (TI->getOperand(1) == FI->getOperand(1)) { |
| 7572 | MatchOp = TI->getOperand(1); |
| 7573 | OtherOpT = TI->getOperand(0); |
| 7574 | OtherOpF = FI->getOperand(0); |
| 7575 | MatchIsOpZero = false; |
| 7576 | } else if (!TI->isCommutative()) { |
| 7577 | return 0; |
| 7578 | } else if (TI->getOperand(0) == FI->getOperand(1)) { |
| 7579 | MatchOp = TI->getOperand(0); |
| 7580 | OtherOpT = TI->getOperand(1); |
| 7581 | OtherOpF = FI->getOperand(0); |
| 7582 | MatchIsOpZero = true; |
| 7583 | } else if (TI->getOperand(1) == FI->getOperand(0)) { |
| 7584 | MatchOp = TI->getOperand(1); |
| 7585 | OtherOpT = TI->getOperand(0); |
| 7586 | OtherOpF = FI->getOperand(1); |
| 7587 | MatchIsOpZero = true; |
| 7588 | } else { |
| 7589 | return 0; |
| 7590 | } |
| 7591 | |
| 7592 | // If we reach here, they do have operations in common. |
| 7593 | SelectInst *NewSI = new SelectInst(SI.getCondition(), OtherOpT, |
| 7594 | OtherOpF, SI.getName()+".v"); |
| 7595 | InsertNewInstBefore(NewSI, SI); |
| 7596 | |
| 7597 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TI)) { |
| 7598 | if (MatchIsOpZero) |
| 7599 | return BinaryOperator::create(BO->getOpcode(), MatchOp, NewSI); |
| 7600 | else |
| 7601 | return BinaryOperator::create(BO->getOpcode(), NewSI, MatchOp); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 7602 | } |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 7603 | assert(0 && "Shouldn't get here"); |
| 7604 | return 0; |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 7605 | } |
| 7606 | |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 7607 | Instruction *InstCombiner::visitSelectInst(SelectInst &SI) { |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 7608 | Value *CondVal = SI.getCondition(); |
| 7609 | Value *TrueVal = SI.getTrueValue(); |
| 7610 | Value *FalseVal = SI.getFalseValue(); |
| 7611 | |
| 7612 | // select true, X, Y -> X |
| 7613 | // select false, X, Y -> Y |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 7614 | if (ConstantInt *C = dyn_cast<ConstantInt>(CondVal)) |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 7615 | return ReplaceInstUsesWith(SI, C->getZExtValue() ? TrueVal : FalseVal); |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 7616 | |
| 7617 | // select C, X, X -> X |
| 7618 | if (TrueVal == FalseVal) |
| 7619 | return ReplaceInstUsesWith(SI, TrueVal); |
| 7620 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 7621 | if (isa<UndefValue>(TrueVal)) // select C, undef, X -> X |
| 7622 | return ReplaceInstUsesWith(SI, FalseVal); |
| 7623 | if (isa<UndefValue>(FalseVal)) // select C, X, undef -> X |
| 7624 | return ReplaceInstUsesWith(SI, TrueVal); |
| 7625 | if (isa<UndefValue>(CondVal)) { // select undef, X, Y -> X or Y |
| 7626 | if (isa<Constant>(TrueVal)) |
| 7627 | return ReplaceInstUsesWith(SI, TrueVal); |
| 7628 | else |
| 7629 | return ReplaceInstUsesWith(SI, FalseVal); |
| 7630 | } |
| 7631 | |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 7632 | if (SI.getType() == Type::Int1Ty) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 7633 | if (ConstantInt *C = dyn_cast<ConstantInt>(TrueVal)) { |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 7634 | if (C->getZExtValue()) { |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 7635 | // Change: A = select B, true, C --> A = or B, C |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 7636 | return BinaryOperator::createOr(CondVal, FalseVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 7637 | } else { |
| 7638 | // Change: A = select B, false, C --> A = and !B, C |
| 7639 | Value *NotCond = |
| 7640 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
| 7641 | "not."+CondVal->getName()), SI); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 7642 | return BinaryOperator::createAnd(NotCond, FalseVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 7643 | } |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 7644 | } else if (ConstantInt *C = dyn_cast<ConstantInt>(FalseVal)) { |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 7645 | if (C->getZExtValue() == false) { |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 7646 | // Change: A = select B, C, false --> A = and B, C |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 7647 | return BinaryOperator::createAnd(CondVal, TrueVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 7648 | } else { |
| 7649 | // Change: A = select B, C, true --> A = or !B, C |
| 7650 | Value *NotCond = |
| 7651 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
| 7652 | "not."+CondVal->getName()), SI); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 7653 | return BinaryOperator::createOr(NotCond, TrueVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 7654 | } |
| 7655 | } |
Chris Lattner | cfa5975 | 2007-11-25 21:27:53 +0000 | [diff] [blame] | 7656 | |
| 7657 | // select a, b, a -> a&b |
| 7658 | // select a, a, b -> a|b |
| 7659 | if (CondVal == TrueVal) |
| 7660 | return BinaryOperator::createOr(CondVal, FalseVal); |
| 7661 | else if (CondVal == FalseVal) |
| 7662 | return BinaryOperator::createAnd(CondVal, TrueVal); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 7663 | } |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 7664 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 7665 | // Selecting between two integer constants? |
| 7666 | if (ConstantInt *TrueValC = dyn_cast<ConstantInt>(TrueVal)) |
| 7667 | if (ConstantInt *FalseValC = dyn_cast<ConstantInt>(FalseVal)) { |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 7668 | // select C, 1, 0 -> zext C to int |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 7669 | if (FalseValC->isZero() && TrueValC->getValue() == 1) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7670 | return CastInst::create(Instruction::ZExt, CondVal, SI.getType()); |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 7671 | } else if (TrueValC->isZero() && FalseValC->getValue() == 1) { |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 7672 | // select C, 0, 1 -> zext !C to int |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 7673 | Value *NotCond = |
| 7674 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
Chris Lattner | 82e14fe | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 7675 | "not."+CondVal->getName()), SI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7676 | return CastInst::create(Instruction::ZExt, NotCond, SI.getType()); |
Chris Lattner | 82e14fe | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 7677 | } |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 7678 | |
| 7679 | // FIXME: Turn select 0/-1 and -1/0 into sext from condition! |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 7680 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7681 | if (ICmpInst *IC = dyn_cast<ICmpInst>(SI.getCondition())) { |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 7682 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7683 | // (x <s 0) ? -1 : 0 -> ashr x, 31 |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 7684 | if (TrueValC->isAllOnesValue() && FalseValC->isZero()) |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 7685 | if (ConstantInt *CmpCst = dyn_cast<ConstantInt>(IC->getOperand(1))) { |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 7686 | if (IC->getPredicate() == ICmpInst::ICMP_SLT && CmpCst->isZero()) { |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 7687 | // The comparison constant and the result are not neccessarily the |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7688 | // same width. Make an all-ones value by inserting a AShr. |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 7689 | Value *X = IC->getOperand(0); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 7690 | uint32_t Bits = X->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7691 | Constant *ShAmt = ConstantInt::get(X->getType(), Bits-1); |
| 7692 | Instruction *SRA = BinaryOperator::create(Instruction::AShr, X, |
| 7693 | ShAmt, "ones"); |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 7694 | InsertNewInstBefore(SRA, SI); |
| 7695 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7696 | // Finally, convert to the type of the select RHS. We figure out |
| 7697 | // if this requires a SExt, Trunc or BitCast based on the sizes. |
| 7698 | Instruction::CastOps opc = Instruction::BitCast; |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 7699 | uint32_t SRASize = SRA->getType()->getPrimitiveSizeInBits(); |
| 7700 | uint32_t SISize = SI.getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7701 | if (SRASize < SISize) |
| 7702 | opc = Instruction::SExt; |
| 7703 | else if (SRASize > SISize) |
| 7704 | opc = Instruction::Trunc; |
| 7705 | return CastInst::create(opc, SRA, SI.getType()); |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 7706 | } |
| 7707 | } |
| 7708 | |
| 7709 | |
| 7710 | // 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] | 7711 | // 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] | 7712 | // non-constant value, eliminate this whole mess. This corresponds to |
| 7713 | // cases like this: ((X & 27) ? 27 : 0) |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 7714 | if (TrueValC->isZero() || FalseValC->isZero()) |
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 7715 | if (IC->isEquality() && isa<ConstantInt>(IC->getOperand(1)) && |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 7716 | cast<Constant>(IC->getOperand(1))->isNullValue()) |
| 7717 | if (Instruction *ICA = dyn_cast<Instruction>(IC->getOperand(0))) |
| 7718 | if (ICA->getOpcode() == Instruction::And && |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7719 | isa<ConstantInt>(ICA->getOperand(1)) && |
| 7720 | (ICA->getOperand(1) == TrueValC || |
| 7721 | ICA->getOperand(1) == FalseValC) && |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 7722 | isOneBitSet(cast<ConstantInt>(ICA->getOperand(1)))) { |
| 7723 | // 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] | 7724 | // know whether we have a icmp_ne or icmp_eq and whether the |
| 7725 | // true or false val is the zero. |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 7726 | bool ShouldNotVal = !TrueValC->isZero(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7727 | ShouldNotVal ^= IC->getPredicate() == ICmpInst::ICMP_NE; |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 7728 | Value *V = ICA; |
| 7729 | if (ShouldNotVal) |
| 7730 | V = InsertNewInstBefore(BinaryOperator::create( |
| 7731 | Instruction::Xor, V, ICA->getOperand(1)), SI); |
| 7732 | return ReplaceInstUsesWith(SI, V); |
| 7733 | } |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 7734 | } |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 7735 | } |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 7736 | |
| 7737 | // 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] | 7738 | if (FCmpInst *FCI = dyn_cast<FCmpInst>(CondVal)) { |
| 7739 | if (FCI->getOperand(0) == TrueVal && FCI->getOperand(1) == FalseVal) { |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 7740 | // Transform (X == Y) ? X : Y -> Y |
Dale Johannesen | 5a2174f | 2007-10-03 17:45:27 +0000 | [diff] [blame] | 7741 | if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) { |
| 7742 | // This is not safe in general for floating point: |
| 7743 | // consider X== -0, Y== +0. |
| 7744 | // It becomes safe if either operand is a nonzero constant. |
| 7745 | ConstantFP *CFPt, *CFPf; |
| 7746 | if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) && |
| 7747 | !CFPt->getValueAPF().isZero()) || |
| 7748 | ((CFPf = dyn_cast<ConstantFP>(FalseVal)) && |
| 7749 | !CFPf->getValueAPF().isZero())) |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 7750 | return ReplaceInstUsesWith(SI, FalseVal); |
Dale Johannesen | 5a2174f | 2007-10-03 17:45:27 +0000 | [diff] [blame] | 7751 | } |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 7752 | // Transform (X != Y) ? X : Y -> X |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7753 | if (FCI->getPredicate() == FCmpInst::FCMP_ONE) |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 7754 | return ReplaceInstUsesWith(SI, TrueVal); |
| 7755 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 7756 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7757 | } else if (FCI->getOperand(0) == FalseVal && FCI->getOperand(1) == TrueVal){ |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 7758 | // Transform (X == Y) ? Y : X -> X |
Dale Johannesen | 5a2174f | 2007-10-03 17:45:27 +0000 | [diff] [blame] | 7759 | if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) { |
| 7760 | // This is not safe in general for floating point: |
| 7761 | // consider X== -0, Y== +0. |
| 7762 | // It becomes safe if either operand is a nonzero constant. |
| 7763 | ConstantFP *CFPt, *CFPf; |
| 7764 | if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) && |
| 7765 | !CFPt->getValueAPF().isZero()) || |
| 7766 | ((CFPf = dyn_cast<ConstantFP>(FalseVal)) && |
| 7767 | !CFPf->getValueAPF().isZero())) |
| 7768 | return ReplaceInstUsesWith(SI, FalseVal); |
| 7769 | } |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 7770 | // Transform (X != Y) ? Y : X -> Y |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7771 | if (FCI->getPredicate() == FCmpInst::FCMP_ONE) |
| 7772 | return ReplaceInstUsesWith(SI, TrueVal); |
| 7773 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 7774 | } |
| 7775 | } |
| 7776 | |
| 7777 | // See if we are selecting two values based on a comparison of the two values. |
| 7778 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(CondVal)) { |
| 7779 | if (ICI->getOperand(0) == TrueVal && ICI->getOperand(1) == FalseVal) { |
| 7780 | // Transform (X == Y) ? X : Y -> Y |
| 7781 | if (ICI->getPredicate() == ICmpInst::ICMP_EQ) |
| 7782 | return ReplaceInstUsesWith(SI, FalseVal); |
| 7783 | // Transform (X != Y) ? X : Y -> X |
| 7784 | if (ICI->getPredicate() == ICmpInst::ICMP_NE) |
| 7785 | return ReplaceInstUsesWith(SI, TrueVal); |
| 7786 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 7787 | |
| 7788 | } else if (ICI->getOperand(0) == FalseVal && ICI->getOperand(1) == TrueVal){ |
| 7789 | // Transform (X == Y) ? Y : X -> X |
| 7790 | if (ICI->getPredicate() == ICmpInst::ICMP_EQ) |
| 7791 | return ReplaceInstUsesWith(SI, FalseVal); |
| 7792 | // Transform (X != Y) ? Y : X -> Y |
| 7793 | if (ICI->getPredicate() == ICmpInst::ICMP_NE) |
Chris Lattner | fbede52 | 2004-04-11 01:39:19 +0000 | [diff] [blame] | 7794 | return ReplaceInstUsesWith(SI, TrueVal); |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 7795 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 7796 | } |
| 7797 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7798 | |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 7799 | if (Instruction *TI = dyn_cast<Instruction>(TrueVal)) |
| 7800 | if (Instruction *FI = dyn_cast<Instruction>(FalseVal)) |
| 7801 | if (TI->hasOneUse() && FI->hasOneUse()) { |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 7802 | Instruction *AddOp = 0, *SubOp = 0; |
| 7803 | |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 7804 | // Turn (select C, (op X, Y), (op X, Z)) -> (op X, (select C, Y, Z)) |
| 7805 | if (TI->getOpcode() == FI->getOpcode()) |
| 7806 | if (Instruction *IV = FoldSelectOpOp(SI, TI, FI)) |
| 7807 | return IV; |
| 7808 | |
| 7809 | // Turn select C, (X+Y), (X-Y) --> (X+(select C, Y, (-Y))). This is |
| 7810 | // even legal for FP. |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 7811 | if (TI->getOpcode() == Instruction::Sub && |
| 7812 | FI->getOpcode() == Instruction::Add) { |
| 7813 | AddOp = FI; SubOp = TI; |
| 7814 | } else if (FI->getOpcode() == Instruction::Sub && |
| 7815 | TI->getOpcode() == Instruction::Add) { |
| 7816 | AddOp = TI; SubOp = FI; |
| 7817 | } |
| 7818 | |
| 7819 | if (AddOp) { |
| 7820 | Value *OtherAddOp = 0; |
| 7821 | if (SubOp->getOperand(0) == AddOp->getOperand(0)) { |
| 7822 | OtherAddOp = AddOp->getOperand(1); |
| 7823 | } else if (SubOp->getOperand(0) == AddOp->getOperand(1)) { |
| 7824 | OtherAddOp = AddOp->getOperand(0); |
| 7825 | } |
| 7826 | |
| 7827 | if (OtherAddOp) { |
Chris Lattner | 97f37a4 | 2006-02-24 18:05:58 +0000 | [diff] [blame] | 7828 | // So at this point we know we have (Y -> OtherAddOp): |
| 7829 | // select C, (add X, Y), (sub X, Z) |
| 7830 | Value *NegVal; // Compute -Z |
| 7831 | if (Constant *C = dyn_cast<Constant>(SubOp->getOperand(1))) { |
| 7832 | NegVal = ConstantExpr::getNeg(C); |
| 7833 | } else { |
| 7834 | NegVal = InsertNewInstBefore( |
| 7835 | BinaryOperator::createNeg(SubOp->getOperand(1), "tmp"), SI); |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 7836 | } |
Chris Lattner | 97f37a4 | 2006-02-24 18:05:58 +0000 | [diff] [blame] | 7837 | |
| 7838 | Value *NewTrueOp = OtherAddOp; |
| 7839 | Value *NewFalseOp = NegVal; |
| 7840 | if (AddOp != TI) |
| 7841 | std::swap(NewTrueOp, NewFalseOp); |
| 7842 | Instruction *NewSel = |
| 7843 | new SelectInst(CondVal, NewTrueOp,NewFalseOp,SI.getName()+".p"); |
| 7844 | |
| 7845 | NewSel = InsertNewInstBefore(NewSel, SI); |
| 7846 | return BinaryOperator::createAdd(SubOp->getOperand(0), NewSel); |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 7847 | } |
| 7848 | } |
| 7849 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7850 | |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7851 | // See if we can fold the select into one of our operands. |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 7852 | if (SI.getType()->isInteger()) { |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7853 | // See the comment above GetSelectFoldableOperands for a description of the |
| 7854 | // transformation we are doing here. |
| 7855 | if (Instruction *TVI = dyn_cast<Instruction>(TrueVal)) |
| 7856 | if (TVI->hasOneUse() && TVI->getNumOperands() == 2 && |
| 7857 | !isa<Constant>(FalseVal)) |
| 7858 | if (unsigned SFO = GetSelectFoldableOperands(TVI)) { |
| 7859 | unsigned OpToFold = 0; |
| 7860 | if ((SFO & 1) && FalseVal == TVI->getOperand(0)) { |
| 7861 | OpToFold = 1; |
| 7862 | } else if ((SFO & 2) && FalseVal == TVI->getOperand(1)) { |
| 7863 | OpToFold = 2; |
| 7864 | } |
| 7865 | |
| 7866 | if (OpToFold) { |
| 7867 | Constant *C = GetSelectFoldableConstant(TVI); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7868 | Instruction *NewSel = |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 7869 | new SelectInst(SI.getCondition(), TVI->getOperand(2-OpToFold), C); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7870 | InsertNewInstBefore(NewSel, SI); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 7871 | NewSel->takeName(TVI); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7872 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TVI)) |
| 7873 | return BinaryOperator::create(BO->getOpcode(), FalseVal, NewSel); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7874 | else { |
| 7875 | assert(0 && "Unknown instruction!!"); |
| 7876 | } |
| 7877 | } |
| 7878 | } |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 7879 | |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7880 | if (Instruction *FVI = dyn_cast<Instruction>(FalseVal)) |
| 7881 | if (FVI->hasOneUse() && FVI->getNumOperands() == 2 && |
| 7882 | !isa<Constant>(TrueVal)) |
| 7883 | if (unsigned SFO = GetSelectFoldableOperands(FVI)) { |
| 7884 | unsigned OpToFold = 0; |
| 7885 | if ((SFO & 1) && TrueVal == FVI->getOperand(0)) { |
| 7886 | OpToFold = 1; |
| 7887 | } else if ((SFO & 2) && TrueVal == FVI->getOperand(1)) { |
| 7888 | OpToFold = 2; |
| 7889 | } |
| 7890 | |
| 7891 | if (OpToFold) { |
| 7892 | Constant *C = GetSelectFoldableConstant(FVI); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7893 | Instruction *NewSel = |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 7894 | new SelectInst(SI.getCondition(), C, FVI->getOperand(2-OpToFold)); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7895 | InsertNewInstBefore(NewSel, SI); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 7896 | NewSel->takeName(FVI); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7897 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(FVI)) |
| 7898 | return BinaryOperator::create(BO->getOpcode(), TrueVal, NewSel); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7899 | else |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7900 | assert(0 && "Unknown instruction!!"); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7901 | } |
| 7902 | } |
| 7903 | } |
Chris Lattner | a1df33c | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 7904 | |
| 7905 | if (BinaryOperator::isNot(CondVal)) { |
| 7906 | SI.setOperand(0, BinaryOperator::getNotArgument(CondVal)); |
| 7907 | SI.setOperand(1, FalseVal); |
| 7908 | SI.setOperand(2, TrueVal); |
| 7909 | return &SI; |
| 7910 | } |
| 7911 | |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 7912 | return 0; |
| 7913 | } |
| 7914 | |
Chris Lattner | f2369f2 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 7915 | /// GetOrEnforceKnownAlignment - If the specified pointer has an alignment that |
| 7916 | /// we can determine, return it, otherwise return 0. If PrefAlign is specified, |
| 7917 | /// and it is more than the alignment of the ultimate object, see if we can |
| 7918 | /// increase the alignment of the ultimate object, making this check succeed. |
| 7919 | static unsigned GetOrEnforceKnownAlignment(Value *V, TargetData *TD, |
| 7920 | unsigned PrefAlign = 0) { |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7921 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) { |
| 7922 | unsigned Align = GV->getAlignment(); |
Andrew Lenharth | b410df9 | 2007-11-08 18:45:15 +0000 | [diff] [blame] | 7923 | if (Align == 0 && TD && GV->getType()->getElementType()->isSized()) |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 7924 | Align = TD->getPrefTypeAlignment(GV->getType()->getElementType()); |
Chris Lattner | f2369f2 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 7925 | |
| 7926 | // If there is a large requested alignment and we can, bump up the alignment |
| 7927 | // of the global. |
| 7928 | if (PrefAlign > Align && GV->hasInitializer()) { |
| 7929 | GV->setAlignment(PrefAlign); |
| 7930 | Align = PrefAlign; |
| 7931 | } |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7932 | return Align; |
| 7933 | } else if (AllocationInst *AI = dyn_cast<AllocationInst>(V)) { |
| 7934 | unsigned Align = AI->getAlignment(); |
| 7935 | if (Align == 0 && TD) { |
| 7936 | if (isa<AllocaInst>(AI)) |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 7937 | Align = TD->getPrefTypeAlignment(AI->getType()->getElementType()); |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7938 | else if (isa<MallocInst>(AI)) { |
| 7939 | // Malloc returns maximally aligned memory. |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 7940 | Align = TD->getABITypeAlignment(AI->getType()->getElementType()); |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 7941 | Align = |
| 7942 | std::max(Align, |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 7943 | (unsigned)TD->getABITypeAlignment(Type::DoubleTy)); |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 7944 | Align = |
| 7945 | std::max(Align, |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 7946 | (unsigned)TD->getABITypeAlignment(Type::Int64Ty)); |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7947 | } |
| 7948 | } |
Chris Lattner | f2369f2 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 7949 | |
| 7950 | // If there is a requested alignment and if this is an alloca, round up. We |
| 7951 | // don't do this for malloc, because some systems can't respect the request. |
| 7952 | if (PrefAlign > Align && isa<AllocaInst>(AI)) { |
| 7953 | AI->setAlignment(PrefAlign); |
| 7954 | Align = PrefAlign; |
| 7955 | } |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7956 | return Align; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7957 | } else if (isa<BitCastInst>(V) || |
Chris Lattner | 51c26e9 | 2006-03-07 01:28:57 +0000 | [diff] [blame] | 7958 | (isa<ConstantExpr>(V) && |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7959 | cast<ConstantExpr>(V)->getOpcode() == Instruction::BitCast)) { |
Chris Lattner | f2369f2 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 7960 | return GetOrEnforceKnownAlignment(cast<User>(V)->getOperand(0), |
| 7961 | TD, PrefAlign); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7962 | } else if (User *GEPI = dyn_castGetElementPtr(V)) { |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7963 | // If all indexes are zero, it is just the alignment of the base pointer. |
| 7964 | bool AllZeroOperands = true; |
| 7965 | for (unsigned i = 1, e = GEPI->getNumOperands(); i != e; ++i) |
| 7966 | if (!isa<Constant>(GEPI->getOperand(i)) || |
| 7967 | !cast<Constant>(GEPI->getOperand(i))->isNullValue()) { |
| 7968 | AllZeroOperands = false; |
| 7969 | break; |
| 7970 | } |
Chris Lattner | f2369f2 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 7971 | |
| 7972 | if (AllZeroOperands) { |
| 7973 | // Treat this like a bitcast. |
| 7974 | return GetOrEnforceKnownAlignment(GEPI->getOperand(0), TD, PrefAlign); |
| 7975 | } |
| 7976 | |
| 7977 | unsigned BaseAlignment = GetOrEnforceKnownAlignment(GEPI->getOperand(0),TD); |
| 7978 | if (BaseAlignment == 0) return 0; |
| 7979 | |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7980 | // Otherwise, if the base alignment is >= the alignment we expect for the |
| 7981 | // base pointer type, then we know that the resultant pointer is aligned at |
| 7982 | // least as much as its type requires. |
| 7983 | if (!TD) return 0; |
| 7984 | |
| 7985 | const Type *BasePtrTy = GEPI->getOperand(0)->getType(); |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 7986 | const PointerType *PtrTy = cast<PointerType>(BasePtrTy); |
Lauro Ramos Venancio | c7d1114 | 2007-07-31 20:13:21 +0000 | [diff] [blame] | 7987 | unsigned Align = TD->getABITypeAlignment(PtrTy->getElementType()); |
| 7988 | if (Align <= BaseAlignment) { |
Chris Lattner | 51c26e9 | 2006-03-07 01:28:57 +0000 | [diff] [blame] | 7989 | const Type *GEPTy = GEPI->getType(); |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 7990 | const PointerType *GEPPtrTy = cast<PointerType>(GEPTy); |
Lauro Ramos Venancio | c7d1114 | 2007-07-31 20:13:21 +0000 | [diff] [blame] | 7991 | Align = std::min(Align, (unsigned) |
| 7992 | TD->getABITypeAlignment(GEPPtrTy->getElementType())); |
| 7993 | return Align; |
Chris Lattner | 51c26e9 | 2006-03-07 01:28:57 +0000 | [diff] [blame] | 7994 | } |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7995 | return 0; |
| 7996 | } |
| 7997 | return 0; |
| 7998 | } |
| 7999 | |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8000 | Instruction *InstCombiner::SimplifyMemTransfer(MemIntrinsic *MI) { |
| 8001 | unsigned DstAlign = GetOrEnforceKnownAlignment(MI->getOperand(1), TD); |
| 8002 | unsigned SrcAlign = GetOrEnforceKnownAlignment(MI->getOperand(2), TD); |
| 8003 | unsigned MinAlign = std::min(DstAlign, SrcAlign); |
| 8004 | unsigned CopyAlign = MI->getAlignment()->getZExtValue(); |
| 8005 | |
| 8006 | if (CopyAlign < MinAlign) { |
| 8007 | MI->setAlignment(ConstantInt::get(Type::Int32Ty, MinAlign)); |
| 8008 | return MI; |
| 8009 | } |
| 8010 | |
| 8011 | // If MemCpyInst length is 1/2/4/8 bytes then replace memcpy with |
| 8012 | // load/store. |
| 8013 | ConstantInt *MemOpLength = dyn_cast<ConstantInt>(MI->getOperand(3)); |
| 8014 | if (MemOpLength == 0) return 0; |
| 8015 | |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 8016 | // Source and destination pointer types are always "i8*" for intrinsic. See |
| 8017 | // if the size is something we can handle with a single primitive load/store. |
| 8018 | // A single load+store correctly handles overlapping memory in the memmove |
| 8019 | // case. |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8020 | unsigned Size = MemOpLength->getZExtValue(); |
| 8021 | if (Size == 0 || Size > 8 || (Size&(Size-1))) |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 8022 | return 0; // If not 1/2/4/8 bytes, exit. |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8023 | |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 8024 | // Use an integer load+store unless we can find something better. |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8025 | Type *NewPtrTy = PointerType::getUnqual(IntegerType::get(Size<<3)); |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 8026 | |
| 8027 | // Memcpy forces the use of i8* for the source and destination. That means |
| 8028 | // that if you're using memcpy to move one double around, you'll get a cast |
| 8029 | // from double* to i8*. We'd much rather use a double load+store rather than |
| 8030 | // an i64 load+store, here because this improves the odds that the source or |
| 8031 | // dest address will be promotable. See if we can find a better type than the |
| 8032 | // integer datatype. |
| 8033 | if (Value *Op = getBitCastOperand(MI->getOperand(1))) { |
| 8034 | const Type *SrcETy = cast<PointerType>(Op->getType())->getElementType(); |
| 8035 | if (SrcETy->isSized() && TD->getTypeStoreSize(SrcETy) == Size) { |
| 8036 | // The SrcETy might be something like {{{double}}} or [1 x double]. Rip |
| 8037 | // down through these levels if so. |
| 8038 | while (!SrcETy->isFirstClassType()) { |
| 8039 | if (const StructType *STy = dyn_cast<StructType>(SrcETy)) { |
| 8040 | if (STy->getNumElements() == 1) |
| 8041 | SrcETy = STy->getElementType(0); |
| 8042 | else |
| 8043 | break; |
| 8044 | } else if (const ArrayType *ATy = dyn_cast<ArrayType>(SrcETy)) { |
| 8045 | if (ATy->getNumElements() == 1) |
| 8046 | SrcETy = ATy->getElementType(); |
| 8047 | else |
| 8048 | break; |
| 8049 | } else |
| 8050 | break; |
| 8051 | } |
| 8052 | |
| 8053 | if (SrcETy->isFirstClassType()) |
| 8054 | NewPtrTy = PointerType::getUnqual(SrcETy); |
| 8055 | } |
| 8056 | } |
| 8057 | |
| 8058 | |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8059 | // If the memcpy/memmove provides better alignment info than we can |
| 8060 | // infer, use it. |
| 8061 | SrcAlign = std::max(SrcAlign, CopyAlign); |
| 8062 | DstAlign = std::max(DstAlign, CopyAlign); |
| 8063 | |
| 8064 | Value *Src = InsertBitCastBefore(MI->getOperand(2), NewPtrTy, *MI); |
| 8065 | Value *Dest = InsertBitCastBefore(MI->getOperand(1), NewPtrTy, *MI); |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 8066 | Instruction *L = new LoadInst(Src, "tmp", false, SrcAlign); |
| 8067 | InsertNewInstBefore(L, *MI); |
| 8068 | InsertNewInstBefore(new StoreInst(L, Dest, false, DstAlign), *MI); |
| 8069 | |
| 8070 | // Set the size of the copy to 0, it will be deleted on the next iteration. |
| 8071 | MI->setOperand(3, Constant::getNullValue(MemOpLength->getType())); |
| 8072 | return MI; |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8073 | } |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 8074 | |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 8075 | /// visitCallInst - CallInst simplification. This mostly only handles folding |
| 8076 | /// of intrinsic instructions. For normal calls, it allows visitCallSite to do |
| 8077 | /// the heavy lifting. |
| 8078 | /// |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8079 | Instruction *InstCombiner::visitCallInst(CallInst &CI) { |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 8080 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(&CI); |
| 8081 | if (!II) return visitCallSite(&CI); |
| 8082 | |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 8083 | // Intrinsics cannot occur in an invoke, so handle them here instead of in |
| 8084 | // visitCallSite. |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 8085 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(II)) { |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8086 | bool Changed = false; |
| 8087 | |
| 8088 | // memmove/cpy/set of zero bytes is a noop. |
| 8089 | if (Constant *NumBytes = dyn_cast<Constant>(MI->getLength())) { |
| 8090 | if (NumBytes->isNullValue()) return EraseInstFromFunction(CI); |
| 8091 | |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8092 | if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes)) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8093 | if (CI->getZExtValue() == 1) { |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8094 | // Replace the instruction with just byte operations. We would |
| 8095 | // transform other cases to loads/stores, but we don't know if |
| 8096 | // alignment is sufficient. |
| 8097 | } |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 8098 | } |
| 8099 | |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8100 | // If we have a memmove and the source operation is a constant global, |
| 8101 | // then the source and dest pointers can't alias, so we can change this |
| 8102 | // into a call to memcpy. |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8103 | if (MemMoveInst *MMI = dyn_cast<MemMoveInst>(MI)) { |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8104 | if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource())) |
| 8105 | if (GVSrc->isConstant()) { |
| 8106 | Module *M = CI.getParent()->getParent()->getParent(); |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 8107 | Intrinsic::ID MemCpyID; |
| 8108 | if (CI.getOperand(3)->getType() == Type::Int32Ty) |
| 8109 | MemCpyID = Intrinsic::memcpy_i32; |
Chris Lattner | 2195939 | 2006-03-03 01:34:17 +0000 | [diff] [blame] | 8110 | else |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 8111 | MemCpyID = Intrinsic::memcpy_i64; |
| 8112 | CI.setOperand(0, Intrinsic::getDeclaration(M, MemCpyID)); |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8113 | Changed = true; |
| 8114 | } |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8115 | } |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8116 | |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8117 | // If we can determine a pointer alignment that is bigger than currently |
| 8118 | // set, update the alignment. |
| 8119 | if (isa<MemCpyInst>(MI) || isa<MemMoveInst>(MI)) { |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8120 | if (Instruction *I = SimplifyMemTransfer(MI)) |
| 8121 | return I; |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8122 | } else if (isa<MemSetInst>(MI)) { |
Chris Lattner | f2369f2 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 8123 | unsigned Alignment = GetOrEnforceKnownAlignment(MI->getDest(), TD); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8124 | if (MI->getAlignment()->getZExtValue() < Alignment) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8125 | MI->setAlignment(ConstantInt::get(Type::Int32Ty, Alignment)); |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8126 | Changed = true; |
| 8127 | } |
| 8128 | } |
| 8129 | |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 8130 | if (Changed) return II; |
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 8131 | } else { |
| 8132 | switch (II->getIntrinsicID()) { |
| 8133 | default: break; |
Chris Lattner | 82ed58f | 2006-04-02 05:30:25 +0000 | [diff] [blame] | 8134 | case Intrinsic::ppc_altivec_lvx: |
| 8135 | case Intrinsic::ppc_altivec_lvxl: |
Chris Lattner | fd6bdf0 | 2006-04-17 22:26:56 +0000 | [diff] [blame] | 8136 | case Intrinsic::x86_sse_loadu_ps: |
| 8137 | case Intrinsic::x86_sse2_loadu_pd: |
| 8138 | case Intrinsic::x86_sse2_loadu_dq: |
| 8139 | // Turn PPC lvx -> load if the pointer is known aligned. |
| 8140 | // Turn X86 loadups -> load if the pointer is known aligned. |
Chris Lattner | f2369f2 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 8141 | if (GetOrEnforceKnownAlignment(II->getOperand(1), TD, 16) >= 16) { |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 8142 | Value *Ptr = InsertBitCastBefore(II->getOperand(1), |
| 8143 | PointerType::getUnqual(II->getType()), |
| 8144 | CI); |
Chris Lattner | 82ed58f | 2006-04-02 05:30:25 +0000 | [diff] [blame] | 8145 | return new LoadInst(Ptr); |
| 8146 | } |
| 8147 | break; |
| 8148 | case Intrinsic::ppc_altivec_stvx: |
| 8149 | case Intrinsic::ppc_altivec_stvxl: |
| 8150 | // Turn stvx -> store if the pointer is known aligned. |
Chris Lattner | f2369f2 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 8151 | if (GetOrEnforceKnownAlignment(II->getOperand(2), TD, 16) >= 16) { |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 8152 | const Type *OpPtrTy = |
| 8153 | PointerType::getUnqual(II->getOperand(1)->getType()); |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 8154 | Value *Ptr = InsertBitCastBefore(II->getOperand(2), OpPtrTy, CI); |
Chris Lattner | 82ed58f | 2006-04-02 05:30:25 +0000 | [diff] [blame] | 8155 | return new StoreInst(II->getOperand(1), Ptr); |
| 8156 | } |
| 8157 | break; |
Chris Lattner | fd6bdf0 | 2006-04-17 22:26:56 +0000 | [diff] [blame] | 8158 | case Intrinsic::x86_sse_storeu_ps: |
| 8159 | case Intrinsic::x86_sse2_storeu_pd: |
| 8160 | case Intrinsic::x86_sse2_storeu_dq: |
| 8161 | case Intrinsic::x86_sse2_storel_dq: |
| 8162 | // Turn X86 storeu -> store if the pointer is known aligned. |
Chris Lattner | f2369f2 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 8163 | if (GetOrEnforceKnownAlignment(II->getOperand(1), TD, 16) >= 16) { |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 8164 | const Type *OpPtrTy = |
| 8165 | PointerType::getUnqual(II->getOperand(2)->getType()); |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 8166 | Value *Ptr = InsertBitCastBefore(II->getOperand(1), OpPtrTy, CI); |
Chris Lattner | fd6bdf0 | 2006-04-17 22:26:56 +0000 | [diff] [blame] | 8167 | return new StoreInst(II->getOperand(2), Ptr); |
| 8168 | } |
| 8169 | break; |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 8170 | |
| 8171 | case Intrinsic::x86_sse_cvttss2si: { |
| 8172 | // These intrinsics only demands the 0th element of its input vector. If |
| 8173 | // we can simplify the input based on that, do so now. |
| 8174 | uint64_t UndefElts; |
| 8175 | if (Value *V = SimplifyDemandedVectorElts(II->getOperand(1), 1, |
| 8176 | UndefElts)) { |
| 8177 | II->setOperand(1, V); |
| 8178 | return II; |
| 8179 | } |
| 8180 | break; |
| 8181 | } |
| 8182 | |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8183 | case Intrinsic::ppc_altivec_vperm: |
| 8184 | // 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] | 8185 | if (ConstantVector *Mask = dyn_cast<ConstantVector>(II->getOperand(3))) { |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8186 | assert(Mask->getNumOperands() == 16 && "Bad type for intrinsic!"); |
| 8187 | |
| 8188 | // Check that all of the elements are integer constants or undefs. |
| 8189 | bool AllEltsOk = true; |
| 8190 | for (unsigned i = 0; i != 16; ++i) { |
| 8191 | if (!isa<ConstantInt>(Mask->getOperand(i)) && |
| 8192 | !isa<UndefValue>(Mask->getOperand(i))) { |
| 8193 | AllEltsOk = false; |
| 8194 | break; |
| 8195 | } |
| 8196 | } |
| 8197 | |
| 8198 | if (AllEltsOk) { |
| 8199 | // Cast the input vectors to byte vectors. |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 8200 | Value *Op0 =InsertBitCastBefore(II->getOperand(1),Mask->getType(),CI); |
| 8201 | Value *Op1 =InsertBitCastBefore(II->getOperand(2),Mask->getType(),CI); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8202 | Value *Result = UndefValue::get(Op0->getType()); |
| 8203 | |
| 8204 | // Only extract each element once. |
| 8205 | Value *ExtractedElts[32]; |
| 8206 | memset(ExtractedElts, 0, sizeof(ExtractedElts)); |
| 8207 | |
| 8208 | for (unsigned i = 0; i != 16; ++i) { |
| 8209 | if (isa<UndefValue>(Mask->getOperand(i))) |
| 8210 | continue; |
Chris Lattner | e34e9a2 | 2007-04-14 23:32:02 +0000 | [diff] [blame] | 8211 | unsigned Idx=cast<ConstantInt>(Mask->getOperand(i))->getZExtValue(); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8212 | Idx &= 31; // Match the hardware behavior. |
| 8213 | |
| 8214 | if (ExtractedElts[Idx] == 0) { |
| 8215 | Instruction *Elt = |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 8216 | new ExtractElementInst(Idx < 16 ? Op0 : Op1, Idx&15, "tmp"); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8217 | InsertNewInstBefore(Elt, CI); |
| 8218 | ExtractedElts[Idx] = Elt; |
| 8219 | } |
| 8220 | |
| 8221 | // Insert this value into the result vector. |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 8222 | Result = new InsertElementInst(Result, ExtractedElts[Idx], i,"tmp"); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8223 | InsertNewInstBefore(cast<Instruction>(Result), CI); |
| 8224 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8225 | return CastInst::create(Instruction::BitCast, Result, CI.getType()); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8226 | } |
| 8227 | } |
| 8228 | break; |
| 8229 | |
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 8230 | case Intrinsic::stackrestore: { |
| 8231 | // If the save is right next to the restore, remove the restore. This can |
| 8232 | // happen when variable allocas are DCE'd. |
| 8233 | if (IntrinsicInst *SS = dyn_cast<IntrinsicInst>(II->getOperand(1))) { |
| 8234 | if (SS->getIntrinsicID() == Intrinsic::stacksave) { |
| 8235 | BasicBlock::iterator BI = SS; |
| 8236 | if (&*++BI == II) |
| 8237 | return EraseInstFromFunction(CI); |
| 8238 | } |
| 8239 | } |
| 8240 | |
Chris Lattner | bf1d8a7 | 2008-02-18 06:12:38 +0000 | [diff] [blame] | 8241 | // Scan down this block to see if there is another stack restore in the |
| 8242 | // same block without an intervening call/alloca. |
| 8243 | BasicBlock::iterator BI = II; |
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 8244 | TerminatorInst *TI = II->getParent()->getTerminator(); |
Chris Lattner | bf1d8a7 | 2008-02-18 06:12:38 +0000 | [diff] [blame] | 8245 | bool CannotRemove = false; |
| 8246 | for (++BI; &*BI != TI; ++BI) { |
| 8247 | if (isa<AllocaInst>(BI)) { |
| 8248 | CannotRemove = true; |
| 8249 | break; |
| 8250 | } |
| 8251 | if (isa<CallInst>(BI)) { |
| 8252 | if (!isa<IntrinsicInst>(BI)) { |
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 8253 | CannotRemove = true; |
| 8254 | break; |
| 8255 | } |
Chris Lattner | bf1d8a7 | 2008-02-18 06:12:38 +0000 | [diff] [blame] | 8256 | // If there is a stackrestore below this one, remove this one. |
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 8257 | return EraseInstFromFunction(CI); |
Chris Lattner | bf1d8a7 | 2008-02-18 06:12:38 +0000 | [diff] [blame] | 8258 | } |
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 8259 | } |
Chris Lattner | bf1d8a7 | 2008-02-18 06:12:38 +0000 | [diff] [blame] | 8260 | |
| 8261 | // If the stack restore is in a return/unwind block and if there are no |
| 8262 | // allocas or calls between the restore and the return, nuke the restore. |
| 8263 | if (!CannotRemove && (isa<ReturnInst>(TI) || isa<UnwindInst>(TI))) |
| 8264 | return EraseInstFromFunction(CI); |
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 8265 | break; |
| 8266 | } |
| 8267 | } |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8268 | } |
| 8269 | |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 8270 | return visitCallSite(II); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8271 | } |
| 8272 | |
| 8273 | // InvokeInst simplification |
| 8274 | // |
| 8275 | Instruction *InstCombiner::visitInvokeInst(InvokeInst &II) { |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 8276 | return visitCallSite(&II); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8277 | } |
| 8278 | |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 8279 | // visitCallSite - Improvements for call and invoke instructions. |
| 8280 | // |
| 8281 | Instruction *InstCombiner::visitCallSite(CallSite CS) { |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8282 | bool Changed = false; |
| 8283 | |
| 8284 | // If the callee is a constexpr cast of a function, attempt to move the cast |
| 8285 | // to the arguments of the call/invoke. |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 8286 | if (transformConstExprCastCall(CS)) return 0; |
| 8287 | |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8288 | Value *Callee = CS.getCalledValue(); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8289 | |
Chris Lattner | 08b22ec | 2005-05-13 07:09:09 +0000 | [diff] [blame] | 8290 | if (Function *CalleeF = dyn_cast<Function>(Callee)) |
| 8291 | if (CalleeF->getCallingConv() != CS.getCallingConv()) { |
| 8292 | Instruction *OldCall = CS.getInstruction(); |
| 8293 | // If the call and callee calling conventions don't match, this call must |
| 8294 | // be unreachable, as the call is undefined. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 8295 | new StoreInst(ConstantInt::getTrue(), |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 8296 | UndefValue::get(PointerType::getUnqual(Type::Int1Ty)), |
| 8297 | OldCall); |
Chris Lattner | 08b22ec | 2005-05-13 07:09:09 +0000 | [diff] [blame] | 8298 | if (!OldCall->use_empty()) |
| 8299 | OldCall->replaceAllUsesWith(UndefValue::get(OldCall->getType())); |
| 8300 | if (isa<CallInst>(OldCall)) // Not worth removing an invoke here. |
| 8301 | return EraseInstFromFunction(*OldCall); |
| 8302 | return 0; |
| 8303 | } |
| 8304 | |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8305 | if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) { |
| 8306 | // This instruction is not reachable, just remove it. We insert a store to |
| 8307 | // undef so that we know that this code is not reachable, despite the fact |
| 8308 | // that we can't modify the CFG here. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 8309 | new StoreInst(ConstantInt::getTrue(), |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 8310 | UndefValue::get(PointerType::getUnqual(Type::Int1Ty)), |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8311 | CS.getInstruction()); |
| 8312 | |
| 8313 | if (!CS.getInstruction()->use_empty()) |
| 8314 | CS.getInstruction()-> |
| 8315 | replaceAllUsesWith(UndefValue::get(CS.getInstruction()->getType())); |
| 8316 | |
| 8317 | if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) { |
| 8318 | // Don't break the CFG, insert a dummy cond branch. |
| 8319 | new BranchInst(II->getNormalDest(), II->getUnwindDest(), |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 8320 | ConstantInt::getTrue(), II); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8321 | } |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8322 | return EraseInstFromFunction(*CS.getInstruction()); |
| 8323 | } |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8324 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8325 | if (BitCastInst *BC = dyn_cast<BitCastInst>(Callee)) |
| 8326 | if (IntrinsicInst *In = dyn_cast<IntrinsicInst>(BC->getOperand(0))) |
| 8327 | if (In->getIntrinsicID() == Intrinsic::init_trampoline) |
| 8328 | return transformCallThroughTrampoline(CS); |
| 8329 | |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8330 | const PointerType *PTy = cast<PointerType>(Callee->getType()); |
| 8331 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
| 8332 | if (FTy->isVarArg()) { |
| 8333 | // See if we can optimize any arguments passed through the varargs area of |
| 8334 | // the call. |
| 8335 | for (CallSite::arg_iterator I = CS.arg_begin()+FTy->getNumParams(), |
| 8336 | E = CS.arg_end(); I != E; ++I) |
| 8337 | if (CastInst *CI = dyn_cast<CastInst>(*I)) { |
| 8338 | // If this cast does not effect the value passed through the varargs |
| 8339 | // area, we can eliminate the use of the cast. |
| 8340 | Value *Op = CI->getOperand(0); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8341 | if (CI->isLosslessCast()) { |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8342 | *I = Op; |
| 8343 | Changed = true; |
| 8344 | } |
| 8345 | } |
| 8346 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8347 | |
Duncan Sands | f0c3354 | 2007-12-19 21:13:37 +0000 | [diff] [blame] | 8348 | if (isa<InlineAsm>(Callee) && !CS.doesNotThrow()) { |
Duncan Sands | ece2c04 | 2007-12-16 15:51:49 +0000 | [diff] [blame] | 8349 | // Inline asm calls cannot throw - mark them 'nounwind'. |
Duncan Sands | f0c3354 | 2007-12-19 21:13:37 +0000 | [diff] [blame] | 8350 | CS.setDoesNotThrow(); |
Duncan Sands | ece2c04 | 2007-12-16 15:51:49 +0000 | [diff] [blame] | 8351 | Changed = true; |
| 8352 | } |
| 8353 | |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8354 | return Changed ? CS.getInstruction() : 0; |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 8355 | } |
| 8356 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8357 | // transformConstExprCastCall - If the callee is a constexpr cast of a function, |
| 8358 | // attempt to move the cast to the arguments of the call/invoke. |
| 8359 | // |
| 8360 | bool InstCombiner::transformConstExprCastCall(CallSite CS) { |
| 8361 | if (!isa<ConstantExpr>(CS.getCalledValue())) return false; |
| 8362 | ConstantExpr *CE = cast<ConstantExpr>(CS.getCalledValue()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8363 | if (CE->getOpcode() != Instruction::BitCast || |
| 8364 | !isa<Function>(CE->getOperand(0))) |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8365 | return false; |
Reid Spencer | 8863f18 | 2004-07-18 00:38:32 +0000 | [diff] [blame] | 8366 | Function *Callee = cast<Function>(CE->getOperand(0)); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8367 | Instruction *Caller = CS.getInstruction(); |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8368 | const ParamAttrsList* CallerPAL = CS.getParamAttrs(); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8369 | |
| 8370 | // Okay, this is a cast from a function to a different type. Unless doing so |
| 8371 | // would cause a type conversion of one of our arguments, change this call to |
| 8372 | // be a direct call with arguments casted to the appropriate types. |
| 8373 | // |
| 8374 | const FunctionType *FT = Callee->getFunctionType(); |
| 8375 | const Type *OldRetTy = Caller->getType(); |
| 8376 | |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 8377 | // Check to see if we are changing the return type... |
| 8378 | if (OldRetTy != FT->getReturnType()) { |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 8379 | if (Callee->isDeclaration() && !Caller->use_empty() && |
Chris Lattner | 46013f4 | 2007-01-06 19:53:32 +0000 | [diff] [blame] | 8380 | // Conversion is ok if changing from pointer to int of same size. |
| 8381 | !(isa<PointerType>(FT->getReturnType()) && |
| 8382 | TD->getIntPtrType() == OldRetTy)) |
Chris Lattner | ec47992 | 2007-01-06 02:09:32 +0000 | [diff] [blame] | 8383 | return false; // Cannot transform this return value. |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 8384 | |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8385 | if (!Caller->use_empty() && |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8386 | // void -> non-void is handled specially |
Duncan Sands | e1e520f | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 8387 | FT->getReturnType() != Type::VoidTy && |
| 8388 | !CastInst::isCastable(FT->getReturnType(), OldRetTy)) |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8389 | return false; // Cannot transform this return value. |
| 8390 | |
Duncan Sands | 6c3470e | 2008-01-07 17:16:06 +0000 | [diff] [blame] | 8391 | if (CallerPAL && !Caller->use_empty()) { |
Dale Johannesen | 0d51e7e | 2008-02-19 21:38:47 +0000 | [diff] [blame] | 8392 | ParameterAttributes RAttrs = CallerPAL->getParamAttrs(0); |
Duncan Sands | 6c3470e | 2008-01-07 17:16:06 +0000 | [diff] [blame] | 8393 | if (RAttrs & ParamAttr::typeIncompatible(FT->getReturnType())) |
| 8394 | return false; // Attribute not compatible with transformed value. |
| 8395 | } |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8396 | |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 8397 | // If the callsite is an invoke instruction, and the return value is used by |
| 8398 | // a PHI node in a successor, we cannot change the return type of the call |
| 8399 | // because there is no place to put the cast instruction (without breaking |
| 8400 | // the critical edge). Bail out in this case. |
| 8401 | if (!Caller->use_empty()) |
| 8402 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) |
| 8403 | for (Value::use_iterator UI = II->use_begin(), E = II->use_end(); |
| 8404 | UI != E; ++UI) |
| 8405 | if (PHINode *PN = dyn_cast<PHINode>(*UI)) |
| 8406 | if (PN->getParent() == II->getNormalDest() || |
Chris Lattner | aeb2a1d | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 8407 | PN->getParent() == II->getUnwindDest()) |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 8408 | return false; |
| 8409 | } |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8410 | |
| 8411 | unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin()); |
| 8412 | unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8413 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8414 | CallSite::arg_iterator AI = CS.arg_begin(); |
| 8415 | for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) { |
| 8416 | const Type *ParamTy = FT->getParamType(i); |
Andrew Lenharth | b8e604c | 2006-06-28 01:01:52 +0000 | [diff] [blame] | 8417 | const Type *ActTy = (*AI)->getType(); |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8418 | |
| 8419 | if (!CastInst::isCastable(ActTy, ParamTy)) |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8420 | return false; // Cannot transform this parameter value. |
| 8421 | |
Duncan Sands | 6c3470e | 2008-01-07 17:16:06 +0000 | [diff] [blame] | 8422 | if (CallerPAL) { |
Dale Johannesen | 0d51e7e | 2008-02-19 21:38:47 +0000 | [diff] [blame] | 8423 | ParameterAttributes PAttrs = CallerPAL->getParamAttrs(i + 1); |
Duncan Sands | 6c3470e | 2008-01-07 17:16:06 +0000 | [diff] [blame] | 8424 | if (PAttrs & ParamAttr::typeIncompatible(ParamTy)) |
| 8425 | return false; // Attribute not compatible with transformed value. |
| 8426 | } |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8427 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8428 | ConstantInt *c = dyn_cast<ConstantInt>(*AI); |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8429 | // Some conversions are safe even if we do not have a body. |
| 8430 | // Either we can cast directly, or we can upconvert the argument |
Chris Lattner | ec47992 | 2007-01-06 02:09:32 +0000 | [diff] [blame] | 8431 | bool isConvertible = ActTy == ParamTy || |
Chris Lattner | 46013f4 | 2007-01-06 19:53:32 +0000 | [diff] [blame] | 8432 | (isa<PointerType>(ParamTy) && isa<PointerType>(ActTy)) || |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 8433 | (ParamTy->isInteger() && ActTy->isInteger() && |
Reid Spencer | abaa8ca | 2007-01-08 16:32:00 +0000 | [diff] [blame] | 8434 | ParamTy->getPrimitiveSizeInBits() >= ActTy->getPrimitiveSizeInBits()) || |
| 8435 | (c && ParamTy->getPrimitiveSizeInBits() >= ActTy->getPrimitiveSizeInBits() |
Zhou Sheng | 0fc5095 | 2007-03-25 05:01:29 +0000 | [diff] [blame] | 8436 | && c->getValue().isStrictlyPositive()); |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 8437 | if (Callee->isDeclaration() && !isConvertible) return false; |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8438 | } |
| 8439 | |
| 8440 | if (FT->getNumParams() < NumActualArgs && !FT->isVarArg() && |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 8441 | Callee->isDeclaration()) |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8442 | return false; // Do not delete arguments unless we have a function body... |
| 8443 | |
Duncan Sands | e1e520f | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 8444 | if (FT->getNumParams() < NumActualArgs && FT->isVarArg() && CallerPAL) |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8445 | // 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] | 8446 | // won't be dropping them. Check that these extra arguments have attributes |
| 8447 | // that are compatible with being a vararg call argument. |
| 8448 | for (unsigned i = CallerPAL->size(); i; --i) { |
| 8449 | if (CallerPAL->getParamIndex(i - 1) <= FT->getNumParams()) |
| 8450 | break; |
Dale Johannesen | 0d51e7e | 2008-02-19 21:38:47 +0000 | [diff] [blame] | 8451 | ParameterAttributes PAttrs = CallerPAL->getParamAttrsAtIndex(i - 1); |
Duncan Sands | e1e520f | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 8452 | if (PAttrs & ParamAttr::VarArgsIncompatible) |
| 8453 | return false; |
| 8454 | } |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8455 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8456 | // Okay, we decided that this is a safe thing to do: go ahead and start |
| 8457 | // inserting cast instructions as necessary... |
| 8458 | std::vector<Value*> Args; |
| 8459 | Args.reserve(NumActualArgs); |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8460 | ParamAttrsVector attrVec; |
| 8461 | attrVec.reserve(NumCommonArgs); |
| 8462 | |
| 8463 | // Get any return attributes. |
Dale Johannesen | 0d51e7e | 2008-02-19 21:38:47 +0000 | [diff] [blame] | 8464 | ParameterAttributes RAttrs = CallerPAL ? CallerPAL->getParamAttrs(0) : |
| 8465 | ParamAttr::None; |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8466 | |
| 8467 | // If the return value is not being used, the type may not be compatible |
| 8468 | // with the existing attributes. Wipe out any problematic attributes. |
Duncan Sands | 6c3470e | 2008-01-07 17:16:06 +0000 | [diff] [blame] | 8469 | RAttrs &= ~ParamAttr::typeIncompatible(FT->getReturnType()); |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8470 | |
| 8471 | // Add the new return attributes. |
| 8472 | if (RAttrs) |
| 8473 | attrVec.push_back(ParamAttrsWithIndex::get(0, RAttrs)); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8474 | |
| 8475 | AI = CS.arg_begin(); |
| 8476 | for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) { |
| 8477 | const Type *ParamTy = FT->getParamType(i); |
| 8478 | if ((*AI)->getType() == ParamTy) { |
| 8479 | Args.push_back(*AI); |
| 8480 | } else { |
Reid Spencer | 8a903db | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 8481 | Instruction::CastOps opcode = CastInst::getCastOpcode(*AI, |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8482 | false, ParamTy, false); |
Reid Spencer | 8a903db | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 8483 | CastInst *NewCast = CastInst::create(opcode, *AI, ParamTy, "tmp"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8484 | Args.push_back(InsertNewInstBefore(NewCast, *Caller)); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8485 | } |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8486 | |
| 8487 | // Add any parameter attributes. |
Dale Johannesen | 0d51e7e | 2008-02-19 21:38:47 +0000 | [diff] [blame] | 8488 | ParameterAttributes PAttrs = CallerPAL ? CallerPAL->getParamAttrs(i + 1) : |
| 8489 | ParamAttr::None; |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8490 | if (PAttrs) |
| 8491 | attrVec.push_back(ParamAttrsWithIndex::get(i + 1, PAttrs)); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8492 | } |
| 8493 | |
| 8494 | // If the function takes more arguments than the call was taking, add them |
| 8495 | // now... |
| 8496 | for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i) |
| 8497 | Args.push_back(Constant::getNullValue(FT->getParamType(i))); |
| 8498 | |
| 8499 | // If we are removing arguments to the function, emit an obnoxious warning... |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 8500 | if (FT->getNumParams() < NumActualArgs) { |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8501 | if (!FT->isVarArg()) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 8502 | cerr << "WARNING: While resolving call to function '" |
| 8503 | << Callee->getName() << "' arguments were dropped!\n"; |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8504 | } else { |
| 8505 | // Add all of the arguments in their promoted form to the arg list... |
| 8506 | for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) { |
| 8507 | const Type *PTy = getPromotedType((*AI)->getType()); |
| 8508 | if (PTy != (*AI)->getType()) { |
| 8509 | // Must promote to pass through va_arg area! |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8510 | Instruction::CastOps opcode = CastInst::getCastOpcode(*AI, false, |
| 8511 | PTy, false); |
Reid Spencer | 8a903db | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 8512 | Instruction *Cast = CastInst::create(opcode, *AI, PTy, "tmp"); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8513 | InsertNewInstBefore(Cast, *Caller); |
| 8514 | Args.push_back(Cast); |
| 8515 | } else { |
| 8516 | Args.push_back(*AI); |
| 8517 | } |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8518 | |
Duncan Sands | e1e520f | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 8519 | // Add any parameter attributes. |
Dale Johannesen | 0d51e7e | 2008-02-19 21:38:47 +0000 | [diff] [blame] | 8520 | ParameterAttributes PAttrs = CallerPAL ? |
| 8521 | CallerPAL->getParamAttrs(i + 1) : |
| 8522 | ParamAttr::None; |
Duncan Sands | e1e520f | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 8523 | if (PAttrs) |
| 8524 | attrVec.push_back(ParamAttrsWithIndex::get(i + 1, PAttrs)); |
| 8525 | } |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8526 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 8527 | } |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8528 | |
| 8529 | if (FT->getReturnType() == Type::VoidTy) |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 8530 | Caller->setName(""); // Void type should not have a name. |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8531 | |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8532 | const ParamAttrsList* NewCallerPAL = ParamAttrsList::get(attrVec); |
| 8533 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8534 | Instruction *NC; |
| 8535 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
Chris Lattner | aeb2a1d | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 8536 | NC = new InvokeInst(Callee, II->getNormalDest(), II->getUnwindDest(), |
David Greene | f1355a5 | 2007-08-27 19:04:21 +0000 | [diff] [blame] | 8537 | Args.begin(), Args.end(), Caller->getName(), Caller); |
Reid Spencer | ed3fa85 | 2007-07-30 19:53:57 +0000 | [diff] [blame] | 8538 | cast<InvokeInst>(NC)->setCallingConv(II->getCallingConv()); |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8539 | cast<InvokeInst>(NC)->setParamAttrs(NewCallerPAL); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8540 | } else { |
Chris Lattner | 684b22d | 2007-08-02 16:53:43 +0000 | [diff] [blame] | 8541 | NC = new CallInst(Callee, Args.begin(), Args.end(), |
| 8542 | Caller->getName(), Caller); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 8543 | CallInst *CI = cast<CallInst>(Caller); |
| 8544 | if (CI->isTailCall()) |
Chris Lattner | a9e9211 | 2005-05-06 06:48:21 +0000 | [diff] [blame] | 8545 | cast<CallInst>(NC)->setTailCall(); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 8546 | cast<CallInst>(NC)->setCallingConv(CI->getCallingConv()); |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8547 | cast<CallInst>(NC)->setParamAttrs(NewCallerPAL); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8548 | } |
| 8549 | |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 8550 | // Insert a cast of the return type as necessary. |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8551 | Value *NV = NC; |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8552 | if (OldRetTy != NV->getType() && !Caller->use_empty()) { |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8553 | if (NV->getType() != Type::VoidTy) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8554 | Instruction::CastOps opcode = CastInst::getCastOpcode(NC, false, |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8555 | OldRetTy, false); |
| 8556 | NV = NC = CastInst::create(opcode, NC, OldRetTy, "tmp"); |
Chris Lattner | bb60904 | 2003-10-30 00:46:41 +0000 | [diff] [blame] | 8557 | |
| 8558 | // If this is an invoke instruction, we should insert it after the first |
| 8559 | // non-phi, instruction in the normal successor block. |
| 8560 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
| 8561 | BasicBlock::iterator I = II->getNormalDest()->begin(); |
| 8562 | while (isa<PHINode>(I)) ++I; |
| 8563 | InsertNewInstBefore(NC, *I); |
| 8564 | } else { |
| 8565 | // Otherwise, it's a call, just insert cast right after the call instr |
| 8566 | InsertNewInstBefore(NC, *Caller); |
| 8567 | } |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 8568 | AddUsersToWorkList(*Caller); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8569 | } else { |
Chris Lattner | c30bda7 | 2004-10-17 21:22:38 +0000 | [diff] [blame] | 8570 | NV = UndefValue::get(Caller->getType()); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8571 | } |
| 8572 | } |
| 8573 | |
| 8574 | if (Caller->getType() != Type::VoidTy && !Caller->use_empty()) |
| 8575 | Caller->replaceAllUsesWith(NV); |
Chris Lattner | f22a5c6 | 2007-03-02 19:59:19 +0000 | [diff] [blame] | 8576 | Caller->eraseFromParent(); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 8577 | RemoveFromWorkList(Caller); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8578 | return true; |
| 8579 | } |
| 8580 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8581 | // transformCallThroughTrampoline - Turn a call to a function created by the |
| 8582 | // init_trampoline intrinsic into a direct call to the underlying function. |
| 8583 | // |
| 8584 | Instruction *InstCombiner::transformCallThroughTrampoline(CallSite CS) { |
| 8585 | Value *Callee = CS.getCalledValue(); |
| 8586 | const PointerType *PTy = cast<PointerType>(Callee->getType()); |
| 8587 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 8588 | const ParamAttrsList *Attrs = CS.getParamAttrs(); |
| 8589 | |
| 8590 | // If the call already has the 'nest' attribute somewhere then give up - |
| 8591 | // otherwise 'nest' would occur twice after splicing in the chain. |
| 8592 | if (Attrs && Attrs->hasAttrSomewhere(ParamAttr::Nest)) |
| 8593 | return 0; |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8594 | |
| 8595 | IntrinsicInst *Tramp = |
| 8596 | cast<IntrinsicInst>(cast<BitCastInst>(Callee)->getOperand(0)); |
| 8597 | |
| 8598 | Function *NestF = |
| 8599 | cast<Function>(IntrinsicInst::StripPointerCasts(Tramp->getOperand(2))); |
| 8600 | const PointerType *NestFPTy = cast<PointerType>(NestF->getType()); |
| 8601 | const FunctionType *NestFTy = cast<FunctionType>(NestFPTy->getElementType()); |
| 8602 | |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 8603 | if (const ParamAttrsList *NestAttrs = NestF->getParamAttrs()) { |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8604 | unsigned NestIdx = 1; |
| 8605 | const Type *NestTy = 0; |
Dale Johannesen | 0d51e7e | 2008-02-19 21:38:47 +0000 | [diff] [blame] | 8606 | ParameterAttributes NestAttr = ParamAttr::None; |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8607 | |
| 8608 | // Look for a parameter marked with the 'nest' attribute. |
| 8609 | for (FunctionType::param_iterator I = NestFTy->param_begin(), |
| 8610 | E = NestFTy->param_end(); I != E; ++NestIdx, ++I) |
| 8611 | if (NestAttrs->paramHasAttr(NestIdx, ParamAttr::Nest)) { |
| 8612 | // Record the parameter type and any other attributes. |
| 8613 | NestTy = *I; |
| 8614 | NestAttr = NestAttrs->getParamAttrs(NestIdx); |
| 8615 | break; |
| 8616 | } |
| 8617 | |
| 8618 | if (NestTy) { |
| 8619 | Instruction *Caller = CS.getInstruction(); |
| 8620 | std::vector<Value*> NewArgs; |
| 8621 | NewArgs.reserve(unsigned(CS.arg_end()-CS.arg_begin())+1); |
| 8622 | |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 8623 | ParamAttrsVector NewAttrs; |
| 8624 | NewAttrs.reserve(Attrs ? Attrs->size() + 1 : 1); |
| 8625 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8626 | // Insert the nest argument into the call argument list, which may |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 8627 | // mean appending it. Likewise for attributes. |
| 8628 | |
| 8629 | // Add any function result attributes. |
Dale Johannesen | 0d51e7e | 2008-02-19 21:38:47 +0000 | [diff] [blame] | 8630 | ParameterAttributes Attr = Attrs ? Attrs->getParamAttrs(0) : |
| 8631 | ParamAttr::None; |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 8632 | if (Attr) |
| 8633 | NewAttrs.push_back (ParamAttrsWithIndex::get(0, Attr)); |
| 8634 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8635 | { |
| 8636 | unsigned Idx = 1; |
| 8637 | CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); |
| 8638 | do { |
| 8639 | if (Idx == NestIdx) { |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 8640 | // Add the chain argument and attributes. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8641 | Value *NestVal = Tramp->getOperand(3); |
| 8642 | if (NestVal->getType() != NestTy) |
| 8643 | NestVal = new BitCastInst(NestVal, NestTy, "nest", Caller); |
| 8644 | NewArgs.push_back(NestVal); |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 8645 | NewAttrs.push_back(ParamAttrsWithIndex::get(NestIdx, NestAttr)); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8646 | } |
| 8647 | |
| 8648 | if (I == E) |
| 8649 | break; |
| 8650 | |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 8651 | // Add the original argument and attributes. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8652 | NewArgs.push_back(*I); |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 8653 | Attr = Attrs ? Attrs->getParamAttrs(Idx) : 0; |
| 8654 | if (Attr) |
| 8655 | NewAttrs.push_back |
| 8656 | (ParamAttrsWithIndex::get(Idx + (Idx >= NestIdx), Attr)); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8657 | |
| 8658 | ++Idx, ++I; |
| 8659 | } while (1); |
| 8660 | } |
| 8661 | |
| 8662 | // The trampoline may have been bitcast to a bogus type (FTy). |
| 8663 | // Handle this by synthesizing a new function type, equal to FTy |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 8664 | // with the chain parameter inserted. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8665 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8666 | std::vector<const Type*> NewTypes; |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8667 | NewTypes.reserve(FTy->getNumParams()+1); |
| 8668 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8669 | // 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] | 8670 | // mean appending it. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8671 | { |
| 8672 | unsigned Idx = 1; |
| 8673 | FunctionType::param_iterator I = FTy->param_begin(), |
| 8674 | E = FTy->param_end(); |
| 8675 | |
| 8676 | do { |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 8677 | if (Idx == NestIdx) |
| 8678 | // Add the chain's type. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8679 | NewTypes.push_back(NestTy); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8680 | |
| 8681 | if (I == E) |
| 8682 | break; |
| 8683 | |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 8684 | // Add the original type. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8685 | NewTypes.push_back(*I); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8686 | |
| 8687 | ++Idx, ++I; |
| 8688 | } while (1); |
| 8689 | } |
| 8690 | |
| 8691 | // Replace the trampoline call with a direct call. Let the generic |
| 8692 | // code sort out any function type mismatches. |
| 8693 | FunctionType *NewFTy = |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 8694 | FunctionType::get(FTy->getReturnType(), NewTypes, FTy->isVarArg()); |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 8695 | Constant *NewCallee = NestF->getType() == PointerType::getUnqual(NewFTy) ? |
| 8696 | NestF : ConstantExpr::getBitCast(NestF, PointerType::getUnqual(NewFTy)); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 8697 | const ParamAttrsList *NewPAL = ParamAttrsList::get(NewAttrs); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8698 | |
| 8699 | Instruction *NewCaller; |
| 8700 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
| 8701 | NewCaller = new InvokeInst(NewCallee, |
| 8702 | II->getNormalDest(), II->getUnwindDest(), |
| 8703 | NewArgs.begin(), NewArgs.end(), |
| 8704 | Caller->getName(), Caller); |
| 8705 | cast<InvokeInst>(NewCaller)->setCallingConv(II->getCallingConv()); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 8706 | cast<InvokeInst>(NewCaller)->setParamAttrs(NewPAL); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8707 | } else { |
| 8708 | NewCaller = new CallInst(NewCallee, NewArgs.begin(), NewArgs.end(), |
| 8709 | Caller->getName(), Caller); |
| 8710 | if (cast<CallInst>(Caller)->isTailCall()) |
| 8711 | cast<CallInst>(NewCaller)->setTailCall(); |
| 8712 | cast<CallInst>(NewCaller)-> |
| 8713 | setCallingConv(cast<CallInst>(Caller)->getCallingConv()); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 8714 | cast<CallInst>(NewCaller)->setParamAttrs(NewPAL); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8715 | } |
| 8716 | if (Caller->getType() != Type::VoidTy && !Caller->use_empty()) |
| 8717 | Caller->replaceAllUsesWith(NewCaller); |
| 8718 | Caller->eraseFromParent(); |
| 8719 | RemoveFromWorkList(Caller); |
| 8720 | return 0; |
| 8721 | } |
| 8722 | } |
| 8723 | |
| 8724 | // Replace the trampoline call with a direct call. Since there is no 'nest' |
| 8725 | // parameter, there is no need to adjust the argument list. Let the generic |
| 8726 | // code sort out any function type mismatches. |
| 8727 | Constant *NewCallee = |
| 8728 | NestF->getType() == PTy ? NestF : ConstantExpr::getBitCast(NestF, PTy); |
| 8729 | CS.setCalledFunction(NewCallee); |
| 8730 | return CS.getInstruction(); |
| 8731 | } |
| 8732 | |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 8733 | /// FoldPHIArgBinOpIntoPHI - If we have something like phi [add (a,b), add(c,d)] |
| 8734 | /// and if a/b/c/d and the add's all have a single use, turn this into two phi's |
| 8735 | /// and a single binop. |
| 8736 | Instruction *InstCombiner::FoldPHIArgBinOpIntoPHI(PHINode &PN) { |
| 8737 | Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0)); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 8738 | assert(isa<BinaryOperator>(FirstInst) || isa<GetElementPtrInst>(FirstInst) || |
| 8739 | isa<CmpInst>(FirstInst)); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 8740 | unsigned Opc = FirstInst->getOpcode(); |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 8741 | Value *LHSVal = FirstInst->getOperand(0); |
| 8742 | Value *RHSVal = FirstInst->getOperand(1); |
| 8743 | |
| 8744 | const Type *LHSType = LHSVal->getType(); |
| 8745 | const Type *RHSType = RHSVal->getType(); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 8746 | |
| 8747 | // Scan to see if all operands are the same opcode, all have one use, and all |
| 8748 | // kill their operands (i.e. the operands have one use). |
Chris Lattner | a90a24c | 2006-11-01 04:55:47 +0000 | [diff] [blame] | 8749 | for (unsigned i = 0; i != PN.getNumIncomingValues(); ++i) { |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 8750 | Instruction *I = dyn_cast<Instruction>(PN.getIncomingValue(i)); |
Chris Lattner | a90a24c | 2006-11-01 04:55:47 +0000 | [diff] [blame] | 8751 | if (!I || I->getOpcode() != Opc || !I->hasOneUse() || |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8752 | // 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] | 8753 | // types or GEP's with different index types. |
| 8754 | I->getOperand(0)->getType() != LHSType || |
| 8755 | I->getOperand(1)->getType() != RHSType) |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 8756 | return 0; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8757 | |
| 8758 | // If they are CmpInst instructions, check their predicates |
| 8759 | if (Opc == Instruction::ICmp || Opc == Instruction::FCmp) |
| 8760 | if (cast<CmpInst>(I)->getPredicate() != |
| 8761 | cast<CmpInst>(FirstInst)->getPredicate()) |
| 8762 | return 0; |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 8763 | |
| 8764 | // Keep track of which operand needs a phi node. |
| 8765 | if (I->getOperand(0) != LHSVal) LHSVal = 0; |
| 8766 | if (I->getOperand(1) != RHSVal) RHSVal = 0; |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 8767 | } |
| 8768 | |
Chris Lattner | 53738a4 | 2006-11-08 19:42:28 +0000 | [diff] [blame] | 8769 | // Otherwise, this is safe to transform, determine if it is profitable. |
| 8770 | |
| 8771 | // If this is a GEP, and if the index (not the pointer) needs a PHI, bail out. |
| 8772 | // Indexes are often folded into load/store instructions, so we don't want to |
| 8773 | // hide them behind a phi. |
| 8774 | if (isa<GetElementPtrInst>(FirstInst) && RHSVal == 0) |
| 8775 | return 0; |
| 8776 | |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 8777 | Value *InLHS = FirstInst->getOperand(0); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 8778 | Value *InRHS = FirstInst->getOperand(1); |
Chris Lattner | 53738a4 | 2006-11-08 19:42:28 +0000 | [diff] [blame] | 8779 | PHINode *NewLHS = 0, *NewRHS = 0; |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 8780 | if (LHSVal == 0) { |
| 8781 | NewLHS = new PHINode(LHSType, FirstInst->getOperand(0)->getName()+".pn"); |
| 8782 | NewLHS->reserveOperandSpace(PN.getNumOperands()/2); |
| 8783 | NewLHS->addIncoming(InLHS, PN.getIncomingBlock(0)); |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 8784 | InsertNewInstBefore(NewLHS, PN); |
| 8785 | LHSVal = NewLHS; |
| 8786 | } |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 8787 | |
| 8788 | if (RHSVal == 0) { |
| 8789 | NewRHS = new PHINode(RHSType, FirstInst->getOperand(1)->getName()+".pn"); |
| 8790 | NewRHS->reserveOperandSpace(PN.getNumOperands()/2); |
| 8791 | NewRHS->addIncoming(InRHS, PN.getIncomingBlock(0)); |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 8792 | InsertNewInstBefore(NewRHS, PN); |
| 8793 | RHSVal = NewRHS; |
| 8794 | } |
| 8795 | |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 8796 | // Add all operands to the new PHIs. |
| 8797 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 8798 | if (NewLHS) { |
| 8799 | Value *NewInLHS =cast<Instruction>(PN.getIncomingValue(i))->getOperand(0); |
| 8800 | NewLHS->addIncoming(NewInLHS, PN.getIncomingBlock(i)); |
| 8801 | } |
| 8802 | if (NewRHS) { |
| 8803 | Value *NewInRHS =cast<Instruction>(PN.getIncomingValue(i))->getOperand(1); |
| 8804 | NewRHS->addIncoming(NewInRHS, PN.getIncomingBlock(i)); |
| 8805 | } |
| 8806 | } |
| 8807 | |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 8808 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 8809 | return BinaryOperator::create(BinOp->getOpcode(), LHSVal, RHSVal); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8810 | else if (CmpInst *CIOp = dyn_cast<CmpInst>(FirstInst)) |
| 8811 | return CmpInst::create(CIOp->getOpcode(), CIOp->getPredicate(), LHSVal, |
| 8812 | RHSVal); |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 8813 | else { |
| 8814 | assert(isa<GetElementPtrInst>(FirstInst)); |
| 8815 | return new GetElementPtrInst(LHSVal, RHSVal); |
| 8816 | } |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 8817 | } |
| 8818 | |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 8819 | /// isSafeToSinkLoad - Return true if we know that it is safe sink the load out |
| 8820 | /// of the block that defines it. This means that it must be obvious the value |
| 8821 | /// of the load is not changed from the point of the load to the end of the |
| 8822 | /// block it is in. |
Chris Lattner | fd905ca | 2007-02-01 22:30:07 +0000 | [diff] [blame] | 8823 | /// |
| 8824 | /// Finally, it is safe, but not profitable, to sink a load targetting a |
| 8825 | /// non-address-taken alloca. Doing so will cause us to not promote the alloca |
| 8826 | /// to a register. |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 8827 | static bool isSafeToSinkLoad(LoadInst *L) { |
| 8828 | BasicBlock::iterator BBI = L, E = L->getParent()->end(); |
| 8829 | |
| 8830 | for (++BBI; BBI != E; ++BBI) |
| 8831 | if (BBI->mayWriteToMemory()) |
| 8832 | return false; |
Chris Lattner | fd905ca | 2007-02-01 22:30:07 +0000 | [diff] [blame] | 8833 | |
| 8834 | // Check for non-address taken alloca. If not address-taken already, it isn't |
| 8835 | // profitable to do this xform. |
| 8836 | if (AllocaInst *AI = dyn_cast<AllocaInst>(L->getOperand(0))) { |
| 8837 | bool isAddressTaken = false; |
| 8838 | for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); |
| 8839 | UI != E; ++UI) { |
| 8840 | if (isa<LoadInst>(UI)) continue; |
| 8841 | if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) { |
| 8842 | // If storing TO the alloca, then the address isn't taken. |
| 8843 | if (SI->getOperand(1) == AI) continue; |
| 8844 | } |
| 8845 | isAddressTaken = true; |
| 8846 | break; |
| 8847 | } |
| 8848 | |
| 8849 | if (!isAddressTaken) |
| 8850 | return false; |
| 8851 | } |
| 8852 | |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 8853 | return true; |
| 8854 | } |
| 8855 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8856 | |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 8857 | // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary" |
| 8858 | // operator and they all are only used by the PHI, PHI together their |
| 8859 | // inputs, and do the operation once, to the result of the PHI. |
| 8860 | Instruction *InstCombiner::FoldPHIArgOpIntoPHI(PHINode &PN) { |
| 8861 | Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0)); |
| 8862 | |
| 8863 | // Scan the instruction, looking for input operations that can be folded away. |
| 8864 | // If all input operands to the phi are the same instruction (e.g. a cast from |
| 8865 | // the same type or "+42") we can pull the operation through the PHI, reducing |
| 8866 | // code size and simplifying code. |
| 8867 | Constant *ConstantOp = 0; |
| 8868 | const Type *CastSrcTy = 0; |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 8869 | bool isVolatile = false; |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 8870 | if (isa<CastInst>(FirstInst)) { |
| 8871 | CastSrcTy = FirstInst->getOperand(0)->getType(); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 8872 | } else if (isa<BinaryOperator>(FirstInst) || isa<CmpInst>(FirstInst)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8873 | // Can fold binop, compare or shift here if the RHS is a constant, |
| 8874 | // otherwise call FoldPHIArgBinOpIntoPHI. |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 8875 | ConstantOp = dyn_cast<Constant>(FirstInst->getOperand(1)); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 8876 | if (ConstantOp == 0) |
| 8877 | return FoldPHIArgBinOpIntoPHI(PN); |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 8878 | } else if (LoadInst *LI = dyn_cast<LoadInst>(FirstInst)) { |
| 8879 | isVolatile = LI->isVolatile(); |
| 8880 | // We can't sink the load if the loaded value could be modified between the |
| 8881 | // load and the PHI. |
| 8882 | if (LI->getParent() != PN.getIncomingBlock(0) || |
| 8883 | !isSafeToSinkLoad(LI)) |
| 8884 | return 0; |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 8885 | } else if (isa<GetElementPtrInst>(FirstInst)) { |
Chris Lattner | 53738a4 | 2006-11-08 19:42:28 +0000 | [diff] [blame] | 8886 | if (FirstInst->getNumOperands() == 2) |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 8887 | return FoldPHIArgBinOpIntoPHI(PN); |
| 8888 | // Can't handle general GEPs yet. |
| 8889 | return 0; |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 8890 | } else { |
| 8891 | return 0; // Cannot fold this operation. |
| 8892 | } |
| 8893 | |
| 8894 | // Check to see if all arguments are the same operation. |
| 8895 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 8896 | if (!isa<Instruction>(PN.getIncomingValue(i))) return 0; |
| 8897 | Instruction *I = cast<Instruction>(PN.getIncomingValue(i)); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8898 | if (!I->hasOneUse() || !I->isSameOperationAs(FirstInst)) |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 8899 | return 0; |
| 8900 | if (CastSrcTy) { |
| 8901 | if (I->getOperand(0)->getType() != CastSrcTy) |
| 8902 | return 0; // Cast operation must match. |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 8903 | } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8904 | // We can't sink the load if the loaded value could be modified between |
| 8905 | // the load and the PHI. |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 8906 | if (LI->isVolatile() != isVolatile || |
| 8907 | LI->getParent() != PN.getIncomingBlock(i) || |
| 8908 | !isSafeToSinkLoad(LI)) |
| 8909 | return 0; |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 8910 | } else if (I->getOperand(1) != ConstantOp) { |
| 8911 | return 0; |
| 8912 | } |
| 8913 | } |
| 8914 | |
| 8915 | // Okay, they are all the same operation. Create a new PHI node of the |
| 8916 | // correct type, and PHI together all of the LHS's of the instructions. |
| 8917 | PHINode *NewPN = new PHINode(FirstInst->getOperand(0)->getType(), |
| 8918 | PN.getName()+".in"); |
Chris Lattner | 5551706 | 2005-01-29 00:39:08 +0000 | [diff] [blame] | 8919 | NewPN->reserveOperandSpace(PN.getNumOperands()/2); |
Chris Lattner | b589344 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 8920 | |
| 8921 | Value *InVal = FirstInst->getOperand(0); |
| 8922 | NewPN->addIncoming(InVal, PN.getIncomingBlock(0)); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 8923 | |
| 8924 | // Add all operands to the new PHI. |
Chris Lattner | b589344 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 8925 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 8926 | Value *NewInVal = cast<Instruction>(PN.getIncomingValue(i))->getOperand(0); |
| 8927 | if (NewInVal != InVal) |
| 8928 | InVal = 0; |
| 8929 | NewPN->addIncoming(NewInVal, PN.getIncomingBlock(i)); |
| 8930 | } |
| 8931 | |
| 8932 | Value *PhiVal; |
| 8933 | if (InVal) { |
| 8934 | // The new PHI unions all of the same values together. This is really |
| 8935 | // common, so we handle it intelligently here for compile-time speed. |
| 8936 | PhiVal = InVal; |
| 8937 | delete NewPN; |
| 8938 | } else { |
| 8939 | InsertNewInstBefore(NewPN, PN); |
| 8940 | PhiVal = NewPN; |
| 8941 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8942 | |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 8943 | // Insert and return the new operation. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8944 | if (CastInst* FirstCI = dyn_cast<CastInst>(FirstInst)) |
| 8945 | return CastInst::create(FirstCI->getOpcode(), PhiVal, PN.getType()); |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 8946 | else if (isa<LoadInst>(FirstInst)) |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 8947 | return new LoadInst(PhiVal, "", isVolatile); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 8948 | else if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) |
Chris Lattner | b589344 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 8949 | return BinaryOperator::create(BinOp->getOpcode(), PhiVal, ConstantOp); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8950 | else if (CmpInst *CIOp = dyn_cast<CmpInst>(FirstInst)) |
| 8951 | return CmpInst::create(CIOp->getOpcode(), CIOp->getPredicate(), |
| 8952 | PhiVal, ConstantOp); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 8953 | else |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 8954 | assert(0 && "Unknown operation"); |
Jeff Cohen | ca5183d | 2007-03-05 00:00:42 +0000 | [diff] [blame] | 8955 | return 0; |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 8956 | } |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 8957 | |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 8958 | /// DeadPHICycle - Return true if this PHI node is only used by a PHI node cycle |
| 8959 | /// that is dead. |
Chris Lattner | 0e5444b | 2007-03-26 20:40:50 +0000 | [diff] [blame] | 8960 | static bool DeadPHICycle(PHINode *PN, |
| 8961 | SmallPtrSet<PHINode*, 16> &PotentiallyDeadPHIs) { |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 8962 | if (PN->use_empty()) return true; |
| 8963 | if (!PN->hasOneUse()) return false; |
| 8964 | |
| 8965 | // Remember this node, and if we find the cycle, return. |
Chris Lattner | 0e5444b | 2007-03-26 20:40:50 +0000 | [diff] [blame] | 8966 | if (!PotentiallyDeadPHIs.insert(PN)) |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 8967 | return true; |
Chris Lattner | 92103de | 2007-08-28 04:23:55 +0000 | [diff] [blame] | 8968 | |
| 8969 | // Don't scan crazily complex things. |
| 8970 | if (PotentiallyDeadPHIs.size() == 16) |
| 8971 | return false; |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 8972 | |
| 8973 | if (PHINode *PU = dyn_cast<PHINode>(PN->use_back())) |
| 8974 | return DeadPHICycle(PU, PotentiallyDeadPHIs); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8975 | |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 8976 | return false; |
| 8977 | } |
| 8978 | |
Chris Lattner | cf5008a | 2007-11-06 21:52:06 +0000 | [diff] [blame] | 8979 | /// PHIsEqualValue - Return true if this phi node is always equal to |
| 8980 | /// NonPhiInVal. This happens with mutually cyclic phi nodes like: |
| 8981 | /// z = some value; x = phi (y, z); y = phi (x, z) |
| 8982 | static bool PHIsEqualValue(PHINode *PN, Value *NonPhiInVal, |
| 8983 | SmallPtrSet<PHINode*, 16> &ValueEqualPHIs) { |
| 8984 | // See if we already saw this PHI node. |
| 8985 | if (!ValueEqualPHIs.insert(PN)) |
| 8986 | return true; |
| 8987 | |
| 8988 | // Don't scan crazily complex things. |
| 8989 | if (ValueEqualPHIs.size() == 16) |
| 8990 | return false; |
| 8991 | |
| 8992 | // Scan the operands to see if they are either phi nodes or are equal to |
| 8993 | // the value. |
| 8994 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 8995 | Value *Op = PN->getIncomingValue(i); |
| 8996 | if (PHINode *OpPN = dyn_cast<PHINode>(Op)) { |
| 8997 | if (!PHIsEqualValue(OpPN, NonPhiInVal, ValueEqualPHIs)) |
| 8998 | return false; |
| 8999 | } else if (Op != NonPhiInVal) |
| 9000 | return false; |
| 9001 | } |
| 9002 | |
| 9003 | return true; |
| 9004 | } |
| 9005 | |
| 9006 | |
Chris Lattner | 473945d | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 9007 | // PHINode simplification |
| 9008 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 9009 | Instruction *InstCombiner::visitPHINode(PHINode &PN) { |
Owen Anderson | b64ab87 | 2006-07-10 22:15:25 +0000 | [diff] [blame] | 9010 | // If LCSSA is around, don't mess with Phi nodes |
Chris Lattner | f964f32 | 2007-03-04 04:27:24 +0000 | [diff] [blame] | 9011 | if (MustPreserveLCSSA) return 0; |
Owen Anderson | d1b78a1 | 2006-07-10 19:03:49 +0000 | [diff] [blame] | 9012 | |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 9013 | if (Value *V = PN.hasConstantValue()) |
| 9014 | return ReplaceInstUsesWith(PN, V); |
| 9015 | |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 9016 | // If all PHI operands are the same operation, pull them through the PHI, |
| 9017 | // reducing code size. |
| 9018 | if (isa<Instruction>(PN.getIncomingValue(0)) && |
| 9019 | PN.getIncomingValue(0)->hasOneUse()) |
| 9020 | if (Instruction *Result = FoldPHIArgOpIntoPHI(PN)) |
| 9021 | return Result; |
| 9022 | |
| 9023 | // If this is a trivial cycle in the PHI node graph, remove it. Basically, if |
| 9024 | // this PHI only has a single use (a PHI), and if that PHI only has one use (a |
| 9025 | // PHI)... break the cycle. |
Chris Lattner | ff9f13a | 2007-01-15 07:30:06 +0000 | [diff] [blame] | 9026 | if (PN.hasOneUse()) { |
| 9027 | Instruction *PHIUser = cast<Instruction>(PN.use_back()); |
| 9028 | if (PHINode *PU = dyn_cast<PHINode>(PHIUser)) { |
Chris Lattner | 0e5444b | 2007-03-26 20:40:50 +0000 | [diff] [blame] | 9029 | SmallPtrSet<PHINode*, 16> PotentiallyDeadPHIs; |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 9030 | PotentiallyDeadPHIs.insert(&PN); |
| 9031 | if (DeadPHICycle(PU, PotentiallyDeadPHIs)) |
| 9032 | return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType())); |
| 9033 | } |
Chris Lattner | ff9f13a | 2007-01-15 07:30:06 +0000 | [diff] [blame] | 9034 | |
| 9035 | // If this phi has a single use, and if that use just computes a value for |
| 9036 | // the next iteration of a loop, delete the phi. This occurs with unused |
| 9037 | // induction variables, e.g. "for (int j = 0; ; ++j);". Detecting this |
| 9038 | // common case here is good because the only other things that catch this |
| 9039 | // are induction variable analysis (sometimes) and ADCE, which is only run |
| 9040 | // late. |
| 9041 | if (PHIUser->hasOneUse() && |
| 9042 | (isa<BinaryOperator>(PHIUser) || isa<GetElementPtrInst>(PHIUser)) && |
| 9043 | PHIUser->use_back() == &PN) { |
| 9044 | return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType())); |
| 9045 | } |
| 9046 | } |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 9047 | |
Chris Lattner | cf5008a | 2007-11-06 21:52:06 +0000 | [diff] [blame] | 9048 | // We sometimes end up with phi cycles that non-obviously end up being the |
| 9049 | // same value, for example: |
| 9050 | // z = some value; x = phi (y, z); y = phi (x, z) |
| 9051 | // where the phi nodes don't necessarily need to be in the same block. Do a |
| 9052 | // quick check to see if the PHI node only contains a single non-phi value, if |
| 9053 | // so, scan to see if the phi cycle is actually equal to that value. |
| 9054 | { |
| 9055 | unsigned InValNo = 0, NumOperandVals = PN.getNumIncomingValues(); |
| 9056 | // Scan for the first non-phi operand. |
| 9057 | while (InValNo != NumOperandVals && |
| 9058 | isa<PHINode>(PN.getIncomingValue(InValNo))) |
| 9059 | ++InValNo; |
| 9060 | |
| 9061 | if (InValNo != NumOperandVals) { |
| 9062 | Value *NonPhiInVal = PN.getOperand(InValNo); |
| 9063 | |
| 9064 | // Scan the rest of the operands to see if there are any conflicts, if so |
| 9065 | // there is no need to recursively scan other phis. |
| 9066 | for (++InValNo; InValNo != NumOperandVals; ++InValNo) { |
| 9067 | Value *OpVal = PN.getIncomingValue(InValNo); |
| 9068 | if (OpVal != NonPhiInVal && !isa<PHINode>(OpVal)) |
| 9069 | break; |
| 9070 | } |
| 9071 | |
| 9072 | // If we scanned over all operands, then we have one unique value plus |
| 9073 | // phi values. Scan PHI nodes to see if they all merge in each other or |
| 9074 | // the value. |
| 9075 | if (InValNo == NumOperandVals) { |
| 9076 | SmallPtrSet<PHINode*, 16> ValueEqualPHIs; |
| 9077 | if (PHIsEqualValue(&PN, NonPhiInVal, ValueEqualPHIs)) |
| 9078 | return ReplaceInstUsesWith(PN, NonPhiInVal); |
| 9079 | } |
| 9080 | } |
| 9081 | } |
Chris Lattner | 60921c9 | 2003-12-19 05:58:40 +0000 | [diff] [blame] | 9082 | return 0; |
Chris Lattner | 473945d | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 9083 | } |
| 9084 | |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9085 | static Value *InsertCastToIntPtrTy(Value *V, const Type *DTy, |
| 9086 | Instruction *InsertPoint, |
| 9087 | InstCombiner *IC) { |
Reid Spencer | abaa8ca | 2007-01-08 16:32:00 +0000 | [diff] [blame] | 9088 | unsigned PtrSize = DTy->getPrimitiveSizeInBits(); |
| 9089 | unsigned VTySize = V->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9090 | // We must cast correctly to the pointer type. Ensure that we |
| 9091 | // sign extend the integer value if it is smaller as this is |
| 9092 | // used for address computation. |
| 9093 | Instruction::CastOps opcode = |
| 9094 | (VTySize < PtrSize ? Instruction::SExt : |
| 9095 | (VTySize == PtrSize ? Instruction::BitCast : Instruction::Trunc)); |
| 9096 | return IC->InsertCastBefore(opcode, V, DTy, *InsertPoint); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9097 | } |
| 9098 | |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 9099 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 9100 | Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9101 | Value *PtrOp = GEP.getOperand(0); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 9102 | // Is it 'getelementptr %P, i32 0' or 'getelementptr %P' |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 9103 | // If so, eliminate the noop. |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 9104 | if (GEP.getNumOperands() == 1) |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9105 | return ReplaceInstUsesWith(GEP, PtrOp); |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 9106 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 9107 | if (isa<UndefValue>(GEP.getOperand(0))) |
| 9108 | return ReplaceInstUsesWith(GEP, UndefValue::get(GEP.getType())); |
| 9109 | |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 9110 | bool HasZeroPointerIndex = false; |
| 9111 | if (Constant *C = dyn_cast<Constant>(GEP.getOperand(1))) |
| 9112 | HasZeroPointerIndex = C->isNullValue(); |
| 9113 | |
| 9114 | if (GEP.getNumOperands() == 2 && HasZeroPointerIndex) |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9115 | return ReplaceInstUsesWith(GEP, PtrOp); |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 9116 | |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9117 | // Eliminate unneeded casts for indices. |
| 9118 | bool MadeChange = false; |
Chris Lattner | db9654e | 2007-03-25 20:43:09 +0000 | [diff] [blame] | 9119 | |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 9120 | gep_type_iterator GTI = gep_type_begin(GEP); |
Chris Lattner | db9654e | 2007-03-25 20:43:09 +0000 | [diff] [blame] | 9121 | for (unsigned i = 1, e = GEP.getNumOperands(); i != e; ++i, ++GTI) { |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 9122 | if (isa<SequentialType>(*GTI)) { |
| 9123 | if (CastInst *CI = dyn_cast<CastInst>(GEP.getOperand(i))) { |
Chris Lattner | 76b7a06 | 2007-01-15 07:02:54 +0000 | [diff] [blame] | 9124 | if (CI->getOpcode() == Instruction::ZExt || |
| 9125 | CI->getOpcode() == Instruction::SExt) { |
| 9126 | const Type *SrcTy = CI->getOperand(0)->getType(); |
| 9127 | // We can eliminate a cast from i32 to i64 iff the target |
| 9128 | // is a 32-bit pointer target. |
| 9129 | if (SrcTy->getPrimitiveSizeInBits() >= TD->getPointerSizeInBits()) { |
| 9130 | MadeChange = true; |
| 9131 | GEP.setOperand(i, CI->getOperand(0)); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9132 | } |
| 9133 | } |
| 9134 | } |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 9135 | // If we are using a wider index than needed for this platform, shrink it |
| 9136 | // to what we need. If the incoming value needs a cast instruction, |
| 9137 | // insert it. This explicit cast can make subsequent optimizations more |
| 9138 | // obvious. |
| 9139 | Value *Op = GEP.getOperand(i); |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 9140 | if (TD->getTypeSizeInBits(Op->getType()) > TD->getPointerSizeInBits()) { |
Chris Lattner | 4f1134e | 2004-04-17 18:16:10 +0000 | [diff] [blame] | 9141 | if (Constant *C = dyn_cast<Constant>(Op)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9142 | GEP.setOperand(i, ConstantExpr::getTrunc(C, TD->getIntPtrType())); |
Chris Lattner | 4f1134e | 2004-04-17 18:16:10 +0000 | [diff] [blame] | 9143 | MadeChange = true; |
| 9144 | } else { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9145 | Op = InsertCastBefore(Instruction::Trunc, Op, TD->getIntPtrType(), |
| 9146 | GEP); |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 9147 | GEP.setOperand(i, Op); |
| 9148 | MadeChange = true; |
| 9149 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 9150 | } |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9151 | } |
Chris Lattner | db9654e | 2007-03-25 20:43:09 +0000 | [diff] [blame] | 9152 | } |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9153 | if (MadeChange) return &GEP; |
| 9154 | |
Chris Lattner | db9654e | 2007-03-25 20:43:09 +0000 | [diff] [blame] | 9155 | // If this GEP instruction doesn't move the pointer, and if the input operand |
| 9156 | // is a bitcast of another pointer, just replace the GEP with a bitcast of the |
| 9157 | // real input to the dest type. |
Chris Lattner | 6a94de2 | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 9158 | if (GEP.hasAllZeroIndices()) { |
| 9159 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(GEP.getOperand(0))) { |
| 9160 | // If the bitcast is of an allocation, and the allocation will be |
| 9161 | // converted to match the type of the cast, don't touch this. |
| 9162 | if (isa<AllocationInst>(BCI->getOperand(0))) { |
| 9163 | // 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] | 9164 | if (Instruction *I = visitBitCast(*BCI)) { |
| 9165 | if (I != BCI) { |
| 9166 | I->takeName(BCI); |
| 9167 | BCI->getParent()->getInstList().insert(BCI, I); |
| 9168 | ReplaceInstUsesWith(*BCI, I); |
| 9169 | } |
Chris Lattner | 6a94de2 | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 9170 | return &GEP; |
Chris Lattner | a79dd43 | 2007-10-12 18:05:47 +0000 | [diff] [blame] | 9171 | } |
Chris Lattner | 6a94de2 | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 9172 | } |
| 9173 | return new BitCastInst(BCI->getOperand(0), GEP.getType()); |
| 9174 | } |
| 9175 | } |
| 9176 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9177 | // Combine Indices - If the source pointer to this getelementptr instruction |
| 9178 | // is a getelementptr instruction, combine the indices of the two |
| 9179 | // getelementptr instructions into a single instruction. |
| 9180 | // |
Chris Lattner | 72588fc | 2007-02-15 22:48:32 +0000 | [diff] [blame] | 9181 | SmallVector<Value*, 8> SrcGEPOperands; |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 9182 | if (User *Src = dyn_castGetElementPtr(PtrOp)) |
Chris Lattner | 72588fc | 2007-02-15 22:48:32 +0000 | [diff] [blame] | 9183 | SrcGEPOperands.append(Src->op_begin(), Src->op_end()); |
Chris Lattner | ebd985c | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 9184 | |
| 9185 | if (!SrcGEPOperands.empty()) { |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9186 | // Note that if our source is a gep chain itself that we wait for that |
| 9187 | // chain to be resolved before we perform this transformation. This |
| 9188 | // avoids us creating a TON of code in some cases. |
| 9189 | // |
| 9190 | if (isa<GetElementPtrInst>(SrcGEPOperands[0]) && |
| 9191 | cast<Instruction>(SrcGEPOperands[0])->getNumOperands() == 2) |
| 9192 | return 0; // Wait until our source is folded to completion. |
| 9193 | |
Chris Lattner | 72588fc | 2007-02-15 22:48:32 +0000 | [diff] [blame] | 9194 | SmallVector<Value*, 8> Indices; |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9195 | |
| 9196 | // Find out whether the last index in the source GEP is a sequential idx. |
| 9197 | bool EndsWithSequential = false; |
| 9198 | for (gep_type_iterator I = gep_type_begin(*cast<User>(PtrOp)), |
| 9199 | E = gep_type_end(*cast<User>(PtrOp)); I != E; ++I) |
Chris Lattner | be97b4e | 2004-05-08 22:41:42 +0000 | [diff] [blame] | 9200 | EndsWithSequential = !isa<StructType>(*I); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9201 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9202 | // Can we combine the two pointer arithmetics offsets? |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9203 | if (EndsWithSequential) { |
Chris Lattner | decd081 | 2003-03-05 22:33:14 +0000 | [diff] [blame] | 9204 | // Replace: gep (gep %P, long B), long A, ... |
| 9205 | // With: T = long A+B; gep %P, T, ... |
| 9206 | // |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9207 | Value *Sum, *SO1 = SrcGEPOperands.back(), *GO1 = GEP.getOperand(1); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9208 | if (SO1 == Constant::getNullValue(SO1->getType())) { |
| 9209 | Sum = GO1; |
| 9210 | } else if (GO1 == Constant::getNullValue(GO1->getType())) { |
| 9211 | Sum = SO1; |
| 9212 | } else { |
| 9213 | // If they aren't the same type, convert both to an integer of the |
| 9214 | // target's pointer size. |
| 9215 | if (SO1->getType() != GO1->getType()) { |
| 9216 | if (Constant *SO1C = dyn_cast<Constant>(SO1)) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9217 | SO1 = ConstantExpr::getIntegerCast(SO1C, GO1->getType(), true); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9218 | } else if (Constant *GO1C = dyn_cast<Constant>(GO1)) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9219 | GO1 = ConstantExpr::getIntegerCast(GO1C, SO1->getType(), true); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9220 | } else { |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 9221 | unsigned PS = TD->getPointerSizeInBits(); |
| 9222 | if (TD->getTypeSizeInBits(SO1->getType()) == PS) { |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9223 | // Convert GO1 to SO1's type. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9224 | GO1 = InsertCastToIntPtrTy(GO1, SO1->getType(), &GEP, this); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9225 | |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 9226 | } else if (TD->getTypeSizeInBits(GO1->getType()) == PS) { |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9227 | // Convert SO1 to GO1's type. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9228 | SO1 = InsertCastToIntPtrTy(SO1, GO1->getType(), &GEP, this); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9229 | } else { |
| 9230 | const Type *PT = TD->getIntPtrType(); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9231 | SO1 = InsertCastToIntPtrTy(SO1, PT, &GEP, this); |
| 9232 | GO1 = InsertCastToIntPtrTy(GO1, PT, &GEP, this); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9233 | } |
| 9234 | } |
| 9235 | } |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9236 | if (isa<Constant>(SO1) && isa<Constant>(GO1)) |
| 9237 | Sum = ConstantExpr::getAdd(cast<Constant>(SO1), cast<Constant>(GO1)); |
| 9238 | else { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 9239 | Sum = BinaryOperator::createAdd(SO1, GO1, PtrOp->getName()+".sum"); |
| 9240 | InsertNewInstBefore(cast<Instruction>(Sum), GEP); |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9241 | } |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9242 | } |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9243 | |
| 9244 | // Recycle the GEP we already have if possible. |
| 9245 | if (SrcGEPOperands.size() == 2) { |
| 9246 | GEP.setOperand(0, SrcGEPOperands[0]); |
| 9247 | GEP.setOperand(1, Sum); |
| 9248 | return &GEP; |
| 9249 | } else { |
| 9250 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, |
| 9251 | SrcGEPOperands.end()-1); |
| 9252 | Indices.push_back(Sum); |
| 9253 | Indices.insert(Indices.end(), GEP.op_begin()+2, GEP.op_end()); |
| 9254 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9255 | } else if (isa<Constant>(*GEP.idx_begin()) && |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9256 | cast<Constant>(*GEP.idx_begin())->isNullValue() && |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9257 | SrcGEPOperands.size() != 1) { |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9258 | // 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] | 9259 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, |
| 9260 | SrcGEPOperands.end()); |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9261 | Indices.insert(Indices.end(), GEP.idx_begin()+1, GEP.idx_end()); |
| 9262 | } |
| 9263 | |
| 9264 | if (!Indices.empty()) |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 9265 | return new GetElementPtrInst(SrcGEPOperands[0], Indices.begin(), |
| 9266 | Indices.end(), GEP.getName()); |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 9267 | |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9268 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(PtrOp)) { |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 9269 | // GEP of global variable. If all of the indices for this GEP are |
| 9270 | // constants, we can promote this to a constexpr instead of an instruction. |
| 9271 | |
| 9272 | // Scan for nonconstants... |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 9273 | SmallVector<Constant*, 8> Indices; |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 9274 | User::op_iterator I = GEP.idx_begin(), E = GEP.idx_end(); |
| 9275 | for (; I != E && isa<Constant>(*I); ++I) |
| 9276 | Indices.push_back(cast<Constant>(*I)); |
| 9277 | |
| 9278 | if (I == E) { // If they are all constants... |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 9279 | Constant *CE = ConstantExpr::getGetElementPtr(GV, |
| 9280 | &Indices[0],Indices.size()); |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 9281 | |
| 9282 | // Replace all uses of the GEP with the new constexpr... |
| 9283 | return ReplaceInstUsesWith(GEP, CE); |
| 9284 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9285 | } else if (Value *X = getBitCastOperand(PtrOp)) { // Is the operand a cast? |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 9286 | if (!isa<PointerType>(X->getType())) { |
| 9287 | // Not interesting. Source pointer must be a cast from pointer. |
| 9288 | } else if (HasZeroPointerIndex) { |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9289 | // transform: GEP (bitcast [10 x i8]* X to [0 x i8]*), i32 0, ... |
| 9290 | // into : GEP [10 x i8]* X, i32 0, ... |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 9291 | // |
| 9292 | // This occurs when the program declares an array extern like "int X[];" |
| 9293 | // |
| 9294 | const PointerType *CPTy = cast<PointerType>(PtrOp->getType()); |
| 9295 | const PointerType *XTy = cast<PointerType>(X->getType()); |
| 9296 | if (const ArrayType *XATy = |
| 9297 | dyn_cast<ArrayType>(XTy->getElementType())) |
| 9298 | if (const ArrayType *CATy = |
| 9299 | dyn_cast<ArrayType>(CPTy->getElementType())) |
| 9300 | if (CATy->getElementType() == XATy->getElementType()) { |
| 9301 | // At this point, we know that the cast source type is a pointer |
| 9302 | // to an array of the same type as the destination pointer |
| 9303 | // array. Because the array type is never stepped over (there |
| 9304 | // is a leading zero) we can fold the cast into this GEP. |
| 9305 | GEP.setOperand(0, X); |
| 9306 | return &GEP; |
| 9307 | } |
| 9308 | } else if (GEP.getNumOperands() == 2) { |
| 9309 | // Transform things like: |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9310 | // %t = getelementptr i32* bitcast ([2 x i32]* %str to i32*), i32 %V |
| 9311 | // into: %t1 = getelementptr [2 x i32]* %str, i32 0, i32 %V; bitcast |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 9312 | const Type *SrcElTy = cast<PointerType>(X->getType())->getElementType(); |
| 9313 | const Type *ResElTy=cast<PointerType>(PtrOp->getType())->getElementType(); |
| 9314 | if (isa<ArrayType>(SrcElTy) && |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 9315 | TD->getABITypeSize(cast<ArrayType>(SrcElTy)->getElementType()) == |
| 9316 | TD->getABITypeSize(ResElTy)) { |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 9317 | Value *Idx[2]; |
| 9318 | Idx[0] = Constant::getNullValue(Type::Int32Ty); |
| 9319 | Idx[1] = GEP.getOperand(1); |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 9320 | Value *V = InsertNewInstBefore( |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 9321 | new GetElementPtrInst(X, Idx, Idx + 2, GEP.getName()), GEP); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9322 | // V and GEP are both pointer types --> BitCast |
| 9323 | return new BitCastInst(V, GEP.getType()); |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 9324 | } |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9325 | |
| 9326 | // Transform things like: |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9327 | // getelementptr i8* bitcast ([100 x double]* X to i8*), i32 %tmp |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9328 | // (where tmp = 8*tmp2) into: |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9329 | // getelementptr [100 x double]* %arr, i32 0, i32 %tmp2; bitcast |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9330 | |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9331 | if (isa<ArrayType>(SrcElTy) && ResElTy == Type::Int8Ty) { |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9332 | uint64_t ArrayEltSize = |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 9333 | TD->getABITypeSize(cast<ArrayType>(SrcElTy)->getElementType()); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9334 | |
| 9335 | // Check to see if "tmp" is a scale by a multiple of ArrayEltSize. We |
| 9336 | // allow either a mul, shift, or constant here. |
| 9337 | Value *NewIdx = 0; |
| 9338 | ConstantInt *Scale = 0; |
| 9339 | if (ArrayEltSize == 1) { |
| 9340 | NewIdx = GEP.getOperand(1); |
| 9341 | Scale = ConstantInt::get(NewIdx->getType(), 1); |
| 9342 | } else if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP.getOperand(1))) { |
Chris Lattner | 6e2f843 | 2005-09-14 17:32:56 +0000 | [diff] [blame] | 9343 | NewIdx = ConstantInt::get(CI->getType(), 1); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9344 | Scale = CI; |
| 9345 | } else if (Instruction *Inst =dyn_cast<Instruction>(GEP.getOperand(1))){ |
| 9346 | if (Inst->getOpcode() == Instruction::Shl && |
| 9347 | isa<ConstantInt>(Inst->getOperand(1))) { |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 9348 | ConstantInt *ShAmt = cast<ConstantInt>(Inst->getOperand(1)); |
| 9349 | uint32_t ShAmtVal = ShAmt->getLimitedValue(64); |
| 9350 | Scale = ConstantInt::get(Inst->getType(), 1ULL << ShAmtVal); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9351 | NewIdx = Inst->getOperand(0); |
| 9352 | } else if (Inst->getOpcode() == Instruction::Mul && |
| 9353 | isa<ConstantInt>(Inst->getOperand(1))) { |
| 9354 | Scale = cast<ConstantInt>(Inst->getOperand(1)); |
| 9355 | NewIdx = Inst->getOperand(0); |
| 9356 | } |
| 9357 | } |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9358 | |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9359 | // 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] | 9360 | // out, perform the transformation. Note, we don't know whether Scale is |
| 9361 | // signed or not. We'll use unsigned version of division/modulo |
| 9362 | // operation after making sure Scale doesn't have the sign bit set. |
| 9363 | if (Scale && Scale->getSExtValue() >= 0LL && |
| 9364 | Scale->getZExtValue() % ArrayEltSize == 0) { |
| 9365 | Scale = ConstantInt::get(Scale->getType(), |
| 9366 | Scale->getZExtValue() / ArrayEltSize); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 9367 | if (Scale->getZExtValue() != 1) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9368 | Constant *C = ConstantExpr::getIntegerCast(Scale, NewIdx->getType(), |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9369 | false /*ZExt*/); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9370 | Instruction *Sc = BinaryOperator::createMul(NewIdx, C, "idxscale"); |
| 9371 | NewIdx = InsertNewInstBefore(Sc, GEP); |
| 9372 | } |
| 9373 | |
| 9374 | // Insert the new GEP instruction. |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 9375 | Value *Idx[2]; |
| 9376 | Idx[0] = Constant::getNullValue(Type::Int32Ty); |
| 9377 | Idx[1] = NewIdx; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9378 | Instruction *NewGEP = |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 9379 | new GetElementPtrInst(X, Idx, Idx + 2, GEP.getName()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9380 | NewGEP = InsertNewInstBefore(NewGEP, GEP); |
| 9381 | // The NewGEP must be pointer typed, so must the old one -> BitCast |
| 9382 | return new BitCastInst(NewGEP, GEP.getType()); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9383 | } |
| 9384 | } |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 9385 | } |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9386 | } |
| 9387 | |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9388 | return 0; |
| 9389 | } |
| 9390 | |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 9391 | Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) { |
| 9392 | // 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] | 9393 | if (AI.isArrayAllocation()) { // Check C != 1 |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 9394 | if (const ConstantInt *C = dyn_cast<ConstantInt>(AI.getArraySize())) { |
| 9395 | const Type *NewTy = |
| 9396 | ArrayType::get(AI.getAllocatedType(), C->getZExtValue()); |
Chris Lattner | 0006bd7 | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 9397 | AllocationInst *New = 0; |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 9398 | |
| 9399 | // Create and insert the replacement instruction... |
| 9400 | if (isa<MallocInst>(AI)) |
Nate Begeman | 14b0529 | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 9401 | New = new MallocInst(NewTy, 0, AI.getAlignment(), AI.getName()); |
Chris Lattner | 0006bd7 | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 9402 | else { |
| 9403 | assert(isa<AllocaInst>(AI) && "Unknown type of allocation inst!"); |
Nate Begeman | 14b0529 | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 9404 | New = new AllocaInst(NewTy, 0, AI.getAlignment(), AI.getName()); |
Chris Lattner | 0006bd7 | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 9405 | } |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 9406 | |
| 9407 | InsertNewInstBefore(New, AI); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9408 | |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 9409 | // Scan to the end of the allocation instructions, to skip over a block of |
| 9410 | // allocas if possible... |
| 9411 | // |
| 9412 | BasicBlock::iterator It = New; |
| 9413 | while (isa<AllocationInst>(*It)) ++It; |
| 9414 | |
| 9415 | // Now that I is pointing to the first non-allocation-inst in the block, |
| 9416 | // insert our getelementptr instruction... |
| 9417 | // |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9418 | Value *NullIdx = Constant::getNullValue(Type::Int32Ty); |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 9419 | Value *Idx[2]; |
| 9420 | Idx[0] = NullIdx; |
| 9421 | Idx[1] = NullIdx; |
| 9422 | Value *V = new GetElementPtrInst(New, Idx, Idx + 2, |
Chris Lattner | 693787a | 2005-05-04 19:10:26 +0000 | [diff] [blame] | 9423 | New->getName()+".sub", It); |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 9424 | |
| 9425 | // Now make everything use the getelementptr instead of the original |
| 9426 | // allocation. |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 9427 | return ReplaceInstUsesWith(AI, V); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 9428 | } else if (isa<UndefValue>(AI.getArraySize())) { |
| 9429 | return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 9430 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 9431 | } |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 9432 | |
| 9433 | // If alloca'ing a zero byte object, replace the alloca with a null pointer. |
| 9434 | // Note that we only do this for alloca's, because malloc should allocate and |
| 9435 | // return a unique pointer, even for a zero byte allocation. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9436 | if (isa<AllocaInst>(AI) && AI.getAllocatedType()->isSized() && |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 9437 | TD->getABITypeSize(AI.getAllocatedType()) == 0) |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 9438 | return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); |
| 9439 | |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 9440 | return 0; |
| 9441 | } |
| 9442 | |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 9443 | Instruction *InstCombiner::visitFreeInst(FreeInst &FI) { |
| 9444 | Value *Op = FI.getOperand(0); |
| 9445 | |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 9446 | // free undef -> unreachable. |
| 9447 | if (isa<UndefValue>(Op)) { |
| 9448 | // 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] | 9449 | new StoreInst(ConstantInt::getTrue(), |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 9450 | UndefValue::get(PointerType::getUnqual(Type::Int1Ty)), &FI); |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 9451 | return EraseInstFromFunction(FI); |
| 9452 | } |
Chris Lattner | 6fe5541 | 2007-04-14 00:20:02 +0000 | [diff] [blame] | 9453 | |
Chris Lattner | 6160e85 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 9454 | // If we have 'free null' delete the instruction. This can happen in stl code |
| 9455 | // when lots of inlining happens. |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 9456 | if (isa<ConstantPointerNull>(Op)) |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 9457 | return EraseInstFromFunction(FI); |
Chris Lattner | 6fe5541 | 2007-04-14 00:20:02 +0000 | [diff] [blame] | 9458 | |
| 9459 | // Change free <ty>* (cast <ty2>* X to <ty>*) into free <ty2>* X |
| 9460 | if (BitCastInst *CI = dyn_cast<BitCastInst>(Op)) { |
| 9461 | FI.setOperand(0, CI->getOperand(0)); |
| 9462 | return &FI; |
| 9463 | } |
| 9464 | |
| 9465 | // Change free (gep X, 0,0,0,0) into free(X) |
| 9466 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) { |
| 9467 | if (GEPI->hasAllZeroIndices()) { |
| 9468 | AddToWorkList(GEPI); |
| 9469 | FI.setOperand(0, GEPI->getOperand(0)); |
| 9470 | return &FI; |
| 9471 | } |
| 9472 | } |
| 9473 | |
| 9474 | // Change free(malloc) into nothing, if the malloc has a single use. |
| 9475 | if (MallocInst *MI = dyn_cast<MallocInst>(Op)) |
| 9476 | if (MI->hasOneUse()) { |
| 9477 | EraseInstFromFunction(FI); |
| 9478 | return EraseInstFromFunction(*MI); |
| 9479 | } |
Chris Lattner | 6160e85 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 9480 | |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 9481 | return 0; |
| 9482 | } |
| 9483 | |
| 9484 | |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 9485 | /// InstCombineLoadCast - Fold 'load (cast P)' -> cast (load P)' when possible. |
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 9486 | static Instruction *InstCombineLoadCast(InstCombiner &IC, LoadInst &LI, |
| 9487 | const TargetData *TD) { |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 9488 | User *CI = cast<User>(LI.getOperand(0)); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 9489 | Value *CastOp = CI->getOperand(0); |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 9490 | |
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 9491 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(CI)) { |
| 9492 | // Instead of loading constant c string, use corresponding integer value |
| 9493 | // directly if string length is small enough. |
| 9494 | const std::string &Str = CE->getOperand(0)->getStringValue(); |
| 9495 | if (!Str.empty()) { |
| 9496 | unsigned len = Str.length(); |
| 9497 | const Type *Ty = cast<PointerType>(CE->getType())->getElementType(); |
| 9498 | unsigned numBits = Ty->getPrimitiveSizeInBits(); |
| 9499 | // Replace LI with immediate integer store. |
| 9500 | if ((numBits >> 3) == len + 1) { |
| 9501 | APInt StrVal(numBits, 0); |
| 9502 | APInt SingleChar(numBits, 0); |
| 9503 | if (TD->isLittleEndian()) { |
| 9504 | for (signed i = len-1; i >= 0; i--) { |
| 9505 | SingleChar = (uint64_t) Str[i]; |
| 9506 | StrVal = (StrVal << 8) | SingleChar; |
| 9507 | } |
| 9508 | } else { |
| 9509 | for (unsigned i = 0; i < len; i++) { |
| 9510 | SingleChar = (uint64_t) Str[i]; |
| 9511 | StrVal = (StrVal << 8) | SingleChar; |
| 9512 | } |
| 9513 | // Append NULL at the end. |
| 9514 | SingleChar = 0; |
| 9515 | StrVal = (StrVal << 8) | SingleChar; |
| 9516 | } |
| 9517 | Value *NL = ConstantInt::get(StrVal); |
| 9518 | return IC.ReplaceInstUsesWith(LI, NL); |
| 9519 | } |
| 9520 | } |
| 9521 | } |
| 9522 | |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 9523 | const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType(); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 9524 | if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) { |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 9525 | const Type *SrcPTy = SrcTy->getElementType(); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 9526 | |
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 9527 | if (DestPTy->isInteger() || isa<PointerType>(DestPTy) || |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 9528 | isa<VectorType>(DestPTy)) { |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 9529 | // If the source is an array, the code below will not succeed. Check to |
| 9530 | // see if a trivial 'gep P, 0, 0' will help matters. Only do this for |
| 9531 | // constants. |
| 9532 | if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy)) |
| 9533 | if (Constant *CSrc = dyn_cast<Constant>(CastOp)) |
| 9534 | if (ASrcTy->getNumElements() != 0) { |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 9535 | Value *Idxs[2]; |
| 9536 | Idxs[0] = Idxs[1] = Constant::getNullValue(Type::Int32Ty); |
| 9537 | CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs, 2); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 9538 | SrcTy = cast<PointerType>(CastOp->getType()); |
| 9539 | SrcPTy = SrcTy->getElementType(); |
| 9540 | } |
| 9541 | |
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 9542 | if ((SrcPTy->isInteger() || isa<PointerType>(SrcPTy) || |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 9543 | isa<VectorType>(SrcPTy)) && |
Chris Lattner | b1515fe | 2005-03-29 06:37:47 +0000 | [diff] [blame] | 9544 | // Do not allow turning this into a load of an integer, which is then |
| 9545 | // casted to a pointer, this pessimizes pointer analysis a lot. |
| 9546 | (isa<PointerType>(SrcPTy) == isa<PointerType>(LI.getType())) && |
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 9547 | IC.getTargetData().getTypeSizeInBits(SrcPTy) == |
| 9548 | IC.getTargetData().getTypeSizeInBits(DestPTy)) { |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9549 | |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 9550 | // Okay, we are casting from one integer or pointer type to another of |
| 9551 | // the same size. Instead of casting the pointer before the load, cast |
| 9552 | // the result of the loaded value. |
| 9553 | Value *NewLoad = IC.InsertNewInstBefore(new LoadInst(CastOp, |
| 9554 | CI->getName(), |
| 9555 | LI.isVolatile()),LI); |
| 9556 | // Now cast the result of the load. |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 9557 | return new BitCastInst(NewLoad, LI.getType()); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 9558 | } |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 9559 | } |
| 9560 | } |
| 9561 | return 0; |
| 9562 | } |
| 9563 | |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 9564 | /// isSafeToLoadUnconditionally - Return true if we know that executing a load |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 9565 | /// from this value cannot trap. If it is not obviously safe to load from the |
| 9566 | /// specified pointer, we do a quick local scan of the basic block containing |
| 9567 | /// ScanFrom, to determine if the address is already accessed. |
| 9568 | static bool isSafeToLoadUnconditionally(Value *V, Instruction *ScanFrom) { |
Duncan Sands | 892c7e4 | 2007-09-19 10:10:31 +0000 | [diff] [blame] | 9569 | // If it is an alloca it is always safe to load from. |
| 9570 | if (isa<AllocaInst>(V)) return true; |
| 9571 | |
Duncan Sands | 46318cd | 2007-09-19 10:25:38 +0000 | [diff] [blame] | 9572 | // 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] | 9573 | if (const GlobalValue *GV = dyn_cast<GlobalVariable>(V)) |
Duncan Sands | 46318cd | 2007-09-19 10:25:38 +0000 | [diff] [blame] | 9574 | // Don't try to evaluate aliases. External weak GV can be null. |
Duncan Sands | 892c7e4 | 2007-09-19 10:10:31 +0000 | [diff] [blame] | 9575 | return !isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage(); |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 9576 | |
| 9577 | // Otherwise, be a little bit agressive by scanning the local block where we |
| 9578 | // 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] | 9579 | // from/to. If so, the previous load or store would have already trapped, |
| 9580 | // so there is no harm doing an extra load (also, CSE will later eliminate |
| 9581 | // the load entirely). |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 9582 | BasicBlock::iterator BBI = ScanFrom, E = ScanFrom->getParent()->begin(); |
| 9583 | |
Alkis Evlogimenos | 7b6ec60 | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 9584 | while (BBI != E) { |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 9585 | --BBI; |
| 9586 | |
| 9587 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { |
| 9588 | if (LI->getOperand(0) == V) return true; |
| 9589 | } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) |
| 9590 | if (SI->getOperand(1) == V) return true; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9591 | |
Alkis Evlogimenos | 7b6ec60 | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 9592 | } |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 9593 | return false; |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 9594 | } |
| 9595 | |
Chris Lattner | 8d2e888 | 2007-08-11 18:48:48 +0000 | [diff] [blame] | 9596 | /// GetUnderlyingObject - Trace through a series of getelementptrs and bitcasts |
| 9597 | /// until we find the underlying object a pointer is referring to or something |
| 9598 | /// we don't understand. Note that the returned pointer may be offset from the |
| 9599 | /// input, because we ignore GEP indices. |
| 9600 | static Value *GetUnderlyingObject(Value *Ptr) { |
| 9601 | while (1) { |
| 9602 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) { |
| 9603 | if (CE->getOpcode() == Instruction::BitCast || |
| 9604 | CE->getOpcode() == Instruction::GetElementPtr) |
| 9605 | Ptr = CE->getOperand(0); |
| 9606 | else |
| 9607 | return Ptr; |
| 9608 | } else if (BitCastInst *BCI = dyn_cast<BitCastInst>(Ptr)) { |
| 9609 | Ptr = BCI->getOperand(0); |
| 9610 | } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) { |
| 9611 | Ptr = GEP->getOperand(0); |
| 9612 | } else { |
| 9613 | return Ptr; |
| 9614 | } |
| 9615 | } |
| 9616 | } |
| 9617 | |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 9618 | Instruction *InstCombiner::visitLoadInst(LoadInst &LI) { |
| 9619 | Value *Op = LI.getOperand(0); |
Chris Lattner | 5f16a13 | 2004-01-12 04:13:56 +0000 | [diff] [blame] | 9620 | |
Dan Gohman | 9941f74 | 2007-07-20 16:34:21 +0000 | [diff] [blame] | 9621 | // Attempt to improve the alignment. |
Chris Lattner | f2369f2 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 9622 | unsigned KnownAlign = GetOrEnforceKnownAlignment(Op, TD); |
Dan Gohman | 9941f74 | 2007-07-20 16:34:21 +0000 | [diff] [blame] | 9623 | if (KnownAlign > LI.getAlignment()) |
| 9624 | LI.setAlignment(KnownAlign); |
| 9625 | |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 9626 | // load (cast X) --> cast (load X) iff safe |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 9627 | if (isa<CastInst>(Op)) |
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 9628 | if (Instruction *Res = InstCombineLoadCast(*this, LI, TD)) |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 9629 | return Res; |
| 9630 | |
| 9631 | // None of the following transforms are legal for volatile loads. |
| 9632 | if (LI.isVolatile()) return 0; |
Chris Lattner | 62f254d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 9633 | |
Chris Lattner | 62f254d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 9634 | if (&LI.getParent()->front() != &LI) { |
| 9635 | BasicBlock::iterator BBI = &LI; --BBI; |
Chris Lattner | 9c1f0fd | 2005-09-12 22:21:03 +0000 | [diff] [blame] | 9636 | // If the instruction immediately before this is a store to the same |
| 9637 | // address, do a simple form of store->load forwarding. |
Chris Lattner | 62f254d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 9638 | if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) |
| 9639 | if (SI->getOperand(1) == LI.getOperand(0)) |
| 9640 | return ReplaceInstUsesWith(LI, SI->getOperand(0)); |
Chris Lattner | 9c1f0fd | 2005-09-12 22:21:03 +0000 | [diff] [blame] | 9641 | if (LoadInst *LIB = dyn_cast<LoadInst>(BBI)) |
| 9642 | if (LIB->getOperand(0) == LI.getOperand(0)) |
| 9643 | return ReplaceInstUsesWith(LI, LIB); |
Chris Lattner | 62f254d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 9644 | } |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 9645 | |
Christopher Lamb | b15147e | 2007-12-29 07:56:53 +0000 | [diff] [blame] | 9646 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) { |
| 9647 | const Value *GEPI0 = GEPI->getOperand(0); |
| 9648 | // TODO: Consider a target hook for valid address spaces for this xform. |
| 9649 | if (isa<ConstantPointerNull>(GEPI0) && |
| 9650 | cast<PointerType>(GEPI0->getType())->getAddressSpace() == 0) { |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 9651 | // Insert a new store to null instruction before the load to indicate |
| 9652 | // that this code is not reachable. We do this instead of inserting |
| 9653 | // an unreachable instruction directly because we cannot modify the |
| 9654 | // CFG. |
| 9655 | new StoreInst(UndefValue::get(LI.getType()), |
| 9656 | Constant::getNullValue(Op->getType()), &LI); |
| 9657 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
| 9658 | } |
Christopher Lamb | b15147e | 2007-12-29 07:56:53 +0000 | [diff] [blame] | 9659 | } |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 9660 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 9661 | if (Constant *C = dyn_cast<Constant>(Op)) { |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 9662 | // load null/undef -> undef |
Christopher Lamb | b15147e | 2007-12-29 07:56:53 +0000 | [diff] [blame] | 9663 | // TODO: Consider a target hook for valid address spaces for this xform. |
| 9664 | if (isa<UndefValue>(C) || (C->isNullValue() && |
| 9665 | cast<PointerType>(Op->getType())->getAddressSpace() == 0)) { |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 9666 | // Insert a new store to null instruction before the load to indicate that |
| 9667 | // this code is not reachable. We do this instead of inserting an |
| 9668 | // unreachable instruction directly because we cannot modify the CFG. |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 9669 | new StoreInst(UndefValue::get(LI.getType()), |
| 9670 | Constant::getNullValue(Op->getType()), &LI); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 9671 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 9672 | } |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 9673 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 9674 | // Instcombine load (constant global) into the value loaded. |
| 9675 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op)) |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 9676 | if (GV->isConstant() && !GV->isDeclaration()) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 9677 | return ReplaceInstUsesWith(LI, GV->getInitializer()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9678 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 9679 | // Instcombine load (constantexpr_GEP global, 0, ...) into the value loaded. |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 9680 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op)) { |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 9681 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 9682 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0))) |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 9683 | if (GV->isConstant() && !GV->isDeclaration()) |
Chris Lattner | 363f2a2 | 2005-09-26 05:28:06 +0000 | [diff] [blame] | 9684 | if (Constant *V = |
| 9685 | ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE)) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 9686 | return ReplaceInstUsesWith(LI, V); |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 9687 | if (CE->getOperand(0)->isNullValue()) { |
| 9688 | // Insert a new store to null instruction before the load to indicate |
| 9689 | // that this code is not reachable. We do this instead of inserting |
| 9690 | // an unreachable instruction directly because we cannot modify the |
| 9691 | // CFG. |
| 9692 | new StoreInst(UndefValue::get(LI.getType()), |
| 9693 | Constant::getNullValue(Op->getType()), &LI); |
| 9694 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
| 9695 | } |
| 9696 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9697 | } else if (CE->isCast()) { |
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 9698 | if (Instruction *Res = InstCombineLoadCast(*this, LI, TD)) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 9699 | return Res; |
| 9700 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 9701 | } |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 9702 | } |
Chris Lattner | 8d2e888 | 2007-08-11 18:48:48 +0000 | [diff] [blame] | 9703 | |
| 9704 | // If this load comes from anywhere in a constant global, and if the global |
| 9705 | // is all undef or zero, we know what it loads. |
| 9706 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GetUnderlyingObject(Op))) { |
| 9707 | if (GV->isConstant() && GV->hasInitializer()) { |
| 9708 | if (GV->getInitializer()->isNullValue()) |
| 9709 | return ReplaceInstUsesWith(LI, Constant::getNullValue(LI.getType())); |
| 9710 | else if (isa<UndefValue>(GV->getInitializer())) |
| 9711 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
| 9712 | } |
| 9713 | } |
Chris Lattner | f499eac | 2004-04-08 20:39:49 +0000 | [diff] [blame] | 9714 | |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 9715 | if (Op->hasOneUse()) { |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 9716 | // Change select and PHI nodes to select values instead of addresses: this |
| 9717 | // helps alias analysis out a lot, allows many others simplifications, and |
| 9718 | // exposes redundancy in the code. |
| 9719 | // |
| 9720 | // Note that we cannot do the transformation unless we know that the |
| 9721 | // introduced loads cannot trap! Something like this is valid as long as |
| 9722 | // the condition is always false: load (select bool %C, int* null, int* %G), |
| 9723 | // but it would not be valid if we transformed it to load from null |
| 9724 | // unconditionally. |
| 9725 | // |
| 9726 | if (SelectInst *SI = dyn_cast<SelectInst>(Op)) { |
| 9727 | // load (select (Cond, &V1, &V2)) --> select(Cond, load &V1, load &V2). |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 9728 | if (isSafeToLoadUnconditionally(SI->getOperand(1), SI) && |
| 9729 | isSafeToLoadUnconditionally(SI->getOperand(2), SI)) { |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 9730 | Value *V1 = InsertNewInstBefore(new LoadInst(SI->getOperand(1), |
Chris Lattner | 79f0c8e | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 9731 | SI->getOperand(1)->getName()+".val"), LI); |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 9732 | Value *V2 = InsertNewInstBefore(new LoadInst(SI->getOperand(2), |
Chris Lattner | 79f0c8e | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 9733 | SI->getOperand(2)->getName()+".val"), LI); |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 9734 | return new SelectInst(SI->getCondition(), V1, V2); |
| 9735 | } |
| 9736 | |
Chris Lattner | 684fe21 | 2004-09-23 15:46:00 +0000 | [diff] [blame] | 9737 | // load (select (cond, null, P)) -> load P |
| 9738 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(1))) |
| 9739 | if (C->isNullValue()) { |
| 9740 | LI.setOperand(0, SI->getOperand(2)); |
| 9741 | return &LI; |
| 9742 | } |
| 9743 | |
| 9744 | // load (select (cond, P, null)) -> load P |
| 9745 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(2))) |
| 9746 | if (C->isNullValue()) { |
| 9747 | LI.setOperand(0, SI->getOperand(1)); |
| 9748 | return &LI; |
| 9749 | } |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 9750 | } |
| 9751 | } |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 9752 | return 0; |
| 9753 | } |
| 9754 | |
Reid Spencer | 55af2b5 | 2007-01-19 21:20:31 +0000 | [diff] [blame] | 9755 | /// InstCombineStoreToCast - Fold store V, (cast P) -> store (cast V), P |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 9756 | /// when possible. |
| 9757 | static Instruction *InstCombineStoreToCast(InstCombiner &IC, StoreInst &SI) { |
| 9758 | User *CI = cast<User>(SI.getOperand(1)); |
| 9759 | Value *CastOp = CI->getOperand(0); |
| 9760 | |
| 9761 | const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType(); |
| 9762 | if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) { |
| 9763 | const Type *SrcPTy = SrcTy->getElementType(); |
| 9764 | |
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 9765 | if (DestPTy->isInteger() || isa<PointerType>(DestPTy)) { |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 9766 | // If the source is an array, the code below will not succeed. Check to |
| 9767 | // see if a trivial 'gep P, 0, 0' will help matters. Only do this for |
| 9768 | // constants. |
| 9769 | if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy)) |
| 9770 | if (Constant *CSrc = dyn_cast<Constant>(CastOp)) |
| 9771 | if (ASrcTy->getNumElements() != 0) { |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 9772 | Value* Idxs[2]; |
| 9773 | Idxs[0] = Idxs[1] = Constant::getNullValue(Type::Int32Ty); |
| 9774 | CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs, 2); |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 9775 | SrcTy = cast<PointerType>(CastOp->getType()); |
| 9776 | SrcPTy = SrcTy->getElementType(); |
| 9777 | } |
| 9778 | |
Reid Spencer | 67f827c | 2007-01-20 23:35:48 +0000 | [diff] [blame] | 9779 | if ((SrcPTy->isInteger() || isa<PointerType>(SrcPTy)) && |
| 9780 | IC.getTargetData().getTypeSizeInBits(SrcPTy) == |
| 9781 | IC.getTargetData().getTypeSizeInBits(DestPTy)) { |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 9782 | |
| 9783 | // 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] | 9784 | // the same size. Instead of casting the pointer before |
| 9785 | // the store, cast the value to be stored. |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 9786 | Value *NewCast; |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 9787 | Value *SIOp0 = SI.getOperand(0); |
Reid Spencer | 7515396 | 2007-01-18 18:54:33 +0000 | [diff] [blame] | 9788 | Instruction::CastOps opcode = Instruction::BitCast; |
| 9789 | const Type* CastSrcTy = SIOp0->getType(); |
| 9790 | const Type* CastDstTy = SrcPTy; |
| 9791 | if (isa<PointerType>(CastDstTy)) { |
| 9792 | if (CastSrcTy->isInteger()) |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 9793 | opcode = Instruction::IntToPtr; |
Reid Spencer | 67f827c | 2007-01-20 23:35:48 +0000 | [diff] [blame] | 9794 | } else if (isa<IntegerType>(CastDstTy)) { |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 9795 | if (isa<PointerType>(SIOp0->getType())) |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 9796 | opcode = Instruction::PtrToInt; |
| 9797 | } |
| 9798 | if (Constant *C = dyn_cast<Constant>(SIOp0)) |
Reid Spencer | 7515396 | 2007-01-18 18:54:33 +0000 | [diff] [blame] | 9799 | NewCast = ConstantExpr::getCast(opcode, C, CastDstTy); |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 9800 | else |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9801 | NewCast = IC.InsertNewInstBefore( |
Reid Spencer | 7515396 | 2007-01-18 18:54:33 +0000 | [diff] [blame] | 9802 | CastInst::create(opcode, SIOp0, CastDstTy, SIOp0->getName()+".c"), |
| 9803 | SI); |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 9804 | return new StoreInst(NewCast, CastOp); |
| 9805 | } |
| 9806 | } |
| 9807 | } |
| 9808 | return 0; |
| 9809 | } |
| 9810 | |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 9811 | Instruction *InstCombiner::visitStoreInst(StoreInst &SI) { |
| 9812 | Value *Val = SI.getOperand(0); |
| 9813 | Value *Ptr = SI.getOperand(1); |
| 9814 | |
| 9815 | if (isa<UndefValue>(Ptr)) { // store X, undef -> noop (even if volatile) |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 9816 | EraseInstFromFunction(SI); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 9817 | ++NumCombined; |
| 9818 | return 0; |
| 9819 | } |
Chris Lattner | 836692d | 2007-01-15 06:51:56 +0000 | [diff] [blame] | 9820 | |
| 9821 | // If the RHS is an alloca with a single use, zapify the store, making the |
| 9822 | // alloca dead. |
| 9823 | if (Ptr->hasOneUse()) { |
| 9824 | if (isa<AllocaInst>(Ptr)) { |
| 9825 | EraseInstFromFunction(SI); |
| 9826 | ++NumCombined; |
| 9827 | return 0; |
| 9828 | } |
| 9829 | |
| 9830 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) |
| 9831 | if (isa<AllocaInst>(GEP->getOperand(0)) && |
| 9832 | GEP->getOperand(0)->hasOneUse()) { |
| 9833 | EraseInstFromFunction(SI); |
| 9834 | ++NumCombined; |
| 9835 | return 0; |
| 9836 | } |
| 9837 | } |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 9838 | |
Dan Gohman | 9941f74 | 2007-07-20 16:34:21 +0000 | [diff] [blame] | 9839 | // Attempt to improve the alignment. |
Chris Lattner | f2369f2 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 9840 | unsigned KnownAlign = GetOrEnforceKnownAlignment(Ptr, TD); |
Dan Gohman | 9941f74 | 2007-07-20 16:34:21 +0000 | [diff] [blame] | 9841 | if (KnownAlign > SI.getAlignment()) |
| 9842 | SI.setAlignment(KnownAlign); |
| 9843 | |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 9844 | // Do really simple DSE, to catch cases where there are several consequtive |
| 9845 | // stores to the same location, separated by a few arithmetic operations. This |
| 9846 | // situation often occurs with bitfield accesses. |
| 9847 | BasicBlock::iterator BBI = &SI; |
| 9848 | for (unsigned ScanInsts = 6; BBI != SI.getParent()->begin() && ScanInsts; |
| 9849 | --ScanInsts) { |
| 9850 | --BBI; |
| 9851 | |
| 9852 | if (StoreInst *PrevSI = dyn_cast<StoreInst>(BBI)) { |
| 9853 | // Prev store isn't volatile, and stores to the same location? |
| 9854 | if (!PrevSI->isVolatile() && PrevSI->getOperand(1) == SI.getOperand(1)) { |
| 9855 | ++NumDeadStore; |
| 9856 | ++BBI; |
| 9857 | EraseInstFromFunction(*PrevSI); |
| 9858 | continue; |
| 9859 | } |
| 9860 | break; |
| 9861 | } |
| 9862 | |
Chris Lattner | b4db97f | 2006-05-26 19:19:20 +0000 | [diff] [blame] | 9863 | // If this is a load, we have to stop. However, if the loaded value is from |
| 9864 | // the pointer we're loading and is producing the pointer we're storing, |
| 9865 | // then *this* store is dead (X = load P; store X -> P). |
| 9866 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { |
Chris Lattner | a54c7eb | 2007-09-07 05:33:03 +0000 | [diff] [blame] | 9867 | if (LI == Val && LI->getOperand(0) == Ptr && !SI.isVolatile()) { |
Chris Lattner | b4db97f | 2006-05-26 19:19:20 +0000 | [diff] [blame] | 9868 | EraseInstFromFunction(SI); |
| 9869 | ++NumCombined; |
| 9870 | return 0; |
| 9871 | } |
| 9872 | // Otherwise, this is a load from some other location. Stores before it |
| 9873 | // may not be dead. |
| 9874 | break; |
| 9875 | } |
| 9876 | |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 9877 | // Don't skip over loads or things that can modify memory. |
Chris Lattner | b4db97f | 2006-05-26 19:19:20 +0000 | [diff] [blame] | 9878 | if (BBI->mayWriteToMemory()) |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 9879 | break; |
| 9880 | } |
| 9881 | |
| 9882 | |
| 9883 | if (SI.isVolatile()) return 0; // Don't hack volatile stores. |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 9884 | |
| 9885 | // store X, null -> turns into 'unreachable' in SimplifyCFG |
| 9886 | if (isa<ConstantPointerNull>(Ptr)) { |
| 9887 | if (!isa<UndefValue>(Val)) { |
| 9888 | SI.setOperand(0, UndefValue::get(Val->getType())); |
| 9889 | if (Instruction *U = dyn_cast<Instruction>(Val)) |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9890 | AddToWorkList(U); // Dropped a use. |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 9891 | ++NumCombined; |
| 9892 | } |
| 9893 | return 0; // Do not modify these! |
| 9894 | } |
| 9895 | |
| 9896 | // store undef, Ptr -> noop |
| 9897 | if (isa<UndefValue>(Val)) { |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 9898 | EraseInstFromFunction(SI); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 9899 | ++NumCombined; |
| 9900 | return 0; |
| 9901 | } |
| 9902 | |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 9903 | // If the pointer destination is a cast, see if we can fold the cast into the |
| 9904 | // source instead. |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 9905 | if (isa<CastInst>(Ptr)) |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 9906 | if (Instruction *Res = InstCombineStoreToCast(*this, SI)) |
| 9907 | return Res; |
| 9908 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9909 | if (CE->isCast()) |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 9910 | if (Instruction *Res = InstCombineStoreToCast(*this, SI)) |
| 9911 | return Res; |
| 9912 | |
Chris Lattner | 408902b | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 9913 | |
| 9914 | // If this store is the last instruction in the basic block, and if the block |
| 9915 | // 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] | 9916 | BBI = &SI; ++BBI; |
Chris Lattner | 408902b | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 9917 | if (BranchInst *BI = dyn_cast<BranchInst>(BBI)) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 9918 | if (BI->isUnconditional()) |
| 9919 | if (SimplifyStoreAtEndOfBlock(SI)) |
| 9920 | return 0; // xform done! |
Chris Lattner | 408902b | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 9921 | |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 9922 | return 0; |
| 9923 | } |
| 9924 | |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 9925 | /// SimplifyStoreAtEndOfBlock - Turn things like: |
| 9926 | /// if () { *P = v1; } else { *P = v2 } |
| 9927 | /// into a phi node with a store in the successor. |
| 9928 | /// |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 9929 | /// Simplify things like: |
| 9930 | /// *P = v1; if () { *P = v2; } |
| 9931 | /// into a phi node with a store in the successor. |
| 9932 | /// |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 9933 | bool InstCombiner::SimplifyStoreAtEndOfBlock(StoreInst &SI) { |
| 9934 | BasicBlock *StoreBB = SI.getParent(); |
| 9935 | |
| 9936 | // Check to see if the successor block has exactly two incoming edges. If |
| 9937 | // so, see if the other predecessor contains a store to the same location. |
| 9938 | // 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] | 9939 | BasicBlock *DestBB = StoreBB->getTerminator()->getSuccessor(0); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 9940 | |
| 9941 | // Determine whether Dest has exactly two predecessors and, if so, compute |
| 9942 | // the other predecessor. |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 9943 | pred_iterator PI = pred_begin(DestBB); |
| 9944 | BasicBlock *OtherBB = 0; |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 9945 | if (*PI != StoreBB) |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 9946 | OtherBB = *PI; |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 9947 | ++PI; |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 9948 | if (PI == pred_end(DestBB)) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 9949 | return false; |
| 9950 | |
| 9951 | if (*PI != StoreBB) { |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 9952 | if (OtherBB) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 9953 | return false; |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 9954 | OtherBB = *PI; |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 9955 | } |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 9956 | if (++PI != pred_end(DestBB)) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 9957 | return false; |
| 9958 | |
| 9959 | |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 9960 | // Verify that the other block ends in a branch and is not otherwise empty. |
| 9961 | BasicBlock::iterator BBI = OtherBB->getTerminator(); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 9962 | BranchInst *OtherBr = dyn_cast<BranchInst>(BBI); |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 9963 | if (!OtherBr || BBI == OtherBB->begin()) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 9964 | return false; |
| 9965 | |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 9966 | // If the other block ends in an unconditional branch, check for the 'if then |
| 9967 | // else' case. there is an instruction before the branch. |
| 9968 | StoreInst *OtherStore = 0; |
| 9969 | if (OtherBr->isUnconditional()) { |
| 9970 | // If this isn't a store, or isn't a store to the same location, bail out. |
| 9971 | --BBI; |
| 9972 | OtherStore = dyn_cast<StoreInst>(BBI); |
| 9973 | if (!OtherStore || OtherStore->getOperand(1) != SI.getOperand(1)) |
| 9974 | return false; |
| 9975 | } else { |
Chris Lattner | d717c18 | 2007-05-05 22:32:24 +0000 | [diff] [blame] | 9976 | // 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] | 9977 | // destinations is StoreBB, then we have the if/then case. |
| 9978 | if (OtherBr->getSuccessor(0) != StoreBB && |
| 9979 | OtherBr->getSuccessor(1) != StoreBB) |
| 9980 | return false; |
| 9981 | |
| 9982 | // 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] | 9983 | // if/then triangle. See if there is a store to the same ptr as SI that |
| 9984 | // lives in OtherBB. |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 9985 | for (;; --BBI) { |
| 9986 | // Check to see if we find the matching store. |
| 9987 | if ((OtherStore = dyn_cast<StoreInst>(BBI))) { |
| 9988 | if (OtherStore->getOperand(1) != SI.getOperand(1)) |
| 9989 | return false; |
| 9990 | break; |
| 9991 | } |
Chris Lattner | d717c18 | 2007-05-05 22:32:24 +0000 | [diff] [blame] | 9992 | // If we find something that may be using the stored value, or if we run |
| 9993 | // out of instructions, we can't do the xform. |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 9994 | if (isa<LoadInst>(BBI) || BBI->mayWriteToMemory() || |
| 9995 | BBI == OtherBB->begin()) |
| 9996 | return false; |
| 9997 | } |
| 9998 | |
| 9999 | // In order to eliminate the store in OtherBr, we have to |
| 10000 | // make sure nothing reads the stored value in StoreBB. |
| 10001 | for (BasicBlock::iterator I = StoreBB->begin(); &*I != &SI; ++I) { |
| 10002 | // FIXME: This should really be AA driven. |
| 10003 | if (isa<LoadInst>(I) || I->mayWriteToMemory()) |
| 10004 | return false; |
| 10005 | } |
| 10006 | } |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10007 | |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10008 | // Insert a PHI node now if we need it. |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10009 | Value *MergedVal = OtherStore->getOperand(0); |
| 10010 | if (MergedVal != SI.getOperand(0)) { |
| 10011 | PHINode *PN = new PHINode(MergedVal->getType(), "storemerge"); |
| 10012 | PN->reserveOperandSpace(2); |
| 10013 | PN->addIncoming(SI.getOperand(0), SI.getParent()); |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10014 | PN->addIncoming(OtherStore->getOperand(0), OtherBB); |
| 10015 | MergedVal = InsertNewInstBefore(PN, DestBB->front()); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10016 | } |
| 10017 | |
| 10018 | // Advance to a place where it is safe to insert the new store and |
| 10019 | // insert it. |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10020 | BBI = DestBB->begin(); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10021 | while (isa<PHINode>(BBI)) ++BBI; |
| 10022 | InsertNewInstBefore(new StoreInst(MergedVal, SI.getOperand(1), |
| 10023 | OtherStore->isVolatile()), *BBI); |
| 10024 | |
| 10025 | // Nuke the old stores. |
| 10026 | EraseInstFromFunction(SI); |
| 10027 | EraseInstFromFunction(*OtherStore); |
| 10028 | ++NumCombined; |
| 10029 | return true; |
| 10030 | } |
| 10031 | |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10032 | |
Chris Lattner | c4d10eb | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 10033 | Instruction *InstCombiner::visitBranchInst(BranchInst &BI) { |
| 10034 | // 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] | 10035 | Value *X = 0; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 10036 | BasicBlock *TrueDest; |
| 10037 | BasicBlock *FalseDest; |
| 10038 | if (match(&BI, m_Br(m_Not(m_Value(X)), TrueDest, FalseDest)) && |
| 10039 | !isa<Constant>(X)) { |
| 10040 | // Swap Destinations and condition... |
| 10041 | BI.setCondition(X); |
| 10042 | BI.setSuccessor(0, FalseDest); |
| 10043 | BI.setSuccessor(1, TrueDest); |
| 10044 | return &BI; |
| 10045 | } |
| 10046 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10047 | // Cannonicalize fcmp_one -> fcmp_oeq |
| 10048 | FCmpInst::Predicate FPred; Value *Y; |
| 10049 | if (match(&BI, m_Br(m_FCmp(FPred, m_Value(X), m_Value(Y)), |
| 10050 | TrueDest, FalseDest))) |
| 10051 | if ((FPred == FCmpInst::FCMP_ONE || FPred == FCmpInst::FCMP_OLE || |
| 10052 | FPred == FCmpInst::FCMP_OGE) && BI.getCondition()->hasOneUse()) { |
| 10053 | FCmpInst *I = cast<FCmpInst>(BI.getCondition()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10054 | FCmpInst::Predicate NewPred = FCmpInst::getInversePredicate(FPred); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 10055 | Instruction *NewSCC = new FCmpInst(NewPred, X, Y, "", I); |
| 10056 | NewSCC->takeName(I); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10057 | // Swap Destinations and condition... |
| 10058 | BI.setCondition(NewSCC); |
| 10059 | BI.setSuccessor(0, FalseDest); |
| 10060 | BI.setSuccessor(1, TrueDest); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10061 | RemoveFromWorkList(I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 10062 | I->eraseFromParent(); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10063 | AddToWorkList(NewSCC); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10064 | return &BI; |
| 10065 | } |
| 10066 | |
| 10067 | // Cannonicalize icmp_ne -> icmp_eq |
| 10068 | ICmpInst::Predicate IPred; |
| 10069 | if (match(&BI, m_Br(m_ICmp(IPred, m_Value(X), m_Value(Y)), |
| 10070 | TrueDest, FalseDest))) |
| 10071 | if ((IPred == ICmpInst::ICMP_NE || IPred == ICmpInst::ICMP_ULE || |
| 10072 | IPred == ICmpInst::ICMP_SLE || IPred == ICmpInst::ICMP_UGE || |
| 10073 | IPred == ICmpInst::ICMP_SGE) && BI.getCondition()->hasOneUse()) { |
| 10074 | ICmpInst *I = cast<ICmpInst>(BI.getCondition()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10075 | ICmpInst::Predicate NewPred = ICmpInst::getInversePredicate(IPred); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 10076 | Instruction *NewSCC = new ICmpInst(NewPred, X, Y, "", I); |
| 10077 | NewSCC->takeName(I); |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 10078 | // Swap Destinations and condition... |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 10079 | BI.setCondition(NewSCC); |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 10080 | BI.setSuccessor(0, FalseDest); |
| 10081 | BI.setSuccessor(1, TrueDest); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10082 | RemoveFromWorkList(I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 10083 | I->eraseFromParent();; |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10084 | AddToWorkList(NewSCC); |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 10085 | return &BI; |
| 10086 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 10087 | |
Chris Lattner | c4d10eb | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 10088 | return 0; |
| 10089 | } |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 10090 | |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 10091 | Instruction *InstCombiner::visitSwitchInst(SwitchInst &SI) { |
| 10092 | Value *Cond = SI.getCondition(); |
| 10093 | if (Instruction *I = dyn_cast<Instruction>(Cond)) { |
| 10094 | if (I->getOpcode() == Instruction::Add) |
| 10095 | if (ConstantInt *AddRHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 10096 | // change 'switch (X+4) case 1:' into 'switch (X) case -3' |
| 10097 | for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10098 | SI.setOperand(i,ConstantExpr::getSub(cast<Constant>(SI.getOperand(i)), |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 10099 | AddRHS)); |
| 10100 | SI.setOperand(0, I->getOperand(0)); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10101 | AddToWorkList(I); |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 10102 | return &SI; |
| 10103 | } |
| 10104 | } |
| 10105 | return 0; |
| 10106 | } |
| 10107 | |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10108 | /// CheapToScalarize - Return true if the value is cheaper to scalarize than it |
| 10109 | /// is to leave as a vector operation. |
| 10110 | static bool CheapToScalarize(Value *V, bool isConstant) { |
| 10111 | if (isa<ConstantAggregateZero>(V)) |
| 10112 | return true; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10113 | if (ConstantVector *C = dyn_cast<ConstantVector>(V)) { |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10114 | if (isConstant) return true; |
| 10115 | // If all elts are the same, we can extract. |
| 10116 | Constant *Op0 = C->getOperand(0); |
| 10117 | for (unsigned i = 1; i < C->getNumOperands(); ++i) |
| 10118 | if (C->getOperand(i) != Op0) |
| 10119 | return false; |
| 10120 | return true; |
| 10121 | } |
| 10122 | Instruction *I = dyn_cast<Instruction>(V); |
| 10123 | if (!I) return false; |
| 10124 | |
| 10125 | // Insert element gets simplified to the inserted element or is deleted if |
| 10126 | // this is constant idx extract element and its a constant idx insertelt. |
| 10127 | if (I->getOpcode() == Instruction::InsertElement && isConstant && |
| 10128 | isa<ConstantInt>(I->getOperand(2))) |
| 10129 | return true; |
| 10130 | if (I->getOpcode() == Instruction::Load && I->hasOneUse()) |
| 10131 | return true; |
| 10132 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) |
| 10133 | if (BO->hasOneUse() && |
| 10134 | (CheapToScalarize(BO->getOperand(0), isConstant) || |
| 10135 | CheapToScalarize(BO->getOperand(1), isConstant))) |
| 10136 | return true; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10137 | if (CmpInst *CI = dyn_cast<CmpInst>(I)) |
| 10138 | if (CI->hasOneUse() && |
| 10139 | (CheapToScalarize(CI->getOperand(0), isConstant) || |
| 10140 | CheapToScalarize(CI->getOperand(1), isConstant))) |
| 10141 | return true; |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10142 | |
| 10143 | return false; |
| 10144 | } |
| 10145 | |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 10146 | /// Read and decode a shufflevector mask. |
| 10147 | /// |
| 10148 | /// It turns undef elements into values that are larger than the number of |
| 10149 | /// elements in the input. |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10150 | static std::vector<unsigned> getShuffleMask(const ShuffleVectorInst *SVI) { |
| 10151 | unsigned NElts = SVI->getType()->getNumElements(); |
| 10152 | if (isa<ConstantAggregateZero>(SVI->getOperand(2))) |
| 10153 | return std::vector<unsigned>(NElts, 0); |
| 10154 | if (isa<UndefValue>(SVI->getOperand(2))) |
| 10155 | return std::vector<unsigned>(NElts, 2*NElts); |
| 10156 | |
| 10157 | std::vector<unsigned> Result; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10158 | const ConstantVector *CP = cast<ConstantVector>(SVI->getOperand(2)); |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10159 | for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) |
| 10160 | if (isa<UndefValue>(CP->getOperand(i))) |
| 10161 | Result.push_back(NElts*2); // undef -> 8 |
| 10162 | else |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10163 | Result.push_back(cast<ConstantInt>(CP->getOperand(i))->getZExtValue()); |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10164 | return Result; |
| 10165 | } |
| 10166 | |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10167 | /// FindScalarElement - Given a vector and an element number, see if the scalar |
| 10168 | /// value is already around as a register, for example if it were inserted then |
| 10169 | /// extracted from the vector. |
| 10170 | static Value *FindScalarElement(Value *V, unsigned EltNo) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10171 | assert(isa<VectorType>(V->getType()) && "Not looking at a vector?"); |
| 10172 | const VectorType *PTy = cast<VectorType>(V->getType()); |
Chris Lattner | 389a6f5 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 10173 | unsigned Width = PTy->getNumElements(); |
| 10174 | if (EltNo >= Width) // Out of range access. |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10175 | return UndefValue::get(PTy->getElementType()); |
| 10176 | |
| 10177 | if (isa<UndefValue>(V)) |
| 10178 | return UndefValue::get(PTy->getElementType()); |
| 10179 | else if (isa<ConstantAggregateZero>(V)) |
| 10180 | return Constant::getNullValue(PTy->getElementType()); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10181 | else if (ConstantVector *CP = dyn_cast<ConstantVector>(V)) |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10182 | return CP->getOperand(EltNo); |
| 10183 | else if (InsertElementInst *III = dyn_cast<InsertElementInst>(V)) { |
| 10184 | // 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] | 10185 | if (!isa<ConstantInt>(III->getOperand(2))) |
| 10186 | return 0; |
| 10187 | unsigned IIElt = cast<ConstantInt>(III->getOperand(2))->getZExtValue(); |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10188 | |
| 10189 | // If this is an insert to the element we are looking for, return the |
| 10190 | // inserted value. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10191 | if (EltNo == IIElt) |
| 10192 | return III->getOperand(1); |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10193 | |
| 10194 | // Otherwise, the insertelement doesn't modify the value, recurse on its |
| 10195 | // vector input. |
| 10196 | return FindScalarElement(III->getOperand(0), EltNo); |
Chris Lattner | 389a6f5 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 10197 | } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(V)) { |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10198 | unsigned InEl = getShuffleMask(SVI)[EltNo]; |
| 10199 | if (InEl < Width) |
| 10200 | return FindScalarElement(SVI->getOperand(0), InEl); |
| 10201 | else if (InEl < Width*2) |
| 10202 | return FindScalarElement(SVI->getOperand(1), InEl - Width); |
| 10203 | else |
| 10204 | return UndefValue::get(PTy->getElementType()); |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10205 | } |
| 10206 | |
| 10207 | // Otherwise, we don't know. |
| 10208 | return 0; |
| 10209 | } |
| 10210 | |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10211 | Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) { |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10212 | |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 10213 | // If vector val is undef, replace extract with scalar undef. |
Chris Lattner | 1f13c88 | 2006-03-31 18:25:14 +0000 | [diff] [blame] | 10214 | if (isa<UndefValue>(EI.getOperand(0))) |
| 10215 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); |
| 10216 | |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 10217 | // If vector val is constant 0, replace extract with scalar 0. |
Chris Lattner | 1f13c88 | 2006-03-31 18:25:14 +0000 | [diff] [blame] | 10218 | if (isa<ConstantAggregateZero>(EI.getOperand(0))) |
| 10219 | return ReplaceInstUsesWith(EI, Constant::getNullValue(EI.getType())); |
| 10220 | |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10221 | if (ConstantVector *C = dyn_cast<ConstantVector>(EI.getOperand(0))) { |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 10222 | // If vector val is constant with uniform operands, replace EI |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10223 | // with that operand |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10224 | Constant *op0 = C->getOperand(0); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10225 | for (unsigned i = 1; i < C->getNumOperands(); ++i) |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10226 | if (C->getOperand(i) != op0) { |
| 10227 | op0 = 0; |
| 10228 | break; |
| 10229 | } |
| 10230 | if (op0) |
| 10231 | return ReplaceInstUsesWith(EI, op0); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10232 | } |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10233 | |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10234 | // If extracting a specified index from the vector, see if we can recursively |
| 10235 | // find a previously computed scalar that was inserted into the vector. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10236 | if (ConstantInt *IdxC = dyn_cast<ConstantInt>(EI.getOperand(1))) { |
Chris Lattner | 8546409 | 2007-04-09 01:37:55 +0000 | [diff] [blame] | 10237 | unsigned IndexVal = IdxC->getZExtValue(); |
| 10238 | unsigned VectorWidth = |
| 10239 | cast<VectorType>(EI.getOperand(0)->getType())->getNumElements(); |
| 10240 | |
| 10241 | // If this is extracting an invalid index, turn this into undef, to avoid |
| 10242 | // crashing the code below. |
| 10243 | if (IndexVal >= VectorWidth) |
| 10244 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); |
| 10245 | |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 10246 | // This instruction only demands the single element from the input vector. |
| 10247 | // If the input vector has a single use, simplify it based on this use |
| 10248 | // property. |
Chris Lattner | 8546409 | 2007-04-09 01:37:55 +0000 | [diff] [blame] | 10249 | if (EI.getOperand(0)->hasOneUse() && VectorWidth != 1) { |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 10250 | uint64_t UndefElts; |
| 10251 | if (Value *V = SimplifyDemandedVectorElts(EI.getOperand(0), |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10252 | 1 << IndexVal, |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 10253 | UndefElts)) { |
| 10254 | EI.setOperand(0, V); |
| 10255 | return &EI; |
| 10256 | } |
| 10257 | } |
| 10258 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10259 | if (Value *Elt = FindScalarElement(EI.getOperand(0), IndexVal)) |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10260 | return ReplaceInstUsesWith(EI, Elt); |
Chris Lattner | b7300fa | 2007-04-14 23:02:14 +0000 | [diff] [blame] | 10261 | |
| 10262 | // If the this extractelement is directly using a bitcast from a vector of |
| 10263 | // the same number of elements, see if we can find the source element from |
| 10264 | // it. In this case, we will end up needing to bitcast the scalars. |
| 10265 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(EI.getOperand(0))) { |
| 10266 | if (const VectorType *VT = |
| 10267 | dyn_cast<VectorType>(BCI->getOperand(0)->getType())) |
| 10268 | if (VT->getNumElements() == VectorWidth) |
| 10269 | if (Value *Elt = FindScalarElement(BCI->getOperand(0), IndexVal)) |
| 10270 | return new BitCastInst(Elt, EI.getType()); |
| 10271 | } |
Chris Lattner | 389a6f5 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 10272 | } |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10273 | |
Chris Lattner | 73fa49d | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 10274 | if (Instruction *I = dyn_cast<Instruction>(EI.getOperand(0))) { |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10275 | if (I->hasOneUse()) { |
| 10276 | // Push extractelement into predecessor operation if legal and |
| 10277 | // profitable to do so |
| 10278 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) { |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10279 | bool isConstantElt = isa<ConstantInt>(EI.getOperand(1)); |
| 10280 | if (CheapToScalarize(BO, isConstantElt)) { |
| 10281 | ExtractElementInst *newEI0 = |
| 10282 | new ExtractElementInst(BO->getOperand(0), EI.getOperand(1), |
| 10283 | EI.getName()+".lhs"); |
| 10284 | ExtractElementInst *newEI1 = |
| 10285 | new ExtractElementInst(BO->getOperand(1), EI.getOperand(1), |
| 10286 | EI.getName()+".rhs"); |
| 10287 | InsertNewInstBefore(newEI0, EI); |
| 10288 | InsertNewInstBefore(newEI1, EI); |
| 10289 | return BinaryOperator::create(BO->getOpcode(), newEI0, newEI1); |
| 10290 | } |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 10291 | } else if (isa<LoadInst>(I)) { |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 10292 | unsigned AS = |
| 10293 | cast<PointerType>(I->getOperand(0)->getType())->getAddressSpace(); |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 10294 | Value *Ptr = InsertBitCastBefore(I->getOperand(0), |
| 10295 | PointerType::get(EI.getType(), AS),EI); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10296 | GetElementPtrInst *GEP = |
Reid Spencer | de33124 | 2006-11-29 01:11:01 +0000 | [diff] [blame] | 10297 | new GetElementPtrInst(Ptr, EI.getOperand(1), I->getName() + ".gep"); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10298 | InsertNewInstBefore(GEP, EI); |
| 10299 | return new LoadInst(GEP); |
Chris Lattner | 73fa49d | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 10300 | } |
| 10301 | } |
| 10302 | if (InsertElementInst *IE = dyn_cast<InsertElementInst>(I)) { |
| 10303 | // Extracting the inserted element? |
| 10304 | if (IE->getOperand(2) == EI.getOperand(1)) |
| 10305 | return ReplaceInstUsesWith(EI, IE->getOperand(1)); |
| 10306 | // If the inserted and extracted elements are constants, they must not |
| 10307 | // be the same value, extract from the pre-inserted value instead. |
| 10308 | if (isa<Constant>(IE->getOperand(2)) && |
| 10309 | isa<Constant>(EI.getOperand(1))) { |
| 10310 | AddUsesToWorkList(EI); |
| 10311 | EI.setOperand(0, IE->getOperand(0)); |
| 10312 | return &EI; |
| 10313 | } |
| 10314 | } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(I)) { |
| 10315 | // If this is extracting an element from a shufflevector, figure out where |
| 10316 | // it came from and extract from the appropriate input element instead. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10317 | if (ConstantInt *Elt = dyn_cast<ConstantInt>(EI.getOperand(1))) { |
| 10318 | unsigned SrcIdx = getShuffleMask(SVI)[Elt->getZExtValue()]; |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10319 | Value *Src; |
| 10320 | if (SrcIdx < SVI->getType()->getNumElements()) |
| 10321 | Src = SVI->getOperand(0); |
| 10322 | else if (SrcIdx < SVI->getType()->getNumElements()*2) { |
| 10323 | SrcIdx -= SVI->getType()->getNumElements(); |
| 10324 | Src = SVI->getOperand(1); |
| 10325 | } else { |
| 10326 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); |
Chris Lattner | df084ff | 2006-03-30 22:02:40 +0000 | [diff] [blame] | 10327 | } |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 10328 | return new ExtractElementInst(Src, SrcIdx); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10329 | } |
| 10330 | } |
Chris Lattner | 73fa49d | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 10331 | } |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10332 | return 0; |
| 10333 | } |
| 10334 | |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10335 | /// CollectSingleShuffleElements - If V is a shuffle of values that ONLY returns |
| 10336 | /// elements from either LHS or RHS, return the shuffle mask and true. |
| 10337 | /// Otherwise, return false. |
| 10338 | static bool CollectSingleShuffleElements(Value *V, Value *LHS, Value *RHS, |
| 10339 | std::vector<Constant*> &Mask) { |
| 10340 | assert(V->getType() == LHS->getType() && V->getType() == RHS->getType() && |
| 10341 | "Invalid CollectSingleShuffleElements"); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10342 | unsigned NumElts = cast<VectorType>(V->getType())->getNumElements(); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10343 | |
| 10344 | if (isa<UndefValue>(V)) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10345 | Mask.assign(NumElts, UndefValue::get(Type::Int32Ty)); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10346 | return true; |
| 10347 | } else if (V == LHS) { |
| 10348 | for (unsigned i = 0; i != NumElts; ++i) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10349 | Mask.push_back(ConstantInt::get(Type::Int32Ty, i)); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10350 | return true; |
| 10351 | } else if (V == RHS) { |
| 10352 | for (unsigned i = 0; i != NumElts; ++i) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10353 | Mask.push_back(ConstantInt::get(Type::Int32Ty, i+NumElts)); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10354 | return true; |
| 10355 | } else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) { |
| 10356 | // If this is an insert of an extract from some other vector, include it. |
| 10357 | Value *VecOp = IEI->getOperand(0); |
| 10358 | Value *ScalarOp = IEI->getOperand(1); |
| 10359 | Value *IdxOp = IEI->getOperand(2); |
| 10360 | |
Chris Lattner | d929f06 | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 10361 | if (!isa<ConstantInt>(IdxOp)) |
| 10362 | return false; |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10363 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Chris Lattner | d929f06 | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 10364 | |
| 10365 | if (isa<UndefValue>(ScalarOp)) { // inserting undef into vector. |
| 10366 | // Okay, we can handle this if the vector we are insertinting into is |
| 10367 | // transitively ok. |
| 10368 | if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) { |
| 10369 | // If so, update the mask to reflect the inserted undef. |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10370 | Mask[InsertedIdx] = UndefValue::get(Type::Int32Ty); |
Chris Lattner | d929f06 | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 10371 | return true; |
| 10372 | } |
| 10373 | } else if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)){ |
| 10374 | if (isa<ConstantInt>(EI->getOperand(1)) && |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10375 | EI->getOperand(0)->getType() == V->getType()) { |
| 10376 | unsigned ExtractedIdx = |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10377 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10378 | |
| 10379 | // This must be extracting from either LHS or RHS. |
| 10380 | if (EI->getOperand(0) == LHS || EI->getOperand(0) == RHS) { |
| 10381 | // Okay, we can handle this if the vector we are insertinting into is |
| 10382 | // transitively ok. |
| 10383 | if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) { |
| 10384 | // If so, update the mask to reflect the inserted value. |
| 10385 | if (EI->getOperand(0) == LHS) { |
| 10386 | Mask[InsertedIdx & (NumElts-1)] = |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10387 | ConstantInt::get(Type::Int32Ty, ExtractedIdx); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10388 | } else { |
| 10389 | assert(EI->getOperand(0) == RHS); |
| 10390 | Mask[InsertedIdx & (NumElts-1)] = |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10391 | ConstantInt::get(Type::Int32Ty, ExtractedIdx+NumElts); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10392 | |
| 10393 | } |
| 10394 | return true; |
| 10395 | } |
| 10396 | } |
| 10397 | } |
| 10398 | } |
| 10399 | } |
| 10400 | // TODO: Handle shufflevector here! |
| 10401 | |
| 10402 | return false; |
| 10403 | } |
| 10404 | |
| 10405 | /// CollectShuffleElements - We are building a shuffle of V, using RHS as the |
| 10406 | /// RHS of the shuffle instruction, if it is not null. Return a shuffle mask |
| 10407 | /// that computes V and the LHS value of the shuffle. |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10408 | static Value *CollectShuffleElements(Value *V, std::vector<Constant*> &Mask, |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10409 | Value *&RHS) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10410 | assert(isa<VectorType>(V->getType()) && |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10411 | (RHS == 0 || V->getType() == RHS->getType()) && |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10412 | "Invalid shuffle!"); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10413 | unsigned NumElts = cast<VectorType>(V->getType())->getNumElements(); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10414 | |
| 10415 | if (isa<UndefValue>(V)) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10416 | Mask.assign(NumElts, UndefValue::get(Type::Int32Ty)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10417 | return V; |
| 10418 | } else if (isa<ConstantAggregateZero>(V)) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10419 | Mask.assign(NumElts, ConstantInt::get(Type::Int32Ty, 0)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10420 | return V; |
| 10421 | } else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) { |
| 10422 | // If this is an insert of an extract from some other vector, include it. |
| 10423 | Value *VecOp = IEI->getOperand(0); |
| 10424 | Value *ScalarOp = IEI->getOperand(1); |
| 10425 | Value *IdxOp = IEI->getOperand(2); |
| 10426 | |
| 10427 | if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) { |
| 10428 | if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) && |
| 10429 | EI->getOperand(0)->getType() == V->getType()) { |
| 10430 | unsigned ExtractedIdx = |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10431 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
| 10432 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10433 | |
| 10434 | // Either the extracted from or inserted into vector must be RHSVec, |
| 10435 | // otherwise we'd end up with a shuffle of three inputs. |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10436 | if (EI->getOperand(0) == RHS || RHS == 0) { |
| 10437 | RHS = EI->getOperand(0); |
| 10438 | Value *V = CollectShuffleElements(VecOp, Mask, RHS); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10439 | Mask[InsertedIdx & (NumElts-1)] = |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10440 | ConstantInt::get(Type::Int32Ty, NumElts+ExtractedIdx); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10441 | return V; |
| 10442 | } |
| 10443 | |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10444 | if (VecOp == RHS) { |
| 10445 | Value *V = CollectShuffleElements(EI->getOperand(0), Mask, RHS); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10446 | // Everything but the extracted element is replaced with the RHS. |
| 10447 | for (unsigned i = 0; i != NumElts; ++i) { |
| 10448 | if (i != InsertedIdx) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10449 | Mask[i] = ConstantInt::get(Type::Int32Ty, NumElts+i); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10450 | } |
| 10451 | return V; |
| 10452 | } |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10453 | |
| 10454 | // If this insertelement is a chain that comes from exactly these two |
| 10455 | // vectors, return the vector and the effective shuffle. |
| 10456 | if (CollectSingleShuffleElements(IEI, EI->getOperand(0), RHS, Mask)) |
| 10457 | return EI->getOperand(0); |
| 10458 | |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10459 | } |
| 10460 | } |
| 10461 | } |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10462 | // TODO: Handle shufflevector here! |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10463 | |
| 10464 | // Otherwise, can't do anything fancy. Return an identity vector. |
| 10465 | for (unsigned i = 0; i != NumElts; ++i) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10466 | Mask.push_back(ConstantInt::get(Type::Int32Ty, i)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10467 | return V; |
| 10468 | } |
| 10469 | |
| 10470 | Instruction *InstCombiner::visitInsertElementInst(InsertElementInst &IE) { |
| 10471 | Value *VecOp = IE.getOperand(0); |
| 10472 | Value *ScalarOp = IE.getOperand(1); |
| 10473 | Value *IdxOp = IE.getOperand(2); |
| 10474 | |
Chris Lattner | 599ded1 | 2007-04-09 01:11:16 +0000 | [diff] [blame] | 10475 | // Inserting an undef or into an undefined place, remove this. |
| 10476 | if (isa<UndefValue>(ScalarOp) || isa<UndefValue>(IdxOp)) |
| 10477 | ReplaceInstUsesWith(IE, VecOp); |
| 10478 | |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10479 | // If the inserted element was extracted from some other vector, and if the |
| 10480 | // indexes are constant, try to turn this into a shufflevector operation. |
| 10481 | if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) { |
| 10482 | if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) && |
| 10483 | EI->getOperand(0)->getType() == IE.getType()) { |
| 10484 | unsigned NumVectorElts = IE.getType()->getNumElements(); |
Chris Lattner | e34e9a2 | 2007-04-14 23:32:02 +0000 | [diff] [blame] | 10485 | unsigned ExtractedIdx = |
| 10486 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10487 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10488 | |
| 10489 | if (ExtractedIdx >= NumVectorElts) // Out of range extract. |
| 10490 | return ReplaceInstUsesWith(IE, VecOp); |
| 10491 | |
| 10492 | if (InsertedIdx >= NumVectorElts) // Out of range insert. |
| 10493 | return ReplaceInstUsesWith(IE, UndefValue::get(IE.getType())); |
| 10494 | |
| 10495 | // If we are extracting a value from a vector, then inserting it right |
| 10496 | // back into the same place, just use the input vector. |
| 10497 | if (EI->getOperand(0) == VecOp && ExtractedIdx == InsertedIdx) |
| 10498 | return ReplaceInstUsesWith(IE, VecOp); |
| 10499 | |
| 10500 | // We could theoretically do this for ANY input. However, doing so could |
| 10501 | // turn chains of insertelement instructions into a chain of shufflevector |
| 10502 | // instructions, and right now we do not merge shufflevectors. As such, |
| 10503 | // only do this in a situation where it is clear that there is benefit. |
| 10504 | if (isa<UndefValue>(VecOp) || isa<ConstantAggregateZero>(VecOp)) { |
| 10505 | // Turn this into shuffle(EIOp0, VecOp, Mask). The result has all of |
| 10506 | // the values of VecOp, except then one read from EIOp0. |
| 10507 | // Build a new shuffle mask. |
| 10508 | std::vector<Constant*> Mask; |
| 10509 | if (isa<UndefValue>(VecOp)) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10510 | Mask.assign(NumVectorElts, UndefValue::get(Type::Int32Ty)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10511 | else { |
| 10512 | assert(isa<ConstantAggregateZero>(VecOp) && "Unknown thing"); |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10513 | Mask.assign(NumVectorElts, ConstantInt::get(Type::Int32Ty, |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10514 | NumVectorElts)); |
| 10515 | } |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10516 | Mask[InsertedIdx] = ConstantInt::get(Type::Int32Ty, ExtractedIdx); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10517 | return new ShuffleVectorInst(EI->getOperand(0), VecOp, |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10518 | ConstantVector::get(Mask)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10519 | } |
| 10520 | |
| 10521 | // If this insertelement isn't used by some other insertelement, turn it |
| 10522 | // (and any insertelements it points to), into one big shuffle. |
| 10523 | if (!IE.hasOneUse() || !isa<InsertElementInst>(IE.use_back())) { |
| 10524 | std::vector<Constant*> Mask; |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10525 | Value *RHS = 0; |
| 10526 | Value *LHS = CollectShuffleElements(&IE, Mask, RHS); |
| 10527 | if (RHS == 0) RHS = UndefValue::get(LHS->getType()); |
| 10528 | // We now have a shuffle of LHS, RHS, Mask. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10529 | return new ShuffleVectorInst(LHS, RHS, ConstantVector::get(Mask)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10530 | } |
| 10531 | } |
| 10532 | } |
| 10533 | |
| 10534 | return 0; |
| 10535 | } |
| 10536 | |
| 10537 | |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 10538 | Instruction *InstCombiner::visitShuffleVectorInst(ShuffleVectorInst &SVI) { |
| 10539 | Value *LHS = SVI.getOperand(0); |
| 10540 | Value *RHS = SVI.getOperand(1); |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10541 | std::vector<unsigned> Mask = getShuffleMask(&SVI); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 10542 | |
| 10543 | bool MadeChange = false; |
| 10544 | |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 10545 | // Undefined shuffle mask -> undefined value. |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10546 | if (isa<UndefValue>(SVI.getOperand(2))) |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 10547 | return ReplaceInstUsesWith(SVI, UndefValue::get(SVI.getType())); |
| 10548 | |
Chris Lattner | e4929dd | 2007-01-05 07:36:08 +0000 | [diff] [blame] | 10549 | // 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] | 10550 | // the undef, change them to undefs. |
Chris Lattner | e4929dd | 2007-01-05 07:36:08 +0000 | [diff] [blame] | 10551 | if (isa<UndefValue>(SVI.getOperand(1))) { |
| 10552 | // Scan to see if there are any references to the RHS. If so, replace them |
| 10553 | // with undef element refs and set MadeChange to true. |
| 10554 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
| 10555 | if (Mask[i] >= e && Mask[i] != 2*e) { |
| 10556 | Mask[i] = 2*e; |
| 10557 | MadeChange = true; |
| 10558 | } |
| 10559 | } |
| 10560 | |
| 10561 | if (MadeChange) { |
| 10562 | // Remap any references to RHS to use LHS. |
| 10563 | std::vector<Constant*> Elts; |
| 10564 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
| 10565 | if (Mask[i] == 2*e) |
| 10566 | Elts.push_back(UndefValue::get(Type::Int32Ty)); |
| 10567 | else |
| 10568 | Elts.push_back(ConstantInt::get(Type::Int32Ty, Mask[i])); |
| 10569 | } |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10570 | SVI.setOperand(2, ConstantVector::get(Elts)); |
Chris Lattner | e4929dd | 2007-01-05 07:36:08 +0000 | [diff] [blame] | 10571 | } |
| 10572 | } |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10573 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10574 | // Canonicalize shuffle(x ,x,mask) -> shuffle(x, undef,mask') |
| 10575 | // Canonicalize shuffle(undef,x,mask) -> shuffle(x, undef,mask'). |
| 10576 | if (LHS == RHS || isa<UndefValue>(LHS)) { |
| 10577 | if (isa<UndefValue>(LHS) && LHS == RHS) { |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 10578 | // shuffle(undef,undef,mask) -> undef. |
| 10579 | return ReplaceInstUsesWith(SVI, LHS); |
| 10580 | } |
| 10581 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10582 | // Remap any references to RHS to use LHS. |
| 10583 | std::vector<Constant*> Elts; |
| 10584 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 10585 | if (Mask[i] >= 2*e) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10586 | Elts.push_back(UndefValue::get(Type::Int32Ty)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 10587 | else { |
| 10588 | if ((Mask[i] >= e && isa<UndefValue>(RHS)) || |
| 10589 | (Mask[i] < e && isa<UndefValue>(LHS))) |
| 10590 | Mask[i] = 2*e; // Turn into undef. |
| 10591 | else |
| 10592 | Mask[i] &= (e-1); // Force to LHS. |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10593 | Elts.push_back(ConstantInt::get(Type::Int32Ty, Mask[i])); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 10594 | } |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 10595 | } |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10596 | SVI.setOperand(0, SVI.getOperand(1)); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 10597 | SVI.setOperand(1, UndefValue::get(RHS->getType())); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10598 | SVI.setOperand(2, ConstantVector::get(Elts)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 10599 | LHS = SVI.getOperand(0); |
| 10600 | RHS = SVI.getOperand(1); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 10601 | MadeChange = true; |
| 10602 | } |
| 10603 | |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 10604 | // Analyze the shuffle, are the LHS or RHS and identity shuffles? |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10605 | bool isLHSID = true, isRHSID = true; |
Chris Lattner | 706126d | 2006-04-16 00:03:56 +0000 | [diff] [blame] | 10606 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10607 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
| 10608 | if (Mask[i] >= e*2) continue; // Ignore undef values. |
| 10609 | // Is this an identity shuffle of the LHS value? |
| 10610 | isLHSID &= (Mask[i] == i); |
| 10611 | |
| 10612 | // Is this an identity shuffle of the RHS value? |
| 10613 | isRHSID &= (Mask[i]-e == i); |
Chris Lattner | 706126d | 2006-04-16 00:03:56 +0000 | [diff] [blame] | 10614 | } |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 10615 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10616 | // Eliminate identity shuffles. |
| 10617 | if (isLHSID) return ReplaceInstUsesWith(SVI, LHS); |
| 10618 | if (isRHSID) return ReplaceInstUsesWith(SVI, RHS); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 10619 | |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 10620 | // If the LHS is a shufflevector itself, see if we can combine it with this |
| 10621 | // one without producing an unusual shuffle. Here we are really conservative: |
| 10622 | // we are absolutely afraid of producing a shuffle mask not in the input |
| 10623 | // program, because the code gen may not be smart enough to turn a merged |
| 10624 | // shuffle into two specific shuffles: it may produce worse code. As such, |
| 10625 | // we only merge two shuffles if the result is one of the two input shuffle |
| 10626 | // masks. In this case, merging the shuffles just removes one instruction, |
| 10627 | // which we know is safe. This is good for things like turning: |
| 10628 | // (splat(splat)) -> splat. |
| 10629 | if (ShuffleVectorInst *LHSSVI = dyn_cast<ShuffleVectorInst>(LHS)) { |
| 10630 | if (isa<UndefValue>(RHS)) { |
| 10631 | std::vector<unsigned> LHSMask = getShuffleMask(LHSSVI); |
| 10632 | |
| 10633 | std::vector<unsigned> NewMask; |
| 10634 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) |
| 10635 | if (Mask[i] >= 2*e) |
| 10636 | NewMask.push_back(2*e); |
| 10637 | else |
| 10638 | NewMask.push_back(LHSMask[Mask[i]]); |
| 10639 | |
| 10640 | // If the result mask is equal to the src shuffle or this shuffle mask, do |
| 10641 | // the replacement. |
| 10642 | if (NewMask == LHSMask || NewMask == Mask) { |
| 10643 | std::vector<Constant*> Elts; |
| 10644 | for (unsigned i = 0, e = NewMask.size(); i != e; ++i) { |
| 10645 | if (NewMask[i] >= e*2) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10646 | Elts.push_back(UndefValue::get(Type::Int32Ty)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 10647 | } else { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10648 | Elts.push_back(ConstantInt::get(Type::Int32Ty, NewMask[i])); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 10649 | } |
| 10650 | } |
| 10651 | return new ShuffleVectorInst(LHSSVI->getOperand(0), |
| 10652 | LHSSVI->getOperand(1), |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10653 | ConstantVector::get(Elts)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 10654 | } |
| 10655 | } |
| 10656 | } |
Chris Lattner | c5eff44 | 2007-01-30 22:32:46 +0000 | [diff] [blame] | 10657 | |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 10658 | return MadeChange ? &SVI : 0; |
| 10659 | } |
| 10660 | |
| 10661 | |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10662 | |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 10663 | |
| 10664 | /// TryToSinkInstruction - Try to move the specified instruction from its |
| 10665 | /// current block into the beginning of DestBlock, which can only happen if it's |
| 10666 | /// safe to move the instruction past all of the instructions between it and the |
| 10667 | /// end of its block. |
| 10668 | static bool TryToSinkInstruction(Instruction *I, BasicBlock *DestBlock) { |
| 10669 | assert(I->hasOneUse() && "Invariants didn't hold!"); |
| 10670 | |
Chris Lattner | 108e902 | 2005-10-27 17:13:11 +0000 | [diff] [blame] | 10671 | // Cannot move control-flow-involving, volatile loads, vaarg, etc. |
| 10672 | if (isa<PHINode>(I) || I->mayWriteToMemory()) return false; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 10673 | |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 10674 | // Do not sink alloca instructions out of the entry block. |
Dan Gohman | ecb7a77 | 2007-03-22 16:38:57 +0000 | [diff] [blame] | 10675 | if (isa<AllocaInst>(I) && I->getParent() == |
| 10676 | &DestBlock->getParent()->getEntryBlock()) |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 10677 | return false; |
| 10678 | |
Chris Lattner | 96a52a6 | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 10679 | // We can only sink load instructions if there is nothing between the load and |
| 10680 | // the end of block that could change the value. |
| 10681 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Chris Lattner | 96a52a6 | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 10682 | for (BasicBlock::iterator Scan = LI, E = LI->getParent()->end(); |
| 10683 | Scan != E; ++Scan) |
| 10684 | if (Scan->mayWriteToMemory()) |
| 10685 | return false; |
Chris Lattner | 96a52a6 | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 10686 | } |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 10687 | |
| 10688 | BasicBlock::iterator InsertPos = DestBlock->begin(); |
| 10689 | while (isa<PHINode>(InsertPos)) ++InsertPos; |
| 10690 | |
Chris Lattner | 4bc5f80 | 2005-08-08 19:11:57 +0000 | [diff] [blame] | 10691 | I->moveBefore(InsertPos); |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 10692 | ++NumSunkInst; |
| 10693 | return true; |
| 10694 | } |
| 10695 | |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 10696 | |
| 10697 | /// AddReachableCodeToWorklist - Walk the function in depth-first order, adding |
| 10698 | /// all reachable code to the worklist. |
| 10699 | /// |
| 10700 | /// This has a couple of tricks to make the code faster and more powerful. In |
| 10701 | /// particular, we constant fold and DCE instructions as we go, to avoid adding |
| 10702 | /// them to the worklist (this significantly speeds up instcombine on code where |
| 10703 | /// many instructions are dead or constant). Additionally, if we find a branch |
| 10704 | /// whose condition is a known constant, we only visit the reachable successors. |
| 10705 | /// |
| 10706 | static void AddReachableCodeToWorklist(BasicBlock *BB, |
Chris Lattner | 1f87a58 | 2007-02-15 19:41:52 +0000 | [diff] [blame] | 10707 | SmallPtrSet<BasicBlock*, 64> &Visited, |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10708 | InstCombiner &IC, |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 10709 | const TargetData *TD) { |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 10710 | std::vector<BasicBlock*> Worklist; |
| 10711 | Worklist.push_back(BB); |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 10712 | |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 10713 | while (!Worklist.empty()) { |
| 10714 | BB = Worklist.back(); |
| 10715 | Worklist.pop_back(); |
| 10716 | |
| 10717 | // We have now visited this block! If we've already been here, ignore it. |
| 10718 | if (!Visited.insert(BB)) continue; |
| 10719 | |
| 10720 | for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ) { |
| 10721 | Instruction *Inst = BBI++; |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 10722 | |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 10723 | // DCE instruction if trivially dead. |
| 10724 | if (isInstructionTriviallyDead(Inst)) { |
| 10725 | ++NumDeadInst; |
| 10726 | DOUT << "IC: DCE: " << *Inst; |
| 10727 | Inst->eraseFromParent(); |
| 10728 | continue; |
| 10729 | } |
| 10730 | |
| 10731 | // ConstantProp instruction if trivially constant. |
| 10732 | if (Constant *C = ConstantFoldInstruction(Inst, TD)) { |
| 10733 | DOUT << "IC: ConstFold to: " << *C << " from: " << *Inst; |
| 10734 | Inst->replaceAllUsesWith(C); |
| 10735 | ++NumConstProp; |
| 10736 | Inst->eraseFromParent(); |
| 10737 | continue; |
| 10738 | } |
Chris Lattner | 3ccc6bc | 2007-07-20 22:06:41 +0000 | [diff] [blame] | 10739 | |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 10740 | IC.AddToWorkList(Inst); |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 10741 | } |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 10742 | |
| 10743 | // Recursively visit successors. If this is a branch or switch on a |
| 10744 | // constant, only visit the reachable successor. |
| 10745 | TerminatorInst *TI = BB->getTerminator(); |
| 10746 | if (BranchInst *BI = dyn_cast<BranchInst>(TI)) { |
| 10747 | if (BI->isConditional() && isa<ConstantInt>(BI->getCondition())) { |
| 10748 | bool CondVal = cast<ConstantInt>(BI->getCondition())->getZExtValue(); |
| 10749 | Worklist.push_back(BI->getSuccessor(!CondVal)); |
| 10750 | continue; |
| 10751 | } |
| 10752 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) { |
| 10753 | if (ConstantInt *Cond = dyn_cast<ConstantInt>(SI->getCondition())) { |
| 10754 | // See if this is an explicit destination. |
| 10755 | for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i) |
| 10756 | if (SI->getCaseValue(i) == Cond) { |
| 10757 | Worklist.push_back(SI->getSuccessor(i)); |
| 10758 | continue; |
| 10759 | } |
| 10760 | |
| 10761 | // Otherwise it is the default destination. |
| 10762 | Worklist.push_back(SI->getSuccessor(0)); |
| 10763 | continue; |
| 10764 | } |
| 10765 | } |
| 10766 | |
| 10767 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) |
| 10768 | Worklist.push_back(TI->getSuccessor(i)); |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 10769 | } |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 10770 | } |
| 10771 | |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 10772 | bool InstCombiner::DoOneIteration(Function &F, unsigned Iteration) { |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 10773 | bool Changed = false; |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 10774 | TD = &getAnalysis<TargetData>(); |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 10775 | |
| 10776 | DEBUG(DOUT << "\n\nINSTCOMBINE ITERATION #" << Iteration << " on " |
| 10777 | << F.getNameStr() << "\n"); |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 10778 | |
Chris Lattner | b3d5970 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 10779 | { |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 10780 | // Do a depth-first traversal of the function, populate the worklist with |
| 10781 | // the reachable instructions. Ignore blocks that are not reachable. Keep |
| 10782 | // track of which blocks we visit. |
Chris Lattner | 1f87a58 | 2007-02-15 19:41:52 +0000 | [diff] [blame] | 10783 | SmallPtrSet<BasicBlock*, 64> Visited; |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10784 | AddReachableCodeToWorklist(F.begin(), Visited, *this, TD); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 10785 | |
Chris Lattner | b3d5970 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 10786 | // Do a quick scan over the function. If we find any blocks that are |
| 10787 | // unreachable, remove any instructions inside of them. This prevents |
| 10788 | // the instcombine code from having to deal with some bad special cases. |
| 10789 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 10790 | if (!Visited.count(BB)) { |
| 10791 | Instruction *Term = BB->getTerminator(); |
| 10792 | while (Term != BB->begin()) { // Remove instrs bottom-up |
| 10793 | BasicBlock::iterator I = Term; --I; |
Chris Lattner | 6ffe551 | 2004-04-27 15:13:33 +0000 | [diff] [blame] | 10794 | |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 10795 | DOUT << "IC: DCE: " << *I; |
Chris Lattner | b3d5970 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 10796 | ++NumDeadInst; |
| 10797 | |
| 10798 | if (!I->use_empty()) |
| 10799 | I->replaceAllUsesWith(UndefValue::get(I->getType())); |
| 10800 | I->eraseFromParent(); |
| 10801 | } |
| 10802 | } |
| 10803 | } |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 10804 | |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10805 | while (!Worklist.empty()) { |
| 10806 | Instruction *I = RemoveOneFromWorkList(); |
| 10807 | if (I == 0) continue; // skip null values. |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 10808 | |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 10809 | // Check to see if we can DCE the instruction. |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 10810 | if (isInstructionTriviallyDead(I)) { |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 10811 | // Add operands to the worklist. |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 10812 | if (I->getNumOperands() < 4) |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 10813 | AddUsesToWorkList(*I); |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 10814 | ++NumDeadInst; |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 10815 | |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 10816 | DOUT << "IC: DCE: " << *I; |
Chris Lattner | ad5fec1 | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 10817 | |
| 10818 | I->eraseFromParent(); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10819 | RemoveFromWorkList(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 10820 | continue; |
| 10821 | } |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 10822 | |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 10823 | // Instruction isn't dead, see if we can constant propagate it. |
Chris Lattner | 0a19ffa | 2007-01-30 23:16:15 +0000 | [diff] [blame] | 10824 | if (Constant *C = ConstantFoldInstruction(I, TD)) { |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 10825 | DOUT << "IC: ConstFold to: " << *C << " from: " << *I; |
Chris Lattner | ad5fec1 | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 10826 | |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 10827 | // Add operands to the worklist. |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 10828 | AddUsesToWorkList(*I); |
Chris Lattner | c736d56 | 2002-12-05 22:41:53 +0000 | [diff] [blame] | 10829 | ReplaceInstUsesWith(*I, C); |
| 10830 | |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 10831 | ++NumConstProp; |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 10832 | I->eraseFromParent(); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10833 | RemoveFromWorkList(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 10834 | continue; |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 10835 | } |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 10836 | |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 10837 | // See if we can trivially sink this instruction to a successor basic block. |
| 10838 | if (I->hasOneUse()) { |
| 10839 | BasicBlock *BB = I->getParent(); |
| 10840 | BasicBlock *UserParent = cast<Instruction>(I->use_back())->getParent(); |
| 10841 | if (UserParent != BB) { |
| 10842 | bool UserIsSuccessor = false; |
| 10843 | // See if the user is one of our successors. |
| 10844 | for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI) |
| 10845 | if (*SI == UserParent) { |
| 10846 | UserIsSuccessor = true; |
| 10847 | break; |
| 10848 | } |
| 10849 | |
| 10850 | // If the user is one of our immediate successors, and if that successor |
| 10851 | // only has us as a predecessors (we'd have to split the critical edge |
| 10852 | // otherwise), we can keep going. |
| 10853 | if (UserIsSuccessor && !isa<PHINode>(I->use_back()) && |
| 10854 | next(pred_begin(UserParent)) == pred_end(UserParent)) |
| 10855 | // Okay, the CFG is simple enough, try to sink this instruction. |
| 10856 | Changed |= TryToSinkInstruction(I, UserParent); |
| 10857 | } |
| 10858 | } |
| 10859 | |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 10860 | // Now that we have an instruction, try combining it to simplify it... |
Reid Spencer | a9b8101 | 2007-03-26 17:44:01 +0000 | [diff] [blame] | 10861 | #ifndef NDEBUG |
| 10862 | std::string OrigI; |
| 10863 | #endif |
| 10864 | DEBUG(std::ostringstream SS; I->print(SS); OrigI = SS.str();); |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 10865 | if (Instruction *Result = visit(*I)) { |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 10866 | ++NumCombined; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 10867 | // Should we replace the old instruction with a new one? |
Chris Lattner | b3bc8fa | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 10868 | if (Result != I) { |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 10869 | DOUT << "IC: Old = " << *I |
| 10870 | << " New = " << *Result; |
Chris Lattner | 0cea42a | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 10871 | |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 10872 | // Everything uses the new instruction now. |
| 10873 | I->replaceAllUsesWith(Result); |
| 10874 | |
| 10875 | // Push the new instruction and any users onto the worklist. |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10876 | AddToWorkList(Result); |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 10877 | AddUsersToWorkList(*Result); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 10878 | |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 10879 | // Move the name to the new instruction first. |
| 10880 | Result->takeName(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 10881 | |
| 10882 | // Insert the new instruction into the basic block... |
| 10883 | BasicBlock *InstParent = I->getParent(); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 10884 | BasicBlock::iterator InsertPos = I; |
| 10885 | |
| 10886 | if (!isa<PHINode>(Result)) // If combining a PHI, don't insert |
| 10887 | while (isa<PHINode>(InsertPos)) // middle of a block of PHIs. |
| 10888 | ++InsertPos; |
| 10889 | |
| 10890 | InstParent->getInstList().insert(InsertPos, Result); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 10891 | |
Chris Lattner | 00d5131 | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 10892 | // Make sure that we reprocess all operands now that we reduced their |
| 10893 | // use counts. |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10894 | AddUsesToWorkList(*I); |
Chris Lattner | 216d4d8 | 2004-05-01 23:19:52 +0000 | [diff] [blame] | 10895 | |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 10896 | // Instructions can end up on the worklist more than once. Make sure |
| 10897 | // we do not process an instruction that has been deleted. |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10898 | RemoveFromWorkList(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 10899 | |
| 10900 | // Erase the old instruction. |
| 10901 | InstParent->getInstList().erase(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 10902 | } else { |
Evan Cheng | c7baf68 | 2007-03-27 16:44:48 +0000 | [diff] [blame] | 10903 | #ifndef NDEBUG |
Reid Spencer | a9b8101 | 2007-03-26 17:44:01 +0000 | [diff] [blame] | 10904 | DOUT << "IC: Mod = " << OrigI |
| 10905 | << " New = " << *I; |
Evan Cheng | c7baf68 | 2007-03-27 16:44:48 +0000 | [diff] [blame] | 10906 | #endif |
Chris Lattner | 0cea42a | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 10907 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 10908 | // If the instruction was modified, it's possible that it is now dead. |
| 10909 | // if so, remove it. |
Chris Lattner | 00d5131 | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 10910 | if (isInstructionTriviallyDead(I)) { |
| 10911 | // Make sure we process all operands now that we are reducing their |
| 10912 | // use counts. |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 10913 | AddUsesToWorkList(*I); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 10914 | |
Chris Lattner | 00d5131 | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 10915 | // Instructions may end up in the worklist more than once. Erase all |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10916 | // occurrences of this instruction. |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10917 | RemoveFromWorkList(I); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10918 | I->eraseFromParent(); |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 10919 | } else { |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 10920 | AddToWorkList(I); |
| 10921 | AddUsersToWorkList(*I); |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 10922 | } |
Chris Lattner | b3bc8fa | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 10923 | } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 10924 | Changed = true; |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 10925 | } |
| 10926 | } |
| 10927 | |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 10928 | assert(WorklistMap.empty() && "Worklist empty, but map not?"); |
Chris Lattner | a9ff5eb | 2007-08-05 08:47:58 +0000 | [diff] [blame] | 10929 | |
| 10930 | // Do an explicit clear, this shrinks the map if needed. |
| 10931 | WorklistMap.clear(); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 10932 | return Changed; |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 10933 | } |
| 10934 | |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 10935 | |
| 10936 | bool InstCombiner::runOnFunction(Function &F) { |
Chris Lattner | f964f32 | 2007-03-04 04:27:24 +0000 | [diff] [blame] | 10937 | MustPreserveLCSSA = mustPreserveAnalysisID(LCSSAID); |
| 10938 | |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 10939 | bool EverMadeChange = false; |
| 10940 | |
| 10941 | // Iterate while there is work to do. |
| 10942 | unsigned Iteration = 0; |
| 10943 | while (DoOneIteration(F, Iteration++)) |
| 10944 | EverMadeChange = true; |
| 10945 | return EverMadeChange; |
| 10946 | } |
| 10947 | |
Brian Gaeke | 96d4bf7 | 2004-07-27 17:43:21 +0000 | [diff] [blame] | 10948 | FunctionPass *llvm::createInstructionCombiningPass() { |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 10949 | return new InstCombiner(); |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 10950 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 10951 | |