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 | 32ed46b | 2004-05-04 15:19:33 +0000 | [diff] [blame] | 15 | // %Y = add int %X, 1 |
| 16 | // %Z = add int %Y, 1 |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 17 | // into: |
Chris Lattner | 32ed46b | 2004-05-04 15:19:33 +0000 | [diff] [blame] | 18 | // %Z = add int %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 | 6734b57 | 2007-02-04 00:40:42 +0000 | [diff] [blame] | 59 | #include <set> |
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: |
| 79 | /// AddToWorkList - Add the specified instruction to the worklist if it |
| 80 | /// isn't already in it. |
| 81 | void AddToWorkList(Instruction *I) { |
| 82 | if (WorklistMap.insert(std::make_pair(I, Worklist.size()))) |
| 83 | Worklist.push_back(I); |
| 84 | } |
| 85 | |
| 86 | // RemoveFromWorkList - remove I from the worklist if it exists. |
| 87 | void RemoveFromWorkList(Instruction *I) { |
| 88 | DenseMap<Instruction*, unsigned>::iterator It = WorklistMap.find(I); |
| 89 | if (It == WorklistMap.end()) return; // Not in worklist. |
| 90 | |
| 91 | // Don't bother moving everything down, just null out the slot. |
| 92 | Worklist[It->second] = 0; |
| 93 | |
| 94 | WorklistMap.erase(It); |
| 95 | } |
| 96 | |
| 97 | Instruction *RemoveOneFromWorkList() { |
| 98 | Instruction *I = Worklist.back(); |
| 99 | Worklist.pop_back(); |
| 100 | WorklistMap.erase(I); |
| 101 | return I; |
| 102 | } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 103 | |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 104 | |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 105 | /// AddUsersToWorkList - When an instruction is simplified, add all users of |
| 106 | /// the instruction to the work lists because they might get more simplified |
| 107 | /// now. |
| 108 | /// |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 109 | void AddUsersToWorkList(Value &I) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 110 | for (Value::use_iterator UI = I.use_begin(), UE = I.use_end(); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 111 | UI != UE; ++UI) |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 112 | AddToWorkList(cast<Instruction>(*UI)); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 115 | /// AddUsesToWorkList - When an instruction is simplified, add operands to |
| 116 | /// the work lists because they might get more simplified now. |
| 117 | /// |
| 118 | void AddUsesToWorkList(Instruction &I) { |
| 119 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) |
| 120 | if (Instruction *Op = dyn_cast<Instruction>(I.getOperand(i))) |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 121 | AddToWorkList(Op); |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 122 | } |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 123 | |
| 124 | /// AddSoonDeadInstToWorklist - The specified instruction is about to become |
| 125 | /// dead. Add all of its operands to the worklist, turning them into |
| 126 | /// undef's to reduce the number of uses of those instructions. |
| 127 | /// |
| 128 | /// Return the specified operand before it is turned into an undef. |
| 129 | /// |
| 130 | Value *AddSoonDeadInstToWorklist(Instruction &I, unsigned op) { |
| 131 | Value *R = I.getOperand(op); |
| 132 | |
| 133 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) |
| 134 | if (Instruction *Op = dyn_cast<Instruction>(I.getOperand(i))) { |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 135 | AddToWorkList(Op); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 136 | // Set the operand to undef to drop the use. |
| 137 | I.setOperand(i, UndefValue::get(Op->getType())); |
| 138 | } |
| 139 | |
| 140 | return R; |
| 141 | } |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 142 | |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 143 | public: |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 144 | virtual bool runOnFunction(Function &F); |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 145 | |
| 146 | bool DoOneIteration(Function &F, unsigned ItNum); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 147 | |
Chris Lattner | 97e52e4 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 148 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 149 | AU.addRequired<TargetData>(); |
Owen Anderson | d1b78a1 | 2006-07-10 19:03:49 +0000 | [diff] [blame] | 150 | AU.addPreservedID(LCSSAID); |
Chris Lattner | cb2610e | 2002-10-21 20:00:28 +0000 | [diff] [blame] | 151 | AU.setPreservesCFG(); |
Chris Lattner | 97e52e4 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 154 | TargetData &getTargetData() const { return *TD; } |
| 155 | |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 156 | // Visitation implementation - Implement instruction combining for different |
| 157 | // instruction types. The semantics are as follows: |
| 158 | // Return Value: |
| 159 | // null - No change was made |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 160 | // 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] | 161 | // otherwise - Change was made, replace I with returned instruction |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 162 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 163 | Instruction *visitAdd(BinaryOperator &I); |
| 164 | Instruction *visitSub(BinaryOperator &I); |
| 165 | Instruction *visitMul(BinaryOperator &I); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 166 | Instruction *visitURem(BinaryOperator &I); |
| 167 | Instruction *visitSRem(BinaryOperator &I); |
| 168 | Instruction *visitFRem(BinaryOperator &I); |
| 169 | Instruction *commonRemTransforms(BinaryOperator &I); |
| 170 | Instruction *commonIRemTransforms(BinaryOperator &I); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 171 | Instruction *commonDivTransforms(BinaryOperator &I); |
| 172 | Instruction *commonIDivTransforms(BinaryOperator &I); |
| 173 | Instruction *visitUDiv(BinaryOperator &I); |
| 174 | Instruction *visitSDiv(BinaryOperator &I); |
| 175 | Instruction *visitFDiv(BinaryOperator &I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 176 | Instruction *visitAnd(BinaryOperator &I); |
| 177 | Instruction *visitOr (BinaryOperator &I); |
| 178 | Instruction *visitXor(BinaryOperator &I); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 179 | Instruction *visitShl(BinaryOperator &I); |
| 180 | Instruction *visitAShr(BinaryOperator &I); |
| 181 | Instruction *visitLShr(BinaryOperator &I); |
| 182 | Instruction *commonShiftTransforms(BinaryOperator &I); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 183 | Instruction *visitFCmpInst(FCmpInst &I); |
| 184 | Instruction *visitICmpInst(ICmpInst &I); |
| 185 | Instruction *visitICmpInstWithCastAndCast(ICmpInst &ICI); |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 186 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 187 | Instruction *FoldGEPICmp(User *GEPLHS, Value *RHS, |
| 188 | ICmpInst::Predicate Cond, Instruction &I); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 189 | Instruction *FoldShiftByConstant(Value *Op0, ConstantInt *Op1, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 190 | BinaryOperator &I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 191 | Instruction *commonCastTransforms(CastInst &CI); |
| 192 | Instruction *commonIntCastTransforms(CastInst &CI); |
| 193 | Instruction *visitTrunc(CastInst &CI); |
| 194 | Instruction *visitZExt(CastInst &CI); |
| 195 | Instruction *visitSExt(CastInst &CI); |
| 196 | Instruction *visitFPTrunc(CastInst &CI); |
| 197 | Instruction *visitFPExt(CastInst &CI); |
| 198 | Instruction *visitFPToUI(CastInst &CI); |
| 199 | Instruction *visitFPToSI(CastInst &CI); |
| 200 | Instruction *visitUIToFP(CastInst &CI); |
| 201 | Instruction *visitSIToFP(CastInst &CI); |
| 202 | Instruction *visitPtrToInt(CastInst &CI); |
| 203 | Instruction *visitIntToPtr(CastInst &CI); |
| 204 | Instruction *visitBitCast(CastInst &CI); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 205 | Instruction *FoldSelectOpOp(SelectInst &SI, Instruction *TI, |
| 206 | Instruction *FI); |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 207 | Instruction *visitSelectInst(SelectInst &CI); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 208 | Instruction *visitCallInst(CallInst &CI); |
| 209 | Instruction *visitInvokeInst(InvokeInst &II); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 210 | Instruction *visitPHINode(PHINode &PN); |
| 211 | Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP); |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 212 | Instruction *visitAllocationInst(AllocationInst &AI); |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 213 | Instruction *visitFreeInst(FreeInst &FI); |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 214 | Instruction *visitLoadInst(LoadInst &LI); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 215 | Instruction *visitStoreInst(StoreInst &SI); |
Chris Lattner | c4d10eb | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 216 | Instruction *visitBranchInst(BranchInst &BI); |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 217 | Instruction *visitSwitchInst(SwitchInst &SI); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 218 | Instruction *visitInsertElementInst(InsertElementInst &IE); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 219 | Instruction *visitExtractElementInst(ExtractElementInst &EI); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 220 | Instruction *visitShuffleVectorInst(ShuffleVectorInst &SVI); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 221 | |
| 222 | // visitInstruction - Specify what to return for unhandled instructions... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 223 | Instruction *visitInstruction(Instruction &I) { return 0; } |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 224 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 225 | private: |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 226 | Instruction *visitCallSite(CallSite CS); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 227 | bool transformConstExprCastCall(CallSite CS); |
| 228 | |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 229 | public: |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 230 | // InsertNewInstBefore - insert an instruction New before instruction Old |
| 231 | // in the program. Add the new instruction to the worklist. |
| 232 | // |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 233 | Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) { |
Chris Lattner | e6f9a91 | 2002-08-23 18:32:43 +0000 | [diff] [blame] | 234 | assert(New && New->getParent() == 0 && |
| 235 | "New instruction already inserted into a basic block!"); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 236 | BasicBlock *BB = Old.getParent(); |
| 237 | BB->getInstList().insert(&Old, New); // Insert inst |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 238 | AddToWorkList(New); |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 239 | return New; |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 242 | /// InsertCastBefore - Insert a cast of V to TY before the instruction POS. |
| 243 | /// This also adds the cast to the worklist. Finally, this returns the |
| 244 | /// cast. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 245 | Value *InsertCastBefore(Instruction::CastOps opc, Value *V, const Type *Ty, |
| 246 | Instruction &Pos) { |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 247 | if (V->getType() == Ty) return V; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 248 | |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 249 | if (Constant *CV = dyn_cast<Constant>(V)) |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 250 | return ConstantExpr::getCast(opc, CV, Ty); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 251 | |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 252 | Instruction *C = CastInst::create(opc, V, Ty, V->getName(), &Pos); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 253 | AddToWorkList(C); |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 254 | return C; |
| 255 | } |
| 256 | |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 257 | // ReplaceInstUsesWith - This method is to be used when an instruction is |
| 258 | // found to be dead, replacable with another preexisting expression. Here |
| 259 | // we add all uses of I to the worklist, replace all uses of I with the new |
| 260 | // value, then return I, so that the inst combiner will know that I was |
| 261 | // modified. |
| 262 | // |
| 263 | Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) { |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 264 | AddUsersToWorkList(I); // Add all modified instrs to worklist |
Chris Lattner | 15a76c0 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 265 | if (&I != V) { |
| 266 | I.replaceAllUsesWith(V); |
| 267 | return &I; |
| 268 | } else { |
| 269 | // If we are replacing the instruction with itself, this must be in a |
| 270 | // segment of unreachable code, so just clobber the instruction. |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 271 | I.replaceAllUsesWith(UndefValue::get(I.getType())); |
Chris Lattner | 15a76c0 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 272 | return &I; |
| 273 | } |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 274 | } |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 275 | |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 276 | // UpdateValueUsesWith - This method is to be used when an value is |
| 277 | // found to be replacable with another preexisting expression or was |
| 278 | // updated. Here we add all uses of I to the worklist, replace all uses of |
| 279 | // I with the new value (unless the instruction was just updated), then |
| 280 | // return true, so that the inst combiner will know that I was modified. |
| 281 | // |
| 282 | bool UpdateValueUsesWith(Value *Old, Value *New) { |
| 283 | AddUsersToWorkList(*Old); // Add all modified instrs to worklist |
| 284 | if (Old != New) |
| 285 | Old->replaceAllUsesWith(New); |
| 286 | if (Instruction *I = dyn_cast<Instruction>(Old)) |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 287 | AddToWorkList(I); |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 288 | if (Instruction *I = dyn_cast<Instruction>(New)) |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 289 | AddToWorkList(I); |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 290 | return true; |
| 291 | } |
| 292 | |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 293 | // EraseInstFromFunction - When dealing with an instruction that has side |
| 294 | // effects or produces a void value, we can't rely on DCE to delete the |
| 295 | // instruction. Instead, visit methods should return the value returned by |
| 296 | // this function. |
| 297 | Instruction *EraseInstFromFunction(Instruction &I) { |
| 298 | assert(I.use_empty() && "Cannot erase instruction that is used!"); |
| 299 | AddUsesToWorkList(I); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 300 | RemoveFromWorkList(&I); |
Chris Lattner | 954f66a | 2004-11-18 21:41:39 +0000 | [diff] [blame] | 301 | I.eraseFromParent(); |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 302 | return 0; // Don't do anything with FI |
| 303 | } |
| 304 | |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 305 | private: |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 306 | /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the |
| 307 | /// InsertBefore instruction. This is specialized a bit to avoid inserting |
| 308 | /// casts that are known to not do anything... |
| 309 | /// |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 310 | Value *InsertOperandCastBefore(Instruction::CastOps opcode, |
| 311 | Value *V, const Type *DestTy, |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 312 | Instruction *InsertBefore); |
| 313 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 314 | /// SimplifyCommutative - This performs a few simplifications for |
| 315 | /// commutative operators. |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 316 | bool SimplifyCommutative(BinaryOperator &I); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 317 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 318 | /// SimplifyCompare - This reorders the operands of a CmpInst to get them in |
| 319 | /// most-complex to least-complex order. |
| 320 | bool SimplifyCompare(CmpInst &I); |
| 321 | |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 322 | bool SimplifyDemandedBits(Value *V, uint64_t Mask, |
| 323 | uint64_t &KnownZero, uint64_t &KnownOne, |
| 324 | unsigned Depth = 0); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 325 | |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 326 | Value *SimplifyDemandedVectorElts(Value *V, uint64_t DemandedElts, |
| 327 | uint64_t &UndefElts, unsigned Depth = 0); |
| 328 | |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 329 | // FoldOpIntoPhi - Given a binary operator or cast instruction which has a |
| 330 | // PHI node as operand #0, see if we can fold the instruction into the PHI |
| 331 | // (which is only possible if all operands to the PHI are constants). |
| 332 | Instruction *FoldOpIntoPhi(Instruction &I); |
| 333 | |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 334 | // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary" |
| 335 | // operator and they all are only used by the PHI, PHI together their |
| 336 | // inputs, and do the operation once, to the result of the PHI. |
| 337 | Instruction *FoldPHIArgOpIntoPHI(PHINode &PN); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 338 | Instruction *FoldPHIArgBinOpIntoPHI(PHINode &PN); |
| 339 | |
| 340 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 341 | Instruction *OptAndOp(Instruction *Op, ConstantInt *OpRHS, |
| 342 | ConstantInt *AndRHS, BinaryOperator &TheAnd); |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 343 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 344 | Value *FoldLogicalPlusAnd(Value *LHS, Value *RHS, ConstantInt *Mask, |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 345 | bool isSub, Instruction &I); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 346 | Instruction *InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 347 | bool isSigned, bool Inside, Instruction &IB); |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 348 | Instruction *PromoteCastOfAllocation(CastInst &CI, AllocationInst &AI); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 349 | Instruction *MatchBSwap(BinaryOperator &I); |
| 350 | |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 351 | Value *EvaluateInDifferentType(Value *V, const Type *Ty, bool isSigned); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 352 | }; |
Chris Lattner | f629309 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 353 | |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 354 | RegisterPass<InstCombiner> X("instcombine", "Combine redundant instructions"); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 357 | // getComplexity: Assign a complexity or rank value to LLVM Values... |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 358 | // 0 -> undef, 1 -> Const, 2 -> Other, 3 -> Arg, 3 -> Unary, 4 -> OtherInst |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 359 | static unsigned getComplexity(Value *V) { |
| 360 | if (isa<Instruction>(V)) { |
| 361 | if (BinaryOperator::isNeg(V) || BinaryOperator::isNot(V)) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 362 | return 3; |
| 363 | return 4; |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 364 | } |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 365 | if (isa<Argument>(V)) return 3; |
| 366 | return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2; |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 367 | } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 368 | |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 369 | // isOnlyUse - Return true if this instruction will be deleted if we stop using |
| 370 | // it. |
| 371 | static bool isOnlyUse(Value *V) { |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 372 | return V->hasOneUse() || isa<Constant>(V); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 375 | // getPromotedType - Return the specified type promoted as it would be to pass |
| 376 | // though a va_arg area... |
| 377 | static const Type *getPromotedType(const Type *Ty) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 378 | if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty)) { |
| 379 | if (ITy->getBitWidth() < 32) |
| 380 | return Type::Int32Ty; |
| 381 | } else if (Ty == Type::FloatTy) |
| 382 | return Type::DoubleTy; |
| 383 | return Ty; |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 386 | /// getBitCastOperand - If the specified operand is a CastInst or a constant |
| 387 | /// expression bitcast, return the operand value, otherwise return null. |
| 388 | static Value *getBitCastOperand(Value *V) { |
| 389 | if (BitCastInst *I = dyn_cast<BitCastInst>(V)) |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 390 | return I->getOperand(0); |
| 391 | else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 392 | if (CE->getOpcode() == Instruction::BitCast) |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 393 | return CE->getOperand(0); |
| 394 | return 0; |
| 395 | } |
| 396 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 397 | /// This function is a wrapper around CastInst::isEliminableCastPair. It |
| 398 | /// simply extracts arguments and returns what that function returns. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 399 | static Instruction::CastOps |
| 400 | isEliminableCastPair( |
| 401 | const CastInst *CI, ///< The first cast instruction |
| 402 | unsigned opcode, ///< The opcode of the second cast instruction |
| 403 | const Type *DstTy, ///< The target type for the second cast instruction |
| 404 | TargetData *TD ///< The target data for pointer size |
| 405 | ) { |
| 406 | |
| 407 | const Type *SrcTy = CI->getOperand(0)->getType(); // A from above |
| 408 | const Type *MidTy = CI->getType(); // B from above |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 409 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 410 | // Get the opcodes of the two Cast instructions |
| 411 | Instruction::CastOps firstOp = Instruction::CastOps(CI->getOpcode()); |
| 412 | Instruction::CastOps secondOp = Instruction::CastOps(opcode); |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 413 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 414 | return Instruction::CastOps( |
| 415 | CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, |
| 416 | DstTy, TD->getIntPtrType())); |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | /// ValueRequiresCast - Return true if the cast from "V to Ty" actually results |
| 420 | /// in any code being generated. It does not require codegen if V is simple |
| 421 | /// enough or if the cast can be folded into other casts. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 422 | static bool ValueRequiresCast(Instruction::CastOps opcode, const Value *V, |
| 423 | const Type *Ty, TargetData *TD) { |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 424 | if (V->getType() == Ty || isa<Constant>(V)) return false; |
| 425 | |
Chris Lattner | 01575b7 | 2006-05-25 23:24:33 +0000 | [diff] [blame] | 426 | // 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] | 427 | if (const CastInst *CI = dyn_cast<CastInst>(V)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 428 | if (isEliminableCastPair(CI, opcode, Ty, TD)) |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 429 | return false; |
| 430 | return true; |
| 431 | } |
| 432 | |
| 433 | /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the |
| 434 | /// InsertBefore instruction. This is specialized a bit to avoid inserting |
| 435 | /// casts that are known to not do anything... |
| 436 | /// |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 437 | Value *InstCombiner::InsertOperandCastBefore(Instruction::CastOps opcode, |
| 438 | Value *V, const Type *DestTy, |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 439 | Instruction *InsertBefore) { |
| 440 | if (V->getType() == DestTy) return V; |
| 441 | if (Constant *C = dyn_cast<Constant>(V)) |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 442 | return ConstantExpr::getCast(opcode, C, DestTy); |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 443 | |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 444 | return InsertCastBefore(opcode, V, DestTy, *InsertBefore); |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 447 | // SimplifyCommutative - This performs a few simplifications for commutative |
| 448 | // operators: |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 449 | // |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 450 | // 1. Order operands such that they are listed from right (least complex) to |
| 451 | // left (most complex). This puts constants before unary operators before |
| 452 | // binary operators. |
| 453 | // |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 454 | // 2. Transform: (op (op V, C1), C2) ==> (op V, (op C1, C2)) |
| 455 | // 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] | 456 | // |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 457 | bool InstCombiner::SimplifyCommutative(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 458 | bool Changed = false; |
| 459 | if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1))) |
| 460 | Changed = !I.swapOperands(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 461 | |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 462 | if (!I.isAssociative()) return Changed; |
| 463 | Instruction::BinaryOps Opcode = I.getOpcode(); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 464 | if (BinaryOperator *Op = dyn_cast<BinaryOperator>(I.getOperand(0))) |
| 465 | if (Op->getOpcode() == Opcode && isa<Constant>(Op->getOperand(1))) { |
| 466 | if (isa<Constant>(I.getOperand(1))) { |
Chris Lattner | 2a9c847 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 467 | Constant *Folded = ConstantExpr::get(I.getOpcode(), |
| 468 | cast<Constant>(I.getOperand(1)), |
| 469 | cast<Constant>(Op->getOperand(1))); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 470 | I.setOperand(0, Op->getOperand(0)); |
| 471 | I.setOperand(1, Folded); |
| 472 | return true; |
| 473 | } else if (BinaryOperator *Op1=dyn_cast<BinaryOperator>(I.getOperand(1))) |
| 474 | if (Op1->getOpcode() == Opcode && isa<Constant>(Op1->getOperand(1)) && |
| 475 | isOnlyUse(Op) && isOnlyUse(Op1)) { |
| 476 | Constant *C1 = cast<Constant>(Op->getOperand(1)); |
| 477 | Constant *C2 = cast<Constant>(Op1->getOperand(1)); |
| 478 | |
| 479 | // 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] | 480 | Constant *Folded = ConstantExpr::get(I.getOpcode(), C1, C2); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 481 | Instruction *New = BinaryOperator::create(Opcode, Op->getOperand(0), |
| 482 | Op1->getOperand(0), |
| 483 | Op1->getName(), &I); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 484 | AddToWorkList(New); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 485 | I.setOperand(0, New); |
| 486 | I.setOperand(1, Folded); |
| 487 | return true; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 488 | } |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 489 | } |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 490 | return Changed; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 491 | } |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 492 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 493 | /// SimplifyCompare - For a CmpInst this function just orders the operands |
| 494 | /// so that theyare listed from right (least complex) to left (most complex). |
| 495 | /// This puts constants before unary operators before binary operators. |
| 496 | bool InstCombiner::SimplifyCompare(CmpInst &I) { |
| 497 | if (getComplexity(I.getOperand(0)) >= getComplexity(I.getOperand(1))) |
| 498 | return false; |
| 499 | I.swapOperands(); |
| 500 | // Compare instructions are not associative so there's nothing else we can do. |
| 501 | return true; |
| 502 | } |
| 503 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 504 | // dyn_castNegVal - Given a 'sub' instruction, return the RHS of the instruction |
| 505 | // if the LHS is a constant zero (which is the 'negate' form). |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 506 | // |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 507 | static inline Value *dyn_castNegVal(Value *V) { |
| 508 | if (BinaryOperator::isNeg(V)) |
Chris Lattner | a1df33c | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 509 | return BinaryOperator::getNegArgument(V); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 510 | |
Chris Lattner | 0ce8580 | 2004-12-14 20:08:06 +0000 | [diff] [blame] | 511 | // Constants can be considered to be negated values if they can be folded. |
| 512 | if (ConstantInt *C = dyn_cast<ConstantInt>(V)) |
| 513 | return ConstantExpr::getNeg(C); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 514 | return 0; |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 517 | static inline Value *dyn_castNotVal(Value *V) { |
| 518 | if (BinaryOperator::isNot(V)) |
Chris Lattner | a1df33c | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 519 | return BinaryOperator::getNotArgument(V); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 520 | |
| 521 | // Constants can be considered to be not'ed values... |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 522 | if (ConstantInt *C = dyn_cast<ConstantInt>(V)) |
Chris Lattner | 448c323 | 2004-06-10 02:12:35 +0000 | [diff] [blame] | 523 | return ConstantExpr::getNot(C); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 524 | return 0; |
| 525 | } |
| 526 | |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 527 | // dyn_castFoldableMul - If this value is a multiply that can be folded into |
| 528 | // other computations (because it has a constant operand), return the |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 529 | // non-constant operand of the multiply, and set CST to point to the multiplier. |
| 530 | // Otherwise, return null. |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 531 | // |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 532 | static inline Value *dyn_castFoldableMul(Value *V, ConstantInt *&CST) { |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 533 | if (V->hasOneUse() && V->getType()->isInteger()) |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 534 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 535 | if (I->getOpcode() == Instruction::Mul) |
Chris Lattner | 50e60c7 | 2004-11-15 05:54:07 +0000 | [diff] [blame] | 536 | if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 537 | return I->getOperand(0); |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 538 | if (I->getOpcode() == Instruction::Shl) |
Chris Lattner | 50e60c7 | 2004-11-15 05:54:07 +0000 | [diff] [blame] | 539 | if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) { |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 540 | // The multiplier is really 1 << CST. |
| 541 | Constant *One = ConstantInt::get(V->getType(), 1); |
| 542 | CST = cast<ConstantInt>(ConstantExpr::getShl(One, CST)); |
| 543 | return I->getOperand(0); |
| 544 | } |
| 545 | } |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 546 | return 0; |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 547 | } |
Chris Lattner | af2930e | 2002-08-14 17:51:49 +0000 | [diff] [blame] | 548 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 549 | /// dyn_castGetElementPtr - If this is a getelementptr instruction or constant |
| 550 | /// expression, return it. |
| 551 | static User *dyn_castGetElementPtr(Value *V) { |
| 552 | if (isa<GetElementPtrInst>(V)) return cast<User>(V); |
| 553 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
| 554 | if (CE->getOpcode() == Instruction::GetElementPtr) |
| 555 | return cast<User>(V); |
| 556 | return false; |
| 557 | } |
| 558 | |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 559 | // AddOne, SubOne - Add or subtract a constant one from an integer constant... |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 560 | static ConstantInt *AddOne(ConstantInt *C) { |
| 561 | return cast<ConstantInt>(ConstantExpr::getAdd(C, |
| 562 | ConstantInt::get(C->getType(), 1))); |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 563 | } |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 564 | static ConstantInt *SubOne(ConstantInt *C) { |
| 565 | return cast<ConstantInt>(ConstantExpr::getSub(C, |
| 566 | ConstantInt::get(C->getType(), 1))); |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 567 | } |
| 568 | |
Chris Lattner | 68d5ff2 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 569 | /// ComputeMaskedBits - Determine which of the bits specified in Mask are |
| 570 | /// known to be either zero or one and return them in the KnownZero/KnownOne |
| 571 | /// bitsets. This code only analyzes bits in Mask, in order to short-circuit |
| 572 | /// processing. |
| 573 | static void ComputeMaskedBits(Value *V, uint64_t Mask, uint64_t &KnownZero, |
| 574 | uint64_t &KnownOne, unsigned Depth = 0) { |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 575 | // Note, we cannot consider 'undef' to be "IsZero" here. The problem is that |
| 576 | // we cannot optimize based on the assumption that it is zero without changing |
Chris Lattner | 3bedbd9 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 577 | // it to be an explicit zero. If we don't change it to zero, other code could |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 578 | // optimized based on the contradictory assumption that it is non-zero. |
| 579 | // Because instcombine aggressively folds operations with undef args anyway, |
| 580 | // this won't lose us code quality. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 581 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
Chris Lattner | 68d5ff2 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 582 | // We know all of the bits for a constant! |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 583 | KnownOne = CI->getZExtValue() & Mask; |
Chris Lattner | 68d5ff2 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 584 | KnownZero = ~KnownOne & Mask; |
| 585 | return; |
| 586 | } |
| 587 | |
| 588 | KnownZero = KnownOne = 0; // Don't know anything. |
Chris Lattner | 74c51a0 | 2006-02-07 08:05:22 +0000 | [diff] [blame] | 589 | if (Depth == 6 || Mask == 0) |
Chris Lattner | 68d5ff2 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 590 | return; // Limit search depth. |
| 591 | |
| 592 | uint64_t KnownZero2, KnownOne2; |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 593 | Instruction *I = dyn_cast<Instruction>(V); |
| 594 | if (!I) return; |
| 595 | |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 596 | Mask &= cast<IntegerType>(V->getType())->getBitMask(); |
Chris Lattner | e315830 | 2006-05-04 17:33:35 +0000 | [diff] [blame] | 597 | |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 598 | switch (I->getOpcode()) { |
| 599 | case Instruction::And: |
| 600 | // If either the LHS or the RHS are Zero, the result is zero. |
| 601 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, Depth+1); |
| 602 | Mask &= ~KnownZero; |
| 603 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1); |
| 604 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 605 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 606 | |
| 607 | // Output known-1 bits are only known if set in both the LHS & RHS. |
| 608 | KnownOne &= KnownOne2; |
| 609 | // Output known-0 are known to be clear if zero in either the LHS | RHS. |
| 610 | KnownZero |= KnownZero2; |
| 611 | return; |
| 612 | case Instruction::Or: |
| 613 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, Depth+1); |
| 614 | Mask &= ~KnownOne; |
| 615 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1); |
| 616 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 617 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 618 | |
| 619 | // Output known-0 bits are only known if clear in both the LHS & RHS. |
| 620 | KnownZero &= KnownZero2; |
| 621 | // Output known-1 are known to be set if set in either the LHS | RHS. |
| 622 | KnownOne |= KnownOne2; |
| 623 | return; |
| 624 | case Instruction::Xor: { |
| 625 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, Depth+1); |
| 626 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1); |
| 627 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 628 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 629 | |
| 630 | // Output known-0 bits are known if clear or set in both the LHS & RHS. |
| 631 | uint64_t KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2); |
| 632 | // Output known-1 are known to be set if set in only one of the LHS, RHS. |
| 633 | KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2); |
| 634 | KnownZero = KnownZeroOut; |
| 635 | return; |
| 636 | } |
| 637 | case Instruction::Select: |
| 638 | ComputeMaskedBits(I->getOperand(2), Mask, KnownZero, KnownOne, Depth+1); |
| 639 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1); |
| 640 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 641 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 642 | |
| 643 | // Only known if known in both the LHS and RHS. |
| 644 | KnownOne &= KnownOne2; |
| 645 | KnownZero &= KnownZero2; |
| 646 | return; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 647 | case Instruction::FPTrunc: |
| 648 | case Instruction::FPExt: |
| 649 | case Instruction::FPToUI: |
| 650 | case Instruction::FPToSI: |
| 651 | case Instruction::SIToFP: |
| 652 | case Instruction::PtrToInt: |
| 653 | case Instruction::UIToFP: |
| 654 | case Instruction::IntToPtr: |
| 655 | return; // Can't work with floating point or pointers |
| 656 | case Instruction::Trunc: |
| 657 | // All these have integer operands |
| 658 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero, KnownOne, Depth+1); |
| 659 | return; |
| 660 | case Instruction::BitCast: { |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 661 | const Type *SrcTy = I->getOperand(0)->getType(); |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 662 | if (SrcTy->isInteger()) { |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 663 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero, KnownOne, Depth+1); |
Chris Lattner | 68d5ff2 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 664 | return; |
| 665 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 666 | break; |
| 667 | } |
| 668 | case Instruction::ZExt: { |
| 669 | // Compute the bits in the result that are not present in the input. |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 670 | const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType()); |
| 671 | uint64_t NotIn = ~SrcTy->getBitMask(); |
| 672 | uint64_t NewBits = cast<IntegerType>(I->getType())->getBitMask() & NotIn; |
Chris Lattner | 60de63d | 2005-10-09 06:36:35 +0000 | [diff] [blame] | 673 | |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 674 | Mask &= SrcTy->getBitMask(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 675 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero, KnownOne, Depth+1); |
| 676 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 677 | // The top bits are known to be zero. |
| 678 | KnownZero |= NewBits; |
| 679 | return; |
| 680 | } |
| 681 | case Instruction::SExt: { |
| 682 | // Compute the bits in the result that are not present in the input. |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 683 | const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType()); |
| 684 | uint64_t NotIn = ~SrcTy->getBitMask(); |
| 685 | uint64_t NewBits = cast<IntegerType>(I->getType())->getBitMask() & NotIn; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 686 | |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 687 | Mask &= SrcTy->getBitMask(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 688 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero, KnownOne, Depth+1); |
| 689 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
Chris Lattner | 74c51a0 | 2006-02-07 08:05:22 +0000 | [diff] [blame] | 690 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 691 | // If the sign bit of the input is known set or clear, then we know the |
| 692 | // top bits of the result. |
| 693 | uint64_t InSignBit = 1ULL << (SrcTy->getPrimitiveSizeInBits()-1); |
| 694 | if (KnownZero & InSignBit) { // Input sign bit known zero |
| 695 | KnownZero |= NewBits; |
| 696 | KnownOne &= ~NewBits; |
| 697 | } else if (KnownOne & InSignBit) { // Input sign bit known set |
| 698 | KnownOne |= NewBits; |
| 699 | KnownZero &= ~NewBits; |
| 700 | } else { // Input sign bit unknown |
| 701 | KnownZero &= ~NewBits; |
| 702 | KnownOne &= ~NewBits; |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 703 | } |
| 704 | return; |
| 705 | } |
| 706 | case Instruction::Shl: |
| 707 | // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0 |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 708 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 709 | uint64_t ShiftAmt = SA->getZExtValue(); |
| 710 | Mask >>= ShiftAmt; |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 711 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero, KnownOne, Depth+1); |
| 712 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 713 | KnownZero <<= ShiftAmt; |
| 714 | KnownOne <<= ShiftAmt; |
| 715 | KnownZero |= (1ULL << ShiftAmt)-1; // low bits known zero. |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 716 | return; |
| 717 | } |
| 718 | break; |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 719 | case Instruction::LShr: |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 720 | // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0 |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 721 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 722 | // Compute the new bits that are at the top now. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 723 | uint64_t ShiftAmt = SA->getZExtValue(); |
| 724 | uint64_t HighBits = (1ULL << ShiftAmt)-1; |
| 725 | HighBits <<= I->getType()->getPrimitiveSizeInBits()-ShiftAmt; |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 726 | |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 727 | // Unsigned shift right. |
| 728 | Mask <<= ShiftAmt; |
| 729 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero,KnownOne,Depth+1); |
| 730 | assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); |
| 731 | KnownZero >>= ShiftAmt; |
| 732 | KnownOne >>= ShiftAmt; |
| 733 | KnownZero |= HighBits; // high bits known zero. |
| 734 | return; |
| 735 | } |
| 736 | break; |
| 737 | case Instruction::AShr: |
| 738 | // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0 |
| 739 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 740 | // Compute the new bits that are at the top now. |
| 741 | uint64_t ShiftAmt = SA->getZExtValue(); |
| 742 | uint64_t HighBits = (1ULL << ShiftAmt)-1; |
| 743 | HighBits <<= I->getType()->getPrimitiveSizeInBits()-ShiftAmt; |
| 744 | |
| 745 | // Signed shift right. |
| 746 | Mask <<= ShiftAmt; |
| 747 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero,KnownOne,Depth+1); |
| 748 | assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); |
| 749 | KnownZero >>= ShiftAmt; |
| 750 | KnownOne >>= ShiftAmt; |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 751 | |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 752 | // Handle the sign bits. |
| 753 | uint64_t SignBit = 1ULL << (I->getType()->getPrimitiveSizeInBits()-1); |
| 754 | SignBit >>= ShiftAmt; // Adjust to where it is now in the mask. |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 755 | |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 756 | if (KnownZero & SignBit) { // New bits are known zero. |
| 757 | KnownZero |= HighBits; |
| 758 | } else if (KnownOne & SignBit) { // New bits are known one. |
| 759 | KnownOne |= HighBits; |
Chris Lattner | 68d5ff2 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 760 | } |
| 761 | return; |
Chris Lattner | 60de63d | 2005-10-09 06:36:35 +0000 | [diff] [blame] | 762 | } |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 763 | break; |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 764 | } |
Chris Lattner | 74c51a0 | 2006-02-07 08:05:22 +0000 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use |
| 768 | /// this predicate to simplify operations downstream. Mask is known to be zero |
| 769 | /// for bits that V cannot have. |
| 770 | static bool MaskedValueIsZero(Value *V, uint64_t Mask, unsigned Depth = 0) { |
Chris Lattner | 68d5ff2 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 771 | uint64_t KnownZero, KnownOne; |
| 772 | ComputeMaskedBits(V, Mask, KnownZero, KnownOne, Depth); |
| 773 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 774 | return (KnownZero & Mask) == Mask; |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 775 | } |
| 776 | |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 777 | /// ShrinkDemandedConstant - Check to see if the specified operand of the |
| 778 | /// specified instruction is a constant integer. If so, check to see if there |
| 779 | /// are any bits set in the constant that are not demanded. If so, shrink the |
| 780 | /// constant and return true. |
| 781 | static bool ShrinkDemandedConstant(Instruction *I, unsigned OpNo, |
| 782 | uint64_t Demanded) { |
| 783 | ConstantInt *OpC = dyn_cast<ConstantInt>(I->getOperand(OpNo)); |
| 784 | if (!OpC) return false; |
| 785 | |
| 786 | // If there are no bits set that aren't demanded, nothing to do. |
| 787 | if ((~Demanded & OpC->getZExtValue()) == 0) |
| 788 | return false; |
| 789 | |
| 790 | // This is producing any bits that are not needed, shrink the RHS. |
| 791 | uint64_t Val = Demanded & OpC->getZExtValue(); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 792 | I->setOperand(OpNo, ConstantInt::get(OpC->getType(), Val)); |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 793 | return true; |
| 794 | } |
| 795 | |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 796 | // ComputeSignedMinMaxValuesFromKnownBits - Given a signed integer type and a |
| 797 | // set of known zero and one bits, compute the maximum and minimum values that |
| 798 | // could have the specified known zero and known one bits, returning them in |
| 799 | // min/max. |
| 800 | static void ComputeSignedMinMaxValuesFromKnownBits(const Type *Ty, |
| 801 | uint64_t KnownZero, |
| 802 | uint64_t KnownOne, |
| 803 | int64_t &Min, int64_t &Max) { |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 804 | uint64_t TypeBits = cast<IntegerType>(Ty)->getBitMask(); |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 805 | uint64_t UnknownBits = ~(KnownZero|KnownOne) & TypeBits; |
| 806 | |
| 807 | uint64_t SignBit = 1ULL << (Ty->getPrimitiveSizeInBits()-1); |
| 808 | |
| 809 | // The minimum value is when all unknown bits are zeros, EXCEPT for the sign |
| 810 | // bit if it is unknown. |
| 811 | Min = KnownOne; |
| 812 | Max = KnownOne|UnknownBits; |
| 813 | |
| 814 | if (SignBit & UnknownBits) { // Sign bit is unknown |
| 815 | Min |= SignBit; |
| 816 | Max &= ~SignBit; |
| 817 | } |
| 818 | |
| 819 | // Sign extend the min/max values. |
| 820 | int ShAmt = 64-Ty->getPrimitiveSizeInBits(); |
| 821 | Min = (Min << ShAmt) >> ShAmt; |
| 822 | Max = (Max << ShAmt) >> ShAmt; |
| 823 | } |
| 824 | |
| 825 | // ComputeUnsignedMinMaxValuesFromKnownBits - Given an unsigned integer type and |
| 826 | // a set of known zero and one bits, compute the maximum and minimum values that |
| 827 | // could have the specified known zero and known one bits, returning them in |
| 828 | // min/max. |
| 829 | static void ComputeUnsignedMinMaxValuesFromKnownBits(const Type *Ty, |
| 830 | uint64_t KnownZero, |
| 831 | uint64_t KnownOne, |
| 832 | uint64_t &Min, |
| 833 | uint64_t &Max) { |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 834 | uint64_t TypeBits = cast<IntegerType>(Ty)->getBitMask(); |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 835 | uint64_t UnknownBits = ~(KnownZero|KnownOne) & TypeBits; |
| 836 | |
| 837 | // The minimum value is when the unknown bits are all zeros. |
| 838 | Min = KnownOne; |
| 839 | // The maximum value is when the unknown bits are all ones. |
| 840 | Max = KnownOne|UnknownBits; |
| 841 | } |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 842 | |
| 843 | |
| 844 | /// SimplifyDemandedBits - Look at V. At this point, we know that only the |
| 845 | /// DemandedMask bits of the result of V are ever used downstream. If we can |
| 846 | /// use this information to simplify V, do so and return true. Otherwise, |
| 847 | /// analyze the expression and return a mask of KnownOne and KnownZero bits for |
| 848 | /// the expression (used to simplify the caller). The KnownZero/One bits may |
| 849 | /// only be accurate for those bits in the DemandedMask. |
| 850 | bool InstCombiner::SimplifyDemandedBits(Value *V, uint64_t DemandedMask, |
| 851 | uint64_t &KnownZero, uint64_t &KnownOne, |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 852 | unsigned Depth) { |
Chris Lattner | d5fa214 | 2007-03-04 23:16:36 +0000 | [diff] [blame] | 853 | const IntegerType *VTy = cast<IntegerType>(V->getType()); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 854 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 855 | // We know all of the bits for a constant! |
| 856 | KnownOne = CI->getZExtValue() & DemandedMask; |
| 857 | KnownZero = ~KnownOne & DemandedMask; |
| 858 | return false; |
| 859 | } |
| 860 | |
| 861 | KnownZero = KnownOne = 0; |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 862 | if (!V->hasOneUse()) { // Other users may use these bits. |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 863 | if (Depth != 0) { // Not at the root. |
| 864 | // Just compute the KnownZero/KnownOne bits to simplify things downstream. |
| 865 | ComputeMaskedBits(V, DemandedMask, KnownZero, KnownOne, Depth); |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 866 | return false; |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 867 | } |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 868 | // If this is the root being simplified, allow it to have multiple uses, |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 869 | // just set the DemandedMask to all bits. |
Chris Lattner | d5fa214 | 2007-03-04 23:16:36 +0000 | [diff] [blame] | 870 | DemandedMask = VTy->getBitMask(); |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 871 | } else if (DemandedMask == 0) { // Not demanding any bits from V. |
Chris Lattner | d5fa214 | 2007-03-04 23:16:36 +0000 | [diff] [blame] | 872 | if (V != UndefValue::get(VTy)) |
| 873 | return UpdateValueUsesWith(V, UndefValue::get(VTy)); |
Chris Lattner | 74c51a0 | 2006-02-07 08:05:22 +0000 | [diff] [blame] | 874 | return false; |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 875 | } else if (Depth == 6) { // Limit search depth. |
| 876 | return false; |
| 877 | } |
| 878 | |
| 879 | Instruction *I = dyn_cast<Instruction>(V); |
| 880 | if (!I) return false; // Only analyze instructions. |
| 881 | |
Chris Lattner | d5fa214 | 2007-03-04 23:16:36 +0000 | [diff] [blame] | 882 | DemandedMask &= VTy->getBitMask(); |
Chris Lattner | e315830 | 2006-05-04 17:33:35 +0000 | [diff] [blame] | 883 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 884 | uint64_t KnownZero2 = 0, KnownOne2 = 0; |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 885 | switch (I->getOpcode()) { |
| 886 | default: break; |
| 887 | case Instruction::And: |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 888 | // If either the LHS or the RHS are Zero, the result is zero. |
| 889 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, |
| 890 | KnownZero, KnownOne, Depth+1)) |
| 891 | return true; |
| 892 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 893 | |
| 894 | // If something is known zero on the RHS, the bits aren't demanded on the |
| 895 | // LHS. |
| 896 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask & ~KnownZero, |
| 897 | KnownZero2, KnownOne2, Depth+1)) |
| 898 | return true; |
| 899 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 900 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 901 | // If all of the demanded bits are known 1 on one side, return the other. |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 902 | // These bits cannot contribute to the result of the 'and'. |
| 903 | if ((DemandedMask & ~KnownZero2 & KnownOne) == (DemandedMask & ~KnownZero2)) |
| 904 | return UpdateValueUsesWith(I, I->getOperand(0)); |
| 905 | if ((DemandedMask & ~KnownZero & KnownOne2) == (DemandedMask & ~KnownZero)) |
| 906 | return UpdateValueUsesWith(I, I->getOperand(1)); |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 907 | |
| 908 | // If all of the demanded bits in the inputs are known zeros, return zero. |
| 909 | if ((DemandedMask & (KnownZero|KnownZero2)) == DemandedMask) |
Chris Lattner | d5fa214 | 2007-03-04 23:16:36 +0000 | [diff] [blame] | 910 | return UpdateValueUsesWith(I, Constant::getNullValue(VTy)); |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 911 | |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 912 | // If the RHS is a constant, see if we can simplify it. |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 913 | if (ShrinkDemandedConstant(I, 1, DemandedMask & ~KnownZero2)) |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 914 | return UpdateValueUsesWith(I, I); |
| 915 | |
| 916 | // Output known-1 bits are only known if set in both the LHS & RHS. |
| 917 | KnownOne &= KnownOne2; |
| 918 | // Output known-0 are known to be clear if zero in either the LHS | RHS. |
| 919 | KnownZero |= KnownZero2; |
| 920 | break; |
| 921 | case Instruction::Or: |
| 922 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, |
| 923 | KnownZero, KnownOne, Depth+1)) |
| 924 | return true; |
| 925 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 926 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask & ~KnownOne, |
| 927 | KnownZero2, KnownOne2, Depth+1)) |
| 928 | return true; |
| 929 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 930 | |
| 931 | // If all of the demanded bits are known zero on one side, return the other. |
| 932 | // These bits cannot contribute to the result of the 'or'. |
Jeff Cohen | bce4805 | 2006-02-18 03:20:33 +0000 | [diff] [blame] | 933 | if ((DemandedMask & ~KnownOne2 & KnownZero) == (DemandedMask & ~KnownOne2)) |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 934 | return UpdateValueUsesWith(I, I->getOperand(0)); |
Jeff Cohen | bce4805 | 2006-02-18 03:20:33 +0000 | [diff] [blame] | 935 | if ((DemandedMask & ~KnownOne & KnownZero2) == (DemandedMask & ~KnownOne)) |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 936 | return UpdateValueUsesWith(I, I->getOperand(1)); |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 937 | |
| 938 | // If all of the potentially set bits on one side are known to be set on |
| 939 | // the other side, just use the 'other' side. |
| 940 | if ((DemandedMask & (~KnownZero) & KnownOne2) == |
| 941 | (DemandedMask & (~KnownZero))) |
| 942 | return UpdateValueUsesWith(I, I->getOperand(0)); |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 943 | if ((DemandedMask & (~KnownZero2) & KnownOne) == |
| 944 | (DemandedMask & (~KnownZero2))) |
| 945 | return UpdateValueUsesWith(I, I->getOperand(1)); |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 946 | |
| 947 | // If the RHS is a constant, see if we can simplify it. |
| 948 | if (ShrinkDemandedConstant(I, 1, DemandedMask)) |
| 949 | return UpdateValueUsesWith(I, I); |
| 950 | |
| 951 | // Output known-0 bits are only known if clear in both the LHS & RHS. |
| 952 | KnownZero &= KnownZero2; |
| 953 | // Output known-1 are known to be set if set in either the LHS | RHS. |
| 954 | KnownOne |= KnownOne2; |
| 955 | break; |
| 956 | case Instruction::Xor: { |
| 957 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, |
| 958 | KnownZero, KnownOne, Depth+1)) |
| 959 | return true; |
| 960 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 961 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, |
| 962 | KnownZero2, KnownOne2, Depth+1)) |
| 963 | return true; |
| 964 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 965 | |
| 966 | // If all of the demanded bits are known zero on one side, return the other. |
| 967 | // These bits cannot contribute to the result of the 'xor'. |
| 968 | if ((DemandedMask & KnownZero) == DemandedMask) |
| 969 | return UpdateValueUsesWith(I, I->getOperand(0)); |
| 970 | if ((DemandedMask & KnownZero2) == DemandedMask) |
| 971 | return UpdateValueUsesWith(I, I->getOperand(1)); |
| 972 | |
| 973 | // Output known-0 bits are known if clear or set in both the LHS & RHS. |
| 974 | uint64_t KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2); |
| 975 | // Output known-1 are known to be set if set in only one of the LHS, RHS. |
| 976 | uint64_t KnownOneOut = (KnownZero & KnownOne2) | (KnownOne & KnownZero2); |
| 977 | |
Chris Lattner | f2f1643 | 2006-11-27 19:55:07 +0000 | [diff] [blame] | 978 | // If all of the demanded bits are known to be zero on one side or the |
| 979 | // other, turn this into an *inclusive* or. |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 980 | // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0 |
Chris Lattner | f2f1643 | 2006-11-27 19:55:07 +0000 | [diff] [blame] | 981 | if ((DemandedMask & ~KnownZero & ~KnownZero2) == 0) { |
| 982 | Instruction *Or = |
| 983 | BinaryOperator::createOr(I->getOperand(0), I->getOperand(1), |
| 984 | I->getName()); |
| 985 | InsertNewInstBefore(Or, *I); |
| 986 | return UpdateValueUsesWith(I, Or); |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 987 | } |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 988 | |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 989 | // If all of the demanded bits on one side are known, and all of the set |
| 990 | // bits on that side are also known to be set on the other side, turn this |
| 991 | // into an AND, as we know the bits will be cleared. |
| 992 | // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2 |
| 993 | if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask) { // all known |
| 994 | if ((KnownOne & KnownOne2) == KnownOne) { |
Chris Lattner | d5fa214 | 2007-03-04 23:16:36 +0000 | [diff] [blame] | 995 | Constant *AndC = ConstantInt::get(VTy, ~KnownOne & DemandedMask); |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 996 | Instruction *And = |
| 997 | BinaryOperator::createAnd(I->getOperand(0), AndC, "tmp"); |
| 998 | InsertNewInstBefore(And, *I); |
| 999 | return UpdateValueUsesWith(I, And); |
| 1000 | } |
| 1001 | } |
| 1002 | |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1003 | // If the RHS is a constant, see if we can simplify it. |
| 1004 | // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1. |
| 1005 | if (ShrinkDemandedConstant(I, 1, DemandedMask)) |
| 1006 | return UpdateValueUsesWith(I, I); |
| 1007 | |
| 1008 | KnownZero = KnownZeroOut; |
| 1009 | KnownOne = KnownOneOut; |
| 1010 | break; |
| 1011 | } |
| 1012 | case Instruction::Select: |
| 1013 | if (SimplifyDemandedBits(I->getOperand(2), DemandedMask, |
| 1014 | KnownZero, KnownOne, Depth+1)) |
| 1015 | return true; |
| 1016 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, |
| 1017 | KnownZero2, KnownOne2, Depth+1)) |
| 1018 | return true; |
| 1019 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 1020 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 1021 | |
| 1022 | // If the operands are constants, see if we can simplify them. |
| 1023 | if (ShrinkDemandedConstant(I, 1, DemandedMask)) |
| 1024 | return UpdateValueUsesWith(I, I); |
| 1025 | if (ShrinkDemandedConstant(I, 2, DemandedMask)) |
| 1026 | return UpdateValueUsesWith(I, I); |
| 1027 | |
| 1028 | // Only known if known in both the LHS and RHS. |
| 1029 | KnownOne &= KnownOne2; |
| 1030 | KnownZero &= KnownZero2; |
| 1031 | break; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1032 | case Instruction::Trunc: |
| 1033 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, |
| 1034 | KnownZero, KnownOne, Depth+1)) |
| 1035 | return true; |
| 1036 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 1037 | break; |
| 1038 | case Instruction::BitCast: |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1039 | if (!I->getOperand(0)->getType()->isInteger()) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1040 | return false; |
Chris Lattner | f6bd07c | 2006-09-16 03:14:10 +0000 | [diff] [blame] | 1041 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1042 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, |
| 1043 | KnownZero, KnownOne, Depth+1)) |
| 1044 | return true; |
| 1045 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 1046 | break; |
| 1047 | case Instruction::ZExt: { |
| 1048 | // Compute the bits in the result that are not present in the input. |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 1049 | const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType()); |
| 1050 | uint64_t NotIn = ~SrcTy->getBitMask(); |
Chris Lattner | d5fa214 | 2007-03-04 23:16:36 +0000 | [diff] [blame] | 1051 | uint64_t NewBits = VTy->getBitMask() & NotIn; |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1052 | |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 1053 | DemandedMask &= SrcTy->getBitMask(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1054 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, |
| 1055 | KnownZero, KnownOne, Depth+1)) |
| 1056 | return true; |
| 1057 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 1058 | // The top bits are known to be zero. |
| 1059 | KnownZero |= NewBits; |
| 1060 | break; |
| 1061 | } |
| 1062 | case Instruction::SExt: { |
| 1063 | // Compute the bits in the result that are not present in the input. |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 1064 | const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType()); |
| 1065 | uint64_t NotIn = ~SrcTy->getBitMask(); |
Chris Lattner | d5fa214 | 2007-03-04 23:16:36 +0000 | [diff] [blame] | 1066 | uint64_t NewBits = VTy->getBitMask() & NotIn; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1067 | |
| 1068 | // Get the sign bit for the source type |
| 1069 | uint64_t InSignBit = 1ULL << (SrcTy->getPrimitiveSizeInBits()-1); |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 1070 | int64_t InputDemandedBits = DemandedMask & SrcTy->getBitMask(); |
Chris Lattner | f345fe4 | 2006-02-13 22:41:07 +0000 | [diff] [blame] | 1071 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1072 | // If any of the sign extended bits are demanded, we know that the sign |
| 1073 | // bit is demanded. |
| 1074 | if (NewBits & DemandedMask) |
| 1075 | InputDemandedBits |= InSignBit; |
Chris Lattner | f345fe4 | 2006-02-13 22:41:07 +0000 | [diff] [blame] | 1076 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1077 | if (SimplifyDemandedBits(I->getOperand(0), InputDemandedBits, |
| 1078 | KnownZero, KnownOne, Depth+1)) |
| 1079 | return true; |
| 1080 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1081 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1082 | // If the sign bit of the input is known set or clear, then we know the |
| 1083 | // top bits of the result. |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 1084 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1085 | // If the input sign bit is known zero, or if the NewBits are not demanded |
| 1086 | // convert this into a zero extension. |
| 1087 | if ((KnownZero & InSignBit) || (NewBits & ~DemandedMask) == NewBits) { |
| 1088 | // Convert to ZExt cast |
Chris Lattner | d5fa214 | 2007-03-04 23:16:36 +0000 | [diff] [blame] | 1089 | CastInst *NewCast = new ZExtInst(I->getOperand(0), VTy, I->getName(), I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1090 | return UpdateValueUsesWith(I, NewCast); |
| 1091 | } else if (KnownOne & InSignBit) { // Input sign bit known set |
| 1092 | KnownOne |= NewBits; |
| 1093 | KnownZero &= ~NewBits; |
| 1094 | } else { // Input sign bit unknown |
| 1095 | KnownZero &= ~NewBits; |
| 1096 | KnownOne &= ~NewBits; |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 1097 | } |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1098 | break; |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 1099 | } |
Chris Lattner | b4a2f05 | 2006-11-09 05:12:27 +0000 | [diff] [blame] | 1100 | case Instruction::Add: |
| 1101 | // If there is a constant on the RHS, there are a variety of xformations |
| 1102 | // we can do. |
| 1103 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 1104 | // If null, this should be simplified elsewhere. Some of the xforms here |
| 1105 | // won't work if the RHS is zero. |
| 1106 | if (RHS->isNullValue()) |
| 1107 | break; |
| 1108 | |
| 1109 | // Figure out what the input bits are. If the top bits of the and result |
| 1110 | // are not demanded, then the add doesn't demand them from its input |
| 1111 | // either. |
| 1112 | |
| 1113 | // Shift the demanded mask up so that it's at the top of the uint64_t. |
Chris Lattner | d5fa214 | 2007-03-04 23:16:36 +0000 | [diff] [blame] | 1114 | unsigned BitWidth = VTy->getPrimitiveSizeInBits(); |
Chris Lattner | b4a2f05 | 2006-11-09 05:12:27 +0000 | [diff] [blame] | 1115 | unsigned NLZ = CountLeadingZeros_64(DemandedMask << (64-BitWidth)); |
| 1116 | |
| 1117 | // If the top bit of the output is demanded, demand everything from the |
| 1118 | // input. Otherwise, we demand all the input bits except NLZ top bits. |
Jeff Cohen | dfc1299 | 2007-01-08 20:17:17 +0000 | [diff] [blame] | 1119 | uint64_t InDemandedBits = ~0ULL >> (64-BitWidth+NLZ); |
Chris Lattner | b4a2f05 | 2006-11-09 05:12:27 +0000 | [diff] [blame] | 1120 | |
| 1121 | // Find information about known zero/one bits in the input. |
| 1122 | if (SimplifyDemandedBits(I->getOperand(0), InDemandedBits, |
| 1123 | KnownZero2, KnownOne2, Depth+1)) |
| 1124 | return true; |
| 1125 | |
| 1126 | // If the RHS of the add has bits set that can't affect the input, reduce |
| 1127 | // the constant. |
| 1128 | if (ShrinkDemandedConstant(I, 1, InDemandedBits)) |
| 1129 | return UpdateValueUsesWith(I, I); |
| 1130 | |
| 1131 | // Avoid excess work. |
| 1132 | if (KnownZero2 == 0 && KnownOne2 == 0) |
| 1133 | break; |
| 1134 | |
| 1135 | // Turn it into OR if input bits are zero. |
| 1136 | if ((KnownZero2 & RHS->getZExtValue()) == RHS->getZExtValue()) { |
| 1137 | Instruction *Or = |
| 1138 | BinaryOperator::createOr(I->getOperand(0), I->getOperand(1), |
| 1139 | I->getName()); |
| 1140 | InsertNewInstBefore(Or, *I); |
| 1141 | return UpdateValueUsesWith(I, Or); |
| 1142 | } |
| 1143 | |
| 1144 | // We can say something about the output known-zero and known-one bits, |
| 1145 | // depending on potential carries from the input constant and the |
| 1146 | // unknowns. For example if the LHS is known to have at most the 0x0F0F0 |
| 1147 | // bits set and the RHS constant is 0x01001, then we know we have a known |
| 1148 | // one mask of 0x00001 and a known zero mask of 0xE0F0E. |
| 1149 | |
| 1150 | // To compute this, we first compute the potential carry bits. These are |
| 1151 | // the bits which may be modified. I'm not aware of a better way to do |
| 1152 | // this scan. |
| 1153 | uint64_t RHSVal = RHS->getZExtValue(); |
| 1154 | |
| 1155 | bool CarryIn = false; |
| 1156 | uint64_t CarryBits = 0; |
| 1157 | uint64_t CurBit = 1; |
| 1158 | for (unsigned i = 0; i != BitWidth; ++i, CurBit <<= 1) { |
| 1159 | // Record the current carry in. |
| 1160 | if (CarryIn) CarryBits |= CurBit; |
| 1161 | |
| 1162 | bool CarryOut; |
| 1163 | |
| 1164 | // This bit has a carry out unless it is "zero + zero" or |
| 1165 | // "zero + anything" with no carry in. |
| 1166 | if ((KnownZero2 & CurBit) && ((RHSVal & CurBit) == 0)) { |
| 1167 | CarryOut = false; // 0 + 0 has no carry out, even with carry in. |
| 1168 | } else if (!CarryIn && |
| 1169 | ((KnownZero2 & CurBit) || ((RHSVal & CurBit) == 0))) { |
| 1170 | CarryOut = false; // 0 + anything has no carry out if no carry in. |
| 1171 | } else { |
| 1172 | // Otherwise, we have to assume we have a carry out. |
| 1173 | CarryOut = true; |
| 1174 | } |
| 1175 | |
| 1176 | // This stage's carry out becomes the next stage's carry-in. |
| 1177 | CarryIn = CarryOut; |
| 1178 | } |
| 1179 | |
| 1180 | // Now that we know which bits have carries, compute the known-1/0 sets. |
| 1181 | |
| 1182 | // Bits are known one if they are known zero in one operand and one in the |
| 1183 | // other, and there is no input carry. |
| 1184 | KnownOne = ((KnownZero2 & RHSVal) | (KnownOne2 & ~RHSVal)) & ~CarryBits; |
| 1185 | |
| 1186 | // Bits are known zero if they are known zero in both operands and there |
| 1187 | // is no input carry. |
| 1188 | KnownZero = KnownZero2 & ~RHSVal & ~CarryBits; |
Chris Lattner | 783ccdb | 2007-03-05 00:02:29 +0000 | [diff] [blame^] | 1189 | } else { |
| 1190 | // If the high-bits of this ADD are not demanded, then it does not demand |
| 1191 | // the high bits of its LHS or RHS. |
| 1192 | if ((DemandedMask & VTy->getSignBit()) == 0) { |
| 1193 | // Right fill the mask of bits for this ADD to demand the most |
| 1194 | // significant bit and all those below it. |
| 1195 | unsigned NLZ = CountLeadingZeros_64(DemandedMask); |
| 1196 | uint64_t DemandedFromOps = ~0ULL >> NLZ; |
| 1197 | if (SimplifyDemandedBits(I->getOperand(0), DemandedFromOps, |
| 1198 | KnownZero2, KnownOne2, Depth+1)) |
| 1199 | return true; |
| 1200 | if (SimplifyDemandedBits(I->getOperand(1), DemandedFromOps, |
| 1201 | KnownZero2, KnownOne2, Depth+1)) |
| 1202 | return true; |
| 1203 | } |
| 1204 | } |
| 1205 | break; |
| 1206 | case Instruction::Sub: |
| 1207 | // If the high-bits of this SUB are not demanded, then it does not demand |
| 1208 | // the high bits of its LHS or RHS. |
| 1209 | if ((DemandedMask & VTy->getSignBit()) == 0) { |
| 1210 | // Right fill the mask of bits for this SUB to demand the most |
| 1211 | // significant bit and all those below it. |
| 1212 | unsigned NLZ = CountLeadingZeros_64(DemandedMask); |
| 1213 | uint64_t DemandedFromOps = ~0ULL >> NLZ; |
| 1214 | if (SimplifyDemandedBits(I->getOperand(0), DemandedFromOps, |
| 1215 | KnownZero2, KnownOne2, Depth+1)) |
| 1216 | return true; |
| 1217 | if (SimplifyDemandedBits(I->getOperand(1), DemandedFromOps, |
| 1218 | KnownZero2, KnownOne2, Depth+1)) |
| 1219 | return true; |
Chris Lattner | b4a2f05 | 2006-11-09 05:12:27 +0000 | [diff] [blame] | 1220 | } |
| 1221 | break; |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 1222 | case Instruction::Shl: |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1223 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 1224 | uint64_t ShiftAmt = SA->getZExtValue(); |
| 1225 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask >> ShiftAmt, |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1226 | KnownZero, KnownOne, Depth+1)) |
| 1227 | return true; |
| 1228 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1229 | KnownZero <<= ShiftAmt; |
| 1230 | KnownOne <<= ShiftAmt; |
| 1231 | KnownZero |= (1ULL << ShiftAmt) - 1; // low bits known zero. |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1232 | } |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 1233 | break; |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1234 | case Instruction::LShr: |
| 1235 | // For a logical shift right |
| 1236 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 1237 | unsigned ShiftAmt = SA->getZExtValue(); |
| 1238 | |
| 1239 | // Compute the new bits that are at the top now. |
| 1240 | uint64_t HighBits = (1ULL << ShiftAmt)-1; |
Chris Lattner | d5fa214 | 2007-03-04 23:16:36 +0000 | [diff] [blame] | 1241 | HighBits <<= VTy->getBitWidth() - ShiftAmt; |
| 1242 | uint64_t TypeMask = VTy->getBitMask(); |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1243 | // Unsigned shift right. |
| 1244 | if (SimplifyDemandedBits(I->getOperand(0), |
| 1245 | (DemandedMask << ShiftAmt) & TypeMask, |
| 1246 | KnownZero, KnownOne, Depth+1)) |
| 1247 | return true; |
| 1248 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 1249 | KnownZero &= TypeMask; |
| 1250 | KnownOne &= TypeMask; |
| 1251 | KnownZero >>= ShiftAmt; |
| 1252 | KnownOne >>= ShiftAmt; |
| 1253 | KnownZero |= HighBits; // high bits known zero. |
| 1254 | } |
| 1255 | break; |
| 1256 | case Instruction::AShr: |
Chris Lattner | b736379 | 2006-09-18 04:31:40 +0000 | [diff] [blame] | 1257 | // If this is an arithmetic shift right and only the low-bit is set, we can |
| 1258 | // always convert this into a logical shr, even if the shift amount is |
| 1259 | // variable. The low bit of the shift cannot be an input sign bit unless |
| 1260 | // the shift amount is >= the size of the datatype, which is undefined. |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1261 | if (DemandedMask == 1) { |
| 1262 | // Perform the logical shift right. |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 1263 | Value *NewVal = BinaryOperator::createLShr( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1264 | I->getOperand(0), I->getOperand(1), I->getName()); |
Reid Spencer | 811b0cb | 2006-10-26 19:19:06 +0000 | [diff] [blame] | 1265 | InsertNewInstBefore(cast<Instruction>(NewVal), *I); |
Chris Lattner | b736379 | 2006-09-18 04:31:40 +0000 | [diff] [blame] | 1266 | return UpdateValueUsesWith(I, NewVal); |
| 1267 | } |
| 1268 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1269 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 1270 | unsigned ShiftAmt = SA->getZExtValue(); |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1271 | |
| 1272 | // Compute the new bits that are at the top now. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1273 | uint64_t HighBits = (1ULL << ShiftAmt)-1; |
Chris Lattner | d5fa214 | 2007-03-04 23:16:36 +0000 | [diff] [blame] | 1274 | HighBits <<= VTy->getBitWidth() - ShiftAmt; |
| 1275 | uint64_t TypeMask = VTy->getBitMask(); |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1276 | // Signed shift right. |
| 1277 | if (SimplifyDemandedBits(I->getOperand(0), |
| 1278 | (DemandedMask << ShiftAmt) & TypeMask, |
| 1279 | KnownZero, KnownOne, Depth+1)) |
| 1280 | return true; |
| 1281 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 1282 | KnownZero &= TypeMask; |
| 1283 | KnownOne &= TypeMask; |
| 1284 | KnownZero >>= ShiftAmt; |
| 1285 | KnownOne >>= ShiftAmt; |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1286 | |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1287 | // Handle the sign bits. |
Chris Lattner | d5fa214 | 2007-03-04 23:16:36 +0000 | [diff] [blame] | 1288 | uint64_t SignBit = 1ULL << (VTy->getBitWidth()-1); |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1289 | SignBit >>= ShiftAmt; // Adjust to where it is now in the mask. |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1290 | |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1291 | // If the input sign bit is known to be zero, or if none of the top bits |
| 1292 | // are demanded, turn this into an unsigned shift right. |
| 1293 | if ((KnownZero & SignBit) || (HighBits & ~DemandedMask) == HighBits) { |
| 1294 | // Perform the logical shift right. |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 1295 | Value *NewVal = BinaryOperator::createLShr( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1296 | I->getOperand(0), SA, I->getName()); |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1297 | InsertNewInstBefore(cast<Instruction>(NewVal), *I); |
| 1298 | return UpdateValueUsesWith(I, NewVal); |
| 1299 | } else if (KnownOne & SignBit) { // New bits are known one. |
| 1300 | KnownOne |= HighBits; |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 1301 | } |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1302 | } |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 1303 | break; |
| 1304 | } |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1305 | |
| 1306 | // If the client is only demanding bits that we know, return the known |
| 1307 | // constant. |
| 1308 | if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask) |
Chris Lattner | d5fa214 | 2007-03-04 23:16:36 +0000 | [diff] [blame] | 1309 | return UpdateValueUsesWith(I, ConstantInt::get(VTy, KnownOne)); |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 1310 | return false; |
| 1311 | } |
| 1312 | |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1313 | |
| 1314 | /// SimplifyDemandedVectorElts - The specified value producecs a vector with |
| 1315 | /// 64 or fewer elements. DemandedElts contains the set of elements that are |
| 1316 | /// actually used by the caller. This method analyzes which elements of the |
| 1317 | /// operand are undef and returns that information in UndefElts. |
| 1318 | /// |
| 1319 | /// If the information about demanded elements can be used to simplify the |
| 1320 | /// operation, the operation is simplified, then the resultant value is |
| 1321 | /// returned. This returns null if no change was made. |
| 1322 | Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, uint64_t DemandedElts, |
| 1323 | uint64_t &UndefElts, |
| 1324 | unsigned Depth) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1325 | unsigned VWidth = cast<VectorType>(V->getType())->getNumElements(); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1326 | assert(VWidth <= 64 && "Vector too wide to analyze!"); |
| 1327 | uint64_t EltMask = ~0ULL >> (64-VWidth); |
| 1328 | assert(DemandedElts != EltMask && (DemandedElts & ~EltMask) == 0 && |
| 1329 | "Invalid DemandedElts!"); |
| 1330 | |
| 1331 | if (isa<UndefValue>(V)) { |
| 1332 | // If the entire vector is undefined, just return this info. |
| 1333 | UndefElts = EltMask; |
| 1334 | return 0; |
| 1335 | } else if (DemandedElts == 0) { // If nothing is demanded, provide undef. |
| 1336 | UndefElts = EltMask; |
| 1337 | return UndefValue::get(V->getType()); |
| 1338 | } |
| 1339 | |
| 1340 | UndefElts = 0; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1341 | if (ConstantVector *CP = dyn_cast<ConstantVector>(V)) { |
| 1342 | const Type *EltTy = cast<VectorType>(V->getType())->getElementType(); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1343 | Constant *Undef = UndefValue::get(EltTy); |
| 1344 | |
| 1345 | std::vector<Constant*> Elts; |
| 1346 | for (unsigned i = 0; i != VWidth; ++i) |
| 1347 | if (!(DemandedElts & (1ULL << i))) { // If not demanded, set to undef. |
| 1348 | Elts.push_back(Undef); |
| 1349 | UndefElts |= (1ULL << i); |
| 1350 | } else if (isa<UndefValue>(CP->getOperand(i))) { // Already undef. |
| 1351 | Elts.push_back(Undef); |
| 1352 | UndefElts |= (1ULL << i); |
| 1353 | } else { // Otherwise, defined. |
| 1354 | Elts.push_back(CP->getOperand(i)); |
| 1355 | } |
| 1356 | |
| 1357 | // If we changed the constant, return it. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1358 | Constant *NewCP = ConstantVector::get(Elts); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1359 | return NewCP != CP ? NewCP : 0; |
| 1360 | } else if (isa<ConstantAggregateZero>(V)) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1361 | // Simplify the CAZ to a ConstantVector where the non-demanded elements are |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1362 | // set to undef. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1363 | const Type *EltTy = cast<VectorType>(V->getType())->getElementType(); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1364 | Constant *Zero = Constant::getNullValue(EltTy); |
| 1365 | Constant *Undef = UndefValue::get(EltTy); |
| 1366 | std::vector<Constant*> Elts; |
| 1367 | for (unsigned i = 0; i != VWidth; ++i) |
| 1368 | Elts.push_back((DemandedElts & (1ULL << i)) ? Zero : Undef); |
| 1369 | UndefElts = DemandedElts ^ EltMask; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1370 | return ConstantVector::get(Elts); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1371 | } |
| 1372 | |
| 1373 | if (!V->hasOneUse()) { // Other users may use these bits. |
| 1374 | if (Depth != 0) { // Not at the root. |
| 1375 | // TODO: Just compute the UndefElts information recursively. |
| 1376 | return false; |
| 1377 | } |
| 1378 | return false; |
| 1379 | } else if (Depth == 10) { // Limit search depth. |
| 1380 | return false; |
| 1381 | } |
| 1382 | |
| 1383 | Instruction *I = dyn_cast<Instruction>(V); |
| 1384 | if (!I) return false; // Only analyze instructions. |
| 1385 | |
| 1386 | bool MadeChange = false; |
| 1387 | uint64_t UndefElts2; |
| 1388 | Value *TmpV; |
| 1389 | switch (I->getOpcode()) { |
| 1390 | default: break; |
| 1391 | |
| 1392 | case Instruction::InsertElement: { |
| 1393 | // If this is a variable index, we don't know which element it overwrites. |
| 1394 | // demand exactly the same input as we produce. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1395 | ConstantInt *Idx = dyn_cast<ConstantInt>(I->getOperand(2)); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1396 | if (Idx == 0) { |
| 1397 | // Note that we can't propagate undef elt info, because we don't know |
| 1398 | // which elt is getting updated. |
| 1399 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, |
| 1400 | UndefElts2, Depth+1); |
| 1401 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } |
| 1402 | break; |
| 1403 | } |
| 1404 | |
| 1405 | // If this is inserting an element that isn't demanded, remove this |
| 1406 | // insertelement. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1407 | unsigned IdxNo = Idx->getZExtValue(); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1408 | if (IdxNo >= VWidth || (DemandedElts & (1ULL << IdxNo)) == 0) |
| 1409 | return AddSoonDeadInstToWorklist(*I, 0); |
| 1410 | |
| 1411 | // Otherwise, the element inserted overwrites whatever was there, so the |
| 1412 | // input demanded set is simpler than the output set. |
| 1413 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), |
| 1414 | DemandedElts & ~(1ULL << IdxNo), |
| 1415 | UndefElts, Depth+1); |
| 1416 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } |
| 1417 | |
| 1418 | // The inserted element is defined. |
| 1419 | UndefElts |= 1ULL << IdxNo; |
| 1420 | break; |
| 1421 | } |
| 1422 | |
| 1423 | case Instruction::And: |
| 1424 | case Instruction::Or: |
| 1425 | case Instruction::Xor: |
| 1426 | case Instruction::Add: |
| 1427 | case Instruction::Sub: |
| 1428 | case Instruction::Mul: |
| 1429 | // div/rem demand all inputs, because they don't want divide by zero. |
| 1430 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, |
| 1431 | UndefElts, Depth+1); |
| 1432 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } |
| 1433 | TmpV = SimplifyDemandedVectorElts(I->getOperand(1), DemandedElts, |
| 1434 | UndefElts2, Depth+1); |
| 1435 | if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; } |
| 1436 | |
| 1437 | // Output elements are undefined if both are undefined. Consider things |
| 1438 | // like undef&0. The result is known zero, not undef. |
| 1439 | UndefElts &= UndefElts2; |
| 1440 | break; |
| 1441 | |
| 1442 | case Instruction::Call: { |
| 1443 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(I); |
| 1444 | if (!II) break; |
| 1445 | switch (II->getIntrinsicID()) { |
| 1446 | default: break; |
| 1447 | |
| 1448 | // Binary vector operations that work column-wise. A dest element is a |
| 1449 | // function of the corresponding input elements from the two inputs. |
| 1450 | case Intrinsic::x86_sse_sub_ss: |
| 1451 | case Intrinsic::x86_sse_mul_ss: |
| 1452 | case Intrinsic::x86_sse_min_ss: |
| 1453 | case Intrinsic::x86_sse_max_ss: |
| 1454 | case Intrinsic::x86_sse2_sub_sd: |
| 1455 | case Intrinsic::x86_sse2_mul_sd: |
| 1456 | case Intrinsic::x86_sse2_min_sd: |
| 1457 | case Intrinsic::x86_sse2_max_sd: |
| 1458 | TmpV = SimplifyDemandedVectorElts(II->getOperand(1), DemandedElts, |
| 1459 | UndefElts, Depth+1); |
| 1460 | if (TmpV) { II->setOperand(1, TmpV); MadeChange = true; } |
| 1461 | TmpV = SimplifyDemandedVectorElts(II->getOperand(2), DemandedElts, |
| 1462 | UndefElts2, Depth+1); |
| 1463 | if (TmpV) { II->setOperand(2, TmpV); MadeChange = true; } |
| 1464 | |
| 1465 | // If only the low elt is demanded and this is a scalarizable intrinsic, |
| 1466 | // scalarize it now. |
| 1467 | if (DemandedElts == 1) { |
| 1468 | switch (II->getIntrinsicID()) { |
| 1469 | default: break; |
| 1470 | case Intrinsic::x86_sse_sub_ss: |
| 1471 | case Intrinsic::x86_sse_mul_ss: |
| 1472 | case Intrinsic::x86_sse2_sub_sd: |
| 1473 | case Intrinsic::x86_sse2_mul_sd: |
| 1474 | // TODO: Lower MIN/MAX/ABS/etc |
| 1475 | Value *LHS = II->getOperand(1); |
| 1476 | Value *RHS = II->getOperand(2); |
| 1477 | // Extract the element as scalars. |
| 1478 | LHS = InsertNewInstBefore(new ExtractElementInst(LHS, 0U,"tmp"), *II); |
| 1479 | RHS = InsertNewInstBefore(new ExtractElementInst(RHS, 0U,"tmp"), *II); |
| 1480 | |
| 1481 | switch (II->getIntrinsicID()) { |
| 1482 | default: assert(0 && "Case stmts out of sync!"); |
| 1483 | case Intrinsic::x86_sse_sub_ss: |
| 1484 | case Intrinsic::x86_sse2_sub_sd: |
| 1485 | TmpV = InsertNewInstBefore(BinaryOperator::createSub(LHS, RHS, |
| 1486 | II->getName()), *II); |
| 1487 | break; |
| 1488 | case Intrinsic::x86_sse_mul_ss: |
| 1489 | case Intrinsic::x86_sse2_mul_sd: |
| 1490 | TmpV = InsertNewInstBefore(BinaryOperator::createMul(LHS, RHS, |
| 1491 | II->getName()), *II); |
| 1492 | break; |
| 1493 | } |
| 1494 | |
| 1495 | Instruction *New = |
| 1496 | new InsertElementInst(UndefValue::get(II->getType()), TmpV, 0U, |
| 1497 | II->getName()); |
| 1498 | InsertNewInstBefore(New, *II); |
| 1499 | AddSoonDeadInstToWorklist(*II, 0); |
| 1500 | return New; |
| 1501 | } |
| 1502 | } |
| 1503 | |
| 1504 | // Output elements are undefined if both are undefined. Consider things |
| 1505 | // like undef&0. The result is known zero, not undef. |
| 1506 | UndefElts &= UndefElts2; |
| 1507 | break; |
| 1508 | } |
| 1509 | break; |
| 1510 | } |
| 1511 | } |
| 1512 | return MadeChange ? I : 0; |
| 1513 | } |
| 1514 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1515 | /// @returns true if the specified compare instruction is |
| 1516 | /// true when both operands are equal... |
| 1517 | /// @brief Determine if the ICmpInst returns true if both operands are equal |
| 1518 | static bool isTrueWhenEqual(ICmpInst &ICI) { |
| 1519 | ICmpInst::Predicate pred = ICI.getPredicate(); |
| 1520 | return pred == ICmpInst::ICMP_EQ || pred == ICmpInst::ICMP_UGE || |
| 1521 | pred == ICmpInst::ICMP_SGE || pred == ICmpInst::ICMP_ULE || |
| 1522 | pred == ICmpInst::ICMP_SLE; |
| 1523 | } |
| 1524 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1525 | /// AssociativeOpt - Perform an optimization on an associative operator. This |
| 1526 | /// function is designed to check a chain of associative operators for a |
| 1527 | /// potential to apply a certain optimization. Since the optimization may be |
| 1528 | /// applicable if the expression was reassociated, this checks the chain, then |
| 1529 | /// reassociates the expression as necessary to expose the optimization |
| 1530 | /// opportunity. This makes use of a special Functor, which must define |
| 1531 | /// 'shouldApply' and 'apply' methods. |
| 1532 | /// |
| 1533 | template<typename Functor> |
| 1534 | Instruction *AssociativeOpt(BinaryOperator &Root, const Functor &F) { |
| 1535 | unsigned Opcode = Root.getOpcode(); |
| 1536 | Value *LHS = Root.getOperand(0); |
| 1537 | |
| 1538 | // Quick check, see if the immediate LHS matches... |
| 1539 | if (F.shouldApply(LHS)) |
| 1540 | return F.apply(Root); |
| 1541 | |
| 1542 | // Otherwise, if the LHS is not of the same opcode as the root, return. |
| 1543 | Instruction *LHSI = dyn_cast<Instruction>(LHS); |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 1544 | while (LHSI && LHSI->getOpcode() == Opcode && LHSI->hasOneUse()) { |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1545 | // Should we apply this transform to the RHS? |
| 1546 | bool ShouldApply = F.shouldApply(LHSI->getOperand(1)); |
| 1547 | |
| 1548 | // If not to the RHS, check to see if we should apply to the LHS... |
| 1549 | if (!ShouldApply && F.shouldApply(LHSI->getOperand(0))) { |
| 1550 | cast<BinaryOperator>(LHSI)->swapOperands(); // Make the LHS the RHS |
| 1551 | ShouldApply = true; |
| 1552 | } |
| 1553 | |
| 1554 | // If the functor wants to apply the optimization to the RHS of LHSI, |
| 1555 | // reassociate the expression from ((? op A) op B) to (? op (A op B)) |
| 1556 | if (ShouldApply) { |
| 1557 | BasicBlock *BB = Root.getParent(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1558 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1559 | // Now all of the instructions are in the current basic block, go ahead |
| 1560 | // and perform the reassociation. |
| 1561 | Instruction *TmpLHSI = cast<Instruction>(Root.getOperand(0)); |
| 1562 | |
| 1563 | // First move the selected RHS to the LHS of the root... |
| 1564 | Root.setOperand(0, LHSI->getOperand(1)); |
| 1565 | |
| 1566 | // Make what used to be the LHS of the root be the user of the root... |
| 1567 | Value *ExtraOperand = TmpLHSI->getOperand(1); |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1568 | if (&Root == TmpLHSI) { |
Chris Lattner | 15a76c0 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 1569 | Root.replaceAllUsesWith(Constant::getNullValue(TmpLHSI->getType())); |
| 1570 | return 0; |
| 1571 | } |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1572 | Root.replaceAllUsesWith(TmpLHSI); // Users now use TmpLHSI |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1573 | TmpLHSI->setOperand(1, &Root); // TmpLHSI now uses the root |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1574 | TmpLHSI->getParent()->getInstList().remove(TmpLHSI); |
| 1575 | BasicBlock::iterator ARI = &Root; ++ARI; |
| 1576 | BB->getInstList().insert(ARI, TmpLHSI); // Move TmpLHSI to after Root |
| 1577 | ARI = Root; |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1578 | |
| 1579 | // Now propagate the ExtraOperand down the chain of instructions until we |
| 1580 | // get to LHSI. |
| 1581 | while (TmpLHSI != LHSI) { |
| 1582 | Instruction *NextLHSI = cast<Instruction>(TmpLHSI->getOperand(0)); |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1583 | // Move the instruction to immediately before the chain we are |
| 1584 | // constructing to avoid breaking dominance properties. |
| 1585 | NextLHSI->getParent()->getInstList().remove(NextLHSI); |
| 1586 | BB->getInstList().insert(ARI, NextLHSI); |
| 1587 | ARI = NextLHSI; |
| 1588 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1589 | Value *NextOp = NextLHSI->getOperand(1); |
| 1590 | NextLHSI->setOperand(1, ExtraOperand); |
| 1591 | TmpLHSI = NextLHSI; |
| 1592 | ExtraOperand = NextOp; |
| 1593 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1594 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1595 | // Now that the instructions are reassociated, have the functor perform |
| 1596 | // the transformation... |
| 1597 | return F.apply(Root); |
| 1598 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1599 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1600 | LHSI = dyn_cast<Instruction>(LHSI->getOperand(0)); |
| 1601 | } |
| 1602 | return 0; |
| 1603 | } |
| 1604 | |
| 1605 | |
| 1606 | // AddRHS - Implements: X + X --> X << 1 |
| 1607 | struct AddRHS { |
| 1608 | Value *RHS; |
| 1609 | AddRHS(Value *rhs) : RHS(rhs) {} |
| 1610 | bool shouldApply(Value *LHS) const { return LHS == RHS; } |
| 1611 | Instruction *apply(BinaryOperator &Add) const { |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 1612 | return BinaryOperator::createShl(Add.getOperand(0), |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1613 | ConstantInt::get(Add.getType(), 1)); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1614 | } |
| 1615 | }; |
| 1616 | |
| 1617 | // AddMaskingAnd - Implements (A & C1)+(B & C2) --> (A & C1)|(B & C2) |
| 1618 | // iff C1&C2 == 0 |
| 1619 | struct AddMaskingAnd { |
| 1620 | Constant *C2; |
| 1621 | AddMaskingAnd(Constant *c) : C2(c) {} |
| 1622 | bool shouldApply(Value *LHS) const { |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1623 | ConstantInt *C1; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1624 | return match(LHS, m_And(m_Value(), m_ConstantInt(C1))) && |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1625 | ConstantExpr::getAnd(C1, C2)->isNullValue(); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1626 | } |
| 1627 | Instruction *apply(BinaryOperator &Add) const { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1628 | return BinaryOperator::createOr(Add.getOperand(0), Add.getOperand(1)); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1629 | } |
| 1630 | }; |
| 1631 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1632 | static Value *FoldOperationIntoSelectOperand(Instruction &I, Value *SO, |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1633 | InstCombiner *IC) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1634 | if (CastInst *CI = dyn_cast<CastInst>(&I)) { |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1635 | if (Constant *SOC = dyn_cast<Constant>(SO)) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1636 | return ConstantExpr::getCast(CI->getOpcode(), SOC, I.getType()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1637 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1638 | return IC->InsertNewInstBefore(CastInst::create( |
| 1639 | CI->getOpcode(), SO, I.getType(), SO->getName() + ".cast"), I); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1640 | } |
| 1641 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1642 | // Figure out if the constant is the left or the right argument. |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1643 | bool ConstIsRHS = isa<Constant>(I.getOperand(1)); |
| 1644 | Constant *ConstOperand = cast<Constant>(I.getOperand(ConstIsRHS)); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1645 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1646 | if (Constant *SOC = dyn_cast<Constant>(SO)) { |
| 1647 | if (ConstIsRHS) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1648 | return ConstantExpr::get(I.getOpcode(), SOC, ConstOperand); |
| 1649 | return ConstantExpr::get(I.getOpcode(), ConstOperand, SOC); |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1650 | } |
| 1651 | |
| 1652 | Value *Op0 = SO, *Op1 = ConstOperand; |
| 1653 | if (!ConstIsRHS) |
| 1654 | std::swap(Op0, Op1); |
| 1655 | Instruction *New; |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1656 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I)) |
| 1657 | New = BinaryOperator::create(BO->getOpcode(), Op0, Op1,SO->getName()+".op"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1658 | else if (CmpInst *CI = dyn_cast<CmpInst>(&I)) |
| 1659 | New = CmpInst::create(CI->getOpcode(), CI->getPredicate(), Op0, Op1, |
| 1660 | SO->getName()+".cmp"); |
Chris Lattner | 326c0f3 | 2004-04-10 19:15:56 +0000 | [diff] [blame] | 1661 | else { |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1662 | assert(0 && "Unknown binary instruction type!"); |
Chris Lattner | 326c0f3 | 2004-04-10 19:15:56 +0000 | [diff] [blame] | 1663 | abort(); |
| 1664 | } |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1665 | return IC->InsertNewInstBefore(New, I); |
| 1666 | } |
| 1667 | |
| 1668 | // FoldOpIntoSelect - Given an instruction with a select as one operand and a |
| 1669 | // constant as the other operand, try to fold the binary operator into the |
| 1670 | // select arguments. This also works for Cast instructions, which obviously do |
| 1671 | // not have a second operand. |
| 1672 | static Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI, |
| 1673 | InstCombiner *IC) { |
| 1674 | // Don't modify shared select instructions |
| 1675 | if (!SI->hasOneUse()) return 0; |
| 1676 | Value *TV = SI->getOperand(1); |
| 1677 | Value *FV = SI->getOperand(2); |
| 1678 | |
| 1679 | if (isa<Constant>(TV) || isa<Constant>(FV)) { |
Chris Lattner | 956db27 | 2005-04-21 05:43:13 +0000 | [diff] [blame] | 1680 | // Bool selects with constant operands can be folded to logical ops. |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 1681 | if (SI->getType() == Type::Int1Ty) return 0; |
Chris Lattner | 956db27 | 2005-04-21 05:43:13 +0000 | [diff] [blame] | 1682 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1683 | Value *SelectTrueVal = FoldOperationIntoSelectOperand(Op, TV, IC); |
| 1684 | Value *SelectFalseVal = FoldOperationIntoSelectOperand(Op, FV, IC); |
| 1685 | |
| 1686 | return new SelectInst(SI->getCondition(), SelectTrueVal, |
| 1687 | SelectFalseVal); |
| 1688 | } |
| 1689 | return 0; |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1690 | } |
| 1691 | |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1692 | |
| 1693 | /// FoldOpIntoPhi - Given a binary operator or cast instruction which has a PHI |
| 1694 | /// node as operand #0, see if we can fold the instruction into the PHI (which |
| 1695 | /// is only possible if all operands to the PHI are constants). |
| 1696 | Instruction *InstCombiner::FoldOpIntoPhi(Instruction &I) { |
| 1697 | PHINode *PN = cast<PHINode>(I.getOperand(0)); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 1698 | unsigned NumPHIValues = PN->getNumIncomingValues(); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1699 | if (!PN->hasOneUse() || NumPHIValues == 0) return 0; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1700 | |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1701 | // Check to see if all of the operands of the PHI are constants. If there is |
| 1702 | // 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] | 1703 | // or if *it* is a PHI, bail out. |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1704 | BasicBlock *NonConstBB = 0; |
| 1705 | for (unsigned i = 0; i != NumPHIValues; ++i) |
| 1706 | if (!isa<Constant>(PN->getIncomingValue(i))) { |
| 1707 | if (NonConstBB) return 0; // More than one non-const value. |
Chris Lattner | b303668 | 2007-02-24 01:03:45 +0000 | [diff] [blame] | 1708 | if (isa<PHINode>(PN->getIncomingValue(i))) return 0; // Itself a phi. |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1709 | NonConstBB = PN->getIncomingBlock(i); |
| 1710 | |
| 1711 | // If the incoming non-constant value is in I's block, we have an infinite |
| 1712 | // loop. |
| 1713 | if (NonConstBB == I.getParent()) |
| 1714 | return 0; |
| 1715 | } |
| 1716 | |
| 1717 | // If there is exactly one non-constant value, we can insert a copy of the |
| 1718 | // operation in that block. However, if this is a critical edge, we would be |
| 1719 | // inserting the computation one some other paths (e.g. inside a loop). Only |
| 1720 | // do this if the pred block is unconditionally branching into the phi block. |
| 1721 | if (NonConstBB) { |
| 1722 | BranchInst *BI = dyn_cast<BranchInst>(NonConstBB->getTerminator()); |
| 1723 | if (!BI || !BI->isUnconditional()) return 0; |
| 1724 | } |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1725 | |
| 1726 | // Okay, we can do the transformation: create the new PHI node. |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 1727 | PHINode *NewPN = new PHINode(I.getType(), ""); |
Chris Lattner | 5551706 | 2005-01-29 00:39:08 +0000 | [diff] [blame] | 1728 | NewPN->reserveOperandSpace(PN->getNumOperands()/2); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1729 | InsertNewInstBefore(NewPN, *PN); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 1730 | NewPN->takeName(PN); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1731 | |
| 1732 | // Next, add all of the operands to the PHI. |
| 1733 | if (I.getNumOperands() == 2) { |
| 1734 | Constant *C = cast<Constant>(I.getOperand(1)); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 1735 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1736 | Value *InV; |
| 1737 | if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1738 | if (CmpInst *CI = dyn_cast<CmpInst>(&I)) |
| 1739 | InV = ConstantExpr::getCompare(CI->getPredicate(), InC, C); |
| 1740 | else |
| 1741 | InV = ConstantExpr::get(I.getOpcode(), InC, C); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1742 | } else { |
| 1743 | assert(PN->getIncomingBlock(i) == NonConstBB); |
| 1744 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I)) |
| 1745 | InV = BinaryOperator::create(BO->getOpcode(), |
| 1746 | PN->getIncomingValue(i), C, "phitmp", |
| 1747 | NonConstBB->getTerminator()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1748 | else if (CmpInst *CI = dyn_cast<CmpInst>(&I)) |
| 1749 | InV = CmpInst::create(CI->getOpcode(), |
| 1750 | CI->getPredicate(), |
| 1751 | PN->getIncomingValue(i), C, "phitmp", |
| 1752 | NonConstBB->getTerminator()); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1753 | else |
| 1754 | assert(0 && "Unknown binop!"); |
| 1755 | |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 1756 | AddToWorkList(cast<Instruction>(InV)); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1757 | } |
| 1758 | NewPN->addIncoming(InV, PN->getIncomingBlock(i)); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1759 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1760 | } else { |
| 1761 | CastInst *CI = cast<CastInst>(&I); |
| 1762 | const Type *RetTy = CI->getType(); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 1763 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1764 | Value *InV; |
| 1765 | if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1766 | InV = ConstantExpr::getCast(CI->getOpcode(), InC, RetTy); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1767 | } else { |
| 1768 | assert(PN->getIncomingBlock(i) == NonConstBB); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1769 | InV = CastInst::create(CI->getOpcode(), PN->getIncomingValue(i), |
| 1770 | I.getType(), "phitmp", |
| 1771 | NonConstBB->getTerminator()); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 1772 | AddToWorkList(cast<Instruction>(InV)); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1773 | } |
| 1774 | NewPN->addIncoming(InV, PN->getIncomingBlock(i)); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1775 | } |
| 1776 | } |
| 1777 | return ReplaceInstUsesWith(I, NewPN); |
| 1778 | } |
| 1779 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1780 | Instruction *InstCombiner::visitAdd(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 1781 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1782 | Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 1783 | |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 1784 | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 1785 | // X + undef -> undef |
| 1786 | if (isa<UndefValue>(RHS)) |
| 1787 | return ReplaceInstUsesWith(I, RHS); |
| 1788 | |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 1789 | // X + 0 --> X |
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 1790 | if (!I.getType()->isFPOrFPVector()) { // NOTE: -0 + +0 = +0. |
Chris Lattner | 5e678e0 | 2005-10-17 17:56:38 +0000 | [diff] [blame] | 1791 | if (RHSC->isNullValue()) |
| 1792 | return ReplaceInstUsesWith(I, LHS); |
Chris Lattner | 8532cf6 | 2005-10-17 20:18:38 +0000 | [diff] [blame] | 1793 | } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) { |
| 1794 | if (CFP->isExactlyValue(-0.0)) |
| 1795 | return ReplaceInstUsesWith(I, LHS); |
Chris Lattner | 5e678e0 | 2005-10-17 17:56:38 +0000 | [diff] [blame] | 1796 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1797 | |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 1798 | if (ConstantInt *CI = dyn_cast<ConstantInt>(RHSC)) { |
Chris Lattner | b4a2f05 | 2006-11-09 05:12:27 +0000 | [diff] [blame] | 1799 | // X + (signbit) --> X ^ signbit |
Chris Lattner | 74c51a0 | 2006-02-07 08:05:22 +0000 | [diff] [blame] | 1800 | uint64_t Val = CI->getZExtValue(); |
Chris Lattner | 1a074fc | 2006-02-07 07:00:41 +0000 | [diff] [blame] | 1801 | if (Val == (1ULL << (CI->getType()->getPrimitiveSizeInBits()-1))) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1802 | return BinaryOperator::createXor(LHS, RHS); |
Chris Lattner | b4a2f05 | 2006-11-09 05:12:27 +0000 | [diff] [blame] | 1803 | |
| 1804 | // See if SimplifyDemandedBits can simplify this. This handles stuff like |
| 1805 | // (X & 254)+1 -> (X&254)|1 |
| 1806 | uint64_t KnownZero, KnownOne; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1807 | if (!isa<VectorType>(I.getType()) && |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 1808 | SimplifyDemandedBits(&I, cast<IntegerType>(I.getType())->getBitMask(), |
Chris Lattner | b4a2f05 | 2006-11-09 05:12:27 +0000 | [diff] [blame] | 1809 | KnownZero, KnownOne)) |
| 1810 | return &I; |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 1811 | } |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1812 | |
| 1813 | if (isa<PHINode>(LHS)) |
| 1814 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 1815 | return NV; |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1816 | |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 1817 | ConstantInt *XorRHS = 0; |
| 1818 | Value *XorLHS = 0; |
Chris Lattner | c5eff44 | 2007-01-30 22:32:46 +0000 | [diff] [blame] | 1819 | if (isa<ConstantInt>(RHSC) && |
| 1820 | match(LHS, m_Xor(m_Value(XorLHS), m_ConstantInt(XorRHS)))) { |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1821 | unsigned TySizeBits = I.getType()->getPrimitiveSizeInBits(); |
| 1822 | int64_t RHSSExt = cast<ConstantInt>(RHSC)->getSExtValue(); |
| 1823 | uint64_t RHSZExt = cast<ConstantInt>(RHSC)->getZExtValue(); |
| 1824 | |
| 1825 | uint64_t C0080Val = 1ULL << 31; |
| 1826 | int64_t CFF80Val = -C0080Val; |
| 1827 | unsigned Size = 32; |
| 1828 | do { |
| 1829 | if (TySizeBits > Size) { |
| 1830 | bool Found = false; |
| 1831 | // If we have ADD(XOR(AND(X, 0xFF), 0x80), 0xF..F80), it's a sext. |
| 1832 | // If we have ADD(XOR(AND(X, 0xFF), 0xF..F80), 0x80), it's a sext. |
| 1833 | if (RHSSExt == CFF80Val) { |
| 1834 | if (XorRHS->getZExtValue() == C0080Val) |
| 1835 | Found = true; |
| 1836 | } else if (RHSZExt == C0080Val) { |
| 1837 | if (XorRHS->getSExtValue() == CFF80Val) |
| 1838 | Found = true; |
| 1839 | } |
| 1840 | if (Found) { |
| 1841 | // This is a sign extend if the top bits are known zero. |
Chris Lattner | 68d5ff2 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 1842 | uint64_t Mask = ~0ULL; |
Chris Lattner | 3bedbd9 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 1843 | Mask <<= 64-(TySizeBits-Size); |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 1844 | Mask &= cast<IntegerType>(XorLHS->getType())->getBitMask(); |
Chris Lattner | 3bedbd9 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 1845 | if (!MaskedValueIsZero(XorLHS, Mask)) |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1846 | Size = 0; // Not a sign ext, but can't be any others either. |
| 1847 | goto FoundSExt; |
| 1848 | } |
| 1849 | } |
| 1850 | Size >>= 1; |
| 1851 | C0080Val >>= Size; |
| 1852 | CFF80Val >>= Size; |
| 1853 | } while (Size >= 8); |
| 1854 | |
| 1855 | FoundSExt: |
| 1856 | const Type *MiddleType = 0; |
| 1857 | switch (Size) { |
| 1858 | default: break; |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 1859 | case 32: MiddleType = Type::Int32Ty; break; |
| 1860 | case 16: MiddleType = Type::Int16Ty; break; |
| 1861 | case 8: MiddleType = Type::Int8Ty; break; |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1862 | } |
| 1863 | if (MiddleType) { |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 1864 | Instruction *NewTrunc = new TruncInst(XorLHS, MiddleType, "sext"); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1865 | InsertNewInstBefore(NewTrunc, I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1866 | return new SExtInst(NewTrunc, I.getType()); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1867 | } |
| 1868 | } |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 1869 | } |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 1870 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1871 | // X + X --> X << 1 |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1872 | if (I.getType()->isInteger() && I.getType() != Type::Int1Ty) { |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1873 | if (Instruction *Result = AssociativeOpt(I, AddRHS(RHS))) return Result; |
Chris Lattner | 7edc8c2 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 1874 | |
| 1875 | if (Instruction *RHSI = dyn_cast<Instruction>(RHS)) { |
| 1876 | if (RHSI->getOpcode() == Instruction::Sub) |
| 1877 | if (LHS == RHSI->getOperand(1)) // A + (B - A) --> B |
| 1878 | return ReplaceInstUsesWith(I, RHSI->getOperand(0)); |
| 1879 | } |
| 1880 | if (Instruction *LHSI = dyn_cast<Instruction>(LHS)) { |
| 1881 | if (LHSI->getOpcode() == Instruction::Sub) |
| 1882 | if (RHS == LHSI->getOperand(1)) // (B - A) + A --> B |
| 1883 | return ReplaceInstUsesWith(I, LHSI->getOperand(0)); |
| 1884 | } |
Robert Bocchino | 7169828 | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 1885 | } |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 1886 | |
Chris Lattner | 5c4afb9 | 2002-05-08 22:46:53 +0000 | [diff] [blame] | 1887 | // -A + B --> B - A |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 1888 | if (Value *V = dyn_castNegVal(LHS)) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1889 | return BinaryOperator::createSub(RHS, V); |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 1890 | |
| 1891 | // A + -B --> A - B |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 1892 | if (!isa<Constant>(RHS)) |
| 1893 | if (Value *V = dyn_castNegVal(RHS)) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1894 | return BinaryOperator::createSub(LHS, V); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1895 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1896 | |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 1897 | ConstantInt *C2; |
| 1898 | if (Value *X = dyn_castFoldableMul(LHS, C2)) { |
| 1899 | if (X == RHS) // X*C + X --> X * (C+1) |
| 1900 | return BinaryOperator::createMul(RHS, AddOne(C2)); |
| 1901 | |
| 1902 | // X*C1 + X*C2 --> X * (C1+C2) |
| 1903 | ConstantInt *C1; |
| 1904 | if (X == dyn_castFoldableMul(RHS, C1)) |
| 1905 | return BinaryOperator::createMul(X, ConstantExpr::getAdd(C1, C2)); |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 1906 | } |
| 1907 | |
| 1908 | // X + X*C --> X * (C+1) |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 1909 | if (dyn_castFoldableMul(RHS, C2) == LHS) |
| 1910 | return BinaryOperator::createMul(LHS, AddOne(C2)); |
| 1911 | |
Chris Lattner | e617c9e | 2007-01-05 02:17:46 +0000 | [diff] [blame] | 1912 | // X + ~X --> -1 since ~X = -X-1 |
| 1913 | if (dyn_castNotVal(LHS) == RHS || |
| 1914 | dyn_castNotVal(RHS) == LHS) |
| 1915 | return ReplaceInstUsesWith(I, ConstantInt::getAllOnesValue(I.getType())); |
| 1916 | |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 1917 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1918 | // (A & C1)+(B & C2) --> (A & C1)|(B & C2) iff C1&C2 == 0 |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1919 | if (match(RHS, m_And(m_Value(), m_ConstantInt(C2)))) |
Chris Lattner | e617c9e | 2007-01-05 02:17:46 +0000 | [diff] [blame] | 1920 | if (Instruction *R = AssociativeOpt(I, AddMaskingAnd(C2))) |
| 1921 | return R; |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 1922 | |
Chris Lattner | 6b03205 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 1923 | if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) { |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 1924 | Value *X = 0; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1925 | if (match(LHS, m_Not(m_Value(X)))) { // ~X + C --> (C-1) - X |
| 1926 | Constant *C= ConstantExpr::getSub(CRHS, ConstantInt::get(I.getType(), 1)); |
| 1927 | return BinaryOperator::createSub(C, X); |
Chris Lattner | 6b03205 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 1928 | } |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1929 | |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 1930 | // (X & FF00) + xx00 -> (X+xx00) & FF00 |
| 1931 | if (LHS->hasOneUse() && match(LHS, m_And(m_Value(X), m_ConstantInt(C2)))) { |
| 1932 | Constant *Anded = ConstantExpr::getAnd(CRHS, C2); |
| 1933 | if (Anded == CRHS) { |
| 1934 | // See if all bits from the first bit set in the Add RHS up are included |
| 1935 | // in the mask. First, get the rightmost bit. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1936 | uint64_t AddRHSV = CRHS->getZExtValue(); |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 1937 | |
| 1938 | // Form a mask of all bits from the lowest bit added through the top. |
| 1939 | uint64_t AddRHSHighBits = ~((AddRHSV & -AddRHSV)-1); |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 1940 | AddRHSHighBits &= C2->getType()->getBitMask(); |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 1941 | |
| 1942 | // See if the and mask includes all of these bits. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1943 | uint64_t AddRHSHighBitsAnd = AddRHSHighBits & C2->getZExtValue(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1944 | |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 1945 | if (AddRHSHighBits == AddRHSHighBitsAnd) { |
| 1946 | // Okay, the xform is safe. Insert the new add pronto. |
| 1947 | Value *NewAdd = InsertNewInstBefore(BinaryOperator::createAdd(X, CRHS, |
| 1948 | LHS->getName()), I); |
| 1949 | return BinaryOperator::createAnd(NewAdd, C2); |
| 1950 | } |
| 1951 | } |
| 1952 | } |
| 1953 | |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1954 | // Try to fold constant add into select arguments. |
| 1955 | if (SelectInst *SI = dyn_cast<SelectInst>(LHS)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1956 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1957 | return R; |
Chris Lattner | 6b03205 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 1958 | } |
| 1959 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1960 | // add (cast *A to intptrtype) B -> |
| 1961 | // cast (GEP (cast *A to sbyte*) B) -> |
| 1962 | // intptrtype |
Andrew Lenharth | 16d7955 | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 1963 | { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1964 | CastInst *CI = dyn_cast<CastInst>(LHS); |
| 1965 | Value *Other = RHS; |
Andrew Lenharth | 16d7955 | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 1966 | if (!CI) { |
| 1967 | CI = dyn_cast<CastInst>(RHS); |
| 1968 | Other = LHS; |
| 1969 | } |
Andrew Lenharth | 4563326 | 2006-09-20 15:37:57 +0000 | [diff] [blame] | 1970 | if (CI && CI->getType()->isSized() && |
Reid Spencer | abaa8ca | 2007-01-08 16:32:00 +0000 | [diff] [blame] | 1971 | (CI->getType()->getPrimitiveSizeInBits() == |
| 1972 | TD->getIntPtrType()->getPrimitiveSizeInBits()) |
Andrew Lenharth | 4563326 | 2006-09-20 15:37:57 +0000 | [diff] [blame] | 1973 | && isa<PointerType>(CI->getOperand(0)->getType())) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 1974 | Value *I2 = InsertCastBefore(Instruction::BitCast, CI->getOperand(0), |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 1975 | PointerType::get(Type::Int8Ty), I); |
Andrew Lenharth | 4563326 | 2006-09-20 15:37:57 +0000 | [diff] [blame] | 1976 | I2 = InsertNewInstBefore(new GetElementPtrInst(I2, Other, "ctg2"), I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1977 | return new PtrToIntInst(I2, CI->getType()); |
Andrew Lenharth | 16d7955 | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 1978 | } |
| 1979 | } |
| 1980 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1981 | return Changed ? &I : 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1982 | } |
| 1983 | |
Chris Lattner | 1ba5bcd | 2003-07-22 21:46:59 +0000 | [diff] [blame] | 1984 | // isSignBit - Return true if the value represented by the constant only has the |
| 1985 | // highest order bit set. |
| 1986 | static bool isSignBit(ConstantInt *CI) { |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 1987 | unsigned NumBits = CI->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1988 | return (CI->getZExtValue() & (~0ULL >> (64-NumBits))) == (1ULL << (NumBits-1)); |
Chris Lattner | 1ba5bcd | 2003-07-22 21:46:59 +0000 | [diff] [blame] | 1989 | } |
| 1990 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1991 | Instruction *InstCombiner::visitSub(BinaryOperator &I) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1992 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1993 | |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1994 | if (Op0 == Op1) // sub X, X -> 0 |
| 1995 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1996 | |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1997 | // If this is a 'B = x-(-A)', change to B = x+A... |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 1998 | if (Value *V = dyn_castNegVal(Op1)) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1999 | return BinaryOperator::createAdd(Op0, V); |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 2000 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2001 | if (isa<UndefValue>(Op0)) |
| 2002 | return ReplaceInstUsesWith(I, Op0); // undef - X -> undef |
| 2003 | if (isa<UndefValue>(Op1)) |
| 2004 | return ReplaceInstUsesWith(I, Op1); // X - undef -> undef |
| 2005 | |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 2006 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op0)) { |
| 2007 | // Replace (-1 - A) with (~A)... |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2008 | if (C->isAllOnesValue()) |
| 2009 | return BinaryOperator::createNot(Op1); |
Chris Lattner | 4037171 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 2010 | |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 2011 | // C - ~X == X + (1+C) |
Reid Spencer | 4b828e6 | 2005-06-18 17:37:34 +0000 | [diff] [blame] | 2012 | Value *X = 0; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2013 | if (match(Op1, m_Not(m_Value(X)))) |
| 2014 | return BinaryOperator::createAdd(X, |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2015 | ConstantExpr::getAdd(C, ConstantInt::get(I.getType(), 1))); |
Chris Lattner | 76b7a06 | 2007-01-15 07:02:54 +0000 | [diff] [blame] | 2016 | // -(X >>u 31) -> (X >>s 31) |
| 2017 | // -(X >>s 31) -> (X >>u 31) |
Chris Lattner | bfe492b | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 2018 | if (C->isNullValue()) { |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2019 | if (BinaryOperator *SI = dyn_cast<BinaryOperator>(Op1)) |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2020 | if (SI->getOpcode() == Instruction::LShr) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2021 | if (ConstantInt *CU = dyn_cast<ConstantInt>(SI->getOperand(1))) { |
Chris Lattner | 9c29067 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 2022 | // Check to see if we are shifting out everything but the sign bit. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2023 | if (CU->getZExtValue() == |
| 2024 | SI->getType()->getPrimitiveSizeInBits()-1) { |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2025 | // Ok, the transformation is safe. Insert AShr. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2026 | return BinaryOperator::create(Instruction::AShr, |
| 2027 | SI->getOperand(0), CU, SI->getName()); |
Chris Lattner | 9c29067 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 2028 | } |
| 2029 | } |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2030 | } |
| 2031 | else if (SI->getOpcode() == Instruction::AShr) { |
| 2032 | if (ConstantInt *CU = dyn_cast<ConstantInt>(SI->getOperand(1))) { |
| 2033 | // Check to see if we are shifting out everything but the sign bit. |
| 2034 | if (CU->getZExtValue() == |
| 2035 | SI->getType()->getPrimitiveSizeInBits()-1) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 2036 | // Ok, the transformation is safe. Insert LShr. |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 2037 | return BinaryOperator::createLShr( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2038 | SI->getOperand(0), CU, SI->getName()); |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2039 | } |
| 2040 | } |
| 2041 | } |
Chris Lattner | bfe492b | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 2042 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2043 | |
| 2044 | // Try to fold constant sub into select arguments. |
| 2045 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2046 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2047 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2048 | |
| 2049 | if (isa<PHINode>(Op0)) |
| 2050 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2051 | return NV; |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 2052 | } |
| 2053 | |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2054 | if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) { |
| 2055 | if (Op1I->getOpcode() == Instruction::Add && |
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 2056 | !Op0->getType()->isFPOrFPVector()) { |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2057 | if (Op1I->getOperand(0) == Op0) // X-(X+Y) == -Y |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2058 | return BinaryOperator::createNeg(Op1I->getOperand(1), I.getName()); |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2059 | else if (Op1I->getOperand(1) == Op0) // X-(Y+X) == -Y |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2060 | return BinaryOperator::createNeg(Op1I->getOperand(0), I.getName()); |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2061 | else if (ConstantInt *CI1 = dyn_cast<ConstantInt>(I.getOperand(0))) { |
| 2062 | if (ConstantInt *CI2 = dyn_cast<ConstantInt>(Op1I->getOperand(1))) |
| 2063 | // C1-(X+C2) --> (C1-C2)-X |
| 2064 | return BinaryOperator::createSub(ConstantExpr::getSub(CI1, CI2), |
| 2065 | Op1I->getOperand(0)); |
| 2066 | } |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2067 | } |
| 2068 | |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 2069 | if (Op1I->hasOneUse()) { |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2070 | // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression |
| 2071 | // is not used by anyone else... |
| 2072 | // |
Chris Lattner | 0517e72 | 2004-02-02 20:09:56 +0000 | [diff] [blame] | 2073 | if (Op1I->getOpcode() == Instruction::Sub && |
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 2074 | !Op1I->getType()->isFPOrFPVector()) { |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2075 | // Swap the two operands of the subexpr... |
| 2076 | Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1); |
| 2077 | Op1I->setOperand(0, IIOp1); |
| 2078 | Op1I->setOperand(1, IIOp0); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2079 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2080 | // Create the new top level add instruction... |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2081 | return BinaryOperator::createAdd(Op0, Op1); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2082 | } |
| 2083 | |
| 2084 | // Replace (A - (A & B)) with (A & ~B) if this is the only use of (A&B)... |
| 2085 | // |
| 2086 | if (Op1I->getOpcode() == Instruction::And && |
| 2087 | (Op1I->getOperand(0) == Op0 || Op1I->getOperand(1) == Op0)) { |
| 2088 | Value *OtherOp = Op1I->getOperand(Op1I->getOperand(0) == Op0); |
| 2089 | |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 2090 | Value *NewNot = |
| 2091 | InsertNewInstBefore(BinaryOperator::createNot(OtherOp, "B.not"), I); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2092 | return BinaryOperator::createAnd(Op0, NewNot); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2093 | } |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2094 | |
Reid Spencer | ac5209e | 2006-10-16 23:08:08 +0000 | [diff] [blame] | 2095 | // 0 - (X sdiv C) -> (X sdiv -C) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2096 | if (Op1I->getOpcode() == Instruction::SDiv) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2097 | if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0)) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2098 | if (CSI->isNullValue()) |
Chris Lattner | 91ccc15 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 2099 | if (Constant *DivRHS = dyn_cast<Constant>(Op1I->getOperand(1))) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2100 | return BinaryOperator::createSDiv(Op1I->getOperand(0), |
Chris Lattner | 91ccc15 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 2101 | ConstantExpr::getNeg(DivRHS)); |
| 2102 | |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2103 | // X - X*C --> X * (1-C) |
Reid Spencer | 4b828e6 | 2005-06-18 17:37:34 +0000 | [diff] [blame] | 2104 | ConstantInt *C2 = 0; |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2105 | if (dyn_castFoldableMul(Op1I, C2) == Op0) { |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2106 | Constant *CP1 = |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2107 | ConstantExpr::getSub(ConstantInt::get(I.getType(), 1), C2); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2108 | return BinaryOperator::createMul(Op0, CP1); |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2109 | } |
Chris Lattner | 4037171 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 2110 | } |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2111 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2112 | |
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 2113 | if (!Op0->getType()->isFPOrFPVector()) |
Chris Lattner | 7edc8c2 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 2114 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) |
| 2115 | if (Op0I->getOpcode() == Instruction::Add) { |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 2116 | if (Op0I->getOperand(0) == Op1) // (Y+X)-Y == X |
| 2117 | return ReplaceInstUsesWith(I, Op0I->getOperand(1)); |
| 2118 | else if (Op0I->getOperand(1) == Op1) // (X+Y)-Y == X |
| 2119 | return ReplaceInstUsesWith(I, Op0I->getOperand(0)); |
Chris Lattner | 7edc8c2 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 2120 | } else if (Op0I->getOpcode() == Instruction::Sub) { |
| 2121 | if (Op0I->getOperand(0) == Op1) // (X-Y)-X == -Y |
| 2122 | return BinaryOperator::createNeg(Op0I->getOperand(1), I.getName()); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 2123 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2124 | |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2125 | ConstantInt *C1; |
| 2126 | if (Value *X = dyn_castFoldableMul(Op0, C1)) { |
| 2127 | if (X == Op1) { // X*C - X --> X * (C-1) |
| 2128 | Constant *CP1 = ConstantExpr::getSub(C1, ConstantInt::get(I.getType(),1)); |
| 2129 | return BinaryOperator::createMul(Op1, CP1); |
| 2130 | } |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2131 | |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2132 | ConstantInt *C2; // X*C1 - X*C2 -> X * (C1-C2) |
| 2133 | if (X == dyn_castFoldableMul(Op1, C2)) |
| 2134 | return BinaryOperator::createMul(Op1, ConstantExpr::getSub(C1, C2)); |
| 2135 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2136 | return 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2137 | } |
| 2138 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2139 | /// isSignBitCheck - Given an exploded icmp instruction, return true if it |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2140 | /// really just returns true if the most significant (sign) bit is set. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2141 | static bool isSignBitCheck(ICmpInst::Predicate pred, ConstantInt *RHS) { |
| 2142 | switch (pred) { |
| 2143 | case ICmpInst::ICMP_SLT: |
| 2144 | // True if LHS s< RHS and RHS == 0 |
| 2145 | return RHS->isNullValue(); |
| 2146 | case ICmpInst::ICMP_SLE: |
| 2147 | // True if LHS s<= RHS and RHS == -1 |
| 2148 | return RHS->isAllOnesValue(); |
| 2149 | case ICmpInst::ICMP_UGE: |
| 2150 | // True if LHS u>= RHS and RHS == high-bit-mask (2^7, 2^15, 2^31, etc) |
| 2151 | return RHS->getZExtValue() == (1ULL << |
| 2152 | (RHS->getType()->getPrimitiveSizeInBits()-1)); |
| 2153 | case ICmpInst::ICMP_UGT: |
| 2154 | // True if LHS u> RHS and RHS == high-bit-mask - 1 |
| 2155 | return RHS->getZExtValue() == |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 2156 | (1ULL << (RHS->getType()->getPrimitiveSizeInBits()-1))-1; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2157 | default: |
| 2158 | return false; |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2159 | } |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2160 | } |
| 2161 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2162 | Instruction *InstCombiner::visitMul(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 2163 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2164 | Value *Op0 = I.getOperand(0); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2165 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2166 | if (isa<UndefValue>(I.getOperand(1))) // undef * X -> 0 |
| 2167 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2168 | |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2169 | // Simplify mul instructions with a constant RHS... |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2170 | if (Constant *Op1 = dyn_cast<Constant>(I.getOperand(1))) { |
| 2171 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 2172 | |
| 2173 | // ((X << C1)*C2) == (X * (C2 << C1)) |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2174 | if (BinaryOperator *SI = dyn_cast<BinaryOperator>(Op0)) |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 2175 | if (SI->getOpcode() == Instruction::Shl) |
| 2176 | if (Constant *ShOp = dyn_cast<Constant>(SI->getOperand(1))) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2177 | return BinaryOperator::createMul(SI->getOperand(0), |
| 2178 | ConstantExpr::getShl(CI, ShOp)); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2179 | |
Chris Lattner | 515c97c | 2003-09-11 22:24:54 +0000 | [diff] [blame] | 2180 | if (CI->isNullValue()) |
| 2181 | return ReplaceInstUsesWith(I, Op1); // X * 0 == 0 |
| 2182 | if (CI->equalsInt(1)) // X * 1 == X |
| 2183 | return ReplaceInstUsesWith(I, Op0); |
| 2184 | if (CI->isAllOnesValue()) // X * -1 == 0 - X |
Chris Lattner | 0af1fab | 2003-06-25 17:09:20 +0000 | [diff] [blame] | 2185 | return BinaryOperator::createNeg(Op0, I.getName()); |
Chris Lattner | 6c1ce21 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 2186 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2187 | int64_t Val = (int64_t)cast<ConstantInt>(CI)->getZExtValue(); |
Chris Lattner | bcd7db5 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 2188 | if (isPowerOf2_64(Val)) { // Replace X*(2^C) with X << C |
| 2189 | uint64_t C = Log2_64(Val); |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 2190 | return BinaryOperator::createShl(Op0, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2191 | ConstantInt::get(Op0->getType(), C)); |
Chris Lattner | bcd7db5 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 2192 | } |
Robert Bocchino | 7169828 | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 2193 | } else if (ConstantFP *Op1F = dyn_cast<ConstantFP>(Op1)) { |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2194 | if (Op1F->isNullValue()) |
| 2195 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 6c1ce21 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 2196 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2197 | // "In IEEE floating point, x*1 is not equivalent to x for nans. However, |
| 2198 | // ANSI says we can drop signals, so we can do this anyway." (from GCC) |
| 2199 | if (Op1F->getValue() == 1.0) |
| 2200 | return ReplaceInstUsesWith(I, Op0); // Eliminate 'mul double %X, 1.0' |
| 2201 | } |
Chris Lattner | ab51f3f | 2006-03-04 06:04:02 +0000 | [diff] [blame] | 2202 | |
| 2203 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) |
| 2204 | if (Op0I->getOpcode() == Instruction::Add && Op0I->hasOneUse() && |
| 2205 | isa<ConstantInt>(Op0I->getOperand(1))) { |
| 2206 | // Canonicalize (X+C1)*C2 -> X*C2+C1*C2. |
| 2207 | Instruction *Add = BinaryOperator::createMul(Op0I->getOperand(0), |
| 2208 | Op1, "tmp"); |
| 2209 | InsertNewInstBefore(Add, I); |
| 2210 | Value *C1C2 = ConstantExpr::getMul(Op1, |
| 2211 | cast<Constant>(Op0I->getOperand(1))); |
| 2212 | return BinaryOperator::createAdd(Add, C1C2); |
| 2213 | |
| 2214 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2215 | |
| 2216 | // Try to fold constant mul into select arguments. |
| 2217 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2218 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2219 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2220 | |
| 2221 | if (isa<PHINode>(Op0)) |
| 2222 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2223 | return NV; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2224 | } |
| 2225 | |
Chris Lattner | a4f445b | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 2226 | if (Value *Op0v = dyn_castNegVal(Op0)) // -X * -Y = X*Y |
| 2227 | if (Value *Op1v = dyn_castNegVal(I.getOperand(1))) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2228 | return BinaryOperator::createMul(Op0v, Op1v); |
Chris Lattner | a4f445b | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 2229 | |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2230 | // If one of the operands of the multiply is a cast from a boolean value, then |
| 2231 | // we know the bool is either zero or one, so this is a 'masking' multiply. |
| 2232 | // See if we can simplify things based on how the boolean was originally |
| 2233 | // formed. |
| 2234 | CastInst *BoolCast = 0; |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 2235 | if (ZExtInst *CI = dyn_cast<ZExtInst>(I.getOperand(0))) |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 2236 | if (CI->getOperand(0)->getType() == Type::Int1Ty) |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2237 | BoolCast = CI; |
| 2238 | if (!BoolCast) |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 2239 | if (ZExtInst *CI = dyn_cast<ZExtInst>(I.getOperand(1))) |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 2240 | if (CI->getOperand(0)->getType() == Type::Int1Ty) |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2241 | BoolCast = CI; |
| 2242 | if (BoolCast) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2243 | if (ICmpInst *SCI = dyn_cast<ICmpInst>(BoolCast->getOperand(0))) { |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2244 | Value *SCIOp0 = SCI->getOperand(0), *SCIOp1 = SCI->getOperand(1); |
| 2245 | const Type *SCOpTy = SCIOp0->getType(); |
| 2246 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2247 | // 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] | 2248 | // multiply into a shift/and combination. |
| 2249 | if (isa<ConstantInt>(SCIOp1) && |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2250 | isSignBitCheck(SCI->getPredicate(), cast<ConstantInt>(SCIOp1))) { |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2251 | // Shift the X value right to turn it into "all signbits". |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2252 | Constant *Amt = ConstantInt::get(SCIOp0->getType(), |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 2253 | SCOpTy->getPrimitiveSizeInBits()-1); |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2254 | Value *V = |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2255 | InsertNewInstBefore( |
| 2256 | BinaryOperator::create(Instruction::AShr, SCIOp0, Amt, |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2257 | BoolCast->getOperand(0)->getName()+ |
| 2258 | ".mask"), I); |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2259 | |
| 2260 | // If the multiply type is not the same as the source type, sign extend |
| 2261 | // or truncate to the multiply type. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 2262 | if (I.getType() != V->getType()) { |
| 2263 | unsigned SrcBits = V->getType()->getPrimitiveSizeInBits(); |
| 2264 | unsigned DstBits = I.getType()->getPrimitiveSizeInBits(); |
| 2265 | Instruction::CastOps opcode = |
| 2266 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2267 | (SrcBits < DstBits ? Instruction::SExt : Instruction::Trunc)); |
| 2268 | V = InsertCastBefore(opcode, V, I.getType(), I); |
| 2269 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2270 | |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2271 | Value *OtherOp = Op0 == BoolCast ? I.getOperand(1) : Op0; |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2272 | return BinaryOperator::createAnd(V, OtherOp); |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2273 | } |
| 2274 | } |
| 2275 | } |
| 2276 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2277 | return Changed ? &I : 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2278 | } |
| 2279 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2280 | /// This function implements the transforms on div instructions that work |
| 2281 | /// regardless of the kind of div instruction it is (udiv, sdiv, or fdiv). It is |
| 2282 | /// used by the visitors to those instructions. |
| 2283 | /// @brief Transforms common to all three div instructions |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2284 | Instruction *InstCombiner::commonDivTransforms(BinaryOperator &I) { |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2285 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2286 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2287 | // undef / X -> 0 |
| 2288 | if (isa<UndefValue>(Op0)) |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2289 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2290 | |
| 2291 | // X / undef -> undef |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2292 | if (isa<UndefValue>(Op1)) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2293 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2294 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2295 | // Handle cases involving: div X, (select Cond, Y, Z) |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2296 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { |
| 2297 | // 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] | 2298 | // same basic block, then we replace the select with Y, and the condition |
| 2299 | // 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] | 2300 | // 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] | 2301 | // 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] | 2302 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(1))) |
| 2303 | if (ST->isNullValue()) { |
| 2304 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); |
| 2305 | if (CondI && CondI->getParent() == I.getParent()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2306 | UpdateValueUsesWith(CondI, ConstantInt::getFalse()); |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2307 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
| 2308 | I.setOperand(1, SI->getOperand(2)); |
| 2309 | else |
| 2310 | UpdateValueUsesWith(SI, SI->getOperand(2)); |
| 2311 | return &I; |
| 2312 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2313 | |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2314 | // Likewise for: div X, (Cond ? Y : 0) -> div X, Y |
| 2315 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(2))) |
| 2316 | if (ST->isNullValue()) { |
| 2317 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); |
| 2318 | if (CondI && CondI->getParent() == I.getParent()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2319 | UpdateValueUsesWith(CondI, ConstantInt::getTrue()); |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2320 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
| 2321 | I.setOperand(1, SI->getOperand(1)); |
| 2322 | else |
| 2323 | UpdateValueUsesWith(SI, SI->getOperand(1)); |
| 2324 | return &I; |
| 2325 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2326 | } |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2327 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2328 | return 0; |
| 2329 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2330 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2331 | /// This function implements the transforms common to both integer division |
| 2332 | /// instructions (udiv and sdiv). It is called by the visitors to those integer |
| 2333 | /// division instructions. |
| 2334 | /// @brief Common integer divide transforms |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2335 | Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2336 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2337 | |
| 2338 | if (Instruction *Common = commonDivTransforms(I)) |
| 2339 | return Common; |
| 2340 | |
| 2341 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
| 2342 | // div X, 1 == X |
| 2343 | if (RHS->equalsInt(1)) |
| 2344 | return ReplaceInstUsesWith(I, Op0); |
| 2345 | |
| 2346 | // (X / C1) / C2 -> X / (C1*C2) |
| 2347 | if (Instruction *LHS = dyn_cast<Instruction>(Op0)) |
| 2348 | if (Instruction::BinaryOps(LHS->getOpcode()) == I.getOpcode()) |
| 2349 | if (ConstantInt *LHSRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) { |
| 2350 | return BinaryOperator::create(I.getOpcode(), LHS->getOperand(0), |
| 2351 | ConstantExpr::getMul(RHS, LHSRHS)); |
Chris Lattner | bf70b83 | 2005-04-08 04:03:26 +0000 | [diff] [blame] | 2352 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2353 | |
| 2354 | if (!RHS->isNullValue()) { // avoid X udiv 0 |
| 2355 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 2356 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 2357 | return R; |
| 2358 | if (isa<PHINode>(Op0)) |
| 2359 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2360 | return NV; |
| 2361 | } |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2362 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2363 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2364 | // 0 / X == 0, we don't need to preserve faults! |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2365 | if (ConstantInt *LHS = dyn_cast<ConstantInt>(Op0)) |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2366 | if (LHS->equalsInt(0)) |
| 2367 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2368 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2369 | return 0; |
| 2370 | } |
| 2371 | |
| 2372 | Instruction *InstCombiner::visitUDiv(BinaryOperator &I) { |
| 2373 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2374 | |
| 2375 | // Handle the integer div common cases |
| 2376 | if (Instruction *Common = commonIDivTransforms(I)) |
| 2377 | return Common; |
| 2378 | |
| 2379 | // X udiv C^2 -> X >> C |
| 2380 | // Check to see if this is an unsigned division with an exact power of 2, |
| 2381 | // if so, convert to a right shift. |
| 2382 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op1)) { |
| 2383 | if (uint64_t Val = C->getZExtValue()) // Don't break X / 0 |
| 2384 | if (isPowerOf2_64(Val)) { |
| 2385 | uint64_t ShiftAmt = Log2_64(Val); |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 2386 | return BinaryOperator::createLShr(Op0, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2387 | ConstantInt::get(Op0->getType(), ShiftAmt)); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2388 | } |
| 2389 | } |
| 2390 | |
| 2391 | // X udiv (C1 << N), where C1 is "1<<C2" --> X >> (N+C2) |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2392 | if (BinaryOperator *RHSI = dyn_cast<BinaryOperator>(I.getOperand(1))) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2393 | if (RHSI->getOpcode() == Instruction::Shl && |
| 2394 | isa<ConstantInt>(RHSI->getOperand(0))) { |
| 2395 | uint64_t C1 = cast<ConstantInt>(RHSI->getOperand(0))->getZExtValue(); |
| 2396 | if (isPowerOf2_64(C1)) { |
| 2397 | Value *N = RHSI->getOperand(1); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2398 | const Type *NTy = N->getType(); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2399 | if (uint64_t C2 = Log2_64(C1)) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2400 | Constant *C2V = ConstantInt::get(NTy, C2); |
| 2401 | N = InsertNewInstBefore(BinaryOperator::createAdd(N, C2V, "tmp"), I); |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2402 | } |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 2403 | return BinaryOperator::createLShr(Op0, N); |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2404 | } |
| 2405 | } |
Chris Lattner | c812e5d | 2005-11-05 07:40:31 +0000 | [diff] [blame] | 2406 | } |
| 2407 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2408 | // udiv X, (Select Cond, C1, C2) --> Select Cond, (shr X, C1), (shr X, C2) |
| 2409 | // where C1&C2 are powers of two. |
| 2410 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { |
| 2411 | if (ConstantInt *STO = dyn_cast<ConstantInt>(SI->getOperand(1))) |
| 2412 | if (ConstantInt *SFO = dyn_cast<ConstantInt>(SI->getOperand(2))) |
| 2413 | if (!STO->isNullValue() && !STO->isNullValue()) { |
| 2414 | uint64_t TVA = STO->getZExtValue(), FVA = SFO->getZExtValue(); |
| 2415 | if (isPowerOf2_64(TVA) && isPowerOf2_64(FVA)) { |
| 2416 | // Compute the shift amounts |
| 2417 | unsigned TSA = Log2_64(TVA), FSA = Log2_64(FVA); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2418 | // Construct the "on true" case of the select |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2419 | Constant *TC = ConstantInt::get(Op0->getType(), TSA); |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 2420 | Instruction *TSI = BinaryOperator::createLShr( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2421 | Op0, TC, SI->getName()+".t"); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2422 | TSI = InsertNewInstBefore(TSI, I); |
| 2423 | |
| 2424 | // Construct the "on false" case of the select |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2425 | Constant *FC = ConstantInt::get(Op0->getType(), FSA); |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 2426 | Instruction *FSI = BinaryOperator::createLShr( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2427 | Op0, FC, SI->getName()+".f"); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2428 | FSI = InsertNewInstBefore(FSI, I); |
| 2429 | |
| 2430 | // construct the select instruction and return it. |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2431 | return new SelectInst(SI->getOperand(0), TSI, FSI, SI->getName()); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2432 | } |
| 2433 | } |
| 2434 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2435 | return 0; |
| 2436 | } |
| 2437 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2438 | Instruction *InstCombiner::visitSDiv(BinaryOperator &I) { |
| 2439 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2440 | |
| 2441 | // Handle the integer div common cases |
| 2442 | if (Instruction *Common = commonIDivTransforms(I)) |
| 2443 | return Common; |
| 2444 | |
| 2445 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
| 2446 | // sdiv X, -1 == -X |
| 2447 | if (RHS->isAllOnesValue()) |
| 2448 | return BinaryOperator::createNeg(Op0); |
| 2449 | |
| 2450 | // -X/C -> X/-C |
| 2451 | if (Value *LHSNeg = dyn_castNegVal(Op0)) |
| 2452 | return BinaryOperator::createSDiv(LHSNeg, ConstantExpr::getNeg(RHS)); |
| 2453 | } |
| 2454 | |
| 2455 | // If the sign bits of both operands are zero (i.e. we can prove they are |
| 2456 | // unsigned inputs), turn this into a udiv. |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 2457 | if (I.getType()->isInteger()) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2458 | uint64_t Mask = 1ULL << (I.getType()->getPrimitiveSizeInBits()-1); |
| 2459 | if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) { |
| 2460 | return BinaryOperator::createUDiv(Op0, Op1, I.getName()); |
| 2461 | } |
| 2462 | } |
| 2463 | |
| 2464 | return 0; |
| 2465 | } |
| 2466 | |
| 2467 | Instruction *InstCombiner::visitFDiv(BinaryOperator &I) { |
| 2468 | return commonDivTransforms(I); |
| 2469 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2470 | |
Chris Lattner | db3f873 | 2006-03-02 06:50:58 +0000 | [diff] [blame] | 2471 | /// GetFactor - If we can prove that the specified value is at least a multiple |
| 2472 | /// of some factor, return that factor. |
| 2473 | static Constant *GetFactor(Value *V) { |
| 2474 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) |
| 2475 | return CI; |
| 2476 | |
| 2477 | // Unless we can be tricky, we know this is a multiple of 1. |
| 2478 | Constant *Result = ConstantInt::get(V->getType(), 1); |
| 2479 | |
| 2480 | Instruction *I = dyn_cast<Instruction>(V); |
| 2481 | if (!I) return Result; |
| 2482 | |
| 2483 | if (I->getOpcode() == Instruction::Mul) { |
| 2484 | // Handle multiplies by a constant, etc. |
| 2485 | return ConstantExpr::getMul(GetFactor(I->getOperand(0)), |
| 2486 | GetFactor(I->getOperand(1))); |
| 2487 | } else if (I->getOpcode() == Instruction::Shl) { |
| 2488 | // (X<<C) -> X * (1 << C) |
| 2489 | if (Constant *ShRHS = dyn_cast<Constant>(I->getOperand(1))) { |
| 2490 | ShRHS = ConstantExpr::getShl(Result, ShRHS); |
| 2491 | return ConstantExpr::getMul(GetFactor(I->getOperand(0)), ShRHS); |
| 2492 | } |
| 2493 | } else if (I->getOpcode() == Instruction::And) { |
| 2494 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 2495 | // X & 0xFFF0 is known to be a multiple of 16. |
| 2496 | unsigned Zeros = CountTrailingZeros_64(RHS->getZExtValue()); |
| 2497 | if (Zeros != V->getType()->getPrimitiveSizeInBits()) |
| 2498 | return ConstantExpr::getShl(Result, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2499 | ConstantInt::get(Result->getType(), Zeros)); |
Chris Lattner | db3f873 | 2006-03-02 06:50:58 +0000 | [diff] [blame] | 2500 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2501 | } else if (CastInst *CI = dyn_cast<CastInst>(I)) { |
Chris Lattner | db3f873 | 2006-03-02 06:50:58 +0000 | [diff] [blame] | 2502 | // Only handle int->int casts. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2503 | if (!CI->isIntegerCast()) |
| 2504 | return Result; |
| 2505 | Value *Op = CI->getOperand(0); |
| 2506 | return ConstantExpr::getCast(CI->getOpcode(), GetFactor(Op), V->getType()); |
Chris Lattner | db3f873 | 2006-03-02 06:50:58 +0000 | [diff] [blame] | 2507 | } |
| 2508 | return Result; |
| 2509 | } |
| 2510 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2511 | /// This function implements the transforms on rem instructions that work |
| 2512 | /// regardless of the kind of rem instruction it is (urem, srem, or frem). It |
| 2513 | /// is used by the visitors to those instructions. |
| 2514 | /// @brief Transforms common to all three rem instructions |
| 2515 | Instruction *InstCombiner::commonRemTransforms(BinaryOperator &I) { |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2516 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2517 | |
Chris Lattner | 19ccd5c | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 2518 | // 0 % X == 0, we don't need to preserve faults! |
| 2519 | if (Constant *LHS = dyn_cast<Constant>(Op0)) |
| 2520 | if (LHS->isNullValue()) |
| 2521 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2522 | |
| 2523 | if (isa<UndefValue>(Op0)) // undef % X -> 0 |
| 2524 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2525 | if (isa<UndefValue>(Op1)) |
| 2526 | return ReplaceInstUsesWith(I, Op1); // X % undef -> undef |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2527 | |
| 2528 | // Handle cases involving: rem X, (select Cond, Y, Z) |
| 2529 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { |
| 2530 | // rem X, (Cond ? 0 : Y) -> rem X, Y. If the rem and the select are in |
| 2531 | // the same basic block, then we replace the select with Y, and the |
| 2532 | // condition of the select with false (if the cond value is in the same |
| 2533 | // BB). If the select has uses other than the div, this allows them to be |
| 2534 | // simplified also. |
| 2535 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(1))) |
| 2536 | if (ST->isNullValue()) { |
| 2537 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); |
| 2538 | if (CondI && CondI->getParent() == I.getParent()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2539 | UpdateValueUsesWith(CondI, ConstantInt::getFalse()); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2540 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
| 2541 | I.setOperand(1, SI->getOperand(2)); |
| 2542 | else |
| 2543 | UpdateValueUsesWith(SI, SI->getOperand(2)); |
Chris Lattner | 5b73c08 | 2004-07-06 07:01:22 +0000 | [diff] [blame] | 2544 | return &I; |
| 2545 | } |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2546 | // Likewise for: rem X, (Cond ? Y : 0) -> rem X, Y |
| 2547 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(2))) |
| 2548 | if (ST->isNullValue()) { |
| 2549 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); |
| 2550 | if (CondI && CondI->getParent() == I.getParent()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2551 | UpdateValueUsesWith(CondI, ConstantInt::getTrue()); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2552 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
| 2553 | I.setOperand(1, SI->getOperand(1)); |
| 2554 | else |
| 2555 | UpdateValueUsesWith(SI, SI->getOperand(1)); |
| 2556 | return &I; |
| 2557 | } |
Chris Lattner | 11a49f2 | 2005-11-05 07:28:37 +0000 | [diff] [blame] | 2558 | } |
Chris Lattner | 5b73c08 | 2004-07-06 07:01:22 +0000 | [diff] [blame] | 2559 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2560 | return 0; |
| 2561 | } |
| 2562 | |
| 2563 | /// This function implements the transforms common to both integer remainder |
| 2564 | /// instructions (urem and srem). It is called by the visitors to those integer |
| 2565 | /// remainder instructions. |
| 2566 | /// @brief Common integer remainder transforms |
| 2567 | Instruction *InstCombiner::commonIRemTransforms(BinaryOperator &I) { |
| 2568 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2569 | |
| 2570 | if (Instruction *common = commonRemTransforms(I)) |
| 2571 | return common; |
| 2572 | |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2573 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 19ccd5c | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 2574 | // X % 0 == undef, we don't need to preserve faults! |
| 2575 | if (RHS->equalsInt(0)) |
| 2576 | return ReplaceInstUsesWith(I, UndefValue::get(I.getType())); |
| 2577 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2578 | if (RHS->equalsInt(1)) // X % 1 == 0 |
| 2579 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2580 | |
Chris Lattner | 9794392 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 2581 | if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) { |
| 2582 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0I)) { |
| 2583 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 2584 | return R; |
| 2585 | } else if (isa<PHINode>(Op0I)) { |
| 2586 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2587 | return NV; |
Chris Lattner | 9794392 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 2588 | } |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2589 | // (X * C1) % C2 --> 0 iff C1 % C2 == 0 |
| 2590 | if (ConstantExpr::getSRem(GetFactor(Op0I), RHS)->isNullValue()) |
Chris Lattner | db3f873 | 2006-03-02 06:50:58 +0000 | [diff] [blame] | 2591 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | 9794392 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 2592 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2593 | } |
| 2594 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2595 | return 0; |
| 2596 | } |
| 2597 | |
| 2598 | Instruction *InstCombiner::visitURem(BinaryOperator &I) { |
| 2599 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2600 | |
| 2601 | if (Instruction *common = commonIRemTransforms(I)) |
| 2602 | return common; |
| 2603 | |
| 2604 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
| 2605 | // X urem C^2 -> X and C |
| 2606 | // Check to see if this is an unsigned remainder with an exact power of 2, |
| 2607 | // if so, convert to a bitwise and. |
| 2608 | if (ConstantInt *C = dyn_cast<ConstantInt>(RHS)) |
| 2609 | if (isPowerOf2_64(C->getZExtValue())) |
| 2610 | return BinaryOperator::createAnd(Op0, SubOne(C)); |
| 2611 | } |
| 2612 | |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2613 | if (Instruction *RHSI = dyn_cast<Instruction>(I.getOperand(1))) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2614 | // Turn A % (C << N), where C is 2^k, into A & ((C << N)-1) |
| 2615 | if (RHSI->getOpcode() == Instruction::Shl && |
| 2616 | isa<ConstantInt>(RHSI->getOperand(0))) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2617 | unsigned C1 = cast<ConstantInt>(RHSI->getOperand(0))->getZExtValue(); |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2618 | if (isPowerOf2_64(C1)) { |
| 2619 | Constant *N1 = ConstantInt::getAllOnesValue(I.getType()); |
| 2620 | Value *Add = InsertNewInstBefore(BinaryOperator::createAdd(RHSI, N1, |
| 2621 | "tmp"), I); |
| 2622 | return BinaryOperator::createAnd(Op0, Add); |
| 2623 | } |
| 2624 | } |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2625 | } |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2626 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2627 | // urem X, (select Cond, 2^C1, 2^C2) --> select Cond, (and X, C1), (and X, C2) |
| 2628 | // where C1&C2 are powers of two. |
| 2629 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { |
| 2630 | if (ConstantInt *STO = dyn_cast<ConstantInt>(SI->getOperand(1))) |
| 2631 | if (ConstantInt *SFO = dyn_cast<ConstantInt>(SI->getOperand(2))) { |
| 2632 | // STO == 0 and SFO == 0 handled above. |
| 2633 | if (isPowerOf2_64(STO->getZExtValue()) && |
| 2634 | isPowerOf2_64(SFO->getZExtValue())) { |
| 2635 | Value *TrueAnd = InsertNewInstBefore( |
| 2636 | BinaryOperator::createAnd(Op0, SubOne(STO), SI->getName()+".t"), I); |
| 2637 | Value *FalseAnd = InsertNewInstBefore( |
| 2638 | BinaryOperator::createAnd(Op0, SubOne(SFO), SI->getName()+".f"), I); |
| 2639 | return new SelectInst(SI->getOperand(0), TrueAnd, FalseAnd); |
| 2640 | } |
| 2641 | } |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2642 | } |
| 2643 | |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2644 | return 0; |
| 2645 | } |
| 2646 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2647 | Instruction *InstCombiner::visitSRem(BinaryOperator &I) { |
| 2648 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2649 | |
| 2650 | if (Instruction *common = commonIRemTransforms(I)) |
| 2651 | return common; |
| 2652 | |
| 2653 | if (Value *RHSNeg = dyn_castNegVal(Op1)) |
| 2654 | if (!isa<ConstantInt>(RHSNeg) || |
| 2655 | cast<ConstantInt>(RHSNeg)->getSExtValue() > 0) { |
| 2656 | // X % -Y -> X % Y |
| 2657 | AddUsesToWorkList(I); |
| 2658 | I.setOperand(1, RHSNeg); |
| 2659 | return &I; |
| 2660 | } |
| 2661 | |
| 2662 | // If the top bits of both operands are zero (i.e. we can prove they are |
| 2663 | // unsigned inputs), turn this into a urem. |
| 2664 | uint64_t Mask = 1ULL << (I.getType()->getPrimitiveSizeInBits()-1); |
| 2665 | if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) { |
| 2666 | // X srem Y -> X urem Y, iff X and Y don't have sign bit set |
| 2667 | return BinaryOperator::createURem(Op0, Op1, I.getName()); |
| 2668 | } |
| 2669 | |
| 2670 | return 0; |
| 2671 | } |
| 2672 | |
| 2673 | Instruction *InstCombiner::visitFRem(BinaryOperator &I) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2674 | return commonRemTransforms(I); |
| 2675 | } |
| 2676 | |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2677 | // isMaxValueMinusOne - return true if this is Max-1 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2678 | static bool isMaxValueMinusOne(const ConstantInt *C, bool isSigned) { |
| 2679 | if (isSigned) { |
| 2680 | // Calculate 0111111111..11111 |
| 2681 | unsigned TypeBits = C->getType()->getPrimitiveSizeInBits(); |
| 2682 | int64_t Val = INT64_MAX; // All ones |
| 2683 | Val >>= 64-TypeBits; // Shift out unwanted 1 bits... |
| 2684 | return C->getSExtValue() == Val-1; |
| 2685 | } |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 2686 | return C->getZExtValue() == C->getType()->getBitMask()-1; |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2687 | } |
| 2688 | |
| 2689 | // isMinValuePlusOne - return true if this is Min+1 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2690 | static bool isMinValuePlusOne(const ConstantInt *C, bool isSigned) { |
| 2691 | if (isSigned) { |
| 2692 | // Calculate 1111111111000000000000 |
| 2693 | unsigned TypeBits = C->getType()->getPrimitiveSizeInBits(); |
| 2694 | int64_t Val = -1; // All ones |
| 2695 | Val <<= TypeBits-1; // Shift over to the right spot |
| 2696 | return C->getSExtValue() == Val+1; |
| 2697 | } |
| 2698 | return C->getZExtValue() == 1; // unsigned |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2699 | } |
| 2700 | |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2701 | // isOneBitSet - Return true if there is exactly one bit set in the specified |
| 2702 | // constant. |
| 2703 | static bool isOneBitSet(const ConstantInt *CI) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2704 | uint64_t V = CI->getZExtValue(); |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2705 | return V && (V & (V-1)) == 0; |
| 2706 | } |
| 2707 | |
Chris Lattner | b20ba0a | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2708 | #if 0 // Currently unused |
| 2709 | // isLowOnes - Return true if the constant is of the form 0+1+. |
| 2710 | static bool isLowOnes(const ConstantInt *CI) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2711 | uint64_t V = CI->getZExtValue(); |
Chris Lattner | b20ba0a | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2712 | |
| 2713 | // There won't be bits set in parts that the type doesn't contain. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2714 | V &= ConstantInt::getAllOnesValue(CI->getType())->getZExtValue(); |
Chris Lattner | b20ba0a | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2715 | |
| 2716 | uint64_t U = V+1; // If it is low ones, this should be a power of two. |
| 2717 | return U && V && (U & V) == 0; |
| 2718 | } |
| 2719 | #endif |
| 2720 | |
| 2721 | // isHighOnes - Return true if the constant is of the form 1+0+. |
| 2722 | // This is the same as lowones(~X). |
| 2723 | static bool isHighOnes(const ConstantInt *CI) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2724 | uint64_t V = ~CI->getZExtValue(); |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 2725 | if (~V == 0) return false; // 0's does not match "1+" |
Chris Lattner | b20ba0a | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2726 | |
| 2727 | // There won't be bits set in parts that the type doesn't contain. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2728 | V &= ConstantInt::getAllOnesValue(CI->getType())->getZExtValue(); |
Chris Lattner | b20ba0a | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2729 | |
| 2730 | uint64_t U = V+1; // If it is low ones, this should be a power of two. |
| 2731 | return U && V && (U & V) == 0; |
| 2732 | } |
| 2733 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2734 | /// getICmpCode - Encode a icmp predicate into a three bit mask. These bits |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2735 | /// are carefully arranged to allow folding of expressions such as: |
| 2736 | /// |
| 2737 | /// (A < B) | (A > B) --> (A != B) |
| 2738 | /// |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2739 | /// Note that this is only valid if the first and second predicates have the |
| 2740 | /// same sign. Is illegal to do: (A u< B) | (A s> B) |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2741 | /// |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2742 | /// Three bits are used to represent the condition, as follows: |
| 2743 | /// 0 A > B |
| 2744 | /// 1 A == B |
| 2745 | /// 2 A < B |
| 2746 | /// |
| 2747 | /// <=> Value Definition |
| 2748 | /// 000 0 Always false |
| 2749 | /// 001 1 A > B |
| 2750 | /// 010 2 A == B |
| 2751 | /// 011 3 A >= B |
| 2752 | /// 100 4 A < B |
| 2753 | /// 101 5 A != B |
| 2754 | /// 110 6 A <= B |
| 2755 | /// 111 7 Always true |
| 2756 | /// |
| 2757 | static unsigned getICmpCode(const ICmpInst *ICI) { |
| 2758 | switch (ICI->getPredicate()) { |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2759 | // False -> 0 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2760 | case ICmpInst::ICMP_UGT: return 1; // 001 |
| 2761 | case ICmpInst::ICMP_SGT: return 1; // 001 |
| 2762 | case ICmpInst::ICMP_EQ: return 2; // 010 |
| 2763 | case ICmpInst::ICMP_UGE: return 3; // 011 |
| 2764 | case ICmpInst::ICMP_SGE: return 3; // 011 |
| 2765 | case ICmpInst::ICMP_ULT: return 4; // 100 |
| 2766 | case ICmpInst::ICMP_SLT: return 4; // 100 |
| 2767 | case ICmpInst::ICMP_NE: return 5; // 101 |
| 2768 | case ICmpInst::ICMP_ULE: return 6; // 110 |
| 2769 | case ICmpInst::ICMP_SLE: return 6; // 110 |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2770 | // True -> 7 |
| 2771 | default: |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2772 | assert(0 && "Invalid ICmp predicate!"); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2773 | return 0; |
| 2774 | } |
| 2775 | } |
| 2776 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2777 | /// getICmpValue - This is the complement of getICmpCode, which turns an |
| 2778 | /// opcode and two operands into either a constant true or false, or a brand |
| 2779 | /// new /// ICmp instruction. The sign is passed in to determine which kind |
| 2780 | /// of predicate to use in new icmp instructions. |
| 2781 | static Value *getICmpValue(bool sign, unsigned code, Value *LHS, Value *RHS) { |
| 2782 | switch (code) { |
| 2783 | default: assert(0 && "Illegal ICmp code!"); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2784 | case 0: return ConstantInt::getFalse(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2785 | case 1: |
| 2786 | if (sign) |
| 2787 | return new ICmpInst(ICmpInst::ICMP_SGT, LHS, RHS); |
| 2788 | else |
| 2789 | return new ICmpInst(ICmpInst::ICMP_UGT, LHS, RHS); |
| 2790 | case 2: return new ICmpInst(ICmpInst::ICMP_EQ, LHS, RHS); |
| 2791 | case 3: |
| 2792 | if (sign) |
| 2793 | return new ICmpInst(ICmpInst::ICMP_SGE, LHS, RHS); |
| 2794 | else |
| 2795 | return new ICmpInst(ICmpInst::ICMP_UGE, LHS, RHS); |
| 2796 | case 4: |
| 2797 | if (sign) |
| 2798 | return new ICmpInst(ICmpInst::ICMP_SLT, LHS, RHS); |
| 2799 | else |
| 2800 | return new ICmpInst(ICmpInst::ICMP_ULT, LHS, RHS); |
| 2801 | case 5: return new ICmpInst(ICmpInst::ICMP_NE, LHS, RHS); |
| 2802 | case 6: |
| 2803 | if (sign) |
| 2804 | return new ICmpInst(ICmpInst::ICMP_SLE, LHS, RHS); |
| 2805 | else |
| 2806 | return new ICmpInst(ICmpInst::ICMP_ULE, LHS, RHS); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2807 | case 7: return ConstantInt::getTrue(); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2808 | } |
| 2809 | } |
| 2810 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2811 | static bool PredicatesFoldable(ICmpInst::Predicate p1, ICmpInst::Predicate p2) { |
| 2812 | return (ICmpInst::isSignedPredicate(p1) == ICmpInst::isSignedPredicate(p2)) || |
| 2813 | (ICmpInst::isSignedPredicate(p1) && |
| 2814 | (p2 == ICmpInst::ICMP_EQ || p2 == ICmpInst::ICMP_NE)) || |
| 2815 | (ICmpInst::isSignedPredicate(p2) && |
| 2816 | (p1 == ICmpInst::ICMP_EQ || p1 == ICmpInst::ICMP_NE)); |
| 2817 | } |
| 2818 | |
| 2819 | namespace { |
| 2820 | // FoldICmpLogical - Implements (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B) |
| 2821 | struct FoldICmpLogical { |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2822 | InstCombiner &IC; |
| 2823 | Value *LHS, *RHS; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2824 | ICmpInst::Predicate pred; |
| 2825 | FoldICmpLogical(InstCombiner &ic, ICmpInst *ICI) |
| 2826 | : IC(ic), LHS(ICI->getOperand(0)), RHS(ICI->getOperand(1)), |
| 2827 | pred(ICI->getPredicate()) {} |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2828 | bool shouldApply(Value *V) const { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2829 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(V)) |
| 2830 | if (PredicatesFoldable(pred, ICI->getPredicate())) |
| 2831 | return (ICI->getOperand(0) == LHS && ICI->getOperand(1) == RHS || |
| 2832 | ICI->getOperand(0) == RHS && ICI->getOperand(1) == LHS); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2833 | return false; |
| 2834 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2835 | Instruction *apply(Instruction &Log) const { |
| 2836 | ICmpInst *ICI = cast<ICmpInst>(Log.getOperand(0)); |
| 2837 | if (ICI->getOperand(0) != LHS) { |
| 2838 | assert(ICI->getOperand(1) == LHS); |
| 2839 | ICI->swapOperands(); // Swap the LHS and RHS of the ICmp |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2840 | } |
| 2841 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2842 | unsigned LHSCode = getICmpCode(ICI); |
| 2843 | unsigned RHSCode = getICmpCode(cast<ICmpInst>(Log.getOperand(1))); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2844 | unsigned Code; |
| 2845 | switch (Log.getOpcode()) { |
| 2846 | case Instruction::And: Code = LHSCode & RHSCode; break; |
| 2847 | case Instruction::Or: Code = LHSCode | RHSCode; break; |
| 2848 | case Instruction::Xor: Code = LHSCode ^ RHSCode; break; |
Chris Lattner | 021c190 | 2003-09-22 20:33:34 +0000 | [diff] [blame] | 2849 | default: assert(0 && "Illegal logical opcode!"); return 0; |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2850 | } |
| 2851 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2852 | Value *RV = getICmpValue(ICmpInst::isSignedPredicate(pred), Code, LHS, RHS); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2853 | if (Instruction *I = dyn_cast<Instruction>(RV)) |
| 2854 | return I; |
| 2855 | // Otherwise, it's a constant boolean value... |
| 2856 | return IC.ReplaceInstUsesWith(Log, RV); |
| 2857 | } |
| 2858 | }; |
Chris Lattner | d23b5ba | 2006-11-15 04:53:24 +0000 | [diff] [blame] | 2859 | } // end anonymous namespace |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2860 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2861 | // OptAndOp - This handles expressions of the form ((val OP C1) & C2). Where |
| 2862 | // 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] | 2863 | // guaranteed to be a binary operator. |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2864 | Instruction *InstCombiner::OptAndOp(Instruction *Op, |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2865 | ConstantInt *OpRHS, |
| 2866 | ConstantInt *AndRHS, |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2867 | BinaryOperator &TheAnd) { |
| 2868 | Value *X = Op->getOperand(0); |
Chris Lattner | 76f7fe2 | 2004-01-12 19:47:05 +0000 | [diff] [blame] | 2869 | Constant *Together = 0; |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2870 | if (!Op->isShift()) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2871 | Together = ConstantExpr::getAnd(AndRHS, OpRHS); |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2872 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2873 | switch (Op->getOpcode()) { |
| 2874 | case Instruction::Xor: |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2875 | if (Op->hasOneUse()) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2876 | // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2) |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 2877 | Instruction *And = BinaryOperator::createAnd(X, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2878 | InsertNewInstBefore(And, TheAnd); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 2879 | And->takeName(Op); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2880 | return BinaryOperator::createXor(And, Together); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2881 | } |
| 2882 | break; |
| 2883 | case Instruction::Or: |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2884 | if (Together == AndRHS) // (X | C) & C --> C |
| 2885 | return ReplaceInstUsesWith(TheAnd, AndRHS); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2886 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2887 | if (Op->hasOneUse() && Together != OpRHS) { |
| 2888 | // (X | C1) & C2 --> (X | (C1&C2)) & C2 |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 2889 | Instruction *Or = BinaryOperator::createOr(X, Together); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2890 | InsertNewInstBefore(Or, TheAnd); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 2891 | Or->takeName(Op); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2892 | return BinaryOperator::createAnd(Or, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2893 | } |
| 2894 | break; |
| 2895 | case Instruction::Add: |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 2896 | if (Op->hasOneUse()) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2897 | // Adding a one to a single bit bit-field should be turned into an XOR |
| 2898 | // of the bit. First thing to check is to see if this AND is with a |
| 2899 | // single bit constant. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2900 | uint64_t AndRHSV = cast<ConstantInt>(AndRHS)->getZExtValue(); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2901 | |
| 2902 | // Clear bits that are not part of the constant. |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 2903 | AndRHSV &= AndRHS->getType()->getBitMask(); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2904 | |
| 2905 | // If there is only one bit set... |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2906 | if (isOneBitSet(cast<ConstantInt>(AndRHS))) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2907 | // Ok, at this point, we know that we are masking the result of the |
| 2908 | // ADD down to exactly one bit. If the constant we are adding has |
| 2909 | // no bits set below this bit, then we can eliminate the ADD. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2910 | uint64_t AddRHS = cast<ConstantInt>(OpRHS)->getZExtValue(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2911 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2912 | // Check to see if any bits below the one bit set in AndRHSV are set. |
| 2913 | if ((AddRHS & (AndRHSV-1)) == 0) { |
| 2914 | // If not, the only thing that can effect the output of the AND is |
| 2915 | // the bit specified by AndRHSV. If that bit is set, the effect of |
| 2916 | // the XOR is to toggle the bit. If it is clear, then the ADD has |
| 2917 | // no effect. |
| 2918 | if ((AddRHS & AndRHSV) == 0) { // Bit is not set, noop |
| 2919 | TheAnd.setOperand(0, X); |
| 2920 | return &TheAnd; |
| 2921 | } else { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2922 | // Pull the XOR out of the AND. |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 2923 | Instruction *NewAnd = BinaryOperator::createAnd(X, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2924 | InsertNewInstBefore(NewAnd, TheAnd); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 2925 | NewAnd->takeName(Op); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2926 | return BinaryOperator::createXor(NewAnd, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2927 | } |
| 2928 | } |
| 2929 | } |
| 2930 | } |
| 2931 | break; |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 2932 | |
| 2933 | case Instruction::Shl: { |
| 2934 | // We know that the AND will not produce any of the bits shifted in, so if |
| 2935 | // the anded constant includes them, clear them now! |
| 2936 | // |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2937 | Constant *AllOne = ConstantInt::getAllOnesValue(AndRHS->getType()); |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 2938 | Constant *ShlMask = ConstantExpr::getShl(AllOne, OpRHS); |
| 2939 | Constant *CI = ConstantExpr::getAnd(AndRHS, ShlMask); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2940 | |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 2941 | if (CI == ShlMask) { // Masking out bits that the shift already masks |
| 2942 | return ReplaceInstUsesWith(TheAnd, Op); // No need for the and. |
| 2943 | } else if (CI != AndRHS) { // Reducing bits set in and. |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 2944 | TheAnd.setOperand(1, CI); |
| 2945 | return &TheAnd; |
| 2946 | } |
| 2947 | break; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2948 | } |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2949 | case Instruction::LShr: |
| 2950 | { |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 2951 | // We know that the AND will not produce any of the bits shifted in, so if |
| 2952 | // the anded constant includes them, clear them now! This only applies to |
| 2953 | // unsigned shifts, because a signed shr may bring in set bits! |
| 2954 | // |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2955 | Constant *AllOne = ConstantInt::getAllOnesValue(AndRHS->getType()); |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2956 | Constant *ShrMask = ConstantExpr::getLShr(AllOne, OpRHS); |
| 2957 | Constant *CI = ConstantExpr::getAnd(AndRHS, ShrMask); |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 2958 | |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2959 | if (CI == ShrMask) { // Masking out bits that the shift already masks. |
| 2960 | return ReplaceInstUsesWith(TheAnd, Op); |
| 2961 | } else if (CI != AndRHS) { |
| 2962 | TheAnd.setOperand(1, CI); // Reduce bits set in and cst. |
| 2963 | return &TheAnd; |
| 2964 | } |
| 2965 | break; |
| 2966 | } |
| 2967 | case Instruction::AShr: |
| 2968 | // Signed shr. |
| 2969 | // See if this is shifting in some sign extension, then masking it out |
| 2970 | // with an and. |
| 2971 | if (Op->hasOneUse()) { |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2972 | Constant *AllOne = ConstantInt::getAllOnesValue(AndRHS->getType()); |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2973 | Constant *ShrMask = ConstantExpr::getLShr(AllOne, OpRHS); |
Reid Spencer | 7eb7638 | 2006-12-13 17:19:09 +0000 | [diff] [blame] | 2974 | Constant *C = ConstantExpr::getAnd(AndRHS, ShrMask); |
| 2975 | if (C == AndRHS) { // Masking out bits shifted in. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 2976 | // (Val ashr C1) & C2 -> (Val lshr C1) & C2 |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2977 | // Make the argument unsigned. |
| 2978 | Value *ShVal = Op->getOperand(0); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2979 | ShVal = InsertNewInstBefore( |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 2980 | BinaryOperator::createLShr(ShVal, OpRHS, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2981 | Op->getName()), TheAnd); |
Reid Spencer | 7eb7638 | 2006-12-13 17:19:09 +0000 | [diff] [blame] | 2982 | return BinaryOperator::createAnd(ShVal, AndRHS, TheAnd.getName()); |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 2983 | } |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 2984 | } |
| 2985 | break; |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2986 | } |
| 2987 | return 0; |
| 2988 | } |
| 2989 | |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2990 | |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2991 | /// InsertRangeTest - Emit a computation of: (V >= Lo && V < Hi) if Inside is |
| 2992 | /// 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] | 2993 | /// (V-Lo) <u Hi-Lo. This method expects that Lo <= Hi. isSigned indicates |
| 2994 | /// 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] | 2995 | /// insert new instructions. |
| 2996 | Instruction *InstCombiner::InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2997 | bool isSigned, bool Inside, |
| 2998 | Instruction &IB) { |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2999 | assert(cast<ConstantInt>(ConstantExpr::getICmp((isSigned ? |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 3000 | ICmpInst::ICMP_SLE:ICmpInst::ICMP_ULE), Lo, Hi))->getZExtValue() && |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3001 | "Lo is not <= Hi in range emission code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3002 | |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3003 | if (Inside) { |
| 3004 | if (Lo == Hi) // Trivially false. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3005 | return new ICmpInst(ICmpInst::ICMP_NE, V, V); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3006 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3007 | // V >= Min && V < Hi --> V < Hi |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3008 | if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3009 | ICmpInst::Predicate pred = (isSigned ? |
| 3010 | ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT); |
| 3011 | return new ICmpInst(pred, V, Hi); |
| 3012 | } |
| 3013 | |
| 3014 | // Emit V-Lo <u Hi-Lo |
| 3015 | Constant *NegLo = ConstantExpr::getNeg(Lo); |
| 3016 | Instruction *Add = BinaryOperator::createAdd(V, NegLo, V->getName()+".off"); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3017 | InsertNewInstBefore(Add, IB); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3018 | Constant *UpperBound = ConstantExpr::getAdd(NegLo, Hi); |
| 3019 | return new ICmpInst(ICmpInst::ICMP_ULT, Add, UpperBound); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3020 | } |
| 3021 | |
| 3022 | if (Lo == Hi) // Trivially true. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3023 | return new ICmpInst(ICmpInst::ICMP_EQ, V, V); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3024 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3025 | // V < Min || V >= Hi ->'V > Hi-1' |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3026 | Hi = SubOne(cast<ConstantInt>(Hi)); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3027 | if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3028 | ICmpInst::Predicate pred = (isSigned ? |
| 3029 | ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT); |
| 3030 | return new ICmpInst(pred, V, Hi); |
| 3031 | } |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3032 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3033 | // Emit V-Lo > Hi-1-Lo |
| 3034 | Constant *NegLo = ConstantExpr::getNeg(Lo); |
| 3035 | Instruction *Add = BinaryOperator::createAdd(V, NegLo, V->getName()+".off"); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3036 | InsertNewInstBefore(Add, IB); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3037 | Constant *LowerBound = ConstantExpr::getAdd(NegLo, Hi); |
| 3038 | return new ICmpInst(ICmpInst::ICMP_UGT, Add, LowerBound); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3039 | } |
| 3040 | |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3041 | // isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with |
| 3042 | // any number of 0s on either side. The 1s are allowed to wrap from LSB to |
| 3043 | // MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is |
| 3044 | // not, since all 1s are not contiguous. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3045 | static bool isRunOfOnes(ConstantInt *Val, unsigned &MB, unsigned &ME) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3046 | uint64_t V = Val->getZExtValue(); |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3047 | if (!isShiftedMask_64(V)) return false; |
| 3048 | |
| 3049 | // look for the first zero bit after the run of ones |
| 3050 | MB = 64-CountLeadingZeros_64((V - 1) ^ V); |
| 3051 | // look for the first non-zero bit |
| 3052 | ME = 64-CountLeadingZeros_64(V); |
| 3053 | return true; |
| 3054 | } |
| 3055 | |
| 3056 | |
| 3057 | |
| 3058 | /// FoldLogicalPlusAnd - This is part of an expression (LHS +/- RHS) & Mask, |
| 3059 | /// where isSub determines whether the operator is a sub. If we can fold one of |
| 3060 | /// the following xforms: |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3061 | /// |
| 3062 | /// ((A & N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == Mask |
| 3063 | /// ((A | N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0 |
| 3064 | /// ((A ^ N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0 |
| 3065 | /// |
| 3066 | /// return (A +/- B). |
| 3067 | /// |
| 3068 | Value *InstCombiner::FoldLogicalPlusAnd(Value *LHS, Value *RHS, |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3069 | ConstantInt *Mask, bool isSub, |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3070 | Instruction &I) { |
| 3071 | Instruction *LHSI = dyn_cast<Instruction>(LHS); |
| 3072 | if (!LHSI || LHSI->getNumOperands() != 2 || |
| 3073 | !isa<ConstantInt>(LHSI->getOperand(1))) return 0; |
| 3074 | |
| 3075 | ConstantInt *N = cast<ConstantInt>(LHSI->getOperand(1)); |
| 3076 | |
| 3077 | switch (LHSI->getOpcode()) { |
| 3078 | default: return 0; |
| 3079 | case Instruction::And: |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3080 | if (ConstantExpr::getAnd(N, Mask) == Mask) { |
| 3081 | // If the AndRHS is a power of two minus one (0+1+), this is simple. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3082 | if ((Mask->getZExtValue() & Mask->getZExtValue()+1) == 0) |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3083 | break; |
| 3084 | |
| 3085 | // Otherwise, if Mask is 0+1+0+, and if B is known to have the low 0+ |
| 3086 | // part, we don't need any explicit masks to take them out of A. If that |
| 3087 | // is all N is, ignore it. |
| 3088 | unsigned MB, ME; |
| 3089 | if (isRunOfOnes(Mask, MB, ME)) { // begin/end bit of run, inclusive |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 3090 | uint64_t Mask = cast<IntegerType>(RHS->getType())->getBitMask(); |
Chris Lattner | 3bedbd9 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3091 | Mask >>= 64-MB+1; |
| 3092 | if (MaskedValueIsZero(RHS, Mask)) |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3093 | break; |
| 3094 | } |
| 3095 | } |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3096 | return 0; |
| 3097 | case Instruction::Or: |
| 3098 | case Instruction::Xor: |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3099 | // If the AndRHS is a power of two minus one (0+1+), and N&Mask == 0 |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3100 | if ((Mask->getZExtValue() & Mask->getZExtValue()+1) == 0 && |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3101 | ConstantExpr::getAnd(N, Mask)->isNullValue()) |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3102 | break; |
| 3103 | return 0; |
| 3104 | } |
| 3105 | |
| 3106 | Instruction *New; |
| 3107 | if (isSub) |
| 3108 | New = BinaryOperator::createSub(LHSI->getOperand(0), RHS, "fold"); |
| 3109 | else |
| 3110 | New = BinaryOperator::createAdd(LHSI->getOperand(0), RHS, "fold"); |
| 3111 | return InsertNewInstBefore(New, I); |
| 3112 | } |
| 3113 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3114 | Instruction *InstCombiner::visitAnd(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 3115 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3116 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3117 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3118 | if (isa<UndefValue>(Op1)) // X & undef -> 0 |
| 3119 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 3120 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3121 | // and X, X = X |
| 3122 | if (Op0 == Op1) |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 3123 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3124 | |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3125 | // 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] | 3126 | // purpose is to compute bits we don't care about. |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 3127 | uint64_t KnownZero, KnownOne; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 3128 | if (!isa<VectorType>(I.getType())) { |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 3129 | if (SimplifyDemandedBits(&I, cast<IntegerType>(I.getType())->getBitMask(), |
Chris Lattner | 696ee0a | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3130 | KnownZero, KnownOne)) |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 3131 | return &I; |
Chris Lattner | 696ee0a | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3132 | } else { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 3133 | if (ConstantVector *CP = dyn_cast<ConstantVector>(Op1)) { |
Chris Lattner | 696ee0a | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3134 | if (CP->isAllOnesValue()) |
| 3135 | return ReplaceInstUsesWith(I, I.getOperand(0)); |
| 3136 | } |
| 3137 | } |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 3138 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3139 | if (ConstantInt *AndRHS = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 7560c3a | 2006-02-08 07:34:50 +0000 | [diff] [blame] | 3140 | uint64_t AndRHSMask = AndRHS->getZExtValue(); |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 3141 | uint64_t TypeMask = cast<IntegerType>(Op0->getType())->getBitMask(); |
Chris Lattner | 7560c3a | 2006-02-08 07:34:50 +0000 | [diff] [blame] | 3142 | uint64_t NotAndRHS = AndRHSMask^TypeMask; |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3143 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3144 | // Optimize a variety of ((val OP C1) & C2) combinations... |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3145 | if (isa<BinaryOperator>(Op0)) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3146 | Instruction *Op0I = cast<Instruction>(Op0); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3147 | Value *Op0LHS = Op0I->getOperand(0); |
| 3148 | Value *Op0RHS = Op0I->getOperand(1); |
| 3149 | switch (Op0I->getOpcode()) { |
| 3150 | case Instruction::Xor: |
| 3151 | case Instruction::Or: |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 3152 | // If the mask is only needed on one incoming arm, push it up. |
| 3153 | if (Op0I->hasOneUse()) { |
| 3154 | if (MaskedValueIsZero(Op0LHS, NotAndRHS)) { |
| 3155 | // Not masking anything out for the LHS, move to RHS. |
| 3156 | Instruction *NewRHS = BinaryOperator::createAnd(Op0RHS, AndRHS, |
| 3157 | Op0RHS->getName()+".masked"); |
| 3158 | InsertNewInstBefore(NewRHS, I); |
| 3159 | return BinaryOperator::create( |
| 3160 | cast<BinaryOperator>(Op0I)->getOpcode(), Op0LHS, NewRHS); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3161 | } |
Chris Lattner | 3bedbd9 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3162 | if (!isa<Constant>(Op0RHS) && |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 3163 | MaskedValueIsZero(Op0RHS, NotAndRHS)) { |
| 3164 | // Not masking anything out for the RHS, move to LHS. |
| 3165 | Instruction *NewLHS = BinaryOperator::createAnd(Op0LHS, AndRHS, |
| 3166 | Op0LHS->getName()+".masked"); |
| 3167 | InsertNewInstBefore(NewLHS, I); |
| 3168 | return BinaryOperator::create( |
| 3169 | cast<BinaryOperator>(Op0I)->getOpcode(), NewLHS, Op0RHS); |
| 3170 | } |
| 3171 | } |
| 3172 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3173 | break; |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3174 | case Instruction::Add: |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3175 | // ((A & N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == AndRHS. |
| 3176 | // ((A | N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0 |
| 3177 | // ((A ^ N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0 |
| 3178 | if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, false, I)) |
| 3179 | return BinaryOperator::createAnd(V, AndRHS); |
| 3180 | if (Value *V = FoldLogicalPlusAnd(Op0RHS, Op0LHS, AndRHS, false, I)) |
| 3181 | return BinaryOperator::createAnd(V, AndRHS); // Add commutes |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3182 | break; |
| 3183 | |
| 3184 | case Instruction::Sub: |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3185 | // ((A & N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == AndRHS. |
| 3186 | // ((A | N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0 |
| 3187 | // ((A ^ N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0 |
| 3188 | if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, true, I)) |
| 3189 | return BinaryOperator::createAnd(V, AndRHS); |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3190 | break; |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3191 | } |
| 3192 | |
Chris Lattner | 5840326 | 2003-07-23 19:25:52 +0000 | [diff] [blame] | 3193 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3194 | if (Instruction *Res = OptAndOp(Op0I, Op0CI, AndRHS, I)) |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3195 | return Res; |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3196 | } else if (CastInst *CI = dyn_cast<CastInst>(Op0)) { |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3197 | // If this is an integer truncation or change from signed-to-unsigned, and |
| 3198 | // if the source is an and/or with immediate, transform it. This |
| 3199 | // frequently occurs for bitfield accesses. |
| 3200 | if (Instruction *CastOp = dyn_cast<Instruction>(CI->getOperand(0))) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3201 | if ((isa<TruncInst>(CI) || isa<BitCastInst>(CI)) && |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3202 | CastOp->getNumOperands() == 2) |
Chris Lattner | 7560c3a | 2006-02-08 07:34:50 +0000 | [diff] [blame] | 3203 | if (ConstantInt *AndCI = dyn_cast<ConstantInt>(CastOp->getOperand(1))) |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3204 | if (CastOp->getOpcode() == Instruction::And) { |
| 3205 | // Change: and (cast (and X, C1) to T), C2 |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3206 | // into : and (cast X to T), trunc_or_bitcast(C1)&C2 |
| 3207 | // This will fold the two constants together, which may allow |
| 3208 | // other simplifications. |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 3209 | Instruction *NewCast = CastInst::createTruncOrBitCast( |
| 3210 | CastOp->getOperand(0), I.getType(), |
| 3211 | CastOp->getName()+".shrunk"); |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3212 | NewCast = InsertNewInstBefore(NewCast, I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3213 | // trunc_or_bitcast(C1)&C2 |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 3214 | Constant *C3 = ConstantExpr::getTruncOrBitCast(AndCI,I.getType()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3215 | C3 = ConstantExpr::getAnd(C3, AndRHS); |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3216 | return BinaryOperator::createAnd(NewCast, C3); |
| 3217 | } else if (CastOp->getOpcode() == Instruction::Or) { |
| 3218 | // Change: and (cast (or X, C1) to T), C2 |
| 3219 | // into : trunc(C1)&C2 iff trunc(C1)&C2 == C2 |
Chris Lattner | bb4e7b2 | 2006-12-12 19:11:20 +0000 | [diff] [blame] | 3220 | Constant *C3 = ConstantExpr::getTruncOrBitCast(AndCI,I.getType()); |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3221 | if (ConstantExpr::getAnd(C3, AndRHS) == AndRHS) // trunc(C1)&C2 |
| 3222 | return ReplaceInstUsesWith(I, AndRHS); |
| 3223 | } |
| 3224 | } |
Chris Lattner | 06782f8 | 2003-07-23 19:36:21 +0000 | [diff] [blame] | 3225 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3226 | |
| 3227 | // Try to fold constant and into select arguments. |
| 3228 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3229 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3230 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 3231 | if (isa<PHINode>(Op0)) |
| 3232 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 3233 | return NV; |
Chris Lattner | c6a8aff | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 3234 | } |
| 3235 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 3236 | Value *Op0NotVal = dyn_castNotVal(Op0); |
| 3237 | Value *Op1NotVal = dyn_castNotVal(Op1); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3238 | |
Chris Lattner | 5b62aa7 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 3239 | if (Op0NotVal == Op1 || Op1NotVal == Op0) // A & ~A == ~A & A == 0 |
| 3240 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 3241 | |
Misha Brukman | cb6267b | 2004-07-30 12:50:08 +0000 | [diff] [blame] | 3242 | // (~A & ~B) == (~(A | B)) - De Morgan's Law |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 3243 | if (Op0NotVal && Op1NotVal && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3244 | Instruction *Or = BinaryOperator::createOr(Op0NotVal, Op1NotVal, |
| 3245 | I.getName()+".demorgan"); |
Chris Lattner | c6a8aff | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 3246 | InsertNewInstBefore(Or, I); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3247 | return BinaryOperator::createNot(Or); |
| 3248 | } |
Chris Lattner | 2082ad9 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3249 | |
| 3250 | { |
| 3251 | Value *A = 0, *B = 0; |
Chris Lattner | 2082ad9 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3252 | if (match(Op0, m_Or(m_Value(A), m_Value(B)))) |
| 3253 | if (A == Op1 || B == Op1) // (A | ?) & A --> A |
| 3254 | return ReplaceInstUsesWith(I, Op1); |
| 3255 | if (match(Op1, m_Or(m_Value(A), m_Value(B)))) |
| 3256 | if (A == Op0 || B == Op0) // A & (A | ?) --> A |
| 3257 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 3258 | |
| 3259 | if (Op0->hasOneUse() && |
| 3260 | match(Op0, m_Xor(m_Value(A), m_Value(B)))) { |
| 3261 | if (A == Op1) { // (A^B)&A -> A&(A^B) |
| 3262 | I.swapOperands(); // Simplify below |
| 3263 | std::swap(Op0, Op1); |
| 3264 | } else if (B == Op1) { // (A^B)&B -> B&(B^A) |
| 3265 | cast<BinaryOperator>(Op0)->swapOperands(); |
| 3266 | I.swapOperands(); // Simplify below |
| 3267 | std::swap(Op0, Op1); |
| 3268 | } |
| 3269 | } |
| 3270 | if (Op1->hasOneUse() && |
| 3271 | match(Op1, m_Xor(m_Value(A), m_Value(B)))) { |
| 3272 | if (B == Op0) { // B&(A^B) -> B&(B^A) |
| 3273 | cast<BinaryOperator>(Op1)->swapOperands(); |
| 3274 | std::swap(A, B); |
| 3275 | } |
| 3276 | if (A == Op0) { // A&(A^B) -> A & ~B |
| 3277 | Instruction *NotB = BinaryOperator::createNot(B, "tmp"); |
| 3278 | InsertNewInstBefore(NotB, I); |
| 3279 | return BinaryOperator::createAnd(A, NotB); |
| 3280 | } |
| 3281 | } |
Chris Lattner | 2082ad9 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3282 | } |
| 3283 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3284 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(Op1)) { |
| 3285 | // (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B) |
| 3286 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS))) |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3287 | return R; |
| 3288 | |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3289 | Value *LHSVal, *RHSVal; |
| 3290 | ConstantInt *LHSCst, *RHSCst; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3291 | ICmpInst::Predicate LHSCC, RHSCC; |
| 3292 | if (match(Op0, m_ICmp(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst)))) |
| 3293 | if (match(RHS, m_ICmp(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst)))) |
| 3294 | if (LHSVal == RHSVal && // Found (X icmp C1) & (X icmp C2) |
| 3295 | // ICMP_[GL]E X, CST is folded to ICMP_[GL]T elsewhere. |
| 3296 | LHSCC != ICmpInst::ICMP_UGE && LHSCC != ICmpInst::ICMP_ULE && |
| 3297 | RHSCC != ICmpInst::ICMP_UGE && RHSCC != ICmpInst::ICMP_ULE && |
| 3298 | LHSCC != ICmpInst::ICMP_SGE && LHSCC != ICmpInst::ICMP_SLE && |
| 3299 | RHSCC != ICmpInst::ICMP_SGE && RHSCC != ICmpInst::ICMP_SLE) { |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3300 | // Ensure that the larger constant is on the RHS. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3301 | ICmpInst::Predicate GT = ICmpInst::isSignedPredicate(LHSCC) ? |
| 3302 | ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; |
| 3303 | Constant *Cmp = ConstantExpr::getICmp(GT, LHSCst, RHSCst); |
| 3304 | ICmpInst *LHS = cast<ICmpInst>(Op0); |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 3305 | if (cast<ConstantInt>(Cmp)->getZExtValue()) { |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3306 | std::swap(LHS, RHS); |
| 3307 | std::swap(LHSCst, RHSCst); |
| 3308 | std::swap(LHSCC, RHSCC); |
| 3309 | } |
| 3310 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3311 | // At this point, we know we have have two icmp instructions |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3312 | // comparing a value against two constants and and'ing the result |
| 3313 | // together. Because of the above check, we know that we only have |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3314 | // icmp eq, icmp ne, icmp [su]lt, and icmp [SU]gt here. We also know |
| 3315 | // (from the FoldICmpLogical check above), that the two constants |
| 3316 | // are not equal and that the larger constant is on the RHS |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3317 | assert(LHSCst != RHSCst && "Compares not folded above?"); |
| 3318 | |
| 3319 | switch (LHSCC) { |
| 3320 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3321 | case ICmpInst::ICMP_EQ: |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3322 | switch (RHSCC) { |
| 3323 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3324 | case ICmpInst::ICMP_EQ: // (X == 13 & X == 15) -> false |
| 3325 | case ICmpInst::ICMP_UGT: // (X == 13 & X > 15) -> false |
| 3326 | case ICmpInst::ICMP_SGT: // (X == 13 & X > 15) -> false |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3327 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3328 | case ICmpInst::ICMP_NE: // (X == 13 & X != 15) -> X == 13 |
| 3329 | case ICmpInst::ICMP_ULT: // (X == 13 & X < 15) -> X == 13 |
| 3330 | case ICmpInst::ICMP_SLT: // (X == 13 & X < 15) -> X == 13 |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3331 | return ReplaceInstUsesWith(I, LHS); |
| 3332 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3333 | case ICmpInst::ICMP_NE: |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3334 | switch (RHSCC) { |
| 3335 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3336 | case ICmpInst::ICMP_ULT: |
| 3337 | if (LHSCst == SubOne(RHSCst)) // (X != 13 & X u< 14) -> X < 13 |
| 3338 | return new ICmpInst(ICmpInst::ICMP_ULT, LHSVal, LHSCst); |
| 3339 | break; // (X != 13 & X u< 15) -> no change |
| 3340 | case ICmpInst::ICMP_SLT: |
| 3341 | if (LHSCst == SubOne(RHSCst)) // (X != 13 & X s< 14) -> X < 13 |
| 3342 | return new ICmpInst(ICmpInst::ICMP_SLT, LHSVal, LHSCst); |
| 3343 | break; // (X != 13 & X s< 15) -> no change |
| 3344 | case ICmpInst::ICMP_EQ: // (X != 13 & X == 15) -> X == 15 |
| 3345 | case ICmpInst::ICMP_UGT: // (X != 13 & X u> 15) -> X u> 15 |
| 3346 | case ICmpInst::ICMP_SGT: // (X != 13 & X s> 15) -> X s> 15 |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3347 | return ReplaceInstUsesWith(I, RHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3348 | case ICmpInst::ICMP_NE: |
| 3349 | if (LHSCst == SubOne(RHSCst)){// (X != 13 & X != 14) -> X-13 >u 1 |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3350 | Constant *AddCST = ConstantExpr::getNeg(LHSCst); |
| 3351 | Instruction *Add = BinaryOperator::createAdd(LHSVal, AddCST, |
| 3352 | LHSVal->getName()+".off"); |
| 3353 | InsertNewInstBefore(Add, I); |
Chris Lattner | 424db02 | 2007-01-27 23:08:34 +0000 | [diff] [blame] | 3354 | return new ICmpInst(ICmpInst::ICMP_UGT, Add, |
| 3355 | ConstantInt::get(Add->getType(), 1)); |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3356 | } |
| 3357 | break; // (X != 13 & X != 15) -> no change |
| 3358 | } |
| 3359 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3360 | case ICmpInst::ICMP_ULT: |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3361 | switch (RHSCC) { |
| 3362 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3363 | case ICmpInst::ICMP_EQ: // (X u< 13 & X == 15) -> false |
| 3364 | case ICmpInst::ICMP_UGT: // (X u< 13 & X u> 15) -> false |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3365 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3366 | case ICmpInst::ICMP_SGT: // (X u< 13 & X s> 15) -> no change |
| 3367 | break; |
| 3368 | case ICmpInst::ICMP_NE: // (X u< 13 & X != 15) -> X u< 13 |
| 3369 | 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] | 3370 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3371 | case ICmpInst::ICMP_SLT: // (X u< 13 & X s< 15) -> no change |
| 3372 | break; |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3373 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3374 | break; |
| 3375 | case ICmpInst::ICMP_SLT: |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3376 | switch (RHSCC) { |
| 3377 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3378 | case ICmpInst::ICMP_EQ: // (X s< 13 & X == 15) -> false |
| 3379 | case ICmpInst::ICMP_SGT: // (X s< 13 & X s> 15) -> false |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3380 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3381 | case ICmpInst::ICMP_UGT: // (X s< 13 & X u> 15) -> no change |
| 3382 | break; |
| 3383 | case ICmpInst::ICMP_NE: // (X s< 13 & X != 15) -> X < 13 |
| 3384 | case ICmpInst::ICMP_SLT: // (X s< 13 & X s< 15) -> X < 13 |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3385 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3386 | case ICmpInst::ICMP_ULT: // (X s< 13 & X u< 15) -> no change |
| 3387 | break; |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3388 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3389 | break; |
| 3390 | case ICmpInst::ICMP_UGT: |
| 3391 | switch (RHSCC) { |
| 3392 | default: assert(0 && "Unknown integer condition code!"); |
| 3393 | case ICmpInst::ICMP_EQ: // (X u> 13 & X == 15) -> X > 13 |
| 3394 | return ReplaceInstUsesWith(I, LHS); |
| 3395 | case ICmpInst::ICMP_UGT: // (X u> 13 & X u> 15) -> X u> 15 |
| 3396 | return ReplaceInstUsesWith(I, RHS); |
| 3397 | case ICmpInst::ICMP_SGT: // (X u> 13 & X s> 15) -> no change |
| 3398 | break; |
| 3399 | case ICmpInst::ICMP_NE: |
| 3400 | if (RHSCst == AddOne(LHSCst)) // (X u> 13 & X != 14) -> X u> 14 |
| 3401 | return new ICmpInst(LHSCC, LHSVal, RHSCst); |
| 3402 | break; // (X u> 13 & X != 15) -> no change |
| 3403 | case ICmpInst::ICMP_ULT: // (X u> 13 & X u< 15) ->(X-14) <u 1 |
| 3404 | return InsertRangeTest(LHSVal, AddOne(LHSCst), RHSCst, false, |
| 3405 | true, I); |
| 3406 | case ICmpInst::ICMP_SLT: // (X u> 13 & X s< 15) -> no change |
| 3407 | break; |
| 3408 | } |
| 3409 | break; |
| 3410 | case ICmpInst::ICMP_SGT: |
| 3411 | switch (RHSCC) { |
| 3412 | default: assert(0 && "Unknown integer condition code!"); |
| 3413 | case ICmpInst::ICMP_EQ: // (X s> 13 & X == 15) -> X s> 13 |
| 3414 | return ReplaceInstUsesWith(I, LHS); |
| 3415 | case ICmpInst::ICMP_SGT: // (X s> 13 & X s> 15) -> X s> 15 |
| 3416 | return ReplaceInstUsesWith(I, RHS); |
| 3417 | case ICmpInst::ICMP_UGT: // (X s> 13 & X u> 15) -> no change |
| 3418 | break; |
| 3419 | case ICmpInst::ICMP_NE: |
| 3420 | if (RHSCst == AddOne(LHSCst)) // (X s> 13 & X != 14) -> X s> 14 |
| 3421 | return new ICmpInst(LHSCC, LHSVal, RHSCst); |
| 3422 | break; // (X s> 13 & X != 15) -> no change |
| 3423 | case ICmpInst::ICMP_SLT: // (X s> 13 & X s< 15) ->(X-14) s< 1 |
| 3424 | return InsertRangeTest(LHSVal, AddOne(LHSCst), RHSCst, true, |
| 3425 | true, I); |
| 3426 | case ICmpInst::ICMP_ULT: // (X s> 13 & X u< 15) -> no change |
| 3427 | break; |
| 3428 | } |
| 3429 | break; |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3430 | } |
| 3431 | } |
| 3432 | } |
| 3433 | |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3434 | // fold (and (cast A), (cast B)) -> (cast (and A, B)) |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3435 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) |
| 3436 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) |
| 3437 | if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind ? |
| 3438 | const Type *SrcTy = Op0C->getOperand(0)->getType(); |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 3439 | if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() && |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3440 | // 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] | 3441 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), |
| 3442 | I.getType(), TD) && |
| 3443 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), |
| 3444 | I.getType(), TD)) { |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3445 | Instruction *NewOp = BinaryOperator::createAnd(Op0C->getOperand(0), |
| 3446 | Op1C->getOperand(0), |
| 3447 | I.getName()); |
| 3448 | InsertNewInstBefore(NewOp, I); |
| 3449 | return CastInst::create(Op0C->getOpcode(), NewOp, I.getType()); |
| 3450 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3451 | } |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 3452 | |
| 3453 | // (X >> Z) & (Y >> Z) -> (X&Y) >> Z for all shifts. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3454 | if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) { |
| 3455 | if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0)) |
| 3456 | if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() && |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 3457 | SI0->getOperand(1) == SI1->getOperand(1) && |
| 3458 | (SI0->hasOneUse() || SI1->hasOneUse())) { |
| 3459 | Instruction *NewOp = |
| 3460 | InsertNewInstBefore(BinaryOperator::createAnd(SI0->getOperand(0), |
| 3461 | SI1->getOperand(0), |
| 3462 | SI0->getName()), I); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3463 | return BinaryOperator::create(SI1->getOpcode(), NewOp, |
| 3464 | SI1->getOperand(1)); |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 3465 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3466 | } |
| 3467 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3468 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3469 | } |
| 3470 | |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3471 | /// CollectBSwapParts - Look to see if the specified value defines a single byte |
| 3472 | /// in the result. If it does, and if the specified byte hasn't been filled in |
| 3473 | /// yet, fill it in and return false. |
Chris Lattner | 535014f | 2007-02-15 22:52:10 +0000 | [diff] [blame] | 3474 | static bool CollectBSwapParts(Value *V, SmallVector<Value*, 8> &ByteValues) { |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3475 | Instruction *I = dyn_cast<Instruction>(V); |
| 3476 | if (I == 0) return true; |
| 3477 | |
| 3478 | // If this is an or instruction, it is an inner node of the bswap. |
| 3479 | if (I->getOpcode() == Instruction::Or) |
| 3480 | return CollectBSwapParts(I->getOperand(0), ByteValues) || |
| 3481 | CollectBSwapParts(I->getOperand(1), ByteValues); |
| 3482 | |
| 3483 | // If this is a shift by a constant int, and it is "24", then its operand |
| 3484 | // defines a byte. We only handle unsigned types here. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3485 | if (I->isShift() && isa<ConstantInt>(I->getOperand(1))) { |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3486 | // Not shifting the entire input by N-1 bytes? |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3487 | if (cast<ConstantInt>(I->getOperand(1))->getZExtValue() != |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3488 | 8*(ByteValues.size()-1)) |
| 3489 | return true; |
| 3490 | |
| 3491 | unsigned DestNo; |
| 3492 | if (I->getOpcode() == Instruction::Shl) { |
| 3493 | // X << 24 defines the top byte with the lowest of the input bytes. |
| 3494 | DestNo = ByteValues.size()-1; |
| 3495 | } else { |
| 3496 | // X >>u 24 defines the low byte with the highest of the input bytes. |
| 3497 | DestNo = 0; |
| 3498 | } |
| 3499 | |
| 3500 | // If the destination byte value is already defined, the values are or'd |
| 3501 | // together, which isn't a bswap (unless it's an or of the same bits). |
| 3502 | if (ByteValues[DestNo] && ByteValues[DestNo] != I->getOperand(0)) |
| 3503 | return true; |
| 3504 | ByteValues[DestNo] = I->getOperand(0); |
| 3505 | return false; |
| 3506 | } |
| 3507 | |
| 3508 | // Otherwise, we can only handle and(shift X, imm), imm). Bail out of if we |
| 3509 | // don't have this. |
| 3510 | Value *Shift = 0, *ShiftLHS = 0; |
| 3511 | ConstantInt *AndAmt = 0, *ShiftAmt = 0; |
| 3512 | if (!match(I, m_And(m_Value(Shift), m_ConstantInt(AndAmt))) || |
| 3513 | !match(Shift, m_Shift(m_Value(ShiftLHS), m_ConstantInt(ShiftAmt)))) |
| 3514 | return true; |
| 3515 | Instruction *SI = cast<Instruction>(Shift); |
| 3516 | |
| 3517 | // Make sure that the shift amount is by a multiple of 8 and isn't too big. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3518 | if (ShiftAmt->getZExtValue() & 7 || |
| 3519 | ShiftAmt->getZExtValue() > 8*ByteValues.size()) |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3520 | return true; |
| 3521 | |
| 3522 | // Turn 0xFF -> 0, 0xFF00 -> 1, 0xFF0000 -> 2, etc. |
| 3523 | unsigned DestByte; |
| 3524 | for (DestByte = 0; DestByte != ByteValues.size(); ++DestByte) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3525 | if (AndAmt->getZExtValue() == uint64_t(0xFF) << 8*DestByte) |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3526 | break; |
| 3527 | // Unknown mask for bswap. |
| 3528 | if (DestByte == ByteValues.size()) return true; |
| 3529 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3530 | unsigned ShiftBytes = ShiftAmt->getZExtValue()/8; |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3531 | unsigned SrcByte; |
| 3532 | if (SI->getOpcode() == Instruction::Shl) |
| 3533 | SrcByte = DestByte - ShiftBytes; |
| 3534 | else |
| 3535 | SrcByte = DestByte + ShiftBytes; |
| 3536 | |
| 3537 | // If the SrcByte isn't a bswapped value from the DestByte, reject it. |
| 3538 | if (SrcByte != ByteValues.size()-DestByte-1) |
| 3539 | return true; |
| 3540 | |
| 3541 | // If the destination byte value is already defined, the values are or'd |
| 3542 | // together, which isn't a bswap (unless it's an or of the same bits). |
| 3543 | if (ByteValues[DestByte] && ByteValues[DestByte] != SI->getOperand(0)) |
| 3544 | return true; |
| 3545 | ByteValues[DestByte] = SI->getOperand(0); |
| 3546 | return false; |
| 3547 | } |
| 3548 | |
| 3549 | /// MatchBSwap - Given an OR instruction, check to see if this is a bswap idiom. |
| 3550 | /// If so, insert the new bswap intrinsic and return it. |
| 3551 | Instruction *InstCombiner::MatchBSwap(BinaryOperator &I) { |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3552 | // We cannot bswap one byte. |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 3553 | if (I.getType() == Type::Int8Ty) |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3554 | return 0; |
| 3555 | |
| 3556 | /// ByteValues - For each byte of the result, we keep track of which value |
| 3557 | /// defines each byte. |
Chris Lattner | 535014f | 2007-02-15 22:52:10 +0000 | [diff] [blame] | 3558 | SmallVector<Value*, 8> ByteValues; |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 3559 | ByteValues.resize(TD->getTypeSize(I.getType())); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3560 | |
| 3561 | // Try to find all the pieces corresponding to the bswap. |
| 3562 | if (CollectBSwapParts(I.getOperand(0), ByteValues) || |
| 3563 | CollectBSwapParts(I.getOperand(1), ByteValues)) |
| 3564 | return 0; |
| 3565 | |
| 3566 | // Check to see if all of the bytes come from the same value. |
| 3567 | Value *V = ByteValues[0]; |
| 3568 | if (V == 0) return 0; // Didn't find a byte? Must be zero. |
| 3569 | |
| 3570 | // Check to make sure that all of the bytes come from the same value. |
| 3571 | for (unsigned i = 1, e = ByteValues.size(); i != e; ++i) |
| 3572 | if (ByteValues[i] != V) |
| 3573 | return 0; |
| 3574 | |
| 3575 | // If they do then *success* we can turn this into a bswap. Figure out what |
| 3576 | // bswap to make it into. |
| 3577 | Module *M = I.getParent()->getParent()->getParent(); |
Chris Lattner | ed36b2f | 2006-07-11 18:31:26 +0000 | [diff] [blame] | 3578 | const char *FnName = 0; |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 3579 | if (I.getType() == Type::Int16Ty) |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3580 | FnName = "llvm.bswap.i16"; |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 3581 | else if (I.getType() == Type::Int32Ty) |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3582 | FnName = "llvm.bswap.i32"; |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 3583 | else if (I.getType() == Type::Int64Ty) |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3584 | FnName = "llvm.bswap.i64"; |
| 3585 | else |
| 3586 | assert(0 && "Unknown integer type!"); |
Chris Lattner | 9214196 | 2007-01-07 06:58:05 +0000 | [diff] [blame] | 3587 | Constant *F = M->getOrInsertFunction(FnName, I.getType(), I.getType(), NULL); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3588 | return new CallInst(F, V); |
| 3589 | } |
| 3590 | |
| 3591 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3592 | Instruction *InstCombiner::visitOr(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 3593 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3594 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3595 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3596 | if (isa<UndefValue>(Op1)) |
| 3597 | return ReplaceInstUsesWith(I, // X | undef -> -1 |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3598 | ConstantInt::getAllOnesValue(I.getType())); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3599 | |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3600 | // or X, X = X |
| 3601 | if (Op0 == Op1) |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 3602 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3603 | |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3604 | // See if we can simplify any instructions used by the instruction whose sole |
| 3605 | // purpose is to compute bits we don't care about. |
| 3606 | uint64_t KnownZero, KnownOne; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 3607 | if (!isa<VectorType>(I.getType()) && |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 3608 | SimplifyDemandedBits(&I, cast<IntegerType>(I.getType())->getBitMask(), |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3609 | KnownZero, KnownOne)) |
| 3610 | return &I; |
| 3611 | |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3612 | // or X, -1 == -1 |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3613 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 3614 | ConstantInt *C1 = 0; Value *X = 0; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3615 | // (X & C1) | C2 --> (X | C2) & (C1|C2) |
| 3616 | 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] | 3617 | Instruction *Or = BinaryOperator::createOr(X, RHS); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3618 | InsertNewInstBefore(Or, I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3619 | Or->takeName(Op0); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3620 | return BinaryOperator::createAnd(Or, ConstantExpr::getOr(RHS, C1)); |
| 3621 | } |
Chris Lattner | ad44ebf | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 3622 | |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3623 | // (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2) |
| 3624 | 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] | 3625 | Instruction *Or = BinaryOperator::createOr(X, RHS); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3626 | InsertNewInstBefore(Or, I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3627 | Or->takeName(Op0); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3628 | return BinaryOperator::createXor(Or, |
| 3629 | ConstantExpr::getAnd(C1, ConstantExpr::getNot(RHS))); |
Chris Lattner | ad44ebf | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 3630 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3631 | |
| 3632 | // Try to fold constant and into select arguments. |
| 3633 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3634 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3635 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 3636 | if (isa<PHINode>(Op0)) |
| 3637 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 3638 | return NV; |
Chris Lattner | ad44ebf | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 3639 | } |
| 3640 | |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 3641 | Value *A = 0, *B = 0; |
| 3642 | ConstantInt *C1 = 0, *C2 = 0; |
Chris Lattner | f4d4c87 | 2005-05-07 23:49:08 +0000 | [diff] [blame] | 3643 | |
| 3644 | if (match(Op0, m_And(m_Value(A), m_Value(B)))) |
| 3645 | if (A == Op1 || B == Op1) // (A & ?) | A --> A |
| 3646 | return ReplaceInstUsesWith(I, Op1); |
| 3647 | if (match(Op1, m_And(m_Value(A), m_Value(B)))) |
| 3648 | if (A == Op0 || B == Op0) // A | (A & ?) --> A |
| 3649 | return ReplaceInstUsesWith(I, Op0); |
| 3650 | |
Chris Lattner | 6423d4c | 2006-07-10 20:25:24 +0000 | [diff] [blame] | 3651 | // (A | B) | C and A | (B | C) -> bswap if possible. |
| 3652 | // (A >> B) | (C << D) and (A << B) | (B >> C) -> bswap if possible. |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3653 | if (match(Op0, m_Or(m_Value(), m_Value())) || |
Chris Lattner | 6423d4c | 2006-07-10 20:25:24 +0000 | [diff] [blame] | 3654 | match(Op1, m_Or(m_Value(), m_Value())) || |
| 3655 | (match(Op0, m_Shift(m_Value(), m_Value())) && |
| 3656 | match(Op1, m_Shift(m_Value(), m_Value())))) { |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3657 | if (Instruction *BSwap = MatchBSwap(I)) |
| 3658 | return BSwap; |
| 3659 | } |
| 3660 | |
Chris Lattner | 6e4c649 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 3661 | // (X^C)|Y -> (X|Y)^C iff Y&C == 0 |
| 3662 | if (Op0->hasOneUse() && match(Op0, m_Xor(m_Value(A), m_ConstantInt(C1))) && |
Chris Lattner | 3bedbd9 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3663 | MaskedValueIsZero(Op1, C1->getZExtValue())) { |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3664 | Instruction *NOr = BinaryOperator::createOr(A, Op1); |
| 3665 | InsertNewInstBefore(NOr, I); |
| 3666 | NOr->takeName(Op0); |
| 3667 | return BinaryOperator::createXor(NOr, C1); |
Chris Lattner | 6e4c649 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 3668 | } |
| 3669 | |
| 3670 | // Y|(X^C) -> (X|Y)^C iff Y&C == 0 |
| 3671 | if (Op1->hasOneUse() && match(Op1, m_Xor(m_Value(A), m_ConstantInt(C1))) && |
Chris Lattner | 3bedbd9 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3672 | MaskedValueIsZero(Op0, C1->getZExtValue())) { |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3673 | Instruction *NOr = BinaryOperator::createOr(A, Op0); |
| 3674 | InsertNewInstBefore(NOr, I); |
| 3675 | NOr->takeName(Op0); |
| 3676 | return BinaryOperator::createXor(NOr, C1); |
Chris Lattner | 6e4c649 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 3677 | } |
| 3678 | |
Chris Lattner | e9bed7d | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 3679 | // (A & C1)|(B & C2) |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3680 | if (match(Op0, m_And(m_Value(A), m_ConstantInt(C1))) && |
Chris Lattner | e9bed7d | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 3681 | match(Op1, m_And(m_Value(B), m_ConstantInt(C2)))) { |
| 3682 | |
| 3683 | if (A == B) // (A & C1)|(A & C2) == A & (C1|C2) |
| 3684 | return BinaryOperator::createAnd(A, ConstantExpr::getOr(C1, C2)); |
| 3685 | |
| 3686 | |
Chris Lattner | 0b7c0bf | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 3687 | // If we have: ((V + N) & C1) | (V & C2) |
| 3688 | // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0 |
| 3689 | // replace with V+N. |
| 3690 | if (C1 == ConstantExpr::getNot(C2)) { |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 3691 | Value *V1 = 0, *V2 = 0; |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3692 | if ((C2->getZExtValue() & (C2->getZExtValue()+1)) == 0 && // C2 == 0+1+ |
Chris Lattner | 0b7c0bf | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 3693 | match(A, m_Add(m_Value(V1), m_Value(V2)))) { |
| 3694 | // Add commutes, try both ways. |
Chris Lattner | 3bedbd9 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3695 | if (V1 == B && MaskedValueIsZero(V2, C2->getZExtValue())) |
Chris Lattner | 0b7c0bf | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 3696 | return ReplaceInstUsesWith(I, A); |
Chris Lattner | 3bedbd9 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3697 | if (V2 == B && MaskedValueIsZero(V1, C2->getZExtValue())) |
Chris Lattner | 0b7c0bf | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 3698 | return ReplaceInstUsesWith(I, A); |
| 3699 | } |
| 3700 | // Or commutes, try both ways. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3701 | if ((C1->getZExtValue() & (C1->getZExtValue()+1)) == 0 && |
Chris Lattner | 0b7c0bf | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 3702 | match(B, m_Add(m_Value(V1), m_Value(V2)))) { |
| 3703 | // Add commutes, try both ways. |
Chris Lattner | 3bedbd9 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3704 | if (V1 == A && MaskedValueIsZero(V2, C1->getZExtValue())) |
Chris Lattner | 0b7c0bf | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 3705 | return ReplaceInstUsesWith(I, B); |
Chris Lattner | 3bedbd9 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3706 | if (V2 == A && MaskedValueIsZero(V1, C1->getZExtValue())) |
Chris Lattner | 0b7c0bf | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 3707 | return ReplaceInstUsesWith(I, B); |
Chris Lattner | e9bed7d | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 3708 | } |
| 3709 | } |
| 3710 | } |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 3711 | |
| 3712 | // (X >> Z) | (Y >> Z) -> (X|Y) >> Z for all shifts. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3713 | if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) { |
| 3714 | if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0)) |
| 3715 | if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() && |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 3716 | SI0->getOperand(1) == SI1->getOperand(1) && |
| 3717 | (SI0->hasOneUse() || SI1->hasOneUse())) { |
| 3718 | Instruction *NewOp = |
| 3719 | InsertNewInstBefore(BinaryOperator::createOr(SI0->getOperand(0), |
| 3720 | SI1->getOperand(0), |
| 3721 | SI0->getName()), I); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3722 | return BinaryOperator::create(SI1->getOpcode(), NewOp, |
| 3723 | SI1->getOperand(1)); |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 3724 | } |
| 3725 | } |
Chris Lattner | 67ca768 | 2003-08-12 19:11:07 +0000 | [diff] [blame] | 3726 | |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3727 | if (match(Op0, m_Not(m_Value(A)))) { // ~A | Op1 |
| 3728 | if (A == Op1) // ~A | A == -1 |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3729 | return ReplaceInstUsesWith(I, |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3730 | ConstantInt::getAllOnesValue(I.getType())); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3731 | } else { |
| 3732 | A = 0; |
| 3733 | } |
Chris Lattner | f4d4c87 | 2005-05-07 23:49:08 +0000 | [diff] [blame] | 3734 | // Note, A is still live here! |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3735 | if (match(Op1, m_Not(m_Value(B)))) { // Op0 | ~B |
| 3736 | if (Op0 == B) |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3737 | return ReplaceInstUsesWith(I, |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3738 | ConstantInt::getAllOnesValue(I.getType())); |
Chris Lattner | a27231a | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 3739 | |
Misha Brukman | cb6267b | 2004-07-30 12:50:08 +0000 | [diff] [blame] | 3740 | // (~A | ~B) == (~(A & B)) - De Morgan's Law |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3741 | if (A && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
| 3742 | Value *And = InsertNewInstBefore(BinaryOperator::createAnd(A, B, |
| 3743 | I.getName()+".demorgan"), I); |
| 3744 | return BinaryOperator::createNot(And); |
| 3745 | } |
Chris Lattner | a27231a | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 3746 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3747 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3748 | // (icmp1 A, B) | (icmp2 A, B) --> (icmp3 A, B) |
| 3749 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1))) { |
| 3750 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS))) |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3751 | return R; |
| 3752 | |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3753 | Value *LHSVal, *RHSVal; |
| 3754 | ConstantInt *LHSCst, *RHSCst; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3755 | ICmpInst::Predicate LHSCC, RHSCC; |
| 3756 | if (match(Op0, m_ICmp(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst)))) |
| 3757 | if (match(RHS, m_ICmp(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst)))) |
| 3758 | if (LHSVal == RHSVal && // Found (X icmp C1) | (X icmp C2) |
| 3759 | // icmp [us][gl]e x, cst is folded to icmp [us][gl]t elsewhere. |
| 3760 | LHSCC != ICmpInst::ICMP_UGE && LHSCC != ICmpInst::ICMP_ULE && |
| 3761 | RHSCC != ICmpInst::ICMP_UGE && RHSCC != ICmpInst::ICMP_ULE && |
| 3762 | LHSCC != ICmpInst::ICMP_SGE && LHSCC != ICmpInst::ICMP_SLE && |
| 3763 | RHSCC != ICmpInst::ICMP_SGE && RHSCC != ICmpInst::ICMP_SLE) { |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3764 | // Ensure that the larger constant is on the RHS. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3765 | ICmpInst::Predicate GT = ICmpInst::isSignedPredicate(LHSCC) ? |
| 3766 | ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; |
| 3767 | Constant *Cmp = ConstantExpr::getICmp(GT, LHSCst, RHSCst); |
| 3768 | ICmpInst *LHS = cast<ICmpInst>(Op0); |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 3769 | if (cast<ConstantInt>(Cmp)->getZExtValue()) { |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3770 | std::swap(LHS, RHS); |
| 3771 | std::swap(LHSCst, RHSCst); |
| 3772 | std::swap(LHSCC, RHSCC); |
| 3773 | } |
| 3774 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3775 | // At this point, we know we have have two icmp instructions |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3776 | // comparing a value against two constants and or'ing the result |
| 3777 | // together. Because of the above check, we know that we only have |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3778 | // ICMP_EQ, ICMP_NE, ICMP_LT, and ICMP_GT here. We also know (from the |
| 3779 | // FoldICmpLogical check above), that the two constants are not |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3780 | // equal. |
| 3781 | assert(LHSCst != RHSCst && "Compares not folded above?"); |
| 3782 | |
| 3783 | switch (LHSCC) { |
| 3784 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3785 | case ICmpInst::ICMP_EQ: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3786 | switch (RHSCC) { |
| 3787 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3788 | case ICmpInst::ICMP_EQ: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3789 | if (LHSCst == SubOne(RHSCst)) {// (X == 13 | X == 14) -> X-13 <u 2 |
| 3790 | Constant *AddCST = ConstantExpr::getNeg(LHSCst); |
| 3791 | Instruction *Add = BinaryOperator::createAdd(LHSVal, AddCST, |
| 3792 | LHSVal->getName()+".off"); |
| 3793 | InsertNewInstBefore(Add, I); |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3794 | AddCST = ConstantExpr::getSub(AddOne(RHSCst), LHSCst); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3795 | return new ICmpInst(ICmpInst::ICMP_ULT, Add, AddCST); |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3796 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3797 | break; // (X == 13 | X == 15) -> no change |
| 3798 | case ICmpInst::ICMP_UGT: // (X == 13 | X u> 14) -> no change |
| 3799 | case ICmpInst::ICMP_SGT: // (X == 13 | X s> 14) -> no change |
Chris Lattner | 240d6f4 | 2005-04-19 06:04:18 +0000 | [diff] [blame] | 3800 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3801 | case ICmpInst::ICMP_NE: // (X == 13 | X != 15) -> X != 15 |
| 3802 | case ICmpInst::ICMP_ULT: // (X == 13 | X u< 15) -> X u< 15 |
| 3803 | case ICmpInst::ICMP_SLT: // (X == 13 | X s< 15) -> X s< 15 |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3804 | return ReplaceInstUsesWith(I, RHS); |
| 3805 | } |
| 3806 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3807 | case ICmpInst::ICMP_NE: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3808 | switch (RHSCC) { |
| 3809 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3810 | case ICmpInst::ICMP_EQ: // (X != 13 | X == 15) -> X != 13 |
| 3811 | case ICmpInst::ICMP_UGT: // (X != 13 | X u> 15) -> X != 13 |
| 3812 | case ICmpInst::ICMP_SGT: // (X != 13 | X s> 15) -> X != 13 |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3813 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3814 | case ICmpInst::ICMP_NE: // (X != 13 | X != 15) -> true |
| 3815 | case ICmpInst::ICMP_ULT: // (X != 13 | X u< 15) -> true |
| 3816 | case ICmpInst::ICMP_SLT: // (X != 13 | X s< 15) -> true |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3817 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3818 | } |
| 3819 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3820 | case ICmpInst::ICMP_ULT: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3821 | switch (RHSCC) { |
| 3822 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3823 | case ICmpInst::ICMP_EQ: // (X u< 13 | X == 14) -> no change |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3824 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3825 | case ICmpInst::ICMP_UGT: // (X u< 13 | X u> 15) ->(X-13) u> 2 |
| 3826 | return InsertRangeTest(LHSVal, LHSCst, AddOne(RHSCst), false, |
| 3827 | false, I); |
| 3828 | case ICmpInst::ICMP_SGT: // (X u< 13 | X s> 15) -> no change |
| 3829 | break; |
| 3830 | case ICmpInst::ICMP_NE: // (X u< 13 | X != 15) -> X != 15 |
| 3831 | 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] | 3832 | return ReplaceInstUsesWith(I, RHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3833 | case ICmpInst::ICMP_SLT: // (X u< 13 | X s< 15) -> no change |
| 3834 | break; |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3835 | } |
| 3836 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3837 | case ICmpInst::ICMP_SLT: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3838 | switch (RHSCC) { |
| 3839 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3840 | case ICmpInst::ICMP_EQ: // (X s< 13 | X == 14) -> no change |
| 3841 | break; |
| 3842 | case ICmpInst::ICMP_SGT: // (X s< 13 | X s> 15) ->(X-13) s> 2 |
| 3843 | return InsertRangeTest(LHSVal, LHSCst, AddOne(RHSCst), true, |
| 3844 | false, I); |
| 3845 | case ICmpInst::ICMP_UGT: // (X s< 13 | X u> 15) -> no change |
| 3846 | break; |
| 3847 | case ICmpInst::ICMP_NE: // (X s< 13 | X != 15) -> X != 15 |
| 3848 | case ICmpInst::ICMP_SLT: // (X s< 13 | X s< 15) -> X s< 15 |
| 3849 | return ReplaceInstUsesWith(I, RHS); |
| 3850 | case ICmpInst::ICMP_ULT: // (X s< 13 | X u< 15) -> no change |
| 3851 | break; |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3852 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3853 | break; |
| 3854 | case ICmpInst::ICMP_UGT: |
| 3855 | switch (RHSCC) { |
| 3856 | default: assert(0 && "Unknown integer condition code!"); |
| 3857 | case ICmpInst::ICMP_EQ: // (X u> 13 | X == 15) -> X u> 13 |
| 3858 | case ICmpInst::ICMP_UGT: // (X u> 13 | X u> 15) -> X u> 13 |
| 3859 | return ReplaceInstUsesWith(I, LHS); |
| 3860 | case ICmpInst::ICMP_SGT: // (X u> 13 | X s> 15) -> no change |
| 3861 | break; |
| 3862 | case ICmpInst::ICMP_NE: // (X u> 13 | X != 15) -> true |
| 3863 | case ICmpInst::ICMP_ULT: // (X u> 13 | X u< 15) -> true |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3864 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3865 | case ICmpInst::ICMP_SLT: // (X u> 13 | X s< 15) -> no change |
| 3866 | break; |
| 3867 | } |
| 3868 | break; |
| 3869 | case ICmpInst::ICMP_SGT: |
| 3870 | switch (RHSCC) { |
| 3871 | default: assert(0 && "Unknown integer condition code!"); |
| 3872 | case ICmpInst::ICMP_EQ: // (X s> 13 | X == 15) -> X > 13 |
| 3873 | case ICmpInst::ICMP_SGT: // (X s> 13 | X s> 15) -> X > 13 |
| 3874 | return ReplaceInstUsesWith(I, LHS); |
| 3875 | case ICmpInst::ICMP_UGT: // (X s> 13 | X u> 15) -> no change |
| 3876 | break; |
| 3877 | case ICmpInst::ICMP_NE: // (X s> 13 | X != 15) -> true |
| 3878 | case ICmpInst::ICMP_SLT: // (X s> 13 | X s< 15) -> true |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3879 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3880 | case ICmpInst::ICMP_ULT: // (X s> 13 | X u< 15) -> no change |
| 3881 | break; |
| 3882 | } |
| 3883 | break; |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3884 | } |
| 3885 | } |
| 3886 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3887 | |
| 3888 | // fold (or (cast A), (cast B)) -> (cast (or A, B)) |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3889 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3890 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3891 | if (Op0C->getOpcode() == Op1C->getOpcode()) {// same cast kind ? |
| 3892 | const Type *SrcTy = Op0C->getOperand(0)->getType(); |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 3893 | if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() && |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3894 | // 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] | 3895 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), |
| 3896 | I.getType(), TD) && |
| 3897 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), |
| 3898 | I.getType(), TD)) { |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3899 | Instruction *NewOp = BinaryOperator::createOr(Op0C->getOperand(0), |
| 3900 | Op1C->getOperand(0), |
| 3901 | I.getName()); |
| 3902 | InsertNewInstBefore(NewOp, I); |
| 3903 | return CastInst::create(Op0C->getOpcode(), NewOp, I.getType()); |
| 3904 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3905 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3906 | |
Chris Lattner | e9bed7d | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 3907 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3908 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3909 | } |
| 3910 | |
Chris Lattner | c317d39 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 3911 | // XorSelf - Implements: X ^ X --> 0 |
| 3912 | struct XorSelf { |
| 3913 | Value *RHS; |
| 3914 | XorSelf(Value *rhs) : RHS(rhs) {} |
| 3915 | bool shouldApply(Value *LHS) const { return LHS == RHS; } |
| 3916 | Instruction *apply(BinaryOperator &Xor) const { |
| 3917 | return &Xor; |
| 3918 | } |
| 3919 | }; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3920 | |
| 3921 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3922 | Instruction *InstCombiner::visitXor(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 3923 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3924 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3925 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3926 | if (isa<UndefValue>(Op1)) |
| 3927 | return ReplaceInstUsesWith(I, Op1); // X ^ undef -> undef |
| 3928 | |
Chris Lattner | c317d39 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 3929 | // xor X, X = 0, even if X is nested in a sequence of Xor's. |
| 3930 | if (Instruction *Result = AssociativeOpt(I, XorSelf(Op1))) { |
| 3931 | assert(Result == &I && "AssociativeOpt didn't work?"); |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 3932 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | c317d39 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 3933 | } |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3934 | |
| 3935 | // See if we can simplify any instructions used by the instruction whose sole |
| 3936 | // purpose is to compute bits we don't care about. |
| 3937 | uint64_t KnownZero, KnownOne; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 3938 | if (!isa<VectorType>(I.getType()) && |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 3939 | SimplifyDemandedBits(&I, cast<IntegerType>(I.getType())->getBitMask(), |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3940 | KnownZero, KnownOne)) |
| 3941 | return &I; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3942 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3943 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3944 | // xor (icmp A, B), true = not (icmp A, B) = !icmp A, B |
| 3945 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(Op0)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3946 | if (RHS == ConstantInt::getTrue() && ICI->hasOneUse()) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3947 | return new ICmpInst(ICI->getInversePredicate(), |
| 3948 | ICI->getOperand(0), ICI->getOperand(1)); |
Chris Lattner | ad5b4fb | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 3949 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3950 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) { |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 3951 | // ~(c-X) == X-c-1 == X+(-c-1) |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3952 | if (Op0I->getOpcode() == Instruction::Sub && RHS->isAllOnesValue()) |
| 3953 | if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3954 | Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C); |
| 3955 | Constant *ConstantRHS = ConstantExpr::getSub(NegOp0I0C, |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3956 | ConstantInt::get(I.getType(), 1)); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3957 | return BinaryOperator::createAdd(Op0I->getOperand(1), ConstantRHS); |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3958 | } |
Chris Lattner | 5b62aa7 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 3959 | |
| 3960 | // ~(~X & Y) --> (X | ~Y) |
| 3961 | if (Op0I->getOpcode() == Instruction::And && RHS->isAllOnesValue()) { |
| 3962 | if (dyn_castNotVal(Op0I->getOperand(1))) Op0I->swapOperands(); |
| 3963 | if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0))) { |
| 3964 | Instruction *NotY = |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3965 | BinaryOperator::createNot(Op0I->getOperand(1), |
Chris Lattner | 5b62aa7 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 3966 | Op0I->getOperand(1)->getName()+".not"); |
| 3967 | InsertNewInstBefore(NotY, I); |
| 3968 | return BinaryOperator::createOr(Op0NotVal, NotY); |
| 3969 | } |
| 3970 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3971 | |
Chris Lattner | eca0c5c | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 3972 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3973 | if (Op0I->getOpcode() == Instruction::Add) { |
Chris Lattner | 689d24b | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 3974 | // ~(X-c) --> (-c-1)-X |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3975 | if (RHS->isAllOnesValue()) { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3976 | Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI); |
| 3977 | return BinaryOperator::createSub( |
| 3978 | ConstantExpr::getSub(NegOp0CI, |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3979 | ConstantInt::get(I.getType(), 1)), |
Chris Lattner | 689d24b | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 3980 | Op0I->getOperand(0)); |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3981 | } |
Chris Lattner | 02bd1b3 | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 3982 | } else if (Op0I->getOpcode() == Instruction::Or) { |
| 3983 | // (X|C1)^C2 -> X^(C1|C2) iff X&~C1 == 0 |
| 3984 | if (MaskedValueIsZero(Op0I->getOperand(0), Op0CI->getZExtValue())) { |
| 3985 | Constant *NewRHS = ConstantExpr::getOr(Op0CI, RHS); |
| 3986 | // Anything in both C1 and C2 is known to be zero, remove it from |
| 3987 | // NewRHS. |
| 3988 | Constant *CommonBits = ConstantExpr::getAnd(Op0CI, RHS); |
| 3989 | NewRHS = ConstantExpr::getAnd(NewRHS, |
| 3990 | ConstantExpr::getNot(CommonBits)); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 3991 | AddToWorkList(Op0I); |
Chris Lattner | 02bd1b3 | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 3992 | I.setOperand(0, Op0I->getOperand(0)); |
| 3993 | I.setOperand(1, NewRHS); |
| 3994 | return &I; |
| 3995 | } |
Chris Lattner | eca0c5c | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 3996 | } |
Chris Lattner | 05bd1b2 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 3997 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3998 | |
| 3999 | // Try to fold constant and into select arguments. |
| 4000 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 4001 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 4002 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 4003 | if (isa<PHINode>(Op0)) |
| 4004 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 4005 | return NV; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4006 | } |
| 4007 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 4008 | if (Value *X = dyn_castNotVal(Op0)) // ~A ^ A == -1 |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4009 | if (X == Op1) |
| 4010 | return ReplaceInstUsesWith(I, |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4011 | ConstantInt::getAllOnesValue(I.getType())); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4012 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 4013 | if (Value *X = dyn_castNotVal(Op1)) // A ^ ~A == -1 |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4014 | if (X == Op0) |
| 4015 | return ReplaceInstUsesWith(I, |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4016 | ConstantInt::getAllOnesValue(I.getType())); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4017 | |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4018 | if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) |
Chris Lattner | 26ca7e1 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 4019 | if (Op1I->getOpcode() == Instruction::Or) { |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4020 | if (Op1I->getOperand(0) == Op0) { // B^(B|A) == (A|B)^B |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4021 | Op1I->swapOperands(); |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4022 | I.swapOperands(); |
| 4023 | std::swap(Op0, Op1); |
| 4024 | } else if (Op1I->getOperand(1) == Op0) { // B^(A|B) == (A|B)^B |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4025 | I.swapOperands(); // Simplified below. |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4026 | std::swap(Op0, Op1); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4027 | } |
Chris Lattner | 26ca7e1 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 4028 | } else if (Op1I->getOpcode() == Instruction::Xor) { |
| 4029 | if (Op0 == Op1I->getOperand(0)) // A^(A^B) == B |
| 4030 | return ReplaceInstUsesWith(I, Op1I->getOperand(1)); |
| 4031 | else if (Op0 == Op1I->getOperand(1)) // A^(B^A) == B |
| 4032 | return ReplaceInstUsesWith(I, Op1I->getOperand(0)); |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4033 | } else if (Op1I->getOpcode() == Instruction::And && Op1I->hasOneUse()) { |
| 4034 | if (Op1I->getOperand(0) == Op0) // A^(A&B) -> A^(B&A) |
| 4035 | Op1I->swapOperands(); |
| 4036 | if (Op0 == Op1I->getOperand(1)) { // A^(B&A) -> (B&A)^A |
| 4037 | I.swapOperands(); // Simplified below. |
| 4038 | std::swap(Op0, Op1); |
| 4039 | } |
Chris Lattner | 26ca7e1 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 4040 | } |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4041 | |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4042 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 4043 | if (Op0I->getOpcode() == Instruction::Or && Op0I->hasOneUse()) { |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4044 | if (Op0I->getOperand(0) == Op1) // (B|A)^B == (A|B)^B |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4045 | Op0I->swapOperands(); |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 4046 | if (Op0I->getOperand(1) == Op1) { // (A|B)^B == A & ~B |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4047 | Instruction *NotB = BinaryOperator::createNot(Op1, "tmp"); |
| 4048 | InsertNewInstBefore(NotB, I); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4049 | return BinaryOperator::createAnd(Op0I->getOperand(0), NotB); |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4050 | } |
Chris Lattner | 26ca7e1 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 4051 | } else if (Op0I->getOpcode() == Instruction::Xor) { |
| 4052 | if (Op1 == Op0I->getOperand(0)) // (A^B)^A == B |
| 4053 | return ReplaceInstUsesWith(I, Op0I->getOperand(1)); |
| 4054 | else if (Op1 == Op0I->getOperand(1)) // (B^A)^A == B |
| 4055 | return ReplaceInstUsesWith(I, Op0I->getOperand(0)); |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4056 | } else if (Op0I->getOpcode() == Instruction::And && Op0I->hasOneUse()) { |
| 4057 | if (Op0I->getOperand(0) == Op1) // (A&B)^A -> (B&A)^A |
| 4058 | Op0I->swapOperands(); |
Chris Lattner | ae1ab39 | 2006-04-01 22:05:01 +0000 | [diff] [blame] | 4059 | if (Op0I->getOperand(1) == Op1 && // (B&A)^A == ~B & A |
| 4060 | !isa<ConstantInt>(Op1)) { // Canonical form is (B&C)^C |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4061 | Instruction *N = BinaryOperator::createNot(Op0I->getOperand(0), "tmp"); |
| 4062 | InsertNewInstBefore(N, I); |
| 4063 | return BinaryOperator::createAnd(N, Op1); |
| 4064 | } |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4065 | } |
| 4066 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4067 | // (icmp1 A, B) ^ (icmp2 A, B) --> (icmp3 A, B) |
| 4068 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1))) |
| 4069 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS))) |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 4070 | return R; |
| 4071 | |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4072 | // fold (xor (cast A), (cast B)) -> (cast (xor A, B)) |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4073 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4074 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4075 | if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind? |
| 4076 | const Type *SrcTy = Op0C->getOperand(0)->getType(); |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 4077 | if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() && |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4078 | // 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] | 4079 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), |
| 4080 | I.getType(), TD) && |
| 4081 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), |
| 4082 | I.getType(), TD)) { |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4083 | Instruction *NewOp = BinaryOperator::createXor(Op0C->getOperand(0), |
| 4084 | Op1C->getOperand(0), |
| 4085 | I.getName()); |
| 4086 | InsertNewInstBefore(NewOp, I); |
| 4087 | return CastInst::create(Op0C->getOpcode(), NewOp, I.getType()); |
| 4088 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4089 | } |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4090 | |
| 4091 | // (X >> Z) ^ (Y >> Z) -> (X^Y) >> Z for all shifts. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4092 | if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) { |
| 4093 | if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0)) |
| 4094 | if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() && |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4095 | SI0->getOperand(1) == SI1->getOperand(1) && |
| 4096 | (SI0->hasOneUse() || SI1->hasOneUse())) { |
| 4097 | Instruction *NewOp = |
| 4098 | InsertNewInstBefore(BinaryOperator::createXor(SI0->getOperand(0), |
| 4099 | SI1->getOperand(0), |
| 4100 | SI0->getName()), I); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4101 | return BinaryOperator::create(SI1->getOpcode(), NewOp, |
| 4102 | SI1->getOperand(1)); |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4103 | } |
| 4104 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4105 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4106 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4107 | } |
| 4108 | |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4109 | static bool isPositive(ConstantInt *C) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4110 | return C->getSExtValue() >= 0; |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4111 | } |
| 4112 | |
| 4113 | /// AddWithOverflow - Compute Result = In1+In2, returning true if the result |
| 4114 | /// overflowed for this type. |
| 4115 | static bool AddWithOverflow(ConstantInt *&Result, ConstantInt *In1, |
| 4116 | ConstantInt *In2) { |
| 4117 | Result = cast<ConstantInt>(ConstantExpr::getAdd(In1, In2)); |
| 4118 | |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 4119 | return cast<ConstantInt>(Result)->getZExtValue() < |
| 4120 | cast<ConstantInt>(In1)->getZExtValue(); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4121 | } |
| 4122 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4123 | /// EmitGEPOffset - Given a getelementptr instruction/constantexpr, emit the |
| 4124 | /// code necessary to compute the offset from the base pointer (without adding |
| 4125 | /// in the base pointer). Return the result as a signed integer of intptr size. |
| 4126 | static Value *EmitGEPOffset(User *GEP, Instruction &I, InstCombiner &IC) { |
| 4127 | TargetData &TD = IC.getTargetData(); |
| 4128 | gep_type_iterator GTI = gep_type_begin(GEP); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4129 | const Type *IntPtrTy = TD.getIntPtrType(); |
| 4130 | Value *Result = Constant::getNullValue(IntPtrTy); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4131 | |
| 4132 | // Build a mask for high order bits. |
Chris Lattner | 1a074fc | 2006-02-07 07:00:41 +0000 | [diff] [blame] | 4133 | uint64_t PtrSizeMask = ~0ULL >> (64-TD.getPointerSize()*8); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4134 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4135 | for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i, ++GTI) { |
| 4136 | Value *Op = GEP->getOperand(i); |
Chris Lattner | 0b84c80 | 2005-01-13 23:26:48 +0000 | [diff] [blame] | 4137 | uint64_t Size = TD.getTypeSize(GTI.getIndexedType()) & PtrSizeMask; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4138 | Constant *Scale = ConstantInt::get(IntPtrTy, Size); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4139 | if (Constant *OpC = dyn_cast<Constant>(Op)) { |
| 4140 | if (!OpC->isNullValue()) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4141 | OpC = ConstantExpr::getIntegerCast(OpC, IntPtrTy, true /*SExt*/); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4142 | Scale = ConstantExpr::getMul(OpC, Scale); |
| 4143 | if (Constant *RC = dyn_cast<Constant>(Result)) |
| 4144 | Result = ConstantExpr::getAdd(RC, Scale); |
| 4145 | else { |
| 4146 | // Emit an add instruction. |
| 4147 | Result = IC.InsertNewInstBefore( |
| 4148 | BinaryOperator::createAdd(Result, Scale, |
| 4149 | GEP->getName()+".offs"), I); |
| 4150 | } |
| 4151 | } |
| 4152 | } else { |
Chris Lattner | 6f7f02f | 2005-01-14 17:17:59 +0000 | [diff] [blame] | 4153 | // Convert to correct type. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4154 | Op = IC.InsertNewInstBefore(CastInst::createSExtOrBitCast(Op, IntPtrTy, |
Chris Lattner | 6f7f02f | 2005-01-14 17:17:59 +0000 | [diff] [blame] | 4155 | Op->getName()+".c"), I); |
| 4156 | if (Size != 1) |
Chris Lattner | 5bdf04c | 2005-01-13 20:40:58 +0000 | [diff] [blame] | 4157 | // We'll let instcombine(mul) convert this to a shl if possible. |
| 4158 | Op = IC.InsertNewInstBefore(BinaryOperator::createMul(Op, Scale, |
| 4159 | GEP->getName()+".idx"), I); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4160 | |
| 4161 | // Emit an add instruction. |
Chris Lattner | 5bdf04c | 2005-01-13 20:40:58 +0000 | [diff] [blame] | 4162 | Result = IC.InsertNewInstBefore(BinaryOperator::createAdd(Op, Result, |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4163 | GEP->getName()+".offs"), I); |
| 4164 | } |
| 4165 | } |
| 4166 | return Result; |
| 4167 | } |
| 4168 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4169 | /// FoldGEPICmp - Fold comparisons between a GEP instruction and something |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4170 | /// 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] | 4171 | Instruction *InstCombiner::FoldGEPICmp(User *GEPLHS, Value *RHS, |
| 4172 | ICmpInst::Predicate Cond, |
| 4173 | Instruction &I) { |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4174 | assert(dyn_castGetElementPtr(GEPLHS) && "LHS is not a getelementptr!"); |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4175 | |
| 4176 | if (CastInst *CI = dyn_cast<CastInst>(RHS)) |
| 4177 | if (isa<PointerType>(CI->getOperand(0)->getType())) |
| 4178 | RHS = CI->getOperand(0); |
| 4179 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4180 | Value *PtrBase = GEPLHS->getOperand(0); |
| 4181 | if (PtrBase == RHS) { |
| 4182 | // 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] | 4183 | // OFFSET if this is a icmp_eq or icmp_ne comparison, just return whether |
| 4184 | // each index is zero or not. |
| 4185 | if (Cond == ICmpInst::ICMP_EQ || Cond == ICmpInst::ICMP_NE) { |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4186 | Instruction *InVal = 0; |
Chris Lattner | ad5fec1 | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 4187 | gep_type_iterator GTI = gep_type_begin(GEPLHS); |
| 4188 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i, ++GTI) { |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4189 | bool EmitIt = true; |
| 4190 | if (Constant *C = dyn_cast<Constant>(GEPLHS->getOperand(i))) { |
| 4191 | if (isa<UndefValue>(C)) // undef index -> undef. |
| 4192 | return ReplaceInstUsesWith(I, UndefValue::get(I.getType())); |
| 4193 | if (C->isNullValue()) |
| 4194 | EmitIt = false; |
Chris Lattner | ad5fec1 | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 4195 | else if (TD->getTypeSize(GTI.getIndexedType()) == 0) { |
| 4196 | EmitIt = false; // This is indexing into a zero sized array? |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4197 | } else if (isa<ConstantInt>(C)) |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4198 | return ReplaceInstUsesWith(I, // No comparison is needed here. |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4199 | ConstantInt::get(Type::Int1Ty, |
| 4200 | Cond == ICmpInst::ICMP_NE)); |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4201 | } |
| 4202 | |
| 4203 | if (EmitIt) { |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4204 | Instruction *Comp = |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4205 | new ICmpInst(Cond, GEPLHS->getOperand(i), |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4206 | Constant::getNullValue(GEPLHS->getOperand(i)->getType())); |
| 4207 | if (InVal == 0) |
| 4208 | InVal = Comp; |
| 4209 | else { |
| 4210 | InVal = InsertNewInstBefore(InVal, I); |
| 4211 | InsertNewInstBefore(Comp, I); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4212 | if (Cond == ICmpInst::ICMP_NE) // True if any are unequal |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4213 | InVal = BinaryOperator::createOr(InVal, Comp); |
| 4214 | else // True if all are equal |
| 4215 | InVal = BinaryOperator::createAnd(InVal, Comp); |
| 4216 | } |
| 4217 | } |
| 4218 | } |
| 4219 | |
| 4220 | if (InVal) |
| 4221 | return InVal; |
| 4222 | else |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4223 | // No comparison is needed here, all indexes = 0 |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4224 | ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
| 4225 | Cond == ICmpInst::ICMP_EQ)); |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4226 | } |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4227 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4228 | // 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] | 4229 | // the result to fold to a constant! |
| 4230 | if (isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) { |
| 4231 | // ((gep Ptr, OFFSET) cmp Ptr) ---> (OFFSET cmp 0). |
| 4232 | Value *Offset = EmitGEPOffset(GEPLHS, I, *this); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4233 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), Offset, |
| 4234 | Constant::getNullValue(Offset->getType())); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4235 | } |
| 4236 | } else if (User *GEPRHS = dyn_castGetElementPtr(RHS)) { |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 4237 | // If the base pointers are different, but the indices are the same, just |
| 4238 | // compare the base pointer. |
| 4239 | if (PtrBase != GEPRHS->getOperand(0)) { |
| 4240 | bool IndicesTheSame = GEPLHS->getNumOperands()==GEPRHS->getNumOperands(); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 4241 | IndicesTheSame &= GEPLHS->getOperand(0)->getType() == |
Chris Lattner | 93b94a6 | 2005-04-26 14:40:41 +0000 | [diff] [blame] | 4242 | GEPRHS->getOperand(0)->getType(); |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 4243 | if (IndicesTheSame) |
| 4244 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i) |
| 4245 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { |
| 4246 | IndicesTheSame = false; |
| 4247 | break; |
| 4248 | } |
| 4249 | |
| 4250 | // If all indices are the same, just compare the base pointers. |
| 4251 | if (IndicesTheSame) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4252 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), |
| 4253 | GEPLHS->getOperand(0), GEPRHS->getOperand(0)); |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 4254 | |
| 4255 | // Otherwise, the base pointers are different and the indices are |
| 4256 | // different, bail out. |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4257 | return 0; |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 4258 | } |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4259 | |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4260 | // If one of the GEPs has all zero indices, recurse. |
| 4261 | bool AllZeros = true; |
| 4262 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i) |
| 4263 | if (!isa<Constant>(GEPLHS->getOperand(i)) || |
| 4264 | !cast<Constant>(GEPLHS->getOperand(i))->isNullValue()) { |
| 4265 | AllZeros = false; |
| 4266 | break; |
| 4267 | } |
| 4268 | if (AllZeros) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4269 | return FoldGEPICmp(GEPRHS, GEPLHS->getOperand(0), |
| 4270 | ICmpInst::getSwappedPredicate(Cond), I); |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 4271 | |
| 4272 | // If the other GEP has all zero indices, recurse. |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4273 | AllZeros = true; |
| 4274 | for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i) |
| 4275 | if (!isa<Constant>(GEPRHS->getOperand(i)) || |
| 4276 | !cast<Constant>(GEPRHS->getOperand(i))->isNullValue()) { |
| 4277 | AllZeros = false; |
| 4278 | break; |
| 4279 | } |
| 4280 | if (AllZeros) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4281 | return FoldGEPICmp(GEPLHS, GEPRHS->getOperand(0), Cond, I); |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4282 | |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 4283 | if (GEPLHS->getNumOperands() == GEPRHS->getNumOperands()) { |
| 4284 | // If the GEPs only differ by one index, compare it. |
| 4285 | unsigned NumDifferences = 0; // Keep track of # differences. |
| 4286 | unsigned DiffOperand = 0; // The operand that differs. |
| 4287 | for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i) |
| 4288 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4289 | if (GEPLHS->getOperand(i)->getType()->getPrimitiveSizeInBits() != |
| 4290 | GEPRHS->getOperand(i)->getType()->getPrimitiveSizeInBits()) { |
Chris Lattner | 45f57b8 | 2005-01-21 23:06:49 +0000 | [diff] [blame] | 4291 | // Irreconcilable differences. |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 4292 | NumDifferences = 2; |
| 4293 | break; |
| 4294 | } else { |
| 4295 | if (NumDifferences++) break; |
| 4296 | DiffOperand = i; |
| 4297 | } |
| 4298 | } |
| 4299 | |
| 4300 | if (NumDifferences == 0) // SAME GEP? |
| 4301 | return ReplaceInstUsesWith(I, // No comparison is needed here. |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4302 | ConstantInt::get(Type::Int1Ty, |
| 4303 | Cond == ICmpInst::ICMP_EQ)); |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 4304 | else if (NumDifferences == 1) { |
Chris Lattner | 45f57b8 | 2005-01-21 23:06:49 +0000 | [diff] [blame] | 4305 | Value *LHSV = GEPLHS->getOperand(DiffOperand); |
| 4306 | Value *RHSV = GEPRHS->getOperand(DiffOperand); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4307 | // Make sure we do a signed comparison here. |
| 4308 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), LHSV, RHSV); |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 4309 | } |
| 4310 | } |
| 4311 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4312 | // 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] | 4313 | // the result to fold to a constant! |
| 4314 | if ((isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) && |
| 4315 | (isa<ConstantExpr>(GEPRHS) || GEPRHS->hasOneUse())) { |
| 4316 | // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) ---> (OFFSET1 cmp OFFSET2) |
| 4317 | Value *L = EmitGEPOffset(GEPLHS, I, *this); |
| 4318 | Value *R = EmitGEPOffset(GEPRHS, I, *this); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4319 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), L, R); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4320 | } |
| 4321 | } |
| 4322 | return 0; |
| 4323 | } |
| 4324 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4325 | Instruction *InstCombiner::visitFCmpInst(FCmpInst &I) { |
| 4326 | bool Changed = SimplifyCompare(I); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4327 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4328 | |
Chris Lattner | 58e9746 | 2007-01-14 19:42:17 +0000 | [diff] [blame] | 4329 | // Fold trivial predicates. |
| 4330 | if (I.getPredicate() == FCmpInst::FCMP_FALSE) |
| 4331 | return ReplaceInstUsesWith(I, Constant::getNullValue(Type::Int1Ty)); |
| 4332 | if (I.getPredicate() == FCmpInst::FCMP_TRUE) |
| 4333 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 1)); |
| 4334 | |
| 4335 | // Simplify 'fcmp pred X, X' |
| 4336 | if (Op0 == Op1) { |
| 4337 | switch (I.getPredicate()) { |
| 4338 | default: assert(0 && "Unknown predicate!"); |
| 4339 | case FCmpInst::FCMP_UEQ: // True if unordered or equal |
| 4340 | case FCmpInst::FCMP_UGE: // True if unordered, greater than, or equal |
| 4341 | case FCmpInst::FCMP_ULE: // True if unordered, less than, or equal |
| 4342 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 1)); |
| 4343 | case FCmpInst::FCMP_OGT: // True if ordered and greater than |
| 4344 | case FCmpInst::FCMP_OLT: // True if ordered and less than |
| 4345 | case FCmpInst::FCMP_ONE: // True if ordered and operands are unequal |
| 4346 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 0)); |
| 4347 | |
| 4348 | case FCmpInst::FCMP_UNO: // True if unordered: isnan(X) | isnan(Y) |
| 4349 | case FCmpInst::FCMP_ULT: // True if unordered or less than |
| 4350 | case FCmpInst::FCMP_UGT: // True if unordered or greater than |
| 4351 | case FCmpInst::FCMP_UNE: // True if unordered or not equal |
| 4352 | // Canonicalize these to be 'fcmp uno %X, 0.0'. |
| 4353 | I.setPredicate(FCmpInst::FCMP_UNO); |
| 4354 | I.setOperand(1, Constant::getNullValue(Op0->getType())); |
| 4355 | return &I; |
| 4356 | |
| 4357 | case FCmpInst::FCMP_ORD: // True if ordered (no nans) |
| 4358 | case FCmpInst::FCMP_OEQ: // True if ordered and equal |
| 4359 | case FCmpInst::FCMP_OGE: // True if ordered and greater than or equal |
| 4360 | case FCmpInst::FCMP_OLE: // True if ordered and less than or equal |
| 4361 | // Canonicalize these to be 'fcmp ord %X, 0.0'. |
| 4362 | I.setPredicate(FCmpInst::FCMP_ORD); |
| 4363 | I.setOperand(1, Constant::getNullValue(Op0->getType())); |
| 4364 | return &I; |
| 4365 | } |
| 4366 | } |
| 4367 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4368 | if (isa<UndefValue>(Op1)) // fcmp pred X, undef -> undef |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 4369 | return ReplaceInstUsesWith(I, UndefValue::get(Type::Int1Ty)); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4370 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4371 | // Handle fcmp with constant RHS |
| 4372 | if (Constant *RHSC = dyn_cast<Constant>(Op1)) { |
| 4373 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
| 4374 | switch (LHSI->getOpcode()) { |
| 4375 | case Instruction::PHI: |
| 4376 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 4377 | return NV; |
| 4378 | break; |
| 4379 | case Instruction::Select: |
| 4380 | // If either operand of the select is a constant, we can fold the |
| 4381 | // comparison into the select arms, which will cause one to be |
| 4382 | // constant folded and the select turned into a bitwise or. |
| 4383 | Value *Op1 = 0, *Op2 = 0; |
| 4384 | if (LHSI->hasOneUse()) { |
| 4385 | if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) { |
| 4386 | // Fold the known value into the constant operand. |
| 4387 | Op1 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC); |
| 4388 | // Insert a new FCmp of the other select operand. |
| 4389 | Op2 = InsertNewInstBefore(new FCmpInst(I.getPredicate(), |
| 4390 | LHSI->getOperand(2), RHSC, |
| 4391 | I.getName()), I); |
| 4392 | } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) { |
| 4393 | // Fold the known value into the constant operand. |
| 4394 | Op2 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC); |
| 4395 | // Insert a new FCmp of the other select operand. |
| 4396 | Op1 = InsertNewInstBefore(new FCmpInst(I.getPredicate(), |
| 4397 | LHSI->getOperand(1), RHSC, |
| 4398 | I.getName()), I); |
| 4399 | } |
| 4400 | } |
| 4401 | |
| 4402 | if (Op1) |
| 4403 | return new SelectInst(LHSI->getOperand(0), Op1, Op2); |
| 4404 | break; |
| 4405 | } |
| 4406 | } |
| 4407 | |
| 4408 | return Changed ? &I : 0; |
| 4409 | } |
| 4410 | |
| 4411 | Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { |
| 4412 | bool Changed = SimplifyCompare(I); |
| 4413 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 4414 | const Type *Ty = Op0->getType(); |
| 4415 | |
| 4416 | // icmp X, X |
| 4417 | if (Op0 == Op1) |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4418 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
| 4419 | isTrueWhenEqual(I))); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4420 | |
| 4421 | if (isa<UndefValue>(Op1)) // X icmp undef -> undef |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 4422 | return ReplaceInstUsesWith(I, UndefValue::get(Type::Int1Ty)); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4423 | |
| 4424 | // icmp of GlobalValues can never equal each other as long as they aren't |
| 4425 | // external weak linkage type. |
| 4426 | if (GlobalValue *GV0 = dyn_cast<GlobalValue>(Op0)) |
| 4427 | if (GlobalValue *GV1 = dyn_cast<GlobalValue>(Op1)) |
| 4428 | if (!GV0->hasExternalWeakLinkage() || !GV1->hasExternalWeakLinkage()) |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4429 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
| 4430 | !isTrueWhenEqual(I))); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4431 | |
| 4432 | // icmp <global/alloca*/null>, <global/alloca*/null> - Global/Stack value |
Chris Lattner | 711b340 | 2004-11-14 07:33:16 +0000 | [diff] [blame] | 4433 | // addresses never equal each other! We already know that Op0 != Op1. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4434 | if ((isa<GlobalValue>(Op0) || isa<AllocaInst>(Op0) || |
| 4435 | isa<ConstantPointerNull>(Op0)) && |
| 4436 | (isa<GlobalValue>(Op1) || isa<AllocaInst>(Op1) || |
Chris Lattner | 711b340 | 2004-11-14 07:33:16 +0000 | [diff] [blame] | 4437 | isa<ConstantPointerNull>(Op1))) |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4438 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
| 4439 | !isTrueWhenEqual(I))); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4440 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4441 | // icmp's with boolean values can always be turned into bitwise operations |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 4442 | if (Ty == Type::Int1Ty) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4443 | switch (I.getPredicate()) { |
| 4444 | default: assert(0 && "Invalid icmp instruction!"); |
| 4445 | case ICmpInst::ICMP_EQ: { // icmp eq bool %A, %B -> ~(A^B) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4446 | Instruction *Xor = BinaryOperator::createXor(Op0, Op1, I.getName()+"tmp"); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4447 | InsertNewInstBefore(Xor, I); |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 4448 | return BinaryOperator::createNot(Xor); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4449 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4450 | case ICmpInst::ICMP_NE: // icmp eq bool %A, %B -> A^B |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 4451 | return BinaryOperator::createXor(Op0, Op1); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4452 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4453 | case ICmpInst::ICMP_UGT: |
| 4454 | case ICmpInst::ICMP_SGT: |
| 4455 | std::swap(Op0, Op1); // Change icmp gt -> icmp lt |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 4456 | // FALL THROUGH |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4457 | case ICmpInst::ICMP_ULT: |
| 4458 | case ICmpInst::ICMP_SLT: { // icmp lt bool A, B -> ~X & Y |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 4459 | Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp"); |
| 4460 | InsertNewInstBefore(Not, I); |
| 4461 | return BinaryOperator::createAnd(Not, Op1); |
| 4462 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4463 | case ICmpInst::ICMP_UGE: |
| 4464 | case ICmpInst::ICMP_SGE: |
| 4465 | std::swap(Op0, Op1); // Change icmp ge -> icmp le |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 4466 | // FALL THROUGH |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4467 | case ICmpInst::ICMP_ULE: |
| 4468 | case ICmpInst::ICMP_SLE: { // icmp le bool %A, %B -> ~A | B |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 4469 | Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp"); |
| 4470 | InsertNewInstBefore(Not, I); |
| 4471 | return BinaryOperator::createOr(Not, Op1); |
| 4472 | } |
| 4473 | } |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4474 | } |
| 4475 | |
Chris Lattner | 2be51ae | 2004-06-09 04:24:29 +0000 | [diff] [blame] | 4476 | // See if we are doing a comparison between a constant and an instruction that |
| 4477 | // can be folded into the comparison. |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4478 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4479 | switch (I.getPredicate()) { |
| 4480 | default: break; |
| 4481 | case ICmpInst::ICMP_ULT: // A <u MIN -> FALSE |
| 4482 | if (CI->isMinValue(false)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4483 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4484 | if (CI->isMaxValue(false)) // A <u MAX -> A != MAX |
| 4485 | return new ICmpInst(ICmpInst::ICMP_NE, Op0,Op1); |
| 4486 | if (isMinValuePlusOne(CI,false)) // A <u MIN+1 -> A == MIN |
| 4487 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, SubOne(CI)); |
| 4488 | break; |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4489 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4490 | case ICmpInst::ICMP_SLT: |
| 4491 | if (CI->isMinValue(true)) // A <s MIN -> FALSE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4492 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4493 | if (CI->isMaxValue(true)) // A <s MAX -> A != MAX |
| 4494 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
| 4495 | if (isMinValuePlusOne(CI,true)) // A <s MIN+1 -> A == MIN |
| 4496 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, SubOne(CI)); |
| 4497 | break; |
| 4498 | |
| 4499 | case ICmpInst::ICMP_UGT: |
| 4500 | if (CI->isMaxValue(false)) // A >u MAX -> FALSE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4501 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4502 | if (CI->isMinValue(false)) // A >u MIN -> A != MIN |
| 4503 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
| 4504 | if (isMaxValueMinusOne(CI, false)) // A >u MAX-1 -> A == MAX |
| 4505 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, AddOne(CI)); |
| 4506 | break; |
| 4507 | |
| 4508 | case ICmpInst::ICMP_SGT: |
| 4509 | if (CI->isMaxValue(true)) // A >s MAX -> FALSE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4510 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4511 | if (CI->isMinValue(true)) // A >s MIN -> A != MIN |
| 4512 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
| 4513 | if (isMaxValueMinusOne(CI, true)) // A >s MAX-1 -> A == MAX |
| 4514 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, AddOne(CI)); |
| 4515 | break; |
| 4516 | |
| 4517 | case ICmpInst::ICMP_ULE: |
| 4518 | if (CI->isMaxValue(false)) // A <=u MAX -> TRUE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4519 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4520 | if (CI->isMinValue(false)) // A <=u MIN -> A == MIN |
| 4521 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); |
| 4522 | if (isMaxValueMinusOne(CI,false)) // A <=u MAX-1 -> A != MAX |
| 4523 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, AddOne(CI)); |
| 4524 | break; |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4525 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4526 | case ICmpInst::ICMP_SLE: |
| 4527 | if (CI->isMaxValue(true)) // A <=s MAX -> TRUE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4528 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4529 | if (CI->isMinValue(true)) // A <=s MIN -> A == MIN |
| 4530 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); |
| 4531 | if (isMaxValueMinusOne(CI,true)) // A <=s MAX-1 -> A != MAX |
| 4532 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, AddOne(CI)); |
| 4533 | break; |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4534 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4535 | case ICmpInst::ICMP_UGE: |
| 4536 | if (CI->isMinValue(false)) // A >=u MIN -> TRUE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4537 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4538 | if (CI->isMaxValue(false)) // A >=u MAX -> A == MAX |
| 4539 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); |
| 4540 | if (isMinValuePlusOne(CI,false)) // A >=u MIN-1 -> A != MIN |
| 4541 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, SubOne(CI)); |
| 4542 | break; |
| 4543 | |
| 4544 | case ICmpInst::ICMP_SGE: |
| 4545 | if (CI->isMinValue(true)) // A >=s MIN -> TRUE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4546 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4547 | if (CI->isMaxValue(true)) // A >=s MAX -> A == MAX |
| 4548 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); |
| 4549 | if (isMinValuePlusOne(CI,true)) // A >=s MIN-1 -> A != MIN |
| 4550 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, SubOne(CI)); |
| 4551 | break; |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4552 | } |
| 4553 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4554 | // If we still have a icmp le or icmp ge instruction, turn it into the |
| 4555 | // appropriate icmp lt or icmp gt instruction. Since the border cases have |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4556 | // already been handled above, this requires little checking. |
| 4557 | // |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4558 | if (I.getPredicate() == ICmpInst::ICMP_ULE) |
| 4559 | return new ICmpInst(ICmpInst::ICMP_ULT, Op0, AddOne(CI)); |
| 4560 | if (I.getPredicate() == ICmpInst::ICMP_SLE) |
| 4561 | return new ICmpInst(ICmpInst::ICMP_SLT, Op0, AddOne(CI)); |
| 4562 | if (I.getPredicate() == ICmpInst::ICMP_UGE) |
| 4563 | return new ICmpInst( ICmpInst::ICMP_UGT, Op0, SubOne(CI)); |
| 4564 | if (I.getPredicate() == ICmpInst::ICMP_SGE) |
| 4565 | return new ICmpInst(ICmpInst::ICMP_SGT, Op0, SubOne(CI)); |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 4566 | |
| 4567 | // See if we can fold the comparison based on bits known to be zero or one |
| 4568 | // in the input. |
| 4569 | uint64_t KnownZero, KnownOne; |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 4570 | if (SimplifyDemandedBits(Op0, cast<IntegerType>(Ty)->getBitMask(), |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 4571 | KnownZero, KnownOne, 0)) |
| 4572 | return &I; |
| 4573 | |
| 4574 | // Given the known and unknown bits, compute a range that the LHS could be |
| 4575 | // in. |
| 4576 | if (KnownOne | KnownZero) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4577 | // Compute the Min, Max and RHS values based on the known bits. For the |
| 4578 | // EQ and NE we use unsigned values. |
Reid Spencer | b3307b2 | 2006-12-23 19:17:57 +0000 | [diff] [blame] | 4579 | uint64_t UMin = 0, UMax = 0, URHSVal = 0; |
| 4580 | int64_t SMin = 0, SMax = 0, SRHSVal = 0; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4581 | if (ICmpInst::isSignedPredicate(I.getPredicate())) { |
| 4582 | SRHSVal = CI->getSExtValue(); |
| 4583 | ComputeSignedMinMaxValuesFromKnownBits(Ty, KnownZero, KnownOne, SMin, |
| 4584 | SMax); |
| 4585 | } else { |
| 4586 | URHSVal = CI->getZExtValue(); |
| 4587 | ComputeUnsignedMinMaxValuesFromKnownBits(Ty, KnownZero, KnownOne, UMin, |
| 4588 | UMax); |
| 4589 | } |
| 4590 | switch (I.getPredicate()) { // LE/GE have been folded already. |
| 4591 | default: assert(0 && "Unknown icmp opcode!"); |
| 4592 | case ICmpInst::ICMP_EQ: |
| 4593 | if (UMax < URHSVal || UMin > URHSVal) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4594 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4595 | break; |
| 4596 | case ICmpInst::ICMP_NE: |
| 4597 | if (UMax < URHSVal || UMin > URHSVal) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4598 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4599 | break; |
| 4600 | case ICmpInst::ICMP_ULT: |
| 4601 | if (UMax < URHSVal) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4602 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4603 | if (UMin > URHSVal) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4604 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4605 | break; |
| 4606 | case ICmpInst::ICMP_UGT: |
| 4607 | if (UMin > URHSVal) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4608 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4609 | if (UMax < URHSVal) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4610 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4611 | break; |
| 4612 | case ICmpInst::ICMP_SLT: |
| 4613 | if (SMax < SRHSVal) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4614 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4615 | if (SMin > SRHSVal) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4616 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4617 | break; |
| 4618 | case ICmpInst::ICMP_SGT: |
| 4619 | if (SMin > SRHSVal) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4620 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4621 | if (SMax < SRHSVal) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4622 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4623 | break; |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 4624 | } |
| 4625 | } |
| 4626 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4627 | // 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] | 4628 | // instruction, see if that instruction also has constants so that the |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4629 | // instruction can be folded into the icmp |
Chris Lattner | 3c6a0d4 | 2004-05-25 06:32:08 +0000 | [diff] [blame] | 4630 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
Chris Lattner | 648e3bc | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 4631 | switch (LHSI->getOpcode()) { |
| 4632 | case Instruction::And: |
| 4633 | if (LHSI->hasOneUse() && isa<ConstantInt>(LHSI->getOperand(1)) && |
| 4634 | LHSI->getOperand(0)->hasOneUse()) { |
Chris Lattner | e695a3b | 2006-09-18 05:27:43 +0000 | [diff] [blame] | 4635 | ConstantInt *AndCST = cast<ConstantInt>(LHSI->getOperand(1)); |
| 4636 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4637 | // If the LHS is an AND of a truncating cast, we can widen the |
Chris Lattner | e695a3b | 2006-09-18 05:27:43 +0000 | [diff] [blame] | 4638 | // and/compare to be the input width without changing the value |
| 4639 | // produced, eliminating a cast. |
| 4640 | if (CastInst *Cast = dyn_cast<CastInst>(LHSI->getOperand(0))) { |
| 4641 | // We can do this transformation if either the AND constant does not |
| 4642 | // have its sign bit set or if it is an equality comparison. |
| 4643 | // Extending a relational comparison when we're checking the sign |
| 4644 | // bit would not work. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 4645 | if (Cast->hasOneUse() && isa<TruncInst>(Cast) && |
Chris Lattner | e695a3b | 2006-09-18 05:27:43 +0000 | [diff] [blame] | 4646 | (I.isEquality() || |
| 4647 | (AndCST->getZExtValue() == (uint64_t)AndCST->getSExtValue()) && |
| 4648 | (CI->getZExtValue() == (uint64_t)CI->getSExtValue()))) { |
| 4649 | ConstantInt *NewCST; |
| 4650 | ConstantInt *NewCI; |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 4651 | NewCST = ConstantInt::get(Cast->getOperand(0)->getType(), |
| 4652 | AndCST->getZExtValue()); |
| 4653 | NewCI = ConstantInt::get(Cast->getOperand(0)->getType(), |
| 4654 | CI->getZExtValue()); |
Chris Lattner | e695a3b | 2006-09-18 05:27:43 +0000 | [diff] [blame] | 4655 | Instruction *NewAnd = |
| 4656 | BinaryOperator::createAnd(Cast->getOperand(0), NewCST, |
| 4657 | LHSI->getName()); |
| 4658 | InsertNewInstBefore(NewAnd, I); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4659 | return new ICmpInst(I.getPredicate(), NewAnd, NewCI); |
Chris Lattner | e695a3b | 2006-09-18 05:27:43 +0000 | [diff] [blame] | 4660 | } |
| 4661 | } |
| 4662 | |
Chris Lattner | 648e3bc | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 4663 | // If this is: (X >> C1) & C2 != C3 (where any shift and any compare |
| 4664 | // could exist), turn it into (X & (C2 << C1)) != (C3 << C1). This |
| 4665 | // happens a LOT in code produced by the C front-end, for bitfield |
| 4666 | // access. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4667 | BinaryOperator *Shift = dyn_cast<BinaryOperator>(LHSI->getOperand(0)); |
| 4668 | if (Shift && !Shift->isShift()) |
| 4669 | Shift = 0; |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 4670 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4671 | ConstantInt *ShAmt; |
| 4672 | ShAmt = Shift ? dyn_cast<ConstantInt>(Shift->getOperand(1)) : 0; |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 4673 | const Type *Ty = Shift ? Shift->getType() : 0; // Type of the shift. |
| 4674 | const Type *AndTy = AndCST->getType(); // Type of the and. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4675 | |
Chris Lattner | 648e3bc | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 4676 | // We can fold this as long as we can't shift unknown bits |
| 4677 | // into the mask. This can only happen with signed shift |
| 4678 | // rights, as they sign-extend. |
| 4679 | if (ShAmt) { |
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 4680 | bool CanFold = Shift->isLogicalShift(); |
Chris Lattner | 648e3bc | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 4681 | if (!CanFold) { |
| 4682 | // To test for the bad case of the signed shr, see if any |
| 4683 | // of the bits shifted in could be tested after the mask. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4684 | int ShAmtVal = Ty->getPrimitiveSizeInBits()-ShAmt->getZExtValue(); |
Chris Lattner | d7e31cf | 2005-06-17 01:29:28 +0000 | [diff] [blame] | 4685 | if (ShAmtVal < 0) ShAmtVal = 0; // Out of range shift. |
| 4686 | |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4687 | Constant *OShAmt = ConstantInt::get(AndTy, ShAmtVal); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4688 | Constant *ShVal = |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 4689 | ConstantExpr::getShl(ConstantInt::getAllOnesValue(AndTy), |
| 4690 | OShAmt); |
Chris Lattner | 648e3bc | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 4691 | if (ConstantExpr::getAnd(ShVal, AndCST)->isNullValue()) |
| 4692 | CanFold = true; |
| 4693 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4694 | |
Chris Lattner | 648e3bc | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 4695 | if (CanFold) { |
Chris Lattner | 0cba71b | 2004-09-28 17:54:07 +0000 | [diff] [blame] | 4696 | Constant *NewCst; |
| 4697 | if (Shift->getOpcode() == Instruction::Shl) |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 4698 | NewCst = ConstantExpr::getLShr(CI, ShAmt); |
Chris Lattner | 0cba71b | 2004-09-28 17:54:07 +0000 | [diff] [blame] | 4699 | else |
| 4700 | NewCst = ConstantExpr::getShl(CI, ShAmt); |
Chris Lattner | 83c4ec0 | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 4701 | |
Chris Lattner | 648e3bc | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 4702 | // Check to see if we are shifting out any of the bits being |
| 4703 | // compared. |
| 4704 | if (ConstantExpr::get(Shift->getOpcode(), NewCst, ShAmt) != CI){ |
| 4705 | // If we shifted bits out, the fold is not going to work out. |
| 4706 | // As a special case, check to see if this means that the |
| 4707 | // result is always true or false now. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4708 | if (I.getPredicate() == ICmpInst::ICMP_EQ) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4709 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4710 | if (I.getPredicate() == ICmpInst::ICMP_NE) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4711 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | 648e3bc | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 4712 | } else { |
| 4713 | I.setOperand(1, NewCst); |
Chris Lattner | 0cba71b | 2004-09-28 17:54:07 +0000 | [diff] [blame] | 4714 | Constant *NewAndCST; |
| 4715 | if (Shift->getOpcode() == Instruction::Shl) |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 4716 | NewAndCST = ConstantExpr::getLShr(AndCST, ShAmt); |
Chris Lattner | 0cba71b | 2004-09-28 17:54:07 +0000 | [diff] [blame] | 4717 | else |
| 4718 | NewAndCST = ConstantExpr::getShl(AndCST, ShAmt); |
| 4719 | LHSI->setOperand(1, NewAndCST); |
Reid Spencer | 8c5a53a | 2007-01-04 05:23:51 +0000 | [diff] [blame] | 4720 | LHSI->setOperand(0, Shift->getOperand(0)); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 4721 | AddToWorkList(Shift); // Shift is dead. |
Chris Lattner | 648e3bc | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 4722 | AddUsesToWorkList(I); |
| 4723 | return &I; |
Chris Lattner | 5eb9194 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 4724 | } |
| 4725 | } |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 4726 | } |
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 4727 | |
| 4728 | // Turn ((X >> Y) & C) == 0 into (X & (C << Y)) == 0. The later is |
| 4729 | // preferable because it allows the C<<Y expression to be hoisted out |
| 4730 | // of a loop if Y is invariant and X is not. |
| 4731 | if (Shift && Shift->hasOneUse() && CI->isNullValue() && |
Chris Lattner | 6d7ca92 | 2006-09-18 18:27:05 +0000 | [diff] [blame] | 4732 | I.isEquality() && !Shift->isArithmeticShift() && |
| 4733 | isa<Instruction>(Shift->getOperand(0))) { |
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 4734 | // Compute C << Y. |
| 4735 | Value *NS; |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 4736 | if (Shift->getOpcode() == Instruction::LShr) { |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 4737 | NS = BinaryOperator::createShl(AndCST, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4738 | Shift->getOperand(1), "tmp"); |
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 4739 | } else { |
Reid Spencer | 7eb7638 | 2006-12-13 17:19:09 +0000 | [diff] [blame] | 4740 | // Insert a logical shift. |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 4741 | NS = BinaryOperator::createLShr(AndCST, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4742 | Shift->getOperand(1), "tmp"); |
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 4743 | } |
| 4744 | InsertNewInstBefore(cast<Instruction>(NS), I); |
| 4745 | |
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 4746 | // Compute X & (C << Y). |
Reid Spencer | 8c5a53a | 2007-01-04 05:23:51 +0000 | [diff] [blame] | 4747 | Instruction *NewAnd = BinaryOperator::createAnd( |
| 4748 | Shift->getOperand(0), NS, LHSI->getName()); |
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 4749 | InsertNewInstBefore(NewAnd, I); |
| 4750 | |
| 4751 | I.setOperand(0, NewAnd); |
| 4752 | return &I; |
| 4753 | } |
Chris Lattner | 648e3bc | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 4754 | } |
| 4755 | break; |
Chris Lattner | 83c4ec0 | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 4756 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4757 | case Instruction::Shl: // (icmp pred (shl X, ShAmt), CI) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4758 | if (ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1))) { |
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 4759 | if (I.isEquality()) { |
Chris Lattner | e17a128 | 2005-06-15 20:53:31 +0000 | [diff] [blame] | 4760 | unsigned TypeBits = CI->getType()->getPrimitiveSizeInBits(); |
| 4761 | |
| 4762 | // Check that the shift amount is in range. If not, don't perform |
| 4763 | // undefined shifts. When the shift is visited it will be |
| 4764 | // simplified. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4765 | if (ShAmt->getZExtValue() >= TypeBits) |
Chris Lattner | e17a128 | 2005-06-15 20:53:31 +0000 | [diff] [blame] | 4766 | break; |
| 4767 | |
Chris Lattner | 18d19ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 4768 | // If we are comparing against bits always shifted out, the |
| 4769 | // comparison cannot succeed. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4770 | Constant *Comp = |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 4771 | ConstantExpr::getShl(ConstantExpr::getLShr(CI, ShAmt), ShAmt); |
Chris Lattner | 18d19ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 4772 | if (Comp != CI) {// Comparing against a bit that we know is zero. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4773 | bool IsICMP_NE = I.getPredicate() == ICmpInst::ICMP_NE; |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4774 | Constant *Cst = ConstantInt::get(Type::Int1Ty, IsICMP_NE); |
Chris Lattner | 18d19ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 4775 | return ReplaceInstUsesWith(I, Cst); |
| 4776 | } |
| 4777 | |
| 4778 | if (LHSI->hasOneUse()) { |
| 4779 | // Otherwise strength reduce the shift into an and. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4780 | unsigned ShAmtVal = (unsigned)ShAmt->getZExtValue(); |
Chris Lattner | 18d19ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 4781 | uint64_t Val = (1ULL << (TypeBits-ShAmtVal))-1; |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 4782 | Constant *Mask = ConstantInt::get(CI->getType(), Val); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4783 | |
Chris Lattner | 18d19ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 4784 | Instruction *AndI = |
| 4785 | BinaryOperator::createAnd(LHSI->getOperand(0), |
| 4786 | Mask, LHSI->getName()+".mask"); |
| 4787 | Value *And = InsertNewInstBefore(AndI, I); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4788 | return new ICmpInst(I.getPredicate(), And, |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 4789 | ConstantExpr::getLShr(CI, ShAmt)); |
Chris Lattner | 18d19ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 4790 | } |
| 4791 | } |
Chris Lattner | 18d19ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 4792 | } |
| 4793 | break; |
| 4794 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4795 | case Instruction::LShr: // (icmp pred (shr X, ShAmt), CI) |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 4796 | case Instruction::AShr: |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4797 | if (ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1))) { |
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 4798 | if (I.isEquality()) { |
Chris Lattner | e17a128 | 2005-06-15 20:53:31 +0000 | [diff] [blame] | 4799 | // Check that the shift amount is in range. If not, don't perform |
| 4800 | // undefined shifts. When the shift is visited it will be |
| 4801 | // simplified. |
Chris Lattner | aa457ac | 2005-06-16 01:52:07 +0000 | [diff] [blame] | 4802 | unsigned TypeBits = CI->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4803 | if (ShAmt->getZExtValue() >= TypeBits) |
Chris Lattner | e17a128 | 2005-06-15 20:53:31 +0000 | [diff] [blame] | 4804 | break; |
| 4805 | |
Chris Lattner | f63f647 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 4806 | // If we are comparing against bits always shifted out, the |
| 4807 | // comparison cannot succeed. |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 4808 | Constant *Comp; |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 4809 | if (LHSI->getOpcode() == Instruction::LShr) |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 4810 | Comp = ConstantExpr::getLShr(ConstantExpr::getShl(CI, ShAmt), |
| 4811 | ShAmt); |
| 4812 | else |
| 4813 | Comp = ConstantExpr::getAShr(ConstantExpr::getShl(CI, ShAmt), |
| 4814 | ShAmt); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4815 | |
Chris Lattner | f63f647 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 4816 | if (Comp != CI) {// Comparing against a bit that we know is zero. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4817 | bool IsICMP_NE = I.getPredicate() == ICmpInst::ICMP_NE; |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4818 | Constant *Cst = ConstantInt::get(Type::Int1Ty, IsICMP_NE); |
Chris Lattner | f63f647 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 4819 | return ReplaceInstUsesWith(I, Cst); |
| 4820 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4821 | |
Chris Lattner | f63f647 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 4822 | if (LHSI->hasOneUse() || CI->isNullValue()) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4823 | unsigned ShAmtVal = (unsigned)ShAmt->getZExtValue(); |
Chris Lattner | 18d19ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 4824 | |
Chris Lattner | f63f647 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 4825 | // Otherwise strength reduce the shift into an and. |
| 4826 | uint64_t Val = ~0ULL; // All ones. |
| 4827 | Val <<= ShAmtVal; // Shift over to the right spot. |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 4828 | Val &= ~0ULL >> (64-TypeBits); |
| 4829 | Constant *Mask = ConstantInt::get(CI->getType(), Val); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4830 | |
Chris Lattner | f63f647 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 4831 | Instruction *AndI = |
| 4832 | BinaryOperator::createAnd(LHSI->getOperand(0), |
| 4833 | Mask, LHSI->getName()+".mask"); |
| 4834 | Value *And = InsertNewInstBefore(AndI, I); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4835 | return new ICmpInst(I.getPredicate(), And, |
Chris Lattner | f63f647 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 4836 | ConstantExpr::getShl(CI, ShAmt)); |
| 4837 | } |
Chris Lattner | f63f647 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 4838 | } |
| 4839 | } |
| 4840 | break; |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 4841 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 4842 | case Instruction::SDiv: |
| 4843 | case Instruction::UDiv: |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4844 | // Fold: icmp pred ([us]div X, C1), C2 -> range test |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 4845 | // Fold this div into the comparison, producing a range check. |
| 4846 | // Determine, based on the divide type, what the range is being |
| 4847 | // checked. If there is an overflow on the low or high side, remember |
| 4848 | // it, otherwise compute the range [low, hi) bounding the new value. |
| 4849 | // See: InsertRangeTest above for the kinds of replacements possible. |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4850 | if (ConstantInt *DivRHS = dyn_cast<ConstantInt>(LHSI->getOperand(1))) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 4851 | // FIXME: If the operand types don't match the type of the divide |
| 4852 | // then don't attempt this transform. The code below doesn't have the |
| 4853 | // logic to deal with a signed divide and an unsigned compare (and |
| 4854 | // vice versa). This is because (x /s C1) <s C2 produces different |
| 4855 | // results than (x /s C1) <u C2 or (x /u C1) <s C2 or even |
| 4856 | // (x /u C1) <u C2. Simply casting the operands and result won't |
| 4857 | // work. :( The if statement below tests that condition and bails |
| 4858 | // if it finds it. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4859 | bool DivIsSigned = LHSI->getOpcode() == Instruction::SDiv; |
| 4860 | if (!I.isEquality() && DivIsSigned != I.isSignedPredicate()) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 4861 | break; |
| 4862 | |
| 4863 | // Initialize the variables that will indicate the nature of the |
| 4864 | // range check. |
| 4865 | bool LoOverflow = false, HiOverflow = false; |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4866 | ConstantInt *LoBound = 0, *HiBound = 0; |
| 4867 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 4868 | // Compute Prod = CI * DivRHS. We are essentially solving an equation |
| 4869 | // of form X/C1=C2. We solve for X by multiplying C1 (DivRHS) and |
| 4870 | // C2 (CI). By solving for X we can turn this into a range check |
| 4871 | // instead of computing a divide. |
| 4872 | ConstantInt *Prod = |
| 4873 | cast<ConstantInt>(ConstantExpr::getMul(CI, DivRHS)); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4874 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 4875 | // Determine if the product overflows by seeing if the product is |
| 4876 | // not equal to the divide. Make sure we do the same kind of divide |
| 4877 | // as in the LHS instruction that we're folding. |
| 4878 | bool ProdOV = !DivRHS->isNullValue() && |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4879 | (DivIsSigned ? ConstantExpr::getSDiv(Prod, DivRHS) : |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 4880 | ConstantExpr::getUDiv(Prod, DivRHS)) != CI; |
| 4881 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4882 | // Get the ICmp opcode |
| 4883 | ICmpInst::Predicate predicate = I.getPredicate(); |
Chris Lattner | 6a9fdfa | 2004-10-11 19:40:04 +0000 | [diff] [blame] | 4884 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 4885 | if (DivRHS->isNullValue()) { |
| 4886 | // Don't hack on divide by zeros! |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4887 | } else if (!DivIsSigned) { // udiv |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4888 | LoBound = Prod; |
| 4889 | LoOverflow = ProdOV; |
| 4890 | HiOverflow = ProdOV || AddWithOverflow(HiBound, LoBound, DivRHS); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 4891 | } else if (isPositive(DivRHS)) { // Divisor is > 0. |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4892 | if (CI->isNullValue()) { // (X / pos) op 0 |
| 4893 | // Can't overflow. |
| 4894 | LoBound = cast<ConstantInt>(ConstantExpr::getNeg(SubOne(DivRHS))); |
| 4895 | HiBound = DivRHS; |
| 4896 | } else if (isPositive(CI)) { // (X / pos) op pos |
| 4897 | LoBound = Prod; |
| 4898 | LoOverflow = ProdOV; |
| 4899 | HiOverflow = ProdOV || AddWithOverflow(HiBound, Prod, DivRHS); |
| 4900 | } else { // (X / pos) op neg |
| 4901 | Constant *DivRHSH = ConstantExpr::getNeg(SubOne(DivRHS)); |
| 4902 | LoOverflow = AddWithOverflow(LoBound, Prod, |
| 4903 | cast<ConstantInt>(DivRHSH)); |
| 4904 | HiBound = Prod; |
| 4905 | HiOverflow = ProdOV; |
| 4906 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 4907 | } else { // Divisor is < 0. |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4908 | if (CI->isNullValue()) { // (X / neg) op 0 |
| 4909 | LoBound = AddOne(DivRHS); |
| 4910 | HiBound = cast<ConstantInt>(ConstantExpr::getNeg(DivRHS)); |
Chris Lattner | 5662503 | 2005-06-17 02:05:55 +0000 | [diff] [blame] | 4911 | if (HiBound == DivRHS) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 4912 | LoBound = 0; // - INTMIN = INTMIN |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4913 | } else if (isPositive(CI)) { // (X / neg) op pos |
| 4914 | HiOverflow = LoOverflow = ProdOV; |
| 4915 | if (!LoOverflow) |
| 4916 | LoOverflow = AddWithOverflow(LoBound, Prod, AddOne(DivRHS)); |
| 4917 | HiBound = AddOne(Prod); |
| 4918 | } else { // (X / neg) op neg |
| 4919 | LoBound = Prod; |
| 4920 | LoOverflow = HiOverflow = ProdOV; |
| 4921 | HiBound = cast<ConstantInt>(ConstantExpr::getSub(Prod, DivRHS)); |
| 4922 | } |
Chris Lattner | 340a05f | 2004-10-08 19:15:44 +0000 | [diff] [blame] | 4923 | |
Chris Lattner | 6a9fdfa | 2004-10-11 19:40:04 +0000 | [diff] [blame] | 4924 | // Dividing by a negate swaps the condition. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4925 | predicate = ICmpInst::getSwappedPredicate(predicate); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4926 | } |
| 4927 | |
| 4928 | if (LoBound) { |
| 4929 | Value *X = LHSI->getOperand(0); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4930 | switch (predicate) { |
| 4931 | default: assert(0 && "Unhandled icmp opcode!"); |
| 4932 | case ICmpInst::ICMP_EQ: |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4933 | if (LoOverflow && HiOverflow) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4934 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4935 | else if (HiOverflow) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4936 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : |
| 4937 | ICmpInst::ICMP_UGE, X, LoBound); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4938 | else if (LoOverflow) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4939 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : |
| 4940 | ICmpInst::ICMP_ULT, X, HiBound); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4941 | else |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4942 | return InsertRangeTest(X, LoBound, HiBound, DivIsSigned, |
| 4943 | true, I); |
| 4944 | case ICmpInst::ICMP_NE: |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4945 | if (LoOverflow && HiOverflow) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4946 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4947 | else if (HiOverflow) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4948 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : |
| 4949 | ICmpInst::ICMP_ULT, X, LoBound); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4950 | else if (LoOverflow) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4951 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : |
| 4952 | ICmpInst::ICMP_UGE, X, HiBound); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4953 | else |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4954 | return InsertRangeTest(X, LoBound, HiBound, DivIsSigned, |
| 4955 | false, I); |
| 4956 | case ICmpInst::ICMP_ULT: |
| 4957 | case ICmpInst::ICMP_SLT: |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4958 | if (LoOverflow) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4959 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4960 | return new ICmpInst(predicate, X, LoBound); |
| 4961 | case ICmpInst::ICMP_UGT: |
| 4962 | case ICmpInst::ICMP_SGT: |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4963 | if (HiOverflow) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4964 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4965 | if (predicate == ICmpInst::ICMP_UGT) |
| 4966 | return new ICmpInst(ICmpInst::ICMP_UGE, X, HiBound); |
| 4967 | else |
| 4968 | return new ICmpInst(ICmpInst::ICMP_SGE, X, HiBound); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4969 | } |
| 4970 | } |
| 4971 | } |
| 4972 | break; |
Chris Lattner | 648e3bc | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 4973 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4974 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4975 | // Simplify icmp_eq and icmp_ne instructions with integer constant RHS. |
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 4976 | if (I.isEquality()) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4977 | bool isICMP_NE = I.getPredicate() == ICmpInst::ICMP_NE; |
Chris Lattner | bc5d414 | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 4978 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4979 | // If the first operand is (add|sub|and|or|xor|rem) with a constant, and |
| 4980 | // the second operand is a constant, simplify a bit. |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 4981 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0)) { |
| 4982 | switch (BO->getOpcode()) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4983 | case Instruction::SRem: |
| 4984 | // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one. |
| 4985 | if (CI->isNullValue() && isa<ConstantInt>(BO->getOperand(1)) && |
| 4986 | BO->hasOneUse()) { |
| 4987 | int64_t V = cast<ConstantInt>(BO->getOperand(1))->getSExtValue(); |
| 4988 | if (V > 1 && isPowerOf2_64(V)) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 4989 | Value *NewRem = InsertNewInstBefore(BinaryOperator::createURem( |
| 4990 | BO->getOperand(0), BO->getOperand(1), BO->getName()), I); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4991 | return new ICmpInst(I.getPredicate(), NewRem, |
| 4992 | Constant::getNullValue(BO->getType())); |
Chris Lattner | 3571b72 | 2004-07-06 07:38:18 +0000 | [diff] [blame] | 4993 | } |
Chris Lattner | bcd7db5 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 4994 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4995 | break; |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 4996 | case Instruction::Add: |
Chris Lattner | 15d58b6 | 2004-06-27 22:51:36 +0000 | [diff] [blame] | 4997 | // Replace ((add A, B) != C) with (A != C-B) if B & C are constants. |
| 4998 | if (ConstantInt *BOp1C = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
Chris Lattner | 3d834bf | 2004-09-21 21:35:23 +0000 | [diff] [blame] | 4999 | if (BO->hasOneUse()) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5000 | return new ICmpInst(I.getPredicate(), BO->getOperand(0), |
| 5001 | ConstantExpr::getSub(CI, BOp1C)); |
Chris Lattner | 15d58b6 | 2004-06-27 22:51:36 +0000 | [diff] [blame] | 5002 | } else if (CI->isNullValue()) { |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 5003 | // Replace ((add A, B) != 0) with (A != -B) if A or B is |
| 5004 | // efficiently invertible, or if the add has just this one use. |
| 5005 | Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5006 | |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 5007 | if (Value *NegVal = dyn_castNegVal(BOp1)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5008 | return new ICmpInst(I.getPredicate(), BOp0, NegVal); |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 5009 | else if (Value *NegVal = dyn_castNegVal(BOp0)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5010 | return new ICmpInst(I.getPredicate(), NegVal, BOp1); |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 5011 | else if (BO->hasOneUse()) { |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 5012 | Instruction *Neg = BinaryOperator::createNeg(BOp1); |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 5013 | InsertNewInstBefore(Neg, I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 5014 | Neg->takeName(BO); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5015 | return new ICmpInst(I.getPredicate(), BOp0, Neg); |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 5016 | } |
| 5017 | } |
| 5018 | break; |
| 5019 | case Instruction::Xor: |
| 5020 | // For the xor case, we can xor two constants together, eliminating |
| 5021 | // the explicit xor. |
| 5022 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5023 | return new ICmpInst(I.getPredicate(), BO->getOperand(0), |
| 5024 | ConstantExpr::getXor(CI, BOC)); |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 5025 | |
| 5026 | // FALLTHROUGH |
| 5027 | case Instruction::Sub: |
| 5028 | // Replace (([sub|xor] A, B) != 0) with (A != B) |
| 5029 | if (CI->isNullValue()) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5030 | return new ICmpInst(I.getPredicate(), BO->getOperand(0), |
| 5031 | BO->getOperand(1)); |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 5032 | break; |
| 5033 | |
| 5034 | case Instruction::Or: |
| 5035 | // If bits are being or'd in that are not present in the constant we |
| 5036 | // are comparing against, then the comparison could never succeed! |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 5037 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) { |
Chris Lattner | 448c323 | 2004-06-10 02:12:35 +0000 | [diff] [blame] | 5038 | Constant *NotCI = ConstantExpr::getNot(CI); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 5039 | if (!ConstantExpr::getAnd(BOC, NotCI)->isNullValue()) |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 5040 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
| 5041 | isICMP_NE)); |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 5042 | } |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 5043 | break; |
| 5044 | |
| 5045 | case Instruction::And: |
| 5046 | if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
Chris Lattner | bc5d414 | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 5047 | // If bits are being compared against that are and'd out, then the |
| 5048 | // comparison can never succeed! |
Chris Lattner | 448c323 | 2004-06-10 02:12:35 +0000 | [diff] [blame] | 5049 | if (!ConstantExpr::getAnd(CI, |
| 5050 | ConstantExpr::getNot(BOC))->isNullValue()) |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 5051 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
| 5052 | isICMP_NE)); |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 5053 | |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 5054 | // If we have ((X & C) == C), turn it into ((X & C) != 0). |
Chris Lattner | 3285a6f | 2004-06-10 02:33:20 +0000 | [diff] [blame] | 5055 | if (CI == BOC && isOneBitSet(CI)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5056 | return new ICmpInst(isICMP_NE ? ICmpInst::ICMP_EQ : |
| 5057 | ICmpInst::ICMP_NE, Op0, |
| 5058 | Constant::getNullValue(CI->getType())); |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 5059 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5060 | // Replace (and X, (1 << size(X)-1) != 0) with x s< 0 |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 5061 | if (isSignBit(BOC)) { |
| 5062 | Value *X = BO->getOperand(0); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5063 | Constant *Zero = Constant::getNullValue(X->getType()); |
| 5064 | ICmpInst::Predicate pred = isICMP_NE ? |
| 5065 | ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGE; |
| 5066 | return new ICmpInst(pred, X, Zero); |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 5067 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5068 | |
Chris Lattner | 83c4ec0 | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 5069 | // ((X & ~7) == 0) --> X < 8 |
Chris Lattner | b20ba0a | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 5070 | if (CI->isNullValue() && isHighOnes(BOC)) { |
| 5071 | Value *X = BO->getOperand(0); |
Chris Lattner | 83c4ec0 | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 5072 | Constant *NegX = ConstantExpr::getNeg(BOC); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5073 | ICmpInst::Predicate pred = isICMP_NE ? |
| 5074 | ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT; |
| 5075 | return new ICmpInst(pred, X, NegX); |
Chris Lattner | b20ba0a | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 5076 | } |
| 5077 | |
Chris Lattner | bc5d414 | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 5078 | } |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 5079 | default: break; |
| 5080 | } |
Chris Lattner | 458cf46 | 2006-11-29 05:02:16 +0000 | [diff] [blame] | 5081 | } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Op0)) { |
| 5082 | // Handle set{eq|ne} <intrinsic>, intcst. |
| 5083 | switch (II->getIntrinsicID()) { |
| 5084 | default: break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5085 | case Intrinsic::bswap_i16: |
| 5086 | // icmp eq (bswap(x)), c -> icmp eq (x,bswap(c)) |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 5087 | AddToWorkList(II); // Dead? |
Chris Lattner | 458cf46 | 2006-11-29 05:02:16 +0000 | [diff] [blame] | 5088 | I.setOperand(0, II->getOperand(1)); |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 5089 | I.setOperand(1, ConstantInt::get(Type::Int16Ty, |
Chris Lattner | 458cf46 | 2006-11-29 05:02:16 +0000 | [diff] [blame] | 5090 | ByteSwap_16(CI->getZExtValue()))); |
| 5091 | return &I; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5092 | case Intrinsic::bswap_i32: |
| 5093 | // icmp eq (bswap(x)), c -> icmp eq (x,bswap(c)) |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 5094 | AddToWorkList(II); // Dead? |
Chris Lattner | 458cf46 | 2006-11-29 05:02:16 +0000 | [diff] [blame] | 5095 | I.setOperand(0, II->getOperand(1)); |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 5096 | I.setOperand(1, ConstantInt::get(Type::Int32Ty, |
Chris Lattner | 458cf46 | 2006-11-29 05:02:16 +0000 | [diff] [blame] | 5097 | ByteSwap_32(CI->getZExtValue()))); |
| 5098 | return &I; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5099 | case Intrinsic::bswap_i64: |
| 5100 | // icmp eq (bswap(x)), c -> icmp eq (x,bswap(c)) |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 5101 | AddToWorkList(II); // Dead? |
Chris Lattner | 458cf46 | 2006-11-29 05:02:16 +0000 | [diff] [blame] | 5102 | I.setOperand(0, II->getOperand(1)); |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 5103 | I.setOperand(1, ConstantInt::get(Type::Int64Ty, |
Chris Lattner | 458cf46 | 2006-11-29 05:02:16 +0000 | [diff] [blame] | 5104 | ByteSwap_64(CI->getZExtValue()))); |
| 5105 | return &I; |
| 5106 | } |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 5107 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5108 | } else { // Not a ICMP_EQ/ICMP_NE |
| 5109 | // If the LHS is a cast from an integral value of the same size, then |
| 5110 | // since we know the RHS is a constant, try to simlify. |
Chris Lattner | c5943fb | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 5111 | if (CastInst *Cast = dyn_cast<CastInst>(Op0)) { |
| 5112 | Value *CastOp = Cast->getOperand(0); |
| 5113 | const Type *SrcTy = CastOp->getType(); |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5114 | unsigned SrcTySize = SrcTy->getPrimitiveSizeInBits(); |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 5115 | if (SrcTy->isInteger() && |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5116 | SrcTySize == Cast->getType()->getPrimitiveSizeInBits()) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5117 | // If this is an unsigned comparison, try to make the comparison use |
| 5118 | // smaller constant values. |
| 5119 | switch (I.getPredicate()) { |
| 5120 | default: break; |
| 5121 | case ICmpInst::ICMP_ULT: { // X u< 128 => X s> -1 |
| 5122 | ConstantInt *CUI = cast<ConstantInt>(CI); |
| 5123 | if (CUI->getZExtValue() == 1ULL << (SrcTySize-1)) |
| 5124 | return new ICmpInst(ICmpInst::ICMP_SGT, CastOp, |
Reid Spencer | 62aa905 | 2007-03-01 19:33:52 +0000 | [diff] [blame] | 5125 | ConstantInt::get(SrcTy, -1ULL)); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5126 | break; |
| 5127 | } |
| 5128 | case ICmpInst::ICMP_UGT: { // X u> 127 => X s< 0 |
| 5129 | ConstantInt *CUI = cast<ConstantInt>(CI); |
| 5130 | if (CUI->getZExtValue() == (1ULL << (SrcTySize-1))-1) |
| 5131 | return new ICmpInst(ICmpInst::ICMP_SLT, CastOp, |
| 5132 | Constant::getNullValue(SrcTy)); |
| 5133 | break; |
| 5134 | } |
Chris Lattner | c5943fb | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 5135 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5136 | |
Chris Lattner | c5943fb | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 5137 | } |
| 5138 | } |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 5139 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5140 | } |
| 5141 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5142 | // Handle icmp with constant RHS |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5143 | if (Constant *RHSC = dyn_cast<Constant>(Op1)) { |
| 5144 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
| 5145 | switch (LHSI->getOpcode()) { |
Chris Lattner | 9fb25db | 2005-05-01 04:42:15 +0000 | [diff] [blame] | 5146 | case Instruction::GetElementPtr: |
| 5147 | if (RHSC->isNullValue()) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5148 | // 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] | 5149 | bool isAllZeros = true; |
| 5150 | for (unsigned i = 1, e = LHSI->getNumOperands(); i != e; ++i) |
| 5151 | if (!isa<Constant>(LHSI->getOperand(i)) || |
| 5152 | !cast<Constant>(LHSI->getOperand(i))->isNullValue()) { |
| 5153 | isAllZeros = false; |
| 5154 | break; |
| 5155 | } |
| 5156 | if (isAllZeros) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5157 | return new ICmpInst(I.getPredicate(), LHSI->getOperand(0), |
Chris Lattner | 9fb25db | 2005-05-01 04:42:15 +0000 | [diff] [blame] | 5158 | Constant::getNullValue(LHSI->getOperand(0)->getType())); |
| 5159 | } |
| 5160 | break; |
| 5161 | |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5162 | case Instruction::PHI: |
| 5163 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 5164 | return NV; |
| 5165 | break; |
| 5166 | case Instruction::Select: |
| 5167 | // If either operand of the select is a constant, we can fold the |
| 5168 | // comparison into the select arms, which will cause one to be |
| 5169 | // constant folded and the select turned into a bitwise or. |
| 5170 | Value *Op1 = 0, *Op2 = 0; |
| 5171 | if (LHSI->hasOneUse()) { |
| 5172 | if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) { |
| 5173 | // Fold the known value into the constant operand. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5174 | Op1 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC); |
| 5175 | // Insert a new ICmp of the other select operand. |
| 5176 | Op2 = InsertNewInstBefore(new ICmpInst(I.getPredicate(), |
| 5177 | LHSI->getOperand(2), RHSC, |
| 5178 | I.getName()), I); |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5179 | } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) { |
| 5180 | // Fold the known value into the constant operand. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5181 | Op2 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC); |
| 5182 | // Insert a new ICmp of the other select operand. |
| 5183 | Op1 = InsertNewInstBefore(new ICmpInst(I.getPredicate(), |
| 5184 | LHSI->getOperand(1), RHSC, |
| 5185 | I.getName()), I); |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5186 | } |
| 5187 | } |
Jeff Cohen | 9d80930 | 2005-04-23 21:38:35 +0000 | [diff] [blame] | 5188 | |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5189 | if (Op1) |
| 5190 | return new SelectInst(LHSI->getOperand(0), Op1, Op2); |
| 5191 | break; |
| 5192 | } |
| 5193 | } |
| 5194 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5195 | // 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] | 5196 | if (User *GEP = dyn_castGetElementPtr(Op0)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5197 | if (Instruction *NI = FoldGEPICmp(GEP, Op1, I.getPredicate(), I)) |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5198 | return NI; |
| 5199 | if (User *GEP = dyn_castGetElementPtr(Op1)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5200 | if (Instruction *NI = FoldGEPICmp(GEP, Op0, |
| 5201 | ICmpInst::getSwappedPredicate(I.getPredicate()), I)) |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5202 | return NI; |
| 5203 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5204 | // 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] | 5205 | // values. If the ptr->ptr cast can be stripped off both arguments, we do so |
| 5206 | // now. |
| 5207 | if (BitCastInst *CI = dyn_cast<BitCastInst>(Op0)) { |
| 5208 | if (isa<PointerType>(Op0->getType()) && |
| 5209 | (isa<Constant>(Op1) || isa<BitCastInst>(Op1))) { |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5210 | // We keep moving the cast from the left operand over to the right |
| 5211 | // operand, where it can often be eliminated completely. |
Chris Lattner | 57d8637 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 5212 | Op0 = CI->getOperand(0); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5213 | |
Chris Lattner | 57d8637 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 5214 | // If operand #1 is a bitcast instruction, it must also be a ptr->ptr cast |
| 5215 | // so eliminate it as well. |
| 5216 | if (BitCastInst *CI2 = dyn_cast<BitCastInst>(Op1)) |
| 5217 | Op1 = CI2->getOperand(0); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5218 | |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5219 | // 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] | 5220 | if (Op0->getType() != Op1->getType()) |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5221 | if (Constant *Op1C = dyn_cast<Constant>(Op1)) { |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 5222 | Op1 = ConstantExpr::getBitCast(Op1C, Op0->getType()); |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5223 | } else { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5224 | // Otherwise, cast the RHS right before the icmp |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 5225 | Op1 = InsertCastBefore(Instruction::BitCast, Op1, Op0->getType(), I); |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5226 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5227 | return new ICmpInst(I.getPredicate(), Op0, Op1); |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5228 | } |
Chris Lattner | 57d8637 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 5229 | } |
| 5230 | |
| 5231 | if (isa<CastInst>(Op0)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5232 | // Handle the special case of: icmp (cast bool to X), <cst> |
Chris Lattner | 6870805 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 5233 | // This comes up when you have code like |
| 5234 | // int X = A < B; |
| 5235 | // if (X) ... |
| 5236 | // For generality, we handle any zero-extension of any operand comparison |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5237 | // with a constant or another cast from the same type. |
| 5238 | if (isa<ConstantInt>(Op1) || isa<CastInst>(Op1)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5239 | if (Instruction *R = visitICmpInstWithCastAndCast(I)) |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5240 | return R; |
Chris Lattner | 6870805 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 5241 | } |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5242 | |
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 5243 | if (I.isEquality()) { |
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5244 | Value *A, *B, *C, *D; |
| 5245 | if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) { |
| 5246 | if (A == Op1 || B == Op1) { // (A^B) == A -> B == 0 |
| 5247 | Value *OtherVal = A == Op1 ? B : A; |
| 5248 | return new ICmpInst(I.getPredicate(), OtherVal, |
| 5249 | Constant::getNullValue(A->getType())); |
| 5250 | } |
| 5251 | |
| 5252 | if (match(Op1, m_Xor(m_Value(C), m_Value(D)))) { |
| 5253 | // A^c1 == C^c2 --> A == C^(c1^c2) |
| 5254 | if (ConstantInt *C1 = dyn_cast<ConstantInt>(B)) |
| 5255 | if (ConstantInt *C2 = dyn_cast<ConstantInt>(D)) |
| 5256 | if (Op1->hasOneUse()) { |
| 5257 | Constant *NC = ConstantExpr::getXor(C1, C2); |
| 5258 | Instruction *Xor = BinaryOperator::createXor(C, NC, "tmp"); |
| 5259 | return new ICmpInst(I.getPredicate(), A, |
| 5260 | InsertNewInstBefore(Xor, I)); |
| 5261 | } |
| 5262 | |
| 5263 | // A^B == A^D -> B == D |
| 5264 | if (A == C) return new ICmpInst(I.getPredicate(), B, D); |
| 5265 | if (A == D) return new ICmpInst(I.getPredicate(), B, C); |
| 5266 | if (B == C) return new ICmpInst(I.getPredicate(), A, D); |
| 5267 | if (B == D) return new ICmpInst(I.getPredicate(), A, C); |
| 5268 | } |
| 5269 | } |
| 5270 | |
| 5271 | if (match(Op1, m_Xor(m_Value(A), m_Value(B))) && |
| 5272 | (A == Op0 || B == Op0)) { |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5273 | // A == (A^B) -> B == 0 |
| 5274 | Value *OtherVal = A == Op0 ? B : A; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5275 | return new ICmpInst(I.getPredicate(), OtherVal, |
| 5276 | Constant::getNullValue(A->getType())); |
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5277 | } |
| 5278 | 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] | 5279 | // (A-B) == A -> B == 0 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5280 | return new ICmpInst(I.getPredicate(), B, |
| 5281 | Constant::getNullValue(B->getType())); |
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5282 | } |
| 5283 | 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] | 5284 | // A == (A-B) -> B == 0 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5285 | return new ICmpInst(I.getPredicate(), B, |
| 5286 | Constant::getNullValue(B->getType())); |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5287 | } |
Chris Lattner | 9c2328e | 2006-11-14 06:06:06 +0000 | [diff] [blame] | 5288 | |
Chris Lattner | 9c2328e | 2006-11-14 06:06:06 +0000 | [diff] [blame] | 5289 | // (X&Z) == (Y&Z) -> (X^Y) & Z == 0 |
| 5290 | if (Op0->hasOneUse() && Op1->hasOneUse() && |
| 5291 | match(Op0, m_And(m_Value(A), m_Value(B))) && |
| 5292 | match(Op1, m_And(m_Value(C), m_Value(D)))) { |
| 5293 | Value *X = 0, *Y = 0, *Z = 0; |
| 5294 | |
| 5295 | if (A == C) { |
| 5296 | X = B; Y = D; Z = A; |
| 5297 | } else if (A == D) { |
| 5298 | X = B; Y = C; Z = A; |
| 5299 | } else if (B == C) { |
| 5300 | X = A; Y = D; Z = B; |
| 5301 | } else if (B == D) { |
| 5302 | X = A; Y = C; Z = B; |
| 5303 | } |
| 5304 | |
| 5305 | if (X) { // Build (X^Y) & Z |
| 5306 | Op1 = InsertNewInstBefore(BinaryOperator::createXor(X, Y, "tmp"), I); |
| 5307 | Op1 = InsertNewInstBefore(BinaryOperator::createAnd(Op1, Z, "tmp"), I); |
| 5308 | I.setOperand(0, Op1); |
| 5309 | I.setOperand(1, Constant::getNullValue(Op1->getType())); |
| 5310 | return &I; |
| 5311 | } |
| 5312 | } |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5313 | } |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 5314 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5315 | } |
| 5316 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5317 | // visitICmpInstWithCastAndCast - Handle icmp (cast x to y), (cast/cst). |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5318 | // We only handle extending casts so far. |
| 5319 | // |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5320 | Instruction *InstCombiner::visitICmpInstWithCastAndCast(ICmpInst &ICI) { |
| 5321 | const CastInst *LHSCI = cast<CastInst>(ICI.getOperand(0)); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 5322 | Value *LHSCIOp = LHSCI->getOperand(0); |
| 5323 | const Type *SrcTy = LHSCIOp->getType(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5324 | const Type *DestTy = LHSCI->getType(); |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5325 | Value *RHSCIOp; |
| 5326 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5327 | // We only handle extension cast instructions, so far. Enforce this. |
| 5328 | if (LHSCI->getOpcode() != Instruction::ZExt && |
| 5329 | LHSCI->getOpcode() != Instruction::SExt) |
Chris Lattner | b352fa5 | 2005-01-17 03:20:02 +0000 | [diff] [blame] | 5330 | return 0; |
| 5331 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5332 | bool isSignedExt = LHSCI->getOpcode() == Instruction::SExt; |
| 5333 | bool isSignedCmp = ICI.isSignedPredicate(); |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5334 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5335 | if (CastInst *CI = dyn_cast<CastInst>(ICI.getOperand(1))) { |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5336 | // Not an extension from the same type? |
| 5337 | RHSCIOp = CI->getOperand(0); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5338 | if (RHSCIOp->getType() != LHSCIOp->getType()) |
| 5339 | return 0; |
Chris Lattner | a5c5e77 | 2007-01-13 23:11:38 +0000 | [diff] [blame] | 5340 | |
| 5341 | // If the signedness of the two compares doesn't agree (i.e. one is a sext |
| 5342 | // and the other is a zext), then we can't handle this. |
| 5343 | if (CI->getOpcode() != LHSCI->getOpcode()) |
| 5344 | return 0; |
| 5345 | |
| 5346 | // Likewise, if the signedness of the [sz]exts and the compare don't match, |
| 5347 | // then we can't handle this. |
| 5348 | if (isSignedExt != isSignedCmp && !ICI.isEquality()) |
| 5349 | return 0; |
| 5350 | |
| 5351 | // Okay, just insert a compare of the reduced operands now! |
| 5352 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSCIOp); |
Reid Spencer | 6731d5c | 2004-11-28 21:31:15 +0000 | [diff] [blame] | 5353 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5354 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5355 | // If we aren't dealing with a constant on the RHS, exit early |
| 5356 | ConstantInt *CI = dyn_cast<ConstantInt>(ICI.getOperand(1)); |
| 5357 | if (!CI) |
| 5358 | return 0; |
| 5359 | |
| 5360 | // Compute the constant that would happen if we truncated to SrcTy then |
| 5361 | // reextended to DestTy. |
| 5362 | Constant *Res1 = ConstantExpr::getTrunc(CI, SrcTy); |
| 5363 | Constant *Res2 = ConstantExpr::getCast(LHSCI->getOpcode(), Res1, DestTy); |
| 5364 | |
| 5365 | // If the re-extended constant didn't change... |
| 5366 | if (Res2 == CI) { |
| 5367 | // Make sure that sign of the Cmp and the sign of the Cast are the same. |
| 5368 | // For example, we might have: |
| 5369 | // %A = sext short %X to uint |
| 5370 | // %B = icmp ugt uint %A, 1330 |
| 5371 | // It is incorrect to transform this into |
| 5372 | // %B = icmp ugt short %X, 1330 |
| 5373 | // because %A may have negative value. |
| 5374 | // |
| 5375 | // However, it is OK if SrcTy is bool (See cast-set.ll testcase) |
| 5376 | // OR operation is EQ/NE. |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 5377 | if (isSignedExt == isSignedCmp || SrcTy == Type::Int1Ty || ICI.isEquality()) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5378 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, Res1); |
| 5379 | else |
| 5380 | return 0; |
| 5381 | } |
| 5382 | |
| 5383 | // The re-extended constant changed so the constant cannot be represented |
| 5384 | // in the shorter type. Consequently, we cannot emit a simple comparison. |
| 5385 | |
| 5386 | // First, handle some easy cases. We know the result cannot be equal at this |
| 5387 | // point so handle the ICI.isEquality() cases |
| 5388 | if (ICI.getPredicate() == ICmpInst::ICMP_EQ) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5389 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5390 | if (ICI.getPredicate() == ICmpInst::ICMP_NE) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5391 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5392 | |
| 5393 | // Evaluate the comparison for LT (we invert for GT below). LE and GE cases |
| 5394 | // should have been folded away previously and not enter in here. |
| 5395 | Value *Result; |
| 5396 | if (isSignedCmp) { |
| 5397 | // We're performing a signed comparison. |
| 5398 | if (cast<ConstantInt>(CI)->getSExtValue() < 0) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5399 | Result = ConstantInt::getFalse(); // X < (small) --> false |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5400 | else |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5401 | Result = ConstantInt::getTrue(); // X < (large) --> true |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5402 | } else { |
| 5403 | // We're performing an unsigned comparison. |
| 5404 | if (isSignedExt) { |
| 5405 | // We're performing an unsigned comp with a sign extended value. |
| 5406 | // This is true if the input is >= 0. [aka >s -1] |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5407 | Constant *NegOne = ConstantInt::getAllOnesValue(SrcTy); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5408 | Result = InsertNewInstBefore(new ICmpInst(ICmpInst::ICMP_SGT, LHSCIOp, |
| 5409 | NegOne, ICI.getName()), ICI); |
| 5410 | } else { |
| 5411 | // Unsigned extend & unsigned compare -> always true. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5412 | Result = ConstantInt::getTrue(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5413 | } |
| 5414 | } |
| 5415 | |
| 5416 | // Finally, return the value computed. |
| 5417 | if (ICI.getPredicate() == ICmpInst::ICMP_ULT || |
| 5418 | ICI.getPredicate() == ICmpInst::ICMP_SLT) { |
| 5419 | return ReplaceInstUsesWith(ICI, Result); |
| 5420 | } else { |
| 5421 | assert((ICI.getPredicate()==ICmpInst::ICMP_UGT || |
| 5422 | ICI.getPredicate()==ICmpInst::ICMP_SGT) && |
| 5423 | "ICmp should be folded!"); |
| 5424 | if (Constant *CI = dyn_cast<Constant>(Result)) |
| 5425 | return ReplaceInstUsesWith(ICI, ConstantExpr::getNot(CI)); |
| 5426 | else |
| 5427 | return BinaryOperator::createNot(Result); |
| 5428 | } |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5429 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5430 | |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 5431 | Instruction *InstCombiner::visitShl(BinaryOperator &I) { |
| 5432 | return commonShiftTransforms(I); |
| 5433 | } |
| 5434 | |
| 5435 | Instruction *InstCombiner::visitLShr(BinaryOperator &I) { |
| 5436 | return commonShiftTransforms(I); |
| 5437 | } |
| 5438 | |
| 5439 | Instruction *InstCombiner::visitAShr(BinaryOperator &I) { |
| 5440 | return commonShiftTransforms(I); |
| 5441 | } |
| 5442 | |
| 5443 | Instruction *InstCombiner::commonShiftTransforms(BinaryOperator &I) { |
| 5444 | assert(I.getOperand(1)->getType() == I.getOperand(0)->getType()); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 5445 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5446 | |
| 5447 | // shl X, 0 == X and shr X, 0 == X |
| 5448 | // shl 0, X == 0 and shr 0, X == 0 |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 5449 | if (Op1 == Constant::getNullValue(Op1->getType()) || |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 5450 | Op0 == Constant::getNullValue(Op0->getType())) |
| 5451 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 8d6bbdb | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 5452 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5453 | if (isa<UndefValue>(Op0)) { |
| 5454 | if (I.getOpcode() == Instruction::AShr) // undef >>s X -> undef |
Chris Lattner | 79a564c | 2004-10-16 23:28:04 +0000 | [diff] [blame] | 5455 | return ReplaceInstUsesWith(I, Op0); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5456 | else // undef << X -> 0, undef >>u X -> 0 |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5457 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 5458 | } |
| 5459 | if (isa<UndefValue>(Op1)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5460 | if (I.getOpcode() == Instruction::AShr) // X >>s undef -> X |
| 5461 | return ReplaceInstUsesWith(I, Op0); |
| 5462 | else // X << undef, X >>u undef -> 0 |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5463 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5464 | } |
| 5465 | |
Chris Lattner | de2b660 | 2006-11-10 23:38:52 +0000 | [diff] [blame] | 5466 | // ashr int -1, X = -1 (for any arithmetic shift rights of ~0) |
| 5467 | if (I.getOpcode() == Instruction::AShr) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5468 | if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0)) |
Chris Lattner | de2b660 | 2006-11-10 23:38:52 +0000 | [diff] [blame] | 5469 | if (CSI->isAllOnesValue()) |
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 5470 | return ReplaceInstUsesWith(I, CSI); |
| 5471 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 5472 | // Try to fold constant and into select arguments. |
| 5473 | if (isa<Constant>(Op0)) |
| 5474 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 5475 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 5476 | return R; |
| 5477 | |
Chris Lattner | 120347e | 2005-05-08 17:34:56 +0000 | [diff] [blame] | 5478 | // See if we can turn a signed shr into an unsigned shr. |
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 5479 | if (I.isArithmeticShift()) { |
Chris Lattner | 3bedbd9 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 5480 | if (MaskedValueIsZero(Op0, |
| 5481 | 1ULL << (I.getType()->getPrimitiveSizeInBits()-1))) { |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 5482 | return BinaryOperator::createLShr(Op0, Op1, I.getName()); |
Chris Lattner | 120347e | 2005-05-08 17:34:56 +0000 | [diff] [blame] | 5483 | } |
| 5484 | } |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 5485 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5486 | if (ConstantInt *CUI = dyn_cast<ConstantInt>(Op1)) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 5487 | if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I)) |
| 5488 | return Res; |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5489 | return 0; |
| 5490 | } |
| 5491 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5492 | Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 5493 | BinaryOperator &I) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5494 | bool isLeftShift = I.getOpcode() == Instruction::Shl; |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5495 | |
Chris Lattner | 8d6bbdb | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 5496 | // See if we can simplify any instructions used by the instruction whose sole |
| 5497 | // purpose is to compute bits we don't care about. |
| 5498 | uint64_t KnownZero, KnownOne; |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 5499 | if (SimplifyDemandedBits(&I, cast<IntegerType>(I.getType())->getBitMask(), |
Chris Lattner | 8d6bbdb | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 5500 | KnownZero, KnownOne)) |
| 5501 | return &I; |
| 5502 | |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5503 | // shl uint X, 32 = 0 and shr ubyte Y, 9 = 0, ... just don't eliminate shr |
| 5504 | // of a signed value. |
| 5505 | // |
| 5506 | unsigned TypeBits = Op0->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5507 | if (Op1->getZExtValue() >= TypeBits) { |
Chris Lattner | 0737c24 | 2007-02-02 05:29:55 +0000 | [diff] [blame] | 5508 | if (I.getOpcode() != Instruction::AShr) |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5509 | return ReplaceInstUsesWith(I, Constant::getNullValue(Op0->getType())); |
| 5510 | else { |
Chris Lattner | 0737c24 | 2007-02-02 05:29:55 +0000 | [diff] [blame] | 5511 | I.setOperand(1, ConstantInt::get(I.getType(), TypeBits-1)); |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5512 | return &I; |
Chris Lattner | 8adac75 | 2004-02-23 20:30:06 +0000 | [diff] [blame] | 5513 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5514 | } |
| 5515 | |
| 5516 | // ((X*C1) << C2) == (X * (C1 << C2)) |
| 5517 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0)) |
| 5518 | if (BO->getOpcode() == Instruction::Mul && isLeftShift) |
| 5519 | if (Constant *BOOp = dyn_cast<Constant>(BO->getOperand(1))) |
| 5520 | return BinaryOperator::createMul(BO->getOperand(0), |
| 5521 | ConstantExpr::getShl(BOOp, Op1)); |
| 5522 | |
| 5523 | // Try to fold constant and into select arguments. |
| 5524 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 5525 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 5526 | return R; |
| 5527 | if (isa<PHINode>(Op0)) |
| 5528 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 5529 | return NV; |
| 5530 | |
| 5531 | if (Op0->hasOneUse()) { |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5532 | if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0)) { |
| 5533 | // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C) |
| 5534 | Value *V1, *V2; |
| 5535 | ConstantInt *CC; |
| 5536 | switch (Op0BO->getOpcode()) { |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 5537 | default: break; |
| 5538 | case Instruction::Add: |
| 5539 | case Instruction::And: |
| 5540 | case Instruction::Or: |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 5541 | case Instruction::Xor: { |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 5542 | // These operators commute. |
| 5543 | // Turn (Y + (X >> C)) << C -> (X + (Y << C)) & (~0 << C) |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5544 | if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() && |
| 5545 | match(Op0BO->getOperand(1), |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5546 | m_Shr(m_Value(V1), m_ConstantInt(CC))) && CC == Op1) { |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 5547 | Instruction *YS = BinaryOperator::createShl( |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5548 | Op0BO->getOperand(0), Op1, |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5549 | Op0BO->getName()); |
| 5550 | InsertNewInstBefore(YS, I); // (Y << C) |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 5551 | Instruction *X = |
| 5552 | BinaryOperator::create(Op0BO->getOpcode(), YS, V1, |
| 5553 | Op0BO->getOperand(1)->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5554 | InsertNewInstBefore(X, I); // (X + (Y << C)) |
| 5555 | Constant *C2 = ConstantInt::getAllOnesValue(X->getType()); |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5556 | C2 = ConstantExpr::getShl(C2, Op1); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5557 | return BinaryOperator::createAnd(X, C2); |
| 5558 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5559 | |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5560 | // Turn (Y + ((X >> C) & CC)) << C -> ((X & (CC << C)) + (Y << C)) |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 5561 | Value *Op0BOOp1 = Op0BO->getOperand(1); |
| 5562 | if (isLeftShift && Op0BOOp1->hasOneUse() && V2 == Op1 && |
| 5563 | match(Op0BOOp1, |
| 5564 | m_And(m_Shr(m_Value(V1), m_Value(V2)),m_ConstantInt(CC))) && |
| 5565 | cast<BinaryOperator>(Op0BOOp1)->getOperand(0)-> hasOneUse()) { |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 5566 | Instruction *YS = BinaryOperator::createShl( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 5567 | Op0BO->getOperand(0), Op1, |
| 5568 | Op0BO->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5569 | InsertNewInstBefore(YS, I); // (Y << C) |
| 5570 | Instruction *XM = |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5571 | BinaryOperator::createAnd(V1, ConstantExpr::getShl(CC, Op1), |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5572 | V1->getName()+".mask"); |
| 5573 | InsertNewInstBefore(XM, I); // X & (CC << C) |
| 5574 | |
| 5575 | return BinaryOperator::create(Op0BO->getOpcode(), YS, XM); |
| 5576 | } |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 5577 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5578 | |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 5579 | // FALL THROUGH. |
| 5580 | case Instruction::Sub: { |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 5581 | // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C) |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5582 | if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() && |
| 5583 | match(Op0BO->getOperand(0), |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5584 | m_Shr(m_Value(V1), m_ConstantInt(CC))) && CC == Op1) { |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 5585 | Instruction *YS = BinaryOperator::createShl( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 5586 | Op0BO->getOperand(1), Op1, |
| 5587 | Op0BO->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5588 | InsertNewInstBefore(YS, I); // (Y << C) |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 5589 | Instruction *X = |
Chris Lattner | 13d4ab4 | 2006-05-31 21:14:00 +0000 | [diff] [blame] | 5590 | BinaryOperator::create(Op0BO->getOpcode(), V1, YS, |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 5591 | Op0BO->getOperand(0)->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5592 | InsertNewInstBefore(X, I); // (X + (Y << C)) |
| 5593 | Constant *C2 = ConstantInt::getAllOnesValue(X->getType()); |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5594 | C2 = ConstantExpr::getShl(C2, Op1); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5595 | return BinaryOperator::createAnd(X, C2); |
| 5596 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5597 | |
Chris Lattner | 13d4ab4 | 2006-05-31 21:14:00 +0000 | [diff] [blame] | 5598 | // Turn (((X >> C)&CC) + Y) << C -> (X + (Y << C)) & (CC << C) |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5599 | if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() && |
| 5600 | match(Op0BO->getOperand(0), |
| 5601 | m_And(m_Shr(m_Value(V1), m_Value(V2)), |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5602 | m_ConstantInt(CC))) && V2 == Op1 && |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 5603 | cast<BinaryOperator>(Op0BO->getOperand(0)) |
| 5604 | ->getOperand(0)->hasOneUse()) { |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 5605 | Instruction *YS = BinaryOperator::createShl( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 5606 | Op0BO->getOperand(1), Op1, |
| 5607 | Op0BO->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5608 | InsertNewInstBefore(YS, I); // (Y << C) |
| 5609 | Instruction *XM = |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5610 | BinaryOperator::createAnd(V1, ConstantExpr::getShl(CC, Op1), |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5611 | V1->getName()+".mask"); |
| 5612 | InsertNewInstBefore(XM, I); // X & (CC << C) |
| 5613 | |
Chris Lattner | 13d4ab4 | 2006-05-31 21:14:00 +0000 | [diff] [blame] | 5614 | return BinaryOperator::create(Op0BO->getOpcode(), XM, YS); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5615 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5616 | |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 5617 | break; |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 5618 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5619 | } |
| 5620 | |
| 5621 | |
| 5622 | // If the operand is an bitwise operator with a constant RHS, and the |
| 5623 | // shift is the only use, we can pull it out of the shift. |
| 5624 | if (ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand(1))) { |
| 5625 | bool isValid = true; // Valid only for And, Or, Xor |
| 5626 | bool highBitSet = false; // Transform if high bit of constant set? |
| 5627 | |
| 5628 | switch (Op0BO->getOpcode()) { |
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 5629 | default: isValid = false; break; // Do not perform transform! |
Chris Lattner | 1f7e160 | 2004-10-08 03:46:20 +0000 | [diff] [blame] | 5630 | case Instruction::Add: |
| 5631 | isValid = isLeftShift; |
| 5632 | break; |
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 5633 | case Instruction::Or: |
| 5634 | case Instruction::Xor: |
| 5635 | highBitSet = false; |
| 5636 | break; |
| 5637 | case Instruction::And: |
| 5638 | highBitSet = true; |
| 5639 | break; |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5640 | } |
| 5641 | |
| 5642 | // If this is a signed shift right, and the high bit is modified |
| 5643 | // by the logical operation, do not perform the transformation. |
| 5644 | // The highBitSet boolean indicates the value of the high bit of |
| 5645 | // the constant which would cause it to be modified for this |
| 5646 | // operation. |
| 5647 | // |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5648 | if (isValid && !isLeftShift && I.getOpcode() == Instruction::AShr) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5649 | uint64_t Val = Op0C->getZExtValue(); |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5650 | isValid = ((Val & (1 << (TypeBits-1))) != 0) == highBitSet; |
| 5651 | } |
| 5652 | |
| 5653 | if (isValid) { |
| 5654 | Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, Op1); |
| 5655 | |
| 5656 | Instruction *NewShift = |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 5657 | BinaryOperator::create(I.getOpcode(), Op0BO->getOperand(0), Op1); |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5658 | InsertNewInstBefore(NewShift, I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 5659 | NewShift->takeName(Op0BO); |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5660 | |
| 5661 | return BinaryOperator::create(Op0BO->getOpcode(), NewShift, |
| 5662 | NewRHS); |
| 5663 | } |
| 5664 | } |
| 5665 | } |
| 5666 | } |
| 5667 | |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5668 | // 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] | 5669 | BinaryOperator *ShiftOp = dyn_cast<BinaryOperator>(Op0); |
| 5670 | if (ShiftOp && !ShiftOp->isShift()) |
| 5671 | ShiftOp = 0; |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5672 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5673 | if (ShiftOp && isa<ConstantInt>(ShiftOp->getOperand(1))) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5674 | ConstantInt *ShiftAmt1C = cast<ConstantInt>(ShiftOp->getOperand(1)); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5675 | unsigned ShiftAmt1 = (unsigned)ShiftAmt1C->getZExtValue(); |
| 5676 | unsigned ShiftAmt2 = (unsigned)Op1->getZExtValue(); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5677 | assert(ShiftAmt2 != 0 && "Should have been simplified earlier"); |
| 5678 | if (ShiftAmt1 == 0) return 0; // Will be simplified in the future. |
| 5679 | Value *X = ShiftOp->getOperand(0); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5680 | |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5681 | unsigned AmtSum = ShiftAmt1+ShiftAmt2; // Fold into one big shift. |
| 5682 | if (AmtSum > I.getType()->getPrimitiveSizeInBits()) |
| 5683 | AmtSum = I.getType()->getPrimitiveSizeInBits(); |
| 5684 | |
| 5685 | const IntegerType *Ty = cast<IntegerType>(I.getType()); |
| 5686 | |
| 5687 | // Check for (X << c1) << c2 and (X >> c1) >> c2 |
Chris Lattner | 7f3da2d | 2007-02-03 23:28:07 +0000 | [diff] [blame] | 5688 | if (I.getOpcode() == ShiftOp->getOpcode()) { |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5689 | return BinaryOperator::create(I.getOpcode(), X, |
| 5690 | ConstantInt::get(Ty, AmtSum)); |
| 5691 | } else if (ShiftOp->getOpcode() == Instruction::LShr && |
| 5692 | I.getOpcode() == Instruction::AShr) { |
| 5693 | // ((X >>u C1) >>s C2) -> (X >>u (C1+C2)) since C1 != 0. |
| 5694 | return BinaryOperator::createLShr(X, ConstantInt::get(Ty, AmtSum)); |
| 5695 | } else if (ShiftOp->getOpcode() == Instruction::AShr && |
| 5696 | I.getOpcode() == Instruction::LShr) { |
| 5697 | // ((X >>s C1) >>u C2) -> ((X >>s (C1+C2)) & mask) since C1 != 0. |
| 5698 | Instruction *Shift = |
| 5699 | BinaryOperator::createAShr(X, ConstantInt::get(Ty, AmtSum)); |
| 5700 | InsertNewInstBefore(Shift, I); |
| 5701 | |
| 5702 | uint64_t Mask = Ty->getBitMask() >> ShiftAmt2; |
| 5703 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Ty, Mask)); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5704 | } |
| 5705 | |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5706 | // Okay, if we get here, one shift must be left, and the other shift must be |
| 5707 | // right. See if the amounts are equal. |
| 5708 | if (ShiftAmt1 == ShiftAmt2) { |
| 5709 | // If we have ((X >>? C) << C), turn this into X & (-1 << C). |
| 5710 | if (I.getOpcode() == Instruction::Shl) { |
Chris Lattner | 4f3ebab | 2007-02-05 04:09:35 +0000 | [diff] [blame] | 5711 | uint64_t Mask = Ty->getBitMask() << ShiftAmt1; |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5712 | return BinaryOperator::createAnd(X, ConstantInt::get(Ty, Mask)); |
| 5713 | } |
| 5714 | // If we have ((X << C) >>u C), turn this into X & (-1 >>u C). |
| 5715 | if (I.getOpcode() == Instruction::LShr) { |
Chris Lattner | 4f3ebab | 2007-02-05 04:09:35 +0000 | [diff] [blame] | 5716 | uint64_t Mask = Ty->getBitMask() >> ShiftAmt1; |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5717 | return BinaryOperator::createAnd(X, ConstantInt::get(Ty, Mask)); |
| 5718 | } |
| 5719 | // We can simplify ((X << C) >>s C) into a trunc + sext. |
| 5720 | // NOTE: we could do this for any C, but that would make 'unusual' integer |
| 5721 | // types. For now, just stick to ones well-supported by the code |
| 5722 | // generators. |
| 5723 | const Type *SExtType = 0; |
| 5724 | switch (Ty->getBitWidth() - ShiftAmt1) { |
| 5725 | case 8 : SExtType = Type::Int8Ty; break; |
| 5726 | case 16: SExtType = Type::Int16Ty; break; |
| 5727 | case 32: SExtType = Type::Int32Ty; break; |
| 5728 | default: break; |
| 5729 | } |
| 5730 | if (SExtType) { |
| 5731 | Instruction *NewTrunc = new TruncInst(X, SExtType, "sext"); |
| 5732 | InsertNewInstBefore(NewTrunc, I); |
| 5733 | return new SExtInst(NewTrunc, Ty); |
| 5734 | } |
| 5735 | // Otherwise, we can't handle it yet. |
| 5736 | } else if (ShiftAmt1 < ShiftAmt2) { |
| 5737 | unsigned ShiftDiff = ShiftAmt2-ShiftAmt1; |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5738 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 5739 | // (X >>? C1) << C2 --> X << (C2-C1) & (-1 << C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5740 | if (I.getOpcode() == Instruction::Shl) { |
| 5741 | assert(ShiftOp->getOpcode() == Instruction::LShr || |
| 5742 | ShiftOp->getOpcode() == Instruction::AShr); |
Chris Lattner | e8d56c5 | 2006-01-07 01:32:28 +0000 | [diff] [blame] | 5743 | Instruction *Shift = |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5744 | BinaryOperator::createShl(X, ConstantInt::get(Ty, ShiftDiff)); |
Chris Lattner | e8d56c5 | 2006-01-07 01:32:28 +0000 | [diff] [blame] | 5745 | InsertNewInstBefore(Shift, I); |
| 5746 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 5747 | uint64_t Mask = Ty->getBitMask() << ShiftAmt2; |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5748 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Ty, Mask)); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5749 | } |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5750 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 5751 | // (X << C1) >>u C2 --> X >>u (C2-C1) & (-1 >> C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5752 | if (I.getOpcode() == Instruction::LShr) { |
| 5753 | assert(ShiftOp->getOpcode() == Instruction::Shl); |
| 5754 | Instruction *Shift = |
| 5755 | BinaryOperator::createLShr(X, ConstantInt::get(Ty, ShiftDiff)); |
| 5756 | InsertNewInstBefore(Shift, I); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5757 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 5758 | uint64_t Mask = Ty->getBitMask() >> ShiftAmt2; |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5759 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Ty, Mask)); |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 5760 | } |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5761 | |
| 5762 | // We can't handle (X << C1) >>s C2, it shifts arbitrary bits in. |
| 5763 | } else { |
| 5764 | assert(ShiftAmt2 < ShiftAmt1); |
| 5765 | unsigned ShiftDiff = ShiftAmt1-ShiftAmt2; |
| 5766 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 5767 | // (X >>? C1) << C2 --> X >>? (C1-C2) & (-1 << C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5768 | if (I.getOpcode() == Instruction::Shl) { |
| 5769 | assert(ShiftOp->getOpcode() == Instruction::LShr || |
| 5770 | ShiftOp->getOpcode() == Instruction::AShr); |
| 5771 | Instruction *Shift = |
| 5772 | BinaryOperator::create(ShiftOp->getOpcode(), X, |
| 5773 | ConstantInt::get(Ty, ShiftDiff)); |
| 5774 | InsertNewInstBefore(Shift, I); |
| 5775 | |
| 5776 | uint64_t Mask = Ty->getBitMask() << ShiftAmt2; |
| 5777 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Ty, Mask)); |
| 5778 | } |
| 5779 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 5780 | // (X << C1) >>u C2 --> X << (C1-C2) & (-1 >> C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5781 | if (I.getOpcode() == Instruction::LShr) { |
| 5782 | assert(ShiftOp->getOpcode() == Instruction::Shl); |
| 5783 | Instruction *Shift = |
| 5784 | BinaryOperator::createShl(X, ConstantInt::get(Ty, ShiftDiff)); |
| 5785 | InsertNewInstBefore(Shift, I); |
| 5786 | |
| 5787 | uint64_t Mask = Ty->getBitMask() >> ShiftAmt2; |
| 5788 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Ty, Mask)); |
| 5789 | } |
| 5790 | |
| 5791 | // 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] | 5792 | } |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5793 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5794 | return 0; |
| 5795 | } |
| 5796 | |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 5797 | |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 5798 | /// DecomposeSimpleLinearExpr - Analyze 'Val', seeing if it is a simple linear |
| 5799 | /// expression. If so, decompose it, returning some value X, such that Val is |
| 5800 | /// X*Scale+Offset. |
| 5801 | /// |
| 5802 | static Value *DecomposeSimpleLinearExpr(Value *Val, unsigned &Scale, |
| 5803 | unsigned &Offset) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 5804 | assert(Val->getType() == Type::Int32Ty && "Unexpected allocation size type!"); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5805 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 5806 | Offset = CI->getZExtValue(); |
| 5807 | Scale = 1; |
| 5808 | return ConstantInt::get(Type::Int32Ty, 0); |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 5809 | } else if (Instruction *I = dyn_cast<Instruction>(Val)) { |
| 5810 | if (I->getNumOperands() == 2) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5811 | if (ConstantInt *CUI = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 5812 | if (I->getOpcode() == Instruction::Shl) { |
| 5813 | // This is a value scaled by '1 << the shift amt'. |
| 5814 | Scale = 1U << CUI->getZExtValue(); |
| 5815 | Offset = 0; |
| 5816 | return I->getOperand(0); |
| 5817 | } else if (I->getOpcode() == Instruction::Mul) { |
| 5818 | // This value is scaled by 'CUI'. |
| 5819 | Scale = CUI->getZExtValue(); |
| 5820 | Offset = 0; |
| 5821 | return I->getOperand(0); |
| 5822 | } else if (I->getOpcode() == Instruction::Add) { |
| 5823 | // We have X+C. Check to see if we really have (X*C2)+C1, |
| 5824 | // where C1 is divisible by C2. |
| 5825 | unsigned SubScale; |
| 5826 | Value *SubVal = |
| 5827 | DecomposeSimpleLinearExpr(I->getOperand(0), SubScale, Offset); |
| 5828 | Offset += CUI->getZExtValue(); |
| 5829 | if (SubScale > 1 && (Offset % SubScale == 0)) { |
| 5830 | Scale = SubScale; |
| 5831 | return SubVal; |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 5832 | } |
| 5833 | } |
| 5834 | } |
| 5835 | } |
| 5836 | } |
| 5837 | |
| 5838 | // Otherwise, we can't look past this. |
| 5839 | Scale = 1; |
| 5840 | Offset = 0; |
| 5841 | return Val; |
| 5842 | } |
| 5843 | |
| 5844 | |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 5845 | /// PromoteCastOfAllocation - If we find a cast of an allocation instruction, |
| 5846 | /// try to eliminate the cast by moving the type information into the alloc. |
| 5847 | Instruction *InstCombiner::PromoteCastOfAllocation(CastInst &CI, |
| 5848 | AllocationInst &AI) { |
| 5849 | const PointerType *PTy = dyn_cast<PointerType>(CI.getType()); |
Chris Lattner | 0ddac2a | 2005-10-27 05:53:56 +0000 | [diff] [blame] | 5850 | if (!PTy) return 0; // Not casting the allocation to a pointer type. |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 5851 | |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 5852 | // Remove any uses of AI that are dead. |
| 5853 | assert(!CI.use_empty() && "Dead instructions should be removed earlier!"); |
Chris Lattner | 535014f | 2007-02-15 22:52:10 +0000 | [diff] [blame] | 5854 | |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 5855 | for (Value::use_iterator UI = AI.use_begin(), E = AI.use_end(); UI != E; ) { |
| 5856 | Instruction *User = cast<Instruction>(*UI++); |
| 5857 | if (isInstructionTriviallyDead(User)) { |
| 5858 | while (UI != E && *UI == User) |
| 5859 | ++UI; // If this instruction uses AI more than once, don't break UI. |
| 5860 | |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 5861 | ++NumDeadInst; |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 5862 | DOUT << "IC: DCE: " << *User; |
Chris Lattner | f22a5c6 | 2007-03-02 19:59:19 +0000 | [diff] [blame] | 5863 | EraseInstFromFunction(*User); |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 5864 | } |
| 5865 | } |
| 5866 | |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 5867 | // Get the type really allocated and the type casted to. |
| 5868 | const Type *AllocElTy = AI.getAllocatedType(); |
| 5869 | const Type *CastElTy = PTy->getElementType(); |
| 5870 | if (!AllocElTy->isSized() || !CastElTy->isSized()) return 0; |
Chris Lattner | 18e78bb | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 5871 | |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 5872 | unsigned AllocElTyAlign = TD->getABITypeAlignment(AllocElTy); |
| 5873 | unsigned CastElTyAlign = TD->getABITypeAlignment(CastElTy); |
Chris Lattner | 18e78bb | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 5874 | if (CastElTyAlign < AllocElTyAlign) return 0; |
| 5875 | |
Chris Lattner | 39387a5 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 5876 | // If the allocation has multiple uses, only promote it if we are strictly |
| 5877 | // increasing the alignment of the resultant allocation. If we keep it the |
| 5878 | // same, we open the door to infinite loops of various kinds. |
| 5879 | if (!AI.hasOneUse() && CastElTyAlign == AllocElTyAlign) return 0; |
| 5880 | |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 5881 | uint64_t AllocElTySize = TD->getTypeSize(AllocElTy); |
| 5882 | uint64_t CastElTySize = TD->getTypeSize(CastElTy); |
Chris Lattner | 0ddac2a | 2005-10-27 05:53:56 +0000 | [diff] [blame] | 5883 | if (CastElTySize == 0 || AllocElTySize == 0) return 0; |
Chris Lattner | 18e78bb | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 5884 | |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 5885 | // See if we can satisfy the modulus by pulling a scale out of the array |
| 5886 | // size argument. |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 5887 | unsigned ArraySizeScale, ArrayOffset; |
| 5888 | Value *NumElements = // See if the array size is a decomposable linear expr. |
| 5889 | DecomposeSimpleLinearExpr(AI.getOperand(0), ArraySizeScale, ArrayOffset); |
| 5890 | |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 5891 | // If we can now satisfy the modulus, by using a non-1 scale, we really can |
| 5892 | // do the xform. |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 5893 | if ((AllocElTySize*ArraySizeScale) % CastElTySize != 0 || |
| 5894 | (AllocElTySize*ArrayOffset ) % CastElTySize != 0) return 0; |
Chris Lattner | 8142b0a | 2005-10-27 06:12:00 +0000 | [diff] [blame] | 5895 | |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 5896 | unsigned Scale = (AllocElTySize*ArraySizeScale)/CastElTySize; |
| 5897 | Value *Amt = 0; |
| 5898 | if (Scale == 1) { |
| 5899 | Amt = NumElements; |
| 5900 | } else { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5901 | // If the allocation size is constant, form a constant mul expression |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 5902 | Amt = ConstantInt::get(Type::Int32Ty, Scale); |
| 5903 | if (isa<ConstantInt>(NumElements)) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5904 | Amt = ConstantExpr::getMul( |
| 5905 | cast<ConstantInt>(NumElements), cast<ConstantInt>(Amt)); |
| 5906 | // otherwise multiply the amount and the number of elements |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 5907 | else if (Scale != 1) { |
| 5908 | Instruction *Tmp = BinaryOperator::createMul(Amt, NumElements, "tmp"); |
| 5909 | Amt = InsertNewInstBefore(Tmp, AI); |
Chris Lattner | 8142b0a | 2005-10-27 06:12:00 +0000 | [diff] [blame] | 5910 | } |
Chris Lattner | 0ddac2a | 2005-10-27 05:53:56 +0000 | [diff] [blame] | 5911 | } |
| 5912 | |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 5913 | if (unsigned Offset = (AllocElTySize*ArrayOffset)/CastElTySize) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 5914 | Value *Off = ConstantInt::get(Type::Int32Ty, Offset); |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 5915 | Instruction *Tmp = BinaryOperator::createAdd(Amt, Off, "tmp"); |
| 5916 | Amt = InsertNewInstBefore(Tmp, AI); |
| 5917 | } |
| 5918 | |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 5919 | AllocationInst *New; |
| 5920 | if (isa<MallocInst>(AI)) |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 5921 | New = new MallocInst(CastElTy, Amt, AI.getAlignment()); |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 5922 | else |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 5923 | New = new AllocaInst(CastElTy, Amt, AI.getAlignment()); |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 5924 | InsertNewInstBefore(New, AI); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 5925 | New->takeName(&AI); |
Chris Lattner | 39387a5 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 5926 | |
| 5927 | // If the allocation has multiple uses, insert a cast and change all things |
| 5928 | // that used it to use the new cast. This will also hack on CI, but it will |
| 5929 | // die soon. |
| 5930 | if (!AI.hasOneUse()) { |
| 5931 | AddUsesToWorkList(AI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 5932 | // New is the allocation instruction, pointer typed. AI is the original |
| 5933 | // allocation instruction, also pointer typed. Thus, cast to use is BitCast. |
| 5934 | CastInst *NewCast = new BitCastInst(New, AI.getType(), "tmpcast"); |
Chris Lattner | 39387a5 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 5935 | InsertNewInstBefore(NewCast, AI); |
| 5936 | AI.replaceAllUsesWith(NewCast); |
| 5937 | } |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 5938 | return ReplaceInstUsesWith(CI, New); |
| 5939 | } |
| 5940 | |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 5941 | /// CanEvaluateInDifferentType - Return true if we can take the specified value |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 5942 | /// and return it as type Ty without inserting any new casts and without |
| 5943 | /// changing the computed value. This is used by code that tries to decide |
| 5944 | /// whether promoting or shrinking integer operations to wider or smaller types |
| 5945 | /// will allow us to eliminate a truncate or extend. |
| 5946 | /// |
| 5947 | /// This is a truncation operation if Ty is smaller than V->getType(), or an |
| 5948 | /// extension operation if Ty is larger. |
| 5949 | static bool CanEvaluateInDifferentType(Value *V, const IntegerType *Ty, |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 5950 | int &NumCastsRemoved) { |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 5951 | // We can always evaluate constants in another type. |
| 5952 | if (isa<ConstantInt>(V)) |
| 5953 | return true; |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 5954 | |
| 5955 | Instruction *I = dyn_cast<Instruction>(V); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 5956 | if (!I) return false; |
| 5957 | |
| 5958 | const IntegerType *OrigTy = cast<IntegerType>(V->getType()); |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 5959 | |
| 5960 | switch (I->getOpcode()) { |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 5961 | case Instruction::Add: |
| 5962 | case Instruction::Sub: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 5963 | case Instruction::And: |
| 5964 | case Instruction::Or: |
| 5965 | case Instruction::Xor: |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 5966 | if (!I->hasOneUse()) return false; |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 5967 | // These operators can all arbitrarily be extended or truncated. |
| 5968 | return CanEvaluateInDifferentType(I->getOperand(0), Ty, NumCastsRemoved) && |
| 5969 | CanEvaluateInDifferentType(I->getOperand(1), Ty, NumCastsRemoved); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 5970 | |
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 5971 | case Instruction::Shl: |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 5972 | if (!I->hasOneUse()) return false; |
| 5973 | // If we are truncating the result of this SHL, and if it's a shift of a |
| 5974 | // constant amount, we can always perform a SHL in a smaller type. |
| 5975 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 5976 | if (Ty->getBitWidth() < OrigTy->getBitWidth() && |
| 5977 | CI->getZExtValue() < Ty->getBitWidth()) |
| 5978 | return CanEvaluateInDifferentType(I->getOperand(0), Ty,NumCastsRemoved); |
| 5979 | } |
| 5980 | break; |
| 5981 | case Instruction::LShr: |
| 5982 | if (!I->hasOneUse()) return false; |
| 5983 | // If this is a truncate of a logical shr, we can truncate it to a smaller |
| 5984 | // lshr iff we know that the bits we would otherwise be shifting in are |
| 5985 | // already zeros. |
| 5986 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 5987 | if (Ty->getBitWidth() < OrigTy->getBitWidth() && |
| 5988 | MaskedValueIsZero(I->getOperand(0), |
| 5989 | OrigTy->getBitMask() & ~Ty->getBitMask()) && |
| 5990 | CI->getZExtValue() < Ty->getBitWidth()) { |
| 5991 | return CanEvaluateInDifferentType(I->getOperand(0), Ty, NumCastsRemoved); |
| 5992 | } |
| 5993 | } |
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 5994 | break; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 5995 | case Instruction::Trunc: |
| 5996 | case Instruction::ZExt: |
| 5997 | case Instruction::SExt: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 5998 | // If this is a cast from the destination type, we can trivially eliminate |
| 5999 | // it, and this will remove a cast overall. |
| 6000 | if (I->getOperand(0)->getType() == Ty) { |
Chris Lattner | d228018 | 2006-06-28 17:34:50 +0000 | [diff] [blame] | 6001 | // If the first operand is itself a cast, and is eliminable, do not count |
| 6002 | // this as an eliminable cast. We would prefer to eliminate those two |
| 6003 | // casts first. |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 6004 | if (isa<CastInst>(I->getOperand(0))) |
Chris Lattner | d228018 | 2006-06-28 17:34:50 +0000 | [diff] [blame] | 6005 | return true; |
| 6006 | |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6007 | ++NumCastsRemoved; |
| 6008 | return true; |
| 6009 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6010 | break; |
| 6011 | default: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6012 | // TODO: Can handle more cases here. |
| 6013 | break; |
| 6014 | } |
| 6015 | |
| 6016 | return false; |
| 6017 | } |
| 6018 | |
| 6019 | /// EvaluateInDifferentType - Given an expression that |
| 6020 | /// CanEvaluateInDifferentType returns true for, actually insert the code to |
| 6021 | /// evaluate the expression. |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 6022 | Value *InstCombiner::EvaluateInDifferentType(Value *V, const Type *Ty, |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6023 | bool isSigned) { |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6024 | if (Constant *C = dyn_cast<Constant>(V)) |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 6025 | return ConstantExpr::getIntegerCast(C, Ty, isSigned /*Sext or ZExt*/); |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6026 | |
| 6027 | // Otherwise, it must be an instruction. |
| 6028 | Instruction *I = cast<Instruction>(V); |
Chris Lattner | 01859e8 | 2006-05-20 23:14:03 +0000 | [diff] [blame] | 6029 | Instruction *Res = 0; |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6030 | switch (I->getOpcode()) { |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6031 | case Instruction::Add: |
| 6032 | case Instruction::Sub: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6033 | case Instruction::And: |
| 6034 | case Instruction::Or: |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6035 | case Instruction::Xor: |
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 6036 | case Instruction::AShr: |
| 6037 | case Instruction::LShr: |
| 6038 | case Instruction::Shl: { |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 6039 | Value *LHS = EvaluateInDifferentType(I->getOperand(0), Ty, isSigned); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6040 | Value *RHS = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned); |
| 6041 | Res = BinaryOperator::create((Instruction::BinaryOps)I->getOpcode(), |
| 6042 | LHS, RHS, I->getName()); |
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 6043 | break; |
| 6044 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6045 | case Instruction::Trunc: |
| 6046 | case Instruction::ZExt: |
| 6047 | case Instruction::SExt: |
| 6048 | case Instruction::BitCast: |
| 6049 | // If the source type of the cast is the type we're trying for then we can |
| 6050 | // 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] | 6051 | if (I->getOperand(0)->getType() == Ty) |
| 6052 | return I->getOperand(0); |
| 6053 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6054 | // Some other kind of cast, which shouldn't happen, so just .. |
| 6055 | // FALL THROUGH |
| 6056 | default: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6057 | // TODO: Can handle more cases here. |
| 6058 | assert(0 && "Unreachable!"); |
| 6059 | break; |
| 6060 | } |
| 6061 | |
| 6062 | return InsertNewInstBefore(Res, *I); |
| 6063 | } |
| 6064 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6065 | /// @brief Implement the transforms common to all CastInst visitors. |
| 6066 | Instruction *InstCombiner::commonCastTransforms(CastInst &CI) { |
Chris Lattner | 79d35b3 | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 6067 | Value *Src = CI.getOperand(0); |
| 6068 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6069 | // Casting undef to anything results in undef so might as just replace it and |
| 6070 | // get rid of the cast. |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 6071 | if (isa<UndefValue>(Src)) // cast undef -> undef |
| 6072 | return ReplaceInstUsesWith(CI, UndefValue::get(CI.getType())); |
| 6073 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6074 | // Many cases of "cast of a cast" are eliminable. If its eliminable we just |
| 6075 | // eliminate it now. |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 6076 | if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6077 | if (Instruction::CastOps opc = |
| 6078 | isEliminableCastPair(CSrc, CI.getOpcode(), CI.getType(), TD)) { |
| 6079 | // The first cast (CSrc) is eliminable so we need to fix up or replace |
| 6080 | // the second cast (CI). CSrc will then have a good chance of being dead. |
| 6081 | return CastInst::create(opc, CSrc->getOperand(0), CI.getType()); |
Chris Lattner | 8fd217c | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 6082 | } |
| 6083 | } |
Chris Lattner | a710ddc | 2004-05-25 04:29:21 +0000 | [diff] [blame] | 6084 | |
Chris Lattner | 797249b | 2003-06-21 23:12:02 +0000 | [diff] [blame] | 6085 | // If casting the result of a getelementptr instruction with no offset, turn |
| 6086 | // this into a cast of the original pointer! |
| 6087 | // |
Chris Lattner | 79d35b3 | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 6088 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Src)) { |
Chris Lattner | 797249b | 2003-06-21 23:12:02 +0000 | [diff] [blame] | 6089 | bool AllZeroOperands = true; |
| 6090 | for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i) |
| 6091 | if (!isa<Constant>(GEP->getOperand(i)) || |
| 6092 | !cast<Constant>(GEP->getOperand(i))->isNullValue()) { |
| 6093 | AllZeroOperands = false; |
| 6094 | break; |
| 6095 | } |
| 6096 | if (AllZeroOperands) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6097 | // Changing the cast operand is usually not a good idea but it is safe |
| 6098 | // here because the pointer operand is being replaced with another |
| 6099 | // pointer operand so the opcode doesn't need to change. |
Chris Lattner | 797249b | 2003-06-21 23:12:02 +0000 | [diff] [blame] | 6100 | CI.setOperand(0, GEP->getOperand(0)); |
| 6101 | return &CI; |
| 6102 | } |
| 6103 | } |
Chris Lattner | 13c654a | 2006-11-21 17:05:13 +0000 | [diff] [blame] | 6104 | |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 6105 | // If we are casting a malloc or alloca to a pointer to a type of the same |
| 6106 | // size, rewrite the allocation instruction to allocate the "right" type. |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 6107 | if (AllocationInst *AI = dyn_cast<AllocationInst>(Src)) |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6108 | if (Instruction *V = PromoteCastOfAllocation(CI, *AI)) |
| 6109 | return V; |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 6110 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6111 | // 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] | 6112 | if (SelectInst *SI = dyn_cast<SelectInst>(Src)) |
| 6113 | if (Instruction *NV = FoldOpIntoSelect(CI, SI, this)) |
| 6114 | return NV; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6115 | |
| 6116 | // 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] | 6117 | if (isa<PHINode>(Src)) |
| 6118 | if (Instruction *NV = FoldOpIntoPhi(CI)) |
| 6119 | return NV; |
Chris Lattner | 9fb9213 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 6120 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6121 | return 0; |
| 6122 | } |
| 6123 | |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6124 | /// Only the TRUNC, ZEXT, SEXT, and BITCAST can both operand and result as |
| 6125 | /// integer types. This function implements the common transforms for all those |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6126 | /// cases. |
| 6127 | /// @brief Implement the transforms common to CastInst with integer operands |
| 6128 | Instruction *InstCombiner::commonIntCastTransforms(CastInst &CI) { |
| 6129 | if (Instruction *Result = commonCastTransforms(CI)) |
| 6130 | return Result; |
| 6131 | |
| 6132 | Value *Src = CI.getOperand(0); |
| 6133 | const Type *SrcTy = Src->getType(); |
| 6134 | const Type *DestTy = CI.getType(); |
| 6135 | unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits(); |
| 6136 | unsigned DestBitSize = DestTy->getPrimitiveSizeInBits(); |
| 6137 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6138 | // See if we can simplify any instructions used by the LHS whose sole |
| 6139 | // purpose is to compute bits we don't care about. |
| 6140 | uint64_t KnownZero = 0, KnownOne = 0; |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 6141 | if (SimplifyDemandedBits(&CI, cast<IntegerType>(DestTy)->getBitMask(), |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6142 | KnownZero, KnownOne)) |
| 6143 | return &CI; |
| 6144 | |
| 6145 | // If the source isn't an instruction or has more than one use then we |
| 6146 | // can't do anything more. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6147 | Instruction *SrcI = dyn_cast<Instruction>(Src); |
| 6148 | if (!SrcI || !Src->hasOneUse()) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6149 | return 0; |
| 6150 | |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6151 | // Attempt to propagate the cast into the instruction for int->int casts. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6152 | int NumCastsRemoved = 0; |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6153 | if (!isa<BitCastInst>(CI) && |
| 6154 | CanEvaluateInDifferentType(SrcI, cast<IntegerType>(DestTy), |
| 6155 | NumCastsRemoved)) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6156 | // If this cast is a truncate, evaluting in a different type always |
| 6157 | // eliminates the cast, so it is always a win. If this is a noop-cast |
| 6158 | // this just removes a noop cast which isn't pointful, but simplifies |
| 6159 | // the code. If this is a zero-extension, we need to do an AND to |
| 6160 | // maintain the clear top-part of the computation, so we require that |
| 6161 | // the input have eliminated at least one cast. If this is a sign |
| 6162 | // extension, we insert two new casts (to do the extension) so we |
| 6163 | // require that two casts have been eliminated. |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6164 | bool DoXForm; |
| 6165 | switch (CI.getOpcode()) { |
| 6166 | default: |
| 6167 | // All the others use floating point so we shouldn't actually |
| 6168 | // get here because of the check above. |
| 6169 | assert(0 && "Unknown cast type"); |
| 6170 | case Instruction::Trunc: |
| 6171 | DoXForm = true; |
| 6172 | break; |
| 6173 | case Instruction::ZExt: |
| 6174 | DoXForm = NumCastsRemoved >= 1; |
| 6175 | break; |
| 6176 | case Instruction::SExt: |
| 6177 | DoXForm = NumCastsRemoved >= 2; |
| 6178 | break; |
| 6179 | case Instruction::BitCast: |
| 6180 | DoXForm = false; |
| 6181 | break; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6182 | } |
| 6183 | |
| 6184 | if (DoXForm) { |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 6185 | Value *Res = EvaluateInDifferentType(SrcI, DestTy, |
| 6186 | CI.getOpcode() == Instruction::SExt); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6187 | assert(Res->getType() == DestTy); |
| 6188 | switch (CI.getOpcode()) { |
| 6189 | default: assert(0 && "Unknown cast type!"); |
| 6190 | case Instruction::Trunc: |
| 6191 | case Instruction::BitCast: |
| 6192 | // Just replace this cast with the result. |
| 6193 | return ReplaceInstUsesWith(CI, Res); |
| 6194 | case Instruction::ZExt: { |
| 6195 | // We need to emit an AND to clear the high bits. |
| 6196 | assert(SrcBitSize < DestBitSize && "Not a zext?"); |
| 6197 | Constant *C = |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6198 | ConstantInt::get(Type::Int64Ty, (1ULL << SrcBitSize)-1); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6199 | if (DestBitSize < 64) |
| 6200 | C = ConstantExpr::getTrunc(C, DestTy); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6201 | return BinaryOperator::createAnd(Res, C); |
| 6202 | } |
| 6203 | case Instruction::SExt: |
| 6204 | // We need to emit a cast to truncate, then a cast to sext. |
| 6205 | return CastInst::create(Instruction::SExt, |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 6206 | InsertCastBefore(Instruction::Trunc, Res, Src->getType(), |
| 6207 | CI), DestTy); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6208 | } |
| 6209 | } |
| 6210 | } |
| 6211 | |
| 6212 | Value *Op0 = SrcI->getNumOperands() > 0 ? SrcI->getOperand(0) : 0; |
| 6213 | Value *Op1 = SrcI->getNumOperands() > 1 ? SrcI->getOperand(1) : 0; |
| 6214 | |
| 6215 | switch (SrcI->getOpcode()) { |
| 6216 | case Instruction::Add: |
| 6217 | case Instruction::Mul: |
| 6218 | case Instruction::And: |
| 6219 | case Instruction::Or: |
| 6220 | case Instruction::Xor: |
| 6221 | // If we are discarding information, or just changing the sign, |
| 6222 | // rewrite. |
| 6223 | if (DestBitSize <= SrcBitSize && DestBitSize != 1) { |
| 6224 | // Don't insert two casts if they cannot be eliminated. We allow |
| 6225 | // two casts to be inserted if the sizes are the same. This could |
| 6226 | // only be converting signedness, which is a noop. |
| 6227 | if (DestBitSize == SrcBitSize || |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6228 | !ValueRequiresCast(CI.getOpcode(), Op1, DestTy,TD) || |
| 6229 | !ValueRequiresCast(CI.getOpcode(), Op0, DestTy, TD)) { |
Reid Spencer | 7eb7638 | 2006-12-13 17:19:09 +0000 | [diff] [blame] | 6230 | Instruction::CastOps opcode = CI.getOpcode(); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 6231 | Value *Op0c = InsertOperandCastBefore(opcode, Op0, DestTy, SrcI); |
| 6232 | Value *Op1c = InsertOperandCastBefore(opcode, Op1, DestTy, SrcI); |
| 6233 | return BinaryOperator::create( |
| 6234 | cast<BinaryOperator>(SrcI)->getOpcode(), Op0c, Op1c); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6235 | } |
| 6236 | } |
| 6237 | |
| 6238 | // cast (xor bool X, true) to int --> xor (cast bool X to int), 1 |
| 6239 | if (isa<ZExtInst>(CI) && SrcBitSize == 1 && |
| 6240 | SrcI->getOpcode() == Instruction::Xor && |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6241 | Op1 == ConstantInt::getTrue() && |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6242 | (!Op0->hasOneUse() || !isa<CmpInst>(Op0))) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 6243 | Value *New = InsertOperandCastBefore(Instruction::ZExt, Op0, DestTy, &CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6244 | return BinaryOperator::createXor(New, ConstantInt::get(CI.getType(), 1)); |
| 6245 | } |
| 6246 | break; |
| 6247 | case Instruction::SDiv: |
| 6248 | case Instruction::UDiv: |
| 6249 | case Instruction::SRem: |
| 6250 | case Instruction::URem: |
| 6251 | // If we are just changing the sign, rewrite. |
| 6252 | if (DestBitSize == SrcBitSize) { |
| 6253 | // Don't insert two casts if they cannot be eliminated. We allow |
| 6254 | // two casts to be inserted if the sizes are the same. This could |
| 6255 | // only be converting signedness, which is a noop. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6256 | if (!ValueRequiresCast(CI.getOpcode(), Op1, DestTy, TD) || |
| 6257 | !ValueRequiresCast(CI.getOpcode(), Op0, DestTy, TD)) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 6258 | Value *Op0c = InsertOperandCastBefore(Instruction::BitCast, |
| 6259 | Op0, DestTy, SrcI); |
| 6260 | Value *Op1c = InsertOperandCastBefore(Instruction::BitCast, |
| 6261 | Op1, DestTy, SrcI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6262 | return BinaryOperator::create( |
| 6263 | cast<BinaryOperator>(SrcI)->getOpcode(), Op0c, Op1c); |
| 6264 | } |
| 6265 | } |
| 6266 | break; |
| 6267 | |
| 6268 | case Instruction::Shl: |
| 6269 | // Allow changing the sign of the source operand. Do not allow |
| 6270 | // changing the size of the shift, UNLESS the shift amount is a |
| 6271 | // constant. We must not change variable sized shifts to a smaller |
| 6272 | // size, because it is undefined to shift more bits out than exist |
| 6273 | // in the value. |
| 6274 | if (DestBitSize == SrcBitSize || |
| 6275 | (DestBitSize < SrcBitSize && isa<Constant>(Op1))) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 6276 | Instruction::CastOps opcode = (DestBitSize == SrcBitSize ? |
| 6277 | Instruction::BitCast : Instruction::Trunc); |
| 6278 | Value *Op0c = InsertOperandCastBefore(opcode, Op0, DestTy, SrcI); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6279 | Value *Op1c = InsertOperandCastBefore(opcode, Op1, DestTy, SrcI); |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 6280 | return BinaryOperator::createShl(Op0c, Op1c); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6281 | } |
| 6282 | break; |
| 6283 | case Instruction::AShr: |
| 6284 | // If this is a signed shr, and if all bits shifted in are about to be |
| 6285 | // truncated off, turn it into an unsigned shr to allow greater |
| 6286 | // simplifications. |
| 6287 | if (DestBitSize < SrcBitSize && |
| 6288 | isa<ConstantInt>(Op1)) { |
| 6289 | unsigned ShiftAmt = cast<ConstantInt>(Op1)->getZExtValue(); |
| 6290 | if (SrcBitSize > ShiftAmt && SrcBitSize-ShiftAmt >= DestBitSize) { |
| 6291 | // Insert the new logical shift right. |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 6292 | return BinaryOperator::createLShr(Op0, Op1); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6293 | } |
| 6294 | } |
| 6295 | break; |
| 6296 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6297 | case Instruction::ICmp: |
| 6298 | // If we are just checking for a icmp eq of a single bit and casting it |
| 6299 | // to an integer, then shift the bit to the appropriate place and then |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6300 | // cast to integer to avoid the comparison. |
| 6301 | if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) { |
| 6302 | uint64_t Op1CV = Op1C->getZExtValue(); |
| 6303 | // cast (X == 0) to int --> X^1 iff X has only the low bit set. |
| 6304 | // cast (X == 0) to int --> (X>>1)^1 iff X has only the 2nd bit set. |
| 6305 | // cast (X == 1) to int --> X iff X has only the low bit set. |
| 6306 | // cast (X == 2) to int --> X>>1 iff X has only the 2nd bit set. |
| 6307 | // cast (X != 0) to int --> X iff X has only the low bit set. |
| 6308 | // cast (X != 0) to int --> X>>1 iff X has only the 2nd bit set. |
| 6309 | // cast (X != 1) to int --> X^1 iff X has only the low bit set. |
| 6310 | // cast (X != 2) to int --> (X>>1)^1 iff X has only the 2nd bit set. |
| 6311 | if (Op1CV == 0 || isPowerOf2_64(Op1CV)) { |
| 6312 | // If Op1C some other power of two, convert: |
| 6313 | uint64_t KnownZero, KnownOne; |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 6314 | uint64_t TypeMask = Op1C->getType()->getBitMask(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6315 | ComputeMaskedBits(Op0, TypeMask, KnownZero, KnownOne); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6316 | |
| 6317 | // This only works for EQ and NE |
| 6318 | ICmpInst::Predicate pred = cast<ICmpInst>(SrcI)->getPredicate(); |
| 6319 | if (pred != ICmpInst::ICMP_NE && pred != ICmpInst::ICMP_EQ) |
| 6320 | break; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6321 | |
| 6322 | if (isPowerOf2_64(KnownZero^TypeMask)) { // Exactly 1 possible 1? |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6323 | bool isNE = pred == ICmpInst::ICMP_NE; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6324 | if (Op1CV && (Op1CV != (KnownZero^TypeMask))) { |
| 6325 | // (X&4) == 2 --> false |
| 6326 | // (X&4) != 2 --> true |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 6327 | Constant *Res = ConstantInt::get(Type::Int1Ty, isNE); |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 6328 | Res = ConstantExpr::getZExt(Res, CI.getType()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6329 | return ReplaceInstUsesWith(CI, Res); |
| 6330 | } |
| 6331 | |
| 6332 | unsigned ShiftAmt = Log2_64(KnownZero^TypeMask); |
| 6333 | Value *In = Op0; |
| 6334 | if (ShiftAmt) { |
| 6335 | // Perform a logical shr by shiftamt. |
| 6336 | // Insert the shift to put the result in the low bit. |
| 6337 | In = InsertNewInstBefore( |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 6338 | BinaryOperator::createLShr(In, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6339 | ConstantInt::get(In->getType(), ShiftAmt), |
| 6340 | In->getName()+".lobit"), CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6341 | } |
| 6342 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6343 | if ((Op1CV != 0) == isNE) { // Toggle the low bit. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6344 | Constant *One = ConstantInt::get(In->getType(), 1); |
| 6345 | In = BinaryOperator::createXor(In, One, "tmp"); |
| 6346 | InsertNewInstBefore(cast<Instruction>(In), CI); |
| 6347 | } |
| 6348 | |
| 6349 | if (CI.getType() == In->getType()) |
| 6350 | return ReplaceInstUsesWith(CI, In); |
| 6351 | else |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 6352 | return CastInst::createIntegerCast(In, CI.getType(), false/*ZExt*/); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6353 | } |
| 6354 | } |
| 6355 | } |
| 6356 | break; |
| 6357 | } |
| 6358 | return 0; |
| 6359 | } |
| 6360 | |
| 6361 | Instruction *InstCombiner::visitTrunc(CastInst &CI) { |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 6362 | if (Instruction *Result = commonIntCastTransforms(CI)) |
| 6363 | return Result; |
| 6364 | |
| 6365 | Value *Src = CI.getOperand(0); |
| 6366 | const Type *Ty = CI.getType(); |
| 6367 | unsigned DestBitWidth = Ty->getPrimitiveSizeInBits(); |
| 6368 | |
| 6369 | if (Instruction *SrcI = dyn_cast<Instruction>(Src)) { |
| 6370 | switch (SrcI->getOpcode()) { |
| 6371 | default: break; |
| 6372 | case Instruction::LShr: |
| 6373 | // We can shrink lshr to something smaller if we know the bits shifted in |
| 6374 | // are already zeros. |
| 6375 | if (ConstantInt *ShAmtV = dyn_cast<ConstantInt>(SrcI->getOperand(1))) { |
| 6376 | unsigned ShAmt = ShAmtV->getZExtValue(); |
| 6377 | |
| 6378 | // Get a mask for the bits shifting in. |
| 6379 | uint64_t Mask = (~0ULL >> (64-ShAmt)) << DestBitWidth; |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 6380 | Value* SrcIOp0 = SrcI->getOperand(0); |
| 6381 | if (SrcI->hasOneUse() && MaskedValueIsZero(SrcIOp0, Mask)) { |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 6382 | if (ShAmt >= DestBitWidth) // All zeros. |
| 6383 | return ReplaceInstUsesWith(CI, Constant::getNullValue(Ty)); |
| 6384 | |
| 6385 | // Okay, we can shrink this. Truncate the input, then return a new |
| 6386 | // shift. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6387 | Value *V1 = InsertCastBefore(Instruction::Trunc, SrcIOp0, Ty, CI); |
| 6388 | Value *V2 = InsertCastBefore(Instruction::Trunc, SrcI->getOperand(1), |
| 6389 | Ty, CI); |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 6390 | return BinaryOperator::createLShr(V1, V2); |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 6391 | } |
Chris Lattner | e13ab2a | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 6392 | } else { // This is a variable shr. |
| 6393 | |
| 6394 | // Turn 'trunc (lshr X, Y) to bool' into '(X & (1 << Y)) != 0'. This is |
| 6395 | // more LLVM instructions, but allows '1 << Y' to be hoisted if |
| 6396 | // loop-invariant and CSE'd. |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 6397 | if (CI.getType() == Type::Int1Ty && SrcI->hasOneUse()) { |
Chris Lattner | e13ab2a | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 6398 | Value *One = ConstantInt::get(SrcI->getType(), 1); |
| 6399 | |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6400 | Value *V = InsertNewInstBefore( |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 6401 | BinaryOperator::createShl(One, SrcI->getOperand(1), |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6402 | "tmp"), CI); |
Chris Lattner | e13ab2a | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 6403 | V = InsertNewInstBefore(BinaryOperator::createAnd(V, |
| 6404 | SrcI->getOperand(0), |
| 6405 | "tmp"), CI); |
| 6406 | Value *Zero = Constant::getNullValue(V->getType()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6407 | return new ICmpInst(ICmpInst::ICMP_NE, V, Zero); |
Chris Lattner | e13ab2a | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 6408 | } |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 6409 | } |
| 6410 | break; |
| 6411 | } |
| 6412 | } |
| 6413 | |
| 6414 | return 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6415 | } |
| 6416 | |
| 6417 | Instruction *InstCombiner::visitZExt(CastInst &CI) { |
| 6418 | // If one of the common conversion will work .. |
| 6419 | if (Instruction *Result = commonIntCastTransforms(CI)) |
| 6420 | return Result; |
| 6421 | |
| 6422 | Value *Src = CI.getOperand(0); |
| 6423 | |
| 6424 | // If this is a cast of a cast |
| 6425 | if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6426 | // If this is a TRUNC followed by a ZEXT then we are dealing with integral |
| 6427 | // types and if the sizes are just right we can convert this into a logical |
| 6428 | // 'and' which will be much cheaper than the pair of casts. |
| 6429 | if (isa<TruncInst>(CSrc)) { |
| 6430 | // Get the sizes of the types involved |
| 6431 | Value *A = CSrc->getOperand(0); |
| 6432 | unsigned SrcSize = A->getType()->getPrimitiveSizeInBits(); |
| 6433 | unsigned MidSize = CSrc->getType()->getPrimitiveSizeInBits(); |
| 6434 | unsigned DstSize = CI.getType()->getPrimitiveSizeInBits(); |
| 6435 | // If we're actually extending zero bits and the trunc is a no-op |
| 6436 | if (MidSize < DstSize && SrcSize == DstSize) { |
| 6437 | // Replace both of the casts with an And of the type mask. |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 6438 | uint64_t AndValue = cast<IntegerType>(CSrc->getType())->getBitMask(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6439 | Constant *AndConst = ConstantInt::get(A->getType(), AndValue); |
| 6440 | Instruction *And = |
| 6441 | BinaryOperator::createAnd(CSrc->getOperand(0), AndConst); |
| 6442 | // Unfortunately, if the type changed, we need to cast it back. |
| 6443 | if (And->getType() != CI.getType()) { |
| 6444 | And->setName(CSrc->getName()+".mask"); |
| 6445 | InsertNewInstBefore(And, CI); |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 6446 | And = CastInst::createIntegerCast(And, CI.getType(), false/*ZExt*/); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6447 | } |
| 6448 | return And; |
| 6449 | } |
| 6450 | } |
| 6451 | } |
| 6452 | |
| 6453 | return 0; |
| 6454 | } |
| 6455 | |
| 6456 | Instruction *InstCombiner::visitSExt(CastInst &CI) { |
| 6457 | return commonIntCastTransforms(CI); |
| 6458 | } |
| 6459 | |
| 6460 | Instruction *InstCombiner::visitFPTrunc(CastInst &CI) { |
| 6461 | return commonCastTransforms(CI); |
| 6462 | } |
| 6463 | |
| 6464 | Instruction *InstCombiner::visitFPExt(CastInst &CI) { |
| 6465 | return commonCastTransforms(CI); |
| 6466 | } |
| 6467 | |
| 6468 | Instruction *InstCombiner::visitFPToUI(CastInst &CI) { |
Reid Spencer | 44c030a | 2006-11-30 23:13:36 +0000 | [diff] [blame] | 6469 | return commonCastTransforms(CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6470 | } |
| 6471 | |
| 6472 | Instruction *InstCombiner::visitFPToSI(CastInst &CI) { |
Reid Spencer | 44c030a | 2006-11-30 23:13:36 +0000 | [diff] [blame] | 6473 | return commonCastTransforms(CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6474 | } |
| 6475 | |
| 6476 | Instruction *InstCombiner::visitUIToFP(CastInst &CI) { |
| 6477 | return commonCastTransforms(CI); |
| 6478 | } |
| 6479 | |
| 6480 | Instruction *InstCombiner::visitSIToFP(CastInst &CI) { |
| 6481 | return commonCastTransforms(CI); |
| 6482 | } |
| 6483 | |
| 6484 | Instruction *InstCombiner::visitPtrToInt(CastInst &CI) { |
Reid Spencer | 44c030a | 2006-11-30 23:13:36 +0000 | [diff] [blame] | 6485 | return commonCastTransforms(CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6486 | } |
| 6487 | |
| 6488 | Instruction *InstCombiner::visitIntToPtr(CastInst &CI) { |
| 6489 | return commonCastTransforms(CI); |
| 6490 | } |
| 6491 | |
| 6492 | Instruction *InstCombiner::visitBitCast(CastInst &CI) { |
| 6493 | |
| 6494 | // If the operands are integer typed then apply the integer transforms, |
| 6495 | // otherwise just apply the common ones. |
| 6496 | Value *Src = CI.getOperand(0); |
| 6497 | const Type *SrcTy = Src->getType(); |
| 6498 | const Type *DestTy = CI.getType(); |
| 6499 | |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 6500 | if (SrcTy->isInteger() && DestTy->isInteger()) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6501 | if (Instruction *Result = commonIntCastTransforms(CI)) |
| 6502 | return Result; |
| 6503 | } else { |
| 6504 | if (Instruction *Result = commonCastTransforms(CI)) |
| 6505 | return Result; |
| 6506 | } |
| 6507 | |
| 6508 | |
| 6509 | // Get rid of casts from one type to the same type. These are useless and can |
| 6510 | // be replaced by the operand. |
| 6511 | if (DestTy == Src->getType()) |
| 6512 | return ReplaceInstUsesWith(CI, Src); |
| 6513 | |
Chris Lattner | 9fb9213 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 6514 | // If the source and destination are pointers, and this cast is equivalent to |
| 6515 | // a getelementptr X, 0, 0, 0... turn it into the appropriate getelementptr. |
| 6516 | // This can enhance SROA and other transforms that want type-safe pointers. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6517 | if (const PointerType *DstPTy = dyn_cast<PointerType>(DestTy)) { |
| 6518 | if (const PointerType *SrcPTy = dyn_cast<PointerType>(SrcTy)) { |
| 6519 | const Type *DstElTy = DstPTy->getElementType(); |
| 6520 | const Type *SrcElTy = SrcPTy->getElementType(); |
Chris Lattner | 9fb9213 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 6521 | |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6522 | Constant *ZeroUInt = Constant::getNullValue(Type::Int32Ty); |
Chris Lattner | 9fb9213 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 6523 | unsigned NumZeros = 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6524 | while (SrcElTy != DstElTy && |
| 6525 | isa<CompositeType>(SrcElTy) && !isa<PointerType>(SrcElTy) && |
| 6526 | SrcElTy->getNumContainedTypes() /* not "{}" */) { |
| 6527 | SrcElTy = cast<CompositeType>(SrcElTy)->getTypeAtIndex(ZeroUInt); |
Chris Lattner | 9fb9213 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 6528 | ++NumZeros; |
| 6529 | } |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 6530 | |
Chris Lattner | 9fb9213 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 6531 | // If we found a path from the src to dest, create the getelementptr now. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6532 | if (SrcElTy == DstElTy) { |
Chris Lattner | fbbe92f | 2007-01-31 20:08:52 +0000 | [diff] [blame] | 6533 | SmallVector<Value*, 8> Idxs(NumZeros+1, ZeroUInt); |
| 6534 | return new GetElementPtrInst(Src, &Idxs[0], Idxs.size()); |
Chris Lattner | 9fb9213 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 6535 | } |
| 6536 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6537 | } |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 6538 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6539 | if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(Src)) { |
| 6540 | if (SVI->hasOneUse()) { |
| 6541 | // Okay, we have (bitconvert (shuffle ..)). Check to see if this is |
| 6542 | // a bitconvert to a vector with the same # elts. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 6543 | if (isa<VectorType>(DestTy) && |
| 6544 | cast<VectorType>(DestTy)->getNumElements() == |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6545 | SVI->getType()->getNumElements()) { |
| 6546 | CastInst *Tmp; |
| 6547 | // If either of the operands is a cast from CI.getType(), then |
| 6548 | // evaluating the shuffle in the casted destination's type will allow |
| 6549 | // us to eliminate at least one cast. |
| 6550 | if (((Tmp = dyn_cast<CastInst>(SVI->getOperand(0))) && |
| 6551 | Tmp->getOperand(0)->getType() == DestTy) || |
| 6552 | ((Tmp = dyn_cast<CastInst>(SVI->getOperand(1))) && |
| 6553 | Tmp->getOperand(0)->getType() == DestTy)) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 6554 | Value *LHS = InsertOperandCastBefore(Instruction::BitCast, |
| 6555 | SVI->getOperand(0), DestTy, &CI); |
| 6556 | Value *RHS = InsertOperandCastBefore(Instruction::BitCast, |
| 6557 | SVI->getOperand(1), DestTy, &CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6558 | // Return a new shuffle vector. Use the same element ID's, as we |
| 6559 | // know the vector types match #elts. |
| 6560 | return new ShuffleVectorInst(LHS, RHS, SVI->getOperand(2)); |
Chris Lattner | 01575b7 | 2006-05-25 23:24:33 +0000 | [diff] [blame] | 6561 | } |
| 6562 | } |
| 6563 | } |
| 6564 | } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 6565 | return 0; |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 6566 | } |
| 6567 | |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6568 | /// GetSelectFoldableOperands - We want to turn code that looks like this: |
| 6569 | /// %C = or %A, %B |
| 6570 | /// %D = select %cond, %C, %A |
| 6571 | /// into: |
| 6572 | /// %C = select %cond, %B, 0 |
| 6573 | /// %D = or %A, %C |
| 6574 | /// |
| 6575 | /// Assuming that the specified instruction is an operand to the select, return |
| 6576 | /// a bitmask indicating which operands of this instruction are foldable if they |
| 6577 | /// equal the other incoming value of the select. |
| 6578 | /// |
| 6579 | static unsigned GetSelectFoldableOperands(Instruction *I) { |
| 6580 | switch (I->getOpcode()) { |
| 6581 | case Instruction::Add: |
| 6582 | case Instruction::Mul: |
| 6583 | case Instruction::And: |
| 6584 | case Instruction::Or: |
| 6585 | case Instruction::Xor: |
| 6586 | return 3; // Can fold through either operand. |
| 6587 | case Instruction::Sub: // Can only fold on the amount subtracted. |
| 6588 | case Instruction::Shl: // Can only fold on the shift amount. |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 6589 | case Instruction::LShr: |
| 6590 | case Instruction::AShr: |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6591 | return 1; |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6592 | default: |
| 6593 | return 0; // Cannot fold |
| 6594 | } |
| 6595 | } |
| 6596 | |
| 6597 | /// GetSelectFoldableConstant - For the same transformation as the previous |
| 6598 | /// function, return the identity constant that goes into the select. |
| 6599 | static Constant *GetSelectFoldableConstant(Instruction *I) { |
| 6600 | switch (I->getOpcode()) { |
| 6601 | default: assert(0 && "This cannot happen!"); abort(); |
| 6602 | case Instruction::Add: |
| 6603 | case Instruction::Sub: |
| 6604 | case Instruction::Or: |
| 6605 | case Instruction::Xor: |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6606 | case Instruction::Shl: |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 6607 | case Instruction::LShr: |
| 6608 | case Instruction::AShr: |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6609 | return Constant::getNullValue(I->getType()); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6610 | case Instruction::And: |
| 6611 | return ConstantInt::getAllOnesValue(I->getType()); |
| 6612 | case Instruction::Mul: |
| 6613 | return ConstantInt::get(I->getType(), 1); |
| 6614 | } |
| 6615 | } |
| 6616 | |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 6617 | /// FoldSelectOpOp - Here we have (select c, TI, FI), and we know that TI and FI |
| 6618 | /// have the same opcode and only one use each. Try to simplify this. |
| 6619 | Instruction *InstCombiner::FoldSelectOpOp(SelectInst &SI, Instruction *TI, |
| 6620 | Instruction *FI) { |
| 6621 | if (TI->getNumOperands() == 1) { |
| 6622 | // If this is a non-volatile load or a cast from the same type, |
| 6623 | // merge. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6624 | if (TI->isCast()) { |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 6625 | if (TI->getOperand(0)->getType() != FI->getOperand(0)->getType()) |
| 6626 | return 0; |
| 6627 | } else { |
| 6628 | return 0; // unknown unary op. |
| 6629 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6630 | |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 6631 | // Fold this by inserting a select from the input values. |
| 6632 | SelectInst *NewSI = new SelectInst(SI.getCondition(), TI->getOperand(0), |
| 6633 | FI->getOperand(0), SI.getName()+".v"); |
| 6634 | InsertNewInstBefore(NewSI, SI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6635 | return CastInst::create(Instruction::CastOps(TI->getOpcode()), NewSI, |
| 6636 | TI->getType()); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 6637 | } |
| 6638 | |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6639 | // Only handle binary operators here. |
| 6640 | if (!isa<BinaryOperator>(TI)) |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 6641 | return 0; |
| 6642 | |
| 6643 | // Figure out if the operations have any operands in common. |
| 6644 | Value *MatchOp, *OtherOpT, *OtherOpF; |
| 6645 | bool MatchIsOpZero; |
| 6646 | if (TI->getOperand(0) == FI->getOperand(0)) { |
| 6647 | MatchOp = TI->getOperand(0); |
| 6648 | OtherOpT = TI->getOperand(1); |
| 6649 | OtherOpF = FI->getOperand(1); |
| 6650 | MatchIsOpZero = true; |
| 6651 | } else if (TI->getOperand(1) == FI->getOperand(1)) { |
| 6652 | MatchOp = TI->getOperand(1); |
| 6653 | OtherOpT = TI->getOperand(0); |
| 6654 | OtherOpF = FI->getOperand(0); |
| 6655 | MatchIsOpZero = false; |
| 6656 | } else if (!TI->isCommutative()) { |
| 6657 | return 0; |
| 6658 | } else if (TI->getOperand(0) == FI->getOperand(1)) { |
| 6659 | MatchOp = TI->getOperand(0); |
| 6660 | OtherOpT = TI->getOperand(1); |
| 6661 | OtherOpF = FI->getOperand(0); |
| 6662 | MatchIsOpZero = true; |
| 6663 | } else if (TI->getOperand(1) == FI->getOperand(0)) { |
| 6664 | MatchOp = TI->getOperand(1); |
| 6665 | OtherOpT = TI->getOperand(0); |
| 6666 | OtherOpF = FI->getOperand(1); |
| 6667 | MatchIsOpZero = true; |
| 6668 | } else { |
| 6669 | return 0; |
| 6670 | } |
| 6671 | |
| 6672 | // If we reach here, they do have operations in common. |
| 6673 | SelectInst *NewSI = new SelectInst(SI.getCondition(), OtherOpT, |
| 6674 | OtherOpF, SI.getName()+".v"); |
| 6675 | InsertNewInstBefore(NewSI, SI); |
| 6676 | |
| 6677 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TI)) { |
| 6678 | if (MatchIsOpZero) |
| 6679 | return BinaryOperator::create(BO->getOpcode(), MatchOp, NewSI); |
| 6680 | else |
| 6681 | return BinaryOperator::create(BO->getOpcode(), NewSI, MatchOp); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 6682 | } |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6683 | assert(0 && "Shouldn't get here"); |
| 6684 | return 0; |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 6685 | } |
| 6686 | |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 6687 | Instruction *InstCombiner::visitSelectInst(SelectInst &SI) { |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 6688 | Value *CondVal = SI.getCondition(); |
| 6689 | Value *TrueVal = SI.getTrueValue(); |
| 6690 | Value *FalseVal = SI.getFalseValue(); |
| 6691 | |
| 6692 | // select true, X, Y -> X |
| 6693 | // select false, X, Y -> Y |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6694 | if (ConstantInt *C = dyn_cast<ConstantInt>(CondVal)) |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 6695 | return ReplaceInstUsesWith(SI, C->getZExtValue() ? TrueVal : FalseVal); |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 6696 | |
| 6697 | // select C, X, X -> X |
| 6698 | if (TrueVal == FalseVal) |
| 6699 | return ReplaceInstUsesWith(SI, TrueVal); |
| 6700 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 6701 | if (isa<UndefValue>(TrueVal)) // select C, undef, X -> X |
| 6702 | return ReplaceInstUsesWith(SI, FalseVal); |
| 6703 | if (isa<UndefValue>(FalseVal)) // select C, X, undef -> X |
| 6704 | return ReplaceInstUsesWith(SI, TrueVal); |
| 6705 | if (isa<UndefValue>(CondVal)) { // select undef, X, Y -> X or Y |
| 6706 | if (isa<Constant>(TrueVal)) |
| 6707 | return ReplaceInstUsesWith(SI, TrueVal); |
| 6708 | else |
| 6709 | return ReplaceInstUsesWith(SI, FalseVal); |
| 6710 | } |
| 6711 | |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 6712 | if (SI.getType() == Type::Int1Ty) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 6713 | if (ConstantInt *C = dyn_cast<ConstantInt>(TrueVal)) { |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 6714 | if (C->getZExtValue()) { |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 6715 | // Change: A = select B, true, C --> A = or B, C |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 6716 | return BinaryOperator::createOr(CondVal, FalseVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 6717 | } else { |
| 6718 | // Change: A = select B, false, C --> A = and !B, C |
| 6719 | Value *NotCond = |
| 6720 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
| 6721 | "not."+CondVal->getName()), SI); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 6722 | return BinaryOperator::createAnd(NotCond, FalseVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 6723 | } |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 6724 | } else if (ConstantInt *C = dyn_cast<ConstantInt>(FalseVal)) { |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 6725 | if (C->getZExtValue() == false) { |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 6726 | // Change: A = select B, C, false --> A = and B, C |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 6727 | return BinaryOperator::createAnd(CondVal, TrueVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 6728 | } else { |
| 6729 | // Change: A = select B, C, true --> A = or !B, C |
| 6730 | Value *NotCond = |
| 6731 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
| 6732 | "not."+CondVal->getName()), SI); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 6733 | return BinaryOperator::createOr(NotCond, TrueVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 6734 | } |
| 6735 | } |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6736 | } |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 6737 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 6738 | // Selecting between two integer constants? |
| 6739 | if (ConstantInt *TrueValC = dyn_cast<ConstantInt>(TrueVal)) |
| 6740 | if (ConstantInt *FalseValC = dyn_cast<ConstantInt>(FalseVal)) { |
| 6741 | // select C, 1, 0 -> cast C to int |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6742 | if (FalseValC->isNullValue() && TrueValC->getZExtValue() == 1) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6743 | return CastInst::create(Instruction::ZExt, CondVal, SI.getType()); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6744 | } else if (TrueValC->isNullValue() && FalseValC->getZExtValue() == 1) { |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 6745 | // select C, 0, 1 -> cast !C to int |
| 6746 | Value *NotCond = |
| 6747 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
Chris Lattner | 82e14fe | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 6748 | "not."+CondVal->getName()), SI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6749 | return CastInst::create(Instruction::ZExt, NotCond, SI.getType()); |
Chris Lattner | 82e14fe | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 6750 | } |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 6751 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6752 | if (ICmpInst *IC = dyn_cast<ICmpInst>(SI.getCondition())) { |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 6753 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6754 | // (x <s 0) ? -1 : 0 -> ashr x, 31 |
| 6755 | // (x >u 2147483647) ? -1 : 0 -> ashr x, 31 |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 6756 | if (TrueValC->isAllOnesValue() && FalseValC->isNullValue()) |
| 6757 | if (ConstantInt *CmpCst = dyn_cast<ConstantInt>(IC->getOperand(1))) { |
| 6758 | bool CanXForm = false; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6759 | if (IC->isSignedPredicate()) |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 6760 | CanXForm = CmpCst->isNullValue() && |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6761 | IC->getPredicate() == ICmpInst::ICMP_SLT; |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 6762 | else { |
| 6763 | unsigned Bits = CmpCst->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6764 | CanXForm = (CmpCst->getZExtValue() == ~0ULL >> (64-Bits+1)) && |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6765 | IC->getPredicate() == ICmpInst::ICMP_UGT; |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 6766 | } |
| 6767 | |
| 6768 | if (CanXForm) { |
| 6769 | // The comparison constant and the result are not neccessarily the |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6770 | // same width. Make an all-ones value by inserting a AShr. |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 6771 | Value *X = IC->getOperand(0); |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 6772 | unsigned Bits = X->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6773 | Constant *ShAmt = ConstantInt::get(X->getType(), Bits-1); |
| 6774 | Instruction *SRA = BinaryOperator::create(Instruction::AShr, X, |
| 6775 | ShAmt, "ones"); |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 6776 | InsertNewInstBefore(SRA, SI); |
| 6777 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6778 | // Finally, convert to the type of the select RHS. We figure out |
| 6779 | // if this requires a SExt, Trunc or BitCast based on the sizes. |
| 6780 | Instruction::CastOps opc = Instruction::BitCast; |
| 6781 | unsigned SRASize = SRA->getType()->getPrimitiveSizeInBits(); |
| 6782 | unsigned SISize = SI.getType()->getPrimitiveSizeInBits(); |
| 6783 | if (SRASize < SISize) |
| 6784 | opc = Instruction::SExt; |
| 6785 | else if (SRASize > SISize) |
| 6786 | opc = Instruction::Trunc; |
| 6787 | return CastInst::create(opc, SRA, SI.getType()); |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 6788 | } |
| 6789 | } |
| 6790 | |
| 6791 | |
| 6792 | // If one of the constants is zero (we know they can't both be) and we |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6793 | // have a fcmp instruction with zero, and we have an 'and' with the |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 6794 | // non-constant value, eliminate this whole mess. This corresponds to |
| 6795 | // cases like this: ((X & 27) ? 27 : 0) |
| 6796 | if (TrueValC->isNullValue() || FalseValC->isNullValue()) |
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 6797 | if (IC->isEquality() && isa<ConstantInt>(IC->getOperand(1)) && |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 6798 | cast<Constant>(IC->getOperand(1))->isNullValue()) |
| 6799 | if (Instruction *ICA = dyn_cast<Instruction>(IC->getOperand(0))) |
| 6800 | if (ICA->getOpcode() == Instruction::And && |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6801 | isa<ConstantInt>(ICA->getOperand(1)) && |
| 6802 | (ICA->getOperand(1) == TrueValC || |
| 6803 | ICA->getOperand(1) == FalseValC) && |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 6804 | isOneBitSet(cast<ConstantInt>(ICA->getOperand(1)))) { |
| 6805 | // 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] | 6806 | // know whether we have a icmp_ne or icmp_eq and whether the |
| 6807 | // true or false val is the zero. |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 6808 | bool ShouldNotVal = !TrueValC->isNullValue(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6809 | ShouldNotVal ^= IC->getPredicate() == ICmpInst::ICMP_NE; |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 6810 | Value *V = ICA; |
| 6811 | if (ShouldNotVal) |
| 6812 | V = InsertNewInstBefore(BinaryOperator::create( |
| 6813 | Instruction::Xor, V, ICA->getOperand(1)), SI); |
| 6814 | return ReplaceInstUsesWith(SI, V); |
| 6815 | } |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 6816 | } |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 6817 | } |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 6818 | |
| 6819 | // 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] | 6820 | if (FCmpInst *FCI = dyn_cast<FCmpInst>(CondVal)) { |
| 6821 | if (FCI->getOperand(0) == TrueVal && FCI->getOperand(1) == FalseVal) { |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 6822 | // Transform (X == Y) ? X : Y -> Y |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6823 | if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 6824 | return ReplaceInstUsesWith(SI, FalseVal); |
| 6825 | // Transform (X != Y) ? X : Y -> X |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6826 | if (FCI->getPredicate() == FCmpInst::FCMP_ONE) |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 6827 | return ReplaceInstUsesWith(SI, TrueVal); |
| 6828 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 6829 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6830 | } else if (FCI->getOperand(0) == FalseVal && FCI->getOperand(1) == TrueVal){ |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 6831 | // Transform (X == Y) ? Y : X -> X |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6832 | if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) |
Chris Lattner | fbede52 | 2004-04-11 01:39:19 +0000 | [diff] [blame] | 6833 | return ReplaceInstUsesWith(SI, FalseVal); |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 6834 | // Transform (X != Y) ? Y : X -> Y |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6835 | if (FCI->getPredicate() == FCmpInst::FCMP_ONE) |
| 6836 | return ReplaceInstUsesWith(SI, TrueVal); |
| 6837 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 6838 | } |
| 6839 | } |
| 6840 | |
| 6841 | // See if we are selecting two values based on a comparison of the two values. |
| 6842 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(CondVal)) { |
| 6843 | if (ICI->getOperand(0) == TrueVal && ICI->getOperand(1) == FalseVal) { |
| 6844 | // Transform (X == Y) ? X : Y -> Y |
| 6845 | if (ICI->getPredicate() == ICmpInst::ICMP_EQ) |
| 6846 | return ReplaceInstUsesWith(SI, FalseVal); |
| 6847 | // Transform (X != Y) ? X : Y -> X |
| 6848 | if (ICI->getPredicate() == ICmpInst::ICMP_NE) |
| 6849 | return ReplaceInstUsesWith(SI, TrueVal); |
| 6850 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 6851 | |
| 6852 | } else if (ICI->getOperand(0) == FalseVal && ICI->getOperand(1) == TrueVal){ |
| 6853 | // Transform (X == Y) ? Y : X -> X |
| 6854 | if (ICI->getPredicate() == ICmpInst::ICMP_EQ) |
| 6855 | return ReplaceInstUsesWith(SI, FalseVal); |
| 6856 | // Transform (X != Y) ? Y : X -> Y |
| 6857 | if (ICI->getPredicate() == ICmpInst::ICMP_NE) |
Chris Lattner | fbede52 | 2004-04-11 01:39:19 +0000 | [diff] [blame] | 6858 | return ReplaceInstUsesWith(SI, TrueVal); |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 6859 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 6860 | } |
| 6861 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6862 | |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 6863 | if (Instruction *TI = dyn_cast<Instruction>(TrueVal)) |
| 6864 | if (Instruction *FI = dyn_cast<Instruction>(FalseVal)) |
| 6865 | if (TI->hasOneUse() && FI->hasOneUse()) { |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 6866 | Instruction *AddOp = 0, *SubOp = 0; |
| 6867 | |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 6868 | // Turn (select C, (op X, Y), (op X, Z)) -> (op X, (select C, Y, Z)) |
| 6869 | if (TI->getOpcode() == FI->getOpcode()) |
| 6870 | if (Instruction *IV = FoldSelectOpOp(SI, TI, FI)) |
| 6871 | return IV; |
| 6872 | |
| 6873 | // Turn select C, (X+Y), (X-Y) --> (X+(select C, Y, (-Y))). This is |
| 6874 | // even legal for FP. |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 6875 | if (TI->getOpcode() == Instruction::Sub && |
| 6876 | FI->getOpcode() == Instruction::Add) { |
| 6877 | AddOp = FI; SubOp = TI; |
| 6878 | } else if (FI->getOpcode() == Instruction::Sub && |
| 6879 | TI->getOpcode() == Instruction::Add) { |
| 6880 | AddOp = TI; SubOp = FI; |
| 6881 | } |
| 6882 | |
| 6883 | if (AddOp) { |
| 6884 | Value *OtherAddOp = 0; |
| 6885 | if (SubOp->getOperand(0) == AddOp->getOperand(0)) { |
| 6886 | OtherAddOp = AddOp->getOperand(1); |
| 6887 | } else if (SubOp->getOperand(0) == AddOp->getOperand(1)) { |
| 6888 | OtherAddOp = AddOp->getOperand(0); |
| 6889 | } |
| 6890 | |
| 6891 | if (OtherAddOp) { |
Chris Lattner | 97f37a4 | 2006-02-24 18:05:58 +0000 | [diff] [blame] | 6892 | // So at this point we know we have (Y -> OtherAddOp): |
| 6893 | // select C, (add X, Y), (sub X, Z) |
| 6894 | Value *NegVal; // Compute -Z |
| 6895 | if (Constant *C = dyn_cast<Constant>(SubOp->getOperand(1))) { |
| 6896 | NegVal = ConstantExpr::getNeg(C); |
| 6897 | } else { |
| 6898 | NegVal = InsertNewInstBefore( |
| 6899 | BinaryOperator::createNeg(SubOp->getOperand(1), "tmp"), SI); |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 6900 | } |
Chris Lattner | 97f37a4 | 2006-02-24 18:05:58 +0000 | [diff] [blame] | 6901 | |
| 6902 | Value *NewTrueOp = OtherAddOp; |
| 6903 | Value *NewFalseOp = NegVal; |
| 6904 | if (AddOp != TI) |
| 6905 | std::swap(NewTrueOp, NewFalseOp); |
| 6906 | Instruction *NewSel = |
| 6907 | new SelectInst(CondVal, NewTrueOp,NewFalseOp,SI.getName()+".p"); |
| 6908 | |
| 6909 | NewSel = InsertNewInstBefore(NewSel, SI); |
| 6910 | return BinaryOperator::createAdd(SubOp->getOperand(0), NewSel); |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 6911 | } |
| 6912 | } |
| 6913 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6914 | |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6915 | // See if we can fold the select into one of our operands. |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 6916 | if (SI.getType()->isInteger()) { |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6917 | // See the comment above GetSelectFoldableOperands for a description of the |
| 6918 | // transformation we are doing here. |
| 6919 | if (Instruction *TVI = dyn_cast<Instruction>(TrueVal)) |
| 6920 | if (TVI->hasOneUse() && TVI->getNumOperands() == 2 && |
| 6921 | !isa<Constant>(FalseVal)) |
| 6922 | if (unsigned SFO = GetSelectFoldableOperands(TVI)) { |
| 6923 | unsigned OpToFold = 0; |
| 6924 | if ((SFO & 1) && FalseVal == TVI->getOperand(0)) { |
| 6925 | OpToFold = 1; |
| 6926 | } else if ((SFO & 2) && FalseVal == TVI->getOperand(1)) { |
| 6927 | OpToFold = 2; |
| 6928 | } |
| 6929 | |
| 6930 | if (OpToFold) { |
| 6931 | Constant *C = GetSelectFoldableConstant(TVI); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6932 | Instruction *NewSel = |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6933 | new SelectInst(SI.getCondition(), TVI->getOperand(2-OpToFold), C); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6934 | InsertNewInstBefore(NewSel, SI); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6935 | NewSel->takeName(TVI); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6936 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TVI)) |
| 6937 | return BinaryOperator::create(BO->getOpcode(), FalseVal, NewSel); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6938 | else { |
| 6939 | assert(0 && "Unknown instruction!!"); |
| 6940 | } |
| 6941 | } |
| 6942 | } |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 6943 | |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6944 | if (Instruction *FVI = dyn_cast<Instruction>(FalseVal)) |
| 6945 | if (FVI->hasOneUse() && FVI->getNumOperands() == 2 && |
| 6946 | !isa<Constant>(TrueVal)) |
| 6947 | if (unsigned SFO = GetSelectFoldableOperands(FVI)) { |
| 6948 | unsigned OpToFold = 0; |
| 6949 | if ((SFO & 1) && TrueVal == FVI->getOperand(0)) { |
| 6950 | OpToFold = 1; |
| 6951 | } else if ((SFO & 2) && TrueVal == FVI->getOperand(1)) { |
| 6952 | OpToFold = 2; |
| 6953 | } |
| 6954 | |
| 6955 | if (OpToFold) { |
| 6956 | Constant *C = GetSelectFoldableConstant(FVI); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6957 | Instruction *NewSel = |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6958 | new SelectInst(SI.getCondition(), C, FVI->getOperand(2-OpToFold)); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6959 | InsertNewInstBefore(NewSel, SI); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6960 | NewSel->takeName(FVI); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6961 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(FVI)) |
| 6962 | return BinaryOperator::create(BO->getOpcode(), TrueVal, NewSel); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6963 | else |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6964 | assert(0 && "Unknown instruction!!"); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6965 | } |
| 6966 | } |
| 6967 | } |
Chris Lattner | a1df33c | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 6968 | |
| 6969 | if (BinaryOperator::isNot(CondVal)) { |
| 6970 | SI.setOperand(0, BinaryOperator::getNotArgument(CondVal)); |
| 6971 | SI.setOperand(1, FalseVal); |
| 6972 | SI.setOperand(2, TrueVal); |
| 6973 | return &SI; |
| 6974 | } |
| 6975 | |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 6976 | return 0; |
| 6977 | } |
| 6978 | |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 6979 | /// GetKnownAlignment - If the specified pointer has an alignment that we can |
| 6980 | /// determine, return it, otherwise return 0. |
| 6981 | static unsigned GetKnownAlignment(Value *V, TargetData *TD) { |
| 6982 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) { |
| 6983 | unsigned Align = GV->getAlignment(); |
| 6984 | if (Align == 0 && TD) |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 6985 | Align = TD->getPrefTypeAlignment(GV->getType()->getElementType()); |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 6986 | return Align; |
| 6987 | } else if (AllocationInst *AI = dyn_cast<AllocationInst>(V)) { |
| 6988 | unsigned Align = AI->getAlignment(); |
| 6989 | if (Align == 0 && TD) { |
| 6990 | if (isa<AllocaInst>(AI)) |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 6991 | Align = TD->getPrefTypeAlignment(AI->getType()->getElementType()); |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 6992 | else if (isa<MallocInst>(AI)) { |
| 6993 | // Malloc returns maximally aligned memory. |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 6994 | Align = TD->getABITypeAlignment(AI->getType()->getElementType()); |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 6995 | Align = |
| 6996 | std::max(Align, |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 6997 | (unsigned)TD->getABITypeAlignment(Type::DoubleTy)); |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 6998 | Align = |
| 6999 | std::max(Align, |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 7000 | (unsigned)TD->getABITypeAlignment(Type::Int64Ty)); |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7001 | } |
| 7002 | } |
| 7003 | return Align; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7004 | } else if (isa<BitCastInst>(V) || |
Chris Lattner | 51c26e9 | 2006-03-07 01:28:57 +0000 | [diff] [blame] | 7005 | (isa<ConstantExpr>(V) && |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7006 | cast<ConstantExpr>(V)->getOpcode() == Instruction::BitCast)) { |
Chris Lattner | 51c26e9 | 2006-03-07 01:28:57 +0000 | [diff] [blame] | 7007 | User *CI = cast<User>(V); |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7008 | if (isa<PointerType>(CI->getOperand(0)->getType())) |
| 7009 | return GetKnownAlignment(CI->getOperand(0), TD); |
| 7010 | return 0; |
Chris Lattner | 51c26e9 | 2006-03-07 01:28:57 +0000 | [diff] [blame] | 7011 | } else if (isa<GetElementPtrInst>(V) || |
| 7012 | (isa<ConstantExpr>(V) && |
| 7013 | cast<ConstantExpr>(V)->getOpcode()==Instruction::GetElementPtr)) { |
| 7014 | User *GEPI = cast<User>(V); |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7015 | unsigned BaseAlignment = GetKnownAlignment(GEPI->getOperand(0), TD); |
| 7016 | if (BaseAlignment == 0) return 0; |
| 7017 | |
| 7018 | // If all indexes are zero, it is just the alignment of the base pointer. |
| 7019 | bool AllZeroOperands = true; |
| 7020 | for (unsigned i = 1, e = GEPI->getNumOperands(); i != e; ++i) |
| 7021 | if (!isa<Constant>(GEPI->getOperand(i)) || |
| 7022 | !cast<Constant>(GEPI->getOperand(i))->isNullValue()) { |
| 7023 | AllZeroOperands = false; |
| 7024 | break; |
| 7025 | } |
| 7026 | if (AllZeroOperands) |
| 7027 | return BaseAlignment; |
| 7028 | |
| 7029 | // Otherwise, if the base alignment is >= the alignment we expect for the |
| 7030 | // base pointer type, then we know that the resultant pointer is aligned at |
| 7031 | // least as much as its type requires. |
| 7032 | if (!TD) return 0; |
| 7033 | |
| 7034 | const Type *BasePtrTy = GEPI->getOperand(0)->getType(); |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 7035 | const PointerType *PtrTy = cast<PointerType>(BasePtrTy); |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 7036 | if (TD->getABITypeAlignment(PtrTy->getElementType()) |
Chris Lattner | 51c26e9 | 2006-03-07 01:28:57 +0000 | [diff] [blame] | 7037 | <= BaseAlignment) { |
| 7038 | const Type *GEPTy = GEPI->getType(); |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 7039 | const PointerType *GEPPtrTy = cast<PointerType>(GEPTy); |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 7040 | return TD->getABITypeAlignment(GEPPtrTy->getElementType()); |
Chris Lattner | 51c26e9 | 2006-03-07 01:28:57 +0000 | [diff] [blame] | 7041 | } |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7042 | return 0; |
| 7043 | } |
| 7044 | return 0; |
| 7045 | } |
| 7046 | |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 7047 | |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 7048 | /// visitCallInst - CallInst simplification. This mostly only handles folding |
| 7049 | /// of intrinsic instructions. For normal calls, it allows visitCallSite to do |
| 7050 | /// the heavy lifting. |
| 7051 | /// |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7052 | Instruction *InstCombiner::visitCallInst(CallInst &CI) { |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 7053 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(&CI); |
| 7054 | if (!II) return visitCallSite(&CI); |
| 7055 | |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 7056 | // Intrinsics cannot occur in an invoke, so handle them here instead of in |
| 7057 | // visitCallSite. |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 7058 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(II)) { |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 7059 | bool Changed = false; |
| 7060 | |
| 7061 | // memmove/cpy/set of zero bytes is a noop. |
| 7062 | if (Constant *NumBytes = dyn_cast<Constant>(MI->getLength())) { |
| 7063 | if (NumBytes->isNullValue()) return EraseInstFromFunction(CI); |
| 7064 | |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 7065 | if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes)) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7066 | if (CI->getZExtValue() == 1) { |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 7067 | // Replace the instruction with just byte operations. We would |
| 7068 | // transform other cases to loads/stores, but we don't know if |
| 7069 | // alignment is sufficient. |
| 7070 | } |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 7071 | } |
| 7072 | |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 7073 | // If we have a memmove and the source operation is a constant global, |
| 7074 | // then the source and dest pointers can't alias, so we can change this |
| 7075 | // into a call to memcpy. |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7076 | if (MemMoveInst *MMI = dyn_cast<MemMoveInst>(II)) { |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 7077 | if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource())) |
| 7078 | if (GVSrc->isConstant()) { |
| 7079 | Module *M = CI.getParent()->getParent()->getParent(); |
Chris Lattner | 2195939 | 2006-03-03 01:34:17 +0000 | [diff] [blame] | 7080 | const char *Name; |
Andrew Lenharth | 8ed4c47 | 2006-11-03 22:45:50 +0000 | [diff] [blame] | 7081 | if (CI.getCalledFunction()->getFunctionType()->getParamType(2) == |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 7082 | Type::Int32Ty) |
Chris Lattner | 2195939 | 2006-03-03 01:34:17 +0000 | [diff] [blame] | 7083 | Name = "llvm.memcpy.i32"; |
| 7084 | else |
| 7085 | Name = "llvm.memcpy.i64"; |
Chris Lattner | 9214196 | 2007-01-07 06:58:05 +0000 | [diff] [blame] | 7086 | Constant *MemCpy = M->getOrInsertFunction(Name, |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 7087 | CI.getCalledFunction()->getFunctionType()); |
| 7088 | CI.setOperand(0, MemCpy); |
| 7089 | Changed = true; |
| 7090 | } |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7091 | } |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 7092 | |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7093 | // If we can determine a pointer alignment that is bigger than currently |
| 7094 | // set, update the alignment. |
| 7095 | if (isa<MemCpyInst>(MI) || isa<MemMoveInst>(MI)) { |
| 7096 | unsigned Alignment1 = GetKnownAlignment(MI->getOperand(1), TD); |
| 7097 | unsigned Alignment2 = GetKnownAlignment(MI->getOperand(2), TD); |
| 7098 | unsigned Align = std::min(Alignment1, Alignment2); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7099 | if (MI->getAlignment()->getZExtValue() < Align) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 7100 | MI->setAlignment(ConstantInt::get(Type::Int32Ty, Align)); |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7101 | Changed = true; |
| 7102 | } |
| 7103 | } else if (isa<MemSetInst>(MI)) { |
| 7104 | unsigned Alignment = GetKnownAlignment(MI->getDest(), TD); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7105 | if (MI->getAlignment()->getZExtValue() < Alignment) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 7106 | MI->setAlignment(ConstantInt::get(Type::Int32Ty, Alignment)); |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7107 | Changed = true; |
| 7108 | } |
| 7109 | } |
| 7110 | |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 7111 | if (Changed) return II; |
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 7112 | } else { |
| 7113 | switch (II->getIntrinsicID()) { |
| 7114 | default: break; |
Chris Lattner | 82ed58f | 2006-04-02 05:30:25 +0000 | [diff] [blame] | 7115 | case Intrinsic::ppc_altivec_lvx: |
| 7116 | case Intrinsic::ppc_altivec_lvxl: |
Chris Lattner | fd6bdf0 | 2006-04-17 22:26:56 +0000 | [diff] [blame] | 7117 | case Intrinsic::x86_sse_loadu_ps: |
| 7118 | case Intrinsic::x86_sse2_loadu_pd: |
| 7119 | case Intrinsic::x86_sse2_loadu_dq: |
| 7120 | // Turn PPC lvx -> load if the pointer is known aligned. |
| 7121 | // Turn X86 loadups -> load if the pointer is known aligned. |
Chris Lattner | 82ed58f | 2006-04-02 05:30:25 +0000 | [diff] [blame] | 7122 | if (GetKnownAlignment(II->getOperand(1), TD) >= 16) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7123 | Value *Ptr = InsertCastBefore(Instruction::BitCast, II->getOperand(1), |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 7124 | PointerType::get(II->getType()), CI); |
Chris Lattner | 82ed58f | 2006-04-02 05:30:25 +0000 | [diff] [blame] | 7125 | return new LoadInst(Ptr); |
| 7126 | } |
| 7127 | break; |
| 7128 | case Intrinsic::ppc_altivec_stvx: |
| 7129 | case Intrinsic::ppc_altivec_stvxl: |
| 7130 | // Turn stvx -> store if the pointer is known aligned. |
| 7131 | if (GetKnownAlignment(II->getOperand(2), TD) >= 16) { |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 7132 | const Type *OpPtrTy = PointerType::get(II->getOperand(1)->getType()); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7133 | Value *Ptr = InsertCastBefore(Instruction::BitCast, II->getOperand(2), |
| 7134 | OpPtrTy, CI); |
Chris Lattner | 82ed58f | 2006-04-02 05:30:25 +0000 | [diff] [blame] | 7135 | return new StoreInst(II->getOperand(1), Ptr); |
| 7136 | } |
| 7137 | break; |
Chris Lattner | fd6bdf0 | 2006-04-17 22:26:56 +0000 | [diff] [blame] | 7138 | case Intrinsic::x86_sse_storeu_ps: |
| 7139 | case Intrinsic::x86_sse2_storeu_pd: |
| 7140 | case Intrinsic::x86_sse2_storeu_dq: |
| 7141 | case Intrinsic::x86_sse2_storel_dq: |
| 7142 | // Turn X86 storeu -> store if the pointer is known aligned. |
| 7143 | if (GetKnownAlignment(II->getOperand(1), TD) >= 16) { |
| 7144 | const Type *OpPtrTy = PointerType::get(II->getOperand(2)->getType()); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7145 | Value *Ptr = InsertCastBefore(Instruction::BitCast, II->getOperand(1), |
| 7146 | OpPtrTy, CI); |
Chris Lattner | fd6bdf0 | 2006-04-17 22:26:56 +0000 | [diff] [blame] | 7147 | return new StoreInst(II->getOperand(2), Ptr); |
| 7148 | } |
| 7149 | break; |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 7150 | |
| 7151 | case Intrinsic::x86_sse_cvttss2si: { |
| 7152 | // These intrinsics only demands the 0th element of its input vector. If |
| 7153 | // we can simplify the input based on that, do so now. |
| 7154 | uint64_t UndefElts; |
| 7155 | if (Value *V = SimplifyDemandedVectorElts(II->getOperand(1), 1, |
| 7156 | UndefElts)) { |
| 7157 | II->setOperand(1, V); |
| 7158 | return II; |
| 7159 | } |
| 7160 | break; |
| 7161 | } |
| 7162 | |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 7163 | case Intrinsic::ppc_altivec_vperm: |
| 7164 | // 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] | 7165 | if (ConstantVector *Mask = dyn_cast<ConstantVector>(II->getOperand(3))) { |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 7166 | assert(Mask->getNumOperands() == 16 && "Bad type for intrinsic!"); |
| 7167 | |
| 7168 | // Check that all of the elements are integer constants or undefs. |
| 7169 | bool AllEltsOk = true; |
| 7170 | for (unsigned i = 0; i != 16; ++i) { |
| 7171 | if (!isa<ConstantInt>(Mask->getOperand(i)) && |
| 7172 | !isa<UndefValue>(Mask->getOperand(i))) { |
| 7173 | AllEltsOk = false; |
| 7174 | break; |
| 7175 | } |
| 7176 | } |
| 7177 | |
| 7178 | if (AllEltsOk) { |
| 7179 | // Cast the input vectors to byte vectors. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7180 | Value *Op0 = InsertCastBefore(Instruction::BitCast, |
| 7181 | II->getOperand(1), Mask->getType(), CI); |
| 7182 | Value *Op1 = InsertCastBefore(Instruction::BitCast, |
| 7183 | II->getOperand(2), Mask->getType(), CI); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 7184 | Value *Result = UndefValue::get(Op0->getType()); |
| 7185 | |
| 7186 | // Only extract each element once. |
| 7187 | Value *ExtractedElts[32]; |
| 7188 | memset(ExtractedElts, 0, sizeof(ExtractedElts)); |
| 7189 | |
| 7190 | for (unsigned i = 0; i != 16; ++i) { |
| 7191 | if (isa<UndefValue>(Mask->getOperand(i))) |
| 7192 | continue; |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7193 | unsigned Idx =cast<ConstantInt>(Mask->getOperand(i))->getZExtValue(); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 7194 | Idx &= 31; // Match the hardware behavior. |
| 7195 | |
| 7196 | if (ExtractedElts[Idx] == 0) { |
| 7197 | Instruction *Elt = |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 7198 | new ExtractElementInst(Idx < 16 ? Op0 : Op1, Idx&15, "tmp"); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 7199 | InsertNewInstBefore(Elt, CI); |
| 7200 | ExtractedElts[Idx] = Elt; |
| 7201 | } |
| 7202 | |
| 7203 | // Insert this value into the result vector. |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 7204 | Result = new InsertElementInst(Result, ExtractedElts[Idx], i,"tmp"); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 7205 | InsertNewInstBefore(cast<Instruction>(Result), CI); |
| 7206 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7207 | return CastInst::create(Instruction::BitCast, Result, CI.getType()); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 7208 | } |
| 7209 | } |
| 7210 | break; |
| 7211 | |
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 7212 | case Intrinsic::stackrestore: { |
| 7213 | // If the save is right next to the restore, remove the restore. This can |
| 7214 | // happen when variable allocas are DCE'd. |
| 7215 | if (IntrinsicInst *SS = dyn_cast<IntrinsicInst>(II->getOperand(1))) { |
| 7216 | if (SS->getIntrinsicID() == Intrinsic::stacksave) { |
| 7217 | BasicBlock::iterator BI = SS; |
| 7218 | if (&*++BI == II) |
| 7219 | return EraseInstFromFunction(CI); |
| 7220 | } |
| 7221 | } |
| 7222 | |
| 7223 | // If the stack restore is in a return/unwind block and if there are no |
| 7224 | // allocas or calls between the restore and the return, nuke the restore. |
| 7225 | TerminatorInst *TI = II->getParent()->getTerminator(); |
| 7226 | if (isa<ReturnInst>(TI) || isa<UnwindInst>(TI)) { |
| 7227 | BasicBlock::iterator BI = II; |
| 7228 | bool CannotRemove = false; |
| 7229 | for (++BI; &*BI != TI; ++BI) { |
| 7230 | if (isa<AllocaInst>(BI) || |
| 7231 | (isa<CallInst>(BI) && !isa<IntrinsicInst>(BI))) { |
| 7232 | CannotRemove = true; |
| 7233 | break; |
| 7234 | } |
| 7235 | } |
| 7236 | if (!CannotRemove) |
| 7237 | return EraseInstFromFunction(CI); |
| 7238 | } |
| 7239 | break; |
| 7240 | } |
| 7241 | } |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 7242 | } |
| 7243 | |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 7244 | return visitCallSite(II); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7245 | } |
| 7246 | |
| 7247 | // InvokeInst simplification |
| 7248 | // |
| 7249 | Instruction *InstCombiner::visitInvokeInst(InvokeInst &II) { |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 7250 | return visitCallSite(&II); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7251 | } |
| 7252 | |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 7253 | // visitCallSite - Improvements for call and invoke instructions. |
| 7254 | // |
| 7255 | Instruction *InstCombiner::visitCallSite(CallSite CS) { |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 7256 | bool Changed = false; |
| 7257 | |
| 7258 | // If the callee is a constexpr cast of a function, attempt to move the cast |
| 7259 | // to the arguments of the call/invoke. |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 7260 | if (transformConstExprCastCall(CS)) return 0; |
| 7261 | |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 7262 | Value *Callee = CS.getCalledValue(); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 7263 | |
Chris Lattner | 08b22ec | 2005-05-13 07:09:09 +0000 | [diff] [blame] | 7264 | if (Function *CalleeF = dyn_cast<Function>(Callee)) |
| 7265 | if (CalleeF->getCallingConv() != CS.getCallingConv()) { |
| 7266 | Instruction *OldCall = CS.getInstruction(); |
| 7267 | // If the call and callee calling conventions don't match, this call must |
| 7268 | // be unreachable, as the call is undefined. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 7269 | new StoreInst(ConstantInt::getTrue(), |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 7270 | UndefValue::get(PointerType::get(Type::Int1Ty)), OldCall); |
Chris Lattner | 08b22ec | 2005-05-13 07:09:09 +0000 | [diff] [blame] | 7271 | if (!OldCall->use_empty()) |
| 7272 | OldCall->replaceAllUsesWith(UndefValue::get(OldCall->getType())); |
| 7273 | if (isa<CallInst>(OldCall)) // Not worth removing an invoke here. |
| 7274 | return EraseInstFromFunction(*OldCall); |
| 7275 | return 0; |
| 7276 | } |
| 7277 | |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 7278 | if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) { |
| 7279 | // This instruction is not reachable, just remove it. We insert a store to |
| 7280 | // undef so that we know that this code is not reachable, despite the fact |
| 7281 | // that we can't modify the CFG here. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 7282 | new StoreInst(ConstantInt::getTrue(), |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 7283 | UndefValue::get(PointerType::get(Type::Int1Ty)), |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 7284 | CS.getInstruction()); |
| 7285 | |
| 7286 | if (!CS.getInstruction()->use_empty()) |
| 7287 | CS.getInstruction()-> |
| 7288 | replaceAllUsesWith(UndefValue::get(CS.getInstruction()->getType())); |
| 7289 | |
| 7290 | if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) { |
| 7291 | // Don't break the CFG, insert a dummy cond branch. |
| 7292 | new BranchInst(II->getNormalDest(), II->getUnwindDest(), |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 7293 | ConstantInt::getTrue(), II); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 7294 | } |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 7295 | return EraseInstFromFunction(*CS.getInstruction()); |
| 7296 | } |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 7297 | |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 7298 | const PointerType *PTy = cast<PointerType>(Callee->getType()); |
| 7299 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
| 7300 | if (FTy->isVarArg()) { |
| 7301 | // See if we can optimize any arguments passed through the varargs area of |
| 7302 | // the call. |
| 7303 | for (CallSite::arg_iterator I = CS.arg_begin()+FTy->getNumParams(), |
| 7304 | E = CS.arg_end(); I != E; ++I) |
| 7305 | if (CastInst *CI = dyn_cast<CastInst>(*I)) { |
| 7306 | // If this cast does not effect the value passed through the varargs |
| 7307 | // area, we can eliminate the use of the cast. |
| 7308 | Value *Op = CI->getOperand(0); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7309 | if (CI->isLosslessCast()) { |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 7310 | *I = Op; |
| 7311 | Changed = true; |
| 7312 | } |
| 7313 | } |
| 7314 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7315 | |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 7316 | return Changed ? CS.getInstruction() : 0; |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 7317 | } |
| 7318 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7319 | // transformConstExprCastCall - If the callee is a constexpr cast of a function, |
| 7320 | // attempt to move the cast to the arguments of the call/invoke. |
| 7321 | // |
| 7322 | bool InstCombiner::transformConstExprCastCall(CallSite CS) { |
| 7323 | if (!isa<ConstantExpr>(CS.getCalledValue())) return false; |
| 7324 | ConstantExpr *CE = cast<ConstantExpr>(CS.getCalledValue()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7325 | if (CE->getOpcode() != Instruction::BitCast || |
| 7326 | !isa<Function>(CE->getOperand(0))) |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7327 | return false; |
Reid Spencer | 8863f18 | 2004-07-18 00:38:32 +0000 | [diff] [blame] | 7328 | Function *Callee = cast<Function>(CE->getOperand(0)); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7329 | Instruction *Caller = CS.getInstruction(); |
| 7330 | |
| 7331 | // Okay, this is a cast from a function to a different type. Unless doing so |
| 7332 | // would cause a type conversion of one of our arguments, change this call to |
| 7333 | // be a direct call with arguments casted to the appropriate types. |
| 7334 | // |
| 7335 | const FunctionType *FT = Callee->getFunctionType(); |
| 7336 | const Type *OldRetTy = Caller->getType(); |
| 7337 | |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 7338 | // Check to see if we are changing the return type... |
| 7339 | if (OldRetTy != FT->getReturnType()) { |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 7340 | if (Callee->isDeclaration() && !Caller->use_empty() && |
Chris Lattner | 46013f4 | 2007-01-06 19:53:32 +0000 | [diff] [blame] | 7341 | OldRetTy != FT->getReturnType() && |
| 7342 | // Conversion is ok if changing from pointer to int of same size. |
| 7343 | !(isa<PointerType>(FT->getReturnType()) && |
| 7344 | TD->getIntPtrType() == OldRetTy)) |
Chris Lattner | ec47992 | 2007-01-06 02:09:32 +0000 | [diff] [blame] | 7345 | return false; // Cannot transform this return value. |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 7346 | |
| 7347 | // If the callsite is an invoke instruction, and the return value is used by |
| 7348 | // a PHI node in a successor, we cannot change the return type of the call |
| 7349 | // because there is no place to put the cast instruction (without breaking |
| 7350 | // the critical edge). Bail out in this case. |
| 7351 | if (!Caller->use_empty()) |
| 7352 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) |
| 7353 | for (Value::use_iterator UI = II->use_begin(), E = II->use_end(); |
| 7354 | UI != E; ++UI) |
| 7355 | if (PHINode *PN = dyn_cast<PHINode>(*UI)) |
| 7356 | if (PN->getParent() == II->getNormalDest() || |
Chris Lattner | aeb2a1d | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 7357 | PN->getParent() == II->getUnwindDest()) |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 7358 | return false; |
| 7359 | } |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7360 | |
| 7361 | unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin()); |
| 7362 | unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7363 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7364 | CallSite::arg_iterator AI = CS.arg_begin(); |
| 7365 | for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) { |
| 7366 | const Type *ParamTy = FT->getParamType(i); |
Andrew Lenharth | b8e604c | 2006-06-28 01:01:52 +0000 | [diff] [blame] | 7367 | const Type *ActTy = (*AI)->getType(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7368 | ConstantInt *c = dyn_cast<ConstantInt>(*AI); |
Andrew Lenharth | b8e604c | 2006-06-28 01:01:52 +0000 | [diff] [blame] | 7369 | //Either we can cast directly, or we can upconvert the argument |
Chris Lattner | ec47992 | 2007-01-06 02:09:32 +0000 | [diff] [blame] | 7370 | bool isConvertible = ActTy == ParamTy || |
Chris Lattner | 46013f4 | 2007-01-06 19:53:32 +0000 | [diff] [blame] | 7371 | (isa<PointerType>(ParamTy) && isa<PointerType>(ActTy)) || |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 7372 | (ParamTy->isInteger() && ActTy->isInteger() && |
Reid Spencer | abaa8ca | 2007-01-08 16:32:00 +0000 | [diff] [blame] | 7373 | ParamTy->getPrimitiveSizeInBits() >= ActTy->getPrimitiveSizeInBits()) || |
| 7374 | (c && ParamTy->getPrimitiveSizeInBits() >= ActTy->getPrimitiveSizeInBits() |
| 7375 | && c->getSExtValue() > 0); |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 7376 | if (Callee->isDeclaration() && !isConvertible) return false; |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7377 | } |
| 7378 | |
| 7379 | if (FT->getNumParams() < NumActualArgs && !FT->isVarArg() && |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 7380 | Callee->isDeclaration()) |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7381 | return false; // Do not delete arguments unless we have a function body... |
| 7382 | |
| 7383 | // Okay, we decided that this is a safe thing to do: go ahead and start |
| 7384 | // inserting cast instructions as necessary... |
| 7385 | std::vector<Value*> Args; |
| 7386 | Args.reserve(NumActualArgs); |
| 7387 | |
| 7388 | AI = CS.arg_begin(); |
| 7389 | for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) { |
| 7390 | const Type *ParamTy = FT->getParamType(i); |
| 7391 | if ((*AI)->getType() == ParamTy) { |
| 7392 | Args.push_back(*AI); |
| 7393 | } else { |
Reid Spencer | 8a903db | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 7394 | Instruction::CastOps opcode = CastInst::getCastOpcode(*AI, |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 7395 | false, ParamTy, false); |
Reid Spencer | 8a903db | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 7396 | CastInst *NewCast = CastInst::create(opcode, *AI, ParamTy, "tmp"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7397 | Args.push_back(InsertNewInstBefore(NewCast, *Caller)); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7398 | } |
| 7399 | } |
| 7400 | |
| 7401 | // If the function takes more arguments than the call was taking, add them |
| 7402 | // now... |
| 7403 | for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i) |
| 7404 | Args.push_back(Constant::getNullValue(FT->getParamType(i))); |
| 7405 | |
| 7406 | // If we are removing arguments to the function, emit an obnoxious warning... |
| 7407 | if (FT->getNumParams() < NumActualArgs) |
| 7408 | if (!FT->isVarArg()) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 7409 | cerr << "WARNING: While resolving call to function '" |
| 7410 | << Callee->getName() << "' arguments were dropped!\n"; |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7411 | } else { |
| 7412 | // Add all of the arguments in their promoted form to the arg list... |
| 7413 | for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) { |
| 7414 | const Type *PTy = getPromotedType((*AI)->getType()); |
| 7415 | if (PTy != (*AI)->getType()) { |
| 7416 | // Must promote to pass through va_arg area! |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 7417 | Instruction::CastOps opcode = CastInst::getCastOpcode(*AI, false, |
| 7418 | PTy, false); |
Reid Spencer | 8a903db | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 7419 | Instruction *Cast = CastInst::create(opcode, *AI, PTy, "tmp"); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7420 | InsertNewInstBefore(Cast, *Caller); |
| 7421 | Args.push_back(Cast); |
| 7422 | } else { |
| 7423 | Args.push_back(*AI); |
| 7424 | } |
| 7425 | } |
| 7426 | } |
| 7427 | |
| 7428 | if (FT->getReturnType() == Type::VoidTy) |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 7429 | Caller->setName(""); // Void type should not have a name. |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7430 | |
| 7431 | Instruction *NC; |
| 7432 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
Chris Lattner | aeb2a1d | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 7433 | NC = new InvokeInst(Callee, II->getNormalDest(), II->getUnwindDest(), |
Chris Lattner | 93e985f | 2007-02-13 02:10:56 +0000 | [diff] [blame] | 7434 | &Args[0], Args.size(), Caller->getName(), Caller); |
Chris Lattner | e437026 | 2005-05-14 12:25:32 +0000 | [diff] [blame] | 7435 | cast<InvokeInst>(II)->setCallingConv(II->getCallingConv()); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7436 | } else { |
Chris Lattner | 93e985f | 2007-02-13 02:10:56 +0000 | [diff] [blame] | 7437 | NC = new CallInst(Callee, &Args[0], Args.size(), Caller->getName(), Caller); |
Chris Lattner | a9e9211 | 2005-05-06 06:48:21 +0000 | [diff] [blame] | 7438 | if (cast<CallInst>(Caller)->isTailCall()) |
| 7439 | cast<CallInst>(NC)->setTailCall(); |
Chris Lattner | e437026 | 2005-05-14 12:25:32 +0000 | [diff] [blame] | 7440 | cast<CallInst>(NC)->setCallingConv(cast<CallInst>(Caller)->getCallingConv()); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7441 | } |
| 7442 | |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 7443 | // Insert a cast of the return type as necessary. |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7444 | Value *NV = NC; |
| 7445 | if (Caller->getType() != NV->getType() && !Caller->use_empty()) { |
| 7446 | if (NV->getType() != Type::VoidTy) { |
Reid Spencer | 8a903db | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 7447 | const Type *CallerTy = Caller->getType(); |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 7448 | Instruction::CastOps opcode = CastInst::getCastOpcode(NC, false, |
| 7449 | CallerTy, false); |
Reid Spencer | 8a903db | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 7450 | NV = NC = CastInst::create(opcode, NC, CallerTy, "tmp"); |
Chris Lattner | bb60904 | 2003-10-30 00:46:41 +0000 | [diff] [blame] | 7451 | |
| 7452 | // If this is an invoke instruction, we should insert it after the first |
| 7453 | // non-phi, instruction in the normal successor block. |
| 7454 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
| 7455 | BasicBlock::iterator I = II->getNormalDest()->begin(); |
| 7456 | while (isa<PHINode>(I)) ++I; |
| 7457 | InsertNewInstBefore(NC, *I); |
| 7458 | } else { |
| 7459 | // Otherwise, it's a call, just insert cast right after the call instr |
| 7460 | InsertNewInstBefore(NC, *Caller); |
| 7461 | } |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 7462 | AddUsersToWorkList(*Caller); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7463 | } else { |
Chris Lattner | c30bda7 | 2004-10-17 21:22:38 +0000 | [diff] [blame] | 7464 | NV = UndefValue::get(Caller->getType()); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7465 | } |
| 7466 | } |
| 7467 | |
| 7468 | if (Caller->getType() != Type::VoidTy && !Caller->use_empty()) |
| 7469 | Caller->replaceAllUsesWith(NV); |
Chris Lattner | f22a5c6 | 2007-03-02 19:59:19 +0000 | [diff] [blame] | 7470 | Caller->eraseFromParent(); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 7471 | RemoveFromWorkList(Caller); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7472 | return true; |
| 7473 | } |
| 7474 | |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7475 | /// FoldPHIArgBinOpIntoPHI - If we have something like phi [add (a,b), add(c,d)] |
| 7476 | /// and if a/b/c/d and the add's all have a single use, turn this into two phi's |
| 7477 | /// and a single binop. |
| 7478 | Instruction *InstCombiner::FoldPHIArgBinOpIntoPHI(PHINode &PN) { |
| 7479 | Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0)); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7480 | assert(isa<BinaryOperator>(FirstInst) || isa<GetElementPtrInst>(FirstInst) || |
| 7481 | isa<CmpInst>(FirstInst)); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7482 | unsigned Opc = FirstInst->getOpcode(); |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 7483 | Value *LHSVal = FirstInst->getOperand(0); |
| 7484 | Value *RHSVal = FirstInst->getOperand(1); |
| 7485 | |
| 7486 | const Type *LHSType = LHSVal->getType(); |
| 7487 | const Type *RHSType = RHSVal->getType(); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7488 | |
| 7489 | // Scan to see if all operands are the same opcode, all have one use, and all |
| 7490 | // kill their operands (i.e. the operands have one use). |
Chris Lattner | a90a24c | 2006-11-01 04:55:47 +0000 | [diff] [blame] | 7491 | for (unsigned i = 0; i != PN.getNumIncomingValues(); ++i) { |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7492 | Instruction *I = dyn_cast<Instruction>(PN.getIncomingValue(i)); |
Chris Lattner | a90a24c | 2006-11-01 04:55:47 +0000 | [diff] [blame] | 7493 | if (!I || I->getOpcode() != Opc || !I->hasOneUse() || |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7494 | // 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] | 7495 | // types or GEP's with different index types. |
| 7496 | I->getOperand(0)->getType() != LHSType || |
| 7497 | I->getOperand(1)->getType() != RHSType) |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7498 | return 0; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7499 | |
| 7500 | // If they are CmpInst instructions, check their predicates |
| 7501 | if (Opc == Instruction::ICmp || Opc == Instruction::FCmp) |
| 7502 | if (cast<CmpInst>(I)->getPredicate() != |
| 7503 | cast<CmpInst>(FirstInst)->getPredicate()) |
| 7504 | return 0; |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 7505 | |
| 7506 | // Keep track of which operand needs a phi node. |
| 7507 | if (I->getOperand(0) != LHSVal) LHSVal = 0; |
| 7508 | if (I->getOperand(1) != RHSVal) RHSVal = 0; |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7509 | } |
| 7510 | |
Chris Lattner | 53738a4 | 2006-11-08 19:42:28 +0000 | [diff] [blame] | 7511 | // Otherwise, this is safe to transform, determine if it is profitable. |
| 7512 | |
| 7513 | // If this is a GEP, and if the index (not the pointer) needs a PHI, bail out. |
| 7514 | // Indexes are often folded into load/store instructions, so we don't want to |
| 7515 | // hide them behind a phi. |
| 7516 | if (isa<GetElementPtrInst>(FirstInst) && RHSVal == 0) |
| 7517 | return 0; |
| 7518 | |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7519 | Value *InLHS = FirstInst->getOperand(0); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7520 | Value *InRHS = FirstInst->getOperand(1); |
Chris Lattner | 53738a4 | 2006-11-08 19:42:28 +0000 | [diff] [blame] | 7521 | PHINode *NewLHS = 0, *NewRHS = 0; |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 7522 | if (LHSVal == 0) { |
| 7523 | NewLHS = new PHINode(LHSType, FirstInst->getOperand(0)->getName()+".pn"); |
| 7524 | NewLHS->reserveOperandSpace(PN.getNumOperands()/2); |
| 7525 | NewLHS->addIncoming(InLHS, PN.getIncomingBlock(0)); |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 7526 | InsertNewInstBefore(NewLHS, PN); |
| 7527 | LHSVal = NewLHS; |
| 7528 | } |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 7529 | |
| 7530 | if (RHSVal == 0) { |
| 7531 | NewRHS = new PHINode(RHSType, FirstInst->getOperand(1)->getName()+".pn"); |
| 7532 | NewRHS->reserveOperandSpace(PN.getNumOperands()/2); |
| 7533 | NewRHS->addIncoming(InRHS, PN.getIncomingBlock(0)); |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 7534 | InsertNewInstBefore(NewRHS, PN); |
| 7535 | RHSVal = NewRHS; |
| 7536 | } |
| 7537 | |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 7538 | // Add all operands to the new PHIs. |
| 7539 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 7540 | if (NewLHS) { |
| 7541 | Value *NewInLHS =cast<Instruction>(PN.getIncomingValue(i))->getOperand(0); |
| 7542 | NewLHS->addIncoming(NewInLHS, PN.getIncomingBlock(i)); |
| 7543 | } |
| 7544 | if (NewRHS) { |
| 7545 | Value *NewInRHS =cast<Instruction>(PN.getIncomingValue(i))->getOperand(1); |
| 7546 | NewRHS->addIncoming(NewInRHS, PN.getIncomingBlock(i)); |
| 7547 | } |
| 7548 | } |
| 7549 | |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7550 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 7551 | return BinaryOperator::create(BinOp->getOpcode(), LHSVal, RHSVal); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7552 | else if (CmpInst *CIOp = dyn_cast<CmpInst>(FirstInst)) |
| 7553 | return CmpInst::create(CIOp->getOpcode(), CIOp->getPredicate(), LHSVal, |
| 7554 | RHSVal); |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 7555 | else { |
| 7556 | assert(isa<GetElementPtrInst>(FirstInst)); |
| 7557 | return new GetElementPtrInst(LHSVal, RHSVal); |
| 7558 | } |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7559 | } |
| 7560 | |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 7561 | /// isSafeToSinkLoad - Return true if we know that it is safe sink the load out |
| 7562 | /// of the block that defines it. This means that it must be obvious the value |
| 7563 | /// of the load is not changed from the point of the load to the end of the |
| 7564 | /// block it is in. |
Chris Lattner | fd905ca | 2007-02-01 22:30:07 +0000 | [diff] [blame] | 7565 | /// |
| 7566 | /// Finally, it is safe, but not profitable, to sink a load targetting a |
| 7567 | /// non-address-taken alloca. Doing so will cause us to not promote the alloca |
| 7568 | /// to a register. |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 7569 | static bool isSafeToSinkLoad(LoadInst *L) { |
| 7570 | BasicBlock::iterator BBI = L, E = L->getParent()->end(); |
| 7571 | |
| 7572 | for (++BBI; BBI != E; ++BBI) |
| 7573 | if (BBI->mayWriteToMemory()) |
| 7574 | return false; |
Chris Lattner | fd905ca | 2007-02-01 22:30:07 +0000 | [diff] [blame] | 7575 | |
| 7576 | // Check for non-address taken alloca. If not address-taken already, it isn't |
| 7577 | // profitable to do this xform. |
| 7578 | if (AllocaInst *AI = dyn_cast<AllocaInst>(L->getOperand(0))) { |
| 7579 | bool isAddressTaken = false; |
| 7580 | for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); |
| 7581 | UI != E; ++UI) { |
| 7582 | if (isa<LoadInst>(UI)) continue; |
| 7583 | if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) { |
| 7584 | // If storing TO the alloca, then the address isn't taken. |
| 7585 | if (SI->getOperand(1) == AI) continue; |
| 7586 | } |
| 7587 | isAddressTaken = true; |
| 7588 | break; |
| 7589 | } |
| 7590 | |
| 7591 | if (!isAddressTaken) |
| 7592 | return false; |
| 7593 | } |
| 7594 | |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 7595 | return true; |
| 7596 | } |
| 7597 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7598 | |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 7599 | // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary" |
| 7600 | // operator and they all are only used by the PHI, PHI together their |
| 7601 | // inputs, and do the operation once, to the result of the PHI. |
| 7602 | Instruction *InstCombiner::FoldPHIArgOpIntoPHI(PHINode &PN) { |
| 7603 | Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0)); |
| 7604 | |
| 7605 | // Scan the instruction, looking for input operations that can be folded away. |
| 7606 | // If all input operands to the phi are the same instruction (e.g. a cast from |
| 7607 | // the same type or "+42") we can pull the operation through the PHI, reducing |
| 7608 | // code size and simplifying code. |
| 7609 | Constant *ConstantOp = 0; |
| 7610 | const Type *CastSrcTy = 0; |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 7611 | bool isVolatile = false; |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 7612 | if (isa<CastInst>(FirstInst)) { |
| 7613 | CastSrcTy = FirstInst->getOperand(0)->getType(); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7614 | } else if (isa<BinaryOperator>(FirstInst) || isa<CmpInst>(FirstInst)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7615 | // Can fold binop, compare or shift here if the RHS is a constant, |
| 7616 | // otherwise call FoldPHIArgBinOpIntoPHI. |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 7617 | ConstantOp = dyn_cast<Constant>(FirstInst->getOperand(1)); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7618 | if (ConstantOp == 0) |
| 7619 | return FoldPHIArgBinOpIntoPHI(PN); |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 7620 | } else if (LoadInst *LI = dyn_cast<LoadInst>(FirstInst)) { |
| 7621 | isVolatile = LI->isVolatile(); |
| 7622 | // We can't sink the load if the loaded value could be modified between the |
| 7623 | // load and the PHI. |
| 7624 | if (LI->getParent() != PN.getIncomingBlock(0) || |
| 7625 | !isSafeToSinkLoad(LI)) |
| 7626 | return 0; |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 7627 | } else if (isa<GetElementPtrInst>(FirstInst)) { |
Chris Lattner | 53738a4 | 2006-11-08 19:42:28 +0000 | [diff] [blame] | 7628 | if (FirstInst->getNumOperands() == 2) |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 7629 | return FoldPHIArgBinOpIntoPHI(PN); |
| 7630 | // Can't handle general GEPs yet. |
| 7631 | return 0; |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 7632 | } else { |
| 7633 | return 0; // Cannot fold this operation. |
| 7634 | } |
| 7635 | |
| 7636 | // Check to see if all arguments are the same operation. |
| 7637 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 7638 | if (!isa<Instruction>(PN.getIncomingValue(i))) return 0; |
| 7639 | Instruction *I = cast<Instruction>(PN.getIncomingValue(i)); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7640 | if (!I->hasOneUse() || !I->isSameOperationAs(FirstInst)) |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 7641 | return 0; |
| 7642 | if (CastSrcTy) { |
| 7643 | if (I->getOperand(0)->getType() != CastSrcTy) |
| 7644 | return 0; // Cast operation must match. |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 7645 | } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7646 | // We can't sink the load if the loaded value could be modified between |
| 7647 | // the load and the PHI. |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 7648 | if (LI->isVolatile() != isVolatile || |
| 7649 | LI->getParent() != PN.getIncomingBlock(i) || |
| 7650 | !isSafeToSinkLoad(LI)) |
| 7651 | return 0; |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 7652 | } else if (I->getOperand(1) != ConstantOp) { |
| 7653 | return 0; |
| 7654 | } |
| 7655 | } |
| 7656 | |
| 7657 | // Okay, they are all the same operation. Create a new PHI node of the |
| 7658 | // correct type, and PHI together all of the LHS's of the instructions. |
| 7659 | PHINode *NewPN = new PHINode(FirstInst->getOperand(0)->getType(), |
| 7660 | PN.getName()+".in"); |
Chris Lattner | 5551706 | 2005-01-29 00:39:08 +0000 | [diff] [blame] | 7661 | NewPN->reserveOperandSpace(PN.getNumOperands()/2); |
Chris Lattner | b589344 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 7662 | |
| 7663 | Value *InVal = FirstInst->getOperand(0); |
| 7664 | NewPN->addIncoming(InVal, PN.getIncomingBlock(0)); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 7665 | |
| 7666 | // Add all operands to the new PHI. |
Chris Lattner | b589344 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 7667 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 7668 | Value *NewInVal = cast<Instruction>(PN.getIncomingValue(i))->getOperand(0); |
| 7669 | if (NewInVal != InVal) |
| 7670 | InVal = 0; |
| 7671 | NewPN->addIncoming(NewInVal, PN.getIncomingBlock(i)); |
| 7672 | } |
| 7673 | |
| 7674 | Value *PhiVal; |
| 7675 | if (InVal) { |
| 7676 | // The new PHI unions all of the same values together. This is really |
| 7677 | // common, so we handle it intelligently here for compile-time speed. |
| 7678 | PhiVal = InVal; |
| 7679 | delete NewPN; |
| 7680 | } else { |
| 7681 | InsertNewInstBefore(NewPN, PN); |
| 7682 | PhiVal = NewPN; |
| 7683 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7684 | |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 7685 | // Insert and return the new operation. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7686 | if (CastInst* FirstCI = dyn_cast<CastInst>(FirstInst)) |
| 7687 | return CastInst::create(FirstCI->getOpcode(), PhiVal, PN.getType()); |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 7688 | else if (isa<LoadInst>(FirstInst)) |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 7689 | return new LoadInst(PhiVal, "", isVolatile); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 7690 | else if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) |
Chris Lattner | b589344 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 7691 | return BinaryOperator::create(BinOp->getOpcode(), PhiVal, ConstantOp); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7692 | else if (CmpInst *CIOp = dyn_cast<CmpInst>(FirstInst)) |
| 7693 | return CmpInst::create(CIOp->getOpcode(), CIOp->getPredicate(), |
| 7694 | PhiVal, ConstantOp); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 7695 | else |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7696 | assert(0 && "Unknown operation"); |
Jeff Cohen | ca5183d | 2007-03-05 00:00:42 +0000 | [diff] [blame] | 7697 | return 0; |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 7698 | } |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 7699 | |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 7700 | /// DeadPHICycle - Return true if this PHI node is only used by a PHI node cycle |
| 7701 | /// that is dead. |
| 7702 | static bool DeadPHICycle(PHINode *PN, std::set<PHINode*> &PotentiallyDeadPHIs) { |
| 7703 | if (PN->use_empty()) return true; |
| 7704 | if (!PN->hasOneUse()) return false; |
| 7705 | |
| 7706 | // Remember this node, and if we find the cycle, return. |
| 7707 | if (!PotentiallyDeadPHIs.insert(PN).second) |
| 7708 | return true; |
| 7709 | |
| 7710 | if (PHINode *PU = dyn_cast<PHINode>(PN->use_back())) |
| 7711 | return DeadPHICycle(PU, PotentiallyDeadPHIs); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7712 | |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 7713 | return false; |
| 7714 | } |
| 7715 | |
Chris Lattner | 473945d | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 7716 | // PHINode simplification |
| 7717 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 7718 | Instruction *InstCombiner::visitPHINode(PHINode &PN) { |
Owen Anderson | b64ab87 | 2006-07-10 22:15:25 +0000 | [diff] [blame] | 7719 | // If LCSSA is around, don't mess with Phi nodes |
Chris Lattner | f964f32 | 2007-03-04 04:27:24 +0000 | [diff] [blame] | 7720 | if (MustPreserveLCSSA) return 0; |
Owen Anderson | d1b78a1 | 2006-07-10 19:03:49 +0000 | [diff] [blame] | 7721 | |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 7722 | if (Value *V = PN.hasConstantValue()) |
| 7723 | return ReplaceInstUsesWith(PN, V); |
| 7724 | |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 7725 | // If all PHI operands are the same operation, pull them through the PHI, |
| 7726 | // reducing code size. |
| 7727 | if (isa<Instruction>(PN.getIncomingValue(0)) && |
| 7728 | PN.getIncomingValue(0)->hasOneUse()) |
| 7729 | if (Instruction *Result = FoldPHIArgOpIntoPHI(PN)) |
| 7730 | return Result; |
| 7731 | |
| 7732 | // If this is a trivial cycle in the PHI node graph, remove it. Basically, if |
| 7733 | // this PHI only has a single use (a PHI), and if that PHI only has one use (a |
| 7734 | // PHI)... break the cycle. |
Chris Lattner | ff9f13a | 2007-01-15 07:30:06 +0000 | [diff] [blame] | 7735 | if (PN.hasOneUse()) { |
| 7736 | Instruction *PHIUser = cast<Instruction>(PN.use_back()); |
| 7737 | if (PHINode *PU = dyn_cast<PHINode>(PHIUser)) { |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 7738 | std::set<PHINode*> PotentiallyDeadPHIs; |
| 7739 | PotentiallyDeadPHIs.insert(&PN); |
| 7740 | if (DeadPHICycle(PU, PotentiallyDeadPHIs)) |
| 7741 | return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType())); |
| 7742 | } |
Chris Lattner | ff9f13a | 2007-01-15 07:30:06 +0000 | [diff] [blame] | 7743 | |
| 7744 | // If this phi has a single use, and if that use just computes a value for |
| 7745 | // the next iteration of a loop, delete the phi. This occurs with unused |
| 7746 | // induction variables, e.g. "for (int j = 0; ; ++j);". Detecting this |
| 7747 | // common case here is good because the only other things that catch this |
| 7748 | // are induction variable analysis (sometimes) and ADCE, which is only run |
| 7749 | // late. |
| 7750 | if (PHIUser->hasOneUse() && |
| 7751 | (isa<BinaryOperator>(PHIUser) || isa<GetElementPtrInst>(PHIUser)) && |
| 7752 | PHIUser->use_back() == &PN) { |
| 7753 | return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType())); |
| 7754 | } |
| 7755 | } |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 7756 | |
Chris Lattner | 60921c9 | 2003-12-19 05:58:40 +0000 | [diff] [blame] | 7757 | return 0; |
Chris Lattner | 473945d | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 7758 | } |
| 7759 | |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7760 | static Value *InsertCastToIntPtrTy(Value *V, const Type *DTy, |
| 7761 | Instruction *InsertPoint, |
| 7762 | InstCombiner *IC) { |
Reid Spencer | abaa8ca | 2007-01-08 16:32:00 +0000 | [diff] [blame] | 7763 | unsigned PtrSize = DTy->getPrimitiveSizeInBits(); |
| 7764 | unsigned VTySize = V->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7765 | // We must cast correctly to the pointer type. Ensure that we |
| 7766 | // sign extend the integer value if it is smaller as this is |
| 7767 | // used for address computation. |
| 7768 | Instruction::CastOps opcode = |
| 7769 | (VTySize < PtrSize ? Instruction::SExt : |
| 7770 | (VTySize == PtrSize ? Instruction::BitCast : Instruction::Trunc)); |
| 7771 | return IC->InsertCastBefore(opcode, V, DTy, *InsertPoint); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7772 | } |
| 7773 | |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 7774 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 7775 | Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7776 | Value *PtrOp = GEP.getOperand(0); |
Chris Lattner | c54e2b8 | 2003-05-22 19:07:21 +0000 | [diff] [blame] | 7777 | // Is it 'getelementptr %P, long 0' or 'getelementptr %P' |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 7778 | // If so, eliminate the noop. |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 7779 | if (GEP.getNumOperands() == 1) |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7780 | return ReplaceInstUsesWith(GEP, PtrOp); |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 7781 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 7782 | if (isa<UndefValue>(GEP.getOperand(0))) |
| 7783 | return ReplaceInstUsesWith(GEP, UndefValue::get(GEP.getType())); |
| 7784 | |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 7785 | bool HasZeroPointerIndex = false; |
| 7786 | if (Constant *C = dyn_cast<Constant>(GEP.getOperand(1))) |
| 7787 | HasZeroPointerIndex = C->isNullValue(); |
| 7788 | |
| 7789 | if (GEP.getNumOperands() == 2 && HasZeroPointerIndex) |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7790 | return ReplaceInstUsesWith(GEP, PtrOp); |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 7791 | |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7792 | // Eliminate unneeded casts for indices. |
| 7793 | bool MadeChange = false; |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 7794 | gep_type_iterator GTI = gep_type_begin(GEP); |
| 7795 | for (unsigned i = 1, e = GEP.getNumOperands(); i != e; ++i, ++GTI) |
| 7796 | if (isa<SequentialType>(*GTI)) { |
| 7797 | if (CastInst *CI = dyn_cast<CastInst>(GEP.getOperand(i))) { |
Chris Lattner | 76b7a06 | 2007-01-15 07:02:54 +0000 | [diff] [blame] | 7798 | if (CI->getOpcode() == Instruction::ZExt || |
| 7799 | CI->getOpcode() == Instruction::SExt) { |
| 7800 | const Type *SrcTy = CI->getOperand(0)->getType(); |
| 7801 | // We can eliminate a cast from i32 to i64 iff the target |
| 7802 | // is a 32-bit pointer target. |
| 7803 | if (SrcTy->getPrimitiveSizeInBits() >= TD->getPointerSizeInBits()) { |
| 7804 | MadeChange = true; |
| 7805 | GEP.setOperand(i, CI->getOperand(0)); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7806 | } |
| 7807 | } |
| 7808 | } |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 7809 | // If we are using a wider index than needed for this platform, shrink it |
| 7810 | // to what we need. If the incoming value needs a cast instruction, |
| 7811 | // insert it. This explicit cast can make subsequent optimizations more |
| 7812 | // obvious. |
| 7813 | Value *Op = GEP.getOperand(i); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 7814 | if (TD->getTypeSize(Op->getType()) > TD->getPointerSize()) |
Chris Lattner | 4f1134e | 2004-04-17 18:16:10 +0000 | [diff] [blame] | 7815 | if (Constant *C = dyn_cast<Constant>(Op)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7816 | GEP.setOperand(i, ConstantExpr::getTrunc(C, TD->getIntPtrType())); |
Chris Lattner | 4f1134e | 2004-04-17 18:16:10 +0000 | [diff] [blame] | 7817 | MadeChange = true; |
| 7818 | } else { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7819 | Op = InsertCastBefore(Instruction::Trunc, Op, TD->getIntPtrType(), |
| 7820 | GEP); |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 7821 | GEP.setOperand(i, Op); |
| 7822 | MadeChange = true; |
| 7823 | } |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7824 | } |
| 7825 | if (MadeChange) return &GEP; |
| 7826 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 7827 | // Combine Indices - If the source pointer to this getelementptr instruction |
| 7828 | // is a getelementptr instruction, combine the indices of the two |
| 7829 | // getelementptr instructions into a single instruction. |
| 7830 | // |
Chris Lattner | 72588fc | 2007-02-15 22:48:32 +0000 | [diff] [blame] | 7831 | SmallVector<Value*, 8> SrcGEPOperands; |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 7832 | if (User *Src = dyn_castGetElementPtr(PtrOp)) |
Chris Lattner | 72588fc | 2007-02-15 22:48:32 +0000 | [diff] [blame] | 7833 | SrcGEPOperands.append(Src->op_begin(), Src->op_end()); |
Chris Lattner | ebd985c | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 7834 | |
| 7835 | if (!SrcGEPOperands.empty()) { |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7836 | // Note that if our source is a gep chain itself that we wait for that |
| 7837 | // chain to be resolved before we perform this transformation. This |
| 7838 | // avoids us creating a TON of code in some cases. |
| 7839 | // |
| 7840 | if (isa<GetElementPtrInst>(SrcGEPOperands[0]) && |
| 7841 | cast<Instruction>(SrcGEPOperands[0])->getNumOperands() == 2) |
| 7842 | return 0; // Wait until our source is folded to completion. |
| 7843 | |
Chris Lattner | 72588fc | 2007-02-15 22:48:32 +0000 | [diff] [blame] | 7844 | SmallVector<Value*, 8> Indices; |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7845 | |
| 7846 | // Find out whether the last index in the source GEP is a sequential idx. |
| 7847 | bool EndsWithSequential = false; |
| 7848 | for (gep_type_iterator I = gep_type_begin(*cast<User>(PtrOp)), |
| 7849 | E = gep_type_end(*cast<User>(PtrOp)); I != E; ++I) |
Chris Lattner | be97b4e | 2004-05-08 22:41:42 +0000 | [diff] [blame] | 7850 | EndsWithSequential = !isa<StructType>(*I); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7851 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 7852 | // Can we combine the two pointer arithmetics offsets? |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7853 | if (EndsWithSequential) { |
Chris Lattner | decd081 | 2003-03-05 22:33:14 +0000 | [diff] [blame] | 7854 | // Replace: gep (gep %P, long B), long A, ... |
| 7855 | // With: T = long A+B; gep %P, T, ... |
| 7856 | // |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7857 | Value *Sum, *SO1 = SrcGEPOperands.back(), *GO1 = GEP.getOperand(1); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7858 | if (SO1 == Constant::getNullValue(SO1->getType())) { |
| 7859 | Sum = GO1; |
| 7860 | } else if (GO1 == Constant::getNullValue(GO1->getType())) { |
| 7861 | Sum = SO1; |
| 7862 | } else { |
| 7863 | // If they aren't the same type, convert both to an integer of the |
| 7864 | // target's pointer size. |
| 7865 | if (SO1->getType() != GO1->getType()) { |
| 7866 | if (Constant *SO1C = dyn_cast<Constant>(SO1)) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7867 | SO1 = ConstantExpr::getIntegerCast(SO1C, GO1->getType(), true); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7868 | } else if (Constant *GO1C = dyn_cast<Constant>(GO1)) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7869 | GO1 = ConstantExpr::getIntegerCast(GO1C, SO1->getType(), true); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7870 | } else { |
| 7871 | unsigned PS = TD->getPointerSize(); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 7872 | if (TD->getTypeSize(SO1->getType()) == PS) { |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7873 | // Convert GO1 to SO1's type. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7874 | GO1 = InsertCastToIntPtrTy(GO1, SO1->getType(), &GEP, this); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7875 | |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 7876 | } else if (TD->getTypeSize(GO1->getType()) == PS) { |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7877 | // Convert SO1 to GO1's type. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7878 | SO1 = InsertCastToIntPtrTy(SO1, GO1->getType(), &GEP, this); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7879 | } else { |
| 7880 | const Type *PT = TD->getIntPtrType(); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7881 | SO1 = InsertCastToIntPtrTy(SO1, PT, &GEP, this); |
| 7882 | GO1 = InsertCastToIntPtrTy(GO1, PT, &GEP, this); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7883 | } |
| 7884 | } |
| 7885 | } |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7886 | if (isa<Constant>(SO1) && isa<Constant>(GO1)) |
| 7887 | Sum = ConstantExpr::getAdd(cast<Constant>(SO1), cast<Constant>(GO1)); |
| 7888 | else { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 7889 | Sum = BinaryOperator::createAdd(SO1, GO1, PtrOp->getName()+".sum"); |
| 7890 | InsertNewInstBefore(cast<Instruction>(Sum), GEP); |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7891 | } |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7892 | } |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7893 | |
| 7894 | // Recycle the GEP we already have if possible. |
| 7895 | if (SrcGEPOperands.size() == 2) { |
| 7896 | GEP.setOperand(0, SrcGEPOperands[0]); |
| 7897 | GEP.setOperand(1, Sum); |
| 7898 | return &GEP; |
| 7899 | } else { |
| 7900 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, |
| 7901 | SrcGEPOperands.end()-1); |
| 7902 | Indices.push_back(Sum); |
| 7903 | Indices.insert(Indices.end(), GEP.op_begin()+2, GEP.op_end()); |
| 7904 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7905 | } else if (isa<Constant>(*GEP.idx_begin()) && |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7906 | cast<Constant>(*GEP.idx_begin())->isNullValue() && |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7907 | SrcGEPOperands.size() != 1) { |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 7908 | // 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] | 7909 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, |
| 7910 | SrcGEPOperands.end()); |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 7911 | Indices.insert(Indices.end(), GEP.idx_begin()+1, GEP.idx_end()); |
| 7912 | } |
| 7913 | |
| 7914 | if (!Indices.empty()) |
Chris Lattner | 1ccd185 | 2007-02-12 22:56:41 +0000 | [diff] [blame] | 7915 | return new GetElementPtrInst(SrcGEPOperands[0], &Indices[0], |
| 7916 | Indices.size(), GEP.getName()); |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 7917 | |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7918 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(PtrOp)) { |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 7919 | // GEP of global variable. If all of the indices for this GEP are |
| 7920 | // constants, we can promote this to a constexpr instead of an instruction. |
| 7921 | |
| 7922 | // Scan for nonconstants... |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 7923 | SmallVector<Constant*, 8> Indices; |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 7924 | User::op_iterator I = GEP.idx_begin(), E = GEP.idx_end(); |
| 7925 | for (; I != E && isa<Constant>(*I); ++I) |
| 7926 | Indices.push_back(cast<Constant>(*I)); |
| 7927 | |
| 7928 | if (I == E) { // If they are all constants... |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 7929 | Constant *CE = ConstantExpr::getGetElementPtr(GV, |
| 7930 | &Indices[0],Indices.size()); |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 7931 | |
| 7932 | // Replace all uses of the GEP with the new constexpr... |
| 7933 | return ReplaceInstUsesWith(GEP, CE); |
| 7934 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7935 | } else if (Value *X = getBitCastOperand(PtrOp)) { // Is the operand a cast? |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 7936 | if (!isa<PointerType>(X->getType())) { |
| 7937 | // Not interesting. Source pointer must be a cast from pointer. |
| 7938 | } else if (HasZeroPointerIndex) { |
| 7939 | // transform: GEP (cast [10 x ubyte]* X to [0 x ubyte]*), long 0, ... |
| 7940 | // into : GEP [10 x ubyte]* X, long 0, ... |
| 7941 | // |
| 7942 | // This occurs when the program declares an array extern like "int X[];" |
| 7943 | // |
| 7944 | const PointerType *CPTy = cast<PointerType>(PtrOp->getType()); |
| 7945 | const PointerType *XTy = cast<PointerType>(X->getType()); |
| 7946 | if (const ArrayType *XATy = |
| 7947 | dyn_cast<ArrayType>(XTy->getElementType())) |
| 7948 | if (const ArrayType *CATy = |
| 7949 | dyn_cast<ArrayType>(CPTy->getElementType())) |
| 7950 | if (CATy->getElementType() == XATy->getElementType()) { |
| 7951 | // At this point, we know that the cast source type is a pointer |
| 7952 | // to an array of the same type as the destination pointer |
| 7953 | // array. Because the array type is never stepped over (there |
| 7954 | // is a leading zero) we can fold the cast into this GEP. |
| 7955 | GEP.setOperand(0, X); |
| 7956 | return &GEP; |
| 7957 | } |
| 7958 | } else if (GEP.getNumOperands() == 2) { |
| 7959 | // Transform things like: |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 7960 | // %t = getelementptr ubyte* cast ([2 x int]* %str to uint*), uint %V |
| 7961 | // into: %t1 = getelementptr [2 x int*]* %str, int 0, uint %V; cast |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 7962 | const Type *SrcElTy = cast<PointerType>(X->getType())->getElementType(); |
| 7963 | const Type *ResElTy=cast<PointerType>(PtrOp->getType())->getElementType(); |
| 7964 | if (isa<ArrayType>(SrcElTy) && |
| 7965 | TD->getTypeSize(cast<ArrayType>(SrcElTy)->getElementType()) == |
| 7966 | TD->getTypeSize(ResElTy)) { |
| 7967 | Value *V = InsertNewInstBefore( |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 7968 | new GetElementPtrInst(X, Constant::getNullValue(Type::Int32Ty), |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 7969 | GEP.getOperand(1), GEP.getName()), GEP); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7970 | // V and GEP are both pointer types --> BitCast |
| 7971 | return new BitCastInst(V, GEP.getType()); |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 7972 | } |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 7973 | |
| 7974 | // Transform things like: |
| 7975 | // getelementptr sbyte* cast ([100 x double]* X to sbyte*), int %tmp |
| 7976 | // (where tmp = 8*tmp2) into: |
| 7977 | // getelementptr [100 x double]* %arr, int 0, int %tmp.2 |
| 7978 | |
| 7979 | if (isa<ArrayType>(SrcElTy) && |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 7980 | (ResElTy == Type::Int8Ty || ResElTy == Type::Int8Ty)) { |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 7981 | uint64_t ArrayEltSize = |
| 7982 | TD->getTypeSize(cast<ArrayType>(SrcElTy)->getElementType()); |
| 7983 | |
| 7984 | // Check to see if "tmp" is a scale by a multiple of ArrayEltSize. We |
| 7985 | // allow either a mul, shift, or constant here. |
| 7986 | Value *NewIdx = 0; |
| 7987 | ConstantInt *Scale = 0; |
| 7988 | if (ArrayEltSize == 1) { |
| 7989 | NewIdx = GEP.getOperand(1); |
| 7990 | Scale = ConstantInt::get(NewIdx->getType(), 1); |
| 7991 | } else if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP.getOperand(1))) { |
Chris Lattner | 6e2f843 | 2005-09-14 17:32:56 +0000 | [diff] [blame] | 7992 | NewIdx = ConstantInt::get(CI->getType(), 1); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 7993 | Scale = CI; |
| 7994 | } else if (Instruction *Inst =dyn_cast<Instruction>(GEP.getOperand(1))){ |
| 7995 | if (Inst->getOpcode() == Instruction::Shl && |
| 7996 | isa<ConstantInt>(Inst->getOperand(1))) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7997 | unsigned ShAmt = |
| 7998 | cast<ConstantInt>(Inst->getOperand(1))->getZExtValue(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7999 | Scale = ConstantInt::get(Inst->getType(), 1ULL << ShAmt); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 8000 | NewIdx = Inst->getOperand(0); |
| 8001 | } else if (Inst->getOpcode() == Instruction::Mul && |
| 8002 | isa<ConstantInt>(Inst->getOperand(1))) { |
| 8003 | Scale = cast<ConstantInt>(Inst->getOperand(1)); |
| 8004 | NewIdx = Inst->getOperand(0); |
| 8005 | } |
| 8006 | } |
| 8007 | |
| 8008 | // If the index will be to exactly the right offset with the scale taken |
| 8009 | // out, perform the transformation. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8010 | if (Scale && Scale->getZExtValue() % ArrayEltSize == 0) { |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 8011 | if (isa<ConstantInt>(Scale)) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8012 | Scale = ConstantInt::get(Scale->getType(), |
| 8013 | Scale->getZExtValue() / ArrayEltSize); |
| 8014 | if (Scale->getZExtValue() != 1) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 8015 | Constant *C = ConstantExpr::getIntegerCast(Scale, NewIdx->getType(), |
| 8016 | true /*SExt*/); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 8017 | Instruction *Sc = BinaryOperator::createMul(NewIdx, C, "idxscale"); |
| 8018 | NewIdx = InsertNewInstBefore(Sc, GEP); |
| 8019 | } |
| 8020 | |
| 8021 | // Insert the new GEP instruction. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8022 | Instruction *NewGEP = |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8023 | new GetElementPtrInst(X, Constant::getNullValue(Type::Int32Ty), |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 8024 | NewIdx, GEP.getName()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8025 | NewGEP = InsertNewInstBefore(NewGEP, GEP); |
| 8026 | // The NewGEP must be pointer typed, so must the old one -> BitCast |
| 8027 | return new BitCastInst(NewGEP, GEP.getType()); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 8028 | } |
| 8029 | } |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 8030 | } |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 8031 | } |
| 8032 | |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 8033 | return 0; |
| 8034 | } |
| 8035 | |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 8036 | Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) { |
| 8037 | // Convert: malloc Ty, C - where C is a constant != 1 into: malloc [C x Ty], 1 |
| 8038 | if (AI.isArrayAllocation()) // Check C != 1 |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8039 | if (const ConstantInt *C = dyn_cast<ConstantInt>(AI.getArraySize())) { |
| 8040 | const Type *NewTy = |
| 8041 | ArrayType::get(AI.getAllocatedType(), C->getZExtValue()); |
Chris Lattner | 0006bd7 | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 8042 | AllocationInst *New = 0; |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 8043 | |
| 8044 | // Create and insert the replacement instruction... |
| 8045 | if (isa<MallocInst>(AI)) |
Nate Begeman | 14b0529 | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 8046 | New = new MallocInst(NewTy, 0, AI.getAlignment(), AI.getName()); |
Chris Lattner | 0006bd7 | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 8047 | else { |
| 8048 | assert(isa<AllocaInst>(AI) && "Unknown type of allocation inst!"); |
Nate Begeman | 14b0529 | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 8049 | New = new AllocaInst(NewTy, 0, AI.getAlignment(), AI.getName()); |
Chris Lattner | 0006bd7 | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 8050 | } |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 8051 | |
| 8052 | InsertNewInstBefore(New, AI); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8053 | |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 8054 | // Scan to the end of the allocation instructions, to skip over a block of |
| 8055 | // allocas if possible... |
| 8056 | // |
| 8057 | BasicBlock::iterator It = New; |
| 8058 | while (isa<AllocationInst>(*It)) ++It; |
| 8059 | |
| 8060 | // Now that I is pointing to the first non-allocation-inst in the block, |
| 8061 | // insert our getelementptr instruction... |
| 8062 | // |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8063 | Value *NullIdx = Constant::getNullValue(Type::Int32Ty); |
Chris Lattner | 693787a | 2005-05-04 19:10:26 +0000 | [diff] [blame] | 8064 | Value *V = new GetElementPtrInst(New, NullIdx, NullIdx, |
| 8065 | New->getName()+".sub", It); |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 8066 | |
| 8067 | // Now make everything use the getelementptr instead of the original |
| 8068 | // allocation. |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 8069 | return ReplaceInstUsesWith(AI, V); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8070 | } else if (isa<UndefValue>(AI.getArraySize())) { |
| 8071 | return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 8072 | } |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 8073 | |
| 8074 | // If alloca'ing a zero byte object, replace the alloca with a null pointer. |
| 8075 | // Note that we only do this for alloca's, because malloc should allocate and |
| 8076 | // return a unique pointer, even for a zero byte allocation. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8077 | if (isa<AllocaInst>(AI) && AI.getAllocatedType()->isSized() && |
Chris Lattner | cf27afb | 2004-07-02 22:55:47 +0000 | [diff] [blame] | 8078 | TD->getTypeSize(AI.getAllocatedType()) == 0) |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 8079 | return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); |
| 8080 | |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 8081 | return 0; |
| 8082 | } |
| 8083 | |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 8084 | Instruction *InstCombiner::visitFreeInst(FreeInst &FI) { |
| 8085 | Value *Op = FI.getOperand(0); |
| 8086 | |
| 8087 | // Change free <ty>* (cast <ty2>* X to <ty>*) into free <ty2>* X |
| 8088 | if (CastInst *CI = dyn_cast<CastInst>(Op)) |
| 8089 | if (isa<PointerType>(CI->getOperand(0)->getType())) { |
| 8090 | FI.setOperand(0, CI->getOperand(0)); |
| 8091 | return &FI; |
| 8092 | } |
| 8093 | |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8094 | // free undef -> unreachable. |
| 8095 | if (isa<UndefValue>(Op)) { |
| 8096 | // 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] | 8097 | new StoreInst(ConstantInt::getTrue(), |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 8098 | UndefValue::get(PointerType::get(Type::Int1Ty)), &FI); |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8099 | return EraseInstFromFunction(FI); |
| 8100 | } |
| 8101 | |
Chris Lattner | 6160e85 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 8102 | // If we have 'free null' delete the instruction. This can happen in stl code |
| 8103 | // when lots of inlining happens. |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8104 | if (isa<ConstantPointerNull>(Op)) |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 8105 | return EraseInstFromFunction(FI); |
Chris Lattner | 6160e85 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 8106 | |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 8107 | return 0; |
| 8108 | } |
| 8109 | |
| 8110 | |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8111 | /// InstCombineLoadCast - Fold 'load (cast P)' -> cast (load P)' when possible. |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 8112 | static Instruction *InstCombineLoadCast(InstCombiner &IC, LoadInst &LI) { |
| 8113 | User *CI = cast<User>(LI.getOperand(0)); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 8114 | Value *CastOp = CI->getOperand(0); |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 8115 | |
| 8116 | const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType(); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 8117 | if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) { |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 8118 | const Type *SrcPTy = SrcTy->getElementType(); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 8119 | |
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 8120 | if (DestPTy->isInteger() || isa<PointerType>(DestPTy) || |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 8121 | isa<VectorType>(DestPTy)) { |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 8122 | // If the source is an array, the code below will not succeed. Check to |
| 8123 | // see if a trivial 'gep P, 0, 0' will help matters. Only do this for |
| 8124 | // constants. |
| 8125 | if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy)) |
| 8126 | if (Constant *CSrc = dyn_cast<Constant>(CastOp)) |
| 8127 | if (ASrcTy->getNumElements() != 0) { |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 8128 | Value *Idxs[2]; |
| 8129 | Idxs[0] = Idxs[1] = Constant::getNullValue(Type::Int32Ty); |
| 8130 | CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs, 2); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 8131 | SrcTy = cast<PointerType>(CastOp->getType()); |
| 8132 | SrcPTy = SrcTy->getElementType(); |
| 8133 | } |
| 8134 | |
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 8135 | if ((SrcPTy->isInteger() || isa<PointerType>(SrcPTy) || |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 8136 | isa<VectorType>(SrcPTy)) && |
Chris Lattner | b1515fe | 2005-03-29 06:37:47 +0000 | [diff] [blame] | 8137 | // Do not allow turning this into a load of an integer, which is then |
| 8138 | // casted to a pointer, this pessimizes pointer analysis a lot. |
| 8139 | (isa<PointerType>(SrcPTy) == isa<PointerType>(LI.getType())) && |
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 8140 | IC.getTargetData().getTypeSizeInBits(SrcPTy) == |
| 8141 | IC.getTargetData().getTypeSizeInBits(DestPTy)) { |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8142 | |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 8143 | // Okay, we are casting from one integer or pointer type to another of |
| 8144 | // the same size. Instead of casting the pointer before the load, cast |
| 8145 | // the result of the loaded value. |
| 8146 | Value *NewLoad = IC.InsertNewInstBefore(new LoadInst(CastOp, |
| 8147 | CI->getName(), |
| 8148 | LI.isVolatile()),LI); |
| 8149 | // Now cast the result of the load. |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 8150 | return new BitCastInst(NewLoad, LI.getType()); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 8151 | } |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 8152 | } |
| 8153 | } |
| 8154 | return 0; |
| 8155 | } |
| 8156 | |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 8157 | /// isSafeToLoadUnconditionally - Return true if we know that executing a load |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 8158 | /// from this value cannot trap. If it is not obviously safe to load from the |
| 8159 | /// specified pointer, we do a quick local scan of the basic block containing |
| 8160 | /// ScanFrom, to determine if the address is already accessed. |
| 8161 | static bool isSafeToLoadUnconditionally(Value *V, Instruction *ScanFrom) { |
| 8162 | // If it is an alloca or global variable, it is always safe to load from. |
| 8163 | if (isa<AllocaInst>(V) || isa<GlobalVariable>(V)) return true; |
| 8164 | |
| 8165 | // Otherwise, be a little bit agressive by scanning the local block where we |
| 8166 | // 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] | 8167 | // from/to. If so, the previous load or store would have already trapped, |
| 8168 | // so there is no harm doing an extra load (also, CSE will later eliminate |
| 8169 | // the load entirely). |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 8170 | BasicBlock::iterator BBI = ScanFrom, E = ScanFrom->getParent()->begin(); |
| 8171 | |
Alkis Evlogimenos | 7b6ec60 | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 8172 | while (BBI != E) { |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 8173 | --BBI; |
| 8174 | |
| 8175 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { |
| 8176 | if (LI->getOperand(0) == V) return true; |
| 8177 | } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) |
| 8178 | if (SI->getOperand(1) == V) return true; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8179 | |
Alkis Evlogimenos | 7b6ec60 | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 8180 | } |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 8181 | return false; |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 8182 | } |
| 8183 | |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 8184 | Instruction *InstCombiner::visitLoadInst(LoadInst &LI) { |
| 8185 | Value *Op = LI.getOperand(0); |
Chris Lattner | 5f16a13 | 2004-01-12 04:13:56 +0000 | [diff] [blame] | 8186 | |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 8187 | // load (cast X) --> cast (load X) iff safe |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 8188 | if (isa<CastInst>(Op)) |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 8189 | if (Instruction *Res = InstCombineLoadCast(*this, LI)) |
| 8190 | return Res; |
| 8191 | |
| 8192 | // None of the following transforms are legal for volatile loads. |
| 8193 | if (LI.isVolatile()) return 0; |
Chris Lattner | 62f254d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 8194 | |
Chris Lattner | 62f254d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 8195 | if (&LI.getParent()->front() != &LI) { |
| 8196 | BasicBlock::iterator BBI = &LI; --BBI; |
Chris Lattner | 9c1f0fd | 2005-09-12 22:21:03 +0000 | [diff] [blame] | 8197 | // If the instruction immediately before this is a store to the same |
| 8198 | // address, do a simple form of store->load forwarding. |
Chris Lattner | 62f254d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 8199 | if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) |
| 8200 | if (SI->getOperand(1) == LI.getOperand(0)) |
| 8201 | return ReplaceInstUsesWith(LI, SI->getOperand(0)); |
Chris Lattner | 9c1f0fd | 2005-09-12 22:21:03 +0000 | [diff] [blame] | 8202 | if (LoadInst *LIB = dyn_cast<LoadInst>(BBI)) |
| 8203 | if (LIB->getOperand(0) == LI.getOperand(0)) |
| 8204 | return ReplaceInstUsesWith(LI, LIB); |
Chris Lattner | 62f254d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 8205 | } |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 8206 | |
| 8207 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) |
| 8208 | if (isa<ConstantPointerNull>(GEPI->getOperand(0)) || |
| 8209 | isa<UndefValue>(GEPI->getOperand(0))) { |
| 8210 | // Insert a new store to null instruction before the load to indicate |
| 8211 | // that this code is not reachable. We do this instead of inserting |
| 8212 | // an unreachable instruction directly because we cannot modify the |
| 8213 | // CFG. |
| 8214 | new StoreInst(UndefValue::get(LI.getType()), |
| 8215 | Constant::getNullValue(Op->getType()), &LI); |
| 8216 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
| 8217 | } |
| 8218 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8219 | if (Constant *C = dyn_cast<Constant>(Op)) { |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 8220 | // load null/undef -> undef |
| 8221 | if ((C->isNullValue() || isa<UndefValue>(C))) { |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8222 | // Insert a new store to null instruction before the load to indicate that |
| 8223 | // this code is not reachable. We do this instead of inserting an |
| 8224 | // unreachable instruction directly because we cannot modify the CFG. |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 8225 | new StoreInst(UndefValue::get(LI.getType()), |
| 8226 | Constant::getNullValue(Op->getType()), &LI); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8227 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8228 | } |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 8229 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8230 | // Instcombine load (constant global) into the value loaded. |
| 8231 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op)) |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 8232 | if (GV->isConstant() && !GV->isDeclaration()) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8233 | return ReplaceInstUsesWith(LI, GV->getInitializer()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8234 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8235 | // Instcombine load (constantexpr_GEP global, 0, ...) into the value loaded. |
| 8236 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op)) |
| 8237 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 8238 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0))) |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 8239 | if (GV->isConstant() && !GV->isDeclaration()) |
Chris Lattner | 363f2a2 | 2005-09-26 05:28:06 +0000 | [diff] [blame] | 8240 | if (Constant *V = |
| 8241 | ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE)) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8242 | return ReplaceInstUsesWith(LI, V); |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 8243 | if (CE->getOperand(0)->isNullValue()) { |
| 8244 | // Insert a new store to null instruction before the load to indicate |
| 8245 | // that this code is not reachable. We do this instead of inserting |
| 8246 | // an unreachable instruction directly because we cannot modify the |
| 8247 | // CFG. |
| 8248 | new StoreInst(UndefValue::get(LI.getType()), |
| 8249 | Constant::getNullValue(Op->getType()), &LI); |
| 8250 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
| 8251 | } |
| 8252 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8253 | } else if (CE->isCast()) { |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8254 | if (Instruction *Res = InstCombineLoadCast(*this, LI)) |
| 8255 | return Res; |
| 8256 | } |
| 8257 | } |
Chris Lattner | f499eac | 2004-04-08 20:39:49 +0000 | [diff] [blame] | 8258 | |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 8259 | if (Op->hasOneUse()) { |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 8260 | // Change select and PHI nodes to select values instead of addresses: this |
| 8261 | // helps alias analysis out a lot, allows many others simplifications, and |
| 8262 | // exposes redundancy in the code. |
| 8263 | // |
| 8264 | // Note that we cannot do the transformation unless we know that the |
| 8265 | // introduced loads cannot trap! Something like this is valid as long as |
| 8266 | // the condition is always false: load (select bool %C, int* null, int* %G), |
| 8267 | // but it would not be valid if we transformed it to load from null |
| 8268 | // unconditionally. |
| 8269 | // |
| 8270 | if (SelectInst *SI = dyn_cast<SelectInst>(Op)) { |
| 8271 | // load (select (Cond, &V1, &V2)) --> select(Cond, load &V1, load &V2). |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 8272 | if (isSafeToLoadUnconditionally(SI->getOperand(1), SI) && |
| 8273 | isSafeToLoadUnconditionally(SI->getOperand(2), SI)) { |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 8274 | Value *V1 = InsertNewInstBefore(new LoadInst(SI->getOperand(1), |
Chris Lattner | 79f0c8e | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 8275 | SI->getOperand(1)->getName()+".val"), LI); |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 8276 | Value *V2 = InsertNewInstBefore(new LoadInst(SI->getOperand(2), |
Chris Lattner | 79f0c8e | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 8277 | SI->getOperand(2)->getName()+".val"), LI); |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 8278 | return new SelectInst(SI->getCondition(), V1, V2); |
| 8279 | } |
| 8280 | |
Chris Lattner | 684fe21 | 2004-09-23 15:46:00 +0000 | [diff] [blame] | 8281 | // load (select (cond, null, P)) -> load P |
| 8282 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(1))) |
| 8283 | if (C->isNullValue()) { |
| 8284 | LI.setOperand(0, SI->getOperand(2)); |
| 8285 | return &LI; |
| 8286 | } |
| 8287 | |
| 8288 | // load (select (cond, P, null)) -> load P |
| 8289 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(2))) |
| 8290 | if (C->isNullValue()) { |
| 8291 | LI.setOperand(0, SI->getOperand(1)); |
| 8292 | return &LI; |
| 8293 | } |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 8294 | } |
| 8295 | } |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 8296 | return 0; |
| 8297 | } |
| 8298 | |
Reid Spencer | 55af2b5 | 2007-01-19 21:20:31 +0000 | [diff] [blame] | 8299 | /// InstCombineStoreToCast - Fold store V, (cast P) -> store (cast V), P |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8300 | /// when possible. |
| 8301 | static Instruction *InstCombineStoreToCast(InstCombiner &IC, StoreInst &SI) { |
| 8302 | User *CI = cast<User>(SI.getOperand(1)); |
| 8303 | Value *CastOp = CI->getOperand(0); |
| 8304 | |
| 8305 | const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType(); |
| 8306 | if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) { |
| 8307 | const Type *SrcPTy = SrcTy->getElementType(); |
| 8308 | |
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 8309 | if (DestPTy->isInteger() || isa<PointerType>(DestPTy)) { |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8310 | // If the source is an array, the code below will not succeed. Check to |
| 8311 | // see if a trivial 'gep P, 0, 0' will help matters. Only do this for |
| 8312 | // constants. |
| 8313 | if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy)) |
| 8314 | if (Constant *CSrc = dyn_cast<Constant>(CastOp)) |
| 8315 | if (ASrcTy->getNumElements() != 0) { |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 8316 | Value* Idxs[2]; |
| 8317 | Idxs[0] = Idxs[1] = Constant::getNullValue(Type::Int32Ty); |
| 8318 | CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs, 2); |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8319 | SrcTy = cast<PointerType>(CastOp->getType()); |
| 8320 | SrcPTy = SrcTy->getElementType(); |
| 8321 | } |
| 8322 | |
Reid Spencer | 67f827c | 2007-01-20 23:35:48 +0000 | [diff] [blame] | 8323 | if ((SrcPTy->isInteger() || isa<PointerType>(SrcPTy)) && |
| 8324 | IC.getTargetData().getTypeSizeInBits(SrcPTy) == |
| 8325 | IC.getTargetData().getTypeSizeInBits(DestPTy)) { |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8326 | |
| 8327 | // 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] | 8328 | // the same size. Instead of casting the pointer before |
| 8329 | // the store, cast the value to be stored. |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8330 | Value *NewCast; |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 8331 | Value *SIOp0 = SI.getOperand(0); |
Reid Spencer | 7515396 | 2007-01-18 18:54:33 +0000 | [diff] [blame] | 8332 | Instruction::CastOps opcode = Instruction::BitCast; |
| 8333 | const Type* CastSrcTy = SIOp0->getType(); |
| 8334 | const Type* CastDstTy = SrcPTy; |
| 8335 | if (isa<PointerType>(CastDstTy)) { |
| 8336 | if (CastSrcTy->isInteger()) |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 8337 | opcode = Instruction::IntToPtr; |
Reid Spencer | 67f827c | 2007-01-20 23:35:48 +0000 | [diff] [blame] | 8338 | } else if (isa<IntegerType>(CastDstTy)) { |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 8339 | if (isa<PointerType>(SIOp0->getType())) |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 8340 | opcode = Instruction::PtrToInt; |
| 8341 | } |
| 8342 | if (Constant *C = dyn_cast<Constant>(SIOp0)) |
Reid Spencer | 7515396 | 2007-01-18 18:54:33 +0000 | [diff] [blame] | 8343 | NewCast = ConstantExpr::getCast(opcode, C, CastDstTy); |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8344 | else |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8345 | NewCast = IC.InsertNewInstBefore( |
Reid Spencer | 7515396 | 2007-01-18 18:54:33 +0000 | [diff] [blame] | 8346 | CastInst::create(opcode, SIOp0, CastDstTy, SIOp0->getName()+".c"), |
| 8347 | SI); |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8348 | return new StoreInst(NewCast, CastOp); |
| 8349 | } |
| 8350 | } |
| 8351 | } |
| 8352 | return 0; |
| 8353 | } |
| 8354 | |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 8355 | Instruction *InstCombiner::visitStoreInst(StoreInst &SI) { |
| 8356 | Value *Val = SI.getOperand(0); |
| 8357 | Value *Ptr = SI.getOperand(1); |
| 8358 | |
| 8359 | if (isa<UndefValue>(Ptr)) { // store X, undef -> noop (even if volatile) |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 8360 | EraseInstFromFunction(SI); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 8361 | ++NumCombined; |
| 8362 | return 0; |
| 8363 | } |
Chris Lattner | 836692d | 2007-01-15 06:51:56 +0000 | [diff] [blame] | 8364 | |
| 8365 | // If the RHS is an alloca with a single use, zapify the store, making the |
| 8366 | // alloca dead. |
| 8367 | if (Ptr->hasOneUse()) { |
| 8368 | if (isa<AllocaInst>(Ptr)) { |
| 8369 | EraseInstFromFunction(SI); |
| 8370 | ++NumCombined; |
| 8371 | return 0; |
| 8372 | } |
| 8373 | |
| 8374 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) |
| 8375 | if (isa<AllocaInst>(GEP->getOperand(0)) && |
| 8376 | GEP->getOperand(0)->hasOneUse()) { |
| 8377 | EraseInstFromFunction(SI); |
| 8378 | ++NumCombined; |
| 8379 | return 0; |
| 8380 | } |
| 8381 | } |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 8382 | |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 8383 | // Do really simple DSE, to catch cases where there are several consequtive |
| 8384 | // stores to the same location, separated by a few arithmetic operations. This |
| 8385 | // situation often occurs with bitfield accesses. |
| 8386 | BasicBlock::iterator BBI = &SI; |
| 8387 | for (unsigned ScanInsts = 6; BBI != SI.getParent()->begin() && ScanInsts; |
| 8388 | --ScanInsts) { |
| 8389 | --BBI; |
| 8390 | |
| 8391 | if (StoreInst *PrevSI = dyn_cast<StoreInst>(BBI)) { |
| 8392 | // Prev store isn't volatile, and stores to the same location? |
| 8393 | if (!PrevSI->isVolatile() && PrevSI->getOperand(1) == SI.getOperand(1)) { |
| 8394 | ++NumDeadStore; |
| 8395 | ++BBI; |
| 8396 | EraseInstFromFunction(*PrevSI); |
| 8397 | continue; |
| 8398 | } |
| 8399 | break; |
| 8400 | } |
| 8401 | |
Chris Lattner | b4db97f | 2006-05-26 19:19:20 +0000 | [diff] [blame] | 8402 | // If this is a load, we have to stop. However, if the loaded value is from |
| 8403 | // the pointer we're loading and is producing the pointer we're storing, |
| 8404 | // then *this* store is dead (X = load P; store X -> P). |
| 8405 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { |
| 8406 | if (LI == Val && LI->getOperand(0) == Ptr) { |
| 8407 | EraseInstFromFunction(SI); |
| 8408 | ++NumCombined; |
| 8409 | return 0; |
| 8410 | } |
| 8411 | // Otherwise, this is a load from some other location. Stores before it |
| 8412 | // may not be dead. |
| 8413 | break; |
| 8414 | } |
| 8415 | |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 8416 | // Don't skip over loads or things that can modify memory. |
Chris Lattner | b4db97f | 2006-05-26 19:19:20 +0000 | [diff] [blame] | 8417 | if (BBI->mayWriteToMemory()) |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 8418 | break; |
| 8419 | } |
| 8420 | |
| 8421 | |
| 8422 | if (SI.isVolatile()) return 0; // Don't hack volatile stores. |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 8423 | |
| 8424 | // store X, null -> turns into 'unreachable' in SimplifyCFG |
| 8425 | if (isa<ConstantPointerNull>(Ptr)) { |
| 8426 | if (!isa<UndefValue>(Val)) { |
| 8427 | SI.setOperand(0, UndefValue::get(Val->getType())); |
| 8428 | if (Instruction *U = dyn_cast<Instruction>(Val)) |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 8429 | AddToWorkList(U); // Dropped a use. |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 8430 | ++NumCombined; |
| 8431 | } |
| 8432 | return 0; // Do not modify these! |
| 8433 | } |
| 8434 | |
| 8435 | // store undef, Ptr -> noop |
| 8436 | if (isa<UndefValue>(Val)) { |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 8437 | EraseInstFromFunction(SI); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 8438 | ++NumCombined; |
| 8439 | return 0; |
| 8440 | } |
| 8441 | |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8442 | // If the pointer destination is a cast, see if we can fold the cast into the |
| 8443 | // source instead. |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 8444 | if (isa<CastInst>(Ptr)) |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8445 | if (Instruction *Res = InstCombineStoreToCast(*this, SI)) |
| 8446 | return Res; |
| 8447 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8448 | if (CE->isCast()) |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8449 | if (Instruction *Res = InstCombineStoreToCast(*this, SI)) |
| 8450 | return Res; |
| 8451 | |
Chris Lattner | 408902b | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 8452 | |
| 8453 | // If this store is the last instruction in the basic block, and if the block |
| 8454 | // 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] | 8455 | BBI = &SI; ++BBI; |
Chris Lattner | 408902b | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 8456 | if (BranchInst *BI = dyn_cast<BranchInst>(BBI)) |
| 8457 | if (BI->isUnconditional()) { |
| 8458 | // Check to see if the successor block has exactly two incoming edges. If |
| 8459 | // so, see if the other predecessor contains a store to the same location. |
| 8460 | // if so, insert a PHI node (if needed) and move the stores down. |
| 8461 | BasicBlock *Dest = BI->getSuccessor(0); |
| 8462 | |
| 8463 | pred_iterator PI = pred_begin(Dest); |
| 8464 | BasicBlock *Other = 0; |
| 8465 | if (*PI != BI->getParent()) |
| 8466 | Other = *PI; |
| 8467 | ++PI; |
| 8468 | if (PI != pred_end(Dest)) { |
| 8469 | if (*PI != BI->getParent()) |
| 8470 | if (Other) |
| 8471 | Other = 0; |
| 8472 | else |
| 8473 | Other = *PI; |
| 8474 | if (++PI != pred_end(Dest)) |
| 8475 | Other = 0; |
| 8476 | } |
| 8477 | if (Other) { // If only one other pred... |
| 8478 | BBI = Other->getTerminator(); |
| 8479 | // Make sure this other block ends in an unconditional branch and that |
| 8480 | // there is an instruction before the branch. |
| 8481 | if (isa<BranchInst>(BBI) && cast<BranchInst>(BBI)->isUnconditional() && |
| 8482 | BBI != Other->begin()) { |
| 8483 | --BBI; |
| 8484 | StoreInst *OtherStore = dyn_cast<StoreInst>(BBI); |
| 8485 | |
| 8486 | // If this instruction is a store to the same location. |
| 8487 | if (OtherStore && OtherStore->getOperand(1) == SI.getOperand(1)) { |
| 8488 | // Okay, we know we can perform this transformation. Insert a PHI |
| 8489 | // node now if we need it. |
| 8490 | Value *MergedVal = OtherStore->getOperand(0); |
| 8491 | if (MergedVal != SI.getOperand(0)) { |
| 8492 | PHINode *PN = new PHINode(MergedVal->getType(), "storemerge"); |
| 8493 | PN->reserveOperandSpace(2); |
| 8494 | PN->addIncoming(SI.getOperand(0), SI.getParent()); |
| 8495 | PN->addIncoming(OtherStore->getOperand(0), Other); |
| 8496 | MergedVal = InsertNewInstBefore(PN, Dest->front()); |
| 8497 | } |
| 8498 | |
| 8499 | // Advance to a place where it is safe to insert the new store and |
| 8500 | // insert it. |
| 8501 | BBI = Dest->begin(); |
| 8502 | while (isa<PHINode>(BBI)) ++BBI; |
| 8503 | InsertNewInstBefore(new StoreInst(MergedVal, SI.getOperand(1), |
| 8504 | OtherStore->isVolatile()), *BBI); |
| 8505 | |
| 8506 | // Nuke the old stores. |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 8507 | EraseInstFromFunction(SI); |
| 8508 | EraseInstFromFunction(*OtherStore); |
Chris Lattner | 408902b | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 8509 | ++NumCombined; |
| 8510 | return 0; |
| 8511 | } |
| 8512 | } |
| 8513 | } |
| 8514 | } |
| 8515 | |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 8516 | return 0; |
| 8517 | } |
| 8518 | |
| 8519 | |
Chris Lattner | c4d10eb | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 8520 | Instruction *InstCombiner::visitBranchInst(BranchInst &BI) { |
| 8521 | // 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] | 8522 | Value *X = 0; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 8523 | BasicBlock *TrueDest; |
| 8524 | BasicBlock *FalseDest; |
| 8525 | if (match(&BI, m_Br(m_Not(m_Value(X)), TrueDest, FalseDest)) && |
| 8526 | !isa<Constant>(X)) { |
| 8527 | // Swap Destinations and condition... |
| 8528 | BI.setCondition(X); |
| 8529 | BI.setSuccessor(0, FalseDest); |
| 8530 | BI.setSuccessor(1, TrueDest); |
| 8531 | return &BI; |
| 8532 | } |
| 8533 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8534 | // Cannonicalize fcmp_one -> fcmp_oeq |
| 8535 | FCmpInst::Predicate FPred; Value *Y; |
| 8536 | if (match(&BI, m_Br(m_FCmp(FPred, m_Value(X), m_Value(Y)), |
| 8537 | TrueDest, FalseDest))) |
| 8538 | if ((FPred == FCmpInst::FCMP_ONE || FPred == FCmpInst::FCMP_OLE || |
| 8539 | FPred == FCmpInst::FCMP_OGE) && BI.getCondition()->hasOneUse()) { |
| 8540 | FCmpInst *I = cast<FCmpInst>(BI.getCondition()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8541 | FCmpInst::Predicate NewPred = FCmpInst::getInversePredicate(FPred); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 8542 | Instruction *NewSCC = new FCmpInst(NewPred, X, Y, "", I); |
| 8543 | NewSCC->takeName(I); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8544 | // Swap Destinations and condition... |
| 8545 | BI.setCondition(NewSCC); |
| 8546 | BI.setSuccessor(0, FalseDest); |
| 8547 | BI.setSuccessor(1, TrueDest); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 8548 | RemoveFromWorkList(I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 8549 | I->eraseFromParent(); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 8550 | AddToWorkList(NewSCC); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8551 | return &BI; |
| 8552 | } |
| 8553 | |
| 8554 | // Cannonicalize icmp_ne -> icmp_eq |
| 8555 | ICmpInst::Predicate IPred; |
| 8556 | if (match(&BI, m_Br(m_ICmp(IPred, m_Value(X), m_Value(Y)), |
| 8557 | TrueDest, FalseDest))) |
| 8558 | if ((IPred == ICmpInst::ICMP_NE || IPred == ICmpInst::ICMP_ULE || |
| 8559 | IPred == ICmpInst::ICMP_SLE || IPred == ICmpInst::ICMP_UGE || |
| 8560 | IPred == ICmpInst::ICMP_SGE) && BI.getCondition()->hasOneUse()) { |
| 8561 | ICmpInst *I = cast<ICmpInst>(BI.getCondition()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8562 | ICmpInst::Predicate NewPred = ICmpInst::getInversePredicate(IPred); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 8563 | Instruction *NewSCC = new ICmpInst(NewPred, X, Y, "", I); |
| 8564 | NewSCC->takeName(I); |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 8565 | // Swap Destinations and condition... |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 8566 | BI.setCondition(NewSCC); |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 8567 | BI.setSuccessor(0, FalseDest); |
| 8568 | BI.setSuccessor(1, TrueDest); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 8569 | RemoveFromWorkList(I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 8570 | I->eraseFromParent();; |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 8571 | AddToWorkList(NewSCC); |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 8572 | return &BI; |
| 8573 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8574 | |
Chris Lattner | c4d10eb | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 8575 | return 0; |
| 8576 | } |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 8577 | |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 8578 | Instruction *InstCombiner::visitSwitchInst(SwitchInst &SI) { |
| 8579 | Value *Cond = SI.getCondition(); |
| 8580 | if (Instruction *I = dyn_cast<Instruction>(Cond)) { |
| 8581 | if (I->getOpcode() == Instruction::Add) |
| 8582 | if (ConstantInt *AddRHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 8583 | // change 'switch (X+4) case 1:' into 'switch (X) case -3' |
| 8584 | for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8585 | SI.setOperand(i,ConstantExpr::getSub(cast<Constant>(SI.getOperand(i)), |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 8586 | AddRHS)); |
| 8587 | SI.setOperand(0, I->getOperand(0)); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 8588 | AddToWorkList(I); |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 8589 | return &SI; |
| 8590 | } |
| 8591 | } |
| 8592 | return 0; |
| 8593 | } |
| 8594 | |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 8595 | /// CheapToScalarize - Return true if the value is cheaper to scalarize than it |
| 8596 | /// is to leave as a vector operation. |
| 8597 | static bool CheapToScalarize(Value *V, bool isConstant) { |
| 8598 | if (isa<ConstantAggregateZero>(V)) |
| 8599 | return true; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 8600 | if (ConstantVector *C = dyn_cast<ConstantVector>(V)) { |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 8601 | if (isConstant) return true; |
| 8602 | // If all elts are the same, we can extract. |
| 8603 | Constant *Op0 = C->getOperand(0); |
| 8604 | for (unsigned i = 1; i < C->getNumOperands(); ++i) |
| 8605 | if (C->getOperand(i) != Op0) |
| 8606 | return false; |
| 8607 | return true; |
| 8608 | } |
| 8609 | Instruction *I = dyn_cast<Instruction>(V); |
| 8610 | if (!I) return false; |
| 8611 | |
| 8612 | // Insert element gets simplified to the inserted element or is deleted if |
| 8613 | // this is constant idx extract element and its a constant idx insertelt. |
| 8614 | if (I->getOpcode() == Instruction::InsertElement && isConstant && |
| 8615 | isa<ConstantInt>(I->getOperand(2))) |
| 8616 | return true; |
| 8617 | if (I->getOpcode() == Instruction::Load && I->hasOneUse()) |
| 8618 | return true; |
| 8619 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) |
| 8620 | if (BO->hasOneUse() && |
| 8621 | (CheapToScalarize(BO->getOperand(0), isConstant) || |
| 8622 | CheapToScalarize(BO->getOperand(1), isConstant))) |
| 8623 | return true; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8624 | if (CmpInst *CI = dyn_cast<CmpInst>(I)) |
| 8625 | if (CI->hasOneUse() && |
| 8626 | (CheapToScalarize(CI->getOperand(0), isConstant) || |
| 8627 | CheapToScalarize(CI->getOperand(1), isConstant))) |
| 8628 | return true; |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 8629 | |
| 8630 | return false; |
| 8631 | } |
| 8632 | |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 8633 | /// Read and decode a shufflevector mask. |
| 8634 | /// |
| 8635 | /// It turns undef elements into values that are larger than the number of |
| 8636 | /// elements in the input. |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8637 | static std::vector<unsigned> getShuffleMask(const ShuffleVectorInst *SVI) { |
| 8638 | unsigned NElts = SVI->getType()->getNumElements(); |
| 8639 | if (isa<ConstantAggregateZero>(SVI->getOperand(2))) |
| 8640 | return std::vector<unsigned>(NElts, 0); |
| 8641 | if (isa<UndefValue>(SVI->getOperand(2))) |
| 8642 | return std::vector<unsigned>(NElts, 2*NElts); |
| 8643 | |
| 8644 | std::vector<unsigned> Result; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 8645 | const ConstantVector *CP = cast<ConstantVector>(SVI->getOperand(2)); |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8646 | for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) |
| 8647 | if (isa<UndefValue>(CP->getOperand(i))) |
| 8648 | Result.push_back(NElts*2); // undef -> 8 |
| 8649 | else |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8650 | Result.push_back(cast<ConstantInt>(CP->getOperand(i))->getZExtValue()); |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8651 | return Result; |
| 8652 | } |
| 8653 | |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8654 | /// FindScalarElement - Given a vector and an element number, see if the scalar |
| 8655 | /// value is already around as a register, for example if it were inserted then |
| 8656 | /// extracted from the vector. |
| 8657 | static Value *FindScalarElement(Value *V, unsigned EltNo) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 8658 | assert(isa<VectorType>(V->getType()) && "Not looking at a vector?"); |
| 8659 | const VectorType *PTy = cast<VectorType>(V->getType()); |
Chris Lattner | 389a6f5 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 8660 | unsigned Width = PTy->getNumElements(); |
| 8661 | if (EltNo >= Width) // Out of range access. |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8662 | return UndefValue::get(PTy->getElementType()); |
| 8663 | |
| 8664 | if (isa<UndefValue>(V)) |
| 8665 | return UndefValue::get(PTy->getElementType()); |
| 8666 | else if (isa<ConstantAggregateZero>(V)) |
| 8667 | return Constant::getNullValue(PTy->getElementType()); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 8668 | else if (ConstantVector *CP = dyn_cast<ConstantVector>(V)) |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8669 | return CP->getOperand(EltNo); |
| 8670 | else if (InsertElementInst *III = dyn_cast<InsertElementInst>(V)) { |
| 8671 | // 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] | 8672 | if (!isa<ConstantInt>(III->getOperand(2))) |
| 8673 | return 0; |
| 8674 | unsigned IIElt = cast<ConstantInt>(III->getOperand(2))->getZExtValue(); |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8675 | |
| 8676 | // If this is an insert to the element we are looking for, return the |
| 8677 | // inserted value. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8678 | if (EltNo == IIElt) |
| 8679 | return III->getOperand(1); |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8680 | |
| 8681 | // Otherwise, the insertelement doesn't modify the value, recurse on its |
| 8682 | // vector input. |
| 8683 | return FindScalarElement(III->getOperand(0), EltNo); |
Chris Lattner | 389a6f5 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 8684 | } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(V)) { |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8685 | unsigned InEl = getShuffleMask(SVI)[EltNo]; |
| 8686 | if (InEl < Width) |
| 8687 | return FindScalarElement(SVI->getOperand(0), InEl); |
| 8688 | else if (InEl < Width*2) |
| 8689 | return FindScalarElement(SVI->getOperand(1), InEl - Width); |
| 8690 | else |
| 8691 | return UndefValue::get(PTy->getElementType()); |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8692 | } |
| 8693 | |
| 8694 | // Otherwise, we don't know. |
| 8695 | return 0; |
| 8696 | } |
| 8697 | |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 8698 | Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) { |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8699 | |
Chris Lattner | 1f13c88 | 2006-03-31 18:25:14 +0000 | [diff] [blame] | 8700 | // If packed val is undef, replace extract with scalar undef. |
| 8701 | if (isa<UndefValue>(EI.getOperand(0))) |
| 8702 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); |
| 8703 | |
| 8704 | // If packed val is constant 0, replace extract with scalar 0. |
| 8705 | if (isa<ConstantAggregateZero>(EI.getOperand(0))) |
| 8706 | return ReplaceInstUsesWith(EI, Constant::getNullValue(EI.getType())); |
| 8707 | |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 8708 | if (ConstantVector *C = dyn_cast<ConstantVector>(EI.getOperand(0))) { |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 8709 | // If packed val is constant with uniform operands, replace EI |
| 8710 | // with that operand |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 8711 | Constant *op0 = C->getOperand(0); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 8712 | for (unsigned i = 1; i < C->getNumOperands(); ++i) |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 8713 | if (C->getOperand(i) != op0) { |
| 8714 | op0 = 0; |
| 8715 | break; |
| 8716 | } |
| 8717 | if (op0) |
| 8718 | return ReplaceInstUsesWith(EI, op0); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 8719 | } |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 8720 | |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8721 | // If extracting a specified index from the vector, see if we can recursively |
| 8722 | // find a previously computed scalar that was inserted into the vector. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8723 | if (ConstantInt *IdxC = dyn_cast<ConstantInt>(EI.getOperand(1))) { |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 8724 | // This instruction only demands the single element from the input vector. |
| 8725 | // If the input vector has a single use, simplify it based on this use |
| 8726 | // property. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8727 | uint64_t IndexVal = IdxC->getZExtValue(); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 8728 | if (EI.getOperand(0)->hasOneUse()) { |
| 8729 | uint64_t UndefElts; |
| 8730 | if (Value *V = SimplifyDemandedVectorElts(EI.getOperand(0), |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8731 | 1 << IndexVal, |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 8732 | UndefElts)) { |
| 8733 | EI.setOperand(0, V); |
| 8734 | return &EI; |
| 8735 | } |
| 8736 | } |
| 8737 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8738 | if (Value *Elt = FindScalarElement(EI.getOperand(0), IndexVal)) |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8739 | return ReplaceInstUsesWith(EI, Elt); |
Chris Lattner | 389a6f5 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 8740 | } |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8741 | |
Chris Lattner | 73fa49d | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 8742 | if (Instruction *I = dyn_cast<Instruction>(EI.getOperand(0))) { |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 8743 | if (I->hasOneUse()) { |
| 8744 | // Push extractelement into predecessor operation if legal and |
| 8745 | // profitable to do so |
| 8746 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) { |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 8747 | bool isConstantElt = isa<ConstantInt>(EI.getOperand(1)); |
| 8748 | if (CheapToScalarize(BO, isConstantElt)) { |
| 8749 | ExtractElementInst *newEI0 = |
| 8750 | new ExtractElementInst(BO->getOperand(0), EI.getOperand(1), |
| 8751 | EI.getName()+".lhs"); |
| 8752 | ExtractElementInst *newEI1 = |
| 8753 | new ExtractElementInst(BO->getOperand(1), EI.getOperand(1), |
| 8754 | EI.getName()+".rhs"); |
| 8755 | InsertNewInstBefore(newEI0, EI); |
| 8756 | InsertNewInstBefore(newEI1, EI); |
| 8757 | return BinaryOperator::create(BO->getOpcode(), newEI0, newEI1); |
| 8758 | } |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 8759 | } else if (isa<LoadInst>(I)) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 8760 | Value *Ptr = InsertCastBefore(Instruction::BitCast, I->getOperand(0), |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 8761 | PointerType::get(EI.getType()), EI); |
| 8762 | GetElementPtrInst *GEP = |
Reid Spencer | de33124 | 2006-11-29 01:11:01 +0000 | [diff] [blame] | 8763 | new GetElementPtrInst(Ptr, EI.getOperand(1), I->getName() + ".gep"); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 8764 | InsertNewInstBefore(GEP, EI); |
| 8765 | return new LoadInst(GEP); |
Chris Lattner | 73fa49d | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 8766 | } |
| 8767 | } |
| 8768 | if (InsertElementInst *IE = dyn_cast<InsertElementInst>(I)) { |
| 8769 | // Extracting the inserted element? |
| 8770 | if (IE->getOperand(2) == EI.getOperand(1)) |
| 8771 | return ReplaceInstUsesWith(EI, IE->getOperand(1)); |
| 8772 | // If the inserted and extracted elements are constants, they must not |
| 8773 | // be the same value, extract from the pre-inserted value instead. |
| 8774 | if (isa<Constant>(IE->getOperand(2)) && |
| 8775 | isa<Constant>(EI.getOperand(1))) { |
| 8776 | AddUsesToWorkList(EI); |
| 8777 | EI.setOperand(0, IE->getOperand(0)); |
| 8778 | return &EI; |
| 8779 | } |
| 8780 | } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(I)) { |
| 8781 | // If this is extracting an element from a shufflevector, figure out where |
| 8782 | // it came from and extract from the appropriate input element instead. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8783 | if (ConstantInt *Elt = dyn_cast<ConstantInt>(EI.getOperand(1))) { |
| 8784 | unsigned SrcIdx = getShuffleMask(SVI)[Elt->getZExtValue()]; |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8785 | Value *Src; |
| 8786 | if (SrcIdx < SVI->getType()->getNumElements()) |
| 8787 | Src = SVI->getOperand(0); |
| 8788 | else if (SrcIdx < SVI->getType()->getNumElements()*2) { |
| 8789 | SrcIdx -= SVI->getType()->getNumElements(); |
| 8790 | Src = SVI->getOperand(1); |
| 8791 | } else { |
| 8792 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); |
Chris Lattner | df084ff | 2006-03-30 22:02:40 +0000 | [diff] [blame] | 8793 | } |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 8794 | return new ExtractElementInst(Src, SrcIdx); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 8795 | } |
| 8796 | } |
Chris Lattner | 73fa49d | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 8797 | } |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 8798 | return 0; |
| 8799 | } |
| 8800 | |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8801 | /// CollectSingleShuffleElements - If V is a shuffle of values that ONLY returns |
| 8802 | /// elements from either LHS or RHS, return the shuffle mask and true. |
| 8803 | /// Otherwise, return false. |
| 8804 | static bool CollectSingleShuffleElements(Value *V, Value *LHS, Value *RHS, |
| 8805 | std::vector<Constant*> &Mask) { |
| 8806 | assert(V->getType() == LHS->getType() && V->getType() == RHS->getType() && |
| 8807 | "Invalid CollectSingleShuffleElements"); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 8808 | unsigned NumElts = cast<VectorType>(V->getType())->getNumElements(); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8809 | |
| 8810 | if (isa<UndefValue>(V)) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8811 | Mask.assign(NumElts, UndefValue::get(Type::Int32Ty)); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8812 | return true; |
| 8813 | } else if (V == LHS) { |
| 8814 | for (unsigned i = 0; i != NumElts; ++i) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8815 | Mask.push_back(ConstantInt::get(Type::Int32Ty, i)); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8816 | return true; |
| 8817 | } else if (V == RHS) { |
| 8818 | for (unsigned i = 0; i != NumElts; ++i) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8819 | Mask.push_back(ConstantInt::get(Type::Int32Ty, i+NumElts)); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8820 | return true; |
| 8821 | } else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) { |
| 8822 | // If this is an insert of an extract from some other vector, include it. |
| 8823 | Value *VecOp = IEI->getOperand(0); |
| 8824 | Value *ScalarOp = IEI->getOperand(1); |
| 8825 | Value *IdxOp = IEI->getOperand(2); |
| 8826 | |
Chris Lattner | d929f06 | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 8827 | if (!isa<ConstantInt>(IdxOp)) |
| 8828 | return false; |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8829 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Chris Lattner | d929f06 | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 8830 | |
| 8831 | if (isa<UndefValue>(ScalarOp)) { // inserting undef into vector. |
| 8832 | // Okay, we can handle this if the vector we are insertinting into is |
| 8833 | // transitively ok. |
| 8834 | if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) { |
| 8835 | // If so, update the mask to reflect the inserted undef. |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8836 | Mask[InsertedIdx] = UndefValue::get(Type::Int32Ty); |
Chris Lattner | d929f06 | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 8837 | return true; |
| 8838 | } |
| 8839 | } else if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)){ |
| 8840 | if (isa<ConstantInt>(EI->getOperand(1)) && |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8841 | EI->getOperand(0)->getType() == V->getType()) { |
| 8842 | unsigned ExtractedIdx = |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8843 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8844 | |
| 8845 | // This must be extracting from either LHS or RHS. |
| 8846 | if (EI->getOperand(0) == LHS || EI->getOperand(0) == RHS) { |
| 8847 | // Okay, we can handle this if the vector we are insertinting into is |
| 8848 | // transitively ok. |
| 8849 | if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) { |
| 8850 | // If so, update the mask to reflect the inserted value. |
| 8851 | if (EI->getOperand(0) == LHS) { |
| 8852 | Mask[InsertedIdx & (NumElts-1)] = |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8853 | ConstantInt::get(Type::Int32Ty, ExtractedIdx); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8854 | } else { |
| 8855 | assert(EI->getOperand(0) == RHS); |
| 8856 | Mask[InsertedIdx & (NumElts-1)] = |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8857 | ConstantInt::get(Type::Int32Ty, ExtractedIdx+NumElts); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8858 | |
| 8859 | } |
| 8860 | return true; |
| 8861 | } |
| 8862 | } |
| 8863 | } |
| 8864 | } |
| 8865 | } |
| 8866 | // TODO: Handle shufflevector here! |
| 8867 | |
| 8868 | return false; |
| 8869 | } |
| 8870 | |
| 8871 | /// CollectShuffleElements - We are building a shuffle of V, using RHS as the |
| 8872 | /// RHS of the shuffle instruction, if it is not null. Return a shuffle mask |
| 8873 | /// that computes V and the LHS value of the shuffle. |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8874 | static Value *CollectShuffleElements(Value *V, std::vector<Constant*> &Mask, |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8875 | Value *&RHS) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 8876 | assert(isa<VectorType>(V->getType()) && |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8877 | (RHS == 0 || V->getType() == RHS->getType()) && |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8878 | "Invalid shuffle!"); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 8879 | unsigned NumElts = cast<VectorType>(V->getType())->getNumElements(); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8880 | |
| 8881 | if (isa<UndefValue>(V)) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8882 | Mask.assign(NumElts, UndefValue::get(Type::Int32Ty)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8883 | return V; |
| 8884 | } else if (isa<ConstantAggregateZero>(V)) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8885 | Mask.assign(NumElts, ConstantInt::get(Type::Int32Ty, 0)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8886 | return V; |
| 8887 | } else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) { |
| 8888 | // If this is an insert of an extract from some other vector, include it. |
| 8889 | Value *VecOp = IEI->getOperand(0); |
| 8890 | Value *ScalarOp = IEI->getOperand(1); |
| 8891 | Value *IdxOp = IEI->getOperand(2); |
| 8892 | |
| 8893 | if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) { |
| 8894 | if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) && |
| 8895 | EI->getOperand(0)->getType() == V->getType()) { |
| 8896 | unsigned ExtractedIdx = |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8897 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
| 8898 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8899 | |
| 8900 | // Either the extracted from or inserted into vector must be RHSVec, |
| 8901 | // otherwise we'd end up with a shuffle of three inputs. |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8902 | if (EI->getOperand(0) == RHS || RHS == 0) { |
| 8903 | RHS = EI->getOperand(0); |
| 8904 | Value *V = CollectShuffleElements(VecOp, Mask, RHS); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8905 | Mask[InsertedIdx & (NumElts-1)] = |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8906 | ConstantInt::get(Type::Int32Ty, NumElts+ExtractedIdx); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8907 | return V; |
| 8908 | } |
| 8909 | |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8910 | if (VecOp == RHS) { |
| 8911 | Value *V = CollectShuffleElements(EI->getOperand(0), Mask, RHS); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8912 | // Everything but the extracted element is replaced with the RHS. |
| 8913 | for (unsigned i = 0; i != NumElts; ++i) { |
| 8914 | if (i != InsertedIdx) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8915 | Mask[i] = ConstantInt::get(Type::Int32Ty, NumElts+i); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8916 | } |
| 8917 | return V; |
| 8918 | } |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8919 | |
| 8920 | // If this insertelement is a chain that comes from exactly these two |
| 8921 | // vectors, return the vector and the effective shuffle. |
| 8922 | if (CollectSingleShuffleElements(IEI, EI->getOperand(0), RHS, Mask)) |
| 8923 | return EI->getOperand(0); |
| 8924 | |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8925 | } |
| 8926 | } |
| 8927 | } |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8928 | // TODO: Handle shufflevector here! |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8929 | |
| 8930 | // Otherwise, can't do anything fancy. Return an identity vector. |
| 8931 | for (unsigned i = 0; i != NumElts; ++i) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8932 | Mask.push_back(ConstantInt::get(Type::Int32Ty, i)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8933 | return V; |
| 8934 | } |
| 8935 | |
| 8936 | Instruction *InstCombiner::visitInsertElementInst(InsertElementInst &IE) { |
| 8937 | Value *VecOp = IE.getOperand(0); |
| 8938 | Value *ScalarOp = IE.getOperand(1); |
| 8939 | Value *IdxOp = IE.getOperand(2); |
| 8940 | |
| 8941 | // If the inserted element was extracted from some other vector, and if the |
| 8942 | // indexes are constant, try to turn this into a shufflevector operation. |
| 8943 | if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) { |
| 8944 | if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) && |
| 8945 | EI->getOperand(0)->getType() == IE.getType()) { |
| 8946 | unsigned NumVectorElts = IE.getType()->getNumElements(); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8947 | unsigned ExtractedIdx=cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
| 8948 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8949 | |
| 8950 | if (ExtractedIdx >= NumVectorElts) // Out of range extract. |
| 8951 | return ReplaceInstUsesWith(IE, VecOp); |
| 8952 | |
| 8953 | if (InsertedIdx >= NumVectorElts) // Out of range insert. |
| 8954 | return ReplaceInstUsesWith(IE, UndefValue::get(IE.getType())); |
| 8955 | |
| 8956 | // If we are extracting a value from a vector, then inserting it right |
| 8957 | // back into the same place, just use the input vector. |
| 8958 | if (EI->getOperand(0) == VecOp && ExtractedIdx == InsertedIdx) |
| 8959 | return ReplaceInstUsesWith(IE, VecOp); |
| 8960 | |
| 8961 | // We could theoretically do this for ANY input. However, doing so could |
| 8962 | // turn chains of insertelement instructions into a chain of shufflevector |
| 8963 | // instructions, and right now we do not merge shufflevectors. As such, |
| 8964 | // only do this in a situation where it is clear that there is benefit. |
| 8965 | if (isa<UndefValue>(VecOp) || isa<ConstantAggregateZero>(VecOp)) { |
| 8966 | // Turn this into shuffle(EIOp0, VecOp, Mask). The result has all of |
| 8967 | // the values of VecOp, except then one read from EIOp0. |
| 8968 | // Build a new shuffle mask. |
| 8969 | std::vector<Constant*> Mask; |
| 8970 | if (isa<UndefValue>(VecOp)) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8971 | Mask.assign(NumVectorElts, UndefValue::get(Type::Int32Ty)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8972 | else { |
| 8973 | assert(isa<ConstantAggregateZero>(VecOp) && "Unknown thing"); |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8974 | Mask.assign(NumVectorElts, ConstantInt::get(Type::Int32Ty, |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8975 | NumVectorElts)); |
| 8976 | } |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8977 | Mask[InsertedIdx] = ConstantInt::get(Type::Int32Ty, ExtractedIdx); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8978 | return new ShuffleVectorInst(EI->getOperand(0), VecOp, |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 8979 | ConstantVector::get(Mask)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8980 | } |
| 8981 | |
| 8982 | // If this insertelement isn't used by some other insertelement, turn it |
| 8983 | // (and any insertelements it points to), into one big shuffle. |
| 8984 | if (!IE.hasOneUse() || !isa<InsertElementInst>(IE.use_back())) { |
| 8985 | std::vector<Constant*> Mask; |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8986 | Value *RHS = 0; |
| 8987 | Value *LHS = CollectShuffleElements(&IE, Mask, RHS); |
| 8988 | if (RHS == 0) RHS = UndefValue::get(LHS->getType()); |
| 8989 | // We now have a shuffle of LHS, RHS, Mask. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 8990 | return new ShuffleVectorInst(LHS, RHS, ConstantVector::get(Mask)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8991 | } |
| 8992 | } |
| 8993 | } |
| 8994 | |
| 8995 | return 0; |
| 8996 | } |
| 8997 | |
| 8998 | |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 8999 | Instruction *InstCombiner::visitShuffleVectorInst(ShuffleVectorInst &SVI) { |
| 9000 | Value *LHS = SVI.getOperand(0); |
| 9001 | Value *RHS = SVI.getOperand(1); |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 9002 | std::vector<unsigned> Mask = getShuffleMask(&SVI); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 9003 | |
| 9004 | bool MadeChange = false; |
| 9005 | |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 9006 | // Undefined shuffle mask -> undefined value. |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 9007 | if (isa<UndefValue>(SVI.getOperand(2))) |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 9008 | return ReplaceInstUsesWith(SVI, UndefValue::get(SVI.getType())); |
| 9009 | |
Chris Lattner | e4929dd | 2007-01-05 07:36:08 +0000 | [diff] [blame] | 9010 | // 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] | 9011 | // the undef, change them to undefs. |
Chris Lattner | e4929dd | 2007-01-05 07:36:08 +0000 | [diff] [blame] | 9012 | if (isa<UndefValue>(SVI.getOperand(1))) { |
| 9013 | // Scan to see if there are any references to the RHS. If so, replace them |
| 9014 | // with undef element refs and set MadeChange to true. |
| 9015 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
| 9016 | if (Mask[i] >= e && Mask[i] != 2*e) { |
| 9017 | Mask[i] = 2*e; |
| 9018 | MadeChange = true; |
| 9019 | } |
| 9020 | } |
| 9021 | |
| 9022 | if (MadeChange) { |
| 9023 | // Remap any references to RHS to use LHS. |
| 9024 | std::vector<Constant*> Elts; |
| 9025 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
| 9026 | if (Mask[i] == 2*e) |
| 9027 | Elts.push_back(UndefValue::get(Type::Int32Ty)); |
| 9028 | else |
| 9029 | Elts.push_back(ConstantInt::get(Type::Int32Ty, Mask[i])); |
| 9030 | } |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 9031 | SVI.setOperand(2, ConstantVector::get(Elts)); |
Chris Lattner | e4929dd | 2007-01-05 07:36:08 +0000 | [diff] [blame] | 9032 | } |
| 9033 | } |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 9034 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 9035 | // Canonicalize shuffle(x ,x,mask) -> shuffle(x, undef,mask') |
| 9036 | // Canonicalize shuffle(undef,x,mask) -> shuffle(x, undef,mask'). |
| 9037 | if (LHS == RHS || isa<UndefValue>(LHS)) { |
| 9038 | if (isa<UndefValue>(LHS) && LHS == RHS) { |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 9039 | // shuffle(undef,undef,mask) -> undef. |
| 9040 | return ReplaceInstUsesWith(SVI, LHS); |
| 9041 | } |
| 9042 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 9043 | // Remap any references to RHS to use LHS. |
| 9044 | std::vector<Constant*> Elts; |
| 9045 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 9046 | if (Mask[i] >= 2*e) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9047 | Elts.push_back(UndefValue::get(Type::Int32Ty)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 9048 | else { |
| 9049 | if ((Mask[i] >= e && isa<UndefValue>(RHS)) || |
| 9050 | (Mask[i] < e && isa<UndefValue>(LHS))) |
| 9051 | Mask[i] = 2*e; // Turn into undef. |
| 9052 | else |
| 9053 | Mask[i] &= (e-1); // Force to LHS. |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9054 | Elts.push_back(ConstantInt::get(Type::Int32Ty, Mask[i])); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 9055 | } |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 9056 | } |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 9057 | SVI.setOperand(0, SVI.getOperand(1)); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 9058 | SVI.setOperand(1, UndefValue::get(RHS->getType())); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 9059 | SVI.setOperand(2, ConstantVector::get(Elts)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 9060 | LHS = SVI.getOperand(0); |
| 9061 | RHS = SVI.getOperand(1); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 9062 | MadeChange = true; |
| 9063 | } |
| 9064 | |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 9065 | // Analyze the shuffle, are the LHS or RHS and identity shuffles? |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 9066 | bool isLHSID = true, isRHSID = true; |
Chris Lattner | 706126d | 2006-04-16 00:03:56 +0000 | [diff] [blame] | 9067 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 9068 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
| 9069 | if (Mask[i] >= e*2) continue; // Ignore undef values. |
| 9070 | // Is this an identity shuffle of the LHS value? |
| 9071 | isLHSID &= (Mask[i] == i); |
| 9072 | |
| 9073 | // Is this an identity shuffle of the RHS value? |
| 9074 | isRHSID &= (Mask[i]-e == i); |
Chris Lattner | 706126d | 2006-04-16 00:03:56 +0000 | [diff] [blame] | 9075 | } |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 9076 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 9077 | // Eliminate identity shuffles. |
| 9078 | if (isLHSID) return ReplaceInstUsesWith(SVI, LHS); |
| 9079 | if (isRHSID) return ReplaceInstUsesWith(SVI, RHS); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 9080 | |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 9081 | // If the LHS is a shufflevector itself, see if we can combine it with this |
| 9082 | // one without producing an unusual shuffle. Here we are really conservative: |
| 9083 | // we are absolutely afraid of producing a shuffle mask not in the input |
| 9084 | // program, because the code gen may not be smart enough to turn a merged |
| 9085 | // shuffle into two specific shuffles: it may produce worse code. As such, |
| 9086 | // we only merge two shuffles if the result is one of the two input shuffle |
| 9087 | // masks. In this case, merging the shuffles just removes one instruction, |
| 9088 | // which we know is safe. This is good for things like turning: |
| 9089 | // (splat(splat)) -> splat. |
| 9090 | if (ShuffleVectorInst *LHSSVI = dyn_cast<ShuffleVectorInst>(LHS)) { |
| 9091 | if (isa<UndefValue>(RHS)) { |
| 9092 | std::vector<unsigned> LHSMask = getShuffleMask(LHSSVI); |
| 9093 | |
| 9094 | std::vector<unsigned> NewMask; |
| 9095 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) |
| 9096 | if (Mask[i] >= 2*e) |
| 9097 | NewMask.push_back(2*e); |
| 9098 | else |
| 9099 | NewMask.push_back(LHSMask[Mask[i]]); |
| 9100 | |
| 9101 | // If the result mask is equal to the src shuffle or this shuffle mask, do |
| 9102 | // the replacement. |
| 9103 | if (NewMask == LHSMask || NewMask == Mask) { |
| 9104 | std::vector<Constant*> Elts; |
| 9105 | for (unsigned i = 0, e = NewMask.size(); i != e; ++i) { |
| 9106 | if (NewMask[i] >= e*2) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9107 | Elts.push_back(UndefValue::get(Type::Int32Ty)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 9108 | } else { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9109 | Elts.push_back(ConstantInt::get(Type::Int32Ty, NewMask[i])); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 9110 | } |
| 9111 | } |
| 9112 | return new ShuffleVectorInst(LHSSVI->getOperand(0), |
| 9113 | LHSSVI->getOperand(1), |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 9114 | ConstantVector::get(Elts)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 9115 | } |
| 9116 | } |
| 9117 | } |
Chris Lattner | c5eff44 | 2007-01-30 22:32:46 +0000 | [diff] [blame] | 9118 | |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 9119 | return MadeChange ? &SVI : 0; |
| 9120 | } |
| 9121 | |
| 9122 | |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 9123 | |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 9124 | |
| 9125 | /// TryToSinkInstruction - Try to move the specified instruction from its |
| 9126 | /// current block into the beginning of DestBlock, which can only happen if it's |
| 9127 | /// safe to move the instruction past all of the instructions between it and the |
| 9128 | /// end of its block. |
| 9129 | static bool TryToSinkInstruction(Instruction *I, BasicBlock *DestBlock) { |
| 9130 | assert(I->hasOneUse() && "Invariants didn't hold!"); |
| 9131 | |
Chris Lattner | 108e902 | 2005-10-27 17:13:11 +0000 | [diff] [blame] | 9132 | // Cannot move control-flow-involving, volatile loads, vaarg, etc. |
| 9133 | if (isa<PHINode>(I) || I->mayWriteToMemory()) return false; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9134 | |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 9135 | // Do not sink alloca instructions out of the entry block. |
| 9136 | if (isa<AllocaInst>(I) && I->getParent() == &DestBlock->getParent()->front()) |
| 9137 | return false; |
| 9138 | |
Chris Lattner | 96a52a6 | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 9139 | // We can only sink load instructions if there is nothing between the load and |
| 9140 | // the end of block that could change the value. |
| 9141 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Chris Lattner | 96a52a6 | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 9142 | for (BasicBlock::iterator Scan = LI, E = LI->getParent()->end(); |
| 9143 | Scan != E; ++Scan) |
| 9144 | if (Scan->mayWriteToMemory()) |
| 9145 | return false; |
Chris Lattner | 96a52a6 | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 9146 | } |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 9147 | |
| 9148 | BasicBlock::iterator InsertPos = DestBlock->begin(); |
| 9149 | while (isa<PHINode>(InsertPos)) ++InsertPos; |
| 9150 | |
Chris Lattner | 4bc5f80 | 2005-08-08 19:11:57 +0000 | [diff] [blame] | 9151 | I->moveBefore(InsertPos); |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 9152 | ++NumSunkInst; |
| 9153 | return true; |
| 9154 | } |
| 9155 | |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9156 | |
| 9157 | /// AddReachableCodeToWorklist - Walk the function in depth-first order, adding |
| 9158 | /// all reachable code to the worklist. |
| 9159 | /// |
| 9160 | /// This has a couple of tricks to make the code faster and more powerful. In |
| 9161 | /// particular, we constant fold and DCE instructions as we go, to avoid adding |
| 9162 | /// them to the worklist (this significantly speeds up instcombine on code where |
| 9163 | /// many instructions are dead or constant). Additionally, if we find a branch |
| 9164 | /// whose condition is a known constant, we only visit the reachable successors. |
| 9165 | /// |
| 9166 | static void AddReachableCodeToWorklist(BasicBlock *BB, |
Chris Lattner | 1f87a58 | 2007-02-15 19:41:52 +0000 | [diff] [blame] | 9167 | SmallPtrSet<BasicBlock*, 64> &Visited, |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9168 | InstCombiner &IC, |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 9169 | const TargetData *TD) { |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9170 | // We have now visited this block! If we've already been here, bail out. |
Chris Lattner | 1f87a58 | 2007-02-15 19:41:52 +0000 | [diff] [blame] | 9171 | if (!Visited.insert(BB)) return; |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9172 | |
| 9173 | for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ) { |
| 9174 | Instruction *Inst = BBI++; |
| 9175 | |
| 9176 | // DCE instruction if trivially dead. |
| 9177 | if (isInstructionTriviallyDead(Inst)) { |
| 9178 | ++NumDeadInst; |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 9179 | DOUT << "IC: DCE: " << *Inst; |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9180 | Inst->eraseFromParent(); |
| 9181 | continue; |
| 9182 | } |
| 9183 | |
| 9184 | // ConstantProp instruction if trivially constant. |
Chris Lattner | 0a19ffa | 2007-01-30 23:16:15 +0000 | [diff] [blame] | 9185 | if (Constant *C = ConstantFoldInstruction(Inst, TD)) { |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 9186 | DOUT << "IC: ConstFold to: " << *C << " from: " << *Inst; |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9187 | Inst->replaceAllUsesWith(C); |
| 9188 | ++NumConstProp; |
| 9189 | Inst->eraseFromParent(); |
| 9190 | continue; |
| 9191 | } |
| 9192 | |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9193 | IC.AddToWorkList(Inst); |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9194 | } |
| 9195 | |
| 9196 | // Recursively visit successors. If this is a branch or switch on a constant, |
| 9197 | // only visit the reachable successor. |
| 9198 | TerminatorInst *TI = BB->getTerminator(); |
| 9199 | if (BranchInst *BI = dyn_cast<BranchInst>(TI)) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 9200 | if (BI->isConditional() && isa<ConstantInt>(BI->getCondition())) { |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 9201 | bool CondVal = cast<ConstantInt>(BI->getCondition())->getZExtValue(); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9202 | AddReachableCodeToWorklist(BI->getSuccessor(!CondVal), Visited, IC, TD); |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9203 | return; |
| 9204 | } |
| 9205 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) { |
| 9206 | if (ConstantInt *Cond = dyn_cast<ConstantInt>(SI->getCondition())) { |
| 9207 | // See if this is an explicit destination. |
| 9208 | for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i) |
| 9209 | if (SI->getCaseValue(i) == Cond) { |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9210 | AddReachableCodeToWorklist(SI->getSuccessor(i), Visited, IC, TD); |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9211 | return; |
| 9212 | } |
| 9213 | |
| 9214 | // Otherwise it is the default destination. |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9215 | AddReachableCodeToWorklist(SI->getSuccessor(0), Visited, IC, TD); |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9216 | return; |
| 9217 | } |
| 9218 | } |
| 9219 | |
| 9220 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9221 | AddReachableCodeToWorklist(TI->getSuccessor(i), Visited, IC, TD); |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9222 | } |
| 9223 | |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 9224 | bool InstCombiner::DoOneIteration(Function &F, unsigned Iteration) { |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 9225 | bool Changed = false; |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 9226 | TD = &getAnalysis<TargetData>(); |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 9227 | |
| 9228 | DEBUG(DOUT << "\n\nINSTCOMBINE ITERATION #" << Iteration << " on " |
| 9229 | << F.getNameStr() << "\n"); |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9230 | |
Chris Lattner | b3d5970 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 9231 | { |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9232 | // Do a depth-first traversal of the function, populate the worklist with |
| 9233 | // the reachable instructions. Ignore blocks that are not reachable. Keep |
| 9234 | // track of which blocks we visit. |
Chris Lattner | 1f87a58 | 2007-02-15 19:41:52 +0000 | [diff] [blame] | 9235 | SmallPtrSet<BasicBlock*, 64> Visited; |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9236 | AddReachableCodeToWorklist(F.begin(), Visited, *this, TD); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 9237 | |
Chris Lattner | b3d5970 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 9238 | // Do a quick scan over the function. If we find any blocks that are |
| 9239 | // unreachable, remove any instructions inside of them. This prevents |
| 9240 | // the instcombine code from having to deal with some bad special cases. |
| 9241 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 9242 | if (!Visited.count(BB)) { |
| 9243 | Instruction *Term = BB->getTerminator(); |
| 9244 | while (Term != BB->begin()) { // Remove instrs bottom-up |
| 9245 | BasicBlock::iterator I = Term; --I; |
Chris Lattner | 6ffe551 | 2004-04-27 15:13:33 +0000 | [diff] [blame] | 9246 | |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 9247 | DOUT << "IC: DCE: " << *I; |
Chris Lattner | b3d5970 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 9248 | ++NumDeadInst; |
| 9249 | |
| 9250 | if (!I->use_empty()) |
| 9251 | I->replaceAllUsesWith(UndefValue::get(I->getType())); |
| 9252 | I->eraseFromParent(); |
| 9253 | } |
| 9254 | } |
| 9255 | } |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9256 | |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9257 | while (!Worklist.empty()) { |
| 9258 | Instruction *I = RemoveOneFromWorkList(); |
| 9259 | if (I == 0) continue; // skip null values. |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9260 | |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 9261 | // Check to see if we can DCE the instruction. |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 9262 | if (isInstructionTriviallyDead(I)) { |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 9263 | // Add operands to the worklist. |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9264 | if (I->getNumOperands() < 4) |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 9265 | AddUsesToWorkList(*I); |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 9266 | ++NumDeadInst; |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9267 | |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 9268 | DOUT << "IC: DCE: " << *I; |
Chris Lattner | ad5fec1 | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 9269 | |
| 9270 | I->eraseFromParent(); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9271 | RemoveFromWorkList(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9272 | continue; |
| 9273 | } |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 9274 | |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 9275 | // Instruction isn't dead, see if we can constant propagate it. |
Chris Lattner | 0a19ffa | 2007-01-30 23:16:15 +0000 | [diff] [blame] | 9276 | if (Constant *C = ConstantFoldInstruction(I, TD)) { |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 9277 | DOUT << "IC: ConstFold to: " << *C << " from: " << *I; |
Chris Lattner | ad5fec1 | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 9278 | |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 9279 | // Add operands to the worklist. |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 9280 | AddUsesToWorkList(*I); |
Chris Lattner | c736d56 | 2002-12-05 22:41:53 +0000 | [diff] [blame] | 9281 | ReplaceInstUsesWith(*I, C); |
| 9282 | |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 9283 | ++NumConstProp; |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9284 | I->eraseFromParent(); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9285 | RemoveFromWorkList(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9286 | continue; |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 9287 | } |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9288 | |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 9289 | // See if we can trivially sink this instruction to a successor basic block. |
| 9290 | if (I->hasOneUse()) { |
| 9291 | BasicBlock *BB = I->getParent(); |
| 9292 | BasicBlock *UserParent = cast<Instruction>(I->use_back())->getParent(); |
| 9293 | if (UserParent != BB) { |
| 9294 | bool UserIsSuccessor = false; |
| 9295 | // See if the user is one of our successors. |
| 9296 | for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI) |
| 9297 | if (*SI == UserParent) { |
| 9298 | UserIsSuccessor = true; |
| 9299 | break; |
| 9300 | } |
| 9301 | |
| 9302 | // If the user is one of our immediate successors, and if that successor |
| 9303 | // only has us as a predecessors (we'd have to split the critical edge |
| 9304 | // otherwise), we can keep going. |
| 9305 | if (UserIsSuccessor && !isa<PHINode>(I->use_back()) && |
| 9306 | next(pred_begin(UserParent)) == pred_end(UserParent)) |
| 9307 | // Okay, the CFG is simple enough, try to sink this instruction. |
| 9308 | Changed |= TryToSinkInstruction(I, UserParent); |
| 9309 | } |
| 9310 | } |
| 9311 | |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9312 | // Now that we have an instruction, try combining it to simplify it... |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9313 | if (Instruction *Result = visit(*I)) { |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 9314 | ++NumCombined; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 9315 | // Should we replace the old instruction with a new one? |
Chris Lattner | b3bc8fa | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 9316 | if (Result != I) { |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 9317 | DOUT << "IC: Old = " << *I |
| 9318 | << " New = " << *Result; |
Chris Lattner | 0cea42a | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 9319 | |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 9320 | // Everything uses the new instruction now. |
| 9321 | I->replaceAllUsesWith(Result); |
| 9322 | |
| 9323 | // Push the new instruction and any users onto the worklist. |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9324 | AddToWorkList(Result); |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 9325 | AddUsersToWorkList(*Result); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9326 | |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 9327 | // Move the name to the new instruction first. |
| 9328 | Result->takeName(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9329 | |
| 9330 | // Insert the new instruction into the basic block... |
| 9331 | BasicBlock *InstParent = I->getParent(); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9332 | BasicBlock::iterator InsertPos = I; |
| 9333 | |
| 9334 | if (!isa<PHINode>(Result)) // If combining a PHI, don't insert |
| 9335 | while (isa<PHINode>(InsertPos)) // middle of a block of PHIs. |
| 9336 | ++InsertPos; |
| 9337 | |
| 9338 | InstParent->getInstList().insert(InsertPos, Result); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9339 | |
Chris Lattner | 00d5131 | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 9340 | // Make sure that we reprocess all operands now that we reduced their |
| 9341 | // use counts. |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9342 | AddUsesToWorkList(*I); |
Chris Lattner | 216d4d8 | 2004-05-01 23:19:52 +0000 | [diff] [blame] | 9343 | |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 9344 | // Instructions can end up on the worklist more than once. Make sure |
| 9345 | // we do not process an instruction that has been deleted. |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9346 | RemoveFromWorkList(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9347 | |
| 9348 | // Erase the old instruction. |
| 9349 | InstParent->getInstList().erase(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 9350 | } else { |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 9351 | DOUT << "IC: MOD = " << *I; |
Chris Lattner | 0cea42a | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 9352 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9353 | // If the instruction was modified, it's possible that it is now dead. |
| 9354 | // if so, remove it. |
Chris Lattner | 00d5131 | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 9355 | if (isInstructionTriviallyDead(I)) { |
| 9356 | // Make sure we process all operands now that we are reducing their |
| 9357 | // use counts. |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 9358 | AddUsesToWorkList(*I); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9359 | |
Chris Lattner | 00d5131 | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 9360 | // Instructions may end up in the worklist more than once. Erase all |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 9361 | // occurrences of this instruction. |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9362 | RemoveFromWorkList(I); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 9363 | I->eraseFromParent(); |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 9364 | } else { |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 9365 | AddToWorkList(I); |
| 9366 | AddUsersToWorkList(*I); |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9367 | } |
Chris Lattner | b3bc8fa | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 9368 | } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 9369 | Changed = true; |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9370 | } |
| 9371 | } |
| 9372 | |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 9373 | assert(WorklistMap.empty() && "Worklist empty, but map not?"); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 9374 | return Changed; |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 9375 | } |
| 9376 | |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 9377 | |
| 9378 | bool InstCombiner::runOnFunction(Function &F) { |
Chris Lattner | f964f32 | 2007-03-04 04:27:24 +0000 | [diff] [blame] | 9379 | MustPreserveLCSSA = mustPreserveAnalysisID(LCSSAID); |
| 9380 | |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 9381 | bool EverMadeChange = false; |
| 9382 | |
| 9383 | // Iterate while there is work to do. |
| 9384 | unsigned Iteration = 0; |
| 9385 | while (DoOneIteration(F, Iteration++)) |
| 9386 | EverMadeChange = true; |
| 9387 | return EverMadeChange; |
| 9388 | } |
| 9389 | |
Brian Gaeke | 96d4bf7 | 2004-07-27 17:43:21 +0000 | [diff] [blame] | 9390 | FunctionPass *llvm::createInstructionCombiningPass() { |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 9391 | return new InstCombiner(); |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 9392 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 9393 | |