Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1 | //===- InstructionCombining.cpp - Combine multiple instructions -----------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 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 | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9 | // |
| 10 | // InstructionCombining - Combine instructions to form fewer, simple |
Chris Lattner | 99f48c6 | 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 | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 13 | // |
| 14 | // This pass combines things like: |
Chris Lattner | dd1a86d | 2004-05-04 15:19:33 +0000 | [diff] [blame] | 15 | // %Y = add int %X, 1 |
| 16 | // %Z = add int %Y, 1 |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 17 | // into: |
Chris Lattner | dd1a86d | 2004-05-04 15:19:33 +0000 | [diff] [blame] | 18 | // %Z = add int %X, 2 |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 19 | // |
| 20 | // This is a simple worklist driven algorithm. |
| 21 | // |
Chris Lattner | 216c7b8 | 2003-09-10 05:29:43 +0000 | [diff] [blame] | 22 | // This pass guarantees that the following canonicalizations are performed on |
Chris Lattner | bfb1d03 | 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 | deaa0dd | 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 | 266e42b | 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 | ede3fe0 | 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 | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 32 | // ... etc. |
Chris Lattner | bfb1d03 | 2003-07-23 21:41:57 +0000 | [diff] [blame] | 33 | // |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 34 | //===----------------------------------------------------------------------===// |
| 35 | |
Chris Lattner | 7d2a539 | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 36 | #define DEBUG_TYPE "instcombine" |
Chris Lattner | b4cfa7f | 2002-05-07 20:03:00 +0000 | [diff] [blame] | 37 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 38 | #include "llvm/IntrinsicInst.h" |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 39 | #include "llvm/Pass.h" |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 40 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 41 | #include "llvm/GlobalVariable.h" |
Chris Lattner | 024f4ab | 2007-01-30 23:46:24 +0000 | [diff] [blame] | 42 | #include "llvm/Analysis/ConstantFolding.h" |
Chris Lattner | f4ad165 | 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 | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 46 | #include "llvm/Support/CallSite.h" |
Chris Lattner | 39c98bb | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 47 | #include "llvm/Support/Debug.h" |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 48 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 49 | #include "llvm/Support/InstVisitor.h" |
Chris Lattner | 22d00a8 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 50 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 51 | #include "llvm/Support/PatternMatch.h" |
Chris Lattner | 3d27be1 | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 52 | #include "llvm/Support/Compiler.h" |
Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 53 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | f96f4a8 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 54 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 7907e5f | 2007-02-15 19:41:52 +0000 | [diff] [blame] | 55 | #include "llvm/ADT/SmallPtrSet.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 56 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | 39c98bb | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 57 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 053c093 | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 58 | #include <algorithm> |
Reid Spencer | 3f4e6e8 | 2007-02-04 00:40:42 +0000 | [diff] [blame] | 59 | #include <set> |
Chris Lattner | 8427bff | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 60 | using namespace llvm; |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 61 | using namespace llvm::PatternMatch; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 62 | |
Chris Lattner | 79a42ac | 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 | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 68 | |
Chris Lattner | 79a42ac | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 69 | namespace { |
Chris Lattner | 4a4c7fe | 2006-06-28 22:08:15 +0000 | [diff] [blame] | 70 | class VISIBILITY_HIDDEN InstCombiner |
| 71 | : public FunctionPass, |
| 72 | public InstVisitor<InstCombiner, Instruction*> { |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 73 | // Worklist of all of the instructions that need to be simplified. |
Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 74 | std::vector<Instruction*> Worklist; |
| 75 | DenseMap<Instruction*, unsigned> WorklistMap; |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 76 | TargetData *TD; |
Chris Lattner | 8258b44 | 2007-03-04 04:27:24 +0000 | [diff] [blame] | 77 | bool MustPreserveLCSSA; |
Chris Lattner | b15e2b1 | 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 | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 103 | |
Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 104 | |
Chris Lattner | 51ea127 | 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 | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 109 | void AddUsersToWorkList(Value &I) { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 110 | for (Value::use_iterator UI = I.use_begin(), UE = I.use_end(); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 111 | UI != UE; ++UI) |
Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 112 | AddToWorkList(cast<Instruction>(*UI)); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Chris Lattner | 51ea127 | 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 | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 121 | AddToWorkList(Op); |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 122 | } |
Chris Lattner | 2deeaea | 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 | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 135 | AddToWorkList(Op); |
Chris Lattner | 2deeaea | 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 | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 142 | |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 143 | public: |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 144 | virtual bool runOnFunction(Function &F); |
Chris Lattner | 960a543 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 145 | |
| 146 | bool DoOneIteration(Function &F, unsigned ItNum); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 147 | |
Chris Lattner | f12cc84 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 148 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 149 | AU.addRequired<TargetData>(); |
Owen Anderson | a6968f8 | 2006-07-10 19:03:49 +0000 | [diff] [blame] | 150 | AU.addPreservedID(LCSSAID); |
Chris Lattner | 820d971 | 2002-10-21 20:00:28 +0000 | [diff] [blame] | 151 | AU.setPreservesCFG(); |
Chris Lattner | f12cc84 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 154 | TargetData &getTargetData() const { return *TD; } |
| 155 | |
Chris Lattner | 260ab20 | 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 | e679449 | 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 | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 161 | // otherwise - Change was made, replace I with returned instruction |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 162 | // |
Chris Lattner | 113f4f4 | 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 | 7eb55b3 | 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 | 7e80b0b | 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 | 113f4f4 | 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 | 2341c22 | 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 | 266e42b | 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 | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 186 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 187 | Instruction *FoldGEPICmp(User *GEPLHS, Value *RHS, |
| 188 | ICmpInst::Predicate Cond, Instruction &I); |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 189 | Instruction *FoldShiftByConstant(Value *Op0, ConstantInt *Op1, |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 190 | BinaryOperator &I); |
Reid Spencer | 6c38f0b | 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 | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 205 | Instruction *FoldSelectOpOp(SelectInst &SI, Instruction *TI, |
| 206 | Instruction *FI); |
Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 207 | Instruction *visitSelectInst(SelectInst &CI); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 208 | Instruction *visitCallInst(CallInst &CI); |
| 209 | Instruction *visitInvokeInst(InvokeInst &II); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 210 | Instruction *visitPHINode(PHINode &PN); |
| 211 | Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP); |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 212 | Instruction *visitAllocationInst(AllocationInst &AI); |
Chris Lattner | 8427bff | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 213 | Instruction *visitFreeInst(FreeInst &FI); |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 214 | Instruction *visitLoadInst(LoadInst &LI); |
Chris Lattner | 31f486c | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 215 | Instruction *visitStoreInst(StoreInst &SI); |
Chris Lattner | 9eef8a7 | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 216 | Instruction *visitBranchInst(BranchInst &BI); |
Chris Lattner | 4c9c20a | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 217 | Instruction *visitSwitchInst(SwitchInst &SI); |
Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 218 | Instruction *visitInsertElementInst(InsertElementInst &IE); |
Robert Bocchino | a835296 | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 219 | Instruction *visitExtractElementInst(ExtractElementInst &EI); |
Chris Lattner | fbb77a4 | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 220 | Instruction *visitShuffleVectorInst(ShuffleVectorInst &SVI); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 221 | |
| 222 | // visitInstruction - Specify what to return for unhandled instructions... |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 223 | Instruction *visitInstruction(Instruction &I) { return 0; } |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 224 | |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 225 | private: |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 226 | Instruction *visitCallSite(CallSite CS); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 227 | bool transformConstExprCastCall(CallSite CS); |
| 228 | |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 229 | public: |
Chris Lattner | 6d14f2a | 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 | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 233 | Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) { |
Chris Lattner | 65217ff | 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 | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 236 | BasicBlock *BB = Old.getParent(); |
| 237 | BB->getInstList().insert(&Old, New); // Insert inst |
Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 238 | AddToWorkList(New); |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 239 | return New; |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Chris Lattner | 7e79427 | 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 | 13bc5d7 | 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 | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 247 | if (V->getType() == Ty) return V; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 248 | |
Chris Lattner | e79d249 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 249 | if (Constant *CV = dyn_cast<Constant>(V)) |
Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 250 | return ConstantExpr::getCast(opc, CV, Ty); |
Chris Lattner | e79d249 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 251 | |
Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 252 | Instruction *C = CastInst::create(opc, V, Ty, V->getName(), &Pos); |
Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 253 | AddToWorkList(C); |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 254 | return C; |
| 255 | } |
| 256 | |
Chris Lattner | 6d14f2a | 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 | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 264 | AddUsersToWorkList(I); // Add all modified instrs to worklist |
Chris Lattner | 8953b90 | 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 | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 271 | I.replaceAllUsesWith(UndefValue::get(I.getType())); |
Chris Lattner | 8953b90 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 272 | return &I; |
| 273 | } |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 274 | } |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 275 | |
Chris Lattner | 2590e51 | 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 | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 287 | AddToWorkList(I); |
Chris Lattner | 5b2edb1 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 288 | if (Instruction *I = dyn_cast<Instruction>(New)) |
Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 289 | AddToWorkList(I); |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 290 | return true; |
| 291 | } |
| 292 | |
Chris Lattner | 51ea127 | 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 | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 300 | RemoveFromWorkList(&I); |
Chris Lattner | 9530754 | 2004-11-18 21:41:39 +0000 | [diff] [blame] | 301 | I.eraseFromParent(); |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 302 | return 0; // Don't do anything with FI |
| 303 | } |
| 304 | |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 305 | private: |
Chris Lattner | dfae8be | 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 | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 310 | Value *InsertOperandCastBefore(Instruction::CastOps opcode, |
| 311 | Value *V, const Type *DestTy, |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 312 | Instruction *InsertBefore); |
| 313 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 314 | /// SimplifyCommutative - This performs a few simplifications for |
| 315 | /// commutative operators. |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 316 | bool SimplifyCommutative(BinaryOperator &I); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 317 | |
Reid Spencer | 266e42b | 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 | 0157e7f | 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 | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 325 | |
Chris Lattner | 2deeaea | 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 | 6a4adcd | 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 | 7515cab | 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 | cadac0c | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 338 | Instruction *FoldPHIArgBinOpIntoPHI(PHINode &PN); |
| 339 | |
| 340 | |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 341 | Instruction *OptAndOp(Instruction *Op, ConstantInt *OpRHS, |
| 342 | ConstantInt *AndRHS, BinaryOperator &TheAnd); |
Chris Lattner | af51757 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 343 | |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 344 | Value *FoldLogicalPlusAnd(Value *LHS, Value *RHS, ConstantInt *Mask, |
Chris Lattner | af51757 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 345 | bool isSub, Instruction &I); |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 346 | Instruction *InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 347 | bool isSigned, bool Inside, Instruction &IB); |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 348 | Instruction *PromoteCastOfAllocation(CastInst &CI, AllocationInst &AI); |
Chris Lattner | c482a9e | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 349 | Instruction *MatchBSwap(BinaryOperator &I); |
| 350 | |
Reid Spencer | 74a528b | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 351 | Value *EvaluateInDifferentType(Value *V, const Type *Ty, bool isSigned); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 352 | }; |
Chris Lattner | b28b680 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 353 | |
Chris Lattner | c2d3d31 | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 354 | RegisterPass<InstCombiner> X("instcombine", "Combine redundant instructions"); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 357 | // getComplexity: Assign a complexity or rank value to LLVM Values... |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 358 | // 0 -> undef, 1 -> Const, 2 -> Other, 3 -> Arg, 3 -> Unary, 4 -> OtherInst |
Chris Lattner | dcf240a | 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 | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 362 | return 3; |
| 363 | return 4; |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 364 | } |
Chris Lattner | 81a7a23 | 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 | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 367 | } |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 368 | |
Chris Lattner | 7fb29e1 | 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 | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 372 | return V->hasOneUse() || isa<Constant>(V); |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Chris Lattner | e79e854 | 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 | 7a9c62b | 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 | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Reid Spencer | 6c38f0b | 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 | 567b81f | 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 | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 392 | if (CE->getOpcode() == Instruction::BitCast) |
Chris Lattner | 567b81f | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 393 | return CE->getOperand(0); |
| 394 | return 0; |
| 395 | } |
| 396 | |
Reid Spencer | 6c38f0b | 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 | 6c38f0b | 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 | 1d441ad | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 409 | |
Reid Spencer | 6c38f0b | 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 | 1d441ad | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 413 | |
Reid Spencer | 6c38f0b | 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 | 1d441ad | 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 | 266e42b | 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 | 1d441ad | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 424 | if (V->getType() == Ty || isa<Constant>(V)) return false; |
| 425 | |
Chris Lattner | 99155be | 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 | 1d441ad | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 427 | if (const CastInst *CI = dyn_cast<CastInst>(V)) |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 428 | if (isEliminableCastPair(CI, opcode, Ty, TD)) |
Chris Lattner | 1d441ad | 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 | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 437 | Value *InstCombiner::InsertOperandCastBefore(Instruction::CastOps opcode, |
| 438 | Value *V, const Type *DestTy, |
Chris Lattner | 1d441ad | 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 | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 442 | return ConstantExpr::getCast(opcode, C, DestTy); |
Chris Lattner | 1d441ad | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 443 | |
Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 444 | return InsertCastBefore(opcode, V, DestTy, *InsertBefore); |
Chris Lattner | 1d441ad | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 447 | // SimplifyCommutative - This performs a few simplifications for commutative |
| 448 | // operators: |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 449 | // |
Chris Lattner | dcf240a | 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 | 7fb29e1 | 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 | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 456 | // |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 457 | bool InstCombiner::SimplifyCommutative(BinaryOperator &I) { |
Chris Lattner | dcf240a | 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 | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 461 | |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 462 | if (!I.isAssociative()) return Changed; |
| 463 | Instruction::BinaryOps Opcode = I.getOpcode(); |
Chris Lattner | 7fb29e1 | 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 | 3442844 | 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 | 7fb29e1 | 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 | 3442844 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 480 | Constant *Folded = ConstantExpr::get(I.getOpcode(), C1, C2); |
Chris Lattner | 7fb29e1 | 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 | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 484 | AddToWorkList(New); |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 485 | I.setOperand(0, New); |
| 486 | I.setOperand(1, Folded); |
| 487 | return true; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 488 | } |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 489 | } |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 490 | return Changed; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 491 | } |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 492 | |
Reid Spencer | 266e42b | 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 | bb74e22 | 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 | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 506 | // |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 507 | static inline Value *dyn_castNegVal(Value *V) { |
| 508 | if (BinaryOperator::isNeg(V)) |
Chris Lattner | d6f636a | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 509 | return BinaryOperator::getNegArgument(V); |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 510 | |
Chris Lattner | 9ad0d55 | 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 | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 514 | return 0; |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 517 | static inline Value *dyn_castNotVal(Value *V) { |
| 518 | if (BinaryOperator::isNot(V)) |
Chris Lattner | d6f636a | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 519 | return BinaryOperator::getNotArgument(V); |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 520 | |
| 521 | // Constants can be considered to be not'ed values... |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 522 | if (ConstantInt *C = dyn_cast<ConstantInt>(V)) |
Chris Lattner | c8e7e29 | 2004-06-10 02:12:35 +0000 | [diff] [blame] | 523 | return ConstantExpr::getNot(C); |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 524 | return 0; |
| 525 | } |
| 526 | |
Chris Lattner | 7fb29e1 | 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 | 8c3e7b9 | 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 | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 531 | // |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 532 | static inline Value *dyn_castFoldableMul(Value *V, ConstantInt *&CST) { |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 533 | if (V->hasOneUse() && V->getType()->isInteger()) |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 534 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 535 | if (I->getOpcode() == Instruction::Mul) |
Chris Lattner | 97013636 | 2004-11-15 05:54:07 +0000 | [diff] [blame] | 536 | if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 537 | return I->getOperand(0); |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 538 | if (I->getOpcode() == Instruction::Shl) |
Chris Lattner | 97013636 | 2004-11-15 05:54:07 +0000 | [diff] [blame] | 539 | if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) { |
Chris Lattner | 8c3e7b9 | 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 | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 546 | return 0; |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 547 | } |
Chris Lattner | 31ae863 | 2002-08-14 17:51:49 +0000 | [diff] [blame] | 548 | |
Chris Lattner | 0798af3 | 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 | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 559 | // AddOne, SubOne - Add or subtract a constant one from an integer constant... |
Chris Lattner | 6862fbd | 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 | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 563 | } |
Chris Lattner | 6862fbd | 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 | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 567 | } |
| 568 | |
Chris Lattner | 4534dd59 | 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 | 0b3557f | 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 | c3ebf40 | 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 | 0b3557f | 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 | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 581 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
Chris Lattner | 4534dd59 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 582 | // We know all of the bits for a constant! |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 583 | KnownOne = CI->getZExtValue() & Mask; |
Chris Lattner | 4534dd59 | 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 | 92a6865 | 2006-02-07 08:05:22 +0000 | [diff] [blame] | 589 | if (Depth == 6 || Mask == 0) |
Chris Lattner | 4534dd59 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 590 | return; // Limit search depth. |
| 591 | |
| 592 | uint64_t KnownZero2, KnownOne2; |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 593 | Instruction *I = dyn_cast<Instruction>(V); |
| 594 | if (!I) return; |
| 595 | |
Reid Spencer | a94d394 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 596 | Mask &= cast<IntegerType>(V->getType())->getBitMask(); |
Chris Lattner | fb29692 | 2006-05-04 17:33:35 +0000 | [diff] [blame] | 597 | |
Chris Lattner | 0157e7f | 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 | 6c38f0b | 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 | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 661 | const Type *SrcTy = I->getOperand(0)->getType(); |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 662 | if (SrcTy->isInteger()) { |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 663 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero, KnownOne, Depth+1); |
Chris Lattner | 4534dd59 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 664 | return; |
| 665 | } |
Reid Spencer | 6c38f0b | 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 | a94d394 | 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 | 62010c4 | 2005-10-09 06:36:35 +0000 | [diff] [blame] | 673 | |
Reid Spencer | a94d394 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 674 | Mask &= SrcTy->getBitMask(); |
Reid Spencer | 6c38f0b | 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 | a94d394 | 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 | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 686 | |
Reid Spencer | a94d394 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 687 | Mask &= SrcTy->getBitMask(); |
Reid Spencer | 6c38f0b | 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 | 92a6865 | 2006-02-07 08:05:22 +0000 | [diff] [blame] | 690 | |
Reid Spencer | 6c38f0b | 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 | 0157e7f | 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 | e0fc4df | 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 | 0157e7f | 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 | e0fc4df | 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 | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 716 | return; |
| 717 | } |
| 718 | break; |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 719 | case Instruction::LShr: |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 720 | // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0 |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 721 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 722 | // Compute the new bits that are at the top now. |
Reid Spencer | e0fc4df | 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 | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 726 | |
Reid Spencer | fdff938 | 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 | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 751 | |
Reid Spencer | fdff938 | 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 | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 755 | |
Reid Spencer | fdff938 | 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 | 4534dd59 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 760 | } |
| 761 | return; |
Chris Lattner | 62010c4 | 2005-10-09 06:36:35 +0000 | [diff] [blame] | 762 | } |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 763 | break; |
Chris Lattner | 0b3557f | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 764 | } |
Chris Lattner | 92a6865 | 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 | 4534dd59 | 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 | 0b3557f | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 775 | } |
| 776 | |
Chris Lattner | 0157e7f | 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 | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 792 | I->setOperand(OpNo, ConstantInt::get(OpC->getType(), Val)); |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 793 | return true; |
| 794 | } |
| 795 | |
Chris Lattner | ee0f280 | 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 | a94d394 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 804 | uint64_t TypeBits = cast<IntegerType>(Ty)->getBitMask(); |
Chris Lattner | ee0f280 | 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 | a94d394 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 834 | uint64_t TypeBits = cast<IntegerType>(Ty)->getBitMask(); |
Chris Lattner | ee0f280 | 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 | 0157e7f | 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 | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 852 | unsigned Depth) { |
Chris Lattner | ab2f913 | 2007-03-04 23:16:36 +0000 | [diff] [blame^] | 853 | const IntegerType *VTy = cast<IntegerType>(V->getType()); |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 854 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
Chris Lattner | 0157e7f | 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 | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 862 | if (!V->hasOneUse()) { // Other users may use these bits. |
Chris Lattner | 0157e7f | 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 | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 866 | return false; |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 867 | } |
Chris Lattner | 2590e51 | 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 | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 869 | // just set the DemandedMask to all bits. |
Chris Lattner | ab2f913 | 2007-03-04 23:16:36 +0000 | [diff] [blame^] | 870 | DemandedMask = VTy->getBitMask(); |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 871 | } else if (DemandedMask == 0) { // Not demanding any bits from V. |
Chris Lattner | ab2f913 | 2007-03-04 23:16:36 +0000 | [diff] [blame^] | 872 | if (V != UndefValue::get(VTy)) |
| 873 | return UpdateValueUsesWith(V, UndefValue::get(VTy)); |
Chris Lattner | 92a6865 | 2006-02-07 08:05:22 +0000 | [diff] [blame] | 874 | return false; |
Chris Lattner | 2590e51 | 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 | ab2f913 | 2007-03-04 23:16:36 +0000 | [diff] [blame^] | 882 | DemandedMask &= VTy->getBitMask(); |
Chris Lattner | fb29692 | 2006-05-04 17:33:35 +0000 | [diff] [blame] | 883 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 884 | uint64_t KnownZero2 = 0, KnownOne2 = 0; |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 885 | switch (I->getOpcode()) { |
| 886 | default: break; |
| 887 | case Instruction::And: |
Chris Lattner | 0157e7f | 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 | 6c38f0b | 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 | 0157e7f | 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 | 5b2edb1 | 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 | ab2f913 | 2007-03-04 23:16:36 +0000 | [diff] [blame^] | 910 | return UpdateValueUsesWith(I, Constant::getNullValue(VTy)); |
Chris Lattner | 5b2edb1 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 911 | |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 912 | // If the RHS is a constant, see if we can simplify it. |
Chris Lattner | 5b2edb1 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 913 | if (ShrinkDemandedConstant(I, 1, DemandedMask & ~KnownZero2)) |
Chris Lattner | 0157e7f | 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 | 0add83e | 2006-02-18 03:20:33 +0000 | [diff] [blame] | 933 | if ((DemandedMask & ~KnownOne2 & KnownZero) == (DemandedMask & ~KnownOne2)) |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 934 | return UpdateValueUsesWith(I, I->getOperand(0)); |
Jeff Cohen | 0add83e | 2006-02-18 03:20:33 +0000 | [diff] [blame] | 935 | if ((DemandedMask & ~KnownOne & KnownZero2) == (DemandedMask & ~KnownOne)) |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 936 | return UpdateValueUsesWith(I, I->getOperand(1)); |
Chris Lattner | 5b2edb1 | 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 | 8a77efe | 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 | 0157e7f | 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 | 8e9a7b7 | 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 | 5b2edb1 | 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 | 8e9a7b7 | 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 | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 987 | } |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 988 | |
Chris Lattner | 5b2edb1 | 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 | ab2f913 | 2007-03-04 23:16:36 +0000 | [diff] [blame^] | 995 | Constant *AndC = ConstantInt::get(VTy, ~KnownOne & DemandedMask); |
Chris Lattner | 5b2edb1 | 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 | 0157e7f | 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 | 6c38f0b | 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 | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1039 | if (!I->getOperand(0)->getType()->isInteger()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1040 | return false; |
Chris Lattner | 850465d | 2006-09-16 03:14:10 +0000 | [diff] [blame] | 1041 | |
Reid Spencer | 6c38f0b | 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 | a94d394 | 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 | ab2f913 | 2007-03-04 23:16:36 +0000 | [diff] [blame^] | 1051 | uint64_t NewBits = VTy->getBitMask() & NotIn; |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1052 | |
Reid Spencer | a94d394 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 1053 | DemandedMask &= SrcTy->getBitMask(); |
Reid Spencer | 6c38f0b | 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 | a94d394 | 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 | ab2f913 | 2007-03-04 23:16:36 +0000 | [diff] [blame^] | 1066 | uint64_t NewBits = VTy->getBitMask() & NotIn; |
Reid Spencer | 6c38f0b | 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 | a94d394 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 1070 | int64_t InputDemandedBits = DemandedMask & SrcTy->getBitMask(); |
Chris Lattner | 7d85228 | 2006-02-13 22:41:07 +0000 | [diff] [blame] | 1071 | |
Reid Spencer | 6c38f0b | 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 | 7d85228 | 2006-02-13 22:41:07 +0000 | [diff] [blame] | 1076 | |
Reid Spencer | 6c38f0b | 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 | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1081 | |
Reid Spencer | 6c38f0b | 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 | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 1084 | |
Reid Spencer | 6c38f0b | 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 | ab2f913 | 2007-03-04 23:16:36 +0000 | [diff] [blame^] | 1089 | CastInst *NewCast = new ZExtInst(I->getOperand(0), VTy, I->getName(), I); |
Reid Spencer | 6c38f0b | 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 | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 1097 | } |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1098 | break; |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 1099 | } |
Chris Lattner | 6e2c15c | 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 | ab2f913 | 2007-03-04 23:16:36 +0000 | [diff] [blame^] | 1114 | unsigned BitWidth = VTy->getPrimitiveSizeInBits(); |
Chris Lattner | 6e2c15c | 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 | 223004c | 2007-01-08 20:17:17 +0000 | [diff] [blame] | 1119 | uint64_t InDemandedBits = ~0ULL >> (64-BitWidth+NLZ); |
Chris Lattner | 6e2c15c | 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; |
| 1189 | } |
| 1190 | break; |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 1191 | case Instruction::Shl: |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1192 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 1193 | uint64_t ShiftAmt = SA->getZExtValue(); |
| 1194 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask >> ShiftAmt, |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1195 | KnownZero, KnownOne, Depth+1)) |
| 1196 | return true; |
| 1197 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1198 | KnownZero <<= ShiftAmt; |
| 1199 | KnownOne <<= ShiftAmt; |
| 1200 | KnownZero |= (1ULL << ShiftAmt) - 1; // low bits known zero. |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1201 | } |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 1202 | break; |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1203 | case Instruction::LShr: |
| 1204 | // For a logical shift right |
| 1205 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 1206 | unsigned ShiftAmt = SA->getZExtValue(); |
| 1207 | |
| 1208 | // Compute the new bits that are at the top now. |
| 1209 | uint64_t HighBits = (1ULL << ShiftAmt)-1; |
Chris Lattner | ab2f913 | 2007-03-04 23:16:36 +0000 | [diff] [blame^] | 1210 | HighBits <<= VTy->getBitWidth() - ShiftAmt; |
| 1211 | uint64_t TypeMask = VTy->getBitMask(); |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1212 | // Unsigned shift right. |
| 1213 | if (SimplifyDemandedBits(I->getOperand(0), |
| 1214 | (DemandedMask << ShiftAmt) & TypeMask, |
| 1215 | KnownZero, KnownOne, Depth+1)) |
| 1216 | return true; |
| 1217 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 1218 | KnownZero &= TypeMask; |
| 1219 | KnownOne &= TypeMask; |
| 1220 | KnownZero >>= ShiftAmt; |
| 1221 | KnownOne >>= ShiftAmt; |
| 1222 | KnownZero |= HighBits; // high bits known zero. |
| 1223 | } |
| 1224 | break; |
| 1225 | case Instruction::AShr: |
Chris Lattner | 420c4bc | 2006-09-18 04:31:40 +0000 | [diff] [blame] | 1226 | // If this is an arithmetic shift right and only the low-bit is set, we can |
| 1227 | // always convert this into a logical shr, even if the shift amount is |
| 1228 | // variable. The low bit of the shift cannot be an input sign bit unless |
| 1229 | // the shift amount is >= the size of the datatype, which is undefined. |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1230 | if (DemandedMask == 1) { |
| 1231 | // Perform the logical shift right. |
Reid Spencer | 0d5f923 | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 1232 | Value *NewVal = BinaryOperator::createLShr( |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1233 | I->getOperand(0), I->getOperand(1), I->getName()); |
Reid Spencer | 00c482b | 2006-10-26 19:19:06 +0000 | [diff] [blame] | 1234 | InsertNewInstBefore(cast<Instruction>(NewVal), *I); |
Chris Lattner | 420c4bc | 2006-09-18 04:31:40 +0000 | [diff] [blame] | 1235 | return UpdateValueUsesWith(I, NewVal); |
| 1236 | } |
| 1237 | |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1238 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 1239 | unsigned ShiftAmt = SA->getZExtValue(); |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1240 | |
| 1241 | // Compute the new bits that are at the top now. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1242 | uint64_t HighBits = (1ULL << ShiftAmt)-1; |
Chris Lattner | ab2f913 | 2007-03-04 23:16:36 +0000 | [diff] [blame^] | 1243 | HighBits <<= VTy->getBitWidth() - ShiftAmt; |
| 1244 | uint64_t TypeMask = VTy->getBitMask(); |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1245 | // Signed shift right. |
| 1246 | if (SimplifyDemandedBits(I->getOperand(0), |
| 1247 | (DemandedMask << ShiftAmt) & TypeMask, |
| 1248 | KnownZero, KnownOne, Depth+1)) |
| 1249 | return true; |
| 1250 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 1251 | KnownZero &= TypeMask; |
| 1252 | KnownOne &= TypeMask; |
| 1253 | KnownZero >>= ShiftAmt; |
| 1254 | KnownOne >>= ShiftAmt; |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1255 | |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1256 | // Handle the sign bits. |
Chris Lattner | ab2f913 | 2007-03-04 23:16:36 +0000 | [diff] [blame^] | 1257 | uint64_t SignBit = 1ULL << (VTy->getBitWidth()-1); |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1258 | SignBit >>= ShiftAmt; // Adjust to where it is now in the mask. |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1259 | |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1260 | // If the input sign bit is known to be zero, or if none of the top bits |
| 1261 | // are demanded, turn this into an unsigned shift right. |
| 1262 | if ((KnownZero & SignBit) || (HighBits & ~DemandedMask) == HighBits) { |
| 1263 | // Perform the logical shift right. |
Reid Spencer | 0d5f923 | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 1264 | Value *NewVal = BinaryOperator::createLShr( |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1265 | I->getOperand(0), SA, I->getName()); |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1266 | InsertNewInstBefore(cast<Instruction>(NewVal), *I); |
| 1267 | return UpdateValueUsesWith(I, NewVal); |
| 1268 | } else if (KnownOne & SignBit) { // New bits are known one. |
| 1269 | KnownOne |= HighBits; |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 1270 | } |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1271 | } |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 1272 | break; |
| 1273 | } |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1274 | |
| 1275 | // If the client is only demanding bits that we know, return the known |
| 1276 | // constant. |
| 1277 | if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask) |
Chris Lattner | ab2f913 | 2007-03-04 23:16:36 +0000 | [diff] [blame^] | 1278 | return UpdateValueUsesWith(I, ConstantInt::get(VTy, KnownOne)); |
Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 1279 | return false; |
| 1280 | } |
| 1281 | |
Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1282 | |
| 1283 | /// SimplifyDemandedVectorElts - The specified value producecs a vector with |
| 1284 | /// 64 or fewer elements. DemandedElts contains the set of elements that are |
| 1285 | /// actually used by the caller. This method analyzes which elements of the |
| 1286 | /// operand are undef and returns that information in UndefElts. |
| 1287 | /// |
| 1288 | /// If the information about demanded elements can be used to simplify the |
| 1289 | /// operation, the operation is simplified, then the resultant value is |
| 1290 | /// returned. This returns null if no change was made. |
| 1291 | Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, uint64_t DemandedElts, |
| 1292 | uint64_t &UndefElts, |
| 1293 | unsigned Depth) { |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1294 | unsigned VWidth = cast<VectorType>(V->getType())->getNumElements(); |
Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1295 | assert(VWidth <= 64 && "Vector too wide to analyze!"); |
| 1296 | uint64_t EltMask = ~0ULL >> (64-VWidth); |
| 1297 | assert(DemandedElts != EltMask && (DemandedElts & ~EltMask) == 0 && |
| 1298 | "Invalid DemandedElts!"); |
| 1299 | |
| 1300 | if (isa<UndefValue>(V)) { |
| 1301 | // If the entire vector is undefined, just return this info. |
| 1302 | UndefElts = EltMask; |
| 1303 | return 0; |
| 1304 | } else if (DemandedElts == 0) { // If nothing is demanded, provide undef. |
| 1305 | UndefElts = EltMask; |
| 1306 | return UndefValue::get(V->getType()); |
| 1307 | } |
| 1308 | |
| 1309 | UndefElts = 0; |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1310 | if (ConstantVector *CP = dyn_cast<ConstantVector>(V)) { |
| 1311 | const Type *EltTy = cast<VectorType>(V->getType())->getElementType(); |
Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1312 | Constant *Undef = UndefValue::get(EltTy); |
| 1313 | |
| 1314 | std::vector<Constant*> Elts; |
| 1315 | for (unsigned i = 0; i != VWidth; ++i) |
| 1316 | if (!(DemandedElts & (1ULL << i))) { // If not demanded, set to undef. |
| 1317 | Elts.push_back(Undef); |
| 1318 | UndefElts |= (1ULL << i); |
| 1319 | } else if (isa<UndefValue>(CP->getOperand(i))) { // Already undef. |
| 1320 | Elts.push_back(Undef); |
| 1321 | UndefElts |= (1ULL << i); |
| 1322 | } else { // Otherwise, defined. |
| 1323 | Elts.push_back(CP->getOperand(i)); |
| 1324 | } |
| 1325 | |
| 1326 | // If we changed the constant, return it. |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1327 | Constant *NewCP = ConstantVector::get(Elts); |
Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1328 | return NewCP != CP ? NewCP : 0; |
| 1329 | } else if (isa<ConstantAggregateZero>(V)) { |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1330 | // Simplify the CAZ to a ConstantVector where the non-demanded elements are |
Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1331 | // set to undef. |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1332 | const Type *EltTy = cast<VectorType>(V->getType())->getElementType(); |
Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1333 | Constant *Zero = Constant::getNullValue(EltTy); |
| 1334 | Constant *Undef = UndefValue::get(EltTy); |
| 1335 | std::vector<Constant*> Elts; |
| 1336 | for (unsigned i = 0; i != VWidth; ++i) |
| 1337 | Elts.push_back((DemandedElts & (1ULL << i)) ? Zero : Undef); |
| 1338 | UndefElts = DemandedElts ^ EltMask; |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1339 | return ConstantVector::get(Elts); |
Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | if (!V->hasOneUse()) { // Other users may use these bits. |
| 1343 | if (Depth != 0) { // Not at the root. |
| 1344 | // TODO: Just compute the UndefElts information recursively. |
| 1345 | return false; |
| 1346 | } |
| 1347 | return false; |
| 1348 | } else if (Depth == 10) { // Limit search depth. |
| 1349 | return false; |
| 1350 | } |
| 1351 | |
| 1352 | Instruction *I = dyn_cast<Instruction>(V); |
| 1353 | if (!I) return false; // Only analyze instructions. |
| 1354 | |
| 1355 | bool MadeChange = false; |
| 1356 | uint64_t UndefElts2; |
| 1357 | Value *TmpV; |
| 1358 | switch (I->getOpcode()) { |
| 1359 | default: break; |
| 1360 | |
| 1361 | case Instruction::InsertElement: { |
| 1362 | // If this is a variable index, we don't know which element it overwrites. |
| 1363 | // demand exactly the same input as we produce. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1364 | ConstantInt *Idx = dyn_cast<ConstantInt>(I->getOperand(2)); |
Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1365 | if (Idx == 0) { |
| 1366 | // Note that we can't propagate undef elt info, because we don't know |
| 1367 | // which elt is getting updated. |
| 1368 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, |
| 1369 | UndefElts2, Depth+1); |
| 1370 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } |
| 1371 | break; |
| 1372 | } |
| 1373 | |
| 1374 | // If this is inserting an element that isn't demanded, remove this |
| 1375 | // insertelement. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1376 | unsigned IdxNo = Idx->getZExtValue(); |
Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1377 | if (IdxNo >= VWidth || (DemandedElts & (1ULL << IdxNo)) == 0) |
| 1378 | return AddSoonDeadInstToWorklist(*I, 0); |
| 1379 | |
| 1380 | // Otherwise, the element inserted overwrites whatever was there, so the |
| 1381 | // input demanded set is simpler than the output set. |
| 1382 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), |
| 1383 | DemandedElts & ~(1ULL << IdxNo), |
| 1384 | UndefElts, Depth+1); |
| 1385 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } |
| 1386 | |
| 1387 | // The inserted element is defined. |
| 1388 | UndefElts |= 1ULL << IdxNo; |
| 1389 | break; |
| 1390 | } |
| 1391 | |
| 1392 | case Instruction::And: |
| 1393 | case Instruction::Or: |
| 1394 | case Instruction::Xor: |
| 1395 | case Instruction::Add: |
| 1396 | case Instruction::Sub: |
| 1397 | case Instruction::Mul: |
| 1398 | // div/rem demand all inputs, because they don't want divide by zero. |
| 1399 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, |
| 1400 | UndefElts, Depth+1); |
| 1401 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } |
| 1402 | TmpV = SimplifyDemandedVectorElts(I->getOperand(1), DemandedElts, |
| 1403 | UndefElts2, Depth+1); |
| 1404 | if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; } |
| 1405 | |
| 1406 | // Output elements are undefined if both are undefined. Consider things |
| 1407 | // like undef&0. The result is known zero, not undef. |
| 1408 | UndefElts &= UndefElts2; |
| 1409 | break; |
| 1410 | |
| 1411 | case Instruction::Call: { |
| 1412 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(I); |
| 1413 | if (!II) break; |
| 1414 | switch (II->getIntrinsicID()) { |
| 1415 | default: break; |
| 1416 | |
| 1417 | // Binary vector operations that work column-wise. A dest element is a |
| 1418 | // function of the corresponding input elements from the two inputs. |
| 1419 | case Intrinsic::x86_sse_sub_ss: |
| 1420 | case Intrinsic::x86_sse_mul_ss: |
| 1421 | case Intrinsic::x86_sse_min_ss: |
| 1422 | case Intrinsic::x86_sse_max_ss: |
| 1423 | case Intrinsic::x86_sse2_sub_sd: |
| 1424 | case Intrinsic::x86_sse2_mul_sd: |
| 1425 | case Intrinsic::x86_sse2_min_sd: |
| 1426 | case Intrinsic::x86_sse2_max_sd: |
| 1427 | TmpV = SimplifyDemandedVectorElts(II->getOperand(1), DemandedElts, |
| 1428 | UndefElts, Depth+1); |
| 1429 | if (TmpV) { II->setOperand(1, TmpV); MadeChange = true; } |
| 1430 | TmpV = SimplifyDemandedVectorElts(II->getOperand(2), DemandedElts, |
| 1431 | UndefElts2, Depth+1); |
| 1432 | if (TmpV) { II->setOperand(2, TmpV); MadeChange = true; } |
| 1433 | |
| 1434 | // If only the low elt is demanded and this is a scalarizable intrinsic, |
| 1435 | // scalarize it now. |
| 1436 | if (DemandedElts == 1) { |
| 1437 | switch (II->getIntrinsicID()) { |
| 1438 | default: break; |
| 1439 | case Intrinsic::x86_sse_sub_ss: |
| 1440 | case Intrinsic::x86_sse_mul_ss: |
| 1441 | case Intrinsic::x86_sse2_sub_sd: |
| 1442 | case Intrinsic::x86_sse2_mul_sd: |
| 1443 | // TODO: Lower MIN/MAX/ABS/etc |
| 1444 | Value *LHS = II->getOperand(1); |
| 1445 | Value *RHS = II->getOperand(2); |
| 1446 | // Extract the element as scalars. |
| 1447 | LHS = InsertNewInstBefore(new ExtractElementInst(LHS, 0U,"tmp"), *II); |
| 1448 | RHS = InsertNewInstBefore(new ExtractElementInst(RHS, 0U,"tmp"), *II); |
| 1449 | |
| 1450 | switch (II->getIntrinsicID()) { |
| 1451 | default: assert(0 && "Case stmts out of sync!"); |
| 1452 | case Intrinsic::x86_sse_sub_ss: |
| 1453 | case Intrinsic::x86_sse2_sub_sd: |
| 1454 | TmpV = InsertNewInstBefore(BinaryOperator::createSub(LHS, RHS, |
| 1455 | II->getName()), *II); |
| 1456 | break; |
| 1457 | case Intrinsic::x86_sse_mul_ss: |
| 1458 | case Intrinsic::x86_sse2_mul_sd: |
| 1459 | TmpV = InsertNewInstBefore(BinaryOperator::createMul(LHS, RHS, |
| 1460 | II->getName()), *II); |
| 1461 | break; |
| 1462 | } |
| 1463 | |
| 1464 | Instruction *New = |
| 1465 | new InsertElementInst(UndefValue::get(II->getType()), TmpV, 0U, |
| 1466 | II->getName()); |
| 1467 | InsertNewInstBefore(New, *II); |
| 1468 | AddSoonDeadInstToWorklist(*II, 0); |
| 1469 | return New; |
| 1470 | } |
| 1471 | } |
| 1472 | |
| 1473 | // Output elements are undefined if both are undefined. Consider things |
| 1474 | // like undef&0. The result is known zero, not undef. |
| 1475 | UndefElts &= UndefElts2; |
| 1476 | break; |
| 1477 | } |
| 1478 | break; |
| 1479 | } |
| 1480 | } |
| 1481 | return MadeChange ? I : 0; |
| 1482 | } |
| 1483 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1484 | /// @returns true if the specified compare instruction is |
| 1485 | /// true when both operands are equal... |
| 1486 | /// @brief Determine if the ICmpInst returns true if both operands are equal |
| 1487 | static bool isTrueWhenEqual(ICmpInst &ICI) { |
| 1488 | ICmpInst::Predicate pred = ICI.getPredicate(); |
| 1489 | return pred == ICmpInst::ICMP_EQ || pred == ICmpInst::ICMP_UGE || |
| 1490 | pred == ICmpInst::ICMP_SGE || pred == ICmpInst::ICMP_ULE || |
| 1491 | pred == ICmpInst::ICMP_SLE; |
| 1492 | } |
| 1493 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1494 | /// AssociativeOpt - Perform an optimization on an associative operator. This |
| 1495 | /// function is designed to check a chain of associative operators for a |
| 1496 | /// potential to apply a certain optimization. Since the optimization may be |
| 1497 | /// applicable if the expression was reassociated, this checks the chain, then |
| 1498 | /// reassociates the expression as necessary to expose the optimization |
| 1499 | /// opportunity. This makes use of a special Functor, which must define |
| 1500 | /// 'shouldApply' and 'apply' methods. |
| 1501 | /// |
| 1502 | template<typename Functor> |
| 1503 | Instruction *AssociativeOpt(BinaryOperator &Root, const Functor &F) { |
| 1504 | unsigned Opcode = Root.getOpcode(); |
| 1505 | Value *LHS = Root.getOperand(0); |
| 1506 | |
| 1507 | // Quick check, see if the immediate LHS matches... |
| 1508 | if (F.shouldApply(LHS)) |
| 1509 | return F.apply(Root); |
| 1510 | |
| 1511 | // Otherwise, if the LHS is not of the same opcode as the root, return. |
| 1512 | Instruction *LHSI = dyn_cast<Instruction>(LHS); |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 1513 | while (LHSI && LHSI->getOpcode() == Opcode && LHSI->hasOneUse()) { |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1514 | // Should we apply this transform to the RHS? |
| 1515 | bool ShouldApply = F.shouldApply(LHSI->getOperand(1)); |
| 1516 | |
| 1517 | // If not to the RHS, check to see if we should apply to the LHS... |
| 1518 | if (!ShouldApply && F.shouldApply(LHSI->getOperand(0))) { |
| 1519 | cast<BinaryOperator>(LHSI)->swapOperands(); // Make the LHS the RHS |
| 1520 | ShouldApply = true; |
| 1521 | } |
| 1522 | |
| 1523 | // If the functor wants to apply the optimization to the RHS of LHSI, |
| 1524 | // reassociate the expression from ((? op A) op B) to (? op (A op B)) |
| 1525 | if (ShouldApply) { |
| 1526 | BasicBlock *BB = Root.getParent(); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1527 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1528 | // Now all of the instructions are in the current basic block, go ahead |
| 1529 | // and perform the reassociation. |
| 1530 | Instruction *TmpLHSI = cast<Instruction>(Root.getOperand(0)); |
| 1531 | |
| 1532 | // First move the selected RHS to the LHS of the root... |
| 1533 | Root.setOperand(0, LHSI->getOperand(1)); |
| 1534 | |
| 1535 | // Make what used to be the LHS of the root be the user of the root... |
| 1536 | Value *ExtraOperand = TmpLHSI->getOperand(1); |
Chris Lattner | 284d3b0 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1537 | if (&Root == TmpLHSI) { |
Chris Lattner | 8953b90 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 1538 | Root.replaceAllUsesWith(Constant::getNullValue(TmpLHSI->getType())); |
| 1539 | return 0; |
| 1540 | } |
Chris Lattner | 284d3b0 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1541 | Root.replaceAllUsesWith(TmpLHSI); // Users now use TmpLHSI |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1542 | TmpLHSI->setOperand(1, &Root); // TmpLHSI now uses the root |
Chris Lattner | 284d3b0 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1543 | TmpLHSI->getParent()->getInstList().remove(TmpLHSI); |
| 1544 | BasicBlock::iterator ARI = &Root; ++ARI; |
| 1545 | BB->getInstList().insert(ARI, TmpLHSI); // Move TmpLHSI to after Root |
| 1546 | ARI = Root; |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1547 | |
| 1548 | // Now propagate the ExtraOperand down the chain of instructions until we |
| 1549 | // get to LHSI. |
| 1550 | while (TmpLHSI != LHSI) { |
| 1551 | Instruction *NextLHSI = cast<Instruction>(TmpLHSI->getOperand(0)); |
Chris Lattner | 284d3b0 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1552 | // Move the instruction to immediately before the chain we are |
| 1553 | // constructing to avoid breaking dominance properties. |
| 1554 | NextLHSI->getParent()->getInstList().remove(NextLHSI); |
| 1555 | BB->getInstList().insert(ARI, NextLHSI); |
| 1556 | ARI = NextLHSI; |
| 1557 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1558 | Value *NextOp = NextLHSI->getOperand(1); |
| 1559 | NextLHSI->setOperand(1, ExtraOperand); |
| 1560 | TmpLHSI = NextLHSI; |
| 1561 | ExtraOperand = NextOp; |
| 1562 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1563 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1564 | // Now that the instructions are reassociated, have the functor perform |
| 1565 | // the transformation... |
| 1566 | return F.apply(Root); |
| 1567 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1568 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1569 | LHSI = dyn_cast<Instruction>(LHSI->getOperand(0)); |
| 1570 | } |
| 1571 | return 0; |
| 1572 | } |
| 1573 | |
| 1574 | |
| 1575 | // AddRHS - Implements: X + X --> X << 1 |
| 1576 | struct AddRHS { |
| 1577 | Value *RHS; |
| 1578 | AddRHS(Value *rhs) : RHS(rhs) {} |
| 1579 | bool shouldApply(Value *LHS) const { return LHS == RHS; } |
| 1580 | Instruction *apply(BinaryOperator &Add) const { |
Reid Spencer | 0d5f923 | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 1581 | return BinaryOperator::createShl(Add.getOperand(0), |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1582 | ConstantInt::get(Add.getType(), 1)); |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1583 | } |
| 1584 | }; |
| 1585 | |
| 1586 | // AddMaskingAnd - Implements (A & C1)+(B & C2) --> (A & C1)|(B & C2) |
| 1587 | // iff C1&C2 == 0 |
| 1588 | struct AddMaskingAnd { |
| 1589 | Constant *C2; |
| 1590 | AddMaskingAnd(Constant *c) : C2(c) {} |
| 1591 | bool shouldApply(Value *LHS) const { |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1592 | ConstantInt *C1; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1593 | return match(LHS, m_And(m_Value(), m_ConstantInt(C1))) && |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1594 | ConstantExpr::getAnd(C1, C2)->isNullValue(); |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1595 | } |
| 1596 | Instruction *apply(BinaryOperator &Add) const { |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1597 | return BinaryOperator::createOr(Add.getOperand(0), Add.getOperand(1)); |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1598 | } |
| 1599 | }; |
| 1600 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1601 | static Value *FoldOperationIntoSelectOperand(Instruction &I, Value *SO, |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1602 | InstCombiner *IC) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1603 | if (CastInst *CI = dyn_cast<CastInst>(&I)) { |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1604 | if (Constant *SOC = dyn_cast<Constant>(SO)) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1605 | return ConstantExpr::getCast(CI->getOpcode(), SOC, I.getType()); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1606 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1607 | return IC->InsertNewInstBefore(CastInst::create( |
| 1608 | CI->getOpcode(), SO, I.getType(), SO->getName() + ".cast"), I); |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1609 | } |
| 1610 | |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1611 | // Figure out if the constant is the left or the right argument. |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1612 | bool ConstIsRHS = isa<Constant>(I.getOperand(1)); |
| 1613 | Constant *ConstOperand = cast<Constant>(I.getOperand(ConstIsRHS)); |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1614 | |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1615 | if (Constant *SOC = dyn_cast<Constant>(SO)) { |
| 1616 | if (ConstIsRHS) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1617 | return ConstantExpr::get(I.getOpcode(), SOC, ConstOperand); |
| 1618 | return ConstantExpr::get(I.getOpcode(), ConstOperand, SOC); |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1619 | } |
| 1620 | |
| 1621 | Value *Op0 = SO, *Op1 = ConstOperand; |
| 1622 | if (!ConstIsRHS) |
| 1623 | std::swap(Op0, Op1); |
| 1624 | Instruction *New; |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1625 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I)) |
| 1626 | New = BinaryOperator::create(BO->getOpcode(), Op0, Op1,SO->getName()+".op"); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1627 | else if (CmpInst *CI = dyn_cast<CmpInst>(&I)) |
| 1628 | New = CmpInst::create(CI->getOpcode(), CI->getPredicate(), Op0, Op1, |
| 1629 | SO->getName()+".cmp"); |
Chris Lattner | f9d9665 | 2004-04-10 19:15:56 +0000 | [diff] [blame] | 1630 | else { |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1631 | assert(0 && "Unknown binary instruction type!"); |
Chris Lattner | f9d9665 | 2004-04-10 19:15:56 +0000 | [diff] [blame] | 1632 | abort(); |
| 1633 | } |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1634 | return IC->InsertNewInstBefore(New, I); |
| 1635 | } |
| 1636 | |
| 1637 | // FoldOpIntoSelect - Given an instruction with a select as one operand and a |
| 1638 | // constant as the other operand, try to fold the binary operator into the |
| 1639 | // select arguments. This also works for Cast instructions, which obviously do |
| 1640 | // not have a second operand. |
| 1641 | static Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI, |
| 1642 | InstCombiner *IC) { |
| 1643 | // Don't modify shared select instructions |
| 1644 | if (!SI->hasOneUse()) return 0; |
| 1645 | Value *TV = SI->getOperand(1); |
| 1646 | Value *FV = SI->getOperand(2); |
| 1647 | |
| 1648 | if (isa<Constant>(TV) || isa<Constant>(FV)) { |
Chris Lattner | 374e659 | 2005-04-21 05:43:13 +0000 | [diff] [blame] | 1649 | // Bool selects with constant operands can be folded to logical ops. |
Reid Spencer | 542964f | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 1650 | if (SI->getType() == Type::Int1Ty) return 0; |
Chris Lattner | 374e659 | 2005-04-21 05:43:13 +0000 | [diff] [blame] | 1651 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1652 | Value *SelectTrueVal = FoldOperationIntoSelectOperand(Op, TV, IC); |
| 1653 | Value *SelectFalseVal = FoldOperationIntoSelectOperand(Op, FV, IC); |
| 1654 | |
| 1655 | return new SelectInst(SI->getCondition(), SelectTrueVal, |
| 1656 | SelectFalseVal); |
| 1657 | } |
| 1658 | return 0; |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1659 | } |
| 1660 | |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1661 | |
| 1662 | /// FoldOpIntoPhi - Given a binary operator or cast instruction which has a PHI |
| 1663 | /// node as operand #0, see if we can fold the instruction into the PHI (which |
| 1664 | /// is only possible if all operands to the PHI are constants). |
| 1665 | Instruction *InstCombiner::FoldOpIntoPhi(Instruction &I) { |
| 1666 | PHINode *PN = cast<PHINode>(I.getOperand(0)); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 1667 | unsigned NumPHIValues = PN->getNumIncomingValues(); |
Chris Lattner | 0468987 | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1668 | if (!PN->hasOneUse() || NumPHIValues == 0) return 0; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1669 | |
Chris Lattner | 0468987 | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1670 | // Check to see if all of the operands of the PHI are constants. If there is |
| 1671 | // one non-constant value, remember the BB it is. If there is more than one |
Chris Lattner | c4d8e7e | 2007-02-24 01:03:45 +0000 | [diff] [blame] | 1672 | // or if *it* is a PHI, bail out. |
Chris Lattner | 0468987 | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1673 | BasicBlock *NonConstBB = 0; |
| 1674 | for (unsigned i = 0; i != NumPHIValues; ++i) |
| 1675 | if (!isa<Constant>(PN->getIncomingValue(i))) { |
| 1676 | if (NonConstBB) return 0; // More than one non-const value. |
Chris Lattner | c4d8e7e | 2007-02-24 01:03:45 +0000 | [diff] [blame] | 1677 | if (isa<PHINode>(PN->getIncomingValue(i))) return 0; // Itself a phi. |
Chris Lattner | 0468987 | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1678 | NonConstBB = PN->getIncomingBlock(i); |
| 1679 | |
| 1680 | // If the incoming non-constant value is in I's block, we have an infinite |
| 1681 | // loop. |
| 1682 | if (NonConstBB == I.getParent()) |
| 1683 | return 0; |
| 1684 | } |
| 1685 | |
| 1686 | // If there is exactly one non-constant value, we can insert a copy of the |
| 1687 | // operation in that block. However, if this is a critical edge, we would be |
| 1688 | // inserting the computation one some other paths (e.g. inside a loop). Only |
| 1689 | // do this if the pred block is unconditionally branching into the phi block. |
| 1690 | if (NonConstBB) { |
| 1691 | BranchInst *BI = dyn_cast<BranchInst>(NonConstBB->getTerminator()); |
| 1692 | if (!BI || !BI->isUnconditional()) return 0; |
| 1693 | } |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1694 | |
| 1695 | // Okay, we can do the transformation: create the new PHI node. |
Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 1696 | PHINode *NewPN = new PHINode(I.getType(), ""); |
Chris Lattner | d8e2018 | 2005-01-29 00:39:08 +0000 | [diff] [blame] | 1697 | NewPN->reserveOperandSpace(PN->getNumOperands()/2); |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1698 | InsertNewInstBefore(NewPN, *PN); |
Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 1699 | NewPN->takeName(PN); |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1700 | |
| 1701 | // Next, add all of the operands to the PHI. |
| 1702 | if (I.getNumOperands() == 2) { |
| 1703 | Constant *C = cast<Constant>(I.getOperand(1)); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 1704 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
Chris Lattner | 0468987 | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1705 | Value *InV; |
| 1706 | if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1707 | if (CmpInst *CI = dyn_cast<CmpInst>(&I)) |
| 1708 | InV = ConstantExpr::getCompare(CI->getPredicate(), InC, C); |
| 1709 | else |
| 1710 | InV = ConstantExpr::get(I.getOpcode(), InC, C); |
Chris Lattner | 0468987 | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1711 | } else { |
| 1712 | assert(PN->getIncomingBlock(i) == NonConstBB); |
| 1713 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I)) |
| 1714 | InV = BinaryOperator::create(BO->getOpcode(), |
| 1715 | PN->getIncomingValue(i), C, "phitmp", |
| 1716 | NonConstBB->getTerminator()); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1717 | else if (CmpInst *CI = dyn_cast<CmpInst>(&I)) |
| 1718 | InV = CmpInst::create(CI->getOpcode(), |
| 1719 | CI->getPredicate(), |
| 1720 | PN->getIncomingValue(i), C, "phitmp", |
| 1721 | NonConstBB->getTerminator()); |
Chris Lattner | 0468987 | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1722 | else |
| 1723 | assert(0 && "Unknown binop!"); |
| 1724 | |
Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 1725 | AddToWorkList(cast<Instruction>(InV)); |
Chris Lattner | 0468987 | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1726 | } |
| 1727 | NewPN->addIncoming(InV, PN->getIncomingBlock(i)); |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1728 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1729 | } else { |
| 1730 | CastInst *CI = cast<CastInst>(&I); |
| 1731 | const Type *RetTy = CI->getType(); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 1732 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
Chris Lattner | 0468987 | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1733 | Value *InV; |
| 1734 | if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1735 | InV = ConstantExpr::getCast(CI->getOpcode(), InC, RetTy); |
Chris Lattner | 0468987 | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1736 | } else { |
| 1737 | assert(PN->getIncomingBlock(i) == NonConstBB); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1738 | InV = CastInst::create(CI->getOpcode(), PN->getIncomingValue(i), |
| 1739 | I.getType(), "phitmp", |
| 1740 | NonConstBB->getTerminator()); |
Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 1741 | AddToWorkList(cast<Instruction>(InV)); |
Chris Lattner | 0468987 | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1742 | } |
| 1743 | NewPN->addIncoming(InV, PN->getIncomingBlock(i)); |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1744 | } |
| 1745 | } |
| 1746 | return ReplaceInstUsesWith(I, NewPN); |
| 1747 | } |
| 1748 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1749 | Instruction *InstCombiner::visitAdd(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 1750 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1751 | Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 1752 | |
Chris Lattner | cf4a996 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 1753 | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 1754 | // X + undef -> undef |
| 1755 | if (isa<UndefValue>(RHS)) |
| 1756 | return ReplaceInstUsesWith(I, RHS); |
| 1757 | |
Chris Lattner | cf4a996 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 1758 | // X + 0 --> X |
Chris Lattner | 7a002fe | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 1759 | if (!I.getType()->isFPOrFPVector()) { // NOTE: -0 + +0 = +0. |
Chris Lattner | 7fde91e | 2005-10-17 17:56:38 +0000 | [diff] [blame] | 1760 | if (RHSC->isNullValue()) |
| 1761 | return ReplaceInstUsesWith(I, LHS); |
Chris Lattner | da1b152 | 2005-10-17 20:18:38 +0000 | [diff] [blame] | 1762 | } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) { |
| 1763 | if (CFP->isExactlyValue(-0.0)) |
| 1764 | return ReplaceInstUsesWith(I, LHS); |
Chris Lattner | 7fde91e | 2005-10-17 17:56:38 +0000 | [diff] [blame] | 1765 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1766 | |
Chris Lattner | cf4a996 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 1767 | if (ConstantInt *CI = dyn_cast<ConstantInt>(RHSC)) { |
Chris Lattner | 6e2c15c | 2006-11-09 05:12:27 +0000 | [diff] [blame] | 1768 | // X + (signbit) --> X ^ signbit |
Chris Lattner | 92a6865 | 2006-02-07 08:05:22 +0000 | [diff] [blame] | 1769 | uint64_t Val = CI->getZExtValue(); |
Chris Lattner | 77defba | 2006-02-07 07:00:41 +0000 | [diff] [blame] | 1770 | if (Val == (1ULL << (CI->getType()->getPrimitiveSizeInBits()-1))) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1771 | return BinaryOperator::createXor(LHS, RHS); |
Chris Lattner | 6e2c15c | 2006-11-09 05:12:27 +0000 | [diff] [blame] | 1772 | |
| 1773 | // See if SimplifyDemandedBits can simplify this. This handles stuff like |
| 1774 | // (X & 254)+1 -> (X&254)|1 |
| 1775 | uint64_t KnownZero, KnownOne; |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1776 | if (!isa<VectorType>(I.getType()) && |
Reid Spencer | a94d394 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 1777 | SimplifyDemandedBits(&I, cast<IntegerType>(I.getType())->getBitMask(), |
Chris Lattner | 6e2c15c | 2006-11-09 05:12:27 +0000 | [diff] [blame] | 1778 | KnownZero, KnownOne)) |
| 1779 | return &I; |
Chris Lattner | cf4a996 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 1780 | } |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1781 | |
| 1782 | if (isa<PHINode>(LHS)) |
| 1783 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 1784 | return NV; |
Chris Lattner | 0b3557f | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1785 | |
Chris Lattner | 330628a | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 1786 | ConstantInt *XorRHS = 0; |
| 1787 | Value *XorLHS = 0; |
Chris Lattner | 4284f64 | 2007-01-30 22:32:46 +0000 | [diff] [blame] | 1788 | if (isa<ConstantInt>(RHSC) && |
| 1789 | match(LHS, m_Xor(m_Value(XorLHS), m_ConstantInt(XorRHS)))) { |
Chris Lattner | 0b3557f | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1790 | unsigned TySizeBits = I.getType()->getPrimitiveSizeInBits(); |
| 1791 | int64_t RHSSExt = cast<ConstantInt>(RHSC)->getSExtValue(); |
| 1792 | uint64_t RHSZExt = cast<ConstantInt>(RHSC)->getZExtValue(); |
| 1793 | |
| 1794 | uint64_t C0080Val = 1ULL << 31; |
| 1795 | int64_t CFF80Val = -C0080Val; |
| 1796 | unsigned Size = 32; |
| 1797 | do { |
| 1798 | if (TySizeBits > Size) { |
| 1799 | bool Found = false; |
| 1800 | // If we have ADD(XOR(AND(X, 0xFF), 0x80), 0xF..F80), it's a sext. |
| 1801 | // If we have ADD(XOR(AND(X, 0xFF), 0xF..F80), 0x80), it's a sext. |
| 1802 | if (RHSSExt == CFF80Val) { |
| 1803 | if (XorRHS->getZExtValue() == C0080Val) |
| 1804 | Found = true; |
| 1805 | } else if (RHSZExt == C0080Val) { |
| 1806 | if (XorRHS->getSExtValue() == CFF80Val) |
| 1807 | Found = true; |
| 1808 | } |
| 1809 | if (Found) { |
| 1810 | // This is a sign extend if the top bits are known zero. |
Chris Lattner | 4534dd59 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 1811 | uint64_t Mask = ~0ULL; |
Chris Lattner | c3ebf40 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 1812 | Mask <<= 64-(TySizeBits-Size); |
Reid Spencer | a94d394 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 1813 | Mask &= cast<IntegerType>(XorLHS->getType())->getBitMask(); |
Chris Lattner | c3ebf40 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 1814 | if (!MaskedValueIsZero(XorLHS, Mask)) |
Chris Lattner | 0b3557f | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1815 | Size = 0; // Not a sign ext, but can't be any others either. |
| 1816 | goto FoundSExt; |
| 1817 | } |
| 1818 | } |
| 1819 | Size >>= 1; |
| 1820 | C0080Val >>= Size; |
| 1821 | CFF80Val >>= Size; |
| 1822 | } while (Size >= 8); |
| 1823 | |
| 1824 | FoundSExt: |
| 1825 | const Type *MiddleType = 0; |
| 1826 | switch (Size) { |
| 1827 | default: break; |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 1828 | case 32: MiddleType = Type::Int32Ty; break; |
| 1829 | case 16: MiddleType = Type::Int16Ty; break; |
| 1830 | case 8: MiddleType = Type::Int8Ty; break; |
Chris Lattner | 0b3557f | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1831 | } |
| 1832 | if (MiddleType) { |
Reid Spencer | bb65ebf | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 1833 | Instruction *NewTrunc = new TruncInst(XorLHS, MiddleType, "sext"); |
Chris Lattner | 0b3557f | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1834 | InsertNewInstBefore(NewTrunc, I); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1835 | return new SExtInst(NewTrunc, I.getType()); |
Chris Lattner | 0b3557f | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1836 | } |
| 1837 | } |
Chris Lattner | cf4a996 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 1838 | } |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 1839 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1840 | // X + X --> X << 1 |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1841 | if (I.getType()->isInteger() && I.getType() != Type::Int1Ty) { |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1842 | if (Instruction *Result = AssociativeOpt(I, AddRHS(RHS))) return Result; |
Chris Lattner | 4706046 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 1843 | |
| 1844 | if (Instruction *RHSI = dyn_cast<Instruction>(RHS)) { |
| 1845 | if (RHSI->getOpcode() == Instruction::Sub) |
| 1846 | if (LHS == RHSI->getOperand(1)) // A + (B - A) --> B |
| 1847 | return ReplaceInstUsesWith(I, RHSI->getOperand(0)); |
| 1848 | } |
| 1849 | if (Instruction *LHSI = dyn_cast<Instruction>(LHS)) { |
| 1850 | if (LHSI->getOpcode() == Instruction::Sub) |
| 1851 | if (RHS == LHSI->getOperand(1)) // (B - A) + A --> B |
| 1852 | return ReplaceInstUsesWith(I, LHSI->getOperand(0)); |
| 1853 | } |
Robert Bocchino | 7b5b86c | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 1854 | } |
Chris Lattner | ede3fe0 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 1855 | |
Chris Lattner | 147e975 | 2002-05-08 22:46:53 +0000 | [diff] [blame] | 1856 | // -A + B --> B - A |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 1857 | if (Value *V = dyn_castNegVal(LHS)) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1858 | return BinaryOperator::createSub(RHS, V); |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 1859 | |
| 1860 | // A + -B --> A - B |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 1861 | if (!isa<Constant>(RHS)) |
| 1862 | if (Value *V = dyn_castNegVal(RHS)) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1863 | return BinaryOperator::createSub(LHS, V); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1864 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1865 | |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 1866 | ConstantInt *C2; |
| 1867 | if (Value *X = dyn_castFoldableMul(LHS, C2)) { |
| 1868 | if (X == RHS) // X*C + X --> X * (C+1) |
| 1869 | return BinaryOperator::createMul(RHS, AddOne(C2)); |
| 1870 | |
| 1871 | // X*C1 + X*C2 --> X * (C1+C2) |
| 1872 | ConstantInt *C1; |
| 1873 | if (X == dyn_castFoldableMul(RHS, C1)) |
| 1874 | return BinaryOperator::createMul(X, ConstantExpr::getAdd(C1, C2)); |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 1875 | } |
| 1876 | |
| 1877 | // X + X*C --> X * (C+1) |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 1878 | if (dyn_castFoldableMul(RHS, C2) == LHS) |
| 1879 | return BinaryOperator::createMul(LHS, AddOne(C2)); |
| 1880 | |
Chris Lattner | 23eb8ec | 2007-01-05 02:17:46 +0000 | [diff] [blame] | 1881 | // X + ~X --> -1 since ~X = -X-1 |
| 1882 | if (dyn_castNotVal(LHS) == RHS || |
| 1883 | dyn_castNotVal(RHS) == LHS) |
| 1884 | return ReplaceInstUsesWith(I, ConstantInt::getAllOnesValue(I.getType())); |
| 1885 | |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 1886 | |
Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1887 | // (A & C1)+(B & C2) --> (A & C1)|(B & C2) iff C1&C2 == 0 |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1888 | if (match(RHS, m_And(m_Value(), m_ConstantInt(C2)))) |
Chris Lattner | 23eb8ec | 2007-01-05 02:17:46 +0000 | [diff] [blame] | 1889 | if (Instruction *R = AssociativeOpt(I, AddMaskingAnd(C2))) |
| 1890 | return R; |
Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 1891 | |
Chris Lattner | b9cde76 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 1892 | if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) { |
Chris Lattner | 330628a | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 1893 | Value *X = 0; |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1894 | if (match(LHS, m_Not(m_Value(X)))) { // ~X + C --> (C-1) - X |
| 1895 | Constant *C= ConstantExpr::getSub(CRHS, ConstantInt::get(I.getType(), 1)); |
| 1896 | return BinaryOperator::createSub(C, X); |
Chris Lattner | b9cde76 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 1897 | } |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1898 | |
Chris Lattner | bff91d9 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 1899 | // (X & FF00) + xx00 -> (X+xx00) & FF00 |
| 1900 | if (LHS->hasOneUse() && match(LHS, m_And(m_Value(X), m_ConstantInt(C2)))) { |
| 1901 | Constant *Anded = ConstantExpr::getAnd(CRHS, C2); |
| 1902 | if (Anded == CRHS) { |
| 1903 | // See if all bits from the first bit set in the Add RHS up are included |
| 1904 | // in the mask. First, get the rightmost bit. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1905 | uint64_t AddRHSV = CRHS->getZExtValue(); |
Chris Lattner | bff91d9 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 1906 | |
| 1907 | // Form a mask of all bits from the lowest bit added through the top. |
| 1908 | uint64_t AddRHSHighBits = ~((AddRHSV & -AddRHSV)-1); |
Reid Spencer | a94d394 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 1909 | AddRHSHighBits &= C2->getType()->getBitMask(); |
Chris Lattner | bff91d9 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 1910 | |
| 1911 | // See if the and mask includes all of these bits. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1912 | uint64_t AddRHSHighBitsAnd = AddRHSHighBits & C2->getZExtValue(); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1913 | |
Chris Lattner | bff91d9 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 1914 | if (AddRHSHighBits == AddRHSHighBitsAnd) { |
| 1915 | // Okay, the xform is safe. Insert the new add pronto. |
| 1916 | Value *NewAdd = InsertNewInstBefore(BinaryOperator::createAdd(X, CRHS, |
| 1917 | LHS->getName()), I); |
| 1918 | return BinaryOperator::createAnd(NewAdd, C2); |
| 1919 | } |
| 1920 | } |
| 1921 | } |
| 1922 | |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1923 | // Try to fold constant add into select arguments. |
| 1924 | if (SelectInst *SI = dyn_cast<SelectInst>(LHS)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1925 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1926 | return R; |
Chris Lattner | b9cde76 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 1927 | } |
| 1928 | |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1929 | // add (cast *A to intptrtype) B -> |
| 1930 | // cast (GEP (cast *A to sbyte*) B) -> |
| 1931 | // intptrtype |
Andrew Lenharth | 4f339be | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 1932 | { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1933 | CastInst *CI = dyn_cast<CastInst>(LHS); |
| 1934 | Value *Other = RHS; |
Andrew Lenharth | 4f339be | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 1935 | if (!CI) { |
| 1936 | CI = dyn_cast<CastInst>(RHS); |
| 1937 | Other = LHS; |
| 1938 | } |
Andrew Lenharth | 44cb67a | 2006-09-20 15:37:57 +0000 | [diff] [blame] | 1939 | if (CI && CI->getType()->isSized() && |
Reid Spencer | 8f166b0 | 2007-01-08 16:32:00 +0000 | [diff] [blame] | 1940 | (CI->getType()->getPrimitiveSizeInBits() == |
| 1941 | TD->getIntPtrType()->getPrimitiveSizeInBits()) |
Andrew Lenharth | 44cb67a | 2006-09-20 15:37:57 +0000 | [diff] [blame] | 1942 | && isa<PointerType>(CI->getOperand(0)->getType())) { |
Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 1943 | Value *I2 = InsertCastBefore(Instruction::BitCast, CI->getOperand(0), |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 1944 | PointerType::get(Type::Int8Ty), I); |
Andrew Lenharth | 44cb67a | 2006-09-20 15:37:57 +0000 | [diff] [blame] | 1945 | I2 = InsertNewInstBefore(new GetElementPtrInst(I2, Other, "ctg2"), I); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1946 | return new PtrToIntInst(I2, CI->getType()); |
Andrew Lenharth | 4f339be | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 1947 | } |
| 1948 | } |
| 1949 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1950 | return Changed ? &I : 0; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1951 | } |
| 1952 | |
Chris Lattner | bdb0ce0 | 2003-07-22 21:46:59 +0000 | [diff] [blame] | 1953 | // isSignBit - Return true if the value represented by the constant only has the |
| 1954 | // highest order bit set. |
| 1955 | static bool isSignBit(ConstantInt *CI) { |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 1956 | unsigned NumBits = CI->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1957 | return (CI->getZExtValue() & (~0ULL >> (64-NumBits))) == (1ULL << (NumBits-1)); |
Chris Lattner | bdb0ce0 | 2003-07-22 21:46:59 +0000 | [diff] [blame] | 1958 | } |
| 1959 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1960 | Instruction *InstCombiner::visitSub(BinaryOperator &I) { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1961 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1962 | |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1963 | if (Op0 == Op1) // sub X, X -> 0 |
| 1964 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1965 | |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1966 | // If this is a 'B = x-(-A)', change to B = x+A... |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 1967 | if (Value *V = dyn_castNegVal(Op1)) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1968 | return BinaryOperator::createAdd(Op0, V); |
Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 1969 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 1970 | if (isa<UndefValue>(Op0)) |
| 1971 | return ReplaceInstUsesWith(I, Op0); // undef - X -> undef |
| 1972 | if (isa<UndefValue>(Op1)) |
| 1973 | return ReplaceInstUsesWith(I, Op1); // X - undef -> undef |
| 1974 | |
Chris Lattner | 8f2f598 | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 1975 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op0)) { |
| 1976 | // Replace (-1 - A) with (~A)... |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1977 | if (C->isAllOnesValue()) |
| 1978 | return BinaryOperator::createNot(Op1); |
Chris Lattner | ad3c495 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 1979 | |
Chris Lattner | 8f2f598 | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 1980 | // C - ~X == X + (1+C) |
Reid Spencer | 4fdd96c | 2005-06-18 17:37:34 +0000 | [diff] [blame] | 1981 | Value *X = 0; |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1982 | if (match(Op1, m_Not(m_Value(X)))) |
| 1983 | return BinaryOperator::createAdd(X, |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1984 | ConstantExpr::getAdd(C, ConstantInt::get(I.getType(), 1))); |
Chris Lattner | 27df1db | 2007-01-15 07:02:54 +0000 | [diff] [blame] | 1985 | // -(X >>u 31) -> (X >>s 31) |
| 1986 | // -(X >>s 31) -> (X >>u 31) |
Chris Lattner | 022167f | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 1987 | if (C->isNullValue()) { |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1988 | if (BinaryOperator *SI = dyn_cast<BinaryOperator>(Op1)) |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1989 | if (SI->getOpcode() == Instruction::LShr) { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1990 | if (ConstantInt *CU = dyn_cast<ConstantInt>(SI->getOperand(1))) { |
Chris Lattner | 92295c5 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 1991 | // Check to see if we are shifting out everything but the sign bit. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1992 | if (CU->getZExtValue() == |
| 1993 | SI->getType()->getPrimitiveSizeInBits()-1) { |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1994 | // Ok, the transformation is safe. Insert AShr. |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1995 | return BinaryOperator::create(Instruction::AShr, |
| 1996 | SI->getOperand(0), CU, SI->getName()); |
Chris Lattner | 92295c5 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 1997 | } |
| 1998 | } |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1999 | } |
| 2000 | else if (SI->getOpcode() == Instruction::AShr) { |
| 2001 | if (ConstantInt *CU = dyn_cast<ConstantInt>(SI->getOperand(1))) { |
| 2002 | // Check to see if we are shifting out everything but the sign bit. |
| 2003 | if (CU->getZExtValue() == |
| 2004 | SI->getType()->getPrimitiveSizeInBits()-1) { |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 2005 | // Ok, the transformation is safe. Insert LShr. |
Reid Spencer | 0d5f923 | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 2006 | return BinaryOperator::createLShr( |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2007 | SI->getOperand(0), CU, SI->getName()); |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2008 | } |
| 2009 | } |
| 2010 | } |
Chris Lattner | 022167f | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 2011 | } |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2012 | |
| 2013 | // Try to fold constant sub into select arguments. |
| 2014 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2015 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2016 | return R; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2017 | |
| 2018 | if (isa<PHINode>(Op0)) |
| 2019 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2020 | return NV; |
Chris Lattner | 8f2f598 | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 2021 | } |
| 2022 | |
Chris Lattner | a9be449 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2023 | if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) { |
| 2024 | if (Op1I->getOpcode() == Instruction::Add && |
Chris Lattner | 7a002fe | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 2025 | !Op0->getType()->isFPOrFPVector()) { |
Chris Lattner | c7f3c1a | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2026 | if (Op1I->getOperand(0) == Op0) // X-(X+Y) == -Y |
Chris Lattner | a9be449 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2027 | return BinaryOperator::createNeg(Op1I->getOperand(1), I.getName()); |
Chris Lattner | c7f3c1a | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2028 | else if (Op1I->getOperand(1) == Op0) // X-(Y+X) == -Y |
Chris Lattner | a9be449 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2029 | return BinaryOperator::createNeg(Op1I->getOperand(0), I.getName()); |
Chris Lattner | c7f3c1a | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2030 | else if (ConstantInt *CI1 = dyn_cast<ConstantInt>(I.getOperand(0))) { |
| 2031 | if (ConstantInt *CI2 = dyn_cast<ConstantInt>(Op1I->getOperand(1))) |
| 2032 | // C1-(X+C2) --> (C1-C2)-X |
| 2033 | return BinaryOperator::createSub(ConstantExpr::getSub(CI1, CI2), |
| 2034 | Op1I->getOperand(0)); |
| 2035 | } |
Chris Lattner | a9be449 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2036 | } |
| 2037 | |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 2038 | if (Op1I->hasOneUse()) { |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2039 | // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression |
| 2040 | // is not used by anyone else... |
| 2041 | // |
Chris Lattner | c2f0aa5 | 2004-02-02 20:09:56 +0000 | [diff] [blame] | 2042 | if (Op1I->getOpcode() == Instruction::Sub && |
Chris Lattner | 7a002fe | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 2043 | !Op1I->getType()->isFPOrFPVector()) { |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2044 | // Swap the two operands of the subexpr... |
| 2045 | Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1); |
| 2046 | Op1I->setOperand(0, IIOp1); |
| 2047 | Op1I->setOperand(1, IIOp0); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2048 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2049 | // Create the new top level add instruction... |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2050 | return BinaryOperator::createAdd(Op0, Op1); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2051 | } |
| 2052 | |
| 2053 | // Replace (A - (A & B)) with (A & ~B) if this is the only use of (A&B)... |
| 2054 | // |
| 2055 | if (Op1I->getOpcode() == Instruction::And && |
| 2056 | (Op1I->getOperand(0) == Op0 || Op1I->getOperand(1) == Op0)) { |
| 2057 | Value *OtherOp = Op1I->getOperand(Op1I->getOperand(0) == Op0); |
| 2058 | |
Chris Lattner | 396dbfe | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 2059 | Value *NewNot = |
| 2060 | InsertNewInstBefore(BinaryOperator::createNot(OtherOp, "B.not"), I); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2061 | return BinaryOperator::createAnd(Op0, NewNot); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2062 | } |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2063 | |
Reid Spencer | 3c51495 | 2006-10-16 23:08:08 +0000 | [diff] [blame] | 2064 | // 0 - (X sdiv C) -> (X sdiv -C) |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2065 | if (Op1I->getOpcode() == Instruction::SDiv) |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2066 | if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0)) |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2067 | if (CSI->isNullValue()) |
Chris Lattner | 0aee4b7 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 2068 | if (Constant *DivRHS = dyn_cast<Constant>(Op1I->getOperand(1))) |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2069 | return BinaryOperator::createSDiv(Op1I->getOperand(0), |
Chris Lattner | 0aee4b7 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 2070 | ConstantExpr::getNeg(DivRHS)); |
| 2071 | |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2072 | // X - X*C --> X * (1-C) |
Reid Spencer | 4fdd96c | 2005-06-18 17:37:34 +0000 | [diff] [blame] | 2073 | ConstantInt *C2 = 0; |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2074 | if (dyn_castFoldableMul(Op1I, C2) == Op0) { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2075 | Constant *CP1 = |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2076 | ConstantExpr::getSub(ConstantInt::get(I.getType(), 1), C2); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2077 | return BinaryOperator::createMul(Op0, CP1); |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2078 | } |
Chris Lattner | ad3c495 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 2079 | } |
Chris Lattner | a9be449 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2080 | } |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2081 | |
Chris Lattner | 7a002fe | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 2082 | if (!Op0->getType()->isFPOrFPVector()) |
Chris Lattner | 4706046 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 2083 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) |
| 2084 | if (Op0I->getOpcode() == Instruction::Add) { |
Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 2085 | if (Op0I->getOperand(0) == Op1) // (Y+X)-Y == X |
| 2086 | return ReplaceInstUsesWith(I, Op0I->getOperand(1)); |
| 2087 | else if (Op0I->getOperand(1) == Op1) // (X+Y)-Y == X |
| 2088 | return ReplaceInstUsesWith(I, Op0I->getOperand(0)); |
Chris Lattner | 4706046 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 2089 | } else if (Op0I->getOpcode() == Instruction::Sub) { |
| 2090 | if (Op0I->getOperand(0) == Op1) // (X-Y)-X == -Y |
| 2091 | return BinaryOperator::createNeg(Op0I->getOperand(1), I.getName()); |
Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 2092 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2093 | |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2094 | ConstantInt *C1; |
| 2095 | if (Value *X = dyn_castFoldableMul(Op0, C1)) { |
| 2096 | if (X == Op1) { // X*C - X --> X * (C-1) |
| 2097 | Constant *CP1 = ConstantExpr::getSub(C1, ConstantInt::get(I.getType(),1)); |
| 2098 | return BinaryOperator::createMul(Op1, CP1); |
| 2099 | } |
Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2100 | |
Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2101 | ConstantInt *C2; // X*C1 - X*C2 -> X * (C1-C2) |
| 2102 | if (X == dyn_castFoldableMul(Op1, C2)) |
| 2103 | return BinaryOperator::createMul(Op1, ConstantExpr::getSub(C1, C2)); |
| 2104 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2105 | return 0; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2106 | } |
| 2107 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2108 | /// isSignBitCheck - Given an exploded icmp instruction, return true if it |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2109 | /// really just returns true if the most significant (sign) bit is set. |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2110 | static bool isSignBitCheck(ICmpInst::Predicate pred, ConstantInt *RHS) { |
| 2111 | switch (pred) { |
| 2112 | case ICmpInst::ICMP_SLT: |
| 2113 | // True if LHS s< RHS and RHS == 0 |
| 2114 | return RHS->isNullValue(); |
| 2115 | case ICmpInst::ICMP_SLE: |
| 2116 | // True if LHS s<= RHS and RHS == -1 |
| 2117 | return RHS->isAllOnesValue(); |
| 2118 | case ICmpInst::ICMP_UGE: |
| 2119 | // True if LHS u>= RHS and RHS == high-bit-mask (2^7, 2^15, 2^31, etc) |
| 2120 | return RHS->getZExtValue() == (1ULL << |
| 2121 | (RHS->getType()->getPrimitiveSizeInBits()-1)); |
| 2122 | case ICmpInst::ICMP_UGT: |
| 2123 | // True if LHS u> RHS and RHS == high-bit-mask - 1 |
| 2124 | return RHS->getZExtValue() == |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 2125 | (1ULL << (RHS->getType()->getPrimitiveSizeInBits()-1))-1; |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2126 | default: |
| 2127 | return false; |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2128 | } |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2129 | } |
| 2130 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2131 | Instruction *InstCombiner::visitMul(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 2132 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2133 | Value *Op0 = I.getOperand(0); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2134 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2135 | if (isa<UndefValue>(I.getOperand(1))) // undef * X -> 0 |
| 2136 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2137 | |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2138 | // Simplify mul instructions with a constant RHS... |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2139 | if (Constant *Op1 = dyn_cast<Constant>(I.getOperand(1))) { |
| 2140 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | ede3fe0 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 2141 | |
| 2142 | // ((X << C1)*C2) == (X * (C2 << C1)) |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2143 | if (BinaryOperator *SI = dyn_cast<BinaryOperator>(Op0)) |
Chris Lattner | ede3fe0 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 2144 | if (SI->getOpcode() == Instruction::Shl) |
| 2145 | if (Constant *ShOp = dyn_cast<Constant>(SI->getOperand(1))) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2146 | return BinaryOperator::createMul(SI->getOperand(0), |
| 2147 | ConstantExpr::getShl(CI, ShOp)); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2148 | |
Chris Lattner | cce81be | 2003-09-11 22:24:54 +0000 | [diff] [blame] | 2149 | if (CI->isNullValue()) |
| 2150 | return ReplaceInstUsesWith(I, Op1); // X * 0 == 0 |
| 2151 | if (CI->equalsInt(1)) // X * 1 == X |
| 2152 | return ReplaceInstUsesWith(I, Op0); |
| 2153 | if (CI->isAllOnesValue()) // X * -1 == 0 - X |
Chris Lattner | 35236d8 | 2003-06-25 17:09:20 +0000 | [diff] [blame] | 2154 | return BinaryOperator::createNeg(Op0, I.getName()); |
Chris Lattner | 31ba129 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 2155 | |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2156 | int64_t Val = (int64_t)cast<ConstantInt>(CI)->getZExtValue(); |
Chris Lattner | 22d00a8 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 2157 | if (isPowerOf2_64(Val)) { // Replace X*(2^C) with X << C |
| 2158 | uint64_t C = Log2_64(Val); |
Reid Spencer | 0d5f923 | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 2159 | return BinaryOperator::createShl(Op0, |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2160 | ConstantInt::get(Op0->getType(), C)); |
Chris Lattner | 22d00a8 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 2161 | } |
Robert Bocchino | 7b5b86c | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 2162 | } else if (ConstantFP *Op1F = dyn_cast<ConstantFP>(Op1)) { |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2163 | if (Op1F->isNullValue()) |
| 2164 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 31ba129 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 2165 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2166 | // "In IEEE floating point, x*1 is not equivalent to x for nans. However, |
| 2167 | // ANSI says we can drop signals, so we can do this anyway." (from GCC) |
| 2168 | if (Op1F->getValue() == 1.0) |
| 2169 | return ReplaceInstUsesWith(I, Op0); // Eliminate 'mul double %X, 1.0' |
| 2170 | } |
Chris Lattner | 32c01df | 2006-03-04 06:04:02 +0000 | [diff] [blame] | 2171 | |
| 2172 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) |
| 2173 | if (Op0I->getOpcode() == Instruction::Add && Op0I->hasOneUse() && |
| 2174 | isa<ConstantInt>(Op0I->getOperand(1))) { |
| 2175 | // Canonicalize (X+C1)*C2 -> X*C2+C1*C2. |
| 2176 | Instruction *Add = BinaryOperator::createMul(Op0I->getOperand(0), |
| 2177 | Op1, "tmp"); |
| 2178 | InsertNewInstBefore(Add, I); |
| 2179 | Value *C1C2 = ConstantExpr::getMul(Op1, |
| 2180 | cast<Constant>(Op0I->getOperand(1))); |
| 2181 | return BinaryOperator::createAdd(Add, C1C2); |
| 2182 | |
| 2183 | } |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2184 | |
| 2185 | // Try to fold constant mul into select arguments. |
| 2186 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2187 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2188 | return R; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2189 | |
| 2190 | if (isa<PHINode>(Op0)) |
| 2191 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2192 | return NV; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2193 | } |
| 2194 | |
Chris Lattner | 934a64cf | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 2195 | if (Value *Op0v = dyn_castNegVal(Op0)) // -X * -Y = X*Y |
| 2196 | if (Value *Op1v = dyn_castNegVal(I.getOperand(1))) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2197 | return BinaryOperator::createMul(Op0v, Op1v); |
Chris Lattner | 934a64cf | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 2198 | |
Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2199 | // If one of the operands of the multiply is a cast from a boolean value, then |
| 2200 | // we know the bool is either zero or one, so this is a 'masking' multiply. |
| 2201 | // See if we can simplify things based on how the boolean was originally |
| 2202 | // formed. |
| 2203 | CastInst *BoolCast = 0; |
Reid Spencer | 74a528b | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 2204 | if (ZExtInst *CI = dyn_cast<ZExtInst>(I.getOperand(0))) |
Reid Spencer | 542964f | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 2205 | if (CI->getOperand(0)->getType() == Type::Int1Ty) |
Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2206 | BoolCast = CI; |
| 2207 | if (!BoolCast) |
Reid Spencer | 74a528b | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 2208 | if (ZExtInst *CI = dyn_cast<ZExtInst>(I.getOperand(1))) |
Reid Spencer | 542964f | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 2209 | if (CI->getOperand(0)->getType() == Type::Int1Ty) |
Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2210 | BoolCast = CI; |
| 2211 | if (BoolCast) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2212 | if (ICmpInst *SCI = dyn_cast<ICmpInst>(BoolCast->getOperand(0))) { |
Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2213 | Value *SCIOp0 = SCI->getOperand(0), *SCIOp1 = SCI->getOperand(1); |
| 2214 | const Type *SCOpTy = SCIOp0->getType(); |
| 2215 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2216 | // If the icmp is true iff the sign bit of X is set, then convert this |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2217 | // multiply into a shift/and combination. |
| 2218 | if (isa<ConstantInt>(SCIOp1) && |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2219 | isSignBitCheck(SCI->getPredicate(), cast<ConstantInt>(SCIOp1))) { |
Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2220 | // Shift the X value right to turn it into "all signbits". |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2221 | Constant *Amt = ConstantInt::get(SCIOp0->getType(), |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 2222 | SCOpTy->getPrimitiveSizeInBits()-1); |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2223 | Value *V = |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2224 | InsertNewInstBefore( |
| 2225 | BinaryOperator::create(Instruction::AShr, SCIOp0, Amt, |
Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2226 | BoolCast->getOperand(0)->getName()+ |
| 2227 | ".mask"), I); |
Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2228 | |
| 2229 | // If the multiply type is not the same as the source type, sign extend |
| 2230 | // or truncate to the multiply type. |
Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 2231 | if (I.getType() != V->getType()) { |
| 2232 | unsigned SrcBits = V->getType()->getPrimitiveSizeInBits(); |
| 2233 | unsigned DstBits = I.getType()->getPrimitiveSizeInBits(); |
| 2234 | Instruction::CastOps opcode = |
| 2235 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2236 | (SrcBits < DstBits ? Instruction::SExt : Instruction::Trunc)); |
| 2237 | V = InsertCastBefore(opcode, V, I.getType(), I); |
| 2238 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2239 | |
Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2240 | Value *OtherOp = Op0 == BoolCast ? I.getOperand(1) : Op0; |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2241 | return BinaryOperator::createAnd(V, OtherOp); |
Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2242 | } |
| 2243 | } |
| 2244 | } |
| 2245 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2246 | return Changed ? &I : 0; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2247 | } |
| 2248 | |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2249 | /// This function implements the transforms on div instructions that work |
| 2250 | /// regardless of the kind of div instruction it is (udiv, sdiv, or fdiv). It is |
| 2251 | /// used by the visitors to those instructions. |
| 2252 | /// @brief Transforms common to all three div instructions |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2253 | Instruction *InstCombiner::commonDivTransforms(BinaryOperator &I) { |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2254 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2255 | |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2256 | // undef / X -> 0 |
| 2257 | if (isa<UndefValue>(Op0)) |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2258 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2259 | |
| 2260 | // X / undef -> undef |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2261 | if (isa<UndefValue>(Op1)) |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2262 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2263 | |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2264 | // Handle cases involving: div X, (select Cond, Y, Z) |
Chris Lattner | d79dc79 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2265 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { |
| 2266 | // div X, (Cond ? 0 : Y) -> div X, Y. If the div and the select are in the |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2267 | // same basic block, then we replace the select with Y, and the condition |
| 2268 | // of the select with false (if the cond value is in the same BB). If the |
Chris Lattner | d79dc79 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2269 | // select has uses other than the div, this allows them to be simplified |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2270 | // also. Note that div X, Y is just as good as div X, 0 (undef) |
Chris Lattner | d79dc79 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2271 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(1))) |
| 2272 | if (ST->isNullValue()) { |
| 2273 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); |
| 2274 | if (CondI && CondI->getParent() == I.getParent()) |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2275 | UpdateValueUsesWith(CondI, ConstantInt::getFalse()); |
Chris Lattner | d79dc79 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2276 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
| 2277 | I.setOperand(1, SI->getOperand(2)); |
| 2278 | else |
| 2279 | UpdateValueUsesWith(SI, SI->getOperand(2)); |
| 2280 | return &I; |
| 2281 | } |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2282 | |
Chris Lattner | d79dc79 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2283 | // Likewise for: div X, (Cond ? Y : 0) -> div X, Y |
| 2284 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(2))) |
| 2285 | if (ST->isNullValue()) { |
| 2286 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); |
| 2287 | if (CondI && CondI->getParent() == I.getParent()) |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2288 | UpdateValueUsesWith(CondI, ConstantInt::getTrue()); |
Chris Lattner | d79dc79 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2289 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
| 2290 | I.setOperand(1, SI->getOperand(1)); |
| 2291 | else |
| 2292 | UpdateValueUsesWith(SI, SI->getOperand(1)); |
| 2293 | return &I; |
| 2294 | } |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2295 | } |
Chris Lattner | d79dc79 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2296 | |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2297 | return 0; |
| 2298 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2299 | |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2300 | /// This function implements the transforms common to both integer division |
| 2301 | /// instructions (udiv and sdiv). It is called by the visitors to those integer |
| 2302 | /// division instructions. |
| 2303 | /// @brief Common integer divide transforms |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2304 | Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) { |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2305 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2306 | |
| 2307 | if (Instruction *Common = commonDivTransforms(I)) |
| 2308 | return Common; |
| 2309 | |
| 2310 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
| 2311 | // div X, 1 == X |
| 2312 | if (RHS->equalsInt(1)) |
| 2313 | return ReplaceInstUsesWith(I, Op0); |
| 2314 | |
| 2315 | // (X / C1) / C2 -> X / (C1*C2) |
| 2316 | if (Instruction *LHS = dyn_cast<Instruction>(Op0)) |
| 2317 | if (Instruction::BinaryOps(LHS->getOpcode()) == I.getOpcode()) |
| 2318 | if (ConstantInt *LHSRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) { |
| 2319 | return BinaryOperator::create(I.getOpcode(), LHS->getOperand(0), |
| 2320 | ConstantExpr::getMul(RHS, LHSRHS)); |
Chris Lattner | 4236261 | 2005-04-08 04:03:26 +0000 | [diff] [blame] | 2321 | } |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2322 | |
| 2323 | if (!RHS->isNullValue()) { // avoid X udiv 0 |
| 2324 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 2325 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 2326 | return R; |
| 2327 | if (isa<PHINode>(Op0)) |
| 2328 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2329 | return NV; |
| 2330 | } |
Chris Lattner | d79dc79 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2331 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2332 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2333 | // 0 / X == 0, we don't need to preserve faults! |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2334 | if (ConstantInt *LHS = dyn_cast<ConstantInt>(Op0)) |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2335 | if (LHS->equalsInt(0)) |
| 2336 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2337 | |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2338 | return 0; |
| 2339 | } |
| 2340 | |
| 2341 | Instruction *InstCombiner::visitUDiv(BinaryOperator &I) { |
| 2342 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2343 | |
| 2344 | // Handle the integer div common cases |
| 2345 | if (Instruction *Common = commonIDivTransforms(I)) |
| 2346 | return Common; |
| 2347 | |
| 2348 | // X udiv C^2 -> X >> C |
| 2349 | // Check to see if this is an unsigned division with an exact power of 2, |
| 2350 | // if so, convert to a right shift. |
| 2351 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op1)) { |
| 2352 | if (uint64_t Val = C->getZExtValue()) // Don't break X / 0 |
| 2353 | if (isPowerOf2_64(Val)) { |
| 2354 | uint64_t ShiftAmt = Log2_64(Val); |
Reid Spencer | 0d5f923 | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 2355 | return BinaryOperator::createLShr(Op0, |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2356 | ConstantInt::get(Op0->getType(), ShiftAmt)); |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2357 | } |
| 2358 | } |
| 2359 | |
| 2360 | // X udiv (C1 << N), where C1 is "1<<C2" --> X >> (N+C2) |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2361 | if (BinaryOperator *RHSI = dyn_cast<BinaryOperator>(I.getOperand(1))) { |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2362 | if (RHSI->getOpcode() == Instruction::Shl && |
| 2363 | isa<ConstantInt>(RHSI->getOperand(0))) { |
| 2364 | uint64_t C1 = cast<ConstantInt>(RHSI->getOperand(0))->getZExtValue(); |
| 2365 | if (isPowerOf2_64(C1)) { |
| 2366 | Value *N = RHSI->getOperand(1); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2367 | const Type *NTy = N->getType(); |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2368 | if (uint64_t C2 = Log2_64(C1)) { |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2369 | Constant *C2V = ConstantInt::get(NTy, C2); |
| 2370 | N = InsertNewInstBefore(BinaryOperator::createAdd(N, C2V, "tmp"), I); |
Chris Lattner | 2e90b73 | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2371 | } |
Reid Spencer | 0d5f923 | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 2372 | return BinaryOperator::createLShr(Op0, N); |
Chris Lattner | 2e90b73 | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2373 | } |
| 2374 | } |
Chris Lattner | dd0c174 | 2005-11-05 07:40:31 +0000 | [diff] [blame] | 2375 | } |
| 2376 | |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2377 | // udiv X, (Select Cond, C1, C2) --> Select Cond, (shr X, C1), (shr X, C2) |
| 2378 | // where C1&C2 are powers of two. |
| 2379 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { |
| 2380 | if (ConstantInt *STO = dyn_cast<ConstantInt>(SI->getOperand(1))) |
| 2381 | if (ConstantInt *SFO = dyn_cast<ConstantInt>(SI->getOperand(2))) |
| 2382 | if (!STO->isNullValue() && !STO->isNullValue()) { |
| 2383 | uint64_t TVA = STO->getZExtValue(), FVA = SFO->getZExtValue(); |
| 2384 | if (isPowerOf2_64(TVA) && isPowerOf2_64(FVA)) { |
| 2385 | // Compute the shift amounts |
| 2386 | unsigned TSA = Log2_64(TVA), FSA = Log2_64(FVA); |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2387 | // Construct the "on true" case of the select |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2388 | Constant *TC = ConstantInt::get(Op0->getType(), TSA); |
Reid Spencer | 0d5f923 | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 2389 | Instruction *TSI = BinaryOperator::createLShr( |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2390 | Op0, TC, SI->getName()+".t"); |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2391 | TSI = InsertNewInstBefore(TSI, I); |
| 2392 | |
| 2393 | // Construct the "on false" case of the select |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2394 | Constant *FC = ConstantInt::get(Op0->getType(), FSA); |
Reid Spencer | 0d5f923 | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 2395 | Instruction *FSI = BinaryOperator::createLShr( |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2396 | Op0, FC, SI->getName()+".f"); |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2397 | FSI = InsertNewInstBefore(FSI, I); |
| 2398 | |
| 2399 | // construct the select instruction and return it. |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2400 | return new SelectInst(SI->getOperand(0), TSI, FSI, SI->getName()); |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2401 | } |
| 2402 | } |
| 2403 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2404 | return 0; |
| 2405 | } |
| 2406 | |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2407 | Instruction *InstCombiner::visitSDiv(BinaryOperator &I) { |
| 2408 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2409 | |
| 2410 | // Handle the integer div common cases |
| 2411 | if (Instruction *Common = commonIDivTransforms(I)) |
| 2412 | return Common; |
| 2413 | |
| 2414 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
| 2415 | // sdiv X, -1 == -X |
| 2416 | if (RHS->isAllOnesValue()) |
| 2417 | return BinaryOperator::createNeg(Op0); |
| 2418 | |
| 2419 | // -X/C -> X/-C |
| 2420 | if (Value *LHSNeg = dyn_castNegVal(Op0)) |
| 2421 | return BinaryOperator::createSDiv(LHSNeg, ConstantExpr::getNeg(RHS)); |
| 2422 | } |
| 2423 | |
| 2424 | // If the sign bits of both operands are zero (i.e. we can prove they are |
| 2425 | // unsigned inputs), turn this into a udiv. |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 2426 | if (I.getType()->isInteger()) { |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2427 | uint64_t Mask = 1ULL << (I.getType()->getPrimitiveSizeInBits()-1); |
| 2428 | if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) { |
| 2429 | return BinaryOperator::createUDiv(Op0, Op1, I.getName()); |
| 2430 | } |
| 2431 | } |
| 2432 | |
| 2433 | return 0; |
| 2434 | } |
| 2435 | |
| 2436 | Instruction *InstCombiner::visitFDiv(BinaryOperator &I) { |
| 2437 | return commonDivTransforms(I); |
| 2438 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2439 | |
Chris Lattner | 85dda9a | 2006-03-02 06:50:58 +0000 | [diff] [blame] | 2440 | /// GetFactor - If we can prove that the specified value is at least a multiple |
| 2441 | /// of some factor, return that factor. |
| 2442 | static Constant *GetFactor(Value *V) { |
| 2443 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) |
| 2444 | return CI; |
| 2445 | |
| 2446 | // Unless we can be tricky, we know this is a multiple of 1. |
| 2447 | Constant *Result = ConstantInt::get(V->getType(), 1); |
| 2448 | |
| 2449 | Instruction *I = dyn_cast<Instruction>(V); |
| 2450 | if (!I) return Result; |
| 2451 | |
| 2452 | if (I->getOpcode() == Instruction::Mul) { |
| 2453 | // Handle multiplies by a constant, etc. |
| 2454 | return ConstantExpr::getMul(GetFactor(I->getOperand(0)), |
| 2455 | GetFactor(I->getOperand(1))); |
| 2456 | } else if (I->getOpcode() == Instruction::Shl) { |
| 2457 | // (X<<C) -> X * (1 << C) |
| 2458 | if (Constant *ShRHS = dyn_cast<Constant>(I->getOperand(1))) { |
| 2459 | ShRHS = ConstantExpr::getShl(Result, ShRHS); |
| 2460 | return ConstantExpr::getMul(GetFactor(I->getOperand(0)), ShRHS); |
| 2461 | } |
| 2462 | } else if (I->getOpcode() == Instruction::And) { |
| 2463 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 2464 | // X & 0xFFF0 is known to be a multiple of 16. |
| 2465 | unsigned Zeros = CountTrailingZeros_64(RHS->getZExtValue()); |
| 2466 | if (Zeros != V->getType()->getPrimitiveSizeInBits()) |
| 2467 | return ConstantExpr::getShl(Result, |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2468 | ConstantInt::get(Result->getType(), Zeros)); |
Chris Lattner | 85dda9a | 2006-03-02 06:50:58 +0000 | [diff] [blame] | 2469 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2470 | } else if (CastInst *CI = dyn_cast<CastInst>(I)) { |
Chris Lattner | 85dda9a | 2006-03-02 06:50:58 +0000 | [diff] [blame] | 2471 | // Only handle int->int casts. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2472 | if (!CI->isIntegerCast()) |
| 2473 | return Result; |
| 2474 | Value *Op = CI->getOperand(0); |
| 2475 | return ConstantExpr::getCast(CI->getOpcode(), GetFactor(Op), V->getType()); |
Chris Lattner | 85dda9a | 2006-03-02 06:50:58 +0000 | [diff] [blame] | 2476 | } |
| 2477 | return Result; |
| 2478 | } |
| 2479 | |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2480 | /// This function implements the transforms on rem instructions that work |
| 2481 | /// regardless of the kind of rem instruction it is (urem, srem, or frem). It |
| 2482 | /// is used by the visitors to those instructions. |
| 2483 | /// @brief Transforms common to all three rem instructions |
| 2484 | Instruction *InstCombiner::commonRemTransforms(BinaryOperator &I) { |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2485 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2486 | |
Chris Lattner | 0de4a8d | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 2487 | // 0 % X == 0, we don't need to preserve faults! |
| 2488 | if (Constant *LHS = dyn_cast<Constant>(Op0)) |
| 2489 | if (LHS->isNullValue()) |
| 2490 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2491 | |
| 2492 | if (isa<UndefValue>(Op0)) // undef % X -> 0 |
| 2493 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2494 | if (isa<UndefValue>(Op1)) |
| 2495 | return ReplaceInstUsesWith(I, Op1); // X % undef -> undef |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2496 | |
| 2497 | // Handle cases involving: rem X, (select Cond, Y, Z) |
| 2498 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { |
| 2499 | // rem X, (Cond ? 0 : Y) -> rem X, Y. If the rem and the select are in |
| 2500 | // the same basic block, then we replace the select with Y, and the |
| 2501 | // condition of the select with false (if the cond value is in the same |
| 2502 | // BB). If the select has uses other than the div, this allows them to be |
| 2503 | // simplified also. |
| 2504 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(1))) |
| 2505 | if (ST->isNullValue()) { |
| 2506 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); |
| 2507 | if (CondI && CondI->getParent() == I.getParent()) |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2508 | UpdateValueUsesWith(CondI, ConstantInt::getFalse()); |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2509 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
| 2510 | I.setOperand(1, SI->getOperand(2)); |
| 2511 | else |
| 2512 | UpdateValueUsesWith(SI, SI->getOperand(2)); |
Chris Lattner | 7fd5f07 | 2004-07-06 07:01:22 +0000 | [diff] [blame] | 2513 | return &I; |
| 2514 | } |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2515 | // Likewise for: rem X, (Cond ? Y : 0) -> rem X, Y |
| 2516 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(2))) |
| 2517 | if (ST->isNullValue()) { |
| 2518 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); |
| 2519 | if (CondI && CondI->getParent() == I.getParent()) |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2520 | UpdateValueUsesWith(CondI, ConstantInt::getTrue()); |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2521 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
| 2522 | I.setOperand(1, SI->getOperand(1)); |
| 2523 | else |
| 2524 | UpdateValueUsesWith(SI, SI->getOperand(1)); |
| 2525 | return &I; |
| 2526 | } |
Chris Lattner | e9ff0ea | 2005-11-05 07:28:37 +0000 | [diff] [blame] | 2527 | } |
Chris Lattner | 7fd5f07 | 2004-07-06 07:01:22 +0000 | [diff] [blame] | 2528 | |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2529 | return 0; |
| 2530 | } |
| 2531 | |
| 2532 | /// This function implements the transforms common to both integer remainder |
| 2533 | /// instructions (urem and srem). It is called by the visitors to those integer |
| 2534 | /// remainder instructions. |
| 2535 | /// @brief Common integer remainder transforms |
| 2536 | Instruction *InstCombiner::commonIRemTransforms(BinaryOperator &I) { |
| 2537 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2538 | |
| 2539 | if (Instruction *common = commonRemTransforms(I)) |
| 2540 | return common; |
| 2541 | |
Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2542 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 0de4a8d | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 2543 | // X % 0 == undef, we don't need to preserve faults! |
| 2544 | if (RHS->equalsInt(0)) |
| 2545 | return ReplaceInstUsesWith(I, UndefValue::get(I.getType())); |
| 2546 | |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2547 | if (RHS->equalsInt(1)) // X % 1 == 0 |
| 2548 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2549 | |
Chris Lattner | b70f141 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 2550 | if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) { |
| 2551 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0I)) { |
| 2552 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 2553 | return R; |
| 2554 | } else if (isa<PHINode>(Op0I)) { |
| 2555 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2556 | return NV; |
Chris Lattner | b70f141 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 2557 | } |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2558 | // (X * C1) % C2 --> 0 iff C1 % C2 == 0 |
| 2559 | if (ConstantExpr::getSRem(GetFactor(Op0I), RHS)->isNullValue()) |
Chris Lattner | 85dda9a | 2006-03-02 06:50:58 +0000 | [diff] [blame] | 2560 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | b70f141 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 2561 | } |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2562 | } |
| 2563 | |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2564 | return 0; |
| 2565 | } |
| 2566 | |
| 2567 | Instruction *InstCombiner::visitURem(BinaryOperator &I) { |
| 2568 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2569 | |
| 2570 | if (Instruction *common = commonIRemTransforms(I)) |
| 2571 | return common; |
| 2572 | |
| 2573 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
| 2574 | // X urem C^2 -> X and C |
| 2575 | // Check to see if this is an unsigned remainder with an exact power of 2, |
| 2576 | // if so, convert to a bitwise and. |
| 2577 | if (ConstantInt *C = dyn_cast<ConstantInt>(RHS)) |
| 2578 | if (isPowerOf2_64(C->getZExtValue())) |
| 2579 | return BinaryOperator::createAnd(Op0, SubOne(C)); |
| 2580 | } |
| 2581 | |
Chris Lattner | 2e90b73 | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2582 | if (Instruction *RHSI = dyn_cast<Instruction>(I.getOperand(1))) { |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2583 | // Turn A % (C << N), where C is 2^k, into A & ((C << N)-1) |
| 2584 | if (RHSI->getOpcode() == Instruction::Shl && |
| 2585 | isa<ConstantInt>(RHSI->getOperand(0))) { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2586 | unsigned C1 = cast<ConstantInt>(RHSI->getOperand(0))->getZExtValue(); |
Chris Lattner | 2e90b73 | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2587 | if (isPowerOf2_64(C1)) { |
| 2588 | Constant *N1 = ConstantInt::getAllOnesValue(I.getType()); |
| 2589 | Value *Add = InsertNewInstBefore(BinaryOperator::createAdd(RHSI, N1, |
| 2590 | "tmp"), I); |
| 2591 | return BinaryOperator::createAnd(Op0, Add); |
| 2592 | } |
| 2593 | } |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2594 | } |
Chris Lattner | d79dc79 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2595 | |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2596 | // urem X, (select Cond, 2^C1, 2^C2) --> select Cond, (and X, C1), (and X, C2) |
| 2597 | // where C1&C2 are powers of two. |
| 2598 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { |
| 2599 | if (ConstantInt *STO = dyn_cast<ConstantInt>(SI->getOperand(1))) |
| 2600 | if (ConstantInt *SFO = dyn_cast<ConstantInt>(SI->getOperand(2))) { |
| 2601 | // STO == 0 and SFO == 0 handled above. |
| 2602 | if (isPowerOf2_64(STO->getZExtValue()) && |
| 2603 | isPowerOf2_64(SFO->getZExtValue())) { |
| 2604 | Value *TrueAnd = InsertNewInstBefore( |
| 2605 | BinaryOperator::createAnd(Op0, SubOne(STO), SI->getName()+".t"), I); |
| 2606 | Value *FalseAnd = InsertNewInstBefore( |
| 2607 | BinaryOperator::createAnd(Op0, SubOne(SFO), SI->getName()+".f"), I); |
| 2608 | return new SelectInst(SI->getOperand(0), TrueAnd, FalseAnd); |
| 2609 | } |
| 2610 | } |
Chris Lattner | 2e90b73 | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2611 | } |
| 2612 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2613 | return 0; |
| 2614 | } |
| 2615 | |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2616 | Instruction *InstCombiner::visitSRem(BinaryOperator &I) { |
| 2617 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2618 | |
| 2619 | if (Instruction *common = commonIRemTransforms(I)) |
| 2620 | return common; |
| 2621 | |
| 2622 | if (Value *RHSNeg = dyn_castNegVal(Op1)) |
| 2623 | if (!isa<ConstantInt>(RHSNeg) || |
| 2624 | cast<ConstantInt>(RHSNeg)->getSExtValue() > 0) { |
| 2625 | // X % -Y -> X % Y |
| 2626 | AddUsesToWorkList(I); |
| 2627 | I.setOperand(1, RHSNeg); |
| 2628 | return &I; |
| 2629 | } |
| 2630 | |
| 2631 | // If the top bits of both operands are zero (i.e. we can prove they are |
| 2632 | // unsigned inputs), turn this into a urem. |
| 2633 | uint64_t Mask = 1ULL << (I.getType()->getPrimitiveSizeInBits()-1); |
| 2634 | if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) { |
| 2635 | // X srem Y -> X urem Y, iff X and Y don't have sign bit set |
| 2636 | return BinaryOperator::createURem(Op0, Op1, I.getName()); |
| 2637 | } |
| 2638 | |
| 2639 | return 0; |
| 2640 | } |
| 2641 | |
| 2642 | Instruction *InstCombiner::visitFRem(BinaryOperator &I) { |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2643 | return commonRemTransforms(I); |
| 2644 | } |
| 2645 | |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2646 | // isMaxValueMinusOne - return true if this is Max-1 |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2647 | static bool isMaxValueMinusOne(const ConstantInt *C, bool isSigned) { |
| 2648 | if (isSigned) { |
| 2649 | // Calculate 0111111111..11111 |
| 2650 | unsigned TypeBits = C->getType()->getPrimitiveSizeInBits(); |
| 2651 | int64_t Val = INT64_MAX; // All ones |
| 2652 | Val >>= 64-TypeBits; // Shift out unwanted 1 bits... |
| 2653 | return C->getSExtValue() == Val-1; |
| 2654 | } |
Reid Spencer | a94d394 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 2655 | return C->getZExtValue() == C->getType()->getBitMask()-1; |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2656 | } |
| 2657 | |
| 2658 | // isMinValuePlusOne - return true if this is Min+1 |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2659 | static bool isMinValuePlusOne(const ConstantInt *C, bool isSigned) { |
| 2660 | if (isSigned) { |
| 2661 | // Calculate 1111111111000000000000 |
| 2662 | unsigned TypeBits = C->getType()->getPrimitiveSizeInBits(); |
| 2663 | int64_t Val = -1; // All ones |
| 2664 | Val <<= TypeBits-1; // Shift over to the right spot |
| 2665 | return C->getSExtValue() == Val+1; |
| 2666 | } |
| 2667 | return C->getZExtValue() == 1; // unsigned |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2668 | } |
| 2669 | |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2670 | // isOneBitSet - Return true if there is exactly one bit set in the specified |
| 2671 | // constant. |
| 2672 | static bool isOneBitSet(const ConstantInt *CI) { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2673 | uint64_t V = CI->getZExtValue(); |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2674 | return V && (V & (V-1)) == 0; |
| 2675 | } |
| 2676 | |
Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2677 | #if 0 // Currently unused |
| 2678 | // isLowOnes - Return true if the constant is of the form 0+1+. |
| 2679 | static bool isLowOnes(const ConstantInt *CI) { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2680 | uint64_t V = CI->getZExtValue(); |
Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2681 | |
| 2682 | // There won't be bits set in parts that the type doesn't contain. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2683 | V &= ConstantInt::getAllOnesValue(CI->getType())->getZExtValue(); |
Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2684 | |
| 2685 | uint64_t U = V+1; // If it is low ones, this should be a power of two. |
| 2686 | return U && V && (U & V) == 0; |
| 2687 | } |
| 2688 | #endif |
| 2689 | |
| 2690 | // isHighOnes - Return true if the constant is of the form 1+0+. |
| 2691 | // This is the same as lowones(~X). |
| 2692 | static bool isHighOnes(const ConstantInt *CI) { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2693 | uint64_t V = ~CI->getZExtValue(); |
Chris Lattner | 2c14cf7 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 2694 | if (~V == 0) return false; // 0's does not match "1+" |
Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2695 | |
| 2696 | // There won't be bits set in parts that the type doesn't contain. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2697 | V &= ConstantInt::getAllOnesValue(CI->getType())->getZExtValue(); |
Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2698 | |
| 2699 | uint64_t U = V+1; // If it is low ones, this should be a power of two. |
| 2700 | return U && V && (U & V) == 0; |
| 2701 | } |
| 2702 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2703 | /// getICmpCode - Encode a icmp predicate into a three bit mask. These bits |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2704 | /// are carefully arranged to allow folding of expressions such as: |
| 2705 | /// |
| 2706 | /// (A < B) | (A > B) --> (A != B) |
| 2707 | /// |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2708 | /// Note that this is only valid if the first and second predicates have the |
| 2709 | /// same sign. Is illegal to do: (A u< B) | (A s> B) |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2710 | /// |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2711 | /// Three bits are used to represent the condition, as follows: |
| 2712 | /// 0 A > B |
| 2713 | /// 1 A == B |
| 2714 | /// 2 A < B |
| 2715 | /// |
| 2716 | /// <=> Value Definition |
| 2717 | /// 000 0 Always false |
| 2718 | /// 001 1 A > B |
| 2719 | /// 010 2 A == B |
| 2720 | /// 011 3 A >= B |
| 2721 | /// 100 4 A < B |
| 2722 | /// 101 5 A != B |
| 2723 | /// 110 6 A <= B |
| 2724 | /// 111 7 Always true |
| 2725 | /// |
| 2726 | static unsigned getICmpCode(const ICmpInst *ICI) { |
| 2727 | switch (ICI->getPredicate()) { |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2728 | // False -> 0 |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2729 | case ICmpInst::ICMP_UGT: return 1; // 001 |
| 2730 | case ICmpInst::ICMP_SGT: return 1; // 001 |
| 2731 | case ICmpInst::ICMP_EQ: return 2; // 010 |
| 2732 | case ICmpInst::ICMP_UGE: return 3; // 011 |
| 2733 | case ICmpInst::ICMP_SGE: return 3; // 011 |
| 2734 | case ICmpInst::ICMP_ULT: return 4; // 100 |
| 2735 | case ICmpInst::ICMP_SLT: return 4; // 100 |
| 2736 | case ICmpInst::ICMP_NE: return 5; // 101 |
| 2737 | case ICmpInst::ICMP_ULE: return 6; // 110 |
| 2738 | case ICmpInst::ICMP_SLE: return 6; // 110 |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2739 | // True -> 7 |
| 2740 | default: |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2741 | assert(0 && "Invalid ICmp predicate!"); |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2742 | return 0; |
| 2743 | } |
| 2744 | } |
| 2745 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2746 | /// getICmpValue - This is the complement of getICmpCode, which turns an |
| 2747 | /// opcode and two operands into either a constant true or false, or a brand |
| 2748 | /// new /// ICmp instruction. The sign is passed in to determine which kind |
| 2749 | /// of predicate to use in new icmp instructions. |
| 2750 | static Value *getICmpValue(bool sign, unsigned code, Value *LHS, Value *RHS) { |
| 2751 | switch (code) { |
| 2752 | default: assert(0 && "Illegal ICmp code!"); |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2753 | case 0: return ConstantInt::getFalse(); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2754 | case 1: |
| 2755 | if (sign) |
| 2756 | return new ICmpInst(ICmpInst::ICMP_SGT, LHS, RHS); |
| 2757 | else |
| 2758 | return new ICmpInst(ICmpInst::ICMP_UGT, LHS, RHS); |
| 2759 | case 2: return new ICmpInst(ICmpInst::ICMP_EQ, LHS, RHS); |
| 2760 | case 3: |
| 2761 | if (sign) |
| 2762 | return new ICmpInst(ICmpInst::ICMP_SGE, LHS, RHS); |
| 2763 | else |
| 2764 | return new ICmpInst(ICmpInst::ICMP_UGE, LHS, RHS); |
| 2765 | case 4: |
| 2766 | if (sign) |
| 2767 | return new ICmpInst(ICmpInst::ICMP_SLT, LHS, RHS); |
| 2768 | else |
| 2769 | return new ICmpInst(ICmpInst::ICMP_ULT, LHS, RHS); |
| 2770 | case 5: return new ICmpInst(ICmpInst::ICMP_NE, LHS, RHS); |
| 2771 | case 6: |
| 2772 | if (sign) |
| 2773 | return new ICmpInst(ICmpInst::ICMP_SLE, LHS, RHS); |
| 2774 | else |
| 2775 | return new ICmpInst(ICmpInst::ICMP_ULE, LHS, RHS); |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2776 | case 7: return ConstantInt::getTrue(); |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2777 | } |
| 2778 | } |
| 2779 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2780 | static bool PredicatesFoldable(ICmpInst::Predicate p1, ICmpInst::Predicate p2) { |
| 2781 | return (ICmpInst::isSignedPredicate(p1) == ICmpInst::isSignedPredicate(p2)) || |
| 2782 | (ICmpInst::isSignedPredicate(p1) && |
| 2783 | (p2 == ICmpInst::ICMP_EQ || p2 == ICmpInst::ICMP_NE)) || |
| 2784 | (ICmpInst::isSignedPredicate(p2) && |
| 2785 | (p1 == ICmpInst::ICMP_EQ || p1 == ICmpInst::ICMP_NE)); |
| 2786 | } |
| 2787 | |
| 2788 | namespace { |
| 2789 | // FoldICmpLogical - Implements (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B) |
| 2790 | struct FoldICmpLogical { |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2791 | InstCombiner &IC; |
| 2792 | Value *LHS, *RHS; |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2793 | ICmpInst::Predicate pred; |
| 2794 | FoldICmpLogical(InstCombiner &ic, ICmpInst *ICI) |
| 2795 | : IC(ic), LHS(ICI->getOperand(0)), RHS(ICI->getOperand(1)), |
| 2796 | pred(ICI->getPredicate()) {} |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2797 | bool shouldApply(Value *V) const { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2798 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(V)) |
| 2799 | if (PredicatesFoldable(pred, ICI->getPredicate())) |
| 2800 | return (ICI->getOperand(0) == LHS && ICI->getOperand(1) == RHS || |
| 2801 | ICI->getOperand(0) == RHS && ICI->getOperand(1) == LHS); |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2802 | return false; |
| 2803 | } |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2804 | Instruction *apply(Instruction &Log) const { |
| 2805 | ICmpInst *ICI = cast<ICmpInst>(Log.getOperand(0)); |
| 2806 | if (ICI->getOperand(0) != LHS) { |
| 2807 | assert(ICI->getOperand(1) == LHS); |
| 2808 | ICI->swapOperands(); // Swap the LHS and RHS of the ICmp |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2809 | } |
| 2810 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2811 | unsigned LHSCode = getICmpCode(ICI); |
| 2812 | unsigned RHSCode = getICmpCode(cast<ICmpInst>(Log.getOperand(1))); |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2813 | unsigned Code; |
| 2814 | switch (Log.getOpcode()) { |
| 2815 | case Instruction::And: Code = LHSCode & RHSCode; break; |
| 2816 | case Instruction::Or: Code = LHSCode | RHSCode; break; |
| 2817 | case Instruction::Xor: Code = LHSCode ^ RHSCode; break; |
Chris Lattner | 2caaaba | 2003-09-22 20:33:34 +0000 | [diff] [blame] | 2818 | default: assert(0 && "Illegal logical opcode!"); return 0; |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2819 | } |
| 2820 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2821 | Value *RV = getICmpValue(ICmpInst::isSignedPredicate(pred), Code, LHS, RHS); |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2822 | if (Instruction *I = dyn_cast<Instruction>(RV)) |
| 2823 | return I; |
| 2824 | // Otherwise, it's a constant boolean value... |
| 2825 | return IC.ReplaceInstUsesWith(Log, RV); |
| 2826 | } |
| 2827 | }; |
Chris Lattner | e3a63d1 | 2006-11-15 04:53:24 +0000 | [diff] [blame] | 2828 | } // end anonymous namespace |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2829 | |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2830 | // OptAndOp - This handles expressions of the form ((val OP C1) & C2). Where |
| 2831 | // the Op parameter is 'OP', OpRHS is 'C1', and AndRHS is 'C2'. Op is |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2832 | // guaranteed to be a binary operator. |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2833 | Instruction *InstCombiner::OptAndOp(Instruction *Op, |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2834 | ConstantInt *OpRHS, |
| 2835 | ConstantInt *AndRHS, |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2836 | BinaryOperator &TheAnd) { |
| 2837 | Value *X = Op->getOperand(0); |
Chris Lattner | fcf21a7 | 2004-01-12 19:47:05 +0000 | [diff] [blame] | 2838 | Constant *Together = 0; |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2839 | if (!Op->isShift()) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2840 | Together = ConstantExpr::getAnd(AndRHS, OpRHS); |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2841 | |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2842 | switch (Op->getOpcode()) { |
| 2843 | case Instruction::Xor: |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2844 | if (Op->hasOneUse()) { |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2845 | // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2) |
Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 2846 | Instruction *And = BinaryOperator::createAnd(X, AndRHS); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2847 | InsertNewInstBefore(And, TheAnd); |
Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 2848 | And->takeName(Op); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2849 | return BinaryOperator::createXor(And, Together); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2850 | } |
| 2851 | break; |
| 2852 | case Instruction::Or: |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2853 | if (Together == AndRHS) // (X | C) & C --> C |
| 2854 | return ReplaceInstUsesWith(TheAnd, AndRHS); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2855 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2856 | if (Op->hasOneUse() && Together != OpRHS) { |
| 2857 | // (X | C1) & C2 --> (X | (C1&C2)) & C2 |
Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 2858 | Instruction *Or = BinaryOperator::createOr(X, Together); |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2859 | InsertNewInstBefore(Or, TheAnd); |
Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 2860 | Or->takeName(Op); |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2861 | return BinaryOperator::createAnd(Or, AndRHS); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2862 | } |
| 2863 | break; |
| 2864 | case Instruction::Add: |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 2865 | if (Op->hasOneUse()) { |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2866 | // Adding a one to a single bit bit-field should be turned into an XOR |
| 2867 | // of the bit. First thing to check is to see if this AND is with a |
| 2868 | // single bit constant. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2869 | uint64_t AndRHSV = cast<ConstantInt>(AndRHS)->getZExtValue(); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2870 | |
| 2871 | // Clear bits that are not part of the constant. |
Reid Spencer | a94d394 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 2872 | AndRHSV &= AndRHS->getType()->getBitMask(); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2873 | |
| 2874 | // If there is only one bit set... |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2875 | if (isOneBitSet(cast<ConstantInt>(AndRHS))) { |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2876 | // Ok, at this point, we know that we are masking the result of the |
| 2877 | // ADD down to exactly one bit. If the constant we are adding has |
| 2878 | // no bits set below this bit, then we can eliminate the ADD. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2879 | uint64_t AddRHS = cast<ConstantInt>(OpRHS)->getZExtValue(); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2880 | |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2881 | // Check to see if any bits below the one bit set in AndRHSV are set. |
| 2882 | if ((AddRHS & (AndRHSV-1)) == 0) { |
| 2883 | // If not, the only thing that can effect the output of the AND is |
| 2884 | // the bit specified by AndRHSV. If that bit is set, the effect of |
| 2885 | // the XOR is to toggle the bit. If it is clear, then the ADD has |
| 2886 | // no effect. |
| 2887 | if ((AddRHS & AndRHSV) == 0) { // Bit is not set, noop |
| 2888 | TheAnd.setOperand(0, X); |
| 2889 | return &TheAnd; |
| 2890 | } else { |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2891 | // Pull the XOR out of the AND. |
Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 2892 | Instruction *NewAnd = BinaryOperator::createAnd(X, AndRHS); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2893 | InsertNewInstBefore(NewAnd, TheAnd); |
Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 2894 | NewAnd->takeName(Op); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2895 | return BinaryOperator::createXor(NewAnd, AndRHS); |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2896 | } |
| 2897 | } |
| 2898 | } |
| 2899 | } |
| 2900 | break; |
Chris Lattner | 2da2917 | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 2901 | |
| 2902 | case Instruction::Shl: { |
| 2903 | // We know that the AND will not produce any of the bits shifted in, so if |
| 2904 | // the anded constant includes them, clear them now! |
| 2905 | // |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2906 | Constant *AllOne = ConstantInt::getAllOnesValue(AndRHS->getType()); |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 2907 | Constant *ShlMask = ConstantExpr::getShl(AllOne, OpRHS); |
| 2908 | Constant *CI = ConstantExpr::getAnd(AndRHS, ShlMask); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2909 | |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 2910 | if (CI == ShlMask) { // Masking out bits that the shift already masks |
| 2911 | return ReplaceInstUsesWith(TheAnd, Op); // No need for the and. |
| 2912 | } else if (CI != AndRHS) { // Reducing bits set in and. |
Chris Lattner | 2da2917 | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 2913 | TheAnd.setOperand(1, CI); |
| 2914 | return &TheAnd; |
| 2915 | } |
| 2916 | break; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2917 | } |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2918 | case Instruction::LShr: |
| 2919 | { |
Chris Lattner | 2da2917 | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 2920 | // We know that the AND will not produce any of the bits shifted in, so if |
| 2921 | // the anded constant includes them, clear them now! This only applies to |
| 2922 | // unsigned shifts, because a signed shr may bring in set bits! |
| 2923 | // |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2924 | Constant *AllOne = ConstantInt::getAllOnesValue(AndRHS->getType()); |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2925 | Constant *ShrMask = ConstantExpr::getLShr(AllOne, OpRHS); |
| 2926 | Constant *CI = ConstantExpr::getAnd(AndRHS, ShrMask); |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 2927 | |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2928 | if (CI == ShrMask) { // Masking out bits that the shift already masks. |
| 2929 | return ReplaceInstUsesWith(TheAnd, Op); |
| 2930 | } else if (CI != AndRHS) { |
| 2931 | TheAnd.setOperand(1, CI); // Reduce bits set in and cst. |
| 2932 | return &TheAnd; |
| 2933 | } |
| 2934 | break; |
| 2935 | } |
| 2936 | case Instruction::AShr: |
| 2937 | // Signed shr. |
| 2938 | // See if this is shifting in some sign extension, then masking it out |
| 2939 | // with an and. |
| 2940 | if (Op->hasOneUse()) { |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2941 | Constant *AllOne = ConstantInt::getAllOnesValue(AndRHS->getType()); |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2942 | Constant *ShrMask = ConstantExpr::getLShr(AllOne, OpRHS); |
Reid Spencer | 2a499b0 | 2006-12-13 17:19:09 +0000 | [diff] [blame] | 2943 | Constant *C = ConstantExpr::getAnd(AndRHS, ShrMask); |
| 2944 | if (C == AndRHS) { // Masking out bits shifted in. |
Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 2945 | // (Val ashr C1) & C2 -> (Val lshr C1) & C2 |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2946 | // Make the argument unsigned. |
| 2947 | Value *ShVal = Op->getOperand(0); |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2948 | ShVal = InsertNewInstBefore( |
Reid Spencer | 0d5f923 | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 2949 | BinaryOperator::createLShr(ShVal, OpRHS, |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2950 | Op->getName()), TheAnd); |
Reid Spencer | 2a499b0 | 2006-12-13 17:19:09 +0000 | [diff] [blame] | 2951 | return BinaryOperator::createAnd(ShVal, AndRHS, TheAnd.getName()); |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 2952 | } |
Chris Lattner | 2da2917 | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 2953 | } |
| 2954 | break; |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2955 | } |
| 2956 | return 0; |
| 2957 | } |
| 2958 | |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2959 | |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2960 | /// InsertRangeTest - Emit a computation of: (V >= Lo && V < Hi) if Inside is |
| 2961 | /// true, otherwise (V < Lo || V >= Hi). In pratice, we emit the more efficient |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2962 | /// (V-Lo) <u Hi-Lo. This method expects that Lo <= Hi. isSigned indicates |
| 2963 | /// whether to treat the V, Lo and HI as signed or not. IB is the location to |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2964 | /// insert new instructions. |
| 2965 | Instruction *InstCombiner::InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2966 | bool isSigned, bool Inside, |
| 2967 | Instruction &IB) { |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2968 | assert(cast<ConstantInt>(ConstantExpr::getICmp((isSigned ? |
Reid Spencer | cddc9df | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 2969 | ICmpInst::ICMP_SLE:ICmpInst::ICMP_ULE), Lo, Hi))->getZExtValue() && |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2970 | "Lo is not <= Hi in range emission code!"); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2971 | |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2972 | if (Inside) { |
| 2973 | if (Lo == Hi) // Trivially false. |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2974 | return new ICmpInst(ICmpInst::ICMP_NE, V, V); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2975 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2976 | // V >= Min && V < Hi --> V < Hi |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2977 | if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2978 | ICmpInst::Predicate pred = (isSigned ? |
| 2979 | ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT); |
| 2980 | return new ICmpInst(pred, V, Hi); |
| 2981 | } |
| 2982 | |
| 2983 | // Emit V-Lo <u Hi-Lo |
| 2984 | Constant *NegLo = ConstantExpr::getNeg(Lo); |
| 2985 | Instruction *Add = BinaryOperator::createAdd(V, NegLo, V->getName()+".off"); |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2986 | InsertNewInstBefore(Add, IB); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2987 | Constant *UpperBound = ConstantExpr::getAdd(NegLo, Hi); |
| 2988 | return new ICmpInst(ICmpInst::ICMP_ULT, Add, UpperBound); |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2989 | } |
| 2990 | |
| 2991 | if (Lo == Hi) // Trivially true. |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2992 | return new ICmpInst(ICmpInst::ICMP_EQ, V, V); |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2993 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2994 | // V < Min || V >= Hi ->'V > Hi-1' |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2995 | Hi = SubOne(cast<ConstantInt>(Hi)); |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2996 | if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2997 | ICmpInst::Predicate pred = (isSigned ? |
| 2998 | ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT); |
| 2999 | return new ICmpInst(pred, V, Hi); |
| 3000 | } |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3001 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3002 | // Emit V-Lo > Hi-1-Lo |
| 3003 | Constant *NegLo = ConstantExpr::getNeg(Lo); |
| 3004 | Instruction *Add = BinaryOperator::createAdd(V, NegLo, V->getName()+".off"); |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3005 | InsertNewInstBefore(Add, IB); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3006 | Constant *LowerBound = ConstantExpr::getAdd(NegLo, Hi); |
| 3007 | return new ICmpInst(ICmpInst::ICMP_UGT, Add, LowerBound); |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3008 | } |
| 3009 | |
Chris Lattner | b4b2530 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3010 | // isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with |
| 3011 | // any number of 0s on either side. The 1s are allowed to wrap from LSB to |
| 3012 | // MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is |
| 3013 | // not, since all 1s are not contiguous. |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3014 | static bool isRunOfOnes(ConstantInt *Val, unsigned &MB, unsigned &ME) { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3015 | uint64_t V = Val->getZExtValue(); |
Chris Lattner | b4b2530 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3016 | if (!isShiftedMask_64(V)) return false; |
| 3017 | |
| 3018 | // look for the first zero bit after the run of ones |
| 3019 | MB = 64-CountLeadingZeros_64((V - 1) ^ V); |
| 3020 | // look for the first non-zero bit |
| 3021 | ME = 64-CountLeadingZeros_64(V); |
| 3022 | return true; |
| 3023 | } |
| 3024 | |
| 3025 | |
| 3026 | |
| 3027 | /// FoldLogicalPlusAnd - This is part of an expression (LHS +/- RHS) & Mask, |
| 3028 | /// where isSub determines whether the operator is a sub. If we can fold one of |
| 3029 | /// the following xforms: |
Chris Lattner | af51757 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3030 | /// |
| 3031 | /// ((A & N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == Mask |
| 3032 | /// ((A | N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0 |
| 3033 | /// ((A ^ N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0 |
| 3034 | /// |
| 3035 | /// return (A +/- B). |
| 3036 | /// |
| 3037 | Value *InstCombiner::FoldLogicalPlusAnd(Value *LHS, Value *RHS, |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3038 | ConstantInt *Mask, bool isSub, |
Chris Lattner | af51757 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3039 | Instruction &I) { |
| 3040 | Instruction *LHSI = dyn_cast<Instruction>(LHS); |
| 3041 | if (!LHSI || LHSI->getNumOperands() != 2 || |
| 3042 | !isa<ConstantInt>(LHSI->getOperand(1))) return 0; |
| 3043 | |
| 3044 | ConstantInt *N = cast<ConstantInt>(LHSI->getOperand(1)); |
| 3045 | |
| 3046 | switch (LHSI->getOpcode()) { |
| 3047 | default: return 0; |
| 3048 | case Instruction::And: |
Chris Lattner | b4b2530 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3049 | if (ConstantExpr::getAnd(N, Mask) == Mask) { |
| 3050 | // If the AndRHS is a power of two minus one (0+1+), this is simple. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3051 | if ((Mask->getZExtValue() & Mask->getZExtValue()+1) == 0) |
Chris Lattner | b4b2530 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3052 | break; |
| 3053 | |
| 3054 | // Otherwise, if Mask is 0+1+0+, and if B is known to have the low 0+ |
| 3055 | // part, we don't need any explicit masks to take them out of A. If that |
| 3056 | // is all N is, ignore it. |
| 3057 | unsigned MB, ME; |
| 3058 | if (isRunOfOnes(Mask, MB, ME)) { // begin/end bit of run, inclusive |
Reid Spencer | a94d394 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 3059 | uint64_t Mask = cast<IntegerType>(RHS->getType())->getBitMask(); |
Chris Lattner | c3ebf40 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3060 | Mask >>= 64-MB+1; |
| 3061 | if (MaskedValueIsZero(RHS, Mask)) |
Chris Lattner | b4b2530 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3062 | break; |
| 3063 | } |
| 3064 | } |
Chris Lattner | af51757 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3065 | return 0; |
| 3066 | case Instruction::Or: |
| 3067 | case Instruction::Xor: |
Chris Lattner | b4b2530 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3068 | // If the AndRHS is a power of two minus one (0+1+), and N&Mask == 0 |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3069 | if ((Mask->getZExtValue() & Mask->getZExtValue()+1) == 0 && |
Chris Lattner | b4b2530 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3070 | ConstantExpr::getAnd(N, Mask)->isNullValue()) |
Chris Lattner | af51757 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3071 | break; |
| 3072 | return 0; |
| 3073 | } |
| 3074 | |
| 3075 | Instruction *New; |
| 3076 | if (isSub) |
| 3077 | New = BinaryOperator::createSub(LHSI->getOperand(0), RHS, "fold"); |
| 3078 | else |
| 3079 | New = BinaryOperator::createAdd(LHSI->getOperand(0), RHS, "fold"); |
| 3080 | return InsertNewInstBefore(New, I); |
| 3081 | } |
| 3082 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3083 | Instruction *InstCombiner::visitAnd(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 3084 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3085 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3086 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3087 | if (isa<UndefValue>(Op1)) // X & undef -> 0 |
| 3088 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 3089 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3090 | // and X, X = X |
| 3091 | if (Op0 == Op1) |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 3092 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3093 | |
Chris Lattner | 5b2edb1 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3094 | // See if we can simplify any instructions used by the instruction whose sole |
Chris Lattner | 5997cf9 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 3095 | // purpose is to compute bits we don't care about. |
Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 3096 | uint64_t KnownZero, KnownOne; |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 3097 | if (!isa<VectorType>(I.getType())) { |
Reid Spencer | a94d394 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 3098 | if (SimplifyDemandedBits(&I, cast<IntegerType>(I.getType())->getBitMask(), |
Chris Lattner | 120ab03 | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3099 | KnownZero, KnownOne)) |
Chris Lattner | 5997cf9 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 3100 | return &I; |
Chris Lattner | 120ab03 | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3101 | } else { |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 3102 | if (ConstantVector *CP = dyn_cast<ConstantVector>(Op1)) { |
Chris Lattner | 120ab03 | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3103 | if (CP->isAllOnesValue()) |
| 3104 | return ReplaceInstUsesWith(I, I.getOperand(0)); |
| 3105 | } |
| 3106 | } |
Chris Lattner | 5997cf9 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 3107 | |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3108 | if (ConstantInt *AndRHS = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | ab2dc4d | 2006-02-08 07:34:50 +0000 | [diff] [blame] | 3109 | uint64_t AndRHSMask = AndRHS->getZExtValue(); |
Reid Spencer | a94d394 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 3110 | uint64_t TypeMask = cast<IntegerType>(Op0->getType())->getBitMask(); |
Chris Lattner | ab2dc4d | 2006-02-08 07:34:50 +0000 | [diff] [blame] | 3111 | uint64_t NotAndRHS = AndRHSMask^TypeMask; |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3112 | |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3113 | // Optimize a variety of ((val OP C1) & C2) combinations... |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3114 | if (isa<BinaryOperator>(Op0)) { |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3115 | Instruction *Op0I = cast<Instruction>(Op0); |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3116 | Value *Op0LHS = Op0I->getOperand(0); |
| 3117 | Value *Op0RHS = Op0I->getOperand(1); |
| 3118 | switch (Op0I->getOpcode()) { |
| 3119 | case Instruction::Xor: |
| 3120 | case Instruction::Or: |
Chris Lattner | 9e2c7fa | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 3121 | // If the mask is only needed on one incoming arm, push it up. |
| 3122 | if (Op0I->hasOneUse()) { |
| 3123 | if (MaskedValueIsZero(Op0LHS, NotAndRHS)) { |
| 3124 | // Not masking anything out for the LHS, move to RHS. |
| 3125 | Instruction *NewRHS = BinaryOperator::createAnd(Op0RHS, AndRHS, |
| 3126 | Op0RHS->getName()+".masked"); |
| 3127 | InsertNewInstBefore(NewRHS, I); |
| 3128 | return BinaryOperator::create( |
| 3129 | cast<BinaryOperator>(Op0I)->getOpcode(), Op0LHS, NewRHS); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3130 | } |
Chris Lattner | c3ebf40 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3131 | if (!isa<Constant>(Op0RHS) && |
Chris Lattner | 9e2c7fa | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 3132 | MaskedValueIsZero(Op0RHS, NotAndRHS)) { |
| 3133 | // Not masking anything out for the RHS, move to LHS. |
| 3134 | Instruction *NewLHS = BinaryOperator::createAnd(Op0LHS, AndRHS, |
| 3135 | Op0LHS->getName()+".masked"); |
| 3136 | InsertNewInstBefore(NewLHS, I); |
| 3137 | return BinaryOperator::create( |
| 3138 | cast<BinaryOperator>(Op0I)->getOpcode(), NewLHS, Op0RHS); |
| 3139 | } |
| 3140 | } |
| 3141 | |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3142 | break; |
Chris Lattner | af51757 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3143 | case Instruction::Add: |
Chris Lattner | b4b2530 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3144 | // ((A & N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == AndRHS. |
| 3145 | // ((A | N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0 |
| 3146 | // ((A ^ N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0 |
| 3147 | if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, false, I)) |
| 3148 | return BinaryOperator::createAnd(V, AndRHS); |
| 3149 | if (Value *V = FoldLogicalPlusAnd(Op0RHS, Op0LHS, AndRHS, false, I)) |
| 3150 | return BinaryOperator::createAnd(V, AndRHS); // Add commutes |
Chris Lattner | af51757 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3151 | break; |
| 3152 | |
| 3153 | case Instruction::Sub: |
Chris Lattner | b4b2530 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3154 | // ((A & N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == AndRHS. |
| 3155 | // ((A | N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0 |
| 3156 | // ((A ^ N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0 |
| 3157 | if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, true, I)) |
| 3158 | return BinaryOperator::createAnd(V, AndRHS); |
Chris Lattner | af51757 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3159 | break; |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3160 | } |
| 3161 | |
Chris Lattner | 16464b3 | 2003-07-23 19:25:52 +0000 | [diff] [blame] | 3162 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3163 | if (Instruction *Res = OptAndOp(Op0I, Op0CI, AndRHS, I)) |
Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3164 | return Res; |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3165 | } else if (CastInst *CI = dyn_cast<CastInst>(Op0)) { |
Chris Lattner | 2c14cf7 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3166 | // If this is an integer truncation or change from signed-to-unsigned, and |
| 3167 | // if the source is an and/or with immediate, transform it. This |
| 3168 | // frequently occurs for bitfield accesses. |
| 3169 | if (Instruction *CastOp = dyn_cast<Instruction>(CI->getOperand(0))) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3170 | if ((isa<TruncInst>(CI) || isa<BitCastInst>(CI)) && |
Chris Lattner | 2c14cf7 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3171 | CastOp->getNumOperands() == 2) |
Chris Lattner | ab2dc4d | 2006-02-08 07:34:50 +0000 | [diff] [blame] | 3172 | if (ConstantInt *AndCI = dyn_cast<ConstantInt>(CastOp->getOperand(1))) |
Chris Lattner | 2c14cf7 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3173 | if (CastOp->getOpcode() == Instruction::And) { |
| 3174 | // Change: and (cast (and X, C1) to T), C2 |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3175 | // into : and (cast X to T), trunc_or_bitcast(C1)&C2 |
| 3176 | // This will fold the two constants together, which may allow |
| 3177 | // other simplifications. |
Reid Spencer | bb65ebf | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 3178 | Instruction *NewCast = CastInst::createTruncOrBitCast( |
| 3179 | CastOp->getOperand(0), I.getType(), |
| 3180 | CastOp->getName()+".shrunk"); |
Chris Lattner | 2c14cf7 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3181 | NewCast = InsertNewInstBefore(NewCast, I); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3182 | // trunc_or_bitcast(C1)&C2 |
Reid Spencer | bb65ebf | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 3183 | Constant *C3 = ConstantExpr::getTruncOrBitCast(AndCI,I.getType()); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3184 | C3 = ConstantExpr::getAnd(C3, AndRHS); |
Chris Lattner | 2c14cf7 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3185 | return BinaryOperator::createAnd(NewCast, C3); |
| 3186 | } else if (CastOp->getOpcode() == Instruction::Or) { |
| 3187 | // Change: and (cast (or X, C1) to T), C2 |
| 3188 | // into : trunc(C1)&C2 iff trunc(C1)&C2 == C2 |
Chris Lattner | 2dc148e | 2006-12-12 19:11:20 +0000 | [diff] [blame] | 3189 | Constant *C3 = ConstantExpr::getTruncOrBitCast(AndCI,I.getType()); |
Chris Lattner | 2c14cf7 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3190 | if (ConstantExpr::getAnd(C3, AndRHS) == AndRHS) // trunc(C1)&C2 |
| 3191 | return ReplaceInstUsesWith(I, AndRHS); |
| 3192 | } |
| 3193 | } |
Chris Lattner | 33217db | 2003-07-23 19:36:21 +0000 | [diff] [blame] | 3194 | } |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3195 | |
| 3196 | // Try to fold constant and into select arguments. |
| 3197 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3198 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3199 | return R; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 3200 | if (isa<PHINode>(Op0)) |
| 3201 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 3202 | return NV; |
Chris Lattner | 49b47ae | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 3203 | } |
| 3204 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 3205 | Value *Op0NotVal = dyn_castNotVal(Op0); |
| 3206 | Value *Op1NotVal = dyn_castNotVal(Op1); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3207 | |
Chris Lattner | 023a483 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 3208 | if (Op0NotVal == Op1 || Op1NotVal == Op0) // A & ~A == ~A & A == 0 |
| 3209 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 3210 | |
Misha Brukman | 9c003d8 | 2004-07-30 12:50:08 +0000 | [diff] [blame] | 3211 | // (~A & ~B) == (~(A | B)) - De Morgan's Law |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 3212 | if (Op0NotVal && Op1NotVal && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3213 | Instruction *Or = BinaryOperator::createOr(Op0NotVal, Op1NotVal, |
| 3214 | I.getName()+".demorgan"); |
Chris Lattner | 49b47ae | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 3215 | InsertNewInstBefore(Or, I); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3216 | return BinaryOperator::createNot(Or); |
| 3217 | } |
Chris Lattner | 8b10ab3 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3218 | |
| 3219 | { |
| 3220 | Value *A = 0, *B = 0; |
Chris Lattner | 8b10ab3 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3221 | if (match(Op0, m_Or(m_Value(A), m_Value(B)))) |
| 3222 | if (A == Op1 || B == Op1) // (A | ?) & A --> A |
| 3223 | return ReplaceInstUsesWith(I, Op1); |
| 3224 | if (match(Op1, m_Or(m_Value(A), m_Value(B)))) |
| 3225 | if (A == Op0 || B == Op0) // A & (A | ?) --> A |
| 3226 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | dcd0792 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 3227 | |
| 3228 | if (Op0->hasOneUse() && |
| 3229 | match(Op0, m_Xor(m_Value(A), m_Value(B)))) { |
| 3230 | if (A == Op1) { // (A^B)&A -> A&(A^B) |
| 3231 | I.swapOperands(); // Simplify below |
| 3232 | std::swap(Op0, Op1); |
| 3233 | } else if (B == Op1) { // (A^B)&B -> B&(B^A) |
| 3234 | cast<BinaryOperator>(Op0)->swapOperands(); |
| 3235 | I.swapOperands(); // Simplify below |
| 3236 | std::swap(Op0, Op1); |
| 3237 | } |
| 3238 | } |
| 3239 | if (Op1->hasOneUse() && |
| 3240 | match(Op1, m_Xor(m_Value(A), m_Value(B)))) { |
| 3241 | if (B == Op0) { // B&(A^B) -> B&(B^A) |
| 3242 | cast<BinaryOperator>(Op1)->swapOperands(); |
| 3243 | std::swap(A, B); |
| 3244 | } |
| 3245 | if (A == Op0) { // A&(A^B) -> A & ~B |
| 3246 | Instruction *NotB = BinaryOperator::createNot(B, "tmp"); |
| 3247 | InsertNewInstBefore(NotB, I); |
| 3248 | return BinaryOperator::createAnd(A, NotB); |
| 3249 | } |
| 3250 | } |
Chris Lattner | 8b10ab3 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3251 | } |
| 3252 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3253 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(Op1)) { |
| 3254 | // (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B) |
| 3255 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS))) |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3256 | return R; |
| 3257 | |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3258 | Value *LHSVal, *RHSVal; |
| 3259 | ConstantInt *LHSCst, *RHSCst; |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3260 | ICmpInst::Predicate LHSCC, RHSCC; |
| 3261 | if (match(Op0, m_ICmp(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst)))) |
| 3262 | if (match(RHS, m_ICmp(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst)))) |
| 3263 | if (LHSVal == RHSVal && // Found (X icmp C1) & (X icmp C2) |
| 3264 | // ICMP_[GL]E X, CST is folded to ICMP_[GL]T elsewhere. |
| 3265 | LHSCC != ICmpInst::ICMP_UGE && LHSCC != ICmpInst::ICMP_ULE && |
| 3266 | RHSCC != ICmpInst::ICMP_UGE && RHSCC != ICmpInst::ICMP_ULE && |
| 3267 | LHSCC != ICmpInst::ICMP_SGE && LHSCC != ICmpInst::ICMP_SLE && |
| 3268 | RHSCC != ICmpInst::ICMP_SGE && RHSCC != ICmpInst::ICMP_SLE) { |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3269 | // Ensure that the larger constant is on the RHS. |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3270 | ICmpInst::Predicate GT = ICmpInst::isSignedPredicate(LHSCC) ? |
| 3271 | ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; |
| 3272 | Constant *Cmp = ConstantExpr::getICmp(GT, LHSCst, RHSCst); |
| 3273 | ICmpInst *LHS = cast<ICmpInst>(Op0); |
Reid Spencer | cddc9df | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 3274 | if (cast<ConstantInt>(Cmp)->getZExtValue()) { |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3275 | std::swap(LHS, RHS); |
| 3276 | std::swap(LHSCst, RHSCst); |
| 3277 | std::swap(LHSCC, RHSCC); |
| 3278 | } |
| 3279 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3280 | // At this point, we know we have have two icmp instructions |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3281 | // comparing a value against two constants and and'ing the result |
| 3282 | // together. Because of the above check, we know that we only have |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3283 | // icmp eq, icmp ne, icmp [su]lt, and icmp [SU]gt here. We also know |
| 3284 | // (from the FoldICmpLogical check above), that the two constants |
| 3285 | // are not equal and that the larger constant is on the RHS |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3286 | assert(LHSCst != RHSCst && "Compares not folded above?"); |
| 3287 | |
| 3288 | switch (LHSCC) { |
| 3289 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3290 | case ICmpInst::ICMP_EQ: |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3291 | switch (RHSCC) { |
| 3292 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3293 | case ICmpInst::ICMP_EQ: // (X == 13 & X == 15) -> false |
| 3294 | case ICmpInst::ICMP_UGT: // (X == 13 & X > 15) -> false |
| 3295 | case ICmpInst::ICMP_SGT: // (X == 13 & X > 15) -> false |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3296 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3297 | case ICmpInst::ICMP_NE: // (X == 13 & X != 15) -> X == 13 |
| 3298 | case ICmpInst::ICMP_ULT: // (X == 13 & X < 15) -> X == 13 |
| 3299 | case ICmpInst::ICMP_SLT: // (X == 13 & X < 15) -> X == 13 |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3300 | return ReplaceInstUsesWith(I, LHS); |
| 3301 | } |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3302 | case ICmpInst::ICMP_NE: |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3303 | switch (RHSCC) { |
| 3304 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3305 | case ICmpInst::ICMP_ULT: |
| 3306 | if (LHSCst == SubOne(RHSCst)) // (X != 13 & X u< 14) -> X < 13 |
| 3307 | return new ICmpInst(ICmpInst::ICMP_ULT, LHSVal, LHSCst); |
| 3308 | break; // (X != 13 & X u< 15) -> no change |
| 3309 | case ICmpInst::ICMP_SLT: |
| 3310 | if (LHSCst == SubOne(RHSCst)) // (X != 13 & X s< 14) -> X < 13 |
| 3311 | return new ICmpInst(ICmpInst::ICMP_SLT, LHSVal, LHSCst); |
| 3312 | break; // (X != 13 & X s< 15) -> no change |
| 3313 | case ICmpInst::ICMP_EQ: // (X != 13 & X == 15) -> X == 15 |
| 3314 | case ICmpInst::ICMP_UGT: // (X != 13 & X u> 15) -> X u> 15 |
| 3315 | case ICmpInst::ICMP_SGT: // (X != 13 & X s> 15) -> X s> 15 |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3316 | return ReplaceInstUsesWith(I, RHS); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3317 | case ICmpInst::ICMP_NE: |
| 3318 | if (LHSCst == SubOne(RHSCst)){// (X != 13 & X != 14) -> X-13 >u 1 |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3319 | Constant *AddCST = ConstantExpr::getNeg(LHSCst); |
| 3320 | Instruction *Add = BinaryOperator::createAdd(LHSVal, AddCST, |
| 3321 | LHSVal->getName()+".off"); |
| 3322 | InsertNewInstBefore(Add, I); |
Chris Lattner | c8fb6de | 2007-01-27 23:08:34 +0000 | [diff] [blame] | 3323 | return new ICmpInst(ICmpInst::ICMP_UGT, Add, |
| 3324 | ConstantInt::get(Add->getType(), 1)); |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3325 | } |
| 3326 | break; // (X != 13 & X != 15) -> no change |
| 3327 | } |
| 3328 | break; |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3329 | case ICmpInst::ICMP_ULT: |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3330 | switch (RHSCC) { |
| 3331 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3332 | case ICmpInst::ICMP_EQ: // (X u< 13 & X == 15) -> false |
| 3333 | case ICmpInst::ICMP_UGT: // (X u< 13 & X u> 15) -> false |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3334 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3335 | case ICmpInst::ICMP_SGT: // (X u< 13 & X s> 15) -> no change |
| 3336 | break; |
| 3337 | case ICmpInst::ICMP_NE: // (X u< 13 & X != 15) -> X u< 13 |
| 3338 | case ICmpInst::ICMP_ULT: // (X u< 13 & X u< 15) -> X u< 13 |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3339 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3340 | case ICmpInst::ICMP_SLT: // (X u< 13 & X s< 15) -> no change |
| 3341 | break; |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3342 | } |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3343 | break; |
| 3344 | case ICmpInst::ICMP_SLT: |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3345 | switch (RHSCC) { |
| 3346 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3347 | case ICmpInst::ICMP_EQ: // (X s< 13 & X == 15) -> false |
| 3348 | case ICmpInst::ICMP_SGT: // (X s< 13 & X s> 15) -> false |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3349 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3350 | case ICmpInst::ICMP_UGT: // (X s< 13 & X u> 15) -> no change |
| 3351 | break; |
| 3352 | case ICmpInst::ICMP_NE: // (X s< 13 & X != 15) -> X < 13 |
| 3353 | case ICmpInst::ICMP_SLT: // (X s< 13 & X s< 15) -> X < 13 |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3354 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3355 | case ICmpInst::ICMP_ULT: // (X s< 13 & X u< 15) -> no change |
| 3356 | break; |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3357 | } |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3358 | break; |
| 3359 | case ICmpInst::ICMP_UGT: |
| 3360 | switch (RHSCC) { |
| 3361 | default: assert(0 && "Unknown integer condition code!"); |
| 3362 | case ICmpInst::ICMP_EQ: // (X u> 13 & X == 15) -> X > 13 |
| 3363 | return ReplaceInstUsesWith(I, LHS); |
| 3364 | case ICmpInst::ICMP_UGT: // (X u> 13 & X u> 15) -> X u> 15 |
| 3365 | return ReplaceInstUsesWith(I, RHS); |
| 3366 | case ICmpInst::ICMP_SGT: // (X u> 13 & X s> 15) -> no change |
| 3367 | break; |
| 3368 | case ICmpInst::ICMP_NE: |
| 3369 | if (RHSCst == AddOne(LHSCst)) // (X u> 13 & X != 14) -> X u> 14 |
| 3370 | return new ICmpInst(LHSCC, LHSVal, RHSCst); |
| 3371 | break; // (X u> 13 & X != 15) -> no change |
| 3372 | case ICmpInst::ICMP_ULT: // (X u> 13 & X u< 15) ->(X-14) <u 1 |
| 3373 | return InsertRangeTest(LHSVal, AddOne(LHSCst), RHSCst, false, |
| 3374 | true, I); |
| 3375 | case ICmpInst::ICMP_SLT: // (X u> 13 & X s< 15) -> no change |
| 3376 | break; |
| 3377 | } |
| 3378 | break; |
| 3379 | case ICmpInst::ICMP_SGT: |
| 3380 | switch (RHSCC) { |
| 3381 | default: assert(0 && "Unknown integer condition code!"); |
| 3382 | case ICmpInst::ICMP_EQ: // (X s> 13 & X == 15) -> X s> 13 |
| 3383 | return ReplaceInstUsesWith(I, LHS); |
| 3384 | case ICmpInst::ICMP_SGT: // (X s> 13 & X s> 15) -> X s> 15 |
| 3385 | return ReplaceInstUsesWith(I, RHS); |
| 3386 | case ICmpInst::ICMP_UGT: // (X s> 13 & X u> 15) -> no change |
| 3387 | break; |
| 3388 | case ICmpInst::ICMP_NE: |
| 3389 | if (RHSCst == AddOne(LHSCst)) // (X s> 13 & X != 14) -> X s> 14 |
| 3390 | return new ICmpInst(LHSCC, LHSVal, RHSCst); |
| 3391 | break; // (X s> 13 & X != 15) -> no change |
| 3392 | case ICmpInst::ICMP_SLT: // (X s> 13 & X s< 15) ->(X-14) s< 1 |
| 3393 | return InsertRangeTest(LHSVal, AddOne(LHSCst), RHSCst, true, |
| 3394 | true, I); |
| 3395 | case ICmpInst::ICMP_ULT: // (X s> 13 & X u< 15) -> no change |
| 3396 | break; |
| 3397 | } |
| 3398 | break; |
Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3399 | } |
| 3400 | } |
| 3401 | } |
| 3402 | |
Chris Lattner | 3af1053 | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3403 | // fold (and (cast A), (cast B)) -> (cast (and A, B)) |
Reid Spencer | 799b5bf | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3404 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) |
| 3405 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) |
| 3406 | if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind ? |
| 3407 | const Type *SrcTy = Op0C->getOperand(0)->getType(); |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 3408 | if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() && |
Reid Spencer | 799b5bf | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3409 | // Only do this if the casts both really cause code to be generated. |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3410 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), |
| 3411 | I.getType(), TD) && |
| 3412 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), |
| 3413 | I.getType(), TD)) { |
Reid Spencer | 799b5bf | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3414 | Instruction *NewOp = BinaryOperator::createAnd(Op0C->getOperand(0), |
| 3415 | Op1C->getOperand(0), |
| 3416 | I.getName()); |
| 3417 | InsertNewInstBefore(NewOp, I); |
| 3418 | return CastInst::create(Op0C->getOpcode(), NewOp, I.getType()); |
| 3419 | } |
Chris Lattner | 3af1053 | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3420 | } |
Chris Lattner | f05d69a | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 3421 | |
| 3422 | // (X >> Z) & (Y >> Z) -> (X&Y) >> Z for all shifts. |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3423 | if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) { |
| 3424 | if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0)) |
| 3425 | if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() && |
Chris Lattner | f05d69a | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 3426 | SI0->getOperand(1) == SI1->getOperand(1) && |
| 3427 | (SI0->hasOneUse() || SI1->hasOneUse())) { |
| 3428 | Instruction *NewOp = |
| 3429 | InsertNewInstBefore(BinaryOperator::createAnd(SI0->getOperand(0), |
| 3430 | SI1->getOperand(0), |
| 3431 | SI0->getName()), I); |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3432 | return BinaryOperator::create(SI1->getOpcode(), NewOp, |
| 3433 | SI1->getOperand(1)); |
Chris Lattner | f05d69a | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 3434 | } |
Chris Lattner | 3af1053 | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3435 | } |
| 3436 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3437 | return Changed ? &I : 0; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3438 | } |
| 3439 | |
Chris Lattner | c482a9e | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3440 | /// CollectBSwapParts - Look to see if the specified value defines a single byte |
| 3441 | /// in the result. If it does, and if the specified byte hasn't been filled in |
| 3442 | /// yet, fill it in and return false. |
Chris Lattner | 99c6cf6 | 2007-02-15 22:52:10 +0000 | [diff] [blame] | 3443 | static bool CollectBSwapParts(Value *V, SmallVector<Value*, 8> &ByteValues) { |
Chris Lattner | c482a9e | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3444 | Instruction *I = dyn_cast<Instruction>(V); |
| 3445 | if (I == 0) return true; |
| 3446 | |
| 3447 | // If this is an or instruction, it is an inner node of the bswap. |
| 3448 | if (I->getOpcode() == Instruction::Or) |
| 3449 | return CollectBSwapParts(I->getOperand(0), ByteValues) || |
| 3450 | CollectBSwapParts(I->getOperand(1), ByteValues); |
| 3451 | |
| 3452 | // If this is a shift by a constant int, and it is "24", then its operand |
| 3453 | // defines a byte. We only handle unsigned types here. |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3454 | if (I->isShift() && isa<ConstantInt>(I->getOperand(1))) { |
Chris Lattner | c482a9e | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3455 | // Not shifting the entire input by N-1 bytes? |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3456 | if (cast<ConstantInt>(I->getOperand(1))->getZExtValue() != |
Chris Lattner | c482a9e | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3457 | 8*(ByteValues.size()-1)) |
| 3458 | return true; |
| 3459 | |
| 3460 | unsigned DestNo; |
| 3461 | if (I->getOpcode() == Instruction::Shl) { |
| 3462 | // X << 24 defines the top byte with the lowest of the input bytes. |
| 3463 | DestNo = ByteValues.size()-1; |
| 3464 | } else { |
| 3465 | // X >>u 24 defines the low byte with the highest of the input bytes. |
| 3466 | DestNo = 0; |
| 3467 | } |
| 3468 | |
| 3469 | // If the destination byte value is already defined, the values are or'd |
| 3470 | // together, which isn't a bswap (unless it's an or of the same bits). |
| 3471 | if (ByteValues[DestNo] && ByteValues[DestNo] != I->getOperand(0)) |
| 3472 | return true; |
| 3473 | ByteValues[DestNo] = I->getOperand(0); |
| 3474 | return false; |
| 3475 | } |
| 3476 | |
| 3477 | // Otherwise, we can only handle and(shift X, imm), imm). Bail out of if we |
| 3478 | // don't have this. |
| 3479 | Value *Shift = 0, *ShiftLHS = 0; |
| 3480 | ConstantInt *AndAmt = 0, *ShiftAmt = 0; |
| 3481 | if (!match(I, m_And(m_Value(Shift), m_ConstantInt(AndAmt))) || |
| 3482 | !match(Shift, m_Shift(m_Value(ShiftLHS), m_ConstantInt(ShiftAmt)))) |
| 3483 | return true; |
| 3484 | Instruction *SI = cast<Instruction>(Shift); |
| 3485 | |
| 3486 | // Make sure that the shift amount is by a multiple of 8 and isn't too big. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3487 | if (ShiftAmt->getZExtValue() & 7 || |
| 3488 | ShiftAmt->getZExtValue() > 8*ByteValues.size()) |
Chris Lattner | c482a9e | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3489 | return true; |
| 3490 | |
| 3491 | // Turn 0xFF -> 0, 0xFF00 -> 1, 0xFF0000 -> 2, etc. |
| 3492 | unsigned DestByte; |
| 3493 | for (DestByte = 0; DestByte != ByteValues.size(); ++DestByte) |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3494 | if (AndAmt->getZExtValue() == uint64_t(0xFF) << 8*DestByte) |
Chris Lattner | c482a9e | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3495 | break; |
| 3496 | // Unknown mask for bswap. |
| 3497 | if (DestByte == ByteValues.size()) return true; |
| 3498 | |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3499 | unsigned ShiftBytes = ShiftAmt->getZExtValue()/8; |
Chris Lattner | c482a9e | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3500 | unsigned SrcByte; |
| 3501 | if (SI->getOpcode() == Instruction::Shl) |
| 3502 | SrcByte = DestByte - ShiftBytes; |
| 3503 | else |
| 3504 | SrcByte = DestByte + ShiftBytes; |
| 3505 | |
| 3506 | // If the SrcByte isn't a bswapped value from the DestByte, reject it. |
| 3507 | if (SrcByte != ByteValues.size()-DestByte-1) |
| 3508 | return true; |
| 3509 | |
| 3510 | // If the destination byte value is already defined, the values are or'd |
| 3511 | // together, which isn't a bswap (unless it's an or of the same bits). |
| 3512 | if (ByteValues[DestByte] && ByteValues[DestByte] != SI->getOperand(0)) |
| 3513 | return true; |
| 3514 | ByteValues[DestByte] = SI->getOperand(0); |
| 3515 | return false; |
| 3516 | } |
| 3517 | |
| 3518 | /// MatchBSwap - Given an OR instruction, check to see if this is a bswap idiom. |
| 3519 | /// If so, insert the new bswap intrinsic and return it. |
| 3520 | Instruction *InstCombiner::MatchBSwap(BinaryOperator &I) { |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3521 | // We cannot bswap one byte. |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 3522 | if (I.getType() == Type::Int8Ty) |
Chris Lattner | c482a9e | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3523 | return 0; |
| 3524 | |
| 3525 | /// ByteValues - For each byte of the result, we keep track of which value |
| 3526 | /// defines each byte. |
Chris Lattner | 99c6cf6 | 2007-02-15 22:52:10 +0000 | [diff] [blame] | 3527 | SmallVector<Value*, 8> ByteValues; |
Reid Spencer | 7a9c62b | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 3528 | ByteValues.resize(TD->getTypeSize(I.getType())); |
Chris Lattner | c482a9e | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3529 | |
| 3530 | // Try to find all the pieces corresponding to the bswap. |
| 3531 | if (CollectBSwapParts(I.getOperand(0), ByteValues) || |
| 3532 | CollectBSwapParts(I.getOperand(1), ByteValues)) |
| 3533 | return 0; |
| 3534 | |
| 3535 | // Check to see if all of the bytes come from the same value. |
| 3536 | Value *V = ByteValues[0]; |
| 3537 | if (V == 0) return 0; // Didn't find a byte? Must be zero. |
| 3538 | |
| 3539 | // Check to make sure that all of the bytes come from the same value. |
| 3540 | for (unsigned i = 1, e = ByteValues.size(); i != e; ++i) |
| 3541 | if (ByteValues[i] != V) |
| 3542 | return 0; |
| 3543 | |
| 3544 | // If they do then *success* we can turn this into a bswap. Figure out what |
| 3545 | // bswap to make it into. |
| 3546 | Module *M = I.getParent()->getParent()->getParent(); |
Chris Lattner | 091b6ea | 2006-07-11 18:31:26 +0000 | [diff] [blame] | 3547 | const char *FnName = 0; |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 3548 | if (I.getType() == Type::Int16Ty) |
Chris Lattner | c482a9e | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3549 | FnName = "llvm.bswap.i16"; |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 3550 | else if (I.getType() == Type::Int32Ty) |
Chris Lattner | c482a9e | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3551 | FnName = "llvm.bswap.i32"; |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 3552 | else if (I.getType() == Type::Int64Ty) |
Chris Lattner | c482a9e | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3553 | FnName = "llvm.bswap.i64"; |
| 3554 | else |
| 3555 | assert(0 && "Unknown integer type!"); |
Chris Lattner | fbc524f | 2007-01-07 06:58:05 +0000 | [diff] [blame] | 3556 | Constant *F = M->getOrInsertFunction(FnName, I.getType(), I.getType(), NULL); |
Chris Lattner | c482a9e | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3557 | return new CallInst(F, V); |
| 3558 | } |
| 3559 | |
| 3560 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3561 | Instruction *InstCombiner::visitOr(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 3562 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3563 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3564 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3565 | if (isa<UndefValue>(Op1)) |
| 3566 | return ReplaceInstUsesWith(I, // X | undef -> -1 |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3567 | ConstantInt::getAllOnesValue(I.getType())); |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3568 | |
Chris Lattner | 5b2edb1 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3569 | // or X, X = X |
| 3570 | if (Op0 == Op1) |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 3571 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3572 | |
Chris Lattner | 5b2edb1 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3573 | // See if we can simplify any instructions used by the instruction whose sole |
| 3574 | // purpose is to compute bits we don't care about. |
| 3575 | uint64_t KnownZero, KnownOne; |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 3576 | if (!isa<VectorType>(I.getType()) && |
Reid Spencer | a94d394 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 3577 | SimplifyDemandedBits(&I, cast<IntegerType>(I.getType())->getBitMask(), |
Chris Lattner | 5b2edb1 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3578 | KnownZero, KnownOne)) |
| 3579 | return &I; |
| 3580 | |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3581 | // or X, -1 == -1 |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3582 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 330628a | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 3583 | ConstantInt *C1 = 0; Value *X = 0; |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3584 | // (X & C1) | C2 --> (X | C2) & (C1|C2) |
| 3585 | if (match(Op0, m_And(m_Value(X), m_ConstantInt(C1))) && isOnlyUse(Op0)) { |
Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3586 | Instruction *Or = BinaryOperator::createOr(X, RHS); |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3587 | InsertNewInstBefore(Or, I); |
Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3588 | Or->takeName(Op0); |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3589 | return BinaryOperator::createAnd(Or, ConstantExpr::getOr(RHS, C1)); |
| 3590 | } |
Chris Lattner | 8f0d156 | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 3591 | |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3592 | // (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2) |
| 3593 | if (match(Op0, m_Xor(m_Value(X), m_ConstantInt(C1))) && isOnlyUse(Op0)) { |
Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3594 | Instruction *Or = BinaryOperator::createOr(X, RHS); |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3595 | InsertNewInstBefore(Or, I); |
Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3596 | Or->takeName(Op0); |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3597 | return BinaryOperator::createXor(Or, |
| 3598 | ConstantExpr::getAnd(C1, ConstantExpr::getNot(RHS))); |
Chris Lattner | 8f0d156 | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 3599 | } |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3600 | |
| 3601 | // Try to fold constant and into select arguments. |
| 3602 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3603 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3604 | return R; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 3605 | if (isa<PHINode>(Op0)) |
| 3606 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 3607 | return NV; |
Chris Lattner | 8f0d156 | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 3608 | } |
| 3609 | |
Chris Lattner | 330628a | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 3610 | Value *A = 0, *B = 0; |
| 3611 | ConstantInt *C1 = 0, *C2 = 0; |
Chris Lattner | 4294cec | 2005-05-07 23:49:08 +0000 | [diff] [blame] | 3612 | |
| 3613 | if (match(Op0, m_And(m_Value(A), m_Value(B)))) |
| 3614 | if (A == Op1 || B == Op1) // (A & ?) | A --> A |
| 3615 | return ReplaceInstUsesWith(I, Op1); |
| 3616 | if (match(Op1, m_And(m_Value(A), m_Value(B)))) |
| 3617 | if (A == Op0 || B == Op0) // A | (A & ?) --> A |
| 3618 | return ReplaceInstUsesWith(I, Op0); |
| 3619 | |
Chris Lattner | b7845d6 | 2006-07-10 20:25:24 +0000 | [diff] [blame] | 3620 | // (A | B) | C and A | (B | C) -> bswap if possible. |
| 3621 | // (A >> B) | (C << D) and (A << B) | (B >> C) -> bswap if possible. |
Chris Lattner | c482a9e | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3622 | if (match(Op0, m_Or(m_Value(), m_Value())) || |
Chris Lattner | b7845d6 | 2006-07-10 20:25:24 +0000 | [diff] [blame] | 3623 | match(Op1, m_Or(m_Value(), m_Value())) || |
| 3624 | (match(Op0, m_Shift(m_Value(), m_Value())) && |
| 3625 | match(Op1, m_Shift(m_Value(), m_Value())))) { |
Chris Lattner | c482a9e | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3626 | if (Instruction *BSwap = MatchBSwap(I)) |
| 3627 | return BSwap; |
| 3628 | } |
| 3629 | |
Chris Lattner | b62f508 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 3630 | // (X^C)|Y -> (X|Y)^C iff Y&C == 0 |
| 3631 | if (Op0->hasOneUse() && match(Op0, m_Xor(m_Value(A), m_ConstantInt(C1))) && |
Chris Lattner | c3ebf40 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3632 | MaskedValueIsZero(Op1, C1->getZExtValue())) { |
Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3633 | Instruction *NOr = BinaryOperator::createOr(A, Op1); |
| 3634 | InsertNewInstBefore(NOr, I); |
| 3635 | NOr->takeName(Op0); |
| 3636 | return BinaryOperator::createXor(NOr, C1); |
Chris Lattner | b62f508 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 3637 | } |
| 3638 | |
| 3639 | // Y|(X^C) -> (X|Y)^C iff Y&C == 0 |
| 3640 | if (Op1->hasOneUse() && match(Op1, m_Xor(m_Value(A), m_ConstantInt(C1))) && |
Chris Lattner | c3ebf40 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3641 | MaskedValueIsZero(Op0, C1->getZExtValue())) { |
Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3642 | Instruction *NOr = BinaryOperator::createOr(A, Op0); |
| 3643 | InsertNewInstBefore(NOr, I); |
| 3644 | NOr->takeName(Op0); |
| 3645 | return BinaryOperator::createXor(NOr, C1); |
Chris Lattner | b62f508 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 3646 | } |
| 3647 | |
Chris Lattner | 1521298 | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 3648 | // (A & C1)|(B & C2) |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3649 | if (match(Op0, m_And(m_Value(A), m_ConstantInt(C1))) && |
Chris Lattner | 1521298 | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 3650 | match(Op1, m_And(m_Value(B), m_ConstantInt(C2)))) { |
| 3651 | |
| 3652 | if (A == B) // (A & C1)|(A & C2) == A & (C1|C2) |
| 3653 | return BinaryOperator::createAnd(A, ConstantExpr::getOr(C1, C2)); |
| 3654 | |
| 3655 | |
Chris Lattner | 01f56c6 | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 3656 | // If we have: ((V + N) & C1) | (V & C2) |
| 3657 | // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0 |
| 3658 | // replace with V+N. |
| 3659 | if (C1 == ConstantExpr::getNot(C2)) { |
Chris Lattner | 330628a | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 3660 | Value *V1 = 0, *V2 = 0; |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3661 | if ((C2->getZExtValue() & (C2->getZExtValue()+1)) == 0 && // C2 == 0+1+ |
Chris Lattner | 01f56c6 | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 3662 | match(A, m_Add(m_Value(V1), m_Value(V2)))) { |
| 3663 | // Add commutes, try both ways. |
Chris Lattner | c3ebf40 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3664 | if (V1 == B && MaskedValueIsZero(V2, C2->getZExtValue())) |
Chris Lattner | 01f56c6 | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 3665 | return ReplaceInstUsesWith(I, A); |
Chris Lattner | c3ebf40 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3666 | if (V2 == B && MaskedValueIsZero(V1, C2->getZExtValue())) |
Chris Lattner | 01f56c6 | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 3667 | return ReplaceInstUsesWith(I, A); |
| 3668 | } |
| 3669 | // Or commutes, try both ways. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3670 | if ((C1->getZExtValue() & (C1->getZExtValue()+1)) == 0 && |
Chris Lattner | 01f56c6 | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 3671 | match(B, m_Add(m_Value(V1), m_Value(V2)))) { |
| 3672 | // Add commutes, try both ways. |
Chris Lattner | c3ebf40 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3673 | if (V1 == A && MaskedValueIsZero(V2, C1->getZExtValue())) |
Chris Lattner | 01f56c6 | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 3674 | return ReplaceInstUsesWith(I, B); |
Chris Lattner | c3ebf40 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3675 | if (V2 == A && MaskedValueIsZero(V1, C1->getZExtValue())) |
Chris Lattner | 01f56c6 | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 3676 | return ReplaceInstUsesWith(I, B); |
Chris Lattner | 1521298 | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 3677 | } |
| 3678 | } |
| 3679 | } |
Chris Lattner | f05d69a | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 3680 | |
| 3681 | // (X >> Z) | (Y >> Z) -> (X|Y) >> Z for all shifts. |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3682 | if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) { |
| 3683 | if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0)) |
| 3684 | if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() && |
Chris Lattner | f05d69a | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 3685 | SI0->getOperand(1) == SI1->getOperand(1) && |
| 3686 | (SI0->hasOneUse() || SI1->hasOneUse())) { |
| 3687 | Instruction *NewOp = |
| 3688 | InsertNewInstBefore(BinaryOperator::createOr(SI0->getOperand(0), |
| 3689 | SI1->getOperand(0), |
| 3690 | SI0->getName()), I); |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3691 | return BinaryOperator::create(SI1->getOpcode(), NewOp, |
| 3692 | SI1->getOperand(1)); |
Chris Lattner | f05d69a | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 3693 | } |
| 3694 | } |
Chris Lattner | 812aab7 | 2003-08-12 19:11:07 +0000 | [diff] [blame] | 3695 | |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3696 | if (match(Op0, m_Not(m_Value(A)))) { // ~A | Op1 |
| 3697 | if (A == Op1) // ~A | A == -1 |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3698 | return ReplaceInstUsesWith(I, |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3699 | ConstantInt::getAllOnesValue(I.getType())); |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3700 | } else { |
| 3701 | A = 0; |
| 3702 | } |
Chris Lattner | 4294cec | 2005-05-07 23:49:08 +0000 | [diff] [blame] | 3703 | // Note, A is still live here! |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3704 | if (match(Op1, m_Not(m_Value(B)))) { // Op0 | ~B |
| 3705 | if (Op0 == B) |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3706 | return ReplaceInstUsesWith(I, |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3707 | ConstantInt::getAllOnesValue(I.getType())); |
Chris Lattner | 3e327a4 | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 3708 | |
Misha Brukman | 9c003d8 | 2004-07-30 12:50:08 +0000 | [diff] [blame] | 3709 | // (~A | ~B) == (~(A & B)) - De Morgan's Law |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3710 | if (A && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
| 3711 | Value *And = InsertNewInstBefore(BinaryOperator::createAnd(A, B, |
| 3712 | I.getName()+".demorgan"), I); |
| 3713 | return BinaryOperator::createNot(And); |
| 3714 | } |
Chris Lattner | 3e327a4 | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 3715 | } |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3716 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3717 | // (icmp1 A, B) | (icmp2 A, B) --> (icmp3 A, B) |
| 3718 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1))) { |
| 3719 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS))) |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3720 | return R; |
| 3721 | |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3722 | Value *LHSVal, *RHSVal; |
| 3723 | ConstantInt *LHSCst, *RHSCst; |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3724 | ICmpInst::Predicate LHSCC, RHSCC; |
| 3725 | if (match(Op0, m_ICmp(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst)))) |
| 3726 | if (match(RHS, m_ICmp(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst)))) |
| 3727 | if (LHSVal == RHSVal && // Found (X icmp C1) | (X icmp C2) |
| 3728 | // icmp [us][gl]e x, cst is folded to icmp [us][gl]t elsewhere. |
| 3729 | LHSCC != ICmpInst::ICMP_UGE && LHSCC != ICmpInst::ICMP_ULE && |
| 3730 | RHSCC != ICmpInst::ICMP_UGE && RHSCC != ICmpInst::ICMP_ULE && |
| 3731 | LHSCC != ICmpInst::ICMP_SGE && LHSCC != ICmpInst::ICMP_SLE && |
| 3732 | RHSCC != ICmpInst::ICMP_SGE && RHSCC != ICmpInst::ICMP_SLE) { |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3733 | // Ensure that the larger constant is on the RHS. |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3734 | ICmpInst::Predicate GT = ICmpInst::isSignedPredicate(LHSCC) ? |
| 3735 | ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; |
| 3736 | Constant *Cmp = ConstantExpr::getICmp(GT, LHSCst, RHSCst); |
| 3737 | ICmpInst *LHS = cast<ICmpInst>(Op0); |
Reid Spencer | cddc9df | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 3738 | if (cast<ConstantInt>(Cmp)->getZExtValue()) { |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3739 | std::swap(LHS, RHS); |
| 3740 | std::swap(LHSCst, RHSCst); |
| 3741 | std::swap(LHSCC, RHSCC); |
| 3742 | } |
| 3743 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3744 | // At this point, we know we have have two icmp instructions |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3745 | // comparing a value against two constants and or'ing the result |
| 3746 | // together. Because of the above check, we know that we only have |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3747 | // ICMP_EQ, ICMP_NE, ICMP_LT, and ICMP_GT here. We also know (from the |
| 3748 | // FoldICmpLogical check above), that the two constants are not |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3749 | // equal. |
| 3750 | assert(LHSCst != RHSCst && "Compares not folded above?"); |
| 3751 | |
| 3752 | switch (LHSCC) { |
| 3753 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3754 | case ICmpInst::ICMP_EQ: |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3755 | switch (RHSCC) { |
| 3756 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3757 | case ICmpInst::ICMP_EQ: |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3758 | if (LHSCst == SubOne(RHSCst)) {// (X == 13 | X == 14) -> X-13 <u 2 |
| 3759 | Constant *AddCST = ConstantExpr::getNeg(LHSCst); |
| 3760 | Instruction *Add = BinaryOperator::createAdd(LHSVal, AddCST, |
| 3761 | LHSVal->getName()+".off"); |
| 3762 | InsertNewInstBefore(Add, I); |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3763 | AddCST = ConstantExpr::getSub(AddOne(RHSCst), LHSCst); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3764 | return new ICmpInst(ICmpInst::ICMP_ULT, Add, AddCST); |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3765 | } |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3766 | break; // (X == 13 | X == 15) -> no change |
| 3767 | case ICmpInst::ICMP_UGT: // (X == 13 | X u> 14) -> no change |
| 3768 | case ICmpInst::ICMP_SGT: // (X == 13 | X s> 14) -> no change |
Chris Lattner | 5c21946 | 2005-04-19 06:04:18 +0000 | [diff] [blame] | 3769 | break; |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3770 | case ICmpInst::ICMP_NE: // (X == 13 | X != 15) -> X != 15 |
| 3771 | case ICmpInst::ICMP_ULT: // (X == 13 | X u< 15) -> X u< 15 |
| 3772 | case ICmpInst::ICMP_SLT: // (X == 13 | X s< 15) -> X s< 15 |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3773 | return ReplaceInstUsesWith(I, RHS); |
| 3774 | } |
| 3775 | break; |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3776 | case ICmpInst::ICMP_NE: |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3777 | switch (RHSCC) { |
| 3778 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3779 | case ICmpInst::ICMP_EQ: // (X != 13 | X == 15) -> X != 13 |
| 3780 | case ICmpInst::ICMP_UGT: // (X != 13 | X u> 15) -> X != 13 |
| 3781 | case ICmpInst::ICMP_SGT: // (X != 13 | X s> 15) -> X != 13 |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3782 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3783 | case ICmpInst::ICMP_NE: // (X != 13 | X != 15) -> true |
| 3784 | case ICmpInst::ICMP_ULT: // (X != 13 | X u< 15) -> true |
| 3785 | case ICmpInst::ICMP_SLT: // (X != 13 | X s< 15) -> true |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3786 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3787 | } |
| 3788 | break; |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3789 | case ICmpInst::ICMP_ULT: |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3790 | switch (RHSCC) { |
| 3791 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3792 | case ICmpInst::ICMP_EQ: // (X u< 13 | X == 14) -> no change |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3793 | break; |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3794 | case ICmpInst::ICMP_UGT: // (X u< 13 | X u> 15) ->(X-13) u> 2 |
| 3795 | return InsertRangeTest(LHSVal, LHSCst, AddOne(RHSCst), false, |
| 3796 | false, I); |
| 3797 | case ICmpInst::ICMP_SGT: // (X u< 13 | X s> 15) -> no change |
| 3798 | break; |
| 3799 | case ICmpInst::ICMP_NE: // (X u< 13 | X != 15) -> X != 15 |
| 3800 | case ICmpInst::ICMP_ULT: // (X u< 13 | X u< 15) -> X u< 15 |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3801 | return ReplaceInstUsesWith(I, RHS); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3802 | case ICmpInst::ICMP_SLT: // (X u< 13 | X s< 15) -> no change |
| 3803 | break; |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3804 | } |
| 3805 | break; |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3806 | case ICmpInst::ICMP_SLT: |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3807 | switch (RHSCC) { |
| 3808 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3809 | case ICmpInst::ICMP_EQ: // (X s< 13 | X == 14) -> no change |
| 3810 | break; |
| 3811 | case ICmpInst::ICMP_SGT: // (X s< 13 | X s> 15) ->(X-13) s> 2 |
| 3812 | return InsertRangeTest(LHSVal, LHSCst, AddOne(RHSCst), true, |
| 3813 | false, I); |
| 3814 | case ICmpInst::ICMP_UGT: // (X s< 13 | X u> 15) -> no change |
| 3815 | break; |
| 3816 | case ICmpInst::ICMP_NE: // (X s< 13 | X != 15) -> X != 15 |
| 3817 | case ICmpInst::ICMP_SLT: // (X s< 13 | X s< 15) -> X s< 15 |
| 3818 | return ReplaceInstUsesWith(I, RHS); |
| 3819 | case ICmpInst::ICMP_ULT: // (X s< 13 | X u< 15) -> no change |
| 3820 | break; |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3821 | } |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3822 | break; |
| 3823 | case ICmpInst::ICMP_UGT: |
| 3824 | switch (RHSCC) { |
| 3825 | default: assert(0 && "Unknown integer condition code!"); |
| 3826 | case ICmpInst::ICMP_EQ: // (X u> 13 | X == 15) -> X u> 13 |
| 3827 | case ICmpInst::ICMP_UGT: // (X u> 13 | X u> 15) -> X u> 13 |
| 3828 | return ReplaceInstUsesWith(I, LHS); |
| 3829 | case ICmpInst::ICMP_SGT: // (X u> 13 | X s> 15) -> no change |
| 3830 | break; |
| 3831 | case ICmpInst::ICMP_NE: // (X u> 13 | X != 15) -> true |
| 3832 | case ICmpInst::ICMP_ULT: // (X u> 13 | X u< 15) -> true |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3833 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3834 | case ICmpInst::ICMP_SLT: // (X u> 13 | X s< 15) -> no change |
| 3835 | break; |
| 3836 | } |
| 3837 | break; |
| 3838 | case ICmpInst::ICMP_SGT: |
| 3839 | switch (RHSCC) { |
| 3840 | default: assert(0 && "Unknown integer condition code!"); |
| 3841 | case ICmpInst::ICMP_EQ: // (X s> 13 | X == 15) -> X > 13 |
| 3842 | case ICmpInst::ICMP_SGT: // (X s> 13 | X s> 15) -> X > 13 |
| 3843 | return ReplaceInstUsesWith(I, LHS); |
| 3844 | case ICmpInst::ICMP_UGT: // (X s> 13 | X u> 15) -> no change |
| 3845 | break; |
| 3846 | case ICmpInst::ICMP_NE: // (X s> 13 | X != 15) -> true |
| 3847 | case ICmpInst::ICMP_SLT: // (X s> 13 | X s< 15) -> true |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3848 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3849 | case ICmpInst::ICMP_ULT: // (X s> 13 | X u< 15) -> no change |
| 3850 | break; |
| 3851 | } |
| 3852 | break; |
Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3853 | } |
| 3854 | } |
| 3855 | } |
Chris Lattner | 3af1053 | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3856 | |
| 3857 | // fold (or (cast A), (cast B)) -> (cast (or A, B)) |
Reid Spencer | 799b5bf | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3858 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) |
Chris Lattner | 3af1053 | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3859 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) |
Reid Spencer | 799b5bf | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3860 | if (Op0C->getOpcode() == Op1C->getOpcode()) {// same cast kind ? |
| 3861 | const Type *SrcTy = Op0C->getOperand(0)->getType(); |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 3862 | if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() && |
Reid Spencer | 799b5bf | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3863 | // Only do this if the casts both really cause code to be generated. |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3864 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), |
| 3865 | I.getType(), TD) && |
| 3866 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), |
| 3867 | I.getType(), TD)) { |
Reid Spencer | 799b5bf | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3868 | Instruction *NewOp = BinaryOperator::createOr(Op0C->getOperand(0), |
| 3869 | Op1C->getOperand(0), |
| 3870 | I.getName()); |
| 3871 | InsertNewInstBefore(NewOp, I); |
| 3872 | return CastInst::create(Op0C->getOpcode(), NewOp, I.getType()); |
| 3873 | } |
Chris Lattner | 3af1053 | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3874 | } |
Chris Lattner | 3af1053 | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3875 | |
Chris Lattner | 1521298 | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 3876 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3877 | return Changed ? &I : 0; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3878 | } |
| 3879 | |
Chris Lattner | c207635 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 3880 | // XorSelf - Implements: X ^ X --> 0 |
| 3881 | struct XorSelf { |
| 3882 | Value *RHS; |
| 3883 | XorSelf(Value *rhs) : RHS(rhs) {} |
| 3884 | bool shouldApply(Value *LHS) const { return LHS == RHS; } |
| 3885 | Instruction *apply(BinaryOperator &Xor) const { |
| 3886 | return &Xor; |
| 3887 | } |
| 3888 | }; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3889 | |
| 3890 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3891 | Instruction *InstCombiner::visitXor(BinaryOperator &I) { |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 3892 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3893 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3894 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3895 | if (isa<UndefValue>(Op1)) |
| 3896 | return ReplaceInstUsesWith(I, Op1); // X ^ undef -> undef |
| 3897 | |
Chris Lattner | c207635 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 3898 | // xor X, X = 0, even if X is nested in a sequence of Xor's. |
| 3899 | if (Instruction *Result = AssociativeOpt(I, XorSelf(Op1))) { |
| 3900 | assert(Result == &I && "AssociativeOpt didn't work?"); |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 3901 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | c207635 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 3902 | } |
Chris Lattner | 5b2edb1 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3903 | |
| 3904 | // See if we can simplify any instructions used by the instruction whose sole |
| 3905 | // purpose is to compute bits we don't care about. |
| 3906 | uint64_t KnownZero, KnownOne; |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 3907 | if (!isa<VectorType>(I.getType()) && |
Reid Spencer | a94d394 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 3908 | SimplifyDemandedBits(&I, cast<IntegerType>(I.getType())->getBitMask(), |
Chris Lattner | 5b2edb1 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3909 | KnownZero, KnownOne)) |
| 3910 | return &I; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3911 | |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3912 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3913 | // xor (icmp A, B), true = not (icmp A, B) = !icmp A, B |
| 3914 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(Op0)) |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3915 | if (RHS == ConstantInt::getTrue() && ICI->hasOneUse()) |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3916 | return new ICmpInst(ICI->getInversePredicate(), |
| 3917 | ICI->getOperand(0), ICI->getOperand(1)); |
Chris Lattner | e580666 | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 3918 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3919 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) { |
Chris Lattner | 8f2f598 | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 3920 | // ~(c-X) == X-c-1 == X+(-c-1) |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3921 | if (Op0I->getOpcode() == Instruction::Sub && RHS->isAllOnesValue()) |
| 3922 | if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) { |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3923 | Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C); |
| 3924 | Constant *ConstantRHS = ConstantExpr::getSub(NegOp0I0C, |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3925 | ConstantInt::get(I.getType(), 1)); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3926 | return BinaryOperator::createAdd(Op0I->getOperand(1), ConstantRHS); |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3927 | } |
Chris Lattner | 023a483 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 3928 | |
| 3929 | // ~(~X & Y) --> (X | ~Y) |
| 3930 | if (Op0I->getOpcode() == Instruction::And && RHS->isAllOnesValue()) { |
| 3931 | if (dyn_castNotVal(Op0I->getOperand(1))) Op0I->swapOperands(); |
| 3932 | if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0))) { |
| 3933 | Instruction *NotY = |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3934 | BinaryOperator::createNot(Op0I->getOperand(1), |
Chris Lattner | 023a483 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 3935 | Op0I->getOperand(1)->getName()+".not"); |
| 3936 | InsertNewInstBefore(NotY, I); |
| 3937 | return BinaryOperator::createOr(Op0NotVal, NotY); |
| 3938 | } |
| 3939 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3940 | |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 3941 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) |
Chris Lattner | 5b2edb1 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3942 | if (Op0I->getOpcode() == Instruction::Add) { |
Chris Lattner | 0f68fa6 | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 3943 | // ~(X-c) --> (-c-1)-X |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3944 | if (RHS->isAllOnesValue()) { |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3945 | Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI); |
| 3946 | return BinaryOperator::createSub( |
| 3947 | ConstantExpr::getSub(NegOp0CI, |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3948 | ConstantInt::get(I.getType(), 1)), |
Chris Lattner | 0f68fa6 | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 3949 | Op0I->getOperand(0)); |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3950 | } |
Chris Lattner | f78df7c | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 3951 | } else if (Op0I->getOpcode() == Instruction::Or) { |
| 3952 | // (X|C1)^C2 -> X^(C1|C2) iff X&~C1 == 0 |
| 3953 | if (MaskedValueIsZero(Op0I->getOperand(0), Op0CI->getZExtValue())) { |
| 3954 | Constant *NewRHS = ConstantExpr::getOr(Op0CI, RHS); |
| 3955 | // Anything in both C1 and C2 is known to be zero, remove it from |
| 3956 | // NewRHS. |
| 3957 | Constant *CommonBits = ConstantExpr::getAnd(Op0CI, RHS); |
| 3958 | NewRHS = ConstantExpr::getAnd(NewRHS, |
| 3959 | ConstantExpr::getNot(CommonBits)); |
Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 3960 | AddToWorkList(Op0I); |
Chris Lattner | f78df7c | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 3961 | I.setOperand(0, Op0I->getOperand(0)); |
| 3962 | I.setOperand(1, NewRHS); |
| 3963 | return &I; |
| 3964 | } |
Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 3965 | } |
Chris Lattner | b8d6e40 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 3966 | } |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3967 | |
| 3968 | // Try to fold constant and into select arguments. |
| 3969 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3970 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3971 | return R; |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 3972 | if (isa<PHINode>(Op0)) |
| 3973 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 3974 | return NV; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3975 | } |
| 3976 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 3977 | if (Value *X = dyn_castNotVal(Op0)) // ~A ^ A == -1 |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3978 | if (X == Op1) |
| 3979 | return ReplaceInstUsesWith(I, |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3980 | ConstantInt::getAllOnesValue(I.getType())); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3981 | |
Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 3982 | if (Value *X = dyn_castNotVal(Op1)) // A ^ ~A == -1 |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3983 | if (X == Op0) |
| 3984 | return ReplaceInstUsesWith(I, |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3985 | ConstantInt::getAllOnesValue(I.getType())); |
Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3986 | |
Chris Lattner | dcd0792 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 3987 | if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) |
Chris Lattner | b36d908 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 3988 | if (Op1I->getOpcode() == Instruction::Or) { |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 3989 | if (Op1I->getOperand(0) == Op0) { // B^(B|A) == (A|B)^B |
Chris Lattner | dcd0792 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 3990 | Op1I->swapOperands(); |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 3991 | I.swapOperands(); |
| 3992 | std::swap(Op0, Op1); |
| 3993 | } else if (Op1I->getOperand(1) == Op0) { // B^(A|B) == (A|B)^B |
Chris Lattner | dcd0792 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 3994 | I.swapOperands(); // Simplified below. |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 3995 | std::swap(Op0, Op1); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3996 | } |
Chris Lattner | b36d908 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 3997 | } else if (Op1I->getOpcode() == Instruction::Xor) { |
| 3998 | if (Op0 == Op1I->getOperand(0)) // A^(A^B) == B |
| 3999 | return ReplaceInstUsesWith(I, Op1I->getOperand(1)); |
| 4000 | else if (Op0 == Op1I->getOperand(1)) // A^(B^A) == B |
| 4001 | return ReplaceInstUsesWith(I, Op1I->getOperand(0)); |
Chris Lattner | dcd0792 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4002 | } else if (Op1I->getOpcode() == Instruction::And && Op1I->hasOneUse()) { |
| 4003 | if (Op1I->getOperand(0) == Op0) // A^(A&B) -> A^(B&A) |
| 4004 | Op1I->swapOperands(); |
| 4005 | if (Op0 == Op1I->getOperand(1)) { // A^(B&A) -> (B&A)^A |
| 4006 | I.swapOperands(); // Simplified below. |
| 4007 | std::swap(Op0, Op1); |
| 4008 | } |
Chris Lattner | b36d908 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 4009 | } |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4010 | |
Chris Lattner | dcd0792 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4011 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 4012 | if (Op0I->getOpcode() == Instruction::Or && Op0I->hasOneUse()) { |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4013 | if (Op0I->getOperand(0) == Op1) // (B|A)^B == (A|B)^B |
Chris Lattner | dcd0792 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4014 | Op0I->swapOperands(); |
Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 4015 | if (Op0I->getOperand(1) == Op1) { // (A|B)^B == A & ~B |
Chris Lattner | dcd0792 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4016 | Instruction *NotB = BinaryOperator::createNot(Op1, "tmp"); |
| 4017 | InsertNewInstBefore(NotB, I); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4018 | return BinaryOperator::createAnd(Op0I->getOperand(0), NotB); |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4019 | } |
Chris Lattner | b36d908 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 4020 | } else if (Op0I->getOpcode() == Instruction::Xor) { |
| 4021 | if (Op1 == Op0I->getOperand(0)) // (A^B)^A == B |
| 4022 | return ReplaceInstUsesWith(I, Op0I->getOperand(1)); |
| 4023 | else if (Op1 == Op0I->getOperand(1)) // (B^A)^A == B |
| 4024 | return ReplaceInstUsesWith(I, Op0I->getOperand(0)); |
Chris Lattner | dcd0792 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4025 | } else if (Op0I->getOpcode() == Instruction::And && Op0I->hasOneUse()) { |
| 4026 | if (Op0I->getOperand(0) == Op1) // (A&B)^A -> (B&A)^A |
| 4027 | Op0I->swapOperands(); |
Chris Lattner | 6cf4914 | 2006-04-01 22:05:01 +0000 | [diff] [blame] | 4028 | if (Op0I->getOperand(1) == Op1 && // (B&A)^A == ~B & A |
| 4029 | !isa<ConstantInt>(Op1)) { // Canonical form is (B&C)^C |
Chris Lattner | dcd0792 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4030 | Instruction *N = BinaryOperator::createNot(Op0I->getOperand(0), "tmp"); |
| 4031 | InsertNewInstBefore(N, I); |
| 4032 | return BinaryOperator::createAnd(N, Op1); |
| 4033 | } |
Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4034 | } |
| 4035 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4036 | // (icmp1 A, B) ^ (icmp2 A, B) --> (icmp3 A, B) |
| 4037 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1))) |
| 4038 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS))) |
Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 4039 | return R; |
| 4040 | |
Chris Lattner | 3af1053 | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4041 | // fold (xor (cast A), (cast B)) -> (cast (xor A, B)) |
Reid Spencer | 799b5bf | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4042 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) |
Chris Lattner | 3af1053 | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4043 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) |
Reid Spencer | 799b5bf | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4044 | if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind? |
| 4045 | const Type *SrcTy = Op0C->getOperand(0)->getType(); |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 4046 | if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() && |
Reid Spencer | 799b5bf | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4047 | // Only do this if the casts both really cause code to be generated. |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4048 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), |
| 4049 | I.getType(), TD) && |
| 4050 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), |
| 4051 | I.getType(), TD)) { |
Reid Spencer | 799b5bf | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4052 | Instruction *NewOp = BinaryOperator::createXor(Op0C->getOperand(0), |
| 4053 | Op1C->getOperand(0), |
| 4054 | I.getName()); |
| 4055 | InsertNewInstBefore(NewOp, I); |
| 4056 | return CastInst::create(Op0C->getOpcode(), NewOp, I.getType()); |
| 4057 | } |
Chris Lattner | 3af1053 | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4058 | } |
Chris Lattner | f05d69a | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4059 | |
| 4060 | // (X >> Z) ^ (Y >> Z) -> (X^Y) >> Z for all shifts. |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4061 | if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) { |
| 4062 | if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0)) |
| 4063 | if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() && |
Chris Lattner | f05d69a | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4064 | SI0->getOperand(1) == SI1->getOperand(1) && |
| 4065 | (SI0->hasOneUse() || SI1->hasOneUse())) { |
| 4066 | Instruction *NewOp = |
| 4067 | InsertNewInstBefore(BinaryOperator::createXor(SI0->getOperand(0), |
| 4068 | SI1->getOperand(0), |
| 4069 | SI0->getName()), I); |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4070 | return BinaryOperator::create(SI1->getOpcode(), NewOp, |
| 4071 | SI1->getOperand(1)); |
Chris Lattner | f05d69a | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4072 | } |
| 4073 | } |
Chris Lattner | 3af1053 | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4074 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4075 | return Changed ? &I : 0; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4076 | } |
| 4077 | |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4078 | static bool isPositive(ConstantInt *C) { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4079 | return C->getSExtValue() >= 0; |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4080 | } |
| 4081 | |
| 4082 | /// AddWithOverflow - Compute Result = In1+In2, returning true if the result |
| 4083 | /// overflowed for this type. |
| 4084 | static bool AddWithOverflow(ConstantInt *&Result, ConstantInt *In1, |
| 4085 | ConstantInt *In2) { |
| 4086 | Result = cast<ConstantInt>(ConstantExpr::getAdd(In1, In2)); |
| 4087 | |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 4088 | return cast<ConstantInt>(Result)->getZExtValue() < |
| 4089 | cast<ConstantInt>(In1)->getZExtValue(); |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4090 | } |
| 4091 | |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4092 | /// EmitGEPOffset - Given a getelementptr instruction/constantexpr, emit the |
| 4093 | /// code necessary to compute the offset from the base pointer (without adding |
| 4094 | /// in the base pointer). Return the result as a signed integer of intptr size. |
| 4095 | static Value *EmitGEPOffset(User *GEP, Instruction &I, InstCombiner &IC) { |
| 4096 | TargetData &TD = IC.getTargetData(); |
| 4097 | gep_type_iterator GTI = gep_type_begin(GEP); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4098 | const Type *IntPtrTy = TD.getIntPtrType(); |
| 4099 | Value *Result = Constant::getNullValue(IntPtrTy); |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4100 | |
| 4101 | // Build a mask for high order bits. |
Chris Lattner | 77defba | 2006-02-07 07:00:41 +0000 | [diff] [blame] | 4102 | uint64_t PtrSizeMask = ~0ULL >> (64-TD.getPointerSize()*8); |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4103 | |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4104 | for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i, ++GTI) { |
| 4105 | Value *Op = GEP->getOperand(i); |
Chris Lattner | d35d210 | 2005-01-13 23:26:48 +0000 | [diff] [blame] | 4106 | uint64_t Size = TD.getTypeSize(GTI.getIndexedType()) & PtrSizeMask; |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4107 | Constant *Scale = ConstantInt::get(IntPtrTy, Size); |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4108 | if (Constant *OpC = dyn_cast<Constant>(Op)) { |
| 4109 | if (!OpC->isNullValue()) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4110 | OpC = ConstantExpr::getIntegerCast(OpC, IntPtrTy, true /*SExt*/); |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4111 | Scale = ConstantExpr::getMul(OpC, Scale); |
| 4112 | if (Constant *RC = dyn_cast<Constant>(Result)) |
| 4113 | Result = ConstantExpr::getAdd(RC, Scale); |
| 4114 | else { |
| 4115 | // Emit an add instruction. |
| 4116 | Result = IC.InsertNewInstBefore( |
| 4117 | BinaryOperator::createAdd(Result, Scale, |
| 4118 | GEP->getName()+".offs"), I); |
| 4119 | } |
| 4120 | } |
| 4121 | } else { |
Chris Lattner | 7aa41cf | 2005-01-14 17:17:59 +0000 | [diff] [blame] | 4122 | // Convert to correct type. |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4123 | Op = IC.InsertNewInstBefore(CastInst::createSExtOrBitCast(Op, IntPtrTy, |
Chris Lattner | 7aa41cf | 2005-01-14 17:17:59 +0000 | [diff] [blame] | 4124 | Op->getName()+".c"), I); |
| 4125 | if (Size != 1) |
Chris Lattner | 4cb9fa3 | 2005-01-13 20:40:58 +0000 | [diff] [blame] | 4126 | // We'll let instcombine(mul) convert this to a shl if possible. |
| 4127 | Op = IC.InsertNewInstBefore(BinaryOperator::createMul(Op, Scale, |
| 4128 | GEP->getName()+".idx"), I); |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4129 | |
| 4130 | // Emit an add instruction. |
Chris Lattner | 4cb9fa3 | 2005-01-13 20:40:58 +0000 | [diff] [blame] | 4131 | Result = IC.InsertNewInstBefore(BinaryOperator::createAdd(Op, Result, |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4132 | GEP->getName()+".offs"), I); |
| 4133 | } |
| 4134 | } |
| 4135 | return Result; |
| 4136 | } |
| 4137 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4138 | /// FoldGEPICmp - Fold comparisons between a GEP instruction and something |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4139 | /// else. At this point we know that the GEP is on the LHS of the comparison. |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4140 | Instruction *InstCombiner::FoldGEPICmp(User *GEPLHS, Value *RHS, |
| 4141 | ICmpInst::Predicate Cond, |
| 4142 | Instruction &I) { |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4143 | assert(dyn_castGetElementPtr(GEPLHS) && "LHS is not a getelementptr!"); |
Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4144 | |
| 4145 | if (CastInst *CI = dyn_cast<CastInst>(RHS)) |
| 4146 | if (isa<PointerType>(CI->getOperand(0)->getType())) |
| 4147 | RHS = CI->getOperand(0); |
| 4148 | |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4149 | Value *PtrBase = GEPLHS->getOperand(0); |
| 4150 | if (PtrBase == RHS) { |
| 4151 | // As an optimization, we don't actually have to compute the actual value of |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4152 | // OFFSET if this is a icmp_eq or icmp_ne comparison, just return whether |
| 4153 | // each index is zero or not. |
| 4154 | if (Cond == ICmpInst::ICMP_EQ || Cond == ICmpInst::ICMP_NE) { |
Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4155 | Instruction *InVal = 0; |
Chris Lattner | cd517ff | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 4156 | gep_type_iterator GTI = gep_type_begin(GEPLHS); |
| 4157 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i, ++GTI) { |
Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4158 | bool EmitIt = true; |
| 4159 | if (Constant *C = dyn_cast<Constant>(GEPLHS->getOperand(i))) { |
| 4160 | if (isa<UndefValue>(C)) // undef index -> undef. |
| 4161 | return ReplaceInstUsesWith(I, UndefValue::get(I.getType())); |
| 4162 | if (C->isNullValue()) |
| 4163 | EmitIt = false; |
Chris Lattner | cd517ff | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 4164 | else if (TD->getTypeSize(GTI.getIndexedType()) == 0) { |
| 4165 | EmitIt = false; // This is indexing into a zero sized array? |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4166 | } else if (isa<ConstantInt>(C)) |
Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4167 | return ReplaceInstUsesWith(I, // No comparison is needed here. |
Reid Spencer | cddc9df | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4168 | ConstantInt::get(Type::Int1Ty, |
| 4169 | Cond == ICmpInst::ICMP_NE)); |
Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4170 | } |
| 4171 | |
| 4172 | if (EmitIt) { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4173 | Instruction *Comp = |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4174 | new ICmpInst(Cond, GEPLHS->getOperand(i), |
Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4175 | Constant::getNullValue(GEPLHS->getOperand(i)->getType())); |
| 4176 | if (InVal == 0) |
| 4177 | InVal = Comp; |
| 4178 | else { |
| 4179 | InVal = InsertNewInstBefore(InVal, I); |
| 4180 | InsertNewInstBefore(Comp, I); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4181 | if (Cond == ICmpInst::ICMP_NE) // True if any are unequal |
Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4182 | InVal = BinaryOperator::createOr(InVal, Comp); |
| 4183 | else // True if all are equal |
| 4184 | InVal = BinaryOperator::createAnd(InVal, Comp); |
| 4185 | } |
| 4186 | } |
| 4187 | } |
| 4188 | |
| 4189 | if (InVal) |
| 4190 | return InVal; |
| 4191 | else |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4192 | // No comparison is needed here, all indexes = 0 |
Reid Spencer | cddc9df | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4193 | ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
| 4194 | Cond == ICmpInst::ICMP_EQ)); |
Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4195 | } |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4196 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4197 | // Only lower this if the icmp is the only user of the GEP or if we expect |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4198 | // the result to fold to a constant! |
| 4199 | if (isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) { |
| 4200 | // ((gep Ptr, OFFSET) cmp Ptr) ---> (OFFSET cmp 0). |
| 4201 | Value *Offset = EmitGEPOffset(GEPLHS, I, *this); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4202 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), Offset, |
| 4203 | Constant::getNullValue(Offset->getType())); |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4204 | } |
| 4205 | } else if (User *GEPRHS = dyn_castGetElementPtr(RHS)) { |
Chris Lattner | a21bf8d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 4206 | // If the base pointers are different, but the indices are the same, just |
| 4207 | // compare the base pointer. |
| 4208 | if (PtrBase != GEPRHS->getOperand(0)) { |
| 4209 | bool IndicesTheSame = GEPLHS->getNumOperands()==GEPRHS->getNumOperands(); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 4210 | IndicesTheSame &= GEPLHS->getOperand(0)->getType() == |
Chris Lattner | bd43b9d | 2005-04-26 14:40:41 +0000 | [diff] [blame] | 4211 | GEPRHS->getOperand(0)->getType(); |
Chris Lattner | a21bf8d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 4212 | if (IndicesTheSame) |
| 4213 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i) |
| 4214 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { |
| 4215 | IndicesTheSame = false; |
| 4216 | break; |
| 4217 | } |
| 4218 | |
| 4219 | // If all indices are the same, just compare the base pointers. |
| 4220 | if (IndicesTheSame) |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4221 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), |
| 4222 | GEPLHS->getOperand(0), GEPRHS->getOperand(0)); |
Chris Lattner | a21bf8d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 4223 | |
| 4224 | // Otherwise, the base pointers are different and the indices are |
| 4225 | // different, bail out. |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4226 | return 0; |
Chris Lattner | a21bf8d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 4227 | } |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4228 | |
Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4229 | // If one of the GEPs has all zero indices, recurse. |
| 4230 | bool AllZeros = true; |
| 4231 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i) |
| 4232 | if (!isa<Constant>(GEPLHS->getOperand(i)) || |
| 4233 | !cast<Constant>(GEPLHS->getOperand(i))->isNullValue()) { |
| 4234 | AllZeros = false; |
| 4235 | break; |
| 4236 | } |
| 4237 | if (AllZeros) |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4238 | return FoldGEPICmp(GEPRHS, GEPLHS->getOperand(0), |
| 4239 | ICmpInst::getSwappedPredicate(Cond), I); |
Chris Lattner | 4fa8982 | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 4240 | |
| 4241 | // If the other GEP has all zero indices, recurse. |
Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4242 | AllZeros = true; |
| 4243 | for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i) |
| 4244 | if (!isa<Constant>(GEPRHS->getOperand(i)) || |
| 4245 | !cast<Constant>(GEPRHS->getOperand(i))->isNullValue()) { |
| 4246 | AllZeros = false; |
| 4247 | break; |
| 4248 | } |
| 4249 | if (AllZeros) |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4250 | return FoldGEPICmp(GEPLHS, GEPRHS->getOperand(0), Cond, I); |
Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4251 | |
Chris Lattner | 4fa8982 | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 4252 | if (GEPLHS->getNumOperands() == GEPRHS->getNumOperands()) { |
| 4253 | // If the GEPs only differ by one index, compare it. |
| 4254 | unsigned NumDifferences = 0; // Keep track of # differences. |
| 4255 | unsigned DiffOperand = 0; // The operand that differs. |
| 4256 | for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i) |
| 4257 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4258 | if (GEPLHS->getOperand(i)->getType()->getPrimitiveSizeInBits() != |
| 4259 | GEPRHS->getOperand(i)->getType()->getPrimitiveSizeInBits()) { |
Chris Lattner | fc4429e | 2005-01-21 23:06:49 +0000 | [diff] [blame] | 4260 | // Irreconcilable differences. |
Chris Lattner | 4fa8982 | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 4261 | NumDifferences = 2; |
| 4262 | break; |
| 4263 | } else { |
| 4264 | if (NumDifferences++) break; |
| 4265 | DiffOperand = i; |
| 4266 | } |
| 4267 | } |
| 4268 | |
| 4269 | if (NumDifferences == 0) // SAME GEP? |
| 4270 | return ReplaceInstUsesWith(I, // No comparison is needed here. |
Reid Spencer | cddc9df | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4271 | ConstantInt::get(Type::Int1Ty, |
| 4272 | Cond == ICmpInst::ICMP_EQ)); |
Chris Lattner | 4fa8982 | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 4273 | else if (NumDifferences == 1) { |
Chris Lattner | fc4429e | 2005-01-21 23:06:49 +0000 | [diff] [blame] | 4274 | Value *LHSV = GEPLHS->getOperand(DiffOperand); |
| 4275 | Value *RHSV = GEPRHS->getOperand(DiffOperand); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4276 | // Make sure we do a signed comparison here. |
| 4277 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), LHSV, RHSV); |
Chris Lattner | 4fa8982 | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 4278 | } |
| 4279 | } |
| 4280 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4281 | // Only lower this if the icmp is the only user of the GEP or if we expect |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4282 | // the result to fold to a constant! |
| 4283 | if ((isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) && |
| 4284 | (isa<ConstantExpr>(GEPRHS) || GEPRHS->hasOneUse())) { |
| 4285 | // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) ---> (OFFSET1 cmp OFFSET2) |
| 4286 | Value *L = EmitGEPOffset(GEPLHS, I, *this); |
| 4287 | Value *R = EmitGEPOffset(GEPRHS, I, *this); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4288 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), L, R); |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4289 | } |
| 4290 | } |
| 4291 | return 0; |
| 4292 | } |
| 4293 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4294 | Instruction *InstCombiner::visitFCmpInst(FCmpInst &I) { |
| 4295 | bool Changed = SimplifyCompare(I); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4296 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4297 | |
Chris Lattner | 6ee923f | 2007-01-14 19:42:17 +0000 | [diff] [blame] | 4298 | // Fold trivial predicates. |
| 4299 | if (I.getPredicate() == FCmpInst::FCMP_FALSE) |
| 4300 | return ReplaceInstUsesWith(I, Constant::getNullValue(Type::Int1Ty)); |
| 4301 | if (I.getPredicate() == FCmpInst::FCMP_TRUE) |
| 4302 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 1)); |
| 4303 | |
| 4304 | // Simplify 'fcmp pred X, X' |
| 4305 | if (Op0 == Op1) { |
| 4306 | switch (I.getPredicate()) { |
| 4307 | default: assert(0 && "Unknown predicate!"); |
| 4308 | case FCmpInst::FCMP_UEQ: // True if unordered or equal |
| 4309 | case FCmpInst::FCMP_UGE: // True if unordered, greater than, or equal |
| 4310 | case FCmpInst::FCMP_ULE: // True if unordered, less than, or equal |
| 4311 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 1)); |
| 4312 | case FCmpInst::FCMP_OGT: // True if ordered and greater than |
| 4313 | case FCmpInst::FCMP_OLT: // True if ordered and less than |
| 4314 | case FCmpInst::FCMP_ONE: // True if ordered and operands are unequal |
| 4315 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 0)); |
| 4316 | |
| 4317 | case FCmpInst::FCMP_UNO: // True if unordered: isnan(X) | isnan(Y) |
| 4318 | case FCmpInst::FCMP_ULT: // True if unordered or less than |
| 4319 | case FCmpInst::FCMP_UGT: // True if unordered or greater than |
| 4320 | case FCmpInst::FCMP_UNE: // True if unordered or not equal |
| 4321 | // Canonicalize these to be 'fcmp uno %X, 0.0'. |
| 4322 | I.setPredicate(FCmpInst::FCMP_UNO); |
| 4323 | I.setOperand(1, Constant::getNullValue(Op0->getType())); |
| 4324 | return &I; |
| 4325 | |
| 4326 | case FCmpInst::FCMP_ORD: // True if ordered (no nans) |
| 4327 | case FCmpInst::FCMP_OEQ: // True if ordered and equal |
| 4328 | case FCmpInst::FCMP_OGE: // True if ordered and greater than or equal |
| 4329 | case FCmpInst::FCMP_OLE: // True if ordered and less than or equal |
| 4330 | // Canonicalize these to be 'fcmp ord %X, 0.0'. |
| 4331 | I.setPredicate(FCmpInst::FCMP_ORD); |
| 4332 | I.setOperand(1, Constant::getNullValue(Op0->getType())); |
| 4333 | return &I; |
| 4334 | } |
| 4335 | } |
| 4336 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4337 | if (isa<UndefValue>(Op1)) // fcmp pred X, undef -> undef |
Reid Spencer | 542964f | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 4338 | return ReplaceInstUsesWith(I, UndefValue::get(Type::Int1Ty)); |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4339 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4340 | // Handle fcmp with constant RHS |
| 4341 | if (Constant *RHSC = dyn_cast<Constant>(Op1)) { |
| 4342 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
| 4343 | switch (LHSI->getOpcode()) { |
| 4344 | case Instruction::PHI: |
| 4345 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 4346 | return NV; |
| 4347 | break; |
| 4348 | case Instruction::Select: |
| 4349 | // If either operand of the select is a constant, we can fold the |
| 4350 | // comparison into the select arms, which will cause one to be |
| 4351 | // constant folded and the select turned into a bitwise or. |
| 4352 | Value *Op1 = 0, *Op2 = 0; |
| 4353 | if (LHSI->hasOneUse()) { |
| 4354 | if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) { |
| 4355 | // Fold the known value into the constant operand. |
| 4356 | Op1 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC); |
| 4357 | // Insert a new FCmp of the other select operand. |
| 4358 | Op2 = InsertNewInstBefore(new FCmpInst(I.getPredicate(), |
| 4359 | LHSI->getOperand(2), RHSC, |
| 4360 | I.getName()), I); |
| 4361 | } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) { |
| 4362 | // Fold the known value into the constant operand. |
| 4363 | Op2 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC); |
| 4364 | // Insert a new FCmp of the other select operand. |
| 4365 | Op1 = InsertNewInstBefore(new FCmpInst(I.getPredicate(), |
| 4366 | LHSI->getOperand(1), RHSC, |
| 4367 | I.getName()), I); |
| 4368 | } |
| 4369 | } |
| 4370 | |
| 4371 | if (Op1) |
| 4372 | return new SelectInst(LHSI->getOperand(0), Op1, Op2); |
| 4373 | break; |
| 4374 | } |
| 4375 | } |
| 4376 | |
| 4377 | return Changed ? &I : 0; |
| 4378 | } |
| 4379 | |
| 4380 | Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { |
| 4381 | bool Changed = SimplifyCompare(I); |
| 4382 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 4383 | const Type *Ty = Op0->getType(); |
| 4384 | |
| 4385 | // icmp X, X |
| 4386 | if (Op0 == Op1) |
Reid Spencer | cddc9df | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4387 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
| 4388 | isTrueWhenEqual(I))); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4389 | |
| 4390 | if (isa<UndefValue>(Op1)) // X icmp undef -> undef |
Reid Spencer | 542964f | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 4391 | return ReplaceInstUsesWith(I, UndefValue::get(Type::Int1Ty)); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4392 | |
| 4393 | // icmp of GlobalValues can never equal each other as long as they aren't |
| 4394 | // external weak linkage type. |
| 4395 | if (GlobalValue *GV0 = dyn_cast<GlobalValue>(Op0)) |
| 4396 | if (GlobalValue *GV1 = dyn_cast<GlobalValue>(Op1)) |
| 4397 | if (!GV0->hasExternalWeakLinkage() || !GV1->hasExternalWeakLinkage()) |
Reid Spencer | cddc9df | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4398 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
| 4399 | !isTrueWhenEqual(I))); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4400 | |
| 4401 | // icmp <global/alloca*/null>, <global/alloca*/null> - Global/Stack value |
Chris Lattner | 15ff1e1 | 2004-11-14 07:33:16 +0000 | [diff] [blame] | 4402 | // addresses never equal each other! We already know that Op0 != Op1. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4403 | if ((isa<GlobalValue>(Op0) || isa<AllocaInst>(Op0) || |
| 4404 | isa<ConstantPointerNull>(Op0)) && |
| 4405 | (isa<GlobalValue>(Op1) || isa<AllocaInst>(Op1) || |
Chris Lattner | 15ff1e1 | 2004-11-14 07:33:16 +0000 | [diff] [blame] | 4406 | isa<ConstantPointerNull>(Op1))) |
Reid Spencer | cddc9df | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4407 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
| 4408 | !isTrueWhenEqual(I))); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4409 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4410 | // icmp's with boolean values can always be turned into bitwise operations |
Reid Spencer | 542964f | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 4411 | if (Ty == Type::Int1Ty) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4412 | switch (I.getPredicate()) { |
| 4413 | default: assert(0 && "Invalid icmp instruction!"); |
| 4414 | case ICmpInst::ICMP_EQ: { // icmp eq bool %A, %B -> ~(A^B) |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4415 | Instruction *Xor = BinaryOperator::createXor(Op0, Op1, I.getName()+"tmp"); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4416 | InsertNewInstBefore(Xor, I); |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 4417 | return BinaryOperator::createNot(Xor); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4418 | } |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4419 | case ICmpInst::ICMP_NE: // icmp eq bool %A, %B -> A^B |
Chris Lattner | 4456da6 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 4420 | return BinaryOperator::createXor(Op0, Op1); |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4421 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4422 | case ICmpInst::ICMP_UGT: |
| 4423 | case ICmpInst::ICMP_SGT: |
| 4424 | std::swap(Op0, Op1); // Change icmp gt -> icmp lt |
Chris Lattner | 4456da6 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 4425 | // FALL THROUGH |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4426 | case ICmpInst::ICMP_ULT: |
| 4427 | case ICmpInst::ICMP_SLT: { // icmp lt bool A, B -> ~X & Y |
Chris Lattner | 4456da6 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 4428 | Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp"); |
| 4429 | InsertNewInstBefore(Not, I); |
| 4430 | return BinaryOperator::createAnd(Not, Op1); |
| 4431 | } |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4432 | case ICmpInst::ICMP_UGE: |
| 4433 | case ICmpInst::ICMP_SGE: |
| 4434 | std::swap(Op0, Op1); // Change icmp ge -> icmp le |
Chris Lattner | 4456da6 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 4435 | // FALL THROUGH |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4436 | case ICmpInst::ICMP_ULE: |
| 4437 | case ICmpInst::ICMP_SLE: { // icmp le bool %A, %B -> ~A | B |
Chris Lattner | 4456da6 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 4438 | Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp"); |
| 4439 | InsertNewInstBefore(Not, I); |
| 4440 | return BinaryOperator::createOr(Not, Op1); |
| 4441 | } |
| 4442 | } |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4443 | } |
| 4444 | |
Chris Lattner | 2dd0174 | 2004-06-09 04:24:29 +0000 | [diff] [blame] | 4445 | // See if we are doing a comparison between a constant and an instruction that |
| 4446 | // can be folded into the comparison. |
Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4447 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4448 | switch (I.getPredicate()) { |
| 4449 | default: break; |
| 4450 | case ICmpInst::ICMP_ULT: // A <u MIN -> FALSE |
| 4451 | if (CI->isMinValue(false)) |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4452 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4453 | if (CI->isMaxValue(false)) // A <u MAX -> A != MAX |
| 4454 | return new ICmpInst(ICmpInst::ICMP_NE, Op0,Op1); |
| 4455 | if (isMinValuePlusOne(CI,false)) // A <u MIN+1 -> A == MIN |
| 4456 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, SubOne(CI)); |
| 4457 | break; |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4458 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4459 | case ICmpInst::ICMP_SLT: |
| 4460 | if (CI->isMinValue(true)) // A <s MIN -> FALSE |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4461 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4462 | if (CI->isMaxValue(true)) // A <s MAX -> A != MAX |
| 4463 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
| 4464 | if (isMinValuePlusOne(CI,true)) // A <s MIN+1 -> A == MIN |
| 4465 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, SubOne(CI)); |
| 4466 | break; |
| 4467 | |
| 4468 | case ICmpInst::ICMP_UGT: |
| 4469 | if (CI->isMaxValue(false)) // A >u MAX -> FALSE |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4470 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4471 | if (CI->isMinValue(false)) // A >u MIN -> A != MIN |
| 4472 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
| 4473 | if (isMaxValueMinusOne(CI, false)) // A >u MAX-1 -> A == MAX |
| 4474 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, AddOne(CI)); |
| 4475 | break; |
| 4476 | |
| 4477 | case ICmpInst::ICMP_SGT: |
| 4478 | if (CI->isMaxValue(true)) // A >s MAX -> FALSE |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4479 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4480 | if (CI->isMinValue(true)) // A >s MIN -> A != MIN |
| 4481 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
| 4482 | if (isMaxValueMinusOne(CI, true)) // A >s MAX-1 -> A == MAX |
| 4483 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, AddOne(CI)); |
| 4484 | break; |
| 4485 | |
| 4486 | case ICmpInst::ICMP_ULE: |
| 4487 | if (CI->isMaxValue(false)) // A <=u MAX -> TRUE |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4488 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4489 | if (CI->isMinValue(false)) // A <=u MIN -> A == MIN |
| 4490 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); |
| 4491 | if (isMaxValueMinusOne(CI,false)) // A <=u MAX-1 -> A != MAX |
| 4492 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, AddOne(CI)); |
| 4493 | break; |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4494 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4495 | case ICmpInst::ICMP_SLE: |
| 4496 | if (CI->isMaxValue(true)) // A <=s MAX -> TRUE |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4497 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4498 | if (CI->isMinValue(true)) // A <=s MIN -> A == MIN |
| 4499 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); |
| 4500 | if (isMaxValueMinusOne(CI,true)) // A <=s MAX-1 -> A != MAX |
| 4501 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, AddOne(CI)); |
| 4502 | break; |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4503 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4504 | case ICmpInst::ICMP_UGE: |
| 4505 | if (CI->isMinValue(false)) // A >=u MIN -> TRUE |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4506 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4507 | if (CI->isMaxValue(false)) // A >=u MAX -> A == MAX |
| 4508 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); |
| 4509 | if (isMinValuePlusOne(CI,false)) // A >=u MIN-1 -> A != MIN |
| 4510 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, SubOne(CI)); |
| 4511 | break; |
| 4512 | |
| 4513 | case ICmpInst::ICMP_SGE: |
| 4514 | if (CI->isMinValue(true)) // A >=s MIN -> TRUE |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4515 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4516 | if (CI->isMaxValue(true)) // A >=s MAX -> A == MAX |
| 4517 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); |
| 4518 | if (isMinValuePlusOne(CI,true)) // A >=s MIN-1 -> A != MIN |
| 4519 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, SubOne(CI)); |
| 4520 | break; |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4521 | } |
| 4522 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4523 | // If we still have a icmp le or icmp ge instruction, turn it into the |
| 4524 | // appropriate icmp lt or icmp gt instruction. Since the border cases have |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4525 | // already been handled above, this requires little checking. |
| 4526 | // |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4527 | if (I.getPredicate() == ICmpInst::ICMP_ULE) |
| 4528 | return new ICmpInst(ICmpInst::ICMP_ULT, Op0, AddOne(CI)); |
| 4529 | if (I.getPredicate() == ICmpInst::ICMP_SLE) |
| 4530 | return new ICmpInst(ICmpInst::ICMP_SLT, Op0, AddOne(CI)); |
| 4531 | if (I.getPredicate() == ICmpInst::ICMP_UGE) |
| 4532 | return new ICmpInst( ICmpInst::ICMP_UGT, Op0, SubOne(CI)); |
| 4533 | if (I.getPredicate() == ICmpInst::ICMP_SGE) |
| 4534 | return new ICmpInst(ICmpInst::ICMP_SGT, Op0, SubOne(CI)); |
Chris Lattner | ee0f280 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 4535 | |
| 4536 | // See if we can fold the comparison based on bits known to be zero or one |
| 4537 | // in the input. |
| 4538 | uint64_t KnownZero, KnownOne; |
Reid Spencer | a94d394 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 4539 | if (SimplifyDemandedBits(Op0, cast<IntegerType>(Ty)->getBitMask(), |
Chris Lattner | ee0f280 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 4540 | KnownZero, KnownOne, 0)) |
| 4541 | return &I; |
| 4542 | |
| 4543 | // Given the known and unknown bits, compute a range that the LHS could be |
| 4544 | // in. |
| 4545 | if (KnownOne | KnownZero) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4546 | // Compute the Min, Max and RHS values based on the known bits. For the |
| 4547 | // EQ and NE we use unsigned values. |
Reid Spencer | 910f23f | 2006-12-23 19:17:57 +0000 | [diff] [blame] | 4548 | uint64_t UMin = 0, UMax = 0, URHSVal = 0; |
| 4549 | int64_t SMin = 0, SMax = 0, SRHSVal = 0; |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4550 | if (ICmpInst::isSignedPredicate(I.getPredicate())) { |
| 4551 | SRHSVal = CI->getSExtValue(); |
| 4552 | ComputeSignedMinMaxValuesFromKnownBits(Ty, KnownZero, KnownOne, SMin, |
| 4553 | SMax); |
| 4554 | } else { |
| 4555 | URHSVal = CI->getZExtValue(); |
| 4556 | ComputeUnsignedMinMaxValuesFromKnownBits(Ty, KnownZero, KnownOne, UMin, |
| 4557 | UMax); |
| 4558 | } |
| 4559 | switch (I.getPredicate()) { // LE/GE have been folded already. |
| 4560 | default: assert(0 && "Unknown icmp opcode!"); |
| 4561 | case ICmpInst::ICMP_EQ: |
| 4562 | if (UMax < URHSVal || UMin > URHSVal) |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4563 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4564 | break; |
| 4565 | case ICmpInst::ICMP_NE: |
| 4566 | if (UMax < URHSVal || UMin > URHSVal) |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4567 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4568 | break; |
| 4569 | case ICmpInst::ICMP_ULT: |
| 4570 | if (UMax < URHSVal) |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4571 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4572 | if (UMin > URHSVal) |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4573 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4574 | break; |
| 4575 | case ICmpInst::ICMP_UGT: |
| 4576 | if (UMin > URHSVal) |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4577 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4578 | if (UMax < URHSVal) |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4579 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4580 | break; |
| 4581 | case ICmpInst::ICMP_SLT: |
| 4582 | if (SMax < SRHSVal) |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4583 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4584 | if (SMin > SRHSVal) |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4585 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4586 | break; |
| 4587 | case ICmpInst::ICMP_SGT: |
| 4588 | if (SMin > SRHSVal) |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4589 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4590 | if (SMax < SRHSVal) |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4591 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4592 | break; |
Chris Lattner | ee0f280 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 4593 | } |
| 4594 | } |
| 4595 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4596 | // Since the RHS is a ConstantInt (CI), if the left hand side is an |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 4597 | // instruction, see if that instruction also has constants so that the |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4598 | // instruction can be folded into the icmp |
Chris Lattner | e1e10e1 | 2004-05-25 06:32:08 +0000 | [diff] [blame] | 4599 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 4600 | switch (LHSI->getOpcode()) { |
| 4601 | case Instruction::And: |
| 4602 | if (LHSI->hasOneUse() && isa<ConstantInt>(LHSI->getOperand(1)) && |
| 4603 | LHSI->getOperand(0)->hasOneUse()) { |
Chris Lattner | 4922a0e | 2006-09-18 05:27:43 +0000 | [diff] [blame] | 4604 | ConstantInt *AndCST = cast<ConstantInt>(LHSI->getOperand(1)); |
| 4605 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4606 | // If the LHS is an AND of a truncating cast, we can widen the |
Chris Lattner | 4922a0e | 2006-09-18 05:27:43 +0000 | [diff] [blame] | 4607 | // and/compare to be the input width without changing the value |
| 4608 | // produced, eliminating a cast. |
| 4609 | if (CastInst *Cast = dyn_cast<CastInst>(LHSI->getOperand(0))) { |
| 4610 | // We can do this transformation if either the AND constant does not |
| 4611 | // have its sign bit set or if it is an equality comparison. |
| 4612 | // Extending a relational comparison when we're checking the sign |
| 4613 | // bit would not work. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 4614 | if (Cast->hasOneUse() && isa<TruncInst>(Cast) && |
Chris Lattner | 4922a0e | 2006-09-18 05:27:43 +0000 | [diff] [blame] | 4615 | (I.isEquality() || |
| 4616 | (AndCST->getZExtValue() == (uint64_t)AndCST->getSExtValue()) && |
| 4617 | (CI->getZExtValue() == (uint64_t)CI->getSExtValue()))) { |
| 4618 | ConstantInt *NewCST; |
| 4619 | ConstantInt *NewCI; |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 4620 | NewCST = ConstantInt::get(Cast->getOperand(0)->getType(), |
| 4621 | AndCST->getZExtValue()); |
| 4622 | NewCI = ConstantInt::get(Cast->getOperand(0)->getType(), |
| 4623 | CI->getZExtValue()); |
Chris Lattner | 4922a0e | 2006-09-18 05:27:43 +0000 | [diff] [blame] | 4624 | Instruction *NewAnd = |
| 4625 | BinaryOperator::createAnd(Cast->getOperand(0), NewCST, |
| 4626 | LHSI->getName()); |
| 4627 | InsertNewInstBefore(NewAnd, I); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4628 | return new ICmpInst(I.getPredicate(), NewAnd, NewCI); |
Chris Lattner | 4922a0e | 2006-09-18 05:27:43 +0000 | [diff] [blame] | 4629 | } |
| 4630 | } |
| 4631 | |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 4632 | // If this is: (X >> C1) & C2 != C3 (where any shift and any compare |
| 4633 | // could exist), turn it into (X & (C2 << C1)) != (C3 << C1). This |
| 4634 | // happens a LOT in code produced by the C front-end, for bitfield |
| 4635 | // access. |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4636 | BinaryOperator *Shift = dyn_cast<BinaryOperator>(LHSI->getOperand(0)); |
| 4637 | if (Shift && !Shift->isShift()) |
| 4638 | Shift = 0; |
Chris Lattner | ee0f280 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 4639 | |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4640 | ConstantInt *ShAmt; |
| 4641 | ShAmt = Shift ? dyn_cast<ConstantInt>(Shift->getOperand(1)) : 0; |
Chris Lattner | ee0f280 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 4642 | const Type *Ty = Shift ? Shift->getType() : 0; // Type of the shift. |
| 4643 | const Type *AndTy = AndCST->getType(); // Type of the and. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4644 | |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 4645 | // We can fold this as long as we can't shift unknown bits |
| 4646 | // into the mask. This can only happen with signed shift |
| 4647 | // rights, as they sign-extend. |
| 4648 | if (ShAmt) { |
Chris Lattner | b3f24c9 | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 4649 | bool CanFold = Shift->isLogicalShift(); |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 4650 | if (!CanFold) { |
| 4651 | // To test for the bad case of the signed shr, see if any |
| 4652 | // of the bits shifted in could be tested after the mask. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4653 | int ShAmtVal = Ty->getPrimitiveSizeInBits()-ShAmt->getZExtValue(); |
Chris Lattner | c53cb9d | 2005-06-17 01:29:28 +0000 | [diff] [blame] | 4654 | if (ShAmtVal < 0) ShAmtVal = 0; // Out of range shift. |
| 4655 | |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4656 | Constant *OShAmt = ConstantInt::get(AndTy, ShAmtVal); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4657 | Constant *ShVal = |
Chris Lattner | ee0f280 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 4658 | ConstantExpr::getShl(ConstantInt::getAllOnesValue(AndTy), |
| 4659 | OShAmt); |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 4660 | if (ConstantExpr::getAnd(ShVal, AndCST)->isNullValue()) |
| 4661 | CanFold = true; |
| 4662 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4663 | |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 4664 | if (CanFold) { |
Chris Lattner | 6afc02f | 2004-09-28 17:54:07 +0000 | [diff] [blame] | 4665 | Constant *NewCst; |
| 4666 | if (Shift->getOpcode() == Instruction::Shl) |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 4667 | NewCst = ConstantExpr::getLShr(CI, ShAmt); |
Chris Lattner | 6afc02f | 2004-09-28 17:54:07 +0000 | [diff] [blame] | 4668 | else |
| 4669 | NewCst = ConstantExpr::getShl(CI, ShAmt); |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 4670 | |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 4671 | // Check to see if we are shifting out any of the bits being |
| 4672 | // compared. |
| 4673 | if (ConstantExpr::get(Shift->getOpcode(), NewCst, ShAmt) != CI){ |
| 4674 | // If we shifted bits out, the fold is not going to work out. |
| 4675 | // As a special case, check to see if this means that the |
| 4676 | // result is always true or false now. |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4677 | if (I.getPredicate() == ICmpInst::ICMP_EQ) |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4678 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4679 | if (I.getPredicate() == ICmpInst::ICMP_NE) |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4680 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 4681 | } else { |
| 4682 | I.setOperand(1, NewCst); |
Chris Lattner | 6afc02f | 2004-09-28 17:54:07 +0000 | [diff] [blame] | 4683 | Constant *NewAndCST; |
| 4684 | if (Shift->getOpcode() == Instruction::Shl) |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 4685 | NewAndCST = ConstantExpr::getLShr(AndCST, ShAmt); |
Chris Lattner | 6afc02f | 2004-09-28 17:54:07 +0000 | [diff] [blame] | 4686 | else |
| 4687 | NewAndCST = ConstantExpr::getShl(AndCST, ShAmt); |
| 4688 | LHSI->setOperand(1, NewAndCST); |
Reid Spencer | 6ff3e73 | 2007-01-04 05:23:51 +0000 | [diff] [blame] | 4689 | LHSI->setOperand(0, Shift->getOperand(0)); |
Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 4690 | AddToWorkList(Shift); // Shift is dead. |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 4691 | AddUsesToWorkList(I); |
| 4692 | return &I; |
Chris Lattner | 1638de4 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 4693 | } |
| 4694 | } |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 4695 | } |
Chris Lattner | b3f24c9 | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 4696 | |
| 4697 | // Turn ((X >> Y) & C) == 0 into (X & (C << Y)) == 0. The later is |
| 4698 | // preferable because it allows the C<<Y expression to be hoisted out |
| 4699 | // of a loop if Y is invariant and X is not. |
| 4700 | if (Shift && Shift->hasOneUse() && CI->isNullValue() && |
Chris Lattner | de07792 | 2006-09-18 18:27:05 +0000 | [diff] [blame] | 4701 | I.isEquality() && !Shift->isArithmeticShift() && |
| 4702 | isa<Instruction>(Shift->getOperand(0))) { |
Chris Lattner | b3f24c9 | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 4703 | // Compute C << Y. |
| 4704 | Value *NS; |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 4705 | if (Shift->getOpcode() == Instruction::LShr) { |
Reid Spencer | 0d5f923 | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 4706 | NS = BinaryOperator::createShl(AndCST, |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4707 | Shift->getOperand(1), "tmp"); |
Chris Lattner | b3f24c9 | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 4708 | } else { |
Reid Spencer | 2a499b0 | 2006-12-13 17:19:09 +0000 | [diff] [blame] | 4709 | // Insert a logical shift. |
Reid Spencer | 0d5f923 | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 4710 | NS = BinaryOperator::createLShr(AndCST, |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4711 | Shift->getOperand(1), "tmp"); |
Chris Lattner | b3f24c9 | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 4712 | } |
| 4713 | InsertNewInstBefore(cast<Instruction>(NS), I); |
| 4714 | |
Chris Lattner | b3f24c9 | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 4715 | // Compute X & (C << Y). |
Reid Spencer | 6ff3e73 | 2007-01-04 05:23:51 +0000 | [diff] [blame] | 4716 | Instruction *NewAnd = BinaryOperator::createAnd( |
| 4717 | Shift->getOperand(0), NS, LHSI->getName()); |
Chris Lattner | b3f24c9 | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 4718 | InsertNewInstBefore(NewAnd, I); |
| 4719 | |
| 4720 | I.setOperand(0, NewAnd); |
| 4721 | return &I; |
| 4722 | } |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 4723 | } |
| 4724 | break; |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 4725 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4726 | case Instruction::Shl: // (icmp pred (shl X, ShAmt), CI) |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4727 | if (ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1))) { |
Chris Lattner | b3f24c9 | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 4728 | if (I.isEquality()) { |
Chris Lattner | 19b57f5 | 2005-06-15 20:53:31 +0000 | [diff] [blame] | 4729 | unsigned TypeBits = CI->getType()->getPrimitiveSizeInBits(); |
| 4730 | |
| 4731 | // Check that the shift amount is in range. If not, don't perform |
| 4732 | // undefined shifts. When the shift is visited it will be |
| 4733 | // simplified. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4734 | if (ShAmt->getZExtValue() >= TypeBits) |
Chris Lattner | 19b57f5 | 2005-06-15 20:53:31 +0000 | [diff] [blame] | 4735 | break; |
| 4736 | |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 4737 | // If we are comparing against bits always shifted out, the |
| 4738 | // comparison cannot succeed. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4739 | Constant *Comp = |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 4740 | ConstantExpr::getShl(ConstantExpr::getLShr(CI, ShAmt), ShAmt); |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 4741 | if (Comp != CI) {// Comparing against a bit that we know is zero. |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4742 | bool IsICMP_NE = I.getPredicate() == ICmpInst::ICMP_NE; |
Reid Spencer | cddc9df | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4743 | Constant *Cst = ConstantInt::get(Type::Int1Ty, IsICMP_NE); |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 4744 | return ReplaceInstUsesWith(I, Cst); |
| 4745 | } |
| 4746 | |
| 4747 | if (LHSI->hasOneUse()) { |
| 4748 | // Otherwise strength reduce the shift into an and. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4749 | unsigned ShAmtVal = (unsigned)ShAmt->getZExtValue(); |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 4750 | uint64_t Val = (1ULL << (TypeBits-ShAmtVal))-1; |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 4751 | Constant *Mask = ConstantInt::get(CI->getType(), Val); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4752 | |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 4753 | Instruction *AndI = |
| 4754 | BinaryOperator::createAnd(LHSI->getOperand(0), |
| 4755 | Mask, LHSI->getName()+".mask"); |
| 4756 | Value *And = InsertNewInstBefore(AndI, I); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4757 | return new ICmpInst(I.getPredicate(), And, |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 4758 | ConstantExpr::getLShr(CI, ShAmt)); |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 4759 | } |
| 4760 | } |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 4761 | } |
| 4762 | break; |
| 4763 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4764 | case Instruction::LShr: // (icmp pred (shr X, ShAmt), CI) |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 4765 | case Instruction::AShr: |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4766 | if (ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1))) { |
Chris Lattner | b3f24c9 | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 4767 | if (I.isEquality()) { |
Chris Lattner | 19b57f5 | 2005-06-15 20:53:31 +0000 | [diff] [blame] | 4768 | // Check that the shift amount is in range. If not, don't perform |
| 4769 | // undefined shifts. When the shift is visited it will be |
| 4770 | // simplified. |
Chris Lattner | 104002b | 2005-06-16 01:52:07 +0000 | [diff] [blame] | 4771 | unsigned TypeBits = CI->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4772 | if (ShAmt->getZExtValue() >= TypeBits) |
Chris Lattner | 19b57f5 | 2005-06-15 20:53:31 +0000 | [diff] [blame] | 4773 | break; |
| 4774 | |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 4775 | // If we are comparing against bits always shifted out, the |
| 4776 | // comparison cannot succeed. |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 4777 | Constant *Comp; |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 4778 | if (LHSI->getOpcode() == Instruction::LShr) |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 4779 | Comp = ConstantExpr::getLShr(ConstantExpr::getShl(CI, ShAmt), |
| 4780 | ShAmt); |
| 4781 | else |
| 4782 | Comp = ConstantExpr::getAShr(ConstantExpr::getShl(CI, ShAmt), |
| 4783 | ShAmt); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4784 | |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 4785 | if (Comp != CI) {// Comparing against a bit that we know is zero. |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4786 | bool IsICMP_NE = I.getPredicate() == ICmpInst::ICMP_NE; |
Reid Spencer | cddc9df | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4787 | Constant *Cst = ConstantInt::get(Type::Int1Ty, IsICMP_NE); |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 4788 | return ReplaceInstUsesWith(I, Cst); |
| 4789 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4790 | |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 4791 | if (LHSI->hasOneUse() || CI->isNullValue()) { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4792 | unsigned ShAmtVal = (unsigned)ShAmt->getZExtValue(); |
Chris Lattner | 272d5ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 4793 | |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 4794 | // Otherwise strength reduce the shift into an and. |
| 4795 | uint64_t Val = ~0ULL; // All ones. |
| 4796 | Val <<= ShAmtVal; // Shift over to the right spot. |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 4797 | Val &= ~0ULL >> (64-TypeBits); |
| 4798 | Constant *Mask = ConstantInt::get(CI->getType(), Val); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4799 | |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 4800 | Instruction *AndI = |
| 4801 | BinaryOperator::createAnd(LHSI->getOperand(0), |
| 4802 | Mask, LHSI->getName()+".mask"); |
| 4803 | Value *And = InsertNewInstBefore(AndI, I); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4804 | return new ICmpInst(I.getPredicate(), And, |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 4805 | ConstantExpr::getShl(CI, ShAmt)); |
| 4806 | } |
Chris Lattner | 1023b87 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 4807 | } |
| 4808 | } |
| 4809 | break; |
Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 4810 | |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 4811 | case Instruction::SDiv: |
| 4812 | case Instruction::UDiv: |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4813 | // Fold: icmp pred ([us]div X, C1), C2 -> range test |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 4814 | // Fold this div into the comparison, producing a range check. |
| 4815 | // Determine, based on the divide type, what the range is being |
| 4816 | // checked. If there is an overflow on the low or high side, remember |
| 4817 | // it, otherwise compute the range [low, hi) bounding the new value. |
| 4818 | // See: InsertRangeTest above for the kinds of replacements possible. |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4819 | if (ConstantInt *DivRHS = dyn_cast<ConstantInt>(LHSI->getOperand(1))) { |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 4820 | // FIXME: If the operand types don't match the type of the divide |
| 4821 | // then don't attempt this transform. The code below doesn't have the |
| 4822 | // logic to deal with a signed divide and an unsigned compare (and |
| 4823 | // vice versa). This is because (x /s C1) <s C2 produces different |
| 4824 | // results than (x /s C1) <u C2 or (x /u C1) <s C2 or even |
| 4825 | // (x /u C1) <u C2. Simply casting the operands and result won't |
| 4826 | // work. :( The if statement below tests that condition and bails |
| 4827 | // if it finds it. |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4828 | bool DivIsSigned = LHSI->getOpcode() == Instruction::SDiv; |
| 4829 | if (!I.isEquality() && DivIsSigned != I.isSignedPredicate()) |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 4830 | break; |
| 4831 | |
| 4832 | // Initialize the variables that will indicate the nature of the |
| 4833 | // range check. |
| 4834 | bool LoOverflow = false, HiOverflow = false; |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4835 | ConstantInt *LoBound = 0, *HiBound = 0; |
| 4836 | |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 4837 | // Compute Prod = CI * DivRHS. We are essentially solving an equation |
| 4838 | // of form X/C1=C2. We solve for X by multiplying C1 (DivRHS) and |
| 4839 | // C2 (CI). By solving for X we can turn this into a range check |
| 4840 | // instead of computing a divide. |
| 4841 | ConstantInt *Prod = |
| 4842 | cast<ConstantInt>(ConstantExpr::getMul(CI, DivRHS)); |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4843 | |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 4844 | // Determine if the product overflows by seeing if the product is |
| 4845 | // not equal to the divide. Make sure we do the same kind of divide |
| 4846 | // as in the LHS instruction that we're folding. |
| 4847 | bool ProdOV = !DivRHS->isNullValue() && |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4848 | (DivIsSigned ? ConstantExpr::getSDiv(Prod, DivRHS) : |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 4849 | ConstantExpr::getUDiv(Prod, DivRHS)) != CI; |
| 4850 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4851 | // Get the ICmp opcode |
| 4852 | ICmpInst::Predicate predicate = I.getPredicate(); |
Chris Lattner | a92af96 | 2004-10-11 19:40:04 +0000 | [diff] [blame] | 4853 | |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 4854 | if (DivRHS->isNullValue()) { |
| 4855 | // Don't hack on divide by zeros! |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4856 | } else if (!DivIsSigned) { // udiv |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4857 | LoBound = Prod; |
| 4858 | LoOverflow = ProdOV; |
| 4859 | HiOverflow = ProdOV || AddWithOverflow(HiBound, LoBound, DivRHS); |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 4860 | } else if (isPositive(DivRHS)) { // Divisor is > 0. |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4861 | if (CI->isNullValue()) { // (X / pos) op 0 |
| 4862 | // Can't overflow. |
| 4863 | LoBound = cast<ConstantInt>(ConstantExpr::getNeg(SubOne(DivRHS))); |
| 4864 | HiBound = DivRHS; |
| 4865 | } else if (isPositive(CI)) { // (X / pos) op pos |
| 4866 | LoBound = Prod; |
| 4867 | LoOverflow = ProdOV; |
| 4868 | HiOverflow = ProdOV || AddWithOverflow(HiBound, Prod, DivRHS); |
| 4869 | } else { // (X / pos) op neg |
| 4870 | Constant *DivRHSH = ConstantExpr::getNeg(SubOne(DivRHS)); |
| 4871 | LoOverflow = AddWithOverflow(LoBound, Prod, |
| 4872 | cast<ConstantInt>(DivRHSH)); |
| 4873 | HiBound = Prod; |
| 4874 | HiOverflow = ProdOV; |
| 4875 | } |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 4876 | } else { // Divisor is < 0. |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4877 | if (CI->isNullValue()) { // (X / neg) op 0 |
| 4878 | LoBound = AddOne(DivRHS); |
| 4879 | HiBound = cast<ConstantInt>(ConstantExpr::getNeg(DivRHS)); |
Chris Lattner | 73bcba5 | 2005-06-17 02:05:55 +0000 | [diff] [blame] | 4880 | if (HiBound == DivRHS) |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 4881 | LoBound = 0; // - INTMIN = INTMIN |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4882 | } else if (isPositive(CI)) { // (X / neg) op pos |
| 4883 | HiOverflow = LoOverflow = ProdOV; |
| 4884 | if (!LoOverflow) |
| 4885 | LoOverflow = AddWithOverflow(LoBound, Prod, AddOne(DivRHS)); |
| 4886 | HiBound = AddOne(Prod); |
| 4887 | } else { // (X / neg) op neg |
| 4888 | LoBound = Prod; |
| 4889 | LoOverflow = HiOverflow = ProdOV; |
| 4890 | HiBound = cast<ConstantInt>(ConstantExpr::getSub(Prod, DivRHS)); |
| 4891 | } |
Chris Lattner | 0b41e86 | 2004-10-08 19:15:44 +0000 | [diff] [blame] | 4892 | |
Chris Lattner | a92af96 | 2004-10-11 19:40:04 +0000 | [diff] [blame] | 4893 | // Dividing by a negate swaps the condition. |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4894 | predicate = ICmpInst::getSwappedPredicate(predicate); |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4895 | } |
| 4896 | |
| 4897 | if (LoBound) { |
| 4898 | Value *X = LHSI->getOperand(0); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4899 | switch (predicate) { |
| 4900 | default: assert(0 && "Unhandled icmp opcode!"); |
| 4901 | case ICmpInst::ICMP_EQ: |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4902 | if (LoOverflow && HiOverflow) |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4903 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4904 | else if (HiOverflow) |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4905 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : |
| 4906 | ICmpInst::ICMP_UGE, X, LoBound); |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4907 | else if (LoOverflow) |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4908 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : |
| 4909 | ICmpInst::ICMP_ULT, X, HiBound); |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4910 | else |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4911 | return InsertRangeTest(X, LoBound, HiBound, DivIsSigned, |
| 4912 | true, I); |
| 4913 | case ICmpInst::ICMP_NE: |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4914 | if (LoOverflow && HiOverflow) |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4915 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4916 | else if (HiOverflow) |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4917 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : |
| 4918 | ICmpInst::ICMP_ULT, X, LoBound); |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4919 | else if (LoOverflow) |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4920 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : |
| 4921 | ICmpInst::ICMP_UGE, X, HiBound); |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4922 | else |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4923 | return InsertRangeTest(X, LoBound, HiBound, DivIsSigned, |
| 4924 | false, I); |
| 4925 | case ICmpInst::ICMP_ULT: |
| 4926 | case ICmpInst::ICMP_SLT: |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4927 | if (LoOverflow) |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4928 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4929 | return new ICmpInst(predicate, X, LoBound); |
| 4930 | case ICmpInst::ICMP_UGT: |
| 4931 | case ICmpInst::ICMP_SGT: |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4932 | if (HiOverflow) |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4933 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4934 | if (predicate == ICmpInst::ICMP_UGT) |
| 4935 | return new ICmpInst(ICmpInst::ICMP_UGE, X, HiBound); |
| 4936 | else |
| 4937 | return new ICmpInst(ICmpInst::ICMP_SGE, X, HiBound); |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4938 | } |
| 4939 | } |
| 4940 | } |
| 4941 | break; |
Chris Lattner | e1b4d2a | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 4942 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4943 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4944 | // Simplify icmp_eq and icmp_ne instructions with integer constant RHS. |
Chris Lattner | b3f24c9 | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 4945 | if (I.isEquality()) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4946 | bool isICMP_NE = I.getPredicate() == ICmpInst::ICMP_NE; |
Chris Lattner | d492a0b | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 4947 | |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4948 | // If the first operand is (add|sub|and|or|xor|rem) with a constant, and |
| 4949 | // the second operand is a constant, simplify a bit. |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 4950 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0)) { |
| 4951 | switch (BO->getOpcode()) { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4952 | case Instruction::SRem: |
| 4953 | // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one. |
| 4954 | if (CI->isNullValue() && isa<ConstantInt>(BO->getOperand(1)) && |
| 4955 | BO->hasOneUse()) { |
| 4956 | int64_t V = cast<ConstantInt>(BO->getOperand(1))->getSExtValue(); |
| 4957 | if (V > 1 && isPowerOf2_64(V)) { |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 4958 | Value *NewRem = InsertNewInstBefore(BinaryOperator::createURem( |
| 4959 | BO->getOperand(0), BO->getOperand(1), BO->getName()), I); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4960 | return new ICmpInst(I.getPredicate(), NewRem, |
| 4961 | Constant::getNullValue(BO->getType())); |
Chris Lattner | 23b47b6 | 2004-07-06 07:38:18 +0000 | [diff] [blame] | 4962 | } |
Chris Lattner | 22d00a8 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 4963 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4964 | break; |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 4965 | case Instruction::Add: |
Chris Lattner | 6e07936 | 2004-06-27 22:51:36 +0000 | [diff] [blame] | 4966 | // Replace ((add A, B) != C) with (A != C-B) if B & C are constants. |
| 4967 | if (ConstantInt *BOp1C = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
Chris Lattner | b121ae1 | 2004-09-21 21:35:23 +0000 | [diff] [blame] | 4968 | if (BO->hasOneUse()) |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4969 | return new ICmpInst(I.getPredicate(), BO->getOperand(0), |
| 4970 | ConstantExpr::getSub(CI, BOp1C)); |
Chris Lattner | 6e07936 | 2004-06-27 22:51:36 +0000 | [diff] [blame] | 4971 | } else if (CI->isNullValue()) { |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 4972 | // Replace ((add A, B) != 0) with (A != -B) if A or B is |
| 4973 | // efficiently invertible, or if the add has just this one use. |
| 4974 | Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4975 | |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 4976 | if (Value *NegVal = dyn_castNegVal(BOp1)) |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4977 | return new ICmpInst(I.getPredicate(), BOp0, NegVal); |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 4978 | else if (Value *NegVal = dyn_castNegVal(BOp0)) |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4979 | return new ICmpInst(I.getPredicate(), NegVal, BOp1); |
Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 4980 | else if (BO->hasOneUse()) { |
Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 4981 | Instruction *Neg = BinaryOperator::createNeg(BOp1); |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 4982 | InsertNewInstBefore(Neg, I); |
Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 4983 | Neg->takeName(BO); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4984 | return new ICmpInst(I.getPredicate(), BOp0, Neg); |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 4985 | } |
| 4986 | } |
| 4987 | break; |
| 4988 | case Instruction::Xor: |
| 4989 | // For the xor case, we can xor two constants together, eliminating |
| 4990 | // the explicit xor. |
| 4991 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4992 | return new ICmpInst(I.getPredicate(), BO->getOperand(0), |
| 4993 | ConstantExpr::getXor(CI, BOC)); |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 4994 | |
| 4995 | // FALLTHROUGH |
| 4996 | case Instruction::Sub: |
| 4997 | // Replace (([sub|xor] A, B) != 0) with (A != B) |
| 4998 | if (CI->isNullValue()) |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4999 | return new ICmpInst(I.getPredicate(), BO->getOperand(0), |
| 5000 | BO->getOperand(1)); |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 5001 | break; |
| 5002 | |
| 5003 | case Instruction::Or: |
| 5004 | // If bits are being or'd in that are not present in the constant we |
| 5005 | // are comparing against, then the comparison could never succeed! |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 5006 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) { |
Chris Lattner | c8e7e29 | 2004-06-10 02:12:35 +0000 | [diff] [blame] | 5007 | Constant *NotCI = ConstantExpr::getNot(CI); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 5008 | if (!ConstantExpr::getAnd(BOC, NotCI)->isNullValue()) |
Reid Spencer | cddc9df | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 5009 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
| 5010 | isICMP_NE)); |
Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 5011 | } |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 5012 | break; |
| 5013 | |
| 5014 | case Instruction::And: |
| 5015 | if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
Chris Lattner | d492a0b | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 5016 | // If bits are being compared against that are and'd out, then the |
| 5017 | // comparison can never succeed! |
Chris Lattner | c8e7e29 | 2004-06-10 02:12:35 +0000 | [diff] [blame] | 5018 | if (!ConstantExpr::getAnd(CI, |
| 5019 | ConstantExpr::getNot(BOC))->isNullValue()) |
Reid Spencer | cddc9df | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 5020 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
| 5021 | isICMP_NE)); |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 5022 | |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 5023 | // If we have ((X & C) == C), turn it into ((X & C) != 0). |
Chris Lattner | ee59d4b | 2004-06-10 02:33:20 +0000 | [diff] [blame] | 5024 | if (CI == BOC && isOneBitSet(CI)) |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5025 | return new ICmpInst(isICMP_NE ? ICmpInst::ICMP_EQ : |
| 5026 | ICmpInst::ICMP_NE, Op0, |
| 5027 | Constant::getNullValue(CI->getType())); |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 5028 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5029 | // Replace (and X, (1 << size(X)-1) != 0) with x s< 0 |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 5030 | if (isSignBit(BOC)) { |
| 5031 | Value *X = BO->getOperand(0); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5032 | Constant *Zero = Constant::getNullValue(X->getType()); |
| 5033 | ICmpInst::Predicate pred = isICMP_NE ? |
| 5034 | ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGE; |
| 5035 | return new ICmpInst(pred, X, Zero); |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 5036 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5037 | |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 5038 | // ((X & ~7) == 0) --> X < 8 |
Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 5039 | if (CI->isNullValue() && isHighOnes(BOC)) { |
| 5040 | Value *X = BO->getOperand(0); |
Chris Lattner | bfff18a | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 5041 | Constant *NegX = ConstantExpr::getNeg(BOC); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5042 | ICmpInst::Predicate pred = isICMP_NE ? |
| 5043 | ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT; |
| 5044 | return new ICmpInst(pred, X, NegX); |
Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 5045 | } |
| 5046 | |
Chris Lattner | d492a0b | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 5047 | } |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 5048 | default: break; |
| 5049 | } |
Chris Lattner | a7942b7 | 2006-11-29 05:02:16 +0000 | [diff] [blame] | 5050 | } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Op0)) { |
| 5051 | // Handle set{eq|ne} <intrinsic>, intcst. |
| 5052 | switch (II->getIntrinsicID()) { |
| 5053 | default: break; |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5054 | case Intrinsic::bswap_i16: |
| 5055 | // icmp eq (bswap(x)), c -> icmp eq (x,bswap(c)) |
Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 5056 | AddToWorkList(II); // Dead? |
Chris Lattner | a7942b7 | 2006-11-29 05:02:16 +0000 | [diff] [blame] | 5057 | I.setOperand(0, II->getOperand(1)); |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 5058 | I.setOperand(1, ConstantInt::get(Type::Int16Ty, |
Chris Lattner | a7942b7 | 2006-11-29 05:02:16 +0000 | [diff] [blame] | 5059 | ByteSwap_16(CI->getZExtValue()))); |
| 5060 | return &I; |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5061 | case Intrinsic::bswap_i32: |
| 5062 | // icmp eq (bswap(x)), c -> icmp eq (x,bswap(c)) |
Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 5063 | AddToWorkList(II); // Dead? |
Chris Lattner | a7942b7 | 2006-11-29 05:02:16 +0000 | [diff] [blame] | 5064 | I.setOperand(0, II->getOperand(1)); |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 5065 | I.setOperand(1, ConstantInt::get(Type::Int32Ty, |
Chris Lattner | a7942b7 | 2006-11-29 05:02:16 +0000 | [diff] [blame] | 5066 | ByteSwap_32(CI->getZExtValue()))); |
| 5067 | return &I; |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5068 | case Intrinsic::bswap_i64: |
| 5069 | // icmp eq (bswap(x)), c -> icmp eq (x,bswap(c)) |
Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 5070 | AddToWorkList(II); // Dead? |
Chris Lattner | a7942b7 | 2006-11-29 05:02:16 +0000 | [diff] [blame] | 5071 | I.setOperand(0, II->getOperand(1)); |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 5072 | I.setOperand(1, ConstantInt::get(Type::Int64Ty, |
Chris Lattner | a7942b7 | 2006-11-29 05:02:16 +0000 | [diff] [blame] | 5073 | ByteSwap_64(CI->getZExtValue()))); |
| 5074 | return &I; |
| 5075 | } |
Chris Lattner | c992add | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 5076 | } |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5077 | } else { // Not a ICMP_EQ/ICMP_NE |
| 5078 | // If the LHS is a cast from an integral value of the same size, then |
| 5079 | // since we know the RHS is a constant, try to simlify. |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 5080 | if (CastInst *Cast = dyn_cast<CastInst>(Op0)) { |
| 5081 | Value *CastOp = Cast->getOperand(0); |
| 5082 | const Type *SrcTy = CastOp->getType(); |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5083 | unsigned SrcTySize = SrcTy->getPrimitiveSizeInBits(); |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 5084 | if (SrcTy->isInteger() && |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5085 | SrcTySize == Cast->getType()->getPrimitiveSizeInBits()) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5086 | // If this is an unsigned comparison, try to make the comparison use |
| 5087 | // smaller constant values. |
| 5088 | switch (I.getPredicate()) { |
| 5089 | default: break; |
| 5090 | case ICmpInst::ICMP_ULT: { // X u< 128 => X s> -1 |
| 5091 | ConstantInt *CUI = cast<ConstantInt>(CI); |
| 5092 | if (CUI->getZExtValue() == 1ULL << (SrcTySize-1)) |
| 5093 | return new ICmpInst(ICmpInst::ICMP_SGT, CastOp, |
Reid Spencer | 24f1a0e | 2007-03-01 19:33:52 +0000 | [diff] [blame] | 5094 | ConstantInt::get(SrcTy, -1ULL)); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5095 | break; |
| 5096 | } |
| 5097 | case ICmpInst::ICMP_UGT: { // X u> 127 => X s< 0 |
| 5098 | ConstantInt *CUI = cast<ConstantInt>(CI); |
| 5099 | if (CUI->getZExtValue() == (1ULL << (SrcTySize-1))-1) |
| 5100 | return new ICmpInst(ICmpInst::ICMP_SLT, CastOp, |
| 5101 | Constant::getNullValue(SrcTy)); |
| 5102 | break; |
| 5103 | } |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 5104 | } |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5105 | |
Chris Lattner | 2b55ea3 | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 5106 | } |
| 5107 | } |
Chris Lattner | e967b34 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 5108 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5109 | } |
| 5110 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5111 | // Handle icmp with constant RHS |
Chris Lattner | 77c32c3 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5112 | if (Constant *RHSC = dyn_cast<Constant>(Op1)) { |
| 5113 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
| 5114 | switch (LHSI->getOpcode()) { |
Chris Lattner | a816eee | 2005-05-01 04:42:15 +0000 | [diff] [blame] | 5115 | case Instruction::GetElementPtr: |
| 5116 | if (RHSC->isNullValue()) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5117 | // icmp pred GEP (P, int 0, int 0, int 0), null -> icmp pred P, null |
Chris Lattner | a816eee | 2005-05-01 04:42:15 +0000 | [diff] [blame] | 5118 | bool isAllZeros = true; |
| 5119 | for (unsigned i = 1, e = LHSI->getNumOperands(); i != e; ++i) |
| 5120 | if (!isa<Constant>(LHSI->getOperand(i)) || |
| 5121 | !cast<Constant>(LHSI->getOperand(i))->isNullValue()) { |
| 5122 | isAllZeros = false; |
| 5123 | break; |
| 5124 | } |
| 5125 | if (isAllZeros) |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5126 | return new ICmpInst(I.getPredicate(), LHSI->getOperand(0), |
Chris Lattner | a816eee | 2005-05-01 04:42:15 +0000 | [diff] [blame] | 5127 | Constant::getNullValue(LHSI->getOperand(0)->getType())); |
| 5128 | } |
| 5129 | break; |
| 5130 | |
Chris Lattner | 77c32c3 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5131 | case Instruction::PHI: |
| 5132 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 5133 | return NV; |
| 5134 | break; |
| 5135 | case Instruction::Select: |
| 5136 | // If either operand of the select is a constant, we can fold the |
| 5137 | // comparison into the select arms, which will cause one to be |
| 5138 | // constant folded and the select turned into a bitwise or. |
| 5139 | Value *Op1 = 0, *Op2 = 0; |
| 5140 | if (LHSI->hasOneUse()) { |
| 5141 | if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) { |
| 5142 | // Fold the known value into the constant operand. |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5143 | Op1 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC); |
| 5144 | // Insert a new ICmp of the other select operand. |
| 5145 | Op2 = InsertNewInstBefore(new ICmpInst(I.getPredicate(), |
| 5146 | LHSI->getOperand(2), RHSC, |
| 5147 | I.getName()), I); |
Chris Lattner | 77c32c3 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5148 | } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) { |
| 5149 | // Fold the known value into the constant operand. |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5150 | Op2 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC); |
| 5151 | // Insert a new ICmp of the other select operand. |
| 5152 | Op1 = InsertNewInstBefore(new ICmpInst(I.getPredicate(), |
| 5153 | LHSI->getOperand(1), RHSC, |
| 5154 | I.getName()), I); |
Chris Lattner | 77c32c3 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5155 | } |
| 5156 | } |
Jeff Cohen | 8263985 | 2005-04-23 21:38:35 +0000 | [diff] [blame] | 5157 | |
Chris Lattner | 77c32c3 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5158 | if (Op1) |
| 5159 | return new SelectInst(LHSI->getOperand(0), Op1, Op2); |
| 5160 | break; |
| 5161 | } |
| 5162 | } |
| 5163 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5164 | // If we can optimize a 'icmp GEP, P' or 'icmp P, GEP', do so now. |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5165 | if (User *GEP = dyn_castGetElementPtr(Op0)) |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5166 | if (Instruction *NI = FoldGEPICmp(GEP, Op1, I.getPredicate(), I)) |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5167 | return NI; |
| 5168 | if (User *GEP = dyn_castGetElementPtr(Op1)) |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5169 | if (Instruction *NI = FoldGEPICmp(GEP, Op0, |
| 5170 | ICmpInst::getSwappedPredicate(I.getPredicate()), I)) |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5171 | return NI; |
| 5172 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5173 | // Test to see if the operands of the icmp are casted versions of other |
Chris Lattner | 64d87b0 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 5174 | // values. If the ptr->ptr cast can be stripped off both arguments, we do so |
| 5175 | // now. |
| 5176 | if (BitCastInst *CI = dyn_cast<BitCastInst>(Op0)) { |
| 5177 | if (isa<PointerType>(Op0->getType()) && |
| 5178 | (isa<Constant>(Op1) || isa<BitCastInst>(Op1))) { |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5179 | // We keep moving the cast from the left operand over to the right |
| 5180 | // operand, where it can often be eliminated completely. |
Chris Lattner | 64d87b0 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 5181 | Op0 = CI->getOperand(0); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5182 | |
Chris Lattner | 64d87b0 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 5183 | // If operand #1 is a bitcast instruction, it must also be a ptr->ptr cast |
| 5184 | // so eliminate it as well. |
| 5185 | if (BitCastInst *CI2 = dyn_cast<BitCastInst>(Op1)) |
| 5186 | Op1 = CI2->getOperand(0); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5187 | |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5188 | // If Op1 is a constant, we can fold the cast into the constant. |
Chris Lattner | 64d87b0 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 5189 | if (Op0->getType() != Op1->getType()) |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5190 | if (Constant *Op1C = dyn_cast<Constant>(Op1)) { |
Reid Spencer | bb65ebf | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 5191 | Op1 = ConstantExpr::getBitCast(Op1C, Op0->getType()); |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5192 | } else { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5193 | // Otherwise, cast the RHS right before the icmp |
Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 5194 | Op1 = InsertCastBefore(Instruction::BitCast, Op1, Op0->getType(), I); |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5195 | } |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5196 | return new ICmpInst(I.getPredicate(), Op0, Op1); |
Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5197 | } |
Chris Lattner | 64d87b0 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 5198 | } |
| 5199 | |
| 5200 | if (isa<CastInst>(Op0)) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5201 | // Handle the special case of: icmp (cast bool to X), <cst> |
Chris Lattner | 6444c37 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 5202 | // This comes up when you have code like |
| 5203 | // int X = A < B; |
| 5204 | // if (X) ... |
| 5205 | // For generality, we handle any zero-extension of any operand comparison |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5206 | // with a constant or another cast from the same type. |
| 5207 | if (isa<ConstantInt>(Op1) || isa<CastInst>(Op1)) |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5208 | if (Instruction *R = visitICmpInstWithCastAndCast(I)) |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5209 | return R; |
Chris Lattner | 6444c37 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 5210 | } |
Chris Lattner | f5c8a0b | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5211 | |
Chris Lattner | b3f24c9 | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 5212 | if (I.isEquality()) { |
Chris Lattner | 17c7c03 | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5213 | Value *A, *B, *C, *D; |
| 5214 | if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) { |
| 5215 | if (A == Op1 || B == Op1) { // (A^B) == A -> B == 0 |
| 5216 | Value *OtherVal = A == Op1 ? B : A; |
| 5217 | return new ICmpInst(I.getPredicate(), OtherVal, |
| 5218 | Constant::getNullValue(A->getType())); |
| 5219 | } |
| 5220 | |
| 5221 | if (match(Op1, m_Xor(m_Value(C), m_Value(D)))) { |
| 5222 | // A^c1 == C^c2 --> A == C^(c1^c2) |
| 5223 | if (ConstantInt *C1 = dyn_cast<ConstantInt>(B)) |
| 5224 | if (ConstantInt *C2 = dyn_cast<ConstantInt>(D)) |
| 5225 | if (Op1->hasOneUse()) { |
| 5226 | Constant *NC = ConstantExpr::getXor(C1, C2); |
| 5227 | Instruction *Xor = BinaryOperator::createXor(C, NC, "tmp"); |
| 5228 | return new ICmpInst(I.getPredicate(), A, |
| 5229 | InsertNewInstBefore(Xor, I)); |
| 5230 | } |
| 5231 | |
| 5232 | // A^B == A^D -> B == D |
| 5233 | if (A == C) return new ICmpInst(I.getPredicate(), B, D); |
| 5234 | if (A == D) return new ICmpInst(I.getPredicate(), B, C); |
| 5235 | if (B == C) return new ICmpInst(I.getPredicate(), A, D); |
| 5236 | if (B == D) return new ICmpInst(I.getPredicate(), A, C); |
| 5237 | } |
| 5238 | } |
| 5239 | |
| 5240 | if (match(Op1, m_Xor(m_Value(A), m_Value(B))) && |
| 5241 | (A == Op0 || B == Op0)) { |
Chris Lattner | f5c8a0b | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5242 | // A == (A^B) -> B == 0 |
| 5243 | Value *OtherVal = A == Op0 ? B : A; |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5244 | return new ICmpInst(I.getPredicate(), OtherVal, |
| 5245 | Constant::getNullValue(A->getType())); |
Chris Lattner | 17c7c03 | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5246 | } |
| 5247 | if (match(Op0, m_Sub(m_Value(A), m_Value(B))) && A == Op1) { |
Chris Lattner | f5c8a0b | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5248 | // (A-B) == A -> B == 0 |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5249 | return new ICmpInst(I.getPredicate(), B, |
| 5250 | Constant::getNullValue(B->getType())); |
Chris Lattner | 17c7c03 | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5251 | } |
| 5252 | if (match(Op1, m_Sub(m_Value(A), m_Value(B))) && A == Op0) { |
Chris Lattner | f5c8a0b | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5253 | // A == (A-B) -> B == 0 |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5254 | return new ICmpInst(I.getPredicate(), B, |
| 5255 | Constant::getNullValue(B->getType())); |
Chris Lattner | f5c8a0b | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5256 | } |
Chris Lattner | d12a4bf | 2006-11-14 06:06:06 +0000 | [diff] [blame] | 5257 | |
Chris Lattner | d12a4bf | 2006-11-14 06:06:06 +0000 | [diff] [blame] | 5258 | // (X&Z) == (Y&Z) -> (X^Y) & Z == 0 |
| 5259 | if (Op0->hasOneUse() && Op1->hasOneUse() && |
| 5260 | match(Op0, m_And(m_Value(A), m_Value(B))) && |
| 5261 | match(Op1, m_And(m_Value(C), m_Value(D)))) { |
| 5262 | Value *X = 0, *Y = 0, *Z = 0; |
| 5263 | |
| 5264 | if (A == C) { |
| 5265 | X = B; Y = D; Z = A; |
| 5266 | } else if (A == D) { |
| 5267 | X = B; Y = C; Z = A; |
| 5268 | } else if (B == C) { |
| 5269 | X = A; Y = D; Z = B; |
| 5270 | } else if (B == D) { |
| 5271 | X = A; Y = C; Z = B; |
| 5272 | } |
| 5273 | |
| 5274 | if (X) { // Build (X^Y) & Z |
| 5275 | Op1 = InsertNewInstBefore(BinaryOperator::createXor(X, Y, "tmp"), I); |
| 5276 | Op1 = InsertNewInstBefore(BinaryOperator::createAnd(Op1, Z, "tmp"), I); |
| 5277 | I.setOperand(0, Op1); |
| 5278 | I.setOperand(1, Constant::getNullValue(Op1->getType())); |
| 5279 | return &I; |
| 5280 | } |
| 5281 | } |
Chris Lattner | f5c8a0b | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5282 | } |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 5283 | return Changed ? &I : 0; |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5284 | } |
| 5285 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5286 | // visitICmpInstWithCastAndCast - Handle icmp (cast x to y), (cast/cst). |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5287 | // We only handle extending casts so far. |
| 5288 | // |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5289 | Instruction *InstCombiner::visitICmpInstWithCastAndCast(ICmpInst &ICI) { |
| 5290 | const CastInst *LHSCI = cast<CastInst>(ICI.getOperand(0)); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 5291 | Value *LHSCIOp = LHSCI->getOperand(0); |
| 5292 | const Type *SrcTy = LHSCIOp->getType(); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5293 | const Type *DestTy = LHSCI->getType(); |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5294 | Value *RHSCIOp; |
| 5295 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5296 | // We only handle extension cast instructions, so far. Enforce this. |
| 5297 | if (LHSCI->getOpcode() != Instruction::ZExt && |
| 5298 | LHSCI->getOpcode() != Instruction::SExt) |
Chris Lattner | 03f06f1 | 2005-01-17 03:20:02 +0000 | [diff] [blame] | 5299 | return 0; |
| 5300 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5301 | bool isSignedExt = LHSCI->getOpcode() == Instruction::SExt; |
| 5302 | bool isSignedCmp = ICI.isSignedPredicate(); |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5303 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5304 | if (CastInst *CI = dyn_cast<CastInst>(ICI.getOperand(1))) { |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5305 | // Not an extension from the same type? |
| 5306 | RHSCIOp = CI->getOperand(0); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5307 | if (RHSCIOp->getType() != LHSCIOp->getType()) |
| 5308 | return 0; |
Chris Lattner | 387bf3f | 2007-01-13 23:11:38 +0000 | [diff] [blame] | 5309 | |
| 5310 | // If the signedness of the two compares doesn't agree (i.e. one is a sext |
| 5311 | // and the other is a zext), then we can't handle this. |
| 5312 | if (CI->getOpcode() != LHSCI->getOpcode()) |
| 5313 | return 0; |
| 5314 | |
| 5315 | // Likewise, if the signedness of the [sz]exts and the compare don't match, |
| 5316 | // then we can't handle this. |
| 5317 | if (isSignedExt != isSignedCmp && !ICI.isEquality()) |
| 5318 | return 0; |
| 5319 | |
| 5320 | // Okay, just insert a compare of the reduced operands now! |
| 5321 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSCIOp); |
Reid Spencer | 279fa25 | 2004-11-28 21:31:15 +0000 | [diff] [blame] | 5322 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5323 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5324 | // If we aren't dealing with a constant on the RHS, exit early |
| 5325 | ConstantInt *CI = dyn_cast<ConstantInt>(ICI.getOperand(1)); |
| 5326 | if (!CI) |
| 5327 | return 0; |
| 5328 | |
| 5329 | // Compute the constant that would happen if we truncated to SrcTy then |
| 5330 | // reextended to DestTy. |
| 5331 | Constant *Res1 = ConstantExpr::getTrunc(CI, SrcTy); |
| 5332 | Constant *Res2 = ConstantExpr::getCast(LHSCI->getOpcode(), Res1, DestTy); |
| 5333 | |
| 5334 | // If the re-extended constant didn't change... |
| 5335 | if (Res2 == CI) { |
| 5336 | // Make sure that sign of the Cmp and the sign of the Cast are the same. |
| 5337 | // For example, we might have: |
| 5338 | // %A = sext short %X to uint |
| 5339 | // %B = icmp ugt uint %A, 1330 |
| 5340 | // It is incorrect to transform this into |
| 5341 | // %B = icmp ugt short %X, 1330 |
| 5342 | // because %A may have negative value. |
| 5343 | // |
| 5344 | // However, it is OK if SrcTy is bool (See cast-set.ll testcase) |
| 5345 | // OR operation is EQ/NE. |
Reid Spencer | 542964f | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 5346 | if (isSignedExt == isSignedCmp || SrcTy == Type::Int1Ty || ICI.isEquality()) |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5347 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, Res1); |
| 5348 | else |
| 5349 | return 0; |
| 5350 | } |
| 5351 | |
| 5352 | // The re-extended constant changed so the constant cannot be represented |
| 5353 | // in the shorter type. Consequently, we cannot emit a simple comparison. |
| 5354 | |
| 5355 | // First, handle some easy cases. We know the result cannot be equal at this |
| 5356 | // point so handle the ICI.isEquality() cases |
| 5357 | if (ICI.getPredicate() == ICmpInst::ICMP_EQ) |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5358 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5359 | if (ICI.getPredicate() == ICmpInst::ICMP_NE) |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5360 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5361 | |
| 5362 | // Evaluate the comparison for LT (we invert for GT below). LE and GE cases |
| 5363 | // should have been folded away previously and not enter in here. |
| 5364 | Value *Result; |
| 5365 | if (isSignedCmp) { |
| 5366 | // We're performing a signed comparison. |
| 5367 | if (cast<ConstantInt>(CI)->getSExtValue() < 0) |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5368 | Result = ConstantInt::getFalse(); // X < (small) --> false |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5369 | else |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5370 | Result = ConstantInt::getTrue(); // X < (large) --> true |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5371 | } else { |
| 5372 | // We're performing an unsigned comparison. |
| 5373 | if (isSignedExt) { |
| 5374 | // We're performing an unsigned comp with a sign extended value. |
| 5375 | // This is true if the input is >= 0. [aka >s -1] |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5376 | Constant *NegOne = ConstantInt::getAllOnesValue(SrcTy); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5377 | Result = InsertNewInstBefore(new ICmpInst(ICmpInst::ICMP_SGT, LHSCIOp, |
| 5378 | NegOne, ICI.getName()), ICI); |
| 5379 | } else { |
| 5380 | // Unsigned extend & unsigned compare -> always true. |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5381 | Result = ConstantInt::getTrue(); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5382 | } |
| 5383 | } |
| 5384 | |
| 5385 | // Finally, return the value computed. |
| 5386 | if (ICI.getPredicate() == ICmpInst::ICMP_ULT || |
| 5387 | ICI.getPredicate() == ICmpInst::ICMP_SLT) { |
| 5388 | return ReplaceInstUsesWith(ICI, Result); |
| 5389 | } else { |
| 5390 | assert((ICI.getPredicate()==ICmpInst::ICMP_UGT || |
| 5391 | ICI.getPredicate()==ICmpInst::ICMP_SGT) && |
| 5392 | "ICmp should be folded!"); |
| 5393 | if (Constant *CI = dyn_cast<Constant>(Result)) |
| 5394 | return ReplaceInstUsesWith(ICI, ConstantExpr::getNot(CI)); |
| 5395 | else |
| 5396 | return BinaryOperator::createNot(Result); |
| 5397 | } |
Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5398 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5399 | |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 5400 | Instruction *InstCombiner::visitShl(BinaryOperator &I) { |
| 5401 | return commonShiftTransforms(I); |
| 5402 | } |
| 5403 | |
| 5404 | Instruction *InstCombiner::visitLShr(BinaryOperator &I) { |
| 5405 | return commonShiftTransforms(I); |
| 5406 | } |
| 5407 | |
| 5408 | Instruction *InstCombiner::visitAShr(BinaryOperator &I) { |
| 5409 | return commonShiftTransforms(I); |
| 5410 | } |
| 5411 | |
| 5412 | Instruction *InstCombiner::commonShiftTransforms(BinaryOperator &I) { |
| 5413 | assert(I.getOperand(1)->getType() == I.getOperand(0)->getType()); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 5414 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5415 | |
| 5416 | // shl X, 0 == X and shr X, 0 == X |
| 5417 | // shl 0, X == 0 and shr 0, X == 0 |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 5418 | if (Op1 == Constant::getNullValue(Op1->getType()) || |
Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 5419 | Op0 == Constant::getNullValue(Op0->getType())) |
| 5420 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | f5b4ef7 | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 5421 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5422 | if (isa<UndefValue>(Op0)) { |
| 5423 | if (I.getOpcode() == Instruction::AShr) // undef >>s X -> undef |
Chris Lattner | 67f0545 | 2004-10-16 23:28:04 +0000 | [diff] [blame] | 5424 | return ReplaceInstUsesWith(I, Op0); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5425 | else // undef << X -> 0, undef >>u X -> 0 |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5426 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 5427 | } |
| 5428 | if (isa<UndefValue>(Op1)) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5429 | if (I.getOpcode() == Instruction::AShr) // X >>s undef -> X |
| 5430 | return ReplaceInstUsesWith(I, Op0); |
| 5431 | else // X << undef, X >>u undef -> 0 |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5432 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5433 | } |
| 5434 | |
Chris Lattner | d4dee40 | 2006-11-10 23:38:52 +0000 | [diff] [blame] | 5435 | // ashr int -1, X = -1 (for any arithmetic shift rights of ~0) |
| 5436 | if (I.getOpcode() == Instruction::AShr) |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5437 | if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0)) |
Chris Lattner | d4dee40 | 2006-11-10 23:38:52 +0000 | [diff] [blame] | 5438 | if (CSI->isAllOnesValue()) |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 5439 | return ReplaceInstUsesWith(I, CSI); |
| 5440 | |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 5441 | // Try to fold constant and into select arguments. |
| 5442 | if (isa<Constant>(Op0)) |
| 5443 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 5444 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 5445 | return R; |
| 5446 | |
Chris Lattner | b18dbbf | 2005-05-08 17:34:56 +0000 | [diff] [blame] | 5447 | // See if we can turn a signed shr into an unsigned shr. |
Chris Lattner | b3f24c9 | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 5448 | if (I.isArithmeticShift()) { |
Chris Lattner | c3ebf40 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 5449 | if (MaskedValueIsZero(Op0, |
| 5450 | 1ULL << (I.getType()->getPrimitiveSizeInBits()-1))) { |
Reid Spencer | 0d5f923 | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 5451 | return BinaryOperator::createLShr(Op0, Op1, I.getName()); |
Chris Lattner | b18dbbf | 2005-05-08 17:34:56 +0000 | [diff] [blame] | 5452 | } |
| 5453 | } |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 5454 | |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5455 | if (ConstantInt *CUI = dyn_cast<ConstantInt>(Op1)) |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 5456 | if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I)) |
| 5457 | return Res; |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5458 | return 0; |
| 5459 | } |
| 5460 | |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5461 | Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1, |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 5462 | BinaryOperator &I) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5463 | bool isLeftShift = I.getOpcode() == Instruction::Shl; |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5464 | |
Chris Lattner | f5b4ef7 | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 5465 | // See if we can simplify any instructions used by the instruction whose sole |
| 5466 | // purpose is to compute bits we don't care about. |
| 5467 | uint64_t KnownZero, KnownOne; |
Reid Spencer | a94d394 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 5468 | if (SimplifyDemandedBits(&I, cast<IntegerType>(I.getType())->getBitMask(), |
Chris Lattner | f5b4ef7 | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 5469 | KnownZero, KnownOne)) |
| 5470 | return &I; |
| 5471 | |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5472 | // shl uint X, 32 = 0 and shr ubyte Y, 9 = 0, ... just don't eliminate shr |
| 5473 | // of a signed value. |
| 5474 | // |
| 5475 | unsigned TypeBits = Op0->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5476 | if (Op1->getZExtValue() >= TypeBits) { |
Chris Lattner | d5fea61 | 2007-02-02 05:29:55 +0000 | [diff] [blame] | 5477 | if (I.getOpcode() != Instruction::AShr) |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5478 | return ReplaceInstUsesWith(I, Constant::getNullValue(Op0->getType())); |
| 5479 | else { |
Chris Lattner | d5fea61 | 2007-02-02 05:29:55 +0000 | [diff] [blame] | 5480 | I.setOperand(1, ConstantInt::get(I.getType(), TypeBits-1)); |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5481 | return &I; |
Chris Lattner | f5ce254 | 2004-02-23 20:30:06 +0000 | [diff] [blame] | 5482 | } |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5483 | } |
| 5484 | |
| 5485 | // ((X*C1) << C2) == (X * (C1 << C2)) |
| 5486 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0)) |
| 5487 | if (BO->getOpcode() == Instruction::Mul && isLeftShift) |
| 5488 | if (Constant *BOOp = dyn_cast<Constant>(BO->getOperand(1))) |
| 5489 | return BinaryOperator::createMul(BO->getOperand(0), |
| 5490 | ConstantExpr::getShl(BOOp, Op1)); |
| 5491 | |
| 5492 | // Try to fold constant and into select arguments. |
| 5493 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 5494 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 5495 | return R; |
| 5496 | if (isa<PHINode>(Op0)) |
| 5497 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 5498 | return NV; |
| 5499 | |
| 5500 | if (Op0->hasOneUse()) { |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5501 | if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0)) { |
| 5502 | // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C) |
| 5503 | Value *V1, *V2; |
| 5504 | ConstantInt *CC; |
| 5505 | switch (Op0BO->getOpcode()) { |
Chris Lattner | 27cb9db | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 5506 | default: break; |
| 5507 | case Instruction::Add: |
| 5508 | case Instruction::And: |
| 5509 | case Instruction::Or: |
Reid Spencer | 2f34b98 | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 5510 | case Instruction::Xor: { |
Chris Lattner | 27cb9db | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 5511 | // These operators commute. |
| 5512 | // Turn (Y + (X >> C)) << C -> (X + (Y << C)) & (~0 << C) |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5513 | if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() && |
| 5514 | match(Op0BO->getOperand(1), |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5515 | m_Shr(m_Value(V1), m_ConstantInt(CC))) && CC == Op1) { |
Reid Spencer | 0d5f923 | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 5516 | Instruction *YS = BinaryOperator::createShl( |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5517 | Op0BO->getOperand(0), Op1, |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5518 | Op0BO->getName()); |
| 5519 | InsertNewInstBefore(YS, I); // (Y << C) |
Chris Lattner | 24cd2fa | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 5520 | Instruction *X = |
| 5521 | BinaryOperator::create(Op0BO->getOpcode(), YS, V1, |
| 5522 | Op0BO->getOperand(1)->getName()); |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5523 | InsertNewInstBefore(X, I); // (X + (Y << C)) |
| 5524 | Constant *C2 = ConstantInt::getAllOnesValue(X->getType()); |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5525 | C2 = ConstantExpr::getShl(C2, Op1); |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5526 | return BinaryOperator::createAnd(X, C2); |
| 5527 | } |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5528 | |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5529 | // Turn (Y + ((X >> C) & CC)) << C -> ((X & (CC << C)) + (Y << C)) |
Reid Spencer | 2f34b98 | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 5530 | Value *Op0BOOp1 = Op0BO->getOperand(1); |
| 5531 | if (isLeftShift && Op0BOOp1->hasOneUse() && V2 == Op1 && |
| 5532 | match(Op0BOOp1, |
| 5533 | m_And(m_Shr(m_Value(V1), m_Value(V2)),m_ConstantInt(CC))) && |
| 5534 | cast<BinaryOperator>(Op0BOOp1)->getOperand(0)-> hasOneUse()) { |
Reid Spencer | 0d5f923 | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 5535 | Instruction *YS = BinaryOperator::createShl( |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 5536 | Op0BO->getOperand(0), Op1, |
| 5537 | Op0BO->getName()); |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5538 | InsertNewInstBefore(YS, I); // (Y << C) |
| 5539 | Instruction *XM = |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5540 | BinaryOperator::createAnd(V1, ConstantExpr::getShl(CC, Op1), |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5541 | V1->getName()+".mask"); |
| 5542 | InsertNewInstBefore(XM, I); // X & (CC << C) |
| 5543 | |
| 5544 | return BinaryOperator::create(Op0BO->getOpcode(), YS, XM); |
| 5545 | } |
Reid Spencer | 2f34b98 | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 5546 | } |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5547 | |
Reid Spencer | 2f34b98 | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 5548 | // FALL THROUGH. |
| 5549 | case Instruction::Sub: { |
Chris Lattner | 27cb9db | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 5550 | // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C) |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5551 | if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() && |
| 5552 | match(Op0BO->getOperand(0), |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5553 | m_Shr(m_Value(V1), m_ConstantInt(CC))) && CC == Op1) { |
Reid Spencer | 0d5f923 | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 5554 | Instruction *YS = BinaryOperator::createShl( |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 5555 | Op0BO->getOperand(1), Op1, |
| 5556 | Op0BO->getName()); |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5557 | InsertNewInstBefore(YS, I); // (Y << C) |
Chris Lattner | 24cd2fa | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 5558 | Instruction *X = |
Chris Lattner | 1df0e98 | 2006-05-31 21:14:00 +0000 | [diff] [blame] | 5559 | BinaryOperator::create(Op0BO->getOpcode(), V1, YS, |
Chris Lattner | 24cd2fa | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 5560 | Op0BO->getOperand(0)->getName()); |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5561 | InsertNewInstBefore(X, I); // (X + (Y << C)) |
| 5562 | Constant *C2 = ConstantInt::getAllOnesValue(X->getType()); |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5563 | C2 = ConstantExpr::getShl(C2, Op1); |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5564 | return BinaryOperator::createAnd(X, C2); |
| 5565 | } |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5566 | |
Chris Lattner | 1df0e98 | 2006-05-31 21:14:00 +0000 | [diff] [blame] | 5567 | // Turn (((X >> C)&CC) + Y) << C -> (X + (Y << C)) & (CC << C) |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5568 | if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() && |
| 5569 | match(Op0BO->getOperand(0), |
| 5570 | m_And(m_Shr(m_Value(V1), m_Value(V2)), |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5571 | m_ConstantInt(CC))) && V2 == Op1 && |
Chris Lattner | 24cd2fa | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 5572 | cast<BinaryOperator>(Op0BO->getOperand(0)) |
| 5573 | ->getOperand(0)->hasOneUse()) { |
Reid Spencer | 0d5f923 | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 5574 | Instruction *YS = BinaryOperator::createShl( |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 5575 | Op0BO->getOperand(1), Op1, |
| 5576 | Op0BO->getName()); |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5577 | InsertNewInstBefore(YS, I); // (Y << C) |
| 5578 | Instruction *XM = |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5579 | BinaryOperator::createAnd(V1, ConstantExpr::getShl(CC, Op1), |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5580 | V1->getName()+".mask"); |
| 5581 | InsertNewInstBefore(XM, I); // X & (CC << C) |
| 5582 | |
Chris Lattner | 1df0e98 | 2006-05-31 21:14:00 +0000 | [diff] [blame] | 5583 | return BinaryOperator::create(Op0BO->getOpcode(), XM, YS); |
Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5584 | } |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5585 | |
Chris Lattner | 27cb9db | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 5586 | break; |
Reid Spencer | 2f34b98 | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 5587 | } |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5588 | } |
| 5589 | |
| 5590 | |
| 5591 | // If the operand is an bitwise operator with a constant RHS, and the |
| 5592 | // shift is the only use, we can pull it out of the shift. |
| 5593 | if (ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand(1))) { |
| 5594 | bool isValid = true; // Valid only for And, Or, Xor |
| 5595 | bool highBitSet = false; // Transform if high bit of constant set? |
| 5596 | |
| 5597 | switch (Op0BO->getOpcode()) { |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 5598 | default: isValid = false; break; // Do not perform transform! |
Chris Lattner | 44bd392 | 2004-10-08 03:46:20 +0000 | [diff] [blame] | 5599 | case Instruction::Add: |
| 5600 | isValid = isLeftShift; |
| 5601 | break; |
Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 5602 | case Instruction::Or: |
| 5603 | case Instruction::Xor: |
| 5604 | highBitSet = false; |
| 5605 | break; |
| 5606 | case Instruction::And: |
| 5607 | highBitSet = true; |
| 5608 | break; |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5609 | } |
| 5610 | |
| 5611 | // If this is a signed shift right, and the high bit is modified |
| 5612 | // by the logical operation, do not perform the transformation. |
| 5613 | // The highBitSet boolean indicates the value of the high bit of |
| 5614 | // the constant which would cause it to be modified for this |
| 5615 | // operation. |
| 5616 | // |
Chris Lattner | 3e009e8 | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5617 | if (isValid && !isLeftShift && I.getOpcode() == Instruction::AShr) { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5618 | uint64_t Val = Op0C->getZExtValue(); |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5619 | isValid = ((Val & (1 << (TypeBits-1))) != 0) == highBitSet; |
| 5620 | } |
| 5621 | |
| 5622 | if (isValid) { |
| 5623 | Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, Op1); |
| 5624 | |
| 5625 | Instruction *NewShift = |
Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 5626 | BinaryOperator::create(I.getOpcode(), Op0BO->getOperand(0), Op1); |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5627 | InsertNewInstBefore(NewShift, I); |
Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 5628 | NewShift->takeName(Op0BO); |
Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5629 | |
| 5630 | return BinaryOperator::create(Op0BO->getOpcode(), NewShift, |
| 5631 | NewRHS); |
| 5632 | } |
| 5633 | } |
| 5634 | } |
| 5635 | } |
| 5636 | |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5637 | // Find out if this is a shift of a shift by a constant. |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 5638 | BinaryOperator *ShiftOp = dyn_cast<BinaryOperator>(Op0); |
| 5639 | if (ShiftOp && !ShiftOp->isShift()) |
| 5640 | ShiftOp = 0; |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5641 | |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5642 | if (ShiftOp && isa<ConstantInt>(ShiftOp->getOperand(1))) { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5643 | ConstantInt *ShiftAmt1C = cast<ConstantInt>(ShiftOp->getOperand(1)); |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5644 | unsigned ShiftAmt1 = (unsigned)ShiftAmt1C->getZExtValue(); |
| 5645 | unsigned ShiftAmt2 = (unsigned)Op1->getZExtValue(); |
Chris Lattner | 3e009e8 | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5646 | assert(ShiftAmt2 != 0 && "Should have been simplified earlier"); |
| 5647 | if (ShiftAmt1 == 0) return 0; // Will be simplified in the future. |
| 5648 | Value *X = ShiftOp->getOperand(0); |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5649 | |
Chris Lattner | 3e009e8 | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5650 | unsigned AmtSum = ShiftAmt1+ShiftAmt2; // Fold into one big shift. |
| 5651 | if (AmtSum > I.getType()->getPrimitiveSizeInBits()) |
| 5652 | AmtSum = I.getType()->getPrimitiveSizeInBits(); |
| 5653 | |
| 5654 | const IntegerType *Ty = cast<IntegerType>(I.getType()); |
| 5655 | |
| 5656 | // Check for (X << c1) << c2 and (X >> c1) >> c2 |
Chris Lattner | 6c344e5 | 2007-02-03 23:28:07 +0000 | [diff] [blame] | 5657 | if (I.getOpcode() == ShiftOp->getOpcode()) { |
Chris Lattner | 3e009e8 | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5658 | return BinaryOperator::create(I.getOpcode(), X, |
| 5659 | ConstantInt::get(Ty, AmtSum)); |
| 5660 | } else if (ShiftOp->getOpcode() == Instruction::LShr && |
| 5661 | I.getOpcode() == Instruction::AShr) { |
| 5662 | // ((X >>u C1) >>s C2) -> (X >>u (C1+C2)) since C1 != 0. |
| 5663 | return BinaryOperator::createLShr(X, ConstantInt::get(Ty, AmtSum)); |
| 5664 | } else if (ShiftOp->getOpcode() == Instruction::AShr && |
| 5665 | I.getOpcode() == Instruction::LShr) { |
| 5666 | // ((X >>s C1) >>u C2) -> ((X >>s (C1+C2)) & mask) since C1 != 0. |
| 5667 | Instruction *Shift = |
| 5668 | BinaryOperator::createAShr(X, ConstantInt::get(Ty, AmtSum)); |
| 5669 | InsertNewInstBefore(Shift, I); |
| 5670 | |
| 5671 | uint64_t Mask = Ty->getBitMask() >> ShiftAmt2; |
| 5672 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Ty, Mask)); |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5673 | } |
| 5674 | |
Chris Lattner | 3e009e8 | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5675 | // Okay, if we get here, one shift must be left, and the other shift must be |
| 5676 | // right. See if the amounts are equal. |
| 5677 | if (ShiftAmt1 == ShiftAmt2) { |
| 5678 | // If we have ((X >>? C) << C), turn this into X & (-1 << C). |
| 5679 | if (I.getOpcode() == Instruction::Shl) { |
Chris Lattner | 0a28e90 | 2007-02-05 04:09:35 +0000 | [diff] [blame] | 5680 | uint64_t Mask = Ty->getBitMask() << ShiftAmt1; |
Chris Lattner | 3e009e8 | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5681 | return BinaryOperator::createAnd(X, ConstantInt::get(Ty, Mask)); |
| 5682 | } |
| 5683 | // If we have ((X << C) >>u C), turn this into X & (-1 >>u C). |
| 5684 | if (I.getOpcode() == Instruction::LShr) { |
Chris Lattner | 0a28e90 | 2007-02-05 04:09:35 +0000 | [diff] [blame] | 5685 | uint64_t Mask = Ty->getBitMask() >> ShiftAmt1; |
Chris Lattner | 3e009e8 | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5686 | return BinaryOperator::createAnd(X, ConstantInt::get(Ty, Mask)); |
| 5687 | } |
| 5688 | // We can simplify ((X << C) >>s C) into a trunc + sext. |
| 5689 | // NOTE: we could do this for any C, but that would make 'unusual' integer |
| 5690 | // types. For now, just stick to ones well-supported by the code |
| 5691 | // generators. |
| 5692 | const Type *SExtType = 0; |
| 5693 | switch (Ty->getBitWidth() - ShiftAmt1) { |
| 5694 | case 8 : SExtType = Type::Int8Ty; break; |
| 5695 | case 16: SExtType = Type::Int16Ty; break; |
| 5696 | case 32: SExtType = Type::Int32Ty; break; |
| 5697 | default: break; |
| 5698 | } |
| 5699 | if (SExtType) { |
| 5700 | Instruction *NewTrunc = new TruncInst(X, SExtType, "sext"); |
| 5701 | InsertNewInstBefore(NewTrunc, I); |
| 5702 | return new SExtInst(NewTrunc, Ty); |
| 5703 | } |
| 5704 | // Otherwise, we can't handle it yet. |
| 5705 | } else if (ShiftAmt1 < ShiftAmt2) { |
| 5706 | unsigned ShiftDiff = ShiftAmt2-ShiftAmt1; |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5707 | |
Chris Lattner | 83ac5ae9 | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 5708 | // (X >>? C1) << C2 --> X << (C2-C1) & (-1 << C2) |
Chris Lattner | 3e009e8 | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5709 | if (I.getOpcode() == Instruction::Shl) { |
| 5710 | assert(ShiftOp->getOpcode() == Instruction::LShr || |
| 5711 | ShiftOp->getOpcode() == Instruction::AShr); |
Chris Lattner | 9cbfbc2 | 2006-01-07 01:32:28 +0000 | [diff] [blame] | 5712 | Instruction *Shift = |
Chris Lattner | 3e009e8 | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5713 | BinaryOperator::createShl(X, ConstantInt::get(Ty, ShiftDiff)); |
Chris Lattner | 9cbfbc2 | 2006-01-07 01:32:28 +0000 | [diff] [blame] | 5714 | InsertNewInstBefore(Shift, I); |
| 5715 | |
Chris Lattner | 83ac5ae9 | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 5716 | uint64_t Mask = Ty->getBitMask() << ShiftAmt2; |
Chris Lattner | 3e009e8 | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5717 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Ty, Mask)); |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5718 | } |
Chris Lattner | 3e009e8 | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5719 | |
Chris Lattner | 83ac5ae9 | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 5720 | // (X << C1) >>u C2 --> X >>u (C2-C1) & (-1 >> C2) |
Chris Lattner | 3e009e8 | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5721 | if (I.getOpcode() == Instruction::LShr) { |
| 5722 | assert(ShiftOp->getOpcode() == Instruction::Shl); |
| 5723 | Instruction *Shift = |
| 5724 | BinaryOperator::createLShr(X, ConstantInt::get(Ty, ShiftDiff)); |
| 5725 | InsertNewInstBefore(Shift, I); |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5726 | |
Chris Lattner | 83ac5ae9 | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 5727 | uint64_t Mask = Ty->getBitMask() >> ShiftAmt2; |
Chris Lattner | 3e009e8 | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5728 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Ty, Mask)); |
Chris Lattner | 27cb9db | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 5729 | } |
Chris Lattner | 3e009e8 | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5730 | |
| 5731 | // We can't handle (X << C1) >>s C2, it shifts arbitrary bits in. |
| 5732 | } else { |
| 5733 | assert(ShiftAmt2 < ShiftAmt1); |
| 5734 | unsigned ShiftDiff = ShiftAmt1-ShiftAmt2; |
| 5735 | |
Chris Lattner | 83ac5ae9 | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 5736 | // (X >>? C1) << C2 --> X >>? (C1-C2) & (-1 << C2) |
Chris Lattner | 3e009e8 | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5737 | if (I.getOpcode() == Instruction::Shl) { |
| 5738 | assert(ShiftOp->getOpcode() == Instruction::LShr || |
| 5739 | ShiftOp->getOpcode() == Instruction::AShr); |
| 5740 | Instruction *Shift = |
| 5741 | BinaryOperator::create(ShiftOp->getOpcode(), X, |
| 5742 | ConstantInt::get(Ty, ShiftDiff)); |
| 5743 | InsertNewInstBefore(Shift, I); |
| 5744 | |
| 5745 | uint64_t Mask = Ty->getBitMask() << ShiftAmt2; |
| 5746 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Ty, Mask)); |
| 5747 | } |
| 5748 | |
Chris Lattner | 83ac5ae9 | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 5749 | // (X << C1) >>u C2 --> X << (C1-C2) & (-1 >> C2) |
Chris Lattner | 3e009e8 | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5750 | if (I.getOpcode() == Instruction::LShr) { |
| 5751 | assert(ShiftOp->getOpcode() == Instruction::Shl); |
| 5752 | Instruction *Shift = |
| 5753 | BinaryOperator::createShl(X, ConstantInt::get(Ty, ShiftDiff)); |
| 5754 | InsertNewInstBefore(Shift, I); |
| 5755 | |
| 5756 | uint64_t Mask = Ty->getBitMask() >> ShiftAmt2; |
| 5757 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Ty, Mask)); |
| 5758 | } |
| 5759 | |
| 5760 | // We can't handle (X << C1) >>a C2, it shifts arbitrary bits in. |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 5761 | } |
Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5762 | } |
Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5763 | return 0; |
| 5764 | } |
| 5765 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 5766 | |
Chris Lattner | 8f663e8 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 5767 | /// DecomposeSimpleLinearExpr - Analyze 'Val', seeing if it is a simple linear |
| 5768 | /// expression. If so, decompose it, returning some value X, such that Val is |
| 5769 | /// X*Scale+Offset. |
| 5770 | /// |
| 5771 | static Value *DecomposeSimpleLinearExpr(Value *Val, unsigned &Scale, |
| 5772 | unsigned &Offset) { |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 5773 | assert(Val->getType() == Type::Int32Ty && "Unexpected allocation size type!"); |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5774 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) { |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 5775 | Offset = CI->getZExtValue(); |
| 5776 | Scale = 1; |
| 5777 | return ConstantInt::get(Type::Int32Ty, 0); |
Chris Lattner | 8f663e8 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 5778 | } else if (Instruction *I = dyn_cast<Instruction>(Val)) { |
| 5779 | if (I->getNumOperands() == 2) { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5780 | if (ConstantInt *CUI = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 5781 | if (I->getOpcode() == Instruction::Shl) { |
| 5782 | // This is a value scaled by '1 << the shift amt'. |
| 5783 | Scale = 1U << CUI->getZExtValue(); |
| 5784 | Offset = 0; |
| 5785 | return I->getOperand(0); |
| 5786 | } else if (I->getOpcode() == Instruction::Mul) { |
| 5787 | // This value is scaled by 'CUI'. |
| 5788 | Scale = CUI->getZExtValue(); |
| 5789 | Offset = 0; |
| 5790 | return I->getOperand(0); |
| 5791 | } else if (I->getOpcode() == Instruction::Add) { |
| 5792 | // We have X+C. Check to see if we really have (X*C2)+C1, |
| 5793 | // where C1 is divisible by C2. |
| 5794 | unsigned SubScale; |
| 5795 | Value *SubVal = |
| 5796 | DecomposeSimpleLinearExpr(I->getOperand(0), SubScale, Offset); |
| 5797 | Offset += CUI->getZExtValue(); |
| 5798 | if (SubScale > 1 && (Offset % SubScale == 0)) { |
| 5799 | Scale = SubScale; |
| 5800 | return SubVal; |
Chris Lattner | 8f663e8 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 5801 | } |
| 5802 | } |
| 5803 | } |
| 5804 | } |
| 5805 | } |
| 5806 | |
| 5807 | // Otherwise, we can't look past this. |
| 5808 | Scale = 1; |
| 5809 | Offset = 0; |
| 5810 | return Val; |
| 5811 | } |
| 5812 | |
| 5813 | |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 5814 | /// PromoteCastOfAllocation - If we find a cast of an allocation instruction, |
| 5815 | /// try to eliminate the cast by moving the type information into the alloc. |
| 5816 | Instruction *InstCombiner::PromoteCastOfAllocation(CastInst &CI, |
| 5817 | AllocationInst &AI) { |
| 5818 | const PointerType *PTy = dyn_cast<PointerType>(CI.getType()); |
Chris Lattner | bb17180 | 2005-10-27 05:53:56 +0000 | [diff] [blame] | 5819 | if (!PTy) return 0; // Not casting the allocation to a pointer type. |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 5820 | |
Chris Lattner | ac87beb | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 5821 | // Remove any uses of AI that are dead. |
| 5822 | assert(!CI.use_empty() && "Dead instructions should be removed earlier!"); |
Chris Lattner | 99c6cf6 | 2007-02-15 22:52:10 +0000 | [diff] [blame] | 5823 | |
Chris Lattner | ac87beb | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 5824 | for (Value::use_iterator UI = AI.use_begin(), E = AI.use_end(); UI != E; ) { |
| 5825 | Instruction *User = cast<Instruction>(*UI++); |
| 5826 | if (isInstructionTriviallyDead(User)) { |
| 5827 | while (UI != E && *UI == User) |
| 5828 | ++UI; // If this instruction uses AI more than once, don't break UI. |
| 5829 | |
Chris Lattner | ac87beb | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 5830 | ++NumDeadInst; |
Bill Wendling | 5dbf43c | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 5831 | DOUT << "IC: DCE: " << *User; |
Chris Lattner | 51f5457 | 2007-03-02 19:59:19 +0000 | [diff] [blame] | 5832 | EraseInstFromFunction(*User); |
Chris Lattner | ac87beb | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 5833 | } |
| 5834 | } |
| 5835 | |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 5836 | // Get the type really allocated and the type casted to. |
| 5837 | const Type *AllocElTy = AI.getAllocatedType(); |
| 5838 | const Type *CastElTy = PTy->getElementType(); |
| 5839 | if (!AllocElTy->isSized() || !CastElTy->isSized()) return 0; |
Chris Lattner | 355ecc0 | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 5840 | |
Chris Lattner | 945e437 | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 5841 | unsigned AllocElTyAlign = TD->getABITypeAlignment(AllocElTy); |
| 5842 | unsigned CastElTyAlign = TD->getABITypeAlignment(CastElTy); |
Chris Lattner | 355ecc0 | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 5843 | if (CastElTyAlign < AllocElTyAlign) return 0; |
| 5844 | |
Chris Lattner | 46705b2 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 5845 | // If the allocation has multiple uses, only promote it if we are strictly |
| 5846 | // increasing the alignment of the resultant allocation. If we keep it the |
| 5847 | // same, we open the door to infinite loops of various kinds. |
| 5848 | if (!AI.hasOneUse() && CastElTyAlign == AllocElTyAlign) return 0; |
| 5849 | |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 5850 | uint64_t AllocElTySize = TD->getTypeSize(AllocElTy); |
| 5851 | uint64_t CastElTySize = TD->getTypeSize(CastElTy); |
Chris Lattner | bb17180 | 2005-10-27 05:53:56 +0000 | [diff] [blame] | 5852 | if (CastElTySize == 0 || AllocElTySize == 0) return 0; |
Chris Lattner | 355ecc0 | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 5853 | |
Chris Lattner | 8270c33 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 5854 | // See if we can satisfy the modulus by pulling a scale out of the array |
| 5855 | // size argument. |
Chris Lattner | 8f663e8 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 5856 | unsigned ArraySizeScale, ArrayOffset; |
| 5857 | Value *NumElements = // See if the array size is a decomposable linear expr. |
| 5858 | DecomposeSimpleLinearExpr(AI.getOperand(0), ArraySizeScale, ArrayOffset); |
| 5859 | |
Chris Lattner | 8270c33 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 5860 | // If we can now satisfy the modulus, by using a non-1 scale, we really can |
| 5861 | // do the xform. |
Chris Lattner | 8f663e8 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 5862 | if ((AllocElTySize*ArraySizeScale) % CastElTySize != 0 || |
| 5863 | (AllocElTySize*ArrayOffset ) % CastElTySize != 0) return 0; |
Chris Lattner | b3ecf96 | 2005-10-27 06:12:00 +0000 | [diff] [blame] | 5864 | |
Chris Lattner | 8270c33 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 5865 | unsigned Scale = (AllocElTySize*ArraySizeScale)/CastElTySize; |
| 5866 | Value *Amt = 0; |
| 5867 | if (Scale == 1) { |
| 5868 | Amt = NumElements; |
| 5869 | } else { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5870 | // If the allocation size is constant, form a constant mul expression |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 5871 | Amt = ConstantInt::get(Type::Int32Ty, Scale); |
| 5872 | if (isa<ConstantInt>(NumElements)) |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5873 | Amt = ConstantExpr::getMul( |
| 5874 | cast<ConstantInt>(NumElements), cast<ConstantInt>(Amt)); |
| 5875 | // otherwise multiply the amount and the number of elements |
Chris Lattner | 8270c33 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 5876 | else if (Scale != 1) { |
| 5877 | Instruction *Tmp = BinaryOperator::createMul(Amt, NumElements, "tmp"); |
| 5878 | Amt = InsertNewInstBefore(Tmp, AI); |
Chris Lattner | b3ecf96 | 2005-10-27 06:12:00 +0000 | [diff] [blame] | 5879 | } |
Chris Lattner | bb17180 | 2005-10-27 05:53:56 +0000 | [diff] [blame] | 5880 | } |
| 5881 | |
Chris Lattner | 8f663e8 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 5882 | if (unsigned Offset = (AllocElTySize*ArrayOffset)/CastElTySize) { |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 5883 | Value *Off = ConstantInt::get(Type::Int32Ty, Offset); |
Chris Lattner | 8f663e8 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 5884 | Instruction *Tmp = BinaryOperator::createAdd(Amt, Off, "tmp"); |
| 5885 | Amt = InsertNewInstBefore(Tmp, AI); |
| 5886 | } |
| 5887 | |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 5888 | AllocationInst *New; |
| 5889 | if (isa<MallocInst>(AI)) |
Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 5890 | New = new MallocInst(CastElTy, Amt, AI.getAlignment()); |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 5891 | else |
Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 5892 | New = new AllocaInst(CastElTy, Amt, AI.getAlignment()); |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 5893 | InsertNewInstBefore(New, AI); |
Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 5894 | New->takeName(&AI); |
Chris Lattner | 46705b2 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 5895 | |
| 5896 | // If the allocation has multiple uses, insert a cast and change all things |
| 5897 | // that used it to use the new cast. This will also hack on CI, but it will |
| 5898 | // die soon. |
| 5899 | if (!AI.hasOneUse()) { |
| 5900 | AddUsesToWorkList(AI); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 5901 | // New is the allocation instruction, pointer typed. AI is the original |
| 5902 | // allocation instruction, also pointer typed. Thus, cast to use is BitCast. |
| 5903 | CastInst *NewCast = new BitCastInst(New, AI.getType(), "tmpcast"); |
Chris Lattner | 46705b2 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 5904 | InsertNewInstBefore(NewCast, AI); |
| 5905 | AI.replaceAllUsesWith(NewCast); |
| 5906 | } |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 5907 | return ReplaceInstUsesWith(CI, New); |
| 5908 | } |
| 5909 | |
Chris Lattner | 1ebbe6a | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 5910 | /// CanEvaluateInDifferentType - Return true if we can take the specified value |
Chris Lattner | da1d04a | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 5911 | /// and return it as type Ty without inserting any new casts and without |
| 5912 | /// changing the computed value. This is used by code that tries to decide |
| 5913 | /// whether promoting or shrinking integer operations to wider or smaller types |
| 5914 | /// will allow us to eliminate a truncate or extend. |
| 5915 | /// |
| 5916 | /// This is a truncation operation if Ty is smaller than V->getType(), or an |
| 5917 | /// extension operation if Ty is larger. |
| 5918 | static bool CanEvaluateInDifferentType(Value *V, const IntegerType *Ty, |
Chris Lattner | 1ebbe6a | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 5919 | int &NumCastsRemoved) { |
Chris Lattner | da1d04a | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 5920 | // We can always evaluate constants in another type. |
| 5921 | if (isa<ConstantInt>(V)) |
| 5922 | return true; |
Chris Lattner | 1ebbe6a | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 5923 | |
| 5924 | Instruction *I = dyn_cast<Instruction>(V); |
Chris Lattner | da1d04a | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 5925 | if (!I) return false; |
| 5926 | |
| 5927 | const IntegerType *OrigTy = cast<IntegerType>(V->getType()); |
Chris Lattner | 1ebbe6a | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 5928 | |
| 5929 | switch (I->getOpcode()) { |
Chris Lattner | da1d04a | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 5930 | case Instruction::Add: |
| 5931 | case Instruction::Sub: |
Chris Lattner | 1ebbe6a | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 5932 | case Instruction::And: |
| 5933 | case Instruction::Or: |
| 5934 | case Instruction::Xor: |
Chris Lattner | da1d04a | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 5935 | if (!I->hasOneUse()) return false; |
Chris Lattner | 1ebbe6a | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 5936 | // These operators can all arbitrarily be extended or truncated. |
| 5937 | return CanEvaluateInDifferentType(I->getOperand(0), Ty, NumCastsRemoved) && |
| 5938 | CanEvaluateInDifferentType(I->getOperand(1), Ty, NumCastsRemoved); |
Chris Lattner | da1d04a | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 5939 | |
Chris Lattner | 960acb0 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 5940 | case Instruction::Shl: |
Chris Lattner | da1d04a | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 5941 | if (!I->hasOneUse()) return false; |
| 5942 | // If we are truncating the result of this SHL, and if it's a shift of a |
| 5943 | // constant amount, we can always perform a SHL in a smaller type. |
| 5944 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 5945 | if (Ty->getBitWidth() < OrigTy->getBitWidth() && |
| 5946 | CI->getZExtValue() < Ty->getBitWidth()) |
| 5947 | return CanEvaluateInDifferentType(I->getOperand(0), Ty,NumCastsRemoved); |
| 5948 | } |
| 5949 | break; |
| 5950 | case Instruction::LShr: |
| 5951 | if (!I->hasOneUse()) return false; |
| 5952 | // If this is a truncate of a logical shr, we can truncate it to a smaller |
| 5953 | // lshr iff we know that the bits we would otherwise be shifting in are |
| 5954 | // already zeros. |
| 5955 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 5956 | if (Ty->getBitWidth() < OrigTy->getBitWidth() && |
| 5957 | MaskedValueIsZero(I->getOperand(0), |
| 5958 | OrigTy->getBitMask() & ~Ty->getBitMask()) && |
| 5959 | CI->getZExtValue() < Ty->getBitWidth()) { |
| 5960 | return CanEvaluateInDifferentType(I->getOperand(0), Ty, NumCastsRemoved); |
| 5961 | } |
| 5962 | } |
Chris Lattner | 960acb0 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 5963 | break; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 5964 | case Instruction::Trunc: |
| 5965 | case Instruction::ZExt: |
| 5966 | case Instruction::SExt: |
Chris Lattner | 1ebbe6a | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 5967 | // If this is a cast from the destination type, we can trivially eliminate |
| 5968 | // it, and this will remove a cast overall. |
| 5969 | if (I->getOperand(0)->getType() == Ty) { |
Chris Lattner | 3fda386 | 2006-06-28 17:34:50 +0000 | [diff] [blame] | 5970 | // If the first operand is itself a cast, and is eliminable, do not count |
| 5971 | // this as an eliminable cast. We would prefer to eliminate those two |
| 5972 | // casts first. |
Reid Spencer | de46e48 | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 5973 | if (isa<CastInst>(I->getOperand(0))) |
Chris Lattner | 3fda386 | 2006-06-28 17:34:50 +0000 | [diff] [blame] | 5974 | return true; |
| 5975 | |
Chris Lattner | 1ebbe6a | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 5976 | ++NumCastsRemoved; |
| 5977 | return true; |
| 5978 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 5979 | break; |
| 5980 | default: |
Chris Lattner | 1ebbe6a | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 5981 | // TODO: Can handle more cases here. |
| 5982 | break; |
| 5983 | } |
| 5984 | |
| 5985 | return false; |
| 5986 | } |
| 5987 | |
| 5988 | /// EvaluateInDifferentType - Given an expression that |
| 5989 | /// CanEvaluateInDifferentType returns true for, actually insert the code to |
| 5990 | /// evaluate the expression. |
Reid Spencer | 74a528b | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 5991 | Value *InstCombiner::EvaluateInDifferentType(Value *V, const Type *Ty, |
Chris Lattner | da1d04a | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 5992 | bool isSigned) { |
Chris Lattner | 1ebbe6a | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 5993 | if (Constant *C = dyn_cast<Constant>(V)) |
Reid Spencer | 74a528b | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 5994 | return ConstantExpr::getIntegerCast(C, Ty, isSigned /*Sext or ZExt*/); |
Chris Lattner | 1ebbe6a | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 5995 | |
| 5996 | // Otherwise, it must be an instruction. |
| 5997 | Instruction *I = cast<Instruction>(V); |
Chris Lattner | d0622b6 | 2006-05-20 23:14:03 +0000 | [diff] [blame] | 5998 | Instruction *Res = 0; |
Chris Lattner | 1ebbe6a | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 5999 | switch (I->getOpcode()) { |
Chris Lattner | da1d04a | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6000 | case Instruction::Add: |
| 6001 | case Instruction::Sub: |
Chris Lattner | 1ebbe6a | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6002 | case Instruction::And: |
| 6003 | case Instruction::Or: |
Chris Lattner | da1d04a | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6004 | case Instruction::Xor: |
Chris Lattner | 960acb0 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 6005 | case Instruction::AShr: |
| 6006 | case Instruction::LShr: |
| 6007 | case Instruction::Shl: { |
Reid Spencer | 74a528b | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 6008 | Value *LHS = EvaluateInDifferentType(I->getOperand(0), Ty, isSigned); |
Chris Lattner | da1d04a | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6009 | Value *RHS = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned); |
| 6010 | Res = BinaryOperator::create((Instruction::BinaryOps)I->getOpcode(), |
| 6011 | LHS, RHS, I->getName()); |
Chris Lattner | 960acb0 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 6012 | break; |
| 6013 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6014 | case Instruction::Trunc: |
| 6015 | case Instruction::ZExt: |
| 6016 | case Instruction::SExt: |
| 6017 | case Instruction::BitCast: |
| 6018 | // If the source type of the cast is the type we're trying for then we can |
| 6019 | // just return the source. There's no need to insert it because its not new. |
Chris Lattner | 1ebbe6a | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6020 | if (I->getOperand(0)->getType() == Ty) |
| 6021 | return I->getOperand(0); |
| 6022 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6023 | // Some other kind of cast, which shouldn't happen, so just .. |
| 6024 | // FALL THROUGH |
| 6025 | default: |
Chris Lattner | 1ebbe6a | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6026 | // TODO: Can handle more cases here. |
| 6027 | assert(0 && "Unreachable!"); |
| 6028 | break; |
| 6029 | } |
| 6030 | |
| 6031 | return InsertNewInstBefore(Res, *I); |
| 6032 | } |
| 6033 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6034 | /// @brief Implement the transforms common to all CastInst visitors. |
| 6035 | Instruction *InstCombiner::commonCastTransforms(CastInst &CI) { |
Chris Lattner | 55d4bda | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 6036 | Value *Src = CI.getOperand(0); |
| 6037 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6038 | // Casting undef to anything results in undef so might as just replace it and |
| 6039 | // get rid of the cast. |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 6040 | if (isa<UndefValue>(Src)) // cast undef -> undef |
| 6041 | return ReplaceInstUsesWith(CI, UndefValue::get(CI.getType())); |
| 6042 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6043 | // Many cases of "cast of a cast" are eliminable. If its eliminable we just |
| 6044 | // eliminate it now. |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 6045 | if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6046 | if (Instruction::CastOps opc = |
| 6047 | isEliminableCastPair(CSrc, CI.getOpcode(), CI.getType(), TD)) { |
| 6048 | // The first cast (CSrc) is eliminable so we need to fix up or replace |
| 6049 | // the second cast (CI). CSrc will then have a good chance of being dead. |
| 6050 | return CastInst::create(opc, CSrc->getOperand(0), CI.getType()); |
Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 6051 | } |
| 6052 | } |
Chris Lattner | 0384165 | 2004-05-25 04:29:21 +0000 | [diff] [blame] | 6053 | |
Chris Lattner | d0d5160 | 2003-06-21 23:12:02 +0000 | [diff] [blame] | 6054 | // If casting the result of a getelementptr instruction with no offset, turn |
| 6055 | // this into a cast of the original pointer! |
| 6056 | // |
Chris Lattner | 55d4bda | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 6057 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Src)) { |
Chris Lattner | d0d5160 | 2003-06-21 23:12:02 +0000 | [diff] [blame] | 6058 | bool AllZeroOperands = true; |
| 6059 | for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i) |
| 6060 | if (!isa<Constant>(GEP->getOperand(i)) || |
| 6061 | !cast<Constant>(GEP->getOperand(i))->isNullValue()) { |
| 6062 | AllZeroOperands = false; |
| 6063 | break; |
| 6064 | } |
| 6065 | if (AllZeroOperands) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6066 | // Changing the cast operand is usually not a good idea but it is safe |
| 6067 | // here because the pointer operand is being replaced with another |
| 6068 | // pointer operand so the opcode doesn't need to change. |
Chris Lattner | d0d5160 | 2003-06-21 23:12:02 +0000 | [diff] [blame] | 6069 | CI.setOperand(0, GEP->getOperand(0)); |
| 6070 | return &CI; |
| 6071 | } |
| 6072 | } |
Chris Lattner | ec45a4c | 2006-11-21 17:05:13 +0000 | [diff] [blame] | 6073 | |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 6074 | // If we are casting a malloc or alloca to a pointer to a type of the same |
| 6075 | // size, rewrite the allocation instruction to allocate the "right" type. |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 6076 | if (AllocationInst *AI = dyn_cast<AllocationInst>(Src)) |
Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6077 | if (Instruction *V = PromoteCastOfAllocation(CI, *AI)) |
| 6078 | return V; |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 6079 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6080 | // If we are casting a select then fold the cast into the select |
Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 6081 | if (SelectInst *SI = dyn_cast<SelectInst>(Src)) |
| 6082 | if (Instruction *NV = FoldOpIntoSelect(CI, SI, this)) |
| 6083 | return NV; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6084 | |
| 6085 | // If we are casting a PHI then fold the cast into the PHI |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 6086 | if (isa<PHINode>(Src)) |
| 6087 | if (Instruction *NV = FoldOpIntoPhi(CI)) |
| 6088 | return NV; |
Chris Lattner | b19a5c6 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 6089 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6090 | return 0; |
| 6091 | } |
| 6092 | |
Chris Lattner | da1d04a | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6093 | /// Only the TRUNC, ZEXT, SEXT, and BITCAST can both operand and result as |
| 6094 | /// integer types. This function implements the common transforms for all those |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6095 | /// cases. |
| 6096 | /// @brief Implement the transforms common to CastInst with integer operands |
| 6097 | Instruction *InstCombiner::commonIntCastTransforms(CastInst &CI) { |
| 6098 | if (Instruction *Result = commonCastTransforms(CI)) |
| 6099 | return Result; |
| 6100 | |
| 6101 | Value *Src = CI.getOperand(0); |
| 6102 | const Type *SrcTy = Src->getType(); |
| 6103 | const Type *DestTy = CI.getType(); |
| 6104 | unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits(); |
| 6105 | unsigned DestBitSize = DestTy->getPrimitiveSizeInBits(); |
| 6106 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6107 | // See if we can simplify any instructions used by the LHS whose sole |
| 6108 | // purpose is to compute bits we don't care about. |
| 6109 | uint64_t KnownZero = 0, KnownOne = 0; |
Reid Spencer | a94d394 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 6110 | if (SimplifyDemandedBits(&CI, cast<IntegerType>(DestTy)->getBitMask(), |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6111 | KnownZero, KnownOne)) |
| 6112 | return &CI; |
| 6113 | |
| 6114 | // If the source isn't an instruction or has more than one use then we |
| 6115 | // can't do anything more. |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6116 | Instruction *SrcI = dyn_cast<Instruction>(Src); |
| 6117 | if (!SrcI || !Src->hasOneUse()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6118 | return 0; |
| 6119 | |
Chris Lattner | da1d04a | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6120 | // Attempt to propagate the cast into the instruction for int->int casts. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6121 | int NumCastsRemoved = 0; |
Chris Lattner | da1d04a | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6122 | if (!isa<BitCastInst>(CI) && |
| 6123 | CanEvaluateInDifferentType(SrcI, cast<IntegerType>(DestTy), |
| 6124 | NumCastsRemoved)) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6125 | // If this cast is a truncate, evaluting in a different type always |
| 6126 | // eliminates the cast, so it is always a win. If this is a noop-cast |
| 6127 | // this just removes a noop cast which isn't pointful, but simplifies |
| 6128 | // the code. If this is a zero-extension, we need to do an AND to |
| 6129 | // maintain the clear top-part of the computation, so we require that |
| 6130 | // the input have eliminated at least one cast. If this is a sign |
| 6131 | // extension, we insert two new casts (to do the extension) so we |
| 6132 | // require that two casts have been eliminated. |
Chris Lattner | da1d04a | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6133 | bool DoXForm; |
| 6134 | switch (CI.getOpcode()) { |
| 6135 | default: |
| 6136 | // All the others use floating point so we shouldn't actually |
| 6137 | // get here because of the check above. |
| 6138 | assert(0 && "Unknown cast type"); |
| 6139 | case Instruction::Trunc: |
| 6140 | DoXForm = true; |
| 6141 | break; |
| 6142 | case Instruction::ZExt: |
| 6143 | DoXForm = NumCastsRemoved >= 1; |
| 6144 | break; |
| 6145 | case Instruction::SExt: |
| 6146 | DoXForm = NumCastsRemoved >= 2; |
| 6147 | break; |
| 6148 | case Instruction::BitCast: |
| 6149 | DoXForm = false; |
| 6150 | break; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6151 | } |
| 6152 | |
| 6153 | if (DoXForm) { |
Reid Spencer | 74a528b | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 6154 | Value *Res = EvaluateInDifferentType(SrcI, DestTy, |
| 6155 | CI.getOpcode() == Instruction::SExt); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6156 | assert(Res->getType() == DestTy); |
| 6157 | switch (CI.getOpcode()) { |
| 6158 | default: assert(0 && "Unknown cast type!"); |
| 6159 | case Instruction::Trunc: |
| 6160 | case Instruction::BitCast: |
| 6161 | // Just replace this cast with the result. |
| 6162 | return ReplaceInstUsesWith(CI, Res); |
| 6163 | case Instruction::ZExt: { |
| 6164 | // We need to emit an AND to clear the high bits. |
| 6165 | assert(SrcBitSize < DestBitSize && "Not a zext?"); |
| 6166 | Constant *C = |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6167 | ConstantInt::get(Type::Int64Ty, (1ULL << SrcBitSize)-1); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6168 | if (DestBitSize < 64) |
| 6169 | C = ConstantExpr::getTrunc(C, DestTy); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6170 | return BinaryOperator::createAnd(Res, C); |
| 6171 | } |
| 6172 | case Instruction::SExt: |
| 6173 | // We need to emit a cast to truncate, then a cast to sext. |
| 6174 | return CastInst::create(Instruction::SExt, |
Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 6175 | InsertCastBefore(Instruction::Trunc, Res, Src->getType(), |
| 6176 | CI), DestTy); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6177 | } |
| 6178 | } |
| 6179 | } |
| 6180 | |
| 6181 | Value *Op0 = SrcI->getNumOperands() > 0 ? SrcI->getOperand(0) : 0; |
| 6182 | Value *Op1 = SrcI->getNumOperands() > 1 ? SrcI->getOperand(1) : 0; |
| 6183 | |
| 6184 | switch (SrcI->getOpcode()) { |
| 6185 | case Instruction::Add: |
| 6186 | case Instruction::Mul: |
| 6187 | case Instruction::And: |
| 6188 | case Instruction::Or: |
| 6189 | case Instruction::Xor: |
| 6190 | // If we are discarding information, or just changing the sign, |
| 6191 | // rewrite. |
| 6192 | if (DestBitSize <= SrcBitSize && DestBitSize != 1) { |
| 6193 | // Don't insert two casts if they cannot be eliminated. We allow |
| 6194 | // two casts to be inserted if the sizes are the same. This could |
| 6195 | // only be converting signedness, which is a noop. |
| 6196 | if (DestBitSize == SrcBitSize || |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6197 | !ValueRequiresCast(CI.getOpcode(), Op1, DestTy,TD) || |
| 6198 | !ValueRequiresCast(CI.getOpcode(), Op0, DestTy, TD)) { |
Reid Spencer | 2a499b0 | 2006-12-13 17:19:09 +0000 | [diff] [blame] | 6199 | Instruction::CastOps opcode = CI.getOpcode(); |
Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 6200 | Value *Op0c = InsertOperandCastBefore(opcode, Op0, DestTy, SrcI); |
| 6201 | Value *Op1c = InsertOperandCastBefore(opcode, Op1, DestTy, SrcI); |
| 6202 | return BinaryOperator::create( |
| 6203 | cast<BinaryOperator>(SrcI)->getOpcode(), Op0c, Op1c); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6204 | } |
| 6205 | } |
| 6206 | |
| 6207 | // cast (xor bool X, true) to int --> xor (cast bool X to int), 1 |
| 6208 | if (isa<ZExtInst>(CI) && SrcBitSize == 1 && |
| 6209 | SrcI->getOpcode() == Instruction::Xor && |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6210 | Op1 == ConstantInt::getTrue() && |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6211 | (!Op0->hasOneUse() || !isa<CmpInst>(Op0))) { |
Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 6212 | Value *New = InsertOperandCastBefore(Instruction::ZExt, Op0, DestTy, &CI); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6213 | return BinaryOperator::createXor(New, ConstantInt::get(CI.getType(), 1)); |
| 6214 | } |
| 6215 | break; |
| 6216 | case Instruction::SDiv: |
| 6217 | case Instruction::UDiv: |
| 6218 | case Instruction::SRem: |
| 6219 | case Instruction::URem: |
| 6220 | // If we are just changing the sign, rewrite. |
| 6221 | if (DestBitSize == SrcBitSize) { |
| 6222 | // Don't insert two casts if they cannot be eliminated. We allow |
| 6223 | // two casts to be inserted if the sizes are the same. This could |
| 6224 | // only be converting signedness, which is a noop. |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6225 | if (!ValueRequiresCast(CI.getOpcode(), Op1, DestTy, TD) || |
| 6226 | !ValueRequiresCast(CI.getOpcode(), Op0, DestTy, TD)) { |
Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 6227 | Value *Op0c = InsertOperandCastBefore(Instruction::BitCast, |
| 6228 | Op0, DestTy, SrcI); |
| 6229 | Value *Op1c = InsertOperandCastBefore(Instruction::BitCast, |
| 6230 | Op1, DestTy, SrcI); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6231 | return BinaryOperator::create( |
| 6232 | cast<BinaryOperator>(SrcI)->getOpcode(), Op0c, Op1c); |
| 6233 | } |
| 6234 | } |
| 6235 | break; |
| 6236 | |
| 6237 | case Instruction::Shl: |
| 6238 | // Allow changing the sign of the source operand. Do not allow |
| 6239 | // changing the size of the shift, UNLESS the shift amount is a |
| 6240 | // constant. We must not change variable sized shifts to a smaller |
| 6241 | // size, because it is undefined to shift more bits out than exist |
| 6242 | // in the value. |
| 6243 | if (DestBitSize == SrcBitSize || |
| 6244 | (DestBitSize < SrcBitSize && isa<Constant>(Op1))) { |
Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 6245 | Instruction::CastOps opcode = (DestBitSize == SrcBitSize ? |
| 6246 | Instruction::BitCast : Instruction::Trunc); |
| 6247 | Value *Op0c = InsertOperandCastBefore(opcode, Op0, DestTy, SrcI); |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6248 | Value *Op1c = InsertOperandCastBefore(opcode, Op1, DestTy, SrcI); |
Reid Spencer | 0d5f923 | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 6249 | return BinaryOperator::createShl(Op0c, Op1c); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6250 | } |
| 6251 | break; |
| 6252 | case Instruction::AShr: |
| 6253 | // If this is a signed shr, and if all bits shifted in are about to be |
| 6254 | // truncated off, turn it into an unsigned shr to allow greater |
| 6255 | // simplifications. |
| 6256 | if (DestBitSize < SrcBitSize && |
| 6257 | isa<ConstantInt>(Op1)) { |
| 6258 | unsigned ShiftAmt = cast<ConstantInt>(Op1)->getZExtValue(); |
| 6259 | if (SrcBitSize > ShiftAmt && SrcBitSize-ShiftAmt >= DestBitSize) { |
| 6260 | // Insert the new logical shift right. |
Reid Spencer | 0d5f923 | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 6261 | return BinaryOperator::createLShr(Op0, Op1); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6262 | } |
| 6263 | } |
| 6264 | break; |
| 6265 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6266 | case Instruction::ICmp: |
| 6267 | // If we are just checking for a icmp eq of a single bit and casting it |
| 6268 | // to an integer, then shift the bit to the appropriate place and then |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6269 | // cast to integer to avoid the comparison. |
| 6270 | if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) { |
| 6271 | uint64_t Op1CV = Op1C->getZExtValue(); |
| 6272 | // cast (X == 0) to int --> X^1 iff X has only the low bit set. |
| 6273 | // cast (X == 0) to int --> (X>>1)^1 iff X has only the 2nd bit set. |
| 6274 | // cast (X == 1) to int --> X iff X has only the low bit set. |
| 6275 | // cast (X == 2) to int --> X>>1 iff X has only the 2nd bit set. |
| 6276 | // cast (X != 0) to int --> X iff X has only the low bit set. |
| 6277 | // cast (X != 0) to int --> X>>1 iff X has only the 2nd bit set. |
| 6278 | // cast (X != 1) to int --> X^1 iff X has only the low bit set. |
| 6279 | // cast (X != 2) to int --> (X>>1)^1 iff X has only the 2nd bit set. |
| 6280 | if (Op1CV == 0 || isPowerOf2_64(Op1CV)) { |
| 6281 | // If Op1C some other power of two, convert: |
| 6282 | uint64_t KnownZero, KnownOne; |
Reid Spencer | a94d394 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 6283 | uint64_t TypeMask = Op1C->getType()->getBitMask(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6284 | ComputeMaskedBits(Op0, TypeMask, KnownZero, KnownOne); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6285 | |
| 6286 | // This only works for EQ and NE |
| 6287 | ICmpInst::Predicate pred = cast<ICmpInst>(SrcI)->getPredicate(); |
| 6288 | if (pred != ICmpInst::ICMP_NE && pred != ICmpInst::ICMP_EQ) |
| 6289 | break; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6290 | |
| 6291 | if (isPowerOf2_64(KnownZero^TypeMask)) { // Exactly 1 possible 1? |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6292 | bool isNE = pred == ICmpInst::ICMP_NE; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6293 | if (Op1CV && (Op1CV != (KnownZero^TypeMask))) { |
| 6294 | // (X&4) == 2 --> false |
| 6295 | // (X&4) != 2 --> true |
Reid Spencer | cddc9df | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 6296 | Constant *Res = ConstantInt::get(Type::Int1Ty, isNE); |
Reid Spencer | bb65ebf | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 6297 | Res = ConstantExpr::getZExt(Res, CI.getType()); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6298 | return ReplaceInstUsesWith(CI, Res); |
| 6299 | } |
| 6300 | |
| 6301 | unsigned ShiftAmt = Log2_64(KnownZero^TypeMask); |
| 6302 | Value *In = Op0; |
| 6303 | if (ShiftAmt) { |
| 6304 | // Perform a logical shr by shiftamt. |
| 6305 | // Insert the shift to put the result in the low bit. |
| 6306 | In = InsertNewInstBefore( |
Reid Spencer | 0d5f923 | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 6307 | BinaryOperator::createLShr(In, |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6308 | ConstantInt::get(In->getType(), ShiftAmt), |
| 6309 | In->getName()+".lobit"), CI); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6310 | } |
| 6311 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6312 | if ((Op1CV != 0) == isNE) { // Toggle the low bit. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6313 | Constant *One = ConstantInt::get(In->getType(), 1); |
| 6314 | In = BinaryOperator::createXor(In, One, "tmp"); |
| 6315 | InsertNewInstBefore(cast<Instruction>(In), CI); |
| 6316 | } |
| 6317 | |
| 6318 | if (CI.getType() == In->getType()) |
| 6319 | return ReplaceInstUsesWith(CI, In); |
| 6320 | else |
Reid Spencer | bb65ebf | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 6321 | return CastInst::createIntegerCast(In, CI.getType(), false/*ZExt*/); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6322 | } |
| 6323 | } |
| 6324 | } |
| 6325 | break; |
| 6326 | } |
| 6327 | return 0; |
| 6328 | } |
| 6329 | |
| 6330 | Instruction *InstCombiner::visitTrunc(CastInst &CI) { |
Chris Lattner | d747f01 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 6331 | if (Instruction *Result = commonIntCastTransforms(CI)) |
| 6332 | return Result; |
| 6333 | |
| 6334 | Value *Src = CI.getOperand(0); |
| 6335 | const Type *Ty = CI.getType(); |
| 6336 | unsigned DestBitWidth = Ty->getPrimitiveSizeInBits(); |
| 6337 | |
| 6338 | if (Instruction *SrcI = dyn_cast<Instruction>(Src)) { |
| 6339 | switch (SrcI->getOpcode()) { |
| 6340 | default: break; |
| 6341 | case Instruction::LShr: |
| 6342 | // We can shrink lshr to something smaller if we know the bits shifted in |
| 6343 | // are already zeros. |
| 6344 | if (ConstantInt *ShAmtV = dyn_cast<ConstantInt>(SrcI->getOperand(1))) { |
| 6345 | unsigned ShAmt = ShAmtV->getZExtValue(); |
| 6346 | |
| 6347 | // Get a mask for the bits shifting in. |
| 6348 | uint64_t Mask = (~0ULL >> (64-ShAmt)) << DestBitWidth; |
Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 6349 | Value* SrcIOp0 = SrcI->getOperand(0); |
| 6350 | if (SrcI->hasOneUse() && MaskedValueIsZero(SrcIOp0, Mask)) { |
Chris Lattner | d747f01 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 6351 | if (ShAmt >= DestBitWidth) // All zeros. |
| 6352 | return ReplaceInstUsesWith(CI, Constant::getNullValue(Ty)); |
| 6353 | |
| 6354 | // Okay, we can shrink this. Truncate the input, then return a new |
| 6355 | // shift. |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6356 | Value *V1 = InsertCastBefore(Instruction::Trunc, SrcIOp0, Ty, CI); |
| 6357 | Value *V2 = InsertCastBefore(Instruction::Trunc, SrcI->getOperand(1), |
| 6358 | Ty, CI); |
Reid Spencer | 0d5f923 | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 6359 | return BinaryOperator::createLShr(V1, V2); |
Chris Lattner | d747f01 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 6360 | } |
Chris Lattner | c209b58 | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 6361 | } else { // This is a variable shr. |
| 6362 | |
| 6363 | // Turn 'trunc (lshr X, Y) to bool' into '(X & (1 << Y)) != 0'. This is |
| 6364 | // more LLVM instructions, but allows '1 << Y' to be hoisted if |
| 6365 | // loop-invariant and CSE'd. |
Reid Spencer | 542964f | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 6366 | if (CI.getType() == Type::Int1Ty && SrcI->hasOneUse()) { |
Chris Lattner | c209b58 | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 6367 | Value *One = ConstantInt::get(SrcI->getType(), 1); |
| 6368 | |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6369 | Value *V = InsertNewInstBefore( |
Reid Spencer | 0d5f923 | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 6370 | BinaryOperator::createShl(One, SrcI->getOperand(1), |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6371 | "tmp"), CI); |
Chris Lattner | c209b58 | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 6372 | V = InsertNewInstBefore(BinaryOperator::createAnd(V, |
| 6373 | SrcI->getOperand(0), |
| 6374 | "tmp"), CI); |
| 6375 | Value *Zero = Constant::getNullValue(V->getType()); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6376 | return new ICmpInst(ICmpInst::ICMP_NE, V, Zero); |
Chris Lattner | c209b58 | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 6377 | } |
Chris Lattner | d747f01 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 6378 | } |
| 6379 | break; |
| 6380 | } |
| 6381 | } |
| 6382 | |
| 6383 | return 0; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6384 | } |
| 6385 | |
| 6386 | Instruction *InstCombiner::visitZExt(CastInst &CI) { |
| 6387 | // If one of the common conversion will work .. |
| 6388 | if (Instruction *Result = commonIntCastTransforms(CI)) |
| 6389 | return Result; |
| 6390 | |
| 6391 | Value *Src = CI.getOperand(0); |
| 6392 | |
| 6393 | // If this is a cast of a cast |
| 6394 | if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6395 | // If this is a TRUNC followed by a ZEXT then we are dealing with integral |
| 6396 | // types and if the sizes are just right we can convert this into a logical |
| 6397 | // 'and' which will be much cheaper than the pair of casts. |
| 6398 | if (isa<TruncInst>(CSrc)) { |
| 6399 | // Get the sizes of the types involved |
| 6400 | Value *A = CSrc->getOperand(0); |
| 6401 | unsigned SrcSize = A->getType()->getPrimitiveSizeInBits(); |
| 6402 | unsigned MidSize = CSrc->getType()->getPrimitiveSizeInBits(); |
| 6403 | unsigned DstSize = CI.getType()->getPrimitiveSizeInBits(); |
| 6404 | // If we're actually extending zero bits and the trunc is a no-op |
| 6405 | if (MidSize < DstSize && SrcSize == DstSize) { |
| 6406 | // Replace both of the casts with an And of the type mask. |
Reid Spencer | a94d394 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 6407 | uint64_t AndValue = cast<IntegerType>(CSrc->getType())->getBitMask(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6408 | Constant *AndConst = ConstantInt::get(A->getType(), AndValue); |
| 6409 | Instruction *And = |
| 6410 | BinaryOperator::createAnd(CSrc->getOperand(0), AndConst); |
| 6411 | // Unfortunately, if the type changed, we need to cast it back. |
| 6412 | if (And->getType() != CI.getType()) { |
| 6413 | And->setName(CSrc->getName()+".mask"); |
| 6414 | InsertNewInstBefore(And, CI); |
Reid Spencer | bb65ebf | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 6415 | And = CastInst::createIntegerCast(And, CI.getType(), false/*ZExt*/); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6416 | } |
| 6417 | return And; |
| 6418 | } |
| 6419 | } |
| 6420 | } |
| 6421 | |
| 6422 | return 0; |
| 6423 | } |
| 6424 | |
| 6425 | Instruction *InstCombiner::visitSExt(CastInst &CI) { |
| 6426 | return commonIntCastTransforms(CI); |
| 6427 | } |
| 6428 | |
| 6429 | Instruction *InstCombiner::visitFPTrunc(CastInst &CI) { |
| 6430 | return commonCastTransforms(CI); |
| 6431 | } |
| 6432 | |
| 6433 | Instruction *InstCombiner::visitFPExt(CastInst &CI) { |
| 6434 | return commonCastTransforms(CI); |
| 6435 | } |
| 6436 | |
| 6437 | Instruction *InstCombiner::visitFPToUI(CastInst &CI) { |
Reid Spencer | ad05ee9 | 2006-11-30 23:13:36 +0000 | [diff] [blame] | 6438 | return commonCastTransforms(CI); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6439 | } |
| 6440 | |
| 6441 | Instruction *InstCombiner::visitFPToSI(CastInst &CI) { |
Reid Spencer | ad05ee9 | 2006-11-30 23:13:36 +0000 | [diff] [blame] | 6442 | return commonCastTransforms(CI); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6443 | } |
| 6444 | |
| 6445 | Instruction *InstCombiner::visitUIToFP(CastInst &CI) { |
| 6446 | return commonCastTransforms(CI); |
| 6447 | } |
| 6448 | |
| 6449 | Instruction *InstCombiner::visitSIToFP(CastInst &CI) { |
| 6450 | return commonCastTransforms(CI); |
| 6451 | } |
| 6452 | |
| 6453 | Instruction *InstCombiner::visitPtrToInt(CastInst &CI) { |
Reid Spencer | ad05ee9 | 2006-11-30 23:13:36 +0000 | [diff] [blame] | 6454 | return commonCastTransforms(CI); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6455 | } |
| 6456 | |
| 6457 | Instruction *InstCombiner::visitIntToPtr(CastInst &CI) { |
| 6458 | return commonCastTransforms(CI); |
| 6459 | } |
| 6460 | |
| 6461 | Instruction *InstCombiner::visitBitCast(CastInst &CI) { |
| 6462 | |
| 6463 | // If the operands are integer typed then apply the integer transforms, |
| 6464 | // otherwise just apply the common ones. |
| 6465 | Value *Src = CI.getOperand(0); |
| 6466 | const Type *SrcTy = Src->getType(); |
| 6467 | const Type *DestTy = CI.getType(); |
| 6468 | |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 6469 | if (SrcTy->isInteger() && DestTy->isInteger()) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6470 | if (Instruction *Result = commonIntCastTransforms(CI)) |
| 6471 | return Result; |
| 6472 | } else { |
| 6473 | if (Instruction *Result = commonCastTransforms(CI)) |
| 6474 | return Result; |
| 6475 | } |
| 6476 | |
| 6477 | |
| 6478 | // Get rid of casts from one type to the same type. These are useless and can |
| 6479 | // be replaced by the operand. |
| 6480 | if (DestTy == Src->getType()) |
| 6481 | return ReplaceInstUsesWith(CI, Src); |
| 6482 | |
Chris Lattner | b19a5c6 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 6483 | // If the source and destination are pointers, and this cast is equivalent to |
| 6484 | // a getelementptr X, 0, 0, 0... turn it into the appropriate getelementptr. |
| 6485 | // This can enhance SROA and other transforms that want type-safe pointers. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6486 | if (const PointerType *DstPTy = dyn_cast<PointerType>(DestTy)) { |
| 6487 | if (const PointerType *SrcPTy = dyn_cast<PointerType>(SrcTy)) { |
| 6488 | const Type *DstElTy = DstPTy->getElementType(); |
| 6489 | const Type *SrcElTy = SrcPTy->getElementType(); |
Chris Lattner | b19a5c6 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 6490 | |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6491 | Constant *ZeroUInt = Constant::getNullValue(Type::Int32Ty); |
Chris Lattner | b19a5c6 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 6492 | unsigned NumZeros = 0; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6493 | while (SrcElTy != DstElTy && |
| 6494 | isa<CompositeType>(SrcElTy) && !isa<PointerType>(SrcElTy) && |
| 6495 | SrcElTy->getNumContainedTypes() /* not "{}" */) { |
| 6496 | SrcElTy = cast<CompositeType>(SrcElTy)->getTypeAtIndex(ZeroUInt); |
Chris Lattner | b19a5c6 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 6497 | ++NumZeros; |
| 6498 | } |
Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 6499 | |
Chris Lattner | b19a5c6 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 6500 | // If we found a path from the src to dest, create the getelementptr now. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6501 | if (SrcElTy == DstElTy) { |
Chris Lattner | 416a893 | 2007-01-31 20:08:52 +0000 | [diff] [blame] | 6502 | SmallVector<Value*, 8> Idxs(NumZeros+1, ZeroUInt); |
| 6503 | return new GetElementPtrInst(Src, &Idxs[0], Idxs.size()); |
Chris Lattner | b19a5c6 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 6504 | } |
| 6505 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6506 | } |
Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 6507 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6508 | if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(Src)) { |
| 6509 | if (SVI->hasOneUse()) { |
| 6510 | // Okay, we have (bitconvert (shuffle ..)). Check to see if this is |
| 6511 | // a bitconvert to a vector with the same # elts. |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 6512 | if (isa<VectorType>(DestTy) && |
| 6513 | cast<VectorType>(DestTy)->getNumElements() == |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6514 | SVI->getType()->getNumElements()) { |
| 6515 | CastInst *Tmp; |
| 6516 | // If either of the operands is a cast from CI.getType(), then |
| 6517 | // evaluating the shuffle in the casted destination's type will allow |
| 6518 | // us to eliminate at least one cast. |
| 6519 | if (((Tmp = dyn_cast<CastInst>(SVI->getOperand(0))) && |
| 6520 | Tmp->getOperand(0)->getType() == DestTy) || |
| 6521 | ((Tmp = dyn_cast<CastInst>(SVI->getOperand(1))) && |
| 6522 | Tmp->getOperand(0)->getType() == DestTy)) { |
Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 6523 | Value *LHS = InsertOperandCastBefore(Instruction::BitCast, |
| 6524 | SVI->getOperand(0), DestTy, &CI); |
| 6525 | Value *RHS = InsertOperandCastBefore(Instruction::BitCast, |
| 6526 | SVI->getOperand(1), DestTy, &CI); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6527 | // Return a new shuffle vector. Use the same element ID's, as we |
| 6528 | // know the vector types match #elts. |
| 6529 | return new ShuffleVectorInst(LHS, RHS, SVI->getOperand(2)); |
Chris Lattner | 99155be | 2006-05-25 23:24:33 +0000 | [diff] [blame] | 6530 | } |
| 6531 | } |
| 6532 | } |
| 6533 | } |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 6534 | return 0; |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 6535 | } |
| 6536 | |
Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6537 | /// GetSelectFoldableOperands - We want to turn code that looks like this: |
| 6538 | /// %C = or %A, %B |
| 6539 | /// %D = select %cond, %C, %A |
| 6540 | /// into: |
| 6541 | /// %C = select %cond, %B, 0 |
| 6542 | /// %D = or %A, %C |
| 6543 | /// |
| 6544 | /// Assuming that the specified instruction is an operand to the select, return |
| 6545 | /// a bitmask indicating which operands of this instruction are foldable if they |
| 6546 | /// equal the other incoming value of the select. |
| 6547 | /// |
| 6548 | static unsigned GetSelectFoldableOperands(Instruction *I) { |
| 6549 | switch (I->getOpcode()) { |
| 6550 | case Instruction::Add: |
| 6551 | case Instruction::Mul: |
| 6552 | case Instruction::And: |
| 6553 | case Instruction::Or: |
| 6554 | case Instruction::Xor: |
| 6555 | return 3; // Can fold through either operand. |
| 6556 | case Instruction::Sub: // Can only fold on the amount subtracted. |
| 6557 | case Instruction::Shl: // Can only fold on the shift amount. |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 6558 | case Instruction::LShr: |
| 6559 | case Instruction::AShr: |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6560 | return 1; |
Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6561 | default: |
| 6562 | return 0; // Cannot fold |
| 6563 | } |
| 6564 | } |
| 6565 | |
| 6566 | /// GetSelectFoldableConstant - For the same transformation as the previous |
| 6567 | /// function, return the identity constant that goes into the select. |
| 6568 | static Constant *GetSelectFoldableConstant(Instruction *I) { |
| 6569 | switch (I->getOpcode()) { |
| 6570 | default: assert(0 && "This cannot happen!"); abort(); |
| 6571 | case Instruction::Add: |
| 6572 | case Instruction::Sub: |
| 6573 | case Instruction::Or: |
| 6574 | case Instruction::Xor: |
Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6575 | case Instruction::Shl: |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 6576 | case Instruction::LShr: |
| 6577 | case Instruction::AShr: |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6578 | return Constant::getNullValue(I->getType()); |
Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6579 | case Instruction::And: |
| 6580 | return ConstantInt::getAllOnesValue(I->getType()); |
| 6581 | case Instruction::Mul: |
| 6582 | return ConstantInt::get(I->getType(), 1); |
| 6583 | } |
| 6584 | } |
| 6585 | |
Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 6586 | /// FoldSelectOpOp - Here we have (select c, TI, FI), and we know that TI and FI |
| 6587 | /// have the same opcode and only one use each. Try to simplify this. |
| 6588 | Instruction *InstCombiner::FoldSelectOpOp(SelectInst &SI, Instruction *TI, |
| 6589 | Instruction *FI) { |
| 6590 | if (TI->getNumOperands() == 1) { |
| 6591 | // If this is a non-volatile load or a cast from the same type, |
| 6592 | // merge. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6593 | if (TI->isCast()) { |
Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 6594 | if (TI->getOperand(0)->getType() != FI->getOperand(0)->getType()) |
| 6595 | return 0; |
| 6596 | } else { |
| 6597 | return 0; // unknown unary op. |
| 6598 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6599 | |
Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 6600 | // Fold this by inserting a select from the input values. |
| 6601 | SelectInst *NewSI = new SelectInst(SI.getCondition(), TI->getOperand(0), |
| 6602 | FI->getOperand(0), SI.getName()+".v"); |
| 6603 | InsertNewInstBefore(NewSI, SI); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6604 | return CastInst::create(Instruction::CastOps(TI->getOpcode()), NewSI, |
| 6605 | TI->getType()); |
Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 6606 | } |
| 6607 | |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6608 | // Only handle binary operators here. |
| 6609 | if (!isa<BinaryOperator>(TI)) |
Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 6610 | return 0; |
| 6611 | |
| 6612 | // Figure out if the operations have any operands in common. |
| 6613 | Value *MatchOp, *OtherOpT, *OtherOpF; |
| 6614 | bool MatchIsOpZero; |
| 6615 | if (TI->getOperand(0) == FI->getOperand(0)) { |
| 6616 | MatchOp = TI->getOperand(0); |
| 6617 | OtherOpT = TI->getOperand(1); |
| 6618 | OtherOpF = FI->getOperand(1); |
| 6619 | MatchIsOpZero = true; |
| 6620 | } else if (TI->getOperand(1) == FI->getOperand(1)) { |
| 6621 | MatchOp = TI->getOperand(1); |
| 6622 | OtherOpT = TI->getOperand(0); |
| 6623 | OtherOpF = FI->getOperand(0); |
| 6624 | MatchIsOpZero = false; |
| 6625 | } else if (!TI->isCommutative()) { |
| 6626 | return 0; |
| 6627 | } else if (TI->getOperand(0) == FI->getOperand(1)) { |
| 6628 | MatchOp = TI->getOperand(0); |
| 6629 | OtherOpT = TI->getOperand(1); |
| 6630 | OtherOpF = FI->getOperand(0); |
| 6631 | MatchIsOpZero = true; |
| 6632 | } else if (TI->getOperand(1) == FI->getOperand(0)) { |
| 6633 | MatchOp = TI->getOperand(1); |
| 6634 | OtherOpT = TI->getOperand(0); |
| 6635 | OtherOpF = FI->getOperand(1); |
| 6636 | MatchIsOpZero = true; |
| 6637 | } else { |
| 6638 | return 0; |
| 6639 | } |
| 6640 | |
| 6641 | // If we reach here, they do have operations in common. |
| 6642 | SelectInst *NewSI = new SelectInst(SI.getCondition(), OtherOpT, |
| 6643 | OtherOpF, SI.getName()+".v"); |
| 6644 | InsertNewInstBefore(NewSI, SI); |
| 6645 | |
| 6646 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TI)) { |
| 6647 | if (MatchIsOpZero) |
| 6648 | return BinaryOperator::create(BO->getOpcode(), MatchOp, NewSI); |
| 6649 | else |
| 6650 | return BinaryOperator::create(BO->getOpcode(), NewSI, MatchOp); |
Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 6651 | } |
Reid Spencer | 2f34b98 | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6652 | assert(0 && "Shouldn't get here"); |
| 6653 | return 0; |
Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 6654 | } |
| 6655 | |
Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 6656 | Instruction *InstCombiner::visitSelectInst(SelectInst &SI) { |
Chris Lattner | 533bc49 | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 6657 | Value *CondVal = SI.getCondition(); |
| 6658 | Value *TrueVal = SI.getTrueValue(); |
| 6659 | Value *FalseVal = SI.getFalseValue(); |
| 6660 | |
| 6661 | // select true, X, Y -> X |
| 6662 | // select false, X, Y -> Y |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6663 | if (ConstantInt *C = dyn_cast<ConstantInt>(CondVal)) |
Reid Spencer | cddc9df | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 6664 | return ReplaceInstUsesWith(SI, C->getZExtValue() ? TrueVal : FalseVal); |
Chris Lattner | 533bc49 | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 6665 | |
| 6666 | // select C, X, X -> X |
| 6667 | if (TrueVal == FalseVal) |
| 6668 | return ReplaceInstUsesWith(SI, TrueVal); |
| 6669 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 6670 | if (isa<UndefValue>(TrueVal)) // select C, undef, X -> X |
| 6671 | return ReplaceInstUsesWith(SI, FalseVal); |
| 6672 | if (isa<UndefValue>(FalseVal)) // select C, X, undef -> X |
| 6673 | return ReplaceInstUsesWith(SI, TrueVal); |
| 6674 | if (isa<UndefValue>(CondVal)) { // select undef, X, Y -> X or Y |
| 6675 | if (isa<Constant>(TrueVal)) |
| 6676 | return ReplaceInstUsesWith(SI, TrueVal); |
| 6677 | else |
| 6678 | return ReplaceInstUsesWith(SI, FalseVal); |
| 6679 | } |
| 6680 | |
Reid Spencer | 542964f | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 6681 | if (SI.getType() == Type::Int1Ty) { |
Reid Spencer | 7a9c62b | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 6682 | if (ConstantInt *C = dyn_cast<ConstantInt>(TrueVal)) { |
Reid Spencer | cddc9df | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 6683 | if (C->getZExtValue()) { |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 6684 | // Change: A = select B, true, C --> A = or B, C |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 6685 | return BinaryOperator::createOr(CondVal, FalseVal); |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 6686 | } else { |
| 6687 | // Change: A = select B, false, C --> A = and !B, C |
| 6688 | Value *NotCond = |
| 6689 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
| 6690 | "not."+CondVal->getName()), SI); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 6691 | return BinaryOperator::createAnd(NotCond, FalseVal); |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 6692 | } |
Reid Spencer | 7a9c62b | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 6693 | } else if (ConstantInt *C = dyn_cast<ConstantInt>(FalseVal)) { |
Reid Spencer | cddc9df | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 6694 | if (C->getZExtValue() == false) { |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 6695 | // Change: A = select B, C, false --> A = and B, C |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 6696 | return BinaryOperator::createAnd(CondVal, TrueVal); |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 6697 | } else { |
| 6698 | // Change: A = select B, C, true --> A = or !B, C |
| 6699 | Value *NotCond = |
| 6700 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
| 6701 | "not."+CondVal->getName()), SI); |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 6702 | return BinaryOperator::createOr(NotCond, TrueVal); |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 6703 | } |
| 6704 | } |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6705 | } |
Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 6706 | |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 6707 | // Selecting between two integer constants? |
| 6708 | if (ConstantInt *TrueValC = dyn_cast<ConstantInt>(TrueVal)) |
| 6709 | if (ConstantInt *FalseValC = dyn_cast<ConstantInt>(FalseVal)) { |
| 6710 | // select C, 1, 0 -> cast C to int |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6711 | if (FalseValC->isNullValue() && TrueValC->getZExtValue() == 1) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6712 | return CastInst::create(Instruction::ZExt, CondVal, SI.getType()); |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6713 | } else if (TrueValC->isNullValue() && FalseValC->getZExtValue() == 1) { |
Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 6714 | // select C, 0, 1 -> cast !C to int |
| 6715 | Value *NotCond = |
| 6716 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
Chris Lattner | cf7baf3 | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 6717 | "not."+CondVal->getName()), SI); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6718 | return CastInst::create(Instruction::ZExt, NotCond, SI.getType()); |
Chris Lattner | cf7baf3 | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 6719 | } |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 6720 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6721 | if (ICmpInst *IC = dyn_cast<ICmpInst>(SI.getCondition())) { |
Chris Lattner | 380c7e9 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 6722 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6723 | // (x <s 0) ? -1 : 0 -> ashr x, 31 |
| 6724 | // (x >u 2147483647) ? -1 : 0 -> ashr x, 31 |
Chris Lattner | 380c7e9 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 6725 | if (TrueValC->isAllOnesValue() && FalseValC->isNullValue()) |
| 6726 | if (ConstantInt *CmpCst = dyn_cast<ConstantInt>(IC->getOperand(1))) { |
| 6727 | bool CanXForm = false; |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6728 | if (IC->isSignedPredicate()) |
Chris Lattner | 380c7e9 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 6729 | CanXForm = CmpCst->isNullValue() && |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6730 | IC->getPredicate() == ICmpInst::ICMP_SLT; |
Chris Lattner | 380c7e9 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 6731 | else { |
| 6732 | unsigned Bits = CmpCst->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6733 | CanXForm = (CmpCst->getZExtValue() == ~0ULL >> (64-Bits+1)) && |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6734 | IC->getPredicate() == ICmpInst::ICMP_UGT; |
Chris Lattner | 380c7e9 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 6735 | } |
| 6736 | |
| 6737 | if (CanXForm) { |
| 6738 | // The comparison constant and the result are not neccessarily the |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6739 | // same width. Make an all-ones value by inserting a AShr. |
Chris Lattner | 380c7e9 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 6740 | Value *X = IC->getOperand(0); |
Chris Lattner | 380c7e9 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 6741 | unsigned Bits = X->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6742 | Constant *ShAmt = ConstantInt::get(X->getType(), Bits-1); |
| 6743 | Instruction *SRA = BinaryOperator::create(Instruction::AShr, X, |
| 6744 | ShAmt, "ones"); |
Chris Lattner | 380c7e9 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 6745 | InsertNewInstBefore(SRA, SI); |
| 6746 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6747 | // Finally, convert to the type of the select RHS. We figure out |
| 6748 | // if this requires a SExt, Trunc or BitCast based on the sizes. |
| 6749 | Instruction::CastOps opc = Instruction::BitCast; |
| 6750 | unsigned SRASize = SRA->getType()->getPrimitiveSizeInBits(); |
| 6751 | unsigned SISize = SI.getType()->getPrimitiveSizeInBits(); |
| 6752 | if (SRASize < SISize) |
| 6753 | opc = Instruction::SExt; |
| 6754 | else if (SRASize > SISize) |
| 6755 | opc = Instruction::Trunc; |
| 6756 | return CastInst::create(opc, SRA, SI.getType()); |
Chris Lattner | 380c7e9 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 6757 | } |
| 6758 | } |
| 6759 | |
| 6760 | |
| 6761 | // If one of the constants is zero (we know they can't both be) and we |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6762 | // have a fcmp instruction with zero, and we have an 'and' with the |
Chris Lattner | 380c7e9 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 6763 | // non-constant value, eliminate this whole mess. This corresponds to |
| 6764 | // cases like this: ((X & 27) ? 27 : 0) |
| 6765 | if (TrueValC->isNullValue() || FalseValC->isNullValue()) |
Chris Lattner | b3f24c9 | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 6766 | if (IC->isEquality() && isa<ConstantInt>(IC->getOperand(1)) && |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 6767 | cast<Constant>(IC->getOperand(1))->isNullValue()) |
| 6768 | if (Instruction *ICA = dyn_cast<Instruction>(IC->getOperand(0))) |
| 6769 | if (ICA->getOpcode() == Instruction::And && |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6770 | isa<ConstantInt>(ICA->getOperand(1)) && |
| 6771 | (ICA->getOperand(1) == TrueValC || |
| 6772 | ICA->getOperand(1) == FalseValC) && |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 6773 | isOneBitSet(cast<ConstantInt>(ICA->getOperand(1)))) { |
| 6774 | // Okay, now we know that everything is set up, we just don't |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6775 | // know whether we have a icmp_ne or icmp_eq and whether the |
| 6776 | // true or false val is the zero. |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 6777 | bool ShouldNotVal = !TrueValC->isNullValue(); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6778 | ShouldNotVal ^= IC->getPredicate() == ICmpInst::ICMP_NE; |
Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 6779 | Value *V = ICA; |
| 6780 | if (ShouldNotVal) |
| 6781 | V = InsertNewInstBefore(BinaryOperator::create( |
| 6782 | Instruction::Xor, V, ICA->getOperand(1)), SI); |
| 6783 | return ReplaceInstUsesWith(SI, V); |
| 6784 | } |
Chris Lattner | 380c7e9 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 6785 | } |
Chris Lattner | 533bc49 | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 6786 | } |
Chris Lattner | 623fba1 | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 6787 | |
| 6788 | // See if we are selecting two values based on a comparison of the two values. |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6789 | if (FCmpInst *FCI = dyn_cast<FCmpInst>(CondVal)) { |
| 6790 | if (FCI->getOperand(0) == TrueVal && FCI->getOperand(1) == FalseVal) { |
Chris Lattner | 623fba1 | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 6791 | // Transform (X == Y) ? X : Y -> Y |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6792 | if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) |
Chris Lattner | 623fba1 | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 6793 | return ReplaceInstUsesWith(SI, FalseVal); |
| 6794 | // Transform (X != Y) ? X : Y -> X |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6795 | if (FCI->getPredicate() == FCmpInst::FCMP_ONE) |
Chris Lattner | 623fba1 | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 6796 | return ReplaceInstUsesWith(SI, TrueVal); |
| 6797 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 6798 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6799 | } else if (FCI->getOperand(0) == FalseVal && FCI->getOperand(1) == TrueVal){ |
Chris Lattner | 623fba1 | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 6800 | // Transform (X == Y) ? Y : X -> X |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6801 | if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) |
Chris Lattner | 24cf020 | 2004-04-11 01:39:19 +0000 | [diff] [blame] | 6802 | return ReplaceInstUsesWith(SI, FalseVal); |
Chris Lattner | 623fba1 | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 6803 | // Transform (X != Y) ? Y : X -> Y |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6804 | if (FCI->getPredicate() == FCmpInst::FCMP_ONE) |
| 6805 | return ReplaceInstUsesWith(SI, TrueVal); |
| 6806 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 6807 | } |
| 6808 | } |
| 6809 | |
| 6810 | // See if we are selecting two values based on a comparison of the two values. |
| 6811 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(CondVal)) { |
| 6812 | if (ICI->getOperand(0) == TrueVal && ICI->getOperand(1) == FalseVal) { |
| 6813 | // Transform (X == Y) ? X : Y -> Y |
| 6814 | if (ICI->getPredicate() == ICmpInst::ICMP_EQ) |
| 6815 | return ReplaceInstUsesWith(SI, FalseVal); |
| 6816 | // Transform (X != Y) ? X : Y -> X |
| 6817 | if (ICI->getPredicate() == ICmpInst::ICMP_NE) |
| 6818 | return ReplaceInstUsesWith(SI, TrueVal); |
| 6819 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 6820 | |
| 6821 | } else if (ICI->getOperand(0) == FalseVal && ICI->getOperand(1) == TrueVal){ |
| 6822 | // Transform (X == Y) ? Y : X -> X |
| 6823 | if (ICI->getPredicate() == ICmpInst::ICMP_EQ) |
| 6824 | return ReplaceInstUsesWith(SI, FalseVal); |
| 6825 | // Transform (X != Y) ? Y : X -> Y |
| 6826 | if (ICI->getPredicate() == ICmpInst::ICMP_NE) |
Chris Lattner | 24cf020 | 2004-04-11 01:39:19 +0000 | [diff] [blame] | 6827 | return ReplaceInstUsesWith(SI, TrueVal); |
Chris Lattner | 623fba1 | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 6828 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 6829 | } |
| 6830 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6831 | |
Chris Lattner | a04c904 | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 6832 | if (Instruction *TI = dyn_cast<Instruction>(TrueVal)) |
| 6833 | if (Instruction *FI = dyn_cast<Instruction>(FalseVal)) |
| 6834 | if (TI->hasOneUse() && FI->hasOneUse()) { |
Chris Lattner | a04c904 | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 6835 | Instruction *AddOp = 0, *SubOp = 0; |
| 6836 | |
Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 6837 | // Turn (select C, (op X, Y), (op X, Z)) -> (op X, (select C, Y, Z)) |
| 6838 | if (TI->getOpcode() == FI->getOpcode()) |
| 6839 | if (Instruction *IV = FoldSelectOpOp(SI, TI, FI)) |
| 6840 | return IV; |
| 6841 | |
| 6842 | // Turn select C, (X+Y), (X-Y) --> (X+(select C, Y, (-Y))). This is |
| 6843 | // even legal for FP. |
Chris Lattner | a04c904 | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 6844 | if (TI->getOpcode() == Instruction::Sub && |
| 6845 | FI->getOpcode() == Instruction::Add) { |
| 6846 | AddOp = FI; SubOp = TI; |
| 6847 | } else if (FI->getOpcode() == Instruction::Sub && |
| 6848 | TI->getOpcode() == Instruction::Add) { |
| 6849 | AddOp = TI; SubOp = FI; |
| 6850 | } |
| 6851 | |
| 6852 | if (AddOp) { |
| 6853 | Value *OtherAddOp = 0; |
| 6854 | if (SubOp->getOperand(0) == AddOp->getOperand(0)) { |
| 6855 | OtherAddOp = AddOp->getOperand(1); |
| 6856 | } else if (SubOp->getOperand(0) == AddOp->getOperand(1)) { |
| 6857 | OtherAddOp = AddOp->getOperand(0); |
| 6858 | } |
| 6859 | |
| 6860 | if (OtherAddOp) { |
Chris Lattner | b580d26 | 2006-02-24 18:05:58 +0000 | [diff] [blame] | 6861 | // So at this point we know we have (Y -> OtherAddOp): |
| 6862 | // select C, (add X, Y), (sub X, Z) |
| 6863 | Value *NegVal; // Compute -Z |
| 6864 | if (Constant *C = dyn_cast<Constant>(SubOp->getOperand(1))) { |
| 6865 | NegVal = ConstantExpr::getNeg(C); |
| 6866 | } else { |
| 6867 | NegVal = InsertNewInstBefore( |
| 6868 | BinaryOperator::createNeg(SubOp->getOperand(1), "tmp"), SI); |
Chris Lattner | a04c904 | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 6869 | } |
Chris Lattner | b580d26 | 2006-02-24 18:05:58 +0000 | [diff] [blame] | 6870 | |
| 6871 | Value *NewTrueOp = OtherAddOp; |
| 6872 | Value *NewFalseOp = NegVal; |
| 6873 | if (AddOp != TI) |
| 6874 | std::swap(NewTrueOp, NewFalseOp); |
| 6875 | Instruction *NewSel = |
| 6876 | new SelectInst(CondVal, NewTrueOp,NewFalseOp,SI.getName()+".p"); |
| 6877 | |
| 6878 | NewSel = InsertNewInstBefore(NewSel, SI); |
| 6879 | return BinaryOperator::createAdd(SubOp->getOperand(0), NewSel); |
Chris Lattner | a04c904 | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 6880 | } |
| 6881 | } |
| 6882 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6883 | |
Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6884 | // See if we can fold the select into one of our operands. |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 6885 | if (SI.getType()->isInteger()) { |
Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6886 | // See the comment above GetSelectFoldableOperands for a description of the |
| 6887 | // transformation we are doing here. |
| 6888 | if (Instruction *TVI = dyn_cast<Instruction>(TrueVal)) |
| 6889 | if (TVI->hasOneUse() && TVI->getNumOperands() == 2 && |
| 6890 | !isa<Constant>(FalseVal)) |
| 6891 | if (unsigned SFO = GetSelectFoldableOperands(TVI)) { |
| 6892 | unsigned OpToFold = 0; |
| 6893 | if ((SFO & 1) && FalseVal == TVI->getOperand(0)) { |
| 6894 | OpToFold = 1; |
| 6895 | } else if ((SFO & 2) && FalseVal == TVI->getOperand(1)) { |
| 6896 | OpToFold = 2; |
| 6897 | } |
| 6898 | |
| 6899 | if (OpToFold) { |
| 6900 | Constant *C = GetSelectFoldableConstant(TVI); |
Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6901 | Instruction *NewSel = |
Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6902 | new SelectInst(SI.getCondition(), TVI->getOperand(2-OpToFold), C); |
Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6903 | InsertNewInstBefore(NewSel, SI); |
Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6904 | NewSel->takeName(TVI); |
Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6905 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TVI)) |
| 6906 | return BinaryOperator::create(BO->getOpcode(), FalseVal, NewSel); |
Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6907 | else { |
| 6908 | assert(0 && "Unknown instruction!!"); |
| 6909 | } |
| 6910 | } |
| 6911 | } |
Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 6912 | |
Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6913 | if (Instruction *FVI = dyn_cast<Instruction>(FalseVal)) |
| 6914 | if (FVI->hasOneUse() && FVI->getNumOperands() == 2 && |
| 6915 | !isa<Constant>(TrueVal)) |
| 6916 | if (unsigned SFO = GetSelectFoldableOperands(FVI)) { |
| 6917 | unsigned OpToFold = 0; |
| 6918 | if ((SFO & 1) && TrueVal == FVI->getOperand(0)) { |
| 6919 | OpToFold = 1; |
| 6920 | } else if ((SFO & 2) && TrueVal == FVI->getOperand(1)) { |
| 6921 | OpToFold = 2; |
| 6922 | } |
| 6923 | |
| 6924 | if (OpToFold) { |
| 6925 | Constant *C = GetSelectFoldableConstant(FVI); |
Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6926 | Instruction *NewSel = |
Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6927 | new SelectInst(SI.getCondition(), C, FVI->getOperand(2-OpToFold)); |
Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6928 | InsertNewInstBefore(NewSel, SI); |
Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6929 | NewSel->takeName(FVI); |
Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6930 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(FVI)) |
| 6931 | return BinaryOperator::create(BO->getOpcode(), TrueVal, NewSel); |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6932 | else |
Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6933 | assert(0 && "Unknown instruction!!"); |
Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6934 | } |
| 6935 | } |
| 6936 | } |
Chris Lattner | d6f636a | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 6937 | |
| 6938 | if (BinaryOperator::isNot(CondVal)) { |
| 6939 | SI.setOperand(0, BinaryOperator::getNotArgument(CondVal)); |
| 6940 | SI.setOperand(1, FalseVal); |
| 6941 | SI.setOperand(2, TrueVal); |
| 6942 | return &SI; |
| 6943 | } |
| 6944 | |
Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 6945 | return 0; |
| 6946 | } |
| 6947 | |
Chris Lattner | 82f2ef2 | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 6948 | /// GetKnownAlignment - If the specified pointer has an alignment that we can |
| 6949 | /// determine, return it, otherwise return 0. |
| 6950 | static unsigned GetKnownAlignment(Value *V, TargetData *TD) { |
| 6951 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) { |
| 6952 | unsigned Align = GV->getAlignment(); |
| 6953 | if (Align == 0 && TD) |
Chris Lattner | 945e437 | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 6954 | Align = TD->getPrefTypeAlignment(GV->getType()->getElementType()); |
Chris Lattner | 82f2ef2 | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 6955 | return Align; |
| 6956 | } else if (AllocationInst *AI = dyn_cast<AllocationInst>(V)) { |
| 6957 | unsigned Align = AI->getAlignment(); |
| 6958 | if (Align == 0 && TD) { |
| 6959 | if (isa<AllocaInst>(AI)) |
Chris Lattner | 945e437 | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 6960 | Align = TD->getPrefTypeAlignment(AI->getType()->getElementType()); |
Chris Lattner | 82f2ef2 | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 6961 | else if (isa<MallocInst>(AI)) { |
| 6962 | // Malloc returns maximally aligned memory. |
Chris Lattner | 945e437 | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 6963 | Align = TD->getABITypeAlignment(AI->getType()->getElementType()); |
Chris Lattner | 50ee0e4 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 6964 | Align = |
| 6965 | std::max(Align, |
Chris Lattner | 945e437 | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 6966 | (unsigned)TD->getABITypeAlignment(Type::DoubleTy)); |
Chris Lattner | 50ee0e4 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 6967 | Align = |
| 6968 | std::max(Align, |
Chris Lattner | 945e437 | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 6969 | (unsigned)TD->getABITypeAlignment(Type::Int64Ty)); |
Chris Lattner | 82f2ef2 | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 6970 | } |
| 6971 | } |
| 6972 | return Align; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6973 | } else if (isa<BitCastInst>(V) || |
Chris Lattner | 53ef5a0 | 2006-03-07 01:28:57 +0000 | [diff] [blame] | 6974 | (isa<ConstantExpr>(V) && |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6975 | cast<ConstantExpr>(V)->getOpcode() == Instruction::BitCast)) { |
Chris Lattner | 53ef5a0 | 2006-03-07 01:28:57 +0000 | [diff] [blame] | 6976 | User *CI = cast<User>(V); |
Chris Lattner | 82f2ef2 | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 6977 | if (isa<PointerType>(CI->getOperand(0)->getType())) |
| 6978 | return GetKnownAlignment(CI->getOperand(0), TD); |
| 6979 | return 0; |
Chris Lattner | 53ef5a0 | 2006-03-07 01:28:57 +0000 | [diff] [blame] | 6980 | } else if (isa<GetElementPtrInst>(V) || |
| 6981 | (isa<ConstantExpr>(V) && |
| 6982 | cast<ConstantExpr>(V)->getOpcode()==Instruction::GetElementPtr)) { |
| 6983 | User *GEPI = cast<User>(V); |
Chris Lattner | 82f2ef2 | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 6984 | unsigned BaseAlignment = GetKnownAlignment(GEPI->getOperand(0), TD); |
| 6985 | if (BaseAlignment == 0) return 0; |
| 6986 | |
| 6987 | // If all indexes are zero, it is just the alignment of the base pointer. |
| 6988 | bool AllZeroOperands = true; |
| 6989 | for (unsigned i = 1, e = GEPI->getNumOperands(); i != e; ++i) |
| 6990 | if (!isa<Constant>(GEPI->getOperand(i)) || |
| 6991 | !cast<Constant>(GEPI->getOperand(i))->isNullValue()) { |
| 6992 | AllZeroOperands = false; |
| 6993 | break; |
| 6994 | } |
| 6995 | if (AllZeroOperands) |
| 6996 | return BaseAlignment; |
| 6997 | |
| 6998 | // Otherwise, if the base alignment is >= the alignment we expect for the |
| 6999 | // base pointer type, then we know that the resultant pointer is aligned at |
| 7000 | // least as much as its type requires. |
| 7001 | if (!TD) return 0; |
| 7002 | |
| 7003 | const Type *BasePtrTy = GEPI->getOperand(0)->getType(); |
Chris Lattner | 50ee0e4 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 7004 | const PointerType *PtrTy = cast<PointerType>(BasePtrTy); |
Chris Lattner | 945e437 | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 7005 | if (TD->getABITypeAlignment(PtrTy->getElementType()) |
Chris Lattner | 53ef5a0 | 2006-03-07 01:28:57 +0000 | [diff] [blame] | 7006 | <= BaseAlignment) { |
| 7007 | const Type *GEPTy = GEPI->getType(); |
Chris Lattner | 50ee0e4 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 7008 | const PointerType *GEPPtrTy = cast<PointerType>(GEPTy); |
Chris Lattner | 945e437 | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 7009 | return TD->getABITypeAlignment(GEPPtrTy->getElementType()); |
Chris Lattner | 53ef5a0 | 2006-03-07 01:28:57 +0000 | [diff] [blame] | 7010 | } |
Chris Lattner | 82f2ef2 | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7011 | return 0; |
| 7012 | } |
| 7013 | return 0; |
| 7014 | } |
| 7015 | |
Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 7016 | |
Chris Lattner | c66b223 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 7017 | /// visitCallInst - CallInst simplification. This mostly only handles folding |
| 7018 | /// of intrinsic instructions. For normal calls, it allows visitCallSite to do |
| 7019 | /// the heavy lifting. |
| 7020 | /// |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7021 | Instruction *InstCombiner::visitCallInst(CallInst &CI) { |
Chris Lattner | c66b223 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 7022 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(&CI); |
| 7023 | if (!II) return visitCallSite(&CI); |
| 7024 | |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 7025 | // Intrinsics cannot occur in an invoke, so handle them here instead of in |
| 7026 | // visitCallSite. |
Chris Lattner | c66b223 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 7027 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(II)) { |
Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 7028 | bool Changed = false; |
| 7029 | |
| 7030 | // memmove/cpy/set of zero bytes is a noop. |
| 7031 | if (Constant *NumBytes = dyn_cast<Constant>(MI->getLength())) { |
| 7032 | if (NumBytes->isNullValue()) return EraseInstFromFunction(CI); |
| 7033 | |
Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 7034 | if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes)) |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7035 | if (CI->getZExtValue() == 1) { |
Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 7036 | // Replace the instruction with just byte operations. We would |
| 7037 | // transform other cases to loads/stores, but we don't know if |
| 7038 | // alignment is sufficient. |
| 7039 | } |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 7040 | } |
| 7041 | |
Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 7042 | // If we have a memmove and the source operation is a constant global, |
| 7043 | // then the source and dest pointers can't alias, so we can change this |
| 7044 | // into a call to memcpy. |
Chris Lattner | 82f2ef2 | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7045 | if (MemMoveInst *MMI = dyn_cast<MemMoveInst>(II)) { |
Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 7046 | if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource())) |
| 7047 | if (GVSrc->isConstant()) { |
| 7048 | Module *M = CI.getParent()->getParent()->getParent(); |
Chris Lattner | 681ef2f | 2006-03-03 01:34:17 +0000 | [diff] [blame] | 7049 | const char *Name; |
Andrew Lenharth | 0ebb0b0 | 2006-11-03 22:45:50 +0000 | [diff] [blame] | 7050 | if (CI.getCalledFunction()->getFunctionType()->getParamType(2) == |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 7051 | Type::Int32Ty) |
Chris Lattner | 681ef2f | 2006-03-03 01:34:17 +0000 | [diff] [blame] | 7052 | Name = "llvm.memcpy.i32"; |
| 7053 | else |
| 7054 | Name = "llvm.memcpy.i64"; |
Chris Lattner | fbc524f | 2007-01-07 06:58:05 +0000 | [diff] [blame] | 7055 | Constant *MemCpy = M->getOrInsertFunction(Name, |
Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 7056 | CI.getCalledFunction()->getFunctionType()); |
| 7057 | CI.setOperand(0, MemCpy); |
| 7058 | Changed = true; |
| 7059 | } |
Chris Lattner | 82f2ef2 | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7060 | } |
Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 7061 | |
Chris Lattner | 82f2ef2 | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7062 | // If we can determine a pointer alignment that is bigger than currently |
| 7063 | // set, update the alignment. |
| 7064 | if (isa<MemCpyInst>(MI) || isa<MemMoveInst>(MI)) { |
| 7065 | unsigned Alignment1 = GetKnownAlignment(MI->getOperand(1), TD); |
| 7066 | unsigned Alignment2 = GetKnownAlignment(MI->getOperand(2), TD); |
| 7067 | unsigned Align = std::min(Alignment1, Alignment2); |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7068 | if (MI->getAlignment()->getZExtValue() < Align) { |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 7069 | MI->setAlignment(ConstantInt::get(Type::Int32Ty, Align)); |
Chris Lattner | 82f2ef2 | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7070 | Changed = true; |
| 7071 | } |
| 7072 | } else if (isa<MemSetInst>(MI)) { |
| 7073 | unsigned Alignment = GetKnownAlignment(MI->getDest(), TD); |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7074 | if (MI->getAlignment()->getZExtValue() < Alignment) { |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 7075 | MI->setAlignment(ConstantInt::get(Type::Int32Ty, Alignment)); |
Chris Lattner | 82f2ef2 | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7076 | Changed = true; |
| 7077 | } |
| 7078 | } |
| 7079 | |
Chris Lattner | c66b223 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 7080 | if (Changed) return II; |
Chris Lattner | 503221f | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 7081 | } else { |
| 7082 | switch (II->getIntrinsicID()) { |
| 7083 | default: break; |
Chris Lattner | f42d0ae | 2006-04-02 05:30:25 +0000 | [diff] [blame] | 7084 | case Intrinsic::ppc_altivec_lvx: |
| 7085 | case Intrinsic::ppc_altivec_lvxl: |
Chris Lattner | 36dd7c9 | 2006-04-17 22:26:56 +0000 | [diff] [blame] | 7086 | case Intrinsic::x86_sse_loadu_ps: |
| 7087 | case Intrinsic::x86_sse2_loadu_pd: |
| 7088 | case Intrinsic::x86_sse2_loadu_dq: |
| 7089 | // Turn PPC lvx -> load if the pointer is known aligned. |
| 7090 | // Turn X86 loadups -> load if the pointer is known aligned. |
Chris Lattner | f42d0ae | 2006-04-02 05:30:25 +0000 | [diff] [blame] | 7091 | if (GetKnownAlignment(II->getOperand(1), TD) >= 16) { |
Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7092 | Value *Ptr = InsertCastBefore(Instruction::BitCast, II->getOperand(1), |
Chris Lattner | e79d249 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 7093 | PointerType::get(II->getType()), CI); |
Chris Lattner | f42d0ae | 2006-04-02 05:30:25 +0000 | [diff] [blame] | 7094 | return new LoadInst(Ptr); |
| 7095 | } |
| 7096 | break; |
| 7097 | case Intrinsic::ppc_altivec_stvx: |
| 7098 | case Intrinsic::ppc_altivec_stvxl: |
| 7099 | // Turn stvx -> store if the pointer is known aligned. |
| 7100 | if (GetKnownAlignment(II->getOperand(2), TD) >= 16) { |
Chris Lattner | e79d249 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 7101 | const Type *OpPtrTy = PointerType::get(II->getOperand(1)->getType()); |
Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7102 | Value *Ptr = InsertCastBefore(Instruction::BitCast, II->getOperand(2), |
| 7103 | OpPtrTy, CI); |
Chris Lattner | f42d0ae | 2006-04-02 05:30:25 +0000 | [diff] [blame] | 7104 | return new StoreInst(II->getOperand(1), Ptr); |
| 7105 | } |
| 7106 | break; |
Chris Lattner | 36dd7c9 | 2006-04-17 22:26:56 +0000 | [diff] [blame] | 7107 | case Intrinsic::x86_sse_storeu_ps: |
| 7108 | case Intrinsic::x86_sse2_storeu_pd: |
| 7109 | case Intrinsic::x86_sse2_storeu_dq: |
| 7110 | case Intrinsic::x86_sse2_storel_dq: |
| 7111 | // Turn X86 storeu -> store if the pointer is known aligned. |
| 7112 | if (GetKnownAlignment(II->getOperand(1), TD) >= 16) { |
| 7113 | const Type *OpPtrTy = PointerType::get(II->getOperand(2)->getType()); |
Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7114 | Value *Ptr = InsertCastBefore(Instruction::BitCast, II->getOperand(1), |
| 7115 | OpPtrTy, CI); |
Chris Lattner | 36dd7c9 | 2006-04-17 22:26:56 +0000 | [diff] [blame] | 7116 | return new StoreInst(II->getOperand(2), Ptr); |
| 7117 | } |
| 7118 | break; |
Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 7119 | |
| 7120 | case Intrinsic::x86_sse_cvttss2si: { |
| 7121 | // These intrinsics only demands the 0th element of its input vector. If |
| 7122 | // we can simplify the input based on that, do so now. |
| 7123 | uint64_t UndefElts; |
| 7124 | if (Value *V = SimplifyDemandedVectorElts(II->getOperand(1), 1, |
| 7125 | UndefElts)) { |
| 7126 | II->setOperand(1, V); |
| 7127 | return II; |
| 7128 | } |
| 7129 | break; |
| 7130 | } |
| 7131 | |
Chris Lattner | e79d249 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 7132 | case Intrinsic::ppc_altivec_vperm: |
| 7133 | // Turn vperm(V1,V2,mask) -> shuffle(V1,V2,mask) if mask is a constant. |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 7134 | if (ConstantVector *Mask = dyn_cast<ConstantVector>(II->getOperand(3))) { |
Chris Lattner | e79d249 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 7135 | assert(Mask->getNumOperands() == 16 && "Bad type for intrinsic!"); |
| 7136 | |
| 7137 | // Check that all of the elements are integer constants or undefs. |
| 7138 | bool AllEltsOk = true; |
| 7139 | for (unsigned i = 0; i != 16; ++i) { |
| 7140 | if (!isa<ConstantInt>(Mask->getOperand(i)) && |
| 7141 | !isa<UndefValue>(Mask->getOperand(i))) { |
| 7142 | AllEltsOk = false; |
| 7143 | break; |
| 7144 | } |
| 7145 | } |
| 7146 | |
| 7147 | if (AllEltsOk) { |
| 7148 | // Cast the input vectors to byte vectors. |
Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7149 | Value *Op0 = InsertCastBefore(Instruction::BitCast, |
| 7150 | II->getOperand(1), Mask->getType(), CI); |
| 7151 | Value *Op1 = InsertCastBefore(Instruction::BitCast, |
| 7152 | II->getOperand(2), Mask->getType(), CI); |
Chris Lattner | e79d249 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 7153 | Value *Result = UndefValue::get(Op0->getType()); |
| 7154 | |
| 7155 | // Only extract each element once. |
| 7156 | Value *ExtractedElts[32]; |
| 7157 | memset(ExtractedElts, 0, sizeof(ExtractedElts)); |
| 7158 | |
| 7159 | for (unsigned i = 0; i != 16; ++i) { |
| 7160 | if (isa<UndefValue>(Mask->getOperand(i))) |
| 7161 | continue; |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7162 | unsigned Idx =cast<ConstantInt>(Mask->getOperand(i))->getZExtValue(); |
Chris Lattner | e79d249 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 7163 | Idx &= 31; // Match the hardware behavior. |
| 7164 | |
| 7165 | if (ExtractedElts[Idx] == 0) { |
| 7166 | Instruction *Elt = |
Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 7167 | new ExtractElementInst(Idx < 16 ? Op0 : Op1, Idx&15, "tmp"); |
Chris Lattner | e79d249 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 7168 | InsertNewInstBefore(Elt, CI); |
| 7169 | ExtractedElts[Idx] = Elt; |
| 7170 | } |
| 7171 | |
| 7172 | // Insert this value into the result vector. |
Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 7173 | Result = new InsertElementInst(Result, ExtractedElts[Idx], i,"tmp"); |
Chris Lattner | e79d249 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 7174 | InsertNewInstBefore(cast<Instruction>(Result), CI); |
| 7175 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7176 | return CastInst::create(Instruction::BitCast, Result, CI.getType()); |
Chris Lattner | e79d249 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 7177 | } |
| 7178 | } |
| 7179 | break; |
| 7180 | |
Chris Lattner | 503221f | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 7181 | case Intrinsic::stackrestore: { |
| 7182 | // If the save is right next to the restore, remove the restore. This can |
| 7183 | // happen when variable allocas are DCE'd. |
| 7184 | if (IntrinsicInst *SS = dyn_cast<IntrinsicInst>(II->getOperand(1))) { |
| 7185 | if (SS->getIntrinsicID() == Intrinsic::stacksave) { |
| 7186 | BasicBlock::iterator BI = SS; |
| 7187 | if (&*++BI == II) |
| 7188 | return EraseInstFromFunction(CI); |
| 7189 | } |
| 7190 | } |
| 7191 | |
| 7192 | // If the stack restore is in a return/unwind block and if there are no |
| 7193 | // allocas or calls between the restore and the return, nuke the restore. |
| 7194 | TerminatorInst *TI = II->getParent()->getTerminator(); |
| 7195 | if (isa<ReturnInst>(TI) || isa<UnwindInst>(TI)) { |
| 7196 | BasicBlock::iterator BI = II; |
| 7197 | bool CannotRemove = false; |
| 7198 | for (++BI; &*BI != TI; ++BI) { |
| 7199 | if (isa<AllocaInst>(BI) || |
| 7200 | (isa<CallInst>(BI) && !isa<IntrinsicInst>(BI))) { |
| 7201 | CannotRemove = true; |
| 7202 | break; |
| 7203 | } |
| 7204 | } |
| 7205 | if (!CannotRemove) |
| 7206 | return EraseInstFromFunction(CI); |
| 7207 | } |
| 7208 | break; |
| 7209 | } |
| 7210 | } |
Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 7211 | } |
| 7212 | |
Chris Lattner | c66b223 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 7213 | return visitCallSite(II); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7214 | } |
| 7215 | |
| 7216 | // InvokeInst simplification |
| 7217 | // |
| 7218 | Instruction *InstCombiner::visitInvokeInst(InvokeInst &II) { |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 7219 | return visitCallSite(&II); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7220 | } |
| 7221 | |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 7222 | // visitCallSite - Improvements for call and invoke instructions. |
| 7223 | // |
| 7224 | Instruction *InstCombiner::visitCallSite(CallSite CS) { |
Chris Lattner | 75b4d1d | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 7225 | bool Changed = false; |
| 7226 | |
| 7227 | // If the callee is a constexpr cast of a function, attempt to move the cast |
| 7228 | // to the arguments of the call/invoke. |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 7229 | if (transformConstExprCastCall(CS)) return 0; |
| 7230 | |
Chris Lattner | 75b4d1d | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 7231 | Value *Callee = CS.getCalledValue(); |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 7232 | |
Chris Lattner | 61d9d81 | 2005-05-13 07:09:09 +0000 | [diff] [blame] | 7233 | if (Function *CalleeF = dyn_cast<Function>(Callee)) |
| 7234 | if (CalleeF->getCallingConv() != CS.getCallingConv()) { |
| 7235 | Instruction *OldCall = CS.getInstruction(); |
| 7236 | // If the call and callee calling conventions don't match, this call must |
| 7237 | // be unreachable, as the call is undefined. |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 7238 | new StoreInst(ConstantInt::getTrue(), |
Reid Spencer | 542964f | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 7239 | UndefValue::get(PointerType::get(Type::Int1Ty)), OldCall); |
Chris Lattner | 61d9d81 | 2005-05-13 07:09:09 +0000 | [diff] [blame] | 7240 | if (!OldCall->use_empty()) |
| 7241 | OldCall->replaceAllUsesWith(UndefValue::get(OldCall->getType())); |
| 7242 | if (isa<CallInst>(OldCall)) // Not worth removing an invoke here. |
| 7243 | return EraseInstFromFunction(*OldCall); |
| 7244 | return 0; |
| 7245 | } |
| 7246 | |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 7247 | if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) { |
| 7248 | // This instruction is not reachable, just remove it. We insert a store to |
| 7249 | // undef so that we know that this code is not reachable, despite the fact |
| 7250 | // that we can't modify the CFG here. |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 7251 | new StoreInst(ConstantInt::getTrue(), |
Reid Spencer | 542964f | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 7252 | UndefValue::get(PointerType::get(Type::Int1Ty)), |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 7253 | CS.getInstruction()); |
| 7254 | |
| 7255 | if (!CS.getInstruction()->use_empty()) |
| 7256 | CS.getInstruction()-> |
| 7257 | replaceAllUsesWith(UndefValue::get(CS.getInstruction()->getType())); |
| 7258 | |
| 7259 | if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) { |
| 7260 | // Don't break the CFG, insert a dummy cond branch. |
| 7261 | new BranchInst(II->getNormalDest(), II->getUnwindDest(), |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 7262 | ConstantInt::getTrue(), II); |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 7263 | } |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 7264 | return EraseInstFromFunction(*CS.getInstruction()); |
| 7265 | } |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 7266 | |
Chris Lattner | 75b4d1d | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 7267 | const PointerType *PTy = cast<PointerType>(Callee->getType()); |
| 7268 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
| 7269 | if (FTy->isVarArg()) { |
| 7270 | // See if we can optimize any arguments passed through the varargs area of |
| 7271 | // the call. |
| 7272 | for (CallSite::arg_iterator I = CS.arg_begin()+FTy->getNumParams(), |
| 7273 | E = CS.arg_end(); I != E; ++I) |
| 7274 | if (CastInst *CI = dyn_cast<CastInst>(*I)) { |
| 7275 | // If this cast does not effect the value passed through the varargs |
| 7276 | // area, we can eliminate the use of the cast. |
| 7277 | Value *Op = CI->getOperand(0); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7278 | if (CI->isLosslessCast()) { |
Chris Lattner | 75b4d1d | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 7279 | *I = Op; |
| 7280 | Changed = true; |
| 7281 | } |
| 7282 | } |
| 7283 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7284 | |
Chris Lattner | 75b4d1d | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 7285 | return Changed ? CS.getInstruction() : 0; |
Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 7286 | } |
| 7287 | |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7288 | // transformConstExprCastCall - If the callee is a constexpr cast of a function, |
| 7289 | // attempt to move the cast to the arguments of the call/invoke. |
| 7290 | // |
| 7291 | bool InstCombiner::transformConstExprCastCall(CallSite CS) { |
| 7292 | if (!isa<ConstantExpr>(CS.getCalledValue())) return false; |
| 7293 | ConstantExpr *CE = cast<ConstantExpr>(CS.getCalledValue()); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7294 | if (CE->getOpcode() != Instruction::BitCast || |
| 7295 | !isa<Function>(CE->getOperand(0))) |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7296 | return false; |
Reid Spencer | 8743687 | 2004-07-18 00:38:32 +0000 | [diff] [blame] | 7297 | Function *Callee = cast<Function>(CE->getOperand(0)); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7298 | Instruction *Caller = CS.getInstruction(); |
| 7299 | |
| 7300 | // Okay, this is a cast from a function to a different type. Unless doing so |
| 7301 | // would cause a type conversion of one of our arguments, change this call to |
| 7302 | // be a direct call with arguments casted to the appropriate types. |
| 7303 | // |
| 7304 | const FunctionType *FT = Callee->getFunctionType(); |
| 7305 | const Type *OldRetTy = Caller->getType(); |
| 7306 | |
Chris Lattner | 1f7942f | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 7307 | // Check to see if we are changing the return type... |
| 7308 | if (OldRetTy != FT->getReturnType()) { |
Reid Spencer | 5301e7c | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 7309 | if (Callee->isDeclaration() && !Caller->use_empty() && |
Chris Lattner | 7051d75 | 2007-01-06 19:53:32 +0000 | [diff] [blame] | 7310 | OldRetTy != FT->getReturnType() && |
| 7311 | // Conversion is ok if changing from pointer to int of same size. |
| 7312 | !(isa<PointerType>(FT->getReturnType()) && |
| 7313 | TD->getIntPtrType() == OldRetTy)) |
Chris Lattner | 400f959 | 2007-01-06 02:09:32 +0000 | [diff] [blame] | 7314 | return false; // Cannot transform this return value. |
Chris Lattner | 1f7942f | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 7315 | |
| 7316 | // If the callsite is an invoke instruction, and the return value is used by |
| 7317 | // a PHI node in a successor, we cannot change the return type of the call |
| 7318 | // because there is no place to put the cast instruction (without breaking |
| 7319 | // the critical edge). Bail out in this case. |
| 7320 | if (!Caller->use_empty()) |
| 7321 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) |
| 7322 | for (Value::use_iterator UI = II->use_begin(), E = II->use_end(); |
| 7323 | UI != E; ++UI) |
| 7324 | if (PHINode *PN = dyn_cast<PHINode>(*UI)) |
| 7325 | if (PN->getParent() == II->getNormalDest() || |
Chris Lattner | fae8ab3 | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 7326 | PN->getParent() == II->getUnwindDest()) |
Chris Lattner | 1f7942f | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 7327 | return false; |
| 7328 | } |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7329 | |
| 7330 | unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin()); |
| 7331 | unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7332 | |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7333 | CallSite::arg_iterator AI = CS.arg_begin(); |
| 7334 | for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) { |
| 7335 | const Type *ParamTy = FT->getParamType(i); |
Andrew Lenharth | ebfa24e | 2006-06-28 01:01:52 +0000 | [diff] [blame] | 7336 | const Type *ActTy = (*AI)->getType(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7337 | ConstantInt *c = dyn_cast<ConstantInt>(*AI); |
Andrew Lenharth | ebfa24e | 2006-06-28 01:01:52 +0000 | [diff] [blame] | 7338 | //Either we can cast directly, or we can upconvert the argument |
Chris Lattner | 400f959 | 2007-01-06 02:09:32 +0000 | [diff] [blame] | 7339 | bool isConvertible = ActTy == ParamTy || |
Chris Lattner | 7051d75 | 2007-01-06 19:53:32 +0000 | [diff] [blame] | 7340 | (isa<PointerType>(ParamTy) && isa<PointerType>(ActTy)) || |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 7341 | (ParamTy->isInteger() && ActTy->isInteger() && |
Reid Spencer | 8f166b0 | 2007-01-08 16:32:00 +0000 | [diff] [blame] | 7342 | ParamTy->getPrimitiveSizeInBits() >= ActTy->getPrimitiveSizeInBits()) || |
| 7343 | (c && ParamTy->getPrimitiveSizeInBits() >= ActTy->getPrimitiveSizeInBits() |
| 7344 | && c->getSExtValue() > 0); |
Reid Spencer | 5301e7c | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 7345 | if (Callee->isDeclaration() && !isConvertible) return false; |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7346 | } |
| 7347 | |
| 7348 | if (FT->getNumParams() < NumActualArgs && !FT->isVarArg() && |
Reid Spencer | 5301e7c | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 7349 | Callee->isDeclaration()) |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7350 | return false; // Do not delete arguments unless we have a function body... |
| 7351 | |
| 7352 | // Okay, we decided that this is a safe thing to do: go ahead and start |
| 7353 | // inserting cast instructions as necessary... |
| 7354 | std::vector<Value*> Args; |
| 7355 | Args.reserve(NumActualArgs); |
| 7356 | |
| 7357 | AI = CS.arg_begin(); |
| 7358 | for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) { |
| 7359 | const Type *ParamTy = FT->getParamType(i); |
| 7360 | if ((*AI)->getType() == ParamTy) { |
| 7361 | Args.push_back(*AI); |
| 7362 | } else { |
Reid Spencer | 668d90f | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 7363 | Instruction::CastOps opcode = CastInst::getCastOpcode(*AI, |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 7364 | false, ParamTy, false); |
Reid Spencer | 668d90f | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 7365 | CastInst *NewCast = CastInst::create(opcode, *AI, ParamTy, "tmp"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7366 | Args.push_back(InsertNewInstBefore(NewCast, *Caller)); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7367 | } |
| 7368 | } |
| 7369 | |
| 7370 | // If the function takes more arguments than the call was taking, add them |
| 7371 | // now... |
| 7372 | for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i) |
| 7373 | Args.push_back(Constant::getNullValue(FT->getParamType(i))); |
| 7374 | |
| 7375 | // If we are removing arguments to the function, emit an obnoxious warning... |
| 7376 | if (FT->getNumParams() < NumActualArgs) |
| 7377 | if (!FT->isVarArg()) { |
Bill Wendling | f3baad3 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 7378 | cerr << "WARNING: While resolving call to function '" |
| 7379 | << Callee->getName() << "' arguments were dropped!\n"; |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7380 | } else { |
| 7381 | // Add all of the arguments in their promoted form to the arg list... |
| 7382 | for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) { |
| 7383 | const Type *PTy = getPromotedType((*AI)->getType()); |
| 7384 | if (PTy != (*AI)->getType()) { |
| 7385 | // Must promote to pass through va_arg area! |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 7386 | Instruction::CastOps opcode = CastInst::getCastOpcode(*AI, false, |
| 7387 | PTy, false); |
Reid Spencer | 668d90f | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 7388 | Instruction *Cast = CastInst::create(opcode, *AI, PTy, "tmp"); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7389 | InsertNewInstBefore(Cast, *Caller); |
| 7390 | Args.push_back(Cast); |
| 7391 | } else { |
| 7392 | Args.push_back(*AI); |
| 7393 | } |
| 7394 | } |
| 7395 | } |
| 7396 | |
| 7397 | if (FT->getReturnType() == Type::VoidTy) |
Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 7398 | Caller->setName(""); // Void type should not have a name. |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7399 | |
| 7400 | Instruction *NC; |
| 7401 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
Chris Lattner | fae8ab3 | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 7402 | NC = new InvokeInst(Callee, II->getNormalDest(), II->getUnwindDest(), |
Chris Lattner | a06a8fd | 2007-02-13 02:10:56 +0000 | [diff] [blame] | 7403 | &Args[0], Args.size(), Caller->getName(), Caller); |
Chris Lattner | 05c703e | 2005-05-14 12:25:32 +0000 | [diff] [blame] | 7404 | cast<InvokeInst>(II)->setCallingConv(II->getCallingConv()); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7405 | } else { |
Chris Lattner | a06a8fd | 2007-02-13 02:10:56 +0000 | [diff] [blame] | 7406 | NC = new CallInst(Callee, &Args[0], Args.size(), Caller->getName(), Caller); |
Chris Lattner | 6aacb0f | 2005-05-06 06:48:21 +0000 | [diff] [blame] | 7407 | if (cast<CallInst>(Caller)->isTailCall()) |
| 7408 | cast<CallInst>(NC)->setTailCall(); |
Chris Lattner | 05c703e | 2005-05-14 12:25:32 +0000 | [diff] [blame] | 7409 | cast<CallInst>(NC)->setCallingConv(cast<CallInst>(Caller)->getCallingConv()); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7410 | } |
| 7411 | |
Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 7412 | // Insert a cast of the return type as necessary. |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7413 | Value *NV = NC; |
| 7414 | if (Caller->getType() != NV->getType() && !Caller->use_empty()) { |
| 7415 | if (NV->getType() != Type::VoidTy) { |
Reid Spencer | 668d90f | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 7416 | const Type *CallerTy = Caller->getType(); |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 7417 | Instruction::CastOps opcode = CastInst::getCastOpcode(NC, false, |
| 7418 | CallerTy, false); |
Reid Spencer | 668d90f | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 7419 | NV = NC = CastInst::create(opcode, NC, CallerTy, "tmp"); |
Chris Lattner | 686767f | 2003-10-30 00:46:41 +0000 | [diff] [blame] | 7420 | |
| 7421 | // If this is an invoke instruction, we should insert it after the first |
| 7422 | // non-phi, instruction in the normal successor block. |
| 7423 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
| 7424 | BasicBlock::iterator I = II->getNormalDest()->begin(); |
| 7425 | while (isa<PHINode>(I)) ++I; |
| 7426 | InsertNewInstBefore(NC, *I); |
| 7427 | } else { |
| 7428 | // Otherwise, it's a call, just insert cast right after the call instr |
| 7429 | InsertNewInstBefore(NC, *Caller); |
| 7430 | } |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 7431 | AddUsersToWorkList(*Caller); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7432 | } else { |
Chris Lattner | e29d634 | 2004-10-17 21:22:38 +0000 | [diff] [blame] | 7433 | NV = UndefValue::get(Caller->getType()); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7434 | } |
| 7435 | } |
| 7436 | |
| 7437 | if (Caller->getType() != Type::VoidTy && !Caller->use_empty()) |
| 7438 | Caller->replaceAllUsesWith(NV); |
Chris Lattner | 51f5457 | 2007-03-02 19:59:19 +0000 | [diff] [blame] | 7439 | Caller->eraseFromParent(); |
Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 7440 | RemoveFromWorkList(Caller); |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7441 | return true; |
| 7442 | } |
| 7443 | |
Chris Lattner | cadac0c | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7444 | /// FoldPHIArgBinOpIntoPHI - If we have something like phi [add (a,b), add(c,d)] |
| 7445 | /// and if a/b/c/d and the add's all have a single use, turn this into two phi's |
| 7446 | /// and a single binop. |
| 7447 | Instruction *InstCombiner::FoldPHIArgBinOpIntoPHI(PHINode &PN) { |
| 7448 | Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0)); |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7449 | assert(isa<BinaryOperator>(FirstInst) || isa<GetElementPtrInst>(FirstInst) || |
| 7450 | isa<CmpInst>(FirstInst)); |
Chris Lattner | cadac0c | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7451 | unsigned Opc = FirstInst->getOpcode(); |
Chris Lattner | cd62f11 | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 7452 | Value *LHSVal = FirstInst->getOperand(0); |
| 7453 | Value *RHSVal = FirstInst->getOperand(1); |
| 7454 | |
| 7455 | const Type *LHSType = LHSVal->getType(); |
| 7456 | const Type *RHSType = RHSVal->getType(); |
Chris Lattner | cadac0c | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7457 | |
| 7458 | // Scan to see if all operands are the same opcode, all have one use, and all |
| 7459 | // kill their operands (i.e. the operands have one use). |
Chris Lattner | dc826fc | 2006-11-01 04:55:47 +0000 | [diff] [blame] | 7460 | for (unsigned i = 0; i != PN.getNumIncomingValues(); ++i) { |
Chris Lattner | cadac0c | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7461 | Instruction *I = dyn_cast<Instruction>(PN.getIncomingValue(i)); |
Chris Lattner | dc826fc | 2006-11-01 04:55:47 +0000 | [diff] [blame] | 7462 | if (!I || I->getOpcode() != Opc || !I->hasOneUse() || |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7463 | // Verify type of the LHS matches so we don't fold cmp's of different |
Chris Lattner | eebea43 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 7464 | // types or GEP's with different index types. |
| 7465 | I->getOperand(0)->getType() != LHSType || |
| 7466 | I->getOperand(1)->getType() != RHSType) |
Chris Lattner | cadac0c | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7467 | return 0; |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7468 | |
| 7469 | // If they are CmpInst instructions, check their predicates |
| 7470 | if (Opc == Instruction::ICmp || Opc == Instruction::FCmp) |
| 7471 | if (cast<CmpInst>(I)->getPredicate() != |
| 7472 | cast<CmpInst>(FirstInst)->getPredicate()) |
| 7473 | return 0; |
Chris Lattner | cd62f11 | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 7474 | |
| 7475 | // Keep track of which operand needs a phi node. |
| 7476 | if (I->getOperand(0) != LHSVal) LHSVal = 0; |
| 7477 | if (I->getOperand(1) != RHSVal) RHSVal = 0; |
Chris Lattner | cadac0c | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7478 | } |
| 7479 | |
Chris Lattner | 4f218d5 | 2006-11-08 19:42:28 +0000 | [diff] [blame] | 7480 | // Otherwise, this is safe to transform, determine if it is profitable. |
| 7481 | |
| 7482 | // If this is a GEP, and if the index (not the pointer) needs a PHI, bail out. |
| 7483 | // Indexes are often folded into load/store instructions, so we don't want to |
| 7484 | // hide them behind a phi. |
| 7485 | if (isa<GetElementPtrInst>(FirstInst) && RHSVal == 0) |
| 7486 | return 0; |
| 7487 | |
Chris Lattner | cadac0c | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7488 | Value *InLHS = FirstInst->getOperand(0); |
Chris Lattner | cadac0c | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7489 | Value *InRHS = FirstInst->getOperand(1); |
Chris Lattner | 4f218d5 | 2006-11-08 19:42:28 +0000 | [diff] [blame] | 7490 | PHINode *NewLHS = 0, *NewRHS = 0; |
Chris Lattner | cd62f11 | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 7491 | if (LHSVal == 0) { |
| 7492 | NewLHS = new PHINode(LHSType, FirstInst->getOperand(0)->getName()+".pn"); |
| 7493 | NewLHS->reserveOperandSpace(PN.getNumOperands()/2); |
| 7494 | NewLHS->addIncoming(InLHS, PN.getIncomingBlock(0)); |
Chris Lattner | eebea43 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 7495 | InsertNewInstBefore(NewLHS, PN); |
| 7496 | LHSVal = NewLHS; |
| 7497 | } |
Chris Lattner | cd62f11 | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 7498 | |
| 7499 | if (RHSVal == 0) { |
| 7500 | NewRHS = new PHINode(RHSType, FirstInst->getOperand(1)->getName()+".pn"); |
| 7501 | NewRHS->reserveOperandSpace(PN.getNumOperands()/2); |
| 7502 | NewRHS->addIncoming(InRHS, PN.getIncomingBlock(0)); |
Chris Lattner | eebea43 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 7503 | InsertNewInstBefore(NewRHS, PN); |
| 7504 | RHSVal = NewRHS; |
| 7505 | } |
| 7506 | |
Chris Lattner | cd62f11 | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 7507 | // Add all operands to the new PHIs. |
| 7508 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 7509 | if (NewLHS) { |
| 7510 | Value *NewInLHS =cast<Instruction>(PN.getIncomingValue(i))->getOperand(0); |
| 7511 | NewLHS->addIncoming(NewInLHS, PN.getIncomingBlock(i)); |
| 7512 | } |
| 7513 | if (NewRHS) { |
| 7514 | Value *NewInRHS =cast<Instruction>(PN.getIncomingValue(i))->getOperand(1); |
| 7515 | NewRHS->addIncoming(NewInRHS, PN.getIncomingBlock(i)); |
| 7516 | } |
| 7517 | } |
| 7518 | |
Chris Lattner | cadac0c | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7519 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) |
Chris Lattner | eebea43 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 7520 | return BinaryOperator::create(BinOp->getOpcode(), LHSVal, RHSVal); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7521 | else if (CmpInst *CIOp = dyn_cast<CmpInst>(FirstInst)) |
| 7522 | return CmpInst::create(CIOp->getOpcode(), CIOp->getPredicate(), LHSVal, |
| 7523 | RHSVal); |
Chris Lattner | eebea43 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 7524 | else { |
| 7525 | assert(isa<GetElementPtrInst>(FirstInst)); |
| 7526 | return new GetElementPtrInst(LHSVal, RHSVal); |
| 7527 | } |
Chris Lattner | cadac0c | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7528 | } |
| 7529 | |
Chris Lattner | 14f82c7 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 7530 | /// isSafeToSinkLoad - Return true if we know that it is safe sink the load out |
| 7531 | /// of the block that defines it. This means that it must be obvious the value |
| 7532 | /// of the load is not changed from the point of the load to the end of the |
| 7533 | /// block it is in. |
Chris Lattner | c904205 | 2007-02-01 22:30:07 +0000 | [diff] [blame] | 7534 | /// |
| 7535 | /// Finally, it is safe, but not profitable, to sink a load targetting a |
| 7536 | /// non-address-taken alloca. Doing so will cause us to not promote the alloca |
| 7537 | /// to a register. |
Chris Lattner | 14f82c7 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 7538 | static bool isSafeToSinkLoad(LoadInst *L) { |
| 7539 | BasicBlock::iterator BBI = L, E = L->getParent()->end(); |
| 7540 | |
| 7541 | for (++BBI; BBI != E; ++BBI) |
| 7542 | if (BBI->mayWriteToMemory()) |
| 7543 | return false; |
Chris Lattner | c904205 | 2007-02-01 22:30:07 +0000 | [diff] [blame] | 7544 | |
| 7545 | // Check for non-address taken alloca. If not address-taken already, it isn't |
| 7546 | // profitable to do this xform. |
| 7547 | if (AllocaInst *AI = dyn_cast<AllocaInst>(L->getOperand(0))) { |
| 7548 | bool isAddressTaken = false; |
| 7549 | for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); |
| 7550 | UI != E; ++UI) { |
| 7551 | if (isa<LoadInst>(UI)) continue; |
| 7552 | if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) { |
| 7553 | // If storing TO the alloca, then the address isn't taken. |
| 7554 | if (SI->getOperand(1) == AI) continue; |
| 7555 | } |
| 7556 | isAddressTaken = true; |
| 7557 | break; |
| 7558 | } |
| 7559 | |
| 7560 | if (!isAddressTaken) |
| 7561 | return false; |
| 7562 | } |
| 7563 | |
Chris Lattner | 14f82c7 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 7564 | return true; |
| 7565 | } |
| 7566 | |
Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7567 | |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 7568 | // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary" |
| 7569 | // operator and they all are only used by the PHI, PHI together their |
| 7570 | // inputs, and do the operation once, to the result of the PHI. |
| 7571 | Instruction *InstCombiner::FoldPHIArgOpIntoPHI(PHINode &PN) { |
| 7572 | Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0)); |
| 7573 | |
| 7574 | // Scan the instruction, looking for input operations that can be folded away. |
| 7575 | // If all input operands to the phi are the same instruction (e.g. a cast from |
| 7576 | // the same type or "+42") we can pull the operation through the PHI, reducing |
| 7577 | // code size and simplifying code. |
| 7578 | Constant *ConstantOp = 0; |
| 7579 | const Type *CastSrcTy = 0; |
Chris Lattner | 14f82c7 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 7580 | bool isVolatile = false; |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 7581 | if (isa<CastInst>(FirstInst)) { |
| 7582 | CastSrcTy = FirstInst->getOperand(0)->getType(); |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7583 | } else if (isa<BinaryOperator>(FirstInst) || isa<CmpInst>(FirstInst)) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7584 | // Can fold binop, compare or shift here if the RHS is a constant, |
| 7585 | // otherwise call FoldPHIArgBinOpIntoPHI. |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 7586 | ConstantOp = dyn_cast<Constant>(FirstInst->getOperand(1)); |
Chris Lattner | cadac0c | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7587 | if (ConstantOp == 0) |
| 7588 | return FoldPHIArgBinOpIntoPHI(PN); |
Chris Lattner | 14f82c7 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 7589 | } else if (LoadInst *LI = dyn_cast<LoadInst>(FirstInst)) { |
| 7590 | isVolatile = LI->isVolatile(); |
| 7591 | // We can't sink the load if the loaded value could be modified between the |
| 7592 | // load and the PHI. |
| 7593 | if (LI->getParent() != PN.getIncomingBlock(0) || |
| 7594 | !isSafeToSinkLoad(LI)) |
| 7595 | return 0; |
Chris Lattner | eebea43 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 7596 | } else if (isa<GetElementPtrInst>(FirstInst)) { |
Chris Lattner | 4f218d5 | 2006-11-08 19:42:28 +0000 | [diff] [blame] | 7597 | if (FirstInst->getNumOperands() == 2) |
Chris Lattner | eebea43 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 7598 | return FoldPHIArgBinOpIntoPHI(PN); |
| 7599 | // Can't handle general GEPs yet. |
| 7600 | return 0; |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 7601 | } else { |
| 7602 | return 0; // Cannot fold this operation. |
| 7603 | } |
| 7604 | |
| 7605 | // Check to see if all arguments are the same operation. |
| 7606 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 7607 | if (!isa<Instruction>(PN.getIncomingValue(i))) return 0; |
| 7608 | Instruction *I = cast<Instruction>(PN.getIncomingValue(i)); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7609 | if (!I->hasOneUse() || !I->isSameOperationAs(FirstInst)) |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 7610 | return 0; |
| 7611 | if (CastSrcTy) { |
| 7612 | if (I->getOperand(0)->getType() != CastSrcTy) |
| 7613 | return 0; // Cast operation must match. |
Chris Lattner | 14f82c7 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 7614 | } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7615 | // We can't sink the load if the loaded value could be modified between |
| 7616 | // the load and the PHI. |
Chris Lattner | 14f82c7 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 7617 | if (LI->isVolatile() != isVolatile || |
| 7618 | LI->getParent() != PN.getIncomingBlock(i) || |
| 7619 | !isSafeToSinkLoad(LI)) |
| 7620 | return 0; |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 7621 | } else if (I->getOperand(1) != ConstantOp) { |
| 7622 | return 0; |
| 7623 | } |
| 7624 | } |
| 7625 | |
| 7626 | // Okay, they are all the same operation. Create a new PHI node of the |
| 7627 | // correct type, and PHI together all of the LHS's of the instructions. |
| 7628 | PHINode *NewPN = new PHINode(FirstInst->getOperand(0)->getType(), |
| 7629 | PN.getName()+".in"); |
Chris Lattner | d8e2018 | 2005-01-29 00:39:08 +0000 | [diff] [blame] | 7630 | NewPN->reserveOperandSpace(PN.getNumOperands()/2); |
Chris Lattner | 46dd5a6 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 7631 | |
| 7632 | Value *InVal = FirstInst->getOperand(0); |
| 7633 | NewPN->addIncoming(InVal, PN.getIncomingBlock(0)); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 7634 | |
| 7635 | // Add all operands to the new PHI. |
Chris Lattner | 46dd5a6 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 7636 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 7637 | Value *NewInVal = cast<Instruction>(PN.getIncomingValue(i))->getOperand(0); |
| 7638 | if (NewInVal != InVal) |
| 7639 | InVal = 0; |
| 7640 | NewPN->addIncoming(NewInVal, PN.getIncomingBlock(i)); |
| 7641 | } |
| 7642 | |
| 7643 | Value *PhiVal; |
| 7644 | if (InVal) { |
| 7645 | // The new PHI unions all of the same values together. This is really |
| 7646 | // common, so we handle it intelligently here for compile-time speed. |
| 7647 | PhiVal = InVal; |
| 7648 | delete NewPN; |
| 7649 | } else { |
| 7650 | InsertNewInstBefore(NewPN, PN); |
| 7651 | PhiVal = NewPN; |
| 7652 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7653 | |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 7654 | // Insert and return the new operation. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7655 | if (CastInst* FirstCI = dyn_cast<CastInst>(FirstInst)) |
| 7656 | return CastInst::create(FirstCI->getOpcode(), PhiVal, PN.getType()); |
Reid Spencer | de46e48 | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 7657 | else if (isa<LoadInst>(FirstInst)) |
Chris Lattner | 14f82c7 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 7658 | return new LoadInst(PhiVal, "", isVolatile); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 7659 | else if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) |
Chris Lattner | 46dd5a6 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 7660 | return BinaryOperator::create(BinOp->getOpcode(), PhiVal, ConstantOp); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7661 | else if (CmpInst *CIOp = dyn_cast<CmpInst>(FirstInst)) |
| 7662 | return CmpInst::create(CIOp->getOpcode(), CIOp->getPredicate(), |
| 7663 | PhiVal, ConstantOp); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 7664 | else |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7665 | assert(0 && "Unknown operation"); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 7666 | } |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 7667 | |
Chris Lattner | 7153643 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 7668 | /// DeadPHICycle - Return true if this PHI node is only used by a PHI node cycle |
| 7669 | /// that is dead. |
| 7670 | static bool DeadPHICycle(PHINode *PN, std::set<PHINode*> &PotentiallyDeadPHIs) { |
| 7671 | if (PN->use_empty()) return true; |
| 7672 | if (!PN->hasOneUse()) return false; |
| 7673 | |
| 7674 | // Remember this node, and if we find the cycle, return. |
| 7675 | if (!PotentiallyDeadPHIs.insert(PN).second) |
| 7676 | return true; |
| 7677 | |
| 7678 | if (PHINode *PU = dyn_cast<PHINode>(PN->use_back())) |
| 7679 | return DeadPHICycle(PU, PotentiallyDeadPHIs); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7680 | |
Chris Lattner | 7153643 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 7681 | return false; |
| 7682 | } |
| 7683 | |
Chris Lattner | bbbdd85 | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 7684 | // PHINode simplification |
| 7685 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 7686 | Instruction *InstCombiner::visitPHINode(PHINode &PN) { |
Owen Anderson | bbf8990 | 2006-07-10 22:15:25 +0000 | [diff] [blame] | 7687 | // If LCSSA is around, don't mess with Phi nodes |
Chris Lattner | 8258b44 | 2007-03-04 04:27:24 +0000 | [diff] [blame] | 7688 | if (MustPreserveLCSSA) return 0; |
Owen Anderson | a6968f8 | 2006-07-10 19:03:49 +0000 | [diff] [blame] | 7689 | |
Owen Anderson | ae8aa64 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 7690 | if (Value *V = PN.hasConstantValue()) |
| 7691 | return ReplaceInstUsesWith(PN, V); |
| 7692 | |
Owen Anderson | ae8aa64 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 7693 | // If all PHI operands are the same operation, pull them through the PHI, |
| 7694 | // reducing code size. |
| 7695 | if (isa<Instruction>(PN.getIncomingValue(0)) && |
| 7696 | PN.getIncomingValue(0)->hasOneUse()) |
| 7697 | if (Instruction *Result = FoldPHIArgOpIntoPHI(PN)) |
| 7698 | return Result; |
| 7699 | |
| 7700 | // If this is a trivial cycle in the PHI node graph, remove it. Basically, if |
| 7701 | // this PHI only has a single use (a PHI), and if that PHI only has one use (a |
| 7702 | // PHI)... break the cycle. |
Chris Lattner | c8dcede | 2007-01-15 07:30:06 +0000 | [diff] [blame] | 7703 | if (PN.hasOneUse()) { |
| 7704 | Instruction *PHIUser = cast<Instruction>(PN.use_back()); |
| 7705 | if (PHINode *PU = dyn_cast<PHINode>(PHIUser)) { |
Owen Anderson | ae8aa64 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 7706 | std::set<PHINode*> PotentiallyDeadPHIs; |
| 7707 | PotentiallyDeadPHIs.insert(&PN); |
| 7708 | if (DeadPHICycle(PU, PotentiallyDeadPHIs)) |
| 7709 | return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType())); |
| 7710 | } |
Chris Lattner | c8dcede | 2007-01-15 07:30:06 +0000 | [diff] [blame] | 7711 | |
| 7712 | // If this phi has a single use, and if that use just computes a value for |
| 7713 | // the next iteration of a loop, delete the phi. This occurs with unused |
| 7714 | // induction variables, e.g. "for (int j = 0; ; ++j);". Detecting this |
| 7715 | // common case here is good because the only other things that catch this |
| 7716 | // are induction variable analysis (sometimes) and ADCE, which is only run |
| 7717 | // late. |
| 7718 | if (PHIUser->hasOneUse() && |
| 7719 | (isa<BinaryOperator>(PHIUser) || isa<GetElementPtrInst>(PHIUser)) && |
| 7720 | PHIUser->use_back() == &PN) { |
| 7721 | return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType())); |
| 7722 | } |
| 7723 | } |
Owen Anderson | ae8aa64 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 7724 | |
Chris Lattner | 91daeb5 | 2003-12-19 05:58:40 +0000 | [diff] [blame] | 7725 | return 0; |
Chris Lattner | bbbdd85 | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 7726 | } |
| 7727 | |
Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7728 | static Value *InsertCastToIntPtrTy(Value *V, const Type *DTy, |
| 7729 | Instruction *InsertPoint, |
| 7730 | InstCombiner *IC) { |
Reid Spencer | 8f166b0 | 2007-01-08 16:32:00 +0000 | [diff] [blame] | 7731 | unsigned PtrSize = DTy->getPrimitiveSizeInBits(); |
| 7732 | unsigned VTySize = V->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7733 | // We must cast correctly to the pointer type. Ensure that we |
| 7734 | // sign extend the integer value if it is smaller as this is |
| 7735 | // used for address computation. |
| 7736 | Instruction::CastOps opcode = |
| 7737 | (VTySize < PtrSize ? Instruction::SExt : |
| 7738 | (VTySize == PtrSize ? Instruction::BitCast : Instruction::Trunc)); |
| 7739 | return IC->InsertCastBefore(opcode, V, DTy, *InsertPoint); |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7740 | } |
| 7741 | |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 7742 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 7743 | Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7744 | Value *PtrOp = GEP.getOperand(0); |
Chris Lattner | 471bd76 | 2003-05-22 19:07:21 +0000 | [diff] [blame] | 7745 | // Is it 'getelementptr %P, long 0' or 'getelementptr %P' |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 7746 | // If so, eliminate the noop. |
Chris Lattner | 8d0bacb | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 7747 | if (GEP.getNumOperands() == 1) |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7748 | return ReplaceInstUsesWith(GEP, PtrOp); |
Chris Lattner | 8d0bacb | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 7749 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 7750 | if (isa<UndefValue>(GEP.getOperand(0))) |
| 7751 | return ReplaceInstUsesWith(GEP, UndefValue::get(GEP.getType())); |
| 7752 | |
Chris Lattner | 8d0bacb | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 7753 | bool HasZeroPointerIndex = false; |
| 7754 | if (Constant *C = dyn_cast<Constant>(GEP.getOperand(1))) |
| 7755 | HasZeroPointerIndex = C->isNullValue(); |
| 7756 | |
| 7757 | if (GEP.getNumOperands() == 2 && HasZeroPointerIndex) |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7758 | return ReplaceInstUsesWith(GEP, PtrOp); |
Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 7759 | |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7760 | // Eliminate unneeded casts for indices. |
| 7761 | bool MadeChange = false; |
Chris Lattner | 2b2412d | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 7762 | gep_type_iterator GTI = gep_type_begin(GEP); |
| 7763 | for (unsigned i = 1, e = GEP.getNumOperands(); i != e; ++i, ++GTI) |
| 7764 | if (isa<SequentialType>(*GTI)) { |
| 7765 | if (CastInst *CI = dyn_cast<CastInst>(GEP.getOperand(i))) { |
Chris Lattner | 27df1db | 2007-01-15 07:02:54 +0000 | [diff] [blame] | 7766 | if (CI->getOpcode() == Instruction::ZExt || |
| 7767 | CI->getOpcode() == Instruction::SExt) { |
| 7768 | const Type *SrcTy = CI->getOperand(0)->getType(); |
| 7769 | // We can eliminate a cast from i32 to i64 iff the target |
| 7770 | // is a 32-bit pointer target. |
| 7771 | if (SrcTy->getPrimitiveSizeInBits() >= TD->getPointerSizeInBits()) { |
| 7772 | MadeChange = true; |
| 7773 | GEP.setOperand(i, CI->getOperand(0)); |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7774 | } |
| 7775 | } |
| 7776 | } |
Chris Lattner | 2b2412d | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 7777 | // If we are using a wider index than needed for this platform, shrink it |
| 7778 | // to what we need. If the incoming value needs a cast instruction, |
| 7779 | // insert it. This explicit cast can make subsequent optimizations more |
| 7780 | // obvious. |
| 7781 | Value *Op = GEP.getOperand(i); |
Reid Spencer | 7a9c62b | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 7782 | if (TD->getTypeSize(Op->getType()) > TD->getPointerSize()) |
Chris Lattner | 1e9ac1a | 2004-04-17 18:16:10 +0000 | [diff] [blame] | 7783 | if (Constant *C = dyn_cast<Constant>(Op)) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7784 | GEP.setOperand(i, ConstantExpr::getTrunc(C, TD->getIntPtrType())); |
Chris Lattner | 1e9ac1a | 2004-04-17 18:16:10 +0000 | [diff] [blame] | 7785 | MadeChange = true; |
| 7786 | } else { |
Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7787 | Op = InsertCastBefore(Instruction::Trunc, Op, TD->getIntPtrType(), |
| 7788 | GEP); |
Chris Lattner | 2b2412d | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 7789 | GEP.setOperand(i, Op); |
| 7790 | MadeChange = true; |
| 7791 | } |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7792 | } |
| 7793 | if (MadeChange) return &GEP; |
| 7794 | |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 7795 | // Combine Indices - If the source pointer to this getelementptr instruction |
| 7796 | // is a getelementptr instruction, combine the indices of the two |
| 7797 | // getelementptr instructions into a single instruction. |
| 7798 | // |
Chris Lattner | af6094f | 2007-02-15 22:48:32 +0000 | [diff] [blame] | 7799 | SmallVector<Value*, 8> SrcGEPOperands; |
Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 7800 | if (User *Src = dyn_castGetElementPtr(PtrOp)) |
Chris Lattner | af6094f | 2007-02-15 22:48:32 +0000 | [diff] [blame] | 7801 | SrcGEPOperands.append(Src->op_begin(), Src->op_end()); |
Chris Lattner | 57c67b0 | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 7802 | |
| 7803 | if (!SrcGEPOperands.empty()) { |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7804 | // Note that if our source is a gep chain itself that we wait for that |
| 7805 | // chain to be resolved before we perform this transformation. This |
| 7806 | // avoids us creating a TON of code in some cases. |
| 7807 | // |
| 7808 | if (isa<GetElementPtrInst>(SrcGEPOperands[0]) && |
| 7809 | cast<Instruction>(SrcGEPOperands[0])->getNumOperands() == 2) |
| 7810 | return 0; // Wait until our source is folded to completion. |
| 7811 | |
Chris Lattner | af6094f | 2007-02-15 22:48:32 +0000 | [diff] [blame] | 7812 | SmallVector<Value*, 8> Indices; |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7813 | |
| 7814 | // Find out whether the last index in the source GEP is a sequential idx. |
| 7815 | bool EndsWithSequential = false; |
| 7816 | for (gep_type_iterator I = gep_type_begin(*cast<User>(PtrOp)), |
| 7817 | E = gep_type_end(*cast<User>(PtrOp)); I != E; ++I) |
Chris Lattner | 8ec5f88 | 2004-05-08 22:41:42 +0000 | [diff] [blame] | 7818 | EndsWithSequential = !isa<StructType>(*I); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7819 | |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 7820 | // Can we combine the two pointer arithmetics offsets? |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7821 | if (EndsWithSequential) { |
Chris Lattner | 235af56 | 2003-03-05 22:33:14 +0000 | [diff] [blame] | 7822 | // Replace: gep (gep %P, long B), long A, ... |
| 7823 | // With: T = long A+B; gep %P, T, ... |
| 7824 | // |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7825 | Value *Sum, *SO1 = SrcGEPOperands.back(), *GO1 = GEP.getOperand(1); |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7826 | if (SO1 == Constant::getNullValue(SO1->getType())) { |
| 7827 | Sum = GO1; |
| 7828 | } else if (GO1 == Constant::getNullValue(GO1->getType())) { |
| 7829 | Sum = SO1; |
| 7830 | } else { |
| 7831 | // If they aren't the same type, convert both to an integer of the |
| 7832 | // target's pointer size. |
| 7833 | if (SO1->getType() != GO1->getType()) { |
| 7834 | if (Constant *SO1C = dyn_cast<Constant>(SO1)) { |
Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7835 | SO1 = ConstantExpr::getIntegerCast(SO1C, GO1->getType(), true); |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7836 | } else if (Constant *GO1C = dyn_cast<Constant>(GO1)) { |
Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7837 | GO1 = ConstantExpr::getIntegerCast(GO1C, SO1->getType(), true); |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7838 | } else { |
| 7839 | unsigned PS = TD->getPointerSize(); |
Reid Spencer | 7a9c62b | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 7840 | if (TD->getTypeSize(SO1->getType()) == PS) { |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7841 | // Convert GO1 to SO1's type. |
Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7842 | GO1 = InsertCastToIntPtrTy(GO1, SO1->getType(), &GEP, this); |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7843 | |
Reid Spencer | 7a9c62b | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 7844 | } else if (TD->getTypeSize(GO1->getType()) == PS) { |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7845 | // Convert SO1 to GO1's type. |
Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7846 | SO1 = InsertCastToIntPtrTy(SO1, GO1->getType(), &GEP, this); |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7847 | } else { |
| 7848 | const Type *PT = TD->getIntPtrType(); |
Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7849 | SO1 = InsertCastToIntPtrTy(SO1, PT, &GEP, this); |
| 7850 | GO1 = InsertCastToIntPtrTy(GO1, PT, &GEP, this); |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7851 | } |
| 7852 | } |
| 7853 | } |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7854 | if (isa<Constant>(SO1) && isa<Constant>(GO1)) |
| 7855 | Sum = ConstantExpr::getAdd(cast<Constant>(SO1), cast<Constant>(GO1)); |
| 7856 | else { |
Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 7857 | Sum = BinaryOperator::createAdd(SO1, GO1, PtrOp->getName()+".sum"); |
| 7858 | InsertNewInstBefore(cast<Instruction>(Sum), GEP); |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7859 | } |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7860 | } |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7861 | |
| 7862 | // Recycle the GEP we already have if possible. |
| 7863 | if (SrcGEPOperands.size() == 2) { |
| 7864 | GEP.setOperand(0, SrcGEPOperands[0]); |
| 7865 | GEP.setOperand(1, Sum); |
| 7866 | return &GEP; |
| 7867 | } else { |
| 7868 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, |
| 7869 | SrcGEPOperands.end()-1); |
| 7870 | Indices.push_back(Sum); |
| 7871 | Indices.insert(Indices.end(), GEP.op_begin()+2, GEP.op_end()); |
| 7872 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7873 | } else if (isa<Constant>(*GEP.idx_begin()) && |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7874 | cast<Constant>(*GEP.idx_begin())->isNullValue() && |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7875 | SrcGEPOperands.size() != 1) { |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 7876 | // Otherwise we can do the fold if the first index of the GEP is a zero |
Chris Lattner | 57c67b0 | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 7877 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, |
| 7878 | SrcGEPOperands.end()); |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 7879 | Indices.insert(Indices.end(), GEP.idx_begin()+1, GEP.idx_end()); |
| 7880 | } |
| 7881 | |
| 7882 | if (!Indices.empty()) |
Chris Lattner | a731513 | 2007-02-12 22:56:41 +0000 | [diff] [blame] | 7883 | return new GetElementPtrInst(SrcGEPOperands[0], &Indices[0], |
| 7884 | Indices.size(), GEP.getName()); |
Chris Lattner | c59af1d | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 7885 | |
Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7886 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(PtrOp)) { |
Chris Lattner | c59af1d | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 7887 | // GEP of global variable. If all of the indices for this GEP are |
| 7888 | // constants, we can promote this to a constexpr instead of an instruction. |
| 7889 | |
| 7890 | // Scan for nonconstants... |
Chris Lattner | f96f4a8 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 7891 | SmallVector<Constant*, 8> Indices; |
Chris Lattner | c59af1d | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 7892 | User::op_iterator I = GEP.idx_begin(), E = GEP.idx_end(); |
| 7893 | for (; I != E && isa<Constant>(*I); ++I) |
| 7894 | Indices.push_back(cast<Constant>(*I)); |
| 7895 | |
| 7896 | if (I == E) { // If they are all constants... |
Chris Lattner | f96f4a8 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 7897 | Constant *CE = ConstantExpr::getGetElementPtr(GV, |
| 7898 | &Indices[0],Indices.size()); |
Chris Lattner | c59af1d | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 7899 | |
| 7900 | // Replace all uses of the GEP with the new constexpr... |
| 7901 | return ReplaceInstUsesWith(GEP, CE); |
| 7902 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7903 | } else if (Value *X = getBitCastOperand(PtrOp)) { // Is the operand a cast? |
Chris Lattner | 567b81f | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 7904 | if (!isa<PointerType>(X->getType())) { |
| 7905 | // Not interesting. Source pointer must be a cast from pointer. |
| 7906 | } else if (HasZeroPointerIndex) { |
| 7907 | // transform: GEP (cast [10 x ubyte]* X to [0 x ubyte]*), long 0, ... |
| 7908 | // into : GEP [10 x ubyte]* X, long 0, ... |
| 7909 | // |
| 7910 | // This occurs when the program declares an array extern like "int X[];" |
| 7911 | // |
| 7912 | const PointerType *CPTy = cast<PointerType>(PtrOp->getType()); |
| 7913 | const PointerType *XTy = cast<PointerType>(X->getType()); |
| 7914 | if (const ArrayType *XATy = |
| 7915 | dyn_cast<ArrayType>(XTy->getElementType())) |
| 7916 | if (const ArrayType *CATy = |
| 7917 | dyn_cast<ArrayType>(CPTy->getElementType())) |
| 7918 | if (CATy->getElementType() == XATy->getElementType()) { |
| 7919 | // At this point, we know that the cast source type is a pointer |
| 7920 | // to an array of the same type as the destination pointer |
| 7921 | // array. Because the array type is never stepped over (there |
| 7922 | // is a leading zero) we can fold the cast into this GEP. |
| 7923 | GEP.setOperand(0, X); |
| 7924 | return &GEP; |
| 7925 | } |
| 7926 | } else if (GEP.getNumOperands() == 2) { |
| 7927 | // Transform things like: |
Chris Lattner | 2a89329 | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 7928 | // %t = getelementptr ubyte* cast ([2 x int]* %str to uint*), uint %V |
| 7929 | // into: %t1 = getelementptr [2 x int*]* %str, int 0, uint %V; cast |
Chris Lattner | 567b81f | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 7930 | const Type *SrcElTy = cast<PointerType>(X->getType())->getElementType(); |
| 7931 | const Type *ResElTy=cast<PointerType>(PtrOp->getType())->getElementType(); |
| 7932 | if (isa<ArrayType>(SrcElTy) && |
| 7933 | TD->getTypeSize(cast<ArrayType>(SrcElTy)->getElementType()) == |
| 7934 | TD->getTypeSize(ResElTy)) { |
| 7935 | Value *V = InsertNewInstBefore( |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 7936 | new GetElementPtrInst(X, Constant::getNullValue(Type::Int32Ty), |
Chris Lattner | 567b81f | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 7937 | GEP.getOperand(1), GEP.getName()), GEP); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7938 | // V and GEP are both pointer types --> BitCast |
| 7939 | return new BitCastInst(V, GEP.getType()); |
Chris Lattner | 8d0bacb | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 7940 | } |
Chris Lattner | 2a89329 | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 7941 | |
| 7942 | // Transform things like: |
| 7943 | // getelementptr sbyte* cast ([100 x double]* X to sbyte*), int %tmp |
| 7944 | // (where tmp = 8*tmp2) into: |
| 7945 | // getelementptr [100 x double]* %arr, int 0, int %tmp.2 |
| 7946 | |
| 7947 | if (isa<ArrayType>(SrcElTy) && |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 7948 | (ResElTy == Type::Int8Ty || ResElTy == Type::Int8Ty)) { |
Chris Lattner | 2a89329 | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 7949 | uint64_t ArrayEltSize = |
| 7950 | TD->getTypeSize(cast<ArrayType>(SrcElTy)->getElementType()); |
| 7951 | |
| 7952 | // Check to see if "tmp" is a scale by a multiple of ArrayEltSize. We |
| 7953 | // allow either a mul, shift, or constant here. |
| 7954 | Value *NewIdx = 0; |
| 7955 | ConstantInt *Scale = 0; |
| 7956 | if (ArrayEltSize == 1) { |
| 7957 | NewIdx = GEP.getOperand(1); |
| 7958 | Scale = ConstantInt::get(NewIdx->getType(), 1); |
| 7959 | } else if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP.getOperand(1))) { |
Chris Lattner | a393e4d | 2005-09-14 17:32:56 +0000 | [diff] [blame] | 7960 | NewIdx = ConstantInt::get(CI->getType(), 1); |
Chris Lattner | 2a89329 | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 7961 | Scale = CI; |
| 7962 | } else if (Instruction *Inst =dyn_cast<Instruction>(GEP.getOperand(1))){ |
| 7963 | if (Inst->getOpcode() == Instruction::Shl && |
| 7964 | isa<ConstantInt>(Inst->getOperand(1))) { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7965 | unsigned ShAmt = |
| 7966 | cast<ConstantInt>(Inst->getOperand(1))->getZExtValue(); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7967 | Scale = ConstantInt::get(Inst->getType(), 1ULL << ShAmt); |
Chris Lattner | 2a89329 | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 7968 | NewIdx = Inst->getOperand(0); |
| 7969 | } else if (Inst->getOpcode() == Instruction::Mul && |
| 7970 | isa<ConstantInt>(Inst->getOperand(1))) { |
| 7971 | Scale = cast<ConstantInt>(Inst->getOperand(1)); |
| 7972 | NewIdx = Inst->getOperand(0); |
| 7973 | } |
| 7974 | } |
| 7975 | |
| 7976 | // If the index will be to exactly the right offset with the scale taken |
| 7977 | // out, perform the transformation. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7978 | if (Scale && Scale->getZExtValue() % ArrayEltSize == 0) { |
Reid Spencer | de46e48 | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 7979 | if (isa<ConstantInt>(Scale)) |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7980 | Scale = ConstantInt::get(Scale->getType(), |
| 7981 | Scale->getZExtValue() / ArrayEltSize); |
| 7982 | if (Scale->getZExtValue() != 1) { |
Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7983 | Constant *C = ConstantExpr::getIntegerCast(Scale, NewIdx->getType(), |
| 7984 | true /*SExt*/); |
Chris Lattner | 2a89329 | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 7985 | Instruction *Sc = BinaryOperator::createMul(NewIdx, C, "idxscale"); |
| 7986 | NewIdx = InsertNewInstBefore(Sc, GEP); |
| 7987 | } |
| 7988 | |
| 7989 | // Insert the new GEP instruction. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7990 | Instruction *NewGEP = |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 7991 | new GetElementPtrInst(X, Constant::getNullValue(Type::Int32Ty), |
Chris Lattner | 2a89329 | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 7992 | NewIdx, GEP.getName()); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7993 | NewGEP = InsertNewInstBefore(NewGEP, GEP); |
| 7994 | // The NewGEP must be pointer typed, so must the old one -> BitCast |
| 7995 | return new BitCastInst(NewGEP, GEP.getType()); |
Chris Lattner | 2a89329 | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 7996 | } |
| 7997 | } |
Chris Lattner | 8d0bacb | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 7998 | } |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 7999 | } |
| 8000 | |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 8001 | return 0; |
| 8002 | } |
| 8003 | |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 8004 | Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) { |
| 8005 | // Convert: malloc Ty, C - where C is a constant != 1 into: malloc [C x Ty], 1 |
| 8006 | if (AI.isArrayAllocation()) // Check C != 1 |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8007 | if (const ConstantInt *C = dyn_cast<ConstantInt>(AI.getArraySize())) { |
| 8008 | const Type *NewTy = |
| 8009 | ArrayType::get(AI.getAllocatedType(), C->getZExtValue()); |
Chris Lattner | a2620ac | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 8010 | AllocationInst *New = 0; |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 8011 | |
| 8012 | // Create and insert the replacement instruction... |
| 8013 | if (isa<MallocInst>(AI)) |
Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 8014 | New = new MallocInst(NewTy, 0, AI.getAlignment(), AI.getName()); |
Chris Lattner | a2620ac | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 8015 | else { |
| 8016 | assert(isa<AllocaInst>(AI) && "Unknown type of allocation inst!"); |
Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 8017 | New = new AllocaInst(NewTy, 0, AI.getAlignment(), AI.getName()); |
Chris Lattner | a2620ac | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 8018 | } |
Chris Lattner | abb77c9 | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 8019 | |
| 8020 | InsertNewInstBefore(New, AI); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8021 | |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 8022 | // Scan to the end of the allocation instructions, to skip over a block of |
| 8023 | // allocas if possible... |
| 8024 | // |
| 8025 | BasicBlock::iterator It = New; |
| 8026 | while (isa<AllocationInst>(*It)) ++It; |
| 8027 | |
| 8028 | // Now that I is pointing to the first non-allocation-inst in the block, |
| 8029 | // insert our getelementptr instruction... |
| 8030 | // |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8031 | Value *NullIdx = Constant::getNullValue(Type::Int32Ty); |
Chris Lattner | 809dfac | 2005-05-04 19:10:26 +0000 | [diff] [blame] | 8032 | Value *V = new GetElementPtrInst(New, NullIdx, NullIdx, |
| 8033 | New->getName()+".sub", It); |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 8034 | |
| 8035 | // Now make everything use the getelementptr instead of the original |
| 8036 | // allocation. |
Chris Lattner | abb77c9 | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 8037 | return ReplaceInstUsesWith(AI, V); |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8038 | } else if (isa<UndefValue>(AI.getArraySize())) { |
| 8039 | return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 8040 | } |
Chris Lattner | abb77c9 | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 8041 | |
| 8042 | // If alloca'ing a zero byte object, replace the alloca with a null pointer. |
| 8043 | // Note that we only do this for alloca's, because malloc should allocate and |
| 8044 | // return a unique pointer, even for a zero byte allocation. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8045 | if (isa<AllocaInst>(AI) && AI.getAllocatedType()->isSized() && |
Chris Lattner | 49df6ce | 2004-07-02 22:55:47 +0000 | [diff] [blame] | 8046 | TD->getTypeSize(AI.getAllocatedType()) == 0) |
Chris Lattner | abb77c9 | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 8047 | return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); |
| 8048 | |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 8049 | return 0; |
| 8050 | } |
| 8051 | |
Chris Lattner | 8427bff | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 8052 | Instruction *InstCombiner::visitFreeInst(FreeInst &FI) { |
| 8053 | Value *Op = FI.getOperand(0); |
| 8054 | |
| 8055 | // Change free <ty>* (cast <ty2>* X to <ty>*) into free <ty2>* X |
| 8056 | if (CastInst *CI = dyn_cast<CastInst>(Op)) |
| 8057 | if (isa<PointerType>(CI->getOperand(0)->getType())) { |
| 8058 | FI.setOperand(0, CI->getOperand(0)); |
| 8059 | return &FI; |
| 8060 | } |
| 8061 | |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8062 | // free undef -> unreachable. |
| 8063 | if (isa<UndefValue>(Op)) { |
| 8064 | // Insert a new store to null because we cannot modify the CFG here. |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 8065 | new StoreInst(ConstantInt::getTrue(), |
Reid Spencer | 542964f | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 8066 | UndefValue::get(PointerType::get(Type::Int1Ty)), &FI); |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8067 | return EraseInstFromFunction(FI); |
| 8068 | } |
| 8069 | |
Chris Lattner | f3a3660 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 8070 | // If we have 'free null' delete the instruction. This can happen in stl code |
| 8071 | // when lots of inlining happens. |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8072 | if (isa<ConstantPointerNull>(Op)) |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 8073 | return EraseInstFromFunction(FI); |
Chris Lattner | f3a3660 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 8074 | |
Chris Lattner | 8427bff | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 8075 | return 0; |
| 8076 | } |
| 8077 | |
| 8078 | |
Chris Lattner | 72684fe | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8079 | /// InstCombineLoadCast - Fold 'load (cast P)' -> cast (load P)' when possible. |
Chris Lattner | 35e2477 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 8080 | static Instruction *InstCombineLoadCast(InstCombiner &IC, LoadInst &LI) { |
| 8081 | User *CI = cast<User>(LI.getOperand(0)); |
Chris Lattner | fe1b0b8 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 8082 | Value *CastOp = CI->getOperand(0); |
Chris Lattner | 35e2477 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 8083 | |
| 8084 | const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType(); |
Chris Lattner | fe1b0b8 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 8085 | if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) { |
Chris Lattner | 35e2477 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 8086 | const Type *SrcPTy = SrcTy->getElementType(); |
Chris Lattner | fe1b0b8 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 8087 | |
Reid Spencer | 31a4ef4 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 8088 | if (DestPTy->isInteger() || isa<PointerType>(DestPTy) || |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 8089 | isa<VectorType>(DestPTy)) { |
Chris Lattner | fe1b0b8 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 8090 | // If the source is an array, the code below will not succeed. Check to |
| 8091 | // see if a trivial 'gep P, 0, 0' will help matters. Only do this for |
| 8092 | // constants. |
| 8093 | if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy)) |
| 8094 | if (Constant *CSrc = dyn_cast<Constant>(CastOp)) |
| 8095 | if (ASrcTy->getNumElements() != 0) { |
Chris Lattner | f96f4a8 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 8096 | Value *Idxs[2]; |
| 8097 | Idxs[0] = Idxs[1] = Constant::getNullValue(Type::Int32Ty); |
| 8098 | CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs, 2); |
Chris Lattner | fe1b0b8 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 8099 | SrcTy = cast<PointerType>(CastOp->getType()); |
| 8100 | SrcPTy = SrcTy->getElementType(); |
| 8101 | } |
| 8102 | |
Reid Spencer | 31a4ef4 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 8103 | if ((SrcPTy->isInteger() || isa<PointerType>(SrcPTy) || |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 8104 | isa<VectorType>(SrcPTy)) && |
Chris Lattner | ecfa9b5 | 2005-03-29 06:37:47 +0000 | [diff] [blame] | 8105 | // Do not allow turning this into a load of an integer, which is then |
| 8106 | // casted to a pointer, this pessimizes pointer analysis a lot. |
| 8107 | (isa<PointerType>(SrcPTy) == isa<PointerType>(LI.getType())) && |
Reid Spencer | 31a4ef4 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 8108 | IC.getTargetData().getTypeSizeInBits(SrcPTy) == |
| 8109 | IC.getTargetData().getTypeSizeInBits(DestPTy)) { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8110 | |
Chris Lattner | fe1b0b8 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 8111 | // Okay, we are casting from one integer or pointer type to another of |
| 8112 | // the same size. Instead of casting the pointer before the load, cast |
| 8113 | // the result of the loaded value. |
| 8114 | Value *NewLoad = IC.InsertNewInstBefore(new LoadInst(CastOp, |
| 8115 | CI->getName(), |
| 8116 | LI.isVolatile()),LI); |
| 8117 | // Now cast the result of the load. |
Reid Spencer | bb65ebf | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 8118 | return new BitCastInst(NewLoad, LI.getType()); |
Chris Lattner | fe1b0b8 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 8119 | } |
Chris Lattner | 35e2477 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 8120 | } |
| 8121 | } |
| 8122 | return 0; |
| 8123 | } |
| 8124 | |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 8125 | /// isSafeToLoadUnconditionally - Return true if we know that executing a load |
Chris Lattner | e6f1309 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 8126 | /// from this value cannot trap. If it is not obviously safe to load from the |
| 8127 | /// specified pointer, we do a quick local scan of the basic block containing |
| 8128 | /// ScanFrom, to determine if the address is already accessed. |
| 8129 | static bool isSafeToLoadUnconditionally(Value *V, Instruction *ScanFrom) { |
| 8130 | // If it is an alloca or global variable, it is always safe to load from. |
| 8131 | if (isa<AllocaInst>(V) || isa<GlobalVariable>(V)) return true; |
| 8132 | |
| 8133 | // Otherwise, be a little bit agressive by scanning the local block where we |
| 8134 | // want to check to see if the pointer is already being loaded or stored |
Alkis Evlogimenos | d59cebf | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 8135 | // from/to. If so, the previous load or store would have already trapped, |
| 8136 | // so there is no harm doing an extra load (also, CSE will later eliminate |
| 8137 | // the load entirely). |
Chris Lattner | e6f1309 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 8138 | BasicBlock::iterator BBI = ScanFrom, E = ScanFrom->getParent()->begin(); |
| 8139 | |
Alkis Evlogimenos | d59cebf | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 8140 | while (BBI != E) { |
Chris Lattner | e6f1309 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 8141 | --BBI; |
| 8142 | |
| 8143 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { |
| 8144 | if (LI->getOperand(0) == V) return true; |
| 8145 | } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) |
| 8146 | if (SI->getOperand(1) == V) return true; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8147 | |
Alkis Evlogimenos | d59cebf | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 8148 | } |
Chris Lattner | e6f1309 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 8149 | return false; |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 8150 | } |
| 8151 | |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 8152 | Instruction *InstCombiner::visitLoadInst(LoadInst &LI) { |
| 8153 | Value *Op = LI.getOperand(0); |
Chris Lattner | 7e8af38 | 2004-01-12 04:13:56 +0000 | [diff] [blame] | 8154 | |
Chris Lattner | a9d84e3 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 8155 | // load (cast X) --> cast (load X) iff safe |
Reid Spencer | de46e48 | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 8156 | if (isa<CastInst>(Op)) |
Chris Lattner | a9d84e3 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 8157 | if (Instruction *Res = InstCombineLoadCast(*this, LI)) |
| 8158 | return Res; |
| 8159 | |
| 8160 | // None of the following transforms are legal for volatile loads. |
| 8161 | if (LI.isVolatile()) return 0; |
Chris Lattner | b990f7d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 8162 | |
Chris Lattner | b990f7d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 8163 | if (&LI.getParent()->front() != &LI) { |
| 8164 | BasicBlock::iterator BBI = &LI; --BBI; |
Chris Lattner | e0bfdf1 | 2005-09-12 22:21:03 +0000 | [diff] [blame] | 8165 | // If the instruction immediately before this is a store to the same |
| 8166 | // address, do a simple form of store->load forwarding. |
Chris Lattner | b990f7d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 8167 | if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) |
| 8168 | if (SI->getOperand(1) == LI.getOperand(0)) |
| 8169 | return ReplaceInstUsesWith(LI, SI->getOperand(0)); |
Chris Lattner | e0bfdf1 | 2005-09-12 22:21:03 +0000 | [diff] [blame] | 8170 | if (LoadInst *LIB = dyn_cast<LoadInst>(BBI)) |
| 8171 | if (LIB->getOperand(0) == LI.getOperand(0)) |
| 8172 | return ReplaceInstUsesWith(LI, LIB); |
Chris Lattner | b990f7d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 8173 | } |
Chris Lattner | a9d84e3 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 8174 | |
| 8175 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) |
| 8176 | if (isa<ConstantPointerNull>(GEPI->getOperand(0)) || |
| 8177 | isa<UndefValue>(GEPI->getOperand(0))) { |
| 8178 | // Insert a new store to null instruction before the load to indicate |
| 8179 | // that this code is not reachable. We do this instead of inserting |
| 8180 | // an unreachable instruction directly because we cannot modify the |
| 8181 | // CFG. |
| 8182 | new StoreInst(UndefValue::get(LI.getType()), |
| 8183 | Constant::getNullValue(Op->getType()), &LI); |
| 8184 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
| 8185 | } |
| 8186 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8187 | if (Constant *C = dyn_cast<Constant>(Op)) { |
Chris Lattner | a9d84e3 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 8188 | // load null/undef -> undef |
| 8189 | if ((C->isNullValue() || isa<UndefValue>(C))) { |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8190 | // Insert a new store to null instruction before the load to indicate that |
| 8191 | // this code is not reachable. We do this instead of inserting an |
| 8192 | // unreachable instruction directly because we cannot modify the CFG. |
Chris Lattner | a9d84e3 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 8193 | new StoreInst(UndefValue::get(LI.getType()), |
| 8194 | Constant::getNullValue(Op->getType()), &LI); |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8195 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8196 | } |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 8197 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8198 | // Instcombine load (constant global) into the value loaded. |
| 8199 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op)) |
Reid Spencer | 5301e7c | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 8200 | if (GV->isConstant() && !GV->isDeclaration()) |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8201 | return ReplaceInstUsesWith(LI, GV->getInitializer()); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8202 | |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8203 | // Instcombine load (constantexpr_GEP global, 0, ...) into the value loaded. |
| 8204 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op)) |
| 8205 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 8206 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0))) |
Reid Spencer | 5301e7c | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 8207 | if (GV->isConstant() && !GV->isDeclaration()) |
Chris Lattner | 0b011ec | 2005-09-26 05:28:06 +0000 | [diff] [blame] | 8208 | if (Constant *V = |
| 8209 | ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE)) |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8210 | return ReplaceInstUsesWith(LI, V); |
Chris Lattner | a9d84e3 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 8211 | if (CE->getOperand(0)->isNullValue()) { |
| 8212 | // Insert a new store to null instruction before the load to indicate |
| 8213 | // that this code is not reachable. We do this instead of inserting |
| 8214 | // an unreachable instruction directly because we cannot modify the |
| 8215 | // CFG. |
| 8216 | new StoreInst(UndefValue::get(LI.getType()), |
| 8217 | Constant::getNullValue(Op->getType()), &LI); |
| 8218 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
| 8219 | } |
| 8220 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8221 | } else if (CE->isCast()) { |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8222 | if (Instruction *Res = InstCombineLoadCast(*this, LI)) |
| 8223 | return Res; |
| 8224 | } |
| 8225 | } |
Chris Lattner | e228ee5 | 2004-04-08 20:39:49 +0000 | [diff] [blame] | 8226 | |
Chris Lattner | a9d84e3 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 8227 | if (Op->hasOneUse()) { |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 8228 | // Change select and PHI nodes to select values instead of addresses: this |
| 8229 | // helps alias analysis out a lot, allows many others simplifications, and |
| 8230 | // exposes redundancy in the code. |
| 8231 | // |
| 8232 | // Note that we cannot do the transformation unless we know that the |
| 8233 | // introduced loads cannot trap! Something like this is valid as long as |
| 8234 | // the condition is always false: load (select bool %C, int* null, int* %G), |
| 8235 | // but it would not be valid if we transformed it to load from null |
| 8236 | // unconditionally. |
| 8237 | // |
| 8238 | if (SelectInst *SI = dyn_cast<SelectInst>(Op)) { |
| 8239 | // load (select (Cond, &V1, &V2)) --> select(Cond, load &V1, load &V2). |
Chris Lattner | e6f1309 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 8240 | if (isSafeToLoadUnconditionally(SI->getOperand(1), SI) && |
| 8241 | isSafeToLoadUnconditionally(SI->getOperand(2), SI)) { |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 8242 | Value *V1 = InsertNewInstBefore(new LoadInst(SI->getOperand(1), |
Chris Lattner | 4261855 | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 8243 | SI->getOperand(1)->getName()+".val"), LI); |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 8244 | Value *V2 = InsertNewInstBefore(new LoadInst(SI->getOperand(2), |
Chris Lattner | 4261855 | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 8245 | SI->getOperand(2)->getName()+".val"), LI); |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 8246 | return new SelectInst(SI->getCondition(), V1, V2); |
| 8247 | } |
| 8248 | |
Chris Lattner | bdcf41a | 2004-09-23 15:46:00 +0000 | [diff] [blame] | 8249 | // load (select (cond, null, P)) -> load P |
| 8250 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(1))) |
| 8251 | if (C->isNullValue()) { |
| 8252 | LI.setOperand(0, SI->getOperand(2)); |
| 8253 | return &LI; |
| 8254 | } |
| 8255 | |
| 8256 | // load (select (cond, P, null)) -> load P |
| 8257 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(2))) |
| 8258 | if (C->isNullValue()) { |
| 8259 | LI.setOperand(0, SI->getOperand(1)); |
| 8260 | return &LI; |
| 8261 | } |
Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 8262 | } |
| 8263 | } |
Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 8264 | return 0; |
| 8265 | } |
| 8266 | |
Reid Spencer | e928a15 | 2007-01-19 21:20:31 +0000 | [diff] [blame] | 8267 | /// InstCombineStoreToCast - Fold store V, (cast P) -> store (cast V), P |
Chris Lattner | 72684fe | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8268 | /// when possible. |
| 8269 | static Instruction *InstCombineStoreToCast(InstCombiner &IC, StoreInst &SI) { |
| 8270 | User *CI = cast<User>(SI.getOperand(1)); |
| 8271 | Value *CastOp = CI->getOperand(0); |
| 8272 | |
| 8273 | const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType(); |
| 8274 | if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) { |
| 8275 | const Type *SrcPTy = SrcTy->getElementType(); |
| 8276 | |
Reid Spencer | 31a4ef4 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 8277 | if (DestPTy->isInteger() || isa<PointerType>(DestPTy)) { |
Chris Lattner | 72684fe | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8278 | // If the source is an array, the code below will not succeed. Check to |
| 8279 | // see if a trivial 'gep P, 0, 0' will help matters. Only do this for |
| 8280 | // constants. |
| 8281 | if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy)) |
| 8282 | if (Constant *CSrc = dyn_cast<Constant>(CastOp)) |
| 8283 | if (ASrcTy->getNumElements() != 0) { |
Chris Lattner | f96f4a8 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 8284 | Value* Idxs[2]; |
| 8285 | Idxs[0] = Idxs[1] = Constant::getNullValue(Type::Int32Ty); |
| 8286 | CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs, 2); |
Chris Lattner | 72684fe | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8287 | SrcTy = cast<PointerType>(CastOp->getType()); |
| 8288 | SrcPTy = SrcTy->getElementType(); |
| 8289 | } |
| 8290 | |
Reid Spencer | 9a4bed0 | 2007-01-20 23:35:48 +0000 | [diff] [blame] | 8291 | if ((SrcPTy->isInteger() || isa<PointerType>(SrcPTy)) && |
| 8292 | IC.getTargetData().getTypeSizeInBits(SrcPTy) == |
| 8293 | IC.getTargetData().getTypeSizeInBits(DestPTy)) { |
Chris Lattner | 72684fe | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8294 | |
| 8295 | // Okay, we are casting from one integer or pointer type to another of |
Reid Spencer | c050af9 | 2007-01-18 18:54:33 +0000 | [diff] [blame] | 8296 | // the same size. Instead of casting the pointer before |
| 8297 | // the store, cast the value to be stored. |
Chris Lattner | 72684fe | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8298 | Value *NewCast; |
Reid Spencer | bb65ebf | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 8299 | Value *SIOp0 = SI.getOperand(0); |
Reid Spencer | c050af9 | 2007-01-18 18:54:33 +0000 | [diff] [blame] | 8300 | Instruction::CastOps opcode = Instruction::BitCast; |
| 8301 | const Type* CastSrcTy = SIOp0->getType(); |
| 8302 | const Type* CastDstTy = SrcPTy; |
| 8303 | if (isa<PointerType>(CastDstTy)) { |
| 8304 | if (CastSrcTy->isInteger()) |
Reid Spencer | bb65ebf | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 8305 | opcode = Instruction::IntToPtr; |
Reid Spencer | 9a4bed0 | 2007-01-20 23:35:48 +0000 | [diff] [blame] | 8306 | } else if (isa<IntegerType>(CastDstTy)) { |
Reid Spencer | 74a528b | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 8307 | if (isa<PointerType>(SIOp0->getType())) |
Reid Spencer | bb65ebf | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 8308 | opcode = Instruction::PtrToInt; |
| 8309 | } |
| 8310 | if (Constant *C = dyn_cast<Constant>(SIOp0)) |
Reid Spencer | c050af9 | 2007-01-18 18:54:33 +0000 | [diff] [blame] | 8311 | NewCast = ConstantExpr::getCast(opcode, C, CastDstTy); |
Chris Lattner | 72684fe | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8312 | else |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8313 | NewCast = IC.InsertNewInstBefore( |
Reid Spencer | c050af9 | 2007-01-18 18:54:33 +0000 | [diff] [blame] | 8314 | CastInst::create(opcode, SIOp0, CastDstTy, SIOp0->getName()+".c"), |
| 8315 | SI); |
Chris Lattner | 72684fe | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8316 | return new StoreInst(NewCast, CastOp); |
| 8317 | } |
| 8318 | } |
| 8319 | } |
| 8320 | return 0; |
| 8321 | } |
| 8322 | |
Chris Lattner | 31f486c | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 8323 | Instruction *InstCombiner::visitStoreInst(StoreInst &SI) { |
| 8324 | Value *Val = SI.getOperand(0); |
| 8325 | Value *Ptr = SI.getOperand(1); |
| 8326 | |
| 8327 | if (isa<UndefValue>(Ptr)) { // store X, undef -> noop (even if volatile) |
Chris Lattner | 5997cf9 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 8328 | EraseInstFromFunction(SI); |
Chris Lattner | 31f486c | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 8329 | ++NumCombined; |
| 8330 | return 0; |
| 8331 | } |
Chris Lattner | a4beeef | 2007-01-15 06:51:56 +0000 | [diff] [blame] | 8332 | |
| 8333 | // If the RHS is an alloca with a single use, zapify the store, making the |
| 8334 | // alloca dead. |
| 8335 | if (Ptr->hasOneUse()) { |
| 8336 | if (isa<AllocaInst>(Ptr)) { |
| 8337 | EraseInstFromFunction(SI); |
| 8338 | ++NumCombined; |
| 8339 | return 0; |
| 8340 | } |
| 8341 | |
| 8342 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) |
| 8343 | if (isa<AllocaInst>(GEP->getOperand(0)) && |
| 8344 | GEP->getOperand(0)->hasOneUse()) { |
| 8345 | EraseInstFromFunction(SI); |
| 8346 | ++NumCombined; |
| 8347 | return 0; |
| 8348 | } |
| 8349 | } |
Chris Lattner | 31f486c | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 8350 | |
Chris Lattner | 5997cf9 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 8351 | // Do really simple DSE, to catch cases where there are several consequtive |
| 8352 | // stores to the same location, separated by a few arithmetic operations. This |
| 8353 | // situation often occurs with bitfield accesses. |
| 8354 | BasicBlock::iterator BBI = &SI; |
| 8355 | for (unsigned ScanInsts = 6; BBI != SI.getParent()->begin() && ScanInsts; |
| 8356 | --ScanInsts) { |
| 8357 | --BBI; |
| 8358 | |
| 8359 | if (StoreInst *PrevSI = dyn_cast<StoreInst>(BBI)) { |
| 8360 | // Prev store isn't volatile, and stores to the same location? |
| 8361 | if (!PrevSI->isVolatile() && PrevSI->getOperand(1) == SI.getOperand(1)) { |
| 8362 | ++NumDeadStore; |
| 8363 | ++BBI; |
| 8364 | EraseInstFromFunction(*PrevSI); |
| 8365 | continue; |
| 8366 | } |
| 8367 | break; |
| 8368 | } |
| 8369 | |
Chris Lattner | dab43b2 | 2006-05-26 19:19:20 +0000 | [diff] [blame] | 8370 | // If this is a load, we have to stop. However, if the loaded value is from |
| 8371 | // the pointer we're loading and is producing the pointer we're storing, |
| 8372 | // then *this* store is dead (X = load P; store X -> P). |
| 8373 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { |
| 8374 | if (LI == Val && LI->getOperand(0) == Ptr) { |
| 8375 | EraseInstFromFunction(SI); |
| 8376 | ++NumCombined; |
| 8377 | return 0; |
| 8378 | } |
| 8379 | // Otherwise, this is a load from some other location. Stores before it |
| 8380 | // may not be dead. |
| 8381 | break; |
| 8382 | } |
| 8383 | |
Chris Lattner | 5997cf9 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 8384 | // Don't skip over loads or things that can modify memory. |
Chris Lattner | dab43b2 | 2006-05-26 19:19:20 +0000 | [diff] [blame] | 8385 | if (BBI->mayWriteToMemory()) |
Chris Lattner | 5997cf9 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 8386 | break; |
| 8387 | } |
| 8388 | |
| 8389 | |
| 8390 | if (SI.isVolatile()) return 0; // Don't hack volatile stores. |
Chris Lattner | 31f486c | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 8391 | |
| 8392 | // store X, null -> turns into 'unreachable' in SimplifyCFG |
| 8393 | if (isa<ConstantPointerNull>(Ptr)) { |
| 8394 | if (!isa<UndefValue>(Val)) { |
| 8395 | SI.setOperand(0, UndefValue::get(Val->getType())); |
| 8396 | if (Instruction *U = dyn_cast<Instruction>(Val)) |
Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 8397 | AddToWorkList(U); // Dropped a use. |
Chris Lattner | 31f486c | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 8398 | ++NumCombined; |
| 8399 | } |
| 8400 | return 0; // Do not modify these! |
| 8401 | } |
| 8402 | |
| 8403 | // store undef, Ptr -> noop |
| 8404 | if (isa<UndefValue>(Val)) { |
Chris Lattner | 5997cf9 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 8405 | EraseInstFromFunction(SI); |
Chris Lattner | 31f486c | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 8406 | ++NumCombined; |
| 8407 | return 0; |
| 8408 | } |
| 8409 | |
Chris Lattner | 72684fe | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8410 | // If the pointer destination is a cast, see if we can fold the cast into the |
| 8411 | // source instead. |
Reid Spencer | de46e48 | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 8412 | if (isa<CastInst>(Ptr)) |
Chris Lattner | 72684fe | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8413 | if (Instruction *Res = InstCombineStoreToCast(*this, SI)) |
| 8414 | return Res; |
| 8415 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8416 | if (CE->isCast()) |
Chris Lattner | 72684fe | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8417 | if (Instruction *Res = InstCombineStoreToCast(*this, SI)) |
| 8418 | return Res; |
| 8419 | |
Chris Lattner | 219175c | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 8420 | |
| 8421 | // If this store is the last instruction in the basic block, and if the block |
| 8422 | // ends with an unconditional branch, try to move it to the successor block. |
Chris Lattner | 5997cf9 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 8423 | BBI = &SI; ++BBI; |
Chris Lattner | 219175c | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 8424 | if (BranchInst *BI = dyn_cast<BranchInst>(BBI)) |
| 8425 | if (BI->isUnconditional()) { |
| 8426 | // Check to see if the successor block has exactly two incoming edges. If |
| 8427 | // so, see if the other predecessor contains a store to the same location. |
| 8428 | // if so, insert a PHI node (if needed) and move the stores down. |
| 8429 | BasicBlock *Dest = BI->getSuccessor(0); |
| 8430 | |
| 8431 | pred_iterator PI = pred_begin(Dest); |
| 8432 | BasicBlock *Other = 0; |
| 8433 | if (*PI != BI->getParent()) |
| 8434 | Other = *PI; |
| 8435 | ++PI; |
| 8436 | if (PI != pred_end(Dest)) { |
| 8437 | if (*PI != BI->getParent()) |
| 8438 | if (Other) |
| 8439 | Other = 0; |
| 8440 | else |
| 8441 | Other = *PI; |
| 8442 | if (++PI != pred_end(Dest)) |
| 8443 | Other = 0; |
| 8444 | } |
| 8445 | if (Other) { // If only one other pred... |
| 8446 | BBI = Other->getTerminator(); |
| 8447 | // Make sure this other block ends in an unconditional branch and that |
| 8448 | // there is an instruction before the branch. |
| 8449 | if (isa<BranchInst>(BBI) && cast<BranchInst>(BBI)->isUnconditional() && |
| 8450 | BBI != Other->begin()) { |
| 8451 | --BBI; |
| 8452 | StoreInst *OtherStore = dyn_cast<StoreInst>(BBI); |
| 8453 | |
| 8454 | // If this instruction is a store to the same location. |
| 8455 | if (OtherStore && OtherStore->getOperand(1) == SI.getOperand(1)) { |
| 8456 | // Okay, we know we can perform this transformation. Insert a PHI |
| 8457 | // node now if we need it. |
| 8458 | Value *MergedVal = OtherStore->getOperand(0); |
| 8459 | if (MergedVal != SI.getOperand(0)) { |
| 8460 | PHINode *PN = new PHINode(MergedVal->getType(), "storemerge"); |
| 8461 | PN->reserveOperandSpace(2); |
| 8462 | PN->addIncoming(SI.getOperand(0), SI.getParent()); |
| 8463 | PN->addIncoming(OtherStore->getOperand(0), Other); |
| 8464 | MergedVal = InsertNewInstBefore(PN, Dest->front()); |
| 8465 | } |
| 8466 | |
| 8467 | // Advance to a place where it is safe to insert the new store and |
| 8468 | // insert it. |
| 8469 | BBI = Dest->begin(); |
| 8470 | while (isa<PHINode>(BBI)) ++BBI; |
| 8471 | InsertNewInstBefore(new StoreInst(MergedVal, SI.getOperand(1), |
| 8472 | OtherStore->isVolatile()), *BBI); |
| 8473 | |
| 8474 | // Nuke the old stores. |
Chris Lattner | 5997cf9 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 8475 | EraseInstFromFunction(SI); |
| 8476 | EraseInstFromFunction(*OtherStore); |
Chris Lattner | 219175c | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 8477 | ++NumCombined; |
| 8478 | return 0; |
| 8479 | } |
| 8480 | } |
| 8481 | } |
| 8482 | } |
| 8483 | |
Chris Lattner | 31f486c | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 8484 | return 0; |
| 8485 | } |
| 8486 | |
| 8487 | |
Chris Lattner | 9eef8a7 | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 8488 | Instruction *InstCombiner::visitBranchInst(BranchInst &BI) { |
| 8489 | // Change br (not X), label True, label False to: br X, label False, True |
Reid Spencer | 4fdd96c | 2005-06-18 17:37:34 +0000 | [diff] [blame] | 8490 | Value *X = 0; |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 8491 | BasicBlock *TrueDest; |
| 8492 | BasicBlock *FalseDest; |
| 8493 | if (match(&BI, m_Br(m_Not(m_Value(X)), TrueDest, FalseDest)) && |
| 8494 | !isa<Constant>(X)) { |
| 8495 | // Swap Destinations and condition... |
| 8496 | BI.setCondition(X); |
| 8497 | BI.setSuccessor(0, FalseDest); |
| 8498 | BI.setSuccessor(1, TrueDest); |
| 8499 | return &BI; |
| 8500 | } |
| 8501 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8502 | // Cannonicalize fcmp_one -> fcmp_oeq |
| 8503 | FCmpInst::Predicate FPred; Value *Y; |
| 8504 | if (match(&BI, m_Br(m_FCmp(FPred, m_Value(X), m_Value(Y)), |
| 8505 | TrueDest, FalseDest))) |
| 8506 | if ((FPred == FCmpInst::FCMP_ONE || FPred == FCmpInst::FCMP_OLE || |
| 8507 | FPred == FCmpInst::FCMP_OGE) && BI.getCondition()->hasOneUse()) { |
| 8508 | FCmpInst *I = cast<FCmpInst>(BI.getCondition()); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8509 | FCmpInst::Predicate NewPred = FCmpInst::getInversePredicate(FPred); |
Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 8510 | Instruction *NewSCC = new FCmpInst(NewPred, X, Y, "", I); |
| 8511 | NewSCC->takeName(I); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8512 | // Swap Destinations and condition... |
| 8513 | BI.setCondition(NewSCC); |
| 8514 | BI.setSuccessor(0, FalseDest); |
| 8515 | BI.setSuccessor(1, TrueDest); |
Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 8516 | RemoveFromWorkList(I); |
Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 8517 | I->eraseFromParent(); |
Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 8518 | AddToWorkList(NewSCC); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8519 | return &BI; |
| 8520 | } |
| 8521 | |
| 8522 | // Cannonicalize icmp_ne -> icmp_eq |
| 8523 | ICmpInst::Predicate IPred; |
| 8524 | if (match(&BI, m_Br(m_ICmp(IPred, m_Value(X), m_Value(Y)), |
| 8525 | TrueDest, FalseDest))) |
| 8526 | if ((IPred == ICmpInst::ICMP_NE || IPred == ICmpInst::ICMP_ULE || |
| 8527 | IPred == ICmpInst::ICMP_SLE || IPred == ICmpInst::ICMP_UGE || |
| 8528 | IPred == ICmpInst::ICMP_SGE) && BI.getCondition()->hasOneUse()) { |
| 8529 | ICmpInst *I = cast<ICmpInst>(BI.getCondition()); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8530 | ICmpInst::Predicate NewPred = ICmpInst::getInversePredicate(IPred); |
Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 8531 | Instruction *NewSCC = new ICmpInst(NewPred, X, Y, "", I); |
| 8532 | NewSCC->takeName(I); |
Chris Lattner | e967b34 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 8533 | // Swap Destinations and condition... |
Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 8534 | BI.setCondition(NewSCC); |
Chris Lattner | e967b34 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 8535 | BI.setSuccessor(0, FalseDest); |
| 8536 | BI.setSuccessor(1, TrueDest); |
Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 8537 | RemoveFromWorkList(I); |
Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 8538 | I->eraseFromParent();; |
Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 8539 | AddToWorkList(NewSCC); |
Chris Lattner | e967b34 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 8540 | return &BI; |
| 8541 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8542 | |
Chris Lattner | 9eef8a7 | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 8543 | return 0; |
| 8544 | } |
Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 8545 | |
Chris Lattner | 4c9c20a | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 8546 | Instruction *InstCombiner::visitSwitchInst(SwitchInst &SI) { |
| 8547 | Value *Cond = SI.getCondition(); |
| 8548 | if (Instruction *I = dyn_cast<Instruction>(Cond)) { |
| 8549 | if (I->getOpcode() == Instruction::Add) |
| 8550 | if (ConstantInt *AddRHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 8551 | // change 'switch (X+4) case 1:' into 'switch (X) case -3' |
| 8552 | for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2) |
Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8553 | SI.setOperand(i,ConstantExpr::getSub(cast<Constant>(SI.getOperand(i)), |
Chris Lattner | 4c9c20a | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 8554 | AddRHS)); |
| 8555 | SI.setOperand(0, I->getOperand(0)); |
Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 8556 | AddToWorkList(I); |
Chris Lattner | 4c9c20a | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 8557 | return &SI; |
| 8558 | } |
| 8559 | } |
| 8560 | return 0; |
| 8561 | } |
| 8562 | |
Chris Lattner | 6bc9865 | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 8563 | /// CheapToScalarize - Return true if the value is cheaper to scalarize than it |
| 8564 | /// is to leave as a vector operation. |
| 8565 | static bool CheapToScalarize(Value *V, bool isConstant) { |
| 8566 | if (isa<ConstantAggregateZero>(V)) |
| 8567 | return true; |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 8568 | if (ConstantVector *C = dyn_cast<ConstantVector>(V)) { |
Chris Lattner | 6bc9865 | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 8569 | if (isConstant) return true; |
| 8570 | // If all elts are the same, we can extract. |
| 8571 | Constant *Op0 = C->getOperand(0); |
| 8572 | for (unsigned i = 1; i < C->getNumOperands(); ++i) |
| 8573 | if (C->getOperand(i) != Op0) |
| 8574 | return false; |
| 8575 | return true; |
| 8576 | } |
| 8577 | Instruction *I = dyn_cast<Instruction>(V); |
| 8578 | if (!I) return false; |
| 8579 | |
| 8580 | // Insert element gets simplified to the inserted element or is deleted if |
| 8581 | // this is constant idx extract element and its a constant idx insertelt. |
| 8582 | if (I->getOpcode() == Instruction::InsertElement && isConstant && |
| 8583 | isa<ConstantInt>(I->getOperand(2))) |
| 8584 | return true; |
| 8585 | if (I->getOpcode() == Instruction::Load && I->hasOneUse()) |
| 8586 | return true; |
| 8587 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) |
| 8588 | if (BO->hasOneUse() && |
| 8589 | (CheapToScalarize(BO->getOperand(0), isConstant) || |
| 8590 | CheapToScalarize(BO->getOperand(1), isConstant))) |
| 8591 | return true; |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8592 | if (CmpInst *CI = dyn_cast<CmpInst>(I)) |
| 8593 | if (CI->hasOneUse() && |
| 8594 | (CheapToScalarize(CI->getOperand(0), isConstant) || |
| 8595 | CheapToScalarize(CI->getOperand(1), isConstant))) |
| 8596 | return true; |
Chris Lattner | 6bc9865 | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 8597 | |
| 8598 | return false; |
| 8599 | } |
| 8600 | |
Chris Lattner | 945e437 | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 8601 | /// Read and decode a shufflevector mask. |
| 8602 | /// |
| 8603 | /// It turns undef elements into values that are larger than the number of |
| 8604 | /// elements in the input. |
Chris Lattner | 12249be | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8605 | static std::vector<unsigned> getShuffleMask(const ShuffleVectorInst *SVI) { |
| 8606 | unsigned NElts = SVI->getType()->getNumElements(); |
| 8607 | if (isa<ConstantAggregateZero>(SVI->getOperand(2))) |
| 8608 | return std::vector<unsigned>(NElts, 0); |
| 8609 | if (isa<UndefValue>(SVI->getOperand(2))) |
| 8610 | return std::vector<unsigned>(NElts, 2*NElts); |
| 8611 | |
| 8612 | std::vector<unsigned> Result; |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 8613 | const ConstantVector *CP = cast<ConstantVector>(SVI->getOperand(2)); |
Chris Lattner | 12249be | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8614 | for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) |
| 8615 | if (isa<UndefValue>(CP->getOperand(i))) |
| 8616 | Result.push_back(NElts*2); // undef -> 8 |
| 8617 | else |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8618 | Result.push_back(cast<ConstantInt>(CP->getOperand(i))->getZExtValue()); |
Chris Lattner | 12249be | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8619 | return Result; |
| 8620 | } |
| 8621 | |
Chris Lattner | 8d1d8d3 | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8622 | /// FindScalarElement - Given a vector and an element number, see if the scalar |
| 8623 | /// value is already around as a register, for example if it were inserted then |
| 8624 | /// extracted from the vector. |
| 8625 | static Value *FindScalarElement(Value *V, unsigned EltNo) { |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 8626 | assert(isa<VectorType>(V->getType()) && "Not looking at a vector?"); |
| 8627 | const VectorType *PTy = cast<VectorType>(V->getType()); |
Chris Lattner | 2d37f92 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 8628 | unsigned Width = PTy->getNumElements(); |
| 8629 | if (EltNo >= Width) // Out of range access. |
Chris Lattner | 8d1d8d3 | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8630 | return UndefValue::get(PTy->getElementType()); |
| 8631 | |
| 8632 | if (isa<UndefValue>(V)) |
| 8633 | return UndefValue::get(PTy->getElementType()); |
| 8634 | else if (isa<ConstantAggregateZero>(V)) |
| 8635 | return Constant::getNullValue(PTy->getElementType()); |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 8636 | else if (ConstantVector *CP = dyn_cast<ConstantVector>(V)) |
Chris Lattner | 8d1d8d3 | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8637 | return CP->getOperand(EltNo); |
| 8638 | else if (InsertElementInst *III = dyn_cast<InsertElementInst>(V)) { |
| 8639 | // If this is an insert to a variable element, we don't know what it is. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8640 | if (!isa<ConstantInt>(III->getOperand(2))) |
| 8641 | return 0; |
| 8642 | unsigned IIElt = cast<ConstantInt>(III->getOperand(2))->getZExtValue(); |
Chris Lattner | 8d1d8d3 | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8643 | |
| 8644 | // If this is an insert to the element we are looking for, return the |
| 8645 | // inserted value. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8646 | if (EltNo == IIElt) |
| 8647 | return III->getOperand(1); |
Chris Lattner | 8d1d8d3 | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8648 | |
| 8649 | // Otherwise, the insertelement doesn't modify the value, recurse on its |
| 8650 | // vector input. |
| 8651 | return FindScalarElement(III->getOperand(0), EltNo); |
Chris Lattner | 2d37f92 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 8652 | } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(V)) { |
Chris Lattner | 12249be | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8653 | unsigned InEl = getShuffleMask(SVI)[EltNo]; |
| 8654 | if (InEl < Width) |
| 8655 | return FindScalarElement(SVI->getOperand(0), InEl); |
| 8656 | else if (InEl < Width*2) |
| 8657 | return FindScalarElement(SVI->getOperand(1), InEl - Width); |
| 8658 | else |
| 8659 | return UndefValue::get(PTy->getElementType()); |
Chris Lattner | 8d1d8d3 | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8660 | } |
| 8661 | |
| 8662 | // Otherwise, we don't know. |
| 8663 | return 0; |
| 8664 | } |
| 8665 | |
Robert Bocchino | a835296 | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 8666 | Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) { |
Chris Lattner | 8d1d8d3 | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8667 | |
Chris Lattner | 92346c3 | 2006-03-31 18:25:14 +0000 | [diff] [blame] | 8668 | // If packed val is undef, replace extract with scalar undef. |
| 8669 | if (isa<UndefValue>(EI.getOperand(0))) |
| 8670 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); |
| 8671 | |
| 8672 | // If packed val is constant 0, replace extract with scalar 0. |
| 8673 | if (isa<ConstantAggregateZero>(EI.getOperand(0))) |
| 8674 | return ReplaceInstUsesWith(EI, Constant::getNullValue(EI.getType())); |
| 8675 | |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 8676 | if (ConstantVector *C = dyn_cast<ConstantVector>(EI.getOperand(0))) { |
Robert Bocchino | a835296 | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 8677 | // If packed val is constant with uniform operands, replace EI |
| 8678 | // with that operand |
Chris Lattner | 6bc9865 | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 8679 | Constant *op0 = C->getOperand(0); |
Robert Bocchino | a835296 | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 8680 | for (unsigned i = 1; i < C->getNumOperands(); ++i) |
Chris Lattner | 6bc9865 | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 8681 | if (C->getOperand(i) != op0) { |
| 8682 | op0 = 0; |
| 8683 | break; |
| 8684 | } |
| 8685 | if (op0) |
| 8686 | return ReplaceInstUsesWith(EI, op0); |
Robert Bocchino | a835296 | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 8687 | } |
Chris Lattner | 6bc9865 | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 8688 | |
Chris Lattner | 8d1d8d3 | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8689 | // If extracting a specified index from the vector, see if we can recursively |
| 8690 | // find a previously computed scalar that was inserted into the vector. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8691 | if (ConstantInt *IdxC = dyn_cast<ConstantInt>(EI.getOperand(1))) { |
Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 8692 | // This instruction only demands the single element from the input vector. |
| 8693 | // If the input vector has a single use, simplify it based on this use |
| 8694 | // property. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8695 | uint64_t IndexVal = IdxC->getZExtValue(); |
Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 8696 | if (EI.getOperand(0)->hasOneUse()) { |
| 8697 | uint64_t UndefElts; |
| 8698 | if (Value *V = SimplifyDemandedVectorElts(EI.getOperand(0), |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8699 | 1 << IndexVal, |
Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 8700 | UndefElts)) { |
| 8701 | EI.setOperand(0, V); |
| 8702 | return &EI; |
| 8703 | } |
| 8704 | } |
| 8705 | |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8706 | if (Value *Elt = FindScalarElement(EI.getOperand(0), IndexVal)) |
Chris Lattner | 8d1d8d3 | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8707 | return ReplaceInstUsesWith(EI, Elt); |
Chris Lattner | 2d37f92 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 8708 | } |
Chris Lattner | 8d1d8d3 | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8709 | |
Chris Lattner | 83f6578 | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 8710 | if (Instruction *I = dyn_cast<Instruction>(EI.getOperand(0))) { |
Robert Bocchino | a835296 | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 8711 | if (I->hasOneUse()) { |
| 8712 | // Push extractelement into predecessor operation if legal and |
| 8713 | // profitable to do so |
| 8714 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) { |
Chris Lattner | 6bc9865 | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 8715 | bool isConstantElt = isa<ConstantInt>(EI.getOperand(1)); |
| 8716 | if (CheapToScalarize(BO, isConstantElt)) { |
| 8717 | ExtractElementInst *newEI0 = |
| 8718 | new ExtractElementInst(BO->getOperand(0), EI.getOperand(1), |
| 8719 | EI.getName()+".lhs"); |
| 8720 | ExtractElementInst *newEI1 = |
| 8721 | new ExtractElementInst(BO->getOperand(1), EI.getOperand(1), |
| 8722 | EI.getName()+".rhs"); |
| 8723 | InsertNewInstBefore(newEI0, EI); |
| 8724 | InsertNewInstBefore(newEI1, EI); |
| 8725 | return BinaryOperator::create(BO->getOpcode(), newEI0, newEI1); |
| 8726 | } |
Reid Spencer | de46e48 | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 8727 | } else if (isa<LoadInst>(I)) { |
Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 8728 | Value *Ptr = InsertCastBefore(Instruction::BitCast, I->getOperand(0), |
Robert Bocchino | a835296 | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 8729 | PointerType::get(EI.getType()), EI); |
| 8730 | GetElementPtrInst *GEP = |
Reid Spencer | a736fdf | 2006-11-29 01:11:01 +0000 | [diff] [blame] | 8731 | new GetElementPtrInst(Ptr, EI.getOperand(1), I->getName() + ".gep"); |
Robert Bocchino | a835296 | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 8732 | InsertNewInstBefore(GEP, EI); |
| 8733 | return new LoadInst(GEP); |
Chris Lattner | 83f6578 | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 8734 | } |
| 8735 | } |
| 8736 | if (InsertElementInst *IE = dyn_cast<InsertElementInst>(I)) { |
| 8737 | // Extracting the inserted element? |
| 8738 | if (IE->getOperand(2) == EI.getOperand(1)) |
| 8739 | return ReplaceInstUsesWith(EI, IE->getOperand(1)); |
| 8740 | // If the inserted and extracted elements are constants, they must not |
| 8741 | // be the same value, extract from the pre-inserted value instead. |
| 8742 | if (isa<Constant>(IE->getOperand(2)) && |
| 8743 | isa<Constant>(EI.getOperand(1))) { |
| 8744 | AddUsesToWorkList(EI); |
| 8745 | EI.setOperand(0, IE->getOperand(0)); |
| 8746 | return &EI; |
| 8747 | } |
| 8748 | } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(I)) { |
| 8749 | // If this is extracting an element from a shufflevector, figure out where |
| 8750 | // it came from and extract from the appropriate input element instead. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8751 | if (ConstantInt *Elt = dyn_cast<ConstantInt>(EI.getOperand(1))) { |
| 8752 | unsigned SrcIdx = getShuffleMask(SVI)[Elt->getZExtValue()]; |
Chris Lattner | 12249be | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8753 | Value *Src; |
| 8754 | if (SrcIdx < SVI->getType()->getNumElements()) |
| 8755 | Src = SVI->getOperand(0); |
| 8756 | else if (SrcIdx < SVI->getType()->getNumElements()*2) { |
| 8757 | SrcIdx -= SVI->getType()->getNumElements(); |
| 8758 | Src = SVI->getOperand(1); |
| 8759 | } else { |
| 8760 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); |
Chris Lattner | 612fa8e | 2006-03-30 22:02:40 +0000 | [diff] [blame] | 8761 | } |
Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 8762 | return new ExtractElementInst(Src, SrcIdx); |
Robert Bocchino | a835296 | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 8763 | } |
| 8764 | } |
Chris Lattner | 83f6578 | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 8765 | } |
Robert Bocchino | a835296 | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 8766 | return 0; |
| 8767 | } |
| 8768 | |
Chris Lattner | 9095186 | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8769 | /// CollectSingleShuffleElements - If V is a shuffle of values that ONLY returns |
| 8770 | /// elements from either LHS or RHS, return the shuffle mask and true. |
| 8771 | /// Otherwise, return false. |
| 8772 | static bool CollectSingleShuffleElements(Value *V, Value *LHS, Value *RHS, |
| 8773 | std::vector<Constant*> &Mask) { |
| 8774 | assert(V->getType() == LHS->getType() && V->getType() == RHS->getType() && |
| 8775 | "Invalid CollectSingleShuffleElements"); |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 8776 | unsigned NumElts = cast<VectorType>(V->getType())->getNumElements(); |
Chris Lattner | 9095186 | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8777 | |
| 8778 | if (isa<UndefValue>(V)) { |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8779 | Mask.assign(NumElts, UndefValue::get(Type::Int32Ty)); |
Chris Lattner | 9095186 | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8780 | return true; |
| 8781 | } else if (V == LHS) { |
| 8782 | for (unsigned i = 0; i != NumElts; ++i) |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8783 | Mask.push_back(ConstantInt::get(Type::Int32Ty, i)); |
Chris Lattner | 9095186 | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8784 | return true; |
| 8785 | } else if (V == RHS) { |
| 8786 | for (unsigned i = 0; i != NumElts; ++i) |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8787 | Mask.push_back(ConstantInt::get(Type::Int32Ty, i+NumElts)); |
Chris Lattner | 9095186 | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8788 | return true; |
| 8789 | } else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) { |
| 8790 | // If this is an insert of an extract from some other vector, include it. |
| 8791 | Value *VecOp = IEI->getOperand(0); |
| 8792 | Value *ScalarOp = IEI->getOperand(1); |
| 8793 | Value *IdxOp = IEI->getOperand(2); |
| 8794 | |
Chris Lattner | b6cb64b | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 8795 | if (!isa<ConstantInt>(IdxOp)) |
| 8796 | return false; |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8797 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Chris Lattner | b6cb64b | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 8798 | |
| 8799 | if (isa<UndefValue>(ScalarOp)) { // inserting undef into vector. |
| 8800 | // Okay, we can handle this if the vector we are insertinting into is |
| 8801 | // transitively ok. |
| 8802 | if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) { |
| 8803 | // If so, update the mask to reflect the inserted undef. |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8804 | Mask[InsertedIdx] = UndefValue::get(Type::Int32Ty); |
Chris Lattner | b6cb64b | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 8805 | return true; |
| 8806 | } |
| 8807 | } else if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)){ |
| 8808 | if (isa<ConstantInt>(EI->getOperand(1)) && |
Chris Lattner | 9095186 | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8809 | EI->getOperand(0)->getType() == V->getType()) { |
| 8810 | unsigned ExtractedIdx = |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8811 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
Chris Lattner | 9095186 | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8812 | |
| 8813 | // This must be extracting from either LHS or RHS. |
| 8814 | if (EI->getOperand(0) == LHS || EI->getOperand(0) == RHS) { |
| 8815 | // Okay, we can handle this if the vector we are insertinting into is |
| 8816 | // transitively ok. |
| 8817 | if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) { |
| 8818 | // If so, update the mask to reflect the inserted value. |
| 8819 | if (EI->getOperand(0) == LHS) { |
| 8820 | Mask[InsertedIdx & (NumElts-1)] = |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8821 | ConstantInt::get(Type::Int32Ty, ExtractedIdx); |
Chris Lattner | 9095186 | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8822 | } else { |
| 8823 | assert(EI->getOperand(0) == RHS); |
| 8824 | Mask[InsertedIdx & (NumElts-1)] = |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8825 | ConstantInt::get(Type::Int32Ty, ExtractedIdx+NumElts); |
Chris Lattner | 9095186 | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8826 | |
| 8827 | } |
| 8828 | return true; |
| 8829 | } |
| 8830 | } |
| 8831 | } |
| 8832 | } |
| 8833 | } |
| 8834 | // TODO: Handle shufflevector here! |
| 8835 | |
| 8836 | return false; |
| 8837 | } |
| 8838 | |
| 8839 | /// CollectShuffleElements - We are building a shuffle of V, using RHS as the |
| 8840 | /// RHS of the shuffle instruction, if it is not null. Return a shuffle mask |
| 8841 | /// that computes V and the LHS value of the shuffle. |
Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8842 | static Value *CollectShuffleElements(Value *V, std::vector<Constant*> &Mask, |
Chris Lattner | 9095186 | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8843 | Value *&RHS) { |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 8844 | assert(isa<VectorType>(V->getType()) && |
Chris Lattner | 9095186 | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8845 | (RHS == 0 || V->getType() == RHS->getType()) && |
Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8846 | "Invalid shuffle!"); |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 8847 | unsigned NumElts = cast<VectorType>(V->getType())->getNumElements(); |
Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8848 | |
| 8849 | if (isa<UndefValue>(V)) { |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8850 | Mask.assign(NumElts, UndefValue::get(Type::Int32Ty)); |
Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8851 | return V; |
| 8852 | } else if (isa<ConstantAggregateZero>(V)) { |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8853 | Mask.assign(NumElts, ConstantInt::get(Type::Int32Ty, 0)); |
Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8854 | return V; |
| 8855 | } else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) { |
| 8856 | // If this is an insert of an extract from some other vector, include it. |
| 8857 | Value *VecOp = IEI->getOperand(0); |
| 8858 | Value *ScalarOp = IEI->getOperand(1); |
| 8859 | Value *IdxOp = IEI->getOperand(2); |
| 8860 | |
| 8861 | if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) { |
| 8862 | if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) && |
| 8863 | EI->getOperand(0)->getType() == V->getType()) { |
| 8864 | unsigned ExtractedIdx = |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8865 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
| 8866 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8867 | |
| 8868 | // Either the extracted from or inserted into vector must be RHSVec, |
| 8869 | // otherwise we'd end up with a shuffle of three inputs. |
Chris Lattner | 9095186 | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8870 | if (EI->getOperand(0) == RHS || RHS == 0) { |
| 8871 | RHS = EI->getOperand(0); |
| 8872 | Value *V = CollectShuffleElements(VecOp, Mask, RHS); |
Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8873 | Mask[InsertedIdx & (NumElts-1)] = |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8874 | ConstantInt::get(Type::Int32Ty, NumElts+ExtractedIdx); |
Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8875 | return V; |
| 8876 | } |
| 8877 | |
Chris Lattner | 9095186 | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8878 | if (VecOp == RHS) { |
| 8879 | Value *V = CollectShuffleElements(EI->getOperand(0), Mask, RHS); |
Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8880 | // Everything but the extracted element is replaced with the RHS. |
| 8881 | for (unsigned i = 0; i != NumElts; ++i) { |
| 8882 | if (i != InsertedIdx) |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8883 | Mask[i] = ConstantInt::get(Type::Int32Ty, NumElts+i); |
Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8884 | } |
| 8885 | return V; |
| 8886 | } |
Chris Lattner | 9095186 | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8887 | |
| 8888 | // If this insertelement is a chain that comes from exactly these two |
| 8889 | // vectors, return the vector and the effective shuffle. |
| 8890 | if (CollectSingleShuffleElements(IEI, EI->getOperand(0), RHS, Mask)) |
| 8891 | return EI->getOperand(0); |
| 8892 | |
Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8893 | } |
| 8894 | } |
| 8895 | } |
Chris Lattner | 9095186 | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8896 | // TODO: Handle shufflevector here! |
Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8897 | |
| 8898 | // Otherwise, can't do anything fancy. Return an identity vector. |
| 8899 | for (unsigned i = 0; i != NumElts; ++i) |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8900 | Mask.push_back(ConstantInt::get(Type::Int32Ty, i)); |
Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8901 | return V; |
| 8902 | } |
| 8903 | |
| 8904 | Instruction *InstCombiner::visitInsertElementInst(InsertElementInst &IE) { |
| 8905 | Value *VecOp = IE.getOperand(0); |
| 8906 | Value *ScalarOp = IE.getOperand(1); |
| 8907 | Value *IdxOp = IE.getOperand(2); |
| 8908 | |
| 8909 | // If the inserted element was extracted from some other vector, and if the |
| 8910 | // indexes are constant, try to turn this into a shufflevector operation. |
| 8911 | if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) { |
| 8912 | if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) && |
| 8913 | EI->getOperand(0)->getType() == IE.getType()) { |
| 8914 | unsigned NumVectorElts = IE.getType()->getNumElements(); |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8915 | unsigned ExtractedIdx=cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
| 8916 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8917 | |
| 8918 | if (ExtractedIdx >= NumVectorElts) // Out of range extract. |
| 8919 | return ReplaceInstUsesWith(IE, VecOp); |
| 8920 | |
| 8921 | if (InsertedIdx >= NumVectorElts) // Out of range insert. |
| 8922 | return ReplaceInstUsesWith(IE, UndefValue::get(IE.getType())); |
| 8923 | |
| 8924 | // If we are extracting a value from a vector, then inserting it right |
| 8925 | // back into the same place, just use the input vector. |
| 8926 | if (EI->getOperand(0) == VecOp && ExtractedIdx == InsertedIdx) |
| 8927 | return ReplaceInstUsesWith(IE, VecOp); |
| 8928 | |
| 8929 | // We could theoretically do this for ANY input. However, doing so could |
| 8930 | // turn chains of insertelement instructions into a chain of shufflevector |
| 8931 | // instructions, and right now we do not merge shufflevectors. As such, |
| 8932 | // only do this in a situation where it is clear that there is benefit. |
| 8933 | if (isa<UndefValue>(VecOp) || isa<ConstantAggregateZero>(VecOp)) { |
| 8934 | // Turn this into shuffle(EIOp0, VecOp, Mask). The result has all of |
| 8935 | // the values of VecOp, except then one read from EIOp0. |
| 8936 | // Build a new shuffle mask. |
| 8937 | std::vector<Constant*> Mask; |
| 8938 | if (isa<UndefValue>(VecOp)) |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8939 | Mask.assign(NumVectorElts, UndefValue::get(Type::Int32Ty)); |
Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8940 | else { |
| 8941 | assert(isa<ConstantAggregateZero>(VecOp) && "Unknown thing"); |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8942 | Mask.assign(NumVectorElts, ConstantInt::get(Type::Int32Ty, |
Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8943 | NumVectorElts)); |
| 8944 | } |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8945 | Mask[InsertedIdx] = ConstantInt::get(Type::Int32Ty, ExtractedIdx); |
Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8946 | return new ShuffleVectorInst(EI->getOperand(0), VecOp, |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 8947 | ConstantVector::get(Mask)); |
Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8948 | } |
| 8949 | |
| 8950 | // If this insertelement isn't used by some other insertelement, turn it |
| 8951 | // (and any insertelements it points to), into one big shuffle. |
| 8952 | if (!IE.hasOneUse() || !isa<InsertElementInst>(IE.use_back())) { |
| 8953 | std::vector<Constant*> Mask; |
Chris Lattner | 9095186 | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8954 | Value *RHS = 0; |
| 8955 | Value *LHS = CollectShuffleElements(&IE, Mask, RHS); |
| 8956 | if (RHS == 0) RHS = UndefValue::get(LHS->getType()); |
| 8957 | // We now have a shuffle of LHS, RHS, Mask. |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 8958 | return new ShuffleVectorInst(LHS, RHS, ConstantVector::get(Mask)); |
Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8959 | } |
| 8960 | } |
| 8961 | } |
| 8962 | |
| 8963 | return 0; |
| 8964 | } |
| 8965 | |
| 8966 | |
Chris Lattner | fbb77a4 | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 8967 | Instruction *InstCombiner::visitShuffleVectorInst(ShuffleVectorInst &SVI) { |
| 8968 | Value *LHS = SVI.getOperand(0); |
| 8969 | Value *RHS = SVI.getOperand(1); |
Chris Lattner | 12249be | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8970 | std::vector<unsigned> Mask = getShuffleMask(&SVI); |
Chris Lattner | fbb77a4 | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 8971 | |
| 8972 | bool MadeChange = false; |
| 8973 | |
Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 8974 | // Undefined shuffle mask -> undefined value. |
Chris Lattner | 12249be | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8975 | if (isa<UndefValue>(SVI.getOperand(2))) |
Chris Lattner | fbb77a4 | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 8976 | return ReplaceInstUsesWith(SVI, UndefValue::get(SVI.getType())); |
| 8977 | |
Chris Lattner | d7b6ea1 | 2007-01-05 07:36:08 +0000 | [diff] [blame] | 8978 | // If we have shuffle(x, undef, mask) and any elements of mask refer to |
Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8979 | // the undef, change them to undefs. |
Chris Lattner | d7b6ea1 | 2007-01-05 07:36:08 +0000 | [diff] [blame] | 8980 | if (isa<UndefValue>(SVI.getOperand(1))) { |
| 8981 | // Scan to see if there are any references to the RHS. If so, replace them |
| 8982 | // with undef element refs and set MadeChange to true. |
| 8983 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
| 8984 | if (Mask[i] >= e && Mask[i] != 2*e) { |
| 8985 | Mask[i] = 2*e; |
| 8986 | MadeChange = true; |
| 8987 | } |
| 8988 | } |
| 8989 | |
| 8990 | if (MadeChange) { |
| 8991 | // Remap any references to RHS to use LHS. |
| 8992 | std::vector<Constant*> Elts; |
| 8993 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
| 8994 | if (Mask[i] == 2*e) |
| 8995 | Elts.push_back(UndefValue::get(Type::Int32Ty)); |
| 8996 | else |
| 8997 | Elts.push_back(ConstantInt::get(Type::Int32Ty, Mask[i])); |
| 8998 | } |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 8999 | SVI.setOperand(2, ConstantVector::get(Elts)); |
Chris Lattner | d7b6ea1 | 2007-01-05 07:36:08 +0000 | [diff] [blame] | 9000 | } |
| 9001 | } |
Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 9002 | |
Chris Lattner | 12249be | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 9003 | // Canonicalize shuffle(x ,x,mask) -> shuffle(x, undef,mask') |
| 9004 | // Canonicalize shuffle(undef,x,mask) -> shuffle(x, undef,mask'). |
| 9005 | if (LHS == RHS || isa<UndefValue>(LHS)) { |
| 9006 | if (isa<UndefValue>(LHS) && LHS == RHS) { |
Chris Lattner | fbb77a4 | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 9007 | // shuffle(undef,undef,mask) -> undef. |
| 9008 | return ReplaceInstUsesWith(SVI, LHS); |
| 9009 | } |
| 9010 | |
Chris Lattner | 12249be | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 9011 | // Remap any references to RHS to use LHS. |
| 9012 | std::vector<Constant*> Elts; |
| 9013 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
Chris Lattner | 0e47716 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 9014 | if (Mask[i] >= 2*e) |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9015 | Elts.push_back(UndefValue::get(Type::Int32Ty)); |
Chris Lattner | 0e47716 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 9016 | else { |
| 9017 | if ((Mask[i] >= e && isa<UndefValue>(RHS)) || |
| 9018 | (Mask[i] < e && isa<UndefValue>(LHS))) |
| 9019 | Mask[i] = 2*e; // Turn into undef. |
| 9020 | else |
| 9021 | Mask[i] &= (e-1); // Force to LHS. |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9022 | Elts.push_back(ConstantInt::get(Type::Int32Ty, Mask[i])); |
Chris Lattner | 0e47716 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 9023 | } |
Chris Lattner | fbb77a4 | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 9024 | } |
Chris Lattner | 12249be | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 9025 | SVI.setOperand(0, SVI.getOperand(1)); |
Chris Lattner | fbb77a4 | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 9026 | SVI.setOperand(1, UndefValue::get(RHS->getType())); |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 9027 | SVI.setOperand(2, ConstantVector::get(Elts)); |
Chris Lattner | 0e47716 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 9028 | LHS = SVI.getOperand(0); |
| 9029 | RHS = SVI.getOperand(1); |
Chris Lattner | fbb77a4 | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 9030 | MadeChange = true; |
| 9031 | } |
| 9032 | |
Chris Lattner | 0e47716 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 9033 | // Analyze the shuffle, are the LHS or RHS and identity shuffles? |
Chris Lattner | 12249be | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 9034 | bool isLHSID = true, isRHSID = true; |
Chris Lattner | 34cebe7 | 2006-04-16 00:03:56 +0000 | [diff] [blame] | 9035 | |
Chris Lattner | 12249be | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 9036 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
| 9037 | if (Mask[i] >= e*2) continue; // Ignore undef values. |
| 9038 | // Is this an identity shuffle of the LHS value? |
| 9039 | isLHSID &= (Mask[i] == i); |
| 9040 | |
| 9041 | // Is this an identity shuffle of the RHS value? |
| 9042 | isRHSID &= (Mask[i]-e == i); |
Chris Lattner | 34cebe7 | 2006-04-16 00:03:56 +0000 | [diff] [blame] | 9043 | } |
Chris Lattner | fbb77a4 | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 9044 | |
Chris Lattner | 12249be | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 9045 | // Eliminate identity shuffles. |
| 9046 | if (isLHSID) return ReplaceInstUsesWith(SVI, LHS); |
| 9047 | if (isRHSID) return ReplaceInstUsesWith(SVI, RHS); |
Chris Lattner | fbb77a4 | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 9048 | |
Chris Lattner | 0e47716 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 9049 | // If the LHS is a shufflevector itself, see if we can combine it with this |
| 9050 | // one without producing an unusual shuffle. Here we are really conservative: |
| 9051 | // we are absolutely afraid of producing a shuffle mask not in the input |
| 9052 | // program, because the code gen may not be smart enough to turn a merged |
| 9053 | // shuffle into two specific shuffles: it may produce worse code. As such, |
| 9054 | // we only merge two shuffles if the result is one of the two input shuffle |
| 9055 | // masks. In this case, merging the shuffles just removes one instruction, |
| 9056 | // which we know is safe. This is good for things like turning: |
| 9057 | // (splat(splat)) -> splat. |
| 9058 | if (ShuffleVectorInst *LHSSVI = dyn_cast<ShuffleVectorInst>(LHS)) { |
| 9059 | if (isa<UndefValue>(RHS)) { |
| 9060 | std::vector<unsigned> LHSMask = getShuffleMask(LHSSVI); |
| 9061 | |
| 9062 | std::vector<unsigned> NewMask; |
| 9063 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) |
| 9064 | if (Mask[i] >= 2*e) |
| 9065 | NewMask.push_back(2*e); |
| 9066 | else |
| 9067 | NewMask.push_back(LHSMask[Mask[i]]); |
| 9068 | |
| 9069 | // If the result mask is equal to the src shuffle or this shuffle mask, do |
| 9070 | // the replacement. |
| 9071 | if (NewMask == LHSMask || NewMask == Mask) { |
| 9072 | std::vector<Constant*> Elts; |
| 9073 | for (unsigned i = 0, e = NewMask.size(); i != e; ++i) { |
| 9074 | if (NewMask[i] >= e*2) { |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9075 | Elts.push_back(UndefValue::get(Type::Int32Ty)); |
Chris Lattner | 0e47716 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 9076 | } else { |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9077 | Elts.push_back(ConstantInt::get(Type::Int32Ty, NewMask[i])); |
Chris Lattner | 0e47716 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 9078 | } |
| 9079 | } |
| 9080 | return new ShuffleVectorInst(LHSSVI->getOperand(0), |
| 9081 | LHSSVI->getOperand(1), |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 9082 | ConstantVector::get(Elts)); |
Chris Lattner | 0e47716 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 9083 | } |
| 9084 | } |
| 9085 | } |
Chris Lattner | 4284f64 | 2007-01-30 22:32:46 +0000 | [diff] [blame] | 9086 | |
Chris Lattner | fbb77a4 | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 9087 | return MadeChange ? &SVI : 0; |
| 9088 | } |
| 9089 | |
| 9090 | |
Robert Bocchino | a835296 | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 9091 | |
Chris Lattner | 39c98bb | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 9092 | |
| 9093 | /// TryToSinkInstruction - Try to move the specified instruction from its |
| 9094 | /// current block into the beginning of DestBlock, which can only happen if it's |
| 9095 | /// safe to move the instruction past all of the instructions between it and the |
| 9096 | /// end of its block. |
| 9097 | static bool TryToSinkInstruction(Instruction *I, BasicBlock *DestBlock) { |
| 9098 | assert(I->hasOneUse() && "Invariants didn't hold!"); |
| 9099 | |
Chris Lattner | c4f67e6 | 2005-10-27 17:13:11 +0000 | [diff] [blame] | 9100 | // Cannot move control-flow-involving, volatile loads, vaarg, etc. |
| 9101 | if (isa<PHINode>(I) || I->mayWriteToMemory()) return false; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9102 | |
Chris Lattner | 39c98bb | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 9103 | // Do not sink alloca instructions out of the entry block. |
| 9104 | if (isa<AllocaInst>(I) && I->getParent() == &DestBlock->getParent()->front()) |
| 9105 | return false; |
| 9106 | |
Chris Lattner | f17a2fb | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 9107 | // We can only sink load instructions if there is nothing between the load and |
| 9108 | // the end of block that could change the value. |
| 9109 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Chris Lattner | f17a2fb | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 9110 | for (BasicBlock::iterator Scan = LI, E = LI->getParent()->end(); |
| 9111 | Scan != E; ++Scan) |
| 9112 | if (Scan->mayWriteToMemory()) |
| 9113 | return false; |
Chris Lattner | f17a2fb | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 9114 | } |
Chris Lattner | 39c98bb | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 9115 | |
| 9116 | BasicBlock::iterator InsertPos = DestBlock->begin(); |
| 9117 | while (isa<PHINode>(InsertPos)) ++InsertPos; |
| 9118 | |
Chris Lattner | 9f269e4 | 2005-08-08 19:11:57 +0000 | [diff] [blame] | 9119 | I->moveBefore(InsertPos); |
Chris Lattner | 39c98bb | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 9120 | ++NumSunkInst; |
| 9121 | return true; |
| 9122 | } |
| 9123 | |
Chris Lattner | a36ee4e | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9124 | |
| 9125 | /// AddReachableCodeToWorklist - Walk the function in depth-first order, adding |
| 9126 | /// all reachable code to the worklist. |
| 9127 | /// |
| 9128 | /// This has a couple of tricks to make the code faster and more powerful. In |
| 9129 | /// particular, we constant fold and DCE instructions as we go, to avoid adding |
| 9130 | /// them to the worklist (this significantly speeds up instcombine on code where |
| 9131 | /// many instructions are dead or constant). Additionally, if we find a branch |
| 9132 | /// whose condition is a known constant, we only visit the reachable successors. |
| 9133 | /// |
| 9134 | static void AddReachableCodeToWorklist(BasicBlock *BB, |
Chris Lattner | 7907e5f | 2007-02-15 19:41:52 +0000 | [diff] [blame] | 9135 | SmallPtrSet<BasicBlock*, 64> &Visited, |
Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9136 | InstCombiner &IC, |
Chris Lattner | 1443bc5 | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 9137 | const TargetData *TD) { |
Chris Lattner | a36ee4e | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9138 | // We have now visited this block! If we've already been here, bail out. |
Chris Lattner | 7907e5f | 2007-02-15 19:41:52 +0000 | [diff] [blame] | 9139 | if (!Visited.insert(BB)) return; |
Chris Lattner | a36ee4e | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9140 | |
| 9141 | for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ) { |
| 9142 | Instruction *Inst = BBI++; |
| 9143 | |
| 9144 | // DCE instruction if trivially dead. |
| 9145 | if (isInstructionTriviallyDead(Inst)) { |
| 9146 | ++NumDeadInst; |
Bill Wendling | 5dbf43c | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 9147 | DOUT << "IC: DCE: " << *Inst; |
Chris Lattner | a36ee4e | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9148 | Inst->eraseFromParent(); |
| 9149 | continue; |
| 9150 | } |
| 9151 | |
| 9152 | // ConstantProp instruction if trivially constant. |
Chris Lattner | e3eda25 | 2007-01-30 23:16:15 +0000 | [diff] [blame] | 9153 | if (Constant *C = ConstantFoldInstruction(Inst, TD)) { |
Bill Wendling | 5dbf43c | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 9154 | DOUT << "IC: ConstFold to: " << *C << " from: " << *Inst; |
Chris Lattner | a36ee4e | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9155 | Inst->replaceAllUsesWith(C); |
| 9156 | ++NumConstProp; |
| 9157 | Inst->eraseFromParent(); |
| 9158 | continue; |
| 9159 | } |
| 9160 | |
Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9161 | IC.AddToWorkList(Inst); |
Chris Lattner | a36ee4e | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9162 | } |
| 9163 | |
| 9164 | // Recursively visit successors. If this is a branch or switch on a constant, |
| 9165 | // only visit the reachable successor. |
| 9166 | TerminatorInst *TI = BB->getTerminator(); |
| 9167 | if (BranchInst *BI = dyn_cast<BranchInst>(TI)) { |
Reid Spencer | 7a9c62b | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 9168 | if (BI->isConditional() && isa<ConstantInt>(BI->getCondition())) { |
Reid Spencer | cddc9df | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 9169 | bool CondVal = cast<ConstantInt>(BI->getCondition())->getZExtValue(); |
Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9170 | AddReachableCodeToWorklist(BI->getSuccessor(!CondVal), Visited, IC, TD); |
Chris Lattner | a36ee4e | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9171 | return; |
| 9172 | } |
| 9173 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) { |
| 9174 | if (ConstantInt *Cond = dyn_cast<ConstantInt>(SI->getCondition())) { |
| 9175 | // See if this is an explicit destination. |
| 9176 | for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i) |
| 9177 | if (SI->getCaseValue(i) == Cond) { |
Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9178 | AddReachableCodeToWorklist(SI->getSuccessor(i), Visited, IC, TD); |
Chris Lattner | a36ee4e | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9179 | return; |
| 9180 | } |
| 9181 | |
| 9182 | // Otherwise it is the default destination. |
Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9183 | AddReachableCodeToWorklist(SI->getSuccessor(0), Visited, IC, TD); |
Chris Lattner | a36ee4e | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9184 | return; |
| 9185 | } |
| 9186 | } |
| 9187 | |
| 9188 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) |
Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9189 | AddReachableCodeToWorklist(TI->getSuccessor(i), Visited, IC, TD); |
Chris Lattner | a36ee4e | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9190 | } |
| 9191 | |
Chris Lattner | 960a543 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 9192 | bool InstCombiner::DoOneIteration(Function &F, unsigned Iteration) { |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 9193 | bool Changed = false; |
Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 9194 | TD = &getAnalysis<TargetData>(); |
Chris Lattner | 960a543 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 9195 | |
| 9196 | DEBUG(DOUT << "\n\nINSTCOMBINE ITERATION #" << Iteration << " on " |
| 9197 | << F.getNameStr() << "\n"); |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9198 | |
Chris Lattner | 4ed40f7 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 9199 | { |
Chris Lattner | a36ee4e | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9200 | // Do a depth-first traversal of the function, populate the worklist with |
| 9201 | // the reachable instructions. Ignore blocks that are not reachable. Keep |
| 9202 | // track of which blocks we visit. |
Chris Lattner | 7907e5f | 2007-02-15 19:41:52 +0000 | [diff] [blame] | 9203 | SmallPtrSet<BasicBlock*, 64> Visited; |
Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9204 | AddReachableCodeToWorklist(F.begin(), Visited, *this, TD); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 9205 | |
Chris Lattner | 4ed40f7 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 9206 | // Do a quick scan over the function. If we find any blocks that are |
| 9207 | // unreachable, remove any instructions inside of them. This prevents |
| 9208 | // the instcombine code from having to deal with some bad special cases. |
| 9209 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 9210 | if (!Visited.count(BB)) { |
| 9211 | Instruction *Term = BB->getTerminator(); |
| 9212 | while (Term != BB->begin()) { // Remove instrs bottom-up |
| 9213 | BasicBlock::iterator I = Term; --I; |
Chris Lattner | 2d3a7a6 | 2004-04-27 15:13:33 +0000 | [diff] [blame] | 9214 | |
Bill Wendling | 5dbf43c | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 9215 | DOUT << "IC: DCE: " << *I; |
Chris Lattner | 4ed40f7 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 9216 | ++NumDeadInst; |
| 9217 | |
| 9218 | if (!I->use_empty()) |
| 9219 | I->replaceAllUsesWith(UndefValue::get(I->getType())); |
| 9220 | I->eraseFromParent(); |
| 9221 | } |
| 9222 | } |
| 9223 | } |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9224 | |
Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9225 | while (!Worklist.empty()) { |
| 9226 | Instruction *I = RemoveOneFromWorkList(); |
| 9227 | if (I == 0) continue; // skip null values. |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9228 | |
Chris Lattner | 1443bc5 | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 9229 | // Check to see if we can DCE the instruction. |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 9230 | if (isInstructionTriviallyDead(I)) { |
Chris Lattner | 1443bc5 | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 9231 | // Add operands to the worklist. |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9232 | if (I->getNumOperands() < 4) |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 9233 | AddUsesToWorkList(*I); |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 9234 | ++NumDeadInst; |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9235 | |
Bill Wendling | 5dbf43c | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 9236 | DOUT << "IC: DCE: " << *I; |
Chris Lattner | cd517ff | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 9237 | |
| 9238 | I->eraseFromParent(); |
Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9239 | RemoveFromWorkList(I); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9240 | continue; |
| 9241 | } |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 9242 | |
Chris Lattner | 1443bc5 | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 9243 | // Instruction isn't dead, see if we can constant propagate it. |
Chris Lattner | e3eda25 | 2007-01-30 23:16:15 +0000 | [diff] [blame] | 9244 | if (Constant *C = ConstantFoldInstruction(I, TD)) { |
Bill Wendling | 5dbf43c | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 9245 | DOUT << "IC: ConstFold to: " << *C << " from: " << *I; |
Chris Lattner | cd517ff | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 9246 | |
Chris Lattner | 1443bc5 | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 9247 | // Add operands to the worklist. |
Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 9248 | AddUsesToWorkList(*I); |
Chris Lattner | c6509f4 | 2002-12-05 22:41:53 +0000 | [diff] [blame] | 9249 | ReplaceInstUsesWith(*I, C); |
| 9250 | |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 9251 | ++NumConstProp; |
Chris Lattner | a36ee4e | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9252 | I->eraseFromParent(); |
Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9253 | RemoveFromWorkList(I); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9254 | continue; |
Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 9255 | } |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9256 | |
Chris Lattner | 39c98bb | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 9257 | // See if we can trivially sink this instruction to a successor basic block. |
| 9258 | if (I->hasOneUse()) { |
| 9259 | BasicBlock *BB = I->getParent(); |
| 9260 | BasicBlock *UserParent = cast<Instruction>(I->use_back())->getParent(); |
| 9261 | if (UserParent != BB) { |
| 9262 | bool UserIsSuccessor = false; |
| 9263 | // See if the user is one of our successors. |
| 9264 | for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI) |
| 9265 | if (*SI == UserParent) { |
| 9266 | UserIsSuccessor = true; |
| 9267 | break; |
| 9268 | } |
| 9269 | |
| 9270 | // If the user is one of our immediate successors, and if that successor |
| 9271 | // only has us as a predecessors (we'd have to split the critical edge |
| 9272 | // otherwise), we can keep going. |
| 9273 | if (UserIsSuccessor && !isa<PHINode>(I->use_back()) && |
| 9274 | next(pred_begin(UserParent)) == pred_end(UserParent)) |
| 9275 | // Okay, the CFG is simple enough, try to sink this instruction. |
| 9276 | Changed |= TryToSinkInstruction(I, UserParent); |
| 9277 | } |
| 9278 | } |
| 9279 | |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9280 | // Now that we have an instruction, try combining it to simplify it... |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9281 | if (Instruction *Result = visit(*I)) { |
Chris Lattner | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 9282 | ++NumCombined; |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 9283 | // Should we replace the old instruction with a new one? |
Chris Lattner | 053c093 | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 9284 | if (Result != I) { |
Bill Wendling | 5dbf43c | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 9285 | DOUT << "IC: Old = " << *I |
| 9286 | << " New = " << *Result; |
Chris Lattner | 7d2a539 | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 9287 | |
Chris Lattner | 396dbfe | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 9288 | // Everything uses the new instruction now. |
| 9289 | I->replaceAllUsesWith(Result); |
| 9290 | |
| 9291 | // Push the new instruction and any users onto the worklist. |
Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9292 | AddToWorkList(Result); |
Chris Lattner | 396dbfe | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 9293 | AddUsersToWorkList(*Result); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9294 | |
Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 9295 | // Move the name to the new instruction first. |
| 9296 | Result->takeName(I); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9297 | |
| 9298 | // Insert the new instruction into the basic block... |
| 9299 | BasicBlock *InstParent = I->getParent(); |
Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9300 | BasicBlock::iterator InsertPos = I; |
| 9301 | |
| 9302 | if (!isa<PHINode>(Result)) // If combining a PHI, don't insert |
| 9303 | while (isa<PHINode>(InsertPos)) // middle of a block of PHIs. |
| 9304 | ++InsertPos; |
| 9305 | |
| 9306 | InstParent->getInstList().insert(InsertPos, Result); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9307 | |
Chris Lattner | 63d75af | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 9308 | // Make sure that we reprocess all operands now that we reduced their |
| 9309 | // use counts. |
Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9310 | AddUsesToWorkList(*I); |
Chris Lattner | b643a9e | 2004-05-01 23:19:52 +0000 | [diff] [blame] | 9311 | |
Chris Lattner | 396dbfe | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 9312 | // Instructions can end up on the worklist more than once. Make sure |
| 9313 | // we do not process an instruction that has been deleted. |
Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9314 | RemoveFromWorkList(I); |
Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9315 | |
| 9316 | // Erase the old instruction. |
| 9317 | InstParent->getInstList().erase(I); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 9318 | } else { |
Bill Wendling | 5dbf43c | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 9319 | DOUT << "IC: MOD = " << *I; |
Chris Lattner | 7d2a539 | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 9320 | |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9321 | // If the instruction was modified, it's possible that it is now dead. |
| 9322 | // if so, remove it. |
Chris Lattner | 63d75af | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 9323 | if (isInstructionTriviallyDead(I)) { |
| 9324 | // Make sure we process all operands now that we are reducing their |
| 9325 | // use counts. |
Chris Lattner | 960a543 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 9326 | AddUsesToWorkList(*I); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9327 | |
Chris Lattner | 63d75af | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 9328 | // Instructions may end up in the worklist more than once. Erase all |
Robert Bocchino | a835296 | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 9329 | // occurrences of this instruction. |
Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9330 | RemoveFromWorkList(I); |
Chris Lattner | 31f486c | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 9331 | I->eraseFromParent(); |
Chris Lattner | 396dbfe | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 9332 | } else { |
Chris Lattner | 960a543 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 9333 | AddToWorkList(I); |
| 9334 | AddUsersToWorkList(*I); |
Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9335 | } |
Chris Lattner | 053c093 | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 9336 | } |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 9337 | Changed = true; |
Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9338 | } |
| 9339 | } |
| 9340 | |
Chris Lattner | 960a543 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 9341 | assert(WorklistMap.empty() && "Worklist empty, but map not?"); |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 9342 | return Changed; |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 9343 | } |
| 9344 | |
Chris Lattner | 960a543 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 9345 | |
| 9346 | bool InstCombiner::runOnFunction(Function &F) { |
Chris Lattner | 8258b44 | 2007-03-04 04:27:24 +0000 | [diff] [blame] | 9347 | MustPreserveLCSSA = mustPreserveAnalysisID(LCSSAID); |
| 9348 | |
Chris Lattner | 960a543 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 9349 | bool EverMadeChange = false; |
| 9350 | |
| 9351 | // Iterate while there is work to do. |
| 9352 | unsigned Iteration = 0; |
| 9353 | while (DoOneIteration(F, Iteration++)) |
| 9354 | EverMadeChange = true; |
| 9355 | return EverMadeChange; |
| 9356 | } |
| 9357 | |
Brian Gaeke | 38b79e8 | 2004-07-27 17:43:21 +0000 | [diff] [blame] | 9358 | FunctionPass *llvm::createInstructionCombiningPass() { |
Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 9359 | return new InstCombiner(); |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 9360 | } |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 9361 | |