Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1 | //===- InstructionCombining.cpp - Combine multiple instructions -----------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9 | // |
| 10 | // InstructionCombining - Combine instructions to form fewer, simple |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 11 | // instructions. This pass does not modify the CFG This pass is where algebraic |
| 12 | // simplification happens. |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 13 | // |
| 14 | // This pass combines things like: |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 15 | // %Y = add i32 %X, 1 |
| 16 | // %Z = add i32 %Y, 1 |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 17 | // into: |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 18 | // %Z = add i32 %X, 2 |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 19 | // |
| 20 | // This is a simple worklist driven algorithm. |
| 21 | // |
Chris Lattner | 065a616 | 2003-09-10 05:29:43 +0000 | [diff] [blame] | 22 | // This pass guarantees that the following canonicalizations are performed on |
Chris Lattner | 2cd9196 | 2003-07-23 21:41:57 +0000 | [diff] [blame] | 23 | // the program: |
| 24 | // 1. If a binary operator has a constant operand, it is moved to the RHS |
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 25 | // 2. Bitwise operators with constant operands are always grouped so that |
| 26 | // shifts are performed first, then or's, then and's, then xor's. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 27 | // 3. Compare instructions are converted from <,>,<=,>= to ==,!= if possible |
| 28 | // 4. All cmp instructions on boolean values are replaced with logical ops |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 29 | // 5. add X, X is represented as (X*2) => (X << 1) |
| 30 | // 6. Multiplies with a power-of-two constant argument are transformed into |
| 31 | // shifts. |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 32 | // ... etc. |
Chris Lattner | 2cd9196 | 2003-07-23 21:41:57 +0000 | [diff] [blame] | 33 | // |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 34 | //===----------------------------------------------------------------------===// |
| 35 | |
Chris Lattner | 0cea42a | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 36 | #define DEBUG_TYPE "instcombine" |
Chris Lattner | 022103b | 2002-05-07 20:03:00 +0000 | [diff] [blame] | 37 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 38 | #include "llvm/IntrinsicInst.h" |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 39 | #include "llvm/Pass.h" |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 40 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 41 | #include "llvm/GlobalVariable.h" |
Chris Lattner | 79066fa | 2007-01-30 23:46:24 +0000 | [diff] [blame] | 42 | #include "llvm/Analysis/ConstantFolding.h" |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 43 | #include "llvm/Target/TargetData.h" |
| 44 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 45 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 46 | #include "llvm/Support/CallSite.h" |
Nick Lewycky | 5be2920 | 2008-02-03 16:33:09 +0000 | [diff] [blame] | 47 | #include "llvm/Support/ConstantRange.h" |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 48 | #include "llvm/Support/Debug.h" |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 49 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 50 | #include "llvm/Support/InstVisitor.h" |
Chris Lattner | bcd7db5 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 51 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 52 | #include "llvm/Support/PatternMatch.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 53 | #include "llvm/Support/Compiler.h" |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 54 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 55 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 1f87a58 | 2007-02-15 19:41:52 +0000 | [diff] [blame] | 56 | #include "llvm/ADT/SmallPtrSet.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 57 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 58 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | b3bc8fa | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 59 | #include <algorithm> |
Reid Spencer | a9b8101 | 2007-03-26 17:44:01 +0000 | [diff] [blame] | 60 | #include <sstream> |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 61 | using namespace llvm; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 62 | using namespace llvm::PatternMatch; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 63 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 64 | STATISTIC(NumCombined , "Number of insts combined"); |
| 65 | STATISTIC(NumConstProp, "Number of constant folds"); |
| 66 | STATISTIC(NumDeadInst , "Number of dead inst eliminated"); |
| 67 | STATISTIC(NumDeadStore, "Number of dead stores eliminated"); |
| 68 | STATISTIC(NumSunkInst , "Number of instructions sunk"); |
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 69 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 70 | namespace { |
Chris Lattner | f4b5461 | 2006-06-28 22:08:15 +0000 | [diff] [blame] | 71 | class VISIBILITY_HIDDEN InstCombiner |
| 72 | : public FunctionPass, |
| 73 | public InstVisitor<InstCombiner, Instruction*> { |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 74 | // Worklist of all of the instructions that need to be simplified. |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 75 | std::vector<Instruction*> Worklist; |
| 76 | DenseMap<Instruction*, unsigned> WorklistMap; |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 77 | TargetData *TD; |
Chris Lattner | f964f32 | 2007-03-04 04:27:24 +0000 | [diff] [blame] | 78 | bool MustPreserveLCSSA; |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 79 | public: |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 80 | static char ID; // Pass identification, replacement for typeid |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 81 | InstCombiner() : FunctionPass((intptr_t)&ID) {} |
| 82 | |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 83 | /// AddToWorkList - Add the specified instruction to the worklist if it |
| 84 | /// isn't already in it. |
| 85 | void AddToWorkList(Instruction *I) { |
| 86 | if (WorklistMap.insert(std::make_pair(I, Worklist.size()))) |
| 87 | Worklist.push_back(I); |
| 88 | } |
| 89 | |
| 90 | // RemoveFromWorkList - remove I from the worklist if it exists. |
| 91 | void RemoveFromWorkList(Instruction *I) { |
| 92 | DenseMap<Instruction*, unsigned>::iterator It = WorklistMap.find(I); |
| 93 | if (It == WorklistMap.end()) return; // Not in worklist. |
| 94 | |
| 95 | // Don't bother moving everything down, just null out the slot. |
| 96 | Worklist[It->second] = 0; |
| 97 | |
| 98 | WorklistMap.erase(It); |
| 99 | } |
| 100 | |
| 101 | Instruction *RemoveOneFromWorkList() { |
| 102 | Instruction *I = Worklist.back(); |
| 103 | Worklist.pop_back(); |
| 104 | WorklistMap.erase(I); |
| 105 | return I; |
| 106 | } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 107 | |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 108 | |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 109 | /// AddUsersToWorkList - When an instruction is simplified, add all users of |
| 110 | /// the instruction to the work lists because they might get more simplified |
| 111 | /// now. |
| 112 | /// |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 113 | void AddUsersToWorkList(Value &I) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 114 | for (Value::use_iterator UI = I.use_begin(), UE = I.use_end(); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 115 | UI != UE; ++UI) |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 116 | AddToWorkList(cast<Instruction>(*UI)); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 119 | /// AddUsesToWorkList - When an instruction is simplified, add operands to |
| 120 | /// the work lists because they might get more simplified now. |
| 121 | /// |
| 122 | void AddUsesToWorkList(Instruction &I) { |
| 123 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) |
| 124 | if (Instruction *Op = dyn_cast<Instruction>(I.getOperand(i))) |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 125 | AddToWorkList(Op); |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 126 | } |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 127 | |
| 128 | /// AddSoonDeadInstToWorklist - The specified instruction is about to become |
| 129 | /// dead. Add all of its operands to the worklist, turning them into |
| 130 | /// undef's to reduce the number of uses of those instructions. |
| 131 | /// |
| 132 | /// Return the specified operand before it is turned into an undef. |
| 133 | /// |
| 134 | Value *AddSoonDeadInstToWorklist(Instruction &I, unsigned op) { |
| 135 | Value *R = I.getOperand(op); |
| 136 | |
| 137 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) |
| 138 | if (Instruction *Op = dyn_cast<Instruction>(I.getOperand(i))) { |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 139 | AddToWorkList(Op); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 140 | // Set the operand to undef to drop the use. |
| 141 | I.setOperand(i, UndefValue::get(Op->getType())); |
| 142 | } |
| 143 | |
| 144 | return R; |
| 145 | } |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 146 | |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 147 | public: |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 148 | virtual bool runOnFunction(Function &F); |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 149 | |
| 150 | bool DoOneIteration(Function &F, unsigned ItNum); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 151 | |
Chris Lattner | 97e52e4 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 152 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 153 | AU.addRequired<TargetData>(); |
Owen Anderson | d1b78a1 | 2006-07-10 19:03:49 +0000 | [diff] [blame] | 154 | AU.addPreservedID(LCSSAID); |
Chris Lattner | cb2610e | 2002-10-21 20:00:28 +0000 | [diff] [blame] | 155 | AU.setPreservesCFG(); |
Chris Lattner | 97e52e4 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 158 | TargetData &getTargetData() const { return *TD; } |
| 159 | |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 160 | // Visitation implementation - Implement instruction combining for different |
| 161 | // instruction types. The semantics are as follows: |
| 162 | // Return Value: |
| 163 | // null - No change was made |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 164 | // 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] | 165 | // otherwise - Change was made, replace I with returned instruction |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 166 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 167 | Instruction *visitAdd(BinaryOperator &I); |
| 168 | Instruction *visitSub(BinaryOperator &I); |
| 169 | Instruction *visitMul(BinaryOperator &I); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 170 | Instruction *visitURem(BinaryOperator &I); |
| 171 | Instruction *visitSRem(BinaryOperator &I); |
| 172 | Instruction *visitFRem(BinaryOperator &I); |
| 173 | Instruction *commonRemTransforms(BinaryOperator &I); |
| 174 | Instruction *commonIRemTransforms(BinaryOperator &I); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 175 | Instruction *commonDivTransforms(BinaryOperator &I); |
| 176 | Instruction *commonIDivTransforms(BinaryOperator &I); |
| 177 | Instruction *visitUDiv(BinaryOperator &I); |
| 178 | Instruction *visitSDiv(BinaryOperator &I); |
| 179 | Instruction *visitFDiv(BinaryOperator &I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 180 | Instruction *visitAnd(BinaryOperator &I); |
| 181 | Instruction *visitOr (BinaryOperator &I); |
| 182 | Instruction *visitXor(BinaryOperator &I); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 183 | Instruction *visitShl(BinaryOperator &I); |
| 184 | Instruction *visitAShr(BinaryOperator &I); |
| 185 | Instruction *visitLShr(BinaryOperator &I); |
| 186 | Instruction *commonShiftTransforms(BinaryOperator &I); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 187 | Instruction *visitFCmpInst(FCmpInst &I); |
| 188 | Instruction *visitICmpInst(ICmpInst &I); |
| 189 | Instruction *visitICmpInstWithCastAndCast(ICmpInst &ICI); |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 190 | Instruction *visitICmpInstWithInstAndIntCst(ICmpInst &ICI, |
| 191 | Instruction *LHS, |
| 192 | ConstantInt *RHS); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 193 | Instruction *FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI, |
| 194 | ConstantInt *DivRHS); |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 195 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 196 | Instruction *FoldGEPICmp(User *GEPLHS, Value *RHS, |
| 197 | ICmpInst::Predicate Cond, Instruction &I); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 198 | Instruction *FoldShiftByConstant(Value *Op0, ConstantInt *Op1, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 199 | BinaryOperator &I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 200 | Instruction *commonCastTransforms(CastInst &CI); |
| 201 | Instruction *commonIntCastTransforms(CastInst &CI); |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 202 | Instruction *commonPointerCastTransforms(CastInst &CI); |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 203 | Instruction *visitTrunc(TruncInst &CI); |
| 204 | Instruction *visitZExt(ZExtInst &CI); |
| 205 | Instruction *visitSExt(SExtInst &CI); |
Chris Lattner | b753065 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 206 | Instruction *visitFPTrunc(FPTruncInst &CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 207 | Instruction *visitFPExt(CastInst &CI); |
| 208 | Instruction *visitFPToUI(CastInst &CI); |
| 209 | Instruction *visitFPToSI(CastInst &CI); |
| 210 | Instruction *visitUIToFP(CastInst &CI); |
| 211 | Instruction *visitSIToFP(CastInst &CI); |
| 212 | Instruction *visitPtrToInt(CastInst &CI); |
Chris Lattner | f9d9e45 | 2008-01-08 07:23:51 +0000 | [diff] [blame] | 213 | Instruction *visitIntToPtr(IntToPtrInst &CI); |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 214 | Instruction *visitBitCast(BitCastInst &CI); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 215 | Instruction *FoldSelectOpOp(SelectInst &SI, Instruction *TI, |
| 216 | Instruction *FI); |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 217 | Instruction *visitSelectInst(SelectInst &CI); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 218 | Instruction *visitCallInst(CallInst &CI); |
| 219 | Instruction *visitInvokeInst(InvokeInst &II); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 220 | Instruction *visitPHINode(PHINode &PN); |
| 221 | Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP); |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 222 | Instruction *visitAllocationInst(AllocationInst &AI); |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 223 | Instruction *visitFreeInst(FreeInst &FI); |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 224 | Instruction *visitLoadInst(LoadInst &LI); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 225 | Instruction *visitStoreInst(StoreInst &SI); |
Chris Lattner | c4d10eb | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 226 | Instruction *visitBranchInst(BranchInst &BI); |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 227 | Instruction *visitSwitchInst(SwitchInst &SI); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 228 | Instruction *visitInsertElementInst(InsertElementInst &IE); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 229 | Instruction *visitExtractElementInst(ExtractElementInst &EI); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 230 | Instruction *visitShuffleVectorInst(ShuffleVectorInst &SVI); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 231 | |
| 232 | // visitInstruction - Specify what to return for unhandled instructions... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 233 | Instruction *visitInstruction(Instruction &I) { return 0; } |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 234 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 235 | private: |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 236 | Instruction *visitCallSite(CallSite CS); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 237 | bool transformConstExprCastCall(CallSite CS); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 238 | Instruction *transformCallThroughTrampoline(CallSite CS); |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 239 | Instruction *transformZExtICmp(ICmpInst *ICI, Instruction &CI, |
| 240 | bool DoXform = true); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 241 | |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 242 | public: |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 243 | // InsertNewInstBefore - insert an instruction New before instruction Old |
| 244 | // in the program. Add the new instruction to the worklist. |
| 245 | // |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 246 | Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) { |
Chris Lattner | e6f9a91 | 2002-08-23 18:32:43 +0000 | [diff] [blame] | 247 | assert(New && New->getParent() == 0 && |
| 248 | "New instruction already inserted into a basic block!"); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 249 | BasicBlock *BB = Old.getParent(); |
| 250 | BB->getInstList().insert(&Old, New); // Insert inst |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 251 | AddToWorkList(New); |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 252 | return New; |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 255 | /// InsertCastBefore - Insert a cast of V to TY before the instruction POS. |
| 256 | /// This also adds the cast to the worklist. Finally, this returns the |
| 257 | /// cast. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 258 | Value *InsertCastBefore(Instruction::CastOps opc, Value *V, const Type *Ty, |
| 259 | Instruction &Pos) { |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 260 | if (V->getType() == Ty) return V; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 261 | |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 262 | if (Constant *CV = dyn_cast<Constant>(V)) |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 263 | return ConstantExpr::getCast(opc, CV, Ty); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 264 | |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 265 | Instruction *C = CastInst::create(opc, V, Ty, V->getName(), &Pos); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 266 | AddToWorkList(C); |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 267 | return C; |
| 268 | } |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 269 | |
| 270 | Value *InsertBitCastBefore(Value *V, const Type *Ty, Instruction &Pos) { |
| 271 | return InsertCastBefore(Instruction::BitCast, V, Ty, Pos); |
| 272 | } |
| 273 | |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 274 | |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 275 | // ReplaceInstUsesWith - This method is to be used when an instruction is |
| 276 | // found to be dead, replacable with another preexisting expression. Here |
| 277 | // we add all uses of I to the worklist, replace all uses of I with the new |
| 278 | // value, then return I, so that the inst combiner will know that I was |
| 279 | // modified. |
| 280 | // |
| 281 | Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) { |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 282 | AddUsersToWorkList(I); // Add all modified instrs to worklist |
Chris Lattner | 15a76c0 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 283 | if (&I != V) { |
| 284 | I.replaceAllUsesWith(V); |
| 285 | return &I; |
| 286 | } else { |
| 287 | // If we are replacing the instruction with itself, this must be in a |
| 288 | // segment of unreachable code, so just clobber the instruction. |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 289 | I.replaceAllUsesWith(UndefValue::get(I.getType())); |
Chris Lattner | 15a76c0 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 290 | return &I; |
| 291 | } |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 292 | } |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 293 | |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 294 | // UpdateValueUsesWith - This method is to be used when an value is |
| 295 | // found to be replacable with another preexisting expression or was |
| 296 | // updated. Here we add all uses of I to the worklist, replace all uses of |
| 297 | // I with the new value (unless the instruction was just updated), then |
| 298 | // return true, so that the inst combiner will know that I was modified. |
| 299 | // |
| 300 | bool UpdateValueUsesWith(Value *Old, Value *New) { |
| 301 | AddUsersToWorkList(*Old); // Add all modified instrs to worklist |
| 302 | if (Old != New) |
| 303 | Old->replaceAllUsesWith(New); |
| 304 | if (Instruction *I = dyn_cast<Instruction>(Old)) |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 305 | AddToWorkList(I); |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 306 | if (Instruction *I = dyn_cast<Instruction>(New)) |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 307 | AddToWorkList(I); |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 308 | return true; |
| 309 | } |
| 310 | |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 311 | // EraseInstFromFunction - When dealing with an instruction that has side |
| 312 | // effects or produces a void value, we can't rely on DCE to delete the |
| 313 | // instruction. Instead, visit methods should return the value returned by |
| 314 | // this function. |
| 315 | Instruction *EraseInstFromFunction(Instruction &I) { |
| 316 | assert(I.use_empty() && "Cannot erase instruction that is used!"); |
| 317 | AddUsesToWorkList(I); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 318 | RemoveFromWorkList(&I); |
Chris Lattner | 954f66a | 2004-11-18 21:41:39 +0000 | [diff] [blame] | 319 | I.eraseFromParent(); |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 320 | return 0; // Don't do anything with FI |
| 321 | } |
| 322 | |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 323 | private: |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 324 | /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the |
| 325 | /// InsertBefore instruction. This is specialized a bit to avoid inserting |
| 326 | /// casts that are known to not do anything... |
| 327 | /// |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 328 | Value *InsertOperandCastBefore(Instruction::CastOps opcode, |
| 329 | Value *V, const Type *DestTy, |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 330 | Instruction *InsertBefore); |
| 331 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 332 | /// SimplifyCommutative - This performs a few simplifications for |
| 333 | /// commutative operators. |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 334 | bool SimplifyCommutative(BinaryOperator &I); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 335 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 336 | /// SimplifyCompare - This reorders the operands of a CmpInst to get them in |
| 337 | /// most-complex to least-complex order. |
| 338 | bool SimplifyCompare(CmpInst &I); |
| 339 | |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 340 | /// SimplifyDemandedBits - Attempts to replace V with a simpler value based |
| 341 | /// on the demanded bits. |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 342 | bool SimplifyDemandedBits(Value *V, APInt DemandedMask, |
| 343 | APInt& KnownZero, APInt& KnownOne, |
| 344 | unsigned Depth = 0); |
| 345 | |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 346 | Value *SimplifyDemandedVectorElts(Value *V, uint64_t DemandedElts, |
| 347 | uint64_t &UndefElts, unsigned Depth = 0); |
| 348 | |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 349 | // FoldOpIntoPhi - Given a binary operator or cast instruction which has a |
| 350 | // PHI node as operand #0, see if we can fold the instruction into the PHI |
| 351 | // (which is only possible if all operands to the PHI are constants). |
| 352 | Instruction *FoldOpIntoPhi(Instruction &I); |
| 353 | |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 354 | // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary" |
| 355 | // operator and they all are only used by the PHI, PHI together their |
| 356 | // inputs, and do the operation once, to the result of the PHI. |
| 357 | Instruction *FoldPHIArgOpIntoPHI(PHINode &PN); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 358 | Instruction *FoldPHIArgBinOpIntoPHI(PHINode &PN); |
| 359 | |
| 360 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 361 | Instruction *OptAndOp(Instruction *Op, ConstantInt *OpRHS, |
| 362 | ConstantInt *AndRHS, BinaryOperator &TheAnd); |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 363 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 364 | Value *FoldLogicalPlusAnd(Value *LHS, Value *RHS, ConstantInt *Mask, |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 365 | bool isSub, Instruction &I); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 366 | Instruction *InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 367 | bool isSigned, bool Inside, Instruction &IB); |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 368 | Instruction *PromoteCastOfAllocation(BitCastInst &CI, AllocationInst &AI); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 369 | Instruction *MatchBSwap(BinaryOperator &I); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 370 | bool SimplifyStoreAtEndOfBlock(StoreInst &SI); |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 371 | Instruction *SimplifyMemTransfer(MemIntrinsic *MI); |
| 372 | |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 373 | |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 374 | Value *EvaluateInDifferentType(Value *V, const Type *Ty, bool isSigned); |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 375 | |
| 376 | void ComputeMaskedBits(Value *V, const APInt &Mask, APInt& KnownZero, |
| 377 | APInt& KnownOne, unsigned Depth = 0); |
| 378 | bool MaskedValueIsZero(Value *V, const APInt& Mask, unsigned Depth = 0); |
| 379 | bool CanEvaluateInDifferentType(Value *V, const IntegerType *Ty, |
| 380 | unsigned CastOpc, |
| 381 | int &NumCastsRemoved); |
| 382 | unsigned GetOrEnforceKnownAlignment(Value *V, |
| 383 | unsigned PrefAlign = 0); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 384 | }; |
Chris Lattner | f629309 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 385 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 386 | char InstCombiner::ID = 0; |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 387 | RegisterPass<InstCombiner> X("instcombine", "Combine redundant instructions"); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 390 | // getComplexity: Assign a complexity or rank value to LLVM Values... |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 391 | // 0 -> undef, 1 -> Const, 2 -> Other, 3 -> Arg, 3 -> Unary, 4 -> OtherInst |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 392 | static unsigned getComplexity(Value *V) { |
| 393 | if (isa<Instruction>(V)) { |
| 394 | if (BinaryOperator::isNeg(V) || BinaryOperator::isNot(V)) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 395 | return 3; |
| 396 | return 4; |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 397 | } |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 398 | if (isa<Argument>(V)) return 3; |
| 399 | return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2; |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 400 | } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 401 | |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 402 | // isOnlyUse - Return true if this instruction will be deleted if we stop using |
| 403 | // it. |
| 404 | static bool isOnlyUse(Value *V) { |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 405 | return V->hasOneUse() || isa<Constant>(V); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 408 | // getPromotedType - Return the specified type promoted as it would be to pass |
| 409 | // though a va_arg area... |
| 410 | static const Type *getPromotedType(const Type *Ty) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 411 | if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty)) { |
| 412 | if (ITy->getBitWidth() < 32) |
| 413 | return Type::Int32Ty; |
Chris Lattner | 2b7e0ad | 2007-05-23 01:17:04 +0000 | [diff] [blame] | 414 | } |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 415 | return Ty; |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 418 | /// getBitCastOperand - If the specified operand is a CastInst or a constant |
| 419 | /// expression bitcast, return the operand value, otherwise return null. |
| 420 | static Value *getBitCastOperand(Value *V) { |
| 421 | if (BitCastInst *I = dyn_cast<BitCastInst>(V)) |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 422 | return I->getOperand(0); |
| 423 | else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 424 | if (CE->getOpcode() == Instruction::BitCast) |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 425 | return CE->getOperand(0); |
| 426 | return 0; |
| 427 | } |
| 428 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 429 | /// This function is a wrapper around CastInst::isEliminableCastPair. It |
| 430 | /// simply extracts arguments and returns what that function returns. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 431 | static Instruction::CastOps |
| 432 | isEliminableCastPair( |
| 433 | const CastInst *CI, ///< The first cast instruction |
| 434 | unsigned opcode, ///< The opcode of the second cast instruction |
| 435 | const Type *DstTy, ///< The target type for the second cast instruction |
| 436 | TargetData *TD ///< The target data for pointer size |
| 437 | ) { |
| 438 | |
| 439 | const Type *SrcTy = CI->getOperand(0)->getType(); // A from above |
| 440 | const Type *MidTy = CI->getType(); // B from above |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 441 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 442 | // Get the opcodes of the two Cast instructions |
| 443 | Instruction::CastOps firstOp = Instruction::CastOps(CI->getOpcode()); |
| 444 | Instruction::CastOps secondOp = Instruction::CastOps(opcode); |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 445 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 446 | return Instruction::CastOps( |
| 447 | CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, |
| 448 | DstTy, TD->getIntPtrType())); |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | /// ValueRequiresCast - Return true if the cast from "V to Ty" actually results |
| 452 | /// in any code being generated. It does not require codegen if V is simple |
| 453 | /// enough or if the cast can be folded into other casts. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 454 | static bool ValueRequiresCast(Instruction::CastOps opcode, const Value *V, |
| 455 | const Type *Ty, TargetData *TD) { |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 456 | if (V->getType() == Ty || isa<Constant>(V)) return false; |
| 457 | |
Chris Lattner | 01575b7 | 2006-05-25 23:24:33 +0000 | [diff] [blame] | 458 | // 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] | 459 | if (const CastInst *CI = dyn_cast<CastInst>(V)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 460 | if (isEliminableCastPair(CI, opcode, Ty, TD)) |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 461 | return false; |
| 462 | return true; |
| 463 | } |
| 464 | |
| 465 | /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the |
| 466 | /// InsertBefore instruction. This is specialized a bit to avoid inserting |
| 467 | /// casts that are known to not do anything... |
| 468 | /// |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 469 | Value *InstCombiner::InsertOperandCastBefore(Instruction::CastOps opcode, |
| 470 | Value *V, const Type *DestTy, |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 471 | Instruction *InsertBefore) { |
| 472 | if (V->getType() == DestTy) return V; |
| 473 | if (Constant *C = dyn_cast<Constant>(V)) |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 474 | return ConstantExpr::getCast(opcode, C, DestTy); |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 475 | |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 476 | return InsertCastBefore(opcode, V, DestTy, *InsertBefore); |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 479 | // SimplifyCommutative - This performs a few simplifications for commutative |
| 480 | // operators: |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 481 | // |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 482 | // 1. Order operands such that they are listed from right (least complex) to |
| 483 | // left (most complex). This puts constants before unary operators before |
| 484 | // binary operators. |
| 485 | // |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 486 | // 2. Transform: (op (op V, C1), C2) ==> (op V, (op C1, C2)) |
| 487 | // 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] | 488 | // |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 489 | bool InstCombiner::SimplifyCommutative(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 490 | bool Changed = false; |
| 491 | if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1))) |
| 492 | Changed = !I.swapOperands(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 493 | |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 494 | if (!I.isAssociative()) return Changed; |
| 495 | Instruction::BinaryOps Opcode = I.getOpcode(); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 496 | if (BinaryOperator *Op = dyn_cast<BinaryOperator>(I.getOperand(0))) |
| 497 | if (Op->getOpcode() == Opcode && isa<Constant>(Op->getOperand(1))) { |
| 498 | if (isa<Constant>(I.getOperand(1))) { |
Chris Lattner | 2a9c847 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 499 | Constant *Folded = ConstantExpr::get(I.getOpcode(), |
| 500 | cast<Constant>(I.getOperand(1)), |
| 501 | cast<Constant>(Op->getOperand(1))); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 502 | I.setOperand(0, Op->getOperand(0)); |
| 503 | I.setOperand(1, Folded); |
| 504 | return true; |
| 505 | } else if (BinaryOperator *Op1=dyn_cast<BinaryOperator>(I.getOperand(1))) |
| 506 | if (Op1->getOpcode() == Opcode && isa<Constant>(Op1->getOperand(1)) && |
| 507 | isOnlyUse(Op) && isOnlyUse(Op1)) { |
| 508 | Constant *C1 = cast<Constant>(Op->getOperand(1)); |
| 509 | Constant *C2 = cast<Constant>(Op1->getOperand(1)); |
| 510 | |
| 511 | // 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] | 512 | Constant *Folded = ConstantExpr::get(I.getOpcode(), C1, C2); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 513 | Instruction *New = BinaryOperator::create(Opcode, Op->getOperand(0), |
| 514 | Op1->getOperand(0), |
| 515 | Op1->getName(), &I); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 516 | AddToWorkList(New); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 517 | I.setOperand(0, New); |
| 518 | I.setOperand(1, Folded); |
| 519 | return true; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 520 | } |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 521 | } |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 522 | return Changed; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 523 | } |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 524 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 525 | /// SimplifyCompare - For a CmpInst this function just orders the operands |
| 526 | /// so that theyare listed from right (least complex) to left (most complex). |
| 527 | /// This puts constants before unary operators before binary operators. |
| 528 | bool InstCombiner::SimplifyCompare(CmpInst &I) { |
| 529 | if (getComplexity(I.getOperand(0)) >= getComplexity(I.getOperand(1))) |
| 530 | return false; |
| 531 | I.swapOperands(); |
| 532 | // Compare instructions are not associative so there's nothing else we can do. |
| 533 | return true; |
| 534 | } |
| 535 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 536 | // dyn_castNegVal - Given a 'sub' instruction, return the RHS of the instruction |
| 537 | // if the LHS is a constant zero (which is the 'negate' form). |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 538 | // |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 539 | static inline Value *dyn_castNegVal(Value *V) { |
| 540 | if (BinaryOperator::isNeg(V)) |
Chris Lattner | a1df33c | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 541 | return BinaryOperator::getNegArgument(V); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 542 | |
Chris Lattner | 0ce8580 | 2004-12-14 20:08:06 +0000 | [diff] [blame] | 543 | // Constants can be considered to be negated values if they can be folded. |
| 544 | if (ConstantInt *C = dyn_cast<ConstantInt>(V)) |
| 545 | return ConstantExpr::getNeg(C); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 546 | return 0; |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 549 | static inline Value *dyn_castNotVal(Value *V) { |
| 550 | if (BinaryOperator::isNot(V)) |
Chris Lattner | a1df33c | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 551 | return BinaryOperator::getNotArgument(V); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 552 | |
| 553 | // Constants can be considered to be not'ed values... |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 554 | if (ConstantInt *C = dyn_cast<ConstantInt>(V)) |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 555 | return ConstantInt::get(~C->getValue()); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 556 | return 0; |
| 557 | } |
| 558 | |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 559 | // dyn_castFoldableMul - If this value is a multiply that can be folded into |
| 560 | // other computations (because it has a constant operand), return the |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 561 | // non-constant operand of the multiply, and set CST to point to the multiplier. |
| 562 | // Otherwise, return null. |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 563 | // |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 564 | static inline Value *dyn_castFoldableMul(Value *V, ConstantInt *&CST) { |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 565 | if (V->hasOneUse() && V->getType()->isInteger()) |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 566 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 567 | if (I->getOpcode() == Instruction::Mul) |
Chris Lattner | 50e60c7 | 2004-11-15 05:54:07 +0000 | [diff] [blame] | 568 | if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 569 | return I->getOperand(0); |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 570 | if (I->getOpcode() == Instruction::Shl) |
Chris Lattner | 50e60c7 | 2004-11-15 05:54:07 +0000 | [diff] [blame] | 571 | if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) { |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 572 | // The multiplier is really 1 << CST. |
Zhou Sheng | 97b52c2 | 2007-03-29 01:57:21 +0000 | [diff] [blame] | 573 | uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth(); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 574 | uint32_t CSTVal = CST->getLimitedValue(BitWidth); |
Zhou Sheng | 97b52c2 | 2007-03-29 01:57:21 +0000 | [diff] [blame] | 575 | CST = ConstantInt::get(APInt(BitWidth, 1).shl(CSTVal)); |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 576 | return I->getOperand(0); |
| 577 | } |
| 578 | } |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 579 | return 0; |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 580 | } |
Chris Lattner | af2930e | 2002-08-14 17:51:49 +0000 | [diff] [blame] | 581 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 582 | /// dyn_castGetElementPtr - If this is a getelementptr instruction or constant |
| 583 | /// expression, return it. |
| 584 | static User *dyn_castGetElementPtr(Value *V) { |
| 585 | if (isa<GetElementPtrInst>(V)) return cast<User>(V); |
| 586 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
| 587 | if (CE->getOpcode() == Instruction::GetElementPtr) |
| 588 | return cast<User>(V); |
| 589 | return false; |
| 590 | } |
| 591 | |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 592 | /// getOpcode - If this is an Instruction or a ConstantExpr, return the |
| 593 | /// opcode value. Otherwise return UserOp1. |
| 594 | static unsigned getOpcode(User *U) { |
| 595 | if (Instruction *I = dyn_cast<Instruction>(U)) |
| 596 | return I->getOpcode(); |
| 597 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) |
| 598 | return CE->getOpcode(); |
| 599 | // Use UserOp1 to mean there's no opcode. |
| 600 | return Instruction::UserOp1; |
| 601 | } |
| 602 | |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 603 | /// AddOne - Add one to a ConstantInt |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 604 | static ConstantInt *AddOne(ConstantInt *C) { |
Reid Spencer | 2149a9d | 2007-03-25 19:55:33 +0000 | [diff] [blame] | 605 | APInt Val(C->getValue()); |
| 606 | return ConstantInt::get(++Val); |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 607 | } |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 608 | /// SubOne - Subtract one from a ConstantInt |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 609 | static ConstantInt *SubOne(ConstantInt *C) { |
Reid Spencer | 2149a9d | 2007-03-25 19:55:33 +0000 | [diff] [blame] | 610 | APInt Val(C->getValue()); |
| 611 | return ConstantInt::get(--Val); |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 612 | } |
| 613 | /// Add - Add two ConstantInts together |
| 614 | static ConstantInt *Add(ConstantInt *C1, ConstantInt *C2) { |
| 615 | return ConstantInt::get(C1->getValue() + C2->getValue()); |
| 616 | } |
| 617 | /// And - Bitwise AND two ConstantInts together |
| 618 | static ConstantInt *And(ConstantInt *C1, ConstantInt *C2) { |
| 619 | return ConstantInt::get(C1->getValue() & C2->getValue()); |
| 620 | } |
| 621 | /// Subtract - Subtract one ConstantInt from another |
| 622 | static ConstantInt *Subtract(ConstantInt *C1, ConstantInt *C2) { |
| 623 | return ConstantInt::get(C1->getValue() - C2->getValue()); |
| 624 | } |
| 625 | /// Multiply - Multiply two ConstantInts together |
| 626 | static ConstantInt *Multiply(ConstantInt *C1, ConstantInt *C2) { |
| 627 | return ConstantInt::get(C1->getValue() * C2->getValue()); |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 628 | } |
Nick Lewycky | e0cfecf | 2008-02-18 22:48:05 +0000 | [diff] [blame] | 629 | /// MultiplyOverflows - True if the multiply can not be expressed in an int |
| 630 | /// this size. |
| 631 | static bool MultiplyOverflows(ConstantInt *C1, ConstantInt *C2, bool sign) { |
| 632 | uint32_t W = C1->getBitWidth(); |
| 633 | APInt LHSExt = C1->getValue(), RHSExt = C2->getValue(); |
| 634 | if (sign) { |
| 635 | LHSExt.sext(W * 2); |
| 636 | RHSExt.sext(W * 2); |
| 637 | } else { |
| 638 | LHSExt.zext(W * 2); |
| 639 | RHSExt.zext(W * 2); |
| 640 | } |
| 641 | |
| 642 | APInt MulExt = LHSExt * RHSExt; |
| 643 | |
| 644 | if (sign) { |
| 645 | APInt Min = APInt::getSignedMinValue(W).sext(W * 2); |
| 646 | APInt Max = APInt::getSignedMaxValue(W).sext(W * 2); |
| 647 | return MulExt.slt(Min) || MulExt.sgt(Max); |
| 648 | } else |
| 649 | return MulExt.ugt(APInt::getLowBitsSet(W * 2, W)); |
| 650 | } |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 651 | |
Chris Lattner | 68d5ff2 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 652 | /// ComputeMaskedBits - Determine which of the bits specified in Mask are |
| 653 | /// 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] | 654 | /// bit sets. This code only analyzes bits in Mask, in order to short-circuit |
| 655 | /// processing. |
| 656 | /// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that |
| 657 | /// we cannot optimize based on the assumption that it is zero without changing |
| 658 | /// it to be an explicit zero. If we don't change it to zero, other code could |
| 659 | /// optimized based on the contradictory assumption that it is non-zero. |
| 660 | /// Because instcombine aggressively folds operations with undef args anyway, |
| 661 | /// this won't lose us code quality. |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 662 | void InstCombiner::ComputeMaskedBits(Value *V, const APInt &Mask, |
| 663 | APInt& KnownZero, APInt& KnownOne, |
| 664 | unsigned Depth) { |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 665 | assert(V && "No Value?"); |
| 666 | assert(Depth <= 6 && "Limit Search Depth"); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 667 | uint32_t BitWidth = Mask.getBitWidth(); |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 668 | assert((V->getType()->isInteger() || isa<PointerType>(V->getType())) && |
| 669 | "Not integer or pointer type!"); |
| 670 | assert((!TD || TD->getTypeSizeInBits(V->getType()) == BitWidth) && |
| 671 | (!isa<IntegerType>(V->getType()) || |
| 672 | V->getType()->getPrimitiveSizeInBits() == BitWidth) && |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 673 | KnownZero.getBitWidth() == BitWidth && |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 674 | KnownOne.getBitWidth() == BitWidth && |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 675 | "V, Mask, KnownOne and KnownZero should have same BitWidth"); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 676 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
| 677 | // We know all of the bits for a constant! |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 678 | KnownOne = CI->getValue() & Mask; |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 679 | KnownZero = ~KnownOne & Mask; |
| 680 | return; |
| 681 | } |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 682 | // Null is all-zeros. |
| 683 | if (isa<ConstantPointerNull>(V)) { |
| 684 | KnownOne.clear(); |
| 685 | KnownZero = Mask; |
| 686 | return; |
| 687 | } |
| 688 | // The address of an aligned GlobalValue has trailing zeros. |
| 689 | if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
| 690 | unsigned Align = GV->getAlignment(); |
| 691 | if (Align == 0 && TD && GV->getType()->getElementType()->isSized()) |
| 692 | Align = TD->getPrefTypeAlignment(GV->getType()->getElementType()); |
| 693 | if (Align > 0) |
| 694 | KnownZero = Mask & APInt::getLowBitsSet(BitWidth, |
| 695 | CountTrailingZeros_32(Align)); |
| 696 | else |
| 697 | KnownZero.clear(); |
| 698 | KnownOne.clear(); |
| 699 | return; |
| 700 | } |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 701 | |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 702 | if (Depth == 6 || Mask == 0) |
| 703 | return; // Limit search depth. |
| 704 | |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 705 | User *I = dyn_cast<User>(V); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 706 | if (!I) return; |
| 707 | |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 708 | KnownZero.clear(); KnownOne.clear(); // Don't know anything. |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 709 | APInt KnownZero2(KnownZero), KnownOne2(KnownOne); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 710 | |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 711 | switch (getOpcode(I)) { |
| 712 | default: break; |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 713 | case Instruction::And: { |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 714 | // If either the LHS or the RHS are Zero, the result is zero. |
| 715 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, Depth+1); |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 716 | APInt Mask2(Mask & ~KnownZero); |
| 717 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 718 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 719 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 720 | |
| 721 | // Output known-1 bits are only known if set in both the LHS & RHS. |
| 722 | KnownOne &= KnownOne2; |
| 723 | // Output known-0 are known to be clear if zero in either the LHS | RHS. |
| 724 | KnownZero |= KnownZero2; |
| 725 | return; |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 726 | } |
| 727 | case Instruction::Or: { |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 728 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, Depth+1); |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 729 | APInt Mask2(Mask & ~KnownOne); |
| 730 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 731 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 732 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 733 | |
| 734 | // Output known-0 bits are only known if clear in both the LHS & RHS. |
| 735 | KnownZero &= KnownZero2; |
| 736 | // Output known-1 are known to be set if set in either the LHS | RHS. |
| 737 | KnownOne |= KnownOne2; |
| 738 | return; |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 739 | } |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 740 | case Instruction::Xor: { |
| 741 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, Depth+1); |
| 742 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1); |
| 743 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 744 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 745 | |
| 746 | // Output known-0 bits are known if clear or set in both the LHS & RHS. |
| 747 | APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2); |
| 748 | // Output known-1 are known to be set if set in only one of the LHS, RHS. |
| 749 | KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2); |
| 750 | KnownZero = KnownZeroOut; |
| 751 | return; |
| 752 | } |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 753 | case Instruction::Mul: { |
| 754 | APInt Mask2 = APInt::getAllOnesValue(BitWidth); |
| 755 | ComputeMaskedBits(I->getOperand(1), Mask2, KnownZero, KnownOne, Depth+1); |
| 756 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1); |
| 757 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 758 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 759 | |
| 760 | // If low bits are zero in either operand, output low known-0 bits. |
| 761 | // More trickiness is possible, but this is sufficient for the |
| 762 | // interesting case of alignment computation. |
| 763 | KnownOne.clear(); |
| 764 | unsigned TrailZ = KnownZero.countTrailingOnes() + |
| 765 | KnownZero2.countTrailingOnes(); |
| 766 | TrailZ = std::min(TrailZ, BitWidth); |
| 767 | KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ); |
| 768 | KnownZero &= Mask; |
| 769 | return; |
| 770 | } |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 771 | case Instruction::Select: |
| 772 | ComputeMaskedBits(I->getOperand(2), Mask, KnownZero, KnownOne, Depth+1); |
| 773 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1); |
| 774 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 775 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 776 | |
| 777 | // Only known if known in both the LHS and RHS. |
| 778 | KnownOne &= KnownOne2; |
| 779 | KnownZero &= KnownZero2; |
| 780 | return; |
| 781 | case Instruction::FPTrunc: |
| 782 | case Instruction::FPExt: |
| 783 | case Instruction::FPToUI: |
| 784 | case Instruction::FPToSI: |
| 785 | case Instruction::SIToFP: |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 786 | case Instruction::UIToFP: |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 787 | return; // Can't work with floating point. |
| 788 | case Instruction::PtrToInt: |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 789 | case Instruction::IntToPtr: |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 790 | // We can't handle these if we don't know the pointer size. |
| 791 | if (!TD) return; |
| 792 | // Fall through and handle them the same as zext/trunc. |
| 793 | case Instruction::ZExt: |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 794 | case Instruction::Trunc: { |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 795 | // All these have integer operands |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 796 | const Type *SrcTy = I->getOperand(0)->getType(); |
| 797 | uint32_t SrcBitWidth = TD ? |
| 798 | TD->getTypeSizeInBits(SrcTy) : |
| 799 | SrcTy->getPrimitiveSizeInBits(); |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 800 | APInt MaskIn(Mask); |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 801 | MaskIn.zextOrTrunc(SrcBitWidth); |
| 802 | KnownZero.zextOrTrunc(SrcBitWidth); |
| 803 | KnownOne.zextOrTrunc(SrcBitWidth); |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 804 | ComputeMaskedBits(I->getOperand(0), MaskIn, KnownZero, KnownOne, Depth+1); |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 805 | KnownZero.zextOrTrunc(BitWidth); |
| 806 | KnownOne.zextOrTrunc(BitWidth); |
| 807 | // Any top bits are known to be zero. |
| 808 | if (BitWidth > SrcBitWidth) |
| 809 | KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 810 | return; |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 811 | } |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 812 | case Instruction::BitCast: { |
| 813 | const Type *SrcTy = I->getOperand(0)->getType(); |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 814 | if (SrcTy->isInteger() || isa<PointerType>(SrcTy)) { |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 815 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero, KnownOne, Depth+1); |
| 816 | return; |
| 817 | } |
| 818 | break; |
| 819 | } |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 820 | case Instruction::SExt: { |
| 821 | // Compute the bits in the result that are not present in the input. |
| 822 | const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType()); |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 823 | uint32_t SrcBitWidth = SrcTy->getBitWidth(); |
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 824 | |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 825 | APInt MaskIn(Mask); |
| 826 | MaskIn.trunc(SrcBitWidth); |
| 827 | KnownZero.trunc(SrcBitWidth); |
| 828 | KnownOne.trunc(SrcBitWidth); |
| 829 | ComputeMaskedBits(I->getOperand(0), MaskIn, KnownZero, KnownOne, Depth+1); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 830 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 831 | KnownZero.zext(BitWidth); |
| 832 | KnownOne.zext(BitWidth); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 833 | |
| 834 | // If the sign bit of the input is known set or clear, then we know the |
| 835 | // top bits of the result. |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 836 | if (KnownZero[SrcBitWidth-1]) // Input sign bit known zero |
Zhou Sheng | 34a4b38 | 2007-03-28 17:38:21 +0000 | [diff] [blame] | 837 | KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth); |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 838 | else if (KnownOne[SrcBitWidth-1]) // Input sign bit known set |
Zhou Sheng | 34a4b38 | 2007-03-28 17:38:21 +0000 | [diff] [blame] | 839 | KnownOne |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 840 | return; |
| 841 | } |
| 842 | case Instruction::Shl: |
| 843 | // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0 |
| 844 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 845 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 846 | APInt Mask2(Mask.lshr(ShiftAmt)); |
| 847 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero, KnownOne, Depth+1); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 848 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
Zhou Sheng | 430f626 | 2007-03-12 05:44:52 +0000 | [diff] [blame] | 849 | KnownZero <<= ShiftAmt; |
| 850 | KnownOne <<= ShiftAmt; |
Reid Spencer | 2149a9d | 2007-03-25 19:55:33 +0000 | [diff] [blame] | 851 | KnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt); // low bits known 0 |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 852 | return; |
| 853 | } |
| 854 | break; |
| 855 | case Instruction::LShr: |
| 856 | // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0 |
| 857 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 858 | // Compute the new bits that are at the top now. |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 859 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 860 | |
| 861 | // Unsigned shift right. |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 862 | APInt Mask2(Mask.shl(ShiftAmt)); |
| 863 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero,KnownOne,Depth+1); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 864 | assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); |
| 865 | KnownZero = APIntOps::lshr(KnownZero, ShiftAmt); |
| 866 | KnownOne = APIntOps::lshr(KnownOne, ShiftAmt); |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 867 | // high bits known zero. |
| 868 | KnownZero |= APInt::getHighBitsSet(BitWidth, ShiftAmt); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 869 | return; |
| 870 | } |
| 871 | break; |
| 872 | case Instruction::AShr: |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 873 | // (ashr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0 |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 874 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 875 | // Compute the new bits that are at the top now. |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 876 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 877 | |
| 878 | // Signed shift right. |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 879 | APInt Mask2(Mask.shl(ShiftAmt)); |
| 880 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero,KnownOne,Depth+1); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 881 | assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); |
| 882 | KnownZero = APIntOps::lshr(KnownZero, ShiftAmt); |
| 883 | KnownOne = APIntOps::lshr(KnownOne, ShiftAmt); |
| 884 | |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 885 | APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt)); |
| 886 | if (KnownZero[BitWidth-ShiftAmt-1]) // New bits are known zero. |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 887 | KnownZero |= HighBits; |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 888 | else if (KnownOne[BitWidth-ShiftAmt-1]) // New bits are known one. |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 889 | KnownOne |= HighBits; |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 890 | return; |
| 891 | } |
| 892 | break; |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 893 | case Instruction::Sub: { |
| 894 | if (ConstantInt *CLHS = dyn_cast<ConstantInt>(I->getOperand(0))) { |
| 895 | // We know that the top bits of C-X are clear if X contains less bits |
| 896 | // than C (i.e. no wrap-around can happen). For example, 20-X is |
| 897 | // positive if we can prove that X is >= 0 and < 16. |
| 898 | if (!CLHS->getValue().isNegative()) { |
| 899 | unsigned NLZ = (CLHS->getValue()+1).countLeadingZeros(); |
| 900 | // NLZ can't be BitWidth with no sign bit |
| 901 | APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1); |
| 902 | ComputeMaskedBits(I->getOperand(1), MaskV, KnownZero, KnownOne, Depth+1); |
| 903 | |
| 904 | // If all of the MaskV bits are known to be zero, then we know the output |
| 905 | // top bits are zero, because we now know that the output is from [0-C]. |
| 906 | if ((KnownZero & MaskV) == MaskV) { |
| 907 | unsigned NLZ2 = CLHS->getValue().countLeadingZeros(); |
| 908 | // Top bits known zero. |
| 909 | KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask; |
| 910 | KnownOne = APInt(BitWidth, 0); // No one bits known. |
| 911 | } else { |
| 912 | KnownZero = KnownOne = APInt(BitWidth, 0); // Otherwise, nothing known. |
| 913 | } |
| 914 | return; |
| 915 | } |
| 916 | } |
| 917 | } |
| 918 | // fall through |
Duncan Sands | 1d57a75 | 2008-03-21 08:32:17 +0000 | [diff] [blame] | 919 | case Instruction::Add: { |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 920 | // If either the LHS or the RHS are Zero, the result is zero. |
| 921 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, Depth+1); |
| 922 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1); |
| 923 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 924 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 925 | |
| 926 | // Output known-0 bits are known if clear or set in both the low clear bits |
| 927 | // common to both LHS & RHS. For example, 8+(X<<3) is known to have the |
| 928 | // low 3 bits clear. |
| 929 | unsigned KnownZeroOut = std::min(KnownZero.countTrailingOnes(), |
| 930 | KnownZero2.countTrailingOnes()); |
| 931 | |
| 932 | KnownZero = APInt::getLowBitsSet(BitWidth, KnownZeroOut); |
| 933 | KnownOne = APInt(BitWidth, 0); |
| 934 | return; |
Duncan Sands | 1d57a75 | 2008-03-21 08:32:17 +0000 | [diff] [blame] | 935 | } |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 936 | case Instruction::SRem: |
| 937 | if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 938 | APInt RA = Rem->getValue(); |
| 939 | if (RA.isPowerOf2() || (-RA).isPowerOf2()) { |
| 940 | APInt LowBits = RA.isStrictlyPositive() ? ((RA - 1) | RA) : ~RA; |
| 941 | APInt Mask2 = LowBits | APInt::getSignBit(BitWidth); |
| 942 | ComputeMaskedBits(I->getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1); |
| 943 | |
| 944 | // The sign of a remainder is equal to the sign of the first |
| 945 | // operand (zero being positive). |
| 946 | if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits)) |
| 947 | KnownZero2 |= ~LowBits; |
| 948 | else if (KnownOne2[BitWidth-1]) |
| 949 | KnownOne2 |= ~LowBits; |
| 950 | |
| 951 | KnownZero |= KnownZero2 & Mask; |
| 952 | KnownOne |= KnownOne2 & Mask; |
| 953 | |
| 954 | assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); |
| 955 | } |
| 956 | } |
| 957 | break; |
| 958 | case Instruction::URem: |
| 959 | if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 960 | APInt RA = Rem->getValue(); |
| 961 | if (RA.isStrictlyPositive() && RA.isPowerOf2()) { |
| 962 | APInt LowBits = (RA - 1) | RA; |
| 963 | APInt Mask2 = LowBits & Mask; |
| 964 | KnownZero |= ~LowBits & Mask; |
| 965 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero, KnownOne,Depth+1); |
| 966 | assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); |
| 967 | } |
| 968 | } else { |
| 969 | // Since the result is less than or equal to RHS, any leading zero bits |
| 970 | // in RHS must also exist in the result. |
| 971 | APInt AllOnes = APInt::getAllOnesValue(BitWidth); |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 972 | ComputeMaskedBits(I->getOperand(1), AllOnes, KnownZero2, KnownOne2, |
| 973 | Depth+1); |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 974 | |
| 975 | uint32_t Leaders = KnownZero2.countLeadingOnes(); |
| 976 | KnownZero |= APInt::getHighBitsSet(BitWidth, Leaders) & Mask; |
| 977 | assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); |
| 978 | } |
| 979 | break; |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 980 | |
| 981 | case Instruction::Alloca: |
| 982 | case Instruction::Malloc: { |
| 983 | AllocationInst *AI = cast<AllocationInst>(V); |
| 984 | unsigned Align = AI->getAlignment(); |
| 985 | if (Align == 0 && TD) { |
| 986 | if (isa<AllocaInst>(AI)) |
| 987 | Align = TD->getPrefTypeAlignment(AI->getType()->getElementType()); |
| 988 | else if (isa<MallocInst>(AI)) { |
| 989 | // Malloc returns maximally aligned memory. |
| 990 | Align = TD->getABITypeAlignment(AI->getType()->getElementType()); |
| 991 | Align = |
| 992 | std::max(Align, |
| 993 | (unsigned)TD->getABITypeAlignment(Type::DoubleTy)); |
| 994 | Align = |
| 995 | std::max(Align, |
| 996 | (unsigned)TD->getABITypeAlignment(Type::Int64Ty)); |
| 997 | } |
| 998 | } |
| 999 | |
| 1000 | if (Align > 0) |
| 1001 | KnownZero = Mask & APInt::getLowBitsSet(BitWidth, |
| 1002 | CountTrailingZeros_32(Align)); |
| 1003 | break; |
| 1004 | } |
| 1005 | case Instruction::GetElementPtr: { |
| 1006 | // Analyze all of the subscripts of this getelementptr instruction |
| 1007 | // to determine if we can prove known low zero bits. |
| 1008 | APInt LocalMask = APInt::getAllOnesValue(BitWidth); |
| 1009 | APInt LocalKnownZero(BitWidth, 0), LocalKnownOne(BitWidth, 0); |
| 1010 | ComputeMaskedBits(I->getOperand(0), LocalMask, |
| 1011 | LocalKnownZero, LocalKnownOne, Depth+1); |
| 1012 | unsigned TrailZ = LocalKnownZero.countTrailingOnes(); |
| 1013 | |
| 1014 | gep_type_iterator GTI = gep_type_begin(I); |
| 1015 | for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) { |
| 1016 | Value *Index = I->getOperand(i); |
| 1017 | if (const StructType *STy = dyn_cast<StructType>(*GTI)) { |
| 1018 | // Handle struct member offset arithmetic. |
| 1019 | if (!TD) return; |
| 1020 | const StructLayout *SL = TD->getStructLayout(STy); |
| 1021 | unsigned Idx = cast<ConstantInt>(Index)->getZExtValue(); |
| 1022 | uint64_t Offset = SL->getElementOffset(Idx); |
| 1023 | TrailZ = std::min(TrailZ, |
| 1024 | CountTrailingZeros_64(Offset)); |
| 1025 | } else { |
| 1026 | // Handle array index arithmetic. |
| 1027 | const Type *IndexedTy = GTI.getIndexedType(); |
| 1028 | if (!IndexedTy->isSized()) return; |
| 1029 | unsigned GEPOpiBits = Index->getType()->getPrimitiveSizeInBits(); |
| 1030 | uint64_t TypeSize = TD ? TD->getABITypeSize(IndexedTy) : 1; |
| 1031 | LocalMask = APInt::getAllOnesValue(GEPOpiBits); |
| 1032 | LocalKnownZero = LocalKnownOne = APInt(GEPOpiBits, 0); |
| 1033 | ComputeMaskedBits(Index, LocalMask, |
| 1034 | LocalKnownZero, LocalKnownOne, Depth+1); |
| 1035 | TrailZ = std::min(TrailZ, |
| 1036 | CountTrailingZeros_64(TypeSize) + |
| 1037 | LocalKnownZero.countTrailingOnes()); |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) & Mask; |
| 1042 | break; |
| 1043 | } |
| 1044 | case Instruction::PHI: { |
| 1045 | PHINode *P = cast<PHINode>(I); |
| 1046 | // Handle the case of a simple two-predecessor recurrence PHI. |
| 1047 | // There's a lot more that could theoretically be done here, but |
| 1048 | // this is sufficient to catch some interesting cases. |
| 1049 | if (P->getNumIncomingValues() == 2) { |
| 1050 | for (unsigned i = 0; i != 2; ++i) { |
| 1051 | Value *L = P->getIncomingValue(i); |
| 1052 | Value *R = P->getIncomingValue(!i); |
| 1053 | User *LU = dyn_cast<User>(L); |
| 1054 | unsigned Opcode = LU ? getOpcode(LU) : (unsigned)Instruction::UserOp1; |
| 1055 | // Check for operations that have the property that if |
| 1056 | // both their operands have low zero bits, the result |
| 1057 | // will have low zero bits. |
| 1058 | if (Opcode == Instruction::Add || |
| 1059 | Opcode == Instruction::Sub || |
| 1060 | Opcode == Instruction::And || |
| 1061 | Opcode == Instruction::Or || |
| 1062 | Opcode == Instruction::Mul) { |
| 1063 | Value *LL = LU->getOperand(0); |
| 1064 | Value *LR = LU->getOperand(1); |
| 1065 | // Find a recurrence. |
| 1066 | if (LL == I) |
| 1067 | L = LR; |
| 1068 | else if (LR == I) |
| 1069 | L = LL; |
| 1070 | else |
| 1071 | break; |
| 1072 | // Ok, we have a PHI of the form L op= R. Check for low |
| 1073 | // zero bits. |
| 1074 | APInt Mask2 = APInt::getAllOnesValue(BitWidth); |
| 1075 | ComputeMaskedBits(R, Mask2, KnownZero2, KnownOne2, Depth+1); |
| 1076 | Mask2 = APInt::getLowBitsSet(BitWidth, |
| 1077 | KnownZero2.countTrailingOnes()); |
| 1078 | KnownOne2.clear(); |
| 1079 | KnownZero2.clear(); |
| 1080 | ComputeMaskedBits(L, Mask2, KnownZero2, KnownOne2, Depth+1); |
| 1081 | KnownZero = Mask & |
| 1082 | APInt::getLowBitsSet(BitWidth, |
| 1083 | KnownZero2.countTrailingOnes()); |
| 1084 | break; |
| 1085 | } |
| 1086 | } |
| 1087 | } |
| 1088 | break; |
| 1089 | } |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 1090 | } |
| 1091 | } |
| 1092 | |
Reid Spencer | e7816b5 | 2007-03-08 01:52:58 +0000 | [diff] [blame] | 1093 | /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use |
| 1094 | /// this predicate to simplify operations downstream. Mask is known to be zero |
| 1095 | /// for bits that V cannot have. |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 1096 | bool InstCombiner::MaskedValueIsZero(Value *V, const APInt& Mask, |
| 1097 | unsigned Depth) { |
Zhou Sheng | edd089c | 2007-03-12 16:54:56 +0000 | [diff] [blame] | 1098 | APInt KnownZero(Mask.getBitWidth(), 0), KnownOne(Mask.getBitWidth(), 0); |
Reid Spencer | e7816b5 | 2007-03-08 01:52:58 +0000 | [diff] [blame] | 1099 | ComputeMaskedBits(V, Mask, KnownZero, KnownOne, Depth); |
| 1100 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 1101 | return (KnownZero & Mask) == Mask; |
| 1102 | } |
| 1103 | |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1104 | /// ShrinkDemandedConstant - Check to see if the specified operand of the |
| 1105 | /// specified instruction is a constant integer. If so, check to see if there |
| 1106 | /// are any bits set in the constant that are not demanded. If so, shrink the |
| 1107 | /// constant and return true. |
| 1108 | static bool ShrinkDemandedConstant(Instruction *I, unsigned OpNo, |
Reid Spencer | 6b79e2d | 2007-03-12 17:15:10 +0000 | [diff] [blame] | 1109 | APInt Demanded) { |
| 1110 | assert(I && "No instruction?"); |
| 1111 | assert(OpNo < I->getNumOperands() && "Operand index too large"); |
| 1112 | |
| 1113 | // If the operand is not a constant integer, nothing to do. |
| 1114 | ConstantInt *OpC = dyn_cast<ConstantInt>(I->getOperand(OpNo)); |
| 1115 | if (!OpC) return false; |
| 1116 | |
| 1117 | // If there are no bits set that aren't demanded, nothing to do. |
| 1118 | Demanded.zextOrTrunc(OpC->getValue().getBitWidth()); |
| 1119 | if ((~Demanded & OpC->getValue()) == 0) |
| 1120 | return false; |
| 1121 | |
| 1122 | // This instruction is producing bits that are not demanded. Shrink the RHS. |
| 1123 | Demanded &= OpC->getValue(); |
| 1124 | I->setOperand(OpNo, ConstantInt::get(Demanded)); |
| 1125 | return true; |
| 1126 | } |
| 1127 | |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 1128 | // ComputeSignedMinMaxValuesFromKnownBits - Given a signed integer type and a |
| 1129 | // set of known zero and one bits, compute the maximum and minimum values that |
| 1130 | // could have the specified known zero and known one bits, returning them in |
| 1131 | // min/max. |
| 1132 | static void ComputeSignedMinMaxValuesFromKnownBits(const Type *Ty, |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 1133 | const APInt& KnownZero, |
| 1134 | const APInt& KnownOne, |
| 1135 | APInt& Min, APInt& Max) { |
| 1136 | uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth(); |
| 1137 | assert(KnownZero.getBitWidth() == BitWidth && |
| 1138 | KnownOne.getBitWidth() == BitWidth && |
| 1139 | Min.getBitWidth() == BitWidth && Max.getBitWidth() == BitWidth && |
| 1140 | "Ty, KnownZero, KnownOne and Min, Max must have equal bitwidth."); |
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 1141 | APInt UnknownBits = ~(KnownZero|KnownOne); |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 1142 | |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 1143 | // The minimum value is when all unknown bits are zeros, EXCEPT for the sign |
| 1144 | // bit if it is unknown. |
| 1145 | Min = KnownOne; |
| 1146 | Max = KnownOne|UnknownBits; |
| 1147 | |
Zhou Sheng | 4acf155 | 2007-03-28 05:15:57 +0000 | [diff] [blame] | 1148 | if (UnknownBits[BitWidth-1]) { // Sign bit is unknown |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 1149 | Min.set(BitWidth-1); |
| 1150 | Max.clear(BitWidth-1); |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 1151 | } |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | // ComputeUnsignedMinMaxValuesFromKnownBits - Given an unsigned integer type and |
| 1155 | // a set of known zero and one bits, compute the maximum and minimum values that |
| 1156 | // could have the specified known zero and known one bits, returning them in |
| 1157 | // min/max. |
| 1158 | static void ComputeUnsignedMinMaxValuesFromKnownBits(const Type *Ty, |
Chris Lattner | a9ff5eb | 2007-08-05 08:47:58 +0000 | [diff] [blame] | 1159 | const APInt &KnownZero, |
| 1160 | const APInt &KnownOne, |
| 1161 | APInt &Min, APInt &Max) { |
| 1162 | uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth(); BitWidth = BitWidth; |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 1163 | assert(KnownZero.getBitWidth() == BitWidth && |
| 1164 | KnownOne.getBitWidth() == BitWidth && |
| 1165 | Min.getBitWidth() == BitWidth && Max.getBitWidth() && |
| 1166 | "Ty, KnownZero, KnownOne and Min, Max must have equal bitwidth."); |
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 1167 | APInt UnknownBits = ~(KnownZero|KnownOne); |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 1168 | |
| 1169 | // The minimum value is when the unknown bits are all zeros. |
| 1170 | Min = KnownOne; |
| 1171 | // The maximum value is when the unknown bits are all ones. |
| 1172 | Max = KnownOne|UnknownBits; |
| 1173 | } |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1174 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1175 | /// SimplifyDemandedBits - This function attempts to replace V with a simpler |
| 1176 | /// value based on the demanded bits. When this function is called, it is known |
| 1177 | /// that only the bits set in DemandedMask of the result of V are ever used |
| 1178 | /// downstream. Consequently, depending on the mask and V, it may be possible |
| 1179 | /// to replace V with a constant or one of its operands. In such cases, this |
| 1180 | /// function does the replacement and returns true. In all other cases, it |
| 1181 | /// returns false after analyzing the expression and setting KnownOne and known |
| 1182 | /// to be one in the expression. KnownZero contains all the bits that are known |
| 1183 | /// to be zero in the expression. These are provided to potentially allow the |
| 1184 | /// caller (which might recursively be SimplifyDemandedBits itself) to simplify |
| 1185 | /// the expression. KnownOne and KnownZero always follow the invariant that |
| 1186 | /// KnownOne & KnownZero == 0. That is, a bit can't be both 1 and 0. Note that |
| 1187 | /// the bits in KnownOne and KnownZero may only be accurate for those bits set |
| 1188 | /// in DemandedMask. Note also that the bitwidth of V, DemandedMask, KnownZero |
| 1189 | /// and KnownOne must all be the same. |
| 1190 | bool InstCombiner::SimplifyDemandedBits(Value *V, APInt DemandedMask, |
| 1191 | APInt& KnownZero, APInt& KnownOne, |
| 1192 | unsigned Depth) { |
| 1193 | assert(V != 0 && "Null pointer of Value???"); |
| 1194 | assert(Depth <= 6 && "Limit Search Depth"); |
| 1195 | uint32_t BitWidth = DemandedMask.getBitWidth(); |
| 1196 | const IntegerType *VTy = cast<IntegerType>(V->getType()); |
| 1197 | assert(VTy->getBitWidth() == BitWidth && |
| 1198 | KnownZero.getBitWidth() == BitWidth && |
| 1199 | KnownOne.getBitWidth() == BitWidth && |
| 1200 | "Value *V, DemandedMask, KnownZero and KnownOne \ |
| 1201 | must have same BitWidth"); |
| 1202 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
| 1203 | // We know all of the bits for a constant! |
| 1204 | KnownOne = CI->getValue() & DemandedMask; |
| 1205 | KnownZero = ~KnownOne & DemandedMask; |
| 1206 | return false; |
| 1207 | } |
| 1208 | |
Zhou Sheng | 9670445 | 2007-03-14 03:21:24 +0000 | [diff] [blame] | 1209 | KnownZero.clear(); |
| 1210 | KnownOne.clear(); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1211 | if (!V->hasOneUse()) { // Other users may use these bits. |
| 1212 | if (Depth != 0) { // Not at the root. |
| 1213 | // Just compute the KnownZero/KnownOne bits to simplify things downstream. |
| 1214 | ComputeMaskedBits(V, DemandedMask, KnownZero, KnownOne, Depth); |
| 1215 | return false; |
| 1216 | } |
| 1217 | // If this is the root being simplified, allow it to have multiple uses, |
| 1218 | // just set the DemandedMask to all bits. |
| 1219 | DemandedMask = APInt::getAllOnesValue(BitWidth); |
| 1220 | } else if (DemandedMask == 0) { // Not demanding any bits from V. |
| 1221 | if (V != UndefValue::get(VTy)) |
| 1222 | return UpdateValueUsesWith(V, UndefValue::get(VTy)); |
| 1223 | return false; |
| 1224 | } else if (Depth == 6) { // Limit search depth. |
| 1225 | return false; |
| 1226 | } |
| 1227 | |
| 1228 | Instruction *I = dyn_cast<Instruction>(V); |
| 1229 | if (!I) return false; // Only analyze instructions. |
| 1230 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1231 | APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0); |
| 1232 | APInt &RHSKnownZero = KnownZero, &RHSKnownOne = KnownOne; |
| 1233 | switch (I->getOpcode()) { |
| 1234 | default: break; |
| 1235 | case Instruction::And: |
| 1236 | // If either the LHS or the RHS are Zero, the result is zero. |
| 1237 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, |
| 1238 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 1239 | return true; |
| 1240 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1241 | "Bits known to be one AND zero?"); |
| 1242 | |
| 1243 | // If something is known zero on the RHS, the bits aren't demanded on the |
| 1244 | // LHS. |
| 1245 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask & ~RHSKnownZero, |
| 1246 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1247 | return true; |
| 1248 | assert((LHSKnownZero & LHSKnownOne) == 0 && |
| 1249 | "Bits known to be one AND zero?"); |
| 1250 | |
| 1251 | // If all of the demanded bits are known 1 on one side, return the other. |
| 1252 | // These bits cannot contribute to the result of the 'and'. |
| 1253 | if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) == |
| 1254 | (DemandedMask & ~LHSKnownZero)) |
| 1255 | return UpdateValueUsesWith(I, I->getOperand(0)); |
| 1256 | if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) == |
| 1257 | (DemandedMask & ~RHSKnownZero)) |
| 1258 | return UpdateValueUsesWith(I, I->getOperand(1)); |
| 1259 | |
| 1260 | // If all of the demanded bits in the inputs are known zeros, return zero. |
| 1261 | if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask) |
| 1262 | return UpdateValueUsesWith(I, Constant::getNullValue(VTy)); |
| 1263 | |
| 1264 | // If the RHS is a constant, see if we can simplify it. |
| 1265 | if (ShrinkDemandedConstant(I, 1, DemandedMask & ~LHSKnownZero)) |
| 1266 | return UpdateValueUsesWith(I, I); |
| 1267 | |
| 1268 | // Output known-1 bits are only known if set in both the LHS & RHS. |
| 1269 | RHSKnownOne &= LHSKnownOne; |
| 1270 | // Output known-0 are known to be clear if zero in either the LHS | RHS. |
| 1271 | RHSKnownZero |= LHSKnownZero; |
| 1272 | break; |
| 1273 | case Instruction::Or: |
| 1274 | // If either the LHS or the RHS are One, the result is One. |
| 1275 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, |
| 1276 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 1277 | return true; |
| 1278 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1279 | "Bits known to be one AND zero?"); |
| 1280 | // If something is known one on the RHS, the bits aren't demanded on the |
| 1281 | // LHS. |
| 1282 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask & ~RHSKnownOne, |
| 1283 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1284 | return true; |
| 1285 | assert((LHSKnownZero & LHSKnownOne) == 0 && |
| 1286 | "Bits known to be one AND zero?"); |
| 1287 | |
| 1288 | // If all of the demanded bits are known zero on one side, return the other. |
| 1289 | // These bits cannot contribute to the result of the 'or'. |
| 1290 | if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) == |
| 1291 | (DemandedMask & ~LHSKnownOne)) |
| 1292 | return UpdateValueUsesWith(I, I->getOperand(0)); |
| 1293 | if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) == |
| 1294 | (DemandedMask & ~RHSKnownOne)) |
| 1295 | return UpdateValueUsesWith(I, I->getOperand(1)); |
| 1296 | |
| 1297 | // If all of the potentially set bits on one side are known to be set on |
| 1298 | // the other side, just use the 'other' side. |
| 1299 | if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) == |
| 1300 | (DemandedMask & (~RHSKnownZero))) |
| 1301 | return UpdateValueUsesWith(I, I->getOperand(0)); |
| 1302 | if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) == |
| 1303 | (DemandedMask & (~LHSKnownZero))) |
| 1304 | return UpdateValueUsesWith(I, I->getOperand(1)); |
| 1305 | |
| 1306 | // If the RHS is a constant, see if we can simplify it. |
| 1307 | if (ShrinkDemandedConstant(I, 1, DemandedMask)) |
| 1308 | return UpdateValueUsesWith(I, I); |
| 1309 | |
| 1310 | // Output known-0 bits are only known if clear in both the LHS & RHS. |
| 1311 | RHSKnownZero &= LHSKnownZero; |
| 1312 | // Output known-1 are known to be set if set in either the LHS | RHS. |
| 1313 | RHSKnownOne |= LHSKnownOne; |
| 1314 | break; |
| 1315 | case Instruction::Xor: { |
| 1316 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, |
| 1317 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 1318 | return true; |
| 1319 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1320 | "Bits known to be one AND zero?"); |
| 1321 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, |
| 1322 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1323 | return true; |
| 1324 | assert((LHSKnownZero & LHSKnownOne) == 0 && |
| 1325 | "Bits known to be one AND zero?"); |
| 1326 | |
| 1327 | // If all of the demanded bits are known zero on one side, return the other. |
| 1328 | // These bits cannot contribute to the result of the 'xor'. |
| 1329 | if ((DemandedMask & RHSKnownZero) == DemandedMask) |
| 1330 | return UpdateValueUsesWith(I, I->getOperand(0)); |
| 1331 | if ((DemandedMask & LHSKnownZero) == DemandedMask) |
| 1332 | return UpdateValueUsesWith(I, I->getOperand(1)); |
| 1333 | |
| 1334 | // Output known-0 bits are known if clear or set in both the LHS & RHS. |
| 1335 | APInt KnownZeroOut = (RHSKnownZero & LHSKnownZero) | |
| 1336 | (RHSKnownOne & LHSKnownOne); |
| 1337 | // Output known-1 are known to be set if set in only one of the LHS, RHS. |
| 1338 | APInt KnownOneOut = (RHSKnownZero & LHSKnownOne) | |
| 1339 | (RHSKnownOne & LHSKnownZero); |
| 1340 | |
| 1341 | // If all of the demanded bits are known to be zero on one side or the |
| 1342 | // other, turn this into an *inclusive* or. |
| 1343 | // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0 |
| 1344 | if ((DemandedMask & ~RHSKnownZero & ~LHSKnownZero) == 0) { |
| 1345 | Instruction *Or = |
| 1346 | BinaryOperator::createOr(I->getOperand(0), I->getOperand(1), |
| 1347 | I->getName()); |
| 1348 | InsertNewInstBefore(Or, *I); |
| 1349 | return UpdateValueUsesWith(I, Or); |
| 1350 | } |
| 1351 | |
| 1352 | // If all of the demanded bits on one side are known, and all of the set |
| 1353 | // bits on that side are also known to be set on the other side, turn this |
| 1354 | // into an AND, as we know the bits will be cleared. |
| 1355 | // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2 |
| 1356 | if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) { |
| 1357 | // all known |
| 1358 | if ((RHSKnownOne & LHSKnownOne) == RHSKnownOne) { |
| 1359 | Constant *AndC = ConstantInt::get(~RHSKnownOne & DemandedMask); |
| 1360 | Instruction *And = |
| 1361 | BinaryOperator::createAnd(I->getOperand(0), AndC, "tmp"); |
| 1362 | InsertNewInstBefore(And, *I); |
| 1363 | return UpdateValueUsesWith(I, And); |
| 1364 | } |
| 1365 | } |
| 1366 | |
| 1367 | // If the RHS is a constant, see if we can simplify it. |
| 1368 | // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1. |
| 1369 | if (ShrinkDemandedConstant(I, 1, DemandedMask)) |
| 1370 | return UpdateValueUsesWith(I, I); |
| 1371 | |
| 1372 | RHSKnownZero = KnownZeroOut; |
| 1373 | RHSKnownOne = KnownOneOut; |
| 1374 | break; |
| 1375 | } |
| 1376 | case Instruction::Select: |
| 1377 | if (SimplifyDemandedBits(I->getOperand(2), DemandedMask, |
| 1378 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 1379 | return true; |
| 1380 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, |
| 1381 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1382 | return true; |
| 1383 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1384 | "Bits known to be one AND zero?"); |
| 1385 | assert((LHSKnownZero & LHSKnownOne) == 0 && |
| 1386 | "Bits known to be one AND zero?"); |
| 1387 | |
| 1388 | // If the operands are constants, see if we can simplify them. |
| 1389 | if (ShrinkDemandedConstant(I, 1, DemandedMask)) |
| 1390 | return UpdateValueUsesWith(I, I); |
| 1391 | if (ShrinkDemandedConstant(I, 2, DemandedMask)) |
| 1392 | return UpdateValueUsesWith(I, I); |
| 1393 | |
| 1394 | // Only known if known in both the LHS and RHS. |
| 1395 | RHSKnownOne &= LHSKnownOne; |
| 1396 | RHSKnownZero &= LHSKnownZero; |
| 1397 | break; |
| 1398 | case Instruction::Trunc: { |
| 1399 | uint32_t truncBf = |
| 1400 | cast<IntegerType>(I->getOperand(0)->getType())->getBitWidth(); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1401 | DemandedMask.zext(truncBf); |
| 1402 | RHSKnownZero.zext(truncBf); |
| 1403 | RHSKnownOne.zext(truncBf); |
| 1404 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, |
| 1405 | RHSKnownZero, RHSKnownOne, Depth+1)) |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1406 | return true; |
| 1407 | DemandedMask.trunc(BitWidth); |
| 1408 | RHSKnownZero.trunc(BitWidth); |
| 1409 | RHSKnownOne.trunc(BitWidth); |
| 1410 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1411 | "Bits known to be one AND zero?"); |
| 1412 | break; |
| 1413 | } |
| 1414 | case Instruction::BitCast: |
| 1415 | if (!I->getOperand(0)->getType()->isInteger()) |
| 1416 | return false; |
| 1417 | |
| 1418 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, |
| 1419 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 1420 | return true; |
| 1421 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1422 | "Bits known to be one AND zero?"); |
| 1423 | break; |
| 1424 | case Instruction::ZExt: { |
| 1425 | // Compute the bits in the result that are not present in the input. |
| 1426 | const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType()); |
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 1427 | uint32_t SrcBitWidth = SrcTy->getBitWidth(); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1428 | |
Zhou Sheng | d48653a | 2007-03-29 04:45:55 +0000 | [diff] [blame] | 1429 | DemandedMask.trunc(SrcBitWidth); |
| 1430 | RHSKnownZero.trunc(SrcBitWidth); |
| 1431 | RHSKnownOne.trunc(SrcBitWidth); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1432 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, |
| 1433 | RHSKnownZero, RHSKnownOne, Depth+1)) |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1434 | return true; |
| 1435 | DemandedMask.zext(BitWidth); |
| 1436 | RHSKnownZero.zext(BitWidth); |
| 1437 | RHSKnownOne.zext(BitWidth); |
| 1438 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1439 | "Bits known to be one AND zero?"); |
| 1440 | // The top bits are known to be zero. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1441 | RHSKnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1442 | break; |
| 1443 | } |
| 1444 | case Instruction::SExt: { |
| 1445 | // Compute the bits in the result that are not present in the input. |
| 1446 | const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType()); |
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 1447 | uint32_t SrcBitWidth = SrcTy->getBitWidth(); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1448 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1449 | APInt InputDemandedBits = DemandedMask & |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1450 | APInt::getLowBitsSet(BitWidth, SrcBitWidth); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1451 | |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1452 | APInt NewBits(APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1453 | // If any of the sign extended bits are demanded, we know that the sign |
| 1454 | // bit is demanded. |
| 1455 | if ((NewBits & DemandedMask) != 0) |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 1456 | InputDemandedBits.set(SrcBitWidth-1); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1457 | |
Zhou Sheng | d48653a | 2007-03-29 04:45:55 +0000 | [diff] [blame] | 1458 | InputDemandedBits.trunc(SrcBitWidth); |
| 1459 | RHSKnownZero.trunc(SrcBitWidth); |
| 1460 | RHSKnownOne.trunc(SrcBitWidth); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1461 | if (SimplifyDemandedBits(I->getOperand(0), InputDemandedBits, |
| 1462 | RHSKnownZero, RHSKnownOne, Depth+1)) |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1463 | return true; |
| 1464 | InputDemandedBits.zext(BitWidth); |
| 1465 | RHSKnownZero.zext(BitWidth); |
| 1466 | RHSKnownOne.zext(BitWidth); |
| 1467 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1468 | "Bits known to be one AND zero?"); |
| 1469 | |
| 1470 | // If the sign bit of the input is known set or clear, then we know the |
| 1471 | // top bits of the result. |
| 1472 | |
| 1473 | // If the input sign bit is known zero, or if the NewBits are not demanded |
| 1474 | // convert this into a zero extension. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1475 | if (RHSKnownZero[SrcBitWidth-1] || (NewBits & ~DemandedMask) == NewBits) |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1476 | { |
| 1477 | // Convert to ZExt cast |
| 1478 | CastInst *NewCast = new ZExtInst(I->getOperand(0), VTy, I->getName(), I); |
| 1479 | return UpdateValueUsesWith(I, NewCast); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1480 | } else if (RHSKnownOne[SrcBitWidth-1]) { // Input sign bit known set |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1481 | RHSKnownOne |= NewBits; |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1482 | } |
| 1483 | break; |
| 1484 | } |
| 1485 | case Instruction::Add: { |
| 1486 | // Figure out what the input bits are. If the top bits of the and result |
| 1487 | // are not demanded, then the add doesn't demand them from its input |
| 1488 | // either. |
Reid Spencer | 55702aa | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 1489 | uint32_t NLZ = DemandedMask.countLeadingZeros(); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1490 | |
| 1491 | // If there is a constant on the RHS, there are a variety of xformations |
| 1492 | // we can do. |
| 1493 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 1494 | // If null, this should be simplified elsewhere. Some of the xforms here |
| 1495 | // won't work if the RHS is zero. |
| 1496 | if (RHS->isZero()) |
| 1497 | break; |
| 1498 | |
| 1499 | // If the top bit of the output is demanded, demand everything from the |
| 1500 | // input. Otherwise, we demand all the input bits except NLZ top bits. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1501 | APInt InDemandedBits(APInt::getLowBitsSet(BitWidth, BitWidth - NLZ)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1502 | |
| 1503 | // Find information about known zero/one bits in the input. |
| 1504 | if (SimplifyDemandedBits(I->getOperand(0), InDemandedBits, |
| 1505 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1506 | return true; |
| 1507 | |
| 1508 | // If the RHS of the add has bits set that can't affect the input, reduce |
| 1509 | // the constant. |
| 1510 | if (ShrinkDemandedConstant(I, 1, InDemandedBits)) |
| 1511 | return UpdateValueUsesWith(I, I); |
| 1512 | |
| 1513 | // Avoid excess work. |
| 1514 | if (LHSKnownZero == 0 && LHSKnownOne == 0) |
| 1515 | break; |
| 1516 | |
| 1517 | // Turn it into OR if input bits are zero. |
| 1518 | if ((LHSKnownZero & RHS->getValue()) == RHS->getValue()) { |
| 1519 | Instruction *Or = |
| 1520 | BinaryOperator::createOr(I->getOperand(0), I->getOperand(1), |
| 1521 | I->getName()); |
| 1522 | InsertNewInstBefore(Or, *I); |
| 1523 | return UpdateValueUsesWith(I, Or); |
| 1524 | } |
| 1525 | |
| 1526 | // We can say something about the output known-zero and known-one bits, |
| 1527 | // depending on potential carries from the input constant and the |
| 1528 | // unknowns. For example if the LHS is known to have at most the 0x0F0F0 |
| 1529 | // bits set and the RHS constant is 0x01001, then we know we have a known |
| 1530 | // one mask of 0x00001 and a known zero mask of 0xE0F0E. |
| 1531 | |
| 1532 | // To compute this, we first compute the potential carry bits. These are |
| 1533 | // the bits which may be modified. I'm not aware of a better way to do |
| 1534 | // this scan. |
Zhou Sheng | b9cb95f | 2007-03-31 02:38:39 +0000 | [diff] [blame] | 1535 | const APInt& RHSVal = RHS->getValue(); |
| 1536 | APInt CarryBits((~LHSKnownZero + RHSVal) ^ (~LHSKnownZero ^ RHSVal)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1537 | |
| 1538 | // Now that we know which bits have carries, compute the known-1/0 sets. |
| 1539 | |
| 1540 | // Bits are known one if they are known zero in one operand and one in the |
| 1541 | // other, and there is no input carry. |
| 1542 | RHSKnownOne = ((LHSKnownZero & RHSVal) | |
| 1543 | (LHSKnownOne & ~RHSVal)) & ~CarryBits; |
| 1544 | |
| 1545 | // Bits are known zero if they are known zero in both operands and there |
| 1546 | // is no input carry. |
| 1547 | RHSKnownZero = LHSKnownZero & ~RHSVal & ~CarryBits; |
| 1548 | } else { |
| 1549 | // If the high-bits of this ADD are not demanded, then it does not demand |
| 1550 | // the high bits of its LHS or RHS. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1551 | if (DemandedMask[BitWidth-1] == 0) { |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1552 | // Right fill the mask of bits for this ADD to demand the most |
| 1553 | // significant bit and all those below it. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1554 | APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1555 | if (SimplifyDemandedBits(I->getOperand(0), DemandedFromOps, |
| 1556 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1557 | return true; |
| 1558 | if (SimplifyDemandedBits(I->getOperand(1), DemandedFromOps, |
| 1559 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1560 | return true; |
| 1561 | } |
| 1562 | } |
| 1563 | break; |
| 1564 | } |
| 1565 | case Instruction::Sub: |
| 1566 | // If the high-bits of this SUB are not demanded, then it does not demand |
| 1567 | // the high bits of its LHS or RHS. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1568 | if (DemandedMask[BitWidth-1] == 0) { |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1569 | // Right fill the mask of bits for this SUB to demand the most |
| 1570 | // significant bit and all those below it. |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 1571 | uint32_t NLZ = DemandedMask.countLeadingZeros(); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1572 | APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1573 | if (SimplifyDemandedBits(I->getOperand(0), DemandedFromOps, |
| 1574 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1575 | return true; |
| 1576 | if (SimplifyDemandedBits(I->getOperand(1), DemandedFromOps, |
| 1577 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1578 | return true; |
| 1579 | } |
| 1580 | break; |
| 1581 | case Instruction::Shl: |
| 1582 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 1583 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1584 | APInt DemandedMaskIn(DemandedMask.lshr(ShiftAmt)); |
| 1585 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMaskIn, |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1586 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 1587 | return true; |
| 1588 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1589 | "Bits known to be one AND zero?"); |
| 1590 | RHSKnownZero <<= ShiftAmt; |
| 1591 | RHSKnownOne <<= ShiftAmt; |
| 1592 | // low bits known zero. |
Zhou Sheng | adc1495 | 2007-03-14 09:07:33 +0000 | [diff] [blame] | 1593 | if (ShiftAmt) |
Zhou Sheng | e9e03f6 | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 1594 | RHSKnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1595 | } |
| 1596 | break; |
| 1597 | case Instruction::LShr: |
| 1598 | // For a logical shift right |
| 1599 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 1600 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1601 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1602 | // Unsigned shift right. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1603 | APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt)); |
| 1604 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMaskIn, |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1605 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 1606 | return true; |
| 1607 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1608 | "Bits known to be one AND zero?"); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1609 | RHSKnownZero = APIntOps::lshr(RHSKnownZero, ShiftAmt); |
| 1610 | RHSKnownOne = APIntOps::lshr(RHSKnownOne, ShiftAmt); |
Zhou Sheng | adc1495 | 2007-03-14 09:07:33 +0000 | [diff] [blame] | 1611 | if (ShiftAmt) { |
| 1612 | // Compute the new bits that are at the top now. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1613 | APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt)); |
Zhou Sheng | adc1495 | 2007-03-14 09:07:33 +0000 | [diff] [blame] | 1614 | RHSKnownZero |= HighBits; // high bits known zero. |
| 1615 | } |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1616 | } |
| 1617 | break; |
| 1618 | case Instruction::AShr: |
| 1619 | // If this is an arithmetic shift right and only the low-bit is set, we can |
| 1620 | // always convert this into a logical shr, even if the shift amount is |
| 1621 | // variable. The low bit of the shift cannot be an input sign bit unless |
| 1622 | // the shift amount is >= the size of the datatype, which is undefined. |
| 1623 | if (DemandedMask == 1) { |
| 1624 | // Perform the logical shift right. |
| 1625 | Value *NewVal = BinaryOperator::createLShr( |
| 1626 | I->getOperand(0), I->getOperand(1), I->getName()); |
| 1627 | InsertNewInstBefore(cast<Instruction>(NewVal), *I); |
| 1628 | return UpdateValueUsesWith(I, NewVal); |
| 1629 | } |
Chris Lattner | 4241e4d | 2007-07-15 20:54:51 +0000 | [diff] [blame] | 1630 | |
| 1631 | // If the sign bit is the only bit demanded by this ashr, then there is no |
| 1632 | // need to do it, the shift doesn't change the high bit. |
| 1633 | if (DemandedMask.isSignBit()) |
| 1634 | return UpdateValueUsesWith(I, I->getOperand(0)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1635 | |
| 1636 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 1637 | uint32_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1638 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1639 | // Signed shift right. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1640 | APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt)); |
Lauro Ramos Venancio | d0499af | 2007-06-06 17:08:48 +0000 | [diff] [blame] | 1641 | // If any of the "high bits" are demanded, we should set the sign bit as |
| 1642 | // demanded. |
| 1643 | if (DemandedMask.countLeadingZeros() <= ShiftAmt) |
| 1644 | DemandedMaskIn.set(BitWidth-1); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1645 | if (SimplifyDemandedBits(I->getOperand(0), |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1646 | DemandedMaskIn, |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1647 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 1648 | return true; |
| 1649 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1650 | "Bits known to be one AND zero?"); |
| 1651 | // Compute the new bits that are at the top now. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1652 | APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1653 | RHSKnownZero = APIntOps::lshr(RHSKnownZero, ShiftAmt); |
| 1654 | RHSKnownOne = APIntOps::lshr(RHSKnownOne, ShiftAmt); |
| 1655 | |
| 1656 | // Handle the sign bits. |
| 1657 | APInt SignBit(APInt::getSignBit(BitWidth)); |
| 1658 | // Adjust to where it is now in the mask. |
| 1659 | SignBit = APIntOps::lshr(SignBit, ShiftAmt); |
| 1660 | |
| 1661 | // If the input sign bit is known to be zero, or if none of the top bits |
| 1662 | // are demanded, turn this into an unsigned shift right. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1663 | if (RHSKnownZero[BitWidth-ShiftAmt-1] || |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1664 | (HighBits & ~DemandedMask) == HighBits) { |
| 1665 | // Perform the logical shift right. |
| 1666 | Value *NewVal = BinaryOperator::createLShr( |
| 1667 | I->getOperand(0), SA, I->getName()); |
| 1668 | InsertNewInstBefore(cast<Instruction>(NewVal), *I); |
| 1669 | return UpdateValueUsesWith(I, NewVal); |
| 1670 | } else if ((RHSKnownOne & SignBit) != 0) { // New bits are known one. |
| 1671 | RHSKnownOne |= HighBits; |
| 1672 | } |
| 1673 | } |
| 1674 | break; |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 1675 | case Instruction::SRem: |
| 1676 | if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 1677 | APInt RA = Rem->getValue(); |
| 1678 | if (RA.isPowerOf2() || (-RA).isPowerOf2()) { |
| 1679 | APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) | RA : ~RA; |
| 1680 | APInt Mask2 = LowBits | APInt::getSignBit(BitWidth); |
| 1681 | if (SimplifyDemandedBits(I->getOperand(0), Mask2, |
| 1682 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1683 | return true; |
| 1684 | |
| 1685 | if (LHSKnownZero[BitWidth-1] || ((LHSKnownZero & LowBits) == LowBits)) |
| 1686 | LHSKnownZero |= ~LowBits; |
| 1687 | else if (LHSKnownOne[BitWidth-1]) |
| 1688 | LHSKnownOne |= ~LowBits; |
| 1689 | |
| 1690 | KnownZero |= LHSKnownZero & DemandedMask; |
| 1691 | KnownOne |= LHSKnownOne & DemandedMask; |
| 1692 | |
| 1693 | assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); |
| 1694 | } |
| 1695 | } |
| 1696 | break; |
| 1697 | case Instruction::URem: |
| 1698 | if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 1699 | APInt RA = Rem->getValue(); |
| 1700 | if (RA.isPowerOf2()) { |
| 1701 | APInt LowBits = (RA - 1) | RA; |
| 1702 | APInt Mask2 = LowBits & DemandedMask; |
| 1703 | KnownZero |= ~LowBits & DemandedMask; |
| 1704 | if (SimplifyDemandedBits(I->getOperand(0), Mask2, |
| 1705 | KnownZero, KnownOne, Depth+1)) |
| 1706 | return true; |
| 1707 | |
| 1708 | assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); |
| 1709 | } |
| 1710 | } else { |
| 1711 | APInt KnownZero2(BitWidth, 0), KnownOne2(BitWidth, 0); |
| 1712 | APInt AllOnes = APInt::getAllOnesValue(BitWidth); |
| 1713 | if (SimplifyDemandedBits(I->getOperand(1), AllOnes, |
| 1714 | KnownZero2, KnownOne2, Depth+1)) |
| 1715 | return true; |
| 1716 | |
| 1717 | uint32_t Leaders = KnownZero2.countLeadingOnes(); |
| 1718 | KnownZero |= APInt::getHighBitsSet(BitWidth, Leaders) & DemandedMask; |
| 1719 | } |
| 1720 | break; |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1721 | } |
| 1722 | |
| 1723 | // If the client is only demanding bits that we know, return the known |
| 1724 | // constant. |
| 1725 | if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) |
| 1726 | return UpdateValueUsesWith(I, ConstantInt::get(RHSKnownOne)); |
| 1727 | return false; |
| 1728 | } |
| 1729 | |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1730 | |
| 1731 | /// SimplifyDemandedVectorElts - The specified value producecs a vector with |
| 1732 | /// 64 or fewer elements. DemandedElts contains the set of elements that are |
| 1733 | /// actually used by the caller. This method analyzes which elements of the |
| 1734 | /// operand are undef and returns that information in UndefElts. |
| 1735 | /// |
| 1736 | /// If the information about demanded elements can be used to simplify the |
| 1737 | /// operation, the operation is simplified, then the resultant value is |
| 1738 | /// returned. This returns null if no change was made. |
| 1739 | Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, uint64_t DemandedElts, |
| 1740 | uint64_t &UndefElts, |
| 1741 | unsigned Depth) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1742 | unsigned VWidth = cast<VectorType>(V->getType())->getNumElements(); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1743 | assert(VWidth <= 64 && "Vector too wide to analyze!"); |
| 1744 | uint64_t EltMask = ~0ULL >> (64-VWidth); |
| 1745 | assert(DemandedElts != EltMask && (DemandedElts & ~EltMask) == 0 && |
| 1746 | "Invalid DemandedElts!"); |
| 1747 | |
| 1748 | if (isa<UndefValue>(V)) { |
| 1749 | // If the entire vector is undefined, just return this info. |
| 1750 | UndefElts = EltMask; |
| 1751 | return 0; |
| 1752 | } else if (DemandedElts == 0) { // If nothing is demanded, provide undef. |
| 1753 | UndefElts = EltMask; |
| 1754 | return UndefValue::get(V->getType()); |
| 1755 | } |
| 1756 | |
| 1757 | UndefElts = 0; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1758 | if (ConstantVector *CP = dyn_cast<ConstantVector>(V)) { |
| 1759 | const Type *EltTy = cast<VectorType>(V->getType())->getElementType(); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1760 | Constant *Undef = UndefValue::get(EltTy); |
| 1761 | |
| 1762 | std::vector<Constant*> Elts; |
| 1763 | for (unsigned i = 0; i != VWidth; ++i) |
| 1764 | if (!(DemandedElts & (1ULL << i))) { // If not demanded, set to undef. |
| 1765 | Elts.push_back(Undef); |
| 1766 | UndefElts |= (1ULL << i); |
| 1767 | } else if (isa<UndefValue>(CP->getOperand(i))) { // Already undef. |
| 1768 | Elts.push_back(Undef); |
| 1769 | UndefElts |= (1ULL << i); |
| 1770 | } else { // Otherwise, defined. |
| 1771 | Elts.push_back(CP->getOperand(i)); |
| 1772 | } |
| 1773 | |
| 1774 | // If we changed the constant, return it. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1775 | Constant *NewCP = ConstantVector::get(Elts); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1776 | return NewCP != CP ? NewCP : 0; |
| 1777 | } else if (isa<ConstantAggregateZero>(V)) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1778 | // Simplify the CAZ to a ConstantVector where the non-demanded elements are |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1779 | // set to undef. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1780 | const Type *EltTy = cast<VectorType>(V->getType())->getElementType(); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1781 | Constant *Zero = Constant::getNullValue(EltTy); |
| 1782 | Constant *Undef = UndefValue::get(EltTy); |
| 1783 | std::vector<Constant*> Elts; |
| 1784 | for (unsigned i = 0; i != VWidth; ++i) |
| 1785 | Elts.push_back((DemandedElts & (1ULL << i)) ? Zero : Undef); |
| 1786 | UndefElts = DemandedElts ^ EltMask; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1787 | return ConstantVector::get(Elts); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1788 | } |
| 1789 | |
| 1790 | if (!V->hasOneUse()) { // Other users may use these bits. |
| 1791 | if (Depth != 0) { // Not at the root. |
| 1792 | // TODO: Just compute the UndefElts information recursively. |
| 1793 | return false; |
| 1794 | } |
| 1795 | return false; |
| 1796 | } else if (Depth == 10) { // Limit search depth. |
| 1797 | return false; |
| 1798 | } |
| 1799 | |
| 1800 | Instruction *I = dyn_cast<Instruction>(V); |
| 1801 | if (!I) return false; // Only analyze instructions. |
| 1802 | |
| 1803 | bool MadeChange = false; |
| 1804 | uint64_t UndefElts2; |
| 1805 | Value *TmpV; |
| 1806 | switch (I->getOpcode()) { |
| 1807 | default: break; |
| 1808 | |
| 1809 | case Instruction::InsertElement: { |
| 1810 | // If this is a variable index, we don't know which element it overwrites. |
| 1811 | // demand exactly the same input as we produce. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1812 | ConstantInt *Idx = dyn_cast<ConstantInt>(I->getOperand(2)); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1813 | if (Idx == 0) { |
| 1814 | // Note that we can't propagate undef elt info, because we don't know |
| 1815 | // which elt is getting updated. |
| 1816 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, |
| 1817 | UndefElts2, Depth+1); |
| 1818 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } |
| 1819 | break; |
| 1820 | } |
| 1821 | |
| 1822 | // If this is inserting an element that isn't demanded, remove this |
| 1823 | // insertelement. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1824 | unsigned IdxNo = Idx->getZExtValue(); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1825 | if (IdxNo >= VWidth || (DemandedElts & (1ULL << IdxNo)) == 0) |
| 1826 | return AddSoonDeadInstToWorklist(*I, 0); |
| 1827 | |
| 1828 | // Otherwise, the element inserted overwrites whatever was there, so the |
| 1829 | // input demanded set is simpler than the output set. |
| 1830 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), |
| 1831 | DemandedElts & ~(1ULL << IdxNo), |
| 1832 | UndefElts, Depth+1); |
| 1833 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } |
| 1834 | |
| 1835 | // The inserted element is defined. |
| 1836 | UndefElts |= 1ULL << IdxNo; |
| 1837 | break; |
| 1838 | } |
Chris Lattner | 6987833 | 2007-04-14 22:29:23 +0000 | [diff] [blame] | 1839 | case Instruction::BitCast: { |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 1840 | // Vector->vector casts only. |
Chris Lattner | 6987833 | 2007-04-14 22:29:23 +0000 | [diff] [blame] | 1841 | const VectorType *VTy = dyn_cast<VectorType>(I->getOperand(0)->getType()); |
| 1842 | if (!VTy) break; |
| 1843 | unsigned InVWidth = VTy->getNumElements(); |
| 1844 | uint64_t InputDemandedElts = 0; |
| 1845 | unsigned Ratio; |
| 1846 | |
| 1847 | if (VWidth == InVWidth) { |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 1848 | // 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] | 1849 | // elements as are demanded of us. |
| 1850 | Ratio = 1; |
| 1851 | InputDemandedElts = DemandedElts; |
| 1852 | } else if (VWidth > InVWidth) { |
| 1853 | // Untested so far. |
| 1854 | break; |
| 1855 | |
| 1856 | // If there are more elements in the result than there are in the source, |
| 1857 | // then an input element is live if any of the corresponding output |
| 1858 | // elements are live. |
| 1859 | Ratio = VWidth/InVWidth; |
| 1860 | for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) { |
| 1861 | if (DemandedElts & (1ULL << OutIdx)) |
| 1862 | InputDemandedElts |= 1ULL << (OutIdx/Ratio); |
| 1863 | } |
| 1864 | } else { |
| 1865 | // Untested so far. |
| 1866 | break; |
| 1867 | |
| 1868 | // If there are more elements in the source than there are in the result, |
| 1869 | // then an input element is live if the corresponding output element is |
| 1870 | // live. |
| 1871 | Ratio = InVWidth/VWidth; |
| 1872 | for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx) |
| 1873 | if (DemandedElts & (1ULL << InIdx/Ratio)) |
| 1874 | InputDemandedElts |= 1ULL << InIdx; |
| 1875 | } |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1876 | |
Chris Lattner | 6987833 | 2007-04-14 22:29:23 +0000 | [diff] [blame] | 1877 | // div/rem demand all inputs, because they don't want divide by zero. |
| 1878 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), InputDemandedElts, |
| 1879 | UndefElts2, Depth+1); |
| 1880 | if (TmpV) { |
| 1881 | I->setOperand(0, TmpV); |
| 1882 | MadeChange = true; |
| 1883 | } |
| 1884 | |
| 1885 | UndefElts = UndefElts2; |
| 1886 | if (VWidth > InVWidth) { |
| 1887 | assert(0 && "Unimp"); |
| 1888 | // If there are more elements in the result than there are in the source, |
| 1889 | // then an output element is undef if the corresponding input element is |
| 1890 | // undef. |
| 1891 | for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) |
| 1892 | if (UndefElts2 & (1ULL << (OutIdx/Ratio))) |
| 1893 | UndefElts |= 1ULL << OutIdx; |
| 1894 | } else if (VWidth < InVWidth) { |
| 1895 | assert(0 && "Unimp"); |
| 1896 | // If there are more elements in the source than there are in the result, |
| 1897 | // then a result element is undef if all of the corresponding input |
| 1898 | // elements are undef. |
| 1899 | UndefElts = ~0ULL >> (64-VWidth); // Start out all undef. |
| 1900 | for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx) |
| 1901 | if ((UndefElts2 & (1ULL << InIdx)) == 0) // Not undef? |
| 1902 | UndefElts &= ~(1ULL << (InIdx/Ratio)); // Clear undef bit. |
| 1903 | } |
| 1904 | break; |
| 1905 | } |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1906 | case Instruction::And: |
| 1907 | case Instruction::Or: |
| 1908 | case Instruction::Xor: |
| 1909 | case Instruction::Add: |
| 1910 | case Instruction::Sub: |
| 1911 | case Instruction::Mul: |
| 1912 | // div/rem demand all inputs, because they don't want divide by zero. |
| 1913 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, |
| 1914 | UndefElts, Depth+1); |
| 1915 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } |
| 1916 | TmpV = SimplifyDemandedVectorElts(I->getOperand(1), DemandedElts, |
| 1917 | UndefElts2, Depth+1); |
| 1918 | if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; } |
| 1919 | |
| 1920 | // Output elements are undefined if both are undefined. Consider things |
| 1921 | // like undef&0. The result is known zero, not undef. |
| 1922 | UndefElts &= UndefElts2; |
| 1923 | break; |
| 1924 | |
| 1925 | case Instruction::Call: { |
| 1926 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(I); |
| 1927 | if (!II) break; |
| 1928 | switch (II->getIntrinsicID()) { |
| 1929 | default: break; |
| 1930 | |
| 1931 | // Binary vector operations that work column-wise. A dest element is a |
| 1932 | // function of the corresponding input elements from the two inputs. |
| 1933 | case Intrinsic::x86_sse_sub_ss: |
| 1934 | case Intrinsic::x86_sse_mul_ss: |
| 1935 | case Intrinsic::x86_sse_min_ss: |
| 1936 | case Intrinsic::x86_sse_max_ss: |
| 1937 | case Intrinsic::x86_sse2_sub_sd: |
| 1938 | case Intrinsic::x86_sse2_mul_sd: |
| 1939 | case Intrinsic::x86_sse2_min_sd: |
| 1940 | case Intrinsic::x86_sse2_max_sd: |
| 1941 | TmpV = SimplifyDemandedVectorElts(II->getOperand(1), DemandedElts, |
| 1942 | UndefElts, Depth+1); |
| 1943 | if (TmpV) { II->setOperand(1, TmpV); MadeChange = true; } |
| 1944 | TmpV = SimplifyDemandedVectorElts(II->getOperand(2), DemandedElts, |
| 1945 | UndefElts2, Depth+1); |
| 1946 | if (TmpV) { II->setOperand(2, TmpV); MadeChange = true; } |
| 1947 | |
| 1948 | // If only the low elt is demanded and this is a scalarizable intrinsic, |
| 1949 | // scalarize it now. |
| 1950 | if (DemandedElts == 1) { |
| 1951 | switch (II->getIntrinsicID()) { |
| 1952 | default: break; |
| 1953 | case Intrinsic::x86_sse_sub_ss: |
| 1954 | case Intrinsic::x86_sse_mul_ss: |
| 1955 | case Intrinsic::x86_sse2_sub_sd: |
| 1956 | case Intrinsic::x86_sse2_mul_sd: |
| 1957 | // TODO: Lower MIN/MAX/ABS/etc |
| 1958 | Value *LHS = II->getOperand(1); |
| 1959 | Value *RHS = II->getOperand(2); |
| 1960 | // Extract the element as scalars. |
| 1961 | LHS = InsertNewInstBefore(new ExtractElementInst(LHS, 0U,"tmp"), *II); |
| 1962 | RHS = InsertNewInstBefore(new ExtractElementInst(RHS, 0U,"tmp"), *II); |
| 1963 | |
| 1964 | switch (II->getIntrinsicID()) { |
| 1965 | default: assert(0 && "Case stmts out of sync!"); |
| 1966 | case Intrinsic::x86_sse_sub_ss: |
| 1967 | case Intrinsic::x86_sse2_sub_sd: |
| 1968 | TmpV = InsertNewInstBefore(BinaryOperator::createSub(LHS, RHS, |
| 1969 | II->getName()), *II); |
| 1970 | break; |
| 1971 | case Intrinsic::x86_sse_mul_ss: |
| 1972 | case Intrinsic::x86_sse2_mul_sd: |
| 1973 | TmpV = InsertNewInstBefore(BinaryOperator::createMul(LHS, RHS, |
| 1974 | II->getName()), *II); |
| 1975 | break; |
| 1976 | } |
| 1977 | |
| 1978 | Instruction *New = |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 1979 | InsertElementInst::Create(UndefValue::get(II->getType()), TmpV, 0U, |
| 1980 | II->getName()); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1981 | InsertNewInstBefore(New, *II); |
| 1982 | AddSoonDeadInstToWorklist(*II, 0); |
| 1983 | return New; |
| 1984 | } |
| 1985 | } |
| 1986 | |
| 1987 | // Output elements are undefined if both are undefined. Consider things |
| 1988 | // like undef&0. The result is known zero, not undef. |
| 1989 | UndefElts &= UndefElts2; |
| 1990 | break; |
| 1991 | } |
| 1992 | break; |
| 1993 | } |
| 1994 | } |
| 1995 | return MadeChange ? I : 0; |
| 1996 | } |
| 1997 | |
Nick Lewycky | 455e176 | 2007-09-06 02:40:25 +0000 | [diff] [blame] | 1998 | /// @returns true if the specified compare predicate is |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1999 | /// true when both operands are equal... |
Nick Lewycky | 455e176 | 2007-09-06 02:40:25 +0000 | [diff] [blame] | 2000 | /// @brief Determine if the icmp Predicate is true when both operands are equal |
| 2001 | static bool isTrueWhenEqual(ICmpInst::Predicate pred) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2002 | return pred == ICmpInst::ICMP_EQ || pred == ICmpInst::ICMP_UGE || |
| 2003 | pred == ICmpInst::ICMP_SGE || pred == ICmpInst::ICMP_ULE || |
| 2004 | pred == ICmpInst::ICMP_SLE; |
| 2005 | } |
| 2006 | |
Nick Lewycky | 455e176 | 2007-09-06 02:40:25 +0000 | [diff] [blame] | 2007 | /// @returns true if the specified compare instruction is |
| 2008 | /// true when both operands are equal... |
| 2009 | /// @brief Determine if the ICmpInst returns true when both operands are equal |
| 2010 | static bool isTrueWhenEqual(ICmpInst &ICI) { |
| 2011 | return isTrueWhenEqual(ICI.getPredicate()); |
| 2012 | } |
| 2013 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2014 | /// AssociativeOpt - Perform an optimization on an associative operator. This |
| 2015 | /// function is designed to check a chain of associative operators for a |
| 2016 | /// potential to apply a certain optimization. Since the optimization may be |
| 2017 | /// applicable if the expression was reassociated, this checks the chain, then |
| 2018 | /// reassociates the expression as necessary to expose the optimization |
| 2019 | /// opportunity. This makes use of a special Functor, which must define |
| 2020 | /// 'shouldApply' and 'apply' methods. |
| 2021 | /// |
| 2022 | template<typename Functor> |
| 2023 | Instruction *AssociativeOpt(BinaryOperator &Root, const Functor &F) { |
| 2024 | unsigned Opcode = Root.getOpcode(); |
| 2025 | Value *LHS = Root.getOperand(0); |
| 2026 | |
| 2027 | // Quick check, see if the immediate LHS matches... |
| 2028 | if (F.shouldApply(LHS)) |
| 2029 | return F.apply(Root); |
| 2030 | |
| 2031 | // Otherwise, if the LHS is not of the same opcode as the root, return. |
| 2032 | Instruction *LHSI = dyn_cast<Instruction>(LHS); |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 2033 | while (LHSI && LHSI->getOpcode() == Opcode && LHSI->hasOneUse()) { |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2034 | // Should we apply this transform to the RHS? |
| 2035 | bool ShouldApply = F.shouldApply(LHSI->getOperand(1)); |
| 2036 | |
| 2037 | // If not to the RHS, check to see if we should apply to the LHS... |
| 2038 | if (!ShouldApply && F.shouldApply(LHSI->getOperand(0))) { |
| 2039 | cast<BinaryOperator>(LHSI)->swapOperands(); // Make the LHS the RHS |
| 2040 | ShouldApply = true; |
| 2041 | } |
| 2042 | |
| 2043 | // If the functor wants to apply the optimization to the RHS of LHSI, |
| 2044 | // reassociate the expression from ((? op A) op B) to (? op (A op B)) |
| 2045 | if (ShouldApply) { |
| 2046 | BasicBlock *BB = Root.getParent(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2047 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2048 | // Now all of the instructions are in the current basic block, go ahead |
| 2049 | // and perform the reassociation. |
| 2050 | Instruction *TmpLHSI = cast<Instruction>(Root.getOperand(0)); |
| 2051 | |
| 2052 | // First move the selected RHS to the LHS of the root... |
| 2053 | Root.setOperand(0, LHSI->getOperand(1)); |
| 2054 | |
| 2055 | // Make what used to be the LHS of the root be the user of the root... |
| 2056 | Value *ExtraOperand = TmpLHSI->getOperand(1); |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 2057 | if (&Root == TmpLHSI) { |
Chris Lattner | 15a76c0 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 2058 | Root.replaceAllUsesWith(Constant::getNullValue(TmpLHSI->getType())); |
| 2059 | return 0; |
| 2060 | } |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 2061 | Root.replaceAllUsesWith(TmpLHSI); // Users now use TmpLHSI |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2062 | TmpLHSI->setOperand(1, &Root); // TmpLHSI now uses the root |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 2063 | TmpLHSI->getParent()->getInstList().remove(TmpLHSI); |
| 2064 | BasicBlock::iterator ARI = &Root; ++ARI; |
| 2065 | BB->getInstList().insert(ARI, TmpLHSI); // Move TmpLHSI to after Root |
| 2066 | ARI = Root; |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2067 | |
| 2068 | // Now propagate the ExtraOperand down the chain of instructions until we |
| 2069 | // get to LHSI. |
| 2070 | while (TmpLHSI != LHSI) { |
| 2071 | Instruction *NextLHSI = cast<Instruction>(TmpLHSI->getOperand(0)); |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 2072 | // Move the instruction to immediately before the chain we are |
| 2073 | // constructing to avoid breaking dominance properties. |
| 2074 | NextLHSI->getParent()->getInstList().remove(NextLHSI); |
| 2075 | BB->getInstList().insert(ARI, NextLHSI); |
| 2076 | ARI = NextLHSI; |
| 2077 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2078 | Value *NextOp = NextLHSI->getOperand(1); |
| 2079 | NextLHSI->setOperand(1, ExtraOperand); |
| 2080 | TmpLHSI = NextLHSI; |
| 2081 | ExtraOperand = NextOp; |
| 2082 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2083 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2084 | // Now that the instructions are reassociated, have the functor perform |
| 2085 | // the transformation... |
| 2086 | return F.apply(Root); |
| 2087 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2088 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2089 | LHSI = dyn_cast<Instruction>(LHSI->getOperand(0)); |
| 2090 | } |
| 2091 | return 0; |
| 2092 | } |
| 2093 | |
| 2094 | |
| 2095 | // AddRHS - Implements: X + X --> X << 1 |
| 2096 | struct AddRHS { |
| 2097 | Value *RHS; |
| 2098 | AddRHS(Value *rhs) : RHS(rhs) {} |
| 2099 | bool shouldApply(Value *LHS) const { return LHS == RHS; } |
| 2100 | Instruction *apply(BinaryOperator &Add) const { |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 2101 | return BinaryOperator::createShl(Add.getOperand(0), |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2102 | ConstantInt::get(Add.getType(), 1)); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2103 | } |
| 2104 | }; |
| 2105 | |
| 2106 | // AddMaskingAnd - Implements (A & C1)+(B & C2) --> (A & C1)|(B & C2) |
| 2107 | // iff C1&C2 == 0 |
| 2108 | struct AddMaskingAnd { |
| 2109 | Constant *C2; |
| 2110 | AddMaskingAnd(Constant *c) : C2(c) {} |
| 2111 | bool shouldApply(Value *LHS) const { |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2112 | ConstantInt *C1; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2113 | return match(LHS, m_And(m_Value(), m_ConstantInt(C1))) && |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2114 | ConstantExpr::getAnd(C1, C2)->isNullValue(); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2115 | } |
| 2116 | Instruction *apply(BinaryOperator &Add) const { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2117 | return BinaryOperator::createOr(Add.getOperand(0), Add.getOperand(1)); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2118 | } |
| 2119 | }; |
| 2120 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2121 | static Value *FoldOperationIntoSelectOperand(Instruction &I, Value *SO, |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2122 | InstCombiner *IC) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2123 | if (CastInst *CI = dyn_cast<CastInst>(&I)) { |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2124 | if (Constant *SOC = dyn_cast<Constant>(SO)) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2125 | return ConstantExpr::getCast(CI->getOpcode(), SOC, I.getType()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2126 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2127 | return IC->InsertNewInstBefore(CastInst::create( |
| 2128 | CI->getOpcode(), SO, I.getType(), SO->getName() + ".cast"), I); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2129 | } |
| 2130 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2131 | // Figure out if the constant is the left or the right argument. |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2132 | bool ConstIsRHS = isa<Constant>(I.getOperand(1)); |
| 2133 | Constant *ConstOperand = cast<Constant>(I.getOperand(ConstIsRHS)); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2134 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2135 | if (Constant *SOC = dyn_cast<Constant>(SO)) { |
| 2136 | if (ConstIsRHS) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2137 | return ConstantExpr::get(I.getOpcode(), SOC, ConstOperand); |
| 2138 | return ConstantExpr::get(I.getOpcode(), ConstOperand, SOC); |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2139 | } |
| 2140 | |
| 2141 | Value *Op0 = SO, *Op1 = ConstOperand; |
| 2142 | if (!ConstIsRHS) |
| 2143 | std::swap(Op0, Op1); |
| 2144 | Instruction *New; |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2145 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I)) |
| 2146 | New = BinaryOperator::create(BO->getOpcode(), Op0, Op1,SO->getName()+".op"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2147 | else if (CmpInst *CI = dyn_cast<CmpInst>(&I)) |
| 2148 | New = CmpInst::create(CI->getOpcode(), CI->getPredicate(), Op0, Op1, |
| 2149 | SO->getName()+".cmp"); |
Chris Lattner | 326c0f3 | 2004-04-10 19:15:56 +0000 | [diff] [blame] | 2150 | else { |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2151 | assert(0 && "Unknown binary instruction type!"); |
Chris Lattner | 326c0f3 | 2004-04-10 19:15:56 +0000 | [diff] [blame] | 2152 | abort(); |
| 2153 | } |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2154 | return IC->InsertNewInstBefore(New, I); |
| 2155 | } |
| 2156 | |
| 2157 | // FoldOpIntoSelect - Given an instruction with a select as one operand and a |
| 2158 | // constant as the other operand, try to fold the binary operator into the |
| 2159 | // select arguments. This also works for Cast instructions, which obviously do |
| 2160 | // not have a second operand. |
| 2161 | static Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI, |
| 2162 | InstCombiner *IC) { |
| 2163 | // Don't modify shared select instructions |
| 2164 | if (!SI->hasOneUse()) return 0; |
| 2165 | Value *TV = SI->getOperand(1); |
| 2166 | Value *FV = SI->getOperand(2); |
| 2167 | |
| 2168 | if (isa<Constant>(TV) || isa<Constant>(FV)) { |
Chris Lattner | 956db27 | 2005-04-21 05:43:13 +0000 | [diff] [blame] | 2169 | // Bool selects with constant operands can be folded to logical ops. |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 2170 | if (SI->getType() == Type::Int1Ty) return 0; |
Chris Lattner | 956db27 | 2005-04-21 05:43:13 +0000 | [diff] [blame] | 2171 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2172 | Value *SelectTrueVal = FoldOperationIntoSelectOperand(Op, TV, IC); |
| 2173 | Value *SelectFalseVal = FoldOperationIntoSelectOperand(Op, FV, IC); |
| 2174 | |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 2175 | return SelectInst::Create(SI->getCondition(), SelectTrueVal, |
| 2176 | SelectFalseVal); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2177 | } |
| 2178 | return 0; |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2179 | } |
| 2180 | |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2181 | |
| 2182 | /// FoldOpIntoPhi - Given a binary operator or cast instruction which has a PHI |
| 2183 | /// node as operand #0, see if we can fold the instruction into the PHI (which |
| 2184 | /// is only possible if all operands to the PHI are constants). |
| 2185 | Instruction *InstCombiner::FoldOpIntoPhi(Instruction &I) { |
| 2186 | PHINode *PN = cast<PHINode>(I.getOperand(0)); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 2187 | unsigned NumPHIValues = PN->getNumIncomingValues(); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 2188 | if (!PN->hasOneUse() || NumPHIValues == 0) return 0; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2189 | |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 2190 | // Check to see if all of the operands of the PHI are constants. If there is |
| 2191 | // 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] | 2192 | // or if *it* is a PHI, bail out. |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 2193 | BasicBlock *NonConstBB = 0; |
| 2194 | for (unsigned i = 0; i != NumPHIValues; ++i) |
| 2195 | if (!isa<Constant>(PN->getIncomingValue(i))) { |
| 2196 | if (NonConstBB) return 0; // More than one non-const value. |
Chris Lattner | b303668 | 2007-02-24 01:03:45 +0000 | [diff] [blame] | 2197 | if (isa<PHINode>(PN->getIncomingValue(i))) return 0; // Itself a phi. |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 2198 | NonConstBB = PN->getIncomingBlock(i); |
| 2199 | |
| 2200 | // If the incoming non-constant value is in I's block, we have an infinite |
| 2201 | // loop. |
| 2202 | if (NonConstBB == I.getParent()) |
| 2203 | return 0; |
| 2204 | } |
| 2205 | |
| 2206 | // If there is exactly one non-constant value, we can insert a copy of the |
| 2207 | // operation in that block. However, if this is a critical edge, we would be |
| 2208 | // inserting the computation one some other paths (e.g. inside a loop). Only |
| 2209 | // do this if the pred block is unconditionally branching into the phi block. |
| 2210 | if (NonConstBB) { |
| 2211 | BranchInst *BI = dyn_cast<BranchInst>(NonConstBB->getTerminator()); |
| 2212 | if (!BI || !BI->isUnconditional()) return 0; |
| 2213 | } |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2214 | |
| 2215 | // Okay, we can do the transformation: create the new PHI node. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 2216 | PHINode *NewPN = PHINode::Create(I.getType(), ""); |
Chris Lattner | 5551706 | 2005-01-29 00:39:08 +0000 | [diff] [blame] | 2217 | NewPN->reserveOperandSpace(PN->getNumOperands()/2); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2218 | InsertNewInstBefore(NewPN, *PN); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 2219 | NewPN->takeName(PN); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2220 | |
| 2221 | // Next, add all of the operands to the PHI. |
| 2222 | if (I.getNumOperands() == 2) { |
| 2223 | Constant *C = cast<Constant>(I.getOperand(1)); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 2224 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
Chris Lattner | a9ff5eb | 2007-08-05 08:47:58 +0000 | [diff] [blame] | 2225 | Value *InV = 0; |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 2226 | if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2227 | if (CmpInst *CI = dyn_cast<CmpInst>(&I)) |
| 2228 | InV = ConstantExpr::getCompare(CI->getPredicate(), InC, C); |
| 2229 | else |
| 2230 | InV = ConstantExpr::get(I.getOpcode(), InC, C); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 2231 | } else { |
| 2232 | assert(PN->getIncomingBlock(i) == NonConstBB); |
| 2233 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I)) |
| 2234 | InV = BinaryOperator::create(BO->getOpcode(), |
| 2235 | PN->getIncomingValue(i), C, "phitmp", |
| 2236 | NonConstBB->getTerminator()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2237 | else if (CmpInst *CI = dyn_cast<CmpInst>(&I)) |
| 2238 | InV = CmpInst::create(CI->getOpcode(), |
| 2239 | CI->getPredicate(), |
| 2240 | PN->getIncomingValue(i), C, "phitmp", |
| 2241 | NonConstBB->getTerminator()); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 2242 | else |
| 2243 | assert(0 && "Unknown binop!"); |
| 2244 | |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 2245 | AddToWorkList(cast<Instruction>(InV)); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 2246 | } |
| 2247 | NewPN->addIncoming(InV, PN->getIncomingBlock(i)); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2248 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2249 | } else { |
| 2250 | CastInst *CI = cast<CastInst>(&I); |
| 2251 | const Type *RetTy = CI->getType(); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 2252 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 2253 | Value *InV; |
| 2254 | if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2255 | InV = ConstantExpr::getCast(CI->getOpcode(), InC, RetTy); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 2256 | } else { |
| 2257 | assert(PN->getIncomingBlock(i) == NonConstBB); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2258 | InV = CastInst::create(CI->getOpcode(), PN->getIncomingValue(i), |
| 2259 | I.getType(), "phitmp", |
| 2260 | NonConstBB->getTerminator()); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 2261 | AddToWorkList(cast<Instruction>(InV)); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 2262 | } |
| 2263 | NewPN->addIncoming(InV, PN->getIncomingBlock(i)); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2264 | } |
| 2265 | } |
| 2266 | return ReplaceInstUsesWith(I, NewPN); |
| 2267 | } |
| 2268 | |
Chris Lattner | 2454a2e | 2008-01-29 06:52:45 +0000 | [diff] [blame] | 2269 | |
| 2270 | /// CannotBeNegativeZero - Return true if we can prove that the specified FP |
| 2271 | /// value is never equal to -0.0. |
| 2272 | /// |
| 2273 | /// Note that this function will need to be revisited when we support nondefault |
| 2274 | /// rounding modes! |
| 2275 | /// |
| 2276 | static bool CannotBeNegativeZero(const Value *V) { |
| 2277 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V)) |
| 2278 | return !CFP->getValueAPF().isNegZero(); |
| 2279 | |
| 2280 | // (add x, 0.0) is guaranteed to return +0.0, not -0.0. |
| 2281 | if (const Instruction *I = dyn_cast<Instruction>(V)) { |
| 2282 | if (I->getOpcode() == Instruction::Add && |
| 2283 | isa<ConstantFP>(I->getOperand(1)) && |
| 2284 | cast<ConstantFP>(I->getOperand(1))->isNullValue()) |
| 2285 | return true; |
| 2286 | |
| 2287 | if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) |
| 2288 | if (II->getIntrinsicID() == Intrinsic::sqrt) |
| 2289 | return CannotBeNegativeZero(II->getOperand(1)); |
| 2290 | |
| 2291 | if (const CallInst *CI = dyn_cast<CallInst>(I)) |
| 2292 | if (const Function *F = CI->getCalledFunction()) { |
| 2293 | if (F->isDeclaration()) { |
| 2294 | switch (F->getNameLen()) { |
| 2295 | case 3: // abs(x) != -0.0 |
| 2296 | if (!strcmp(F->getNameStart(), "abs")) return true; |
| 2297 | break; |
| 2298 | case 4: // abs[lf](x) != -0.0 |
| 2299 | if (!strcmp(F->getNameStart(), "absf")) return true; |
| 2300 | if (!strcmp(F->getNameStart(), "absl")) return true; |
| 2301 | break; |
| 2302 | } |
| 2303 | } |
| 2304 | } |
| 2305 | } |
| 2306 | |
| 2307 | return false; |
| 2308 | } |
| 2309 | |
| 2310 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2311 | Instruction *InstCombiner::visitAdd(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 2312 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2313 | Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 2314 | |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 2315 | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2316 | // X + undef -> undef |
| 2317 | if (isa<UndefValue>(RHS)) |
| 2318 | return ReplaceInstUsesWith(I, RHS); |
| 2319 | |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 2320 | // X + 0 --> X |
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 2321 | if (!I.getType()->isFPOrFPVector()) { // NOTE: -0 + +0 = +0. |
Chris Lattner | 5e678e0 | 2005-10-17 17:56:38 +0000 | [diff] [blame] | 2322 | if (RHSC->isNullValue()) |
| 2323 | return ReplaceInstUsesWith(I, LHS); |
Chris Lattner | 8532cf6 | 2005-10-17 20:18:38 +0000 | [diff] [blame] | 2324 | } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) { |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 2325 | if (CFP->isExactlyValue(ConstantFP::getNegativeZero |
| 2326 | (I.getType())->getValueAPF())) |
Chris Lattner | 8532cf6 | 2005-10-17 20:18:38 +0000 | [diff] [blame] | 2327 | return ReplaceInstUsesWith(I, LHS); |
Chris Lattner | 5e678e0 | 2005-10-17 17:56:38 +0000 | [diff] [blame] | 2328 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2329 | |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 2330 | if (ConstantInt *CI = dyn_cast<ConstantInt>(RHSC)) { |
Chris Lattner | b4a2f05 | 2006-11-09 05:12:27 +0000 | [diff] [blame] | 2331 | // X + (signbit) --> X ^ signbit |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2332 | const APInt& Val = CI->getValue(); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 2333 | uint32_t BitWidth = Val.getBitWidth(); |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 2334 | if (Val == APInt::getSignBit(BitWidth)) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2335 | return BinaryOperator::createXor(LHS, RHS); |
Chris Lattner | b4a2f05 | 2006-11-09 05:12:27 +0000 | [diff] [blame] | 2336 | |
| 2337 | // See if SimplifyDemandedBits can simplify this. This handles stuff like |
| 2338 | // (X & 254)+1 -> (X&254)|1 |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 2339 | if (!isa<VectorType>(I.getType())) { |
| 2340 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 2341 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth), |
| 2342 | KnownZero, KnownOne)) |
| 2343 | return &I; |
| 2344 | } |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 2345 | } |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2346 | |
| 2347 | if (isa<PHINode>(LHS)) |
| 2348 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2349 | return NV; |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2350 | |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 2351 | ConstantInt *XorRHS = 0; |
| 2352 | Value *XorLHS = 0; |
Chris Lattner | c5eff44 | 2007-01-30 22:32:46 +0000 | [diff] [blame] | 2353 | if (isa<ConstantInt>(RHSC) && |
| 2354 | match(LHS, m_Xor(m_Value(XorLHS), m_ConstantInt(XorRHS)))) { |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 2355 | uint32_t TySizeBits = I.getType()->getPrimitiveSizeInBits(); |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2356 | const APInt& RHSVal = cast<ConstantInt>(RHSC)->getValue(); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2357 | |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 2358 | uint32_t Size = TySizeBits / 2; |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 2359 | APInt C0080Val(APInt(TySizeBits, 1ULL).shl(Size - 1)); |
| 2360 | APInt CFF80Val(-C0080Val); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2361 | do { |
| 2362 | if (TySizeBits > Size) { |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2363 | // If we have ADD(XOR(AND(X, 0xFF), 0x80), 0xF..F80), it's a sext. |
| 2364 | // 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] | 2365 | if ((RHSVal == CFF80Val && XorRHS->getValue() == C0080Val) || |
| 2366 | (RHSVal == C0080Val && XorRHS->getValue() == CFF80Val)) { |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2367 | // This is a sign extend if the top bits are known zero. |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 2368 | if (!MaskedValueIsZero(XorLHS, |
| 2369 | APInt::getHighBitsSet(TySizeBits, TySizeBits - Size))) |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2370 | 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] | 2371 | break; |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2372 | } |
| 2373 | } |
| 2374 | Size >>= 1; |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 2375 | C0080Val = APIntOps::lshr(C0080Val, Size); |
| 2376 | CFF80Val = APIntOps::ashr(CFF80Val, Size); |
| 2377 | } while (Size >= 1); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2378 | |
Reid Spencer | 35c3885 | 2007-03-28 01:36:16 +0000 | [diff] [blame] | 2379 | // FIXME: This shouldn't be necessary. When the backends can handle types |
| 2380 | // with funny bit widths then this whole cascade of if statements should |
| 2381 | // be removed. It is just here to get the size of the "middle" type back |
| 2382 | // up to something that the back ends can handle. |
| 2383 | const Type *MiddleType = 0; |
| 2384 | switch (Size) { |
| 2385 | default: break; |
| 2386 | case 32: MiddleType = Type::Int32Ty; break; |
| 2387 | case 16: MiddleType = Type::Int16Ty; break; |
| 2388 | case 8: MiddleType = Type::Int8Ty; break; |
| 2389 | } |
| 2390 | if (MiddleType) { |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 2391 | Instruction *NewTrunc = new TruncInst(XorLHS, MiddleType, "sext"); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2392 | InsertNewInstBefore(NewTrunc, I); |
Reid Spencer | 35c3885 | 2007-03-28 01:36:16 +0000 | [diff] [blame] | 2393 | return new SExtInst(NewTrunc, I.getType(), I.getName()); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2394 | } |
| 2395 | } |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 2396 | } |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 2397 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2398 | // X + X --> X << 1 |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 2399 | if (I.getType()->isInteger() && I.getType() != Type::Int1Ty) { |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2400 | if (Instruction *Result = AssociativeOpt(I, AddRHS(RHS))) return Result; |
Chris Lattner | 7edc8c2 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 2401 | |
| 2402 | if (Instruction *RHSI = dyn_cast<Instruction>(RHS)) { |
| 2403 | if (RHSI->getOpcode() == Instruction::Sub) |
| 2404 | if (LHS == RHSI->getOperand(1)) // A + (B - A) --> B |
| 2405 | return ReplaceInstUsesWith(I, RHSI->getOperand(0)); |
| 2406 | } |
| 2407 | if (Instruction *LHSI = dyn_cast<Instruction>(LHS)) { |
| 2408 | if (LHSI->getOpcode() == Instruction::Sub) |
| 2409 | if (RHS == LHSI->getOperand(1)) // (B - A) + A --> B |
| 2410 | return ReplaceInstUsesWith(I, LHSI->getOperand(0)); |
| 2411 | } |
Robert Bocchino | 7169828 | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 2412 | } |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 2413 | |
Chris Lattner | 5c4afb9 | 2002-05-08 22:46:53 +0000 | [diff] [blame] | 2414 | // -A + B --> B - A |
Chris Lattner | dd12f96 | 2008-02-17 21:03:36 +0000 | [diff] [blame] | 2415 | // -A + -B --> -(A + B) |
| 2416 | if (Value *LHSV = dyn_castNegVal(LHS)) { |
Chris Lattner | e10c0b9 | 2008-02-18 17:50:16 +0000 | [diff] [blame] | 2417 | if (LHS->getType()->isIntOrIntVector()) { |
| 2418 | if (Value *RHSV = dyn_castNegVal(RHS)) { |
| 2419 | Instruction *NewAdd = BinaryOperator::createAdd(LHSV, RHSV, "sum"); |
| 2420 | InsertNewInstBefore(NewAdd, I); |
| 2421 | return BinaryOperator::createNeg(NewAdd); |
| 2422 | } |
Chris Lattner | dd12f96 | 2008-02-17 21:03:36 +0000 | [diff] [blame] | 2423 | } |
| 2424 | |
| 2425 | return BinaryOperator::createSub(RHS, LHSV); |
| 2426 | } |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 2427 | |
| 2428 | // A + -B --> A - B |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 2429 | if (!isa<Constant>(RHS)) |
| 2430 | if (Value *V = dyn_castNegVal(RHS)) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2431 | return BinaryOperator::createSub(LHS, V); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2432 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2433 | |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2434 | ConstantInt *C2; |
| 2435 | if (Value *X = dyn_castFoldableMul(LHS, C2)) { |
| 2436 | if (X == RHS) // X*C + X --> X * (C+1) |
| 2437 | return BinaryOperator::createMul(RHS, AddOne(C2)); |
| 2438 | |
| 2439 | // X*C1 + X*C2 --> X * (C1+C2) |
| 2440 | ConstantInt *C1; |
| 2441 | if (X == dyn_castFoldableMul(RHS, C1)) |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2442 | return BinaryOperator::createMul(X, Add(C1, C2)); |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2443 | } |
| 2444 | |
| 2445 | // X + X*C --> X * (C+1) |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2446 | if (dyn_castFoldableMul(RHS, C2) == LHS) |
| 2447 | return BinaryOperator::createMul(LHS, AddOne(C2)); |
| 2448 | |
Chris Lattner | e617c9e | 2007-01-05 02:17:46 +0000 | [diff] [blame] | 2449 | // X + ~X --> -1 since ~X = -X-1 |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 2450 | if (dyn_castNotVal(LHS) == RHS || dyn_castNotVal(RHS) == LHS) |
| 2451 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | e617c9e | 2007-01-05 02:17:46 +0000 | [diff] [blame] | 2452 | |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2453 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2454 | // (A & C1)+(B & C2) --> (A & C1)|(B & C2) iff C1&C2 == 0 |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2455 | if (match(RHS, m_And(m_Value(), m_ConstantInt(C2)))) |
Chris Lattner | e617c9e | 2007-01-05 02:17:46 +0000 | [diff] [blame] | 2456 | if (Instruction *R = AssociativeOpt(I, AddMaskingAnd(C2))) |
| 2457 | return R; |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 2458 | |
Nick Lewycky | b6eabff | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 2459 | // W*X + Y*Z --> W * (X+Z) iff W == Y |
Nick Lewycky | 0c2c3f6 | 2008-02-03 08:19:11 +0000 | [diff] [blame] | 2460 | if (I.getType()->isIntOrIntVector()) { |
Nick Lewycky | b6eabff | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 2461 | Value *W, *X, *Y, *Z; |
| 2462 | if (match(LHS, m_Mul(m_Value(W), m_Value(X))) && |
| 2463 | match(RHS, m_Mul(m_Value(Y), m_Value(Z)))) { |
| 2464 | if (W != Y) { |
| 2465 | if (W == Z) { |
Bill Wendling | 587c01d | 2008-02-26 10:53:30 +0000 | [diff] [blame] | 2466 | std::swap(Y, Z); |
Nick Lewycky | b6eabff | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 2467 | } else if (Y == X) { |
Bill Wendling | 587c01d | 2008-02-26 10:53:30 +0000 | [diff] [blame] | 2468 | std::swap(W, X); |
| 2469 | } else if (X == Z) { |
Nick Lewycky | b6eabff | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 2470 | std::swap(Y, Z); |
| 2471 | std::swap(W, X); |
| 2472 | } |
| 2473 | } |
| 2474 | |
| 2475 | if (W == Y) { |
| 2476 | Value *NewAdd = InsertNewInstBefore(BinaryOperator::createAdd(X, Z, |
| 2477 | LHS->getName()), I); |
| 2478 | return BinaryOperator::createMul(W, NewAdd); |
| 2479 | } |
| 2480 | } |
| 2481 | } |
| 2482 | |
Chris Lattner | 6b03205 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 2483 | if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) { |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 2484 | Value *X = 0; |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2485 | if (match(LHS, m_Not(m_Value(X)))) // ~X + C --> (C-1) - X |
| 2486 | return BinaryOperator::createSub(SubOne(CRHS), X); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2487 | |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2488 | // (X & FF00) + xx00 -> (X+xx00) & FF00 |
| 2489 | 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] | 2490 | Constant *Anded = And(CRHS, C2); |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2491 | if (Anded == CRHS) { |
| 2492 | // See if all bits from the first bit set in the Add RHS up are included |
| 2493 | // in the mask. First, get the rightmost bit. |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2494 | const APInt& AddRHSV = CRHS->getValue(); |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2495 | |
| 2496 | // 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] | 2497 | APInt AddRHSHighBits(~((AddRHSV & -AddRHSV)-1)); |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2498 | |
| 2499 | // See if the and mask includes all of these bits. |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2500 | APInt AddRHSHighBitsAnd(AddRHSHighBits & C2->getValue()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2501 | |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2502 | if (AddRHSHighBits == AddRHSHighBitsAnd) { |
| 2503 | // Okay, the xform is safe. Insert the new add pronto. |
| 2504 | Value *NewAdd = InsertNewInstBefore(BinaryOperator::createAdd(X, CRHS, |
| 2505 | LHS->getName()), I); |
| 2506 | return BinaryOperator::createAnd(NewAdd, C2); |
| 2507 | } |
| 2508 | } |
| 2509 | } |
| 2510 | |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2511 | // Try to fold constant add into select arguments. |
| 2512 | if (SelectInst *SI = dyn_cast<SelectInst>(LHS)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2513 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2514 | return R; |
Chris Lattner | 6b03205 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 2515 | } |
| 2516 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2517 | // add (cast *A to intptrtype) B -> |
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2518 | // cast (GEP (cast *A to sbyte*) B) --> intptrtype |
Andrew Lenharth | 16d7955 | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 2519 | { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2520 | CastInst *CI = dyn_cast<CastInst>(LHS); |
| 2521 | Value *Other = RHS; |
Andrew Lenharth | 16d7955 | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 2522 | if (!CI) { |
| 2523 | CI = dyn_cast<CastInst>(RHS); |
| 2524 | Other = LHS; |
| 2525 | } |
Andrew Lenharth | 4563326 | 2006-09-20 15:37:57 +0000 | [diff] [blame] | 2526 | if (CI && CI->getType()->isSized() && |
Reid Spencer | abaa8ca | 2007-01-08 16:32:00 +0000 | [diff] [blame] | 2527 | (CI->getType()->getPrimitiveSizeInBits() == |
| 2528 | TD->getIntPtrType()->getPrimitiveSizeInBits()) |
Andrew Lenharth | 4563326 | 2006-09-20 15:37:57 +0000 | [diff] [blame] | 2529 | && isa<PointerType>(CI->getOperand(0)->getType())) { |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 2530 | unsigned AS = |
| 2531 | cast<PointerType>(CI->getOperand(0)->getType())->getAddressSpace(); |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 2532 | Value *I2 = InsertBitCastBefore(CI->getOperand(0), |
| 2533 | PointerType::get(Type::Int8Ty, AS), I); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 2534 | I2 = InsertNewInstBefore(GetElementPtrInst::Create(I2, Other, "ctg2"), I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2535 | return new PtrToIntInst(I2, CI->getType()); |
Andrew Lenharth | 16d7955 | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 2536 | } |
| 2537 | } |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2538 | |
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2539 | // add (select X 0 (sub n A)) A --> select X A n |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2540 | { |
| 2541 | SelectInst *SI = dyn_cast<SelectInst>(LHS); |
| 2542 | Value *Other = RHS; |
| 2543 | if (!SI) { |
| 2544 | SI = dyn_cast<SelectInst>(RHS); |
| 2545 | Other = LHS; |
| 2546 | } |
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2547 | if (SI && SI->hasOneUse()) { |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2548 | Value *TV = SI->getTrueValue(); |
| 2549 | Value *FV = SI->getFalseValue(); |
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2550 | Value *A, *N; |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2551 | |
| 2552 | // Can we fold the add into the argument of the select? |
| 2553 | // We check both true and false select arguments for a matching subtract. |
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2554 | if (match(FV, m_Zero()) && match(TV, m_Sub(m_Value(N), m_Value(A))) && |
| 2555 | A == Other) // Fold the add into the true select value. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 2556 | return SelectInst::Create(SI->getCondition(), N, A); |
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2557 | if (match(TV, m_Zero()) && match(FV, m_Sub(m_Value(N), m_Value(A))) && |
| 2558 | A == Other) // Fold the add into the false select value. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 2559 | return SelectInst::Create(SI->getCondition(), A, N); |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2560 | } |
| 2561 | } |
Chris Lattner | 2454a2e | 2008-01-29 06:52:45 +0000 | [diff] [blame] | 2562 | |
| 2563 | // Check for X+0.0. Simplify it to X if we know X is not -0.0. |
| 2564 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHS)) |
| 2565 | if (CFP->getValueAPF().isPosZero() && CannotBeNegativeZero(LHS)) |
| 2566 | return ReplaceInstUsesWith(I, LHS); |
Andrew Lenharth | 16d7955 | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 2567 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2568 | return Changed ? &I : 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2569 | } |
| 2570 | |
Chris Lattner | 1ba5bcd | 2003-07-22 21:46:59 +0000 | [diff] [blame] | 2571 | // isSignBit - Return true if the value represented by the constant only has the |
| 2572 | // highest order bit set. |
| 2573 | static bool isSignBit(ConstantInt *CI) { |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 2574 | uint32_t NumBits = CI->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 5a1e3e1 | 2007-03-19 20:58:18 +0000 | [diff] [blame] | 2575 | return CI->getValue() == APInt::getSignBit(NumBits); |
Chris Lattner | 1ba5bcd | 2003-07-22 21:46:59 +0000 | [diff] [blame] | 2576 | } |
| 2577 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2578 | Instruction *InstCombiner::visitSub(BinaryOperator &I) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2579 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2580 | |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2581 | if (Op0 == Op1) // sub X, X -> 0 |
| 2582 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2583 | |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2584 | // If this is a 'B = x-(-A)', change to B = x+A... |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 2585 | if (Value *V = dyn_castNegVal(Op1)) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2586 | return BinaryOperator::createAdd(Op0, V); |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 2587 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2588 | if (isa<UndefValue>(Op0)) |
| 2589 | return ReplaceInstUsesWith(I, Op0); // undef - X -> undef |
| 2590 | if (isa<UndefValue>(Op1)) |
| 2591 | return ReplaceInstUsesWith(I, Op1); // X - undef -> undef |
| 2592 | |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 2593 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op0)) { |
| 2594 | // Replace (-1 - A) with (~A)... |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2595 | if (C->isAllOnesValue()) |
| 2596 | return BinaryOperator::createNot(Op1); |
Chris Lattner | 4037171 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 2597 | |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 2598 | // C - ~X == X + (1+C) |
Reid Spencer | 4b828e6 | 2005-06-18 17:37:34 +0000 | [diff] [blame] | 2599 | Value *X = 0; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2600 | if (match(Op1, m_Not(m_Value(X)))) |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2601 | return BinaryOperator::createAdd(X, AddOne(C)); |
| 2602 | |
Chris Lattner | 76b7a06 | 2007-01-15 07:02:54 +0000 | [diff] [blame] | 2603 | // -(X >>u 31) -> (X >>s 31) |
| 2604 | // -(X >>s 31) -> (X >>u 31) |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 2605 | if (C->isZero()) { |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 2606 | if (BinaryOperator *SI = dyn_cast<BinaryOperator>(Op1)) { |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2607 | if (SI->getOpcode() == Instruction::LShr) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2608 | if (ConstantInt *CU = dyn_cast<ConstantInt>(SI->getOperand(1))) { |
Chris Lattner | 9c29067 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 2609 | // 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] | 2610 | if (CU->getLimitedValue(SI->getType()->getPrimitiveSizeInBits()) == |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2611 | SI->getType()->getPrimitiveSizeInBits()-1) { |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2612 | // Ok, the transformation is safe. Insert AShr. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2613 | return BinaryOperator::create(Instruction::AShr, |
| 2614 | SI->getOperand(0), CU, SI->getName()); |
Chris Lattner | 9c29067 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 2615 | } |
| 2616 | } |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2617 | } |
| 2618 | else if (SI->getOpcode() == Instruction::AShr) { |
| 2619 | if (ConstantInt *CU = dyn_cast<ConstantInt>(SI->getOperand(1))) { |
| 2620 | // 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] | 2621 | if (CU->getLimitedValue(SI->getType()->getPrimitiveSizeInBits()) == |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2622 | SI->getType()->getPrimitiveSizeInBits()-1) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 2623 | // Ok, the transformation is safe. Insert LShr. |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 2624 | return BinaryOperator::createLShr( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2625 | SI->getOperand(0), CU, SI->getName()); |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2626 | } |
| 2627 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 2628 | } |
| 2629 | } |
Chris Lattner | bfe492b | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 2630 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2631 | |
| 2632 | // Try to fold constant sub into select arguments. |
| 2633 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2634 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2635 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2636 | |
| 2637 | if (isa<PHINode>(Op0)) |
| 2638 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2639 | return NV; |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 2640 | } |
| 2641 | |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2642 | if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) { |
| 2643 | if (Op1I->getOpcode() == Instruction::Add && |
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 2644 | !Op0->getType()->isFPOrFPVector()) { |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2645 | if (Op1I->getOperand(0) == Op0) // X-(X+Y) == -Y |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2646 | return BinaryOperator::createNeg(Op1I->getOperand(1), I.getName()); |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2647 | else if (Op1I->getOperand(1) == Op0) // X-(Y+X) == -Y |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2648 | return BinaryOperator::createNeg(Op1I->getOperand(0), I.getName()); |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2649 | else if (ConstantInt *CI1 = dyn_cast<ConstantInt>(I.getOperand(0))) { |
| 2650 | if (ConstantInt *CI2 = dyn_cast<ConstantInt>(Op1I->getOperand(1))) |
| 2651 | // C1-(X+C2) --> (C1-C2)-X |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2652 | return BinaryOperator::createSub(Subtract(CI1, CI2), |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2653 | Op1I->getOperand(0)); |
| 2654 | } |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2655 | } |
| 2656 | |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 2657 | if (Op1I->hasOneUse()) { |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2658 | // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression |
| 2659 | // is not used by anyone else... |
| 2660 | // |
Chris Lattner | 0517e72 | 2004-02-02 20:09:56 +0000 | [diff] [blame] | 2661 | if (Op1I->getOpcode() == Instruction::Sub && |
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 2662 | !Op1I->getType()->isFPOrFPVector()) { |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2663 | // Swap the two operands of the subexpr... |
| 2664 | Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1); |
| 2665 | Op1I->setOperand(0, IIOp1); |
| 2666 | Op1I->setOperand(1, IIOp0); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2667 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2668 | // Create the new top level add instruction... |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2669 | return BinaryOperator::createAdd(Op0, Op1); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2670 | } |
| 2671 | |
| 2672 | // Replace (A - (A & B)) with (A & ~B) if this is the only use of (A&B)... |
| 2673 | // |
| 2674 | if (Op1I->getOpcode() == Instruction::And && |
| 2675 | (Op1I->getOperand(0) == Op0 || Op1I->getOperand(1) == Op0)) { |
| 2676 | Value *OtherOp = Op1I->getOperand(Op1I->getOperand(0) == Op0); |
| 2677 | |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 2678 | Value *NewNot = |
| 2679 | InsertNewInstBefore(BinaryOperator::createNot(OtherOp, "B.not"), I); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2680 | return BinaryOperator::createAnd(Op0, NewNot); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2681 | } |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2682 | |
Reid Spencer | ac5209e | 2006-10-16 23:08:08 +0000 | [diff] [blame] | 2683 | // 0 - (X sdiv C) -> (X sdiv -C) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2684 | if (Op1I->getOpcode() == Instruction::SDiv) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2685 | if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0)) |
Zhou Sheng | 843f0767 | 2007-04-19 05:39:12 +0000 | [diff] [blame] | 2686 | if (CSI->isZero()) |
Chris Lattner | 91ccc15 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 2687 | if (Constant *DivRHS = dyn_cast<Constant>(Op1I->getOperand(1))) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2688 | return BinaryOperator::createSDiv(Op1I->getOperand(0), |
Chris Lattner | 91ccc15 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 2689 | ConstantExpr::getNeg(DivRHS)); |
| 2690 | |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2691 | // X - X*C --> X * (1-C) |
Reid Spencer | 4b828e6 | 2005-06-18 17:37:34 +0000 | [diff] [blame] | 2692 | ConstantInt *C2 = 0; |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2693 | if (dyn_castFoldableMul(Op1I, C2) == Op0) { |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2694 | Constant *CP1 = Subtract(ConstantInt::get(I.getType(), 1), C2); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2695 | return BinaryOperator::createMul(Op0, CP1); |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2696 | } |
Dan Gohman | 5d066ff | 2007-09-17 17:31:57 +0000 | [diff] [blame] | 2697 | |
| 2698 | // X - ((X / Y) * Y) --> X % Y |
| 2699 | if (Op1I->getOpcode() == Instruction::Mul) |
| 2700 | if (Instruction *I = dyn_cast<Instruction>(Op1I->getOperand(0))) |
| 2701 | if (Op0 == I->getOperand(0) && |
| 2702 | Op1I->getOperand(1) == I->getOperand(1)) { |
| 2703 | if (I->getOpcode() == Instruction::SDiv) |
| 2704 | return BinaryOperator::createSRem(Op0, Op1I->getOperand(1)); |
| 2705 | if (I->getOpcode() == Instruction::UDiv) |
| 2706 | return BinaryOperator::createURem(Op0, Op1I->getOperand(1)); |
| 2707 | } |
Chris Lattner | 4037171 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 2708 | } |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2709 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2710 | |
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 2711 | if (!Op0->getType()->isFPOrFPVector()) |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 2712 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) { |
Chris Lattner | 7edc8c2 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 2713 | if (Op0I->getOpcode() == Instruction::Add) { |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 2714 | if (Op0I->getOperand(0) == Op1) // (Y+X)-Y == X |
| 2715 | return ReplaceInstUsesWith(I, Op0I->getOperand(1)); |
| 2716 | else if (Op0I->getOperand(1) == Op1) // (X+Y)-Y == X |
| 2717 | return ReplaceInstUsesWith(I, Op0I->getOperand(0)); |
Chris Lattner | 7edc8c2 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 2718 | } else if (Op0I->getOpcode() == Instruction::Sub) { |
| 2719 | if (Op0I->getOperand(0) == Op1) // (X-Y)-X == -Y |
| 2720 | return BinaryOperator::createNeg(Op0I->getOperand(1), I.getName()); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 2721 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 2722 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2723 | |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2724 | ConstantInt *C1; |
| 2725 | if (Value *X = dyn_castFoldableMul(Op0, C1)) { |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2726 | if (X == Op1) // X*C - X --> X * (C-1) |
| 2727 | return BinaryOperator::createMul(Op1, SubOne(C1)); |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2728 | |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2729 | ConstantInt *C2; // X*C1 - X*C2 -> X * (C1-C2) |
| 2730 | if (X == dyn_castFoldableMul(Op1, C2)) |
Zhou Sheng | 58d13af | 2008-02-22 10:00:35 +0000 | [diff] [blame] | 2731 | return BinaryOperator::createMul(X, Subtract(C1, C2)); |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2732 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2733 | return 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2734 | } |
| 2735 | |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2736 | /// isSignBitCheck - Given an exploded icmp instruction, return true if the |
| 2737 | /// comparison only checks the sign bit. If it only checks the sign bit, set |
| 2738 | /// TrueIfSigned if the result of the comparison is true when the input value is |
| 2739 | /// signed. |
| 2740 | static bool isSignBitCheck(ICmpInst::Predicate pred, ConstantInt *RHS, |
| 2741 | bool &TrueIfSigned) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2742 | switch (pred) { |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2743 | case ICmpInst::ICMP_SLT: // True if LHS s< 0 |
| 2744 | TrueIfSigned = true; |
| 2745 | return RHS->isZero(); |
Chris Lattner | cb7122b | 2007-07-16 04:15:34 +0000 | [diff] [blame] | 2746 | case ICmpInst::ICMP_SLE: // True if LHS s<= RHS and RHS == -1 |
| 2747 | TrueIfSigned = true; |
| 2748 | return RHS->isAllOnesValue(); |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2749 | case ICmpInst::ICMP_SGT: // True if LHS s> -1 |
| 2750 | TrueIfSigned = false; |
| 2751 | return RHS->isAllOnesValue(); |
Chris Lattner | cb7122b | 2007-07-16 04:15:34 +0000 | [diff] [blame] | 2752 | case ICmpInst::ICMP_UGT: |
| 2753 | // True if LHS u> RHS and RHS == high-bit-mask - 1 |
| 2754 | TrueIfSigned = true; |
| 2755 | return RHS->getValue() == |
| 2756 | APInt::getSignedMaxValue(RHS->getType()->getPrimitiveSizeInBits()); |
| 2757 | case ICmpInst::ICMP_UGE: |
| 2758 | // True if LHS u>= RHS and RHS == high-bit-mask (2^7, 2^15, 2^31, etc) |
| 2759 | TrueIfSigned = true; |
| 2760 | return RHS->getValue() == |
| 2761 | APInt::getSignBit(RHS->getType()->getPrimitiveSizeInBits()); |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2762 | default: |
| 2763 | return false; |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2764 | } |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2765 | } |
| 2766 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2767 | Instruction *InstCombiner::visitMul(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 2768 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2769 | Value *Op0 = I.getOperand(0); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2770 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2771 | if (isa<UndefValue>(I.getOperand(1))) // undef * X -> 0 |
| 2772 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2773 | |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2774 | // Simplify mul instructions with a constant RHS... |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2775 | if (Constant *Op1 = dyn_cast<Constant>(I.getOperand(1))) { |
| 2776 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 2777 | |
| 2778 | // ((X << C1)*C2) == (X * (C2 << C1)) |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2779 | if (BinaryOperator *SI = dyn_cast<BinaryOperator>(Op0)) |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 2780 | if (SI->getOpcode() == Instruction::Shl) |
| 2781 | if (Constant *ShOp = dyn_cast<Constant>(SI->getOperand(1))) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2782 | return BinaryOperator::createMul(SI->getOperand(0), |
| 2783 | ConstantExpr::getShl(CI, ShOp)); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2784 | |
Zhou Sheng | 843f0767 | 2007-04-19 05:39:12 +0000 | [diff] [blame] | 2785 | if (CI->isZero()) |
Chris Lattner | 515c97c | 2003-09-11 22:24:54 +0000 | [diff] [blame] | 2786 | return ReplaceInstUsesWith(I, Op1); // X * 0 == 0 |
| 2787 | if (CI->equalsInt(1)) // X * 1 == X |
| 2788 | return ReplaceInstUsesWith(I, Op0); |
| 2789 | if (CI->isAllOnesValue()) // X * -1 == 0 - X |
Chris Lattner | 0af1fab | 2003-06-25 17:09:20 +0000 | [diff] [blame] | 2790 | return BinaryOperator::createNeg(Op0, I.getName()); |
Chris Lattner | 6c1ce21 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 2791 | |
Zhou Sheng | 97b52c2 | 2007-03-29 01:57:21 +0000 | [diff] [blame] | 2792 | const APInt& Val = cast<ConstantInt>(CI)->getValue(); |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2793 | if (Val.isPowerOf2()) { // Replace X*(2^C) with X << C |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 2794 | return BinaryOperator::createShl(Op0, |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2795 | ConstantInt::get(Op0->getType(), Val.logBase2())); |
Chris Lattner | bcd7db5 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 2796 | } |
Robert Bocchino | 7169828 | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 2797 | } else if (ConstantFP *Op1F = dyn_cast<ConstantFP>(Op1)) { |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2798 | if (Op1F->isNullValue()) |
| 2799 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 6c1ce21 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 2800 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2801 | // "In IEEE floating point, x*1 is not equivalent to x for nans. However, |
| 2802 | // 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] | 2803 | // We need a better interface for long double here. |
| 2804 | if (Op1->getType() == Type::FloatTy || Op1->getType() == Type::DoubleTy) |
| 2805 | if (Op1F->isExactlyValue(1.0)) |
| 2806 | return ReplaceInstUsesWith(I, Op0); // Eliminate 'mul double %X, 1.0' |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2807 | } |
Chris Lattner | ab51f3f | 2006-03-04 06:04:02 +0000 | [diff] [blame] | 2808 | |
| 2809 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) |
| 2810 | if (Op0I->getOpcode() == Instruction::Add && Op0I->hasOneUse() && |
| 2811 | isa<ConstantInt>(Op0I->getOperand(1))) { |
| 2812 | // Canonicalize (X+C1)*C2 -> X*C2+C1*C2. |
| 2813 | Instruction *Add = BinaryOperator::createMul(Op0I->getOperand(0), |
| 2814 | Op1, "tmp"); |
| 2815 | InsertNewInstBefore(Add, I); |
| 2816 | Value *C1C2 = ConstantExpr::getMul(Op1, |
| 2817 | cast<Constant>(Op0I->getOperand(1))); |
| 2818 | return BinaryOperator::createAdd(Add, C1C2); |
| 2819 | |
| 2820 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2821 | |
| 2822 | // Try to fold constant mul into select arguments. |
| 2823 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2824 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2825 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2826 | |
| 2827 | if (isa<PHINode>(Op0)) |
| 2828 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2829 | return NV; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2830 | } |
| 2831 | |
Chris Lattner | a4f445b | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 2832 | if (Value *Op0v = dyn_castNegVal(Op0)) // -X * -Y = X*Y |
| 2833 | if (Value *Op1v = dyn_castNegVal(I.getOperand(1))) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2834 | return BinaryOperator::createMul(Op0v, Op1v); |
Chris Lattner | a4f445b | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 2835 | |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2836 | // If one of the operands of the multiply is a cast from a boolean value, then |
| 2837 | // we know the bool is either zero or one, so this is a 'masking' multiply. |
| 2838 | // See if we can simplify things based on how the boolean was originally |
| 2839 | // formed. |
| 2840 | CastInst *BoolCast = 0; |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 2841 | if (ZExtInst *CI = dyn_cast<ZExtInst>(I.getOperand(0))) |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 2842 | if (CI->getOperand(0)->getType() == Type::Int1Ty) |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2843 | BoolCast = CI; |
| 2844 | if (!BoolCast) |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 2845 | if (ZExtInst *CI = dyn_cast<ZExtInst>(I.getOperand(1))) |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 2846 | if (CI->getOperand(0)->getType() == Type::Int1Ty) |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2847 | BoolCast = CI; |
| 2848 | if (BoolCast) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2849 | if (ICmpInst *SCI = dyn_cast<ICmpInst>(BoolCast->getOperand(0))) { |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2850 | Value *SCIOp0 = SCI->getOperand(0), *SCIOp1 = SCI->getOperand(1); |
| 2851 | const Type *SCOpTy = SCIOp0->getType(); |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2852 | bool TIS = false; |
| 2853 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2854 | // 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] | 2855 | // multiply into a shift/and combination. |
| 2856 | if (isa<ConstantInt>(SCIOp1) && |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2857 | isSignBitCheck(SCI->getPredicate(), cast<ConstantInt>(SCIOp1), TIS) && |
| 2858 | TIS) { |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2859 | // Shift the X value right to turn it into "all signbits". |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2860 | Constant *Amt = ConstantInt::get(SCIOp0->getType(), |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 2861 | SCOpTy->getPrimitiveSizeInBits()-1); |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2862 | Value *V = |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2863 | InsertNewInstBefore( |
| 2864 | BinaryOperator::create(Instruction::AShr, SCIOp0, Amt, |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2865 | BoolCast->getOperand(0)->getName()+ |
| 2866 | ".mask"), I); |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2867 | |
| 2868 | // If the multiply type is not the same as the source type, sign extend |
| 2869 | // or truncate to the multiply type. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 2870 | if (I.getType() != V->getType()) { |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 2871 | uint32_t SrcBits = V->getType()->getPrimitiveSizeInBits(); |
| 2872 | uint32_t DstBits = I.getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 2873 | Instruction::CastOps opcode = |
| 2874 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2875 | (SrcBits < DstBits ? Instruction::SExt : Instruction::Trunc)); |
| 2876 | V = InsertCastBefore(opcode, V, I.getType(), I); |
| 2877 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2878 | |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2879 | Value *OtherOp = Op0 == BoolCast ? I.getOperand(1) : Op0; |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2880 | return BinaryOperator::createAnd(V, OtherOp); |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2881 | } |
| 2882 | } |
| 2883 | } |
| 2884 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2885 | return Changed ? &I : 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2886 | } |
| 2887 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2888 | /// This function implements the transforms on div instructions that work |
| 2889 | /// regardless of the kind of div instruction it is (udiv, sdiv, or fdiv). It is |
| 2890 | /// used by the visitors to those instructions. |
| 2891 | /// @brief Transforms common to all three div instructions |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2892 | Instruction *InstCombiner::commonDivTransforms(BinaryOperator &I) { |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2893 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2894 | |
Chris Lattner | 50b2ca4 | 2008-02-19 06:12:18 +0000 | [diff] [blame] | 2895 | // undef / X -> 0 for integer. |
| 2896 | // undef / X -> undef for FP (the undef could be a snan). |
| 2897 | if (isa<UndefValue>(Op0)) { |
| 2898 | if (Op0->getType()->isFPOrFPVector()) |
| 2899 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2900 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | 50b2ca4 | 2008-02-19 06:12:18 +0000 | [diff] [blame] | 2901 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2902 | |
| 2903 | // X / undef -> undef |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2904 | if (isa<UndefValue>(Op1)) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2905 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2906 | |
Chris Lattner | 25feae5 | 2008-01-28 00:58:18 +0000 | [diff] [blame] | 2907 | // Handle cases involving: [su]div X, (select Cond, Y, Z) |
| 2908 | // This does not apply for fdiv. |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2909 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { |
Chris Lattner | 25feae5 | 2008-01-28 00:58:18 +0000 | [diff] [blame] | 2910 | // [su]div X, (Cond ? 0 : Y) -> div X, Y. If the div and the select are in |
| 2911 | // the same basic block, then we replace the select with Y, and the |
| 2912 | // condition of the select with false (if the cond value is in the same BB). |
| 2913 | // If the select has uses other than the div, this allows them to be |
| 2914 | // simplified also. Note that div X, Y is just as good as div X, 0 (undef) |
| 2915 | if (ConstantInt *ST = dyn_cast<ConstantInt>(SI->getOperand(1))) |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2916 | if (ST->isNullValue()) { |
| 2917 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); |
| 2918 | if (CondI && CondI->getParent() == I.getParent()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2919 | UpdateValueUsesWith(CondI, ConstantInt::getFalse()); |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2920 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
| 2921 | I.setOperand(1, SI->getOperand(2)); |
| 2922 | else |
| 2923 | UpdateValueUsesWith(SI, SI->getOperand(2)); |
| 2924 | return &I; |
| 2925 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2926 | |
Chris Lattner | 25feae5 | 2008-01-28 00:58:18 +0000 | [diff] [blame] | 2927 | // Likewise for: [su]div X, (Cond ? Y : 0) -> div X, Y |
| 2928 | if (ConstantInt *ST = dyn_cast<ConstantInt>(SI->getOperand(2))) |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2929 | if (ST->isNullValue()) { |
| 2930 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); |
| 2931 | if (CondI && CondI->getParent() == I.getParent()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2932 | UpdateValueUsesWith(CondI, ConstantInt::getTrue()); |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2933 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
| 2934 | I.setOperand(1, SI->getOperand(1)); |
| 2935 | else |
| 2936 | UpdateValueUsesWith(SI, SI->getOperand(1)); |
| 2937 | return &I; |
| 2938 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2939 | } |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2940 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2941 | return 0; |
| 2942 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2943 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2944 | /// This function implements the transforms common to both integer division |
| 2945 | /// instructions (udiv and sdiv). It is called by the visitors to those integer |
| 2946 | /// division instructions. |
| 2947 | /// @brief Common integer divide transforms |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2948 | Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2949 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2950 | |
| 2951 | if (Instruction *Common = commonDivTransforms(I)) |
| 2952 | return Common; |
| 2953 | |
| 2954 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
| 2955 | // div X, 1 == X |
| 2956 | if (RHS->equalsInt(1)) |
| 2957 | return ReplaceInstUsesWith(I, Op0); |
| 2958 | |
| 2959 | // (X / C1) / C2 -> X / (C1*C2) |
| 2960 | if (Instruction *LHS = dyn_cast<Instruction>(Op0)) |
| 2961 | if (Instruction::BinaryOps(LHS->getOpcode()) == I.getOpcode()) |
| 2962 | if (ConstantInt *LHSRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) { |
Nick Lewycky | e0cfecf | 2008-02-18 22:48:05 +0000 | [diff] [blame] | 2963 | if (MultiplyOverflows(RHS, LHSRHS, I.getOpcode()==Instruction::SDiv)) |
| 2964 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2965 | else |
| 2966 | return BinaryOperator::create(I.getOpcode(), LHS->getOperand(0), |
| 2967 | Multiply(RHS, LHSRHS)); |
Chris Lattner | bf70b83 | 2005-04-08 04:03:26 +0000 | [diff] [blame] | 2968 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2969 | |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2970 | if (!RHS->isZero()) { // avoid X udiv 0 |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2971 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 2972 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 2973 | return R; |
| 2974 | if (isa<PHINode>(Op0)) |
| 2975 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2976 | return NV; |
| 2977 | } |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2978 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2979 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2980 | // 0 / X == 0, we don't need to preserve faults! |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2981 | if (ConstantInt *LHS = dyn_cast<ConstantInt>(Op0)) |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2982 | if (LHS->equalsInt(0)) |
| 2983 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2984 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2985 | return 0; |
| 2986 | } |
| 2987 | |
| 2988 | Instruction *InstCombiner::visitUDiv(BinaryOperator &I) { |
| 2989 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2990 | |
| 2991 | // Handle the integer div common cases |
| 2992 | if (Instruction *Common = commonIDivTransforms(I)) |
| 2993 | return Common; |
| 2994 | |
| 2995 | // X udiv C^2 -> X >> C |
| 2996 | // Check to see if this is an unsigned division with an exact power of 2, |
| 2997 | // if so, convert to a right shift. |
| 2998 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op1)) { |
Reid Spencer | 6eb0d99 | 2007-03-26 23:58:26 +0000 | [diff] [blame] | 2999 | if (C->getValue().isPowerOf2()) // 0 not included in isPowerOf2 |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 3000 | return BinaryOperator::createLShr(Op0, |
Zhou Sheng | 0fc5095 | 2007-03-25 05:01:29 +0000 | [diff] [blame] | 3001 | ConstantInt::get(Op0->getType(), C->getValue().logBase2())); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3002 | } |
| 3003 | |
| 3004 | // X udiv (C1 << N), where C1 is "1<<C2" --> X >> (N+C2) |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3005 | if (BinaryOperator *RHSI = dyn_cast<BinaryOperator>(I.getOperand(1))) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3006 | if (RHSI->getOpcode() == Instruction::Shl && |
| 3007 | isa<ConstantInt>(RHSI->getOperand(0))) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3008 | const APInt& C1 = cast<ConstantInt>(RHSI->getOperand(0))->getValue(); |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 3009 | if (C1.isPowerOf2()) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3010 | Value *N = RHSI->getOperand(1); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3011 | const Type *NTy = N->getType(); |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 3012 | if (uint32_t C2 = C1.logBase2()) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3013 | Constant *C2V = ConstantInt::get(NTy, C2); |
| 3014 | N = InsertNewInstBefore(BinaryOperator::createAdd(N, C2V, "tmp"), I); |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 3015 | } |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 3016 | return BinaryOperator::createLShr(Op0, N); |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 3017 | } |
| 3018 | } |
Chris Lattner | c812e5d | 2005-11-05 07:40:31 +0000 | [diff] [blame] | 3019 | } |
| 3020 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3021 | // udiv X, (Select Cond, C1, C2) --> Select Cond, (shr X, C1), (shr X, C2) |
| 3022 | // where C1&C2 are powers of two. |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 3023 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3024 | if (ConstantInt *STO = dyn_cast<ConstantInt>(SI->getOperand(1))) |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 3025 | if (ConstantInt *SFO = dyn_cast<ConstantInt>(SI->getOperand(2))) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3026 | const APInt &TVA = STO->getValue(), &FVA = SFO->getValue(); |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 3027 | if (TVA.isPowerOf2() && FVA.isPowerOf2()) { |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 3028 | // Compute the shift amounts |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 3029 | uint32_t TSA = TVA.logBase2(), FSA = FVA.logBase2(); |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 3030 | // Construct the "on true" case of the select |
| 3031 | Constant *TC = ConstantInt::get(Op0->getType(), TSA); |
| 3032 | Instruction *TSI = BinaryOperator::createLShr( |
| 3033 | Op0, TC, SI->getName()+".t"); |
| 3034 | TSI = InsertNewInstBefore(TSI, I); |
| 3035 | |
| 3036 | // Construct the "on false" case of the select |
| 3037 | Constant *FC = ConstantInt::get(Op0->getType(), FSA); |
| 3038 | Instruction *FSI = BinaryOperator::createLShr( |
| 3039 | Op0, FC, SI->getName()+".f"); |
| 3040 | FSI = InsertNewInstBefore(FSI, I); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3041 | |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 3042 | // construct the select instruction and return it. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 3043 | return SelectInst::Create(SI->getOperand(0), TSI, FSI, SI->getName()); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3044 | } |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 3045 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3046 | return 0; |
| 3047 | } |
| 3048 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3049 | Instruction *InstCombiner::visitSDiv(BinaryOperator &I) { |
| 3050 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 3051 | |
| 3052 | // Handle the integer div common cases |
| 3053 | if (Instruction *Common = commonIDivTransforms(I)) |
| 3054 | return Common; |
| 3055 | |
| 3056 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
| 3057 | // sdiv X, -1 == -X |
| 3058 | if (RHS->isAllOnesValue()) |
| 3059 | return BinaryOperator::createNeg(Op0); |
| 3060 | |
| 3061 | // -X/C -> X/-C |
| 3062 | if (Value *LHSNeg = dyn_castNegVal(Op0)) |
| 3063 | return BinaryOperator::createSDiv(LHSNeg, ConstantExpr::getNeg(RHS)); |
| 3064 | } |
| 3065 | |
| 3066 | // If the sign bits of both operands are zero (i.e. we can prove they are |
| 3067 | // unsigned inputs), turn this into a udiv. |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 3068 | if (I.getType()->isInteger()) { |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 3069 | APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits())); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3070 | if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) { |
Dan Gohman | cff5509 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 3071 | // 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] | 3072 | return BinaryOperator::createUDiv(Op0, Op1, I.getName()); |
| 3073 | } |
| 3074 | } |
| 3075 | |
| 3076 | return 0; |
| 3077 | } |
| 3078 | |
| 3079 | Instruction *InstCombiner::visitFDiv(BinaryOperator &I) { |
| 3080 | return commonDivTransforms(I); |
| 3081 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3082 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3083 | /// This function implements the transforms on rem instructions that work |
| 3084 | /// regardless of the kind of rem instruction it is (urem, srem, or frem). It |
| 3085 | /// is used by the visitors to those instructions. |
| 3086 | /// @brief Transforms common to all three rem instructions |
| 3087 | Instruction *InstCombiner::commonRemTransforms(BinaryOperator &I) { |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 3088 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3089 | |
Chris Lattner | 50b2ca4 | 2008-02-19 06:12:18 +0000 | [diff] [blame] | 3090 | // 0 % X == 0 for integer, we don't need to preserve faults! |
Chris Lattner | 19ccd5c | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 3091 | if (Constant *LHS = dyn_cast<Constant>(Op0)) |
| 3092 | if (LHS->isNullValue()) |
| 3093 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 3094 | |
Chris Lattner | 50b2ca4 | 2008-02-19 06:12:18 +0000 | [diff] [blame] | 3095 | if (isa<UndefValue>(Op0)) { // undef % X -> 0 |
| 3096 | if (I.getType()->isFPOrFPVector()) |
| 3097 | return ReplaceInstUsesWith(I, Op0); // X % undef -> undef (could be SNaN) |
Chris Lattner | 19ccd5c | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 3098 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | 50b2ca4 | 2008-02-19 06:12:18 +0000 | [diff] [blame] | 3099 | } |
Chris Lattner | 19ccd5c | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 3100 | if (isa<UndefValue>(Op1)) |
| 3101 | return ReplaceInstUsesWith(I, Op1); // X % undef -> undef |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3102 | |
| 3103 | // Handle cases involving: rem X, (select Cond, Y, Z) |
| 3104 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { |
| 3105 | // rem X, (Cond ? 0 : Y) -> rem X, Y. If the rem and the select are in |
| 3106 | // the same basic block, then we replace the select with Y, and the |
| 3107 | // condition of the select with false (if the cond value is in the same |
| 3108 | // BB). If the select has uses other than the div, this allows them to be |
| 3109 | // simplified also. |
| 3110 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(1))) |
| 3111 | if (ST->isNullValue()) { |
| 3112 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); |
| 3113 | if (CondI && CondI->getParent() == I.getParent()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3114 | UpdateValueUsesWith(CondI, ConstantInt::getFalse()); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3115 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
| 3116 | I.setOperand(1, SI->getOperand(2)); |
| 3117 | else |
| 3118 | UpdateValueUsesWith(SI, SI->getOperand(2)); |
Chris Lattner | 5b73c08 | 2004-07-06 07:01:22 +0000 | [diff] [blame] | 3119 | return &I; |
| 3120 | } |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3121 | // Likewise for: rem X, (Cond ? Y : 0) -> rem X, Y |
| 3122 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(2))) |
| 3123 | if (ST->isNullValue()) { |
| 3124 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); |
| 3125 | if (CondI && CondI->getParent() == I.getParent()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3126 | UpdateValueUsesWith(CondI, ConstantInt::getTrue()); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3127 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
| 3128 | I.setOperand(1, SI->getOperand(1)); |
| 3129 | else |
| 3130 | UpdateValueUsesWith(SI, SI->getOperand(1)); |
| 3131 | return &I; |
| 3132 | } |
Chris Lattner | 11a49f2 | 2005-11-05 07:28:37 +0000 | [diff] [blame] | 3133 | } |
Chris Lattner | 5b73c08 | 2004-07-06 07:01:22 +0000 | [diff] [blame] | 3134 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3135 | return 0; |
| 3136 | } |
| 3137 | |
| 3138 | /// This function implements the transforms common to both integer remainder |
| 3139 | /// instructions (urem and srem). It is called by the visitors to those integer |
| 3140 | /// remainder instructions. |
| 3141 | /// @brief Common integer remainder transforms |
| 3142 | Instruction *InstCombiner::commonIRemTransforms(BinaryOperator &I) { |
| 3143 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 3144 | |
| 3145 | if (Instruction *common = commonRemTransforms(I)) |
| 3146 | return common; |
| 3147 | |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 3148 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 19ccd5c | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 3149 | // X % 0 == undef, we don't need to preserve faults! |
| 3150 | if (RHS->equalsInt(0)) |
| 3151 | return ReplaceInstUsesWith(I, UndefValue::get(I.getType())); |
| 3152 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3153 | if (RHS->equalsInt(1)) // X % 1 == 0 |
| 3154 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 3155 | |
Chris Lattner | 9794392 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 3156 | if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) { |
| 3157 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0I)) { |
| 3158 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 3159 | return R; |
| 3160 | } else if (isa<PHINode>(Op0I)) { |
| 3161 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 3162 | return NV; |
Chris Lattner | 9794392 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 3163 | } |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 3164 | |
| 3165 | // See if we can fold away this rem instruction. |
| 3166 | uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth(); |
| 3167 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 3168 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth), |
| 3169 | KnownZero, KnownOne)) |
| 3170 | return &I; |
Chris Lattner | 9794392 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 3171 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3172 | } |
| 3173 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3174 | return 0; |
| 3175 | } |
| 3176 | |
| 3177 | Instruction *InstCombiner::visitURem(BinaryOperator &I) { |
| 3178 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 3179 | |
| 3180 | if (Instruction *common = commonIRemTransforms(I)) |
| 3181 | return common; |
| 3182 | |
| 3183 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
| 3184 | // X urem C^2 -> X and C |
| 3185 | // Check to see if this is an unsigned remainder with an exact power of 2, |
| 3186 | // if so, convert to a bitwise and. |
| 3187 | if (ConstantInt *C = dyn_cast<ConstantInt>(RHS)) |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 3188 | if (C->getValue().isPowerOf2()) |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3189 | return BinaryOperator::createAnd(Op0, SubOne(C)); |
| 3190 | } |
| 3191 | |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 3192 | if (Instruction *RHSI = dyn_cast<Instruction>(I.getOperand(1))) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3193 | // Turn A % (C << N), where C is 2^k, into A & ((C << N)-1) |
| 3194 | if (RHSI->getOpcode() == Instruction::Shl && |
| 3195 | isa<ConstantInt>(RHSI->getOperand(0))) { |
Zhou Sheng | 0fc5095 | 2007-03-25 05:01:29 +0000 | [diff] [blame] | 3196 | if (cast<ConstantInt>(RHSI->getOperand(0))->getValue().isPowerOf2()) { |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 3197 | Constant *N1 = ConstantInt::getAllOnesValue(I.getType()); |
| 3198 | Value *Add = InsertNewInstBefore(BinaryOperator::createAdd(RHSI, N1, |
| 3199 | "tmp"), I); |
| 3200 | return BinaryOperator::createAnd(Op0, Add); |
| 3201 | } |
| 3202 | } |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3203 | } |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 3204 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3205 | // urem X, (select Cond, 2^C1, 2^C2) --> select Cond, (and X, C1), (and X, C2) |
| 3206 | // where C1&C2 are powers of two. |
| 3207 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { |
| 3208 | if (ConstantInt *STO = dyn_cast<ConstantInt>(SI->getOperand(1))) |
| 3209 | if (ConstantInt *SFO = dyn_cast<ConstantInt>(SI->getOperand(2))) { |
| 3210 | // STO == 0 and SFO == 0 handled above. |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 3211 | if ((STO->getValue().isPowerOf2()) && |
| 3212 | (SFO->getValue().isPowerOf2())) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3213 | Value *TrueAnd = InsertNewInstBefore( |
| 3214 | BinaryOperator::createAnd(Op0, SubOne(STO), SI->getName()+".t"), I); |
| 3215 | Value *FalseAnd = InsertNewInstBefore( |
| 3216 | BinaryOperator::createAnd(Op0, SubOne(SFO), SI->getName()+".f"), I); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 3217 | return SelectInst::Create(SI->getOperand(0), TrueAnd, FalseAnd); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3218 | } |
| 3219 | } |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 3220 | } |
| 3221 | |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3222 | return 0; |
| 3223 | } |
| 3224 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3225 | Instruction *InstCombiner::visitSRem(BinaryOperator &I) { |
| 3226 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 3227 | |
Dan Gohman | cff5509 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 3228 | // Handle the integer rem common cases |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3229 | if (Instruction *common = commonIRemTransforms(I)) |
| 3230 | return common; |
| 3231 | |
| 3232 | if (Value *RHSNeg = dyn_castNegVal(Op1)) |
| 3233 | if (!isa<ConstantInt>(RHSNeg) || |
Zhou Sheng | 0fc5095 | 2007-03-25 05:01:29 +0000 | [diff] [blame] | 3234 | cast<ConstantInt>(RHSNeg)->getValue().isStrictlyPositive()) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3235 | // X % -Y -> X % Y |
| 3236 | AddUsesToWorkList(I); |
| 3237 | I.setOperand(1, RHSNeg); |
| 3238 | return &I; |
| 3239 | } |
| 3240 | |
Dan Gohman | cff5509 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 3241 | // 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] | 3242 | // unsigned inputs), turn this into a urem. |
Dan Gohman | cff5509 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 3243 | if (I.getType()->isInteger()) { |
| 3244 | APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits())); |
| 3245 | if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) { |
| 3246 | // X srem Y -> X urem Y, iff X and Y don't have sign bit set |
| 3247 | return BinaryOperator::createURem(Op0, Op1, I.getName()); |
| 3248 | } |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3249 | } |
| 3250 | |
| 3251 | return 0; |
| 3252 | } |
| 3253 | |
| 3254 | Instruction *InstCombiner::visitFRem(BinaryOperator &I) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3255 | return commonRemTransforms(I); |
| 3256 | } |
| 3257 | |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 3258 | // isMaxValueMinusOne - return true if this is Max-1 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3259 | static bool isMaxValueMinusOne(const ConstantInt *C, bool isSigned) { |
Reid Spencer | 3a2a9fb | 2007-03-19 21:10:28 +0000 | [diff] [blame] | 3260 | uint32_t TypeBits = C->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 3261 | if (!isSigned) |
| 3262 | return C->getValue() == APInt::getAllOnesValue(TypeBits) - 1; |
| 3263 | return C->getValue() == APInt::getSignedMaxValue(TypeBits)-1; |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 3264 | } |
| 3265 | |
| 3266 | // isMinValuePlusOne - return true if this is Min+1 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3267 | static bool isMinValuePlusOne(const ConstantInt *C, bool isSigned) { |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 3268 | if (!isSigned) |
| 3269 | return C->getValue() == 1; // unsigned |
| 3270 | |
| 3271 | // Calculate 1111111111000000000000 |
| 3272 | uint32_t TypeBits = C->getType()->getPrimitiveSizeInBits(); |
| 3273 | return C->getValue() == APInt::getSignedMinValue(TypeBits)+1; |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 3274 | } |
| 3275 | |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 3276 | // isOneBitSet - Return true if there is exactly one bit set in the specified |
| 3277 | // constant. |
| 3278 | static bool isOneBitSet(const ConstantInt *CI) { |
Reid Spencer | 5f6a895 | 2007-03-20 00:16:52 +0000 | [diff] [blame] | 3279 | return CI->getValue().isPowerOf2(); |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 3280 | } |
| 3281 | |
Chris Lattner | b20ba0a | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 3282 | // isHighOnes - Return true if the constant is of the form 1+0+. |
| 3283 | // This is the same as lowones(~X). |
| 3284 | static bool isHighOnes(const ConstantInt *CI) { |
Zhou Sheng | 2cde46c | 2007-03-20 12:49:06 +0000 | [diff] [blame] | 3285 | return (~CI->getValue() + 1).isPowerOf2(); |
Chris Lattner | b20ba0a | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 3286 | } |
| 3287 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3288 | /// getICmpCode - Encode a icmp predicate into a three bit mask. These bits |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3289 | /// are carefully arranged to allow folding of expressions such as: |
| 3290 | /// |
| 3291 | /// (A < B) | (A > B) --> (A != B) |
| 3292 | /// |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3293 | /// Note that this is only valid if the first and second predicates have the |
| 3294 | /// same sign. Is illegal to do: (A u< B) | (A s> B) |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3295 | /// |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3296 | /// Three bits are used to represent the condition, as follows: |
| 3297 | /// 0 A > B |
| 3298 | /// 1 A == B |
| 3299 | /// 2 A < B |
| 3300 | /// |
| 3301 | /// <=> Value Definition |
| 3302 | /// 000 0 Always false |
| 3303 | /// 001 1 A > B |
| 3304 | /// 010 2 A == B |
| 3305 | /// 011 3 A >= B |
| 3306 | /// 100 4 A < B |
| 3307 | /// 101 5 A != B |
| 3308 | /// 110 6 A <= B |
| 3309 | /// 111 7 Always true |
| 3310 | /// |
| 3311 | static unsigned getICmpCode(const ICmpInst *ICI) { |
| 3312 | switch (ICI->getPredicate()) { |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3313 | // False -> 0 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3314 | case ICmpInst::ICMP_UGT: return 1; // 001 |
| 3315 | case ICmpInst::ICMP_SGT: return 1; // 001 |
| 3316 | case ICmpInst::ICMP_EQ: return 2; // 010 |
| 3317 | case ICmpInst::ICMP_UGE: return 3; // 011 |
| 3318 | case ICmpInst::ICMP_SGE: return 3; // 011 |
| 3319 | case ICmpInst::ICMP_ULT: return 4; // 100 |
| 3320 | case ICmpInst::ICMP_SLT: return 4; // 100 |
| 3321 | case ICmpInst::ICMP_NE: return 5; // 101 |
| 3322 | case ICmpInst::ICMP_ULE: return 6; // 110 |
| 3323 | case ICmpInst::ICMP_SLE: return 6; // 110 |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3324 | // True -> 7 |
| 3325 | default: |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3326 | assert(0 && "Invalid ICmp predicate!"); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3327 | return 0; |
| 3328 | } |
| 3329 | } |
| 3330 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3331 | /// getICmpValue - This is the complement of getICmpCode, which turns an |
| 3332 | /// 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] | 3333 | /// new ICmp instruction. The sign is passed in to determine which kind |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3334 | /// of predicate to use in new icmp instructions. |
| 3335 | static Value *getICmpValue(bool sign, unsigned code, Value *LHS, Value *RHS) { |
| 3336 | switch (code) { |
| 3337 | default: assert(0 && "Illegal ICmp code!"); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3338 | case 0: return ConstantInt::getFalse(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3339 | case 1: |
| 3340 | if (sign) |
| 3341 | return new ICmpInst(ICmpInst::ICMP_SGT, LHS, RHS); |
| 3342 | else |
| 3343 | return new ICmpInst(ICmpInst::ICMP_UGT, LHS, RHS); |
| 3344 | case 2: return new ICmpInst(ICmpInst::ICMP_EQ, LHS, RHS); |
| 3345 | case 3: |
| 3346 | if (sign) |
| 3347 | return new ICmpInst(ICmpInst::ICMP_SGE, LHS, RHS); |
| 3348 | else |
| 3349 | return new ICmpInst(ICmpInst::ICMP_UGE, LHS, RHS); |
| 3350 | case 4: |
| 3351 | if (sign) |
| 3352 | return new ICmpInst(ICmpInst::ICMP_SLT, LHS, RHS); |
| 3353 | else |
| 3354 | return new ICmpInst(ICmpInst::ICMP_ULT, LHS, RHS); |
| 3355 | case 5: return new ICmpInst(ICmpInst::ICMP_NE, LHS, RHS); |
| 3356 | case 6: |
| 3357 | if (sign) |
| 3358 | return new ICmpInst(ICmpInst::ICMP_SLE, LHS, RHS); |
| 3359 | else |
| 3360 | return new ICmpInst(ICmpInst::ICMP_ULE, LHS, RHS); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3361 | case 7: return ConstantInt::getTrue(); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3362 | } |
| 3363 | } |
| 3364 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3365 | static bool PredicatesFoldable(ICmpInst::Predicate p1, ICmpInst::Predicate p2) { |
| 3366 | return (ICmpInst::isSignedPredicate(p1) == ICmpInst::isSignedPredicate(p2)) || |
| 3367 | (ICmpInst::isSignedPredicate(p1) && |
| 3368 | (p2 == ICmpInst::ICMP_EQ || p2 == ICmpInst::ICMP_NE)) || |
| 3369 | (ICmpInst::isSignedPredicate(p2) && |
| 3370 | (p1 == ICmpInst::ICMP_EQ || p1 == ICmpInst::ICMP_NE)); |
| 3371 | } |
| 3372 | |
| 3373 | namespace { |
| 3374 | // FoldICmpLogical - Implements (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B) |
| 3375 | struct FoldICmpLogical { |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3376 | InstCombiner &IC; |
| 3377 | Value *LHS, *RHS; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3378 | ICmpInst::Predicate pred; |
| 3379 | FoldICmpLogical(InstCombiner &ic, ICmpInst *ICI) |
| 3380 | : IC(ic), LHS(ICI->getOperand(0)), RHS(ICI->getOperand(1)), |
| 3381 | pred(ICI->getPredicate()) {} |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3382 | bool shouldApply(Value *V) const { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3383 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(V)) |
| 3384 | if (PredicatesFoldable(pred, ICI->getPredicate())) |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 3385 | return ((ICI->getOperand(0) == LHS && ICI->getOperand(1) == RHS) || |
| 3386 | (ICI->getOperand(0) == RHS && ICI->getOperand(1) == LHS)); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3387 | return false; |
| 3388 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3389 | Instruction *apply(Instruction &Log) const { |
| 3390 | ICmpInst *ICI = cast<ICmpInst>(Log.getOperand(0)); |
| 3391 | if (ICI->getOperand(0) != LHS) { |
| 3392 | assert(ICI->getOperand(1) == LHS); |
| 3393 | ICI->swapOperands(); // Swap the LHS and RHS of the ICmp |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3394 | } |
| 3395 | |
Chris Lattner | bc1dbfc | 2007-03-13 14:27:42 +0000 | [diff] [blame] | 3396 | ICmpInst *RHSICI = cast<ICmpInst>(Log.getOperand(1)); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3397 | unsigned LHSCode = getICmpCode(ICI); |
Chris Lattner | bc1dbfc | 2007-03-13 14:27:42 +0000 | [diff] [blame] | 3398 | unsigned RHSCode = getICmpCode(RHSICI); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3399 | unsigned Code; |
| 3400 | switch (Log.getOpcode()) { |
| 3401 | case Instruction::And: Code = LHSCode & RHSCode; break; |
| 3402 | case Instruction::Or: Code = LHSCode | RHSCode; break; |
| 3403 | case Instruction::Xor: Code = LHSCode ^ RHSCode; break; |
Chris Lattner | 021c190 | 2003-09-22 20:33:34 +0000 | [diff] [blame] | 3404 | default: assert(0 && "Illegal logical opcode!"); return 0; |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3405 | } |
| 3406 | |
Chris Lattner | bc1dbfc | 2007-03-13 14:27:42 +0000 | [diff] [blame] | 3407 | bool isSigned = ICmpInst::isSignedPredicate(RHSICI->getPredicate()) || |
| 3408 | ICmpInst::isSignedPredicate(ICI->getPredicate()); |
| 3409 | |
| 3410 | Value *RV = getICmpValue(isSigned, Code, LHS, RHS); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3411 | if (Instruction *I = dyn_cast<Instruction>(RV)) |
| 3412 | return I; |
| 3413 | // Otherwise, it's a constant boolean value... |
| 3414 | return IC.ReplaceInstUsesWith(Log, RV); |
| 3415 | } |
| 3416 | }; |
Chris Lattner | d23b5ba | 2006-11-15 04:53:24 +0000 | [diff] [blame] | 3417 | } // end anonymous namespace |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3418 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3419 | // OptAndOp - This handles expressions of the form ((val OP C1) & C2). Where |
| 3420 | // 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] | 3421 | // guaranteed to be a binary operator. |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3422 | Instruction *InstCombiner::OptAndOp(Instruction *Op, |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3423 | ConstantInt *OpRHS, |
| 3424 | ConstantInt *AndRHS, |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3425 | BinaryOperator &TheAnd) { |
| 3426 | Value *X = Op->getOperand(0); |
Chris Lattner | 76f7fe2 | 2004-01-12 19:47:05 +0000 | [diff] [blame] | 3427 | Constant *Together = 0; |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3428 | if (!Op->isShift()) |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 3429 | Together = And(AndRHS, OpRHS); |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3430 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3431 | switch (Op->getOpcode()) { |
| 3432 | case Instruction::Xor: |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3433 | if (Op->hasOneUse()) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3434 | // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2) |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3435 | Instruction *And = BinaryOperator::createAnd(X, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3436 | InsertNewInstBefore(And, TheAnd); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3437 | And->takeName(Op); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3438 | return BinaryOperator::createXor(And, Together); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3439 | } |
| 3440 | break; |
| 3441 | case Instruction::Or: |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3442 | if (Together == AndRHS) // (X | C) & C --> C |
| 3443 | return ReplaceInstUsesWith(TheAnd, AndRHS); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3444 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3445 | if (Op->hasOneUse() && Together != OpRHS) { |
| 3446 | // (X | C1) & C2 --> (X | (C1&C2)) & C2 |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3447 | Instruction *Or = BinaryOperator::createOr(X, Together); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3448 | InsertNewInstBefore(Or, TheAnd); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3449 | Or->takeName(Op); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3450 | return BinaryOperator::createAnd(Or, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3451 | } |
| 3452 | break; |
| 3453 | case Instruction::Add: |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 3454 | if (Op->hasOneUse()) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3455 | // Adding a one to a single bit bit-field should be turned into an XOR |
| 3456 | // of the bit. First thing to check is to see if this AND is with a |
| 3457 | // single bit constant. |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3458 | const APInt& AndRHSV = cast<ConstantInt>(AndRHS)->getValue(); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3459 | |
| 3460 | // If there is only one bit set... |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 3461 | if (isOneBitSet(cast<ConstantInt>(AndRHS))) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3462 | // Ok, at this point, we know that we are masking the result of the |
| 3463 | // ADD down to exactly one bit. If the constant we are adding has |
| 3464 | // no bits set below this bit, then we can eliminate the ADD. |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3465 | const APInt& AddRHS = cast<ConstantInt>(OpRHS)->getValue(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3466 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3467 | // Check to see if any bits below the one bit set in AndRHSV are set. |
| 3468 | if ((AddRHS & (AndRHSV-1)) == 0) { |
| 3469 | // If not, the only thing that can effect the output of the AND is |
| 3470 | // the bit specified by AndRHSV. If that bit is set, the effect of |
| 3471 | // the XOR is to toggle the bit. If it is clear, then the ADD has |
| 3472 | // no effect. |
| 3473 | if ((AddRHS & AndRHSV) == 0) { // Bit is not set, noop |
| 3474 | TheAnd.setOperand(0, X); |
| 3475 | return &TheAnd; |
| 3476 | } else { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3477 | // Pull the XOR out of the AND. |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3478 | Instruction *NewAnd = BinaryOperator::createAnd(X, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3479 | InsertNewInstBefore(NewAnd, TheAnd); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3480 | NewAnd->takeName(Op); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3481 | return BinaryOperator::createXor(NewAnd, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3482 | } |
| 3483 | } |
| 3484 | } |
| 3485 | } |
| 3486 | break; |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 3487 | |
| 3488 | case Instruction::Shl: { |
| 3489 | // We know that the AND will not produce any of the bits shifted in, so if |
| 3490 | // the anded constant includes them, clear them now! |
| 3491 | // |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3492 | uint32_t BitWidth = AndRHS->getType()->getBitWidth(); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3493 | uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3494 | APInt ShlMask(APInt::getHighBitsSet(BitWidth, BitWidth-OpRHSVal)); |
| 3495 | ConstantInt *CI = ConstantInt::get(AndRHS->getValue() & ShlMask); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3496 | |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3497 | if (CI->getValue() == ShlMask) { |
| 3498 | // Masking out bits that the shift already masks |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 3499 | return ReplaceInstUsesWith(TheAnd, Op); // No need for the and. |
| 3500 | } else if (CI != AndRHS) { // Reducing bits set in and. |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 3501 | TheAnd.setOperand(1, CI); |
| 3502 | return &TheAnd; |
| 3503 | } |
| 3504 | break; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3505 | } |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 3506 | case Instruction::LShr: |
| 3507 | { |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 3508 | // We know that the AND will not produce any of the bits shifted in, so if |
| 3509 | // the anded constant includes them, clear them now! This only applies to |
| 3510 | // unsigned shifts, because a signed shr may bring in set bits! |
| 3511 | // |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3512 | uint32_t BitWidth = AndRHS->getType()->getBitWidth(); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3513 | uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3514 | APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal)); |
| 3515 | ConstantInt *CI = ConstantInt::get(AndRHS->getValue() & ShrMask); |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 3516 | |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3517 | if (CI->getValue() == ShrMask) { |
| 3518 | // Masking out bits that the shift already masks. |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 3519 | return ReplaceInstUsesWith(TheAnd, Op); |
| 3520 | } else if (CI != AndRHS) { |
| 3521 | TheAnd.setOperand(1, CI); // Reduce bits set in and cst. |
| 3522 | return &TheAnd; |
| 3523 | } |
| 3524 | break; |
| 3525 | } |
| 3526 | case Instruction::AShr: |
| 3527 | // Signed shr. |
| 3528 | // See if this is shifting in some sign extension, then masking it out |
| 3529 | // with an and. |
| 3530 | if (Op->hasOneUse()) { |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3531 | uint32_t BitWidth = AndRHS->getType()->getBitWidth(); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3532 | uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3533 | APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal)); |
| 3534 | Constant *C = ConstantInt::get(AndRHS->getValue() & ShrMask); |
Reid Spencer | 7eb7638 | 2006-12-13 17:19:09 +0000 | [diff] [blame] | 3535 | if (C == AndRHS) { // Masking out bits shifted in. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 3536 | // (Val ashr C1) & C2 -> (Val lshr C1) & C2 |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 3537 | // Make the argument unsigned. |
| 3538 | Value *ShVal = Op->getOperand(0); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3539 | ShVal = InsertNewInstBefore( |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 3540 | BinaryOperator::createLShr(ShVal, OpRHS, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3541 | Op->getName()), TheAnd); |
Reid Spencer | 7eb7638 | 2006-12-13 17:19:09 +0000 | [diff] [blame] | 3542 | return BinaryOperator::createAnd(ShVal, AndRHS, TheAnd.getName()); |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 3543 | } |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 3544 | } |
| 3545 | break; |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3546 | } |
| 3547 | return 0; |
| 3548 | } |
| 3549 | |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 3550 | |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3551 | /// InsertRangeTest - Emit a computation of: (V >= Lo && V < Hi) if Inside is |
| 3552 | /// 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] | 3553 | /// (V-Lo) <u Hi-Lo. This method expects that Lo <= Hi. isSigned indicates |
| 3554 | /// 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] | 3555 | /// insert new instructions. |
| 3556 | Instruction *InstCombiner::InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3557 | bool isSigned, bool Inside, |
| 3558 | Instruction &IB) { |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3559 | assert(cast<ConstantInt>(ConstantExpr::getICmp((isSigned ? |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 3560 | ICmpInst::ICMP_SLE:ICmpInst::ICMP_ULE), Lo, Hi))->getZExtValue() && |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3561 | "Lo is not <= Hi in range emission code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3562 | |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3563 | if (Inside) { |
| 3564 | if (Lo == Hi) // Trivially false. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3565 | return new ICmpInst(ICmpInst::ICMP_NE, V, V); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3566 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3567 | // V >= Min && V < Hi --> V < Hi |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3568 | if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) { |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 3569 | ICmpInst::Predicate pred = (isSigned ? |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3570 | ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT); |
| 3571 | return new ICmpInst(pred, V, Hi); |
| 3572 | } |
| 3573 | |
| 3574 | // Emit V-Lo <u Hi-Lo |
| 3575 | Constant *NegLo = ConstantExpr::getNeg(Lo); |
| 3576 | Instruction *Add = BinaryOperator::createAdd(V, NegLo, V->getName()+".off"); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3577 | InsertNewInstBefore(Add, IB); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3578 | Constant *UpperBound = ConstantExpr::getAdd(NegLo, Hi); |
| 3579 | return new ICmpInst(ICmpInst::ICMP_ULT, Add, UpperBound); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3580 | } |
| 3581 | |
| 3582 | if (Lo == Hi) // Trivially true. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3583 | return new ICmpInst(ICmpInst::ICMP_EQ, V, V); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3584 | |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 3585 | // V < Min || V >= Hi -> V > Hi-1 |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3586 | Hi = SubOne(cast<ConstantInt>(Hi)); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3587 | if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3588 | ICmpInst::Predicate pred = (isSigned ? |
| 3589 | ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT); |
| 3590 | return new ICmpInst(pred, V, Hi); |
| 3591 | } |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3592 | |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 3593 | // Emit V-Lo >u Hi-1-Lo |
| 3594 | // Note that Hi has already had one subtracted from it, above. |
| 3595 | ConstantInt *NegLo = cast<ConstantInt>(ConstantExpr::getNeg(Lo)); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3596 | Instruction *Add = BinaryOperator::createAdd(V, NegLo, V->getName()+".off"); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3597 | InsertNewInstBefore(Add, IB); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3598 | Constant *LowerBound = ConstantExpr::getAdd(NegLo, Hi); |
| 3599 | return new ICmpInst(ICmpInst::ICMP_UGT, Add, LowerBound); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3600 | } |
| 3601 | |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3602 | // isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with |
| 3603 | // any number of 0s on either side. The 1s are allowed to wrap from LSB to |
| 3604 | // MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is |
| 3605 | // not, since all 1s are not contiguous. |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 3606 | static bool isRunOfOnes(ConstantInt *Val, uint32_t &MB, uint32_t &ME) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3607 | const APInt& V = Val->getValue(); |
Reid Spencer | f244252 | 2007-03-24 00:42:08 +0000 | [diff] [blame] | 3608 | uint32_t BitWidth = Val->getType()->getBitWidth(); |
| 3609 | if (!APIntOps::isShiftedMask(BitWidth, V)) return false; |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3610 | |
| 3611 | // look for the first zero bit after the run of ones |
Reid Spencer | f244252 | 2007-03-24 00:42:08 +0000 | [diff] [blame] | 3612 | MB = BitWidth - ((V - 1) ^ V).countLeadingZeros(); |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3613 | // look for the first non-zero bit |
Reid Spencer | f244252 | 2007-03-24 00:42:08 +0000 | [diff] [blame] | 3614 | ME = V.getActiveBits(); |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3615 | return true; |
| 3616 | } |
| 3617 | |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3618 | /// FoldLogicalPlusAnd - This is part of an expression (LHS +/- RHS) & Mask, |
| 3619 | /// where isSub determines whether the operator is a sub. If we can fold one of |
| 3620 | /// the following xforms: |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3621 | /// |
| 3622 | /// ((A & N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == Mask |
| 3623 | /// ((A | N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0 |
| 3624 | /// ((A ^ N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0 |
| 3625 | /// |
| 3626 | /// return (A +/- B). |
| 3627 | /// |
| 3628 | Value *InstCombiner::FoldLogicalPlusAnd(Value *LHS, Value *RHS, |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3629 | ConstantInt *Mask, bool isSub, |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3630 | Instruction &I) { |
| 3631 | Instruction *LHSI = dyn_cast<Instruction>(LHS); |
| 3632 | if (!LHSI || LHSI->getNumOperands() != 2 || |
| 3633 | !isa<ConstantInt>(LHSI->getOperand(1))) return 0; |
| 3634 | |
| 3635 | ConstantInt *N = cast<ConstantInt>(LHSI->getOperand(1)); |
| 3636 | |
| 3637 | switch (LHSI->getOpcode()) { |
| 3638 | default: return 0; |
| 3639 | case Instruction::And: |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 3640 | if (And(N, Mask) == Mask) { |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3641 | // 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] | 3642 | if ((Mask->getValue().countLeadingZeros() + |
| 3643 | Mask->getValue().countPopulation()) == |
| 3644 | Mask->getValue().getBitWidth()) |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3645 | break; |
| 3646 | |
| 3647 | // Otherwise, if Mask is 0+1+0+, and if B is known to have the low 0+ |
| 3648 | // part, we don't need any explicit masks to take them out of A. If that |
| 3649 | // is all N is, ignore it. |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 3650 | uint32_t MB = 0, ME = 0; |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3651 | if (isRunOfOnes(Mask, MB, ME)) { // begin/end bit of run, inclusive |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 3652 | uint32_t BitWidth = cast<IntegerType>(RHS->getType())->getBitWidth(); |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3653 | APInt Mask(APInt::getLowBitsSet(BitWidth, MB-1)); |
Chris Lattner | 3bedbd9 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3654 | if (MaskedValueIsZero(RHS, Mask)) |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3655 | break; |
| 3656 | } |
| 3657 | } |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3658 | return 0; |
| 3659 | case Instruction::Or: |
| 3660 | case Instruction::Xor: |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3661 | // 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] | 3662 | if ((Mask->getValue().countLeadingZeros() + |
| 3663 | Mask->getValue().countPopulation()) == Mask->getValue().getBitWidth() |
Reid Spencer | 6eb0d99 | 2007-03-26 23:58:26 +0000 | [diff] [blame] | 3664 | && And(N, Mask)->isZero()) |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3665 | break; |
| 3666 | return 0; |
| 3667 | } |
| 3668 | |
| 3669 | Instruction *New; |
| 3670 | if (isSub) |
| 3671 | New = BinaryOperator::createSub(LHSI->getOperand(0), RHS, "fold"); |
| 3672 | else |
| 3673 | New = BinaryOperator::createAdd(LHSI->getOperand(0), RHS, "fold"); |
| 3674 | return InsertNewInstBefore(New, I); |
| 3675 | } |
| 3676 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3677 | Instruction *InstCombiner::visitAnd(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 3678 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3679 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3680 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3681 | if (isa<UndefValue>(Op1)) // X & undef -> 0 |
| 3682 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 3683 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3684 | // and X, X = X |
| 3685 | if (Op0 == Op1) |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 3686 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3687 | |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3688 | // 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] | 3689 | // purpose is to compute bits we don't care about. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 3690 | if (!isa<VectorType>(I.getType())) { |
Reid Spencer | a03d45f | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 3691 | uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth(); |
| 3692 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 3693 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth), |
Chris Lattner | 696ee0a | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3694 | KnownZero, KnownOne)) |
Reid Spencer | 6eb0d99 | 2007-03-26 23:58:26 +0000 | [diff] [blame] | 3695 | return &I; |
Chris Lattner | 696ee0a | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3696 | } else { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 3697 | if (ConstantVector *CP = dyn_cast<ConstantVector>(Op1)) { |
Chris Lattner | 041a6c9 | 2007-06-15 05:26:55 +0000 | [diff] [blame] | 3698 | if (CP->isAllOnesValue()) // X & <-1,-1> -> X |
Chris Lattner | 696ee0a | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3699 | return ReplaceInstUsesWith(I, I.getOperand(0)); |
Chris Lattner | 041a6c9 | 2007-06-15 05:26:55 +0000 | [diff] [blame] | 3700 | } else if (isa<ConstantAggregateZero>(Op1)) { |
| 3701 | return ReplaceInstUsesWith(I, Op1); // X & <0,0> -> <0,0> |
Chris Lattner | 696ee0a | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3702 | } |
| 3703 | } |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 3704 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3705 | if (ConstantInt *AndRHS = dyn_cast<ConstantInt>(Op1)) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3706 | const APInt& AndRHSMask = AndRHS->getValue(); |
| 3707 | APInt NotAndRHS(~AndRHSMask); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3708 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3709 | // Optimize a variety of ((val OP C1) & C2) combinations... |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3710 | if (isa<BinaryOperator>(Op0)) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3711 | Instruction *Op0I = cast<Instruction>(Op0); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3712 | Value *Op0LHS = Op0I->getOperand(0); |
| 3713 | Value *Op0RHS = Op0I->getOperand(1); |
| 3714 | switch (Op0I->getOpcode()) { |
| 3715 | case Instruction::Xor: |
| 3716 | case Instruction::Or: |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 3717 | // If the mask is only needed on one incoming arm, push it up. |
| 3718 | if (Op0I->hasOneUse()) { |
| 3719 | if (MaskedValueIsZero(Op0LHS, NotAndRHS)) { |
| 3720 | // Not masking anything out for the LHS, move to RHS. |
| 3721 | Instruction *NewRHS = BinaryOperator::createAnd(Op0RHS, AndRHS, |
| 3722 | Op0RHS->getName()+".masked"); |
| 3723 | InsertNewInstBefore(NewRHS, I); |
| 3724 | return BinaryOperator::create( |
| 3725 | cast<BinaryOperator>(Op0I)->getOpcode(), Op0LHS, NewRHS); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3726 | } |
Chris Lattner | 3bedbd9 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3727 | if (!isa<Constant>(Op0RHS) && |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 3728 | MaskedValueIsZero(Op0RHS, NotAndRHS)) { |
| 3729 | // Not masking anything out for the RHS, move to LHS. |
| 3730 | Instruction *NewLHS = BinaryOperator::createAnd(Op0LHS, AndRHS, |
| 3731 | Op0LHS->getName()+".masked"); |
| 3732 | InsertNewInstBefore(NewLHS, I); |
| 3733 | return BinaryOperator::create( |
| 3734 | cast<BinaryOperator>(Op0I)->getOpcode(), NewLHS, Op0RHS); |
| 3735 | } |
| 3736 | } |
| 3737 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3738 | break; |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3739 | case Instruction::Add: |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3740 | // ((A & N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == AndRHS. |
| 3741 | // ((A | N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0 |
| 3742 | // ((A ^ N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0 |
| 3743 | if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, false, I)) |
| 3744 | return BinaryOperator::createAnd(V, AndRHS); |
| 3745 | if (Value *V = FoldLogicalPlusAnd(Op0RHS, Op0LHS, AndRHS, false, I)) |
| 3746 | return BinaryOperator::createAnd(V, AndRHS); // Add commutes |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3747 | break; |
| 3748 | |
| 3749 | case Instruction::Sub: |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3750 | // ((A & N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == AndRHS. |
| 3751 | // ((A | N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0 |
| 3752 | // ((A ^ N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0 |
| 3753 | if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, true, I)) |
| 3754 | return BinaryOperator::createAnd(V, AndRHS); |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3755 | break; |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3756 | } |
| 3757 | |
Chris Lattner | 5840326 | 2003-07-23 19:25:52 +0000 | [diff] [blame] | 3758 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3759 | if (Instruction *Res = OptAndOp(Op0I, Op0CI, AndRHS, I)) |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3760 | return Res; |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3761 | } else if (CastInst *CI = dyn_cast<CastInst>(Op0)) { |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3762 | // If this is an integer truncation or change from signed-to-unsigned, and |
| 3763 | // if the source is an and/or with immediate, transform it. This |
| 3764 | // frequently occurs for bitfield accesses. |
| 3765 | if (Instruction *CastOp = dyn_cast<Instruction>(CI->getOperand(0))) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3766 | if ((isa<TruncInst>(CI) || isa<BitCastInst>(CI)) && |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3767 | CastOp->getNumOperands() == 2) |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 3768 | if (ConstantInt *AndCI = dyn_cast<ConstantInt>(CastOp->getOperand(1))) { |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3769 | if (CastOp->getOpcode() == Instruction::And) { |
| 3770 | // Change: and (cast (and X, C1) to T), C2 |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3771 | // into : and (cast X to T), trunc_or_bitcast(C1)&C2 |
| 3772 | // This will fold the two constants together, which may allow |
| 3773 | // other simplifications. |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 3774 | Instruction *NewCast = CastInst::createTruncOrBitCast( |
| 3775 | CastOp->getOperand(0), I.getType(), |
| 3776 | CastOp->getName()+".shrunk"); |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3777 | NewCast = InsertNewInstBefore(NewCast, I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3778 | // trunc_or_bitcast(C1)&C2 |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 3779 | Constant *C3 = ConstantExpr::getTruncOrBitCast(AndCI,I.getType()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3780 | C3 = ConstantExpr::getAnd(C3, AndRHS); |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3781 | return BinaryOperator::createAnd(NewCast, C3); |
| 3782 | } else if (CastOp->getOpcode() == Instruction::Or) { |
| 3783 | // Change: and (cast (or X, C1) to T), C2 |
| 3784 | // into : trunc(C1)&C2 iff trunc(C1)&C2 == C2 |
Chris Lattner | bb4e7b2 | 2006-12-12 19:11:20 +0000 | [diff] [blame] | 3785 | Constant *C3 = ConstantExpr::getTruncOrBitCast(AndCI,I.getType()); |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3786 | if (ConstantExpr::getAnd(C3, AndRHS) == AndRHS) // trunc(C1)&C2 |
| 3787 | return ReplaceInstUsesWith(I, AndRHS); |
| 3788 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 3789 | } |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3790 | } |
Chris Lattner | 06782f8 | 2003-07-23 19:36:21 +0000 | [diff] [blame] | 3791 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3792 | |
| 3793 | // Try to fold constant and into select arguments. |
| 3794 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3795 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3796 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 3797 | if (isa<PHINode>(Op0)) |
| 3798 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 3799 | return NV; |
Chris Lattner | c6a8aff | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 3800 | } |
| 3801 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 3802 | Value *Op0NotVal = dyn_castNotVal(Op0); |
| 3803 | Value *Op1NotVal = dyn_castNotVal(Op1); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3804 | |
Chris Lattner | 5b62aa7 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 3805 | if (Op0NotVal == Op1 || Op1NotVal == Op0) // A & ~A == ~A & A == 0 |
| 3806 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 3807 | |
Misha Brukman | cb6267b | 2004-07-30 12:50:08 +0000 | [diff] [blame] | 3808 | // (~A & ~B) == (~(A | B)) - De Morgan's Law |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 3809 | if (Op0NotVal && Op1NotVal && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3810 | Instruction *Or = BinaryOperator::createOr(Op0NotVal, Op1NotVal, |
| 3811 | I.getName()+".demorgan"); |
Chris Lattner | c6a8aff | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 3812 | InsertNewInstBefore(Or, I); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3813 | return BinaryOperator::createNot(Or); |
| 3814 | } |
Chris Lattner | 2082ad9 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3815 | |
| 3816 | { |
Chris Lattner | 003b620 | 2007-06-15 05:58:24 +0000 | [diff] [blame] | 3817 | Value *A = 0, *B = 0, *C = 0, *D = 0; |
| 3818 | if (match(Op0, m_Or(m_Value(A), m_Value(B)))) { |
Chris Lattner | 2082ad9 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3819 | if (A == Op1 || B == Op1) // (A | ?) & A --> A |
| 3820 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 003b620 | 2007-06-15 05:58:24 +0000 | [diff] [blame] | 3821 | |
| 3822 | // (A|B) & ~(A&B) -> A^B |
| 3823 | if (match(Op1, m_Not(m_And(m_Value(C), m_Value(D))))) { |
| 3824 | if ((A == C && B == D) || (A == D && B == C)) |
| 3825 | return BinaryOperator::createXor(A, B); |
| 3826 | } |
| 3827 | } |
| 3828 | |
| 3829 | if (match(Op1, m_Or(m_Value(A), m_Value(B)))) { |
Chris Lattner | 2082ad9 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3830 | if (A == Op0 || B == Op0) // A & (A | ?) --> A |
| 3831 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 003b620 | 2007-06-15 05:58:24 +0000 | [diff] [blame] | 3832 | |
| 3833 | // ~(A&B) & (A|B) -> A^B |
| 3834 | if (match(Op0, m_Not(m_And(m_Value(C), m_Value(D))))) { |
| 3835 | if ((A == C && B == D) || (A == D && B == C)) |
| 3836 | return BinaryOperator::createXor(A, B); |
| 3837 | } |
| 3838 | } |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 3839 | |
| 3840 | if (Op0->hasOneUse() && |
| 3841 | match(Op0, m_Xor(m_Value(A), m_Value(B)))) { |
| 3842 | if (A == Op1) { // (A^B)&A -> A&(A^B) |
| 3843 | I.swapOperands(); // Simplify below |
| 3844 | std::swap(Op0, Op1); |
| 3845 | } else if (B == Op1) { // (A^B)&B -> B&(B^A) |
| 3846 | cast<BinaryOperator>(Op0)->swapOperands(); |
| 3847 | I.swapOperands(); // Simplify below |
| 3848 | std::swap(Op0, Op1); |
| 3849 | } |
| 3850 | } |
| 3851 | if (Op1->hasOneUse() && |
| 3852 | match(Op1, m_Xor(m_Value(A), m_Value(B)))) { |
| 3853 | if (B == Op0) { // B&(A^B) -> B&(B^A) |
| 3854 | cast<BinaryOperator>(Op1)->swapOperands(); |
| 3855 | std::swap(A, B); |
| 3856 | } |
| 3857 | if (A == Op0) { // A&(A^B) -> A & ~B |
| 3858 | Instruction *NotB = BinaryOperator::createNot(B, "tmp"); |
| 3859 | InsertNewInstBefore(NotB, I); |
| 3860 | return BinaryOperator::createAnd(A, NotB); |
| 3861 | } |
| 3862 | } |
Chris Lattner | 2082ad9 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3863 | } |
| 3864 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3865 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(Op1)) { |
| 3866 | // (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B) |
| 3867 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS))) |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3868 | return R; |
| 3869 | |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3870 | Value *LHSVal, *RHSVal; |
| 3871 | ConstantInt *LHSCst, *RHSCst; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3872 | ICmpInst::Predicate LHSCC, RHSCC; |
| 3873 | if (match(Op0, m_ICmp(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst)))) |
| 3874 | if (match(RHS, m_ICmp(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst)))) |
| 3875 | if (LHSVal == RHSVal && // Found (X icmp C1) & (X icmp C2) |
| 3876 | // ICMP_[GL]E X, CST is folded to ICMP_[GL]T elsewhere. |
| 3877 | LHSCC != ICmpInst::ICMP_UGE && LHSCC != ICmpInst::ICMP_ULE && |
| 3878 | RHSCC != ICmpInst::ICMP_UGE && RHSCC != ICmpInst::ICMP_ULE && |
| 3879 | LHSCC != ICmpInst::ICMP_SGE && LHSCC != ICmpInst::ICMP_SLE && |
Chris Lattner | eec8b9a | 2007-11-22 23:47:13 +0000 | [diff] [blame] | 3880 | RHSCC != ICmpInst::ICMP_SGE && RHSCC != ICmpInst::ICMP_SLE && |
| 3881 | |
| 3882 | // Don't try to fold ICMP_SLT + ICMP_ULT. |
| 3883 | (ICmpInst::isEquality(LHSCC) || ICmpInst::isEquality(RHSCC) || |
| 3884 | ICmpInst::isSignedPredicate(LHSCC) == |
| 3885 | ICmpInst::isSignedPredicate(RHSCC))) { |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3886 | // Ensure that the larger constant is on the RHS. |
Chris Lattner | ee2b7a4 | 2008-01-13 20:59:02 +0000 | [diff] [blame] | 3887 | ICmpInst::Predicate GT; |
| 3888 | if (ICmpInst::isSignedPredicate(LHSCC) || |
| 3889 | (ICmpInst::isEquality(LHSCC) && |
| 3890 | ICmpInst::isSignedPredicate(RHSCC))) |
| 3891 | GT = ICmpInst::ICMP_SGT; |
| 3892 | else |
| 3893 | GT = ICmpInst::ICMP_UGT; |
| 3894 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3895 | Constant *Cmp = ConstantExpr::getICmp(GT, LHSCst, RHSCst); |
| 3896 | ICmpInst *LHS = cast<ICmpInst>(Op0); |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 3897 | if (cast<ConstantInt>(Cmp)->getZExtValue()) { |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3898 | std::swap(LHS, RHS); |
| 3899 | std::swap(LHSCst, RHSCst); |
| 3900 | std::swap(LHSCC, RHSCC); |
| 3901 | } |
| 3902 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3903 | // At this point, we know we have have two icmp instructions |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3904 | // comparing a value against two constants and and'ing the result |
| 3905 | // together. Because of the above check, we know that we only have |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3906 | // icmp eq, icmp ne, icmp [su]lt, and icmp [SU]gt here. We also know |
| 3907 | // (from the FoldICmpLogical check above), that the two constants |
| 3908 | // are not equal and that the larger constant is on the RHS |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3909 | assert(LHSCst != RHSCst && "Compares not folded above?"); |
| 3910 | |
| 3911 | switch (LHSCC) { |
| 3912 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3913 | case ICmpInst::ICMP_EQ: |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3914 | switch (RHSCC) { |
| 3915 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3916 | case ICmpInst::ICMP_EQ: // (X == 13 & X == 15) -> false |
| 3917 | case ICmpInst::ICMP_UGT: // (X == 13 & X > 15) -> false |
| 3918 | case ICmpInst::ICMP_SGT: // (X == 13 & X > 15) -> false |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3919 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3920 | case ICmpInst::ICMP_NE: // (X == 13 & X != 15) -> X == 13 |
| 3921 | case ICmpInst::ICMP_ULT: // (X == 13 & X < 15) -> X == 13 |
| 3922 | case ICmpInst::ICMP_SLT: // (X == 13 & X < 15) -> X == 13 |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3923 | return ReplaceInstUsesWith(I, LHS); |
| 3924 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3925 | case ICmpInst::ICMP_NE: |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3926 | switch (RHSCC) { |
| 3927 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3928 | case ICmpInst::ICMP_ULT: |
| 3929 | if (LHSCst == SubOne(RHSCst)) // (X != 13 & X u< 14) -> X < 13 |
| 3930 | return new ICmpInst(ICmpInst::ICMP_ULT, LHSVal, LHSCst); |
| 3931 | break; // (X != 13 & X u< 15) -> no change |
| 3932 | case ICmpInst::ICMP_SLT: |
| 3933 | if (LHSCst == SubOne(RHSCst)) // (X != 13 & X s< 14) -> X < 13 |
| 3934 | return new ICmpInst(ICmpInst::ICMP_SLT, LHSVal, LHSCst); |
| 3935 | break; // (X != 13 & X s< 15) -> no change |
| 3936 | case ICmpInst::ICMP_EQ: // (X != 13 & X == 15) -> X == 15 |
| 3937 | case ICmpInst::ICMP_UGT: // (X != 13 & X u> 15) -> X u> 15 |
| 3938 | case ICmpInst::ICMP_SGT: // (X != 13 & X s> 15) -> X s> 15 |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3939 | return ReplaceInstUsesWith(I, RHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3940 | case ICmpInst::ICMP_NE: |
| 3941 | if (LHSCst == SubOne(RHSCst)){// (X != 13 & X != 14) -> X-13 >u 1 |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3942 | Constant *AddCST = ConstantExpr::getNeg(LHSCst); |
| 3943 | Instruction *Add = BinaryOperator::createAdd(LHSVal, AddCST, |
| 3944 | LHSVal->getName()+".off"); |
| 3945 | InsertNewInstBefore(Add, I); |
Chris Lattner | 424db02 | 2007-01-27 23:08:34 +0000 | [diff] [blame] | 3946 | return new ICmpInst(ICmpInst::ICMP_UGT, Add, |
| 3947 | ConstantInt::get(Add->getType(), 1)); |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3948 | } |
| 3949 | break; // (X != 13 & X != 15) -> no change |
| 3950 | } |
| 3951 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3952 | case ICmpInst::ICMP_ULT: |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3953 | switch (RHSCC) { |
| 3954 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3955 | case ICmpInst::ICMP_EQ: // (X u< 13 & X == 15) -> false |
| 3956 | case ICmpInst::ICMP_UGT: // (X u< 13 & X u> 15) -> false |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3957 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3958 | case ICmpInst::ICMP_SGT: // (X u< 13 & X s> 15) -> no change |
| 3959 | break; |
| 3960 | case ICmpInst::ICMP_NE: // (X u< 13 & X != 15) -> X u< 13 |
| 3961 | 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] | 3962 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3963 | case ICmpInst::ICMP_SLT: // (X u< 13 & X s< 15) -> no change |
| 3964 | break; |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3965 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3966 | break; |
| 3967 | case ICmpInst::ICMP_SLT: |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3968 | switch (RHSCC) { |
| 3969 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3970 | case ICmpInst::ICMP_EQ: // (X s< 13 & X == 15) -> false |
| 3971 | case ICmpInst::ICMP_SGT: // (X s< 13 & X s> 15) -> false |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3972 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3973 | case ICmpInst::ICMP_UGT: // (X s< 13 & X u> 15) -> no change |
| 3974 | break; |
| 3975 | case ICmpInst::ICMP_NE: // (X s< 13 & X != 15) -> X < 13 |
| 3976 | case ICmpInst::ICMP_SLT: // (X s< 13 & X s< 15) -> X < 13 |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3977 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3978 | case ICmpInst::ICMP_ULT: // (X s< 13 & X u< 15) -> no change |
| 3979 | break; |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3980 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3981 | break; |
| 3982 | case ICmpInst::ICMP_UGT: |
| 3983 | switch (RHSCC) { |
| 3984 | default: assert(0 && "Unknown integer condition code!"); |
| 3985 | case ICmpInst::ICMP_EQ: // (X u> 13 & X == 15) -> X > 13 |
| 3986 | return ReplaceInstUsesWith(I, LHS); |
| 3987 | case ICmpInst::ICMP_UGT: // (X u> 13 & X u> 15) -> X u> 15 |
| 3988 | return ReplaceInstUsesWith(I, RHS); |
| 3989 | case ICmpInst::ICMP_SGT: // (X u> 13 & X s> 15) -> no change |
| 3990 | break; |
| 3991 | case ICmpInst::ICMP_NE: |
| 3992 | if (RHSCst == AddOne(LHSCst)) // (X u> 13 & X != 14) -> X u> 14 |
| 3993 | return new ICmpInst(LHSCC, LHSVal, RHSCst); |
| 3994 | break; // (X u> 13 & X != 15) -> no change |
| 3995 | case ICmpInst::ICMP_ULT: // (X u> 13 & X u< 15) ->(X-14) <u 1 |
| 3996 | return InsertRangeTest(LHSVal, AddOne(LHSCst), RHSCst, false, |
| 3997 | true, I); |
| 3998 | case ICmpInst::ICMP_SLT: // (X u> 13 & X s< 15) -> no change |
| 3999 | break; |
| 4000 | } |
| 4001 | break; |
| 4002 | case ICmpInst::ICMP_SGT: |
| 4003 | switch (RHSCC) { |
| 4004 | default: assert(0 && "Unknown integer condition code!"); |
Chris Lattner | a7d1ab0 | 2007-11-16 06:04:17 +0000 | [diff] [blame] | 4005 | case ICmpInst::ICMP_EQ: // (X s> 13 & X == 15) -> X == 15 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4006 | case ICmpInst::ICMP_SGT: // (X s> 13 & X s> 15) -> X s> 15 |
| 4007 | return ReplaceInstUsesWith(I, RHS); |
| 4008 | case ICmpInst::ICMP_UGT: // (X s> 13 & X u> 15) -> no change |
| 4009 | break; |
| 4010 | case ICmpInst::ICMP_NE: |
| 4011 | if (RHSCst == AddOne(LHSCst)) // (X s> 13 & X != 14) -> X s> 14 |
| 4012 | return new ICmpInst(LHSCC, LHSVal, RHSCst); |
| 4013 | break; // (X s> 13 & X != 15) -> no change |
| 4014 | case ICmpInst::ICMP_SLT: // (X s> 13 & X s< 15) ->(X-14) s< 1 |
| 4015 | return InsertRangeTest(LHSVal, AddOne(LHSCst), RHSCst, true, |
| 4016 | true, I); |
| 4017 | case ICmpInst::ICMP_ULT: // (X s> 13 & X u< 15) -> no change |
| 4018 | break; |
| 4019 | } |
| 4020 | break; |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 4021 | } |
| 4022 | } |
| 4023 | } |
| 4024 | |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4025 | // fold (and (cast A), (cast B)) -> (cast (and A, B)) |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4026 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) |
| 4027 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) |
| 4028 | if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind ? |
| 4029 | const Type *SrcTy = Op0C->getOperand(0)->getType(); |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 4030 | if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() && |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4031 | // 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] | 4032 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), |
| 4033 | I.getType(), TD) && |
| 4034 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), |
| 4035 | I.getType(), TD)) { |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4036 | Instruction *NewOp = BinaryOperator::createAnd(Op0C->getOperand(0), |
| 4037 | Op1C->getOperand(0), |
| 4038 | I.getName()); |
| 4039 | InsertNewInstBefore(NewOp, I); |
| 4040 | return CastInst::create(Op0C->getOpcode(), NewOp, I.getType()); |
| 4041 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4042 | } |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4043 | |
| 4044 | // (X >> Z) & (Y >> Z) -> (X&Y) >> Z for all shifts. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4045 | if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) { |
| 4046 | if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0)) |
| 4047 | if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() && |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4048 | SI0->getOperand(1) == SI1->getOperand(1) && |
| 4049 | (SI0->hasOneUse() || SI1->hasOneUse())) { |
| 4050 | Instruction *NewOp = |
| 4051 | InsertNewInstBefore(BinaryOperator::createAnd(SI0->getOperand(0), |
| 4052 | SI1->getOperand(0), |
| 4053 | SI0->getName()), I); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4054 | return BinaryOperator::create(SI1->getOpcode(), NewOp, |
| 4055 | SI1->getOperand(1)); |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4056 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4057 | } |
| 4058 | |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4059 | // (fcmp ord x, c) & (fcmp ord y, c) -> (fcmp ord x, y) |
| 4060 | if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0))) { |
| 4061 | if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1))) { |
| 4062 | if (LHS->getPredicate() == FCmpInst::FCMP_ORD && |
| 4063 | RHS->getPredicate() == FCmpInst::FCMP_ORD) |
| 4064 | if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1))) |
| 4065 | if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) { |
| 4066 | // If either of the constants are nans, then the whole thing returns |
| 4067 | // false. |
Chris Lattner | be3e348 | 2007-10-24 18:54:45 +0000 | [diff] [blame] | 4068 | if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN()) |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4069 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
| 4070 | return new FCmpInst(FCmpInst::FCMP_ORD, LHS->getOperand(0), |
| 4071 | RHS->getOperand(0)); |
| 4072 | } |
| 4073 | } |
| 4074 | } |
| 4075 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4076 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4077 | } |
| 4078 | |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4079 | /// CollectBSwapParts - Look to see if the specified value defines a single byte |
| 4080 | /// in the result. If it does, and if the specified byte hasn't been filled in |
| 4081 | /// yet, fill it in and return false. |
Chris Lattner | 535014f | 2007-02-15 22:52:10 +0000 | [diff] [blame] | 4082 | static bool CollectBSwapParts(Value *V, SmallVector<Value*, 8> &ByteValues) { |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4083 | Instruction *I = dyn_cast<Instruction>(V); |
| 4084 | if (I == 0) return true; |
| 4085 | |
| 4086 | // If this is an or instruction, it is an inner node of the bswap. |
| 4087 | if (I->getOpcode() == Instruction::Or) |
| 4088 | return CollectBSwapParts(I->getOperand(0), ByteValues) || |
| 4089 | CollectBSwapParts(I->getOperand(1), ByteValues); |
| 4090 | |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 4091 | uint32_t BitWidth = I->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4092 | // If this is a shift by a constant int, and it is "24", then its operand |
| 4093 | // defines a byte. We only handle unsigned types here. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4094 | if (I->isShift() && isa<ConstantInt>(I->getOperand(1))) { |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4095 | // Not shifting the entire input by N-1 bytes? |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 4096 | if (cast<ConstantInt>(I->getOperand(1))->getLimitedValue(BitWidth) != |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4097 | 8*(ByteValues.size()-1)) |
| 4098 | return true; |
| 4099 | |
| 4100 | unsigned DestNo; |
| 4101 | if (I->getOpcode() == Instruction::Shl) { |
| 4102 | // X << 24 defines the top byte with the lowest of the input bytes. |
| 4103 | DestNo = ByteValues.size()-1; |
| 4104 | } else { |
| 4105 | // X >>u 24 defines the low byte with the highest of the input bytes. |
| 4106 | DestNo = 0; |
| 4107 | } |
| 4108 | |
| 4109 | // If the destination byte value is already defined, the values are or'd |
| 4110 | // together, which isn't a bswap (unless it's an or of the same bits). |
| 4111 | if (ByteValues[DestNo] && ByteValues[DestNo] != I->getOperand(0)) |
| 4112 | return true; |
| 4113 | ByteValues[DestNo] = I->getOperand(0); |
| 4114 | return false; |
| 4115 | } |
| 4116 | |
| 4117 | // Otherwise, we can only handle and(shift X, imm), imm). Bail out of if we |
| 4118 | // don't have this. |
| 4119 | Value *Shift = 0, *ShiftLHS = 0; |
| 4120 | ConstantInt *AndAmt = 0, *ShiftAmt = 0; |
| 4121 | if (!match(I, m_And(m_Value(Shift), m_ConstantInt(AndAmt))) || |
| 4122 | !match(Shift, m_Shift(m_Value(ShiftLHS), m_ConstantInt(ShiftAmt)))) |
| 4123 | return true; |
| 4124 | Instruction *SI = cast<Instruction>(Shift); |
| 4125 | |
| 4126 | // 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] | 4127 | if (ShiftAmt->getLimitedValue(BitWidth) & 7 || |
| 4128 | ShiftAmt->getLimitedValue(BitWidth) > 8*ByteValues.size()) |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4129 | return true; |
| 4130 | |
| 4131 | // Turn 0xFF -> 0, 0xFF00 -> 1, 0xFF0000 -> 2, etc. |
| 4132 | unsigned DestByte; |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 4133 | if (AndAmt->getValue().getActiveBits() > 64) |
| 4134 | return true; |
| 4135 | uint64_t AndAmtVal = AndAmt->getZExtValue(); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4136 | for (DestByte = 0; DestByte != ByteValues.size(); ++DestByte) |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 4137 | if (AndAmtVal == uint64_t(0xFF) << 8*DestByte) |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4138 | break; |
| 4139 | // Unknown mask for bswap. |
| 4140 | if (DestByte == ByteValues.size()) return true; |
| 4141 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4142 | unsigned ShiftBytes = ShiftAmt->getZExtValue()/8; |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4143 | unsigned SrcByte; |
| 4144 | if (SI->getOpcode() == Instruction::Shl) |
| 4145 | SrcByte = DestByte - ShiftBytes; |
| 4146 | else |
| 4147 | SrcByte = DestByte + ShiftBytes; |
| 4148 | |
| 4149 | // If the SrcByte isn't a bswapped value from the DestByte, reject it. |
| 4150 | if (SrcByte != ByteValues.size()-DestByte-1) |
| 4151 | return true; |
| 4152 | |
| 4153 | // If the destination byte value is already defined, the values are or'd |
| 4154 | // together, which isn't a bswap (unless it's an or of the same bits). |
| 4155 | if (ByteValues[DestByte] && ByteValues[DestByte] != SI->getOperand(0)) |
| 4156 | return true; |
| 4157 | ByteValues[DestByte] = SI->getOperand(0); |
| 4158 | return false; |
| 4159 | } |
| 4160 | |
| 4161 | /// MatchBSwap - Given an OR instruction, check to see if this is a bswap idiom. |
| 4162 | /// If so, insert the new bswap intrinsic and return it. |
| 4163 | Instruction *InstCombiner::MatchBSwap(BinaryOperator &I) { |
Chris Lattner | 55fc8c4 | 2007-04-01 20:57:36 +0000 | [diff] [blame] | 4164 | const IntegerType *ITy = dyn_cast<IntegerType>(I.getType()); |
| 4165 | if (!ITy || ITy->getBitWidth() % 16) |
| 4166 | return 0; // Can only bswap pairs of bytes. Can't do vectors. |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4167 | |
| 4168 | /// ByteValues - For each byte of the result, we keep track of which value |
| 4169 | /// defines each byte. |
Chris Lattner | 535014f | 2007-02-15 22:52:10 +0000 | [diff] [blame] | 4170 | SmallVector<Value*, 8> ByteValues; |
Chris Lattner | 55fc8c4 | 2007-04-01 20:57:36 +0000 | [diff] [blame] | 4171 | ByteValues.resize(ITy->getBitWidth()/8); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4172 | |
| 4173 | // Try to find all the pieces corresponding to the bswap. |
| 4174 | if (CollectBSwapParts(I.getOperand(0), ByteValues) || |
| 4175 | CollectBSwapParts(I.getOperand(1), ByteValues)) |
| 4176 | return 0; |
| 4177 | |
| 4178 | // Check to see if all of the bytes come from the same value. |
| 4179 | Value *V = ByteValues[0]; |
| 4180 | if (V == 0) return 0; // Didn't find a byte? Must be zero. |
| 4181 | |
| 4182 | // Check to make sure that all of the bytes come from the same value. |
| 4183 | for (unsigned i = 1, e = ByteValues.size(); i != e; ++i) |
| 4184 | if (ByteValues[i] != V) |
| 4185 | return 0; |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 4186 | const Type *Tys[] = { ITy }; |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4187 | Module *M = I.getParent()->getParent()->getParent(); |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 4188 | Function *F = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 4189 | return CallInst::Create(F, V); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4190 | } |
| 4191 | |
| 4192 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4193 | Instruction *InstCombiner::visitOr(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 4194 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4195 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4196 | |
Chris Lattner | 42593e6 | 2007-03-24 23:56:43 +0000 | [diff] [blame] | 4197 | if (isa<UndefValue>(Op1)) // X | undef -> -1 |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4198 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4199 | |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 4200 | // or X, X = X |
| 4201 | if (Op0 == Op1) |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 4202 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4203 | |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 4204 | // See if we can simplify any instructions used by the instruction whose sole |
| 4205 | // purpose is to compute bits we don't care about. |
Chris Lattner | 42593e6 | 2007-03-24 23:56:43 +0000 | [diff] [blame] | 4206 | if (!isa<VectorType>(I.getType())) { |
| 4207 | uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth(); |
| 4208 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 4209 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth), |
| 4210 | KnownZero, KnownOne)) |
| 4211 | return &I; |
Chris Lattner | 041a6c9 | 2007-06-15 05:26:55 +0000 | [diff] [blame] | 4212 | } else if (isa<ConstantAggregateZero>(Op1)) { |
| 4213 | return ReplaceInstUsesWith(I, Op0); // X | <0,0> -> X |
| 4214 | } else if (ConstantVector *CP = dyn_cast<ConstantVector>(Op1)) { |
| 4215 | if (CP->isAllOnesValue()) // X | <-1,-1> -> <-1,-1> |
| 4216 | return ReplaceInstUsesWith(I, I.getOperand(1)); |
Chris Lattner | 42593e6 | 2007-03-24 23:56:43 +0000 | [diff] [blame] | 4217 | } |
Chris Lattner | 041a6c9 | 2007-06-15 05:26:55 +0000 | [diff] [blame] | 4218 | |
| 4219 | |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 4220 | |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4221 | // or X, -1 == -1 |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4222 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 4223 | ConstantInt *C1 = 0; Value *X = 0; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4224 | // (X & C1) | C2 --> (X | C2) & (C1|C2) |
| 4225 | 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] | 4226 | Instruction *Or = BinaryOperator::createOr(X, RHS); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4227 | InsertNewInstBefore(Or, I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 4228 | Or->takeName(Op0); |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 4229 | return BinaryOperator::createAnd(Or, |
| 4230 | ConstantInt::get(RHS->getValue() | C1->getValue())); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4231 | } |
Chris Lattner | ad44ebf | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 4232 | |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4233 | // (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2) |
| 4234 | 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] | 4235 | Instruction *Or = BinaryOperator::createOr(X, RHS); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4236 | InsertNewInstBefore(Or, I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 4237 | Or->takeName(Op0); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4238 | return BinaryOperator::createXor(Or, |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 4239 | ConstantInt::get(C1->getValue() & ~RHS->getValue())); |
Chris Lattner | ad44ebf | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 4240 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 4241 | |
| 4242 | // Try to fold constant and into select arguments. |
| 4243 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 4244 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 4245 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 4246 | if (isa<PHINode>(Op0)) |
| 4247 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 4248 | return NV; |
Chris Lattner | ad44ebf | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 4249 | } |
| 4250 | |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 4251 | Value *A = 0, *B = 0; |
| 4252 | ConstantInt *C1 = 0, *C2 = 0; |
Chris Lattner | f4d4c87 | 2005-05-07 23:49:08 +0000 | [diff] [blame] | 4253 | |
| 4254 | if (match(Op0, m_And(m_Value(A), m_Value(B)))) |
| 4255 | if (A == Op1 || B == Op1) // (A & ?) | A --> A |
| 4256 | return ReplaceInstUsesWith(I, Op1); |
| 4257 | if (match(Op1, m_And(m_Value(A), m_Value(B)))) |
| 4258 | if (A == Op0 || B == Op0) // A | (A & ?) --> A |
| 4259 | return ReplaceInstUsesWith(I, Op0); |
| 4260 | |
Chris Lattner | 6423d4c | 2006-07-10 20:25:24 +0000 | [diff] [blame] | 4261 | // (A | B) | C and A | (B | C) -> bswap if possible. |
| 4262 | // (A >> B) | (C << D) and (A << B) | (B >> C) -> bswap if possible. |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4263 | if (match(Op0, m_Or(m_Value(), m_Value())) || |
Chris Lattner | 6423d4c | 2006-07-10 20:25:24 +0000 | [diff] [blame] | 4264 | match(Op1, m_Or(m_Value(), m_Value())) || |
| 4265 | (match(Op0, m_Shift(m_Value(), m_Value())) && |
| 4266 | match(Op1, m_Shift(m_Value(), m_Value())))) { |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4267 | if (Instruction *BSwap = MatchBSwap(I)) |
| 4268 | return BSwap; |
| 4269 | } |
| 4270 | |
Chris Lattner | 6e4c649 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 4271 | // (X^C)|Y -> (X|Y)^C iff Y&C == 0 |
| 4272 | 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] | 4273 | MaskedValueIsZero(Op1, C1->getValue())) { |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 4274 | Instruction *NOr = BinaryOperator::createOr(A, Op1); |
| 4275 | InsertNewInstBefore(NOr, I); |
| 4276 | NOr->takeName(Op0); |
| 4277 | return BinaryOperator::createXor(NOr, C1); |
Chris Lattner | 6e4c649 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 4278 | } |
| 4279 | |
| 4280 | // Y|(X^C) -> (X|Y)^C iff Y&C == 0 |
| 4281 | 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] | 4282 | MaskedValueIsZero(Op0, C1->getValue())) { |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 4283 | Instruction *NOr = BinaryOperator::createOr(A, Op0); |
| 4284 | InsertNewInstBefore(NOr, I); |
| 4285 | NOr->takeName(Op0); |
| 4286 | return BinaryOperator::createXor(NOr, C1); |
Chris Lattner | 6e4c649 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 4287 | } |
| 4288 | |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 4289 | // (A & C)|(B & D) |
Chris Lattner | 2384d7b | 2007-06-19 05:43:49 +0000 | [diff] [blame] | 4290 | Value *C = 0, *D = 0; |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 4291 | if (match(Op0, m_And(m_Value(A), m_Value(C))) && |
| 4292 | match(Op1, m_And(m_Value(B), m_Value(D)))) { |
Chris Lattner | 6cae0e0 | 2007-04-08 07:55:22 +0000 | [diff] [blame] | 4293 | Value *V1 = 0, *V2 = 0, *V3 = 0; |
| 4294 | C1 = dyn_cast<ConstantInt>(C); |
| 4295 | C2 = dyn_cast<ConstantInt>(D); |
| 4296 | if (C1 && C2) { // (A & C1)|(B & C2) |
| 4297 | // If we have: ((V + N) & C1) | (V & C2) |
| 4298 | // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0 |
| 4299 | // replace with V+N. |
| 4300 | if (C1->getValue() == ~C2->getValue()) { |
| 4301 | if ((C2->getValue() & (C2->getValue()+1)) == 0 && // C2 == 0+1+ |
| 4302 | match(A, m_Add(m_Value(V1), m_Value(V2)))) { |
| 4303 | // Add commutes, try both ways. |
| 4304 | if (V1 == B && MaskedValueIsZero(V2, C2->getValue())) |
| 4305 | return ReplaceInstUsesWith(I, A); |
| 4306 | if (V2 == B && MaskedValueIsZero(V1, C2->getValue())) |
| 4307 | return ReplaceInstUsesWith(I, A); |
| 4308 | } |
| 4309 | // Or commutes, try both ways. |
| 4310 | if ((C1->getValue() & (C1->getValue()+1)) == 0 && |
| 4311 | match(B, m_Add(m_Value(V1), m_Value(V2)))) { |
| 4312 | // Add commutes, try both ways. |
| 4313 | if (V1 == A && MaskedValueIsZero(V2, C1->getValue())) |
| 4314 | return ReplaceInstUsesWith(I, B); |
| 4315 | if (V2 == A && MaskedValueIsZero(V1, C1->getValue())) |
| 4316 | return ReplaceInstUsesWith(I, B); |
| 4317 | } |
| 4318 | } |
Chris Lattner | 044e533 | 2007-04-08 08:01:49 +0000 | [diff] [blame] | 4319 | V1 = 0; V2 = 0; V3 = 0; |
Chris Lattner | 6cae0e0 | 2007-04-08 07:55:22 +0000 | [diff] [blame] | 4320 | } |
| 4321 | |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 4322 | // Check to see if we have any common things being and'ed. If so, find the |
| 4323 | // terms for V1 & (V2|V3). |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 4324 | if (isOnlyUse(Op0) || isOnlyUse(Op1)) { |
| 4325 | if (A == B) // (A & C)|(A & D) == A & (C|D) |
| 4326 | V1 = A, V2 = C, V3 = D; |
| 4327 | else if (A == D) // (A & C)|(B & A) == A & (B|C) |
| 4328 | V1 = A, V2 = B, V3 = C; |
| 4329 | else if (C == B) // (A & C)|(C & D) == C & (A|D) |
| 4330 | V1 = C, V2 = A, V3 = D; |
| 4331 | else if (C == D) // (A & C)|(B & C) == C & (A|B) |
| 4332 | V1 = C, V2 = A, V3 = B; |
| 4333 | |
| 4334 | if (V1) { |
| 4335 | Value *Or = |
| 4336 | InsertNewInstBefore(BinaryOperator::createOr(V2, V3, "tmp"), I); |
| 4337 | return BinaryOperator::createAnd(V1, Or); |
Chris Lattner | 0b7c0bf | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 4338 | } |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 4339 | } |
Chris Lattner | e9bed7d | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 4340 | } |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4341 | |
| 4342 | // (X >> Z) | (Y >> Z) -> (X|Y) >> Z for all shifts. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4343 | if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) { |
| 4344 | if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0)) |
| 4345 | if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() && |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4346 | SI0->getOperand(1) == SI1->getOperand(1) && |
| 4347 | (SI0->hasOneUse() || SI1->hasOneUse())) { |
| 4348 | Instruction *NewOp = |
| 4349 | InsertNewInstBefore(BinaryOperator::createOr(SI0->getOperand(0), |
| 4350 | SI1->getOperand(0), |
| 4351 | SI0->getName()), I); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4352 | return BinaryOperator::create(SI1->getOpcode(), NewOp, |
| 4353 | SI1->getOperand(1)); |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4354 | } |
| 4355 | } |
Chris Lattner | 67ca768 | 2003-08-12 19:11:07 +0000 | [diff] [blame] | 4356 | |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4357 | if (match(Op0, m_Not(m_Value(A)))) { // ~A | Op1 |
| 4358 | if (A == Op1) // ~A | A == -1 |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4359 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4360 | } else { |
| 4361 | A = 0; |
| 4362 | } |
Chris Lattner | f4d4c87 | 2005-05-07 23:49:08 +0000 | [diff] [blame] | 4363 | // Note, A is still live here! |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4364 | if (match(Op1, m_Not(m_Value(B)))) { // Op0 | ~B |
| 4365 | if (Op0 == B) |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4366 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | a27231a | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 4367 | |
Misha Brukman | cb6267b | 2004-07-30 12:50:08 +0000 | [diff] [blame] | 4368 | // (~A | ~B) == (~(A & B)) - De Morgan's Law |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4369 | if (A && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
| 4370 | Value *And = InsertNewInstBefore(BinaryOperator::createAnd(A, B, |
| 4371 | I.getName()+".demorgan"), I); |
| 4372 | return BinaryOperator::createNot(And); |
| 4373 | } |
Chris Lattner | a27231a | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 4374 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4375 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4376 | // (icmp1 A, B) | (icmp2 A, B) --> (icmp3 A, B) |
| 4377 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1))) { |
| 4378 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS))) |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 4379 | return R; |
| 4380 | |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4381 | Value *LHSVal, *RHSVal; |
| 4382 | ConstantInt *LHSCst, *RHSCst; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4383 | ICmpInst::Predicate LHSCC, RHSCC; |
| 4384 | if (match(Op0, m_ICmp(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst)))) |
| 4385 | if (match(RHS, m_ICmp(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst)))) |
| 4386 | if (LHSVal == RHSVal && // Found (X icmp C1) | (X icmp C2) |
| 4387 | // icmp [us][gl]e x, cst is folded to icmp [us][gl]t elsewhere. |
| 4388 | LHSCC != ICmpInst::ICMP_UGE && LHSCC != ICmpInst::ICMP_ULE && |
| 4389 | RHSCC != ICmpInst::ICMP_UGE && RHSCC != ICmpInst::ICMP_ULE && |
| 4390 | LHSCC != ICmpInst::ICMP_SGE && LHSCC != ICmpInst::ICMP_SLE && |
Chris Lattner | 8885887 | 2007-05-11 05:55:56 +0000 | [diff] [blame] | 4391 | RHSCC != ICmpInst::ICMP_SGE && RHSCC != ICmpInst::ICMP_SLE && |
| 4392 | // We can't fold (ugt x, C) | (sgt x, C2). |
| 4393 | PredicatesFoldable(LHSCC, RHSCC)) { |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4394 | // Ensure that the larger constant is on the RHS. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4395 | ICmpInst *LHS = cast<ICmpInst>(Op0); |
Chris Lattner | 8885887 | 2007-05-11 05:55:56 +0000 | [diff] [blame] | 4396 | bool NeedsSwap; |
| 4397 | if (ICmpInst::isSignedPredicate(LHSCC)) |
Chris Lattner | 3aea1bd | 2007-05-11 16:58:45 +0000 | [diff] [blame] | 4398 | NeedsSwap = LHSCst->getValue().sgt(RHSCst->getValue()); |
Chris Lattner | 8885887 | 2007-05-11 05:55:56 +0000 | [diff] [blame] | 4399 | else |
Chris Lattner | 3aea1bd | 2007-05-11 16:58:45 +0000 | [diff] [blame] | 4400 | NeedsSwap = LHSCst->getValue().ugt(RHSCst->getValue()); |
Chris Lattner | 8885887 | 2007-05-11 05:55:56 +0000 | [diff] [blame] | 4401 | |
| 4402 | if (NeedsSwap) { |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4403 | std::swap(LHS, RHS); |
| 4404 | std::swap(LHSCst, RHSCst); |
| 4405 | std::swap(LHSCC, RHSCC); |
| 4406 | } |
| 4407 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4408 | // At this point, we know we have have two icmp instructions |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4409 | // comparing a value against two constants and or'ing the result |
| 4410 | // together. Because of the above check, we know that we only have |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4411 | // ICMP_EQ, ICMP_NE, ICMP_LT, and ICMP_GT here. We also know (from the |
| 4412 | // FoldICmpLogical check above), that the two constants are not |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4413 | // equal. |
| 4414 | assert(LHSCst != RHSCst && "Compares not folded above?"); |
| 4415 | |
| 4416 | switch (LHSCC) { |
| 4417 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4418 | case ICmpInst::ICMP_EQ: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4419 | switch (RHSCC) { |
| 4420 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4421 | case ICmpInst::ICMP_EQ: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4422 | if (LHSCst == SubOne(RHSCst)) {// (X == 13 | X == 14) -> X-13 <u 2 |
| 4423 | Constant *AddCST = ConstantExpr::getNeg(LHSCst); |
| 4424 | Instruction *Add = BinaryOperator::createAdd(LHSVal, AddCST, |
| 4425 | LHSVal->getName()+".off"); |
| 4426 | InsertNewInstBefore(Add, I); |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 4427 | AddCST = Subtract(AddOne(RHSCst), LHSCst); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4428 | return new ICmpInst(ICmpInst::ICMP_ULT, Add, AddCST); |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4429 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4430 | break; // (X == 13 | X == 15) -> no change |
| 4431 | case ICmpInst::ICMP_UGT: // (X == 13 | X u> 14) -> no change |
| 4432 | case ICmpInst::ICMP_SGT: // (X == 13 | X s> 14) -> no change |
Chris Lattner | 240d6f4 | 2005-04-19 06:04:18 +0000 | [diff] [blame] | 4433 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4434 | case ICmpInst::ICMP_NE: // (X == 13 | X != 15) -> X != 15 |
| 4435 | case ICmpInst::ICMP_ULT: // (X == 13 | X u< 15) -> X u< 15 |
| 4436 | case ICmpInst::ICMP_SLT: // (X == 13 | X s< 15) -> X s< 15 |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4437 | return ReplaceInstUsesWith(I, RHS); |
| 4438 | } |
| 4439 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4440 | case ICmpInst::ICMP_NE: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4441 | switch (RHSCC) { |
| 4442 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4443 | case ICmpInst::ICMP_EQ: // (X != 13 | X == 15) -> X != 13 |
| 4444 | case ICmpInst::ICMP_UGT: // (X != 13 | X u> 15) -> X != 13 |
| 4445 | case ICmpInst::ICMP_SGT: // (X != 13 | X s> 15) -> X != 13 |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4446 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4447 | case ICmpInst::ICMP_NE: // (X != 13 | X != 15) -> true |
| 4448 | case ICmpInst::ICMP_ULT: // (X != 13 | X u< 15) -> true |
| 4449 | case ICmpInst::ICMP_SLT: // (X != 13 | X s< 15) -> true |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4450 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4451 | } |
| 4452 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4453 | case ICmpInst::ICMP_ULT: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4454 | switch (RHSCC) { |
| 4455 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4456 | case ICmpInst::ICMP_EQ: // (X u< 13 | X == 14) -> no change |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4457 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4458 | 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] | 4459 | // If RHSCst is [us]MAXINT, it is always false. Not handling |
| 4460 | // this can cause overflow. |
| 4461 | if (RHSCst->isMaxValue(false)) |
| 4462 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4463 | return InsertRangeTest(LHSVal, LHSCst, AddOne(RHSCst), false, |
| 4464 | false, I); |
| 4465 | case ICmpInst::ICMP_SGT: // (X u< 13 | X s> 15) -> no change |
| 4466 | break; |
| 4467 | case ICmpInst::ICMP_NE: // (X u< 13 | X != 15) -> X != 15 |
| 4468 | 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] | 4469 | return ReplaceInstUsesWith(I, RHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4470 | case ICmpInst::ICMP_SLT: // (X u< 13 | X s< 15) -> no change |
| 4471 | break; |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4472 | } |
| 4473 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4474 | case ICmpInst::ICMP_SLT: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4475 | switch (RHSCC) { |
| 4476 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4477 | case ICmpInst::ICMP_EQ: // (X s< 13 | X == 14) -> no change |
| 4478 | break; |
| 4479 | 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] | 4480 | // If RHSCst is [us]MAXINT, it is always false. Not handling |
| 4481 | // this can cause overflow. |
| 4482 | if (RHSCst->isMaxValue(true)) |
| 4483 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4484 | return InsertRangeTest(LHSVal, LHSCst, AddOne(RHSCst), true, |
| 4485 | false, I); |
| 4486 | case ICmpInst::ICMP_UGT: // (X s< 13 | X u> 15) -> no change |
| 4487 | break; |
| 4488 | case ICmpInst::ICMP_NE: // (X s< 13 | X != 15) -> X != 15 |
| 4489 | case ICmpInst::ICMP_SLT: // (X s< 13 | X s< 15) -> X s< 15 |
| 4490 | return ReplaceInstUsesWith(I, RHS); |
| 4491 | case ICmpInst::ICMP_ULT: // (X s< 13 | X u< 15) -> no change |
| 4492 | break; |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4493 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4494 | break; |
| 4495 | case ICmpInst::ICMP_UGT: |
| 4496 | switch (RHSCC) { |
| 4497 | default: assert(0 && "Unknown integer condition code!"); |
| 4498 | case ICmpInst::ICMP_EQ: // (X u> 13 | X == 15) -> X u> 13 |
| 4499 | case ICmpInst::ICMP_UGT: // (X u> 13 | X u> 15) -> X u> 13 |
| 4500 | return ReplaceInstUsesWith(I, LHS); |
| 4501 | case ICmpInst::ICMP_SGT: // (X u> 13 | X s> 15) -> no change |
| 4502 | break; |
| 4503 | case ICmpInst::ICMP_NE: // (X u> 13 | X != 15) -> true |
| 4504 | case ICmpInst::ICMP_ULT: // (X u> 13 | X u< 15) -> true |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4505 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4506 | case ICmpInst::ICMP_SLT: // (X u> 13 | X s< 15) -> no change |
| 4507 | break; |
| 4508 | } |
| 4509 | break; |
| 4510 | case ICmpInst::ICMP_SGT: |
| 4511 | switch (RHSCC) { |
| 4512 | default: assert(0 && "Unknown integer condition code!"); |
| 4513 | case ICmpInst::ICMP_EQ: // (X s> 13 | X == 15) -> X > 13 |
| 4514 | case ICmpInst::ICMP_SGT: // (X s> 13 | X s> 15) -> X > 13 |
| 4515 | return ReplaceInstUsesWith(I, LHS); |
| 4516 | case ICmpInst::ICMP_UGT: // (X s> 13 | X u> 15) -> no change |
| 4517 | break; |
| 4518 | case ICmpInst::ICMP_NE: // (X s> 13 | X != 15) -> true |
| 4519 | case ICmpInst::ICMP_SLT: // (X s> 13 | X s< 15) -> true |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4520 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4521 | case ICmpInst::ICMP_ULT: // (X s> 13 | X u< 15) -> no change |
| 4522 | break; |
| 4523 | } |
| 4524 | break; |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4525 | } |
| 4526 | } |
| 4527 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4528 | |
| 4529 | // fold (or (cast A), (cast B)) -> (cast (or A, B)) |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4530 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) { |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4531 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4532 | if (Op0C->getOpcode() == Op1C->getOpcode()) {// same cast kind ? |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 4533 | if (!isa<ICmpInst>(Op0C->getOperand(0)) || |
| 4534 | !isa<ICmpInst>(Op1C->getOperand(0))) { |
| 4535 | const Type *SrcTy = Op0C->getOperand(0)->getType(); |
| 4536 | if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() && |
| 4537 | // Only do this if the casts both really cause code to be |
| 4538 | // generated. |
| 4539 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), |
| 4540 | I.getType(), TD) && |
| 4541 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), |
| 4542 | I.getType(), TD)) { |
| 4543 | Instruction *NewOp = BinaryOperator::createOr(Op0C->getOperand(0), |
| 4544 | Op1C->getOperand(0), |
| 4545 | I.getName()); |
| 4546 | InsertNewInstBefore(NewOp, I); |
| 4547 | return CastInst::create(Op0C->getOpcode(), NewOp, I.getType()); |
| 4548 | } |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4549 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4550 | } |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4551 | } |
| 4552 | |
| 4553 | |
| 4554 | // (fcmp uno x, c) | (fcmp uno y, c) -> (fcmp uno x, y) |
| 4555 | if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0))) { |
| 4556 | if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1))) { |
| 4557 | if (LHS->getPredicate() == FCmpInst::FCMP_UNO && |
Chris Lattner | 5ebd936 | 2008-02-29 06:09:11 +0000 | [diff] [blame] | 4558 | RHS->getPredicate() == FCmpInst::FCMP_UNO && |
| 4559 | LHS->getOperand(0)->getType() == RHS->getOperand(0)->getType()) |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4560 | if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1))) |
| 4561 | if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) { |
| 4562 | // If either of the constants are nans, then the whole thing returns |
| 4563 | // true. |
Chris Lattner | be3e348 | 2007-10-24 18:54:45 +0000 | [diff] [blame] | 4564 | if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN()) |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4565 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
| 4566 | |
| 4567 | // Otherwise, no need to compare the two constants, compare the |
| 4568 | // rest. |
| 4569 | return new FCmpInst(FCmpInst::FCMP_UNO, LHS->getOperand(0), |
| 4570 | RHS->getOperand(0)); |
| 4571 | } |
| 4572 | } |
| 4573 | } |
Chris Lattner | e9bed7d | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 4574 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4575 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4576 | } |
| 4577 | |
Chris Lattner | c317d39 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 4578 | // XorSelf - Implements: X ^ X --> 0 |
| 4579 | struct XorSelf { |
| 4580 | Value *RHS; |
| 4581 | XorSelf(Value *rhs) : RHS(rhs) {} |
| 4582 | bool shouldApply(Value *LHS) const { return LHS == RHS; } |
| 4583 | Instruction *apply(BinaryOperator &Xor) const { |
| 4584 | return &Xor; |
| 4585 | } |
| 4586 | }; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4587 | |
| 4588 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4589 | Instruction *InstCombiner::visitXor(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 4590 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4591 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4592 | |
Evan Cheng | d34af78 | 2008-03-25 20:07:13 +0000 | [diff] [blame] | 4593 | if (isa<UndefValue>(Op1)) { |
| 4594 | if (isa<UndefValue>(Op0)) |
| 4595 | // Handle undef ^ undef -> 0 special case. This is a common |
| 4596 | // idiom (misuse). |
| 4597 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4598 | return ReplaceInstUsesWith(I, Op1); // X ^ undef -> undef |
Evan Cheng | d34af78 | 2008-03-25 20:07:13 +0000 | [diff] [blame] | 4599 | } |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4600 | |
Chris Lattner | c317d39 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 4601 | // xor X, X = 0, even if X is nested in a sequence of Xor's. |
| 4602 | if (Instruction *Result = AssociativeOpt(I, XorSelf(Op1))) { |
Chris Lattner | a9ff5eb | 2007-08-05 08:47:58 +0000 | [diff] [blame] | 4603 | assert(Result == &I && "AssociativeOpt didn't work?"); Result=Result; |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 4604 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | c317d39 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 4605 | } |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 4606 | |
| 4607 | // See if we can simplify any instructions used by the instruction whose sole |
| 4608 | // purpose is to compute bits we don't care about. |
Reid Spencer | a03d45f | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 4609 | if (!isa<VectorType>(I.getType())) { |
| 4610 | uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth(); |
| 4611 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 4612 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth), |
| 4613 | KnownZero, KnownOne)) |
| 4614 | return &I; |
Chris Lattner | 041a6c9 | 2007-06-15 05:26:55 +0000 | [diff] [blame] | 4615 | } else if (isa<ConstantAggregateZero>(Op1)) { |
| 4616 | return ReplaceInstUsesWith(I, Op0); // X ^ <0,0> -> X |
Reid Spencer | a03d45f | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 4617 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4618 | |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4619 | // Is this a ~ operation? |
| 4620 | if (Value *NotOp = dyn_castNotVal(&I)) { |
| 4621 | // ~(~X & Y) --> (X | ~Y) - De Morgan's Law |
| 4622 | // ~(~X | Y) === (X & ~Y) - De Morgan's Law |
| 4623 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(NotOp)) { |
| 4624 | if (Op0I->getOpcode() == Instruction::And || |
| 4625 | Op0I->getOpcode() == Instruction::Or) { |
| 4626 | if (dyn_castNotVal(Op0I->getOperand(1))) Op0I->swapOperands(); |
| 4627 | if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0))) { |
| 4628 | Instruction *NotY = |
| 4629 | BinaryOperator::createNot(Op0I->getOperand(1), |
| 4630 | Op0I->getOperand(1)->getName()+".not"); |
| 4631 | InsertNewInstBefore(NotY, I); |
| 4632 | if (Op0I->getOpcode() == Instruction::And) |
| 4633 | return BinaryOperator::createOr(Op0NotVal, NotY); |
| 4634 | else |
| 4635 | return BinaryOperator::createAnd(Op0NotVal, NotY); |
| 4636 | } |
| 4637 | } |
| 4638 | } |
| 4639 | } |
| 4640 | |
| 4641 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4642 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Nick Lewycky | f947b3e | 2007-08-06 20:04:16 +0000 | [diff] [blame] | 4643 | // xor (cmp A, B), true = not (cmp A, B) = !cmp A, B |
| 4644 | if (RHS == ConstantInt::getTrue() && Op0->hasOneUse()) { |
| 4645 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(Op0)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4646 | return new ICmpInst(ICI->getInversePredicate(), |
| 4647 | ICI->getOperand(0), ICI->getOperand(1)); |
Chris Lattner | ad5b4fb | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 4648 | |
Nick Lewycky | f947b3e | 2007-08-06 20:04:16 +0000 | [diff] [blame] | 4649 | if (FCmpInst *FCI = dyn_cast<FCmpInst>(Op0)) |
| 4650 | return new FCmpInst(FCI->getInversePredicate(), |
| 4651 | FCI->getOperand(0), FCI->getOperand(1)); |
| 4652 | } |
| 4653 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4654 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) { |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 4655 | // ~(c-X) == X-c-1 == X+(-c-1) |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4656 | if (Op0I->getOpcode() == Instruction::Sub && RHS->isAllOnesValue()) |
| 4657 | if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4658 | Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C); |
| 4659 | Constant *ConstantRHS = ConstantExpr::getSub(NegOp0I0C, |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4660 | ConstantInt::get(I.getType(), 1)); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4661 | return BinaryOperator::createAdd(Op0I->getOperand(1), ConstantRHS); |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4662 | } |
Chris Lattner | 5c6e2db | 2007-04-02 05:36:22 +0000 | [diff] [blame] | 4663 | |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 4664 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) { |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 4665 | if (Op0I->getOpcode() == Instruction::Add) { |
Chris Lattner | 689d24b | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 4666 | // ~(X-c) --> (-c-1)-X |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4667 | if (RHS->isAllOnesValue()) { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4668 | Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI); |
| 4669 | return BinaryOperator::createSub( |
| 4670 | ConstantExpr::getSub(NegOp0CI, |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4671 | ConstantInt::get(I.getType(), 1)), |
Chris Lattner | 689d24b | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 4672 | Op0I->getOperand(0)); |
Chris Lattner | acf4e07 | 2007-04-02 05:42:22 +0000 | [diff] [blame] | 4673 | } else if (RHS->getValue().isSignBit()) { |
Chris Lattner | 5c6e2db | 2007-04-02 05:36:22 +0000 | [diff] [blame] | 4674 | // (X + C) ^ signbit -> (X + C + signbit) |
| 4675 | Constant *C = ConstantInt::get(RHS->getValue() + Op0CI->getValue()); |
| 4676 | return BinaryOperator::createAdd(Op0I->getOperand(0), C); |
Chris Lattner | cd1d6d5 | 2007-04-02 05:48:58 +0000 | [diff] [blame] | 4677 | |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4678 | } |
Chris Lattner | 02bd1b3 | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 4679 | } else if (Op0I->getOpcode() == Instruction::Or) { |
| 4680 | // (X|C1)^C2 -> X^(C1|C2) iff X&~C1 == 0 |
Reid Spencer | a03d45f | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 4681 | if (MaskedValueIsZero(Op0I->getOperand(0), Op0CI->getValue())) { |
Chris Lattner | 02bd1b3 | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 4682 | Constant *NewRHS = ConstantExpr::getOr(Op0CI, RHS); |
| 4683 | // Anything in both C1 and C2 is known to be zero, remove it from |
| 4684 | // NewRHS. |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 4685 | Constant *CommonBits = And(Op0CI, RHS); |
Chris Lattner | 02bd1b3 | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 4686 | NewRHS = ConstantExpr::getAnd(NewRHS, |
| 4687 | ConstantExpr::getNot(CommonBits)); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 4688 | AddToWorkList(Op0I); |
Chris Lattner | 02bd1b3 | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 4689 | I.setOperand(0, Op0I->getOperand(0)); |
| 4690 | I.setOperand(1, NewRHS); |
| 4691 | return &I; |
| 4692 | } |
Chris Lattner | eca0c5c | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 4693 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 4694 | } |
Chris Lattner | 05bd1b2 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 4695 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 4696 | |
| 4697 | // Try to fold constant and into select arguments. |
| 4698 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 4699 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 4700 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 4701 | if (isa<PHINode>(Op0)) |
| 4702 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 4703 | return NV; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4704 | } |
| 4705 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 4706 | if (Value *X = dyn_castNotVal(Op0)) // ~A ^ A == -1 |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4707 | if (X == Op1) |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4708 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4709 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 4710 | if (Value *X = dyn_castNotVal(Op1)) // A ^ ~A == -1 |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4711 | if (X == Op0) |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4712 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4713 | |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4714 | |
| 4715 | BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1); |
| 4716 | if (Op1I) { |
| 4717 | Value *A, *B; |
| 4718 | if (match(Op1I, m_Or(m_Value(A), m_Value(B)))) { |
| 4719 | if (A == Op0) { // B^(B|A) == (A|B)^B |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4720 | Op1I->swapOperands(); |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4721 | I.swapOperands(); |
| 4722 | std::swap(Op0, Op1); |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4723 | } else if (B == Op0) { // B^(A|B) == (A|B)^B |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4724 | I.swapOperands(); // Simplified below. |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4725 | std::swap(Op0, Op1); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4726 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4727 | } else if (match(Op1I, m_Xor(m_Value(A), m_Value(B)))) { |
| 4728 | if (Op0 == A) // A^(A^B) == B |
| 4729 | return ReplaceInstUsesWith(I, B); |
| 4730 | else if (Op0 == B) // A^(B^A) == B |
| 4731 | return ReplaceInstUsesWith(I, A); |
| 4732 | } 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] | 4733 | if (A == Op0) { // A^(A&B) -> A^(B&A) |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4734 | Op1I->swapOperands(); |
Chris Lattner | 6abbdf9 | 2007-04-01 05:36:37 +0000 | [diff] [blame] | 4735 | std::swap(A, B); |
| 4736 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4737 | if (B == Op0) { // A^(B&A) -> (B&A)^A |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4738 | I.swapOperands(); // Simplified below. |
| 4739 | std::swap(Op0, Op1); |
| 4740 | } |
Chris Lattner | 26ca7e1 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 4741 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4742 | } |
| 4743 | |
| 4744 | BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0); |
| 4745 | if (Op0I) { |
| 4746 | Value *A, *B; |
| 4747 | if (match(Op0I, m_Or(m_Value(A), m_Value(B))) && Op0I->hasOneUse()) { |
| 4748 | if (A == Op1) // (B|A)^B == (A|B)^B |
| 4749 | std::swap(A, B); |
| 4750 | if (B == Op1) { // (A|B)^B == A & ~B |
| 4751 | Instruction *NotB = |
| 4752 | InsertNewInstBefore(BinaryOperator::createNot(Op1, "tmp"), I); |
| 4753 | return BinaryOperator::createAnd(A, NotB); |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4754 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4755 | } else if (match(Op0I, m_Xor(m_Value(A), m_Value(B)))) { |
| 4756 | if (Op1 == A) // (A^B)^A == B |
| 4757 | return ReplaceInstUsesWith(I, B); |
| 4758 | else if (Op1 == B) // (B^A)^A == B |
| 4759 | return ReplaceInstUsesWith(I, A); |
| 4760 | } else if (match(Op0I, m_And(m_Value(A), m_Value(B))) && Op0I->hasOneUse()){ |
| 4761 | if (A == Op1) // (A&B)^A -> (B&A)^A |
| 4762 | std::swap(A, B); |
| 4763 | if (B == Op1 && // (B&A)^A == ~B & A |
Chris Lattner | ae1ab39 | 2006-04-01 22:05:01 +0000 | [diff] [blame] | 4764 | !isa<ConstantInt>(Op1)) { // Canonical form is (B&C)^C |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4765 | Instruction *N = |
| 4766 | InsertNewInstBefore(BinaryOperator::createNot(A, "tmp"), I); |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4767 | return BinaryOperator::createAnd(N, Op1); |
| 4768 | } |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4769 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4770 | } |
| 4771 | |
| 4772 | // (X >> Z) ^ (Y >> Z) -> (X^Y) >> Z for all shifts. |
| 4773 | if (Op0I && Op1I && Op0I->isShift() && |
| 4774 | Op0I->getOpcode() == Op1I->getOpcode() && |
| 4775 | Op0I->getOperand(1) == Op1I->getOperand(1) && |
| 4776 | (Op1I->hasOneUse() || Op1I->hasOneUse())) { |
| 4777 | Instruction *NewOp = |
| 4778 | InsertNewInstBefore(BinaryOperator::createXor(Op0I->getOperand(0), |
| 4779 | Op1I->getOperand(0), |
| 4780 | Op0I->getName()), I); |
| 4781 | return BinaryOperator::create(Op1I->getOpcode(), NewOp, |
| 4782 | Op1I->getOperand(1)); |
| 4783 | } |
| 4784 | |
| 4785 | if (Op0I && Op1I) { |
| 4786 | Value *A, *B, *C, *D; |
| 4787 | // (A & B)^(A | B) -> A ^ B |
| 4788 | if (match(Op0I, m_And(m_Value(A), m_Value(B))) && |
| 4789 | match(Op1I, m_Or(m_Value(C), m_Value(D)))) { |
| 4790 | if ((A == C && B == D) || (A == D && B == C)) |
| 4791 | return BinaryOperator::createXor(A, B); |
| 4792 | } |
| 4793 | // (A | B)^(A & B) -> A ^ B |
| 4794 | if (match(Op0I, m_Or(m_Value(A), m_Value(B))) && |
| 4795 | match(Op1I, m_And(m_Value(C), m_Value(D)))) { |
| 4796 | if ((A == C && B == D) || (A == D && B == C)) |
| 4797 | return BinaryOperator::createXor(A, B); |
| 4798 | } |
| 4799 | |
| 4800 | // (A & B)^(C & D) |
| 4801 | if ((Op0I->hasOneUse() || Op1I->hasOneUse()) && |
| 4802 | match(Op0I, m_And(m_Value(A), m_Value(B))) && |
| 4803 | match(Op1I, m_And(m_Value(C), m_Value(D)))) { |
| 4804 | // (X & Y)^(X & Y) -> (Y^Z) & X |
| 4805 | Value *X = 0, *Y = 0, *Z = 0; |
| 4806 | if (A == C) |
| 4807 | X = A, Y = B, Z = D; |
| 4808 | else if (A == D) |
| 4809 | X = A, Y = B, Z = C; |
| 4810 | else if (B == C) |
| 4811 | X = B, Y = A, Z = D; |
| 4812 | else if (B == D) |
| 4813 | X = B, Y = A, Z = C; |
| 4814 | |
| 4815 | if (X) { |
| 4816 | Instruction *NewOp = |
| 4817 | InsertNewInstBefore(BinaryOperator::createXor(Y, Z, Op0->getName()), I); |
| 4818 | return BinaryOperator::createAnd(NewOp, X); |
| 4819 | } |
| 4820 | } |
| 4821 | } |
| 4822 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4823 | // (icmp1 A, B) ^ (icmp2 A, B) --> (icmp3 A, B) |
| 4824 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1))) |
| 4825 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS))) |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 4826 | return R; |
| 4827 | |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4828 | // fold (xor (cast A), (cast B)) -> (cast (xor A, B)) |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4829 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) { |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4830 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4831 | if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind? |
| 4832 | const Type *SrcTy = Op0C->getOperand(0)->getType(); |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 4833 | if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() && |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4834 | // 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] | 4835 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), |
| 4836 | I.getType(), TD) && |
| 4837 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), |
| 4838 | I.getType(), TD)) { |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4839 | Instruction *NewOp = BinaryOperator::createXor(Op0C->getOperand(0), |
| 4840 | Op1C->getOperand(0), |
| 4841 | I.getName()); |
| 4842 | InsertNewInstBefore(NewOp, I); |
| 4843 | return CastInst::create(Op0C->getOpcode(), NewOp, I.getType()); |
| 4844 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4845 | } |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4846 | } |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4847 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4848 | } |
| 4849 | |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4850 | /// AddWithOverflow - Compute Result = In1+In2, returning true if the result |
| 4851 | /// overflowed for this type. |
| 4852 | static bool AddWithOverflow(ConstantInt *&Result, ConstantInt *In1, |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 4853 | ConstantInt *In2, bool IsSigned = false) { |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 4854 | Result = cast<ConstantInt>(Add(In1, In2)); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4855 | |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 4856 | if (IsSigned) |
| 4857 | if (In2->getValue().isNegative()) |
| 4858 | return Result->getValue().sgt(In1->getValue()); |
| 4859 | else |
| 4860 | return Result->getValue().slt(In1->getValue()); |
| 4861 | else |
| 4862 | return Result->getValue().ult(In1->getValue()); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4863 | } |
| 4864 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4865 | /// EmitGEPOffset - Given a getelementptr instruction/constantexpr, emit the |
| 4866 | /// code necessary to compute the offset from the base pointer (without adding |
| 4867 | /// in the base pointer). Return the result as a signed integer of intptr size. |
| 4868 | static Value *EmitGEPOffset(User *GEP, Instruction &I, InstCombiner &IC) { |
| 4869 | TargetData &TD = IC.getTargetData(); |
| 4870 | gep_type_iterator GTI = gep_type_begin(GEP); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4871 | const Type *IntPtrTy = TD.getIntPtrType(); |
| 4872 | Value *Result = Constant::getNullValue(IntPtrTy); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4873 | |
| 4874 | // Build a mask for high order bits. |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4875 | unsigned IntPtrWidth = TD.getPointerSize()*8; |
| 4876 | uint64_t PtrSizeMask = ~0ULL >> (64-IntPtrWidth); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4877 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4878 | for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i, ++GTI) { |
| 4879 | Value *Op = GEP->getOperand(i); |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 4880 | uint64_t Size = TD.getABITypeSize(GTI.getIndexedType()) & PtrSizeMask; |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4881 | if (ConstantInt *OpC = dyn_cast<ConstantInt>(Op)) { |
| 4882 | if (OpC->isZero()) continue; |
| 4883 | |
| 4884 | // Handle a struct index, which adds its field offset to the pointer. |
| 4885 | if (const StructType *STy = dyn_cast<StructType>(*GTI)) { |
| 4886 | Size = TD.getStructLayout(STy)->getElementOffset(OpC->getZExtValue()); |
| 4887 | |
| 4888 | if (ConstantInt *RC = dyn_cast<ConstantInt>(Result)) |
| 4889 | Result = ConstantInt::get(RC->getValue() + APInt(IntPtrWidth, Size)); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 4890 | else |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4891 | Result = IC.InsertNewInstBefore( |
| 4892 | BinaryOperator::createAdd(Result, |
| 4893 | ConstantInt::get(IntPtrTy, Size), |
| 4894 | GEP->getName()+".offs"), I); |
| 4895 | continue; |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 4896 | } |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4897 | |
| 4898 | Constant *Scale = ConstantInt::get(IntPtrTy, Size); |
| 4899 | Constant *OC = ConstantExpr::getIntegerCast(OpC, IntPtrTy, true /*SExt*/); |
| 4900 | Scale = ConstantExpr::getMul(OC, Scale); |
| 4901 | if (Constant *RC = dyn_cast<Constant>(Result)) |
| 4902 | Result = ConstantExpr::getAdd(RC, Scale); |
| 4903 | else { |
| 4904 | // Emit an add instruction. |
| 4905 | Result = IC.InsertNewInstBefore( |
| 4906 | BinaryOperator::createAdd(Result, Scale, |
| 4907 | GEP->getName()+".offs"), I); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 4908 | } |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4909 | continue; |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4910 | } |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4911 | // Convert to correct type. |
| 4912 | if (Op->getType() != IntPtrTy) { |
| 4913 | if (Constant *OpC = dyn_cast<Constant>(Op)) |
| 4914 | Op = ConstantExpr::getSExt(OpC, IntPtrTy); |
| 4915 | else |
| 4916 | Op = IC.InsertNewInstBefore(new SExtInst(Op, IntPtrTy, |
| 4917 | Op->getName()+".c"), I); |
| 4918 | } |
| 4919 | if (Size != 1) { |
| 4920 | Constant *Scale = ConstantInt::get(IntPtrTy, Size); |
| 4921 | if (Constant *OpC = dyn_cast<Constant>(Op)) |
| 4922 | Op = ConstantExpr::getMul(OpC, Scale); |
| 4923 | else // We'll let instcombine(mul) convert this to a shl if possible. |
| 4924 | Op = IC.InsertNewInstBefore(BinaryOperator::createMul(Op, Scale, |
| 4925 | GEP->getName()+".idx"), I); |
| 4926 | } |
| 4927 | |
| 4928 | // Emit an add instruction. |
| 4929 | if (isa<Constant>(Op) && isa<Constant>(Result)) |
| 4930 | Result = ConstantExpr::getAdd(cast<Constant>(Op), |
| 4931 | cast<Constant>(Result)); |
| 4932 | else |
| 4933 | Result = IC.InsertNewInstBefore(BinaryOperator::createAdd(Op, Result, |
| 4934 | GEP->getName()+".offs"), I); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4935 | } |
| 4936 | return Result; |
| 4937 | } |
| 4938 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4939 | /// FoldGEPICmp - Fold comparisons between a GEP instruction and something |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4940 | /// 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] | 4941 | Instruction *InstCombiner::FoldGEPICmp(User *GEPLHS, Value *RHS, |
| 4942 | ICmpInst::Predicate Cond, |
| 4943 | Instruction &I) { |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4944 | assert(dyn_castGetElementPtr(GEPLHS) && "LHS is not a getelementptr!"); |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4945 | |
| 4946 | if (CastInst *CI = dyn_cast<CastInst>(RHS)) |
| 4947 | if (isa<PointerType>(CI->getOperand(0)->getType())) |
| 4948 | RHS = CI->getOperand(0); |
| 4949 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4950 | Value *PtrBase = GEPLHS->getOperand(0); |
| 4951 | if (PtrBase == RHS) { |
Chris Lattner | 7c95deb | 2008-02-05 04:45:32 +0000 | [diff] [blame] | 4952 | // ((gep Ptr, OFFSET) cmp Ptr) ---> (OFFSET cmp 0). |
| 4953 | // This transformation is valid because we know pointers can't overflow. |
| 4954 | Value *Offset = EmitGEPOffset(GEPLHS, I, *this); |
| 4955 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), Offset, |
| 4956 | Constant::getNullValue(Offset->getType())); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4957 | } else if (User *GEPRHS = dyn_castGetElementPtr(RHS)) { |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 4958 | // If the base pointers are different, but the indices are the same, just |
| 4959 | // compare the base pointer. |
| 4960 | if (PtrBase != GEPRHS->getOperand(0)) { |
| 4961 | bool IndicesTheSame = GEPLHS->getNumOperands()==GEPRHS->getNumOperands(); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 4962 | IndicesTheSame &= GEPLHS->getOperand(0)->getType() == |
Chris Lattner | 93b94a6 | 2005-04-26 14:40:41 +0000 | [diff] [blame] | 4963 | GEPRHS->getOperand(0)->getType(); |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 4964 | if (IndicesTheSame) |
| 4965 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i) |
| 4966 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { |
| 4967 | IndicesTheSame = false; |
| 4968 | break; |
| 4969 | } |
| 4970 | |
| 4971 | // If all indices are the same, just compare the base pointers. |
| 4972 | if (IndicesTheSame) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4973 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), |
| 4974 | GEPLHS->getOperand(0), GEPRHS->getOperand(0)); |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 4975 | |
| 4976 | // Otherwise, the base pointers are different and the indices are |
| 4977 | // different, bail out. |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4978 | return 0; |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 4979 | } |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4980 | |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4981 | // If one of the GEPs has all zero indices, recurse. |
| 4982 | bool AllZeros = true; |
| 4983 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i) |
| 4984 | if (!isa<Constant>(GEPLHS->getOperand(i)) || |
| 4985 | !cast<Constant>(GEPLHS->getOperand(i))->isNullValue()) { |
| 4986 | AllZeros = false; |
| 4987 | break; |
| 4988 | } |
| 4989 | if (AllZeros) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4990 | return FoldGEPICmp(GEPRHS, GEPLHS->getOperand(0), |
| 4991 | ICmpInst::getSwappedPredicate(Cond), I); |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 4992 | |
| 4993 | // If the other GEP has all zero indices, recurse. |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4994 | AllZeros = true; |
| 4995 | for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i) |
| 4996 | if (!isa<Constant>(GEPRHS->getOperand(i)) || |
| 4997 | !cast<Constant>(GEPRHS->getOperand(i))->isNullValue()) { |
| 4998 | AllZeros = false; |
| 4999 | break; |
| 5000 | } |
| 5001 | if (AllZeros) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5002 | return FoldGEPICmp(GEPLHS, GEPRHS->getOperand(0), Cond, I); |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 5003 | |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 5004 | if (GEPLHS->getNumOperands() == GEPRHS->getNumOperands()) { |
| 5005 | // If the GEPs only differ by one index, compare it. |
| 5006 | unsigned NumDifferences = 0; // Keep track of # differences. |
| 5007 | unsigned DiffOperand = 0; // The operand that differs. |
| 5008 | for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i) |
| 5009 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5010 | if (GEPLHS->getOperand(i)->getType()->getPrimitiveSizeInBits() != |
| 5011 | GEPRHS->getOperand(i)->getType()->getPrimitiveSizeInBits()) { |
Chris Lattner | 45f57b8 | 2005-01-21 23:06:49 +0000 | [diff] [blame] | 5012 | // Irreconcilable differences. |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 5013 | NumDifferences = 2; |
| 5014 | break; |
| 5015 | } else { |
| 5016 | if (NumDifferences++) break; |
| 5017 | DiffOperand = i; |
| 5018 | } |
| 5019 | } |
| 5020 | |
| 5021 | if (NumDifferences == 0) // SAME GEP? |
| 5022 | return ReplaceInstUsesWith(I, // No comparison is needed here. |
Nick Lewycky | 455e176 | 2007-09-06 02:40:25 +0000 | [diff] [blame] | 5023 | ConstantInt::get(Type::Int1Ty, |
| 5024 | isTrueWhenEqual(Cond))); |
| 5025 | |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 5026 | else if (NumDifferences == 1) { |
Chris Lattner | 45f57b8 | 2005-01-21 23:06:49 +0000 | [diff] [blame] | 5027 | Value *LHSV = GEPLHS->getOperand(DiffOperand); |
| 5028 | Value *RHSV = GEPRHS->getOperand(DiffOperand); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5029 | // Make sure we do a signed comparison here. |
| 5030 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), LHSV, RHSV); |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 5031 | } |
| 5032 | } |
| 5033 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5034 | // 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] | 5035 | // the result to fold to a constant! |
| 5036 | if ((isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) && |
| 5037 | (isa<ConstantExpr>(GEPRHS) || GEPRHS->hasOneUse())) { |
| 5038 | // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) ---> (OFFSET1 cmp OFFSET2) |
| 5039 | Value *L = EmitGEPOffset(GEPLHS, I, *this); |
| 5040 | Value *R = EmitGEPOffset(GEPRHS, I, *this); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5041 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), L, R); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5042 | } |
| 5043 | } |
| 5044 | return 0; |
| 5045 | } |
| 5046 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5047 | Instruction *InstCombiner::visitFCmpInst(FCmpInst &I) { |
| 5048 | bool Changed = SimplifyCompare(I); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5049 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5050 | |
Chris Lattner | 58e9746 | 2007-01-14 19:42:17 +0000 | [diff] [blame] | 5051 | // Fold trivial predicates. |
| 5052 | if (I.getPredicate() == FCmpInst::FCMP_FALSE) |
| 5053 | return ReplaceInstUsesWith(I, Constant::getNullValue(Type::Int1Ty)); |
| 5054 | if (I.getPredicate() == FCmpInst::FCMP_TRUE) |
| 5055 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 1)); |
| 5056 | |
| 5057 | // Simplify 'fcmp pred X, X' |
| 5058 | if (Op0 == Op1) { |
| 5059 | switch (I.getPredicate()) { |
| 5060 | default: assert(0 && "Unknown predicate!"); |
| 5061 | case FCmpInst::FCMP_UEQ: // True if unordered or equal |
| 5062 | case FCmpInst::FCMP_UGE: // True if unordered, greater than, or equal |
| 5063 | case FCmpInst::FCMP_ULE: // True if unordered, less than, or equal |
| 5064 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 1)); |
| 5065 | case FCmpInst::FCMP_OGT: // True if ordered and greater than |
| 5066 | case FCmpInst::FCMP_OLT: // True if ordered and less than |
| 5067 | case FCmpInst::FCMP_ONE: // True if ordered and operands are unequal |
| 5068 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 0)); |
| 5069 | |
| 5070 | case FCmpInst::FCMP_UNO: // True if unordered: isnan(X) | isnan(Y) |
| 5071 | case FCmpInst::FCMP_ULT: // True if unordered or less than |
| 5072 | case FCmpInst::FCMP_UGT: // True if unordered or greater than |
| 5073 | case FCmpInst::FCMP_UNE: // True if unordered or not equal |
| 5074 | // Canonicalize these to be 'fcmp uno %X, 0.0'. |
| 5075 | I.setPredicate(FCmpInst::FCMP_UNO); |
| 5076 | I.setOperand(1, Constant::getNullValue(Op0->getType())); |
| 5077 | return &I; |
| 5078 | |
| 5079 | case FCmpInst::FCMP_ORD: // True if ordered (no nans) |
| 5080 | case FCmpInst::FCMP_OEQ: // True if ordered and equal |
| 5081 | case FCmpInst::FCMP_OGE: // True if ordered and greater than or equal |
| 5082 | case FCmpInst::FCMP_OLE: // True if ordered and less than or equal |
| 5083 | // Canonicalize these to be 'fcmp ord %X, 0.0'. |
| 5084 | I.setPredicate(FCmpInst::FCMP_ORD); |
| 5085 | I.setOperand(1, Constant::getNullValue(Op0->getType())); |
| 5086 | return &I; |
| 5087 | } |
| 5088 | } |
| 5089 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5090 | if (isa<UndefValue>(Op1)) // fcmp pred X, undef -> undef |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 5091 | return ReplaceInstUsesWith(I, UndefValue::get(Type::Int1Ty)); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5092 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5093 | // Handle fcmp with constant RHS |
| 5094 | if (Constant *RHSC = dyn_cast<Constant>(Op1)) { |
| 5095 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
| 5096 | switch (LHSI->getOpcode()) { |
| 5097 | case Instruction::PHI: |
| 5098 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 5099 | return NV; |
| 5100 | break; |
| 5101 | case Instruction::Select: |
| 5102 | // If either operand of the select is a constant, we can fold the |
| 5103 | // comparison into the select arms, which will cause one to be |
| 5104 | // constant folded and the select turned into a bitwise or. |
| 5105 | Value *Op1 = 0, *Op2 = 0; |
| 5106 | if (LHSI->hasOneUse()) { |
| 5107 | if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) { |
| 5108 | // Fold the known value into the constant operand. |
| 5109 | Op1 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC); |
| 5110 | // Insert a new FCmp of the other select operand. |
| 5111 | Op2 = InsertNewInstBefore(new FCmpInst(I.getPredicate(), |
| 5112 | LHSI->getOperand(2), RHSC, |
| 5113 | I.getName()), I); |
| 5114 | } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) { |
| 5115 | // Fold the known value into the constant operand. |
| 5116 | Op2 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC); |
| 5117 | // Insert a new FCmp of the other select operand. |
| 5118 | Op1 = InsertNewInstBefore(new FCmpInst(I.getPredicate(), |
| 5119 | LHSI->getOperand(1), RHSC, |
| 5120 | I.getName()), I); |
| 5121 | } |
| 5122 | } |
| 5123 | |
| 5124 | if (Op1) |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 5125 | return SelectInst::Create(LHSI->getOperand(0), Op1, Op2); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5126 | break; |
| 5127 | } |
| 5128 | } |
| 5129 | |
| 5130 | return Changed ? &I : 0; |
| 5131 | } |
| 5132 | |
| 5133 | Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { |
| 5134 | bool Changed = SimplifyCompare(I); |
| 5135 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 5136 | const Type *Ty = Op0->getType(); |
| 5137 | |
| 5138 | // icmp X, X |
| 5139 | if (Op0 == Op1) |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 5140 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
| 5141 | isTrueWhenEqual(I))); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5142 | |
| 5143 | if (isa<UndefValue>(Op1)) // X icmp undef -> undef |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 5144 | return ReplaceInstUsesWith(I, UndefValue::get(Type::Int1Ty)); |
Christopher Lamb | 7a0678c | 2007-12-18 21:32:20 +0000 | [diff] [blame] | 5145 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5146 | // icmp <global/alloca*/null>, <global/alloca*/null> - Global/Stack value |
Chris Lattner | 711b340 | 2004-11-14 07:33:16 +0000 | [diff] [blame] | 5147 | // addresses never equal each other! We already know that Op0 != Op1. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5148 | if ((isa<GlobalValue>(Op0) || isa<AllocaInst>(Op0) || |
| 5149 | isa<ConstantPointerNull>(Op0)) && |
| 5150 | (isa<GlobalValue>(Op1) || isa<AllocaInst>(Op1) || |
Chris Lattner | 711b340 | 2004-11-14 07:33:16 +0000 | [diff] [blame] | 5151 | isa<ConstantPointerNull>(Op1))) |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 5152 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
| 5153 | !isTrueWhenEqual(I))); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5154 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5155 | // icmp's with boolean values can always be turned into bitwise operations |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 5156 | if (Ty == Type::Int1Ty) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5157 | switch (I.getPredicate()) { |
| 5158 | default: assert(0 && "Invalid icmp instruction!"); |
| 5159 | case ICmpInst::ICMP_EQ: { // icmp eq bool %A, %B -> ~(A^B) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 5160 | Instruction *Xor = BinaryOperator::createXor(Op0, Op1, I.getName()+"tmp"); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5161 | InsertNewInstBefore(Xor, I); |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5162 | return BinaryOperator::createNot(Xor); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5163 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5164 | case ICmpInst::ICMP_NE: // icmp eq bool %A, %B -> A^B |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 5165 | return BinaryOperator::createXor(Op0, Op1); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5166 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5167 | case ICmpInst::ICMP_UGT: |
| 5168 | case ICmpInst::ICMP_SGT: |
| 5169 | std::swap(Op0, Op1); // Change icmp gt -> icmp lt |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 5170 | // FALL THROUGH |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5171 | case ICmpInst::ICMP_ULT: |
| 5172 | case ICmpInst::ICMP_SLT: { // icmp lt bool A, B -> ~X & Y |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 5173 | Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp"); |
| 5174 | InsertNewInstBefore(Not, I); |
| 5175 | return BinaryOperator::createAnd(Not, Op1); |
| 5176 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5177 | case ICmpInst::ICMP_UGE: |
| 5178 | case ICmpInst::ICMP_SGE: |
| 5179 | std::swap(Op0, Op1); // Change icmp ge -> icmp le |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 5180 | // FALL THROUGH |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5181 | case ICmpInst::ICMP_ULE: |
| 5182 | case ICmpInst::ICMP_SLE: { // icmp le bool %A, %B -> ~A | B |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 5183 | Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp"); |
| 5184 | InsertNewInstBefore(Not, I); |
| 5185 | return BinaryOperator::createOr(Not, Op1); |
| 5186 | } |
| 5187 | } |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5188 | } |
| 5189 | |
Chris Lattner | 2be51ae | 2004-06-09 04:24:29 +0000 | [diff] [blame] | 5190 | // See if we are doing a comparison between a constant and an instruction that |
| 5191 | // can be folded into the comparison. |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5192 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Christopher Lamb | 103e1a3 | 2007-12-20 07:21:11 +0000 | [diff] [blame] | 5193 | Value *A, *B; |
| 5194 | |
Chris Lattner | b656601 | 2008-01-05 01:18:20 +0000 | [diff] [blame] | 5195 | // (icmp ne/eq (sub A B) 0) -> (icmp ne/eq A, B) |
| 5196 | if (I.isEquality() && CI->isNullValue() && |
| 5197 | match(Op0, m_Sub(m_Value(A), m_Value(B)))) { |
| 5198 | // (icmp cond A B) if cond is equality |
| 5199 | return new ICmpInst(I.getPredicate(), A, B); |
Owen Anderson | f5783f8 | 2007-12-28 07:42:12 +0000 | [diff] [blame] | 5200 | } |
Christopher Lamb | 103e1a3 | 2007-12-20 07:21:11 +0000 | [diff] [blame] | 5201 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5202 | switch (I.getPredicate()) { |
| 5203 | default: break; |
| 5204 | case ICmpInst::ICMP_ULT: // A <u MIN -> FALSE |
| 5205 | if (CI->isMinValue(false)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5206 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5207 | if (CI->isMaxValue(false)) // A <u MAX -> A != MAX |
| 5208 | return new ICmpInst(ICmpInst::ICMP_NE, Op0,Op1); |
| 5209 | if (isMinValuePlusOne(CI,false)) // A <u MIN+1 -> A == MIN |
| 5210 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, SubOne(CI)); |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 5211 | // (x <u 2147483648) -> (x >s -1) -> true if sign bit clear |
| 5212 | if (CI->isMinValue(true)) |
| 5213 | return new ICmpInst(ICmpInst::ICMP_SGT, Op0, |
| 5214 | ConstantInt::getAllOnesValue(Op0->getType())); |
| 5215 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5216 | break; |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 5217 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5218 | case ICmpInst::ICMP_SLT: |
| 5219 | if (CI->isMinValue(true)) // A <s MIN -> FALSE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5220 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5221 | if (CI->isMaxValue(true)) // A <s MAX -> A != MAX |
| 5222 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
| 5223 | if (isMinValuePlusOne(CI,true)) // A <s MIN+1 -> A == MIN |
| 5224 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, SubOne(CI)); |
| 5225 | break; |
| 5226 | |
| 5227 | case ICmpInst::ICMP_UGT: |
| 5228 | if (CI->isMaxValue(false)) // A >u MAX -> FALSE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5229 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5230 | if (CI->isMinValue(false)) // A >u MIN -> A != MIN |
| 5231 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
| 5232 | if (isMaxValueMinusOne(CI, false)) // A >u MAX-1 -> A == MAX |
| 5233 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, AddOne(CI)); |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 5234 | |
| 5235 | // (x >u 2147483647) -> (x <s 0) -> true if sign bit set |
| 5236 | if (CI->isMaxValue(true)) |
| 5237 | return new ICmpInst(ICmpInst::ICMP_SLT, Op0, |
| 5238 | ConstantInt::getNullValue(Op0->getType())); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5239 | break; |
| 5240 | |
| 5241 | case ICmpInst::ICMP_SGT: |
| 5242 | if (CI->isMaxValue(true)) // A >s MAX -> FALSE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5243 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5244 | if (CI->isMinValue(true)) // A >s MIN -> A != MIN |
| 5245 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
| 5246 | if (isMaxValueMinusOne(CI, true)) // A >s MAX-1 -> A == MAX |
| 5247 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, AddOne(CI)); |
| 5248 | break; |
| 5249 | |
| 5250 | case ICmpInst::ICMP_ULE: |
| 5251 | if (CI->isMaxValue(false)) // A <=u MAX -> TRUE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5252 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5253 | if (CI->isMinValue(false)) // A <=u MIN -> A == MIN |
| 5254 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); |
| 5255 | if (isMaxValueMinusOne(CI,false)) // A <=u MAX-1 -> A != MAX |
| 5256 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, AddOne(CI)); |
| 5257 | break; |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 5258 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5259 | case ICmpInst::ICMP_SLE: |
| 5260 | if (CI->isMaxValue(true)) // A <=s MAX -> TRUE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5261 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5262 | if (CI->isMinValue(true)) // A <=s MIN -> A == MIN |
| 5263 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); |
| 5264 | if (isMaxValueMinusOne(CI,true)) // A <=s MAX-1 -> A != MAX |
| 5265 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, AddOne(CI)); |
| 5266 | break; |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 5267 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5268 | case ICmpInst::ICMP_UGE: |
| 5269 | if (CI->isMinValue(false)) // A >=u MIN -> TRUE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5270 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5271 | if (CI->isMaxValue(false)) // A >=u MAX -> A == MAX |
| 5272 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); |
| 5273 | if (isMinValuePlusOne(CI,false)) // A >=u MIN-1 -> A != MIN |
| 5274 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, SubOne(CI)); |
| 5275 | break; |
| 5276 | |
| 5277 | case ICmpInst::ICMP_SGE: |
| 5278 | if (CI->isMinValue(true)) // A >=s MIN -> TRUE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5279 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5280 | if (CI->isMaxValue(true)) // A >=s MAX -> A == MAX |
| 5281 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); |
| 5282 | if (isMinValuePlusOne(CI,true)) // A >=s MIN-1 -> A != MIN |
| 5283 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, SubOne(CI)); |
| 5284 | break; |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 5285 | } |
| 5286 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5287 | // If we still have a icmp le or icmp ge instruction, turn it into the |
| 5288 | // appropriate icmp lt or icmp gt instruction. Since the border cases have |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 5289 | // already been handled above, this requires little checking. |
| 5290 | // |
Reid Spencer | 2149a9d | 2007-03-25 19:55:33 +0000 | [diff] [blame] | 5291 | switch (I.getPredicate()) { |
Chris Lattner | 4241e4d | 2007-07-15 20:54:51 +0000 | [diff] [blame] | 5292 | default: break; |
| 5293 | case ICmpInst::ICMP_ULE: |
| 5294 | return new ICmpInst(ICmpInst::ICMP_ULT, Op0, AddOne(CI)); |
| 5295 | case ICmpInst::ICMP_SLE: |
| 5296 | return new ICmpInst(ICmpInst::ICMP_SLT, Op0, AddOne(CI)); |
| 5297 | case ICmpInst::ICMP_UGE: |
| 5298 | return new ICmpInst( ICmpInst::ICMP_UGT, Op0, SubOne(CI)); |
| 5299 | case ICmpInst::ICMP_SGE: |
| 5300 | return new ICmpInst(ICmpInst::ICMP_SGT, Op0, SubOne(CI)); |
Reid Spencer | 2149a9d | 2007-03-25 19:55:33 +0000 | [diff] [blame] | 5301 | } |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 5302 | |
| 5303 | // 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] | 5304 | // in the input. If this comparison is a normal comparison, it demands all |
| 5305 | // bits, if it is a sign bit comparison, it only demands the sign bit. |
| 5306 | |
| 5307 | bool UnusedBit; |
| 5308 | bool isSignBit = isSignBitCheck(I.getPredicate(), CI, UnusedBit); |
| 5309 | |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5310 | uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth(); |
| 5311 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
Chris Lattner | 4241e4d | 2007-07-15 20:54:51 +0000 | [diff] [blame] | 5312 | if (SimplifyDemandedBits(Op0, |
| 5313 | isSignBit ? APInt::getSignBit(BitWidth) |
| 5314 | : APInt::getAllOnesValue(BitWidth), |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 5315 | KnownZero, KnownOne, 0)) |
| 5316 | return &I; |
| 5317 | |
| 5318 | // Given the known and unknown bits, compute a range that the LHS could be |
| 5319 | // in. |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5320 | if ((KnownOne | KnownZero) != 0) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5321 | // Compute the Min, Max and RHS values based on the known bits. For the |
| 5322 | // EQ and NE we use unsigned values. |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 5323 | APInt Min(BitWidth, 0), Max(BitWidth, 0); |
| 5324 | const APInt& RHSVal = CI->getValue(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5325 | if (ICmpInst::isSignedPredicate(I.getPredicate())) { |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5326 | ComputeSignedMinMaxValuesFromKnownBits(Ty, KnownZero, KnownOne, Min, |
| 5327 | Max); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5328 | } else { |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5329 | ComputeUnsignedMinMaxValuesFromKnownBits(Ty, KnownZero, KnownOne, Min, |
| 5330 | Max); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5331 | } |
| 5332 | switch (I.getPredicate()) { // LE/GE have been folded already. |
| 5333 | default: assert(0 && "Unknown icmp opcode!"); |
| 5334 | case ICmpInst::ICMP_EQ: |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5335 | if (Max.ult(RHSVal) || Min.ugt(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5336 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5337 | break; |
| 5338 | case ICmpInst::ICMP_NE: |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5339 | if (Max.ult(RHSVal) || Min.ugt(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5340 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5341 | break; |
| 5342 | case ICmpInst::ICMP_ULT: |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5343 | if (Max.ult(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5344 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | 81973ef | 2007-04-09 23:52:13 +0000 | [diff] [blame] | 5345 | if (Min.uge(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5346 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5347 | break; |
| 5348 | case ICmpInst::ICMP_UGT: |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5349 | if (Min.ugt(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5350 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | 81973ef | 2007-04-09 23:52:13 +0000 | [diff] [blame] | 5351 | if (Max.ule(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5352 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5353 | break; |
| 5354 | case ICmpInst::ICMP_SLT: |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5355 | if (Max.slt(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5356 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5357 | if (Min.sgt(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5358 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5359 | break; |
| 5360 | case ICmpInst::ICMP_SGT: |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5361 | if (Min.sgt(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5362 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | 81973ef | 2007-04-09 23:52:13 +0000 | [diff] [blame] | 5363 | if (Max.sle(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5364 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5365 | break; |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 5366 | } |
| 5367 | } |
| 5368 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5369 | // 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] | 5370 | // instruction, see if that instruction also has constants so that the |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5371 | // instruction can be folded into the icmp |
Chris Lattner | 3c6a0d4 | 2004-05-25 06:32:08 +0000 | [diff] [blame] | 5372 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5373 | if (Instruction *Res = visitICmpInstWithInstAndIntCst(I, LHSI, CI)) |
| 5374 | return Res; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5375 | } |
| 5376 | |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5377 | // Handle icmp with constant (but not simple integer constant) RHS |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5378 | if (Constant *RHSC = dyn_cast<Constant>(Op1)) { |
| 5379 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
| 5380 | switch (LHSI->getOpcode()) { |
Chris Lattner | 9fb25db | 2005-05-01 04:42:15 +0000 | [diff] [blame] | 5381 | case Instruction::GetElementPtr: |
| 5382 | if (RHSC->isNullValue()) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5383 | // 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] | 5384 | bool isAllZeros = true; |
| 5385 | for (unsigned i = 1, e = LHSI->getNumOperands(); i != e; ++i) |
| 5386 | if (!isa<Constant>(LHSI->getOperand(i)) || |
| 5387 | !cast<Constant>(LHSI->getOperand(i))->isNullValue()) { |
| 5388 | isAllZeros = false; |
| 5389 | break; |
| 5390 | } |
| 5391 | if (isAllZeros) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5392 | return new ICmpInst(I.getPredicate(), LHSI->getOperand(0), |
Chris Lattner | 9fb25db | 2005-05-01 04:42:15 +0000 | [diff] [blame] | 5393 | Constant::getNullValue(LHSI->getOperand(0)->getType())); |
| 5394 | } |
| 5395 | break; |
| 5396 | |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5397 | case Instruction::PHI: |
| 5398 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 5399 | return NV; |
| 5400 | break; |
Chris Lattner | 4802d90 | 2007-04-06 18:57:34 +0000 | [diff] [blame] | 5401 | case Instruction::Select: { |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5402 | // If either operand of the select is a constant, we can fold the |
| 5403 | // comparison into the select arms, which will cause one to be |
| 5404 | // constant folded and the select turned into a bitwise or. |
| 5405 | Value *Op1 = 0, *Op2 = 0; |
| 5406 | if (LHSI->hasOneUse()) { |
| 5407 | if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) { |
| 5408 | // Fold the known value into the constant operand. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5409 | Op1 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC); |
| 5410 | // Insert a new ICmp of the other select operand. |
| 5411 | Op2 = InsertNewInstBefore(new ICmpInst(I.getPredicate(), |
| 5412 | LHSI->getOperand(2), RHSC, |
| 5413 | I.getName()), I); |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5414 | } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) { |
| 5415 | // Fold the known value into the constant operand. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5416 | Op2 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC); |
| 5417 | // Insert a new ICmp of the other select operand. |
| 5418 | Op1 = InsertNewInstBefore(new ICmpInst(I.getPredicate(), |
| 5419 | LHSI->getOperand(1), RHSC, |
| 5420 | I.getName()), I); |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5421 | } |
| 5422 | } |
Jeff Cohen | 9d80930 | 2005-04-23 21:38:35 +0000 | [diff] [blame] | 5423 | |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5424 | if (Op1) |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 5425 | return SelectInst::Create(LHSI->getOperand(0), Op1, Op2); |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5426 | break; |
| 5427 | } |
Chris Lattner | 4802d90 | 2007-04-06 18:57:34 +0000 | [diff] [blame] | 5428 | case Instruction::Malloc: |
| 5429 | // If we have (malloc != null), and if the malloc has a single use, we |
| 5430 | // can assume it is successful and remove the malloc. |
| 5431 | if (LHSI->hasOneUse() && isa<ConstantPointerNull>(RHSC)) { |
| 5432 | AddToWorkList(LHSI); |
| 5433 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
| 5434 | !isTrueWhenEqual(I))); |
| 5435 | } |
| 5436 | break; |
| 5437 | } |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5438 | } |
| 5439 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5440 | // 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] | 5441 | if (User *GEP = dyn_castGetElementPtr(Op0)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5442 | if (Instruction *NI = FoldGEPICmp(GEP, Op1, I.getPredicate(), I)) |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5443 | return NI; |
| 5444 | if (User *GEP = dyn_castGetElementPtr(Op1)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5445 | if (Instruction *NI = FoldGEPICmp(GEP, Op0, |
| 5446 | ICmpInst::getSwappedPredicate(I.getPredicate()), I)) |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5447 | return NI; |
| 5448 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5449 | // 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] | 5450 | // values. If the ptr->ptr cast can be stripped off both arguments, we do so |
| 5451 | // now. |
| 5452 | if (BitCastInst *CI = dyn_cast<BitCastInst>(Op0)) { |
| 5453 | if (isa<PointerType>(Op0->getType()) && |
| 5454 | (isa<Constant>(Op1) || isa<BitCastInst>(Op1))) { |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5455 | // We keep moving the cast from the left operand over to the right |
| 5456 | // operand, where it can often be eliminated completely. |
Chris Lattner | 57d8637 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 5457 | Op0 = CI->getOperand(0); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5458 | |
Chris Lattner | 57d8637 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 5459 | // If operand #1 is a bitcast instruction, it must also be a ptr->ptr cast |
| 5460 | // so eliminate it as well. |
| 5461 | if (BitCastInst *CI2 = dyn_cast<BitCastInst>(Op1)) |
| 5462 | Op1 = CI2->getOperand(0); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5463 | |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5464 | // If Op1 is a constant, we can fold the cast into the constant. |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 5465 | if (Op0->getType() != Op1->getType()) { |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5466 | if (Constant *Op1C = dyn_cast<Constant>(Op1)) { |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 5467 | Op1 = ConstantExpr::getBitCast(Op1C, Op0->getType()); |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5468 | } else { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5469 | // Otherwise, cast the RHS right before the icmp |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 5470 | Op1 = InsertBitCastBefore(Op1, Op0->getType(), I); |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5471 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 5472 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5473 | return new ICmpInst(I.getPredicate(), Op0, Op1); |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5474 | } |
Chris Lattner | 57d8637 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 5475 | } |
| 5476 | |
| 5477 | if (isa<CastInst>(Op0)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5478 | // Handle the special case of: icmp (cast bool to X), <cst> |
Chris Lattner | 6870805 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 5479 | // This comes up when you have code like |
| 5480 | // int X = A < B; |
| 5481 | // if (X) ... |
| 5482 | // For generality, we handle any zero-extension of any operand comparison |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5483 | // with a constant or another cast from the same type. |
| 5484 | if (isa<ConstantInt>(Op1) || isa<CastInst>(Op1)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5485 | if (Instruction *R = visitICmpInstWithCastAndCast(I)) |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5486 | return R; |
Chris Lattner | 6870805 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 5487 | } |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5488 | |
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 5489 | if (I.isEquality()) { |
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5490 | Value *A, *B, *C, *D; |
| 5491 | if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) { |
| 5492 | if (A == Op1 || B == Op1) { // (A^B) == A -> B == 0 |
| 5493 | Value *OtherVal = A == Op1 ? B : A; |
| 5494 | return new ICmpInst(I.getPredicate(), OtherVal, |
| 5495 | Constant::getNullValue(A->getType())); |
| 5496 | } |
| 5497 | |
| 5498 | if (match(Op1, m_Xor(m_Value(C), m_Value(D)))) { |
| 5499 | // A^c1 == C^c2 --> A == C^(c1^c2) |
| 5500 | if (ConstantInt *C1 = dyn_cast<ConstantInt>(B)) |
| 5501 | if (ConstantInt *C2 = dyn_cast<ConstantInt>(D)) |
| 5502 | if (Op1->hasOneUse()) { |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 5503 | Constant *NC = ConstantInt::get(C1->getValue() ^ C2->getValue()); |
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5504 | Instruction *Xor = BinaryOperator::createXor(C, NC, "tmp"); |
| 5505 | return new ICmpInst(I.getPredicate(), A, |
| 5506 | InsertNewInstBefore(Xor, I)); |
| 5507 | } |
| 5508 | |
| 5509 | // A^B == A^D -> B == D |
| 5510 | if (A == C) return new ICmpInst(I.getPredicate(), B, D); |
| 5511 | if (A == D) return new ICmpInst(I.getPredicate(), B, C); |
| 5512 | if (B == C) return new ICmpInst(I.getPredicate(), A, D); |
| 5513 | if (B == D) return new ICmpInst(I.getPredicate(), A, C); |
| 5514 | } |
| 5515 | } |
| 5516 | |
| 5517 | if (match(Op1, m_Xor(m_Value(A), m_Value(B))) && |
| 5518 | (A == Op0 || B == Op0)) { |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5519 | // A == (A^B) -> B == 0 |
| 5520 | Value *OtherVal = A == Op0 ? B : A; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5521 | return new ICmpInst(I.getPredicate(), OtherVal, |
| 5522 | Constant::getNullValue(A->getType())); |
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5523 | } |
| 5524 | 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] | 5525 | // (A-B) == A -> B == 0 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5526 | return new ICmpInst(I.getPredicate(), B, |
| 5527 | Constant::getNullValue(B->getType())); |
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5528 | } |
| 5529 | 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] | 5530 | // A == (A-B) -> B == 0 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5531 | return new ICmpInst(I.getPredicate(), B, |
| 5532 | Constant::getNullValue(B->getType())); |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5533 | } |
Chris Lattner | 9c2328e | 2006-11-14 06:06:06 +0000 | [diff] [blame] | 5534 | |
Chris Lattner | 9c2328e | 2006-11-14 06:06:06 +0000 | [diff] [blame] | 5535 | // (X&Z) == (Y&Z) -> (X^Y) & Z == 0 |
| 5536 | if (Op0->hasOneUse() && Op1->hasOneUse() && |
| 5537 | match(Op0, m_And(m_Value(A), m_Value(B))) && |
| 5538 | match(Op1, m_And(m_Value(C), m_Value(D)))) { |
| 5539 | Value *X = 0, *Y = 0, *Z = 0; |
| 5540 | |
| 5541 | if (A == C) { |
| 5542 | X = B; Y = D; Z = A; |
| 5543 | } else if (A == D) { |
| 5544 | X = B; Y = C; Z = A; |
| 5545 | } else if (B == C) { |
| 5546 | X = A; Y = D; Z = B; |
| 5547 | } else if (B == D) { |
| 5548 | X = A; Y = C; Z = B; |
| 5549 | } |
| 5550 | |
| 5551 | if (X) { // Build (X^Y) & Z |
| 5552 | Op1 = InsertNewInstBefore(BinaryOperator::createXor(X, Y, "tmp"), I); |
| 5553 | Op1 = InsertNewInstBefore(BinaryOperator::createAnd(Op1, Z, "tmp"), I); |
| 5554 | I.setOperand(0, Op1); |
| 5555 | I.setOperand(1, Constant::getNullValue(Op1->getType())); |
| 5556 | return &I; |
| 5557 | } |
| 5558 | } |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5559 | } |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 5560 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5561 | } |
| 5562 | |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5563 | |
| 5564 | /// FoldICmpDivCst - Fold "icmp pred, ([su]div X, DivRHS), CmpRHS" where DivRHS |
| 5565 | /// and CmpRHS are both known to be integer constants. |
| 5566 | Instruction *InstCombiner::FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI, |
| 5567 | ConstantInt *DivRHS) { |
| 5568 | ConstantInt *CmpRHS = cast<ConstantInt>(ICI.getOperand(1)); |
| 5569 | const APInt &CmpRHSV = CmpRHS->getValue(); |
| 5570 | |
| 5571 | // FIXME: If the operand types don't match the type of the divide |
| 5572 | // then don't attempt this transform. The code below doesn't have the |
| 5573 | // logic to deal with a signed divide and an unsigned compare (and |
| 5574 | // vice versa). This is because (x /s C1) <s C2 produces different |
| 5575 | // results than (x /s C1) <u C2 or (x /u C1) <s C2 or even |
| 5576 | // (x /u C1) <u C2. Simply casting the operands and result won't |
| 5577 | // work. :( The if statement below tests that condition and bails |
| 5578 | // if it finds it. |
| 5579 | bool DivIsSigned = DivI->getOpcode() == Instruction::SDiv; |
| 5580 | if (!ICI.isEquality() && DivIsSigned != ICI.isSignedPredicate()) |
| 5581 | return 0; |
| 5582 | if (DivRHS->isZero()) |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5583 | return 0; // The ProdOV computation fails on divide by zero. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5584 | |
| 5585 | // Compute Prod = CI * DivRHS. We are essentially solving an equation |
| 5586 | // of form X/C1=C2. We solve for X by multiplying C1 (DivRHS) and |
| 5587 | // C2 (CI). By solving for X we can turn this into a range check |
| 5588 | // instead of computing a divide. |
| 5589 | ConstantInt *Prod = Multiply(CmpRHS, DivRHS); |
| 5590 | |
| 5591 | // Determine if the product overflows by seeing if the product is |
| 5592 | // not equal to the divide. Make sure we do the same kind of divide |
| 5593 | // as in the LHS instruction that we're folding. |
| 5594 | bool ProdOV = (DivIsSigned ? ConstantExpr::getSDiv(Prod, DivRHS) : |
| 5595 | ConstantExpr::getUDiv(Prod, DivRHS)) != CmpRHS; |
| 5596 | |
| 5597 | // Get the ICmp opcode |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5598 | ICmpInst::Predicate Pred = ICI.getPredicate(); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5599 | |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5600 | // Figure out the interval that is being checked. For example, a comparison |
| 5601 | // like "X /u 5 == 0" is really checking that X is in the interval [0, 5). |
| 5602 | // Compute this interval based on the constants involved and the signedness of |
| 5603 | // the compare/divide. This computes a half-open interval, keeping track of |
| 5604 | // whether either value in the interval overflows. After analysis each |
| 5605 | // overflow variable is set to 0 if it's corresponding bound variable is valid |
| 5606 | // -1 if overflowed off the bottom end, or +1 if overflowed off the top end. |
| 5607 | int LoOverflow = 0, HiOverflow = 0; |
| 5608 | ConstantInt *LoBound = 0, *HiBound = 0; |
| 5609 | |
| 5610 | |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5611 | if (!DivIsSigned) { // udiv |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5612 | // e.g. X/5 op 3 --> [15, 20) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5613 | LoBound = Prod; |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5614 | HiOverflow = LoOverflow = ProdOV; |
| 5615 | if (!HiOverflow) |
| 5616 | HiOverflow = AddWithOverflow(HiBound, LoBound, DivRHS, false); |
Dan Gohman | 7649127 | 2008-02-13 22:09:18 +0000 | [diff] [blame] | 5617 | } else if (DivRHS->getValue().isStrictlyPositive()) { // Divisor is > 0. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5618 | if (CmpRHSV == 0) { // (X / pos) op 0 |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5619 | // Can't overflow. e.g. X/2 op 0 --> [-1, 2) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5620 | LoBound = cast<ConstantInt>(ConstantExpr::getNeg(SubOne(DivRHS))); |
| 5621 | HiBound = DivRHS; |
Dan Gohman | 7649127 | 2008-02-13 22:09:18 +0000 | [diff] [blame] | 5622 | } else if (CmpRHSV.isStrictlyPositive()) { // (X / pos) op pos |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5623 | LoBound = Prod; // e.g. X/5 op 3 --> [15, 20) |
| 5624 | HiOverflow = LoOverflow = ProdOV; |
| 5625 | if (!HiOverflow) |
| 5626 | HiOverflow = AddWithOverflow(HiBound, Prod, DivRHS, true); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5627 | } else { // (X / pos) op neg |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5628 | // e.g. X/5 op -3 --> [-15-4, -15+1) --> [-19, -14) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5629 | Constant *DivRHSH = ConstantExpr::getNeg(SubOne(DivRHS)); |
| 5630 | LoOverflow = AddWithOverflow(LoBound, Prod, |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5631 | cast<ConstantInt>(DivRHSH), true) ? -1 : 0; |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5632 | HiBound = AddOne(Prod); |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5633 | HiOverflow = ProdOV ? -1 : 0; |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5634 | } |
Dan Gohman | 7649127 | 2008-02-13 22:09:18 +0000 | [diff] [blame] | 5635 | } else if (DivRHS->getValue().isNegative()) { // Divisor is < 0. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5636 | if (CmpRHSV == 0) { // (X / neg) op 0 |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5637 | // e.g. X/-5 op 0 --> [-4, 5) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5638 | LoBound = AddOne(DivRHS); |
| 5639 | HiBound = cast<ConstantInt>(ConstantExpr::getNeg(DivRHS)); |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5640 | if (HiBound == DivRHS) { // -INTMIN = INTMIN |
| 5641 | HiOverflow = 1; // [INTMIN+1, overflow) |
| 5642 | HiBound = 0; // e.g. X/INTMIN = 0 --> X > INTMIN |
| 5643 | } |
Dan Gohman | 7649127 | 2008-02-13 22:09:18 +0000 | [diff] [blame] | 5644 | } else if (CmpRHSV.isStrictlyPositive()) { // (X / neg) op pos |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5645 | // e.g. X/-5 op 3 --> [-19, -14) |
| 5646 | HiOverflow = LoOverflow = ProdOV ? -1 : 0; |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5647 | if (!LoOverflow) |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5648 | LoOverflow = AddWithOverflow(LoBound, Prod, AddOne(DivRHS), true) ?-1:0; |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5649 | HiBound = AddOne(Prod); |
| 5650 | } else { // (X / neg) op neg |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5651 | // e.g. X/-5 op -3 --> [15, 20) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5652 | LoBound = Prod; |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5653 | LoOverflow = HiOverflow = ProdOV ? 1 : 0; |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5654 | HiBound = Subtract(Prod, DivRHS); |
| 5655 | } |
| 5656 | |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5657 | // Dividing by a negative swaps the condition. LT <-> GT |
| 5658 | Pred = ICmpInst::getSwappedPredicate(Pred); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5659 | } |
| 5660 | |
| 5661 | Value *X = DivI->getOperand(0); |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5662 | switch (Pred) { |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5663 | default: assert(0 && "Unhandled icmp opcode!"); |
| 5664 | case ICmpInst::ICMP_EQ: |
| 5665 | if (LoOverflow && HiOverflow) |
| 5666 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
| 5667 | else if (HiOverflow) |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5668 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5669 | ICmpInst::ICMP_UGE, X, LoBound); |
| 5670 | else if (LoOverflow) |
| 5671 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : |
| 5672 | ICmpInst::ICMP_ULT, X, HiBound); |
| 5673 | else |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5674 | return InsertRangeTest(X, LoBound, HiBound, DivIsSigned, true, ICI); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5675 | case ICmpInst::ICMP_NE: |
| 5676 | if (LoOverflow && HiOverflow) |
| 5677 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); |
| 5678 | else if (HiOverflow) |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5679 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5680 | ICmpInst::ICMP_ULT, X, LoBound); |
| 5681 | else if (LoOverflow) |
| 5682 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : |
| 5683 | ICmpInst::ICMP_UGE, X, HiBound); |
| 5684 | else |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5685 | return InsertRangeTest(X, LoBound, HiBound, DivIsSigned, false, ICI); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5686 | case ICmpInst::ICMP_ULT: |
| 5687 | case ICmpInst::ICMP_SLT: |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5688 | if (LoOverflow == +1) // Low bound is greater than input range. |
| 5689 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); |
| 5690 | if (LoOverflow == -1) // Low bound is less than input range. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5691 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5692 | return new ICmpInst(Pred, X, LoBound); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5693 | case ICmpInst::ICMP_UGT: |
| 5694 | case ICmpInst::ICMP_SGT: |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5695 | if (HiOverflow == +1) // High bound greater than input range. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5696 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5697 | else if (HiOverflow == -1) // High bound less than input range. |
| 5698 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); |
| 5699 | if (Pred == ICmpInst::ICMP_UGT) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5700 | return new ICmpInst(ICmpInst::ICMP_UGE, X, HiBound); |
| 5701 | else |
| 5702 | return new ICmpInst(ICmpInst::ICMP_SGE, X, HiBound); |
| 5703 | } |
| 5704 | } |
| 5705 | |
| 5706 | |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5707 | /// visitICmpInstWithInstAndIntCst - Handle "icmp (instr, intcst)". |
| 5708 | /// |
| 5709 | Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI, |
| 5710 | Instruction *LHSI, |
| 5711 | ConstantInt *RHS) { |
| 5712 | const APInt &RHSV = RHS->getValue(); |
| 5713 | |
| 5714 | switch (LHSI->getOpcode()) { |
Duncan Sands | 0091bf2 | 2007-04-04 06:42:45 +0000 | [diff] [blame] | 5715 | case Instruction::Xor: // (icmp pred (xor X, XorCST), CI) |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5716 | if (ConstantInt *XorCST = dyn_cast<ConstantInt>(LHSI->getOperand(1))) { |
| 5717 | // If this is a comparison that tests the signbit (X < 0) or (x > -1), |
| 5718 | // fold the xor. |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 5719 | if ((ICI.getPredicate() == ICmpInst::ICMP_SLT && RHSV == 0) || |
| 5720 | (ICI.getPredicate() == ICmpInst::ICMP_SGT && RHSV.isAllOnesValue())) { |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5721 | Value *CompareVal = LHSI->getOperand(0); |
| 5722 | |
| 5723 | // If the sign bit of the XorCST is not set, there is no change to |
| 5724 | // the operation, just stop using the Xor. |
| 5725 | if (!XorCST->getValue().isNegative()) { |
| 5726 | ICI.setOperand(0, CompareVal); |
| 5727 | AddToWorkList(LHSI); |
| 5728 | return &ICI; |
| 5729 | } |
| 5730 | |
| 5731 | // Was the old condition true if the operand is positive? |
| 5732 | bool isTrueIfPositive = ICI.getPredicate() == ICmpInst::ICMP_SGT; |
| 5733 | |
| 5734 | // If so, the new one isn't. |
| 5735 | isTrueIfPositive ^= true; |
| 5736 | |
| 5737 | if (isTrueIfPositive) |
| 5738 | return new ICmpInst(ICmpInst::ICMP_SGT, CompareVal, SubOne(RHS)); |
| 5739 | else |
| 5740 | return new ICmpInst(ICmpInst::ICMP_SLT, CompareVal, AddOne(RHS)); |
| 5741 | } |
| 5742 | } |
| 5743 | break; |
| 5744 | case Instruction::And: // (icmp pred (and X, AndCST), RHS) |
| 5745 | if (LHSI->hasOneUse() && isa<ConstantInt>(LHSI->getOperand(1)) && |
| 5746 | LHSI->getOperand(0)->hasOneUse()) { |
| 5747 | ConstantInt *AndCST = cast<ConstantInt>(LHSI->getOperand(1)); |
| 5748 | |
| 5749 | // If the LHS is an AND of a truncating cast, we can widen the |
| 5750 | // and/compare to be the input width without changing the value |
| 5751 | // produced, eliminating a cast. |
| 5752 | if (TruncInst *Cast = dyn_cast<TruncInst>(LHSI->getOperand(0))) { |
| 5753 | // We can do this transformation if either the AND constant does not |
| 5754 | // have its sign bit set or if it is an equality comparison. |
| 5755 | // Extending a relational comparison when we're checking the sign |
| 5756 | // bit would not work. |
| 5757 | if (Cast->hasOneUse() && |
Anton Korobeynikov | 4aefd6b | 2008-02-20 12:07:57 +0000 | [diff] [blame] | 5758 | (ICI.isEquality() || |
| 5759 | (AndCST->getValue().isNonNegative() && RHSV.isNonNegative()))) { |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5760 | uint32_t BitWidth = |
| 5761 | cast<IntegerType>(Cast->getOperand(0)->getType())->getBitWidth(); |
| 5762 | APInt NewCST = AndCST->getValue(); |
| 5763 | NewCST.zext(BitWidth); |
| 5764 | APInt NewCI = RHSV; |
| 5765 | NewCI.zext(BitWidth); |
| 5766 | Instruction *NewAnd = |
| 5767 | BinaryOperator::createAnd(Cast->getOperand(0), |
| 5768 | ConstantInt::get(NewCST),LHSI->getName()); |
| 5769 | InsertNewInstBefore(NewAnd, ICI); |
| 5770 | return new ICmpInst(ICI.getPredicate(), NewAnd, |
| 5771 | ConstantInt::get(NewCI)); |
| 5772 | } |
| 5773 | } |
| 5774 | |
| 5775 | // If this is: (X >> C1) & C2 != C3 (where any shift and any compare |
| 5776 | // could exist), turn it into (X & (C2 << C1)) != (C3 << C1). This |
| 5777 | // happens a LOT in code produced by the C front-end, for bitfield |
| 5778 | // access. |
| 5779 | BinaryOperator *Shift = dyn_cast<BinaryOperator>(LHSI->getOperand(0)); |
| 5780 | if (Shift && !Shift->isShift()) |
| 5781 | Shift = 0; |
| 5782 | |
| 5783 | ConstantInt *ShAmt; |
| 5784 | ShAmt = Shift ? dyn_cast<ConstantInt>(Shift->getOperand(1)) : 0; |
| 5785 | const Type *Ty = Shift ? Shift->getType() : 0; // Type of the shift. |
| 5786 | const Type *AndTy = AndCST->getType(); // Type of the and. |
| 5787 | |
| 5788 | // We can fold this as long as we can't shift unknown bits |
| 5789 | // into the mask. This can only happen with signed shift |
| 5790 | // rights, as they sign-extend. |
| 5791 | if (ShAmt) { |
| 5792 | bool CanFold = Shift->isLogicalShift(); |
| 5793 | if (!CanFold) { |
| 5794 | // To test for the bad case of the signed shr, see if any |
| 5795 | // of the bits shifted in could be tested after the mask. |
| 5796 | uint32_t TyBits = Ty->getPrimitiveSizeInBits(); |
| 5797 | int ShAmtVal = TyBits - ShAmt->getLimitedValue(TyBits); |
| 5798 | |
| 5799 | uint32_t BitWidth = AndTy->getPrimitiveSizeInBits(); |
| 5800 | if ((APInt::getHighBitsSet(BitWidth, BitWidth-ShAmtVal) & |
| 5801 | AndCST->getValue()) == 0) |
| 5802 | CanFold = true; |
| 5803 | } |
| 5804 | |
| 5805 | if (CanFold) { |
| 5806 | Constant *NewCst; |
| 5807 | if (Shift->getOpcode() == Instruction::Shl) |
| 5808 | NewCst = ConstantExpr::getLShr(RHS, ShAmt); |
| 5809 | else |
| 5810 | NewCst = ConstantExpr::getShl(RHS, ShAmt); |
| 5811 | |
| 5812 | // Check to see if we are shifting out any of the bits being |
| 5813 | // compared. |
| 5814 | if (ConstantExpr::get(Shift->getOpcode(), NewCst, ShAmt) != RHS) { |
| 5815 | // If we shifted bits out, the fold is not going to work out. |
| 5816 | // As a special case, check to see if this means that the |
| 5817 | // result is always true or false now. |
| 5818 | if (ICI.getPredicate() == ICmpInst::ICMP_EQ) |
| 5819 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
| 5820 | if (ICI.getPredicate() == ICmpInst::ICMP_NE) |
| 5821 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); |
| 5822 | } else { |
| 5823 | ICI.setOperand(1, NewCst); |
| 5824 | Constant *NewAndCST; |
| 5825 | if (Shift->getOpcode() == Instruction::Shl) |
| 5826 | NewAndCST = ConstantExpr::getLShr(AndCST, ShAmt); |
| 5827 | else |
| 5828 | NewAndCST = ConstantExpr::getShl(AndCST, ShAmt); |
| 5829 | LHSI->setOperand(1, NewAndCST); |
| 5830 | LHSI->setOperand(0, Shift->getOperand(0)); |
| 5831 | AddToWorkList(Shift); // Shift is dead. |
| 5832 | AddUsesToWorkList(ICI); |
| 5833 | return &ICI; |
| 5834 | } |
| 5835 | } |
| 5836 | } |
| 5837 | |
| 5838 | // Turn ((X >> Y) & C) == 0 into (X & (C << Y)) == 0. The later is |
| 5839 | // preferable because it allows the C<<Y expression to be hoisted out |
| 5840 | // of a loop if Y is invariant and X is not. |
| 5841 | if (Shift && Shift->hasOneUse() && RHSV == 0 && |
| 5842 | ICI.isEquality() && !Shift->isArithmeticShift() && |
| 5843 | isa<Instruction>(Shift->getOperand(0))) { |
| 5844 | // Compute C << Y. |
| 5845 | Value *NS; |
| 5846 | if (Shift->getOpcode() == Instruction::LShr) { |
| 5847 | NS = BinaryOperator::createShl(AndCST, |
| 5848 | Shift->getOperand(1), "tmp"); |
| 5849 | } else { |
| 5850 | // Insert a logical shift. |
| 5851 | NS = BinaryOperator::createLShr(AndCST, |
| 5852 | Shift->getOperand(1), "tmp"); |
| 5853 | } |
| 5854 | InsertNewInstBefore(cast<Instruction>(NS), ICI); |
| 5855 | |
| 5856 | // Compute X & (C << Y). |
| 5857 | Instruction *NewAnd = |
| 5858 | BinaryOperator::createAnd(Shift->getOperand(0), NS, LHSI->getName()); |
| 5859 | InsertNewInstBefore(NewAnd, ICI); |
| 5860 | |
| 5861 | ICI.setOperand(0, NewAnd); |
| 5862 | return &ICI; |
| 5863 | } |
| 5864 | } |
| 5865 | break; |
| 5866 | |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 5867 | case Instruction::Shl: { // (icmp pred (shl X, ShAmt), CI) |
| 5868 | ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1)); |
| 5869 | if (!ShAmt) break; |
| 5870 | |
| 5871 | uint32_t TypeBits = RHSV.getBitWidth(); |
| 5872 | |
| 5873 | // Check that the shift amount is in range. If not, don't perform |
| 5874 | // undefined shifts. When the shift is visited it will be |
| 5875 | // simplified. |
| 5876 | if (ShAmt->uge(TypeBits)) |
| 5877 | break; |
| 5878 | |
| 5879 | if (ICI.isEquality()) { |
| 5880 | // If we are comparing against bits always shifted out, the |
| 5881 | // comparison cannot succeed. |
| 5882 | Constant *Comp = |
| 5883 | ConstantExpr::getShl(ConstantExpr::getLShr(RHS, ShAmt), ShAmt); |
| 5884 | if (Comp != RHS) {// Comparing against a bit that we know is zero. |
| 5885 | bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE; |
| 5886 | Constant *Cst = ConstantInt::get(Type::Int1Ty, IsICMP_NE); |
| 5887 | return ReplaceInstUsesWith(ICI, Cst); |
| 5888 | } |
| 5889 | |
| 5890 | if (LHSI->hasOneUse()) { |
| 5891 | // Otherwise strength reduce the shift into an and. |
| 5892 | uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits); |
| 5893 | Constant *Mask = |
| 5894 | ConstantInt::get(APInt::getLowBitsSet(TypeBits, TypeBits-ShAmtVal)); |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5895 | |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 5896 | Instruction *AndI = |
| 5897 | BinaryOperator::createAnd(LHSI->getOperand(0), |
| 5898 | Mask, LHSI->getName()+".mask"); |
| 5899 | Value *And = InsertNewInstBefore(AndI, ICI); |
| 5900 | return new ICmpInst(ICI.getPredicate(), And, |
| 5901 | ConstantInt::get(RHSV.lshr(ShAmtVal))); |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5902 | } |
| 5903 | } |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 5904 | |
| 5905 | // Otherwise, if this is a comparison of the sign bit, simplify to and/test. |
| 5906 | bool TrueIfSigned = false; |
| 5907 | if (LHSI->hasOneUse() && |
| 5908 | isSignBitCheck(ICI.getPredicate(), RHS, TrueIfSigned)) { |
| 5909 | // (X << 31) <s 0 --> (X&1) != 0 |
| 5910 | Constant *Mask = ConstantInt::get(APInt(TypeBits, 1) << |
| 5911 | (TypeBits-ShAmt->getZExtValue()-1)); |
| 5912 | Instruction *AndI = |
| 5913 | BinaryOperator::createAnd(LHSI->getOperand(0), |
| 5914 | Mask, LHSI->getName()+".mask"); |
| 5915 | Value *And = InsertNewInstBefore(AndI, ICI); |
| 5916 | |
| 5917 | return new ICmpInst(TrueIfSigned ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ, |
| 5918 | And, Constant::getNullValue(And->getType())); |
| 5919 | } |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5920 | break; |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 5921 | } |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5922 | |
| 5923 | case Instruction::LShr: // (icmp pred (shr X, ShAmt), CI) |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 5924 | case Instruction::AShr: { |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 5925 | // Only handle equality comparisons of shift-by-constant. |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 5926 | ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1)); |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 5927 | if (!ShAmt || !ICI.isEquality()) break; |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 5928 | |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 5929 | // Check that the shift amount is in range. If not, don't perform |
| 5930 | // undefined shifts. When the shift is visited it will be |
| 5931 | // simplified. |
| 5932 | uint32_t TypeBits = RHSV.getBitWidth(); |
| 5933 | if (ShAmt->uge(TypeBits)) |
| 5934 | break; |
| 5935 | |
| 5936 | uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits); |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 5937 | |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 5938 | // If we are comparing against bits always shifted out, the |
| 5939 | // comparison cannot succeed. |
| 5940 | APInt Comp = RHSV << ShAmtVal; |
| 5941 | if (LHSI->getOpcode() == Instruction::LShr) |
| 5942 | Comp = Comp.lshr(ShAmtVal); |
| 5943 | else |
| 5944 | Comp = Comp.ashr(ShAmtVal); |
| 5945 | |
| 5946 | if (Comp != RHSV) { // Comparing against a bit that we know is zero. |
| 5947 | bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE; |
| 5948 | Constant *Cst = ConstantInt::get(Type::Int1Ty, IsICMP_NE); |
| 5949 | return ReplaceInstUsesWith(ICI, Cst); |
| 5950 | } |
| 5951 | |
| 5952 | // Otherwise, check to see if the bits shifted out are known to be zero. |
| 5953 | // If so, we can compare against the unshifted value: |
| 5954 | // (X & 4) >> 1 == 2 --> (X & 4) == 4. |
| 5955 | if (MaskedValueIsZero(LHSI->getOperand(0), |
| 5956 | APInt::getLowBitsSet(Comp.getBitWidth(), ShAmtVal))) { |
| 5957 | return new ICmpInst(ICI.getPredicate(), LHSI->getOperand(0), |
| 5958 | ConstantExpr::getShl(RHS, ShAmt)); |
| 5959 | } |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 5960 | |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 5961 | if (LHSI->hasOneUse() || RHSV == 0) { |
| 5962 | // Otherwise strength reduce the shift into an and. |
| 5963 | APInt Val(APInt::getHighBitsSet(TypeBits, TypeBits - ShAmtVal)); |
| 5964 | Constant *Mask = ConstantInt::get(Val); |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 5965 | |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 5966 | Instruction *AndI = |
| 5967 | BinaryOperator::createAnd(LHSI->getOperand(0), |
| 5968 | Mask, LHSI->getName()+".mask"); |
| 5969 | Value *And = InsertNewInstBefore(AndI, ICI); |
| 5970 | return new ICmpInst(ICI.getPredicate(), And, |
| 5971 | ConstantExpr::getShl(RHS, ShAmt)); |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5972 | } |
| 5973 | break; |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 5974 | } |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5975 | |
| 5976 | case Instruction::SDiv: |
| 5977 | case Instruction::UDiv: |
| 5978 | // Fold: icmp pred ([us]div X, C1), C2 -> range test |
| 5979 | // Fold this div into the comparison, producing a range check. |
| 5980 | // Determine, based on the divide type, what the range is being |
| 5981 | // checked. If there is an overflow on the low or high side, remember |
| 5982 | // it, otherwise compute the range [low, hi) bounding the new value. |
| 5983 | // See: InsertRangeTest above for the kinds of replacements possible. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5984 | if (ConstantInt *DivRHS = dyn_cast<ConstantInt>(LHSI->getOperand(1))) |
| 5985 | if (Instruction *R = FoldICmpDivCst(ICI, cast<BinaryOperator>(LHSI), |
| 5986 | DivRHS)) |
| 5987 | return R; |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5988 | break; |
Nick Lewycky | 5be2920 | 2008-02-03 16:33:09 +0000 | [diff] [blame] | 5989 | |
| 5990 | case Instruction::Add: |
| 5991 | // Fold: icmp pred (add, X, C1), C2 |
| 5992 | |
| 5993 | if (!ICI.isEquality()) { |
| 5994 | ConstantInt *LHSC = dyn_cast<ConstantInt>(LHSI->getOperand(1)); |
| 5995 | if (!LHSC) break; |
| 5996 | const APInt &LHSV = LHSC->getValue(); |
| 5997 | |
| 5998 | ConstantRange CR = ICI.makeConstantRange(ICI.getPredicate(), RHSV) |
| 5999 | .subtract(LHSV); |
| 6000 | |
| 6001 | if (ICI.isSignedPredicate()) { |
| 6002 | if (CR.getLower().isSignBit()) { |
| 6003 | return new ICmpInst(ICmpInst::ICMP_SLT, LHSI->getOperand(0), |
| 6004 | ConstantInt::get(CR.getUpper())); |
| 6005 | } else if (CR.getUpper().isSignBit()) { |
| 6006 | return new ICmpInst(ICmpInst::ICMP_SGE, LHSI->getOperand(0), |
| 6007 | ConstantInt::get(CR.getLower())); |
| 6008 | } |
| 6009 | } else { |
| 6010 | if (CR.getLower().isMinValue()) { |
| 6011 | return new ICmpInst(ICmpInst::ICMP_ULT, LHSI->getOperand(0), |
| 6012 | ConstantInt::get(CR.getUpper())); |
| 6013 | } else if (CR.getUpper().isMinValue()) { |
| 6014 | return new ICmpInst(ICmpInst::ICMP_UGE, LHSI->getOperand(0), |
| 6015 | ConstantInt::get(CR.getLower())); |
| 6016 | } |
| 6017 | } |
| 6018 | } |
| 6019 | break; |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6020 | } |
| 6021 | |
| 6022 | // Simplify icmp_eq and icmp_ne instructions with integer constant RHS. |
| 6023 | if (ICI.isEquality()) { |
| 6024 | bool isICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE; |
| 6025 | |
| 6026 | // If the first operand is (add|sub|and|or|xor|rem) with a constant, and |
| 6027 | // the second operand is a constant, simplify a bit. |
| 6028 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(LHSI)) { |
| 6029 | switch (BO->getOpcode()) { |
| 6030 | case Instruction::SRem: |
| 6031 | // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one. |
| 6032 | if (RHSV == 0 && isa<ConstantInt>(BO->getOperand(1)) &&BO->hasOneUse()){ |
| 6033 | const APInt &V = cast<ConstantInt>(BO->getOperand(1))->getValue(); |
| 6034 | if (V.sgt(APInt(V.getBitWidth(), 1)) && V.isPowerOf2()) { |
| 6035 | Instruction *NewRem = |
| 6036 | BinaryOperator::createURem(BO->getOperand(0), BO->getOperand(1), |
| 6037 | BO->getName()); |
| 6038 | InsertNewInstBefore(NewRem, ICI); |
| 6039 | return new ICmpInst(ICI.getPredicate(), NewRem, |
| 6040 | Constant::getNullValue(BO->getType())); |
| 6041 | } |
| 6042 | } |
| 6043 | break; |
| 6044 | case Instruction::Add: |
| 6045 | // Replace ((add A, B) != C) with (A != C-B) if B & C are constants. |
| 6046 | if (ConstantInt *BOp1C = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
| 6047 | if (BO->hasOneUse()) |
| 6048 | return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), |
| 6049 | Subtract(RHS, BOp1C)); |
| 6050 | } else if (RHSV == 0) { |
| 6051 | // Replace ((add A, B) != 0) with (A != -B) if A or B is |
| 6052 | // efficiently invertible, or if the add has just this one use. |
| 6053 | Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1); |
| 6054 | |
| 6055 | if (Value *NegVal = dyn_castNegVal(BOp1)) |
| 6056 | return new ICmpInst(ICI.getPredicate(), BOp0, NegVal); |
| 6057 | else if (Value *NegVal = dyn_castNegVal(BOp0)) |
| 6058 | return new ICmpInst(ICI.getPredicate(), NegVal, BOp1); |
| 6059 | else if (BO->hasOneUse()) { |
| 6060 | Instruction *Neg = BinaryOperator::createNeg(BOp1); |
| 6061 | InsertNewInstBefore(Neg, ICI); |
| 6062 | Neg->takeName(BO); |
| 6063 | return new ICmpInst(ICI.getPredicate(), BOp0, Neg); |
| 6064 | } |
| 6065 | } |
| 6066 | break; |
| 6067 | case Instruction::Xor: |
| 6068 | // For the xor case, we can xor two constants together, eliminating |
| 6069 | // the explicit xor. |
| 6070 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) |
| 6071 | return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), |
| 6072 | ConstantExpr::getXor(RHS, BOC)); |
| 6073 | |
| 6074 | // FALLTHROUGH |
| 6075 | case Instruction::Sub: |
| 6076 | // Replace (([sub|xor] A, B) != 0) with (A != B) |
| 6077 | if (RHSV == 0) |
| 6078 | return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), |
| 6079 | BO->getOperand(1)); |
| 6080 | break; |
| 6081 | |
| 6082 | case Instruction::Or: |
| 6083 | // If bits are being or'd in that are not present in the constant we |
| 6084 | // are comparing against, then the comparison could never succeed! |
| 6085 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) { |
| 6086 | Constant *NotCI = ConstantExpr::getNot(RHS); |
| 6087 | if (!ConstantExpr::getAnd(BOC, NotCI)->isNullValue()) |
| 6088 | return ReplaceInstUsesWith(ICI, ConstantInt::get(Type::Int1Ty, |
| 6089 | isICMP_NE)); |
| 6090 | } |
| 6091 | break; |
| 6092 | |
| 6093 | case Instruction::And: |
| 6094 | if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
| 6095 | // If bits are being compared against that are and'd out, then the |
| 6096 | // comparison can never succeed! |
| 6097 | if ((RHSV & ~BOC->getValue()) != 0) |
| 6098 | return ReplaceInstUsesWith(ICI, ConstantInt::get(Type::Int1Ty, |
| 6099 | isICMP_NE)); |
| 6100 | |
| 6101 | // If we have ((X & C) == C), turn it into ((X & C) != 0). |
| 6102 | if (RHS == BOC && RHSV.isPowerOf2()) |
| 6103 | return new ICmpInst(isICMP_NE ? ICmpInst::ICMP_EQ : |
| 6104 | ICmpInst::ICMP_NE, LHSI, |
| 6105 | Constant::getNullValue(RHS->getType())); |
| 6106 | |
| 6107 | // Replace (and X, (1 << size(X)-1) != 0) with x s< 0 |
| 6108 | if (isSignBit(BOC)) { |
| 6109 | Value *X = BO->getOperand(0); |
| 6110 | Constant *Zero = Constant::getNullValue(X->getType()); |
| 6111 | ICmpInst::Predicate pred = isICMP_NE ? |
| 6112 | ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGE; |
| 6113 | return new ICmpInst(pred, X, Zero); |
| 6114 | } |
| 6115 | |
| 6116 | // ((X & ~7) == 0) --> X < 8 |
| 6117 | if (RHSV == 0 && isHighOnes(BOC)) { |
| 6118 | Value *X = BO->getOperand(0); |
| 6119 | Constant *NegX = ConstantExpr::getNeg(BOC); |
| 6120 | ICmpInst::Predicate pred = isICMP_NE ? |
| 6121 | ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT; |
| 6122 | return new ICmpInst(pred, X, NegX); |
| 6123 | } |
| 6124 | } |
| 6125 | default: break; |
| 6126 | } |
| 6127 | } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(LHSI)) { |
| 6128 | // Handle icmp {eq|ne} <intrinsic>, intcst. |
| 6129 | if (II->getIntrinsicID() == Intrinsic::bswap) { |
| 6130 | AddToWorkList(II); |
| 6131 | ICI.setOperand(0, II->getOperand(1)); |
| 6132 | ICI.setOperand(1, ConstantInt::get(RHSV.byteSwap())); |
| 6133 | return &ICI; |
| 6134 | } |
| 6135 | } |
| 6136 | } else { // Not a ICMP_EQ/ICMP_NE |
Chris Lattner | e34e9a2 | 2007-04-14 23:32:02 +0000 | [diff] [blame] | 6137 | // If the LHS is a cast from an integral value of the same size, |
| 6138 | // then since we know the RHS is a constant, try to simlify. |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6139 | if (CastInst *Cast = dyn_cast<CastInst>(LHSI)) { |
| 6140 | Value *CastOp = Cast->getOperand(0); |
| 6141 | const Type *SrcTy = CastOp->getType(); |
| 6142 | uint32_t SrcTySize = SrcTy->getPrimitiveSizeInBits(); |
| 6143 | if (SrcTy->isInteger() && |
| 6144 | SrcTySize == Cast->getType()->getPrimitiveSizeInBits()) { |
| 6145 | // If this is an unsigned comparison, try to make the comparison use |
| 6146 | // smaller constant values. |
| 6147 | if (ICI.getPredicate() == ICmpInst::ICMP_ULT && RHSV.isSignBit()) { |
| 6148 | // X u< 128 => X s> -1 |
| 6149 | return new ICmpInst(ICmpInst::ICMP_SGT, CastOp, |
| 6150 | ConstantInt::get(APInt::getAllOnesValue(SrcTySize))); |
| 6151 | } else if (ICI.getPredicate() == ICmpInst::ICMP_UGT && |
| 6152 | RHSV == APInt::getSignedMaxValue(SrcTySize)) { |
| 6153 | // X u> 127 => X s< 0 |
| 6154 | return new ICmpInst(ICmpInst::ICMP_SLT, CastOp, |
| 6155 | Constant::getNullValue(SrcTy)); |
| 6156 | } |
| 6157 | } |
| 6158 | } |
| 6159 | } |
| 6160 | return 0; |
| 6161 | } |
| 6162 | |
| 6163 | /// visitICmpInstWithCastAndCast - Handle icmp (cast x to y), (cast/cst). |
| 6164 | /// We only handle extending casts so far. |
| 6165 | /// |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6166 | Instruction *InstCombiner::visitICmpInstWithCastAndCast(ICmpInst &ICI) { |
| 6167 | const CastInst *LHSCI = cast<CastInst>(ICI.getOperand(0)); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6168 | Value *LHSCIOp = LHSCI->getOperand(0); |
| 6169 | const Type *SrcTy = LHSCIOp->getType(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6170 | const Type *DestTy = LHSCI->getType(); |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 6171 | Value *RHSCIOp; |
| 6172 | |
Chris Lattner | 8c756c1 | 2007-05-05 22:41:33 +0000 | [diff] [blame] | 6173 | // Turn icmp (ptrtoint x), (ptrtoint/c) into a compare of the input if the |
| 6174 | // integer type is the same size as the pointer type. |
| 6175 | if (LHSCI->getOpcode() == Instruction::PtrToInt && |
| 6176 | getTargetData().getPointerSizeInBits() == |
| 6177 | cast<IntegerType>(DestTy)->getBitWidth()) { |
| 6178 | Value *RHSOp = 0; |
| 6179 | if (Constant *RHSC = dyn_cast<Constant>(ICI.getOperand(1))) { |
Chris Lattner | 6f6f512 | 2007-05-06 07:24:03 +0000 | [diff] [blame] | 6180 | RHSOp = ConstantExpr::getIntToPtr(RHSC, SrcTy); |
Chris Lattner | 8c756c1 | 2007-05-05 22:41:33 +0000 | [diff] [blame] | 6181 | } else if (PtrToIntInst *RHSC = dyn_cast<PtrToIntInst>(ICI.getOperand(1))) { |
| 6182 | RHSOp = RHSC->getOperand(0); |
| 6183 | // If the pointer types don't match, insert a bitcast. |
| 6184 | if (LHSCIOp->getType() != RHSOp->getType()) |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 6185 | RHSOp = InsertBitCastBefore(RHSOp, LHSCIOp->getType(), ICI); |
Chris Lattner | 8c756c1 | 2007-05-05 22:41:33 +0000 | [diff] [blame] | 6186 | } |
| 6187 | |
| 6188 | if (RHSOp) |
| 6189 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSOp); |
| 6190 | } |
| 6191 | |
| 6192 | // The code below only handles extension cast instructions, so far. |
| 6193 | // Enforce this. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6194 | if (LHSCI->getOpcode() != Instruction::ZExt && |
| 6195 | LHSCI->getOpcode() != Instruction::SExt) |
Chris Lattner | b352fa5 | 2005-01-17 03:20:02 +0000 | [diff] [blame] | 6196 | return 0; |
| 6197 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6198 | bool isSignedExt = LHSCI->getOpcode() == Instruction::SExt; |
| 6199 | bool isSignedCmp = ICI.isSignedPredicate(); |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 6200 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6201 | if (CastInst *CI = dyn_cast<CastInst>(ICI.getOperand(1))) { |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 6202 | // Not an extension from the same type? |
| 6203 | RHSCIOp = CI->getOperand(0); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6204 | if (RHSCIOp->getType() != LHSCIOp->getType()) |
| 6205 | return 0; |
Chris Lattner | a5c5e77 | 2007-01-13 23:11:38 +0000 | [diff] [blame] | 6206 | |
Nick Lewycky | 4189a53 | 2008-01-28 03:48:02 +0000 | [diff] [blame] | 6207 | // 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] | 6208 | // and the other is a zext), then we can't handle this. |
| 6209 | if (CI->getOpcode() != LHSCI->getOpcode()) |
| 6210 | return 0; |
| 6211 | |
Nick Lewycky | 4189a53 | 2008-01-28 03:48:02 +0000 | [diff] [blame] | 6212 | // Deal with equality cases early. |
| 6213 | if (ICI.isEquality()) |
| 6214 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSCIOp); |
| 6215 | |
| 6216 | // A signed comparison of sign extended values simplifies into a |
| 6217 | // signed comparison. |
| 6218 | if (isSignedCmp && isSignedExt) |
| 6219 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSCIOp); |
| 6220 | |
| 6221 | // The other three cases all fold into an unsigned comparison. |
| 6222 | return new ICmpInst(ICI.getUnsignedPredicate(), LHSCIOp, RHSCIOp); |
Reid Spencer | 6731d5c | 2004-11-28 21:31:15 +0000 | [diff] [blame] | 6223 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 6224 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6225 | // If we aren't dealing with a constant on the RHS, exit early |
| 6226 | ConstantInt *CI = dyn_cast<ConstantInt>(ICI.getOperand(1)); |
| 6227 | if (!CI) |
| 6228 | return 0; |
| 6229 | |
| 6230 | // Compute the constant that would happen if we truncated to SrcTy then |
| 6231 | // reextended to DestTy. |
| 6232 | Constant *Res1 = ConstantExpr::getTrunc(CI, SrcTy); |
| 6233 | Constant *Res2 = ConstantExpr::getCast(LHSCI->getOpcode(), Res1, DestTy); |
| 6234 | |
| 6235 | // If the re-extended constant didn't change... |
| 6236 | if (Res2 == CI) { |
| 6237 | // Make sure that sign of the Cmp and the sign of the Cast are the same. |
| 6238 | // For example, we might have: |
| 6239 | // %A = sext short %X to uint |
| 6240 | // %B = icmp ugt uint %A, 1330 |
| 6241 | // It is incorrect to transform this into |
| 6242 | // %B = icmp ugt short %X, 1330 |
| 6243 | // because %A may have negative value. |
| 6244 | // |
| 6245 | // However, it is OK if SrcTy is bool (See cast-set.ll testcase) |
| 6246 | // OR operation is EQ/NE. |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 6247 | if (isSignedExt == isSignedCmp || SrcTy == Type::Int1Ty || ICI.isEquality()) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6248 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, Res1); |
| 6249 | else |
| 6250 | return 0; |
| 6251 | } |
| 6252 | |
| 6253 | // The re-extended constant changed so the constant cannot be represented |
| 6254 | // in the shorter type. Consequently, we cannot emit a simple comparison. |
| 6255 | |
| 6256 | // First, handle some easy cases. We know the result cannot be equal at this |
| 6257 | // point so handle the ICI.isEquality() cases |
| 6258 | if (ICI.getPredicate() == ICmpInst::ICMP_EQ) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6259 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6260 | if (ICI.getPredicate() == ICmpInst::ICMP_NE) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6261 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6262 | |
| 6263 | // Evaluate the comparison for LT (we invert for GT below). LE and GE cases |
| 6264 | // should have been folded away previously and not enter in here. |
| 6265 | Value *Result; |
| 6266 | if (isSignedCmp) { |
| 6267 | // We're performing a signed comparison. |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 6268 | if (cast<ConstantInt>(CI)->getValue().isNegative()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6269 | Result = ConstantInt::getFalse(); // X < (small) --> false |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6270 | else |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6271 | Result = ConstantInt::getTrue(); // X < (large) --> true |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6272 | } else { |
| 6273 | // We're performing an unsigned comparison. |
| 6274 | if (isSignedExt) { |
| 6275 | // We're performing an unsigned comp with a sign extended value. |
| 6276 | // This is true if the input is >= 0. [aka >s -1] |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6277 | Constant *NegOne = ConstantInt::getAllOnesValue(SrcTy); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6278 | Result = InsertNewInstBefore(new ICmpInst(ICmpInst::ICMP_SGT, LHSCIOp, |
| 6279 | NegOne, ICI.getName()), ICI); |
| 6280 | } else { |
| 6281 | // Unsigned extend & unsigned compare -> always true. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6282 | Result = ConstantInt::getTrue(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6283 | } |
| 6284 | } |
| 6285 | |
| 6286 | // Finally, return the value computed. |
| 6287 | if (ICI.getPredicate() == ICmpInst::ICMP_ULT || |
| 6288 | ICI.getPredicate() == ICmpInst::ICMP_SLT) { |
| 6289 | return ReplaceInstUsesWith(ICI, Result); |
| 6290 | } else { |
| 6291 | assert((ICI.getPredicate()==ICmpInst::ICMP_UGT || |
| 6292 | ICI.getPredicate()==ICmpInst::ICMP_SGT) && |
| 6293 | "ICmp should be folded!"); |
| 6294 | if (Constant *CI = dyn_cast<Constant>(Result)) |
| 6295 | return ReplaceInstUsesWith(ICI, ConstantExpr::getNot(CI)); |
| 6296 | else |
| 6297 | return BinaryOperator::createNot(Result); |
| 6298 | } |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 6299 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 6300 | |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6301 | Instruction *InstCombiner::visitShl(BinaryOperator &I) { |
| 6302 | return commonShiftTransforms(I); |
| 6303 | } |
| 6304 | |
| 6305 | Instruction *InstCombiner::visitLShr(BinaryOperator &I) { |
| 6306 | return commonShiftTransforms(I); |
| 6307 | } |
| 6308 | |
| 6309 | Instruction *InstCombiner::visitAShr(BinaryOperator &I) { |
Chris Lattner | 348f665 | 2007-12-06 01:59:46 +0000 | [diff] [blame] | 6310 | if (Instruction *R = commonShiftTransforms(I)) |
| 6311 | return R; |
| 6312 | |
| 6313 | Value *Op0 = I.getOperand(0); |
| 6314 | |
| 6315 | // ashr int -1, X = -1 (for any arithmetic shift rights of ~0) |
| 6316 | if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0)) |
| 6317 | if (CSI->isAllOnesValue()) |
| 6318 | return ReplaceInstUsesWith(I, CSI); |
| 6319 | |
| 6320 | // See if we can turn a signed shr into an unsigned shr. |
| 6321 | if (MaskedValueIsZero(Op0, |
| 6322 | APInt::getSignBit(I.getType()->getPrimitiveSizeInBits()))) |
| 6323 | return BinaryOperator::createLShr(Op0, I.getOperand(1)); |
| 6324 | |
| 6325 | return 0; |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6326 | } |
| 6327 | |
| 6328 | Instruction *InstCombiner::commonShiftTransforms(BinaryOperator &I) { |
| 6329 | assert(I.getOperand(1)->getType() == I.getOperand(0)->getType()); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 6330 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 6331 | |
| 6332 | // shl X, 0 == X and shr X, 0 == X |
| 6333 | // shl 0, X == 0 and shr 0, X == 0 |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6334 | if (Op1 == Constant::getNullValue(Op1->getType()) || |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 6335 | Op0 == Constant::getNullValue(Op0->getType())) |
| 6336 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 8d6bbdb | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 6337 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6338 | if (isa<UndefValue>(Op0)) { |
| 6339 | if (I.getOpcode() == Instruction::AShr) // undef >>s X -> undef |
Chris Lattner | 79a564c | 2004-10-16 23:28:04 +0000 | [diff] [blame] | 6340 | return ReplaceInstUsesWith(I, Op0); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6341 | else // undef << X -> 0, undef >>u X -> 0 |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 6342 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 6343 | } |
| 6344 | if (isa<UndefValue>(Op1)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6345 | if (I.getOpcode() == Instruction::AShr) // X >>s undef -> X |
| 6346 | return ReplaceInstUsesWith(I, Op0); |
| 6347 | else // X << undef, X >>u undef -> 0 |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 6348 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 6349 | } |
| 6350 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 6351 | // Try to fold constant and into select arguments. |
| 6352 | if (isa<Constant>(Op0)) |
| 6353 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 6354 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 6355 | return R; |
| 6356 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6357 | if (ConstantInt *CUI = dyn_cast<ConstantInt>(Op1)) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6358 | if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I)) |
| 6359 | return Res; |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6360 | return 0; |
| 6361 | } |
| 6362 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6363 | Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6364 | BinaryOperator &I) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6365 | bool isLeftShift = I.getOpcode() == Instruction::Shl; |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6366 | |
Chris Lattner | 8d6bbdb | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 6367 | // See if we can simplify any instructions used by the instruction whose sole |
| 6368 | // purpose is to compute bits we don't care about. |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6369 | uint32_t TypeBits = Op0->getType()->getPrimitiveSizeInBits(); |
| 6370 | APInt KnownZero(TypeBits, 0), KnownOne(TypeBits, 0); |
| 6371 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(TypeBits), |
Chris Lattner | 8d6bbdb | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 6372 | KnownZero, KnownOne)) |
| 6373 | return &I; |
| 6374 | |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6375 | // shl uint X, 32 = 0 and shr ubyte Y, 9 = 0, ... just don't eliminate shr |
| 6376 | // of a signed value. |
| 6377 | // |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 6378 | if (Op1->uge(TypeBits)) { |
Chris Lattner | 0737c24 | 2007-02-02 05:29:55 +0000 | [diff] [blame] | 6379 | if (I.getOpcode() != Instruction::AShr) |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6380 | return ReplaceInstUsesWith(I, Constant::getNullValue(Op0->getType())); |
| 6381 | else { |
Chris Lattner | 0737c24 | 2007-02-02 05:29:55 +0000 | [diff] [blame] | 6382 | I.setOperand(1, ConstantInt::get(I.getType(), TypeBits-1)); |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6383 | return &I; |
Chris Lattner | 8adac75 | 2004-02-23 20:30:06 +0000 | [diff] [blame] | 6384 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6385 | } |
| 6386 | |
| 6387 | // ((X*C1) << C2) == (X * (C1 << C2)) |
| 6388 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0)) |
| 6389 | if (BO->getOpcode() == Instruction::Mul && isLeftShift) |
| 6390 | if (Constant *BOOp = dyn_cast<Constant>(BO->getOperand(1))) |
| 6391 | return BinaryOperator::createMul(BO->getOperand(0), |
| 6392 | ConstantExpr::getShl(BOOp, Op1)); |
| 6393 | |
| 6394 | // Try to fold constant and into select arguments. |
| 6395 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 6396 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 6397 | return R; |
| 6398 | if (isa<PHINode>(Op0)) |
| 6399 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 6400 | return NV; |
| 6401 | |
Chris Lattner | 8999dd3 | 2007-12-22 09:07:47 +0000 | [diff] [blame] | 6402 | // Fold shift2(trunc(shift1(x,c1)), c2) -> trunc(shift2(shift1(x,c1),c2)) |
| 6403 | if (TruncInst *TI = dyn_cast<TruncInst>(Op0)) { |
| 6404 | Instruction *TrOp = dyn_cast<Instruction>(TI->getOperand(0)); |
| 6405 | // If 'shift2' is an ashr, we would have to get the sign bit into a funny |
| 6406 | // place. Don't try to do this transformation in this case. Also, we |
| 6407 | // require that the input operand is a shift-by-constant so that we have |
| 6408 | // confidence that the shifts will get folded together. We could do this |
| 6409 | // xform in more cases, but it is unlikely to be profitable. |
| 6410 | if (TrOp && I.isLogicalShift() && TrOp->isShift() && |
| 6411 | isa<ConstantInt>(TrOp->getOperand(1))) { |
| 6412 | // Okay, we'll do this xform. Make the shift of shift. |
| 6413 | Constant *ShAmt = ConstantExpr::getZExt(Op1, TrOp->getType()); |
| 6414 | Instruction *NSh = BinaryOperator::create(I.getOpcode(), TrOp, ShAmt, |
| 6415 | I.getName()); |
| 6416 | InsertNewInstBefore(NSh, I); // (shift2 (shift1 & 0x00FF), c2) |
| 6417 | |
| 6418 | // For logical shifts, the truncation has the effect of making the high |
| 6419 | // part of the register be zeros. Emulate this by inserting an AND to |
| 6420 | // clear the top bits as needed. This 'and' will usually be zapped by |
| 6421 | // other xforms later if dead. |
| 6422 | unsigned SrcSize = TrOp->getType()->getPrimitiveSizeInBits(); |
| 6423 | unsigned DstSize = TI->getType()->getPrimitiveSizeInBits(); |
| 6424 | APInt MaskV(APInt::getLowBitsSet(SrcSize, DstSize)); |
| 6425 | |
| 6426 | // The mask we constructed says what the trunc would do if occurring |
| 6427 | // between the shifts. We want to know the effect *after* the second |
| 6428 | // shift. We know that it is a logical shift by a constant, so adjust the |
| 6429 | // mask as appropriate. |
| 6430 | if (I.getOpcode() == Instruction::Shl) |
| 6431 | MaskV <<= Op1->getZExtValue(); |
| 6432 | else { |
| 6433 | assert(I.getOpcode() == Instruction::LShr && "Unknown logical shift"); |
| 6434 | MaskV = MaskV.lshr(Op1->getZExtValue()); |
| 6435 | } |
| 6436 | |
| 6437 | Instruction *And = BinaryOperator::createAnd(NSh, ConstantInt::get(MaskV), |
| 6438 | TI->getName()); |
| 6439 | InsertNewInstBefore(And, I); // shift1 & 0x00FF |
| 6440 | |
| 6441 | // Return the value truncated to the interesting size. |
| 6442 | return new TruncInst(And, I.getType()); |
| 6443 | } |
| 6444 | } |
| 6445 | |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6446 | if (Op0->hasOneUse()) { |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6447 | if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0)) { |
| 6448 | // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C) |
| 6449 | Value *V1, *V2; |
| 6450 | ConstantInt *CC; |
| 6451 | switch (Op0BO->getOpcode()) { |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 6452 | default: break; |
| 6453 | case Instruction::Add: |
| 6454 | case Instruction::And: |
| 6455 | case Instruction::Or: |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6456 | case Instruction::Xor: { |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 6457 | // These operators commute. |
| 6458 | // Turn (Y + (X >> C)) << C -> (X + (Y << C)) & (~0 << C) |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6459 | if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() && |
| 6460 | match(Op0BO->getOperand(1), |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6461 | m_Shr(m_Value(V1), m_ConstantInt(CC))) && CC == Op1) { |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 6462 | Instruction *YS = BinaryOperator::createShl( |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6463 | Op0BO->getOperand(0), Op1, |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6464 | Op0BO->getName()); |
| 6465 | InsertNewInstBefore(YS, I); // (Y << C) |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 6466 | Instruction *X = |
| 6467 | BinaryOperator::create(Op0BO->getOpcode(), YS, V1, |
| 6468 | Op0BO->getOperand(1)->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6469 | InsertNewInstBefore(X, I); // (X + (Y << C)) |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 6470 | uint32_t Op1Val = Op1->getLimitedValue(TypeBits); |
Zhou Sheng | 90b9681 | 2007-03-30 05:45:18 +0000 | [diff] [blame] | 6471 | return BinaryOperator::createAnd(X, ConstantInt::get( |
| 6472 | APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val))); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6473 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6474 | |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6475 | // Turn (Y + ((X >> C) & CC)) << C -> ((X & (CC << C)) + (Y << C)) |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6476 | Value *Op0BOOp1 = Op0BO->getOperand(1); |
Chris Lattner | 3c69849 | 2007-03-05 00:11:19 +0000 | [diff] [blame] | 6477 | if (isLeftShift && Op0BOOp1->hasOneUse() && |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6478 | match(Op0BOOp1, |
| 6479 | 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] | 6480 | cast<BinaryOperator>(Op0BOOp1)->getOperand(0)->hasOneUse() && |
| 6481 | V2 == Op1) { |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 6482 | Instruction *YS = BinaryOperator::createShl( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6483 | Op0BO->getOperand(0), Op1, |
| 6484 | Op0BO->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6485 | InsertNewInstBefore(YS, I); // (Y << C) |
| 6486 | Instruction *XM = |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6487 | BinaryOperator::createAnd(V1, ConstantExpr::getShl(CC, Op1), |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6488 | V1->getName()+".mask"); |
| 6489 | InsertNewInstBefore(XM, I); // X & (CC << C) |
| 6490 | |
| 6491 | return BinaryOperator::create(Op0BO->getOpcode(), YS, XM); |
| 6492 | } |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6493 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6494 | |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6495 | // FALL THROUGH. |
| 6496 | case Instruction::Sub: { |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 6497 | // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C) |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6498 | if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() && |
| 6499 | match(Op0BO->getOperand(0), |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6500 | m_Shr(m_Value(V1), m_ConstantInt(CC))) && CC == Op1) { |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 6501 | Instruction *YS = BinaryOperator::createShl( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6502 | Op0BO->getOperand(1), Op1, |
| 6503 | Op0BO->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6504 | InsertNewInstBefore(YS, I); // (Y << C) |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 6505 | Instruction *X = |
Chris Lattner | 13d4ab4 | 2006-05-31 21:14:00 +0000 | [diff] [blame] | 6506 | BinaryOperator::create(Op0BO->getOpcode(), V1, YS, |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 6507 | Op0BO->getOperand(0)->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6508 | InsertNewInstBefore(X, I); // (X + (Y << C)) |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 6509 | uint32_t Op1Val = Op1->getLimitedValue(TypeBits); |
Zhou Sheng | 90b9681 | 2007-03-30 05:45:18 +0000 | [diff] [blame] | 6510 | return BinaryOperator::createAnd(X, ConstantInt::get( |
| 6511 | APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val))); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6512 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6513 | |
Chris Lattner | 13d4ab4 | 2006-05-31 21:14:00 +0000 | [diff] [blame] | 6514 | // Turn (((X >> C)&CC) + Y) << C -> (X + (Y << C)) & (CC << C) |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6515 | if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() && |
| 6516 | match(Op0BO->getOperand(0), |
| 6517 | m_And(m_Shr(m_Value(V1), m_Value(V2)), |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6518 | m_ConstantInt(CC))) && V2 == Op1 && |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 6519 | cast<BinaryOperator>(Op0BO->getOperand(0)) |
| 6520 | ->getOperand(0)->hasOneUse()) { |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 6521 | Instruction *YS = BinaryOperator::createShl( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6522 | Op0BO->getOperand(1), Op1, |
| 6523 | Op0BO->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6524 | InsertNewInstBefore(YS, I); // (Y << C) |
| 6525 | Instruction *XM = |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6526 | BinaryOperator::createAnd(V1, ConstantExpr::getShl(CC, Op1), |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6527 | V1->getName()+".mask"); |
| 6528 | InsertNewInstBefore(XM, I); // X & (CC << C) |
| 6529 | |
Chris Lattner | 13d4ab4 | 2006-05-31 21:14:00 +0000 | [diff] [blame] | 6530 | return BinaryOperator::create(Op0BO->getOpcode(), XM, YS); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6531 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6532 | |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 6533 | break; |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6534 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6535 | } |
| 6536 | |
| 6537 | |
| 6538 | // If the operand is an bitwise operator with a constant RHS, and the |
| 6539 | // shift is the only use, we can pull it out of the shift. |
| 6540 | if (ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand(1))) { |
| 6541 | bool isValid = true; // Valid only for And, Or, Xor |
| 6542 | bool highBitSet = false; // Transform if high bit of constant set? |
| 6543 | |
| 6544 | switch (Op0BO->getOpcode()) { |
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 6545 | default: isValid = false; break; // Do not perform transform! |
Chris Lattner | 1f7e160 | 2004-10-08 03:46:20 +0000 | [diff] [blame] | 6546 | case Instruction::Add: |
| 6547 | isValid = isLeftShift; |
| 6548 | break; |
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 6549 | case Instruction::Or: |
| 6550 | case Instruction::Xor: |
| 6551 | highBitSet = false; |
| 6552 | break; |
| 6553 | case Instruction::And: |
| 6554 | highBitSet = true; |
| 6555 | break; |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6556 | } |
| 6557 | |
| 6558 | // If this is a signed shift right, and the high bit is modified |
| 6559 | // by the logical operation, do not perform the transformation. |
| 6560 | // The highBitSet boolean indicates the value of the high bit of |
| 6561 | // the constant which would cause it to be modified for this |
| 6562 | // operation. |
| 6563 | // |
Chris Lattner | c95ba44 | 2007-12-06 06:25:04 +0000 | [diff] [blame] | 6564 | if (isValid && I.getOpcode() == Instruction::AShr) |
Zhou Sheng | e9e03f6 | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 6565 | isValid = Op0C->getValue()[TypeBits-1] == highBitSet; |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6566 | |
| 6567 | if (isValid) { |
| 6568 | Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, Op1); |
| 6569 | |
| 6570 | Instruction *NewShift = |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6571 | BinaryOperator::create(I.getOpcode(), Op0BO->getOperand(0), Op1); |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6572 | InsertNewInstBefore(NewShift, I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6573 | NewShift->takeName(Op0BO); |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6574 | |
| 6575 | return BinaryOperator::create(Op0BO->getOpcode(), NewShift, |
| 6576 | NewRHS); |
| 6577 | } |
| 6578 | } |
| 6579 | } |
| 6580 | } |
| 6581 | |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6582 | // 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] | 6583 | BinaryOperator *ShiftOp = dyn_cast<BinaryOperator>(Op0); |
| 6584 | if (ShiftOp && !ShiftOp->isShift()) |
| 6585 | ShiftOp = 0; |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6586 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6587 | if (ShiftOp && isa<ConstantInt>(ShiftOp->getOperand(1))) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6588 | ConstantInt *ShiftAmt1C = cast<ConstantInt>(ShiftOp->getOperand(1)); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 6589 | uint32_t ShiftAmt1 = ShiftAmt1C->getLimitedValue(TypeBits); |
| 6590 | uint32_t ShiftAmt2 = Op1->getLimitedValue(TypeBits); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6591 | assert(ShiftAmt2 != 0 && "Should have been simplified earlier"); |
| 6592 | if (ShiftAmt1 == 0) return 0; // Will be simplified in the future. |
| 6593 | Value *X = ShiftOp->getOperand(0); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6594 | |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 6595 | uint32_t AmtSum = ShiftAmt1+ShiftAmt2; // Fold into one big shift. |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6596 | if (AmtSum > TypeBits) |
| 6597 | AmtSum = TypeBits; |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6598 | |
| 6599 | const IntegerType *Ty = cast<IntegerType>(I.getType()); |
| 6600 | |
| 6601 | // Check for (X << c1) << c2 and (X >> c1) >> c2 |
Chris Lattner | 7f3da2d | 2007-02-03 23:28:07 +0000 | [diff] [blame] | 6602 | if (I.getOpcode() == ShiftOp->getOpcode()) { |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6603 | return BinaryOperator::create(I.getOpcode(), X, |
| 6604 | ConstantInt::get(Ty, AmtSum)); |
| 6605 | } else if (ShiftOp->getOpcode() == Instruction::LShr && |
| 6606 | I.getOpcode() == Instruction::AShr) { |
| 6607 | // ((X >>u C1) >>s C2) -> (X >>u (C1+C2)) since C1 != 0. |
| 6608 | return BinaryOperator::createLShr(X, ConstantInt::get(Ty, AmtSum)); |
| 6609 | } else if (ShiftOp->getOpcode() == Instruction::AShr && |
| 6610 | I.getOpcode() == Instruction::LShr) { |
| 6611 | // ((X >>s C1) >>u C2) -> ((X >>s (C1+C2)) & mask) since C1 != 0. |
| 6612 | Instruction *Shift = |
| 6613 | BinaryOperator::createAShr(X, ConstantInt::get(Ty, AmtSum)); |
| 6614 | InsertNewInstBefore(Shift, I); |
| 6615 | |
Zhou Sheng | e9e03f6 | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 6616 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6617 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6618 | } |
| 6619 | |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6620 | // Okay, if we get here, one shift must be left, and the other shift must be |
| 6621 | // right. See if the amounts are equal. |
| 6622 | if (ShiftAmt1 == ShiftAmt2) { |
| 6623 | // If we have ((X >>? C) << C), turn this into X & (-1 << C). |
| 6624 | if (I.getOpcode() == Instruction::Shl) { |
Reid Spencer | 55702aa | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 6625 | APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt1)); |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6626 | return BinaryOperator::createAnd(X, ConstantInt::get(Mask)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6627 | } |
| 6628 | // If we have ((X << C) >>u C), turn this into X & (-1 >>u C). |
| 6629 | if (I.getOpcode() == Instruction::LShr) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 6630 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt1)); |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6631 | return BinaryOperator::createAnd(X, ConstantInt::get(Mask)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6632 | } |
| 6633 | // We can simplify ((X << C) >>s C) into a trunc + sext. |
| 6634 | // NOTE: we could do this for any C, but that would make 'unusual' integer |
| 6635 | // types. For now, just stick to ones well-supported by the code |
| 6636 | // generators. |
| 6637 | const Type *SExtType = 0; |
| 6638 | switch (Ty->getBitWidth() - ShiftAmt1) { |
Zhou Sheng | e9e03f6 | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 6639 | case 1 : |
| 6640 | case 8 : |
| 6641 | case 16 : |
| 6642 | case 32 : |
| 6643 | case 64 : |
| 6644 | case 128: |
| 6645 | SExtType = IntegerType::get(Ty->getBitWidth() - ShiftAmt1); |
| 6646 | break; |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6647 | default: break; |
| 6648 | } |
| 6649 | if (SExtType) { |
| 6650 | Instruction *NewTrunc = new TruncInst(X, SExtType, "sext"); |
| 6651 | InsertNewInstBefore(NewTrunc, I); |
| 6652 | return new SExtInst(NewTrunc, Ty); |
| 6653 | } |
| 6654 | // Otherwise, we can't handle it yet. |
| 6655 | } else if (ShiftAmt1 < ShiftAmt2) { |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 6656 | uint32_t ShiftDiff = ShiftAmt2-ShiftAmt1; |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6657 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 6658 | // (X >>? C1) << C2 --> X << (C2-C1) & (-1 << C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6659 | if (I.getOpcode() == Instruction::Shl) { |
| 6660 | assert(ShiftOp->getOpcode() == Instruction::LShr || |
| 6661 | ShiftOp->getOpcode() == Instruction::AShr); |
Chris Lattner | e8d56c5 | 2006-01-07 01:32:28 +0000 | [diff] [blame] | 6662 | Instruction *Shift = |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6663 | BinaryOperator::createShl(X, ConstantInt::get(Ty, ShiftDiff)); |
Chris Lattner | e8d56c5 | 2006-01-07 01:32:28 +0000 | [diff] [blame] | 6664 | InsertNewInstBefore(Shift, I); |
| 6665 | |
Reid Spencer | 55702aa | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 6666 | APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
| 6667 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6668 | } |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6669 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 6670 | // (X << C1) >>u C2 --> X >>u (C2-C1) & (-1 >> C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6671 | if (I.getOpcode() == Instruction::LShr) { |
| 6672 | assert(ShiftOp->getOpcode() == Instruction::Shl); |
| 6673 | Instruction *Shift = |
| 6674 | BinaryOperator::createLShr(X, ConstantInt::get(Ty, ShiftDiff)); |
| 6675 | InsertNewInstBefore(Shift, I); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6676 | |
Reid Spencer | d5e30f0 | 2007-03-26 17:18:58 +0000 | [diff] [blame] | 6677 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6678 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 6679 | } |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6680 | |
| 6681 | // We can't handle (X << C1) >>s C2, it shifts arbitrary bits in. |
| 6682 | } else { |
| 6683 | assert(ShiftAmt2 < ShiftAmt1); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 6684 | uint32_t ShiftDiff = ShiftAmt1-ShiftAmt2; |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6685 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 6686 | // (X >>? C1) << C2 --> X >>? (C1-C2) & (-1 << C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6687 | if (I.getOpcode() == Instruction::Shl) { |
| 6688 | assert(ShiftOp->getOpcode() == Instruction::LShr || |
| 6689 | ShiftOp->getOpcode() == Instruction::AShr); |
| 6690 | Instruction *Shift = |
| 6691 | BinaryOperator::create(ShiftOp->getOpcode(), X, |
| 6692 | ConstantInt::get(Ty, ShiftDiff)); |
| 6693 | InsertNewInstBefore(Shift, I); |
| 6694 | |
Reid Spencer | 55702aa | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 6695 | APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6696 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6697 | } |
| 6698 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 6699 | // (X << C1) >>u C2 --> X << (C1-C2) & (-1 >> C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6700 | if (I.getOpcode() == Instruction::LShr) { |
| 6701 | assert(ShiftOp->getOpcode() == Instruction::Shl); |
| 6702 | Instruction *Shift = |
| 6703 | BinaryOperator::createShl(X, ConstantInt::get(Ty, ShiftDiff)); |
| 6704 | InsertNewInstBefore(Shift, I); |
| 6705 | |
Reid Spencer | 68d27cf | 2007-03-26 23:45:51 +0000 | [diff] [blame] | 6706 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6707 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6708 | } |
| 6709 | |
| 6710 | // 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] | 6711 | } |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6712 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 6713 | return 0; |
| 6714 | } |
| 6715 | |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 6716 | |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 6717 | /// DecomposeSimpleLinearExpr - Analyze 'Val', seeing if it is a simple linear |
| 6718 | /// expression. If so, decompose it, returning some value X, such that Val is |
| 6719 | /// X*Scale+Offset. |
| 6720 | /// |
| 6721 | static Value *DecomposeSimpleLinearExpr(Value *Val, unsigned &Scale, |
Jeff Cohen | 86796be | 2007-04-04 16:58:57 +0000 | [diff] [blame] | 6722 | int &Offset) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6723 | assert(Val->getType() == Type::Int32Ty && "Unexpected allocation size type!"); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6724 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6725 | Offset = CI->getZExtValue(); |
Chris Lattner | 6a94de2 | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 6726 | Scale = 0; |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6727 | return ConstantInt::get(Type::Int32Ty, 0); |
Chris Lattner | 6a94de2 | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 6728 | } else if (BinaryOperator *I = dyn_cast<BinaryOperator>(Val)) { |
| 6729 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 6730 | if (I->getOpcode() == Instruction::Shl) { |
| 6731 | // This is a value scaled by '1 << the shift amt'. |
| 6732 | Scale = 1U << RHS->getZExtValue(); |
| 6733 | Offset = 0; |
| 6734 | return I->getOperand(0); |
| 6735 | } else if (I->getOpcode() == Instruction::Mul) { |
| 6736 | // This value is scaled by 'RHS'. |
| 6737 | Scale = RHS->getZExtValue(); |
| 6738 | Offset = 0; |
| 6739 | return I->getOperand(0); |
| 6740 | } else if (I->getOpcode() == Instruction::Add) { |
| 6741 | // We have X+C. Check to see if we really have (X*C2)+C1, |
| 6742 | // where C1 is divisible by C2. |
| 6743 | unsigned SubScale; |
| 6744 | Value *SubVal = |
| 6745 | DecomposeSimpleLinearExpr(I->getOperand(0), SubScale, Offset); |
| 6746 | Offset += RHS->getZExtValue(); |
| 6747 | Scale = SubScale; |
| 6748 | return SubVal; |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 6749 | } |
| 6750 | } |
| 6751 | } |
| 6752 | |
| 6753 | // Otherwise, we can't look past this. |
| 6754 | Scale = 1; |
| 6755 | Offset = 0; |
| 6756 | return Val; |
| 6757 | } |
| 6758 | |
| 6759 | |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6760 | /// PromoteCastOfAllocation - If we find a cast of an allocation instruction, |
| 6761 | /// 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] | 6762 | Instruction *InstCombiner::PromoteCastOfAllocation(BitCastInst &CI, |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6763 | AllocationInst &AI) { |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 6764 | const PointerType *PTy = cast<PointerType>(CI.getType()); |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6765 | |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 6766 | // Remove any uses of AI that are dead. |
| 6767 | assert(!CI.use_empty() && "Dead instructions should be removed earlier!"); |
Chris Lattner | 535014f | 2007-02-15 22:52:10 +0000 | [diff] [blame] | 6768 | |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 6769 | for (Value::use_iterator UI = AI.use_begin(), E = AI.use_end(); UI != E; ) { |
| 6770 | Instruction *User = cast<Instruction>(*UI++); |
| 6771 | if (isInstructionTriviallyDead(User)) { |
| 6772 | while (UI != E && *UI == User) |
| 6773 | ++UI; // If this instruction uses AI more than once, don't break UI. |
| 6774 | |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 6775 | ++NumDeadInst; |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 6776 | DOUT << "IC: DCE: " << *User; |
Chris Lattner | f22a5c6 | 2007-03-02 19:59:19 +0000 | [diff] [blame] | 6777 | EraseInstFromFunction(*User); |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 6778 | } |
| 6779 | } |
| 6780 | |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6781 | // Get the type really allocated and the type casted to. |
| 6782 | const Type *AllocElTy = AI.getAllocatedType(); |
| 6783 | const Type *CastElTy = PTy->getElementType(); |
| 6784 | if (!AllocElTy->isSized() || !CastElTy->isSized()) return 0; |
Chris Lattner | 18e78bb | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 6785 | |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 6786 | unsigned AllocElTyAlign = TD->getABITypeAlignment(AllocElTy); |
| 6787 | unsigned CastElTyAlign = TD->getABITypeAlignment(CastElTy); |
Chris Lattner | 18e78bb | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 6788 | if (CastElTyAlign < AllocElTyAlign) return 0; |
| 6789 | |
Chris Lattner | 39387a5 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 6790 | // If the allocation has multiple uses, only promote it if we are strictly |
| 6791 | // increasing the alignment of the resultant allocation. If we keep it the |
| 6792 | // same, we open the door to infinite loops of various kinds. |
| 6793 | if (!AI.hasOneUse() && CastElTyAlign == AllocElTyAlign) return 0; |
| 6794 | |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 6795 | uint64_t AllocElTySize = TD->getABITypeSize(AllocElTy); |
| 6796 | uint64_t CastElTySize = TD->getABITypeSize(CastElTy); |
Chris Lattner | 0ddac2a | 2005-10-27 05:53:56 +0000 | [diff] [blame] | 6797 | if (CastElTySize == 0 || AllocElTySize == 0) return 0; |
Chris Lattner | 18e78bb | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 6798 | |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 6799 | // See if we can satisfy the modulus by pulling a scale out of the array |
| 6800 | // size argument. |
Jeff Cohen | 86796be | 2007-04-04 16:58:57 +0000 | [diff] [blame] | 6801 | unsigned ArraySizeScale; |
| 6802 | int ArrayOffset; |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 6803 | Value *NumElements = // See if the array size is a decomposable linear expr. |
| 6804 | DecomposeSimpleLinearExpr(AI.getOperand(0), ArraySizeScale, ArrayOffset); |
| 6805 | |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 6806 | // If we can now satisfy the modulus, by using a non-1 scale, we really can |
| 6807 | // do the xform. |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 6808 | if ((AllocElTySize*ArraySizeScale) % CastElTySize != 0 || |
| 6809 | (AllocElTySize*ArrayOffset ) % CastElTySize != 0) return 0; |
Chris Lattner | 8142b0a | 2005-10-27 06:12:00 +0000 | [diff] [blame] | 6810 | |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 6811 | unsigned Scale = (AllocElTySize*ArraySizeScale)/CastElTySize; |
| 6812 | Value *Amt = 0; |
| 6813 | if (Scale == 1) { |
| 6814 | Amt = NumElements; |
| 6815 | } else { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6816 | // If the allocation size is constant, form a constant mul expression |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6817 | Amt = ConstantInt::get(Type::Int32Ty, Scale); |
| 6818 | if (isa<ConstantInt>(NumElements)) |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 6819 | Amt = Multiply(cast<ConstantInt>(NumElements), cast<ConstantInt>(Amt)); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6820 | // otherwise multiply the amount and the number of elements |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 6821 | else if (Scale != 1) { |
| 6822 | Instruction *Tmp = BinaryOperator::createMul(Amt, NumElements, "tmp"); |
| 6823 | Amt = InsertNewInstBefore(Tmp, AI); |
Chris Lattner | 8142b0a | 2005-10-27 06:12:00 +0000 | [diff] [blame] | 6824 | } |
Chris Lattner | 0ddac2a | 2005-10-27 05:53:56 +0000 | [diff] [blame] | 6825 | } |
| 6826 | |
Jeff Cohen | 86796be | 2007-04-04 16:58:57 +0000 | [diff] [blame] | 6827 | if (int Offset = (AllocElTySize*ArrayOffset)/CastElTySize) { |
| 6828 | Value *Off = ConstantInt::get(Type::Int32Ty, Offset, true); |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 6829 | Instruction *Tmp = BinaryOperator::createAdd(Amt, Off, "tmp"); |
| 6830 | Amt = InsertNewInstBefore(Tmp, AI); |
| 6831 | } |
| 6832 | |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6833 | AllocationInst *New; |
| 6834 | if (isa<MallocInst>(AI)) |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6835 | New = new MallocInst(CastElTy, Amt, AI.getAlignment()); |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6836 | else |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6837 | New = new AllocaInst(CastElTy, Amt, AI.getAlignment()); |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6838 | InsertNewInstBefore(New, AI); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6839 | New->takeName(&AI); |
Chris Lattner | 39387a5 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 6840 | |
| 6841 | // If the allocation has multiple uses, insert a cast and change all things |
| 6842 | // that used it to use the new cast. This will also hack on CI, but it will |
| 6843 | // die soon. |
| 6844 | if (!AI.hasOneUse()) { |
| 6845 | AddUsesToWorkList(AI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6846 | // New is the allocation instruction, pointer typed. AI is the original |
| 6847 | // allocation instruction, also pointer typed. Thus, cast to use is BitCast. |
| 6848 | CastInst *NewCast = new BitCastInst(New, AI.getType(), "tmpcast"); |
Chris Lattner | 39387a5 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 6849 | InsertNewInstBefore(NewCast, AI); |
| 6850 | AI.replaceAllUsesWith(NewCast); |
| 6851 | } |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6852 | return ReplaceInstUsesWith(CI, New); |
| 6853 | } |
| 6854 | |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6855 | /// CanEvaluateInDifferentType - Return true if we can take the specified value |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6856 | /// and return it as type Ty without inserting any new casts and without |
| 6857 | /// changing the computed value. This is used by code that tries to decide |
| 6858 | /// whether promoting or shrinking integer operations to wider or smaller types |
| 6859 | /// will allow us to eliminate a truncate or extend. |
| 6860 | /// |
| 6861 | /// This is a truncation operation if Ty is smaller than V->getType(), or an |
| 6862 | /// extension operation if Ty is larger. |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 6863 | bool InstCombiner::CanEvaluateInDifferentType(Value *V, const IntegerType *Ty, |
| 6864 | unsigned CastOpc, |
| 6865 | int &NumCastsRemoved) { |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6866 | // We can always evaluate constants in another type. |
| 6867 | if (isa<ConstantInt>(V)) |
| 6868 | return true; |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6869 | |
| 6870 | Instruction *I = dyn_cast<Instruction>(V); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6871 | if (!I) return false; |
| 6872 | |
| 6873 | const IntegerType *OrigTy = cast<IntegerType>(V->getType()); |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6874 | |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 6875 | // If this is an extension or truncate, we can often eliminate it. |
| 6876 | if (isa<TruncInst>(I) || isa<ZExtInst>(I) || isa<SExtInst>(I)) { |
| 6877 | // If this is a cast from the destination type, we can trivially eliminate |
| 6878 | // it, and this will remove a cast overall. |
| 6879 | if (I->getOperand(0)->getType() == Ty) { |
| 6880 | // If the first operand is itself a cast, and is eliminable, do not count |
| 6881 | // this as an eliminable cast. We would prefer to eliminate those two |
| 6882 | // casts first. |
| 6883 | if (!isa<CastInst>(I->getOperand(0))) |
| 6884 | ++NumCastsRemoved; |
| 6885 | return true; |
| 6886 | } |
| 6887 | } |
| 6888 | |
| 6889 | // We can't extend or shrink something that has multiple uses: doing so would |
| 6890 | // require duplicating the instruction in general, which isn't profitable. |
| 6891 | if (!I->hasOneUse()) return false; |
| 6892 | |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6893 | switch (I->getOpcode()) { |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6894 | case Instruction::Add: |
| 6895 | case Instruction::Sub: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6896 | case Instruction::And: |
| 6897 | case Instruction::Or: |
| 6898 | case Instruction::Xor: |
| 6899 | // These operators can all arbitrarily be extended or truncated. |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 6900 | return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc, |
| 6901 | NumCastsRemoved) && |
| 6902 | CanEvaluateInDifferentType(I->getOperand(1), Ty, CastOpc, |
| 6903 | NumCastsRemoved); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6904 | |
Nick Lewycky | e6b0c00 | 2008-01-22 05:08:48 +0000 | [diff] [blame] | 6905 | case Instruction::Mul: |
Nick Lewycky | e6b0c00 | 2008-01-22 05:08:48 +0000 | [diff] [blame] | 6906 | // A multiply can be truncated by truncating its operands. |
| 6907 | return Ty->getBitWidth() < OrigTy->getBitWidth() && |
| 6908 | CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc, |
| 6909 | NumCastsRemoved) && |
| 6910 | CanEvaluateInDifferentType(I->getOperand(1), Ty, CastOpc, |
| 6911 | NumCastsRemoved); |
| 6912 | |
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 6913 | case Instruction::Shl: |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6914 | // If we are truncating the result of this SHL, and if it's a shift of a |
| 6915 | // constant amount, we can always perform a SHL in a smaller type. |
| 6916 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 6917 | uint32_t BitWidth = Ty->getBitWidth(); |
| 6918 | if (BitWidth < OrigTy->getBitWidth() && |
| 6919 | CI->getLimitedValue(BitWidth) < BitWidth) |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 6920 | return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc, |
| 6921 | NumCastsRemoved); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6922 | } |
| 6923 | break; |
| 6924 | case Instruction::LShr: |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6925 | // If this is a truncate of a logical shr, we can truncate it to a smaller |
| 6926 | // lshr iff we know that the bits we would otherwise be shifting in are |
| 6927 | // already zeros. |
| 6928 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 6929 | uint32_t OrigBitWidth = OrigTy->getBitWidth(); |
| 6930 | uint32_t BitWidth = Ty->getBitWidth(); |
| 6931 | if (BitWidth < OrigBitWidth && |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6932 | MaskedValueIsZero(I->getOperand(0), |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 6933 | APInt::getHighBitsSet(OrigBitWidth, OrigBitWidth-BitWidth)) && |
| 6934 | CI->getLimitedValue(BitWidth) < BitWidth) { |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 6935 | return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc, |
| 6936 | NumCastsRemoved); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6937 | } |
| 6938 | } |
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 6939 | break; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6940 | case Instruction::ZExt: |
| 6941 | case Instruction::SExt: |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 6942 | case Instruction::Trunc: |
| 6943 | // 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] | 6944 | // can safely replace it. Note that replacing it does not reduce the number |
| 6945 | // of casts in the input. |
| 6946 | if (I->getOpcode() == CastOpc) |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6947 | return true; |
Chris Lattner | 50d9d77 | 2007-09-10 23:46:29 +0000 | [diff] [blame] | 6948 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6949 | break; |
| 6950 | default: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6951 | // TODO: Can handle more cases here. |
| 6952 | break; |
| 6953 | } |
| 6954 | |
| 6955 | return false; |
| 6956 | } |
| 6957 | |
| 6958 | /// EvaluateInDifferentType - Given an expression that |
| 6959 | /// CanEvaluateInDifferentType returns true for, actually insert the code to |
| 6960 | /// evaluate the expression. |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 6961 | Value *InstCombiner::EvaluateInDifferentType(Value *V, const Type *Ty, |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6962 | bool isSigned) { |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6963 | if (Constant *C = dyn_cast<Constant>(V)) |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 6964 | return ConstantExpr::getIntegerCast(C, Ty, isSigned /*Sext or ZExt*/); |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6965 | |
| 6966 | // Otherwise, it must be an instruction. |
| 6967 | Instruction *I = cast<Instruction>(V); |
Chris Lattner | 01859e8 | 2006-05-20 23:14:03 +0000 | [diff] [blame] | 6968 | Instruction *Res = 0; |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6969 | switch (I->getOpcode()) { |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6970 | case Instruction::Add: |
| 6971 | case Instruction::Sub: |
Nick Lewycky | e6b0c00 | 2008-01-22 05:08:48 +0000 | [diff] [blame] | 6972 | case Instruction::Mul: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6973 | case Instruction::And: |
| 6974 | case Instruction::Or: |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6975 | case Instruction::Xor: |
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 6976 | case Instruction::AShr: |
| 6977 | case Instruction::LShr: |
| 6978 | case Instruction::Shl: { |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 6979 | Value *LHS = EvaluateInDifferentType(I->getOperand(0), Ty, isSigned); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6980 | Value *RHS = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned); |
| 6981 | Res = BinaryOperator::create((Instruction::BinaryOps)I->getOpcode(), |
| 6982 | LHS, RHS, I->getName()); |
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 6983 | break; |
| 6984 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6985 | case Instruction::Trunc: |
| 6986 | case Instruction::ZExt: |
| 6987 | case Instruction::SExt: |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6988 | // 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] | 6989 | // just return the source. There's no need to insert it because it is not |
| 6990 | // new. |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6991 | if (I->getOperand(0)->getType() == Ty) |
| 6992 | return I->getOperand(0); |
| 6993 | |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 6994 | // Otherwise, must be the same type of case, so just reinsert a new one. |
| 6995 | Res = CastInst::create(cast<CastInst>(I)->getOpcode(), I->getOperand(0), |
| 6996 | Ty, I->getName()); |
| 6997 | break; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6998 | default: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6999 | // TODO: Can handle more cases here. |
| 7000 | assert(0 && "Unreachable!"); |
| 7001 | break; |
| 7002 | } |
| 7003 | |
| 7004 | return InsertNewInstBefore(Res, *I); |
| 7005 | } |
| 7006 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7007 | /// @brief Implement the transforms common to all CastInst visitors. |
| 7008 | Instruction *InstCombiner::commonCastTransforms(CastInst &CI) { |
Chris Lattner | 79d35b3 | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 7009 | Value *Src = CI.getOperand(0); |
| 7010 | |
Dan Gohman | 23d9d27 | 2007-05-11 21:10:54 +0000 | [diff] [blame] | 7011 | // 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] | 7012 | // eliminate it now. |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 7013 | if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7014 | if (Instruction::CastOps opc = |
| 7015 | isEliminableCastPair(CSrc, CI.getOpcode(), CI.getType(), TD)) { |
| 7016 | // The first cast (CSrc) is eliminable so we need to fix up or replace |
| 7017 | // the second cast (CI). CSrc will then have a good chance of being dead. |
| 7018 | return CastInst::create(opc, CSrc->getOperand(0), CI.getType()); |
Chris Lattner | 8fd217c | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 7019 | } |
| 7020 | } |
Chris Lattner | a710ddc | 2004-05-25 04:29:21 +0000 | [diff] [blame] | 7021 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7022 | // 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] | 7023 | if (SelectInst *SI = dyn_cast<SelectInst>(Src)) |
| 7024 | if (Instruction *NV = FoldOpIntoSelect(CI, SI, this)) |
| 7025 | return NV; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7026 | |
| 7027 | // 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] | 7028 | if (isa<PHINode>(Src)) |
| 7029 | if (Instruction *NV = FoldOpIntoPhi(CI)) |
| 7030 | return NV; |
Chris Lattner | 9fb9213 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 7031 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7032 | return 0; |
| 7033 | } |
| 7034 | |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7035 | /// @brief Implement the transforms for cast of pointer (bitcast/ptrtoint) |
| 7036 | Instruction *InstCombiner::commonPointerCastTransforms(CastInst &CI) { |
| 7037 | Value *Src = CI.getOperand(0); |
| 7038 | |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7039 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Src)) { |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7040 | // If casting the result of a getelementptr instruction with no offset, turn |
| 7041 | // this into a cast of the original pointer! |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7042 | if (GEP->hasAllZeroIndices()) { |
| 7043 | // Changing the cast operand is usually not a good idea but it is safe |
| 7044 | // here because the pointer operand is being replaced with another |
| 7045 | // pointer operand so the opcode doesn't need to change. |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7046 | AddToWorkList(GEP); |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7047 | CI.setOperand(0, GEP->getOperand(0)); |
| 7048 | return &CI; |
| 7049 | } |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7050 | |
| 7051 | // If the GEP has a single use, and the base pointer is a bitcast, and the |
| 7052 | // GEP computes a constant offset, see if we can convert these three |
| 7053 | // instructions into fewer. This typically happens with unions and other |
| 7054 | // non-type-safe code. |
| 7055 | if (GEP->hasOneUse() && isa<BitCastInst>(GEP->getOperand(0))) { |
| 7056 | if (GEP->hasAllConstantIndices()) { |
| 7057 | // We are guaranteed to get a constant from EmitGEPOffset. |
| 7058 | ConstantInt *OffsetV = cast<ConstantInt>(EmitGEPOffset(GEP, CI, *this)); |
| 7059 | int64_t Offset = OffsetV->getSExtValue(); |
| 7060 | |
| 7061 | // Get the base pointer input of the bitcast, and the type it points to. |
| 7062 | Value *OrigBase = cast<BitCastInst>(GEP->getOperand(0))->getOperand(0); |
| 7063 | const Type *GEPIdxTy = |
| 7064 | cast<PointerType>(OrigBase->getType())->getElementType(); |
| 7065 | if (GEPIdxTy->isSized()) { |
| 7066 | SmallVector<Value*, 8> NewIndices; |
| 7067 | |
Chris Lattner | c42e226 | 2007-05-05 01:59:31 +0000 | [diff] [blame] | 7068 | // Start with the index over the outer type. Note that the type size |
| 7069 | // might be zero (even if the offset isn't zero) if the indexed type |
| 7070 | // is something like [0 x {int, int}] |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7071 | const Type *IntPtrTy = TD->getIntPtrType(); |
Chris Lattner | c42e226 | 2007-05-05 01:59:31 +0000 | [diff] [blame] | 7072 | int64_t FirstIdx = 0; |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 7073 | if (int64_t TySize = TD->getABITypeSize(GEPIdxTy)) { |
Chris Lattner | c42e226 | 2007-05-05 01:59:31 +0000 | [diff] [blame] | 7074 | FirstIdx = Offset/TySize; |
| 7075 | Offset %= TySize; |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7076 | |
Chris Lattner | c42e226 | 2007-05-05 01:59:31 +0000 | [diff] [blame] | 7077 | // Handle silly modulus not returning values values [0..TySize). |
| 7078 | if (Offset < 0) { |
| 7079 | --FirstIdx; |
| 7080 | Offset += TySize; |
| 7081 | assert(Offset >= 0); |
| 7082 | } |
Chris Lattner | d717c18 | 2007-05-05 22:32:24 +0000 | [diff] [blame] | 7083 | assert((uint64_t)Offset < (uint64_t)TySize &&"Out of range offset"); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7084 | } |
| 7085 | |
| 7086 | NewIndices.push_back(ConstantInt::get(IntPtrTy, FirstIdx)); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7087 | |
| 7088 | // Index into the types. If we fail, set OrigBase to null. |
| 7089 | while (Offset) { |
| 7090 | if (const StructType *STy = dyn_cast<StructType>(GEPIdxTy)) { |
| 7091 | const StructLayout *SL = TD->getStructLayout(STy); |
Chris Lattner | 6b6aef8 | 2007-05-15 00:16:00 +0000 | [diff] [blame] | 7092 | if (Offset < (int64_t)SL->getSizeInBytes()) { |
| 7093 | unsigned Elt = SL->getElementContainingOffset(Offset); |
| 7094 | NewIndices.push_back(ConstantInt::get(Type::Int32Ty, Elt)); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7095 | |
Chris Lattner | 6b6aef8 | 2007-05-15 00:16:00 +0000 | [diff] [blame] | 7096 | Offset -= SL->getElementOffset(Elt); |
| 7097 | GEPIdxTy = STy->getElementType(Elt); |
| 7098 | } else { |
| 7099 | // Otherwise, we can't index into this, bail out. |
| 7100 | Offset = 0; |
| 7101 | OrigBase = 0; |
| 7102 | } |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7103 | } else if (isa<ArrayType>(GEPIdxTy) || isa<VectorType>(GEPIdxTy)) { |
| 7104 | const SequentialType *STy = cast<SequentialType>(GEPIdxTy); |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 7105 | if (uint64_t EltSize = TD->getABITypeSize(STy->getElementType())){ |
Chris Lattner | 6b6aef8 | 2007-05-15 00:16:00 +0000 | [diff] [blame] | 7106 | NewIndices.push_back(ConstantInt::get(IntPtrTy,Offset/EltSize)); |
| 7107 | Offset %= EltSize; |
| 7108 | } else { |
| 7109 | NewIndices.push_back(ConstantInt::get(IntPtrTy, 0)); |
| 7110 | } |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7111 | GEPIdxTy = STy->getElementType(); |
| 7112 | } else { |
| 7113 | // Otherwise, we can't index into this, bail out. |
| 7114 | Offset = 0; |
| 7115 | OrigBase = 0; |
| 7116 | } |
| 7117 | } |
| 7118 | if (OrigBase) { |
| 7119 | // If we were able to index down into an element, create the GEP |
| 7120 | // and bitcast the result. This eliminates one bitcast, potentially |
| 7121 | // two. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 7122 | Instruction *NGEP = GetElementPtrInst::Create(OrigBase, |
| 7123 | NewIndices.begin(), |
| 7124 | NewIndices.end(), ""); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7125 | InsertNewInstBefore(NGEP, CI); |
| 7126 | NGEP->takeName(GEP); |
| 7127 | |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7128 | if (isa<BitCastInst>(CI)) |
| 7129 | return new BitCastInst(NGEP, CI.getType()); |
| 7130 | assert(isa<PtrToIntInst>(CI)); |
| 7131 | return new PtrToIntInst(NGEP, CI.getType()); |
| 7132 | } |
| 7133 | } |
| 7134 | } |
| 7135 | } |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7136 | } |
| 7137 | |
| 7138 | return commonCastTransforms(CI); |
| 7139 | } |
| 7140 | |
| 7141 | |
| 7142 | |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7143 | /// Only the TRUNC, ZEXT, SEXT, and BITCAST can both operand and result as |
| 7144 | /// integer types. This function implements the common transforms for all those |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7145 | /// cases. |
| 7146 | /// @brief Implement the transforms common to CastInst with integer operands |
| 7147 | Instruction *InstCombiner::commonIntCastTransforms(CastInst &CI) { |
| 7148 | if (Instruction *Result = commonCastTransforms(CI)) |
| 7149 | return Result; |
| 7150 | |
| 7151 | Value *Src = CI.getOperand(0); |
| 7152 | const Type *SrcTy = Src->getType(); |
| 7153 | const Type *DestTy = CI.getType(); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 7154 | uint32_t SrcBitSize = SrcTy->getPrimitiveSizeInBits(); |
| 7155 | uint32_t DestBitSize = DestTy->getPrimitiveSizeInBits(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7156 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7157 | // See if we can simplify any instructions used by the LHS whose sole |
| 7158 | // purpose is to compute bits we don't care about. |
Reid Spencer | ad6676e | 2007-03-22 20:56:53 +0000 | [diff] [blame] | 7159 | APInt KnownZero(DestBitSize, 0), KnownOne(DestBitSize, 0); |
| 7160 | if (SimplifyDemandedBits(&CI, APInt::getAllOnesValue(DestBitSize), |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7161 | KnownZero, KnownOne)) |
| 7162 | return &CI; |
| 7163 | |
| 7164 | // If the source isn't an instruction or has more than one use then we |
| 7165 | // can't do anything more. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7166 | Instruction *SrcI = dyn_cast<Instruction>(Src); |
| 7167 | if (!SrcI || !Src->hasOneUse()) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7168 | return 0; |
| 7169 | |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7170 | // Attempt to propagate the cast into the instruction for int->int casts. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7171 | int NumCastsRemoved = 0; |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7172 | if (!isa<BitCastInst>(CI) && |
| 7173 | CanEvaluateInDifferentType(SrcI, cast<IntegerType>(DestTy), |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7174 | CI.getOpcode(), NumCastsRemoved)) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7175 | // If this cast is a truncate, evaluting in a different type always |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7176 | // eliminates the cast, so it is always a win. If this is a zero-extension, |
| 7177 | // we need to do an AND to maintain the clear top-part of the computation, |
| 7178 | // so we require that the input have eliminated at least one cast. If this |
| 7179 | // 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] | 7180 | // require that two casts have been eliminated. |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7181 | bool DoXForm; |
| 7182 | switch (CI.getOpcode()) { |
| 7183 | default: |
| 7184 | // All the others use floating point so we shouldn't actually |
| 7185 | // get here because of the check above. |
| 7186 | assert(0 && "Unknown cast type"); |
| 7187 | case Instruction::Trunc: |
| 7188 | DoXForm = true; |
| 7189 | break; |
| 7190 | case Instruction::ZExt: |
| 7191 | DoXForm = NumCastsRemoved >= 1; |
| 7192 | break; |
| 7193 | case Instruction::SExt: |
| 7194 | DoXForm = NumCastsRemoved >= 2; |
| 7195 | break; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7196 | } |
| 7197 | |
| 7198 | if (DoXForm) { |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 7199 | Value *Res = EvaluateInDifferentType(SrcI, DestTy, |
| 7200 | CI.getOpcode() == Instruction::SExt); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7201 | assert(Res->getType() == DestTy); |
| 7202 | switch (CI.getOpcode()) { |
| 7203 | default: assert(0 && "Unknown cast type!"); |
| 7204 | case Instruction::Trunc: |
| 7205 | case Instruction::BitCast: |
| 7206 | // Just replace this cast with the result. |
| 7207 | return ReplaceInstUsesWith(CI, Res); |
| 7208 | case Instruction::ZExt: { |
| 7209 | // We need to emit an AND to clear the high bits. |
| 7210 | assert(SrcBitSize < DestBitSize && "Not a zext?"); |
Chris Lattner | cd1d6d5 | 2007-04-02 05:48:58 +0000 | [diff] [blame] | 7211 | Constant *C = ConstantInt::get(APInt::getLowBitsSet(DestBitSize, |
| 7212 | SrcBitSize)); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7213 | return BinaryOperator::createAnd(Res, C); |
| 7214 | } |
| 7215 | case Instruction::SExt: |
| 7216 | // We need to emit a cast to truncate, then a cast to sext. |
| 7217 | return CastInst::create(Instruction::SExt, |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7218 | InsertCastBefore(Instruction::Trunc, Res, Src->getType(), |
| 7219 | CI), DestTy); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7220 | } |
| 7221 | } |
| 7222 | } |
| 7223 | |
| 7224 | Value *Op0 = SrcI->getNumOperands() > 0 ? SrcI->getOperand(0) : 0; |
| 7225 | Value *Op1 = SrcI->getNumOperands() > 1 ? SrcI->getOperand(1) : 0; |
| 7226 | |
| 7227 | switch (SrcI->getOpcode()) { |
| 7228 | case Instruction::Add: |
| 7229 | case Instruction::Mul: |
| 7230 | case Instruction::And: |
| 7231 | case Instruction::Or: |
| 7232 | case Instruction::Xor: |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 7233 | // If we are discarding information, rewrite. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7234 | if (DestBitSize <= SrcBitSize && DestBitSize != 1) { |
| 7235 | // Don't insert two casts if they cannot be eliminated. We allow |
| 7236 | // two casts to be inserted if the sizes are the same. This could |
| 7237 | // only be converting signedness, which is a noop. |
| 7238 | if (DestBitSize == SrcBitSize || |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7239 | !ValueRequiresCast(CI.getOpcode(), Op1, DestTy,TD) || |
| 7240 | !ValueRequiresCast(CI.getOpcode(), Op0, DestTy, TD)) { |
Reid Spencer | 7eb7638 | 2006-12-13 17:19:09 +0000 | [diff] [blame] | 7241 | Instruction::CastOps opcode = CI.getOpcode(); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7242 | Value *Op0c = InsertOperandCastBefore(opcode, Op0, DestTy, SrcI); |
| 7243 | Value *Op1c = InsertOperandCastBefore(opcode, Op1, DestTy, SrcI); |
| 7244 | return BinaryOperator::create( |
| 7245 | cast<BinaryOperator>(SrcI)->getOpcode(), Op0c, Op1c); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7246 | } |
| 7247 | } |
| 7248 | |
| 7249 | // cast (xor bool X, true) to int --> xor (cast bool X to int), 1 |
| 7250 | if (isa<ZExtInst>(CI) && SrcBitSize == 1 && |
| 7251 | SrcI->getOpcode() == Instruction::Xor && |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 7252 | Op1 == ConstantInt::getTrue() && |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7253 | (!Op0->hasOneUse() || !isa<CmpInst>(Op0))) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7254 | Value *New = InsertOperandCastBefore(Instruction::ZExt, Op0, DestTy, &CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7255 | return BinaryOperator::createXor(New, ConstantInt::get(CI.getType(), 1)); |
| 7256 | } |
| 7257 | break; |
| 7258 | case Instruction::SDiv: |
| 7259 | case Instruction::UDiv: |
| 7260 | case Instruction::SRem: |
| 7261 | case Instruction::URem: |
| 7262 | // If we are just changing the sign, rewrite. |
| 7263 | if (DestBitSize == SrcBitSize) { |
| 7264 | // Don't insert two casts if they cannot be eliminated. We allow |
| 7265 | // two casts to be inserted if the sizes are the same. This could |
| 7266 | // only be converting signedness, which is a noop. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7267 | if (!ValueRequiresCast(CI.getOpcode(), Op1, DestTy, TD) || |
| 7268 | !ValueRequiresCast(CI.getOpcode(), Op0, DestTy, TD)) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7269 | Value *Op0c = InsertOperandCastBefore(Instruction::BitCast, |
| 7270 | Op0, DestTy, SrcI); |
| 7271 | Value *Op1c = InsertOperandCastBefore(Instruction::BitCast, |
| 7272 | Op1, DestTy, SrcI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7273 | return BinaryOperator::create( |
| 7274 | cast<BinaryOperator>(SrcI)->getOpcode(), Op0c, Op1c); |
| 7275 | } |
| 7276 | } |
| 7277 | break; |
| 7278 | |
| 7279 | case Instruction::Shl: |
| 7280 | // Allow changing the sign of the source operand. Do not allow |
| 7281 | // changing the size of the shift, UNLESS the shift amount is a |
| 7282 | // constant. We must not change variable sized shifts to a smaller |
| 7283 | // size, because it is undefined to shift more bits out than exist |
| 7284 | // in the value. |
| 7285 | if (DestBitSize == SrcBitSize || |
| 7286 | (DestBitSize < SrcBitSize && isa<Constant>(Op1))) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7287 | Instruction::CastOps opcode = (DestBitSize == SrcBitSize ? |
| 7288 | Instruction::BitCast : Instruction::Trunc); |
| 7289 | Value *Op0c = InsertOperandCastBefore(opcode, Op0, DestTy, SrcI); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7290 | Value *Op1c = InsertOperandCastBefore(opcode, Op1, DestTy, SrcI); |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 7291 | return BinaryOperator::createShl(Op0c, Op1c); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7292 | } |
| 7293 | break; |
| 7294 | case Instruction::AShr: |
| 7295 | // If this is a signed shr, and if all bits shifted in are about to be |
| 7296 | // truncated off, turn it into an unsigned shr to allow greater |
| 7297 | // simplifications. |
| 7298 | if (DestBitSize < SrcBitSize && |
| 7299 | isa<ConstantInt>(Op1)) { |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 7300 | uint32_t ShiftAmt = cast<ConstantInt>(Op1)->getLimitedValue(SrcBitSize); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7301 | if (SrcBitSize > ShiftAmt && SrcBitSize-ShiftAmt >= DestBitSize) { |
| 7302 | // Insert the new logical shift right. |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 7303 | return BinaryOperator::createLShr(Op0, Op1); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7304 | } |
| 7305 | } |
| 7306 | break; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7307 | } |
| 7308 | return 0; |
| 7309 | } |
| 7310 | |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 7311 | Instruction *InstCombiner::visitTrunc(TruncInst &CI) { |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 7312 | if (Instruction *Result = commonIntCastTransforms(CI)) |
| 7313 | return Result; |
| 7314 | |
| 7315 | Value *Src = CI.getOperand(0); |
| 7316 | const Type *Ty = CI.getType(); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 7317 | uint32_t DestBitWidth = Ty->getPrimitiveSizeInBits(); |
| 7318 | uint32_t SrcBitWidth = cast<IntegerType>(Src->getType())->getBitWidth(); |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 7319 | |
| 7320 | if (Instruction *SrcI = dyn_cast<Instruction>(Src)) { |
| 7321 | switch (SrcI->getOpcode()) { |
| 7322 | default: break; |
| 7323 | case Instruction::LShr: |
| 7324 | // We can shrink lshr to something smaller if we know the bits shifted in |
| 7325 | // are already zeros. |
| 7326 | if (ConstantInt *ShAmtV = dyn_cast<ConstantInt>(SrcI->getOperand(1))) { |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 7327 | uint32_t ShAmt = ShAmtV->getLimitedValue(SrcBitWidth); |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 7328 | |
| 7329 | // Get a mask for the bits shifting in. |
Zhou Sheng | e82fca0 | 2007-03-28 09:19:01 +0000 | [diff] [blame] | 7330 | APInt Mask(APInt::getLowBitsSet(SrcBitWidth, ShAmt).shl(DestBitWidth)); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7331 | Value* SrcIOp0 = SrcI->getOperand(0); |
| 7332 | if (SrcI->hasOneUse() && MaskedValueIsZero(SrcIOp0, Mask)) { |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 7333 | if (ShAmt >= DestBitWidth) // All zeros. |
| 7334 | return ReplaceInstUsesWith(CI, Constant::getNullValue(Ty)); |
| 7335 | |
| 7336 | // Okay, we can shrink this. Truncate the input, then return a new |
| 7337 | // shift. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7338 | Value *V1 = InsertCastBefore(Instruction::Trunc, SrcIOp0, Ty, CI); |
| 7339 | Value *V2 = InsertCastBefore(Instruction::Trunc, SrcI->getOperand(1), |
| 7340 | Ty, CI); |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 7341 | return BinaryOperator::createLShr(V1, V2); |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 7342 | } |
Chris Lattner | e13ab2a | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 7343 | } else { // This is a variable shr. |
| 7344 | |
| 7345 | // Turn 'trunc (lshr X, Y) to bool' into '(X & (1 << Y)) != 0'. This is |
| 7346 | // more LLVM instructions, but allows '1 << Y' to be hoisted if |
| 7347 | // loop-invariant and CSE'd. |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 7348 | if (CI.getType() == Type::Int1Ty && SrcI->hasOneUse()) { |
Chris Lattner | e13ab2a | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 7349 | Value *One = ConstantInt::get(SrcI->getType(), 1); |
| 7350 | |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7351 | Value *V = InsertNewInstBefore( |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 7352 | BinaryOperator::createShl(One, SrcI->getOperand(1), |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7353 | "tmp"), CI); |
Chris Lattner | e13ab2a | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 7354 | V = InsertNewInstBefore(BinaryOperator::createAnd(V, |
| 7355 | SrcI->getOperand(0), |
| 7356 | "tmp"), CI); |
| 7357 | Value *Zero = Constant::getNullValue(V->getType()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7358 | return new ICmpInst(ICmpInst::ICMP_NE, V, Zero); |
Chris Lattner | e13ab2a | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 7359 | } |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 7360 | } |
| 7361 | break; |
| 7362 | } |
| 7363 | } |
| 7364 | |
| 7365 | return 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7366 | } |
| 7367 | |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 7368 | /// transformZExtICmp - Transform (zext icmp) to bitwise / integer operations |
| 7369 | /// in order to eliminate the icmp. |
| 7370 | Instruction *InstCombiner::transformZExtICmp(ICmpInst *ICI, Instruction &CI, |
| 7371 | bool DoXform) { |
| 7372 | // If we are just checking for a icmp eq of a single bit and zext'ing it |
| 7373 | // to an integer, then shift the bit to the appropriate place and then |
| 7374 | // cast to integer to avoid the comparison. |
| 7375 | if (ConstantInt *Op1C = dyn_cast<ConstantInt>(ICI->getOperand(1))) { |
| 7376 | const APInt &Op1CV = Op1C->getValue(); |
| 7377 | |
| 7378 | // zext (x <s 0) to i32 --> x>>u31 true if signbit set. |
| 7379 | // zext (x >s -1) to i32 --> (x>>u31)^1 true if signbit clear. |
| 7380 | if ((ICI->getPredicate() == ICmpInst::ICMP_SLT && Op1CV == 0) || |
| 7381 | (ICI->getPredicate() == ICmpInst::ICMP_SGT &&Op1CV.isAllOnesValue())) { |
| 7382 | if (!DoXform) return ICI; |
| 7383 | |
| 7384 | Value *In = ICI->getOperand(0); |
| 7385 | Value *Sh = ConstantInt::get(In->getType(), |
| 7386 | In->getType()->getPrimitiveSizeInBits()-1); |
| 7387 | In = InsertNewInstBefore(BinaryOperator::createLShr(In, Sh, |
| 7388 | In->getName()+".lobit"), |
| 7389 | CI); |
| 7390 | if (In->getType() != CI.getType()) |
| 7391 | In = CastInst::createIntegerCast(In, CI.getType(), |
| 7392 | false/*ZExt*/, "tmp", &CI); |
| 7393 | |
| 7394 | if (ICI->getPredicate() == ICmpInst::ICMP_SGT) { |
| 7395 | Constant *One = ConstantInt::get(In->getType(), 1); |
| 7396 | In = InsertNewInstBefore(BinaryOperator::createXor(In, One, |
| 7397 | In->getName()+".not"), |
| 7398 | CI); |
| 7399 | } |
| 7400 | |
| 7401 | return ReplaceInstUsesWith(CI, In); |
| 7402 | } |
| 7403 | |
| 7404 | |
| 7405 | |
| 7406 | // zext (X == 0) to i32 --> X^1 iff X has only the low bit set. |
| 7407 | // zext (X == 0) to i32 --> (X>>1)^1 iff X has only the 2nd bit set. |
| 7408 | // zext (X == 1) to i32 --> X iff X has only the low bit set. |
| 7409 | // zext (X == 2) to i32 --> X>>1 iff X has only the 2nd bit set. |
| 7410 | // zext (X != 0) to i32 --> X iff X has only the low bit set. |
| 7411 | // zext (X != 0) to i32 --> X>>1 iff X has only the 2nd bit set. |
| 7412 | // zext (X != 1) to i32 --> X^1 iff X has only the low bit set. |
| 7413 | // zext (X != 2) to i32 --> (X>>1)^1 iff X has only the 2nd bit set. |
| 7414 | if ((Op1CV == 0 || Op1CV.isPowerOf2()) && |
| 7415 | // This only works for EQ and NE |
| 7416 | ICI->isEquality()) { |
| 7417 | // If Op1C some other power of two, convert: |
| 7418 | uint32_t BitWidth = Op1C->getType()->getBitWidth(); |
| 7419 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 7420 | APInt TypeMask(APInt::getAllOnesValue(BitWidth)); |
| 7421 | ComputeMaskedBits(ICI->getOperand(0), TypeMask, KnownZero, KnownOne); |
| 7422 | |
| 7423 | APInt KnownZeroMask(~KnownZero); |
| 7424 | if (KnownZeroMask.isPowerOf2()) { // Exactly 1 possible 1? |
| 7425 | if (!DoXform) return ICI; |
| 7426 | |
| 7427 | bool isNE = ICI->getPredicate() == ICmpInst::ICMP_NE; |
| 7428 | if (Op1CV != 0 && (Op1CV != KnownZeroMask)) { |
| 7429 | // (X&4) == 2 --> false |
| 7430 | // (X&4) != 2 --> true |
| 7431 | Constant *Res = ConstantInt::get(Type::Int1Ty, isNE); |
| 7432 | Res = ConstantExpr::getZExt(Res, CI.getType()); |
| 7433 | return ReplaceInstUsesWith(CI, Res); |
| 7434 | } |
| 7435 | |
| 7436 | uint32_t ShiftAmt = KnownZeroMask.logBase2(); |
| 7437 | Value *In = ICI->getOperand(0); |
| 7438 | if (ShiftAmt) { |
| 7439 | // Perform a logical shr by shiftamt. |
| 7440 | // Insert the shift to put the result in the low bit. |
| 7441 | In = InsertNewInstBefore(BinaryOperator::createLShr(In, |
| 7442 | ConstantInt::get(In->getType(), ShiftAmt), |
| 7443 | In->getName()+".lobit"), CI); |
| 7444 | } |
| 7445 | |
| 7446 | if ((Op1CV != 0) == isNE) { // Toggle the low bit. |
| 7447 | Constant *One = ConstantInt::get(In->getType(), 1); |
| 7448 | In = BinaryOperator::createXor(In, One, "tmp"); |
| 7449 | InsertNewInstBefore(cast<Instruction>(In), CI); |
| 7450 | } |
| 7451 | |
| 7452 | if (CI.getType() == In->getType()) |
| 7453 | return ReplaceInstUsesWith(CI, In); |
| 7454 | else |
| 7455 | return CastInst::createIntegerCast(In, CI.getType(), false/*ZExt*/); |
| 7456 | } |
| 7457 | } |
| 7458 | } |
| 7459 | |
| 7460 | return 0; |
| 7461 | } |
| 7462 | |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 7463 | Instruction *InstCombiner::visitZExt(ZExtInst &CI) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7464 | // If one of the common conversion will work .. |
| 7465 | if (Instruction *Result = commonIntCastTransforms(CI)) |
| 7466 | return Result; |
| 7467 | |
| 7468 | Value *Src = CI.getOperand(0); |
| 7469 | |
| 7470 | // If this is a cast of a cast |
| 7471 | if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7472 | // If this is a TRUNC followed by a ZEXT then we are dealing with integral |
| 7473 | // types and if the sizes are just right we can convert this into a logical |
| 7474 | // 'and' which will be much cheaper than the pair of casts. |
| 7475 | if (isa<TruncInst>(CSrc)) { |
| 7476 | // Get the sizes of the types involved |
| 7477 | Value *A = CSrc->getOperand(0); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 7478 | uint32_t SrcSize = A->getType()->getPrimitiveSizeInBits(); |
| 7479 | uint32_t MidSize = CSrc->getType()->getPrimitiveSizeInBits(); |
| 7480 | uint32_t DstSize = CI.getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7481 | // If we're actually extending zero bits and the trunc is a no-op |
| 7482 | if (MidSize < DstSize && SrcSize == DstSize) { |
| 7483 | // Replace both of the casts with an And of the type mask. |
Zhou Sheng | e82fca0 | 2007-03-28 09:19:01 +0000 | [diff] [blame] | 7484 | APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize)); |
Reid Spencer | ad6676e | 2007-03-22 20:56:53 +0000 | [diff] [blame] | 7485 | Constant *AndConst = ConstantInt::get(AndValue); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7486 | Instruction *And = |
| 7487 | BinaryOperator::createAnd(CSrc->getOperand(0), AndConst); |
| 7488 | // Unfortunately, if the type changed, we need to cast it back. |
| 7489 | if (And->getType() != CI.getType()) { |
| 7490 | And->setName(CSrc->getName()+".mask"); |
| 7491 | InsertNewInstBefore(And, CI); |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 7492 | And = CastInst::createIntegerCast(And, CI.getType(), false/*ZExt*/); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7493 | } |
| 7494 | return And; |
| 7495 | } |
| 7496 | } |
| 7497 | } |
| 7498 | |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 7499 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(Src)) |
| 7500 | return transformZExtICmp(ICI, CI); |
Chris Lattner | a2e2c9b | 2007-04-11 06:53:04 +0000 | [diff] [blame] | 7501 | |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 7502 | BinaryOperator *SrcI = dyn_cast<BinaryOperator>(Src); |
| 7503 | if (SrcI && SrcI->getOpcode() == Instruction::Or) { |
| 7504 | // zext (or icmp, icmp) --> or (zext icmp), (zext icmp) if at least one |
| 7505 | // of the (zext icmp) will be transformed. |
| 7506 | ICmpInst *LHS = dyn_cast<ICmpInst>(SrcI->getOperand(0)); |
| 7507 | ICmpInst *RHS = dyn_cast<ICmpInst>(SrcI->getOperand(1)); |
| 7508 | if (LHS && RHS && LHS->hasOneUse() && RHS->hasOneUse() && |
| 7509 | (transformZExtICmp(LHS, CI, false) || |
| 7510 | transformZExtICmp(RHS, CI, false))) { |
| 7511 | Value *LCast = InsertCastBefore(Instruction::ZExt, LHS, CI.getType(), CI); |
| 7512 | Value *RCast = InsertCastBefore(Instruction::ZExt, RHS, CI.getType(), CI); |
| 7513 | return BinaryOperator::create(Instruction::Or, LCast, RCast); |
Chris Lattner | 66bc325 | 2007-04-11 05:45:39 +0000 | [diff] [blame] | 7514 | } |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 7515 | } |
| 7516 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7517 | return 0; |
| 7518 | } |
| 7519 | |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 7520 | Instruction *InstCombiner::visitSExt(SExtInst &CI) { |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 7521 | if (Instruction *I = commonIntCastTransforms(CI)) |
| 7522 | return I; |
| 7523 | |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 7524 | Value *Src = CI.getOperand(0); |
| 7525 | |
| 7526 | // sext (x <s 0) -> ashr x, 31 -> all ones if signed |
| 7527 | // sext (x >s -1) -> ashr x, 31 -> all ones if not signed |
| 7528 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(Src)) { |
| 7529 | // If we are just checking for a icmp eq of a single bit and zext'ing it |
| 7530 | // to an integer, then shift the bit to the appropriate place and then |
| 7531 | // cast to integer to avoid the comparison. |
| 7532 | if (ConstantInt *Op1C = dyn_cast<ConstantInt>(ICI->getOperand(1))) { |
| 7533 | const APInt &Op1CV = Op1C->getValue(); |
| 7534 | |
| 7535 | // sext (x <s 0) to i32 --> x>>s31 true if signbit set. |
| 7536 | // sext (x >s -1) to i32 --> (x>>s31)^-1 true if signbit clear. |
| 7537 | if ((ICI->getPredicate() == ICmpInst::ICMP_SLT && Op1CV == 0) || |
| 7538 | (ICI->getPredicate() == ICmpInst::ICMP_SGT &&Op1CV.isAllOnesValue())){ |
| 7539 | Value *In = ICI->getOperand(0); |
| 7540 | Value *Sh = ConstantInt::get(In->getType(), |
| 7541 | In->getType()->getPrimitiveSizeInBits()-1); |
| 7542 | In = InsertNewInstBefore(BinaryOperator::createAShr(In, Sh, |
Chris Lattner | e34e9a2 | 2007-04-14 23:32:02 +0000 | [diff] [blame] | 7543 | In->getName()+".lobit"), |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 7544 | CI); |
| 7545 | if (In->getType() != CI.getType()) |
| 7546 | In = CastInst::createIntegerCast(In, CI.getType(), |
| 7547 | true/*SExt*/, "tmp", &CI); |
| 7548 | |
| 7549 | if (ICI->getPredicate() == ICmpInst::ICMP_SGT) |
| 7550 | In = InsertNewInstBefore(BinaryOperator::createNot(In, |
| 7551 | In->getName()+".not"), CI); |
| 7552 | |
| 7553 | return ReplaceInstUsesWith(CI, In); |
| 7554 | } |
| 7555 | } |
| 7556 | } |
| 7557 | |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 7558 | return 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7559 | } |
| 7560 | |
Chris Lattner | b753065 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 7561 | /// FitsInFPType - Return a Constant* for the specified FP constant if it fits |
| 7562 | /// in the specified FP type without changing its value. |
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame^] | 7563 | static Constant *FitsInFPType(ConstantFP *CFP, const fltSemantics &Sem) { |
Chris Lattner | b753065 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 7564 | APFloat F = CFP->getValueAPF(); |
| 7565 | if (F.convert(Sem, APFloat::rmNearestTiesToEven) == APFloat::opOK) |
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame^] | 7566 | return ConstantFP::get(F); |
Chris Lattner | b753065 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 7567 | return 0; |
| 7568 | } |
| 7569 | |
| 7570 | /// LookThroughFPExtensions - If this is an fp extension instruction, look |
| 7571 | /// through it until we get the source value. |
| 7572 | static Value *LookThroughFPExtensions(Value *V) { |
| 7573 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 7574 | if (I->getOpcode() == Instruction::FPExt) |
| 7575 | return LookThroughFPExtensions(I->getOperand(0)); |
| 7576 | |
| 7577 | // If this value is a constant, return the constant in the smallest FP type |
| 7578 | // that can accurately represent it. This allows us to turn |
| 7579 | // (float)((double)X+2.0) into x+2.0f. |
| 7580 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) { |
| 7581 | if (CFP->getType() == Type::PPC_FP128Ty) |
| 7582 | return V; // No constant folding of this. |
| 7583 | // See if the value can be truncated to float and then reextended. |
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame^] | 7584 | if (Value *V = FitsInFPType(CFP, APFloat::IEEEsingle)) |
Chris Lattner | b753065 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 7585 | return V; |
| 7586 | if (CFP->getType() == Type::DoubleTy) |
| 7587 | return V; // Won't shrink. |
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame^] | 7588 | if (Value *V = FitsInFPType(CFP, APFloat::IEEEdouble)) |
Chris Lattner | b753065 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 7589 | return V; |
| 7590 | // Don't try to shrink to various long double types. |
| 7591 | } |
| 7592 | |
| 7593 | return V; |
| 7594 | } |
| 7595 | |
| 7596 | Instruction *InstCombiner::visitFPTrunc(FPTruncInst &CI) { |
| 7597 | if (Instruction *I = commonCastTransforms(CI)) |
| 7598 | return I; |
| 7599 | |
| 7600 | // If we have fptrunc(add (fpextend x), (fpextend y)), where x and y are |
| 7601 | // smaller than the destination type, we can eliminate the truncate by doing |
| 7602 | // the add as the smaller type. This applies to add/sub/mul/div as well as |
| 7603 | // many builtins (sqrt, etc). |
| 7604 | BinaryOperator *OpI = dyn_cast<BinaryOperator>(CI.getOperand(0)); |
| 7605 | if (OpI && OpI->hasOneUse()) { |
| 7606 | switch (OpI->getOpcode()) { |
| 7607 | default: break; |
| 7608 | case Instruction::Add: |
| 7609 | case Instruction::Sub: |
| 7610 | case Instruction::Mul: |
| 7611 | case Instruction::FDiv: |
| 7612 | case Instruction::FRem: |
| 7613 | const Type *SrcTy = OpI->getType(); |
| 7614 | Value *LHSTrunc = LookThroughFPExtensions(OpI->getOperand(0)); |
| 7615 | Value *RHSTrunc = LookThroughFPExtensions(OpI->getOperand(1)); |
| 7616 | if (LHSTrunc->getType() != SrcTy && |
| 7617 | RHSTrunc->getType() != SrcTy) { |
| 7618 | unsigned DstSize = CI.getType()->getPrimitiveSizeInBits(); |
| 7619 | // If the source types were both smaller than the destination type of |
| 7620 | // the cast, do this xform. |
| 7621 | if (LHSTrunc->getType()->getPrimitiveSizeInBits() <= DstSize && |
| 7622 | RHSTrunc->getType()->getPrimitiveSizeInBits() <= DstSize) { |
| 7623 | LHSTrunc = InsertCastBefore(Instruction::FPExt, LHSTrunc, |
| 7624 | CI.getType(), CI); |
| 7625 | RHSTrunc = InsertCastBefore(Instruction::FPExt, RHSTrunc, |
| 7626 | CI.getType(), CI); |
| 7627 | return BinaryOperator::create(OpI->getOpcode(), LHSTrunc, RHSTrunc); |
| 7628 | } |
| 7629 | } |
| 7630 | break; |
| 7631 | } |
| 7632 | } |
| 7633 | return 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7634 | } |
| 7635 | |
| 7636 | Instruction *InstCombiner::visitFPExt(CastInst &CI) { |
| 7637 | return commonCastTransforms(CI); |
| 7638 | } |
| 7639 | |
| 7640 | Instruction *InstCombiner::visitFPToUI(CastInst &CI) { |
Reid Spencer | 44c030a | 2006-11-30 23:13:36 +0000 | [diff] [blame] | 7641 | return commonCastTransforms(CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7642 | } |
| 7643 | |
| 7644 | Instruction *InstCombiner::visitFPToSI(CastInst &CI) { |
Reid Spencer | 44c030a | 2006-11-30 23:13:36 +0000 | [diff] [blame] | 7645 | return commonCastTransforms(CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7646 | } |
| 7647 | |
| 7648 | Instruction *InstCombiner::visitUIToFP(CastInst &CI) { |
| 7649 | return commonCastTransforms(CI); |
| 7650 | } |
| 7651 | |
| 7652 | Instruction *InstCombiner::visitSIToFP(CastInst &CI) { |
| 7653 | return commonCastTransforms(CI); |
| 7654 | } |
| 7655 | |
| 7656 | Instruction *InstCombiner::visitPtrToInt(CastInst &CI) { |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7657 | return commonPointerCastTransforms(CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7658 | } |
| 7659 | |
Chris Lattner | f9d9e45 | 2008-01-08 07:23:51 +0000 | [diff] [blame] | 7660 | Instruction *InstCombiner::visitIntToPtr(IntToPtrInst &CI) { |
| 7661 | if (Instruction *I = commonCastTransforms(CI)) |
| 7662 | return I; |
| 7663 | |
| 7664 | const Type *DestPointee = cast<PointerType>(CI.getType())->getElementType(); |
| 7665 | if (!DestPointee->isSized()) return 0; |
| 7666 | |
| 7667 | // If this is inttoptr(add (ptrtoint x), cst), try to turn this into a GEP. |
| 7668 | ConstantInt *Cst; |
| 7669 | Value *X; |
| 7670 | if (match(CI.getOperand(0), m_Add(m_Cast<PtrToIntInst>(m_Value(X)), |
| 7671 | m_ConstantInt(Cst)))) { |
| 7672 | // If the source and destination operands have the same type, see if this |
| 7673 | // is a single-index GEP. |
| 7674 | if (X->getType() == CI.getType()) { |
| 7675 | // Get the size of the pointee type. |
Bill Wendling | b9d4f8d | 2008-03-14 05:12:19 +0000 | [diff] [blame] | 7676 | uint64_t Size = TD->getABITypeSize(DestPointee); |
Chris Lattner | f9d9e45 | 2008-01-08 07:23:51 +0000 | [diff] [blame] | 7677 | |
| 7678 | // Convert the constant to intptr type. |
| 7679 | APInt Offset = Cst->getValue(); |
| 7680 | Offset.sextOrTrunc(TD->getPointerSizeInBits()); |
| 7681 | |
| 7682 | // If Offset is evenly divisible by Size, we can do this xform. |
| 7683 | if (Size && !APIntOps::srem(Offset, APInt(Offset.getBitWidth(), Size))){ |
| 7684 | Offset = APIntOps::sdiv(Offset, APInt(Offset.getBitWidth(), Size)); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 7685 | return GetElementPtrInst::Create(X, ConstantInt::get(Offset)); |
Chris Lattner | f9d9e45 | 2008-01-08 07:23:51 +0000 | [diff] [blame] | 7686 | } |
| 7687 | } |
| 7688 | // TODO: Could handle other cases, e.g. where add is indexing into field of |
| 7689 | // struct etc. |
| 7690 | } else if (CI.getOperand(0)->hasOneUse() && |
| 7691 | match(CI.getOperand(0), m_Add(m_Value(X), m_ConstantInt(Cst)))) { |
| 7692 | // Otherwise, if this is inttoptr(add x, cst), try to turn this into an |
| 7693 | // "inttoptr+GEP" instead of "add+intptr". |
| 7694 | |
| 7695 | // Get the size of the pointee type. |
| 7696 | uint64_t Size = TD->getABITypeSize(DestPointee); |
| 7697 | |
| 7698 | // Convert the constant to intptr type. |
| 7699 | APInt Offset = Cst->getValue(); |
| 7700 | Offset.sextOrTrunc(TD->getPointerSizeInBits()); |
| 7701 | |
| 7702 | // If Offset is evenly divisible by Size, we can do this xform. |
| 7703 | if (Size && !APIntOps::srem(Offset, APInt(Offset.getBitWidth(), Size))){ |
| 7704 | Offset = APIntOps::sdiv(Offset, APInt(Offset.getBitWidth(), Size)); |
| 7705 | |
| 7706 | Instruction *P = InsertNewInstBefore(new IntToPtrInst(X, CI.getType(), |
| 7707 | "tmp"), CI); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 7708 | return GetElementPtrInst::Create(P, ConstantInt::get(Offset), "tmp"); |
Chris Lattner | f9d9e45 | 2008-01-08 07:23:51 +0000 | [diff] [blame] | 7709 | } |
| 7710 | } |
| 7711 | return 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7712 | } |
| 7713 | |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7714 | Instruction *InstCombiner::visitBitCast(BitCastInst &CI) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7715 | // If the operands are integer typed then apply the integer transforms, |
| 7716 | // otherwise just apply the common ones. |
| 7717 | Value *Src = CI.getOperand(0); |
| 7718 | const Type *SrcTy = Src->getType(); |
| 7719 | const Type *DestTy = CI.getType(); |
| 7720 | |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 7721 | if (SrcTy->isInteger() && DestTy->isInteger()) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7722 | if (Instruction *Result = commonIntCastTransforms(CI)) |
| 7723 | return Result; |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7724 | } else if (isa<PointerType>(SrcTy)) { |
| 7725 | if (Instruction *I = commonPointerCastTransforms(CI)) |
| 7726 | return I; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7727 | } else { |
| 7728 | if (Instruction *Result = commonCastTransforms(CI)) |
| 7729 | return Result; |
| 7730 | } |
| 7731 | |
| 7732 | |
| 7733 | // Get rid of casts from one type to the same type. These are useless and can |
| 7734 | // be replaced by the operand. |
| 7735 | if (DestTy == Src->getType()) |
| 7736 | return ReplaceInstUsesWith(CI, Src); |
| 7737 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7738 | if (const PointerType *DstPTy = dyn_cast<PointerType>(DestTy)) { |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7739 | const PointerType *SrcPTy = cast<PointerType>(SrcTy); |
| 7740 | const Type *DstElTy = DstPTy->getElementType(); |
| 7741 | const Type *SrcElTy = SrcPTy->getElementType(); |
| 7742 | |
Nate Begeman | 83ad90a | 2008-03-31 00:22:16 +0000 | [diff] [blame] | 7743 | // If the address spaces don't match, don't eliminate the bitcast, which is |
| 7744 | // required for changing types. |
| 7745 | if (SrcPTy->getAddressSpace() != DstPTy->getAddressSpace()) |
| 7746 | return 0; |
| 7747 | |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7748 | // If we are casting a malloc or alloca to a pointer to a type of the same |
| 7749 | // size, rewrite the allocation instruction to allocate the "right" type. |
| 7750 | if (AllocationInst *AI = dyn_cast<AllocationInst>(Src)) |
| 7751 | if (Instruction *V = PromoteCastOfAllocation(CI, *AI)) |
| 7752 | return V; |
| 7753 | |
Chris Lattner | d717c18 | 2007-05-05 22:32:24 +0000 | [diff] [blame] | 7754 | // If the source and destination are pointers, and this cast is equivalent |
| 7755 | // 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] | 7756 | // This can enhance SROA and other transforms that want type-safe pointers. |
| 7757 | Constant *ZeroUInt = Constant::getNullValue(Type::Int32Ty); |
| 7758 | unsigned NumZeros = 0; |
| 7759 | while (SrcElTy != DstElTy && |
| 7760 | isa<CompositeType>(SrcElTy) && !isa<PointerType>(SrcElTy) && |
| 7761 | SrcElTy->getNumContainedTypes() /* not "{}" */) { |
| 7762 | SrcElTy = cast<CompositeType>(SrcElTy)->getTypeAtIndex(ZeroUInt); |
| 7763 | ++NumZeros; |
| 7764 | } |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 7765 | |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7766 | // If we found a path from the src to dest, create the getelementptr now. |
| 7767 | if (SrcElTy == DstElTy) { |
| 7768 | SmallVector<Value*, 8> Idxs(NumZeros+1, ZeroUInt); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 7769 | return GetElementPtrInst::Create(Src, Idxs.begin(), Idxs.end(), "", |
| 7770 | ((Instruction*) NULL)); |
Chris Lattner | 9fb9213 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 7771 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7772 | } |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 7773 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7774 | if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(Src)) { |
| 7775 | if (SVI->hasOneUse()) { |
| 7776 | // Okay, we have (bitconvert (shuffle ..)). Check to see if this is |
| 7777 | // a bitconvert to a vector with the same # elts. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 7778 | if (isa<VectorType>(DestTy) && |
| 7779 | cast<VectorType>(DestTy)->getNumElements() == |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7780 | SVI->getType()->getNumElements()) { |
| 7781 | CastInst *Tmp; |
| 7782 | // If either of the operands is a cast from CI.getType(), then |
| 7783 | // evaluating the shuffle in the casted destination's type will allow |
| 7784 | // us to eliminate at least one cast. |
| 7785 | if (((Tmp = dyn_cast<CastInst>(SVI->getOperand(0))) && |
| 7786 | Tmp->getOperand(0)->getType() == DestTy) || |
| 7787 | ((Tmp = dyn_cast<CastInst>(SVI->getOperand(1))) && |
| 7788 | Tmp->getOperand(0)->getType() == DestTy)) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7789 | Value *LHS = InsertOperandCastBefore(Instruction::BitCast, |
| 7790 | SVI->getOperand(0), DestTy, &CI); |
| 7791 | Value *RHS = InsertOperandCastBefore(Instruction::BitCast, |
| 7792 | SVI->getOperand(1), DestTy, &CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7793 | // Return a new shuffle vector. Use the same element ID's, as we |
| 7794 | // know the vector types match #elts. |
| 7795 | return new ShuffleVectorInst(LHS, RHS, SVI->getOperand(2)); |
Chris Lattner | 01575b7 | 2006-05-25 23:24:33 +0000 | [diff] [blame] | 7796 | } |
| 7797 | } |
| 7798 | } |
| 7799 | } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 7800 | return 0; |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 7801 | } |
| 7802 | |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7803 | /// GetSelectFoldableOperands - We want to turn code that looks like this: |
| 7804 | /// %C = or %A, %B |
| 7805 | /// %D = select %cond, %C, %A |
| 7806 | /// into: |
| 7807 | /// %C = select %cond, %B, 0 |
| 7808 | /// %D = or %A, %C |
| 7809 | /// |
| 7810 | /// Assuming that the specified instruction is an operand to the select, return |
| 7811 | /// a bitmask indicating which operands of this instruction are foldable if they |
| 7812 | /// equal the other incoming value of the select. |
| 7813 | /// |
| 7814 | static unsigned GetSelectFoldableOperands(Instruction *I) { |
| 7815 | switch (I->getOpcode()) { |
| 7816 | case Instruction::Add: |
| 7817 | case Instruction::Mul: |
| 7818 | case Instruction::And: |
| 7819 | case Instruction::Or: |
| 7820 | case Instruction::Xor: |
| 7821 | return 3; // Can fold through either operand. |
| 7822 | case Instruction::Sub: // Can only fold on the amount subtracted. |
| 7823 | case Instruction::Shl: // Can only fold on the shift amount. |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 7824 | case Instruction::LShr: |
| 7825 | case Instruction::AShr: |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7826 | return 1; |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7827 | default: |
| 7828 | return 0; // Cannot fold |
| 7829 | } |
| 7830 | } |
| 7831 | |
| 7832 | /// GetSelectFoldableConstant - For the same transformation as the previous |
| 7833 | /// function, return the identity constant that goes into the select. |
| 7834 | static Constant *GetSelectFoldableConstant(Instruction *I) { |
| 7835 | switch (I->getOpcode()) { |
| 7836 | default: assert(0 && "This cannot happen!"); abort(); |
| 7837 | case Instruction::Add: |
| 7838 | case Instruction::Sub: |
| 7839 | case Instruction::Or: |
| 7840 | case Instruction::Xor: |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7841 | case Instruction::Shl: |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 7842 | case Instruction::LShr: |
| 7843 | case Instruction::AShr: |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7844 | return Constant::getNullValue(I->getType()); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7845 | case Instruction::And: |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 7846 | return Constant::getAllOnesValue(I->getType()); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7847 | case Instruction::Mul: |
| 7848 | return ConstantInt::get(I->getType(), 1); |
| 7849 | } |
| 7850 | } |
| 7851 | |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 7852 | /// FoldSelectOpOp - Here we have (select c, TI, FI), and we know that TI and FI |
| 7853 | /// have the same opcode and only one use each. Try to simplify this. |
| 7854 | Instruction *InstCombiner::FoldSelectOpOp(SelectInst &SI, Instruction *TI, |
| 7855 | Instruction *FI) { |
| 7856 | if (TI->getNumOperands() == 1) { |
| 7857 | // If this is a non-volatile load or a cast from the same type, |
| 7858 | // merge. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7859 | if (TI->isCast()) { |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 7860 | if (TI->getOperand(0)->getType() != FI->getOperand(0)->getType()) |
| 7861 | return 0; |
| 7862 | } else { |
| 7863 | return 0; // unknown unary op. |
| 7864 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7865 | |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 7866 | // Fold this by inserting a select from the input values. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 7867 | SelectInst *NewSI = SelectInst::Create(SI.getCondition(), TI->getOperand(0), |
| 7868 | FI->getOperand(0), SI.getName()+".v"); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 7869 | InsertNewInstBefore(NewSI, SI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7870 | return CastInst::create(Instruction::CastOps(TI->getOpcode()), NewSI, |
| 7871 | TI->getType()); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 7872 | } |
| 7873 | |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7874 | // Only handle binary operators here. |
| 7875 | if (!isa<BinaryOperator>(TI)) |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 7876 | return 0; |
| 7877 | |
| 7878 | // Figure out if the operations have any operands in common. |
| 7879 | Value *MatchOp, *OtherOpT, *OtherOpF; |
| 7880 | bool MatchIsOpZero; |
| 7881 | if (TI->getOperand(0) == FI->getOperand(0)) { |
| 7882 | MatchOp = TI->getOperand(0); |
| 7883 | OtherOpT = TI->getOperand(1); |
| 7884 | OtherOpF = FI->getOperand(1); |
| 7885 | MatchIsOpZero = true; |
| 7886 | } else if (TI->getOperand(1) == FI->getOperand(1)) { |
| 7887 | MatchOp = TI->getOperand(1); |
| 7888 | OtherOpT = TI->getOperand(0); |
| 7889 | OtherOpF = FI->getOperand(0); |
| 7890 | MatchIsOpZero = false; |
| 7891 | } else if (!TI->isCommutative()) { |
| 7892 | return 0; |
| 7893 | } else if (TI->getOperand(0) == FI->getOperand(1)) { |
| 7894 | MatchOp = TI->getOperand(0); |
| 7895 | OtherOpT = TI->getOperand(1); |
| 7896 | OtherOpF = FI->getOperand(0); |
| 7897 | MatchIsOpZero = true; |
| 7898 | } else if (TI->getOperand(1) == FI->getOperand(0)) { |
| 7899 | MatchOp = TI->getOperand(1); |
| 7900 | OtherOpT = TI->getOperand(0); |
| 7901 | OtherOpF = FI->getOperand(1); |
| 7902 | MatchIsOpZero = true; |
| 7903 | } else { |
| 7904 | return 0; |
| 7905 | } |
| 7906 | |
| 7907 | // If we reach here, they do have operations in common. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 7908 | SelectInst *NewSI = SelectInst::Create(SI.getCondition(), OtherOpT, |
| 7909 | OtherOpF, SI.getName()+".v"); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 7910 | InsertNewInstBefore(NewSI, SI); |
| 7911 | |
| 7912 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TI)) { |
| 7913 | if (MatchIsOpZero) |
| 7914 | return BinaryOperator::create(BO->getOpcode(), MatchOp, NewSI); |
| 7915 | else |
| 7916 | return BinaryOperator::create(BO->getOpcode(), NewSI, MatchOp); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 7917 | } |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 7918 | assert(0 && "Shouldn't get here"); |
| 7919 | return 0; |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 7920 | } |
| 7921 | |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 7922 | Instruction *InstCombiner::visitSelectInst(SelectInst &SI) { |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 7923 | Value *CondVal = SI.getCondition(); |
| 7924 | Value *TrueVal = SI.getTrueValue(); |
| 7925 | Value *FalseVal = SI.getFalseValue(); |
| 7926 | |
| 7927 | // select true, X, Y -> X |
| 7928 | // select false, X, Y -> Y |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 7929 | if (ConstantInt *C = dyn_cast<ConstantInt>(CondVal)) |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 7930 | return ReplaceInstUsesWith(SI, C->getZExtValue() ? TrueVal : FalseVal); |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 7931 | |
| 7932 | // select C, X, X -> X |
| 7933 | if (TrueVal == FalseVal) |
| 7934 | return ReplaceInstUsesWith(SI, TrueVal); |
| 7935 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 7936 | if (isa<UndefValue>(TrueVal)) // select C, undef, X -> X |
| 7937 | return ReplaceInstUsesWith(SI, FalseVal); |
| 7938 | if (isa<UndefValue>(FalseVal)) // select C, X, undef -> X |
| 7939 | return ReplaceInstUsesWith(SI, TrueVal); |
| 7940 | if (isa<UndefValue>(CondVal)) { // select undef, X, Y -> X or Y |
| 7941 | if (isa<Constant>(TrueVal)) |
| 7942 | return ReplaceInstUsesWith(SI, TrueVal); |
| 7943 | else |
| 7944 | return ReplaceInstUsesWith(SI, FalseVal); |
| 7945 | } |
| 7946 | |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 7947 | if (SI.getType() == Type::Int1Ty) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 7948 | if (ConstantInt *C = dyn_cast<ConstantInt>(TrueVal)) { |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 7949 | if (C->getZExtValue()) { |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 7950 | // Change: A = select B, true, C --> A = or B, C |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 7951 | return BinaryOperator::createOr(CondVal, FalseVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 7952 | } else { |
| 7953 | // Change: A = select B, false, C --> A = and !B, C |
| 7954 | Value *NotCond = |
| 7955 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
| 7956 | "not."+CondVal->getName()), SI); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 7957 | return BinaryOperator::createAnd(NotCond, FalseVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 7958 | } |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 7959 | } else if (ConstantInt *C = dyn_cast<ConstantInt>(FalseVal)) { |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 7960 | if (C->getZExtValue() == false) { |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 7961 | // Change: A = select B, C, false --> A = and B, C |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 7962 | return BinaryOperator::createAnd(CondVal, TrueVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 7963 | } else { |
| 7964 | // Change: A = select B, C, true --> A = or !B, C |
| 7965 | Value *NotCond = |
| 7966 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
| 7967 | "not."+CondVal->getName()), SI); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 7968 | return BinaryOperator::createOr(NotCond, TrueVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 7969 | } |
| 7970 | } |
Chris Lattner | cfa5975 | 2007-11-25 21:27:53 +0000 | [diff] [blame] | 7971 | |
| 7972 | // select a, b, a -> a&b |
| 7973 | // select a, a, b -> a|b |
| 7974 | if (CondVal == TrueVal) |
| 7975 | return BinaryOperator::createOr(CondVal, FalseVal); |
| 7976 | else if (CondVal == FalseVal) |
| 7977 | return BinaryOperator::createAnd(CondVal, TrueVal); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 7978 | } |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 7979 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 7980 | // Selecting between two integer constants? |
| 7981 | if (ConstantInt *TrueValC = dyn_cast<ConstantInt>(TrueVal)) |
| 7982 | if (ConstantInt *FalseValC = dyn_cast<ConstantInt>(FalseVal)) { |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 7983 | // select C, 1, 0 -> zext C to int |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 7984 | if (FalseValC->isZero() && TrueValC->getValue() == 1) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7985 | return CastInst::create(Instruction::ZExt, CondVal, SI.getType()); |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 7986 | } else if (TrueValC->isZero() && FalseValC->getValue() == 1) { |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 7987 | // select C, 0, 1 -> zext !C to int |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 7988 | Value *NotCond = |
| 7989 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
Chris Lattner | 82e14fe | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 7990 | "not."+CondVal->getName()), SI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7991 | return CastInst::create(Instruction::ZExt, NotCond, SI.getType()); |
Chris Lattner | 82e14fe | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 7992 | } |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 7993 | |
| 7994 | // FIXME: Turn select 0/-1 and -1/0 into sext from condition! |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 7995 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7996 | if (ICmpInst *IC = dyn_cast<ICmpInst>(SI.getCondition())) { |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 7997 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7998 | // (x <s 0) ? -1 : 0 -> ashr x, 31 |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 7999 | if (TrueValC->isAllOnesValue() && FalseValC->isZero()) |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 8000 | if (ConstantInt *CmpCst = dyn_cast<ConstantInt>(IC->getOperand(1))) { |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 8001 | if (IC->getPredicate() == ICmpInst::ICMP_SLT && CmpCst->isZero()) { |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 8002 | // The comparison constant and the result are not neccessarily the |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8003 | // same width. Make an all-ones value by inserting a AShr. |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 8004 | Value *X = IC->getOperand(0); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 8005 | uint32_t Bits = X->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 8006 | Constant *ShAmt = ConstantInt::get(X->getType(), Bits-1); |
| 8007 | Instruction *SRA = BinaryOperator::create(Instruction::AShr, X, |
| 8008 | ShAmt, "ones"); |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 8009 | InsertNewInstBefore(SRA, SI); |
| 8010 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8011 | // Finally, convert to the type of the select RHS. We figure out |
| 8012 | // if this requires a SExt, Trunc or BitCast based on the sizes. |
| 8013 | Instruction::CastOps opc = Instruction::BitCast; |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 8014 | uint32_t SRASize = SRA->getType()->getPrimitiveSizeInBits(); |
| 8015 | uint32_t SISize = SI.getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8016 | if (SRASize < SISize) |
| 8017 | opc = Instruction::SExt; |
| 8018 | else if (SRASize > SISize) |
| 8019 | opc = Instruction::Trunc; |
| 8020 | return CastInst::create(opc, SRA, SI.getType()); |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 8021 | } |
| 8022 | } |
| 8023 | |
| 8024 | |
| 8025 | // 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] | 8026 | // 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] | 8027 | // non-constant value, eliminate this whole mess. This corresponds to |
| 8028 | // cases like this: ((X & 27) ? 27 : 0) |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 8029 | if (TrueValC->isZero() || FalseValC->isZero()) |
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 8030 | if (IC->isEquality() && isa<ConstantInt>(IC->getOperand(1)) && |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 8031 | cast<Constant>(IC->getOperand(1))->isNullValue()) |
| 8032 | if (Instruction *ICA = dyn_cast<Instruction>(IC->getOperand(0))) |
| 8033 | if (ICA->getOpcode() == Instruction::And && |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8034 | isa<ConstantInt>(ICA->getOperand(1)) && |
| 8035 | (ICA->getOperand(1) == TrueValC || |
| 8036 | ICA->getOperand(1) == FalseValC) && |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 8037 | isOneBitSet(cast<ConstantInt>(ICA->getOperand(1)))) { |
| 8038 | // 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] | 8039 | // know whether we have a icmp_ne or icmp_eq and whether the |
| 8040 | // true or false val is the zero. |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 8041 | bool ShouldNotVal = !TrueValC->isZero(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8042 | ShouldNotVal ^= IC->getPredicate() == ICmpInst::ICMP_NE; |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 8043 | Value *V = ICA; |
| 8044 | if (ShouldNotVal) |
| 8045 | V = InsertNewInstBefore(BinaryOperator::create( |
| 8046 | Instruction::Xor, V, ICA->getOperand(1)), SI); |
| 8047 | return ReplaceInstUsesWith(SI, V); |
| 8048 | } |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 8049 | } |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 8050 | } |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8051 | |
| 8052 | // 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] | 8053 | if (FCmpInst *FCI = dyn_cast<FCmpInst>(CondVal)) { |
| 8054 | if (FCI->getOperand(0) == TrueVal && FCI->getOperand(1) == FalseVal) { |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8055 | // Transform (X == Y) ? X : Y -> Y |
Dale Johannesen | 5a2174f | 2007-10-03 17:45:27 +0000 | [diff] [blame] | 8056 | if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) { |
| 8057 | // This is not safe in general for floating point: |
| 8058 | // consider X== -0, Y== +0. |
| 8059 | // It becomes safe if either operand is a nonzero constant. |
| 8060 | ConstantFP *CFPt, *CFPf; |
| 8061 | if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) && |
| 8062 | !CFPt->getValueAPF().isZero()) || |
| 8063 | ((CFPf = dyn_cast<ConstantFP>(FalseVal)) && |
| 8064 | !CFPf->getValueAPF().isZero())) |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8065 | return ReplaceInstUsesWith(SI, FalseVal); |
Dale Johannesen | 5a2174f | 2007-10-03 17:45:27 +0000 | [diff] [blame] | 8066 | } |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8067 | // Transform (X != Y) ? X : Y -> X |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8068 | if (FCI->getPredicate() == FCmpInst::FCMP_ONE) |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8069 | return ReplaceInstUsesWith(SI, TrueVal); |
| 8070 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 8071 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8072 | } else if (FCI->getOperand(0) == FalseVal && FCI->getOperand(1) == TrueVal){ |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8073 | // Transform (X == Y) ? Y : X -> X |
Dale Johannesen | 5a2174f | 2007-10-03 17:45:27 +0000 | [diff] [blame] | 8074 | if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) { |
| 8075 | // This is not safe in general for floating point: |
| 8076 | // consider X== -0, Y== +0. |
| 8077 | // It becomes safe if either operand is a nonzero constant. |
| 8078 | ConstantFP *CFPt, *CFPf; |
| 8079 | if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) && |
| 8080 | !CFPt->getValueAPF().isZero()) || |
| 8081 | ((CFPf = dyn_cast<ConstantFP>(FalseVal)) && |
| 8082 | !CFPf->getValueAPF().isZero())) |
| 8083 | return ReplaceInstUsesWith(SI, FalseVal); |
| 8084 | } |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8085 | // Transform (X != Y) ? Y : X -> Y |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8086 | if (FCI->getPredicate() == FCmpInst::FCMP_ONE) |
| 8087 | return ReplaceInstUsesWith(SI, TrueVal); |
| 8088 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 8089 | } |
| 8090 | } |
| 8091 | |
| 8092 | // See if we are selecting two values based on a comparison of the two values. |
| 8093 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(CondVal)) { |
| 8094 | if (ICI->getOperand(0) == TrueVal && ICI->getOperand(1) == FalseVal) { |
| 8095 | // Transform (X == Y) ? X : Y -> Y |
| 8096 | if (ICI->getPredicate() == ICmpInst::ICMP_EQ) |
| 8097 | return ReplaceInstUsesWith(SI, FalseVal); |
| 8098 | // Transform (X != Y) ? X : Y -> X |
| 8099 | if (ICI->getPredicate() == ICmpInst::ICMP_NE) |
| 8100 | return ReplaceInstUsesWith(SI, TrueVal); |
| 8101 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 8102 | |
| 8103 | } else if (ICI->getOperand(0) == FalseVal && ICI->getOperand(1) == TrueVal){ |
| 8104 | // Transform (X == Y) ? Y : X -> X |
| 8105 | if (ICI->getPredicate() == ICmpInst::ICMP_EQ) |
| 8106 | return ReplaceInstUsesWith(SI, FalseVal); |
| 8107 | // Transform (X != Y) ? Y : X -> Y |
| 8108 | if (ICI->getPredicate() == ICmpInst::ICMP_NE) |
Chris Lattner | fbede52 | 2004-04-11 01:39:19 +0000 | [diff] [blame] | 8109 | return ReplaceInstUsesWith(SI, TrueVal); |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8110 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 8111 | } |
| 8112 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8113 | |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 8114 | if (Instruction *TI = dyn_cast<Instruction>(TrueVal)) |
| 8115 | if (Instruction *FI = dyn_cast<Instruction>(FalseVal)) |
| 8116 | if (TI->hasOneUse() && FI->hasOneUse()) { |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 8117 | Instruction *AddOp = 0, *SubOp = 0; |
| 8118 | |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8119 | // Turn (select C, (op X, Y), (op X, Z)) -> (op X, (select C, Y, Z)) |
| 8120 | if (TI->getOpcode() == FI->getOpcode()) |
| 8121 | if (Instruction *IV = FoldSelectOpOp(SI, TI, FI)) |
| 8122 | return IV; |
| 8123 | |
| 8124 | // Turn select C, (X+Y), (X-Y) --> (X+(select C, Y, (-Y))). This is |
| 8125 | // even legal for FP. |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 8126 | if (TI->getOpcode() == Instruction::Sub && |
| 8127 | FI->getOpcode() == Instruction::Add) { |
| 8128 | AddOp = FI; SubOp = TI; |
| 8129 | } else if (FI->getOpcode() == Instruction::Sub && |
| 8130 | TI->getOpcode() == Instruction::Add) { |
| 8131 | AddOp = TI; SubOp = FI; |
| 8132 | } |
| 8133 | |
| 8134 | if (AddOp) { |
| 8135 | Value *OtherAddOp = 0; |
| 8136 | if (SubOp->getOperand(0) == AddOp->getOperand(0)) { |
| 8137 | OtherAddOp = AddOp->getOperand(1); |
| 8138 | } else if (SubOp->getOperand(0) == AddOp->getOperand(1)) { |
| 8139 | OtherAddOp = AddOp->getOperand(0); |
| 8140 | } |
| 8141 | |
| 8142 | if (OtherAddOp) { |
Chris Lattner | 97f37a4 | 2006-02-24 18:05:58 +0000 | [diff] [blame] | 8143 | // So at this point we know we have (Y -> OtherAddOp): |
| 8144 | // select C, (add X, Y), (sub X, Z) |
| 8145 | Value *NegVal; // Compute -Z |
| 8146 | if (Constant *C = dyn_cast<Constant>(SubOp->getOperand(1))) { |
| 8147 | NegVal = ConstantExpr::getNeg(C); |
| 8148 | } else { |
| 8149 | NegVal = InsertNewInstBefore( |
| 8150 | BinaryOperator::createNeg(SubOp->getOperand(1), "tmp"), SI); |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 8151 | } |
Chris Lattner | 97f37a4 | 2006-02-24 18:05:58 +0000 | [diff] [blame] | 8152 | |
| 8153 | Value *NewTrueOp = OtherAddOp; |
| 8154 | Value *NewFalseOp = NegVal; |
| 8155 | if (AddOp != TI) |
| 8156 | std::swap(NewTrueOp, NewFalseOp); |
| 8157 | Instruction *NewSel = |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 8158 | SelectInst::Create(CondVal, NewTrueOp,NewFalseOp,SI.getName()+".p"); |
Chris Lattner | 97f37a4 | 2006-02-24 18:05:58 +0000 | [diff] [blame] | 8159 | |
| 8160 | NewSel = InsertNewInstBefore(NewSel, SI); |
| 8161 | return BinaryOperator::createAdd(SubOp->getOperand(0), NewSel); |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 8162 | } |
| 8163 | } |
| 8164 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8165 | |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8166 | // See if we can fold the select into one of our operands. |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 8167 | if (SI.getType()->isInteger()) { |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8168 | // See the comment above GetSelectFoldableOperands for a description of the |
| 8169 | // transformation we are doing here. |
| 8170 | if (Instruction *TVI = dyn_cast<Instruction>(TrueVal)) |
| 8171 | if (TVI->hasOneUse() && TVI->getNumOperands() == 2 && |
| 8172 | !isa<Constant>(FalseVal)) |
| 8173 | if (unsigned SFO = GetSelectFoldableOperands(TVI)) { |
| 8174 | unsigned OpToFold = 0; |
| 8175 | if ((SFO & 1) && FalseVal == TVI->getOperand(0)) { |
| 8176 | OpToFold = 1; |
| 8177 | } else if ((SFO & 2) && FalseVal == TVI->getOperand(1)) { |
| 8178 | OpToFold = 2; |
| 8179 | } |
| 8180 | |
| 8181 | if (OpToFold) { |
| 8182 | Constant *C = GetSelectFoldableConstant(TVI); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8183 | Instruction *NewSel = |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 8184 | SelectInst::Create(SI.getCondition(), TVI->getOperand(2-OpToFold), C); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8185 | InsertNewInstBefore(NewSel, SI); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 8186 | NewSel->takeName(TVI); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8187 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TVI)) |
| 8188 | return BinaryOperator::create(BO->getOpcode(), FalseVal, NewSel); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8189 | else { |
| 8190 | assert(0 && "Unknown instruction!!"); |
| 8191 | } |
| 8192 | } |
| 8193 | } |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 8194 | |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8195 | if (Instruction *FVI = dyn_cast<Instruction>(FalseVal)) |
| 8196 | if (FVI->hasOneUse() && FVI->getNumOperands() == 2 && |
| 8197 | !isa<Constant>(TrueVal)) |
| 8198 | if (unsigned SFO = GetSelectFoldableOperands(FVI)) { |
| 8199 | unsigned OpToFold = 0; |
| 8200 | if ((SFO & 1) && TrueVal == FVI->getOperand(0)) { |
| 8201 | OpToFold = 1; |
| 8202 | } else if ((SFO & 2) && TrueVal == FVI->getOperand(1)) { |
| 8203 | OpToFold = 2; |
| 8204 | } |
| 8205 | |
| 8206 | if (OpToFold) { |
| 8207 | Constant *C = GetSelectFoldableConstant(FVI); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8208 | Instruction *NewSel = |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 8209 | SelectInst::Create(SI.getCondition(), C, FVI->getOperand(2-OpToFold)); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8210 | InsertNewInstBefore(NewSel, SI); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 8211 | NewSel->takeName(FVI); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8212 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(FVI)) |
| 8213 | return BinaryOperator::create(BO->getOpcode(), TrueVal, NewSel); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 8214 | else |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8215 | assert(0 && "Unknown instruction!!"); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8216 | } |
| 8217 | } |
| 8218 | } |
Chris Lattner | a1df33c | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 8219 | |
| 8220 | if (BinaryOperator::isNot(CondVal)) { |
| 8221 | SI.setOperand(0, BinaryOperator::getNotArgument(CondVal)); |
| 8222 | SI.setOperand(1, FalseVal); |
| 8223 | SI.setOperand(2, TrueVal); |
| 8224 | return &SI; |
| 8225 | } |
| 8226 | |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 8227 | return 0; |
| 8228 | } |
| 8229 | |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8230 | /// EnforceKnownAlignment - If the specified pointer points to an object that |
| 8231 | /// we control, modify the object's alignment to PrefAlign. This isn't |
| 8232 | /// often possible though. If alignment is important, a more reliable approach |
| 8233 | /// is to simply align all global variables and allocation instructions to |
| 8234 | /// their preferred alignment from the beginning. |
| 8235 | /// |
| 8236 | static unsigned EnforceKnownAlignment(Value *V, |
| 8237 | unsigned Align, unsigned PrefAlign) { |
Chris Lattner | f2369f2 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 8238 | |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8239 | User *U = dyn_cast<User>(V); |
| 8240 | if (!U) return Align; |
| 8241 | |
| 8242 | switch (getOpcode(U)) { |
| 8243 | default: break; |
| 8244 | case Instruction::BitCast: |
| 8245 | return EnforceKnownAlignment(U->getOperand(0), Align, PrefAlign); |
| 8246 | case Instruction::GetElementPtr: { |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8247 | // If all indexes are zero, it is just the alignment of the base pointer. |
| 8248 | bool AllZeroOperands = true; |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8249 | for (unsigned i = 1, e = U->getNumOperands(); i != e; ++i) |
| 8250 | if (!isa<Constant>(U->getOperand(i)) || |
| 8251 | !cast<Constant>(U->getOperand(i))->isNullValue()) { |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8252 | AllZeroOperands = false; |
| 8253 | break; |
| 8254 | } |
Chris Lattner | f2369f2 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 8255 | |
| 8256 | if (AllZeroOperands) { |
| 8257 | // Treat this like a bitcast. |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8258 | return EnforceKnownAlignment(U->getOperand(0), Align, PrefAlign); |
Chris Lattner | f2369f2 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 8259 | } |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8260 | break; |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8261 | } |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8262 | } |
| 8263 | |
| 8264 | if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
| 8265 | // If there is a large requested alignment and we can, bump up the alignment |
| 8266 | // of the global. |
| 8267 | if (!GV->isDeclaration()) { |
| 8268 | GV->setAlignment(PrefAlign); |
| 8269 | Align = PrefAlign; |
| 8270 | } |
| 8271 | } else if (AllocationInst *AI = dyn_cast<AllocationInst>(V)) { |
| 8272 | // If there is a requested alignment and if this is an alloca, round up. We |
| 8273 | // don't do this for malloc, because some systems can't respect the request. |
| 8274 | if (isa<AllocaInst>(AI)) { |
| 8275 | AI->setAlignment(PrefAlign); |
| 8276 | Align = PrefAlign; |
| 8277 | } |
| 8278 | } |
| 8279 | |
| 8280 | return Align; |
| 8281 | } |
| 8282 | |
| 8283 | /// GetOrEnforceKnownAlignment - If the specified pointer has an alignment that |
| 8284 | /// we can determine, return it, otherwise return 0. If PrefAlign is specified, |
| 8285 | /// and it is more than the alignment of the ultimate object, see if we can |
| 8286 | /// increase the alignment of the ultimate object, making this check succeed. |
| 8287 | unsigned InstCombiner::GetOrEnforceKnownAlignment(Value *V, |
| 8288 | unsigned PrefAlign) { |
| 8289 | unsigned BitWidth = TD ? TD->getTypeSizeInBits(V->getType()) : |
| 8290 | sizeof(PrefAlign) * CHAR_BIT; |
| 8291 | APInt Mask = APInt::getAllOnesValue(BitWidth); |
| 8292 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 8293 | ComputeMaskedBits(V, Mask, KnownZero, KnownOne); |
| 8294 | unsigned TrailZ = KnownZero.countTrailingOnes(); |
| 8295 | unsigned Align = 1u << std::min(BitWidth - 1, TrailZ); |
| 8296 | |
| 8297 | if (PrefAlign > Align) |
| 8298 | Align = EnforceKnownAlignment(V, Align, PrefAlign); |
| 8299 | |
| 8300 | // We don't need to make any adjustment. |
| 8301 | return Align; |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8302 | } |
| 8303 | |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8304 | Instruction *InstCombiner::SimplifyMemTransfer(MemIntrinsic *MI) { |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8305 | unsigned DstAlign = GetOrEnforceKnownAlignment(MI->getOperand(1)); |
| 8306 | unsigned SrcAlign = GetOrEnforceKnownAlignment(MI->getOperand(2)); |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8307 | unsigned MinAlign = std::min(DstAlign, SrcAlign); |
| 8308 | unsigned CopyAlign = MI->getAlignment()->getZExtValue(); |
| 8309 | |
| 8310 | if (CopyAlign < MinAlign) { |
| 8311 | MI->setAlignment(ConstantInt::get(Type::Int32Ty, MinAlign)); |
| 8312 | return MI; |
| 8313 | } |
| 8314 | |
| 8315 | // If MemCpyInst length is 1/2/4/8 bytes then replace memcpy with |
| 8316 | // load/store. |
| 8317 | ConstantInt *MemOpLength = dyn_cast<ConstantInt>(MI->getOperand(3)); |
| 8318 | if (MemOpLength == 0) return 0; |
| 8319 | |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 8320 | // Source and destination pointer types are always "i8*" for intrinsic. See |
| 8321 | // if the size is something we can handle with a single primitive load/store. |
| 8322 | // A single load+store correctly handles overlapping memory in the memmove |
| 8323 | // case. |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8324 | unsigned Size = MemOpLength->getZExtValue(); |
| 8325 | if (Size == 0 || Size > 8 || (Size&(Size-1))) |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 8326 | return 0; // If not 1/2/4/8 bytes, exit. |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8327 | |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 8328 | // Use an integer load+store unless we can find something better. |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8329 | Type *NewPtrTy = PointerType::getUnqual(IntegerType::get(Size<<3)); |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 8330 | |
| 8331 | // Memcpy forces the use of i8* for the source and destination. That means |
| 8332 | // that if you're using memcpy to move one double around, you'll get a cast |
| 8333 | // from double* to i8*. We'd much rather use a double load+store rather than |
| 8334 | // an i64 load+store, here because this improves the odds that the source or |
| 8335 | // dest address will be promotable. See if we can find a better type than the |
| 8336 | // integer datatype. |
| 8337 | if (Value *Op = getBitCastOperand(MI->getOperand(1))) { |
| 8338 | const Type *SrcETy = cast<PointerType>(Op->getType())->getElementType(); |
| 8339 | if (SrcETy->isSized() && TD->getTypeStoreSize(SrcETy) == Size) { |
| 8340 | // The SrcETy might be something like {{{double}}} or [1 x double]. Rip |
| 8341 | // down through these levels if so. |
| 8342 | while (!SrcETy->isFirstClassType()) { |
| 8343 | if (const StructType *STy = dyn_cast<StructType>(SrcETy)) { |
| 8344 | if (STy->getNumElements() == 1) |
| 8345 | SrcETy = STy->getElementType(0); |
| 8346 | else |
| 8347 | break; |
| 8348 | } else if (const ArrayType *ATy = dyn_cast<ArrayType>(SrcETy)) { |
| 8349 | if (ATy->getNumElements() == 1) |
| 8350 | SrcETy = ATy->getElementType(); |
| 8351 | else |
| 8352 | break; |
| 8353 | } else |
| 8354 | break; |
| 8355 | } |
| 8356 | |
| 8357 | if (SrcETy->isFirstClassType()) |
| 8358 | NewPtrTy = PointerType::getUnqual(SrcETy); |
| 8359 | } |
| 8360 | } |
| 8361 | |
| 8362 | |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8363 | // If the memcpy/memmove provides better alignment info than we can |
| 8364 | // infer, use it. |
| 8365 | SrcAlign = std::max(SrcAlign, CopyAlign); |
| 8366 | DstAlign = std::max(DstAlign, CopyAlign); |
| 8367 | |
| 8368 | Value *Src = InsertBitCastBefore(MI->getOperand(2), NewPtrTy, *MI); |
| 8369 | Value *Dest = InsertBitCastBefore(MI->getOperand(1), NewPtrTy, *MI); |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 8370 | Instruction *L = new LoadInst(Src, "tmp", false, SrcAlign); |
| 8371 | InsertNewInstBefore(L, *MI); |
| 8372 | InsertNewInstBefore(new StoreInst(L, Dest, false, DstAlign), *MI); |
| 8373 | |
| 8374 | // Set the size of the copy to 0, it will be deleted on the next iteration. |
| 8375 | MI->setOperand(3, Constant::getNullValue(MemOpLength->getType())); |
| 8376 | return MI; |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8377 | } |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 8378 | |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 8379 | /// visitCallInst - CallInst simplification. This mostly only handles folding |
| 8380 | /// of intrinsic instructions. For normal calls, it allows visitCallSite to do |
| 8381 | /// the heavy lifting. |
| 8382 | /// |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8383 | Instruction *InstCombiner::visitCallInst(CallInst &CI) { |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 8384 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(&CI); |
| 8385 | if (!II) return visitCallSite(&CI); |
| 8386 | |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 8387 | // Intrinsics cannot occur in an invoke, so handle them here instead of in |
| 8388 | // visitCallSite. |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 8389 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(II)) { |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8390 | bool Changed = false; |
| 8391 | |
| 8392 | // memmove/cpy/set of zero bytes is a noop. |
| 8393 | if (Constant *NumBytes = dyn_cast<Constant>(MI->getLength())) { |
| 8394 | if (NumBytes->isNullValue()) return EraseInstFromFunction(CI); |
| 8395 | |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8396 | if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes)) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8397 | if (CI->getZExtValue() == 1) { |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8398 | // Replace the instruction with just byte operations. We would |
| 8399 | // transform other cases to loads/stores, but we don't know if |
| 8400 | // alignment is sufficient. |
| 8401 | } |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 8402 | } |
| 8403 | |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8404 | // If we have a memmove and the source operation is a constant global, |
| 8405 | // then the source and dest pointers can't alias, so we can change this |
| 8406 | // into a call to memcpy. |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8407 | if (MemMoveInst *MMI = dyn_cast<MemMoveInst>(MI)) { |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8408 | if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource())) |
| 8409 | if (GVSrc->isConstant()) { |
| 8410 | Module *M = CI.getParent()->getParent()->getParent(); |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 8411 | Intrinsic::ID MemCpyID; |
| 8412 | if (CI.getOperand(3)->getType() == Type::Int32Ty) |
| 8413 | MemCpyID = Intrinsic::memcpy_i32; |
Chris Lattner | 2195939 | 2006-03-03 01:34:17 +0000 | [diff] [blame] | 8414 | else |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 8415 | MemCpyID = Intrinsic::memcpy_i64; |
| 8416 | CI.setOperand(0, Intrinsic::getDeclaration(M, MemCpyID)); |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8417 | Changed = true; |
| 8418 | } |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8419 | } |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8420 | |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8421 | // If we can determine a pointer alignment that is bigger than currently |
| 8422 | // set, update the alignment. |
| 8423 | if (isa<MemCpyInst>(MI) || isa<MemMoveInst>(MI)) { |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8424 | if (Instruction *I = SimplifyMemTransfer(MI)) |
| 8425 | return I; |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8426 | } else if (isa<MemSetInst>(MI)) { |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8427 | unsigned Alignment = GetOrEnforceKnownAlignment(MI->getDest()); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8428 | if (MI->getAlignment()->getZExtValue() < Alignment) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8429 | MI->setAlignment(ConstantInt::get(Type::Int32Ty, Alignment)); |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8430 | Changed = true; |
| 8431 | } |
| 8432 | } |
| 8433 | |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 8434 | if (Changed) return II; |
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 8435 | } else { |
| 8436 | switch (II->getIntrinsicID()) { |
| 8437 | default: break; |
Chris Lattner | 82ed58f | 2006-04-02 05:30:25 +0000 | [diff] [blame] | 8438 | case Intrinsic::ppc_altivec_lvx: |
| 8439 | case Intrinsic::ppc_altivec_lvxl: |
Chris Lattner | fd6bdf0 | 2006-04-17 22:26:56 +0000 | [diff] [blame] | 8440 | case Intrinsic::x86_sse_loadu_ps: |
| 8441 | case Intrinsic::x86_sse2_loadu_pd: |
| 8442 | case Intrinsic::x86_sse2_loadu_dq: |
| 8443 | // Turn PPC lvx -> load if the pointer is known aligned. |
| 8444 | // Turn X86 loadups -> load if the pointer is known aligned. |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8445 | if (GetOrEnforceKnownAlignment(II->getOperand(1), 16) >= 16) { |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 8446 | Value *Ptr = InsertBitCastBefore(II->getOperand(1), |
| 8447 | PointerType::getUnqual(II->getType()), |
| 8448 | CI); |
Chris Lattner | 82ed58f | 2006-04-02 05:30:25 +0000 | [diff] [blame] | 8449 | return new LoadInst(Ptr); |
| 8450 | } |
| 8451 | break; |
| 8452 | case Intrinsic::ppc_altivec_stvx: |
| 8453 | case Intrinsic::ppc_altivec_stvxl: |
| 8454 | // Turn stvx -> store if the pointer is known aligned. |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8455 | if (GetOrEnforceKnownAlignment(II->getOperand(2), 16) >= 16) { |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 8456 | const Type *OpPtrTy = |
| 8457 | PointerType::getUnqual(II->getOperand(1)->getType()); |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 8458 | Value *Ptr = InsertBitCastBefore(II->getOperand(2), OpPtrTy, CI); |
Chris Lattner | 82ed58f | 2006-04-02 05:30:25 +0000 | [diff] [blame] | 8459 | return new StoreInst(II->getOperand(1), Ptr); |
| 8460 | } |
| 8461 | break; |
Chris Lattner | fd6bdf0 | 2006-04-17 22:26:56 +0000 | [diff] [blame] | 8462 | case Intrinsic::x86_sse_storeu_ps: |
| 8463 | case Intrinsic::x86_sse2_storeu_pd: |
| 8464 | case Intrinsic::x86_sse2_storeu_dq: |
| 8465 | case Intrinsic::x86_sse2_storel_dq: |
| 8466 | // Turn X86 storeu -> store if the pointer is known aligned. |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8467 | if (GetOrEnforceKnownAlignment(II->getOperand(1), 16) >= 16) { |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 8468 | const Type *OpPtrTy = |
| 8469 | PointerType::getUnqual(II->getOperand(2)->getType()); |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 8470 | Value *Ptr = InsertBitCastBefore(II->getOperand(1), OpPtrTy, CI); |
Chris Lattner | fd6bdf0 | 2006-04-17 22:26:56 +0000 | [diff] [blame] | 8471 | return new StoreInst(II->getOperand(2), Ptr); |
| 8472 | } |
| 8473 | break; |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 8474 | |
| 8475 | case Intrinsic::x86_sse_cvttss2si: { |
| 8476 | // These intrinsics only demands the 0th element of its input vector. If |
| 8477 | // we can simplify the input based on that, do so now. |
| 8478 | uint64_t UndefElts; |
| 8479 | if (Value *V = SimplifyDemandedVectorElts(II->getOperand(1), 1, |
| 8480 | UndefElts)) { |
| 8481 | II->setOperand(1, V); |
| 8482 | return II; |
| 8483 | } |
| 8484 | break; |
| 8485 | } |
| 8486 | |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8487 | case Intrinsic::ppc_altivec_vperm: |
| 8488 | // 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] | 8489 | if (ConstantVector *Mask = dyn_cast<ConstantVector>(II->getOperand(3))) { |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8490 | assert(Mask->getNumOperands() == 16 && "Bad type for intrinsic!"); |
| 8491 | |
| 8492 | // Check that all of the elements are integer constants or undefs. |
| 8493 | bool AllEltsOk = true; |
| 8494 | for (unsigned i = 0; i != 16; ++i) { |
| 8495 | if (!isa<ConstantInt>(Mask->getOperand(i)) && |
| 8496 | !isa<UndefValue>(Mask->getOperand(i))) { |
| 8497 | AllEltsOk = false; |
| 8498 | break; |
| 8499 | } |
| 8500 | } |
| 8501 | |
| 8502 | if (AllEltsOk) { |
| 8503 | // Cast the input vectors to byte vectors. |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 8504 | Value *Op0 =InsertBitCastBefore(II->getOperand(1),Mask->getType(),CI); |
| 8505 | Value *Op1 =InsertBitCastBefore(II->getOperand(2),Mask->getType(),CI); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8506 | Value *Result = UndefValue::get(Op0->getType()); |
| 8507 | |
| 8508 | // Only extract each element once. |
| 8509 | Value *ExtractedElts[32]; |
| 8510 | memset(ExtractedElts, 0, sizeof(ExtractedElts)); |
| 8511 | |
| 8512 | for (unsigned i = 0; i != 16; ++i) { |
| 8513 | if (isa<UndefValue>(Mask->getOperand(i))) |
| 8514 | continue; |
Chris Lattner | e34e9a2 | 2007-04-14 23:32:02 +0000 | [diff] [blame] | 8515 | unsigned Idx=cast<ConstantInt>(Mask->getOperand(i))->getZExtValue(); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8516 | Idx &= 31; // Match the hardware behavior. |
| 8517 | |
| 8518 | if (ExtractedElts[Idx] == 0) { |
| 8519 | Instruction *Elt = |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 8520 | new ExtractElementInst(Idx < 16 ? Op0 : Op1, Idx&15, "tmp"); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8521 | InsertNewInstBefore(Elt, CI); |
| 8522 | ExtractedElts[Idx] = Elt; |
| 8523 | } |
| 8524 | |
| 8525 | // Insert this value into the result vector. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 8526 | Result = InsertElementInst::Create(Result, ExtractedElts[Idx], i, "tmp"); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8527 | InsertNewInstBefore(cast<Instruction>(Result), CI); |
| 8528 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8529 | return CastInst::create(Instruction::BitCast, Result, CI.getType()); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8530 | } |
| 8531 | } |
| 8532 | break; |
| 8533 | |
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 8534 | case Intrinsic::stackrestore: { |
| 8535 | // If the save is right next to the restore, remove the restore. This can |
| 8536 | // happen when variable allocas are DCE'd. |
| 8537 | if (IntrinsicInst *SS = dyn_cast<IntrinsicInst>(II->getOperand(1))) { |
| 8538 | if (SS->getIntrinsicID() == Intrinsic::stacksave) { |
| 8539 | BasicBlock::iterator BI = SS; |
| 8540 | if (&*++BI == II) |
| 8541 | return EraseInstFromFunction(CI); |
| 8542 | } |
| 8543 | } |
| 8544 | |
Chris Lattner | bf1d8a7 | 2008-02-18 06:12:38 +0000 | [diff] [blame] | 8545 | // Scan down this block to see if there is another stack restore in the |
| 8546 | // same block without an intervening call/alloca. |
| 8547 | BasicBlock::iterator BI = II; |
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 8548 | TerminatorInst *TI = II->getParent()->getTerminator(); |
Chris Lattner | bf1d8a7 | 2008-02-18 06:12:38 +0000 | [diff] [blame] | 8549 | bool CannotRemove = false; |
| 8550 | for (++BI; &*BI != TI; ++BI) { |
| 8551 | if (isa<AllocaInst>(BI)) { |
| 8552 | CannotRemove = true; |
| 8553 | break; |
| 8554 | } |
| 8555 | if (isa<CallInst>(BI)) { |
| 8556 | if (!isa<IntrinsicInst>(BI)) { |
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 8557 | CannotRemove = true; |
| 8558 | break; |
| 8559 | } |
Chris Lattner | bf1d8a7 | 2008-02-18 06:12:38 +0000 | [diff] [blame] | 8560 | // If there is a stackrestore below this one, remove this one. |
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 8561 | return EraseInstFromFunction(CI); |
Chris Lattner | bf1d8a7 | 2008-02-18 06:12:38 +0000 | [diff] [blame] | 8562 | } |
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 8563 | } |
Chris Lattner | bf1d8a7 | 2008-02-18 06:12:38 +0000 | [diff] [blame] | 8564 | |
| 8565 | // If the stack restore is in a return/unwind block and if there are no |
| 8566 | // allocas or calls between the restore and the return, nuke the restore. |
| 8567 | if (!CannotRemove && (isa<ReturnInst>(TI) || isa<UnwindInst>(TI))) |
| 8568 | return EraseInstFromFunction(CI); |
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 8569 | break; |
| 8570 | } |
| 8571 | } |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8572 | } |
| 8573 | |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 8574 | return visitCallSite(II); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8575 | } |
| 8576 | |
| 8577 | // InvokeInst simplification |
| 8578 | // |
| 8579 | Instruction *InstCombiner::visitInvokeInst(InvokeInst &II) { |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 8580 | return visitCallSite(&II); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8581 | } |
| 8582 | |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 8583 | // visitCallSite - Improvements for call and invoke instructions. |
| 8584 | // |
| 8585 | Instruction *InstCombiner::visitCallSite(CallSite CS) { |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8586 | bool Changed = false; |
| 8587 | |
| 8588 | // If the callee is a constexpr cast of a function, attempt to move the cast |
| 8589 | // to the arguments of the call/invoke. |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 8590 | if (transformConstExprCastCall(CS)) return 0; |
| 8591 | |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8592 | Value *Callee = CS.getCalledValue(); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8593 | |
Chris Lattner | 08b22ec | 2005-05-13 07:09:09 +0000 | [diff] [blame] | 8594 | if (Function *CalleeF = dyn_cast<Function>(Callee)) |
| 8595 | if (CalleeF->getCallingConv() != CS.getCallingConv()) { |
| 8596 | Instruction *OldCall = CS.getInstruction(); |
| 8597 | // If the call and callee calling conventions don't match, this call must |
| 8598 | // be unreachable, as the call is undefined. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 8599 | new StoreInst(ConstantInt::getTrue(), |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 8600 | UndefValue::get(PointerType::getUnqual(Type::Int1Ty)), |
| 8601 | OldCall); |
Chris Lattner | 08b22ec | 2005-05-13 07:09:09 +0000 | [diff] [blame] | 8602 | if (!OldCall->use_empty()) |
| 8603 | OldCall->replaceAllUsesWith(UndefValue::get(OldCall->getType())); |
| 8604 | if (isa<CallInst>(OldCall)) // Not worth removing an invoke here. |
| 8605 | return EraseInstFromFunction(*OldCall); |
| 8606 | return 0; |
| 8607 | } |
| 8608 | |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8609 | if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) { |
| 8610 | // This instruction is not reachable, just remove it. We insert a store to |
| 8611 | // undef so that we know that this code is not reachable, despite the fact |
| 8612 | // that we can't modify the CFG here. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 8613 | new StoreInst(ConstantInt::getTrue(), |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 8614 | UndefValue::get(PointerType::getUnqual(Type::Int1Ty)), |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8615 | CS.getInstruction()); |
| 8616 | |
| 8617 | if (!CS.getInstruction()->use_empty()) |
| 8618 | CS.getInstruction()-> |
| 8619 | replaceAllUsesWith(UndefValue::get(CS.getInstruction()->getType())); |
| 8620 | |
| 8621 | if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) { |
| 8622 | // Don't break the CFG, insert a dummy cond branch. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 8623 | BranchInst::Create(II->getNormalDest(), II->getUnwindDest(), |
| 8624 | ConstantInt::getTrue(), II); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8625 | } |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8626 | return EraseInstFromFunction(*CS.getInstruction()); |
| 8627 | } |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8628 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8629 | if (BitCastInst *BC = dyn_cast<BitCastInst>(Callee)) |
| 8630 | if (IntrinsicInst *In = dyn_cast<IntrinsicInst>(BC->getOperand(0))) |
| 8631 | if (In->getIntrinsicID() == Intrinsic::init_trampoline) |
| 8632 | return transformCallThroughTrampoline(CS); |
| 8633 | |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8634 | const PointerType *PTy = cast<PointerType>(Callee->getType()); |
| 8635 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
| 8636 | if (FTy->isVarArg()) { |
| 8637 | // See if we can optimize any arguments passed through the varargs area of |
| 8638 | // the call. |
| 8639 | for (CallSite::arg_iterator I = CS.arg_begin()+FTy->getNumParams(), |
| 8640 | E = CS.arg_end(); I != E; ++I) |
| 8641 | if (CastInst *CI = dyn_cast<CastInst>(*I)) { |
| 8642 | // If this cast does not effect the value passed through the varargs |
| 8643 | // area, we can eliminate the use of the cast. |
| 8644 | Value *Op = CI->getOperand(0); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8645 | if (CI->isLosslessCast()) { |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8646 | *I = Op; |
| 8647 | Changed = true; |
| 8648 | } |
| 8649 | } |
| 8650 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8651 | |
Duncan Sands | f0c3354 | 2007-12-19 21:13:37 +0000 | [diff] [blame] | 8652 | if (isa<InlineAsm>(Callee) && !CS.doesNotThrow()) { |
Duncan Sands | ece2c04 | 2007-12-16 15:51:49 +0000 | [diff] [blame] | 8653 | // Inline asm calls cannot throw - mark them 'nounwind'. |
Duncan Sands | f0c3354 | 2007-12-19 21:13:37 +0000 | [diff] [blame] | 8654 | CS.setDoesNotThrow(); |
Duncan Sands | ece2c04 | 2007-12-16 15:51:49 +0000 | [diff] [blame] | 8655 | Changed = true; |
| 8656 | } |
| 8657 | |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8658 | return Changed ? CS.getInstruction() : 0; |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 8659 | } |
| 8660 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8661 | // transformConstExprCastCall - If the callee is a constexpr cast of a function, |
| 8662 | // attempt to move the cast to the arguments of the call/invoke. |
| 8663 | // |
| 8664 | bool InstCombiner::transformConstExprCastCall(CallSite CS) { |
| 8665 | if (!isa<ConstantExpr>(CS.getCalledValue())) return false; |
| 8666 | ConstantExpr *CE = cast<ConstantExpr>(CS.getCalledValue()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8667 | if (CE->getOpcode() != Instruction::BitCast || |
| 8668 | !isa<Function>(CE->getOperand(0))) |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8669 | return false; |
Reid Spencer | 8863f18 | 2004-07-18 00:38:32 +0000 | [diff] [blame] | 8670 | Function *Callee = cast<Function>(CE->getOperand(0)); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8671 | Instruction *Caller = CS.getInstruction(); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8672 | const PAListPtr &CallerPAL = CS.getParamAttrs(); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8673 | |
| 8674 | // Okay, this is a cast from a function to a different type. Unless doing so |
| 8675 | // would cause a type conversion of one of our arguments, change this call to |
| 8676 | // be a direct call with arguments casted to the appropriate types. |
| 8677 | // |
| 8678 | const FunctionType *FT = Callee->getFunctionType(); |
| 8679 | const Type *OldRetTy = Caller->getType(); |
| 8680 | |
Devang Patel | 75e6f02 | 2008-03-11 18:04:06 +0000 | [diff] [blame] | 8681 | if (isa<StructType>(FT->getReturnType())) |
| 8682 | return false; // TODO: Handle multiple return values. |
| 8683 | |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 8684 | // Check to see if we are changing the return type... |
| 8685 | if (OldRetTy != FT->getReturnType()) { |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 8686 | if (Callee->isDeclaration() && !Caller->use_empty() && |
Chris Lattner | 46013f4 | 2007-01-06 19:53:32 +0000 | [diff] [blame] | 8687 | // Conversion is ok if changing from pointer to int of same size. |
| 8688 | !(isa<PointerType>(FT->getReturnType()) && |
| 8689 | TD->getIntPtrType() == OldRetTy)) |
Chris Lattner | ec47992 | 2007-01-06 02:09:32 +0000 | [diff] [blame] | 8690 | return false; // Cannot transform this return value. |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 8691 | |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8692 | if (!Caller->use_empty() && |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8693 | // void -> non-void is handled specially |
Duncan Sands | e1e520f | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 8694 | FT->getReturnType() != Type::VoidTy && |
| 8695 | !CastInst::isCastable(FT->getReturnType(), OldRetTy)) |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8696 | return false; // Cannot transform this return value. |
| 8697 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8698 | if (!CallerPAL.isEmpty() && !Caller->use_empty()) { |
| 8699 | ParameterAttributes RAttrs = CallerPAL.getParamAttrs(0); |
Duncan Sands | 6c3470e | 2008-01-07 17:16:06 +0000 | [diff] [blame] | 8700 | if (RAttrs & ParamAttr::typeIncompatible(FT->getReturnType())) |
| 8701 | return false; // Attribute not compatible with transformed value. |
| 8702 | } |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8703 | |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 8704 | // If the callsite is an invoke instruction, and the return value is used by |
| 8705 | // a PHI node in a successor, we cannot change the return type of the call |
| 8706 | // because there is no place to put the cast instruction (without breaking |
| 8707 | // the critical edge). Bail out in this case. |
| 8708 | if (!Caller->use_empty()) |
| 8709 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) |
| 8710 | for (Value::use_iterator UI = II->use_begin(), E = II->use_end(); |
| 8711 | UI != E; ++UI) |
| 8712 | if (PHINode *PN = dyn_cast<PHINode>(*UI)) |
| 8713 | if (PN->getParent() == II->getNormalDest() || |
Chris Lattner | aeb2a1d | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 8714 | PN->getParent() == II->getUnwindDest()) |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 8715 | return false; |
| 8716 | } |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8717 | |
| 8718 | unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin()); |
| 8719 | unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8720 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8721 | CallSite::arg_iterator AI = CS.arg_begin(); |
| 8722 | for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) { |
| 8723 | const Type *ParamTy = FT->getParamType(i); |
Andrew Lenharth | b8e604c | 2006-06-28 01:01:52 +0000 | [diff] [blame] | 8724 | const Type *ActTy = (*AI)->getType(); |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8725 | |
| 8726 | if (!CastInst::isCastable(ActTy, ParamTy)) |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8727 | return false; // Cannot transform this parameter value. |
| 8728 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8729 | if (CallerPAL.getParamAttrs(i + 1) & ParamAttr::typeIncompatible(ParamTy)) |
| 8730 | return false; // Attribute not compatible with transformed value. |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8731 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8732 | ConstantInt *c = dyn_cast<ConstantInt>(*AI); |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8733 | // Some conversions are safe even if we do not have a body. |
| 8734 | // Either we can cast directly, or we can upconvert the argument |
Chris Lattner | ec47992 | 2007-01-06 02:09:32 +0000 | [diff] [blame] | 8735 | bool isConvertible = ActTy == ParamTy || |
Chris Lattner | 46013f4 | 2007-01-06 19:53:32 +0000 | [diff] [blame] | 8736 | (isa<PointerType>(ParamTy) && isa<PointerType>(ActTy)) || |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 8737 | (ParamTy->isInteger() && ActTy->isInteger() && |
Reid Spencer | abaa8ca | 2007-01-08 16:32:00 +0000 | [diff] [blame] | 8738 | ParamTy->getPrimitiveSizeInBits() >= ActTy->getPrimitiveSizeInBits()) || |
| 8739 | (c && ParamTy->getPrimitiveSizeInBits() >= ActTy->getPrimitiveSizeInBits() |
Zhou Sheng | 0fc5095 | 2007-03-25 05:01:29 +0000 | [diff] [blame] | 8740 | && c->getValue().isStrictlyPositive()); |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 8741 | if (Callee->isDeclaration() && !isConvertible) return false; |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8742 | } |
| 8743 | |
| 8744 | if (FT->getNumParams() < NumActualArgs && !FT->isVarArg() && |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 8745 | Callee->isDeclaration()) |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8746 | return false; // Do not delete arguments unless we have a function body. |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8747 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8748 | if (FT->getNumParams() < NumActualArgs && FT->isVarArg() && |
| 8749 | !CallerPAL.isEmpty()) |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8750 | // 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] | 8751 | // won't be dropping them. Check that these extra arguments have attributes |
| 8752 | // that are compatible with being a vararg call argument. |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8753 | for (unsigned i = CallerPAL.getNumSlots(); i; --i) { |
| 8754 | if (CallerPAL.getSlot(i - 1).Index <= FT->getNumParams()) |
Duncan Sands | e1e520f | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 8755 | break; |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8756 | ParameterAttributes PAttrs = CallerPAL.getSlot(i - 1).Attrs; |
Duncan Sands | e1e520f | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 8757 | if (PAttrs & ParamAttr::VarArgsIncompatible) |
| 8758 | return false; |
| 8759 | } |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8760 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8761 | // Okay, we decided that this is a safe thing to do: go ahead and start |
| 8762 | // inserting cast instructions as necessary... |
| 8763 | std::vector<Value*> Args; |
| 8764 | Args.reserve(NumActualArgs); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8765 | SmallVector<ParamAttrsWithIndex, 8> attrVec; |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8766 | attrVec.reserve(NumCommonArgs); |
| 8767 | |
| 8768 | // Get any return attributes. |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8769 | ParameterAttributes RAttrs = CallerPAL.getParamAttrs(0); |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8770 | |
| 8771 | // If the return value is not being used, the type may not be compatible |
| 8772 | // with the existing attributes. Wipe out any problematic attributes. |
Duncan Sands | 6c3470e | 2008-01-07 17:16:06 +0000 | [diff] [blame] | 8773 | RAttrs &= ~ParamAttr::typeIncompatible(FT->getReturnType()); |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8774 | |
| 8775 | // Add the new return attributes. |
| 8776 | if (RAttrs) |
| 8777 | attrVec.push_back(ParamAttrsWithIndex::get(0, RAttrs)); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8778 | |
| 8779 | AI = CS.arg_begin(); |
| 8780 | for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) { |
| 8781 | const Type *ParamTy = FT->getParamType(i); |
| 8782 | if ((*AI)->getType() == ParamTy) { |
| 8783 | Args.push_back(*AI); |
| 8784 | } else { |
Reid Spencer | 8a903db | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 8785 | Instruction::CastOps opcode = CastInst::getCastOpcode(*AI, |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8786 | false, ParamTy, false); |
Reid Spencer | 8a903db | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 8787 | CastInst *NewCast = CastInst::create(opcode, *AI, ParamTy, "tmp"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8788 | Args.push_back(InsertNewInstBefore(NewCast, *Caller)); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8789 | } |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8790 | |
| 8791 | // Add any parameter attributes. |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8792 | if (ParameterAttributes PAttrs = CallerPAL.getParamAttrs(i + 1)) |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8793 | attrVec.push_back(ParamAttrsWithIndex::get(i + 1, PAttrs)); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8794 | } |
| 8795 | |
| 8796 | // If the function takes more arguments than the call was taking, add them |
| 8797 | // now... |
| 8798 | for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i) |
| 8799 | Args.push_back(Constant::getNullValue(FT->getParamType(i))); |
| 8800 | |
| 8801 | // If we are removing arguments to the function, emit an obnoxious warning... |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 8802 | if (FT->getNumParams() < NumActualArgs) { |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8803 | if (!FT->isVarArg()) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 8804 | cerr << "WARNING: While resolving call to function '" |
| 8805 | << Callee->getName() << "' arguments were dropped!\n"; |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8806 | } else { |
| 8807 | // Add all of the arguments in their promoted form to the arg list... |
| 8808 | for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) { |
| 8809 | const Type *PTy = getPromotedType((*AI)->getType()); |
| 8810 | if (PTy != (*AI)->getType()) { |
| 8811 | // Must promote to pass through va_arg area! |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8812 | Instruction::CastOps opcode = CastInst::getCastOpcode(*AI, false, |
| 8813 | PTy, false); |
Reid Spencer | 8a903db | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 8814 | Instruction *Cast = CastInst::create(opcode, *AI, PTy, "tmp"); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8815 | InsertNewInstBefore(Cast, *Caller); |
| 8816 | Args.push_back(Cast); |
| 8817 | } else { |
| 8818 | Args.push_back(*AI); |
| 8819 | } |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8820 | |
Duncan Sands | e1e520f | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 8821 | // Add any parameter attributes. |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8822 | if (ParameterAttributes PAttrs = CallerPAL.getParamAttrs(i + 1)) |
Duncan Sands | e1e520f | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 8823 | attrVec.push_back(ParamAttrsWithIndex::get(i + 1, PAttrs)); |
| 8824 | } |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8825 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 8826 | } |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8827 | |
| 8828 | if (FT->getReturnType() == Type::VoidTy) |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 8829 | Caller->setName(""); // Void type should not have a name. |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8830 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8831 | const PAListPtr &NewCallerPAL = PAListPtr::get(attrVec.begin(),attrVec.end()); |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8832 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8833 | Instruction *NC; |
| 8834 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 8835 | NC = InvokeInst::Create(Callee, II->getNormalDest(), II->getUnwindDest(), |
| 8836 | Args.begin(), Args.end(), Caller->getName(), Caller); |
Reid Spencer | ed3fa85 | 2007-07-30 19:53:57 +0000 | [diff] [blame] | 8837 | cast<InvokeInst>(NC)->setCallingConv(II->getCallingConv()); |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8838 | cast<InvokeInst>(NC)->setParamAttrs(NewCallerPAL); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8839 | } else { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 8840 | NC = CallInst::Create(Callee, Args.begin(), Args.end(), |
| 8841 | Caller->getName(), Caller); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 8842 | CallInst *CI = cast<CallInst>(Caller); |
| 8843 | if (CI->isTailCall()) |
Chris Lattner | a9e9211 | 2005-05-06 06:48:21 +0000 | [diff] [blame] | 8844 | cast<CallInst>(NC)->setTailCall(); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 8845 | cast<CallInst>(NC)->setCallingConv(CI->getCallingConv()); |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8846 | cast<CallInst>(NC)->setParamAttrs(NewCallerPAL); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8847 | } |
| 8848 | |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 8849 | // Insert a cast of the return type as necessary. |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8850 | Value *NV = NC; |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8851 | if (OldRetTy != NV->getType() && !Caller->use_empty()) { |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8852 | if (NV->getType() != Type::VoidTy) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8853 | Instruction::CastOps opcode = CastInst::getCastOpcode(NC, false, |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8854 | OldRetTy, false); |
| 8855 | NV = NC = CastInst::create(opcode, NC, OldRetTy, "tmp"); |
Chris Lattner | bb60904 | 2003-10-30 00:46:41 +0000 | [diff] [blame] | 8856 | |
| 8857 | // If this is an invoke instruction, we should insert it after the first |
| 8858 | // non-phi, instruction in the normal successor block. |
| 8859 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
| 8860 | BasicBlock::iterator I = II->getNormalDest()->begin(); |
| 8861 | while (isa<PHINode>(I)) ++I; |
| 8862 | InsertNewInstBefore(NC, *I); |
| 8863 | } else { |
| 8864 | // Otherwise, it's a call, just insert cast right after the call instr |
| 8865 | InsertNewInstBefore(NC, *Caller); |
| 8866 | } |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 8867 | AddUsersToWorkList(*Caller); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8868 | } else { |
Chris Lattner | c30bda7 | 2004-10-17 21:22:38 +0000 | [diff] [blame] | 8869 | NV = UndefValue::get(Caller->getType()); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8870 | } |
| 8871 | } |
| 8872 | |
| 8873 | if (Caller->getType() != Type::VoidTy && !Caller->use_empty()) |
| 8874 | Caller->replaceAllUsesWith(NV); |
Chris Lattner | f22a5c6 | 2007-03-02 19:59:19 +0000 | [diff] [blame] | 8875 | Caller->eraseFromParent(); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 8876 | RemoveFromWorkList(Caller); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8877 | return true; |
| 8878 | } |
| 8879 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8880 | // transformCallThroughTrampoline - Turn a call to a function created by the |
| 8881 | // init_trampoline intrinsic into a direct call to the underlying function. |
| 8882 | // |
| 8883 | Instruction *InstCombiner::transformCallThroughTrampoline(CallSite CS) { |
| 8884 | Value *Callee = CS.getCalledValue(); |
| 8885 | const PointerType *PTy = cast<PointerType>(Callee->getType()); |
| 8886 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8887 | const PAListPtr &Attrs = CS.getParamAttrs(); |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 8888 | |
| 8889 | // If the call already has the 'nest' attribute somewhere then give up - |
| 8890 | // otherwise 'nest' would occur twice after splicing in the chain. |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8891 | if (Attrs.hasAttrSomewhere(ParamAttr::Nest)) |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 8892 | return 0; |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8893 | |
| 8894 | IntrinsicInst *Tramp = |
| 8895 | cast<IntrinsicInst>(cast<BitCastInst>(Callee)->getOperand(0)); |
| 8896 | |
| 8897 | Function *NestF = |
| 8898 | cast<Function>(IntrinsicInst::StripPointerCasts(Tramp->getOperand(2))); |
| 8899 | const PointerType *NestFPTy = cast<PointerType>(NestF->getType()); |
| 8900 | const FunctionType *NestFTy = cast<FunctionType>(NestFPTy->getElementType()); |
| 8901 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8902 | const PAListPtr &NestAttrs = NestF->getParamAttrs(); |
| 8903 | if (!NestAttrs.isEmpty()) { |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8904 | unsigned NestIdx = 1; |
| 8905 | const Type *NestTy = 0; |
Dale Johannesen | 0d51e7e | 2008-02-19 21:38:47 +0000 | [diff] [blame] | 8906 | ParameterAttributes NestAttr = ParamAttr::None; |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8907 | |
| 8908 | // Look for a parameter marked with the 'nest' attribute. |
| 8909 | for (FunctionType::param_iterator I = NestFTy->param_begin(), |
| 8910 | E = NestFTy->param_end(); I != E; ++NestIdx, ++I) |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8911 | if (NestAttrs.paramHasAttr(NestIdx, ParamAttr::Nest)) { |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8912 | // Record the parameter type and any other attributes. |
| 8913 | NestTy = *I; |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8914 | NestAttr = NestAttrs.getParamAttrs(NestIdx); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8915 | break; |
| 8916 | } |
| 8917 | |
| 8918 | if (NestTy) { |
| 8919 | Instruction *Caller = CS.getInstruction(); |
| 8920 | std::vector<Value*> NewArgs; |
| 8921 | NewArgs.reserve(unsigned(CS.arg_end()-CS.arg_begin())+1); |
| 8922 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8923 | SmallVector<ParamAttrsWithIndex, 8> NewAttrs; |
| 8924 | NewAttrs.reserve(Attrs.getNumSlots() + 1); |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 8925 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8926 | // Insert the nest argument into the call argument list, which may |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 8927 | // mean appending it. Likewise for attributes. |
| 8928 | |
| 8929 | // Add any function result attributes. |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8930 | if (ParameterAttributes Attr = Attrs.getParamAttrs(0)) |
| 8931 | NewAttrs.push_back(ParamAttrsWithIndex::get(0, Attr)); |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 8932 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8933 | { |
| 8934 | unsigned Idx = 1; |
| 8935 | CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); |
| 8936 | do { |
| 8937 | if (Idx == NestIdx) { |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 8938 | // Add the chain argument and attributes. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8939 | Value *NestVal = Tramp->getOperand(3); |
| 8940 | if (NestVal->getType() != NestTy) |
| 8941 | NestVal = new BitCastInst(NestVal, NestTy, "nest", Caller); |
| 8942 | NewArgs.push_back(NestVal); |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 8943 | NewAttrs.push_back(ParamAttrsWithIndex::get(NestIdx, NestAttr)); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8944 | } |
| 8945 | |
| 8946 | if (I == E) |
| 8947 | break; |
| 8948 | |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 8949 | // Add the original argument and attributes. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8950 | NewArgs.push_back(*I); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8951 | if (ParameterAttributes Attr = Attrs.getParamAttrs(Idx)) |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 8952 | NewAttrs.push_back |
| 8953 | (ParamAttrsWithIndex::get(Idx + (Idx >= NestIdx), Attr)); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8954 | |
| 8955 | ++Idx, ++I; |
| 8956 | } while (1); |
| 8957 | } |
| 8958 | |
| 8959 | // The trampoline may have been bitcast to a bogus type (FTy). |
| 8960 | // Handle this by synthesizing a new function type, equal to FTy |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 8961 | // with the chain parameter inserted. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8962 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8963 | std::vector<const Type*> NewTypes; |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8964 | NewTypes.reserve(FTy->getNumParams()+1); |
| 8965 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8966 | // 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] | 8967 | // mean appending it. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8968 | { |
| 8969 | unsigned Idx = 1; |
| 8970 | FunctionType::param_iterator I = FTy->param_begin(), |
| 8971 | E = FTy->param_end(); |
| 8972 | |
| 8973 | do { |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 8974 | if (Idx == NestIdx) |
| 8975 | // Add the chain's type. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8976 | NewTypes.push_back(NestTy); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8977 | |
| 8978 | if (I == E) |
| 8979 | break; |
| 8980 | |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 8981 | // Add the original type. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8982 | NewTypes.push_back(*I); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8983 | |
| 8984 | ++Idx, ++I; |
| 8985 | } while (1); |
| 8986 | } |
| 8987 | |
| 8988 | // Replace the trampoline call with a direct call. Let the generic |
| 8989 | // code sort out any function type mismatches. |
| 8990 | FunctionType *NewFTy = |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 8991 | FunctionType::get(FTy->getReturnType(), NewTypes, FTy->isVarArg()); |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 8992 | Constant *NewCallee = NestF->getType() == PointerType::getUnqual(NewFTy) ? |
| 8993 | NestF : ConstantExpr::getBitCast(NestF, PointerType::getUnqual(NewFTy)); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8994 | const PAListPtr &NewPAL = PAListPtr::get(NewAttrs.begin(),NewAttrs.end()); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8995 | |
| 8996 | Instruction *NewCaller; |
| 8997 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 8998 | NewCaller = InvokeInst::Create(NewCallee, |
| 8999 | II->getNormalDest(), II->getUnwindDest(), |
| 9000 | NewArgs.begin(), NewArgs.end(), |
| 9001 | Caller->getName(), Caller); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9002 | cast<InvokeInst>(NewCaller)->setCallingConv(II->getCallingConv()); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 9003 | cast<InvokeInst>(NewCaller)->setParamAttrs(NewPAL); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9004 | } else { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9005 | NewCaller = CallInst::Create(NewCallee, NewArgs.begin(), NewArgs.end(), |
| 9006 | Caller->getName(), Caller); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9007 | if (cast<CallInst>(Caller)->isTailCall()) |
| 9008 | cast<CallInst>(NewCaller)->setTailCall(); |
| 9009 | cast<CallInst>(NewCaller)-> |
| 9010 | setCallingConv(cast<CallInst>(Caller)->getCallingConv()); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 9011 | cast<CallInst>(NewCaller)->setParamAttrs(NewPAL); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9012 | } |
| 9013 | if (Caller->getType() != Type::VoidTy && !Caller->use_empty()) |
| 9014 | Caller->replaceAllUsesWith(NewCaller); |
| 9015 | Caller->eraseFromParent(); |
| 9016 | RemoveFromWorkList(Caller); |
| 9017 | return 0; |
| 9018 | } |
| 9019 | } |
| 9020 | |
| 9021 | // Replace the trampoline call with a direct call. Since there is no 'nest' |
| 9022 | // parameter, there is no need to adjust the argument list. Let the generic |
| 9023 | // code sort out any function type mismatches. |
| 9024 | Constant *NewCallee = |
| 9025 | NestF->getType() == PTy ? NestF : ConstantExpr::getBitCast(NestF, PTy); |
| 9026 | CS.setCalledFunction(NewCallee); |
| 9027 | return CS.getInstruction(); |
| 9028 | } |
| 9029 | |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9030 | /// FoldPHIArgBinOpIntoPHI - If we have something like phi [add (a,b), add(c,d)] |
| 9031 | /// and if a/b/c/d and the add's all have a single use, turn this into two phi's |
| 9032 | /// and a single binop. |
| 9033 | Instruction *InstCombiner::FoldPHIArgBinOpIntoPHI(PHINode &PN) { |
| 9034 | Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0)); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 9035 | assert(isa<BinaryOperator>(FirstInst) || isa<GetElementPtrInst>(FirstInst) || |
| 9036 | isa<CmpInst>(FirstInst)); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9037 | unsigned Opc = FirstInst->getOpcode(); |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 9038 | Value *LHSVal = FirstInst->getOperand(0); |
| 9039 | Value *RHSVal = FirstInst->getOperand(1); |
| 9040 | |
| 9041 | const Type *LHSType = LHSVal->getType(); |
| 9042 | const Type *RHSType = RHSVal->getType(); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9043 | |
| 9044 | // Scan to see if all operands are the same opcode, all have one use, and all |
| 9045 | // kill their operands (i.e. the operands have one use). |
Chris Lattner | a90a24c | 2006-11-01 04:55:47 +0000 | [diff] [blame] | 9046 | for (unsigned i = 0; i != PN.getNumIncomingValues(); ++i) { |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9047 | Instruction *I = dyn_cast<Instruction>(PN.getIncomingValue(i)); |
Chris Lattner | a90a24c | 2006-11-01 04:55:47 +0000 | [diff] [blame] | 9048 | if (!I || I->getOpcode() != Opc || !I->hasOneUse() || |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9049 | // 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] | 9050 | // types or GEP's with different index types. |
| 9051 | I->getOperand(0)->getType() != LHSType || |
| 9052 | I->getOperand(1)->getType() != RHSType) |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9053 | return 0; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9054 | |
| 9055 | // If they are CmpInst instructions, check their predicates |
| 9056 | if (Opc == Instruction::ICmp || Opc == Instruction::FCmp) |
| 9057 | if (cast<CmpInst>(I)->getPredicate() != |
| 9058 | cast<CmpInst>(FirstInst)->getPredicate()) |
| 9059 | return 0; |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 9060 | |
| 9061 | // Keep track of which operand needs a phi node. |
| 9062 | if (I->getOperand(0) != LHSVal) LHSVal = 0; |
| 9063 | if (I->getOperand(1) != RHSVal) RHSVal = 0; |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9064 | } |
| 9065 | |
Chris Lattner | 53738a4 | 2006-11-08 19:42:28 +0000 | [diff] [blame] | 9066 | // Otherwise, this is safe to transform, determine if it is profitable. |
| 9067 | |
| 9068 | // If this is a GEP, and if the index (not the pointer) needs a PHI, bail out. |
| 9069 | // Indexes are often folded into load/store instructions, so we don't want to |
| 9070 | // hide them behind a phi. |
| 9071 | if (isa<GetElementPtrInst>(FirstInst) && RHSVal == 0) |
| 9072 | return 0; |
| 9073 | |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9074 | Value *InLHS = FirstInst->getOperand(0); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9075 | Value *InRHS = FirstInst->getOperand(1); |
Chris Lattner | 53738a4 | 2006-11-08 19:42:28 +0000 | [diff] [blame] | 9076 | PHINode *NewLHS = 0, *NewRHS = 0; |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 9077 | if (LHSVal == 0) { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9078 | NewLHS = PHINode::Create(LHSType, FirstInst->getOperand(0)->getName()+".pn"); |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 9079 | NewLHS->reserveOperandSpace(PN.getNumOperands()/2); |
| 9080 | NewLHS->addIncoming(InLHS, PN.getIncomingBlock(0)); |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 9081 | InsertNewInstBefore(NewLHS, PN); |
| 9082 | LHSVal = NewLHS; |
| 9083 | } |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 9084 | |
| 9085 | if (RHSVal == 0) { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9086 | NewRHS = PHINode::Create(RHSType, FirstInst->getOperand(1)->getName()+".pn"); |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 9087 | NewRHS->reserveOperandSpace(PN.getNumOperands()/2); |
| 9088 | NewRHS->addIncoming(InRHS, PN.getIncomingBlock(0)); |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 9089 | InsertNewInstBefore(NewRHS, PN); |
| 9090 | RHSVal = NewRHS; |
| 9091 | } |
| 9092 | |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 9093 | // Add all operands to the new PHIs. |
| 9094 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 9095 | if (NewLHS) { |
| 9096 | Value *NewInLHS =cast<Instruction>(PN.getIncomingValue(i))->getOperand(0); |
| 9097 | NewLHS->addIncoming(NewInLHS, PN.getIncomingBlock(i)); |
| 9098 | } |
| 9099 | if (NewRHS) { |
| 9100 | Value *NewInRHS =cast<Instruction>(PN.getIncomingValue(i))->getOperand(1); |
| 9101 | NewRHS->addIncoming(NewInRHS, PN.getIncomingBlock(i)); |
| 9102 | } |
| 9103 | } |
| 9104 | |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9105 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 9106 | return BinaryOperator::create(BinOp->getOpcode(), LHSVal, RHSVal); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9107 | else if (CmpInst *CIOp = dyn_cast<CmpInst>(FirstInst)) |
| 9108 | return CmpInst::create(CIOp->getOpcode(), CIOp->getPredicate(), LHSVal, |
| 9109 | RHSVal); |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 9110 | else { |
| 9111 | assert(isa<GetElementPtrInst>(FirstInst)); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9112 | return GetElementPtrInst::Create(LHSVal, RHSVal); |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 9113 | } |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9114 | } |
| 9115 | |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 9116 | /// isSafeToSinkLoad - Return true if we know that it is safe sink the load out |
| 9117 | /// of the block that defines it. This means that it must be obvious the value |
| 9118 | /// of the load is not changed from the point of the load to the end of the |
| 9119 | /// block it is in. |
Chris Lattner | fd905ca | 2007-02-01 22:30:07 +0000 | [diff] [blame] | 9120 | /// |
| 9121 | /// Finally, it is safe, but not profitable, to sink a load targetting a |
| 9122 | /// non-address-taken alloca. Doing so will cause us to not promote the alloca |
| 9123 | /// to a register. |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 9124 | static bool isSafeToSinkLoad(LoadInst *L) { |
| 9125 | BasicBlock::iterator BBI = L, E = L->getParent()->end(); |
| 9126 | |
| 9127 | for (++BBI; BBI != E; ++BBI) |
| 9128 | if (BBI->mayWriteToMemory()) |
| 9129 | return false; |
Chris Lattner | fd905ca | 2007-02-01 22:30:07 +0000 | [diff] [blame] | 9130 | |
| 9131 | // Check for non-address taken alloca. If not address-taken already, it isn't |
| 9132 | // profitable to do this xform. |
| 9133 | if (AllocaInst *AI = dyn_cast<AllocaInst>(L->getOperand(0))) { |
| 9134 | bool isAddressTaken = false; |
| 9135 | for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); |
| 9136 | UI != E; ++UI) { |
| 9137 | if (isa<LoadInst>(UI)) continue; |
| 9138 | if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) { |
| 9139 | // If storing TO the alloca, then the address isn't taken. |
| 9140 | if (SI->getOperand(1) == AI) continue; |
| 9141 | } |
| 9142 | isAddressTaken = true; |
| 9143 | break; |
| 9144 | } |
| 9145 | |
| 9146 | if (!isAddressTaken) |
| 9147 | return false; |
| 9148 | } |
| 9149 | |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 9150 | return true; |
| 9151 | } |
| 9152 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9153 | |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9154 | // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary" |
| 9155 | // operator and they all are only used by the PHI, PHI together their |
| 9156 | // inputs, and do the operation once, to the result of the PHI. |
| 9157 | Instruction *InstCombiner::FoldPHIArgOpIntoPHI(PHINode &PN) { |
| 9158 | Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0)); |
| 9159 | |
| 9160 | // Scan the instruction, looking for input operations that can be folded away. |
| 9161 | // If all input operands to the phi are the same instruction (e.g. a cast from |
| 9162 | // the same type or "+42") we can pull the operation through the PHI, reducing |
| 9163 | // code size and simplifying code. |
| 9164 | Constant *ConstantOp = 0; |
| 9165 | const Type *CastSrcTy = 0; |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 9166 | bool isVolatile = false; |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9167 | if (isa<CastInst>(FirstInst)) { |
| 9168 | CastSrcTy = FirstInst->getOperand(0)->getType(); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 9169 | } else if (isa<BinaryOperator>(FirstInst) || isa<CmpInst>(FirstInst)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9170 | // Can fold binop, compare or shift here if the RHS is a constant, |
| 9171 | // otherwise call FoldPHIArgBinOpIntoPHI. |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9172 | ConstantOp = dyn_cast<Constant>(FirstInst->getOperand(1)); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9173 | if (ConstantOp == 0) |
| 9174 | return FoldPHIArgBinOpIntoPHI(PN); |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 9175 | } else if (LoadInst *LI = dyn_cast<LoadInst>(FirstInst)) { |
| 9176 | isVolatile = LI->isVolatile(); |
| 9177 | // We can't sink the load if the loaded value could be modified between the |
| 9178 | // load and the PHI. |
| 9179 | if (LI->getParent() != PN.getIncomingBlock(0) || |
| 9180 | !isSafeToSinkLoad(LI)) |
| 9181 | return 0; |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 9182 | } else if (isa<GetElementPtrInst>(FirstInst)) { |
Chris Lattner | 53738a4 | 2006-11-08 19:42:28 +0000 | [diff] [blame] | 9183 | if (FirstInst->getNumOperands() == 2) |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 9184 | return FoldPHIArgBinOpIntoPHI(PN); |
| 9185 | // Can't handle general GEPs yet. |
| 9186 | return 0; |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9187 | } else { |
| 9188 | return 0; // Cannot fold this operation. |
| 9189 | } |
| 9190 | |
| 9191 | // Check to see if all arguments are the same operation. |
| 9192 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 9193 | if (!isa<Instruction>(PN.getIncomingValue(i))) return 0; |
| 9194 | Instruction *I = cast<Instruction>(PN.getIncomingValue(i)); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9195 | if (!I->hasOneUse() || !I->isSameOperationAs(FirstInst)) |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9196 | return 0; |
| 9197 | if (CastSrcTy) { |
| 9198 | if (I->getOperand(0)->getType() != CastSrcTy) |
| 9199 | return 0; // Cast operation must match. |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 9200 | } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9201 | // We can't sink the load if the loaded value could be modified between |
| 9202 | // the load and the PHI. |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 9203 | if (LI->isVolatile() != isVolatile || |
| 9204 | LI->getParent() != PN.getIncomingBlock(i) || |
| 9205 | !isSafeToSinkLoad(LI)) |
| 9206 | return 0; |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9207 | } else if (I->getOperand(1) != ConstantOp) { |
| 9208 | return 0; |
| 9209 | } |
| 9210 | } |
| 9211 | |
| 9212 | // Okay, they are all the same operation. Create a new PHI node of the |
| 9213 | // correct type, and PHI together all of the LHS's of the instructions. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9214 | PHINode *NewPN = PHINode::Create(FirstInst->getOperand(0)->getType(), |
| 9215 | PN.getName()+".in"); |
Chris Lattner | 5551706 | 2005-01-29 00:39:08 +0000 | [diff] [blame] | 9216 | NewPN->reserveOperandSpace(PN.getNumOperands()/2); |
Chris Lattner | b589344 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 9217 | |
| 9218 | Value *InVal = FirstInst->getOperand(0); |
| 9219 | NewPN->addIncoming(InVal, PN.getIncomingBlock(0)); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9220 | |
| 9221 | // Add all operands to the new PHI. |
Chris Lattner | b589344 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 9222 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 9223 | Value *NewInVal = cast<Instruction>(PN.getIncomingValue(i))->getOperand(0); |
| 9224 | if (NewInVal != InVal) |
| 9225 | InVal = 0; |
| 9226 | NewPN->addIncoming(NewInVal, PN.getIncomingBlock(i)); |
| 9227 | } |
| 9228 | |
| 9229 | Value *PhiVal; |
| 9230 | if (InVal) { |
| 9231 | // The new PHI unions all of the same values together. This is really |
| 9232 | // common, so we handle it intelligently here for compile-time speed. |
| 9233 | PhiVal = InVal; |
| 9234 | delete NewPN; |
| 9235 | } else { |
| 9236 | InsertNewInstBefore(NewPN, PN); |
| 9237 | PhiVal = NewPN; |
| 9238 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9239 | |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9240 | // Insert and return the new operation. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9241 | if (CastInst* FirstCI = dyn_cast<CastInst>(FirstInst)) |
| 9242 | return CastInst::create(FirstCI->getOpcode(), PhiVal, PN.getType()); |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 9243 | else if (isa<LoadInst>(FirstInst)) |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 9244 | return new LoadInst(PhiVal, "", isVolatile); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9245 | else if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) |
Chris Lattner | b589344 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 9246 | return BinaryOperator::create(BinOp->getOpcode(), PhiVal, ConstantOp); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9247 | else if (CmpInst *CIOp = dyn_cast<CmpInst>(FirstInst)) |
| 9248 | return CmpInst::create(CIOp->getOpcode(), CIOp->getPredicate(), |
| 9249 | PhiVal, ConstantOp); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9250 | else |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 9251 | assert(0 && "Unknown operation"); |
Jeff Cohen | ca5183d | 2007-03-05 00:00:42 +0000 | [diff] [blame] | 9252 | return 0; |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9253 | } |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 9254 | |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 9255 | /// DeadPHICycle - Return true if this PHI node is only used by a PHI node cycle |
| 9256 | /// that is dead. |
Chris Lattner | 0e5444b | 2007-03-26 20:40:50 +0000 | [diff] [blame] | 9257 | static bool DeadPHICycle(PHINode *PN, |
| 9258 | SmallPtrSet<PHINode*, 16> &PotentiallyDeadPHIs) { |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 9259 | if (PN->use_empty()) return true; |
| 9260 | if (!PN->hasOneUse()) return false; |
| 9261 | |
| 9262 | // Remember this node, and if we find the cycle, return. |
Chris Lattner | 0e5444b | 2007-03-26 20:40:50 +0000 | [diff] [blame] | 9263 | if (!PotentiallyDeadPHIs.insert(PN)) |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 9264 | return true; |
Chris Lattner | 92103de | 2007-08-28 04:23:55 +0000 | [diff] [blame] | 9265 | |
| 9266 | // Don't scan crazily complex things. |
| 9267 | if (PotentiallyDeadPHIs.size() == 16) |
| 9268 | return false; |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 9269 | |
| 9270 | if (PHINode *PU = dyn_cast<PHINode>(PN->use_back())) |
| 9271 | return DeadPHICycle(PU, PotentiallyDeadPHIs); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9272 | |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 9273 | return false; |
| 9274 | } |
| 9275 | |
Chris Lattner | cf5008a | 2007-11-06 21:52:06 +0000 | [diff] [blame] | 9276 | /// PHIsEqualValue - Return true if this phi node is always equal to |
| 9277 | /// NonPhiInVal. This happens with mutually cyclic phi nodes like: |
| 9278 | /// z = some value; x = phi (y, z); y = phi (x, z) |
| 9279 | static bool PHIsEqualValue(PHINode *PN, Value *NonPhiInVal, |
| 9280 | SmallPtrSet<PHINode*, 16> &ValueEqualPHIs) { |
| 9281 | // See if we already saw this PHI node. |
| 9282 | if (!ValueEqualPHIs.insert(PN)) |
| 9283 | return true; |
| 9284 | |
| 9285 | // Don't scan crazily complex things. |
| 9286 | if (ValueEqualPHIs.size() == 16) |
| 9287 | return false; |
| 9288 | |
| 9289 | // Scan the operands to see if they are either phi nodes or are equal to |
| 9290 | // the value. |
| 9291 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 9292 | Value *Op = PN->getIncomingValue(i); |
| 9293 | if (PHINode *OpPN = dyn_cast<PHINode>(Op)) { |
| 9294 | if (!PHIsEqualValue(OpPN, NonPhiInVal, ValueEqualPHIs)) |
| 9295 | return false; |
| 9296 | } else if (Op != NonPhiInVal) |
| 9297 | return false; |
| 9298 | } |
| 9299 | |
| 9300 | return true; |
| 9301 | } |
| 9302 | |
| 9303 | |
Chris Lattner | 473945d | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 9304 | // PHINode simplification |
| 9305 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 9306 | Instruction *InstCombiner::visitPHINode(PHINode &PN) { |
Owen Anderson | b64ab87 | 2006-07-10 22:15:25 +0000 | [diff] [blame] | 9307 | // If LCSSA is around, don't mess with Phi nodes |
Chris Lattner | f964f32 | 2007-03-04 04:27:24 +0000 | [diff] [blame] | 9308 | if (MustPreserveLCSSA) return 0; |
Owen Anderson | d1b78a1 | 2006-07-10 19:03:49 +0000 | [diff] [blame] | 9309 | |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 9310 | if (Value *V = PN.hasConstantValue()) |
| 9311 | return ReplaceInstUsesWith(PN, V); |
| 9312 | |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 9313 | // If all PHI operands are the same operation, pull them through the PHI, |
| 9314 | // reducing code size. |
| 9315 | if (isa<Instruction>(PN.getIncomingValue(0)) && |
| 9316 | PN.getIncomingValue(0)->hasOneUse()) |
| 9317 | if (Instruction *Result = FoldPHIArgOpIntoPHI(PN)) |
| 9318 | return Result; |
| 9319 | |
| 9320 | // If this is a trivial cycle in the PHI node graph, remove it. Basically, if |
| 9321 | // this PHI only has a single use (a PHI), and if that PHI only has one use (a |
| 9322 | // PHI)... break the cycle. |
Chris Lattner | ff9f13a | 2007-01-15 07:30:06 +0000 | [diff] [blame] | 9323 | if (PN.hasOneUse()) { |
| 9324 | Instruction *PHIUser = cast<Instruction>(PN.use_back()); |
| 9325 | if (PHINode *PU = dyn_cast<PHINode>(PHIUser)) { |
Chris Lattner | 0e5444b | 2007-03-26 20:40:50 +0000 | [diff] [blame] | 9326 | SmallPtrSet<PHINode*, 16> PotentiallyDeadPHIs; |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 9327 | PotentiallyDeadPHIs.insert(&PN); |
| 9328 | if (DeadPHICycle(PU, PotentiallyDeadPHIs)) |
| 9329 | return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType())); |
| 9330 | } |
Chris Lattner | ff9f13a | 2007-01-15 07:30:06 +0000 | [diff] [blame] | 9331 | |
| 9332 | // If this phi has a single use, and if that use just computes a value for |
| 9333 | // the next iteration of a loop, delete the phi. This occurs with unused |
| 9334 | // induction variables, e.g. "for (int j = 0; ; ++j);". Detecting this |
| 9335 | // common case here is good because the only other things that catch this |
| 9336 | // are induction variable analysis (sometimes) and ADCE, which is only run |
| 9337 | // late. |
| 9338 | if (PHIUser->hasOneUse() && |
| 9339 | (isa<BinaryOperator>(PHIUser) || isa<GetElementPtrInst>(PHIUser)) && |
| 9340 | PHIUser->use_back() == &PN) { |
| 9341 | return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType())); |
| 9342 | } |
| 9343 | } |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 9344 | |
Chris Lattner | cf5008a | 2007-11-06 21:52:06 +0000 | [diff] [blame] | 9345 | // We sometimes end up with phi cycles that non-obviously end up being the |
| 9346 | // same value, for example: |
| 9347 | // z = some value; x = phi (y, z); y = phi (x, z) |
| 9348 | // where the phi nodes don't necessarily need to be in the same block. Do a |
| 9349 | // quick check to see if the PHI node only contains a single non-phi value, if |
| 9350 | // so, scan to see if the phi cycle is actually equal to that value. |
| 9351 | { |
| 9352 | unsigned InValNo = 0, NumOperandVals = PN.getNumIncomingValues(); |
| 9353 | // Scan for the first non-phi operand. |
| 9354 | while (InValNo != NumOperandVals && |
| 9355 | isa<PHINode>(PN.getIncomingValue(InValNo))) |
| 9356 | ++InValNo; |
| 9357 | |
| 9358 | if (InValNo != NumOperandVals) { |
| 9359 | Value *NonPhiInVal = PN.getOperand(InValNo); |
| 9360 | |
| 9361 | // Scan the rest of the operands to see if there are any conflicts, if so |
| 9362 | // there is no need to recursively scan other phis. |
| 9363 | for (++InValNo; InValNo != NumOperandVals; ++InValNo) { |
| 9364 | Value *OpVal = PN.getIncomingValue(InValNo); |
| 9365 | if (OpVal != NonPhiInVal && !isa<PHINode>(OpVal)) |
| 9366 | break; |
| 9367 | } |
| 9368 | |
| 9369 | // If we scanned over all operands, then we have one unique value plus |
| 9370 | // phi values. Scan PHI nodes to see if they all merge in each other or |
| 9371 | // the value. |
| 9372 | if (InValNo == NumOperandVals) { |
| 9373 | SmallPtrSet<PHINode*, 16> ValueEqualPHIs; |
| 9374 | if (PHIsEqualValue(&PN, NonPhiInVal, ValueEqualPHIs)) |
| 9375 | return ReplaceInstUsesWith(PN, NonPhiInVal); |
| 9376 | } |
| 9377 | } |
| 9378 | } |
Chris Lattner | 60921c9 | 2003-12-19 05:58:40 +0000 | [diff] [blame] | 9379 | return 0; |
Chris Lattner | 473945d | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 9380 | } |
| 9381 | |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9382 | static Value *InsertCastToIntPtrTy(Value *V, const Type *DTy, |
| 9383 | Instruction *InsertPoint, |
| 9384 | InstCombiner *IC) { |
Reid Spencer | abaa8ca | 2007-01-08 16:32:00 +0000 | [diff] [blame] | 9385 | unsigned PtrSize = DTy->getPrimitiveSizeInBits(); |
| 9386 | unsigned VTySize = V->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9387 | // We must cast correctly to the pointer type. Ensure that we |
| 9388 | // sign extend the integer value if it is smaller as this is |
| 9389 | // used for address computation. |
| 9390 | Instruction::CastOps opcode = |
| 9391 | (VTySize < PtrSize ? Instruction::SExt : |
| 9392 | (VTySize == PtrSize ? Instruction::BitCast : Instruction::Trunc)); |
| 9393 | return IC->InsertCastBefore(opcode, V, DTy, *InsertPoint); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9394 | } |
| 9395 | |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 9396 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 9397 | Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9398 | Value *PtrOp = GEP.getOperand(0); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 9399 | // Is it 'getelementptr %P, i32 0' or 'getelementptr %P' |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 9400 | // If so, eliminate the noop. |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 9401 | if (GEP.getNumOperands() == 1) |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9402 | return ReplaceInstUsesWith(GEP, PtrOp); |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 9403 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 9404 | if (isa<UndefValue>(GEP.getOperand(0))) |
| 9405 | return ReplaceInstUsesWith(GEP, UndefValue::get(GEP.getType())); |
| 9406 | |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 9407 | bool HasZeroPointerIndex = false; |
| 9408 | if (Constant *C = dyn_cast<Constant>(GEP.getOperand(1))) |
| 9409 | HasZeroPointerIndex = C->isNullValue(); |
| 9410 | |
| 9411 | if (GEP.getNumOperands() == 2 && HasZeroPointerIndex) |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9412 | return ReplaceInstUsesWith(GEP, PtrOp); |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 9413 | |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9414 | // Eliminate unneeded casts for indices. |
| 9415 | bool MadeChange = false; |
Chris Lattner | db9654e | 2007-03-25 20:43:09 +0000 | [diff] [blame] | 9416 | |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 9417 | gep_type_iterator GTI = gep_type_begin(GEP); |
Chris Lattner | db9654e | 2007-03-25 20:43:09 +0000 | [diff] [blame] | 9418 | for (unsigned i = 1, e = GEP.getNumOperands(); i != e; ++i, ++GTI) { |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 9419 | if (isa<SequentialType>(*GTI)) { |
| 9420 | if (CastInst *CI = dyn_cast<CastInst>(GEP.getOperand(i))) { |
Chris Lattner | 76b7a06 | 2007-01-15 07:02:54 +0000 | [diff] [blame] | 9421 | if (CI->getOpcode() == Instruction::ZExt || |
| 9422 | CI->getOpcode() == Instruction::SExt) { |
| 9423 | const Type *SrcTy = CI->getOperand(0)->getType(); |
| 9424 | // We can eliminate a cast from i32 to i64 iff the target |
| 9425 | // is a 32-bit pointer target. |
| 9426 | if (SrcTy->getPrimitiveSizeInBits() >= TD->getPointerSizeInBits()) { |
| 9427 | MadeChange = true; |
| 9428 | GEP.setOperand(i, CI->getOperand(0)); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9429 | } |
| 9430 | } |
| 9431 | } |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 9432 | // If we are using a wider index than needed for this platform, shrink it |
| 9433 | // to what we need. If the incoming value needs a cast instruction, |
| 9434 | // insert it. This explicit cast can make subsequent optimizations more |
| 9435 | // obvious. |
| 9436 | Value *Op = GEP.getOperand(i); |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 9437 | if (TD->getTypeSizeInBits(Op->getType()) > TD->getPointerSizeInBits()) { |
Chris Lattner | 4f1134e | 2004-04-17 18:16:10 +0000 | [diff] [blame] | 9438 | if (Constant *C = dyn_cast<Constant>(Op)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9439 | GEP.setOperand(i, ConstantExpr::getTrunc(C, TD->getIntPtrType())); |
Chris Lattner | 4f1134e | 2004-04-17 18:16:10 +0000 | [diff] [blame] | 9440 | MadeChange = true; |
| 9441 | } else { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9442 | Op = InsertCastBefore(Instruction::Trunc, Op, TD->getIntPtrType(), |
| 9443 | GEP); |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 9444 | GEP.setOperand(i, Op); |
| 9445 | MadeChange = true; |
| 9446 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 9447 | } |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9448 | } |
Chris Lattner | db9654e | 2007-03-25 20:43:09 +0000 | [diff] [blame] | 9449 | } |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9450 | if (MadeChange) return &GEP; |
| 9451 | |
Chris Lattner | db9654e | 2007-03-25 20:43:09 +0000 | [diff] [blame] | 9452 | // If this GEP instruction doesn't move the pointer, and if the input operand |
| 9453 | // is a bitcast of another pointer, just replace the GEP with a bitcast of the |
| 9454 | // real input to the dest type. |
Chris Lattner | 6a94de2 | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 9455 | if (GEP.hasAllZeroIndices()) { |
| 9456 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(GEP.getOperand(0))) { |
| 9457 | // If the bitcast is of an allocation, and the allocation will be |
| 9458 | // converted to match the type of the cast, don't touch this. |
| 9459 | if (isa<AllocationInst>(BCI->getOperand(0))) { |
| 9460 | // 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] | 9461 | if (Instruction *I = visitBitCast(*BCI)) { |
| 9462 | if (I != BCI) { |
| 9463 | I->takeName(BCI); |
| 9464 | BCI->getParent()->getInstList().insert(BCI, I); |
| 9465 | ReplaceInstUsesWith(*BCI, I); |
| 9466 | } |
Chris Lattner | 6a94de2 | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 9467 | return &GEP; |
Chris Lattner | a79dd43 | 2007-10-12 18:05:47 +0000 | [diff] [blame] | 9468 | } |
Chris Lattner | 6a94de2 | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 9469 | } |
| 9470 | return new BitCastInst(BCI->getOperand(0), GEP.getType()); |
| 9471 | } |
| 9472 | } |
| 9473 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9474 | // Combine Indices - If the source pointer to this getelementptr instruction |
| 9475 | // is a getelementptr instruction, combine the indices of the two |
| 9476 | // getelementptr instructions into a single instruction. |
| 9477 | // |
Chris Lattner | 72588fc | 2007-02-15 22:48:32 +0000 | [diff] [blame] | 9478 | SmallVector<Value*, 8> SrcGEPOperands; |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 9479 | if (User *Src = dyn_castGetElementPtr(PtrOp)) |
Chris Lattner | 72588fc | 2007-02-15 22:48:32 +0000 | [diff] [blame] | 9480 | SrcGEPOperands.append(Src->op_begin(), Src->op_end()); |
Chris Lattner | ebd985c | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 9481 | |
| 9482 | if (!SrcGEPOperands.empty()) { |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9483 | // Note that if our source is a gep chain itself that we wait for that |
| 9484 | // chain to be resolved before we perform this transformation. This |
| 9485 | // avoids us creating a TON of code in some cases. |
| 9486 | // |
| 9487 | if (isa<GetElementPtrInst>(SrcGEPOperands[0]) && |
| 9488 | cast<Instruction>(SrcGEPOperands[0])->getNumOperands() == 2) |
| 9489 | return 0; // Wait until our source is folded to completion. |
| 9490 | |
Chris Lattner | 72588fc | 2007-02-15 22:48:32 +0000 | [diff] [blame] | 9491 | SmallVector<Value*, 8> Indices; |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9492 | |
| 9493 | // Find out whether the last index in the source GEP is a sequential idx. |
| 9494 | bool EndsWithSequential = false; |
| 9495 | for (gep_type_iterator I = gep_type_begin(*cast<User>(PtrOp)), |
| 9496 | E = gep_type_end(*cast<User>(PtrOp)); I != E; ++I) |
Chris Lattner | be97b4e | 2004-05-08 22:41:42 +0000 | [diff] [blame] | 9497 | EndsWithSequential = !isa<StructType>(*I); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9498 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9499 | // Can we combine the two pointer arithmetics offsets? |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9500 | if (EndsWithSequential) { |
Chris Lattner | decd081 | 2003-03-05 22:33:14 +0000 | [diff] [blame] | 9501 | // Replace: gep (gep %P, long B), long A, ... |
| 9502 | // With: T = long A+B; gep %P, T, ... |
| 9503 | // |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9504 | Value *Sum, *SO1 = SrcGEPOperands.back(), *GO1 = GEP.getOperand(1); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9505 | if (SO1 == Constant::getNullValue(SO1->getType())) { |
| 9506 | Sum = GO1; |
| 9507 | } else if (GO1 == Constant::getNullValue(GO1->getType())) { |
| 9508 | Sum = SO1; |
| 9509 | } else { |
| 9510 | // If they aren't the same type, convert both to an integer of the |
| 9511 | // target's pointer size. |
| 9512 | if (SO1->getType() != GO1->getType()) { |
| 9513 | if (Constant *SO1C = dyn_cast<Constant>(SO1)) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9514 | SO1 = ConstantExpr::getIntegerCast(SO1C, GO1->getType(), true); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9515 | } else if (Constant *GO1C = dyn_cast<Constant>(GO1)) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9516 | GO1 = ConstantExpr::getIntegerCast(GO1C, SO1->getType(), true); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9517 | } else { |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 9518 | unsigned PS = TD->getPointerSizeInBits(); |
| 9519 | if (TD->getTypeSizeInBits(SO1->getType()) == PS) { |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9520 | // Convert GO1 to SO1's type. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9521 | GO1 = InsertCastToIntPtrTy(GO1, SO1->getType(), &GEP, this); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9522 | |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 9523 | } else if (TD->getTypeSizeInBits(GO1->getType()) == PS) { |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9524 | // Convert SO1 to GO1's type. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9525 | SO1 = InsertCastToIntPtrTy(SO1, GO1->getType(), &GEP, this); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9526 | } else { |
| 9527 | const Type *PT = TD->getIntPtrType(); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9528 | SO1 = InsertCastToIntPtrTy(SO1, PT, &GEP, this); |
| 9529 | GO1 = InsertCastToIntPtrTy(GO1, PT, &GEP, this); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9530 | } |
| 9531 | } |
| 9532 | } |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9533 | if (isa<Constant>(SO1) && isa<Constant>(GO1)) |
| 9534 | Sum = ConstantExpr::getAdd(cast<Constant>(SO1), cast<Constant>(GO1)); |
| 9535 | else { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 9536 | Sum = BinaryOperator::createAdd(SO1, GO1, PtrOp->getName()+".sum"); |
| 9537 | InsertNewInstBefore(cast<Instruction>(Sum), GEP); |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9538 | } |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9539 | } |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9540 | |
| 9541 | // Recycle the GEP we already have if possible. |
| 9542 | if (SrcGEPOperands.size() == 2) { |
| 9543 | GEP.setOperand(0, SrcGEPOperands[0]); |
| 9544 | GEP.setOperand(1, Sum); |
| 9545 | return &GEP; |
| 9546 | } else { |
| 9547 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, |
| 9548 | SrcGEPOperands.end()-1); |
| 9549 | Indices.push_back(Sum); |
| 9550 | Indices.insert(Indices.end(), GEP.op_begin()+2, GEP.op_end()); |
| 9551 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9552 | } else if (isa<Constant>(*GEP.idx_begin()) && |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9553 | cast<Constant>(*GEP.idx_begin())->isNullValue() && |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9554 | SrcGEPOperands.size() != 1) { |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9555 | // 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] | 9556 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, |
| 9557 | SrcGEPOperands.end()); |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9558 | Indices.insert(Indices.end(), GEP.idx_begin()+1, GEP.idx_end()); |
| 9559 | } |
| 9560 | |
| 9561 | if (!Indices.empty()) |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9562 | return GetElementPtrInst::Create(SrcGEPOperands[0], Indices.begin(), |
| 9563 | Indices.end(), GEP.getName()); |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 9564 | |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9565 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(PtrOp)) { |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 9566 | // GEP of global variable. If all of the indices for this GEP are |
| 9567 | // constants, we can promote this to a constexpr instead of an instruction. |
| 9568 | |
| 9569 | // Scan for nonconstants... |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 9570 | SmallVector<Constant*, 8> Indices; |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 9571 | User::op_iterator I = GEP.idx_begin(), E = GEP.idx_end(); |
| 9572 | for (; I != E && isa<Constant>(*I); ++I) |
| 9573 | Indices.push_back(cast<Constant>(*I)); |
| 9574 | |
| 9575 | if (I == E) { // If they are all constants... |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 9576 | Constant *CE = ConstantExpr::getGetElementPtr(GV, |
| 9577 | &Indices[0],Indices.size()); |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 9578 | |
| 9579 | // Replace all uses of the GEP with the new constexpr... |
| 9580 | return ReplaceInstUsesWith(GEP, CE); |
| 9581 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9582 | } else if (Value *X = getBitCastOperand(PtrOp)) { // Is the operand a cast? |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 9583 | if (!isa<PointerType>(X->getType())) { |
| 9584 | // Not interesting. Source pointer must be a cast from pointer. |
| 9585 | } else if (HasZeroPointerIndex) { |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9586 | // transform: GEP (bitcast [10 x i8]* X to [0 x i8]*), i32 0, ... |
| 9587 | // into : GEP [10 x i8]* X, i32 0, ... |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 9588 | // |
| 9589 | // This occurs when the program declares an array extern like "int X[];" |
| 9590 | // |
| 9591 | const PointerType *CPTy = cast<PointerType>(PtrOp->getType()); |
| 9592 | const PointerType *XTy = cast<PointerType>(X->getType()); |
| 9593 | if (const ArrayType *XATy = |
| 9594 | dyn_cast<ArrayType>(XTy->getElementType())) |
| 9595 | if (const ArrayType *CATy = |
| 9596 | dyn_cast<ArrayType>(CPTy->getElementType())) |
| 9597 | if (CATy->getElementType() == XATy->getElementType()) { |
| 9598 | // At this point, we know that the cast source type is a pointer |
| 9599 | // to an array of the same type as the destination pointer |
| 9600 | // array. Because the array type is never stepped over (there |
| 9601 | // is a leading zero) we can fold the cast into this GEP. |
| 9602 | GEP.setOperand(0, X); |
| 9603 | return &GEP; |
| 9604 | } |
| 9605 | } else if (GEP.getNumOperands() == 2) { |
| 9606 | // Transform things like: |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9607 | // %t = getelementptr i32* bitcast ([2 x i32]* %str to i32*), i32 %V |
| 9608 | // into: %t1 = getelementptr [2 x i32]* %str, i32 0, i32 %V; bitcast |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 9609 | const Type *SrcElTy = cast<PointerType>(X->getType())->getElementType(); |
| 9610 | const Type *ResElTy=cast<PointerType>(PtrOp->getType())->getElementType(); |
| 9611 | if (isa<ArrayType>(SrcElTy) && |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 9612 | TD->getABITypeSize(cast<ArrayType>(SrcElTy)->getElementType()) == |
| 9613 | TD->getABITypeSize(ResElTy)) { |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 9614 | Value *Idx[2]; |
| 9615 | Idx[0] = Constant::getNullValue(Type::Int32Ty); |
| 9616 | Idx[1] = GEP.getOperand(1); |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 9617 | Value *V = InsertNewInstBefore( |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9618 | GetElementPtrInst::Create(X, Idx, Idx + 2, GEP.getName()), GEP); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9619 | // V and GEP are both pointer types --> BitCast |
| 9620 | return new BitCastInst(V, GEP.getType()); |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 9621 | } |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9622 | |
| 9623 | // Transform things like: |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9624 | // getelementptr i8* bitcast ([100 x double]* X to i8*), i32 %tmp |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9625 | // (where tmp = 8*tmp2) into: |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9626 | // getelementptr [100 x double]* %arr, i32 0, i32 %tmp2; bitcast |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9627 | |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9628 | if (isa<ArrayType>(SrcElTy) && ResElTy == Type::Int8Ty) { |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9629 | uint64_t ArrayEltSize = |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 9630 | TD->getABITypeSize(cast<ArrayType>(SrcElTy)->getElementType()); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9631 | |
| 9632 | // Check to see if "tmp" is a scale by a multiple of ArrayEltSize. We |
| 9633 | // allow either a mul, shift, or constant here. |
| 9634 | Value *NewIdx = 0; |
| 9635 | ConstantInt *Scale = 0; |
| 9636 | if (ArrayEltSize == 1) { |
| 9637 | NewIdx = GEP.getOperand(1); |
| 9638 | Scale = ConstantInt::get(NewIdx->getType(), 1); |
| 9639 | } else if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP.getOperand(1))) { |
Chris Lattner | 6e2f843 | 2005-09-14 17:32:56 +0000 | [diff] [blame] | 9640 | NewIdx = ConstantInt::get(CI->getType(), 1); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9641 | Scale = CI; |
| 9642 | } else if (Instruction *Inst =dyn_cast<Instruction>(GEP.getOperand(1))){ |
| 9643 | if (Inst->getOpcode() == Instruction::Shl && |
| 9644 | isa<ConstantInt>(Inst->getOperand(1))) { |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 9645 | ConstantInt *ShAmt = cast<ConstantInt>(Inst->getOperand(1)); |
| 9646 | uint32_t ShAmtVal = ShAmt->getLimitedValue(64); |
| 9647 | Scale = ConstantInt::get(Inst->getType(), 1ULL << ShAmtVal); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9648 | NewIdx = Inst->getOperand(0); |
| 9649 | } else if (Inst->getOpcode() == Instruction::Mul && |
| 9650 | isa<ConstantInt>(Inst->getOperand(1))) { |
| 9651 | Scale = cast<ConstantInt>(Inst->getOperand(1)); |
| 9652 | NewIdx = Inst->getOperand(0); |
| 9653 | } |
| 9654 | } |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9655 | |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9656 | // 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] | 9657 | // out, perform the transformation. Note, we don't know whether Scale is |
| 9658 | // signed or not. We'll use unsigned version of division/modulo |
| 9659 | // operation after making sure Scale doesn't have the sign bit set. |
| 9660 | if (Scale && Scale->getSExtValue() >= 0LL && |
| 9661 | Scale->getZExtValue() % ArrayEltSize == 0) { |
| 9662 | Scale = ConstantInt::get(Scale->getType(), |
| 9663 | Scale->getZExtValue() / ArrayEltSize); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 9664 | if (Scale->getZExtValue() != 1) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9665 | Constant *C = ConstantExpr::getIntegerCast(Scale, NewIdx->getType(), |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9666 | false /*ZExt*/); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9667 | Instruction *Sc = BinaryOperator::createMul(NewIdx, C, "idxscale"); |
| 9668 | NewIdx = InsertNewInstBefore(Sc, GEP); |
| 9669 | } |
| 9670 | |
| 9671 | // Insert the new GEP instruction. |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 9672 | Value *Idx[2]; |
| 9673 | Idx[0] = Constant::getNullValue(Type::Int32Ty); |
| 9674 | Idx[1] = NewIdx; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9675 | Instruction *NewGEP = |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9676 | GetElementPtrInst::Create(X, Idx, Idx + 2, GEP.getName()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9677 | NewGEP = InsertNewInstBefore(NewGEP, GEP); |
| 9678 | // The NewGEP must be pointer typed, so must the old one -> BitCast |
| 9679 | return new BitCastInst(NewGEP, GEP.getType()); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9680 | } |
| 9681 | } |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 9682 | } |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9683 | } |
| 9684 | |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9685 | return 0; |
| 9686 | } |
| 9687 | |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 9688 | Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) { |
| 9689 | // Convert: malloc Ty, C - where C is a constant != 1 into: malloc [C x Ty], 1 |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 9690 | if (AI.isArrayAllocation()) { // Check C != 1 |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 9691 | if (const ConstantInt *C = dyn_cast<ConstantInt>(AI.getArraySize())) { |
| 9692 | const Type *NewTy = |
| 9693 | ArrayType::get(AI.getAllocatedType(), C->getZExtValue()); |
Chris Lattner | 0006bd7 | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 9694 | AllocationInst *New = 0; |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 9695 | |
| 9696 | // Create and insert the replacement instruction... |
| 9697 | if (isa<MallocInst>(AI)) |
Nate Begeman | 14b0529 | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 9698 | New = new MallocInst(NewTy, 0, AI.getAlignment(), AI.getName()); |
Chris Lattner | 0006bd7 | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 9699 | else { |
| 9700 | assert(isa<AllocaInst>(AI) && "Unknown type of allocation inst!"); |
Nate Begeman | 14b0529 | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 9701 | New = new AllocaInst(NewTy, 0, AI.getAlignment(), AI.getName()); |
Chris Lattner | 0006bd7 | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 9702 | } |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 9703 | |
| 9704 | InsertNewInstBefore(New, AI); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9705 | |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 9706 | // Scan to the end of the allocation instructions, to skip over a block of |
| 9707 | // allocas if possible... |
| 9708 | // |
| 9709 | BasicBlock::iterator It = New; |
| 9710 | while (isa<AllocationInst>(*It)) ++It; |
| 9711 | |
| 9712 | // Now that I is pointing to the first non-allocation-inst in the block, |
| 9713 | // insert our getelementptr instruction... |
| 9714 | // |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9715 | Value *NullIdx = Constant::getNullValue(Type::Int32Ty); |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 9716 | Value *Idx[2]; |
| 9717 | Idx[0] = NullIdx; |
| 9718 | Idx[1] = NullIdx; |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9719 | Value *V = GetElementPtrInst::Create(New, Idx, Idx + 2, |
| 9720 | New->getName()+".sub", It); |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 9721 | |
| 9722 | // Now make everything use the getelementptr instead of the original |
| 9723 | // allocation. |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 9724 | return ReplaceInstUsesWith(AI, V); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 9725 | } else if (isa<UndefValue>(AI.getArraySize())) { |
| 9726 | return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 9727 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 9728 | } |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 9729 | |
| 9730 | // If alloca'ing a zero byte object, replace the alloca with a null pointer. |
| 9731 | // Note that we only do this for alloca's, because malloc should allocate and |
| 9732 | // return a unique pointer, even for a zero byte allocation. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9733 | if (isa<AllocaInst>(AI) && AI.getAllocatedType()->isSized() && |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 9734 | TD->getABITypeSize(AI.getAllocatedType()) == 0) |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 9735 | return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); |
| 9736 | |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 9737 | return 0; |
| 9738 | } |
| 9739 | |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 9740 | Instruction *InstCombiner::visitFreeInst(FreeInst &FI) { |
| 9741 | Value *Op = FI.getOperand(0); |
| 9742 | |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 9743 | // free undef -> unreachable. |
| 9744 | if (isa<UndefValue>(Op)) { |
| 9745 | // 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] | 9746 | new StoreInst(ConstantInt::getTrue(), |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 9747 | UndefValue::get(PointerType::getUnqual(Type::Int1Ty)), &FI); |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 9748 | return EraseInstFromFunction(FI); |
| 9749 | } |
Chris Lattner | 6fe5541 | 2007-04-14 00:20:02 +0000 | [diff] [blame] | 9750 | |
Chris Lattner | 6160e85 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 9751 | // If we have 'free null' delete the instruction. This can happen in stl code |
| 9752 | // when lots of inlining happens. |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 9753 | if (isa<ConstantPointerNull>(Op)) |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 9754 | return EraseInstFromFunction(FI); |
Chris Lattner | 6fe5541 | 2007-04-14 00:20:02 +0000 | [diff] [blame] | 9755 | |
| 9756 | // Change free <ty>* (cast <ty2>* X to <ty>*) into free <ty2>* X |
| 9757 | if (BitCastInst *CI = dyn_cast<BitCastInst>(Op)) { |
| 9758 | FI.setOperand(0, CI->getOperand(0)); |
| 9759 | return &FI; |
| 9760 | } |
| 9761 | |
| 9762 | // Change free (gep X, 0,0,0,0) into free(X) |
| 9763 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) { |
| 9764 | if (GEPI->hasAllZeroIndices()) { |
| 9765 | AddToWorkList(GEPI); |
| 9766 | FI.setOperand(0, GEPI->getOperand(0)); |
| 9767 | return &FI; |
| 9768 | } |
| 9769 | } |
| 9770 | |
| 9771 | // Change free(malloc) into nothing, if the malloc has a single use. |
| 9772 | if (MallocInst *MI = dyn_cast<MallocInst>(Op)) |
| 9773 | if (MI->hasOneUse()) { |
| 9774 | EraseInstFromFunction(FI); |
| 9775 | return EraseInstFromFunction(*MI); |
| 9776 | } |
Chris Lattner | 6160e85 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 9777 | |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 9778 | return 0; |
| 9779 | } |
| 9780 | |
| 9781 | |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 9782 | /// InstCombineLoadCast - Fold 'load (cast P)' -> cast (load P)' when possible. |
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 9783 | static Instruction *InstCombineLoadCast(InstCombiner &IC, LoadInst &LI, |
Bill Wendling | 587c01d | 2008-02-26 10:53:30 +0000 | [diff] [blame] | 9784 | const TargetData *TD) { |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 9785 | User *CI = cast<User>(LI.getOperand(0)); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 9786 | Value *CastOp = CI->getOperand(0); |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 9787 | |
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 9788 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(CI)) { |
| 9789 | // Instead of loading constant c string, use corresponding integer value |
| 9790 | // directly if string length is small enough. |
| 9791 | const std::string &Str = CE->getOperand(0)->getStringValue(); |
| 9792 | if (!Str.empty()) { |
| 9793 | unsigned len = Str.length(); |
| 9794 | const Type *Ty = cast<PointerType>(CE->getType())->getElementType(); |
| 9795 | unsigned numBits = Ty->getPrimitiveSizeInBits(); |
| 9796 | // Replace LI with immediate integer store. |
| 9797 | if ((numBits >> 3) == len + 1) { |
Bill Wendling | 587c01d | 2008-02-26 10:53:30 +0000 | [diff] [blame] | 9798 | APInt StrVal(numBits, 0); |
| 9799 | APInt SingleChar(numBits, 0); |
| 9800 | if (TD->isLittleEndian()) { |
| 9801 | for (signed i = len-1; i >= 0; i--) { |
| 9802 | SingleChar = (uint64_t) Str[i]; |
| 9803 | StrVal = (StrVal << 8) | SingleChar; |
| 9804 | } |
| 9805 | } else { |
| 9806 | for (unsigned i = 0; i < len; i++) { |
| 9807 | SingleChar = (uint64_t) Str[i]; |
| 9808 | StrVal = (StrVal << 8) | SingleChar; |
| 9809 | } |
| 9810 | // Append NULL at the end. |
| 9811 | SingleChar = 0; |
| 9812 | StrVal = (StrVal << 8) | SingleChar; |
| 9813 | } |
| 9814 | Value *NL = ConstantInt::get(StrVal); |
| 9815 | return IC.ReplaceInstUsesWith(LI, NL); |
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 9816 | } |
| 9817 | } |
| 9818 | } |
| 9819 | |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 9820 | const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType(); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 9821 | if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) { |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 9822 | const Type *SrcPTy = SrcTy->getElementType(); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 9823 | |
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 9824 | if (DestPTy->isInteger() || isa<PointerType>(DestPTy) || |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 9825 | isa<VectorType>(DestPTy)) { |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 9826 | // If the source is an array, the code below will not succeed. Check to |
| 9827 | // see if a trivial 'gep P, 0, 0' will help matters. Only do this for |
| 9828 | // constants. |
| 9829 | if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy)) |
| 9830 | if (Constant *CSrc = dyn_cast<Constant>(CastOp)) |
| 9831 | if (ASrcTy->getNumElements() != 0) { |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 9832 | Value *Idxs[2]; |
| 9833 | Idxs[0] = Idxs[1] = Constant::getNullValue(Type::Int32Ty); |
| 9834 | CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs, 2); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 9835 | SrcTy = cast<PointerType>(CastOp->getType()); |
| 9836 | SrcPTy = SrcTy->getElementType(); |
| 9837 | } |
| 9838 | |
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 9839 | if ((SrcPTy->isInteger() || isa<PointerType>(SrcPTy) || |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 9840 | isa<VectorType>(SrcPTy)) && |
Chris Lattner | b1515fe | 2005-03-29 06:37:47 +0000 | [diff] [blame] | 9841 | // Do not allow turning this into a load of an integer, which is then |
| 9842 | // casted to a pointer, this pessimizes pointer analysis a lot. |
| 9843 | (isa<PointerType>(SrcPTy) == isa<PointerType>(LI.getType())) && |
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 9844 | IC.getTargetData().getTypeSizeInBits(SrcPTy) == |
| 9845 | IC.getTargetData().getTypeSizeInBits(DestPTy)) { |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9846 | |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 9847 | // Okay, we are casting from one integer or pointer type to another of |
| 9848 | // the same size. Instead of casting the pointer before the load, cast |
| 9849 | // the result of the loaded value. |
| 9850 | Value *NewLoad = IC.InsertNewInstBefore(new LoadInst(CastOp, |
| 9851 | CI->getName(), |
| 9852 | LI.isVolatile()),LI); |
| 9853 | // Now cast the result of the load. |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 9854 | return new BitCastInst(NewLoad, LI.getType()); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 9855 | } |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 9856 | } |
| 9857 | } |
| 9858 | return 0; |
| 9859 | } |
| 9860 | |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 9861 | /// isSafeToLoadUnconditionally - Return true if we know that executing a load |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 9862 | /// from this value cannot trap. If it is not obviously safe to load from the |
| 9863 | /// specified pointer, we do a quick local scan of the basic block containing |
| 9864 | /// ScanFrom, to determine if the address is already accessed. |
| 9865 | static bool isSafeToLoadUnconditionally(Value *V, Instruction *ScanFrom) { |
Duncan Sands | 892c7e4 | 2007-09-19 10:10:31 +0000 | [diff] [blame] | 9866 | // If it is an alloca it is always safe to load from. |
| 9867 | if (isa<AllocaInst>(V)) return true; |
| 9868 | |
Duncan Sands | 46318cd | 2007-09-19 10:25:38 +0000 | [diff] [blame] | 9869 | // 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] | 9870 | if (const GlobalValue *GV = dyn_cast<GlobalVariable>(V)) |
Duncan Sands | 46318cd | 2007-09-19 10:25:38 +0000 | [diff] [blame] | 9871 | // Don't try to evaluate aliases. External weak GV can be null. |
Duncan Sands | 892c7e4 | 2007-09-19 10:10:31 +0000 | [diff] [blame] | 9872 | return !isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage(); |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 9873 | |
| 9874 | // Otherwise, be a little bit agressive by scanning the local block where we |
| 9875 | // 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] | 9876 | // from/to. If so, the previous load or store would have already trapped, |
| 9877 | // so there is no harm doing an extra load (also, CSE will later eliminate |
| 9878 | // the load entirely). |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 9879 | BasicBlock::iterator BBI = ScanFrom, E = ScanFrom->getParent()->begin(); |
| 9880 | |
Alkis Evlogimenos | 7b6ec60 | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 9881 | while (BBI != E) { |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 9882 | --BBI; |
| 9883 | |
| 9884 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { |
| 9885 | if (LI->getOperand(0) == V) return true; |
| 9886 | } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) |
| 9887 | if (SI->getOperand(1) == V) return true; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9888 | |
Alkis Evlogimenos | 7b6ec60 | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 9889 | } |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 9890 | return false; |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 9891 | } |
| 9892 | |
Chris Lattner | 8d2e888 | 2007-08-11 18:48:48 +0000 | [diff] [blame] | 9893 | /// GetUnderlyingObject - Trace through a series of getelementptrs and bitcasts |
| 9894 | /// until we find the underlying object a pointer is referring to or something |
| 9895 | /// we don't understand. Note that the returned pointer may be offset from the |
| 9896 | /// input, because we ignore GEP indices. |
| 9897 | static Value *GetUnderlyingObject(Value *Ptr) { |
| 9898 | while (1) { |
| 9899 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) { |
| 9900 | if (CE->getOpcode() == Instruction::BitCast || |
| 9901 | CE->getOpcode() == Instruction::GetElementPtr) |
| 9902 | Ptr = CE->getOperand(0); |
| 9903 | else |
| 9904 | return Ptr; |
| 9905 | } else if (BitCastInst *BCI = dyn_cast<BitCastInst>(Ptr)) { |
| 9906 | Ptr = BCI->getOperand(0); |
| 9907 | } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) { |
| 9908 | Ptr = GEP->getOperand(0); |
| 9909 | } else { |
| 9910 | return Ptr; |
| 9911 | } |
| 9912 | } |
| 9913 | } |
| 9914 | |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 9915 | Instruction *InstCombiner::visitLoadInst(LoadInst &LI) { |
| 9916 | Value *Op = LI.getOperand(0); |
Chris Lattner | 5f16a13 | 2004-01-12 04:13:56 +0000 | [diff] [blame] | 9917 | |
Dan Gohman | 9941f74 | 2007-07-20 16:34:21 +0000 | [diff] [blame] | 9918 | // Attempt to improve the alignment. |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 9919 | unsigned KnownAlign = GetOrEnforceKnownAlignment(Op); |
| 9920 | if (KnownAlign > |
| 9921 | (LI.getAlignment() == 0 ? TD->getABITypeAlignment(LI.getType()) : |
| 9922 | LI.getAlignment())) |
Dan Gohman | 9941f74 | 2007-07-20 16:34:21 +0000 | [diff] [blame] | 9923 | LI.setAlignment(KnownAlign); |
| 9924 | |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 9925 | // load (cast X) --> cast (load X) iff safe |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 9926 | if (isa<CastInst>(Op)) |
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 9927 | if (Instruction *Res = InstCombineLoadCast(*this, LI, TD)) |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 9928 | return Res; |
| 9929 | |
| 9930 | // None of the following transforms are legal for volatile loads. |
| 9931 | if (LI.isVolatile()) return 0; |
Chris Lattner | 62f254d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 9932 | |
Chris Lattner | 62f254d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 9933 | if (&LI.getParent()->front() != &LI) { |
| 9934 | BasicBlock::iterator BBI = &LI; --BBI; |
Chris Lattner | 9c1f0fd | 2005-09-12 22:21:03 +0000 | [diff] [blame] | 9935 | // If the instruction immediately before this is a store to the same |
| 9936 | // address, do a simple form of store->load forwarding. |
Chris Lattner | 62f254d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 9937 | if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) |
| 9938 | if (SI->getOperand(1) == LI.getOperand(0)) |
| 9939 | return ReplaceInstUsesWith(LI, SI->getOperand(0)); |
Chris Lattner | 9c1f0fd | 2005-09-12 22:21:03 +0000 | [diff] [blame] | 9940 | if (LoadInst *LIB = dyn_cast<LoadInst>(BBI)) |
| 9941 | if (LIB->getOperand(0) == LI.getOperand(0)) |
| 9942 | return ReplaceInstUsesWith(LI, LIB); |
Chris Lattner | 62f254d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 9943 | } |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 9944 | |
Christopher Lamb | b15147e | 2007-12-29 07:56:53 +0000 | [diff] [blame] | 9945 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) { |
| 9946 | const Value *GEPI0 = GEPI->getOperand(0); |
| 9947 | // TODO: Consider a target hook for valid address spaces for this xform. |
| 9948 | if (isa<ConstantPointerNull>(GEPI0) && |
| 9949 | cast<PointerType>(GEPI0->getType())->getAddressSpace() == 0) { |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 9950 | // Insert a new store to null instruction before the load to indicate |
| 9951 | // that this code is not reachable. We do this instead of inserting |
| 9952 | // an unreachable instruction directly because we cannot modify the |
| 9953 | // CFG. |
| 9954 | new StoreInst(UndefValue::get(LI.getType()), |
| 9955 | Constant::getNullValue(Op->getType()), &LI); |
| 9956 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
| 9957 | } |
Christopher Lamb | b15147e | 2007-12-29 07:56:53 +0000 | [diff] [blame] | 9958 | } |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 9959 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 9960 | if (Constant *C = dyn_cast<Constant>(Op)) { |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 9961 | // load null/undef -> undef |
Christopher Lamb | b15147e | 2007-12-29 07:56:53 +0000 | [diff] [blame] | 9962 | // TODO: Consider a target hook for valid address spaces for this xform. |
| 9963 | if (isa<UndefValue>(C) || (C->isNullValue() && |
| 9964 | cast<PointerType>(Op->getType())->getAddressSpace() == 0)) { |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 9965 | // Insert a new store to null instruction before the load to indicate that |
| 9966 | // this code is not reachable. We do this instead of inserting an |
| 9967 | // unreachable instruction directly because we cannot modify the CFG. |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 9968 | new StoreInst(UndefValue::get(LI.getType()), |
| 9969 | Constant::getNullValue(Op->getType()), &LI); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 9970 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 9971 | } |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 9972 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 9973 | // Instcombine load (constant global) into the value loaded. |
| 9974 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op)) |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 9975 | if (GV->isConstant() && !GV->isDeclaration()) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 9976 | return ReplaceInstUsesWith(LI, GV->getInitializer()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9977 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 9978 | // Instcombine load (constantexpr_GEP global, 0, ...) into the value loaded. |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 9979 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op)) { |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 9980 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 9981 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0))) |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 9982 | if (GV->isConstant() && !GV->isDeclaration()) |
Chris Lattner | 363f2a2 | 2005-09-26 05:28:06 +0000 | [diff] [blame] | 9983 | if (Constant *V = |
| 9984 | ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE)) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 9985 | return ReplaceInstUsesWith(LI, V); |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 9986 | if (CE->getOperand(0)->isNullValue()) { |
| 9987 | // Insert a new store to null instruction before the load to indicate |
| 9988 | // that this code is not reachable. We do this instead of inserting |
| 9989 | // an unreachable instruction directly because we cannot modify the |
| 9990 | // CFG. |
| 9991 | new StoreInst(UndefValue::get(LI.getType()), |
| 9992 | Constant::getNullValue(Op->getType()), &LI); |
| 9993 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
| 9994 | } |
| 9995 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9996 | } else if (CE->isCast()) { |
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 9997 | if (Instruction *Res = InstCombineLoadCast(*this, LI, TD)) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 9998 | return Res; |
| 9999 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 10000 | } |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10001 | } |
Chris Lattner | 8d2e888 | 2007-08-11 18:48:48 +0000 | [diff] [blame] | 10002 | |
| 10003 | // If this load comes from anywhere in a constant global, and if the global |
| 10004 | // is all undef or zero, we know what it loads. |
| 10005 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GetUnderlyingObject(Op))) { |
| 10006 | if (GV->isConstant() && GV->hasInitializer()) { |
| 10007 | if (GV->getInitializer()->isNullValue()) |
| 10008 | return ReplaceInstUsesWith(LI, Constant::getNullValue(LI.getType())); |
| 10009 | else if (isa<UndefValue>(GV->getInitializer())) |
| 10010 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
| 10011 | } |
| 10012 | } |
Chris Lattner | f499eac | 2004-04-08 20:39:49 +0000 | [diff] [blame] | 10013 | |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 10014 | if (Op->hasOneUse()) { |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 10015 | // Change select and PHI nodes to select values instead of addresses: this |
| 10016 | // helps alias analysis out a lot, allows many others simplifications, and |
| 10017 | // exposes redundancy in the code. |
| 10018 | // |
| 10019 | // Note that we cannot do the transformation unless we know that the |
| 10020 | // introduced loads cannot trap! Something like this is valid as long as |
| 10021 | // the condition is always false: load (select bool %C, int* null, int* %G), |
| 10022 | // but it would not be valid if we transformed it to load from null |
| 10023 | // unconditionally. |
| 10024 | // |
| 10025 | if (SelectInst *SI = dyn_cast<SelectInst>(Op)) { |
| 10026 | // load (select (Cond, &V1, &V2)) --> select(Cond, load &V1, load &V2). |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 10027 | if (isSafeToLoadUnconditionally(SI->getOperand(1), SI) && |
| 10028 | isSafeToLoadUnconditionally(SI->getOperand(2), SI)) { |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 10029 | Value *V1 = InsertNewInstBefore(new LoadInst(SI->getOperand(1), |
Chris Lattner | 79f0c8e | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 10030 | SI->getOperand(1)->getName()+".val"), LI); |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 10031 | Value *V2 = InsertNewInstBefore(new LoadInst(SI->getOperand(2), |
Chris Lattner | 79f0c8e | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 10032 | SI->getOperand(2)->getName()+".val"), LI); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 10033 | return SelectInst::Create(SI->getCondition(), V1, V2); |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 10034 | } |
| 10035 | |
Chris Lattner | 684fe21 | 2004-09-23 15:46:00 +0000 | [diff] [blame] | 10036 | // load (select (cond, null, P)) -> load P |
| 10037 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(1))) |
| 10038 | if (C->isNullValue()) { |
| 10039 | LI.setOperand(0, SI->getOperand(2)); |
| 10040 | return &LI; |
| 10041 | } |
| 10042 | |
| 10043 | // load (select (cond, P, null)) -> load P |
| 10044 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(2))) |
| 10045 | if (C->isNullValue()) { |
| 10046 | LI.setOperand(0, SI->getOperand(1)); |
| 10047 | return &LI; |
| 10048 | } |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 10049 | } |
| 10050 | } |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 10051 | return 0; |
| 10052 | } |
| 10053 | |
Reid Spencer | 55af2b5 | 2007-01-19 21:20:31 +0000 | [diff] [blame] | 10054 | /// InstCombineStoreToCast - Fold store V, (cast P) -> store (cast V), P |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10055 | /// when possible. |
| 10056 | static Instruction *InstCombineStoreToCast(InstCombiner &IC, StoreInst &SI) { |
| 10057 | User *CI = cast<User>(SI.getOperand(1)); |
| 10058 | Value *CastOp = CI->getOperand(0); |
| 10059 | |
| 10060 | const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType(); |
| 10061 | if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) { |
| 10062 | const Type *SrcPTy = SrcTy->getElementType(); |
| 10063 | |
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 10064 | if (DestPTy->isInteger() || isa<PointerType>(DestPTy)) { |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10065 | // If the source is an array, the code below will not succeed. Check to |
| 10066 | // see if a trivial 'gep P, 0, 0' will help matters. Only do this for |
| 10067 | // constants. |
| 10068 | if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy)) |
| 10069 | if (Constant *CSrc = dyn_cast<Constant>(CastOp)) |
| 10070 | if (ASrcTy->getNumElements() != 0) { |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 10071 | Value* Idxs[2]; |
| 10072 | Idxs[0] = Idxs[1] = Constant::getNullValue(Type::Int32Ty); |
| 10073 | CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs, 2); |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10074 | SrcTy = cast<PointerType>(CastOp->getType()); |
| 10075 | SrcPTy = SrcTy->getElementType(); |
| 10076 | } |
| 10077 | |
Reid Spencer | 67f827c | 2007-01-20 23:35:48 +0000 | [diff] [blame] | 10078 | if ((SrcPTy->isInteger() || isa<PointerType>(SrcPTy)) && |
| 10079 | IC.getTargetData().getTypeSizeInBits(SrcPTy) == |
| 10080 | IC.getTargetData().getTypeSizeInBits(DestPTy)) { |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10081 | |
| 10082 | // 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] | 10083 | // the same size. Instead of casting the pointer before |
| 10084 | // the store, cast the value to be stored. |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10085 | Value *NewCast; |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 10086 | Value *SIOp0 = SI.getOperand(0); |
Reid Spencer | 7515396 | 2007-01-18 18:54:33 +0000 | [diff] [blame] | 10087 | Instruction::CastOps opcode = Instruction::BitCast; |
| 10088 | const Type* CastSrcTy = SIOp0->getType(); |
| 10089 | const Type* CastDstTy = SrcPTy; |
| 10090 | if (isa<PointerType>(CastDstTy)) { |
| 10091 | if (CastSrcTy->isInteger()) |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 10092 | opcode = Instruction::IntToPtr; |
Reid Spencer | 67f827c | 2007-01-20 23:35:48 +0000 | [diff] [blame] | 10093 | } else if (isa<IntegerType>(CastDstTy)) { |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 10094 | if (isa<PointerType>(SIOp0->getType())) |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 10095 | opcode = Instruction::PtrToInt; |
| 10096 | } |
| 10097 | if (Constant *C = dyn_cast<Constant>(SIOp0)) |
Reid Spencer | 7515396 | 2007-01-18 18:54:33 +0000 | [diff] [blame] | 10098 | NewCast = ConstantExpr::getCast(opcode, C, CastDstTy); |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10099 | else |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 10100 | NewCast = IC.InsertNewInstBefore( |
Reid Spencer | 7515396 | 2007-01-18 18:54:33 +0000 | [diff] [blame] | 10101 | CastInst::create(opcode, SIOp0, CastDstTy, SIOp0->getName()+".c"), |
| 10102 | SI); |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10103 | return new StoreInst(NewCast, CastOp); |
| 10104 | } |
| 10105 | } |
| 10106 | } |
| 10107 | return 0; |
| 10108 | } |
| 10109 | |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10110 | Instruction *InstCombiner::visitStoreInst(StoreInst &SI) { |
| 10111 | Value *Val = SI.getOperand(0); |
| 10112 | Value *Ptr = SI.getOperand(1); |
| 10113 | |
| 10114 | if (isa<UndefValue>(Ptr)) { // store X, undef -> noop (even if volatile) |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 10115 | EraseInstFromFunction(SI); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10116 | ++NumCombined; |
| 10117 | return 0; |
| 10118 | } |
Chris Lattner | 836692d | 2007-01-15 06:51:56 +0000 | [diff] [blame] | 10119 | |
| 10120 | // If the RHS is an alloca with a single use, zapify the store, making the |
| 10121 | // alloca dead. |
| 10122 | if (Ptr->hasOneUse()) { |
| 10123 | if (isa<AllocaInst>(Ptr)) { |
| 10124 | EraseInstFromFunction(SI); |
| 10125 | ++NumCombined; |
| 10126 | return 0; |
| 10127 | } |
| 10128 | |
| 10129 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) |
| 10130 | if (isa<AllocaInst>(GEP->getOperand(0)) && |
| 10131 | GEP->getOperand(0)->hasOneUse()) { |
| 10132 | EraseInstFromFunction(SI); |
| 10133 | ++NumCombined; |
| 10134 | return 0; |
| 10135 | } |
| 10136 | } |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10137 | |
Dan Gohman | 9941f74 | 2007-07-20 16:34:21 +0000 | [diff] [blame] | 10138 | // Attempt to improve the alignment. |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 10139 | unsigned KnownAlign = GetOrEnforceKnownAlignment(Ptr); |
| 10140 | if (KnownAlign > |
| 10141 | (SI.getAlignment() == 0 ? TD->getABITypeAlignment(Val->getType()) : |
| 10142 | SI.getAlignment())) |
Dan Gohman | 9941f74 | 2007-07-20 16:34:21 +0000 | [diff] [blame] | 10143 | SI.setAlignment(KnownAlign); |
| 10144 | |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 10145 | // Do really simple DSE, to catch cases where there are several consequtive |
| 10146 | // stores to the same location, separated by a few arithmetic operations. This |
| 10147 | // situation often occurs with bitfield accesses. |
| 10148 | BasicBlock::iterator BBI = &SI; |
| 10149 | for (unsigned ScanInsts = 6; BBI != SI.getParent()->begin() && ScanInsts; |
| 10150 | --ScanInsts) { |
| 10151 | --BBI; |
| 10152 | |
| 10153 | if (StoreInst *PrevSI = dyn_cast<StoreInst>(BBI)) { |
| 10154 | // Prev store isn't volatile, and stores to the same location? |
| 10155 | if (!PrevSI->isVolatile() && PrevSI->getOperand(1) == SI.getOperand(1)) { |
| 10156 | ++NumDeadStore; |
| 10157 | ++BBI; |
| 10158 | EraseInstFromFunction(*PrevSI); |
| 10159 | continue; |
| 10160 | } |
| 10161 | break; |
| 10162 | } |
| 10163 | |
Chris Lattner | b4db97f | 2006-05-26 19:19:20 +0000 | [diff] [blame] | 10164 | // If this is a load, we have to stop. However, if the loaded value is from |
| 10165 | // the pointer we're loading and is producing the pointer we're storing, |
| 10166 | // then *this* store is dead (X = load P; store X -> P). |
| 10167 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { |
Chris Lattner | a54c7eb | 2007-09-07 05:33:03 +0000 | [diff] [blame] | 10168 | if (LI == Val && LI->getOperand(0) == Ptr && !SI.isVolatile()) { |
Chris Lattner | b4db97f | 2006-05-26 19:19:20 +0000 | [diff] [blame] | 10169 | EraseInstFromFunction(SI); |
| 10170 | ++NumCombined; |
| 10171 | return 0; |
| 10172 | } |
| 10173 | // Otherwise, this is a load from some other location. Stores before it |
| 10174 | // may not be dead. |
| 10175 | break; |
| 10176 | } |
| 10177 | |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 10178 | // Don't skip over loads or things that can modify memory. |
Chris Lattner | b4db97f | 2006-05-26 19:19:20 +0000 | [diff] [blame] | 10179 | if (BBI->mayWriteToMemory()) |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 10180 | break; |
| 10181 | } |
| 10182 | |
| 10183 | |
| 10184 | if (SI.isVolatile()) return 0; // Don't hack volatile stores. |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10185 | |
| 10186 | // store X, null -> turns into 'unreachable' in SimplifyCFG |
| 10187 | if (isa<ConstantPointerNull>(Ptr)) { |
| 10188 | if (!isa<UndefValue>(Val)) { |
| 10189 | SI.setOperand(0, UndefValue::get(Val->getType())); |
| 10190 | if (Instruction *U = dyn_cast<Instruction>(Val)) |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10191 | AddToWorkList(U); // Dropped a use. |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10192 | ++NumCombined; |
| 10193 | } |
| 10194 | return 0; // Do not modify these! |
| 10195 | } |
| 10196 | |
| 10197 | // store undef, Ptr -> noop |
| 10198 | if (isa<UndefValue>(Val)) { |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 10199 | EraseInstFromFunction(SI); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10200 | ++NumCombined; |
| 10201 | return 0; |
| 10202 | } |
| 10203 | |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10204 | // If the pointer destination is a cast, see if we can fold the cast into the |
| 10205 | // source instead. |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 10206 | if (isa<CastInst>(Ptr)) |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10207 | if (Instruction *Res = InstCombineStoreToCast(*this, SI)) |
| 10208 | return Res; |
| 10209 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 10210 | if (CE->isCast()) |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10211 | if (Instruction *Res = InstCombineStoreToCast(*this, SI)) |
| 10212 | return Res; |
| 10213 | |
Chris Lattner | 408902b | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 10214 | |
| 10215 | // If this store is the last instruction in the basic block, and if the block |
| 10216 | // 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] | 10217 | BBI = &SI; ++BBI; |
Chris Lattner | 408902b | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 10218 | if (BranchInst *BI = dyn_cast<BranchInst>(BBI)) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10219 | if (BI->isUnconditional()) |
| 10220 | if (SimplifyStoreAtEndOfBlock(SI)) |
| 10221 | return 0; // xform done! |
Chris Lattner | 408902b | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 10222 | |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10223 | return 0; |
| 10224 | } |
| 10225 | |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10226 | /// SimplifyStoreAtEndOfBlock - Turn things like: |
| 10227 | /// if () { *P = v1; } else { *P = v2 } |
| 10228 | /// into a phi node with a store in the successor. |
| 10229 | /// |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10230 | /// Simplify things like: |
| 10231 | /// *P = v1; if () { *P = v2; } |
| 10232 | /// into a phi node with a store in the successor. |
| 10233 | /// |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10234 | bool InstCombiner::SimplifyStoreAtEndOfBlock(StoreInst &SI) { |
| 10235 | BasicBlock *StoreBB = SI.getParent(); |
| 10236 | |
| 10237 | // Check to see if the successor block has exactly two incoming edges. If |
| 10238 | // so, see if the other predecessor contains a store to the same location. |
| 10239 | // 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] | 10240 | BasicBlock *DestBB = StoreBB->getTerminator()->getSuccessor(0); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10241 | |
| 10242 | // Determine whether Dest has exactly two predecessors and, if so, compute |
| 10243 | // the other predecessor. |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10244 | pred_iterator PI = pred_begin(DestBB); |
| 10245 | BasicBlock *OtherBB = 0; |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10246 | if (*PI != StoreBB) |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10247 | OtherBB = *PI; |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10248 | ++PI; |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10249 | if (PI == pred_end(DestBB)) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10250 | return false; |
| 10251 | |
| 10252 | if (*PI != StoreBB) { |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10253 | if (OtherBB) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10254 | return false; |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10255 | OtherBB = *PI; |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10256 | } |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10257 | if (++PI != pred_end(DestBB)) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10258 | return false; |
| 10259 | |
| 10260 | |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10261 | // Verify that the other block ends in a branch and is not otherwise empty. |
| 10262 | BasicBlock::iterator BBI = OtherBB->getTerminator(); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10263 | BranchInst *OtherBr = dyn_cast<BranchInst>(BBI); |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10264 | if (!OtherBr || BBI == OtherBB->begin()) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10265 | return false; |
| 10266 | |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10267 | // If the other block ends in an unconditional branch, check for the 'if then |
| 10268 | // else' case. there is an instruction before the branch. |
| 10269 | StoreInst *OtherStore = 0; |
| 10270 | if (OtherBr->isUnconditional()) { |
| 10271 | // If this isn't a store, or isn't a store to the same location, bail out. |
| 10272 | --BBI; |
| 10273 | OtherStore = dyn_cast<StoreInst>(BBI); |
| 10274 | if (!OtherStore || OtherStore->getOperand(1) != SI.getOperand(1)) |
| 10275 | return false; |
| 10276 | } else { |
Chris Lattner | d717c18 | 2007-05-05 22:32:24 +0000 | [diff] [blame] | 10277 | // 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] | 10278 | // destinations is StoreBB, then we have the if/then case. |
| 10279 | if (OtherBr->getSuccessor(0) != StoreBB && |
| 10280 | OtherBr->getSuccessor(1) != StoreBB) |
| 10281 | return false; |
| 10282 | |
| 10283 | // 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] | 10284 | // if/then triangle. See if there is a store to the same ptr as SI that |
| 10285 | // lives in OtherBB. |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10286 | for (;; --BBI) { |
| 10287 | // Check to see if we find the matching store. |
| 10288 | if ((OtherStore = dyn_cast<StoreInst>(BBI))) { |
| 10289 | if (OtherStore->getOperand(1) != SI.getOperand(1)) |
| 10290 | return false; |
| 10291 | break; |
| 10292 | } |
Chris Lattner | d717c18 | 2007-05-05 22:32:24 +0000 | [diff] [blame] | 10293 | // If we find something that may be using the stored value, or if we run |
| 10294 | // out of instructions, we can't do the xform. |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10295 | if (isa<LoadInst>(BBI) || BBI->mayWriteToMemory() || |
| 10296 | BBI == OtherBB->begin()) |
| 10297 | return false; |
| 10298 | } |
| 10299 | |
| 10300 | // In order to eliminate the store in OtherBr, we have to |
| 10301 | // make sure nothing reads the stored value in StoreBB. |
| 10302 | for (BasicBlock::iterator I = StoreBB->begin(); &*I != &SI; ++I) { |
| 10303 | // FIXME: This should really be AA driven. |
| 10304 | if (isa<LoadInst>(I) || I->mayWriteToMemory()) |
| 10305 | return false; |
| 10306 | } |
| 10307 | } |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10308 | |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10309 | // Insert a PHI node now if we need it. |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10310 | Value *MergedVal = OtherStore->getOperand(0); |
| 10311 | if (MergedVal != SI.getOperand(0)) { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 10312 | PHINode *PN = PHINode::Create(MergedVal->getType(), "storemerge"); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10313 | PN->reserveOperandSpace(2); |
| 10314 | PN->addIncoming(SI.getOperand(0), SI.getParent()); |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10315 | PN->addIncoming(OtherStore->getOperand(0), OtherBB); |
| 10316 | MergedVal = InsertNewInstBefore(PN, DestBB->front()); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10317 | } |
| 10318 | |
| 10319 | // Advance to a place where it is safe to insert the new store and |
| 10320 | // insert it. |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10321 | BBI = DestBB->begin(); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10322 | while (isa<PHINode>(BBI)) ++BBI; |
| 10323 | InsertNewInstBefore(new StoreInst(MergedVal, SI.getOperand(1), |
| 10324 | OtherStore->isVolatile()), *BBI); |
| 10325 | |
| 10326 | // Nuke the old stores. |
| 10327 | EraseInstFromFunction(SI); |
| 10328 | EraseInstFromFunction(*OtherStore); |
| 10329 | ++NumCombined; |
| 10330 | return true; |
| 10331 | } |
| 10332 | |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10333 | |
Chris Lattner | c4d10eb | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 10334 | Instruction *InstCombiner::visitBranchInst(BranchInst &BI) { |
| 10335 | // 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] | 10336 | Value *X = 0; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 10337 | BasicBlock *TrueDest; |
| 10338 | BasicBlock *FalseDest; |
| 10339 | if (match(&BI, m_Br(m_Not(m_Value(X)), TrueDest, FalseDest)) && |
| 10340 | !isa<Constant>(X)) { |
| 10341 | // Swap Destinations and condition... |
| 10342 | BI.setCondition(X); |
| 10343 | BI.setSuccessor(0, FalseDest); |
| 10344 | BI.setSuccessor(1, TrueDest); |
| 10345 | return &BI; |
| 10346 | } |
| 10347 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10348 | // Cannonicalize fcmp_one -> fcmp_oeq |
| 10349 | FCmpInst::Predicate FPred; Value *Y; |
| 10350 | if (match(&BI, m_Br(m_FCmp(FPred, m_Value(X), m_Value(Y)), |
| 10351 | TrueDest, FalseDest))) |
| 10352 | if ((FPred == FCmpInst::FCMP_ONE || FPred == FCmpInst::FCMP_OLE || |
| 10353 | FPred == FCmpInst::FCMP_OGE) && BI.getCondition()->hasOneUse()) { |
| 10354 | FCmpInst *I = cast<FCmpInst>(BI.getCondition()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10355 | FCmpInst::Predicate NewPred = FCmpInst::getInversePredicate(FPred); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 10356 | Instruction *NewSCC = new FCmpInst(NewPred, X, Y, "", I); |
| 10357 | NewSCC->takeName(I); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10358 | // Swap Destinations and condition... |
| 10359 | BI.setCondition(NewSCC); |
| 10360 | BI.setSuccessor(0, FalseDest); |
| 10361 | BI.setSuccessor(1, TrueDest); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10362 | RemoveFromWorkList(I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 10363 | I->eraseFromParent(); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10364 | AddToWorkList(NewSCC); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10365 | return &BI; |
| 10366 | } |
| 10367 | |
| 10368 | // Cannonicalize icmp_ne -> icmp_eq |
| 10369 | ICmpInst::Predicate IPred; |
| 10370 | if (match(&BI, m_Br(m_ICmp(IPred, m_Value(X), m_Value(Y)), |
| 10371 | TrueDest, FalseDest))) |
| 10372 | if ((IPred == ICmpInst::ICMP_NE || IPred == ICmpInst::ICMP_ULE || |
| 10373 | IPred == ICmpInst::ICMP_SLE || IPred == ICmpInst::ICMP_UGE || |
| 10374 | IPred == ICmpInst::ICMP_SGE) && BI.getCondition()->hasOneUse()) { |
| 10375 | ICmpInst *I = cast<ICmpInst>(BI.getCondition()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10376 | ICmpInst::Predicate NewPred = ICmpInst::getInversePredicate(IPred); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 10377 | Instruction *NewSCC = new ICmpInst(NewPred, X, Y, "", I); |
| 10378 | NewSCC->takeName(I); |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 10379 | // Swap Destinations and condition... |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 10380 | BI.setCondition(NewSCC); |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 10381 | BI.setSuccessor(0, FalseDest); |
| 10382 | BI.setSuccessor(1, TrueDest); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10383 | RemoveFromWorkList(I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 10384 | I->eraseFromParent();; |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10385 | AddToWorkList(NewSCC); |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 10386 | return &BI; |
| 10387 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 10388 | |
Chris Lattner | c4d10eb | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 10389 | return 0; |
| 10390 | } |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 10391 | |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 10392 | Instruction *InstCombiner::visitSwitchInst(SwitchInst &SI) { |
| 10393 | Value *Cond = SI.getCondition(); |
| 10394 | if (Instruction *I = dyn_cast<Instruction>(Cond)) { |
| 10395 | if (I->getOpcode() == Instruction::Add) |
| 10396 | if (ConstantInt *AddRHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 10397 | // change 'switch (X+4) case 1:' into 'switch (X) case -3' |
| 10398 | for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10399 | SI.setOperand(i,ConstantExpr::getSub(cast<Constant>(SI.getOperand(i)), |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 10400 | AddRHS)); |
| 10401 | SI.setOperand(0, I->getOperand(0)); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10402 | AddToWorkList(I); |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 10403 | return &SI; |
| 10404 | } |
| 10405 | } |
| 10406 | return 0; |
| 10407 | } |
| 10408 | |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10409 | /// CheapToScalarize - Return true if the value is cheaper to scalarize than it |
| 10410 | /// is to leave as a vector operation. |
| 10411 | static bool CheapToScalarize(Value *V, bool isConstant) { |
| 10412 | if (isa<ConstantAggregateZero>(V)) |
| 10413 | return true; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10414 | if (ConstantVector *C = dyn_cast<ConstantVector>(V)) { |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10415 | if (isConstant) return true; |
| 10416 | // If all elts are the same, we can extract. |
| 10417 | Constant *Op0 = C->getOperand(0); |
| 10418 | for (unsigned i = 1; i < C->getNumOperands(); ++i) |
| 10419 | if (C->getOperand(i) != Op0) |
| 10420 | return false; |
| 10421 | return true; |
| 10422 | } |
| 10423 | Instruction *I = dyn_cast<Instruction>(V); |
| 10424 | if (!I) return false; |
| 10425 | |
| 10426 | // Insert element gets simplified to the inserted element or is deleted if |
| 10427 | // this is constant idx extract element and its a constant idx insertelt. |
| 10428 | if (I->getOpcode() == Instruction::InsertElement && isConstant && |
| 10429 | isa<ConstantInt>(I->getOperand(2))) |
| 10430 | return true; |
| 10431 | if (I->getOpcode() == Instruction::Load && I->hasOneUse()) |
| 10432 | return true; |
| 10433 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) |
| 10434 | if (BO->hasOneUse() && |
| 10435 | (CheapToScalarize(BO->getOperand(0), isConstant) || |
| 10436 | CheapToScalarize(BO->getOperand(1), isConstant))) |
| 10437 | return true; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10438 | if (CmpInst *CI = dyn_cast<CmpInst>(I)) |
| 10439 | if (CI->hasOneUse() && |
| 10440 | (CheapToScalarize(CI->getOperand(0), isConstant) || |
| 10441 | CheapToScalarize(CI->getOperand(1), isConstant))) |
| 10442 | return true; |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10443 | |
| 10444 | return false; |
| 10445 | } |
| 10446 | |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 10447 | /// Read and decode a shufflevector mask. |
| 10448 | /// |
| 10449 | /// It turns undef elements into values that are larger than the number of |
| 10450 | /// elements in the input. |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10451 | static std::vector<unsigned> getShuffleMask(const ShuffleVectorInst *SVI) { |
| 10452 | unsigned NElts = SVI->getType()->getNumElements(); |
| 10453 | if (isa<ConstantAggregateZero>(SVI->getOperand(2))) |
| 10454 | return std::vector<unsigned>(NElts, 0); |
| 10455 | if (isa<UndefValue>(SVI->getOperand(2))) |
| 10456 | return std::vector<unsigned>(NElts, 2*NElts); |
| 10457 | |
| 10458 | std::vector<unsigned> Result; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10459 | const ConstantVector *CP = cast<ConstantVector>(SVI->getOperand(2)); |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10460 | for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) |
| 10461 | if (isa<UndefValue>(CP->getOperand(i))) |
| 10462 | Result.push_back(NElts*2); // undef -> 8 |
| 10463 | else |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10464 | Result.push_back(cast<ConstantInt>(CP->getOperand(i))->getZExtValue()); |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10465 | return Result; |
| 10466 | } |
| 10467 | |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10468 | /// FindScalarElement - Given a vector and an element number, see if the scalar |
| 10469 | /// value is already around as a register, for example if it were inserted then |
| 10470 | /// extracted from the vector. |
| 10471 | static Value *FindScalarElement(Value *V, unsigned EltNo) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10472 | assert(isa<VectorType>(V->getType()) && "Not looking at a vector?"); |
| 10473 | const VectorType *PTy = cast<VectorType>(V->getType()); |
Chris Lattner | 389a6f5 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 10474 | unsigned Width = PTy->getNumElements(); |
| 10475 | if (EltNo >= Width) // Out of range access. |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10476 | return UndefValue::get(PTy->getElementType()); |
| 10477 | |
| 10478 | if (isa<UndefValue>(V)) |
| 10479 | return UndefValue::get(PTy->getElementType()); |
| 10480 | else if (isa<ConstantAggregateZero>(V)) |
| 10481 | return Constant::getNullValue(PTy->getElementType()); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10482 | else if (ConstantVector *CP = dyn_cast<ConstantVector>(V)) |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10483 | return CP->getOperand(EltNo); |
| 10484 | else if (InsertElementInst *III = dyn_cast<InsertElementInst>(V)) { |
| 10485 | // 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] | 10486 | if (!isa<ConstantInt>(III->getOperand(2))) |
| 10487 | return 0; |
| 10488 | unsigned IIElt = cast<ConstantInt>(III->getOperand(2))->getZExtValue(); |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10489 | |
| 10490 | // If this is an insert to the element we are looking for, return the |
| 10491 | // inserted value. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10492 | if (EltNo == IIElt) |
| 10493 | return III->getOperand(1); |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10494 | |
| 10495 | // Otherwise, the insertelement doesn't modify the value, recurse on its |
| 10496 | // vector input. |
| 10497 | return FindScalarElement(III->getOperand(0), EltNo); |
Chris Lattner | 389a6f5 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 10498 | } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(V)) { |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10499 | unsigned InEl = getShuffleMask(SVI)[EltNo]; |
| 10500 | if (InEl < Width) |
| 10501 | return FindScalarElement(SVI->getOperand(0), InEl); |
| 10502 | else if (InEl < Width*2) |
| 10503 | return FindScalarElement(SVI->getOperand(1), InEl - Width); |
| 10504 | else |
| 10505 | return UndefValue::get(PTy->getElementType()); |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10506 | } |
| 10507 | |
| 10508 | // Otherwise, we don't know. |
| 10509 | return 0; |
| 10510 | } |
| 10511 | |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10512 | Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) { |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10513 | |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 10514 | // If vector val is undef, replace extract with scalar undef. |
Chris Lattner | 1f13c88 | 2006-03-31 18:25:14 +0000 | [diff] [blame] | 10515 | if (isa<UndefValue>(EI.getOperand(0))) |
| 10516 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); |
| 10517 | |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 10518 | // If vector val is constant 0, replace extract with scalar 0. |
Chris Lattner | 1f13c88 | 2006-03-31 18:25:14 +0000 | [diff] [blame] | 10519 | if (isa<ConstantAggregateZero>(EI.getOperand(0))) |
| 10520 | return ReplaceInstUsesWith(EI, Constant::getNullValue(EI.getType())); |
| 10521 | |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10522 | if (ConstantVector *C = dyn_cast<ConstantVector>(EI.getOperand(0))) { |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 10523 | // If vector val is constant with uniform operands, replace EI |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10524 | // with that operand |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10525 | Constant *op0 = C->getOperand(0); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10526 | for (unsigned i = 1; i < C->getNumOperands(); ++i) |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10527 | if (C->getOperand(i) != op0) { |
| 10528 | op0 = 0; |
| 10529 | break; |
| 10530 | } |
| 10531 | if (op0) |
| 10532 | return ReplaceInstUsesWith(EI, op0); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10533 | } |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10534 | |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10535 | // If extracting a specified index from the vector, see if we can recursively |
| 10536 | // find a previously computed scalar that was inserted into the vector. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10537 | if (ConstantInt *IdxC = dyn_cast<ConstantInt>(EI.getOperand(1))) { |
Chris Lattner | 8546409 | 2007-04-09 01:37:55 +0000 | [diff] [blame] | 10538 | unsigned IndexVal = IdxC->getZExtValue(); |
| 10539 | unsigned VectorWidth = |
| 10540 | cast<VectorType>(EI.getOperand(0)->getType())->getNumElements(); |
| 10541 | |
| 10542 | // If this is extracting an invalid index, turn this into undef, to avoid |
| 10543 | // crashing the code below. |
| 10544 | if (IndexVal >= VectorWidth) |
| 10545 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); |
| 10546 | |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 10547 | // This instruction only demands the single element from the input vector. |
| 10548 | // If the input vector has a single use, simplify it based on this use |
| 10549 | // property. |
Chris Lattner | 8546409 | 2007-04-09 01:37:55 +0000 | [diff] [blame] | 10550 | if (EI.getOperand(0)->hasOneUse() && VectorWidth != 1) { |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 10551 | uint64_t UndefElts; |
| 10552 | if (Value *V = SimplifyDemandedVectorElts(EI.getOperand(0), |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10553 | 1 << IndexVal, |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 10554 | UndefElts)) { |
| 10555 | EI.setOperand(0, V); |
| 10556 | return &EI; |
| 10557 | } |
| 10558 | } |
| 10559 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10560 | if (Value *Elt = FindScalarElement(EI.getOperand(0), IndexVal)) |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10561 | return ReplaceInstUsesWith(EI, Elt); |
Chris Lattner | b7300fa | 2007-04-14 23:02:14 +0000 | [diff] [blame] | 10562 | |
| 10563 | // If the this extractelement is directly using a bitcast from a vector of |
| 10564 | // the same number of elements, see if we can find the source element from |
| 10565 | // it. In this case, we will end up needing to bitcast the scalars. |
| 10566 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(EI.getOperand(0))) { |
| 10567 | if (const VectorType *VT = |
| 10568 | dyn_cast<VectorType>(BCI->getOperand(0)->getType())) |
| 10569 | if (VT->getNumElements() == VectorWidth) |
| 10570 | if (Value *Elt = FindScalarElement(BCI->getOperand(0), IndexVal)) |
| 10571 | return new BitCastInst(Elt, EI.getType()); |
| 10572 | } |
Chris Lattner | 389a6f5 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 10573 | } |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10574 | |
Chris Lattner | 73fa49d | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 10575 | if (Instruction *I = dyn_cast<Instruction>(EI.getOperand(0))) { |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10576 | if (I->hasOneUse()) { |
| 10577 | // Push extractelement into predecessor operation if legal and |
| 10578 | // profitable to do so |
| 10579 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) { |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10580 | bool isConstantElt = isa<ConstantInt>(EI.getOperand(1)); |
| 10581 | if (CheapToScalarize(BO, isConstantElt)) { |
| 10582 | ExtractElementInst *newEI0 = |
| 10583 | new ExtractElementInst(BO->getOperand(0), EI.getOperand(1), |
| 10584 | EI.getName()+".lhs"); |
| 10585 | ExtractElementInst *newEI1 = |
| 10586 | new ExtractElementInst(BO->getOperand(1), EI.getOperand(1), |
| 10587 | EI.getName()+".rhs"); |
| 10588 | InsertNewInstBefore(newEI0, EI); |
| 10589 | InsertNewInstBefore(newEI1, EI); |
| 10590 | return BinaryOperator::create(BO->getOpcode(), newEI0, newEI1); |
| 10591 | } |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 10592 | } else if (isa<LoadInst>(I)) { |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 10593 | unsigned AS = |
| 10594 | cast<PointerType>(I->getOperand(0)->getType())->getAddressSpace(); |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 10595 | Value *Ptr = InsertBitCastBefore(I->getOperand(0), |
| 10596 | PointerType::get(EI.getType(), AS),EI); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10597 | GetElementPtrInst *GEP = |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 10598 | GetElementPtrInst::Create(Ptr, EI.getOperand(1), I->getName() + ".gep"); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10599 | InsertNewInstBefore(GEP, EI); |
| 10600 | return new LoadInst(GEP); |
Chris Lattner | 73fa49d | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 10601 | } |
| 10602 | } |
| 10603 | if (InsertElementInst *IE = dyn_cast<InsertElementInst>(I)) { |
| 10604 | // Extracting the inserted element? |
| 10605 | if (IE->getOperand(2) == EI.getOperand(1)) |
| 10606 | return ReplaceInstUsesWith(EI, IE->getOperand(1)); |
| 10607 | // If the inserted and extracted elements are constants, they must not |
| 10608 | // be the same value, extract from the pre-inserted value instead. |
| 10609 | if (isa<Constant>(IE->getOperand(2)) && |
| 10610 | isa<Constant>(EI.getOperand(1))) { |
| 10611 | AddUsesToWorkList(EI); |
| 10612 | EI.setOperand(0, IE->getOperand(0)); |
| 10613 | return &EI; |
| 10614 | } |
| 10615 | } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(I)) { |
| 10616 | // If this is extracting an element from a shufflevector, figure out where |
| 10617 | // it came from and extract from the appropriate input element instead. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10618 | if (ConstantInt *Elt = dyn_cast<ConstantInt>(EI.getOperand(1))) { |
| 10619 | unsigned SrcIdx = getShuffleMask(SVI)[Elt->getZExtValue()]; |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10620 | Value *Src; |
| 10621 | if (SrcIdx < SVI->getType()->getNumElements()) |
| 10622 | Src = SVI->getOperand(0); |
| 10623 | else if (SrcIdx < SVI->getType()->getNumElements()*2) { |
| 10624 | SrcIdx -= SVI->getType()->getNumElements(); |
| 10625 | Src = SVI->getOperand(1); |
| 10626 | } else { |
| 10627 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); |
Chris Lattner | df084ff | 2006-03-30 22:02:40 +0000 | [diff] [blame] | 10628 | } |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 10629 | return new ExtractElementInst(Src, SrcIdx); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10630 | } |
| 10631 | } |
Chris Lattner | 73fa49d | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 10632 | } |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10633 | return 0; |
| 10634 | } |
| 10635 | |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10636 | /// CollectSingleShuffleElements - If V is a shuffle of values that ONLY returns |
| 10637 | /// elements from either LHS or RHS, return the shuffle mask and true. |
| 10638 | /// Otherwise, return false. |
| 10639 | static bool CollectSingleShuffleElements(Value *V, Value *LHS, Value *RHS, |
| 10640 | std::vector<Constant*> &Mask) { |
| 10641 | assert(V->getType() == LHS->getType() && V->getType() == RHS->getType() && |
| 10642 | "Invalid CollectSingleShuffleElements"); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10643 | unsigned NumElts = cast<VectorType>(V->getType())->getNumElements(); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10644 | |
| 10645 | if (isa<UndefValue>(V)) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10646 | Mask.assign(NumElts, UndefValue::get(Type::Int32Ty)); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10647 | return true; |
| 10648 | } else if (V == LHS) { |
| 10649 | for (unsigned i = 0; i != NumElts; ++i) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10650 | Mask.push_back(ConstantInt::get(Type::Int32Ty, i)); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10651 | return true; |
| 10652 | } else if (V == RHS) { |
| 10653 | for (unsigned i = 0; i != NumElts; ++i) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10654 | Mask.push_back(ConstantInt::get(Type::Int32Ty, i+NumElts)); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10655 | return true; |
| 10656 | } else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) { |
| 10657 | // If this is an insert of an extract from some other vector, include it. |
| 10658 | Value *VecOp = IEI->getOperand(0); |
| 10659 | Value *ScalarOp = IEI->getOperand(1); |
| 10660 | Value *IdxOp = IEI->getOperand(2); |
| 10661 | |
Chris Lattner | d929f06 | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 10662 | if (!isa<ConstantInt>(IdxOp)) |
| 10663 | return false; |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10664 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Chris Lattner | d929f06 | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 10665 | |
| 10666 | if (isa<UndefValue>(ScalarOp)) { // inserting undef into vector. |
| 10667 | // Okay, we can handle this if the vector we are insertinting into is |
| 10668 | // transitively ok. |
| 10669 | if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) { |
| 10670 | // If so, update the mask to reflect the inserted undef. |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10671 | Mask[InsertedIdx] = UndefValue::get(Type::Int32Ty); |
Chris Lattner | d929f06 | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 10672 | return true; |
| 10673 | } |
| 10674 | } else if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)){ |
| 10675 | if (isa<ConstantInt>(EI->getOperand(1)) && |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10676 | EI->getOperand(0)->getType() == V->getType()) { |
| 10677 | unsigned ExtractedIdx = |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10678 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10679 | |
| 10680 | // This must be extracting from either LHS or RHS. |
| 10681 | if (EI->getOperand(0) == LHS || EI->getOperand(0) == RHS) { |
| 10682 | // Okay, we can handle this if the vector we are insertinting into is |
| 10683 | // transitively ok. |
| 10684 | if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) { |
| 10685 | // If so, update the mask to reflect the inserted value. |
| 10686 | if (EI->getOperand(0) == LHS) { |
| 10687 | Mask[InsertedIdx & (NumElts-1)] = |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10688 | ConstantInt::get(Type::Int32Ty, ExtractedIdx); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10689 | } else { |
| 10690 | assert(EI->getOperand(0) == RHS); |
| 10691 | Mask[InsertedIdx & (NumElts-1)] = |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10692 | ConstantInt::get(Type::Int32Ty, ExtractedIdx+NumElts); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10693 | |
| 10694 | } |
| 10695 | return true; |
| 10696 | } |
| 10697 | } |
| 10698 | } |
| 10699 | } |
| 10700 | } |
| 10701 | // TODO: Handle shufflevector here! |
| 10702 | |
| 10703 | return false; |
| 10704 | } |
| 10705 | |
| 10706 | /// CollectShuffleElements - We are building a shuffle of V, using RHS as the |
| 10707 | /// RHS of the shuffle instruction, if it is not null. Return a shuffle mask |
| 10708 | /// that computes V and the LHS value of the shuffle. |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10709 | static Value *CollectShuffleElements(Value *V, std::vector<Constant*> &Mask, |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10710 | Value *&RHS) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10711 | assert(isa<VectorType>(V->getType()) && |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10712 | (RHS == 0 || V->getType() == RHS->getType()) && |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10713 | "Invalid shuffle!"); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10714 | unsigned NumElts = cast<VectorType>(V->getType())->getNumElements(); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10715 | |
| 10716 | if (isa<UndefValue>(V)) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10717 | Mask.assign(NumElts, UndefValue::get(Type::Int32Ty)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10718 | return V; |
| 10719 | } else if (isa<ConstantAggregateZero>(V)) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10720 | Mask.assign(NumElts, ConstantInt::get(Type::Int32Ty, 0)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10721 | return V; |
| 10722 | } else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) { |
| 10723 | // If this is an insert of an extract from some other vector, include it. |
| 10724 | Value *VecOp = IEI->getOperand(0); |
| 10725 | Value *ScalarOp = IEI->getOperand(1); |
| 10726 | Value *IdxOp = IEI->getOperand(2); |
| 10727 | |
| 10728 | if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) { |
| 10729 | if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) && |
| 10730 | EI->getOperand(0)->getType() == V->getType()) { |
| 10731 | unsigned ExtractedIdx = |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10732 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
| 10733 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10734 | |
| 10735 | // Either the extracted from or inserted into vector must be RHSVec, |
| 10736 | // otherwise we'd end up with a shuffle of three inputs. |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10737 | if (EI->getOperand(0) == RHS || RHS == 0) { |
| 10738 | RHS = EI->getOperand(0); |
| 10739 | Value *V = CollectShuffleElements(VecOp, Mask, RHS); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10740 | Mask[InsertedIdx & (NumElts-1)] = |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10741 | ConstantInt::get(Type::Int32Ty, NumElts+ExtractedIdx); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10742 | return V; |
| 10743 | } |
| 10744 | |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10745 | if (VecOp == RHS) { |
| 10746 | Value *V = CollectShuffleElements(EI->getOperand(0), Mask, RHS); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10747 | // Everything but the extracted element is replaced with the RHS. |
| 10748 | for (unsigned i = 0; i != NumElts; ++i) { |
| 10749 | if (i != InsertedIdx) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10750 | Mask[i] = ConstantInt::get(Type::Int32Ty, NumElts+i); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10751 | } |
| 10752 | return V; |
| 10753 | } |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10754 | |
| 10755 | // If this insertelement is a chain that comes from exactly these two |
| 10756 | // vectors, return the vector and the effective shuffle. |
| 10757 | if (CollectSingleShuffleElements(IEI, EI->getOperand(0), RHS, Mask)) |
| 10758 | return EI->getOperand(0); |
| 10759 | |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10760 | } |
| 10761 | } |
| 10762 | } |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10763 | // TODO: Handle shufflevector here! |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10764 | |
| 10765 | // Otherwise, can't do anything fancy. Return an identity vector. |
| 10766 | for (unsigned i = 0; i != NumElts; ++i) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10767 | Mask.push_back(ConstantInt::get(Type::Int32Ty, i)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10768 | return V; |
| 10769 | } |
| 10770 | |
| 10771 | Instruction *InstCombiner::visitInsertElementInst(InsertElementInst &IE) { |
| 10772 | Value *VecOp = IE.getOperand(0); |
| 10773 | Value *ScalarOp = IE.getOperand(1); |
| 10774 | Value *IdxOp = IE.getOperand(2); |
| 10775 | |
Chris Lattner | 599ded1 | 2007-04-09 01:11:16 +0000 | [diff] [blame] | 10776 | // Inserting an undef or into an undefined place, remove this. |
| 10777 | if (isa<UndefValue>(ScalarOp) || isa<UndefValue>(IdxOp)) |
| 10778 | ReplaceInstUsesWith(IE, VecOp); |
| 10779 | |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10780 | // If the inserted element was extracted from some other vector, and if the |
| 10781 | // indexes are constant, try to turn this into a shufflevector operation. |
| 10782 | if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) { |
| 10783 | if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) && |
| 10784 | EI->getOperand(0)->getType() == IE.getType()) { |
| 10785 | unsigned NumVectorElts = IE.getType()->getNumElements(); |
Chris Lattner | e34e9a2 | 2007-04-14 23:32:02 +0000 | [diff] [blame] | 10786 | unsigned ExtractedIdx = |
| 10787 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10788 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10789 | |
| 10790 | if (ExtractedIdx >= NumVectorElts) // Out of range extract. |
| 10791 | return ReplaceInstUsesWith(IE, VecOp); |
| 10792 | |
| 10793 | if (InsertedIdx >= NumVectorElts) // Out of range insert. |
| 10794 | return ReplaceInstUsesWith(IE, UndefValue::get(IE.getType())); |
| 10795 | |
| 10796 | // If we are extracting a value from a vector, then inserting it right |
| 10797 | // back into the same place, just use the input vector. |
| 10798 | if (EI->getOperand(0) == VecOp && ExtractedIdx == InsertedIdx) |
| 10799 | return ReplaceInstUsesWith(IE, VecOp); |
| 10800 | |
| 10801 | // We could theoretically do this for ANY input. However, doing so could |
| 10802 | // turn chains of insertelement instructions into a chain of shufflevector |
| 10803 | // instructions, and right now we do not merge shufflevectors. As such, |
| 10804 | // only do this in a situation where it is clear that there is benefit. |
| 10805 | if (isa<UndefValue>(VecOp) || isa<ConstantAggregateZero>(VecOp)) { |
| 10806 | // Turn this into shuffle(EIOp0, VecOp, Mask). The result has all of |
| 10807 | // the values of VecOp, except then one read from EIOp0. |
| 10808 | // Build a new shuffle mask. |
| 10809 | std::vector<Constant*> Mask; |
| 10810 | if (isa<UndefValue>(VecOp)) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10811 | Mask.assign(NumVectorElts, UndefValue::get(Type::Int32Ty)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10812 | else { |
| 10813 | assert(isa<ConstantAggregateZero>(VecOp) && "Unknown thing"); |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10814 | Mask.assign(NumVectorElts, ConstantInt::get(Type::Int32Ty, |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10815 | NumVectorElts)); |
| 10816 | } |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10817 | Mask[InsertedIdx] = ConstantInt::get(Type::Int32Ty, ExtractedIdx); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10818 | return new ShuffleVectorInst(EI->getOperand(0), VecOp, |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10819 | ConstantVector::get(Mask)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10820 | } |
| 10821 | |
| 10822 | // If this insertelement isn't used by some other insertelement, turn it |
| 10823 | // (and any insertelements it points to), into one big shuffle. |
| 10824 | if (!IE.hasOneUse() || !isa<InsertElementInst>(IE.use_back())) { |
| 10825 | std::vector<Constant*> Mask; |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10826 | Value *RHS = 0; |
| 10827 | Value *LHS = CollectShuffleElements(&IE, Mask, RHS); |
| 10828 | if (RHS == 0) RHS = UndefValue::get(LHS->getType()); |
| 10829 | // We now have a shuffle of LHS, RHS, Mask. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10830 | return new ShuffleVectorInst(LHS, RHS, ConstantVector::get(Mask)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10831 | } |
| 10832 | } |
| 10833 | } |
| 10834 | |
| 10835 | return 0; |
| 10836 | } |
| 10837 | |
| 10838 | |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 10839 | Instruction *InstCombiner::visitShuffleVectorInst(ShuffleVectorInst &SVI) { |
| 10840 | Value *LHS = SVI.getOperand(0); |
| 10841 | Value *RHS = SVI.getOperand(1); |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10842 | std::vector<unsigned> Mask = getShuffleMask(&SVI); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 10843 | |
| 10844 | bool MadeChange = false; |
| 10845 | |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 10846 | // Undefined shuffle mask -> undefined value. |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10847 | if (isa<UndefValue>(SVI.getOperand(2))) |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 10848 | return ReplaceInstUsesWith(SVI, UndefValue::get(SVI.getType())); |
| 10849 | |
Chris Lattner | e4929dd | 2007-01-05 07:36:08 +0000 | [diff] [blame] | 10850 | // 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] | 10851 | // the undef, change them to undefs. |
Chris Lattner | e4929dd | 2007-01-05 07:36:08 +0000 | [diff] [blame] | 10852 | if (isa<UndefValue>(SVI.getOperand(1))) { |
| 10853 | // Scan to see if there are any references to the RHS. If so, replace them |
| 10854 | // with undef element refs and set MadeChange to true. |
| 10855 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
| 10856 | if (Mask[i] >= e && Mask[i] != 2*e) { |
| 10857 | Mask[i] = 2*e; |
| 10858 | MadeChange = true; |
| 10859 | } |
| 10860 | } |
| 10861 | |
| 10862 | if (MadeChange) { |
| 10863 | // Remap any references to RHS to use LHS. |
| 10864 | std::vector<Constant*> Elts; |
| 10865 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
| 10866 | if (Mask[i] == 2*e) |
| 10867 | Elts.push_back(UndefValue::get(Type::Int32Ty)); |
| 10868 | else |
| 10869 | Elts.push_back(ConstantInt::get(Type::Int32Ty, Mask[i])); |
| 10870 | } |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10871 | SVI.setOperand(2, ConstantVector::get(Elts)); |
Chris Lattner | e4929dd | 2007-01-05 07:36:08 +0000 | [diff] [blame] | 10872 | } |
| 10873 | } |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10874 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10875 | // Canonicalize shuffle(x ,x,mask) -> shuffle(x, undef,mask') |
| 10876 | // Canonicalize shuffle(undef,x,mask) -> shuffle(x, undef,mask'). |
| 10877 | if (LHS == RHS || isa<UndefValue>(LHS)) { |
| 10878 | if (isa<UndefValue>(LHS) && LHS == RHS) { |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 10879 | // shuffle(undef,undef,mask) -> undef. |
| 10880 | return ReplaceInstUsesWith(SVI, LHS); |
| 10881 | } |
| 10882 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10883 | // Remap any references to RHS to use LHS. |
| 10884 | std::vector<Constant*> Elts; |
| 10885 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 10886 | if (Mask[i] >= 2*e) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10887 | Elts.push_back(UndefValue::get(Type::Int32Ty)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 10888 | else { |
| 10889 | if ((Mask[i] >= e && isa<UndefValue>(RHS)) || |
| 10890 | (Mask[i] < e && isa<UndefValue>(LHS))) |
| 10891 | Mask[i] = 2*e; // Turn into undef. |
| 10892 | else |
| 10893 | Mask[i] &= (e-1); // Force to LHS. |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10894 | Elts.push_back(ConstantInt::get(Type::Int32Ty, Mask[i])); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 10895 | } |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 10896 | } |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10897 | SVI.setOperand(0, SVI.getOperand(1)); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 10898 | SVI.setOperand(1, UndefValue::get(RHS->getType())); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10899 | SVI.setOperand(2, ConstantVector::get(Elts)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 10900 | LHS = SVI.getOperand(0); |
| 10901 | RHS = SVI.getOperand(1); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 10902 | MadeChange = true; |
| 10903 | } |
| 10904 | |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 10905 | // Analyze the shuffle, are the LHS or RHS and identity shuffles? |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10906 | bool isLHSID = true, isRHSID = true; |
Chris Lattner | 706126d | 2006-04-16 00:03:56 +0000 | [diff] [blame] | 10907 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10908 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
| 10909 | if (Mask[i] >= e*2) continue; // Ignore undef values. |
| 10910 | // Is this an identity shuffle of the LHS value? |
| 10911 | isLHSID &= (Mask[i] == i); |
| 10912 | |
| 10913 | // Is this an identity shuffle of the RHS value? |
| 10914 | isRHSID &= (Mask[i]-e == i); |
Chris Lattner | 706126d | 2006-04-16 00:03:56 +0000 | [diff] [blame] | 10915 | } |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 10916 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10917 | // Eliminate identity shuffles. |
| 10918 | if (isLHSID) return ReplaceInstUsesWith(SVI, LHS); |
| 10919 | if (isRHSID) return ReplaceInstUsesWith(SVI, RHS); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 10920 | |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 10921 | // If the LHS is a shufflevector itself, see if we can combine it with this |
| 10922 | // one without producing an unusual shuffle. Here we are really conservative: |
| 10923 | // we are absolutely afraid of producing a shuffle mask not in the input |
| 10924 | // program, because the code gen may not be smart enough to turn a merged |
| 10925 | // shuffle into two specific shuffles: it may produce worse code. As such, |
| 10926 | // we only merge two shuffles if the result is one of the two input shuffle |
| 10927 | // masks. In this case, merging the shuffles just removes one instruction, |
| 10928 | // which we know is safe. This is good for things like turning: |
| 10929 | // (splat(splat)) -> splat. |
| 10930 | if (ShuffleVectorInst *LHSSVI = dyn_cast<ShuffleVectorInst>(LHS)) { |
| 10931 | if (isa<UndefValue>(RHS)) { |
| 10932 | std::vector<unsigned> LHSMask = getShuffleMask(LHSSVI); |
| 10933 | |
| 10934 | std::vector<unsigned> NewMask; |
| 10935 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) |
| 10936 | if (Mask[i] >= 2*e) |
| 10937 | NewMask.push_back(2*e); |
| 10938 | else |
| 10939 | NewMask.push_back(LHSMask[Mask[i]]); |
| 10940 | |
| 10941 | // If the result mask is equal to the src shuffle or this shuffle mask, do |
| 10942 | // the replacement. |
| 10943 | if (NewMask == LHSMask || NewMask == Mask) { |
| 10944 | std::vector<Constant*> Elts; |
| 10945 | for (unsigned i = 0, e = NewMask.size(); i != e; ++i) { |
| 10946 | if (NewMask[i] >= e*2) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10947 | Elts.push_back(UndefValue::get(Type::Int32Ty)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 10948 | } else { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10949 | Elts.push_back(ConstantInt::get(Type::Int32Ty, NewMask[i])); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 10950 | } |
| 10951 | } |
| 10952 | return new ShuffleVectorInst(LHSSVI->getOperand(0), |
| 10953 | LHSSVI->getOperand(1), |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10954 | ConstantVector::get(Elts)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 10955 | } |
| 10956 | } |
| 10957 | } |
Chris Lattner | c5eff44 | 2007-01-30 22:32:46 +0000 | [diff] [blame] | 10958 | |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 10959 | return MadeChange ? &SVI : 0; |
| 10960 | } |
| 10961 | |
| 10962 | |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10963 | |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 10964 | |
| 10965 | /// TryToSinkInstruction - Try to move the specified instruction from its |
| 10966 | /// current block into the beginning of DestBlock, which can only happen if it's |
| 10967 | /// safe to move the instruction past all of the instructions between it and the |
| 10968 | /// end of its block. |
| 10969 | static bool TryToSinkInstruction(Instruction *I, BasicBlock *DestBlock) { |
| 10970 | assert(I->hasOneUse() && "Invariants didn't hold!"); |
| 10971 | |
Chris Lattner | 108e902 | 2005-10-27 17:13:11 +0000 | [diff] [blame] | 10972 | // Cannot move control-flow-involving, volatile loads, vaarg, etc. |
| 10973 | if (isa<PHINode>(I) || I->mayWriteToMemory()) return false; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 10974 | |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 10975 | // Do not sink alloca instructions out of the entry block. |
Dan Gohman | ecb7a77 | 2007-03-22 16:38:57 +0000 | [diff] [blame] | 10976 | if (isa<AllocaInst>(I) && I->getParent() == |
| 10977 | &DestBlock->getParent()->getEntryBlock()) |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 10978 | return false; |
| 10979 | |
Chris Lattner | 96a52a6 | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 10980 | // We can only sink load instructions if there is nothing between the load and |
| 10981 | // the end of block that could change the value. |
| 10982 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Chris Lattner | 96a52a6 | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 10983 | for (BasicBlock::iterator Scan = LI, E = LI->getParent()->end(); |
| 10984 | Scan != E; ++Scan) |
| 10985 | if (Scan->mayWriteToMemory()) |
| 10986 | return false; |
Chris Lattner | 96a52a6 | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 10987 | } |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 10988 | |
| 10989 | BasicBlock::iterator InsertPos = DestBlock->begin(); |
| 10990 | while (isa<PHINode>(InsertPos)) ++InsertPos; |
| 10991 | |
Chris Lattner | 4bc5f80 | 2005-08-08 19:11:57 +0000 | [diff] [blame] | 10992 | I->moveBefore(InsertPos); |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 10993 | ++NumSunkInst; |
| 10994 | return true; |
| 10995 | } |
| 10996 | |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 10997 | |
| 10998 | /// AddReachableCodeToWorklist - Walk the function in depth-first order, adding |
| 10999 | /// all reachable code to the worklist. |
| 11000 | /// |
| 11001 | /// This has a couple of tricks to make the code faster and more powerful. In |
| 11002 | /// particular, we constant fold and DCE instructions as we go, to avoid adding |
| 11003 | /// them to the worklist (this significantly speeds up instcombine on code where |
| 11004 | /// many instructions are dead or constant). Additionally, if we find a branch |
| 11005 | /// whose condition is a known constant, we only visit the reachable successors. |
| 11006 | /// |
| 11007 | static void AddReachableCodeToWorklist(BasicBlock *BB, |
Chris Lattner | 1f87a58 | 2007-02-15 19:41:52 +0000 | [diff] [blame] | 11008 | SmallPtrSet<BasicBlock*, 64> &Visited, |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11009 | InstCombiner &IC, |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 11010 | const TargetData *TD) { |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 11011 | std::vector<BasicBlock*> Worklist; |
| 11012 | Worklist.push_back(BB); |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 11013 | |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 11014 | while (!Worklist.empty()) { |
| 11015 | BB = Worklist.back(); |
| 11016 | Worklist.pop_back(); |
| 11017 | |
| 11018 | // We have now visited this block! If we've already been here, ignore it. |
| 11019 | if (!Visited.insert(BB)) continue; |
| 11020 | |
| 11021 | for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ) { |
| 11022 | Instruction *Inst = BBI++; |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 11023 | |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 11024 | // DCE instruction if trivially dead. |
| 11025 | if (isInstructionTriviallyDead(Inst)) { |
| 11026 | ++NumDeadInst; |
| 11027 | DOUT << "IC: DCE: " << *Inst; |
| 11028 | Inst->eraseFromParent(); |
| 11029 | continue; |
| 11030 | } |
| 11031 | |
| 11032 | // ConstantProp instruction if trivially constant. |
| 11033 | if (Constant *C = ConstantFoldInstruction(Inst, TD)) { |
| 11034 | DOUT << "IC: ConstFold to: " << *C << " from: " << *Inst; |
| 11035 | Inst->replaceAllUsesWith(C); |
| 11036 | ++NumConstProp; |
| 11037 | Inst->eraseFromParent(); |
| 11038 | continue; |
| 11039 | } |
Chris Lattner | 3ccc6bc | 2007-07-20 22:06:41 +0000 | [diff] [blame] | 11040 | |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 11041 | IC.AddToWorkList(Inst); |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 11042 | } |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 11043 | |
| 11044 | // Recursively visit successors. If this is a branch or switch on a |
| 11045 | // constant, only visit the reachable successor. |
Nick Lewycky | 9143699 | 2008-03-09 08:50:23 +0000 | [diff] [blame] | 11046 | if (BB->getUnwindDest()) |
| 11047 | Worklist.push_back(BB->getUnwindDest()); |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 11048 | TerminatorInst *TI = BB->getTerminator(); |
| 11049 | if (BranchInst *BI = dyn_cast<BranchInst>(TI)) { |
| 11050 | if (BI->isConditional() && isa<ConstantInt>(BI->getCondition())) { |
| 11051 | bool CondVal = cast<ConstantInt>(BI->getCondition())->getZExtValue(); |
Nick Lewycky | 9143699 | 2008-03-09 08:50:23 +0000 | [diff] [blame] | 11052 | BasicBlock *ReachableBB = BI->getSuccessor(!CondVal); |
| 11053 | if (ReachableBB != BB->getUnwindDest()) |
| 11054 | Worklist.push_back(ReachableBB); |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 11055 | continue; |
| 11056 | } |
| 11057 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) { |
| 11058 | if (ConstantInt *Cond = dyn_cast<ConstantInt>(SI->getCondition())) { |
| 11059 | // See if this is an explicit destination. |
| 11060 | for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i) |
| 11061 | if (SI->getCaseValue(i) == Cond) { |
Nick Lewycky | 9143699 | 2008-03-09 08:50:23 +0000 | [diff] [blame] | 11062 | BasicBlock *ReachableBB = SI->getSuccessor(i); |
| 11063 | if (ReachableBB != BB->getUnwindDest()) |
| 11064 | Worklist.push_back(ReachableBB); |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 11065 | continue; |
| 11066 | } |
| 11067 | |
| 11068 | // Otherwise it is the default destination. |
| 11069 | Worklist.push_back(SI->getSuccessor(0)); |
| 11070 | continue; |
| 11071 | } |
| 11072 | } |
| 11073 | |
| 11074 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) |
| 11075 | Worklist.push_back(TI->getSuccessor(i)); |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 11076 | } |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 11077 | } |
| 11078 | |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 11079 | bool InstCombiner::DoOneIteration(Function &F, unsigned Iteration) { |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 11080 | bool Changed = false; |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 11081 | TD = &getAnalysis<TargetData>(); |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 11082 | |
| 11083 | DEBUG(DOUT << "\n\nINSTCOMBINE ITERATION #" << Iteration << " on " |
| 11084 | << F.getNameStr() << "\n"); |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 11085 | |
Chris Lattner | b3d5970 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 11086 | { |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 11087 | // Do a depth-first traversal of the function, populate the worklist with |
| 11088 | // the reachable instructions. Ignore blocks that are not reachable. Keep |
| 11089 | // track of which blocks we visit. |
Chris Lattner | 1f87a58 | 2007-02-15 19:41:52 +0000 | [diff] [blame] | 11090 | SmallPtrSet<BasicBlock*, 64> Visited; |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11091 | AddReachableCodeToWorklist(F.begin(), Visited, *this, TD); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 11092 | |
Chris Lattner | b3d5970 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 11093 | // Do a quick scan over the function. If we find any blocks that are |
| 11094 | // unreachable, remove any instructions inside of them. This prevents |
| 11095 | // the instcombine code from having to deal with some bad special cases. |
| 11096 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 11097 | if (!Visited.count(BB)) { |
| 11098 | Instruction *Term = BB->getTerminator(); |
| 11099 | while (Term != BB->begin()) { // Remove instrs bottom-up |
| 11100 | BasicBlock::iterator I = Term; --I; |
Chris Lattner | 6ffe551 | 2004-04-27 15:13:33 +0000 | [diff] [blame] | 11101 | |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 11102 | DOUT << "IC: DCE: " << *I; |
Chris Lattner | b3d5970 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 11103 | ++NumDeadInst; |
| 11104 | |
| 11105 | if (!I->use_empty()) |
| 11106 | I->replaceAllUsesWith(UndefValue::get(I->getType())); |
| 11107 | I->eraseFromParent(); |
| 11108 | } |
| 11109 | } |
| 11110 | } |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 11111 | |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11112 | while (!Worklist.empty()) { |
| 11113 | Instruction *I = RemoveOneFromWorkList(); |
| 11114 | if (I == 0) continue; // skip null values. |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 11115 | |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 11116 | // Check to see if we can DCE the instruction. |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 11117 | if (isInstructionTriviallyDead(I)) { |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 11118 | // Add operands to the worklist. |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11119 | if (I->getNumOperands() < 4) |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 11120 | AddUsesToWorkList(*I); |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 11121 | ++NumDeadInst; |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11122 | |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 11123 | DOUT << "IC: DCE: " << *I; |
Chris Lattner | ad5fec1 | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 11124 | |
| 11125 | I->eraseFromParent(); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11126 | RemoveFromWorkList(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11127 | continue; |
| 11128 | } |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 11129 | |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 11130 | // Instruction isn't dead, see if we can constant propagate it. |
Chris Lattner | 0a19ffa | 2007-01-30 23:16:15 +0000 | [diff] [blame] | 11131 | if (Constant *C = ConstantFoldInstruction(I, TD)) { |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 11132 | DOUT << "IC: ConstFold to: " << *C << " from: " << *I; |
Chris Lattner | ad5fec1 | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 11133 | |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 11134 | // Add operands to the worklist. |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 11135 | AddUsesToWorkList(*I); |
Chris Lattner | c736d56 | 2002-12-05 22:41:53 +0000 | [diff] [blame] | 11136 | ReplaceInstUsesWith(*I, C); |
| 11137 | |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 11138 | ++NumConstProp; |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 11139 | I->eraseFromParent(); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11140 | RemoveFromWorkList(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11141 | continue; |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 11142 | } |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11143 | |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 11144 | // See if we can trivially sink this instruction to a successor basic block. |
| 11145 | if (I->hasOneUse()) { |
| 11146 | BasicBlock *BB = I->getParent(); |
| 11147 | BasicBlock *UserParent = cast<Instruction>(I->use_back())->getParent(); |
| 11148 | if (UserParent != BB) { |
| 11149 | bool UserIsSuccessor = false; |
| 11150 | // See if the user is one of our successors. |
| 11151 | for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI) |
| 11152 | if (*SI == UserParent) { |
| 11153 | UserIsSuccessor = true; |
| 11154 | break; |
| 11155 | } |
| 11156 | |
| 11157 | // If the user is one of our immediate successors, and if that successor |
| 11158 | // only has us as a predecessors (we'd have to split the critical edge |
| 11159 | // otherwise), we can keep going. |
| 11160 | if (UserIsSuccessor && !isa<PHINode>(I->use_back()) && |
| 11161 | next(pred_begin(UserParent)) == pred_end(UserParent)) |
| 11162 | // Okay, the CFG is simple enough, try to sink this instruction. |
| 11163 | Changed |= TryToSinkInstruction(I, UserParent); |
| 11164 | } |
| 11165 | } |
| 11166 | |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 11167 | // Now that we have an instruction, try combining it to simplify it... |
Reid Spencer | a9b8101 | 2007-03-26 17:44:01 +0000 | [diff] [blame] | 11168 | #ifndef NDEBUG |
| 11169 | std::string OrigI; |
| 11170 | #endif |
| 11171 | DEBUG(std::ostringstream SS; I->print(SS); OrigI = SS.str();); |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 11172 | if (Instruction *Result = visit(*I)) { |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 11173 | ++NumCombined; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 11174 | // Should we replace the old instruction with a new one? |
Chris Lattner | b3bc8fa | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 11175 | if (Result != I) { |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 11176 | DOUT << "IC: Old = " << *I |
| 11177 | << " New = " << *Result; |
Chris Lattner | 0cea42a | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 11178 | |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 11179 | // Everything uses the new instruction now. |
| 11180 | I->replaceAllUsesWith(Result); |
| 11181 | |
| 11182 | // Push the new instruction and any users onto the worklist. |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11183 | AddToWorkList(Result); |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 11184 | AddUsersToWorkList(*Result); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11185 | |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 11186 | // Move the name to the new instruction first. |
| 11187 | Result->takeName(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11188 | |
| 11189 | // Insert the new instruction into the basic block... |
| 11190 | BasicBlock *InstParent = I->getParent(); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 11191 | BasicBlock::iterator InsertPos = I; |
| 11192 | |
| 11193 | if (!isa<PHINode>(Result)) // If combining a PHI, don't insert |
| 11194 | while (isa<PHINode>(InsertPos)) // middle of a block of PHIs. |
| 11195 | ++InsertPos; |
| 11196 | |
| 11197 | InstParent->getInstList().insert(InsertPos, Result); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11198 | |
Chris Lattner | 00d5131 | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 11199 | // Make sure that we reprocess all operands now that we reduced their |
| 11200 | // use counts. |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11201 | AddUsesToWorkList(*I); |
Chris Lattner | 216d4d8 | 2004-05-01 23:19:52 +0000 | [diff] [blame] | 11202 | |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 11203 | // Instructions can end up on the worklist more than once. Make sure |
| 11204 | // we do not process an instruction that has been deleted. |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11205 | RemoveFromWorkList(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11206 | |
| 11207 | // Erase the old instruction. |
| 11208 | InstParent->getInstList().erase(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 11209 | } else { |
Evan Cheng | c7baf68 | 2007-03-27 16:44:48 +0000 | [diff] [blame] | 11210 | #ifndef NDEBUG |
Reid Spencer | a9b8101 | 2007-03-26 17:44:01 +0000 | [diff] [blame] | 11211 | DOUT << "IC: Mod = " << OrigI |
| 11212 | << " New = " << *I; |
Evan Cheng | c7baf68 | 2007-03-27 16:44:48 +0000 | [diff] [blame] | 11213 | #endif |
Chris Lattner | 0cea42a | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 11214 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 11215 | // If the instruction was modified, it's possible that it is now dead. |
| 11216 | // if so, remove it. |
Chris Lattner | 00d5131 | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 11217 | if (isInstructionTriviallyDead(I)) { |
| 11218 | // Make sure we process all operands now that we are reducing their |
| 11219 | // use counts. |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 11220 | AddUsesToWorkList(*I); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 11221 | |
Chris Lattner | 00d5131 | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 11222 | // Instructions may end up in the worklist more than once. Erase all |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 11223 | // occurrences of this instruction. |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11224 | RemoveFromWorkList(I); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 11225 | I->eraseFromParent(); |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 11226 | } else { |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 11227 | AddToWorkList(I); |
| 11228 | AddUsersToWorkList(*I); |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 11229 | } |
Chris Lattner | b3bc8fa | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 11230 | } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 11231 | Changed = true; |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 11232 | } |
| 11233 | } |
| 11234 | |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 11235 | assert(WorklistMap.empty() && "Worklist empty, but map not?"); |
Chris Lattner | a9ff5eb | 2007-08-05 08:47:58 +0000 | [diff] [blame] | 11236 | |
| 11237 | // Do an explicit clear, this shrinks the map if needed. |
| 11238 | WorklistMap.clear(); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 11239 | return Changed; |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 11240 | } |
| 11241 | |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 11242 | |
| 11243 | bool InstCombiner::runOnFunction(Function &F) { |
Chris Lattner | f964f32 | 2007-03-04 04:27:24 +0000 | [diff] [blame] | 11244 | MustPreserveLCSSA = mustPreserveAnalysisID(LCSSAID); |
| 11245 | |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 11246 | bool EverMadeChange = false; |
| 11247 | |
| 11248 | // Iterate while there is work to do. |
| 11249 | unsigned Iteration = 0; |
| 11250 | while (DoOneIteration(F, Iteration++)) |
| 11251 | EverMadeChange = true; |
| 11252 | return EverMadeChange; |
| 11253 | } |
| 11254 | |
Brian Gaeke | 96d4bf7 | 2004-07-27 17:43:21 +0000 | [diff] [blame] | 11255 | FunctionPass *llvm::createInstructionCombiningPass() { |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 11256 | return new InstCombiner(); |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 11257 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 11258 | |