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 | } |
| 608 | |
Chris Lattner | 68d5ff2 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 609 | /// ComputeMaskedBits - Determine which of the bits specified in Mask are |
| 610 | /// 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] | 611 | /// bit sets. This code only analyzes bits in Mask, in order to short-circuit |
| 612 | /// processing. |
| 613 | /// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that |
| 614 | /// we cannot optimize based on the assumption that it is zero without changing |
| 615 | /// it to be an explicit zero. If we don't change it to zero, other code could |
| 616 | /// optimized based on the contradictory assumption that it is non-zero. |
| 617 | /// Because instcombine aggressively folds operations with undef args anyway, |
| 618 | /// this won't lose us code quality. |
Reid Spencer | 55702aa | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 619 | static void ComputeMaskedBits(Value *V, const APInt &Mask, APInt& KnownZero, |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 620 | APInt& KnownOne, unsigned Depth = 0) { |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 621 | assert(V && "No Value?"); |
| 622 | assert(Depth <= 6 && "Limit Search Depth"); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 623 | uint32_t BitWidth = Mask.getBitWidth(); |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 624 | assert(cast<IntegerType>(V->getType())->getBitWidth() == BitWidth && |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 625 | KnownZero.getBitWidth() == BitWidth && |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 626 | KnownOne.getBitWidth() == BitWidth && |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 627 | "V, Mask, KnownOne and KnownZero should have same BitWidth"); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 628 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
| 629 | // We know all of the bits for a constant! |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 630 | KnownOne = CI->getValue() & Mask; |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 631 | KnownZero = ~KnownOne & Mask; |
| 632 | return; |
| 633 | } |
| 634 | |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 635 | if (Depth == 6 || Mask == 0) |
| 636 | return; // Limit search depth. |
| 637 | |
| 638 | Instruction *I = dyn_cast<Instruction>(V); |
| 639 | if (!I) return; |
| 640 | |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 641 | KnownZero.clear(); KnownOne.clear(); // Don't know anything. |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 642 | APInt KnownZero2(KnownZero), KnownOne2(KnownOne); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 643 | |
| 644 | switch (I->getOpcode()) { |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 645 | case Instruction::And: { |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 646 | // If either the LHS or the RHS are Zero, the result is zero. |
| 647 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, Depth+1); |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 648 | APInt Mask2(Mask & ~KnownZero); |
| 649 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 650 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 651 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 652 | |
| 653 | // Output known-1 bits are only known if set in both the LHS & RHS. |
| 654 | KnownOne &= KnownOne2; |
| 655 | // Output known-0 are known to be clear if zero in either the LHS | RHS. |
| 656 | KnownZero |= KnownZero2; |
| 657 | return; |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 658 | } |
| 659 | case Instruction::Or: { |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 660 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, Depth+1); |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 661 | APInt Mask2(Mask & ~KnownOne); |
| 662 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 663 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 664 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 665 | |
| 666 | // Output known-0 bits are only known if clear in both the LHS & RHS. |
| 667 | KnownZero &= KnownZero2; |
| 668 | // Output known-1 are known to be set if set in either the LHS | RHS. |
| 669 | KnownOne |= KnownOne2; |
| 670 | return; |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 671 | } |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 672 | case Instruction::Xor: { |
| 673 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, Depth+1); |
| 674 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1); |
| 675 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 676 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 677 | |
| 678 | // Output known-0 bits are known if clear or set in both the LHS & RHS. |
| 679 | APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2); |
| 680 | // Output known-1 are known to be set if set in only one of the LHS, RHS. |
| 681 | KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2); |
| 682 | KnownZero = KnownZeroOut; |
| 683 | return; |
| 684 | } |
| 685 | case Instruction::Select: |
| 686 | ComputeMaskedBits(I->getOperand(2), Mask, KnownZero, KnownOne, Depth+1); |
| 687 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1); |
| 688 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 689 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 690 | |
| 691 | // Only known if known in both the LHS and RHS. |
| 692 | KnownOne &= KnownOne2; |
| 693 | KnownZero &= KnownZero2; |
| 694 | return; |
| 695 | case Instruction::FPTrunc: |
| 696 | case Instruction::FPExt: |
| 697 | case Instruction::FPToUI: |
| 698 | case Instruction::FPToSI: |
| 699 | case Instruction::SIToFP: |
| 700 | case Instruction::PtrToInt: |
| 701 | case Instruction::UIToFP: |
| 702 | case Instruction::IntToPtr: |
| 703 | return; // Can't work with floating point or pointers |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 704 | case Instruction::Trunc: { |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 705 | // All these have integer operands |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 706 | uint32_t SrcBitWidth = |
| 707 | cast<IntegerType>(I->getOperand(0)->getType())->getBitWidth(); |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 708 | APInt MaskIn(Mask); |
| 709 | MaskIn.zext(SrcBitWidth); |
| 710 | KnownZero.zext(SrcBitWidth); |
| 711 | KnownOne.zext(SrcBitWidth); |
| 712 | ComputeMaskedBits(I->getOperand(0), MaskIn, KnownZero, KnownOne, Depth+1); |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 713 | KnownZero.trunc(BitWidth); |
| 714 | KnownOne.trunc(BitWidth); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 715 | return; |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 716 | } |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 717 | case Instruction::BitCast: { |
| 718 | const Type *SrcTy = I->getOperand(0)->getType(); |
| 719 | if (SrcTy->isInteger()) { |
| 720 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero, KnownOne, Depth+1); |
| 721 | return; |
| 722 | } |
| 723 | break; |
| 724 | } |
| 725 | case Instruction::ZExt: { |
| 726 | // Compute the bits in the result that are not present in the input. |
| 727 | const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType()); |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 728 | uint32_t SrcBitWidth = SrcTy->getBitWidth(); |
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 729 | |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 730 | APInt MaskIn(Mask); |
| 731 | MaskIn.trunc(SrcBitWidth); |
| 732 | KnownZero.trunc(SrcBitWidth); |
| 733 | KnownOne.trunc(SrcBitWidth); |
| 734 | ComputeMaskedBits(I->getOperand(0), MaskIn, KnownZero, KnownOne, Depth+1); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 735 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 736 | // The top bits are known to be zero. |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 737 | KnownZero.zext(BitWidth); |
| 738 | KnownOne.zext(BitWidth); |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 739 | KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 740 | return; |
| 741 | } |
| 742 | case Instruction::SExt: { |
| 743 | // Compute the bits in the result that are not present in the input. |
| 744 | const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType()); |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 745 | uint32_t SrcBitWidth = SrcTy->getBitWidth(); |
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 746 | |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 747 | APInt MaskIn(Mask); |
| 748 | MaskIn.trunc(SrcBitWidth); |
| 749 | KnownZero.trunc(SrcBitWidth); |
| 750 | KnownOne.trunc(SrcBitWidth); |
| 751 | ComputeMaskedBits(I->getOperand(0), MaskIn, KnownZero, KnownOne, Depth+1); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 752 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 753 | KnownZero.zext(BitWidth); |
| 754 | KnownOne.zext(BitWidth); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 755 | |
| 756 | // If the sign bit of the input is known set or clear, then we know the |
| 757 | // top bits of the result. |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 758 | if (KnownZero[SrcBitWidth-1]) // Input sign bit known zero |
Zhou Sheng | 34a4b38 | 2007-03-28 17:38:21 +0000 | [diff] [blame] | 759 | KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth); |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 760 | else if (KnownOne[SrcBitWidth-1]) // Input sign bit known set |
Zhou Sheng | 34a4b38 | 2007-03-28 17:38:21 +0000 | [diff] [blame] | 761 | KnownOne |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 762 | return; |
| 763 | } |
| 764 | case Instruction::Shl: |
| 765 | // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0 |
| 766 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 767 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 768 | APInt Mask2(Mask.lshr(ShiftAmt)); |
| 769 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero, KnownOne, Depth+1); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 770 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
Zhou Sheng | 430f626 | 2007-03-12 05:44:52 +0000 | [diff] [blame] | 771 | KnownZero <<= ShiftAmt; |
| 772 | KnownOne <<= ShiftAmt; |
Reid Spencer | 2149a9d | 2007-03-25 19:55:33 +0000 | [diff] [blame] | 773 | KnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt); // low bits known 0 |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 774 | return; |
| 775 | } |
| 776 | break; |
| 777 | case Instruction::LShr: |
| 778 | // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0 |
| 779 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 780 | // Compute the new bits that are at the top now. |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 781 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 782 | |
| 783 | // Unsigned shift right. |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 784 | APInt Mask2(Mask.shl(ShiftAmt)); |
| 785 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero,KnownOne,Depth+1); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 786 | assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); |
| 787 | KnownZero = APIntOps::lshr(KnownZero, ShiftAmt); |
| 788 | KnownOne = APIntOps::lshr(KnownOne, ShiftAmt); |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 789 | // high bits known zero. |
| 790 | KnownZero |= APInt::getHighBitsSet(BitWidth, ShiftAmt); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 791 | return; |
| 792 | } |
| 793 | break; |
| 794 | case Instruction::AShr: |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 795 | // (ashr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0 |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 796 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 797 | // Compute the new bits that are at the top now. |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 798 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 799 | |
| 800 | // Signed shift right. |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 801 | APInt Mask2(Mask.shl(ShiftAmt)); |
| 802 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero,KnownOne,Depth+1); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 803 | assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); |
| 804 | KnownZero = APIntOps::lshr(KnownZero, ShiftAmt); |
| 805 | KnownOne = APIntOps::lshr(KnownOne, ShiftAmt); |
| 806 | |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 807 | APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt)); |
| 808 | if (KnownZero[BitWidth-ShiftAmt-1]) // New bits are known zero. |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 809 | KnownZero |= HighBits; |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 810 | else if (KnownOne[BitWidth-ShiftAmt-1]) // New bits are known one. |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 811 | KnownOne |= HighBits; |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 812 | return; |
| 813 | } |
| 814 | break; |
| 815 | } |
| 816 | } |
| 817 | |
Reid Spencer | e7816b5 | 2007-03-08 01:52:58 +0000 | [diff] [blame] | 818 | /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use |
| 819 | /// this predicate to simplify operations downstream. Mask is known to be zero |
| 820 | /// for bits that V cannot have. |
| 821 | static bool MaskedValueIsZero(Value *V, const APInt& Mask, unsigned Depth = 0) { |
Zhou Sheng | edd089c | 2007-03-12 16:54:56 +0000 | [diff] [blame] | 822 | APInt KnownZero(Mask.getBitWidth(), 0), KnownOne(Mask.getBitWidth(), 0); |
Reid Spencer | e7816b5 | 2007-03-08 01:52:58 +0000 | [diff] [blame] | 823 | ComputeMaskedBits(V, Mask, KnownZero, KnownOne, Depth); |
| 824 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 825 | return (KnownZero & Mask) == Mask; |
| 826 | } |
| 827 | |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 828 | /// ShrinkDemandedConstant - Check to see if the specified operand of the |
| 829 | /// specified instruction is a constant integer. If so, check to see if there |
| 830 | /// are any bits set in the constant that are not demanded. If so, shrink the |
| 831 | /// constant and return true. |
| 832 | static bool ShrinkDemandedConstant(Instruction *I, unsigned OpNo, |
Reid Spencer | 6b79e2d | 2007-03-12 17:15:10 +0000 | [diff] [blame] | 833 | APInt Demanded) { |
| 834 | assert(I && "No instruction?"); |
| 835 | assert(OpNo < I->getNumOperands() && "Operand index too large"); |
| 836 | |
| 837 | // If the operand is not a constant integer, nothing to do. |
| 838 | ConstantInt *OpC = dyn_cast<ConstantInt>(I->getOperand(OpNo)); |
| 839 | if (!OpC) return false; |
| 840 | |
| 841 | // If there are no bits set that aren't demanded, nothing to do. |
| 842 | Demanded.zextOrTrunc(OpC->getValue().getBitWidth()); |
| 843 | if ((~Demanded & OpC->getValue()) == 0) |
| 844 | return false; |
| 845 | |
| 846 | // This instruction is producing bits that are not demanded. Shrink the RHS. |
| 847 | Demanded &= OpC->getValue(); |
| 848 | I->setOperand(OpNo, ConstantInt::get(Demanded)); |
| 849 | return true; |
| 850 | } |
| 851 | |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 852 | // ComputeSignedMinMaxValuesFromKnownBits - Given a signed integer type and a |
| 853 | // set of known zero and one bits, compute the maximum and minimum values that |
| 854 | // could have the specified known zero and known one bits, returning them in |
| 855 | // min/max. |
| 856 | static void ComputeSignedMinMaxValuesFromKnownBits(const Type *Ty, |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 857 | const APInt& KnownZero, |
| 858 | const APInt& KnownOne, |
| 859 | APInt& Min, APInt& Max) { |
| 860 | uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth(); |
| 861 | assert(KnownZero.getBitWidth() == BitWidth && |
| 862 | KnownOne.getBitWidth() == BitWidth && |
| 863 | Min.getBitWidth() == BitWidth && Max.getBitWidth() == BitWidth && |
| 864 | "Ty, KnownZero, KnownOne and Min, Max must have equal bitwidth."); |
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 865 | APInt UnknownBits = ~(KnownZero|KnownOne); |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 866 | |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 867 | // The minimum value is when all unknown bits are zeros, EXCEPT for the sign |
| 868 | // bit if it is unknown. |
| 869 | Min = KnownOne; |
| 870 | Max = KnownOne|UnknownBits; |
| 871 | |
Zhou Sheng | 4acf155 | 2007-03-28 05:15:57 +0000 | [diff] [blame] | 872 | if (UnknownBits[BitWidth-1]) { // Sign bit is unknown |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 873 | Min.set(BitWidth-1); |
| 874 | Max.clear(BitWidth-1); |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 875 | } |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | // ComputeUnsignedMinMaxValuesFromKnownBits - Given an unsigned integer type and |
| 879 | // a set of known zero and one bits, compute the maximum and minimum values that |
| 880 | // could have the specified known zero and known one bits, returning them in |
| 881 | // min/max. |
| 882 | static void ComputeUnsignedMinMaxValuesFromKnownBits(const Type *Ty, |
Chris Lattner | a9ff5eb | 2007-08-05 08:47:58 +0000 | [diff] [blame] | 883 | const APInt &KnownZero, |
| 884 | const APInt &KnownOne, |
| 885 | APInt &Min, APInt &Max) { |
| 886 | uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth(); BitWidth = BitWidth; |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 887 | assert(KnownZero.getBitWidth() == BitWidth && |
| 888 | KnownOne.getBitWidth() == BitWidth && |
| 889 | Min.getBitWidth() == BitWidth && Max.getBitWidth() && |
| 890 | "Ty, KnownZero, KnownOne and Min, Max must have equal bitwidth."); |
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 891 | APInt UnknownBits = ~(KnownZero|KnownOne); |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 892 | |
| 893 | // The minimum value is when the unknown bits are all zeros. |
| 894 | Min = KnownOne; |
| 895 | // The maximum value is when the unknown bits are all ones. |
| 896 | Max = KnownOne|UnknownBits; |
| 897 | } |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 898 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 899 | /// SimplifyDemandedBits - This function attempts to replace V with a simpler |
| 900 | /// value based on the demanded bits. When this function is called, it is known |
| 901 | /// that only the bits set in DemandedMask of the result of V are ever used |
| 902 | /// downstream. Consequently, depending on the mask and V, it may be possible |
| 903 | /// to replace V with a constant or one of its operands. In such cases, this |
| 904 | /// function does the replacement and returns true. In all other cases, it |
| 905 | /// returns false after analyzing the expression and setting KnownOne and known |
| 906 | /// to be one in the expression. KnownZero contains all the bits that are known |
| 907 | /// to be zero in the expression. These are provided to potentially allow the |
| 908 | /// caller (which might recursively be SimplifyDemandedBits itself) to simplify |
| 909 | /// the expression. KnownOne and KnownZero always follow the invariant that |
| 910 | /// KnownOne & KnownZero == 0. That is, a bit can't be both 1 and 0. Note that |
| 911 | /// the bits in KnownOne and KnownZero may only be accurate for those bits set |
| 912 | /// in DemandedMask. Note also that the bitwidth of V, DemandedMask, KnownZero |
| 913 | /// and KnownOne must all be the same. |
| 914 | bool InstCombiner::SimplifyDemandedBits(Value *V, APInt DemandedMask, |
| 915 | APInt& KnownZero, APInt& KnownOne, |
| 916 | unsigned Depth) { |
| 917 | assert(V != 0 && "Null pointer of Value???"); |
| 918 | assert(Depth <= 6 && "Limit Search Depth"); |
| 919 | uint32_t BitWidth = DemandedMask.getBitWidth(); |
| 920 | const IntegerType *VTy = cast<IntegerType>(V->getType()); |
| 921 | assert(VTy->getBitWidth() == BitWidth && |
| 922 | KnownZero.getBitWidth() == BitWidth && |
| 923 | KnownOne.getBitWidth() == BitWidth && |
| 924 | "Value *V, DemandedMask, KnownZero and KnownOne \ |
| 925 | must have same BitWidth"); |
| 926 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
| 927 | // We know all of the bits for a constant! |
| 928 | KnownOne = CI->getValue() & DemandedMask; |
| 929 | KnownZero = ~KnownOne & DemandedMask; |
| 930 | return false; |
| 931 | } |
| 932 | |
Zhou Sheng | 9670445 | 2007-03-14 03:21:24 +0000 | [diff] [blame] | 933 | KnownZero.clear(); |
| 934 | KnownOne.clear(); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 935 | if (!V->hasOneUse()) { // Other users may use these bits. |
| 936 | if (Depth != 0) { // Not at the root. |
| 937 | // Just compute the KnownZero/KnownOne bits to simplify things downstream. |
| 938 | ComputeMaskedBits(V, DemandedMask, KnownZero, KnownOne, Depth); |
| 939 | return false; |
| 940 | } |
| 941 | // If this is the root being simplified, allow it to have multiple uses, |
| 942 | // just set the DemandedMask to all bits. |
| 943 | DemandedMask = APInt::getAllOnesValue(BitWidth); |
| 944 | } else if (DemandedMask == 0) { // Not demanding any bits from V. |
| 945 | if (V != UndefValue::get(VTy)) |
| 946 | return UpdateValueUsesWith(V, UndefValue::get(VTy)); |
| 947 | return false; |
| 948 | } else if (Depth == 6) { // Limit search depth. |
| 949 | return false; |
| 950 | } |
| 951 | |
| 952 | Instruction *I = dyn_cast<Instruction>(V); |
| 953 | if (!I) return false; // Only analyze instructions. |
| 954 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 955 | APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0); |
| 956 | APInt &RHSKnownZero = KnownZero, &RHSKnownOne = KnownOne; |
| 957 | switch (I->getOpcode()) { |
| 958 | default: break; |
| 959 | case Instruction::And: |
| 960 | // If either the LHS or the RHS are Zero, the result is zero. |
| 961 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, |
| 962 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 963 | return true; |
| 964 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 965 | "Bits known to be one AND zero?"); |
| 966 | |
| 967 | // If something is known zero on the RHS, the bits aren't demanded on the |
| 968 | // LHS. |
| 969 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask & ~RHSKnownZero, |
| 970 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 971 | return true; |
| 972 | assert((LHSKnownZero & LHSKnownOne) == 0 && |
| 973 | "Bits known to be one AND zero?"); |
| 974 | |
| 975 | // If all of the demanded bits are known 1 on one side, return the other. |
| 976 | // These bits cannot contribute to the result of the 'and'. |
| 977 | if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) == |
| 978 | (DemandedMask & ~LHSKnownZero)) |
| 979 | return UpdateValueUsesWith(I, I->getOperand(0)); |
| 980 | if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) == |
| 981 | (DemandedMask & ~RHSKnownZero)) |
| 982 | return UpdateValueUsesWith(I, I->getOperand(1)); |
| 983 | |
| 984 | // If all of the demanded bits in the inputs are known zeros, return zero. |
| 985 | if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask) |
| 986 | return UpdateValueUsesWith(I, Constant::getNullValue(VTy)); |
| 987 | |
| 988 | // If the RHS is a constant, see if we can simplify it. |
| 989 | if (ShrinkDemandedConstant(I, 1, DemandedMask & ~LHSKnownZero)) |
| 990 | return UpdateValueUsesWith(I, I); |
| 991 | |
| 992 | // Output known-1 bits are only known if set in both the LHS & RHS. |
| 993 | RHSKnownOne &= LHSKnownOne; |
| 994 | // Output known-0 are known to be clear if zero in either the LHS | RHS. |
| 995 | RHSKnownZero |= LHSKnownZero; |
| 996 | break; |
| 997 | case Instruction::Or: |
| 998 | // If either the LHS or the RHS are One, the result is One. |
| 999 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, |
| 1000 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 1001 | return true; |
| 1002 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1003 | "Bits known to be one AND zero?"); |
| 1004 | // If something is known one on the RHS, the bits aren't demanded on the |
| 1005 | // LHS. |
| 1006 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask & ~RHSKnownOne, |
| 1007 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1008 | return true; |
| 1009 | assert((LHSKnownZero & LHSKnownOne) == 0 && |
| 1010 | "Bits known to be one AND zero?"); |
| 1011 | |
| 1012 | // If all of the demanded bits are known zero on one side, return the other. |
| 1013 | // These bits cannot contribute to the result of the 'or'. |
| 1014 | if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) == |
| 1015 | (DemandedMask & ~LHSKnownOne)) |
| 1016 | return UpdateValueUsesWith(I, I->getOperand(0)); |
| 1017 | if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) == |
| 1018 | (DemandedMask & ~RHSKnownOne)) |
| 1019 | return UpdateValueUsesWith(I, I->getOperand(1)); |
| 1020 | |
| 1021 | // If all of the potentially set bits on one side are known to be set on |
| 1022 | // the other side, just use the 'other' side. |
| 1023 | if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) == |
| 1024 | (DemandedMask & (~RHSKnownZero))) |
| 1025 | return UpdateValueUsesWith(I, I->getOperand(0)); |
| 1026 | if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) == |
| 1027 | (DemandedMask & (~LHSKnownZero))) |
| 1028 | return UpdateValueUsesWith(I, I->getOperand(1)); |
| 1029 | |
| 1030 | // If the RHS is a constant, see if we can simplify it. |
| 1031 | if (ShrinkDemandedConstant(I, 1, DemandedMask)) |
| 1032 | return UpdateValueUsesWith(I, I); |
| 1033 | |
| 1034 | // Output known-0 bits are only known if clear in both the LHS & RHS. |
| 1035 | RHSKnownZero &= LHSKnownZero; |
| 1036 | // Output known-1 are known to be set if set in either the LHS | RHS. |
| 1037 | RHSKnownOne |= LHSKnownOne; |
| 1038 | break; |
| 1039 | case Instruction::Xor: { |
| 1040 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, |
| 1041 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 1042 | return true; |
| 1043 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1044 | "Bits known to be one AND zero?"); |
| 1045 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, |
| 1046 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1047 | return true; |
| 1048 | assert((LHSKnownZero & LHSKnownOne) == 0 && |
| 1049 | "Bits known to be one AND zero?"); |
| 1050 | |
| 1051 | // If all of the demanded bits are known zero on one side, return the other. |
| 1052 | // These bits cannot contribute to the result of the 'xor'. |
| 1053 | if ((DemandedMask & RHSKnownZero) == DemandedMask) |
| 1054 | return UpdateValueUsesWith(I, I->getOperand(0)); |
| 1055 | if ((DemandedMask & LHSKnownZero) == DemandedMask) |
| 1056 | return UpdateValueUsesWith(I, I->getOperand(1)); |
| 1057 | |
| 1058 | // Output known-0 bits are known if clear or set in both the LHS & RHS. |
| 1059 | APInt KnownZeroOut = (RHSKnownZero & LHSKnownZero) | |
| 1060 | (RHSKnownOne & LHSKnownOne); |
| 1061 | // Output known-1 are known to be set if set in only one of the LHS, RHS. |
| 1062 | APInt KnownOneOut = (RHSKnownZero & LHSKnownOne) | |
| 1063 | (RHSKnownOne & LHSKnownZero); |
| 1064 | |
| 1065 | // If all of the demanded bits are known to be zero on one side or the |
| 1066 | // other, turn this into an *inclusive* or. |
| 1067 | // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0 |
| 1068 | if ((DemandedMask & ~RHSKnownZero & ~LHSKnownZero) == 0) { |
| 1069 | Instruction *Or = |
| 1070 | BinaryOperator::createOr(I->getOperand(0), I->getOperand(1), |
| 1071 | I->getName()); |
| 1072 | InsertNewInstBefore(Or, *I); |
| 1073 | return UpdateValueUsesWith(I, Or); |
| 1074 | } |
| 1075 | |
| 1076 | // If all of the demanded bits on one side are known, and all of the set |
| 1077 | // bits on that side are also known to be set on the other side, turn this |
| 1078 | // into an AND, as we know the bits will be cleared. |
| 1079 | // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2 |
| 1080 | if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) { |
| 1081 | // all known |
| 1082 | if ((RHSKnownOne & LHSKnownOne) == RHSKnownOne) { |
| 1083 | Constant *AndC = ConstantInt::get(~RHSKnownOne & DemandedMask); |
| 1084 | Instruction *And = |
| 1085 | BinaryOperator::createAnd(I->getOperand(0), AndC, "tmp"); |
| 1086 | InsertNewInstBefore(And, *I); |
| 1087 | return UpdateValueUsesWith(I, And); |
| 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | // If the RHS is a constant, see if we can simplify it. |
| 1092 | // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1. |
| 1093 | if (ShrinkDemandedConstant(I, 1, DemandedMask)) |
| 1094 | return UpdateValueUsesWith(I, I); |
| 1095 | |
| 1096 | RHSKnownZero = KnownZeroOut; |
| 1097 | RHSKnownOne = KnownOneOut; |
| 1098 | break; |
| 1099 | } |
| 1100 | case Instruction::Select: |
| 1101 | if (SimplifyDemandedBits(I->getOperand(2), DemandedMask, |
| 1102 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 1103 | return true; |
| 1104 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, |
| 1105 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1106 | return true; |
| 1107 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1108 | "Bits known to be one AND zero?"); |
| 1109 | assert((LHSKnownZero & LHSKnownOne) == 0 && |
| 1110 | "Bits known to be one AND zero?"); |
| 1111 | |
| 1112 | // If the operands are constants, see if we can simplify them. |
| 1113 | if (ShrinkDemandedConstant(I, 1, DemandedMask)) |
| 1114 | return UpdateValueUsesWith(I, I); |
| 1115 | if (ShrinkDemandedConstant(I, 2, DemandedMask)) |
| 1116 | return UpdateValueUsesWith(I, I); |
| 1117 | |
| 1118 | // Only known if known in both the LHS and RHS. |
| 1119 | RHSKnownOne &= LHSKnownOne; |
| 1120 | RHSKnownZero &= LHSKnownZero; |
| 1121 | break; |
| 1122 | case Instruction::Trunc: { |
| 1123 | uint32_t truncBf = |
| 1124 | cast<IntegerType>(I->getOperand(0)->getType())->getBitWidth(); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1125 | DemandedMask.zext(truncBf); |
| 1126 | RHSKnownZero.zext(truncBf); |
| 1127 | RHSKnownOne.zext(truncBf); |
| 1128 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, |
| 1129 | RHSKnownZero, RHSKnownOne, Depth+1)) |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1130 | return true; |
| 1131 | DemandedMask.trunc(BitWidth); |
| 1132 | RHSKnownZero.trunc(BitWidth); |
| 1133 | RHSKnownOne.trunc(BitWidth); |
| 1134 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1135 | "Bits known to be one AND zero?"); |
| 1136 | break; |
| 1137 | } |
| 1138 | case Instruction::BitCast: |
| 1139 | if (!I->getOperand(0)->getType()->isInteger()) |
| 1140 | return false; |
| 1141 | |
| 1142 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, |
| 1143 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 1144 | return true; |
| 1145 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1146 | "Bits known to be one AND zero?"); |
| 1147 | break; |
| 1148 | case Instruction::ZExt: { |
| 1149 | // Compute the bits in the result that are not present in the input. |
| 1150 | const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType()); |
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 1151 | uint32_t SrcBitWidth = SrcTy->getBitWidth(); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1152 | |
Zhou Sheng | d48653a | 2007-03-29 04:45:55 +0000 | [diff] [blame] | 1153 | DemandedMask.trunc(SrcBitWidth); |
| 1154 | RHSKnownZero.trunc(SrcBitWidth); |
| 1155 | RHSKnownOne.trunc(SrcBitWidth); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1156 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, |
| 1157 | RHSKnownZero, RHSKnownOne, Depth+1)) |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1158 | return true; |
| 1159 | DemandedMask.zext(BitWidth); |
| 1160 | RHSKnownZero.zext(BitWidth); |
| 1161 | RHSKnownOne.zext(BitWidth); |
| 1162 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1163 | "Bits known to be one AND zero?"); |
| 1164 | // The top bits are known to be zero. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1165 | RHSKnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1166 | break; |
| 1167 | } |
| 1168 | case Instruction::SExt: { |
| 1169 | // Compute the bits in the result that are not present in the input. |
| 1170 | const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType()); |
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 1171 | uint32_t SrcBitWidth = SrcTy->getBitWidth(); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1172 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1173 | APInt InputDemandedBits = DemandedMask & |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1174 | APInt::getLowBitsSet(BitWidth, SrcBitWidth); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1175 | |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1176 | APInt NewBits(APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1177 | // If any of the sign extended bits are demanded, we know that the sign |
| 1178 | // bit is demanded. |
| 1179 | if ((NewBits & DemandedMask) != 0) |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 1180 | InputDemandedBits.set(SrcBitWidth-1); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1181 | |
Zhou Sheng | d48653a | 2007-03-29 04:45:55 +0000 | [diff] [blame] | 1182 | InputDemandedBits.trunc(SrcBitWidth); |
| 1183 | RHSKnownZero.trunc(SrcBitWidth); |
| 1184 | RHSKnownOne.trunc(SrcBitWidth); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1185 | if (SimplifyDemandedBits(I->getOperand(0), InputDemandedBits, |
| 1186 | RHSKnownZero, RHSKnownOne, Depth+1)) |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1187 | return true; |
| 1188 | InputDemandedBits.zext(BitWidth); |
| 1189 | RHSKnownZero.zext(BitWidth); |
| 1190 | RHSKnownOne.zext(BitWidth); |
| 1191 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1192 | "Bits known to be one AND zero?"); |
| 1193 | |
| 1194 | // If the sign bit of the input is known set or clear, then we know the |
| 1195 | // top bits of the result. |
| 1196 | |
| 1197 | // If the input sign bit is known zero, or if the NewBits are not demanded |
| 1198 | // convert this into a zero extension. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1199 | if (RHSKnownZero[SrcBitWidth-1] || (NewBits & ~DemandedMask) == NewBits) |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1200 | { |
| 1201 | // Convert to ZExt cast |
| 1202 | CastInst *NewCast = new ZExtInst(I->getOperand(0), VTy, I->getName(), I); |
| 1203 | return UpdateValueUsesWith(I, NewCast); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1204 | } else if (RHSKnownOne[SrcBitWidth-1]) { // Input sign bit known set |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1205 | RHSKnownOne |= NewBits; |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1206 | } |
| 1207 | break; |
| 1208 | } |
| 1209 | case Instruction::Add: { |
| 1210 | // Figure out what the input bits are. If the top bits of the and result |
| 1211 | // are not demanded, then the add doesn't demand them from its input |
| 1212 | // either. |
Reid Spencer | 55702aa | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 1213 | uint32_t NLZ = DemandedMask.countLeadingZeros(); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1214 | |
| 1215 | // If there is a constant on the RHS, there are a variety of xformations |
| 1216 | // we can do. |
| 1217 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 1218 | // If null, this should be simplified elsewhere. Some of the xforms here |
| 1219 | // won't work if the RHS is zero. |
| 1220 | if (RHS->isZero()) |
| 1221 | break; |
| 1222 | |
| 1223 | // If the top bit of the output is demanded, demand everything from the |
| 1224 | // input. Otherwise, we demand all the input bits except NLZ top bits. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1225 | APInt InDemandedBits(APInt::getLowBitsSet(BitWidth, BitWidth - NLZ)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1226 | |
| 1227 | // Find information about known zero/one bits in the input. |
| 1228 | if (SimplifyDemandedBits(I->getOperand(0), InDemandedBits, |
| 1229 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1230 | return true; |
| 1231 | |
| 1232 | // If the RHS of the add has bits set that can't affect the input, reduce |
| 1233 | // the constant. |
| 1234 | if (ShrinkDemandedConstant(I, 1, InDemandedBits)) |
| 1235 | return UpdateValueUsesWith(I, I); |
| 1236 | |
| 1237 | // Avoid excess work. |
| 1238 | if (LHSKnownZero == 0 && LHSKnownOne == 0) |
| 1239 | break; |
| 1240 | |
| 1241 | // Turn it into OR if input bits are zero. |
| 1242 | if ((LHSKnownZero & RHS->getValue()) == RHS->getValue()) { |
| 1243 | Instruction *Or = |
| 1244 | BinaryOperator::createOr(I->getOperand(0), I->getOperand(1), |
| 1245 | I->getName()); |
| 1246 | InsertNewInstBefore(Or, *I); |
| 1247 | return UpdateValueUsesWith(I, Or); |
| 1248 | } |
| 1249 | |
| 1250 | // We can say something about the output known-zero and known-one bits, |
| 1251 | // depending on potential carries from the input constant and the |
| 1252 | // unknowns. For example if the LHS is known to have at most the 0x0F0F0 |
| 1253 | // bits set and the RHS constant is 0x01001, then we know we have a known |
| 1254 | // one mask of 0x00001 and a known zero mask of 0xE0F0E. |
| 1255 | |
| 1256 | // To compute this, we first compute the potential carry bits. These are |
| 1257 | // the bits which may be modified. I'm not aware of a better way to do |
| 1258 | // this scan. |
Zhou Sheng | b9cb95f | 2007-03-31 02:38:39 +0000 | [diff] [blame] | 1259 | const APInt& RHSVal = RHS->getValue(); |
| 1260 | APInt CarryBits((~LHSKnownZero + RHSVal) ^ (~LHSKnownZero ^ RHSVal)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1261 | |
| 1262 | // Now that we know which bits have carries, compute the known-1/0 sets. |
| 1263 | |
| 1264 | // Bits are known one if they are known zero in one operand and one in the |
| 1265 | // other, and there is no input carry. |
| 1266 | RHSKnownOne = ((LHSKnownZero & RHSVal) | |
| 1267 | (LHSKnownOne & ~RHSVal)) & ~CarryBits; |
| 1268 | |
| 1269 | // Bits are known zero if they are known zero in both operands and there |
| 1270 | // is no input carry. |
| 1271 | RHSKnownZero = LHSKnownZero & ~RHSVal & ~CarryBits; |
| 1272 | } else { |
| 1273 | // If the high-bits of this ADD are not demanded, then it does not demand |
| 1274 | // the high bits of its LHS or RHS. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1275 | if (DemandedMask[BitWidth-1] == 0) { |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1276 | // Right fill the mask of bits for this ADD to demand the most |
| 1277 | // significant bit and all those below it. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1278 | APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1279 | if (SimplifyDemandedBits(I->getOperand(0), DemandedFromOps, |
| 1280 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1281 | return true; |
| 1282 | if (SimplifyDemandedBits(I->getOperand(1), DemandedFromOps, |
| 1283 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1284 | return true; |
| 1285 | } |
| 1286 | } |
| 1287 | break; |
| 1288 | } |
| 1289 | case Instruction::Sub: |
| 1290 | // If the high-bits of this SUB are not demanded, then it does not demand |
| 1291 | // the high bits of its LHS or RHS. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1292 | if (DemandedMask[BitWidth-1] == 0) { |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1293 | // Right fill the mask of bits for this SUB to demand the most |
| 1294 | // significant bit and all those below it. |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 1295 | uint32_t NLZ = DemandedMask.countLeadingZeros(); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1296 | APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1297 | if (SimplifyDemandedBits(I->getOperand(0), DemandedFromOps, |
| 1298 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1299 | return true; |
| 1300 | if (SimplifyDemandedBits(I->getOperand(1), DemandedFromOps, |
| 1301 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1302 | return true; |
| 1303 | } |
| 1304 | break; |
| 1305 | case Instruction::Shl: |
| 1306 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 1307 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1308 | APInt DemandedMaskIn(DemandedMask.lshr(ShiftAmt)); |
| 1309 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMaskIn, |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1310 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 1311 | return true; |
| 1312 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1313 | "Bits known to be one AND zero?"); |
| 1314 | RHSKnownZero <<= ShiftAmt; |
| 1315 | RHSKnownOne <<= ShiftAmt; |
| 1316 | // low bits known zero. |
Zhou Sheng | adc1495 | 2007-03-14 09:07:33 +0000 | [diff] [blame] | 1317 | if (ShiftAmt) |
Zhou Sheng | e9e03f6 | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 1318 | RHSKnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1319 | } |
| 1320 | break; |
| 1321 | case Instruction::LShr: |
| 1322 | // For a logical shift right |
| 1323 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 1324 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1325 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1326 | // Unsigned shift right. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1327 | APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt)); |
| 1328 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMaskIn, |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1329 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 1330 | return true; |
| 1331 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1332 | "Bits known to be one AND zero?"); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1333 | RHSKnownZero = APIntOps::lshr(RHSKnownZero, ShiftAmt); |
| 1334 | RHSKnownOne = APIntOps::lshr(RHSKnownOne, ShiftAmt); |
Zhou Sheng | adc1495 | 2007-03-14 09:07:33 +0000 | [diff] [blame] | 1335 | if (ShiftAmt) { |
| 1336 | // Compute the new bits that are at the top now. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1337 | APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt)); |
Zhou Sheng | adc1495 | 2007-03-14 09:07:33 +0000 | [diff] [blame] | 1338 | RHSKnownZero |= HighBits; // high bits known zero. |
| 1339 | } |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1340 | } |
| 1341 | break; |
| 1342 | case Instruction::AShr: |
| 1343 | // If this is an arithmetic shift right and only the low-bit is set, we can |
| 1344 | // always convert this into a logical shr, even if the shift amount is |
| 1345 | // variable. The low bit of the shift cannot be an input sign bit unless |
| 1346 | // the shift amount is >= the size of the datatype, which is undefined. |
| 1347 | if (DemandedMask == 1) { |
| 1348 | // Perform the logical shift right. |
| 1349 | Value *NewVal = BinaryOperator::createLShr( |
| 1350 | I->getOperand(0), I->getOperand(1), I->getName()); |
| 1351 | InsertNewInstBefore(cast<Instruction>(NewVal), *I); |
| 1352 | return UpdateValueUsesWith(I, NewVal); |
| 1353 | } |
Chris Lattner | 4241e4d | 2007-07-15 20:54:51 +0000 | [diff] [blame] | 1354 | |
| 1355 | // If the sign bit is the only bit demanded by this ashr, then there is no |
| 1356 | // need to do it, the shift doesn't change the high bit. |
| 1357 | if (DemandedMask.isSignBit()) |
| 1358 | return UpdateValueUsesWith(I, I->getOperand(0)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1359 | |
| 1360 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 1361 | uint32_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1362 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1363 | // Signed shift right. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1364 | APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt)); |
Lauro Ramos Venancio | d0499af | 2007-06-06 17:08:48 +0000 | [diff] [blame] | 1365 | // If any of the "high bits" are demanded, we should set the sign bit as |
| 1366 | // demanded. |
| 1367 | if (DemandedMask.countLeadingZeros() <= ShiftAmt) |
| 1368 | DemandedMaskIn.set(BitWidth-1); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1369 | if (SimplifyDemandedBits(I->getOperand(0), |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1370 | DemandedMaskIn, |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1371 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 1372 | return true; |
| 1373 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1374 | "Bits known to be one AND zero?"); |
| 1375 | // Compute the new bits that are at the top now. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1376 | APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1377 | RHSKnownZero = APIntOps::lshr(RHSKnownZero, ShiftAmt); |
| 1378 | RHSKnownOne = APIntOps::lshr(RHSKnownOne, ShiftAmt); |
| 1379 | |
| 1380 | // Handle the sign bits. |
| 1381 | APInt SignBit(APInt::getSignBit(BitWidth)); |
| 1382 | // Adjust to where it is now in the mask. |
| 1383 | SignBit = APIntOps::lshr(SignBit, ShiftAmt); |
| 1384 | |
| 1385 | // If the input sign bit is known to be zero, or if none of the top bits |
| 1386 | // are demanded, turn this into an unsigned shift right. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1387 | if (RHSKnownZero[BitWidth-ShiftAmt-1] || |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1388 | (HighBits & ~DemandedMask) == HighBits) { |
| 1389 | // Perform the logical shift right. |
| 1390 | Value *NewVal = BinaryOperator::createLShr( |
| 1391 | I->getOperand(0), SA, I->getName()); |
| 1392 | InsertNewInstBefore(cast<Instruction>(NewVal), *I); |
| 1393 | return UpdateValueUsesWith(I, NewVal); |
| 1394 | } else if ((RHSKnownOne & SignBit) != 0) { // New bits are known one. |
| 1395 | RHSKnownOne |= HighBits; |
| 1396 | } |
| 1397 | } |
| 1398 | break; |
| 1399 | } |
| 1400 | |
| 1401 | // If the client is only demanding bits that we know, return the known |
| 1402 | // constant. |
| 1403 | if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) |
| 1404 | return UpdateValueUsesWith(I, ConstantInt::get(RHSKnownOne)); |
| 1405 | return false; |
| 1406 | } |
| 1407 | |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1408 | |
| 1409 | /// SimplifyDemandedVectorElts - The specified value producecs a vector with |
| 1410 | /// 64 or fewer elements. DemandedElts contains the set of elements that are |
| 1411 | /// actually used by the caller. This method analyzes which elements of the |
| 1412 | /// operand are undef and returns that information in UndefElts. |
| 1413 | /// |
| 1414 | /// If the information about demanded elements can be used to simplify the |
| 1415 | /// operation, the operation is simplified, then the resultant value is |
| 1416 | /// returned. This returns null if no change was made. |
| 1417 | Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, uint64_t DemandedElts, |
| 1418 | uint64_t &UndefElts, |
| 1419 | unsigned Depth) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1420 | unsigned VWidth = cast<VectorType>(V->getType())->getNumElements(); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1421 | assert(VWidth <= 64 && "Vector too wide to analyze!"); |
| 1422 | uint64_t EltMask = ~0ULL >> (64-VWidth); |
| 1423 | assert(DemandedElts != EltMask && (DemandedElts & ~EltMask) == 0 && |
| 1424 | "Invalid DemandedElts!"); |
| 1425 | |
| 1426 | if (isa<UndefValue>(V)) { |
| 1427 | // If the entire vector is undefined, just return this info. |
| 1428 | UndefElts = EltMask; |
| 1429 | return 0; |
| 1430 | } else if (DemandedElts == 0) { // If nothing is demanded, provide undef. |
| 1431 | UndefElts = EltMask; |
| 1432 | return UndefValue::get(V->getType()); |
| 1433 | } |
| 1434 | |
| 1435 | UndefElts = 0; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1436 | if (ConstantVector *CP = dyn_cast<ConstantVector>(V)) { |
| 1437 | const Type *EltTy = cast<VectorType>(V->getType())->getElementType(); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1438 | Constant *Undef = UndefValue::get(EltTy); |
| 1439 | |
| 1440 | std::vector<Constant*> Elts; |
| 1441 | for (unsigned i = 0; i != VWidth; ++i) |
| 1442 | if (!(DemandedElts & (1ULL << i))) { // If not demanded, set to undef. |
| 1443 | Elts.push_back(Undef); |
| 1444 | UndefElts |= (1ULL << i); |
| 1445 | } else if (isa<UndefValue>(CP->getOperand(i))) { // Already undef. |
| 1446 | Elts.push_back(Undef); |
| 1447 | UndefElts |= (1ULL << i); |
| 1448 | } else { // Otherwise, defined. |
| 1449 | Elts.push_back(CP->getOperand(i)); |
| 1450 | } |
| 1451 | |
| 1452 | // If we changed the constant, return it. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1453 | Constant *NewCP = ConstantVector::get(Elts); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1454 | return NewCP != CP ? NewCP : 0; |
| 1455 | } else if (isa<ConstantAggregateZero>(V)) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1456 | // Simplify the CAZ to a ConstantVector where the non-demanded elements are |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1457 | // set to undef. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1458 | const Type *EltTy = cast<VectorType>(V->getType())->getElementType(); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1459 | Constant *Zero = Constant::getNullValue(EltTy); |
| 1460 | Constant *Undef = UndefValue::get(EltTy); |
| 1461 | std::vector<Constant*> Elts; |
| 1462 | for (unsigned i = 0; i != VWidth; ++i) |
| 1463 | Elts.push_back((DemandedElts & (1ULL << i)) ? Zero : Undef); |
| 1464 | UndefElts = DemandedElts ^ EltMask; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1465 | return ConstantVector::get(Elts); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | if (!V->hasOneUse()) { // Other users may use these bits. |
| 1469 | if (Depth != 0) { // Not at the root. |
| 1470 | // TODO: Just compute the UndefElts information recursively. |
| 1471 | return false; |
| 1472 | } |
| 1473 | return false; |
| 1474 | } else if (Depth == 10) { // Limit search depth. |
| 1475 | return false; |
| 1476 | } |
| 1477 | |
| 1478 | Instruction *I = dyn_cast<Instruction>(V); |
| 1479 | if (!I) return false; // Only analyze instructions. |
| 1480 | |
| 1481 | bool MadeChange = false; |
| 1482 | uint64_t UndefElts2; |
| 1483 | Value *TmpV; |
| 1484 | switch (I->getOpcode()) { |
| 1485 | default: break; |
| 1486 | |
| 1487 | case Instruction::InsertElement: { |
| 1488 | // If this is a variable index, we don't know which element it overwrites. |
| 1489 | // demand exactly the same input as we produce. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1490 | ConstantInt *Idx = dyn_cast<ConstantInt>(I->getOperand(2)); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1491 | if (Idx == 0) { |
| 1492 | // Note that we can't propagate undef elt info, because we don't know |
| 1493 | // which elt is getting updated. |
| 1494 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, |
| 1495 | UndefElts2, Depth+1); |
| 1496 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } |
| 1497 | break; |
| 1498 | } |
| 1499 | |
| 1500 | // If this is inserting an element that isn't demanded, remove this |
| 1501 | // insertelement. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1502 | unsigned IdxNo = Idx->getZExtValue(); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1503 | if (IdxNo >= VWidth || (DemandedElts & (1ULL << IdxNo)) == 0) |
| 1504 | return AddSoonDeadInstToWorklist(*I, 0); |
| 1505 | |
| 1506 | // Otherwise, the element inserted overwrites whatever was there, so the |
| 1507 | // input demanded set is simpler than the output set. |
| 1508 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), |
| 1509 | DemandedElts & ~(1ULL << IdxNo), |
| 1510 | UndefElts, Depth+1); |
| 1511 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } |
| 1512 | |
| 1513 | // The inserted element is defined. |
| 1514 | UndefElts |= 1ULL << IdxNo; |
| 1515 | break; |
| 1516 | } |
Chris Lattner | 6987833 | 2007-04-14 22:29:23 +0000 | [diff] [blame] | 1517 | case Instruction::BitCast: { |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 1518 | // Vector->vector casts only. |
Chris Lattner | 6987833 | 2007-04-14 22:29:23 +0000 | [diff] [blame] | 1519 | const VectorType *VTy = dyn_cast<VectorType>(I->getOperand(0)->getType()); |
| 1520 | if (!VTy) break; |
| 1521 | unsigned InVWidth = VTy->getNumElements(); |
| 1522 | uint64_t InputDemandedElts = 0; |
| 1523 | unsigned Ratio; |
| 1524 | |
| 1525 | if (VWidth == InVWidth) { |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 1526 | // 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] | 1527 | // elements as are demanded of us. |
| 1528 | Ratio = 1; |
| 1529 | InputDemandedElts = DemandedElts; |
| 1530 | } else if (VWidth > InVWidth) { |
| 1531 | // Untested so far. |
| 1532 | break; |
| 1533 | |
| 1534 | // If there are more elements in the result than there are in the source, |
| 1535 | // then an input element is live if any of the corresponding output |
| 1536 | // elements are live. |
| 1537 | Ratio = VWidth/InVWidth; |
| 1538 | for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) { |
| 1539 | if (DemandedElts & (1ULL << OutIdx)) |
| 1540 | InputDemandedElts |= 1ULL << (OutIdx/Ratio); |
| 1541 | } |
| 1542 | } else { |
| 1543 | // Untested so far. |
| 1544 | break; |
| 1545 | |
| 1546 | // If there are more elements in the source than there are in the result, |
| 1547 | // then an input element is live if the corresponding output element is |
| 1548 | // live. |
| 1549 | Ratio = InVWidth/VWidth; |
| 1550 | for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx) |
| 1551 | if (DemandedElts & (1ULL << InIdx/Ratio)) |
| 1552 | InputDemandedElts |= 1ULL << InIdx; |
| 1553 | } |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1554 | |
Chris Lattner | 6987833 | 2007-04-14 22:29:23 +0000 | [diff] [blame] | 1555 | // div/rem demand all inputs, because they don't want divide by zero. |
| 1556 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), InputDemandedElts, |
| 1557 | UndefElts2, Depth+1); |
| 1558 | if (TmpV) { |
| 1559 | I->setOperand(0, TmpV); |
| 1560 | MadeChange = true; |
| 1561 | } |
| 1562 | |
| 1563 | UndefElts = UndefElts2; |
| 1564 | if (VWidth > InVWidth) { |
| 1565 | assert(0 && "Unimp"); |
| 1566 | // If there are more elements in the result than there are in the source, |
| 1567 | // then an output element is undef if the corresponding input element is |
| 1568 | // undef. |
| 1569 | for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) |
| 1570 | if (UndefElts2 & (1ULL << (OutIdx/Ratio))) |
| 1571 | UndefElts |= 1ULL << OutIdx; |
| 1572 | } else if (VWidth < InVWidth) { |
| 1573 | assert(0 && "Unimp"); |
| 1574 | // If there are more elements in the source than there are in the result, |
| 1575 | // then a result element is undef if all of the corresponding input |
| 1576 | // elements are undef. |
| 1577 | UndefElts = ~0ULL >> (64-VWidth); // Start out all undef. |
| 1578 | for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx) |
| 1579 | if ((UndefElts2 & (1ULL << InIdx)) == 0) // Not undef? |
| 1580 | UndefElts &= ~(1ULL << (InIdx/Ratio)); // Clear undef bit. |
| 1581 | } |
| 1582 | break; |
| 1583 | } |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1584 | case Instruction::And: |
| 1585 | case Instruction::Or: |
| 1586 | case Instruction::Xor: |
| 1587 | case Instruction::Add: |
| 1588 | case Instruction::Sub: |
| 1589 | case Instruction::Mul: |
| 1590 | // div/rem demand all inputs, because they don't want divide by zero. |
| 1591 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, |
| 1592 | UndefElts, Depth+1); |
| 1593 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } |
| 1594 | TmpV = SimplifyDemandedVectorElts(I->getOperand(1), DemandedElts, |
| 1595 | UndefElts2, Depth+1); |
| 1596 | if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; } |
| 1597 | |
| 1598 | // Output elements are undefined if both are undefined. Consider things |
| 1599 | // like undef&0. The result is known zero, not undef. |
| 1600 | UndefElts &= UndefElts2; |
| 1601 | break; |
| 1602 | |
| 1603 | case Instruction::Call: { |
| 1604 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(I); |
| 1605 | if (!II) break; |
| 1606 | switch (II->getIntrinsicID()) { |
| 1607 | default: break; |
| 1608 | |
| 1609 | // Binary vector operations that work column-wise. A dest element is a |
| 1610 | // function of the corresponding input elements from the two inputs. |
| 1611 | case Intrinsic::x86_sse_sub_ss: |
| 1612 | case Intrinsic::x86_sse_mul_ss: |
| 1613 | case Intrinsic::x86_sse_min_ss: |
| 1614 | case Intrinsic::x86_sse_max_ss: |
| 1615 | case Intrinsic::x86_sse2_sub_sd: |
| 1616 | case Intrinsic::x86_sse2_mul_sd: |
| 1617 | case Intrinsic::x86_sse2_min_sd: |
| 1618 | case Intrinsic::x86_sse2_max_sd: |
| 1619 | TmpV = SimplifyDemandedVectorElts(II->getOperand(1), DemandedElts, |
| 1620 | UndefElts, Depth+1); |
| 1621 | if (TmpV) { II->setOperand(1, TmpV); MadeChange = true; } |
| 1622 | TmpV = SimplifyDemandedVectorElts(II->getOperand(2), DemandedElts, |
| 1623 | UndefElts2, Depth+1); |
| 1624 | if (TmpV) { II->setOperand(2, TmpV); MadeChange = true; } |
| 1625 | |
| 1626 | // If only the low elt is demanded and this is a scalarizable intrinsic, |
| 1627 | // scalarize it now. |
| 1628 | if (DemandedElts == 1) { |
| 1629 | switch (II->getIntrinsicID()) { |
| 1630 | default: break; |
| 1631 | case Intrinsic::x86_sse_sub_ss: |
| 1632 | case Intrinsic::x86_sse_mul_ss: |
| 1633 | case Intrinsic::x86_sse2_sub_sd: |
| 1634 | case Intrinsic::x86_sse2_mul_sd: |
| 1635 | // TODO: Lower MIN/MAX/ABS/etc |
| 1636 | Value *LHS = II->getOperand(1); |
| 1637 | Value *RHS = II->getOperand(2); |
| 1638 | // Extract the element as scalars. |
| 1639 | LHS = InsertNewInstBefore(new ExtractElementInst(LHS, 0U,"tmp"), *II); |
| 1640 | RHS = InsertNewInstBefore(new ExtractElementInst(RHS, 0U,"tmp"), *II); |
| 1641 | |
| 1642 | switch (II->getIntrinsicID()) { |
| 1643 | default: assert(0 && "Case stmts out of sync!"); |
| 1644 | case Intrinsic::x86_sse_sub_ss: |
| 1645 | case Intrinsic::x86_sse2_sub_sd: |
| 1646 | TmpV = InsertNewInstBefore(BinaryOperator::createSub(LHS, RHS, |
| 1647 | II->getName()), *II); |
| 1648 | break; |
| 1649 | case Intrinsic::x86_sse_mul_ss: |
| 1650 | case Intrinsic::x86_sse2_mul_sd: |
| 1651 | TmpV = InsertNewInstBefore(BinaryOperator::createMul(LHS, RHS, |
| 1652 | II->getName()), *II); |
| 1653 | break; |
| 1654 | } |
| 1655 | |
| 1656 | Instruction *New = |
| 1657 | new InsertElementInst(UndefValue::get(II->getType()), TmpV, 0U, |
| 1658 | II->getName()); |
| 1659 | InsertNewInstBefore(New, *II); |
| 1660 | AddSoonDeadInstToWorklist(*II, 0); |
| 1661 | return New; |
| 1662 | } |
| 1663 | } |
| 1664 | |
| 1665 | // Output elements are undefined if both are undefined. Consider things |
| 1666 | // like undef&0. The result is known zero, not undef. |
| 1667 | UndefElts &= UndefElts2; |
| 1668 | break; |
| 1669 | } |
| 1670 | break; |
| 1671 | } |
| 1672 | } |
| 1673 | return MadeChange ? I : 0; |
| 1674 | } |
| 1675 | |
Nick Lewycky | 455e176 | 2007-09-06 02:40:25 +0000 | [diff] [blame] | 1676 | /// @returns true if the specified compare predicate is |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1677 | /// true when both operands are equal... |
Nick Lewycky | 455e176 | 2007-09-06 02:40:25 +0000 | [diff] [blame] | 1678 | /// @brief Determine if the icmp Predicate is true when both operands are equal |
| 1679 | static bool isTrueWhenEqual(ICmpInst::Predicate pred) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1680 | return pred == ICmpInst::ICMP_EQ || pred == ICmpInst::ICMP_UGE || |
| 1681 | pred == ICmpInst::ICMP_SGE || pred == ICmpInst::ICMP_ULE || |
| 1682 | pred == ICmpInst::ICMP_SLE; |
| 1683 | } |
| 1684 | |
Nick Lewycky | 455e176 | 2007-09-06 02:40:25 +0000 | [diff] [blame] | 1685 | /// @returns true if the specified compare instruction is |
| 1686 | /// true when both operands are equal... |
| 1687 | /// @brief Determine if the ICmpInst returns true when both operands are equal |
| 1688 | static bool isTrueWhenEqual(ICmpInst &ICI) { |
| 1689 | return isTrueWhenEqual(ICI.getPredicate()); |
| 1690 | } |
| 1691 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1692 | /// AssociativeOpt - Perform an optimization on an associative operator. This |
| 1693 | /// function is designed to check a chain of associative operators for a |
| 1694 | /// potential to apply a certain optimization. Since the optimization may be |
| 1695 | /// applicable if the expression was reassociated, this checks the chain, then |
| 1696 | /// reassociates the expression as necessary to expose the optimization |
| 1697 | /// opportunity. This makes use of a special Functor, which must define |
| 1698 | /// 'shouldApply' and 'apply' methods. |
| 1699 | /// |
| 1700 | template<typename Functor> |
| 1701 | Instruction *AssociativeOpt(BinaryOperator &Root, const Functor &F) { |
| 1702 | unsigned Opcode = Root.getOpcode(); |
| 1703 | Value *LHS = Root.getOperand(0); |
| 1704 | |
| 1705 | // Quick check, see if the immediate LHS matches... |
| 1706 | if (F.shouldApply(LHS)) |
| 1707 | return F.apply(Root); |
| 1708 | |
| 1709 | // Otherwise, if the LHS is not of the same opcode as the root, return. |
| 1710 | Instruction *LHSI = dyn_cast<Instruction>(LHS); |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 1711 | while (LHSI && LHSI->getOpcode() == Opcode && LHSI->hasOneUse()) { |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1712 | // Should we apply this transform to the RHS? |
| 1713 | bool ShouldApply = F.shouldApply(LHSI->getOperand(1)); |
| 1714 | |
| 1715 | // If not to the RHS, check to see if we should apply to the LHS... |
| 1716 | if (!ShouldApply && F.shouldApply(LHSI->getOperand(0))) { |
| 1717 | cast<BinaryOperator>(LHSI)->swapOperands(); // Make the LHS the RHS |
| 1718 | ShouldApply = true; |
| 1719 | } |
| 1720 | |
| 1721 | // If the functor wants to apply the optimization to the RHS of LHSI, |
| 1722 | // reassociate the expression from ((? op A) op B) to (? op (A op B)) |
| 1723 | if (ShouldApply) { |
| 1724 | BasicBlock *BB = Root.getParent(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1725 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1726 | // Now all of the instructions are in the current basic block, go ahead |
| 1727 | // and perform the reassociation. |
| 1728 | Instruction *TmpLHSI = cast<Instruction>(Root.getOperand(0)); |
| 1729 | |
| 1730 | // First move the selected RHS to the LHS of the root... |
| 1731 | Root.setOperand(0, LHSI->getOperand(1)); |
| 1732 | |
| 1733 | // Make what used to be the LHS of the root be the user of the root... |
| 1734 | Value *ExtraOperand = TmpLHSI->getOperand(1); |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1735 | if (&Root == TmpLHSI) { |
Chris Lattner | 15a76c0 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 1736 | Root.replaceAllUsesWith(Constant::getNullValue(TmpLHSI->getType())); |
| 1737 | return 0; |
| 1738 | } |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1739 | Root.replaceAllUsesWith(TmpLHSI); // Users now use TmpLHSI |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1740 | TmpLHSI->setOperand(1, &Root); // TmpLHSI now uses the root |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1741 | TmpLHSI->getParent()->getInstList().remove(TmpLHSI); |
| 1742 | BasicBlock::iterator ARI = &Root; ++ARI; |
| 1743 | BB->getInstList().insert(ARI, TmpLHSI); // Move TmpLHSI to after Root |
| 1744 | ARI = Root; |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1745 | |
| 1746 | // Now propagate the ExtraOperand down the chain of instructions until we |
| 1747 | // get to LHSI. |
| 1748 | while (TmpLHSI != LHSI) { |
| 1749 | Instruction *NextLHSI = cast<Instruction>(TmpLHSI->getOperand(0)); |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1750 | // Move the instruction to immediately before the chain we are |
| 1751 | // constructing to avoid breaking dominance properties. |
| 1752 | NextLHSI->getParent()->getInstList().remove(NextLHSI); |
| 1753 | BB->getInstList().insert(ARI, NextLHSI); |
| 1754 | ARI = NextLHSI; |
| 1755 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1756 | Value *NextOp = NextLHSI->getOperand(1); |
| 1757 | NextLHSI->setOperand(1, ExtraOperand); |
| 1758 | TmpLHSI = NextLHSI; |
| 1759 | ExtraOperand = NextOp; |
| 1760 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1761 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1762 | // Now that the instructions are reassociated, have the functor perform |
| 1763 | // the transformation... |
| 1764 | return F.apply(Root); |
| 1765 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1766 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1767 | LHSI = dyn_cast<Instruction>(LHSI->getOperand(0)); |
| 1768 | } |
| 1769 | return 0; |
| 1770 | } |
| 1771 | |
| 1772 | |
| 1773 | // AddRHS - Implements: X + X --> X << 1 |
| 1774 | struct AddRHS { |
| 1775 | Value *RHS; |
| 1776 | AddRHS(Value *rhs) : RHS(rhs) {} |
| 1777 | bool shouldApply(Value *LHS) const { return LHS == RHS; } |
| 1778 | Instruction *apply(BinaryOperator &Add) const { |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 1779 | return BinaryOperator::createShl(Add.getOperand(0), |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1780 | ConstantInt::get(Add.getType(), 1)); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1781 | } |
| 1782 | }; |
| 1783 | |
| 1784 | // AddMaskingAnd - Implements (A & C1)+(B & C2) --> (A & C1)|(B & C2) |
| 1785 | // iff C1&C2 == 0 |
| 1786 | struct AddMaskingAnd { |
| 1787 | Constant *C2; |
| 1788 | AddMaskingAnd(Constant *c) : C2(c) {} |
| 1789 | bool shouldApply(Value *LHS) const { |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1790 | ConstantInt *C1; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1791 | return match(LHS, m_And(m_Value(), m_ConstantInt(C1))) && |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1792 | ConstantExpr::getAnd(C1, C2)->isNullValue(); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1793 | } |
| 1794 | Instruction *apply(BinaryOperator &Add) const { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1795 | return BinaryOperator::createOr(Add.getOperand(0), Add.getOperand(1)); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1796 | } |
| 1797 | }; |
| 1798 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1799 | static Value *FoldOperationIntoSelectOperand(Instruction &I, Value *SO, |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1800 | InstCombiner *IC) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1801 | if (CastInst *CI = dyn_cast<CastInst>(&I)) { |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1802 | if (Constant *SOC = dyn_cast<Constant>(SO)) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1803 | return ConstantExpr::getCast(CI->getOpcode(), SOC, I.getType()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1804 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1805 | return IC->InsertNewInstBefore(CastInst::create( |
| 1806 | CI->getOpcode(), SO, I.getType(), SO->getName() + ".cast"), I); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1807 | } |
| 1808 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1809 | // Figure out if the constant is the left or the right argument. |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1810 | bool ConstIsRHS = isa<Constant>(I.getOperand(1)); |
| 1811 | Constant *ConstOperand = cast<Constant>(I.getOperand(ConstIsRHS)); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1812 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1813 | if (Constant *SOC = dyn_cast<Constant>(SO)) { |
| 1814 | if (ConstIsRHS) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1815 | return ConstantExpr::get(I.getOpcode(), SOC, ConstOperand); |
| 1816 | return ConstantExpr::get(I.getOpcode(), ConstOperand, SOC); |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1817 | } |
| 1818 | |
| 1819 | Value *Op0 = SO, *Op1 = ConstOperand; |
| 1820 | if (!ConstIsRHS) |
| 1821 | std::swap(Op0, Op1); |
| 1822 | Instruction *New; |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1823 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I)) |
| 1824 | New = BinaryOperator::create(BO->getOpcode(), Op0, Op1,SO->getName()+".op"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1825 | else if (CmpInst *CI = dyn_cast<CmpInst>(&I)) |
| 1826 | New = CmpInst::create(CI->getOpcode(), CI->getPredicate(), Op0, Op1, |
| 1827 | SO->getName()+".cmp"); |
Chris Lattner | 326c0f3 | 2004-04-10 19:15:56 +0000 | [diff] [blame] | 1828 | else { |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1829 | assert(0 && "Unknown binary instruction type!"); |
Chris Lattner | 326c0f3 | 2004-04-10 19:15:56 +0000 | [diff] [blame] | 1830 | abort(); |
| 1831 | } |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1832 | return IC->InsertNewInstBefore(New, I); |
| 1833 | } |
| 1834 | |
| 1835 | // FoldOpIntoSelect - Given an instruction with a select as one operand and a |
| 1836 | // constant as the other operand, try to fold the binary operator into the |
| 1837 | // select arguments. This also works for Cast instructions, which obviously do |
| 1838 | // not have a second operand. |
| 1839 | static Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI, |
| 1840 | InstCombiner *IC) { |
| 1841 | // Don't modify shared select instructions |
| 1842 | if (!SI->hasOneUse()) return 0; |
| 1843 | Value *TV = SI->getOperand(1); |
| 1844 | Value *FV = SI->getOperand(2); |
| 1845 | |
| 1846 | if (isa<Constant>(TV) || isa<Constant>(FV)) { |
Chris Lattner | 956db27 | 2005-04-21 05:43:13 +0000 | [diff] [blame] | 1847 | // Bool selects with constant operands can be folded to logical ops. |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 1848 | if (SI->getType() == Type::Int1Ty) return 0; |
Chris Lattner | 956db27 | 2005-04-21 05:43:13 +0000 | [diff] [blame] | 1849 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1850 | Value *SelectTrueVal = FoldOperationIntoSelectOperand(Op, TV, IC); |
| 1851 | Value *SelectFalseVal = FoldOperationIntoSelectOperand(Op, FV, IC); |
| 1852 | |
| 1853 | return new SelectInst(SI->getCondition(), SelectTrueVal, |
| 1854 | SelectFalseVal); |
| 1855 | } |
| 1856 | return 0; |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1857 | } |
| 1858 | |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1859 | |
| 1860 | /// FoldOpIntoPhi - Given a binary operator or cast instruction which has a PHI |
| 1861 | /// node as operand #0, see if we can fold the instruction into the PHI (which |
| 1862 | /// is only possible if all operands to the PHI are constants). |
| 1863 | Instruction *InstCombiner::FoldOpIntoPhi(Instruction &I) { |
| 1864 | PHINode *PN = cast<PHINode>(I.getOperand(0)); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 1865 | unsigned NumPHIValues = PN->getNumIncomingValues(); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1866 | if (!PN->hasOneUse() || NumPHIValues == 0) return 0; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1867 | |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1868 | // Check to see if all of the operands of the PHI are constants. If there is |
| 1869 | // 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] | 1870 | // or if *it* is a PHI, bail out. |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1871 | BasicBlock *NonConstBB = 0; |
| 1872 | for (unsigned i = 0; i != NumPHIValues; ++i) |
| 1873 | if (!isa<Constant>(PN->getIncomingValue(i))) { |
| 1874 | if (NonConstBB) return 0; // More than one non-const value. |
Chris Lattner | b303668 | 2007-02-24 01:03:45 +0000 | [diff] [blame] | 1875 | if (isa<PHINode>(PN->getIncomingValue(i))) return 0; // Itself a phi. |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1876 | NonConstBB = PN->getIncomingBlock(i); |
| 1877 | |
| 1878 | // If the incoming non-constant value is in I's block, we have an infinite |
| 1879 | // loop. |
| 1880 | if (NonConstBB == I.getParent()) |
| 1881 | return 0; |
| 1882 | } |
| 1883 | |
| 1884 | // If there is exactly one non-constant value, we can insert a copy of the |
| 1885 | // operation in that block. However, if this is a critical edge, we would be |
| 1886 | // inserting the computation one some other paths (e.g. inside a loop). Only |
| 1887 | // do this if the pred block is unconditionally branching into the phi block. |
| 1888 | if (NonConstBB) { |
| 1889 | BranchInst *BI = dyn_cast<BranchInst>(NonConstBB->getTerminator()); |
| 1890 | if (!BI || !BI->isUnconditional()) return 0; |
| 1891 | } |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1892 | |
| 1893 | // Okay, we can do the transformation: create the new PHI node. |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 1894 | PHINode *NewPN = new PHINode(I.getType(), ""); |
Chris Lattner | 5551706 | 2005-01-29 00:39:08 +0000 | [diff] [blame] | 1895 | NewPN->reserveOperandSpace(PN->getNumOperands()/2); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1896 | InsertNewInstBefore(NewPN, *PN); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 1897 | NewPN->takeName(PN); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1898 | |
| 1899 | // Next, add all of the operands to the PHI. |
| 1900 | if (I.getNumOperands() == 2) { |
| 1901 | Constant *C = cast<Constant>(I.getOperand(1)); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 1902 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
Chris Lattner | a9ff5eb | 2007-08-05 08:47:58 +0000 | [diff] [blame] | 1903 | Value *InV = 0; |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1904 | if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1905 | if (CmpInst *CI = dyn_cast<CmpInst>(&I)) |
| 1906 | InV = ConstantExpr::getCompare(CI->getPredicate(), InC, C); |
| 1907 | else |
| 1908 | InV = ConstantExpr::get(I.getOpcode(), InC, C); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1909 | } else { |
| 1910 | assert(PN->getIncomingBlock(i) == NonConstBB); |
| 1911 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I)) |
| 1912 | InV = BinaryOperator::create(BO->getOpcode(), |
| 1913 | PN->getIncomingValue(i), C, "phitmp", |
| 1914 | NonConstBB->getTerminator()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1915 | else if (CmpInst *CI = dyn_cast<CmpInst>(&I)) |
| 1916 | InV = CmpInst::create(CI->getOpcode(), |
| 1917 | CI->getPredicate(), |
| 1918 | PN->getIncomingValue(i), C, "phitmp", |
| 1919 | NonConstBB->getTerminator()); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1920 | else |
| 1921 | assert(0 && "Unknown binop!"); |
| 1922 | |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 1923 | AddToWorkList(cast<Instruction>(InV)); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1924 | } |
| 1925 | NewPN->addIncoming(InV, PN->getIncomingBlock(i)); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1926 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1927 | } else { |
| 1928 | CastInst *CI = cast<CastInst>(&I); |
| 1929 | const Type *RetTy = CI->getType(); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 1930 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1931 | Value *InV; |
| 1932 | if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1933 | InV = ConstantExpr::getCast(CI->getOpcode(), InC, RetTy); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1934 | } else { |
| 1935 | assert(PN->getIncomingBlock(i) == NonConstBB); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1936 | InV = CastInst::create(CI->getOpcode(), PN->getIncomingValue(i), |
| 1937 | I.getType(), "phitmp", |
| 1938 | NonConstBB->getTerminator()); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 1939 | AddToWorkList(cast<Instruction>(InV)); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1940 | } |
| 1941 | NewPN->addIncoming(InV, PN->getIncomingBlock(i)); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1942 | } |
| 1943 | } |
| 1944 | return ReplaceInstUsesWith(I, NewPN); |
| 1945 | } |
| 1946 | |
Chris Lattner | 2454a2e | 2008-01-29 06:52:45 +0000 | [diff] [blame] | 1947 | |
| 1948 | /// CannotBeNegativeZero - Return true if we can prove that the specified FP |
| 1949 | /// value is never equal to -0.0. |
| 1950 | /// |
| 1951 | /// Note that this function will need to be revisited when we support nondefault |
| 1952 | /// rounding modes! |
| 1953 | /// |
| 1954 | static bool CannotBeNegativeZero(const Value *V) { |
| 1955 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V)) |
| 1956 | return !CFP->getValueAPF().isNegZero(); |
| 1957 | |
| 1958 | // (add x, 0.0) is guaranteed to return +0.0, not -0.0. |
| 1959 | if (const Instruction *I = dyn_cast<Instruction>(V)) { |
| 1960 | if (I->getOpcode() == Instruction::Add && |
| 1961 | isa<ConstantFP>(I->getOperand(1)) && |
| 1962 | cast<ConstantFP>(I->getOperand(1))->isNullValue()) |
| 1963 | return true; |
| 1964 | |
| 1965 | if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) |
| 1966 | if (II->getIntrinsicID() == Intrinsic::sqrt) |
| 1967 | return CannotBeNegativeZero(II->getOperand(1)); |
| 1968 | |
| 1969 | if (const CallInst *CI = dyn_cast<CallInst>(I)) |
| 1970 | if (const Function *F = CI->getCalledFunction()) { |
| 1971 | if (F->isDeclaration()) { |
| 1972 | switch (F->getNameLen()) { |
| 1973 | case 3: // abs(x) != -0.0 |
| 1974 | if (!strcmp(F->getNameStart(), "abs")) return true; |
| 1975 | break; |
| 1976 | case 4: // abs[lf](x) != -0.0 |
| 1977 | if (!strcmp(F->getNameStart(), "absf")) return true; |
| 1978 | if (!strcmp(F->getNameStart(), "absl")) return true; |
| 1979 | break; |
| 1980 | } |
| 1981 | } |
| 1982 | } |
| 1983 | } |
| 1984 | |
| 1985 | return false; |
| 1986 | } |
| 1987 | |
| 1988 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1989 | Instruction *InstCombiner::visitAdd(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 1990 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1991 | Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 1992 | |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 1993 | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 1994 | // X + undef -> undef |
| 1995 | if (isa<UndefValue>(RHS)) |
| 1996 | return ReplaceInstUsesWith(I, RHS); |
| 1997 | |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 1998 | // X + 0 --> X |
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 1999 | if (!I.getType()->isFPOrFPVector()) { // NOTE: -0 + +0 = +0. |
Chris Lattner | 5e678e0 | 2005-10-17 17:56:38 +0000 | [diff] [blame] | 2000 | if (RHSC->isNullValue()) |
| 2001 | return ReplaceInstUsesWith(I, LHS); |
Chris Lattner | 8532cf6 | 2005-10-17 20:18:38 +0000 | [diff] [blame] | 2002 | } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) { |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 2003 | if (CFP->isExactlyValue(ConstantFP::getNegativeZero |
| 2004 | (I.getType())->getValueAPF())) |
Chris Lattner | 8532cf6 | 2005-10-17 20:18:38 +0000 | [diff] [blame] | 2005 | return ReplaceInstUsesWith(I, LHS); |
Chris Lattner | 5e678e0 | 2005-10-17 17:56:38 +0000 | [diff] [blame] | 2006 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2007 | |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 2008 | if (ConstantInt *CI = dyn_cast<ConstantInt>(RHSC)) { |
Chris Lattner | b4a2f05 | 2006-11-09 05:12:27 +0000 | [diff] [blame] | 2009 | // X + (signbit) --> X ^ signbit |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2010 | const APInt& Val = CI->getValue(); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 2011 | uint32_t BitWidth = Val.getBitWidth(); |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 2012 | if (Val == APInt::getSignBit(BitWidth)) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2013 | return BinaryOperator::createXor(LHS, RHS); |
Chris Lattner | b4a2f05 | 2006-11-09 05:12:27 +0000 | [diff] [blame] | 2014 | |
| 2015 | // See if SimplifyDemandedBits can simplify this. This handles stuff like |
| 2016 | // (X & 254)+1 -> (X&254)|1 |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 2017 | if (!isa<VectorType>(I.getType())) { |
| 2018 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 2019 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth), |
| 2020 | KnownZero, KnownOne)) |
| 2021 | return &I; |
| 2022 | } |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 2023 | } |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2024 | |
| 2025 | if (isa<PHINode>(LHS)) |
| 2026 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2027 | return NV; |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2028 | |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 2029 | ConstantInt *XorRHS = 0; |
| 2030 | Value *XorLHS = 0; |
Chris Lattner | c5eff44 | 2007-01-30 22:32:46 +0000 | [diff] [blame] | 2031 | if (isa<ConstantInt>(RHSC) && |
| 2032 | match(LHS, m_Xor(m_Value(XorLHS), m_ConstantInt(XorRHS)))) { |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 2033 | uint32_t TySizeBits = I.getType()->getPrimitiveSizeInBits(); |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2034 | const APInt& RHSVal = cast<ConstantInt>(RHSC)->getValue(); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2035 | |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 2036 | uint32_t Size = TySizeBits / 2; |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 2037 | APInt C0080Val(APInt(TySizeBits, 1ULL).shl(Size - 1)); |
| 2038 | APInt CFF80Val(-C0080Val); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2039 | do { |
| 2040 | if (TySizeBits > Size) { |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2041 | // If we have ADD(XOR(AND(X, 0xFF), 0x80), 0xF..F80), it's a sext. |
| 2042 | // 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] | 2043 | if ((RHSVal == CFF80Val && XorRHS->getValue() == C0080Val) || |
| 2044 | (RHSVal == C0080Val && XorRHS->getValue() == CFF80Val)) { |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2045 | // This is a sign extend if the top bits are known zero. |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 2046 | if (!MaskedValueIsZero(XorLHS, |
| 2047 | APInt::getHighBitsSet(TySizeBits, TySizeBits - Size))) |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2048 | 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] | 2049 | break; |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2050 | } |
| 2051 | } |
| 2052 | Size >>= 1; |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 2053 | C0080Val = APIntOps::lshr(C0080Val, Size); |
| 2054 | CFF80Val = APIntOps::ashr(CFF80Val, Size); |
| 2055 | } while (Size >= 1); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2056 | |
Reid Spencer | 35c3885 | 2007-03-28 01:36:16 +0000 | [diff] [blame] | 2057 | // FIXME: This shouldn't be necessary. When the backends can handle types |
| 2058 | // with funny bit widths then this whole cascade of if statements should |
| 2059 | // be removed. It is just here to get the size of the "middle" type back |
| 2060 | // up to something that the back ends can handle. |
| 2061 | const Type *MiddleType = 0; |
| 2062 | switch (Size) { |
| 2063 | default: break; |
| 2064 | case 32: MiddleType = Type::Int32Ty; break; |
| 2065 | case 16: MiddleType = Type::Int16Ty; break; |
| 2066 | case 8: MiddleType = Type::Int8Ty; break; |
| 2067 | } |
| 2068 | if (MiddleType) { |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 2069 | Instruction *NewTrunc = new TruncInst(XorLHS, MiddleType, "sext"); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2070 | InsertNewInstBefore(NewTrunc, I); |
Reid Spencer | 35c3885 | 2007-03-28 01:36:16 +0000 | [diff] [blame] | 2071 | return new SExtInst(NewTrunc, I.getType(), I.getName()); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2072 | } |
| 2073 | } |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 2074 | } |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 2075 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2076 | // X + X --> X << 1 |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 2077 | if (I.getType()->isInteger() && I.getType() != Type::Int1Ty) { |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2078 | if (Instruction *Result = AssociativeOpt(I, AddRHS(RHS))) return Result; |
Chris Lattner | 7edc8c2 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 2079 | |
| 2080 | if (Instruction *RHSI = dyn_cast<Instruction>(RHS)) { |
| 2081 | if (RHSI->getOpcode() == Instruction::Sub) |
| 2082 | if (LHS == RHSI->getOperand(1)) // A + (B - A) --> B |
| 2083 | return ReplaceInstUsesWith(I, RHSI->getOperand(0)); |
| 2084 | } |
| 2085 | if (Instruction *LHSI = dyn_cast<Instruction>(LHS)) { |
| 2086 | if (LHSI->getOpcode() == Instruction::Sub) |
| 2087 | if (RHS == LHSI->getOperand(1)) // (B - A) + A --> B |
| 2088 | return ReplaceInstUsesWith(I, LHSI->getOperand(0)); |
| 2089 | } |
Robert Bocchino | 7169828 | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 2090 | } |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 2091 | |
Chris Lattner | 5c4afb9 | 2002-05-08 22:46:53 +0000 | [diff] [blame] | 2092 | // -A + B --> B - A |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 2093 | if (Value *V = dyn_castNegVal(LHS)) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2094 | return BinaryOperator::createSub(RHS, V); |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 2095 | |
| 2096 | // A + -B --> A - B |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 2097 | if (!isa<Constant>(RHS)) |
| 2098 | if (Value *V = dyn_castNegVal(RHS)) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2099 | return BinaryOperator::createSub(LHS, V); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2100 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2101 | |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2102 | ConstantInt *C2; |
| 2103 | if (Value *X = dyn_castFoldableMul(LHS, C2)) { |
| 2104 | if (X == RHS) // X*C + X --> X * (C+1) |
| 2105 | return BinaryOperator::createMul(RHS, AddOne(C2)); |
| 2106 | |
| 2107 | // X*C1 + X*C2 --> X * (C1+C2) |
| 2108 | ConstantInt *C1; |
| 2109 | if (X == dyn_castFoldableMul(RHS, C1)) |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2110 | return BinaryOperator::createMul(X, Add(C1, C2)); |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2111 | } |
| 2112 | |
| 2113 | // X + X*C --> X * (C+1) |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2114 | if (dyn_castFoldableMul(RHS, C2) == LHS) |
| 2115 | return BinaryOperator::createMul(LHS, AddOne(C2)); |
| 2116 | |
Chris Lattner | e617c9e | 2007-01-05 02:17:46 +0000 | [diff] [blame] | 2117 | // X + ~X --> -1 since ~X = -X-1 |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 2118 | if (dyn_castNotVal(LHS) == RHS || dyn_castNotVal(RHS) == LHS) |
| 2119 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | e617c9e | 2007-01-05 02:17:46 +0000 | [diff] [blame] | 2120 | |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2121 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2122 | // (A & C1)+(B & C2) --> (A & C1)|(B & C2) iff C1&C2 == 0 |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2123 | if (match(RHS, m_And(m_Value(), m_ConstantInt(C2)))) |
Chris Lattner | e617c9e | 2007-01-05 02:17:46 +0000 | [diff] [blame] | 2124 | if (Instruction *R = AssociativeOpt(I, AddMaskingAnd(C2))) |
| 2125 | return R; |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 2126 | |
Nick Lewycky | b6eabff | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 2127 | // W*X + Y*Z --> W * (X+Z) iff W == Y |
Nick Lewycky | 0c2c3f6 | 2008-02-03 08:19:11 +0000 | [diff] [blame] | 2128 | if (I.getType()->isIntOrIntVector()) { |
Nick Lewycky | b6eabff | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 2129 | Value *W, *X, *Y, *Z; |
| 2130 | if (match(LHS, m_Mul(m_Value(W), m_Value(X))) && |
| 2131 | match(RHS, m_Mul(m_Value(Y), m_Value(Z)))) { |
| 2132 | if (W != Y) { |
| 2133 | if (W == Z) { |
| 2134 | std::swap(Y, Z); |
| 2135 | } else if (Y == X) { |
| 2136 | std::swap(W, X); |
| 2137 | } else if (X == Z) { |
| 2138 | std::swap(Y, Z); |
| 2139 | std::swap(W, X); |
| 2140 | } |
| 2141 | } |
| 2142 | |
| 2143 | if (W == Y) { |
| 2144 | Value *NewAdd = InsertNewInstBefore(BinaryOperator::createAdd(X, Z, |
| 2145 | LHS->getName()), I); |
| 2146 | return BinaryOperator::createMul(W, NewAdd); |
| 2147 | } |
| 2148 | } |
| 2149 | } |
| 2150 | |
Chris Lattner | 6b03205 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 2151 | if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) { |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 2152 | Value *X = 0; |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2153 | if (match(LHS, m_Not(m_Value(X)))) // ~X + C --> (C-1) - X |
| 2154 | return BinaryOperator::createSub(SubOne(CRHS), X); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2155 | |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2156 | // (X & FF00) + xx00 -> (X+xx00) & FF00 |
| 2157 | 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] | 2158 | Constant *Anded = And(CRHS, C2); |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2159 | if (Anded == CRHS) { |
| 2160 | // See if all bits from the first bit set in the Add RHS up are included |
| 2161 | // in the mask. First, get the rightmost bit. |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2162 | const APInt& AddRHSV = CRHS->getValue(); |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2163 | |
| 2164 | // 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] | 2165 | APInt AddRHSHighBits(~((AddRHSV & -AddRHSV)-1)); |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2166 | |
| 2167 | // See if the and mask includes all of these bits. |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2168 | APInt AddRHSHighBitsAnd(AddRHSHighBits & C2->getValue()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2169 | |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2170 | if (AddRHSHighBits == AddRHSHighBitsAnd) { |
| 2171 | // Okay, the xform is safe. Insert the new add pronto. |
| 2172 | Value *NewAdd = InsertNewInstBefore(BinaryOperator::createAdd(X, CRHS, |
| 2173 | LHS->getName()), I); |
| 2174 | return BinaryOperator::createAnd(NewAdd, C2); |
| 2175 | } |
| 2176 | } |
| 2177 | } |
| 2178 | |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2179 | // Try to fold constant add into select arguments. |
| 2180 | if (SelectInst *SI = dyn_cast<SelectInst>(LHS)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2181 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2182 | return R; |
Chris Lattner | 6b03205 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 2183 | } |
| 2184 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2185 | // add (cast *A to intptrtype) B -> |
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2186 | // cast (GEP (cast *A to sbyte*) B) --> intptrtype |
Andrew Lenharth | 16d7955 | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 2187 | { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2188 | CastInst *CI = dyn_cast<CastInst>(LHS); |
| 2189 | Value *Other = RHS; |
Andrew Lenharth | 16d7955 | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 2190 | if (!CI) { |
| 2191 | CI = dyn_cast<CastInst>(RHS); |
| 2192 | Other = LHS; |
| 2193 | } |
Andrew Lenharth | 4563326 | 2006-09-20 15:37:57 +0000 | [diff] [blame] | 2194 | if (CI && CI->getType()->isSized() && |
Reid Spencer | abaa8ca | 2007-01-08 16:32:00 +0000 | [diff] [blame] | 2195 | (CI->getType()->getPrimitiveSizeInBits() == |
| 2196 | TD->getIntPtrType()->getPrimitiveSizeInBits()) |
Andrew Lenharth | 4563326 | 2006-09-20 15:37:57 +0000 | [diff] [blame] | 2197 | && isa<PointerType>(CI->getOperand(0)->getType())) { |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 2198 | unsigned AS = |
| 2199 | cast<PointerType>(CI->getOperand(0)->getType())->getAddressSpace(); |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 2200 | Value *I2 = InsertBitCastBefore(CI->getOperand(0), |
| 2201 | PointerType::get(Type::Int8Ty, AS), I); |
Andrew Lenharth | 4563326 | 2006-09-20 15:37:57 +0000 | [diff] [blame] | 2202 | I2 = InsertNewInstBefore(new GetElementPtrInst(I2, Other, "ctg2"), I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2203 | return new PtrToIntInst(I2, CI->getType()); |
Andrew Lenharth | 16d7955 | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 2204 | } |
| 2205 | } |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2206 | |
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2207 | // add (select X 0 (sub n A)) A --> select X A n |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2208 | { |
| 2209 | SelectInst *SI = dyn_cast<SelectInst>(LHS); |
| 2210 | Value *Other = RHS; |
| 2211 | if (!SI) { |
| 2212 | SI = dyn_cast<SelectInst>(RHS); |
| 2213 | Other = LHS; |
| 2214 | } |
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2215 | if (SI && SI->hasOneUse()) { |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2216 | Value *TV = SI->getTrueValue(); |
| 2217 | Value *FV = SI->getFalseValue(); |
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2218 | Value *A, *N; |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2219 | |
| 2220 | // Can we fold the add into the argument of the select? |
| 2221 | // We check both true and false select arguments for a matching subtract. |
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2222 | if (match(FV, m_Zero()) && match(TV, m_Sub(m_Value(N), m_Value(A))) && |
| 2223 | A == Other) // Fold the add into the true select value. |
| 2224 | return new SelectInst(SI->getCondition(), N, A); |
| 2225 | if (match(TV, m_Zero()) && match(FV, m_Sub(m_Value(N), m_Value(A))) && |
| 2226 | A == Other) // Fold the add into the false select value. |
| 2227 | return new SelectInst(SI->getCondition(), A, N); |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2228 | } |
| 2229 | } |
Chris Lattner | 2454a2e | 2008-01-29 06:52:45 +0000 | [diff] [blame] | 2230 | |
| 2231 | // Check for X+0.0. Simplify it to X if we know X is not -0.0. |
| 2232 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHS)) |
| 2233 | if (CFP->getValueAPF().isPosZero() && CannotBeNegativeZero(LHS)) |
| 2234 | return ReplaceInstUsesWith(I, LHS); |
Andrew Lenharth | 16d7955 | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 2235 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2236 | return Changed ? &I : 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2237 | } |
| 2238 | |
Chris Lattner | 1ba5bcd | 2003-07-22 21:46:59 +0000 | [diff] [blame] | 2239 | // isSignBit - Return true if the value represented by the constant only has the |
| 2240 | // highest order bit set. |
| 2241 | static bool isSignBit(ConstantInt *CI) { |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 2242 | uint32_t NumBits = CI->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 5a1e3e1 | 2007-03-19 20:58:18 +0000 | [diff] [blame] | 2243 | return CI->getValue() == APInt::getSignBit(NumBits); |
Chris Lattner | 1ba5bcd | 2003-07-22 21:46:59 +0000 | [diff] [blame] | 2244 | } |
| 2245 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2246 | Instruction *InstCombiner::visitSub(BinaryOperator &I) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2247 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2248 | |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2249 | if (Op0 == Op1) // sub X, X -> 0 |
| 2250 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2251 | |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2252 | // If this is a 'B = x-(-A)', change to B = x+A... |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 2253 | if (Value *V = dyn_castNegVal(Op1)) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2254 | return BinaryOperator::createAdd(Op0, V); |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 2255 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2256 | if (isa<UndefValue>(Op0)) |
| 2257 | return ReplaceInstUsesWith(I, Op0); // undef - X -> undef |
| 2258 | if (isa<UndefValue>(Op1)) |
| 2259 | return ReplaceInstUsesWith(I, Op1); // X - undef -> undef |
| 2260 | |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 2261 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op0)) { |
| 2262 | // Replace (-1 - A) with (~A)... |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2263 | if (C->isAllOnesValue()) |
| 2264 | return BinaryOperator::createNot(Op1); |
Chris Lattner | 4037171 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 2265 | |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 2266 | // C - ~X == X + (1+C) |
Reid Spencer | 4b828e6 | 2005-06-18 17:37:34 +0000 | [diff] [blame] | 2267 | Value *X = 0; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2268 | if (match(Op1, m_Not(m_Value(X)))) |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2269 | return BinaryOperator::createAdd(X, AddOne(C)); |
| 2270 | |
Chris Lattner | 76b7a06 | 2007-01-15 07:02:54 +0000 | [diff] [blame] | 2271 | // -(X >>u 31) -> (X >>s 31) |
| 2272 | // -(X >>s 31) -> (X >>u 31) |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 2273 | if (C->isZero()) { |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2274 | if (BinaryOperator *SI = dyn_cast<BinaryOperator>(Op1)) |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2275 | if (SI->getOpcode() == Instruction::LShr) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2276 | if (ConstantInt *CU = dyn_cast<ConstantInt>(SI->getOperand(1))) { |
Chris Lattner | 9c29067 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 2277 | // 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] | 2278 | if (CU->getLimitedValue(SI->getType()->getPrimitiveSizeInBits()) == |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2279 | SI->getType()->getPrimitiveSizeInBits()-1) { |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2280 | // Ok, the transformation is safe. Insert AShr. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2281 | return BinaryOperator::create(Instruction::AShr, |
| 2282 | SI->getOperand(0), CU, SI->getName()); |
Chris Lattner | 9c29067 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 2283 | } |
| 2284 | } |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2285 | } |
| 2286 | else if (SI->getOpcode() == Instruction::AShr) { |
| 2287 | if (ConstantInt *CU = dyn_cast<ConstantInt>(SI->getOperand(1))) { |
| 2288 | // 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] | 2289 | if (CU->getLimitedValue(SI->getType()->getPrimitiveSizeInBits()) == |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2290 | SI->getType()->getPrimitiveSizeInBits()-1) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 2291 | // Ok, the transformation is safe. Insert LShr. |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 2292 | return BinaryOperator::createLShr( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2293 | SI->getOperand(0), CU, SI->getName()); |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2294 | } |
| 2295 | } |
| 2296 | } |
Chris Lattner | bfe492b | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 2297 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2298 | |
| 2299 | // Try to fold constant sub into select arguments. |
| 2300 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2301 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2302 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2303 | |
| 2304 | if (isa<PHINode>(Op0)) |
| 2305 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2306 | return NV; |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 2307 | } |
| 2308 | |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2309 | if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) { |
| 2310 | if (Op1I->getOpcode() == Instruction::Add && |
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 2311 | !Op0->getType()->isFPOrFPVector()) { |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2312 | if (Op1I->getOperand(0) == Op0) // X-(X+Y) == -Y |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2313 | return BinaryOperator::createNeg(Op1I->getOperand(1), I.getName()); |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2314 | else if (Op1I->getOperand(1) == Op0) // X-(Y+X) == -Y |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2315 | return BinaryOperator::createNeg(Op1I->getOperand(0), I.getName()); |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2316 | else if (ConstantInt *CI1 = dyn_cast<ConstantInt>(I.getOperand(0))) { |
| 2317 | if (ConstantInt *CI2 = dyn_cast<ConstantInt>(Op1I->getOperand(1))) |
| 2318 | // C1-(X+C2) --> (C1-C2)-X |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2319 | return BinaryOperator::createSub(Subtract(CI1, CI2), |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2320 | Op1I->getOperand(0)); |
| 2321 | } |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2322 | } |
| 2323 | |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 2324 | if (Op1I->hasOneUse()) { |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2325 | // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression |
| 2326 | // is not used by anyone else... |
| 2327 | // |
Chris Lattner | 0517e72 | 2004-02-02 20:09:56 +0000 | [diff] [blame] | 2328 | if (Op1I->getOpcode() == Instruction::Sub && |
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 2329 | !Op1I->getType()->isFPOrFPVector()) { |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2330 | // Swap the two operands of the subexpr... |
| 2331 | Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1); |
| 2332 | Op1I->setOperand(0, IIOp1); |
| 2333 | Op1I->setOperand(1, IIOp0); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2334 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2335 | // Create the new top level add instruction... |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2336 | return BinaryOperator::createAdd(Op0, Op1); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2337 | } |
| 2338 | |
| 2339 | // Replace (A - (A & B)) with (A & ~B) if this is the only use of (A&B)... |
| 2340 | // |
| 2341 | if (Op1I->getOpcode() == Instruction::And && |
| 2342 | (Op1I->getOperand(0) == Op0 || Op1I->getOperand(1) == Op0)) { |
| 2343 | Value *OtherOp = Op1I->getOperand(Op1I->getOperand(0) == Op0); |
| 2344 | |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 2345 | Value *NewNot = |
| 2346 | InsertNewInstBefore(BinaryOperator::createNot(OtherOp, "B.not"), I); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2347 | return BinaryOperator::createAnd(Op0, NewNot); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2348 | } |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2349 | |
Reid Spencer | ac5209e | 2006-10-16 23:08:08 +0000 | [diff] [blame] | 2350 | // 0 - (X sdiv C) -> (X sdiv -C) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2351 | if (Op1I->getOpcode() == Instruction::SDiv) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2352 | if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0)) |
Zhou Sheng | 843f0767 | 2007-04-19 05:39:12 +0000 | [diff] [blame] | 2353 | if (CSI->isZero()) |
Chris Lattner | 91ccc15 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 2354 | if (Constant *DivRHS = dyn_cast<Constant>(Op1I->getOperand(1))) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2355 | return BinaryOperator::createSDiv(Op1I->getOperand(0), |
Chris Lattner | 91ccc15 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 2356 | ConstantExpr::getNeg(DivRHS)); |
| 2357 | |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2358 | // X - X*C --> X * (1-C) |
Reid Spencer | 4b828e6 | 2005-06-18 17:37:34 +0000 | [diff] [blame] | 2359 | ConstantInt *C2 = 0; |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2360 | if (dyn_castFoldableMul(Op1I, C2) == Op0) { |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2361 | Constant *CP1 = Subtract(ConstantInt::get(I.getType(), 1), C2); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2362 | return BinaryOperator::createMul(Op0, CP1); |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2363 | } |
Dan Gohman | 5d066ff | 2007-09-17 17:31:57 +0000 | [diff] [blame] | 2364 | |
| 2365 | // X - ((X / Y) * Y) --> X % Y |
| 2366 | if (Op1I->getOpcode() == Instruction::Mul) |
| 2367 | if (Instruction *I = dyn_cast<Instruction>(Op1I->getOperand(0))) |
| 2368 | if (Op0 == I->getOperand(0) && |
| 2369 | Op1I->getOperand(1) == I->getOperand(1)) { |
| 2370 | if (I->getOpcode() == Instruction::SDiv) |
| 2371 | return BinaryOperator::createSRem(Op0, Op1I->getOperand(1)); |
| 2372 | if (I->getOpcode() == Instruction::UDiv) |
| 2373 | return BinaryOperator::createURem(Op0, Op1I->getOperand(1)); |
| 2374 | } |
Chris Lattner | 4037171 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 2375 | } |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2376 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2377 | |
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 2378 | if (!Op0->getType()->isFPOrFPVector()) |
Chris Lattner | 7edc8c2 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 2379 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) |
| 2380 | if (Op0I->getOpcode() == Instruction::Add) { |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 2381 | if (Op0I->getOperand(0) == Op1) // (Y+X)-Y == X |
| 2382 | return ReplaceInstUsesWith(I, Op0I->getOperand(1)); |
| 2383 | else if (Op0I->getOperand(1) == Op1) // (X+Y)-Y == X |
| 2384 | return ReplaceInstUsesWith(I, Op0I->getOperand(0)); |
Chris Lattner | 7edc8c2 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 2385 | } else if (Op0I->getOpcode() == Instruction::Sub) { |
| 2386 | if (Op0I->getOperand(0) == Op1) // (X-Y)-X == -Y |
| 2387 | return BinaryOperator::createNeg(Op0I->getOperand(1), I.getName()); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 2388 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2389 | |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2390 | ConstantInt *C1; |
| 2391 | if (Value *X = dyn_castFoldableMul(Op0, C1)) { |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2392 | if (X == Op1) // X*C - X --> X * (C-1) |
| 2393 | return BinaryOperator::createMul(Op1, SubOne(C1)); |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2394 | |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2395 | ConstantInt *C2; // X*C1 - X*C2 -> X * (C1-C2) |
| 2396 | if (X == dyn_castFoldableMul(Op1, C2)) |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2397 | return BinaryOperator::createMul(Op1, Subtract(C1, C2)); |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2398 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2399 | return 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2400 | } |
| 2401 | |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2402 | /// isSignBitCheck - Given an exploded icmp instruction, return true if the |
| 2403 | /// comparison only checks the sign bit. If it only checks the sign bit, set |
| 2404 | /// TrueIfSigned if the result of the comparison is true when the input value is |
| 2405 | /// signed. |
| 2406 | static bool isSignBitCheck(ICmpInst::Predicate pred, ConstantInt *RHS, |
| 2407 | bool &TrueIfSigned) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2408 | switch (pred) { |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2409 | case ICmpInst::ICMP_SLT: // True if LHS s< 0 |
| 2410 | TrueIfSigned = true; |
| 2411 | return RHS->isZero(); |
Chris Lattner | cb7122b | 2007-07-16 04:15:34 +0000 | [diff] [blame] | 2412 | case ICmpInst::ICMP_SLE: // True if LHS s<= RHS and RHS == -1 |
| 2413 | TrueIfSigned = true; |
| 2414 | return RHS->isAllOnesValue(); |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2415 | case ICmpInst::ICMP_SGT: // True if LHS s> -1 |
| 2416 | TrueIfSigned = false; |
| 2417 | return RHS->isAllOnesValue(); |
Chris Lattner | cb7122b | 2007-07-16 04:15:34 +0000 | [diff] [blame] | 2418 | case ICmpInst::ICMP_UGT: |
| 2419 | // True if LHS u> RHS and RHS == high-bit-mask - 1 |
| 2420 | TrueIfSigned = true; |
| 2421 | return RHS->getValue() == |
| 2422 | APInt::getSignedMaxValue(RHS->getType()->getPrimitiveSizeInBits()); |
| 2423 | case ICmpInst::ICMP_UGE: |
| 2424 | // True if LHS u>= RHS and RHS == high-bit-mask (2^7, 2^15, 2^31, etc) |
| 2425 | TrueIfSigned = true; |
| 2426 | return RHS->getValue() == |
| 2427 | APInt::getSignBit(RHS->getType()->getPrimitiveSizeInBits()); |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2428 | default: |
| 2429 | return false; |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2430 | } |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2431 | } |
| 2432 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2433 | Instruction *InstCombiner::visitMul(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 2434 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2435 | Value *Op0 = I.getOperand(0); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2436 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2437 | if (isa<UndefValue>(I.getOperand(1))) // undef * X -> 0 |
| 2438 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2439 | |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2440 | // Simplify mul instructions with a constant RHS... |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2441 | if (Constant *Op1 = dyn_cast<Constant>(I.getOperand(1))) { |
| 2442 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 2443 | |
| 2444 | // ((X << C1)*C2) == (X * (C2 << C1)) |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2445 | if (BinaryOperator *SI = dyn_cast<BinaryOperator>(Op0)) |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 2446 | if (SI->getOpcode() == Instruction::Shl) |
| 2447 | if (Constant *ShOp = dyn_cast<Constant>(SI->getOperand(1))) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2448 | return BinaryOperator::createMul(SI->getOperand(0), |
| 2449 | ConstantExpr::getShl(CI, ShOp)); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2450 | |
Zhou Sheng | 843f0767 | 2007-04-19 05:39:12 +0000 | [diff] [blame] | 2451 | if (CI->isZero()) |
Chris Lattner | 515c97c | 2003-09-11 22:24:54 +0000 | [diff] [blame] | 2452 | return ReplaceInstUsesWith(I, Op1); // X * 0 == 0 |
| 2453 | if (CI->equalsInt(1)) // X * 1 == X |
| 2454 | return ReplaceInstUsesWith(I, Op0); |
| 2455 | if (CI->isAllOnesValue()) // X * -1 == 0 - X |
Chris Lattner | 0af1fab | 2003-06-25 17:09:20 +0000 | [diff] [blame] | 2456 | return BinaryOperator::createNeg(Op0, I.getName()); |
Chris Lattner | 6c1ce21 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 2457 | |
Zhou Sheng | 97b52c2 | 2007-03-29 01:57:21 +0000 | [diff] [blame] | 2458 | const APInt& Val = cast<ConstantInt>(CI)->getValue(); |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2459 | if (Val.isPowerOf2()) { // Replace X*(2^C) with X << C |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 2460 | return BinaryOperator::createShl(Op0, |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2461 | ConstantInt::get(Op0->getType(), Val.logBase2())); |
Chris Lattner | bcd7db5 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 2462 | } |
Robert Bocchino | 7169828 | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 2463 | } else if (ConstantFP *Op1F = dyn_cast<ConstantFP>(Op1)) { |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2464 | if (Op1F->isNullValue()) |
| 2465 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 6c1ce21 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 2466 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2467 | // "In IEEE floating point, x*1 is not equivalent to x for nans. However, |
| 2468 | // 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] | 2469 | // We need a better interface for long double here. |
| 2470 | if (Op1->getType() == Type::FloatTy || Op1->getType() == Type::DoubleTy) |
| 2471 | if (Op1F->isExactlyValue(1.0)) |
| 2472 | return ReplaceInstUsesWith(I, Op0); // Eliminate 'mul double %X, 1.0' |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2473 | } |
Chris Lattner | ab51f3f | 2006-03-04 06:04:02 +0000 | [diff] [blame] | 2474 | |
| 2475 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) |
| 2476 | if (Op0I->getOpcode() == Instruction::Add && Op0I->hasOneUse() && |
| 2477 | isa<ConstantInt>(Op0I->getOperand(1))) { |
| 2478 | // Canonicalize (X+C1)*C2 -> X*C2+C1*C2. |
| 2479 | Instruction *Add = BinaryOperator::createMul(Op0I->getOperand(0), |
| 2480 | Op1, "tmp"); |
| 2481 | InsertNewInstBefore(Add, I); |
| 2482 | Value *C1C2 = ConstantExpr::getMul(Op1, |
| 2483 | cast<Constant>(Op0I->getOperand(1))); |
| 2484 | return BinaryOperator::createAdd(Add, C1C2); |
| 2485 | |
| 2486 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2487 | |
| 2488 | // Try to fold constant mul into select arguments. |
| 2489 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2490 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2491 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2492 | |
| 2493 | if (isa<PHINode>(Op0)) |
| 2494 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2495 | return NV; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2496 | } |
| 2497 | |
Chris Lattner | a4f445b | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 2498 | if (Value *Op0v = dyn_castNegVal(Op0)) // -X * -Y = X*Y |
| 2499 | if (Value *Op1v = dyn_castNegVal(I.getOperand(1))) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2500 | return BinaryOperator::createMul(Op0v, Op1v); |
Chris Lattner | a4f445b | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 2501 | |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2502 | // If one of the operands of the multiply is a cast from a boolean value, then |
| 2503 | // we know the bool is either zero or one, so this is a 'masking' multiply. |
| 2504 | // See if we can simplify things based on how the boolean was originally |
| 2505 | // formed. |
| 2506 | CastInst *BoolCast = 0; |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 2507 | if (ZExtInst *CI = dyn_cast<ZExtInst>(I.getOperand(0))) |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 2508 | if (CI->getOperand(0)->getType() == Type::Int1Ty) |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2509 | BoolCast = CI; |
| 2510 | if (!BoolCast) |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 2511 | if (ZExtInst *CI = dyn_cast<ZExtInst>(I.getOperand(1))) |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 2512 | if (CI->getOperand(0)->getType() == Type::Int1Ty) |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2513 | BoolCast = CI; |
| 2514 | if (BoolCast) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2515 | if (ICmpInst *SCI = dyn_cast<ICmpInst>(BoolCast->getOperand(0))) { |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2516 | Value *SCIOp0 = SCI->getOperand(0), *SCIOp1 = SCI->getOperand(1); |
| 2517 | const Type *SCOpTy = SCIOp0->getType(); |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2518 | bool TIS = false; |
| 2519 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2520 | // 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] | 2521 | // multiply into a shift/and combination. |
| 2522 | if (isa<ConstantInt>(SCIOp1) && |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2523 | isSignBitCheck(SCI->getPredicate(), cast<ConstantInt>(SCIOp1), TIS) && |
| 2524 | TIS) { |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2525 | // Shift the X value right to turn it into "all signbits". |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2526 | Constant *Amt = ConstantInt::get(SCIOp0->getType(), |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 2527 | SCOpTy->getPrimitiveSizeInBits()-1); |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2528 | Value *V = |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2529 | InsertNewInstBefore( |
| 2530 | BinaryOperator::create(Instruction::AShr, SCIOp0, Amt, |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2531 | BoolCast->getOperand(0)->getName()+ |
| 2532 | ".mask"), I); |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2533 | |
| 2534 | // If the multiply type is not the same as the source type, sign extend |
| 2535 | // or truncate to the multiply type. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 2536 | if (I.getType() != V->getType()) { |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 2537 | uint32_t SrcBits = V->getType()->getPrimitiveSizeInBits(); |
| 2538 | uint32_t DstBits = I.getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 2539 | Instruction::CastOps opcode = |
| 2540 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2541 | (SrcBits < DstBits ? Instruction::SExt : Instruction::Trunc)); |
| 2542 | V = InsertCastBefore(opcode, V, I.getType(), I); |
| 2543 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2544 | |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2545 | Value *OtherOp = Op0 == BoolCast ? I.getOperand(1) : Op0; |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2546 | return BinaryOperator::createAnd(V, OtherOp); |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2547 | } |
| 2548 | } |
| 2549 | } |
| 2550 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2551 | return Changed ? &I : 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2552 | } |
| 2553 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2554 | /// This function implements the transforms on div instructions that work |
| 2555 | /// regardless of the kind of div instruction it is (udiv, sdiv, or fdiv). It is |
| 2556 | /// used by the visitors to those instructions. |
| 2557 | /// @brief Transforms common to all three div instructions |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2558 | Instruction *InstCombiner::commonDivTransforms(BinaryOperator &I) { |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2559 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2560 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2561 | // undef / X -> 0 |
| 2562 | if (isa<UndefValue>(Op0)) |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2563 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2564 | |
| 2565 | // X / undef -> undef |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2566 | if (isa<UndefValue>(Op1)) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2567 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2568 | |
Chris Lattner | 25feae5 | 2008-01-28 00:58:18 +0000 | [diff] [blame] | 2569 | // Handle cases involving: [su]div X, (select Cond, Y, Z) |
| 2570 | // This does not apply for fdiv. |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2571 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { |
Chris Lattner | 25feae5 | 2008-01-28 00:58:18 +0000 | [diff] [blame] | 2572 | // [su]div X, (Cond ? 0 : Y) -> div X, Y. If the div and the select are in |
| 2573 | // the same basic block, then we replace the select with Y, and the |
| 2574 | // condition of the select with false (if the cond value is in the same BB). |
| 2575 | // If the select has uses other than the div, this allows them to be |
| 2576 | // simplified also. Note that div X, Y is just as good as div X, 0 (undef) |
| 2577 | if (ConstantInt *ST = dyn_cast<ConstantInt>(SI->getOperand(1))) |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2578 | if (ST->isNullValue()) { |
| 2579 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); |
| 2580 | if (CondI && CondI->getParent() == I.getParent()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2581 | UpdateValueUsesWith(CondI, ConstantInt::getFalse()); |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2582 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
| 2583 | I.setOperand(1, SI->getOperand(2)); |
| 2584 | else |
| 2585 | UpdateValueUsesWith(SI, SI->getOperand(2)); |
| 2586 | return &I; |
| 2587 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2588 | |
Chris Lattner | 25feae5 | 2008-01-28 00:58:18 +0000 | [diff] [blame] | 2589 | // Likewise for: [su]div X, (Cond ? Y : 0) -> div X, Y |
| 2590 | if (ConstantInt *ST = dyn_cast<ConstantInt>(SI->getOperand(2))) |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2591 | if (ST->isNullValue()) { |
| 2592 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); |
| 2593 | if (CondI && CondI->getParent() == I.getParent()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2594 | UpdateValueUsesWith(CondI, ConstantInt::getTrue()); |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2595 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
| 2596 | I.setOperand(1, SI->getOperand(1)); |
| 2597 | else |
| 2598 | UpdateValueUsesWith(SI, SI->getOperand(1)); |
| 2599 | return &I; |
| 2600 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2601 | } |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2602 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2603 | return 0; |
| 2604 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2605 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2606 | /// This function implements the transforms common to both integer division |
| 2607 | /// instructions (udiv and sdiv). It is called by the visitors to those integer |
| 2608 | /// division instructions. |
| 2609 | /// @brief Common integer divide transforms |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2610 | Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2611 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2612 | |
| 2613 | if (Instruction *Common = commonDivTransforms(I)) |
| 2614 | return Common; |
| 2615 | |
| 2616 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
| 2617 | // div X, 1 == X |
| 2618 | if (RHS->equalsInt(1)) |
| 2619 | return ReplaceInstUsesWith(I, Op0); |
| 2620 | |
| 2621 | // (X / C1) / C2 -> X / (C1*C2) |
| 2622 | if (Instruction *LHS = dyn_cast<Instruction>(Op0)) |
| 2623 | if (Instruction::BinaryOps(LHS->getOpcode()) == I.getOpcode()) |
| 2624 | if (ConstantInt *LHSRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) { |
| 2625 | return BinaryOperator::create(I.getOpcode(), LHS->getOperand(0), |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2626 | Multiply(RHS, LHSRHS)); |
Chris Lattner | bf70b83 | 2005-04-08 04:03:26 +0000 | [diff] [blame] | 2627 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2628 | |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2629 | if (!RHS->isZero()) { // avoid X udiv 0 |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2630 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 2631 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 2632 | return R; |
| 2633 | if (isa<PHINode>(Op0)) |
| 2634 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2635 | return NV; |
| 2636 | } |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2637 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2638 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2639 | // 0 / X == 0, we don't need to preserve faults! |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2640 | if (ConstantInt *LHS = dyn_cast<ConstantInt>(Op0)) |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2641 | if (LHS->equalsInt(0)) |
| 2642 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2643 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2644 | return 0; |
| 2645 | } |
| 2646 | |
| 2647 | Instruction *InstCombiner::visitUDiv(BinaryOperator &I) { |
| 2648 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2649 | |
| 2650 | // Handle the integer div common cases |
| 2651 | if (Instruction *Common = commonIDivTransforms(I)) |
| 2652 | return Common; |
| 2653 | |
| 2654 | // X udiv C^2 -> X >> C |
| 2655 | // Check to see if this is an unsigned division with an exact power of 2, |
| 2656 | // if so, convert to a right shift. |
| 2657 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op1)) { |
Reid Spencer | 6eb0d99 | 2007-03-26 23:58:26 +0000 | [diff] [blame] | 2658 | if (C->getValue().isPowerOf2()) // 0 not included in isPowerOf2 |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2659 | return BinaryOperator::createLShr(Op0, |
Zhou Sheng | 0fc5095 | 2007-03-25 05:01:29 +0000 | [diff] [blame] | 2660 | ConstantInt::get(Op0->getType(), C->getValue().logBase2())); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2661 | } |
| 2662 | |
| 2663 | // X udiv (C1 << N), where C1 is "1<<C2" --> X >> (N+C2) |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2664 | if (BinaryOperator *RHSI = dyn_cast<BinaryOperator>(I.getOperand(1))) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2665 | if (RHSI->getOpcode() == Instruction::Shl && |
| 2666 | isa<ConstantInt>(RHSI->getOperand(0))) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2667 | const APInt& C1 = cast<ConstantInt>(RHSI->getOperand(0))->getValue(); |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2668 | if (C1.isPowerOf2()) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2669 | Value *N = RHSI->getOperand(1); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2670 | const Type *NTy = N->getType(); |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 2671 | if (uint32_t C2 = C1.logBase2()) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2672 | Constant *C2V = ConstantInt::get(NTy, C2); |
| 2673 | N = InsertNewInstBefore(BinaryOperator::createAdd(N, C2V, "tmp"), I); |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2674 | } |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 2675 | return BinaryOperator::createLShr(Op0, N); |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2676 | } |
| 2677 | } |
Chris Lattner | c812e5d | 2005-11-05 07:40:31 +0000 | [diff] [blame] | 2678 | } |
| 2679 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2680 | // udiv X, (Select Cond, C1, C2) --> Select Cond, (shr X, C1), (shr X, C2) |
| 2681 | // where C1&C2 are powers of two. |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2682 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2683 | if (ConstantInt *STO = dyn_cast<ConstantInt>(SI->getOperand(1))) |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2684 | if (ConstantInt *SFO = dyn_cast<ConstantInt>(SI->getOperand(2))) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2685 | const APInt &TVA = STO->getValue(), &FVA = SFO->getValue(); |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2686 | if (TVA.isPowerOf2() && FVA.isPowerOf2()) { |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2687 | // Compute the shift amounts |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2688 | uint32_t TSA = TVA.logBase2(), FSA = FVA.logBase2(); |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2689 | // Construct the "on true" case of the select |
| 2690 | Constant *TC = ConstantInt::get(Op0->getType(), TSA); |
| 2691 | Instruction *TSI = BinaryOperator::createLShr( |
| 2692 | Op0, TC, SI->getName()+".t"); |
| 2693 | TSI = InsertNewInstBefore(TSI, I); |
| 2694 | |
| 2695 | // Construct the "on false" case of the select |
| 2696 | Constant *FC = ConstantInt::get(Op0->getType(), FSA); |
| 2697 | Instruction *FSI = BinaryOperator::createLShr( |
| 2698 | Op0, FC, SI->getName()+".f"); |
| 2699 | FSI = InsertNewInstBefore(FSI, I); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2700 | |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2701 | // construct the select instruction and return it. |
| 2702 | return new SelectInst(SI->getOperand(0), TSI, FSI, SI->getName()); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2703 | } |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2704 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2705 | return 0; |
| 2706 | } |
| 2707 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2708 | Instruction *InstCombiner::visitSDiv(BinaryOperator &I) { |
| 2709 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2710 | |
| 2711 | // Handle the integer div common cases |
| 2712 | if (Instruction *Common = commonIDivTransforms(I)) |
| 2713 | return Common; |
| 2714 | |
| 2715 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
| 2716 | // sdiv X, -1 == -X |
| 2717 | if (RHS->isAllOnesValue()) |
| 2718 | return BinaryOperator::createNeg(Op0); |
| 2719 | |
| 2720 | // -X/C -> X/-C |
| 2721 | if (Value *LHSNeg = dyn_castNegVal(Op0)) |
| 2722 | return BinaryOperator::createSDiv(LHSNeg, ConstantExpr::getNeg(RHS)); |
| 2723 | } |
| 2724 | |
| 2725 | // If the sign bits of both operands are zero (i.e. we can prove they are |
| 2726 | // unsigned inputs), turn this into a udiv. |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 2727 | if (I.getType()->isInteger()) { |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2728 | APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits())); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2729 | if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) { |
Dan Gohman | cff5509 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 2730 | // 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] | 2731 | return BinaryOperator::createUDiv(Op0, Op1, I.getName()); |
| 2732 | } |
| 2733 | } |
| 2734 | |
| 2735 | return 0; |
| 2736 | } |
| 2737 | |
| 2738 | Instruction *InstCombiner::visitFDiv(BinaryOperator &I) { |
| 2739 | return commonDivTransforms(I); |
| 2740 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2741 | |
Chris Lattner | db3f873 | 2006-03-02 06:50:58 +0000 | [diff] [blame] | 2742 | /// GetFactor - If we can prove that the specified value is at least a multiple |
| 2743 | /// of some factor, return that factor. |
| 2744 | static Constant *GetFactor(Value *V) { |
| 2745 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) |
| 2746 | return CI; |
| 2747 | |
| 2748 | // Unless we can be tricky, we know this is a multiple of 1. |
| 2749 | Constant *Result = ConstantInt::get(V->getType(), 1); |
| 2750 | |
| 2751 | Instruction *I = dyn_cast<Instruction>(V); |
| 2752 | if (!I) return Result; |
| 2753 | |
| 2754 | if (I->getOpcode() == Instruction::Mul) { |
| 2755 | // Handle multiplies by a constant, etc. |
| 2756 | return ConstantExpr::getMul(GetFactor(I->getOperand(0)), |
| 2757 | GetFactor(I->getOperand(1))); |
| 2758 | } else if (I->getOpcode() == Instruction::Shl) { |
| 2759 | // (X<<C) -> X * (1 << C) |
| 2760 | if (Constant *ShRHS = dyn_cast<Constant>(I->getOperand(1))) { |
| 2761 | ShRHS = ConstantExpr::getShl(Result, ShRHS); |
| 2762 | return ConstantExpr::getMul(GetFactor(I->getOperand(0)), ShRHS); |
| 2763 | } |
| 2764 | } else if (I->getOpcode() == Instruction::And) { |
| 2765 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 2766 | // X & 0xFFF0 is known to be a multiple of 16. |
Reid Spencer | f244252 | 2007-03-24 00:42:08 +0000 | [diff] [blame] | 2767 | uint32_t Zeros = RHS->getValue().countTrailingZeros(); |
Chris Lattner | 148083a | 2007-11-23 22:35:18 +0000 | [diff] [blame] | 2768 | if (Zeros != V->getType()->getPrimitiveSizeInBits())// don't shift by "32" |
Chris Lattner | db3f873 | 2006-03-02 06:50:58 +0000 | [diff] [blame] | 2769 | return ConstantExpr::getShl(Result, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2770 | ConstantInt::get(Result->getType(), Zeros)); |
Chris Lattner | db3f873 | 2006-03-02 06:50:58 +0000 | [diff] [blame] | 2771 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2772 | } else if (CastInst *CI = dyn_cast<CastInst>(I)) { |
Chris Lattner | db3f873 | 2006-03-02 06:50:58 +0000 | [diff] [blame] | 2773 | // Only handle int->int casts. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2774 | if (!CI->isIntegerCast()) |
| 2775 | return Result; |
| 2776 | Value *Op = CI->getOperand(0); |
| 2777 | return ConstantExpr::getCast(CI->getOpcode(), GetFactor(Op), V->getType()); |
Chris Lattner | db3f873 | 2006-03-02 06:50:58 +0000 | [diff] [blame] | 2778 | } |
| 2779 | return Result; |
| 2780 | } |
| 2781 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2782 | /// This function implements the transforms on rem instructions that work |
| 2783 | /// regardless of the kind of rem instruction it is (urem, srem, or frem). It |
| 2784 | /// is used by the visitors to those instructions. |
| 2785 | /// @brief Transforms common to all three rem instructions |
| 2786 | Instruction *InstCombiner::commonRemTransforms(BinaryOperator &I) { |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2787 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2788 | |
Chris Lattner | 19ccd5c | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 2789 | // 0 % X == 0, we don't need to preserve faults! |
| 2790 | if (Constant *LHS = dyn_cast<Constant>(Op0)) |
| 2791 | if (LHS->isNullValue()) |
| 2792 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2793 | |
| 2794 | if (isa<UndefValue>(Op0)) // undef % X -> 0 |
| 2795 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2796 | if (isa<UndefValue>(Op1)) |
| 2797 | return ReplaceInstUsesWith(I, Op1); // X % undef -> undef |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2798 | |
| 2799 | // Handle cases involving: rem X, (select Cond, Y, Z) |
| 2800 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { |
| 2801 | // rem X, (Cond ? 0 : Y) -> rem X, Y. If the rem and the select are in |
| 2802 | // the same basic block, then we replace the select with Y, and the |
| 2803 | // condition of the select with false (if the cond value is in the same |
| 2804 | // BB). If the select has uses other than the div, this allows them to be |
| 2805 | // simplified also. |
| 2806 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(1))) |
| 2807 | if (ST->isNullValue()) { |
| 2808 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); |
| 2809 | if (CondI && CondI->getParent() == I.getParent()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2810 | UpdateValueUsesWith(CondI, ConstantInt::getFalse()); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2811 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
| 2812 | I.setOperand(1, SI->getOperand(2)); |
| 2813 | else |
| 2814 | UpdateValueUsesWith(SI, SI->getOperand(2)); |
Chris Lattner | 5b73c08 | 2004-07-06 07:01:22 +0000 | [diff] [blame] | 2815 | return &I; |
| 2816 | } |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2817 | // Likewise for: rem X, (Cond ? Y : 0) -> rem X, Y |
| 2818 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(2))) |
| 2819 | if (ST->isNullValue()) { |
| 2820 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); |
| 2821 | if (CondI && CondI->getParent() == I.getParent()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2822 | UpdateValueUsesWith(CondI, ConstantInt::getTrue()); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2823 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
| 2824 | I.setOperand(1, SI->getOperand(1)); |
| 2825 | else |
| 2826 | UpdateValueUsesWith(SI, SI->getOperand(1)); |
| 2827 | return &I; |
| 2828 | } |
Chris Lattner | 11a49f2 | 2005-11-05 07:28:37 +0000 | [diff] [blame] | 2829 | } |
Chris Lattner | 5b73c08 | 2004-07-06 07:01:22 +0000 | [diff] [blame] | 2830 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2831 | return 0; |
| 2832 | } |
| 2833 | |
| 2834 | /// This function implements the transforms common to both integer remainder |
| 2835 | /// instructions (urem and srem). It is called by the visitors to those integer |
| 2836 | /// remainder instructions. |
| 2837 | /// @brief Common integer remainder transforms |
| 2838 | Instruction *InstCombiner::commonIRemTransforms(BinaryOperator &I) { |
| 2839 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2840 | |
| 2841 | if (Instruction *common = commonRemTransforms(I)) |
| 2842 | return common; |
| 2843 | |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2844 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 19ccd5c | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 2845 | // X % 0 == undef, we don't need to preserve faults! |
| 2846 | if (RHS->equalsInt(0)) |
| 2847 | return ReplaceInstUsesWith(I, UndefValue::get(I.getType())); |
| 2848 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2849 | if (RHS->equalsInt(1)) // X % 1 == 0 |
| 2850 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2851 | |
Chris Lattner | 9794392 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 2852 | if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) { |
| 2853 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0I)) { |
| 2854 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 2855 | return R; |
| 2856 | } else if (isa<PHINode>(Op0I)) { |
| 2857 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2858 | return NV; |
Chris Lattner | 9794392 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 2859 | } |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2860 | // (X * C1) % C2 --> 0 iff C1 % C2 == 0 |
| 2861 | if (ConstantExpr::getSRem(GetFactor(Op0I), RHS)->isNullValue()) |
Chris Lattner | db3f873 | 2006-03-02 06:50:58 +0000 | [diff] [blame] | 2862 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | 9794392 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 2863 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2864 | } |
| 2865 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2866 | return 0; |
| 2867 | } |
| 2868 | |
| 2869 | Instruction *InstCombiner::visitURem(BinaryOperator &I) { |
| 2870 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2871 | |
| 2872 | if (Instruction *common = commonIRemTransforms(I)) |
| 2873 | return common; |
| 2874 | |
| 2875 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
| 2876 | // X urem C^2 -> X and C |
| 2877 | // Check to see if this is an unsigned remainder with an exact power of 2, |
| 2878 | // if so, convert to a bitwise and. |
| 2879 | if (ConstantInt *C = dyn_cast<ConstantInt>(RHS)) |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2880 | if (C->getValue().isPowerOf2()) |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2881 | return BinaryOperator::createAnd(Op0, SubOne(C)); |
| 2882 | } |
| 2883 | |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2884 | if (Instruction *RHSI = dyn_cast<Instruction>(I.getOperand(1))) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2885 | // Turn A % (C << N), where C is 2^k, into A & ((C << N)-1) |
| 2886 | if (RHSI->getOpcode() == Instruction::Shl && |
| 2887 | isa<ConstantInt>(RHSI->getOperand(0))) { |
Zhou Sheng | 0fc5095 | 2007-03-25 05:01:29 +0000 | [diff] [blame] | 2888 | if (cast<ConstantInt>(RHSI->getOperand(0))->getValue().isPowerOf2()) { |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2889 | Constant *N1 = ConstantInt::getAllOnesValue(I.getType()); |
| 2890 | Value *Add = InsertNewInstBefore(BinaryOperator::createAdd(RHSI, N1, |
| 2891 | "tmp"), I); |
| 2892 | return BinaryOperator::createAnd(Op0, Add); |
| 2893 | } |
| 2894 | } |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2895 | } |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2896 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2897 | // urem X, (select Cond, 2^C1, 2^C2) --> select Cond, (and X, C1), (and X, C2) |
| 2898 | // where C1&C2 are powers of two. |
| 2899 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { |
| 2900 | if (ConstantInt *STO = dyn_cast<ConstantInt>(SI->getOperand(1))) |
| 2901 | if (ConstantInt *SFO = dyn_cast<ConstantInt>(SI->getOperand(2))) { |
| 2902 | // STO == 0 and SFO == 0 handled above. |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2903 | if ((STO->getValue().isPowerOf2()) && |
| 2904 | (SFO->getValue().isPowerOf2())) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2905 | Value *TrueAnd = InsertNewInstBefore( |
| 2906 | BinaryOperator::createAnd(Op0, SubOne(STO), SI->getName()+".t"), I); |
| 2907 | Value *FalseAnd = InsertNewInstBefore( |
| 2908 | BinaryOperator::createAnd(Op0, SubOne(SFO), SI->getName()+".f"), I); |
| 2909 | return new SelectInst(SI->getOperand(0), TrueAnd, FalseAnd); |
| 2910 | } |
| 2911 | } |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2912 | } |
| 2913 | |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2914 | return 0; |
| 2915 | } |
| 2916 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2917 | Instruction *InstCombiner::visitSRem(BinaryOperator &I) { |
| 2918 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2919 | |
Dan Gohman | cff5509 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 2920 | // Handle the integer rem common cases |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2921 | if (Instruction *common = commonIRemTransforms(I)) |
| 2922 | return common; |
| 2923 | |
| 2924 | if (Value *RHSNeg = dyn_castNegVal(Op1)) |
| 2925 | if (!isa<ConstantInt>(RHSNeg) || |
Zhou Sheng | 0fc5095 | 2007-03-25 05:01:29 +0000 | [diff] [blame] | 2926 | cast<ConstantInt>(RHSNeg)->getValue().isStrictlyPositive()) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2927 | // X % -Y -> X % Y |
| 2928 | AddUsesToWorkList(I); |
| 2929 | I.setOperand(1, RHSNeg); |
| 2930 | return &I; |
| 2931 | } |
| 2932 | |
Dan Gohman | cff5509 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 2933 | // 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] | 2934 | // unsigned inputs), turn this into a urem. |
Dan Gohman | cff5509 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 2935 | if (I.getType()->isInteger()) { |
| 2936 | APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits())); |
| 2937 | if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) { |
| 2938 | // X srem Y -> X urem Y, iff X and Y don't have sign bit set |
| 2939 | return BinaryOperator::createURem(Op0, Op1, I.getName()); |
| 2940 | } |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2941 | } |
| 2942 | |
| 2943 | return 0; |
| 2944 | } |
| 2945 | |
| 2946 | Instruction *InstCombiner::visitFRem(BinaryOperator &I) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2947 | return commonRemTransforms(I); |
| 2948 | } |
| 2949 | |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2950 | // isMaxValueMinusOne - return true if this is Max-1 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2951 | static bool isMaxValueMinusOne(const ConstantInt *C, bool isSigned) { |
Reid Spencer | 3a2a9fb | 2007-03-19 21:10:28 +0000 | [diff] [blame] | 2952 | uint32_t TypeBits = C->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2953 | if (!isSigned) |
| 2954 | return C->getValue() == APInt::getAllOnesValue(TypeBits) - 1; |
| 2955 | return C->getValue() == APInt::getSignedMaxValue(TypeBits)-1; |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2956 | } |
| 2957 | |
| 2958 | // isMinValuePlusOne - return true if this is Min+1 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2959 | static bool isMinValuePlusOne(const ConstantInt *C, bool isSigned) { |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2960 | if (!isSigned) |
| 2961 | return C->getValue() == 1; // unsigned |
| 2962 | |
| 2963 | // Calculate 1111111111000000000000 |
| 2964 | uint32_t TypeBits = C->getType()->getPrimitiveSizeInBits(); |
| 2965 | return C->getValue() == APInt::getSignedMinValue(TypeBits)+1; |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2966 | } |
| 2967 | |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2968 | // isOneBitSet - Return true if there is exactly one bit set in the specified |
| 2969 | // constant. |
| 2970 | static bool isOneBitSet(const ConstantInt *CI) { |
Reid Spencer | 5f6a895 | 2007-03-20 00:16:52 +0000 | [diff] [blame] | 2971 | return CI->getValue().isPowerOf2(); |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2972 | } |
| 2973 | |
Chris Lattner | b20ba0a | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2974 | // isHighOnes - Return true if the constant is of the form 1+0+. |
| 2975 | // This is the same as lowones(~X). |
| 2976 | static bool isHighOnes(const ConstantInt *CI) { |
Zhou Sheng | 2cde46c | 2007-03-20 12:49:06 +0000 | [diff] [blame] | 2977 | return (~CI->getValue() + 1).isPowerOf2(); |
Chris Lattner | b20ba0a | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2978 | } |
| 2979 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2980 | /// getICmpCode - Encode a icmp predicate into a three bit mask. These bits |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2981 | /// are carefully arranged to allow folding of expressions such as: |
| 2982 | /// |
| 2983 | /// (A < B) | (A > B) --> (A != B) |
| 2984 | /// |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2985 | /// Note that this is only valid if the first and second predicates have the |
| 2986 | /// same sign. Is illegal to do: (A u< B) | (A s> B) |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2987 | /// |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2988 | /// Three bits are used to represent the condition, as follows: |
| 2989 | /// 0 A > B |
| 2990 | /// 1 A == B |
| 2991 | /// 2 A < B |
| 2992 | /// |
| 2993 | /// <=> Value Definition |
| 2994 | /// 000 0 Always false |
| 2995 | /// 001 1 A > B |
| 2996 | /// 010 2 A == B |
| 2997 | /// 011 3 A >= B |
| 2998 | /// 100 4 A < B |
| 2999 | /// 101 5 A != B |
| 3000 | /// 110 6 A <= B |
| 3001 | /// 111 7 Always true |
| 3002 | /// |
| 3003 | static unsigned getICmpCode(const ICmpInst *ICI) { |
| 3004 | switch (ICI->getPredicate()) { |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3005 | // False -> 0 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3006 | case ICmpInst::ICMP_UGT: return 1; // 001 |
| 3007 | case ICmpInst::ICMP_SGT: return 1; // 001 |
| 3008 | case ICmpInst::ICMP_EQ: return 2; // 010 |
| 3009 | case ICmpInst::ICMP_UGE: return 3; // 011 |
| 3010 | case ICmpInst::ICMP_SGE: return 3; // 011 |
| 3011 | case ICmpInst::ICMP_ULT: return 4; // 100 |
| 3012 | case ICmpInst::ICMP_SLT: return 4; // 100 |
| 3013 | case ICmpInst::ICMP_NE: return 5; // 101 |
| 3014 | case ICmpInst::ICMP_ULE: return 6; // 110 |
| 3015 | case ICmpInst::ICMP_SLE: return 6; // 110 |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3016 | // True -> 7 |
| 3017 | default: |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3018 | assert(0 && "Invalid ICmp predicate!"); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3019 | return 0; |
| 3020 | } |
| 3021 | } |
| 3022 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3023 | /// getICmpValue - This is the complement of getICmpCode, which turns an |
| 3024 | /// 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] | 3025 | /// new ICmp instruction. The sign is passed in to determine which kind |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3026 | /// of predicate to use in new icmp instructions. |
| 3027 | static Value *getICmpValue(bool sign, unsigned code, Value *LHS, Value *RHS) { |
| 3028 | switch (code) { |
| 3029 | default: assert(0 && "Illegal ICmp code!"); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3030 | case 0: return ConstantInt::getFalse(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3031 | case 1: |
| 3032 | if (sign) |
| 3033 | return new ICmpInst(ICmpInst::ICMP_SGT, LHS, RHS); |
| 3034 | else |
| 3035 | return new ICmpInst(ICmpInst::ICMP_UGT, LHS, RHS); |
| 3036 | case 2: return new ICmpInst(ICmpInst::ICMP_EQ, LHS, RHS); |
| 3037 | case 3: |
| 3038 | if (sign) |
| 3039 | return new ICmpInst(ICmpInst::ICMP_SGE, LHS, RHS); |
| 3040 | else |
| 3041 | return new ICmpInst(ICmpInst::ICMP_UGE, LHS, RHS); |
| 3042 | case 4: |
| 3043 | if (sign) |
| 3044 | return new ICmpInst(ICmpInst::ICMP_SLT, LHS, RHS); |
| 3045 | else |
| 3046 | return new ICmpInst(ICmpInst::ICMP_ULT, LHS, RHS); |
| 3047 | case 5: return new ICmpInst(ICmpInst::ICMP_NE, LHS, RHS); |
| 3048 | case 6: |
| 3049 | if (sign) |
| 3050 | return new ICmpInst(ICmpInst::ICMP_SLE, LHS, RHS); |
| 3051 | else |
| 3052 | return new ICmpInst(ICmpInst::ICMP_ULE, LHS, RHS); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3053 | case 7: return ConstantInt::getTrue(); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3054 | } |
| 3055 | } |
| 3056 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3057 | static bool PredicatesFoldable(ICmpInst::Predicate p1, ICmpInst::Predicate p2) { |
| 3058 | return (ICmpInst::isSignedPredicate(p1) == ICmpInst::isSignedPredicate(p2)) || |
| 3059 | (ICmpInst::isSignedPredicate(p1) && |
| 3060 | (p2 == ICmpInst::ICMP_EQ || p2 == ICmpInst::ICMP_NE)) || |
| 3061 | (ICmpInst::isSignedPredicate(p2) && |
| 3062 | (p1 == ICmpInst::ICMP_EQ || p1 == ICmpInst::ICMP_NE)); |
| 3063 | } |
| 3064 | |
| 3065 | namespace { |
| 3066 | // FoldICmpLogical - Implements (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B) |
| 3067 | struct FoldICmpLogical { |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3068 | InstCombiner &IC; |
| 3069 | Value *LHS, *RHS; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3070 | ICmpInst::Predicate pred; |
| 3071 | FoldICmpLogical(InstCombiner &ic, ICmpInst *ICI) |
| 3072 | : IC(ic), LHS(ICI->getOperand(0)), RHS(ICI->getOperand(1)), |
| 3073 | pred(ICI->getPredicate()) {} |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3074 | bool shouldApply(Value *V) const { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3075 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(V)) |
| 3076 | if (PredicatesFoldable(pred, ICI->getPredicate())) |
| 3077 | return (ICI->getOperand(0) == LHS && ICI->getOperand(1) == RHS || |
| 3078 | ICI->getOperand(0) == RHS && ICI->getOperand(1) == LHS); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3079 | return false; |
| 3080 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3081 | Instruction *apply(Instruction &Log) const { |
| 3082 | ICmpInst *ICI = cast<ICmpInst>(Log.getOperand(0)); |
| 3083 | if (ICI->getOperand(0) != LHS) { |
| 3084 | assert(ICI->getOperand(1) == LHS); |
| 3085 | ICI->swapOperands(); // Swap the LHS and RHS of the ICmp |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3086 | } |
| 3087 | |
Chris Lattner | bc1dbfc | 2007-03-13 14:27:42 +0000 | [diff] [blame] | 3088 | ICmpInst *RHSICI = cast<ICmpInst>(Log.getOperand(1)); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3089 | unsigned LHSCode = getICmpCode(ICI); |
Chris Lattner | bc1dbfc | 2007-03-13 14:27:42 +0000 | [diff] [blame] | 3090 | unsigned RHSCode = getICmpCode(RHSICI); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3091 | unsigned Code; |
| 3092 | switch (Log.getOpcode()) { |
| 3093 | case Instruction::And: Code = LHSCode & RHSCode; break; |
| 3094 | case Instruction::Or: Code = LHSCode | RHSCode; break; |
| 3095 | case Instruction::Xor: Code = LHSCode ^ RHSCode; break; |
Chris Lattner | 021c190 | 2003-09-22 20:33:34 +0000 | [diff] [blame] | 3096 | default: assert(0 && "Illegal logical opcode!"); return 0; |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3097 | } |
| 3098 | |
Chris Lattner | bc1dbfc | 2007-03-13 14:27:42 +0000 | [diff] [blame] | 3099 | bool isSigned = ICmpInst::isSignedPredicate(RHSICI->getPredicate()) || |
| 3100 | ICmpInst::isSignedPredicate(ICI->getPredicate()); |
| 3101 | |
| 3102 | Value *RV = getICmpValue(isSigned, Code, LHS, RHS); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3103 | if (Instruction *I = dyn_cast<Instruction>(RV)) |
| 3104 | return I; |
| 3105 | // Otherwise, it's a constant boolean value... |
| 3106 | return IC.ReplaceInstUsesWith(Log, RV); |
| 3107 | } |
| 3108 | }; |
Chris Lattner | d23b5ba | 2006-11-15 04:53:24 +0000 | [diff] [blame] | 3109 | } // end anonymous namespace |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3110 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3111 | // OptAndOp - This handles expressions of the form ((val OP C1) & C2). Where |
| 3112 | // 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] | 3113 | // guaranteed to be a binary operator. |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3114 | Instruction *InstCombiner::OptAndOp(Instruction *Op, |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3115 | ConstantInt *OpRHS, |
| 3116 | ConstantInt *AndRHS, |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3117 | BinaryOperator &TheAnd) { |
| 3118 | Value *X = Op->getOperand(0); |
Chris Lattner | 76f7fe2 | 2004-01-12 19:47:05 +0000 | [diff] [blame] | 3119 | Constant *Together = 0; |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3120 | if (!Op->isShift()) |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 3121 | Together = And(AndRHS, OpRHS); |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3122 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3123 | switch (Op->getOpcode()) { |
| 3124 | case Instruction::Xor: |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3125 | if (Op->hasOneUse()) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3126 | // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2) |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3127 | Instruction *And = BinaryOperator::createAnd(X, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3128 | InsertNewInstBefore(And, TheAnd); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3129 | And->takeName(Op); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3130 | return BinaryOperator::createXor(And, Together); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3131 | } |
| 3132 | break; |
| 3133 | case Instruction::Or: |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3134 | if (Together == AndRHS) // (X | C) & C --> C |
| 3135 | return ReplaceInstUsesWith(TheAnd, AndRHS); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3136 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3137 | if (Op->hasOneUse() && Together != OpRHS) { |
| 3138 | // (X | C1) & C2 --> (X | (C1&C2)) & C2 |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3139 | Instruction *Or = BinaryOperator::createOr(X, Together); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3140 | InsertNewInstBefore(Or, TheAnd); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3141 | Or->takeName(Op); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3142 | return BinaryOperator::createAnd(Or, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3143 | } |
| 3144 | break; |
| 3145 | case Instruction::Add: |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 3146 | if (Op->hasOneUse()) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3147 | // Adding a one to a single bit bit-field should be turned into an XOR |
| 3148 | // of the bit. First thing to check is to see if this AND is with a |
| 3149 | // single bit constant. |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3150 | const APInt& AndRHSV = cast<ConstantInt>(AndRHS)->getValue(); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3151 | |
| 3152 | // If there is only one bit set... |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 3153 | if (isOneBitSet(cast<ConstantInt>(AndRHS))) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3154 | // Ok, at this point, we know that we are masking the result of the |
| 3155 | // ADD down to exactly one bit. If the constant we are adding has |
| 3156 | // no bits set below this bit, then we can eliminate the ADD. |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3157 | const APInt& AddRHS = cast<ConstantInt>(OpRHS)->getValue(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3158 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3159 | // Check to see if any bits below the one bit set in AndRHSV are set. |
| 3160 | if ((AddRHS & (AndRHSV-1)) == 0) { |
| 3161 | // If not, the only thing that can effect the output of the AND is |
| 3162 | // the bit specified by AndRHSV. If that bit is set, the effect of |
| 3163 | // the XOR is to toggle the bit. If it is clear, then the ADD has |
| 3164 | // no effect. |
| 3165 | if ((AddRHS & AndRHSV) == 0) { // Bit is not set, noop |
| 3166 | TheAnd.setOperand(0, X); |
| 3167 | return &TheAnd; |
| 3168 | } else { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3169 | // Pull the XOR out of the AND. |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3170 | Instruction *NewAnd = BinaryOperator::createAnd(X, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3171 | InsertNewInstBefore(NewAnd, TheAnd); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3172 | NewAnd->takeName(Op); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3173 | return BinaryOperator::createXor(NewAnd, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3174 | } |
| 3175 | } |
| 3176 | } |
| 3177 | } |
| 3178 | break; |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 3179 | |
| 3180 | case Instruction::Shl: { |
| 3181 | // We know that the AND will not produce any of the bits shifted in, so if |
| 3182 | // the anded constant includes them, clear them now! |
| 3183 | // |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3184 | uint32_t BitWidth = AndRHS->getType()->getBitWidth(); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3185 | uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3186 | APInt ShlMask(APInt::getHighBitsSet(BitWidth, BitWidth-OpRHSVal)); |
| 3187 | ConstantInt *CI = ConstantInt::get(AndRHS->getValue() & ShlMask); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3188 | |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3189 | if (CI->getValue() == ShlMask) { |
| 3190 | // Masking out bits that the shift already masks |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 3191 | return ReplaceInstUsesWith(TheAnd, Op); // No need for the and. |
| 3192 | } else if (CI != AndRHS) { // Reducing bits set in and. |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 3193 | TheAnd.setOperand(1, CI); |
| 3194 | return &TheAnd; |
| 3195 | } |
| 3196 | break; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3197 | } |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 3198 | case Instruction::LShr: |
| 3199 | { |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 3200 | // We know that the AND will not produce any of the bits shifted in, so if |
| 3201 | // the anded constant includes them, clear them now! This only applies to |
| 3202 | // unsigned shifts, because a signed shr may bring in set bits! |
| 3203 | // |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3204 | uint32_t BitWidth = AndRHS->getType()->getBitWidth(); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3205 | uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3206 | APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal)); |
| 3207 | ConstantInt *CI = ConstantInt::get(AndRHS->getValue() & ShrMask); |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 3208 | |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3209 | if (CI->getValue() == ShrMask) { |
| 3210 | // Masking out bits that the shift already masks. |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 3211 | return ReplaceInstUsesWith(TheAnd, Op); |
| 3212 | } else if (CI != AndRHS) { |
| 3213 | TheAnd.setOperand(1, CI); // Reduce bits set in and cst. |
| 3214 | return &TheAnd; |
| 3215 | } |
| 3216 | break; |
| 3217 | } |
| 3218 | case Instruction::AShr: |
| 3219 | // Signed shr. |
| 3220 | // See if this is shifting in some sign extension, then masking it out |
| 3221 | // with an and. |
| 3222 | if (Op->hasOneUse()) { |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3223 | uint32_t BitWidth = AndRHS->getType()->getBitWidth(); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3224 | uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3225 | APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal)); |
| 3226 | Constant *C = ConstantInt::get(AndRHS->getValue() & ShrMask); |
Reid Spencer | 7eb7638 | 2006-12-13 17:19:09 +0000 | [diff] [blame] | 3227 | if (C == AndRHS) { // Masking out bits shifted in. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 3228 | // (Val ashr C1) & C2 -> (Val lshr C1) & C2 |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 3229 | // Make the argument unsigned. |
| 3230 | Value *ShVal = Op->getOperand(0); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3231 | ShVal = InsertNewInstBefore( |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 3232 | BinaryOperator::createLShr(ShVal, OpRHS, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3233 | Op->getName()), TheAnd); |
Reid Spencer | 7eb7638 | 2006-12-13 17:19:09 +0000 | [diff] [blame] | 3234 | return BinaryOperator::createAnd(ShVal, AndRHS, TheAnd.getName()); |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 3235 | } |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 3236 | } |
| 3237 | break; |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3238 | } |
| 3239 | return 0; |
| 3240 | } |
| 3241 | |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 3242 | |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3243 | /// InsertRangeTest - Emit a computation of: (V >= Lo && V < Hi) if Inside is |
| 3244 | /// 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] | 3245 | /// (V-Lo) <u Hi-Lo. This method expects that Lo <= Hi. isSigned indicates |
| 3246 | /// 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] | 3247 | /// insert new instructions. |
| 3248 | Instruction *InstCombiner::InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3249 | bool isSigned, bool Inside, |
| 3250 | Instruction &IB) { |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3251 | assert(cast<ConstantInt>(ConstantExpr::getICmp((isSigned ? |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 3252 | ICmpInst::ICMP_SLE:ICmpInst::ICMP_ULE), Lo, Hi))->getZExtValue() && |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3253 | "Lo is not <= Hi in range emission code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3254 | |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3255 | if (Inside) { |
| 3256 | if (Lo == Hi) // Trivially false. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3257 | return new ICmpInst(ICmpInst::ICMP_NE, V, V); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3258 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3259 | // V >= Min && V < Hi --> V < Hi |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3260 | if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) { |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 3261 | ICmpInst::Predicate pred = (isSigned ? |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3262 | ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT); |
| 3263 | return new ICmpInst(pred, V, Hi); |
| 3264 | } |
| 3265 | |
| 3266 | // Emit V-Lo <u Hi-Lo |
| 3267 | Constant *NegLo = ConstantExpr::getNeg(Lo); |
| 3268 | Instruction *Add = BinaryOperator::createAdd(V, NegLo, V->getName()+".off"); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3269 | InsertNewInstBefore(Add, IB); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3270 | Constant *UpperBound = ConstantExpr::getAdd(NegLo, Hi); |
| 3271 | return new ICmpInst(ICmpInst::ICMP_ULT, Add, UpperBound); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3272 | } |
| 3273 | |
| 3274 | if (Lo == Hi) // Trivially true. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3275 | return new ICmpInst(ICmpInst::ICMP_EQ, V, V); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3276 | |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 3277 | // V < Min || V >= Hi -> V > Hi-1 |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3278 | Hi = SubOne(cast<ConstantInt>(Hi)); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3279 | if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3280 | ICmpInst::Predicate pred = (isSigned ? |
| 3281 | ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT); |
| 3282 | return new ICmpInst(pred, V, Hi); |
| 3283 | } |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3284 | |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 3285 | // Emit V-Lo >u Hi-1-Lo |
| 3286 | // Note that Hi has already had one subtracted from it, above. |
| 3287 | ConstantInt *NegLo = cast<ConstantInt>(ConstantExpr::getNeg(Lo)); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3288 | Instruction *Add = BinaryOperator::createAdd(V, NegLo, V->getName()+".off"); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3289 | InsertNewInstBefore(Add, IB); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3290 | Constant *LowerBound = ConstantExpr::getAdd(NegLo, Hi); |
| 3291 | return new ICmpInst(ICmpInst::ICMP_UGT, Add, LowerBound); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3292 | } |
| 3293 | |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3294 | // isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with |
| 3295 | // any number of 0s on either side. The 1s are allowed to wrap from LSB to |
| 3296 | // MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is |
| 3297 | // not, since all 1s are not contiguous. |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 3298 | static bool isRunOfOnes(ConstantInt *Val, uint32_t &MB, uint32_t &ME) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3299 | const APInt& V = Val->getValue(); |
Reid Spencer | f244252 | 2007-03-24 00:42:08 +0000 | [diff] [blame] | 3300 | uint32_t BitWidth = Val->getType()->getBitWidth(); |
| 3301 | if (!APIntOps::isShiftedMask(BitWidth, V)) return false; |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3302 | |
| 3303 | // look for the first zero bit after the run of ones |
Reid Spencer | f244252 | 2007-03-24 00:42:08 +0000 | [diff] [blame] | 3304 | MB = BitWidth - ((V - 1) ^ V).countLeadingZeros(); |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3305 | // look for the first non-zero bit |
Reid Spencer | f244252 | 2007-03-24 00:42:08 +0000 | [diff] [blame] | 3306 | ME = V.getActiveBits(); |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3307 | return true; |
| 3308 | } |
| 3309 | |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3310 | /// FoldLogicalPlusAnd - This is part of an expression (LHS +/- RHS) & Mask, |
| 3311 | /// where isSub determines whether the operator is a sub. If we can fold one of |
| 3312 | /// the following xforms: |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3313 | /// |
| 3314 | /// ((A & N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == Mask |
| 3315 | /// ((A | N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0 |
| 3316 | /// ((A ^ N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0 |
| 3317 | /// |
| 3318 | /// return (A +/- B). |
| 3319 | /// |
| 3320 | Value *InstCombiner::FoldLogicalPlusAnd(Value *LHS, Value *RHS, |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3321 | ConstantInt *Mask, bool isSub, |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3322 | Instruction &I) { |
| 3323 | Instruction *LHSI = dyn_cast<Instruction>(LHS); |
| 3324 | if (!LHSI || LHSI->getNumOperands() != 2 || |
| 3325 | !isa<ConstantInt>(LHSI->getOperand(1))) return 0; |
| 3326 | |
| 3327 | ConstantInt *N = cast<ConstantInt>(LHSI->getOperand(1)); |
| 3328 | |
| 3329 | switch (LHSI->getOpcode()) { |
| 3330 | default: return 0; |
| 3331 | case Instruction::And: |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 3332 | if (And(N, Mask) == Mask) { |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3333 | // 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] | 3334 | if ((Mask->getValue().countLeadingZeros() + |
| 3335 | Mask->getValue().countPopulation()) == |
| 3336 | Mask->getValue().getBitWidth()) |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3337 | break; |
| 3338 | |
| 3339 | // Otherwise, if Mask is 0+1+0+, and if B is known to have the low 0+ |
| 3340 | // part, we don't need any explicit masks to take them out of A. If that |
| 3341 | // is all N is, ignore it. |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 3342 | uint32_t MB = 0, ME = 0; |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3343 | if (isRunOfOnes(Mask, MB, ME)) { // begin/end bit of run, inclusive |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 3344 | uint32_t BitWidth = cast<IntegerType>(RHS->getType())->getBitWidth(); |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3345 | APInt Mask(APInt::getLowBitsSet(BitWidth, MB-1)); |
Chris Lattner | 3bedbd9 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3346 | if (MaskedValueIsZero(RHS, Mask)) |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3347 | break; |
| 3348 | } |
| 3349 | } |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3350 | return 0; |
| 3351 | case Instruction::Or: |
| 3352 | case Instruction::Xor: |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3353 | // 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] | 3354 | if ((Mask->getValue().countLeadingZeros() + |
| 3355 | Mask->getValue().countPopulation()) == Mask->getValue().getBitWidth() |
Reid Spencer | 6eb0d99 | 2007-03-26 23:58:26 +0000 | [diff] [blame] | 3356 | && And(N, Mask)->isZero()) |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3357 | break; |
| 3358 | return 0; |
| 3359 | } |
| 3360 | |
| 3361 | Instruction *New; |
| 3362 | if (isSub) |
| 3363 | New = BinaryOperator::createSub(LHSI->getOperand(0), RHS, "fold"); |
| 3364 | else |
| 3365 | New = BinaryOperator::createAdd(LHSI->getOperand(0), RHS, "fold"); |
| 3366 | return InsertNewInstBefore(New, I); |
| 3367 | } |
| 3368 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3369 | Instruction *InstCombiner::visitAnd(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 3370 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3371 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3372 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3373 | if (isa<UndefValue>(Op1)) // X & undef -> 0 |
| 3374 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 3375 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3376 | // and X, X = X |
| 3377 | if (Op0 == Op1) |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 3378 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3379 | |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3380 | // 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] | 3381 | // purpose is to compute bits we don't care about. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 3382 | if (!isa<VectorType>(I.getType())) { |
Reid Spencer | a03d45f | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 3383 | uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth(); |
| 3384 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 3385 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth), |
Chris Lattner | 696ee0a | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3386 | KnownZero, KnownOne)) |
Reid Spencer | 6eb0d99 | 2007-03-26 23:58:26 +0000 | [diff] [blame] | 3387 | return &I; |
Chris Lattner | 696ee0a | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3388 | } else { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 3389 | if (ConstantVector *CP = dyn_cast<ConstantVector>(Op1)) { |
Chris Lattner | 041a6c9 | 2007-06-15 05:26:55 +0000 | [diff] [blame] | 3390 | if (CP->isAllOnesValue()) // X & <-1,-1> -> X |
Chris Lattner | 696ee0a | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3391 | return ReplaceInstUsesWith(I, I.getOperand(0)); |
Chris Lattner | 041a6c9 | 2007-06-15 05:26:55 +0000 | [diff] [blame] | 3392 | } else if (isa<ConstantAggregateZero>(Op1)) { |
| 3393 | return ReplaceInstUsesWith(I, Op1); // X & <0,0> -> <0,0> |
Chris Lattner | 696ee0a | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3394 | } |
| 3395 | } |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 3396 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3397 | if (ConstantInt *AndRHS = dyn_cast<ConstantInt>(Op1)) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3398 | const APInt& AndRHSMask = AndRHS->getValue(); |
| 3399 | APInt NotAndRHS(~AndRHSMask); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3400 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3401 | // Optimize a variety of ((val OP C1) & C2) combinations... |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3402 | if (isa<BinaryOperator>(Op0)) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3403 | Instruction *Op0I = cast<Instruction>(Op0); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3404 | Value *Op0LHS = Op0I->getOperand(0); |
| 3405 | Value *Op0RHS = Op0I->getOperand(1); |
| 3406 | switch (Op0I->getOpcode()) { |
| 3407 | case Instruction::Xor: |
| 3408 | case Instruction::Or: |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 3409 | // If the mask is only needed on one incoming arm, push it up. |
| 3410 | if (Op0I->hasOneUse()) { |
| 3411 | if (MaskedValueIsZero(Op0LHS, NotAndRHS)) { |
| 3412 | // Not masking anything out for the LHS, move to RHS. |
| 3413 | Instruction *NewRHS = BinaryOperator::createAnd(Op0RHS, AndRHS, |
| 3414 | Op0RHS->getName()+".masked"); |
| 3415 | InsertNewInstBefore(NewRHS, I); |
| 3416 | return BinaryOperator::create( |
| 3417 | cast<BinaryOperator>(Op0I)->getOpcode(), Op0LHS, NewRHS); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3418 | } |
Chris Lattner | 3bedbd9 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3419 | if (!isa<Constant>(Op0RHS) && |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 3420 | MaskedValueIsZero(Op0RHS, NotAndRHS)) { |
| 3421 | // Not masking anything out for the RHS, move to LHS. |
| 3422 | Instruction *NewLHS = BinaryOperator::createAnd(Op0LHS, AndRHS, |
| 3423 | Op0LHS->getName()+".masked"); |
| 3424 | InsertNewInstBefore(NewLHS, I); |
| 3425 | return BinaryOperator::create( |
| 3426 | cast<BinaryOperator>(Op0I)->getOpcode(), NewLHS, Op0RHS); |
| 3427 | } |
| 3428 | } |
| 3429 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3430 | break; |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3431 | case Instruction::Add: |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3432 | // ((A & N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == AndRHS. |
| 3433 | // ((A | N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0 |
| 3434 | // ((A ^ N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0 |
| 3435 | if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, false, I)) |
| 3436 | return BinaryOperator::createAnd(V, AndRHS); |
| 3437 | if (Value *V = FoldLogicalPlusAnd(Op0RHS, Op0LHS, AndRHS, false, I)) |
| 3438 | return BinaryOperator::createAnd(V, AndRHS); // Add commutes |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3439 | break; |
| 3440 | |
| 3441 | case Instruction::Sub: |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3442 | // ((A & N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == AndRHS. |
| 3443 | // ((A | N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0 |
| 3444 | // ((A ^ N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0 |
| 3445 | if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, true, I)) |
| 3446 | return BinaryOperator::createAnd(V, AndRHS); |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3447 | break; |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3448 | } |
| 3449 | |
Chris Lattner | 5840326 | 2003-07-23 19:25:52 +0000 | [diff] [blame] | 3450 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3451 | if (Instruction *Res = OptAndOp(Op0I, Op0CI, AndRHS, I)) |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3452 | return Res; |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3453 | } else if (CastInst *CI = dyn_cast<CastInst>(Op0)) { |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3454 | // If this is an integer truncation or change from signed-to-unsigned, and |
| 3455 | // if the source is an and/or with immediate, transform it. This |
| 3456 | // frequently occurs for bitfield accesses. |
| 3457 | if (Instruction *CastOp = dyn_cast<Instruction>(CI->getOperand(0))) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3458 | if ((isa<TruncInst>(CI) || isa<BitCastInst>(CI)) && |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3459 | CastOp->getNumOperands() == 2) |
Chris Lattner | 7560c3a | 2006-02-08 07:34:50 +0000 | [diff] [blame] | 3460 | if (ConstantInt *AndCI = dyn_cast<ConstantInt>(CastOp->getOperand(1))) |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3461 | if (CastOp->getOpcode() == Instruction::And) { |
| 3462 | // Change: and (cast (and X, C1) to T), C2 |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3463 | // into : and (cast X to T), trunc_or_bitcast(C1)&C2 |
| 3464 | // This will fold the two constants together, which may allow |
| 3465 | // other simplifications. |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 3466 | Instruction *NewCast = CastInst::createTruncOrBitCast( |
| 3467 | CastOp->getOperand(0), I.getType(), |
| 3468 | CastOp->getName()+".shrunk"); |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3469 | NewCast = InsertNewInstBefore(NewCast, I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3470 | // trunc_or_bitcast(C1)&C2 |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 3471 | Constant *C3 = ConstantExpr::getTruncOrBitCast(AndCI,I.getType()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3472 | C3 = ConstantExpr::getAnd(C3, AndRHS); |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3473 | return BinaryOperator::createAnd(NewCast, C3); |
| 3474 | } else if (CastOp->getOpcode() == Instruction::Or) { |
| 3475 | // Change: and (cast (or X, C1) to T), C2 |
| 3476 | // into : trunc(C1)&C2 iff trunc(C1)&C2 == C2 |
Chris Lattner | bb4e7b2 | 2006-12-12 19:11:20 +0000 | [diff] [blame] | 3477 | Constant *C3 = ConstantExpr::getTruncOrBitCast(AndCI,I.getType()); |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3478 | if (ConstantExpr::getAnd(C3, AndRHS) == AndRHS) // trunc(C1)&C2 |
| 3479 | return ReplaceInstUsesWith(I, AndRHS); |
| 3480 | } |
| 3481 | } |
Chris Lattner | 06782f8 | 2003-07-23 19:36:21 +0000 | [diff] [blame] | 3482 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3483 | |
| 3484 | // Try to fold constant and into select arguments. |
| 3485 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3486 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3487 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 3488 | if (isa<PHINode>(Op0)) |
| 3489 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 3490 | return NV; |
Chris Lattner | c6a8aff | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 3491 | } |
| 3492 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 3493 | Value *Op0NotVal = dyn_castNotVal(Op0); |
| 3494 | Value *Op1NotVal = dyn_castNotVal(Op1); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3495 | |
Chris Lattner | 5b62aa7 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 3496 | if (Op0NotVal == Op1 || Op1NotVal == Op0) // A & ~A == ~A & A == 0 |
| 3497 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 3498 | |
Misha Brukman | cb6267b | 2004-07-30 12:50:08 +0000 | [diff] [blame] | 3499 | // (~A & ~B) == (~(A | B)) - De Morgan's Law |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 3500 | if (Op0NotVal && Op1NotVal && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3501 | Instruction *Or = BinaryOperator::createOr(Op0NotVal, Op1NotVal, |
| 3502 | I.getName()+".demorgan"); |
Chris Lattner | c6a8aff | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 3503 | InsertNewInstBefore(Or, I); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3504 | return BinaryOperator::createNot(Or); |
| 3505 | } |
Chris Lattner | 2082ad9 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3506 | |
| 3507 | { |
Chris Lattner | 003b620 | 2007-06-15 05:58:24 +0000 | [diff] [blame] | 3508 | Value *A = 0, *B = 0, *C = 0, *D = 0; |
| 3509 | if (match(Op0, m_Or(m_Value(A), m_Value(B)))) { |
Chris Lattner | 2082ad9 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3510 | if (A == Op1 || B == Op1) // (A | ?) & A --> A |
| 3511 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 003b620 | 2007-06-15 05:58:24 +0000 | [diff] [blame] | 3512 | |
| 3513 | // (A|B) & ~(A&B) -> A^B |
| 3514 | if (match(Op1, m_Not(m_And(m_Value(C), m_Value(D))))) { |
| 3515 | if ((A == C && B == D) || (A == D && B == C)) |
| 3516 | return BinaryOperator::createXor(A, B); |
| 3517 | } |
| 3518 | } |
| 3519 | |
| 3520 | if (match(Op1, m_Or(m_Value(A), m_Value(B)))) { |
Chris Lattner | 2082ad9 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3521 | if (A == Op0 || B == Op0) // A & (A | ?) --> A |
| 3522 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 003b620 | 2007-06-15 05:58:24 +0000 | [diff] [blame] | 3523 | |
| 3524 | // ~(A&B) & (A|B) -> A^B |
| 3525 | if (match(Op0, m_Not(m_And(m_Value(C), m_Value(D))))) { |
| 3526 | if ((A == C && B == D) || (A == D && B == C)) |
| 3527 | return BinaryOperator::createXor(A, B); |
| 3528 | } |
| 3529 | } |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 3530 | |
| 3531 | if (Op0->hasOneUse() && |
| 3532 | match(Op0, m_Xor(m_Value(A), m_Value(B)))) { |
| 3533 | if (A == Op1) { // (A^B)&A -> A&(A^B) |
| 3534 | I.swapOperands(); // Simplify below |
| 3535 | std::swap(Op0, Op1); |
| 3536 | } else if (B == Op1) { // (A^B)&B -> B&(B^A) |
| 3537 | cast<BinaryOperator>(Op0)->swapOperands(); |
| 3538 | I.swapOperands(); // Simplify below |
| 3539 | std::swap(Op0, Op1); |
| 3540 | } |
| 3541 | } |
| 3542 | if (Op1->hasOneUse() && |
| 3543 | match(Op1, m_Xor(m_Value(A), m_Value(B)))) { |
| 3544 | if (B == Op0) { // B&(A^B) -> B&(B^A) |
| 3545 | cast<BinaryOperator>(Op1)->swapOperands(); |
| 3546 | std::swap(A, B); |
| 3547 | } |
| 3548 | if (A == Op0) { // A&(A^B) -> A & ~B |
| 3549 | Instruction *NotB = BinaryOperator::createNot(B, "tmp"); |
| 3550 | InsertNewInstBefore(NotB, I); |
| 3551 | return BinaryOperator::createAnd(A, NotB); |
| 3552 | } |
| 3553 | } |
Chris Lattner | 2082ad9 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3554 | } |
| 3555 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3556 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(Op1)) { |
| 3557 | // (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B) |
| 3558 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS))) |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3559 | return R; |
| 3560 | |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3561 | Value *LHSVal, *RHSVal; |
| 3562 | ConstantInt *LHSCst, *RHSCst; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3563 | ICmpInst::Predicate LHSCC, RHSCC; |
| 3564 | if (match(Op0, m_ICmp(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst)))) |
| 3565 | if (match(RHS, m_ICmp(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst)))) |
| 3566 | if (LHSVal == RHSVal && // Found (X icmp C1) & (X icmp C2) |
| 3567 | // ICMP_[GL]E X, CST is folded to ICMP_[GL]T elsewhere. |
| 3568 | LHSCC != ICmpInst::ICMP_UGE && LHSCC != ICmpInst::ICMP_ULE && |
| 3569 | RHSCC != ICmpInst::ICMP_UGE && RHSCC != ICmpInst::ICMP_ULE && |
| 3570 | LHSCC != ICmpInst::ICMP_SGE && LHSCC != ICmpInst::ICMP_SLE && |
Chris Lattner | eec8b9a | 2007-11-22 23:47:13 +0000 | [diff] [blame] | 3571 | RHSCC != ICmpInst::ICMP_SGE && RHSCC != ICmpInst::ICMP_SLE && |
| 3572 | |
| 3573 | // Don't try to fold ICMP_SLT + ICMP_ULT. |
| 3574 | (ICmpInst::isEquality(LHSCC) || ICmpInst::isEquality(RHSCC) || |
| 3575 | ICmpInst::isSignedPredicate(LHSCC) == |
| 3576 | ICmpInst::isSignedPredicate(RHSCC))) { |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3577 | // Ensure that the larger constant is on the RHS. |
Chris Lattner | ee2b7a4 | 2008-01-13 20:59:02 +0000 | [diff] [blame] | 3578 | ICmpInst::Predicate GT; |
| 3579 | if (ICmpInst::isSignedPredicate(LHSCC) || |
| 3580 | (ICmpInst::isEquality(LHSCC) && |
| 3581 | ICmpInst::isSignedPredicate(RHSCC))) |
| 3582 | GT = ICmpInst::ICMP_SGT; |
| 3583 | else |
| 3584 | GT = ICmpInst::ICMP_UGT; |
| 3585 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3586 | Constant *Cmp = ConstantExpr::getICmp(GT, LHSCst, RHSCst); |
| 3587 | ICmpInst *LHS = cast<ICmpInst>(Op0); |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 3588 | if (cast<ConstantInt>(Cmp)->getZExtValue()) { |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3589 | std::swap(LHS, RHS); |
| 3590 | std::swap(LHSCst, RHSCst); |
| 3591 | std::swap(LHSCC, RHSCC); |
| 3592 | } |
| 3593 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3594 | // At this point, we know we have have two icmp instructions |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3595 | // comparing a value against two constants and and'ing the result |
| 3596 | // together. Because of the above check, we know that we only have |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3597 | // icmp eq, icmp ne, icmp [su]lt, and icmp [SU]gt here. We also know |
| 3598 | // (from the FoldICmpLogical check above), that the two constants |
| 3599 | // are not equal and that the larger constant is on the RHS |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3600 | assert(LHSCst != RHSCst && "Compares not folded above?"); |
| 3601 | |
| 3602 | switch (LHSCC) { |
| 3603 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3604 | case ICmpInst::ICMP_EQ: |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3605 | switch (RHSCC) { |
| 3606 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3607 | case ICmpInst::ICMP_EQ: // (X == 13 & X == 15) -> false |
| 3608 | case ICmpInst::ICMP_UGT: // (X == 13 & X > 15) -> false |
| 3609 | case ICmpInst::ICMP_SGT: // (X == 13 & X > 15) -> false |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3610 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3611 | case ICmpInst::ICMP_NE: // (X == 13 & X != 15) -> X == 13 |
| 3612 | case ICmpInst::ICMP_ULT: // (X == 13 & X < 15) -> X == 13 |
| 3613 | case ICmpInst::ICMP_SLT: // (X == 13 & X < 15) -> X == 13 |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3614 | return ReplaceInstUsesWith(I, LHS); |
| 3615 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3616 | case ICmpInst::ICMP_NE: |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3617 | switch (RHSCC) { |
| 3618 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3619 | case ICmpInst::ICMP_ULT: |
| 3620 | if (LHSCst == SubOne(RHSCst)) // (X != 13 & X u< 14) -> X < 13 |
| 3621 | return new ICmpInst(ICmpInst::ICMP_ULT, LHSVal, LHSCst); |
| 3622 | break; // (X != 13 & X u< 15) -> no change |
| 3623 | case ICmpInst::ICMP_SLT: |
| 3624 | if (LHSCst == SubOne(RHSCst)) // (X != 13 & X s< 14) -> X < 13 |
| 3625 | return new ICmpInst(ICmpInst::ICMP_SLT, LHSVal, LHSCst); |
| 3626 | break; // (X != 13 & X s< 15) -> no change |
| 3627 | case ICmpInst::ICMP_EQ: // (X != 13 & X == 15) -> X == 15 |
| 3628 | case ICmpInst::ICMP_UGT: // (X != 13 & X u> 15) -> X u> 15 |
| 3629 | case ICmpInst::ICMP_SGT: // (X != 13 & X s> 15) -> X s> 15 |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3630 | return ReplaceInstUsesWith(I, RHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3631 | case ICmpInst::ICMP_NE: |
| 3632 | if (LHSCst == SubOne(RHSCst)){// (X != 13 & X != 14) -> X-13 >u 1 |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3633 | Constant *AddCST = ConstantExpr::getNeg(LHSCst); |
| 3634 | Instruction *Add = BinaryOperator::createAdd(LHSVal, AddCST, |
| 3635 | LHSVal->getName()+".off"); |
| 3636 | InsertNewInstBefore(Add, I); |
Chris Lattner | 424db02 | 2007-01-27 23:08:34 +0000 | [diff] [blame] | 3637 | return new ICmpInst(ICmpInst::ICMP_UGT, Add, |
| 3638 | ConstantInt::get(Add->getType(), 1)); |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3639 | } |
| 3640 | break; // (X != 13 & X != 15) -> no change |
| 3641 | } |
| 3642 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3643 | case ICmpInst::ICMP_ULT: |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3644 | switch (RHSCC) { |
| 3645 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3646 | case ICmpInst::ICMP_EQ: // (X u< 13 & X == 15) -> false |
| 3647 | case ICmpInst::ICMP_UGT: // (X u< 13 & X u> 15) -> false |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3648 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3649 | case ICmpInst::ICMP_SGT: // (X u< 13 & X s> 15) -> no change |
| 3650 | break; |
| 3651 | case ICmpInst::ICMP_NE: // (X u< 13 & X != 15) -> X u< 13 |
| 3652 | 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] | 3653 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3654 | case ICmpInst::ICMP_SLT: // (X u< 13 & X s< 15) -> no change |
| 3655 | break; |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3656 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3657 | break; |
| 3658 | case ICmpInst::ICMP_SLT: |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3659 | switch (RHSCC) { |
| 3660 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3661 | case ICmpInst::ICMP_EQ: // (X s< 13 & X == 15) -> false |
| 3662 | case ICmpInst::ICMP_SGT: // (X s< 13 & X s> 15) -> false |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3663 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3664 | case ICmpInst::ICMP_UGT: // (X s< 13 & X u> 15) -> no change |
| 3665 | break; |
| 3666 | case ICmpInst::ICMP_NE: // (X s< 13 & X != 15) -> X < 13 |
| 3667 | case ICmpInst::ICMP_SLT: // (X s< 13 & X s< 15) -> X < 13 |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3668 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3669 | case ICmpInst::ICMP_ULT: // (X s< 13 & X u< 15) -> no change |
| 3670 | break; |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3671 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3672 | break; |
| 3673 | case ICmpInst::ICMP_UGT: |
| 3674 | switch (RHSCC) { |
| 3675 | default: assert(0 && "Unknown integer condition code!"); |
| 3676 | case ICmpInst::ICMP_EQ: // (X u> 13 & X == 15) -> X > 13 |
| 3677 | return ReplaceInstUsesWith(I, LHS); |
| 3678 | case ICmpInst::ICMP_UGT: // (X u> 13 & X u> 15) -> X u> 15 |
| 3679 | return ReplaceInstUsesWith(I, RHS); |
| 3680 | case ICmpInst::ICMP_SGT: // (X u> 13 & X s> 15) -> no change |
| 3681 | break; |
| 3682 | case ICmpInst::ICMP_NE: |
| 3683 | if (RHSCst == AddOne(LHSCst)) // (X u> 13 & X != 14) -> X u> 14 |
| 3684 | return new ICmpInst(LHSCC, LHSVal, RHSCst); |
| 3685 | break; // (X u> 13 & X != 15) -> no change |
| 3686 | case ICmpInst::ICMP_ULT: // (X u> 13 & X u< 15) ->(X-14) <u 1 |
| 3687 | return InsertRangeTest(LHSVal, AddOne(LHSCst), RHSCst, false, |
| 3688 | true, I); |
| 3689 | case ICmpInst::ICMP_SLT: // (X u> 13 & X s< 15) -> no change |
| 3690 | break; |
| 3691 | } |
| 3692 | break; |
| 3693 | case ICmpInst::ICMP_SGT: |
| 3694 | switch (RHSCC) { |
| 3695 | default: assert(0 && "Unknown integer condition code!"); |
Chris Lattner | a7d1ab0 | 2007-11-16 06:04:17 +0000 | [diff] [blame] | 3696 | case ICmpInst::ICMP_EQ: // (X s> 13 & X == 15) -> X == 15 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3697 | case ICmpInst::ICMP_SGT: // (X s> 13 & X s> 15) -> X s> 15 |
| 3698 | return ReplaceInstUsesWith(I, RHS); |
| 3699 | case ICmpInst::ICMP_UGT: // (X s> 13 & X u> 15) -> no change |
| 3700 | break; |
| 3701 | case ICmpInst::ICMP_NE: |
| 3702 | if (RHSCst == AddOne(LHSCst)) // (X s> 13 & X != 14) -> X s> 14 |
| 3703 | return new ICmpInst(LHSCC, LHSVal, RHSCst); |
| 3704 | break; // (X s> 13 & X != 15) -> no change |
| 3705 | case ICmpInst::ICMP_SLT: // (X s> 13 & X s< 15) ->(X-14) s< 1 |
| 3706 | return InsertRangeTest(LHSVal, AddOne(LHSCst), RHSCst, true, |
| 3707 | true, I); |
| 3708 | case ICmpInst::ICMP_ULT: // (X s> 13 & X u< 15) -> no change |
| 3709 | break; |
| 3710 | } |
| 3711 | break; |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3712 | } |
| 3713 | } |
| 3714 | } |
| 3715 | |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3716 | // fold (and (cast A), (cast B)) -> (cast (and A, B)) |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3717 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) |
| 3718 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) |
| 3719 | if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind ? |
| 3720 | const Type *SrcTy = Op0C->getOperand(0)->getType(); |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 3721 | if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() && |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3722 | // 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] | 3723 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), |
| 3724 | I.getType(), TD) && |
| 3725 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), |
| 3726 | I.getType(), TD)) { |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3727 | Instruction *NewOp = BinaryOperator::createAnd(Op0C->getOperand(0), |
| 3728 | Op1C->getOperand(0), |
| 3729 | I.getName()); |
| 3730 | InsertNewInstBefore(NewOp, I); |
| 3731 | return CastInst::create(Op0C->getOpcode(), NewOp, I.getType()); |
| 3732 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3733 | } |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 3734 | |
| 3735 | // (X >> Z) & (Y >> Z) -> (X&Y) >> Z for all shifts. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3736 | if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) { |
| 3737 | if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0)) |
| 3738 | if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() && |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 3739 | SI0->getOperand(1) == SI1->getOperand(1) && |
| 3740 | (SI0->hasOneUse() || SI1->hasOneUse())) { |
| 3741 | Instruction *NewOp = |
| 3742 | InsertNewInstBefore(BinaryOperator::createAnd(SI0->getOperand(0), |
| 3743 | SI1->getOperand(0), |
| 3744 | SI0->getName()), I); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3745 | return BinaryOperator::create(SI1->getOpcode(), NewOp, |
| 3746 | SI1->getOperand(1)); |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 3747 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3748 | } |
| 3749 | |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 3750 | // (fcmp ord x, c) & (fcmp ord y, c) -> (fcmp ord x, y) |
| 3751 | if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0))) { |
| 3752 | if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1))) { |
| 3753 | if (LHS->getPredicate() == FCmpInst::FCMP_ORD && |
| 3754 | RHS->getPredicate() == FCmpInst::FCMP_ORD) |
| 3755 | if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1))) |
| 3756 | if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) { |
| 3757 | // If either of the constants are nans, then the whole thing returns |
| 3758 | // false. |
Chris Lattner | be3e348 | 2007-10-24 18:54:45 +0000 | [diff] [blame] | 3759 | if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN()) |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 3760 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
| 3761 | return new FCmpInst(FCmpInst::FCMP_ORD, LHS->getOperand(0), |
| 3762 | RHS->getOperand(0)); |
| 3763 | } |
| 3764 | } |
| 3765 | } |
| 3766 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3767 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3768 | } |
| 3769 | |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3770 | /// CollectBSwapParts - Look to see if the specified value defines a single byte |
| 3771 | /// in the result. If it does, and if the specified byte hasn't been filled in |
| 3772 | /// yet, fill it in and return false. |
Chris Lattner | 535014f | 2007-02-15 22:52:10 +0000 | [diff] [blame] | 3773 | static bool CollectBSwapParts(Value *V, SmallVector<Value*, 8> &ByteValues) { |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3774 | Instruction *I = dyn_cast<Instruction>(V); |
| 3775 | if (I == 0) return true; |
| 3776 | |
| 3777 | // If this is an or instruction, it is an inner node of the bswap. |
| 3778 | if (I->getOpcode() == Instruction::Or) |
| 3779 | return CollectBSwapParts(I->getOperand(0), ByteValues) || |
| 3780 | CollectBSwapParts(I->getOperand(1), ByteValues); |
| 3781 | |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3782 | uint32_t BitWidth = I->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3783 | // If this is a shift by a constant int, and it is "24", then its operand |
| 3784 | // defines a byte. We only handle unsigned types here. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3785 | if (I->isShift() && isa<ConstantInt>(I->getOperand(1))) { |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3786 | // Not shifting the entire input by N-1 bytes? |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3787 | if (cast<ConstantInt>(I->getOperand(1))->getLimitedValue(BitWidth) != |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3788 | 8*(ByteValues.size()-1)) |
| 3789 | return true; |
| 3790 | |
| 3791 | unsigned DestNo; |
| 3792 | if (I->getOpcode() == Instruction::Shl) { |
| 3793 | // X << 24 defines the top byte with the lowest of the input bytes. |
| 3794 | DestNo = ByteValues.size()-1; |
| 3795 | } else { |
| 3796 | // X >>u 24 defines the low byte with the highest of the input bytes. |
| 3797 | DestNo = 0; |
| 3798 | } |
| 3799 | |
| 3800 | // If the destination byte value is already defined, the values are or'd |
| 3801 | // together, which isn't a bswap (unless it's an or of the same bits). |
| 3802 | if (ByteValues[DestNo] && ByteValues[DestNo] != I->getOperand(0)) |
| 3803 | return true; |
| 3804 | ByteValues[DestNo] = I->getOperand(0); |
| 3805 | return false; |
| 3806 | } |
| 3807 | |
| 3808 | // Otherwise, we can only handle and(shift X, imm), imm). Bail out of if we |
| 3809 | // don't have this. |
| 3810 | Value *Shift = 0, *ShiftLHS = 0; |
| 3811 | ConstantInt *AndAmt = 0, *ShiftAmt = 0; |
| 3812 | if (!match(I, m_And(m_Value(Shift), m_ConstantInt(AndAmt))) || |
| 3813 | !match(Shift, m_Shift(m_Value(ShiftLHS), m_ConstantInt(ShiftAmt)))) |
| 3814 | return true; |
| 3815 | Instruction *SI = cast<Instruction>(Shift); |
| 3816 | |
| 3817 | // 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] | 3818 | if (ShiftAmt->getLimitedValue(BitWidth) & 7 || |
| 3819 | ShiftAmt->getLimitedValue(BitWidth) > 8*ByteValues.size()) |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3820 | return true; |
| 3821 | |
| 3822 | // Turn 0xFF -> 0, 0xFF00 -> 1, 0xFF0000 -> 2, etc. |
| 3823 | unsigned DestByte; |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3824 | if (AndAmt->getValue().getActiveBits() > 64) |
| 3825 | return true; |
| 3826 | uint64_t AndAmtVal = AndAmt->getZExtValue(); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3827 | for (DestByte = 0; DestByte != ByteValues.size(); ++DestByte) |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3828 | if (AndAmtVal == uint64_t(0xFF) << 8*DestByte) |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3829 | break; |
| 3830 | // Unknown mask for bswap. |
| 3831 | if (DestByte == ByteValues.size()) return true; |
| 3832 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3833 | unsigned ShiftBytes = ShiftAmt->getZExtValue()/8; |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3834 | unsigned SrcByte; |
| 3835 | if (SI->getOpcode() == Instruction::Shl) |
| 3836 | SrcByte = DestByte - ShiftBytes; |
| 3837 | else |
| 3838 | SrcByte = DestByte + ShiftBytes; |
| 3839 | |
| 3840 | // If the SrcByte isn't a bswapped value from the DestByte, reject it. |
| 3841 | if (SrcByte != ByteValues.size()-DestByte-1) |
| 3842 | return true; |
| 3843 | |
| 3844 | // If the destination byte value is already defined, the values are or'd |
| 3845 | // together, which isn't a bswap (unless it's an or of the same bits). |
| 3846 | if (ByteValues[DestByte] && ByteValues[DestByte] != SI->getOperand(0)) |
| 3847 | return true; |
| 3848 | ByteValues[DestByte] = SI->getOperand(0); |
| 3849 | return false; |
| 3850 | } |
| 3851 | |
| 3852 | /// MatchBSwap - Given an OR instruction, check to see if this is a bswap idiom. |
| 3853 | /// If so, insert the new bswap intrinsic and return it. |
| 3854 | Instruction *InstCombiner::MatchBSwap(BinaryOperator &I) { |
Chris Lattner | 55fc8c4 | 2007-04-01 20:57:36 +0000 | [diff] [blame] | 3855 | const IntegerType *ITy = dyn_cast<IntegerType>(I.getType()); |
| 3856 | if (!ITy || ITy->getBitWidth() % 16) |
| 3857 | return 0; // Can only bswap pairs of bytes. Can't do vectors. |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3858 | |
| 3859 | /// ByteValues - For each byte of the result, we keep track of which value |
| 3860 | /// defines each byte. |
Chris Lattner | 535014f | 2007-02-15 22:52:10 +0000 | [diff] [blame] | 3861 | SmallVector<Value*, 8> ByteValues; |
Chris Lattner | 55fc8c4 | 2007-04-01 20:57:36 +0000 | [diff] [blame] | 3862 | ByteValues.resize(ITy->getBitWidth()/8); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3863 | |
| 3864 | // Try to find all the pieces corresponding to the bswap. |
| 3865 | if (CollectBSwapParts(I.getOperand(0), ByteValues) || |
| 3866 | CollectBSwapParts(I.getOperand(1), ByteValues)) |
| 3867 | return 0; |
| 3868 | |
| 3869 | // Check to see if all of the bytes come from the same value. |
| 3870 | Value *V = ByteValues[0]; |
| 3871 | if (V == 0) return 0; // Didn't find a byte? Must be zero. |
| 3872 | |
| 3873 | // Check to make sure that all of the bytes come from the same value. |
| 3874 | for (unsigned i = 1, e = ByteValues.size(); i != e; ++i) |
| 3875 | if (ByteValues[i] != V) |
| 3876 | return 0; |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 3877 | const Type *Tys[] = { ITy }; |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3878 | Module *M = I.getParent()->getParent()->getParent(); |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 3879 | Function *F = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3880 | return new CallInst(F, V); |
| 3881 | } |
| 3882 | |
| 3883 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3884 | Instruction *InstCombiner::visitOr(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 3885 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3886 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3887 | |
Chris Lattner | 42593e6 | 2007-03-24 23:56:43 +0000 | [diff] [blame] | 3888 | if (isa<UndefValue>(Op1)) // X | undef -> -1 |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 3889 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3890 | |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3891 | // or X, X = X |
| 3892 | if (Op0 == Op1) |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 3893 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3894 | |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3895 | // See if we can simplify any instructions used by the instruction whose sole |
| 3896 | // purpose is to compute bits we don't care about. |
Chris Lattner | 42593e6 | 2007-03-24 23:56:43 +0000 | [diff] [blame] | 3897 | if (!isa<VectorType>(I.getType())) { |
| 3898 | uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth(); |
| 3899 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 3900 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth), |
| 3901 | KnownZero, KnownOne)) |
| 3902 | return &I; |
Chris Lattner | 041a6c9 | 2007-06-15 05:26:55 +0000 | [diff] [blame] | 3903 | } else if (isa<ConstantAggregateZero>(Op1)) { |
| 3904 | return ReplaceInstUsesWith(I, Op0); // X | <0,0> -> X |
| 3905 | } else if (ConstantVector *CP = dyn_cast<ConstantVector>(Op1)) { |
| 3906 | if (CP->isAllOnesValue()) // X | <-1,-1> -> <-1,-1> |
| 3907 | return ReplaceInstUsesWith(I, I.getOperand(1)); |
Chris Lattner | 42593e6 | 2007-03-24 23:56:43 +0000 | [diff] [blame] | 3908 | } |
Chris Lattner | 041a6c9 | 2007-06-15 05:26:55 +0000 | [diff] [blame] | 3909 | |
| 3910 | |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3911 | |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3912 | // or X, -1 == -1 |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3913 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 3914 | ConstantInt *C1 = 0; Value *X = 0; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3915 | // (X & C1) | C2 --> (X | C2) & (C1|C2) |
| 3916 | 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] | 3917 | Instruction *Or = BinaryOperator::createOr(X, RHS); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3918 | InsertNewInstBefore(Or, I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3919 | Or->takeName(Op0); |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 3920 | return BinaryOperator::createAnd(Or, |
| 3921 | ConstantInt::get(RHS->getValue() | C1->getValue())); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3922 | } |
Chris Lattner | ad44ebf | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 3923 | |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3924 | // (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2) |
| 3925 | 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] | 3926 | Instruction *Or = BinaryOperator::createOr(X, RHS); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3927 | InsertNewInstBefore(Or, I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3928 | Or->takeName(Op0); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3929 | return BinaryOperator::createXor(Or, |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 3930 | ConstantInt::get(C1->getValue() & ~RHS->getValue())); |
Chris Lattner | ad44ebf | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 3931 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3932 | |
| 3933 | // Try to fold constant and into select arguments. |
| 3934 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3935 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3936 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 3937 | if (isa<PHINode>(Op0)) |
| 3938 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 3939 | return NV; |
Chris Lattner | ad44ebf | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 3940 | } |
| 3941 | |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 3942 | Value *A = 0, *B = 0; |
| 3943 | ConstantInt *C1 = 0, *C2 = 0; |
Chris Lattner | f4d4c87 | 2005-05-07 23:49:08 +0000 | [diff] [blame] | 3944 | |
| 3945 | if (match(Op0, m_And(m_Value(A), m_Value(B)))) |
| 3946 | if (A == Op1 || B == Op1) // (A & ?) | A --> A |
| 3947 | return ReplaceInstUsesWith(I, Op1); |
| 3948 | if (match(Op1, m_And(m_Value(A), m_Value(B)))) |
| 3949 | if (A == Op0 || B == Op0) // A | (A & ?) --> A |
| 3950 | return ReplaceInstUsesWith(I, Op0); |
| 3951 | |
Chris Lattner | 6423d4c | 2006-07-10 20:25:24 +0000 | [diff] [blame] | 3952 | // (A | B) | C and A | (B | C) -> bswap if possible. |
| 3953 | // (A >> B) | (C << D) and (A << B) | (B >> C) -> bswap if possible. |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3954 | if (match(Op0, m_Or(m_Value(), m_Value())) || |
Chris Lattner | 6423d4c | 2006-07-10 20:25:24 +0000 | [diff] [blame] | 3955 | match(Op1, m_Or(m_Value(), m_Value())) || |
| 3956 | (match(Op0, m_Shift(m_Value(), m_Value())) && |
| 3957 | match(Op1, m_Shift(m_Value(), m_Value())))) { |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3958 | if (Instruction *BSwap = MatchBSwap(I)) |
| 3959 | return BSwap; |
| 3960 | } |
| 3961 | |
Chris Lattner | 6e4c649 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 3962 | // (X^C)|Y -> (X|Y)^C iff Y&C == 0 |
| 3963 | 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] | 3964 | MaskedValueIsZero(Op1, C1->getValue())) { |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3965 | Instruction *NOr = BinaryOperator::createOr(A, Op1); |
| 3966 | InsertNewInstBefore(NOr, I); |
| 3967 | NOr->takeName(Op0); |
| 3968 | return BinaryOperator::createXor(NOr, C1); |
Chris Lattner | 6e4c649 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 3969 | } |
| 3970 | |
| 3971 | // Y|(X^C) -> (X|Y)^C iff Y&C == 0 |
| 3972 | 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] | 3973 | MaskedValueIsZero(Op0, C1->getValue())) { |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3974 | Instruction *NOr = BinaryOperator::createOr(A, Op0); |
| 3975 | InsertNewInstBefore(NOr, I); |
| 3976 | NOr->takeName(Op0); |
| 3977 | return BinaryOperator::createXor(NOr, C1); |
Chris Lattner | 6e4c649 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 3978 | } |
| 3979 | |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 3980 | // (A & C)|(B & D) |
Chris Lattner | 2384d7b | 2007-06-19 05:43:49 +0000 | [diff] [blame] | 3981 | Value *C = 0, *D = 0; |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 3982 | if (match(Op0, m_And(m_Value(A), m_Value(C))) && |
| 3983 | match(Op1, m_And(m_Value(B), m_Value(D)))) { |
Chris Lattner | 6cae0e0 | 2007-04-08 07:55:22 +0000 | [diff] [blame] | 3984 | Value *V1 = 0, *V2 = 0, *V3 = 0; |
| 3985 | C1 = dyn_cast<ConstantInt>(C); |
| 3986 | C2 = dyn_cast<ConstantInt>(D); |
| 3987 | if (C1 && C2) { // (A & C1)|(B & C2) |
| 3988 | // If we have: ((V + N) & C1) | (V & C2) |
| 3989 | // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0 |
| 3990 | // replace with V+N. |
| 3991 | if (C1->getValue() == ~C2->getValue()) { |
| 3992 | if ((C2->getValue() & (C2->getValue()+1)) == 0 && // C2 == 0+1+ |
| 3993 | match(A, m_Add(m_Value(V1), m_Value(V2)))) { |
| 3994 | // Add commutes, try both ways. |
| 3995 | if (V1 == B && MaskedValueIsZero(V2, C2->getValue())) |
| 3996 | return ReplaceInstUsesWith(I, A); |
| 3997 | if (V2 == B && MaskedValueIsZero(V1, C2->getValue())) |
| 3998 | return ReplaceInstUsesWith(I, A); |
| 3999 | } |
| 4000 | // Or commutes, try both ways. |
| 4001 | if ((C1->getValue() & (C1->getValue()+1)) == 0 && |
| 4002 | match(B, m_Add(m_Value(V1), m_Value(V2)))) { |
| 4003 | // Add commutes, try both ways. |
| 4004 | if (V1 == A && MaskedValueIsZero(V2, C1->getValue())) |
| 4005 | return ReplaceInstUsesWith(I, B); |
| 4006 | if (V2 == A && MaskedValueIsZero(V1, C1->getValue())) |
| 4007 | return ReplaceInstUsesWith(I, B); |
| 4008 | } |
| 4009 | } |
Chris Lattner | 044e533 | 2007-04-08 08:01:49 +0000 | [diff] [blame] | 4010 | V1 = 0; V2 = 0; V3 = 0; |
Chris Lattner | 6cae0e0 | 2007-04-08 07:55:22 +0000 | [diff] [blame] | 4011 | } |
| 4012 | |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 4013 | // Check to see if we have any common things being and'ed. If so, find the |
| 4014 | // terms for V1 & (V2|V3). |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 4015 | if (isOnlyUse(Op0) || isOnlyUse(Op1)) { |
| 4016 | if (A == B) // (A & C)|(A & D) == A & (C|D) |
| 4017 | V1 = A, V2 = C, V3 = D; |
| 4018 | else if (A == D) // (A & C)|(B & A) == A & (B|C) |
| 4019 | V1 = A, V2 = B, V3 = C; |
| 4020 | else if (C == B) // (A & C)|(C & D) == C & (A|D) |
| 4021 | V1 = C, V2 = A, V3 = D; |
| 4022 | else if (C == D) // (A & C)|(B & C) == C & (A|B) |
| 4023 | V1 = C, V2 = A, V3 = B; |
| 4024 | |
| 4025 | if (V1) { |
| 4026 | Value *Or = |
| 4027 | InsertNewInstBefore(BinaryOperator::createOr(V2, V3, "tmp"), I); |
| 4028 | return BinaryOperator::createAnd(V1, Or); |
Chris Lattner | 0b7c0bf | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 4029 | } |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 4030 | } |
Chris Lattner | e9bed7d | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 4031 | } |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4032 | |
| 4033 | // (X >> Z) | (Y >> Z) -> (X|Y) >> Z for all shifts. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4034 | if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) { |
| 4035 | if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0)) |
| 4036 | if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() && |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4037 | SI0->getOperand(1) == SI1->getOperand(1) && |
| 4038 | (SI0->hasOneUse() || SI1->hasOneUse())) { |
| 4039 | Instruction *NewOp = |
| 4040 | InsertNewInstBefore(BinaryOperator::createOr(SI0->getOperand(0), |
| 4041 | SI1->getOperand(0), |
| 4042 | SI0->getName()), I); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4043 | return BinaryOperator::create(SI1->getOpcode(), NewOp, |
| 4044 | SI1->getOperand(1)); |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4045 | } |
| 4046 | } |
Chris Lattner | 67ca768 | 2003-08-12 19:11:07 +0000 | [diff] [blame] | 4047 | |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4048 | if (match(Op0, m_Not(m_Value(A)))) { // ~A | Op1 |
| 4049 | if (A == Op1) // ~A | A == -1 |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4050 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4051 | } else { |
| 4052 | A = 0; |
| 4053 | } |
Chris Lattner | f4d4c87 | 2005-05-07 23:49:08 +0000 | [diff] [blame] | 4054 | // Note, A is still live here! |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4055 | if (match(Op1, m_Not(m_Value(B)))) { // Op0 | ~B |
| 4056 | if (Op0 == B) |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4057 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | a27231a | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 4058 | |
Misha Brukman | cb6267b | 2004-07-30 12:50:08 +0000 | [diff] [blame] | 4059 | // (~A | ~B) == (~(A & B)) - De Morgan's Law |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4060 | if (A && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
| 4061 | Value *And = InsertNewInstBefore(BinaryOperator::createAnd(A, B, |
| 4062 | I.getName()+".demorgan"), I); |
| 4063 | return BinaryOperator::createNot(And); |
| 4064 | } |
Chris Lattner | a27231a | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 4065 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4066 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4067 | // (icmp1 A, B) | (icmp2 A, B) --> (icmp3 A, B) |
| 4068 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1))) { |
| 4069 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS))) |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 4070 | return R; |
| 4071 | |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4072 | Value *LHSVal, *RHSVal; |
| 4073 | ConstantInt *LHSCst, *RHSCst; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4074 | ICmpInst::Predicate LHSCC, RHSCC; |
| 4075 | if (match(Op0, m_ICmp(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst)))) |
| 4076 | if (match(RHS, m_ICmp(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst)))) |
| 4077 | if (LHSVal == RHSVal && // Found (X icmp C1) | (X icmp C2) |
| 4078 | // icmp [us][gl]e x, cst is folded to icmp [us][gl]t elsewhere. |
| 4079 | LHSCC != ICmpInst::ICMP_UGE && LHSCC != ICmpInst::ICMP_ULE && |
| 4080 | RHSCC != ICmpInst::ICMP_UGE && RHSCC != ICmpInst::ICMP_ULE && |
| 4081 | LHSCC != ICmpInst::ICMP_SGE && LHSCC != ICmpInst::ICMP_SLE && |
Chris Lattner | 8885887 | 2007-05-11 05:55:56 +0000 | [diff] [blame] | 4082 | RHSCC != ICmpInst::ICMP_SGE && RHSCC != ICmpInst::ICMP_SLE && |
| 4083 | // We can't fold (ugt x, C) | (sgt x, C2). |
| 4084 | PredicatesFoldable(LHSCC, RHSCC)) { |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4085 | // Ensure that the larger constant is on the RHS. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4086 | ICmpInst *LHS = cast<ICmpInst>(Op0); |
Chris Lattner | 8885887 | 2007-05-11 05:55:56 +0000 | [diff] [blame] | 4087 | bool NeedsSwap; |
| 4088 | if (ICmpInst::isSignedPredicate(LHSCC)) |
Chris Lattner | 3aea1bd | 2007-05-11 16:58:45 +0000 | [diff] [blame] | 4089 | NeedsSwap = LHSCst->getValue().sgt(RHSCst->getValue()); |
Chris Lattner | 8885887 | 2007-05-11 05:55:56 +0000 | [diff] [blame] | 4090 | else |
Chris Lattner | 3aea1bd | 2007-05-11 16:58:45 +0000 | [diff] [blame] | 4091 | NeedsSwap = LHSCst->getValue().ugt(RHSCst->getValue()); |
Chris Lattner | 8885887 | 2007-05-11 05:55:56 +0000 | [diff] [blame] | 4092 | |
| 4093 | if (NeedsSwap) { |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4094 | std::swap(LHS, RHS); |
| 4095 | std::swap(LHSCst, RHSCst); |
| 4096 | std::swap(LHSCC, RHSCC); |
| 4097 | } |
| 4098 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4099 | // At this point, we know we have have two icmp instructions |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4100 | // comparing a value against two constants and or'ing the result |
| 4101 | // together. Because of the above check, we know that we only have |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4102 | // ICMP_EQ, ICMP_NE, ICMP_LT, and ICMP_GT here. We also know (from the |
| 4103 | // FoldICmpLogical check above), that the two constants are not |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4104 | // equal. |
| 4105 | assert(LHSCst != RHSCst && "Compares not folded above?"); |
| 4106 | |
| 4107 | switch (LHSCC) { |
| 4108 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4109 | case ICmpInst::ICMP_EQ: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4110 | switch (RHSCC) { |
| 4111 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4112 | case ICmpInst::ICMP_EQ: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4113 | if (LHSCst == SubOne(RHSCst)) {// (X == 13 | X == 14) -> X-13 <u 2 |
| 4114 | Constant *AddCST = ConstantExpr::getNeg(LHSCst); |
| 4115 | Instruction *Add = BinaryOperator::createAdd(LHSVal, AddCST, |
| 4116 | LHSVal->getName()+".off"); |
| 4117 | InsertNewInstBefore(Add, I); |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 4118 | AddCST = Subtract(AddOne(RHSCst), LHSCst); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4119 | return new ICmpInst(ICmpInst::ICMP_ULT, Add, AddCST); |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4120 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4121 | break; // (X == 13 | X == 15) -> no change |
| 4122 | case ICmpInst::ICMP_UGT: // (X == 13 | X u> 14) -> no change |
| 4123 | case ICmpInst::ICMP_SGT: // (X == 13 | X s> 14) -> no change |
Chris Lattner | 240d6f4 | 2005-04-19 06:04:18 +0000 | [diff] [blame] | 4124 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4125 | case ICmpInst::ICMP_NE: // (X == 13 | X != 15) -> X != 15 |
| 4126 | case ICmpInst::ICMP_ULT: // (X == 13 | X u< 15) -> X u< 15 |
| 4127 | case ICmpInst::ICMP_SLT: // (X == 13 | X s< 15) -> X s< 15 |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4128 | return ReplaceInstUsesWith(I, RHS); |
| 4129 | } |
| 4130 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4131 | case ICmpInst::ICMP_NE: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4132 | switch (RHSCC) { |
| 4133 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4134 | case ICmpInst::ICMP_EQ: // (X != 13 | X == 15) -> X != 13 |
| 4135 | case ICmpInst::ICMP_UGT: // (X != 13 | X u> 15) -> X != 13 |
| 4136 | case ICmpInst::ICMP_SGT: // (X != 13 | X s> 15) -> X != 13 |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4137 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4138 | case ICmpInst::ICMP_NE: // (X != 13 | X != 15) -> true |
| 4139 | case ICmpInst::ICMP_ULT: // (X != 13 | X u< 15) -> true |
| 4140 | case ICmpInst::ICMP_SLT: // (X != 13 | X s< 15) -> true |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4141 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4142 | } |
| 4143 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4144 | case ICmpInst::ICMP_ULT: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4145 | switch (RHSCC) { |
| 4146 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4147 | case ICmpInst::ICMP_EQ: // (X u< 13 | X == 14) -> no change |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4148 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4149 | 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] | 4150 | // If RHSCst is [us]MAXINT, it is always false. Not handling |
| 4151 | // this can cause overflow. |
| 4152 | if (RHSCst->isMaxValue(false)) |
| 4153 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4154 | return InsertRangeTest(LHSVal, LHSCst, AddOne(RHSCst), false, |
| 4155 | false, I); |
| 4156 | case ICmpInst::ICMP_SGT: // (X u< 13 | X s> 15) -> no change |
| 4157 | break; |
| 4158 | case ICmpInst::ICMP_NE: // (X u< 13 | X != 15) -> X != 15 |
| 4159 | 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] | 4160 | return ReplaceInstUsesWith(I, RHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4161 | case ICmpInst::ICMP_SLT: // (X u< 13 | X s< 15) -> no change |
| 4162 | break; |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4163 | } |
| 4164 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4165 | case ICmpInst::ICMP_SLT: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4166 | switch (RHSCC) { |
| 4167 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4168 | case ICmpInst::ICMP_EQ: // (X s< 13 | X == 14) -> no change |
| 4169 | break; |
| 4170 | 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] | 4171 | // If RHSCst is [us]MAXINT, it is always false. Not handling |
| 4172 | // this can cause overflow. |
| 4173 | if (RHSCst->isMaxValue(true)) |
| 4174 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4175 | return InsertRangeTest(LHSVal, LHSCst, AddOne(RHSCst), true, |
| 4176 | false, I); |
| 4177 | case ICmpInst::ICMP_UGT: // (X s< 13 | X u> 15) -> no change |
| 4178 | break; |
| 4179 | case ICmpInst::ICMP_NE: // (X s< 13 | X != 15) -> X != 15 |
| 4180 | case ICmpInst::ICMP_SLT: // (X s< 13 | X s< 15) -> X s< 15 |
| 4181 | return ReplaceInstUsesWith(I, RHS); |
| 4182 | case ICmpInst::ICMP_ULT: // (X s< 13 | X u< 15) -> no change |
| 4183 | break; |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4184 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4185 | break; |
| 4186 | case ICmpInst::ICMP_UGT: |
| 4187 | switch (RHSCC) { |
| 4188 | default: assert(0 && "Unknown integer condition code!"); |
| 4189 | case ICmpInst::ICMP_EQ: // (X u> 13 | X == 15) -> X u> 13 |
| 4190 | case ICmpInst::ICMP_UGT: // (X u> 13 | X u> 15) -> X u> 13 |
| 4191 | return ReplaceInstUsesWith(I, LHS); |
| 4192 | case ICmpInst::ICMP_SGT: // (X u> 13 | X s> 15) -> no change |
| 4193 | break; |
| 4194 | case ICmpInst::ICMP_NE: // (X u> 13 | X != 15) -> true |
| 4195 | case ICmpInst::ICMP_ULT: // (X u> 13 | X u< 15) -> true |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4196 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4197 | case ICmpInst::ICMP_SLT: // (X u> 13 | X s< 15) -> no change |
| 4198 | break; |
| 4199 | } |
| 4200 | break; |
| 4201 | case ICmpInst::ICMP_SGT: |
| 4202 | switch (RHSCC) { |
| 4203 | default: assert(0 && "Unknown integer condition code!"); |
| 4204 | case ICmpInst::ICMP_EQ: // (X s> 13 | X == 15) -> X > 13 |
| 4205 | case ICmpInst::ICMP_SGT: // (X s> 13 | X s> 15) -> X > 13 |
| 4206 | return ReplaceInstUsesWith(I, LHS); |
| 4207 | case ICmpInst::ICMP_UGT: // (X s> 13 | X u> 15) -> no change |
| 4208 | break; |
| 4209 | case ICmpInst::ICMP_NE: // (X s> 13 | X != 15) -> true |
| 4210 | case ICmpInst::ICMP_SLT: // (X s> 13 | X s< 15) -> true |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4211 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4212 | case ICmpInst::ICMP_ULT: // (X s> 13 | X u< 15) -> no change |
| 4213 | break; |
| 4214 | } |
| 4215 | break; |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4216 | } |
| 4217 | } |
| 4218 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4219 | |
| 4220 | // fold (or (cast A), (cast B)) -> (cast (or A, B)) |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4221 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) { |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4222 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4223 | if (Op0C->getOpcode() == Op1C->getOpcode()) {// same cast kind ? |
| 4224 | const Type *SrcTy = Op0C->getOperand(0)->getType(); |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 4225 | if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() && |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4226 | // 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] | 4227 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), |
| 4228 | I.getType(), TD) && |
| 4229 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), |
| 4230 | I.getType(), TD)) { |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4231 | Instruction *NewOp = BinaryOperator::createOr(Op0C->getOperand(0), |
| 4232 | Op1C->getOperand(0), |
| 4233 | I.getName()); |
| 4234 | InsertNewInstBefore(NewOp, I); |
| 4235 | return CastInst::create(Op0C->getOpcode(), NewOp, I.getType()); |
| 4236 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4237 | } |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4238 | } |
| 4239 | |
| 4240 | |
| 4241 | // (fcmp uno x, c) | (fcmp uno y, c) -> (fcmp uno x, y) |
| 4242 | if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0))) { |
| 4243 | if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1))) { |
| 4244 | if (LHS->getPredicate() == FCmpInst::FCMP_UNO && |
| 4245 | RHS->getPredicate() == FCmpInst::FCMP_UNO) |
| 4246 | if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1))) |
| 4247 | if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) { |
| 4248 | // If either of the constants are nans, then the whole thing returns |
| 4249 | // true. |
Chris Lattner | be3e348 | 2007-10-24 18:54:45 +0000 | [diff] [blame] | 4250 | if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN()) |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4251 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
| 4252 | |
| 4253 | // Otherwise, no need to compare the two constants, compare the |
| 4254 | // rest. |
| 4255 | return new FCmpInst(FCmpInst::FCMP_UNO, LHS->getOperand(0), |
| 4256 | RHS->getOperand(0)); |
| 4257 | } |
| 4258 | } |
| 4259 | } |
Chris Lattner | e9bed7d | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 4260 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4261 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4262 | } |
| 4263 | |
Chris Lattner | c317d39 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 4264 | // XorSelf - Implements: X ^ X --> 0 |
| 4265 | struct XorSelf { |
| 4266 | Value *RHS; |
| 4267 | XorSelf(Value *rhs) : RHS(rhs) {} |
| 4268 | bool shouldApply(Value *LHS) const { return LHS == RHS; } |
| 4269 | Instruction *apply(BinaryOperator &Xor) const { |
| 4270 | return &Xor; |
| 4271 | } |
| 4272 | }; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4273 | |
| 4274 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4275 | Instruction *InstCombiner::visitXor(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 4276 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4277 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4278 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4279 | if (isa<UndefValue>(Op1)) |
| 4280 | return ReplaceInstUsesWith(I, Op1); // X ^ undef -> undef |
| 4281 | |
Chris Lattner | c317d39 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 4282 | // xor X, X = 0, even if X is nested in a sequence of Xor's. |
| 4283 | if (Instruction *Result = AssociativeOpt(I, XorSelf(Op1))) { |
Chris Lattner | a9ff5eb | 2007-08-05 08:47:58 +0000 | [diff] [blame] | 4284 | assert(Result == &I && "AssociativeOpt didn't work?"); Result=Result; |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 4285 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | c317d39 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 4286 | } |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 4287 | |
| 4288 | // See if we can simplify any instructions used by the instruction whose sole |
| 4289 | // purpose is to compute bits we don't care about. |
Reid Spencer | a03d45f | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 4290 | if (!isa<VectorType>(I.getType())) { |
| 4291 | uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth(); |
| 4292 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 4293 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth), |
| 4294 | KnownZero, KnownOne)) |
| 4295 | return &I; |
Chris Lattner | 041a6c9 | 2007-06-15 05:26:55 +0000 | [diff] [blame] | 4296 | } else if (isa<ConstantAggregateZero>(Op1)) { |
| 4297 | return ReplaceInstUsesWith(I, Op0); // X ^ <0,0> -> X |
Reid Spencer | a03d45f | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 4298 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4299 | |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4300 | // Is this a ~ operation? |
| 4301 | if (Value *NotOp = dyn_castNotVal(&I)) { |
| 4302 | // ~(~X & Y) --> (X | ~Y) - De Morgan's Law |
| 4303 | // ~(~X | Y) === (X & ~Y) - De Morgan's Law |
| 4304 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(NotOp)) { |
| 4305 | if (Op0I->getOpcode() == Instruction::And || |
| 4306 | Op0I->getOpcode() == Instruction::Or) { |
| 4307 | if (dyn_castNotVal(Op0I->getOperand(1))) Op0I->swapOperands(); |
| 4308 | if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0))) { |
| 4309 | Instruction *NotY = |
| 4310 | BinaryOperator::createNot(Op0I->getOperand(1), |
| 4311 | Op0I->getOperand(1)->getName()+".not"); |
| 4312 | InsertNewInstBefore(NotY, I); |
| 4313 | if (Op0I->getOpcode() == Instruction::And) |
| 4314 | return BinaryOperator::createOr(Op0NotVal, NotY); |
| 4315 | else |
| 4316 | return BinaryOperator::createAnd(Op0NotVal, NotY); |
| 4317 | } |
| 4318 | } |
| 4319 | } |
| 4320 | } |
| 4321 | |
| 4322 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4323 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Nick Lewycky | f947b3e | 2007-08-06 20:04:16 +0000 | [diff] [blame] | 4324 | // xor (cmp A, B), true = not (cmp A, B) = !cmp A, B |
| 4325 | if (RHS == ConstantInt::getTrue() && Op0->hasOneUse()) { |
| 4326 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(Op0)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4327 | return new ICmpInst(ICI->getInversePredicate(), |
| 4328 | ICI->getOperand(0), ICI->getOperand(1)); |
Chris Lattner | ad5b4fb | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 4329 | |
Nick Lewycky | f947b3e | 2007-08-06 20:04:16 +0000 | [diff] [blame] | 4330 | if (FCmpInst *FCI = dyn_cast<FCmpInst>(Op0)) |
| 4331 | return new FCmpInst(FCI->getInversePredicate(), |
| 4332 | FCI->getOperand(0), FCI->getOperand(1)); |
| 4333 | } |
| 4334 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4335 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) { |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 4336 | // ~(c-X) == X-c-1 == X+(-c-1) |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4337 | if (Op0I->getOpcode() == Instruction::Sub && RHS->isAllOnesValue()) |
| 4338 | if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4339 | Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C); |
| 4340 | Constant *ConstantRHS = ConstantExpr::getSub(NegOp0I0C, |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4341 | ConstantInt::get(I.getType(), 1)); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4342 | return BinaryOperator::createAdd(Op0I->getOperand(1), ConstantRHS); |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4343 | } |
Chris Lattner | 5c6e2db | 2007-04-02 05:36:22 +0000 | [diff] [blame] | 4344 | |
Chris Lattner | eca0c5c | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 4345 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 4346 | if (Op0I->getOpcode() == Instruction::Add) { |
Chris Lattner | 689d24b | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 4347 | // ~(X-c) --> (-c-1)-X |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4348 | if (RHS->isAllOnesValue()) { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4349 | Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI); |
| 4350 | return BinaryOperator::createSub( |
| 4351 | ConstantExpr::getSub(NegOp0CI, |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4352 | ConstantInt::get(I.getType(), 1)), |
Chris Lattner | 689d24b | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 4353 | Op0I->getOperand(0)); |
Chris Lattner | acf4e07 | 2007-04-02 05:42:22 +0000 | [diff] [blame] | 4354 | } else if (RHS->getValue().isSignBit()) { |
Chris Lattner | 5c6e2db | 2007-04-02 05:36:22 +0000 | [diff] [blame] | 4355 | // (X + C) ^ signbit -> (X + C + signbit) |
| 4356 | Constant *C = ConstantInt::get(RHS->getValue() + Op0CI->getValue()); |
| 4357 | return BinaryOperator::createAdd(Op0I->getOperand(0), C); |
Chris Lattner | cd1d6d5 | 2007-04-02 05:48:58 +0000 | [diff] [blame] | 4358 | |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4359 | } |
Chris Lattner | 02bd1b3 | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 4360 | } else if (Op0I->getOpcode() == Instruction::Or) { |
| 4361 | // (X|C1)^C2 -> X^(C1|C2) iff X&~C1 == 0 |
Reid Spencer | a03d45f | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 4362 | if (MaskedValueIsZero(Op0I->getOperand(0), Op0CI->getValue())) { |
Chris Lattner | 02bd1b3 | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 4363 | Constant *NewRHS = ConstantExpr::getOr(Op0CI, RHS); |
| 4364 | // Anything in both C1 and C2 is known to be zero, remove it from |
| 4365 | // NewRHS. |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 4366 | Constant *CommonBits = And(Op0CI, RHS); |
Chris Lattner | 02bd1b3 | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 4367 | NewRHS = ConstantExpr::getAnd(NewRHS, |
| 4368 | ConstantExpr::getNot(CommonBits)); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 4369 | AddToWorkList(Op0I); |
Chris Lattner | 02bd1b3 | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 4370 | I.setOperand(0, Op0I->getOperand(0)); |
| 4371 | I.setOperand(1, NewRHS); |
| 4372 | return &I; |
| 4373 | } |
Chris Lattner | eca0c5c | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 4374 | } |
Chris Lattner | 05bd1b2 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 4375 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 4376 | |
| 4377 | // Try to fold constant and into select arguments. |
| 4378 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 4379 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 4380 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 4381 | if (isa<PHINode>(Op0)) |
| 4382 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 4383 | return NV; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4384 | } |
| 4385 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 4386 | if (Value *X = dyn_castNotVal(Op0)) // ~A ^ A == -1 |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4387 | if (X == Op1) |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4388 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4389 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 4390 | if (Value *X = dyn_castNotVal(Op1)) // A ^ ~A == -1 |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4391 | if (X == Op0) |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4392 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4393 | |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4394 | |
| 4395 | BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1); |
| 4396 | if (Op1I) { |
| 4397 | Value *A, *B; |
| 4398 | if (match(Op1I, m_Or(m_Value(A), m_Value(B)))) { |
| 4399 | if (A == Op0) { // B^(B|A) == (A|B)^B |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4400 | Op1I->swapOperands(); |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4401 | I.swapOperands(); |
| 4402 | std::swap(Op0, Op1); |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4403 | } else if (B == Op0) { // B^(A|B) == (A|B)^B |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4404 | I.swapOperands(); // Simplified below. |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4405 | std::swap(Op0, Op1); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4406 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4407 | } else if (match(Op1I, m_Xor(m_Value(A), m_Value(B)))) { |
| 4408 | if (Op0 == A) // A^(A^B) == B |
| 4409 | return ReplaceInstUsesWith(I, B); |
| 4410 | else if (Op0 == B) // A^(B^A) == B |
| 4411 | return ReplaceInstUsesWith(I, A); |
| 4412 | } 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] | 4413 | if (A == Op0) { // A^(A&B) -> A^(B&A) |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4414 | Op1I->swapOperands(); |
Chris Lattner | 6abbdf9 | 2007-04-01 05:36:37 +0000 | [diff] [blame] | 4415 | std::swap(A, B); |
| 4416 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4417 | if (B == Op0) { // A^(B&A) -> (B&A)^A |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4418 | I.swapOperands(); // Simplified below. |
| 4419 | std::swap(Op0, Op1); |
| 4420 | } |
Chris Lattner | 26ca7e1 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 4421 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4422 | } |
| 4423 | |
| 4424 | BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0); |
| 4425 | if (Op0I) { |
| 4426 | Value *A, *B; |
| 4427 | if (match(Op0I, m_Or(m_Value(A), m_Value(B))) && Op0I->hasOneUse()) { |
| 4428 | if (A == Op1) // (B|A)^B == (A|B)^B |
| 4429 | std::swap(A, B); |
| 4430 | if (B == Op1) { // (A|B)^B == A & ~B |
| 4431 | Instruction *NotB = |
| 4432 | InsertNewInstBefore(BinaryOperator::createNot(Op1, "tmp"), I); |
| 4433 | return BinaryOperator::createAnd(A, NotB); |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4434 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4435 | } else if (match(Op0I, m_Xor(m_Value(A), m_Value(B)))) { |
| 4436 | if (Op1 == A) // (A^B)^A == B |
| 4437 | return ReplaceInstUsesWith(I, B); |
| 4438 | else if (Op1 == B) // (B^A)^A == B |
| 4439 | return ReplaceInstUsesWith(I, A); |
| 4440 | } else if (match(Op0I, m_And(m_Value(A), m_Value(B))) && Op0I->hasOneUse()){ |
| 4441 | if (A == Op1) // (A&B)^A -> (B&A)^A |
| 4442 | std::swap(A, B); |
| 4443 | if (B == Op1 && // (B&A)^A == ~B & A |
Chris Lattner | ae1ab39 | 2006-04-01 22:05:01 +0000 | [diff] [blame] | 4444 | !isa<ConstantInt>(Op1)) { // Canonical form is (B&C)^C |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4445 | Instruction *N = |
| 4446 | InsertNewInstBefore(BinaryOperator::createNot(A, "tmp"), I); |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4447 | return BinaryOperator::createAnd(N, Op1); |
| 4448 | } |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4449 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4450 | } |
| 4451 | |
| 4452 | // (X >> Z) ^ (Y >> Z) -> (X^Y) >> Z for all shifts. |
| 4453 | if (Op0I && Op1I && Op0I->isShift() && |
| 4454 | Op0I->getOpcode() == Op1I->getOpcode() && |
| 4455 | Op0I->getOperand(1) == Op1I->getOperand(1) && |
| 4456 | (Op1I->hasOneUse() || Op1I->hasOneUse())) { |
| 4457 | Instruction *NewOp = |
| 4458 | InsertNewInstBefore(BinaryOperator::createXor(Op0I->getOperand(0), |
| 4459 | Op1I->getOperand(0), |
| 4460 | Op0I->getName()), I); |
| 4461 | return BinaryOperator::create(Op1I->getOpcode(), NewOp, |
| 4462 | Op1I->getOperand(1)); |
| 4463 | } |
| 4464 | |
| 4465 | if (Op0I && Op1I) { |
| 4466 | Value *A, *B, *C, *D; |
| 4467 | // (A & B)^(A | B) -> A ^ B |
| 4468 | if (match(Op0I, m_And(m_Value(A), m_Value(B))) && |
| 4469 | match(Op1I, m_Or(m_Value(C), m_Value(D)))) { |
| 4470 | if ((A == C && B == D) || (A == D && B == C)) |
| 4471 | return BinaryOperator::createXor(A, B); |
| 4472 | } |
| 4473 | // (A | B)^(A & B) -> A ^ B |
| 4474 | if (match(Op0I, m_Or(m_Value(A), m_Value(B))) && |
| 4475 | match(Op1I, m_And(m_Value(C), m_Value(D)))) { |
| 4476 | if ((A == C && B == D) || (A == D && B == C)) |
| 4477 | return BinaryOperator::createXor(A, B); |
| 4478 | } |
| 4479 | |
| 4480 | // (A & B)^(C & D) |
| 4481 | if ((Op0I->hasOneUse() || Op1I->hasOneUse()) && |
| 4482 | match(Op0I, m_And(m_Value(A), m_Value(B))) && |
| 4483 | match(Op1I, m_And(m_Value(C), m_Value(D)))) { |
| 4484 | // (X & Y)^(X & Y) -> (Y^Z) & X |
| 4485 | Value *X = 0, *Y = 0, *Z = 0; |
| 4486 | if (A == C) |
| 4487 | X = A, Y = B, Z = D; |
| 4488 | else if (A == D) |
| 4489 | X = A, Y = B, Z = C; |
| 4490 | else if (B == C) |
| 4491 | X = B, Y = A, Z = D; |
| 4492 | else if (B == D) |
| 4493 | X = B, Y = A, Z = C; |
| 4494 | |
| 4495 | if (X) { |
| 4496 | Instruction *NewOp = |
| 4497 | InsertNewInstBefore(BinaryOperator::createXor(Y, Z, Op0->getName()), I); |
| 4498 | return BinaryOperator::createAnd(NewOp, X); |
| 4499 | } |
| 4500 | } |
| 4501 | } |
| 4502 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4503 | // (icmp1 A, B) ^ (icmp2 A, B) --> (icmp3 A, B) |
| 4504 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1))) |
| 4505 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS))) |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 4506 | return R; |
| 4507 | |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4508 | // fold (xor (cast A), (cast B)) -> (cast (xor A, B)) |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4509 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) { |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4510 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4511 | if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind? |
| 4512 | const Type *SrcTy = Op0C->getOperand(0)->getType(); |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 4513 | if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() && |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4514 | // 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] | 4515 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), |
| 4516 | I.getType(), TD) && |
| 4517 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), |
| 4518 | I.getType(), TD)) { |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4519 | Instruction *NewOp = BinaryOperator::createXor(Op0C->getOperand(0), |
| 4520 | Op1C->getOperand(0), |
| 4521 | I.getName()); |
| 4522 | InsertNewInstBefore(NewOp, I); |
| 4523 | return CastInst::create(Op0C->getOpcode(), NewOp, I.getType()); |
| 4524 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4525 | } |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4526 | } |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4527 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4528 | } |
| 4529 | |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4530 | /// AddWithOverflow - Compute Result = In1+In2, returning true if the result |
| 4531 | /// overflowed for this type. |
| 4532 | static bool AddWithOverflow(ConstantInt *&Result, ConstantInt *In1, |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 4533 | ConstantInt *In2, bool IsSigned = false) { |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 4534 | Result = cast<ConstantInt>(Add(In1, In2)); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4535 | |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 4536 | if (IsSigned) |
| 4537 | if (In2->getValue().isNegative()) |
| 4538 | return Result->getValue().sgt(In1->getValue()); |
| 4539 | else |
| 4540 | return Result->getValue().slt(In1->getValue()); |
| 4541 | else |
| 4542 | return Result->getValue().ult(In1->getValue()); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4543 | } |
| 4544 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4545 | /// EmitGEPOffset - Given a getelementptr instruction/constantexpr, emit the |
| 4546 | /// code necessary to compute the offset from the base pointer (without adding |
| 4547 | /// in the base pointer). Return the result as a signed integer of intptr size. |
| 4548 | static Value *EmitGEPOffset(User *GEP, Instruction &I, InstCombiner &IC) { |
| 4549 | TargetData &TD = IC.getTargetData(); |
| 4550 | gep_type_iterator GTI = gep_type_begin(GEP); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4551 | const Type *IntPtrTy = TD.getIntPtrType(); |
| 4552 | Value *Result = Constant::getNullValue(IntPtrTy); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4553 | |
| 4554 | // Build a mask for high order bits. |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4555 | unsigned IntPtrWidth = TD.getPointerSize()*8; |
| 4556 | uint64_t PtrSizeMask = ~0ULL >> (64-IntPtrWidth); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4557 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4558 | for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i, ++GTI) { |
| 4559 | Value *Op = GEP->getOperand(i); |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 4560 | uint64_t Size = TD.getABITypeSize(GTI.getIndexedType()) & PtrSizeMask; |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4561 | if (ConstantInt *OpC = dyn_cast<ConstantInt>(Op)) { |
| 4562 | if (OpC->isZero()) continue; |
| 4563 | |
| 4564 | // Handle a struct index, which adds its field offset to the pointer. |
| 4565 | if (const StructType *STy = dyn_cast<StructType>(*GTI)) { |
| 4566 | Size = TD.getStructLayout(STy)->getElementOffset(OpC->getZExtValue()); |
| 4567 | |
| 4568 | if (ConstantInt *RC = dyn_cast<ConstantInt>(Result)) |
| 4569 | Result = ConstantInt::get(RC->getValue() + APInt(IntPtrWidth, Size)); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 4570 | else |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4571 | Result = IC.InsertNewInstBefore( |
| 4572 | BinaryOperator::createAdd(Result, |
| 4573 | ConstantInt::get(IntPtrTy, Size), |
| 4574 | GEP->getName()+".offs"), I); |
| 4575 | continue; |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 4576 | } |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4577 | |
| 4578 | Constant *Scale = ConstantInt::get(IntPtrTy, Size); |
| 4579 | Constant *OC = ConstantExpr::getIntegerCast(OpC, IntPtrTy, true /*SExt*/); |
| 4580 | Scale = ConstantExpr::getMul(OC, Scale); |
| 4581 | if (Constant *RC = dyn_cast<Constant>(Result)) |
| 4582 | Result = ConstantExpr::getAdd(RC, Scale); |
| 4583 | else { |
| 4584 | // Emit an add instruction. |
| 4585 | Result = IC.InsertNewInstBefore( |
| 4586 | BinaryOperator::createAdd(Result, Scale, |
| 4587 | GEP->getName()+".offs"), I); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 4588 | } |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4589 | continue; |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4590 | } |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4591 | // Convert to correct type. |
| 4592 | if (Op->getType() != IntPtrTy) { |
| 4593 | if (Constant *OpC = dyn_cast<Constant>(Op)) |
| 4594 | Op = ConstantExpr::getSExt(OpC, IntPtrTy); |
| 4595 | else |
| 4596 | Op = IC.InsertNewInstBefore(new SExtInst(Op, IntPtrTy, |
| 4597 | Op->getName()+".c"), I); |
| 4598 | } |
| 4599 | if (Size != 1) { |
| 4600 | Constant *Scale = ConstantInt::get(IntPtrTy, Size); |
| 4601 | if (Constant *OpC = dyn_cast<Constant>(Op)) |
| 4602 | Op = ConstantExpr::getMul(OpC, Scale); |
| 4603 | else // We'll let instcombine(mul) convert this to a shl if possible. |
| 4604 | Op = IC.InsertNewInstBefore(BinaryOperator::createMul(Op, Scale, |
| 4605 | GEP->getName()+".idx"), I); |
| 4606 | } |
| 4607 | |
| 4608 | // Emit an add instruction. |
| 4609 | if (isa<Constant>(Op) && isa<Constant>(Result)) |
| 4610 | Result = ConstantExpr::getAdd(cast<Constant>(Op), |
| 4611 | cast<Constant>(Result)); |
| 4612 | else |
| 4613 | Result = IC.InsertNewInstBefore(BinaryOperator::createAdd(Op, Result, |
| 4614 | GEP->getName()+".offs"), I); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4615 | } |
| 4616 | return Result; |
| 4617 | } |
| 4618 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4619 | /// FoldGEPICmp - Fold comparisons between a GEP instruction and something |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4620 | /// 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] | 4621 | Instruction *InstCombiner::FoldGEPICmp(User *GEPLHS, Value *RHS, |
| 4622 | ICmpInst::Predicate Cond, |
| 4623 | Instruction &I) { |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4624 | assert(dyn_castGetElementPtr(GEPLHS) && "LHS is not a getelementptr!"); |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4625 | |
| 4626 | if (CastInst *CI = dyn_cast<CastInst>(RHS)) |
| 4627 | if (isa<PointerType>(CI->getOperand(0)->getType())) |
| 4628 | RHS = CI->getOperand(0); |
| 4629 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4630 | Value *PtrBase = GEPLHS->getOperand(0); |
| 4631 | if (PtrBase == RHS) { |
Chris Lattner | 7c95deb | 2008-02-05 04:45:32 +0000 | [diff] [blame] | 4632 | // ((gep Ptr, OFFSET) cmp Ptr) ---> (OFFSET cmp 0). |
| 4633 | // This transformation is valid because we know pointers can't overflow. |
| 4634 | Value *Offset = EmitGEPOffset(GEPLHS, I, *this); |
| 4635 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), Offset, |
| 4636 | Constant::getNullValue(Offset->getType())); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4637 | } else if (User *GEPRHS = dyn_castGetElementPtr(RHS)) { |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 4638 | // If the base pointers are different, but the indices are the same, just |
| 4639 | // compare the base pointer. |
| 4640 | if (PtrBase != GEPRHS->getOperand(0)) { |
| 4641 | bool IndicesTheSame = GEPLHS->getNumOperands()==GEPRHS->getNumOperands(); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 4642 | IndicesTheSame &= GEPLHS->getOperand(0)->getType() == |
Chris Lattner | 93b94a6 | 2005-04-26 14:40:41 +0000 | [diff] [blame] | 4643 | GEPRHS->getOperand(0)->getType(); |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 4644 | if (IndicesTheSame) |
| 4645 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i) |
| 4646 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { |
| 4647 | IndicesTheSame = false; |
| 4648 | break; |
| 4649 | } |
| 4650 | |
| 4651 | // If all indices are the same, just compare the base pointers. |
| 4652 | if (IndicesTheSame) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4653 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), |
| 4654 | GEPLHS->getOperand(0), GEPRHS->getOperand(0)); |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 4655 | |
| 4656 | // Otherwise, the base pointers are different and the indices are |
| 4657 | // different, bail out. |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4658 | return 0; |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 4659 | } |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4660 | |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4661 | // If one of the GEPs has all zero indices, recurse. |
| 4662 | bool AllZeros = true; |
| 4663 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i) |
| 4664 | if (!isa<Constant>(GEPLHS->getOperand(i)) || |
| 4665 | !cast<Constant>(GEPLHS->getOperand(i))->isNullValue()) { |
| 4666 | AllZeros = false; |
| 4667 | break; |
| 4668 | } |
| 4669 | if (AllZeros) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4670 | return FoldGEPICmp(GEPRHS, GEPLHS->getOperand(0), |
| 4671 | ICmpInst::getSwappedPredicate(Cond), I); |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 4672 | |
| 4673 | // If the other GEP has all zero indices, recurse. |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4674 | AllZeros = true; |
| 4675 | for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i) |
| 4676 | if (!isa<Constant>(GEPRHS->getOperand(i)) || |
| 4677 | !cast<Constant>(GEPRHS->getOperand(i))->isNullValue()) { |
| 4678 | AllZeros = false; |
| 4679 | break; |
| 4680 | } |
| 4681 | if (AllZeros) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4682 | return FoldGEPICmp(GEPLHS, GEPRHS->getOperand(0), Cond, I); |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4683 | |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 4684 | if (GEPLHS->getNumOperands() == GEPRHS->getNumOperands()) { |
| 4685 | // If the GEPs only differ by one index, compare it. |
| 4686 | unsigned NumDifferences = 0; // Keep track of # differences. |
| 4687 | unsigned DiffOperand = 0; // The operand that differs. |
| 4688 | for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i) |
| 4689 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4690 | if (GEPLHS->getOperand(i)->getType()->getPrimitiveSizeInBits() != |
| 4691 | GEPRHS->getOperand(i)->getType()->getPrimitiveSizeInBits()) { |
Chris Lattner | 45f57b8 | 2005-01-21 23:06:49 +0000 | [diff] [blame] | 4692 | // Irreconcilable differences. |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 4693 | NumDifferences = 2; |
| 4694 | break; |
| 4695 | } else { |
| 4696 | if (NumDifferences++) break; |
| 4697 | DiffOperand = i; |
| 4698 | } |
| 4699 | } |
| 4700 | |
| 4701 | if (NumDifferences == 0) // SAME GEP? |
| 4702 | return ReplaceInstUsesWith(I, // No comparison is needed here. |
Nick Lewycky | 455e176 | 2007-09-06 02:40:25 +0000 | [diff] [blame] | 4703 | ConstantInt::get(Type::Int1Ty, |
| 4704 | isTrueWhenEqual(Cond))); |
| 4705 | |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 4706 | else if (NumDifferences == 1) { |
Chris Lattner | 45f57b8 | 2005-01-21 23:06:49 +0000 | [diff] [blame] | 4707 | Value *LHSV = GEPLHS->getOperand(DiffOperand); |
| 4708 | Value *RHSV = GEPRHS->getOperand(DiffOperand); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4709 | // Make sure we do a signed comparison here. |
| 4710 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), LHSV, RHSV); |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 4711 | } |
| 4712 | } |
| 4713 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4714 | // 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] | 4715 | // the result to fold to a constant! |
| 4716 | if ((isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) && |
| 4717 | (isa<ConstantExpr>(GEPRHS) || GEPRHS->hasOneUse())) { |
| 4718 | // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) ---> (OFFSET1 cmp OFFSET2) |
| 4719 | Value *L = EmitGEPOffset(GEPLHS, I, *this); |
| 4720 | Value *R = EmitGEPOffset(GEPRHS, I, *this); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4721 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), L, R); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4722 | } |
| 4723 | } |
| 4724 | return 0; |
| 4725 | } |
| 4726 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4727 | Instruction *InstCombiner::visitFCmpInst(FCmpInst &I) { |
| 4728 | bool Changed = SimplifyCompare(I); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4729 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4730 | |
Chris Lattner | 58e9746 | 2007-01-14 19:42:17 +0000 | [diff] [blame] | 4731 | // Fold trivial predicates. |
| 4732 | if (I.getPredicate() == FCmpInst::FCMP_FALSE) |
| 4733 | return ReplaceInstUsesWith(I, Constant::getNullValue(Type::Int1Ty)); |
| 4734 | if (I.getPredicate() == FCmpInst::FCMP_TRUE) |
| 4735 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 1)); |
| 4736 | |
| 4737 | // Simplify 'fcmp pred X, X' |
| 4738 | if (Op0 == Op1) { |
| 4739 | switch (I.getPredicate()) { |
| 4740 | default: assert(0 && "Unknown predicate!"); |
| 4741 | case FCmpInst::FCMP_UEQ: // True if unordered or equal |
| 4742 | case FCmpInst::FCMP_UGE: // True if unordered, greater than, or equal |
| 4743 | case FCmpInst::FCMP_ULE: // True if unordered, less than, or equal |
| 4744 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 1)); |
| 4745 | case FCmpInst::FCMP_OGT: // True if ordered and greater than |
| 4746 | case FCmpInst::FCMP_OLT: // True if ordered and less than |
| 4747 | case FCmpInst::FCMP_ONE: // True if ordered and operands are unequal |
| 4748 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 0)); |
| 4749 | |
| 4750 | case FCmpInst::FCMP_UNO: // True if unordered: isnan(X) | isnan(Y) |
| 4751 | case FCmpInst::FCMP_ULT: // True if unordered or less than |
| 4752 | case FCmpInst::FCMP_UGT: // True if unordered or greater than |
| 4753 | case FCmpInst::FCMP_UNE: // True if unordered or not equal |
| 4754 | // Canonicalize these to be 'fcmp uno %X, 0.0'. |
| 4755 | I.setPredicate(FCmpInst::FCMP_UNO); |
| 4756 | I.setOperand(1, Constant::getNullValue(Op0->getType())); |
| 4757 | return &I; |
| 4758 | |
| 4759 | case FCmpInst::FCMP_ORD: // True if ordered (no nans) |
| 4760 | case FCmpInst::FCMP_OEQ: // True if ordered and equal |
| 4761 | case FCmpInst::FCMP_OGE: // True if ordered and greater than or equal |
| 4762 | case FCmpInst::FCMP_OLE: // True if ordered and less than or equal |
| 4763 | // Canonicalize these to be 'fcmp ord %X, 0.0'. |
| 4764 | I.setPredicate(FCmpInst::FCMP_ORD); |
| 4765 | I.setOperand(1, Constant::getNullValue(Op0->getType())); |
| 4766 | return &I; |
| 4767 | } |
| 4768 | } |
| 4769 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4770 | if (isa<UndefValue>(Op1)) // fcmp pred X, undef -> undef |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 4771 | return ReplaceInstUsesWith(I, UndefValue::get(Type::Int1Ty)); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4772 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4773 | // Handle fcmp with constant RHS |
| 4774 | if (Constant *RHSC = dyn_cast<Constant>(Op1)) { |
| 4775 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
| 4776 | switch (LHSI->getOpcode()) { |
| 4777 | case Instruction::PHI: |
| 4778 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 4779 | return NV; |
| 4780 | break; |
| 4781 | case Instruction::Select: |
| 4782 | // If either operand of the select is a constant, we can fold the |
| 4783 | // comparison into the select arms, which will cause one to be |
| 4784 | // constant folded and the select turned into a bitwise or. |
| 4785 | Value *Op1 = 0, *Op2 = 0; |
| 4786 | if (LHSI->hasOneUse()) { |
| 4787 | if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) { |
| 4788 | // Fold the known value into the constant operand. |
| 4789 | Op1 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC); |
| 4790 | // Insert a new FCmp of the other select operand. |
| 4791 | Op2 = InsertNewInstBefore(new FCmpInst(I.getPredicate(), |
| 4792 | LHSI->getOperand(2), RHSC, |
| 4793 | I.getName()), I); |
| 4794 | } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) { |
| 4795 | // Fold the known value into the constant operand. |
| 4796 | Op2 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC); |
| 4797 | // Insert a new FCmp of the other select operand. |
| 4798 | Op1 = InsertNewInstBefore(new FCmpInst(I.getPredicate(), |
| 4799 | LHSI->getOperand(1), RHSC, |
| 4800 | I.getName()), I); |
| 4801 | } |
| 4802 | } |
| 4803 | |
| 4804 | if (Op1) |
| 4805 | return new SelectInst(LHSI->getOperand(0), Op1, Op2); |
| 4806 | break; |
| 4807 | } |
| 4808 | } |
| 4809 | |
| 4810 | return Changed ? &I : 0; |
| 4811 | } |
| 4812 | |
| 4813 | Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { |
| 4814 | bool Changed = SimplifyCompare(I); |
| 4815 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 4816 | const Type *Ty = Op0->getType(); |
| 4817 | |
| 4818 | // icmp X, X |
| 4819 | if (Op0 == Op1) |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4820 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
| 4821 | isTrueWhenEqual(I))); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4822 | |
| 4823 | if (isa<UndefValue>(Op1)) // X icmp undef -> undef |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 4824 | return ReplaceInstUsesWith(I, UndefValue::get(Type::Int1Ty)); |
Christopher Lamb | 7a0678c | 2007-12-18 21:32:20 +0000 | [diff] [blame] | 4825 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4826 | // icmp <global/alloca*/null>, <global/alloca*/null> - Global/Stack value |
Chris Lattner | 711b340 | 2004-11-14 07:33:16 +0000 | [diff] [blame] | 4827 | // addresses never equal each other! We already know that Op0 != Op1. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4828 | if ((isa<GlobalValue>(Op0) || isa<AllocaInst>(Op0) || |
| 4829 | isa<ConstantPointerNull>(Op0)) && |
| 4830 | (isa<GlobalValue>(Op1) || isa<AllocaInst>(Op1) || |
Chris Lattner | 711b340 | 2004-11-14 07:33:16 +0000 | [diff] [blame] | 4831 | isa<ConstantPointerNull>(Op1))) |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4832 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
| 4833 | !isTrueWhenEqual(I))); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4834 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4835 | // icmp's with boolean values can always be turned into bitwise operations |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 4836 | if (Ty == Type::Int1Ty) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4837 | switch (I.getPredicate()) { |
| 4838 | default: assert(0 && "Invalid icmp instruction!"); |
| 4839 | case ICmpInst::ICMP_EQ: { // icmp eq bool %A, %B -> ~(A^B) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4840 | Instruction *Xor = BinaryOperator::createXor(Op0, Op1, I.getName()+"tmp"); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4841 | InsertNewInstBefore(Xor, I); |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 4842 | return BinaryOperator::createNot(Xor); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4843 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4844 | case ICmpInst::ICMP_NE: // icmp eq bool %A, %B -> A^B |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 4845 | return BinaryOperator::createXor(Op0, Op1); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4846 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4847 | case ICmpInst::ICMP_UGT: |
| 4848 | case ICmpInst::ICMP_SGT: |
| 4849 | std::swap(Op0, Op1); // Change icmp gt -> icmp lt |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 4850 | // FALL THROUGH |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4851 | case ICmpInst::ICMP_ULT: |
| 4852 | case ICmpInst::ICMP_SLT: { // icmp lt bool A, B -> ~X & Y |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 4853 | Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp"); |
| 4854 | InsertNewInstBefore(Not, I); |
| 4855 | return BinaryOperator::createAnd(Not, Op1); |
| 4856 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4857 | case ICmpInst::ICMP_UGE: |
| 4858 | case ICmpInst::ICMP_SGE: |
| 4859 | std::swap(Op0, Op1); // Change icmp ge -> icmp le |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 4860 | // FALL THROUGH |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4861 | case ICmpInst::ICMP_ULE: |
| 4862 | case ICmpInst::ICMP_SLE: { // icmp le bool %A, %B -> ~A | B |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 4863 | Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp"); |
| 4864 | InsertNewInstBefore(Not, I); |
| 4865 | return BinaryOperator::createOr(Not, Op1); |
| 4866 | } |
| 4867 | } |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4868 | } |
| 4869 | |
Chris Lattner | 2be51ae | 2004-06-09 04:24:29 +0000 | [diff] [blame] | 4870 | // See if we are doing a comparison between a constant and an instruction that |
| 4871 | // can be folded into the comparison. |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4872 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Christopher Lamb | 103e1a3 | 2007-12-20 07:21:11 +0000 | [diff] [blame] | 4873 | Value *A, *B; |
| 4874 | |
Chris Lattner | b656601 | 2008-01-05 01:18:20 +0000 | [diff] [blame] | 4875 | // (icmp ne/eq (sub A B) 0) -> (icmp ne/eq A, B) |
| 4876 | if (I.isEquality() && CI->isNullValue() && |
| 4877 | match(Op0, m_Sub(m_Value(A), m_Value(B)))) { |
| 4878 | // (icmp cond A B) if cond is equality |
| 4879 | return new ICmpInst(I.getPredicate(), A, B); |
Owen Anderson | f5783f8 | 2007-12-28 07:42:12 +0000 | [diff] [blame] | 4880 | } |
Christopher Lamb | 103e1a3 | 2007-12-20 07:21:11 +0000 | [diff] [blame] | 4881 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4882 | switch (I.getPredicate()) { |
| 4883 | default: break; |
| 4884 | case ICmpInst::ICMP_ULT: // A <u MIN -> FALSE |
| 4885 | if (CI->isMinValue(false)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4886 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4887 | if (CI->isMaxValue(false)) // A <u MAX -> A != MAX |
| 4888 | return new ICmpInst(ICmpInst::ICMP_NE, Op0,Op1); |
| 4889 | if (isMinValuePlusOne(CI,false)) // A <u MIN+1 -> A == MIN |
| 4890 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, SubOne(CI)); |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 4891 | // (x <u 2147483648) -> (x >s -1) -> true if sign bit clear |
| 4892 | if (CI->isMinValue(true)) |
| 4893 | return new ICmpInst(ICmpInst::ICMP_SGT, Op0, |
| 4894 | ConstantInt::getAllOnesValue(Op0->getType())); |
| 4895 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4896 | break; |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4897 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4898 | case ICmpInst::ICMP_SLT: |
| 4899 | if (CI->isMinValue(true)) // A <s MIN -> FALSE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4900 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4901 | if (CI->isMaxValue(true)) // A <s MAX -> A != MAX |
| 4902 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
| 4903 | if (isMinValuePlusOne(CI,true)) // A <s MIN+1 -> A == MIN |
| 4904 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, SubOne(CI)); |
| 4905 | break; |
| 4906 | |
| 4907 | case ICmpInst::ICMP_UGT: |
| 4908 | if (CI->isMaxValue(false)) // A >u MAX -> FALSE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4909 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4910 | if (CI->isMinValue(false)) // A >u MIN -> A != MIN |
| 4911 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
| 4912 | if (isMaxValueMinusOne(CI, false)) // A >u MAX-1 -> A == MAX |
| 4913 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, AddOne(CI)); |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 4914 | |
| 4915 | // (x >u 2147483647) -> (x <s 0) -> true if sign bit set |
| 4916 | if (CI->isMaxValue(true)) |
| 4917 | return new ICmpInst(ICmpInst::ICMP_SLT, Op0, |
| 4918 | ConstantInt::getNullValue(Op0->getType())); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4919 | break; |
| 4920 | |
| 4921 | case ICmpInst::ICMP_SGT: |
| 4922 | if (CI->isMaxValue(true)) // A >s MAX -> FALSE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4923 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4924 | if (CI->isMinValue(true)) // A >s MIN -> A != MIN |
| 4925 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
| 4926 | if (isMaxValueMinusOne(CI, true)) // A >s MAX-1 -> A == MAX |
| 4927 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, AddOne(CI)); |
| 4928 | break; |
| 4929 | |
| 4930 | case ICmpInst::ICMP_ULE: |
| 4931 | if (CI->isMaxValue(false)) // A <=u MAX -> TRUE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4932 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4933 | if (CI->isMinValue(false)) // A <=u MIN -> A == MIN |
| 4934 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); |
| 4935 | if (isMaxValueMinusOne(CI,false)) // A <=u MAX-1 -> A != MAX |
| 4936 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, AddOne(CI)); |
| 4937 | break; |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4938 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4939 | case ICmpInst::ICMP_SLE: |
| 4940 | if (CI->isMaxValue(true)) // A <=s MAX -> TRUE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4941 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4942 | if (CI->isMinValue(true)) // A <=s MIN -> A == MIN |
| 4943 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); |
| 4944 | if (isMaxValueMinusOne(CI,true)) // A <=s MAX-1 -> A != MAX |
| 4945 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, AddOne(CI)); |
| 4946 | break; |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4947 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4948 | case ICmpInst::ICMP_UGE: |
| 4949 | if (CI->isMinValue(false)) // A >=u MIN -> TRUE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4950 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4951 | if (CI->isMaxValue(false)) // A >=u MAX -> A == MAX |
| 4952 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); |
| 4953 | if (isMinValuePlusOne(CI,false)) // A >=u MIN-1 -> A != MIN |
| 4954 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, SubOne(CI)); |
| 4955 | break; |
| 4956 | |
| 4957 | case ICmpInst::ICMP_SGE: |
| 4958 | if (CI->isMinValue(true)) // A >=s MIN -> TRUE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4959 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4960 | if (CI->isMaxValue(true)) // A >=s MAX -> A == MAX |
| 4961 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); |
| 4962 | if (isMinValuePlusOne(CI,true)) // A >=s MIN-1 -> A != MIN |
| 4963 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, SubOne(CI)); |
| 4964 | break; |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4965 | } |
| 4966 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4967 | // If we still have a icmp le or icmp ge instruction, turn it into the |
| 4968 | // appropriate icmp lt or icmp gt instruction. Since the border cases have |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4969 | // already been handled above, this requires little checking. |
| 4970 | // |
Reid Spencer | 2149a9d | 2007-03-25 19:55:33 +0000 | [diff] [blame] | 4971 | switch (I.getPredicate()) { |
Chris Lattner | 4241e4d | 2007-07-15 20:54:51 +0000 | [diff] [blame] | 4972 | default: break; |
| 4973 | case ICmpInst::ICMP_ULE: |
| 4974 | return new ICmpInst(ICmpInst::ICMP_ULT, Op0, AddOne(CI)); |
| 4975 | case ICmpInst::ICMP_SLE: |
| 4976 | return new ICmpInst(ICmpInst::ICMP_SLT, Op0, AddOne(CI)); |
| 4977 | case ICmpInst::ICMP_UGE: |
| 4978 | return new ICmpInst( ICmpInst::ICMP_UGT, Op0, SubOne(CI)); |
| 4979 | case ICmpInst::ICMP_SGE: |
| 4980 | return new ICmpInst(ICmpInst::ICMP_SGT, Op0, SubOne(CI)); |
Reid Spencer | 2149a9d | 2007-03-25 19:55:33 +0000 | [diff] [blame] | 4981 | } |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 4982 | |
| 4983 | // 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] | 4984 | // in the input. If this comparison is a normal comparison, it demands all |
| 4985 | // bits, if it is a sign bit comparison, it only demands the sign bit. |
| 4986 | |
| 4987 | bool UnusedBit; |
| 4988 | bool isSignBit = isSignBitCheck(I.getPredicate(), CI, UnusedBit); |
| 4989 | |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 4990 | uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth(); |
| 4991 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
Chris Lattner | 4241e4d | 2007-07-15 20:54:51 +0000 | [diff] [blame] | 4992 | if (SimplifyDemandedBits(Op0, |
| 4993 | isSignBit ? APInt::getSignBit(BitWidth) |
| 4994 | : APInt::getAllOnesValue(BitWidth), |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 4995 | KnownZero, KnownOne, 0)) |
| 4996 | return &I; |
| 4997 | |
| 4998 | // Given the known and unknown bits, compute a range that the LHS could be |
| 4999 | // in. |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5000 | if ((KnownOne | KnownZero) != 0) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5001 | // Compute the Min, Max and RHS values based on the known bits. For the |
| 5002 | // EQ and NE we use unsigned values. |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 5003 | APInt Min(BitWidth, 0), Max(BitWidth, 0); |
| 5004 | const APInt& RHSVal = CI->getValue(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5005 | if (ICmpInst::isSignedPredicate(I.getPredicate())) { |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5006 | ComputeSignedMinMaxValuesFromKnownBits(Ty, KnownZero, KnownOne, Min, |
| 5007 | Max); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5008 | } else { |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5009 | ComputeUnsignedMinMaxValuesFromKnownBits(Ty, KnownZero, KnownOne, Min, |
| 5010 | Max); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5011 | } |
| 5012 | switch (I.getPredicate()) { // LE/GE have been folded already. |
| 5013 | default: assert(0 && "Unknown icmp opcode!"); |
| 5014 | case ICmpInst::ICMP_EQ: |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5015 | if (Max.ult(RHSVal) || Min.ugt(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5016 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5017 | break; |
| 5018 | case ICmpInst::ICMP_NE: |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5019 | if (Max.ult(RHSVal) || Min.ugt(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5020 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5021 | break; |
| 5022 | case ICmpInst::ICMP_ULT: |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5023 | if (Max.ult(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5024 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | 81973ef | 2007-04-09 23:52:13 +0000 | [diff] [blame] | 5025 | if (Min.uge(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5026 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5027 | break; |
| 5028 | case ICmpInst::ICMP_UGT: |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5029 | if (Min.ugt(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5030 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | 81973ef | 2007-04-09 23:52:13 +0000 | [diff] [blame] | 5031 | if (Max.ule(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5032 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5033 | break; |
| 5034 | case ICmpInst::ICMP_SLT: |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5035 | if (Max.slt(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5036 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5037 | if (Min.sgt(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5038 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5039 | break; |
| 5040 | case ICmpInst::ICMP_SGT: |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5041 | if (Min.sgt(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5042 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | 81973ef | 2007-04-09 23:52:13 +0000 | [diff] [blame] | 5043 | if (Max.sle(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5044 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5045 | break; |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 5046 | } |
| 5047 | } |
| 5048 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5049 | // 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] | 5050 | // instruction, see if that instruction also has constants so that the |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5051 | // instruction can be folded into the icmp |
Chris Lattner | 3c6a0d4 | 2004-05-25 06:32:08 +0000 | [diff] [blame] | 5052 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5053 | if (Instruction *Res = visitICmpInstWithInstAndIntCst(I, LHSI, CI)) |
| 5054 | return Res; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5055 | } |
| 5056 | |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5057 | // Handle icmp with constant (but not simple integer constant) RHS |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5058 | if (Constant *RHSC = dyn_cast<Constant>(Op1)) { |
| 5059 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
| 5060 | switch (LHSI->getOpcode()) { |
Chris Lattner | 9fb25db | 2005-05-01 04:42:15 +0000 | [diff] [blame] | 5061 | case Instruction::GetElementPtr: |
| 5062 | if (RHSC->isNullValue()) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5063 | // 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] | 5064 | bool isAllZeros = true; |
| 5065 | for (unsigned i = 1, e = LHSI->getNumOperands(); i != e; ++i) |
| 5066 | if (!isa<Constant>(LHSI->getOperand(i)) || |
| 5067 | !cast<Constant>(LHSI->getOperand(i))->isNullValue()) { |
| 5068 | isAllZeros = false; |
| 5069 | break; |
| 5070 | } |
| 5071 | if (isAllZeros) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5072 | return new ICmpInst(I.getPredicate(), LHSI->getOperand(0), |
Chris Lattner | 9fb25db | 2005-05-01 04:42:15 +0000 | [diff] [blame] | 5073 | Constant::getNullValue(LHSI->getOperand(0)->getType())); |
| 5074 | } |
| 5075 | break; |
| 5076 | |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5077 | case Instruction::PHI: |
| 5078 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 5079 | return NV; |
| 5080 | break; |
Chris Lattner | 4802d90 | 2007-04-06 18:57:34 +0000 | [diff] [blame] | 5081 | case Instruction::Select: { |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5082 | // If either operand of the select is a constant, we can fold the |
| 5083 | // comparison into the select arms, which will cause one to be |
| 5084 | // constant folded and the select turned into a bitwise or. |
| 5085 | Value *Op1 = 0, *Op2 = 0; |
| 5086 | if (LHSI->hasOneUse()) { |
| 5087 | if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) { |
| 5088 | // Fold the known value into the constant operand. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5089 | Op1 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC); |
| 5090 | // Insert a new ICmp of the other select operand. |
| 5091 | Op2 = InsertNewInstBefore(new ICmpInst(I.getPredicate(), |
| 5092 | LHSI->getOperand(2), RHSC, |
| 5093 | I.getName()), I); |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5094 | } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) { |
| 5095 | // Fold the known value into the constant operand. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5096 | Op2 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC); |
| 5097 | // Insert a new ICmp of the other select operand. |
| 5098 | Op1 = InsertNewInstBefore(new ICmpInst(I.getPredicate(), |
| 5099 | LHSI->getOperand(1), RHSC, |
| 5100 | I.getName()), I); |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5101 | } |
| 5102 | } |
Jeff Cohen | 9d80930 | 2005-04-23 21:38:35 +0000 | [diff] [blame] | 5103 | |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5104 | if (Op1) |
| 5105 | return new SelectInst(LHSI->getOperand(0), Op1, Op2); |
| 5106 | break; |
| 5107 | } |
Chris Lattner | 4802d90 | 2007-04-06 18:57:34 +0000 | [diff] [blame] | 5108 | case Instruction::Malloc: |
| 5109 | // If we have (malloc != null), and if the malloc has a single use, we |
| 5110 | // can assume it is successful and remove the malloc. |
| 5111 | if (LHSI->hasOneUse() && isa<ConstantPointerNull>(RHSC)) { |
| 5112 | AddToWorkList(LHSI); |
| 5113 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
| 5114 | !isTrueWhenEqual(I))); |
| 5115 | } |
| 5116 | break; |
| 5117 | } |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5118 | } |
| 5119 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5120 | // 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] | 5121 | if (User *GEP = dyn_castGetElementPtr(Op0)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5122 | if (Instruction *NI = FoldGEPICmp(GEP, Op1, I.getPredicate(), I)) |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5123 | return NI; |
| 5124 | if (User *GEP = dyn_castGetElementPtr(Op1)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5125 | if (Instruction *NI = FoldGEPICmp(GEP, Op0, |
| 5126 | ICmpInst::getSwappedPredicate(I.getPredicate()), I)) |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5127 | return NI; |
| 5128 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5129 | // 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] | 5130 | // values. If the ptr->ptr cast can be stripped off both arguments, we do so |
| 5131 | // now. |
| 5132 | if (BitCastInst *CI = dyn_cast<BitCastInst>(Op0)) { |
| 5133 | if (isa<PointerType>(Op0->getType()) && |
| 5134 | (isa<Constant>(Op1) || isa<BitCastInst>(Op1))) { |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5135 | // We keep moving the cast from the left operand over to the right |
| 5136 | // operand, where it can often be eliminated completely. |
Chris Lattner | 57d8637 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 5137 | Op0 = CI->getOperand(0); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5138 | |
Chris Lattner | 57d8637 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 5139 | // If operand #1 is a bitcast instruction, it must also be a ptr->ptr cast |
| 5140 | // so eliminate it as well. |
| 5141 | if (BitCastInst *CI2 = dyn_cast<BitCastInst>(Op1)) |
| 5142 | Op1 = CI2->getOperand(0); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5143 | |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5144 | // If Op1 is a constant, we can fold the cast into the constant. |
Chris Lattner | 57d8637 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 5145 | if (Op0->getType() != Op1->getType()) |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5146 | if (Constant *Op1C = dyn_cast<Constant>(Op1)) { |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 5147 | Op1 = ConstantExpr::getBitCast(Op1C, Op0->getType()); |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5148 | } else { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5149 | // Otherwise, cast the RHS right before the icmp |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 5150 | Op1 = InsertBitCastBefore(Op1, Op0->getType(), I); |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5151 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5152 | return new ICmpInst(I.getPredicate(), Op0, Op1); |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5153 | } |
Chris Lattner | 57d8637 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 5154 | } |
| 5155 | |
| 5156 | if (isa<CastInst>(Op0)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5157 | // Handle the special case of: icmp (cast bool to X), <cst> |
Chris Lattner | 6870805 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 5158 | // This comes up when you have code like |
| 5159 | // int X = A < B; |
| 5160 | // if (X) ... |
| 5161 | // For generality, we handle any zero-extension of any operand comparison |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5162 | // with a constant or another cast from the same type. |
| 5163 | if (isa<ConstantInt>(Op1) || isa<CastInst>(Op1)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5164 | if (Instruction *R = visitICmpInstWithCastAndCast(I)) |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5165 | return R; |
Chris Lattner | 6870805 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 5166 | } |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5167 | |
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 5168 | if (I.isEquality()) { |
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5169 | Value *A, *B, *C, *D; |
| 5170 | if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) { |
| 5171 | if (A == Op1 || B == Op1) { // (A^B) == A -> B == 0 |
| 5172 | Value *OtherVal = A == Op1 ? B : A; |
| 5173 | return new ICmpInst(I.getPredicate(), OtherVal, |
| 5174 | Constant::getNullValue(A->getType())); |
| 5175 | } |
| 5176 | |
| 5177 | if (match(Op1, m_Xor(m_Value(C), m_Value(D)))) { |
| 5178 | // A^c1 == C^c2 --> A == C^(c1^c2) |
| 5179 | if (ConstantInt *C1 = dyn_cast<ConstantInt>(B)) |
| 5180 | if (ConstantInt *C2 = dyn_cast<ConstantInt>(D)) |
| 5181 | if (Op1->hasOneUse()) { |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 5182 | Constant *NC = ConstantInt::get(C1->getValue() ^ C2->getValue()); |
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5183 | Instruction *Xor = BinaryOperator::createXor(C, NC, "tmp"); |
| 5184 | return new ICmpInst(I.getPredicate(), A, |
| 5185 | InsertNewInstBefore(Xor, I)); |
| 5186 | } |
| 5187 | |
| 5188 | // A^B == A^D -> B == D |
| 5189 | if (A == C) return new ICmpInst(I.getPredicate(), B, D); |
| 5190 | if (A == D) return new ICmpInst(I.getPredicate(), B, C); |
| 5191 | if (B == C) return new ICmpInst(I.getPredicate(), A, D); |
| 5192 | if (B == D) return new ICmpInst(I.getPredicate(), A, C); |
| 5193 | } |
| 5194 | } |
| 5195 | |
| 5196 | if (match(Op1, m_Xor(m_Value(A), m_Value(B))) && |
| 5197 | (A == Op0 || B == Op0)) { |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5198 | // A == (A^B) -> B == 0 |
| 5199 | Value *OtherVal = A == Op0 ? B : A; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5200 | return new ICmpInst(I.getPredicate(), OtherVal, |
| 5201 | Constant::getNullValue(A->getType())); |
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5202 | } |
| 5203 | 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] | 5204 | // (A-B) == A -> B == 0 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5205 | return new ICmpInst(I.getPredicate(), B, |
| 5206 | Constant::getNullValue(B->getType())); |
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5207 | } |
| 5208 | 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] | 5209 | // A == (A-B) -> B == 0 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5210 | return new ICmpInst(I.getPredicate(), B, |
| 5211 | Constant::getNullValue(B->getType())); |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5212 | } |
Chris Lattner | 9c2328e | 2006-11-14 06:06:06 +0000 | [diff] [blame] | 5213 | |
Chris Lattner | 9c2328e | 2006-11-14 06:06:06 +0000 | [diff] [blame] | 5214 | // (X&Z) == (Y&Z) -> (X^Y) & Z == 0 |
| 5215 | if (Op0->hasOneUse() && Op1->hasOneUse() && |
| 5216 | match(Op0, m_And(m_Value(A), m_Value(B))) && |
| 5217 | match(Op1, m_And(m_Value(C), m_Value(D)))) { |
| 5218 | Value *X = 0, *Y = 0, *Z = 0; |
| 5219 | |
| 5220 | if (A == C) { |
| 5221 | X = B; Y = D; Z = A; |
| 5222 | } else if (A == D) { |
| 5223 | X = B; Y = C; Z = A; |
| 5224 | } else if (B == C) { |
| 5225 | X = A; Y = D; Z = B; |
| 5226 | } else if (B == D) { |
| 5227 | X = A; Y = C; Z = B; |
| 5228 | } |
| 5229 | |
| 5230 | if (X) { // Build (X^Y) & Z |
| 5231 | Op1 = InsertNewInstBefore(BinaryOperator::createXor(X, Y, "tmp"), I); |
| 5232 | Op1 = InsertNewInstBefore(BinaryOperator::createAnd(Op1, Z, "tmp"), I); |
| 5233 | I.setOperand(0, Op1); |
| 5234 | I.setOperand(1, Constant::getNullValue(Op1->getType())); |
| 5235 | return &I; |
| 5236 | } |
| 5237 | } |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5238 | } |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 5239 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5240 | } |
| 5241 | |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5242 | |
| 5243 | /// FoldICmpDivCst - Fold "icmp pred, ([su]div X, DivRHS), CmpRHS" where DivRHS |
| 5244 | /// and CmpRHS are both known to be integer constants. |
| 5245 | Instruction *InstCombiner::FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI, |
| 5246 | ConstantInt *DivRHS) { |
| 5247 | ConstantInt *CmpRHS = cast<ConstantInt>(ICI.getOperand(1)); |
| 5248 | const APInt &CmpRHSV = CmpRHS->getValue(); |
| 5249 | |
| 5250 | // FIXME: If the operand types don't match the type of the divide |
| 5251 | // then don't attempt this transform. The code below doesn't have the |
| 5252 | // logic to deal with a signed divide and an unsigned compare (and |
| 5253 | // vice versa). This is because (x /s C1) <s C2 produces different |
| 5254 | // results than (x /s C1) <u C2 or (x /u C1) <s C2 or even |
| 5255 | // (x /u C1) <u C2. Simply casting the operands and result won't |
| 5256 | // work. :( The if statement below tests that condition and bails |
| 5257 | // if it finds it. |
| 5258 | bool DivIsSigned = DivI->getOpcode() == Instruction::SDiv; |
| 5259 | if (!ICI.isEquality() && DivIsSigned != ICI.isSignedPredicate()) |
| 5260 | return 0; |
| 5261 | if (DivRHS->isZero()) |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5262 | return 0; // The ProdOV computation fails on divide by zero. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5263 | |
| 5264 | // Compute Prod = CI * DivRHS. We are essentially solving an equation |
| 5265 | // of form X/C1=C2. We solve for X by multiplying C1 (DivRHS) and |
| 5266 | // C2 (CI). By solving for X we can turn this into a range check |
| 5267 | // instead of computing a divide. |
| 5268 | ConstantInt *Prod = Multiply(CmpRHS, DivRHS); |
| 5269 | |
| 5270 | // Determine if the product overflows by seeing if the product is |
| 5271 | // not equal to the divide. Make sure we do the same kind of divide |
| 5272 | // as in the LHS instruction that we're folding. |
| 5273 | bool ProdOV = (DivIsSigned ? ConstantExpr::getSDiv(Prod, DivRHS) : |
| 5274 | ConstantExpr::getUDiv(Prod, DivRHS)) != CmpRHS; |
| 5275 | |
| 5276 | // Get the ICmp opcode |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5277 | ICmpInst::Predicate Pred = ICI.getPredicate(); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5278 | |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5279 | // Figure out the interval that is being checked. For example, a comparison |
| 5280 | // like "X /u 5 == 0" is really checking that X is in the interval [0, 5). |
| 5281 | // Compute this interval based on the constants involved and the signedness of |
| 5282 | // the compare/divide. This computes a half-open interval, keeping track of |
| 5283 | // whether either value in the interval overflows. After analysis each |
| 5284 | // overflow variable is set to 0 if it's corresponding bound variable is valid |
| 5285 | // -1 if overflowed off the bottom end, or +1 if overflowed off the top end. |
| 5286 | int LoOverflow = 0, HiOverflow = 0; |
| 5287 | ConstantInt *LoBound = 0, *HiBound = 0; |
| 5288 | |
| 5289 | |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5290 | if (!DivIsSigned) { // udiv |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5291 | // e.g. X/5 op 3 --> [15, 20) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5292 | LoBound = Prod; |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5293 | HiOverflow = LoOverflow = ProdOV; |
| 5294 | if (!HiOverflow) |
| 5295 | HiOverflow = AddWithOverflow(HiBound, LoBound, DivRHS, false); |
Dan Gohman | 7649127 | 2008-02-13 22:09:18 +0000 | [diff] [blame^] | 5296 | } else if (DivRHS->getValue().isStrictlyPositive()) { // Divisor is > 0. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5297 | if (CmpRHSV == 0) { // (X / pos) op 0 |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5298 | // Can't overflow. e.g. X/2 op 0 --> [-1, 2) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5299 | LoBound = cast<ConstantInt>(ConstantExpr::getNeg(SubOne(DivRHS))); |
| 5300 | HiBound = DivRHS; |
Dan Gohman | 7649127 | 2008-02-13 22:09:18 +0000 | [diff] [blame^] | 5301 | } else if (CmpRHSV.isStrictlyPositive()) { // (X / pos) op pos |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5302 | LoBound = Prod; // e.g. X/5 op 3 --> [15, 20) |
| 5303 | HiOverflow = LoOverflow = ProdOV; |
| 5304 | if (!HiOverflow) |
| 5305 | HiOverflow = AddWithOverflow(HiBound, Prod, DivRHS, true); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5306 | } else { // (X / pos) op neg |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5307 | // e.g. X/5 op -3 --> [-15-4, -15+1) --> [-19, -14) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5308 | Constant *DivRHSH = ConstantExpr::getNeg(SubOne(DivRHS)); |
| 5309 | LoOverflow = AddWithOverflow(LoBound, Prod, |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5310 | cast<ConstantInt>(DivRHSH), true) ? -1 : 0; |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5311 | HiBound = AddOne(Prod); |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5312 | HiOverflow = ProdOV ? -1 : 0; |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5313 | } |
Dan Gohman | 7649127 | 2008-02-13 22:09:18 +0000 | [diff] [blame^] | 5314 | } else if (DivRHS->getValue().isNegative()) { // Divisor is < 0. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5315 | if (CmpRHSV == 0) { // (X / neg) op 0 |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5316 | // e.g. X/-5 op 0 --> [-4, 5) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5317 | LoBound = AddOne(DivRHS); |
| 5318 | HiBound = cast<ConstantInt>(ConstantExpr::getNeg(DivRHS)); |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5319 | if (HiBound == DivRHS) { // -INTMIN = INTMIN |
| 5320 | HiOverflow = 1; // [INTMIN+1, overflow) |
| 5321 | HiBound = 0; // e.g. X/INTMIN = 0 --> X > INTMIN |
| 5322 | } |
Dan Gohman | 7649127 | 2008-02-13 22:09:18 +0000 | [diff] [blame^] | 5323 | } else if (CmpRHSV.isStrictlyPositive()) { // (X / neg) op pos |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5324 | // e.g. X/-5 op 3 --> [-19, -14) |
| 5325 | HiOverflow = LoOverflow = ProdOV ? -1 : 0; |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5326 | if (!LoOverflow) |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5327 | LoOverflow = AddWithOverflow(LoBound, Prod, AddOne(DivRHS), true) ?-1:0; |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5328 | HiBound = AddOne(Prod); |
| 5329 | } else { // (X / neg) op neg |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5330 | // e.g. X/-5 op -3 --> [15, 20) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5331 | LoBound = Prod; |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5332 | LoOverflow = HiOverflow = ProdOV ? 1 : 0; |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5333 | HiBound = Subtract(Prod, DivRHS); |
| 5334 | } |
| 5335 | |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5336 | // Dividing by a negative swaps the condition. LT <-> GT |
| 5337 | Pred = ICmpInst::getSwappedPredicate(Pred); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5338 | } |
| 5339 | |
| 5340 | Value *X = DivI->getOperand(0); |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5341 | switch (Pred) { |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5342 | default: assert(0 && "Unhandled icmp opcode!"); |
| 5343 | case ICmpInst::ICMP_EQ: |
| 5344 | if (LoOverflow && HiOverflow) |
| 5345 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
| 5346 | else if (HiOverflow) |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5347 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5348 | ICmpInst::ICMP_UGE, X, LoBound); |
| 5349 | else if (LoOverflow) |
| 5350 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : |
| 5351 | ICmpInst::ICMP_ULT, X, HiBound); |
| 5352 | else |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5353 | return InsertRangeTest(X, LoBound, HiBound, DivIsSigned, true, ICI); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5354 | case ICmpInst::ICMP_NE: |
| 5355 | if (LoOverflow && HiOverflow) |
| 5356 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); |
| 5357 | else if (HiOverflow) |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5358 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5359 | ICmpInst::ICMP_ULT, X, LoBound); |
| 5360 | else if (LoOverflow) |
| 5361 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : |
| 5362 | ICmpInst::ICMP_UGE, X, HiBound); |
| 5363 | else |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5364 | return InsertRangeTest(X, LoBound, HiBound, DivIsSigned, false, ICI); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5365 | case ICmpInst::ICMP_ULT: |
| 5366 | case ICmpInst::ICMP_SLT: |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5367 | if (LoOverflow == +1) // Low bound is greater than input range. |
| 5368 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); |
| 5369 | if (LoOverflow == -1) // Low bound is less than input range. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5370 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5371 | return new ICmpInst(Pred, X, LoBound); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5372 | case ICmpInst::ICMP_UGT: |
| 5373 | case ICmpInst::ICMP_SGT: |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5374 | if (HiOverflow == +1) // High bound greater than input range. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5375 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5376 | else if (HiOverflow == -1) // High bound less than input range. |
| 5377 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); |
| 5378 | if (Pred == ICmpInst::ICMP_UGT) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5379 | return new ICmpInst(ICmpInst::ICMP_UGE, X, HiBound); |
| 5380 | else |
| 5381 | return new ICmpInst(ICmpInst::ICMP_SGE, X, HiBound); |
| 5382 | } |
| 5383 | } |
| 5384 | |
| 5385 | |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5386 | /// visitICmpInstWithInstAndIntCst - Handle "icmp (instr, intcst)". |
| 5387 | /// |
| 5388 | Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI, |
| 5389 | Instruction *LHSI, |
| 5390 | ConstantInt *RHS) { |
| 5391 | const APInt &RHSV = RHS->getValue(); |
| 5392 | |
| 5393 | switch (LHSI->getOpcode()) { |
Duncan Sands | 0091bf2 | 2007-04-04 06:42:45 +0000 | [diff] [blame] | 5394 | case Instruction::Xor: // (icmp pred (xor X, XorCST), CI) |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5395 | if (ConstantInt *XorCST = dyn_cast<ConstantInt>(LHSI->getOperand(1))) { |
| 5396 | // If this is a comparison that tests the signbit (X < 0) or (x > -1), |
| 5397 | // fold the xor. |
| 5398 | if (ICI.getPredicate() == ICmpInst::ICMP_SLT && RHSV == 0 || |
| 5399 | ICI.getPredicate() == ICmpInst::ICMP_SGT && RHSV.isAllOnesValue()) { |
| 5400 | Value *CompareVal = LHSI->getOperand(0); |
| 5401 | |
| 5402 | // If the sign bit of the XorCST is not set, there is no change to |
| 5403 | // the operation, just stop using the Xor. |
| 5404 | if (!XorCST->getValue().isNegative()) { |
| 5405 | ICI.setOperand(0, CompareVal); |
| 5406 | AddToWorkList(LHSI); |
| 5407 | return &ICI; |
| 5408 | } |
| 5409 | |
| 5410 | // Was the old condition true if the operand is positive? |
| 5411 | bool isTrueIfPositive = ICI.getPredicate() == ICmpInst::ICMP_SGT; |
| 5412 | |
| 5413 | // If so, the new one isn't. |
| 5414 | isTrueIfPositive ^= true; |
| 5415 | |
| 5416 | if (isTrueIfPositive) |
| 5417 | return new ICmpInst(ICmpInst::ICMP_SGT, CompareVal, SubOne(RHS)); |
| 5418 | else |
| 5419 | return new ICmpInst(ICmpInst::ICMP_SLT, CompareVal, AddOne(RHS)); |
| 5420 | } |
| 5421 | } |
| 5422 | break; |
| 5423 | case Instruction::And: // (icmp pred (and X, AndCST), RHS) |
| 5424 | if (LHSI->hasOneUse() && isa<ConstantInt>(LHSI->getOperand(1)) && |
| 5425 | LHSI->getOperand(0)->hasOneUse()) { |
| 5426 | ConstantInt *AndCST = cast<ConstantInt>(LHSI->getOperand(1)); |
| 5427 | |
| 5428 | // If the LHS is an AND of a truncating cast, we can widen the |
| 5429 | // and/compare to be the input width without changing the value |
| 5430 | // produced, eliminating a cast. |
| 5431 | if (TruncInst *Cast = dyn_cast<TruncInst>(LHSI->getOperand(0))) { |
| 5432 | // We can do this transformation if either the AND constant does not |
| 5433 | // have its sign bit set or if it is an equality comparison. |
| 5434 | // Extending a relational comparison when we're checking the sign |
| 5435 | // bit would not work. |
| 5436 | if (Cast->hasOneUse() && |
Dan Gohman | 7649127 | 2008-02-13 22:09:18 +0000 | [diff] [blame^] | 5437 | (ICI.isEquality() || AndCST->getValue().isNonNegative() && |
| 5438 | RHSV.isNonNegative())) { |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5439 | uint32_t BitWidth = |
| 5440 | cast<IntegerType>(Cast->getOperand(0)->getType())->getBitWidth(); |
| 5441 | APInt NewCST = AndCST->getValue(); |
| 5442 | NewCST.zext(BitWidth); |
| 5443 | APInt NewCI = RHSV; |
| 5444 | NewCI.zext(BitWidth); |
| 5445 | Instruction *NewAnd = |
| 5446 | BinaryOperator::createAnd(Cast->getOperand(0), |
| 5447 | ConstantInt::get(NewCST),LHSI->getName()); |
| 5448 | InsertNewInstBefore(NewAnd, ICI); |
| 5449 | return new ICmpInst(ICI.getPredicate(), NewAnd, |
| 5450 | ConstantInt::get(NewCI)); |
| 5451 | } |
| 5452 | } |
| 5453 | |
| 5454 | // If this is: (X >> C1) & C2 != C3 (where any shift and any compare |
| 5455 | // could exist), turn it into (X & (C2 << C1)) != (C3 << C1). This |
| 5456 | // happens a LOT in code produced by the C front-end, for bitfield |
| 5457 | // access. |
| 5458 | BinaryOperator *Shift = dyn_cast<BinaryOperator>(LHSI->getOperand(0)); |
| 5459 | if (Shift && !Shift->isShift()) |
| 5460 | Shift = 0; |
| 5461 | |
| 5462 | ConstantInt *ShAmt; |
| 5463 | ShAmt = Shift ? dyn_cast<ConstantInt>(Shift->getOperand(1)) : 0; |
| 5464 | const Type *Ty = Shift ? Shift->getType() : 0; // Type of the shift. |
| 5465 | const Type *AndTy = AndCST->getType(); // Type of the and. |
| 5466 | |
| 5467 | // We can fold this as long as we can't shift unknown bits |
| 5468 | // into the mask. This can only happen with signed shift |
| 5469 | // rights, as they sign-extend. |
| 5470 | if (ShAmt) { |
| 5471 | bool CanFold = Shift->isLogicalShift(); |
| 5472 | if (!CanFold) { |
| 5473 | // To test for the bad case of the signed shr, see if any |
| 5474 | // of the bits shifted in could be tested after the mask. |
| 5475 | uint32_t TyBits = Ty->getPrimitiveSizeInBits(); |
| 5476 | int ShAmtVal = TyBits - ShAmt->getLimitedValue(TyBits); |
| 5477 | |
| 5478 | uint32_t BitWidth = AndTy->getPrimitiveSizeInBits(); |
| 5479 | if ((APInt::getHighBitsSet(BitWidth, BitWidth-ShAmtVal) & |
| 5480 | AndCST->getValue()) == 0) |
| 5481 | CanFold = true; |
| 5482 | } |
| 5483 | |
| 5484 | if (CanFold) { |
| 5485 | Constant *NewCst; |
| 5486 | if (Shift->getOpcode() == Instruction::Shl) |
| 5487 | NewCst = ConstantExpr::getLShr(RHS, ShAmt); |
| 5488 | else |
| 5489 | NewCst = ConstantExpr::getShl(RHS, ShAmt); |
| 5490 | |
| 5491 | // Check to see if we are shifting out any of the bits being |
| 5492 | // compared. |
| 5493 | if (ConstantExpr::get(Shift->getOpcode(), NewCst, ShAmt) != RHS) { |
| 5494 | // If we shifted bits out, the fold is not going to work out. |
| 5495 | // As a special case, check to see if this means that the |
| 5496 | // result is always true or false now. |
| 5497 | if (ICI.getPredicate() == ICmpInst::ICMP_EQ) |
| 5498 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
| 5499 | if (ICI.getPredicate() == ICmpInst::ICMP_NE) |
| 5500 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); |
| 5501 | } else { |
| 5502 | ICI.setOperand(1, NewCst); |
| 5503 | Constant *NewAndCST; |
| 5504 | if (Shift->getOpcode() == Instruction::Shl) |
| 5505 | NewAndCST = ConstantExpr::getLShr(AndCST, ShAmt); |
| 5506 | else |
| 5507 | NewAndCST = ConstantExpr::getShl(AndCST, ShAmt); |
| 5508 | LHSI->setOperand(1, NewAndCST); |
| 5509 | LHSI->setOperand(0, Shift->getOperand(0)); |
| 5510 | AddToWorkList(Shift); // Shift is dead. |
| 5511 | AddUsesToWorkList(ICI); |
| 5512 | return &ICI; |
| 5513 | } |
| 5514 | } |
| 5515 | } |
| 5516 | |
| 5517 | // Turn ((X >> Y) & C) == 0 into (X & (C << Y)) == 0. The later is |
| 5518 | // preferable because it allows the C<<Y expression to be hoisted out |
| 5519 | // of a loop if Y is invariant and X is not. |
| 5520 | if (Shift && Shift->hasOneUse() && RHSV == 0 && |
| 5521 | ICI.isEquality() && !Shift->isArithmeticShift() && |
| 5522 | isa<Instruction>(Shift->getOperand(0))) { |
| 5523 | // Compute C << Y. |
| 5524 | Value *NS; |
| 5525 | if (Shift->getOpcode() == Instruction::LShr) { |
| 5526 | NS = BinaryOperator::createShl(AndCST, |
| 5527 | Shift->getOperand(1), "tmp"); |
| 5528 | } else { |
| 5529 | // Insert a logical shift. |
| 5530 | NS = BinaryOperator::createLShr(AndCST, |
| 5531 | Shift->getOperand(1), "tmp"); |
| 5532 | } |
| 5533 | InsertNewInstBefore(cast<Instruction>(NS), ICI); |
| 5534 | |
| 5535 | // Compute X & (C << Y). |
| 5536 | Instruction *NewAnd = |
| 5537 | BinaryOperator::createAnd(Shift->getOperand(0), NS, LHSI->getName()); |
| 5538 | InsertNewInstBefore(NewAnd, ICI); |
| 5539 | |
| 5540 | ICI.setOperand(0, NewAnd); |
| 5541 | return &ICI; |
| 5542 | } |
| 5543 | } |
| 5544 | break; |
| 5545 | |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 5546 | case Instruction::Shl: { // (icmp pred (shl X, ShAmt), CI) |
| 5547 | ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1)); |
| 5548 | if (!ShAmt) break; |
| 5549 | |
| 5550 | uint32_t TypeBits = RHSV.getBitWidth(); |
| 5551 | |
| 5552 | // Check that the shift amount is in range. If not, don't perform |
| 5553 | // undefined shifts. When the shift is visited it will be |
| 5554 | // simplified. |
| 5555 | if (ShAmt->uge(TypeBits)) |
| 5556 | break; |
| 5557 | |
| 5558 | if (ICI.isEquality()) { |
| 5559 | // If we are comparing against bits always shifted out, the |
| 5560 | // comparison cannot succeed. |
| 5561 | Constant *Comp = |
| 5562 | ConstantExpr::getShl(ConstantExpr::getLShr(RHS, ShAmt), ShAmt); |
| 5563 | if (Comp != RHS) {// Comparing against a bit that we know is zero. |
| 5564 | bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE; |
| 5565 | Constant *Cst = ConstantInt::get(Type::Int1Ty, IsICMP_NE); |
| 5566 | return ReplaceInstUsesWith(ICI, Cst); |
| 5567 | } |
| 5568 | |
| 5569 | if (LHSI->hasOneUse()) { |
| 5570 | // Otherwise strength reduce the shift into an and. |
| 5571 | uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits); |
| 5572 | Constant *Mask = |
| 5573 | ConstantInt::get(APInt::getLowBitsSet(TypeBits, TypeBits-ShAmtVal)); |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5574 | |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 5575 | Instruction *AndI = |
| 5576 | BinaryOperator::createAnd(LHSI->getOperand(0), |
| 5577 | Mask, LHSI->getName()+".mask"); |
| 5578 | Value *And = InsertNewInstBefore(AndI, ICI); |
| 5579 | return new ICmpInst(ICI.getPredicate(), And, |
| 5580 | ConstantInt::get(RHSV.lshr(ShAmtVal))); |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5581 | } |
| 5582 | } |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 5583 | |
| 5584 | // Otherwise, if this is a comparison of the sign bit, simplify to and/test. |
| 5585 | bool TrueIfSigned = false; |
| 5586 | if (LHSI->hasOneUse() && |
| 5587 | isSignBitCheck(ICI.getPredicate(), RHS, TrueIfSigned)) { |
| 5588 | // (X << 31) <s 0 --> (X&1) != 0 |
| 5589 | Constant *Mask = ConstantInt::get(APInt(TypeBits, 1) << |
| 5590 | (TypeBits-ShAmt->getZExtValue()-1)); |
| 5591 | Instruction *AndI = |
| 5592 | BinaryOperator::createAnd(LHSI->getOperand(0), |
| 5593 | Mask, LHSI->getName()+".mask"); |
| 5594 | Value *And = InsertNewInstBefore(AndI, ICI); |
| 5595 | |
| 5596 | return new ICmpInst(TrueIfSigned ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ, |
| 5597 | And, Constant::getNullValue(And->getType())); |
| 5598 | } |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5599 | break; |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 5600 | } |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5601 | |
| 5602 | case Instruction::LShr: // (icmp pred (shr X, ShAmt), CI) |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 5603 | case Instruction::AShr: { |
| 5604 | ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1)); |
| 5605 | if (!ShAmt) break; |
| 5606 | |
| 5607 | if (ICI.isEquality()) { |
| 5608 | // Check that the shift amount is in range. If not, don't perform |
| 5609 | // undefined shifts. When the shift is visited it will be |
| 5610 | // simplified. |
| 5611 | uint32_t TypeBits = RHSV.getBitWidth(); |
| 5612 | if (ShAmt->uge(TypeBits)) |
| 5613 | break; |
| 5614 | uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits); |
| 5615 | |
| 5616 | // If we are comparing against bits always shifted out, the |
| 5617 | // comparison cannot succeed. |
| 5618 | APInt Comp = RHSV << ShAmtVal; |
| 5619 | if (LHSI->getOpcode() == Instruction::LShr) |
| 5620 | Comp = Comp.lshr(ShAmtVal); |
| 5621 | else |
| 5622 | Comp = Comp.ashr(ShAmtVal); |
| 5623 | |
| 5624 | if (Comp != RHSV) { // Comparing against a bit that we know is zero. |
| 5625 | bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE; |
| 5626 | Constant *Cst = ConstantInt::get(Type::Int1Ty, IsICMP_NE); |
| 5627 | return ReplaceInstUsesWith(ICI, Cst); |
| 5628 | } |
| 5629 | |
| 5630 | if (LHSI->hasOneUse() || RHSV == 0) { |
| 5631 | // Otherwise strength reduce the shift into an and. |
| 5632 | APInt Val(APInt::getHighBitsSet(TypeBits, TypeBits - ShAmtVal)); |
| 5633 | Constant *Mask = ConstantInt::get(Val); |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5634 | |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 5635 | Instruction *AndI = |
| 5636 | BinaryOperator::createAnd(LHSI->getOperand(0), |
| 5637 | Mask, LHSI->getName()+".mask"); |
| 5638 | Value *And = InsertNewInstBefore(AndI, ICI); |
| 5639 | return new ICmpInst(ICI.getPredicate(), And, |
| 5640 | ConstantExpr::getShl(RHS, ShAmt)); |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5641 | } |
| 5642 | } |
| 5643 | break; |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 5644 | } |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5645 | |
| 5646 | case Instruction::SDiv: |
| 5647 | case Instruction::UDiv: |
| 5648 | // Fold: icmp pred ([us]div X, C1), C2 -> range test |
| 5649 | // Fold this div into the comparison, producing a range check. |
| 5650 | // Determine, based on the divide type, what the range is being |
| 5651 | // checked. If there is an overflow on the low or high side, remember |
| 5652 | // it, otherwise compute the range [low, hi) bounding the new value. |
| 5653 | // See: InsertRangeTest above for the kinds of replacements possible. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5654 | if (ConstantInt *DivRHS = dyn_cast<ConstantInt>(LHSI->getOperand(1))) |
| 5655 | if (Instruction *R = FoldICmpDivCst(ICI, cast<BinaryOperator>(LHSI), |
| 5656 | DivRHS)) |
| 5657 | return R; |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5658 | break; |
Nick Lewycky | 5be2920 | 2008-02-03 16:33:09 +0000 | [diff] [blame] | 5659 | |
| 5660 | case Instruction::Add: |
| 5661 | // Fold: icmp pred (add, X, C1), C2 |
| 5662 | |
| 5663 | if (!ICI.isEquality()) { |
| 5664 | ConstantInt *LHSC = dyn_cast<ConstantInt>(LHSI->getOperand(1)); |
| 5665 | if (!LHSC) break; |
| 5666 | const APInt &LHSV = LHSC->getValue(); |
| 5667 | |
| 5668 | ConstantRange CR = ICI.makeConstantRange(ICI.getPredicate(), RHSV) |
| 5669 | .subtract(LHSV); |
| 5670 | |
| 5671 | if (ICI.isSignedPredicate()) { |
| 5672 | if (CR.getLower().isSignBit()) { |
| 5673 | return new ICmpInst(ICmpInst::ICMP_SLT, LHSI->getOperand(0), |
| 5674 | ConstantInt::get(CR.getUpper())); |
| 5675 | } else if (CR.getUpper().isSignBit()) { |
| 5676 | return new ICmpInst(ICmpInst::ICMP_SGE, LHSI->getOperand(0), |
| 5677 | ConstantInt::get(CR.getLower())); |
| 5678 | } |
| 5679 | } else { |
| 5680 | if (CR.getLower().isMinValue()) { |
| 5681 | return new ICmpInst(ICmpInst::ICMP_ULT, LHSI->getOperand(0), |
| 5682 | ConstantInt::get(CR.getUpper())); |
| 5683 | } else if (CR.getUpper().isMinValue()) { |
| 5684 | return new ICmpInst(ICmpInst::ICMP_UGE, LHSI->getOperand(0), |
| 5685 | ConstantInt::get(CR.getLower())); |
| 5686 | } |
| 5687 | } |
| 5688 | } |
| 5689 | break; |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5690 | } |
| 5691 | |
| 5692 | // Simplify icmp_eq and icmp_ne instructions with integer constant RHS. |
| 5693 | if (ICI.isEquality()) { |
| 5694 | bool isICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE; |
| 5695 | |
| 5696 | // If the first operand is (add|sub|and|or|xor|rem) with a constant, and |
| 5697 | // the second operand is a constant, simplify a bit. |
| 5698 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(LHSI)) { |
| 5699 | switch (BO->getOpcode()) { |
| 5700 | case Instruction::SRem: |
| 5701 | // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one. |
| 5702 | if (RHSV == 0 && isa<ConstantInt>(BO->getOperand(1)) &&BO->hasOneUse()){ |
| 5703 | const APInt &V = cast<ConstantInt>(BO->getOperand(1))->getValue(); |
| 5704 | if (V.sgt(APInt(V.getBitWidth(), 1)) && V.isPowerOf2()) { |
| 5705 | Instruction *NewRem = |
| 5706 | BinaryOperator::createURem(BO->getOperand(0), BO->getOperand(1), |
| 5707 | BO->getName()); |
| 5708 | InsertNewInstBefore(NewRem, ICI); |
| 5709 | return new ICmpInst(ICI.getPredicate(), NewRem, |
| 5710 | Constant::getNullValue(BO->getType())); |
| 5711 | } |
| 5712 | } |
| 5713 | break; |
| 5714 | case Instruction::Add: |
| 5715 | // Replace ((add A, B) != C) with (A != C-B) if B & C are constants. |
| 5716 | if (ConstantInt *BOp1C = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
| 5717 | if (BO->hasOneUse()) |
| 5718 | return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), |
| 5719 | Subtract(RHS, BOp1C)); |
| 5720 | } else if (RHSV == 0) { |
| 5721 | // Replace ((add A, B) != 0) with (A != -B) if A or B is |
| 5722 | // efficiently invertible, or if the add has just this one use. |
| 5723 | Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1); |
| 5724 | |
| 5725 | if (Value *NegVal = dyn_castNegVal(BOp1)) |
| 5726 | return new ICmpInst(ICI.getPredicate(), BOp0, NegVal); |
| 5727 | else if (Value *NegVal = dyn_castNegVal(BOp0)) |
| 5728 | return new ICmpInst(ICI.getPredicate(), NegVal, BOp1); |
| 5729 | else if (BO->hasOneUse()) { |
| 5730 | Instruction *Neg = BinaryOperator::createNeg(BOp1); |
| 5731 | InsertNewInstBefore(Neg, ICI); |
| 5732 | Neg->takeName(BO); |
| 5733 | return new ICmpInst(ICI.getPredicate(), BOp0, Neg); |
| 5734 | } |
| 5735 | } |
| 5736 | break; |
| 5737 | case Instruction::Xor: |
| 5738 | // For the xor case, we can xor two constants together, eliminating |
| 5739 | // the explicit xor. |
| 5740 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) |
| 5741 | return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), |
| 5742 | ConstantExpr::getXor(RHS, BOC)); |
| 5743 | |
| 5744 | // FALLTHROUGH |
| 5745 | case Instruction::Sub: |
| 5746 | // Replace (([sub|xor] A, B) != 0) with (A != B) |
| 5747 | if (RHSV == 0) |
| 5748 | return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), |
| 5749 | BO->getOperand(1)); |
| 5750 | break; |
| 5751 | |
| 5752 | case Instruction::Or: |
| 5753 | // If bits are being or'd in that are not present in the constant we |
| 5754 | // are comparing against, then the comparison could never succeed! |
| 5755 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) { |
| 5756 | Constant *NotCI = ConstantExpr::getNot(RHS); |
| 5757 | if (!ConstantExpr::getAnd(BOC, NotCI)->isNullValue()) |
| 5758 | return ReplaceInstUsesWith(ICI, ConstantInt::get(Type::Int1Ty, |
| 5759 | isICMP_NE)); |
| 5760 | } |
| 5761 | break; |
| 5762 | |
| 5763 | case Instruction::And: |
| 5764 | if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
| 5765 | // If bits are being compared against that are and'd out, then the |
| 5766 | // comparison can never succeed! |
| 5767 | if ((RHSV & ~BOC->getValue()) != 0) |
| 5768 | return ReplaceInstUsesWith(ICI, ConstantInt::get(Type::Int1Ty, |
| 5769 | isICMP_NE)); |
| 5770 | |
| 5771 | // If we have ((X & C) == C), turn it into ((X & C) != 0). |
| 5772 | if (RHS == BOC && RHSV.isPowerOf2()) |
| 5773 | return new ICmpInst(isICMP_NE ? ICmpInst::ICMP_EQ : |
| 5774 | ICmpInst::ICMP_NE, LHSI, |
| 5775 | Constant::getNullValue(RHS->getType())); |
| 5776 | |
| 5777 | // Replace (and X, (1 << size(X)-1) != 0) with x s< 0 |
| 5778 | if (isSignBit(BOC)) { |
| 5779 | Value *X = BO->getOperand(0); |
| 5780 | Constant *Zero = Constant::getNullValue(X->getType()); |
| 5781 | ICmpInst::Predicate pred = isICMP_NE ? |
| 5782 | ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGE; |
| 5783 | return new ICmpInst(pred, X, Zero); |
| 5784 | } |
| 5785 | |
| 5786 | // ((X & ~7) == 0) --> X < 8 |
| 5787 | if (RHSV == 0 && isHighOnes(BOC)) { |
| 5788 | Value *X = BO->getOperand(0); |
| 5789 | Constant *NegX = ConstantExpr::getNeg(BOC); |
| 5790 | ICmpInst::Predicate pred = isICMP_NE ? |
| 5791 | ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT; |
| 5792 | return new ICmpInst(pred, X, NegX); |
| 5793 | } |
| 5794 | } |
| 5795 | default: break; |
| 5796 | } |
| 5797 | } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(LHSI)) { |
| 5798 | // Handle icmp {eq|ne} <intrinsic>, intcst. |
| 5799 | if (II->getIntrinsicID() == Intrinsic::bswap) { |
| 5800 | AddToWorkList(II); |
| 5801 | ICI.setOperand(0, II->getOperand(1)); |
| 5802 | ICI.setOperand(1, ConstantInt::get(RHSV.byteSwap())); |
| 5803 | return &ICI; |
| 5804 | } |
| 5805 | } |
| 5806 | } else { // Not a ICMP_EQ/ICMP_NE |
Chris Lattner | e34e9a2 | 2007-04-14 23:32:02 +0000 | [diff] [blame] | 5807 | // If the LHS is a cast from an integral value of the same size, |
| 5808 | // then since we know the RHS is a constant, try to simlify. |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5809 | if (CastInst *Cast = dyn_cast<CastInst>(LHSI)) { |
| 5810 | Value *CastOp = Cast->getOperand(0); |
| 5811 | const Type *SrcTy = CastOp->getType(); |
| 5812 | uint32_t SrcTySize = SrcTy->getPrimitiveSizeInBits(); |
| 5813 | if (SrcTy->isInteger() && |
| 5814 | SrcTySize == Cast->getType()->getPrimitiveSizeInBits()) { |
| 5815 | // If this is an unsigned comparison, try to make the comparison use |
| 5816 | // smaller constant values. |
| 5817 | if (ICI.getPredicate() == ICmpInst::ICMP_ULT && RHSV.isSignBit()) { |
| 5818 | // X u< 128 => X s> -1 |
| 5819 | return new ICmpInst(ICmpInst::ICMP_SGT, CastOp, |
| 5820 | ConstantInt::get(APInt::getAllOnesValue(SrcTySize))); |
| 5821 | } else if (ICI.getPredicate() == ICmpInst::ICMP_UGT && |
| 5822 | RHSV == APInt::getSignedMaxValue(SrcTySize)) { |
| 5823 | // X u> 127 => X s< 0 |
| 5824 | return new ICmpInst(ICmpInst::ICMP_SLT, CastOp, |
| 5825 | Constant::getNullValue(SrcTy)); |
| 5826 | } |
| 5827 | } |
| 5828 | } |
| 5829 | } |
| 5830 | return 0; |
| 5831 | } |
| 5832 | |
| 5833 | /// visitICmpInstWithCastAndCast - Handle icmp (cast x to y), (cast/cst). |
| 5834 | /// We only handle extending casts so far. |
| 5835 | /// |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5836 | Instruction *InstCombiner::visitICmpInstWithCastAndCast(ICmpInst &ICI) { |
| 5837 | const CastInst *LHSCI = cast<CastInst>(ICI.getOperand(0)); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 5838 | Value *LHSCIOp = LHSCI->getOperand(0); |
| 5839 | const Type *SrcTy = LHSCIOp->getType(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5840 | const Type *DestTy = LHSCI->getType(); |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5841 | Value *RHSCIOp; |
| 5842 | |
Chris Lattner | 8c756c1 | 2007-05-05 22:41:33 +0000 | [diff] [blame] | 5843 | // Turn icmp (ptrtoint x), (ptrtoint/c) into a compare of the input if the |
| 5844 | // integer type is the same size as the pointer type. |
| 5845 | if (LHSCI->getOpcode() == Instruction::PtrToInt && |
| 5846 | getTargetData().getPointerSizeInBits() == |
| 5847 | cast<IntegerType>(DestTy)->getBitWidth()) { |
| 5848 | Value *RHSOp = 0; |
| 5849 | if (Constant *RHSC = dyn_cast<Constant>(ICI.getOperand(1))) { |
Chris Lattner | 6f6f512 | 2007-05-06 07:24:03 +0000 | [diff] [blame] | 5850 | RHSOp = ConstantExpr::getIntToPtr(RHSC, SrcTy); |
Chris Lattner | 8c756c1 | 2007-05-05 22:41:33 +0000 | [diff] [blame] | 5851 | } else if (PtrToIntInst *RHSC = dyn_cast<PtrToIntInst>(ICI.getOperand(1))) { |
| 5852 | RHSOp = RHSC->getOperand(0); |
| 5853 | // If the pointer types don't match, insert a bitcast. |
| 5854 | if (LHSCIOp->getType() != RHSOp->getType()) |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 5855 | RHSOp = InsertBitCastBefore(RHSOp, LHSCIOp->getType(), ICI); |
Chris Lattner | 8c756c1 | 2007-05-05 22:41:33 +0000 | [diff] [blame] | 5856 | } |
| 5857 | |
| 5858 | if (RHSOp) |
| 5859 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSOp); |
| 5860 | } |
| 5861 | |
| 5862 | // The code below only handles extension cast instructions, so far. |
| 5863 | // Enforce this. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5864 | if (LHSCI->getOpcode() != Instruction::ZExt && |
| 5865 | LHSCI->getOpcode() != Instruction::SExt) |
Chris Lattner | b352fa5 | 2005-01-17 03:20:02 +0000 | [diff] [blame] | 5866 | return 0; |
| 5867 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5868 | bool isSignedExt = LHSCI->getOpcode() == Instruction::SExt; |
| 5869 | bool isSignedCmp = ICI.isSignedPredicate(); |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5870 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5871 | if (CastInst *CI = dyn_cast<CastInst>(ICI.getOperand(1))) { |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5872 | // Not an extension from the same type? |
| 5873 | RHSCIOp = CI->getOperand(0); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5874 | if (RHSCIOp->getType() != LHSCIOp->getType()) |
| 5875 | return 0; |
Chris Lattner | a5c5e77 | 2007-01-13 23:11:38 +0000 | [diff] [blame] | 5876 | |
Nick Lewycky | 4189a53 | 2008-01-28 03:48:02 +0000 | [diff] [blame] | 5877 | // 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] | 5878 | // and the other is a zext), then we can't handle this. |
| 5879 | if (CI->getOpcode() != LHSCI->getOpcode()) |
| 5880 | return 0; |
| 5881 | |
Nick Lewycky | 4189a53 | 2008-01-28 03:48:02 +0000 | [diff] [blame] | 5882 | // Deal with equality cases early. |
| 5883 | if (ICI.isEquality()) |
| 5884 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSCIOp); |
| 5885 | |
| 5886 | // A signed comparison of sign extended values simplifies into a |
| 5887 | // signed comparison. |
| 5888 | if (isSignedCmp && isSignedExt) |
| 5889 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSCIOp); |
| 5890 | |
| 5891 | // The other three cases all fold into an unsigned comparison. |
| 5892 | return new ICmpInst(ICI.getUnsignedPredicate(), LHSCIOp, RHSCIOp); |
Reid Spencer | 6731d5c | 2004-11-28 21:31:15 +0000 | [diff] [blame] | 5893 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5894 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5895 | // If we aren't dealing with a constant on the RHS, exit early |
| 5896 | ConstantInt *CI = dyn_cast<ConstantInt>(ICI.getOperand(1)); |
| 5897 | if (!CI) |
| 5898 | return 0; |
| 5899 | |
| 5900 | // Compute the constant that would happen if we truncated to SrcTy then |
| 5901 | // reextended to DestTy. |
| 5902 | Constant *Res1 = ConstantExpr::getTrunc(CI, SrcTy); |
| 5903 | Constant *Res2 = ConstantExpr::getCast(LHSCI->getOpcode(), Res1, DestTy); |
| 5904 | |
| 5905 | // If the re-extended constant didn't change... |
| 5906 | if (Res2 == CI) { |
| 5907 | // Make sure that sign of the Cmp and the sign of the Cast are the same. |
| 5908 | // For example, we might have: |
| 5909 | // %A = sext short %X to uint |
| 5910 | // %B = icmp ugt uint %A, 1330 |
| 5911 | // It is incorrect to transform this into |
| 5912 | // %B = icmp ugt short %X, 1330 |
| 5913 | // because %A may have negative value. |
| 5914 | // |
| 5915 | // However, it is OK if SrcTy is bool (See cast-set.ll testcase) |
| 5916 | // OR operation is EQ/NE. |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 5917 | if (isSignedExt == isSignedCmp || SrcTy == Type::Int1Ty || ICI.isEquality()) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5918 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, Res1); |
| 5919 | else |
| 5920 | return 0; |
| 5921 | } |
| 5922 | |
| 5923 | // The re-extended constant changed so the constant cannot be represented |
| 5924 | // in the shorter type. Consequently, we cannot emit a simple comparison. |
| 5925 | |
| 5926 | // First, handle some easy cases. We know the result cannot be equal at this |
| 5927 | // point so handle the ICI.isEquality() cases |
| 5928 | if (ICI.getPredicate() == ICmpInst::ICMP_EQ) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5929 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5930 | if (ICI.getPredicate() == ICmpInst::ICMP_NE) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5931 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5932 | |
| 5933 | // Evaluate the comparison for LT (we invert for GT below). LE and GE cases |
| 5934 | // should have been folded away previously and not enter in here. |
| 5935 | Value *Result; |
| 5936 | if (isSignedCmp) { |
| 5937 | // We're performing a signed comparison. |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5938 | if (cast<ConstantInt>(CI)->getValue().isNegative()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5939 | Result = ConstantInt::getFalse(); // X < (small) --> false |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5940 | else |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5941 | Result = ConstantInt::getTrue(); // X < (large) --> true |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5942 | } else { |
| 5943 | // We're performing an unsigned comparison. |
| 5944 | if (isSignedExt) { |
| 5945 | // We're performing an unsigned comp with a sign extended value. |
| 5946 | // This is true if the input is >= 0. [aka >s -1] |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5947 | Constant *NegOne = ConstantInt::getAllOnesValue(SrcTy); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5948 | Result = InsertNewInstBefore(new ICmpInst(ICmpInst::ICMP_SGT, LHSCIOp, |
| 5949 | NegOne, ICI.getName()), ICI); |
| 5950 | } else { |
| 5951 | // Unsigned extend & unsigned compare -> always true. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5952 | Result = ConstantInt::getTrue(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5953 | } |
| 5954 | } |
| 5955 | |
| 5956 | // Finally, return the value computed. |
| 5957 | if (ICI.getPredicate() == ICmpInst::ICMP_ULT || |
| 5958 | ICI.getPredicate() == ICmpInst::ICMP_SLT) { |
| 5959 | return ReplaceInstUsesWith(ICI, Result); |
| 5960 | } else { |
| 5961 | assert((ICI.getPredicate()==ICmpInst::ICMP_UGT || |
| 5962 | ICI.getPredicate()==ICmpInst::ICMP_SGT) && |
| 5963 | "ICmp should be folded!"); |
| 5964 | if (Constant *CI = dyn_cast<Constant>(Result)) |
| 5965 | return ReplaceInstUsesWith(ICI, ConstantExpr::getNot(CI)); |
| 5966 | else |
| 5967 | return BinaryOperator::createNot(Result); |
| 5968 | } |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5969 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5970 | |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 5971 | Instruction *InstCombiner::visitShl(BinaryOperator &I) { |
| 5972 | return commonShiftTransforms(I); |
| 5973 | } |
| 5974 | |
| 5975 | Instruction *InstCombiner::visitLShr(BinaryOperator &I) { |
| 5976 | return commonShiftTransforms(I); |
| 5977 | } |
| 5978 | |
| 5979 | Instruction *InstCombiner::visitAShr(BinaryOperator &I) { |
Chris Lattner | 348f665 | 2007-12-06 01:59:46 +0000 | [diff] [blame] | 5980 | if (Instruction *R = commonShiftTransforms(I)) |
| 5981 | return R; |
| 5982 | |
| 5983 | Value *Op0 = I.getOperand(0); |
| 5984 | |
| 5985 | // ashr int -1, X = -1 (for any arithmetic shift rights of ~0) |
| 5986 | if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0)) |
| 5987 | if (CSI->isAllOnesValue()) |
| 5988 | return ReplaceInstUsesWith(I, CSI); |
| 5989 | |
| 5990 | // See if we can turn a signed shr into an unsigned shr. |
| 5991 | if (MaskedValueIsZero(Op0, |
| 5992 | APInt::getSignBit(I.getType()->getPrimitiveSizeInBits()))) |
| 5993 | return BinaryOperator::createLShr(Op0, I.getOperand(1)); |
| 5994 | |
| 5995 | return 0; |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 5996 | } |
| 5997 | |
| 5998 | Instruction *InstCombiner::commonShiftTransforms(BinaryOperator &I) { |
| 5999 | assert(I.getOperand(1)->getType() == I.getOperand(0)->getType()); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 6000 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 6001 | |
| 6002 | // shl X, 0 == X and shr X, 0 == X |
| 6003 | // shl 0, X == 0 and shr 0, X == 0 |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6004 | if (Op1 == Constant::getNullValue(Op1->getType()) || |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 6005 | Op0 == Constant::getNullValue(Op0->getType())) |
| 6006 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 8d6bbdb | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 6007 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6008 | if (isa<UndefValue>(Op0)) { |
| 6009 | if (I.getOpcode() == Instruction::AShr) // undef >>s X -> undef |
Chris Lattner | 79a564c | 2004-10-16 23:28:04 +0000 | [diff] [blame] | 6010 | return ReplaceInstUsesWith(I, Op0); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6011 | else // undef << X -> 0, undef >>u X -> 0 |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 6012 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 6013 | } |
| 6014 | if (isa<UndefValue>(Op1)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6015 | if (I.getOpcode() == Instruction::AShr) // X >>s undef -> X |
| 6016 | return ReplaceInstUsesWith(I, Op0); |
| 6017 | else // X << undef, X >>u undef -> 0 |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 6018 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 6019 | } |
| 6020 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 6021 | // Try to fold constant and into select arguments. |
| 6022 | if (isa<Constant>(Op0)) |
| 6023 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 6024 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 6025 | return R; |
| 6026 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6027 | if (ConstantInt *CUI = dyn_cast<ConstantInt>(Op1)) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6028 | if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I)) |
| 6029 | return Res; |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6030 | return 0; |
| 6031 | } |
| 6032 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6033 | Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6034 | BinaryOperator &I) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6035 | bool isLeftShift = I.getOpcode() == Instruction::Shl; |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6036 | |
Chris Lattner | 8d6bbdb | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 6037 | // See if we can simplify any instructions used by the instruction whose sole |
| 6038 | // purpose is to compute bits we don't care about. |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6039 | uint32_t TypeBits = Op0->getType()->getPrimitiveSizeInBits(); |
| 6040 | APInt KnownZero(TypeBits, 0), KnownOne(TypeBits, 0); |
| 6041 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(TypeBits), |
Chris Lattner | 8d6bbdb | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 6042 | KnownZero, KnownOne)) |
| 6043 | return &I; |
| 6044 | |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6045 | // shl uint X, 32 = 0 and shr ubyte Y, 9 = 0, ... just don't eliminate shr |
| 6046 | // of a signed value. |
| 6047 | // |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 6048 | if (Op1->uge(TypeBits)) { |
Chris Lattner | 0737c24 | 2007-02-02 05:29:55 +0000 | [diff] [blame] | 6049 | if (I.getOpcode() != Instruction::AShr) |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6050 | return ReplaceInstUsesWith(I, Constant::getNullValue(Op0->getType())); |
| 6051 | else { |
Chris Lattner | 0737c24 | 2007-02-02 05:29:55 +0000 | [diff] [blame] | 6052 | I.setOperand(1, ConstantInt::get(I.getType(), TypeBits-1)); |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6053 | return &I; |
Chris Lattner | 8adac75 | 2004-02-23 20:30:06 +0000 | [diff] [blame] | 6054 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6055 | } |
| 6056 | |
| 6057 | // ((X*C1) << C2) == (X * (C1 << C2)) |
| 6058 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0)) |
| 6059 | if (BO->getOpcode() == Instruction::Mul && isLeftShift) |
| 6060 | if (Constant *BOOp = dyn_cast<Constant>(BO->getOperand(1))) |
| 6061 | return BinaryOperator::createMul(BO->getOperand(0), |
| 6062 | ConstantExpr::getShl(BOOp, Op1)); |
| 6063 | |
| 6064 | // Try to fold constant and into select arguments. |
| 6065 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 6066 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 6067 | return R; |
| 6068 | if (isa<PHINode>(Op0)) |
| 6069 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 6070 | return NV; |
| 6071 | |
Chris Lattner | 8999dd3 | 2007-12-22 09:07:47 +0000 | [diff] [blame] | 6072 | // Fold shift2(trunc(shift1(x,c1)), c2) -> trunc(shift2(shift1(x,c1),c2)) |
| 6073 | if (TruncInst *TI = dyn_cast<TruncInst>(Op0)) { |
| 6074 | Instruction *TrOp = dyn_cast<Instruction>(TI->getOperand(0)); |
| 6075 | // If 'shift2' is an ashr, we would have to get the sign bit into a funny |
| 6076 | // place. Don't try to do this transformation in this case. Also, we |
| 6077 | // require that the input operand is a shift-by-constant so that we have |
| 6078 | // confidence that the shifts will get folded together. We could do this |
| 6079 | // xform in more cases, but it is unlikely to be profitable. |
| 6080 | if (TrOp && I.isLogicalShift() && TrOp->isShift() && |
| 6081 | isa<ConstantInt>(TrOp->getOperand(1))) { |
| 6082 | // Okay, we'll do this xform. Make the shift of shift. |
| 6083 | Constant *ShAmt = ConstantExpr::getZExt(Op1, TrOp->getType()); |
| 6084 | Instruction *NSh = BinaryOperator::create(I.getOpcode(), TrOp, ShAmt, |
| 6085 | I.getName()); |
| 6086 | InsertNewInstBefore(NSh, I); // (shift2 (shift1 & 0x00FF), c2) |
| 6087 | |
| 6088 | // For logical shifts, the truncation has the effect of making the high |
| 6089 | // part of the register be zeros. Emulate this by inserting an AND to |
| 6090 | // clear the top bits as needed. This 'and' will usually be zapped by |
| 6091 | // other xforms later if dead. |
| 6092 | unsigned SrcSize = TrOp->getType()->getPrimitiveSizeInBits(); |
| 6093 | unsigned DstSize = TI->getType()->getPrimitiveSizeInBits(); |
| 6094 | APInt MaskV(APInt::getLowBitsSet(SrcSize, DstSize)); |
| 6095 | |
| 6096 | // The mask we constructed says what the trunc would do if occurring |
| 6097 | // between the shifts. We want to know the effect *after* the second |
| 6098 | // shift. We know that it is a logical shift by a constant, so adjust the |
| 6099 | // mask as appropriate. |
| 6100 | if (I.getOpcode() == Instruction::Shl) |
| 6101 | MaskV <<= Op1->getZExtValue(); |
| 6102 | else { |
| 6103 | assert(I.getOpcode() == Instruction::LShr && "Unknown logical shift"); |
| 6104 | MaskV = MaskV.lshr(Op1->getZExtValue()); |
| 6105 | } |
| 6106 | |
| 6107 | Instruction *And = BinaryOperator::createAnd(NSh, ConstantInt::get(MaskV), |
| 6108 | TI->getName()); |
| 6109 | InsertNewInstBefore(And, I); // shift1 & 0x00FF |
| 6110 | |
| 6111 | // Return the value truncated to the interesting size. |
| 6112 | return new TruncInst(And, I.getType()); |
| 6113 | } |
| 6114 | } |
| 6115 | |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6116 | if (Op0->hasOneUse()) { |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6117 | if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0)) { |
| 6118 | // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C) |
| 6119 | Value *V1, *V2; |
| 6120 | ConstantInt *CC; |
| 6121 | switch (Op0BO->getOpcode()) { |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 6122 | default: break; |
| 6123 | case Instruction::Add: |
| 6124 | case Instruction::And: |
| 6125 | case Instruction::Or: |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6126 | case Instruction::Xor: { |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 6127 | // These operators commute. |
| 6128 | // Turn (Y + (X >> C)) << C -> (X + (Y << C)) & (~0 << C) |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6129 | if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() && |
| 6130 | match(Op0BO->getOperand(1), |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6131 | m_Shr(m_Value(V1), m_ConstantInt(CC))) && CC == Op1) { |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 6132 | Instruction *YS = BinaryOperator::createShl( |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6133 | Op0BO->getOperand(0), Op1, |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6134 | Op0BO->getName()); |
| 6135 | InsertNewInstBefore(YS, I); // (Y << C) |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 6136 | Instruction *X = |
| 6137 | BinaryOperator::create(Op0BO->getOpcode(), YS, V1, |
| 6138 | Op0BO->getOperand(1)->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6139 | InsertNewInstBefore(X, I); // (X + (Y << C)) |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 6140 | uint32_t Op1Val = Op1->getLimitedValue(TypeBits); |
Zhou Sheng | 90b9681 | 2007-03-30 05:45:18 +0000 | [diff] [blame] | 6141 | return BinaryOperator::createAnd(X, ConstantInt::get( |
| 6142 | APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val))); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6143 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6144 | |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6145 | // Turn (Y + ((X >> C) & CC)) << C -> ((X & (CC << C)) + (Y << C)) |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6146 | Value *Op0BOOp1 = Op0BO->getOperand(1); |
Chris Lattner | 3c69849 | 2007-03-05 00:11:19 +0000 | [diff] [blame] | 6147 | if (isLeftShift && Op0BOOp1->hasOneUse() && |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6148 | match(Op0BOOp1, |
| 6149 | 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] | 6150 | cast<BinaryOperator>(Op0BOOp1)->getOperand(0)->hasOneUse() && |
| 6151 | V2 == Op1) { |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 6152 | Instruction *YS = BinaryOperator::createShl( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6153 | Op0BO->getOperand(0), Op1, |
| 6154 | Op0BO->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6155 | InsertNewInstBefore(YS, I); // (Y << C) |
| 6156 | Instruction *XM = |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6157 | BinaryOperator::createAnd(V1, ConstantExpr::getShl(CC, Op1), |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6158 | V1->getName()+".mask"); |
| 6159 | InsertNewInstBefore(XM, I); // X & (CC << C) |
| 6160 | |
| 6161 | return BinaryOperator::create(Op0BO->getOpcode(), YS, XM); |
| 6162 | } |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6163 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6164 | |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6165 | // FALL THROUGH. |
| 6166 | case Instruction::Sub: { |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 6167 | // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C) |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6168 | if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() && |
| 6169 | match(Op0BO->getOperand(0), |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6170 | m_Shr(m_Value(V1), m_ConstantInt(CC))) && CC == Op1) { |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 6171 | Instruction *YS = BinaryOperator::createShl( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6172 | Op0BO->getOperand(1), Op1, |
| 6173 | Op0BO->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6174 | InsertNewInstBefore(YS, I); // (Y << C) |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 6175 | Instruction *X = |
Chris Lattner | 13d4ab4 | 2006-05-31 21:14:00 +0000 | [diff] [blame] | 6176 | BinaryOperator::create(Op0BO->getOpcode(), V1, YS, |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 6177 | Op0BO->getOperand(0)->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6178 | InsertNewInstBefore(X, I); // (X + (Y << C)) |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 6179 | uint32_t Op1Val = Op1->getLimitedValue(TypeBits); |
Zhou Sheng | 90b9681 | 2007-03-30 05:45:18 +0000 | [diff] [blame] | 6180 | return BinaryOperator::createAnd(X, ConstantInt::get( |
| 6181 | APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val))); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6182 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6183 | |
Chris Lattner | 13d4ab4 | 2006-05-31 21:14:00 +0000 | [diff] [blame] | 6184 | // Turn (((X >> C)&CC) + Y) << C -> (X + (Y << C)) & (CC << C) |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6185 | if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() && |
| 6186 | match(Op0BO->getOperand(0), |
| 6187 | m_And(m_Shr(m_Value(V1), m_Value(V2)), |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6188 | m_ConstantInt(CC))) && V2 == Op1 && |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 6189 | cast<BinaryOperator>(Op0BO->getOperand(0)) |
| 6190 | ->getOperand(0)->hasOneUse()) { |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 6191 | Instruction *YS = BinaryOperator::createShl( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6192 | Op0BO->getOperand(1), Op1, |
| 6193 | Op0BO->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6194 | InsertNewInstBefore(YS, I); // (Y << C) |
| 6195 | Instruction *XM = |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6196 | BinaryOperator::createAnd(V1, ConstantExpr::getShl(CC, Op1), |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6197 | V1->getName()+".mask"); |
| 6198 | InsertNewInstBefore(XM, I); // X & (CC << C) |
| 6199 | |
Chris Lattner | 13d4ab4 | 2006-05-31 21:14:00 +0000 | [diff] [blame] | 6200 | return BinaryOperator::create(Op0BO->getOpcode(), XM, YS); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6201 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6202 | |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 6203 | break; |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6204 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6205 | } |
| 6206 | |
| 6207 | |
| 6208 | // If the operand is an bitwise operator with a constant RHS, and the |
| 6209 | // shift is the only use, we can pull it out of the shift. |
| 6210 | if (ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand(1))) { |
| 6211 | bool isValid = true; // Valid only for And, Or, Xor |
| 6212 | bool highBitSet = false; // Transform if high bit of constant set? |
| 6213 | |
| 6214 | switch (Op0BO->getOpcode()) { |
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 6215 | default: isValid = false; break; // Do not perform transform! |
Chris Lattner | 1f7e160 | 2004-10-08 03:46:20 +0000 | [diff] [blame] | 6216 | case Instruction::Add: |
| 6217 | isValid = isLeftShift; |
| 6218 | break; |
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 6219 | case Instruction::Or: |
| 6220 | case Instruction::Xor: |
| 6221 | highBitSet = false; |
| 6222 | break; |
| 6223 | case Instruction::And: |
| 6224 | highBitSet = true; |
| 6225 | break; |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6226 | } |
| 6227 | |
| 6228 | // If this is a signed shift right, and the high bit is modified |
| 6229 | // by the logical operation, do not perform the transformation. |
| 6230 | // The highBitSet boolean indicates the value of the high bit of |
| 6231 | // the constant which would cause it to be modified for this |
| 6232 | // operation. |
| 6233 | // |
Chris Lattner | c95ba44 | 2007-12-06 06:25:04 +0000 | [diff] [blame] | 6234 | if (isValid && I.getOpcode() == Instruction::AShr) |
Zhou Sheng | e9e03f6 | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 6235 | isValid = Op0C->getValue()[TypeBits-1] == highBitSet; |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6236 | |
| 6237 | if (isValid) { |
| 6238 | Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, Op1); |
| 6239 | |
| 6240 | Instruction *NewShift = |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6241 | BinaryOperator::create(I.getOpcode(), Op0BO->getOperand(0), Op1); |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6242 | InsertNewInstBefore(NewShift, I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6243 | NewShift->takeName(Op0BO); |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6244 | |
| 6245 | return BinaryOperator::create(Op0BO->getOpcode(), NewShift, |
| 6246 | NewRHS); |
| 6247 | } |
| 6248 | } |
| 6249 | } |
| 6250 | } |
| 6251 | |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6252 | // 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] | 6253 | BinaryOperator *ShiftOp = dyn_cast<BinaryOperator>(Op0); |
| 6254 | if (ShiftOp && !ShiftOp->isShift()) |
| 6255 | ShiftOp = 0; |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6256 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6257 | if (ShiftOp && isa<ConstantInt>(ShiftOp->getOperand(1))) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6258 | ConstantInt *ShiftAmt1C = cast<ConstantInt>(ShiftOp->getOperand(1)); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 6259 | uint32_t ShiftAmt1 = ShiftAmt1C->getLimitedValue(TypeBits); |
| 6260 | uint32_t ShiftAmt2 = Op1->getLimitedValue(TypeBits); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6261 | assert(ShiftAmt2 != 0 && "Should have been simplified earlier"); |
| 6262 | if (ShiftAmt1 == 0) return 0; // Will be simplified in the future. |
| 6263 | Value *X = ShiftOp->getOperand(0); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6264 | |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 6265 | uint32_t AmtSum = ShiftAmt1+ShiftAmt2; // Fold into one big shift. |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6266 | if (AmtSum > TypeBits) |
| 6267 | AmtSum = TypeBits; |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6268 | |
| 6269 | const IntegerType *Ty = cast<IntegerType>(I.getType()); |
| 6270 | |
| 6271 | // Check for (X << c1) << c2 and (X >> c1) >> c2 |
Chris Lattner | 7f3da2d | 2007-02-03 23:28:07 +0000 | [diff] [blame] | 6272 | if (I.getOpcode() == ShiftOp->getOpcode()) { |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6273 | return BinaryOperator::create(I.getOpcode(), X, |
| 6274 | ConstantInt::get(Ty, AmtSum)); |
| 6275 | } else if (ShiftOp->getOpcode() == Instruction::LShr && |
| 6276 | I.getOpcode() == Instruction::AShr) { |
| 6277 | // ((X >>u C1) >>s C2) -> (X >>u (C1+C2)) since C1 != 0. |
| 6278 | return BinaryOperator::createLShr(X, ConstantInt::get(Ty, AmtSum)); |
| 6279 | } else if (ShiftOp->getOpcode() == Instruction::AShr && |
| 6280 | I.getOpcode() == Instruction::LShr) { |
| 6281 | // ((X >>s C1) >>u C2) -> ((X >>s (C1+C2)) & mask) since C1 != 0. |
| 6282 | Instruction *Shift = |
| 6283 | BinaryOperator::createAShr(X, ConstantInt::get(Ty, AmtSum)); |
| 6284 | InsertNewInstBefore(Shift, I); |
| 6285 | |
Zhou Sheng | e9e03f6 | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 6286 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6287 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6288 | } |
| 6289 | |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6290 | // Okay, if we get here, one shift must be left, and the other shift must be |
| 6291 | // right. See if the amounts are equal. |
| 6292 | if (ShiftAmt1 == ShiftAmt2) { |
| 6293 | // If we have ((X >>? C) << C), turn this into X & (-1 << C). |
| 6294 | if (I.getOpcode() == Instruction::Shl) { |
Reid Spencer | 55702aa | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 6295 | APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt1)); |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6296 | return BinaryOperator::createAnd(X, ConstantInt::get(Mask)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6297 | } |
| 6298 | // If we have ((X << C) >>u C), turn this into X & (-1 >>u C). |
| 6299 | if (I.getOpcode() == Instruction::LShr) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 6300 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt1)); |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6301 | return BinaryOperator::createAnd(X, ConstantInt::get(Mask)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6302 | } |
| 6303 | // We can simplify ((X << C) >>s C) into a trunc + sext. |
| 6304 | // NOTE: we could do this for any C, but that would make 'unusual' integer |
| 6305 | // types. For now, just stick to ones well-supported by the code |
| 6306 | // generators. |
| 6307 | const Type *SExtType = 0; |
| 6308 | switch (Ty->getBitWidth() - ShiftAmt1) { |
Zhou Sheng | e9e03f6 | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 6309 | case 1 : |
| 6310 | case 8 : |
| 6311 | case 16 : |
| 6312 | case 32 : |
| 6313 | case 64 : |
| 6314 | case 128: |
| 6315 | SExtType = IntegerType::get(Ty->getBitWidth() - ShiftAmt1); |
| 6316 | break; |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6317 | default: break; |
| 6318 | } |
| 6319 | if (SExtType) { |
| 6320 | Instruction *NewTrunc = new TruncInst(X, SExtType, "sext"); |
| 6321 | InsertNewInstBefore(NewTrunc, I); |
| 6322 | return new SExtInst(NewTrunc, Ty); |
| 6323 | } |
| 6324 | // Otherwise, we can't handle it yet. |
| 6325 | } else if (ShiftAmt1 < ShiftAmt2) { |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 6326 | uint32_t ShiftDiff = ShiftAmt2-ShiftAmt1; |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6327 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 6328 | // (X >>? C1) << C2 --> X << (C2-C1) & (-1 << C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6329 | if (I.getOpcode() == Instruction::Shl) { |
| 6330 | assert(ShiftOp->getOpcode() == Instruction::LShr || |
| 6331 | ShiftOp->getOpcode() == Instruction::AShr); |
Chris Lattner | e8d56c5 | 2006-01-07 01:32:28 +0000 | [diff] [blame] | 6332 | Instruction *Shift = |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6333 | BinaryOperator::createShl(X, ConstantInt::get(Ty, ShiftDiff)); |
Chris Lattner | e8d56c5 | 2006-01-07 01:32:28 +0000 | [diff] [blame] | 6334 | InsertNewInstBefore(Shift, I); |
| 6335 | |
Reid Spencer | 55702aa | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 6336 | APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
| 6337 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6338 | } |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6339 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 6340 | // (X << C1) >>u C2 --> X >>u (C2-C1) & (-1 >> C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6341 | if (I.getOpcode() == Instruction::LShr) { |
| 6342 | assert(ShiftOp->getOpcode() == Instruction::Shl); |
| 6343 | Instruction *Shift = |
| 6344 | BinaryOperator::createLShr(X, ConstantInt::get(Ty, ShiftDiff)); |
| 6345 | InsertNewInstBefore(Shift, I); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6346 | |
Reid Spencer | d5e30f0 | 2007-03-26 17:18:58 +0000 | [diff] [blame] | 6347 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6348 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 6349 | } |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6350 | |
| 6351 | // We can't handle (X << C1) >>s C2, it shifts arbitrary bits in. |
| 6352 | } else { |
| 6353 | assert(ShiftAmt2 < ShiftAmt1); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 6354 | uint32_t ShiftDiff = ShiftAmt1-ShiftAmt2; |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6355 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 6356 | // (X >>? C1) << C2 --> X >>? (C1-C2) & (-1 << C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6357 | if (I.getOpcode() == Instruction::Shl) { |
| 6358 | assert(ShiftOp->getOpcode() == Instruction::LShr || |
| 6359 | ShiftOp->getOpcode() == Instruction::AShr); |
| 6360 | Instruction *Shift = |
| 6361 | BinaryOperator::create(ShiftOp->getOpcode(), X, |
| 6362 | ConstantInt::get(Ty, ShiftDiff)); |
| 6363 | InsertNewInstBefore(Shift, I); |
| 6364 | |
Reid Spencer | 55702aa | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 6365 | APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6366 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6367 | } |
| 6368 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 6369 | // (X << C1) >>u C2 --> X << (C1-C2) & (-1 >> C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6370 | if (I.getOpcode() == Instruction::LShr) { |
| 6371 | assert(ShiftOp->getOpcode() == Instruction::Shl); |
| 6372 | Instruction *Shift = |
| 6373 | BinaryOperator::createShl(X, ConstantInt::get(Ty, ShiftDiff)); |
| 6374 | InsertNewInstBefore(Shift, I); |
| 6375 | |
Reid Spencer | 68d27cf | 2007-03-26 23:45:51 +0000 | [diff] [blame] | 6376 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6377 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6378 | } |
| 6379 | |
| 6380 | // 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] | 6381 | } |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6382 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 6383 | return 0; |
| 6384 | } |
| 6385 | |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 6386 | |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 6387 | /// DecomposeSimpleLinearExpr - Analyze 'Val', seeing if it is a simple linear |
| 6388 | /// expression. If so, decompose it, returning some value X, such that Val is |
| 6389 | /// X*Scale+Offset. |
| 6390 | /// |
| 6391 | static Value *DecomposeSimpleLinearExpr(Value *Val, unsigned &Scale, |
Jeff Cohen | 86796be | 2007-04-04 16:58:57 +0000 | [diff] [blame] | 6392 | int &Offset) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6393 | assert(Val->getType() == Type::Int32Ty && "Unexpected allocation size type!"); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6394 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6395 | Offset = CI->getZExtValue(); |
Chris Lattner | 6a94de2 | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 6396 | Scale = 0; |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6397 | return ConstantInt::get(Type::Int32Ty, 0); |
Chris Lattner | 6a94de2 | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 6398 | } else if (BinaryOperator *I = dyn_cast<BinaryOperator>(Val)) { |
| 6399 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 6400 | if (I->getOpcode() == Instruction::Shl) { |
| 6401 | // This is a value scaled by '1 << the shift amt'. |
| 6402 | Scale = 1U << RHS->getZExtValue(); |
| 6403 | Offset = 0; |
| 6404 | return I->getOperand(0); |
| 6405 | } else if (I->getOpcode() == Instruction::Mul) { |
| 6406 | // This value is scaled by 'RHS'. |
| 6407 | Scale = RHS->getZExtValue(); |
| 6408 | Offset = 0; |
| 6409 | return I->getOperand(0); |
| 6410 | } else if (I->getOpcode() == Instruction::Add) { |
| 6411 | // We have X+C. Check to see if we really have (X*C2)+C1, |
| 6412 | // where C1 is divisible by C2. |
| 6413 | unsigned SubScale; |
| 6414 | Value *SubVal = |
| 6415 | DecomposeSimpleLinearExpr(I->getOperand(0), SubScale, Offset); |
| 6416 | Offset += RHS->getZExtValue(); |
| 6417 | Scale = SubScale; |
| 6418 | return SubVal; |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 6419 | } |
| 6420 | } |
| 6421 | } |
| 6422 | |
| 6423 | // Otherwise, we can't look past this. |
| 6424 | Scale = 1; |
| 6425 | Offset = 0; |
| 6426 | return Val; |
| 6427 | } |
| 6428 | |
| 6429 | |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6430 | /// PromoteCastOfAllocation - If we find a cast of an allocation instruction, |
| 6431 | /// 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] | 6432 | Instruction *InstCombiner::PromoteCastOfAllocation(BitCastInst &CI, |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6433 | AllocationInst &AI) { |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 6434 | const PointerType *PTy = cast<PointerType>(CI.getType()); |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6435 | |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 6436 | // Remove any uses of AI that are dead. |
| 6437 | assert(!CI.use_empty() && "Dead instructions should be removed earlier!"); |
Chris Lattner | 535014f | 2007-02-15 22:52:10 +0000 | [diff] [blame] | 6438 | |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 6439 | for (Value::use_iterator UI = AI.use_begin(), E = AI.use_end(); UI != E; ) { |
| 6440 | Instruction *User = cast<Instruction>(*UI++); |
| 6441 | if (isInstructionTriviallyDead(User)) { |
| 6442 | while (UI != E && *UI == User) |
| 6443 | ++UI; // If this instruction uses AI more than once, don't break UI. |
| 6444 | |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 6445 | ++NumDeadInst; |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 6446 | DOUT << "IC: DCE: " << *User; |
Chris Lattner | f22a5c6 | 2007-03-02 19:59:19 +0000 | [diff] [blame] | 6447 | EraseInstFromFunction(*User); |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 6448 | } |
| 6449 | } |
| 6450 | |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6451 | // Get the type really allocated and the type casted to. |
| 6452 | const Type *AllocElTy = AI.getAllocatedType(); |
| 6453 | const Type *CastElTy = PTy->getElementType(); |
| 6454 | if (!AllocElTy->isSized() || !CastElTy->isSized()) return 0; |
Chris Lattner | 18e78bb | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 6455 | |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 6456 | unsigned AllocElTyAlign = TD->getABITypeAlignment(AllocElTy); |
| 6457 | unsigned CastElTyAlign = TD->getABITypeAlignment(CastElTy); |
Chris Lattner | 18e78bb | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 6458 | if (CastElTyAlign < AllocElTyAlign) return 0; |
| 6459 | |
Chris Lattner | 39387a5 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 6460 | // If the allocation has multiple uses, only promote it if we are strictly |
| 6461 | // increasing the alignment of the resultant allocation. If we keep it the |
| 6462 | // same, we open the door to infinite loops of various kinds. |
| 6463 | if (!AI.hasOneUse() && CastElTyAlign == AllocElTyAlign) return 0; |
| 6464 | |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 6465 | uint64_t AllocElTySize = TD->getABITypeSize(AllocElTy); |
| 6466 | uint64_t CastElTySize = TD->getABITypeSize(CastElTy); |
Chris Lattner | 0ddac2a | 2005-10-27 05:53:56 +0000 | [diff] [blame] | 6467 | if (CastElTySize == 0 || AllocElTySize == 0) return 0; |
Chris Lattner | 18e78bb | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 6468 | |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 6469 | // See if we can satisfy the modulus by pulling a scale out of the array |
| 6470 | // size argument. |
Jeff Cohen | 86796be | 2007-04-04 16:58:57 +0000 | [diff] [blame] | 6471 | unsigned ArraySizeScale; |
| 6472 | int ArrayOffset; |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 6473 | Value *NumElements = // See if the array size is a decomposable linear expr. |
| 6474 | DecomposeSimpleLinearExpr(AI.getOperand(0), ArraySizeScale, ArrayOffset); |
| 6475 | |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 6476 | // If we can now satisfy the modulus, by using a non-1 scale, we really can |
| 6477 | // do the xform. |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 6478 | if ((AllocElTySize*ArraySizeScale) % CastElTySize != 0 || |
| 6479 | (AllocElTySize*ArrayOffset ) % CastElTySize != 0) return 0; |
Chris Lattner | 8142b0a | 2005-10-27 06:12:00 +0000 | [diff] [blame] | 6480 | |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 6481 | unsigned Scale = (AllocElTySize*ArraySizeScale)/CastElTySize; |
| 6482 | Value *Amt = 0; |
| 6483 | if (Scale == 1) { |
| 6484 | Amt = NumElements; |
| 6485 | } else { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6486 | // If the allocation size is constant, form a constant mul expression |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6487 | Amt = ConstantInt::get(Type::Int32Ty, Scale); |
| 6488 | if (isa<ConstantInt>(NumElements)) |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 6489 | Amt = Multiply(cast<ConstantInt>(NumElements), cast<ConstantInt>(Amt)); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6490 | // otherwise multiply the amount and the number of elements |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 6491 | else if (Scale != 1) { |
| 6492 | Instruction *Tmp = BinaryOperator::createMul(Amt, NumElements, "tmp"); |
| 6493 | Amt = InsertNewInstBefore(Tmp, AI); |
Chris Lattner | 8142b0a | 2005-10-27 06:12:00 +0000 | [diff] [blame] | 6494 | } |
Chris Lattner | 0ddac2a | 2005-10-27 05:53:56 +0000 | [diff] [blame] | 6495 | } |
| 6496 | |
Jeff Cohen | 86796be | 2007-04-04 16:58:57 +0000 | [diff] [blame] | 6497 | if (int Offset = (AllocElTySize*ArrayOffset)/CastElTySize) { |
| 6498 | Value *Off = ConstantInt::get(Type::Int32Ty, Offset, true); |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 6499 | Instruction *Tmp = BinaryOperator::createAdd(Amt, Off, "tmp"); |
| 6500 | Amt = InsertNewInstBefore(Tmp, AI); |
| 6501 | } |
| 6502 | |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6503 | AllocationInst *New; |
| 6504 | if (isa<MallocInst>(AI)) |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6505 | New = new MallocInst(CastElTy, Amt, AI.getAlignment()); |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6506 | else |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6507 | New = new AllocaInst(CastElTy, Amt, AI.getAlignment()); |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6508 | InsertNewInstBefore(New, AI); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6509 | New->takeName(&AI); |
Chris Lattner | 39387a5 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 6510 | |
| 6511 | // If the allocation has multiple uses, insert a cast and change all things |
| 6512 | // that used it to use the new cast. This will also hack on CI, but it will |
| 6513 | // die soon. |
| 6514 | if (!AI.hasOneUse()) { |
| 6515 | AddUsesToWorkList(AI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6516 | // New is the allocation instruction, pointer typed. AI is the original |
| 6517 | // allocation instruction, also pointer typed. Thus, cast to use is BitCast. |
| 6518 | CastInst *NewCast = new BitCastInst(New, AI.getType(), "tmpcast"); |
Chris Lattner | 39387a5 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 6519 | InsertNewInstBefore(NewCast, AI); |
| 6520 | AI.replaceAllUsesWith(NewCast); |
| 6521 | } |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6522 | return ReplaceInstUsesWith(CI, New); |
| 6523 | } |
| 6524 | |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6525 | /// CanEvaluateInDifferentType - Return true if we can take the specified value |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6526 | /// and return it as type Ty without inserting any new casts and without |
| 6527 | /// changing the computed value. This is used by code that tries to decide |
| 6528 | /// whether promoting or shrinking integer operations to wider or smaller types |
| 6529 | /// will allow us to eliminate a truncate or extend. |
| 6530 | /// |
| 6531 | /// This is a truncation operation if Ty is smaller than V->getType(), or an |
| 6532 | /// extension operation if Ty is larger. |
| 6533 | static bool CanEvaluateInDifferentType(Value *V, const IntegerType *Ty, |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 6534 | unsigned CastOpc, int &NumCastsRemoved) { |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6535 | // We can always evaluate constants in another type. |
| 6536 | if (isa<ConstantInt>(V)) |
| 6537 | return true; |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6538 | |
| 6539 | Instruction *I = dyn_cast<Instruction>(V); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6540 | if (!I) return false; |
| 6541 | |
| 6542 | const IntegerType *OrigTy = cast<IntegerType>(V->getType()); |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6543 | |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 6544 | // If this is an extension or truncate, we can often eliminate it. |
| 6545 | if (isa<TruncInst>(I) || isa<ZExtInst>(I) || isa<SExtInst>(I)) { |
| 6546 | // If this is a cast from the destination type, we can trivially eliminate |
| 6547 | // it, and this will remove a cast overall. |
| 6548 | if (I->getOperand(0)->getType() == Ty) { |
| 6549 | // If the first operand is itself a cast, and is eliminable, do not count |
| 6550 | // this as an eliminable cast. We would prefer to eliminate those two |
| 6551 | // casts first. |
| 6552 | if (!isa<CastInst>(I->getOperand(0))) |
| 6553 | ++NumCastsRemoved; |
| 6554 | return true; |
| 6555 | } |
| 6556 | } |
| 6557 | |
| 6558 | // We can't extend or shrink something that has multiple uses: doing so would |
| 6559 | // require duplicating the instruction in general, which isn't profitable. |
| 6560 | if (!I->hasOneUse()) return false; |
| 6561 | |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6562 | switch (I->getOpcode()) { |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6563 | case Instruction::Add: |
| 6564 | case Instruction::Sub: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6565 | case Instruction::And: |
| 6566 | case Instruction::Or: |
| 6567 | case Instruction::Xor: |
| 6568 | // These operators can all arbitrarily be extended or truncated. |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 6569 | return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc, |
| 6570 | NumCastsRemoved) && |
| 6571 | CanEvaluateInDifferentType(I->getOperand(1), Ty, CastOpc, |
| 6572 | NumCastsRemoved); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6573 | |
Nick Lewycky | e6b0c00 | 2008-01-22 05:08:48 +0000 | [diff] [blame] | 6574 | case Instruction::Mul: |
Nick Lewycky | e6b0c00 | 2008-01-22 05:08:48 +0000 | [diff] [blame] | 6575 | // A multiply can be truncated by truncating its operands. |
| 6576 | return Ty->getBitWidth() < OrigTy->getBitWidth() && |
| 6577 | CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc, |
| 6578 | NumCastsRemoved) && |
| 6579 | CanEvaluateInDifferentType(I->getOperand(1), Ty, CastOpc, |
| 6580 | NumCastsRemoved); |
| 6581 | |
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 6582 | case Instruction::Shl: |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6583 | // If we are truncating the result of this SHL, and if it's a shift of a |
| 6584 | // constant amount, we can always perform a SHL in a smaller type. |
| 6585 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 6586 | uint32_t BitWidth = Ty->getBitWidth(); |
| 6587 | if (BitWidth < OrigTy->getBitWidth() && |
| 6588 | CI->getLimitedValue(BitWidth) < BitWidth) |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 6589 | return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc, |
| 6590 | NumCastsRemoved); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6591 | } |
| 6592 | break; |
| 6593 | case Instruction::LShr: |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6594 | // If this is a truncate of a logical shr, we can truncate it to a smaller |
| 6595 | // lshr iff we know that the bits we would otherwise be shifting in are |
| 6596 | // already zeros. |
| 6597 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 6598 | uint32_t OrigBitWidth = OrigTy->getBitWidth(); |
| 6599 | uint32_t BitWidth = Ty->getBitWidth(); |
| 6600 | if (BitWidth < OrigBitWidth && |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6601 | MaskedValueIsZero(I->getOperand(0), |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 6602 | APInt::getHighBitsSet(OrigBitWidth, OrigBitWidth-BitWidth)) && |
| 6603 | CI->getLimitedValue(BitWidth) < BitWidth) { |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 6604 | return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc, |
| 6605 | NumCastsRemoved); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6606 | } |
| 6607 | } |
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 6608 | break; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6609 | case Instruction::ZExt: |
| 6610 | case Instruction::SExt: |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 6611 | case Instruction::Trunc: |
| 6612 | // 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] | 6613 | // can safely replace it. Note that replacing it does not reduce the number |
| 6614 | // of casts in the input. |
| 6615 | if (I->getOpcode() == CastOpc) |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6616 | return true; |
Chris Lattner | 50d9d77 | 2007-09-10 23:46:29 +0000 | [diff] [blame] | 6617 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6618 | break; |
| 6619 | default: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6620 | // TODO: Can handle more cases here. |
| 6621 | break; |
| 6622 | } |
| 6623 | |
| 6624 | return false; |
| 6625 | } |
| 6626 | |
| 6627 | /// EvaluateInDifferentType - Given an expression that |
| 6628 | /// CanEvaluateInDifferentType returns true for, actually insert the code to |
| 6629 | /// evaluate the expression. |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 6630 | Value *InstCombiner::EvaluateInDifferentType(Value *V, const Type *Ty, |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6631 | bool isSigned) { |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6632 | if (Constant *C = dyn_cast<Constant>(V)) |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 6633 | return ConstantExpr::getIntegerCast(C, Ty, isSigned /*Sext or ZExt*/); |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6634 | |
| 6635 | // Otherwise, it must be an instruction. |
| 6636 | Instruction *I = cast<Instruction>(V); |
Chris Lattner | 01859e8 | 2006-05-20 23:14:03 +0000 | [diff] [blame] | 6637 | Instruction *Res = 0; |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6638 | switch (I->getOpcode()) { |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6639 | case Instruction::Add: |
| 6640 | case Instruction::Sub: |
Nick Lewycky | e6b0c00 | 2008-01-22 05:08:48 +0000 | [diff] [blame] | 6641 | case Instruction::Mul: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6642 | case Instruction::And: |
| 6643 | case Instruction::Or: |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6644 | case Instruction::Xor: |
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 6645 | case Instruction::AShr: |
| 6646 | case Instruction::LShr: |
| 6647 | case Instruction::Shl: { |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 6648 | Value *LHS = EvaluateInDifferentType(I->getOperand(0), Ty, isSigned); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6649 | Value *RHS = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned); |
| 6650 | Res = BinaryOperator::create((Instruction::BinaryOps)I->getOpcode(), |
| 6651 | LHS, RHS, I->getName()); |
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 6652 | break; |
| 6653 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6654 | case Instruction::Trunc: |
| 6655 | case Instruction::ZExt: |
| 6656 | case Instruction::SExt: |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6657 | // 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] | 6658 | // just return the source. There's no need to insert it because it is not |
| 6659 | // new. |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6660 | if (I->getOperand(0)->getType() == Ty) |
| 6661 | return I->getOperand(0); |
| 6662 | |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 6663 | // Otherwise, must be the same type of case, so just reinsert a new one. |
| 6664 | Res = CastInst::create(cast<CastInst>(I)->getOpcode(), I->getOperand(0), |
| 6665 | Ty, I->getName()); |
| 6666 | break; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6667 | default: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6668 | // TODO: Can handle more cases here. |
| 6669 | assert(0 && "Unreachable!"); |
| 6670 | break; |
| 6671 | } |
| 6672 | |
| 6673 | return InsertNewInstBefore(Res, *I); |
| 6674 | } |
| 6675 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6676 | /// @brief Implement the transforms common to all CastInst visitors. |
| 6677 | Instruction *InstCombiner::commonCastTransforms(CastInst &CI) { |
Chris Lattner | 79d35b3 | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 6678 | Value *Src = CI.getOperand(0); |
| 6679 | |
Dan Gohman | 23d9d27 | 2007-05-11 21:10:54 +0000 | [diff] [blame] | 6680 | // 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] | 6681 | // eliminate it now. |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 6682 | if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6683 | if (Instruction::CastOps opc = |
| 6684 | isEliminableCastPair(CSrc, CI.getOpcode(), CI.getType(), TD)) { |
| 6685 | // The first cast (CSrc) is eliminable so we need to fix up or replace |
| 6686 | // the second cast (CI). CSrc will then have a good chance of being dead. |
| 6687 | return CastInst::create(opc, CSrc->getOperand(0), CI.getType()); |
Chris Lattner | 8fd217c | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 6688 | } |
| 6689 | } |
Chris Lattner | a710ddc | 2004-05-25 04:29:21 +0000 | [diff] [blame] | 6690 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6691 | // 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] | 6692 | if (SelectInst *SI = dyn_cast<SelectInst>(Src)) |
| 6693 | if (Instruction *NV = FoldOpIntoSelect(CI, SI, this)) |
| 6694 | return NV; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6695 | |
| 6696 | // 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] | 6697 | if (isa<PHINode>(Src)) |
| 6698 | if (Instruction *NV = FoldOpIntoPhi(CI)) |
| 6699 | return NV; |
Chris Lattner | 9fb9213 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 6700 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6701 | return 0; |
| 6702 | } |
| 6703 | |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 6704 | /// @brief Implement the transforms for cast of pointer (bitcast/ptrtoint) |
| 6705 | Instruction *InstCombiner::commonPointerCastTransforms(CastInst &CI) { |
| 6706 | Value *Src = CI.getOperand(0); |
| 6707 | |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 6708 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Src)) { |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 6709 | // If casting the result of a getelementptr instruction with no offset, turn |
| 6710 | // this into a cast of the original pointer! |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 6711 | if (GEP->hasAllZeroIndices()) { |
| 6712 | // Changing the cast operand is usually not a good idea but it is safe |
| 6713 | // here because the pointer operand is being replaced with another |
| 6714 | // pointer operand so the opcode doesn't need to change. |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 6715 | AddToWorkList(GEP); |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 6716 | CI.setOperand(0, GEP->getOperand(0)); |
| 6717 | return &CI; |
| 6718 | } |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 6719 | |
| 6720 | // If the GEP has a single use, and the base pointer is a bitcast, and the |
| 6721 | // GEP computes a constant offset, see if we can convert these three |
| 6722 | // instructions into fewer. This typically happens with unions and other |
| 6723 | // non-type-safe code. |
| 6724 | if (GEP->hasOneUse() && isa<BitCastInst>(GEP->getOperand(0))) { |
| 6725 | if (GEP->hasAllConstantIndices()) { |
| 6726 | // We are guaranteed to get a constant from EmitGEPOffset. |
| 6727 | ConstantInt *OffsetV = cast<ConstantInt>(EmitGEPOffset(GEP, CI, *this)); |
| 6728 | int64_t Offset = OffsetV->getSExtValue(); |
| 6729 | |
| 6730 | // Get the base pointer input of the bitcast, and the type it points to. |
| 6731 | Value *OrigBase = cast<BitCastInst>(GEP->getOperand(0))->getOperand(0); |
| 6732 | const Type *GEPIdxTy = |
| 6733 | cast<PointerType>(OrigBase->getType())->getElementType(); |
| 6734 | if (GEPIdxTy->isSized()) { |
| 6735 | SmallVector<Value*, 8> NewIndices; |
| 6736 | |
Chris Lattner | c42e226 | 2007-05-05 01:59:31 +0000 | [diff] [blame] | 6737 | // Start with the index over the outer type. Note that the type size |
| 6738 | // might be zero (even if the offset isn't zero) if the indexed type |
| 6739 | // is something like [0 x {int, int}] |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 6740 | const Type *IntPtrTy = TD->getIntPtrType(); |
Chris Lattner | c42e226 | 2007-05-05 01:59:31 +0000 | [diff] [blame] | 6741 | int64_t FirstIdx = 0; |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 6742 | if (int64_t TySize = TD->getABITypeSize(GEPIdxTy)) { |
Chris Lattner | c42e226 | 2007-05-05 01:59:31 +0000 | [diff] [blame] | 6743 | FirstIdx = Offset/TySize; |
| 6744 | Offset %= TySize; |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 6745 | |
Chris Lattner | c42e226 | 2007-05-05 01:59:31 +0000 | [diff] [blame] | 6746 | // Handle silly modulus not returning values values [0..TySize). |
| 6747 | if (Offset < 0) { |
| 6748 | --FirstIdx; |
| 6749 | Offset += TySize; |
| 6750 | assert(Offset >= 0); |
| 6751 | } |
Chris Lattner | d717c18 | 2007-05-05 22:32:24 +0000 | [diff] [blame] | 6752 | assert((uint64_t)Offset < (uint64_t)TySize &&"Out of range offset"); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 6753 | } |
| 6754 | |
| 6755 | NewIndices.push_back(ConstantInt::get(IntPtrTy, FirstIdx)); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 6756 | |
| 6757 | // Index into the types. If we fail, set OrigBase to null. |
| 6758 | while (Offset) { |
| 6759 | if (const StructType *STy = dyn_cast<StructType>(GEPIdxTy)) { |
| 6760 | const StructLayout *SL = TD->getStructLayout(STy); |
Chris Lattner | 6b6aef8 | 2007-05-15 00:16:00 +0000 | [diff] [blame] | 6761 | if (Offset < (int64_t)SL->getSizeInBytes()) { |
| 6762 | unsigned Elt = SL->getElementContainingOffset(Offset); |
| 6763 | NewIndices.push_back(ConstantInt::get(Type::Int32Ty, Elt)); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 6764 | |
Chris Lattner | 6b6aef8 | 2007-05-15 00:16:00 +0000 | [diff] [blame] | 6765 | Offset -= SL->getElementOffset(Elt); |
| 6766 | GEPIdxTy = STy->getElementType(Elt); |
| 6767 | } else { |
| 6768 | // Otherwise, we can't index into this, bail out. |
| 6769 | Offset = 0; |
| 6770 | OrigBase = 0; |
| 6771 | } |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 6772 | } else if (isa<ArrayType>(GEPIdxTy) || isa<VectorType>(GEPIdxTy)) { |
| 6773 | const SequentialType *STy = cast<SequentialType>(GEPIdxTy); |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 6774 | if (uint64_t EltSize = TD->getABITypeSize(STy->getElementType())){ |
Chris Lattner | 6b6aef8 | 2007-05-15 00:16:00 +0000 | [diff] [blame] | 6775 | NewIndices.push_back(ConstantInt::get(IntPtrTy,Offset/EltSize)); |
| 6776 | Offset %= EltSize; |
| 6777 | } else { |
| 6778 | NewIndices.push_back(ConstantInt::get(IntPtrTy, 0)); |
| 6779 | } |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 6780 | GEPIdxTy = STy->getElementType(); |
| 6781 | } else { |
| 6782 | // Otherwise, we can't index into this, bail out. |
| 6783 | Offset = 0; |
| 6784 | OrigBase = 0; |
| 6785 | } |
| 6786 | } |
| 6787 | if (OrigBase) { |
| 6788 | // If we were able to index down into an element, create the GEP |
| 6789 | // and bitcast the result. This eliminates one bitcast, potentially |
| 6790 | // two. |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 6791 | Instruction *NGEP = new GetElementPtrInst(OrigBase, |
| 6792 | NewIndices.begin(), |
| 6793 | NewIndices.end(), ""); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 6794 | InsertNewInstBefore(NGEP, CI); |
| 6795 | NGEP->takeName(GEP); |
| 6796 | |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 6797 | if (isa<BitCastInst>(CI)) |
| 6798 | return new BitCastInst(NGEP, CI.getType()); |
| 6799 | assert(isa<PtrToIntInst>(CI)); |
| 6800 | return new PtrToIntInst(NGEP, CI.getType()); |
| 6801 | } |
| 6802 | } |
| 6803 | } |
| 6804 | } |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 6805 | } |
| 6806 | |
| 6807 | return commonCastTransforms(CI); |
| 6808 | } |
| 6809 | |
| 6810 | |
| 6811 | |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6812 | /// Only the TRUNC, ZEXT, SEXT, and BITCAST can both operand and result as |
| 6813 | /// integer types. This function implements the common transforms for all those |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6814 | /// cases. |
| 6815 | /// @brief Implement the transforms common to CastInst with integer operands |
| 6816 | Instruction *InstCombiner::commonIntCastTransforms(CastInst &CI) { |
| 6817 | if (Instruction *Result = commonCastTransforms(CI)) |
| 6818 | return Result; |
| 6819 | |
| 6820 | Value *Src = CI.getOperand(0); |
| 6821 | const Type *SrcTy = Src->getType(); |
| 6822 | const Type *DestTy = CI.getType(); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 6823 | uint32_t SrcBitSize = SrcTy->getPrimitiveSizeInBits(); |
| 6824 | uint32_t DestBitSize = DestTy->getPrimitiveSizeInBits(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6825 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6826 | // See if we can simplify any instructions used by the LHS whose sole |
| 6827 | // purpose is to compute bits we don't care about. |
Reid Spencer | ad6676e | 2007-03-22 20:56:53 +0000 | [diff] [blame] | 6828 | APInt KnownZero(DestBitSize, 0), KnownOne(DestBitSize, 0); |
| 6829 | if (SimplifyDemandedBits(&CI, APInt::getAllOnesValue(DestBitSize), |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6830 | KnownZero, KnownOne)) |
| 6831 | return &CI; |
| 6832 | |
| 6833 | // If the source isn't an instruction or has more than one use then we |
| 6834 | // can't do anything more. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6835 | Instruction *SrcI = dyn_cast<Instruction>(Src); |
| 6836 | if (!SrcI || !Src->hasOneUse()) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6837 | return 0; |
| 6838 | |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6839 | // Attempt to propagate the cast into the instruction for int->int casts. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6840 | int NumCastsRemoved = 0; |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6841 | if (!isa<BitCastInst>(CI) && |
| 6842 | CanEvaluateInDifferentType(SrcI, cast<IntegerType>(DestTy), |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 6843 | CI.getOpcode(), NumCastsRemoved)) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6844 | // If this cast is a truncate, evaluting in a different type always |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 6845 | // eliminates the cast, so it is always a win. If this is a zero-extension, |
| 6846 | // we need to do an AND to maintain the clear top-part of the computation, |
| 6847 | // so we require that the input have eliminated at least one cast. If this |
| 6848 | // 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] | 6849 | // require that two casts have been eliminated. |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6850 | bool DoXForm; |
| 6851 | switch (CI.getOpcode()) { |
| 6852 | default: |
| 6853 | // All the others use floating point so we shouldn't actually |
| 6854 | // get here because of the check above. |
| 6855 | assert(0 && "Unknown cast type"); |
| 6856 | case Instruction::Trunc: |
| 6857 | DoXForm = true; |
| 6858 | break; |
| 6859 | case Instruction::ZExt: |
| 6860 | DoXForm = NumCastsRemoved >= 1; |
| 6861 | break; |
| 6862 | case Instruction::SExt: |
| 6863 | DoXForm = NumCastsRemoved >= 2; |
| 6864 | break; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6865 | } |
| 6866 | |
| 6867 | if (DoXForm) { |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 6868 | Value *Res = EvaluateInDifferentType(SrcI, DestTy, |
| 6869 | CI.getOpcode() == Instruction::SExt); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6870 | assert(Res->getType() == DestTy); |
| 6871 | switch (CI.getOpcode()) { |
| 6872 | default: assert(0 && "Unknown cast type!"); |
| 6873 | case Instruction::Trunc: |
| 6874 | case Instruction::BitCast: |
| 6875 | // Just replace this cast with the result. |
| 6876 | return ReplaceInstUsesWith(CI, Res); |
| 6877 | case Instruction::ZExt: { |
| 6878 | // We need to emit an AND to clear the high bits. |
| 6879 | assert(SrcBitSize < DestBitSize && "Not a zext?"); |
Chris Lattner | cd1d6d5 | 2007-04-02 05:48:58 +0000 | [diff] [blame] | 6880 | Constant *C = ConstantInt::get(APInt::getLowBitsSet(DestBitSize, |
| 6881 | SrcBitSize)); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6882 | return BinaryOperator::createAnd(Res, C); |
| 6883 | } |
| 6884 | case Instruction::SExt: |
| 6885 | // We need to emit a cast to truncate, then a cast to sext. |
| 6886 | return CastInst::create(Instruction::SExt, |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 6887 | InsertCastBefore(Instruction::Trunc, Res, Src->getType(), |
| 6888 | CI), DestTy); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6889 | } |
| 6890 | } |
| 6891 | } |
| 6892 | |
| 6893 | Value *Op0 = SrcI->getNumOperands() > 0 ? SrcI->getOperand(0) : 0; |
| 6894 | Value *Op1 = SrcI->getNumOperands() > 1 ? SrcI->getOperand(1) : 0; |
| 6895 | |
| 6896 | switch (SrcI->getOpcode()) { |
| 6897 | case Instruction::Add: |
| 6898 | case Instruction::Mul: |
| 6899 | case Instruction::And: |
| 6900 | case Instruction::Or: |
| 6901 | case Instruction::Xor: |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6902 | // If we are discarding information, rewrite. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6903 | if (DestBitSize <= SrcBitSize && DestBitSize != 1) { |
| 6904 | // Don't insert two casts if they cannot be eliminated. We allow |
| 6905 | // two casts to be inserted if the sizes are the same. This could |
| 6906 | // only be converting signedness, which is a noop. |
| 6907 | if (DestBitSize == SrcBitSize || |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6908 | !ValueRequiresCast(CI.getOpcode(), Op1, DestTy,TD) || |
| 6909 | !ValueRequiresCast(CI.getOpcode(), Op0, DestTy, TD)) { |
Reid Spencer | 7eb7638 | 2006-12-13 17:19:09 +0000 | [diff] [blame] | 6910 | Instruction::CastOps opcode = CI.getOpcode(); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 6911 | Value *Op0c = InsertOperandCastBefore(opcode, Op0, DestTy, SrcI); |
| 6912 | Value *Op1c = InsertOperandCastBefore(opcode, Op1, DestTy, SrcI); |
| 6913 | return BinaryOperator::create( |
| 6914 | cast<BinaryOperator>(SrcI)->getOpcode(), Op0c, Op1c); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6915 | } |
| 6916 | } |
| 6917 | |
| 6918 | // cast (xor bool X, true) to int --> xor (cast bool X to int), 1 |
| 6919 | if (isa<ZExtInst>(CI) && SrcBitSize == 1 && |
| 6920 | SrcI->getOpcode() == Instruction::Xor && |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6921 | Op1 == ConstantInt::getTrue() && |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6922 | (!Op0->hasOneUse() || !isa<CmpInst>(Op0))) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 6923 | Value *New = InsertOperandCastBefore(Instruction::ZExt, Op0, DestTy, &CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6924 | return BinaryOperator::createXor(New, ConstantInt::get(CI.getType(), 1)); |
| 6925 | } |
| 6926 | break; |
| 6927 | case Instruction::SDiv: |
| 6928 | case Instruction::UDiv: |
| 6929 | case Instruction::SRem: |
| 6930 | case Instruction::URem: |
| 6931 | // If we are just changing the sign, rewrite. |
| 6932 | if (DestBitSize == SrcBitSize) { |
| 6933 | // Don't insert two casts if they cannot be eliminated. We allow |
| 6934 | // two casts to be inserted if the sizes are the same. This could |
| 6935 | // only be converting signedness, which is a noop. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6936 | if (!ValueRequiresCast(CI.getOpcode(), Op1, DestTy, TD) || |
| 6937 | !ValueRequiresCast(CI.getOpcode(), Op0, DestTy, TD)) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 6938 | Value *Op0c = InsertOperandCastBefore(Instruction::BitCast, |
| 6939 | Op0, DestTy, SrcI); |
| 6940 | Value *Op1c = InsertOperandCastBefore(Instruction::BitCast, |
| 6941 | Op1, DestTy, SrcI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6942 | return BinaryOperator::create( |
| 6943 | cast<BinaryOperator>(SrcI)->getOpcode(), Op0c, Op1c); |
| 6944 | } |
| 6945 | } |
| 6946 | break; |
| 6947 | |
| 6948 | case Instruction::Shl: |
| 6949 | // Allow changing the sign of the source operand. Do not allow |
| 6950 | // changing the size of the shift, UNLESS the shift amount is a |
| 6951 | // constant. We must not change variable sized shifts to a smaller |
| 6952 | // size, because it is undefined to shift more bits out than exist |
| 6953 | // in the value. |
| 6954 | if (DestBitSize == SrcBitSize || |
| 6955 | (DestBitSize < SrcBitSize && isa<Constant>(Op1))) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 6956 | Instruction::CastOps opcode = (DestBitSize == SrcBitSize ? |
| 6957 | Instruction::BitCast : Instruction::Trunc); |
| 6958 | Value *Op0c = InsertOperandCastBefore(opcode, Op0, DestTy, SrcI); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6959 | Value *Op1c = InsertOperandCastBefore(opcode, Op1, DestTy, SrcI); |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 6960 | return BinaryOperator::createShl(Op0c, Op1c); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6961 | } |
| 6962 | break; |
| 6963 | case Instruction::AShr: |
| 6964 | // If this is a signed shr, and if all bits shifted in are about to be |
| 6965 | // truncated off, turn it into an unsigned shr to allow greater |
| 6966 | // simplifications. |
| 6967 | if (DestBitSize < SrcBitSize && |
| 6968 | isa<ConstantInt>(Op1)) { |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 6969 | uint32_t ShiftAmt = cast<ConstantInt>(Op1)->getLimitedValue(SrcBitSize); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6970 | if (SrcBitSize > ShiftAmt && SrcBitSize-ShiftAmt >= DestBitSize) { |
| 6971 | // Insert the new logical shift right. |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 6972 | return BinaryOperator::createLShr(Op0, Op1); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6973 | } |
| 6974 | } |
| 6975 | break; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6976 | } |
| 6977 | return 0; |
| 6978 | } |
| 6979 | |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 6980 | Instruction *InstCombiner::visitTrunc(TruncInst &CI) { |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 6981 | if (Instruction *Result = commonIntCastTransforms(CI)) |
| 6982 | return Result; |
| 6983 | |
| 6984 | Value *Src = CI.getOperand(0); |
| 6985 | const Type *Ty = CI.getType(); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 6986 | uint32_t DestBitWidth = Ty->getPrimitiveSizeInBits(); |
| 6987 | uint32_t SrcBitWidth = cast<IntegerType>(Src->getType())->getBitWidth(); |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 6988 | |
| 6989 | if (Instruction *SrcI = dyn_cast<Instruction>(Src)) { |
| 6990 | switch (SrcI->getOpcode()) { |
| 6991 | default: break; |
| 6992 | case Instruction::LShr: |
| 6993 | // We can shrink lshr to something smaller if we know the bits shifted in |
| 6994 | // are already zeros. |
| 6995 | if (ConstantInt *ShAmtV = dyn_cast<ConstantInt>(SrcI->getOperand(1))) { |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 6996 | uint32_t ShAmt = ShAmtV->getLimitedValue(SrcBitWidth); |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 6997 | |
| 6998 | // Get a mask for the bits shifting in. |
Zhou Sheng | e82fca0 | 2007-03-28 09:19:01 +0000 | [diff] [blame] | 6999 | APInt Mask(APInt::getLowBitsSet(SrcBitWidth, ShAmt).shl(DestBitWidth)); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7000 | Value* SrcIOp0 = SrcI->getOperand(0); |
| 7001 | if (SrcI->hasOneUse() && MaskedValueIsZero(SrcIOp0, Mask)) { |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 7002 | if (ShAmt >= DestBitWidth) // All zeros. |
| 7003 | return ReplaceInstUsesWith(CI, Constant::getNullValue(Ty)); |
| 7004 | |
| 7005 | // Okay, we can shrink this. Truncate the input, then return a new |
| 7006 | // shift. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7007 | Value *V1 = InsertCastBefore(Instruction::Trunc, SrcIOp0, Ty, CI); |
| 7008 | Value *V2 = InsertCastBefore(Instruction::Trunc, SrcI->getOperand(1), |
| 7009 | Ty, CI); |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 7010 | return BinaryOperator::createLShr(V1, V2); |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 7011 | } |
Chris Lattner | e13ab2a | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 7012 | } else { // This is a variable shr. |
| 7013 | |
| 7014 | // Turn 'trunc (lshr X, Y) to bool' into '(X & (1 << Y)) != 0'. This is |
| 7015 | // more LLVM instructions, but allows '1 << Y' to be hoisted if |
| 7016 | // loop-invariant and CSE'd. |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 7017 | if (CI.getType() == Type::Int1Ty && SrcI->hasOneUse()) { |
Chris Lattner | e13ab2a | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 7018 | Value *One = ConstantInt::get(SrcI->getType(), 1); |
| 7019 | |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7020 | Value *V = InsertNewInstBefore( |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 7021 | BinaryOperator::createShl(One, SrcI->getOperand(1), |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7022 | "tmp"), CI); |
Chris Lattner | e13ab2a | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 7023 | V = InsertNewInstBefore(BinaryOperator::createAnd(V, |
| 7024 | SrcI->getOperand(0), |
| 7025 | "tmp"), CI); |
| 7026 | Value *Zero = Constant::getNullValue(V->getType()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7027 | return new ICmpInst(ICmpInst::ICMP_NE, V, Zero); |
Chris Lattner | e13ab2a | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 7028 | } |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 7029 | } |
| 7030 | break; |
| 7031 | } |
| 7032 | } |
| 7033 | |
| 7034 | return 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7035 | } |
| 7036 | |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 7037 | Instruction *InstCombiner::visitZExt(ZExtInst &CI) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7038 | // If one of the common conversion will work .. |
| 7039 | if (Instruction *Result = commonIntCastTransforms(CI)) |
| 7040 | return Result; |
| 7041 | |
| 7042 | Value *Src = CI.getOperand(0); |
| 7043 | |
| 7044 | // If this is a cast of a cast |
| 7045 | if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7046 | // If this is a TRUNC followed by a ZEXT then we are dealing with integral |
| 7047 | // types and if the sizes are just right we can convert this into a logical |
| 7048 | // 'and' which will be much cheaper than the pair of casts. |
| 7049 | if (isa<TruncInst>(CSrc)) { |
| 7050 | // Get the sizes of the types involved |
| 7051 | Value *A = CSrc->getOperand(0); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 7052 | uint32_t SrcSize = A->getType()->getPrimitiveSizeInBits(); |
| 7053 | uint32_t MidSize = CSrc->getType()->getPrimitiveSizeInBits(); |
| 7054 | uint32_t DstSize = CI.getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7055 | // If we're actually extending zero bits and the trunc is a no-op |
| 7056 | if (MidSize < DstSize && SrcSize == DstSize) { |
| 7057 | // Replace both of the casts with an And of the type mask. |
Zhou Sheng | e82fca0 | 2007-03-28 09:19:01 +0000 | [diff] [blame] | 7058 | APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize)); |
Reid Spencer | ad6676e | 2007-03-22 20:56:53 +0000 | [diff] [blame] | 7059 | Constant *AndConst = ConstantInt::get(AndValue); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7060 | Instruction *And = |
| 7061 | BinaryOperator::createAnd(CSrc->getOperand(0), AndConst); |
| 7062 | // Unfortunately, if the type changed, we need to cast it back. |
| 7063 | if (And->getType() != CI.getType()) { |
| 7064 | And->setName(CSrc->getName()+".mask"); |
| 7065 | InsertNewInstBefore(And, CI); |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 7066 | And = CastInst::createIntegerCast(And, CI.getType(), false/*ZExt*/); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7067 | } |
| 7068 | return And; |
| 7069 | } |
| 7070 | } |
| 7071 | } |
| 7072 | |
Chris Lattner | 66bc325 | 2007-04-11 05:45:39 +0000 | [diff] [blame] | 7073 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(Src)) { |
| 7074 | // If we are just checking for a icmp eq of a single bit and zext'ing it |
| 7075 | // to an integer, then shift the bit to the appropriate place and then |
| 7076 | // cast to integer to avoid the comparison. |
| 7077 | if (ConstantInt *Op1C = dyn_cast<ConstantInt>(ICI->getOperand(1))) { |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 7078 | const APInt &Op1CV = Op1C->getValue(); |
Chris Lattner | a2e2c9b | 2007-04-11 06:53:04 +0000 | [diff] [blame] | 7079 | |
| 7080 | // zext (x <s 0) to i32 --> x>>u31 true if signbit set. |
| 7081 | // zext (x >s -1) to i32 --> (x>>u31)^1 true if signbit clear. |
| 7082 | if ((ICI->getPredicate() == ICmpInst::ICMP_SLT && Op1CV == 0) || |
| 7083 | (ICI->getPredicate() == ICmpInst::ICMP_SGT &&Op1CV.isAllOnesValue())){ |
| 7084 | Value *In = ICI->getOperand(0); |
| 7085 | Value *Sh = ConstantInt::get(In->getType(), |
| 7086 | In->getType()->getPrimitiveSizeInBits()-1); |
| 7087 | In = InsertNewInstBefore(BinaryOperator::createLShr(In, Sh, |
Chris Lattner | e34e9a2 | 2007-04-14 23:32:02 +0000 | [diff] [blame] | 7088 | In->getName()+".lobit"), |
Chris Lattner | a2e2c9b | 2007-04-11 06:53:04 +0000 | [diff] [blame] | 7089 | CI); |
| 7090 | if (In->getType() != CI.getType()) |
| 7091 | In = CastInst::createIntegerCast(In, CI.getType(), |
| 7092 | false/*ZExt*/, "tmp", &CI); |
| 7093 | |
| 7094 | if (ICI->getPredicate() == ICmpInst::ICMP_SGT) { |
| 7095 | Constant *One = ConstantInt::get(In->getType(), 1); |
| 7096 | In = InsertNewInstBefore(BinaryOperator::createXor(In, One, |
Chris Lattner | e34e9a2 | 2007-04-14 23:32:02 +0000 | [diff] [blame] | 7097 | In->getName()+".not"), |
Chris Lattner | a2e2c9b | 2007-04-11 06:53:04 +0000 | [diff] [blame] | 7098 | CI); |
| 7099 | } |
| 7100 | |
| 7101 | return ReplaceInstUsesWith(CI, In); |
| 7102 | } |
| 7103 | |
| 7104 | |
| 7105 | |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 7106 | // zext (X == 0) to i32 --> X^1 iff X has only the low bit set. |
| 7107 | // zext (X == 0) to i32 --> (X>>1)^1 iff X has only the 2nd bit set. |
| 7108 | // zext (X == 1) to i32 --> X iff X has only the low bit set. |
| 7109 | // zext (X == 2) to i32 --> X>>1 iff X has only the 2nd bit set. |
| 7110 | // zext (X != 0) to i32 --> X iff X has only the low bit set. |
| 7111 | // zext (X != 0) to i32 --> X>>1 iff X has only the 2nd bit set. |
| 7112 | // zext (X != 1) to i32 --> X^1 iff X has only the low bit set. |
| 7113 | // 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] | 7114 | if ((Op1CV == 0 || Op1CV.isPowerOf2()) && |
| 7115 | // This only works for EQ and NE |
| 7116 | ICI->isEquality()) { |
| 7117 | // If Op1C some other power of two, convert: |
| 7118 | uint32_t BitWidth = Op1C->getType()->getBitWidth(); |
| 7119 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 7120 | APInt TypeMask(APInt::getAllOnesValue(BitWidth)); |
| 7121 | ComputeMaskedBits(ICI->getOperand(0), TypeMask, KnownZero, KnownOne); |
| 7122 | |
| 7123 | APInt KnownZeroMask(~KnownZero); |
| 7124 | if (KnownZeroMask.isPowerOf2()) { // Exactly 1 possible 1? |
| 7125 | bool isNE = ICI->getPredicate() == ICmpInst::ICMP_NE; |
| 7126 | if (Op1CV != 0 && (Op1CV != KnownZeroMask)) { |
| 7127 | // (X&4) == 2 --> false |
| 7128 | // (X&4) != 2 --> true |
| 7129 | Constant *Res = ConstantInt::get(Type::Int1Ty, isNE); |
| 7130 | Res = ConstantExpr::getZExt(Res, CI.getType()); |
| 7131 | return ReplaceInstUsesWith(CI, Res); |
| 7132 | } |
| 7133 | |
| 7134 | uint32_t ShiftAmt = KnownZeroMask.logBase2(); |
| 7135 | Value *In = ICI->getOperand(0); |
| 7136 | if (ShiftAmt) { |
| 7137 | // Perform a logical shr by shiftamt. |
| 7138 | // Insert the shift to put the result in the low bit. |
| 7139 | In = InsertNewInstBefore( |
| 7140 | BinaryOperator::createLShr(In, |
| 7141 | ConstantInt::get(In->getType(), ShiftAmt), |
| 7142 | In->getName()+".lobit"), CI); |
| 7143 | } |
| 7144 | |
| 7145 | if ((Op1CV != 0) == isNE) { // Toggle the low bit. |
| 7146 | Constant *One = ConstantInt::get(In->getType(), 1); |
| 7147 | In = BinaryOperator::createXor(In, One, "tmp"); |
| 7148 | InsertNewInstBefore(cast<Instruction>(In), CI); |
| 7149 | } |
| 7150 | |
| 7151 | if (CI.getType() == In->getType()) |
| 7152 | return ReplaceInstUsesWith(CI, In); |
| 7153 | else |
| 7154 | return CastInst::createIntegerCast(In, CI.getType(), false/*ZExt*/); |
| 7155 | } |
| 7156 | } |
| 7157 | } |
| 7158 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7159 | return 0; |
| 7160 | } |
| 7161 | |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 7162 | Instruction *InstCombiner::visitSExt(SExtInst &CI) { |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 7163 | if (Instruction *I = commonIntCastTransforms(CI)) |
| 7164 | return I; |
| 7165 | |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 7166 | Value *Src = CI.getOperand(0); |
| 7167 | |
| 7168 | // sext (x <s 0) -> ashr x, 31 -> all ones if signed |
| 7169 | // sext (x >s -1) -> ashr x, 31 -> all ones if not signed |
| 7170 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(Src)) { |
| 7171 | // If we are just checking for a icmp eq of a single bit and zext'ing it |
| 7172 | // to an integer, then shift the bit to the appropriate place and then |
| 7173 | // cast to integer to avoid the comparison. |
| 7174 | if (ConstantInt *Op1C = dyn_cast<ConstantInt>(ICI->getOperand(1))) { |
| 7175 | const APInt &Op1CV = Op1C->getValue(); |
| 7176 | |
| 7177 | // sext (x <s 0) to i32 --> x>>s31 true if signbit set. |
| 7178 | // sext (x >s -1) to i32 --> (x>>s31)^-1 true if signbit clear. |
| 7179 | if ((ICI->getPredicate() == ICmpInst::ICMP_SLT && Op1CV == 0) || |
| 7180 | (ICI->getPredicate() == ICmpInst::ICMP_SGT &&Op1CV.isAllOnesValue())){ |
| 7181 | Value *In = ICI->getOperand(0); |
| 7182 | Value *Sh = ConstantInt::get(In->getType(), |
| 7183 | In->getType()->getPrimitiveSizeInBits()-1); |
| 7184 | In = InsertNewInstBefore(BinaryOperator::createAShr(In, Sh, |
Chris Lattner | e34e9a2 | 2007-04-14 23:32:02 +0000 | [diff] [blame] | 7185 | In->getName()+".lobit"), |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 7186 | CI); |
| 7187 | if (In->getType() != CI.getType()) |
| 7188 | In = CastInst::createIntegerCast(In, CI.getType(), |
| 7189 | true/*SExt*/, "tmp", &CI); |
| 7190 | |
| 7191 | if (ICI->getPredicate() == ICmpInst::ICMP_SGT) |
| 7192 | In = InsertNewInstBefore(BinaryOperator::createNot(In, |
| 7193 | In->getName()+".not"), CI); |
| 7194 | |
| 7195 | return ReplaceInstUsesWith(CI, In); |
| 7196 | } |
| 7197 | } |
| 7198 | } |
| 7199 | |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 7200 | return 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7201 | } |
| 7202 | |
Chris Lattner | b753065 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 7203 | /// FitsInFPType - Return a Constant* for the specified FP constant if it fits |
| 7204 | /// in the specified FP type without changing its value. |
| 7205 | static Constant *FitsInFPType(ConstantFP *CFP, const Type *FPTy, |
| 7206 | const fltSemantics &Sem) { |
| 7207 | APFloat F = CFP->getValueAPF(); |
| 7208 | if (F.convert(Sem, APFloat::rmNearestTiesToEven) == APFloat::opOK) |
| 7209 | return ConstantFP::get(FPTy, F); |
| 7210 | return 0; |
| 7211 | } |
| 7212 | |
| 7213 | /// LookThroughFPExtensions - If this is an fp extension instruction, look |
| 7214 | /// through it until we get the source value. |
| 7215 | static Value *LookThroughFPExtensions(Value *V) { |
| 7216 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 7217 | if (I->getOpcode() == Instruction::FPExt) |
| 7218 | return LookThroughFPExtensions(I->getOperand(0)); |
| 7219 | |
| 7220 | // If this value is a constant, return the constant in the smallest FP type |
| 7221 | // that can accurately represent it. This allows us to turn |
| 7222 | // (float)((double)X+2.0) into x+2.0f. |
| 7223 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) { |
| 7224 | if (CFP->getType() == Type::PPC_FP128Ty) |
| 7225 | return V; // No constant folding of this. |
| 7226 | // See if the value can be truncated to float and then reextended. |
| 7227 | if (Value *V = FitsInFPType(CFP, Type::FloatTy, APFloat::IEEEsingle)) |
| 7228 | return V; |
| 7229 | if (CFP->getType() == Type::DoubleTy) |
| 7230 | return V; // Won't shrink. |
| 7231 | if (Value *V = FitsInFPType(CFP, Type::DoubleTy, APFloat::IEEEdouble)) |
| 7232 | return V; |
| 7233 | // Don't try to shrink to various long double types. |
| 7234 | } |
| 7235 | |
| 7236 | return V; |
| 7237 | } |
| 7238 | |
| 7239 | Instruction *InstCombiner::visitFPTrunc(FPTruncInst &CI) { |
| 7240 | if (Instruction *I = commonCastTransforms(CI)) |
| 7241 | return I; |
| 7242 | |
| 7243 | // If we have fptrunc(add (fpextend x), (fpextend y)), where x and y are |
| 7244 | // smaller than the destination type, we can eliminate the truncate by doing |
| 7245 | // the add as the smaller type. This applies to add/sub/mul/div as well as |
| 7246 | // many builtins (sqrt, etc). |
| 7247 | BinaryOperator *OpI = dyn_cast<BinaryOperator>(CI.getOperand(0)); |
| 7248 | if (OpI && OpI->hasOneUse()) { |
| 7249 | switch (OpI->getOpcode()) { |
| 7250 | default: break; |
| 7251 | case Instruction::Add: |
| 7252 | case Instruction::Sub: |
| 7253 | case Instruction::Mul: |
| 7254 | case Instruction::FDiv: |
| 7255 | case Instruction::FRem: |
| 7256 | const Type *SrcTy = OpI->getType(); |
| 7257 | Value *LHSTrunc = LookThroughFPExtensions(OpI->getOperand(0)); |
| 7258 | Value *RHSTrunc = LookThroughFPExtensions(OpI->getOperand(1)); |
| 7259 | if (LHSTrunc->getType() != SrcTy && |
| 7260 | RHSTrunc->getType() != SrcTy) { |
| 7261 | unsigned DstSize = CI.getType()->getPrimitiveSizeInBits(); |
| 7262 | // If the source types were both smaller than the destination type of |
| 7263 | // the cast, do this xform. |
| 7264 | if (LHSTrunc->getType()->getPrimitiveSizeInBits() <= DstSize && |
| 7265 | RHSTrunc->getType()->getPrimitiveSizeInBits() <= DstSize) { |
| 7266 | LHSTrunc = InsertCastBefore(Instruction::FPExt, LHSTrunc, |
| 7267 | CI.getType(), CI); |
| 7268 | RHSTrunc = InsertCastBefore(Instruction::FPExt, RHSTrunc, |
| 7269 | CI.getType(), CI); |
| 7270 | return BinaryOperator::create(OpI->getOpcode(), LHSTrunc, RHSTrunc); |
| 7271 | } |
| 7272 | } |
| 7273 | break; |
| 7274 | } |
| 7275 | } |
| 7276 | return 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7277 | } |
| 7278 | |
| 7279 | Instruction *InstCombiner::visitFPExt(CastInst &CI) { |
| 7280 | return commonCastTransforms(CI); |
| 7281 | } |
| 7282 | |
| 7283 | Instruction *InstCombiner::visitFPToUI(CastInst &CI) { |
Reid Spencer | 44c030a | 2006-11-30 23:13:36 +0000 | [diff] [blame] | 7284 | return commonCastTransforms(CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7285 | } |
| 7286 | |
| 7287 | Instruction *InstCombiner::visitFPToSI(CastInst &CI) { |
Reid Spencer | 44c030a | 2006-11-30 23:13:36 +0000 | [diff] [blame] | 7288 | return commonCastTransforms(CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7289 | } |
| 7290 | |
| 7291 | Instruction *InstCombiner::visitUIToFP(CastInst &CI) { |
| 7292 | return commonCastTransforms(CI); |
| 7293 | } |
| 7294 | |
| 7295 | Instruction *InstCombiner::visitSIToFP(CastInst &CI) { |
| 7296 | return commonCastTransforms(CI); |
| 7297 | } |
| 7298 | |
| 7299 | Instruction *InstCombiner::visitPtrToInt(CastInst &CI) { |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7300 | return commonPointerCastTransforms(CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7301 | } |
| 7302 | |
Chris Lattner | f9d9e45 | 2008-01-08 07:23:51 +0000 | [diff] [blame] | 7303 | Instruction *InstCombiner::visitIntToPtr(IntToPtrInst &CI) { |
| 7304 | if (Instruction *I = commonCastTransforms(CI)) |
| 7305 | return I; |
| 7306 | |
| 7307 | const Type *DestPointee = cast<PointerType>(CI.getType())->getElementType(); |
| 7308 | if (!DestPointee->isSized()) return 0; |
| 7309 | |
| 7310 | // If this is inttoptr(add (ptrtoint x), cst), try to turn this into a GEP. |
| 7311 | ConstantInt *Cst; |
| 7312 | Value *X; |
| 7313 | if (match(CI.getOperand(0), m_Add(m_Cast<PtrToIntInst>(m_Value(X)), |
| 7314 | m_ConstantInt(Cst)))) { |
| 7315 | // If the source and destination operands have the same type, see if this |
| 7316 | // is a single-index GEP. |
| 7317 | if (X->getType() == CI.getType()) { |
| 7318 | // Get the size of the pointee type. |
| 7319 | uint64_t Size = TD->getABITypeSizeInBits(DestPointee); |
| 7320 | |
| 7321 | // Convert the constant to intptr type. |
| 7322 | APInt Offset = Cst->getValue(); |
| 7323 | Offset.sextOrTrunc(TD->getPointerSizeInBits()); |
| 7324 | |
| 7325 | // If Offset is evenly divisible by Size, we can do this xform. |
| 7326 | if (Size && !APIntOps::srem(Offset, APInt(Offset.getBitWidth(), Size))){ |
| 7327 | Offset = APIntOps::sdiv(Offset, APInt(Offset.getBitWidth(), Size)); |
| 7328 | return new GetElementPtrInst(X, ConstantInt::get(Offset)); |
| 7329 | } |
| 7330 | } |
| 7331 | // TODO: Could handle other cases, e.g. where add is indexing into field of |
| 7332 | // struct etc. |
| 7333 | } else if (CI.getOperand(0)->hasOneUse() && |
| 7334 | match(CI.getOperand(0), m_Add(m_Value(X), m_ConstantInt(Cst)))) { |
| 7335 | // Otherwise, if this is inttoptr(add x, cst), try to turn this into an |
| 7336 | // "inttoptr+GEP" instead of "add+intptr". |
| 7337 | |
| 7338 | // Get the size of the pointee type. |
| 7339 | uint64_t Size = TD->getABITypeSize(DestPointee); |
| 7340 | |
| 7341 | // Convert the constant to intptr type. |
| 7342 | APInt Offset = Cst->getValue(); |
| 7343 | Offset.sextOrTrunc(TD->getPointerSizeInBits()); |
| 7344 | |
| 7345 | // If Offset is evenly divisible by Size, we can do this xform. |
| 7346 | if (Size && !APIntOps::srem(Offset, APInt(Offset.getBitWidth(), Size))){ |
| 7347 | Offset = APIntOps::sdiv(Offset, APInt(Offset.getBitWidth(), Size)); |
| 7348 | |
| 7349 | Instruction *P = InsertNewInstBefore(new IntToPtrInst(X, CI.getType(), |
| 7350 | "tmp"), CI); |
| 7351 | return new GetElementPtrInst(P, ConstantInt::get(Offset), "tmp"); |
| 7352 | } |
| 7353 | } |
| 7354 | return 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7355 | } |
| 7356 | |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7357 | Instruction *InstCombiner::visitBitCast(BitCastInst &CI) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7358 | // If the operands are integer typed then apply the integer transforms, |
| 7359 | // otherwise just apply the common ones. |
| 7360 | Value *Src = CI.getOperand(0); |
| 7361 | const Type *SrcTy = Src->getType(); |
| 7362 | const Type *DestTy = CI.getType(); |
| 7363 | |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 7364 | if (SrcTy->isInteger() && DestTy->isInteger()) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7365 | if (Instruction *Result = commonIntCastTransforms(CI)) |
| 7366 | return Result; |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7367 | } else if (isa<PointerType>(SrcTy)) { |
| 7368 | if (Instruction *I = commonPointerCastTransforms(CI)) |
| 7369 | return I; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7370 | } else { |
| 7371 | if (Instruction *Result = commonCastTransforms(CI)) |
| 7372 | return Result; |
| 7373 | } |
| 7374 | |
| 7375 | |
| 7376 | // Get rid of casts from one type to the same type. These are useless and can |
| 7377 | // be replaced by the operand. |
| 7378 | if (DestTy == Src->getType()) |
| 7379 | return ReplaceInstUsesWith(CI, Src); |
| 7380 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7381 | if (const PointerType *DstPTy = dyn_cast<PointerType>(DestTy)) { |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7382 | const PointerType *SrcPTy = cast<PointerType>(SrcTy); |
| 7383 | const Type *DstElTy = DstPTy->getElementType(); |
| 7384 | const Type *SrcElTy = SrcPTy->getElementType(); |
| 7385 | |
| 7386 | // If we are casting a malloc or alloca to a pointer to a type of the same |
| 7387 | // size, rewrite the allocation instruction to allocate the "right" type. |
| 7388 | if (AllocationInst *AI = dyn_cast<AllocationInst>(Src)) |
| 7389 | if (Instruction *V = PromoteCastOfAllocation(CI, *AI)) |
| 7390 | return V; |
| 7391 | |
Chris Lattner | d717c18 | 2007-05-05 22:32:24 +0000 | [diff] [blame] | 7392 | // If the source and destination are pointers, and this cast is equivalent |
| 7393 | // 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] | 7394 | // This can enhance SROA and other transforms that want type-safe pointers. |
| 7395 | Constant *ZeroUInt = Constant::getNullValue(Type::Int32Ty); |
| 7396 | unsigned NumZeros = 0; |
| 7397 | while (SrcElTy != DstElTy && |
| 7398 | isa<CompositeType>(SrcElTy) && !isa<PointerType>(SrcElTy) && |
| 7399 | SrcElTy->getNumContainedTypes() /* not "{}" */) { |
| 7400 | SrcElTy = cast<CompositeType>(SrcElTy)->getTypeAtIndex(ZeroUInt); |
| 7401 | ++NumZeros; |
| 7402 | } |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 7403 | |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7404 | // If we found a path from the src to dest, create the getelementptr now. |
| 7405 | if (SrcElTy == DstElTy) { |
| 7406 | SmallVector<Value*, 8> Idxs(NumZeros+1, ZeroUInt); |
Chuck Rose III | c331d30 | 2007-09-05 20:36:41 +0000 | [diff] [blame] | 7407 | return new GetElementPtrInst(Src, Idxs.begin(), Idxs.end(), "", |
| 7408 | ((Instruction*) NULL)); |
Chris Lattner | 9fb9213 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 7409 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7410 | } |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 7411 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7412 | if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(Src)) { |
| 7413 | if (SVI->hasOneUse()) { |
| 7414 | // Okay, we have (bitconvert (shuffle ..)). Check to see if this is |
| 7415 | // a bitconvert to a vector with the same # elts. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 7416 | if (isa<VectorType>(DestTy) && |
| 7417 | cast<VectorType>(DestTy)->getNumElements() == |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7418 | SVI->getType()->getNumElements()) { |
| 7419 | CastInst *Tmp; |
| 7420 | // If either of the operands is a cast from CI.getType(), then |
| 7421 | // evaluating the shuffle in the casted destination's type will allow |
| 7422 | // us to eliminate at least one cast. |
| 7423 | if (((Tmp = dyn_cast<CastInst>(SVI->getOperand(0))) && |
| 7424 | Tmp->getOperand(0)->getType() == DestTy) || |
| 7425 | ((Tmp = dyn_cast<CastInst>(SVI->getOperand(1))) && |
| 7426 | Tmp->getOperand(0)->getType() == DestTy)) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7427 | Value *LHS = InsertOperandCastBefore(Instruction::BitCast, |
| 7428 | SVI->getOperand(0), DestTy, &CI); |
| 7429 | Value *RHS = InsertOperandCastBefore(Instruction::BitCast, |
| 7430 | SVI->getOperand(1), DestTy, &CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7431 | // Return a new shuffle vector. Use the same element ID's, as we |
| 7432 | // know the vector types match #elts. |
| 7433 | return new ShuffleVectorInst(LHS, RHS, SVI->getOperand(2)); |
Chris Lattner | 01575b7 | 2006-05-25 23:24:33 +0000 | [diff] [blame] | 7434 | } |
| 7435 | } |
| 7436 | } |
| 7437 | } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 7438 | return 0; |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 7439 | } |
| 7440 | |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7441 | /// GetSelectFoldableOperands - We want to turn code that looks like this: |
| 7442 | /// %C = or %A, %B |
| 7443 | /// %D = select %cond, %C, %A |
| 7444 | /// into: |
| 7445 | /// %C = select %cond, %B, 0 |
| 7446 | /// %D = or %A, %C |
| 7447 | /// |
| 7448 | /// Assuming that the specified instruction is an operand to the select, return |
| 7449 | /// a bitmask indicating which operands of this instruction are foldable if they |
| 7450 | /// equal the other incoming value of the select. |
| 7451 | /// |
| 7452 | static unsigned GetSelectFoldableOperands(Instruction *I) { |
| 7453 | switch (I->getOpcode()) { |
| 7454 | case Instruction::Add: |
| 7455 | case Instruction::Mul: |
| 7456 | case Instruction::And: |
| 7457 | case Instruction::Or: |
| 7458 | case Instruction::Xor: |
| 7459 | return 3; // Can fold through either operand. |
| 7460 | case Instruction::Sub: // Can only fold on the amount subtracted. |
| 7461 | case Instruction::Shl: // Can only fold on the shift amount. |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 7462 | case Instruction::LShr: |
| 7463 | case Instruction::AShr: |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7464 | return 1; |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7465 | default: |
| 7466 | return 0; // Cannot fold |
| 7467 | } |
| 7468 | } |
| 7469 | |
| 7470 | /// GetSelectFoldableConstant - For the same transformation as the previous |
| 7471 | /// function, return the identity constant that goes into the select. |
| 7472 | static Constant *GetSelectFoldableConstant(Instruction *I) { |
| 7473 | switch (I->getOpcode()) { |
| 7474 | default: assert(0 && "This cannot happen!"); abort(); |
| 7475 | case Instruction::Add: |
| 7476 | case Instruction::Sub: |
| 7477 | case Instruction::Or: |
| 7478 | case Instruction::Xor: |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7479 | case Instruction::Shl: |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 7480 | case Instruction::LShr: |
| 7481 | case Instruction::AShr: |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7482 | return Constant::getNullValue(I->getType()); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7483 | case Instruction::And: |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 7484 | return Constant::getAllOnesValue(I->getType()); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7485 | case Instruction::Mul: |
| 7486 | return ConstantInt::get(I->getType(), 1); |
| 7487 | } |
| 7488 | } |
| 7489 | |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 7490 | /// FoldSelectOpOp - Here we have (select c, TI, FI), and we know that TI and FI |
| 7491 | /// have the same opcode and only one use each. Try to simplify this. |
| 7492 | Instruction *InstCombiner::FoldSelectOpOp(SelectInst &SI, Instruction *TI, |
| 7493 | Instruction *FI) { |
| 7494 | if (TI->getNumOperands() == 1) { |
| 7495 | // If this is a non-volatile load or a cast from the same type, |
| 7496 | // merge. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7497 | if (TI->isCast()) { |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 7498 | if (TI->getOperand(0)->getType() != FI->getOperand(0)->getType()) |
| 7499 | return 0; |
| 7500 | } else { |
| 7501 | return 0; // unknown unary op. |
| 7502 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7503 | |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 7504 | // Fold this by inserting a select from the input values. |
| 7505 | SelectInst *NewSI = new SelectInst(SI.getCondition(), TI->getOperand(0), |
| 7506 | FI->getOperand(0), SI.getName()+".v"); |
| 7507 | InsertNewInstBefore(NewSI, SI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7508 | return CastInst::create(Instruction::CastOps(TI->getOpcode()), NewSI, |
| 7509 | TI->getType()); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 7510 | } |
| 7511 | |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7512 | // Only handle binary operators here. |
| 7513 | if (!isa<BinaryOperator>(TI)) |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 7514 | return 0; |
| 7515 | |
| 7516 | // Figure out if the operations have any operands in common. |
| 7517 | Value *MatchOp, *OtherOpT, *OtherOpF; |
| 7518 | bool MatchIsOpZero; |
| 7519 | if (TI->getOperand(0) == FI->getOperand(0)) { |
| 7520 | MatchOp = TI->getOperand(0); |
| 7521 | OtherOpT = TI->getOperand(1); |
| 7522 | OtherOpF = FI->getOperand(1); |
| 7523 | MatchIsOpZero = true; |
| 7524 | } else if (TI->getOperand(1) == FI->getOperand(1)) { |
| 7525 | MatchOp = TI->getOperand(1); |
| 7526 | OtherOpT = TI->getOperand(0); |
| 7527 | OtherOpF = FI->getOperand(0); |
| 7528 | MatchIsOpZero = false; |
| 7529 | } else if (!TI->isCommutative()) { |
| 7530 | return 0; |
| 7531 | } else if (TI->getOperand(0) == FI->getOperand(1)) { |
| 7532 | MatchOp = TI->getOperand(0); |
| 7533 | OtherOpT = TI->getOperand(1); |
| 7534 | OtherOpF = FI->getOperand(0); |
| 7535 | MatchIsOpZero = true; |
| 7536 | } else if (TI->getOperand(1) == FI->getOperand(0)) { |
| 7537 | MatchOp = TI->getOperand(1); |
| 7538 | OtherOpT = TI->getOperand(0); |
| 7539 | OtherOpF = FI->getOperand(1); |
| 7540 | MatchIsOpZero = true; |
| 7541 | } else { |
| 7542 | return 0; |
| 7543 | } |
| 7544 | |
| 7545 | // If we reach here, they do have operations in common. |
| 7546 | SelectInst *NewSI = new SelectInst(SI.getCondition(), OtherOpT, |
| 7547 | OtherOpF, SI.getName()+".v"); |
| 7548 | InsertNewInstBefore(NewSI, SI); |
| 7549 | |
| 7550 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TI)) { |
| 7551 | if (MatchIsOpZero) |
| 7552 | return BinaryOperator::create(BO->getOpcode(), MatchOp, NewSI); |
| 7553 | else |
| 7554 | return BinaryOperator::create(BO->getOpcode(), NewSI, MatchOp); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 7555 | } |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 7556 | assert(0 && "Shouldn't get here"); |
| 7557 | return 0; |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 7558 | } |
| 7559 | |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 7560 | Instruction *InstCombiner::visitSelectInst(SelectInst &SI) { |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 7561 | Value *CondVal = SI.getCondition(); |
| 7562 | Value *TrueVal = SI.getTrueValue(); |
| 7563 | Value *FalseVal = SI.getFalseValue(); |
| 7564 | |
| 7565 | // select true, X, Y -> X |
| 7566 | // select false, X, Y -> Y |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 7567 | if (ConstantInt *C = dyn_cast<ConstantInt>(CondVal)) |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 7568 | return ReplaceInstUsesWith(SI, C->getZExtValue() ? TrueVal : FalseVal); |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 7569 | |
| 7570 | // select C, X, X -> X |
| 7571 | if (TrueVal == FalseVal) |
| 7572 | return ReplaceInstUsesWith(SI, TrueVal); |
| 7573 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 7574 | if (isa<UndefValue>(TrueVal)) // select C, undef, X -> X |
| 7575 | return ReplaceInstUsesWith(SI, FalseVal); |
| 7576 | if (isa<UndefValue>(FalseVal)) // select C, X, undef -> X |
| 7577 | return ReplaceInstUsesWith(SI, TrueVal); |
| 7578 | if (isa<UndefValue>(CondVal)) { // select undef, X, Y -> X or Y |
| 7579 | if (isa<Constant>(TrueVal)) |
| 7580 | return ReplaceInstUsesWith(SI, TrueVal); |
| 7581 | else |
| 7582 | return ReplaceInstUsesWith(SI, FalseVal); |
| 7583 | } |
| 7584 | |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 7585 | if (SI.getType() == Type::Int1Ty) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 7586 | if (ConstantInt *C = dyn_cast<ConstantInt>(TrueVal)) { |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 7587 | if (C->getZExtValue()) { |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 7588 | // Change: A = select B, true, C --> A = or B, C |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 7589 | return BinaryOperator::createOr(CondVal, FalseVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 7590 | } else { |
| 7591 | // Change: A = select B, false, C --> A = and !B, C |
| 7592 | Value *NotCond = |
| 7593 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
| 7594 | "not."+CondVal->getName()), SI); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 7595 | return BinaryOperator::createAnd(NotCond, FalseVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 7596 | } |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 7597 | } else if (ConstantInt *C = dyn_cast<ConstantInt>(FalseVal)) { |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 7598 | if (C->getZExtValue() == false) { |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 7599 | // Change: A = select B, C, false --> A = and B, C |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 7600 | return BinaryOperator::createAnd(CondVal, TrueVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 7601 | } else { |
| 7602 | // Change: A = select B, C, true --> A = or !B, C |
| 7603 | Value *NotCond = |
| 7604 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
| 7605 | "not."+CondVal->getName()), SI); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 7606 | return BinaryOperator::createOr(NotCond, TrueVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 7607 | } |
| 7608 | } |
Chris Lattner | cfa5975 | 2007-11-25 21:27:53 +0000 | [diff] [blame] | 7609 | |
| 7610 | // select a, b, a -> a&b |
| 7611 | // select a, a, b -> a|b |
| 7612 | if (CondVal == TrueVal) |
| 7613 | return BinaryOperator::createOr(CondVal, FalseVal); |
| 7614 | else if (CondVal == FalseVal) |
| 7615 | return BinaryOperator::createAnd(CondVal, TrueVal); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 7616 | } |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 7617 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 7618 | // Selecting between two integer constants? |
| 7619 | if (ConstantInt *TrueValC = dyn_cast<ConstantInt>(TrueVal)) |
| 7620 | if (ConstantInt *FalseValC = dyn_cast<ConstantInt>(FalseVal)) { |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 7621 | // select C, 1, 0 -> zext C to int |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 7622 | if (FalseValC->isZero() && TrueValC->getValue() == 1) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7623 | return CastInst::create(Instruction::ZExt, CondVal, SI.getType()); |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 7624 | } else if (TrueValC->isZero() && FalseValC->getValue() == 1) { |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 7625 | // select C, 0, 1 -> zext !C to int |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 7626 | Value *NotCond = |
| 7627 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
Chris Lattner | 82e14fe | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 7628 | "not."+CondVal->getName()), SI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7629 | return CastInst::create(Instruction::ZExt, NotCond, SI.getType()); |
Chris Lattner | 82e14fe | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 7630 | } |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 7631 | |
| 7632 | // FIXME: Turn select 0/-1 and -1/0 into sext from condition! |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 7633 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7634 | if (ICmpInst *IC = dyn_cast<ICmpInst>(SI.getCondition())) { |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 7635 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7636 | // (x <s 0) ? -1 : 0 -> ashr x, 31 |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 7637 | if (TrueValC->isAllOnesValue() && FalseValC->isZero()) |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 7638 | if (ConstantInt *CmpCst = dyn_cast<ConstantInt>(IC->getOperand(1))) { |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 7639 | if (IC->getPredicate() == ICmpInst::ICMP_SLT && CmpCst->isZero()) { |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 7640 | // The comparison constant and the result are not neccessarily the |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7641 | // same width. Make an all-ones value by inserting a AShr. |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 7642 | Value *X = IC->getOperand(0); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 7643 | uint32_t Bits = X->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7644 | Constant *ShAmt = ConstantInt::get(X->getType(), Bits-1); |
| 7645 | Instruction *SRA = BinaryOperator::create(Instruction::AShr, X, |
| 7646 | ShAmt, "ones"); |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 7647 | InsertNewInstBefore(SRA, SI); |
| 7648 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7649 | // Finally, convert to the type of the select RHS. We figure out |
| 7650 | // if this requires a SExt, Trunc or BitCast based on the sizes. |
| 7651 | Instruction::CastOps opc = Instruction::BitCast; |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 7652 | uint32_t SRASize = SRA->getType()->getPrimitiveSizeInBits(); |
| 7653 | uint32_t SISize = SI.getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7654 | if (SRASize < SISize) |
| 7655 | opc = Instruction::SExt; |
| 7656 | else if (SRASize > SISize) |
| 7657 | opc = Instruction::Trunc; |
| 7658 | return CastInst::create(opc, SRA, SI.getType()); |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 7659 | } |
| 7660 | } |
| 7661 | |
| 7662 | |
| 7663 | // 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] | 7664 | // 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] | 7665 | // non-constant value, eliminate this whole mess. This corresponds to |
| 7666 | // cases like this: ((X & 27) ? 27 : 0) |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 7667 | if (TrueValC->isZero() || FalseValC->isZero()) |
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 7668 | if (IC->isEquality() && isa<ConstantInt>(IC->getOperand(1)) && |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 7669 | cast<Constant>(IC->getOperand(1))->isNullValue()) |
| 7670 | if (Instruction *ICA = dyn_cast<Instruction>(IC->getOperand(0))) |
| 7671 | if (ICA->getOpcode() == Instruction::And && |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7672 | isa<ConstantInt>(ICA->getOperand(1)) && |
| 7673 | (ICA->getOperand(1) == TrueValC || |
| 7674 | ICA->getOperand(1) == FalseValC) && |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 7675 | isOneBitSet(cast<ConstantInt>(ICA->getOperand(1)))) { |
| 7676 | // 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] | 7677 | // know whether we have a icmp_ne or icmp_eq and whether the |
| 7678 | // true or false val is the zero. |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 7679 | bool ShouldNotVal = !TrueValC->isZero(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7680 | ShouldNotVal ^= IC->getPredicate() == ICmpInst::ICMP_NE; |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 7681 | Value *V = ICA; |
| 7682 | if (ShouldNotVal) |
| 7683 | V = InsertNewInstBefore(BinaryOperator::create( |
| 7684 | Instruction::Xor, V, ICA->getOperand(1)), SI); |
| 7685 | return ReplaceInstUsesWith(SI, V); |
| 7686 | } |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 7687 | } |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 7688 | } |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 7689 | |
| 7690 | // 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] | 7691 | if (FCmpInst *FCI = dyn_cast<FCmpInst>(CondVal)) { |
| 7692 | if (FCI->getOperand(0) == TrueVal && FCI->getOperand(1) == FalseVal) { |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 7693 | // Transform (X == Y) ? X : Y -> Y |
Dale Johannesen | 5a2174f | 2007-10-03 17:45:27 +0000 | [diff] [blame] | 7694 | if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) { |
| 7695 | // This is not safe in general for floating point: |
| 7696 | // consider X== -0, Y== +0. |
| 7697 | // It becomes safe if either operand is a nonzero constant. |
| 7698 | ConstantFP *CFPt, *CFPf; |
| 7699 | if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) && |
| 7700 | !CFPt->getValueAPF().isZero()) || |
| 7701 | ((CFPf = dyn_cast<ConstantFP>(FalseVal)) && |
| 7702 | !CFPf->getValueAPF().isZero())) |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 7703 | return ReplaceInstUsesWith(SI, FalseVal); |
Dale Johannesen | 5a2174f | 2007-10-03 17:45:27 +0000 | [diff] [blame] | 7704 | } |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 7705 | // Transform (X != Y) ? X : Y -> X |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7706 | if (FCI->getPredicate() == FCmpInst::FCMP_ONE) |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 7707 | return ReplaceInstUsesWith(SI, TrueVal); |
| 7708 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 7709 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7710 | } else if (FCI->getOperand(0) == FalseVal && FCI->getOperand(1) == TrueVal){ |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 7711 | // Transform (X == Y) ? Y : X -> X |
Dale Johannesen | 5a2174f | 2007-10-03 17:45:27 +0000 | [diff] [blame] | 7712 | if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) { |
| 7713 | // This is not safe in general for floating point: |
| 7714 | // consider X== -0, Y== +0. |
| 7715 | // It becomes safe if either operand is a nonzero constant. |
| 7716 | ConstantFP *CFPt, *CFPf; |
| 7717 | if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) && |
| 7718 | !CFPt->getValueAPF().isZero()) || |
| 7719 | ((CFPf = dyn_cast<ConstantFP>(FalseVal)) && |
| 7720 | !CFPf->getValueAPF().isZero())) |
| 7721 | return ReplaceInstUsesWith(SI, FalseVal); |
| 7722 | } |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 7723 | // Transform (X != Y) ? Y : X -> Y |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7724 | if (FCI->getPredicate() == FCmpInst::FCMP_ONE) |
| 7725 | return ReplaceInstUsesWith(SI, TrueVal); |
| 7726 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 7727 | } |
| 7728 | } |
| 7729 | |
| 7730 | // See if we are selecting two values based on a comparison of the two values. |
| 7731 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(CondVal)) { |
| 7732 | if (ICI->getOperand(0) == TrueVal && ICI->getOperand(1) == FalseVal) { |
| 7733 | // Transform (X == Y) ? X : Y -> Y |
| 7734 | if (ICI->getPredicate() == ICmpInst::ICMP_EQ) |
| 7735 | return ReplaceInstUsesWith(SI, FalseVal); |
| 7736 | // Transform (X != Y) ? X : Y -> X |
| 7737 | if (ICI->getPredicate() == ICmpInst::ICMP_NE) |
| 7738 | return ReplaceInstUsesWith(SI, TrueVal); |
| 7739 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 7740 | |
| 7741 | } else if (ICI->getOperand(0) == FalseVal && ICI->getOperand(1) == TrueVal){ |
| 7742 | // Transform (X == Y) ? Y : X -> X |
| 7743 | if (ICI->getPredicate() == ICmpInst::ICMP_EQ) |
| 7744 | return ReplaceInstUsesWith(SI, FalseVal); |
| 7745 | // Transform (X != Y) ? Y : X -> Y |
| 7746 | if (ICI->getPredicate() == ICmpInst::ICMP_NE) |
Chris Lattner | fbede52 | 2004-04-11 01:39:19 +0000 | [diff] [blame] | 7747 | return ReplaceInstUsesWith(SI, TrueVal); |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 7748 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 7749 | } |
| 7750 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7751 | |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 7752 | if (Instruction *TI = dyn_cast<Instruction>(TrueVal)) |
| 7753 | if (Instruction *FI = dyn_cast<Instruction>(FalseVal)) |
| 7754 | if (TI->hasOneUse() && FI->hasOneUse()) { |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 7755 | Instruction *AddOp = 0, *SubOp = 0; |
| 7756 | |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 7757 | // Turn (select C, (op X, Y), (op X, Z)) -> (op X, (select C, Y, Z)) |
| 7758 | if (TI->getOpcode() == FI->getOpcode()) |
| 7759 | if (Instruction *IV = FoldSelectOpOp(SI, TI, FI)) |
| 7760 | return IV; |
| 7761 | |
| 7762 | // Turn select C, (X+Y), (X-Y) --> (X+(select C, Y, (-Y))). This is |
| 7763 | // even legal for FP. |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 7764 | if (TI->getOpcode() == Instruction::Sub && |
| 7765 | FI->getOpcode() == Instruction::Add) { |
| 7766 | AddOp = FI; SubOp = TI; |
| 7767 | } else if (FI->getOpcode() == Instruction::Sub && |
| 7768 | TI->getOpcode() == Instruction::Add) { |
| 7769 | AddOp = TI; SubOp = FI; |
| 7770 | } |
| 7771 | |
| 7772 | if (AddOp) { |
| 7773 | Value *OtherAddOp = 0; |
| 7774 | if (SubOp->getOperand(0) == AddOp->getOperand(0)) { |
| 7775 | OtherAddOp = AddOp->getOperand(1); |
| 7776 | } else if (SubOp->getOperand(0) == AddOp->getOperand(1)) { |
| 7777 | OtherAddOp = AddOp->getOperand(0); |
| 7778 | } |
| 7779 | |
| 7780 | if (OtherAddOp) { |
Chris Lattner | 97f37a4 | 2006-02-24 18:05:58 +0000 | [diff] [blame] | 7781 | // So at this point we know we have (Y -> OtherAddOp): |
| 7782 | // select C, (add X, Y), (sub X, Z) |
| 7783 | Value *NegVal; // Compute -Z |
| 7784 | if (Constant *C = dyn_cast<Constant>(SubOp->getOperand(1))) { |
| 7785 | NegVal = ConstantExpr::getNeg(C); |
| 7786 | } else { |
| 7787 | NegVal = InsertNewInstBefore( |
| 7788 | BinaryOperator::createNeg(SubOp->getOperand(1), "tmp"), SI); |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 7789 | } |
Chris Lattner | 97f37a4 | 2006-02-24 18:05:58 +0000 | [diff] [blame] | 7790 | |
| 7791 | Value *NewTrueOp = OtherAddOp; |
| 7792 | Value *NewFalseOp = NegVal; |
| 7793 | if (AddOp != TI) |
| 7794 | std::swap(NewTrueOp, NewFalseOp); |
| 7795 | Instruction *NewSel = |
| 7796 | new SelectInst(CondVal, NewTrueOp,NewFalseOp,SI.getName()+".p"); |
| 7797 | |
| 7798 | NewSel = InsertNewInstBefore(NewSel, SI); |
| 7799 | return BinaryOperator::createAdd(SubOp->getOperand(0), NewSel); |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 7800 | } |
| 7801 | } |
| 7802 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7803 | |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7804 | // See if we can fold the select into one of our operands. |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 7805 | if (SI.getType()->isInteger()) { |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7806 | // See the comment above GetSelectFoldableOperands for a description of the |
| 7807 | // transformation we are doing here. |
| 7808 | if (Instruction *TVI = dyn_cast<Instruction>(TrueVal)) |
| 7809 | if (TVI->hasOneUse() && TVI->getNumOperands() == 2 && |
| 7810 | !isa<Constant>(FalseVal)) |
| 7811 | if (unsigned SFO = GetSelectFoldableOperands(TVI)) { |
| 7812 | unsigned OpToFold = 0; |
| 7813 | if ((SFO & 1) && FalseVal == TVI->getOperand(0)) { |
| 7814 | OpToFold = 1; |
| 7815 | } else if ((SFO & 2) && FalseVal == TVI->getOperand(1)) { |
| 7816 | OpToFold = 2; |
| 7817 | } |
| 7818 | |
| 7819 | if (OpToFold) { |
| 7820 | Constant *C = GetSelectFoldableConstant(TVI); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7821 | Instruction *NewSel = |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 7822 | new SelectInst(SI.getCondition(), TVI->getOperand(2-OpToFold), C); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7823 | InsertNewInstBefore(NewSel, SI); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 7824 | NewSel->takeName(TVI); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7825 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TVI)) |
| 7826 | return BinaryOperator::create(BO->getOpcode(), FalseVal, NewSel); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7827 | else { |
| 7828 | assert(0 && "Unknown instruction!!"); |
| 7829 | } |
| 7830 | } |
| 7831 | } |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 7832 | |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7833 | if (Instruction *FVI = dyn_cast<Instruction>(FalseVal)) |
| 7834 | if (FVI->hasOneUse() && FVI->getNumOperands() == 2 && |
| 7835 | !isa<Constant>(TrueVal)) |
| 7836 | if (unsigned SFO = GetSelectFoldableOperands(FVI)) { |
| 7837 | unsigned OpToFold = 0; |
| 7838 | if ((SFO & 1) && TrueVal == FVI->getOperand(0)) { |
| 7839 | OpToFold = 1; |
| 7840 | } else if ((SFO & 2) && TrueVal == FVI->getOperand(1)) { |
| 7841 | OpToFold = 2; |
| 7842 | } |
| 7843 | |
| 7844 | if (OpToFold) { |
| 7845 | Constant *C = GetSelectFoldableConstant(FVI); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7846 | Instruction *NewSel = |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 7847 | new SelectInst(SI.getCondition(), C, FVI->getOperand(2-OpToFold)); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7848 | InsertNewInstBefore(NewSel, SI); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 7849 | NewSel->takeName(FVI); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7850 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(FVI)) |
| 7851 | return BinaryOperator::create(BO->getOpcode(), TrueVal, NewSel); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7852 | else |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7853 | assert(0 && "Unknown instruction!!"); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7854 | } |
| 7855 | } |
| 7856 | } |
Chris Lattner | a1df33c | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 7857 | |
| 7858 | if (BinaryOperator::isNot(CondVal)) { |
| 7859 | SI.setOperand(0, BinaryOperator::getNotArgument(CondVal)); |
| 7860 | SI.setOperand(1, FalseVal); |
| 7861 | SI.setOperand(2, TrueVal); |
| 7862 | return &SI; |
| 7863 | } |
| 7864 | |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 7865 | return 0; |
| 7866 | } |
| 7867 | |
Chris Lattner | f2369f2 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 7868 | /// GetOrEnforceKnownAlignment - If the specified pointer has an alignment that |
| 7869 | /// we can determine, return it, otherwise return 0. If PrefAlign is specified, |
| 7870 | /// and it is more than the alignment of the ultimate object, see if we can |
| 7871 | /// increase the alignment of the ultimate object, making this check succeed. |
| 7872 | static unsigned GetOrEnforceKnownAlignment(Value *V, TargetData *TD, |
| 7873 | unsigned PrefAlign = 0) { |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7874 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) { |
| 7875 | unsigned Align = GV->getAlignment(); |
Andrew Lenharth | b410df9 | 2007-11-08 18:45:15 +0000 | [diff] [blame] | 7876 | if (Align == 0 && TD && GV->getType()->getElementType()->isSized()) |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 7877 | Align = TD->getPrefTypeAlignment(GV->getType()->getElementType()); |
Chris Lattner | f2369f2 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 7878 | |
| 7879 | // If there is a large requested alignment and we can, bump up the alignment |
| 7880 | // of the global. |
| 7881 | if (PrefAlign > Align && GV->hasInitializer()) { |
| 7882 | GV->setAlignment(PrefAlign); |
| 7883 | Align = PrefAlign; |
| 7884 | } |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7885 | return Align; |
| 7886 | } else if (AllocationInst *AI = dyn_cast<AllocationInst>(V)) { |
| 7887 | unsigned Align = AI->getAlignment(); |
| 7888 | if (Align == 0 && TD) { |
| 7889 | if (isa<AllocaInst>(AI)) |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 7890 | Align = TD->getPrefTypeAlignment(AI->getType()->getElementType()); |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7891 | else if (isa<MallocInst>(AI)) { |
| 7892 | // Malloc returns maximally aligned memory. |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 7893 | Align = TD->getABITypeAlignment(AI->getType()->getElementType()); |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 7894 | Align = |
| 7895 | std::max(Align, |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 7896 | (unsigned)TD->getABITypeAlignment(Type::DoubleTy)); |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 7897 | Align = |
| 7898 | std::max(Align, |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 7899 | (unsigned)TD->getABITypeAlignment(Type::Int64Ty)); |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7900 | } |
| 7901 | } |
Chris Lattner | f2369f2 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 7902 | |
| 7903 | // If there is a requested alignment and if this is an alloca, round up. We |
| 7904 | // don't do this for malloc, because some systems can't respect the request. |
| 7905 | if (PrefAlign > Align && isa<AllocaInst>(AI)) { |
| 7906 | AI->setAlignment(PrefAlign); |
| 7907 | Align = PrefAlign; |
| 7908 | } |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7909 | return Align; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7910 | } else if (isa<BitCastInst>(V) || |
Chris Lattner | 51c26e9 | 2006-03-07 01:28:57 +0000 | [diff] [blame] | 7911 | (isa<ConstantExpr>(V) && |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7912 | cast<ConstantExpr>(V)->getOpcode() == Instruction::BitCast)) { |
Chris Lattner | f2369f2 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 7913 | return GetOrEnforceKnownAlignment(cast<User>(V)->getOperand(0), |
| 7914 | TD, PrefAlign); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7915 | } else if (User *GEPI = dyn_castGetElementPtr(V)) { |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7916 | // If all indexes are zero, it is just the alignment of the base pointer. |
| 7917 | bool AllZeroOperands = true; |
| 7918 | for (unsigned i = 1, e = GEPI->getNumOperands(); i != e; ++i) |
| 7919 | if (!isa<Constant>(GEPI->getOperand(i)) || |
| 7920 | !cast<Constant>(GEPI->getOperand(i))->isNullValue()) { |
| 7921 | AllZeroOperands = false; |
| 7922 | break; |
| 7923 | } |
Chris Lattner | f2369f2 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 7924 | |
| 7925 | if (AllZeroOperands) { |
| 7926 | // Treat this like a bitcast. |
| 7927 | return GetOrEnforceKnownAlignment(GEPI->getOperand(0), TD, PrefAlign); |
| 7928 | } |
| 7929 | |
| 7930 | unsigned BaseAlignment = GetOrEnforceKnownAlignment(GEPI->getOperand(0),TD); |
| 7931 | if (BaseAlignment == 0) return 0; |
| 7932 | |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7933 | // Otherwise, if the base alignment is >= the alignment we expect for the |
| 7934 | // base pointer type, then we know that the resultant pointer is aligned at |
| 7935 | // least as much as its type requires. |
| 7936 | if (!TD) return 0; |
| 7937 | |
| 7938 | const Type *BasePtrTy = GEPI->getOperand(0)->getType(); |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 7939 | const PointerType *PtrTy = cast<PointerType>(BasePtrTy); |
Lauro Ramos Venancio | c7d1114 | 2007-07-31 20:13:21 +0000 | [diff] [blame] | 7940 | unsigned Align = TD->getABITypeAlignment(PtrTy->getElementType()); |
| 7941 | if (Align <= BaseAlignment) { |
Chris Lattner | 51c26e9 | 2006-03-07 01:28:57 +0000 | [diff] [blame] | 7942 | const Type *GEPTy = GEPI->getType(); |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 7943 | const PointerType *GEPPtrTy = cast<PointerType>(GEPTy); |
Lauro Ramos Venancio | c7d1114 | 2007-07-31 20:13:21 +0000 | [diff] [blame] | 7944 | Align = std::min(Align, (unsigned) |
| 7945 | TD->getABITypeAlignment(GEPPtrTy->getElementType())); |
| 7946 | return Align; |
Chris Lattner | 51c26e9 | 2006-03-07 01:28:57 +0000 | [diff] [blame] | 7947 | } |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7948 | return 0; |
| 7949 | } |
| 7950 | return 0; |
| 7951 | } |
| 7952 | |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 7953 | Instruction *InstCombiner::SimplifyMemTransfer(MemIntrinsic *MI) { |
| 7954 | unsigned DstAlign = GetOrEnforceKnownAlignment(MI->getOperand(1), TD); |
| 7955 | unsigned SrcAlign = GetOrEnforceKnownAlignment(MI->getOperand(2), TD); |
| 7956 | unsigned MinAlign = std::min(DstAlign, SrcAlign); |
| 7957 | unsigned CopyAlign = MI->getAlignment()->getZExtValue(); |
| 7958 | |
| 7959 | if (CopyAlign < MinAlign) { |
| 7960 | MI->setAlignment(ConstantInt::get(Type::Int32Ty, MinAlign)); |
| 7961 | return MI; |
| 7962 | } |
| 7963 | |
| 7964 | // If MemCpyInst length is 1/2/4/8 bytes then replace memcpy with |
| 7965 | // load/store. |
| 7966 | ConstantInt *MemOpLength = dyn_cast<ConstantInt>(MI->getOperand(3)); |
| 7967 | if (MemOpLength == 0) return 0; |
| 7968 | |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 7969 | // Source and destination pointer types are always "i8*" for intrinsic. See |
| 7970 | // if the size is something we can handle with a single primitive load/store. |
| 7971 | // A single load+store correctly handles overlapping memory in the memmove |
| 7972 | // case. |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 7973 | unsigned Size = MemOpLength->getZExtValue(); |
| 7974 | if (Size == 0 || Size > 8 || (Size&(Size-1))) |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 7975 | return 0; // If not 1/2/4/8 bytes, exit. |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 7976 | |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 7977 | // Use an integer load+store unless we can find something better. |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 7978 | Type *NewPtrTy = PointerType::getUnqual(IntegerType::get(Size<<3)); |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 7979 | |
| 7980 | // Memcpy forces the use of i8* for the source and destination. That means |
| 7981 | // that if you're using memcpy to move one double around, you'll get a cast |
| 7982 | // from double* to i8*. We'd much rather use a double load+store rather than |
| 7983 | // an i64 load+store, here because this improves the odds that the source or |
| 7984 | // dest address will be promotable. See if we can find a better type than the |
| 7985 | // integer datatype. |
| 7986 | if (Value *Op = getBitCastOperand(MI->getOperand(1))) { |
| 7987 | const Type *SrcETy = cast<PointerType>(Op->getType())->getElementType(); |
| 7988 | if (SrcETy->isSized() && TD->getTypeStoreSize(SrcETy) == Size) { |
| 7989 | // The SrcETy might be something like {{{double}}} or [1 x double]. Rip |
| 7990 | // down through these levels if so. |
| 7991 | while (!SrcETy->isFirstClassType()) { |
| 7992 | if (const StructType *STy = dyn_cast<StructType>(SrcETy)) { |
| 7993 | if (STy->getNumElements() == 1) |
| 7994 | SrcETy = STy->getElementType(0); |
| 7995 | else |
| 7996 | break; |
| 7997 | } else if (const ArrayType *ATy = dyn_cast<ArrayType>(SrcETy)) { |
| 7998 | if (ATy->getNumElements() == 1) |
| 7999 | SrcETy = ATy->getElementType(); |
| 8000 | else |
| 8001 | break; |
| 8002 | } else |
| 8003 | break; |
| 8004 | } |
| 8005 | |
| 8006 | if (SrcETy->isFirstClassType()) |
| 8007 | NewPtrTy = PointerType::getUnqual(SrcETy); |
| 8008 | } |
| 8009 | } |
| 8010 | |
| 8011 | |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8012 | // If the memcpy/memmove provides better alignment info than we can |
| 8013 | // infer, use it. |
| 8014 | SrcAlign = std::max(SrcAlign, CopyAlign); |
| 8015 | DstAlign = std::max(DstAlign, CopyAlign); |
| 8016 | |
| 8017 | Value *Src = InsertBitCastBefore(MI->getOperand(2), NewPtrTy, *MI); |
| 8018 | Value *Dest = InsertBitCastBefore(MI->getOperand(1), NewPtrTy, *MI); |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 8019 | Instruction *L = new LoadInst(Src, "tmp", false, SrcAlign); |
| 8020 | InsertNewInstBefore(L, *MI); |
| 8021 | InsertNewInstBefore(new StoreInst(L, Dest, false, DstAlign), *MI); |
| 8022 | |
| 8023 | // Set the size of the copy to 0, it will be deleted on the next iteration. |
| 8024 | MI->setOperand(3, Constant::getNullValue(MemOpLength->getType())); |
| 8025 | return MI; |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8026 | } |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 8027 | |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 8028 | /// visitCallInst - CallInst simplification. This mostly only handles folding |
| 8029 | /// of intrinsic instructions. For normal calls, it allows visitCallSite to do |
| 8030 | /// the heavy lifting. |
| 8031 | /// |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8032 | Instruction *InstCombiner::visitCallInst(CallInst &CI) { |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 8033 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(&CI); |
| 8034 | if (!II) return visitCallSite(&CI); |
| 8035 | |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 8036 | // Intrinsics cannot occur in an invoke, so handle them here instead of in |
| 8037 | // visitCallSite. |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 8038 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(II)) { |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8039 | bool Changed = false; |
| 8040 | |
| 8041 | // memmove/cpy/set of zero bytes is a noop. |
| 8042 | if (Constant *NumBytes = dyn_cast<Constant>(MI->getLength())) { |
| 8043 | if (NumBytes->isNullValue()) return EraseInstFromFunction(CI); |
| 8044 | |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8045 | if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes)) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8046 | if (CI->getZExtValue() == 1) { |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8047 | // Replace the instruction with just byte operations. We would |
| 8048 | // transform other cases to loads/stores, but we don't know if |
| 8049 | // alignment is sufficient. |
| 8050 | } |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 8051 | } |
| 8052 | |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8053 | // If we have a memmove and the source operation is a constant global, |
| 8054 | // then the source and dest pointers can't alias, so we can change this |
| 8055 | // into a call to memcpy. |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8056 | if (MemMoveInst *MMI = dyn_cast<MemMoveInst>(MI)) { |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8057 | if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource())) |
| 8058 | if (GVSrc->isConstant()) { |
| 8059 | Module *M = CI.getParent()->getParent()->getParent(); |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 8060 | Intrinsic::ID MemCpyID; |
| 8061 | if (CI.getOperand(3)->getType() == Type::Int32Ty) |
| 8062 | MemCpyID = Intrinsic::memcpy_i32; |
Chris Lattner | 2195939 | 2006-03-03 01:34:17 +0000 | [diff] [blame] | 8063 | else |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 8064 | MemCpyID = Intrinsic::memcpy_i64; |
| 8065 | CI.setOperand(0, Intrinsic::getDeclaration(M, MemCpyID)); |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8066 | Changed = true; |
| 8067 | } |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8068 | } |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8069 | |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8070 | // If we can determine a pointer alignment that is bigger than currently |
| 8071 | // set, update the alignment. |
| 8072 | if (isa<MemCpyInst>(MI) || isa<MemMoveInst>(MI)) { |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8073 | if (Instruction *I = SimplifyMemTransfer(MI)) |
| 8074 | return I; |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8075 | } else if (isa<MemSetInst>(MI)) { |
Chris Lattner | f2369f2 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 8076 | unsigned Alignment = GetOrEnforceKnownAlignment(MI->getDest(), TD); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8077 | if (MI->getAlignment()->getZExtValue() < Alignment) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8078 | MI->setAlignment(ConstantInt::get(Type::Int32Ty, Alignment)); |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8079 | Changed = true; |
| 8080 | } |
| 8081 | } |
| 8082 | |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 8083 | if (Changed) return II; |
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 8084 | } else { |
| 8085 | switch (II->getIntrinsicID()) { |
| 8086 | default: break; |
Chris Lattner | 82ed58f | 2006-04-02 05:30:25 +0000 | [diff] [blame] | 8087 | case Intrinsic::ppc_altivec_lvx: |
| 8088 | case Intrinsic::ppc_altivec_lvxl: |
Chris Lattner | fd6bdf0 | 2006-04-17 22:26:56 +0000 | [diff] [blame] | 8089 | case Intrinsic::x86_sse_loadu_ps: |
| 8090 | case Intrinsic::x86_sse2_loadu_pd: |
| 8091 | case Intrinsic::x86_sse2_loadu_dq: |
| 8092 | // Turn PPC lvx -> load if the pointer is known aligned. |
| 8093 | // Turn X86 loadups -> load if the pointer is known aligned. |
Chris Lattner | f2369f2 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 8094 | if (GetOrEnforceKnownAlignment(II->getOperand(1), TD, 16) >= 16) { |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 8095 | Value *Ptr = InsertBitCastBefore(II->getOperand(1), |
| 8096 | PointerType::getUnqual(II->getType()), |
| 8097 | CI); |
Chris Lattner | 82ed58f | 2006-04-02 05:30:25 +0000 | [diff] [blame] | 8098 | return new LoadInst(Ptr); |
| 8099 | } |
| 8100 | break; |
| 8101 | case Intrinsic::ppc_altivec_stvx: |
| 8102 | case Intrinsic::ppc_altivec_stvxl: |
| 8103 | // Turn stvx -> store if the pointer is known aligned. |
Chris Lattner | f2369f2 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 8104 | if (GetOrEnforceKnownAlignment(II->getOperand(2), TD, 16) >= 16) { |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 8105 | const Type *OpPtrTy = |
| 8106 | PointerType::getUnqual(II->getOperand(1)->getType()); |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 8107 | Value *Ptr = InsertBitCastBefore(II->getOperand(2), OpPtrTy, CI); |
Chris Lattner | 82ed58f | 2006-04-02 05:30:25 +0000 | [diff] [blame] | 8108 | return new StoreInst(II->getOperand(1), Ptr); |
| 8109 | } |
| 8110 | break; |
Chris Lattner | fd6bdf0 | 2006-04-17 22:26:56 +0000 | [diff] [blame] | 8111 | case Intrinsic::x86_sse_storeu_ps: |
| 8112 | case Intrinsic::x86_sse2_storeu_pd: |
| 8113 | case Intrinsic::x86_sse2_storeu_dq: |
| 8114 | case Intrinsic::x86_sse2_storel_dq: |
| 8115 | // Turn X86 storeu -> store if the pointer is known aligned. |
Chris Lattner | f2369f2 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 8116 | if (GetOrEnforceKnownAlignment(II->getOperand(1), TD, 16) >= 16) { |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 8117 | const Type *OpPtrTy = |
| 8118 | PointerType::getUnqual(II->getOperand(2)->getType()); |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 8119 | Value *Ptr = InsertBitCastBefore(II->getOperand(1), OpPtrTy, CI); |
Chris Lattner | fd6bdf0 | 2006-04-17 22:26:56 +0000 | [diff] [blame] | 8120 | return new StoreInst(II->getOperand(2), Ptr); |
| 8121 | } |
| 8122 | break; |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 8123 | |
| 8124 | case Intrinsic::x86_sse_cvttss2si: { |
| 8125 | // These intrinsics only demands the 0th element of its input vector. If |
| 8126 | // we can simplify the input based on that, do so now. |
| 8127 | uint64_t UndefElts; |
| 8128 | if (Value *V = SimplifyDemandedVectorElts(II->getOperand(1), 1, |
| 8129 | UndefElts)) { |
| 8130 | II->setOperand(1, V); |
| 8131 | return II; |
| 8132 | } |
| 8133 | break; |
| 8134 | } |
| 8135 | |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8136 | case Intrinsic::ppc_altivec_vperm: |
| 8137 | // 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] | 8138 | if (ConstantVector *Mask = dyn_cast<ConstantVector>(II->getOperand(3))) { |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8139 | assert(Mask->getNumOperands() == 16 && "Bad type for intrinsic!"); |
| 8140 | |
| 8141 | // Check that all of the elements are integer constants or undefs. |
| 8142 | bool AllEltsOk = true; |
| 8143 | for (unsigned i = 0; i != 16; ++i) { |
| 8144 | if (!isa<ConstantInt>(Mask->getOperand(i)) && |
| 8145 | !isa<UndefValue>(Mask->getOperand(i))) { |
| 8146 | AllEltsOk = false; |
| 8147 | break; |
| 8148 | } |
| 8149 | } |
| 8150 | |
| 8151 | if (AllEltsOk) { |
| 8152 | // Cast the input vectors to byte vectors. |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 8153 | Value *Op0 =InsertBitCastBefore(II->getOperand(1),Mask->getType(),CI); |
| 8154 | Value *Op1 =InsertBitCastBefore(II->getOperand(2),Mask->getType(),CI); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8155 | Value *Result = UndefValue::get(Op0->getType()); |
| 8156 | |
| 8157 | // Only extract each element once. |
| 8158 | Value *ExtractedElts[32]; |
| 8159 | memset(ExtractedElts, 0, sizeof(ExtractedElts)); |
| 8160 | |
| 8161 | for (unsigned i = 0; i != 16; ++i) { |
| 8162 | if (isa<UndefValue>(Mask->getOperand(i))) |
| 8163 | continue; |
Chris Lattner | e34e9a2 | 2007-04-14 23:32:02 +0000 | [diff] [blame] | 8164 | unsigned Idx=cast<ConstantInt>(Mask->getOperand(i))->getZExtValue(); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8165 | Idx &= 31; // Match the hardware behavior. |
| 8166 | |
| 8167 | if (ExtractedElts[Idx] == 0) { |
| 8168 | Instruction *Elt = |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 8169 | new ExtractElementInst(Idx < 16 ? Op0 : Op1, Idx&15, "tmp"); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8170 | InsertNewInstBefore(Elt, CI); |
| 8171 | ExtractedElts[Idx] = Elt; |
| 8172 | } |
| 8173 | |
| 8174 | // Insert this value into the result vector. |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 8175 | Result = new InsertElementInst(Result, ExtractedElts[Idx], i,"tmp"); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8176 | InsertNewInstBefore(cast<Instruction>(Result), CI); |
| 8177 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8178 | return CastInst::create(Instruction::BitCast, Result, CI.getType()); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8179 | } |
| 8180 | } |
| 8181 | break; |
| 8182 | |
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 8183 | case Intrinsic::stackrestore: { |
| 8184 | // If the save is right next to the restore, remove the restore. This can |
| 8185 | // happen when variable allocas are DCE'd. |
| 8186 | if (IntrinsicInst *SS = dyn_cast<IntrinsicInst>(II->getOperand(1))) { |
| 8187 | if (SS->getIntrinsicID() == Intrinsic::stacksave) { |
| 8188 | BasicBlock::iterator BI = SS; |
| 8189 | if (&*++BI == II) |
| 8190 | return EraseInstFromFunction(CI); |
| 8191 | } |
| 8192 | } |
| 8193 | |
| 8194 | // If the stack restore is in a return/unwind block and if there are no |
| 8195 | // allocas or calls between the restore and the return, nuke the restore. |
| 8196 | TerminatorInst *TI = II->getParent()->getTerminator(); |
| 8197 | if (isa<ReturnInst>(TI) || isa<UnwindInst>(TI)) { |
| 8198 | BasicBlock::iterator BI = II; |
| 8199 | bool CannotRemove = false; |
| 8200 | for (++BI; &*BI != TI; ++BI) { |
| 8201 | if (isa<AllocaInst>(BI) || |
| 8202 | (isa<CallInst>(BI) && !isa<IntrinsicInst>(BI))) { |
| 8203 | CannotRemove = true; |
| 8204 | break; |
| 8205 | } |
| 8206 | } |
| 8207 | if (!CannotRemove) |
| 8208 | return EraseInstFromFunction(CI); |
| 8209 | } |
| 8210 | break; |
| 8211 | } |
| 8212 | } |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8213 | } |
| 8214 | |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 8215 | return visitCallSite(II); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8216 | } |
| 8217 | |
| 8218 | // InvokeInst simplification |
| 8219 | // |
| 8220 | Instruction *InstCombiner::visitInvokeInst(InvokeInst &II) { |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 8221 | return visitCallSite(&II); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8222 | } |
| 8223 | |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 8224 | // visitCallSite - Improvements for call and invoke instructions. |
| 8225 | // |
| 8226 | Instruction *InstCombiner::visitCallSite(CallSite CS) { |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8227 | bool Changed = false; |
| 8228 | |
| 8229 | // If the callee is a constexpr cast of a function, attempt to move the cast |
| 8230 | // to the arguments of the call/invoke. |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 8231 | if (transformConstExprCastCall(CS)) return 0; |
| 8232 | |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8233 | Value *Callee = CS.getCalledValue(); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8234 | |
Chris Lattner | 08b22ec | 2005-05-13 07:09:09 +0000 | [diff] [blame] | 8235 | if (Function *CalleeF = dyn_cast<Function>(Callee)) |
| 8236 | if (CalleeF->getCallingConv() != CS.getCallingConv()) { |
| 8237 | Instruction *OldCall = CS.getInstruction(); |
| 8238 | // If the call and callee calling conventions don't match, this call must |
| 8239 | // be unreachable, as the call is undefined. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 8240 | new StoreInst(ConstantInt::getTrue(), |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 8241 | UndefValue::get(PointerType::getUnqual(Type::Int1Ty)), |
| 8242 | OldCall); |
Chris Lattner | 08b22ec | 2005-05-13 07:09:09 +0000 | [diff] [blame] | 8243 | if (!OldCall->use_empty()) |
| 8244 | OldCall->replaceAllUsesWith(UndefValue::get(OldCall->getType())); |
| 8245 | if (isa<CallInst>(OldCall)) // Not worth removing an invoke here. |
| 8246 | return EraseInstFromFunction(*OldCall); |
| 8247 | return 0; |
| 8248 | } |
| 8249 | |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8250 | if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) { |
| 8251 | // This instruction is not reachable, just remove it. We insert a store to |
| 8252 | // undef so that we know that this code is not reachable, despite the fact |
| 8253 | // that we can't modify the CFG here. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 8254 | new StoreInst(ConstantInt::getTrue(), |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 8255 | UndefValue::get(PointerType::getUnqual(Type::Int1Ty)), |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8256 | CS.getInstruction()); |
| 8257 | |
| 8258 | if (!CS.getInstruction()->use_empty()) |
| 8259 | CS.getInstruction()-> |
| 8260 | replaceAllUsesWith(UndefValue::get(CS.getInstruction()->getType())); |
| 8261 | |
| 8262 | if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) { |
| 8263 | // Don't break the CFG, insert a dummy cond branch. |
| 8264 | new BranchInst(II->getNormalDest(), II->getUnwindDest(), |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 8265 | ConstantInt::getTrue(), II); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8266 | } |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8267 | return EraseInstFromFunction(*CS.getInstruction()); |
| 8268 | } |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8269 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8270 | if (BitCastInst *BC = dyn_cast<BitCastInst>(Callee)) |
| 8271 | if (IntrinsicInst *In = dyn_cast<IntrinsicInst>(BC->getOperand(0))) |
| 8272 | if (In->getIntrinsicID() == Intrinsic::init_trampoline) |
| 8273 | return transformCallThroughTrampoline(CS); |
| 8274 | |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8275 | const PointerType *PTy = cast<PointerType>(Callee->getType()); |
| 8276 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
| 8277 | if (FTy->isVarArg()) { |
| 8278 | // See if we can optimize any arguments passed through the varargs area of |
| 8279 | // the call. |
| 8280 | for (CallSite::arg_iterator I = CS.arg_begin()+FTy->getNumParams(), |
| 8281 | E = CS.arg_end(); I != E; ++I) |
| 8282 | if (CastInst *CI = dyn_cast<CastInst>(*I)) { |
| 8283 | // If this cast does not effect the value passed through the varargs |
| 8284 | // area, we can eliminate the use of the cast. |
| 8285 | Value *Op = CI->getOperand(0); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8286 | if (CI->isLosslessCast()) { |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8287 | *I = Op; |
| 8288 | Changed = true; |
| 8289 | } |
| 8290 | } |
| 8291 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8292 | |
Duncan Sands | f0c3354 | 2007-12-19 21:13:37 +0000 | [diff] [blame] | 8293 | if (isa<InlineAsm>(Callee) && !CS.doesNotThrow()) { |
Duncan Sands | ece2c04 | 2007-12-16 15:51:49 +0000 | [diff] [blame] | 8294 | // Inline asm calls cannot throw - mark them 'nounwind'. |
Duncan Sands | f0c3354 | 2007-12-19 21:13:37 +0000 | [diff] [blame] | 8295 | CS.setDoesNotThrow(); |
Duncan Sands | ece2c04 | 2007-12-16 15:51:49 +0000 | [diff] [blame] | 8296 | Changed = true; |
| 8297 | } |
| 8298 | |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8299 | return Changed ? CS.getInstruction() : 0; |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 8300 | } |
| 8301 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8302 | // transformConstExprCastCall - If the callee is a constexpr cast of a function, |
| 8303 | // attempt to move the cast to the arguments of the call/invoke. |
| 8304 | // |
| 8305 | bool InstCombiner::transformConstExprCastCall(CallSite CS) { |
| 8306 | if (!isa<ConstantExpr>(CS.getCalledValue())) return false; |
| 8307 | ConstantExpr *CE = cast<ConstantExpr>(CS.getCalledValue()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8308 | if (CE->getOpcode() != Instruction::BitCast || |
| 8309 | !isa<Function>(CE->getOperand(0))) |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8310 | return false; |
Reid Spencer | 8863f18 | 2004-07-18 00:38:32 +0000 | [diff] [blame] | 8311 | Function *Callee = cast<Function>(CE->getOperand(0)); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8312 | Instruction *Caller = CS.getInstruction(); |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8313 | const ParamAttrsList* CallerPAL = CS.getParamAttrs(); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8314 | |
| 8315 | // Okay, this is a cast from a function to a different type. Unless doing so |
| 8316 | // would cause a type conversion of one of our arguments, change this call to |
| 8317 | // be a direct call with arguments casted to the appropriate types. |
| 8318 | // |
| 8319 | const FunctionType *FT = Callee->getFunctionType(); |
| 8320 | const Type *OldRetTy = Caller->getType(); |
| 8321 | |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 8322 | // Check to see if we are changing the return type... |
| 8323 | if (OldRetTy != FT->getReturnType()) { |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 8324 | if (Callee->isDeclaration() && !Caller->use_empty() && |
Chris Lattner | 46013f4 | 2007-01-06 19:53:32 +0000 | [diff] [blame] | 8325 | // Conversion is ok if changing from pointer to int of same size. |
| 8326 | !(isa<PointerType>(FT->getReturnType()) && |
| 8327 | TD->getIntPtrType() == OldRetTy)) |
Chris Lattner | ec47992 | 2007-01-06 02:09:32 +0000 | [diff] [blame] | 8328 | return false; // Cannot transform this return value. |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 8329 | |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8330 | if (!Caller->use_empty() && |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8331 | // void -> non-void is handled specially |
Duncan Sands | e1e520f | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 8332 | FT->getReturnType() != Type::VoidTy && |
| 8333 | !CastInst::isCastable(FT->getReturnType(), OldRetTy)) |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8334 | return false; // Cannot transform this return value. |
| 8335 | |
Duncan Sands | 6c3470e | 2008-01-07 17:16:06 +0000 | [diff] [blame] | 8336 | if (CallerPAL && !Caller->use_empty()) { |
| 8337 | uint16_t RAttrs = CallerPAL->getParamAttrs(0); |
| 8338 | if (RAttrs & ParamAttr::typeIncompatible(FT->getReturnType())) |
| 8339 | return false; // Attribute not compatible with transformed value. |
| 8340 | } |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8341 | |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 8342 | // If the callsite is an invoke instruction, and the return value is used by |
| 8343 | // a PHI node in a successor, we cannot change the return type of the call |
| 8344 | // because there is no place to put the cast instruction (without breaking |
| 8345 | // the critical edge). Bail out in this case. |
| 8346 | if (!Caller->use_empty()) |
| 8347 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) |
| 8348 | for (Value::use_iterator UI = II->use_begin(), E = II->use_end(); |
| 8349 | UI != E; ++UI) |
| 8350 | if (PHINode *PN = dyn_cast<PHINode>(*UI)) |
| 8351 | if (PN->getParent() == II->getNormalDest() || |
Chris Lattner | aeb2a1d | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 8352 | PN->getParent() == II->getUnwindDest()) |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 8353 | return false; |
| 8354 | } |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8355 | |
| 8356 | unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin()); |
| 8357 | unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8358 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8359 | CallSite::arg_iterator AI = CS.arg_begin(); |
| 8360 | for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) { |
| 8361 | const Type *ParamTy = FT->getParamType(i); |
Andrew Lenharth | b8e604c | 2006-06-28 01:01:52 +0000 | [diff] [blame] | 8362 | const Type *ActTy = (*AI)->getType(); |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8363 | |
| 8364 | if (!CastInst::isCastable(ActTy, ParamTy)) |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8365 | return false; // Cannot transform this parameter value. |
| 8366 | |
Duncan Sands | 6c3470e | 2008-01-07 17:16:06 +0000 | [diff] [blame] | 8367 | if (CallerPAL) { |
| 8368 | uint16_t PAttrs = CallerPAL->getParamAttrs(i + 1); |
| 8369 | if (PAttrs & ParamAttr::typeIncompatible(ParamTy)) |
| 8370 | return false; // Attribute not compatible with transformed value. |
| 8371 | } |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8372 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8373 | ConstantInt *c = dyn_cast<ConstantInt>(*AI); |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8374 | // Some conversions are safe even if we do not have a body. |
| 8375 | // Either we can cast directly, or we can upconvert the argument |
Chris Lattner | ec47992 | 2007-01-06 02:09:32 +0000 | [diff] [blame] | 8376 | bool isConvertible = ActTy == ParamTy || |
Chris Lattner | 46013f4 | 2007-01-06 19:53:32 +0000 | [diff] [blame] | 8377 | (isa<PointerType>(ParamTy) && isa<PointerType>(ActTy)) || |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 8378 | (ParamTy->isInteger() && ActTy->isInteger() && |
Reid Spencer | abaa8ca | 2007-01-08 16:32:00 +0000 | [diff] [blame] | 8379 | ParamTy->getPrimitiveSizeInBits() >= ActTy->getPrimitiveSizeInBits()) || |
| 8380 | (c && ParamTy->getPrimitiveSizeInBits() >= ActTy->getPrimitiveSizeInBits() |
Zhou Sheng | 0fc5095 | 2007-03-25 05:01:29 +0000 | [diff] [blame] | 8381 | && c->getValue().isStrictlyPositive()); |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 8382 | if (Callee->isDeclaration() && !isConvertible) return false; |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8383 | } |
| 8384 | |
| 8385 | if (FT->getNumParams() < NumActualArgs && !FT->isVarArg() && |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 8386 | Callee->isDeclaration()) |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8387 | return false; // Do not delete arguments unless we have a function body... |
| 8388 | |
Duncan Sands | e1e520f | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 8389 | if (FT->getNumParams() < NumActualArgs && FT->isVarArg() && CallerPAL) |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8390 | // 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] | 8391 | // won't be dropping them. Check that these extra arguments have attributes |
| 8392 | // that are compatible with being a vararg call argument. |
| 8393 | for (unsigned i = CallerPAL->size(); i; --i) { |
| 8394 | if (CallerPAL->getParamIndex(i - 1) <= FT->getNumParams()) |
| 8395 | break; |
| 8396 | uint16_t PAttrs = CallerPAL->getParamAttrsAtIndex(i - 1); |
| 8397 | if (PAttrs & ParamAttr::VarArgsIncompatible) |
| 8398 | return false; |
| 8399 | } |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8400 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8401 | // Okay, we decided that this is a safe thing to do: go ahead and start |
| 8402 | // inserting cast instructions as necessary... |
| 8403 | std::vector<Value*> Args; |
| 8404 | Args.reserve(NumActualArgs); |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8405 | ParamAttrsVector attrVec; |
| 8406 | attrVec.reserve(NumCommonArgs); |
| 8407 | |
| 8408 | // Get any return attributes. |
| 8409 | uint16_t RAttrs = CallerPAL ? CallerPAL->getParamAttrs(0) : 0; |
| 8410 | |
| 8411 | // If the return value is not being used, the type may not be compatible |
| 8412 | // with the existing attributes. Wipe out any problematic attributes. |
Duncan Sands | 6c3470e | 2008-01-07 17:16:06 +0000 | [diff] [blame] | 8413 | RAttrs &= ~ParamAttr::typeIncompatible(FT->getReturnType()); |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8414 | |
| 8415 | // Add the new return attributes. |
| 8416 | if (RAttrs) |
| 8417 | attrVec.push_back(ParamAttrsWithIndex::get(0, RAttrs)); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8418 | |
| 8419 | AI = CS.arg_begin(); |
| 8420 | for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) { |
| 8421 | const Type *ParamTy = FT->getParamType(i); |
| 8422 | if ((*AI)->getType() == ParamTy) { |
| 8423 | Args.push_back(*AI); |
| 8424 | } else { |
Reid Spencer | 8a903db | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 8425 | Instruction::CastOps opcode = CastInst::getCastOpcode(*AI, |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8426 | false, ParamTy, false); |
Reid Spencer | 8a903db | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 8427 | CastInst *NewCast = CastInst::create(opcode, *AI, ParamTy, "tmp"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8428 | Args.push_back(InsertNewInstBefore(NewCast, *Caller)); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8429 | } |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8430 | |
| 8431 | // Add any parameter attributes. |
| 8432 | uint16_t PAttrs = CallerPAL ? CallerPAL->getParamAttrs(i + 1) : 0; |
| 8433 | if (PAttrs) |
| 8434 | attrVec.push_back(ParamAttrsWithIndex::get(i + 1, PAttrs)); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8435 | } |
| 8436 | |
| 8437 | // If the function takes more arguments than the call was taking, add them |
| 8438 | // now... |
| 8439 | for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i) |
| 8440 | Args.push_back(Constant::getNullValue(FT->getParamType(i))); |
| 8441 | |
| 8442 | // If we are removing arguments to the function, emit an obnoxious warning... |
| 8443 | if (FT->getNumParams() < NumActualArgs) |
| 8444 | if (!FT->isVarArg()) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 8445 | cerr << "WARNING: While resolving call to function '" |
| 8446 | << Callee->getName() << "' arguments were dropped!\n"; |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8447 | } else { |
| 8448 | // Add all of the arguments in their promoted form to the arg list... |
| 8449 | for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) { |
| 8450 | const Type *PTy = getPromotedType((*AI)->getType()); |
| 8451 | if (PTy != (*AI)->getType()) { |
| 8452 | // Must promote to pass through va_arg area! |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8453 | Instruction::CastOps opcode = CastInst::getCastOpcode(*AI, false, |
| 8454 | PTy, false); |
Reid Spencer | 8a903db | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 8455 | Instruction *Cast = CastInst::create(opcode, *AI, PTy, "tmp"); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8456 | InsertNewInstBefore(Cast, *Caller); |
| 8457 | Args.push_back(Cast); |
| 8458 | } else { |
| 8459 | Args.push_back(*AI); |
| 8460 | } |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8461 | |
Duncan Sands | e1e520f | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 8462 | // Add any parameter attributes. |
| 8463 | uint16_t PAttrs = CallerPAL ? CallerPAL->getParamAttrs(i + 1) : 0; |
| 8464 | if (PAttrs) |
| 8465 | attrVec.push_back(ParamAttrsWithIndex::get(i + 1, PAttrs)); |
| 8466 | } |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8467 | } |
| 8468 | |
| 8469 | if (FT->getReturnType() == Type::VoidTy) |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 8470 | Caller->setName(""); // Void type should not have a name. |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8471 | |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8472 | const ParamAttrsList* NewCallerPAL = ParamAttrsList::get(attrVec); |
| 8473 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8474 | Instruction *NC; |
| 8475 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
Chris Lattner | aeb2a1d | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 8476 | NC = new InvokeInst(Callee, II->getNormalDest(), II->getUnwindDest(), |
David Greene | f1355a5 | 2007-08-27 19:04:21 +0000 | [diff] [blame] | 8477 | Args.begin(), Args.end(), Caller->getName(), Caller); |
Reid Spencer | ed3fa85 | 2007-07-30 19:53:57 +0000 | [diff] [blame] | 8478 | cast<InvokeInst>(NC)->setCallingConv(II->getCallingConv()); |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8479 | cast<InvokeInst>(NC)->setParamAttrs(NewCallerPAL); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8480 | } else { |
Chris Lattner | 684b22d | 2007-08-02 16:53:43 +0000 | [diff] [blame] | 8481 | NC = new CallInst(Callee, Args.begin(), Args.end(), |
| 8482 | Caller->getName(), Caller); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 8483 | CallInst *CI = cast<CallInst>(Caller); |
| 8484 | if (CI->isTailCall()) |
Chris Lattner | a9e9211 | 2005-05-06 06:48:21 +0000 | [diff] [blame] | 8485 | cast<CallInst>(NC)->setTailCall(); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 8486 | cast<CallInst>(NC)->setCallingConv(CI->getCallingConv()); |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8487 | cast<CallInst>(NC)->setParamAttrs(NewCallerPAL); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8488 | } |
| 8489 | |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 8490 | // Insert a cast of the return type as necessary. |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8491 | Value *NV = NC; |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8492 | if (OldRetTy != NV->getType() && !Caller->use_empty()) { |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8493 | if (NV->getType() != Type::VoidTy) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8494 | Instruction::CastOps opcode = CastInst::getCastOpcode(NC, false, |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8495 | OldRetTy, false); |
| 8496 | NV = NC = CastInst::create(opcode, NC, OldRetTy, "tmp"); |
Chris Lattner | bb60904 | 2003-10-30 00:46:41 +0000 | [diff] [blame] | 8497 | |
| 8498 | // If this is an invoke instruction, we should insert it after the first |
| 8499 | // non-phi, instruction in the normal successor block. |
| 8500 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
| 8501 | BasicBlock::iterator I = II->getNormalDest()->begin(); |
| 8502 | while (isa<PHINode>(I)) ++I; |
| 8503 | InsertNewInstBefore(NC, *I); |
| 8504 | } else { |
| 8505 | // Otherwise, it's a call, just insert cast right after the call instr |
| 8506 | InsertNewInstBefore(NC, *Caller); |
| 8507 | } |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 8508 | AddUsersToWorkList(*Caller); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8509 | } else { |
Chris Lattner | c30bda7 | 2004-10-17 21:22:38 +0000 | [diff] [blame] | 8510 | NV = UndefValue::get(Caller->getType()); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8511 | } |
| 8512 | } |
| 8513 | |
| 8514 | if (Caller->getType() != Type::VoidTy && !Caller->use_empty()) |
| 8515 | Caller->replaceAllUsesWith(NV); |
Chris Lattner | f22a5c6 | 2007-03-02 19:59:19 +0000 | [diff] [blame] | 8516 | Caller->eraseFromParent(); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 8517 | RemoveFromWorkList(Caller); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8518 | return true; |
| 8519 | } |
| 8520 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8521 | // transformCallThroughTrampoline - Turn a call to a function created by the |
| 8522 | // init_trampoline intrinsic into a direct call to the underlying function. |
| 8523 | // |
| 8524 | Instruction *InstCombiner::transformCallThroughTrampoline(CallSite CS) { |
| 8525 | Value *Callee = CS.getCalledValue(); |
| 8526 | const PointerType *PTy = cast<PointerType>(Callee->getType()); |
| 8527 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 8528 | const ParamAttrsList *Attrs = CS.getParamAttrs(); |
| 8529 | |
| 8530 | // If the call already has the 'nest' attribute somewhere then give up - |
| 8531 | // otherwise 'nest' would occur twice after splicing in the chain. |
| 8532 | if (Attrs && Attrs->hasAttrSomewhere(ParamAttr::Nest)) |
| 8533 | return 0; |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8534 | |
| 8535 | IntrinsicInst *Tramp = |
| 8536 | cast<IntrinsicInst>(cast<BitCastInst>(Callee)->getOperand(0)); |
| 8537 | |
| 8538 | Function *NestF = |
| 8539 | cast<Function>(IntrinsicInst::StripPointerCasts(Tramp->getOperand(2))); |
| 8540 | const PointerType *NestFPTy = cast<PointerType>(NestF->getType()); |
| 8541 | const FunctionType *NestFTy = cast<FunctionType>(NestFPTy->getElementType()); |
| 8542 | |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 8543 | if (const ParamAttrsList *NestAttrs = NestF->getParamAttrs()) { |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8544 | unsigned NestIdx = 1; |
| 8545 | const Type *NestTy = 0; |
| 8546 | uint16_t NestAttr = 0; |
| 8547 | |
| 8548 | // Look for a parameter marked with the 'nest' attribute. |
| 8549 | for (FunctionType::param_iterator I = NestFTy->param_begin(), |
| 8550 | E = NestFTy->param_end(); I != E; ++NestIdx, ++I) |
| 8551 | if (NestAttrs->paramHasAttr(NestIdx, ParamAttr::Nest)) { |
| 8552 | // Record the parameter type and any other attributes. |
| 8553 | NestTy = *I; |
| 8554 | NestAttr = NestAttrs->getParamAttrs(NestIdx); |
| 8555 | break; |
| 8556 | } |
| 8557 | |
| 8558 | if (NestTy) { |
| 8559 | Instruction *Caller = CS.getInstruction(); |
| 8560 | std::vector<Value*> NewArgs; |
| 8561 | NewArgs.reserve(unsigned(CS.arg_end()-CS.arg_begin())+1); |
| 8562 | |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 8563 | ParamAttrsVector NewAttrs; |
| 8564 | NewAttrs.reserve(Attrs ? Attrs->size() + 1 : 1); |
| 8565 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8566 | // Insert the nest argument into the call argument list, which may |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 8567 | // mean appending it. Likewise for attributes. |
| 8568 | |
| 8569 | // Add any function result attributes. |
| 8570 | uint16_t Attr = Attrs ? Attrs->getParamAttrs(0) : 0; |
| 8571 | if (Attr) |
| 8572 | NewAttrs.push_back (ParamAttrsWithIndex::get(0, Attr)); |
| 8573 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8574 | { |
| 8575 | unsigned Idx = 1; |
| 8576 | CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); |
| 8577 | do { |
| 8578 | if (Idx == NestIdx) { |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 8579 | // Add the chain argument and attributes. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8580 | Value *NestVal = Tramp->getOperand(3); |
| 8581 | if (NestVal->getType() != NestTy) |
| 8582 | NestVal = new BitCastInst(NestVal, NestTy, "nest", Caller); |
| 8583 | NewArgs.push_back(NestVal); |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 8584 | NewAttrs.push_back(ParamAttrsWithIndex::get(NestIdx, NestAttr)); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8585 | } |
| 8586 | |
| 8587 | if (I == E) |
| 8588 | break; |
| 8589 | |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 8590 | // Add the original argument and attributes. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8591 | NewArgs.push_back(*I); |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 8592 | Attr = Attrs ? Attrs->getParamAttrs(Idx) : 0; |
| 8593 | if (Attr) |
| 8594 | NewAttrs.push_back |
| 8595 | (ParamAttrsWithIndex::get(Idx + (Idx >= NestIdx), Attr)); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8596 | |
| 8597 | ++Idx, ++I; |
| 8598 | } while (1); |
| 8599 | } |
| 8600 | |
| 8601 | // The trampoline may have been bitcast to a bogus type (FTy). |
| 8602 | // Handle this by synthesizing a new function type, equal to FTy |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 8603 | // with the chain parameter inserted. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8604 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8605 | std::vector<const Type*> NewTypes; |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8606 | NewTypes.reserve(FTy->getNumParams()+1); |
| 8607 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8608 | // 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] | 8609 | // mean appending it. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8610 | { |
| 8611 | unsigned Idx = 1; |
| 8612 | FunctionType::param_iterator I = FTy->param_begin(), |
| 8613 | E = FTy->param_end(); |
| 8614 | |
| 8615 | do { |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 8616 | if (Idx == NestIdx) |
| 8617 | // Add the chain's type. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8618 | NewTypes.push_back(NestTy); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8619 | |
| 8620 | if (I == E) |
| 8621 | break; |
| 8622 | |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 8623 | // Add the original type. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8624 | NewTypes.push_back(*I); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8625 | |
| 8626 | ++Idx, ++I; |
| 8627 | } while (1); |
| 8628 | } |
| 8629 | |
| 8630 | // Replace the trampoline call with a direct call. Let the generic |
| 8631 | // code sort out any function type mismatches. |
| 8632 | FunctionType *NewFTy = |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 8633 | FunctionType::get(FTy->getReturnType(), NewTypes, FTy->isVarArg()); |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 8634 | Constant *NewCallee = NestF->getType() == PointerType::getUnqual(NewFTy) ? |
| 8635 | NestF : ConstantExpr::getBitCast(NestF, PointerType::getUnqual(NewFTy)); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 8636 | const ParamAttrsList *NewPAL = ParamAttrsList::get(NewAttrs); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8637 | |
| 8638 | Instruction *NewCaller; |
| 8639 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
| 8640 | NewCaller = new InvokeInst(NewCallee, |
| 8641 | II->getNormalDest(), II->getUnwindDest(), |
| 8642 | NewArgs.begin(), NewArgs.end(), |
| 8643 | Caller->getName(), Caller); |
| 8644 | cast<InvokeInst>(NewCaller)->setCallingConv(II->getCallingConv()); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 8645 | cast<InvokeInst>(NewCaller)->setParamAttrs(NewPAL); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8646 | } else { |
| 8647 | NewCaller = new CallInst(NewCallee, NewArgs.begin(), NewArgs.end(), |
| 8648 | Caller->getName(), Caller); |
| 8649 | if (cast<CallInst>(Caller)->isTailCall()) |
| 8650 | cast<CallInst>(NewCaller)->setTailCall(); |
| 8651 | cast<CallInst>(NewCaller)-> |
| 8652 | setCallingConv(cast<CallInst>(Caller)->getCallingConv()); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 8653 | cast<CallInst>(NewCaller)->setParamAttrs(NewPAL); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8654 | } |
| 8655 | if (Caller->getType() != Type::VoidTy && !Caller->use_empty()) |
| 8656 | Caller->replaceAllUsesWith(NewCaller); |
| 8657 | Caller->eraseFromParent(); |
| 8658 | RemoveFromWorkList(Caller); |
| 8659 | return 0; |
| 8660 | } |
| 8661 | } |
| 8662 | |
| 8663 | // Replace the trampoline call with a direct call. Since there is no 'nest' |
| 8664 | // parameter, there is no need to adjust the argument list. Let the generic |
| 8665 | // code sort out any function type mismatches. |
| 8666 | Constant *NewCallee = |
| 8667 | NestF->getType() == PTy ? NestF : ConstantExpr::getBitCast(NestF, PTy); |
| 8668 | CS.setCalledFunction(NewCallee); |
| 8669 | return CS.getInstruction(); |
| 8670 | } |
| 8671 | |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 8672 | /// FoldPHIArgBinOpIntoPHI - If we have something like phi [add (a,b), add(c,d)] |
| 8673 | /// and if a/b/c/d and the add's all have a single use, turn this into two phi's |
| 8674 | /// and a single binop. |
| 8675 | Instruction *InstCombiner::FoldPHIArgBinOpIntoPHI(PHINode &PN) { |
| 8676 | Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0)); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 8677 | assert(isa<BinaryOperator>(FirstInst) || isa<GetElementPtrInst>(FirstInst) || |
| 8678 | isa<CmpInst>(FirstInst)); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 8679 | unsigned Opc = FirstInst->getOpcode(); |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 8680 | Value *LHSVal = FirstInst->getOperand(0); |
| 8681 | Value *RHSVal = FirstInst->getOperand(1); |
| 8682 | |
| 8683 | const Type *LHSType = LHSVal->getType(); |
| 8684 | const Type *RHSType = RHSVal->getType(); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 8685 | |
| 8686 | // Scan to see if all operands are the same opcode, all have one use, and all |
| 8687 | // kill their operands (i.e. the operands have one use). |
Chris Lattner | a90a24c | 2006-11-01 04:55:47 +0000 | [diff] [blame] | 8688 | for (unsigned i = 0; i != PN.getNumIncomingValues(); ++i) { |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 8689 | Instruction *I = dyn_cast<Instruction>(PN.getIncomingValue(i)); |
Chris Lattner | a90a24c | 2006-11-01 04:55:47 +0000 | [diff] [blame] | 8690 | if (!I || I->getOpcode() != Opc || !I->hasOneUse() || |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8691 | // 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] | 8692 | // types or GEP's with different index types. |
| 8693 | I->getOperand(0)->getType() != LHSType || |
| 8694 | I->getOperand(1)->getType() != RHSType) |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 8695 | return 0; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8696 | |
| 8697 | // If they are CmpInst instructions, check their predicates |
| 8698 | if (Opc == Instruction::ICmp || Opc == Instruction::FCmp) |
| 8699 | if (cast<CmpInst>(I)->getPredicate() != |
| 8700 | cast<CmpInst>(FirstInst)->getPredicate()) |
| 8701 | return 0; |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 8702 | |
| 8703 | // Keep track of which operand needs a phi node. |
| 8704 | if (I->getOperand(0) != LHSVal) LHSVal = 0; |
| 8705 | if (I->getOperand(1) != RHSVal) RHSVal = 0; |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 8706 | } |
| 8707 | |
Chris Lattner | 53738a4 | 2006-11-08 19:42:28 +0000 | [diff] [blame] | 8708 | // Otherwise, this is safe to transform, determine if it is profitable. |
| 8709 | |
| 8710 | // If this is a GEP, and if the index (not the pointer) needs a PHI, bail out. |
| 8711 | // Indexes are often folded into load/store instructions, so we don't want to |
| 8712 | // hide them behind a phi. |
| 8713 | if (isa<GetElementPtrInst>(FirstInst) && RHSVal == 0) |
| 8714 | return 0; |
| 8715 | |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 8716 | Value *InLHS = FirstInst->getOperand(0); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 8717 | Value *InRHS = FirstInst->getOperand(1); |
Chris Lattner | 53738a4 | 2006-11-08 19:42:28 +0000 | [diff] [blame] | 8718 | PHINode *NewLHS = 0, *NewRHS = 0; |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 8719 | if (LHSVal == 0) { |
| 8720 | NewLHS = new PHINode(LHSType, FirstInst->getOperand(0)->getName()+".pn"); |
| 8721 | NewLHS->reserveOperandSpace(PN.getNumOperands()/2); |
| 8722 | NewLHS->addIncoming(InLHS, PN.getIncomingBlock(0)); |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 8723 | InsertNewInstBefore(NewLHS, PN); |
| 8724 | LHSVal = NewLHS; |
| 8725 | } |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 8726 | |
| 8727 | if (RHSVal == 0) { |
| 8728 | NewRHS = new PHINode(RHSType, FirstInst->getOperand(1)->getName()+".pn"); |
| 8729 | NewRHS->reserveOperandSpace(PN.getNumOperands()/2); |
| 8730 | NewRHS->addIncoming(InRHS, PN.getIncomingBlock(0)); |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 8731 | InsertNewInstBefore(NewRHS, PN); |
| 8732 | RHSVal = NewRHS; |
| 8733 | } |
| 8734 | |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 8735 | // Add all operands to the new PHIs. |
| 8736 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 8737 | if (NewLHS) { |
| 8738 | Value *NewInLHS =cast<Instruction>(PN.getIncomingValue(i))->getOperand(0); |
| 8739 | NewLHS->addIncoming(NewInLHS, PN.getIncomingBlock(i)); |
| 8740 | } |
| 8741 | if (NewRHS) { |
| 8742 | Value *NewInRHS =cast<Instruction>(PN.getIncomingValue(i))->getOperand(1); |
| 8743 | NewRHS->addIncoming(NewInRHS, PN.getIncomingBlock(i)); |
| 8744 | } |
| 8745 | } |
| 8746 | |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 8747 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 8748 | return BinaryOperator::create(BinOp->getOpcode(), LHSVal, RHSVal); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8749 | else if (CmpInst *CIOp = dyn_cast<CmpInst>(FirstInst)) |
| 8750 | return CmpInst::create(CIOp->getOpcode(), CIOp->getPredicate(), LHSVal, |
| 8751 | RHSVal); |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 8752 | else { |
| 8753 | assert(isa<GetElementPtrInst>(FirstInst)); |
| 8754 | return new GetElementPtrInst(LHSVal, RHSVal); |
| 8755 | } |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 8756 | } |
| 8757 | |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 8758 | /// isSafeToSinkLoad - Return true if we know that it is safe sink the load out |
| 8759 | /// of the block that defines it. This means that it must be obvious the value |
| 8760 | /// of the load is not changed from the point of the load to the end of the |
| 8761 | /// block it is in. |
Chris Lattner | fd905ca | 2007-02-01 22:30:07 +0000 | [diff] [blame] | 8762 | /// |
| 8763 | /// Finally, it is safe, but not profitable, to sink a load targetting a |
| 8764 | /// non-address-taken alloca. Doing so will cause us to not promote the alloca |
| 8765 | /// to a register. |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 8766 | static bool isSafeToSinkLoad(LoadInst *L) { |
| 8767 | BasicBlock::iterator BBI = L, E = L->getParent()->end(); |
| 8768 | |
| 8769 | for (++BBI; BBI != E; ++BBI) |
| 8770 | if (BBI->mayWriteToMemory()) |
| 8771 | return false; |
Chris Lattner | fd905ca | 2007-02-01 22:30:07 +0000 | [diff] [blame] | 8772 | |
| 8773 | // Check for non-address taken alloca. If not address-taken already, it isn't |
| 8774 | // profitable to do this xform. |
| 8775 | if (AllocaInst *AI = dyn_cast<AllocaInst>(L->getOperand(0))) { |
| 8776 | bool isAddressTaken = false; |
| 8777 | for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); |
| 8778 | UI != E; ++UI) { |
| 8779 | if (isa<LoadInst>(UI)) continue; |
| 8780 | if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) { |
| 8781 | // If storing TO the alloca, then the address isn't taken. |
| 8782 | if (SI->getOperand(1) == AI) continue; |
| 8783 | } |
| 8784 | isAddressTaken = true; |
| 8785 | break; |
| 8786 | } |
| 8787 | |
| 8788 | if (!isAddressTaken) |
| 8789 | return false; |
| 8790 | } |
| 8791 | |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 8792 | return true; |
| 8793 | } |
| 8794 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8795 | |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 8796 | // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary" |
| 8797 | // operator and they all are only used by the PHI, PHI together their |
| 8798 | // inputs, and do the operation once, to the result of the PHI. |
| 8799 | Instruction *InstCombiner::FoldPHIArgOpIntoPHI(PHINode &PN) { |
| 8800 | Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0)); |
| 8801 | |
| 8802 | // Scan the instruction, looking for input operations that can be folded away. |
| 8803 | // If all input operands to the phi are the same instruction (e.g. a cast from |
| 8804 | // the same type or "+42") we can pull the operation through the PHI, reducing |
| 8805 | // code size and simplifying code. |
| 8806 | Constant *ConstantOp = 0; |
| 8807 | const Type *CastSrcTy = 0; |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 8808 | bool isVolatile = false; |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 8809 | if (isa<CastInst>(FirstInst)) { |
| 8810 | CastSrcTy = FirstInst->getOperand(0)->getType(); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 8811 | } else if (isa<BinaryOperator>(FirstInst) || isa<CmpInst>(FirstInst)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8812 | // Can fold binop, compare or shift here if the RHS is a constant, |
| 8813 | // otherwise call FoldPHIArgBinOpIntoPHI. |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 8814 | ConstantOp = dyn_cast<Constant>(FirstInst->getOperand(1)); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 8815 | if (ConstantOp == 0) |
| 8816 | return FoldPHIArgBinOpIntoPHI(PN); |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 8817 | } else if (LoadInst *LI = dyn_cast<LoadInst>(FirstInst)) { |
| 8818 | isVolatile = LI->isVolatile(); |
| 8819 | // We can't sink the load if the loaded value could be modified between the |
| 8820 | // load and the PHI. |
| 8821 | if (LI->getParent() != PN.getIncomingBlock(0) || |
| 8822 | !isSafeToSinkLoad(LI)) |
| 8823 | return 0; |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 8824 | } else if (isa<GetElementPtrInst>(FirstInst)) { |
Chris Lattner | 53738a4 | 2006-11-08 19:42:28 +0000 | [diff] [blame] | 8825 | if (FirstInst->getNumOperands() == 2) |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 8826 | return FoldPHIArgBinOpIntoPHI(PN); |
| 8827 | // Can't handle general GEPs yet. |
| 8828 | return 0; |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 8829 | } else { |
| 8830 | return 0; // Cannot fold this operation. |
| 8831 | } |
| 8832 | |
| 8833 | // Check to see if all arguments are the same operation. |
| 8834 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 8835 | if (!isa<Instruction>(PN.getIncomingValue(i))) return 0; |
| 8836 | Instruction *I = cast<Instruction>(PN.getIncomingValue(i)); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8837 | if (!I->hasOneUse() || !I->isSameOperationAs(FirstInst)) |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 8838 | return 0; |
| 8839 | if (CastSrcTy) { |
| 8840 | if (I->getOperand(0)->getType() != CastSrcTy) |
| 8841 | return 0; // Cast operation must match. |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 8842 | } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8843 | // We can't sink the load if the loaded value could be modified between |
| 8844 | // the load and the PHI. |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 8845 | if (LI->isVolatile() != isVolatile || |
| 8846 | LI->getParent() != PN.getIncomingBlock(i) || |
| 8847 | !isSafeToSinkLoad(LI)) |
| 8848 | return 0; |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 8849 | } else if (I->getOperand(1) != ConstantOp) { |
| 8850 | return 0; |
| 8851 | } |
| 8852 | } |
| 8853 | |
| 8854 | // Okay, they are all the same operation. Create a new PHI node of the |
| 8855 | // correct type, and PHI together all of the LHS's of the instructions. |
| 8856 | PHINode *NewPN = new PHINode(FirstInst->getOperand(0)->getType(), |
| 8857 | PN.getName()+".in"); |
Chris Lattner | 5551706 | 2005-01-29 00:39:08 +0000 | [diff] [blame] | 8858 | NewPN->reserveOperandSpace(PN.getNumOperands()/2); |
Chris Lattner | b589344 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 8859 | |
| 8860 | Value *InVal = FirstInst->getOperand(0); |
| 8861 | NewPN->addIncoming(InVal, PN.getIncomingBlock(0)); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 8862 | |
| 8863 | // Add all operands to the new PHI. |
Chris Lattner | b589344 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 8864 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 8865 | Value *NewInVal = cast<Instruction>(PN.getIncomingValue(i))->getOperand(0); |
| 8866 | if (NewInVal != InVal) |
| 8867 | InVal = 0; |
| 8868 | NewPN->addIncoming(NewInVal, PN.getIncomingBlock(i)); |
| 8869 | } |
| 8870 | |
| 8871 | Value *PhiVal; |
| 8872 | if (InVal) { |
| 8873 | // The new PHI unions all of the same values together. This is really |
| 8874 | // common, so we handle it intelligently here for compile-time speed. |
| 8875 | PhiVal = InVal; |
| 8876 | delete NewPN; |
| 8877 | } else { |
| 8878 | InsertNewInstBefore(NewPN, PN); |
| 8879 | PhiVal = NewPN; |
| 8880 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8881 | |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 8882 | // Insert and return the new operation. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8883 | if (CastInst* FirstCI = dyn_cast<CastInst>(FirstInst)) |
| 8884 | return CastInst::create(FirstCI->getOpcode(), PhiVal, PN.getType()); |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 8885 | else if (isa<LoadInst>(FirstInst)) |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 8886 | return new LoadInst(PhiVal, "", isVolatile); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 8887 | else if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) |
Chris Lattner | b589344 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 8888 | return BinaryOperator::create(BinOp->getOpcode(), PhiVal, ConstantOp); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8889 | else if (CmpInst *CIOp = dyn_cast<CmpInst>(FirstInst)) |
| 8890 | return CmpInst::create(CIOp->getOpcode(), CIOp->getPredicate(), |
| 8891 | PhiVal, ConstantOp); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 8892 | else |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 8893 | assert(0 && "Unknown operation"); |
Jeff Cohen | ca5183d | 2007-03-05 00:00:42 +0000 | [diff] [blame] | 8894 | return 0; |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 8895 | } |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 8896 | |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 8897 | /// DeadPHICycle - Return true if this PHI node is only used by a PHI node cycle |
| 8898 | /// that is dead. |
Chris Lattner | 0e5444b | 2007-03-26 20:40:50 +0000 | [diff] [blame] | 8899 | static bool DeadPHICycle(PHINode *PN, |
| 8900 | SmallPtrSet<PHINode*, 16> &PotentiallyDeadPHIs) { |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 8901 | if (PN->use_empty()) return true; |
| 8902 | if (!PN->hasOneUse()) return false; |
| 8903 | |
| 8904 | // Remember this node, and if we find the cycle, return. |
Chris Lattner | 0e5444b | 2007-03-26 20:40:50 +0000 | [diff] [blame] | 8905 | if (!PotentiallyDeadPHIs.insert(PN)) |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 8906 | return true; |
Chris Lattner | 92103de | 2007-08-28 04:23:55 +0000 | [diff] [blame] | 8907 | |
| 8908 | // Don't scan crazily complex things. |
| 8909 | if (PotentiallyDeadPHIs.size() == 16) |
| 8910 | return false; |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 8911 | |
| 8912 | if (PHINode *PU = dyn_cast<PHINode>(PN->use_back())) |
| 8913 | return DeadPHICycle(PU, PotentiallyDeadPHIs); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8914 | |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 8915 | return false; |
| 8916 | } |
| 8917 | |
Chris Lattner | cf5008a | 2007-11-06 21:52:06 +0000 | [diff] [blame] | 8918 | /// PHIsEqualValue - Return true if this phi node is always equal to |
| 8919 | /// NonPhiInVal. This happens with mutually cyclic phi nodes like: |
| 8920 | /// z = some value; x = phi (y, z); y = phi (x, z) |
| 8921 | static bool PHIsEqualValue(PHINode *PN, Value *NonPhiInVal, |
| 8922 | SmallPtrSet<PHINode*, 16> &ValueEqualPHIs) { |
| 8923 | // See if we already saw this PHI node. |
| 8924 | if (!ValueEqualPHIs.insert(PN)) |
| 8925 | return true; |
| 8926 | |
| 8927 | // Don't scan crazily complex things. |
| 8928 | if (ValueEqualPHIs.size() == 16) |
| 8929 | return false; |
| 8930 | |
| 8931 | // Scan the operands to see if they are either phi nodes or are equal to |
| 8932 | // the value. |
| 8933 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 8934 | Value *Op = PN->getIncomingValue(i); |
| 8935 | if (PHINode *OpPN = dyn_cast<PHINode>(Op)) { |
| 8936 | if (!PHIsEqualValue(OpPN, NonPhiInVal, ValueEqualPHIs)) |
| 8937 | return false; |
| 8938 | } else if (Op != NonPhiInVal) |
| 8939 | return false; |
| 8940 | } |
| 8941 | |
| 8942 | return true; |
| 8943 | } |
| 8944 | |
| 8945 | |
Chris Lattner | 473945d | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 8946 | // PHINode simplification |
| 8947 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 8948 | Instruction *InstCombiner::visitPHINode(PHINode &PN) { |
Owen Anderson | b64ab87 | 2006-07-10 22:15:25 +0000 | [diff] [blame] | 8949 | // If LCSSA is around, don't mess with Phi nodes |
Chris Lattner | f964f32 | 2007-03-04 04:27:24 +0000 | [diff] [blame] | 8950 | if (MustPreserveLCSSA) return 0; |
Owen Anderson | d1b78a1 | 2006-07-10 19:03:49 +0000 | [diff] [blame] | 8951 | |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 8952 | if (Value *V = PN.hasConstantValue()) |
| 8953 | return ReplaceInstUsesWith(PN, V); |
| 8954 | |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 8955 | // If all PHI operands are the same operation, pull them through the PHI, |
| 8956 | // reducing code size. |
| 8957 | if (isa<Instruction>(PN.getIncomingValue(0)) && |
| 8958 | PN.getIncomingValue(0)->hasOneUse()) |
| 8959 | if (Instruction *Result = FoldPHIArgOpIntoPHI(PN)) |
| 8960 | return Result; |
| 8961 | |
| 8962 | // If this is a trivial cycle in the PHI node graph, remove it. Basically, if |
| 8963 | // this PHI only has a single use (a PHI), and if that PHI only has one use (a |
| 8964 | // PHI)... break the cycle. |
Chris Lattner | ff9f13a | 2007-01-15 07:30:06 +0000 | [diff] [blame] | 8965 | if (PN.hasOneUse()) { |
| 8966 | Instruction *PHIUser = cast<Instruction>(PN.use_back()); |
| 8967 | if (PHINode *PU = dyn_cast<PHINode>(PHIUser)) { |
Chris Lattner | 0e5444b | 2007-03-26 20:40:50 +0000 | [diff] [blame] | 8968 | SmallPtrSet<PHINode*, 16> PotentiallyDeadPHIs; |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 8969 | PotentiallyDeadPHIs.insert(&PN); |
| 8970 | if (DeadPHICycle(PU, PotentiallyDeadPHIs)) |
| 8971 | return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType())); |
| 8972 | } |
Chris Lattner | ff9f13a | 2007-01-15 07:30:06 +0000 | [diff] [blame] | 8973 | |
| 8974 | // If this phi has a single use, and if that use just computes a value for |
| 8975 | // the next iteration of a loop, delete the phi. This occurs with unused |
| 8976 | // induction variables, e.g. "for (int j = 0; ; ++j);". Detecting this |
| 8977 | // common case here is good because the only other things that catch this |
| 8978 | // are induction variable analysis (sometimes) and ADCE, which is only run |
| 8979 | // late. |
| 8980 | if (PHIUser->hasOneUse() && |
| 8981 | (isa<BinaryOperator>(PHIUser) || isa<GetElementPtrInst>(PHIUser)) && |
| 8982 | PHIUser->use_back() == &PN) { |
| 8983 | return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType())); |
| 8984 | } |
| 8985 | } |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 8986 | |
Chris Lattner | cf5008a | 2007-11-06 21:52:06 +0000 | [diff] [blame] | 8987 | // We sometimes end up with phi cycles that non-obviously end up being the |
| 8988 | // same value, for example: |
| 8989 | // z = some value; x = phi (y, z); y = phi (x, z) |
| 8990 | // where the phi nodes don't necessarily need to be in the same block. Do a |
| 8991 | // quick check to see if the PHI node only contains a single non-phi value, if |
| 8992 | // so, scan to see if the phi cycle is actually equal to that value. |
| 8993 | { |
| 8994 | unsigned InValNo = 0, NumOperandVals = PN.getNumIncomingValues(); |
| 8995 | // Scan for the first non-phi operand. |
| 8996 | while (InValNo != NumOperandVals && |
| 8997 | isa<PHINode>(PN.getIncomingValue(InValNo))) |
| 8998 | ++InValNo; |
| 8999 | |
| 9000 | if (InValNo != NumOperandVals) { |
| 9001 | Value *NonPhiInVal = PN.getOperand(InValNo); |
| 9002 | |
| 9003 | // Scan the rest of the operands to see if there are any conflicts, if so |
| 9004 | // there is no need to recursively scan other phis. |
| 9005 | for (++InValNo; InValNo != NumOperandVals; ++InValNo) { |
| 9006 | Value *OpVal = PN.getIncomingValue(InValNo); |
| 9007 | if (OpVal != NonPhiInVal && !isa<PHINode>(OpVal)) |
| 9008 | break; |
| 9009 | } |
| 9010 | |
| 9011 | // If we scanned over all operands, then we have one unique value plus |
| 9012 | // phi values. Scan PHI nodes to see if they all merge in each other or |
| 9013 | // the value. |
| 9014 | if (InValNo == NumOperandVals) { |
| 9015 | SmallPtrSet<PHINode*, 16> ValueEqualPHIs; |
| 9016 | if (PHIsEqualValue(&PN, NonPhiInVal, ValueEqualPHIs)) |
| 9017 | return ReplaceInstUsesWith(PN, NonPhiInVal); |
| 9018 | } |
| 9019 | } |
| 9020 | } |
Chris Lattner | 60921c9 | 2003-12-19 05:58:40 +0000 | [diff] [blame] | 9021 | return 0; |
Chris Lattner | 473945d | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 9022 | } |
| 9023 | |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9024 | static Value *InsertCastToIntPtrTy(Value *V, const Type *DTy, |
| 9025 | Instruction *InsertPoint, |
| 9026 | InstCombiner *IC) { |
Reid Spencer | abaa8ca | 2007-01-08 16:32:00 +0000 | [diff] [blame] | 9027 | unsigned PtrSize = DTy->getPrimitiveSizeInBits(); |
| 9028 | unsigned VTySize = V->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9029 | // We must cast correctly to the pointer type. Ensure that we |
| 9030 | // sign extend the integer value if it is smaller as this is |
| 9031 | // used for address computation. |
| 9032 | Instruction::CastOps opcode = |
| 9033 | (VTySize < PtrSize ? Instruction::SExt : |
| 9034 | (VTySize == PtrSize ? Instruction::BitCast : Instruction::Trunc)); |
| 9035 | return IC->InsertCastBefore(opcode, V, DTy, *InsertPoint); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9036 | } |
| 9037 | |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 9038 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 9039 | Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9040 | Value *PtrOp = GEP.getOperand(0); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 9041 | // Is it 'getelementptr %P, i32 0' or 'getelementptr %P' |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 9042 | // If so, eliminate the noop. |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 9043 | if (GEP.getNumOperands() == 1) |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9044 | return ReplaceInstUsesWith(GEP, PtrOp); |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 9045 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 9046 | if (isa<UndefValue>(GEP.getOperand(0))) |
| 9047 | return ReplaceInstUsesWith(GEP, UndefValue::get(GEP.getType())); |
| 9048 | |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 9049 | bool HasZeroPointerIndex = false; |
| 9050 | if (Constant *C = dyn_cast<Constant>(GEP.getOperand(1))) |
| 9051 | HasZeroPointerIndex = C->isNullValue(); |
| 9052 | |
| 9053 | if (GEP.getNumOperands() == 2 && HasZeroPointerIndex) |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9054 | return ReplaceInstUsesWith(GEP, PtrOp); |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 9055 | |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9056 | // Eliminate unneeded casts for indices. |
| 9057 | bool MadeChange = false; |
Chris Lattner | db9654e | 2007-03-25 20:43:09 +0000 | [diff] [blame] | 9058 | |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 9059 | gep_type_iterator GTI = gep_type_begin(GEP); |
Chris Lattner | db9654e | 2007-03-25 20:43:09 +0000 | [diff] [blame] | 9060 | for (unsigned i = 1, e = GEP.getNumOperands(); i != e; ++i, ++GTI) { |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 9061 | if (isa<SequentialType>(*GTI)) { |
| 9062 | if (CastInst *CI = dyn_cast<CastInst>(GEP.getOperand(i))) { |
Chris Lattner | 76b7a06 | 2007-01-15 07:02:54 +0000 | [diff] [blame] | 9063 | if (CI->getOpcode() == Instruction::ZExt || |
| 9064 | CI->getOpcode() == Instruction::SExt) { |
| 9065 | const Type *SrcTy = CI->getOperand(0)->getType(); |
| 9066 | // We can eliminate a cast from i32 to i64 iff the target |
| 9067 | // is a 32-bit pointer target. |
| 9068 | if (SrcTy->getPrimitiveSizeInBits() >= TD->getPointerSizeInBits()) { |
| 9069 | MadeChange = true; |
| 9070 | GEP.setOperand(i, CI->getOperand(0)); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9071 | } |
| 9072 | } |
| 9073 | } |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 9074 | // If we are using a wider index than needed for this platform, shrink it |
| 9075 | // to what we need. If the incoming value needs a cast instruction, |
| 9076 | // insert it. This explicit cast can make subsequent optimizations more |
| 9077 | // obvious. |
| 9078 | Value *Op = GEP.getOperand(i); |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 9079 | if (TD->getTypeSizeInBits(Op->getType()) > TD->getPointerSizeInBits()) |
Chris Lattner | 4f1134e | 2004-04-17 18:16:10 +0000 | [diff] [blame] | 9080 | if (Constant *C = dyn_cast<Constant>(Op)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9081 | GEP.setOperand(i, ConstantExpr::getTrunc(C, TD->getIntPtrType())); |
Chris Lattner | 4f1134e | 2004-04-17 18:16:10 +0000 | [diff] [blame] | 9082 | MadeChange = true; |
| 9083 | } else { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9084 | Op = InsertCastBefore(Instruction::Trunc, Op, TD->getIntPtrType(), |
| 9085 | GEP); |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 9086 | GEP.setOperand(i, Op); |
| 9087 | MadeChange = true; |
| 9088 | } |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9089 | } |
Chris Lattner | db9654e | 2007-03-25 20:43:09 +0000 | [diff] [blame] | 9090 | } |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9091 | if (MadeChange) return &GEP; |
| 9092 | |
Chris Lattner | db9654e | 2007-03-25 20:43:09 +0000 | [diff] [blame] | 9093 | // If this GEP instruction doesn't move the pointer, and if the input operand |
| 9094 | // is a bitcast of another pointer, just replace the GEP with a bitcast of the |
| 9095 | // real input to the dest type. |
Chris Lattner | 6a94de2 | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 9096 | if (GEP.hasAllZeroIndices()) { |
| 9097 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(GEP.getOperand(0))) { |
| 9098 | // If the bitcast is of an allocation, and the allocation will be |
| 9099 | // converted to match the type of the cast, don't touch this. |
| 9100 | if (isa<AllocationInst>(BCI->getOperand(0))) { |
| 9101 | // 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] | 9102 | if (Instruction *I = visitBitCast(*BCI)) { |
| 9103 | if (I != BCI) { |
| 9104 | I->takeName(BCI); |
| 9105 | BCI->getParent()->getInstList().insert(BCI, I); |
| 9106 | ReplaceInstUsesWith(*BCI, I); |
| 9107 | } |
Chris Lattner | 6a94de2 | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 9108 | return &GEP; |
Chris Lattner | a79dd43 | 2007-10-12 18:05:47 +0000 | [diff] [blame] | 9109 | } |
Chris Lattner | 6a94de2 | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 9110 | } |
| 9111 | return new BitCastInst(BCI->getOperand(0), GEP.getType()); |
| 9112 | } |
| 9113 | } |
| 9114 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9115 | // Combine Indices - If the source pointer to this getelementptr instruction |
| 9116 | // is a getelementptr instruction, combine the indices of the two |
| 9117 | // getelementptr instructions into a single instruction. |
| 9118 | // |
Chris Lattner | 72588fc | 2007-02-15 22:48:32 +0000 | [diff] [blame] | 9119 | SmallVector<Value*, 8> SrcGEPOperands; |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 9120 | if (User *Src = dyn_castGetElementPtr(PtrOp)) |
Chris Lattner | 72588fc | 2007-02-15 22:48:32 +0000 | [diff] [blame] | 9121 | SrcGEPOperands.append(Src->op_begin(), Src->op_end()); |
Chris Lattner | ebd985c | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 9122 | |
| 9123 | if (!SrcGEPOperands.empty()) { |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9124 | // Note that if our source is a gep chain itself that we wait for that |
| 9125 | // chain to be resolved before we perform this transformation. This |
| 9126 | // avoids us creating a TON of code in some cases. |
| 9127 | // |
| 9128 | if (isa<GetElementPtrInst>(SrcGEPOperands[0]) && |
| 9129 | cast<Instruction>(SrcGEPOperands[0])->getNumOperands() == 2) |
| 9130 | return 0; // Wait until our source is folded to completion. |
| 9131 | |
Chris Lattner | 72588fc | 2007-02-15 22:48:32 +0000 | [diff] [blame] | 9132 | SmallVector<Value*, 8> Indices; |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9133 | |
| 9134 | // Find out whether the last index in the source GEP is a sequential idx. |
| 9135 | bool EndsWithSequential = false; |
| 9136 | for (gep_type_iterator I = gep_type_begin(*cast<User>(PtrOp)), |
| 9137 | E = gep_type_end(*cast<User>(PtrOp)); I != E; ++I) |
Chris Lattner | be97b4e | 2004-05-08 22:41:42 +0000 | [diff] [blame] | 9138 | EndsWithSequential = !isa<StructType>(*I); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9139 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9140 | // Can we combine the two pointer arithmetics offsets? |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9141 | if (EndsWithSequential) { |
Chris Lattner | decd081 | 2003-03-05 22:33:14 +0000 | [diff] [blame] | 9142 | // Replace: gep (gep %P, long B), long A, ... |
| 9143 | // With: T = long A+B; gep %P, T, ... |
| 9144 | // |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9145 | Value *Sum, *SO1 = SrcGEPOperands.back(), *GO1 = GEP.getOperand(1); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9146 | if (SO1 == Constant::getNullValue(SO1->getType())) { |
| 9147 | Sum = GO1; |
| 9148 | } else if (GO1 == Constant::getNullValue(GO1->getType())) { |
| 9149 | Sum = SO1; |
| 9150 | } else { |
| 9151 | // If they aren't the same type, convert both to an integer of the |
| 9152 | // target's pointer size. |
| 9153 | if (SO1->getType() != GO1->getType()) { |
| 9154 | if (Constant *SO1C = dyn_cast<Constant>(SO1)) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9155 | SO1 = ConstantExpr::getIntegerCast(SO1C, GO1->getType(), true); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9156 | } else if (Constant *GO1C = dyn_cast<Constant>(GO1)) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9157 | GO1 = ConstantExpr::getIntegerCast(GO1C, SO1->getType(), true); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9158 | } else { |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 9159 | unsigned PS = TD->getPointerSizeInBits(); |
| 9160 | if (TD->getTypeSizeInBits(SO1->getType()) == PS) { |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9161 | // Convert GO1 to SO1's type. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9162 | GO1 = InsertCastToIntPtrTy(GO1, SO1->getType(), &GEP, this); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9163 | |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 9164 | } else if (TD->getTypeSizeInBits(GO1->getType()) == PS) { |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9165 | // Convert SO1 to GO1's type. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9166 | SO1 = InsertCastToIntPtrTy(SO1, GO1->getType(), &GEP, this); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9167 | } else { |
| 9168 | const Type *PT = TD->getIntPtrType(); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9169 | SO1 = InsertCastToIntPtrTy(SO1, PT, &GEP, this); |
| 9170 | GO1 = InsertCastToIntPtrTy(GO1, PT, &GEP, this); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9171 | } |
| 9172 | } |
| 9173 | } |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9174 | if (isa<Constant>(SO1) && isa<Constant>(GO1)) |
| 9175 | Sum = ConstantExpr::getAdd(cast<Constant>(SO1), cast<Constant>(GO1)); |
| 9176 | else { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 9177 | Sum = BinaryOperator::createAdd(SO1, GO1, PtrOp->getName()+".sum"); |
| 9178 | InsertNewInstBefore(cast<Instruction>(Sum), GEP); |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9179 | } |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9180 | } |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9181 | |
| 9182 | // Recycle the GEP we already have if possible. |
| 9183 | if (SrcGEPOperands.size() == 2) { |
| 9184 | GEP.setOperand(0, SrcGEPOperands[0]); |
| 9185 | GEP.setOperand(1, Sum); |
| 9186 | return &GEP; |
| 9187 | } else { |
| 9188 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, |
| 9189 | SrcGEPOperands.end()-1); |
| 9190 | Indices.push_back(Sum); |
| 9191 | Indices.insert(Indices.end(), GEP.op_begin()+2, GEP.op_end()); |
| 9192 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9193 | } else if (isa<Constant>(*GEP.idx_begin()) && |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9194 | cast<Constant>(*GEP.idx_begin())->isNullValue() && |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9195 | SrcGEPOperands.size() != 1) { |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9196 | // 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] | 9197 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, |
| 9198 | SrcGEPOperands.end()); |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9199 | Indices.insert(Indices.end(), GEP.idx_begin()+1, GEP.idx_end()); |
| 9200 | } |
| 9201 | |
| 9202 | if (!Indices.empty()) |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 9203 | return new GetElementPtrInst(SrcGEPOperands[0], Indices.begin(), |
| 9204 | Indices.end(), GEP.getName()); |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 9205 | |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9206 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(PtrOp)) { |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 9207 | // GEP of global variable. If all of the indices for this GEP are |
| 9208 | // constants, we can promote this to a constexpr instead of an instruction. |
| 9209 | |
| 9210 | // Scan for nonconstants... |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 9211 | SmallVector<Constant*, 8> Indices; |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 9212 | User::op_iterator I = GEP.idx_begin(), E = GEP.idx_end(); |
| 9213 | for (; I != E && isa<Constant>(*I); ++I) |
| 9214 | Indices.push_back(cast<Constant>(*I)); |
| 9215 | |
| 9216 | if (I == E) { // If they are all constants... |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 9217 | Constant *CE = ConstantExpr::getGetElementPtr(GV, |
| 9218 | &Indices[0],Indices.size()); |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 9219 | |
| 9220 | // Replace all uses of the GEP with the new constexpr... |
| 9221 | return ReplaceInstUsesWith(GEP, CE); |
| 9222 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9223 | } else if (Value *X = getBitCastOperand(PtrOp)) { // Is the operand a cast? |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 9224 | if (!isa<PointerType>(X->getType())) { |
| 9225 | // Not interesting. Source pointer must be a cast from pointer. |
| 9226 | } else if (HasZeroPointerIndex) { |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9227 | // transform: GEP (bitcast [10 x i8]* X to [0 x i8]*), i32 0, ... |
| 9228 | // into : GEP [10 x i8]* X, i32 0, ... |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 9229 | // |
| 9230 | // This occurs when the program declares an array extern like "int X[];" |
| 9231 | // |
| 9232 | const PointerType *CPTy = cast<PointerType>(PtrOp->getType()); |
| 9233 | const PointerType *XTy = cast<PointerType>(X->getType()); |
| 9234 | if (const ArrayType *XATy = |
| 9235 | dyn_cast<ArrayType>(XTy->getElementType())) |
| 9236 | if (const ArrayType *CATy = |
| 9237 | dyn_cast<ArrayType>(CPTy->getElementType())) |
| 9238 | if (CATy->getElementType() == XATy->getElementType()) { |
| 9239 | // At this point, we know that the cast source type is a pointer |
| 9240 | // to an array of the same type as the destination pointer |
| 9241 | // array. Because the array type is never stepped over (there |
| 9242 | // is a leading zero) we can fold the cast into this GEP. |
| 9243 | GEP.setOperand(0, X); |
| 9244 | return &GEP; |
| 9245 | } |
| 9246 | } else if (GEP.getNumOperands() == 2) { |
| 9247 | // Transform things like: |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9248 | // %t = getelementptr i32* bitcast ([2 x i32]* %str to i32*), i32 %V |
| 9249 | // into: %t1 = getelementptr [2 x i32]* %str, i32 0, i32 %V; bitcast |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 9250 | const Type *SrcElTy = cast<PointerType>(X->getType())->getElementType(); |
| 9251 | const Type *ResElTy=cast<PointerType>(PtrOp->getType())->getElementType(); |
| 9252 | if (isa<ArrayType>(SrcElTy) && |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 9253 | TD->getABITypeSize(cast<ArrayType>(SrcElTy)->getElementType()) == |
| 9254 | TD->getABITypeSize(ResElTy)) { |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 9255 | Value *Idx[2]; |
| 9256 | Idx[0] = Constant::getNullValue(Type::Int32Ty); |
| 9257 | Idx[1] = GEP.getOperand(1); |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 9258 | Value *V = InsertNewInstBefore( |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 9259 | new GetElementPtrInst(X, Idx, Idx + 2, GEP.getName()), GEP); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9260 | // V and GEP are both pointer types --> BitCast |
| 9261 | return new BitCastInst(V, GEP.getType()); |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 9262 | } |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9263 | |
| 9264 | // Transform things like: |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9265 | // getelementptr i8* bitcast ([100 x double]* X to i8*), i32 %tmp |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9266 | // (where tmp = 8*tmp2) into: |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9267 | // getelementptr [100 x double]* %arr, i32 0, i32 %tmp2; bitcast |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9268 | |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9269 | if (isa<ArrayType>(SrcElTy) && ResElTy == Type::Int8Ty) { |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9270 | uint64_t ArrayEltSize = |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 9271 | TD->getABITypeSize(cast<ArrayType>(SrcElTy)->getElementType()); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9272 | |
| 9273 | // Check to see if "tmp" is a scale by a multiple of ArrayEltSize. We |
| 9274 | // allow either a mul, shift, or constant here. |
| 9275 | Value *NewIdx = 0; |
| 9276 | ConstantInt *Scale = 0; |
| 9277 | if (ArrayEltSize == 1) { |
| 9278 | NewIdx = GEP.getOperand(1); |
| 9279 | Scale = ConstantInt::get(NewIdx->getType(), 1); |
| 9280 | } else if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP.getOperand(1))) { |
Chris Lattner | 6e2f843 | 2005-09-14 17:32:56 +0000 | [diff] [blame] | 9281 | NewIdx = ConstantInt::get(CI->getType(), 1); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9282 | Scale = CI; |
| 9283 | } else if (Instruction *Inst =dyn_cast<Instruction>(GEP.getOperand(1))){ |
| 9284 | if (Inst->getOpcode() == Instruction::Shl && |
| 9285 | isa<ConstantInt>(Inst->getOperand(1))) { |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 9286 | ConstantInt *ShAmt = cast<ConstantInt>(Inst->getOperand(1)); |
| 9287 | uint32_t ShAmtVal = ShAmt->getLimitedValue(64); |
| 9288 | Scale = ConstantInt::get(Inst->getType(), 1ULL << ShAmtVal); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9289 | NewIdx = Inst->getOperand(0); |
| 9290 | } else if (Inst->getOpcode() == Instruction::Mul && |
| 9291 | isa<ConstantInt>(Inst->getOperand(1))) { |
| 9292 | Scale = cast<ConstantInt>(Inst->getOperand(1)); |
| 9293 | NewIdx = Inst->getOperand(0); |
| 9294 | } |
| 9295 | } |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9296 | |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9297 | // 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] | 9298 | // out, perform the transformation. Note, we don't know whether Scale is |
| 9299 | // signed or not. We'll use unsigned version of division/modulo |
| 9300 | // operation after making sure Scale doesn't have the sign bit set. |
| 9301 | if (Scale && Scale->getSExtValue() >= 0LL && |
| 9302 | Scale->getZExtValue() % ArrayEltSize == 0) { |
| 9303 | Scale = ConstantInt::get(Scale->getType(), |
| 9304 | Scale->getZExtValue() / ArrayEltSize); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 9305 | if (Scale->getZExtValue() != 1) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9306 | Constant *C = ConstantExpr::getIntegerCast(Scale, NewIdx->getType(), |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9307 | false /*ZExt*/); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9308 | Instruction *Sc = BinaryOperator::createMul(NewIdx, C, "idxscale"); |
| 9309 | NewIdx = InsertNewInstBefore(Sc, GEP); |
| 9310 | } |
| 9311 | |
| 9312 | // Insert the new GEP instruction. |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 9313 | Value *Idx[2]; |
| 9314 | Idx[0] = Constant::getNullValue(Type::Int32Ty); |
| 9315 | Idx[1] = NewIdx; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9316 | Instruction *NewGEP = |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 9317 | new GetElementPtrInst(X, Idx, Idx + 2, GEP.getName()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9318 | NewGEP = InsertNewInstBefore(NewGEP, GEP); |
| 9319 | // The NewGEP must be pointer typed, so must the old one -> BitCast |
| 9320 | return new BitCastInst(NewGEP, GEP.getType()); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9321 | } |
| 9322 | } |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 9323 | } |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9324 | } |
| 9325 | |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9326 | return 0; |
| 9327 | } |
| 9328 | |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 9329 | Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) { |
| 9330 | // Convert: malloc Ty, C - where C is a constant != 1 into: malloc [C x Ty], 1 |
| 9331 | if (AI.isArrayAllocation()) // Check C != 1 |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 9332 | if (const ConstantInt *C = dyn_cast<ConstantInt>(AI.getArraySize())) { |
| 9333 | const Type *NewTy = |
| 9334 | ArrayType::get(AI.getAllocatedType(), C->getZExtValue()); |
Chris Lattner | 0006bd7 | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 9335 | AllocationInst *New = 0; |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 9336 | |
| 9337 | // Create and insert the replacement instruction... |
| 9338 | if (isa<MallocInst>(AI)) |
Nate Begeman | 14b0529 | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 9339 | New = new MallocInst(NewTy, 0, AI.getAlignment(), AI.getName()); |
Chris Lattner | 0006bd7 | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 9340 | else { |
| 9341 | assert(isa<AllocaInst>(AI) && "Unknown type of allocation inst!"); |
Nate Begeman | 14b0529 | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 9342 | New = new AllocaInst(NewTy, 0, AI.getAlignment(), AI.getName()); |
Chris Lattner | 0006bd7 | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 9343 | } |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 9344 | |
| 9345 | InsertNewInstBefore(New, AI); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9346 | |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 9347 | // Scan to the end of the allocation instructions, to skip over a block of |
| 9348 | // allocas if possible... |
| 9349 | // |
| 9350 | BasicBlock::iterator It = New; |
| 9351 | while (isa<AllocationInst>(*It)) ++It; |
| 9352 | |
| 9353 | // Now that I is pointing to the first non-allocation-inst in the block, |
| 9354 | // insert our getelementptr instruction... |
| 9355 | // |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9356 | Value *NullIdx = Constant::getNullValue(Type::Int32Ty); |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 9357 | Value *Idx[2]; |
| 9358 | Idx[0] = NullIdx; |
| 9359 | Idx[1] = NullIdx; |
| 9360 | Value *V = new GetElementPtrInst(New, Idx, Idx + 2, |
Chris Lattner | 693787a | 2005-05-04 19:10:26 +0000 | [diff] [blame] | 9361 | New->getName()+".sub", It); |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 9362 | |
| 9363 | // Now make everything use the getelementptr instead of the original |
| 9364 | // allocation. |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 9365 | return ReplaceInstUsesWith(AI, V); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 9366 | } else if (isa<UndefValue>(AI.getArraySize())) { |
| 9367 | return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 9368 | } |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 9369 | |
| 9370 | // If alloca'ing a zero byte object, replace the alloca with a null pointer. |
| 9371 | // Note that we only do this for alloca's, because malloc should allocate and |
| 9372 | // return a unique pointer, even for a zero byte allocation. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9373 | if (isa<AllocaInst>(AI) && AI.getAllocatedType()->isSized() && |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 9374 | TD->getABITypeSize(AI.getAllocatedType()) == 0) |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 9375 | return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); |
| 9376 | |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 9377 | return 0; |
| 9378 | } |
| 9379 | |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 9380 | Instruction *InstCombiner::visitFreeInst(FreeInst &FI) { |
| 9381 | Value *Op = FI.getOperand(0); |
| 9382 | |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 9383 | // free undef -> unreachable. |
| 9384 | if (isa<UndefValue>(Op)) { |
| 9385 | // 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] | 9386 | new StoreInst(ConstantInt::getTrue(), |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 9387 | UndefValue::get(PointerType::getUnqual(Type::Int1Ty)), &FI); |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 9388 | return EraseInstFromFunction(FI); |
| 9389 | } |
Chris Lattner | 6fe5541 | 2007-04-14 00:20:02 +0000 | [diff] [blame] | 9390 | |
Chris Lattner | 6160e85 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 9391 | // If we have 'free null' delete the instruction. This can happen in stl code |
| 9392 | // when lots of inlining happens. |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 9393 | if (isa<ConstantPointerNull>(Op)) |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 9394 | return EraseInstFromFunction(FI); |
Chris Lattner | 6fe5541 | 2007-04-14 00:20:02 +0000 | [diff] [blame] | 9395 | |
| 9396 | // Change free <ty>* (cast <ty2>* X to <ty>*) into free <ty2>* X |
| 9397 | if (BitCastInst *CI = dyn_cast<BitCastInst>(Op)) { |
| 9398 | FI.setOperand(0, CI->getOperand(0)); |
| 9399 | return &FI; |
| 9400 | } |
| 9401 | |
| 9402 | // Change free (gep X, 0,0,0,0) into free(X) |
| 9403 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) { |
| 9404 | if (GEPI->hasAllZeroIndices()) { |
| 9405 | AddToWorkList(GEPI); |
| 9406 | FI.setOperand(0, GEPI->getOperand(0)); |
| 9407 | return &FI; |
| 9408 | } |
| 9409 | } |
| 9410 | |
| 9411 | // Change free(malloc) into nothing, if the malloc has a single use. |
| 9412 | if (MallocInst *MI = dyn_cast<MallocInst>(Op)) |
| 9413 | if (MI->hasOneUse()) { |
| 9414 | EraseInstFromFunction(FI); |
| 9415 | return EraseInstFromFunction(*MI); |
| 9416 | } |
Chris Lattner | 6160e85 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 9417 | |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 9418 | return 0; |
| 9419 | } |
| 9420 | |
| 9421 | |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 9422 | /// InstCombineLoadCast - Fold 'load (cast P)' -> cast (load P)' when possible. |
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 9423 | static Instruction *InstCombineLoadCast(InstCombiner &IC, LoadInst &LI, |
| 9424 | const TargetData *TD) { |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 9425 | User *CI = cast<User>(LI.getOperand(0)); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 9426 | Value *CastOp = CI->getOperand(0); |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 9427 | |
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 9428 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(CI)) { |
| 9429 | // Instead of loading constant c string, use corresponding integer value |
| 9430 | // directly if string length is small enough. |
| 9431 | const std::string &Str = CE->getOperand(0)->getStringValue(); |
| 9432 | if (!Str.empty()) { |
| 9433 | unsigned len = Str.length(); |
| 9434 | const Type *Ty = cast<PointerType>(CE->getType())->getElementType(); |
| 9435 | unsigned numBits = Ty->getPrimitiveSizeInBits(); |
| 9436 | // Replace LI with immediate integer store. |
| 9437 | if ((numBits >> 3) == len + 1) { |
| 9438 | APInt StrVal(numBits, 0); |
| 9439 | APInt SingleChar(numBits, 0); |
| 9440 | if (TD->isLittleEndian()) { |
| 9441 | for (signed i = len-1; i >= 0; i--) { |
| 9442 | SingleChar = (uint64_t) Str[i]; |
| 9443 | StrVal = (StrVal << 8) | SingleChar; |
| 9444 | } |
| 9445 | } else { |
| 9446 | for (unsigned i = 0; i < len; i++) { |
| 9447 | SingleChar = (uint64_t) Str[i]; |
| 9448 | StrVal = (StrVal << 8) | SingleChar; |
| 9449 | } |
| 9450 | // Append NULL at the end. |
| 9451 | SingleChar = 0; |
| 9452 | StrVal = (StrVal << 8) | SingleChar; |
| 9453 | } |
| 9454 | Value *NL = ConstantInt::get(StrVal); |
| 9455 | return IC.ReplaceInstUsesWith(LI, NL); |
| 9456 | } |
| 9457 | } |
| 9458 | } |
| 9459 | |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 9460 | const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType(); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 9461 | if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) { |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 9462 | const Type *SrcPTy = SrcTy->getElementType(); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 9463 | |
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 9464 | if (DestPTy->isInteger() || isa<PointerType>(DestPTy) || |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 9465 | isa<VectorType>(DestPTy)) { |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 9466 | // If the source is an array, the code below will not succeed. Check to |
| 9467 | // see if a trivial 'gep P, 0, 0' will help matters. Only do this for |
| 9468 | // constants. |
| 9469 | if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy)) |
| 9470 | if (Constant *CSrc = dyn_cast<Constant>(CastOp)) |
| 9471 | if (ASrcTy->getNumElements() != 0) { |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 9472 | Value *Idxs[2]; |
| 9473 | Idxs[0] = Idxs[1] = Constant::getNullValue(Type::Int32Ty); |
| 9474 | CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs, 2); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 9475 | SrcTy = cast<PointerType>(CastOp->getType()); |
| 9476 | SrcPTy = SrcTy->getElementType(); |
| 9477 | } |
| 9478 | |
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 9479 | if ((SrcPTy->isInteger() || isa<PointerType>(SrcPTy) || |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 9480 | isa<VectorType>(SrcPTy)) && |
Chris Lattner | b1515fe | 2005-03-29 06:37:47 +0000 | [diff] [blame] | 9481 | // Do not allow turning this into a load of an integer, which is then |
| 9482 | // casted to a pointer, this pessimizes pointer analysis a lot. |
| 9483 | (isa<PointerType>(SrcPTy) == isa<PointerType>(LI.getType())) && |
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 9484 | IC.getTargetData().getTypeSizeInBits(SrcPTy) == |
| 9485 | IC.getTargetData().getTypeSizeInBits(DestPTy)) { |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9486 | |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 9487 | // Okay, we are casting from one integer or pointer type to another of |
| 9488 | // the same size. Instead of casting the pointer before the load, cast |
| 9489 | // the result of the loaded value. |
| 9490 | Value *NewLoad = IC.InsertNewInstBefore(new LoadInst(CastOp, |
| 9491 | CI->getName(), |
| 9492 | LI.isVolatile()),LI); |
| 9493 | // Now cast the result of the load. |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 9494 | return new BitCastInst(NewLoad, LI.getType()); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 9495 | } |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 9496 | } |
| 9497 | } |
| 9498 | return 0; |
| 9499 | } |
| 9500 | |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 9501 | /// isSafeToLoadUnconditionally - Return true if we know that executing a load |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 9502 | /// from this value cannot trap. If it is not obviously safe to load from the |
| 9503 | /// specified pointer, we do a quick local scan of the basic block containing |
| 9504 | /// ScanFrom, to determine if the address is already accessed. |
| 9505 | static bool isSafeToLoadUnconditionally(Value *V, Instruction *ScanFrom) { |
Duncan Sands | 892c7e4 | 2007-09-19 10:10:31 +0000 | [diff] [blame] | 9506 | // If it is an alloca it is always safe to load from. |
| 9507 | if (isa<AllocaInst>(V)) return true; |
| 9508 | |
Duncan Sands | 46318cd | 2007-09-19 10:25:38 +0000 | [diff] [blame] | 9509 | // 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] | 9510 | if (const GlobalValue *GV = dyn_cast<GlobalVariable>(V)) |
Duncan Sands | 46318cd | 2007-09-19 10:25:38 +0000 | [diff] [blame] | 9511 | // Don't try to evaluate aliases. External weak GV can be null. |
Duncan Sands | 892c7e4 | 2007-09-19 10:10:31 +0000 | [diff] [blame] | 9512 | return !isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage(); |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 9513 | |
| 9514 | // Otherwise, be a little bit agressive by scanning the local block where we |
| 9515 | // 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] | 9516 | // from/to. If so, the previous load or store would have already trapped, |
| 9517 | // so there is no harm doing an extra load (also, CSE will later eliminate |
| 9518 | // the load entirely). |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 9519 | BasicBlock::iterator BBI = ScanFrom, E = ScanFrom->getParent()->begin(); |
| 9520 | |
Alkis Evlogimenos | 7b6ec60 | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 9521 | while (BBI != E) { |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 9522 | --BBI; |
| 9523 | |
| 9524 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { |
| 9525 | if (LI->getOperand(0) == V) return true; |
| 9526 | } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) |
| 9527 | if (SI->getOperand(1) == V) return true; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9528 | |
Alkis Evlogimenos | 7b6ec60 | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 9529 | } |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 9530 | return false; |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 9531 | } |
| 9532 | |
Chris Lattner | 8d2e888 | 2007-08-11 18:48:48 +0000 | [diff] [blame] | 9533 | /// GetUnderlyingObject - Trace through a series of getelementptrs and bitcasts |
| 9534 | /// until we find the underlying object a pointer is referring to or something |
| 9535 | /// we don't understand. Note that the returned pointer may be offset from the |
| 9536 | /// input, because we ignore GEP indices. |
| 9537 | static Value *GetUnderlyingObject(Value *Ptr) { |
| 9538 | while (1) { |
| 9539 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) { |
| 9540 | if (CE->getOpcode() == Instruction::BitCast || |
| 9541 | CE->getOpcode() == Instruction::GetElementPtr) |
| 9542 | Ptr = CE->getOperand(0); |
| 9543 | else |
| 9544 | return Ptr; |
| 9545 | } else if (BitCastInst *BCI = dyn_cast<BitCastInst>(Ptr)) { |
| 9546 | Ptr = BCI->getOperand(0); |
| 9547 | } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) { |
| 9548 | Ptr = GEP->getOperand(0); |
| 9549 | } else { |
| 9550 | return Ptr; |
| 9551 | } |
| 9552 | } |
| 9553 | } |
| 9554 | |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 9555 | Instruction *InstCombiner::visitLoadInst(LoadInst &LI) { |
| 9556 | Value *Op = LI.getOperand(0); |
Chris Lattner | 5f16a13 | 2004-01-12 04:13:56 +0000 | [diff] [blame] | 9557 | |
Dan Gohman | 9941f74 | 2007-07-20 16:34:21 +0000 | [diff] [blame] | 9558 | // Attempt to improve the alignment. |
Chris Lattner | f2369f2 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 9559 | unsigned KnownAlign = GetOrEnforceKnownAlignment(Op, TD); |
Dan Gohman | 9941f74 | 2007-07-20 16:34:21 +0000 | [diff] [blame] | 9560 | if (KnownAlign > LI.getAlignment()) |
| 9561 | LI.setAlignment(KnownAlign); |
| 9562 | |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 9563 | // load (cast X) --> cast (load X) iff safe |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 9564 | if (isa<CastInst>(Op)) |
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 9565 | if (Instruction *Res = InstCombineLoadCast(*this, LI, TD)) |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 9566 | return Res; |
| 9567 | |
| 9568 | // None of the following transforms are legal for volatile loads. |
| 9569 | if (LI.isVolatile()) return 0; |
Chris Lattner | 62f254d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 9570 | |
Chris Lattner | 62f254d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 9571 | if (&LI.getParent()->front() != &LI) { |
| 9572 | BasicBlock::iterator BBI = &LI; --BBI; |
Chris Lattner | 9c1f0fd | 2005-09-12 22:21:03 +0000 | [diff] [blame] | 9573 | // If the instruction immediately before this is a store to the same |
| 9574 | // address, do a simple form of store->load forwarding. |
Chris Lattner | 62f254d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 9575 | if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) |
| 9576 | if (SI->getOperand(1) == LI.getOperand(0)) |
| 9577 | return ReplaceInstUsesWith(LI, SI->getOperand(0)); |
Chris Lattner | 9c1f0fd | 2005-09-12 22:21:03 +0000 | [diff] [blame] | 9578 | if (LoadInst *LIB = dyn_cast<LoadInst>(BBI)) |
| 9579 | if (LIB->getOperand(0) == LI.getOperand(0)) |
| 9580 | return ReplaceInstUsesWith(LI, LIB); |
Chris Lattner | 62f254d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 9581 | } |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 9582 | |
Christopher Lamb | b15147e | 2007-12-29 07:56:53 +0000 | [diff] [blame] | 9583 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) { |
| 9584 | const Value *GEPI0 = GEPI->getOperand(0); |
| 9585 | // TODO: Consider a target hook for valid address spaces for this xform. |
| 9586 | if (isa<ConstantPointerNull>(GEPI0) && |
| 9587 | cast<PointerType>(GEPI0->getType())->getAddressSpace() == 0) { |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 9588 | // Insert a new store to null instruction before the load to indicate |
| 9589 | // that this code is not reachable. We do this instead of inserting |
| 9590 | // an unreachable instruction directly because we cannot modify the |
| 9591 | // CFG. |
| 9592 | new StoreInst(UndefValue::get(LI.getType()), |
| 9593 | Constant::getNullValue(Op->getType()), &LI); |
| 9594 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
| 9595 | } |
Christopher Lamb | b15147e | 2007-12-29 07:56:53 +0000 | [diff] [blame] | 9596 | } |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 9597 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 9598 | if (Constant *C = dyn_cast<Constant>(Op)) { |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 9599 | // load null/undef -> undef |
Christopher Lamb | b15147e | 2007-12-29 07:56:53 +0000 | [diff] [blame] | 9600 | // TODO: Consider a target hook for valid address spaces for this xform. |
| 9601 | if (isa<UndefValue>(C) || (C->isNullValue() && |
| 9602 | cast<PointerType>(Op->getType())->getAddressSpace() == 0)) { |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 9603 | // Insert a new store to null instruction before the load to indicate that |
| 9604 | // this code is not reachable. We do this instead of inserting an |
| 9605 | // unreachable instruction directly because we cannot modify the CFG. |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 9606 | new StoreInst(UndefValue::get(LI.getType()), |
| 9607 | Constant::getNullValue(Op->getType()), &LI); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 9608 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 9609 | } |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 9610 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 9611 | // Instcombine load (constant global) into the value loaded. |
| 9612 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op)) |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 9613 | if (GV->isConstant() && !GV->isDeclaration()) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 9614 | return ReplaceInstUsesWith(LI, GV->getInitializer()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9615 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 9616 | // Instcombine load (constantexpr_GEP global, 0, ...) into the value loaded. |
| 9617 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op)) |
| 9618 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 9619 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0))) |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 9620 | if (GV->isConstant() && !GV->isDeclaration()) |
Chris Lattner | 363f2a2 | 2005-09-26 05:28:06 +0000 | [diff] [blame] | 9621 | if (Constant *V = |
| 9622 | ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE)) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 9623 | return ReplaceInstUsesWith(LI, V); |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 9624 | if (CE->getOperand(0)->isNullValue()) { |
| 9625 | // Insert a new store to null instruction before the load to indicate |
| 9626 | // that this code is not reachable. We do this instead of inserting |
| 9627 | // an unreachable instruction directly because we cannot modify the |
| 9628 | // CFG. |
| 9629 | new StoreInst(UndefValue::get(LI.getType()), |
| 9630 | Constant::getNullValue(Op->getType()), &LI); |
| 9631 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
| 9632 | } |
| 9633 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9634 | } else if (CE->isCast()) { |
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 9635 | if (Instruction *Res = InstCombineLoadCast(*this, LI, TD)) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 9636 | return Res; |
| 9637 | } |
| 9638 | } |
Chris Lattner | 8d2e888 | 2007-08-11 18:48:48 +0000 | [diff] [blame] | 9639 | |
| 9640 | // If this load comes from anywhere in a constant global, and if the global |
| 9641 | // is all undef or zero, we know what it loads. |
| 9642 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GetUnderlyingObject(Op))) { |
| 9643 | if (GV->isConstant() && GV->hasInitializer()) { |
| 9644 | if (GV->getInitializer()->isNullValue()) |
| 9645 | return ReplaceInstUsesWith(LI, Constant::getNullValue(LI.getType())); |
| 9646 | else if (isa<UndefValue>(GV->getInitializer())) |
| 9647 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
| 9648 | } |
| 9649 | } |
Chris Lattner | f499eac | 2004-04-08 20:39:49 +0000 | [diff] [blame] | 9650 | |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 9651 | if (Op->hasOneUse()) { |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 9652 | // Change select and PHI nodes to select values instead of addresses: this |
| 9653 | // helps alias analysis out a lot, allows many others simplifications, and |
| 9654 | // exposes redundancy in the code. |
| 9655 | // |
| 9656 | // Note that we cannot do the transformation unless we know that the |
| 9657 | // introduced loads cannot trap! Something like this is valid as long as |
| 9658 | // the condition is always false: load (select bool %C, int* null, int* %G), |
| 9659 | // but it would not be valid if we transformed it to load from null |
| 9660 | // unconditionally. |
| 9661 | // |
| 9662 | if (SelectInst *SI = dyn_cast<SelectInst>(Op)) { |
| 9663 | // load (select (Cond, &V1, &V2)) --> select(Cond, load &V1, load &V2). |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 9664 | if (isSafeToLoadUnconditionally(SI->getOperand(1), SI) && |
| 9665 | isSafeToLoadUnconditionally(SI->getOperand(2), SI)) { |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 9666 | Value *V1 = InsertNewInstBefore(new LoadInst(SI->getOperand(1), |
Chris Lattner | 79f0c8e | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 9667 | SI->getOperand(1)->getName()+".val"), LI); |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 9668 | Value *V2 = InsertNewInstBefore(new LoadInst(SI->getOperand(2), |
Chris Lattner | 79f0c8e | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 9669 | SI->getOperand(2)->getName()+".val"), LI); |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 9670 | return new SelectInst(SI->getCondition(), V1, V2); |
| 9671 | } |
| 9672 | |
Chris Lattner | 684fe21 | 2004-09-23 15:46:00 +0000 | [diff] [blame] | 9673 | // load (select (cond, null, P)) -> load P |
| 9674 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(1))) |
| 9675 | if (C->isNullValue()) { |
| 9676 | LI.setOperand(0, SI->getOperand(2)); |
| 9677 | return &LI; |
| 9678 | } |
| 9679 | |
| 9680 | // load (select (cond, P, null)) -> load P |
| 9681 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(2))) |
| 9682 | if (C->isNullValue()) { |
| 9683 | LI.setOperand(0, SI->getOperand(1)); |
| 9684 | return &LI; |
| 9685 | } |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 9686 | } |
| 9687 | } |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 9688 | return 0; |
| 9689 | } |
| 9690 | |
Reid Spencer | 55af2b5 | 2007-01-19 21:20:31 +0000 | [diff] [blame] | 9691 | /// InstCombineStoreToCast - Fold store V, (cast P) -> store (cast V), P |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 9692 | /// when possible. |
| 9693 | static Instruction *InstCombineStoreToCast(InstCombiner &IC, StoreInst &SI) { |
| 9694 | User *CI = cast<User>(SI.getOperand(1)); |
| 9695 | Value *CastOp = CI->getOperand(0); |
| 9696 | |
| 9697 | const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType(); |
| 9698 | if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) { |
| 9699 | const Type *SrcPTy = SrcTy->getElementType(); |
| 9700 | |
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 9701 | if (DestPTy->isInteger() || isa<PointerType>(DestPTy)) { |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 9702 | // If the source is an array, the code below will not succeed. Check to |
| 9703 | // see if a trivial 'gep P, 0, 0' will help matters. Only do this for |
| 9704 | // constants. |
| 9705 | if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy)) |
| 9706 | if (Constant *CSrc = dyn_cast<Constant>(CastOp)) |
| 9707 | if (ASrcTy->getNumElements() != 0) { |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 9708 | Value* Idxs[2]; |
| 9709 | Idxs[0] = Idxs[1] = Constant::getNullValue(Type::Int32Ty); |
| 9710 | CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs, 2); |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 9711 | SrcTy = cast<PointerType>(CastOp->getType()); |
| 9712 | SrcPTy = SrcTy->getElementType(); |
| 9713 | } |
| 9714 | |
Reid Spencer | 67f827c | 2007-01-20 23:35:48 +0000 | [diff] [blame] | 9715 | if ((SrcPTy->isInteger() || isa<PointerType>(SrcPTy)) && |
| 9716 | IC.getTargetData().getTypeSizeInBits(SrcPTy) == |
| 9717 | IC.getTargetData().getTypeSizeInBits(DestPTy)) { |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 9718 | |
| 9719 | // 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] | 9720 | // the same size. Instead of casting the pointer before |
| 9721 | // the store, cast the value to be stored. |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 9722 | Value *NewCast; |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 9723 | Value *SIOp0 = SI.getOperand(0); |
Reid Spencer | 7515396 | 2007-01-18 18:54:33 +0000 | [diff] [blame] | 9724 | Instruction::CastOps opcode = Instruction::BitCast; |
| 9725 | const Type* CastSrcTy = SIOp0->getType(); |
| 9726 | const Type* CastDstTy = SrcPTy; |
| 9727 | if (isa<PointerType>(CastDstTy)) { |
| 9728 | if (CastSrcTy->isInteger()) |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 9729 | opcode = Instruction::IntToPtr; |
Reid Spencer | 67f827c | 2007-01-20 23:35:48 +0000 | [diff] [blame] | 9730 | } else if (isa<IntegerType>(CastDstTy)) { |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 9731 | if (isa<PointerType>(SIOp0->getType())) |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 9732 | opcode = Instruction::PtrToInt; |
| 9733 | } |
| 9734 | if (Constant *C = dyn_cast<Constant>(SIOp0)) |
Reid Spencer | 7515396 | 2007-01-18 18:54:33 +0000 | [diff] [blame] | 9735 | NewCast = ConstantExpr::getCast(opcode, C, CastDstTy); |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 9736 | else |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9737 | NewCast = IC.InsertNewInstBefore( |
Reid Spencer | 7515396 | 2007-01-18 18:54:33 +0000 | [diff] [blame] | 9738 | CastInst::create(opcode, SIOp0, CastDstTy, SIOp0->getName()+".c"), |
| 9739 | SI); |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 9740 | return new StoreInst(NewCast, CastOp); |
| 9741 | } |
| 9742 | } |
| 9743 | } |
| 9744 | return 0; |
| 9745 | } |
| 9746 | |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 9747 | Instruction *InstCombiner::visitStoreInst(StoreInst &SI) { |
| 9748 | Value *Val = SI.getOperand(0); |
| 9749 | Value *Ptr = SI.getOperand(1); |
| 9750 | |
| 9751 | if (isa<UndefValue>(Ptr)) { // store X, undef -> noop (even if volatile) |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 9752 | EraseInstFromFunction(SI); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 9753 | ++NumCombined; |
| 9754 | return 0; |
| 9755 | } |
Chris Lattner | 836692d | 2007-01-15 06:51:56 +0000 | [diff] [blame] | 9756 | |
| 9757 | // If the RHS is an alloca with a single use, zapify the store, making the |
| 9758 | // alloca dead. |
| 9759 | if (Ptr->hasOneUse()) { |
| 9760 | if (isa<AllocaInst>(Ptr)) { |
| 9761 | EraseInstFromFunction(SI); |
| 9762 | ++NumCombined; |
| 9763 | return 0; |
| 9764 | } |
| 9765 | |
| 9766 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) |
| 9767 | if (isa<AllocaInst>(GEP->getOperand(0)) && |
| 9768 | GEP->getOperand(0)->hasOneUse()) { |
| 9769 | EraseInstFromFunction(SI); |
| 9770 | ++NumCombined; |
| 9771 | return 0; |
| 9772 | } |
| 9773 | } |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 9774 | |
Dan Gohman | 9941f74 | 2007-07-20 16:34:21 +0000 | [diff] [blame] | 9775 | // Attempt to improve the alignment. |
Chris Lattner | f2369f2 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 9776 | unsigned KnownAlign = GetOrEnforceKnownAlignment(Ptr, TD); |
Dan Gohman | 9941f74 | 2007-07-20 16:34:21 +0000 | [diff] [blame] | 9777 | if (KnownAlign > SI.getAlignment()) |
| 9778 | SI.setAlignment(KnownAlign); |
| 9779 | |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 9780 | // Do really simple DSE, to catch cases where there are several consequtive |
| 9781 | // stores to the same location, separated by a few arithmetic operations. This |
| 9782 | // situation often occurs with bitfield accesses. |
| 9783 | BasicBlock::iterator BBI = &SI; |
| 9784 | for (unsigned ScanInsts = 6; BBI != SI.getParent()->begin() && ScanInsts; |
| 9785 | --ScanInsts) { |
| 9786 | --BBI; |
| 9787 | |
| 9788 | if (StoreInst *PrevSI = dyn_cast<StoreInst>(BBI)) { |
| 9789 | // Prev store isn't volatile, and stores to the same location? |
| 9790 | if (!PrevSI->isVolatile() && PrevSI->getOperand(1) == SI.getOperand(1)) { |
| 9791 | ++NumDeadStore; |
| 9792 | ++BBI; |
| 9793 | EraseInstFromFunction(*PrevSI); |
| 9794 | continue; |
| 9795 | } |
| 9796 | break; |
| 9797 | } |
| 9798 | |
Chris Lattner | b4db97f | 2006-05-26 19:19:20 +0000 | [diff] [blame] | 9799 | // If this is a load, we have to stop. However, if the loaded value is from |
| 9800 | // the pointer we're loading and is producing the pointer we're storing, |
| 9801 | // then *this* store is dead (X = load P; store X -> P). |
| 9802 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { |
Chris Lattner | a54c7eb | 2007-09-07 05:33:03 +0000 | [diff] [blame] | 9803 | if (LI == Val && LI->getOperand(0) == Ptr && !SI.isVolatile()) { |
Chris Lattner | b4db97f | 2006-05-26 19:19:20 +0000 | [diff] [blame] | 9804 | EraseInstFromFunction(SI); |
| 9805 | ++NumCombined; |
| 9806 | return 0; |
| 9807 | } |
| 9808 | // Otherwise, this is a load from some other location. Stores before it |
| 9809 | // may not be dead. |
| 9810 | break; |
| 9811 | } |
| 9812 | |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 9813 | // Don't skip over loads or things that can modify memory. |
Chris Lattner | b4db97f | 2006-05-26 19:19:20 +0000 | [diff] [blame] | 9814 | if (BBI->mayWriteToMemory()) |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 9815 | break; |
| 9816 | } |
| 9817 | |
| 9818 | |
| 9819 | if (SI.isVolatile()) return 0; // Don't hack volatile stores. |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 9820 | |
| 9821 | // store X, null -> turns into 'unreachable' in SimplifyCFG |
| 9822 | if (isa<ConstantPointerNull>(Ptr)) { |
| 9823 | if (!isa<UndefValue>(Val)) { |
| 9824 | SI.setOperand(0, UndefValue::get(Val->getType())); |
| 9825 | if (Instruction *U = dyn_cast<Instruction>(Val)) |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9826 | AddToWorkList(U); // Dropped a use. |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 9827 | ++NumCombined; |
| 9828 | } |
| 9829 | return 0; // Do not modify these! |
| 9830 | } |
| 9831 | |
| 9832 | // store undef, Ptr -> noop |
| 9833 | if (isa<UndefValue>(Val)) { |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 9834 | EraseInstFromFunction(SI); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 9835 | ++NumCombined; |
| 9836 | return 0; |
| 9837 | } |
| 9838 | |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 9839 | // If the pointer destination is a cast, see if we can fold the cast into the |
| 9840 | // source instead. |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 9841 | if (isa<CastInst>(Ptr)) |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 9842 | if (Instruction *Res = InstCombineStoreToCast(*this, SI)) |
| 9843 | return Res; |
| 9844 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9845 | if (CE->isCast()) |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 9846 | if (Instruction *Res = InstCombineStoreToCast(*this, SI)) |
| 9847 | return Res; |
| 9848 | |
Chris Lattner | 408902b | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 9849 | |
| 9850 | // If this store is the last instruction in the basic block, and if the block |
| 9851 | // 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] | 9852 | BBI = &SI; ++BBI; |
Chris Lattner | 408902b | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 9853 | if (BranchInst *BI = dyn_cast<BranchInst>(BBI)) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 9854 | if (BI->isUnconditional()) |
| 9855 | if (SimplifyStoreAtEndOfBlock(SI)) |
| 9856 | return 0; // xform done! |
Chris Lattner | 408902b | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 9857 | |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 9858 | return 0; |
| 9859 | } |
| 9860 | |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 9861 | /// SimplifyStoreAtEndOfBlock - Turn things like: |
| 9862 | /// if () { *P = v1; } else { *P = v2 } |
| 9863 | /// into a phi node with a store in the successor. |
| 9864 | /// |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 9865 | /// Simplify things like: |
| 9866 | /// *P = v1; if () { *P = v2; } |
| 9867 | /// into a phi node with a store in the successor. |
| 9868 | /// |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 9869 | bool InstCombiner::SimplifyStoreAtEndOfBlock(StoreInst &SI) { |
| 9870 | BasicBlock *StoreBB = SI.getParent(); |
| 9871 | |
| 9872 | // Check to see if the successor block has exactly two incoming edges. If |
| 9873 | // so, see if the other predecessor contains a store to the same location. |
| 9874 | // 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] | 9875 | BasicBlock *DestBB = StoreBB->getTerminator()->getSuccessor(0); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 9876 | |
| 9877 | // Determine whether Dest has exactly two predecessors and, if so, compute |
| 9878 | // the other predecessor. |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 9879 | pred_iterator PI = pred_begin(DestBB); |
| 9880 | BasicBlock *OtherBB = 0; |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 9881 | if (*PI != StoreBB) |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 9882 | OtherBB = *PI; |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 9883 | ++PI; |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 9884 | if (PI == pred_end(DestBB)) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 9885 | return false; |
| 9886 | |
| 9887 | if (*PI != StoreBB) { |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 9888 | if (OtherBB) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 9889 | return false; |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 9890 | OtherBB = *PI; |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 9891 | } |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 9892 | if (++PI != pred_end(DestBB)) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 9893 | return false; |
| 9894 | |
| 9895 | |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 9896 | // Verify that the other block ends in a branch and is not otherwise empty. |
| 9897 | BasicBlock::iterator BBI = OtherBB->getTerminator(); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 9898 | BranchInst *OtherBr = dyn_cast<BranchInst>(BBI); |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 9899 | if (!OtherBr || BBI == OtherBB->begin()) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 9900 | return false; |
| 9901 | |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 9902 | // If the other block ends in an unconditional branch, check for the 'if then |
| 9903 | // else' case. there is an instruction before the branch. |
| 9904 | StoreInst *OtherStore = 0; |
| 9905 | if (OtherBr->isUnconditional()) { |
| 9906 | // If this isn't a store, or isn't a store to the same location, bail out. |
| 9907 | --BBI; |
| 9908 | OtherStore = dyn_cast<StoreInst>(BBI); |
| 9909 | if (!OtherStore || OtherStore->getOperand(1) != SI.getOperand(1)) |
| 9910 | return false; |
| 9911 | } else { |
Chris Lattner | d717c18 | 2007-05-05 22:32:24 +0000 | [diff] [blame] | 9912 | // 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] | 9913 | // destinations is StoreBB, then we have the if/then case. |
| 9914 | if (OtherBr->getSuccessor(0) != StoreBB && |
| 9915 | OtherBr->getSuccessor(1) != StoreBB) |
| 9916 | return false; |
| 9917 | |
| 9918 | // 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] | 9919 | // if/then triangle. See if there is a store to the same ptr as SI that |
| 9920 | // lives in OtherBB. |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 9921 | for (;; --BBI) { |
| 9922 | // Check to see if we find the matching store. |
| 9923 | if ((OtherStore = dyn_cast<StoreInst>(BBI))) { |
| 9924 | if (OtherStore->getOperand(1) != SI.getOperand(1)) |
| 9925 | return false; |
| 9926 | break; |
| 9927 | } |
Chris Lattner | d717c18 | 2007-05-05 22:32:24 +0000 | [diff] [blame] | 9928 | // If we find something that may be using the stored value, or if we run |
| 9929 | // out of instructions, we can't do the xform. |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 9930 | if (isa<LoadInst>(BBI) || BBI->mayWriteToMemory() || |
| 9931 | BBI == OtherBB->begin()) |
| 9932 | return false; |
| 9933 | } |
| 9934 | |
| 9935 | // In order to eliminate the store in OtherBr, we have to |
| 9936 | // make sure nothing reads the stored value in StoreBB. |
| 9937 | for (BasicBlock::iterator I = StoreBB->begin(); &*I != &SI; ++I) { |
| 9938 | // FIXME: This should really be AA driven. |
| 9939 | if (isa<LoadInst>(I) || I->mayWriteToMemory()) |
| 9940 | return false; |
| 9941 | } |
| 9942 | } |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 9943 | |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 9944 | // Insert a PHI node now if we need it. |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 9945 | Value *MergedVal = OtherStore->getOperand(0); |
| 9946 | if (MergedVal != SI.getOperand(0)) { |
| 9947 | PHINode *PN = new PHINode(MergedVal->getType(), "storemerge"); |
| 9948 | PN->reserveOperandSpace(2); |
| 9949 | PN->addIncoming(SI.getOperand(0), SI.getParent()); |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 9950 | PN->addIncoming(OtherStore->getOperand(0), OtherBB); |
| 9951 | MergedVal = InsertNewInstBefore(PN, DestBB->front()); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 9952 | } |
| 9953 | |
| 9954 | // Advance to a place where it is safe to insert the new store and |
| 9955 | // insert it. |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 9956 | BBI = DestBB->begin(); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 9957 | while (isa<PHINode>(BBI)) ++BBI; |
| 9958 | InsertNewInstBefore(new StoreInst(MergedVal, SI.getOperand(1), |
| 9959 | OtherStore->isVolatile()), *BBI); |
| 9960 | |
| 9961 | // Nuke the old stores. |
| 9962 | EraseInstFromFunction(SI); |
| 9963 | EraseInstFromFunction(*OtherStore); |
| 9964 | ++NumCombined; |
| 9965 | return true; |
| 9966 | } |
| 9967 | |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 9968 | |
Chris Lattner | c4d10eb | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 9969 | Instruction *InstCombiner::visitBranchInst(BranchInst &BI) { |
| 9970 | // 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] | 9971 | Value *X = 0; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 9972 | BasicBlock *TrueDest; |
| 9973 | BasicBlock *FalseDest; |
| 9974 | if (match(&BI, m_Br(m_Not(m_Value(X)), TrueDest, FalseDest)) && |
| 9975 | !isa<Constant>(X)) { |
| 9976 | // Swap Destinations and condition... |
| 9977 | BI.setCondition(X); |
| 9978 | BI.setSuccessor(0, FalseDest); |
| 9979 | BI.setSuccessor(1, TrueDest); |
| 9980 | return &BI; |
| 9981 | } |
| 9982 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9983 | // Cannonicalize fcmp_one -> fcmp_oeq |
| 9984 | FCmpInst::Predicate FPred; Value *Y; |
| 9985 | if (match(&BI, m_Br(m_FCmp(FPred, m_Value(X), m_Value(Y)), |
| 9986 | TrueDest, FalseDest))) |
| 9987 | if ((FPred == FCmpInst::FCMP_ONE || FPred == FCmpInst::FCMP_OLE || |
| 9988 | FPred == FCmpInst::FCMP_OGE) && BI.getCondition()->hasOneUse()) { |
| 9989 | FCmpInst *I = cast<FCmpInst>(BI.getCondition()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9990 | FCmpInst::Predicate NewPred = FCmpInst::getInversePredicate(FPred); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 9991 | Instruction *NewSCC = new FCmpInst(NewPred, X, Y, "", I); |
| 9992 | NewSCC->takeName(I); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9993 | // Swap Destinations and condition... |
| 9994 | BI.setCondition(NewSCC); |
| 9995 | BI.setSuccessor(0, FalseDest); |
| 9996 | BI.setSuccessor(1, TrueDest); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9997 | RemoveFromWorkList(I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 9998 | I->eraseFromParent(); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9999 | AddToWorkList(NewSCC); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10000 | return &BI; |
| 10001 | } |
| 10002 | |
| 10003 | // Cannonicalize icmp_ne -> icmp_eq |
| 10004 | ICmpInst::Predicate IPred; |
| 10005 | if (match(&BI, m_Br(m_ICmp(IPred, m_Value(X), m_Value(Y)), |
| 10006 | TrueDest, FalseDest))) |
| 10007 | if ((IPred == ICmpInst::ICMP_NE || IPred == ICmpInst::ICMP_ULE || |
| 10008 | IPred == ICmpInst::ICMP_SLE || IPred == ICmpInst::ICMP_UGE || |
| 10009 | IPred == ICmpInst::ICMP_SGE) && BI.getCondition()->hasOneUse()) { |
| 10010 | ICmpInst *I = cast<ICmpInst>(BI.getCondition()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10011 | ICmpInst::Predicate NewPred = ICmpInst::getInversePredicate(IPred); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 10012 | Instruction *NewSCC = new ICmpInst(NewPred, X, Y, "", I); |
| 10013 | NewSCC->takeName(I); |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 10014 | // Swap Destinations and condition... |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 10015 | BI.setCondition(NewSCC); |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 10016 | BI.setSuccessor(0, FalseDest); |
| 10017 | BI.setSuccessor(1, TrueDest); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10018 | RemoveFromWorkList(I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 10019 | I->eraseFromParent();; |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10020 | AddToWorkList(NewSCC); |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 10021 | return &BI; |
| 10022 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 10023 | |
Chris Lattner | c4d10eb | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 10024 | return 0; |
| 10025 | } |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 10026 | |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 10027 | Instruction *InstCombiner::visitSwitchInst(SwitchInst &SI) { |
| 10028 | Value *Cond = SI.getCondition(); |
| 10029 | if (Instruction *I = dyn_cast<Instruction>(Cond)) { |
| 10030 | if (I->getOpcode() == Instruction::Add) |
| 10031 | if (ConstantInt *AddRHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 10032 | // change 'switch (X+4) case 1:' into 'switch (X) case -3' |
| 10033 | for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10034 | SI.setOperand(i,ConstantExpr::getSub(cast<Constant>(SI.getOperand(i)), |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 10035 | AddRHS)); |
| 10036 | SI.setOperand(0, I->getOperand(0)); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10037 | AddToWorkList(I); |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 10038 | return &SI; |
| 10039 | } |
| 10040 | } |
| 10041 | return 0; |
| 10042 | } |
| 10043 | |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10044 | /// CheapToScalarize - Return true if the value is cheaper to scalarize than it |
| 10045 | /// is to leave as a vector operation. |
| 10046 | static bool CheapToScalarize(Value *V, bool isConstant) { |
| 10047 | if (isa<ConstantAggregateZero>(V)) |
| 10048 | return true; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10049 | if (ConstantVector *C = dyn_cast<ConstantVector>(V)) { |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10050 | if (isConstant) return true; |
| 10051 | // If all elts are the same, we can extract. |
| 10052 | Constant *Op0 = C->getOperand(0); |
| 10053 | for (unsigned i = 1; i < C->getNumOperands(); ++i) |
| 10054 | if (C->getOperand(i) != Op0) |
| 10055 | return false; |
| 10056 | return true; |
| 10057 | } |
| 10058 | Instruction *I = dyn_cast<Instruction>(V); |
| 10059 | if (!I) return false; |
| 10060 | |
| 10061 | // Insert element gets simplified to the inserted element or is deleted if |
| 10062 | // this is constant idx extract element and its a constant idx insertelt. |
| 10063 | if (I->getOpcode() == Instruction::InsertElement && isConstant && |
| 10064 | isa<ConstantInt>(I->getOperand(2))) |
| 10065 | return true; |
| 10066 | if (I->getOpcode() == Instruction::Load && I->hasOneUse()) |
| 10067 | return true; |
| 10068 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) |
| 10069 | if (BO->hasOneUse() && |
| 10070 | (CheapToScalarize(BO->getOperand(0), isConstant) || |
| 10071 | CheapToScalarize(BO->getOperand(1), isConstant))) |
| 10072 | return true; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10073 | if (CmpInst *CI = dyn_cast<CmpInst>(I)) |
| 10074 | if (CI->hasOneUse() && |
| 10075 | (CheapToScalarize(CI->getOperand(0), isConstant) || |
| 10076 | CheapToScalarize(CI->getOperand(1), isConstant))) |
| 10077 | return true; |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10078 | |
| 10079 | return false; |
| 10080 | } |
| 10081 | |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 10082 | /// Read and decode a shufflevector mask. |
| 10083 | /// |
| 10084 | /// It turns undef elements into values that are larger than the number of |
| 10085 | /// elements in the input. |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10086 | static std::vector<unsigned> getShuffleMask(const ShuffleVectorInst *SVI) { |
| 10087 | unsigned NElts = SVI->getType()->getNumElements(); |
| 10088 | if (isa<ConstantAggregateZero>(SVI->getOperand(2))) |
| 10089 | return std::vector<unsigned>(NElts, 0); |
| 10090 | if (isa<UndefValue>(SVI->getOperand(2))) |
| 10091 | return std::vector<unsigned>(NElts, 2*NElts); |
| 10092 | |
| 10093 | std::vector<unsigned> Result; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10094 | const ConstantVector *CP = cast<ConstantVector>(SVI->getOperand(2)); |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10095 | for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) |
| 10096 | if (isa<UndefValue>(CP->getOperand(i))) |
| 10097 | Result.push_back(NElts*2); // undef -> 8 |
| 10098 | else |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10099 | Result.push_back(cast<ConstantInt>(CP->getOperand(i))->getZExtValue()); |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10100 | return Result; |
| 10101 | } |
| 10102 | |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10103 | /// FindScalarElement - Given a vector and an element number, see if the scalar |
| 10104 | /// value is already around as a register, for example if it were inserted then |
| 10105 | /// extracted from the vector. |
| 10106 | static Value *FindScalarElement(Value *V, unsigned EltNo) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10107 | assert(isa<VectorType>(V->getType()) && "Not looking at a vector?"); |
| 10108 | const VectorType *PTy = cast<VectorType>(V->getType()); |
Chris Lattner | 389a6f5 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 10109 | unsigned Width = PTy->getNumElements(); |
| 10110 | if (EltNo >= Width) // Out of range access. |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10111 | return UndefValue::get(PTy->getElementType()); |
| 10112 | |
| 10113 | if (isa<UndefValue>(V)) |
| 10114 | return UndefValue::get(PTy->getElementType()); |
| 10115 | else if (isa<ConstantAggregateZero>(V)) |
| 10116 | return Constant::getNullValue(PTy->getElementType()); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10117 | else if (ConstantVector *CP = dyn_cast<ConstantVector>(V)) |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10118 | return CP->getOperand(EltNo); |
| 10119 | else if (InsertElementInst *III = dyn_cast<InsertElementInst>(V)) { |
| 10120 | // 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] | 10121 | if (!isa<ConstantInt>(III->getOperand(2))) |
| 10122 | return 0; |
| 10123 | unsigned IIElt = cast<ConstantInt>(III->getOperand(2))->getZExtValue(); |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10124 | |
| 10125 | // If this is an insert to the element we are looking for, return the |
| 10126 | // inserted value. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10127 | if (EltNo == IIElt) |
| 10128 | return III->getOperand(1); |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10129 | |
| 10130 | // Otherwise, the insertelement doesn't modify the value, recurse on its |
| 10131 | // vector input. |
| 10132 | return FindScalarElement(III->getOperand(0), EltNo); |
Chris Lattner | 389a6f5 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 10133 | } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(V)) { |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10134 | unsigned InEl = getShuffleMask(SVI)[EltNo]; |
| 10135 | if (InEl < Width) |
| 10136 | return FindScalarElement(SVI->getOperand(0), InEl); |
| 10137 | else if (InEl < Width*2) |
| 10138 | return FindScalarElement(SVI->getOperand(1), InEl - Width); |
| 10139 | else |
| 10140 | return UndefValue::get(PTy->getElementType()); |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10141 | } |
| 10142 | |
| 10143 | // Otherwise, we don't know. |
| 10144 | return 0; |
| 10145 | } |
| 10146 | |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10147 | Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) { |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10148 | |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 10149 | // If vector val is undef, replace extract with scalar undef. |
Chris Lattner | 1f13c88 | 2006-03-31 18:25:14 +0000 | [diff] [blame] | 10150 | if (isa<UndefValue>(EI.getOperand(0))) |
| 10151 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); |
| 10152 | |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 10153 | // If vector val is constant 0, replace extract with scalar 0. |
Chris Lattner | 1f13c88 | 2006-03-31 18:25:14 +0000 | [diff] [blame] | 10154 | if (isa<ConstantAggregateZero>(EI.getOperand(0))) |
| 10155 | return ReplaceInstUsesWith(EI, Constant::getNullValue(EI.getType())); |
| 10156 | |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10157 | if (ConstantVector *C = dyn_cast<ConstantVector>(EI.getOperand(0))) { |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 10158 | // If vector val is constant with uniform operands, replace EI |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10159 | // with that operand |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10160 | Constant *op0 = C->getOperand(0); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10161 | for (unsigned i = 1; i < C->getNumOperands(); ++i) |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10162 | if (C->getOperand(i) != op0) { |
| 10163 | op0 = 0; |
| 10164 | break; |
| 10165 | } |
| 10166 | if (op0) |
| 10167 | return ReplaceInstUsesWith(EI, op0); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10168 | } |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10169 | |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10170 | // If extracting a specified index from the vector, see if we can recursively |
| 10171 | // find a previously computed scalar that was inserted into the vector. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10172 | if (ConstantInt *IdxC = dyn_cast<ConstantInt>(EI.getOperand(1))) { |
Chris Lattner | 8546409 | 2007-04-09 01:37:55 +0000 | [diff] [blame] | 10173 | unsigned IndexVal = IdxC->getZExtValue(); |
| 10174 | unsigned VectorWidth = |
| 10175 | cast<VectorType>(EI.getOperand(0)->getType())->getNumElements(); |
| 10176 | |
| 10177 | // If this is extracting an invalid index, turn this into undef, to avoid |
| 10178 | // crashing the code below. |
| 10179 | if (IndexVal >= VectorWidth) |
| 10180 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); |
| 10181 | |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 10182 | // This instruction only demands the single element from the input vector. |
| 10183 | // If the input vector has a single use, simplify it based on this use |
| 10184 | // property. |
Chris Lattner | 8546409 | 2007-04-09 01:37:55 +0000 | [diff] [blame] | 10185 | if (EI.getOperand(0)->hasOneUse() && VectorWidth != 1) { |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 10186 | uint64_t UndefElts; |
| 10187 | if (Value *V = SimplifyDemandedVectorElts(EI.getOperand(0), |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10188 | 1 << IndexVal, |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 10189 | UndefElts)) { |
| 10190 | EI.setOperand(0, V); |
| 10191 | return &EI; |
| 10192 | } |
| 10193 | } |
| 10194 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10195 | if (Value *Elt = FindScalarElement(EI.getOperand(0), IndexVal)) |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10196 | return ReplaceInstUsesWith(EI, Elt); |
Chris Lattner | b7300fa | 2007-04-14 23:02:14 +0000 | [diff] [blame] | 10197 | |
| 10198 | // If the this extractelement is directly using a bitcast from a vector of |
| 10199 | // the same number of elements, see if we can find the source element from |
| 10200 | // it. In this case, we will end up needing to bitcast the scalars. |
| 10201 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(EI.getOperand(0))) { |
| 10202 | if (const VectorType *VT = |
| 10203 | dyn_cast<VectorType>(BCI->getOperand(0)->getType())) |
| 10204 | if (VT->getNumElements() == VectorWidth) |
| 10205 | if (Value *Elt = FindScalarElement(BCI->getOperand(0), IndexVal)) |
| 10206 | return new BitCastInst(Elt, EI.getType()); |
| 10207 | } |
Chris Lattner | 389a6f5 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 10208 | } |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10209 | |
Chris Lattner | 73fa49d | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 10210 | if (Instruction *I = dyn_cast<Instruction>(EI.getOperand(0))) { |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10211 | if (I->hasOneUse()) { |
| 10212 | // Push extractelement into predecessor operation if legal and |
| 10213 | // profitable to do so |
| 10214 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) { |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10215 | bool isConstantElt = isa<ConstantInt>(EI.getOperand(1)); |
| 10216 | if (CheapToScalarize(BO, isConstantElt)) { |
| 10217 | ExtractElementInst *newEI0 = |
| 10218 | new ExtractElementInst(BO->getOperand(0), EI.getOperand(1), |
| 10219 | EI.getName()+".lhs"); |
| 10220 | ExtractElementInst *newEI1 = |
| 10221 | new ExtractElementInst(BO->getOperand(1), EI.getOperand(1), |
| 10222 | EI.getName()+".rhs"); |
| 10223 | InsertNewInstBefore(newEI0, EI); |
| 10224 | InsertNewInstBefore(newEI1, EI); |
| 10225 | return BinaryOperator::create(BO->getOpcode(), newEI0, newEI1); |
| 10226 | } |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 10227 | } else if (isa<LoadInst>(I)) { |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 10228 | unsigned AS = |
| 10229 | cast<PointerType>(I->getOperand(0)->getType())->getAddressSpace(); |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 10230 | Value *Ptr = InsertBitCastBefore(I->getOperand(0), |
| 10231 | PointerType::get(EI.getType(), AS),EI); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10232 | GetElementPtrInst *GEP = |
Reid Spencer | de33124 | 2006-11-29 01:11:01 +0000 | [diff] [blame] | 10233 | new GetElementPtrInst(Ptr, EI.getOperand(1), I->getName() + ".gep"); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10234 | InsertNewInstBefore(GEP, EI); |
| 10235 | return new LoadInst(GEP); |
Chris Lattner | 73fa49d | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 10236 | } |
| 10237 | } |
| 10238 | if (InsertElementInst *IE = dyn_cast<InsertElementInst>(I)) { |
| 10239 | // Extracting the inserted element? |
| 10240 | if (IE->getOperand(2) == EI.getOperand(1)) |
| 10241 | return ReplaceInstUsesWith(EI, IE->getOperand(1)); |
| 10242 | // If the inserted and extracted elements are constants, they must not |
| 10243 | // be the same value, extract from the pre-inserted value instead. |
| 10244 | if (isa<Constant>(IE->getOperand(2)) && |
| 10245 | isa<Constant>(EI.getOperand(1))) { |
| 10246 | AddUsesToWorkList(EI); |
| 10247 | EI.setOperand(0, IE->getOperand(0)); |
| 10248 | return &EI; |
| 10249 | } |
| 10250 | } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(I)) { |
| 10251 | // If this is extracting an element from a shufflevector, figure out where |
| 10252 | // it came from and extract from the appropriate input element instead. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10253 | if (ConstantInt *Elt = dyn_cast<ConstantInt>(EI.getOperand(1))) { |
| 10254 | unsigned SrcIdx = getShuffleMask(SVI)[Elt->getZExtValue()]; |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10255 | Value *Src; |
| 10256 | if (SrcIdx < SVI->getType()->getNumElements()) |
| 10257 | Src = SVI->getOperand(0); |
| 10258 | else if (SrcIdx < SVI->getType()->getNumElements()*2) { |
| 10259 | SrcIdx -= SVI->getType()->getNumElements(); |
| 10260 | Src = SVI->getOperand(1); |
| 10261 | } else { |
| 10262 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); |
Chris Lattner | df084ff | 2006-03-30 22:02:40 +0000 | [diff] [blame] | 10263 | } |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 10264 | return new ExtractElementInst(Src, SrcIdx); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10265 | } |
| 10266 | } |
Chris Lattner | 73fa49d | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 10267 | } |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10268 | return 0; |
| 10269 | } |
| 10270 | |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10271 | /// CollectSingleShuffleElements - If V is a shuffle of values that ONLY returns |
| 10272 | /// elements from either LHS or RHS, return the shuffle mask and true. |
| 10273 | /// Otherwise, return false. |
| 10274 | static bool CollectSingleShuffleElements(Value *V, Value *LHS, Value *RHS, |
| 10275 | std::vector<Constant*> &Mask) { |
| 10276 | assert(V->getType() == LHS->getType() && V->getType() == RHS->getType() && |
| 10277 | "Invalid CollectSingleShuffleElements"); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10278 | unsigned NumElts = cast<VectorType>(V->getType())->getNumElements(); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10279 | |
| 10280 | if (isa<UndefValue>(V)) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10281 | Mask.assign(NumElts, UndefValue::get(Type::Int32Ty)); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10282 | return true; |
| 10283 | } else if (V == LHS) { |
| 10284 | for (unsigned i = 0; i != NumElts; ++i) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10285 | Mask.push_back(ConstantInt::get(Type::Int32Ty, i)); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10286 | return true; |
| 10287 | } else if (V == RHS) { |
| 10288 | for (unsigned i = 0; i != NumElts; ++i) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10289 | Mask.push_back(ConstantInt::get(Type::Int32Ty, i+NumElts)); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10290 | return true; |
| 10291 | } else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) { |
| 10292 | // If this is an insert of an extract from some other vector, include it. |
| 10293 | Value *VecOp = IEI->getOperand(0); |
| 10294 | Value *ScalarOp = IEI->getOperand(1); |
| 10295 | Value *IdxOp = IEI->getOperand(2); |
| 10296 | |
Chris Lattner | d929f06 | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 10297 | if (!isa<ConstantInt>(IdxOp)) |
| 10298 | return false; |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10299 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Chris Lattner | d929f06 | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 10300 | |
| 10301 | if (isa<UndefValue>(ScalarOp)) { // inserting undef into vector. |
| 10302 | // Okay, we can handle this if the vector we are insertinting into is |
| 10303 | // transitively ok. |
| 10304 | if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) { |
| 10305 | // If so, update the mask to reflect the inserted undef. |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10306 | Mask[InsertedIdx] = UndefValue::get(Type::Int32Ty); |
Chris Lattner | d929f06 | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 10307 | return true; |
| 10308 | } |
| 10309 | } else if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)){ |
| 10310 | if (isa<ConstantInt>(EI->getOperand(1)) && |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10311 | EI->getOperand(0)->getType() == V->getType()) { |
| 10312 | unsigned ExtractedIdx = |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10313 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10314 | |
| 10315 | // This must be extracting from either LHS or RHS. |
| 10316 | if (EI->getOperand(0) == LHS || EI->getOperand(0) == RHS) { |
| 10317 | // Okay, we can handle this if the vector we are insertinting into is |
| 10318 | // transitively ok. |
| 10319 | if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) { |
| 10320 | // If so, update the mask to reflect the inserted value. |
| 10321 | if (EI->getOperand(0) == LHS) { |
| 10322 | Mask[InsertedIdx & (NumElts-1)] = |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10323 | ConstantInt::get(Type::Int32Ty, ExtractedIdx); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10324 | } else { |
| 10325 | assert(EI->getOperand(0) == RHS); |
| 10326 | Mask[InsertedIdx & (NumElts-1)] = |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10327 | ConstantInt::get(Type::Int32Ty, ExtractedIdx+NumElts); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10328 | |
| 10329 | } |
| 10330 | return true; |
| 10331 | } |
| 10332 | } |
| 10333 | } |
| 10334 | } |
| 10335 | } |
| 10336 | // TODO: Handle shufflevector here! |
| 10337 | |
| 10338 | return false; |
| 10339 | } |
| 10340 | |
| 10341 | /// CollectShuffleElements - We are building a shuffle of V, using RHS as the |
| 10342 | /// RHS of the shuffle instruction, if it is not null. Return a shuffle mask |
| 10343 | /// that computes V and the LHS value of the shuffle. |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10344 | static Value *CollectShuffleElements(Value *V, std::vector<Constant*> &Mask, |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10345 | Value *&RHS) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10346 | assert(isa<VectorType>(V->getType()) && |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10347 | (RHS == 0 || V->getType() == RHS->getType()) && |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10348 | "Invalid shuffle!"); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10349 | unsigned NumElts = cast<VectorType>(V->getType())->getNumElements(); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10350 | |
| 10351 | if (isa<UndefValue>(V)) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10352 | Mask.assign(NumElts, UndefValue::get(Type::Int32Ty)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10353 | return V; |
| 10354 | } else if (isa<ConstantAggregateZero>(V)) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10355 | Mask.assign(NumElts, ConstantInt::get(Type::Int32Ty, 0)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10356 | return V; |
| 10357 | } else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) { |
| 10358 | // If this is an insert of an extract from some other vector, include it. |
| 10359 | Value *VecOp = IEI->getOperand(0); |
| 10360 | Value *ScalarOp = IEI->getOperand(1); |
| 10361 | Value *IdxOp = IEI->getOperand(2); |
| 10362 | |
| 10363 | if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) { |
| 10364 | if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) && |
| 10365 | EI->getOperand(0)->getType() == V->getType()) { |
| 10366 | unsigned ExtractedIdx = |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10367 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
| 10368 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10369 | |
| 10370 | // Either the extracted from or inserted into vector must be RHSVec, |
| 10371 | // otherwise we'd end up with a shuffle of three inputs. |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10372 | if (EI->getOperand(0) == RHS || RHS == 0) { |
| 10373 | RHS = EI->getOperand(0); |
| 10374 | Value *V = CollectShuffleElements(VecOp, Mask, RHS); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10375 | Mask[InsertedIdx & (NumElts-1)] = |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10376 | ConstantInt::get(Type::Int32Ty, NumElts+ExtractedIdx); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10377 | return V; |
| 10378 | } |
| 10379 | |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10380 | if (VecOp == RHS) { |
| 10381 | Value *V = CollectShuffleElements(EI->getOperand(0), Mask, RHS); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10382 | // Everything but the extracted element is replaced with the RHS. |
| 10383 | for (unsigned i = 0; i != NumElts; ++i) { |
| 10384 | if (i != InsertedIdx) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10385 | Mask[i] = ConstantInt::get(Type::Int32Ty, NumElts+i); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10386 | } |
| 10387 | return V; |
| 10388 | } |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10389 | |
| 10390 | // If this insertelement is a chain that comes from exactly these two |
| 10391 | // vectors, return the vector and the effective shuffle. |
| 10392 | if (CollectSingleShuffleElements(IEI, EI->getOperand(0), RHS, Mask)) |
| 10393 | return EI->getOperand(0); |
| 10394 | |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10395 | } |
| 10396 | } |
| 10397 | } |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10398 | // TODO: Handle shufflevector here! |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10399 | |
| 10400 | // Otherwise, can't do anything fancy. Return an identity vector. |
| 10401 | for (unsigned i = 0; i != NumElts; ++i) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10402 | Mask.push_back(ConstantInt::get(Type::Int32Ty, i)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10403 | return V; |
| 10404 | } |
| 10405 | |
| 10406 | Instruction *InstCombiner::visitInsertElementInst(InsertElementInst &IE) { |
| 10407 | Value *VecOp = IE.getOperand(0); |
| 10408 | Value *ScalarOp = IE.getOperand(1); |
| 10409 | Value *IdxOp = IE.getOperand(2); |
| 10410 | |
Chris Lattner | 599ded1 | 2007-04-09 01:11:16 +0000 | [diff] [blame] | 10411 | // Inserting an undef or into an undefined place, remove this. |
| 10412 | if (isa<UndefValue>(ScalarOp) || isa<UndefValue>(IdxOp)) |
| 10413 | ReplaceInstUsesWith(IE, VecOp); |
| 10414 | |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10415 | // If the inserted element was extracted from some other vector, and if the |
| 10416 | // indexes are constant, try to turn this into a shufflevector operation. |
| 10417 | if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) { |
| 10418 | if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) && |
| 10419 | EI->getOperand(0)->getType() == IE.getType()) { |
| 10420 | unsigned NumVectorElts = IE.getType()->getNumElements(); |
Chris Lattner | e34e9a2 | 2007-04-14 23:32:02 +0000 | [diff] [blame] | 10421 | unsigned ExtractedIdx = |
| 10422 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10423 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10424 | |
| 10425 | if (ExtractedIdx >= NumVectorElts) // Out of range extract. |
| 10426 | return ReplaceInstUsesWith(IE, VecOp); |
| 10427 | |
| 10428 | if (InsertedIdx >= NumVectorElts) // Out of range insert. |
| 10429 | return ReplaceInstUsesWith(IE, UndefValue::get(IE.getType())); |
| 10430 | |
| 10431 | // If we are extracting a value from a vector, then inserting it right |
| 10432 | // back into the same place, just use the input vector. |
| 10433 | if (EI->getOperand(0) == VecOp && ExtractedIdx == InsertedIdx) |
| 10434 | return ReplaceInstUsesWith(IE, VecOp); |
| 10435 | |
| 10436 | // We could theoretically do this for ANY input. However, doing so could |
| 10437 | // turn chains of insertelement instructions into a chain of shufflevector |
| 10438 | // instructions, and right now we do not merge shufflevectors. As such, |
| 10439 | // only do this in a situation where it is clear that there is benefit. |
| 10440 | if (isa<UndefValue>(VecOp) || isa<ConstantAggregateZero>(VecOp)) { |
| 10441 | // Turn this into shuffle(EIOp0, VecOp, Mask). The result has all of |
| 10442 | // the values of VecOp, except then one read from EIOp0. |
| 10443 | // Build a new shuffle mask. |
| 10444 | std::vector<Constant*> Mask; |
| 10445 | if (isa<UndefValue>(VecOp)) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10446 | Mask.assign(NumVectorElts, UndefValue::get(Type::Int32Ty)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10447 | else { |
| 10448 | assert(isa<ConstantAggregateZero>(VecOp) && "Unknown thing"); |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10449 | Mask.assign(NumVectorElts, ConstantInt::get(Type::Int32Ty, |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10450 | NumVectorElts)); |
| 10451 | } |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10452 | Mask[InsertedIdx] = ConstantInt::get(Type::Int32Ty, ExtractedIdx); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10453 | return new ShuffleVectorInst(EI->getOperand(0), VecOp, |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10454 | ConstantVector::get(Mask)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10455 | } |
| 10456 | |
| 10457 | // If this insertelement isn't used by some other insertelement, turn it |
| 10458 | // (and any insertelements it points to), into one big shuffle. |
| 10459 | if (!IE.hasOneUse() || !isa<InsertElementInst>(IE.use_back())) { |
| 10460 | std::vector<Constant*> Mask; |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10461 | Value *RHS = 0; |
| 10462 | Value *LHS = CollectShuffleElements(&IE, Mask, RHS); |
| 10463 | if (RHS == 0) RHS = UndefValue::get(LHS->getType()); |
| 10464 | // We now have a shuffle of LHS, RHS, Mask. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10465 | return new ShuffleVectorInst(LHS, RHS, ConstantVector::get(Mask)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10466 | } |
| 10467 | } |
| 10468 | } |
| 10469 | |
| 10470 | return 0; |
| 10471 | } |
| 10472 | |
| 10473 | |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 10474 | Instruction *InstCombiner::visitShuffleVectorInst(ShuffleVectorInst &SVI) { |
| 10475 | Value *LHS = SVI.getOperand(0); |
| 10476 | Value *RHS = SVI.getOperand(1); |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10477 | std::vector<unsigned> Mask = getShuffleMask(&SVI); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 10478 | |
| 10479 | bool MadeChange = false; |
| 10480 | |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 10481 | // Undefined shuffle mask -> undefined value. |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10482 | if (isa<UndefValue>(SVI.getOperand(2))) |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 10483 | return ReplaceInstUsesWith(SVI, UndefValue::get(SVI.getType())); |
| 10484 | |
Chris Lattner | e4929dd | 2007-01-05 07:36:08 +0000 | [diff] [blame] | 10485 | // 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] | 10486 | // the undef, change them to undefs. |
Chris Lattner | e4929dd | 2007-01-05 07:36:08 +0000 | [diff] [blame] | 10487 | if (isa<UndefValue>(SVI.getOperand(1))) { |
| 10488 | // Scan to see if there are any references to the RHS. If so, replace them |
| 10489 | // with undef element refs and set MadeChange to true. |
| 10490 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
| 10491 | if (Mask[i] >= e && Mask[i] != 2*e) { |
| 10492 | Mask[i] = 2*e; |
| 10493 | MadeChange = true; |
| 10494 | } |
| 10495 | } |
| 10496 | |
| 10497 | if (MadeChange) { |
| 10498 | // Remap any references to RHS to use LHS. |
| 10499 | std::vector<Constant*> Elts; |
| 10500 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
| 10501 | if (Mask[i] == 2*e) |
| 10502 | Elts.push_back(UndefValue::get(Type::Int32Ty)); |
| 10503 | else |
| 10504 | Elts.push_back(ConstantInt::get(Type::Int32Ty, Mask[i])); |
| 10505 | } |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10506 | SVI.setOperand(2, ConstantVector::get(Elts)); |
Chris Lattner | e4929dd | 2007-01-05 07:36:08 +0000 | [diff] [blame] | 10507 | } |
| 10508 | } |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10509 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10510 | // Canonicalize shuffle(x ,x,mask) -> shuffle(x, undef,mask') |
| 10511 | // Canonicalize shuffle(undef,x,mask) -> shuffle(x, undef,mask'). |
| 10512 | if (LHS == RHS || isa<UndefValue>(LHS)) { |
| 10513 | if (isa<UndefValue>(LHS) && LHS == RHS) { |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 10514 | // shuffle(undef,undef,mask) -> undef. |
| 10515 | return ReplaceInstUsesWith(SVI, LHS); |
| 10516 | } |
| 10517 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10518 | // Remap any references to RHS to use LHS. |
| 10519 | std::vector<Constant*> Elts; |
| 10520 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 10521 | if (Mask[i] >= 2*e) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10522 | Elts.push_back(UndefValue::get(Type::Int32Ty)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 10523 | else { |
| 10524 | if ((Mask[i] >= e && isa<UndefValue>(RHS)) || |
| 10525 | (Mask[i] < e && isa<UndefValue>(LHS))) |
| 10526 | Mask[i] = 2*e; // Turn into undef. |
| 10527 | else |
| 10528 | Mask[i] &= (e-1); // Force to LHS. |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10529 | Elts.push_back(ConstantInt::get(Type::Int32Ty, Mask[i])); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 10530 | } |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 10531 | } |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10532 | SVI.setOperand(0, SVI.getOperand(1)); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 10533 | SVI.setOperand(1, UndefValue::get(RHS->getType())); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10534 | SVI.setOperand(2, ConstantVector::get(Elts)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 10535 | LHS = SVI.getOperand(0); |
| 10536 | RHS = SVI.getOperand(1); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 10537 | MadeChange = true; |
| 10538 | } |
| 10539 | |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 10540 | // Analyze the shuffle, are the LHS or RHS and identity shuffles? |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10541 | bool isLHSID = true, isRHSID = true; |
Chris Lattner | 706126d | 2006-04-16 00:03:56 +0000 | [diff] [blame] | 10542 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10543 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
| 10544 | if (Mask[i] >= e*2) continue; // Ignore undef values. |
| 10545 | // Is this an identity shuffle of the LHS value? |
| 10546 | isLHSID &= (Mask[i] == i); |
| 10547 | |
| 10548 | // Is this an identity shuffle of the RHS value? |
| 10549 | isRHSID &= (Mask[i]-e == i); |
Chris Lattner | 706126d | 2006-04-16 00:03:56 +0000 | [diff] [blame] | 10550 | } |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 10551 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10552 | // Eliminate identity shuffles. |
| 10553 | if (isLHSID) return ReplaceInstUsesWith(SVI, LHS); |
| 10554 | if (isRHSID) return ReplaceInstUsesWith(SVI, RHS); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 10555 | |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 10556 | // If the LHS is a shufflevector itself, see if we can combine it with this |
| 10557 | // one without producing an unusual shuffle. Here we are really conservative: |
| 10558 | // we are absolutely afraid of producing a shuffle mask not in the input |
| 10559 | // program, because the code gen may not be smart enough to turn a merged |
| 10560 | // shuffle into two specific shuffles: it may produce worse code. As such, |
| 10561 | // we only merge two shuffles if the result is one of the two input shuffle |
| 10562 | // masks. In this case, merging the shuffles just removes one instruction, |
| 10563 | // which we know is safe. This is good for things like turning: |
| 10564 | // (splat(splat)) -> splat. |
| 10565 | if (ShuffleVectorInst *LHSSVI = dyn_cast<ShuffleVectorInst>(LHS)) { |
| 10566 | if (isa<UndefValue>(RHS)) { |
| 10567 | std::vector<unsigned> LHSMask = getShuffleMask(LHSSVI); |
| 10568 | |
| 10569 | std::vector<unsigned> NewMask; |
| 10570 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) |
| 10571 | if (Mask[i] >= 2*e) |
| 10572 | NewMask.push_back(2*e); |
| 10573 | else |
| 10574 | NewMask.push_back(LHSMask[Mask[i]]); |
| 10575 | |
| 10576 | // If the result mask is equal to the src shuffle or this shuffle mask, do |
| 10577 | // the replacement. |
| 10578 | if (NewMask == LHSMask || NewMask == Mask) { |
| 10579 | std::vector<Constant*> Elts; |
| 10580 | for (unsigned i = 0, e = NewMask.size(); i != e; ++i) { |
| 10581 | if (NewMask[i] >= e*2) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10582 | Elts.push_back(UndefValue::get(Type::Int32Ty)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 10583 | } else { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10584 | Elts.push_back(ConstantInt::get(Type::Int32Ty, NewMask[i])); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 10585 | } |
| 10586 | } |
| 10587 | return new ShuffleVectorInst(LHSSVI->getOperand(0), |
| 10588 | LHSSVI->getOperand(1), |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10589 | ConstantVector::get(Elts)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 10590 | } |
| 10591 | } |
| 10592 | } |
Chris Lattner | c5eff44 | 2007-01-30 22:32:46 +0000 | [diff] [blame] | 10593 | |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 10594 | return MadeChange ? &SVI : 0; |
| 10595 | } |
| 10596 | |
| 10597 | |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10598 | |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 10599 | |
| 10600 | /// TryToSinkInstruction - Try to move the specified instruction from its |
| 10601 | /// current block into the beginning of DestBlock, which can only happen if it's |
| 10602 | /// safe to move the instruction past all of the instructions between it and the |
| 10603 | /// end of its block. |
| 10604 | static bool TryToSinkInstruction(Instruction *I, BasicBlock *DestBlock) { |
| 10605 | assert(I->hasOneUse() && "Invariants didn't hold!"); |
| 10606 | |
Chris Lattner | 108e902 | 2005-10-27 17:13:11 +0000 | [diff] [blame] | 10607 | // Cannot move control-flow-involving, volatile loads, vaarg, etc. |
| 10608 | if (isa<PHINode>(I) || I->mayWriteToMemory()) return false; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 10609 | |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 10610 | // Do not sink alloca instructions out of the entry block. |
Dan Gohman | ecb7a77 | 2007-03-22 16:38:57 +0000 | [diff] [blame] | 10611 | if (isa<AllocaInst>(I) && I->getParent() == |
| 10612 | &DestBlock->getParent()->getEntryBlock()) |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 10613 | return false; |
| 10614 | |
Chris Lattner | 96a52a6 | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 10615 | // We can only sink load instructions if there is nothing between the load and |
| 10616 | // the end of block that could change the value. |
| 10617 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Chris Lattner | 96a52a6 | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 10618 | for (BasicBlock::iterator Scan = LI, E = LI->getParent()->end(); |
| 10619 | Scan != E; ++Scan) |
| 10620 | if (Scan->mayWriteToMemory()) |
| 10621 | return false; |
Chris Lattner | 96a52a6 | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 10622 | } |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 10623 | |
| 10624 | BasicBlock::iterator InsertPos = DestBlock->begin(); |
| 10625 | while (isa<PHINode>(InsertPos)) ++InsertPos; |
| 10626 | |
Chris Lattner | 4bc5f80 | 2005-08-08 19:11:57 +0000 | [diff] [blame] | 10627 | I->moveBefore(InsertPos); |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 10628 | ++NumSunkInst; |
| 10629 | return true; |
| 10630 | } |
| 10631 | |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 10632 | |
| 10633 | /// AddReachableCodeToWorklist - Walk the function in depth-first order, adding |
| 10634 | /// all reachable code to the worklist. |
| 10635 | /// |
| 10636 | /// This has a couple of tricks to make the code faster and more powerful. In |
| 10637 | /// particular, we constant fold and DCE instructions as we go, to avoid adding |
| 10638 | /// them to the worklist (this significantly speeds up instcombine on code where |
| 10639 | /// many instructions are dead or constant). Additionally, if we find a branch |
| 10640 | /// whose condition is a known constant, we only visit the reachable successors. |
| 10641 | /// |
| 10642 | static void AddReachableCodeToWorklist(BasicBlock *BB, |
Chris Lattner | 1f87a58 | 2007-02-15 19:41:52 +0000 | [diff] [blame] | 10643 | SmallPtrSet<BasicBlock*, 64> &Visited, |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10644 | InstCombiner &IC, |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 10645 | const TargetData *TD) { |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 10646 | std::vector<BasicBlock*> Worklist; |
| 10647 | Worklist.push_back(BB); |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 10648 | |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 10649 | while (!Worklist.empty()) { |
| 10650 | BB = Worklist.back(); |
| 10651 | Worklist.pop_back(); |
| 10652 | |
| 10653 | // We have now visited this block! If we've already been here, ignore it. |
| 10654 | if (!Visited.insert(BB)) continue; |
| 10655 | |
| 10656 | for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ) { |
| 10657 | Instruction *Inst = BBI++; |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 10658 | |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 10659 | // DCE instruction if trivially dead. |
| 10660 | if (isInstructionTriviallyDead(Inst)) { |
| 10661 | ++NumDeadInst; |
| 10662 | DOUT << "IC: DCE: " << *Inst; |
| 10663 | Inst->eraseFromParent(); |
| 10664 | continue; |
| 10665 | } |
| 10666 | |
| 10667 | // ConstantProp instruction if trivially constant. |
| 10668 | if (Constant *C = ConstantFoldInstruction(Inst, TD)) { |
| 10669 | DOUT << "IC: ConstFold to: " << *C << " from: " << *Inst; |
| 10670 | Inst->replaceAllUsesWith(C); |
| 10671 | ++NumConstProp; |
| 10672 | Inst->eraseFromParent(); |
| 10673 | continue; |
| 10674 | } |
Chris Lattner | 3ccc6bc | 2007-07-20 22:06:41 +0000 | [diff] [blame] | 10675 | |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 10676 | IC.AddToWorkList(Inst); |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 10677 | } |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 10678 | |
| 10679 | // Recursively visit successors. If this is a branch or switch on a |
| 10680 | // constant, only visit the reachable successor. |
| 10681 | TerminatorInst *TI = BB->getTerminator(); |
| 10682 | if (BranchInst *BI = dyn_cast<BranchInst>(TI)) { |
| 10683 | if (BI->isConditional() && isa<ConstantInt>(BI->getCondition())) { |
| 10684 | bool CondVal = cast<ConstantInt>(BI->getCondition())->getZExtValue(); |
| 10685 | Worklist.push_back(BI->getSuccessor(!CondVal)); |
| 10686 | continue; |
| 10687 | } |
| 10688 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) { |
| 10689 | if (ConstantInt *Cond = dyn_cast<ConstantInt>(SI->getCondition())) { |
| 10690 | // See if this is an explicit destination. |
| 10691 | for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i) |
| 10692 | if (SI->getCaseValue(i) == Cond) { |
| 10693 | Worklist.push_back(SI->getSuccessor(i)); |
| 10694 | continue; |
| 10695 | } |
| 10696 | |
| 10697 | // Otherwise it is the default destination. |
| 10698 | Worklist.push_back(SI->getSuccessor(0)); |
| 10699 | continue; |
| 10700 | } |
| 10701 | } |
| 10702 | |
| 10703 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) |
| 10704 | Worklist.push_back(TI->getSuccessor(i)); |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 10705 | } |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 10706 | } |
| 10707 | |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 10708 | bool InstCombiner::DoOneIteration(Function &F, unsigned Iteration) { |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 10709 | bool Changed = false; |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 10710 | TD = &getAnalysis<TargetData>(); |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 10711 | |
| 10712 | DEBUG(DOUT << "\n\nINSTCOMBINE ITERATION #" << Iteration << " on " |
| 10713 | << F.getNameStr() << "\n"); |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 10714 | |
Chris Lattner | b3d5970 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 10715 | { |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 10716 | // Do a depth-first traversal of the function, populate the worklist with |
| 10717 | // the reachable instructions. Ignore blocks that are not reachable. Keep |
| 10718 | // track of which blocks we visit. |
Chris Lattner | 1f87a58 | 2007-02-15 19:41:52 +0000 | [diff] [blame] | 10719 | SmallPtrSet<BasicBlock*, 64> Visited; |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10720 | AddReachableCodeToWorklist(F.begin(), Visited, *this, TD); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 10721 | |
Chris Lattner | b3d5970 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 10722 | // Do a quick scan over the function. If we find any blocks that are |
| 10723 | // unreachable, remove any instructions inside of them. This prevents |
| 10724 | // the instcombine code from having to deal with some bad special cases. |
| 10725 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 10726 | if (!Visited.count(BB)) { |
| 10727 | Instruction *Term = BB->getTerminator(); |
| 10728 | while (Term != BB->begin()) { // Remove instrs bottom-up |
| 10729 | BasicBlock::iterator I = Term; --I; |
Chris Lattner | 6ffe551 | 2004-04-27 15:13:33 +0000 | [diff] [blame] | 10730 | |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 10731 | DOUT << "IC: DCE: " << *I; |
Chris Lattner | b3d5970 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 10732 | ++NumDeadInst; |
| 10733 | |
| 10734 | if (!I->use_empty()) |
| 10735 | I->replaceAllUsesWith(UndefValue::get(I->getType())); |
| 10736 | I->eraseFromParent(); |
| 10737 | } |
| 10738 | } |
| 10739 | } |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 10740 | |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10741 | while (!Worklist.empty()) { |
| 10742 | Instruction *I = RemoveOneFromWorkList(); |
| 10743 | if (I == 0) continue; // skip null values. |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 10744 | |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 10745 | // Check to see if we can DCE the instruction. |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 10746 | if (isInstructionTriviallyDead(I)) { |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 10747 | // Add operands to the worklist. |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 10748 | if (I->getNumOperands() < 4) |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 10749 | AddUsesToWorkList(*I); |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 10750 | ++NumDeadInst; |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 10751 | |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 10752 | DOUT << "IC: DCE: " << *I; |
Chris Lattner | ad5fec1 | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 10753 | |
| 10754 | I->eraseFromParent(); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10755 | RemoveFromWorkList(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 10756 | continue; |
| 10757 | } |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 10758 | |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 10759 | // Instruction isn't dead, see if we can constant propagate it. |
Chris Lattner | 0a19ffa | 2007-01-30 23:16:15 +0000 | [diff] [blame] | 10760 | if (Constant *C = ConstantFoldInstruction(I, TD)) { |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 10761 | DOUT << "IC: ConstFold to: " << *C << " from: " << *I; |
Chris Lattner | ad5fec1 | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 10762 | |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 10763 | // Add operands to the worklist. |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 10764 | AddUsesToWorkList(*I); |
Chris Lattner | c736d56 | 2002-12-05 22:41:53 +0000 | [diff] [blame] | 10765 | ReplaceInstUsesWith(*I, C); |
| 10766 | |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 10767 | ++NumConstProp; |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 10768 | I->eraseFromParent(); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10769 | RemoveFromWorkList(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 10770 | continue; |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 10771 | } |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 10772 | |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 10773 | // See if we can trivially sink this instruction to a successor basic block. |
| 10774 | if (I->hasOneUse()) { |
| 10775 | BasicBlock *BB = I->getParent(); |
| 10776 | BasicBlock *UserParent = cast<Instruction>(I->use_back())->getParent(); |
| 10777 | if (UserParent != BB) { |
| 10778 | bool UserIsSuccessor = false; |
| 10779 | // See if the user is one of our successors. |
| 10780 | for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI) |
| 10781 | if (*SI == UserParent) { |
| 10782 | UserIsSuccessor = true; |
| 10783 | break; |
| 10784 | } |
| 10785 | |
| 10786 | // If the user is one of our immediate successors, and if that successor |
| 10787 | // only has us as a predecessors (we'd have to split the critical edge |
| 10788 | // otherwise), we can keep going. |
| 10789 | if (UserIsSuccessor && !isa<PHINode>(I->use_back()) && |
| 10790 | next(pred_begin(UserParent)) == pred_end(UserParent)) |
| 10791 | // Okay, the CFG is simple enough, try to sink this instruction. |
| 10792 | Changed |= TryToSinkInstruction(I, UserParent); |
| 10793 | } |
| 10794 | } |
| 10795 | |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 10796 | // Now that we have an instruction, try combining it to simplify it... |
Reid Spencer | a9b8101 | 2007-03-26 17:44:01 +0000 | [diff] [blame] | 10797 | #ifndef NDEBUG |
| 10798 | std::string OrigI; |
| 10799 | #endif |
| 10800 | DEBUG(std::ostringstream SS; I->print(SS); OrigI = SS.str();); |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 10801 | if (Instruction *Result = visit(*I)) { |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 10802 | ++NumCombined; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 10803 | // Should we replace the old instruction with a new one? |
Chris Lattner | b3bc8fa | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 10804 | if (Result != I) { |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 10805 | DOUT << "IC: Old = " << *I |
| 10806 | << " New = " << *Result; |
Chris Lattner | 0cea42a | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 10807 | |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 10808 | // Everything uses the new instruction now. |
| 10809 | I->replaceAllUsesWith(Result); |
| 10810 | |
| 10811 | // Push the new instruction and any users onto the worklist. |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10812 | AddToWorkList(Result); |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 10813 | AddUsersToWorkList(*Result); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 10814 | |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 10815 | // Move the name to the new instruction first. |
| 10816 | Result->takeName(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 10817 | |
| 10818 | // Insert the new instruction into the basic block... |
| 10819 | BasicBlock *InstParent = I->getParent(); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 10820 | BasicBlock::iterator InsertPos = I; |
| 10821 | |
| 10822 | if (!isa<PHINode>(Result)) // If combining a PHI, don't insert |
| 10823 | while (isa<PHINode>(InsertPos)) // middle of a block of PHIs. |
| 10824 | ++InsertPos; |
| 10825 | |
| 10826 | InstParent->getInstList().insert(InsertPos, Result); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 10827 | |
Chris Lattner | 00d5131 | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 10828 | // Make sure that we reprocess all operands now that we reduced their |
| 10829 | // use counts. |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10830 | AddUsesToWorkList(*I); |
Chris Lattner | 216d4d8 | 2004-05-01 23:19:52 +0000 | [diff] [blame] | 10831 | |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 10832 | // Instructions can end up on the worklist more than once. Make sure |
| 10833 | // we do not process an instruction that has been deleted. |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10834 | RemoveFromWorkList(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 10835 | |
| 10836 | // Erase the old instruction. |
| 10837 | InstParent->getInstList().erase(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 10838 | } else { |
Evan Cheng | c7baf68 | 2007-03-27 16:44:48 +0000 | [diff] [blame] | 10839 | #ifndef NDEBUG |
Reid Spencer | a9b8101 | 2007-03-26 17:44:01 +0000 | [diff] [blame] | 10840 | DOUT << "IC: Mod = " << OrigI |
| 10841 | << " New = " << *I; |
Evan Cheng | c7baf68 | 2007-03-27 16:44:48 +0000 | [diff] [blame] | 10842 | #endif |
Chris Lattner | 0cea42a | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 10843 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 10844 | // If the instruction was modified, it's possible that it is now dead. |
| 10845 | // if so, remove it. |
Chris Lattner | 00d5131 | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 10846 | if (isInstructionTriviallyDead(I)) { |
| 10847 | // Make sure we process all operands now that we are reducing their |
| 10848 | // use counts. |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 10849 | AddUsesToWorkList(*I); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 10850 | |
Chris Lattner | 00d5131 | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 10851 | // Instructions may end up in the worklist more than once. Erase all |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10852 | // occurrences of this instruction. |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10853 | RemoveFromWorkList(I); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10854 | I->eraseFromParent(); |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 10855 | } else { |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 10856 | AddToWorkList(I); |
| 10857 | AddUsersToWorkList(*I); |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 10858 | } |
Chris Lattner | b3bc8fa | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 10859 | } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 10860 | Changed = true; |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 10861 | } |
| 10862 | } |
| 10863 | |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 10864 | assert(WorklistMap.empty() && "Worklist empty, but map not?"); |
Chris Lattner | a9ff5eb | 2007-08-05 08:47:58 +0000 | [diff] [blame] | 10865 | |
| 10866 | // Do an explicit clear, this shrinks the map if needed. |
| 10867 | WorklistMap.clear(); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 10868 | return Changed; |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 10869 | } |
| 10870 | |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 10871 | |
| 10872 | bool InstCombiner::runOnFunction(Function &F) { |
Chris Lattner | f964f32 | 2007-03-04 04:27:24 +0000 | [diff] [blame] | 10873 | MustPreserveLCSSA = mustPreserveAnalysisID(LCSSAID); |
| 10874 | |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 10875 | bool EverMadeChange = false; |
| 10876 | |
| 10877 | // Iterate while there is work to do. |
| 10878 | unsigned Iteration = 0; |
| 10879 | while (DoOneIteration(F, Iteration++)) |
| 10880 | EverMadeChange = true; |
| 10881 | return EverMadeChange; |
| 10882 | } |
| 10883 | |
Brian Gaeke | 96d4bf7 | 2004-07-27 17:43:21 +0000 | [diff] [blame] | 10884 | FunctionPass *llvm::createInstructionCombiningPass() { |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 10885 | return new InstCombiner(); |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 10886 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 10887 | |