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 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source 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" |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 47 | #include "llvm/Support/Debug.h" |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 48 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 49 | #include "llvm/Support/InstVisitor.h" |
Chris Lattner | bcd7db5 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 50 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 51 | #include "llvm/Support/PatternMatch.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 52 | #include "llvm/Support/Compiler.h" |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 53 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 54 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 1f87a58 | 2007-02-15 19:41:52 +0000 | [diff] [blame] | 55 | #include "llvm/ADT/SmallPtrSet.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 56 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 57 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | b3bc8fa | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 58 | #include <algorithm> |
Reid Spencer | a9b8101 | 2007-03-26 17:44:01 +0000 | [diff] [blame] | 59 | #include <sstream> |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 60 | using namespace llvm; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 61 | using namespace llvm::PatternMatch; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 62 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 63 | STATISTIC(NumCombined , "Number of insts combined"); |
| 64 | STATISTIC(NumConstProp, "Number of constant folds"); |
| 65 | STATISTIC(NumDeadInst , "Number of dead inst eliminated"); |
| 66 | STATISTIC(NumDeadStore, "Number of dead stores eliminated"); |
| 67 | STATISTIC(NumSunkInst , "Number of instructions sunk"); |
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 68 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 69 | namespace { |
Chris Lattner | f4b5461 | 2006-06-28 22:08:15 +0000 | [diff] [blame] | 70 | class VISIBILITY_HIDDEN InstCombiner |
| 71 | : public FunctionPass, |
| 72 | public InstVisitor<InstCombiner, Instruction*> { |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 73 | // Worklist of all of the instructions that need to be simplified. |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 74 | std::vector<Instruction*> Worklist; |
| 75 | DenseMap<Instruction*, unsigned> WorklistMap; |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 76 | TargetData *TD; |
Chris Lattner | f964f32 | 2007-03-04 04:27:24 +0000 | [diff] [blame] | 77 | bool MustPreserveLCSSA; |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 78 | public: |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 79 | static char ID; // Pass identification, replacement for typeid |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 80 | InstCombiner() : FunctionPass((intptr_t)&ID) {} |
| 81 | |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 82 | /// AddToWorkList - Add the specified instruction to the worklist if it |
| 83 | /// isn't already in it. |
| 84 | void AddToWorkList(Instruction *I) { |
| 85 | if (WorklistMap.insert(std::make_pair(I, Worklist.size()))) |
| 86 | Worklist.push_back(I); |
| 87 | } |
| 88 | |
| 89 | // RemoveFromWorkList - remove I from the worklist if it exists. |
| 90 | void RemoveFromWorkList(Instruction *I) { |
| 91 | DenseMap<Instruction*, unsigned>::iterator It = WorklistMap.find(I); |
| 92 | if (It == WorklistMap.end()) return; // Not in worklist. |
| 93 | |
| 94 | // Don't bother moving everything down, just null out the slot. |
| 95 | Worklist[It->second] = 0; |
| 96 | |
| 97 | WorklistMap.erase(It); |
| 98 | } |
| 99 | |
| 100 | Instruction *RemoveOneFromWorkList() { |
| 101 | Instruction *I = Worklist.back(); |
| 102 | Worklist.pop_back(); |
| 103 | WorklistMap.erase(I); |
| 104 | return I; |
| 105 | } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 106 | |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 107 | |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 108 | /// AddUsersToWorkList - When an instruction is simplified, add all users of |
| 109 | /// the instruction to the work lists because they might get more simplified |
| 110 | /// now. |
| 111 | /// |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 112 | void AddUsersToWorkList(Value &I) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 113 | for (Value::use_iterator UI = I.use_begin(), UE = I.use_end(); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 114 | UI != UE; ++UI) |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 115 | AddToWorkList(cast<Instruction>(*UI)); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 118 | /// AddUsesToWorkList - When an instruction is simplified, add operands to |
| 119 | /// the work lists because they might get more simplified now. |
| 120 | /// |
| 121 | void AddUsesToWorkList(Instruction &I) { |
| 122 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) |
| 123 | if (Instruction *Op = dyn_cast<Instruction>(I.getOperand(i))) |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 124 | AddToWorkList(Op); |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 125 | } |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 126 | |
| 127 | /// AddSoonDeadInstToWorklist - The specified instruction is about to become |
| 128 | /// dead. Add all of its operands to the worklist, turning them into |
| 129 | /// undef's to reduce the number of uses of those instructions. |
| 130 | /// |
| 131 | /// Return the specified operand before it is turned into an undef. |
| 132 | /// |
| 133 | Value *AddSoonDeadInstToWorklist(Instruction &I, unsigned op) { |
| 134 | Value *R = I.getOperand(op); |
| 135 | |
| 136 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) |
| 137 | if (Instruction *Op = dyn_cast<Instruction>(I.getOperand(i))) { |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 138 | AddToWorkList(Op); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 139 | // Set the operand to undef to drop the use. |
| 140 | I.setOperand(i, UndefValue::get(Op->getType())); |
| 141 | } |
| 142 | |
| 143 | return R; |
| 144 | } |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 145 | |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 146 | public: |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 147 | virtual bool runOnFunction(Function &F); |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 148 | |
| 149 | bool DoOneIteration(Function &F, unsigned ItNum); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 150 | |
Chris Lattner | 97e52e4 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 151 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 152 | AU.addRequired<TargetData>(); |
Owen Anderson | d1b78a1 | 2006-07-10 19:03:49 +0000 | [diff] [blame] | 153 | AU.addPreservedID(LCSSAID); |
Chris Lattner | cb2610e | 2002-10-21 20:00:28 +0000 | [diff] [blame] | 154 | AU.setPreservesCFG(); |
Chris Lattner | 97e52e4 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 157 | TargetData &getTargetData() const { return *TD; } |
| 158 | |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 159 | // Visitation implementation - Implement instruction combining for different |
| 160 | // instruction types. The semantics are as follows: |
| 161 | // Return Value: |
| 162 | // null - No change was made |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 163 | // 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] | 164 | // otherwise - Change was made, replace I with returned instruction |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 165 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 166 | Instruction *visitAdd(BinaryOperator &I); |
| 167 | Instruction *visitSub(BinaryOperator &I); |
| 168 | Instruction *visitMul(BinaryOperator &I); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 169 | Instruction *visitURem(BinaryOperator &I); |
| 170 | Instruction *visitSRem(BinaryOperator &I); |
| 171 | Instruction *visitFRem(BinaryOperator &I); |
| 172 | Instruction *commonRemTransforms(BinaryOperator &I); |
| 173 | Instruction *commonIRemTransforms(BinaryOperator &I); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 174 | Instruction *commonDivTransforms(BinaryOperator &I); |
| 175 | Instruction *commonIDivTransforms(BinaryOperator &I); |
| 176 | Instruction *visitUDiv(BinaryOperator &I); |
| 177 | Instruction *visitSDiv(BinaryOperator &I); |
| 178 | Instruction *visitFDiv(BinaryOperator &I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 179 | Instruction *visitAnd(BinaryOperator &I); |
| 180 | Instruction *visitOr (BinaryOperator &I); |
| 181 | Instruction *visitXor(BinaryOperator &I); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 182 | Instruction *visitShl(BinaryOperator &I); |
| 183 | Instruction *visitAShr(BinaryOperator &I); |
| 184 | Instruction *visitLShr(BinaryOperator &I); |
| 185 | Instruction *commonShiftTransforms(BinaryOperator &I); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 186 | Instruction *visitFCmpInst(FCmpInst &I); |
| 187 | Instruction *visitICmpInst(ICmpInst &I); |
| 188 | Instruction *visitICmpInstWithCastAndCast(ICmpInst &ICI); |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 189 | Instruction *visitICmpInstWithInstAndIntCst(ICmpInst &ICI, |
| 190 | Instruction *LHS, |
| 191 | ConstantInt *RHS); |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 192 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 193 | Instruction *FoldGEPICmp(User *GEPLHS, Value *RHS, |
| 194 | ICmpInst::Predicate Cond, Instruction &I); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 195 | Instruction *FoldShiftByConstant(Value *Op0, ConstantInt *Op1, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 196 | BinaryOperator &I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 197 | Instruction *commonCastTransforms(CastInst &CI); |
| 198 | Instruction *commonIntCastTransforms(CastInst &CI); |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 199 | Instruction *commonPointerCastTransforms(CastInst &CI); |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 200 | Instruction *visitTrunc(TruncInst &CI); |
| 201 | Instruction *visitZExt(ZExtInst &CI); |
| 202 | Instruction *visitSExt(SExtInst &CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 203 | Instruction *visitFPTrunc(CastInst &CI); |
| 204 | Instruction *visitFPExt(CastInst &CI); |
| 205 | Instruction *visitFPToUI(CastInst &CI); |
| 206 | Instruction *visitFPToSI(CastInst &CI); |
| 207 | Instruction *visitUIToFP(CastInst &CI); |
| 208 | Instruction *visitSIToFP(CastInst &CI); |
| 209 | Instruction *visitPtrToInt(CastInst &CI); |
| 210 | Instruction *visitIntToPtr(CastInst &CI); |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 211 | Instruction *visitBitCast(BitCastInst &CI); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 212 | Instruction *FoldSelectOpOp(SelectInst &SI, Instruction *TI, |
| 213 | Instruction *FI); |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 214 | Instruction *visitSelectInst(SelectInst &CI); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 215 | Instruction *visitCallInst(CallInst &CI); |
| 216 | Instruction *visitInvokeInst(InvokeInst &II); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 217 | Instruction *visitPHINode(PHINode &PN); |
| 218 | Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP); |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 219 | Instruction *visitAllocationInst(AllocationInst &AI); |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 220 | Instruction *visitFreeInst(FreeInst &FI); |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 221 | Instruction *visitLoadInst(LoadInst &LI); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 222 | Instruction *visitStoreInst(StoreInst &SI); |
Chris Lattner | c4d10eb | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 223 | Instruction *visitBranchInst(BranchInst &BI); |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 224 | Instruction *visitSwitchInst(SwitchInst &SI); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 225 | Instruction *visitInsertElementInst(InsertElementInst &IE); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 226 | Instruction *visitExtractElementInst(ExtractElementInst &EI); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 227 | Instruction *visitShuffleVectorInst(ShuffleVectorInst &SVI); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 228 | |
| 229 | // visitInstruction - Specify what to return for unhandled instructions... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 230 | Instruction *visitInstruction(Instruction &I) { return 0; } |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 231 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 232 | private: |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 233 | Instruction *visitCallSite(CallSite CS); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 234 | bool transformConstExprCastCall(CallSite CS); |
| 235 | |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 236 | public: |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 237 | // InsertNewInstBefore - insert an instruction New before instruction Old |
| 238 | // in the program. Add the new instruction to the worklist. |
| 239 | // |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 240 | Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) { |
Chris Lattner | e6f9a91 | 2002-08-23 18:32:43 +0000 | [diff] [blame] | 241 | assert(New && New->getParent() == 0 && |
| 242 | "New instruction already inserted into a basic block!"); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 243 | BasicBlock *BB = Old.getParent(); |
| 244 | BB->getInstList().insert(&Old, New); // Insert inst |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 245 | AddToWorkList(New); |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 246 | return New; |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 249 | /// InsertCastBefore - Insert a cast of V to TY before the instruction POS. |
| 250 | /// This also adds the cast to the worklist. Finally, this returns the |
| 251 | /// cast. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 252 | Value *InsertCastBefore(Instruction::CastOps opc, Value *V, const Type *Ty, |
| 253 | Instruction &Pos) { |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 254 | if (V->getType() == Ty) return V; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 255 | |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 256 | if (Constant *CV = dyn_cast<Constant>(V)) |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 257 | return ConstantExpr::getCast(opc, CV, Ty); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 258 | |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 259 | Instruction *C = CastInst::create(opc, V, Ty, V->getName(), &Pos); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 260 | AddToWorkList(C); |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 261 | return C; |
| 262 | } |
| 263 | |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 264 | // ReplaceInstUsesWith - This method is to be used when an instruction is |
| 265 | // found to be dead, replacable with another preexisting expression. Here |
| 266 | // we add all uses of I to the worklist, replace all uses of I with the new |
| 267 | // value, then return I, so that the inst combiner will know that I was |
| 268 | // modified. |
| 269 | // |
| 270 | Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) { |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 271 | AddUsersToWorkList(I); // Add all modified instrs to worklist |
Chris Lattner | 15a76c0 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 272 | if (&I != V) { |
| 273 | I.replaceAllUsesWith(V); |
| 274 | return &I; |
| 275 | } else { |
| 276 | // If we are replacing the instruction with itself, this must be in a |
| 277 | // segment of unreachable code, so just clobber the instruction. |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 278 | I.replaceAllUsesWith(UndefValue::get(I.getType())); |
Chris Lattner | 15a76c0 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 279 | return &I; |
| 280 | } |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 281 | } |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 282 | |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 283 | // UpdateValueUsesWith - This method is to be used when an value is |
| 284 | // found to be replacable with another preexisting expression or was |
| 285 | // updated. Here we add all uses of I to the worklist, replace all uses of |
| 286 | // I with the new value (unless the instruction was just updated), then |
| 287 | // return true, so that the inst combiner will know that I was modified. |
| 288 | // |
| 289 | bool UpdateValueUsesWith(Value *Old, Value *New) { |
| 290 | AddUsersToWorkList(*Old); // Add all modified instrs to worklist |
| 291 | if (Old != New) |
| 292 | Old->replaceAllUsesWith(New); |
| 293 | if (Instruction *I = dyn_cast<Instruction>(Old)) |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 294 | AddToWorkList(I); |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 295 | if (Instruction *I = dyn_cast<Instruction>(New)) |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 296 | AddToWorkList(I); |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 297 | return true; |
| 298 | } |
| 299 | |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 300 | // EraseInstFromFunction - When dealing with an instruction that has side |
| 301 | // effects or produces a void value, we can't rely on DCE to delete the |
| 302 | // instruction. Instead, visit methods should return the value returned by |
| 303 | // this function. |
| 304 | Instruction *EraseInstFromFunction(Instruction &I) { |
| 305 | assert(I.use_empty() && "Cannot erase instruction that is used!"); |
| 306 | AddUsesToWorkList(I); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 307 | RemoveFromWorkList(&I); |
Chris Lattner | 954f66a | 2004-11-18 21:41:39 +0000 | [diff] [blame] | 308 | I.eraseFromParent(); |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 309 | return 0; // Don't do anything with FI |
| 310 | } |
| 311 | |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 312 | private: |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 313 | /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the |
| 314 | /// InsertBefore instruction. This is specialized a bit to avoid inserting |
| 315 | /// casts that are known to not do anything... |
| 316 | /// |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 317 | Value *InsertOperandCastBefore(Instruction::CastOps opcode, |
| 318 | Value *V, const Type *DestTy, |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 319 | Instruction *InsertBefore); |
| 320 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 321 | /// SimplifyCommutative - This performs a few simplifications for |
| 322 | /// commutative operators. |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 323 | bool SimplifyCommutative(BinaryOperator &I); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 324 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 325 | /// SimplifyCompare - This reorders the operands of a CmpInst to get them in |
| 326 | /// most-complex to least-complex order. |
| 327 | bool SimplifyCompare(CmpInst &I); |
| 328 | |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 329 | /// SimplifyDemandedBits - Attempts to replace V with a simpler value based |
| 330 | /// on the demanded bits. |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 331 | bool SimplifyDemandedBits(Value *V, APInt DemandedMask, |
| 332 | APInt& KnownZero, APInt& KnownOne, |
| 333 | unsigned Depth = 0); |
| 334 | |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 335 | Value *SimplifyDemandedVectorElts(Value *V, uint64_t DemandedElts, |
| 336 | uint64_t &UndefElts, unsigned Depth = 0); |
| 337 | |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 338 | // FoldOpIntoPhi - Given a binary operator or cast instruction which has a |
| 339 | // PHI node as operand #0, see if we can fold the instruction into the PHI |
| 340 | // (which is only possible if all operands to the PHI are constants). |
| 341 | Instruction *FoldOpIntoPhi(Instruction &I); |
| 342 | |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 343 | // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary" |
| 344 | // operator and they all are only used by the PHI, PHI together their |
| 345 | // inputs, and do the operation once, to the result of the PHI. |
| 346 | Instruction *FoldPHIArgOpIntoPHI(PHINode &PN); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 347 | Instruction *FoldPHIArgBinOpIntoPHI(PHINode &PN); |
| 348 | |
| 349 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 350 | Instruction *OptAndOp(Instruction *Op, ConstantInt *OpRHS, |
| 351 | ConstantInt *AndRHS, BinaryOperator &TheAnd); |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 352 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 353 | Value *FoldLogicalPlusAnd(Value *LHS, Value *RHS, ConstantInt *Mask, |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 354 | bool isSub, Instruction &I); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 355 | Instruction *InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 356 | bool isSigned, bool Inside, Instruction &IB); |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 357 | Instruction *PromoteCastOfAllocation(BitCastInst &CI, AllocationInst &AI); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 358 | Instruction *MatchBSwap(BinaryOperator &I); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 359 | bool SimplifyStoreAtEndOfBlock(StoreInst &SI); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 360 | |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 361 | Value *EvaluateInDifferentType(Value *V, const Type *Ty, bool isSigned); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 362 | }; |
Chris Lattner | f629309 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 363 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 364 | char InstCombiner::ID = 0; |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 365 | RegisterPass<InstCombiner> X("instcombine", "Combine redundant instructions"); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 368 | // getComplexity: Assign a complexity or rank value to LLVM Values... |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 369 | // 0 -> undef, 1 -> Const, 2 -> Other, 3 -> Arg, 3 -> Unary, 4 -> OtherInst |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 370 | static unsigned getComplexity(Value *V) { |
| 371 | if (isa<Instruction>(V)) { |
| 372 | if (BinaryOperator::isNeg(V) || BinaryOperator::isNot(V)) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 373 | return 3; |
| 374 | return 4; |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 375 | } |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 376 | if (isa<Argument>(V)) return 3; |
| 377 | return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2; |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 378 | } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 379 | |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 380 | // isOnlyUse - Return true if this instruction will be deleted if we stop using |
| 381 | // it. |
| 382 | static bool isOnlyUse(Value *V) { |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 383 | return V->hasOneUse() || isa<Constant>(V); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 386 | // getPromotedType - Return the specified type promoted as it would be to pass |
| 387 | // though a va_arg area... |
| 388 | static const Type *getPromotedType(const Type *Ty) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 389 | if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty)) { |
| 390 | if (ITy->getBitWidth() < 32) |
| 391 | return Type::Int32Ty; |
Chris Lattner | 2b7e0ad | 2007-05-23 01:17:04 +0000 | [diff] [blame] | 392 | } |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 393 | return Ty; |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 396 | /// getBitCastOperand - If the specified operand is a CastInst or a constant |
| 397 | /// expression bitcast, return the operand value, otherwise return null. |
| 398 | static Value *getBitCastOperand(Value *V) { |
| 399 | if (BitCastInst *I = dyn_cast<BitCastInst>(V)) |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 400 | return I->getOperand(0); |
| 401 | else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 402 | if (CE->getOpcode() == Instruction::BitCast) |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 403 | return CE->getOperand(0); |
| 404 | return 0; |
| 405 | } |
| 406 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 407 | /// This function is a wrapper around CastInst::isEliminableCastPair. It |
| 408 | /// simply extracts arguments and returns what that function returns. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 409 | static Instruction::CastOps |
| 410 | isEliminableCastPair( |
| 411 | const CastInst *CI, ///< The first cast instruction |
| 412 | unsigned opcode, ///< The opcode of the second cast instruction |
| 413 | const Type *DstTy, ///< The target type for the second cast instruction |
| 414 | TargetData *TD ///< The target data for pointer size |
| 415 | ) { |
| 416 | |
| 417 | const Type *SrcTy = CI->getOperand(0)->getType(); // A from above |
| 418 | const Type *MidTy = CI->getType(); // B from above |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 419 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 420 | // Get the opcodes of the two Cast instructions |
| 421 | Instruction::CastOps firstOp = Instruction::CastOps(CI->getOpcode()); |
| 422 | Instruction::CastOps secondOp = Instruction::CastOps(opcode); |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 423 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 424 | return Instruction::CastOps( |
| 425 | CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, |
| 426 | DstTy, TD->getIntPtrType())); |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | /// ValueRequiresCast - Return true if the cast from "V to Ty" actually results |
| 430 | /// in any code being generated. It does not require codegen if V is simple |
| 431 | /// enough or if the cast can be folded into other casts. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 432 | static bool ValueRequiresCast(Instruction::CastOps opcode, const Value *V, |
| 433 | const Type *Ty, TargetData *TD) { |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 434 | if (V->getType() == Ty || isa<Constant>(V)) return false; |
| 435 | |
Chris Lattner | 01575b7 | 2006-05-25 23:24:33 +0000 | [diff] [blame] | 436 | // 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] | 437 | if (const CastInst *CI = dyn_cast<CastInst>(V)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 438 | if (isEliminableCastPair(CI, opcode, Ty, TD)) |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 439 | return false; |
| 440 | return true; |
| 441 | } |
| 442 | |
| 443 | /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the |
| 444 | /// InsertBefore instruction. This is specialized a bit to avoid inserting |
| 445 | /// casts that are known to not do anything... |
| 446 | /// |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 447 | Value *InstCombiner::InsertOperandCastBefore(Instruction::CastOps opcode, |
| 448 | Value *V, const Type *DestTy, |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 449 | Instruction *InsertBefore) { |
| 450 | if (V->getType() == DestTy) return V; |
| 451 | if (Constant *C = dyn_cast<Constant>(V)) |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 452 | return ConstantExpr::getCast(opcode, C, DestTy); |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 453 | |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 454 | return InsertCastBefore(opcode, V, DestTy, *InsertBefore); |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 457 | // SimplifyCommutative - This performs a few simplifications for commutative |
| 458 | // operators: |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 459 | // |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 460 | // 1. Order operands such that they are listed from right (least complex) to |
| 461 | // left (most complex). This puts constants before unary operators before |
| 462 | // binary operators. |
| 463 | // |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 464 | // 2. Transform: (op (op V, C1), C2) ==> (op V, (op C1, C2)) |
| 465 | // 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] | 466 | // |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 467 | bool InstCombiner::SimplifyCommutative(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 468 | bool Changed = false; |
| 469 | if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1))) |
| 470 | Changed = !I.swapOperands(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 471 | |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 472 | if (!I.isAssociative()) return Changed; |
| 473 | Instruction::BinaryOps Opcode = I.getOpcode(); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 474 | if (BinaryOperator *Op = dyn_cast<BinaryOperator>(I.getOperand(0))) |
| 475 | if (Op->getOpcode() == Opcode && isa<Constant>(Op->getOperand(1))) { |
| 476 | if (isa<Constant>(I.getOperand(1))) { |
Chris Lattner | 2a9c847 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 477 | Constant *Folded = ConstantExpr::get(I.getOpcode(), |
| 478 | cast<Constant>(I.getOperand(1)), |
| 479 | cast<Constant>(Op->getOperand(1))); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 480 | I.setOperand(0, Op->getOperand(0)); |
| 481 | I.setOperand(1, Folded); |
| 482 | return true; |
| 483 | } else if (BinaryOperator *Op1=dyn_cast<BinaryOperator>(I.getOperand(1))) |
| 484 | if (Op1->getOpcode() == Opcode && isa<Constant>(Op1->getOperand(1)) && |
| 485 | isOnlyUse(Op) && isOnlyUse(Op1)) { |
| 486 | Constant *C1 = cast<Constant>(Op->getOperand(1)); |
| 487 | Constant *C2 = cast<Constant>(Op1->getOperand(1)); |
| 488 | |
| 489 | // 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] | 490 | Constant *Folded = ConstantExpr::get(I.getOpcode(), C1, C2); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 491 | Instruction *New = BinaryOperator::create(Opcode, Op->getOperand(0), |
| 492 | Op1->getOperand(0), |
| 493 | Op1->getName(), &I); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 494 | AddToWorkList(New); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 495 | I.setOperand(0, New); |
| 496 | I.setOperand(1, Folded); |
| 497 | return true; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 498 | } |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 499 | } |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 500 | return Changed; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 501 | } |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 502 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 503 | /// SimplifyCompare - For a CmpInst this function just orders the operands |
| 504 | /// so that theyare listed from right (least complex) to left (most complex). |
| 505 | /// This puts constants before unary operators before binary operators. |
| 506 | bool InstCombiner::SimplifyCompare(CmpInst &I) { |
| 507 | if (getComplexity(I.getOperand(0)) >= getComplexity(I.getOperand(1))) |
| 508 | return false; |
| 509 | I.swapOperands(); |
| 510 | // Compare instructions are not associative so there's nothing else we can do. |
| 511 | return true; |
| 512 | } |
| 513 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 514 | // dyn_castNegVal - Given a 'sub' instruction, return the RHS of the instruction |
| 515 | // if the LHS is a constant zero (which is the 'negate' form). |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 516 | // |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 517 | static inline Value *dyn_castNegVal(Value *V) { |
| 518 | if (BinaryOperator::isNeg(V)) |
Chris Lattner | a1df33c | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 519 | return BinaryOperator::getNegArgument(V); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 520 | |
Chris Lattner | 0ce8580 | 2004-12-14 20:08:06 +0000 | [diff] [blame] | 521 | // Constants can be considered to be negated values if they can be folded. |
| 522 | if (ConstantInt *C = dyn_cast<ConstantInt>(V)) |
| 523 | return ConstantExpr::getNeg(C); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 524 | return 0; |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 525 | } |
| 526 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 527 | static inline Value *dyn_castNotVal(Value *V) { |
| 528 | if (BinaryOperator::isNot(V)) |
Chris Lattner | a1df33c | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 529 | return BinaryOperator::getNotArgument(V); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 530 | |
| 531 | // Constants can be considered to be not'ed values... |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 532 | if (ConstantInt *C = dyn_cast<ConstantInt>(V)) |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 533 | return ConstantInt::get(~C->getValue()); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 534 | return 0; |
| 535 | } |
| 536 | |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 537 | // dyn_castFoldableMul - If this value is a multiply that can be folded into |
| 538 | // other computations (because it has a constant operand), return the |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 539 | // non-constant operand of the multiply, and set CST to point to the multiplier. |
| 540 | // Otherwise, return null. |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 541 | // |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 542 | static inline Value *dyn_castFoldableMul(Value *V, ConstantInt *&CST) { |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 543 | if (V->hasOneUse() && V->getType()->isInteger()) |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 544 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 545 | if (I->getOpcode() == Instruction::Mul) |
Chris Lattner | 50e60c7 | 2004-11-15 05:54:07 +0000 | [diff] [blame] | 546 | if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 547 | return I->getOperand(0); |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 548 | if (I->getOpcode() == Instruction::Shl) |
Chris Lattner | 50e60c7 | 2004-11-15 05:54:07 +0000 | [diff] [blame] | 549 | if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) { |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 550 | // The multiplier is really 1 << CST. |
Zhou Sheng | 97b52c2 | 2007-03-29 01:57:21 +0000 | [diff] [blame] | 551 | uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth(); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 552 | uint32_t CSTVal = CST->getLimitedValue(BitWidth); |
Zhou Sheng | 97b52c2 | 2007-03-29 01:57:21 +0000 | [diff] [blame] | 553 | CST = ConstantInt::get(APInt(BitWidth, 1).shl(CSTVal)); |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 554 | return I->getOperand(0); |
| 555 | } |
| 556 | } |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 557 | return 0; |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 558 | } |
Chris Lattner | af2930e | 2002-08-14 17:51:49 +0000 | [diff] [blame] | 559 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 560 | /// dyn_castGetElementPtr - If this is a getelementptr instruction or constant |
| 561 | /// expression, return it. |
| 562 | static User *dyn_castGetElementPtr(Value *V) { |
| 563 | if (isa<GetElementPtrInst>(V)) return cast<User>(V); |
| 564 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
| 565 | if (CE->getOpcode() == Instruction::GetElementPtr) |
| 566 | return cast<User>(V); |
| 567 | return false; |
| 568 | } |
| 569 | |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 570 | /// AddOne - Add one to a ConstantInt |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 571 | static ConstantInt *AddOne(ConstantInt *C) { |
Reid Spencer | 2149a9d | 2007-03-25 19:55:33 +0000 | [diff] [blame] | 572 | APInt Val(C->getValue()); |
| 573 | return ConstantInt::get(++Val); |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 574 | } |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 575 | /// SubOne - Subtract one from a ConstantInt |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 576 | static ConstantInt *SubOne(ConstantInt *C) { |
Reid Spencer | 2149a9d | 2007-03-25 19:55:33 +0000 | [diff] [blame] | 577 | APInt Val(C->getValue()); |
| 578 | return ConstantInt::get(--Val); |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 579 | } |
| 580 | /// Add - Add two ConstantInts together |
| 581 | static ConstantInt *Add(ConstantInt *C1, ConstantInt *C2) { |
| 582 | return ConstantInt::get(C1->getValue() + C2->getValue()); |
| 583 | } |
| 584 | /// And - Bitwise AND two ConstantInts together |
| 585 | static ConstantInt *And(ConstantInt *C1, ConstantInt *C2) { |
| 586 | return ConstantInt::get(C1->getValue() & C2->getValue()); |
| 587 | } |
| 588 | /// Subtract - Subtract one ConstantInt from another |
| 589 | static ConstantInt *Subtract(ConstantInt *C1, ConstantInt *C2) { |
| 590 | return ConstantInt::get(C1->getValue() - C2->getValue()); |
| 591 | } |
| 592 | /// Multiply - Multiply two ConstantInts together |
| 593 | static ConstantInt *Multiply(ConstantInt *C1, ConstantInt *C2) { |
| 594 | return ConstantInt::get(C1->getValue() * C2->getValue()); |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 595 | } |
| 596 | |
Chris Lattner | 68d5ff2 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 597 | /// ComputeMaskedBits - Determine which of the bits specified in Mask are |
| 598 | /// 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] | 599 | /// bit sets. This code only analyzes bits in Mask, in order to short-circuit |
| 600 | /// processing. |
| 601 | /// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that |
| 602 | /// we cannot optimize based on the assumption that it is zero without changing |
| 603 | /// it to be an explicit zero. If we don't change it to zero, other code could |
| 604 | /// optimized based on the contradictory assumption that it is non-zero. |
| 605 | /// Because instcombine aggressively folds operations with undef args anyway, |
| 606 | /// this won't lose us code quality. |
Reid Spencer | 55702aa | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 607 | static void ComputeMaskedBits(Value *V, const APInt &Mask, APInt& KnownZero, |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 608 | APInt& KnownOne, unsigned Depth = 0) { |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 609 | assert(V && "No Value?"); |
| 610 | assert(Depth <= 6 && "Limit Search Depth"); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 611 | uint32_t BitWidth = Mask.getBitWidth(); |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 612 | assert(cast<IntegerType>(V->getType())->getBitWidth() == BitWidth && |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 613 | KnownZero.getBitWidth() == BitWidth && |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 614 | KnownOne.getBitWidth() == BitWidth && |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 615 | "V, Mask, KnownOne and KnownZero should have same BitWidth"); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 616 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
| 617 | // We know all of the bits for a constant! |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 618 | KnownOne = CI->getValue() & Mask; |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 619 | KnownZero = ~KnownOne & Mask; |
| 620 | return; |
| 621 | } |
| 622 | |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 623 | if (Depth == 6 || Mask == 0) |
| 624 | return; // Limit search depth. |
| 625 | |
| 626 | Instruction *I = dyn_cast<Instruction>(V); |
| 627 | if (!I) return; |
| 628 | |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 629 | KnownZero.clear(); KnownOne.clear(); // Don't know anything. |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 630 | APInt KnownZero2(KnownZero), KnownOne2(KnownOne); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 631 | |
| 632 | switch (I->getOpcode()) { |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 633 | case Instruction::And: { |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 634 | // If either the LHS or the RHS are Zero, the result is zero. |
| 635 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, Depth+1); |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 636 | APInt Mask2(Mask & ~KnownZero); |
| 637 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 638 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 639 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 640 | |
| 641 | // Output known-1 bits are only known if set in both the LHS & RHS. |
| 642 | KnownOne &= KnownOne2; |
| 643 | // Output known-0 are known to be clear if zero in either the LHS | RHS. |
| 644 | KnownZero |= KnownZero2; |
| 645 | return; |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 646 | } |
| 647 | case Instruction::Or: { |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 648 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, Depth+1); |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 649 | APInt Mask2(Mask & ~KnownOne); |
| 650 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 651 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 652 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 653 | |
| 654 | // Output known-0 bits are only known if clear in both the LHS & RHS. |
| 655 | KnownZero &= KnownZero2; |
| 656 | // Output known-1 are known to be set if set in either the LHS | RHS. |
| 657 | KnownOne |= KnownOne2; |
| 658 | return; |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 659 | } |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 660 | case Instruction::Xor: { |
| 661 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, Depth+1); |
| 662 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1); |
| 663 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 664 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 665 | |
| 666 | // Output known-0 bits are known if clear or set in both the LHS & RHS. |
| 667 | APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2); |
| 668 | // Output known-1 are known to be set if set in only one of the LHS, RHS. |
| 669 | KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2); |
| 670 | KnownZero = KnownZeroOut; |
| 671 | return; |
| 672 | } |
| 673 | case Instruction::Select: |
| 674 | ComputeMaskedBits(I->getOperand(2), Mask, KnownZero, KnownOne, Depth+1); |
| 675 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1); |
| 676 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 677 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 678 | |
| 679 | // Only known if known in both the LHS and RHS. |
| 680 | KnownOne &= KnownOne2; |
| 681 | KnownZero &= KnownZero2; |
| 682 | return; |
| 683 | case Instruction::FPTrunc: |
| 684 | case Instruction::FPExt: |
| 685 | case Instruction::FPToUI: |
| 686 | case Instruction::FPToSI: |
| 687 | case Instruction::SIToFP: |
| 688 | case Instruction::PtrToInt: |
| 689 | case Instruction::UIToFP: |
| 690 | case Instruction::IntToPtr: |
| 691 | return; // Can't work with floating point or pointers |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 692 | case Instruction::Trunc: { |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 693 | // All these have integer operands |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 694 | uint32_t SrcBitWidth = |
| 695 | cast<IntegerType>(I->getOperand(0)->getType())->getBitWidth(); |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 696 | APInt MaskIn(Mask); |
| 697 | MaskIn.zext(SrcBitWidth); |
| 698 | KnownZero.zext(SrcBitWidth); |
| 699 | KnownOne.zext(SrcBitWidth); |
| 700 | ComputeMaskedBits(I->getOperand(0), MaskIn, KnownZero, KnownOne, Depth+1); |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 701 | KnownZero.trunc(BitWidth); |
| 702 | KnownOne.trunc(BitWidth); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 703 | return; |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 704 | } |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 705 | case Instruction::BitCast: { |
| 706 | const Type *SrcTy = I->getOperand(0)->getType(); |
| 707 | if (SrcTy->isInteger()) { |
| 708 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero, KnownOne, Depth+1); |
| 709 | return; |
| 710 | } |
| 711 | break; |
| 712 | } |
| 713 | case Instruction::ZExt: { |
| 714 | // Compute the bits in the result that are not present in the input. |
| 715 | const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType()); |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 716 | uint32_t SrcBitWidth = SrcTy->getBitWidth(); |
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 717 | |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 718 | APInt MaskIn(Mask); |
| 719 | MaskIn.trunc(SrcBitWidth); |
| 720 | KnownZero.trunc(SrcBitWidth); |
| 721 | KnownOne.trunc(SrcBitWidth); |
| 722 | ComputeMaskedBits(I->getOperand(0), MaskIn, KnownZero, KnownOne, Depth+1); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 723 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 724 | // The top bits are known to be zero. |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 725 | KnownZero.zext(BitWidth); |
| 726 | KnownOne.zext(BitWidth); |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 727 | KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 728 | return; |
| 729 | } |
| 730 | case Instruction::SExt: { |
| 731 | // Compute the bits in the result that are not present in the input. |
| 732 | const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType()); |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 733 | uint32_t SrcBitWidth = SrcTy->getBitWidth(); |
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 734 | |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 735 | APInt MaskIn(Mask); |
| 736 | MaskIn.trunc(SrcBitWidth); |
| 737 | KnownZero.trunc(SrcBitWidth); |
| 738 | KnownOne.trunc(SrcBitWidth); |
| 739 | ComputeMaskedBits(I->getOperand(0), MaskIn, KnownZero, KnownOne, Depth+1); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 740 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 741 | KnownZero.zext(BitWidth); |
| 742 | KnownOne.zext(BitWidth); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 743 | |
| 744 | // If the sign bit of the input is known set or clear, then we know the |
| 745 | // top bits of the result. |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 746 | if (KnownZero[SrcBitWidth-1]) // Input sign bit known zero |
Zhou Sheng | 34a4b38 | 2007-03-28 17:38:21 +0000 | [diff] [blame] | 747 | KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth); |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 748 | else if (KnownOne[SrcBitWidth-1]) // Input sign bit known set |
Zhou Sheng | 34a4b38 | 2007-03-28 17:38:21 +0000 | [diff] [blame] | 749 | KnownOne |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 750 | return; |
| 751 | } |
| 752 | case Instruction::Shl: |
| 753 | // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0 |
| 754 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 755 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 756 | APInt Mask2(Mask.lshr(ShiftAmt)); |
| 757 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero, KnownOne, Depth+1); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 758 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
Zhou Sheng | 430f626 | 2007-03-12 05:44:52 +0000 | [diff] [blame] | 759 | KnownZero <<= ShiftAmt; |
| 760 | KnownOne <<= ShiftAmt; |
Reid Spencer | 2149a9d | 2007-03-25 19:55:33 +0000 | [diff] [blame] | 761 | KnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt); // low bits known 0 |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 762 | return; |
| 763 | } |
| 764 | break; |
| 765 | case Instruction::LShr: |
| 766 | // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0 |
| 767 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 768 | // Compute the new bits that are at the top now. |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 769 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 770 | |
| 771 | // Unsigned shift right. |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 772 | APInt Mask2(Mask.shl(ShiftAmt)); |
| 773 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero,KnownOne,Depth+1); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 774 | assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); |
| 775 | KnownZero = APIntOps::lshr(KnownZero, ShiftAmt); |
| 776 | KnownOne = APIntOps::lshr(KnownOne, ShiftAmt); |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 777 | // high bits known zero. |
| 778 | KnownZero |= APInt::getHighBitsSet(BitWidth, ShiftAmt); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 779 | return; |
| 780 | } |
| 781 | break; |
| 782 | case Instruction::AShr: |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 783 | // (ashr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0 |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 784 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 785 | // Compute the new bits that are at the top now. |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 786 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 787 | |
| 788 | // Signed shift right. |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 789 | APInt Mask2(Mask.shl(ShiftAmt)); |
| 790 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero,KnownOne,Depth+1); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 791 | assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); |
| 792 | KnownZero = APIntOps::lshr(KnownZero, ShiftAmt); |
| 793 | KnownOne = APIntOps::lshr(KnownOne, ShiftAmt); |
| 794 | |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 795 | APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt)); |
| 796 | if (KnownZero[BitWidth-ShiftAmt-1]) // New bits are known zero. |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 797 | KnownZero |= HighBits; |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 798 | else if (KnownOne[BitWidth-ShiftAmt-1]) // New bits are known one. |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 799 | KnownOne |= HighBits; |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 800 | return; |
| 801 | } |
| 802 | break; |
| 803 | } |
| 804 | } |
| 805 | |
Reid Spencer | e7816b5 | 2007-03-08 01:52:58 +0000 | [diff] [blame] | 806 | /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use |
| 807 | /// this predicate to simplify operations downstream. Mask is known to be zero |
| 808 | /// for bits that V cannot have. |
| 809 | static bool MaskedValueIsZero(Value *V, const APInt& Mask, unsigned Depth = 0) { |
Zhou Sheng | edd089c | 2007-03-12 16:54:56 +0000 | [diff] [blame] | 810 | APInt KnownZero(Mask.getBitWidth(), 0), KnownOne(Mask.getBitWidth(), 0); |
Reid Spencer | e7816b5 | 2007-03-08 01:52:58 +0000 | [diff] [blame] | 811 | ComputeMaskedBits(V, Mask, KnownZero, KnownOne, Depth); |
| 812 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 813 | return (KnownZero & Mask) == Mask; |
| 814 | } |
| 815 | |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 816 | /// ShrinkDemandedConstant - Check to see if the specified operand of the |
| 817 | /// specified instruction is a constant integer. If so, check to see if there |
| 818 | /// are any bits set in the constant that are not demanded. If so, shrink the |
| 819 | /// constant and return true. |
| 820 | static bool ShrinkDemandedConstant(Instruction *I, unsigned OpNo, |
Reid Spencer | 6b79e2d | 2007-03-12 17:15:10 +0000 | [diff] [blame] | 821 | APInt Demanded) { |
| 822 | assert(I && "No instruction?"); |
| 823 | assert(OpNo < I->getNumOperands() && "Operand index too large"); |
| 824 | |
| 825 | // If the operand is not a constant integer, nothing to do. |
| 826 | ConstantInt *OpC = dyn_cast<ConstantInt>(I->getOperand(OpNo)); |
| 827 | if (!OpC) return false; |
| 828 | |
| 829 | // If there are no bits set that aren't demanded, nothing to do. |
| 830 | Demanded.zextOrTrunc(OpC->getValue().getBitWidth()); |
| 831 | if ((~Demanded & OpC->getValue()) == 0) |
| 832 | return false; |
| 833 | |
| 834 | // This instruction is producing bits that are not demanded. Shrink the RHS. |
| 835 | Demanded &= OpC->getValue(); |
| 836 | I->setOperand(OpNo, ConstantInt::get(Demanded)); |
| 837 | return true; |
| 838 | } |
| 839 | |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 840 | // ComputeSignedMinMaxValuesFromKnownBits - Given a signed integer type and a |
| 841 | // set of known zero and one bits, compute the maximum and minimum values that |
| 842 | // could have the specified known zero and known one bits, returning them in |
| 843 | // min/max. |
| 844 | static void ComputeSignedMinMaxValuesFromKnownBits(const Type *Ty, |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 845 | const APInt& KnownZero, |
| 846 | const APInt& KnownOne, |
| 847 | APInt& Min, APInt& Max) { |
| 848 | uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth(); |
| 849 | assert(KnownZero.getBitWidth() == BitWidth && |
| 850 | KnownOne.getBitWidth() == BitWidth && |
| 851 | Min.getBitWidth() == BitWidth && Max.getBitWidth() == BitWidth && |
| 852 | "Ty, KnownZero, KnownOne and Min, Max must have equal bitwidth."); |
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 853 | APInt UnknownBits = ~(KnownZero|KnownOne); |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 854 | |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 855 | // The minimum value is when all unknown bits are zeros, EXCEPT for the sign |
| 856 | // bit if it is unknown. |
| 857 | Min = KnownOne; |
| 858 | Max = KnownOne|UnknownBits; |
| 859 | |
Zhou Sheng | 4acf155 | 2007-03-28 05:15:57 +0000 | [diff] [blame] | 860 | if (UnknownBits[BitWidth-1]) { // Sign bit is unknown |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 861 | Min.set(BitWidth-1); |
| 862 | Max.clear(BitWidth-1); |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 863 | } |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | // ComputeUnsignedMinMaxValuesFromKnownBits - Given an unsigned integer type and |
| 867 | // a set of known zero and one bits, compute the maximum and minimum values that |
| 868 | // could have the specified known zero and known one bits, returning them in |
| 869 | // min/max. |
| 870 | static void ComputeUnsignedMinMaxValuesFromKnownBits(const Type *Ty, |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 871 | const APInt& KnownZero, |
| 872 | const APInt& KnownOne, |
| 873 | APInt& Min, |
| 874 | APInt& Max) { |
| 875 | uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth(); |
| 876 | assert(KnownZero.getBitWidth() == BitWidth && |
| 877 | KnownOne.getBitWidth() == BitWidth && |
| 878 | Min.getBitWidth() == BitWidth && Max.getBitWidth() && |
| 879 | "Ty, KnownZero, KnownOne and Min, Max must have equal bitwidth."); |
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 880 | APInt UnknownBits = ~(KnownZero|KnownOne); |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 881 | |
| 882 | // The minimum value is when the unknown bits are all zeros. |
| 883 | Min = KnownOne; |
| 884 | // The maximum value is when the unknown bits are all ones. |
| 885 | Max = KnownOne|UnknownBits; |
| 886 | } |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 887 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 888 | /// SimplifyDemandedBits - This function attempts to replace V with a simpler |
| 889 | /// value based on the demanded bits. When this function is called, it is known |
| 890 | /// that only the bits set in DemandedMask of the result of V are ever used |
| 891 | /// downstream. Consequently, depending on the mask and V, it may be possible |
| 892 | /// to replace V with a constant or one of its operands. In such cases, this |
| 893 | /// function does the replacement and returns true. In all other cases, it |
| 894 | /// returns false after analyzing the expression and setting KnownOne and known |
| 895 | /// to be one in the expression. KnownZero contains all the bits that are known |
| 896 | /// to be zero in the expression. These are provided to potentially allow the |
| 897 | /// caller (which might recursively be SimplifyDemandedBits itself) to simplify |
| 898 | /// the expression. KnownOne and KnownZero always follow the invariant that |
| 899 | /// KnownOne & KnownZero == 0. That is, a bit can't be both 1 and 0. Note that |
| 900 | /// the bits in KnownOne and KnownZero may only be accurate for those bits set |
| 901 | /// in DemandedMask. Note also that the bitwidth of V, DemandedMask, KnownZero |
| 902 | /// and KnownOne must all be the same. |
| 903 | bool InstCombiner::SimplifyDemandedBits(Value *V, APInt DemandedMask, |
| 904 | APInt& KnownZero, APInt& KnownOne, |
| 905 | unsigned Depth) { |
| 906 | assert(V != 0 && "Null pointer of Value???"); |
| 907 | assert(Depth <= 6 && "Limit Search Depth"); |
| 908 | uint32_t BitWidth = DemandedMask.getBitWidth(); |
| 909 | const IntegerType *VTy = cast<IntegerType>(V->getType()); |
| 910 | assert(VTy->getBitWidth() == BitWidth && |
| 911 | KnownZero.getBitWidth() == BitWidth && |
| 912 | KnownOne.getBitWidth() == BitWidth && |
| 913 | "Value *V, DemandedMask, KnownZero and KnownOne \ |
| 914 | must have same BitWidth"); |
| 915 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
| 916 | // We know all of the bits for a constant! |
| 917 | KnownOne = CI->getValue() & DemandedMask; |
| 918 | KnownZero = ~KnownOne & DemandedMask; |
| 919 | return false; |
| 920 | } |
| 921 | |
Zhou Sheng | 9670445 | 2007-03-14 03:21:24 +0000 | [diff] [blame] | 922 | KnownZero.clear(); |
| 923 | KnownOne.clear(); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 924 | if (!V->hasOneUse()) { // Other users may use these bits. |
| 925 | if (Depth != 0) { // Not at the root. |
| 926 | // Just compute the KnownZero/KnownOne bits to simplify things downstream. |
| 927 | ComputeMaskedBits(V, DemandedMask, KnownZero, KnownOne, Depth); |
| 928 | return false; |
| 929 | } |
| 930 | // If this is the root being simplified, allow it to have multiple uses, |
| 931 | // just set the DemandedMask to all bits. |
| 932 | DemandedMask = APInt::getAllOnesValue(BitWidth); |
| 933 | } else if (DemandedMask == 0) { // Not demanding any bits from V. |
| 934 | if (V != UndefValue::get(VTy)) |
| 935 | return UpdateValueUsesWith(V, UndefValue::get(VTy)); |
| 936 | return false; |
| 937 | } else if (Depth == 6) { // Limit search depth. |
| 938 | return false; |
| 939 | } |
| 940 | |
| 941 | Instruction *I = dyn_cast<Instruction>(V); |
| 942 | if (!I) return false; // Only analyze instructions. |
| 943 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 944 | APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0); |
| 945 | APInt &RHSKnownZero = KnownZero, &RHSKnownOne = KnownOne; |
| 946 | switch (I->getOpcode()) { |
| 947 | default: break; |
| 948 | case Instruction::And: |
| 949 | // If either the LHS or the RHS are Zero, the result is zero. |
| 950 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, |
| 951 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 952 | return true; |
| 953 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 954 | "Bits known to be one AND zero?"); |
| 955 | |
| 956 | // If something is known zero on the RHS, the bits aren't demanded on the |
| 957 | // LHS. |
| 958 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask & ~RHSKnownZero, |
| 959 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 960 | return true; |
| 961 | assert((LHSKnownZero & LHSKnownOne) == 0 && |
| 962 | "Bits known to be one AND zero?"); |
| 963 | |
| 964 | // If all of the demanded bits are known 1 on one side, return the other. |
| 965 | // These bits cannot contribute to the result of the 'and'. |
| 966 | if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) == |
| 967 | (DemandedMask & ~LHSKnownZero)) |
| 968 | return UpdateValueUsesWith(I, I->getOperand(0)); |
| 969 | if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) == |
| 970 | (DemandedMask & ~RHSKnownZero)) |
| 971 | return UpdateValueUsesWith(I, I->getOperand(1)); |
| 972 | |
| 973 | // If all of the demanded bits in the inputs are known zeros, return zero. |
| 974 | if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask) |
| 975 | return UpdateValueUsesWith(I, Constant::getNullValue(VTy)); |
| 976 | |
| 977 | // If the RHS is a constant, see if we can simplify it. |
| 978 | if (ShrinkDemandedConstant(I, 1, DemandedMask & ~LHSKnownZero)) |
| 979 | return UpdateValueUsesWith(I, I); |
| 980 | |
| 981 | // Output known-1 bits are only known if set in both the LHS & RHS. |
| 982 | RHSKnownOne &= LHSKnownOne; |
| 983 | // Output known-0 are known to be clear if zero in either the LHS | RHS. |
| 984 | RHSKnownZero |= LHSKnownZero; |
| 985 | break; |
| 986 | case Instruction::Or: |
| 987 | // If either the LHS or the RHS are One, the result is One. |
| 988 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, |
| 989 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 990 | return true; |
| 991 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 992 | "Bits known to be one AND zero?"); |
| 993 | // If something is known one on the RHS, the bits aren't demanded on the |
| 994 | // LHS. |
| 995 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask & ~RHSKnownOne, |
| 996 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 997 | return true; |
| 998 | assert((LHSKnownZero & LHSKnownOne) == 0 && |
| 999 | "Bits known to be one AND zero?"); |
| 1000 | |
| 1001 | // If all of the demanded bits are known zero on one side, return the other. |
| 1002 | // These bits cannot contribute to the result of the 'or'. |
| 1003 | if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) == |
| 1004 | (DemandedMask & ~LHSKnownOne)) |
| 1005 | return UpdateValueUsesWith(I, I->getOperand(0)); |
| 1006 | if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) == |
| 1007 | (DemandedMask & ~RHSKnownOne)) |
| 1008 | return UpdateValueUsesWith(I, I->getOperand(1)); |
| 1009 | |
| 1010 | // If all of the potentially set bits on one side are known to be set on |
| 1011 | // the other side, just use the 'other' side. |
| 1012 | if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) == |
| 1013 | (DemandedMask & (~RHSKnownZero))) |
| 1014 | return UpdateValueUsesWith(I, I->getOperand(0)); |
| 1015 | if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) == |
| 1016 | (DemandedMask & (~LHSKnownZero))) |
| 1017 | return UpdateValueUsesWith(I, I->getOperand(1)); |
| 1018 | |
| 1019 | // If the RHS is a constant, see if we can simplify it. |
| 1020 | if (ShrinkDemandedConstant(I, 1, DemandedMask)) |
| 1021 | return UpdateValueUsesWith(I, I); |
| 1022 | |
| 1023 | // Output known-0 bits are only known if clear in both the LHS & RHS. |
| 1024 | RHSKnownZero &= LHSKnownZero; |
| 1025 | // Output known-1 are known to be set if set in either the LHS | RHS. |
| 1026 | RHSKnownOne |= LHSKnownOne; |
| 1027 | break; |
| 1028 | case Instruction::Xor: { |
| 1029 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, |
| 1030 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 1031 | return true; |
| 1032 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1033 | "Bits known to be one AND zero?"); |
| 1034 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, |
| 1035 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1036 | return true; |
| 1037 | assert((LHSKnownZero & LHSKnownOne) == 0 && |
| 1038 | "Bits known to be one AND zero?"); |
| 1039 | |
| 1040 | // If all of the demanded bits are known zero on one side, return the other. |
| 1041 | // These bits cannot contribute to the result of the 'xor'. |
| 1042 | if ((DemandedMask & RHSKnownZero) == DemandedMask) |
| 1043 | return UpdateValueUsesWith(I, I->getOperand(0)); |
| 1044 | if ((DemandedMask & LHSKnownZero) == DemandedMask) |
| 1045 | return UpdateValueUsesWith(I, I->getOperand(1)); |
| 1046 | |
| 1047 | // Output known-0 bits are known if clear or set in both the LHS & RHS. |
| 1048 | APInt KnownZeroOut = (RHSKnownZero & LHSKnownZero) | |
| 1049 | (RHSKnownOne & LHSKnownOne); |
| 1050 | // Output known-1 are known to be set if set in only one of the LHS, RHS. |
| 1051 | APInt KnownOneOut = (RHSKnownZero & LHSKnownOne) | |
| 1052 | (RHSKnownOne & LHSKnownZero); |
| 1053 | |
| 1054 | // If all of the demanded bits are known to be zero on one side or the |
| 1055 | // other, turn this into an *inclusive* or. |
| 1056 | // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0 |
| 1057 | if ((DemandedMask & ~RHSKnownZero & ~LHSKnownZero) == 0) { |
| 1058 | Instruction *Or = |
| 1059 | BinaryOperator::createOr(I->getOperand(0), I->getOperand(1), |
| 1060 | I->getName()); |
| 1061 | InsertNewInstBefore(Or, *I); |
| 1062 | return UpdateValueUsesWith(I, Or); |
| 1063 | } |
| 1064 | |
| 1065 | // If all of the demanded bits on one side are known, and all of the set |
| 1066 | // bits on that side are also known to be set on the other side, turn this |
| 1067 | // into an AND, as we know the bits will be cleared. |
| 1068 | // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2 |
| 1069 | if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) { |
| 1070 | // all known |
| 1071 | if ((RHSKnownOne & LHSKnownOne) == RHSKnownOne) { |
| 1072 | Constant *AndC = ConstantInt::get(~RHSKnownOne & DemandedMask); |
| 1073 | Instruction *And = |
| 1074 | BinaryOperator::createAnd(I->getOperand(0), AndC, "tmp"); |
| 1075 | InsertNewInstBefore(And, *I); |
| 1076 | return UpdateValueUsesWith(I, And); |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | // If the RHS is a constant, see if we can simplify it. |
| 1081 | // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1. |
| 1082 | if (ShrinkDemandedConstant(I, 1, DemandedMask)) |
| 1083 | return UpdateValueUsesWith(I, I); |
| 1084 | |
| 1085 | RHSKnownZero = KnownZeroOut; |
| 1086 | RHSKnownOne = KnownOneOut; |
| 1087 | break; |
| 1088 | } |
| 1089 | case Instruction::Select: |
| 1090 | if (SimplifyDemandedBits(I->getOperand(2), DemandedMask, |
| 1091 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 1092 | return true; |
| 1093 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, |
| 1094 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1095 | return true; |
| 1096 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1097 | "Bits known to be one AND zero?"); |
| 1098 | assert((LHSKnownZero & LHSKnownOne) == 0 && |
| 1099 | "Bits known to be one AND zero?"); |
| 1100 | |
| 1101 | // If the operands are constants, see if we can simplify them. |
| 1102 | if (ShrinkDemandedConstant(I, 1, DemandedMask)) |
| 1103 | return UpdateValueUsesWith(I, I); |
| 1104 | if (ShrinkDemandedConstant(I, 2, DemandedMask)) |
| 1105 | return UpdateValueUsesWith(I, I); |
| 1106 | |
| 1107 | // Only known if known in both the LHS and RHS. |
| 1108 | RHSKnownOne &= LHSKnownOne; |
| 1109 | RHSKnownZero &= LHSKnownZero; |
| 1110 | break; |
| 1111 | case Instruction::Trunc: { |
| 1112 | uint32_t truncBf = |
| 1113 | cast<IntegerType>(I->getOperand(0)->getType())->getBitWidth(); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1114 | DemandedMask.zext(truncBf); |
| 1115 | RHSKnownZero.zext(truncBf); |
| 1116 | RHSKnownOne.zext(truncBf); |
| 1117 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, |
| 1118 | RHSKnownZero, RHSKnownOne, Depth+1)) |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1119 | return true; |
| 1120 | DemandedMask.trunc(BitWidth); |
| 1121 | RHSKnownZero.trunc(BitWidth); |
| 1122 | RHSKnownOne.trunc(BitWidth); |
| 1123 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1124 | "Bits known to be one AND zero?"); |
| 1125 | break; |
| 1126 | } |
| 1127 | case Instruction::BitCast: |
| 1128 | if (!I->getOperand(0)->getType()->isInteger()) |
| 1129 | return false; |
| 1130 | |
| 1131 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, |
| 1132 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 1133 | return true; |
| 1134 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1135 | "Bits known to be one AND zero?"); |
| 1136 | break; |
| 1137 | case Instruction::ZExt: { |
| 1138 | // Compute the bits in the result that are not present in the input. |
| 1139 | const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType()); |
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 1140 | uint32_t SrcBitWidth = SrcTy->getBitWidth(); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1141 | |
Zhou Sheng | d48653a | 2007-03-29 04:45:55 +0000 | [diff] [blame] | 1142 | DemandedMask.trunc(SrcBitWidth); |
| 1143 | RHSKnownZero.trunc(SrcBitWidth); |
| 1144 | RHSKnownOne.trunc(SrcBitWidth); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1145 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, |
| 1146 | RHSKnownZero, RHSKnownOne, Depth+1)) |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1147 | return true; |
| 1148 | DemandedMask.zext(BitWidth); |
| 1149 | RHSKnownZero.zext(BitWidth); |
| 1150 | RHSKnownOne.zext(BitWidth); |
| 1151 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1152 | "Bits known to be one AND zero?"); |
| 1153 | // The top bits are known to be zero. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1154 | RHSKnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1155 | break; |
| 1156 | } |
| 1157 | case Instruction::SExt: { |
| 1158 | // Compute the bits in the result that are not present in the input. |
| 1159 | const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType()); |
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 1160 | uint32_t SrcBitWidth = SrcTy->getBitWidth(); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1161 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1162 | APInt InputDemandedBits = DemandedMask & |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1163 | APInt::getLowBitsSet(BitWidth, SrcBitWidth); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1164 | |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1165 | APInt NewBits(APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1166 | // If any of the sign extended bits are demanded, we know that the sign |
| 1167 | // bit is demanded. |
| 1168 | if ((NewBits & DemandedMask) != 0) |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 1169 | InputDemandedBits.set(SrcBitWidth-1); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1170 | |
Zhou Sheng | d48653a | 2007-03-29 04:45:55 +0000 | [diff] [blame] | 1171 | InputDemandedBits.trunc(SrcBitWidth); |
| 1172 | RHSKnownZero.trunc(SrcBitWidth); |
| 1173 | RHSKnownOne.trunc(SrcBitWidth); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1174 | if (SimplifyDemandedBits(I->getOperand(0), InputDemandedBits, |
| 1175 | RHSKnownZero, RHSKnownOne, Depth+1)) |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1176 | return true; |
| 1177 | InputDemandedBits.zext(BitWidth); |
| 1178 | RHSKnownZero.zext(BitWidth); |
| 1179 | RHSKnownOne.zext(BitWidth); |
| 1180 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1181 | "Bits known to be one AND zero?"); |
| 1182 | |
| 1183 | // If the sign bit of the input is known set or clear, then we know the |
| 1184 | // top bits of the result. |
| 1185 | |
| 1186 | // If the input sign bit is known zero, or if the NewBits are not demanded |
| 1187 | // convert this into a zero extension. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1188 | if (RHSKnownZero[SrcBitWidth-1] || (NewBits & ~DemandedMask) == NewBits) |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1189 | { |
| 1190 | // Convert to ZExt cast |
| 1191 | CastInst *NewCast = new ZExtInst(I->getOperand(0), VTy, I->getName(), I); |
| 1192 | return UpdateValueUsesWith(I, NewCast); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1193 | } else if (RHSKnownOne[SrcBitWidth-1]) { // Input sign bit known set |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1194 | RHSKnownOne |= NewBits; |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1195 | } |
| 1196 | break; |
| 1197 | } |
| 1198 | case Instruction::Add: { |
| 1199 | // Figure out what the input bits are. If the top bits of the and result |
| 1200 | // are not demanded, then the add doesn't demand them from its input |
| 1201 | // either. |
Reid Spencer | 55702aa | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 1202 | uint32_t NLZ = DemandedMask.countLeadingZeros(); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1203 | |
| 1204 | // If there is a constant on the RHS, there are a variety of xformations |
| 1205 | // we can do. |
| 1206 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 1207 | // If null, this should be simplified elsewhere. Some of the xforms here |
| 1208 | // won't work if the RHS is zero. |
| 1209 | if (RHS->isZero()) |
| 1210 | break; |
| 1211 | |
| 1212 | // If the top bit of the output is demanded, demand everything from the |
| 1213 | // input. Otherwise, we demand all the input bits except NLZ top bits. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1214 | APInt InDemandedBits(APInt::getLowBitsSet(BitWidth, BitWidth - NLZ)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1215 | |
| 1216 | // Find information about known zero/one bits in the input. |
| 1217 | if (SimplifyDemandedBits(I->getOperand(0), InDemandedBits, |
| 1218 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1219 | return true; |
| 1220 | |
| 1221 | // If the RHS of the add has bits set that can't affect the input, reduce |
| 1222 | // the constant. |
| 1223 | if (ShrinkDemandedConstant(I, 1, InDemandedBits)) |
| 1224 | return UpdateValueUsesWith(I, I); |
| 1225 | |
| 1226 | // Avoid excess work. |
| 1227 | if (LHSKnownZero == 0 && LHSKnownOne == 0) |
| 1228 | break; |
| 1229 | |
| 1230 | // Turn it into OR if input bits are zero. |
| 1231 | if ((LHSKnownZero & RHS->getValue()) == RHS->getValue()) { |
| 1232 | Instruction *Or = |
| 1233 | BinaryOperator::createOr(I->getOperand(0), I->getOperand(1), |
| 1234 | I->getName()); |
| 1235 | InsertNewInstBefore(Or, *I); |
| 1236 | return UpdateValueUsesWith(I, Or); |
| 1237 | } |
| 1238 | |
| 1239 | // We can say something about the output known-zero and known-one bits, |
| 1240 | // depending on potential carries from the input constant and the |
| 1241 | // unknowns. For example if the LHS is known to have at most the 0x0F0F0 |
| 1242 | // bits set and the RHS constant is 0x01001, then we know we have a known |
| 1243 | // one mask of 0x00001 and a known zero mask of 0xE0F0E. |
| 1244 | |
| 1245 | // To compute this, we first compute the potential carry bits. These are |
| 1246 | // the bits which may be modified. I'm not aware of a better way to do |
| 1247 | // this scan. |
Zhou Sheng | b9cb95f | 2007-03-31 02:38:39 +0000 | [diff] [blame] | 1248 | const APInt& RHSVal = RHS->getValue(); |
| 1249 | APInt CarryBits((~LHSKnownZero + RHSVal) ^ (~LHSKnownZero ^ RHSVal)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1250 | |
| 1251 | // Now that we know which bits have carries, compute the known-1/0 sets. |
| 1252 | |
| 1253 | // Bits are known one if they are known zero in one operand and one in the |
| 1254 | // other, and there is no input carry. |
| 1255 | RHSKnownOne = ((LHSKnownZero & RHSVal) | |
| 1256 | (LHSKnownOne & ~RHSVal)) & ~CarryBits; |
| 1257 | |
| 1258 | // Bits are known zero if they are known zero in both operands and there |
| 1259 | // is no input carry. |
| 1260 | RHSKnownZero = LHSKnownZero & ~RHSVal & ~CarryBits; |
| 1261 | } else { |
| 1262 | // If the high-bits of this ADD are not demanded, then it does not demand |
| 1263 | // the high bits of its LHS or RHS. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1264 | if (DemandedMask[BitWidth-1] == 0) { |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1265 | // Right fill the mask of bits for this ADD to demand the most |
| 1266 | // significant bit and all those below it. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1267 | APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1268 | if (SimplifyDemandedBits(I->getOperand(0), DemandedFromOps, |
| 1269 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1270 | return true; |
| 1271 | if (SimplifyDemandedBits(I->getOperand(1), DemandedFromOps, |
| 1272 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1273 | return true; |
| 1274 | } |
| 1275 | } |
| 1276 | break; |
| 1277 | } |
| 1278 | case Instruction::Sub: |
| 1279 | // If the high-bits of this SUB are not demanded, then it does not demand |
| 1280 | // the high bits of its LHS or RHS. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1281 | if (DemandedMask[BitWidth-1] == 0) { |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1282 | // Right fill the mask of bits for this SUB to demand the most |
| 1283 | // significant bit and all those below it. |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 1284 | uint32_t NLZ = DemandedMask.countLeadingZeros(); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1285 | APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1286 | if (SimplifyDemandedBits(I->getOperand(0), DemandedFromOps, |
| 1287 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1288 | return true; |
| 1289 | if (SimplifyDemandedBits(I->getOperand(1), DemandedFromOps, |
| 1290 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| 1291 | return true; |
| 1292 | } |
| 1293 | break; |
| 1294 | case Instruction::Shl: |
| 1295 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 1296 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1297 | APInt DemandedMaskIn(DemandedMask.lshr(ShiftAmt)); |
| 1298 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMaskIn, |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1299 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 1300 | return true; |
| 1301 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1302 | "Bits known to be one AND zero?"); |
| 1303 | RHSKnownZero <<= ShiftAmt; |
| 1304 | RHSKnownOne <<= ShiftAmt; |
| 1305 | // low bits known zero. |
Zhou Sheng | adc1495 | 2007-03-14 09:07:33 +0000 | [diff] [blame] | 1306 | if (ShiftAmt) |
Zhou Sheng | e9e03f6 | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 1307 | RHSKnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1308 | } |
| 1309 | break; |
| 1310 | case Instruction::LShr: |
| 1311 | // For a logical shift right |
| 1312 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 1313 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1314 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1315 | // Unsigned shift right. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1316 | APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt)); |
| 1317 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMaskIn, |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1318 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 1319 | return true; |
| 1320 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1321 | "Bits known to be one AND zero?"); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1322 | RHSKnownZero = APIntOps::lshr(RHSKnownZero, ShiftAmt); |
| 1323 | RHSKnownOne = APIntOps::lshr(RHSKnownOne, ShiftAmt); |
Zhou Sheng | adc1495 | 2007-03-14 09:07:33 +0000 | [diff] [blame] | 1324 | if (ShiftAmt) { |
| 1325 | // Compute the new bits that are at the top now. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1326 | APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt)); |
Zhou Sheng | adc1495 | 2007-03-14 09:07:33 +0000 | [diff] [blame] | 1327 | RHSKnownZero |= HighBits; // high bits known zero. |
| 1328 | } |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1329 | } |
| 1330 | break; |
| 1331 | case Instruction::AShr: |
| 1332 | // If this is an arithmetic shift right and only the low-bit is set, we can |
| 1333 | // always convert this into a logical shr, even if the shift amount is |
| 1334 | // variable. The low bit of the shift cannot be an input sign bit unless |
| 1335 | // the shift amount is >= the size of the datatype, which is undefined. |
| 1336 | if (DemandedMask == 1) { |
| 1337 | // Perform the logical shift right. |
| 1338 | Value *NewVal = BinaryOperator::createLShr( |
| 1339 | I->getOperand(0), I->getOperand(1), I->getName()); |
| 1340 | InsertNewInstBefore(cast<Instruction>(NewVal), *I); |
| 1341 | return UpdateValueUsesWith(I, NewVal); |
| 1342 | } |
| 1343 | |
| 1344 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 1345 | uint32_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1346 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1347 | // Signed shift right. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1348 | APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt)); |
Lauro Ramos Venancio | d0499af | 2007-06-06 17:08:48 +0000 | [diff] [blame^] | 1349 | // If any of the "high bits" are demanded, we should set the sign bit as |
| 1350 | // demanded. |
| 1351 | if (DemandedMask.countLeadingZeros() <= ShiftAmt) |
| 1352 | DemandedMaskIn.set(BitWidth-1); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1353 | if (SimplifyDemandedBits(I->getOperand(0), |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1354 | DemandedMaskIn, |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1355 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| 1356 | return true; |
| 1357 | assert((RHSKnownZero & RHSKnownOne) == 0 && |
| 1358 | "Bits known to be one AND zero?"); |
| 1359 | // Compute the new bits that are at the top now. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1360 | APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1361 | RHSKnownZero = APIntOps::lshr(RHSKnownZero, ShiftAmt); |
| 1362 | RHSKnownOne = APIntOps::lshr(RHSKnownOne, ShiftAmt); |
| 1363 | |
| 1364 | // Handle the sign bits. |
| 1365 | APInt SignBit(APInt::getSignBit(BitWidth)); |
| 1366 | // Adjust to where it is now in the mask. |
| 1367 | SignBit = APIntOps::lshr(SignBit, ShiftAmt); |
| 1368 | |
| 1369 | // If the input sign bit is known to be zero, or if none of the top bits |
| 1370 | // are demanded, turn this into an unsigned shift right. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1371 | if (RHSKnownZero[BitWidth-ShiftAmt-1] || |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1372 | (HighBits & ~DemandedMask) == HighBits) { |
| 1373 | // Perform the logical shift right. |
| 1374 | Value *NewVal = BinaryOperator::createLShr( |
| 1375 | I->getOperand(0), SA, I->getName()); |
| 1376 | InsertNewInstBefore(cast<Instruction>(NewVal), *I); |
| 1377 | return UpdateValueUsesWith(I, NewVal); |
| 1378 | } else if ((RHSKnownOne & SignBit) != 0) { // New bits are known one. |
| 1379 | RHSKnownOne |= HighBits; |
| 1380 | } |
| 1381 | } |
| 1382 | break; |
| 1383 | } |
| 1384 | |
| 1385 | // If the client is only demanding bits that we know, return the known |
| 1386 | // constant. |
| 1387 | if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) |
| 1388 | return UpdateValueUsesWith(I, ConstantInt::get(RHSKnownOne)); |
| 1389 | return false; |
| 1390 | } |
| 1391 | |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1392 | |
| 1393 | /// SimplifyDemandedVectorElts - The specified value producecs a vector with |
| 1394 | /// 64 or fewer elements. DemandedElts contains the set of elements that are |
| 1395 | /// actually used by the caller. This method analyzes which elements of the |
| 1396 | /// operand are undef and returns that information in UndefElts. |
| 1397 | /// |
| 1398 | /// If the information about demanded elements can be used to simplify the |
| 1399 | /// operation, the operation is simplified, then the resultant value is |
| 1400 | /// returned. This returns null if no change was made. |
| 1401 | Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, uint64_t DemandedElts, |
| 1402 | uint64_t &UndefElts, |
| 1403 | unsigned Depth) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1404 | unsigned VWidth = cast<VectorType>(V->getType())->getNumElements(); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1405 | assert(VWidth <= 64 && "Vector too wide to analyze!"); |
| 1406 | uint64_t EltMask = ~0ULL >> (64-VWidth); |
| 1407 | assert(DemandedElts != EltMask && (DemandedElts & ~EltMask) == 0 && |
| 1408 | "Invalid DemandedElts!"); |
| 1409 | |
| 1410 | if (isa<UndefValue>(V)) { |
| 1411 | // If the entire vector is undefined, just return this info. |
| 1412 | UndefElts = EltMask; |
| 1413 | return 0; |
| 1414 | } else if (DemandedElts == 0) { // If nothing is demanded, provide undef. |
| 1415 | UndefElts = EltMask; |
| 1416 | return UndefValue::get(V->getType()); |
| 1417 | } |
| 1418 | |
| 1419 | UndefElts = 0; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1420 | if (ConstantVector *CP = dyn_cast<ConstantVector>(V)) { |
| 1421 | const Type *EltTy = cast<VectorType>(V->getType())->getElementType(); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1422 | Constant *Undef = UndefValue::get(EltTy); |
| 1423 | |
| 1424 | std::vector<Constant*> Elts; |
| 1425 | for (unsigned i = 0; i != VWidth; ++i) |
| 1426 | if (!(DemandedElts & (1ULL << i))) { // If not demanded, set to undef. |
| 1427 | Elts.push_back(Undef); |
| 1428 | UndefElts |= (1ULL << i); |
| 1429 | } else if (isa<UndefValue>(CP->getOperand(i))) { // Already undef. |
| 1430 | Elts.push_back(Undef); |
| 1431 | UndefElts |= (1ULL << i); |
| 1432 | } else { // Otherwise, defined. |
| 1433 | Elts.push_back(CP->getOperand(i)); |
| 1434 | } |
| 1435 | |
| 1436 | // If we changed the constant, return it. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1437 | Constant *NewCP = ConstantVector::get(Elts); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1438 | return NewCP != CP ? NewCP : 0; |
| 1439 | } else if (isa<ConstantAggregateZero>(V)) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1440 | // Simplify the CAZ to a ConstantVector where the non-demanded elements are |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1441 | // set to undef. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1442 | const Type *EltTy = cast<VectorType>(V->getType())->getElementType(); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1443 | Constant *Zero = Constant::getNullValue(EltTy); |
| 1444 | Constant *Undef = UndefValue::get(EltTy); |
| 1445 | std::vector<Constant*> Elts; |
| 1446 | for (unsigned i = 0; i != VWidth; ++i) |
| 1447 | Elts.push_back((DemandedElts & (1ULL << i)) ? Zero : Undef); |
| 1448 | UndefElts = DemandedElts ^ EltMask; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1449 | return ConstantVector::get(Elts); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1450 | } |
| 1451 | |
| 1452 | if (!V->hasOneUse()) { // Other users may use these bits. |
| 1453 | if (Depth != 0) { // Not at the root. |
| 1454 | // TODO: Just compute the UndefElts information recursively. |
| 1455 | return false; |
| 1456 | } |
| 1457 | return false; |
| 1458 | } else if (Depth == 10) { // Limit search depth. |
| 1459 | return false; |
| 1460 | } |
| 1461 | |
| 1462 | Instruction *I = dyn_cast<Instruction>(V); |
| 1463 | if (!I) return false; // Only analyze instructions. |
| 1464 | |
| 1465 | bool MadeChange = false; |
| 1466 | uint64_t UndefElts2; |
| 1467 | Value *TmpV; |
| 1468 | switch (I->getOpcode()) { |
| 1469 | default: break; |
| 1470 | |
| 1471 | case Instruction::InsertElement: { |
| 1472 | // If this is a variable index, we don't know which element it overwrites. |
| 1473 | // demand exactly the same input as we produce. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1474 | ConstantInt *Idx = dyn_cast<ConstantInt>(I->getOperand(2)); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1475 | if (Idx == 0) { |
| 1476 | // Note that we can't propagate undef elt info, because we don't know |
| 1477 | // which elt is getting updated. |
| 1478 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, |
| 1479 | UndefElts2, Depth+1); |
| 1480 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } |
| 1481 | break; |
| 1482 | } |
| 1483 | |
| 1484 | // If this is inserting an element that isn't demanded, remove this |
| 1485 | // insertelement. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1486 | unsigned IdxNo = Idx->getZExtValue(); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1487 | if (IdxNo >= VWidth || (DemandedElts & (1ULL << IdxNo)) == 0) |
| 1488 | return AddSoonDeadInstToWorklist(*I, 0); |
| 1489 | |
| 1490 | // Otherwise, the element inserted overwrites whatever was there, so the |
| 1491 | // input demanded set is simpler than the output set. |
| 1492 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), |
| 1493 | DemandedElts & ~(1ULL << IdxNo), |
| 1494 | UndefElts, Depth+1); |
| 1495 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } |
| 1496 | |
| 1497 | // The inserted element is defined. |
| 1498 | UndefElts |= 1ULL << IdxNo; |
| 1499 | break; |
| 1500 | } |
Chris Lattner | 6987833 | 2007-04-14 22:29:23 +0000 | [diff] [blame] | 1501 | case Instruction::BitCast: { |
| 1502 | // Packed->packed casts only. |
| 1503 | const VectorType *VTy = dyn_cast<VectorType>(I->getOperand(0)->getType()); |
| 1504 | if (!VTy) break; |
| 1505 | unsigned InVWidth = VTy->getNumElements(); |
| 1506 | uint64_t InputDemandedElts = 0; |
| 1507 | unsigned Ratio; |
| 1508 | |
| 1509 | if (VWidth == InVWidth) { |
| 1510 | // If we are converting from <4x i32> -> <4 x f32>, we demand the same |
| 1511 | // elements as are demanded of us. |
| 1512 | Ratio = 1; |
| 1513 | InputDemandedElts = DemandedElts; |
| 1514 | } else if (VWidth > InVWidth) { |
| 1515 | // Untested so far. |
| 1516 | break; |
| 1517 | |
| 1518 | // If there are more elements in the result than there are in the source, |
| 1519 | // then an input element is live if any of the corresponding output |
| 1520 | // elements are live. |
| 1521 | Ratio = VWidth/InVWidth; |
| 1522 | for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) { |
| 1523 | if (DemandedElts & (1ULL << OutIdx)) |
| 1524 | InputDemandedElts |= 1ULL << (OutIdx/Ratio); |
| 1525 | } |
| 1526 | } else { |
| 1527 | // Untested so far. |
| 1528 | break; |
| 1529 | |
| 1530 | // If there are more elements in the source than there are in the result, |
| 1531 | // then an input element is live if the corresponding output element is |
| 1532 | // live. |
| 1533 | Ratio = InVWidth/VWidth; |
| 1534 | for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx) |
| 1535 | if (DemandedElts & (1ULL << InIdx/Ratio)) |
| 1536 | InputDemandedElts |= 1ULL << InIdx; |
| 1537 | } |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1538 | |
Chris Lattner | 6987833 | 2007-04-14 22:29:23 +0000 | [diff] [blame] | 1539 | // div/rem demand all inputs, because they don't want divide by zero. |
| 1540 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), InputDemandedElts, |
| 1541 | UndefElts2, Depth+1); |
| 1542 | if (TmpV) { |
| 1543 | I->setOperand(0, TmpV); |
| 1544 | MadeChange = true; |
| 1545 | } |
| 1546 | |
| 1547 | UndefElts = UndefElts2; |
| 1548 | if (VWidth > InVWidth) { |
| 1549 | assert(0 && "Unimp"); |
| 1550 | // If there are more elements in the result than there are in the source, |
| 1551 | // then an output element is undef if the corresponding input element is |
| 1552 | // undef. |
| 1553 | for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) |
| 1554 | if (UndefElts2 & (1ULL << (OutIdx/Ratio))) |
| 1555 | UndefElts |= 1ULL << OutIdx; |
| 1556 | } else if (VWidth < InVWidth) { |
| 1557 | assert(0 && "Unimp"); |
| 1558 | // If there are more elements in the source than there are in the result, |
| 1559 | // then a result element is undef if all of the corresponding input |
| 1560 | // elements are undef. |
| 1561 | UndefElts = ~0ULL >> (64-VWidth); // Start out all undef. |
| 1562 | for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx) |
| 1563 | if ((UndefElts2 & (1ULL << InIdx)) == 0) // Not undef? |
| 1564 | UndefElts &= ~(1ULL << (InIdx/Ratio)); // Clear undef bit. |
| 1565 | } |
| 1566 | break; |
| 1567 | } |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1568 | case Instruction::And: |
| 1569 | case Instruction::Or: |
| 1570 | case Instruction::Xor: |
| 1571 | case Instruction::Add: |
| 1572 | case Instruction::Sub: |
| 1573 | case Instruction::Mul: |
| 1574 | // div/rem demand all inputs, because they don't want divide by zero. |
| 1575 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, |
| 1576 | UndefElts, Depth+1); |
| 1577 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } |
| 1578 | TmpV = SimplifyDemandedVectorElts(I->getOperand(1), DemandedElts, |
| 1579 | UndefElts2, Depth+1); |
| 1580 | if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; } |
| 1581 | |
| 1582 | // Output elements are undefined if both are undefined. Consider things |
| 1583 | // like undef&0. The result is known zero, not undef. |
| 1584 | UndefElts &= UndefElts2; |
| 1585 | break; |
| 1586 | |
| 1587 | case Instruction::Call: { |
| 1588 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(I); |
| 1589 | if (!II) break; |
| 1590 | switch (II->getIntrinsicID()) { |
| 1591 | default: break; |
| 1592 | |
| 1593 | // Binary vector operations that work column-wise. A dest element is a |
| 1594 | // function of the corresponding input elements from the two inputs. |
| 1595 | case Intrinsic::x86_sse_sub_ss: |
| 1596 | case Intrinsic::x86_sse_mul_ss: |
| 1597 | case Intrinsic::x86_sse_min_ss: |
| 1598 | case Intrinsic::x86_sse_max_ss: |
| 1599 | case Intrinsic::x86_sse2_sub_sd: |
| 1600 | case Intrinsic::x86_sse2_mul_sd: |
| 1601 | case Intrinsic::x86_sse2_min_sd: |
| 1602 | case Intrinsic::x86_sse2_max_sd: |
| 1603 | TmpV = SimplifyDemandedVectorElts(II->getOperand(1), DemandedElts, |
| 1604 | UndefElts, Depth+1); |
| 1605 | if (TmpV) { II->setOperand(1, TmpV); MadeChange = true; } |
| 1606 | TmpV = SimplifyDemandedVectorElts(II->getOperand(2), DemandedElts, |
| 1607 | UndefElts2, Depth+1); |
| 1608 | if (TmpV) { II->setOperand(2, TmpV); MadeChange = true; } |
| 1609 | |
| 1610 | // If only the low elt is demanded and this is a scalarizable intrinsic, |
| 1611 | // scalarize it now. |
| 1612 | if (DemandedElts == 1) { |
| 1613 | switch (II->getIntrinsicID()) { |
| 1614 | default: break; |
| 1615 | case Intrinsic::x86_sse_sub_ss: |
| 1616 | case Intrinsic::x86_sse_mul_ss: |
| 1617 | case Intrinsic::x86_sse2_sub_sd: |
| 1618 | case Intrinsic::x86_sse2_mul_sd: |
| 1619 | // TODO: Lower MIN/MAX/ABS/etc |
| 1620 | Value *LHS = II->getOperand(1); |
| 1621 | Value *RHS = II->getOperand(2); |
| 1622 | // Extract the element as scalars. |
| 1623 | LHS = InsertNewInstBefore(new ExtractElementInst(LHS, 0U,"tmp"), *II); |
| 1624 | RHS = InsertNewInstBefore(new ExtractElementInst(RHS, 0U,"tmp"), *II); |
| 1625 | |
| 1626 | switch (II->getIntrinsicID()) { |
| 1627 | default: assert(0 && "Case stmts out of sync!"); |
| 1628 | case Intrinsic::x86_sse_sub_ss: |
| 1629 | case Intrinsic::x86_sse2_sub_sd: |
| 1630 | TmpV = InsertNewInstBefore(BinaryOperator::createSub(LHS, RHS, |
| 1631 | II->getName()), *II); |
| 1632 | break; |
| 1633 | case Intrinsic::x86_sse_mul_ss: |
| 1634 | case Intrinsic::x86_sse2_mul_sd: |
| 1635 | TmpV = InsertNewInstBefore(BinaryOperator::createMul(LHS, RHS, |
| 1636 | II->getName()), *II); |
| 1637 | break; |
| 1638 | } |
| 1639 | |
| 1640 | Instruction *New = |
| 1641 | new InsertElementInst(UndefValue::get(II->getType()), TmpV, 0U, |
| 1642 | II->getName()); |
| 1643 | InsertNewInstBefore(New, *II); |
| 1644 | AddSoonDeadInstToWorklist(*II, 0); |
| 1645 | return New; |
| 1646 | } |
| 1647 | } |
| 1648 | |
| 1649 | // Output elements are undefined if both are undefined. Consider things |
| 1650 | // like undef&0. The result is known zero, not undef. |
| 1651 | UndefElts &= UndefElts2; |
| 1652 | break; |
| 1653 | } |
| 1654 | break; |
| 1655 | } |
| 1656 | } |
| 1657 | return MadeChange ? I : 0; |
| 1658 | } |
| 1659 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1660 | /// @returns true if the specified compare instruction is |
| 1661 | /// true when both operands are equal... |
| 1662 | /// @brief Determine if the ICmpInst returns true if both operands are equal |
| 1663 | static bool isTrueWhenEqual(ICmpInst &ICI) { |
| 1664 | ICmpInst::Predicate pred = ICI.getPredicate(); |
| 1665 | return pred == ICmpInst::ICMP_EQ || pred == ICmpInst::ICMP_UGE || |
| 1666 | pred == ICmpInst::ICMP_SGE || pred == ICmpInst::ICMP_ULE || |
| 1667 | pred == ICmpInst::ICMP_SLE; |
| 1668 | } |
| 1669 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1670 | /// AssociativeOpt - Perform an optimization on an associative operator. This |
| 1671 | /// function is designed to check a chain of associative operators for a |
| 1672 | /// potential to apply a certain optimization. Since the optimization may be |
| 1673 | /// applicable if the expression was reassociated, this checks the chain, then |
| 1674 | /// reassociates the expression as necessary to expose the optimization |
| 1675 | /// opportunity. This makes use of a special Functor, which must define |
| 1676 | /// 'shouldApply' and 'apply' methods. |
| 1677 | /// |
| 1678 | template<typename Functor> |
| 1679 | Instruction *AssociativeOpt(BinaryOperator &Root, const Functor &F) { |
| 1680 | unsigned Opcode = Root.getOpcode(); |
| 1681 | Value *LHS = Root.getOperand(0); |
| 1682 | |
| 1683 | // Quick check, see if the immediate LHS matches... |
| 1684 | if (F.shouldApply(LHS)) |
| 1685 | return F.apply(Root); |
| 1686 | |
| 1687 | // Otherwise, if the LHS is not of the same opcode as the root, return. |
| 1688 | Instruction *LHSI = dyn_cast<Instruction>(LHS); |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 1689 | while (LHSI && LHSI->getOpcode() == Opcode && LHSI->hasOneUse()) { |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1690 | // Should we apply this transform to the RHS? |
| 1691 | bool ShouldApply = F.shouldApply(LHSI->getOperand(1)); |
| 1692 | |
| 1693 | // If not to the RHS, check to see if we should apply to the LHS... |
| 1694 | if (!ShouldApply && F.shouldApply(LHSI->getOperand(0))) { |
| 1695 | cast<BinaryOperator>(LHSI)->swapOperands(); // Make the LHS the RHS |
| 1696 | ShouldApply = true; |
| 1697 | } |
| 1698 | |
| 1699 | // If the functor wants to apply the optimization to the RHS of LHSI, |
| 1700 | // reassociate the expression from ((? op A) op B) to (? op (A op B)) |
| 1701 | if (ShouldApply) { |
| 1702 | BasicBlock *BB = Root.getParent(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1703 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1704 | // Now all of the instructions are in the current basic block, go ahead |
| 1705 | // and perform the reassociation. |
| 1706 | Instruction *TmpLHSI = cast<Instruction>(Root.getOperand(0)); |
| 1707 | |
| 1708 | // First move the selected RHS to the LHS of the root... |
| 1709 | Root.setOperand(0, LHSI->getOperand(1)); |
| 1710 | |
| 1711 | // Make what used to be the LHS of the root be the user of the root... |
| 1712 | Value *ExtraOperand = TmpLHSI->getOperand(1); |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1713 | if (&Root == TmpLHSI) { |
Chris Lattner | 15a76c0 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 1714 | Root.replaceAllUsesWith(Constant::getNullValue(TmpLHSI->getType())); |
| 1715 | return 0; |
| 1716 | } |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1717 | Root.replaceAllUsesWith(TmpLHSI); // Users now use TmpLHSI |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1718 | TmpLHSI->setOperand(1, &Root); // TmpLHSI now uses the root |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1719 | TmpLHSI->getParent()->getInstList().remove(TmpLHSI); |
| 1720 | BasicBlock::iterator ARI = &Root; ++ARI; |
| 1721 | BB->getInstList().insert(ARI, TmpLHSI); // Move TmpLHSI to after Root |
| 1722 | ARI = Root; |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1723 | |
| 1724 | // Now propagate the ExtraOperand down the chain of instructions until we |
| 1725 | // get to LHSI. |
| 1726 | while (TmpLHSI != LHSI) { |
| 1727 | Instruction *NextLHSI = cast<Instruction>(TmpLHSI->getOperand(0)); |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1728 | // Move the instruction to immediately before the chain we are |
| 1729 | // constructing to avoid breaking dominance properties. |
| 1730 | NextLHSI->getParent()->getInstList().remove(NextLHSI); |
| 1731 | BB->getInstList().insert(ARI, NextLHSI); |
| 1732 | ARI = NextLHSI; |
| 1733 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1734 | Value *NextOp = NextLHSI->getOperand(1); |
| 1735 | NextLHSI->setOperand(1, ExtraOperand); |
| 1736 | TmpLHSI = NextLHSI; |
| 1737 | ExtraOperand = NextOp; |
| 1738 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1739 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1740 | // Now that the instructions are reassociated, have the functor perform |
| 1741 | // the transformation... |
| 1742 | return F.apply(Root); |
| 1743 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1744 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1745 | LHSI = dyn_cast<Instruction>(LHSI->getOperand(0)); |
| 1746 | } |
| 1747 | return 0; |
| 1748 | } |
| 1749 | |
| 1750 | |
| 1751 | // AddRHS - Implements: X + X --> X << 1 |
| 1752 | struct AddRHS { |
| 1753 | Value *RHS; |
| 1754 | AddRHS(Value *rhs) : RHS(rhs) {} |
| 1755 | bool shouldApply(Value *LHS) const { return LHS == RHS; } |
| 1756 | Instruction *apply(BinaryOperator &Add) const { |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 1757 | return BinaryOperator::createShl(Add.getOperand(0), |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1758 | ConstantInt::get(Add.getType(), 1)); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1759 | } |
| 1760 | }; |
| 1761 | |
| 1762 | // AddMaskingAnd - Implements (A & C1)+(B & C2) --> (A & C1)|(B & C2) |
| 1763 | // iff C1&C2 == 0 |
| 1764 | struct AddMaskingAnd { |
| 1765 | Constant *C2; |
| 1766 | AddMaskingAnd(Constant *c) : C2(c) {} |
| 1767 | bool shouldApply(Value *LHS) const { |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1768 | ConstantInt *C1; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1769 | return match(LHS, m_And(m_Value(), m_ConstantInt(C1))) && |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1770 | ConstantExpr::getAnd(C1, C2)->isNullValue(); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1771 | } |
| 1772 | Instruction *apply(BinaryOperator &Add) const { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1773 | return BinaryOperator::createOr(Add.getOperand(0), Add.getOperand(1)); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1774 | } |
| 1775 | }; |
| 1776 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1777 | static Value *FoldOperationIntoSelectOperand(Instruction &I, Value *SO, |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1778 | InstCombiner *IC) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1779 | if (CastInst *CI = dyn_cast<CastInst>(&I)) { |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1780 | if (Constant *SOC = dyn_cast<Constant>(SO)) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1781 | return ConstantExpr::getCast(CI->getOpcode(), SOC, I.getType()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1782 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1783 | return IC->InsertNewInstBefore(CastInst::create( |
| 1784 | CI->getOpcode(), SO, I.getType(), SO->getName() + ".cast"), I); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1785 | } |
| 1786 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1787 | // Figure out if the constant is the left or the right argument. |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1788 | bool ConstIsRHS = isa<Constant>(I.getOperand(1)); |
| 1789 | Constant *ConstOperand = cast<Constant>(I.getOperand(ConstIsRHS)); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1790 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1791 | if (Constant *SOC = dyn_cast<Constant>(SO)) { |
| 1792 | if (ConstIsRHS) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1793 | return ConstantExpr::get(I.getOpcode(), SOC, ConstOperand); |
| 1794 | return ConstantExpr::get(I.getOpcode(), ConstOperand, SOC); |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1795 | } |
| 1796 | |
| 1797 | Value *Op0 = SO, *Op1 = ConstOperand; |
| 1798 | if (!ConstIsRHS) |
| 1799 | std::swap(Op0, Op1); |
| 1800 | Instruction *New; |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1801 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I)) |
| 1802 | New = BinaryOperator::create(BO->getOpcode(), Op0, Op1,SO->getName()+".op"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1803 | else if (CmpInst *CI = dyn_cast<CmpInst>(&I)) |
| 1804 | New = CmpInst::create(CI->getOpcode(), CI->getPredicate(), Op0, Op1, |
| 1805 | SO->getName()+".cmp"); |
Chris Lattner | 326c0f3 | 2004-04-10 19:15:56 +0000 | [diff] [blame] | 1806 | else { |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1807 | assert(0 && "Unknown binary instruction type!"); |
Chris Lattner | 326c0f3 | 2004-04-10 19:15:56 +0000 | [diff] [blame] | 1808 | abort(); |
| 1809 | } |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1810 | return IC->InsertNewInstBefore(New, I); |
| 1811 | } |
| 1812 | |
| 1813 | // FoldOpIntoSelect - Given an instruction with a select as one operand and a |
| 1814 | // constant as the other operand, try to fold the binary operator into the |
| 1815 | // select arguments. This also works for Cast instructions, which obviously do |
| 1816 | // not have a second operand. |
| 1817 | static Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI, |
| 1818 | InstCombiner *IC) { |
| 1819 | // Don't modify shared select instructions |
| 1820 | if (!SI->hasOneUse()) return 0; |
| 1821 | Value *TV = SI->getOperand(1); |
| 1822 | Value *FV = SI->getOperand(2); |
| 1823 | |
| 1824 | if (isa<Constant>(TV) || isa<Constant>(FV)) { |
Chris Lattner | 956db27 | 2005-04-21 05:43:13 +0000 | [diff] [blame] | 1825 | // Bool selects with constant operands can be folded to logical ops. |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 1826 | if (SI->getType() == Type::Int1Ty) return 0; |
Chris Lattner | 956db27 | 2005-04-21 05:43:13 +0000 | [diff] [blame] | 1827 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1828 | Value *SelectTrueVal = FoldOperationIntoSelectOperand(Op, TV, IC); |
| 1829 | Value *SelectFalseVal = FoldOperationIntoSelectOperand(Op, FV, IC); |
| 1830 | |
| 1831 | return new SelectInst(SI->getCondition(), SelectTrueVal, |
| 1832 | SelectFalseVal); |
| 1833 | } |
| 1834 | return 0; |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1835 | } |
| 1836 | |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1837 | |
| 1838 | /// FoldOpIntoPhi - Given a binary operator or cast instruction which has a PHI |
| 1839 | /// node as operand #0, see if we can fold the instruction into the PHI (which |
| 1840 | /// is only possible if all operands to the PHI are constants). |
| 1841 | Instruction *InstCombiner::FoldOpIntoPhi(Instruction &I) { |
| 1842 | PHINode *PN = cast<PHINode>(I.getOperand(0)); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 1843 | unsigned NumPHIValues = PN->getNumIncomingValues(); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1844 | if (!PN->hasOneUse() || NumPHIValues == 0) return 0; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1845 | |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1846 | // Check to see if all of the operands of the PHI are constants. If there is |
| 1847 | // 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] | 1848 | // or if *it* is a PHI, bail out. |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1849 | BasicBlock *NonConstBB = 0; |
| 1850 | for (unsigned i = 0; i != NumPHIValues; ++i) |
| 1851 | if (!isa<Constant>(PN->getIncomingValue(i))) { |
| 1852 | if (NonConstBB) return 0; // More than one non-const value. |
Chris Lattner | b303668 | 2007-02-24 01:03:45 +0000 | [diff] [blame] | 1853 | if (isa<PHINode>(PN->getIncomingValue(i))) return 0; // Itself a phi. |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1854 | NonConstBB = PN->getIncomingBlock(i); |
| 1855 | |
| 1856 | // If the incoming non-constant value is in I's block, we have an infinite |
| 1857 | // loop. |
| 1858 | if (NonConstBB == I.getParent()) |
| 1859 | return 0; |
| 1860 | } |
| 1861 | |
| 1862 | // If there is exactly one non-constant value, we can insert a copy of the |
| 1863 | // operation in that block. However, if this is a critical edge, we would be |
| 1864 | // inserting the computation one some other paths (e.g. inside a loop). Only |
| 1865 | // do this if the pred block is unconditionally branching into the phi block. |
| 1866 | if (NonConstBB) { |
| 1867 | BranchInst *BI = dyn_cast<BranchInst>(NonConstBB->getTerminator()); |
| 1868 | if (!BI || !BI->isUnconditional()) return 0; |
| 1869 | } |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1870 | |
| 1871 | // Okay, we can do the transformation: create the new PHI node. |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 1872 | PHINode *NewPN = new PHINode(I.getType(), ""); |
Chris Lattner | 5551706 | 2005-01-29 00:39:08 +0000 | [diff] [blame] | 1873 | NewPN->reserveOperandSpace(PN->getNumOperands()/2); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1874 | InsertNewInstBefore(NewPN, *PN); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 1875 | NewPN->takeName(PN); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1876 | |
| 1877 | // Next, add all of the operands to the PHI. |
| 1878 | if (I.getNumOperands() == 2) { |
| 1879 | Constant *C = cast<Constant>(I.getOperand(1)); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 1880 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1881 | Value *InV; |
| 1882 | if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1883 | if (CmpInst *CI = dyn_cast<CmpInst>(&I)) |
| 1884 | InV = ConstantExpr::getCompare(CI->getPredicate(), InC, C); |
| 1885 | else |
| 1886 | InV = ConstantExpr::get(I.getOpcode(), InC, C); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1887 | } else { |
| 1888 | assert(PN->getIncomingBlock(i) == NonConstBB); |
| 1889 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I)) |
| 1890 | InV = BinaryOperator::create(BO->getOpcode(), |
| 1891 | PN->getIncomingValue(i), C, "phitmp", |
| 1892 | NonConstBB->getTerminator()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1893 | else if (CmpInst *CI = dyn_cast<CmpInst>(&I)) |
| 1894 | InV = CmpInst::create(CI->getOpcode(), |
| 1895 | CI->getPredicate(), |
| 1896 | PN->getIncomingValue(i), C, "phitmp", |
| 1897 | NonConstBB->getTerminator()); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1898 | else |
| 1899 | assert(0 && "Unknown binop!"); |
| 1900 | |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 1901 | AddToWorkList(cast<Instruction>(InV)); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1902 | } |
| 1903 | NewPN->addIncoming(InV, PN->getIncomingBlock(i)); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1904 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1905 | } else { |
| 1906 | CastInst *CI = cast<CastInst>(&I); |
| 1907 | const Type *RetTy = CI->getType(); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 1908 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1909 | Value *InV; |
| 1910 | if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1911 | InV = ConstantExpr::getCast(CI->getOpcode(), InC, RetTy); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1912 | } else { |
| 1913 | assert(PN->getIncomingBlock(i) == NonConstBB); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1914 | InV = CastInst::create(CI->getOpcode(), PN->getIncomingValue(i), |
| 1915 | I.getType(), "phitmp", |
| 1916 | NonConstBB->getTerminator()); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 1917 | AddToWorkList(cast<Instruction>(InV)); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1918 | } |
| 1919 | NewPN->addIncoming(InV, PN->getIncomingBlock(i)); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1920 | } |
| 1921 | } |
| 1922 | return ReplaceInstUsesWith(I, NewPN); |
| 1923 | } |
| 1924 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1925 | Instruction *InstCombiner::visitAdd(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 1926 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1927 | Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 1928 | |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 1929 | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 1930 | // X + undef -> undef |
| 1931 | if (isa<UndefValue>(RHS)) |
| 1932 | return ReplaceInstUsesWith(I, RHS); |
| 1933 | |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 1934 | // X + 0 --> X |
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 1935 | if (!I.getType()->isFPOrFPVector()) { // NOTE: -0 + +0 = +0. |
Chris Lattner | 5e678e0 | 2005-10-17 17:56:38 +0000 | [diff] [blame] | 1936 | if (RHSC->isNullValue()) |
| 1937 | return ReplaceInstUsesWith(I, LHS); |
Chris Lattner | 8532cf6 | 2005-10-17 20:18:38 +0000 | [diff] [blame] | 1938 | } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) { |
| 1939 | if (CFP->isExactlyValue(-0.0)) |
| 1940 | return ReplaceInstUsesWith(I, LHS); |
Chris Lattner | 5e678e0 | 2005-10-17 17:56:38 +0000 | [diff] [blame] | 1941 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1942 | |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 1943 | if (ConstantInt *CI = dyn_cast<ConstantInt>(RHSC)) { |
Chris Lattner | b4a2f05 | 2006-11-09 05:12:27 +0000 | [diff] [blame] | 1944 | // X + (signbit) --> X ^ signbit |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 1945 | const APInt& Val = CI->getValue(); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 1946 | uint32_t BitWidth = Val.getBitWidth(); |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 1947 | if (Val == APInt::getSignBit(BitWidth)) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1948 | return BinaryOperator::createXor(LHS, RHS); |
Chris Lattner | b4a2f05 | 2006-11-09 05:12:27 +0000 | [diff] [blame] | 1949 | |
| 1950 | // See if SimplifyDemandedBits can simplify this. This handles stuff like |
| 1951 | // (X & 254)+1 -> (X&254)|1 |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 1952 | if (!isa<VectorType>(I.getType())) { |
| 1953 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 1954 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth), |
| 1955 | KnownZero, KnownOne)) |
| 1956 | return &I; |
| 1957 | } |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 1958 | } |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1959 | |
| 1960 | if (isa<PHINode>(LHS)) |
| 1961 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 1962 | return NV; |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1963 | |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 1964 | ConstantInt *XorRHS = 0; |
| 1965 | Value *XorLHS = 0; |
Chris Lattner | c5eff44 | 2007-01-30 22:32:46 +0000 | [diff] [blame] | 1966 | if (isa<ConstantInt>(RHSC) && |
| 1967 | match(LHS, m_Xor(m_Value(XorLHS), m_ConstantInt(XorRHS)))) { |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 1968 | uint32_t TySizeBits = I.getType()->getPrimitiveSizeInBits(); |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 1969 | const APInt& RHSVal = cast<ConstantInt>(RHSC)->getValue(); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1970 | |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 1971 | uint32_t Size = TySizeBits / 2; |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 1972 | APInt C0080Val(APInt(TySizeBits, 1ULL).shl(Size - 1)); |
| 1973 | APInt CFF80Val(-C0080Val); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1974 | do { |
| 1975 | if (TySizeBits > Size) { |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1976 | // If we have ADD(XOR(AND(X, 0xFF), 0x80), 0xF..F80), it's a sext. |
| 1977 | // 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] | 1978 | if ((RHSVal == CFF80Val && XorRHS->getValue() == C0080Val) || |
| 1979 | (RHSVal == C0080Val && XorRHS->getValue() == CFF80Val)) { |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1980 | // This is a sign extend if the top bits are known zero. |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 1981 | if (!MaskedValueIsZero(XorLHS, |
| 1982 | APInt::getHighBitsSet(TySizeBits, TySizeBits - Size))) |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1983 | 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] | 1984 | break; |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1985 | } |
| 1986 | } |
| 1987 | Size >>= 1; |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 1988 | C0080Val = APIntOps::lshr(C0080Val, Size); |
| 1989 | CFF80Val = APIntOps::ashr(CFF80Val, Size); |
| 1990 | } while (Size >= 1); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1991 | |
Reid Spencer | 35c3885 | 2007-03-28 01:36:16 +0000 | [diff] [blame] | 1992 | // FIXME: This shouldn't be necessary. When the backends can handle types |
| 1993 | // with funny bit widths then this whole cascade of if statements should |
| 1994 | // be removed. It is just here to get the size of the "middle" type back |
| 1995 | // up to something that the back ends can handle. |
| 1996 | const Type *MiddleType = 0; |
| 1997 | switch (Size) { |
| 1998 | default: break; |
| 1999 | case 32: MiddleType = Type::Int32Ty; break; |
| 2000 | case 16: MiddleType = Type::Int16Ty; break; |
| 2001 | case 8: MiddleType = Type::Int8Ty; break; |
| 2002 | } |
| 2003 | if (MiddleType) { |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 2004 | Instruction *NewTrunc = new TruncInst(XorLHS, MiddleType, "sext"); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2005 | InsertNewInstBefore(NewTrunc, I); |
Reid Spencer | 35c3885 | 2007-03-28 01:36:16 +0000 | [diff] [blame] | 2006 | return new SExtInst(NewTrunc, I.getType(), I.getName()); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2007 | } |
| 2008 | } |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 2009 | } |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 2010 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2011 | // X + X --> X << 1 |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 2012 | if (I.getType()->isInteger() && I.getType() != Type::Int1Ty) { |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2013 | if (Instruction *Result = AssociativeOpt(I, AddRHS(RHS))) return Result; |
Chris Lattner | 7edc8c2 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 2014 | |
| 2015 | if (Instruction *RHSI = dyn_cast<Instruction>(RHS)) { |
| 2016 | if (RHSI->getOpcode() == Instruction::Sub) |
| 2017 | if (LHS == RHSI->getOperand(1)) // A + (B - A) --> B |
| 2018 | return ReplaceInstUsesWith(I, RHSI->getOperand(0)); |
| 2019 | } |
| 2020 | if (Instruction *LHSI = dyn_cast<Instruction>(LHS)) { |
| 2021 | if (LHSI->getOpcode() == Instruction::Sub) |
| 2022 | if (RHS == LHSI->getOperand(1)) // (B - A) + A --> B |
| 2023 | return ReplaceInstUsesWith(I, LHSI->getOperand(0)); |
| 2024 | } |
Robert Bocchino | 7169828 | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 2025 | } |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 2026 | |
Chris Lattner | 5c4afb9 | 2002-05-08 22:46:53 +0000 | [diff] [blame] | 2027 | // -A + B --> B - A |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 2028 | if (Value *V = dyn_castNegVal(LHS)) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2029 | return BinaryOperator::createSub(RHS, V); |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 2030 | |
| 2031 | // A + -B --> A - B |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 2032 | if (!isa<Constant>(RHS)) |
| 2033 | if (Value *V = dyn_castNegVal(RHS)) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2034 | return BinaryOperator::createSub(LHS, V); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2035 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2036 | |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2037 | ConstantInt *C2; |
| 2038 | if (Value *X = dyn_castFoldableMul(LHS, C2)) { |
| 2039 | if (X == RHS) // X*C + X --> X * (C+1) |
| 2040 | return BinaryOperator::createMul(RHS, AddOne(C2)); |
| 2041 | |
| 2042 | // X*C1 + X*C2 --> X * (C1+C2) |
| 2043 | ConstantInt *C1; |
| 2044 | if (X == dyn_castFoldableMul(RHS, C1)) |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2045 | return BinaryOperator::createMul(X, Add(C1, C2)); |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2046 | } |
| 2047 | |
| 2048 | // X + X*C --> X * (C+1) |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2049 | if (dyn_castFoldableMul(RHS, C2) == LHS) |
| 2050 | return BinaryOperator::createMul(LHS, AddOne(C2)); |
| 2051 | |
Chris Lattner | e617c9e | 2007-01-05 02:17:46 +0000 | [diff] [blame] | 2052 | // X + ~X --> -1 since ~X = -X-1 |
| 2053 | if (dyn_castNotVal(LHS) == RHS || |
| 2054 | dyn_castNotVal(RHS) == LHS) |
| 2055 | return ReplaceInstUsesWith(I, ConstantInt::getAllOnesValue(I.getType())); |
| 2056 | |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2057 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2058 | // (A & C1)+(B & C2) --> (A & C1)|(B & C2) iff C1&C2 == 0 |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2059 | if (match(RHS, m_And(m_Value(), m_ConstantInt(C2)))) |
Chris Lattner | e617c9e | 2007-01-05 02:17:46 +0000 | [diff] [blame] | 2060 | if (Instruction *R = AssociativeOpt(I, AddMaskingAnd(C2))) |
| 2061 | return R; |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 2062 | |
Chris Lattner | 6b03205 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 2063 | if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) { |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 2064 | Value *X = 0; |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2065 | if (match(LHS, m_Not(m_Value(X)))) // ~X + C --> (C-1) - X |
| 2066 | return BinaryOperator::createSub(SubOne(CRHS), X); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2067 | |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2068 | // (X & FF00) + xx00 -> (X+xx00) & FF00 |
| 2069 | 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] | 2070 | Constant *Anded = And(CRHS, C2); |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2071 | if (Anded == CRHS) { |
| 2072 | // See if all bits from the first bit set in the Add RHS up are included |
| 2073 | // in the mask. First, get the rightmost bit. |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2074 | const APInt& AddRHSV = CRHS->getValue(); |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2075 | |
| 2076 | // 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] | 2077 | APInt AddRHSHighBits(~((AddRHSV & -AddRHSV)-1)); |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2078 | |
| 2079 | // See if the and mask includes all of these bits. |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2080 | APInt AddRHSHighBitsAnd(AddRHSHighBits & C2->getValue()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2081 | |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2082 | if (AddRHSHighBits == AddRHSHighBitsAnd) { |
| 2083 | // Okay, the xform is safe. Insert the new add pronto. |
| 2084 | Value *NewAdd = InsertNewInstBefore(BinaryOperator::createAdd(X, CRHS, |
| 2085 | LHS->getName()), I); |
| 2086 | return BinaryOperator::createAnd(NewAdd, C2); |
| 2087 | } |
| 2088 | } |
| 2089 | } |
| 2090 | |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2091 | // Try to fold constant add into select arguments. |
| 2092 | if (SelectInst *SI = dyn_cast<SelectInst>(LHS)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2093 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2094 | return R; |
Chris Lattner | 6b03205 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 2095 | } |
| 2096 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2097 | // add (cast *A to intptrtype) B -> |
| 2098 | // cast (GEP (cast *A to sbyte*) B) -> |
| 2099 | // intptrtype |
Andrew Lenharth | 16d7955 | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 2100 | { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2101 | CastInst *CI = dyn_cast<CastInst>(LHS); |
| 2102 | Value *Other = RHS; |
Andrew Lenharth | 16d7955 | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 2103 | if (!CI) { |
| 2104 | CI = dyn_cast<CastInst>(RHS); |
| 2105 | Other = LHS; |
| 2106 | } |
Andrew Lenharth | 4563326 | 2006-09-20 15:37:57 +0000 | [diff] [blame] | 2107 | if (CI && CI->getType()->isSized() && |
Reid Spencer | abaa8ca | 2007-01-08 16:32:00 +0000 | [diff] [blame] | 2108 | (CI->getType()->getPrimitiveSizeInBits() == |
| 2109 | TD->getIntPtrType()->getPrimitiveSizeInBits()) |
Andrew Lenharth | 4563326 | 2006-09-20 15:37:57 +0000 | [diff] [blame] | 2110 | && isa<PointerType>(CI->getOperand(0)->getType())) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 2111 | Value *I2 = InsertCastBefore(Instruction::BitCast, CI->getOperand(0), |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 2112 | PointerType::get(Type::Int8Ty), I); |
Andrew Lenharth | 4563326 | 2006-09-20 15:37:57 +0000 | [diff] [blame] | 2113 | I2 = InsertNewInstBefore(new GetElementPtrInst(I2, Other, "ctg2"), I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2114 | return new PtrToIntInst(I2, CI->getType()); |
Andrew Lenharth | 16d7955 | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 2115 | } |
| 2116 | } |
| 2117 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2118 | return Changed ? &I : 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2119 | } |
| 2120 | |
Chris Lattner | 1ba5bcd | 2003-07-22 21:46:59 +0000 | [diff] [blame] | 2121 | // isSignBit - Return true if the value represented by the constant only has the |
| 2122 | // highest order bit set. |
| 2123 | static bool isSignBit(ConstantInt *CI) { |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 2124 | uint32_t NumBits = CI->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 5a1e3e1 | 2007-03-19 20:58:18 +0000 | [diff] [blame] | 2125 | return CI->getValue() == APInt::getSignBit(NumBits); |
Chris Lattner | 1ba5bcd | 2003-07-22 21:46:59 +0000 | [diff] [blame] | 2126 | } |
| 2127 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2128 | Instruction *InstCombiner::visitSub(BinaryOperator &I) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2129 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2130 | |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2131 | if (Op0 == Op1) // sub X, X -> 0 |
| 2132 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2133 | |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2134 | // If this is a 'B = x-(-A)', change to B = x+A... |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 2135 | if (Value *V = dyn_castNegVal(Op1)) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2136 | return BinaryOperator::createAdd(Op0, V); |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 2137 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2138 | if (isa<UndefValue>(Op0)) |
| 2139 | return ReplaceInstUsesWith(I, Op0); // undef - X -> undef |
| 2140 | if (isa<UndefValue>(Op1)) |
| 2141 | return ReplaceInstUsesWith(I, Op1); // X - undef -> undef |
| 2142 | |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 2143 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op0)) { |
| 2144 | // Replace (-1 - A) with (~A)... |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2145 | if (C->isAllOnesValue()) |
| 2146 | return BinaryOperator::createNot(Op1); |
Chris Lattner | 4037171 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 2147 | |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 2148 | // C - ~X == X + (1+C) |
Reid Spencer | 4b828e6 | 2005-06-18 17:37:34 +0000 | [diff] [blame] | 2149 | Value *X = 0; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2150 | if (match(Op1, m_Not(m_Value(X)))) |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2151 | return BinaryOperator::createAdd(X, AddOne(C)); |
| 2152 | |
Chris Lattner | 76b7a06 | 2007-01-15 07:02:54 +0000 | [diff] [blame] | 2153 | // -(X >>u 31) -> (X >>s 31) |
| 2154 | // -(X >>s 31) -> (X >>u 31) |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 2155 | if (C->isZero()) { |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2156 | if (BinaryOperator *SI = dyn_cast<BinaryOperator>(Op1)) |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2157 | if (SI->getOpcode() == Instruction::LShr) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2158 | if (ConstantInt *CU = dyn_cast<ConstantInt>(SI->getOperand(1))) { |
Chris Lattner | 9c29067 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 2159 | // 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] | 2160 | if (CU->getLimitedValue(SI->getType()->getPrimitiveSizeInBits()) == |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2161 | SI->getType()->getPrimitiveSizeInBits()-1) { |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2162 | // Ok, the transformation is safe. Insert AShr. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2163 | return BinaryOperator::create(Instruction::AShr, |
| 2164 | SI->getOperand(0), CU, SI->getName()); |
Chris Lattner | 9c29067 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 2165 | } |
| 2166 | } |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2167 | } |
| 2168 | else if (SI->getOpcode() == Instruction::AShr) { |
| 2169 | if (ConstantInt *CU = dyn_cast<ConstantInt>(SI->getOperand(1))) { |
| 2170 | // 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] | 2171 | if (CU->getLimitedValue(SI->getType()->getPrimitiveSizeInBits()) == |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2172 | SI->getType()->getPrimitiveSizeInBits()-1) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 2173 | // Ok, the transformation is safe. Insert LShr. |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 2174 | return BinaryOperator::createLShr( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2175 | SI->getOperand(0), CU, SI->getName()); |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2176 | } |
| 2177 | } |
| 2178 | } |
Chris Lattner | bfe492b | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 2179 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2180 | |
| 2181 | // Try to fold constant sub into select arguments. |
| 2182 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2183 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2184 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2185 | |
| 2186 | if (isa<PHINode>(Op0)) |
| 2187 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2188 | return NV; |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 2189 | } |
| 2190 | |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2191 | if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) { |
| 2192 | if (Op1I->getOpcode() == Instruction::Add && |
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 2193 | !Op0->getType()->isFPOrFPVector()) { |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2194 | if (Op1I->getOperand(0) == Op0) // X-(X+Y) == -Y |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2195 | return BinaryOperator::createNeg(Op1I->getOperand(1), I.getName()); |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2196 | else if (Op1I->getOperand(1) == Op0) // X-(Y+X) == -Y |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2197 | return BinaryOperator::createNeg(Op1I->getOperand(0), I.getName()); |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2198 | else if (ConstantInt *CI1 = dyn_cast<ConstantInt>(I.getOperand(0))) { |
| 2199 | if (ConstantInt *CI2 = dyn_cast<ConstantInt>(Op1I->getOperand(1))) |
| 2200 | // C1-(X+C2) --> (C1-C2)-X |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2201 | return BinaryOperator::createSub(Subtract(CI1, CI2), |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2202 | Op1I->getOperand(0)); |
| 2203 | } |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2204 | } |
| 2205 | |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 2206 | if (Op1I->hasOneUse()) { |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2207 | // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression |
| 2208 | // is not used by anyone else... |
| 2209 | // |
Chris Lattner | 0517e72 | 2004-02-02 20:09:56 +0000 | [diff] [blame] | 2210 | if (Op1I->getOpcode() == Instruction::Sub && |
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 2211 | !Op1I->getType()->isFPOrFPVector()) { |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2212 | // Swap the two operands of the subexpr... |
| 2213 | Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1); |
| 2214 | Op1I->setOperand(0, IIOp1); |
| 2215 | Op1I->setOperand(1, IIOp0); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2216 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2217 | // Create the new top level add instruction... |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2218 | return BinaryOperator::createAdd(Op0, Op1); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2219 | } |
| 2220 | |
| 2221 | // Replace (A - (A & B)) with (A & ~B) if this is the only use of (A&B)... |
| 2222 | // |
| 2223 | if (Op1I->getOpcode() == Instruction::And && |
| 2224 | (Op1I->getOperand(0) == Op0 || Op1I->getOperand(1) == Op0)) { |
| 2225 | Value *OtherOp = Op1I->getOperand(Op1I->getOperand(0) == Op0); |
| 2226 | |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 2227 | Value *NewNot = |
| 2228 | InsertNewInstBefore(BinaryOperator::createNot(OtherOp, "B.not"), I); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2229 | return BinaryOperator::createAnd(Op0, NewNot); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2230 | } |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2231 | |
Reid Spencer | ac5209e | 2006-10-16 23:08:08 +0000 | [diff] [blame] | 2232 | // 0 - (X sdiv C) -> (X sdiv -C) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2233 | if (Op1I->getOpcode() == Instruction::SDiv) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2234 | if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0)) |
Zhou Sheng | 843f0767 | 2007-04-19 05:39:12 +0000 | [diff] [blame] | 2235 | if (CSI->isZero()) |
Chris Lattner | 91ccc15 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 2236 | if (Constant *DivRHS = dyn_cast<Constant>(Op1I->getOperand(1))) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2237 | return BinaryOperator::createSDiv(Op1I->getOperand(0), |
Chris Lattner | 91ccc15 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 2238 | ConstantExpr::getNeg(DivRHS)); |
| 2239 | |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2240 | // X - X*C --> X * (1-C) |
Reid Spencer | 4b828e6 | 2005-06-18 17:37:34 +0000 | [diff] [blame] | 2241 | ConstantInt *C2 = 0; |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2242 | if (dyn_castFoldableMul(Op1I, C2) == Op0) { |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2243 | Constant *CP1 = Subtract(ConstantInt::get(I.getType(), 1), C2); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2244 | return BinaryOperator::createMul(Op0, CP1); |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2245 | } |
Chris Lattner | 4037171 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 2246 | } |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2247 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2248 | |
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 2249 | if (!Op0->getType()->isFPOrFPVector()) |
Chris Lattner | 7edc8c2 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 2250 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) |
| 2251 | if (Op0I->getOpcode() == Instruction::Add) { |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 2252 | if (Op0I->getOperand(0) == Op1) // (Y+X)-Y == X |
| 2253 | return ReplaceInstUsesWith(I, Op0I->getOperand(1)); |
| 2254 | else if (Op0I->getOperand(1) == Op1) // (X+Y)-Y == X |
| 2255 | return ReplaceInstUsesWith(I, Op0I->getOperand(0)); |
Chris Lattner | 7edc8c2 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 2256 | } else if (Op0I->getOpcode() == Instruction::Sub) { |
| 2257 | if (Op0I->getOperand(0) == Op1) // (X-Y)-X == -Y |
| 2258 | return BinaryOperator::createNeg(Op0I->getOperand(1), I.getName()); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 2259 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2260 | |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2261 | ConstantInt *C1; |
| 2262 | if (Value *X = dyn_castFoldableMul(Op0, C1)) { |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2263 | if (X == Op1) // X*C - X --> X * (C-1) |
| 2264 | return BinaryOperator::createMul(Op1, SubOne(C1)); |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2265 | |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2266 | ConstantInt *C2; // X*C1 - X*C2 -> X * (C1-C2) |
| 2267 | if (X == dyn_castFoldableMul(Op1, C2)) |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2268 | return BinaryOperator::createMul(Op1, Subtract(C1, C2)); |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2269 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2270 | return 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2271 | } |
| 2272 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2273 | /// isSignBitCheck - Given an exploded icmp instruction, return true if it |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2274 | /// really just returns true if the most significant (sign) bit is set. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2275 | static bool isSignBitCheck(ICmpInst::Predicate pred, ConstantInt *RHS) { |
| 2276 | switch (pred) { |
| 2277 | case ICmpInst::ICMP_SLT: |
| 2278 | // True if LHS s< RHS and RHS == 0 |
Zhou Sheng | 843f0767 | 2007-04-19 05:39:12 +0000 | [diff] [blame] | 2279 | return RHS->isZero(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2280 | case ICmpInst::ICMP_SLE: |
| 2281 | // True if LHS s<= RHS and RHS == -1 |
| 2282 | return RHS->isAllOnesValue(); |
| 2283 | case ICmpInst::ICMP_UGE: |
| 2284 | // True if LHS u>= RHS and RHS == high-bit-mask (2^7, 2^15, 2^31, etc) |
Reid Spencer | f244252 | 2007-03-24 00:42:08 +0000 | [diff] [blame] | 2285 | return RHS->getValue() == |
| 2286 | APInt::getSignBit(RHS->getType()->getPrimitiveSizeInBits()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2287 | case ICmpInst::ICMP_UGT: |
| 2288 | // True if LHS u> RHS and RHS == high-bit-mask - 1 |
Reid Spencer | f244252 | 2007-03-24 00:42:08 +0000 | [diff] [blame] | 2289 | return RHS->getValue() == |
| 2290 | APInt::getSignedMaxValue(RHS->getType()->getPrimitiveSizeInBits()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2291 | default: |
| 2292 | return false; |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2293 | } |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2294 | } |
| 2295 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2296 | Instruction *InstCombiner::visitMul(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 2297 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2298 | Value *Op0 = I.getOperand(0); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2299 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2300 | if (isa<UndefValue>(I.getOperand(1))) // undef * X -> 0 |
| 2301 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2302 | |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2303 | // Simplify mul instructions with a constant RHS... |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2304 | if (Constant *Op1 = dyn_cast<Constant>(I.getOperand(1))) { |
| 2305 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 2306 | |
| 2307 | // ((X << C1)*C2) == (X * (C2 << C1)) |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2308 | if (BinaryOperator *SI = dyn_cast<BinaryOperator>(Op0)) |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 2309 | if (SI->getOpcode() == Instruction::Shl) |
| 2310 | if (Constant *ShOp = dyn_cast<Constant>(SI->getOperand(1))) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2311 | return BinaryOperator::createMul(SI->getOperand(0), |
| 2312 | ConstantExpr::getShl(CI, ShOp)); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2313 | |
Zhou Sheng | 843f0767 | 2007-04-19 05:39:12 +0000 | [diff] [blame] | 2314 | if (CI->isZero()) |
Chris Lattner | 515c97c | 2003-09-11 22:24:54 +0000 | [diff] [blame] | 2315 | return ReplaceInstUsesWith(I, Op1); // X * 0 == 0 |
| 2316 | if (CI->equalsInt(1)) // X * 1 == X |
| 2317 | return ReplaceInstUsesWith(I, Op0); |
| 2318 | if (CI->isAllOnesValue()) // X * -1 == 0 - X |
Chris Lattner | 0af1fab | 2003-06-25 17:09:20 +0000 | [diff] [blame] | 2319 | return BinaryOperator::createNeg(Op0, I.getName()); |
Chris Lattner | 6c1ce21 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 2320 | |
Zhou Sheng | 97b52c2 | 2007-03-29 01:57:21 +0000 | [diff] [blame] | 2321 | const APInt& Val = cast<ConstantInt>(CI)->getValue(); |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2322 | if (Val.isPowerOf2()) { // Replace X*(2^C) with X << C |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 2323 | return BinaryOperator::createShl(Op0, |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2324 | ConstantInt::get(Op0->getType(), Val.logBase2())); |
Chris Lattner | bcd7db5 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 2325 | } |
Robert Bocchino | 7169828 | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 2326 | } else if (ConstantFP *Op1F = dyn_cast<ConstantFP>(Op1)) { |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2327 | if (Op1F->isNullValue()) |
| 2328 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 6c1ce21 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 2329 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2330 | // "In IEEE floating point, x*1 is not equivalent to x for nans. However, |
| 2331 | // ANSI says we can drop signals, so we can do this anyway." (from GCC) |
| 2332 | if (Op1F->getValue() == 1.0) |
| 2333 | return ReplaceInstUsesWith(I, Op0); // Eliminate 'mul double %X, 1.0' |
| 2334 | } |
Chris Lattner | ab51f3f | 2006-03-04 06:04:02 +0000 | [diff] [blame] | 2335 | |
| 2336 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) |
| 2337 | if (Op0I->getOpcode() == Instruction::Add && Op0I->hasOneUse() && |
| 2338 | isa<ConstantInt>(Op0I->getOperand(1))) { |
| 2339 | // Canonicalize (X+C1)*C2 -> X*C2+C1*C2. |
| 2340 | Instruction *Add = BinaryOperator::createMul(Op0I->getOperand(0), |
| 2341 | Op1, "tmp"); |
| 2342 | InsertNewInstBefore(Add, I); |
| 2343 | Value *C1C2 = ConstantExpr::getMul(Op1, |
| 2344 | cast<Constant>(Op0I->getOperand(1))); |
| 2345 | return BinaryOperator::createAdd(Add, C1C2); |
| 2346 | |
| 2347 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2348 | |
| 2349 | // Try to fold constant mul into select arguments. |
| 2350 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2351 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2352 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2353 | |
| 2354 | if (isa<PHINode>(Op0)) |
| 2355 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2356 | return NV; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2357 | } |
| 2358 | |
Chris Lattner | a4f445b | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 2359 | if (Value *Op0v = dyn_castNegVal(Op0)) // -X * -Y = X*Y |
| 2360 | if (Value *Op1v = dyn_castNegVal(I.getOperand(1))) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2361 | return BinaryOperator::createMul(Op0v, Op1v); |
Chris Lattner | a4f445b | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 2362 | |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2363 | // If one of the operands of the multiply is a cast from a boolean value, then |
| 2364 | // we know the bool is either zero or one, so this is a 'masking' multiply. |
| 2365 | // See if we can simplify things based on how the boolean was originally |
| 2366 | // formed. |
| 2367 | CastInst *BoolCast = 0; |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 2368 | if (ZExtInst *CI = dyn_cast<ZExtInst>(I.getOperand(0))) |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 2369 | if (CI->getOperand(0)->getType() == Type::Int1Ty) |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2370 | BoolCast = CI; |
| 2371 | if (!BoolCast) |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 2372 | if (ZExtInst *CI = dyn_cast<ZExtInst>(I.getOperand(1))) |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 2373 | if (CI->getOperand(0)->getType() == Type::Int1Ty) |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2374 | BoolCast = CI; |
| 2375 | if (BoolCast) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2376 | if (ICmpInst *SCI = dyn_cast<ICmpInst>(BoolCast->getOperand(0))) { |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2377 | Value *SCIOp0 = SCI->getOperand(0), *SCIOp1 = SCI->getOperand(1); |
| 2378 | const Type *SCOpTy = SCIOp0->getType(); |
| 2379 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2380 | // 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] | 2381 | // multiply into a shift/and combination. |
| 2382 | if (isa<ConstantInt>(SCIOp1) && |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2383 | isSignBitCheck(SCI->getPredicate(), cast<ConstantInt>(SCIOp1))) { |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2384 | // Shift the X value right to turn it into "all signbits". |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2385 | Constant *Amt = ConstantInt::get(SCIOp0->getType(), |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 2386 | SCOpTy->getPrimitiveSizeInBits()-1); |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2387 | Value *V = |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2388 | InsertNewInstBefore( |
| 2389 | BinaryOperator::create(Instruction::AShr, SCIOp0, Amt, |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2390 | BoolCast->getOperand(0)->getName()+ |
| 2391 | ".mask"), I); |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2392 | |
| 2393 | // If the multiply type is not the same as the source type, sign extend |
| 2394 | // or truncate to the multiply type. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 2395 | if (I.getType() != V->getType()) { |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 2396 | uint32_t SrcBits = V->getType()->getPrimitiveSizeInBits(); |
| 2397 | uint32_t DstBits = I.getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 2398 | Instruction::CastOps opcode = |
| 2399 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2400 | (SrcBits < DstBits ? Instruction::SExt : Instruction::Trunc)); |
| 2401 | V = InsertCastBefore(opcode, V, I.getType(), I); |
| 2402 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2403 | |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2404 | Value *OtherOp = Op0 == BoolCast ? I.getOperand(1) : Op0; |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2405 | return BinaryOperator::createAnd(V, OtherOp); |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2406 | } |
| 2407 | } |
| 2408 | } |
| 2409 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2410 | return Changed ? &I : 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2411 | } |
| 2412 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2413 | /// This function implements the transforms on div instructions that work |
| 2414 | /// regardless of the kind of div instruction it is (udiv, sdiv, or fdiv). It is |
| 2415 | /// used by the visitors to those instructions. |
| 2416 | /// @brief Transforms common to all three div instructions |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2417 | Instruction *InstCombiner::commonDivTransforms(BinaryOperator &I) { |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2418 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2419 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2420 | // undef / X -> 0 |
| 2421 | if (isa<UndefValue>(Op0)) |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2422 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2423 | |
| 2424 | // X / undef -> undef |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2425 | if (isa<UndefValue>(Op1)) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2426 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2427 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2428 | // Handle cases involving: div X, (select Cond, Y, Z) |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2429 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { |
| 2430 | // div X, (Cond ? 0 : Y) -> div X, Y. If the div and the select are in the |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2431 | // same basic block, then we replace the select with Y, and the condition |
| 2432 | // of the select with false (if the cond value is in the same BB). If the |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2433 | // select has uses other than the div, this allows them to be simplified |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2434 | // also. Note that div X, Y is just as good as div X, 0 (undef) |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2435 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(1))) |
| 2436 | if (ST->isNullValue()) { |
| 2437 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); |
| 2438 | if (CondI && CondI->getParent() == I.getParent()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2439 | UpdateValueUsesWith(CondI, ConstantInt::getFalse()); |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2440 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
| 2441 | I.setOperand(1, SI->getOperand(2)); |
| 2442 | else |
| 2443 | UpdateValueUsesWith(SI, SI->getOperand(2)); |
| 2444 | return &I; |
| 2445 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2446 | |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2447 | // Likewise for: div X, (Cond ? Y : 0) -> div X, Y |
| 2448 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(2))) |
| 2449 | if (ST->isNullValue()) { |
| 2450 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); |
| 2451 | if (CondI && CondI->getParent() == I.getParent()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2452 | UpdateValueUsesWith(CondI, ConstantInt::getTrue()); |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2453 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
| 2454 | I.setOperand(1, SI->getOperand(1)); |
| 2455 | else |
| 2456 | UpdateValueUsesWith(SI, SI->getOperand(1)); |
| 2457 | return &I; |
| 2458 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2459 | } |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2460 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2461 | return 0; |
| 2462 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2463 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2464 | /// This function implements the transforms common to both integer division |
| 2465 | /// instructions (udiv and sdiv). It is called by the visitors to those integer |
| 2466 | /// division instructions. |
| 2467 | /// @brief Common integer divide transforms |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2468 | Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2469 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2470 | |
| 2471 | if (Instruction *Common = commonDivTransforms(I)) |
| 2472 | return Common; |
| 2473 | |
| 2474 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
| 2475 | // div X, 1 == X |
| 2476 | if (RHS->equalsInt(1)) |
| 2477 | return ReplaceInstUsesWith(I, Op0); |
| 2478 | |
| 2479 | // (X / C1) / C2 -> X / (C1*C2) |
| 2480 | if (Instruction *LHS = dyn_cast<Instruction>(Op0)) |
| 2481 | if (Instruction::BinaryOps(LHS->getOpcode()) == I.getOpcode()) |
| 2482 | if (ConstantInt *LHSRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) { |
| 2483 | return BinaryOperator::create(I.getOpcode(), LHS->getOperand(0), |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2484 | Multiply(RHS, LHSRHS)); |
Chris Lattner | bf70b83 | 2005-04-08 04:03:26 +0000 | [diff] [blame] | 2485 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2486 | |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2487 | if (!RHS->isZero()) { // avoid X udiv 0 |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2488 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 2489 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 2490 | return R; |
| 2491 | if (isa<PHINode>(Op0)) |
| 2492 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2493 | return NV; |
| 2494 | } |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2495 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2496 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2497 | // 0 / X == 0, we don't need to preserve faults! |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2498 | if (ConstantInt *LHS = dyn_cast<ConstantInt>(Op0)) |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2499 | if (LHS->equalsInt(0)) |
| 2500 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2501 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2502 | return 0; |
| 2503 | } |
| 2504 | |
| 2505 | Instruction *InstCombiner::visitUDiv(BinaryOperator &I) { |
| 2506 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2507 | |
| 2508 | // Handle the integer div common cases |
| 2509 | if (Instruction *Common = commonIDivTransforms(I)) |
| 2510 | return Common; |
| 2511 | |
| 2512 | // X udiv C^2 -> X >> C |
| 2513 | // Check to see if this is an unsigned division with an exact power of 2, |
| 2514 | // if so, convert to a right shift. |
| 2515 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op1)) { |
Reid Spencer | 6eb0d99 | 2007-03-26 23:58:26 +0000 | [diff] [blame] | 2516 | if (C->getValue().isPowerOf2()) // 0 not included in isPowerOf2 |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2517 | return BinaryOperator::createLShr(Op0, |
Zhou Sheng | 0fc5095 | 2007-03-25 05:01:29 +0000 | [diff] [blame] | 2518 | ConstantInt::get(Op0->getType(), C->getValue().logBase2())); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2519 | } |
| 2520 | |
| 2521 | // X udiv (C1 << N), where C1 is "1<<C2" --> X >> (N+C2) |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2522 | if (BinaryOperator *RHSI = dyn_cast<BinaryOperator>(I.getOperand(1))) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2523 | if (RHSI->getOpcode() == Instruction::Shl && |
| 2524 | isa<ConstantInt>(RHSI->getOperand(0))) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2525 | const APInt& C1 = cast<ConstantInt>(RHSI->getOperand(0))->getValue(); |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2526 | if (C1.isPowerOf2()) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2527 | Value *N = RHSI->getOperand(1); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2528 | const Type *NTy = N->getType(); |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 2529 | if (uint32_t C2 = C1.logBase2()) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2530 | Constant *C2V = ConstantInt::get(NTy, C2); |
| 2531 | N = InsertNewInstBefore(BinaryOperator::createAdd(N, C2V, "tmp"), I); |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2532 | } |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 2533 | return BinaryOperator::createLShr(Op0, N); |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2534 | } |
| 2535 | } |
Chris Lattner | c812e5d | 2005-11-05 07:40:31 +0000 | [diff] [blame] | 2536 | } |
| 2537 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2538 | // udiv X, (Select Cond, C1, C2) --> Select Cond, (shr X, C1), (shr X, C2) |
| 2539 | // where C1&C2 are powers of two. |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2540 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2541 | if (ConstantInt *STO = dyn_cast<ConstantInt>(SI->getOperand(1))) |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2542 | if (ConstantInt *SFO = dyn_cast<ConstantInt>(SI->getOperand(2))) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2543 | const APInt &TVA = STO->getValue(), &FVA = SFO->getValue(); |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2544 | if (TVA.isPowerOf2() && FVA.isPowerOf2()) { |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2545 | // Compute the shift amounts |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2546 | uint32_t TSA = TVA.logBase2(), FSA = FVA.logBase2(); |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2547 | // Construct the "on true" case of the select |
| 2548 | Constant *TC = ConstantInt::get(Op0->getType(), TSA); |
| 2549 | Instruction *TSI = BinaryOperator::createLShr( |
| 2550 | Op0, TC, SI->getName()+".t"); |
| 2551 | TSI = InsertNewInstBefore(TSI, I); |
| 2552 | |
| 2553 | // Construct the "on false" case of the select |
| 2554 | Constant *FC = ConstantInt::get(Op0->getType(), FSA); |
| 2555 | Instruction *FSI = BinaryOperator::createLShr( |
| 2556 | Op0, FC, SI->getName()+".f"); |
| 2557 | FSI = InsertNewInstBefore(FSI, I); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2558 | |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2559 | // construct the select instruction and return it. |
| 2560 | return new SelectInst(SI->getOperand(0), TSI, FSI, SI->getName()); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2561 | } |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2562 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2563 | return 0; |
| 2564 | } |
| 2565 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2566 | Instruction *InstCombiner::visitSDiv(BinaryOperator &I) { |
| 2567 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2568 | |
| 2569 | // Handle the integer div common cases |
| 2570 | if (Instruction *Common = commonIDivTransforms(I)) |
| 2571 | return Common; |
| 2572 | |
| 2573 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
| 2574 | // sdiv X, -1 == -X |
| 2575 | if (RHS->isAllOnesValue()) |
| 2576 | return BinaryOperator::createNeg(Op0); |
| 2577 | |
| 2578 | // -X/C -> X/-C |
| 2579 | if (Value *LHSNeg = dyn_castNegVal(Op0)) |
| 2580 | return BinaryOperator::createSDiv(LHSNeg, ConstantExpr::getNeg(RHS)); |
| 2581 | } |
| 2582 | |
| 2583 | // If the sign bits of both operands are zero (i.e. we can prove they are |
| 2584 | // unsigned inputs), turn this into a udiv. |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 2585 | if (I.getType()->isInteger()) { |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2586 | APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits())); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2587 | if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) { |
| 2588 | return BinaryOperator::createUDiv(Op0, Op1, I.getName()); |
| 2589 | } |
| 2590 | } |
| 2591 | |
| 2592 | return 0; |
| 2593 | } |
| 2594 | |
| 2595 | Instruction *InstCombiner::visitFDiv(BinaryOperator &I) { |
| 2596 | return commonDivTransforms(I); |
| 2597 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2598 | |
Chris Lattner | db3f873 | 2006-03-02 06:50:58 +0000 | [diff] [blame] | 2599 | /// GetFactor - If we can prove that the specified value is at least a multiple |
| 2600 | /// of some factor, return that factor. |
| 2601 | static Constant *GetFactor(Value *V) { |
| 2602 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) |
| 2603 | return CI; |
| 2604 | |
| 2605 | // Unless we can be tricky, we know this is a multiple of 1. |
| 2606 | Constant *Result = ConstantInt::get(V->getType(), 1); |
| 2607 | |
| 2608 | Instruction *I = dyn_cast<Instruction>(V); |
| 2609 | if (!I) return Result; |
| 2610 | |
| 2611 | if (I->getOpcode() == Instruction::Mul) { |
| 2612 | // Handle multiplies by a constant, etc. |
| 2613 | return ConstantExpr::getMul(GetFactor(I->getOperand(0)), |
| 2614 | GetFactor(I->getOperand(1))); |
| 2615 | } else if (I->getOpcode() == Instruction::Shl) { |
| 2616 | // (X<<C) -> X * (1 << C) |
| 2617 | if (Constant *ShRHS = dyn_cast<Constant>(I->getOperand(1))) { |
| 2618 | ShRHS = ConstantExpr::getShl(Result, ShRHS); |
| 2619 | return ConstantExpr::getMul(GetFactor(I->getOperand(0)), ShRHS); |
| 2620 | } |
| 2621 | } else if (I->getOpcode() == Instruction::And) { |
| 2622 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 2623 | // X & 0xFFF0 is known to be a multiple of 16. |
Reid Spencer | f244252 | 2007-03-24 00:42:08 +0000 | [diff] [blame] | 2624 | uint32_t Zeros = RHS->getValue().countTrailingZeros(); |
Chris Lattner | db3f873 | 2006-03-02 06:50:58 +0000 | [diff] [blame] | 2625 | if (Zeros != V->getType()->getPrimitiveSizeInBits()) |
| 2626 | return ConstantExpr::getShl(Result, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2627 | ConstantInt::get(Result->getType(), Zeros)); |
Chris Lattner | db3f873 | 2006-03-02 06:50:58 +0000 | [diff] [blame] | 2628 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2629 | } else if (CastInst *CI = dyn_cast<CastInst>(I)) { |
Chris Lattner | db3f873 | 2006-03-02 06:50:58 +0000 | [diff] [blame] | 2630 | // Only handle int->int casts. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2631 | if (!CI->isIntegerCast()) |
| 2632 | return Result; |
| 2633 | Value *Op = CI->getOperand(0); |
| 2634 | return ConstantExpr::getCast(CI->getOpcode(), GetFactor(Op), V->getType()); |
Chris Lattner | db3f873 | 2006-03-02 06:50:58 +0000 | [diff] [blame] | 2635 | } |
| 2636 | return Result; |
| 2637 | } |
| 2638 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2639 | /// This function implements the transforms on rem instructions that work |
| 2640 | /// regardless of the kind of rem instruction it is (urem, srem, or frem). It |
| 2641 | /// is used by the visitors to those instructions. |
| 2642 | /// @brief Transforms common to all three rem instructions |
| 2643 | Instruction *InstCombiner::commonRemTransforms(BinaryOperator &I) { |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2644 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2645 | |
Chris Lattner | 19ccd5c | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 2646 | // 0 % X == 0, we don't need to preserve faults! |
| 2647 | if (Constant *LHS = dyn_cast<Constant>(Op0)) |
| 2648 | if (LHS->isNullValue()) |
| 2649 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2650 | |
| 2651 | if (isa<UndefValue>(Op0)) // undef % X -> 0 |
| 2652 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2653 | if (isa<UndefValue>(Op1)) |
| 2654 | return ReplaceInstUsesWith(I, Op1); // X % undef -> undef |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2655 | |
| 2656 | // Handle cases involving: rem X, (select Cond, Y, Z) |
| 2657 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { |
| 2658 | // rem X, (Cond ? 0 : Y) -> rem X, Y. If the rem and the select are in |
| 2659 | // the same basic block, then we replace the select with Y, and the |
| 2660 | // condition of the select with false (if the cond value is in the same |
| 2661 | // BB). If the select has uses other than the div, this allows them to be |
| 2662 | // simplified also. |
| 2663 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(1))) |
| 2664 | if (ST->isNullValue()) { |
| 2665 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); |
| 2666 | if (CondI && CondI->getParent() == I.getParent()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2667 | UpdateValueUsesWith(CondI, ConstantInt::getFalse()); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2668 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
| 2669 | I.setOperand(1, SI->getOperand(2)); |
| 2670 | else |
| 2671 | UpdateValueUsesWith(SI, SI->getOperand(2)); |
Chris Lattner | 5b73c08 | 2004-07-06 07:01:22 +0000 | [diff] [blame] | 2672 | return &I; |
| 2673 | } |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2674 | // Likewise for: rem X, (Cond ? Y : 0) -> rem X, Y |
| 2675 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(2))) |
| 2676 | if (ST->isNullValue()) { |
| 2677 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); |
| 2678 | if (CondI && CondI->getParent() == I.getParent()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2679 | UpdateValueUsesWith(CondI, ConstantInt::getTrue()); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2680 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
| 2681 | I.setOperand(1, SI->getOperand(1)); |
| 2682 | else |
| 2683 | UpdateValueUsesWith(SI, SI->getOperand(1)); |
| 2684 | return &I; |
| 2685 | } |
Chris Lattner | 11a49f2 | 2005-11-05 07:28:37 +0000 | [diff] [blame] | 2686 | } |
Chris Lattner | 5b73c08 | 2004-07-06 07:01:22 +0000 | [diff] [blame] | 2687 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2688 | return 0; |
| 2689 | } |
| 2690 | |
| 2691 | /// This function implements the transforms common to both integer remainder |
| 2692 | /// instructions (urem and srem). It is called by the visitors to those integer |
| 2693 | /// remainder instructions. |
| 2694 | /// @brief Common integer remainder transforms |
| 2695 | Instruction *InstCombiner::commonIRemTransforms(BinaryOperator &I) { |
| 2696 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2697 | |
| 2698 | if (Instruction *common = commonRemTransforms(I)) |
| 2699 | return common; |
| 2700 | |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2701 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 19ccd5c | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 2702 | // X % 0 == undef, we don't need to preserve faults! |
| 2703 | if (RHS->equalsInt(0)) |
| 2704 | return ReplaceInstUsesWith(I, UndefValue::get(I.getType())); |
| 2705 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2706 | if (RHS->equalsInt(1)) // X % 1 == 0 |
| 2707 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2708 | |
Chris Lattner | 9794392 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 2709 | if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) { |
| 2710 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0I)) { |
| 2711 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 2712 | return R; |
| 2713 | } else if (isa<PHINode>(Op0I)) { |
| 2714 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2715 | return NV; |
Chris Lattner | 9794392 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 2716 | } |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2717 | // (X * C1) % C2 --> 0 iff C1 % C2 == 0 |
| 2718 | if (ConstantExpr::getSRem(GetFactor(Op0I), RHS)->isNullValue()) |
Chris Lattner | db3f873 | 2006-03-02 06:50:58 +0000 | [diff] [blame] | 2719 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | 9794392 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 2720 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2721 | } |
| 2722 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2723 | return 0; |
| 2724 | } |
| 2725 | |
| 2726 | Instruction *InstCombiner::visitURem(BinaryOperator &I) { |
| 2727 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2728 | |
| 2729 | if (Instruction *common = commonIRemTransforms(I)) |
| 2730 | return common; |
| 2731 | |
| 2732 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
| 2733 | // X urem C^2 -> X and C |
| 2734 | // Check to see if this is an unsigned remainder with an exact power of 2, |
| 2735 | // if so, convert to a bitwise and. |
| 2736 | if (ConstantInt *C = dyn_cast<ConstantInt>(RHS)) |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2737 | if (C->getValue().isPowerOf2()) |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2738 | return BinaryOperator::createAnd(Op0, SubOne(C)); |
| 2739 | } |
| 2740 | |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2741 | if (Instruction *RHSI = dyn_cast<Instruction>(I.getOperand(1))) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2742 | // Turn A % (C << N), where C is 2^k, into A & ((C << N)-1) |
| 2743 | if (RHSI->getOpcode() == Instruction::Shl && |
| 2744 | isa<ConstantInt>(RHSI->getOperand(0))) { |
Zhou Sheng | 0fc5095 | 2007-03-25 05:01:29 +0000 | [diff] [blame] | 2745 | if (cast<ConstantInt>(RHSI->getOperand(0))->getValue().isPowerOf2()) { |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2746 | Constant *N1 = ConstantInt::getAllOnesValue(I.getType()); |
| 2747 | Value *Add = InsertNewInstBefore(BinaryOperator::createAdd(RHSI, N1, |
| 2748 | "tmp"), I); |
| 2749 | return BinaryOperator::createAnd(Op0, Add); |
| 2750 | } |
| 2751 | } |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2752 | } |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2753 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2754 | // urem X, (select Cond, 2^C1, 2^C2) --> select Cond, (and X, C1), (and X, C2) |
| 2755 | // where C1&C2 are powers of two. |
| 2756 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { |
| 2757 | if (ConstantInt *STO = dyn_cast<ConstantInt>(SI->getOperand(1))) |
| 2758 | if (ConstantInt *SFO = dyn_cast<ConstantInt>(SI->getOperand(2))) { |
| 2759 | // STO == 0 and SFO == 0 handled above. |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2760 | if ((STO->getValue().isPowerOf2()) && |
| 2761 | (SFO->getValue().isPowerOf2())) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2762 | Value *TrueAnd = InsertNewInstBefore( |
| 2763 | BinaryOperator::createAnd(Op0, SubOne(STO), SI->getName()+".t"), I); |
| 2764 | Value *FalseAnd = InsertNewInstBefore( |
| 2765 | BinaryOperator::createAnd(Op0, SubOne(SFO), SI->getName()+".f"), I); |
| 2766 | return new SelectInst(SI->getOperand(0), TrueAnd, FalseAnd); |
| 2767 | } |
| 2768 | } |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2769 | } |
| 2770 | |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2771 | return 0; |
| 2772 | } |
| 2773 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2774 | Instruction *InstCombiner::visitSRem(BinaryOperator &I) { |
| 2775 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2776 | |
| 2777 | if (Instruction *common = commonIRemTransforms(I)) |
| 2778 | return common; |
| 2779 | |
| 2780 | if (Value *RHSNeg = dyn_castNegVal(Op1)) |
| 2781 | if (!isa<ConstantInt>(RHSNeg) || |
Zhou Sheng | 0fc5095 | 2007-03-25 05:01:29 +0000 | [diff] [blame] | 2782 | cast<ConstantInt>(RHSNeg)->getValue().isStrictlyPositive()) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2783 | // X % -Y -> X % Y |
| 2784 | AddUsesToWorkList(I); |
| 2785 | I.setOperand(1, RHSNeg); |
| 2786 | return &I; |
| 2787 | } |
| 2788 | |
| 2789 | // If the top bits of both operands are zero (i.e. we can prove they are |
| 2790 | // unsigned inputs), turn this into a urem. |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2791 | APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits())); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2792 | if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) { |
| 2793 | // X srem Y -> X urem Y, iff X and Y don't have sign bit set |
| 2794 | return BinaryOperator::createURem(Op0, Op1, I.getName()); |
| 2795 | } |
| 2796 | |
| 2797 | return 0; |
| 2798 | } |
| 2799 | |
| 2800 | Instruction *InstCombiner::visitFRem(BinaryOperator &I) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2801 | return commonRemTransforms(I); |
| 2802 | } |
| 2803 | |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2804 | // isMaxValueMinusOne - return true if this is Max-1 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2805 | static bool isMaxValueMinusOne(const ConstantInt *C, bool isSigned) { |
Reid Spencer | 3a2a9fb | 2007-03-19 21:10:28 +0000 | [diff] [blame] | 2806 | uint32_t TypeBits = C->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2807 | if (isSigned) { |
| 2808 | // Calculate 0111111111..11111 |
Reid Spencer | 3a2a9fb | 2007-03-19 21:10:28 +0000 | [diff] [blame] | 2809 | APInt Val(APInt::getSignedMaxValue(TypeBits)); |
| 2810 | return C->getValue() == Val-1; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2811 | } |
Reid Spencer | 3a2a9fb | 2007-03-19 21:10:28 +0000 | [diff] [blame] | 2812 | return C->getValue() == APInt::getAllOnesValue(TypeBits) - 1; |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2813 | } |
| 2814 | |
| 2815 | // isMinValuePlusOne - return true if this is Min+1 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2816 | static bool isMinValuePlusOne(const ConstantInt *C, bool isSigned) { |
| 2817 | if (isSigned) { |
| 2818 | // Calculate 1111111111000000000000 |
Reid Spencer | 727992c | 2007-03-19 21:08:07 +0000 | [diff] [blame] | 2819 | uint32_t TypeBits = C->getType()->getPrimitiveSizeInBits(); |
| 2820 | APInt Val(APInt::getSignedMinValue(TypeBits)); |
| 2821 | return C->getValue() == Val+1; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2822 | } |
Reid Spencer | 727992c | 2007-03-19 21:08:07 +0000 | [diff] [blame] | 2823 | return C->getValue() == 1; // unsigned |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2824 | } |
| 2825 | |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2826 | // isOneBitSet - Return true if there is exactly one bit set in the specified |
| 2827 | // constant. |
| 2828 | static bool isOneBitSet(const ConstantInt *CI) { |
Reid Spencer | 5f6a895 | 2007-03-20 00:16:52 +0000 | [diff] [blame] | 2829 | return CI->getValue().isPowerOf2(); |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2830 | } |
| 2831 | |
Chris Lattner | b20ba0a | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2832 | // isHighOnes - Return true if the constant is of the form 1+0+. |
| 2833 | // This is the same as lowones(~X). |
| 2834 | static bool isHighOnes(const ConstantInt *CI) { |
Zhou Sheng | 2cde46c | 2007-03-20 12:49:06 +0000 | [diff] [blame] | 2835 | return (~CI->getValue() + 1).isPowerOf2(); |
Chris Lattner | b20ba0a | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2836 | } |
| 2837 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2838 | /// getICmpCode - Encode a icmp predicate into a three bit mask. These bits |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2839 | /// are carefully arranged to allow folding of expressions such as: |
| 2840 | /// |
| 2841 | /// (A < B) | (A > B) --> (A != B) |
| 2842 | /// |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2843 | /// Note that this is only valid if the first and second predicates have the |
| 2844 | /// same sign. Is illegal to do: (A u< B) | (A s> B) |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2845 | /// |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2846 | /// Three bits are used to represent the condition, as follows: |
| 2847 | /// 0 A > B |
| 2848 | /// 1 A == B |
| 2849 | /// 2 A < B |
| 2850 | /// |
| 2851 | /// <=> Value Definition |
| 2852 | /// 000 0 Always false |
| 2853 | /// 001 1 A > B |
| 2854 | /// 010 2 A == B |
| 2855 | /// 011 3 A >= B |
| 2856 | /// 100 4 A < B |
| 2857 | /// 101 5 A != B |
| 2858 | /// 110 6 A <= B |
| 2859 | /// 111 7 Always true |
| 2860 | /// |
| 2861 | static unsigned getICmpCode(const ICmpInst *ICI) { |
| 2862 | switch (ICI->getPredicate()) { |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2863 | // False -> 0 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2864 | case ICmpInst::ICMP_UGT: return 1; // 001 |
| 2865 | case ICmpInst::ICMP_SGT: return 1; // 001 |
| 2866 | case ICmpInst::ICMP_EQ: return 2; // 010 |
| 2867 | case ICmpInst::ICMP_UGE: return 3; // 011 |
| 2868 | case ICmpInst::ICMP_SGE: return 3; // 011 |
| 2869 | case ICmpInst::ICMP_ULT: return 4; // 100 |
| 2870 | case ICmpInst::ICMP_SLT: return 4; // 100 |
| 2871 | case ICmpInst::ICMP_NE: return 5; // 101 |
| 2872 | case ICmpInst::ICMP_ULE: return 6; // 110 |
| 2873 | case ICmpInst::ICMP_SLE: return 6; // 110 |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2874 | // True -> 7 |
| 2875 | default: |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2876 | assert(0 && "Invalid ICmp predicate!"); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2877 | return 0; |
| 2878 | } |
| 2879 | } |
| 2880 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2881 | /// getICmpValue - This is the complement of getICmpCode, which turns an |
| 2882 | /// opcode and two operands into either a constant true or false, or a brand |
| 2883 | /// new /// ICmp instruction. The sign is passed in to determine which kind |
| 2884 | /// of predicate to use in new icmp instructions. |
| 2885 | static Value *getICmpValue(bool sign, unsigned code, Value *LHS, Value *RHS) { |
| 2886 | switch (code) { |
| 2887 | default: assert(0 && "Illegal ICmp code!"); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2888 | case 0: return ConstantInt::getFalse(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2889 | case 1: |
| 2890 | if (sign) |
| 2891 | return new ICmpInst(ICmpInst::ICMP_SGT, LHS, RHS); |
| 2892 | else |
| 2893 | return new ICmpInst(ICmpInst::ICMP_UGT, LHS, RHS); |
| 2894 | case 2: return new ICmpInst(ICmpInst::ICMP_EQ, LHS, RHS); |
| 2895 | case 3: |
| 2896 | if (sign) |
| 2897 | return new ICmpInst(ICmpInst::ICMP_SGE, LHS, RHS); |
| 2898 | else |
| 2899 | return new ICmpInst(ICmpInst::ICMP_UGE, LHS, RHS); |
| 2900 | case 4: |
| 2901 | if (sign) |
| 2902 | return new ICmpInst(ICmpInst::ICMP_SLT, LHS, RHS); |
| 2903 | else |
| 2904 | return new ICmpInst(ICmpInst::ICMP_ULT, LHS, RHS); |
| 2905 | case 5: return new ICmpInst(ICmpInst::ICMP_NE, LHS, RHS); |
| 2906 | case 6: |
| 2907 | if (sign) |
| 2908 | return new ICmpInst(ICmpInst::ICMP_SLE, LHS, RHS); |
| 2909 | else |
| 2910 | return new ICmpInst(ICmpInst::ICMP_ULE, LHS, RHS); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2911 | case 7: return ConstantInt::getTrue(); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2912 | } |
| 2913 | } |
| 2914 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2915 | static bool PredicatesFoldable(ICmpInst::Predicate p1, ICmpInst::Predicate p2) { |
| 2916 | return (ICmpInst::isSignedPredicate(p1) == ICmpInst::isSignedPredicate(p2)) || |
| 2917 | (ICmpInst::isSignedPredicate(p1) && |
| 2918 | (p2 == ICmpInst::ICMP_EQ || p2 == ICmpInst::ICMP_NE)) || |
| 2919 | (ICmpInst::isSignedPredicate(p2) && |
| 2920 | (p1 == ICmpInst::ICMP_EQ || p1 == ICmpInst::ICMP_NE)); |
| 2921 | } |
| 2922 | |
| 2923 | namespace { |
| 2924 | // FoldICmpLogical - Implements (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B) |
| 2925 | struct FoldICmpLogical { |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2926 | InstCombiner &IC; |
| 2927 | Value *LHS, *RHS; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2928 | ICmpInst::Predicate pred; |
| 2929 | FoldICmpLogical(InstCombiner &ic, ICmpInst *ICI) |
| 2930 | : IC(ic), LHS(ICI->getOperand(0)), RHS(ICI->getOperand(1)), |
| 2931 | pred(ICI->getPredicate()) {} |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2932 | bool shouldApply(Value *V) const { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2933 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(V)) |
| 2934 | if (PredicatesFoldable(pred, ICI->getPredicate())) |
| 2935 | return (ICI->getOperand(0) == LHS && ICI->getOperand(1) == RHS || |
| 2936 | ICI->getOperand(0) == RHS && ICI->getOperand(1) == LHS); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2937 | return false; |
| 2938 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2939 | Instruction *apply(Instruction &Log) const { |
| 2940 | ICmpInst *ICI = cast<ICmpInst>(Log.getOperand(0)); |
| 2941 | if (ICI->getOperand(0) != LHS) { |
| 2942 | assert(ICI->getOperand(1) == LHS); |
| 2943 | ICI->swapOperands(); // Swap the LHS and RHS of the ICmp |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2944 | } |
| 2945 | |
Chris Lattner | bc1dbfc | 2007-03-13 14:27:42 +0000 | [diff] [blame] | 2946 | ICmpInst *RHSICI = cast<ICmpInst>(Log.getOperand(1)); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2947 | unsigned LHSCode = getICmpCode(ICI); |
Chris Lattner | bc1dbfc | 2007-03-13 14:27:42 +0000 | [diff] [blame] | 2948 | unsigned RHSCode = getICmpCode(RHSICI); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2949 | unsigned Code; |
| 2950 | switch (Log.getOpcode()) { |
| 2951 | case Instruction::And: Code = LHSCode & RHSCode; break; |
| 2952 | case Instruction::Or: Code = LHSCode | RHSCode; break; |
| 2953 | case Instruction::Xor: Code = LHSCode ^ RHSCode; break; |
Chris Lattner | 021c190 | 2003-09-22 20:33:34 +0000 | [diff] [blame] | 2954 | default: assert(0 && "Illegal logical opcode!"); return 0; |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2955 | } |
| 2956 | |
Chris Lattner | bc1dbfc | 2007-03-13 14:27:42 +0000 | [diff] [blame] | 2957 | bool isSigned = ICmpInst::isSignedPredicate(RHSICI->getPredicate()) || |
| 2958 | ICmpInst::isSignedPredicate(ICI->getPredicate()); |
| 2959 | |
| 2960 | Value *RV = getICmpValue(isSigned, Code, LHS, RHS); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2961 | if (Instruction *I = dyn_cast<Instruction>(RV)) |
| 2962 | return I; |
| 2963 | // Otherwise, it's a constant boolean value... |
| 2964 | return IC.ReplaceInstUsesWith(Log, RV); |
| 2965 | } |
| 2966 | }; |
Chris Lattner | d23b5ba | 2006-11-15 04:53:24 +0000 | [diff] [blame] | 2967 | } // end anonymous namespace |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2968 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2969 | // OptAndOp - This handles expressions of the form ((val OP C1) & C2). Where |
| 2970 | // 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] | 2971 | // guaranteed to be a binary operator. |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2972 | Instruction *InstCombiner::OptAndOp(Instruction *Op, |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2973 | ConstantInt *OpRHS, |
| 2974 | ConstantInt *AndRHS, |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2975 | BinaryOperator &TheAnd) { |
| 2976 | Value *X = Op->getOperand(0); |
Chris Lattner | 76f7fe2 | 2004-01-12 19:47:05 +0000 | [diff] [blame] | 2977 | Constant *Together = 0; |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2978 | if (!Op->isShift()) |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2979 | Together = And(AndRHS, OpRHS); |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2980 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2981 | switch (Op->getOpcode()) { |
| 2982 | case Instruction::Xor: |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2983 | if (Op->hasOneUse()) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2984 | // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2) |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 2985 | Instruction *And = BinaryOperator::createAnd(X, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2986 | InsertNewInstBefore(And, TheAnd); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 2987 | And->takeName(Op); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2988 | return BinaryOperator::createXor(And, Together); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2989 | } |
| 2990 | break; |
| 2991 | case Instruction::Or: |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2992 | if (Together == AndRHS) // (X | C) & C --> C |
| 2993 | return ReplaceInstUsesWith(TheAnd, AndRHS); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2994 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2995 | if (Op->hasOneUse() && Together != OpRHS) { |
| 2996 | // (X | C1) & C2 --> (X | (C1&C2)) & C2 |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 2997 | Instruction *Or = BinaryOperator::createOr(X, Together); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2998 | InsertNewInstBefore(Or, TheAnd); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 2999 | Or->takeName(Op); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3000 | return BinaryOperator::createAnd(Or, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3001 | } |
| 3002 | break; |
| 3003 | case Instruction::Add: |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 3004 | if (Op->hasOneUse()) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3005 | // Adding a one to a single bit bit-field should be turned into an XOR |
| 3006 | // of the bit. First thing to check is to see if this AND is with a |
| 3007 | // single bit constant. |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3008 | const APInt& AndRHSV = cast<ConstantInt>(AndRHS)->getValue(); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3009 | |
| 3010 | // If there is only one bit set... |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 3011 | if (isOneBitSet(cast<ConstantInt>(AndRHS))) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3012 | // Ok, at this point, we know that we are masking the result of the |
| 3013 | // ADD down to exactly one bit. If the constant we are adding has |
| 3014 | // no bits set below this bit, then we can eliminate the ADD. |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3015 | const APInt& AddRHS = cast<ConstantInt>(OpRHS)->getValue(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3016 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3017 | // Check to see if any bits below the one bit set in AndRHSV are set. |
| 3018 | if ((AddRHS & (AndRHSV-1)) == 0) { |
| 3019 | // If not, the only thing that can effect the output of the AND is |
| 3020 | // the bit specified by AndRHSV. If that bit is set, the effect of |
| 3021 | // the XOR is to toggle the bit. If it is clear, then the ADD has |
| 3022 | // no effect. |
| 3023 | if ((AddRHS & AndRHSV) == 0) { // Bit is not set, noop |
| 3024 | TheAnd.setOperand(0, X); |
| 3025 | return &TheAnd; |
| 3026 | } else { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3027 | // Pull the XOR out of the AND. |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3028 | Instruction *NewAnd = BinaryOperator::createAnd(X, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3029 | InsertNewInstBefore(NewAnd, TheAnd); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3030 | NewAnd->takeName(Op); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3031 | return BinaryOperator::createXor(NewAnd, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3032 | } |
| 3033 | } |
| 3034 | } |
| 3035 | } |
| 3036 | break; |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 3037 | |
| 3038 | case Instruction::Shl: { |
| 3039 | // We know that the AND will not produce any of the bits shifted in, so if |
| 3040 | // the anded constant includes them, clear them now! |
| 3041 | // |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3042 | uint32_t BitWidth = AndRHS->getType()->getBitWidth(); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3043 | uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3044 | APInt ShlMask(APInt::getHighBitsSet(BitWidth, BitWidth-OpRHSVal)); |
| 3045 | ConstantInt *CI = ConstantInt::get(AndRHS->getValue() & ShlMask); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3046 | |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3047 | if (CI->getValue() == ShlMask) { |
| 3048 | // Masking out bits that the shift already masks |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 3049 | return ReplaceInstUsesWith(TheAnd, Op); // No need for the and. |
| 3050 | } else if (CI != AndRHS) { // Reducing bits set in and. |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 3051 | TheAnd.setOperand(1, CI); |
| 3052 | return &TheAnd; |
| 3053 | } |
| 3054 | break; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3055 | } |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 3056 | case Instruction::LShr: |
| 3057 | { |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 3058 | // We know that the AND will not produce any of the bits shifted in, so if |
| 3059 | // the anded constant includes them, clear them now! This only applies to |
| 3060 | // unsigned shifts, because a signed shr may bring in set bits! |
| 3061 | // |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3062 | uint32_t BitWidth = AndRHS->getType()->getBitWidth(); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3063 | uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3064 | APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal)); |
| 3065 | ConstantInt *CI = ConstantInt::get(AndRHS->getValue() & ShrMask); |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 3066 | |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3067 | if (CI->getValue() == ShrMask) { |
| 3068 | // Masking out bits that the shift already masks. |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 3069 | return ReplaceInstUsesWith(TheAnd, Op); |
| 3070 | } else if (CI != AndRHS) { |
| 3071 | TheAnd.setOperand(1, CI); // Reduce bits set in and cst. |
| 3072 | return &TheAnd; |
| 3073 | } |
| 3074 | break; |
| 3075 | } |
| 3076 | case Instruction::AShr: |
| 3077 | // Signed shr. |
| 3078 | // See if this is shifting in some sign extension, then masking it out |
| 3079 | // with an and. |
| 3080 | if (Op->hasOneUse()) { |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3081 | uint32_t BitWidth = AndRHS->getType()->getBitWidth(); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3082 | uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3083 | APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal)); |
| 3084 | Constant *C = ConstantInt::get(AndRHS->getValue() & ShrMask); |
Reid Spencer | 7eb7638 | 2006-12-13 17:19:09 +0000 | [diff] [blame] | 3085 | if (C == AndRHS) { // Masking out bits shifted in. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 3086 | // (Val ashr C1) & C2 -> (Val lshr C1) & C2 |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 3087 | // Make the argument unsigned. |
| 3088 | Value *ShVal = Op->getOperand(0); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3089 | ShVal = InsertNewInstBefore( |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 3090 | BinaryOperator::createLShr(ShVal, OpRHS, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3091 | Op->getName()), TheAnd); |
Reid Spencer | 7eb7638 | 2006-12-13 17:19:09 +0000 | [diff] [blame] | 3092 | return BinaryOperator::createAnd(ShVal, AndRHS, TheAnd.getName()); |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 3093 | } |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 3094 | } |
| 3095 | break; |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3096 | } |
| 3097 | return 0; |
| 3098 | } |
| 3099 | |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 3100 | |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3101 | /// InsertRangeTest - Emit a computation of: (V >= Lo && V < Hi) if Inside is |
| 3102 | /// 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] | 3103 | /// (V-Lo) <u Hi-Lo. This method expects that Lo <= Hi. isSigned indicates |
| 3104 | /// 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] | 3105 | /// insert new instructions. |
| 3106 | Instruction *InstCombiner::InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3107 | bool isSigned, bool Inside, |
| 3108 | Instruction &IB) { |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3109 | assert(cast<ConstantInt>(ConstantExpr::getICmp((isSigned ? |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 3110 | ICmpInst::ICMP_SLE:ICmpInst::ICMP_ULE), Lo, Hi))->getZExtValue() && |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3111 | "Lo is not <= Hi in range emission code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3112 | |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3113 | if (Inside) { |
| 3114 | if (Lo == Hi) // Trivially false. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3115 | return new ICmpInst(ICmpInst::ICMP_NE, V, V); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3116 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3117 | // V >= Min && V < Hi --> V < Hi |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3118 | if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) { |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 3119 | ICmpInst::Predicate pred = (isSigned ? |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3120 | ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT); |
| 3121 | return new ICmpInst(pred, V, Hi); |
| 3122 | } |
| 3123 | |
| 3124 | // Emit V-Lo <u Hi-Lo |
| 3125 | Constant *NegLo = ConstantExpr::getNeg(Lo); |
| 3126 | Instruction *Add = BinaryOperator::createAdd(V, NegLo, V->getName()+".off"); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3127 | InsertNewInstBefore(Add, IB); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3128 | Constant *UpperBound = ConstantExpr::getAdd(NegLo, Hi); |
| 3129 | return new ICmpInst(ICmpInst::ICMP_ULT, Add, UpperBound); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3130 | } |
| 3131 | |
| 3132 | if (Lo == Hi) // Trivially true. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3133 | return new ICmpInst(ICmpInst::ICMP_EQ, V, V); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3134 | |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 3135 | // V < Min || V >= Hi -> V > Hi-1 |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3136 | Hi = SubOne(cast<ConstantInt>(Hi)); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3137 | if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3138 | ICmpInst::Predicate pred = (isSigned ? |
| 3139 | ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT); |
| 3140 | return new ICmpInst(pred, V, Hi); |
| 3141 | } |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3142 | |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 3143 | // Emit V-Lo >u Hi-1-Lo |
| 3144 | // Note that Hi has already had one subtracted from it, above. |
| 3145 | ConstantInt *NegLo = cast<ConstantInt>(ConstantExpr::getNeg(Lo)); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3146 | Instruction *Add = BinaryOperator::createAdd(V, NegLo, V->getName()+".off"); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3147 | InsertNewInstBefore(Add, IB); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3148 | Constant *LowerBound = ConstantExpr::getAdd(NegLo, Hi); |
| 3149 | return new ICmpInst(ICmpInst::ICMP_UGT, Add, LowerBound); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3150 | } |
| 3151 | |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3152 | // isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with |
| 3153 | // any number of 0s on either side. The 1s are allowed to wrap from LSB to |
| 3154 | // MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is |
| 3155 | // not, since all 1s are not contiguous. |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 3156 | static bool isRunOfOnes(ConstantInt *Val, uint32_t &MB, uint32_t &ME) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3157 | const APInt& V = Val->getValue(); |
Reid Spencer | f244252 | 2007-03-24 00:42:08 +0000 | [diff] [blame] | 3158 | uint32_t BitWidth = Val->getType()->getBitWidth(); |
| 3159 | if (!APIntOps::isShiftedMask(BitWidth, V)) return false; |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3160 | |
| 3161 | // look for the first zero bit after the run of ones |
Reid Spencer | f244252 | 2007-03-24 00:42:08 +0000 | [diff] [blame] | 3162 | MB = BitWidth - ((V - 1) ^ V).countLeadingZeros(); |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3163 | // look for the first non-zero bit |
Reid Spencer | f244252 | 2007-03-24 00:42:08 +0000 | [diff] [blame] | 3164 | ME = V.getActiveBits(); |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3165 | return true; |
| 3166 | } |
| 3167 | |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3168 | /// FoldLogicalPlusAnd - This is part of an expression (LHS +/- RHS) & Mask, |
| 3169 | /// where isSub determines whether the operator is a sub. If we can fold one of |
| 3170 | /// the following xforms: |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3171 | /// |
| 3172 | /// ((A & N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == Mask |
| 3173 | /// ((A | N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0 |
| 3174 | /// ((A ^ N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0 |
| 3175 | /// |
| 3176 | /// return (A +/- B). |
| 3177 | /// |
| 3178 | Value *InstCombiner::FoldLogicalPlusAnd(Value *LHS, Value *RHS, |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3179 | ConstantInt *Mask, bool isSub, |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3180 | Instruction &I) { |
| 3181 | Instruction *LHSI = dyn_cast<Instruction>(LHS); |
| 3182 | if (!LHSI || LHSI->getNumOperands() != 2 || |
| 3183 | !isa<ConstantInt>(LHSI->getOperand(1))) return 0; |
| 3184 | |
| 3185 | ConstantInt *N = cast<ConstantInt>(LHSI->getOperand(1)); |
| 3186 | |
| 3187 | switch (LHSI->getOpcode()) { |
| 3188 | default: return 0; |
| 3189 | case Instruction::And: |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 3190 | if (And(N, Mask) == Mask) { |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3191 | // 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] | 3192 | if ((Mask->getValue().countLeadingZeros() + |
| 3193 | Mask->getValue().countPopulation()) == |
| 3194 | Mask->getValue().getBitWidth()) |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3195 | break; |
| 3196 | |
| 3197 | // Otherwise, if Mask is 0+1+0+, and if B is known to have the low 0+ |
| 3198 | // part, we don't need any explicit masks to take them out of A. If that |
| 3199 | // is all N is, ignore it. |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 3200 | uint32_t MB = 0, ME = 0; |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3201 | if (isRunOfOnes(Mask, MB, ME)) { // begin/end bit of run, inclusive |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 3202 | uint32_t BitWidth = cast<IntegerType>(RHS->getType())->getBitWidth(); |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3203 | APInt Mask(APInt::getLowBitsSet(BitWidth, MB-1)); |
Chris Lattner | 3bedbd9 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3204 | if (MaskedValueIsZero(RHS, Mask)) |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3205 | break; |
| 3206 | } |
| 3207 | } |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3208 | return 0; |
| 3209 | case Instruction::Or: |
| 3210 | case Instruction::Xor: |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3211 | // 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] | 3212 | if ((Mask->getValue().countLeadingZeros() + |
| 3213 | Mask->getValue().countPopulation()) == Mask->getValue().getBitWidth() |
Reid Spencer | 6eb0d99 | 2007-03-26 23:58:26 +0000 | [diff] [blame] | 3214 | && And(N, Mask)->isZero()) |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3215 | break; |
| 3216 | return 0; |
| 3217 | } |
| 3218 | |
| 3219 | Instruction *New; |
| 3220 | if (isSub) |
| 3221 | New = BinaryOperator::createSub(LHSI->getOperand(0), RHS, "fold"); |
| 3222 | else |
| 3223 | New = BinaryOperator::createAdd(LHSI->getOperand(0), RHS, "fold"); |
| 3224 | return InsertNewInstBefore(New, I); |
| 3225 | } |
| 3226 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3227 | Instruction *InstCombiner::visitAnd(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 3228 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3229 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3230 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3231 | if (isa<UndefValue>(Op1)) // X & undef -> 0 |
| 3232 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 3233 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3234 | // and X, X = X |
| 3235 | if (Op0 == Op1) |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 3236 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3237 | |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3238 | // 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] | 3239 | // purpose is to compute bits we don't care about. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 3240 | if (!isa<VectorType>(I.getType())) { |
Reid Spencer | a03d45f | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 3241 | uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth(); |
| 3242 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 3243 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth), |
Chris Lattner | 696ee0a | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3244 | KnownZero, KnownOne)) |
Reid Spencer | 6eb0d99 | 2007-03-26 23:58:26 +0000 | [diff] [blame] | 3245 | return &I; |
Chris Lattner | 696ee0a | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3246 | } else { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 3247 | if (ConstantVector *CP = dyn_cast<ConstantVector>(Op1)) { |
Chris Lattner | 696ee0a | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3248 | if (CP->isAllOnesValue()) |
| 3249 | return ReplaceInstUsesWith(I, I.getOperand(0)); |
| 3250 | } |
| 3251 | } |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 3252 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3253 | if (ConstantInt *AndRHS = dyn_cast<ConstantInt>(Op1)) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3254 | const APInt& AndRHSMask = AndRHS->getValue(); |
| 3255 | APInt NotAndRHS(~AndRHSMask); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3256 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3257 | // Optimize a variety of ((val OP C1) & C2) combinations... |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3258 | if (isa<BinaryOperator>(Op0)) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3259 | Instruction *Op0I = cast<Instruction>(Op0); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3260 | Value *Op0LHS = Op0I->getOperand(0); |
| 3261 | Value *Op0RHS = Op0I->getOperand(1); |
| 3262 | switch (Op0I->getOpcode()) { |
| 3263 | case Instruction::Xor: |
| 3264 | case Instruction::Or: |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 3265 | // If the mask is only needed on one incoming arm, push it up. |
| 3266 | if (Op0I->hasOneUse()) { |
| 3267 | if (MaskedValueIsZero(Op0LHS, NotAndRHS)) { |
| 3268 | // Not masking anything out for the LHS, move to RHS. |
| 3269 | Instruction *NewRHS = BinaryOperator::createAnd(Op0RHS, AndRHS, |
| 3270 | Op0RHS->getName()+".masked"); |
| 3271 | InsertNewInstBefore(NewRHS, I); |
| 3272 | return BinaryOperator::create( |
| 3273 | cast<BinaryOperator>(Op0I)->getOpcode(), Op0LHS, NewRHS); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3274 | } |
Chris Lattner | 3bedbd9 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3275 | if (!isa<Constant>(Op0RHS) && |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 3276 | MaskedValueIsZero(Op0RHS, NotAndRHS)) { |
| 3277 | // Not masking anything out for the RHS, move to LHS. |
| 3278 | Instruction *NewLHS = BinaryOperator::createAnd(Op0LHS, AndRHS, |
| 3279 | Op0LHS->getName()+".masked"); |
| 3280 | InsertNewInstBefore(NewLHS, I); |
| 3281 | return BinaryOperator::create( |
| 3282 | cast<BinaryOperator>(Op0I)->getOpcode(), NewLHS, Op0RHS); |
| 3283 | } |
| 3284 | } |
| 3285 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3286 | break; |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3287 | case Instruction::Add: |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3288 | // ((A & N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == AndRHS. |
| 3289 | // ((A | N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0 |
| 3290 | // ((A ^ N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0 |
| 3291 | if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, false, I)) |
| 3292 | return BinaryOperator::createAnd(V, AndRHS); |
| 3293 | if (Value *V = FoldLogicalPlusAnd(Op0RHS, Op0LHS, AndRHS, false, I)) |
| 3294 | return BinaryOperator::createAnd(V, AndRHS); // Add commutes |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3295 | break; |
| 3296 | |
| 3297 | case Instruction::Sub: |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3298 | // ((A & N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == AndRHS. |
| 3299 | // ((A | N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0 |
| 3300 | // ((A ^ N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0 |
| 3301 | if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, true, I)) |
| 3302 | return BinaryOperator::createAnd(V, AndRHS); |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3303 | break; |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3304 | } |
| 3305 | |
Chris Lattner | 5840326 | 2003-07-23 19:25:52 +0000 | [diff] [blame] | 3306 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3307 | if (Instruction *Res = OptAndOp(Op0I, Op0CI, AndRHS, I)) |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3308 | return Res; |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3309 | } else if (CastInst *CI = dyn_cast<CastInst>(Op0)) { |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3310 | // If this is an integer truncation or change from signed-to-unsigned, and |
| 3311 | // if the source is an and/or with immediate, transform it. This |
| 3312 | // frequently occurs for bitfield accesses. |
| 3313 | if (Instruction *CastOp = dyn_cast<Instruction>(CI->getOperand(0))) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3314 | if ((isa<TruncInst>(CI) || isa<BitCastInst>(CI)) && |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3315 | CastOp->getNumOperands() == 2) |
Chris Lattner | 7560c3a | 2006-02-08 07:34:50 +0000 | [diff] [blame] | 3316 | if (ConstantInt *AndCI = dyn_cast<ConstantInt>(CastOp->getOperand(1))) |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3317 | if (CastOp->getOpcode() == Instruction::And) { |
| 3318 | // Change: and (cast (and X, C1) to T), C2 |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3319 | // into : and (cast X to T), trunc_or_bitcast(C1)&C2 |
| 3320 | // This will fold the two constants together, which may allow |
| 3321 | // other simplifications. |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 3322 | Instruction *NewCast = CastInst::createTruncOrBitCast( |
| 3323 | CastOp->getOperand(0), I.getType(), |
| 3324 | CastOp->getName()+".shrunk"); |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3325 | NewCast = InsertNewInstBefore(NewCast, I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3326 | // trunc_or_bitcast(C1)&C2 |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 3327 | Constant *C3 = ConstantExpr::getTruncOrBitCast(AndCI,I.getType()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3328 | C3 = ConstantExpr::getAnd(C3, AndRHS); |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3329 | return BinaryOperator::createAnd(NewCast, C3); |
| 3330 | } else if (CastOp->getOpcode() == Instruction::Or) { |
| 3331 | // Change: and (cast (or X, C1) to T), C2 |
| 3332 | // into : trunc(C1)&C2 iff trunc(C1)&C2 == C2 |
Chris Lattner | bb4e7b2 | 2006-12-12 19:11:20 +0000 | [diff] [blame] | 3333 | Constant *C3 = ConstantExpr::getTruncOrBitCast(AndCI,I.getType()); |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3334 | if (ConstantExpr::getAnd(C3, AndRHS) == AndRHS) // trunc(C1)&C2 |
| 3335 | return ReplaceInstUsesWith(I, AndRHS); |
| 3336 | } |
| 3337 | } |
Chris Lattner | 06782f8 | 2003-07-23 19:36:21 +0000 | [diff] [blame] | 3338 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3339 | |
| 3340 | // Try to fold constant and into select arguments. |
| 3341 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3342 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3343 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 3344 | if (isa<PHINode>(Op0)) |
| 3345 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 3346 | return NV; |
Chris Lattner | c6a8aff | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 3347 | } |
| 3348 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 3349 | Value *Op0NotVal = dyn_castNotVal(Op0); |
| 3350 | Value *Op1NotVal = dyn_castNotVal(Op1); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3351 | |
Chris Lattner | 5b62aa7 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 3352 | if (Op0NotVal == Op1 || Op1NotVal == Op0) // A & ~A == ~A & A == 0 |
| 3353 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 3354 | |
Misha Brukman | cb6267b | 2004-07-30 12:50:08 +0000 | [diff] [blame] | 3355 | // (~A & ~B) == (~(A | B)) - De Morgan's Law |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 3356 | if (Op0NotVal && Op1NotVal && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3357 | Instruction *Or = BinaryOperator::createOr(Op0NotVal, Op1NotVal, |
| 3358 | I.getName()+".demorgan"); |
Chris Lattner | c6a8aff | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 3359 | InsertNewInstBefore(Or, I); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3360 | return BinaryOperator::createNot(Or); |
| 3361 | } |
Chris Lattner | 2082ad9 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3362 | |
| 3363 | { |
| 3364 | Value *A = 0, *B = 0; |
Chris Lattner | 2082ad9 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3365 | if (match(Op0, m_Or(m_Value(A), m_Value(B)))) |
| 3366 | if (A == Op1 || B == Op1) // (A | ?) & A --> A |
| 3367 | return ReplaceInstUsesWith(I, Op1); |
| 3368 | if (match(Op1, m_Or(m_Value(A), m_Value(B)))) |
| 3369 | if (A == Op0 || B == Op0) // A & (A | ?) --> A |
| 3370 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 3371 | |
| 3372 | if (Op0->hasOneUse() && |
| 3373 | match(Op0, m_Xor(m_Value(A), m_Value(B)))) { |
| 3374 | if (A == Op1) { // (A^B)&A -> A&(A^B) |
| 3375 | I.swapOperands(); // Simplify below |
| 3376 | std::swap(Op0, Op1); |
| 3377 | } else if (B == Op1) { // (A^B)&B -> B&(B^A) |
| 3378 | cast<BinaryOperator>(Op0)->swapOperands(); |
| 3379 | I.swapOperands(); // Simplify below |
| 3380 | std::swap(Op0, Op1); |
| 3381 | } |
| 3382 | } |
| 3383 | if (Op1->hasOneUse() && |
| 3384 | match(Op1, m_Xor(m_Value(A), m_Value(B)))) { |
| 3385 | if (B == Op0) { // B&(A^B) -> B&(B^A) |
| 3386 | cast<BinaryOperator>(Op1)->swapOperands(); |
| 3387 | std::swap(A, B); |
| 3388 | } |
| 3389 | if (A == Op0) { // A&(A^B) -> A & ~B |
| 3390 | Instruction *NotB = BinaryOperator::createNot(B, "tmp"); |
| 3391 | InsertNewInstBefore(NotB, I); |
| 3392 | return BinaryOperator::createAnd(A, NotB); |
| 3393 | } |
| 3394 | } |
Chris Lattner | 2082ad9 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3395 | } |
| 3396 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3397 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(Op1)) { |
| 3398 | // (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B) |
| 3399 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS))) |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3400 | return R; |
| 3401 | |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3402 | Value *LHSVal, *RHSVal; |
| 3403 | ConstantInt *LHSCst, *RHSCst; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3404 | ICmpInst::Predicate LHSCC, RHSCC; |
| 3405 | if (match(Op0, m_ICmp(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst)))) |
| 3406 | if (match(RHS, m_ICmp(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst)))) |
| 3407 | if (LHSVal == RHSVal && // Found (X icmp C1) & (X icmp C2) |
| 3408 | // ICMP_[GL]E X, CST is folded to ICMP_[GL]T elsewhere. |
| 3409 | LHSCC != ICmpInst::ICMP_UGE && LHSCC != ICmpInst::ICMP_ULE && |
| 3410 | RHSCC != ICmpInst::ICMP_UGE && RHSCC != ICmpInst::ICMP_ULE && |
| 3411 | LHSCC != ICmpInst::ICMP_SGE && LHSCC != ICmpInst::ICMP_SLE && |
| 3412 | RHSCC != ICmpInst::ICMP_SGE && RHSCC != ICmpInst::ICMP_SLE) { |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3413 | // Ensure that the larger constant is on the RHS. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3414 | ICmpInst::Predicate GT = ICmpInst::isSignedPredicate(LHSCC) ? |
| 3415 | ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; |
| 3416 | Constant *Cmp = ConstantExpr::getICmp(GT, LHSCst, RHSCst); |
| 3417 | ICmpInst *LHS = cast<ICmpInst>(Op0); |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 3418 | if (cast<ConstantInt>(Cmp)->getZExtValue()) { |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3419 | std::swap(LHS, RHS); |
| 3420 | std::swap(LHSCst, RHSCst); |
| 3421 | std::swap(LHSCC, RHSCC); |
| 3422 | } |
| 3423 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3424 | // At this point, we know we have have two icmp instructions |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3425 | // comparing a value against two constants and and'ing the result |
| 3426 | // together. Because of the above check, we know that we only have |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3427 | // icmp eq, icmp ne, icmp [su]lt, and icmp [SU]gt here. We also know |
| 3428 | // (from the FoldICmpLogical check above), that the two constants |
| 3429 | // are not equal and that the larger constant is on the RHS |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3430 | assert(LHSCst != RHSCst && "Compares not folded above?"); |
| 3431 | |
| 3432 | switch (LHSCC) { |
| 3433 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3434 | case ICmpInst::ICMP_EQ: |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3435 | switch (RHSCC) { |
| 3436 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3437 | case ICmpInst::ICMP_EQ: // (X == 13 & X == 15) -> false |
| 3438 | case ICmpInst::ICMP_UGT: // (X == 13 & X > 15) -> false |
| 3439 | case ICmpInst::ICMP_SGT: // (X == 13 & X > 15) -> false |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3440 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3441 | case ICmpInst::ICMP_NE: // (X == 13 & X != 15) -> X == 13 |
| 3442 | case ICmpInst::ICMP_ULT: // (X == 13 & X < 15) -> X == 13 |
| 3443 | case ICmpInst::ICMP_SLT: // (X == 13 & X < 15) -> X == 13 |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3444 | return ReplaceInstUsesWith(I, LHS); |
| 3445 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3446 | case ICmpInst::ICMP_NE: |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3447 | switch (RHSCC) { |
| 3448 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3449 | case ICmpInst::ICMP_ULT: |
| 3450 | if (LHSCst == SubOne(RHSCst)) // (X != 13 & X u< 14) -> X < 13 |
| 3451 | return new ICmpInst(ICmpInst::ICMP_ULT, LHSVal, LHSCst); |
| 3452 | break; // (X != 13 & X u< 15) -> no change |
| 3453 | case ICmpInst::ICMP_SLT: |
| 3454 | if (LHSCst == SubOne(RHSCst)) // (X != 13 & X s< 14) -> X < 13 |
| 3455 | return new ICmpInst(ICmpInst::ICMP_SLT, LHSVal, LHSCst); |
| 3456 | break; // (X != 13 & X s< 15) -> no change |
| 3457 | case ICmpInst::ICMP_EQ: // (X != 13 & X == 15) -> X == 15 |
| 3458 | case ICmpInst::ICMP_UGT: // (X != 13 & X u> 15) -> X u> 15 |
| 3459 | case ICmpInst::ICMP_SGT: // (X != 13 & X s> 15) -> X s> 15 |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3460 | return ReplaceInstUsesWith(I, RHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3461 | case ICmpInst::ICMP_NE: |
| 3462 | if (LHSCst == SubOne(RHSCst)){// (X != 13 & X != 14) -> X-13 >u 1 |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3463 | Constant *AddCST = ConstantExpr::getNeg(LHSCst); |
| 3464 | Instruction *Add = BinaryOperator::createAdd(LHSVal, AddCST, |
| 3465 | LHSVal->getName()+".off"); |
| 3466 | InsertNewInstBefore(Add, I); |
Chris Lattner | 424db02 | 2007-01-27 23:08:34 +0000 | [diff] [blame] | 3467 | return new ICmpInst(ICmpInst::ICMP_UGT, Add, |
| 3468 | ConstantInt::get(Add->getType(), 1)); |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3469 | } |
| 3470 | break; // (X != 13 & X != 15) -> no change |
| 3471 | } |
| 3472 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3473 | case ICmpInst::ICMP_ULT: |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3474 | switch (RHSCC) { |
| 3475 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3476 | case ICmpInst::ICMP_EQ: // (X u< 13 & X == 15) -> false |
| 3477 | case ICmpInst::ICMP_UGT: // (X u< 13 & X u> 15) -> false |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3478 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3479 | case ICmpInst::ICMP_SGT: // (X u< 13 & X s> 15) -> no change |
| 3480 | break; |
| 3481 | case ICmpInst::ICMP_NE: // (X u< 13 & X != 15) -> X u< 13 |
| 3482 | 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] | 3483 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3484 | case ICmpInst::ICMP_SLT: // (X u< 13 & X s< 15) -> no change |
| 3485 | break; |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3486 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3487 | break; |
| 3488 | case ICmpInst::ICMP_SLT: |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3489 | switch (RHSCC) { |
| 3490 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3491 | case ICmpInst::ICMP_EQ: // (X s< 13 & X == 15) -> false |
| 3492 | case ICmpInst::ICMP_SGT: // (X s< 13 & X s> 15) -> false |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3493 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3494 | case ICmpInst::ICMP_UGT: // (X s< 13 & X u> 15) -> no change |
| 3495 | break; |
| 3496 | case ICmpInst::ICMP_NE: // (X s< 13 & X != 15) -> X < 13 |
| 3497 | case ICmpInst::ICMP_SLT: // (X s< 13 & X s< 15) -> X < 13 |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3498 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3499 | case ICmpInst::ICMP_ULT: // (X s< 13 & X u< 15) -> no change |
| 3500 | break; |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3501 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3502 | break; |
| 3503 | case ICmpInst::ICMP_UGT: |
| 3504 | switch (RHSCC) { |
| 3505 | default: assert(0 && "Unknown integer condition code!"); |
| 3506 | case ICmpInst::ICMP_EQ: // (X u> 13 & X == 15) -> X > 13 |
| 3507 | return ReplaceInstUsesWith(I, LHS); |
| 3508 | case ICmpInst::ICMP_UGT: // (X u> 13 & X u> 15) -> X u> 15 |
| 3509 | return ReplaceInstUsesWith(I, RHS); |
| 3510 | case ICmpInst::ICMP_SGT: // (X u> 13 & X s> 15) -> no change |
| 3511 | break; |
| 3512 | case ICmpInst::ICMP_NE: |
| 3513 | if (RHSCst == AddOne(LHSCst)) // (X u> 13 & X != 14) -> X u> 14 |
| 3514 | return new ICmpInst(LHSCC, LHSVal, RHSCst); |
| 3515 | break; // (X u> 13 & X != 15) -> no change |
| 3516 | case ICmpInst::ICMP_ULT: // (X u> 13 & X u< 15) ->(X-14) <u 1 |
| 3517 | return InsertRangeTest(LHSVal, AddOne(LHSCst), RHSCst, false, |
| 3518 | true, I); |
| 3519 | case ICmpInst::ICMP_SLT: // (X u> 13 & X s< 15) -> no change |
| 3520 | break; |
| 3521 | } |
| 3522 | break; |
| 3523 | case ICmpInst::ICMP_SGT: |
| 3524 | switch (RHSCC) { |
| 3525 | default: assert(0 && "Unknown integer condition code!"); |
| 3526 | case ICmpInst::ICMP_EQ: // (X s> 13 & X == 15) -> X s> 13 |
| 3527 | return ReplaceInstUsesWith(I, LHS); |
| 3528 | case ICmpInst::ICMP_SGT: // (X s> 13 & X s> 15) -> X s> 15 |
| 3529 | return ReplaceInstUsesWith(I, RHS); |
| 3530 | case ICmpInst::ICMP_UGT: // (X s> 13 & X u> 15) -> no change |
| 3531 | break; |
| 3532 | case ICmpInst::ICMP_NE: |
| 3533 | if (RHSCst == AddOne(LHSCst)) // (X s> 13 & X != 14) -> X s> 14 |
| 3534 | return new ICmpInst(LHSCC, LHSVal, RHSCst); |
| 3535 | break; // (X s> 13 & X != 15) -> no change |
| 3536 | case ICmpInst::ICMP_SLT: // (X s> 13 & X s< 15) ->(X-14) s< 1 |
| 3537 | return InsertRangeTest(LHSVal, AddOne(LHSCst), RHSCst, true, |
| 3538 | true, I); |
| 3539 | case ICmpInst::ICMP_ULT: // (X s> 13 & X u< 15) -> no change |
| 3540 | break; |
| 3541 | } |
| 3542 | break; |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3543 | } |
| 3544 | } |
| 3545 | } |
| 3546 | |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3547 | // fold (and (cast A), (cast B)) -> (cast (and A, B)) |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3548 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) |
| 3549 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) |
| 3550 | if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind ? |
| 3551 | const Type *SrcTy = Op0C->getOperand(0)->getType(); |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 3552 | if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() && |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3553 | // 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] | 3554 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), |
| 3555 | I.getType(), TD) && |
| 3556 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), |
| 3557 | I.getType(), TD)) { |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3558 | Instruction *NewOp = BinaryOperator::createAnd(Op0C->getOperand(0), |
| 3559 | Op1C->getOperand(0), |
| 3560 | I.getName()); |
| 3561 | InsertNewInstBefore(NewOp, I); |
| 3562 | return CastInst::create(Op0C->getOpcode(), NewOp, I.getType()); |
| 3563 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3564 | } |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 3565 | |
| 3566 | // (X >> Z) & (Y >> Z) -> (X&Y) >> Z for all shifts. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3567 | if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) { |
| 3568 | if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0)) |
| 3569 | if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() && |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 3570 | SI0->getOperand(1) == SI1->getOperand(1) && |
| 3571 | (SI0->hasOneUse() || SI1->hasOneUse())) { |
| 3572 | Instruction *NewOp = |
| 3573 | InsertNewInstBefore(BinaryOperator::createAnd(SI0->getOperand(0), |
| 3574 | SI1->getOperand(0), |
| 3575 | SI0->getName()), I); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3576 | return BinaryOperator::create(SI1->getOpcode(), NewOp, |
| 3577 | SI1->getOperand(1)); |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 3578 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3579 | } |
| 3580 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3581 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3582 | } |
| 3583 | |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3584 | /// CollectBSwapParts - Look to see if the specified value defines a single byte |
| 3585 | /// in the result. If it does, and if the specified byte hasn't been filled in |
| 3586 | /// yet, fill it in and return false. |
Chris Lattner | 535014f | 2007-02-15 22:52:10 +0000 | [diff] [blame] | 3587 | static bool CollectBSwapParts(Value *V, SmallVector<Value*, 8> &ByteValues) { |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3588 | Instruction *I = dyn_cast<Instruction>(V); |
| 3589 | if (I == 0) return true; |
| 3590 | |
| 3591 | // If this is an or instruction, it is an inner node of the bswap. |
| 3592 | if (I->getOpcode() == Instruction::Or) |
| 3593 | return CollectBSwapParts(I->getOperand(0), ByteValues) || |
| 3594 | CollectBSwapParts(I->getOperand(1), ByteValues); |
| 3595 | |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3596 | uint32_t BitWidth = I->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3597 | // If this is a shift by a constant int, and it is "24", then its operand |
| 3598 | // defines a byte. We only handle unsigned types here. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3599 | if (I->isShift() && isa<ConstantInt>(I->getOperand(1))) { |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3600 | // Not shifting the entire input by N-1 bytes? |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3601 | if (cast<ConstantInt>(I->getOperand(1))->getLimitedValue(BitWidth) != |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3602 | 8*(ByteValues.size()-1)) |
| 3603 | return true; |
| 3604 | |
| 3605 | unsigned DestNo; |
| 3606 | if (I->getOpcode() == Instruction::Shl) { |
| 3607 | // X << 24 defines the top byte with the lowest of the input bytes. |
| 3608 | DestNo = ByteValues.size()-1; |
| 3609 | } else { |
| 3610 | // X >>u 24 defines the low byte with the highest of the input bytes. |
| 3611 | DestNo = 0; |
| 3612 | } |
| 3613 | |
| 3614 | // If the destination byte value is already defined, the values are or'd |
| 3615 | // together, which isn't a bswap (unless it's an or of the same bits). |
| 3616 | if (ByteValues[DestNo] && ByteValues[DestNo] != I->getOperand(0)) |
| 3617 | return true; |
| 3618 | ByteValues[DestNo] = I->getOperand(0); |
| 3619 | return false; |
| 3620 | } |
| 3621 | |
| 3622 | // Otherwise, we can only handle and(shift X, imm), imm). Bail out of if we |
| 3623 | // don't have this. |
| 3624 | Value *Shift = 0, *ShiftLHS = 0; |
| 3625 | ConstantInt *AndAmt = 0, *ShiftAmt = 0; |
| 3626 | if (!match(I, m_And(m_Value(Shift), m_ConstantInt(AndAmt))) || |
| 3627 | !match(Shift, m_Shift(m_Value(ShiftLHS), m_ConstantInt(ShiftAmt)))) |
| 3628 | return true; |
| 3629 | Instruction *SI = cast<Instruction>(Shift); |
| 3630 | |
| 3631 | // 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] | 3632 | if (ShiftAmt->getLimitedValue(BitWidth) & 7 || |
| 3633 | ShiftAmt->getLimitedValue(BitWidth) > 8*ByteValues.size()) |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3634 | return true; |
| 3635 | |
| 3636 | // Turn 0xFF -> 0, 0xFF00 -> 1, 0xFF0000 -> 2, etc. |
| 3637 | unsigned DestByte; |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3638 | if (AndAmt->getValue().getActiveBits() > 64) |
| 3639 | return true; |
| 3640 | uint64_t AndAmtVal = AndAmt->getZExtValue(); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3641 | for (DestByte = 0; DestByte != ByteValues.size(); ++DestByte) |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3642 | if (AndAmtVal == uint64_t(0xFF) << 8*DestByte) |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3643 | break; |
| 3644 | // Unknown mask for bswap. |
| 3645 | if (DestByte == ByteValues.size()) return true; |
| 3646 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3647 | unsigned ShiftBytes = ShiftAmt->getZExtValue()/8; |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3648 | unsigned SrcByte; |
| 3649 | if (SI->getOpcode() == Instruction::Shl) |
| 3650 | SrcByte = DestByte - ShiftBytes; |
| 3651 | else |
| 3652 | SrcByte = DestByte + ShiftBytes; |
| 3653 | |
| 3654 | // If the SrcByte isn't a bswapped value from the DestByte, reject it. |
| 3655 | if (SrcByte != ByteValues.size()-DestByte-1) |
| 3656 | return true; |
| 3657 | |
| 3658 | // If the destination byte value is already defined, the values are or'd |
| 3659 | // together, which isn't a bswap (unless it's an or of the same bits). |
| 3660 | if (ByteValues[DestByte] && ByteValues[DestByte] != SI->getOperand(0)) |
| 3661 | return true; |
| 3662 | ByteValues[DestByte] = SI->getOperand(0); |
| 3663 | return false; |
| 3664 | } |
| 3665 | |
| 3666 | /// MatchBSwap - Given an OR instruction, check to see if this is a bswap idiom. |
| 3667 | /// If so, insert the new bswap intrinsic and return it. |
| 3668 | Instruction *InstCombiner::MatchBSwap(BinaryOperator &I) { |
Chris Lattner | 55fc8c4 | 2007-04-01 20:57:36 +0000 | [diff] [blame] | 3669 | const IntegerType *ITy = dyn_cast<IntegerType>(I.getType()); |
| 3670 | if (!ITy || ITy->getBitWidth() % 16) |
| 3671 | return 0; // Can only bswap pairs of bytes. Can't do vectors. |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3672 | |
| 3673 | /// ByteValues - For each byte of the result, we keep track of which value |
| 3674 | /// defines each byte. |
Chris Lattner | 535014f | 2007-02-15 22:52:10 +0000 | [diff] [blame] | 3675 | SmallVector<Value*, 8> ByteValues; |
Chris Lattner | 55fc8c4 | 2007-04-01 20:57:36 +0000 | [diff] [blame] | 3676 | ByteValues.resize(ITy->getBitWidth()/8); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3677 | |
| 3678 | // Try to find all the pieces corresponding to the bswap. |
| 3679 | if (CollectBSwapParts(I.getOperand(0), ByteValues) || |
| 3680 | CollectBSwapParts(I.getOperand(1), ByteValues)) |
| 3681 | return 0; |
| 3682 | |
| 3683 | // Check to see if all of the bytes come from the same value. |
| 3684 | Value *V = ByteValues[0]; |
| 3685 | if (V == 0) return 0; // Didn't find a byte? Must be zero. |
| 3686 | |
| 3687 | // Check to make sure that all of the bytes come from the same value. |
| 3688 | for (unsigned i = 1, e = ByteValues.size(); i != e; ++i) |
| 3689 | if (ByteValues[i] != V) |
| 3690 | return 0; |
Chris Lattner | 55fc8c4 | 2007-04-01 20:57:36 +0000 | [diff] [blame] | 3691 | const Type *Tys[] = { ITy, ITy }; |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3692 | Module *M = I.getParent()->getParent()->getParent(); |
Chris Lattner | 55fc8c4 | 2007-04-01 20:57:36 +0000 | [diff] [blame] | 3693 | Function *F = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 2); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3694 | return new CallInst(F, V); |
| 3695 | } |
| 3696 | |
| 3697 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3698 | Instruction *InstCombiner::visitOr(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 3699 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3700 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3701 | |
Chris Lattner | 42593e6 | 2007-03-24 23:56:43 +0000 | [diff] [blame] | 3702 | if (isa<UndefValue>(Op1)) // X | undef -> -1 |
| 3703 | return ReplaceInstUsesWith(I, ConstantInt::getAllOnesValue(I.getType())); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3704 | |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3705 | // or X, X = X |
| 3706 | if (Op0 == Op1) |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 3707 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3708 | |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3709 | // See if we can simplify any instructions used by the instruction whose sole |
| 3710 | // purpose is to compute bits we don't care about. |
Chris Lattner | 42593e6 | 2007-03-24 23:56:43 +0000 | [diff] [blame] | 3711 | if (!isa<VectorType>(I.getType())) { |
| 3712 | uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth(); |
| 3713 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 3714 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth), |
| 3715 | KnownZero, KnownOne)) |
| 3716 | return &I; |
| 3717 | } |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3718 | |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3719 | // or X, -1 == -1 |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3720 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 3721 | ConstantInt *C1 = 0; Value *X = 0; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3722 | // (X & C1) | C2 --> (X | C2) & (C1|C2) |
| 3723 | 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] | 3724 | Instruction *Or = BinaryOperator::createOr(X, RHS); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3725 | InsertNewInstBefore(Or, I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3726 | Or->takeName(Op0); |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 3727 | return BinaryOperator::createAnd(Or, |
| 3728 | ConstantInt::get(RHS->getValue() | C1->getValue())); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3729 | } |
Chris Lattner | ad44ebf | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 3730 | |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3731 | // (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2) |
| 3732 | 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] | 3733 | Instruction *Or = BinaryOperator::createOr(X, RHS); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3734 | InsertNewInstBefore(Or, I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3735 | Or->takeName(Op0); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3736 | return BinaryOperator::createXor(Or, |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 3737 | ConstantInt::get(C1->getValue() & ~RHS->getValue())); |
Chris Lattner | ad44ebf | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 3738 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3739 | |
| 3740 | // Try to fold constant and into select arguments. |
| 3741 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3742 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3743 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 3744 | if (isa<PHINode>(Op0)) |
| 3745 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 3746 | return NV; |
Chris Lattner | ad44ebf | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 3747 | } |
| 3748 | |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 3749 | Value *A = 0, *B = 0; |
| 3750 | ConstantInt *C1 = 0, *C2 = 0; |
Chris Lattner | f4d4c87 | 2005-05-07 23:49:08 +0000 | [diff] [blame] | 3751 | |
| 3752 | if (match(Op0, m_And(m_Value(A), m_Value(B)))) |
| 3753 | if (A == Op1 || B == Op1) // (A & ?) | A --> A |
| 3754 | return ReplaceInstUsesWith(I, Op1); |
| 3755 | if (match(Op1, m_And(m_Value(A), m_Value(B)))) |
| 3756 | if (A == Op0 || B == Op0) // A | (A & ?) --> A |
| 3757 | return ReplaceInstUsesWith(I, Op0); |
| 3758 | |
Chris Lattner | 6423d4c | 2006-07-10 20:25:24 +0000 | [diff] [blame] | 3759 | // (A | B) | C and A | (B | C) -> bswap if possible. |
| 3760 | // (A >> B) | (C << D) and (A << B) | (B >> C) -> bswap if possible. |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3761 | if (match(Op0, m_Or(m_Value(), m_Value())) || |
Chris Lattner | 6423d4c | 2006-07-10 20:25:24 +0000 | [diff] [blame] | 3762 | match(Op1, m_Or(m_Value(), m_Value())) || |
| 3763 | (match(Op0, m_Shift(m_Value(), m_Value())) && |
| 3764 | match(Op1, m_Shift(m_Value(), m_Value())))) { |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3765 | if (Instruction *BSwap = MatchBSwap(I)) |
| 3766 | return BSwap; |
| 3767 | } |
| 3768 | |
Chris Lattner | 6e4c649 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 3769 | // (X^C)|Y -> (X|Y)^C iff Y&C == 0 |
| 3770 | 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] | 3771 | MaskedValueIsZero(Op1, C1->getValue())) { |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3772 | Instruction *NOr = BinaryOperator::createOr(A, Op1); |
| 3773 | InsertNewInstBefore(NOr, I); |
| 3774 | NOr->takeName(Op0); |
| 3775 | return BinaryOperator::createXor(NOr, C1); |
Chris Lattner | 6e4c649 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 3776 | } |
| 3777 | |
| 3778 | // Y|(X^C) -> (X|Y)^C iff Y&C == 0 |
| 3779 | 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] | 3780 | MaskedValueIsZero(Op0, C1->getValue())) { |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3781 | Instruction *NOr = BinaryOperator::createOr(A, Op0); |
| 3782 | InsertNewInstBefore(NOr, I); |
| 3783 | NOr->takeName(Op0); |
| 3784 | return BinaryOperator::createXor(NOr, C1); |
Chris Lattner | 6e4c649 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 3785 | } |
| 3786 | |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 3787 | // (A & C)|(B & D) |
| 3788 | Value *C, *D; |
| 3789 | if (match(Op0, m_And(m_Value(A), m_Value(C))) && |
| 3790 | match(Op1, m_And(m_Value(B), m_Value(D)))) { |
Chris Lattner | 6cae0e0 | 2007-04-08 07:55:22 +0000 | [diff] [blame] | 3791 | Value *V1 = 0, *V2 = 0, *V3 = 0; |
| 3792 | C1 = dyn_cast<ConstantInt>(C); |
| 3793 | C2 = dyn_cast<ConstantInt>(D); |
| 3794 | if (C1 && C2) { // (A & C1)|(B & C2) |
| 3795 | // If we have: ((V + N) & C1) | (V & C2) |
| 3796 | // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0 |
| 3797 | // replace with V+N. |
| 3798 | if (C1->getValue() == ~C2->getValue()) { |
| 3799 | if ((C2->getValue() & (C2->getValue()+1)) == 0 && // C2 == 0+1+ |
| 3800 | match(A, m_Add(m_Value(V1), m_Value(V2)))) { |
| 3801 | // Add commutes, try both ways. |
| 3802 | if (V1 == B && MaskedValueIsZero(V2, C2->getValue())) |
| 3803 | return ReplaceInstUsesWith(I, A); |
| 3804 | if (V2 == B && MaskedValueIsZero(V1, C2->getValue())) |
| 3805 | return ReplaceInstUsesWith(I, A); |
| 3806 | } |
| 3807 | // Or commutes, try both ways. |
| 3808 | if ((C1->getValue() & (C1->getValue()+1)) == 0 && |
| 3809 | match(B, m_Add(m_Value(V1), m_Value(V2)))) { |
| 3810 | // Add commutes, try both ways. |
| 3811 | if (V1 == A && MaskedValueIsZero(V2, C1->getValue())) |
| 3812 | return ReplaceInstUsesWith(I, B); |
| 3813 | if (V2 == A && MaskedValueIsZero(V1, C1->getValue())) |
| 3814 | return ReplaceInstUsesWith(I, B); |
| 3815 | } |
| 3816 | } |
Chris Lattner | 044e533 | 2007-04-08 08:01:49 +0000 | [diff] [blame] | 3817 | V1 = 0; V2 = 0; V3 = 0; |
Chris Lattner | 6cae0e0 | 2007-04-08 07:55:22 +0000 | [diff] [blame] | 3818 | } |
| 3819 | |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 3820 | // Check to see if we have any common things being and'ed. If so, find the |
| 3821 | // terms for V1 & (V2|V3). |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 3822 | if (isOnlyUse(Op0) || isOnlyUse(Op1)) { |
| 3823 | if (A == B) // (A & C)|(A & D) == A & (C|D) |
| 3824 | V1 = A, V2 = C, V3 = D; |
| 3825 | else if (A == D) // (A & C)|(B & A) == A & (B|C) |
| 3826 | V1 = A, V2 = B, V3 = C; |
| 3827 | else if (C == B) // (A & C)|(C & D) == C & (A|D) |
| 3828 | V1 = C, V2 = A, V3 = D; |
| 3829 | else if (C == D) // (A & C)|(B & C) == C & (A|B) |
| 3830 | V1 = C, V2 = A, V3 = B; |
| 3831 | |
| 3832 | if (V1) { |
| 3833 | Value *Or = |
| 3834 | InsertNewInstBefore(BinaryOperator::createOr(V2, V3, "tmp"), I); |
| 3835 | return BinaryOperator::createAnd(V1, Or); |
Chris Lattner | 0b7c0bf | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 3836 | } |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 3837 | |
| 3838 | // (V1 & V3)|(V2 & ~V3) -> ((V1 ^ V2) & V3) ^ V2 |
Chris Lattner | 044e533 | 2007-04-08 08:01:49 +0000 | [diff] [blame] | 3839 | if (isOnlyUse(Op0) && isOnlyUse(Op1)) { |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 3840 | // Try all combination of terms to find V3 and ~V3. |
| 3841 | if (A->hasOneUse() && match(A, m_Not(m_Value(V3)))) { |
| 3842 | if (V3 == B) |
| 3843 | V1 = D, V2 = C; |
| 3844 | else if (V3 == D) |
| 3845 | V1 = B, V2 = C; |
| 3846 | } |
| 3847 | if (B->hasOneUse() && match(B, m_Not(m_Value(V3)))) { |
| 3848 | if (V3 == A) |
| 3849 | V1 = C, V2 = D; |
| 3850 | else if (V3 == C) |
| 3851 | V1 = A, V2 = D; |
| 3852 | } |
| 3853 | if (C->hasOneUse() && match(C, m_Not(m_Value(V3)))) { |
| 3854 | if (V3 == B) |
| 3855 | V1 = D, V2 = A; |
| 3856 | else if (V3 == D) |
| 3857 | V1 = B, V2 = A; |
| 3858 | } |
| 3859 | if (D->hasOneUse() && match(D, m_Not(m_Value(V3)))) { |
| 3860 | if (V3 == A) |
| 3861 | V1 = C, V2 = B; |
| 3862 | else if (V3 == C) |
| 3863 | V1 = A, V2 = B; |
| 3864 | } |
| 3865 | if (V1) { |
| 3866 | A = InsertNewInstBefore(BinaryOperator::createXor(V1, V2, "tmp"), I); |
| 3867 | A = InsertNewInstBefore(BinaryOperator::createAnd(A, V3, "tmp"), I); |
| 3868 | return BinaryOperator::createXor(A, V2); |
| 3869 | } |
| 3870 | } |
| 3871 | } |
Chris Lattner | e9bed7d | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 3872 | } |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 3873 | |
| 3874 | // (X >> Z) | (Y >> Z) -> (X|Y) >> Z for all shifts. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3875 | if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) { |
| 3876 | if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0)) |
| 3877 | if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() && |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 3878 | SI0->getOperand(1) == SI1->getOperand(1) && |
| 3879 | (SI0->hasOneUse() || SI1->hasOneUse())) { |
| 3880 | Instruction *NewOp = |
| 3881 | InsertNewInstBefore(BinaryOperator::createOr(SI0->getOperand(0), |
| 3882 | SI1->getOperand(0), |
| 3883 | SI0->getName()), I); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3884 | return BinaryOperator::create(SI1->getOpcode(), NewOp, |
| 3885 | SI1->getOperand(1)); |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 3886 | } |
| 3887 | } |
Chris Lattner | 67ca768 | 2003-08-12 19:11:07 +0000 | [diff] [blame] | 3888 | |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3889 | if (match(Op0, m_Not(m_Value(A)))) { // ~A | Op1 |
| 3890 | if (A == Op1) // ~A | A == -1 |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3891 | return ReplaceInstUsesWith(I, |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3892 | ConstantInt::getAllOnesValue(I.getType())); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3893 | } else { |
| 3894 | A = 0; |
| 3895 | } |
Chris Lattner | f4d4c87 | 2005-05-07 23:49:08 +0000 | [diff] [blame] | 3896 | // Note, A is still live here! |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3897 | if (match(Op1, m_Not(m_Value(B)))) { // Op0 | ~B |
| 3898 | if (Op0 == B) |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3899 | return ReplaceInstUsesWith(I, |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3900 | ConstantInt::getAllOnesValue(I.getType())); |
Chris Lattner | a27231a | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 3901 | |
Misha Brukman | cb6267b | 2004-07-30 12:50:08 +0000 | [diff] [blame] | 3902 | // (~A | ~B) == (~(A & B)) - De Morgan's Law |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3903 | if (A && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
| 3904 | Value *And = InsertNewInstBefore(BinaryOperator::createAnd(A, B, |
| 3905 | I.getName()+".demorgan"), I); |
| 3906 | return BinaryOperator::createNot(And); |
| 3907 | } |
Chris Lattner | a27231a | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 3908 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3909 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3910 | // (icmp1 A, B) | (icmp2 A, B) --> (icmp3 A, B) |
| 3911 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1))) { |
| 3912 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS))) |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3913 | return R; |
| 3914 | |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3915 | Value *LHSVal, *RHSVal; |
| 3916 | ConstantInt *LHSCst, *RHSCst; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3917 | ICmpInst::Predicate LHSCC, RHSCC; |
| 3918 | if (match(Op0, m_ICmp(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst)))) |
| 3919 | if (match(RHS, m_ICmp(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst)))) |
| 3920 | if (LHSVal == RHSVal && // Found (X icmp C1) | (X icmp C2) |
| 3921 | // icmp [us][gl]e x, cst is folded to icmp [us][gl]t elsewhere. |
| 3922 | LHSCC != ICmpInst::ICMP_UGE && LHSCC != ICmpInst::ICMP_ULE && |
| 3923 | RHSCC != ICmpInst::ICMP_UGE && RHSCC != ICmpInst::ICMP_ULE && |
| 3924 | LHSCC != ICmpInst::ICMP_SGE && LHSCC != ICmpInst::ICMP_SLE && |
Chris Lattner | 8885887 | 2007-05-11 05:55:56 +0000 | [diff] [blame] | 3925 | RHSCC != ICmpInst::ICMP_SGE && RHSCC != ICmpInst::ICMP_SLE && |
| 3926 | // We can't fold (ugt x, C) | (sgt x, C2). |
| 3927 | PredicatesFoldable(LHSCC, RHSCC)) { |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3928 | // Ensure that the larger constant is on the RHS. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3929 | ICmpInst *LHS = cast<ICmpInst>(Op0); |
Chris Lattner | 8885887 | 2007-05-11 05:55:56 +0000 | [diff] [blame] | 3930 | bool NeedsSwap; |
| 3931 | if (ICmpInst::isSignedPredicate(LHSCC)) |
Chris Lattner | 3aea1bd | 2007-05-11 16:58:45 +0000 | [diff] [blame] | 3932 | NeedsSwap = LHSCst->getValue().sgt(RHSCst->getValue()); |
Chris Lattner | 8885887 | 2007-05-11 05:55:56 +0000 | [diff] [blame] | 3933 | else |
Chris Lattner | 3aea1bd | 2007-05-11 16:58:45 +0000 | [diff] [blame] | 3934 | NeedsSwap = LHSCst->getValue().ugt(RHSCst->getValue()); |
Chris Lattner | 8885887 | 2007-05-11 05:55:56 +0000 | [diff] [blame] | 3935 | |
| 3936 | if (NeedsSwap) { |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3937 | std::swap(LHS, RHS); |
| 3938 | std::swap(LHSCst, RHSCst); |
| 3939 | std::swap(LHSCC, RHSCC); |
| 3940 | } |
| 3941 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3942 | // At this point, we know we have have two icmp instructions |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3943 | // comparing a value against two constants and or'ing the result |
| 3944 | // together. Because of the above check, we know that we only have |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3945 | // ICMP_EQ, ICMP_NE, ICMP_LT, and ICMP_GT here. We also know (from the |
| 3946 | // FoldICmpLogical check above), that the two constants are not |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3947 | // equal. |
| 3948 | assert(LHSCst != RHSCst && "Compares not folded above?"); |
| 3949 | |
| 3950 | switch (LHSCC) { |
| 3951 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3952 | case ICmpInst::ICMP_EQ: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +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: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3956 | if (LHSCst == SubOne(RHSCst)) {// (X == 13 | X == 14) -> X-13 <u 2 |
| 3957 | Constant *AddCST = ConstantExpr::getNeg(LHSCst); |
| 3958 | Instruction *Add = BinaryOperator::createAdd(LHSVal, AddCST, |
| 3959 | LHSVal->getName()+".off"); |
| 3960 | InsertNewInstBefore(Add, I); |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 3961 | AddCST = Subtract(AddOne(RHSCst), LHSCst); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3962 | return new ICmpInst(ICmpInst::ICMP_ULT, Add, AddCST); |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3963 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3964 | break; // (X == 13 | X == 15) -> no change |
| 3965 | case ICmpInst::ICMP_UGT: // (X == 13 | X u> 14) -> no change |
| 3966 | case ICmpInst::ICMP_SGT: // (X == 13 | X s> 14) -> no change |
Chris Lattner | 240d6f4 | 2005-04-19 06:04:18 +0000 | [diff] [blame] | 3967 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3968 | case ICmpInst::ICMP_NE: // (X == 13 | X != 15) -> X != 15 |
| 3969 | case ICmpInst::ICMP_ULT: // (X == 13 | X u< 15) -> X u< 15 |
| 3970 | case ICmpInst::ICMP_SLT: // (X == 13 | X s< 15) -> X s< 15 |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3971 | return ReplaceInstUsesWith(I, RHS); |
| 3972 | } |
| 3973 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3974 | case ICmpInst::ICMP_NE: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3975 | switch (RHSCC) { |
| 3976 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3977 | case ICmpInst::ICMP_EQ: // (X != 13 | X == 15) -> X != 13 |
| 3978 | case ICmpInst::ICMP_UGT: // (X != 13 | X u> 15) -> X != 13 |
| 3979 | case ICmpInst::ICMP_SGT: // (X != 13 | X s> 15) -> X != 13 |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3980 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3981 | case ICmpInst::ICMP_NE: // (X != 13 | X != 15) -> true |
| 3982 | case ICmpInst::ICMP_ULT: // (X != 13 | X u< 15) -> true |
| 3983 | case ICmpInst::ICMP_SLT: // (X != 13 | X s< 15) -> true |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3984 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3985 | } |
| 3986 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3987 | case ICmpInst::ICMP_ULT: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3988 | switch (RHSCC) { |
| 3989 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3990 | case ICmpInst::ICMP_EQ: // (X u< 13 | X == 14) -> no change |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3991 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3992 | case ICmpInst::ICMP_UGT: // (X u< 13 | X u> 15) ->(X-13) u> 2 |
| 3993 | return InsertRangeTest(LHSVal, LHSCst, AddOne(RHSCst), false, |
| 3994 | false, I); |
| 3995 | case ICmpInst::ICMP_SGT: // (X u< 13 | X s> 15) -> no change |
| 3996 | break; |
| 3997 | case ICmpInst::ICMP_NE: // (X u< 13 | X != 15) -> X != 15 |
| 3998 | 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] | 3999 | return ReplaceInstUsesWith(I, RHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4000 | case ICmpInst::ICMP_SLT: // (X u< 13 | X s< 15) -> no change |
| 4001 | break; |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4002 | } |
| 4003 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4004 | case ICmpInst::ICMP_SLT: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4005 | switch (RHSCC) { |
| 4006 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4007 | case ICmpInst::ICMP_EQ: // (X s< 13 | X == 14) -> no change |
| 4008 | break; |
| 4009 | case ICmpInst::ICMP_SGT: // (X s< 13 | X s> 15) ->(X-13) s> 2 |
| 4010 | return InsertRangeTest(LHSVal, LHSCst, AddOne(RHSCst), true, |
| 4011 | false, I); |
| 4012 | case ICmpInst::ICMP_UGT: // (X s< 13 | X u> 15) -> no change |
| 4013 | break; |
| 4014 | case ICmpInst::ICMP_NE: // (X s< 13 | X != 15) -> X != 15 |
| 4015 | case ICmpInst::ICMP_SLT: // (X s< 13 | X s< 15) -> X s< 15 |
| 4016 | return ReplaceInstUsesWith(I, RHS); |
| 4017 | case ICmpInst::ICMP_ULT: // (X s< 13 | X u< 15) -> no change |
| 4018 | break; |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4019 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4020 | break; |
| 4021 | case ICmpInst::ICMP_UGT: |
| 4022 | switch (RHSCC) { |
| 4023 | default: assert(0 && "Unknown integer condition code!"); |
| 4024 | case ICmpInst::ICMP_EQ: // (X u> 13 | X == 15) -> X u> 13 |
| 4025 | case ICmpInst::ICMP_UGT: // (X u> 13 | X u> 15) -> X u> 13 |
| 4026 | return ReplaceInstUsesWith(I, LHS); |
| 4027 | case ICmpInst::ICMP_SGT: // (X u> 13 | X s> 15) -> no change |
| 4028 | break; |
| 4029 | case ICmpInst::ICMP_NE: // (X u> 13 | X != 15) -> true |
| 4030 | case ICmpInst::ICMP_ULT: // (X u> 13 | X u< 15) -> true |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4031 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4032 | case ICmpInst::ICMP_SLT: // (X u> 13 | X s< 15) -> no change |
| 4033 | break; |
| 4034 | } |
| 4035 | break; |
| 4036 | case ICmpInst::ICMP_SGT: |
| 4037 | switch (RHSCC) { |
| 4038 | default: assert(0 && "Unknown integer condition code!"); |
| 4039 | case ICmpInst::ICMP_EQ: // (X s> 13 | X == 15) -> X > 13 |
| 4040 | case ICmpInst::ICMP_SGT: // (X s> 13 | X s> 15) -> X > 13 |
| 4041 | return ReplaceInstUsesWith(I, LHS); |
| 4042 | case ICmpInst::ICMP_UGT: // (X s> 13 | X u> 15) -> no change |
| 4043 | break; |
| 4044 | case ICmpInst::ICMP_NE: // (X s> 13 | X != 15) -> true |
| 4045 | case ICmpInst::ICMP_SLT: // (X s> 13 | X s< 15) -> true |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4046 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4047 | case ICmpInst::ICMP_ULT: // (X s> 13 | X u< 15) -> no change |
| 4048 | break; |
| 4049 | } |
| 4050 | break; |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4051 | } |
| 4052 | } |
| 4053 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4054 | |
| 4055 | // fold (or (cast A), (cast B)) -> (cast (or A, B)) |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4056 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4057 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4058 | if (Op0C->getOpcode() == Op1C->getOpcode()) {// same cast kind ? |
| 4059 | const Type *SrcTy = Op0C->getOperand(0)->getType(); |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 4060 | if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() && |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4061 | // 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] | 4062 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), |
| 4063 | I.getType(), TD) && |
| 4064 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), |
| 4065 | I.getType(), TD)) { |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4066 | Instruction *NewOp = BinaryOperator::createOr(Op0C->getOperand(0), |
| 4067 | Op1C->getOperand(0), |
| 4068 | I.getName()); |
| 4069 | InsertNewInstBefore(NewOp, I); |
| 4070 | return CastInst::create(Op0C->getOpcode(), NewOp, I.getType()); |
| 4071 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4072 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4073 | |
Chris Lattner | e9bed7d | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 4074 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4075 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4076 | } |
| 4077 | |
Chris Lattner | c317d39 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 4078 | // XorSelf - Implements: X ^ X --> 0 |
| 4079 | struct XorSelf { |
| 4080 | Value *RHS; |
| 4081 | XorSelf(Value *rhs) : RHS(rhs) {} |
| 4082 | bool shouldApply(Value *LHS) const { return LHS == RHS; } |
| 4083 | Instruction *apply(BinaryOperator &Xor) const { |
| 4084 | return &Xor; |
| 4085 | } |
| 4086 | }; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4087 | |
| 4088 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4089 | Instruction *InstCombiner::visitXor(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 4090 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4091 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4092 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4093 | if (isa<UndefValue>(Op1)) |
| 4094 | return ReplaceInstUsesWith(I, Op1); // X ^ undef -> undef |
| 4095 | |
Chris Lattner | c317d39 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 4096 | // xor X, X = 0, even if X is nested in a sequence of Xor's. |
| 4097 | if (Instruction *Result = AssociativeOpt(I, XorSelf(Op1))) { |
| 4098 | assert(Result == &I && "AssociativeOpt didn't work?"); |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 4099 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | c317d39 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 4100 | } |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 4101 | |
| 4102 | // See if we can simplify any instructions used by the instruction whose sole |
| 4103 | // purpose is to compute bits we don't care about. |
Reid Spencer | a03d45f | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 4104 | if (!isa<VectorType>(I.getType())) { |
| 4105 | uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth(); |
| 4106 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 4107 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth), |
| 4108 | KnownZero, KnownOne)) |
| 4109 | return &I; |
| 4110 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4111 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4112 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4113 | // xor (icmp A, B), true = not (icmp A, B) = !icmp A, B |
| 4114 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(Op0)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4115 | if (RHS == ConstantInt::getTrue() && ICI->hasOneUse()) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4116 | return new ICmpInst(ICI->getInversePredicate(), |
| 4117 | ICI->getOperand(0), ICI->getOperand(1)); |
Chris Lattner | ad5b4fb | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 4118 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4119 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) { |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 4120 | // ~(c-X) == X-c-1 == X+(-c-1) |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4121 | if (Op0I->getOpcode() == Instruction::Sub && RHS->isAllOnesValue()) |
| 4122 | if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4123 | Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C); |
| 4124 | Constant *ConstantRHS = ConstantExpr::getSub(NegOp0I0C, |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4125 | ConstantInt::get(I.getType(), 1)); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4126 | return BinaryOperator::createAdd(Op0I->getOperand(1), ConstantRHS); |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4127 | } |
Chris Lattner | 5b62aa7 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 4128 | |
| 4129 | // ~(~X & Y) --> (X | ~Y) |
| 4130 | if (Op0I->getOpcode() == Instruction::And && RHS->isAllOnesValue()) { |
| 4131 | if (dyn_castNotVal(Op0I->getOperand(1))) Op0I->swapOperands(); |
| 4132 | if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0))) { |
| 4133 | Instruction *NotY = |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4134 | BinaryOperator::createNot(Op0I->getOperand(1), |
Chris Lattner | 5b62aa7 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 4135 | Op0I->getOperand(1)->getName()+".not"); |
| 4136 | InsertNewInstBefore(NotY, I); |
| 4137 | return BinaryOperator::createOr(Op0NotVal, NotY); |
| 4138 | } |
| 4139 | } |
Chris Lattner | 5c6e2db | 2007-04-02 05:36:22 +0000 | [diff] [blame] | 4140 | |
Chris Lattner | eca0c5c | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 4141 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 4142 | if (Op0I->getOpcode() == Instruction::Add) { |
Chris Lattner | 689d24b | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 4143 | // ~(X-c) --> (-c-1)-X |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4144 | if (RHS->isAllOnesValue()) { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4145 | Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI); |
| 4146 | return BinaryOperator::createSub( |
| 4147 | ConstantExpr::getSub(NegOp0CI, |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4148 | ConstantInt::get(I.getType(), 1)), |
Chris Lattner | 689d24b | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 4149 | Op0I->getOperand(0)); |
Chris Lattner | acf4e07 | 2007-04-02 05:42:22 +0000 | [diff] [blame] | 4150 | } else if (RHS->getValue().isSignBit()) { |
Chris Lattner | 5c6e2db | 2007-04-02 05:36:22 +0000 | [diff] [blame] | 4151 | // (X + C) ^ signbit -> (X + C + signbit) |
| 4152 | Constant *C = ConstantInt::get(RHS->getValue() + Op0CI->getValue()); |
| 4153 | return BinaryOperator::createAdd(Op0I->getOperand(0), C); |
Chris Lattner | cd1d6d5 | 2007-04-02 05:48:58 +0000 | [diff] [blame] | 4154 | |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4155 | } |
Chris Lattner | 02bd1b3 | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 4156 | } else if (Op0I->getOpcode() == Instruction::Or) { |
| 4157 | // (X|C1)^C2 -> X^(C1|C2) iff X&~C1 == 0 |
Reid Spencer | a03d45f | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 4158 | if (MaskedValueIsZero(Op0I->getOperand(0), Op0CI->getValue())) { |
Chris Lattner | 02bd1b3 | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 4159 | Constant *NewRHS = ConstantExpr::getOr(Op0CI, RHS); |
| 4160 | // Anything in both C1 and C2 is known to be zero, remove it from |
| 4161 | // NewRHS. |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 4162 | Constant *CommonBits = And(Op0CI, RHS); |
Chris Lattner | 02bd1b3 | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 4163 | NewRHS = ConstantExpr::getAnd(NewRHS, |
| 4164 | ConstantExpr::getNot(CommonBits)); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 4165 | AddToWorkList(Op0I); |
Chris Lattner | 02bd1b3 | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 4166 | I.setOperand(0, Op0I->getOperand(0)); |
| 4167 | I.setOperand(1, NewRHS); |
| 4168 | return &I; |
| 4169 | } |
Chris Lattner | eca0c5c | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 4170 | } |
Chris Lattner | 05bd1b2 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 4171 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 4172 | |
| 4173 | // Try to fold constant and into select arguments. |
| 4174 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 4175 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 4176 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 4177 | if (isa<PHINode>(Op0)) |
| 4178 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 4179 | return NV; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4180 | } |
| 4181 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 4182 | if (Value *X = dyn_castNotVal(Op0)) // ~A ^ A == -1 |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4183 | if (X == Op1) |
| 4184 | return ReplaceInstUsesWith(I, |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4185 | ConstantInt::getAllOnesValue(I.getType())); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4186 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 4187 | if (Value *X = dyn_castNotVal(Op1)) // A ^ ~A == -1 |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4188 | if (X == Op0) |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4189 | return ReplaceInstUsesWith(I, ConstantInt::getAllOnesValue(I.getType())); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4190 | |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4191 | |
| 4192 | BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1); |
| 4193 | if (Op1I) { |
| 4194 | Value *A, *B; |
| 4195 | if (match(Op1I, m_Or(m_Value(A), m_Value(B)))) { |
| 4196 | if (A == Op0) { // B^(B|A) == (A|B)^B |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4197 | Op1I->swapOperands(); |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4198 | I.swapOperands(); |
| 4199 | std::swap(Op0, Op1); |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4200 | } else if (B == Op0) { // B^(A|B) == (A|B)^B |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4201 | I.swapOperands(); // Simplified below. |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4202 | std::swap(Op0, Op1); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4203 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4204 | } else if (match(Op1I, m_Xor(m_Value(A), m_Value(B)))) { |
| 4205 | if (Op0 == A) // A^(A^B) == B |
| 4206 | return ReplaceInstUsesWith(I, B); |
| 4207 | else if (Op0 == B) // A^(B^A) == B |
| 4208 | return ReplaceInstUsesWith(I, A); |
| 4209 | } 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] | 4210 | if (A == Op0) { // A^(A&B) -> A^(B&A) |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4211 | Op1I->swapOperands(); |
Chris Lattner | 6abbdf9 | 2007-04-01 05:36:37 +0000 | [diff] [blame] | 4212 | std::swap(A, B); |
| 4213 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4214 | if (B == Op0) { // A^(B&A) -> (B&A)^A |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4215 | I.swapOperands(); // Simplified below. |
| 4216 | std::swap(Op0, Op1); |
| 4217 | } |
Chris Lattner | 26ca7e1 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 4218 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4219 | } |
| 4220 | |
| 4221 | BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0); |
| 4222 | if (Op0I) { |
| 4223 | Value *A, *B; |
| 4224 | if (match(Op0I, m_Or(m_Value(A), m_Value(B))) && Op0I->hasOneUse()) { |
| 4225 | if (A == Op1) // (B|A)^B == (A|B)^B |
| 4226 | std::swap(A, B); |
| 4227 | if (B == Op1) { // (A|B)^B == A & ~B |
| 4228 | Instruction *NotB = |
| 4229 | InsertNewInstBefore(BinaryOperator::createNot(Op1, "tmp"), I); |
| 4230 | return BinaryOperator::createAnd(A, NotB); |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4231 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4232 | } else if (match(Op0I, m_Xor(m_Value(A), m_Value(B)))) { |
| 4233 | if (Op1 == A) // (A^B)^A == B |
| 4234 | return ReplaceInstUsesWith(I, B); |
| 4235 | else if (Op1 == B) // (B^A)^A == B |
| 4236 | return ReplaceInstUsesWith(I, A); |
| 4237 | } else if (match(Op0I, m_And(m_Value(A), m_Value(B))) && Op0I->hasOneUse()){ |
| 4238 | if (A == Op1) // (A&B)^A -> (B&A)^A |
| 4239 | std::swap(A, B); |
| 4240 | if (B == Op1 && // (B&A)^A == ~B & A |
Chris Lattner | ae1ab39 | 2006-04-01 22:05:01 +0000 | [diff] [blame] | 4241 | !isa<ConstantInt>(Op1)) { // Canonical form is (B&C)^C |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4242 | Instruction *N = |
| 4243 | InsertNewInstBefore(BinaryOperator::createNot(A, "tmp"), I); |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4244 | return BinaryOperator::createAnd(N, Op1); |
| 4245 | } |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4246 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4247 | } |
| 4248 | |
| 4249 | // (X >> Z) ^ (Y >> Z) -> (X^Y) >> Z for all shifts. |
| 4250 | if (Op0I && Op1I && Op0I->isShift() && |
| 4251 | Op0I->getOpcode() == Op1I->getOpcode() && |
| 4252 | Op0I->getOperand(1) == Op1I->getOperand(1) && |
| 4253 | (Op1I->hasOneUse() || Op1I->hasOneUse())) { |
| 4254 | Instruction *NewOp = |
| 4255 | InsertNewInstBefore(BinaryOperator::createXor(Op0I->getOperand(0), |
| 4256 | Op1I->getOperand(0), |
| 4257 | Op0I->getName()), I); |
| 4258 | return BinaryOperator::create(Op1I->getOpcode(), NewOp, |
| 4259 | Op1I->getOperand(1)); |
| 4260 | } |
| 4261 | |
| 4262 | if (Op0I && Op1I) { |
| 4263 | Value *A, *B, *C, *D; |
| 4264 | // (A & B)^(A | B) -> A ^ B |
| 4265 | if (match(Op0I, m_And(m_Value(A), m_Value(B))) && |
| 4266 | match(Op1I, m_Or(m_Value(C), m_Value(D)))) { |
| 4267 | if ((A == C && B == D) || (A == D && B == C)) |
| 4268 | return BinaryOperator::createXor(A, B); |
| 4269 | } |
| 4270 | // (A | B)^(A & B) -> A ^ B |
| 4271 | if (match(Op0I, m_Or(m_Value(A), m_Value(B))) && |
| 4272 | match(Op1I, m_And(m_Value(C), m_Value(D)))) { |
| 4273 | if ((A == C && B == D) || (A == D && B == C)) |
| 4274 | return BinaryOperator::createXor(A, B); |
| 4275 | } |
| 4276 | |
| 4277 | // (A & B)^(C & D) |
| 4278 | if ((Op0I->hasOneUse() || Op1I->hasOneUse()) && |
| 4279 | match(Op0I, m_And(m_Value(A), m_Value(B))) && |
| 4280 | match(Op1I, m_And(m_Value(C), m_Value(D)))) { |
| 4281 | // (X & Y)^(X & Y) -> (Y^Z) & X |
| 4282 | Value *X = 0, *Y = 0, *Z = 0; |
| 4283 | if (A == C) |
| 4284 | X = A, Y = B, Z = D; |
| 4285 | else if (A == D) |
| 4286 | X = A, Y = B, Z = C; |
| 4287 | else if (B == C) |
| 4288 | X = B, Y = A, Z = D; |
| 4289 | else if (B == D) |
| 4290 | X = B, Y = A, Z = C; |
| 4291 | |
| 4292 | if (X) { |
| 4293 | Instruction *NewOp = |
| 4294 | InsertNewInstBefore(BinaryOperator::createXor(Y, Z, Op0->getName()), I); |
| 4295 | return BinaryOperator::createAnd(NewOp, X); |
| 4296 | } |
| 4297 | } |
| 4298 | } |
| 4299 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4300 | // (icmp1 A, B) ^ (icmp2 A, B) --> (icmp3 A, B) |
| 4301 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1))) |
| 4302 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS))) |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 4303 | return R; |
| 4304 | |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4305 | // fold (xor (cast A), (cast B)) -> (cast (xor A, B)) |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4306 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4307 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4308 | if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind? |
| 4309 | const Type *SrcTy = Op0C->getOperand(0)->getType(); |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 4310 | if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() && |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4311 | // 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] | 4312 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), |
| 4313 | I.getType(), TD) && |
| 4314 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), |
| 4315 | I.getType(), TD)) { |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4316 | Instruction *NewOp = BinaryOperator::createXor(Op0C->getOperand(0), |
| 4317 | Op1C->getOperand(0), |
| 4318 | I.getName()); |
| 4319 | InsertNewInstBefore(NewOp, I); |
| 4320 | return CastInst::create(Op0C->getOpcode(), NewOp, I.getType()); |
| 4321 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4322 | } |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4323 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4324 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4325 | } |
| 4326 | |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4327 | /// AddWithOverflow - Compute Result = In1+In2, returning true if the result |
| 4328 | /// overflowed for this type. |
| 4329 | static bool AddWithOverflow(ConstantInt *&Result, ConstantInt *In1, |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 4330 | ConstantInt *In2, bool IsSigned = false) { |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 4331 | Result = cast<ConstantInt>(Add(In1, In2)); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4332 | |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 4333 | if (IsSigned) |
| 4334 | if (In2->getValue().isNegative()) |
| 4335 | return Result->getValue().sgt(In1->getValue()); |
| 4336 | else |
| 4337 | return Result->getValue().slt(In1->getValue()); |
| 4338 | else |
| 4339 | return Result->getValue().ult(In1->getValue()); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4340 | } |
| 4341 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4342 | /// EmitGEPOffset - Given a getelementptr instruction/constantexpr, emit the |
| 4343 | /// code necessary to compute the offset from the base pointer (without adding |
| 4344 | /// in the base pointer). Return the result as a signed integer of intptr size. |
| 4345 | static Value *EmitGEPOffset(User *GEP, Instruction &I, InstCombiner &IC) { |
| 4346 | TargetData &TD = IC.getTargetData(); |
| 4347 | gep_type_iterator GTI = gep_type_begin(GEP); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4348 | const Type *IntPtrTy = TD.getIntPtrType(); |
| 4349 | Value *Result = Constant::getNullValue(IntPtrTy); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4350 | |
| 4351 | // Build a mask for high order bits. |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4352 | unsigned IntPtrWidth = TD.getPointerSize()*8; |
| 4353 | uint64_t PtrSizeMask = ~0ULL >> (64-IntPtrWidth); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4354 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4355 | for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i, ++GTI) { |
| 4356 | Value *Op = GEP->getOperand(i); |
Chris Lattner | 0b84c80 | 2005-01-13 23:26:48 +0000 | [diff] [blame] | 4357 | uint64_t Size = TD.getTypeSize(GTI.getIndexedType()) & PtrSizeMask; |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4358 | if (ConstantInt *OpC = dyn_cast<ConstantInt>(Op)) { |
| 4359 | if (OpC->isZero()) continue; |
| 4360 | |
| 4361 | // Handle a struct index, which adds its field offset to the pointer. |
| 4362 | if (const StructType *STy = dyn_cast<StructType>(*GTI)) { |
| 4363 | Size = TD.getStructLayout(STy)->getElementOffset(OpC->getZExtValue()); |
| 4364 | |
| 4365 | if (ConstantInt *RC = dyn_cast<ConstantInt>(Result)) |
| 4366 | Result = ConstantInt::get(RC->getValue() + APInt(IntPtrWidth, Size)); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 4367 | else |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4368 | Result = IC.InsertNewInstBefore( |
| 4369 | BinaryOperator::createAdd(Result, |
| 4370 | ConstantInt::get(IntPtrTy, Size), |
| 4371 | GEP->getName()+".offs"), I); |
| 4372 | continue; |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 4373 | } |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4374 | |
| 4375 | Constant *Scale = ConstantInt::get(IntPtrTy, Size); |
| 4376 | Constant *OC = ConstantExpr::getIntegerCast(OpC, IntPtrTy, true /*SExt*/); |
| 4377 | Scale = ConstantExpr::getMul(OC, Scale); |
| 4378 | if (Constant *RC = dyn_cast<Constant>(Result)) |
| 4379 | Result = ConstantExpr::getAdd(RC, Scale); |
| 4380 | else { |
| 4381 | // Emit an add instruction. |
| 4382 | Result = IC.InsertNewInstBefore( |
| 4383 | BinaryOperator::createAdd(Result, Scale, |
| 4384 | GEP->getName()+".offs"), I); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 4385 | } |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4386 | continue; |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4387 | } |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4388 | // Convert to correct type. |
| 4389 | if (Op->getType() != IntPtrTy) { |
| 4390 | if (Constant *OpC = dyn_cast<Constant>(Op)) |
| 4391 | Op = ConstantExpr::getSExt(OpC, IntPtrTy); |
| 4392 | else |
| 4393 | Op = IC.InsertNewInstBefore(new SExtInst(Op, IntPtrTy, |
| 4394 | Op->getName()+".c"), I); |
| 4395 | } |
| 4396 | if (Size != 1) { |
| 4397 | Constant *Scale = ConstantInt::get(IntPtrTy, Size); |
| 4398 | if (Constant *OpC = dyn_cast<Constant>(Op)) |
| 4399 | Op = ConstantExpr::getMul(OpC, Scale); |
| 4400 | else // We'll let instcombine(mul) convert this to a shl if possible. |
| 4401 | Op = IC.InsertNewInstBefore(BinaryOperator::createMul(Op, Scale, |
| 4402 | GEP->getName()+".idx"), I); |
| 4403 | } |
| 4404 | |
| 4405 | // Emit an add instruction. |
| 4406 | if (isa<Constant>(Op) && isa<Constant>(Result)) |
| 4407 | Result = ConstantExpr::getAdd(cast<Constant>(Op), |
| 4408 | cast<Constant>(Result)); |
| 4409 | else |
| 4410 | Result = IC.InsertNewInstBefore(BinaryOperator::createAdd(Op, Result, |
| 4411 | GEP->getName()+".offs"), I); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4412 | } |
| 4413 | return Result; |
| 4414 | } |
| 4415 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4416 | /// FoldGEPICmp - Fold comparisons between a GEP instruction and something |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4417 | /// 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] | 4418 | Instruction *InstCombiner::FoldGEPICmp(User *GEPLHS, Value *RHS, |
| 4419 | ICmpInst::Predicate Cond, |
| 4420 | Instruction &I) { |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4421 | assert(dyn_castGetElementPtr(GEPLHS) && "LHS is not a getelementptr!"); |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4422 | |
| 4423 | if (CastInst *CI = dyn_cast<CastInst>(RHS)) |
| 4424 | if (isa<PointerType>(CI->getOperand(0)->getType())) |
| 4425 | RHS = CI->getOperand(0); |
| 4426 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4427 | Value *PtrBase = GEPLHS->getOperand(0); |
| 4428 | if (PtrBase == RHS) { |
| 4429 | // As an optimization, we don't actually have to compute the actual value of |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4430 | // OFFSET if this is a icmp_eq or icmp_ne comparison, just return whether |
| 4431 | // each index is zero or not. |
| 4432 | if (Cond == ICmpInst::ICMP_EQ || Cond == ICmpInst::ICMP_NE) { |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4433 | Instruction *InVal = 0; |
Chris Lattner | ad5fec1 | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 4434 | gep_type_iterator GTI = gep_type_begin(GEPLHS); |
| 4435 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i, ++GTI) { |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4436 | bool EmitIt = true; |
| 4437 | if (Constant *C = dyn_cast<Constant>(GEPLHS->getOperand(i))) { |
| 4438 | if (isa<UndefValue>(C)) // undef index -> undef. |
| 4439 | return ReplaceInstUsesWith(I, UndefValue::get(I.getType())); |
| 4440 | if (C->isNullValue()) |
| 4441 | EmitIt = false; |
Chris Lattner | ad5fec1 | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 4442 | else if (TD->getTypeSize(GTI.getIndexedType()) == 0) { |
| 4443 | EmitIt = false; // This is indexing into a zero sized array? |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4444 | } else if (isa<ConstantInt>(C)) |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4445 | return ReplaceInstUsesWith(I, // No comparison is needed here. |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4446 | ConstantInt::get(Type::Int1Ty, |
| 4447 | Cond == ICmpInst::ICMP_NE)); |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4448 | } |
| 4449 | |
| 4450 | if (EmitIt) { |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4451 | Instruction *Comp = |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4452 | new ICmpInst(Cond, GEPLHS->getOperand(i), |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4453 | Constant::getNullValue(GEPLHS->getOperand(i)->getType())); |
| 4454 | if (InVal == 0) |
| 4455 | InVal = Comp; |
| 4456 | else { |
| 4457 | InVal = InsertNewInstBefore(InVal, I); |
| 4458 | InsertNewInstBefore(Comp, I); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4459 | if (Cond == ICmpInst::ICMP_NE) // True if any are unequal |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4460 | InVal = BinaryOperator::createOr(InVal, Comp); |
| 4461 | else // True if all are equal |
| 4462 | InVal = BinaryOperator::createAnd(InVal, Comp); |
| 4463 | } |
| 4464 | } |
| 4465 | } |
| 4466 | |
| 4467 | if (InVal) |
| 4468 | return InVal; |
| 4469 | else |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4470 | // No comparison is needed here, all indexes = 0 |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4471 | ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
| 4472 | Cond == ICmpInst::ICMP_EQ)); |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4473 | } |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4474 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4475 | // 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] | 4476 | // the result to fold to a constant! |
| 4477 | if (isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) { |
| 4478 | // ((gep Ptr, OFFSET) cmp Ptr) ---> (OFFSET cmp 0). |
| 4479 | Value *Offset = EmitGEPOffset(GEPLHS, I, *this); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4480 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), Offset, |
| 4481 | Constant::getNullValue(Offset->getType())); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4482 | } |
| 4483 | } else if (User *GEPRHS = dyn_castGetElementPtr(RHS)) { |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 4484 | // If the base pointers are different, but the indices are the same, just |
| 4485 | // compare the base pointer. |
| 4486 | if (PtrBase != GEPRHS->getOperand(0)) { |
| 4487 | bool IndicesTheSame = GEPLHS->getNumOperands()==GEPRHS->getNumOperands(); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 4488 | IndicesTheSame &= GEPLHS->getOperand(0)->getType() == |
Chris Lattner | 93b94a6 | 2005-04-26 14:40:41 +0000 | [diff] [blame] | 4489 | GEPRHS->getOperand(0)->getType(); |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 4490 | if (IndicesTheSame) |
| 4491 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i) |
| 4492 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { |
| 4493 | IndicesTheSame = false; |
| 4494 | break; |
| 4495 | } |
| 4496 | |
| 4497 | // If all indices are the same, just compare the base pointers. |
| 4498 | if (IndicesTheSame) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4499 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), |
| 4500 | GEPLHS->getOperand(0), GEPRHS->getOperand(0)); |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 4501 | |
| 4502 | // Otherwise, the base pointers are different and the indices are |
| 4503 | // different, bail out. |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4504 | return 0; |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 4505 | } |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4506 | |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4507 | // If one of the GEPs has all zero indices, recurse. |
| 4508 | bool AllZeros = true; |
| 4509 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i) |
| 4510 | if (!isa<Constant>(GEPLHS->getOperand(i)) || |
| 4511 | !cast<Constant>(GEPLHS->getOperand(i))->isNullValue()) { |
| 4512 | AllZeros = false; |
| 4513 | break; |
| 4514 | } |
| 4515 | if (AllZeros) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4516 | return FoldGEPICmp(GEPRHS, GEPLHS->getOperand(0), |
| 4517 | ICmpInst::getSwappedPredicate(Cond), I); |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 4518 | |
| 4519 | // If the other GEP has all zero indices, recurse. |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4520 | AllZeros = true; |
| 4521 | for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i) |
| 4522 | if (!isa<Constant>(GEPRHS->getOperand(i)) || |
| 4523 | !cast<Constant>(GEPRHS->getOperand(i))->isNullValue()) { |
| 4524 | AllZeros = false; |
| 4525 | break; |
| 4526 | } |
| 4527 | if (AllZeros) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4528 | return FoldGEPICmp(GEPLHS, GEPRHS->getOperand(0), Cond, I); |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4529 | |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 4530 | if (GEPLHS->getNumOperands() == GEPRHS->getNumOperands()) { |
| 4531 | // If the GEPs only differ by one index, compare it. |
| 4532 | unsigned NumDifferences = 0; // Keep track of # differences. |
| 4533 | unsigned DiffOperand = 0; // The operand that differs. |
| 4534 | for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i) |
| 4535 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4536 | if (GEPLHS->getOperand(i)->getType()->getPrimitiveSizeInBits() != |
| 4537 | GEPRHS->getOperand(i)->getType()->getPrimitiveSizeInBits()) { |
Chris Lattner | 45f57b8 | 2005-01-21 23:06:49 +0000 | [diff] [blame] | 4538 | // Irreconcilable differences. |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 4539 | NumDifferences = 2; |
| 4540 | break; |
| 4541 | } else { |
| 4542 | if (NumDifferences++) break; |
| 4543 | DiffOperand = i; |
| 4544 | } |
| 4545 | } |
| 4546 | |
| 4547 | if (NumDifferences == 0) // SAME GEP? |
| 4548 | return ReplaceInstUsesWith(I, // No comparison is needed here. |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4549 | ConstantInt::get(Type::Int1Ty, |
| 4550 | Cond == ICmpInst::ICMP_EQ)); |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 4551 | else if (NumDifferences == 1) { |
Chris Lattner | 45f57b8 | 2005-01-21 23:06:49 +0000 | [diff] [blame] | 4552 | Value *LHSV = GEPLHS->getOperand(DiffOperand); |
| 4553 | Value *RHSV = GEPRHS->getOperand(DiffOperand); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4554 | // Make sure we do a signed comparison here. |
| 4555 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), LHSV, RHSV); |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 4556 | } |
| 4557 | } |
| 4558 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4559 | // 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] | 4560 | // the result to fold to a constant! |
| 4561 | if ((isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) && |
| 4562 | (isa<ConstantExpr>(GEPRHS) || GEPRHS->hasOneUse())) { |
| 4563 | // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) ---> (OFFSET1 cmp OFFSET2) |
| 4564 | Value *L = EmitGEPOffset(GEPLHS, I, *this); |
| 4565 | Value *R = EmitGEPOffset(GEPRHS, I, *this); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4566 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), L, R); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4567 | } |
| 4568 | } |
| 4569 | return 0; |
| 4570 | } |
| 4571 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4572 | Instruction *InstCombiner::visitFCmpInst(FCmpInst &I) { |
| 4573 | bool Changed = SimplifyCompare(I); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4574 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4575 | |
Chris Lattner | 58e9746 | 2007-01-14 19:42:17 +0000 | [diff] [blame] | 4576 | // Fold trivial predicates. |
| 4577 | if (I.getPredicate() == FCmpInst::FCMP_FALSE) |
| 4578 | return ReplaceInstUsesWith(I, Constant::getNullValue(Type::Int1Ty)); |
| 4579 | if (I.getPredicate() == FCmpInst::FCMP_TRUE) |
| 4580 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 1)); |
| 4581 | |
| 4582 | // Simplify 'fcmp pred X, X' |
| 4583 | if (Op0 == Op1) { |
| 4584 | switch (I.getPredicate()) { |
| 4585 | default: assert(0 && "Unknown predicate!"); |
| 4586 | case FCmpInst::FCMP_UEQ: // True if unordered or equal |
| 4587 | case FCmpInst::FCMP_UGE: // True if unordered, greater than, or equal |
| 4588 | case FCmpInst::FCMP_ULE: // True if unordered, less than, or equal |
| 4589 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 1)); |
| 4590 | case FCmpInst::FCMP_OGT: // True if ordered and greater than |
| 4591 | case FCmpInst::FCMP_OLT: // True if ordered and less than |
| 4592 | case FCmpInst::FCMP_ONE: // True if ordered and operands are unequal |
| 4593 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 0)); |
| 4594 | |
| 4595 | case FCmpInst::FCMP_UNO: // True if unordered: isnan(X) | isnan(Y) |
| 4596 | case FCmpInst::FCMP_ULT: // True if unordered or less than |
| 4597 | case FCmpInst::FCMP_UGT: // True if unordered or greater than |
| 4598 | case FCmpInst::FCMP_UNE: // True if unordered or not equal |
| 4599 | // Canonicalize these to be 'fcmp uno %X, 0.0'. |
| 4600 | I.setPredicate(FCmpInst::FCMP_UNO); |
| 4601 | I.setOperand(1, Constant::getNullValue(Op0->getType())); |
| 4602 | return &I; |
| 4603 | |
| 4604 | case FCmpInst::FCMP_ORD: // True if ordered (no nans) |
| 4605 | case FCmpInst::FCMP_OEQ: // True if ordered and equal |
| 4606 | case FCmpInst::FCMP_OGE: // True if ordered and greater than or equal |
| 4607 | case FCmpInst::FCMP_OLE: // True if ordered and less than or equal |
| 4608 | // Canonicalize these to be 'fcmp ord %X, 0.0'. |
| 4609 | I.setPredicate(FCmpInst::FCMP_ORD); |
| 4610 | I.setOperand(1, Constant::getNullValue(Op0->getType())); |
| 4611 | return &I; |
| 4612 | } |
| 4613 | } |
| 4614 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4615 | if (isa<UndefValue>(Op1)) // fcmp pred X, undef -> undef |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 4616 | return ReplaceInstUsesWith(I, UndefValue::get(Type::Int1Ty)); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4617 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4618 | // Handle fcmp with constant RHS |
| 4619 | if (Constant *RHSC = dyn_cast<Constant>(Op1)) { |
| 4620 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
| 4621 | switch (LHSI->getOpcode()) { |
| 4622 | case Instruction::PHI: |
| 4623 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 4624 | return NV; |
| 4625 | break; |
| 4626 | case Instruction::Select: |
| 4627 | // If either operand of the select is a constant, we can fold the |
| 4628 | // comparison into the select arms, which will cause one to be |
| 4629 | // constant folded and the select turned into a bitwise or. |
| 4630 | Value *Op1 = 0, *Op2 = 0; |
| 4631 | if (LHSI->hasOneUse()) { |
| 4632 | if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) { |
| 4633 | // Fold the known value into the constant operand. |
| 4634 | Op1 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC); |
| 4635 | // Insert a new FCmp of the other select operand. |
| 4636 | Op2 = InsertNewInstBefore(new FCmpInst(I.getPredicate(), |
| 4637 | LHSI->getOperand(2), RHSC, |
| 4638 | I.getName()), I); |
| 4639 | } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) { |
| 4640 | // Fold the known value into the constant operand. |
| 4641 | Op2 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC); |
| 4642 | // Insert a new FCmp of the other select operand. |
| 4643 | Op1 = InsertNewInstBefore(new FCmpInst(I.getPredicate(), |
| 4644 | LHSI->getOperand(1), RHSC, |
| 4645 | I.getName()), I); |
| 4646 | } |
| 4647 | } |
| 4648 | |
| 4649 | if (Op1) |
| 4650 | return new SelectInst(LHSI->getOperand(0), Op1, Op2); |
| 4651 | break; |
| 4652 | } |
| 4653 | } |
| 4654 | |
| 4655 | return Changed ? &I : 0; |
| 4656 | } |
| 4657 | |
| 4658 | Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { |
| 4659 | bool Changed = SimplifyCompare(I); |
| 4660 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 4661 | const Type *Ty = Op0->getType(); |
| 4662 | |
| 4663 | // icmp X, X |
| 4664 | if (Op0 == Op1) |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4665 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
| 4666 | isTrueWhenEqual(I))); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4667 | |
| 4668 | if (isa<UndefValue>(Op1)) // X icmp undef -> undef |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 4669 | return ReplaceInstUsesWith(I, UndefValue::get(Type::Int1Ty)); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4670 | |
| 4671 | // icmp of GlobalValues can never equal each other as long as they aren't |
| 4672 | // external weak linkage type. |
| 4673 | if (GlobalValue *GV0 = dyn_cast<GlobalValue>(Op0)) |
| 4674 | if (GlobalValue *GV1 = dyn_cast<GlobalValue>(Op1)) |
| 4675 | if (!GV0->hasExternalWeakLinkage() || !GV1->hasExternalWeakLinkage()) |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4676 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
| 4677 | !isTrueWhenEqual(I))); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4678 | |
| 4679 | // icmp <global/alloca*/null>, <global/alloca*/null> - Global/Stack value |
Chris Lattner | 711b340 | 2004-11-14 07:33:16 +0000 | [diff] [blame] | 4680 | // addresses never equal each other! We already know that Op0 != Op1. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4681 | if ((isa<GlobalValue>(Op0) || isa<AllocaInst>(Op0) || |
| 4682 | isa<ConstantPointerNull>(Op0)) && |
| 4683 | (isa<GlobalValue>(Op1) || isa<AllocaInst>(Op1) || |
Chris Lattner | 711b340 | 2004-11-14 07:33:16 +0000 | [diff] [blame] | 4684 | isa<ConstantPointerNull>(Op1))) |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4685 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
| 4686 | !isTrueWhenEqual(I))); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4687 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4688 | // icmp's with boolean values can always be turned into bitwise operations |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 4689 | if (Ty == Type::Int1Ty) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4690 | switch (I.getPredicate()) { |
| 4691 | default: assert(0 && "Invalid icmp instruction!"); |
| 4692 | case ICmpInst::ICMP_EQ: { // icmp eq bool %A, %B -> ~(A^B) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4693 | Instruction *Xor = BinaryOperator::createXor(Op0, Op1, I.getName()+"tmp"); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4694 | InsertNewInstBefore(Xor, I); |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 4695 | return BinaryOperator::createNot(Xor); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4696 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4697 | case ICmpInst::ICMP_NE: // icmp eq bool %A, %B -> A^B |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 4698 | return BinaryOperator::createXor(Op0, Op1); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4699 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4700 | case ICmpInst::ICMP_UGT: |
| 4701 | case ICmpInst::ICMP_SGT: |
| 4702 | std::swap(Op0, Op1); // Change icmp gt -> icmp lt |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 4703 | // FALL THROUGH |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4704 | case ICmpInst::ICMP_ULT: |
| 4705 | case ICmpInst::ICMP_SLT: { // icmp lt bool A, B -> ~X & Y |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 4706 | Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp"); |
| 4707 | InsertNewInstBefore(Not, I); |
| 4708 | return BinaryOperator::createAnd(Not, Op1); |
| 4709 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4710 | case ICmpInst::ICMP_UGE: |
| 4711 | case ICmpInst::ICMP_SGE: |
| 4712 | std::swap(Op0, Op1); // Change icmp ge -> icmp le |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 4713 | // FALL THROUGH |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4714 | case ICmpInst::ICMP_ULE: |
| 4715 | case ICmpInst::ICMP_SLE: { // icmp le bool %A, %B -> ~A | B |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 4716 | Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp"); |
| 4717 | InsertNewInstBefore(Not, I); |
| 4718 | return BinaryOperator::createOr(Not, Op1); |
| 4719 | } |
| 4720 | } |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4721 | } |
| 4722 | |
Chris Lattner | 2be51ae | 2004-06-09 04:24:29 +0000 | [diff] [blame] | 4723 | // See if we are doing a comparison between a constant and an instruction that |
| 4724 | // can be folded into the comparison. |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4725 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4726 | switch (I.getPredicate()) { |
| 4727 | default: break; |
| 4728 | case ICmpInst::ICMP_ULT: // A <u MIN -> FALSE |
| 4729 | if (CI->isMinValue(false)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4730 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4731 | if (CI->isMaxValue(false)) // A <u MAX -> A != MAX |
| 4732 | return new ICmpInst(ICmpInst::ICMP_NE, Op0,Op1); |
| 4733 | if (isMinValuePlusOne(CI,false)) // A <u MIN+1 -> A == MIN |
| 4734 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, SubOne(CI)); |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 4735 | // (x <u 2147483648) -> (x >s -1) -> true if sign bit clear |
| 4736 | if (CI->isMinValue(true)) |
| 4737 | return new ICmpInst(ICmpInst::ICMP_SGT, Op0, |
| 4738 | ConstantInt::getAllOnesValue(Op0->getType())); |
| 4739 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4740 | break; |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4741 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4742 | case ICmpInst::ICMP_SLT: |
| 4743 | if (CI->isMinValue(true)) // A <s MIN -> FALSE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4744 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4745 | if (CI->isMaxValue(true)) // A <s MAX -> A != MAX |
| 4746 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
| 4747 | if (isMinValuePlusOne(CI,true)) // A <s MIN+1 -> A == MIN |
| 4748 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, SubOne(CI)); |
| 4749 | break; |
| 4750 | |
| 4751 | case ICmpInst::ICMP_UGT: |
| 4752 | if (CI->isMaxValue(false)) // A >u MAX -> FALSE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4753 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4754 | if (CI->isMinValue(false)) // A >u MIN -> A != MIN |
| 4755 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
| 4756 | if (isMaxValueMinusOne(CI, false)) // A >u MAX-1 -> A == MAX |
| 4757 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, AddOne(CI)); |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 4758 | |
| 4759 | // (x >u 2147483647) -> (x <s 0) -> true if sign bit set |
| 4760 | if (CI->isMaxValue(true)) |
| 4761 | return new ICmpInst(ICmpInst::ICMP_SLT, Op0, |
| 4762 | ConstantInt::getNullValue(Op0->getType())); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4763 | break; |
| 4764 | |
| 4765 | case ICmpInst::ICMP_SGT: |
| 4766 | if (CI->isMaxValue(true)) // A >s MAX -> FALSE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4767 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4768 | if (CI->isMinValue(true)) // A >s MIN -> A != MIN |
| 4769 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
| 4770 | if (isMaxValueMinusOne(CI, true)) // A >s MAX-1 -> A == MAX |
| 4771 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, AddOne(CI)); |
| 4772 | break; |
| 4773 | |
| 4774 | case ICmpInst::ICMP_ULE: |
| 4775 | if (CI->isMaxValue(false)) // A <=u MAX -> TRUE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4776 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4777 | if (CI->isMinValue(false)) // A <=u MIN -> A == MIN |
| 4778 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); |
| 4779 | if (isMaxValueMinusOne(CI,false)) // A <=u MAX-1 -> A != MAX |
| 4780 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, AddOne(CI)); |
| 4781 | break; |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4782 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4783 | case ICmpInst::ICMP_SLE: |
| 4784 | if (CI->isMaxValue(true)) // A <=s MAX -> TRUE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4785 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4786 | if (CI->isMinValue(true)) // A <=s MIN -> A == MIN |
| 4787 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); |
| 4788 | if (isMaxValueMinusOne(CI,true)) // A <=s MAX-1 -> A != MAX |
| 4789 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, AddOne(CI)); |
| 4790 | break; |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4791 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4792 | case ICmpInst::ICMP_UGE: |
| 4793 | if (CI->isMinValue(false)) // A >=u MIN -> TRUE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4794 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4795 | if (CI->isMaxValue(false)) // A >=u MAX -> A == MAX |
| 4796 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); |
| 4797 | if (isMinValuePlusOne(CI,false)) // A >=u MIN-1 -> A != MIN |
| 4798 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, SubOne(CI)); |
| 4799 | break; |
| 4800 | |
| 4801 | case ICmpInst::ICMP_SGE: |
| 4802 | if (CI->isMinValue(true)) // A >=s MIN -> TRUE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4803 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4804 | if (CI->isMaxValue(true)) // A >=s MAX -> A == MAX |
| 4805 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); |
| 4806 | if (isMinValuePlusOne(CI,true)) // A >=s MIN-1 -> A != MIN |
| 4807 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, SubOne(CI)); |
| 4808 | break; |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4809 | } |
| 4810 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4811 | // If we still have a icmp le or icmp ge instruction, turn it into the |
| 4812 | // appropriate icmp lt or icmp gt instruction. Since the border cases have |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4813 | // already been handled above, this requires little checking. |
| 4814 | // |
Reid Spencer | 2149a9d | 2007-03-25 19:55:33 +0000 | [diff] [blame] | 4815 | switch (I.getPredicate()) { |
| 4816 | default: break; |
| 4817 | case ICmpInst::ICMP_ULE: |
| 4818 | return new ICmpInst(ICmpInst::ICMP_ULT, Op0, AddOne(CI)); |
| 4819 | case ICmpInst::ICMP_SLE: |
| 4820 | return new ICmpInst(ICmpInst::ICMP_SLT, Op0, AddOne(CI)); |
| 4821 | case ICmpInst::ICMP_UGE: |
| 4822 | return new ICmpInst( ICmpInst::ICMP_UGT, Op0, SubOne(CI)); |
| 4823 | case ICmpInst::ICMP_SGE: |
| 4824 | return new ICmpInst(ICmpInst::ICMP_SGT, Op0, SubOne(CI)); |
| 4825 | } |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 4826 | |
| 4827 | // See if we can fold the comparison based on bits known to be zero or one |
| 4828 | // in the input. |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 4829 | uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth(); |
| 4830 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 4831 | if (SimplifyDemandedBits(Op0, APInt::getAllOnesValue(BitWidth), |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 4832 | KnownZero, KnownOne, 0)) |
| 4833 | return &I; |
| 4834 | |
| 4835 | // Given the known and unknown bits, compute a range that the LHS could be |
| 4836 | // in. |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 4837 | if ((KnownOne | KnownZero) != 0) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4838 | // Compute the Min, Max and RHS values based on the known bits. For the |
| 4839 | // EQ and NE we use unsigned values. |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 4840 | APInt Min(BitWidth, 0), Max(BitWidth, 0); |
| 4841 | const APInt& RHSVal = CI->getValue(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4842 | if (ICmpInst::isSignedPredicate(I.getPredicate())) { |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 4843 | ComputeSignedMinMaxValuesFromKnownBits(Ty, KnownZero, KnownOne, Min, |
| 4844 | Max); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4845 | } else { |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 4846 | ComputeUnsignedMinMaxValuesFromKnownBits(Ty, KnownZero, KnownOne, Min, |
| 4847 | Max); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4848 | } |
| 4849 | switch (I.getPredicate()) { // LE/GE have been folded already. |
| 4850 | default: assert(0 && "Unknown icmp opcode!"); |
| 4851 | case ICmpInst::ICMP_EQ: |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 4852 | if (Max.ult(RHSVal) || Min.ugt(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4853 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4854 | break; |
| 4855 | case ICmpInst::ICMP_NE: |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 4856 | if (Max.ult(RHSVal) || Min.ugt(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4857 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4858 | break; |
| 4859 | case ICmpInst::ICMP_ULT: |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 4860 | if (Max.ult(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4861 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | 81973ef | 2007-04-09 23:52:13 +0000 | [diff] [blame] | 4862 | if (Min.uge(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4863 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4864 | break; |
| 4865 | case ICmpInst::ICMP_UGT: |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 4866 | if (Min.ugt(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4867 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | 81973ef | 2007-04-09 23:52:13 +0000 | [diff] [blame] | 4868 | if (Max.ule(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4869 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4870 | break; |
| 4871 | case ICmpInst::ICMP_SLT: |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 4872 | if (Max.slt(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4873 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 4874 | if (Min.sgt(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4875 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4876 | break; |
| 4877 | case ICmpInst::ICMP_SGT: |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 4878 | if (Min.sgt(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4879 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | 81973ef | 2007-04-09 23:52:13 +0000 | [diff] [blame] | 4880 | if (Max.sle(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4881 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4882 | break; |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 4883 | } |
| 4884 | } |
| 4885 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4886 | // 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] | 4887 | // instruction, see if that instruction also has constants so that the |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4888 | // instruction can be folded into the icmp |
Chris Lattner | 3c6a0d4 | 2004-05-25 06:32:08 +0000 | [diff] [blame] | 4889 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 4890 | if (Instruction *Res = visitICmpInstWithInstAndIntCst(I, LHSI, CI)) |
| 4891 | return Res; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4892 | } |
| 4893 | |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 4894 | // Handle icmp with constant (but not simple integer constant) RHS |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 4895 | if (Constant *RHSC = dyn_cast<Constant>(Op1)) { |
| 4896 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
| 4897 | switch (LHSI->getOpcode()) { |
Chris Lattner | 9fb25db | 2005-05-01 04:42:15 +0000 | [diff] [blame] | 4898 | case Instruction::GetElementPtr: |
| 4899 | if (RHSC->isNullValue()) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4900 | // 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] | 4901 | bool isAllZeros = true; |
| 4902 | for (unsigned i = 1, e = LHSI->getNumOperands(); i != e; ++i) |
| 4903 | if (!isa<Constant>(LHSI->getOperand(i)) || |
| 4904 | !cast<Constant>(LHSI->getOperand(i))->isNullValue()) { |
| 4905 | isAllZeros = false; |
| 4906 | break; |
| 4907 | } |
| 4908 | if (isAllZeros) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4909 | return new ICmpInst(I.getPredicate(), LHSI->getOperand(0), |
Chris Lattner | 9fb25db | 2005-05-01 04:42:15 +0000 | [diff] [blame] | 4910 | Constant::getNullValue(LHSI->getOperand(0)->getType())); |
| 4911 | } |
| 4912 | break; |
| 4913 | |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 4914 | case Instruction::PHI: |
| 4915 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 4916 | return NV; |
| 4917 | break; |
Chris Lattner | 4802d90 | 2007-04-06 18:57:34 +0000 | [diff] [blame] | 4918 | case Instruction::Select: { |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 4919 | // If either operand of the select is a constant, we can fold the |
| 4920 | // comparison into the select arms, which will cause one to be |
| 4921 | // constant folded and the select turned into a bitwise or. |
| 4922 | Value *Op1 = 0, *Op2 = 0; |
| 4923 | if (LHSI->hasOneUse()) { |
| 4924 | if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) { |
| 4925 | // Fold the known value into the constant operand. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4926 | Op1 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC); |
| 4927 | // Insert a new ICmp of the other select operand. |
| 4928 | Op2 = InsertNewInstBefore(new ICmpInst(I.getPredicate(), |
| 4929 | LHSI->getOperand(2), RHSC, |
| 4930 | I.getName()), I); |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 4931 | } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) { |
| 4932 | // Fold the known value into the constant operand. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4933 | Op2 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC); |
| 4934 | // Insert a new ICmp of the other select operand. |
| 4935 | Op1 = InsertNewInstBefore(new ICmpInst(I.getPredicate(), |
| 4936 | LHSI->getOperand(1), RHSC, |
| 4937 | I.getName()), I); |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 4938 | } |
| 4939 | } |
Jeff Cohen | 9d80930 | 2005-04-23 21:38:35 +0000 | [diff] [blame] | 4940 | |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 4941 | if (Op1) |
| 4942 | return new SelectInst(LHSI->getOperand(0), Op1, Op2); |
| 4943 | break; |
| 4944 | } |
Chris Lattner | 4802d90 | 2007-04-06 18:57:34 +0000 | [diff] [blame] | 4945 | case Instruction::Malloc: |
| 4946 | // If we have (malloc != null), and if the malloc has a single use, we |
| 4947 | // can assume it is successful and remove the malloc. |
| 4948 | if (LHSI->hasOneUse() && isa<ConstantPointerNull>(RHSC)) { |
| 4949 | AddToWorkList(LHSI); |
| 4950 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
| 4951 | !isTrueWhenEqual(I))); |
| 4952 | } |
| 4953 | break; |
| 4954 | } |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 4955 | } |
| 4956 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4957 | // 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] | 4958 | if (User *GEP = dyn_castGetElementPtr(Op0)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4959 | if (Instruction *NI = FoldGEPICmp(GEP, Op1, I.getPredicate(), I)) |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4960 | return NI; |
| 4961 | if (User *GEP = dyn_castGetElementPtr(Op1)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4962 | if (Instruction *NI = FoldGEPICmp(GEP, Op0, |
| 4963 | ICmpInst::getSwappedPredicate(I.getPredicate()), I)) |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4964 | return NI; |
| 4965 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4966 | // 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] | 4967 | // values. If the ptr->ptr cast can be stripped off both arguments, we do so |
| 4968 | // now. |
| 4969 | if (BitCastInst *CI = dyn_cast<BitCastInst>(Op0)) { |
| 4970 | if (isa<PointerType>(Op0->getType()) && |
| 4971 | (isa<Constant>(Op1) || isa<BitCastInst>(Op1))) { |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 4972 | // We keep moving the cast from the left operand over to the right |
| 4973 | // operand, where it can often be eliminated completely. |
Chris Lattner | 57d8637 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 4974 | Op0 = CI->getOperand(0); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4975 | |
Chris Lattner | 57d8637 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 4976 | // If operand #1 is a bitcast instruction, it must also be a ptr->ptr cast |
| 4977 | // so eliminate it as well. |
| 4978 | if (BitCastInst *CI2 = dyn_cast<BitCastInst>(Op1)) |
| 4979 | Op1 = CI2->getOperand(0); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4980 | |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 4981 | // If Op1 is a constant, we can fold the cast into the constant. |
Chris Lattner | 57d8637 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 4982 | if (Op0->getType() != Op1->getType()) |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 4983 | if (Constant *Op1C = dyn_cast<Constant>(Op1)) { |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 4984 | Op1 = ConstantExpr::getBitCast(Op1C, Op0->getType()); |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 4985 | } else { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4986 | // Otherwise, cast the RHS right before the icmp |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 4987 | Op1 = InsertCastBefore(Instruction::BitCast, Op1, Op0->getType(), I); |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 4988 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4989 | return new ICmpInst(I.getPredicate(), Op0, Op1); |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 4990 | } |
Chris Lattner | 57d8637 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 4991 | } |
| 4992 | |
| 4993 | if (isa<CastInst>(Op0)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4994 | // Handle the special case of: icmp (cast bool to X), <cst> |
Chris Lattner | 6870805 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 4995 | // This comes up when you have code like |
| 4996 | // int X = A < B; |
| 4997 | // if (X) ... |
| 4998 | // For generality, we handle any zero-extension of any operand comparison |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4999 | // with a constant or another cast from the same type. |
| 5000 | if (isa<ConstantInt>(Op1) || isa<CastInst>(Op1)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5001 | if (Instruction *R = visitICmpInstWithCastAndCast(I)) |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5002 | return R; |
Chris Lattner | 6870805 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 5003 | } |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5004 | |
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 5005 | if (I.isEquality()) { |
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5006 | Value *A, *B, *C, *D; |
| 5007 | if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) { |
| 5008 | if (A == Op1 || B == Op1) { // (A^B) == A -> B == 0 |
| 5009 | Value *OtherVal = A == Op1 ? B : A; |
| 5010 | return new ICmpInst(I.getPredicate(), OtherVal, |
| 5011 | Constant::getNullValue(A->getType())); |
| 5012 | } |
| 5013 | |
| 5014 | if (match(Op1, m_Xor(m_Value(C), m_Value(D)))) { |
| 5015 | // A^c1 == C^c2 --> A == C^(c1^c2) |
| 5016 | if (ConstantInt *C1 = dyn_cast<ConstantInt>(B)) |
| 5017 | if (ConstantInt *C2 = dyn_cast<ConstantInt>(D)) |
| 5018 | if (Op1->hasOneUse()) { |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 5019 | Constant *NC = ConstantInt::get(C1->getValue() ^ C2->getValue()); |
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5020 | Instruction *Xor = BinaryOperator::createXor(C, NC, "tmp"); |
| 5021 | return new ICmpInst(I.getPredicate(), A, |
| 5022 | InsertNewInstBefore(Xor, I)); |
| 5023 | } |
| 5024 | |
| 5025 | // A^B == A^D -> B == D |
| 5026 | if (A == C) return new ICmpInst(I.getPredicate(), B, D); |
| 5027 | if (A == D) return new ICmpInst(I.getPredicate(), B, C); |
| 5028 | if (B == C) return new ICmpInst(I.getPredicate(), A, D); |
| 5029 | if (B == D) return new ICmpInst(I.getPredicate(), A, C); |
| 5030 | } |
| 5031 | } |
| 5032 | |
| 5033 | if (match(Op1, m_Xor(m_Value(A), m_Value(B))) && |
| 5034 | (A == Op0 || B == Op0)) { |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5035 | // A == (A^B) -> B == 0 |
| 5036 | Value *OtherVal = A == Op0 ? B : A; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5037 | return new ICmpInst(I.getPredicate(), OtherVal, |
| 5038 | Constant::getNullValue(A->getType())); |
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5039 | } |
| 5040 | 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] | 5041 | // (A-B) == A -> B == 0 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5042 | return new ICmpInst(I.getPredicate(), B, |
| 5043 | Constant::getNullValue(B->getType())); |
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5044 | } |
| 5045 | 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] | 5046 | // A == (A-B) -> B == 0 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5047 | return new ICmpInst(I.getPredicate(), B, |
| 5048 | Constant::getNullValue(B->getType())); |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5049 | } |
Chris Lattner | 9c2328e | 2006-11-14 06:06:06 +0000 | [diff] [blame] | 5050 | |
Chris Lattner | 9c2328e | 2006-11-14 06:06:06 +0000 | [diff] [blame] | 5051 | // (X&Z) == (Y&Z) -> (X^Y) & Z == 0 |
| 5052 | if (Op0->hasOneUse() && Op1->hasOneUse() && |
| 5053 | match(Op0, m_And(m_Value(A), m_Value(B))) && |
| 5054 | match(Op1, m_And(m_Value(C), m_Value(D)))) { |
| 5055 | Value *X = 0, *Y = 0, *Z = 0; |
| 5056 | |
| 5057 | if (A == C) { |
| 5058 | X = B; Y = D; Z = A; |
| 5059 | } else if (A == D) { |
| 5060 | X = B; Y = C; Z = A; |
| 5061 | } else if (B == C) { |
| 5062 | X = A; Y = D; Z = B; |
| 5063 | } else if (B == D) { |
| 5064 | X = A; Y = C; Z = B; |
| 5065 | } |
| 5066 | |
| 5067 | if (X) { // Build (X^Y) & Z |
| 5068 | Op1 = InsertNewInstBefore(BinaryOperator::createXor(X, Y, "tmp"), I); |
| 5069 | Op1 = InsertNewInstBefore(BinaryOperator::createAnd(Op1, Z, "tmp"), I); |
| 5070 | I.setOperand(0, Op1); |
| 5071 | I.setOperand(1, Constant::getNullValue(Op1->getType())); |
| 5072 | return &I; |
| 5073 | } |
| 5074 | } |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5075 | } |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 5076 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5077 | } |
| 5078 | |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5079 | /// visitICmpInstWithInstAndIntCst - Handle "icmp (instr, intcst)". |
| 5080 | /// |
| 5081 | Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI, |
| 5082 | Instruction *LHSI, |
| 5083 | ConstantInt *RHS) { |
| 5084 | const APInt &RHSV = RHS->getValue(); |
| 5085 | |
| 5086 | switch (LHSI->getOpcode()) { |
Duncan Sands | 0091bf2 | 2007-04-04 06:42:45 +0000 | [diff] [blame] | 5087 | case Instruction::Xor: // (icmp pred (xor X, XorCST), CI) |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5088 | if (ConstantInt *XorCST = dyn_cast<ConstantInt>(LHSI->getOperand(1))) { |
| 5089 | // If this is a comparison that tests the signbit (X < 0) or (x > -1), |
| 5090 | // fold the xor. |
| 5091 | if (ICI.getPredicate() == ICmpInst::ICMP_SLT && RHSV == 0 || |
| 5092 | ICI.getPredicate() == ICmpInst::ICMP_SGT && RHSV.isAllOnesValue()) { |
| 5093 | Value *CompareVal = LHSI->getOperand(0); |
| 5094 | |
| 5095 | // If the sign bit of the XorCST is not set, there is no change to |
| 5096 | // the operation, just stop using the Xor. |
| 5097 | if (!XorCST->getValue().isNegative()) { |
| 5098 | ICI.setOperand(0, CompareVal); |
| 5099 | AddToWorkList(LHSI); |
| 5100 | return &ICI; |
| 5101 | } |
| 5102 | |
| 5103 | // Was the old condition true if the operand is positive? |
| 5104 | bool isTrueIfPositive = ICI.getPredicate() == ICmpInst::ICMP_SGT; |
| 5105 | |
| 5106 | // If so, the new one isn't. |
| 5107 | isTrueIfPositive ^= true; |
| 5108 | |
| 5109 | if (isTrueIfPositive) |
| 5110 | return new ICmpInst(ICmpInst::ICMP_SGT, CompareVal, SubOne(RHS)); |
| 5111 | else |
| 5112 | return new ICmpInst(ICmpInst::ICMP_SLT, CompareVal, AddOne(RHS)); |
| 5113 | } |
| 5114 | } |
| 5115 | break; |
| 5116 | case Instruction::And: // (icmp pred (and X, AndCST), RHS) |
| 5117 | if (LHSI->hasOneUse() && isa<ConstantInt>(LHSI->getOperand(1)) && |
| 5118 | LHSI->getOperand(0)->hasOneUse()) { |
| 5119 | ConstantInt *AndCST = cast<ConstantInt>(LHSI->getOperand(1)); |
| 5120 | |
| 5121 | // If the LHS is an AND of a truncating cast, we can widen the |
| 5122 | // and/compare to be the input width without changing the value |
| 5123 | // produced, eliminating a cast. |
| 5124 | if (TruncInst *Cast = dyn_cast<TruncInst>(LHSI->getOperand(0))) { |
| 5125 | // We can do this transformation if either the AND constant does not |
| 5126 | // have its sign bit set or if it is an equality comparison. |
| 5127 | // Extending a relational comparison when we're checking the sign |
| 5128 | // bit would not work. |
| 5129 | if (Cast->hasOneUse() && |
| 5130 | (ICI.isEquality() || AndCST->getValue().isPositive() && |
| 5131 | RHSV.isPositive())) { |
| 5132 | uint32_t BitWidth = |
| 5133 | cast<IntegerType>(Cast->getOperand(0)->getType())->getBitWidth(); |
| 5134 | APInt NewCST = AndCST->getValue(); |
| 5135 | NewCST.zext(BitWidth); |
| 5136 | APInt NewCI = RHSV; |
| 5137 | NewCI.zext(BitWidth); |
| 5138 | Instruction *NewAnd = |
| 5139 | BinaryOperator::createAnd(Cast->getOperand(0), |
| 5140 | ConstantInt::get(NewCST),LHSI->getName()); |
| 5141 | InsertNewInstBefore(NewAnd, ICI); |
| 5142 | return new ICmpInst(ICI.getPredicate(), NewAnd, |
| 5143 | ConstantInt::get(NewCI)); |
| 5144 | } |
| 5145 | } |
| 5146 | |
| 5147 | // If this is: (X >> C1) & C2 != C3 (where any shift and any compare |
| 5148 | // could exist), turn it into (X & (C2 << C1)) != (C3 << C1). This |
| 5149 | // happens a LOT in code produced by the C front-end, for bitfield |
| 5150 | // access. |
| 5151 | BinaryOperator *Shift = dyn_cast<BinaryOperator>(LHSI->getOperand(0)); |
| 5152 | if (Shift && !Shift->isShift()) |
| 5153 | Shift = 0; |
| 5154 | |
| 5155 | ConstantInt *ShAmt; |
| 5156 | ShAmt = Shift ? dyn_cast<ConstantInt>(Shift->getOperand(1)) : 0; |
| 5157 | const Type *Ty = Shift ? Shift->getType() : 0; // Type of the shift. |
| 5158 | const Type *AndTy = AndCST->getType(); // Type of the and. |
| 5159 | |
| 5160 | // We can fold this as long as we can't shift unknown bits |
| 5161 | // into the mask. This can only happen with signed shift |
| 5162 | // rights, as they sign-extend. |
| 5163 | if (ShAmt) { |
| 5164 | bool CanFold = Shift->isLogicalShift(); |
| 5165 | if (!CanFold) { |
| 5166 | // To test for the bad case of the signed shr, see if any |
| 5167 | // of the bits shifted in could be tested after the mask. |
| 5168 | uint32_t TyBits = Ty->getPrimitiveSizeInBits(); |
| 5169 | int ShAmtVal = TyBits - ShAmt->getLimitedValue(TyBits); |
| 5170 | |
| 5171 | uint32_t BitWidth = AndTy->getPrimitiveSizeInBits(); |
| 5172 | if ((APInt::getHighBitsSet(BitWidth, BitWidth-ShAmtVal) & |
| 5173 | AndCST->getValue()) == 0) |
| 5174 | CanFold = true; |
| 5175 | } |
| 5176 | |
| 5177 | if (CanFold) { |
| 5178 | Constant *NewCst; |
| 5179 | if (Shift->getOpcode() == Instruction::Shl) |
| 5180 | NewCst = ConstantExpr::getLShr(RHS, ShAmt); |
| 5181 | else |
| 5182 | NewCst = ConstantExpr::getShl(RHS, ShAmt); |
| 5183 | |
| 5184 | // Check to see if we are shifting out any of the bits being |
| 5185 | // compared. |
| 5186 | if (ConstantExpr::get(Shift->getOpcode(), NewCst, ShAmt) != RHS) { |
| 5187 | // If we shifted bits out, the fold is not going to work out. |
| 5188 | // As a special case, check to see if this means that the |
| 5189 | // result is always true or false now. |
| 5190 | if (ICI.getPredicate() == ICmpInst::ICMP_EQ) |
| 5191 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
| 5192 | if (ICI.getPredicate() == ICmpInst::ICMP_NE) |
| 5193 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); |
| 5194 | } else { |
| 5195 | ICI.setOperand(1, NewCst); |
| 5196 | Constant *NewAndCST; |
| 5197 | if (Shift->getOpcode() == Instruction::Shl) |
| 5198 | NewAndCST = ConstantExpr::getLShr(AndCST, ShAmt); |
| 5199 | else |
| 5200 | NewAndCST = ConstantExpr::getShl(AndCST, ShAmt); |
| 5201 | LHSI->setOperand(1, NewAndCST); |
| 5202 | LHSI->setOperand(0, Shift->getOperand(0)); |
| 5203 | AddToWorkList(Shift); // Shift is dead. |
| 5204 | AddUsesToWorkList(ICI); |
| 5205 | return &ICI; |
| 5206 | } |
| 5207 | } |
| 5208 | } |
| 5209 | |
| 5210 | // Turn ((X >> Y) & C) == 0 into (X & (C << Y)) == 0. The later is |
| 5211 | // preferable because it allows the C<<Y expression to be hoisted out |
| 5212 | // of a loop if Y is invariant and X is not. |
| 5213 | if (Shift && Shift->hasOneUse() && RHSV == 0 && |
| 5214 | ICI.isEquality() && !Shift->isArithmeticShift() && |
| 5215 | isa<Instruction>(Shift->getOperand(0))) { |
| 5216 | // Compute C << Y. |
| 5217 | Value *NS; |
| 5218 | if (Shift->getOpcode() == Instruction::LShr) { |
| 5219 | NS = BinaryOperator::createShl(AndCST, |
| 5220 | Shift->getOperand(1), "tmp"); |
| 5221 | } else { |
| 5222 | // Insert a logical shift. |
| 5223 | NS = BinaryOperator::createLShr(AndCST, |
| 5224 | Shift->getOperand(1), "tmp"); |
| 5225 | } |
| 5226 | InsertNewInstBefore(cast<Instruction>(NS), ICI); |
| 5227 | |
| 5228 | // Compute X & (C << Y). |
| 5229 | Instruction *NewAnd = |
| 5230 | BinaryOperator::createAnd(Shift->getOperand(0), NS, LHSI->getName()); |
| 5231 | InsertNewInstBefore(NewAnd, ICI); |
| 5232 | |
| 5233 | ICI.setOperand(0, NewAnd); |
| 5234 | return &ICI; |
| 5235 | } |
| 5236 | } |
| 5237 | break; |
| 5238 | |
| 5239 | case Instruction::Shl: // (icmp pred (shl X, ShAmt), CI) |
| 5240 | if (ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1))) { |
| 5241 | if (ICI.isEquality()) { |
| 5242 | uint32_t TypeBits = RHSV.getBitWidth(); |
| 5243 | |
| 5244 | // Check that the shift amount is in range. If not, don't perform |
| 5245 | // undefined shifts. When the shift is visited it will be |
| 5246 | // simplified. |
| 5247 | if (ShAmt->uge(TypeBits)) |
| 5248 | break; |
| 5249 | |
| 5250 | // If we are comparing against bits always shifted out, the |
| 5251 | // comparison cannot succeed. |
| 5252 | Constant *Comp = |
| 5253 | ConstantExpr::getShl(ConstantExpr::getLShr(RHS, ShAmt), ShAmt); |
| 5254 | if (Comp != RHS) {// Comparing against a bit that we know is zero. |
| 5255 | bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE; |
| 5256 | Constant *Cst = ConstantInt::get(Type::Int1Ty, IsICMP_NE); |
| 5257 | return ReplaceInstUsesWith(ICI, Cst); |
| 5258 | } |
| 5259 | |
| 5260 | if (LHSI->hasOneUse()) { |
| 5261 | // Otherwise strength reduce the shift into an and. |
| 5262 | uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits); |
| 5263 | Constant *Mask = |
| 5264 | ConstantInt::get(APInt::getLowBitsSet(TypeBits, TypeBits-ShAmtVal)); |
| 5265 | |
| 5266 | Instruction *AndI = |
| 5267 | BinaryOperator::createAnd(LHSI->getOperand(0), |
| 5268 | Mask, LHSI->getName()+".mask"); |
| 5269 | Value *And = InsertNewInstBefore(AndI, ICI); |
| 5270 | return new ICmpInst(ICI.getPredicate(), And, |
Chris Lattner | 7305084 | 2007-04-03 23:29:39 +0000 | [diff] [blame] | 5271 | ConstantInt::get(RHSV.lshr(ShAmtVal))); |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5272 | } |
| 5273 | } |
| 5274 | } |
| 5275 | break; |
| 5276 | |
| 5277 | case Instruction::LShr: // (icmp pred (shr X, ShAmt), CI) |
| 5278 | case Instruction::AShr: |
| 5279 | if (ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1))) { |
| 5280 | if (ICI.isEquality()) { |
| 5281 | // Check that the shift amount is in range. If not, don't perform |
| 5282 | // undefined shifts. When the shift is visited it will be |
| 5283 | // simplified. |
| 5284 | uint32_t TypeBits = RHSV.getBitWidth(); |
| 5285 | if (ShAmt->uge(TypeBits)) |
| 5286 | break; |
| 5287 | uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits); |
| 5288 | |
| 5289 | // If we are comparing against bits always shifted out, the |
| 5290 | // comparison cannot succeed. |
| 5291 | APInt Comp = RHSV << ShAmtVal; |
| 5292 | if (LHSI->getOpcode() == Instruction::LShr) |
| 5293 | Comp = Comp.lshr(ShAmtVal); |
| 5294 | else |
| 5295 | Comp = Comp.ashr(ShAmtVal); |
| 5296 | |
| 5297 | if (Comp != RHSV) { // Comparing against a bit that we know is zero. |
| 5298 | bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE; |
| 5299 | Constant *Cst = ConstantInt::get(Type::Int1Ty, IsICMP_NE); |
| 5300 | return ReplaceInstUsesWith(ICI, Cst); |
| 5301 | } |
| 5302 | |
| 5303 | if (LHSI->hasOneUse() || RHSV == 0) { |
| 5304 | // Otherwise strength reduce the shift into an and. |
| 5305 | APInt Val(APInt::getHighBitsSet(TypeBits, TypeBits - ShAmtVal)); |
| 5306 | Constant *Mask = ConstantInt::get(Val); |
| 5307 | |
| 5308 | Instruction *AndI = |
| 5309 | BinaryOperator::createAnd(LHSI->getOperand(0), |
| 5310 | Mask, LHSI->getName()+".mask"); |
| 5311 | Value *And = InsertNewInstBefore(AndI, ICI); |
| 5312 | return new ICmpInst(ICI.getPredicate(), And, |
| 5313 | ConstantExpr::getShl(RHS, ShAmt)); |
| 5314 | } |
| 5315 | } |
| 5316 | } |
| 5317 | break; |
| 5318 | |
| 5319 | case Instruction::SDiv: |
| 5320 | case Instruction::UDiv: |
| 5321 | // Fold: icmp pred ([us]div X, C1), C2 -> range test |
| 5322 | // Fold this div into the comparison, producing a range check. |
| 5323 | // Determine, based on the divide type, what the range is being |
| 5324 | // checked. If there is an overflow on the low or high side, remember |
| 5325 | // it, otherwise compute the range [low, hi) bounding the new value. |
| 5326 | // See: InsertRangeTest above for the kinds of replacements possible. |
| 5327 | if (ConstantInt *DivRHS = dyn_cast<ConstantInt>(LHSI->getOperand(1))) { |
| 5328 | // FIXME: If the operand types don't match the type of the divide |
| 5329 | // then don't attempt this transform. The code below doesn't have the |
| 5330 | // logic to deal with a signed divide and an unsigned compare (and |
| 5331 | // vice versa). This is because (x /s C1) <s C2 produces different |
| 5332 | // results than (x /s C1) <u C2 or (x /u C1) <s C2 or even |
| 5333 | // (x /u C1) <u C2. Simply casting the operands and result won't |
| 5334 | // work. :( The if statement below tests that condition and bails |
| 5335 | // if it finds it. |
| 5336 | bool DivIsSigned = LHSI->getOpcode() == Instruction::SDiv; |
| 5337 | if (!ICI.isEquality() && DivIsSigned != ICI.isSignedPredicate()) |
| 5338 | break; |
| 5339 | if (DivRHS->isZero()) |
| 5340 | break; // Don't hack on div by zero |
| 5341 | |
| 5342 | // Initialize the variables that will indicate the nature of the |
| 5343 | // range check. |
| 5344 | bool LoOverflow = false, HiOverflow = false; |
| 5345 | ConstantInt *LoBound = 0, *HiBound = 0; |
| 5346 | |
| 5347 | // Compute Prod = CI * DivRHS. We are essentially solving an equation |
| 5348 | // of form X/C1=C2. We solve for X by multiplying C1 (DivRHS) and |
| 5349 | // C2 (CI). By solving for X we can turn this into a range check |
| 5350 | // instead of computing a divide. |
| 5351 | ConstantInt *Prod = Multiply(RHS, DivRHS); |
| 5352 | |
| 5353 | // Determine if the product overflows by seeing if the product is |
| 5354 | // not equal to the divide. Make sure we do the same kind of divide |
| 5355 | // as in the LHS instruction that we're folding. |
| 5356 | bool ProdOV = (DivIsSigned ? ConstantExpr::getSDiv(Prod, DivRHS) : |
| 5357 | ConstantExpr::getUDiv(Prod, DivRHS)) != RHS; |
| 5358 | |
| 5359 | // Get the ICmp opcode |
| 5360 | ICmpInst::Predicate predicate = ICI.getPredicate(); |
| 5361 | |
| 5362 | if (!DivIsSigned) { // udiv |
| 5363 | LoBound = Prod; |
| 5364 | LoOverflow = ProdOV; |
| 5365 | HiOverflow = ProdOV || |
| 5366 | AddWithOverflow(HiBound, LoBound, DivRHS, false); |
| 5367 | } else if (DivRHS->getValue().isPositive()) { // Divisor is > 0. |
| 5368 | if (RHSV == 0) { // (X / pos) op 0 |
| 5369 | // Can't overflow. |
| 5370 | LoBound = cast<ConstantInt>(ConstantExpr::getNeg(SubOne(DivRHS))); |
| 5371 | HiBound = DivRHS; |
| 5372 | } else if (RHSV.isPositive()) { // (X / pos) op pos |
| 5373 | LoBound = Prod; |
| 5374 | LoOverflow = ProdOV; |
| 5375 | HiOverflow = ProdOV || |
| 5376 | AddWithOverflow(HiBound, Prod, DivRHS, true); |
| 5377 | } else { // (X / pos) op neg |
| 5378 | Constant *DivRHSH = ConstantExpr::getNeg(SubOne(DivRHS)); |
| 5379 | LoOverflow = AddWithOverflow(LoBound, Prod, |
| 5380 | cast<ConstantInt>(DivRHSH), true); |
| 5381 | HiBound = AddOne(Prod); |
| 5382 | HiOverflow = ProdOV; |
| 5383 | } |
| 5384 | } else { // Divisor is < 0. |
| 5385 | if (RHSV == 0) { // (X / neg) op 0 |
| 5386 | LoBound = AddOne(DivRHS); |
| 5387 | HiBound = cast<ConstantInt>(ConstantExpr::getNeg(DivRHS)); |
| 5388 | if (HiBound == DivRHS) |
| 5389 | LoBound = 0; // - INTMIN = INTMIN |
| 5390 | } else if (RHSV.isPositive()) { // (X / neg) op pos |
| 5391 | HiOverflow = LoOverflow = ProdOV; |
| 5392 | if (!LoOverflow) |
| 5393 | LoOverflow = AddWithOverflow(LoBound, Prod, AddOne(DivRHS), |
| 5394 | true); |
| 5395 | HiBound = AddOne(Prod); |
| 5396 | } else { // (X / neg) op neg |
| 5397 | LoBound = Prod; |
| 5398 | LoOverflow = HiOverflow = ProdOV; |
| 5399 | HiBound = Subtract(Prod, DivRHS); |
| 5400 | } |
| 5401 | |
| 5402 | // Dividing by a negate swaps the condition. |
| 5403 | predicate = ICmpInst::getSwappedPredicate(predicate); |
| 5404 | } |
| 5405 | |
| 5406 | if (LoBound) { |
| 5407 | Value *X = LHSI->getOperand(0); |
| 5408 | switch (predicate) { |
| 5409 | default: assert(0 && "Unhandled icmp opcode!"); |
| 5410 | case ICmpInst::ICMP_EQ: |
| 5411 | if (LoOverflow && HiOverflow) |
| 5412 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
| 5413 | else if (HiOverflow) |
| 5414 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : |
| 5415 | ICmpInst::ICMP_UGE, X, LoBound); |
| 5416 | else if (LoOverflow) |
| 5417 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : |
| 5418 | ICmpInst::ICMP_ULT, X, HiBound); |
| 5419 | else |
| 5420 | return InsertRangeTest(X, LoBound, HiBound, DivIsSigned, |
| 5421 | true, ICI); |
| 5422 | case ICmpInst::ICMP_NE: |
| 5423 | if (LoOverflow && HiOverflow) |
| 5424 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); |
| 5425 | else if (HiOverflow) |
| 5426 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : |
| 5427 | ICmpInst::ICMP_ULT, X, LoBound); |
| 5428 | else if (LoOverflow) |
| 5429 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : |
| 5430 | ICmpInst::ICMP_UGE, X, HiBound); |
| 5431 | else |
| 5432 | return InsertRangeTest(X, LoBound, HiBound, DivIsSigned, |
| 5433 | false, ICI); |
| 5434 | case ICmpInst::ICMP_ULT: |
| 5435 | case ICmpInst::ICMP_SLT: |
| 5436 | if (LoOverflow) |
| 5437 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
| 5438 | return new ICmpInst(predicate, X, LoBound); |
| 5439 | case ICmpInst::ICMP_UGT: |
| 5440 | case ICmpInst::ICMP_SGT: |
| 5441 | if (HiOverflow) |
| 5442 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
| 5443 | if (predicate == ICmpInst::ICMP_UGT) |
| 5444 | return new ICmpInst(ICmpInst::ICMP_UGE, X, HiBound); |
| 5445 | else |
| 5446 | return new ICmpInst(ICmpInst::ICMP_SGE, X, HiBound); |
| 5447 | } |
| 5448 | } |
| 5449 | } |
| 5450 | break; |
| 5451 | } |
| 5452 | |
| 5453 | // Simplify icmp_eq and icmp_ne instructions with integer constant RHS. |
| 5454 | if (ICI.isEquality()) { |
| 5455 | bool isICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE; |
| 5456 | |
| 5457 | // If the first operand is (add|sub|and|or|xor|rem) with a constant, and |
| 5458 | // the second operand is a constant, simplify a bit. |
| 5459 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(LHSI)) { |
| 5460 | switch (BO->getOpcode()) { |
| 5461 | case Instruction::SRem: |
| 5462 | // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one. |
| 5463 | if (RHSV == 0 && isa<ConstantInt>(BO->getOperand(1)) &&BO->hasOneUse()){ |
| 5464 | const APInt &V = cast<ConstantInt>(BO->getOperand(1))->getValue(); |
| 5465 | if (V.sgt(APInt(V.getBitWidth(), 1)) && V.isPowerOf2()) { |
| 5466 | Instruction *NewRem = |
| 5467 | BinaryOperator::createURem(BO->getOperand(0), BO->getOperand(1), |
| 5468 | BO->getName()); |
| 5469 | InsertNewInstBefore(NewRem, ICI); |
| 5470 | return new ICmpInst(ICI.getPredicate(), NewRem, |
| 5471 | Constant::getNullValue(BO->getType())); |
| 5472 | } |
| 5473 | } |
| 5474 | break; |
| 5475 | case Instruction::Add: |
| 5476 | // Replace ((add A, B) != C) with (A != C-B) if B & C are constants. |
| 5477 | if (ConstantInt *BOp1C = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
| 5478 | if (BO->hasOneUse()) |
| 5479 | return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), |
| 5480 | Subtract(RHS, BOp1C)); |
| 5481 | } else if (RHSV == 0) { |
| 5482 | // Replace ((add A, B) != 0) with (A != -B) if A or B is |
| 5483 | // efficiently invertible, or if the add has just this one use. |
| 5484 | Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1); |
| 5485 | |
| 5486 | if (Value *NegVal = dyn_castNegVal(BOp1)) |
| 5487 | return new ICmpInst(ICI.getPredicate(), BOp0, NegVal); |
| 5488 | else if (Value *NegVal = dyn_castNegVal(BOp0)) |
| 5489 | return new ICmpInst(ICI.getPredicate(), NegVal, BOp1); |
| 5490 | else if (BO->hasOneUse()) { |
| 5491 | Instruction *Neg = BinaryOperator::createNeg(BOp1); |
| 5492 | InsertNewInstBefore(Neg, ICI); |
| 5493 | Neg->takeName(BO); |
| 5494 | return new ICmpInst(ICI.getPredicate(), BOp0, Neg); |
| 5495 | } |
| 5496 | } |
| 5497 | break; |
| 5498 | case Instruction::Xor: |
| 5499 | // For the xor case, we can xor two constants together, eliminating |
| 5500 | // the explicit xor. |
| 5501 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) |
| 5502 | return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), |
| 5503 | ConstantExpr::getXor(RHS, BOC)); |
| 5504 | |
| 5505 | // FALLTHROUGH |
| 5506 | case Instruction::Sub: |
| 5507 | // Replace (([sub|xor] A, B) != 0) with (A != B) |
| 5508 | if (RHSV == 0) |
| 5509 | return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), |
| 5510 | BO->getOperand(1)); |
| 5511 | break; |
| 5512 | |
| 5513 | case Instruction::Or: |
| 5514 | // If bits are being or'd in that are not present in the constant we |
| 5515 | // are comparing against, then the comparison could never succeed! |
| 5516 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) { |
| 5517 | Constant *NotCI = ConstantExpr::getNot(RHS); |
| 5518 | if (!ConstantExpr::getAnd(BOC, NotCI)->isNullValue()) |
| 5519 | return ReplaceInstUsesWith(ICI, ConstantInt::get(Type::Int1Ty, |
| 5520 | isICMP_NE)); |
| 5521 | } |
| 5522 | break; |
| 5523 | |
| 5524 | case Instruction::And: |
| 5525 | if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
| 5526 | // If bits are being compared against that are and'd out, then the |
| 5527 | // comparison can never succeed! |
| 5528 | if ((RHSV & ~BOC->getValue()) != 0) |
| 5529 | return ReplaceInstUsesWith(ICI, ConstantInt::get(Type::Int1Ty, |
| 5530 | isICMP_NE)); |
| 5531 | |
| 5532 | // If we have ((X & C) == C), turn it into ((X & C) != 0). |
| 5533 | if (RHS == BOC && RHSV.isPowerOf2()) |
| 5534 | return new ICmpInst(isICMP_NE ? ICmpInst::ICMP_EQ : |
| 5535 | ICmpInst::ICMP_NE, LHSI, |
| 5536 | Constant::getNullValue(RHS->getType())); |
| 5537 | |
| 5538 | // Replace (and X, (1 << size(X)-1) != 0) with x s< 0 |
| 5539 | if (isSignBit(BOC)) { |
| 5540 | Value *X = BO->getOperand(0); |
| 5541 | Constant *Zero = Constant::getNullValue(X->getType()); |
| 5542 | ICmpInst::Predicate pred = isICMP_NE ? |
| 5543 | ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGE; |
| 5544 | return new ICmpInst(pred, X, Zero); |
| 5545 | } |
| 5546 | |
| 5547 | // ((X & ~7) == 0) --> X < 8 |
| 5548 | if (RHSV == 0 && isHighOnes(BOC)) { |
| 5549 | Value *X = BO->getOperand(0); |
| 5550 | Constant *NegX = ConstantExpr::getNeg(BOC); |
| 5551 | ICmpInst::Predicate pred = isICMP_NE ? |
| 5552 | ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT; |
| 5553 | return new ICmpInst(pred, X, NegX); |
| 5554 | } |
| 5555 | } |
| 5556 | default: break; |
| 5557 | } |
| 5558 | } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(LHSI)) { |
| 5559 | // Handle icmp {eq|ne} <intrinsic>, intcst. |
| 5560 | if (II->getIntrinsicID() == Intrinsic::bswap) { |
| 5561 | AddToWorkList(II); |
| 5562 | ICI.setOperand(0, II->getOperand(1)); |
| 5563 | ICI.setOperand(1, ConstantInt::get(RHSV.byteSwap())); |
| 5564 | return &ICI; |
| 5565 | } |
| 5566 | } |
| 5567 | } else { // Not a ICMP_EQ/ICMP_NE |
Chris Lattner | e34e9a2 | 2007-04-14 23:32:02 +0000 | [diff] [blame] | 5568 | // If the LHS is a cast from an integral value of the same size, |
| 5569 | // then since we know the RHS is a constant, try to simlify. |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5570 | if (CastInst *Cast = dyn_cast<CastInst>(LHSI)) { |
| 5571 | Value *CastOp = Cast->getOperand(0); |
| 5572 | const Type *SrcTy = CastOp->getType(); |
| 5573 | uint32_t SrcTySize = SrcTy->getPrimitiveSizeInBits(); |
| 5574 | if (SrcTy->isInteger() && |
| 5575 | SrcTySize == Cast->getType()->getPrimitiveSizeInBits()) { |
| 5576 | // If this is an unsigned comparison, try to make the comparison use |
| 5577 | // smaller constant values. |
| 5578 | if (ICI.getPredicate() == ICmpInst::ICMP_ULT && RHSV.isSignBit()) { |
| 5579 | // X u< 128 => X s> -1 |
| 5580 | return new ICmpInst(ICmpInst::ICMP_SGT, CastOp, |
| 5581 | ConstantInt::get(APInt::getAllOnesValue(SrcTySize))); |
| 5582 | } else if (ICI.getPredicate() == ICmpInst::ICMP_UGT && |
| 5583 | RHSV == APInt::getSignedMaxValue(SrcTySize)) { |
| 5584 | // X u> 127 => X s< 0 |
| 5585 | return new ICmpInst(ICmpInst::ICMP_SLT, CastOp, |
| 5586 | Constant::getNullValue(SrcTy)); |
| 5587 | } |
| 5588 | } |
| 5589 | } |
| 5590 | } |
| 5591 | return 0; |
| 5592 | } |
| 5593 | |
| 5594 | /// visitICmpInstWithCastAndCast - Handle icmp (cast x to y), (cast/cst). |
| 5595 | /// We only handle extending casts so far. |
| 5596 | /// |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5597 | Instruction *InstCombiner::visitICmpInstWithCastAndCast(ICmpInst &ICI) { |
| 5598 | const CastInst *LHSCI = cast<CastInst>(ICI.getOperand(0)); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 5599 | Value *LHSCIOp = LHSCI->getOperand(0); |
| 5600 | const Type *SrcTy = LHSCIOp->getType(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5601 | const Type *DestTy = LHSCI->getType(); |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5602 | Value *RHSCIOp; |
| 5603 | |
Chris Lattner | 8c756c1 | 2007-05-05 22:41:33 +0000 | [diff] [blame] | 5604 | // Turn icmp (ptrtoint x), (ptrtoint/c) into a compare of the input if the |
| 5605 | // integer type is the same size as the pointer type. |
| 5606 | if (LHSCI->getOpcode() == Instruction::PtrToInt && |
| 5607 | getTargetData().getPointerSizeInBits() == |
| 5608 | cast<IntegerType>(DestTy)->getBitWidth()) { |
| 5609 | Value *RHSOp = 0; |
| 5610 | if (Constant *RHSC = dyn_cast<Constant>(ICI.getOperand(1))) { |
Chris Lattner | 6f6f512 | 2007-05-06 07:24:03 +0000 | [diff] [blame] | 5611 | RHSOp = ConstantExpr::getIntToPtr(RHSC, SrcTy); |
Chris Lattner | 8c756c1 | 2007-05-05 22:41:33 +0000 | [diff] [blame] | 5612 | } else if (PtrToIntInst *RHSC = dyn_cast<PtrToIntInst>(ICI.getOperand(1))) { |
| 5613 | RHSOp = RHSC->getOperand(0); |
| 5614 | // If the pointer types don't match, insert a bitcast. |
| 5615 | if (LHSCIOp->getType() != RHSOp->getType()) |
| 5616 | RHSOp = InsertCastBefore(Instruction::BitCast, RHSOp, |
| 5617 | LHSCIOp->getType(), ICI); |
| 5618 | } |
| 5619 | |
| 5620 | if (RHSOp) |
| 5621 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSOp); |
| 5622 | } |
| 5623 | |
| 5624 | // The code below only handles extension cast instructions, so far. |
| 5625 | // Enforce this. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5626 | if (LHSCI->getOpcode() != Instruction::ZExt && |
| 5627 | LHSCI->getOpcode() != Instruction::SExt) |
Chris Lattner | b352fa5 | 2005-01-17 03:20:02 +0000 | [diff] [blame] | 5628 | return 0; |
| 5629 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5630 | bool isSignedExt = LHSCI->getOpcode() == Instruction::SExt; |
| 5631 | bool isSignedCmp = ICI.isSignedPredicate(); |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5632 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5633 | if (CastInst *CI = dyn_cast<CastInst>(ICI.getOperand(1))) { |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5634 | // Not an extension from the same type? |
| 5635 | RHSCIOp = CI->getOperand(0); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5636 | if (RHSCIOp->getType() != LHSCIOp->getType()) |
| 5637 | return 0; |
Chris Lattner | a5c5e77 | 2007-01-13 23:11:38 +0000 | [diff] [blame] | 5638 | |
| 5639 | // If the signedness of the two compares doesn't agree (i.e. one is a sext |
| 5640 | // and the other is a zext), then we can't handle this. |
| 5641 | if (CI->getOpcode() != LHSCI->getOpcode()) |
| 5642 | return 0; |
| 5643 | |
| 5644 | // Likewise, if the signedness of the [sz]exts and the compare don't match, |
| 5645 | // then we can't handle this. |
| 5646 | if (isSignedExt != isSignedCmp && !ICI.isEquality()) |
| 5647 | return 0; |
| 5648 | |
| 5649 | // Okay, just insert a compare of the reduced operands now! |
| 5650 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSCIOp); |
Reid Spencer | 6731d5c | 2004-11-28 21:31:15 +0000 | [diff] [blame] | 5651 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5652 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5653 | // If we aren't dealing with a constant on the RHS, exit early |
| 5654 | ConstantInt *CI = dyn_cast<ConstantInt>(ICI.getOperand(1)); |
| 5655 | if (!CI) |
| 5656 | return 0; |
| 5657 | |
| 5658 | // Compute the constant that would happen if we truncated to SrcTy then |
| 5659 | // reextended to DestTy. |
| 5660 | Constant *Res1 = ConstantExpr::getTrunc(CI, SrcTy); |
| 5661 | Constant *Res2 = ConstantExpr::getCast(LHSCI->getOpcode(), Res1, DestTy); |
| 5662 | |
| 5663 | // If the re-extended constant didn't change... |
| 5664 | if (Res2 == CI) { |
| 5665 | // Make sure that sign of the Cmp and the sign of the Cast are the same. |
| 5666 | // For example, we might have: |
| 5667 | // %A = sext short %X to uint |
| 5668 | // %B = icmp ugt uint %A, 1330 |
| 5669 | // It is incorrect to transform this into |
| 5670 | // %B = icmp ugt short %X, 1330 |
| 5671 | // because %A may have negative value. |
| 5672 | // |
| 5673 | // However, it is OK if SrcTy is bool (See cast-set.ll testcase) |
| 5674 | // OR operation is EQ/NE. |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 5675 | if (isSignedExt == isSignedCmp || SrcTy == Type::Int1Ty || ICI.isEquality()) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5676 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, Res1); |
| 5677 | else |
| 5678 | return 0; |
| 5679 | } |
| 5680 | |
| 5681 | // The re-extended constant changed so the constant cannot be represented |
| 5682 | // in the shorter type. Consequently, we cannot emit a simple comparison. |
| 5683 | |
| 5684 | // First, handle some easy cases. We know the result cannot be equal at this |
| 5685 | // point so handle the ICI.isEquality() cases |
| 5686 | if (ICI.getPredicate() == ICmpInst::ICMP_EQ) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5687 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5688 | if (ICI.getPredicate() == ICmpInst::ICMP_NE) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5689 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5690 | |
| 5691 | // Evaluate the comparison for LT (we invert for GT below). LE and GE cases |
| 5692 | // should have been folded away previously and not enter in here. |
| 5693 | Value *Result; |
| 5694 | if (isSignedCmp) { |
| 5695 | // We're performing a signed comparison. |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5696 | if (cast<ConstantInt>(CI)->getValue().isNegative()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5697 | Result = ConstantInt::getFalse(); // X < (small) --> false |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5698 | else |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5699 | Result = ConstantInt::getTrue(); // X < (large) --> true |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5700 | } else { |
| 5701 | // We're performing an unsigned comparison. |
| 5702 | if (isSignedExt) { |
| 5703 | // We're performing an unsigned comp with a sign extended value. |
| 5704 | // This is true if the input is >= 0. [aka >s -1] |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5705 | Constant *NegOne = ConstantInt::getAllOnesValue(SrcTy); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5706 | Result = InsertNewInstBefore(new ICmpInst(ICmpInst::ICMP_SGT, LHSCIOp, |
| 5707 | NegOne, ICI.getName()), ICI); |
| 5708 | } else { |
| 5709 | // Unsigned extend & unsigned compare -> always true. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5710 | Result = ConstantInt::getTrue(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5711 | } |
| 5712 | } |
| 5713 | |
| 5714 | // Finally, return the value computed. |
| 5715 | if (ICI.getPredicate() == ICmpInst::ICMP_ULT || |
| 5716 | ICI.getPredicate() == ICmpInst::ICMP_SLT) { |
| 5717 | return ReplaceInstUsesWith(ICI, Result); |
| 5718 | } else { |
| 5719 | assert((ICI.getPredicate()==ICmpInst::ICMP_UGT || |
| 5720 | ICI.getPredicate()==ICmpInst::ICMP_SGT) && |
| 5721 | "ICmp should be folded!"); |
| 5722 | if (Constant *CI = dyn_cast<Constant>(Result)) |
| 5723 | return ReplaceInstUsesWith(ICI, ConstantExpr::getNot(CI)); |
| 5724 | else |
| 5725 | return BinaryOperator::createNot(Result); |
| 5726 | } |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5727 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5728 | |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 5729 | Instruction *InstCombiner::visitShl(BinaryOperator &I) { |
| 5730 | return commonShiftTransforms(I); |
| 5731 | } |
| 5732 | |
| 5733 | Instruction *InstCombiner::visitLShr(BinaryOperator &I) { |
| 5734 | return commonShiftTransforms(I); |
| 5735 | } |
| 5736 | |
| 5737 | Instruction *InstCombiner::visitAShr(BinaryOperator &I) { |
| 5738 | return commonShiftTransforms(I); |
| 5739 | } |
| 5740 | |
| 5741 | Instruction *InstCombiner::commonShiftTransforms(BinaryOperator &I) { |
| 5742 | assert(I.getOperand(1)->getType() == I.getOperand(0)->getType()); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 5743 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5744 | |
| 5745 | // shl X, 0 == X and shr X, 0 == X |
| 5746 | // shl 0, X == 0 and shr 0, X == 0 |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 5747 | if (Op1 == Constant::getNullValue(Op1->getType()) || |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 5748 | Op0 == Constant::getNullValue(Op0->getType())) |
| 5749 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 8d6bbdb | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 5750 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5751 | if (isa<UndefValue>(Op0)) { |
| 5752 | if (I.getOpcode() == Instruction::AShr) // undef >>s X -> undef |
Chris Lattner | 79a564c | 2004-10-16 23:28:04 +0000 | [diff] [blame] | 5753 | return ReplaceInstUsesWith(I, Op0); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5754 | else // undef << X -> 0, undef >>u X -> 0 |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5755 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 5756 | } |
| 5757 | if (isa<UndefValue>(Op1)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5758 | if (I.getOpcode() == Instruction::AShr) // X >>s undef -> X |
| 5759 | return ReplaceInstUsesWith(I, Op0); |
| 5760 | else // X << undef, X >>u undef -> 0 |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5761 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5762 | } |
| 5763 | |
Chris Lattner | de2b660 | 2006-11-10 23:38:52 +0000 | [diff] [blame] | 5764 | // ashr int -1, X = -1 (for any arithmetic shift rights of ~0) |
| 5765 | if (I.getOpcode() == Instruction::AShr) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5766 | if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0)) |
Chris Lattner | de2b660 | 2006-11-10 23:38:52 +0000 | [diff] [blame] | 5767 | if (CSI->isAllOnesValue()) |
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 5768 | return ReplaceInstUsesWith(I, CSI); |
| 5769 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 5770 | // Try to fold constant and into select arguments. |
| 5771 | if (isa<Constant>(Op0)) |
| 5772 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 5773 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 5774 | return R; |
| 5775 | |
Chris Lattner | 120347e | 2005-05-08 17:34:56 +0000 | [diff] [blame] | 5776 | // See if we can turn a signed shr into an unsigned shr. |
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 5777 | if (I.isArithmeticShift()) { |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 5778 | if (MaskedValueIsZero(Op0, |
| 5779 | APInt::getSignBit(I.getType()->getPrimitiveSizeInBits()))) { |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 5780 | return BinaryOperator::createLShr(Op0, Op1, I.getName()); |
Chris Lattner | 120347e | 2005-05-08 17:34:56 +0000 | [diff] [blame] | 5781 | } |
| 5782 | } |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 5783 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5784 | if (ConstantInt *CUI = dyn_cast<ConstantInt>(Op1)) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 5785 | if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I)) |
| 5786 | return Res; |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5787 | return 0; |
| 5788 | } |
| 5789 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5790 | Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 5791 | BinaryOperator &I) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5792 | bool isLeftShift = I.getOpcode() == Instruction::Shl; |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5793 | |
Chris Lattner | 8d6bbdb | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 5794 | // See if we can simplify any instructions used by the instruction whose sole |
| 5795 | // purpose is to compute bits we don't care about. |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 5796 | uint32_t TypeBits = Op0->getType()->getPrimitiveSizeInBits(); |
| 5797 | APInt KnownZero(TypeBits, 0), KnownOne(TypeBits, 0); |
| 5798 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(TypeBits), |
Chris Lattner | 8d6bbdb | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 5799 | KnownZero, KnownOne)) |
| 5800 | return &I; |
| 5801 | |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5802 | // shl uint X, 32 = 0 and shr ubyte Y, 9 = 0, ... just don't eliminate shr |
| 5803 | // of a signed value. |
| 5804 | // |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 5805 | if (Op1->uge(TypeBits)) { |
Chris Lattner | 0737c24 | 2007-02-02 05:29:55 +0000 | [diff] [blame] | 5806 | if (I.getOpcode() != Instruction::AShr) |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5807 | return ReplaceInstUsesWith(I, Constant::getNullValue(Op0->getType())); |
| 5808 | else { |
Chris Lattner | 0737c24 | 2007-02-02 05:29:55 +0000 | [diff] [blame] | 5809 | I.setOperand(1, ConstantInt::get(I.getType(), TypeBits-1)); |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5810 | return &I; |
Chris Lattner | 8adac75 | 2004-02-23 20:30:06 +0000 | [diff] [blame] | 5811 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5812 | } |
| 5813 | |
| 5814 | // ((X*C1) << C2) == (X * (C1 << C2)) |
| 5815 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0)) |
| 5816 | if (BO->getOpcode() == Instruction::Mul && isLeftShift) |
| 5817 | if (Constant *BOOp = dyn_cast<Constant>(BO->getOperand(1))) |
| 5818 | return BinaryOperator::createMul(BO->getOperand(0), |
| 5819 | ConstantExpr::getShl(BOOp, Op1)); |
| 5820 | |
| 5821 | // Try to fold constant and into select arguments. |
| 5822 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 5823 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 5824 | return R; |
| 5825 | if (isa<PHINode>(Op0)) |
| 5826 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 5827 | return NV; |
| 5828 | |
| 5829 | if (Op0->hasOneUse()) { |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5830 | if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0)) { |
| 5831 | // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C) |
| 5832 | Value *V1, *V2; |
| 5833 | ConstantInt *CC; |
| 5834 | switch (Op0BO->getOpcode()) { |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 5835 | default: break; |
| 5836 | case Instruction::Add: |
| 5837 | case Instruction::And: |
| 5838 | case Instruction::Or: |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 5839 | case Instruction::Xor: { |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 5840 | // These operators commute. |
| 5841 | // Turn (Y + (X >> C)) << C -> (X + (Y << C)) & (~0 << C) |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5842 | if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() && |
| 5843 | match(Op0BO->getOperand(1), |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5844 | m_Shr(m_Value(V1), m_ConstantInt(CC))) && CC == Op1) { |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 5845 | Instruction *YS = BinaryOperator::createShl( |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5846 | Op0BO->getOperand(0), Op1, |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5847 | Op0BO->getName()); |
| 5848 | InsertNewInstBefore(YS, I); // (Y << C) |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 5849 | Instruction *X = |
| 5850 | BinaryOperator::create(Op0BO->getOpcode(), YS, V1, |
| 5851 | Op0BO->getOperand(1)->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5852 | InsertNewInstBefore(X, I); // (X + (Y << C)) |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 5853 | uint32_t Op1Val = Op1->getLimitedValue(TypeBits); |
Zhou Sheng | 90b9681 | 2007-03-30 05:45:18 +0000 | [diff] [blame] | 5854 | return BinaryOperator::createAnd(X, ConstantInt::get( |
| 5855 | APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val))); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5856 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5857 | |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5858 | // Turn (Y + ((X >> C) & CC)) << C -> ((X & (CC << C)) + (Y << C)) |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 5859 | Value *Op0BOOp1 = Op0BO->getOperand(1); |
Chris Lattner | 3c69849 | 2007-03-05 00:11:19 +0000 | [diff] [blame] | 5860 | if (isLeftShift && Op0BOOp1->hasOneUse() && |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 5861 | match(Op0BOOp1, |
| 5862 | 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] | 5863 | cast<BinaryOperator>(Op0BOOp1)->getOperand(0)->hasOneUse() && |
| 5864 | V2 == Op1) { |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 5865 | Instruction *YS = BinaryOperator::createShl( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 5866 | Op0BO->getOperand(0), Op1, |
| 5867 | Op0BO->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5868 | InsertNewInstBefore(YS, I); // (Y << C) |
| 5869 | Instruction *XM = |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5870 | BinaryOperator::createAnd(V1, ConstantExpr::getShl(CC, Op1), |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5871 | V1->getName()+".mask"); |
| 5872 | InsertNewInstBefore(XM, I); // X & (CC << C) |
| 5873 | |
| 5874 | return BinaryOperator::create(Op0BO->getOpcode(), YS, XM); |
| 5875 | } |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 5876 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5877 | |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 5878 | // FALL THROUGH. |
| 5879 | case Instruction::Sub: { |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 5880 | // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C) |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5881 | if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() && |
| 5882 | match(Op0BO->getOperand(0), |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5883 | m_Shr(m_Value(V1), m_ConstantInt(CC))) && CC == Op1) { |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 5884 | Instruction *YS = BinaryOperator::createShl( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 5885 | Op0BO->getOperand(1), Op1, |
| 5886 | Op0BO->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5887 | InsertNewInstBefore(YS, I); // (Y << C) |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 5888 | Instruction *X = |
Chris Lattner | 13d4ab4 | 2006-05-31 21:14:00 +0000 | [diff] [blame] | 5889 | BinaryOperator::create(Op0BO->getOpcode(), V1, YS, |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 5890 | Op0BO->getOperand(0)->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5891 | InsertNewInstBefore(X, I); // (X + (Y << C)) |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 5892 | uint32_t Op1Val = Op1->getLimitedValue(TypeBits); |
Zhou Sheng | 90b9681 | 2007-03-30 05:45:18 +0000 | [diff] [blame] | 5893 | return BinaryOperator::createAnd(X, ConstantInt::get( |
| 5894 | APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val))); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5895 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5896 | |
Chris Lattner | 13d4ab4 | 2006-05-31 21:14:00 +0000 | [diff] [blame] | 5897 | // Turn (((X >> C)&CC) + Y) << C -> (X + (Y << C)) & (CC << C) |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5898 | if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() && |
| 5899 | match(Op0BO->getOperand(0), |
| 5900 | m_And(m_Shr(m_Value(V1), m_Value(V2)), |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5901 | m_ConstantInt(CC))) && V2 == Op1 && |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 5902 | cast<BinaryOperator>(Op0BO->getOperand(0)) |
| 5903 | ->getOperand(0)->hasOneUse()) { |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 5904 | Instruction *YS = BinaryOperator::createShl( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 5905 | Op0BO->getOperand(1), Op1, |
| 5906 | Op0BO->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5907 | InsertNewInstBefore(YS, I); // (Y << C) |
| 5908 | Instruction *XM = |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5909 | BinaryOperator::createAnd(V1, ConstantExpr::getShl(CC, Op1), |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5910 | V1->getName()+".mask"); |
| 5911 | InsertNewInstBefore(XM, I); // X & (CC << C) |
| 5912 | |
Chris Lattner | 13d4ab4 | 2006-05-31 21:14:00 +0000 | [diff] [blame] | 5913 | return BinaryOperator::create(Op0BO->getOpcode(), XM, YS); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5914 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5915 | |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 5916 | break; |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 5917 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5918 | } |
| 5919 | |
| 5920 | |
| 5921 | // If the operand is an bitwise operator with a constant RHS, and the |
| 5922 | // shift is the only use, we can pull it out of the shift. |
| 5923 | if (ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand(1))) { |
| 5924 | bool isValid = true; // Valid only for And, Or, Xor |
| 5925 | bool highBitSet = false; // Transform if high bit of constant set? |
| 5926 | |
| 5927 | switch (Op0BO->getOpcode()) { |
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 5928 | default: isValid = false; break; // Do not perform transform! |
Chris Lattner | 1f7e160 | 2004-10-08 03:46:20 +0000 | [diff] [blame] | 5929 | case Instruction::Add: |
| 5930 | isValid = isLeftShift; |
| 5931 | break; |
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 5932 | case Instruction::Or: |
| 5933 | case Instruction::Xor: |
| 5934 | highBitSet = false; |
| 5935 | break; |
| 5936 | case Instruction::And: |
| 5937 | highBitSet = true; |
| 5938 | break; |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5939 | } |
| 5940 | |
| 5941 | // If this is a signed shift right, and the high bit is modified |
| 5942 | // by the logical operation, do not perform the transformation. |
| 5943 | // The highBitSet boolean indicates the value of the high bit of |
| 5944 | // the constant which would cause it to be modified for this |
| 5945 | // operation. |
| 5946 | // |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5947 | if (isValid && !isLeftShift && I.getOpcode() == Instruction::AShr) { |
Zhou Sheng | e9e03f6 | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 5948 | isValid = Op0C->getValue()[TypeBits-1] == highBitSet; |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5949 | } |
| 5950 | |
| 5951 | if (isValid) { |
| 5952 | Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, Op1); |
| 5953 | |
| 5954 | Instruction *NewShift = |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 5955 | BinaryOperator::create(I.getOpcode(), Op0BO->getOperand(0), Op1); |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5956 | InsertNewInstBefore(NewShift, I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 5957 | NewShift->takeName(Op0BO); |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5958 | |
| 5959 | return BinaryOperator::create(Op0BO->getOpcode(), NewShift, |
| 5960 | NewRHS); |
| 5961 | } |
| 5962 | } |
| 5963 | } |
| 5964 | } |
| 5965 | |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5966 | // 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] | 5967 | BinaryOperator *ShiftOp = dyn_cast<BinaryOperator>(Op0); |
| 5968 | if (ShiftOp && !ShiftOp->isShift()) |
| 5969 | ShiftOp = 0; |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5970 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5971 | if (ShiftOp && isa<ConstantInt>(ShiftOp->getOperand(1))) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5972 | ConstantInt *ShiftAmt1C = cast<ConstantInt>(ShiftOp->getOperand(1)); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 5973 | uint32_t ShiftAmt1 = ShiftAmt1C->getLimitedValue(TypeBits); |
| 5974 | uint32_t ShiftAmt2 = Op1->getLimitedValue(TypeBits); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5975 | assert(ShiftAmt2 != 0 && "Should have been simplified earlier"); |
| 5976 | if (ShiftAmt1 == 0) return 0; // Will be simplified in the future. |
| 5977 | Value *X = ShiftOp->getOperand(0); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5978 | |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 5979 | uint32_t AmtSum = ShiftAmt1+ShiftAmt2; // Fold into one big shift. |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 5980 | if (AmtSum > TypeBits) |
| 5981 | AmtSum = TypeBits; |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5982 | |
| 5983 | const IntegerType *Ty = cast<IntegerType>(I.getType()); |
| 5984 | |
| 5985 | // Check for (X << c1) << c2 and (X >> c1) >> c2 |
Chris Lattner | 7f3da2d | 2007-02-03 23:28:07 +0000 | [diff] [blame] | 5986 | if (I.getOpcode() == ShiftOp->getOpcode()) { |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5987 | return BinaryOperator::create(I.getOpcode(), X, |
| 5988 | ConstantInt::get(Ty, AmtSum)); |
| 5989 | } else if (ShiftOp->getOpcode() == Instruction::LShr && |
| 5990 | I.getOpcode() == Instruction::AShr) { |
| 5991 | // ((X >>u C1) >>s C2) -> (X >>u (C1+C2)) since C1 != 0. |
| 5992 | return BinaryOperator::createLShr(X, ConstantInt::get(Ty, AmtSum)); |
| 5993 | } else if (ShiftOp->getOpcode() == Instruction::AShr && |
| 5994 | I.getOpcode() == Instruction::LShr) { |
| 5995 | // ((X >>s C1) >>u C2) -> ((X >>s (C1+C2)) & mask) since C1 != 0. |
| 5996 | Instruction *Shift = |
| 5997 | BinaryOperator::createAShr(X, ConstantInt::get(Ty, AmtSum)); |
| 5998 | InsertNewInstBefore(Shift, I); |
| 5999 | |
Zhou Sheng | e9e03f6 | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 6000 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6001 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6002 | } |
| 6003 | |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6004 | // Okay, if we get here, one shift must be left, and the other shift must be |
| 6005 | // right. See if the amounts are equal. |
| 6006 | if (ShiftAmt1 == ShiftAmt2) { |
| 6007 | // If we have ((X >>? C) << C), turn this into X & (-1 << C). |
| 6008 | if (I.getOpcode() == Instruction::Shl) { |
Reid Spencer | 55702aa | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 6009 | APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt1)); |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6010 | return BinaryOperator::createAnd(X, ConstantInt::get(Mask)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6011 | } |
| 6012 | // If we have ((X << C) >>u C), turn this into X & (-1 >>u C). |
| 6013 | if (I.getOpcode() == Instruction::LShr) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 6014 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt1)); |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6015 | return BinaryOperator::createAnd(X, ConstantInt::get(Mask)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6016 | } |
| 6017 | // We can simplify ((X << C) >>s C) into a trunc + sext. |
| 6018 | // NOTE: we could do this for any C, but that would make 'unusual' integer |
| 6019 | // types. For now, just stick to ones well-supported by the code |
| 6020 | // generators. |
| 6021 | const Type *SExtType = 0; |
| 6022 | switch (Ty->getBitWidth() - ShiftAmt1) { |
Zhou Sheng | e9e03f6 | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 6023 | case 1 : |
| 6024 | case 8 : |
| 6025 | case 16 : |
| 6026 | case 32 : |
| 6027 | case 64 : |
| 6028 | case 128: |
| 6029 | SExtType = IntegerType::get(Ty->getBitWidth() - ShiftAmt1); |
| 6030 | break; |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6031 | default: break; |
| 6032 | } |
| 6033 | if (SExtType) { |
| 6034 | Instruction *NewTrunc = new TruncInst(X, SExtType, "sext"); |
| 6035 | InsertNewInstBefore(NewTrunc, I); |
| 6036 | return new SExtInst(NewTrunc, Ty); |
| 6037 | } |
| 6038 | // Otherwise, we can't handle it yet. |
| 6039 | } else if (ShiftAmt1 < ShiftAmt2) { |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 6040 | uint32_t ShiftDiff = ShiftAmt2-ShiftAmt1; |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6041 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 6042 | // (X >>? C1) << C2 --> X << (C2-C1) & (-1 << C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6043 | if (I.getOpcode() == Instruction::Shl) { |
| 6044 | assert(ShiftOp->getOpcode() == Instruction::LShr || |
| 6045 | ShiftOp->getOpcode() == Instruction::AShr); |
Chris Lattner | e8d56c5 | 2006-01-07 01:32:28 +0000 | [diff] [blame] | 6046 | Instruction *Shift = |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6047 | BinaryOperator::createShl(X, ConstantInt::get(Ty, ShiftDiff)); |
Chris Lattner | e8d56c5 | 2006-01-07 01:32:28 +0000 | [diff] [blame] | 6048 | InsertNewInstBefore(Shift, I); |
| 6049 | |
Reid Spencer | 55702aa | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 6050 | APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
| 6051 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6052 | } |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6053 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 6054 | // (X << C1) >>u C2 --> X >>u (C2-C1) & (-1 >> C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6055 | if (I.getOpcode() == Instruction::LShr) { |
| 6056 | assert(ShiftOp->getOpcode() == Instruction::Shl); |
| 6057 | Instruction *Shift = |
| 6058 | BinaryOperator::createLShr(X, ConstantInt::get(Ty, ShiftDiff)); |
| 6059 | InsertNewInstBefore(Shift, I); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6060 | |
Reid Spencer | d5e30f0 | 2007-03-26 17:18:58 +0000 | [diff] [blame] | 6061 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6062 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 6063 | } |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6064 | |
| 6065 | // We can't handle (X << C1) >>s C2, it shifts arbitrary bits in. |
| 6066 | } else { |
| 6067 | assert(ShiftAmt2 < ShiftAmt1); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 6068 | uint32_t ShiftDiff = ShiftAmt1-ShiftAmt2; |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6069 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 6070 | // (X >>? C1) << C2 --> X >>? (C1-C2) & (-1 << C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6071 | if (I.getOpcode() == Instruction::Shl) { |
| 6072 | assert(ShiftOp->getOpcode() == Instruction::LShr || |
| 6073 | ShiftOp->getOpcode() == Instruction::AShr); |
| 6074 | Instruction *Shift = |
| 6075 | BinaryOperator::create(ShiftOp->getOpcode(), X, |
| 6076 | ConstantInt::get(Ty, ShiftDiff)); |
| 6077 | InsertNewInstBefore(Shift, I); |
| 6078 | |
Reid Spencer | 55702aa | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 6079 | APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6080 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6081 | } |
| 6082 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 6083 | // (X << C1) >>u C2 --> X << (C1-C2) & (-1 >> C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6084 | if (I.getOpcode() == Instruction::LShr) { |
| 6085 | assert(ShiftOp->getOpcode() == Instruction::Shl); |
| 6086 | Instruction *Shift = |
| 6087 | BinaryOperator::createShl(X, ConstantInt::get(Ty, ShiftDiff)); |
| 6088 | InsertNewInstBefore(Shift, I); |
| 6089 | |
Reid Spencer | 68d27cf | 2007-03-26 23:45:51 +0000 | [diff] [blame] | 6090 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6091 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6092 | } |
| 6093 | |
| 6094 | // 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] | 6095 | } |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6096 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 6097 | return 0; |
| 6098 | } |
| 6099 | |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 6100 | |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 6101 | /// DecomposeSimpleLinearExpr - Analyze 'Val', seeing if it is a simple linear |
| 6102 | /// expression. If so, decompose it, returning some value X, such that Val is |
| 6103 | /// X*Scale+Offset. |
| 6104 | /// |
| 6105 | static Value *DecomposeSimpleLinearExpr(Value *Val, unsigned &Scale, |
Jeff Cohen | 86796be | 2007-04-04 16:58:57 +0000 | [diff] [blame] | 6106 | int &Offset) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6107 | assert(Val->getType() == Type::Int32Ty && "Unexpected allocation size type!"); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6108 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6109 | Offset = CI->getZExtValue(); |
| 6110 | Scale = 1; |
| 6111 | return ConstantInt::get(Type::Int32Ty, 0); |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 6112 | } else if (Instruction *I = dyn_cast<Instruction>(Val)) { |
| 6113 | if (I->getNumOperands() == 2) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6114 | if (ConstantInt *CUI = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6115 | if (I->getOpcode() == Instruction::Shl) { |
| 6116 | // This is a value scaled by '1 << the shift amt'. |
| 6117 | Scale = 1U << CUI->getZExtValue(); |
| 6118 | Offset = 0; |
| 6119 | return I->getOperand(0); |
| 6120 | } else if (I->getOpcode() == Instruction::Mul) { |
| 6121 | // This value is scaled by 'CUI'. |
| 6122 | Scale = CUI->getZExtValue(); |
| 6123 | Offset = 0; |
| 6124 | return I->getOperand(0); |
| 6125 | } else if (I->getOpcode() == Instruction::Add) { |
| 6126 | // We have X+C. Check to see if we really have (X*C2)+C1, |
| 6127 | // where C1 is divisible by C2. |
| 6128 | unsigned SubScale; |
| 6129 | Value *SubVal = |
| 6130 | DecomposeSimpleLinearExpr(I->getOperand(0), SubScale, Offset); |
| 6131 | Offset += CUI->getZExtValue(); |
| 6132 | if (SubScale > 1 && (Offset % SubScale == 0)) { |
| 6133 | Scale = SubScale; |
| 6134 | return SubVal; |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 6135 | } |
| 6136 | } |
| 6137 | } |
| 6138 | } |
| 6139 | } |
| 6140 | |
| 6141 | // Otherwise, we can't look past this. |
| 6142 | Scale = 1; |
| 6143 | Offset = 0; |
| 6144 | return Val; |
| 6145 | } |
| 6146 | |
| 6147 | |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6148 | /// PromoteCastOfAllocation - If we find a cast of an allocation instruction, |
| 6149 | /// 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] | 6150 | Instruction *InstCombiner::PromoteCastOfAllocation(BitCastInst &CI, |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6151 | AllocationInst &AI) { |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 6152 | const PointerType *PTy = cast<PointerType>(CI.getType()); |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6153 | |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 6154 | // Remove any uses of AI that are dead. |
| 6155 | assert(!CI.use_empty() && "Dead instructions should be removed earlier!"); |
Chris Lattner | 535014f | 2007-02-15 22:52:10 +0000 | [diff] [blame] | 6156 | |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 6157 | for (Value::use_iterator UI = AI.use_begin(), E = AI.use_end(); UI != E; ) { |
| 6158 | Instruction *User = cast<Instruction>(*UI++); |
| 6159 | if (isInstructionTriviallyDead(User)) { |
| 6160 | while (UI != E && *UI == User) |
| 6161 | ++UI; // If this instruction uses AI more than once, don't break UI. |
| 6162 | |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 6163 | ++NumDeadInst; |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 6164 | DOUT << "IC: DCE: " << *User; |
Chris Lattner | f22a5c6 | 2007-03-02 19:59:19 +0000 | [diff] [blame] | 6165 | EraseInstFromFunction(*User); |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 6166 | } |
| 6167 | } |
| 6168 | |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6169 | // Get the type really allocated and the type casted to. |
| 6170 | const Type *AllocElTy = AI.getAllocatedType(); |
| 6171 | const Type *CastElTy = PTy->getElementType(); |
| 6172 | if (!AllocElTy->isSized() || !CastElTy->isSized()) return 0; |
Chris Lattner | 18e78bb | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 6173 | |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 6174 | unsigned AllocElTyAlign = TD->getABITypeAlignment(AllocElTy); |
| 6175 | unsigned CastElTyAlign = TD->getABITypeAlignment(CastElTy); |
Chris Lattner | 18e78bb | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 6176 | if (CastElTyAlign < AllocElTyAlign) return 0; |
| 6177 | |
Chris Lattner | 39387a5 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 6178 | // If the allocation has multiple uses, only promote it if we are strictly |
| 6179 | // increasing the alignment of the resultant allocation. If we keep it the |
| 6180 | // same, we open the door to infinite loops of various kinds. |
| 6181 | if (!AI.hasOneUse() && CastElTyAlign == AllocElTyAlign) return 0; |
| 6182 | |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6183 | uint64_t AllocElTySize = TD->getTypeSize(AllocElTy); |
| 6184 | uint64_t CastElTySize = TD->getTypeSize(CastElTy); |
Chris Lattner | 0ddac2a | 2005-10-27 05:53:56 +0000 | [diff] [blame] | 6185 | if (CastElTySize == 0 || AllocElTySize == 0) return 0; |
Chris Lattner | 18e78bb | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 6186 | |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 6187 | // See if we can satisfy the modulus by pulling a scale out of the array |
| 6188 | // size argument. |
Jeff Cohen | 86796be | 2007-04-04 16:58:57 +0000 | [diff] [blame] | 6189 | unsigned ArraySizeScale; |
| 6190 | int ArrayOffset; |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 6191 | Value *NumElements = // See if the array size is a decomposable linear expr. |
| 6192 | DecomposeSimpleLinearExpr(AI.getOperand(0), ArraySizeScale, ArrayOffset); |
| 6193 | |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 6194 | // If we can now satisfy the modulus, by using a non-1 scale, we really can |
| 6195 | // do the xform. |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 6196 | if ((AllocElTySize*ArraySizeScale) % CastElTySize != 0 || |
| 6197 | (AllocElTySize*ArrayOffset ) % CastElTySize != 0) return 0; |
Chris Lattner | 8142b0a | 2005-10-27 06:12:00 +0000 | [diff] [blame] | 6198 | |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 6199 | unsigned Scale = (AllocElTySize*ArraySizeScale)/CastElTySize; |
| 6200 | Value *Amt = 0; |
| 6201 | if (Scale == 1) { |
| 6202 | Amt = NumElements; |
| 6203 | } else { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6204 | // If the allocation size is constant, form a constant mul expression |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6205 | Amt = ConstantInt::get(Type::Int32Ty, Scale); |
| 6206 | if (isa<ConstantInt>(NumElements)) |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 6207 | Amt = Multiply(cast<ConstantInt>(NumElements), cast<ConstantInt>(Amt)); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6208 | // otherwise multiply the amount and the number of elements |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 6209 | else if (Scale != 1) { |
| 6210 | Instruction *Tmp = BinaryOperator::createMul(Amt, NumElements, "tmp"); |
| 6211 | Amt = InsertNewInstBefore(Tmp, AI); |
Chris Lattner | 8142b0a | 2005-10-27 06:12:00 +0000 | [diff] [blame] | 6212 | } |
Chris Lattner | 0ddac2a | 2005-10-27 05:53:56 +0000 | [diff] [blame] | 6213 | } |
| 6214 | |
Jeff Cohen | 86796be | 2007-04-04 16:58:57 +0000 | [diff] [blame] | 6215 | if (int Offset = (AllocElTySize*ArrayOffset)/CastElTySize) { |
| 6216 | Value *Off = ConstantInt::get(Type::Int32Ty, Offset, true); |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 6217 | Instruction *Tmp = BinaryOperator::createAdd(Amt, Off, "tmp"); |
| 6218 | Amt = InsertNewInstBefore(Tmp, AI); |
| 6219 | } |
| 6220 | |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6221 | AllocationInst *New; |
| 6222 | if (isa<MallocInst>(AI)) |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6223 | New = new MallocInst(CastElTy, Amt, AI.getAlignment()); |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6224 | else |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6225 | New = new AllocaInst(CastElTy, Amt, AI.getAlignment()); |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6226 | InsertNewInstBefore(New, AI); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6227 | New->takeName(&AI); |
Chris Lattner | 39387a5 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 6228 | |
| 6229 | // If the allocation has multiple uses, insert a cast and change all things |
| 6230 | // that used it to use the new cast. This will also hack on CI, but it will |
| 6231 | // die soon. |
| 6232 | if (!AI.hasOneUse()) { |
| 6233 | AddUsesToWorkList(AI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6234 | // New is the allocation instruction, pointer typed. AI is the original |
| 6235 | // allocation instruction, also pointer typed. Thus, cast to use is BitCast. |
| 6236 | CastInst *NewCast = new BitCastInst(New, AI.getType(), "tmpcast"); |
Chris Lattner | 39387a5 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 6237 | InsertNewInstBefore(NewCast, AI); |
| 6238 | AI.replaceAllUsesWith(NewCast); |
| 6239 | } |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6240 | return ReplaceInstUsesWith(CI, New); |
| 6241 | } |
| 6242 | |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6243 | /// CanEvaluateInDifferentType - Return true if we can take the specified value |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6244 | /// and return it as type Ty without inserting any new casts and without |
| 6245 | /// changing the computed value. This is used by code that tries to decide |
| 6246 | /// whether promoting or shrinking integer operations to wider or smaller types |
| 6247 | /// will allow us to eliminate a truncate or extend. |
| 6248 | /// |
| 6249 | /// This is a truncation operation if Ty is smaller than V->getType(), or an |
| 6250 | /// extension operation if Ty is larger. |
| 6251 | static bool CanEvaluateInDifferentType(Value *V, const IntegerType *Ty, |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6252 | int &NumCastsRemoved) { |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6253 | // We can always evaluate constants in another type. |
| 6254 | if (isa<ConstantInt>(V)) |
| 6255 | return true; |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6256 | |
| 6257 | Instruction *I = dyn_cast<Instruction>(V); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6258 | if (!I) return false; |
| 6259 | |
| 6260 | const IntegerType *OrigTy = cast<IntegerType>(V->getType()); |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6261 | |
| 6262 | switch (I->getOpcode()) { |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6263 | case Instruction::Add: |
| 6264 | case Instruction::Sub: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6265 | case Instruction::And: |
| 6266 | case Instruction::Or: |
| 6267 | case Instruction::Xor: |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6268 | if (!I->hasOneUse()) return false; |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6269 | // These operators can all arbitrarily be extended or truncated. |
| 6270 | return CanEvaluateInDifferentType(I->getOperand(0), Ty, NumCastsRemoved) && |
| 6271 | CanEvaluateInDifferentType(I->getOperand(1), Ty, NumCastsRemoved); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6272 | |
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 6273 | case Instruction::Shl: |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6274 | if (!I->hasOneUse()) return false; |
| 6275 | // If we are truncating the result of this SHL, and if it's a shift of a |
| 6276 | // constant amount, we can always perform a SHL in a smaller type. |
| 6277 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 6278 | uint32_t BitWidth = Ty->getBitWidth(); |
| 6279 | if (BitWidth < OrigTy->getBitWidth() && |
| 6280 | CI->getLimitedValue(BitWidth) < BitWidth) |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6281 | return CanEvaluateInDifferentType(I->getOperand(0), Ty,NumCastsRemoved); |
| 6282 | } |
| 6283 | break; |
| 6284 | case Instruction::LShr: |
| 6285 | if (!I->hasOneUse()) return false; |
| 6286 | // If this is a truncate of a logical shr, we can truncate it to a smaller |
| 6287 | // lshr iff we know that the bits we would otherwise be shifting in are |
| 6288 | // already zeros. |
| 6289 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 6290 | uint32_t OrigBitWidth = OrigTy->getBitWidth(); |
| 6291 | uint32_t BitWidth = Ty->getBitWidth(); |
| 6292 | if (BitWidth < OrigBitWidth && |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6293 | MaskedValueIsZero(I->getOperand(0), |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 6294 | APInt::getHighBitsSet(OrigBitWidth, OrigBitWidth-BitWidth)) && |
| 6295 | CI->getLimitedValue(BitWidth) < BitWidth) { |
Chris Lattner | e34e9a2 | 2007-04-14 23:32:02 +0000 | [diff] [blame] | 6296 | return CanEvaluateInDifferentType(I->getOperand(0), Ty,NumCastsRemoved); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6297 | } |
| 6298 | } |
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 6299 | break; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6300 | case Instruction::Trunc: |
| 6301 | case Instruction::ZExt: |
| 6302 | case Instruction::SExt: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6303 | // If this is a cast from the destination type, we can trivially eliminate |
| 6304 | // it, and this will remove a cast overall. |
| 6305 | if (I->getOperand(0)->getType() == Ty) { |
Chris Lattner | d228018 | 2006-06-28 17:34:50 +0000 | [diff] [blame] | 6306 | // If the first operand is itself a cast, and is eliminable, do not count |
| 6307 | // this as an eliminable cast. We would prefer to eliminate those two |
| 6308 | // casts first. |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 6309 | if (isa<CastInst>(I->getOperand(0))) |
Chris Lattner | d228018 | 2006-06-28 17:34:50 +0000 | [diff] [blame] | 6310 | return true; |
| 6311 | |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6312 | ++NumCastsRemoved; |
| 6313 | return true; |
| 6314 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6315 | break; |
| 6316 | default: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6317 | // TODO: Can handle more cases here. |
| 6318 | break; |
| 6319 | } |
| 6320 | |
| 6321 | return false; |
| 6322 | } |
| 6323 | |
| 6324 | /// EvaluateInDifferentType - Given an expression that |
| 6325 | /// CanEvaluateInDifferentType returns true for, actually insert the code to |
| 6326 | /// evaluate the expression. |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 6327 | Value *InstCombiner::EvaluateInDifferentType(Value *V, const Type *Ty, |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6328 | bool isSigned) { |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6329 | if (Constant *C = dyn_cast<Constant>(V)) |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 6330 | return ConstantExpr::getIntegerCast(C, Ty, isSigned /*Sext or ZExt*/); |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6331 | |
| 6332 | // Otherwise, it must be an instruction. |
| 6333 | Instruction *I = cast<Instruction>(V); |
Chris Lattner | 01859e8 | 2006-05-20 23:14:03 +0000 | [diff] [blame] | 6334 | Instruction *Res = 0; |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6335 | switch (I->getOpcode()) { |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6336 | case Instruction::Add: |
| 6337 | case Instruction::Sub: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6338 | case Instruction::And: |
| 6339 | case Instruction::Or: |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6340 | case Instruction::Xor: |
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 6341 | case Instruction::AShr: |
| 6342 | case Instruction::LShr: |
| 6343 | case Instruction::Shl: { |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 6344 | Value *LHS = EvaluateInDifferentType(I->getOperand(0), Ty, isSigned); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6345 | Value *RHS = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned); |
| 6346 | Res = BinaryOperator::create((Instruction::BinaryOps)I->getOpcode(), |
| 6347 | LHS, RHS, I->getName()); |
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 6348 | break; |
| 6349 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6350 | case Instruction::Trunc: |
| 6351 | case Instruction::ZExt: |
| 6352 | case Instruction::SExt: |
| 6353 | case Instruction::BitCast: |
| 6354 | // If the source type of the cast is the type we're trying for then we can |
| 6355 | // just return the source. There's no need to insert it because its not new. |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6356 | if (I->getOperand(0)->getType() == Ty) |
| 6357 | return I->getOperand(0); |
| 6358 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6359 | // Some other kind of cast, which shouldn't happen, so just .. |
| 6360 | // FALL THROUGH |
| 6361 | default: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6362 | // TODO: Can handle more cases here. |
| 6363 | assert(0 && "Unreachable!"); |
| 6364 | break; |
| 6365 | } |
| 6366 | |
| 6367 | return InsertNewInstBefore(Res, *I); |
| 6368 | } |
| 6369 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6370 | /// @brief Implement the transforms common to all CastInst visitors. |
| 6371 | Instruction *InstCombiner::commonCastTransforms(CastInst &CI) { |
Chris Lattner | 79d35b3 | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 6372 | Value *Src = CI.getOperand(0); |
| 6373 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6374 | // Casting undef to anything results in undef so might as just replace it and |
| 6375 | // get rid of the cast. |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 6376 | if (isa<UndefValue>(Src)) // cast undef -> undef |
| 6377 | return ReplaceInstUsesWith(CI, UndefValue::get(CI.getType())); |
| 6378 | |
Dan Gohman | 23d9d27 | 2007-05-11 21:10:54 +0000 | [diff] [blame] | 6379 | // 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] | 6380 | // eliminate it now. |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 6381 | if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6382 | if (Instruction::CastOps opc = |
| 6383 | isEliminableCastPair(CSrc, CI.getOpcode(), CI.getType(), TD)) { |
| 6384 | // The first cast (CSrc) is eliminable so we need to fix up or replace |
| 6385 | // the second cast (CI). CSrc will then have a good chance of being dead. |
| 6386 | return CastInst::create(opc, CSrc->getOperand(0), CI.getType()); |
Chris Lattner | 8fd217c | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 6387 | } |
| 6388 | } |
Chris Lattner | a710ddc | 2004-05-25 04:29:21 +0000 | [diff] [blame] | 6389 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6390 | // 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] | 6391 | if (SelectInst *SI = dyn_cast<SelectInst>(Src)) |
| 6392 | if (Instruction *NV = FoldOpIntoSelect(CI, SI, this)) |
| 6393 | return NV; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6394 | |
| 6395 | // 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] | 6396 | if (isa<PHINode>(Src)) |
| 6397 | if (Instruction *NV = FoldOpIntoPhi(CI)) |
| 6398 | return NV; |
Chris Lattner | 9fb9213 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 6399 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6400 | return 0; |
| 6401 | } |
| 6402 | |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 6403 | /// @brief Implement the transforms for cast of pointer (bitcast/ptrtoint) |
| 6404 | Instruction *InstCombiner::commonPointerCastTransforms(CastInst &CI) { |
| 6405 | Value *Src = CI.getOperand(0); |
| 6406 | |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 6407 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Src)) { |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 6408 | // If casting the result of a getelementptr instruction with no offset, turn |
| 6409 | // this into a cast of the original pointer! |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 6410 | if (GEP->hasAllZeroIndices()) { |
| 6411 | // Changing the cast operand is usually not a good idea but it is safe |
| 6412 | // here because the pointer operand is being replaced with another |
| 6413 | // pointer operand so the opcode doesn't need to change. |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 6414 | AddToWorkList(GEP); |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 6415 | CI.setOperand(0, GEP->getOperand(0)); |
| 6416 | return &CI; |
| 6417 | } |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 6418 | |
| 6419 | // If the GEP has a single use, and the base pointer is a bitcast, and the |
| 6420 | // GEP computes a constant offset, see if we can convert these three |
| 6421 | // instructions into fewer. This typically happens with unions and other |
| 6422 | // non-type-safe code. |
| 6423 | if (GEP->hasOneUse() && isa<BitCastInst>(GEP->getOperand(0))) { |
| 6424 | if (GEP->hasAllConstantIndices()) { |
| 6425 | // We are guaranteed to get a constant from EmitGEPOffset. |
| 6426 | ConstantInt *OffsetV = cast<ConstantInt>(EmitGEPOffset(GEP, CI, *this)); |
| 6427 | int64_t Offset = OffsetV->getSExtValue(); |
| 6428 | |
| 6429 | // Get the base pointer input of the bitcast, and the type it points to. |
| 6430 | Value *OrigBase = cast<BitCastInst>(GEP->getOperand(0))->getOperand(0); |
| 6431 | const Type *GEPIdxTy = |
| 6432 | cast<PointerType>(OrigBase->getType())->getElementType(); |
| 6433 | if (GEPIdxTy->isSized()) { |
| 6434 | SmallVector<Value*, 8> NewIndices; |
| 6435 | |
Chris Lattner | c42e226 | 2007-05-05 01:59:31 +0000 | [diff] [blame] | 6436 | // Start with the index over the outer type. Note that the type size |
| 6437 | // might be zero (even if the offset isn't zero) if the indexed type |
| 6438 | // is something like [0 x {int, int}] |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 6439 | const Type *IntPtrTy = TD->getIntPtrType(); |
Chris Lattner | c42e226 | 2007-05-05 01:59:31 +0000 | [diff] [blame] | 6440 | int64_t FirstIdx = 0; |
| 6441 | if (int64_t TySize = TD->getTypeSize(GEPIdxTy)) { |
| 6442 | FirstIdx = Offset/TySize; |
| 6443 | Offset %= TySize; |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 6444 | |
Chris Lattner | c42e226 | 2007-05-05 01:59:31 +0000 | [diff] [blame] | 6445 | // Handle silly modulus not returning values values [0..TySize). |
| 6446 | if (Offset < 0) { |
| 6447 | --FirstIdx; |
| 6448 | Offset += TySize; |
| 6449 | assert(Offset >= 0); |
| 6450 | } |
Chris Lattner | d717c18 | 2007-05-05 22:32:24 +0000 | [diff] [blame] | 6451 | assert((uint64_t)Offset < (uint64_t)TySize &&"Out of range offset"); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 6452 | } |
| 6453 | |
| 6454 | NewIndices.push_back(ConstantInt::get(IntPtrTy, FirstIdx)); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 6455 | |
| 6456 | // Index into the types. If we fail, set OrigBase to null. |
| 6457 | while (Offset) { |
| 6458 | if (const StructType *STy = dyn_cast<StructType>(GEPIdxTy)) { |
| 6459 | const StructLayout *SL = TD->getStructLayout(STy); |
Chris Lattner | 6b6aef8 | 2007-05-15 00:16:00 +0000 | [diff] [blame] | 6460 | if (Offset < (int64_t)SL->getSizeInBytes()) { |
| 6461 | unsigned Elt = SL->getElementContainingOffset(Offset); |
| 6462 | NewIndices.push_back(ConstantInt::get(Type::Int32Ty, Elt)); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 6463 | |
Chris Lattner | 6b6aef8 | 2007-05-15 00:16:00 +0000 | [diff] [blame] | 6464 | Offset -= SL->getElementOffset(Elt); |
| 6465 | GEPIdxTy = STy->getElementType(Elt); |
| 6466 | } else { |
| 6467 | // Otherwise, we can't index into this, bail out. |
| 6468 | Offset = 0; |
| 6469 | OrigBase = 0; |
| 6470 | } |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 6471 | } else if (isa<ArrayType>(GEPIdxTy) || isa<VectorType>(GEPIdxTy)) { |
| 6472 | const SequentialType *STy = cast<SequentialType>(GEPIdxTy); |
Chris Lattner | 6b6aef8 | 2007-05-15 00:16:00 +0000 | [diff] [blame] | 6473 | if (uint64_t EltSize = TD->getTypeSize(STy->getElementType())) { |
| 6474 | NewIndices.push_back(ConstantInt::get(IntPtrTy,Offset/EltSize)); |
| 6475 | Offset %= EltSize; |
| 6476 | } else { |
| 6477 | NewIndices.push_back(ConstantInt::get(IntPtrTy, 0)); |
| 6478 | } |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 6479 | GEPIdxTy = STy->getElementType(); |
| 6480 | } else { |
| 6481 | // Otherwise, we can't index into this, bail out. |
| 6482 | Offset = 0; |
| 6483 | OrigBase = 0; |
| 6484 | } |
| 6485 | } |
| 6486 | if (OrigBase) { |
| 6487 | // If we were able to index down into an element, create the GEP |
| 6488 | // and bitcast the result. This eliminates one bitcast, potentially |
| 6489 | // two. |
| 6490 | Instruction *NGEP = new GetElementPtrInst(OrigBase, &NewIndices[0], |
| 6491 | NewIndices.size(), ""); |
| 6492 | InsertNewInstBefore(NGEP, CI); |
| 6493 | NGEP->takeName(GEP); |
| 6494 | |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 6495 | if (isa<BitCastInst>(CI)) |
| 6496 | return new BitCastInst(NGEP, CI.getType()); |
| 6497 | assert(isa<PtrToIntInst>(CI)); |
| 6498 | return new PtrToIntInst(NGEP, CI.getType()); |
| 6499 | } |
| 6500 | } |
| 6501 | } |
| 6502 | } |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 6503 | } |
| 6504 | |
| 6505 | return commonCastTransforms(CI); |
| 6506 | } |
| 6507 | |
| 6508 | |
| 6509 | |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6510 | /// Only the TRUNC, ZEXT, SEXT, and BITCAST can both operand and result as |
| 6511 | /// integer types. This function implements the common transforms for all those |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6512 | /// cases. |
| 6513 | /// @brief Implement the transforms common to CastInst with integer operands |
| 6514 | Instruction *InstCombiner::commonIntCastTransforms(CastInst &CI) { |
| 6515 | if (Instruction *Result = commonCastTransforms(CI)) |
| 6516 | return Result; |
| 6517 | |
| 6518 | Value *Src = CI.getOperand(0); |
| 6519 | const Type *SrcTy = Src->getType(); |
| 6520 | const Type *DestTy = CI.getType(); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 6521 | uint32_t SrcBitSize = SrcTy->getPrimitiveSizeInBits(); |
| 6522 | uint32_t DestBitSize = DestTy->getPrimitiveSizeInBits(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6523 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6524 | // See if we can simplify any instructions used by the LHS whose sole |
| 6525 | // purpose is to compute bits we don't care about. |
Reid Spencer | ad6676e | 2007-03-22 20:56:53 +0000 | [diff] [blame] | 6526 | APInt KnownZero(DestBitSize, 0), KnownOne(DestBitSize, 0); |
| 6527 | if (SimplifyDemandedBits(&CI, APInt::getAllOnesValue(DestBitSize), |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6528 | KnownZero, KnownOne)) |
| 6529 | return &CI; |
| 6530 | |
| 6531 | // If the source isn't an instruction or has more than one use then we |
| 6532 | // can't do anything more. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6533 | Instruction *SrcI = dyn_cast<Instruction>(Src); |
| 6534 | if (!SrcI || !Src->hasOneUse()) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6535 | return 0; |
| 6536 | |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6537 | // Attempt to propagate the cast into the instruction for int->int casts. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6538 | int NumCastsRemoved = 0; |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6539 | if (!isa<BitCastInst>(CI) && |
| 6540 | CanEvaluateInDifferentType(SrcI, cast<IntegerType>(DestTy), |
| 6541 | NumCastsRemoved)) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6542 | // If this cast is a truncate, evaluting in a different type always |
| 6543 | // eliminates the cast, so it is always a win. If this is a noop-cast |
| 6544 | // this just removes a noop cast which isn't pointful, but simplifies |
| 6545 | // the code. If this is a zero-extension, we need to do an AND to |
| 6546 | // maintain the clear top-part of the computation, so we require that |
| 6547 | // the input have eliminated at least one cast. If this is a sign |
| 6548 | // extension, we insert two new casts (to do the extension) so we |
| 6549 | // require that two casts have been eliminated. |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6550 | bool DoXForm; |
| 6551 | switch (CI.getOpcode()) { |
| 6552 | default: |
| 6553 | // All the others use floating point so we shouldn't actually |
| 6554 | // get here because of the check above. |
| 6555 | assert(0 && "Unknown cast type"); |
| 6556 | case Instruction::Trunc: |
| 6557 | DoXForm = true; |
| 6558 | break; |
| 6559 | case Instruction::ZExt: |
| 6560 | DoXForm = NumCastsRemoved >= 1; |
| 6561 | break; |
| 6562 | case Instruction::SExt: |
| 6563 | DoXForm = NumCastsRemoved >= 2; |
| 6564 | break; |
| 6565 | case Instruction::BitCast: |
| 6566 | DoXForm = false; |
| 6567 | break; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6568 | } |
| 6569 | |
| 6570 | if (DoXForm) { |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 6571 | Value *Res = EvaluateInDifferentType(SrcI, DestTy, |
| 6572 | CI.getOpcode() == Instruction::SExt); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6573 | assert(Res->getType() == DestTy); |
| 6574 | switch (CI.getOpcode()) { |
| 6575 | default: assert(0 && "Unknown cast type!"); |
| 6576 | case Instruction::Trunc: |
| 6577 | case Instruction::BitCast: |
| 6578 | // Just replace this cast with the result. |
| 6579 | return ReplaceInstUsesWith(CI, Res); |
| 6580 | case Instruction::ZExt: { |
| 6581 | // We need to emit an AND to clear the high bits. |
| 6582 | assert(SrcBitSize < DestBitSize && "Not a zext?"); |
Chris Lattner | cd1d6d5 | 2007-04-02 05:48:58 +0000 | [diff] [blame] | 6583 | Constant *C = ConstantInt::get(APInt::getLowBitsSet(DestBitSize, |
| 6584 | SrcBitSize)); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6585 | return BinaryOperator::createAnd(Res, C); |
| 6586 | } |
| 6587 | case Instruction::SExt: |
| 6588 | // We need to emit a cast to truncate, then a cast to sext. |
| 6589 | return CastInst::create(Instruction::SExt, |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 6590 | InsertCastBefore(Instruction::Trunc, Res, Src->getType(), |
| 6591 | CI), DestTy); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6592 | } |
| 6593 | } |
| 6594 | } |
| 6595 | |
| 6596 | Value *Op0 = SrcI->getNumOperands() > 0 ? SrcI->getOperand(0) : 0; |
| 6597 | Value *Op1 = SrcI->getNumOperands() > 1 ? SrcI->getOperand(1) : 0; |
| 6598 | |
| 6599 | switch (SrcI->getOpcode()) { |
| 6600 | case Instruction::Add: |
| 6601 | case Instruction::Mul: |
| 6602 | case Instruction::And: |
| 6603 | case Instruction::Or: |
| 6604 | case Instruction::Xor: |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6605 | // If we are discarding information, rewrite. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6606 | if (DestBitSize <= SrcBitSize && DestBitSize != 1) { |
| 6607 | // Don't insert two casts if they cannot be eliminated. We allow |
| 6608 | // two casts to be inserted if the sizes are the same. This could |
| 6609 | // only be converting signedness, which is a noop. |
| 6610 | if (DestBitSize == SrcBitSize || |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6611 | !ValueRequiresCast(CI.getOpcode(), Op1, DestTy,TD) || |
| 6612 | !ValueRequiresCast(CI.getOpcode(), Op0, DestTy, TD)) { |
Reid Spencer | 7eb7638 | 2006-12-13 17:19:09 +0000 | [diff] [blame] | 6613 | Instruction::CastOps opcode = CI.getOpcode(); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 6614 | Value *Op0c = InsertOperandCastBefore(opcode, Op0, DestTy, SrcI); |
| 6615 | Value *Op1c = InsertOperandCastBefore(opcode, Op1, DestTy, SrcI); |
| 6616 | return BinaryOperator::create( |
| 6617 | cast<BinaryOperator>(SrcI)->getOpcode(), Op0c, Op1c); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6618 | } |
| 6619 | } |
| 6620 | |
| 6621 | // cast (xor bool X, true) to int --> xor (cast bool X to int), 1 |
| 6622 | if (isa<ZExtInst>(CI) && SrcBitSize == 1 && |
| 6623 | SrcI->getOpcode() == Instruction::Xor && |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6624 | Op1 == ConstantInt::getTrue() && |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6625 | (!Op0->hasOneUse() || !isa<CmpInst>(Op0))) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 6626 | Value *New = InsertOperandCastBefore(Instruction::ZExt, Op0, DestTy, &CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6627 | return BinaryOperator::createXor(New, ConstantInt::get(CI.getType(), 1)); |
| 6628 | } |
| 6629 | break; |
| 6630 | case Instruction::SDiv: |
| 6631 | case Instruction::UDiv: |
| 6632 | case Instruction::SRem: |
| 6633 | case Instruction::URem: |
| 6634 | // If we are just changing the sign, rewrite. |
| 6635 | if (DestBitSize == SrcBitSize) { |
| 6636 | // Don't insert two casts if they cannot be eliminated. We allow |
| 6637 | // two casts to be inserted if the sizes are the same. This could |
| 6638 | // only be converting signedness, which is a noop. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6639 | if (!ValueRequiresCast(CI.getOpcode(), Op1, DestTy, TD) || |
| 6640 | !ValueRequiresCast(CI.getOpcode(), Op0, DestTy, TD)) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 6641 | Value *Op0c = InsertOperandCastBefore(Instruction::BitCast, |
| 6642 | Op0, DestTy, SrcI); |
| 6643 | Value *Op1c = InsertOperandCastBefore(Instruction::BitCast, |
| 6644 | Op1, DestTy, SrcI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6645 | return BinaryOperator::create( |
| 6646 | cast<BinaryOperator>(SrcI)->getOpcode(), Op0c, Op1c); |
| 6647 | } |
| 6648 | } |
| 6649 | break; |
| 6650 | |
| 6651 | case Instruction::Shl: |
| 6652 | // Allow changing the sign of the source operand. Do not allow |
| 6653 | // changing the size of the shift, UNLESS the shift amount is a |
| 6654 | // constant. We must not change variable sized shifts to a smaller |
| 6655 | // size, because it is undefined to shift more bits out than exist |
| 6656 | // in the value. |
| 6657 | if (DestBitSize == SrcBitSize || |
| 6658 | (DestBitSize < SrcBitSize && isa<Constant>(Op1))) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 6659 | Instruction::CastOps opcode = (DestBitSize == SrcBitSize ? |
| 6660 | Instruction::BitCast : Instruction::Trunc); |
| 6661 | Value *Op0c = InsertOperandCastBefore(opcode, Op0, DestTy, SrcI); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6662 | Value *Op1c = InsertOperandCastBefore(opcode, Op1, DestTy, SrcI); |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 6663 | return BinaryOperator::createShl(Op0c, Op1c); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6664 | } |
| 6665 | break; |
| 6666 | case Instruction::AShr: |
| 6667 | // If this is a signed shr, and if all bits shifted in are about to be |
| 6668 | // truncated off, turn it into an unsigned shr to allow greater |
| 6669 | // simplifications. |
| 6670 | if (DestBitSize < SrcBitSize && |
| 6671 | isa<ConstantInt>(Op1)) { |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 6672 | uint32_t ShiftAmt = cast<ConstantInt>(Op1)->getLimitedValue(SrcBitSize); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6673 | if (SrcBitSize > ShiftAmt && SrcBitSize-ShiftAmt >= DestBitSize) { |
| 6674 | // Insert the new logical shift right. |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 6675 | return BinaryOperator::createLShr(Op0, Op1); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6676 | } |
| 6677 | } |
| 6678 | break; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6679 | } |
| 6680 | return 0; |
| 6681 | } |
| 6682 | |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 6683 | Instruction *InstCombiner::visitTrunc(TruncInst &CI) { |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 6684 | if (Instruction *Result = commonIntCastTransforms(CI)) |
| 6685 | return Result; |
| 6686 | |
| 6687 | Value *Src = CI.getOperand(0); |
| 6688 | const Type *Ty = CI.getType(); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 6689 | uint32_t DestBitWidth = Ty->getPrimitiveSizeInBits(); |
| 6690 | uint32_t SrcBitWidth = cast<IntegerType>(Src->getType())->getBitWidth(); |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 6691 | |
| 6692 | if (Instruction *SrcI = dyn_cast<Instruction>(Src)) { |
| 6693 | switch (SrcI->getOpcode()) { |
| 6694 | default: break; |
| 6695 | case Instruction::LShr: |
| 6696 | // We can shrink lshr to something smaller if we know the bits shifted in |
| 6697 | // are already zeros. |
| 6698 | if (ConstantInt *ShAmtV = dyn_cast<ConstantInt>(SrcI->getOperand(1))) { |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 6699 | uint32_t ShAmt = ShAmtV->getLimitedValue(SrcBitWidth); |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 6700 | |
| 6701 | // Get a mask for the bits shifting in. |
Zhou Sheng | e82fca0 | 2007-03-28 09:19:01 +0000 | [diff] [blame] | 6702 | APInt Mask(APInt::getLowBitsSet(SrcBitWidth, ShAmt).shl(DestBitWidth)); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 6703 | Value* SrcIOp0 = SrcI->getOperand(0); |
| 6704 | if (SrcI->hasOneUse() && MaskedValueIsZero(SrcIOp0, Mask)) { |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 6705 | if (ShAmt >= DestBitWidth) // All zeros. |
| 6706 | return ReplaceInstUsesWith(CI, Constant::getNullValue(Ty)); |
| 6707 | |
| 6708 | // Okay, we can shrink this. Truncate the input, then return a new |
| 6709 | // shift. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6710 | Value *V1 = InsertCastBefore(Instruction::Trunc, SrcIOp0, Ty, CI); |
| 6711 | Value *V2 = InsertCastBefore(Instruction::Trunc, SrcI->getOperand(1), |
| 6712 | Ty, CI); |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 6713 | return BinaryOperator::createLShr(V1, V2); |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 6714 | } |
Chris Lattner | e13ab2a | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 6715 | } else { // This is a variable shr. |
| 6716 | |
| 6717 | // Turn 'trunc (lshr X, Y) to bool' into '(X & (1 << Y)) != 0'. This is |
| 6718 | // more LLVM instructions, but allows '1 << Y' to be hoisted if |
| 6719 | // loop-invariant and CSE'd. |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 6720 | if (CI.getType() == Type::Int1Ty && SrcI->hasOneUse()) { |
Chris Lattner | e13ab2a | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 6721 | Value *One = ConstantInt::get(SrcI->getType(), 1); |
| 6722 | |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6723 | Value *V = InsertNewInstBefore( |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 6724 | BinaryOperator::createShl(One, SrcI->getOperand(1), |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6725 | "tmp"), CI); |
Chris Lattner | e13ab2a | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 6726 | V = InsertNewInstBefore(BinaryOperator::createAnd(V, |
| 6727 | SrcI->getOperand(0), |
| 6728 | "tmp"), CI); |
| 6729 | Value *Zero = Constant::getNullValue(V->getType()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6730 | return new ICmpInst(ICmpInst::ICMP_NE, V, Zero); |
Chris Lattner | e13ab2a | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 6731 | } |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 6732 | } |
| 6733 | break; |
| 6734 | } |
| 6735 | } |
| 6736 | |
| 6737 | return 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6738 | } |
| 6739 | |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 6740 | Instruction *InstCombiner::visitZExt(ZExtInst &CI) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6741 | // If one of the common conversion will work .. |
| 6742 | if (Instruction *Result = commonIntCastTransforms(CI)) |
| 6743 | return Result; |
| 6744 | |
| 6745 | Value *Src = CI.getOperand(0); |
| 6746 | |
| 6747 | // If this is a cast of a cast |
| 6748 | if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6749 | // If this is a TRUNC followed by a ZEXT then we are dealing with integral |
| 6750 | // types and if the sizes are just right we can convert this into a logical |
| 6751 | // 'and' which will be much cheaper than the pair of casts. |
| 6752 | if (isa<TruncInst>(CSrc)) { |
| 6753 | // Get the sizes of the types involved |
| 6754 | Value *A = CSrc->getOperand(0); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 6755 | uint32_t SrcSize = A->getType()->getPrimitiveSizeInBits(); |
| 6756 | uint32_t MidSize = CSrc->getType()->getPrimitiveSizeInBits(); |
| 6757 | uint32_t DstSize = CI.getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6758 | // If we're actually extending zero bits and the trunc is a no-op |
| 6759 | if (MidSize < DstSize && SrcSize == DstSize) { |
| 6760 | // Replace both of the casts with an And of the type mask. |
Zhou Sheng | e82fca0 | 2007-03-28 09:19:01 +0000 | [diff] [blame] | 6761 | APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize)); |
Reid Spencer | ad6676e | 2007-03-22 20:56:53 +0000 | [diff] [blame] | 6762 | Constant *AndConst = ConstantInt::get(AndValue); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6763 | Instruction *And = |
| 6764 | BinaryOperator::createAnd(CSrc->getOperand(0), AndConst); |
| 6765 | // Unfortunately, if the type changed, we need to cast it back. |
| 6766 | if (And->getType() != CI.getType()) { |
| 6767 | And->setName(CSrc->getName()+".mask"); |
| 6768 | InsertNewInstBefore(And, CI); |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 6769 | And = CastInst::createIntegerCast(And, CI.getType(), false/*ZExt*/); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6770 | } |
| 6771 | return And; |
| 6772 | } |
| 6773 | } |
| 6774 | } |
| 6775 | |
Chris Lattner | 66bc325 | 2007-04-11 05:45:39 +0000 | [diff] [blame] | 6776 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(Src)) { |
| 6777 | // If we are just checking for a icmp eq of a single bit and zext'ing it |
| 6778 | // to an integer, then shift the bit to the appropriate place and then |
| 6779 | // cast to integer to avoid the comparison. |
| 6780 | if (ConstantInt *Op1C = dyn_cast<ConstantInt>(ICI->getOperand(1))) { |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 6781 | const APInt &Op1CV = Op1C->getValue(); |
Chris Lattner | a2e2c9b | 2007-04-11 06:53:04 +0000 | [diff] [blame] | 6782 | |
| 6783 | // zext (x <s 0) to i32 --> x>>u31 true if signbit set. |
| 6784 | // zext (x >s -1) to i32 --> (x>>u31)^1 true if signbit clear. |
| 6785 | if ((ICI->getPredicate() == ICmpInst::ICMP_SLT && Op1CV == 0) || |
| 6786 | (ICI->getPredicate() == ICmpInst::ICMP_SGT &&Op1CV.isAllOnesValue())){ |
| 6787 | Value *In = ICI->getOperand(0); |
| 6788 | Value *Sh = ConstantInt::get(In->getType(), |
| 6789 | In->getType()->getPrimitiveSizeInBits()-1); |
| 6790 | In = InsertNewInstBefore(BinaryOperator::createLShr(In, Sh, |
Chris Lattner | e34e9a2 | 2007-04-14 23:32:02 +0000 | [diff] [blame] | 6791 | In->getName()+".lobit"), |
Chris Lattner | a2e2c9b | 2007-04-11 06:53:04 +0000 | [diff] [blame] | 6792 | CI); |
| 6793 | if (In->getType() != CI.getType()) |
| 6794 | In = CastInst::createIntegerCast(In, CI.getType(), |
| 6795 | false/*ZExt*/, "tmp", &CI); |
| 6796 | |
| 6797 | if (ICI->getPredicate() == ICmpInst::ICMP_SGT) { |
| 6798 | Constant *One = ConstantInt::get(In->getType(), 1); |
| 6799 | In = InsertNewInstBefore(BinaryOperator::createXor(In, One, |
Chris Lattner | e34e9a2 | 2007-04-14 23:32:02 +0000 | [diff] [blame] | 6800 | In->getName()+".not"), |
Chris Lattner | a2e2c9b | 2007-04-11 06:53:04 +0000 | [diff] [blame] | 6801 | CI); |
| 6802 | } |
| 6803 | |
| 6804 | return ReplaceInstUsesWith(CI, In); |
| 6805 | } |
| 6806 | |
| 6807 | |
| 6808 | |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 6809 | // zext (X == 0) to i32 --> X^1 iff X has only the low bit set. |
| 6810 | // zext (X == 0) to i32 --> (X>>1)^1 iff X has only the 2nd bit set. |
| 6811 | // zext (X == 1) to i32 --> X iff X has only the low bit set. |
| 6812 | // zext (X == 2) to i32 --> X>>1 iff X has only the 2nd bit set. |
| 6813 | // zext (X != 0) to i32 --> X iff X has only the low bit set. |
| 6814 | // zext (X != 0) to i32 --> X>>1 iff X has only the 2nd bit set. |
| 6815 | // zext (X != 1) to i32 --> X^1 iff X has only the low bit set. |
| 6816 | // zext (X != 2) to i32 --> (X>>1)^1 iff X has only the 2nd bit set. |
Chris Lattner | 66bc325 | 2007-04-11 05:45:39 +0000 | [diff] [blame] | 6817 | if ((Op1CV == 0 || Op1CV.isPowerOf2()) && |
| 6818 | // This only works for EQ and NE |
| 6819 | ICI->isEquality()) { |
| 6820 | // If Op1C some other power of two, convert: |
| 6821 | uint32_t BitWidth = Op1C->getType()->getBitWidth(); |
| 6822 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 6823 | APInt TypeMask(APInt::getAllOnesValue(BitWidth)); |
| 6824 | ComputeMaskedBits(ICI->getOperand(0), TypeMask, KnownZero, KnownOne); |
| 6825 | |
| 6826 | APInt KnownZeroMask(~KnownZero); |
| 6827 | if (KnownZeroMask.isPowerOf2()) { // Exactly 1 possible 1? |
| 6828 | bool isNE = ICI->getPredicate() == ICmpInst::ICMP_NE; |
| 6829 | if (Op1CV != 0 && (Op1CV != KnownZeroMask)) { |
| 6830 | // (X&4) == 2 --> false |
| 6831 | // (X&4) != 2 --> true |
| 6832 | Constant *Res = ConstantInt::get(Type::Int1Ty, isNE); |
| 6833 | Res = ConstantExpr::getZExt(Res, CI.getType()); |
| 6834 | return ReplaceInstUsesWith(CI, Res); |
| 6835 | } |
| 6836 | |
| 6837 | uint32_t ShiftAmt = KnownZeroMask.logBase2(); |
| 6838 | Value *In = ICI->getOperand(0); |
| 6839 | if (ShiftAmt) { |
| 6840 | // Perform a logical shr by shiftamt. |
| 6841 | // Insert the shift to put the result in the low bit. |
| 6842 | In = InsertNewInstBefore( |
| 6843 | BinaryOperator::createLShr(In, |
| 6844 | ConstantInt::get(In->getType(), ShiftAmt), |
| 6845 | In->getName()+".lobit"), CI); |
| 6846 | } |
| 6847 | |
| 6848 | if ((Op1CV != 0) == isNE) { // Toggle the low bit. |
| 6849 | Constant *One = ConstantInt::get(In->getType(), 1); |
| 6850 | In = BinaryOperator::createXor(In, One, "tmp"); |
| 6851 | InsertNewInstBefore(cast<Instruction>(In), CI); |
| 6852 | } |
| 6853 | |
| 6854 | if (CI.getType() == In->getType()) |
| 6855 | return ReplaceInstUsesWith(CI, In); |
| 6856 | else |
| 6857 | return CastInst::createIntegerCast(In, CI.getType(), false/*ZExt*/); |
| 6858 | } |
| 6859 | } |
| 6860 | } |
| 6861 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6862 | return 0; |
| 6863 | } |
| 6864 | |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 6865 | Instruction *InstCombiner::visitSExt(SExtInst &CI) { |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 6866 | if (Instruction *I = commonIntCastTransforms(CI)) |
| 6867 | return I; |
| 6868 | |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 6869 | Value *Src = CI.getOperand(0); |
| 6870 | |
| 6871 | // sext (x <s 0) -> ashr x, 31 -> all ones if signed |
| 6872 | // sext (x >s -1) -> ashr x, 31 -> all ones if not signed |
| 6873 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(Src)) { |
| 6874 | // If we are just checking for a icmp eq of a single bit and zext'ing it |
| 6875 | // to an integer, then shift the bit to the appropriate place and then |
| 6876 | // cast to integer to avoid the comparison. |
| 6877 | if (ConstantInt *Op1C = dyn_cast<ConstantInt>(ICI->getOperand(1))) { |
| 6878 | const APInt &Op1CV = Op1C->getValue(); |
| 6879 | |
| 6880 | // sext (x <s 0) to i32 --> x>>s31 true if signbit set. |
| 6881 | // sext (x >s -1) to i32 --> (x>>s31)^-1 true if signbit clear. |
| 6882 | if ((ICI->getPredicate() == ICmpInst::ICMP_SLT && Op1CV == 0) || |
| 6883 | (ICI->getPredicate() == ICmpInst::ICMP_SGT &&Op1CV.isAllOnesValue())){ |
| 6884 | Value *In = ICI->getOperand(0); |
| 6885 | Value *Sh = ConstantInt::get(In->getType(), |
| 6886 | In->getType()->getPrimitiveSizeInBits()-1); |
| 6887 | In = InsertNewInstBefore(BinaryOperator::createAShr(In, Sh, |
Chris Lattner | e34e9a2 | 2007-04-14 23:32:02 +0000 | [diff] [blame] | 6888 | In->getName()+".lobit"), |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 6889 | CI); |
| 6890 | if (In->getType() != CI.getType()) |
| 6891 | In = CastInst::createIntegerCast(In, CI.getType(), |
| 6892 | true/*SExt*/, "tmp", &CI); |
| 6893 | |
| 6894 | if (ICI->getPredicate() == ICmpInst::ICMP_SGT) |
| 6895 | In = InsertNewInstBefore(BinaryOperator::createNot(In, |
| 6896 | In->getName()+".not"), CI); |
| 6897 | |
| 6898 | return ReplaceInstUsesWith(CI, In); |
| 6899 | } |
| 6900 | } |
| 6901 | } |
| 6902 | |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 6903 | return 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6904 | } |
| 6905 | |
| 6906 | Instruction *InstCombiner::visitFPTrunc(CastInst &CI) { |
| 6907 | return commonCastTransforms(CI); |
| 6908 | } |
| 6909 | |
| 6910 | Instruction *InstCombiner::visitFPExt(CastInst &CI) { |
| 6911 | return commonCastTransforms(CI); |
| 6912 | } |
| 6913 | |
| 6914 | Instruction *InstCombiner::visitFPToUI(CastInst &CI) { |
Reid Spencer | 44c030a | 2006-11-30 23:13:36 +0000 | [diff] [blame] | 6915 | return commonCastTransforms(CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6916 | } |
| 6917 | |
| 6918 | Instruction *InstCombiner::visitFPToSI(CastInst &CI) { |
Reid Spencer | 44c030a | 2006-11-30 23:13:36 +0000 | [diff] [blame] | 6919 | return commonCastTransforms(CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6920 | } |
| 6921 | |
| 6922 | Instruction *InstCombiner::visitUIToFP(CastInst &CI) { |
| 6923 | return commonCastTransforms(CI); |
| 6924 | } |
| 6925 | |
| 6926 | Instruction *InstCombiner::visitSIToFP(CastInst &CI) { |
| 6927 | return commonCastTransforms(CI); |
| 6928 | } |
| 6929 | |
| 6930 | Instruction *InstCombiner::visitPtrToInt(CastInst &CI) { |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 6931 | return commonPointerCastTransforms(CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6932 | } |
| 6933 | |
| 6934 | Instruction *InstCombiner::visitIntToPtr(CastInst &CI) { |
| 6935 | return commonCastTransforms(CI); |
| 6936 | } |
| 6937 | |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 6938 | Instruction *InstCombiner::visitBitCast(BitCastInst &CI) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6939 | // If the operands are integer typed then apply the integer transforms, |
| 6940 | // otherwise just apply the common ones. |
| 6941 | Value *Src = CI.getOperand(0); |
| 6942 | const Type *SrcTy = Src->getType(); |
| 6943 | const Type *DestTy = CI.getType(); |
| 6944 | |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 6945 | if (SrcTy->isInteger() && DestTy->isInteger()) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6946 | if (Instruction *Result = commonIntCastTransforms(CI)) |
| 6947 | return Result; |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 6948 | } else if (isa<PointerType>(SrcTy)) { |
| 6949 | if (Instruction *I = commonPointerCastTransforms(CI)) |
| 6950 | return I; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6951 | } else { |
| 6952 | if (Instruction *Result = commonCastTransforms(CI)) |
| 6953 | return Result; |
| 6954 | } |
| 6955 | |
| 6956 | |
| 6957 | // Get rid of casts from one type to the same type. These are useless and can |
| 6958 | // be replaced by the operand. |
| 6959 | if (DestTy == Src->getType()) |
| 6960 | return ReplaceInstUsesWith(CI, Src); |
| 6961 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6962 | if (const PointerType *DstPTy = dyn_cast<PointerType>(DestTy)) { |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 6963 | const PointerType *SrcPTy = cast<PointerType>(SrcTy); |
| 6964 | const Type *DstElTy = DstPTy->getElementType(); |
| 6965 | const Type *SrcElTy = SrcPTy->getElementType(); |
| 6966 | |
| 6967 | // If we are casting a malloc or alloca to a pointer to a type of the same |
| 6968 | // size, rewrite the allocation instruction to allocate the "right" type. |
| 6969 | if (AllocationInst *AI = dyn_cast<AllocationInst>(Src)) |
| 6970 | if (Instruction *V = PromoteCastOfAllocation(CI, *AI)) |
| 6971 | return V; |
| 6972 | |
Chris Lattner | d717c18 | 2007-05-05 22:32:24 +0000 | [diff] [blame] | 6973 | // If the source and destination are pointers, and this cast is equivalent |
| 6974 | // 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] | 6975 | // This can enhance SROA and other transforms that want type-safe pointers. |
| 6976 | Constant *ZeroUInt = Constant::getNullValue(Type::Int32Ty); |
| 6977 | unsigned NumZeros = 0; |
| 6978 | while (SrcElTy != DstElTy && |
| 6979 | isa<CompositeType>(SrcElTy) && !isa<PointerType>(SrcElTy) && |
| 6980 | SrcElTy->getNumContainedTypes() /* not "{}" */) { |
| 6981 | SrcElTy = cast<CompositeType>(SrcElTy)->getTypeAtIndex(ZeroUInt); |
| 6982 | ++NumZeros; |
| 6983 | } |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 6984 | |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 6985 | // If we found a path from the src to dest, create the getelementptr now. |
| 6986 | if (SrcElTy == DstElTy) { |
| 6987 | SmallVector<Value*, 8> Idxs(NumZeros+1, ZeroUInt); |
| 6988 | return new GetElementPtrInst(Src, &Idxs[0], Idxs.size()); |
Chris Lattner | 9fb9213 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 6989 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6990 | } |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 6991 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6992 | if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(Src)) { |
| 6993 | if (SVI->hasOneUse()) { |
| 6994 | // Okay, we have (bitconvert (shuffle ..)). Check to see if this is |
| 6995 | // a bitconvert to a vector with the same # elts. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 6996 | if (isa<VectorType>(DestTy) && |
| 6997 | cast<VectorType>(DestTy)->getNumElements() == |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6998 | SVI->getType()->getNumElements()) { |
| 6999 | CastInst *Tmp; |
| 7000 | // If either of the operands is a cast from CI.getType(), then |
| 7001 | // evaluating the shuffle in the casted destination's type will allow |
| 7002 | // us to eliminate at least one cast. |
| 7003 | if (((Tmp = dyn_cast<CastInst>(SVI->getOperand(0))) && |
| 7004 | Tmp->getOperand(0)->getType() == DestTy) || |
| 7005 | ((Tmp = dyn_cast<CastInst>(SVI->getOperand(1))) && |
| 7006 | Tmp->getOperand(0)->getType() == DestTy)) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7007 | Value *LHS = InsertOperandCastBefore(Instruction::BitCast, |
| 7008 | SVI->getOperand(0), DestTy, &CI); |
| 7009 | Value *RHS = InsertOperandCastBefore(Instruction::BitCast, |
| 7010 | SVI->getOperand(1), DestTy, &CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7011 | // Return a new shuffle vector. Use the same element ID's, as we |
| 7012 | // know the vector types match #elts. |
| 7013 | return new ShuffleVectorInst(LHS, RHS, SVI->getOperand(2)); |
Chris Lattner | 01575b7 | 2006-05-25 23:24:33 +0000 | [diff] [blame] | 7014 | } |
| 7015 | } |
| 7016 | } |
| 7017 | } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 7018 | return 0; |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 7019 | } |
| 7020 | |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7021 | /// GetSelectFoldableOperands - We want to turn code that looks like this: |
| 7022 | /// %C = or %A, %B |
| 7023 | /// %D = select %cond, %C, %A |
| 7024 | /// into: |
| 7025 | /// %C = select %cond, %B, 0 |
| 7026 | /// %D = or %A, %C |
| 7027 | /// |
| 7028 | /// Assuming that the specified instruction is an operand to the select, return |
| 7029 | /// a bitmask indicating which operands of this instruction are foldable if they |
| 7030 | /// equal the other incoming value of the select. |
| 7031 | /// |
| 7032 | static unsigned GetSelectFoldableOperands(Instruction *I) { |
| 7033 | switch (I->getOpcode()) { |
| 7034 | case Instruction::Add: |
| 7035 | case Instruction::Mul: |
| 7036 | case Instruction::And: |
| 7037 | case Instruction::Or: |
| 7038 | case Instruction::Xor: |
| 7039 | return 3; // Can fold through either operand. |
| 7040 | case Instruction::Sub: // Can only fold on the amount subtracted. |
| 7041 | case Instruction::Shl: // Can only fold on the shift amount. |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 7042 | case Instruction::LShr: |
| 7043 | case Instruction::AShr: |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7044 | return 1; |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7045 | default: |
| 7046 | return 0; // Cannot fold |
| 7047 | } |
| 7048 | } |
| 7049 | |
| 7050 | /// GetSelectFoldableConstant - For the same transformation as the previous |
| 7051 | /// function, return the identity constant that goes into the select. |
| 7052 | static Constant *GetSelectFoldableConstant(Instruction *I) { |
| 7053 | switch (I->getOpcode()) { |
| 7054 | default: assert(0 && "This cannot happen!"); abort(); |
| 7055 | case Instruction::Add: |
| 7056 | case Instruction::Sub: |
| 7057 | case Instruction::Or: |
| 7058 | case Instruction::Xor: |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7059 | case Instruction::Shl: |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 7060 | case Instruction::LShr: |
| 7061 | case Instruction::AShr: |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7062 | return Constant::getNullValue(I->getType()); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7063 | case Instruction::And: |
| 7064 | return ConstantInt::getAllOnesValue(I->getType()); |
| 7065 | case Instruction::Mul: |
| 7066 | return ConstantInt::get(I->getType(), 1); |
| 7067 | } |
| 7068 | } |
| 7069 | |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 7070 | /// FoldSelectOpOp - Here we have (select c, TI, FI), and we know that TI and FI |
| 7071 | /// have the same opcode and only one use each. Try to simplify this. |
| 7072 | Instruction *InstCombiner::FoldSelectOpOp(SelectInst &SI, Instruction *TI, |
| 7073 | Instruction *FI) { |
| 7074 | if (TI->getNumOperands() == 1) { |
| 7075 | // If this is a non-volatile load or a cast from the same type, |
| 7076 | // merge. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7077 | if (TI->isCast()) { |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 7078 | if (TI->getOperand(0)->getType() != FI->getOperand(0)->getType()) |
| 7079 | return 0; |
| 7080 | } else { |
| 7081 | return 0; // unknown unary op. |
| 7082 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7083 | |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 7084 | // Fold this by inserting a select from the input values. |
| 7085 | SelectInst *NewSI = new SelectInst(SI.getCondition(), TI->getOperand(0), |
| 7086 | FI->getOperand(0), SI.getName()+".v"); |
| 7087 | InsertNewInstBefore(NewSI, SI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7088 | return CastInst::create(Instruction::CastOps(TI->getOpcode()), NewSI, |
| 7089 | TI->getType()); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 7090 | } |
| 7091 | |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7092 | // Only handle binary operators here. |
| 7093 | if (!isa<BinaryOperator>(TI)) |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 7094 | return 0; |
| 7095 | |
| 7096 | // Figure out if the operations have any operands in common. |
| 7097 | Value *MatchOp, *OtherOpT, *OtherOpF; |
| 7098 | bool MatchIsOpZero; |
| 7099 | if (TI->getOperand(0) == FI->getOperand(0)) { |
| 7100 | MatchOp = TI->getOperand(0); |
| 7101 | OtherOpT = TI->getOperand(1); |
| 7102 | OtherOpF = FI->getOperand(1); |
| 7103 | MatchIsOpZero = true; |
| 7104 | } else if (TI->getOperand(1) == FI->getOperand(1)) { |
| 7105 | MatchOp = TI->getOperand(1); |
| 7106 | OtherOpT = TI->getOperand(0); |
| 7107 | OtherOpF = FI->getOperand(0); |
| 7108 | MatchIsOpZero = false; |
| 7109 | } else if (!TI->isCommutative()) { |
| 7110 | return 0; |
| 7111 | } else if (TI->getOperand(0) == FI->getOperand(1)) { |
| 7112 | MatchOp = TI->getOperand(0); |
| 7113 | OtherOpT = TI->getOperand(1); |
| 7114 | OtherOpF = FI->getOperand(0); |
| 7115 | MatchIsOpZero = true; |
| 7116 | } else if (TI->getOperand(1) == FI->getOperand(0)) { |
| 7117 | MatchOp = TI->getOperand(1); |
| 7118 | OtherOpT = TI->getOperand(0); |
| 7119 | OtherOpF = FI->getOperand(1); |
| 7120 | MatchIsOpZero = true; |
| 7121 | } else { |
| 7122 | return 0; |
| 7123 | } |
| 7124 | |
| 7125 | // If we reach here, they do have operations in common. |
| 7126 | SelectInst *NewSI = new SelectInst(SI.getCondition(), OtherOpT, |
| 7127 | OtherOpF, SI.getName()+".v"); |
| 7128 | InsertNewInstBefore(NewSI, SI); |
| 7129 | |
| 7130 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TI)) { |
| 7131 | if (MatchIsOpZero) |
| 7132 | return BinaryOperator::create(BO->getOpcode(), MatchOp, NewSI); |
| 7133 | else |
| 7134 | return BinaryOperator::create(BO->getOpcode(), NewSI, MatchOp); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 7135 | } |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 7136 | assert(0 && "Shouldn't get here"); |
| 7137 | return 0; |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 7138 | } |
| 7139 | |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 7140 | Instruction *InstCombiner::visitSelectInst(SelectInst &SI) { |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 7141 | Value *CondVal = SI.getCondition(); |
| 7142 | Value *TrueVal = SI.getTrueValue(); |
| 7143 | Value *FalseVal = SI.getFalseValue(); |
| 7144 | |
| 7145 | // select true, X, Y -> X |
| 7146 | // select false, X, Y -> Y |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 7147 | if (ConstantInt *C = dyn_cast<ConstantInt>(CondVal)) |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 7148 | return ReplaceInstUsesWith(SI, C->getZExtValue() ? TrueVal : FalseVal); |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 7149 | |
| 7150 | // select C, X, X -> X |
| 7151 | if (TrueVal == FalseVal) |
| 7152 | return ReplaceInstUsesWith(SI, TrueVal); |
| 7153 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 7154 | if (isa<UndefValue>(TrueVal)) // select C, undef, X -> X |
| 7155 | return ReplaceInstUsesWith(SI, FalseVal); |
| 7156 | if (isa<UndefValue>(FalseVal)) // select C, X, undef -> X |
| 7157 | return ReplaceInstUsesWith(SI, TrueVal); |
| 7158 | if (isa<UndefValue>(CondVal)) { // select undef, X, Y -> X or Y |
| 7159 | if (isa<Constant>(TrueVal)) |
| 7160 | return ReplaceInstUsesWith(SI, TrueVal); |
| 7161 | else |
| 7162 | return ReplaceInstUsesWith(SI, FalseVal); |
| 7163 | } |
| 7164 | |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 7165 | if (SI.getType() == Type::Int1Ty) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 7166 | if (ConstantInt *C = dyn_cast<ConstantInt>(TrueVal)) { |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 7167 | if (C->getZExtValue()) { |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 7168 | // Change: A = select B, true, C --> A = or B, C |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 7169 | return BinaryOperator::createOr(CondVal, FalseVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 7170 | } else { |
| 7171 | // Change: A = select B, false, C --> A = and !B, C |
| 7172 | Value *NotCond = |
| 7173 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
| 7174 | "not."+CondVal->getName()), SI); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 7175 | return BinaryOperator::createAnd(NotCond, FalseVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 7176 | } |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 7177 | } else if (ConstantInt *C = dyn_cast<ConstantInt>(FalseVal)) { |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 7178 | if (C->getZExtValue() == false) { |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 7179 | // Change: A = select B, C, false --> A = and B, C |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 7180 | return BinaryOperator::createAnd(CondVal, TrueVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 7181 | } else { |
| 7182 | // Change: A = select B, C, true --> A = or !B, C |
| 7183 | Value *NotCond = |
| 7184 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
| 7185 | "not."+CondVal->getName()), SI); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 7186 | return BinaryOperator::createOr(NotCond, TrueVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 7187 | } |
| 7188 | } |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 7189 | } |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 7190 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 7191 | // Selecting between two integer constants? |
| 7192 | if (ConstantInt *TrueValC = dyn_cast<ConstantInt>(TrueVal)) |
| 7193 | if (ConstantInt *FalseValC = dyn_cast<ConstantInt>(FalseVal)) { |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 7194 | // select C, 1, 0 -> zext C to int |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 7195 | if (FalseValC->isZero() && TrueValC->getValue() == 1) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7196 | return CastInst::create(Instruction::ZExt, CondVal, SI.getType()); |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 7197 | } else if (TrueValC->isZero() && FalseValC->getValue() == 1) { |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 7198 | // select C, 0, 1 -> zext !C to int |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 7199 | Value *NotCond = |
| 7200 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
Chris Lattner | 82e14fe | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 7201 | "not."+CondVal->getName()), SI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7202 | return CastInst::create(Instruction::ZExt, NotCond, SI.getType()); |
Chris Lattner | 82e14fe | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 7203 | } |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 7204 | |
| 7205 | // FIXME: Turn select 0/-1 and -1/0 into sext from condition! |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 7206 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7207 | if (ICmpInst *IC = dyn_cast<ICmpInst>(SI.getCondition())) { |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 7208 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7209 | // (x <s 0) ? -1 : 0 -> ashr x, 31 |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 7210 | if (TrueValC->isAllOnesValue() && FalseValC->isZero()) |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 7211 | if (ConstantInt *CmpCst = dyn_cast<ConstantInt>(IC->getOperand(1))) { |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 7212 | if (IC->getPredicate() == ICmpInst::ICMP_SLT && CmpCst->isZero()) { |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 7213 | // The comparison constant and the result are not neccessarily the |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7214 | // same width. Make an all-ones value by inserting a AShr. |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 7215 | Value *X = IC->getOperand(0); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 7216 | uint32_t Bits = X->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7217 | Constant *ShAmt = ConstantInt::get(X->getType(), Bits-1); |
| 7218 | Instruction *SRA = BinaryOperator::create(Instruction::AShr, X, |
| 7219 | ShAmt, "ones"); |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 7220 | InsertNewInstBefore(SRA, SI); |
| 7221 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7222 | // Finally, convert to the type of the select RHS. We figure out |
| 7223 | // if this requires a SExt, Trunc or BitCast based on the sizes. |
| 7224 | Instruction::CastOps opc = Instruction::BitCast; |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 7225 | uint32_t SRASize = SRA->getType()->getPrimitiveSizeInBits(); |
| 7226 | uint32_t SISize = SI.getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7227 | if (SRASize < SISize) |
| 7228 | opc = Instruction::SExt; |
| 7229 | else if (SRASize > SISize) |
| 7230 | opc = Instruction::Trunc; |
| 7231 | return CastInst::create(opc, SRA, SI.getType()); |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 7232 | } |
| 7233 | } |
| 7234 | |
| 7235 | |
| 7236 | // 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] | 7237 | // 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] | 7238 | // non-constant value, eliminate this whole mess. This corresponds to |
| 7239 | // cases like this: ((X & 27) ? 27 : 0) |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 7240 | if (TrueValC->isZero() || FalseValC->isZero()) |
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 7241 | if (IC->isEquality() && isa<ConstantInt>(IC->getOperand(1)) && |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 7242 | cast<Constant>(IC->getOperand(1))->isNullValue()) |
| 7243 | if (Instruction *ICA = dyn_cast<Instruction>(IC->getOperand(0))) |
| 7244 | if (ICA->getOpcode() == Instruction::And && |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7245 | isa<ConstantInt>(ICA->getOperand(1)) && |
| 7246 | (ICA->getOperand(1) == TrueValC || |
| 7247 | ICA->getOperand(1) == FalseValC) && |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 7248 | isOneBitSet(cast<ConstantInt>(ICA->getOperand(1)))) { |
| 7249 | // 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] | 7250 | // know whether we have a icmp_ne or icmp_eq and whether the |
| 7251 | // true or false val is the zero. |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 7252 | bool ShouldNotVal = !TrueValC->isZero(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7253 | ShouldNotVal ^= IC->getPredicate() == ICmpInst::ICMP_NE; |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 7254 | Value *V = ICA; |
| 7255 | if (ShouldNotVal) |
| 7256 | V = InsertNewInstBefore(BinaryOperator::create( |
| 7257 | Instruction::Xor, V, ICA->getOperand(1)), SI); |
| 7258 | return ReplaceInstUsesWith(SI, V); |
| 7259 | } |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 7260 | } |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 7261 | } |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 7262 | |
| 7263 | // 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] | 7264 | if (FCmpInst *FCI = dyn_cast<FCmpInst>(CondVal)) { |
| 7265 | if (FCI->getOperand(0) == TrueVal && FCI->getOperand(1) == FalseVal) { |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 7266 | // Transform (X == Y) ? X : Y -> Y |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7267 | if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 7268 | return ReplaceInstUsesWith(SI, FalseVal); |
| 7269 | // Transform (X != Y) ? X : Y -> X |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7270 | if (FCI->getPredicate() == FCmpInst::FCMP_ONE) |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 7271 | return ReplaceInstUsesWith(SI, TrueVal); |
| 7272 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 7273 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7274 | } else if (FCI->getOperand(0) == FalseVal && FCI->getOperand(1) == TrueVal){ |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 7275 | // Transform (X == Y) ? Y : X -> X |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7276 | if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) |
Chris Lattner | fbede52 | 2004-04-11 01:39:19 +0000 | [diff] [blame] | 7277 | return ReplaceInstUsesWith(SI, FalseVal); |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 7278 | // Transform (X != Y) ? Y : X -> Y |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7279 | if (FCI->getPredicate() == FCmpInst::FCMP_ONE) |
| 7280 | return ReplaceInstUsesWith(SI, TrueVal); |
| 7281 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 7282 | } |
| 7283 | } |
| 7284 | |
| 7285 | // See if we are selecting two values based on a comparison of the two values. |
| 7286 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(CondVal)) { |
| 7287 | if (ICI->getOperand(0) == TrueVal && ICI->getOperand(1) == FalseVal) { |
| 7288 | // Transform (X == Y) ? X : Y -> Y |
| 7289 | if (ICI->getPredicate() == ICmpInst::ICMP_EQ) |
| 7290 | return ReplaceInstUsesWith(SI, FalseVal); |
| 7291 | // Transform (X != Y) ? X : Y -> X |
| 7292 | if (ICI->getPredicate() == ICmpInst::ICMP_NE) |
| 7293 | return ReplaceInstUsesWith(SI, TrueVal); |
| 7294 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 7295 | |
| 7296 | } else if (ICI->getOperand(0) == FalseVal && ICI->getOperand(1) == TrueVal){ |
| 7297 | // Transform (X == Y) ? Y : X -> X |
| 7298 | if (ICI->getPredicate() == ICmpInst::ICMP_EQ) |
| 7299 | return ReplaceInstUsesWith(SI, FalseVal); |
| 7300 | // Transform (X != Y) ? Y : X -> Y |
| 7301 | if (ICI->getPredicate() == ICmpInst::ICMP_NE) |
Chris Lattner | fbede52 | 2004-04-11 01:39:19 +0000 | [diff] [blame] | 7302 | return ReplaceInstUsesWith(SI, TrueVal); |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 7303 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 7304 | } |
| 7305 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7306 | |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 7307 | if (Instruction *TI = dyn_cast<Instruction>(TrueVal)) |
| 7308 | if (Instruction *FI = dyn_cast<Instruction>(FalseVal)) |
| 7309 | if (TI->hasOneUse() && FI->hasOneUse()) { |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 7310 | Instruction *AddOp = 0, *SubOp = 0; |
| 7311 | |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 7312 | // Turn (select C, (op X, Y), (op X, Z)) -> (op X, (select C, Y, Z)) |
| 7313 | if (TI->getOpcode() == FI->getOpcode()) |
| 7314 | if (Instruction *IV = FoldSelectOpOp(SI, TI, FI)) |
| 7315 | return IV; |
| 7316 | |
| 7317 | // Turn select C, (X+Y), (X-Y) --> (X+(select C, Y, (-Y))). This is |
| 7318 | // even legal for FP. |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 7319 | if (TI->getOpcode() == Instruction::Sub && |
| 7320 | FI->getOpcode() == Instruction::Add) { |
| 7321 | AddOp = FI; SubOp = TI; |
| 7322 | } else if (FI->getOpcode() == Instruction::Sub && |
| 7323 | TI->getOpcode() == Instruction::Add) { |
| 7324 | AddOp = TI; SubOp = FI; |
| 7325 | } |
| 7326 | |
| 7327 | if (AddOp) { |
| 7328 | Value *OtherAddOp = 0; |
| 7329 | if (SubOp->getOperand(0) == AddOp->getOperand(0)) { |
| 7330 | OtherAddOp = AddOp->getOperand(1); |
| 7331 | } else if (SubOp->getOperand(0) == AddOp->getOperand(1)) { |
| 7332 | OtherAddOp = AddOp->getOperand(0); |
| 7333 | } |
| 7334 | |
| 7335 | if (OtherAddOp) { |
Chris Lattner | 97f37a4 | 2006-02-24 18:05:58 +0000 | [diff] [blame] | 7336 | // So at this point we know we have (Y -> OtherAddOp): |
| 7337 | // select C, (add X, Y), (sub X, Z) |
| 7338 | Value *NegVal; // Compute -Z |
| 7339 | if (Constant *C = dyn_cast<Constant>(SubOp->getOperand(1))) { |
| 7340 | NegVal = ConstantExpr::getNeg(C); |
| 7341 | } else { |
| 7342 | NegVal = InsertNewInstBefore( |
| 7343 | BinaryOperator::createNeg(SubOp->getOperand(1), "tmp"), SI); |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 7344 | } |
Chris Lattner | 97f37a4 | 2006-02-24 18:05:58 +0000 | [diff] [blame] | 7345 | |
| 7346 | Value *NewTrueOp = OtherAddOp; |
| 7347 | Value *NewFalseOp = NegVal; |
| 7348 | if (AddOp != TI) |
| 7349 | std::swap(NewTrueOp, NewFalseOp); |
| 7350 | Instruction *NewSel = |
| 7351 | new SelectInst(CondVal, NewTrueOp,NewFalseOp,SI.getName()+".p"); |
| 7352 | |
| 7353 | NewSel = InsertNewInstBefore(NewSel, SI); |
| 7354 | return BinaryOperator::createAdd(SubOp->getOperand(0), NewSel); |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 7355 | } |
| 7356 | } |
| 7357 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7358 | |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7359 | // See if we can fold the select into one of our operands. |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 7360 | if (SI.getType()->isInteger()) { |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7361 | // See the comment above GetSelectFoldableOperands for a description of the |
| 7362 | // transformation we are doing here. |
| 7363 | if (Instruction *TVI = dyn_cast<Instruction>(TrueVal)) |
| 7364 | if (TVI->hasOneUse() && TVI->getNumOperands() == 2 && |
| 7365 | !isa<Constant>(FalseVal)) |
| 7366 | if (unsigned SFO = GetSelectFoldableOperands(TVI)) { |
| 7367 | unsigned OpToFold = 0; |
| 7368 | if ((SFO & 1) && FalseVal == TVI->getOperand(0)) { |
| 7369 | OpToFold = 1; |
| 7370 | } else if ((SFO & 2) && FalseVal == TVI->getOperand(1)) { |
| 7371 | OpToFold = 2; |
| 7372 | } |
| 7373 | |
| 7374 | if (OpToFold) { |
| 7375 | Constant *C = GetSelectFoldableConstant(TVI); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7376 | Instruction *NewSel = |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 7377 | new SelectInst(SI.getCondition(), TVI->getOperand(2-OpToFold), C); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7378 | InsertNewInstBefore(NewSel, SI); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 7379 | NewSel->takeName(TVI); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7380 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TVI)) |
| 7381 | return BinaryOperator::create(BO->getOpcode(), FalseVal, NewSel); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7382 | else { |
| 7383 | assert(0 && "Unknown instruction!!"); |
| 7384 | } |
| 7385 | } |
| 7386 | } |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 7387 | |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7388 | if (Instruction *FVI = dyn_cast<Instruction>(FalseVal)) |
| 7389 | if (FVI->hasOneUse() && FVI->getNumOperands() == 2 && |
| 7390 | !isa<Constant>(TrueVal)) |
| 7391 | if (unsigned SFO = GetSelectFoldableOperands(FVI)) { |
| 7392 | unsigned OpToFold = 0; |
| 7393 | if ((SFO & 1) && TrueVal == FVI->getOperand(0)) { |
| 7394 | OpToFold = 1; |
| 7395 | } else if ((SFO & 2) && TrueVal == FVI->getOperand(1)) { |
| 7396 | OpToFold = 2; |
| 7397 | } |
| 7398 | |
| 7399 | if (OpToFold) { |
| 7400 | Constant *C = GetSelectFoldableConstant(FVI); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7401 | Instruction *NewSel = |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 7402 | new SelectInst(SI.getCondition(), C, FVI->getOperand(2-OpToFold)); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7403 | InsertNewInstBefore(NewSel, SI); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 7404 | NewSel->takeName(FVI); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7405 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(FVI)) |
| 7406 | return BinaryOperator::create(BO->getOpcode(), TrueVal, NewSel); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7407 | else |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7408 | assert(0 && "Unknown instruction!!"); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7409 | } |
| 7410 | } |
| 7411 | } |
Chris Lattner | a1df33c | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 7412 | |
| 7413 | if (BinaryOperator::isNot(CondVal)) { |
| 7414 | SI.setOperand(0, BinaryOperator::getNotArgument(CondVal)); |
| 7415 | SI.setOperand(1, FalseVal); |
| 7416 | SI.setOperand(2, TrueVal); |
| 7417 | return &SI; |
| 7418 | } |
| 7419 | |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 7420 | return 0; |
| 7421 | } |
| 7422 | |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7423 | /// GetKnownAlignment - If the specified pointer has an alignment that we can |
| 7424 | /// determine, return it, otherwise return 0. |
| 7425 | static unsigned GetKnownAlignment(Value *V, TargetData *TD) { |
| 7426 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) { |
| 7427 | unsigned Align = GV->getAlignment(); |
| 7428 | if (Align == 0 && TD) |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 7429 | Align = TD->getPrefTypeAlignment(GV->getType()->getElementType()); |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7430 | return Align; |
| 7431 | } else if (AllocationInst *AI = dyn_cast<AllocationInst>(V)) { |
| 7432 | unsigned Align = AI->getAlignment(); |
| 7433 | if (Align == 0 && TD) { |
| 7434 | if (isa<AllocaInst>(AI)) |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 7435 | Align = TD->getPrefTypeAlignment(AI->getType()->getElementType()); |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7436 | else if (isa<MallocInst>(AI)) { |
| 7437 | // Malloc returns maximally aligned memory. |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 7438 | Align = TD->getABITypeAlignment(AI->getType()->getElementType()); |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 7439 | Align = |
| 7440 | std::max(Align, |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 7441 | (unsigned)TD->getABITypeAlignment(Type::DoubleTy)); |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 7442 | Align = |
| 7443 | std::max(Align, |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 7444 | (unsigned)TD->getABITypeAlignment(Type::Int64Ty)); |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7445 | } |
| 7446 | } |
| 7447 | return Align; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7448 | } else if (isa<BitCastInst>(V) || |
Chris Lattner | 51c26e9 | 2006-03-07 01:28:57 +0000 | [diff] [blame] | 7449 | (isa<ConstantExpr>(V) && |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7450 | cast<ConstantExpr>(V)->getOpcode() == Instruction::BitCast)) { |
Chris Lattner | 51c26e9 | 2006-03-07 01:28:57 +0000 | [diff] [blame] | 7451 | User *CI = cast<User>(V); |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7452 | if (isa<PointerType>(CI->getOperand(0)->getType())) |
| 7453 | return GetKnownAlignment(CI->getOperand(0), TD); |
| 7454 | return 0; |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7455 | } else if (User *GEPI = dyn_castGetElementPtr(V)) { |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7456 | unsigned BaseAlignment = GetKnownAlignment(GEPI->getOperand(0), TD); |
| 7457 | if (BaseAlignment == 0) return 0; |
| 7458 | |
| 7459 | // If all indexes are zero, it is just the alignment of the base pointer. |
| 7460 | bool AllZeroOperands = true; |
| 7461 | for (unsigned i = 1, e = GEPI->getNumOperands(); i != e; ++i) |
| 7462 | if (!isa<Constant>(GEPI->getOperand(i)) || |
| 7463 | !cast<Constant>(GEPI->getOperand(i))->isNullValue()) { |
| 7464 | AllZeroOperands = false; |
| 7465 | break; |
| 7466 | } |
| 7467 | if (AllZeroOperands) |
| 7468 | return BaseAlignment; |
| 7469 | |
| 7470 | // Otherwise, if the base alignment is >= the alignment we expect for the |
| 7471 | // base pointer type, then we know that the resultant pointer is aligned at |
| 7472 | // least as much as its type requires. |
| 7473 | if (!TD) return 0; |
| 7474 | |
| 7475 | const Type *BasePtrTy = GEPI->getOperand(0)->getType(); |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 7476 | const PointerType *PtrTy = cast<PointerType>(BasePtrTy); |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 7477 | if (TD->getABITypeAlignment(PtrTy->getElementType()) |
Chris Lattner | 51c26e9 | 2006-03-07 01:28:57 +0000 | [diff] [blame] | 7478 | <= BaseAlignment) { |
| 7479 | const Type *GEPTy = GEPI->getType(); |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 7480 | const PointerType *GEPPtrTy = cast<PointerType>(GEPTy); |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 7481 | return TD->getABITypeAlignment(GEPPtrTy->getElementType()); |
Chris Lattner | 51c26e9 | 2006-03-07 01:28:57 +0000 | [diff] [blame] | 7482 | } |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7483 | return 0; |
| 7484 | } |
| 7485 | return 0; |
| 7486 | } |
| 7487 | |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 7488 | |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 7489 | /// visitCallInst - CallInst simplification. This mostly only handles folding |
| 7490 | /// of intrinsic instructions. For normal calls, it allows visitCallSite to do |
| 7491 | /// the heavy lifting. |
| 7492 | /// |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7493 | Instruction *InstCombiner::visitCallInst(CallInst &CI) { |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 7494 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(&CI); |
| 7495 | if (!II) return visitCallSite(&CI); |
| 7496 | |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 7497 | // Intrinsics cannot occur in an invoke, so handle them here instead of in |
| 7498 | // visitCallSite. |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 7499 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(II)) { |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 7500 | bool Changed = false; |
| 7501 | |
| 7502 | // memmove/cpy/set of zero bytes is a noop. |
| 7503 | if (Constant *NumBytes = dyn_cast<Constant>(MI->getLength())) { |
| 7504 | if (NumBytes->isNullValue()) return EraseInstFromFunction(CI); |
| 7505 | |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 7506 | if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes)) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7507 | if (CI->getZExtValue() == 1) { |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 7508 | // Replace the instruction with just byte operations. We would |
| 7509 | // transform other cases to loads/stores, but we don't know if |
| 7510 | // alignment is sufficient. |
| 7511 | } |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 7512 | } |
| 7513 | |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 7514 | // If we have a memmove and the source operation is a constant global, |
| 7515 | // then the source and dest pointers can't alias, so we can change this |
| 7516 | // into a call to memcpy. |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7517 | if (MemMoveInst *MMI = dyn_cast<MemMoveInst>(II)) { |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 7518 | if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource())) |
| 7519 | if (GVSrc->isConstant()) { |
| 7520 | Module *M = CI.getParent()->getParent()->getParent(); |
Chris Lattner | 2195939 | 2006-03-03 01:34:17 +0000 | [diff] [blame] | 7521 | const char *Name; |
Andrew Lenharth | 8ed4c47 | 2006-11-03 22:45:50 +0000 | [diff] [blame] | 7522 | if (CI.getCalledFunction()->getFunctionType()->getParamType(2) == |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 7523 | Type::Int32Ty) |
Chris Lattner | 2195939 | 2006-03-03 01:34:17 +0000 | [diff] [blame] | 7524 | Name = "llvm.memcpy.i32"; |
| 7525 | else |
| 7526 | Name = "llvm.memcpy.i64"; |
Chris Lattner | 9214196 | 2007-01-07 06:58:05 +0000 | [diff] [blame] | 7527 | Constant *MemCpy = M->getOrInsertFunction(Name, |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 7528 | CI.getCalledFunction()->getFunctionType()); |
| 7529 | CI.setOperand(0, MemCpy); |
| 7530 | Changed = true; |
| 7531 | } |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7532 | } |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 7533 | |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7534 | // If we can determine a pointer alignment that is bigger than currently |
| 7535 | // set, update the alignment. |
| 7536 | if (isa<MemCpyInst>(MI) || isa<MemMoveInst>(MI)) { |
| 7537 | unsigned Alignment1 = GetKnownAlignment(MI->getOperand(1), TD); |
| 7538 | unsigned Alignment2 = GetKnownAlignment(MI->getOperand(2), TD); |
| 7539 | unsigned Align = std::min(Alignment1, Alignment2); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7540 | if (MI->getAlignment()->getZExtValue() < Align) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 7541 | MI->setAlignment(ConstantInt::get(Type::Int32Ty, Align)); |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7542 | Changed = true; |
| 7543 | } |
| 7544 | } else if (isa<MemSetInst>(MI)) { |
| 7545 | unsigned Alignment = GetKnownAlignment(MI->getDest(), TD); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7546 | if (MI->getAlignment()->getZExtValue() < Alignment) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 7547 | MI->setAlignment(ConstantInt::get(Type::Int32Ty, Alignment)); |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7548 | Changed = true; |
| 7549 | } |
| 7550 | } |
| 7551 | |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 7552 | if (Changed) return II; |
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 7553 | } else { |
| 7554 | switch (II->getIntrinsicID()) { |
| 7555 | default: break; |
Chris Lattner | 82ed58f | 2006-04-02 05:30:25 +0000 | [diff] [blame] | 7556 | case Intrinsic::ppc_altivec_lvx: |
| 7557 | case Intrinsic::ppc_altivec_lvxl: |
Chris Lattner | fd6bdf0 | 2006-04-17 22:26:56 +0000 | [diff] [blame] | 7558 | case Intrinsic::x86_sse_loadu_ps: |
| 7559 | case Intrinsic::x86_sse2_loadu_pd: |
| 7560 | case Intrinsic::x86_sse2_loadu_dq: |
| 7561 | // Turn PPC lvx -> load if the pointer is known aligned. |
| 7562 | // Turn X86 loadups -> load if the pointer is known aligned. |
Chris Lattner | 82ed58f | 2006-04-02 05:30:25 +0000 | [diff] [blame] | 7563 | if (GetKnownAlignment(II->getOperand(1), TD) >= 16) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7564 | Value *Ptr = InsertCastBefore(Instruction::BitCast, II->getOperand(1), |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 7565 | PointerType::get(II->getType()), CI); |
Chris Lattner | 82ed58f | 2006-04-02 05:30:25 +0000 | [diff] [blame] | 7566 | return new LoadInst(Ptr); |
| 7567 | } |
| 7568 | break; |
| 7569 | case Intrinsic::ppc_altivec_stvx: |
| 7570 | case Intrinsic::ppc_altivec_stvxl: |
| 7571 | // Turn stvx -> store if the pointer is known aligned. |
| 7572 | if (GetKnownAlignment(II->getOperand(2), TD) >= 16) { |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 7573 | const Type *OpPtrTy = PointerType::get(II->getOperand(1)->getType()); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7574 | Value *Ptr = InsertCastBefore(Instruction::BitCast, II->getOperand(2), |
| 7575 | OpPtrTy, CI); |
Chris Lattner | 82ed58f | 2006-04-02 05:30:25 +0000 | [diff] [blame] | 7576 | return new StoreInst(II->getOperand(1), Ptr); |
| 7577 | } |
| 7578 | break; |
Chris Lattner | fd6bdf0 | 2006-04-17 22:26:56 +0000 | [diff] [blame] | 7579 | case Intrinsic::x86_sse_storeu_ps: |
| 7580 | case Intrinsic::x86_sse2_storeu_pd: |
| 7581 | case Intrinsic::x86_sse2_storeu_dq: |
| 7582 | case Intrinsic::x86_sse2_storel_dq: |
| 7583 | // Turn X86 storeu -> store if the pointer is known aligned. |
| 7584 | if (GetKnownAlignment(II->getOperand(1), TD) >= 16) { |
| 7585 | const Type *OpPtrTy = PointerType::get(II->getOperand(2)->getType()); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7586 | Value *Ptr = InsertCastBefore(Instruction::BitCast, II->getOperand(1), |
| 7587 | OpPtrTy, CI); |
Chris Lattner | fd6bdf0 | 2006-04-17 22:26:56 +0000 | [diff] [blame] | 7588 | return new StoreInst(II->getOperand(2), Ptr); |
| 7589 | } |
| 7590 | break; |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 7591 | |
| 7592 | case Intrinsic::x86_sse_cvttss2si: { |
| 7593 | // These intrinsics only demands the 0th element of its input vector. If |
| 7594 | // we can simplify the input based on that, do so now. |
| 7595 | uint64_t UndefElts; |
| 7596 | if (Value *V = SimplifyDemandedVectorElts(II->getOperand(1), 1, |
| 7597 | UndefElts)) { |
| 7598 | II->setOperand(1, V); |
| 7599 | return II; |
| 7600 | } |
| 7601 | break; |
| 7602 | } |
| 7603 | |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 7604 | case Intrinsic::ppc_altivec_vperm: |
| 7605 | // 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] | 7606 | if (ConstantVector *Mask = dyn_cast<ConstantVector>(II->getOperand(3))) { |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 7607 | assert(Mask->getNumOperands() == 16 && "Bad type for intrinsic!"); |
| 7608 | |
| 7609 | // Check that all of the elements are integer constants or undefs. |
| 7610 | bool AllEltsOk = true; |
| 7611 | for (unsigned i = 0; i != 16; ++i) { |
| 7612 | if (!isa<ConstantInt>(Mask->getOperand(i)) && |
| 7613 | !isa<UndefValue>(Mask->getOperand(i))) { |
| 7614 | AllEltsOk = false; |
| 7615 | break; |
| 7616 | } |
| 7617 | } |
| 7618 | |
| 7619 | if (AllEltsOk) { |
| 7620 | // Cast the input vectors to byte vectors. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7621 | Value *Op0 = InsertCastBefore(Instruction::BitCast, |
| 7622 | II->getOperand(1), Mask->getType(), CI); |
| 7623 | Value *Op1 = InsertCastBefore(Instruction::BitCast, |
| 7624 | II->getOperand(2), Mask->getType(), CI); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 7625 | Value *Result = UndefValue::get(Op0->getType()); |
| 7626 | |
| 7627 | // Only extract each element once. |
| 7628 | Value *ExtractedElts[32]; |
| 7629 | memset(ExtractedElts, 0, sizeof(ExtractedElts)); |
| 7630 | |
| 7631 | for (unsigned i = 0; i != 16; ++i) { |
| 7632 | if (isa<UndefValue>(Mask->getOperand(i))) |
| 7633 | continue; |
Chris Lattner | e34e9a2 | 2007-04-14 23:32:02 +0000 | [diff] [blame] | 7634 | unsigned Idx=cast<ConstantInt>(Mask->getOperand(i))->getZExtValue(); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 7635 | Idx &= 31; // Match the hardware behavior. |
| 7636 | |
| 7637 | if (ExtractedElts[Idx] == 0) { |
| 7638 | Instruction *Elt = |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 7639 | new ExtractElementInst(Idx < 16 ? Op0 : Op1, Idx&15, "tmp"); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 7640 | InsertNewInstBefore(Elt, CI); |
| 7641 | ExtractedElts[Idx] = Elt; |
| 7642 | } |
| 7643 | |
| 7644 | // Insert this value into the result vector. |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 7645 | Result = new InsertElementInst(Result, ExtractedElts[Idx], i,"tmp"); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 7646 | InsertNewInstBefore(cast<Instruction>(Result), CI); |
| 7647 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7648 | return CastInst::create(Instruction::BitCast, Result, CI.getType()); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 7649 | } |
| 7650 | } |
| 7651 | break; |
| 7652 | |
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 7653 | case Intrinsic::stackrestore: { |
| 7654 | // If the save is right next to the restore, remove the restore. This can |
| 7655 | // happen when variable allocas are DCE'd. |
| 7656 | if (IntrinsicInst *SS = dyn_cast<IntrinsicInst>(II->getOperand(1))) { |
| 7657 | if (SS->getIntrinsicID() == Intrinsic::stacksave) { |
| 7658 | BasicBlock::iterator BI = SS; |
| 7659 | if (&*++BI == II) |
| 7660 | return EraseInstFromFunction(CI); |
| 7661 | } |
| 7662 | } |
| 7663 | |
| 7664 | // If the stack restore is in a return/unwind block and if there are no |
| 7665 | // allocas or calls between the restore and the return, nuke the restore. |
| 7666 | TerminatorInst *TI = II->getParent()->getTerminator(); |
| 7667 | if (isa<ReturnInst>(TI) || isa<UnwindInst>(TI)) { |
| 7668 | BasicBlock::iterator BI = II; |
| 7669 | bool CannotRemove = false; |
| 7670 | for (++BI; &*BI != TI; ++BI) { |
| 7671 | if (isa<AllocaInst>(BI) || |
| 7672 | (isa<CallInst>(BI) && !isa<IntrinsicInst>(BI))) { |
| 7673 | CannotRemove = true; |
| 7674 | break; |
| 7675 | } |
| 7676 | } |
| 7677 | if (!CannotRemove) |
| 7678 | return EraseInstFromFunction(CI); |
| 7679 | } |
| 7680 | break; |
| 7681 | } |
| 7682 | } |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 7683 | } |
| 7684 | |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 7685 | return visitCallSite(II); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7686 | } |
| 7687 | |
| 7688 | // InvokeInst simplification |
| 7689 | // |
| 7690 | Instruction *InstCombiner::visitInvokeInst(InvokeInst &II) { |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 7691 | return visitCallSite(&II); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7692 | } |
| 7693 | |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 7694 | // visitCallSite - Improvements for call and invoke instructions. |
| 7695 | // |
| 7696 | Instruction *InstCombiner::visitCallSite(CallSite CS) { |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 7697 | bool Changed = false; |
| 7698 | |
| 7699 | // If the callee is a constexpr cast of a function, attempt to move the cast |
| 7700 | // to the arguments of the call/invoke. |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 7701 | if (transformConstExprCastCall(CS)) return 0; |
| 7702 | |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 7703 | Value *Callee = CS.getCalledValue(); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 7704 | |
Chris Lattner | 08b22ec | 2005-05-13 07:09:09 +0000 | [diff] [blame] | 7705 | if (Function *CalleeF = dyn_cast<Function>(Callee)) |
| 7706 | if (CalleeF->getCallingConv() != CS.getCallingConv()) { |
| 7707 | Instruction *OldCall = CS.getInstruction(); |
| 7708 | // If the call and callee calling conventions don't match, this call must |
| 7709 | // be unreachable, as the call is undefined. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 7710 | new StoreInst(ConstantInt::getTrue(), |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 7711 | UndefValue::get(PointerType::get(Type::Int1Ty)), OldCall); |
Chris Lattner | 08b22ec | 2005-05-13 07:09:09 +0000 | [diff] [blame] | 7712 | if (!OldCall->use_empty()) |
| 7713 | OldCall->replaceAllUsesWith(UndefValue::get(OldCall->getType())); |
| 7714 | if (isa<CallInst>(OldCall)) // Not worth removing an invoke here. |
| 7715 | return EraseInstFromFunction(*OldCall); |
| 7716 | return 0; |
| 7717 | } |
| 7718 | |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 7719 | if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) { |
| 7720 | // This instruction is not reachable, just remove it. We insert a store to |
| 7721 | // undef so that we know that this code is not reachable, despite the fact |
| 7722 | // that we can't modify the CFG here. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 7723 | new StoreInst(ConstantInt::getTrue(), |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 7724 | UndefValue::get(PointerType::get(Type::Int1Ty)), |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 7725 | CS.getInstruction()); |
| 7726 | |
| 7727 | if (!CS.getInstruction()->use_empty()) |
| 7728 | CS.getInstruction()-> |
| 7729 | replaceAllUsesWith(UndefValue::get(CS.getInstruction()->getType())); |
| 7730 | |
| 7731 | if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) { |
| 7732 | // Don't break the CFG, insert a dummy cond branch. |
| 7733 | new BranchInst(II->getNormalDest(), II->getUnwindDest(), |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 7734 | ConstantInt::getTrue(), II); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 7735 | } |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 7736 | return EraseInstFromFunction(*CS.getInstruction()); |
| 7737 | } |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 7738 | |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 7739 | const PointerType *PTy = cast<PointerType>(Callee->getType()); |
| 7740 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
| 7741 | if (FTy->isVarArg()) { |
| 7742 | // See if we can optimize any arguments passed through the varargs area of |
| 7743 | // the call. |
| 7744 | for (CallSite::arg_iterator I = CS.arg_begin()+FTy->getNumParams(), |
| 7745 | E = CS.arg_end(); I != E; ++I) |
| 7746 | if (CastInst *CI = dyn_cast<CastInst>(*I)) { |
| 7747 | // If this cast does not effect the value passed through the varargs |
| 7748 | // area, we can eliminate the use of the cast. |
| 7749 | Value *Op = CI->getOperand(0); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7750 | if (CI->isLosslessCast()) { |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 7751 | *I = Op; |
| 7752 | Changed = true; |
| 7753 | } |
| 7754 | } |
| 7755 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7756 | |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 7757 | return Changed ? CS.getInstruction() : 0; |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 7758 | } |
| 7759 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7760 | // transformConstExprCastCall - If the callee is a constexpr cast of a function, |
| 7761 | // attempt to move the cast to the arguments of the call/invoke. |
| 7762 | // |
| 7763 | bool InstCombiner::transformConstExprCastCall(CallSite CS) { |
| 7764 | if (!isa<ConstantExpr>(CS.getCalledValue())) return false; |
| 7765 | ConstantExpr *CE = cast<ConstantExpr>(CS.getCalledValue()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7766 | if (CE->getOpcode() != Instruction::BitCast || |
| 7767 | !isa<Function>(CE->getOperand(0))) |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7768 | return false; |
Reid Spencer | 8863f18 | 2004-07-18 00:38:32 +0000 | [diff] [blame] | 7769 | Function *Callee = cast<Function>(CE->getOperand(0)); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7770 | Instruction *Caller = CS.getInstruction(); |
| 7771 | |
| 7772 | // Okay, this is a cast from a function to a different type. Unless doing so |
| 7773 | // would cause a type conversion of one of our arguments, change this call to |
| 7774 | // be a direct call with arguments casted to the appropriate types. |
| 7775 | // |
| 7776 | const FunctionType *FT = Callee->getFunctionType(); |
| 7777 | const Type *OldRetTy = Caller->getType(); |
| 7778 | |
Chris Lattner | a2b18de | 2007-05-19 06:51:32 +0000 | [diff] [blame] | 7779 | const FunctionType *ActualFT = |
| 7780 | cast<FunctionType>(cast<PointerType>(CE->getType())->getElementType()); |
| 7781 | |
| 7782 | // If the parameter attributes don't match up, don't do the xform. We don't |
| 7783 | // want to lose an sret attribute or something. |
| 7784 | if (FT->getParamAttrs() != ActualFT->getParamAttrs()) |
| 7785 | return false; |
| 7786 | |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 7787 | // Check to see if we are changing the return type... |
| 7788 | if (OldRetTy != FT->getReturnType()) { |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 7789 | if (Callee->isDeclaration() && !Caller->use_empty() && |
Chris Lattner | 46013f4 | 2007-01-06 19:53:32 +0000 | [diff] [blame] | 7790 | // Conversion is ok if changing from pointer to int of same size. |
| 7791 | !(isa<PointerType>(FT->getReturnType()) && |
| 7792 | TD->getIntPtrType() == OldRetTy)) |
Chris Lattner | ec47992 | 2007-01-06 02:09:32 +0000 | [diff] [blame] | 7793 | return false; // Cannot transform this return value. |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 7794 | |
| 7795 | // If the callsite is an invoke instruction, and the return value is used by |
| 7796 | // a PHI node in a successor, we cannot change the return type of the call |
| 7797 | // because there is no place to put the cast instruction (without breaking |
| 7798 | // the critical edge). Bail out in this case. |
| 7799 | if (!Caller->use_empty()) |
| 7800 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) |
| 7801 | for (Value::use_iterator UI = II->use_begin(), E = II->use_end(); |
| 7802 | UI != E; ++UI) |
| 7803 | if (PHINode *PN = dyn_cast<PHINode>(*UI)) |
| 7804 | if (PN->getParent() == II->getNormalDest() || |
Chris Lattner | aeb2a1d | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 7805 | PN->getParent() == II->getUnwindDest()) |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 7806 | return false; |
| 7807 | } |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7808 | |
| 7809 | unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin()); |
| 7810 | unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7811 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7812 | CallSite::arg_iterator AI = CS.arg_begin(); |
| 7813 | for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) { |
| 7814 | const Type *ParamTy = FT->getParamType(i); |
Andrew Lenharth | b8e604c | 2006-06-28 01:01:52 +0000 | [diff] [blame] | 7815 | const Type *ActTy = (*AI)->getType(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7816 | ConstantInt *c = dyn_cast<ConstantInt>(*AI); |
Dale Johannesen | 16ff304 | 2007-04-04 19:16:42 +0000 | [diff] [blame] | 7817 | //Some conversions are safe even if we do not have a body. |
Andrew Lenharth | b8e604c | 2006-06-28 01:01:52 +0000 | [diff] [blame] | 7818 | //Either we can cast directly, or we can upconvert the argument |
Chris Lattner | ec47992 | 2007-01-06 02:09:32 +0000 | [diff] [blame] | 7819 | bool isConvertible = ActTy == ParamTy || |
Chris Lattner | 46013f4 | 2007-01-06 19:53:32 +0000 | [diff] [blame] | 7820 | (isa<PointerType>(ParamTy) && isa<PointerType>(ActTy)) || |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 7821 | (ParamTy->isInteger() && ActTy->isInteger() && |
Reid Spencer | abaa8ca | 2007-01-08 16:32:00 +0000 | [diff] [blame] | 7822 | ParamTy->getPrimitiveSizeInBits() >= ActTy->getPrimitiveSizeInBits()) || |
| 7823 | (c && ParamTy->getPrimitiveSizeInBits() >= ActTy->getPrimitiveSizeInBits() |
Zhou Sheng | 0fc5095 | 2007-03-25 05:01:29 +0000 | [diff] [blame] | 7824 | && c->getValue().isStrictlyPositive()); |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 7825 | if (Callee->isDeclaration() && !isConvertible) return false; |
Dale Johannesen | 16ff304 | 2007-04-04 19:16:42 +0000 | [diff] [blame] | 7826 | |
| 7827 | // Most other conversions can be done if we have a body, even if these |
| 7828 | // lose information, e.g. int->short. |
| 7829 | // Some conversions cannot be done at all, e.g. float to pointer. |
| 7830 | // Logic here parallels CastInst::getCastOpcode (the design there |
| 7831 | // requires legality checks like this be done before calling it). |
| 7832 | if (ParamTy->isInteger()) { |
| 7833 | if (const VectorType *VActTy = dyn_cast<VectorType>(ActTy)) { |
| 7834 | if (VActTy->getBitWidth() != ParamTy->getPrimitiveSizeInBits()) |
| 7835 | return false; |
| 7836 | } |
| 7837 | if (!ActTy->isInteger() && !ActTy->isFloatingPoint() && |
| 7838 | !isa<PointerType>(ActTy)) |
| 7839 | return false; |
| 7840 | } else if (ParamTy->isFloatingPoint()) { |
| 7841 | if (const VectorType *VActTy = dyn_cast<VectorType>(ActTy)) { |
| 7842 | if (VActTy->getBitWidth() != ParamTy->getPrimitiveSizeInBits()) |
| 7843 | return false; |
| 7844 | } |
| 7845 | if (!ActTy->isInteger() && !ActTy->isFloatingPoint()) |
| 7846 | return false; |
| 7847 | } else if (const VectorType *VParamTy = dyn_cast<VectorType>(ParamTy)) { |
| 7848 | if (const VectorType *VActTy = dyn_cast<VectorType>(ActTy)) { |
| 7849 | if (VActTy->getBitWidth() != VParamTy->getBitWidth()) |
| 7850 | return false; |
| 7851 | } |
| 7852 | if (VParamTy->getBitWidth() != ActTy->getPrimitiveSizeInBits()) |
| 7853 | return false; |
| 7854 | } else if (isa<PointerType>(ParamTy)) { |
| 7855 | if (!ActTy->isInteger() && !isa<PointerType>(ActTy)) |
| 7856 | return false; |
| 7857 | } else { |
| 7858 | return false; |
| 7859 | } |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7860 | } |
| 7861 | |
| 7862 | if (FT->getNumParams() < NumActualArgs && !FT->isVarArg() && |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 7863 | Callee->isDeclaration()) |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7864 | return false; // Do not delete arguments unless we have a function body... |
| 7865 | |
| 7866 | // Okay, we decided that this is a safe thing to do: go ahead and start |
| 7867 | // inserting cast instructions as necessary... |
| 7868 | std::vector<Value*> Args; |
| 7869 | Args.reserve(NumActualArgs); |
| 7870 | |
| 7871 | AI = CS.arg_begin(); |
| 7872 | for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) { |
| 7873 | const Type *ParamTy = FT->getParamType(i); |
| 7874 | if ((*AI)->getType() == ParamTy) { |
| 7875 | Args.push_back(*AI); |
| 7876 | } else { |
Reid Spencer | 8a903db | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 7877 | Instruction::CastOps opcode = CastInst::getCastOpcode(*AI, |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 7878 | false, ParamTy, false); |
Reid Spencer | 8a903db | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 7879 | CastInst *NewCast = CastInst::create(opcode, *AI, ParamTy, "tmp"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7880 | Args.push_back(InsertNewInstBefore(NewCast, *Caller)); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7881 | } |
| 7882 | } |
| 7883 | |
| 7884 | // If the function takes more arguments than the call was taking, add them |
| 7885 | // now... |
| 7886 | for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i) |
| 7887 | Args.push_back(Constant::getNullValue(FT->getParamType(i))); |
| 7888 | |
| 7889 | // If we are removing arguments to the function, emit an obnoxious warning... |
| 7890 | if (FT->getNumParams() < NumActualArgs) |
| 7891 | if (!FT->isVarArg()) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 7892 | cerr << "WARNING: While resolving call to function '" |
| 7893 | << Callee->getName() << "' arguments were dropped!\n"; |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7894 | } else { |
| 7895 | // Add all of the arguments in their promoted form to the arg list... |
| 7896 | for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) { |
| 7897 | const Type *PTy = getPromotedType((*AI)->getType()); |
| 7898 | if (PTy != (*AI)->getType()) { |
| 7899 | // Must promote to pass through va_arg area! |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 7900 | Instruction::CastOps opcode = CastInst::getCastOpcode(*AI, false, |
| 7901 | PTy, false); |
Reid Spencer | 8a903db | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 7902 | Instruction *Cast = CastInst::create(opcode, *AI, PTy, "tmp"); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7903 | InsertNewInstBefore(Cast, *Caller); |
| 7904 | Args.push_back(Cast); |
| 7905 | } else { |
| 7906 | Args.push_back(*AI); |
| 7907 | } |
| 7908 | } |
| 7909 | } |
| 7910 | |
| 7911 | if (FT->getReturnType() == Type::VoidTy) |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 7912 | Caller->setName(""); // Void type should not have a name. |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7913 | |
| 7914 | Instruction *NC; |
| 7915 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
Chris Lattner | aeb2a1d | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 7916 | NC = new InvokeInst(Callee, II->getNormalDest(), II->getUnwindDest(), |
Chris Lattner | 93e985f | 2007-02-13 02:10:56 +0000 | [diff] [blame] | 7917 | &Args[0], Args.size(), Caller->getName(), Caller); |
Chris Lattner | e437026 | 2005-05-14 12:25:32 +0000 | [diff] [blame] | 7918 | cast<InvokeInst>(II)->setCallingConv(II->getCallingConv()); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7919 | } else { |
Chris Lattner | 93e985f | 2007-02-13 02:10:56 +0000 | [diff] [blame] | 7920 | NC = new CallInst(Callee, &Args[0], Args.size(), Caller->getName(), Caller); |
Chris Lattner | a9e9211 | 2005-05-06 06:48:21 +0000 | [diff] [blame] | 7921 | if (cast<CallInst>(Caller)->isTailCall()) |
| 7922 | cast<CallInst>(NC)->setTailCall(); |
Chris Lattner | e437026 | 2005-05-14 12:25:32 +0000 | [diff] [blame] | 7923 | cast<CallInst>(NC)->setCallingConv(cast<CallInst>(Caller)->getCallingConv()); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7924 | } |
| 7925 | |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 7926 | // Insert a cast of the return type as necessary. |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7927 | Value *NV = NC; |
| 7928 | if (Caller->getType() != NV->getType() && !Caller->use_empty()) { |
| 7929 | if (NV->getType() != Type::VoidTy) { |
Reid Spencer | 8a903db | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 7930 | const Type *CallerTy = Caller->getType(); |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 7931 | Instruction::CastOps opcode = CastInst::getCastOpcode(NC, false, |
| 7932 | CallerTy, false); |
Reid Spencer | 8a903db | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 7933 | NV = NC = CastInst::create(opcode, NC, CallerTy, "tmp"); |
Chris Lattner | bb60904 | 2003-10-30 00:46:41 +0000 | [diff] [blame] | 7934 | |
| 7935 | // If this is an invoke instruction, we should insert it after the first |
| 7936 | // non-phi, instruction in the normal successor block. |
| 7937 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
| 7938 | BasicBlock::iterator I = II->getNormalDest()->begin(); |
| 7939 | while (isa<PHINode>(I)) ++I; |
| 7940 | InsertNewInstBefore(NC, *I); |
| 7941 | } else { |
| 7942 | // Otherwise, it's a call, just insert cast right after the call instr |
| 7943 | InsertNewInstBefore(NC, *Caller); |
| 7944 | } |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 7945 | AddUsersToWorkList(*Caller); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7946 | } else { |
Chris Lattner | c30bda7 | 2004-10-17 21:22:38 +0000 | [diff] [blame] | 7947 | NV = UndefValue::get(Caller->getType()); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7948 | } |
| 7949 | } |
| 7950 | |
| 7951 | if (Caller->getType() != Type::VoidTy && !Caller->use_empty()) |
| 7952 | Caller->replaceAllUsesWith(NV); |
Chris Lattner | f22a5c6 | 2007-03-02 19:59:19 +0000 | [diff] [blame] | 7953 | Caller->eraseFromParent(); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 7954 | RemoveFromWorkList(Caller); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7955 | return true; |
| 7956 | } |
| 7957 | |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7958 | /// FoldPHIArgBinOpIntoPHI - If we have something like phi [add (a,b), add(c,d)] |
| 7959 | /// and if a/b/c/d and the add's all have a single use, turn this into two phi's |
| 7960 | /// and a single binop. |
| 7961 | Instruction *InstCombiner::FoldPHIArgBinOpIntoPHI(PHINode &PN) { |
| 7962 | Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0)); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7963 | assert(isa<BinaryOperator>(FirstInst) || isa<GetElementPtrInst>(FirstInst) || |
| 7964 | isa<CmpInst>(FirstInst)); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7965 | unsigned Opc = FirstInst->getOpcode(); |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 7966 | Value *LHSVal = FirstInst->getOperand(0); |
| 7967 | Value *RHSVal = FirstInst->getOperand(1); |
| 7968 | |
| 7969 | const Type *LHSType = LHSVal->getType(); |
| 7970 | const Type *RHSType = RHSVal->getType(); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7971 | |
| 7972 | // Scan to see if all operands are the same opcode, all have one use, and all |
| 7973 | // kill their operands (i.e. the operands have one use). |
Chris Lattner | a90a24c | 2006-11-01 04:55:47 +0000 | [diff] [blame] | 7974 | for (unsigned i = 0; i != PN.getNumIncomingValues(); ++i) { |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7975 | Instruction *I = dyn_cast<Instruction>(PN.getIncomingValue(i)); |
Chris Lattner | a90a24c | 2006-11-01 04:55:47 +0000 | [diff] [blame] | 7976 | if (!I || I->getOpcode() != Opc || !I->hasOneUse() || |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7977 | // 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] | 7978 | // types or GEP's with different index types. |
| 7979 | I->getOperand(0)->getType() != LHSType || |
| 7980 | I->getOperand(1)->getType() != RHSType) |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7981 | return 0; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7982 | |
| 7983 | // If they are CmpInst instructions, check their predicates |
| 7984 | if (Opc == Instruction::ICmp || Opc == Instruction::FCmp) |
| 7985 | if (cast<CmpInst>(I)->getPredicate() != |
| 7986 | cast<CmpInst>(FirstInst)->getPredicate()) |
| 7987 | return 0; |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 7988 | |
| 7989 | // Keep track of which operand needs a phi node. |
| 7990 | if (I->getOperand(0) != LHSVal) LHSVal = 0; |
| 7991 | if (I->getOperand(1) != RHSVal) RHSVal = 0; |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7992 | } |
| 7993 | |
Chris Lattner | 53738a4 | 2006-11-08 19:42:28 +0000 | [diff] [blame] | 7994 | // Otherwise, this is safe to transform, determine if it is profitable. |
| 7995 | |
| 7996 | // If this is a GEP, and if the index (not the pointer) needs a PHI, bail out. |
| 7997 | // Indexes are often folded into load/store instructions, so we don't want to |
| 7998 | // hide them behind a phi. |
| 7999 | if (isa<GetElementPtrInst>(FirstInst) && RHSVal == 0) |
| 8000 | return 0; |
| 8001 | |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 8002 | Value *InLHS = FirstInst->getOperand(0); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 8003 | Value *InRHS = FirstInst->getOperand(1); |
Chris Lattner | 53738a4 | 2006-11-08 19:42:28 +0000 | [diff] [blame] | 8004 | PHINode *NewLHS = 0, *NewRHS = 0; |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 8005 | if (LHSVal == 0) { |
| 8006 | NewLHS = new PHINode(LHSType, FirstInst->getOperand(0)->getName()+".pn"); |
| 8007 | NewLHS->reserveOperandSpace(PN.getNumOperands()/2); |
| 8008 | NewLHS->addIncoming(InLHS, PN.getIncomingBlock(0)); |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 8009 | InsertNewInstBefore(NewLHS, PN); |
| 8010 | LHSVal = NewLHS; |
| 8011 | } |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 8012 | |
| 8013 | if (RHSVal == 0) { |
| 8014 | NewRHS = new PHINode(RHSType, FirstInst->getOperand(1)->getName()+".pn"); |
| 8015 | NewRHS->reserveOperandSpace(PN.getNumOperands()/2); |
| 8016 | NewRHS->addIncoming(InRHS, PN.getIncomingBlock(0)); |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 8017 | InsertNewInstBefore(NewRHS, PN); |
| 8018 | RHSVal = NewRHS; |
| 8019 | } |
| 8020 | |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 8021 | // Add all operands to the new PHIs. |
| 8022 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 8023 | if (NewLHS) { |
| 8024 | Value *NewInLHS =cast<Instruction>(PN.getIncomingValue(i))->getOperand(0); |
| 8025 | NewLHS->addIncoming(NewInLHS, PN.getIncomingBlock(i)); |
| 8026 | } |
| 8027 | if (NewRHS) { |
| 8028 | Value *NewInRHS =cast<Instruction>(PN.getIncomingValue(i))->getOperand(1); |
| 8029 | NewRHS->addIncoming(NewInRHS, PN.getIncomingBlock(i)); |
| 8030 | } |
| 8031 | } |
| 8032 | |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 8033 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 8034 | return BinaryOperator::create(BinOp->getOpcode(), LHSVal, RHSVal); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8035 | else if (CmpInst *CIOp = dyn_cast<CmpInst>(FirstInst)) |
| 8036 | return CmpInst::create(CIOp->getOpcode(), CIOp->getPredicate(), LHSVal, |
| 8037 | RHSVal); |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 8038 | else { |
| 8039 | assert(isa<GetElementPtrInst>(FirstInst)); |
| 8040 | return new GetElementPtrInst(LHSVal, RHSVal); |
| 8041 | } |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 8042 | } |
| 8043 | |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 8044 | /// isSafeToSinkLoad - Return true if we know that it is safe sink the load out |
| 8045 | /// of the block that defines it. This means that it must be obvious the value |
| 8046 | /// of the load is not changed from the point of the load to the end of the |
| 8047 | /// block it is in. |
Chris Lattner | fd905ca | 2007-02-01 22:30:07 +0000 | [diff] [blame] | 8048 | /// |
| 8049 | /// Finally, it is safe, but not profitable, to sink a load targetting a |
| 8050 | /// non-address-taken alloca. Doing so will cause us to not promote the alloca |
| 8051 | /// to a register. |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 8052 | static bool isSafeToSinkLoad(LoadInst *L) { |
| 8053 | BasicBlock::iterator BBI = L, E = L->getParent()->end(); |
| 8054 | |
| 8055 | for (++BBI; BBI != E; ++BBI) |
| 8056 | if (BBI->mayWriteToMemory()) |
| 8057 | return false; |
Chris Lattner | fd905ca | 2007-02-01 22:30:07 +0000 | [diff] [blame] | 8058 | |
| 8059 | // Check for non-address taken alloca. If not address-taken already, it isn't |
| 8060 | // profitable to do this xform. |
| 8061 | if (AllocaInst *AI = dyn_cast<AllocaInst>(L->getOperand(0))) { |
| 8062 | bool isAddressTaken = false; |
| 8063 | for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); |
| 8064 | UI != E; ++UI) { |
| 8065 | if (isa<LoadInst>(UI)) continue; |
| 8066 | if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) { |
| 8067 | // If storing TO the alloca, then the address isn't taken. |
| 8068 | if (SI->getOperand(1) == AI) continue; |
| 8069 | } |
| 8070 | isAddressTaken = true; |
| 8071 | break; |
| 8072 | } |
| 8073 | |
| 8074 | if (!isAddressTaken) |
| 8075 | return false; |
| 8076 | } |
| 8077 | |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 8078 | return true; |
| 8079 | } |
| 8080 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8081 | |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 8082 | // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary" |
| 8083 | // operator and they all are only used by the PHI, PHI together their |
| 8084 | // inputs, and do the operation once, to the result of the PHI. |
| 8085 | Instruction *InstCombiner::FoldPHIArgOpIntoPHI(PHINode &PN) { |
| 8086 | Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0)); |
| 8087 | |
| 8088 | // Scan the instruction, looking for input operations that can be folded away. |
| 8089 | // If all input operands to the phi are the same instruction (e.g. a cast from |
| 8090 | // the same type or "+42") we can pull the operation through the PHI, reducing |
| 8091 | // code size and simplifying code. |
| 8092 | Constant *ConstantOp = 0; |
| 8093 | const Type *CastSrcTy = 0; |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 8094 | bool isVolatile = false; |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 8095 | if (isa<CastInst>(FirstInst)) { |
| 8096 | CastSrcTy = FirstInst->getOperand(0)->getType(); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 8097 | } else if (isa<BinaryOperator>(FirstInst) || isa<CmpInst>(FirstInst)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8098 | // Can fold binop, compare or shift here if the RHS is a constant, |
| 8099 | // otherwise call FoldPHIArgBinOpIntoPHI. |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 8100 | ConstantOp = dyn_cast<Constant>(FirstInst->getOperand(1)); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 8101 | if (ConstantOp == 0) |
| 8102 | return FoldPHIArgBinOpIntoPHI(PN); |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 8103 | } else if (LoadInst *LI = dyn_cast<LoadInst>(FirstInst)) { |
| 8104 | isVolatile = LI->isVolatile(); |
| 8105 | // We can't sink the load if the loaded value could be modified between the |
| 8106 | // load and the PHI. |
| 8107 | if (LI->getParent() != PN.getIncomingBlock(0) || |
| 8108 | !isSafeToSinkLoad(LI)) |
| 8109 | return 0; |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 8110 | } else if (isa<GetElementPtrInst>(FirstInst)) { |
Chris Lattner | 53738a4 | 2006-11-08 19:42:28 +0000 | [diff] [blame] | 8111 | if (FirstInst->getNumOperands() == 2) |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 8112 | return FoldPHIArgBinOpIntoPHI(PN); |
| 8113 | // Can't handle general GEPs yet. |
| 8114 | return 0; |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 8115 | } else { |
| 8116 | return 0; // Cannot fold this operation. |
| 8117 | } |
| 8118 | |
| 8119 | // Check to see if all arguments are the same operation. |
| 8120 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 8121 | if (!isa<Instruction>(PN.getIncomingValue(i))) return 0; |
| 8122 | Instruction *I = cast<Instruction>(PN.getIncomingValue(i)); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8123 | if (!I->hasOneUse() || !I->isSameOperationAs(FirstInst)) |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 8124 | return 0; |
| 8125 | if (CastSrcTy) { |
| 8126 | if (I->getOperand(0)->getType() != CastSrcTy) |
| 8127 | return 0; // Cast operation must match. |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 8128 | } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8129 | // We can't sink the load if the loaded value could be modified between |
| 8130 | // the load and the PHI. |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 8131 | if (LI->isVolatile() != isVolatile || |
| 8132 | LI->getParent() != PN.getIncomingBlock(i) || |
| 8133 | !isSafeToSinkLoad(LI)) |
| 8134 | return 0; |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 8135 | } else if (I->getOperand(1) != ConstantOp) { |
| 8136 | return 0; |
| 8137 | } |
| 8138 | } |
| 8139 | |
| 8140 | // Okay, they are all the same operation. Create a new PHI node of the |
| 8141 | // correct type, and PHI together all of the LHS's of the instructions. |
| 8142 | PHINode *NewPN = new PHINode(FirstInst->getOperand(0)->getType(), |
| 8143 | PN.getName()+".in"); |
Chris Lattner | 5551706 | 2005-01-29 00:39:08 +0000 | [diff] [blame] | 8144 | NewPN->reserveOperandSpace(PN.getNumOperands()/2); |
Chris Lattner | b589344 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 8145 | |
| 8146 | Value *InVal = FirstInst->getOperand(0); |
| 8147 | NewPN->addIncoming(InVal, PN.getIncomingBlock(0)); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 8148 | |
| 8149 | // Add all operands to the new PHI. |
Chris Lattner | b589344 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 8150 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 8151 | Value *NewInVal = cast<Instruction>(PN.getIncomingValue(i))->getOperand(0); |
| 8152 | if (NewInVal != InVal) |
| 8153 | InVal = 0; |
| 8154 | NewPN->addIncoming(NewInVal, PN.getIncomingBlock(i)); |
| 8155 | } |
| 8156 | |
| 8157 | Value *PhiVal; |
| 8158 | if (InVal) { |
| 8159 | // The new PHI unions all of the same values together. This is really |
| 8160 | // common, so we handle it intelligently here for compile-time speed. |
| 8161 | PhiVal = InVal; |
| 8162 | delete NewPN; |
| 8163 | } else { |
| 8164 | InsertNewInstBefore(NewPN, PN); |
| 8165 | PhiVal = NewPN; |
| 8166 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8167 | |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 8168 | // Insert and return the new operation. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8169 | if (CastInst* FirstCI = dyn_cast<CastInst>(FirstInst)) |
| 8170 | return CastInst::create(FirstCI->getOpcode(), PhiVal, PN.getType()); |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 8171 | else if (isa<LoadInst>(FirstInst)) |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 8172 | return new LoadInst(PhiVal, "", isVolatile); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 8173 | else if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) |
Chris Lattner | b589344 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 8174 | return BinaryOperator::create(BinOp->getOpcode(), PhiVal, ConstantOp); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8175 | else if (CmpInst *CIOp = dyn_cast<CmpInst>(FirstInst)) |
| 8176 | return CmpInst::create(CIOp->getOpcode(), CIOp->getPredicate(), |
| 8177 | PhiVal, ConstantOp); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 8178 | else |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 8179 | assert(0 && "Unknown operation"); |
Jeff Cohen | ca5183d | 2007-03-05 00:00:42 +0000 | [diff] [blame] | 8180 | return 0; |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 8181 | } |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 8182 | |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 8183 | /// DeadPHICycle - Return true if this PHI node is only used by a PHI node cycle |
| 8184 | /// that is dead. |
Chris Lattner | 0e5444b | 2007-03-26 20:40:50 +0000 | [diff] [blame] | 8185 | static bool DeadPHICycle(PHINode *PN, |
| 8186 | SmallPtrSet<PHINode*, 16> &PotentiallyDeadPHIs) { |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 8187 | if (PN->use_empty()) return true; |
| 8188 | if (!PN->hasOneUse()) return false; |
| 8189 | |
| 8190 | // Remember this node, and if we find the cycle, return. |
Chris Lattner | 0e5444b | 2007-03-26 20:40:50 +0000 | [diff] [blame] | 8191 | if (!PotentiallyDeadPHIs.insert(PN)) |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 8192 | return true; |
| 8193 | |
| 8194 | if (PHINode *PU = dyn_cast<PHINode>(PN->use_back())) |
| 8195 | return DeadPHICycle(PU, PotentiallyDeadPHIs); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8196 | |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 8197 | return false; |
| 8198 | } |
| 8199 | |
Chris Lattner | 473945d | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 8200 | // PHINode simplification |
| 8201 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 8202 | Instruction *InstCombiner::visitPHINode(PHINode &PN) { |
Owen Anderson | b64ab87 | 2006-07-10 22:15:25 +0000 | [diff] [blame] | 8203 | // If LCSSA is around, don't mess with Phi nodes |
Chris Lattner | f964f32 | 2007-03-04 04:27:24 +0000 | [diff] [blame] | 8204 | if (MustPreserveLCSSA) return 0; |
Owen Anderson | d1b78a1 | 2006-07-10 19:03:49 +0000 | [diff] [blame] | 8205 | |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 8206 | if (Value *V = PN.hasConstantValue()) |
| 8207 | return ReplaceInstUsesWith(PN, V); |
| 8208 | |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 8209 | // If all PHI operands are the same operation, pull them through the PHI, |
| 8210 | // reducing code size. |
| 8211 | if (isa<Instruction>(PN.getIncomingValue(0)) && |
| 8212 | PN.getIncomingValue(0)->hasOneUse()) |
| 8213 | if (Instruction *Result = FoldPHIArgOpIntoPHI(PN)) |
| 8214 | return Result; |
| 8215 | |
| 8216 | // If this is a trivial cycle in the PHI node graph, remove it. Basically, if |
| 8217 | // this PHI only has a single use (a PHI), and if that PHI only has one use (a |
| 8218 | // PHI)... break the cycle. |
Chris Lattner | ff9f13a | 2007-01-15 07:30:06 +0000 | [diff] [blame] | 8219 | if (PN.hasOneUse()) { |
| 8220 | Instruction *PHIUser = cast<Instruction>(PN.use_back()); |
| 8221 | if (PHINode *PU = dyn_cast<PHINode>(PHIUser)) { |
Chris Lattner | 0e5444b | 2007-03-26 20:40:50 +0000 | [diff] [blame] | 8222 | SmallPtrSet<PHINode*, 16> PotentiallyDeadPHIs; |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 8223 | PotentiallyDeadPHIs.insert(&PN); |
| 8224 | if (DeadPHICycle(PU, PotentiallyDeadPHIs)) |
| 8225 | return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType())); |
| 8226 | } |
Chris Lattner | ff9f13a | 2007-01-15 07:30:06 +0000 | [diff] [blame] | 8227 | |
| 8228 | // If this phi has a single use, and if that use just computes a value for |
| 8229 | // the next iteration of a loop, delete the phi. This occurs with unused |
| 8230 | // induction variables, e.g. "for (int j = 0; ; ++j);". Detecting this |
| 8231 | // common case here is good because the only other things that catch this |
| 8232 | // are induction variable analysis (sometimes) and ADCE, which is only run |
| 8233 | // late. |
| 8234 | if (PHIUser->hasOneUse() && |
| 8235 | (isa<BinaryOperator>(PHIUser) || isa<GetElementPtrInst>(PHIUser)) && |
| 8236 | PHIUser->use_back() == &PN) { |
| 8237 | return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType())); |
| 8238 | } |
| 8239 | } |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 8240 | |
Chris Lattner | 60921c9 | 2003-12-19 05:58:40 +0000 | [diff] [blame] | 8241 | return 0; |
Chris Lattner | 473945d | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 8242 | } |
| 8243 | |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 8244 | static Value *InsertCastToIntPtrTy(Value *V, const Type *DTy, |
| 8245 | Instruction *InsertPoint, |
| 8246 | InstCombiner *IC) { |
Reid Spencer | abaa8ca | 2007-01-08 16:32:00 +0000 | [diff] [blame] | 8247 | unsigned PtrSize = DTy->getPrimitiveSizeInBits(); |
| 8248 | unsigned VTySize = V->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 8249 | // We must cast correctly to the pointer type. Ensure that we |
| 8250 | // sign extend the integer value if it is smaller as this is |
| 8251 | // used for address computation. |
| 8252 | Instruction::CastOps opcode = |
| 8253 | (VTySize < PtrSize ? Instruction::SExt : |
| 8254 | (VTySize == PtrSize ? Instruction::BitCast : Instruction::Trunc)); |
| 8255 | return IC->InsertCastBefore(opcode, V, DTy, *InsertPoint); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 8256 | } |
| 8257 | |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 8258 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 8259 | Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 8260 | Value *PtrOp = GEP.getOperand(0); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 8261 | // Is it 'getelementptr %P, i32 0' or 'getelementptr %P' |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 8262 | // If so, eliminate the noop. |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 8263 | if (GEP.getNumOperands() == 1) |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 8264 | return ReplaceInstUsesWith(GEP, PtrOp); |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 8265 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8266 | if (isa<UndefValue>(GEP.getOperand(0))) |
| 8267 | return ReplaceInstUsesWith(GEP, UndefValue::get(GEP.getType())); |
| 8268 | |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 8269 | bool HasZeroPointerIndex = false; |
| 8270 | if (Constant *C = dyn_cast<Constant>(GEP.getOperand(1))) |
| 8271 | HasZeroPointerIndex = C->isNullValue(); |
| 8272 | |
| 8273 | if (GEP.getNumOperands() == 2 && HasZeroPointerIndex) |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 8274 | return ReplaceInstUsesWith(GEP, PtrOp); |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 8275 | |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 8276 | // Eliminate unneeded casts for indices. |
| 8277 | bool MadeChange = false; |
Chris Lattner | db9654e | 2007-03-25 20:43:09 +0000 | [diff] [blame] | 8278 | |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 8279 | gep_type_iterator GTI = gep_type_begin(GEP); |
Chris Lattner | db9654e | 2007-03-25 20:43:09 +0000 | [diff] [blame] | 8280 | for (unsigned i = 1, e = GEP.getNumOperands(); i != e; ++i, ++GTI) { |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 8281 | if (isa<SequentialType>(*GTI)) { |
| 8282 | if (CastInst *CI = dyn_cast<CastInst>(GEP.getOperand(i))) { |
Chris Lattner | 76b7a06 | 2007-01-15 07:02:54 +0000 | [diff] [blame] | 8283 | if (CI->getOpcode() == Instruction::ZExt || |
| 8284 | CI->getOpcode() == Instruction::SExt) { |
| 8285 | const Type *SrcTy = CI->getOperand(0)->getType(); |
| 8286 | // We can eliminate a cast from i32 to i64 iff the target |
| 8287 | // is a 32-bit pointer target. |
| 8288 | if (SrcTy->getPrimitiveSizeInBits() >= TD->getPointerSizeInBits()) { |
| 8289 | MadeChange = true; |
| 8290 | GEP.setOperand(i, CI->getOperand(0)); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 8291 | } |
| 8292 | } |
| 8293 | } |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 8294 | // If we are using a wider index than needed for this platform, shrink it |
| 8295 | // to what we need. If the incoming value needs a cast instruction, |
| 8296 | // insert it. This explicit cast can make subsequent optimizations more |
| 8297 | // obvious. |
| 8298 | Value *Op = GEP.getOperand(i); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 8299 | if (TD->getTypeSize(Op->getType()) > TD->getPointerSize()) |
Chris Lattner | 4f1134e | 2004-04-17 18:16:10 +0000 | [diff] [blame] | 8300 | if (Constant *C = dyn_cast<Constant>(Op)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8301 | GEP.setOperand(i, ConstantExpr::getTrunc(C, TD->getIntPtrType())); |
Chris Lattner | 4f1134e | 2004-04-17 18:16:10 +0000 | [diff] [blame] | 8302 | MadeChange = true; |
| 8303 | } else { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 8304 | Op = InsertCastBefore(Instruction::Trunc, Op, TD->getIntPtrType(), |
| 8305 | GEP); |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 8306 | GEP.setOperand(i, Op); |
| 8307 | MadeChange = true; |
| 8308 | } |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 8309 | } |
Chris Lattner | db9654e | 2007-03-25 20:43:09 +0000 | [diff] [blame] | 8310 | } |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 8311 | if (MadeChange) return &GEP; |
| 8312 | |
Chris Lattner | db9654e | 2007-03-25 20:43:09 +0000 | [diff] [blame] | 8313 | // If this GEP instruction doesn't move the pointer, and if the input operand |
| 8314 | // is a bitcast of another pointer, just replace the GEP with a bitcast of the |
| 8315 | // real input to the dest type. |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 8316 | if (GEP.hasAllZeroIndices() && isa<BitCastInst>(GEP.getOperand(0))) |
Chris Lattner | db9654e | 2007-03-25 20:43:09 +0000 | [diff] [blame] | 8317 | return new BitCastInst(cast<BitCastInst>(GEP.getOperand(0))->getOperand(0), |
| 8318 | GEP.getType()); |
| 8319 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 8320 | // Combine Indices - If the source pointer to this getelementptr instruction |
| 8321 | // is a getelementptr instruction, combine the indices of the two |
| 8322 | // getelementptr instructions into a single instruction. |
| 8323 | // |
Chris Lattner | 72588fc | 2007-02-15 22:48:32 +0000 | [diff] [blame] | 8324 | SmallVector<Value*, 8> SrcGEPOperands; |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 8325 | if (User *Src = dyn_castGetElementPtr(PtrOp)) |
Chris Lattner | 72588fc | 2007-02-15 22:48:32 +0000 | [diff] [blame] | 8326 | SrcGEPOperands.append(Src->op_begin(), Src->op_end()); |
Chris Lattner | ebd985c | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 8327 | |
| 8328 | if (!SrcGEPOperands.empty()) { |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 8329 | // Note that if our source is a gep chain itself that we wait for that |
| 8330 | // chain to be resolved before we perform this transformation. This |
| 8331 | // avoids us creating a TON of code in some cases. |
| 8332 | // |
| 8333 | if (isa<GetElementPtrInst>(SrcGEPOperands[0]) && |
| 8334 | cast<Instruction>(SrcGEPOperands[0])->getNumOperands() == 2) |
| 8335 | return 0; // Wait until our source is folded to completion. |
| 8336 | |
Chris Lattner | 72588fc | 2007-02-15 22:48:32 +0000 | [diff] [blame] | 8337 | SmallVector<Value*, 8> Indices; |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 8338 | |
| 8339 | // Find out whether the last index in the source GEP is a sequential idx. |
| 8340 | bool EndsWithSequential = false; |
| 8341 | for (gep_type_iterator I = gep_type_begin(*cast<User>(PtrOp)), |
| 8342 | E = gep_type_end(*cast<User>(PtrOp)); I != E; ++I) |
Chris Lattner | be97b4e | 2004-05-08 22:41:42 +0000 | [diff] [blame] | 8343 | EndsWithSequential = !isa<StructType>(*I); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8344 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 8345 | // Can we combine the two pointer arithmetics offsets? |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 8346 | if (EndsWithSequential) { |
Chris Lattner | decd081 | 2003-03-05 22:33:14 +0000 | [diff] [blame] | 8347 | // Replace: gep (gep %P, long B), long A, ... |
| 8348 | // With: T = long A+B; gep %P, T, ... |
| 8349 | // |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 8350 | Value *Sum, *SO1 = SrcGEPOperands.back(), *GO1 = GEP.getOperand(1); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 8351 | if (SO1 == Constant::getNullValue(SO1->getType())) { |
| 8352 | Sum = GO1; |
| 8353 | } else if (GO1 == Constant::getNullValue(GO1->getType())) { |
| 8354 | Sum = SO1; |
| 8355 | } else { |
| 8356 | // If they aren't the same type, convert both to an integer of the |
| 8357 | // target's pointer size. |
| 8358 | if (SO1->getType() != GO1->getType()) { |
| 8359 | if (Constant *SO1C = dyn_cast<Constant>(SO1)) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 8360 | SO1 = ConstantExpr::getIntegerCast(SO1C, GO1->getType(), true); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 8361 | } else if (Constant *GO1C = dyn_cast<Constant>(GO1)) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 8362 | GO1 = ConstantExpr::getIntegerCast(GO1C, SO1->getType(), true); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 8363 | } else { |
| 8364 | unsigned PS = TD->getPointerSize(); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 8365 | if (TD->getTypeSize(SO1->getType()) == PS) { |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 8366 | // Convert GO1 to SO1's type. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 8367 | GO1 = InsertCastToIntPtrTy(GO1, SO1->getType(), &GEP, this); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 8368 | |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 8369 | } else if (TD->getTypeSize(GO1->getType()) == PS) { |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 8370 | // Convert SO1 to GO1's type. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 8371 | SO1 = InsertCastToIntPtrTy(SO1, GO1->getType(), &GEP, this); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 8372 | } else { |
| 8373 | const Type *PT = TD->getIntPtrType(); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 8374 | SO1 = InsertCastToIntPtrTy(SO1, PT, &GEP, this); |
| 8375 | GO1 = InsertCastToIntPtrTy(GO1, PT, &GEP, this); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 8376 | } |
| 8377 | } |
| 8378 | } |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 8379 | if (isa<Constant>(SO1) && isa<Constant>(GO1)) |
| 8380 | Sum = ConstantExpr::getAdd(cast<Constant>(SO1), cast<Constant>(GO1)); |
| 8381 | else { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 8382 | Sum = BinaryOperator::createAdd(SO1, GO1, PtrOp->getName()+".sum"); |
| 8383 | InsertNewInstBefore(cast<Instruction>(Sum), GEP); |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 8384 | } |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 8385 | } |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 8386 | |
| 8387 | // Recycle the GEP we already have if possible. |
| 8388 | if (SrcGEPOperands.size() == 2) { |
| 8389 | GEP.setOperand(0, SrcGEPOperands[0]); |
| 8390 | GEP.setOperand(1, Sum); |
| 8391 | return &GEP; |
| 8392 | } else { |
| 8393 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, |
| 8394 | SrcGEPOperands.end()-1); |
| 8395 | Indices.push_back(Sum); |
| 8396 | Indices.insert(Indices.end(), GEP.op_begin()+2, GEP.op_end()); |
| 8397 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8398 | } else if (isa<Constant>(*GEP.idx_begin()) && |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 8399 | cast<Constant>(*GEP.idx_begin())->isNullValue() && |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8400 | SrcGEPOperands.size() != 1) { |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 8401 | // 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] | 8402 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, |
| 8403 | SrcGEPOperands.end()); |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 8404 | Indices.insert(Indices.end(), GEP.idx_begin()+1, GEP.idx_end()); |
| 8405 | } |
| 8406 | |
| 8407 | if (!Indices.empty()) |
Chris Lattner | 1ccd185 | 2007-02-12 22:56:41 +0000 | [diff] [blame] | 8408 | return new GetElementPtrInst(SrcGEPOperands[0], &Indices[0], |
| 8409 | Indices.size(), GEP.getName()); |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 8410 | |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 8411 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(PtrOp)) { |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 8412 | // GEP of global variable. If all of the indices for this GEP are |
| 8413 | // constants, we can promote this to a constexpr instead of an instruction. |
| 8414 | |
| 8415 | // Scan for nonconstants... |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 8416 | SmallVector<Constant*, 8> Indices; |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 8417 | User::op_iterator I = GEP.idx_begin(), E = GEP.idx_end(); |
| 8418 | for (; I != E && isa<Constant>(*I); ++I) |
| 8419 | Indices.push_back(cast<Constant>(*I)); |
| 8420 | |
| 8421 | if (I == E) { // If they are all constants... |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 8422 | Constant *CE = ConstantExpr::getGetElementPtr(GV, |
| 8423 | &Indices[0],Indices.size()); |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 8424 | |
| 8425 | // Replace all uses of the GEP with the new constexpr... |
| 8426 | return ReplaceInstUsesWith(GEP, CE); |
| 8427 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8428 | } else if (Value *X = getBitCastOperand(PtrOp)) { // Is the operand a cast? |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 8429 | if (!isa<PointerType>(X->getType())) { |
| 8430 | // Not interesting. Source pointer must be a cast from pointer. |
| 8431 | } else if (HasZeroPointerIndex) { |
| 8432 | // transform: GEP (cast [10 x ubyte]* X to [0 x ubyte]*), long 0, ... |
| 8433 | // into : GEP [10 x ubyte]* X, long 0, ... |
| 8434 | // |
| 8435 | // This occurs when the program declares an array extern like "int X[];" |
| 8436 | // |
| 8437 | const PointerType *CPTy = cast<PointerType>(PtrOp->getType()); |
| 8438 | const PointerType *XTy = cast<PointerType>(X->getType()); |
| 8439 | if (const ArrayType *XATy = |
| 8440 | dyn_cast<ArrayType>(XTy->getElementType())) |
| 8441 | if (const ArrayType *CATy = |
| 8442 | dyn_cast<ArrayType>(CPTy->getElementType())) |
| 8443 | if (CATy->getElementType() == XATy->getElementType()) { |
| 8444 | // At this point, we know that the cast source type is a pointer |
| 8445 | // to an array of the same type as the destination pointer |
| 8446 | // array. Because the array type is never stepped over (there |
| 8447 | // is a leading zero) we can fold the cast into this GEP. |
| 8448 | GEP.setOperand(0, X); |
| 8449 | return &GEP; |
| 8450 | } |
| 8451 | } else if (GEP.getNumOperands() == 2) { |
| 8452 | // Transform things like: |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 8453 | // %t = getelementptr ubyte* cast ([2 x int]* %str to uint*), uint %V |
| 8454 | // into: %t1 = getelementptr [2 x int*]* %str, int 0, uint %V; cast |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 8455 | const Type *SrcElTy = cast<PointerType>(X->getType())->getElementType(); |
| 8456 | const Type *ResElTy=cast<PointerType>(PtrOp->getType())->getElementType(); |
| 8457 | if (isa<ArrayType>(SrcElTy) && |
| 8458 | TD->getTypeSize(cast<ArrayType>(SrcElTy)->getElementType()) == |
| 8459 | TD->getTypeSize(ResElTy)) { |
| 8460 | Value *V = InsertNewInstBefore( |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8461 | new GetElementPtrInst(X, Constant::getNullValue(Type::Int32Ty), |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 8462 | GEP.getOperand(1), GEP.getName()), GEP); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8463 | // V and GEP are both pointer types --> BitCast |
| 8464 | return new BitCastInst(V, GEP.getType()); |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 8465 | } |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 8466 | |
| 8467 | // Transform things like: |
| 8468 | // getelementptr sbyte* cast ([100 x double]* X to sbyte*), int %tmp |
| 8469 | // (where tmp = 8*tmp2) into: |
| 8470 | // getelementptr [100 x double]* %arr, int 0, int %tmp.2 |
| 8471 | |
| 8472 | if (isa<ArrayType>(SrcElTy) && |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8473 | (ResElTy == Type::Int8Ty || ResElTy == Type::Int8Ty)) { |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 8474 | uint64_t ArrayEltSize = |
| 8475 | TD->getTypeSize(cast<ArrayType>(SrcElTy)->getElementType()); |
| 8476 | |
| 8477 | // Check to see if "tmp" is a scale by a multiple of ArrayEltSize. We |
| 8478 | // allow either a mul, shift, or constant here. |
| 8479 | Value *NewIdx = 0; |
| 8480 | ConstantInt *Scale = 0; |
| 8481 | if (ArrayEltSize == 1) { |
| 8482 | NewIdx = GEP.getOperand(1); |
| 8483 | Scale = ConstantInt::get(NewIdx->getType(), 1); |
| 8484 | } else if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP.getOperand(1))) { |
Chris Lattner | 6e2f843 | 2005-09-14 17:32:56 +0000 | [diff] [blame] | 8485 | NewIdx = ConstantInt::get(CI->getType(), 1); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 8486 | Scale = CI; |
| 8487 | } else if (Instruction *Inst =dyn_cast<Instruction>(GEP.getOperand(1))){ |
| 8488 | if (Inst->getOpcode() == Instruction::Shl && |
| 8489 | isa<ConstantInt>(Inst->getOperand(1))) { |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 8490 | ConstantInt *ShAmt = cast<ConstantInt>(Inst->getOperand(1)); |
| 8491 | uint32_t ShAmtVal = ShAmt->getLimitedValue(64); |
| 8492 | Scale = ConstantInt::get(Inst->getType(), 1ULL << ShAmtVal); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 8493 | NewIdx = Inst->getOperand(0); |
| 8494 | } else if (Inst->getOpcode() == Instruction::Mul && |
| 8495 | isa<ConstantInt>(Inst->getOperand(1))) { |
| 8496 | Scale = cast<ConstantInt>(Inst->getOperand(1)); |
| 8497 | NewIdx = Inst->getOperand(0); |
| 8498 | } |
| 8499 | } |
| 8500 | |
| 8501 | // If the index will be to exactly the right offset with the scale taken |
| 8502 | // out, perform the transformation. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8503 | if (Scale && Scale->getZExtValue() % ArrayEltSize == 0) { |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 8504 | if (isa<ConstantInt>(Scale)) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8505 | Scale = ConstantInt::get(Scale->getType(), |
| 8506 | Scale->getZExtValue() / ArrayEltSize); |
| 8507 | if (Scale->getZExtValue() != 1) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 8508 | Constant *C = ConstantExpr::getIntegerCast(Scale, NewIdx->getType(), |
| 8509 | true /*SExt*/); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 8510 | Instruction *Sc = BinaryOperator::createMul(NewIdx, C, "idxscale"); |
| 8511 | NewIdx = InsertNewInstBefore(Sc, GEP); |
| 8512 | } |
| 8513 | |
| 8514 | // Insert the new GEP instruction. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8515 | Instruction *NewGEP = |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8516 | new GetElementPtrInst(X, Constant::getNullValue(Type::Int32Ty), |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 8517 | NewIdx, GEP.getName()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8518 | NewGEP = InsertNewInstBefore(NewGEP, GEP); |
| 8519 | // The NewGEP must be pointer typed, so must the old one -> BitCast |
| 8520 | return new BitCastInst(NewGEP, GEP.getType()); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 8521 | } |
| 8522 | } |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 8523 | } |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 8524 | } |
| 8525 | |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 8526 | return 0; |
| 8527 | } |
| 8528 | |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 8529 | Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) { |
| 8530 | // Convert: malloc Ty, C - where C is a constant != 1 into: malloc [C x Ty], 1 |
| 8531 | if (AI.isArrayAllocation()) // Check C != 1 |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8532 | if (const ConstantInt *C = dyn_cast<ConstantInt>(AI.getArraySize())) { |
| 8533 | const Type *NewTy = |
| 8534 | ArrayType::get(AI.getAllocatedType(), C->getZExtValue()); |
Chris Lattner | 0006bd7 | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 8535 | AllocationInst *New = 0; |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 8536 | |
| 8537 | // Create and insert the replacement instruction... |
| 8538 | if (isa<MallocInst>(AI)) |
Nate Begeman | 14b0529 | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 8539 | New = new MallocInst(NewTy, 0, AI.getAlignment(), AI.getName()); |
Chris Lattner | 0006bd7 | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 8540 | else { |
| 8541 | assert(isa<AllocaInst>(AI) && "Unknown type of allocation inst!"); |
Nate Begeman | 14b0529 | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 8542 | New = new AllocaInst(NewTy, 0, AI.getAlignment(), AI.getName()); |
Chris Lattner | 0006bd7 | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 8543 | } |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 8544 | |
| 8545 | InsertNewInstBefore(New, AI); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8546 | |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 8547 | // Scan to the end of the allocation instructions, to skip over a block of |
| 8548 | // allocas if possible... |
| 8549 | // |
| 8550 | BasicBlock::iterator It = New; |
| 8551 | while (isa<AllocationInst>(*It)) ++It; |
| 8552 | |
| 8553 | // Now that I is pointing to the first non-allocation-inst in the block, |
| 8554 | // insert our getelementptr instruction... |
| 8555 | // |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8556 | Value *NullIdx = Constant::getNullValue(Type::Int32Ty); |
Chris Lattner | 693787a | 2005-05-04 19:10:26 +0000 | [diff] [blame] | 8557 | Value *V = new GetElementPtrInst(New, NullIdx, NullIdx, |
| 8558 | New->getName()+".sub", It); |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 8559 | |
| 8560 | // Now make everything use the getelementptr instead of the original |
| 8561 | // allocation. |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 8562 | return ReplaceInstUsesWith(AI, V); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8563 | } else if (isa<UndefValue>(AI.getArraySize())) { |
| 8564 | return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 8565 | } |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 8566 | |
| 8567 | // If alloca'ing a zero byte object, replace the alloca with a null pointer. |
| 8568 | // Note that we only do this for alloca's, because malloc should allocate and |
| 8569 | // return a unique pointer, even for a zero byte allocation. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8570 | if (isa<AllocaInst>(AI) && AI.getAllocatedType()->isSized() && |
Chris Lattner | cf27afb | 2004-07-02 22:55:47 +0000 | [diff] [blame] | 8571 | TD->getTypeSize(AI.getAllocatedType()) == 0) |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 8572 | return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); |
| 8573 | |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 8574 | return 0; |
| 8575 | } |
| 8576 | |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 8577 | Instruction *InstCombiner::visitFreeInst(FreeInst &FI) { |
| 8578 | Value *Op = FI.getOperand(0); |
| 8579 | |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8580 | // free undef -> unreachable. |
| 8581 | if (isa<UndefValue>(Op)) { |
| 8582 | // 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] | 8583 | new StoreInst(ConstantInt::getTrue(), |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 8584 | UndefValue::get(PointerType::get(Type::Int1Ty)), &FI); |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8585 | return EraseInstFromFunction(FI); |
| 8586 | } |
Chris Lattner | 6fe5541 | 2007-04-14 00:20:02 +0000 | [diff] [blame] | 8587 | |
Chris Lattner | 6160e85 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 8588 | // If we have 'free null' delete the instruction. This can happen in stl code |
| 8589 | // when lots of inlining happens. |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8590 | if (isa<ConstantPointerNull>(Op)) |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 8591 | return EraseInstFromFunction(FI); |
Chris Lattner | 6fe5541 | 2007-04-14 00:20:02 +0000 | [diff] [blame] | 8592 | |
| 8593 | // Change free <ty>* (cast <ty2>* X to <ty>*) into free <ty2>* X |
| 8594 | if (BitCastInst *CI = dyn_cast<BitCastInst>(Op)) { |
| 8595 | FI.setOperand(0, CI->getOperand(0)); |
| 8596 | return &FI; |
| 8597 | } |
| 8598 | |
| 8599 | // Change free (gep X, 0,0,0,0) into free(X) |
| 8600 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) { |
| 8601 | if (GEPI->hasAllZeroIndices()) { |
| 8602 | AddToWorkList(GEPI); |
| 8603 | FI.setOperand(0, GEPI->getOperand(0)); |
| 8604 | return &FI; |
| 8605 | } |
| 8606 | } |
| 8607 | |
| 8608 | // Change free(malloc) into nothing, if the malloc has a single use. |
| 8609 | if (MallocInst *MI = dyn_cast<MallocInst>(Op)) |
| 8610 | if (MI->hasOneUse()) { |
| 8611 | EraseInstFromFunction(FI); |
| 8612 | return EraseInstFromFunction(*MI); |
| 8613 | } |
Chris Lattner | 6160e85 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 8614 | |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 8615 | return 0; |
| 8616 | } |
| 8617 | |
| 8618 | |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8619 | /// InstCombineLoadCast - Fold 'load (cast P)' -> cast (load P)' when possible. |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 8620 | static Instruction *InstCombineLoadCast(InstCombiner &IC, LoadInst &LI) { |
| 8621 | User *CI = cast<User>(LI.getOperand(0)); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 8622 | Value *CastOp = CI->getOperand(0); |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 8623 | |
| 8624 | const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType(); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 8625 | if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) { |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 8626 | const Type *SrcPTy = SrcTy->getElementType(); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 8627 | |
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 8628 | if (DestPTy->isInteger() || isa<PointerType>(DestPTy) || |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 8629 | isa<VectorType>(DestPTy)) { |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 8630 | // If the source is an array, the code below will not succeed. Check to |
| 8631 | // see if a trivial 'gep P, 0, 0' will help matters. Only do this for |
| 8632 | // constants. |
| 8633 | if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy)) |
| 8634 | if (Constant *CSrc = dyn_cast<Constant>(CastOp)) |
| 8635 | if (ASrcTy->getNumElements() != 0) { |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 8636 | Value *Idxs[2]; |
| 8637 | Idxs[0] = Idxs[1] = Constant::getNullValue(Type::Int32Ty); |
| 8638 | CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs, 2); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 8639 | SrcTy = cast<PointerType>(CastOp->getType()); |
| 8640 | SrcPTy = SrcTy->getElementType(); |
| 8641 | } |
| 8642 | |
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 8643 | if ((SrcPTy->isInteger() || isa<PointerType>(SrcPTy) || |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 8644 | isa<VectorType>(SrcPTy)) && |
Chris Lattner | b1515fe | 2005-03-29 06:37:47 +0000 | [diff] [blame] | 8645 | // Do not allow turning this into a load of an integer, which is then |
| 8646 | // casted to a pointer, this pessimizes pointer analysis a lot. |
| 8647 | (isa<PointerType>(SrcPTy) == isa<PointerType>(LI.getType())) && |
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 8648 | IC.getTargetData().getTypeSizeInBits(SrcPTy) == |
| 8649 | IC.getTargetData().getTypeSizeInBits(DestPTy)) { |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8650 | |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 8651 | // Okay, we are casting from one integer or pointer type to another of |
| 8652 | // the same size. Instead of casting the pointer before the load, cast |
| 8653 | // the result of the loaded value. |
| 8654 | Value *NewLoad = IC.InsertNewInstBefore(new LoadInst(CastOp, |
| 8655 | CI->getName(), |
| 8656 | LI.isVolatile()),LI); |
| 8657 | // Now cast the result of the load. |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 8658 | return new BitCastInst(NewLoad, LI.getType()); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 8659 | } |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 8660 | } |
| 8661 | } |
| 8662 | return 0; |
| 8663 | } |
| 8664 | |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 8665 | /// isSafeToLoadUnconditionally - Return true if we know that executing a load |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 8666 | /// from this value cannot trap. If it is not obviously safe to load from the |
| 8667 | /// specified pointer, we do a quick local scan of the basic block containing |
| 8668 | /// ScanFrom, to determine if the address is already accessed. |
| 8669 | static bool isSafeToLoadUnconditionally(Value *V, Instruction *ScanFrom) { |
| 8670 | // If it is an alloca or global variable, it is always safe to load from. |
| 8671 | if (isa<AllocaInst>(V) || isa<GlobalVariable>(V)) return true; |
| 8672 | |
| 8673 | // Otherwise, be a little bit agressive by scanning the local block where we |
| 8674 | // 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] | 8675 | // from/to. If so, the previous load or store would have already trapped, |
| 8676 | // so there is no harm doing an extra load (also, CSE will later eliminate |
| 8677 | // the load entirely). |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 8678 | BasicBlock::iterator BBI = ScanFrom, E = ScanFrom->getParent()->begin(); |
| 8679 | |
Alkis Evlogimenos | 7b6ec60 | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 8680 | while (BBI != E) { |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 8681 | --BBI; |
| 8682 | |
| 8683 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { |
| 8684 | if (LI->getOperand(0) == V) return true; |
| 8685 | } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) |
| 8686 | if (SI->getOperand(1) == V) return true; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8687 | |
Alkis Evlogimenos | 7b6ec60 | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 8688 | } |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 8689 | return false; |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 8690 | } |
| 8691 | |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 8692 | Instruction *InstCombiner::visitLoadInst(LoadInst &LI) { |
| 8693 | Value *Op = LI.getOperand(0); |
Chris Lattner | 5f16a13 | 2004-01-12 04:13:56 +0000 | [diff] [blame] | 8694 | |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 8695 | // load (cast X) --> cast (load X) iff safe |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 8696 | if (isa<CastInst>(Op)) |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 8697 | if (Instruction *Res = InstCombineLoadCast(*this, LI)) |
| 8698 | return Res; |
| 8699 | |
| 8700 | // None of the following transforms are legal for volatile loads. |
| 8701 | if (LI.isVolatile()) return 0; |
Chris Lattner | 62f254d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 8702 | |
Chris Lattner | 62f254d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 8703 | if (&LI.getParent()->front() != &LI) { |
| 8704 | BasicBlock::iterator BBI = &LI; --BBI; |
Chris Lattner | 9c1f0fd | 2005-09-12 22:21:03 +0000 | [diff] [blame] | 8705 | // If the instruction immediately before this is a store to the same |
| 8706 | // address, do a simple form of store->load forwarding. |
Chris Lattner | 62f254d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 8707 | if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) |
| 8708 | if (SI->getOperand(1) == LI.getOperand(0)) |
| 8709 | return ReplaceInstUsesWith(LI, SI->getOperand(0)); |
Chris Lattner | 9c1f0fd | 2005-09-12 22:21:03 +0000 | [diff] [blame] | 8710 | if (LoadInst *LIB = dyn_cast<LoadInst>(BBI)) |
| 8711 | if (LIB->getOperand(0) == LI.getOperand(0)) |
| 8712 | return ReplaceInstUsesWith(LI, LIB); |
Chris Lattner | 62f254d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 8713 | } |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 8714 | |
| 8715 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 8716 | if (isa<ConstantPointerNull>(GEPI->getOperand(0))) { |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 8717 | // Insert a new store to null instruction before the load to indicate |
| 8718 | // that this code is not reachable. We do this instead of inserting |
| 8719 | // an unreachable instruction directly because we cannot modify the |
| 8720 | // CFG. |
| 8721 | new StoreInst(UndefValue::get(LI.getType()), |
| 8722 | Constant::getNullValue(Op->getType()), &LI); |
| 8723 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
| 8724 | } |
| 8725 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8726 | if (Constant *C = dyn_cast<Constant>(Op)) { |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 8727 | // load null/undef -> undef |
| 8728 | if ((C->isNullValue() || isa<UndefValue>(C))) { |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8729 | // Insert a new store to null instruction before the load to indicate that |
| 8730 | // this code is not reachable. We do this instead of inserting an |
| 8731 | // unreachable instruction directly because we cannot modify the CFG. |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 8732 | new StoreInst(UndefValue::get(LI.getType()), |
| 8733 | Constant::getNullValue(Op->getType()), &LI); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8734 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8735 | } |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 8736 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8737 | // Instcombine load (constant global) into the value loaded. |
| 8738 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op)) |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 8739 | if (GV->isConstant() && !GV->isDeclaration()) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8740 | return ReplaceInstUsesWith(LI, GV->getInitializer()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8741 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8742 | // Instcombine load (constantexpr_GEP global, 0, ...) into the value loaded. |
| 8743 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op)) |
| 8744 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 8745 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0))) |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 8746 | if (GV->isConstant() && !GV->isDeclaration()) |
Chris Lattner | 363f2a2 | 2005-09-26 05:28:06 +0000 | [diff] [blame] | 8747 | if (Constant *V = |
| 8748 | ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE)) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8749 | return ReplaceInstUsesWith(LI, V); |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 8750 | if (CE->getOperand(0)->isNullValue()) { |
| 8751 | // Insert a new store to null instruction before the load to indicate |
| 8752 | // that this code is not reachable. We do this instead of inserting |
| 8753 | // an unreachable instruction directly because we cannot modify the |
| 8754 | // CFG. |
| 8755 | new StoreInst(UndefValue::get(LI.getType()), |
| 8756 | Constant::getNullValue(Op->getType()), &LI); |
| 8757 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
| 8758 | } |
| 8759 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8760 | } else if (CE->isCast()) { |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8761 | if (Instruction *Res = InstCombineLoadCast(*this, LI)) |
| 8762 | return Res; |
| 8763 | } |
| 8764 | } |
Chris Lattner | f499eac | 2004-04-08 20:39:49 +0000 | [diff] [blame] | 8765 | |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 8766 | if (Op->hasOneUse()) { |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 8767 | // Change select and PHI nodes to select values instead of addresses: this |
| 8768 | // helps alias analysis out a lot, allows many others simplifications, and |
| 8769 | // exposes redundancy in the code. |
| 8770 | // |
| 8771 | // Note that we cannot do the transformation unless we know that the |
| 8772 | // introduced loads cannot trap! Something like this is valid as long as |
| 8773 | // the condition is always false: load (select bool %C, int* null, int* %G), |
| 8774 | // but it would not be valid if we transformed it to load from null |
| 8775 | // unconditionally. |
| 8776 | // |
| 8777 | if (SelectInst *SI = dyn_cast<SelectInst>(Op)) { |
| 8778 | // load (select (Cond, &V1, &V2)) --> select(Cond, load &V1, load &V2). |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 8779 | if (isSafeToLoadUnconditionally(SI->getOperand(1), SI) && |
| 8780 | isSafeToLoadUnconditionally(SI->getOperand(2), SI)) { |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 8781 | Value *V1 = InsertNewInstBefore(new LoadInst(SI->getOperand(1), |
Chris Lattner | 79f0c8e | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 8782 | SI->getOperand(1)->getName()+".val"), LI); |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 8783 | Value *V2 = InsertNewInstBefore(new LoadInst(SI->getOperand(2), |
Chris Lattner | 79f0c8e | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 8784 | SI->getOperand(2)->getName()+".val"), LI); |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 8785 | return new SelectInst(SI->getCondition(), V1, V2); |
| 8786 | } |
| 8787 | |
Chris Lattner | 684fe21 | 2004-09-23 15:46:00 +0000 | [diff] [blame] | 8788 | // load (select (cond, null, P)) -> load P |
| 8789 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(1))) |
| 8790 | if (C->isNullValue()) { |
| 8791 | LI.setOperand(0, SI->getOperand(2)); |
| 8792 | return &LI; |
| 8793 | } |
| 8794 | |
| 8795 | // load (select (cond, P, null)) -> load P |
| 8796 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(2))) |
| 8797 | if (C->isNullValue()) { |
| 8798 | LI.setOperand(0, SI->getOperand(1)); |
| 8799 | return &LI; |
| 8800 | } |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 8801 | } |
| 8802 | } |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 8803 | return 0; |
| 8804 | } |
| 8805 | |
Reid Spencer | 55af2b5 | 2007-01-19 21:20:31 +0000 | [diff] [blame] | 8806 | /// InstCombineStoreToCast - Fold store V, (cast P) -> store (cast V), P |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8807 | /// when possible. |
| 8808 | static Instruction *InstCombineStoreToCast(InstCombiner &IC, StoreInst &SI) { |
| 8809 | User *CI = cast<User>(SI.getOperand(1)); |
| 8810 | Value *CastOp = CI->getOperand(0); |
| 8811 | |
| 8812 | const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType(); |
| 8813 | if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) { |
| 8814 | const Type *SrcPTy = SrcTy->getElementType(); |
| 8815 | |
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 8816 | if (DestPTy->isInteger() || isa<PointerType>(DestPTy)) { |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8817 | // If the source is an array, the code below will not succeed. Check to |
| 8818 | // see if a trivial 'gep P, 0, 0' will help matters. Only do this for |
| 8819 | // constants. |
| 8820 | if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy)) |
| 8821 | if (Constant *CSrc = dyn_cast<Constant>(CastOp)) |
| 8822 | if (ASrcTy->getNumElements() != 0) { |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 8823 | Value* Idxs[2]; |
| 8824 | Idxs[0] = Idxs[1] = Constant::getNullValue(Type::Int32Ty); |
| 8825 | CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs, 2); |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8826 | SrcTy = cast<PointerType>(CastOp->getType()); |
| 8827 | SrcPTy = SrcTy->getElementType(); |
| 8828 | } |
| 8829 | |
Reid Spencer | 67f827c | 2007-01-20 23:35:48 +0000 | [diff] [blame] | 8830 | if ((SrcPTy->isInteger() || isa<PointerType>(SrcPTy)) && |
| 8831 | IC.getTargetData().getTypeSizeInBits(SrcPTy) == |
| 8832 | IC.getTargetData().getTypeSizeInBits(DestPTy)) { |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8833 | |
| 8834 | // 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] | 8835 | // the same size. Instead of casting the pointer before |
| 8836 | // the store, cast the value to be stored. |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8837 | Value *NewCast; |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 8838 | Value *SIOp0 = SI.getOperand(0); |
Reid Spencer | 7515396 | 2007-01-18 18:54:33 +0000 | [diff] [blame] | 8839 | Instruction::CastOps opcode = Instruction::BitCast; |
| 8840 | const Type* CastSrcTy = SIOp0->getType(); |
| 8841 | const Type* CastDstTy = SrcPTy; |
| 8842 | if (isa<PointerType>(CastDstTy)) { |
| 8843 | if (CastSrcTy->isInteger()) |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 8844 | opcode = Instruction::IntToPtr; |
Reid Spencer | 67f827c | 2007-01-20 23:35:48 +0000 | [diff] [blame] | 8845 | } else if (isa<IntegerType>(CastDstTy)) { |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 8846 | if (isa<PointerType>(SIOp0->getType())) |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 8847 | opcode = Instruction::PtrToInt; |
| 8848 | } |
| 8849 | if (Constant *C = dyn_cast<Constant>(SIOp0)) |
Reid Spencer | 7515396 | 2007-01-18 18:54:33 +0000 | [diff] [blame] | 8850 | NewCast = ConstantExpr::getCast(opcode, C, CastDstTy); |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8851 | else |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8852 | NewCast = IC.InsertNewInstBefore( |
Reid Spencer | 7515396 | 2007-01-18 18:54:33 +0000 | [diff] [blame] | 8853 | CastInst::create(opcode, SIOp0, CastDstTy, SIOp0->getName()+".c"), |
| 8854 | SI); |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8855 | return new StoreInst(NewCast, CastOp); |
| 8856 | } |
| 8857 | } |
| 8858 | } |
| 8859 | return 0; |
| 8860 | } |
| 8861 | |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 8862 | Instruction *InstCombiner::visitStoreInst(StoreInst &SI) { |
| 8863 | Value *Val = SI.getOperand(0); |
| 8864 | Value *Ptr = SI.getOperand(1); |
| 8865 | |
| 8866 | if (isa<UndefValue>(Ptr)) { // store X, undef -> noop (even if volatile) |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 8867 | EraseInstFromFunction(SI); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 8868 | ++NumCombined; |
| 8869 | return 0; |
| 8870 | } |
Chris Lattner | 836692d | 2007-01-15 06:51:56 +0000 | [diff] [blame] | 8871 | |
| 8872 | // If the RHS is an alloca with a single use, zapify the store, making the |
| 8873 | // alloca dead. |
| 8874 | if (Ptr->hasOneUse()) { |
| 8875 | if (isa<AllocaInst>(Ptr)) { |
| 8876 | EraseInstFromFunction(SI); |
| 8877 | ++NumCombined; |
| 8878 | return 0; |
| 8879 | } |
| 8880 | |
| 8881 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) |
| 8882 | if (isa<AllocaInst>(GEP->getOperand(0)) && |
| 8883 | GEP->getOperand(0)->hasOneUse()) { |
| 8884 | EraseInstFromFunction(SI); |
| 8885 | ++NumCombined; |
| 8886 | return 0; |
| 8887 | } |
| 8888 | } |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 8889 | |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 8890 | // Do really simple DSE, to catch cases where there are several consequtive |
| 8891 | // stores to the same location, separated by a few arithmetic operations. This |
| 8892 | // situation often occurs with bitfield accesses. |
| 8893 | BasicBlock::iterator BBI = &SI; |
| 8894 | for (unsigned ScanInsts = 6; BBI != SI.getParent()->begin() && ScanInsts; |
| 8895 | --ScanInsts) { |
| 8896 | --BBI; |
| 8897 | |
| 8898 | if (StoreInst *PrevSI = dyn_cast<StoreInst>(BBI)) { |
| 8899 | // Prev store isn't volatile, and stores to the same location? |
| 8900 | if (!PrevSI->isVolatile() && PrevSI->getOperand(1) == SI.getOperand(1)) { |
| 8901 | ++NumDeadStore; |
| 8902 | ++BBI; |
| 8903 | EraseInstFromFunction(*PrevSI); |
| 8904 | continue; |
| 8905 | } |
| 8906 | break; |
| 8907 | } |
| 8908 | |
Chris Lattner | b4db97f | 2006-05-26 19:19:20 +0000 | [diff] [blame] | 8909 | // If this is a load, we have to stop. However, if the loaded value is from |
| 8910 | // the pointer we're loading and is producing the pointer we're storing, |
| 8911 | // then *this* store is dead (X = load P; store X -> P). |
| 8912 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { |
| 8913 | if (LI == Val && LI->getOperand(0) == Ptr) { |
| 8914 | EraseInstFromFunction(SI); |
| 8915 | ++NumCombined; |
| 8916 | return 0; |
| 8917 | } |
| 8918 | // Otherwise, this is a load from some other location. Stores before it |
| 8919 | // may not be dead. |
| 8920 | break; |
| 8921 | } |
| 8922 | |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 8923 | // Don't skip over loads or things that can modify memory. |
Chris Lattner | b4db97f | 2006-05-26 19:19:20 +0000 | [diff] [blame] | 8924 | if (BBI->mayWriteToMemory()) |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 8925 | break; |
| 8926 | } |
| 8927 | |
| 8928 | |
| 8929 | if (SI.isVolatile()) return 0; // Don't hack volatile stores. |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 8930 | |
| 8931 | // store X, null -> turns into 'unreachable' in SimplifyCFG |
| 8932 | if (isa<ConstantPointerNull>(Ptr)) { |
| 8933 | if (!isa<UndefValue>(Val)) { |
| 8934 | SI.setOperand(0, UndefValue::get(Val->getType())); |
| 8935 | if (Instruction *U = dyn_cast<Instruction>(Val)) |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 8936 | AddToWorkList(U); // Dropped a use. |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 8937 | ++NumCombined; |
| 8938 | } |
| 8939 | return 0; // Do not modify these! |
| 8940 | } |
| 8941 | |
| 8942 | // store undef, Ptr -> noop |
| 8943 | if (isa<UndefValue>(Val)) { |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 8944 | EraseInstFromFunction(SI); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 8945 | ++NumCombined; |
| 8946 | return 0; |
| 8947 | } |
| 8948 | |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8949 | // If the pointer destination is a cast, see if we can fold the cast into the |
| 8950 | // source instead. |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 8951 | if (isa<CastInst>(Ptr)) |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8952 | if (Instruction *Res = InstCombineStoreToCast(*this, SI)) |
| 8953 | return Res; |
| 8954 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8955 | if (CE->isCast()) |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8956 | if (Instruction *Res = InstCombineStoreToCast(*this, SI)) |
| 8957 | return Res; |
| 8958 | |
Chris Lattner | 408902b | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 8959 | |
| 8960 | // If this store is the last instruction in the basic block, and if the block |
| 8961 | // 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] | 8962 | BBI = &SI; ++BBI; |
Chris Lattner | 408902b | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 8963 | if (BranchInst *BI = dyn_cast<BranchInst>(BBI)) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 8964 | if (BI->isUnconditional()) |
| 8965 | if (SimplifyStoreAtEndOfBlock(SI)) |
| 8966 | return 0; // xform done! |
Chris Lattner | 408902b | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 8967 | |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 8968 | return 0; |
| 8969 | } |
| 8970 | |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 8971 | /// SimplifyStoreAtEndOfBlock - Turn things like: |
| 8972 | /// if () { *P = v1; } else { *P = v2 } |
| 8973 | /// into a phi node with a store in the successor. |
| 8974 | /// |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 8975 | /// Simplify things like: |
| 8976 | /// *P = v1; if () { *P = v2; } |
| 8977 | /// into a phi node with a store in the successor. |
| 8978 | /// |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 8979 | bool InstCombiner::SimplifyStoreAtEndOfBlock(StoreInst &SI) { |
| 8980 | BasicBlock *StoreBB = SI.getParent(); |
| 8981 | |
| 8982 | // Check to see if the successor block has exactly two incoming edges. If |
| 8983 | // so, see if the other predecessor contains a store to the same location. |
| 8984 | // 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] | 8985 | BasicBlock *DestBB = StoreBB->getTerminator()->getSuccessor(0); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 8986 | |
| 8987 | // Determine whether Dest has exactly two predecessors and, if so, compute |
| 8988 | // the other predecessor. |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 8989 | pred_iterator PI = pred_begin(DestBB); |
| 8990 | BasicBlock *OtherBB = 0; |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 8991 | if (*PI != StoreBB) |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 8992 | OtherBB = *PI; |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 8993 | ++PI; |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 8994 | if (PI == pred_end(DestBB)) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 8995 | return false; |
| 8996 | |
| 8997 | if (*PI != StoreBB) { |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 8998 | if (OtherBB) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 8999 | return false; |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 9000 | OtherBB = *PI; |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 9001 | } |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 9002 | if (++PI != pred_end(DestBB)) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 9003 | return false; |
| 9004 | |
| 9005 | |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 9006 | // Verify that the other block ends in a branch and is not otherwise empty. |
| 9007 | BasicBlock::iterator BBI = OtherBB->getTerminator(); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 9008 | BranchInst *OtherBr = dyn_cast<BranchInst>(BBI); |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 9009 | if (!OtherBr || BBI == OtherBB->begin()) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 9010 | return false; |
| 9011 | |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 9012 | // If the other block ends in an unconditional branch, check for the 'if then |
| 9013 | // else' case. there is an instruction before the branch. |
| 9014 | StoreInst *OtherStore = 0; |
| 9015 | if (OtherBr->isUnconditional()) { |
| 9016 | // If this isn't a store, or isn't a store to the same location, bail out. |
| 9017 | --BBI; |
| 9018 | OtherStore = dyn_cast<StoreInst>(BBI); |
| 9019 | if (!OtherStore || OtherStore->getOperand(1) != SI.getOperand(1)) |
| 9020 | return false; |
| 9021 | } else { |
Chris Lattner | d717c18 | 2007-05-05 22:32:24 +0000 | [diff] [blame] | 9022 | // 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] | 9023 | // destinations is StoreBB, then we have the if/then case. |
| 9024 | if (OtherBr->getSuccessor(0) != StoreBB && |
| 9025 | OtherBr->getSuccessor(1) != StoreBB) |
| 9026 | return false; |
| 9027 | |
| 9028 | // 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] | 9029 | // if/then triangle. See if there is a store to the same ptr as SI that |
| 9030 | // lives in OtherBB. |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 9031 | for (;; --BBI) { |
| 9032 | // Check to see if we find the matching store. |
| 9033 | if ((OtherStore = dyn_cast<StoreInst>(BBI))) { |
| 9034 | if (OtherStore->getOperand(1) != SI.getOperand(1)) |
| 9035 | return false; |
| 9036 | break; |
| 9037 | } |
Chris Lattner | d717c18 | 2007-05-05 22:32:24 +0000 | [diff] [blame] | 9038 | // If we find something that may be using the stored value, or if we run |
| 9039 | // out of instructions, we can't do the xform. |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 9040 | if (isa<LoadInst>(BBI) || BBI->mayWriteToMemory() || |
| 9041 | BBI == OtherBB->begin()) |
| 9042 | return false; |
| 9043 | } |
| 9044 | |
| 9045 | // In order to eliminate the store in OtherBr, we have to |
| 9046 | // make sure nothing reads the stored value in StoreBB. |
| 9047 | for (BasicBlock::iterator I = StoreBB->begin(); &*I != &SI; ++I) { |
| 9048 | // FIXME: This should really be AA driven. |
| 9049 | if (isa<LoadInst>(I) || I->mayWriteToMemory()) |
| 9050 | return false; |
| 9051 | } |
| 9052 | } |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 9053 | |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 9054 | // Insert a PHI node now if we need it. |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 9055 | Value *MergedVal = OtherStore->getOperand(0); |
| 9056 | if (MergedVal != SI.getOperand(0)) { |
| 9057 | PHINode *PN = new PHINode(MergedVal->getType(), "storemerge"); |
| 9058 | PN->reserveOperandSpace(2); |
| 9059 | PN->addIncoming(SI.getOperand(0), SI.getParent()); |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 9060 | PN->addIncoming(OtherStore->getOperand(0), OtherBB); |
| 9061 | MergedVal = InsertNewInstBefore(PN, DestBB->front()); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 9062 | } |
| 9063 | |
| 9064 | // Advance to a place where it is safe to insert the new store and |
| 9065 | // insert it. |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 9066 | BBI = DestBB->begin(); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 9067 | while (isa<PHINode>(BBI)) ++BBI; |
| 9068 | InsertNewInstBefore(new StoreInst(MergedVal, SI.getOperand(1), |
| 9069 | OtherStore->isVolatile()), *BBI); |
| 9070 | |
| 9071 | // Nuke the old stores. |
| 9072 | EraseInstFromFunction(SI); |
| 9073 | EraseInstFromFunction(*OtherStore); |
| 9074 | ++NumCombined; |
| 9075 | return true; |
| 9076 | } |
| 9077 | |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 9078 | |
Chris Lattner | c4d10eb | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 9079 | Instruction *InstCombiner::visitBranchInst(BranchInst &BI) { |
| 9080 | // 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] | 9081 | Value *X = 0; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 9082 | BasicBlock *TrueDest; |
| 9083 | BasicBlock *FalseDest; |
| 9084 | if (match(&BI, m_Br(m_Not(m_Value(X)), TrueDest, FalseDest)) && |
| 9085 | !isa<Constant>(X)) { |
| 9086 | // Swap Destinations and condition... |
| 9087 | BI.setCondition(X); |
| 9088 | BI.setSuccessor(0, FalseDest); |
| 9089 | BI.setSuccessor(1, TrueDest); |
| 9090 | return &BI; |
| 9091 | } |
| 9092 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9093 | // Cannonicalize fcmp_one -> fcmp_oeq |
| 9094 | FCmpInst::Predicate FPred; Value *Y; |
| 9095 | if (match(&BI, m_Br(m_FCmp(FPred, m_Value(X), m_Value(Y)), |
| 9096 | TrueDest, FalseDest))) |
| 9097 | if ((FPred == FCmpInst::FCMP_ONE || FPred == FCmpInst::FCMP_OLE || |
| 9098 | FPred == FCmpInst::FCMP_OGE) && BI.getCondition()->hasOneUse()) { |
| 9099 | FCmpInst *I = cast<FCmpInst>(BI.getCondition()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9100 | FCmpInst::Predicate NewPred = FCmpInst::getInversePredicate(FPred); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 9101 | Instruction *NewSCC = new FCmpInst(NewPred, X, Y, "", I); |
| 9102 | NewSCC->takeName(I); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9103 | // Swap Destinations and condition... |
| 9104 | BI.setCondition(NewSCC); |
| 9105 | BI.setSuccessor(0, FalseDest); |
| 9106 | BI.setSuccessor(1, TrueDest); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9107 | RemoveFromWorkList(I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 9108 | I->eraseFromParent(); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9109 | AddToWorkList(NewSCC); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9110 | return &BI; |
| 9111 | } |
| 9112 | |
| 9113 | // Cannonicalize icmp_ne -> icmp_eq |
| 9114 | ICmpInst::Predicate IPred; |
| 9115 | if (match(&BI, m_Br(m_ICmp(IPred, m_Value(X), m_Value(Y)), |
| 9116 | TrueDest, FalseDest))) |
| 9117 | if ((IPred == ICmpInst::ICMP_NE || IPred == ICmpInst::ICMP_ULE || |
| 9118 | IPred == ICmpInst::ICMP_SLE || IPred == ICmpInst::ICMP_UGE || |
| 9119 | IPred == ICmpInst::ICMP_SGE) && BI.getCondition()->hasOneUse()) { |
| 9120 | ICmpInst *I = cast<ICmpInst>(BI.getCondition()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9121 | ICmpInst::Predicate NewPred = ICmpInst::getInversePredicate(IPred); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 9122 | Instruction *NewSCC = new ICmpInst(NewPred, X, Y, "", I); |
| 9123 | NewSCC->takeName(I); |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 9124 | // Swap Destinations and condition... |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 9125 | BI.setCondition(NewSCC); |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 9126 | BI.setSuccessor(0, FalseDest); |
| 9127 | BI.setSuccessor(1, TrueDest); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9128 | RemoveFromWorkList(I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 9129 | I->eraseFromParent();; |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9130 | AddToWorkList(NewSCC); |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 9131 | return &BI; |
| 9132 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9133 | |
Chris Lattner | c4d10eb | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 9134 | return 0; |
| 9135 | } |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 9136 | |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 9137 | Instruction *InstCombiner::visitSwitchInst(SwitchInst &SI) { |
| 9138 | Value *Cond = SI.getCondition(); |
| 9139 | if (Instruction *I = dyn_cast<Instruction>(Cond)) { |
| 9140 | if (I->getOpcode() == Instruction::Add) |
| 9141 | if (ConstantInt *AddRHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 9142 | // change 'switch (X+4) case 1:' into 'switch (X) case -3' |
| 9143 | for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 9144 | SI.setOperand(i,ConstantExpr::getSub(cast<Constant>(SI.getOperand(i)), |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 9145 | AddRHS)); |
| 9146 | SI.setOperand(0, I->getOperand(0)); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9147 | AddToWorkList(I); |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 9148 | return &SI; |
| 9149 | } |
| 9150 | } |
| 9151 | return 0; |
| 9152 | } |
| 9153 | |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 9154 | /// CheapToScalarize - Return true if the value is cheaper to scalarize than it |
| 9155 | /// is to leave as a vector operation. |
| 9156 | static bool CheapToScalarize(Value *V, bool isConstant) { |
| 9157 | if (isa<ConstantAggregateZero>(V)) |
| 9158 | return true; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 9159 | if (ConstantVector *C = dyn_cast<ConstantVector>(V)) { |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 9160 | if (isConstant) return true; |
| 9161 | // If all elts are the same, we can extract. |
| 9162 | Constant *Op0 = C->getOperand(0); |
| 9163 | for (unsigned i = 1; i < C->getNumOperands(); ++i) |
| 9164 | if (C->getOperand(i) != Op0) |
| 9165 | return false; |
| 9166 | return true; |
| 9167 | } |
| 9168 | Instruction *I = dyn_cast<Instruction>(V); |
| 9169 | if (!I) return false; |
| 9170 | |
| 9171 | // Insert element gets simplified to the inserted element or is deleted if |
| 9172 | // this is constant idx extract element and its a constant idx insertelt. |
| 9173 | if (I->getOpcode() == Instruction::InsertElement && isConstant && |
| 9174 | isa<ConstantInt>(I->getOperand(2))) |
| 9175 | return true; |
| 9176 | if (I->getOpcode() == Instruction::Load && I->hasOneUse()) |
| 9177 | return true; |
| 9178 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) |
| 9179 | if (BO->hasOneUse() && |
| 9180 | (CheapToScalarize(BO->getOperand(0), isConstant) || |
| 9181 | CheapToScalarize(BO->getOperand(1), isConstant))) |
| 9182 | return true; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9183 | if (CmpInst *CI = dyn_cast<CmpInst>(I)) |
| 9184 | if (CI->hasOneUse() && |
| 9185 | (CheapToScalarize(CI->getOperand(0), isConstant) || |
| 9186 | CheapToScalarize(CI->getOperand(1), isConstant))) |
| 9187 | return true; |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 9188 | |
| 9189 | return false; |
| 9190 | } |
| 9191 | |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 9192 | /// Read and decode a shufflevector mask. |
| 9193 | /// |
| 9194 | /// It turns undef elements into values that are larger than the number of |
| 9195 | /// elements in the input. |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 9196 | static std::vector<unsigned> getShuffleMask(const ShuffleVectorInst *SVI) { |
| 9197 | unsigned NElts = SVI->getType()->getNumElements(); |
| 9198 | if (isa<ConstantAggregateZero>(SVI->getOperand(2))) |
| 9199 | return std::vector<unsigned>(NElts, 0); |
| 9200 | if (isa<UndefValue>(SVI->getOperand(2))) |
| 9201 | return std::vector<unsigned>(NElts, 2*NElts); |
| 9202 | |
| 9203 | std::vector<unsigned> Result; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 9204 | const ConstantVector *CP = cast<ConstantVector>(SVI->getOperand(2)); |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 9205 | for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) |
| 9206 | if (isa<UndefValue>(CP->getOperand(i))) |
| 9207 | Result.push_back(NElts*2); // undef -> 8 |
| 9208 | else |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 9209 | Result.push_back(cast<ConstantInt>(CP->getOperand(i))->getZExtValue()); |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 9210 | return Result; |
| 9211 | } |
| 9212 | |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 9213 | /// FindScalarElement - Given a vector and an element number, see if the scalar |
| 9214 | /// value is already around as a register, for example if it were inserted then |
| 9215 | /// extracted from the vector. |
| 9216 | static Value *FindScalarElement(Value *V, unsigned EltNo) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 9217 | assert(isa<VectorType>(V->getType()) && "Not looking at a vector?"); |
| 9218 | const VectorType *PTy = cast<VectorType>(V->getType()); |
Chris Lattner | 389a6f5 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 9219 | unsigned Width = PTy->getNumElements(); |
| 9220 | if (EltNo >= Width) // Out of range access. |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 9221 | return UndefValue::get(PTy->getElementType()); |
| 9222 | |
| 9223 | if (isa<UndefValue>(V)) |
| 9224 | return UndefValue::get(PTy->getElementType()); |
| 9225 | else if (isa<ConstantAggregateZero>(V)) |
| 9226 | return Constant::getNullValue(PTy->getElementType()); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 9227 | else if (ConstantVector *CP = dyn_cast<ConstantVector>(V)) |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 9228 | return CP->getOperand(EltNo); |
| 9229 | else if (InsertElementInst *III = dyn_cast<InsertElementInst>(V)) { |
| 9230 | // 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] | 9231 | if (!isa<ConstantInt>(III->getOperand(2))) |
| 9232 | return 0; |
| 9233 | unsigned IIElt = cast<ConstantInt>(III->getOperand(2))->getZExtValue(); |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 9234 | |
| 9235 | // If this is an insert to the element we are looking for, return the |
| 9236 | // inserted value. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 9237 | if (EltNo == IIElt) |
| 9238 | return III->getOperand(1); |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 9239 | |
| 9240 | // Otherwise, the insertelement doesn't modify the value, recurse on its |
| 9241 | // vector input. |
| 9242 | return FindScalarElement(III->getOperand(0), EltNo); |
Chris Lattner | 389a6f5 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 9243 | } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(V)) { |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 9244 | unsigned InEl = getShuffleMask(SVI)[EltNo]; |
| 9245 | if (InEl < Width) |
| 9246 | return FindScalarElement(SVI->getOperand(0), InEl); |
| 9247 | else if (InEl < Width*2) |
| 9248 | return FindScalarElement(SVI->getOperand(1), InEl - Width); |
| 9249 | else |
| 9250 | return UndefValue::get(PTy->getElementType()); |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 9251 | } |
| 9252 | |
| 9253 | // Otherwise, we don't know. |
| 9254 | return 0; |
| 9255 | } |
| 9256 | |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 9257 | Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) { |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 9258 | |
Chris Lattner | 1f13c88 | 2006-03-31 18:25:14 +0000 | [diff] [blame] | 9259 | // If packed val is undef, replace extract with scalar undef. |
| 9260 | if (isa<UndefValue>(EI.getOperand(0))) |
| 9261 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); |
| 9262 | |
| 9263 | // If packed val is constant 0, replace extract with scalar 0. |
| 9264 | if (isa<ConstantAggregateZero>(EI.getOperand(0))) |
| 9265 | return ReplaceInstUsesWith(EI, Constant::getNullValue(EI.getType())); |
| 9266 | |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 9267 | if (ConstantVector *C = dyn_cast<ConstantVector>(EI.getOperand(0))) { |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 9268 | // If packed val is constant with uniform operands, replace EI |
| 9269 | // with that operand |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 9270 | Constant *op0 = C->getOperand(0); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 9271 | for (unsigned i = 1; i < C->getNumOperands(); ++i) |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 9272 | if (C->getOperand(i) != op0) { |
| 9273 | op0 = 0; |
| 9274 | break; |
| 9275 | } |
| 9276 | if (op0) |
| 9277 | return ReplaceInstUsesWith(EI, op0); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 9278 | } |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 9279 | |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 9280 | // If extracting a specified index from the vector, see if we can recursively |
| 9281 | // find a previously computed scalar that was inserted into the vector. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 9282 | if (ConstantInt *IdxC = dyn_cast<ConstantInt>(EI.getOperand(1))) { |
Chris Lattner | 8546409 | 2007-04-09 01:37:55 +0000 | [diff] [blame] | 9283 | unsigned IndexVal = IdxC->getZExtValue(); |
| 9284 | unsigned VectorWidth = |
| 9285 | cast<VectorType>(EI.getOperand(0)->getType())->getNumElements(); |
| 9286 | |
| 9287 | // If this is extracting an invalid index, turn this into undef, to avoid |
| 9288 | // crashing the code below. |
| 9289 | if (IndexVal >= VectorWidth) |
| 9290 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); |
| 9291 | |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 9292 | // This instruction only demands the single element from the input vector. |
| 9293 | // If the input vector has a single use, simplify it based on this use |
| 9294 | // property. |
Chris Lattner | 8546409 | 2007-04-09 01:37:55 +0000 | [diff] [blame] | 9295 | if (EI.getOperand(0)->hasOneUse() && VectorWidth != 1) { |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 9296 | uint64_t UndefElts; |
| 9297 | if (Value *V = SimplifyDemandedVectorElts(EI.getOperand(0), |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 9298 | 1 << IndexVal, |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 9299 | UndefElts)) { |
| 9300 | EI.setOperand(0, V); |
| 9301 | return &EI; |
| 9302 | } |
| 9303 | } |
| 9304 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 9305 | if (Value *Elt = FindScalarElement(EI.getOperand(0), IndexVal)) |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 9306 | return ReplaceInstUsesWith(EI, Elt); |
Chris Lattner | b7300fa | 2007-04-14 23:02:14 +0000 | [diff] [blame] | 9307 | |
| 9308 | // If the this extractelement is directly using a bitcast from a vector of |
| 9309 | // the same number of elements, see if we can find the source element from |
| 9310 | // it. In this case, we will end up needing to bitcast the scalars. |
| 9311 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(EI.getOperand(0))) { |
| 9312 | if (const VectorType *VT = |
| 9313 | dyn_cast<VectorType>(BCI->getOperand(0)->getType())) |
| 9314 | if (VT->getNumElements() == VectorWidth) |
| 9315 | if (Value *Elt = FindScalarElement(BCI->getOperand(0), IndexVal)) |
| 9316 | return new BitCastInst(Elt, EI.getType()); |
| 9317 | } |
Chris Lattner | 389a6f5 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 9318 | } |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 9319 | |
Chris Lattner | 73fa49d | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 9320 | if (Instruction *I = dyn_cast<Instruction>(EI.getOperand(0))) { |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 9321 | if (I->hasOneUse()) { |
| 9322 | // Push extractelement into predecessor operation if legal and |
| 9323 | // profitable to do so |
| 9324 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) { |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 9325 | bool isConstantElt = isa<ConstantInt>(EI.getOperand(1)); |
| 9326 | if (CheapToScalarize(BO, isConstantElt)) { |
| 9327 | ExtractElementInst *newEI0 = |
| 9328 | new ExtractElementInst(BO->getOperand(0), EI.getOperand(1), |
| 9329 | EI.getName()+".lhs"); |
| 9330 | ExtractElementInst *newEI1 = |
| 9331 | new ExtractElementInst(BO->getOperand(1), EI.getOperand(1), |
| 9332 | EI.getName()+".rhs"); |
| 9333 | InsertNewInstBefore(newEI0, EI); |
| 9334 | InsertNewInstBefore(newEI1, EI); |
| 9335 | return BinaryOperator::create(BO->getOpcode(), newEI0, newEI1); |
| 9336 | } |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 9337 | } else if (isa<LoadInst>(I)) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9338 | Value *Ptr = InsertCastBefore(Instruction::BitCast, I->getOperand(0), |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 9339 | PointerType::get(EI.getType()), EI); |
| 9340 | GetElementPtrInst *GEP = |
Reid Spencer | de33124 | 2006-11-29 01:11:01 +0000 | [diff] [blame] | 9341 | new GetElementPtrInst(Ptr, EI.getOperand(1), I->getName() + ".gep"); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 9342 | InsertNewInstBefore(GEP, EI); |
| 9343 | return new LoadInst(GEP); |
Chris Lattner | 73fa49d | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 9344 | } |
| 9345 | } |
| 9346 | if (InsertElementInst *IE = dyn_cast<InsertElementInst>(I)) { |
| 9347 | // Extracting the inserted element? |
| 9348 | if (IE->getOperand(2) == EI.getOperand(1)) |
| 9349 | return ReplaceInstUsesWith(EI, IE->getOperand(1)); |
| 9350 | // If the inserted and extracted elements are constants, they must not |
| 9351 | // be the same value, extract from the pre-inserted value instead. |
| 9352 | if (isa<Constant>(IE->getOperand(2)) && |
| 9353 | isa<Constant>(EI.getOperand(1))) { |
| 9354 | AddUsesToWorkList(EI); |
| 9355 | EI.setOperand(0, IE->getOperand(0)); |
| 9356 | return &EI; |
| 9357 | } |
| 9358 | } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(I)) { |
| 9359 | // If this is extracting an element from a shufflevector, figure out where |
| 9360 | // it came from and extract from the appropriate input element instead. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 9361 | if (ConstantInt *Elt = dyn_cast<ConstantInt>(EI.getOperand(1))) { |
| 9362 | unsigned SrcIdx = getShuffleMask(SVI)[Elt->getZExtValue()]; |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 9363 | Value *Src; |
| 9364 | if (SrcIdx < SVI->getType()->getNumElements()) |
| 9365 | Src = SVI->getOperand(0); |
| 9366 | else if (SrcIdx < SVI->getType()->getNumElements()*2) { |
| 9367 | SrcIdx -= SVI->getType()->getNumElements(); |
| 9368 | Src = SVI->getOperand(1); |
| 9369 | } else { |
| 9370 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); |
Chris Lattner | df084ff | 2006-03-30 22:02:40 +0000 | [diff] [blame] | 9371 | } |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 9372 | return new ExtractElementInst(Src, SrcIdx); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 9373 | } |
| 9374 | } |
Chris Lattner | 73fa49d | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 9375 | } |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 9376 | return 0; |
| 9377 | } |
| 9378 | |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 9379 | /// CollectSingleShuffleElements - If V is a shuffle of values that ONLY returns |
| 9380 | /// elements from either LHS or RHS, return the shuffle mask and true. |
| 9381 | /// Otherwise, return false. |
| 9382 | static bool CollectSingleShuffleElements(Value *V, Value *LHS, Value *RHS, |
| 9383 | std::vector<Constant*> &Mask) { |
| 9384 | assert(V->getType() == LHS->getType() && V->getType() == RHS->getType() && |
| 9385 | "Invalid CollectSingleShuffleElements"); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 9386 | unsigned NumElts = cast<VectorType>(V->getType())->getNumElements(); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 9387 | |
| 9388 | if (isa<UndefValue>(V)) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9389 | Mask.assign(NumElts, UndefValue::get(Type::Int32Ty)); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 9390 | return true; |
| 9391 | } else if (V == LHS) { |
| 9392 | for (unsigned i = 0; i != NumElts; ++i) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9393 | Mask.push_back(ConstantInt::get(Type::Int32Ty, i)); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 9394 | return true; |
| 9395 | } else if (V == RHS) { |
| 9396 | for (unsigned i = 0; i != NumElts; ++i) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9397 | Mask.push_back(ConstantInt::get(Type::Int32Ty, i+NumElts)); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 9398 | return true; |
| 9399 | } else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) { |
| 9400 | // If this is an insert of an extract from some other vector, include it. |
| 9401 | Value *VecOp = IEI->getOperand(0); |
| 9402 | Value *ScalarOp = IEI->getOperand(1); |
| 9403 | Value *IdxOp = IEI->getOperand(2); |
| 9404 | |
Chris Lattner | d929f06 | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 9405 | if (!isa<ConstantInt>(IdxOp)) |
| 9406 | return false; |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 9407 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Chris Lattner | d929f06 | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 9408 | |
| 9409 | if (isa<UndefValue>(ScalarOp)) { // inserting undef into vector. |
| 9410 | // Okay, we can handle this if the vector we are insertinting into is |
| 9411 | // transitively ok. |
| 9412 | if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) { |
| 9413 | // If so, update the mask to reflect the inserted undef. |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9414 | Mask[InsertedIdx] = UndefValue::get(Type::Int32Ty); |
Chris Lattner | d929f06 | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 9415 | return true; |
| 9416 | } |
| 9417 | } else if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)){ |
| 9418 | if (isa<ConstantInt>(EI->getOperand(1)) && |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 9419 | EI->getOperand(0)->getType() == V->getType()) { |
| 9420 | unsigned ExtractedIdx = |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 9421 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 9422 | |
| 9423 | // This must be extracting from either LHS or RHS. |
| 9424 | if (EI->getOperand(0) == LHS || EI->getOperand(0) == RHS) { |
| 9425 | // Okay, we can handle this if the vector we are insertinting into is |
| 9426 | // transitively ok. |
| 9427 | if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) { |
| 9428 | // If so, update the mask to reflect the inserted value. |
| 9429 | if (EI->getOperand(0) == LHS) { |
| 9430 | Mask[InsertedIdx & (NumElts-1)] = |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9431 | ConstantInt::get(Type::Int32Ty, ExtractedIdx); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 9432 | } else { |
| 9433 | assert(EI->getOperand(0) == RHS); |
| 9434 | Mask[InsertedIdx & (NumElts-1)] = |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9435 | ConstantInt::get(Type::Int32Ty, ExtractedIdx+NumElts); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 9436 | |
| 9437 | } |
| 9438 | return true; |
| 9439 | } |
| 9440 | } |
| 9441 | } |
| 9442 | } |
| 9443 | } |
| 9444 | // TODO: Handle shufflevector here! |
| 9445 | |
| 9446 | return false; |
| 9447 | } |
| 9448 | |
| 9449 | /// CollectShuffleElements - We are building a shuffle of V, using RHS as the |
| 9450 | /// RHS of the shuffle instruction, if it is not null. Return a shuffle mask |
| 9451 | /// that computes V and the LHS value of the shuffle. |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 9452 | static Value *CollectShuffleElements(Value *V, std::vector<Constant*> &Mask, |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 9453 | Value *&RHS) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 9454 | assert(isa<VectorType>(V->getType()) && |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 9455 | (RHS == 0 || V->getType() == RHS->getType()) && |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 9456 | "Invalid shuffle!"); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 9457 | unsigned NumElts = cast<VectorType>(V->getType())->getNumElements(); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 9458 | |
| 9459 | if (isa<UndefValue>(V)) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9460 | Mask.assign(NumElts, UndefValue::get(Type::Int32Ty)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 9461 | return V; |
| 9462 | } else if (isa<ConstantAggregateZero>(V)) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9463 | Mask.assign(NumElts, ConstantInt::get(Type::Int32Ty, 0)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 9464 | return V; |
| 9465 | } else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) { |
| 9466 | // If this is an insert of an extract from some other vector, include it. |
| 9467 | Value *VecOp = IEI->getOperand(0); |
| 9468 | Value *ScalarOp = IEI->getOperand(1); |
| 9469 | Value *IdxOp = IEI->getOperand(2); |
| 9470 | |
| 9471 | if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) { |
| 9472 | if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) && |
| 9473 | EI->getOperand(0)->getType() == V->getType()) { |
| 9474 | unsigned ExtractedIdx = |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 9475 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
| 9476 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 9477 | |
| 9478 | // Either the extracted from or inserted into vector must be RHSVec, |
| 9479 | // otherwise we'd end up with a shuffle of three inputs. |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 9480 | if (EI->getOperand(0) == RHS || RHS == 0) { |
| 9481 | RHS = EI->getOperand(0); |
| 9482 | Value *V = CollectShuffleElements(VecOp, Mask, RHS); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 9483 | Mask[InsertedIdx & (NumElts-1)] = |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9484 | ConstantInt::get(Type::Int32Ty, NumElts+ExtractedIdx); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 9485 | return V; |
| 9486 | } |
| 9487 | |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 9488 | if (VecOp == RHS) { |
| 9489 | Value *V = CollectShuffleElements(EI->getOperand(0), Mask, RHS); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 9490 | // Everything but the extracted element is replaced with the RHS. |
| 9491 | for (unsigned i = 0; i != NumElts; ++i) { |
| 9492 | if (i != InsertedIdx) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9493 | Mask[i] = ConstantInt::get(Type::Int32Ty, NumElts+i); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 9494 | } |
| 9495 | return V; |
| 9496 | } |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 9497 | |
| 9498 | // If this insertelement is a chain that comes from exactly these two |
| 9499 | // vectors, return the vector and the effective shuffle. |
| 9500 | if (CollectSingleShuffleElements(IEI, EI->getOperand(0), RHS, Mask)) |
| 9501 | return EI->getOperand(0); |
| 9502 | |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 9503 | } |
| 9504 | } |
| 9505 | } |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 9506 | // TODO: Handle shufflevector here! |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 9507 | |
| 9508 | // Otherwise, can't do anything fancy. Return an identity vector. |
| 9509 | for (unsigned i = 0; i != NumElts; ++i) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9510 | Mask.push_back(ConstantInt::get(Type::Int32Ty, i)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 9511 | return V; |
| 9512 | } |
| 9513 | |
| 9514 | Instruction *InstCombiner::visitInsertElementInst(InsertElementInst &IE) { |
| 9515 | Value *VecOp = IE.getOperand(0); |
| 9516 | Value *ScalarOp = IE.getOperand(1); |
| 9517 | Value *IdxOp = IE.getOperand(2); |
| 9518 | |
Chris Lattner | 599ded1 | 2007-04-09 01:11:16 +0000 | [diff] [blame] | 9519 | // Inserting an undef or into an undefined place, remove this. |
| 9520 | if (isa<UndefValue>(ScalarOp) || isa<UndefValue>(IdxOp)) |
| 9521 | ReplaceInstUsesWith(IE, VecOp); |
| 9522 | |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 9523 | // If the inserted element was extracted from some other vector, and if the |
| 9524 | // indexes are constant, try to turn this into a shufflevector operation. |
| 9525 | if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) { |
| 9526 | if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) && |
| 9527 | EI->getOperand(0)->getType() == IE.getType()) { |
| 9528 | unsigned NumVectorElts = IE.getType()->getNumElements(); |
Chris Lattner | e34e9a2 | 2007-04-14 23:32:02 +0000 | [diff] [blame] | 9529 | unsigned ExtractedIdx = |
| 9530 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 9531 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 9532 | |
| 9533 | if (ExtractedIdx >= NumVectorElts) // Out of range extract. |
| 9534 | return ReplaceInstUsesWith(IE, VecOp); |
| 9535 | |
| 9536 | if (InsertedIdx >= NumVectorElts) // Out of range insert. |
| 9537 | return ReplaceInstUsesWith(IE, UndefValue::get(IE.getType())); |
| 9538 | |
| 9539 | // If we are extracting a value from a vector, then inserting it right |
| 9540 | // back into the same place, just use the input vector. |
| 9541 | if (EI->getOperand(0) == VecOp && ExtractedIdx == InsertedIdx) |
| 9542 | return ReplaceInstUsesWith(IE, VecOp); |
| 9543 | |
| 9544 | // We could theoretically do this for ANY input. However, doing so could |
| 9545 | // turn chains of insertelement instructions into a chain of shufflevector |
| 9546 | // instructions, and right now we do not merge shufflevectors. As such, |
| 9547 | // only do this in a situation where it is clear that there is benefit. |
| 9548 | if (isa<UndefValue>(VecOp) || isa<ConstantAggregateZero>(VecOp)) { |
| 9549 | // Turn this into shuffle(EIOp0, VecOp, Mask). The result has all of |
| 9550 | // the values of VecOp, except then one read from EIOp0. |
| 9551 | // Build a new shuffle mask. |
| 9552 | std::vector<Constant*> Mask; |
| 9553 | if (isa<UndefValue>(VecOp)) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9554 | Mask.assign(NumVectorElts, UndefValue::get(Type::Int32Ty)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 9555 | else { |
| 9556 | assert(isa<ConstantAggregateZero>(VecOp) && "Unknown thing"); |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9557 | Mask.assign(NumVectorElts, ConstantInt::get(Type::Int32Ty, |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 9558 | NumVectorElts)); |
| 9559 | } |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9560 | Mask[InsertedIdx] = ConstantInt::get(Type::Int32Ty, ExtractedIdx); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 9561 | return new ShuffleVectorInst(EI->getOperand(0), VecOp, |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 9562 | ConstantVector::get(Mask)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 9563 | } |
| 9564 | |
| 9565 | // If this insertelement isn't used by some other insertelement, turn it |
| 9566 | // (and any insertelements it points to), into one big shuffle. |
| 9567 | if (!IE.hasOneUse() || !isa<InsertElementInst>(IE.use_back())) { |
| 9568 | std::vector<Constant*> Mask; |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 9569 | Value *RHS = 0; |
| 9570 | Value *LHS = CollectShuffleElements(&IE, Mask, RHS); |
| 9571 | if (RHS == 0) RHS = UndefValue::get(LHS->getType()); |
| 9572 | // We now have a shuffle of LHS, RHS, Mask. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 9573 | return new ShuffleVectorInst(LHS, RHS, ConstantVector::get(Mask)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 9574 | } |
| 9575 | } |
| 9576 | } |
| 9577 | |
| 9578 | return 0; |
| 9579 | } |
| 9580 | |
| 9581 | |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 9582 | Instruction *InstCombiner::visitShuffleVectorInst(ShuffleVectorInst &SVI) { |
| 9583 | Value *LHS = SVI.getOperand(0); |
| 9584 | Value *RHS = SVI.getOperand(1); |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 9585 | std::vector<unsigned> Mask = getShuffleMask(&SVI); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 9586 | |
| 9587 | bool MadeChange = false; |
| 9588 | |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 9589 | // Undefined shuffle mask -> undefined value. |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 9590 | if (isa<UndefValue>(SVI.getOperand(2))) |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 9591 | return ReplaceInstUsesWith(SVI, UndefValue::get(SVI.getType())); |
| 9592 | |
Chris Lattner | e4929dd | 2007-01-05 07:36:08 +0000 | [diff] [blame] | 9593 | // 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] | 9594 | // the undef, change them to undefs. |
Chris Lattner | e4929dd | 2007-01-05 07:36:08 +0000 | [diff] [blame] | 9595 | if (isa<UndefValue>(SVI.getOperand(1))) { |
| 9596 | // Scan to see if there are any references to the RHS. If so, replace them |
| 9597 | // with undef element refs and set MadeChange to true. |
| 9598 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
| 9599 | if (Mask[i] >= e && Mask[i] != 2*e) { |
| 9600 | Mask[i] = 2*e; |
| 9601 | MadeChange = true; |
| 9602 | } |
| 9603 | } |
| 9604 | |
| 9605 | if (MadeChange) { |
| 9606 | // Remap any references to RHS to use LHS. |
| 9607 | std::vector<Constant*> Elts; |
| 9608 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
| 9609 | if (Mask[i] == 2*e) |
| 9610 | Elts.push_back(UndefValue::get(Type::Int32Ty)); |
| 9611 | else |
| 9612 | Elts.push_back(ConstantInt::get(Type::Int32Ty, Mask[i])); |
| 9613 | } |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 9614 | SVI.setOperand(2, ConstantVector::get(Elts)); |
Chris Lattner | e4929dd | 2007-01-05 07:36:08 +0000 | [diff] [blame] | 9615 | } |
| 9616 | } |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 9617 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 9618 | // Canonicalize shuffle(x ,x,mask) -> shuffle(x, undef,mask') |
| 9619 | // Canonicalize shuffle(undef,x,mask) -> shuffle(x, undef,mask'). |
| 9620 | if (LHS == RHS || isa<UndefValue>(LHS)) { |
| 9621 | if (isa<UndefValue>(LHS) && LHS == RHS) { |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 9622 | // shuffle(undef,undef,mask) -> undef. |
| 9623 | return ReplaceInstUsesWith(SVI, LHS); |
| 9624 | } |
| 9625 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 9626 | // Remap any references to RHS to use LHS. |
| 9627 | std::vector<Constant*> Elts; |
| 9628 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 9629 | if (Mask[i] >= 2*e) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9630 | Elts.push_back(UndefValue::get(Type::Int32Ty)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 9631 | else { |
| 9632 | if ((Mask[i] >= e && isa<UndefValue>(RHS)) || |
| 9633 | (Mask[i] < e && isa<UndefValue>(LHS))) |
| 9634 | Mask[i] = 2*e; // Turn into undef. |
| 9635 | else |
| 9636 | Mask[i] &= (e-1); // Force to LHS. |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9637 | Elts.push_back(ConstantInt::get(Type::Int32Ty, Mask[i])); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 9638 | } |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 9639 | } |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 9640 | SVI.setOperand(0, SVI.getOperand(1)); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 9641 | SVI.setOperand(1, UndefValue::get(RHS->getType())); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 9642 | SVI.setOperand(2, ConstantVector::get(Elts)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 9643 | LHS = SVI.getOperand(0); |
| 9644 | RHS = SVI.getOperand(1); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 9645 | MadeChange = true; |
| 9646 | } |
| 9647 | |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 9648 | // Analyze the shuffle, are the LHS or RHS and identity shuffles? |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 9649 | bool isLHSID = true, isRHSID = true; |
Chris Lattner | 706126d | 2006-04-16 00:03:56 +0000 | [diff] [blame] | 9650 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 9651 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
| 9652 | if (Mask[i] >= e*2) continue; // Ignore undef values. |
| 9653 | // Is this an identity shuffle of the LHS value? |
| 9654 | isLHSID &= (Mask[i] == i); |
| 9655 | |
| 9656 | // Is this an identity shuffle of the RHS value? |
| 9657 | isRHSID &= (Mask[i]-e == i); |
Chris Lattner | 706126d | 2006-04-16 00:03:56 +0000 | [diff] [blame] | 9658 | } |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 9659 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 9660 | // Eliminate identity shuffles. |
| 9661 | if (isLHSID) return ReplaceInstUsesWith(SVI, LHS); |
| 9662 | if (isRHSID) return ReplaceInstUsesWith(SVI, RHS); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 9663 | |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 9664 | // If the LHS is a shufflevector itself, see if we can combine it with this |
| 9665 | // one without producing an unusual shuffle. Here we are really conservative: |
| 9666 | // we are absolutely afraid of producing a shuffle mask not in the input |
| 9667 | // program, because the code gen may not be smart enough to turn a merged |
| 9668 | // shuffle into two specific shuffles: it may produce worse code. As such, |
| 9669 | // we only merge two shuffles if the result is one of the two input shuffle |
| 9670 | // masks. In this case, merging the shuffles just removes one instruction, |
| 9671 | // which we know is safe. This is good for things like turning: |
| 9672 | // (splat(splat)) -> splat. |
| 9673 | if (ShuffleVectorInst *LHSSVI = dyn_cast<ShuffleVectorInst>(LHS)) { |
| 9674 | if (isa<UndefValue>(RHS)) { |
| 9675 | std::vector<unsigned> LHSMask = getShuffleMask(LHSSVI); |
| 9676 | |
| 9677 | std::vector<unsigned> NewMask; |
| 9678 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) |
| 9679 | if (Mask[i] >= 2*e) |
| 9680 | NewMask.push_back(2*e); |
| 9681 | else |
| 9682 | NewMask.push_back(LHSMask[Mask[i]]); |
| 9683 | |
| 9684 | // If the result mask is equal to the src shuffle or this shuffle mask, do |
| 9685 | // the replacement. |
| 9686 | if (NewMask == LHSMask || NewMask == Mask) { |
| 9687 | std::vector<Constant*> Elts; |
| 9688 | for (unsigned i = 0, e = NewMask.size(); i != e; ++i) { |
| 9689 | if (NewMask[i] >= e*2) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9690 | Elts.push_back(UndefValue::get(Type::Int32Ty)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 9691 | } else { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9692 | Elts.push_back(ConstantInt::get(Type::Int32Ty, NewMask[i])); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 9693 | } |
| 9694 | } |
| 9695 | return new ShuffleVectorInst(LHSSVI->getOperand(0), |
| 9696 | LHSSVI->getOperand(1), |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 9697 | ConstantVector::get(Elts)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 9698 | } |
| 9699 | } |
| 9700 | } |
Chris Lattner | c5eff44 | 2007-01-30 22:32:46 +0000 | [diff] [blame] | 9701 | |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 9702 | return MadeChange ? &SVI : 0; |
| 9703 | } |
| 9704 | |
| 9705 | |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 9706 | |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 9707 | |
| 9708 | /// TryToSinkInstruction - Try to move the specified instruction from its |
| 9709 | /// current block into the beginning of DestBlock, which can only happen if it's |
| 9710 | /// safe to move the instruction past all of the instructions between it and the |
| 9711 | /// end of its block. |
| 9712 | static bool TryToSinkInstruction(Instruction *I, BasicBlock *DestBlock) { |
| 9713 | assert(I->hasOneUse() && "Invariants didn't hold!"); |
| 9714 | |
Chris Lattner | 108e902 | 2005-10-27 17:13:11 +0000 | [diff] [blame] | 9715 | // Cannot move control-flow-involving, volatile loads, vaarg, etc. |
| 9716 | if (isa<PHINode>(I) || I->mayWriteToMemory()) return false; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9717 | |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 9718 | // Do not sink alloca instructions out of the entry block. |
Dan Gohman | ecb7a77 | 2007-03-22 16:38:57 +0000 | [diff] [blame] | 9719 | if (isa<AllocaInst>(I) && I->getParent() == |
| 9720 | &DestBlock->getParent()->getEntryBlock()) |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 9721 | return false; |
| 9722 | |
Chris Lattner | 96a52a6 | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 9723 | // We can only sink load instructions if there is nothing between the load and |
| 9724 | // the end of block that could change the value. |
| 9725 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Chris Lattner | 96a52a6 | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 9726 | for (BasicBlock::iterator Scan = LI, E = LI->getParent()->end(); |
| 9727 | Scan != E; ++Scan) |
| 9728 | if (Scan->mayWriteToMemory()) |
| 9729 | return false; |
Chris Lattner | 96a52a6 | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 9730 | } |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 9731 | |
| 9732 | BasicBlock::iterator InsertPos = DestBlock->begin(); |
| 9733 | while (isa<PHINode>(InsertPos)) ++InsertPos; |
| 9734 | |
Chris Lattner | 4bc5f80 | 2005-08-08 19:11:57 +0000 | [diff] [blame] | 9735 | I->moveBefore(InsertPos); |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 9736 | ++NumSunkInst; |
| 9737 | return true; |
| 9738 | } |
| 9739 | |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9740 | |
| 9741 | /// AddReachableCodeToWorklist - Walk the function in depth-first order, adding |
| 9742 | /// all reachable code to the worklist. |
| 9743 | /// |
| 9744 | /// This has a couple of tricks to make the code faster and more powerful. In |
| 9745 | /// particular, we constant fold and DCE instructions as we go, to avoid adding |
| 9746 | /// them to the worklist (this significantly speeds up instcombine on code where |
| 9747 | /// many instructions are dead or constant). Additionally, if we find a branch |
| 9748 | /// whose condition is a known constant, we only visit the reachable successors. |
| 9749 | /// |
| 9750 | static void AddReachableCodeToWorklist(BasicBlock *BB, |
Chris Lattner | 1f87a58 | 2007-02-15 19:41:52 +0000 | [diff] [blame] | 9751 | SmallPtrSet<BasicBlock*, 64> &Visited, |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9752 | InstCombiner &IC, |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 9753 | const TargetData *TD) { |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 9754 | std::vector<BasicBlock*> Worklist; |
| 9755 | Worklist.push_back(BB); |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9756 | |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 9757 | while (!Worklist.empty()) { |
| 9758 | BB = Worklist.back(); |
| 9759 | Worklist.pop_back(); |
| 9760 | |
| 9761 | // We have now visited this block! If we've already been here, ignore it. |
| 9762 | if (!Visited.insert(BB)) continue; |
| 9763 | |
| 9764 | for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ) { |
| 9765 | Instruction *Inst = BBI++; |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9766 | |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 9767 | // DCE instruction if trivially dead. |
| 9768 | if (isInstructionTriviallyDead(Inst)) { |
| 9769 | ++NumDeadInst; |
| 9770 | DOUT << "IC: DCE: " << *Inst; |
| 9771 | Inst->eraseFromParent(); |
| 9772 | continue; |
| 9773 | } |
| 9774 | |
| 9775 | // ConstantProp instruction if trivially constant. |
| 9776 | if (Constant *C = ConstantFoldInstruction(Inst, TD)) { |
| 9777 | DOUT << "IC: ConstFold to: " << *C << " from: " << *Inst; |
| 9778 | Inst->replaceAllUsesWith(C); |
| 9779 | ++NumConstProp; |
| 9780 | Inst->eraseFromParent(); |
| 9781 | continue; |
| 9782 | } |
| 9783 | |
| 9784 | IC.AddToWorkList(Inst); |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9785 | } |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 9786 | |
| 9787 | // Recursively visit successors. If this is a branch or switch on a |
| 9788 | // constant, only visit the reachable successor. |
| 9789 | TerminatorInst *TI = BB->getTerminator(); |
| 9790 | if (BranchInst *BI = dyn_cast<BranchInst>(TI)) { |
| 9791 | if (BI->isConditional() && isa<ConstantInt>(BI->getCondition())) { |
| 9792 | bool CondVal = cast<ConstantInt>(BI->getCondition())->getZExtValue(); |
| 9793 | Worklist.push_back(BI->getSuccessor(!CondVal)); |
| 9794 | continue; |
| 9795 | } |
| 9796 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) { |
| 9797 | if (ConstantInt *Cond = dyn_cast<ConstantInt>(SI->getCondition())) { |
| 9798 | // See if this is an explicit destination. |
| 9799 | for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i) |
| 9800 | if (SI->getCaseValue(i) == Cond) { |
| 9801 | Worklist.push_back(SI->getSuccessor(i)); |
| 9802 | continue; |
| 9803 | } |
| 9804 | |
| 9805 | // Otherwise it is the default destination. |
| 9806 | Worklist.push_back(SI->getSuccessor(0)); |
| 9807 | continue; |
| 9808 | } |
| 9809 | } |
| 9810 | |
| 9811 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) |
| 9812 | Worklist.push_back(TI->getSuccessor(i)); |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9813 | } |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9814 | } |
| 9815 | |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 9816 | bool InstCombiner::DoOneIteration(Function &F, unsigned Iteration) { |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 9817 | bool Changed = false; |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 9818 | TD = &getAnalysis<TargetData>(); |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 9819 | |
| 9820 | DEBUG(DOUT << "\n\nINSTCOMBINE ITERATION #" << Iteration << " on " |
| 9821 | << F.getNameStr() << "\n"); |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9822 | |
Chris Lattner | b3d5970 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 9823 | { |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9824 | // Do a depth-first traversal of the function, populate the worklist with |
| 9825 | // the reachable instructions. Ignore blocks that are not reachable. Keep |
| 9826 | // track of which blocks we visit. |
Chris Lattner | 1f87a58 | 2007-02-15 19:41:52 +0000 | [diff] [blame] | 9827 | SmallPtrSet<BasicBlock*, 64> Visited; |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9828 | AddReachableCodeToWorklist(F.begin(), Visited, *this, TD); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 9829 | |
Chris Lattner | b3d5970 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 9830 | // Do a quick scan over the function. If we find any blocks that are |
| 9831 | // unreachable, remove any instructions inside of them. This prevents |
| 9832 | // the instcombine code from having to deal with some bad special cases. |
| 9833 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 9834 | if (!Visited.count(BB)) { |
| 9835 | Instruction *Term = BB->getTerminator(); |
| 9836 | while (Term != BB->begin()) { // Remove instrs bottom-up |
| 9837 | BasicBlock::iterator I = Term; --I; |
Chris Lattner | 6ffe551 | 2004-04-27 15:13:33 +0000 | [diff] [blame] | 9838 | |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 9839 | DOUT << "IC: DCE: " << *I; |
Chris Lattner | b3d5970 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 9840 | ++NumDeadInst; |
| 9841 | |
| 9842 | if (!I->use_empty()) |
| 9843 | I->replaceAllUsesWith(UndefValue::get(I->getType())); |
| 9844 | I->eraseFromParent(); |
| 9845 | } |
| 9846 | } |
| 9847 | } |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9848 | |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9849 | while (!Worklist.empty()) { |
| 9850 | Instruction *I = RemoveOneFromWorkList(); |
| 9851 | if (I == 0) continue; // skip null values. |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9852 | |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 9853 | // Check to see if we can DCE the instruction. |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 9854 | if (isInstructionTriviallyDead(I)) { |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 9855 | // Add operands to the worklist. |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9856 | if (I->getNumOperands() < 4) |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 9857 | AddUsesToWorkList(*I); |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 9858 | ++NumDeadInst; |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9859 | |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 9860 | DOUT << "IC: DCE: " << *I; |
Chris Lattner | ad5fec1 | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 9861 | |
| 9862 | I->eraseFromParent(); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9863 | RemoveFromWorkList(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9864 | continue; |
| 9865 | } |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 9866 | |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 9867 | // Instruction isn't dead, see if we can constant propagate it. |
Chris Lattner | 0a19ffa | 2007-01-30 23:16:15 +0000 | [diff] [blame] | 9868 | if (Constant *C = ConstantFoldInstruction(I, TD)) { |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 9869 | DOUT << "IC: ConstFold to: " << *C << " from: " << *I; |
Chris Lattner | ad5fec1 | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 9870 | |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 9871 | // Add operands to the worklist. |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 9872 | AddUsesToWorkList(*I); |
Chris Lattner | c736d56 | 2002-12-05 22:41:53 +0000 | [diff] [blame] | 9873 | ReplaceInstUsesWith(*I, C); |
| 9874 | |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 9875 | ++NumConstProp; |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9876 | I->eraseFromParent(); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9877 | RemoveFromWorkList(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9878 | continue; |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 9879 | } |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9880 | |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 9881 | // See if we can trivially sink this instruction to a successor basic block. |
| 9882 | if (I->hasOneUse()) { |
| 9883 | BasicBlock *BB = I->getParent(); |
| 9884 | BasicBlock *UserParent = cast<Instruction>(I->use_back())->getParent(); |
| 9885 | if (UserParent != BB) { |
| 9886 | bool UserIsSuccessor = false; |
| 9887 | // See if the user is one of our successors. |
| 9888 | for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI) |
| 9889 | if (*SI == UserParent) { |
| 9890 | UserIsSuccessor = true; |
| 9891 | break; |
| 9892 | } |
| 9893 | |
| 9894 | // If the user is one of our immediate successors, and if that successor |
| 9895 | // only has us as a predecessors (we'd have to split the critical edge |
| 9896 | // otherwise), we can keep going. |
| 9897 | if (UserIsSuccessor && !isa<PHINode>(I->use_back()) && |
| 9898 | next(pred_begin(UserParent)) == pred_end(UserParent)) |
| 9899 | // Okay, the CFG is simple enough, try to sink this instruction. |
| 9900 | Changed |= TryToSinkInstruction(I, UserParent); |
| 9901 | } |
| 9902 | } |
| 9903 | |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9904 | // Now that we have an instruction, try combining it to simplify it... |
Reid Spencer | a9b8101 | 2007-03-26 17:44:01 +0000 | [diff] [blame] | 9905 | #ifndef NDEBUG |
| 9906 | std::string OrigI; |
| 9907 | #endif |
| 9908 | DEBUG(std::ostringstream SS; I->print(SS); OrigI = SS.str();); |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9909 | if (Instruction *Result = visit(*I)) { |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 9910 | ++NumCombined; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 9911 | // Should we replace the old instruction with a new one? |
Chris Lattner | b3bc8fa | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 9912 | if (Result != I) { |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 9913 | DOUT << "IC: Old = " << *I |
| 9914 | << " New = " << *Result; |
Chris Lattner | 0cea42a | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 9915 | |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 9916 | // Everything uses the new instruction now. |
| 9917 | I->replaceAllUsesWith(Result); |
| 9918 | |
| 9919 | // Push the new instruction and any users onto the worklist. |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9920 | AddToWorkList(Result); |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 9921 | AddUsersToWorkList(*Result); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9922 | |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 9923 | // Move the name to the new instruction first. |
| 9924 | Result->takeName(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9925 | |
| 9926 | // Insert the new instruction into the basic block... |
| 9927 | BasicBlock *InstParent = I->getParent(); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9928 | BasicBlock::iterator InsertPos = I; |
| 9929 | |
| 9930 | if (!isa<PHINode>(Result)) // If combining a PHI, don't insert |
| 9931 | while (isa<PHINode>(InsertPos)) // middle of a block of PHIs. |
| 9932 | ++InsertPos; |
| 9933 | |
| 9934 | InstParent->getInstList().insert(InsertPos, Result); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9935 | |
Chris Lattner | 00d5131 | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 9936 | // Make sure that we reprocess all operands now that we reduced their |
| 9937 | // use counts. |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9938 | AddUsesToWorkList(*I); |
Chris Lattner | 216d4d8 | 2004-05-01 23:19:52 +0000 | [diff] [blame] | 9939 | |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 9940 | // Instructions can end up on the worklist more than once. Make sure |
| 9941 | // we do not process an instruction that has been deleted. |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9942 | RemoveFromWorkList(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9943 | |
| 9944 | // Erase the old instruction. |
| 9945 | InstParent->getInstList().erase(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 9946 | } else { |
Evan Cheng | c7baf68 | 2007-03-27 16:44:48 +0000 | [diff] [blame] | 9947 | #ifndef NDEBUG |
Reid Spencer | a9b8101 | 2007-03-26 17:44:01 +0000 | [diff] [blame] | 9948 | DOUT << "IC: Mod = " << OrigI |
| 9949 | << " New = " << *I; |
Evan Cheng | c7baf68 | 2007-03-27 16:44:48 +0000 | [diff] [blame] | 9950 | #endif |
Chris Lattner | 0cea42a | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 9951 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9952 | // If the instruction was modified, it's possible that it is now dead. |
| 9953 | // if so, remove it. |
Chris Lattner | 00d5131 | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 9954 | if (isInstructionTriviallyDead(I)) { |
| 9955 | // Make sure we process all operands now that we are reducing their |
| 9956 | // use counts. |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 9957 | AddUsesToWorkList(*I); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9958 | |
Chris Lattner | 00d5131 | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 9959 | // Instructions may end up in the worklist more than once. Erase all |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 9960 | // occurrences of this instruction. |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9961 | RemoveFromWorkList(I); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 9962 | I->eraseFromParent(); |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 9963 | } else { |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 9964 | AddToWorkList(I); |
| 9965 | AddUsersToWorkList(*I); |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9966 | } |
Chris Lattner | b3bc8fa | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 9967 | } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 9968 | Changed = true; |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9969 | } |
| 9970 | } |
| 9971 | |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 9972 | assert(WorklistMap.empty() && "Worklist empty, but map not?"); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 9973 | return Changed; |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 9974 | } |
| 9975 | |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 9976 | |
| 9977 | bool InstCombiner::runOnFunction(Function &F) { |
Chris Lattner | f964f32 | 2007-03-04 04:27:24 +0000 | [diff] [blame] | 9978 | MustPreserveLCSSA = mustPreserveAnalysisID(LCSSAID); |
| 9979 | |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 9980 | bool EverMadeChange = false; |
| 9981 | |
| 9982 | // Iterate while there is work to do. |
| 9983 | unsigned Iteration = 0; |
| 9984 | while (DoOneIteration(F, Iteration++)) |
| 9985 | EverMadeChange = true; |
| 9986 | return EverMadeChange; |
| 9987 | } |
| 9988 | |
Brian Gaeke | 96d4bf7 | 2004-07-27 17:43:21 +0000 | [diff] [blame] | 9989 | FunctionPass *llvm::createInstructionCombiningPass() { |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 9990 | return new InstCombiner(); |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 9991 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 9992 | |