Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1 | //===- InstructionCombining.cpp - Combine multiple instructions -----------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9 | // |
| 10 | // InstructionCombining - Combine instructions to form fewer, simple |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 11 | // instructions. This pass does not modify the CFG This pass is where algebraic |
| 12 | // simplification happens. |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 13 | // |
| 14 | // This pass combines things like: |
Chris Lattner | 32ed46b | 2004-05-04 15:19:33 +0000 | [diff] [blame] | 15 | // %Y = add int %X, 1 |
| 16 | // %Z = add int %Y, 1 |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 17 | // into: |
Chris Lattner | 32ed46b | 2004-05-04 15:19:33 +0000 | [diff] [blame] | 18 | // %Z = add int %X, 2 |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 19 | // |
| 20 | // This is a simple worklist driven algorithm. |
| 21 | // |
Chris Lattner | 065a616 | 2003-09-10 05:29:43 +0000 | [diff] [blame] | 22 | // This pass guarantees that the following canonicalizations are performed on |
Chris Lattner | 2cd9196 | 2003-07-23 21:41:57 +0000 | [diff] [blame] | 23 | // the program: |
| 24 | // 1. If a binary operator has a constant operand, it is moved to the RHS |
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 25 | // 2. Bitwise operators with constant operands are always grouped so that |
| 26 | // shifts are performed first, then or's, then and's, then xor's. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 27 | // 3. Compare instructions are converted from <,>,<=,>= to ==,!= if possible |
| 28 | // 4. All cmp instructions on boolean values are replaced with logical ops |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 29 | // 5. add X, X is represented as (X*2) => (X << 1) |
| 30 | // 6. Multiplies with a power-of-two constant argument are transformed into |
| 31 | // shifts. |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 32 | // ... etc. |
Chris Lattner | 2cd9196 | 2003-07-23 21:41:57 +0000 | [diff] [blame] | 33 | // |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 34 | //===----------------------------------------------------------------------===// |
| 35 | |
Chris Lattner | 0cea42a | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 36 | #define DEBUG_TYPE "instcombine" |
Chris Lattner | 022103b | 2002-05-07 20:03:00 +0000 | [diff] [blame] | 37 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 38 | #include "llvm/IntrinsicInst.h" |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 39 | #include "llvm/Pass.h" |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 40 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 41 | #include "llvm/GlobalVariable.h" |
Chris Lattner | 79066fa | 2007-01-30 23:46:24 +0000 | [diff] [blame] | 42 | #include "llvm/Analysis/ConstantFolding.h" |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 43 | #include "llvm/Target/TargetData.h" |
| 44 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 45 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 46 | #include "llvm/Support/CallSite.h" |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 47 | #include "llvm/Support/Debug.h" |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 48 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 49 | #include "llvm/Support/InstVisitor.h" |
Chris Lattner | bcd7db5 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 50 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 51 | #include "llvm/Support/PatternMatch.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 52 | #include "llvm/Support/Compiler.h" |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 53 | #include "llvm/ADT/SmallVector.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 54 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 55 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | b3bc8fa | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 56 | #include <algorithm> |
Reid Spencer | 6734b57 | 2007-02-04 00:40:42 +0000 | [diff] [blame] | 57 | #include <set> |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 58 | using namespace llvm; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 59 | using namespace llvm::PatternMatch; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 60 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 61 | STATISTIC(NumCombined , "Number of insts combined"); |
| 62 | STATISTIC(NumConstProp, "Number of constant folds"); |
| 63 | STATISTIC(NumDeadInst , "Number of dead inst eliminated"); |
| 64 | STATISTIC(NumDeadStore, "Number of dead stores eliminated"); |
| 65 | STATISTIC(NumSunkInst , "Number of instructions sunk"); |
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 66 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 67 | namespace { |
Chris Lattner | f4b5461 | 2006-06-28 22:08:15 +0000 | [diff] [blame] | 68 | class VISIBILITY_HIDDEN InstCombiner |
| 69 | : public FunctionPass, |
| 70 | public InstVisitor<InstCombiner, Instruction*> { |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 71 | // Worklist of all of the instructions that need to be simplified. |
| 72 | std::vector<Instruction*> WorkList; |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 73 | TargetData *TD; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 74 | |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 75 | /// AddUsersToWorkList - When an instruction is simplified, add all users of |
| 76 | /// the instruction to the work lists because they might get more simplified |
| 77 | /// now. |
| 78 | /// |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 79 | void AddUsersToWorkList(Value &I) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 80 | for (Value::use_iterator UI = I.use_begin(), UE = I.use_end(); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 81 | UI != UE; ++UI) |
| 82 | WorkList.push_back(cast<Instruction>(*UI)); |
| 83 | } |
| 84 | |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 85 | /// AddUsesToWorkList - When an instruction is simplified, add operands to |
| 86 | /// the work lists because they might get more simplified now. |
| 87 | /// |
| 88 | void AddUsesToWorkList(Instruction &I) { |
| 89 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) |
| 90 | if (Instruction *Op = dyn_cast<Instruction>(I.getOperand(i))) |
| 91 | WorkList.push_back(Op); |
| 92 | } |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 93 | |
| 94 | /// AddSoonDeadInstToWorklist - The specified instruction is about to become |
| 95 | /// dead. Add all of its operands to the worklist, turning them into |
| 96 | /// undef's to reduce the number of uses of those instructions. |
| 97 | /// |
| 98 | /// Return the specified operand before it is turned into an undef. |
| 99 | /// |
| 100 | Value *AddSoonDeadInstToWorklist(Instruction &I, unsigned op) { |
| 101 | Value *R = I.getOperand(op); |
| 102 | |
| 103 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) |
| 104 | if (Instruction *Op = dyn_cast<Instruction>(I.getOperand(i))) { |
| 105 | WorkList.push_back(Op); |
| 106 | // Set the operand to undef to drop the use. |
| 107 | I.setOperand(i, UndefValue::get(Op->getType())); |
| 108 | } |
| 109 | |
| 110 | return R; |
| 111 | } |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 112 | |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 113 | // removeFromWorkList - remove all instances of I from the worklist. |
| 114 | void removeFromWorkList(Instruction *I); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 115 | public: |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 116 | virtual bool runOnFunction(Function &F); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 117 | |
Chris Lattner | 97e52e4 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 118 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 119 | AU.addRequired<TargetData>(); |
Owen Anderson | d1b78a1 | 2006-07-10 19:03:49 +0000 | [diff] [blame] | 120 | AU.addPreservedID(LCSSAID); |
Chris Lattner | cb2610e | 2002-10-21 20:00:28 +0000 | [diff] [blame] | 121 | AU.setPreservesCFG(); |
Chris Lattner | 97e52e4 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 124 | TargetData &getTargetData() const { return *TD; } |
| 125 | |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 126 | // Visitation implementation - Implement instruction combining for different |
| 127 | // instruction types. The semantics are as follows: |
| 128 | // Return Value: |
| 129 | // null - No change was made |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 130 | // I - Change was made, I is still valid, I may be dead though |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 131 | // otherwise - Change was made, replace I with returned instruction |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 132 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 133 | Instruction *visitAdd(BinaryOperator &I); |
| 134 | Instruction *visitSub(BinaryOperator &I); |
| 135 | Instruction *visitMul(BinaryOperator &I); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 136 | Instruction *visitURem(BinaryOperator &I); |
| 137 | Instruction *visitSRem(BinaryOperator &I); |
| 138 | Instruction *visitFRem(BinaryOperator &I); |
| 139 | Instruction *commonRemTransforms(BinaryOperator &I); |
| 140 | Instruction *commonIRemTransforms(BinaryOperator &I); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 141 | Instruction *commonDivTransforms(BinaryOperator &I); |
| 142 | Instruction *commonIDivTransforms(BinaryOperator &I); |
| 143 | Instruction *visitUDiv(BinaryOperator &I); |
| 144 | Instruction *visitSDiv(BinaryOperator &I); |
| 145 | Instruction *visitFDiv(BinaryOperator &I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 146 | Instruction *visitAnd(BinaryOperator &I); |
| 147 | Instruction *visitOr (BinaryOperator &I); |
| 148 | Instruction *visitXor(BinaryOperator &I); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 149 | Instruction *visitShl(BinaryOperator &I); |
| 150 | Instruction *visitAShr(BinaryOperator &I); |
| 151 | Instruction *visitLShr(BinaryOperator &I); |
| 152 | Instruction *commonShiftTransforms(BinaryOperator &I); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 153 | Instruction *visitFCmpInst(FCmpInst &I); |
| 154 | Instruction *visitICmpInst(ICmpInst &I); |
| 155 | Instruction *visitICmpInstWithCastAndCast(ICmpInst &ICI); |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 156 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 157 | Instruction *FoldGEPICmp(User *GEPLHS, Value *RHS, |
| 158 | ICmpInst::Predicate Cond, Instruction &I); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 159 | Instruction *FoldShiftByConstant(Value *Op0, ConstantInt *Op1, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 160 | BinaryOperator &I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 161 | Instruction *commonCastTransforms(CastInst &CI); |
| 162 | Instruction *commonIntCastTransforms(CastInst &CI); |
| 163 | Instruction *visitTrunc(CastInst &CI); |
| 164 | Instruction *visitZExt(CastInst &CI); |
| 165 | Instruction *visitSExt(CastInst &CI); |
| 166 | Instruction *visitFPTrunc(CastInst &CI); |
| 167 | Instruction *visitFPExt(CastInst &CI); |
| 168 | Instruction *visitFPToUI(CastInst &CI); |
| 169 | Instruction *visitFPToSI(CastInst &CI); |
| 170 | Instruction *visitUIToFP(CastInst &CI); |
| 171 | Instruction *visitSIToFP(CastInst &CI); |
| 172 | Instruction *visitPtrToInt(CastInst &CI); |
| 173 | Instruction *visitIntToPtr(CastInst &CI); |
| 174 | Instruction *visitBitCast(CastInst &CI); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 175 | Instruction *FoldSelectOpOp(SelectInst &SI, Instruction *TI, |
| 176 | Instruction *FI); |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 177 | Instruction *visitSelectInst(SelectInst &CI); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 178 | Instruction *visitCallInst(CallInst &CI); |
| 179 | Instruction *visitInvokeInst(InvokeInst &II); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 180 | Instruction *visitPHINode(PHINode &PN); |
| 181 | Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP); |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 182 | Instruction *visitAllocationInst(AllocationInst &AI); |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 183 | Instruction *visitFreeInst(FreeInst &FI); |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 184 | Instruction *visitLoadInst(LoadInst &LI); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 185 | Instruction *visitStoreInst(StoreInst &SI); |
Chris Lattner | c4d10eb | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 186 | Instruction *visitBranchInst(BranchInst &BI); |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 187 | Instruction *visitSwitchInst(SwitchInst &SI); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 188 | Instruction *visitInsertElementInst(InsertElementInst &IE); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 189 | Instruction *visitExtractElementInst(ExtractElementInst &EI); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 190 | Instruction *visitShuffleVectorInst(ShuffleVectorInst &SVI); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 191 | |
| 192 | // visitInstruction - Specify what to return for unhandled instructions... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 193 | Instruction *visitInstruction(Instruction &I) { return 0; } |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 194 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 195 | private: |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 196 | Instruction *visitCallSite(CallSite CS); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 197 | bool transformConstExprCastCall(CallSite CS); |
| 198 | |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 199 | public: |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 200 | // InsertNewInstBefore - insert an instruction New before instruction Old |
| 201 | // in the program. Add the new instruction to the worklist. |
| 202 | // |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 203 | Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) { |
Chris Lattner | e6f9a91 | 2002-08-23 18:32:43 +0000 | [diff] [blame] | 204 | assert(New && New->getParent() == 0 && |
| 205 | "New instruction already inserted into a basic block!"); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 206 | BasicBlock *BB = Old.getParent(); |
| 207 | BB->getInstList().insert(&Old, New); // Insert inst |
| 208 | WorkList.push_back(New); // Add to worklist |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 209 | return New; |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 212 | /// InsertCastBefore - Insert a cast of V to TY before the instruction POS. |
| 213 | /// This also adds the cast to the worklist. Finally, this returns the |
| 214 | /// cast. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 215 | Value *InsertCastBefore(Instruction::CastOps opc, Value *V, const Type *Ty, |
| 216 | Instruction &Pos) { |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 217 | if (V->getType() == Ty) return V; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 218 | |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 219 | if (Constant *CV = dyn_cast<Constant>(V)) |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 220 | return ConstantExpr::getCast(opc, CV, Ty); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 221 | |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 222 | Instruction *C = CastInst::create(opc, V, Ty, V->getName(), &Pos); |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 223 | WorkList.push_back(C); |
| 224 | return C; |
| 225 | } |
| 226 | |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 227 | // ReplaceInstUsesWith - This method is to be used when an instruction is |
| 228 | // found to be dead, replacable with another preexisting expression. Here |
| 229 | // we add all uses of I to the worklist, replace all uses of I with the new |
| 230 | // value, then return I, so that the inst combiner will know that I was |
| 231 | // modified. |
| 232 | // |
| 233 | Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) { |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 234 | AddUsersToWorkList(I); // Add all modified instrs to worklist |
Chris Lattner | 15a76c0 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 235 | if (&I != V) { |
| 236 | I.replaceAllUsesWith(V); |
| 237 | return &I; |
| 238 | } else { |
| 239 | // If we are replacing the instruction with itself, this must be in a |
| 240 | // segment of unreachable code, so just clobber the instruction. |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 241 | I.replaceAllUsesWith(UndefValue::get(I.getType())); |
Chris Lattner | 15a76c0 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 242 | return &I; |
| 243 | } |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 244 | } |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 245 | |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 246 | // UpdateValueUsesWith - This method is to be used when an value is |
| 247 | // found to be replacable with another preexisting expression or was |
| 248 | // updated. Here we add all uses of I to the worklist, replace all uses of |
| 249 | // I with the new value (unless the instruction was just updated), then |
| 250 | // return true, so that the inst combiner will know that I was modified. |
| 251 | // |
| 252 | bool UpdateValueUsesWith(Value *Old, Value *New) { |
| 253 | AddUsersToWorkList(*Old); // Add all modified instrs to worklist |
| 254 | if (Old != New) |
| 255 | Old->replaceAllUsesWith(New); |
| 256 | if (Instruction *I = dyn_cast<Instruction>(Old)) |
| 257 | WorkList.push_back(I); |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 258 | if (Instruction *I = dyn_cast<Instruction>(New)) |
| 259 | WorkList.push_back(I); |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 260 | return true; |
| 261 | } |
| 262 | |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 263 | // EraseInstFromFunction - When dealing with an instruction that has side |
| 264 | // effects or produces a void value, we can't rely on DCE to delete the |
| 265 | // instruction. Instead, visit methods should return the value returned by |
| 266 | // this function. |
| 267 | Instruction *EraseInstFromFunction(Instruction &I) { |
| 268 | assert(I.use_empty() && "Cannot erase instruction that is used!"); |
| 269 | AddUsesToWorkList(I); |
| 270 | removeFromWorkList(&I); |
Chris Lattner | 954f66a | 2004-11-18 21:41:39 +0000 | [diff] [blame] | 271 | I.eraseFromParent(); |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 272 | return 0; // Don't do anything with FI |
| 273 | } |
| 274 | |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 275 | private: |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 276 | /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the |
| 277 | /// InsertBefore instruction. This is specialized a bit to avoid inserting |
| 278 | /// casts that are known to not do anything... |
| 279 | /// |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 280 | Value *InsertOperandCastBefore(Instruction::CastOps opcode, |
| 281 | Value *V, const Type *DestTy, |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 282 | Instruction *InsertBefore); |
| 283 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 284 | /// SimplifyCommutative - This performs a few simplifications for |
| 285 | /// commutative operators. |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 286 | bool SimplifyCommutative(BinaryOperator &I); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 287 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 288 | /// SimplifyCompare - This reorders the operands of a CmpInst to get them in |
| 289 | /// most-complex to least-complex order. |
| 290 | bool SimplifyCompare(CmpInst &I); |
| 291 | |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 292 | bool SimplifyDemandedBits(Value *V, uint64_t Mask, |
| 293 | uint64_t &KnownZero, uint64_t &KnownOne, |
| 294 | unsigned Depth = 0); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 295 | |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 296 | Value *SimplifyDemandedVectorElts(Value *V, uint64_t DemandedElts, |
| 297 | uint64_t &UndefElts, unsigned Depth = 0); |
| 298 | |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 299 | // FoldOpIntoPhi - Given a binary operator or cast instruction which has a |
| 300 | // PHI node as operand #0, see if we can fold the instruction into the PHI |
| 301 | // (which is only possible if all operands to the PHI are constants). |
| 302 | Instruction *FoldOpIntoPhi(Instruction &I); |
| 303 | |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 304 | // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary" |
| 305 | // operator and they all are only used by the PHI, PHI together their |
| 306 | // inputs, and do the operation once, to the result of the PHI. |
| 307 | Instruction *FoldPHIArgOpIntoPHI(PHINode &PN); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 308 | Instruction *FoldPHIArgBinOpIntoPHI(PHINode &PN); |
| 309 | |
| 310 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 311 | Instruction *OptAndOp(Instruction *Op, ConstantInt *OpRHS, |
| 312 | ConstantInt *AndRHS, BinaryOperator &TheAnd); |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 313 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 314 | Value *FoldLogicalPlusAnd(Value *LHS, Value *RHS, ConstantInt *Mask, |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 315 | bool isSub, Instruction &I); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 316 | Instruction *InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 317 | bool isSigned, bool Inside, Instruction &IB); |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 318 | Instruction *PromoteCastOfAllocation(CastInst &CI, AllocationInst &AI); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 319 | Instruction *MatchBSwap(BinaryOperator &I); |
| 320 | |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 321 | Value *EvaluateInDifferentType(Value *V, const Type *Ty, bool isSigned); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 322 | }; |
Chris Lattner | f629309 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 323 | |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 324 | RegisterPass<InstCombiner> X("instcombine", "Combine redundant instructions"); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 327 | // getComplexity: Assign a complexity or rank value to LLVM Values... |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 328 | // 0 -> undef, 1 -> Const, 2 -> Other, 3 -> Arg, 3 -> Unary, 4 -> OtherInst |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 329 | static unsigned getComplexity(Value *V) { |
| 330 | if (isa<Instruction>(V)) { |
| 331 | if (BinaryOperator::isNeg(V) || BinaryOperator::isNot(V)) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 332 | return 3; |
| 333 | return 4; |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 334 | } |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 335 | if (isa<Argument>(V)) return 3; |
| 336 | return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2; |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 337 | } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 338 | |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 339 | // isOnlyUse - Return true if this instruction will be deleted if we stop using |
| 340 | // it. |
| 341 | static bool isOnlyUse(Value *V) { |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 342 | return V->hasOneUse() || isa<Constant>(V); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 345 | // getPromotedType - Return the specified type promoted as it would be to pass |
| 346 | // though a va_arg area... |
| 347 | static const Type *getPromotedType(const Type *Ty) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 348 | if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty)) { |
| 349 | if (ITy->getBitWidth() < 32) |
| 350 | return Type::Int32Ty; |
| 351 | } else if (Ty == Type::FloatTy) |
| 352 | return Type::DoubleTy; |
| 353 | return Ty; |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 356 | /// getBitCastOperand - If the specified operand is a CastInst or a constant |
| 357 | /// expression bitcast, return the operand value, otherwise return null. |
| 358 | static Value *getBitCastOperand(Value *V) { |
| 359 | if (BitCastInst *I = dyn_cast<BitCastInst>(V)) |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 360 | return I->getOperand(0); |
| 361 | else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 362 | if (CE->getOpcode() == Instruction::BitCast) |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 363 | return CE->getOperand(0); |
| 364 | return 0; |
| 365 | } |
| 366 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 367 | /// This function is a wrapper around CastInst::isEliminableCastPair. It |
| 368 | /// simply extracts arguments and returns what that function returns. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 369 | static Instruction::CastOps |
| 370 | isEliminableCastPair( |
| 371 | const CastInst *CI, ///< The first cast instruction |
| 372 | unsigned opcode, ///< The opcode of the second cast instruction |
| 373 | const Type *DstTy, ///< The target type for the second cast instruction |
| 374 | TargetData *TD ///< The target data for pointer size |
| 375 | ) { |
| 376 | |
| 377 | const Type *SrcTy = CI->getOperand(0)->getType(); // A from above |
| 378 | const Type *MidTy = CI->getType(); // B from above |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 379 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 380 | // Get the opcodes of the two Cast instructions |
| 381 | Instruction::CastOps firstOp = Instruction::CastOps(CI->getOpcode()); |
| 382 | Instruction::CastOps secondOp = Instruction::CastOps(opcode); |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 383 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 384 | return Instruction::CastOps( |
| 385 | CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, |
| 386 | DstTy, TD->getIntPtrType())); |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | /// ValueRequiresCast - Return true if the cast from "V to Ty" actually results |
| 390 | /// in any code being generated. It does not require codegen if V is simple |
| 391 | /// enough or if the cast can be folded into other casts. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 392 | static bool ValueRequiresCast(Instruction::CastOps opcode, const Value *V, |
| 393 | const Type *Ty, TargetData *TD) { |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 394 | if (V->getType() == Ty || isa<Constant>(V)) return false; |
| 395 | |
Chris Lattner | 01575b7 | 2006-05-25 23:24:33 +0000 | [diff] [blame] | 396 | // If this is another cast that can be eliminated, it isn't codegen either. |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 397 | if (const CastInst *CI = dyn_cast<CastInst>(V)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 398 | if (isEliminableCastPair(CI, opcode, Ty, TD)) |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 399 | return false; |
| 400 | return true; |
| 401 | } |
| 402 | |
| 403 | /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the |
| 404 | /// InsertBefore instruction. This is specialized a bit to avoid inserting |
| 405 | /// casts that are known to not do anything... |
| 406 | /// |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 407 | Value *InstCombiner::InsertOperandCastBefore(Instruction::CastOps opcode, |
| 408 | Value *V, const Type *DestTy, |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 409 | Instruction *InsertBefore) { |
| 410 | if (V->getType() == DestTy) return V; |
| 411 | if (Constant *C = dyn_cast<Constant>(V)) |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 412 | return ConstantExpr::getCast(opcode, C, DestTy); |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 413 | |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 414 | return InsertCastBefore(opcode, V, DestTy, *InsertBefore); |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 417 | // SimplifyCommutative - This performs a few simplifications for commutative |
| 418 | // operators: |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 419 | // |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 420 | // 1. Order operands such that they are listed from right (least complex) to |
| 421 | // left (most complex). This puts constants before unary operators before |
| 422 | // binary operators. |
| 423 | // |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 424 | // 2. Transform: (op (op V, C1), C2) ==> (op V, (op C1, C2)) |
| 425 | // 3. Transform: (op (op V1, C1), (op V2, C2)) ==> (op (op V1, V2), (op C1,C2)) |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 426 | // |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 427 | bool InstCombiner::SimplifyCommutative(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 428 | bool Changed = false; |
| 429 | if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1))) |
| 430 | Changed = !I.swapOperands(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 431 | |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 432 | if (!I.isAssociative()) return Changed; |
| 433 | Instruction::BinaryOps Opcode = I.getOpcode(); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 434 | if (BinaryOperator *Op = dyn_cast<BinaryOperator>(I.getOperand(0))) |
| 435 | if (Op->getOpcode() == Opcode && isa<Constant>(Op->getOperand(1))) { |
| 436 | if (isa<Constant>(I.getOperand(1))) { |
Chris Lattner | 2a9c847 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 437 | Constant *Folded = ConstantExpr::get(I.getOpcode(), |
| 438 | cast<Constant>(I.getOperand(1)), |
| 439 | cast<Constant>(Op->getOperand(1))); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 440 | I.setOperand(0, Op->getOperand(0)); |
| 441 | I.setOperand(1, Folded); |
| 442 | return true; |
| 443 | } else if (BinaryOperator *Op1=dyn_cast<BinaryOperator>(I.getOperand(1))) |
| 444 | if (Op1->getOpcode() == Opcode && isa<Constant>(Op1->getOperand(1)) && |
| 445 | isOnlyUse(Op) && isOnlyUse(Op1)) { |
| 446 | Constant *C1 = cast<Constant>(Op->getOperand(1)); |
| 447 | Constant *C2 = cast<Constant>(Op1->getOperand(1)); |
| 448 | |
| 449 | // Fold (op (op V1, C1), (op V2, C2)) ==> (op (op V1, V2), (op C1,C2)) |
Chris Lattner | 2a9c847 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 450 | Constant *Folded = ConstantExpr::get(I.getOpcode(), C1, C2); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 451 | Instruction *New = BinaryOperator::create(Opcode, Op->getOperand(0), |
| 452 | Op1->getOperand(0), |
| 453 | Op1->getName(), &I); |
| 454 | WorkList.push_back(New); |
| 455 | I.setOperand(0, New); |
| 456 | I.setOperand(1, Folded); |
| 457 | return true; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 458 | } |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 459 | } |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 460 | return Changed; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 461 | } |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 462 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 463 | /// SimplifyCompare - For a CmpInst this function just orders the operands |
| 464 | /// so that theyare listed from right (least complex) to left (most complex). |
| 465 | /// This puts constants before unary operators before binary operators. |
| 466 | bool InstCombiner::SimplifyCompare(CmpInst &I) { |
| 467 | if (getComplexity(I.getOperand(0)) >= getComplexity(I.getOperand(1))) |
| 468 | return false; |
| 469 | I.swapOperands(); |
| 470 | // Compare instructions are not associative so there's nothing else we can do. |
| 471 | return true; |
| 472 | } |
| 473 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 474 | // dyn_castNegVal - Given a 'sub' instruction, return the RHS of the instruction |
| 475 | // if the LHS is a constant zero (which is the 'negate' form). |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 476 | // |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 477 | static inline Value *dyn_castNegVal(Value *V) { |
| 478 | if (BinaryOperator::isNeg(V)) |
Chris Lattner | a1df33c | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 479 | return BinaryOperator::getNegArgument(V); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 480 | |
Chris Lattner | 0ce8580 | 2004-12-14 20:08:06 +0000 | [diff] [blame] | 481 | // Constants can be considered to be negated values if they can be folded. |
| 482 | if (ConstantInt *C = dyn_cast<ConstantInt>(V)) |
| 483 | return ConstantExpr::getNeg(C); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 484 | return 0; |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 487 | static inline Value *dyn_castNotVal(Value *V) { |
| 488 | if (BinaryOperator::isNot(V)) |
Chris Lattner | a1df33c | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 489 | return BinaryOperator::getNotArgument(V); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 490 | |
| 491 | // Constants can be considered to be not'ed values... |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 492 | if (ConstantInt *C = dyn_cast<ConstantInt>(V)) |
Chris Lattner | 448c323 | 2004-06-10 02:12:35 +0000 | [diff] [blame] | 493 | return ConstantExpr::getNot(C); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 494 | return 0; |
| 495 | } |
| 496 | |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 497 | // dyn_castFoldableMul - If this value is a multiply that can be folded into |
| 498 | // other computations (because it has a constant operand), return the |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 499 | // non-constant operand of the multiply, and set CST to point to the multiplier. |
| 500 | // Otherwise, return null. |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 501 | // |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 502 | static inline Value *dyn_castFoldableMul(Value *V, ConstantInt *&CST) { |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 503 | if (V->hasOneUse() && V->getType()->isInteger()) |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 504 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 505 | if (I->getOpcode() == Instruction::Mul) |
Chris Lattner | 50e60c7 | 2004-11-15 05:54:07 +0000 | [diff] [blame] | 506 | if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 507 | return I->getOperand(0); |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 508 | if (I->getOpcode() == Instruction::Shl) |
Chris Lattner | 50e60c7 | 2004-11-15 05:54:07 +0000 | [diff] [blame] | 509 | if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) { |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 510 | // The multiplier is really 1 << CST. |
| 511 | Constant *One = ConstantInt::get(V->getType(), 1); |
| 512 | CST = cast<ConstantInt>(ConstantExpr::getShl(One, CST)); |
| 513 | return I->getOperand(0); |
| 514 | } |
| 515 | } |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 516 | return 0; |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 517 | } |
Chris Lattner | af2930e | 2002-08-14 17:51:49 +0000 | [diff] [blame] | 518 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 519 | /// dyn_castGetElementPtr - If this is a getelementptr instruction or constant |
| 520 | /// expression, return it. |
| 521 | static User *dyn_castGetElementPtr(Value *V) { |
| 522 | if (isa<GetElementPtrInst>(V)) return cast<User>(V); |
| 523 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
| 524 | if (CE->getOpcode() == Instruction::GetElementPtr) |
| 525 | return cast<User>(V); |
| 526 | return false; |
| 527 | } |
| 528 | |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 529 | // AddOne, SubOne - Add or subtract a constant one from an integer constant... |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 530 | static ConstantInt *AddOne(ConstantInt *C) { |
| 531 | return cast<ConstantInt>(ConstantExpr::getAdd(C, |
| 532 | ConstantInt::get(C->getType(), 1))); |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 533 | } |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 534 | static ConstantInt *SubOne(ConstantInt *C) { |
| 535 | return cast<ConstantInt>(ConstantExpr::getSub(C, |
| 536 | ConstantInt::get(C->getType(), 1))); |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Chris Lattner | 68d5ff2 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 539 | /// ComputeMaskedBits - Determine which of the bits specified in Mask are |
| 540 | /// known to be either zero or one and return them in the KnownZero/KnownOne |
| 541 | /// bitsets. This code only analyzes bits in Mask, in order to short-circuit |
| 542 | /// processing. |
| 543 | static void ComputeMaskedBits(Value *V, uint64_t Mask, uint64_t &KnownZero, |
| 544 | uint64_t &KnownOne, unsigned Depth = 0) { |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 545 | // Note, we cannot consider 'undef' to be "IsZero" here. The problem is that |
| 546 | // we cannot optimize based on the assumption that it is zero without changing |
Chris Lattner | 3bedbd9 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 547 | // it to be an explicit zero. If we don't change it to zero, other code could |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 548 | // optimized based on the contradictory assumption that it is non-zero. |
| 549 | // Because instcombine aggressively folds operations with undef args anyway, |
| 550 | // this won't lose us code quality. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 551 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
Chris Lattner | 68d5ff2 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 552 | // We know all of the bits for a constant! |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 553 | KnownOne = CI->getZExtValue() & Mask; |
Chris Lattner | 68d5ff2 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 554 | KnownZero = ~KnownOne & Mask; |
| 555 | return; |
| 556 | } |
| 557 | |
| 558 | KnownZero = KnownOne = 0; // Don't know anything. |
Chris Lattner | 74c51a0 | 2006-02-07 08:05:22 +0000 | [diff] [blame] | 559 | if (Depth == 6 || Mask == 0) |
Chris Lattner | 68d5ff2 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 560 | return; // Limit search depth. |
| 561 | |
| 562 | uint64_t KnownZero2, KnownOne2; |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 563 | Instruction *I = dyn_cast<Instruction>(V); |
| 564 | if (!I) return; |
| 565 | |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 566 | Mask &= cast<IntegerType>(V->getType())->getBitMask(); |
Chris Lattner | e315830 | 2006-05-04 17:33:35 +0000 | [diff] [blame] | 567 | |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 568 | switch (I->getOpcode()) { |
| 569 | case Instruction::And: |
| 570 | // If either the LHS or the RHS are Zero, the result is zero. |
| 571 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, Depth+1); |
| 572 | Mask &= ~KnownZero; |
| 573 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1); |
| 574 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 575 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 576 | |
| 577 | // Output known-1 bits are only known if set in both the LHS & RHS. |
| 578 | KnownOne &= KnownOne2; |
| 579 | // Output known-0 are known to be clear if zero in either the LHS | RHS. |
| 580 | KnownZero |= KnownZero2; |
| 581 | return; |
| 582 | case Instruction::Or: |
| 583 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, Depth+1); |
| 584 | Mask &= ~KnownOne; |
| 585 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1); |
| 586 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 587 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 588 | |
| 589 | // Output known-0 bits are only known if clear in both the LHS & RHS. |
| 590 | KnownZero &= KnownZero2; |
| 591 | // Output known-1 are known to be set if set in either the LHS | RHS. |
| 592 | KnownOne |= KnownOne2; |
| 593 | return; |
| 594 | case Instruction::Xor: { |
| 595 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, Depth+1); |
| 596 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1); |
| 597 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 598 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 599 | |
| 600 | // Output known-0 bits are known if clear or set in both the LHS & RHS. |
| 601 | uint64_t KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2); |
| 602 | // Output known-1 are known to be set if set in only one of the LHS, RHS. |
| 603 | KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2); |
| 604 | KnownZero = KnownZeroOut; |
| 605 | return; |
| 606 | } |
| 607 | case Instruction::Select: |
| 608 | ComputeMaskedBits(I->getOperand(2), Mask, KnownZero, KnownOne, Depth+1); |
| 609 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1); |
| 610 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 611 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 612 | |
| 613 | // Only known if known in both the LHS and RHS. |
| 614 | KnownOne &= KnownOne2; |
| 615 | KnownZero &= KnownZero2; |
| 616 | return; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 617 | case Instruction::FPTrunc: |
| 618 | case Instruction::FPExt: |
| 619 | case Instruction::FPToUI: |
| 620 | case Instruction::FPToSI: |
| 621 | case Instruction::SIToFP: |
| 622 | case Instruction::PtrToInt: |
| 623 | case Instruction::UIToFP: |
| 624 | case Instruction::IntToPtr: |
| 625 | return; // Can't work with floating point or pointers |
| 626 | case Instruction::Trunc: |
| 627 | // All these have integer operands |
| 628 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero, KnownOne, Depth+1); |
| 629 | return; |
| 630 | case Instruction::BitCast: { |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 631 | const Type *SrcTy = I->getOperand(0)->getType(); |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 632 | if (SrcTy->isInteger()) { |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 633 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero, KnownOne, Depth+1); |
Chris Lattner | 68d5ff2 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 634 | return; |
| 635 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 636 | break; |
| 637 | } |
| 638 | case Instruction::ZExt: { |
| 639 | // Compute the bits in the result that are not present in the input. |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 640 | const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType()); |
| 641 | uint64_t NotIn = ~SrcTy->getBitMask(); |
| 642 | uint64_t NewBits = cast<IntegerType>(I->getType())->getBitMask() & NotIn; |
Chris Lattner | 60de63d | 2005-10-09 06:36:35 +0000 | [diff] [blame] | 643 | |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 644 | Mask &= SrcTy->getBitMask(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 645 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero, KnownOne, Depth+1); |
| 646 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 647 | // The top bits are known to be zero. |
| 648 | KnownZero |= NewBits; |
| 649 | return; |
| 650 | } |
| 651 | case Instruction::SExt: { |
| 652 | // Compute the bits in the result that are not present in the input. |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 653 | const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType()); |
| 654 | uint64_t NotIn = ~SrcTy->getBitMask(); |
| 655 | uint64_t NewBits = cast<IntegerType>(I->getType())->getBitMask() & NotIn; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 656 | |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 657 | Mask &= SrcTy->getBitMask(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 658 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero, KnownOne, Depth+1); |
| 659 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
Chris Lattner | 74c51a0 | 2006-02-07 08:05:22 +0000 | [diff] [blame] | 660 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 661 | // If the sign bit of the input is known set or clear, then we know the |
| 662 | // top bits of the result. |
| 663 | uint64_t InSignBit = 1ULL << (SrcTy->getPrimitiveSizeInBits()-1); |
| 664 | if (KnownZero & InSignBit) { // Input sign bit known zero |
| 665 | KnownZero |= NewBits; |
| 666 | KnownOne &= ~NewBits; |
| 667 | } else if (KnownOne & InSignBit) { // Input sign bit known set |
| 668 | KnownOne |= NewBits; |
| 669 | KnownZero &= ~NewBits; |
| 670 | } else { // Input sign bit unknown |
| 671 | KnownZero &= ~NewBits; |
| 672 | KnownOne &= ~NewBits; |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 673 | } |
| 674 | return; |
| 675 | } |
| 676 | case Instruction::Shl: |
| 677 | // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0 |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 678 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 679 | uint64_t ShiftAmt = SA->getZExtValue(); |
| 680 | Mask >>= ShiftAmt; |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 681 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero, KnownOne, Depth+1); |
| 682 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 683 | KnownZero <<= ShiftAmt; |
| 684 | KnownOne <<= ShiftAmt; |
| 685 | KnownZero |= (1ULL << ShiftAmt)-1; // low bits known zero. |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 686 | return; |
| 687 | } |
| 688 | break; |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 689 | case Instruction::LShr: |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 690 | // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0 |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 691 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 692 | // Compute the new bits that are at the top now. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 693 | uint64_t ShiftAmt = SA->getZExtValue(); |
| 694 | uint64_t HighBits = (1ULL << ShiftAmt)-1; |
| 695 | HighBits <<= I->getType()->getPrimitiveSizeInBits()-ShiftAmt; |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 696 | |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 697 | // Unsigned shift right. |
| 698 | Mask <<= ShiftAmt; |
| 699 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero,KnownOne,Depth+1); |
| 700 | assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); |
| 701 | KnownZero >>= ShiftAmt; |
| 702 | KnownOne >>= ShiftAmt; |
| 703 | KnownZero |= HighBits; // high bits known zero. |
| 704 | return; |
| 705 | } |
| 706 | break; |
| 707 | case Instruction::AShr: |
| 708 | // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0 |
| 709 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 710 | // Compute the new bits that are at the top now. |
| 711 | uint64_t ShiftAmt = SA->getZExtValue(); |
| 712 | uint64_t HighBits = (1ULL << ShiftAmt)-1; |
| 713 | HighBits <<= I->getType()->getPrimitiveSizeInBits()-ShiftAmt; |
| 714 | |
| 715 | // Signed shift right. |
| 716 | Mask <<= ShiftAmt; |
| 717 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero,KnownOne,Depth+1); |
| 718 | assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); |
| 719 | KnownZero >>= ShiftAmt; |
| 720 | KnownOne >>= ShiftAmt; |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 721 | |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 722 | // Handle the sign bits. |
| 723 | uint64_t SignBit = 1ULL << (I->getType()->getPrimitiveSizeInBits()-1); |
| 724 | SignBit >>= ShiftAmt; // Adjust to where it is now in the mask. |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 725 | |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 726 | if (KnownZero & SignBit) { // New bits are known zero. |
| 727 | KnownZero |= HighBits; |
| 728 | } else if (KnownOne & SignBit) { // New bits are known one. |
| 729 | KnownOne |= HighBits; |
Chris Lattner | 68d5ff2 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 730 | } |
| 731 | return; |
Chris Lattner | 60de63d | 2005-10-09 06:36:35 +0000 | [diff] [blame] | 732 | } |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 733 | break; |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 734 | } |
Chris Lattner | 74c51a0 | 2006-02-07 08:05:22 +0000 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use |
| 738 | /// this predicate to simplify operations downstream. Mask is known to be zero |
| 739 | /// for bits that V cannot have. |
| 740 | static bool MaskedValueIsZero(Value *V, uint64_t Mask, unsigned Depth = 0) { |
Chris Lattner | 68d5ff2 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 741 | uint64_t KnownZero, KnownOne; |
| 742 | ComputeMaskedBits(V, Mask, KnownZero, KnownOne, Depth); |
| 743 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 744 | return (KnownZero & Mask) == Mask; |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 745 | } |
| 746 | |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 747 | /// ShrinkDemandedConstant - Check to see if the specified operand of the |
| 748 | /// specified instruction is a constant integer. If so, check to see if there |
| 749 | /// are any bits set in the constant that are not demanded. If so, shrink the |
| 750 | /// constant and return true. |
| 751 | static bool ShrinkDemandedConstant(Instruction *I, unsigned OpNo, |
| 752 | uint64_t Demanded) { |
| 753 | ConstantInt *OpC = dyn_cast<ConstantInt>(I->getOperand(OpNo)); |
| 754 | if (!OpC) return false; |
| 755 | |
| 756 | // If there are no bits set that aren't demanded, nothing to do. |
| 757 | if ((~Demanded & OpC->getZExtValue()) == 0) |
| 758 | return false; |
| 759 | |
| 760 | // This is producing any bits that are not needed, shrink the RHS. |
| 761 | uint64_t Val = Demanded & OpC->getZExtValue(); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 762 | I->setOperand(OpNo, ConstantInt::get(OpC->getType(), Val)); |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 763 | return true; |
| 764 | } |
| 765 | |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 766 | // ComputeSignedMinMaxValuesFromKnownBits - Given a signed integer type and a |
| 767 | // set of known zero and one bits, compute the maximum and minimum values that |
| 768 | // could have the specified known zero and known one bits, returning them in |
| 769 | // min/max. |
| 770 | static void ComputeSignedMinMaxValuesFromKnownBits(const Type *Ty, |
| 771 | uint64_t KnownZero, |
| 772 | uint64_t KnownOne, |
| 773 | int64_t &Min, int64_t &Max) { |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 774 | uint64_t TypeBits = cast<IntegerType>(Ty)->getBitMask(); |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 775 | uint64_t UnknownBits = ~(KnownZero|KnownOne) & TypeBits; |
| 776 | |
| 777 | uint64_t SignBit = 1ULL << (Ty->getPrimitiveSizeInBits()-1); |
| 778 | |
| 779 | // The minimum value is when all unknown bits are zeros, EXCEPT for the sign |
| 780 | // bit if it is unknown. |
| 781 | Min = KnownOne; |
| 782 | Max = KnownOne|UnknownBits; |
| 783 | |
| 784 | if (SignBit & UnknownBits) { // Sign bit is unknown |
| 785 | Min |= SignBit; |
| 786 | Max &= ~SignBit; |
| 787 | } |
| 788 | |
| 789 | // Sign extend the min/max values. |
| 790 | int ShAmt = 64-Ty->getPrimitiveSizeInBits(); |
| 791 | Min = (Min << ShAmt) >> ShAmt; |
| 792 | Max = (Max << ShAmt) >> ShAmt; |
| 793 | } |
| 794 | |
| 795 | // ComputeUnsignedMinMaxValuesFromKnownBits - Given an unsigned integer type and |
| 796 | // a set of known zero and one bits, compute the maximum and minimum values that |
| 797 | // could have the specified known zero and known one bits, returning them in |
| 798 | // min/max. |
| 799 | static void ComputeUnsignedMinMaxValuesFromKnownBits(const Type *Ty, |
| 800 | uint64_t KnownZero, |
| 801 | uint64_t KnownOne, |
| 802 | uint64_t &Min, |
| 803 | uint64_t &Max) { |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 804 | uint64_t TypeBits = cast<IntegerType>(Ty)->getBitMask(); |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 805 | uint64_t UnknownBits = ~(KnownZero|KnownOne) & TypeBits; |
| 806 | |
| 807 | // The minimum value is when the unknown bits are all zeros. |
| 808 | Min = KnownOne; |
| 809 | // The maximum value is when the unknown bits are all ones. |
| 810 | Max = KnownOne|UnknownBits; |
| 811 | } |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 812 | |
| 813 | |
| 814 | /// SimplifyDemandedBits - Look at V. At this point, we know that only the |
| 815 | /// DemandedMask bits of the result of V are ever used downstream. If we can |
| 816 | /// use this information to simplify V, do so and return true. Otherwise, |
| 817 | /// analyze the expression and return a mask of KnownOne and KnownZero bits for |
| 818 | /// the expression (used to simplify the caller). The KnownZero/One bits may |
| 819 | /// only be accurate for those bits in the DemandedMask. |
| 820 | bool InstCombiner::SimplifyDemandedBits(Value *V, uint64_t DemandedMask, |
| 821 | uint64_t &KnownZero, uint64_t &KnownOne, |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 822 | unsigned Depth) { |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 823 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 824 | // We know all of the bits for a constant! |
| 825 | KnownOne = CI->getZExtValue() & DemandedMask; |
| 826 | KnownZero = ~KnownOne & DemandedMask; |
| 827 | return false; |
| 828 | } |
| 829 | |
| 830 | KnownZero = KnownOne = 0; |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 831 | if (!V->hasOneUse()) { // Other users may use these bits. |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 832 | if (Depth != 0) { // Not at the root. |
| 833 | // Just compute the KnownZero/KnownOne bits to simplify things downstream. |
| 834 | ComputeMaskedBits(V, DemandedMask, KnownZero, KnownOne, Depth); |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 835 | return false; |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 836 | } |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 837 | // If this is the root being simplified, allow it to have multiple uses, |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 838 | // just set the DemandedMask to all bits. |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 839 | DemandedMask = cast<IntegerType>(V->getType())->getBitMask(); |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 840 | } else if (DemandedMask == 0) { // Not demanding any bits from V. |
Chris Lattner | 74c51a0 | 2006-02-07 08:05:22 +0000 | [diff] [blame] | 841 | if (V != UndefValue::get(V->getType())) |
| 842 | return UpdateValueUsesWith(V, UndefValue::get(V->getType())); |
| 843 | return false; |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 844 | } else if (Depth == 6) { // Limit search depth. |
| 845 | return false; |
| 846 | } |
| 847 | |
| 848 | Instruction *I = dyn_cast<Instruction>(V); |
| 849 | if (!I) return false; // Only analyze instructions. |
| 850 | |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 851 | DemandedMask &= cast<IntegerType>(V->getType())->getBitMask(); |
Chris Lattner | e315830 | 2006-05-04 17:33:35 +0000 | [diff] [blame] | 852 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 853 | uint64_t KnownZero2 = 0, KnownOne2 = 0; |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 854 | switch (I->getOpcode()) { |
| 855 | default: break; |
| 856 | case Instruction::And: |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 857 | // If either the LHS or the RHS are Zero, the result is zero. |
| 858 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, |
| 859 | KnownZero, KnownOne, Depth+1)) |
| 860 | return true; |
| 861 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 862 | |
| 863 | // If something is known zero on the RHS, the bits aren't demanded on the |
| 864 | // LHS. |
| 865 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask & ~KnownZero, |
| 866 | KnownZero2, KnownOne2, Depth+1)) |
| 867 | return true; |
| 868 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 869 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 870 | // If all of the demanded bits are known 1 on one side, return the other. |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 871 | // These bits cannot contribute to the result of the 'and'. |
| 872 | if ((DemandedMask & ~KnownZero2 & KnownOne) == (DemandedMask & ~KnownZero2)) |
| 873 | return UpdateValueUsesWith(I, I->getOperand(0)); |
| 874 | if ((DemandedMask & ~KnownZero & KnownOne2) == (DemandedMask & ~KnownZero)) |
| 875 | return UpdateValueUsesWith(I, I->getOperand(1)); |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 876 | |
| 877 | // If all of the demanded bits in the inputs are known zeros, return zero. |
| 878 | if ((DemandedMask & (KnownZero|KnownZero2)) == DemandedMask) |
| 879 | return UpdateValueUsesWith(I, Constant::getNullValue(I->getType())); |
| 880 | |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 881 | // If the RHS is a constant, see if we can simplify it. |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 882 | if (ShrinkDemandedConstant(I, 1, DemandedMask & ~KnownZero2)) |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 883 | return UpdateValueUsesWith(I, I); |
| 884 | |
| 885 | // Output known-1 bits are only known if set in both the LHS & RHS. |
| 886 | KnownOne &= KnownOne2; |
| 887 | // Output known-0 are known to be clear if zero in either the LHS | RHS. |
| 888 | KnownZero |= KnownZero2; |
| 889 | break; |
| 890 | case Instruction::Or: |
| 891 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, |
| 892 | KnownZero, KnownOne, Depth+1)) |
| 893 | return true; |
| 894 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 895 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask & ~KnownOne, |
| 896 | KnownZero2, KnownOne2, Depth+1)) |
| 897 | return true; |
| 898 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 899 | |
| 900 | // If all of the demanded bits are known zero on one side, return the other. |
| 901 | // These bits cannot contribute to the result of the 'or'. |
Jeff Cohen | bce4805 | 2006-02-18 03:20:33 +0000 | [diff] [blame] | 902 | if ((DemandedMask & ~KnownOne2 & KnownZero) == (DemandedMask & ~KnownOne2)) |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 903 | return UpdateValueUsesWith(I, I->getOperand(0)); |
Jeff Cohen | bce4805 | 2006-02-18 03:20:33 +0000 | [diff] [blame] | 904 | if ((DemandedMask & ~KnownOne & KnownZero2) == (DemandedMask & ~KnownOne)) |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 905 | return UpdateValueUsesWith(I, I->getOperand(1)); |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 906 | |
| 907 | // If all of the potentially set bits on one side are known to be set on |
| 908 | // the other side, just use the 'other' side. |
| 909 | if ((DemandedMask & (~KnownZero) & KnownOne2) == |
| 910 | (DemandedMask & (~KnownZero))) |
| 911 | return UpdateValueUsesWith(I, I->getOperand(0)); |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 912 | if ((DemandedMask & (~KnownZero2) & KnownOne) == |
| 913 | (DemandedMask & (~KnownZero2))) |
| 914 | return UpdateValueUsesWith(I, I->getOperand(1)); |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 915 | |
| 916 | // If the RHS is a constant, see if we can simplify it. |
| 917 | if (ShrinkDemandedConstant(I, 1, DemandedMask)) |
| 918 | return UpdateValueUsesWith(I, I); |
| 919 | |
| 920 | // Output known-0 bits are only known if clear in both the LHS & RHS. |
| 921 | KnownZero &= KnownZero2; |
| 922 | // Output known-1 are known to be set if set in either the LHS | RHS. |
| 923 | KnownOne |= KnownOne2; |
| 924 | break; |
| 925 | case Instruction::Xor: { |
| 926 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, |
| 927 | KnownZero, KnownOne, Depth+1)) |
| 928 | return true; |
| 929 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 930 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, |
| 931 | KnownZero2, KnownOne2, Depth+1)) |
| 932 | return true; |
| 933 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 934 | |
| 935 | // If all of the demanded bits are known zero on one side, return the other. |
| 936 | // These bits cannot contribute to the result of the 'xor'. |
| 937 | if ((DemandedMask & KnownZero) == DemandedMask) |
| 938 | return UpdateValueUsesWith(I, I->getOperand(0)); |
| 939 | if ((DemandedMask & KnownZero2) == DemandedMask) |
| 940 | return UpdateValueUsesWith(I, I->getOperand(1)); |
| 941 | |
| 942 | // Output known-0 bits are known if clear or set in both the LHS & RHS. |
| 943 | uint64_t KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2); |
| 944 | // Output known-1 are known to be set if set in only one of the LHS, RHS. |
| 945 | uint64_t KnownOneOut = (KnownZero & KnownOne2) | (KnownOne & KnownZero2); |
| 946 | |
Chris Lattner | f2f1643 | 2006-11-27 19:55:07 +0000 | [diff] [blame] | 947 | // If all of the demanded bits are known to be zero on one side or the |
| 948 | // other, turn this into an *inclusive* or. |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 949 | // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0 |
Chris Lattner | f2f1643 | 2006-11-27 19:55:07 +0000 | [diff] [blame] | 950 | if ((DemandedMask & ~KnownZero & ~KnownZero2) == 0) { |
| 951 | Instruction *Or = |
| 952 | BinaryOperator::createOr(I->getOperand(0), I->getOperand(1), |
| 953 | I->getName()); |
| 954 | InsertNewInstBefore(Or, *I); |
| 955 | return UpdateValueUsesWith(I, Or); |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 956 | } |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 957 | |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 958 | // If all of the demanded bits on one side are known, and all of the set |
| 959 | // bits on that side are also known to be set on the other side, turn this |
| 960 | // into an AND, as we know the bits will be cleared. |
| 961 | // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2 |
| 962 | if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask) { // all known |
| 963 | if ((KnownOne & KnownOne2) == KnownOne) { |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 964 | Constant *AndC = ConstantInt::get(I->getType(), |
| 965 | ~KnownOne & DemandedMask); |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 966 | Instruction *And = |
| 967 | BinaryOperator::createAnd(I->getOperand(0), AndC, "tmp"); |
| 968 | InsertNewInstBefore(And, *I); |
| 969 | return UpdateValueUsesWith(I, And); |
| 970 | } |
| 971 | } |
| 972 | |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 973 | // If the RHS is a constant, see if we can simplify it. |
| 974 | // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1. |
| 975 | if (ShrinkDemandedConstant(I, 1, DemandedMask)) |
| 976 | return UpdateValueUsesWith(I, I); |
| 977 | |
| 978 | KnownZero = KnownZeroOut; |
| 979 | KnownOne = KnownOneOut; |
| 980 | break; |
| 981 | } |
| 982 | case Instruction::Select: |
| 983 | if (SimplifyDemandedBits(I->getOperand(2), DemandedMask, |
| 984 | KnownZero, KnownOne, Depth+1)) |
| 985 | return true; |
| 986 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, |
| 987 | KnownZero2, KnownOne2, Depth+1)) |
| 988 | return true; |
| 989 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 990 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 991 | |
| 992 | // If the operands are constants, see if we can simplify them. |
| 993 | if (ShrinkDemandedConstant(I, 1, DemandedMask)) |
| 994 | return UpdateValueUsesWith(I, I); |
| 995 | if (ShrinkDemandedConstant(I, 2, DemandedMask)) |
| 996 | return UpdateValueUsesWith(I, I); |
| 997 | |
| 998 | // Only known if known in both the LHS and RHS. |
| 999 | KnownOne &= KnownOne2; |
| 1000 | KnownZero &= KnownZero2; |
| 1001 | break; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1002 | case Instruction::Trunc: |
| 1003 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, |
| 1004 | KnownZero, KnownOne, Depth+1)) |
| 1005 | return true; |
| 1006 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 1007 | break; |
| 1008 | case Instruction::BitCast: |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1009 | if (!I->getOperand(0)->getType()->isInteger()) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1010 | return false; |
Chris Lattner | f6bd07c | 2006-09-16 03:14:10 +0000 | [diff] [blame] | 1011 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1012 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, |
| 1013 | KnownZero, KnownOne, Depth+1)) |
| 1014 | return true; |
| 1015 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 1016 | break; |
| 1017 | case Instruction::ZExt: { |
| 1018 | // Compute the bits in the result that are not present in the input. |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 1019 | const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType()); |
| 1020 | uint64_t NotIn = ~SrcTy->getBitMask(); |
| 1021 | uint64_t NewBits = cast<IntegerType>(I->getType())->getBitMask() & NotIn; |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1022 | |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 1023 | DemandedMask &= SrcTy->getBitMask(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1024 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, |
| 1025 | KnownZero, KnownOne, Depth+1)) |
| 1026 | return true; |
| 1027 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 1028 | // The top bits are known to be zero. |
| 1029 | KnownZero |= NewBits; |
| 1030 | break; |
| 1031 | } |
| 1032 | case Instruction::SExt: { |
| 1033 | // Compute the bits in the result that are not present in the input. |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 1034 | const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType()); |
| 1035 | uint64_t NotIn = ~SrcTy->getBitMask(); |
| 1036 | uint64_t NewBits = cast<IntegerType>(I->getType())->getBitMask() & NotIn; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1037 | |
| 1038 | // Get the sign bit for the source type |
| 1039 | uint64_t InSignBit = 1ULL << (SrcTy->getPrimitiveSizeInBits()-1); |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 1040 | int64_t InputDemandedBits = DemandedMask & SrcTy->getBitMask(); |
Chris Lattner | f345fe4 | 2006-02-13 22:41:07 +0000 | [diff] [blame] | 1041 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1042 | // If any of the sign extended bits are demanded, we know that the sign |
| 1043 | // bit is demanded. |
| 1044 | if (NewBits & DemandedMask) |
| 1045 | InputDemandedBits |= InSignBit; |
Chris Lattner | f345fe4 | 2006-02-13 22:41:07 +0000 | [diff] [blame] | 1046 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1047 | if (SimplifyDemandedBits(I->getOperand(0), InputDemandedBits, |
| 1048 | KnownZero, KnownOne, Depth+1)) |
| 1049 | return true; |
| 1050 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1051 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1052 | // If the sign bit of the input is known set or clear, then we know the |
| 1053 | // top bits of the result. |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 1054 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1055 | // If the input sign bit is known zero, or if the NewBits are not demanded |
| 1056 | // convert this into a zero extension. |
| 1057 | if ((KnownZero & InSignBit) || (NewBits & ~DemandedMask) == NewBits) { |
| 1058 | // Convert to ZExt cast |
| 1059 | CastInst *NewCast = CastInst::create( |
| 1060 | Instruction::ZExt, I->getOperand(0), I->getType(), I->getName(), I); |
| 1061 | return UpdateValueUsesWith(I, NewCast); |
| 1062 | } else if (KnownOne & InSignBit) { // Input sign bit known set |
| 1063 | KnownOne |= NewBits; |
| 1064 | KnownZero &= ~NewBits; |
| 1065 | } else { // Input sign bit unknown |
| 1066 | KnownZero &= ~NewBits; |
| 1067 | KnownOne &= ~NewBits; |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 1068 | } |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1069 | break; |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 1070 | } |
Chris Lattner | b4a2f05 | 2006-11-09 05:12:27 +0000 | [diff] [blame] | 1071 | case Instruction::Add: |
| 1072 | // If there is a constant on the RHS, there are a variety of xformations |
| 1073 | // we can do. |
| 1074 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 1075 | // If null, this should be simplified elsewhere. Some of the xforms here |
| 1076 | // won't work if the RHS is zero. |
| 1077 | if (RHS->isNullValue()) |
| 1078 | break; |
| 1079 | |
| 1080 | // Figure out what the input bits are. If the top bits of the and result |
| 1081 | // are not demanded, then the add doesn't demand them from its input |
| 1082 | // either. |
| 1083 | |
| 1084 | // Shift the demanded mask up so that it's at the top of the uint64_t. |
| 1085 | unsigned BitWidth = I->getType()->getPrimitiveSizeInBits(); |
| 1086 | unsigned NLZ = CountLeadingZeros_64(DemandedMask << (64-BitWidth)); |
| 1087 | |
| 1088 | // If the top bit of the output is demanded, demand everything from the |
| 1089 | // input. Otherwise, we demand all the input bits except NLZ top bits. |
Jeff Cohen | dfc1299 | 2007-01-08 20:17:17 +0000 | [diff] [blame] | 1090 | uint64_t InDemandedBits = ~0ULL >> (64-BitWidth+NLZ); |
Chris Lattner | b4a2f05 | 2006-11-09 05:12:27 +0000 | [diff] [blame] | 1091 | |
| 1092 | // Find information about known zero/one bits in the input. |
| 1093 | if (SimplifyDemandedBits(I->getOperand(0), InDemandedBits, |
| 1094 | KnownZero2, KnownOne2, Depth+1)) |
| 1095 | return true; |
| 1096 | |
| 1097 | // If the RHS of the add has bits set that can't affect the input, reduce |
| 1098 | // the constant. |
| 1099 | if (ShrinkDemandedConstant(I, 1, InDemandedBits)) |
| 1100 | return UpdateValueUsesWith(I, I); |
| 1101 | |
| 1102 | // Avoid excess work. |
| 1103 | if (KnownZero2 == 0 && KnownOne2 == 0) |
| 1104 | break; |
| 1105 | |
| 1106 | // Turn it into OR if input bits are zero. |
| 1107 | if ((KnownZero2 & RHS->getZExtValue()) == RHS->getZExtValue()) { |
| 1108 | Instruction *Or = |
| 1109 | BinaryOperator::createOr(I->getOperand(0), I->getOperand(1), |
| 1110 | I->getName()); |
| 1111 | InsertNewInstBefore(Or, *I); |
| 1112 | return UpdateValueUsesWith(I, Or); |
| 1113 | } |
| 1114 | |
| 1115 | // We can say something about the output known-zero and known-one bits, |
| 1116 | // depending on potential carries from the input constant and the |
| 1117 | // unknowns. For example if the LHS is known to have at most the 0x0F0F0 |
| 1118 | // bits set and the RHS constant is 0x01001, then we know we have a known |
| 1119 | // one mask of 0x00001 and a known zero mask of 0xE0F0E. |
| 1120 | |
| 1121 | // To compute this, we first compute the potential carry bits. These are |
| 1122 | // the bits which may be modified. I'm not aware of a better way to do |
| 1123 | // this scan. |
| 1124 | uint64_t RHSVal = RHS->getZExtValue(); |
| 1125 | |
| 1126 | bool CarryIn = false; |
| 1127 | uint64_t CarryBits = 0; |
| 1128 | uint64_t CurBit = 1; |
| 1129 | for (unsigned i = 0; i != BitWidth; ++i, CurBit <<= 1) { |
| 1130 | // Record the current carry in. |
| 1131 | if (CarryIn) CarryBits |= CurBit; |
| 1132 | |
| 1133 | bool CarryOut; |
| 1134 | |
| 1135 | // This bit has a carry out unless it is "zero + zero" or |
| 1136 | // "zero + anything" with no carry in. |
| 1137 | if ((KnownZero2 & CurBit) && ((RHSVal & CurBit) == 0)) { |
| 1138 | CarryOut = false; // 0 + 0 has no carry out, even with carry in. |
| 1139 | } else if (!CarryIn && |
| 1140 | ((KnownZero2 & CurBit) || ((RHSVal & CurBit) == 0))) { |
| 1141 | CarryOut = false; // 0 + anything has no carry out if no carry in. |
| 1142 | } else { |
| 1143 | // Otherwise, we have to assume we have a carry out. |
| 1144 | CarryOut = true; |
| 1145 | } |
| 1146 | |
| 1147 | // This stage's carry out becomes the next stage's carry-in. |
| 1148 | CarryIn = CarryOut; |
| 1149 | } |
| 1150 | |
| 1151 | // Now that we know which bits have carries, compute the known-1/0 sets. |
| 1152 | |
| 1153 | // Bits are known one if they are known zero in one operand and one in the |
| 1154 | // other, and there is no input carry. |
| 1155 | KnownOne = ((KnownZero2 & RHSVal) | (KnownOne2 & ~RHSVal)) & ~CarryBits; |
| 1156 | |
| 1157 | // Bits are known zero if they are known zero in both operands and there |
| 1158 | // is no input carry. |
| 1159 | KnownZero = KnownZero2 & ~RHSVal & ~CarryBits; |
| 1160 | } |
| 1161 | break; |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 1162 | case Instruction::Shl: |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1163 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 1164 | uint64_t ShiftAmt = SA->getZExtValue(); |
| 1165 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask >> ShiftAmt, |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1166 | KnownZero, KnownOne, Depth+1)) |
| 1167 | return true; |
| 1168 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1169 | KnownZero <<= ShiftAmt; |
| 1170 | KnownOne <<= ShiftAmt; |
| 1171 | KnownZero |= (1ULL << ShiftAmt) - 1; // low bits known zero. |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1172 | } |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 1173 | break; |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1174 | case Instruction::LShr: |
| 1175 | // For a logical shift right |
| 1176 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 1177 | unsigned ShiftAmt = SA->getZExtValue(); |
| 1178 | |
| 1179 | // Compute the new bits that are at the top now. |
| 1180 | uint64_t HighBits = (1ULL << ShiftAmt)-1; |
| 1181 | HighBits <<= I->getType()->getPrimitiveSizeInBits() - ShiftAmt; |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 1182 | uint64_t TypeMask = cast<IntegerType>(I->getType())->getBitMask(); |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1183 | // Unsigned shift right. |
| 1184 | if (SimplifyDemandedBits(I->getOperand(0), |
| 1185 | (DemandedMask << ShiftAmt) & TypeMask, |
| 1186 | KnownZero, KnownOne, Depth+1)) |
| 1187 | return true; |
| 1188 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 1189 | KnownZero &= TypeMask; |
| 1190 | KnownOne &= TypeMask; |
| 1191 | KnownZero >>= ShiftAmt; |
| 1192 | KnownOne >>= ShiftAmt; |
| 1193 | KnownZero |= HighBits; // high bits known zero. |
| 1194 | } |
| 1195 | break; |
| 1196 | case Instruction::AShr: |
Chris Lattner | b736379 | 2006-09-18 04:31:40 +0000 | [diff] [blame] | 1197 | // If this is an arithmetic shift right and only the low-bit is set, we can |
| 1198 | // always convert this into a logical shr, even if the shift amount is |
| 1199 | // variable. The low bit of the shift cannot be an input sign bit unless |
| 1200 | // the shift amount is >= the size of the datatype, which is undefined. |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1201 | if (DemandedMask == 1) { |
| 1202 | // Perform the logical shift right. |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 1203 | Value *NewVal = BinaryOperator::createLShr( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1204 | I->getOperand(0), I->getOperand(1), I->getName()); |
Reid Spencer | 811b0cb | 2006-10-26 19:19:06 +0000 | [diff] [blame] | 1205 | InsertNewInstBefore(cast<Instruction>(NewVal), *I); |
Chris Lattner | b736379 | 2006-09-18 04:31:40 +0000 | [diff] [blame] | 1206 | return UpdateValueUsesWith(I, NewVal); |
| 1207 | } |
| 1208 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1209 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 1210 | unsigned ShiftAmt = SA->getZExtValue(); |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1211 | |
| 1212 | // Compute the new bits that are at the top now. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1213 | uint64_t HighBits = (1ULL << ShiftAmt)-1; |
| 1214 | HighBits <<= I->getType()->getPrimitiveSizeInBits() - ShiftAmt; |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 1215 | uint64_t TypeMask = cast<IntegerType>(I->getType())->getBitMask(); |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1216 | // Signed shift right. |
| 1217 | if (SimplifyDemandedBits(I->getOperand(0), |
| 1218 | (DemandedMask << ShiftAmt) & TypeMask, |
| 1219 | KnownZero, KnownOne, Depth+1)) |
| 1220 | return true; |
| 1221 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 1222 | KnownZero &= TypeMask; |
| 1223 | KnownOne &= TypeMask; |
| 1224 | KnownZero >>= ShiftAmt; |
| 1225 | KnownOne >>= ShiftAmt; |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1226 | |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1227 | // Handle the sign bits. |
| 1228 | uint64_t SignBit = 1ULL << (I->getType()->getPrimitiveSizeInBits()-1); |
| 1229 | SignBit >>= ShiftAmt; // Adjust to where it is now in the mask. |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1230 | |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1231 | // If the input sign bit is known to be zero, or if none of the top bits |
| 1232 | // are demanded, turn this into an unsigned shift right. |
| 1233 | if ((KnownZero & SignBit) || (HighBits & ~DemandedMask) == HighBits) { |
| 1234 | // Perform the logical shift right. |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 1235 | Value *NewVal = BinaryOperator::createLShr( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1236 | I->getOperand(0), SA, I->getName()); |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1237 | InsertNewInstBefore(cast<Instruction>(NewVal), *I); |
| 1238 | return UpdateValueUsesWith(I, NewVal); |
| 1239 | } else if (KnownOne & SignBit) { // New bits are known one. |
| 1240 | KnownOne |= HighBits; |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 1241 | } |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1242 | } |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 1243 | break; |
| 1244 | } |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1245 | |
| 1246 | // If the client is only demanding bits that we know, return the known |
| 1247 | // constant. |
| 1248 | if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1249 | return UpdateValueUsesWith(I, ConstantInt::get(I->getType(), KnownOne)); |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 1250 | return false; |
| 1251 | } |
| 1252 | |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1253 | |
| 1254 | /// SimplifyDemandedVectorElts - The specified value producecs a vector with |
| 1255 | /// 64 or fewer elements. DemandedElts contains the set of elements that are |
| 1256 | /// actually used by the caller. This method analyzes which elements of the |
| 1257 | /// operand are undef and returns that information in UndefElts. |
| 1258 | /// |
| 1259 | /// If the information about demanded elements can be used to simplify the |
| 1260 | /// operation, the operation is simplified, then the resultant value is |
| 1261 | /// returned. This returns null if no change was made. |
| 1262 | Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, uint64_t DemandedElts, |
| 1263 | uint64_t &UndefElts, |
| 1264 | unsigned Depth) { |
| 1265 | unsigned VWidth = cast<PackedType>(V->getType())->getNumElements(); |
| 1266 | assert(VWidth <= 64 && "Vector too wide to analyze!"); |
| 1267 | uint64_t EltMask = ~0ULL >> (64-VWidth); |
| 1268 | assert(DemandedElts != EltMask && (DemandedElts & ~EltMask) == 0 && |
| 1269 | "Invalid DemandedElts!"); |
| 1270 | |
| 1271 | if (isa<UndefValue>(V)) { |
| 1272 | // If the entire vector is undefined, just return this info. |
| 1273 | UndefElts = EltMask; |
| 1274 | return 0; |
| 1275 | } else if (DemandedElts == 0) { // If nothing is demanded, provide undef. |
| 1276 | UndefElts = EltMask; |
| 1277 | return UndefValue::get(V->getType()); |
| 1278 | } |
| 1279 | |
| 1280 | UndefElts = 0; |
| 1281 | if (ConstantPacked *CP = dyn_cast<ConstantPacked>(V)) { |
| 1282 | const Type *EltTy = cast<PackedType>(V->getType())->getElementType(); |
| 1283 | Constant *Undef = UndefValue::get(EltTy); |
| 1284 | |
| 1285 | std::vector<Constant*> Elts; |
| 1286 | for (unsigned i = 0; i != VWidth; ++i) |
| 1287 | if (!(DemandedElts & (1ULL << i))) { // If not demanded, set to undef. |
| 1288 | Elts.push_back(Undef); |
| 1289 | UndefElts |= (1ULL << i); |
| 1290 | } else if (isa<UndefValue>(CP->getOperand(i))) { // Already undef. |
| 1291 | Elts.push_back(Undef); |
| 1292 | UndefElts |= (1ULL << i); |
| 1293 | } else { // Otherwise, defined. |
| 1294 | Elts.push_back(CP->getOperand(i)); |
| 1295 | } |
| 1296 | |
| 1297 | // If we changed the constant, return it. |
| 1298 | Constant *NewCP = ConstantPacked::get(Elts); |
| 1299 | return NewCP != CP ? NewCP : 0; |
| 1300 | } else if (isa<ConstantAggregateZero>(V)) { |
| 1301 | // Simplify the CAZ to a ConstantPacked where the non-demanded elements are |
| 1302 | // set to undef. |
| 1303 | const Type *EltTy = cast<PackedType>(V->getType())->getElementType(); |
| 1304 | Constant *Zero = Constant::getNullValue(EltTy); |
| 1305 | Constant *Undef = UndefValue::get(EltTy); |
| 1306 | std::vector<Constant*> Elts; |
| 1307 | for (unsigned i = 0; i != VWidth; ++i) |
| 1308 | Elts.push_back((DemandedElts & (1ULL << i)) ? Zero : Undef); |
| 1309 | UndefElts = DemandedElts ^ EltMask; |
| 1310 | return ConstantPacked::get(Elts); |
| 1311 | } |
| 1312 | |
| 1313 | if (!V->hasOneUse()) { // Other users may use these bits. |
| 1314 | if (Depth != 0) { // Not at the root. |
| 1315 | // TODO: Just compute the UndefElts information recursively. |
| 1316 | return false; |
| 1317 | } |
| 1318 | return false; |
| 1319 | } else if (Depth == 10) { // Limit search depth. |
| 1320 | return false; |
| 1321 | } |
| 1322 | |
| 1323 | Instruction *I = dyn_cast<Instruction>(V); |
| 1324 | if (!I) return false; // Only analyze instructions. |
| 1325 | |
| 1326 | bool MadeChange = false; |
| 1327 | uint64_t UndefElts2; |
| 1328 | Value *TmpV; |
| 1329 | switch (I->getOpcode()) { |
| 1330 | default: break; |
| 1331 | |
| 1332 | case Instruction::InsertElement: { |
| 1333 | // If this is a variable index, we don't know which element it overwrites. |
| 1334 | // demand exactly the same input as we produce. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1335 | ConstantInt *Idx = dyn_cast<ConstantInt>(I->getOperand(2)); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1336 | if (Idx == 0) { |
| 1337 | // Note that we can't propagate undef elt info, because we don't know |
| 1338 | // which elt is getting updated. |
| 1339 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, |
| 1340 | UndefElts2, Depth+1); |
| 1341 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } |
| 1342 | break; |
| 1343 | } |
| 1344 | |
| 1345 | // If this is inserting an element that isn't demanded, remove this |
| 1346 | // insertelement. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1347 | unsigned IdxNo = Idx->getZExtValue(); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1348 | if (IdxNo >= VWidth || (DemandedElts & (1ULL << IdxNo)) == 0) |
| 1349 | return AddSoonDeadInstToWorklist(*I, 0); |
| 1350 | |
| 1351 | // Otherwise, the element inserted overwrites whatever was there, so the |
| 1352 | // input demanded set is simpler than the output set. |
| 1353 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), |
| 1354 | DemandedElts & ~(1ULL << IdxNo), |
| 1355 | UndefElts, Depth+1); |
| 1356 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } |
| 1357 | |
| 1358 | // The inserted element is defined. |
| 1359 | UndefElts |= 1ULL << IdxNo; |
| 1360 | break; |
| 1361 | } |
| 1362 | |
| 1363 | case Instruction::And: |
| 1364 | case Instruction::Or: |
| 1365 | case Instruction::Xor: |
| 1366 | case Instruction::Add: |
| 1367 | case Instruction::Sub: |
| 1368 | case Instruction::Mul: |
| 1369 | // div/rem demand all inputs, because they don't want divide by zero. |
| 1370 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, |
| 1371 | UndefElts, Depth+1); |
| 1372 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } |
| 1373 | TmpV = SimplifyDemandedVectorElts(I->getOperand(1), DemandedElts, |
| 1374 | UndefElts2, Depth+1); |
| 1375 | if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; } |
| 1376 | |
| 1377 | // Output elements are undefined if both are undefined. Consider things |
| 1378 | // like undef&0. The result is known zero, not undef. |
| 1379 | UndefElts &= UndefElts2; |
| 1380 | break; |
| 1381 | |
| 1382 | case Instruction::Call: { |
| 1383 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(I); |
| 1384 | if (!II) break; |
| 1385 | switch (II->getIntrinsicID()) { |
| 1386 | default: break; |
| 1387 | |
| 1388 | // Binary vector operations that work column-wise. A dest element is a |
| 1389 | // function of the corresponding input elements from the two inputs. |
| 1390 | case Intrinsic::x86_sse_sub_ss: |
| 1391 | case Intrinsic::x86_sse_mul_ss: |
| 1392 | case Intrinsic::x86_sse_min_ss: |
| 1393 | case Intrinsic::x86_sse_max_ss: |
| 1394 | case Intrinsic::x86_sse2_sub_sd: |
| 1395 | case Intrinsic::x86_sse2_mul_sd: |
| 1396 | case Intrinsic::x86_sse2_min_sd: |
| 1397 | case Intrinsic::x86_sse2_max_sd: |
| 1398 | TmpV = SimplifyDemandedVectorElts(II->getOperand(1), DemandedElts, |
| 1399 | UndefElts, Depth+1); |
| 1400 | if (TmpV) { II->setOperand(1, TmpV); MadeChange = true; } |
| 1401 | TmpV = SimplifyDemandedVectorElts(II->getOperand(2), DemandedElts, |
| 1402 | UndefElts2, Depth+1); |
| 1403 | if (TmpV) { II->setOperand(2, TmpV); MadeChange = true; } |
| 1404 | |
| 1405 | // If only the low elt is demanded and this is a scalarizable intrinsic, |
| 1406 | // scalarize it now. |
| 1407 | if (DemandedElts == 1) { |
| 1408 | switch (II->getIntrinsicID()) { |
| 1409 | default: break; |
| 1410 | case Intrinsic::x86_sse_sub_ss: |
| 1411 | case Intrinsic::x86_sse_mul_ss: |
| 1412 | case Intrinsic::x86_sse2_sub_sd: |
| 1413 | case Intrinsic::x86_sse2_mul_sd: |
| 1414 | // TODO: Lower MIN/MAX/ABS/etc |
| 1415 | Value *LHS = II->getOperand(1); |
| 1416 | Value *RHS = II->getOperand(2); |
| 1417 | // Extract the element as scalars. |
| 1418 | LHS = InsertNewInstBefore(new ExtractElementInst(LHS, 0U,"tmp"), *II); |
| 1419 | RHS = InsertNewInstBefore(new ExtractElementInst(RHS, 0U,"tmp"), *II); |
| 1420 | |
| 1421 | switch (II->getIntrinsicID()) { |
| 1422 | default: assert(0 && "Case stmts out of sync!"); |
| 1423 | case Intrinsic::x86_sse_sub_ss: |
| 1424 | case Intrinsic::x86_sse2_sub_sd: |
| 1425 | TmpV = InsertNewInstBefore(BinaryOperator::createSub(LHS, RHS, |
| 1426 | II->getName()), *II); |
| 1427 | break; |
| 1428 | case Intrinsic::x86_sse_mul_ss: |
| 1429 | case Intrinsic::x86_sse2_mul_sd: |
| 1430 | TmpV = InsertNewInstBefore(BinaryOperator::createMul(LHS, RHS, |
| 1431 | II->getName()), *II); |
| 1432 | break; |
| 1433 | } |
| 1434 | |
| 1435 | Instruction *New = |
| 1436 | new InsertElementInst(UndefValue::get(II->getType()), TmpV, 0U, |
| 1437 | II->getName()); |
| 1438 | InsertNewInstBefore(New, *II); |
| 1439 | AddSoonDeadInstToWorklist(*II, 0); |
| 1440 | return New; |
| 1441 | } |
| 1442 | } |
| 1443 | |
| 1444 | // Output elements are undefined if both are undefined. Consider things |
| 1445 | // like undef&0. The result is known zero, not undef. |
| 1446 | UndefElts &= UndefElts2; |
| 1447 | break; |
| 1448 | } |
| 1449 | break; |
| 1450 | } |
| 1451 | } |
| 1452 | return MadeChange ? I : 0; |
| 1453 | } |
| 1454 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1455 | /// @returns true if the specified compare instruction is |
| 1456 | /// true when both operands are equal... |
| 1457 | /// @brief Determine if the ICmpInst returns true if both operands are equal |
| 1458 | static bool isTrueWhenEqual(ICmpInst &ICI) { |
| 1459 | ICmpInst::Predicate pred = ICI.getPredicate(); |
| 1460 | return pred == ICmpInst::ICMP_EQ || pred == ICmpInst::ICMP_UGE || |
| 1461 | pred == ICmpInst::ICMP_SGE || pred == ICmpInst::ICMP_ULE || |
| 1462 | pred == ICmpInst::ICMP_SLE; |
| 1463 | } |
| 1464 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1465 | /// AssociativeOpt - Perform an optimization on an associative operator. This |
| 1466 | /// function is designed to check a chain of associative operators for a |
| 1467 | /// potential to apply a certain optimization. Since the optimization may be |
| 1468 | /// applicable if the expression was reassociated, this checks the chain, then |
| 1469 | /// reassociates the expression as necessary to expose the optimization |
| 1470 | /// opportunity. This makes use of a special Functor, which must define |
| 1471 | /// 'shouldApply' and 'apply' methods. |
| 1472 | /// |
| 1473 | template<typename Functor> |
| 1474 | Instruction *AssociativeOpt(BinaryOperator &Root, const Functor &F) { |
| 1475 | unsigned Opcode = Root.getOpcode(); |
| 1476 | Value *LHS = Root.getOperand(0); |
| 1477 | |
| 1478 | // Quick check, see if the immediate LHS matches... |
| 1479 | if (F.shouldApply(LHS)) |
| 1480 | return F.apply(Root); |
| 1481 | |
| 1482 | // Otherwise, if the LHS is not of the same opcode as the root, return. |
| 1483 | Instruction *LHSI = dyn_cast<Instruction>(LHS); |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 1484 | while (LHSI && LHSI->getOpcode() == Opcode && LHSI->hasOneUse()) { |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1485 | // Should we apply this transform to the RHS? |
| 1486 | bool ShouldApply = F.shouldApply(LHSI->getOperand(1)); |
| 1487 | |
| 1488 | // If not to the RHS, check to see if we should apply to the LHS... |
| 1489 | if (!ShouldApply && F.shouldApply(LHSI->getOperand(0))) { |
| 1490 | cast<BinaryOperator>(LHSI)->swapOperands(); // Make the LHS the RHS |
| 1491 | ShouldApply = true; |
| 1492 | } |
| 1493 | |
| 1494 | // If the functor wants to apply the optimization to the RHS of LHSI, |
| 1495 | // reassociate the expression from ((? op A) op B) to (? op (A op B)) |
| 1496 | if (ShouldApply) { |
| 1497 | BasicBlock *BB = Root.getParent(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1498 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1499 | // Now all of the instructions are in the current basic block, go ahead |
| 1500 | // and perform the reassociation. |
| 1501 | Instruction *TmpLHSI = cast<Instruction>(Root.getOperand(0)); |
| 1502 | |
| 1503 | // First move the selected RHS to the LHS of the root... |
| 1504 | Root.setOperand(0, LHSI->getOperand(1)); |
| 1505 | |
| 1506 | // Make what used to be the LHS of the root be the user of the root... |
| 1507 | Value *ExtraOperand = TmpLHSI->getOperand(1); |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1508 | if (&Root == TmpLHSI) { |
Chris Lattner | 15a76c0 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 1509 | Root.replaceAllUsesWith(Constant::getNullValue(TmpLHSI->getType())); |
| 1510 | return 0; |
| 1511 | } |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1512 | Root.replaceAllUsesWith(TmpLHSI); // Users now use TmpLHSI |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1513 | TmpLHSI->setOperand(1, &Root); // TmpLHSI now uses the root |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1514 | TmpLHSI->getParent()->getInstList().remove(TmpLHSI); |
| 1515 | BasicBlock::iterator ARI = &Root; ++ARI; |
| 1516 | BB->getInstList().insert(ARI, TmpLHSI); // Move TmpLHSI to after Root |
| 1517 | ARI = Root; |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1518 | |
| 1519 | // Now propagate the ExtraOperand down the chain of instructions until we |
| 1520 | // get to LHSI. |
| 1521 | while (TmpLHSI != LHSI) { |
| 1522 | Instruction *NextLHSI = cast<Instruction>(TmpLHSI->getOperand(0)); |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1523 | // Move the instruction to immediately before the chain we are |
| 1524 | // constructing to avoid breaking dominance properties. |
| 1525 | NextLHSI->getParent()->getInstList().remove(NextLHSI); |
| 1526 | BB->getInstList().insert(ARI, NextLHSI); |
| 1527 | ARI = NextLHSI; |
| 1528 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1529 | Value *NextOp = NextLHSI->getOperand(1); |
| 1530 | NextLHSI->setOperand(1, ExtraOperand); |
| 1531 | TmpLHSI = NextLHSI; |
| 1532 | ExtraOperand = NextOp; |
| 1533 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1534 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1535 | // Now that the instructions are reassociated, have the functor perform |
| 1536 | // the transformation... |
| 1537 | return F.apply(Root); |
| 1538 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1539 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1540 | LHSI = dyn_cast<Instruction>(LHSI->getOperand(0)); |
| 1541 | } |
| 1542 | return 0; |
| 1543 | } |
| 1544 | |
| 1545 | |
| 1546 | // AddRHS - Implements: X + X --> X << 1 |
| 1547 | struct AddRHS { |
| 1548 | Value *RHS; |
| 1549 | AddRHS(Value *rhs) : RHS(rhs) {} |
| 1550 | bool shouldApply(Value *LHS) const { return LHS == RHS; } |
| 1551 | Instruction *apply(BinaryOperator &Add) const { |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 1552 | return BinaryOperator::createShl(Add.getOperand(0), |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1553 | ConstantInt::get(Add.getType(), 1)); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1554 | } |
| 1555 | }; |
| 1556 | |
| 1557 | // AddMaskingAnd - Implements (A & C1)+(B & C2) --> (A & C1)|(B & C2) |
| 1558 | // iff C1&C2 == 0 |
| 1559 | struct AddMaskingAnd { |
| 1560 | Constant *C2; |
| 1561 | AddMaskingAnd(Constant *c) : C2(c) {} |
| 1562 | bool shouldApply(Value *LHS) const { |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1563 | ConstantInt *C1; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1564 | return match(LHS, m_And(m_Value(), m_ConstantInt(C1))) && |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1565 | ConstantExpr::getAnd(C1, C2)->isNullValue(); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1566 | } |
| 1567 | Instruction *apply(BinaryOperator &Add) const { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1568 | return BinaryOperator::createOr(Add.getOperand(0), Add.getOperand(1)); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1569 | } |
| 1570 | }; |
| 1571 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1572 | static Value *FoldOperationIntoSelectOperand(Instruction &I, Value *SO, |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1573 | InstCombiner *IC) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1574 | if (CastInst *CI = dyn_cast<CastInst>(&I)) { |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1575 | if (Constant *SOC = dyn_cast<Constant>(SO)) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1576 | return ConstantExpr::getCast(CI->getOpcode(), SOC, I.getType()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1577 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1578 | return IC->InsertNewInstBefore(CastInst::create( |
| 1579 | CI->getOpcode(), SO, I.getType(), SO->getName() + ".cast"), I); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1580 | } |
| 1581 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1582 | // Figure out if the constant is the left or the right argument. |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1583 | bool ConstIsRHS = isa<Constant>(I.getOperand(1)); |
| 1584 | Constant *ConstOperand = cast<Constant>(I.getOperand(ConstIsRHS)); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1585 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1586 | if (Constant *SOC = dyn_cast<Constant>(SO)) { |
| 1587 | if (ConstIsRHS) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1588 | return ConstantExpr::get(I.getOpcode(), SOC, ConstOperand); |
| 1589 | return ConstantExpr::get(I.getOpcode(), ConstOperand, SOC); |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1590 | } |
| 1591 | |
| 1592 | Value *Op0 = SO, *Op1 = ConstOperand; |
| 1593 | if (!ConstIsRHS) |
| 1594 | std::swap(Op0, Op1); |
| 1595 | Instruction *New; |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1596 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I)) |
| 1597 | New = BinaryOperator::create(BO->getOpcode(), Op0, Op1,SO->getName()+".op"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1598 | else if (CmpInst *CI = dyn_cast<CmpInst>(&I)) |
| 1599 | New = CmpInst::create(CI->getOpcode(), CI->getPredicate(), Op0, Op1, |
| 1600 | SO->getName()+".cmp"); |
Chris Lattner | 326c0f3 | 2004-04-10 19:15:56 +0000 | [diff] [blame] | 1601 | else { |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1602 | assert(0 && "Unknown binary instruction type!"); |
Chris Lattner | 326c0f3 | 2004-04-10 19:15:56 +0000 | [diff] [blame] | 1603 | abort(); |
| 1604 | } |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1605 | return IC->InsertNewInstBefore(New, I); |
| 1606 | } |
| 1607 | |
| 1608 | // FoldOpIntoSelect - Given an instruction with a select as one operand and a |
| 1609 | // constant as the other operand, try to fold the binary operator into the |
| 1610 | // select arguments. This also works for Cast instructions, which obviously do |
| 1611 | // not have a second operand. |
| 1612 | static Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI, |
| 1613 | InstCombiner *IC) { |
| 1614 | // Don't modify shared select instructions |
| 1615 | if (!SI->hasOneUse()) return 0; |
| 1616 | Value *TV = SI->getOperand(1); |
| 1617 | Value *FV = SI->getOperand(2); |
| 1618 | |
| 1619 | if (isa<Constant>(TV) || isa<Constant>(FV)) { |
Chris Lattner | 956db27 | 2005-04-21 05:43:13 +0000 | [diff] [blame] | 1620 | // Bool selects with constant operands can be folded to logical ops. |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 1621 | if (SI->getType() == Type::Int1Ty) return 0; |
Chris Lattner | 956db27 | 2005-04-21 05:43:13 +0000 | [diff] [blame] | 1622 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1623 | Value *SelectTrueVal = FoldOperationIntoSelectOperand(Op, TV, IC); |
| 1624 | Value *SelectFalseVal = FoldOperationIntoSelectOperand(Op, FV, IC); |
| 1625 | |
| 1626 | return new SelectInst(SI->getCondition(), SelectTrueVal, |
| 1627 | SelectFalseVal); |
| 1628 | } |
| 1629 | return 0; |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1630 | } |
| 1631 | |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1632 | |
| 1633 | /// FoldOpIntoPhi - Given a binary operator or cast instruction which has a PHI |
| 1634 | /// node as operand #0, see if we can fold the instruction into the PHI (which |
| 1635 | /// is only possible if all operands to the PHI are constants). |
| 1636 | Instruction *InstCombiner::FoldOpIntoPhi(Instruction &I) { |
| 1637 | PHINode *PN = cast<PHINode>(I.getOperand(0)); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 1638 | unsigned NumPHIValues = PN->getNumIncomingValues(); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1639 | if (!PN->hasOneUse() || NumPHIValues == 0) return 0; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1640 | |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1641 | // Check to see if all of the operands of the PHI are constants. If there is |
| 1642 | // one non-constant value, remember the BB it is. If there is more than one |
| 1643 | // bail out. |
| 1644 | BasicBlock *NonConstBB = 0; |
| 1645 | for (unsigned i = 0; i != NumPHIValues; ++i) |
| 1646 | if (!isa<Constant>(PN->getIncomingValue(i))) { |
| 1647 | if (NonConstBB) return 0; // More than one non-const value. |
| 1648 | NonConstBB = PN->getIncomingBlock(i); |
| 1649 | |
| 1650 | // If the incoming non-constant value is in I's block, we have an infinite |
| 1651 | // loop. |
| 1652 | if (NonConstBB == I.getParent()) |
| 1653 | return 0; |
| 1654 | } |
| 1655 | |
| 1656 | // If there is exactly one non-constant value, we can insert a copy of the |
| 1657 | // operation in that block. However, if this is a critical edge, we would be |
| 1658 | // inserting the computation one some other paths (e.g. inside a loop). Only |
| 1659 | // do this if the pred block is unconditionally branching into the phi block. |
| 1660 | if (NonConstBB) { |
| 1661 | BranchInst *BI = dyn_cast<BranchInst>(NonConstBB->getTerminator()); |
| 1662 | if (!BI || !BI->isUnconditional()) return 0; |
| 1663 | } |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1664 | |
| 1665 | // Okay, we can do the transformation: create the new PHI node. |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 1666 | PHINode *NewPN = new PHINode(I.getType(), ""); |
Chris Lattner | 5551706 | 2005-01-29 00:39:08 +0000 | [diff] [blame] | 1667 | NewPN->reserveOperandSpace(PN->getNumOperands()/2); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1668 | InsertNewInstBefore(NewPN, *PN); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 1669 | NewPN->takeName(PN); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1670 | |
| 1671 | // Next, add all of the operands to the PHI. |
| 1672 | if (I.getNumOperands() == 2) { |
| 1673 | Constant *C = cast<Constant>(I.getOperand(1)); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 1674 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1675 | Value *InV; |
| 1676 | if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1677 | if (CmpInst *CI = dyn_cast<CmpInst>(&I)) |
| 1678 | InV = ConstantExpr::getCompare(CI->getPredicate(), InC, C); |
| 1679 | else |
| 1680 | InV = ConstantExpr::get(I.getOpcode(), InC, C); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1681 | } else { |
| 1682 | assert(PN->getIncomingBlock(i) == NonConstBB); |
| 1683 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I)) |
| 1684 | InV = BinaryOperator::create(BO->getOpcode(), |
| 1685 | PN->getIncomingValue(i), C, "phitmp", |
| 1686 | NonConstBB->getTerminator()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1687 | else if (CmpInst *CI = dyn_cast<CmpInst>(&I)) |
| 1688 | InV = CmpInst::create(CI->getOpcode(), |
| 1689 | CI->getPredicate(), |
| 1690 | PN->getIncomingValue(i), C, "phitmp", |
| 1691 | NonConstBB->getTerminator()); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1692 | else |
| 1693 | assert(0 && "Unknown binop!"); |
| 1694 | |
| 1695 | WorkList.push_back(cast<Instruction>(InV)); |
| 1696 | } |
| 1697 | NewPN->addIncoming(InV, PN->getIncomingBlock(i)); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1698 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1699 | } else { |
| 1700 | CastInst *CI = cast<CastInst>(&I); |
| 1701 | const Type *RetTy = CI->getType(); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 1702 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1703 | Value *InV; |
| 1704 | if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1705 | InV = ConstantExpr::getCast(CI->getOpcode(), InC, RetTy); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1706 | } else { |
| 1707 | assert(PN->getIncomingBlock(i) == NonConstBB); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1708 | InV = CastInst::create(CI->getOpcode(), PN->getIncomingValue(i), |
| 1709 | I.getType(), "phitmp", |
| 1710 | NonConstBB->getTerminator()); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1711 | WorkList.push_back(cast<Instruction>(InV)); |
| 1712 | } |
| 1713 | NewPN->addIncoming(InV, PN->getIncomingBlock(i)); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1714 | } |
| 1715 | } |
| 1716 | return ReplaceInstUsesWith(I, NewPN); |
| 1717 | } |
| 1718 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1719 | Instruction *InstCombiner::visitAdd(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 1720 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1721 | Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 1722 | |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 1723 | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 1724 | // X + undef -> undef |
| 1725 | if (isa<UndefValue>(RHS)) |
| 1726 | return ReplaceInstUsesWith(I, RHS); |
| 1727 | |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 1728 | // X + 0 --> X |
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 1729 | if (!I.getType()->isFPOrFPVector()) { // NOTE: -0 + +0 = +0. |
Chris Lattner | 5e678e0 | 2005-10-17 17:56:38 +0000 | [diff] [blame] | 1730 | if (RHSC->isNullValue()) |
| 1731 | return ReplaceInstUsesWith(I, LHS); |
Chris Lattner | 8532cf6 | 2005-10-17 20:18:38 +0000 | [diff] [blame] | 1732 | } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) { |
| 1733 | if (CFP->isExactlyValue(-0.0)) |
| 1734 | return ReplaceInstUsesWith(I, LHS); |
Chris Lattner | 5e678e0 | 2005-10-17 17:56:38 +0000 | [diff] [blame] | 1735 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1736 | |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 1737 | if (ConstantInt *CI = dyn_cast<ConstantInt>(RHSC)) { |
Chris Lattner | b4a2f05 | 2006-11-09 05:12:27 +0000 | [diff] [blame] | 1738 | // X + (signbit) --> X ^ signbit |
Chris Lattner | 74c51a0 | 2006-02-07 08:05:22 +0000 | [diff] [blame] | 1739 | uint64_t Val = CI->getZExtValue(); |
Chris Lattner | 1a074fc | 2006-02-07 07:00:41 +0000 | [diff] [blame] | 1740 | if (Val == (1ULL << (CI->getType()->getPrimitiveSizeInBits()-1))) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1741 | return BinaryOperator::createXor(LHS, RHS); |
Chris Lattner | b4a2f05 | 2006-11-09 05:12:27 +0000 | [diff] [blame] | 1742 | |
| 1743 | // See if SimplifyDemandedBits can simplify this. This handles stuff like |
| 1744 | // (X & 254)+1 -> (X&254)|1 |
| 1745 | uint64_t KnownZero, KnownOne; |
| 1746 | if (!isa<PackedType>(I.getType()) && |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 1747 | SimplifyDemandedBits(&I, cast<IntegerType>(I.getType())->getBitMask(), |
Chris Lattner | b4a2f05 | 2006-11-09 05:12:27 +0000 | [diff] [blame] | 1748 | KnownZero, KnownOne)) |
| 1749 | return &I; |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 1750 | } |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1751 | |
| 1752 | if (isa<PHINode>(LHS)) |
| 1753 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 1754 | return NV; |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1755 | |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 1756 | ConstantInt *XorRHS = 0; |
| 1757 | Value *XorLHS = 0; |
Chris Lattner | c5eff44 | 2007-01-30 22:32:46 +0000 | [diff] [blame] | 1758 | if (isa<ConstantInt>(RHSC) && |
| 1759 | match(LHS, m_Xor(m_Value(XorLHS), m_ConstantInt(XorRHS)))) { |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1760 | unsigned TySizeBits = I.getType()->getPrimitiveSizeInBits(); |
| 1761 | int64_t RHSSExt = cast<ConstantInt>(RHSC)->getSExtValue(); |
| 1762 | uint64_t RHSZExt = cast<ConstantInt>(RHSC)->getZExtValue(); |
| 1763 | |
| 1764 | uint64_t C0080Val = 1ULL << 31; |
| 1765 | int64_t CFF80Val = -C0080Val; |
| 1766 | unsigned Size = 32; |
| 1767 | do { |
| 1768 | if (TySizeBits > Size) { |
| 1769 | bool Found = false; |
| 1770 | // If we have ADD(XOR(AND(X, 0xFF), 0x80), 0xF..F80), it's a sext. |
| 1771 | // If we have ADD(XOR(AND(X, 0xFF), 0xF..F80), 0x80), it's a sext. |
| 1772 | if (RHSSExt == CFF80Val) { |
| 1773 | if (XorRHS->getZExtValue() == C0080Val) |
| 1774 | Found = true; |
| 1775 | } else if (RHSZExt == C0080Val) { |
| 1776 | if (XorRHS->getSExtValue() == CFF80Val) |
| 1777 | Found = true; |
| 1778 | } |
| 1779 | if (Found) { |
| 1780 | // This is a sign extend if the top bits are known zero. |
Chris Lattner | 68d5ff2 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 1781 | uint64_t Mask = ~0ULL; |
Chris Lattner | 3bedbd9 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 1782 | Mask <<= 64-(TySizeBits-Size); |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 1783 | Mask &= cast<IntegerType>(XorLHS->getType())->getBitMask(); |
Chris Lattner | 3bedbd9 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 1784 | if (!MaskedValueIsZero(XorLHS, Mask)) |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1785 | Size = 0; // Not a sign ext, but can't be any others either. |
| 1786 | goto FoundSExt; |
| 1787 | } |
| 1788 | } |
| 1789 | Size >>= 1; |
| 1790 | C0080Val >>= Size; |
| 1791 | CFF80Val >>= Size; |
| 1792 | } while (Size >= 8); |
| 1793 | |
| 1794 | FoundSExt: |
| 1795 | const Type *MiddleType = 0; |
| 1796 | switch (Size) { |
| 1797 | default: break; |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 1798 | case 32: MiddleType = Type::Int32Ty; break; |
| 1799 | case 16: MiddleType = Type::Int16Ty; break; |
| 1800 | case 8: MiddleType = Type::Int8Ty; break; |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1801 | } |
| 1802 | if (MiddleType) { |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 1803 | Instruction *NewTrunc = new TruncInst(XorLHS, MiddleType, "sext"); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1804 | InsertNewInstBefore(NewTrunc, I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1805 | return new SExtInst(NewTrunc, I.getType()); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1806 | } |
| 1807 | } |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 1808 | } |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 1809 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1810 | // X + X --> X << 1 |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1811 | if (I.getType()->isInteger() && I.getType() != Type::Int1Ty) { |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1812 | if (Instruction *Result = AssociativeOpt(I, AddRHS(RHS))) return Result; |
Chris Lattner | 7edc8c2 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 1813 | |
| 1814 | if (Instruction *RHSI = dyn_cast<Instruction>(RHS)) { |
| 1815 | if (RHSI->getOpcode() == Instruction::Sub) |
| 1816 | if (LHS == RHSI->getOperand(1)) // A + (B - A) --> B |
| 1817 | return ReplaceInstUsesWith(I, RHSI->getOperand(0)); |
| 1818 | } |
| 1819 | if (Instruction *LHSI = dyn_cast<Instruction>(LHS)) { |
| 1820 | if (LHSI->getOpcode() == Instruction::Sub) |
| 1821 | if (RHS == LHSI->getOperand(1)) // (B - A) + A --> B |
| 1822 | return ReplaceInstUsesWith(I, LHSI->getOperand(0)); |
| 1823 | } |
Robert Bocchino | 7169828 | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 1824 | } |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 1825 | |
Chris Lattner | 5c4afb9 | 2002-05-08 22:46:53 +0000 | [diff] [blame] | 1826 | // -A + B --> B - A |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 1827 | if (Value *V = dyn_castNegVal(LHS)) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1828 | return BinaryOperator::createSub(RHS, V); |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 1829 | |
| 1830 | // A + -B --> A - B |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 1831 | if (!isa<Constant>(RHS)) |
| 1832 | if (Value *V = dyn_castNegVal(RHS)) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1833 | return BinaryOperator::createSub(LHS, V); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1834 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1835 | |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 1836 | ConstantInt *C2; |
| 1837 | if (Value *X = dyn_castFoldableMul(LHS, C2)) { |
| 1838 | if (X == RHS) // X*C + X --> X * (C+1) |
| 1839 | return BinaryOperator::createMul(RHS, AddOne(C2)); |
| 1840 | |
| 1841 | // X*C1 + X*C2 --> X * (C1+C2) |
| 1842 | ConstantInt *C1; |
| 1843 | if (X == dyn_castFoldableMul(RHS, C1)) |
| 1844 | return BinaryOperator::createMul(X, ConstantExpr::getAdd(C1, C2)); |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 1845 | } |
| 1846 | |
| 1847 | // X + X*C --> X * (C+1) |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 1848 | if (dyn_castFoldableMul(RHS, C2) == LHS) |
| 1849 | return BinaryOperator::createMul(LHS, AddOne(C2)); |
| 1850 | |
Chris Lattner | e617c9e | 2007-01-05 02:17:46 +0000 | [diff] [blame] | 1851 | // X + ~X --> -1 since ~X = -X-1 |
| 1852 | if (dyn_castNotVal(LHS) == RHS || |
| 1853 | dyn_castNotVal(RHS) == LHS) |
| 1854 | return ReplaceInstUsesWith(I, ConstantInt::getAllOnesValue(I.getType())); |
| 1855 | |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 1856 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1857 | // (A & C1)+(B & C2) --> (A & C1)|(B & C2) iff C1&C2 == 0 |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1858 | if (match(RHS, m_And(m_Value(), m_ConstantInt(C2)))) |
Chris Lattner | e617c9e | 2007-01-05 02:17:46 +0000 | [diff] [blame] | 1859 | if (Instruction *R = AssociativeOpt(I, AddMaskingAnd(C2))) |
| 1860 | return R; |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 1861 | |
Chris Lattner | 6b03205 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 1862 | if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) { |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 1863 | Value *X = 0; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1864 | if (match(LHS, m_Not(m_Value(X)))) { // ~X + C --> (C-1) - X |
| 1865 | Constant *C= ConstantExpr::getSub(CRHS, ConstantInt::get(I.getType(), 1)); |
| 1866 | return BinaryOperator::createSub(C, X); |
Chris Lattner | 6b03205 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 1867 | } |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1868 | |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 1869 | // (X & FF00) + xx00 -> (X+xx00) & FF00 |
| 1870 | if (LHS->hasOneUse() && match(LHS, m_And(m_Value(X), m_ConstantInt(C2)))) { |
| 1871 | Constant *Anded = ConstantExpr::getAnd(CRHS, C2); |
| 1872 | if (Anded == CRHS) { |
| 1873 | // See if all bits from the first bit set in the Add RHS up are included |
| 1874 | // in the mask. First, get the rightmost bit. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1875 | uint64_t AddRHSV = CRHS->getZExtValue(); |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 1876 | |
| 1877 | // Form a mask of all bits from the lowest bit added through the top. |
| 1878 | uint64_t AddRHSHighBits = ~((AddRHSV & -AddRHSV)-1); |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 1879 | AddRHSHighBits &= C2->getType()->getBitMask(); |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 1880 | |
| 1881 | // See if the and mask includes all of these bits. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1882 | uint64_t AddRHSHighBitsAnd = AddRHSHighBits & C2->getZExtValue(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1883 | |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 1884 | if (AddRHSHighBits == AddRHSHighBitsAnd) { |
| 1885 | // Okay, the xform is safe. Insert the new add pronto. |
| 1886 | Value *NewAdd = InsertNewInstBefore(BinaryOperator::createAdd(X, CRHS, |
| 1887 | LHS->getName()), I); |
| 1888 | return BinaryOperator::createAnd(NewAdd, C2); |
| 1889 | } |
| 1890 | } |
| 1891 | } |
| 1892 | |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1893 | // Try to fold constant add into select arguments. |
| 1894 | if (SelectInst *SI = dyn_cast<SelectInst>(LHS)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1895 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1896 | return R; |
Chris Lattner | 6b03205 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 1897 | } |
| 1898 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1899 | // add (cast *A to intptrtype) B -> |
| 1900 | // cast (GEP (cast *A to sbyte*) B) -> |
| 1901 | // intptrtype |
Andrew Lenharth | 16d7955 | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 1902 | { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1903 | CastInst *CI = dyn_cast<CastInst>(LHS); |
| 1904 | Value *Other = RHS; |
Andrew Lenharth | 16d7955 | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 1905 | if (!CI) { |
| 1906 | CI = dyn_cast<CastInst>(RHS); |
| 1907 | Other = LHS; |
| 1908 | } |
Andrew Lenharth | 4563326 | 2006-09-20 15:37:57 +0000 | [diff] [blame] | 1909 | if (CI && CI->getType()->isSized() && |
Reid Spencer | abaa8ca | 2007-01-08 16:32:00 +0000 | [diff] [blame] | 1910 | (CI->getType()->getPrimitiveSizeInBits() == |
| 1911 | TD->getIntPtrType()->getPrimitiveSizeInBits()) |
Andrew Lenharth | 4563326 | 2006-09-20 15:37:57 +0000 | [diff] [blame] | 1912 | && isa<PointerType>(CI->getOperand(0)->getType())) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 1913 | Value *I2 = InsertCastBefore(Instruction::BitCast, CI->getOperand(0), |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 1914 | PointerType::get(Type::Int8Ty), I); |
Andrew Lenharth | 4563326 | 2006-09-20 15:37:57 +0000 | [diff] [blame] | 1915 | I2 = InsertNewInstBefore(new GetElementPtrInst(I2, Other, "ctg2"), I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1916 | return new PtrToIntInst(I2, CI->getType()); |
Andrew Lenharth | 16d7955 | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 1917 | } |
| 1918 | } |
| 1919 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1920 | return Changed ? &I : 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1921 | } |
| 1922 | |
Chris Lattner | 1ba5bcd | 2003-07-22 21:46:59 +0000 | [diff] [blame] | 1923 | // isSignBit - Return true if the value represented by the constant only has the |
| 1924 | // highest order bit set. |
| 1925 | static bool isSignBit(ConstantInt *CI) { |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 1926 | unsigned NumBits = CI->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1927 | return (CI->getZExtValue() & (~0ULL >> (64-NumBits))) == (1ULL << (NumBits-1)); |
Chris Lattner | 1ba5bcd | 2003-07-22 21:46:59 +0000 | [diff] [blame] | 1928 | } |
| 1929 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1930 | Instruction *InstCombiner::visitSub(BinaryOperator &I) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1931 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1932 | |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1933 | if (Op0 == Op1) // sub X, X -> 0 |
| 1934 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1935 | |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1936 | // If this is a 'B = x-(-A)', change to B = x+A... |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 1937 | if (Value *V = dyn_castNegVal(Op1)) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1938 | return BinaryOperator::createAdd(Op0, V); |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 1939 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 1940 | if (isa<UndefValue>(Op0)) |
| 1941 | return ReplaceInstUsesWith(I, Op0); // undef - X -> undef |
| 1942 | if (isa<UndefValue>(Op1)) |
| 1943 | return ReplaceInstUsesWith(I, Op1); // X - undef -> undef |
| 1944 | |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 1945 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op0)) { |
| 1946 | // Replace (-1 - A) with (~A)... |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1947 | if (C->isAllOnesValue()) |
| 1948 | return BinaryOperator::createNot(Op1); |
Chris Lattner | 4037171 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 1949 | |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 1950 | // C - ~X == X + (1+C) |
Reid Spencer | 4b828e6 | 2005-06-18 17:37:34 +0000 | [diff] [blame] | 1951 | Value *X = 0; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1952 | if (match(Op1, m_Not(m_Value(X)))) |
| 1953 | return BinaryOperator::createAdd(X, |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1954 | ConstantExpr::getAdd(C, ConstantInt::get(I.getType(), 1))); |
Chris Lattner | 76b7a06 | 2007-01-15 07:02:54 +0000 | [diff] [blame] | 1955 | // -(X >>u 31) -> (X >>s 31) |
| 1956 | // -(X >>s 31) -> (X >>u 31) |
Chris Lattner | bfe492b | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 1957 | if (C->isNullValue()) { |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1958 | if (BinaryOperator *SI = dyn_cast<BinaryOperator>(Op1)) |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1959 | if (SI->getOpcode() == Instruction::LShr) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1960 | if (ConstantInt *CU = dyn_cast<ConstantInt>(SI->getOperand(1))) { |
Chris Lattner | 9c29067 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 1961 | // Check to see if we are shifting out everything but the sign bit. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1962 | if (CU->getZExtValue() == |
| 1963 | SI->getType()->getPrimitiveSizeInBits()-1) { |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1964 | // Ok, the transformation is safe. Insert AShr. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1965 | return BinaryOperator::create(Instruction::AShr, |
| 1966 | SI->getOperand(0), CU, SI->getName()); |
Chris Lattner | 9c29067 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 1967 | } |
| 1968 | } |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1969 | } |
| 1970 | else if (SI->getOpcode() == Instruction::AShr) { |
| 1971 | if (ConstantInt *CU = dyn_cast<ConstantInt>(SI->getOperand(1))) { |
| 1972 | // Check to see if we are shifting out everything but the sign bit. |
| 1973 | if (CU->getZExtValue() == |
| 1974 | SI->getType()->getPrimitiveSizeInBits()-1) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 1975 | // Ok, the transformation is safe. Insert LShr. |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 1976 | return BinaryOperator::createLShr( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1977 | SI->getOperand(0), CU, SI->getName()); |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1978 | } |
| 1979 | } |
| 1980 | } |
Chris Lattner | bfe492b | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 1981 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1982 | |
| 1983 | // Try to fold constant sub into select arguments. |
| 1984 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1985 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1986 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1987 | |
| 1988 | if (isa<PHINode>(Op0)) |
| 1989 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 1990 | return NV; |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 1991 | } |
| 1992 | |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 1993 | if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) { |
| 1994 | if (Op1I->getOpcode() == Instruction::Add && |
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 1995 | !Op0->getType()->isFPOrFPVector()) { |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 1996 | if (Op1I->getOperand(0) == Op0) // X-(X+Y) == -Y |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 1997 | return BinaryOperator::createNeg(Op1I->getOperand(1), I.getName()); |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 1998 | else if (Op1I->getOperand(1) == Op0) // X-(Y+X) == -Y |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 1999 | return BinaryOperator::createNeg(Op1I->getOperand(0), I.getName()); |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2000 | else if (ConstantInt *CI1 = dyn_cast<ConstantInt>(I.getOperand(0))) { |
| 2001 | if (ConstantInt *CI2 = dyn_cast<ConstantInt>(Op1I->getOperand(1))) |
| 2002 | // C1-(X+C2) --> (C1-C2)-X |
| 2003 | return BinaryOperator::createSub(ConstantExpr::getSub(CI1, CI2), |
| 2004 | Op1I->getOperand(0)); |
| 2005 | } |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2006 | } |
| 2007 | |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 2008 | if (Op1I->hasOneUse()) { |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2009 | // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression |
| 2010 | // is not used by anyone else... |
| 2011 | // |
Chris Lattner | 0517e72 | 2004-02-02 20:09:56 +0000 | [diff] [blame] | 2012 | if (Op1I->getOpcode() == Instruction::Sub && |
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 2013 | !Op1I->getType()->isFPOrFPVector()) { |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2014 | // Swap the two operands of the subexpr... |
| 2015 | Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1); |
| 2016 | Op1I->setOperand(0, IIOp1); |
| 2017 | Op1I->setOperand(1, IIOp0); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2018 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2019 | // Create the new top level add instruction... |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2020 | return BinaryOperator::createAdd(Op0, Op1); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2021 | } |
| 2022 | |
| 2023 | // Replace (A - (A & B)) with (A & ~B) if this is the only use of (A&B)... |
| 2024 | // |
| 2025 | if (Op1I->getOpcode() == Instruction::And && |
| 2026 | (Op1I->getOperand(0) == Op0 || Op1I->getOperand(1) == Op0)) { |
| 2027 | Value *OtherOp = Op1I->getOperand(Op1I->getOperand(0) == Op0); |
| 2028 | |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 2029 | Value *NewNot = |
| 2030 | InsertNewInstBefore(BinaryOperator::createNot(OtherOp, "B.not"), I); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2031 | return BinaryOperator::createAnd(Op0, NewNot); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2032 | } |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2033 | |
Reid Spencer | ac5209e | 2006-10-16 23:08:08 +0000 | [diff] [blame] | 2034 | // 0 - (X sdiv C) -> (X sdiv -C) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2035 | if (Op1I->getOpcode() == Instruction::SDiv) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2036 | if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0)) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2037 | if (CSI->isNullValue()) |
Chris Lattner | 91ccc15 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 2038 | if (Constant *DivRHS = dyn_cast<Constant>(Op1I->getOperand(1))) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2039 | return BinaryOperator::createSDiv(Op1I->getOperand(0), |
Chris Lattner | 91ccc15 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 2040 | ConstantExpr::getNeg(DivRHS)); |
| 2041 | |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2042 | // X - X*C --> X * (1-C) |
Reid Spencer | 4b828e6 | 2005-06-18 17:37:34 +0000 | [diff] [blame] | 2043 | ConstantInt *C2 = 0; |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2044 | if (dyn_castFoldableMul(Op1I, C2) == Op0) { |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2045 | Constant *CP1 = |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2046 | ConstantExpr::getSub(ConstantInt::get(I.getType(), 1), C2); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2047 | return BinaryOperator::createMul(Op0, CP1); |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2048 | } |
Chris Lattner | 4037171 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 2049 | } |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2050 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2051 | |
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 2052 | if (!Op0->getType()->isFPOrFPVector()) |
Chris Lattner | 7edc8c2 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 2053 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) |
| 2054 | if (Op0I->getOpcode() == Instruction::Add) { |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 2055 | if (Op0I->getOperand(0) == Op1) // (Y+X)-Y == X |
| 2056 | return ReplaceInstUsesWith(I, Op0I->getOperand(1)); |
| 2057 | else if (Op0I->getOperand(1) == Op1) // (X+Y)-Y == X |
| 2058 | return ReplaceInstUsesWith(I, Op0I->getOperand(0)); |
Chris Lattner | 7edc8c2 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 2059 | } else if (Op0I->getOpcode() == Instruction::Sub) { |
| 2060 | if (Op0I->getOperand(0) == Op1) // (X-Y)-X == -Y |
| 2061 | return BinaryOperator::createNeg(Op0I->getOperand(1), I.getName()); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 2062 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2063 | |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2064 | ConstantInt *C1; |
| 2065 | if (Value *X = dyn_castFoldableMul(Op0, C1)) { |
| 2066 | if (X == Op1) { // X*C - X --> X * (C-1) |
| 2067 | Constant *CP1 = ConstantExpr::getSub(C1, ConstantInt::get(I.getType(),1)); |
| 2068 | return BinaryOperator::createMul(Op1, CP1); |
| 2069 | } |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2070 | |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2071 | ConstantInt *C2; // X*C1 - X*C2 -> X * (C1-C2) |
| 2072 | if (X == dyn_castFoldableMul(Op1, C2)) |
| 2073 | return BinaryOperator::createMul(Op1, ConstantExpr::getSub(C1, C2)); |
| 2074 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2075 | return 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2076 | } |
| 2077 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2078 | /// isSignBitCheck - Given an exploded icmp instruction, return true if it |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2079 | /// really just returns true if the most significant (sign) bit is set. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2080 | static bool isSignBitCheck(ICmpInst::Predicate pred, ConstantInt *RHS) { |
| 2081 | switch (pred) { |
| 2082 | case ICmpInst::ICMP_SLT: |
| 2083 | // True if LHS s< RHS and RHS == 0 |
| 2084 | return RHS->isNullValue(); |
| 2085 | case ICmpInst::ICMP_SLE: |
| 2086 | // True if LHS s<= RHS and RHS == -1 |
| 2087 | return RHS->isAllOnesValue(); |
| 2088 | case ICmpInst::ICMP_UGE: |
| 2089 | // True if LHS u>= RHS and RHS == high-bit-mask (2^7, 2^15, 2^31, etc) |
| 2090 | return RHS->getZExtValue() == (1ULL << |
| 2091 | (RHS->getType()->getPrimitiveSizeInBits()-1)); |
| 2092 | case ICmpInst::ICMP_UGT: |
| 2093 | // True if LHS u> RHS and RHS == high-bit-mask - 1 |
| 2094 | return RHS->getZExtValue() == |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 2095 | (1ULL << (RHS->getType()->getPrimitiveSizeInBits()-1))-1; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2096 | default: |
| 2097 | return false; |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2098 | } |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2099 | } |
| 2100 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2101 | Instruction *InstCombiner::visitMul(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 2102 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2103 | Value *Op0 = I.getOperand(0); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2104 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2105 | if (isa<UndefValue>(I.getOperand(1))) // undef * X -> 0 |
| 2106 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2107 | |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2108 | // Simplify mul instructions with a constant RHS... |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2109 | if (Constant *Op1 = dyn_cast<Constant>(I.getOperand(1))) { |
| 2110 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 2111 | |
| 2112 | // ((X << C1)*C2) == (X * (C2 << C1)) |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2113 | if (BinaryOperator *SI = dyn_cast<BinaryOperator>(Op0)) |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 2114 | if (SI->getOpcode() == Instruction::Shl) |
| 2115 | if (Constant *ShOp = dyn_cast<Constant>(SI->getOperand(1))) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2116 | return BinaryOperator::createMul(SI->getOperand(0), |
| 2117 | ConstantExpr::getShl(CI, ShOp)); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2118 | |
Chris Lattner | 515c97c | 2003-09-11 22:24:54 +0000 | [diff] [blame] | 2119 | if (CI->isNullValue()) |
| 2120 | return ReplaceInstUsesWith(I, Op1); // X * 0 == 0 |
| 2121 | if (CI->equalsInt(1)) // X * 1 == X |
| 2122 | return ReplaceInstUsesWith(I, Op0); |
| 2123 | if (CI->isAllOnesValue()) // X * -1 == 0 - X |
Chris Lattner | 0af1fab | 2003-06-25 17:09:20 +0000 | [diff] [blame] | 2124 | return BinaryOperator::createNeg(Op0, I.getName()); |
Chris Lattner | 6c1ce21 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 2125 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2126 | int64_t Val = (int64_t)cast<ConstantInt>(CI)->getZExtValue(); |
Chris Lattner | bcd7db5 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 2127 | if (isPowerOf2_64(Val)) { // Replace X*(2^C) with X << C |
| 2128 | uint64_t C = Log2_64(Val); |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 2129 | return BinaryOperator::createShl(Op0, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2130 | ConstantInt::get(Op0->getType(), C)); |
Chris Lattner | bcd7db5 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 2131 | } |
Robert Bocchino | 7169828 | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 2132 | } else if (ConstantFP *Op1F = dyn_cast<ConstantFP>(Op1)) { |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2133 | if (Op1F->isNullValue()) |
| 2134 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 6c1ce21 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 2135 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2136 | // "In IEEE floating point, x*1 is not equivalent to x for nans. However, |
| 2137 | // ANSI says we can drop signals, so we can do this anyway." (from GCC) |
| 2138 | if (Op1F->getValue() == 1.0) |
| 2139 | return ReplaceInstUsesWith(I, Op0); // Eliminate 'mul double %X, 1.0' |
| 2140 | } |
Chris Lattner | ab51f3f | 2006-03-04 06:04:02 +0000 | [diff] [blame] | 2141 | |
| 2142 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) |
| 2143 | if (Op0I->getOpcode() == Instruction::Add && Op0I->hasOneUse() && |
| 2144 | isa<ConstantInt>(Op0I->getOperand(1))) { |
| 2145 | // Canonicalize (X+C1)*C2 -> X*C2+C1*C2. |
| 2146 | Instruction *Add = BinaryOperator::createMul(Op0I->getOperand(0), |
| 2147 | Op1, "tmp"); |
| 2148 | InsertNewInstBefore(Add, I); |
| 2149 | Value *C1C2 = ConstantExpr::getMul(Op1, |
| 2150 | cast<Constant>(Op0I->getOperand(1))); |
| 2151 | return BinaryOperator::createAdd(Add, C1C2); |
| 2152 | |
| 2153 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2154 | |
| 2155 | // Try to fold constant mul into select arguments. |
| 2156 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2157 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2158 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2159 | |
| 2160 | if (isa<PHINode>(Op0)) |
| 2161 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2162 | return NV; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2163 | } |
| 2164 | |
Chris Lattner | a4f445b | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 2165 | if (Value *Op0v = dyn_castNegVal(Op0)) // -X * -Y = X*Y |
| 2166 | if (Value *Op1v = dyn_castNegVal(I.getOperand(1))) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2167 | return BinaryOperator::createMul(Op0v, Op1v); |
Chris Lattner | a4f445b | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 2168 | |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2169 | // If one of the operands of the multiply is a cast from a boolean value, then |
| 2170 | // we know the bool is either zero or one, so this is a 'masking' multiply. |
| 2171 | // See if we can simplify things based on how the boolean was originally |
| 2172 | // formed. |
| 2173 | CastInst *BoolCast = 0; |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 2174 | if (ZExtInst *CI = dyn_cast<ZExtInst>(I.getOperand(0))) |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 2175 | if (CI->getOperand(0)->getType() == Type::Int1Ty) |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2176 | BoolCast = CI; |
| 2177 | if (!BoolCast) |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 2178 | if (ZExtInst *CI = dyn_cast<ZExtInst>(I.getOperand(1))) |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 2179 | if (CI->getOperand(0)->getType() == Type::Int1Ty) |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2180 | BoolCast = CI; |
| 2181 | if (BoolCast) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2182 | if (ICmpInst *SCI = dyn_cast<ICmpInst>(BoolCast->getOperand(0))) { |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2183 | Value *SCIOp0 = SCI->getOperand(0), *SCIOp1 = SCI->getOperand(1); |
| 2184 | const Type *SCOpTy = SCIOp0->getType(); |
| 2185 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2186 | // If the icmp is true iff the sign bit of X is set, then convert this |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2187 | // multiply into a shift/and combination. |
| 2188 | if (isa<ConstantInt>(SCIOp1) && |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2189 | isSignBitCheck(SCI->getPredicate(), cast<ConstantInt>(SCIOp1))) { |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2190 | // Shift the X value right to turn it into "all signbits". |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2191 | Constant *Amt = ConstantInt::get(SCIOp0->getType(), |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 2192 | SCOpTy->getPrimitiveSizeInBits()-1); |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2193 | Value *V = |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2194 | InsertNewInstBefore( |
| 2195 | BinaryOperator::create(Instruction::AShr, SCIOp0, Amt, |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2196 | BoolCast->getOperand(0)->getName()+ |
| 2197 | ".mask"), I); |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2198 | |
| 2199 | // If the multiply type is not the same as the source type, sign extend |
| 2200 | // or truncate to the multiply type. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 2201 | if (I.getType() != V->getType()) { |
| 2202 | unsigned SrcBits = V->getType()->getPrimitiveSizeInBits(); |
| 2203 | unsigned DstBits = I.getType()->getPrimitiveSizeInBits(); |
| 2204 | Instruction::CastOps opcode = |
| 2205 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2206 | (SrcBits < DstBits ? Instruction::SExt : Instruction::Trunc)); |
| 2207 | V = InsertCastBefore(opcode, V, I.getType(), I); |
| 2208 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2209 | |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2210 | Value *OtherOp = Op0 == BoolCast ? I.getOperand(1) : Op0; |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2211 | return BinaryOperator::createAnd(V, OtherOp); |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2212 | } |
| 2213 | } |
| 2214 | } |
| 2215 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2216 | return Changed ? &I : 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2217 | } |
| 2218 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2219 | /// This function implements the transforms on div instructions that work |
| 2220 | /// regardless of the kind of div instruction it is (udiv, sdiv, or fdiv). It is |
| 2221 | /// used by the visitors to those instructions. |
| 2222 | /// @brief Transforms common to all three div instructions |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2223 | Instruction *InstCombiner::commonDivTransforms(BinaryOperator &I) { |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2224 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2225 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2226 | // undef / X -> 0 |
| 2227 | if (isa<UndefValue>(Op0)) |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2228 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2229 | |
| 2230 | // X / undef -> undef |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2231 | if (isa<UndefValue>(Op1)) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2232 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2233 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2234 | // Handle cases involving: div X, (select Cond, Y, Z) |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2235 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { |
| 2236 | // div X, (Cond ? 0 : Y) -> div X, Y. If the div and the select are in the |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2237 | // same basic block, then we replace the select with Y, and the condition |
| 2238 | // of the select with false (if the cond value is in the same BB). If the |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2239 | // select has uses other than the div, this allows them to be simplified |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2240 | // also. Note that div X, Y is just as good as div X, 0 (undef) |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2241 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(1))) |
| 2242 | if (ST->isNullValue()) { |
| 2243 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); |
| 2244 | if (CondI && CondI->getParent() == I.getParent()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2245 | UpdateValueUsesWith(CondI, ConstantInt::getFalse()); |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2246 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
| 2247 | I.setOperand(1, SI->getOperand(2)); |
| 2248 | else |
| 2249 | UpdateValueUsesWith(SI, SI->getOperand(2)); |
| 2250 | return &I; |
| 2251 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2252 | |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2253 | // Likewise for: div X, (Cond ? Y : 0) -> div X, Y |
| 2254 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(2))) |
| 2255 | if (ST->isNullValue()) { |
| 2256 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); |
| 2257 | if (CondI && CondI->getParent() == I.getParent()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2258 | UpdateValueUsesWith(CondI, ConstantInt::getTrue()); |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2259 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
| 2260 | I.setOperand(1, SI->getOperand(1)); |
| 2261 | else |
| 2262 | UpdateValueUsesWith(SI, SI->getOperand(1)); |
| 2263 | return &I; |
| 2264 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2265 | } |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2266 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2267 | return 0; |
| 2268 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2269 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2270 | /// This function implements the transforms common to both integer division |
| 2271 | /// instructions (udiv and sdiv). It is called by the visitors to those integer |
| 2272 | /// division instructions. |
| 2273 | /// @brief Common integer divide transforms |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2274 | Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2275 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2276 | |
| 2277 | if (Instruction *Common = commonDivTransforms(I)) |
| 2278 | return Common; |
| 2279 | |
| 2280 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
| 2281 | // div X, 1 == X |
| 2282 | if (RHS->equalsInt(1)) |
| 2283 | return ReplaceInstUsesWith(I, Op0); |
| 2284 | |
| 2285 | // (X / C1) / C2 -> X / (C1*C2) |
| 2286 | if (Instruction *LHS = dyn_cast<Instruction>(Op0)) |
| 2287 | if (Instruction::BinaryOps(LHS->getOpcode()) == I.getOpcode()) |
| 2288 | if (ConstantInt *LHSRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) { |
| 2289 | return BinaryOperator::create(I.getOpcode(), LHS->getOperand(0), |
| 2290 | ConstantExpr::getMul(RHS, LHSRHS)); |
Chris Lattner | bf70b83 | 2005-04-08 04:03:26 +0000 | [diff] [blame] | 2291 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2292 | |
| 2293 | if (!RHS->isNullValue()) { // avoid X udiv 0 |
| 2294 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 2295 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 2296 | return R; |
| 2297 | if (isa<PHINode>(Op0)) |
| 2298 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2299 | return NV; |
| 2300 | } |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2301 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2302 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2303 | // 0 / X == 0, we don't need to preserve faults! |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2304 | if (ConstantInt *LHS = dyn_cast<ConstantInt>(Op0)) |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2305 | if (LHS->equalsInt(0)) |
| 2306 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2307 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2308 | return 0; |
| 2309 | } |
| 2310 | |
| 2311 | Instruction *InstCombiner::visitUDiv(BinaryOperator &I) { |
| 2312 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2313 | |
| 2314 | // Handle the integer div common cases |
| 2315 | if (Instruction *Common = commonIDivTransforms(I)) |
| 2316 | return Common; |
| 2317 | |
| 2318 | // X udiv C^2 -> X >> C |
| 2319 | // Check to see if this is an unsigned division with an exact power of 2, |
| 2320 | // if so, convert to a right shift. |
| 2321 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op1)) { |
| 2322 | if (uint64_t Val = C->getZExtValue()) // Don't break X / 0 |
| 2323 | if (isPowerOf2_64(Val)) { |
| 2324 | uint64_t ShiftAmt = Log2_64(Val); |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 2325 | return BinaryOperator::createLShr(Op0, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2326 | ConstantInt::get(Op0->getType(), ShiftAmt)); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2327 | } |
| 2328 | } |
| 2329 | |
| 2330 | // X udiv (C1 << N), where C1 is "1<<C2" --> X >> (N+C2) |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2331 | if (BinaryOperator *RHSI = dyn_cast<BinaryOperator>(I.getOperand(1))) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2332 | if (RHSI->getOpcode() == Instruction::Shl && |
| 2333 | isa<ConstantInt>(RHSI->getOperand(0))) { |
| 2334 | uint64_t C1 = cast<ConstantInt>(RHSI->getOperand(0))->getZExtValue(); |
| 2335 | if (isPowerOf2_64(C1)) { |
| 2336 | Value *N = RHSI->getOperand(1); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2337 | const Type *NTy = N->getType(); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2338 | if (uint64_t C2 = Log2_64(C1)) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2339 | Constant *C2V = ConstantInt::get(NTy, C2); |
| 2340 | N = InsertNewInstBefore(BinaryOperator::createAdd(N, C2V, "tmp"), I); |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2341 | } |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 2342 | return BinaryOperator::createLShr(Op0, N); |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2343 | } |
| 2344 | } |
Chris Lattner | c812e5d | 2005-11-05 07:40:31 +0000 | [diff] [blame] | 2345 | } |
| 2346 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2347 | // udiv X, (Select Cond, C1, C2) --> Select Cond, (shr X, C1), (shr X, C2) |
| 2348 | // where C1&C2 are powers of two. |
| 2349 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { |
| 2350 | if (ConstantInt *STO = dyn_cast<ConstantInt>(SI->getOperand(1))) |
| 2351 | if (ConstantInt *SFO = dyn_cast<ConstantInt>(SI->getOperand(2))) |
| 2352 | if (!STO->isNullValue() && !STO->isNullValue()) { |
| 2353 | uint64_t TVA = STO->getZExtValue(), FVA = SFO->getZExtValue(); |
| 2354 | if (isPowerOf2_64(TVA) && isPowerOf2_64(FVA)) { |
| 2355 | // Compute the shift amounts |
| 2356 | unsigned TSA = Log2_64(TVA), FSA = Log2_64(FVA); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2357 | // Construct the "on true" case of the select |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2358 | Constant *TC = ConstantInt::get(Op0->getType(), TSA); |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 2359 | Instruction *TSI = BinaryOperator::createLShr( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2360 | Op0, TC, SI->getName()+".t"); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2361 | TSI = InsertNewInstBefore(TSI, I); |
| 2362 | |
| 2363 | // Construct the "on false" case of the select |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2364 | Constant *FC = ConstantInt::get(Op0->getType(), FSA); |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 2365 | Instruction *FSI = BinaryOperator::createLShr( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2366 | Op0, FC, SI->getName()+".f"); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2367 | FSI = InsertNewInstBefore(FSI, I); |
| 2368 | |
| 2369 | // construct the select instruction and return it. |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2370 | return new SelectInst(SI->getOperand(0), TSI, FSI, SI->getName()); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2371 | } |
| 2372 | } |
| 2373 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2374 | return 0; |
| 2375 | } |
| 2376 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2377 | Instruction *InstCombiner::visitSDiv(BinaryOperator &I) { |
| 2378 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2379 | |
| 2380 | // Handle the integer div common cases |
| 2381 | if (Instruction *Common = commonIDivTransforms(I)) |
| 2382 | return Common; |
| 2383 | |
| 2384 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
| 2385 | // sdiv X, -1 == -X |
| 2386 | if (RHS->isAllOnesValue()) |
| 2387 | return BinaryOperator::createNeg(Op0); |
| 2388 | |
| 2389 | // -X/C -> X/-C |
| 2390 | if (Value *LHSNeg = dyn_castNegVal(Op0)) |
| 2391 | return BinaryOperator::createSDiv(LHSNeg, ConstantExpr::getNeg(RHS)); |
| 2392 | } |
| 2393 | |
| 2394 | // If the sign bits of both operands are zero (i.e. we can prove they are |
| 2395 | // unsigned inputs), turn this into a udiv. |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 2396 | if (I.getType()->isInteger()) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2397 | uint64_t Mask = 1ULL << (I.getType()->getPrimitiveSizeInBits()-1); |
| 2398 | if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) { |
| 2399 | return BinaryOperator::createUDiv(Op0, Op1, I.getName()); |
| 2400 | } |
| 2401 | } |
| 2402 | |
| 2403 | return 0; |
| 2404 | } |
| 2405 | |
| 2406 | Instruction *InstCombiner::visitFDiv(BinaryOperator &I) { |
| 2407 | return commonDivTransforms(I); |
| 2408 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2409 | |
Chris Lattner | db3f873 | 2006-03-02 06:50:58 +0000 | [diff] [blame] | 2410 | /// GetFactor - If we can prove that the specified value is at least a multiple |
| 2411 | /// of some factor, return that factor. |
| 2412 | static Constant *GetFactor(Value *V) { |
| 2413 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) |
| 2414 | return CI; |
| 2415 | |
| 2416 | // Unless we can be tricky, we know this is a multiple of 1. |
| 2417 | Constant *Result = ConstantInt::get(V->getType(), 1); |
| 2418 | |
| 2419 | Instruction *I = dyn_cast<Instruction>(V); |
| 2420 | if (!I) return Result; |
| 2421 | |
| 2422 | if (I->getOpcode() == Instruction::Mul) { |
| 2423 | // Handle multiplies by a constant, etc. |
| 2424 | return ConstantExpr::getMul(GetFactor(I->getOperand(0)), |
| 2425 | GetFactor(I->getOperand(1))); |
| 2426 | } else if (I->getOpcode() == Instruction::Shl) { |
| 2427 | // (X<<C) -> X * (1 << C) |
| 2428 | if (Constant *ShRHS = dyn_cast<Constant>(I->getOperand(1))) { |
| 2429 | ShRHS = ConstantExpr::getShl(Result, ShRHS); |
| 2430 | return ConstantExpr::getMul(GetFactor(I->getOperand(0)), ShRHS); |
| 2431 | } |
| 2432 | } else if (I->getOpcode() == Instruction::And) { |
| 2433 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 2434 | // X & 0xFFF0 is known to be a multiple of 16. |
| 2435 | unsigned Zeros = CountTrailingZeros_64(RHS->getZExtValue()); |
| 2436 | if (Zeros != V->getType()->getPrimitiveSizeInBits()) |
| 2437 | return ConstantExpr::getShl(Result, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2438 | ConstantInt::get(Result->getType(), Zeros)); |
Chris Lattner | db3f873 | 2006-03-02 06:50:58 +0000 | [diff] [blame] | 2439 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2440 | } else if (CastInst *CI = dyn_cast<CastInst>(I)) { |
Chris Lattner | db3f873 | 2006-03-02 06:50:58 +0000 | [diff] [blame] | 2441 | // Only handle int->int casts. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2442 | if (!CI->isIntegerCast()) |
| 2443 | return Result; |
| 2444 | Value *Op = CI->getOperand(0); |
| 2445 | return ConstantExpr::getCast(CI->getOpcode(), GetFactor(Op), V->getType()); |
Chris Lattner | db3f873 | 2006-03-02 06:50:58 +0000 | [diff] [blame] | 2446 | } |
| 2447 | return Result; |
| 2448 | } |
| 2449 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2450 | /// This function implements the transforms on rem instructions that work |
| 2451 | /// regardless of the kind of rem instruction it is (urem, srem, or frem). It |
| 2452 | /// is used by the visitors to those instructions. |
| 2453 | /// @brief Transforms common to all three rem instructions |
| 2454 | Instruction *InstCombiner::commonRemTransforms(BinaryOperator &I) { |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2455 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2456 | |
Chris Lattner | 19ccd5c | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 2457 | // 0 % X == 0, we don't need to preserve faults! |
| 2458 | if (Constant *LHS = dyn_cast<Constant>(Op0)) |
| 2459 | if (LHS->isNullValue()) |
| 2460 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2461 | |
| 2462 | if (isa<UndefValue>(Op0)) // undef % X -> 0 |
| 2463 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2464 | if (isa<UndefValue>(Op1)) |
| 2465 | return ReplaceInstUsesWith(I, Op1); // X % undef -> undef |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2466 | |
| 2467 | // Handle cases involving: rem X, (select Cond, Y, Z) |
| 2468 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { |
| 2469 | // rem X, (Cond ? 0 : Y) -> rem X, Y. If the rem and the select are in |
| 2470 | // the same basic block, then we replace the select with Y, and the |
| 2471 | // condition of the select with false (if the cond value is in the same |
| 2472 | // BB). If the select has uses other than the div, this allows them to be |
| 2473 | // simplified also. |
| 2474 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(1))) |
| 2475 | if (ST->isNullValue()) { |
| 2476 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); |
| 2477 | if (CondI && CondI->getParent() == I.getParent()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2478 | UpdateValueUsesWith(CondI, ConstantInt::getFalse()); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2479 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
| 2480 | I.setOperand(1, SI->getOperand(2)); |
| 2481 | else |
| 2482 | UpdateValueUsesWith(SI, SI->getOperand(2)); |
Chris Lattner | 5b73c08 | 2004-07-06 07:01:22 +0000 | [diff] [blame] | 2483 | return &I; |
| 2484 | } |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2485 | // Likewise for: rem X, (Cond ? Y : 0) -> rem X, Y |
| 2486 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(2))) |
| 2487 | if (ST->isNullValue()) { |
| 2488 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); |
| 2489 | if (CondI && CondI->getParent() == I.getParent()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2490 | UpdateValueUsesWith(CondI, ConstantInt::getTrue()); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2491 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
| 2492 | I.setOperand(1, SI->getOperand(1)); |
| 2493 | else |
| 2494 | UpdateValueUsesWith(SI, SI->getOperand(1)); |
| 2495 | return &I; |
| 2496 | } |
Chris Lattner | 11a49f2 | 2005-11-05 07:28:37 +0000 | [diff] [blame] | 2497 | } |
Chris Lattner | 5b73c08 | 2004-07-06 07:01:22 +0000 | [diff] [blame] | 2498 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2499 | return 0; |
| 2500 | } |
| 2501 | |
| 2502 | /// This function implements the transforms common to both integer remainder |
| 2503 | /// instructions (urem and srem). It is called by the visitors to those integer |
| 2504 | /// remainder instructions. |
| 2505 | /// @brief Common integer remainder transforms |
| 2506 | Instruction *InstCombiner::commonIRemTransforms(BinaryOperator &I) { |
| 2507 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2508 | |
| 2509 | if (Instruction *common = commonRemTransforms(I)) |
| 2510 | return common; |
| 2511 | |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2512 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 19ccd5c | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 2513 | // X % 0 == undef, we don't need to preserve faults! |
| 2514 | if (RHS->equalsInt(0)) |
| 2515 | return ReplaceInstUsesWith(I, UndefValue::get(I.getType())); |
| 2516 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2517 | if (RHS->equalsInt(1)) // X % 1 == 0 |
| 2518 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 2519 | |
Chris Lattner | 9794392 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 2520 | if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) { |
| 2521 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0I)) { |
| 2522 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 2523 | return R; |
| 2524 | } else if (isa<PHINode>(Op0I)) { |
| 2525 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2526 | return NV; |
Chris Lattner | 9794392 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 2527 | } |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2528 | // (X * C1) % C2 --> 0 iff C1 % C2 == 0 |
| 2529 | if (ConstantExpr::getSRem(GetFactor(Op0I), RHS)->isNullValue()) |
Chris Lattner | db3f873 | 2006-03-02 06:50:58 +0000 | [diff] [blame] | 2530 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | 9794392 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 2531 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2532 | } |
| 2533 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2534 | return 0; |
| 2535 | } |
| 2536 | |
| 2537 | Instruction *InstCombiner::visitURem(BinaryOperator &I) { |
| 2538 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2539 | |
| 2540 | if (Instruction *common = commonIRemTransforms(I)) |
| 2541 | return common; |
| 2542 | |
| 2543 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
| 2544 | // X urem C^2 -> X and C |
| 2545 | // Check to see if this is an unsigned remainder with an exact power of 2, |
| 2546 | // if so, convert to a bitwise and. |
| 2547 | if (ConstantInt *C = dyn_cast<ConstantInt>(RHS)) |
| 2548 | if (isPowerOf2_64(C->getZExtValue())) |
| 2549 | return BinaryOperator::createAnd(Op0, SubOne(C)); |
| 2550 | } |
| 2551 | |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2552 | if (Instruction *RHSI = dyn_cast<Instruction>(I.getOperand(1))) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2553 | // Turn A % (C << N), where C is 2^k, into A & ((C << N)-1) |
| 2554 | if (RHSI->getOpcode() == Instruction::Shl && |
| 2555 | isa<ConstantInt>(RHSI->getOperand(0))) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2556 | unsigned C1 = cast<ConstantInt>(RHSI->getOperand(0))->getZExtValue(); |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2557 | if (isPowerOf2_64(C1)) { |
| 2558 | Constant *N1 = ConstantInt::getAllOnesValue(I.getType()); |
| 2559 | Value *Add = InsertNewInstBefore(BinaryOperator::createAdd(RHSI, N1, |
| 2560 | "tmp"), I); |
| 2561 | return BinaryOperator::createAnd(Op0, Add); |
| 2562 | } |
| 2563 | } |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2564 | } |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2565 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2566 | // urem X, (select Cond, 2^C1, 2^C2) --> select Cond, (and X, C1), (and X, C2) |
| 2567 | // where C1&C2 are powers of two. |
| 2568 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { |
| 2569 | if (ConstantInt *STO = dyn_cast<ConstantInt>(SI->getOperand(1))) |
| 2570 | if (ConstantInt *SFO = dyn_cast<ConstantInt>(SI->getOperand(2))) { |
| 2571 | // STO == 0 and SFO == 0 handled above. |
| 2572 | if (isPowerOf2_64(STO->getZExtValue()) && |
| 2573 | isPowerOf2_64(SFO->getZExtValue())) { |
| 2574 | Value *TrueAnd = InsertNewInstBefore( |
| 2575 | BinaryOperator::createAnd(Op0, SubOne(STO), SI->getName()+".t"), I); |
| 2576 | Value *FalseAnd = InsertNewInstBefore( |
| 2577 | BinaryOperator::createAnd(Op0, SubOne(SFO), SI->getName()+".f"), I); |
| 2578 | return new SelectInst(SI->getOperand(0), TrueAnd, FalseAnd); |
| 2579 | } |
| 2580 | } |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2581 | } |
| 2582 | |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2583 | return 0; |
| 2584 | } |
| 2585 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2586 | Instruction *InstCombiner::visitSRem(BinaryOperator &I) { |
| 2587 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2588 | |
| 2589 | if (Instruction *common = commonIRemTransforms(I)) |
| 2590 | return common; |
| 2591 | |
| 2592 | if (Value *RHSNeg = dyn_castNegVal(Op1)) |
| 2593 | if (!isa<ConstantInt>(RHSNeg) || |
| 2594 | cast<ConstantInt>(RHSNeg)->getSExtValue() > 0) { |
| 2595 | // X % -Y -> X % Y |
| 2596 | AddUsesToWorkList(I); |
| 2597 | I.setOperand(1, RHSNeg); |
| 2598 | return &I; |
| 2599 | } |
| 2600 | |
| 2601 | // If the top bits of both operands are zero (i.e. we can prove they are |
| 2602 | // unsigned inputs), turn this into a urem. |
| 2603 | uint64_t Mask = 1ULL << (I.getType()->getPrimitiveSizeInBits()-1); |
| 2604 | if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) { |
| 2605 | // X srem Y -> X urem Y, iff X and Y don't have sign bit set |
| 2606 | return BinaryOperator::createURem(Op0, Op1, I.getName()); |
| 2607 | } |
| 2608 | |
| 2609 | return 0; |
| 2610 | } |
| 2611 | |
| 2612 | Instruction *InstCombiner::visitFRem(BinaryOperator &I) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2613 | return commonRemTransforms(I); |
| 2614 | } |
| 2615 | |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2616 | // isMaxValueMinusOne - return true if this is Max-1 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2617 | static bool isMaxValueMinusOne(const ConstantInt *C, bool isSigned) { |
| 2618 | if (isSigned) { |
| 2619 | // Calculate 0111111111..11111 |
| 2620 | unsigned TypeBits = C->getType()->getPrimitiveSizeInBits(); |
| 2621 | int64_t Val = INT64_MAX; // All ones |
| 2622 | Val >>= 64-TypeBits; // Shift out unwanted 1 bits... |
| 2623 | return C->getSExtValue() == Val-1; |
| 2624 | } |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 2625 | return C->getZExtValue() == C->getType()->getBitMask()-1; |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2626 | } |
| 2627 | |
| 2628 | // isMinValuePlusOne - return true if this is Min+1 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2629 | static bool isMinValuePlusOne(const ConstantInt *C, bool isSigned) { |
| 2630 | if (isSigned) { |
| 2631 | // Calculate 1111111111000000000000 |
| 2632 | unsigned TypeBits = C->getType()->getPrimitiveSizeInBits(); |
| 2633 | int64_t Val = -1; // All ones |
| 2634 | Val <<= TypeBits-1; // Shift over to the right spot |
| 2635 | return C->getSExtValue() == Val+1; |
| 2636 | } |
| 2637 | return C->getZExtValue() == 1; // unsigned |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2638 | } |
| 2639 | |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2640 | // isOneBitSet - Return true if there is exactly one bit set in the specified |
| 2641 | // constant. |
| 2642 | static bool isOneBitSet(const ConstantInt *CI) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2643 | uint64_t V = CI->getZExtValue(); |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2644 | return V && (V & (V-1)) == 0; |
| 2645 | } |
| 2646 | |
Chris Lattner | b20ba0a | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2647 | #if 0 // Currently unused |
| 2648 | // isLowOnes - Return true if the constant is of the form 0+1+. |
| 2649 | static bool isLowOnes(const ConstantInt *CI) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2650 | uint64_t V = CI->getZExtValue(); |
Chris Lattner | b20ba0a | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2651 | |
| 2652 | // There won't be bits set in parts that the type doesn't contain. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2653 | V &= ConstantInt::getAllOnesValue(CI->getType())->getZExtValue(); |
Chris Lattner | b20ba0a | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2654 | |
| 2655 | uint64_t U = V+1; // If it is low ones, this should be a power of two. |
| 2656 | return U && V && (U & V) == 0; |
| 2657 | } |
| 2658 | #endif |
| 2659 | |
| 2660 | // isHighOnes - Return true if the constant is of the form 1+0+. |
| 2661 | // This is the same as lowones(~X). |
| 2662 | static bool isHighOnes(const ConstantInt *CI) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2663 | uint64_t V = ~CI->getZExtValue(); |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 2664 | if (~V == 0) return false; // 0's does not match "1+" |
Chris Lattner | b20ba0a | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2665 | |
| 2666 | // There won't be bits set in parts that the type doesn't contain. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2667 | V &= ConstantInt::getAllOnesValue(CI->getType())->getZExtValue(); |
Chris Lattner | b20ba0a | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2668 | |
| 2669 | uint64_t U = V+1; // If it is low ones, this should be a power of two. |
| 2670 | return U && V && (U & V) == 0; |
| 2671 | } |
| 2672 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2673 | /// getICmpCode - Encode a icmp predicate into a three bit mask. These bits |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2674 | /// are carefully arranged to allow folding of expressions such as: |
| 2675 | /// |
| 2676 | /// (A < B) | (A > B) --> (A != B) |
| 2677 | /// |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2678 | /// Note that this is only valid if the first and second predicates have the |
| 2679 | /// same sign. Is illegal to do: (A u< B) | (A s> B) |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2680 | /// |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2681 | /// Three bits are used to represent the condition, as follows: |
| 2682 | /// 0 A > B |
| 2683 | /// 1 A == B |
| 2684 | /// 2 A < B |
| 2685 | /// |
| 2686 | /// <=> Value Definition |
| 2687 | /// 000 0 Always false |
| 2688 | /// 001 1 A > B |
| 2689 | /// 010 2 A == B |
| 2690 | /// 011 3 A >= B |
| 2691 | /// 100 4 A < B |
| 2692 | /// 101 5 A != B |
| 2693 | /// 110 6 A <= B |
| 2694 | /// 111 7 Always true |
| 2695 | /// |
| 2696 | static unsigned getICmpCode(const ICmpInst *ICI) { |
| 2697 | switch (ICI->getPredicate()) { |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2698 | // False -> 0 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2699 | case ICmpInst::ICMP_UGT: return 1; // 001 |
| 2700 | case ICmpInst::ICMP_SGT: return 1; // 001 |
| 2701 | case ICmpInst::ICMP_EQ: return 2; // 010 |
| 2702 | case ICmpInst::ICMP_UGE: return 3; // 011 |
| 2703 | case ICmpInst::ICMP_SGE: return 3; // 011 |
| 2704 | case ICmpInst::ICMP_ULT: return 4; // 100 |
| 2705 | case ICmpInst::ICMP_SLT: return 4; // 100 |
| 2706 | case ICmpInst::ICMP_NE: return 5; // 101 |
| 2707 | case ICmpInst::ICMP_ULE: return 6; // 110 |
| 2708 | case ICmpInst::ICMP_SLE: return 6; // 110 |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2709 | // True -> 7 |
| 2710 | default: |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2711 | assert(0 && "Invalid ICmp predicate!"); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2712 | return 0; |
| 2713 | } |
| 2714 | } |
| 2715 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2716 | /// getICmpValue - This is the complement of getICmpCode, which turns an |
| 2717 | /// opcode and two operands into either a constant true or false, or a brand |
| 2718 | /// new /// ICmp instruction. The sign is passed in to determine which kind |
| 2719 | /// of predicate to use in new icmp instructions. |
| 2720 | static Value *getICmpValue(bool sign, unsigned code, Value *LHS, Value *RHS) { |
| 2721 | switch (code) { |
| 2722 | default: assert(0 && "Illegal ICmp code!"); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2723 | case 0: return ConstantInt::getFalse(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2724 | case 1: |
| 2725 | if (sign) |
| 2726 | return new ICmpInst(ICmpInst::ICMP_SGT, LHS, RHS); |
| 2727 | else |
| 2728 | return new ICmpInst(ICmpInst::ICMP_UGT, LHS, RHS); |
| 2729 | case 2: return new ICmpInst(ICmpInst::ICMP_EQ, LHS, RHS); |
| 2730 | case 3: |
| 2731 | if (sign) |
| 2732 | return new ICmpInst(ICmpInst::ICMP_SGE, LHS, RHS); |
| 2733 | else |
| 2734 | return new ICmpInst(ICmpInst::ICMP_UGE, LHS, RHS); |
| 2735 | case 4: |
| 2736 | if (sign) |
| 2737 | return new ICmpInst(ICmpInst::ICMP_SLT, LHS, RHS); |
| 2738 | else |
| 2739 | return new ICmpInst(ICmpInst::ICMP_ULT, LHS, RHS); |
| 2740 | case 5: return new ICmpInst(ICmpInst::ICMP_NE, LHS, RHS); |
| 2741 | case 6: |
| 2742 | if (sign) |
| 2743 | return new ICmpInst(ICmpInst::ICMP_SLE, LHS, RHS); |
| 2744 | else |
| 2745 | return new ICmpInst(ICmpInst::ICMP_ULE, LHS, RHS); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2746 | case 7: return ConstantInt::getTrue(); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2747 | } |
| 2748 | } |
| 2749 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2750 | static bool PredicatesFoldable(ICmpInst::Predicate p1, ICmpInst::Predicate p2) { |
| 2751 | return (ICmpInst::isSignedPredicate(p1) == ICmpInst::isSignedPredicate(p2)) || |
| 2752 | (ICmpInst::isSignedPredicate(p1) && |
| 2753 | (p2 == ICmpInst::ICMP_EQ || p2 == ICmpInst::ICMP_NE)) || |
| 2754 | (ICmpInst::isSignedPredicate(p2) && |
| 2755 | (p1 == ICmpInst::ICMP_EQ || p1 == ICmpInst::ICMP_NE)); |
| 2756 | } |
| 2757 | |
| 2758 | namespace { |
| 2759 | // FoldICmpLogical - Implements (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B) |
| 2760 | struct FoldICmpLogical { |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2761 | InstCombiner &IC; |
| 2762 | Value *LHS, *RHS; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2763 | ICmpInst::Predicate pred; |
| 2764 | FoldICmpLogical(InstCombiner &ic, ICmpInst *ICI) |
| 2765 | : IC(ic), LHS(ICI->getOperand(0)), RHS(ICI->getOperand(1)), |
| 2766 | pred(ICI->getPredicate()) {} |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2767 | bool shouldApply(Value *V) const { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2768 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(V)) |
| 2769 | if (PredicatesFoldable(pred, ICI->getPredicate())) |
| 2770 | return (ICI->getOperand(0) == LHS && ICI->getOperand(1) == RHS || |
| 2771 | ICI->getOperand(0) == RHS && ICI->getOperand(1) == LHS); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2772 | return false; |
| 2773 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2774 | Instruction *apply(Instruction &Log) const { |
| 2775 | ICmpInst *ICI = cast<ICmpInst>(Log.getOperand(0)); |
| 2776 | if (ICI->getOperand(0) != LHS) { |
| 2777 | assert(ICI->getOperand(1) == LHS); |
| 2778 | ICI->swapOperands(); // Swap the LHS and RHS of the ICmp |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2779 | } |
| 2780 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2781 | unsigned LHSCode = getICmpCode(ICI); |
| 2782 | unsigned RHSCode = getICmpCode(cast<ICmpInst>(Log.getOperand(1))); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2783 | unsigned Code; |
| 2784 | switch (Log.getOpcode()) { |
| 2785 | case Instruction::And: Code = LHSCode & RHSCode; break; |
| 2786 | case Instruction::Or: Code = LHSCode | RHSCode; break; |
| 2787 | case Instruction::Xor: Code = LHSCode ^ RHSCode; break; |
Chris Lattner | 021c190 | 2003-09-22 20:33:34 +0000 | [diff] [blame] | 2788 | default: assert(0 && "Illegal logical opcode!"); return 0; |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2789 | } |
| 2790 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2791 | Value *RV = getICmpValue(ICmpInst::isSignedPredicate(pred), Code, LHS, RHS); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2792 | if (Instruction *I = dyn_cast<Instruction>(RV)) |
| 2793 | return I; |
| 2794 | // Otherwise, it's a constant boolean value... |
| 2795 | return IC.ReplaceInstUsesWith(Log, RV); |
| 2796 | } |
| 2797 | }; |
Chris Lattner | d23b5ba | 2006-11-15 04:53:24 +0000 | [diff] [blame] | 2798 | } // end anonymous namespace |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2799 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2800 | // OptAndOp - This handles expressions of the form ((val OP C1) & C2). Where |
| 2801 | // the Op parameter is 'OP', OpRHS is 'C1', and AndRHS is 'C2'. Op is |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2802 | // guaranteed to be a binary operator. |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2803 | Instruction *InstCombiner::OptAndOp(Instruction *Op, |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2804 | ConstantInt *OpRHS, |
| 2805 | ConstantInt *AndRHS, |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2806 | BinaryOperator &TheAnd) { |
| 2807 | Value *X = Op->getOperand(0); |
Chris Lattner | 76f7fe2 | 2004-01-12 19:47:05 +0000 | [diff] [blame] | 2808 | Constant *Together = 0; |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2809 | if (!Op->isShift()) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2810 | Together = ConstantExpr::getAnd(AndRHS, OpRHS); |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2811 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2812 | switch (Op->getOpcode()) { |
| 2813 | case Instruction::Xor: |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2814 | if (Op->hasOneUse()) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2815 | // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2) |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 2816 | Instruction *And = BinaryOperator::createAnd(X, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2817 | InsertNewInstBefore(And, TheAnd); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 2818 | And->takeName(Op); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2819 | return BinaryOperator::createXor(And, Together); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2820 | } |
| 2821 | break; |
| 2822 | case Instruction::Or: |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2823 | if (Together == AndRHS) // (X | C) & C --> C |
| 2824 | return ReplaceInstUsesWith(TheAnd, AndRHS); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2825 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2826 | if (Op->hasOneUse() && Together != OpRHS) { |
| 2827 | // (X | C1) & C2 --> (X | (C1&C2)) & C2 |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 2828 | Instruction *Or = BinaryOperator::createOr(X, Together); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2829 | InsertNewInstBefore(Or, TheAnd); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 2830 | Or->takeName(Op); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2831 | return BinaryOperator::createAnd(Or, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2832 | } |
| 2833 | break; |
| 2834 | case Instruction::Add: |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 2835 | if (Op->hasOneUse()) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2836 | // Adding a one to a single bit bit-field should be turned into an XOR |
| 2837 | // of the bit. First thing to check is to see if this AND is with a |
| 2838 | // single bit constant. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2839 | uint64_t AndRHSV = cast<ConstantInt>(AndRHS)->getZExtValue(); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2840 | |
| 2841 | // Clear bits that are not part of the constant. |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 2842 | AndRHSV &= AndRHS->getType()->getBitMask(); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2843 | |
| 2844 | // If there is only one bit set... |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2845 | if (isOneBitSet(cast<ConstantInt>(AndRHS))) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2846 | // Ok, at this point, we know that we are masking the result of the |
| 2847 | // ADD down to exactly one bit. If the constant we are adding has |
| 2848 | // no bits set below this bit, then we can eliminate the ADD. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2849 | uint64_t AddRHS = cast<ConstantInt>(OpRHS)->getZExtValue(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2850 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2851 | // Check to see if any bits below the one bit set in AndRHSV are set. |
| 2852 | if ((AddRHS & (AndRHSV-1)) == 0) { |
| 2853 | // If not, the only thing that can effect the output of the AND is |
| 2854 | // the bit specified by AndRHSV. If that bit is set, the effect of |
| 2855 | // the XOR is to toggle the bit. If it is clear, then the ADD has |
| 2856 | // no effect. |
| 2857 | if ((AddRHS & AndRHSV) == 0) { // Bit is not set, noop |
| 2858 | TheAnd.setOperand(0, X); |
| 2859 | return &TheAnd; |
| 2860 | } else { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2861 | // Pull the XOR out of the AND. |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 2862 | Instruction *NewAnd = BinaryOperator::createAnd(X, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2863 | InsertNewInstBefore(NewAnd, TheAnd); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 2864 | NewAnd->takeName(Op); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2865 | return BinaryOperator::createXor(NewAnd, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2866 | } |
| 2867 | } |
| 2868 | } |
| 2869 | } |
| 2870 | break; |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 2871 | |
| 2872 | case Instruction::Shl: { |
| 2873 | // We know that the AND will not produce any of the bits shifted in, so if |
| 2874 | // the anded constant includes them, clear them now! |
| 2875 | // |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2876 | Constant *AllOne = ConstantInt::getAllOnesValue(AndRHS->getType()); |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 2877 | Constant *ShlMask = ConstantExpr::getShl(AllOne, OpRHS); |
| 2878 | Constant *CI = ConstantExpr::getAnd(AndRHS, ShlMask); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2879 | |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 2880 | if (CI == ShlMask) { // Masking out bits that the shift already masks |
| 2881 | return ReplaceInstUsesWith(TheAnd, Op); // No need for the and. |
| 2882 | } else if (CI != AndRHS) { // Reducing bits set in and. |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 2883 | TheAnd.setOperand(1, CI); |
| 2884 | return &TheAnd; |
| 2885 | } |
| 2886 | break; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2887 | } |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2888 | case Instruction::LShr: |
| 2889 | { |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 2890 | // We know that the AND will not produce any of the bits shifted in, so if |
| 2891 | // the anded constant includes them, clear them now! This only applies to |
| 2892 | // unsigned shifts, because a signed shr may bring in set bits! |
| 2893 | // |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2894 | Constant *AllOne = ConstantInt::getAllOnesValue(AndRHS->getType()); |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2895 | Constant *ShrMask = ConstantExpr::getLShr(AllOne, OpRHS); |
| 2896 | Constant *CI = ConstantExpr::getAnd(AndRHS, ShrMask); |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 2897 | |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2898 | if (CI == ShrMask) { // Masking out bits that the shift already masks. |
| 2899 | return ReplaceInstUsesWith(TheAnd, Op); |
| 2900 | } else if (CI != AndRHS) { |
| 2901 | TheAnd.setOperand(1, CI); // Reduce bits set in and cst. |
| 2902 | return &TheAnd; |
| 2903 | } |
| 2904 | break; |
| 2905 | } |
| 2906 | case Instruction::AShr: |
| 2907 | // Signed shr. |
| 2908 | // See if this is shifting in some sign extension, then masking it out |
| 2909 | // with an and. |
| 2910 | if (Op->hasOneUse()) { |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2911 | Constant *AllOne = ConstantInt::getAllOnesValue(AndRHS->getType()); |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2912 | Constant *ShrMask = ConstantExpr::getLShr(AllOne, OpRHS); |
Reid Spencer | 7eb7638 | 2006-12-13 17:19:09 +0000 | [diff] [blame] | 2913 | Constant *C = ConstantExpr::getAnd(AndRHS, ShrMask); |
| 2914 | if (C == AndRHS) { // Masking out bits shifted in. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 2915 | // (Val ashr C1) & C2 -> (Val lshr C1) & C2 |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2916 | // Make the argument unsigned. |
| 2917 | Value *ShVal = Op->getOperand(0); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2918 | ShVal = InsertNewInstBefore( |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 2919 | BinaryOperator::createLShr(ShVal, OpRHS, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2920 | Op->getName()), TheAnd); |
Reid Spencer | 7eb7638 | 2006-12-13 17:19:09 +0000 | [diff] [blame] | 2921 | return BinaryOperator::createAnd(ShVal, AndRHS, TheAnd.getName()); |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 2922 | } |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 2923 | } |
| 2924 | break; |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2925 | } |
| 2926 | return 0; |
| 2927 | } |
| 2928 | |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2929 | |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2930 | /// InsertRangeTest - Emit a computation of: (V >= Lo && V < Hi) if Inside is |
| 2931 | /// true, otherwise (V < Lo || V >= Hi). In pratice, we emit the more efficient |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2932 | /// (V-Lo) <u Hi-Lo. This method expects that Lo <= Hi. isSigned indicates |
| 2933 | /// whether to treat the V, Lo and HI as signed or not. IB is the location to |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2934 | /// insert new instructions. |
| 2935 | Instruction *InstCombiner::InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2936 | bool isSigned, bool Inside, |
| 2937 | Instruction &IB) { |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2938 | assert(cast<ConstantInt>(ConstantExpr::getICmp((isSigned ? |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 2939 | ICmpInst::ICMP_SLE:ICmpInst::ICMP_ULE), Lo, Hi))->getZExtValue() && |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2940 | "Lo is not <= Hi in range emission code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2941 | |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2942 | if (Inside) { |
| 2943 | if (Lo == Hi) // Trivially false. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2944 | return new ICmpInst(ICmpInst::ICMP_NE, V, V); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2945 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2946 | // V >= Min && V < Hi --> V < Hi |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2947 | if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2948 | ICmpInst::Predicate pred = (isSigned ? |
| 2949 | ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT); |
| 2950 | return new ICmpInst(pred, V, Hi); |
| 2951 | } |
| 2952 | |
| 2953 | // Emit V-Lo <u Hi-Lo |
| 2954 | Constant *NegLo = ConstantExpr::getNeg(Lo); |
| 2955 | Instruction *Add = BinaryOperator::createAdd(V, NegLo, V->getName()+".off"); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2956 | InsertNewInstBefore(Add, IB); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2957 | Constant *UpperBound = ConstantExpr::getAdd(NegLo, Hi); |
| 2958 | return new ICmpInst(ICmpInst::ICMP_ULT, Add, UpperBound); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2959 | } |
| 2960 | |
| 2961 | if (Lo == Hi) // Trivially true. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2962 | return new ICmpInst(ICmpInst::ICMP_EQ, V, V); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2963 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2964 | // V < Min || V >= Hi ->'V > Hi-1' |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2965 | Hi = SubOne(cast<ConstantInt>(Hi)); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2966 | if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2967 | ICmpInst::Predicate pred = (isSigned ? |
| 2968 | ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT); |
| 2969 | return new ICmpInst(pred, V, Hi); |
| 2970 | } |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2971 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2972 | // Emit V-Lo > Hi-1-Lo |
| 2973 | Constant *NegLo = ConstantExpr::getNeg(Lo); |
| 2974 | Instruction *Add = BinaryOperator::createAdd(V, NegLo, V->getName()+".off"); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2975 | InsertNewInstBefore(Add, IB); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2976 | Constant *LowerBound = ConstantExpr::getAdd(NegLo, Hi); |
| 2977 | return new ICmpInst(ICmpInst::ICMP_UGT, Add, LowerBound); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2978 | } |
| 2979 | |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 2980 | // isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with |
| 2981 | // any number of 0s on either side. The 1s are allowed to wrap from LSB to |
| 2982 | // MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is |
| 2983 | // not, since all 1s are not contiguous. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2984 | static bool isRunOfOnes(ConstantInt *Val, unsigned &MB, unsigned &ME) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2985 | uint64_t V = Val->getZExtValue(); |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 2986 | if (!isShiftedMask_64(V)) return false; |
| 2987 | |
| 2988 | // look for the first zero bit after the run of ones |
| 2989 | MB = 64-CountLeadingZeros_64((V - 1) ^ V); |
| 2990 | // look for the first non-zero bit |
| 2991 | ME = 64-CountLeadingZeros_64(V); |
| 2992 | return true; |
| 2993 | } |
| 2994 | |
| 2995 | |
| 2996 | |
| 2997 | /// FoldLogicalPlusAnd - This is part of an expression (LHS +/- RHS) & Mask, |
| 2998 | /// where isSub determines whether the operator is a sub. If we can fold one of |
| 2999 | /// the following xforms: |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3000 | /// |
| 3001 | /// ((A & N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == Mask |
| 3002 | /// ((A | N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0 |
| 3003 | /// ((A ^ N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0 |
| 3004 | /// |
| 3005 | /// return (A +/- B). |
| 3006 | /// |
| 3007 | Value *InstCombiner::FoldLogicalPlusAnd(Value *LHS, Value *RHS, |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3008 | ConstantInt *Mask, bool isSub, |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3009 | Instruction &I) { |
| 3010 | Instruction *LHSI = dyn_cast<Instruction>(LHS); |
| 3011 | if (!LHSI || LHSI->getNumOperands() != 2 || |
| 3012 | !isa<ConstantInt>(LHSI->getOperand(1))) return 0; |
| 3013 | |
| 3014 | ConstantInt *N = cast<ConstantInt>(LHSI->getOperand(1)); |
| 3015 | |
| 3016 | switch (LHSI->getOpcode()) { |
| 3017 | default: return 0; |
| 3018 | case Instruction::And: |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3019 | if (ConstantExpr::getAnd(N, Mask) == Mask) { |
| 3020 | // If the AndRHS is a power of two minus one (0+1+), this is simple. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3021 | if ((Mask->getZExtValue() & Mask->getZExtValue()+1) == 0) |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3022 | break; |
| 3023 | |
| 3024 | // Otherwise, if Mask is 0+1+0+, and if B is known to have the low 0+ |
| 3025 | // part, we don't need any explicit masks to take them out of A. If that |
| 3026 | // is all N is, ignore it. |
| 3027 | unsigned MB, ME; |
| 3028 | if (isRunOfOnes(Mask, MB, ME)) { // begin/end bit of run, inclusive |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 3029 | uint64_t Mask = cast<IntegerType>(RHS->getType())->getBitMask(); |
Chris Lattner | 3bedbd9 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3030 | Mask >>= 64-MB+1; |
| 3031 | if (MaskedValueIsZero(RHS, Mask)) |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3032 | break; |
| 3033 | } |
| 3034 | } |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3035 | return 0; |
| 3036 | case Instruction::Or: |
| 3037 | case Instruction::Xor: |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3038 | // If the AndRHS is a power of two minus one (0+1+), and N&Mask == 0 |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3039 | if ((Mask->getZExtValue() & Mask->getZExtValue()+1) == 0 && |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3040 | ConstantExpr::getAnd(N, Mask)->isNullValue()) |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3041 | break; |
| 3042 | return 0; |
| 3043 | } |
| 3044 | |
| 3045 | Instruction *New; |
| 3046 | if (isSub) |
| 3047 | New = BinaryOperator::createSub(LHSI->getOperand(0), RHS, "fold"); |
| 3048 | else |
| 3049 | New = BinaryOperator::createAdd(LHSI->getOperand(0), RHS, "fold"); |
| 3050 | return InsertNewInstBefore(New, I); |
| 3051 | } |
| 3052 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3053 | Instruction *InstCombiner::visitAnd(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 3054 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3055 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3056 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3057 | if (isa<UndefValue>(Op1)) // X & undef -> 0 |
| 3058 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 3059 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3060 | // and X, X = X |
| 3061 | if (Op0 == Op1) |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 3062 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3063 | |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3064 | // See if we can simplify any instructions used by the instruction whose sole |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 3065 | // purpose is to compute bits we don't care about. |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 3066 | uint64_t KnownZero, KnownOne; |
Chris Lattner | 696ee0a | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3067 | if (!isa<PackedType>(I.getType())) { |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 3068 | if (SimplifyDemandedBits(&I, cast<IntegerType>(I.getType())->getBitMask(), |
Chris Lattner | 696ee0a | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3069 | KnownZero, KnownOne)) |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 3070 | return &I; |
Chris Lattner | 696ee0a | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3071 | } else { |
| 3072 | if (ConstantPacked *CP = dyn_cast<ConstantPacked>(Op1)) { |
| 3073 | if (CP->isAllOnesValue()) |
| 3074 | return ReplaceInstUsesWith(I, I.getOperand(0)); |
| 3075 | } |
| 3076 | } |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 3077 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3078 | if (ConstantInt *AndRHS = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 7560c3a | 2006-02-08 07:34:50 +0000 | [diff] [blame] | 3079 | uint64_t AndRHSMask = AndRHS->getZExtValue(); |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 3080 | uint64_t TypeMask = cast<IntegerType>(Op0->getType())->getBitMask(); |
Chris Lattner | 7560c3a | 2006-02-08 07:34:50 +0000 | [diff] [blame] | 3081 | uint64_t NotAndRHS = AndRHSMask^TypeMask; |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3082 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3083 | // Optimize a variety of ((val OP C1) & C2) combinations... |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3084 | if (isa<BinaryOperator>(Op0)) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3085 | Instruction *Op0I = cast<Instruction>(Op0); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3086 | Value *Op0LHS = Op0I->getOperand(0); |
| 3087 | Value *Op0RHS = Op0I->getOperand(1); |
| 3088 | switch (Op0I->getOpcode()) { |
| 3089 | case Instruction::Xor: |
| 3090 | case Instruction::Or: |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 3091 | // If the mask is only needed on one incoming arm, push it up. |
| 3092 | if (Op0I->hasOneUse()) { |
| 3093 | if (MaskedValueIsZero(Op0LHS, NotAndRHS)) { |
| 3094 | // Not masking anything out for the LHS, move to RHS. |
| 3095 | Instruction *NewRHS = BinaryOperator::createAnd(Op0RHS, AndRHS, |
| 3096 | Op0RHS->getName()+".masked"); |
| 3097 | InsertNewInstBefore(NewRHS, I); |
| 3098 | return BinaryOperator::create( |
| 3099 | cast<BinaryOperator>(Op0I)->getOpcode(), Op0LHS, NewRHS); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3100 | } |
Chris Lattner | 3bedbd9 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3101 | if (!isa<Constant>(Op0RHS) && |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 3102 | MaskedValueIsZero(Op0RHS, NotAndRHS)) { |
| 3103 | // Not masking anything out for the RHS, move to LHS. |
| 3104 | Instruction *NewLHS = BinaryOperator::createAnd(Op0LHS, AndRHS, |
| 3105 | Op0LHS->getName()+".masked"); |
| 3106 | InsertNewInstBefore(NewLHS, I); |
| 3107 | return BinaryOperator::create( |
| 3108 | cast<BinaryOperator>(Op0I)->getOpcode(), NewLHS, Op0RHS); |
| 3109 | } |
| 3110 | } |
| 3111 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3112 | break; |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3113 | case Instruction::Add: |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3114 | // ((A & N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == AndRHS. |
| 3115 | // ((A | N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0 |
| 3116 | // ((A ^ N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0 |
| 3117 | if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, false, I)) |
| 3118 | return BinaryOperator::createAnd(V, AndRHS); |
| 3119 | if (Value *V = FoldLogicalPlusAnd(Op0RHS, Op0LHS, AndRHS, false, I)) |
| 3120 | return BinaryOperator::createAnd(V, AndRHS); // Add commutes |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3121 | break; |
| 3122 | |
| 3123 | case Instruction::Sub: |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3124 | // ((A & N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == AndRHS. |
| 3125 | // ((A | N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0 |
| 3126 | // ((A ^ N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0 |
| 3127 | if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, true, I)) |
| 3128 | return BinaryOperator::createAnd(V, AndRHS); |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3129 | break; |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3130 | } |
| 3131 | |
Chris Lattner | 5840326 | 2003-07-23 19:25:52 +0000 | [diff] [blame] | 3132 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3133 | if (Instruction *Res = OptAndOp(Op0I, Op0CI, AndRHS, I)) |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3134 | return Res; |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3135 | } else if (CastInst *CI = dyn_cast<CastInst>(Op0)) { |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3136 | // If this is an integer truncation or change from signed-to-unsigned, and |
| 3137 | // if the source is an and/or with immediate, transform it. This |
| 3138 | // frequently occurs for bitfield accesses. |
| 3139 | if (Instruction *CastOp = dyn_cast<Instruction>(CI->getOperand(0))) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3140 | if ((isa<TruncInst>(CI) || isa<BitCastInst>(CI)) && |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3141 | CastOp->getNumOperands() == 2) |
Chris Lattner | 7560c3a | 2006-02-08 07:34:50 +0000 | [diff] [blame] | 3142 | if (ConstantInt *AndCI = dyn_cast<ConstantInt>(CastOp->getOperand(1))) |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3143 | if (CastOp->getOpcode() == Instruction::And) { |
| 3144 | // Change: and (cast (and X, C1) to T), C2 |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3145 | // into : and (cast X to T), trunc_or_bitcast(C1)&C2 |
| 3146 | // This will fold the two constants together, which may allow |
| 3147 | // other simplifications. |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 3148 | Instruction *NewCast = CastInst::createTruncOrBitCast( |
| 3149 | CastOp->getOperand(0), I.getType(), |
| 3150 | CastOp->getName()+".shrunk"); |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3151 | NewCast = InsertNewInstBefore(NewCast, I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3152 | // trunc_or_bitcast(C1)&C2 |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 3153 | Constant *C3 = ConstantExpr::getTruncOrBitCast(AndCI,I.getType()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3154 | C3 = ConstantExpr::getAnd(C3, AndRHS); |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3155 | return BinaryOperator::createAnd(NewCast, C3); |
| 3156 | } else if (CastOp->getOpcode() == Instruction::Or) { |
| 3157 | // Change: and (cast (or X, C1) to T), C2 |
| 3158 | // into : trunc(C1)&C2 iff trunc(C1)&C2 == C2 |
Chris Lattner | bb4e7b2 | 2006-12-12 19:11:20 +0000 | [diff] [blame] | 3159 | Constant *C3 = ConstantExpr::getTruncOrBitCast(AndCI,I.getType()); |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3160 | if (ConstantExpr::getAnd(C3, AndRHS) == AndRHS) // trunc(C1)&C2 |
| 3161 | return ReplaceInstUsesWith(I, AndRHS); |
| 3162 | } |
| 3163 | } |
Chris Lattner | 06782f8 | 2003-07-23 19:36:21 +0000 | [diff] [blame] | 3164 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3165 | |
| 3166 | // Try to fold constant and into select arguments. |
| 3167 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3168 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3169 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 3170 | if (isa<PHINode>(Op0)) |
| 3171 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 3172 | return NV; |
Chris Lattner | c6a8aff | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 3173 | } |
| 3174 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 3175 | Value *Op0NotVal = dyn_castNotVal(Op0); |
| 3176 | Value *Op1NotVal = dyn_castNotVal(Op1); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3177 | |
Chris Lattner | 5b62aa7 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 3178 | if (Op0NotVal == Op1 || Op1NotVal == Op0) // A & ~A == ~A & A == 0 |
| 3179 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 3180 | |
Misha Brukman | cb6267b | 2004-07-30 12:50:08 +0000 | [diff] [blame] | 3181 | // (~A & ~B) == (~(A | B)) - De Morgan's Law |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 3182 | if (Op0NotVal && Op1NotVal && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3183 | Instruction *Or = BinaryOperator::createOr(Op0NotVal, Op1NotVal, |
| 3184 | I.getName()+".demorgan"); |
Chris Lattner | c6a8aff | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 3185 | InsertNewInstBefore(Or, I); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3186 | return BinaryOperator::createNot(Or); |
| 3187 | } |
Chris Lattner | 2082ad9 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3188 | |
| 3189 | { |
| 3190 | Value *A = 0, *B = 0; |
Chris Lattner | 2082ad9 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3191 | if (match(Op0, m_Or(m_Value(A), m_Value(B)))) |
| 3192 | if (A == Op1 || B == Op1) // (A | ?) & A --> A |
| 3193 | return ReplaceInstUsesWith(I, Op1); |
| 3194 | if (match(Op1, m_Or(m_Value(A), m_Value(B)))) |
| 3195 | if (A == Op0 || B == Op0) // A & (A | ?) --> A |
| 3196 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 3197 | |
| 3198 | if (Op0->hasOneUse() && |
| 3199 | match(Op0, m_Xor(m_Value(A), m_Value(B)))) { |
| 3200 | if (A == Op1) { // (A^B)&A -> A&(A^B) |
| 3201 | I.swapOperands(); // Simplify below |
| 3202 | std::swap(Op0, Op1); |
| 3203 | } else if (B == Op1) { // (A^B)&B -> B&(B^A) |
| 3204 | cast<BinaryOperator>(Op0)->swapOperands(); |
| 3205 | I.swapOperands(); // Simplify below |
| 3206 | std::swap(Op0, Op1); |
| 3207 | } |
| 3208 | } |
| 3209 | if (Op1->hasOneUse() && |
| 3210 | match(Op1, m_Xor(m_Value(A), m_Value(B)))) { |
| 3211 | if (B == Op0) { // B&(A^B) -> B&(B^A) |
| 3212 | cast<BinaryOperator>(Op1)->swapOperands(); |
| 3213 | std::swap(A, B); |
| 3214 | } |
| 3215 | if (A == Op0) { // A&(A^B) -> A & ~B |
| 3216 | Instruction *NotB = BinaryOperator::createNot(B, "tmp"); |
| 3217 | InsertNewInstBefore(NotB, I); |
| 3218 | return BinaryOperator::createAnd(A, NotB); |
| 3219 | } |
| 3220 | } |
Chris Lattner | 2082ad9 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3221 | } |
| 3222 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3223 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(Op1)) { |
| 3224 | // (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B) |
| 3225 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS))) |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3226 | return R; |
| 3227 | |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3228 | Value *LHSVal, *RHSVal; |
| 3229 | ConstantInt *LHSCst, *RHSCst; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3230 | ICmpInst::Predicate LHSCC, RHSCC; |
| 3231 | if (match(Op0, m_ICmp(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst)))) |
| 3232 | if (match(RHS, m_ICmp(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst)))) |
| 3233 | if (LHSVal == RHSVal && // Found (X icmp C1) & (X icmp C2) |
| 3234 | // ICMP_[GL]E X, CST is folded to ICMP_[GL]T elsewhere. |
| 3235 | LHSCC != ICmpInst::ICMP_UGE && LHSCC != ICmpInst::ICMP_ULE && |
| 3236 | RHSCC != ICmpInst::ICMP_UGE && RHSCC != ICmpInst::ICMP_ULE && |
| 3237 | LHSCC != ICmpInst::ICMP_SGE && LHSCC != ICmpInst::ICMP_SLE && |
| 3238 | RHSCC != ICmpInst::ICMP_SGE && RHSCC != ICmpInst::ICMP_SLE) { |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3239 | // Ensure that the larger constant is on the RHS. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3240 | ICmpInst::Predicate GT = ICmpInst::isSignedPredicate(LHSCC) ? |
| 3241 | ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; |
| 3242 | Constant *Cmp = ConstantExpr::getICmp(GT, LHSCst, RHSCst); |
| 3243 | ICmpInst *LHS = cast<ICmpInst>(Op0); |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 3244 | if (cast<ConstantInt>(Cmp)->getZExtValue()) { |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3245 | std::swap(LHS, RHS); |
| 3246 | std::swap(LHSCst, RHSCst); |
| 3247 | std::swap(LHSCC, RHSCC); |
| 3248 | } |
| 3249 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3250 | // At this point, we know we have have two icmp instructions |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3251 | // comparing a value against two constants and and'ing the result |
| 3252 | // together. Because of the above check, we know that we only have |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3253 | // icmp eq, icmp ne, icmp [su]lt, and icmp [SU]gt here. We also know |
| 3254 | // (from the FoldICmpLogical check above), that the two constants |
| 3255 | // are not equal and that the larger constant is on the RHS |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3256 | assert(LHSCst != RHSCst && "Compares not folded above?"); |
| 3257 | |
| 3258 | switch (LHSCC) { |
| 3259 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3260 | case ICmpInst::ICMP_EQ: |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3261 | switch (RHSCC) { |
| 3262 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3263 | case ICmpInst::ICMP_EQ: // (X == 13 & X == 15) -> false |
| 3264 | case ICmpInst::ICMP_UGT: // (X == 13 & X > 15) -> false |
| 3265 | case ICmpInst::ICMP_SGT: // (X == 13 & X > 15) -> false |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3266 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3267 | case ICmpInst::ICMP_NE: // (X == 13 & X != 15) -> X == 13 |
| 3268 | case ICmpInst::ICMP_ULT: // (X == 13 & X < 15) -> X == 13 |
| 3269 | case ICmpInst::ICMP_SLT: // (X == 13 & X < 15) -> X == 13 |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3270 | return ReplaceInstUsesWith(I, LHS); |
| 3271 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3272 | case ICmpInst::ICMP_NE: |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3273 | switch (RHSCC) { |
| 3274 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3275 | case ICmpInst::ICMP_ULT: |
| 3276 | if (LHSCst == SubOne(RHSCst)) // (X != 13 & X u< 14) -> X < 13 |
| 3277 | return new ICmpInst(ICmpInst::ICMP_ULT, LHSVal, LHSCst); |
| 3278 | break; // (X != 13 & X u< 15) -> no change |
| 3279 | case ICmpInst::ICMP_SLT: |
| 3280 | if (LHSCst == SubOne(RHSCst)) // (X != 13 & X s< 14) -> X < 13 |
| 3281 | return new ICmpInst(ICmpInst::ICMP_SLT, LHSVal, LHSCst); |
| 3282 | break; // (X != 13 & X s< 15) -> no change |
| 3283 | case ICmpInst::ICMP_EQ: // (X != 13 & X == 15) -> X == 15 |
| 3284 | case ICmpInst::ICMP_UGT: // (X != 13 & X u> 15) -> X u> 15 |
| 3285 | case ICmpInst::ICMP_SGT: // (X != 13 & X s> 15) -> X s> 15 |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3286 | return ReplaceInstUsesWith(I, RHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3287 | case ICmpInst::ICMP_NE: |
| 3288 | if (LHSCst == SubOne(RHSCst)){// (X != 13 & X != 14) -> X-13 >u 1 |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3289 | Constant *AddCST = ConstantExpr::getNeg(LHSCst); |
| 3290 | Instruction *Add = BinaryOperator::createAdd(LHSVal, AddCST, |
| 3291 | LHSVal->getName()+".off"); |
| 3292 | InsertNewInstBefore(Add, I); |
Chris Lattner | 424db02 | 2007-01-27 23:08:34 +0000 | [diff] [blame] | 3293 | return new ICmpInst(ICmpInst::ICMP_UGT, Add, |
| 3294 | ConstantInt::get(Add->getType(), 1)); |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3295 | } |
| 3296 | break; // (X != 13 & X != 15) -> no change |
| 3297 | } |
| 3298 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3299 | case ICmpInst::ICMP_ULT: |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3300 | switch (RHSCC) { |
| 3301 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3302 | case ICmpInst::ICMP_EQ: // (X u< 13 & X == 15) -> false |
| 3303 | case ICmpInst::ICMP_UGT: // (X u< 13 & X u> 15) -> false |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3304 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3305 | case ICmpInst::ICMP_SGT: // (X u< 13 & X s> 15) -> no change |
| 3306 | break; |
| 3307 | case ICmpInst::ICMP_NE: // (X u< 13 & X != 15) -> X u< 13 |
| 3308 | case ICmpInst::ICMP_ULT: // (X u< 13 & X u< 15) -> X u< 13 |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3309 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3310 | case ICmpInst::ICMP_SLT: // (X u< 13 & X s< 15) -> no change |
| 3311 | break; |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3312 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3313 | break; |
| 3314 | case ICmpInst::ICMP_SLT: |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3315 | switch (RHSCC) { |
| 3316 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3317 | case ICmpInst::ICMP_EQ: // (X s< 13 & X == 15) -> false |
| 3318 | case ICmpInst::ICMP_SGT: // (X s< 13 & X s> 15) -> false |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3319 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3320 | case ICmpInst::ICMP_UGT: // (X s< 13 & X u> 15) -> no change |
| 3321 | break; |
| 3322 | case ICmpInst::ICMP_NE: // (X s< 13 & X != 15) -> X < 13 |
| 3323 | case ICmpInst::ICMP_SLT: // (X s< 13 & X s< 15) -> X < 13 |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3324 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3325 | case ICmpInst::ICMP_ULT: // (X s< 13 & X u< 15) -> no change |
| 3326 | break; |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3327 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3328 | break; |
| 3329 | case ICmpInst::ICMP_UGT: |
| 3330 | switch (RHSCC) { |
| 3331 | default: assert(0 && "Unknown integer condition code!"); |
| 3332 | case ICmpInst::ICMP_EQ: // (X u> 13 & X == 15) -> X > 13 |
| 3333 | return ReplaceInstUsesWith(I, LHS); |
| 3334 | case ICmpInst::ICMP_UGT: // (X u> 13 & X u> 15) -> X u> 15 |
| 3335 | return ReplaceInstUsesWith(I, RHS); |
| 3336 | case ICmpInst::ICMP_SGT: // (X u> 13 & X s> 15) -> no change |
| 3337 | break; |
| 3338 | case ICmpInst::ICMP_NE: |
| 3339 | if (RHSCst == AddOne(LHSCst)) // (X u> 13 & X != 14) -> X u> 14 |
| 3340 | return new ICmpInst(LHSCC, LHSVal, RHSCst); |
| 3341 | break; // (X u> 13 & X != 15) -> no change |
| 3342 | case ICmpInst::ICMP_ULT: // (X u> 13 & X u< 15) ->(X-14) <u 1 |
| 3343 | return InsertRangeTest(LHSVal, AddOne(LHSCst), RHSCst, false, |
| 3344 | true, I); |
| 3345 | case ICmpInst::ICMP_SLT: // (X u> 13 & X s< 15) -> no change |
| 3346 | break; |
| 3347 | } |
| 3348 | break; |
| 3349 | case ICmpInst::ICMP_SGT: |
| 3350 | switch (RHSCC) { |
| 3351 | default: assert(0 && "Unknown integer condition code!"); |
| 3352 | case ICmpInst::ICMP_EQ: // (X s> 13 & X == 15) -> X s> 13 |
| 3353 | return ReplaceInstUsesWith(I, LHS); |
| 3354 | case ICmpInst::ICMP_SGT: // (X s> 13 & X s> 15) -> X s> 15 |
| 3355 | return ReplaceInstUsesWith(I, RHS); |
| 3356 | case ICmpInst::ICMP_UGT: // (X s> 13 & X u> 15) -> no change |
| 3357 | break; |
| 3358 | case ICmpInst::ICMP_NE: |
| 3359 | if (RHSCst == AddOne(LHSCst)) // (X s> 13 & X != 14) -> X s> 14 |
| 3360 | return new ICmpInst(LHSCC, LHSVal, RHSCst); |
| 3361 | break; // (X s> 13 & X != 15) -> no change |
| 3362 | case ICmpInst::ICMP_SLT: // (X s> 13 & X s< 15) ->(X-14) s< 1 |
| 3363 | return InsertRangeTest(LHSVal, AddOne(LHSCst), RHSCst, true, |
| 3364 | true, I); |
| 3365 | case ICmpInst::ICMP_ULT: // (X s> 13 & X u< 15) -> no change |
| 3366 | break; |
| 3367 | } |
| 3368 | break; |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3369 | } |
| 3370 | } |
| 3371 | } |
| 3372 | |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3373 | // fold (and (cast A), (cast B)) -> (cast (and A, B)) |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3374 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) |
| 3375 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) |
| 3376 | if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind ? |
| 3377 | const Type *SrcTy = Op0C->getOperand(0)->getType(); |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 3378 | if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() && |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3379 | // Only do this if the casts both really cause code to be generated. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3380 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), |
| 3381 | I.getType(), TD) && |
| 3382 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), |
| 3383 | I.getType(), TD)) { |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3384 | Instruction *NewOp = BinaryOperator::createAnd(Op0C->getOperand(0), |
| 3385 | Op1C->getOperand(0), |
| 3386 | I.getName()); |
| 3387 | InsertNewInstBefore(NewOp, I); |
| 3388 | return CastInst::create(Op0C->getOpcode(), NewOp, I.getType()); |
| 3389 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3390 | } |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 3391 | |
| 3392 | // (X >> Z) & (Y >> Z) -> (X&Y) >> Z for all shifts. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3393 | if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) { |
| 3394 | if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0)) |
| 3395 | if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() && |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 3396 | SI0->getOperand(1) == SI1->getOperand(1) && |
| 3397 | (SI0->hasOneUse() || SI1->hasOneUse())) { |
| 3398 | Instruction *NewOp = |
| 3399 | InsertNewInstBefore(BinaryOperator::createAnd(SI0->getOperand(0), |
| 3400 | SI1->getOperand(0), |
| 3401 | SI0->getName()), I); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3402 | return BinaryOperator::create(SI1->getOpcode(), NewOp, |
| 3403 | SI1->getOperand(1)); |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 3404 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3405 | } |
| 3406 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3407 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3408 | } |
| 3409 | |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3410 | /// CollectBSwapParts - Look to see if the specified value defines a single byte |
| 3411 | /// in the result. If it does, and if the specified byte hasn't been filled in |
| 3412 | /// yet, fill it in and return false. |
| 3413 | static bool CollectBSwapParts(Value *V, std::vector<Value*> &ByteValues) { |
| 3414 | Instruction *I = dyn_cast<Instruction>(V); |
| 3415 | if (I == 0) return true; |
| 3416 | |
| 3417 | // If this is an or instruction, it is an inner node of the bswap. |
| 3418 | if (I->getOpcode() == Instruction::Or) |
| 3419 | return CollectBSwapParts(I->getOperand(0), ByteValues) || |
| 3420 | CollectBSwapParts(I->getOperand(1), ByteValues); |
| 3421 | |
| 3422 | // If this is a shift by a constant int, and it is "24", then its operand |
| 3423 | // defines a byte. We only handle unsigned types here. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3424 | if (I->isShift() && isa<ConstantInt>(I->getOperand(1))) { |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3425 | // Not shifting the entire input by N-1 bytes? |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3426 | if (cast<ConstantInt>(I->getOperand(1))->getZExtValue() != |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3427 | 8*(ByteValues.size()-1)) |
| 3428 | return true; |
| 3429 | |
| 3430 | unsigned DestNo; |
| 3431 | if (I->getOpcode() == Instruction::Shl) { |
| 3432 | // X << 24 defines the top byte with the lowest of the input bytes. |
| 3433 | DestNo = ByteValues.size()-1; |
| 3434 | } else { |
| 3435 | // X >>u 24 defines the low byte with the highest of the input bytes. |
| 3436 | DestNo = 0; |
| 3437 | } |
| 3438 | |
| 3439 | // If the destination byte value is already defined, the values are or'd |
| 3440 | // together, which isn't a bswap (unless it's an or of the same bits). |
| 3441 | if (ByteValues[DestNo] && ByteValues[DestNo] != I->getOperand(0)) |
| 3442 | return true; |
| 3443 | ByteValues[DestNo] = I->getOperand(0); |
| 3444 | return false; |
| 3445 | } |
| 3446 | |
| 3447 | // Otherwise, we can only handle and(shift X, imm), imm). Bail out of if we |
| 3448 | // don't have this. |
| 3449 | Value *Shift = 0, *ShiftLHS = 0; |
| 3450 | ConstantInt *AndAmt = 0, *ShiftAmt = 0; |
| 3451 | if (!match(I, m_And(m_Value(Shift), m_ConstantInt(AndAmt))) || |
| 3452 | !match(Shift, m_Shift(m_Value(ShiftLHS), m_ConstantInt(ShiftAmt)))) |
| 3453 | return true; |
| 3454 | Instruction *SI = cast<Instruction>(Shift); |
| 3455 | |
| 3456 | // Make sure that the shift amount is by a multiple of 8 and isn't too big. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3457 | if (ShiftAmt->getZExtValue() & 7 || |
| 3458 | ShiftAmt->getZExtValue() > 8*ByteValues.size()) |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3459 | return true; |
| 3460 | |
| 3461 | // Turn 0xFF -> 0, 0xFF00 -> 1, 0xFF0000 -> 2, etc. |
| 3462 | unsigned DestByte; |
| 3463 | for (DestByte = 0; DestByte != ByteValues.size(); ++DestByte) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3464 | if (AndAmt->getZExtValue() == uint64_t(0xFF) << 8*DestByte) |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3465 | break; |
| 3466 | // Unknown mask for bswap. |
| 3467 | if (DestByte == ByteValues.size()) return true; |
| 3468 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3469 | unsigned ShiftBytes = ShiftAmt->getZExtValue()/8; |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3470 | unsigned SrcByte; |
| 3471 | if (SI->getOpcode() == Instruction::Shl) |
| 3472 | SrcByte = DestByte - ShiftBytes; |
| 3473 | else |
| 3474 | SrcByte = DestByte + ShiftBytes; |
| 3475 | |
| 3476 | // If the SrcByte isn't a bswapped value from the DestByte, reject it. |
| 3477 | if (SrcByte != ByteValues.size()-DestByte-1) |
| 3478 | return true; |
| 3479 | |
| 3480 | // If the destination byte value is already defined, the values are or'd |
| 3481 | // together, which isn't a bswap (unless it's an or of the same bits). |
| 3482 | if (ByteValues[DestByte] && ByteValues[DestByte] != SI->getOperand(0)) |
| 3483 | return true; |
| 3484 | ByteValues[DestByte] = SI->getOperand(0); |
| 3485 | return false; |
| 3486 | } |
| 3487 | |
| 3488 | /// MatchBSwap - Given an OR instruction, check to see if this is a bswap idiom. |
| 3489 | /// If so, insert the new bswap intrinsic and return it. |
| 3490 | Instruction *InstCombiner::MatchBSwap(BinaryOperator &I) { |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3491 | // We cannot bswap one byte. |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 3492 | if (I.getType() == Type::Int8Ty) |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3493 | return 0; |
| 3494 | |
| 3495 | /// ByteValues - For each byte of the result, we keep track of which value |
| 3496 | /// defines each byte. |
| 3497 | std::vector<Value*> ByteValues; |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 3498 | ByteValues.resize(TD->getTypeSize(I.getType())); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3499 | |
| 3500 | // Try to find all the pieces corresponding to the bswap. |
| 3501 | if (CollectBSwapParts(I.getOperand(0), ByteValues) || |
| 3502 | CollectBSwapParts(I.getOperand(1), ByteValues)) |
| 3503 | return 0; |
| 3504 | |
| 3505 | // Check to see if all of the bytes come from the same value. |
| 3506 | Value *V = ByteValues[0]; |
| 3507 | if (V == 0) return 0; // Didn't find a byte? Must be zero. |
| 3508 | |
| 3509 | // Check to make sure that all of the bytes come from the same value. |
| 3510 | for (unsigned i = 1, e = ByteValues.size(); i != e; ++i) |
| 3511 | if (ByteValues[i] != V) |
| 3512 | return 0; |
| 3513 | |
| 3514 | // If they do then *success* we can turn this into a bswap. Figure out what |
| 3515 | // bswap to make it into. |
| 3516 | Module *M = I.getParent()->getParent()->getParent(); |
Chris Lattner | ed36b2f | 2006-07-11 18:31:26 +0000 | [diff] [blame] | 3517 | const char *FnName = 0; |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 3518 | if (I.getType() == Type::Int16Ty) |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3519 | FnName = "llvm.bswap.i16"; |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 3520 | else if (I.getType() == Type::Int32Ty) |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3521 | FnName = "llvm.bswap.i32"; |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 3522 | else if (I.getType() == Type::Int64Ty) |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3523 | FnName = "llvm.bswap.i64"; |
| 3524 | else |
| 3525 | assert(0 && "Unknown integer type!"); |
Chris Lattner | 9214196 | 2007-01-07 06:58:05 +0000 | [diff] [blame] | 3526 | Constant *F = M->getOrInsertFunction(FnName, I.getType(), I.getType(), NULL); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3527 | return new CallInst(F, V); |
| 3528 | } |
| 3529 | |
| 3530 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3531 | Instruction *InstCombiner::visitOr(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 3532 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3533 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3534 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3535 | if (isa<UndefValue>(Op1)) |
| 3536 | return ReplaceInstUsesWith(I, // X | undef -> -1 |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3537 | ConstantInt::getAllOnesValue(I.getType())); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3538 | |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3539 | // or X, X = X |
| 3540 | if (Op0 == Op1) |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 3541 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3542 | |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3543 | // See if we can simplify any instructions used by the instruction whose sole |
| 3544 | // purpose is to compute bits we don't care about. |
| 3545 | uint64_t KnownZero, KnownOne; |
Chris Lattner | 98509ef | 2006-03-25 21:58:26 +0000 | [diff] [blame] | 3546 | if (!isa<PackedType>(I.getType()) && |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 3547 | SimplifyDemandedBits(&I, cast<IntegerType>(I.getType())->getBitMask(), |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3548 | KnownZero, KnownOne)) |
| 3549 | return &I; |
| 3550 | |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3551 | // or X, -1 == -1 |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3552 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 3553 | ConstantInt *C1 = 0; Value *X = 0; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3554 | // (X & C1) | C2 --> (X | C2) & (C1|C2) |
| 3555 | if (match(Op0, m_And(m_Value(X), m_ConstantInt(C1))) && isOnlyUse(Op0)) { |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3556 | Instruction *Or = BinaryOperator::createOr(X, RHS); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3557 | InsertNewInstBefore(Or, I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3558 | Or->takeName(Op0); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3559 | return BinaryOperator::createAnd(Or, ConstantExpr::getOr(RHS, C1)); |
| 3560 | } |
Chris Lattner | ad44ebf | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 3561 | |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3562 | // (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2) |
| 3563 | if (match(Op0, m_Xor(m_Value(X), m_ConstantInt(C1))) && isOnlyUse(Op0)) { |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3564 | Instruction *Or = BinaryOperator::createOr(X, RHS); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3565 | InsertNewInstBefore(Or, I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3566 | Or->takeName(Op0); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3567 | return BinaryOperator::createXor(Or, |
| 3568 | ConstantExpr::getAnd(C1, ConstantExpr::getNot(RHS))); |
Chris Lattner | ad44ebf | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 3569 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3570 | |
| 3571 | // Try to fold constant and into select arguments. |
| 3572 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3573 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3574 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 3575 | if (isa<PHINode>(Op0)) |
| 3576 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 3577 | return NV; |
Chris Lattner | ad44ebf | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 3578 | } |
| 3579 | |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 3580 | Value *A = 0, *B = 0; |
| 3581 | ConstantInt *C1 = 0, *C2 = 0; |
Chris Lattner | f4d4c87 | 2005-05-07 23:49:08 +0000 | [diff] [blame] | 3582 | |
| 3583 | if (match(Op0, m_And(m_Value(A), m_Value(B)))) |
| 3584 | if (A == Op1 || B == Op1) // (A & ?) | A --> A |
| 3585 | return ReplaceInstUsesWith(I, Op1); |
| 3586 | if (match(Op1, m_And(m_Value(A), m_Value(B)))) |
| 3587 | if (A == Op0 || B == Op0) // A | (A & ?) --> A |
| 3588 | return ReplaceInstUsesWith(I, Op0); |
| 3589 | |
Chris Lattner | 6423d4c | 2006-07-10 20:25:24 +0000 | [diff] [blame] | 3590 | // (A | B) | C and A | (B | C) -> bswap if possible. |
| 3591 | // (A >> B) | (C << D) and (A << B) | (B >> C) -> bswap if possible. |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3592 | if (match(Op0, m_Or(m_Value(), m_Value())) || |
Chris Lattner | 6423d4c | 2006-07-10 20:25:24 +0000 | [diff] [blame] | 3593 | match(Op1, m_Or(m_Value(), m_Value())) || |
| 3594 | (match(Op0, m_Shift(m_Value(), m_Value())) && |
| 3595 | match(Op1, m_Shift(m_Value(), m_Value())))) { |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3596 | if (Instruction *BSwap = MatchBSwap(I)) |
| 3597 | return BSwap; |
| 3598 | } |
| 3599 | |
Chris Lattner | 6e4c649 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 3600 | // (X^C)|Y -> (X|Y)^C iff Y&C == 0 |
| 3601 | if (Op0->hasOneUse() && match(Op0, m_Xor(m_Value(A), m_ConstantInt(C1))) && |
Chris Lattner | 3bedbd9 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3602 | MaskedValueIsZero(Op1, C1->getZExtValue())) { |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3603 | Instruction *NOr = BinaryOperator::createOr(A, Op1); |
| 3604 | InsertNewInstBefore(NOr, I); |
| 3605 | NOr->takeName(Op0); |
| 3606 | return BinaryOperator::createXor(NOr, C1); |
Chris Lattner | 6e4c649 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 3607 | } |
| 3608 | |
| 3609 | // Y|(X^C) -> (X|Y)^C iff Y&C == 0 |
| 3610 | if (Op1->hasOneUse() && match(Op1, m_Xor(m_Value(A), m_ConstantInt(C1))) && |
Chris Lattner | 3bedbd9 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3611 | MaskedValueIsZero(Op0, C1->getZExtValue())) { |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3612 | Instruction *NOr = BinaryOperator::createOr(A, Op0); |
| 3613 | InsertNewInstBefore(NOr, I); |
| 3614 | NOr->takeName(Op0); |
| 3615 | return BinaryOperator::createXor(NOr, C1); |
Chris Lattner | 6e4c649 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 3616 | } |
| 3617 | |
Chris Lattner | e9bed7d | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 3618 | // (A & C1)|(B & C2) |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3619 | if (match(Op0, m_And(m_Value(A), m_ConstantInt(C1))) && |
Chris Lattner | e9bed7d | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 3620 | match(Op1, m_And(m_Value(B), m_ConstantInt(C2)))) { |
| 3621 | |
| 3622 | if (A == B) // (A & C1)|(A & C2) == A & (C1|C2) |
| 3623 | return BinaryOperator::createAnd(A, ConstantExpr::getOr(C1, C2)); |
| 3624 | |
| 3625 | |
Chris Lattner | 0b7c0bf | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 3626 | // If we have: ((V + N) & C1) | (V & C2) |
| 3627 | // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0 |
| 3628 | // replace with V+N. |
| 3629 | if (C1 == ConstantExpr::getNot(C2)) { |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 3630 | Value *V1 = 0, *V2 = 0; |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3631 | if ((C2->getZExtValue() & (C2->getZExtValue()+1)) == 0 && // C2 == 0+1+ |
Chris Lattner | 0b7c0bf | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 3632 | match(A, m_Add(m_Value(V1), m_Value(V2)))) { |
| 3633 | // Add commutes, try both ways. |
Chris Lattner | 3bedbd9 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3634 | if (V1 == B && MaskedValueIsZero(V2, C2->getZExtValue())) |
Chris Lattner | 0b7c0bf | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 3635 | return ReplaceInstUsesWith(I, A); |
Chris Lattner | 3bedbd9 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3636 | if (V2 == B && MaskedValueIsZero(V1, C2->getZExtValue())) |
Chris Lattner | 0b7c0bf | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 3637 | return ReplaceInstUsesWith(I, A); |
| 3638 | } |
| 3639 | // Or commutes, try both ways. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3640 | if ((C1->getZExtValue() & (C1->getZExtValue()+1)) == 0 && |
Chris Lattner | 0b7c0bf | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 3641 | match(B, m_Add(m_Value(V1), m_Value(V2)))) { |
| 3642 | // Add commutes, try both ways. |
Chris Lattner | 3bedbd9 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3643 | if (V1 == A && MaskedValueIsZero(V2, C1->getZExtValue())) |
Chris Lattner | 0b7c0bf | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 3644 | return ReplaceInstUsesWith(I, B); |
Chris Lattner | 3bedbd9 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3645 | if (V2 == A && MaskedValueIsZero(V1, C1->getZExtValue())) |
Chris Lattner | 0b7c0bf | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 3646 | return ReplaceInstUsesWith(I, B); |
Chris Lattner | e9bed7d | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 3647 | } |
| 3648 | } |
| 3649 | } |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 3650 | |
| 3651 | // (X >> Z) | (Y >> Z) -> (X|Y) >> Z for all shifts. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3652 | if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) { |
| 3653 | if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0)) |
| 3654 | if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() && |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 3655 | SI0->getOperand(1) == SI1->getOperand(1) && |
| 3656 | (SI0->hasOneUse() || SI1->hasOneUse())) { |
| 3657 | Instruction *NewOp = |
| 3658 | InsertNewInstBefore(BinaryOperator::createOr(SI0->getOperand(0), |
| 3659 | SI1->getOperand(0), |
| 3660 | SI0->getName()), I); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3661 | return BinaryOperator::create(SI1->getOpcode(), NewOp, |
| 3662 | SI1->getOperand(1)); |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 3663 | } |
| 3664 | } |
Chris Lattner | 67ca768 | 2003-08-12 19:11:07 +0000 | [diff] [blame] | 3665 | |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3666 | if (match(Op0, m_Not(m_Value(A)))) { // ~A | Op1 |
| 3667 | if (A == Op1) // ~A | A == -1 |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3668 | return ReplaceInstUsesWith(I, |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3669 | ConstantInt::getAllOnesValue(I.getType())); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3670 | } else { |
| 3671 | A = 0; |
| 3672 | } |
Chris Lattner | f4d4c87 | 2005-05-07 23:49:08 +0000 | [diff] [blame] | 3673 | // Note, A is still live here! |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3674 | if (match(Op1, m_Not(m_Value(B)))) { // Op0 | ~B |
| 3675 | if (Op0 == B) |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3676 | return ReplaceInstUsesWith(I, |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3677 | ConstantInt::getAllOnesValue(I.getType())); |
Chris Lattner | a27231a | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 3678 | |
Misha Brukman | cb6267b | 2004-07-30 12:50:08 +0000 | [diff] [blame] | 3679 | // (~A | ~B) == (~(A & B)) - De Morgan's Law |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3680 | if (A && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
| 3681 | Value *And = InsertNewInstBefore(BinaryOperator::createAnd(A, B, |
| 3682 | I.getName()+".demorgan"), I); |
| 3683 | return BinaryOperator::createNot(And); |
| 3684 | } |
Chris Lattner | a27231a | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 3685 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3686 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3687 | // (icmp1 A, B) | (icmp2 A, B) --> (icmp3 A, B) |
| 3688 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1))) { |
| 3689 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS))) |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3690 | return R; |
| 3691 | |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3692 | Value *LHSVal, *RHSVal; |
| 3693 | ConstantInt *LHSCst, *RHSCst; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3694 | ICmpInst::Predicate LHSCC, RHSCC; |
| 3695 | if (match(Op0, m_ICmp(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst)))) |
| 3696 | if (match(RHS, m_ICmp(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst)))) |
| 3697 | if (LHSVal == RHSVal && // Found (X icmp C1) | (X icmp C2) |
| 3698 | // icmp [us][gl]e x, cst is folded to icmp [us][gl]t elsewhere. |
| 3699 | LHSCC != ICmpInst::ICMP_UGE && LHSCC != ICmpInst::ICMP_ULE && |
| 3700 | RHSCC != ICmpInst::ICMP_UGE && RHSCC != ICmpInst::ICMP_ULE && |
| 3701 | LHSCC != ICmpInst::ICMP_SGE && LHSCC != ICmpInst::ICMP_SLE && |
| 3702 | RHSCC != ICmpInst::ICMP_SGE && RHSCC != ICmpInst::ICMP_SLE) { |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3703 | // Ensure that the larger constant is on the RHS. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3704 | ICmpInst::Predicate GT = ICmpInst::isSignedPredicate(LHSCC) ? |
| 3705 | ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; |
| 3706 | Constant *Cmp = ConstantExpr::getICmp(GT, LHSCst, RHSCst); |
| 3707 | ICmpInst *LHS = cast<ICmpInst>(Op0); |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 3708 | if (cast<ConstantInt>(Cmp)->getZExtValue()) { |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3709 | std::swap(LHS, RHS); |
| 3710 | std::swap(LHSCst, RHSCst); |
| 3711 | std::swap(LHSCC, RHSCC); |
| 3712 | } |
| 3713 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3714 | // At this point, we know we have have two icmp instructions |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3715 | // comparing a value against two constants and or'ing the result |
| 3716 | // together. Because of the above check, we know that we only have |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3717 | // ICMP_EQ, ICMP_NE, ICMP_LT, and ICMP_GT here. We also know (from the |
| 3718 | // FoldICmpLogical check above), that the two constants are not |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3719 | // equal. |
| 3720 | assert(LHSCst != RHSCst && "Compares not folded above?"); |
| 3721 | |
| 3722 | switch (LHSCC) { |
| 3723 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3724 | case ICmpInst::ICMP_EQ: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3725 | switch (RHSCC) { |
| 3726 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3727 | case ICmpInst::ICMP_EQ: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3728 | if (LHSCst == SubOne(RHSCst)) {// (X == 13 | X == 14) -> X-13 <u 2 |
| 3729 | Constant *AddCST = ConstantExpr::getNeg(LHSCst); |
| 3730 | Instruction *Add = BinaryOperator::createAdd(LHSVal, AddCST, |
| 3731 | LHSVal->getName()+".off"); |
| 3732 | InsertNewInstBefore(Add, I); |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3733 | AddCST = ConstantExpr::getSub(AddOne(RHSCst), LHSCst); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3734 | return new ICmpInst(ICmpInst::ICMP_ULT, Add, AddCST); |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3735 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3736 | break; // (X == 13 | X == 15) -> no change |
| 3737 | case ICmpInst::ICMP_UGT: // (X == 13 | X u> 14) -> no change |
| 3738 | case ICmpInst::ICMP_SGT: // (X == 13 | X s> 14) -> no change |
Chris Lattner | 240d6f4 | 2005-04-19 06:04:18 +0000 | [diff] [blame] | 3739 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3740 | case ICmpInst::ICMP_NE: // (X == 13 | X != 15) -> X != 15 |
| 3741 | case ICmpInst::ICMP_ULT: // (X == 13 | X u< 15) -> X u< 15 |
| 3742 | case ICmpInst::ICMP_SLT: // (X == 13 | X s< 15) -> X s< 15 |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3743 | return ReplaceInstUsesWith(I, RHS); |
| 3744 | } |
| 3745 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3746 | case ICmpInst::ICMP_NE: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3747 | switch (RHSCC) { |
| 3748 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3749 | case ICmpInst::ICMP_EQ: // (X != 13 | X == 15) -> X != 13 |
| 3750 | case ICmpInst::ICMP_UGT: // (X != 13 | X u> 15) -> X != 13 |
| 3751 | case ICmpInst::ICMP_SGT: // (X != 13 | X s> 15) -> X != 13 |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3752 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3753 | case ICmpInst::ICMP_NE: // (X != 13 | X != 15) -> true |
| 3754 | case ICmpInst::ICMP_ULT: // (X != 13 | X u< 15) -> true |
| 3755 | case ICmpInst::ICMP_SLT: // (X != 13 | X s< 15) -> true |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3756 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3757 | } |
| 3758 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3759 | case ICmpInst::ICMP_ULT: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3760 | switch (RHSCC) { |
| 3761 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3762 | case ICmpInst::ICMP_EQ: // (X u< 13 | X == 14) -> no change |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3763 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3764 | case ICmpInst::ICMP_UGT: // (X u< 13 | X u> 15) ->(X-13) u> 2 |
| 3765 | return InsertRangeTest(LHSVal, LHSCst, AddOne(RHSCst), false, |
| 3766 | false, I); |
| 3767 | case ICmpInst::ICMP_SGT: // (X u< 13 | X s> 15) -> no change |
| 3768 | break; |
| 3769 | case ICmpInst::ICMP_NE: // (X u< 13 | X != 15) -> X != 15 |
| 3770 | case ICmpInst::ICMP_ULT: // (X u< 13 | X u< 15) -> X u< 15 |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3771 | return ReplaceInstUsesWith(I, RHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3772 | case ICmpInst::ICMP_SLT: // (X u< 13 | X s< 15) -> no change |
| 3773 | break; |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3774 | } |
| 3775 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3776 | case ICmpInst::ICMP_SLT: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3777 | switch (RHSCC) { |
| 3778 | default: assert(0 && "Unknown integer condition code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3779 | case ICmpInst::ICMP_EQ: // (X s< 13 | X == 14) -> no change |
| 3780 | break; |
| 3781 | case ICmpInst::ICMP_SGT: // (X s< 13 | X s> 15) ->(X-13) s> 2 |
| 3782 | return InsertRangeTest(LHSVal, LHSCst, AddOne(RHSCst), true, |
| 3783 | false, I); |
| 3784 | case ICmpInst::ICMP_UGT: // (X s< 13 | X u> 15) -> no change |
| 3785 | break; |
| 3786 | case ICmpInst::ICMP_NE: // (X s< 13 | X != 15) -> X != 15 |
| 3787 | case ICmpInst::ICMP_SLT: // (X s< 13 | X s< 15) -> X s< 15 |
| 3788 | return ReplaceInstUsesWith(I, RHS); |
| 3789 | case ICmpInst::ICMP_ULT: // (X s< 13 | X u< 15) -> no change |
| 3790 | break; |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3791 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3792 | break; |
| 3793 | case ICmpInst::ICMP_UGT: |
| 3794 | switch (RHSCC) { |
| 3795 | default: assert(0 && "Unknown integer condition code!"); |
| 3796 | case ICmpInst::ICMP_EQ: // (X u> 13 | X == 15) -> X u> 13 |
| 3797 | case ICmpInst::ICMP_UGT: // (X u> 13 | X u> 15) -> X u> 13 |
| 3798 | return ReplaceInstUsesWith(I, LHS); |
| 3799 | case ICmpInst::ICMP_SGT: // (X u> 13 | X s> 15) -> no change |
| 3800 | break; |
| 3801 | case ICmpInst::ICMP_NE: // (X u> 13 | X != 15) -> true |
| 3802 | case ICmpInst::ICMP_ULT: // (X u> 13 | X u< 15) -> true |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3803 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3804 | case ICmpInst::ICMP_SLT: // (X u> 13 | X s< 15) -> no change |
| 3805 | break; |
| 3806 | } |
| 3807 | break; |
| 3808 | case ICmpInst::ICMP_SGT: |
| 3809 | switch (RHSCC) { |
| 3810 | default: assert(0 && "Unknown integer condition code!"); |
| 3811 | case ICmpInst::ICMP_EQ: // (X s> 13 | X == 15) -> X > 13 |
| 3812 | case ICmpInst::ICMP_SGT: // (X s> 13 | X s> 15) -> X > 13 |
| 3813 | return ReplaceInstUsesWith(I, LHS); |
| 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) -> true |
| 3817 | case ICmpInst::ICMP_SLT: // (X s> 13 | X s< 15) -> true |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3818 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3819 | case ICmpInst::ICMP_ULT: // (X s> 13 | X u< 15) -> no change |
| 3820 | break; |
| 3821 | } |
| 3822 | break; |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3823 | } |
| 3824 | } |
| 3825 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3826 | |
| 3827 | // fold (or (cast A), (cast B)) -> (cast (or A, B)) |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3828 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3829 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3830 | if (Op0C->getOpcode() == Op1C->getOpcode()) {// same cast kind ? |
| 3831 | const Type *SrcTy = Op0C->getOperand(0)->getType(); |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 3832 | if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() && |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3833 | // Only do this if the casts both really cause code to be generated. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3834 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), |
| 3835 | I.getType(), TD) && |
| 3836 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), |
| 3837 | I.getType(), TD)) { |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3838 | Instruction *NewOp = BinaryOperator::createOr(Op0C->getOperand(0), |
| 3839 | Op1C->getOperand(0), |
| 3840 | I.getName()); |
| 3841 | InsertNewInstBefore(NewOp, I); |
| 3842 | return CastInst::create(Op0C->getOpcode(), NewOp, I.getType()); |
| 3843 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3844 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3845 | |
Chris Lattner | e9bed7d | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 3846 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3847 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3848 | } |
| 3849 | |
Chris Lattner | c317d39 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 3850 | // XorSelf - Implements: X ^ X --> 0 |
| 3851 | struct XorSelf { |
| 3852 | Value *RHS; |
| 3853 | XorSelf(Value *rhs) : RHS(rhs) {} |
| 3854 | bool shouldApply(Value *LHS) const { return LHS == RHS; } |
| 3855 | Instruction *apply(BinaryOperator &Xor) const { |
| 3856 | return &Xor; |
| 3857 | } |
| 3858 | }; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3859 | |
| 3860 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3861 | Instruction *InstCombiner::visitXor(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 3862 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3863 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3864 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3865 | if (isa<UndefValue>(Op1)) |
| 3866 | return ReplaceInstUsesWith(I, Op1); // X ^ undef -> undef |
| 3867 | |
Chris Lattner | c317d39 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 3868 | // xor X, X = 0, even if X is nested in a sequence of Xor's. |
| 3869 | if (Instruction *Result = AssociativeOpt(I, XorSelf(Op1))) { |
| 3870 | assert(Result == &I && "AssociativeOpt didn't work?"); |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 3871 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | c317d39 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 3872 | } |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3873 | |
| 3874 | // See if we can simplify any instructions used by the instruction whose sole |
| 3875 | // purpose is to compute bits we don't care about. |
| 3876 | uint64_t KnownZero, KnownOne; |
Chris Lattner | 98509ef | 2006-03-25 21:58:26 +0000 | [diff] [blame] | 3877 | if (!isa<PackedType>(I.getType()) && |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 3878 | SimplifyDemandedBits(&I, cast<IntegerType>(I.getType())->getBitMask(), |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3879 | KnownZero, KnownOne)) |
| 3880 | return &I; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3881 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3882 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3883 | // xor (icmp A, B), true = not (icmp A, B) = !icmp A, B |
| 3884 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(Op0)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3885 | if (RHS == ConstantInt::getTrue() && ICI->hasOneUse()) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3886 | return new ICmpInst(ICI->getInversePredicate(), |
| 3887 | ICI->getOperand(0), ICI->getOperand(1)); |
Chris Lattner | ad5b4fb | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 3888 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3889 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) { |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 3890 | // ~(c-X) == X-c-1 == X+(-c-1) |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3891 | if (Op0I->getOpcode() == Instruction::Sub && RHS->isAllOnesValue()) |
| 3892 | if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3893 | Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C); |
| 3894 | Constant *ConstantRHS = ConstantExpr::getSub(NegOp0I0C, |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3895 | ConstantInt::get(I.getType(), 1)); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3896 | return BinaryOperator::createAdd(Op0I->getOperand(1), ConstantRHS); |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3897 | } |
Chris Lattner | 5b62aa7 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 3898 | |
| 3899 | // ~(~X & Y) --> (X | ~Y) |
| 3900 | if (Op0I->getOpcode() == Instruction::And && RHS->isAllOnesValue()) { |
| 3901 | if (dyn_castNotVal(Op0I->getOperand(1))) Op0I->swapOperands(); |
| 3902 | if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0))) { |
| 3903 | Instruction *NotY = |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3904 | BinaryOperator::createNot(Op0I->getOperand(1), |
Chris Lattner | 5b62aa7 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 3905 | Op0I->getOperand(1)->getName()+".not"); |
| 3906 | InsertNewInstBefore(NotY, I); |
| 3907 | return BinaryOperator::createOr(Op0NotVal, NotY); |
| 3908 | } |
| 3909 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3910 | |
Chris Lattner | eca0c5c | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 3911 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3912 | if (Op0I->getOpcode() == Instruction::Add) { |
Chris Lattner | 689d24b | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 3913 | // ~(X-c) --> (-c-1)-X |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3914 | if (RHS->isAllOnesValue()) { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3915 | Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI); |
| 3916 | return BinaryOperator::createSub( |
| 3917 | ConstantExpr::getSub(NegOp0CI, |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3918 | ConstantInt::get(I.getType(), 1)), |
Chris Lattner | 689d24b | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 3919 | Op0I->getOperand(0)); |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3920 | } |
Chris Lattner | 02bd1b3 | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 3921 | } else if (Op0I->getOpcode() == Instruction::Or) { |
| 3922 | // (X|C1)^C2 -> X^(C1|C2) iff X&~C1 == 0 |
| 3923 | if (MaskedValueIsZero(Op0I->getOperand(0), Op0CI->getZExtValue())) { |
| 3924 | Constant *NewRHS = ConstantExpr::getOr(Op0CI, RHS); |
| 3925 | // Anything in both C1 and C2 is known to be zero, remove it from |
| 3926 | // NewRHS. |
| 3927 | Constant *CommonBits = ConstantExpr::getAnd(Op0CI, RHS); |
| 3928 | NewRHS = ConstantExpr::getAnd(NewRHS, |
| 3929 | ConstantExpr::getNot(CommonBits)); |
| 3930 | WorkList.push_back(Op0I); |
| 3931 | I.setOperand(0, Op0I->getOperand(0)); |
| 3932 | I.setOperand(1, NewRHS); |
| 3933 | return &I; |
| 3934 | } |
Chris Lattner | eca0c5c | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 3935 | } |
Chris Lattner | 05bd1b2 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 3936 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3937 | |
| 3938 | // Try to fold constant and into select arguments. |
| 3939 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3940 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3941 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 3942 | if (isa<PHINode>(Op0)) |
| 3943 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 3944 | return NV; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3945 | } |
| 3946 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 3947 | if (Value *X = dyn_castNotVal(Op0)) // ~A ^ A == -1 |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3948 | if (X == Op1) |
| 3949 | return ReplaceInstUsesWith(I, |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3950 | ConstantInt::getAllOnesValue(I.getType())); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3951 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 3952 | if (Value *X = dyn_castNotVal(Op1)) // A ^ ~A == -1 |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3953 | if (X == Op0) |
| 3954 | return ReplaceInstUsesWith(I, |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3955 | ConstantInt::getAllOnesValue(I.getType())); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3956 | |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 3957 | if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) |
Chris Lattner | 26ca7e1 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 3958 | if (Op1I->getOpcode() == Instruction::Or) { |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 3959 | if (Op1I->getOperand(0) == Op0) { // B^(B|A) == (A|B)^B |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 3960 | Op1I->swapOperands(); |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 3961 | I.swapOperands(); |
| 3962 | std::swap(Op0, Op1); |
| 3963 | } else if (Op1I->getOperand(1) == Op0) { // B^(A|B) == (A|B)^B |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 3964 | I.swapOperands(); // Simplified below. |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 3965 | std::swap(Op0, Op1); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3966 | } |
Chris Lattner | 26ca7e1 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 3967 | } else if (Op1I->getOpcode() == Instruction::Xor) { |
| 3968 | if (Op0 == Op1I->getOperand(0)) // A^(A^B) == B |
| 3969 | return ReplaceInstUsesWith(I, Op1I->getOperand(1)); |
| 3970 | else if (Op0 == Op1I->getOperand(1)) // A^(B^A) == B |
| 3971 | return ReplaceInstUsesWith(I, Op1I->getOperand(0)); |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 3972 | } else if (Op1I->getOpcode() == Instruction::And && Op1I->hasOneUse()) { |
| 3973 | if (Op1I->getOperand(0) == Op0) // A^(A&B) -> A^(B&A) |
| 3974 | Op1I->swapOperands(); |
| 3975 | if (Op0 == Op1I->getOperand(1)) { // A^(B&A) -> (B&A)^A |
| 3976 | I.swapOperands(); // Simplified below. |
| 3977 | std::swap(Op0, Op1); |
| 3978 | } |
Chris Lattner | 26ca7e1 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 3979 | } |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 3980 | |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 3981 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 3982 | if (Op0I->getOpcode() == Instruction::Or && Op0I->hasOneUse()) { |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 3983 | if (Op0I->getOperand(0) == Op1) // (B|A)^B == (A|B)^B |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 3984 | Op0I->swapOperands(); |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 3985 | if (Op0I->getOperand(1) == Op1) { // (A|B)^B == A & ~B |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 3986 | Instruction *NotB = BinaryOperator::createNot(Op1, "tmp"); |
| 3987 | InsertNewInstBefore(NotB, I); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3988 | return BinaryOperator::createAnd(Op0I->getOperand(0), NotB); |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 3989 | } |
Chris Lattner | 26ca7e1 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 3990 | } else if (Op0I->getOpcode() == Instruction::Xor) { |
| 3991 | if (Op1 == Op0I->getOperand(0)) // (A^B)^A == B |
| 3992 | return ReplaceInstUsesWith(I, Op0I->getOperand(1)); |
| 3993 | else if (Op1 == Op0I->getOperand(1)) // (B^A)^A == B |
| 3994 | return ReplaceInstUsesWith(I, Op0I->getOperand(0)); |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 3995 | } else if (Op0I->getOpcode() == Instruction::And && Op0I->hasOneUse()) { |
| 3996 | if (Op0I->getOperand(0) == Op1) // (A&B)^A -> (B&A)^A |
| 3997 | Op0I->swapOperands(); |
Chris Lattner | ae1ab39 | 2006-04-01 22:05:01 +0000 | [diff] [blame] | 3998 | if (Op0I->getOperand(1) == Op1 && // (B&A)^A == ~B & A |
| 3999 | !isa<ConstantInt>(Op1)) { // Canonical form is (B&C)^C |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4000 | Instruction *N = BinaryOperator::createNot(Op0I->getOperand(0), "tmp"); |
| 4001 | InsertNewInstBefore(N, I); |
| 4002 | return BinaryOperator::createAnd(N, Op1); |
| 4003 | } |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4004 | } |
| 4005 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4006 | // (icmp1 A, B) ^ (icmp2 A, B) --> (icmp3 A, B) |
| 4007 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1))) |
| 4008 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS))) |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 4009 | return R; |
| 4010 | |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4011 | // fold (xor (cast A), (cast B)) -> (cast (xor A, B)) |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4012 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4013 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4014 | if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind? |
| 4015 | const Type *SrcTy = Op0C->getOperand(0)->getType(); |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 4016 | if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() && |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4017 | // Only do this if the casts both really cause code to be generated. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4018 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), |
| 4019 | I.getType(), TD) && |
| 4020 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), |
| 4021 | I.getType(), TD)) { |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4022 | Instruction *NewOp = BinaryOperator::createXor(Op0C->getOperand(0), |
| 4023 | Op1C->getOperand(0), |
| 4024 | I.getName()); |
| 4025 | InsertNewInstBefore(NewOp, I); |
| 4026 | return CastInst::create(Op0C->getOpcode(), NewOp, I.getType()); |
| 4027 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4028 | } |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4029 | |
| 4030 | // (X >> Z) ^ (Y >> Z) -> (X^Y) >> Z for all shifts. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4031 | if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) { |
| 4032 | if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0)) |
| 4033 | if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() && |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4034 | SI0->getOperand(1) == SI1->getOperand(1) && |
| 4035 | (SI0->hasOneUse() || SI1->hasOneUse())) { |
| 4036 | Instruction *NewOp = |
| 4037 | InsertNewInstBefore(BinaryOperator::createXor(SI0->getOperand(0), |
| 4038 | SI1->getOperand(0), |
| 4039 | SI0->getName()), I); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4040 | return BinaryOperator::create(SI1->getOpcode(), NewOp, |
| 4041 | SI1->getOperand(1)); |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4042 | } |
| 4043 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4044 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4045 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4046 | } |
| 4047 | |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4048 | static bool isPositive(ConstantInt *C) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4049 | return C->getSExtValue() >= 0; |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4050 | } |
| 4051 | |
| 4052 | /// AddWithOverflow - Compute Result = In1+In2, returning true if the result |
| 4053 | /// overflowed for this type. |
| 4054 | static bool AddWithOverflow(ConstantInt *&Result, ConstantInt *In1, |
| 4055 | ConstantInt *In2) { |
| 4056 | Result = cast<ConstantInt>(ConstantExpr::getAdd(In1, In2)); |
| 4057 | |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 4058 | return cast<ConstantInt>(Result)->getZExtValue() < |
| 4059 | cast<ConstantInt>(In1)->getZExtValue(); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4060 | } |
| 4061 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4062 | /// EmitGEPOffset - Given a getelementptr instruction/constantexpr, emit the |
| 4063 | /// code necessary to compute the offset from the base pointer (without adding |
| 4064 | /// in the base pointer). Return the result as a signed integer of intptr size. |
| 4065 | static Value *EmitGEPOffset(User *GEP, Instruction &I, InstCombiner &IC) { |
| 4066 | TargetData &TD = IC.getTargetData(); |
| 4067 | gep_type_iterator GTI = gep_type_begin(GEP); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4068 | const Type *IntPtrTy = TD.getIntPtrType(); |
| 4069 | Value *Result = Constant::getNullValue(IntPtrTy); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4070 | |
| 4071 | // Build a mask for high order bits. |
Chris Lattner | 1a074fc | 2006-02-07 07:00:41 +0000 | [diff] [blame] | 4072 | uint64_t PtrSizeMask = ~0ULL >> (64-TD.getPointerSize()*8); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4073 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4074 | for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i, ++GTI) { |
| 4075 | Value *Op = GEP->getOperand(i); |
Chris Lattner | 0b84c80 | 2005-01-13 23:26:48 +0000 | [diff] [blame] | 4076 | uint64_t Size = TD.getTypeSize(GTI.getIndexedType()) & PtrSizeMask; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4077 | Constant *Scale = ConstantInt::get(IntPtrTy, Size); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4078 | if (Constant *OpC = dyn_cast<Constant>(Op)) { |
| 4079 | if (!OpC->isNullValue()) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4080 | OpC = ConstantExpr::getIntegerCast(OpC, IntPtrTy, true /*SExt*/); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4081 | Scale = ConstantExpr::getMul(OpC, Scale); |
| 4082 | if (Constant *RC = dyn_cast<Constant>(Result)) |
| 4083 | Result = ConstantExpr::getAdd(RC, Scale); |
| 4084 | else { |
| 4085 | // Emit an add instruction. |
| 4086 | Result = IC.InsertNewInstBefore( |
| 4087 | BinaryOperator::createAdd(Result, Scale, |
| 4088 | GEP->getName()+".offs"), I); |
| 4089 | } |
| 4090 | } |
| 4091 | } else { |
Chris Lattner | 6f7f02f | 2005-01-14 17:17:59 +0000 | [diff] [blame] | 4092 | // Convert to correct type. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4093 | Op = IC.InsertNewInstBefore(CastInst::createSExtOrBitCast(Op, IntPtrTy, |
Chris Lattner | 6f7f02f | 2005-01-14 17:17:59 +0000 | [diff] [blame] | 4094 | Op->getName()+".c"), I); |
| 4095 | if (Size != 1) |
Chris Lattner | 5bdf04c | 2005-01-13 20:40:58 +0000 | [diff] [blame] | 4096 | // We'll let instcombine(mul) convert this to a shl if possible. |
| 4097 | Op = IC.InsertNewInstBefore(BinaryOperator::createMul(Op, Scale, |
| 4098 | GEP->getName()+".idx"), I); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4099 | |
| 4100 | // Emit an add instruction. |
Chris Lattner | 5bdf04c | 2005-01-13 20:40:58 +0000 | [diff] [blame] | 4101 | Result = IC.InsertNewInstBefore(BinaryOperator::createAdd(Op, Result, |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4102 | GEP->getName()+".offs"), I); |
| 4103 | } |
| 4104 | } |
| 4105 | return Result; |
| 4106 | } |
| 4107 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4108 | /// FoldGEPICmp - Fold comparisons between a GEP instruction and something |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4109 | /// else. At this point we know that the GEP is on the LHS of the comparison. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4110 | Instruction *InstCombiner::FoldGEPICmp(User *GEPLHS, Value *RHS, |
| 4111 | ICmpInst::Predicate Cond, |
| 4112 | Instruction &I) { |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4113 | assert(dyn_castGetElementPtr(GEPLHS) && "LHS is not a getelementptr!"); |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4114 | |
| 4115 | if (CastInst *CI = dyn_cast<CastInst>(RHS)) |
| 4116 | if (isa<PointerType>(CI->getOperand(0)->getType())) |
| 4117 | RHS = CI->getOperand(0); |
| 4118 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4119 | Value *PtrBase = GEPLHS->getOperand(0); |
| 4120 | if (PtrBase == RHS) { |
| 4121 | // As an optimization, we don't actually have to compute the actual value of |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4122 | // OFFSET if this is a icmp_eq or icmp_ne comparison, just return whether |
| 4123 | // each index is zero or not. |
| 4124 | if (Cond == ICmpInst::ICMP_EQ || Cond == ICmpInst::ICMP_NE) { |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4125 | Instruction *InVal = 0; |
Chris Lattner | ad5fec1 | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 4126 | gep_type_iterator GTI = gep_type_begin(GEPLHS); |
| 4127 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i, ++GTI) { |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4128 | bool EmitIt = true; |
| 4129 | if (Constant *C = dyn_cast<Constant>(GEPLHS->getOperand(i))) { |
| 4130 | if (isa<UndefValue>(C)) // undef index -> undef. |
| 4131 | return ReplaceInstUsesWith(I, UndefValue::get(I.getType())); |
| 4132 | if (C->isNullValue()) |
| 4133 | EmitIt = false; |
Chris Lattner | ad5fec1 | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 4134 | else if (TD->getTypeSize(GTI.getIndexedType()) == 0) { |
| 4135 | EmitIt = false; // This is indexing into a zero sized array? |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4136 | } else if (isa<ConstantInt>(C)) |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4137 | return ReplaceInstUsesWith(I, // No comparison is needed here. |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4138 | ConstantInt::get(Type::Int1Ty, |
| 4139 | Cond == ICmpInst::ICMP_NE)); |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4140 | } |
| 4141 | |
| 4142 | if (EmitIt) { |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4143 | Instruction *Comp = |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4144 | new ICmpInst(Cond, GEPLHS->getOperand(i), |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4145 | Constant::getNullValue(GEPLHS->getOperand(i)->getType())); |
| 4146 | if (InVal == 0) |
| 4147 | InVal = Comp; |
| 4148 | else { |
| 4149 | InVal = InsertNewInstBefore(InVal, I); |
| 4150 | InsertNewInstBefore(Comp, I); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4151 | if (Cond == ICmpInst::ICMP_NE) // True if any are unequal |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4152 | InVal = BinaryOperator::createOr(InVal, Comp); |
| 4153 | else // True if all are equal |
| 4154 | InVal = BinaryOperator::createAnd(InVal, Comp); |
| 4155 | } |
| 4156 | } |
| 4157 | } |
| 4158 | |
| 4159 | if (InVal) |
| 4160 | return InVal; |
| 4161 | else |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4162 | // No comparison is needed here, all indexes = 0 |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4163 | ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
| 4164 | Cond == ICmpInst::ICMP_EQ)); |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4165 | } |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4166 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4167 | // Only lower this if the icmp is the only user of the GEP or if we expect |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4168 | // the result to fold to a constant! |
| 4169 | if (isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) { |
| 4170 | // ((gep Ptr, OFFSET) cmp Ptr) ---> (OFFSET cmp 0). |
| 4171 | Value *Offset = EmitGEPOffset(GEPLHS, I, *this); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4172 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), Offset, |
| 4173 | Constant::getNullValue(Offset->getType())); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4174 | } |
| 4175 | } else if (User *GEPRHS = dyn_castGetElementPtr(RHS)) { |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 4176 | // If the base pointers are different, but the indices are the same, just |
| 4177 | // compare the base pointer. |
| 4178 | if (PtrBase != GEPRHS->getOperand(0)) { |
| 4179 | bool IndicesTheSame = GEPLHS->getNumOperands()==GEPRHS->getNumOperands(); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 4180 | IndicesTheSame &= GEPLHS->getOperand(0)->getType() == |
Chris Lattner | 93b94a6 | 2005-04-26 14:40:41 +0000 | [diff] [blame] | 4181 | GEPRHS->getOperand(0)->getType(); |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 4182 | if (IndicesTheSame) |
| 4183 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i) |
| 4184 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { |
| 4185 | IndicesTheSame = false; |
| 4186 | break; |
| 4187 | } |
| 4188 | |
| 4189 | // If all indices are the same, just compare the base pointers. |
| 4190 | if (IndicesTheSame) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4191 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), |
| 4192 | GEPLHS->getOperand(0), GEPRHS->getOperand(0)); |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 4193 | |
| 4194 | // Otherwise, the base pointers are different and the indices are |
| 4195 | // different, bail out. |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4196 | return 0; |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 4197 | } |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4198 | |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4199 | // If one of the GEPs has all zero indices, recurse. |
| 4200 | bool AllZeros = true; |
| 4201 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i) |
| 4202 | if (!isa<Constant>(GEPLHS->getOperand(i)) || |
| 4203 | !cast<Constant>(GEPLHS->getOperand(i))->isNullValue()) { |
| 4204 | AllZeros = false; |
| 4205 | break; |
| 4206 | } |
| 4207 | if (AllZeros) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4208 | return FoldGEPICmp(GEPRHS, GEPLHS->getOperand(0), |
| 4209 | ICmpInst::getSwappedPredicate(Cond), I); |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 4210 | |
| 4211 | // If the other GEP has all zero indices, recurse. |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4212 | AllZeros = true; |
| 4213 | for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i) |
| 4214 | if (!isa<Constant>(GEPRHS->getOperand(i)) || |
| 4215 | !cast<Constant>(GEPRHS->getOperand(i))->isNullValue()) { |
| 4216 | AllZeros = false; |
| 4217 | break; |
| 4218 | } |
| 4219 | if (AllZeros) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4220 | return FoldGEPICmp(GEPLHS, GEPRHS->getOperand(0), Cond, I); |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4221 | |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 4222 | if (GEPLHS->getNumOperands() == GEPRHS->getNumOperands()) { |
| 4223 | // If the GEPs only differ by one index, compare it. |
| 4224 | unsigned NumDifferences = 0; // Keep track of # differences. |
| 4225 | unsigned DiffOperand = 0; // The operand that differs. |
| 4226 | for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i) |
| 4227 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4228 | if (GEPLHS->getOperand(i)->getType()->getPrimitiveSizeInBits() != |
| 4229 | GEPRHS->getOperand(i)->getType()->getPrimitiveSizeInBits()) { |
Chris Lattner | 45f57b8 | 2005-01-21 23:06:49 +0000 | [diff] [blame] | 4230 | // Irreconcilable differences. |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 4231 | NumDifferences = 2; |
| 4232 | break; |
| 4233 | } else { |
| 4234 | if (NumDifferences++) break; |
| 4235 | DiffOperand = i; |
| 4236 | } |
| 4237 | } |
| 4238 | |
| 4239 | if (NumDifferences == 0) // SAME GEP? |
| 4240 | return ReplaceInstUsesWith(I, // No comparison is needed here. |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4241 | ConstantInt::get(Type::Int1Ty, |
| 4242 | Cond == ICmpInst::ICMP_EQ)); |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 4243 | else if (NumDifferences == 1) { |
Chris Lattner | 45f57b8 | 2005-01-21 23:06:49 +0000 | [diff] [blame] | 4244 | Value *LHSV = GEPLHS->getOperand(DiffOperand); |
| 4245 | Value *RHSV = GEPRHS->getOperand(DiffOperand); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4246 | // Make sure we do a signed comparison here. |
| 4247 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), LHSV, RHSV); |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 4248 | } |
| 4249 | } |
| 4250 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4251 | // Only lower this if the icmp is the only user of the GEP or if we expect |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4252 | // the result to fold to a constant! |
| 4253 | if ((isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) && |
| 4254 | (isa<ConstantExpr>(GEPRHS) || GEPRHS->hasOneUse())) { |
| 4255 | // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) ---> (OFFSET1 cmp OFFSET2) |
| 4256 | Value *L = EmitGEPOffset(GEPLHS, I, *this); |
| 4257 | Value *R = EmitGEPOffset(GEPRHS, I, *this); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4258 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), L, R); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4259 | } |
| 4260 | } |
| 4261 | return 0; |
| 4262 | } |
| 4263 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4264 | Instruction *InstCombiner::visitFCmpInst(FCmpInst &I) { |
| 4265 | bool Changed = SimplifyCompare(I); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4266 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4267 | |
Chris Lattner | 58e9746 | 2007-01-14 19:42:17 +0000 | [diff] [blame] | 4268 | // Fold trivial predicates. |
| 4269 | if (I.getPredicate() == FCmpInst::FCMP_FALSE) |
| 4270 | return ReplaceInstUsesWith(I, Constant::getNullValue(Type::Int1Ty)); |
| 4271 | if (I.getPredicate() == FCmpInst::FCMP_TRUE) |
| 4272 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 1)); |
| 4273 | |
| 4274 | // Simplify 'fcmp pred X, X' |
| 4275 | if (Op0 == Op1) { |
| 4276 | switch (I.getPredicate()) { |
| 4277 | default: assert(0 && "Unknown predicate!"); |
| 4278 | case FCmpInst::FCMP_UEQ: // True if unordered or equal |
| 4279 | case FCmpInst::FCMP_UGE: // True if unordered, greater than, or equal |
| 4280 | case FCmpInst::FCMP_ULE: // True if unordered, less than, or equal |
| 4281 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 1)); |
| 4282 | case FCmpInst::FCMP_OGT: // True if ordered and greater than |
| 4283 | case FCmpInst::FCMP_OLT: // True if ordered and less than |
| 4284 | case FCmpInst::FCMP_ONE: // True if ordered and operands are unequal |
| 4285 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 0)); |
| 4286 | |
| 4287 | case FCmpInst::FCMP_UNO: // True if unordered: isnan(X) | isnan(Y) |
| 4288 | case FCmpInst::FCMP_ULT: // True if unordered or less than |
| 4289 | case FCmpInst::FCMP_UGT: // True if unordered or greater than |
| 4290 | case FCmpInst::FCMP_UNE: // True if unordered or not equal |
| 4291 | // Canonicalize these to be 'fcmp uno %X, 0.0'. |
| 4292 | I.setPredicate(FCmpInst::FCMP_UNO); |
| 4293 | I.setOperand(1, Constant::getNullValue(Op0->getType())); |
| 4294 | return &I; |
| 4295 | |
| 4296 | case FCmpInst::FCMP_ORD: // True if ordered (no nans) |
| 4297 | case FCmpInst::FCMP_OEQ: // True if ordered and equal |
| 4298 | case FCmpInst::FCMP_OGE: // True if ordered and greater than or equal |
| 4299 | case FCmpInst::FCMP_OLE: // True if ordered and less than or equal |
| 4300 | // Canonicalize these to be 'fcmp ord %X, 0.0'. |
| 4301 | I.setPredicate(FCmpInst::FCMP_ORD); |
| 4302 | I.setOperand(1, Constant::getNullValue(Op0->getType())); |
| 4303 | return &I; |
| 4304 | } |
| 4305 | } |
| 4306 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4307 | if (isa<UndefValue>(Op1)) // fcmp pred X, undef -> undef |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 4308 | return ReplaceInstUsesWith(I, UndefValue::get(Type::Int1Ty)); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4309 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4310 | // Handle fcmp with constant RHS |
| 4311 | if (Constant *RHSC = dyn_cast<Constant>(Op1)) { |
| 4312 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
| 4313 | switch (LHSI->getOpcode()) { |
| 4314 | case Instruction::PHI: |
| 4315 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 4316 | return NV; |
| 4317 | break; |
| 4318 | case Instruction::Select: |
| 4319 | // If either operand of the select is a constant, we can fold the |
| 4320 | // comparison into the select arms, which will cause one to be |
| 4321 | // constant folded and the select turned into a bitwise or. |
| 4322 | Value *Op1 = 0, *Op2 = 0; |
| 4323 | if (LHSI->hasOneUse()) { |
| 4324 | if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) { |
| 4325 | // Fold the known value into the constant operand. |
| 4326 | Op1 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC); |
| 4327 | // Insert a new FCmp of the other select operand. |
| 4328 | Op2 = InsertNewInstBefore(new FCmpInst(I.getPredicate(), |
| 4329 | LHSI->getOperand(2), RHSC, |
| 4330 | I.getName()), I); |
| 4331 | } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) { |
| 4332 | // Fold the known value into the constant operand. |
| 4333 | Op2 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC); |
| 4334 | // Insert a new FCmp of the other select operand. |
| 4335 | Op1 = InsertNewInstBefore(new FCmpInst(I.getPredicate(), |
| 4336 | LHSI->getOperand(1), RHSC, |
| 4337 | I.getName()), I); |
| 4338 | } |
| 4339 | } |
| 4340 | |
| 4341 | if (Op1) |
| 4342 | return new SelectInst(LHSI->getOperand(0), Op1, Op2); |
| 4343 | break; |
| 4344 | } |
| 4345 | } |
| 4346 | |
| 4347 | return Changed ? &I : 0; |
| 4348 | } |
| 4349 | |
| 4350 | Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { |
| 4351 | bool Changed = SimplifyCompare(I); |
| 4352 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 4353 | const Type *Ty = Op0->getType(); |
| 4354 | |
| 4355 | // icmp X, X |
| 4356 | if (Op0 == Op1) |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4357 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
| 4358 | isTrueWhenEqual(I))); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4359 | |
| 4360 | if (isa<UndefValue>(Op1)) // X icmp undef -> undef |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 4361 | return ReplaceInstUsesWith(I, UndefValue::get(Type::Int1Ty)); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4362 | |
| 4363 | // icmp of GlobalValues can never equal each other as long as they aren't |
| 4364 | // external weak linkage type. |
| 4365 | if (GlobalValue *GV0 = dyn_cast<GlobalValue>(Op0)) |
| 4366 | if (GlobalValue *GV1 = dyn_cast<GlobalValue>(Op1)) |
| 4367 | if (!GV0->hasExternalWeakLinkage() || !GV1->hasExternalWeakLinkage()) |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4368 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
| 4369 | !isTrueWhenEqual(I))); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4370 | |
| 4371 | // icmp <global/alloca*/null>, <global/alloca*/null> - Global/Stack value |
Chris Lattner | 711b340 | 2004-11-14 07:33:16 +0000 | [diff] [blame] | 4372 | // addresses never equal each other! We already know that Op0 != Op1. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4373 | if ((isa<GlobalValue>(Op0) || isa<AllocaInst>(Op0) || |
| 4374 | isa<ConstantPointerNull>(Op0)) && |
| 4375 | (isa<GlobalValue>(Op1) || isa<AllocaInst>(Op1) || |
Chris Lattner | 711b340 | 2004-11-14 07:33:16 +0000 | [diff] [blame] | 4376 | isa<ConstantPointerNull>(Op1))) |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4377 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
| 4378 | !isTrueWhenEqual(I))); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4379 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4380 | // icmp's with boolean values can always be turned into bitwise operations |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 4381 | if (Ty == Type::Int1Ty) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4382 | switch (I.getPredicate()) { |
| 4383 | default: assert(0 && "Invalid icmp instruction!"); |
| 4384 | case ICmpInst::ICMP_EQ: { // icmp eq bool %A, %B -> ~(A^B) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4385 | Instruction *Xor = BinaryOperator::createXor(Op0, Op1, I.getName()+"tmp"); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4386 | InsertNewInstBefore(Xor, I); |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 4387 | return BinaryOperator::createNot(Xor); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4388 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4389 | case ICmpInst::ICMP_NE: // icmp eq bool %A, %B -> A^B |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 4390 | return BinaryOperator::createXor(Op0, Op1); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4391 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4392 | case ICmpInst::ICMP_UGT: |
| 4393 | case ICmpInst::ICMP_SGT: |
| 4394 | std::swap(Op0, Op1); // Change icmp gt -> icmp lt |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 4395 | // FALL THROUGH |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4396 | case ICmpInst::ICMP_ULT: |
| 4397 | case ICmpInst::ICMP_SLT: { // icmp lt bool A, B -> ~X & Y |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 4398 | Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp"); |
| 4399 | InsertNewInstBefore(Not, I); |
| 4400 | return BinaryOperator::createAnd(Not, Op1); |
| 4401 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4402 | case ICmpInst::ICMP_UGE: |
| 4403 | case ICmpInst::ICMP_SGE: |
| 4404 | std::swap(Op0, Op1); // Change icmp ge -> icmp le |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 4405 | // FALL THROUGH |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4406 | case ICmpInst::ICMP_ULE: |
| 4407 | case ICmpInst::ICMP_SLE: { // icmp le bool %A, %B -> ~A | B |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 4408 | Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp"); |
| 4409 | InsertNewInstBefore(Not, I); |
| 4410 | return BinaryOperator::createOr(Not, Op1); |
| 4411 | } |
| 4412 | } |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4413 | } |
| 4414 | |
Chris Lattner | 2be51ae | 2004-06-09 04:24:29 +0000 | [diff] [blame] | 4415 | // See if we are doing a comparison between a constant and an instruction that |
| 4416 | // can be folded into the comparison. |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4417 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4418 | switch (I.getPredicate()) { |
| 4419 | default: break; |
| 4420 | case ICmpInst::ICMP_ULT: // A <u MIN -> FALSE |
| 4421 | if (CI->isMinValue(false)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4422 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4423 | if (CI->isMaxValue(false)) // A <u MAX -> A != MAX |
| 4424 | return new ICmpInst(ICmpInst::ICMP_NE, Op0,Op1); |
| 4425 | if (isMinValuePlusOne(CI,false)) // A <u MIN+1 -> A == MIN |
| 4426 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, SubOne(CI)); |
| 4427 | break; |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4428 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4429 | case ICmpInst::ICMP_SLT: |
| 4430 | if (CI->isMinValue(true)) // A <s MIN -> FALSE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4431 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4432 | if (CI->isMaxValue(true)) // A <s MAX -> A != MAX |
| 4433 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
| 4434 | if (isMinValuePlusOne(CI,true)) // A <s MIN+1 -> A == MIN |
| 4435 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, SubOne(CI)); |
| 4436 | break; |
| 4437 | |
| 4438 | case ICmpInst::ICMP_UGT: |
| 4439 | if (CI->isMaxValue(false)) // A >u MAX -> FALSE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4440 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4441 | if (CI->isMinValue(false)) // A >u MIN -> A != MIN |
| 4442 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
| 4443 | if (isMaxValueMinusOne(CI, false)) // A >u MAX-1 -> A == MAX |
| 4444 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, AddOne(CI)); |
| 4445 | break; |
| 4446 | |
| 4447 | case ICmpInst::ICMP_SGT: |
| 4448 | if (CI->isMaxValue(true)) // A >s MAX -> FALSE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4449 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4450 | if (CI->isMinValue(true)) // A >s MIN -> A != MIN |
| 4451 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
| 4452 | if (isMaxValueMinusOne(CI, true)) // A >s MAX-1 -> A == MAX |
| 4453 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, AddOne(CI)); |
| 4454 | break; |
| 4455 | |
| 4456 | case ICmpInst::ICMP_ULE: |
| 4457 | if (CI->isMaxValue(false)) // A <=u MAX -> TRUE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4458 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4459 | if (CI->isMinValue(false)) // A <=u MIN -> A == MIN |
| 4460 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); |
| 4461 | if (isMaxValueMinusOne(CI,false)) // A <=u MAX-1 -> A != MAX |
| 4462 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, AddOne(CI)); |
| 4463 | break; |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4464 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4465 | case ICmpInst::ICMP_SLE: |
| 4466 | if (CI->isMaxValue(true)) // A <=s MAX -> TRUE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4467 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4468 | if (CI->isMinValue(true)) // A <=s MIN -> A == MIN |
| 4469 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); |
| 4470 | if (isMaxValueMinusOne(CI,true)) // A <=s MAX-1 -> A != MAX |
| 4471 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, AddOne(CI)); |
| 4472 | break; |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4473 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4474 | case ICmpInst::ICMP_UGE: |
| 4475 | if (CI->isMinValue(false)) // A >=u MIN -> TRUE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4476 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4477 | if (CI->isMaxValue(false)) // A >=u MAX -> A == MAX |
| 4478 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); |
| 4479 | if (isMinValuePlusOne(CI,false)) // A >=u MIN-1 -> A != MIN |
| 4480 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, SubOne(CI)); |
| 4481 | break; |
| 4482 | |
| 4483 | case ICmpInst::ICMP_SGE: |
| 4484 | if (CI->isMinValue(true)) // A >=s MIN -> TRUE |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4485 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4486 | if (CI->isMaxValue(true)) // A >=s MAX -> A == MAX |
| 4487 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); |
| 4488 | if (isMinValuePlusOne(CI,true)) // A >=s MIN-1 -> A != MIN |
| 4489 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, SubOne(CI)); |
| 4490 | break; |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4491 | } |
| 4492 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4493 | // If we still have a icmp le or icmp ge instruction, turn it into the |
| 4494 | // appropriate icmp lt or icmp gt instruction. Since the border cases have |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4495 | // already been handled above, this requires little checking. |
| 4496 | // |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4497 | if (I.getPredicate() == ICmpInst::ICMP_ULE) |
| 4498 | return new ICmpInst(ICmpInst::ICMP_ULT, Op0, AddOne(CI)); |
| 4499 | if (I.getPredicate() == ICmpInst::ICMP_SLE) |
| 4500 | return new ICmpInst(ICmpInst::ICMP_SLT, Op0, AddOne(CI)); |
| 4501 | if (I.getPredicate() == ICmpInst::ICMP_UGE) |
| 4502 | return new ICmpInst( ICmpInst::ICMP_UGT, Op0, SubOne(CI)); |
| 4503 | if (I.getPredicate() == ICmpInst::ICMP_SGE) |
| 4504 | return new ICmpInst(ICmpInst::ICMP_SGT, Op0, SubOne(CI)); |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 4505 | |
| 4506 | // See if we can fold the comparison based on bits known to be zero or one |
| 4507 | // in the input. |
| 4508 | uint64_t KnownZero, KnownOne; |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 4509 | if (SimplifyDemandedBits(Op0, cast<IntegerType>(Ty)->getBitMask(), |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 4510 | KnownZero, KnownOne, 0)) |
| 4511 | return &I; |
| 4512 | |
| 4513 | // Given the known and unknown bits, compute a range that the LHS could be |
| 4514 | // in. |
| 4515 | if (KnownOne | KnownZero) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4516 | // Compute the Min, Max and RHS values based on the known bits. For the |
| 4517 | // EQ and NE we use unsigned values. |
Reid Spencer | b3307b2 | 2006-12-23 19:17:57 +0000 | [diff] [blame] | 4518 | uint64_t UMin = 0, UMax = 0, URHSVal = 0; |
| 4519 | int64_t SMin = 0, SMax = 0, SRHSVal = 0; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4520 | if (ICmpInst::isSignedPredicate(I.getPredicate())) { |
| 4521 | SRHSVal = CI->getSExtValue(); |
| 4522 | ComputeSignedMinMaxValuesFromKnownBits(Ty, KnownZero, KnownOne, SMin, |
| 4523 | SMax); |
| 4524 | } else { |
| 4525 | URHSVal = CI->getZExtValue(); |
| 4526 | ComputeUnsignedMinMaxValuesFromKnownBits(Ty, KnownZero, KnownOne, UMin, |
| 4527 | UMax); |
| 4528 | } |
| 4529 | switch (I.getPredicate()) { // LE/GE have been folded already. |
| 4530 | default: assert(0 && "Unknown icmp opcode!"); |
| 4531 | case ICmpInst::ICMP_EQ: |
| 4532 | if (UMax < URHSVal || UMin > URHSVal) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4533 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4534 | break; |
| 4535 | case ICmpInst::ICMP_NE: |
| 4536 | if (UMax < URHSVal || UMin > URHSVal) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4537 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4538 | break; |
| 4539 | case ICmpInst::ICMP_ULT: |
| 4540 | if (UMax < URHSVal) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4541 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4542 | if (UMin > URHSVal) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4543 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4544 | break; |
| 4545 | case ICmpInst::ICMP_UGT: |
| 4546 | if (UMin > URHSVal) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4547 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4548 | if (UMax < URHSVal) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4549 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4550 | break; |
| 4551 | case ICmpInst::ICMP_SLT: |
| 4552 | if (SMax < SRHSVal) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4553 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4554 | if (SMin > SRHSVal) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4555 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4556 | break; |
| 4557 | case ICmpInst::ICMP_SGT: |
| 4558 | if (SMin > SRHSVal) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4559 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4560 | if (SMax < SRHSVal) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4561 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4562 | break; |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 4563 | } |
| 4564 | } |
| 4565 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4566 | // Since the RHS is a ConstantInt (CI), if the left hand side is an |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 4567 | // instruction, see if that instruction also has constants so that the |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4568 | // instruction can be folded into the icmp |
Chris Lattner | 3c6a0d4 | 2004-05-25 06:32:08 +0000 | [diff] [blame] | 4569 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
Chris Lattner | 648e3bc | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 4570 | switch (LHSI->getOpcode()) { |
| 4571 | case Instruction::And: |
| 4572 | if (LHSI->hasOneUse() && isa<ConstantInt>(LHSI->getOperand(1)) && |
| 4573 | LHSI->getOperand(0)->hasOneUse()) { |
Chris Lattner | e695a3b | 2006-09-18 05:27:43 +0000 | [diff] [blame] | 4574 | ConstantInt *AndCST = cast<ConstantInt>(LHSI->getOperand(1)); |
| 4575 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4576 | // If the LHS is an AND of a truncating cast, we can widen the |
Chris Lattner | e695a3b | 2006-09-18 05:27:43 +0000 | [diff] [blame] | 4577 | // and/compare to be the input width without changing the value |
| 4578 | // produced, eliminating a cast. |
| 4579 | if (CastInst *Cast = dyn_cast<CastInst>(LHSI->getOperand(0))) { |
| 4580 | // We can do this transformation if either the AND constant does not |
| 4581 | // have its sign bit set or if it is an equality comparison. |
| 4582 | // Extending a relational comparison when we're checking the sign |
| 4583 | // bit would not work. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 4584 | if (Cast->hasOneUse() && isa<TruncInst>(Cast) && |
Chris Lattner | e695a3b | 2006-09-18 05:27:43 +0000 | [diff] [blame] | 4585 | (I.isEquality() || |
| 4586 | (AndCST->getZExtValue() == (uint64_t)AndCST->getSExtValue()) && |
| 4587 | (CI->getZExtValue() == (uint64_t)CI->getSExtValue()))) { |
| 4588 | ConstantInt *NewCST; |
| 4589 | ConstantInt *NewCI; |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 4590 | NewCST = ConstantInt::get(Cast->getOperand(0)->getType(), |
| 4591 | AndCST->getZExtValue()); |
| 4592 | NewCI = ConstantInt::get(Cast->getOperand(0)->getType(), |
| 4593 | CI->getZExtValue()); |
Chris Lattner | e695a3b | 2006-09-18 05:27:43 +0000 | [diff] [blame] | 4594 | Instruction *NewAnd = |
| 4595 | BinaryOperator::createAnd(Cast->getOperand(0), NewCST, |
| 4596 | LHSI->getName()); |
| 4597 | InsertNewInstBefore(NewAnd, I); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4598 | return new ICmpInst(I.getPredicate(), NewAnd, NewCI); |
Chris Lattner | e695a3b | 2006-09-18 05:27:43 +0000 | [diff] [blame] | 4599 | } |
| 4600 | } |
| 4601 | |
Chris Lattner | 648e3bc | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 4602 | // If this is: (X >> C1) & C2 != C3 (where any shift and any compare |
| 4603 | // could exist), turn it into (X & (C2 << C1)) != (C3 << C1). This |
| 4604 | // happens a LOT in code produced by the C front-end, for bitfield |
| 4605 | // access. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4606 | BinaryOperator *Shift = dyn_cast<BinaryOperator>(LHSI->getOperand(0)); |
| 4607 | if (Shift && !Shift->isShift()) |
| 4608 | Shift = 0; |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 4609 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4610 | ConstantInt *ShAmt; |
| 4611 | ShAmt = Shift ? dyn_cast<ConstantInt>(Shift->getOperand(1)) : 0; |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 4612 | const Type *Ty = Shift ? Shift->getType() : 0; // Type of the shift. |
| 4613 | const Type *AndTy = AndCST->getType(); // Type of the and. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4614 | |
Chris Lattner | 648e3bc | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 4615 | // We can fold this as long as we can't shift unknown bits |
| 4616 | // into the mask. This can only happen with signed shift |
| 4617 | // rights, as they sign-extend. |
| 4618 | if (ShAmt) { |
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 4619 | bool CanFold = Shift->isLogicalShift(); |
Chris Lattner | 648e3bc | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 4620 | if (!CanFold) { |
| 4621 | // To test for the bad case of the signed shr, see if any |
| 4622 | // of the bits shifted in could be tested after the mask. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4623 | int ShAmtVal = Ty->getPrimitiveSizeInBits()-ShAmt->getZExtValue(); |
Chris Lattner | d7e31cf | 2005-06-17 01:29:28 +0000 | [diff] [blame] | 4624 | if (ShAmtVal < 0) ShAmtVal = 0; // Out of range shift. |
| 4625 | |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4626 | Constant *OShAmt = ConstantInt::get(AndTy, ShAmtVal); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4627 | Constant *ShVal = |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 4628 | ConstantExpr::getShl(ConstantInt::getAllOnesValue(AndTy), |
| 4629 | OShAmt); |
Chris Lattner | 648e3bc | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 4630 | if (ConstantExpr::getAnd(ShVal, AndCST)->isNullValue()) |
| 4631 | CanFold = true; |
| 4632 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4633 | |
Chris Lattner | 648e3bc | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 4634 | if (CanFold) { |
Chris Lattner | 0cba71b | 2004-09-28 17:54:07 +0000 | [diff] [blame] | 4635 | Constant *NewCst; |
| 4636 | if (Shift->getOpcode() == Instruction::Shl) |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 4637 | NewCst = ConstantExpr::getLShr(CI, ShAmt); |
Chris Lattner | 0cba71b | 2004-09-28 17:54:07 +0000 | [diff] [blame] | 4638 | else |
| 4639 | NewCst = ConstantExpr::getShl(CI, ShAmt); |
Chris Lattner | 83c4ec0 | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 4640 | |
Chris Lattner | 648e3bc | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 4641 | // Check to see if we are shifting out any of the bits being |
| 4642 | // compared. |
| 4643 | if (ConstantExpr::get(Shift->getOpcode(), NewCst, ShAmt) != CI){ |
| 4644 | // If we shifted bits out, the fold is not going to work out. |
| 4645 | // As a special case, check to see if this means that the |
| 4646 | // result is always true or false now. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4647 | if (I.getPredicate() == ICmpInst::ICMP_EQ) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4648 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4649 | if (I.getPredicate() == ICmpInst::ICMP_NE) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4650 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | 648e3bc | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 4651 | } else { |
| 4652 | I.setOperand(1, NewCst); |
Chris Lattner | 0cba71b | 2004-09-28 17:54:07 +0000 | [diff] [blame] | 4653 | Constant *NewAndCST; |
| 4654 | if (Shift->getOpcode() == Instruction::Shl) |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 4655 | NewAndCST = ConstantExpr::getLShr(AndCST, ShAmt); |
Chris Lattner | 0cba71b | 2004-09-28 17:54:07 +0000 | [diff] [blame] | 4656 | else |
| 4657 | NewAndCST = ConstantExpr::getShl(AndCST, ShAmt); |
| 4658 | LHSI->setOperand(1, NewAndCST); |
Reid Spencer | 8c5a53a | 2007-01-04 05:23:51 +0000 | [diff] [blame] | 4659 | LHSI->setOperand(0, Shift->getOperand(0)); |
Chris Lattner | 648e3bc | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 4660 | WorkList.push_back(Shift); // Shift is dead. |
| 4661 | AddUsesToWorkList(I); |
| 4662 | return &I; |
Chris Lattner | 5eb9194 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 4663 | } |
| 4664 | } |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 4665 | } |
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 4666 | |
| 4667 | // Turn ((X >> Y) & C) == 0 into (X & (C << Y)) == 0. The later is |
| 4668 | // preferable because it allows the C<<Y expression to be hoisted out |
| 4669 | // of a loop if Y is invariant and X is not. |
| 4670 | if (Shift && Shift->hasOneUse() && CI->isNullValue() && |
Chris Lattner | 6d7ca92 | 2006-09-18 18:27:05 +0000 | [diff] [blame] | 4671 | I.isEquality() && !Shift->isArithmeticShift() && |
| 4672 | isa<Instruction>(Shift->getOperand(0))) { |
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 4673 | // Compute C << Y. |
| 4674 | Value *NS; |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 4675 | if (Shift->getOpcode() == Instruction::LShr) { |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 4676 | NS = BinaryOperator::createShl(AndCST, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4677 | Shift->getOperand(1), "tmp"); |
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 4678 | } else { |
Reid Spencer | 7eb7638 | 2006-12-13 17:19:09 +0000 | [diff] [blame] | 4679 | // Insert a logical shift. |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 4680 | NS = BinaryOperator::createLShr(AndCST, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4681 | Shift->getOperand(1), "tmp"); |
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 4682 | } |
| 4683 | InsertNewInstBefore(cast<Instruction>(NS), I); |
| 4684 | |
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 4685 | // Compute X & (C << Y). |
Reid Spencer | 8c5a53a | 2007-01-04 05:23:51 +0000 | [diff] [blame] | 4686 | Instruction *NewAnd = BinaryOperator::createAnd( |
| 4687 | Shift->getOperand(0), NS, LHSI->getName()); |
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 4688 | InsertNewInstBefore(NewAnd, I); |
| 4689 | |
| 4690 | I.setOperand(0, NewAnd); |
| 4691 | return &I; |
| 4692 | } |
Chris Lattner | 648e3bc | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 4693 | } |
| 4694 | break; |
Chris Lattner | 83c4ec0 | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 4695 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4696 | case Instruction::Shl: // (icmp pred (shl X, ShAmt), CI) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4697 | if (ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1))) { |
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 4698 | if (I.isEquality()) { |
Chris Lattner | e17a128 | 2005-06-15 20:53:31 +0000 | [diff] [blame] | 4699 | unsigned TypeBits = CI->getType()->getPrimitiveSizeInBits(); |
| 4700 | |
| 4701 | // Check that the shift amount is in range. If not, don't perform |
| 4702 | // undefined shifts. When the shift is visited it will be |
| 4703 | // simplified. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4704 | if (ShAmt->getZExtValue() >= TypeBits) |
Chris Lattner | e17a128 | 2005-06-15 20:53:31 +0000 | [diff] [blame] | 4705 | break; |
| 4706 | |
Chris Lattner | 18d19ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 4707 | // If we are comparing against bits always shifted out, the |
| 4708 | // comparison cannot succeed. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4709 | Constant *Comp = |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 4710 | ConstantExpr::getShl(ConstantExpr::getLShr(CI, ShAmt), ShAmt); |
Chris Lattner | 18d19ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 4711 | if (Comp != CI) {// Comparing against a bit that we know is zero. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4712 | bool IsICMP_NE = I.getPredicate() == ICmpInst::ICMP_NE; |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4713 | Constant *Cst = ConstantInt::get(Type::Int1Ty, IsICMP_NE); |
Chris Lattner | 18d19ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 4714 | return ReplaceInstUsesWith(I, Cst); |
| 4715 | } |
| 4716 | |
| 4717 | if (LHSI->hasOneUse()) { |
| 4718 | // Otherwise strength reduce the shift into an and. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4719 | unsigned ShAmtVal = (unsigned)ShAmt->getZExtValue(); |
Chris Lattner | 18d19ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 4720 | uint64_t Val = (1ULL << (TypeBits-ShAmtVal))-1; |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 4721 | Constant *Mask = ConstantInt::get(CI->getType(), Val); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4722 | |
Chris Lattner | 18d19ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 4723 | Instruction *AndI = |
| 4724 | BinaryOperator::createAnd(LHSI->getOperand(0), |
| 4725 | Mask, LHSI->getName()+".mask"); |
| 4726 | Value *And = InsertNewInstBefore(AndI, I); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4727 | return new ICmpInst(I.getPredicate(), And, |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 4728 | ConstantExpr::getLShr(CI, ShAmt)); |
Chris Lattner | 18d19ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 4729 | } |
| 4730 | } |
Chris Lattner | 18d19ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 4731 | } |
| 4732 | break; |
| 4733 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4734 | case Instruction::LShr: // (icmp pred (shr X, ShAmt), CI) |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 4735 | case Instruction::AShr: |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4736 | if (ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1))) { |
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 4737 | if (I.isEquality()) { |
Chris Lattner | e17a128 | 2005-06-15 20:53:31 +0000 | [diff] [blame] | 4738 | // Check that the shift amount is in range. If not, don't perform |
| 4739 | // undefined shifts. When the shift is visited it will be |
| 4740 | // simplified. |
Chris Lattner | aa457ac | 2005-06-16 01:52:07 +0000 | [diff] [blame] | 4741 | unsigned TypeBits = CI->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4742 | if (ShAmt->getZExtValue() >= TypeBits) |
Chris Lattner | e17a128 | 2005-06-15 20:53:31 +0000 | [diff] [blame] | 4743 | break; |
| 4744 | |
Chris Lattner | f63f647 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 4745 | // If we are comparing against bits always shifted out, the |
| 4746 | // comparison cannot succeed. |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 4747 | Constant *Comp; |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 4748 | if (LHSI->getOpcode() == Instruction::LShr) |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 4749 | Comp = ConstantExpr::getLShr(ConstantExpr::getShl(CI, ShAmt), |
| 4750 | ShAmt); |
| 4751 | else |
| 4752 | Comp = ConstantExpr::getAShr(ConstantExpr::getShl(CI, ShAmt), |
| 4753 | ShAmt); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4754 | |
Chris Lattner | f63f647 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 4755 | if (Comp != CI) {// Comparing against a bit that we know is zero. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4756 | bool IsICMP_NE = I.getPredicate() == ICmpInst::ICMP_NE; |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4757 | Constant *Cst = ConstantInt::get(Type::Int1Ty, IsICMP_NE); |
Chris Lattner | f63f647 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 4758 | return ReplaceInstUsesWith(I, Cst); |
| 4759 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4760 | |
Chris Lattner | f63f647 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 4761 | if (LHSI->hasOneUse() || CI->isNullValue()) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4762 | unsigned ShAmtVal = (unsigned)ShAmt->getZExtValue(); |
Chris Lattner | 18d19ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 4763 | |
Chris Lattner | f63f647 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 4764 | // Otherwise strength reduce the shift into an and. |
| 4765 | uint64_t Val = ~0ULL; // All ones. |
| 4766 | Val <<= ShAmtVal; // Shift over to the right spot. |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 4767 | Val &= ~0ULL >> (64-TypeBits); |
| 4768 | Constant *Mask = ConstantInt::get(CI->getType(), Val); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4769 | |
Chris Lattner | f63f647 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 4770 | Instruction *AndI = |
| 4771 | BinaryOperator::createAnd(LHSI->getOperand(0), |
| 4772 | Mask, LHSI->getName()+".mask"); |
| 4773 | Value *And = InsertNewInstBefore(AndI, I); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4774 | return new ICmpInst(I.getPredicate(), And, |
Chris Lattner | f63f647 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 4775 | ConstantExpr::getShl(CI, ShAmt)); |
| 4776 | } |
Chris Lattner | f63f647 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 4777 | } |
| 4778 | } |
| 4779 | break; |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 4780 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 4781 | case Instruction::SDiv: |
| 4782 | case Instruction::UDiv: |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4783 | // Fold: icmp pred ([us]div X, C1), C2 -> range test |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 4784 | // Fold this div into the comparison, producing a range check. |
| 4785 | // Determine, based on the divide type, what the range is being |
| 4786 | // checked. If there is an overflow on the low or high side, remember |
| 4787 | // it, otherwise compute the range [low, hi) bounding the new value. |
| 4788 | // See: InsertRangeTest above for the kinds of replacements possible. |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4789 | if (ConstantInt *DivRHS = dyn_cast<ConstantInt>(LHSI->getOperand(1))) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 4790 | // FIXME: If the operand types don't match the type of the divide |
| 4791 | // then don't attempt this transform. The code below doesn't have the |
| 4792 | // logic to deal with a signed divide and an unsigned compare (and |
| 4793 | // vice versa). This is because (x /s C1) <s C2 produces different |
| 4794 | // results than (x /s C1) <u C2 or (x /u C1) <s C2 or even |
| 4795 | // (x /u C1) <u C2. Simply casting the operands and result won't |
| 4796 | // work. :( The if statement below tests that condition and bails |
| 4797 | // if it finds it. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4798 | bool DivIsSigned = LHSI->getOpcode() == Instruction::SDiv; |
| 4799 | if (!I.isEquality() && DivIsSigned != I.isSignedPredicate()) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 4800 | break; |
| 4801 | |
| 4802 | // Initialize the variables that will indicate the nature of the |
| 4803 | // range check. |
| 4804 | bool LoOverflow = false, HiOverflow = false; |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4805 | ConstantInt *LoBound = 0, *HiBound = 0; |
| 4806 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 4807 | // Compute Prod = CI * DivRHS. We are essentially solving an equation |
| 4808 | // of form X/C1=C2. We solve for X by multiplying C1 (DivRHS) and |
| 4809 | // C2 (CI). By solving for X we can turn this into a range check |
| 4810 | // instead of computing a divide. |
| 4811 | ConstantInt *Prod = |
| 4812 | cast<ConstantInt>(ConstantExpr::getMul(CI, DivRHS)); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4813 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 4814 | // Determine if the product overflows by seeing if the product is |
| 4815 | // not equal to the divide. Make sure we do the same kind of divide |
| 4816 | // as in the LHS instruction that we're folding. |
| 4817 | bool ProdOV = !DivRHS->isNullValue() && |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4818 | (DivIsSigned ? ConstantExpr::getSDiv(Prod, DivRHS) : |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 4819 | ConstantExpr::getUDiv(Prod, DivRHS)) != CI; |
| 4820 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4821 | // Get the ICmp opcode |
| 4822 | ICmpInst::Predicate predicate = I.getPredicate(); |
Chris Lattner | 6a9fdfa | 2004-10-11 19:40:04 +0000 | [diff] [blame] | 4823 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 4824 | if (DivRHS->isNullValue()) { |
| 4825 | // Don't hack on divide by zeros! |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4826 | } else if (!DivIsSigned) { // udiv |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4827 | LoBound = Prod; |
| 4828 | LoOverflow = ProdOV; |
| 4829 | HiOverflow = ProdOV || AddWithOverflow(HiBound, LoBound, DivRHS); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 4830 | } else if (isPositive(DivRHS)) { // Divisor is > 0. |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4831 | if (CI->isNullValue()) { // (X / pos) op 0 |
| 4832 | // Can't overflow. |
| 4833 | LoBound = cast<ConstantInt>(ConstantExpr::getNeg(SubOne(DivRHS))); |
| 4834 | HiBound = DivRHS; |
| 4835 | } else if (isPositive(CI)) { // (X / pos) op pos |
| 4836 | LoBound = Prod; |
| 4837 | LoOverflow = ProdOV; |
| 4838 | HiOverflow = ProdOV || AddWithOverflow(HiBound, Prod, DivRHS); |
| 4839 | } else { // (X / pos) op neg |
| 4840 | Constant *DivRHSH = ConstantExpr::getNeg(SubOne(DivRHS)); |
| 4841 | LoOverflow = AddWithOverflow(LoBound, Prod, |
| 4842 | cast<ConstantInt>(DivRHSH)); |
| 4843 | HiBound = Prod; |
| 4844 | HiOverflow = ProdOV; |
| 4845 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 4846 | } else { // Divisor is < 0. |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4847 | if (CI->isNullValue()) { // (X / neg) op 0 |
| 4848 | LoBound = AddOne(DivRHS); |
| 4849 | HiBound = cast<ConstantInt>(ConstantExpr::getNeg(DivRHS)); |
Chris Lattner | 5662503 | 2005-06-17 02:05:55 +0000 | [diff] [blame] | 4850 | if (HiBound == DivRHS) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 4851 | LoBound = 0; // - INTMIN = INTMIN |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4852 | } else if (isPositive(CI)) { // (X / neg) op pos |
| 4853 | HiOverflow = LoOverflow = ProdOV; |
| 4854 | if (!LoOverflow) |
| 4855 | LoOverflow = AddWithOverflow(LoBound, Prod, AddOne(DivRHS)); |
| 4856 | HiBound = AddOne(Prod); |
| 4857 | } else { // (X / neg) op neg |
| 4858 | LoBound = Prod; |
| 4859 | LoOverflow = HiOverflow = ProdOV; |
| 4860 | HiBound = cast<ConstantInt>(ConstantExpr::getSub(Prod, DivRHS)); |
| 4861 | } |
Chris Lattner | 340a05f | 2004-10-08 19:15:44 +0000 | [diff] [blame] | 4862 | |
Chris Lattner | 6a9fdfa | 2004-10-11 19:40:04 +0000 | [diff] [blame] | 4863 | // Dividing by a negate swaps the condition. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4864 | predicate = ICmpInst::getSwappedPredicate(predicate); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4865 | } |
| 4866 | |
| 4867 | if (LoBound) { |
| 4868 | Value *X = LHSI->getOperand(0); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4869 | switch (predicate) { |
| 4870 | default: assert(0 && "Unhandled icmp opcode!"); |
| 4871 | case ICmpInst::ICMP_EQ: |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4872 | if (LoOverflow && HiOverflow) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4873 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4874 | else if (HiOverflow) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4875 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : |
| 4876 | ICmpInst::ICMP_UGE, X, LoBound); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4877 | else if (LoOverflow) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4878 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : |
| 4879 | ICmpInst::ICMP_ULT, X, HiBound); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4880 | else |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4881 | return InsertRangeTest(X, LoBound, HiBound, DivIsSigned, |
| 4882 | true, I); |
| 4883 | case ICmpInst::ICMP_NE: |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4884 | if (LoOverflow && HiOverflow) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4885 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4886 | else if (HiOverflow) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4887 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : |
| 4888 | ICmpInst::ICMP_ULT, X, LoBound); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4889 | else if (LoOverflow) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4890 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : |
| 4891 | ICmpInst::ICMP_UGE, X, HiBound); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4892 | else |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4893 | return InsertRangeTest(X, LoBound, HiBound, DivIsSigned, |
| 4894 | false, I); |
| 4895 | case ICmpInst::ICMP_ULT: |
| 4896 | case ICmpInst::ICMP_SLT: |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4897 | if (LoOverflow) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4898 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4899 | return new ICmpInst(predicate, X, LoBound); |
| 4900 | case ICmpInst::ICMP_UGT: |
| 4901 | case ICmpInst::ICMP_SGT: |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4902 | if (HiOverflow) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4903 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4904 | if (predicate == ICmpInst::ICMP_UGT) |
| 4905 | return new ICmpInst(ICmpInst::ICMP_UGE, X, HiBound); |
| 4906 | else |
| 4907 | return new ICmpInst(ICmpInst::ICMP_SGE, X, HiBound); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4908 | } |
| 4909 | } |
| 4910 | } |
| 4911 | break; |
Chris Lattner | 648e3bc | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 4912 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4913 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4914 | // Simplify icmp_eq and icmp_ne instructions with integer constant RHS. |
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 4915 | if (I.isEquality()) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4916 | bool isICMP_NE = I.getPredicate() == ICmpInst::ICMP_NE; |
Chris Lattner | bc5d414 | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 4917 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4918 | // If the first operand is (add|sub|and|or|xor|rem) with a constant, and |
| 4919 | // the second operand is a constant, simplify a bit. |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 4920 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0)) { |
| 4921 | switch (BO->getOpcode()) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4922 | case Instruction::SRem: |
| 4923 | // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one. |
| 4924 | if (CI->isNullValue() && isa<ConstantInt>(BO->getOperand(1)) && |
| 4925 | BO->hasOneUse()) { |
| 4926 | int64_t V = cast<ConstantInt>(BO->getOperand(1))->getSExtValue(); |
| 4927 | if (V > 1 && isPowerOf2_64(V)) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 4928 | Value *NewRem = InsertNewInstBefore(BinaryOperator::createURem( |
| 4929 | BO->getOperand(0), BO->getOperand(1), BO->getName()), I); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4930 | return new ICmpInst(I.getPredicate(), NewRem, |
| 4931 | Constant::getNullValue(BO->getType())); |
Chris Lattner | 3571b72 | 2004-07-06 07:38:18 +0000 | [diff] [blame] | 4932 | } |
Chris Lattner | bcd7db5 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 4933 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4934 | break; |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 4935 | case Instruction::Add: |
Chris Lattner | 15d58b6 | 2004-06-27 22:51:36 +0000 | [diff] [blame] | 4936 | // Replace ((add A, B) != C) with (A != C-B) if B & C are constants. |
| 4937 | if (ConstantInt *BOp1C = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
Chris Lattner | 3d834bf | 2004-09-21 21:35:23 +0000 | [diff] [blame] | 4938 | if (BO->hasOneUse()) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4939 | return new ICmpInst(I.getPredicate(), BO->getOperand(0), |
| 4940 | ConstantExpr::getSub(CI, BOp1C)); |
Chris Lattner | 15d58b6 | 2004-06-27 22:51:36 +0000 | [diff] [blame] | 4941 | } else if (CI->isNullValue()) { |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 4942 | // Replace ((add A, B) != 0) with (A != -B) if A or B is |
| 4943 | // efficiently invertible, or if the add has just this one use. |
| 4944 | Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4945 | |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 4946 | if (Value *NegVal = dyn_castNegVal(BOp1)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4947 | return new ICmpInst(I.getPredicate(), BOp0, NegVal); |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 4948 | else if (Value *NegVal = dyn_castNegVal(BOp0)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4949 | return new ICmpInst(I.getPredicate(), NegVal, BOp1); |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 4950 | else if (BO->hasOneUse()) { |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 4951 | Instruction *Neg = BinaryOperator::createNeg(BOp1); |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 4952 | InsertNewInstBefore(Neg, I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 4953 | Neg->takeName(BO); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4954 | return new ICmpInst(I.getPredicate(), BOp0, Neg); |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 4955 | } |
| 4956 | } |
| 4957 | break; |
| 4958 | case Instruction::Xor: |
| 4959 | // For the xor case, we can xor two constants together, eliminating |
| 4960 | // the explicit xor. |
| 4961 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4962 | return new ICmpInst(I.getPredicate(), BO->getOperand(0), |
| 4963 | ConstantExpr::getXor(CI, BOC)); |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 4964 | |
| 4965 | // FALLTHROUGH |
| 4966 | case Instruction::Sub: |
| 4967 | // Replace (([sub|xor] A, B) != 0) with (A != B) |
| 4968 | if (CI->isNullValue()) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4969 | return new ICmpInst(I.getPredicate(), BO->getOperand(0), |
| 4970 | BO->getOperand(1)); |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 4971 | break; |
| 4972 | |
| 4973 | case Instruction::Or: |
| 4974 | // If bits are being or'd in that are not present in the constant we |
| 4975 | // are comparing against, then the comparison could never succeed! |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4976 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) { |
Chris Lattner | 448c323 | 2004-06-10 02:12:35 +0000 | [diff] [blame] | 4977 | Constant *NotCI = ConstantExpr::getNot(CI); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4978 | if (!ConstantExpr::getAnd(BOC, NotCI)->isNullValue()) |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4979 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
| 4980 | isICMP_NE)); |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4981 | } |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 4982 | break; |
| 4983 | |
| 4984 | case Instruction::And: |
| 4985 | if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
Chris Lattner | bc5d414 | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 4986 | // If bits are being compared against that are and'd out, then the |
| 4987 | // comparison can never succeed! |
Chris Lattner | 448c323 | 2004-06-10 02:12:35 +0000 | [diff] [blame] | 4988 | if (!ConstantExpr::getAnd(CI, |
| 4989 | ConstantExpr::getNot(BOC))->isNullValue()) |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4990 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
| 4991 | isICMP_NE)); |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 4992 | |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 4993 | // If we have ((X & C) == C), turn it into ((X & C) != 0). |
Chris Lattner | 3285a6f | 2004-06-10 02:33:20 +0000 | [diff] [blame] | 4994 | if (CI == BOC && isOneBitSet(CI)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4995 | return new ICmpInst(isICMP_NE ? ICmpInst::ICMP_EQ : |
| 4996 | ICmpInst::ICMP_NE, Op0, |
| 4997 | Constant::getNullValue(CI->getType())); |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 4998 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4999 | // Replace (and X, (1 << size(X)-1) != 0) with x s< 0 |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 5000 | if (isSignBit(BOC)) { |
| 5001 | Value *X = BO->getOperand(0); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5002 | Constant *Zero = Constant::getNullValue(X->getType()); |
| 5003 | ICmpInst::Predicate pred = isICMP_NE ? |
| 5004 | ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGE; |
| 5005 | return new ICmpInst(pred, X, Zero); |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 5006 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5007 | |
Chris Lattner | 83c4ec0 | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 5008 | // ((X & ~7) == 0) --> X < 8 |
Chris Lattner | b20ba0a | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 5009 | if (CI->isNullValue() && isHighOnes(BOC)) { |
| 5010 | Value *X = BO->getOperand(0); |
Chris Lattner | 83c4ec0 | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 5011 | Constant *NegX = ConstantExpr::getNeg(BOC); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5012 | ICmpInst::Predicate pred = isICMP_NE ? |
| 5013 | ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT; |
| 5014 | return new ICmpInst(pred, X, NegX); |
Chris Lattner | b20ba0a | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 5015 | } |
| 5016 | |
Chris Lattner | bc5d414 | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 5017 | } |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 5018 | default: break; |
| 5019 | } |
Chris Lattner | 458cf46 | 2006-11-29 05:02:16 +0000 | [diff] [blame] | 5020 | } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Op0)) { |
| 5021 | // Handle set{eq|ne} <intrinsic>, intcst. |
| 5022 | switch (II->getIntrinsicID()) { |
| 5023 | default: break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5024 | case Intrinsic::bswap_i16: |
| 5025 | // icmp eq (bswap(x)), c -> icmp eq (x,bswap(c)) |
Chris Lattner | 458cf46 | 2006-11-29 05:02:16 +0000 | [diff] [blame] | 5026 | WorkList.push_back(II); // Dead? |
| 5027 | I.setOperand(0, II->getOperand(1)); |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 5028 | I.setOperand(1, ConstantInt::get(Type::Int16Ty, |
Chris Lattner | 458cf46 | 2006-11-29 05:02:16 +0000 | [diff] [blame] | 5029 | ByteSwap_16(CI->getZExtValue()))); |
| 5030 | return &I; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5031 | case Intrinsic::bswap_i32: |
| 5032 | // icmp eq (bswap(x)), c -> icmp eq (x,bswap(c)) |
Chris Lattner | 458cf46 | 2006-11-29 05:02:16 +0000 | [diff] [blame] | 5033 | WorkList.push_back(II); // Dead? |
| 5034 | I.setOperand(0, II->getOperand(1)); |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 5035 | I.setOperand(1, ConstantInt::get(Type::Int32Ty, |
Chris Lattner | 458cf46 | 2006-11-29 05:02:16 +0000 | [diff] [blame] | 5036 | ByteSwap_32(CI->getZExtValue()))); |
| 5037 | return &I; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5038 | case Intrinsic::bswap_i64: |
| 5039 | // icmp eq (bswap(x)), c -> icmp eq (x,bswap(c)) |
Chris Lattner | 458cf46 | 2006-11-29 05:02:16 +0000 | [diff] [blame] | 5040 | WorkList.push_back(II); // Dead? |
| 5041 | I.setOperand(0, II->getOperand(1)); |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 5042 | I.setOperand(1, ConstantInt::get(Type::Int64Ty, |
Chris Lattner | 458cf46 | 2006-11-29 05:02:16 +0000 | [diff] [blame] | 5043 | ByteSwap_64(CI->getZExtValue()))); |
| 5044 | return &I; |
| 5045 | } |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 5046 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5047 | } else { // Not a ICMP_EQ/ICMP_NE |
| 5048 | // If the LHS is a cast from an integral value of the same size, then |
| 5049 | // since we know the RHS is a constant, try to simlify. |
Chris Lattner | c5943fb | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 5050 | if (CastInst *Cast = dyn_cast<CastInst>(Op0)) { |
| 5051 | Value *CastOp = Cast->getOperand(0); |
| 5052 | const Type *SrcTy = CastOp->getType(); |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5053 | unsigned SrcTySize = SrcTy->getPrimitiveSizeInBits(); |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 5054 | if (SrcTy->isInteger() && |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5055 | SrcTySize == Cast->getType()->getPrimitiveSizeInBits()) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5056 | // If this is an unsigned comparison, try to make the comparison use |
| 5057 | // smaller constant values. |
| 5058 | switch (I.getPredicate()) { |
| 5059 | default: break; |
| 5060 | case ICmpInst::ICMP_ULT: { // X u< 128 => X s> -1 |
| 5061 | ConstantInt *CUI = cast<ConstantInt>(CI); |
| 5062 | if (CUI->getZExtValue() == 1ULL << (SrcTySize-1)) |
| 5063 | return new ICmpInst(ICmpInst::ICMP_SGT, CastOp, |
| 5064 | ConstantInt::get(SrcTy, -1)); |
| 5065 | break; |
| 5066 | } |
| 5067 | case ICmpInst::ICMP_UGT: { // X u> 127 => X s< 0 |
| 5068 | ConstantInt *CUI = cast<ConstantInt>(CI); |
| 5069 | if (CUI->getZExtValue() == (1ULL << (SrcTySize-1))-1) |
| 5070 | return new ICmpInst(ICmpInst::ICMP_SLT, CastOp, |
| 5071 | Constant::getNullValue(SrcTy)); |
| 5072 | break; |
| 5073 | } |
Chris Lattner | c5943fb | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 5074 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5075 | |
Chris Lattner | c5943fb | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 5076 | } |
| 5077 | } |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 5078 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5079 | } |
| 5080 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5081 | // Handle icmp with constant RHS |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5082 | if (Constant *RHSC = dyn_cast<Constant>(Op1)) { |
| 5083 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
| 5084 | switch (LHSI->getOpcode()) { |
Chris Lattner | 9fb25db | 2005-05-01 04:42:15 +0000 | [diff] [blame] | 5085 | case Instruction::GetElementPtr: |
| 5086 | if (RHSC->isNullValue()) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5087 | // icmp pred GEP (P, int 0, int 0, int 0), null -> icmp pred P, null |
Chris Lattner | 9fb25db | 2005-05-01 04:42:15 +0000 | [diff] [blame] | 5088 | bool isAllZeros = true; |
| 5089 | for (unsigned i = 1, e = LHSI->getNumOperands(); i != e; ++i) |
| 5090 | if (!isa<Constant>(LHSI->getOperand(i)) || |
| 5091 | !cast<Constant>(LHSI->getOperand(i))->isNullValue()) { |
| 5092 | isAllZeros = false; |
| 5093 | break; |
| 5094 | } |
| 5095 | if (isAllZeros) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5096 | return new ICmpInst(I.getPredicate(), LHSI->getOperand(0), |
Chris Lattner | 9fb25db | 2005-05-01 04:42:15 +0000 | [diff] [blame] | 5097 | Constant::getNullValue(LHSI->getOperand(0)->getType())); |
| 5098 | } |
| 5099 | break; |
| 5100 | |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5101 | case Instruction::PHI: |
| 5102 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 5103 | return NV; |
| 5104 | break; |
| 5105 | case Instruction::Select: |
| 5106 | // If either operand of the select is a constant, we can fold the |
| 5107 | // comparison into the select arms, which will cause one to be |
| 5108 | // constant folded and the select turned into a bitwise or. |
| 5109 | Value *Op1 = 0, *Op2 = 0; |
| 5110 | if (LHSI->hasOneUse()) { |
| 5111 | if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) { |
| 5112 | // Fold the known value into the constant operand. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5113 | Op1 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC); |
| 5114 | // Insert a new ICmp of the other select operand. |
| 5115 | Op2 = InsertNewInstBefore(new ICmpInst(I.getPredicate(), |
| 5116 | LHSI->getOperand(2), RHSC, |
| 5117 | I.getName()), I); |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5118 | } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) { |
| 5119 | // Fold the known value into the constant operand. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5120 | Op2 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC); |
| 5121 | // Insert a new ICmp of the other select operand. |
| 5122 | Op1 = InsertNewInstBefore(new ICmpInst(I.getPredicate(), |
| 5123 | LHSI->getOperand(1), RHSC, |
| 5124 | I.getName()), I); |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5125 | } |
| 5126 | } |
Jeff Cohen | 9d80930 | 2005-04-23 21:38:35 +0000 | [diff] [blame] | 5127 | |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5128 | if (Op1) |
| 5129 | return new SelectInst(LHSI->getOperand(0), Op1, Op2); |
| 5130 | break; |
| 5131 | } |
| 5132 | } |
| 5133 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5134 | // If we can optimize a 'icmp GEP, P' or 'icmp P, GEP', do so now. |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5135 | if (User *GEP = dyn_castGetElementPtr(Op0)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5136 | if (Instruction *NI = FoldGEPICmp(GEP, Op1, I.getPredicate(), I)) |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5137 | return NI; |
| 5138 | if (User *GEP = dyn_castGetElementPtr(Op1)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5139 | if (Instruction *NI = FoldGEPICmp(GEP, Op0, |
| 5140 | ICmpInst::getSwappedPredicate(I.getPredicate()), I)) |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5141 | return NI; |
| 5142 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5143 | // Test to see if the operands of the icmp are casted versions of other |
Chris Lattner | 57d8637 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 5144 | // values. If the ptr->ptr cast can be stripped off both arguments, we do so |
| 5145 | // now. |
| 5146 | if (BitCastInst *CI = dyn_cast<BitCastInst>(Op0)) { |
| 5147 | if (isa<PointerType>(Op0->getType()) && |
| 5148 | (isa<Constant>(Op1) || isa<BitCastInst>(Op1))) { |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5149 | // We keep moving the cast from the left operand over to the right |
| 5150 | // operand, where it can often be eliminated completely. |
Chris Lattner | 57d8637 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 5151 | Op0 = CI->getOperand(0); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5152 | |
Chris Lattner | 57d8637 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 5153 | // If operand #1 is a bitcast instruction, it must also be a ptr->ptr cast |
| 5154 | // so eliminate it as well. |
| 5155 | if (BitCastInst *CI2 = dyn_cast<BitCastInst>(Op1)) |
| 5156 | Op1 = CI2->getOperand(0); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5157 | |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5158 | // If Op1 is a constant, we can fold the cast into the constant. |
Chris Lattner | 57d8637 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 5159 | if (Op0->getType() != Op1->getType()) |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5160 | if (Constant *Op1C = dyn_cast<Constant>(Op1)) { |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 5161 | Op1 = ConstantExpr::getBitCast(Op1C, Op0->getType()); |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5162 | } else { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5163 | // Otherwise, cast the RHS right before the icmp |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 5164 | Op1 = InsertCastBefore(Instruction::BitCast, Op1, Op0->getType(), I); |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5165 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5166 | return new ICmpInst(I.getPredicate(), Op0, Op1); |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5167 | } |
Chris Lattner | 57d8637 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 5168 | } |
| 5169 | |
| 5170 | if (isa<CastInst>(Op0)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5171 | // Handle the special case of: icmp (cast bool to X), <cst> |
Chris Lattner | 6870805 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 5172 | // This comes up when you have code like |
| 5173 | // int X = A < B; |
| 5174 | // if (X) ... |
| 5175 | // For generality, we handle any zero-extension of any operand comparison |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5176 | // with a constant or another cast from the same type. |
| 5177 | if (isa<ConstantInt>(Op1) || isa<CastInst>(Op1)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5178 | if (Instruction *R = visitICmpInstWithCastAndCast(I)) |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5179 | return R; |
Chris Lattner | 6870805 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 5180 | } |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5181 | |
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 5182 | if (I.isEquality()) { |
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5183 | Value *A, *B, *C, *D; |
| 5184 | if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) { |
| 5185 | if (A == Op1 || B == Op1) { // (A^B) == A -> B == 0 |
| 5186 | Value *OtherVal = A == Op1 ? B : A; |
| 5187 | return new ICmpInst(I.getPredicate(), OtherVal, |
| 5188 | Constant::getNullValue(A->getType())); |
| 5189 | } |
| 5190 | |
| 5191 | if (match(Op1, m_Xor(m_Value(C), m_Value(D)))) { |
| 5192 | // A^c1 == C^c2 --> A == C^(c1^c2) |
| 5193 | if (ConstantInt *C1 = dyn_cast<ConstantInt>(B)) |
| 5194 | if (ConstantInt *C2 = dyn_cast<ConstantInt>(D)) |
| 5195 | if (Op1->hasOneUse()) { |
| 5196 | Constant *NC = ConstantExpr::getXor(C1, C2); |
| 5197 | Instruction *Xor = BinaryOperator::createXor(C, NC, "tmp"); |
| 5198 | return new ICmpInst(I.getPredicate(), A, |
| 5199 | InsertNewInstBefore(Xor, I)); |
| 5200 | } |
| 5201 | |
| 5202 | // A^B == A^D -> B == D |
| 5203 | if (A == C) return new ICmpInst(I.getPredicate(), B, D); |
| 5204 | if (A == D) return new ICmpInst(I.getPredicate(), B, C); |
| 5205 | if (B == C) return new ICmpInst(I.getPredicate(), A, D); |
| 5206 | if (B == D) return new ICmpInst(I.getPredicate(), A, C); |
| 5207 | } |
| 5208 | } |
| 5209 | |
| 5210 | if (match(Op1, m_Xor(m_Value(A), m_Value(B))) && |
| 5211 | (A == Op0 || B == Op0)) { |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5212 | // A == (A^B) -> B == 0 |
| 5213 | Value *OtherVal = A == Op0 ? B : A; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5214 | return new ICmpInst(I.getPredicate(), OtherVal, |
| 5215 | Constant::getNullValue(A->getType())); |
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5216 | } |
| 5217 | if (match(Op0, m_Sub(m_Value(A), m_Value(B))) && A == Op1) { |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5218 | // (A-B) == A -> B == 0 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5219 | return new ICmpInst(I.getPredicate(), B, |
| 5220 | Constant::getNullValue(B->getType())); |
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5221 | } |
| 5222 | if (match(Op1, m_Sub(m_Value(A), m_Value(B))) && A == Op0) { |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5223 | // A == (A-B) -> B == 0 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5224 | return new ICmpInst(I.getPredicate(), B, |
| 5225 | Constant::getNullValue(B->getType())); |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5226 | } |
Chris Lattner | 9c2328e | 2006-11-14 06:06:06 +0000 | [diff] [blame] | 5227 | |
Chris Lattner | 9c2328e | 2006-11-14 06:06:06 +0000 | [diff] [blame] | 5228 | // (X&Z) == (Y&Z) -> (X^Y) & Z == 0 |
| 5229 | if (Op0->hasOneUse() && Op1->hasOneUse() && |
| 5230 | match(Op0, m_And(m_Value(A), m_Value(B))) && |
| 5231 | match(Op1, m_And(m_Value(C), m_Value(D)))) { |
| 5232 | Value *X = 0, *Y = 0, *Z = 0; |
| 5233 | |
| 5234 | if (A == C) { |
| 5235 | X = B; Y = D; Z = A; |
| 5236 | } else if (A == D) { |
| 5237 | X = B; Y = C; Z = A; |
| 5238 | } else if (B == C) { |
| 5239 | X = A; Y = D; Z = B; |
| 5240 | } else if (B == D) { |
| 5241 | X = A; Y = C; Z = B; |
| 5242 | } |
| 5243 | |
| 5244 | if (X) { // Build (X^Y) & Z |
| 5245 | Op1 = InsertNewInstBefore(BinaryOperator::createXor(X, Y, "tmp"), I); |
| 5246 | Op1 = InsertNewInstBefore(BinaryOperator::createAnd(Op1, Z, "tmp"), I); |
| 5247 | I.setOperand(0, Op1); |
| 5248 | I.setOperand(1, Constant::getNullValue(Op1->getType())); |
| 5249 | return &I; |
| 5250 | } |
| 5251 | } |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5252 | } |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 5253 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5254 | } |
| 5255 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5256 | // visitICmpInstWithCastAndCast - Handle icmp (cast x to y), (cast/cst). |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5257 | // We only handle extending casts so far. |
| 5258 | // |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5259 | Instruction *InstCombiner::visitICmpInstWithCastAndCast(ICmpInst &ICI) { |
| 5260 | const CastInst *LHSCI = cast<CastInst>(ICI.getOperand(0)); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 5261 | Value *LHSCIOp = LHSCI->getOperand(0); |
| 5262 | const Type *SrcTy = LHSCIOp->getType(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5263 | const Type *DestTy = LHSCI->getType(); |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5264 | Value *RHSCIOp; |
| 5265 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5266 | // We only handle extension cast instructions, so far. Enforce this. |
| 5267 | if (LHSCI->getOpcode() != Instruction::ZExt && |
| 5268 | LHSCI->getOpcode() != Instruction::SExt) |
Chris Lattner | b352fa5 | 2005-01-17 03:20:02 +0000 | [diff] [blame] | 5269 | return 0; |
| 5270 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5271 | bool isSignedExt = LHSCI->getOpcode() == Instruction::SExt; |
| 5272 | bool isSignedCmp = ICI.isSignedPredicate(); |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5273 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5274 | if (CastInst *CI = dyn_cast<CastInst>(ICI.getOperand(1))) { |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5275 | // Not an extension from the same type? |
| 5276 | RHSCIOp = CI->getOperand(0); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5277 | if (RHSCIOp->getType() != LHSCIOp->getType()) |
| 5278 | return 0; |
Chris Lattner | a5c5e77 | 2007-01-13 23:11:38 +0000 | [diff] [blame] | 5279 | |
| 5280 | // If the signedness of the two compares doesn't agree (i.e. one is a sext |
| 5281 | // and the other is a zext), then we can't handle this. |
| 5282 | if (CI->getOpcode() != LHSCI->getOpcode()) |
| 5283 | return 0; |
| 5284 | |
| 5285 | // Likewise, if the signedness of the [sz]exts and the compare don't match, |
| 5286 | // then we can't handle this. |
| 5287 | if (isSignedExt != isSignedCmp && !ICI.isEquality()) |
| 5288 | return 0; |
| 5289 | |
| 5290 | // Okay, just insert a compare of the reduced operands now! |
| 5291 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSCIOp); |
Reid Spencer | 6731d5c | 2004-11-28 21:31:15 +0000 | [diff] [blame] | 5292 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5293 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5294 | // If we aren't dealing with a constant on the RHS, exit early |
| 5295 | ConstantInt *CI = dyn_cast<ConstantInt>(ICI.getOperand(1)); |
| 5296 | if (!CI) |
| 5297 | return 0; |
| 5298 | |
| 5299 | // Compute the constant that would happen if we truncated to SrcTy then |
| 5300 | // reextended to DestTy. |
| 5301 | Constant *Res1 = ConstantExpr::getTrunc(CI, SrcTy); |
| 5302 | Constant *Res2 = ConstantExpr::getCast(LHSCI->getOpcode(), Res1, DestTy); |
| 5303 | |
| 5304 | // If the re-extended constant didn't change... |
| 5305 | if (Res2 == CI) { |
| 5306 | // Make sure that sign of the Cmp and the sign of the Cast are the same. |
| 5307 | // For example, we might have: |
| 5308 | // %A = sext short %X to uint |
| 5309 | // %B = icmp ugt uint %A, 1330 |
| 5310 | // It is incorrect to transform this into |
| 5311 | // %B = icmp ugt short %X, 1330 |
| 5312 | // because %A may have negative value. |
| 5313 | // |
| 5314 | // However, it is OK if SrcTy is bool (See cast-set.ll testcase) |
| 5315 | // OR operation is EQ/NE. |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 5316 | if (isSignedExt == isSignedCmp || SrcTy == Type::Int1Ty || ICI.isEquality()) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5317 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, Res1); |
| 5318 | else |
| 5319 | return 0; |
| 5320 | } |
| 5321 | |
| 5322 | // The re-extended constant changed so the constant cannot be represented |
| 5323 | // in the shorter type. Consequently, we cannot emit a simple comparison. |
| 5324 | |
| 5325 | // First, handle some easy cases. We know the result cannot be equal at this |
| 5326 | // point so handle the ICI.isEquality() cases |
| 5327 | if (ICI.getPredicate() == ICmpInst::ICMP_EQ) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5328 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5329 | if (ICI.getPredicate() == ICmpInst::ICMP_NE) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5330 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5331 | |
| 5332 | // Evaluate the comparison for LT (we invert for GT below). LE and GE cases |
| 5333 | // should have been folded away previously and not enter in here. |
| 5334 | Value *Result; |
| 5335 | if (isSignedCmp) { |
| 5336 | // We're performing a signed comparison. |
| 5337 | if (cast<ConstantInt>(CI)->getSExtValue() < 0) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5338 | Result = ConstantInt::getFalse(); // X < (small) --> false |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5339 | else |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5340 | Result = ConstantInt::getTrue(); // X < (large) --> true |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5341 | } else { |
| 5342 | // We're performing an unsigned comparison. |
| 5343 | if (isSignedExt) { |
| 5344 | // We're performing an unsigned comp with a sign extended value. |
| 5345 | // This is true if the input is >= 0. [aka >s -1] |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5346 | Constant *NegOne = ConstantInt::getAllOnesValue(SrcTy); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5347 | Result = InsertNewInstBefore(new ICmpInst(ICmpInst::ICMP_SGT, LHSCIOp, |
| 5348 | NegOne, ICI.getName()), ICI); |
| 5349 | } else { |
| 5350 | // Unsigned extend & unsigned compare -> always true. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5351 | Result = ConstantInt::getTrue(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5352 | } |
| 5353 | } |
| 5354 | |
| 5355 | // Finally, return the value computed. |
| 5356 | if (ICI.getPredicate() == ICmpInst::ICMP_ULT || |
| 5357 | ICI.getPredicate() == ICmpInst::ICMP_SLT) { |
| 5358 | return ReplaceInstUsesWith(ICI, Result); |
| 5359 | } else { |
| 5360 | assert((ICI.getPredicate()==ICmpInst::ICMP_UGT || |
| 5361 | ICI.getPredicate()==ICmpInst::ICMP_SGT) && |
| 5362 | "ICmp should be folded!"); |
| 5363 | if (Constant *CI = dyn_cast<Constant>(Result)) |
| 5364 | return ReplaceInstUsesWith(ICI, ConstantExpr::getNot(CI)); |
| 5365 | else |
| 5366 | return BinaryOperator::createNot(Result); |
| 5367 | } |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5368 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5369 | |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 5370 | Instruction *InstCombiner::visitShl(BinaryOperator &I) { |
| 5371 | return commonShiftTransforms(I); |
| 5372 | } |
| 5373 | |
| 5374 | Instruction *InstCombiner::visitLShr(BinaryOperator &I) { |
| 5375 | return commonShiftTransforms(I); |
| 5376 | } |
| 5377 | |
| 5378 | Instruction *InstCombiner::visitAShr(BinaryOperator &I) { |
| 5379 | return commonShiftTransforms(I); |
| 5380 | } |
| 5381 | |
| 5382 | Instruction *InstCombiner::commonShiftTransforms(BinaryOperator &I) { |
| 5383 | assert(I.getOperand(1)->getType() == I.getOperand(0)->getType()); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 5384 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5385 | |
| 5386 | // shl X, 0 == X and shr X, 0 == X |
| 5387 | // shl 0, X == 0 and shr 0, X == 0 |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 5388 | if (Op1 == Constant::getNullValue(Op1->getType()) || |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 5389 | Op0 == Constant::getNullValue(Op0->getType())) |
| 5390 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 8d6bbdb | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 5391 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5392 | if (isa<UndefValue>(Op0)) { |
| 5393 | if (I.getOpcode() == Instruction::AShr) // undef >>s X -> undef |
Chris Lattner | 79a564c | 2004-10-16 23:28:04 +0000 | [diff] [blame] | 5394 | return ReplaceInstUsesWith(I, Op0); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5395 | else // undef << X -> 0, undef >>u X -> 0 |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5396 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 5397 | } |
| 5398 | if (isa<UndefValue>(Op1)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5399 | if (I.getOpcode() == Instruction::AShr) // X >>s undef -> X |
| 5400 | return ReplaceInstUsesWith(I, Op0); |
| 5401 | else // X << undef, X >>u undef -> 0 |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5402 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5403 | } |
| 5404 | |
Chris Lattner | de2b660 | 2006-11-10 23:38:52 +0000 | [diff] [blame] | 5405 | // ashr int -1, X = -1 (for any arithmetic shift rights of ~0) |
| 5406 | if (I.getOpcode() == Instruction::AShr) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5407 | if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0)) |
Chris Lattner | de2b660 | 2006-11-10 23:38:52 +0000 | [diff] [blame] | 5408 | if (CSI->isAllOnesValue()) |
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 5409 | return ReplaceInstUsesWith(I, CSI); |
| 5410 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 5411 | // Try to fold constant and into select arguments. |
| 5412 | if (isa<Constant>(Op0)) |
| 5413 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 5414 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 5415 | return R; |
| 5416 | |
Chris Lattner | 120347e | 2005-05-08 17:34:56 +0000 | [diff] [blame] | 5417 | // See if we can turn a signed shr into an unsigned shr. |
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 5418 | if (I.isArithmeticShift()) { |
Chris Lattner | 3bedbd9 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 5419 | if (MaskedValueIsZero(Op0, |
| 5420 | 1ULL << (I.getType()->getPrimitiveSizeInBits()-1))) { |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 5421 | return BinaryOperator::createLShr(Op0, Op1, I.getName()); |
Chris Lattner | 120347e | 2005-05-08 17:34:56 +0000 | [diff] [blame] | 5422 | } |
| 5423 | } |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 5424 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5425 | if (ConstantInt *CUI = dyn_cast<ConstantInt>(Op1)) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 5426 | if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I)) |
| 5427 | return Res; |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5428 | return 0; |
| 5429 | } |
| 5430 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5431 | Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 5432 | BinaryOperator &I) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5433 | bool isLeftShift = I.getOpcode() == Instruction::Shl; |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5434 | |
Chris Lattner | 8d6bbdb | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 5435 | // See if we can simplify any instructions used by the instruction whose sole |
| 5436 | // purpose is to compute bits we don't care about. |
| 5437 | uint64_t KnownZero, KnownOne; |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 5438 | if (SimplifyDemandedBits(&I, cast<IntegerType>(I.getType())->getBitMask(), |
Chris Lattner | 8d6bbdb | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 5439 | KnownZero, KnownOne)) |
| 5440 | return &I; |
| 5441 | |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5442 | // shl uint X, 32 = 0 and shr ubyte Y, 9 = 0, ... just don't eliminate shr |
| 5443 | // of a signed value. |
| 5444 | // |
| 5445 | unsigned TypeBits = Op0->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5446 | if (Op1->getZExtValue() >= TypeBits) { |
Chris Lattner | 0737c24 | 2007-02-02 05:29:55 +0000 | [diff] [blame] | 5447 | if (I.getOpcode() != Instruction::AShr) |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5448 | return ReplaceInstUsesWith(I, Constant::getNullValue(Op0->getType())); |
| 5449 | else { |
Chris Lattner | 0737c24 | 2007-02-02 05:29:55 +0000 | [diff] [blame] | 5450 | I.setOperand(1, ConstantInt::get(I.getType(), TypeBits-1)); |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5451 | return &I; |
Chris Lattner | 8adac75 | 2004-02-23 20:30:06 +0000 | [diff] [blame] | 5452 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5453 | } |
| 5454 | |
| 5455 | // ((X*C1) << C2) == (X * (C1 << C2)) |
| 5456 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0)) |
| 5457 | if (BO->getOpcode() == Instruction::Mul && isLeftShift) |
| 5458 | if (Constant *BOOp = dyn_cast<Constant>(BO->getOperand(1))) |
| 5459 | return BinaryOperator::createMul(BO->getOperand(0), |
| 5460 | ConstantExpr::getShl(BOOp, Op1)); |
| 5461 | |
| 5462 | // Try to fold constant and into select arguments. |
| 5463 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 5464 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 5465 | return R; |
| 5466 | if (isa<PHINode>(Op0)) |
| 5467 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 5468 | return NV; |
| 5469 | |
| 5470 | if (Op0->hasOneUse()) { |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5471 | if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0)) { |
| 5472 | // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C) |
| 5473 | Value *V1, *V2; |
| 5474 | ConstantInt *CC; |
| 5475 | switch (Op0BO->getOpcode()) { |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 5476 | default: break; |
| 5477 | case Instruction::Add: |
| 5478 | case Instruction::And: |
| 5479 | case Instruction::Or: |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 5480 | case Instruction::Xor: { |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 5481 | // These operators commute. |
| 5482 | // Turn (Y + (X >> C)) << C -> (X + (Y << C)) & (~0 << C) |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5483 | if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() && |
| 5484 | match(Op0BO->getOperand(1), |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5485 | m_Shr(m_Value(V1), m_ConstantInt(CC))) && CC == Op1) { |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 5486 | Instruction *YS = BinaryOperator::createShl( |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5487 | Op0BO->getOperand(0), Op1, |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5488 | Op0BO->getName()); |
| 5489 | InsertNewInstBefore(YS, I); // (Y << C) |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 5490 | Instruction *X = |
| 5491 | BinaryOperator::create(Op0BO->getOpcode(), YS, V1, |
| 5492 | Op0BO->getOperand(1)->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5493 | InsertNewInstBefore(X, I); // (X + (Y << C)) |
| 5494 | Constant *C2 = ConstantInt::getAllOnesValue(X->getType()); |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5495 | C2 = ConstantExpr::getShl(C2, Op1); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5496 | return BinaryOperator::createAnd(X, C2); |
| 5497 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5498 | |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5499 | // Turn (Y + ((X >> C) & CC)) << C -> ((X & (CC << C)) + (Y << C)) |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 5500 | Value *Op0BOOp1 = Op0BO->getOperand(1); |
| 5501 | if (isLeftShift && Op0BOOp1->hasOneUse() && V2 == Op1 && |
| 5502 | match(Op0BOOp1, |
| 5503 | m_And(m_Shr(m_Value(V1), m_Value(V2)),m_ConstantInt(CC))) && |
| 5504 | cast<BinaryOperator>(Op0BOOp1)->getOperand(0)-> hasOneUse()) { |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 5505 | Instruction *YS = BinaryOperator::createShl( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 5506 | Op0BO->getOperand(0), Op1, |
| 5507 | Op0BO->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5508 | InsertNewInstBefore(YS, I); // (Y << C) |
| 5509 | Instruction *XM = |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5510 | BinaryOperator::createAnd(V1, ConstantExpr::getShl(CC, Op1), |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5511 | V1->getName()+".mask"); |
| 5512 | InsertNewInstBefore(XM, I); // X & (CC << C) |
| 5513 | |
| 5514 | return BinaryOperator::create(Op0BO->getOpcode(), YS, XM); |
| 5515 | } |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 5516 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5517 | |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 5518 | // FALL THROUGH. |
| 5519 | case Instruction::Sub: { |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 5520 | // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C) |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5521 | if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() && |
| 5522 | match(Op0BO->getOperand(0), |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5523 | m_Shr(m_Value(V1), m_ConstantInt(CC))) && CC == Op1) { |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 5524 | Instruction *YS = BinaryOperator::createShl( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 5525 | Op0BO->getOperand(1), Op1, |
| 5526 | Op0BO->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5527 | InsertNewInstBefore(YS, I); // (Y << C) |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 5528 | Instruction *X = |
Chris Lattner | 13d4ab4 | 2006-05-31 21:14:00 +0000 | [diff] [blame] | 5529 | BinaryOperator::create(Op0BO->getOpcode(), V1, YS, |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 5530 | Op0BO->getOperand(0)->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5531 | InsertNewInstBefore(X, I); // (X + (Y << C)) |
| 5532 | Constant *C2 = ConstantInt::getAllOnesValue(X->getType()); |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5533 | C2 = ConstantExpr::getShl(C2, Op1); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5534 | return BinaryOperator::createAnd(X, C2); |
| 5535 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5536 | |
Chris Lattner | 13d4ab4 | 2006-05-31 21:14:00 +0000 | [diff] [blame] | 5537 | // Turn (((X >> C)&CC) + Y) << C -> (X + (Y << C)) & (CC << C) |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5538 | if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() && |
| 5539 | match(Op0BO->getOperand(0), |
| 5540 | m_And(m_Shr(m_Value(V1), m_Value(V2)), |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5541 | m_ConstantInt(CC))) && V2 == Op1 && |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 5542 | cast<BinaryOperator>(Op0BO->getOperand(0)) |
| 5543 | ->getOperand(0)->hasOneUse()) { |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 5544 | Instruction *YS = BinaryOperator::createShl( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 5545 | Op0BO->getOperand(1), Op1, |
| 5546 | Op0BO->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5547 | InsertNewInstBefore(YS, I); // (Y << C) |
| 5548 | Instruction *XM = |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5549 | BinaryOperator::createAnd(V1, ConstantExpr::getShl(CC, Op1), |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5550 | V1->getName()+".mask"); |
| 5551 | InsertNewInstBefore(XM, I); // X & (CC << C) |
| 5552 | |
Chris Lattner | 13d4ab4 | 2006-05-31 21:14:00 +0000 | [diff] [blame] | 5553 | return BinaryOperator::create(Op0BO->getOpcode(), XM, YS); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5554 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5555 | |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 5556 | break; |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 5557 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5558 | } |
| 5559 | |
| 5560 | |
| 5561 | // If the operand is an bitwise operator with a constant RHS, and the |
| 5562 | // shift is the only use, we can pull it out of the shift. |
| 5563 | if (ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand(1))) { |
| 5564 | bool isValid = true; // Valid only for And, Or, Xor |
| 5565 | bool highBitSet = false; // Transform if high bit of constant set? |
| 5566 | |
| 5567 | switch (Op0BO->getOpcode()) { |
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 5568 | default: isValid = false; break; // Do not perform transform! |
Chris Lattner | 1f7e160 | 2004-10-08 03:46:20 +0000 | [diff] [blame] | 5569 | case Instruction::Add: |
| 5570 | isValid = isLeftShift; |
| 5571 | break; |
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 5572 | case Instruction::Or: |
| 5573 | case Instruction::Xor: |
| 5574 | highBitSet = false; |
| 5575 | break; |
| 5576 | case Instruction::And: |
| 5577 | highBitSet = true; |
| 5578 | break; |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5579 | } |
| 5580 | |
| 5581 | // If this is a signed shift right, and the high bit is modified |
| 5582 | // by the logical operation, do not perform the transformation. |
| 5583 | // The highBitSet boolean indicates the value of the high bit of |
| 5584 | // the constant which would cause it to be modified for this |
| 5585 | // operation. |
| 5586 | // |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5587 | if (isValid && !isLeftShift && I.getOpcode() == Instruction::AShr) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5588 | uint64_t Val = Op0C->getZExtValue(); |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5589 | isValid = ((Val & (1 << (TypeBits-1))) != 0) == highBitSet; |
| 5590 | } |
| 5591 | |
| 5592 | if (isValid) { |
| 5593 | Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, Op1); |
| 5594 | |
| 5595 | Instruction *NewShift = |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 5596 | BinaryOperator::create(I.getOpcode(), Op0BO->getOperand(0), Op1); |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5597 | InsertNewInstBefore(NewShift, I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 5598 | NewShift->takeName(Op0BO); |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5599 | |
| 5600 | return BinaryOperator::create(Op0BO->getOpcode(), NewShift, |
| 5601 | NewRHS); |
| 5602 | } |
| 5603 | } |
| 5604 | } |
| 5605 | } |
| 5606 | |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5607 | // Find out if this is a shift of a shift by a constant. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 5608 | BinaryOperator *ShiftOp = dyn_cast<BinaryOperator>(Op0); |
| 5609 | if (ShiftOp && !ShiftOp->isShift()) |
| 5610 | ShiftOp = 0; |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5611 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5612 | if (ShiftOp && isa<ConstantInt>(ShiftOp->getOperand(1))) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5613 | ConstantInt *ShiftAmt1C = cast<ConstantInt>(ShiftOp->getOperand(1)); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5614 | unsigned ShiftAmt1 = (unsigned)ShiftAmt1C->getZExtValue(); |
| 5615 | unsigned ShiftAmt2 = (unsigned)Op1->getZExtValue(); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5616 | assert(ShiftAmt2 != 0 && "Should have been simplified earlier"); |
| 5617 | if (ShiftAmt1 == 0) return 0; // Will be simplified in the future. |
| 5618 | Value *X = ShiftOp->getOperand(0); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5619 | |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5620 | unsigned AmtSum = ShiftAmt1+ShiftAmt2; // Fold into one big shift. |
| 5621 | if (AmtSum > I.getType()->getPrimitiveSizeInBits()) |
| 5622 | AmtSum = I.getType()->getPrimitiveSizeInBits(); |
| 5623 | |
| 5624 | const IntegerType *Ty = cast<IntegerType>(I.getType()); |
| 5625 | |
| 5626 | // Check for (X << c1) << c2 and (X >> c1) >> c2 |
Chris Lattner | 7f3da2d | 2007-02-03 23:28:07 +0000 | [diff] [blame] | 5627 | if (I.getOpcode() == ShiftOp->getOpcode()) { |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5628 | return BinaryOperator::create(I.getOpcode(), X, |
| 5629 | ConstantInt::get(Ty, AmtSum)); |
| 5630 | } else if (ShiftOp->getOpcode() == Instruction::LShr && |
| 5631 | I.getOpcode() == Instruction::AShr) { |
| 5632 | // ((X >>u C1) >>s C2) -> (X >>u (C1+C2)) since C1 != 0. |
| 5633 | return BinaryOperator::createLShr(X, ConstantInt::get(Ty, AmtSum)); |
| 5634 | } else if (ShiftOp->getOpcode() == Instruction::AShr && |
| 5635 | I.getOpcode() == Instruction::LShr) { |
| 5636 | // ((X >>s C1) >>u C2) -> ((X >>s (C1+C2)) & mask) since C1 != 0. |
| 5637 | Instruction *Shift = |
| 5638 | BinaryOperator::createAShr(X, ConstantInt::get(Ty, AmtSum)); |
| 5639 | InsertNewInstBefore(Shift, I); |
| 5640 | |
| 5641 | uint64_t Mask = Ty->getBitMask() >> ShiftAmt2; |
| 5642 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Ty, Mask)); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5643 | } |
| 5644 | |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5645 | // Okay, if we get here, one shift must be left, and the other shift must be |
| 5646 | // right. See if the amounts are equal. |
| 5647 | if (ShiftAmt1 == ShiftAmt2) { |
| 5648 | // If we have ((X >>? C) << C), turn this into X & (-1 << C). |
| 5649 | if (I.getOpcode() == Instruction::Shl) { |
Chris Lattner | 4f3ebab | 2007-02-05 04:09:35 +0000 | [diff] [blame] | 5650 | uint64_t Mask = Ty->getBitMask() << ShiftAmt1; |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5651 | return BinaryOperator::createAnd(X, ConstantInt::get(Ty, Mask)); |
| 5652 | } |
| 5653 | // If we have ((X << C) >>u C), turn this into X & (-1 >>u C). |
| 5654 | if (I.getOpcode() == Instruction::LShr) { |
Chris Lattner | 4f3ebab | 2007-02-05 04:09:35 +0000 | [diff] [blame] | 5655 | uint64_t Mask = Ty->getBitMask() >> ShiftAmt1; |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5656 | return BinaryOperator::createAnd(X, ConstantInt::get(Ty, Mask)); |
| 5657 | } |
| 5658 | // We can simplify ((X << C) >>s C) into a trunc + sext. |
| 5659 | // NOTE: we could do this for any C, but that would make 'unusual' integer |
| 5660 | // types. For now, just stick to ones well-supported by the code |
| 5661 | // generators. |
| 5662 | const Type *SExtType = 0; |
| 5663 | switch (Ty->getBitWidth() - ShiftAmt1) { |
| 5664 | case 8 : SExtType = Type::Int8Ty; break; |
| 5665 | case 16: SExtType = Type::Int16Ty; break; |
| 5666 | case 32: SExtType = Type::Int32Ty; break; |
| 5667 | default: break; |
| 5668 | } |
| 5669 | if (SExtType) { |
| 5670 | Instruction *NewTrunc = new TruncInst(X, SExtType, "sext"); |
| 5671 | InsertNewInstBefore(NewTrunc, I); |
| 5672 | return new SExtInst(NewTrunc, Ty); |
| 5673 | } |
| 5674 | // Otherwise, we can't handle it yet. |
| 5675 | } else if (ShiftAmt1 < ShiftAmt2) { |
| 5676 | unsigned ShiftDiff = ShiftAmt2-ShiftAmt1; |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5677 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 5678 | // (X >>? C1) << C2 --> X << (C2-C1) & (-1 << C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5679 | if (I.getOpcode() == Instruction::Shl) { |
| 5680 | assert(ShiftOp->getOpcode() == Instruction::LShr || |
| 5681 | ShiftOp->getOpcode() == Instruction::AShr); |
Chris Lattner | e8d56c5 | 2006-01-07 01:32:28 +0000 | [diff] [blame] | 5682 | Instruction *Shift = |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5683 | BinaryOperator::createShl(X, ConstantInt::get(Ty, ShiftDiff)); |
Chris Lattner | e8d56c5 | 2006-01-07 01:32:28 +0000 | [diff] [blame] | 5684 | InsertNewInstBefore(Shift, I); |
| 5685 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 5686 | uint64_t Mask = Ty->getBitMask() << ShiftAmt2; |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5687 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Ty, Mask)); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5688 | } |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5689 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 5690 | // (X << C1) >>u C2 --> X >>u (C2-C1) & (-1 >> C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5691 | if (I.getOpcode() == Instruction::LShr) { |
| 5692 | assert(ShiftOp->getOpcode() == Instruction::Shl); |
| 5693 | Instruction *Shift = |
| 5694 | BinaryOperator::createLShr(X, ConstantInt::get(Ty, ShiftDiff)); |
| 5695 | InsertNewInstBefore(Shift, I); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5696 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 5697 | uint64_t Mask = Ty->getBitMask() >> ShiftAmt2; |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5698 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Ty, Mask)); |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 5699 | } |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5700 | |
| 5701 | // We can't handle (X << C1) >>s C2, it shifts arbitrary bits in. |
| 5702 | } else { |
| 5703 | assert(ShiftAmt2 < ShiftAmt1); |
| 5704 | unsigned ShiftDiff = ShiftAmt1-ShiftAmt2; |
| 5705 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 5706 | // (X >>? C1) << C2 --> X >>? (C1-C2) & (-1 << C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5707 | if (I.getOpcode() == Instruction::Shl) { |
| 5708 | assert(ShiftOp->getOpcode() == Instruction::LShr || |
| 5709 | ShiftOp->getOpcode() == Instruction::AShr); |
| 5710 | Instruction *Shift = |
| 5711 | BinaryOperator::create(ShiftOp->getOpcode(), X, |
| 5712 | ConstantInt::get(Ty, ShiftDiff)); |
| 5713 | InsertNewInstBefore(Shift, I); |
| 5714 | |
| 5715 | uint64_t Mask = Ty->getBitMask() << ShiftAmt2; |
| 5716 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Ty, Mask)); |
| 5717 | } |
| 5718 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 5719 | // (X << C1) >>u C2 --> X << (C1-C2) & (-1 >> C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5720 | if (I.getOpcode() == Instruction::LShr) { |
| 5721 | assert(ShiftOp->getOpcode() == Instruction::Shl); |
| 5722 | Instruction *Shift = |
| 5723 | BinaryOperator::createShl(X, ConstantInt::get(Ty, ShiftDiff)); |
| 5724 | InsertNewInstBefore(Shift, I); |
| 5725 | |
| 5726 | uint64_t Mask = Ty->getBitMask() >> ShiftAmt2; |
| 5727 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Ty, Mask)); |
| 5728 | } |
| 5729 | |
| 5730 | // We can't handle (X << C1) >>a C2, it shifts arbitrary bits in. |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 5731 | } |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5732 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5733 | return 0; |
| 5734 | } |
| 5735 | |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 5736 | |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 5737 | /// DecomposeSimpleLinearExpr - Analyze 'Val', seeing if it is a simple linear |
| 5738 | /// expression. If so, decompose it, returning some value X, such that Val is |
| 5739 | /// X*Scale+Offset. |
| 5740 | /// |
| 5741 | static Value *DecomposeSimpleLinearExpr(Value *Val, unsigned &Scale, |
| 5742 | unsigned &Offset) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 5743 | assert(Val->getType() == Type::Int32Ty && "Unexpected allocation size type!"); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5744 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 5745 | Offset = CI->getZExtValue(); |
| 5746 | Scale = 1; |
| 5747 | return ConstantInt::get(Type::Int32Ty, 0); |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 5748 | } else if (Instruction *I = dyn_cast<Instruction>(Val)) { |
| 5749 | if (I->getNumOperands() == 2) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5750 | if (ConstantInt *CUI = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 5751 | if (I->getOpcode() == Instruction::Shl) { |
| 5752 | // This is a value scaled by '1 << the shift amt'. |
| 5753 | Scale = 1U << CUI->getZExtValue(); |
| 5754 | Offset = 0; |
| 5755 | return I->getOperand(0); |
| 5756 | } else if (I->getOpcode() == Instruction::Mul) { |
| 5757 | // This value is scaled by 'CUI'. |
| 5758 | Scale = CUI->getZExtValue(); |
| 5759 | Offset = 0; |
| 5760 | return I->getOperand(0); |
| 5761 | } else if (I->getOpcode() == Instruction::Add) { |
| 5762 | // We have X+C. Check to see if we really have (X*C2)+C1, |
| 5763 | // where C1 is divisible by C2. |
| 5764 | unsigned SubScale; |
| 5765 | Value *SubVal = |
| 5766 | DecomposeSimpleLinearExpr(I->getOperand(0), SubScale, Offset); |
| 5767 | Offset += CUI->getZExtValue(); |
| 5768 | if (SubScale > 1 && (Offset % SubScale == 0)) { |
| 5769 | Scale = SubScale; |
| 5770 | return SubVal; |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 5771 | } |
| 5772 | } |
| 5773 | } |
| 5774 | } |
| 5775 | } |
| 5776 | |
| 5777 | // Otherwise, we can't look past this. |
| 5778 | Scale = 1; |
| 5779 | Offset = 0; |
| 5780 | return Val; |
| 5781 | } |
| 5782 | |
| 5783 | |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 5784 | /// PromoteCastOfAllocation - If we find a cast of an allocation instruction, |
| 5785 | /// try to eliminate the cast by moving the type information into the alloc. |
| 5786 | Instruction *InstCombiner::PromoteCastOfAllocation(CastInst &CI, |
| 5787 | AllocationInst &AI) { |
| 5788 | const PointerType *PTy = dyn_cast<PointerType>(CI.getType()); |
Chris Lattner | 0ddac2a | 2005-10-27 05:53:56 +0000 | [diff] [blame] | 5789 | if (!PTy) return 0; // Not casting the allocation to a pointer type. |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 5790 | |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 5791 | // Remove any uses of AI that are dead. |
| 5792 | assert(!CI.use_empty() && "Dead instructions should be removed earlier!"); |
| 5793 | std::vector<Instruction*> DeadUsers; |
| 5794 | for (Value::use_iterator UI = AI.use_begin(), E = AI.use_end(); UI != E; ) { |
| 5795 | Instruction *User = cast<Instruction>(*UI++); |
| 5796 | if (isInstructionTriviallyDead(User)) { |
| 5797 | while (UI != E && *UI == User) |
| 5798 | ++UI; // If this instruction uses AI more than once, don't break UI. |
| 5799 | |
| 5800 | // Add operands to the worklist. |
| 5801 | AddUsesToWorkList(*User); |
| 5802 | ++NumDeadInst; |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 5803 | DOUT << "IC: DCE: " << *User; |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 5804 | |
| 5805 | User->eraseFromParent(); |
| 5806 | removeFromWorkList(User); |
| 5807 | } |
| 5808 | } |
| 5809 | |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 5810 | // Get the type really allocated and the type casted to. |
| 5811 | const Type *AllocElTy = AI.getAllocatedType(); |
| 5812 | const Type *CastElTy = PTy->getElementType(); |
| 5813 | if (!AllocElTy->isSized() || !CastElTy->isSized()) return 0; |
Chris Lattner | 18e78bb | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 5814 | |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame^] | 5815 | unsigned AllocElTyAlign = TD->getABITypeAlignment(AllocElTy); |
| 5816 | unsigned CastElTyAlign = TD->getABITypeAlignment(CastElTy); |
Chris Lattner | 18e78bb | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 5817 | if (CastElTyAlign < AllocElTyAlign) return 0; |
| 5818 | |
Chris Lattner | 39387a5 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 5819 | // If the allocation has multiple uses, only promote it if we are strictly |
| 5820 | // increasing the alignment of the resultant allocation. If we keep it the |
| 5821 | // same, we open the door to infinite loops of various kinds. |
| 5822 | if (!AI.hasOneUse() && CastElTyAlign == AllocElTyAlign) return 0; |
| 5823 | |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 5824 | uint64_t AllocElTySize = TD->getTypeSize(AllocElTy); |
| 5825 | uint64_t CastElTySize = TD->getTypeSize(CastElTy); |
Chris Lattner | 0ddac2a | 2005-10-27 05:53:56 +0000 | [diff] [blame] | 5826 | if (CastElTySize == 0 || AllocElTySize == 0) return 0; |
Chris Lattner | 18e78bb | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 5827 | |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 5828 | // See if we can satisfy the modulus by pulling a scale out of the array |
| 5829 | // size argument. |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 5830 | unsigned ArraySizeScale, ArrayOffset; |
| 5831 | Value *NumElements = // See if the array size is a decomposable linear expr. |
| 5832 | DecomposeSimpleLinearExpr(AI.getOperand(0), ArraySizeScale, ArrayOffset); |
| 5833 | |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 5834 | // If we can now satisfy the modulus, by using a non-1 scale, we really can |
| 5835 | // do the xform. |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 5836 | if ((AllocElTySize*ArraySizeScale) % CastElTySize != 0 || |
| 5837 | (AllocElTySize*ArrayOffset ) % CastElTySize != 0) return 0; |
Chris Lattner | 8142b0a | 2005-10-27 06:12:00 +0000 | [diff] [blame] | 5838 | |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 5839 | unsigned Scale = (AllocElTySize*ArraySizeScale)/CastElTySize; |
| 5840 | Value *Amt = 0; |
| 5841 | if (Scale == 1) { |
| 5842 | Amt = NumElements; |
| 5843 | } else { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5844 | // If the allocation size is constant, form a constant mul expression |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 5845 | Amt = ConstantInt::get(Type::Int32Ty, Scale); |
| 5846 | if (isa<ConstantInt>(NumElements)) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5847 | Amt = ConstantExpr::getMul( |
| 5848 | cast<ConstantInt>(NumElements), cast<ConstantInt>(Amt)); |
| 5849 | // otherwise multiply the amount and the number of elements |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 5850 | else if (Scale != 1) { |
| 5851 | Instruction *Tmp = BinaryOperator::createMul(Amt, NumElements, "tmp"); |
| 5852 | Amt = InsertNewInstBefore(Tmp, AI); |
Chris Lattner | 8142b0a | 2005-10-27 06:12:00 +0000 | [diff] [blame] | 5853 | } |
Chris Lattner | 0ddac2a | 2005-10-27 05:53:56 +0000 | [diff] [blame] | 5854 | } |
| 5855 | |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 5856 | if (unsigned Offset = (AllocElTySize*ArrayOffset)/CastElTySize) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 5857 | Value *Off = ConstantInt::get(Type::Int32Ty, Offset); |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 5858 | Instruction *Tmp = BinaryOperator::createAdd(Amt, Off, "tmp"); |
| 5859 | Amt = InsertNewInstBefore(Tmp, AI); |
| 5860 | } |
| 5861 | |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 5862 | AllocationInst *New; |
| 5863 | if (isa<MallocInst>(AI)) |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 5864 | New = new MallocInst(CastElTy, Amt, AI.getAlignment()); |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 5865 | else |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 5866 | New = new AllocaInst(CastElTy, Amt, AI.getAlignment()); |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 5867 | InsertNewInstBefore(New, AI); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 5868 | New->takeName(&AI); |
Chris Lattner | 39387a5 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 5869 | |
| 5870 | // If the allocation has multiple uses, insert a cast and change all things |
| 5871 | // that used it to use the new cast. This will also hack on CI, but it will |
| 5872 | // die soon. |
| 5873 | if (!AI.hasOneUse()) { |
| 5874 | AddUsesToWorkList(AI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 5875 | // New is the allocation instruction, pointer typed. AI is the original |
| 5876 | // allocation instruction, also pointer typed. Thus, cast to use is BitCast. |
| 5877 | CastInst *NewCast = new BitCastInst(New, AI.getType(), "tmpcast"); |
Chris Lattner | 39387a5 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 5878 | InsertNewInstBefore(NewCast, AI); |
| 5879 | AI.replaceAllUsesWith(NewCast); |
| 5880 | } |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 5881 | return ReplaceInstUsesWith(CI, New); |
| 5882 | } |
| 5883 | |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 5884 | /// CanEvaluateInDifferentType - Return true if we can take the specified value |
| 5885 | /// and return it without inserting any new casts. This is used by code that |
| 5886 | /// tries to decide whether promoting or shrinking integer operations to wider |
| 5887 | /// or smaller types will allow us to eliminate a truncate or extend. |
| 5888 | static bool CanEvaluateInDifferentType(Value *V, const Type *Ty, |
| 5889 | int &NumCastsRemoved) { |
| 5890 | if (isa<Constant>(V)) return true; |
| 5891 | |
| 5892 | Instruction *I = dyn_cast<Instruction>(V); |
| 5893 | if (!I || !I->hasOneUse()) return false; |
| 5894 | |
| 5895 | switch (I->getOpcode()) { |
| 5896 | case Instruction::And: |
| 5897 | case Instruction::Or: |
| 5898 | case Instruction::Xor: |
| 5899 | // These operators can all arbitrarily be extended or truncated. |
| 5900 | return CanEvaluateInDifferentType(I->getOperand(0), Ty, NumCastsRemoved) && |
| 5901 | CanEvaluateInDifferentType(I->getOperand(1), Ty, NumCastsRemoved); |
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 5902 | case Instruction::AShr: |
| 5903 | case Instruction::LShr: |
| 5904 | case Instruction::Shl: |
| 5905 | // If this is just a bitcast changing the sign of the operation, we can |
| 5906 | // convert if the operand can be converted. |
| 5907 | if (V->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits()) |
| 5908 | return CanEvaluateInDifferentType(I->getOperand(0), Ty, NumCastsRemoved); |
| 5909 | break; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 5910 | case Instruction::Trunc: |
| 5911 | case Instruction::ZExt: |
| 5912 | case Instruction::SExt: |
| 5913 | case Instruction::BitCast: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 5914 | // If this is a cast from the destination type, we can trivially eliminate |
| 5915 | // it, and this will remove a cast overall. |
| 5916 | if (I->getOperand(0)->getType() == Ty) { |
Chris Lattner | d228018 | 2006-06-28 17:34:50 +0000 | [diff] [blame] | 5917 | // If the first operand is itself a cast, and is eliminable, do not count |
| 5918 | // this as an eliminable cast. We would prefer to eliminate those two |
| 5919 | // casts first. |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 5920 | if (isa<CastInst>(I->getOperand(0))) |
Chris Lattner | d228018 | 2006-06-28 17:34:50 +0000 | [diff] [blame] | 5921 | return true; |
| 5922 | |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 5923 | ++NumCastsRemoved; |
| 5924 | return true; |
| 5925 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 5926 | break; |
| 5927 | default: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 5928 | // TODO: Can handle more cases here. |
| 5929 | break; |
| 5930 | } |
| 5931 | |
| 5932 | return false; |
| 5933 | } |
| 5934 | |
| 5935 | /// EvaluateInDifferentType - Given an expression that |
| 5936 | /// CanEvaluateInDifferentType returns true for, actually insert the code to |
| 5937 | /// evaluate the expression. |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 5938 | Value *InstCombiner::EvaluateInDifferentType(Value *V, const Type *Ty, |
| 5939 | bool isSigned ) { |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 5940 | if (Constant *C = dyn_cast<Constant>(V)) |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 5941 | return ConstantExpr::getIntegerCast(C, Ty, isSigned /*Sext or ZExt*/); |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 5942 | |
| 5943 | // Otherwise, it must be an instruction. |
| 5944 | Instruction *I = cast<Instruction>(V); |
Chris Lattner | 01859e8 | 2006-05-20 23:14:03 +0000 | [diff] [blame] | 5945 | Instruction *Res = 0; |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 5946 | switch (I->getOpcode()) { |
| 5947 | case Instruction::And: |
| 5948 | case Instruction::Or: |
| 5949 | case Instruction::Xor: { |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 5950 | Value *LHS = EvaluateInDifferentType(I->getOperand(0), Ty, isSigned); |
| 5951 | Value *RHS = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned); |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 5952 | Res = BinaryOperator::create((Instruction::BinaryOps)I->getOpcode(), |
| 5953 | LHS, RHS, I->getName()); |
| 5954 | break; |
| 5955 | } |
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 5956 | case Instruction::AShr: |
| 5957 | case Instruction::LShr: |
| 5958 | case Instruction::Shl: { |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 5959 | Value *LHS = EvaluateInDifferentType(I->getOperand(0), Ty, isSigned); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 5960 | Res = BinaryOperator::create(Instruction::BinaryOps(I->getOpcode()), LHS, |
| 5961 | I->getOperand(1), I->getName()); |
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 5962 | break; |
| 5963 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 5964 | case Instruction::Trunc: |
| 5965 | case Instruction::ZExt: |
| 5966 | case Instruction::SExt: |
| 5967 | case Instruction::BitCast: |
| 5968 | // If the source type of the cast is the type we're trying for then we can |
| 5969 | // just return the source. There's no need to insert it because its not new. |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 5970 | if (I->getOperand(0)->getType() == Ty) |
| 5971 | return I->getOperand(0); |
| 5972 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 5973 | // Some other kind of cast, which shouldn't happen, so just .. |
| 5974 | // FALL THROUGH |
| 5975 | default: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 5976 | // TODO: Can handle more cases here. |
| 5977 | assert(0 && "Unreachable!"); |
| 5978 | break; |
| 5979 | } |
| 5980 | |
| 5981 | return InsertNewInstBefore(Res, *I); |
| 5982 | } |
| 5983 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 5984 | /// @brief Implement the transforms common to all CastInst visitors. |
| 5985 | Instruction *InstCombiner::commonCastTransforms(CastInst &CI) { |
Chris Lattner | 79d35b3 | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 5986 | Value *Src = CI.getOperand(0); |
| 5987 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 5988 | // Casting undef to anything results in undef so might as just replace it and |
| 5989 | // get rid of the cast. |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5990 | if (isa<UndefValue>(Src)) // cast undef -> undef |
| 5991 | return ReplaceInstUsesWith(CI, UndefValue::get(CI.getType())); |
| 5992 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 5993 | // Many cases of "cast of a cast" are eliminable. If its eliminable we just |
| 5994 | // eliminate it now. |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 5995 | if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 5996 | if (Instruction::CastOps opc = |
| 5997 | isEliminableCastPair(CSrc, CI.getOpcode(), CI.getType(), TD)) { |
| 5998 | // The first cast (CSrc) is eliminable so we need to fix up or replace |
| 5999 | // the second cast (CI). CSrc will then have a good chance of being dead. |
| 6000 | return CastInst::create(opc, CSrc->getOperand(0), CI.getType()); |
Chris Lattner | 8fd217c | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 6001 | } |
| 6002 | } |
Chris Lattner | a710ddc | 2004-05-25 04:29:21 +0000 | [diff] [blame] | 6003 | |
Chris Lattner | 797249b | 2003-06-21 23:12:02 +0000 | [diff] [blame] | 6004 | // If casting the result of a getelementptr instruction with no offset, turn |
| 6005 | // this into a cast of the original pointer! |
| 6006 | // |
Chris Lattner | 79d35b3 | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 6007 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Src)) { |
Chris Lattner | 797249b | 2003-06-21 23:12:02 +0000 | [diff] [blame] | 6008 | bool AllZeroOperands = true; |
| 6009 | for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i) |
| 6010 | if (!isa<Constant>(GEP->getOperand(i)) || |
| 6011 | !cast<Constant>(GEP->getOperand(i))->isNullValue()) { |
| 6012 | AllZeroOperands = false; |
| 6013 | break; |
| 6014 | } |
| 6015 | if (AllZeroOperands) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6016 | // Changing the cast operand is usually not a good idea but it is safe |
| 6017 | // here because the pointer operand is being replaced with another |
| 6018 | // pointer operand so the opcode doesn't need to change. |
Chris Lattner | 797249b | 2003-06-21 23:12:02 +0000 | [diff] [blame] | 6019 | CI.setOperand(0, GEP->getOperand(0)); |
| 6020 | return &CI; |
| 6021 | } |
| 6022 | } |
Chris Lattner | 13c654a | 2006-11-21 17:05:13 +0000 | [diff] [blame] | 6023 | |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 6024 | // If we are casting a malloc or alloca to a pointer to a type of the same |
| 6025 | // size, rewrite the allocation instruction to allocate the "right" type. |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 6026 | if (AllocationInst *AI = dyn_cast<AllocationInst>(Src)) |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6027 | if (Instruction *V = PromoteCastOfAllocation(CI, *AI)) |
| 6028 | return V; |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 6029 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6030 | // If we are casting a select then fold the cast into the select |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 6031 | if (SelectInst *SI = dyn_cast<SelectInst>(Src)) |
| 6032 | if (Instruction *NV = FoldOpIntoSelect(CI, SI, this)) |
| 6033 | return NV; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6034 | |
| 6035 | // If we are casting a PHI then fold the cast into the PHI |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 6036 | if (isa<PHINode>(Src)) |
| 6037 | if (Instruction *NV = FoldOpIntoPhi(CI)) |
| 6038 | return NV; |
Chris Lattner | 9fb9213 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 6039 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6040 | return 0; |
| 6041 | } |
| 6042 | |
| 6043 | /// Only the TRUNC, ZEXT, SEXT, and BITCONVERT can have both operands as |
| 6044 | /// integers. This function implements the common transforms for all those |
| 6045 | /// cases. |
| 6046 | /// @brief Implement the transforms common to CastInst with integer operands |
| 6047 | Instruction *InstCombiner::commonIntCastTransforms(CastInst &CI) { |
| 6048 | if (Instruction *Result = commonCastTransforms(CI)) |
| 6049 | return Result; |
| 6050 | |
| 6051 | Value *Src = CI.getOperand(0); |
| 6052 | const Type *SrcTy = Src->getType(); |
| 6053 | const Type *DestTy = CI.getType(); |
| 6054 | unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits(); |
| 6055 | unsigned DestBitSize = DestTy->getPrimitiveSizeInBits(); |
| 6056 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6057 | // See if we can simplify any instructions used by the LHS whose sole |
| 6058 | // purpose is to compute bits we don't care about. |
| 6059 | uint64_t KnownZero = 0, KnownOne = 0; |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 6060 | if (SimplifyDemandedBits(&CI, cast<IntegerType>(DestTy)->getBitMask(), |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6061 | KnownZero, KnownOne)) |
| 6062 | return &CI; |
| 6063 | |
| 6064 | // If the source isn't an instruction or has more than one use then we |
| 6065 | // can't do anything more. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6066 | Instruction *SrcI = dyn_cast<Instruction>(Src); |
| 6067 | if (!SrcI || !Src->hasOneUse()) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6068 | return 0; |
| 6069 | |
| 6070 | // Attempt to propagate the cast into the instruction. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6071 | int NumCastsRemoved = 0; |
| 6072 | if (CanEvaluateInDifferentType(SrcI, DestTy, NumCastsRemoved)) { |
| 6073 | // If this cast is a truncate, evaluting in a different type always |
| 6074 | // eliminates the cast, so it is always a win. If this is a noop-cast |
| 6075 | // this just removes a noop cast which isn't pointful, but simplifies |
| 6076 | // the code. If this is a zero-extension, we need to do an AND to |
| 6077 | // maintain the clear top-part of the computation, so we require that |
| 6078 | // the input have eliminated at least one cast. If this is a sign |
| 6079 | // extension, we insert two new casts (to do the extension) so we |
| 6080 | // require that two casts have been eliminated. |
| 6081 | bool DoXForm = CI.isNoopCast(TD->getIntPtrType()); |
| 6082 | if (!DoXForm) { |
| 6083 | switch (CI.getOpcode()) { |
| 6084 | case Instruction::Trunc: |
| 6085 | DoXForm = true; |
| 6086 | break; |
| 6087 | case Instruction::ZExt: |
| 6088 | DoXForm = NumCastsRemoved >= 1; |
| 6089 | break; |
| 6090 | case Instruction::SExt: |
| 6091 | DoXForm = NumCastsRemoved >= 2; |
| 6092 | break; |
| 6093 | case Instruction::BitCast: |
| 6094 | DoXForm = false; |
| 6095 | break; |
| 6096 | default: |
| 6097 | // All the others use floating point so we shouldn't actually |
| 6098 | // get here because of the check above. |
| 6099 | assert(!"Unknown cast type .. unreachable"); |
| 6100 | break; |
| 6101 | } |
| 6102 | } |
| 6103 | |
| 6104 | if (DoXForm) { |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 6105 | Value *Res = EvaluateInDifferentType(SrcI, DestTy, |
| 6106 | CI.getOpcode() == Instruction::SExt); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6107 | assert(Res->getType() == DestTy); |
| 6108 | switch (CI.getOpcode()) { |
| 6109 | default: assert(0 && "Unknown cast type!"); |
| 6110 | case Instruction::Trunc: |
| 6111 | case Instruction::BitCast: |
| 6112 | // Just replace this cast with the result. |
| 6113 | return ReplaceInstUsesWith(CI, Res); |
| 6114 | case Instruction::ZExt: { |
| 6115 | // We need to emit an AND to clear the high bits. |
| 6116 | assert(SrcBitSize < DestBitSize && "Not a zext?"); |
| 6117 | Constant *C = |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6118 | ConstantInt::get(Type::Int64Ty, (1ULL << SrcBitSize)-1); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6119 | if (DestBitSize < 64) |
| 6120 | C = ConstantExpr::getTrunc(C, DestTy); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6121 | return BinaryOperator::createAnd(Res, C); |
| 6122 | } |
| 6123 | case Instruction::SExt: |
| 6124 | // We need to emit a cast to truncate, then a cast to sext. |
| 6125 | return CastInst::create(Instruction::SExt, |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 6126 | InsertCastBefore(Instruction::Trunc, Res, Src->getType(), |
| 6127 | CI), DestTy); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6128 | } |
| 6129 | } |
| 6130 | } |
| 6131 | |
| 6132 | Value *Op0 = SrcI->getNumOperands() > 0 ? SrcI->getOperand(0) : 0; |
| 6133 | Value *Op1 = SrcI->getNumOperands() > 1 ? SrcI->getOperand(1) : 0; |
| 6134 | |
| 6135 | switch (SrcI->getOpcode()) { |
| 6136 | case Instruction::Add: |
| 6137 | case Instruction::Mul: |
| 6138 | case Instruction::And: |
| 6139 | case Instruction::Or: |
| 6140 | case Instruction::Xor: |
| 6141 | // If we are discarding information, or just changing the sign, |
| 6142 | // rewrite. |
| 6143 | if (DestBitSize <= SrcBitSize && DestBitSize != 1) { |
| 6144 | // Don't insert two casts if they cannot be eliminated. We allow |
| 6145 | // two casts to be inserted if the sizes are the same. This could |
| 6146 | // only be converting signedness, which is a noop. |
| 6147 | if (DestBitSize == SrcBitSize || |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6148 | !ValueRequiresCast(CI.getOpcode(), Op1, DestTy,TD) || |
| 6149 | !ValueRequiresCast(CI.getOpcode(), Op0, DestTy, TD)) { |
Reid Spencer | 7eb7638 | 2006-12-13 17:19:09 +0000 | [diff] [blame] | 6150 | Instruction::CastOps opcode = CI.getOpcode(); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 6151 | Value *Op0c = InsertOperandCastBefore(opcode, Op0, DestTy, SrcI); |
| 6152 | Value *Op1c = InsertOperandCastBefore(opcode, Op1, DestTy, SrcI); |
| 6153 | return BinaryOperator::create( |
| 6154 | cast<BinaryOperator>(SrcI)->getOpcode(), Op0c, Op1c); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6155 | } |
| 6156 | } |
| 6157 | |
| 6158 | // cast (xor bool X, true) to int --> xor (cast bool X to int), 1 |
| 6159 | if (isa<ZExtInst>(CI) && SrcBitSize == 1 && |
| 6160 | SrcI->getOpcode() == Instruction::Xor && |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6161 | Op1 == ConstantInt::getTrue() && |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6162 | (!Op0->hasOneUse() || !isa<CmpInst>(Op0))) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 6163 | Value *New = InsertOperandCastBefore(Instruction::ZExt, Op0, DestTy, &CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6164 | return BinaryOperator::createXor(New, ConstantInt::get(CI.getType(), 1)); |
| 6165 | } |
| 6166 | break; |
| 6167 | case Instruction::SDiv: |
| 6168 | case Instruction::UDiv: |
| 6169 | case Instruction::SRem: |
| 6170 | case Instruction::URem: |
| 6171 | // If we are just changing the sign, rewrite. |
| 6172 | if (DestBitSize == SrcBitSize) { |
| 6173 | // Don't insert two casts if they cannot be eliminated. We allow |
| 6174 | // two casts to be inserted if the sizes are the same. This could |
| 6175 | // only be converting signedness, which is a noop. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6176 | if (!ValueRequiresCast(CI.getOpcode(), Op1, DestTy, TD) || |
| 6177 | !ValueRequiresCast(CI.getOpcode(), Op0, DestTy, TD)) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 6178 | Value *Op0c = InsertOperandCastBefore(Instruction::BitCast, |
| 6179 | Op0, DestTy, SrcI); |
| 6180 | Value *Op1c = InsertOperandCastBefore(Instruction::BitCast, |
| 6181 | Op1, DestTy, SrcI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6182 | return BinaryOperator::create( |
| 6183 | cast<BinaryOperator>(SrcI)->getOpcode(), Op0c, Op1c); |
| 6184 | } |
| 6185 | } |
| 6186 | break; |
| 6187 | |
| 6188 | case Instruction::Shl: |
| 6189 | // Allow changing the sign of the source operand. Do not allow |
| 6190 | // changing the size of the shift, UNLESS the shift amount is a |
| 6191 | // constant. We must not change variable sized shifts to a smaller |
| 6192 | // size, because it is undefined to shift more bits out than exist |
| 6193 | // in the value. |
| 6194 | if (DestBitSize == SrcBitSize || |
| 6195 | (DestBitSize < SrcBitSize && isa<Constant>(Op1))) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 6196 | Instruction::CastOps opcode = (DestBitSize == SrcBitSize ? |
| 6197 | Instruction::BitCast : Instruction::Trunc); |
| 6198 | Value *Op0c = InsertOperandCastBefore(opcode, Op0, DestTy, SrcI); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6199 | Value *Op1c = InsertOperandCastBefore(opcode, Op1, DestTy, SrcI); |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 6200 | return BinaryOperator::createShl(Op0c, Op1c); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6201 | } |
| 6202 | break; |
| 6203 | case Instruction::AShr: |
| 6204 | // If this is a signed shr, and if all bits shifted in are about to be |
| 6205 | // truncated off, turn it into an unsigned shr to allow greater |
| 6206 | // simplifications. |
| 6207 | if (DestBitSize < SrcBitSize && |
| 6208 | isa<ConstantInt>(Op1)) { |
| 6209 | unsigned ShiftAmt = cast<ConstantInt>(Op1)->getZExtValue(); |
| 6210 | if (SrcBitSize > ShiftAmt && SrcBitSize-ShiftAmt >= DestBitSize) { |
| 6211 | // Insert the new logical shift right. |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 6212 | return BinaryOperator::createLShr(Op0, Op1); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6213 | } |
| 6214 | } |
| 6215 | break; |
| 6216 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6217 | case Instruction::ICmp: |
| 6218 | // If we are just checking for a icmp eq of a single bit and casting it |
| 6219 | // to an integer, then shift the bit to the appropriate place and then |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6220 | // cast to integer to avoid the comparison. |
| 6221 | if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) { |
| 6222 | uint64_t Op1CV = Op1C->getZExtValue(); |
| 6223 | // cast (X == 0) to int --> X^1 iff X has only the low bit set. |
| 6224 | // cast (X == 0) to int --> (X>>1)^1 iff X has only the 2nd bit set. |
| 6225 | // cast (X == 1) to int --> X iff X has only the low bit set. |
| 6226 | // cast (X == 2) to int --> X>>1 iff X has only the 2nd bit set. |
| 6227 | // cast (X != 0) to int --> X iff X has only the low bit set. |
| 6228 | // cast (X != 0) to int --> X>>1 iff X has only the 2nd bit set. |
| 6229 | // cast (X != 1) to int --> X^1 iff X has only the low bit set. |
| 6230 | // cast (X != 2) to int --> (X>>1)^1 iff X has only the 2nd bit set. |
| 6231 | if (Op1CV == 0 || isPowerOf2_64(Op1CV)) { |
| 6232 | // If Op1C some other power of two, convert: |
| 6233 | uint64_t KnownZero, KnownOne; |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 6234 | uint64_t TypeMask = Op1C->getType()->getBitMask(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6235 | ComputeMaskedBits(Op0, TypeMask, KnownZero, KnownOne); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6236 | |
| 6237 | // This only works for EQ and NE |
| 6238 | ICmpInst::Predicate pred = cast<ICmpInst>(SrcI)->getPredicate(); |
| 6239 | if (pred != ICmpInst::ICMP_NE && pred != ICmpInst::ICMP_EQ) |
| 6240 | break; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6241 | |
| 6242 | if (isPowerOf2_64(KnownZero^TypeMask)) { // Exactly 1 possible 1? |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6243 | bool isNE = pred == ICmpInst::ICMP_NE; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6244 | if (Op1CV && (Op1CV != (KnownZero^TypeMask))) { |
| 6245 | // (X&4) == 2 --> false |
| 6246 | // (X&4) != 2 --> true |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 6247 | Constant *Res = ConstantInt::get(Type::Int1Ty, isNE); |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 6248 | Res = ConstantExpr::getZExt(Res, CI.getType()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6249 | return ReplaceInstUsesWith(CI, Res); |
| 6250 | } |
| 6251 | |
| 6252 | unsigned ShiftAmt = Log2_64(KnownZero^TypeMask); |
| 6253 | Value *In = Op0; |
| 6254 | if (ShiftAmt) { |
| 6255 | // Perform a logical shr by shiftamt. |
| 6256 | // Insert the shift to put the result in the low bit. |
| 6257 | In = InsertNewInstBefore( |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 6258 | BinaryOperator::createLShr(In, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6259 | ConstantInt::get(In->getType(), ShiftAmt), |
| 6260 | In->getName()+".lobit"), CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6261 | } |
| 6262 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6263 | if ((Op1CV != 0) == isNE) { // Toggle the low bit. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6264 | Constant *One = ConstantInt::get(In->getType(), 1); |
| 6265 | In = BinaryOperator::createXor(In, One, "tmp"); |
| 6266 | InsertNewInstBefore(cast<Instruction>(In), CI); |
| 6267 | } |
| 6268 | |
| 6269 | if (CI.getType() == In->getType()) |
| 6270 | return ReplaceInstUsesWith(CI, In); |
| 6271 | else |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 6272 | return CastInst::createIntegerCast(In, CI.getType(), false/*ZExt*/); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6273 | } |
| 6274 | } |
| 6275 | } |
| 6276 | break; |
| 6277 | } |
| 6278 | return 0; |
| 6279 | } |
| 6280 | |
| 6281 | Instruction *InstCombiner::visitTrunc(CastInst &CI) { |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 6282 | if (Instruction *Result = commonIntCastTransforms(CI)) |
| 6283 | return Result; |
| 6284 | |
| 6285 | Value *Src = CI.getOperand(0); |
| 6286 | const Type *Ty = CI.getType(); |
| 6287 | unsigned DestBitWidth = Ty->getPrimitiveSizeInBits(); |
| 6288 | |
| 6289 | if (Instruction *SrcI = dyn_cast<Instruction>(Src)) { |
| 6290 | switch (SrcI->getOpcode()) { |
| 6291 | default: break; |
| 6292 | case Instruction::LShr: |
| 6293 | // We can shrink lshr to something smaller if we know the bits shifted in |
| 6294 | // are already zeros. |
| 6295 | if (ConstantInt *ShAmtV = dyn_cast<ConstantInt>(SrcI->getOperand(1))) { |
| 6296 | unsigned ShAmt = ShAmtV->getZExtValue(); |
| 6297 | |
| 6298 | // Get a mask for the bits shifting in. |
| 6299 | uint64_t Mask = (~0ULL >> (64-ShAmt)) << DestBitWidth; |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 6300 | Value* SrcIOp0 = SrcI->getOperand(0); |
| 6301 | if (SrcI->hasOneUse() && MaskedValueIsZero(SrcIOp0, Mask)) { |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 6302 | if (ShAmt >= DestBitWidth) // All zeros. |
| 6303 | return ReplaceInstUsesWith(CI, Constant::getNullValue(Ty)); |
| 6304 | |
| 6305 | // Okay, we can shrink this. Truncate the input, then return a new |
| 6306 | // shift. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6307 | Value *V1 = InsertCastBefore(Instruction::Trunc, SrcIOp0, Ty, CI); |
| 6308 | Value *V2 = InsertCastBefore(Instruction::Trunc, SrcI->getOperand(1), |
| 6309 | Ty, CI); |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 6310 | return BinaryOperator::createLShr(V1, V2); |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 6311 | } |
Chris Lattner | e13ab2a | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 6312 | } else { // This is a variable shr. |
| 6313 | |
| 6314 | // Turn 'trunc (lshr X, Y) to bool' into '(X & (1 << Y)) != 0'. This is |
| 6315 | // more LLVM instructions, but allows '1 << Y' to be hoisted if |
| 6316 | // loop-invariant and CSE'd. |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 6317 | if (CI.getType() == Type::Int1Ty && SrcI->hasOneUse()) { |
Chris Lattner | e13ab2a | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 6318 | Value *One = ConstantInt::get(SrcI->getType(), 1); |
| 6319 | |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6320 | Value *V = InsertNewInstBefore( |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 6321 | BinaryOperator::createShl(One, SrcI->getOperand(1), |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6322 | "tmp"), CI); |
Chris Lattner | e13ab2a | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 6323 | V = InsertNewInstBefore(BinaryOperator::createAnd(V, |
| 6324 | SrcI->getOperand(0), |
| 6325 | "tmp"), CI); |
| 6326 | Value *Zero = Constant::getNullValue(V->getType()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6327 | return new ICmpInst(ICmpInst::ICMP_NE, V, Zero); |
Chris Lattner | e13ab2a | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 6328 | } |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 6329 | } |
| 6330 | break; |
| 6331 | } |
| 6332 | } |
| 6333 | |
| 6334 | return 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6335 | } |
| 6336 | |
| 6337 | Instruction *InstCombiner::visitZExt(CastInst &CI) { |
| 6338 | // If one of the common conversion will work .. |
| 6339 | if (Instruction *Result = commonIntCastTransforms(CI)) |
| 6340 | return Result; |
| 6341 | |
| 6342 | Value *Src = CI.getOperand(0); |
| 6343 | |
| 6344 | // If this is a cast of a cast |
| 6345 | if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6346 | // If this is a TRUNC followed by a ZEXT then we are dealing with integral |
| 6347 | // types and if the sizes are just right we can convert this into a logical |
| 6348 | // 'and' which will be much cheaper than the pair of casts. |
| 6349 | if (isa<TruncInst>(CSrc)) { |
| 6350 | // Get the sizes of the types involved |
| 6351 | Value *A = CSrc->getOperand(0); |
| 6352 | unsigned SrcSize = A->getType()->getPrimitiveSizeInBits(); |
| 6353 | unsigned MidSize = CSrc->getType()->getPrimitiveSizeInBits(); |
| 6354 | unsigned DstSize = CI.getType()->getPrimitiveSizeInBits(); |
| 6355 | // If we're actually extending zero bits and the trunc is a no-op |
| 6356 | if (MidSize < DstSize && SrcSize == DstSize) { |
| 6357 | // Replace both of the casts with an And of the type mask. |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 6358 | uint64_t AndValue = cast<IntegerType>(CSrc->getType())->getBitMask(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6359 | Constant *AndConst = ConstantInt::get(A->getType(), AndValue); |
| 6360 | Instruction *And = |
| 6361 | BinaryOperator::createAnd(CSrc->getOperand(0), AndConst); |
| 6362 | // Unfortunately, if the type changed, we need to cast it back. |
| 6363 | if (And->getType() != CI.getType()) { |
| 6364 | And->setName(CSrc->getName()+".mask"); |
| 6365 | InsertNewInstBefore(And, CI); |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 6366 | And = CastInst::createIntegerCast(And, CI.getType(), false/*ZExt*/); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6367 | } |
| 6368 | return And; |
| 6369 | } |
| 6370 | } |
| 6371 | } |
| 6372 | |
| 6373 | return 0; |
| 6374 | } |
| 6375 | |
| 6376 | Instruction *InstCombiner::visitSExt(CastInst &CI) { |
| 6377 | return commonIntCastTransforms(CI); |
| 6378 | } |
| 6379 | |
| 6380 | Instruction *InstCombiner::visitFPTrunc(CastInst &CI) { |
| 6381 | return commonCastTransforms(CI); |
| 6382 | } |
| 6383 | |
| 6384 | Instruction *InstCombiner::visitFPExt(CastInst &CI) { |
| 6385 | return commonCastTransforms(CI); |
| 6386 | } |
| 6387 | |
| 6388 | Instruction *InstCombiner::visitFPToUI(CastInst &CI) { |
Reid Spencer | 44c030a | 2006-11-30 23:13:36 +0000 | [diff] [blame] | 6389 | return commonCastTransforms(CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6390 | } |
| 6391 | |
| 6392 | Instruction *InstCombiner::visitFPToSI(CastInst &CI) { |
Reid Spencer | 44c030a | 2006-11-30 23:13:36 +0000 | [diff] [blame] | 6393 | return commonCastTransforms(CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6394 | } |
| 6395 | |
| 6396 | Instruction *InstCombiner::visitUIToFP(CastInst &CI) { |
| 6397 | return commonCastTransforms(CI); |
| 6398 | } |
| 6399 | |
| 6400 | Instruction *InstCombiner::visitSIToFP(CastInst &CI) { |
| 6401 | return commonCastTransforms(CI); |
| 6402 | } |
| 6403 | |
| 6404 | Instruction *InstCombiner::visitPtrToInt(CastInst &CI) { |
Reid Spencer | 44c030a | 2006-11-30 23:13:36 +0000 | [diff] [blame] | 6405 | return commonCastTransforms(CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6406 | } |
| 6407 | |
| 6408 | Instruction *InstCombiner::visitIntToPtr(CastInst &CI) { |
| 6409 | return commonCastTransforms(CI); |
| 6410 | } |
| 6411 | |
| 6412 | Instruction *InstCombiner::visitBitCast(CastInst &CI) { |
| 6413 | |
| 6414 | // If the operands are integer typed then apply the integer transforms, |
| 6415 | // otherwise just apply the common ones. |
| 6416 | Value *Src = CI.getOperand(0); |
| 6417 | const Type *SrcTy = Src->getType(); |
| 6418 | const Type *DestTy = CI.getType(); |
| 6419 | |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 6420 | if (SrcTy->isInteger() && DestTy->isInteger()) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6421 | if (Instruction *Result = commonIntCastTransforms(CI)) |
| 6422 | return Result; |
| 6423 | } else { |
| 6424 | if (Instruction *Result = commonCastTransforms(CI)) |
| 6425 | return Result; |
| 6426 | } |
| 6427 | |
| 6428 | |
| 6429 | // Get rid of casts from one type to the same type. These are useless and can |
| 6430 | // be replaced by the operand. |
| 6431 | if (DestTy == Src->getType()) |
| 6432 | return ReplaceInstUsesWith(CI, Src); |
| 6433 | |
Chris Lattner | 9fb9213 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 6434 | // If the source and destination are pointers, and this cast is equivalent to |
| 6435 | // a getelementptr X, 0, 0, 0... turn it into the appropriate getelementptr. |
| 6436 | // This can enhance SROA and other transforms that want type-safe pointers. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6437 | if (const PointerType *DstPTy = dyn_cast<PointerType>(DestTy)) { |
| 6438 | if (const PointerType *SrcPTy = dyn_cast<PointerType>(SrcTy)) { |
| 6439 | const Type *DstElTy = DstPTy->getElementType(); |
| 6440 | const Type *SrcElTy = SrcPTy->getElementType(); |
Chris Lattner | 9fb9213 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 6441 | |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6442 | Constant *ZeroUInt = Constant::getNullValue(Type::Int32Ty); |
Chris Lattner | 9fb9213 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 6443 | unsigned NumZeros = 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6444 | while (SrcElTy != DstElTy && |
| 6445 | isa<CompositeType>(SrcElTy) && !isa<PointerType>(SrcElTy) && |
| 6446 | SrcElTy->getNumContainedTypes() /* not "{}" */) { |
| 6447 | SrcElTy = cast<CompositeType>(SrcElTy)->getTypeAtIndex(ZeroUInt); |
Chris Lattner | 9fb9213 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 6448 | ++NumZeros; |
| 6449 | } |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 6450 | |
Chris Lattner | 9fb9213 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 6451 | // If we found a path from the src to dest, create the getelementptr now. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6452 | if (SrcElTy == DstElTy) { |
Chris Lattner | fbbe92f | 2007-01-31 20:08:52 +0000 | [diff] [blame] | 6453 | SmallVector<Value*, 8> Idxs(NumZeros+1, ZeroUInt); |
| 6454 | return new GetElementPtrInst(Src, &Idxs[0], Idxs.size()); |
Chris Lattner | 9fb9213 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 6455 | } |
| 6456 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6457 | } |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 6458 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6459 | if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(Src)) { |
| 6460 | if (SVI->hasOneUse()) { |
| 6461 | // Okay, we have (bitconvert (shuffle ..)). Check to see if this is |
| 6462 | // a bitconvert to a vector with the same # elts. |
| 6463 | if (isa<PackedType>(DestTy) && |
| 6464 | cast<PackedType>(DestTy)->getNumElements() == |
| 6465 | SVI->getType()->getNumElements()) { |
| 6466 | CastInst *Tmp; |
| 6467 | // If either of the operands is a cast from CI.getType(), then |
| 6468 | // evaluating the shuffle in the casted destination's type will allow |
| 6469 | // us to eliminate at least one cast. |
| 6470 | if (((Tmp = dyn_cast<CastInst>(SVI->getOperand(0))) && |
| 6471 | Tmp->getOperand(0)->getType() == DestTy) || |
| 6472 | ((Tmp = dyn_cast<CastInst>(SVI->getOperand(1))) && |
| 6473 | Tmp->getOperand(0)->getType() == DestTy)) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 6474 | Value *LHS = InsertOperandCastBefore(Instruction::BitCast, |
| 6475 | SVI->getOperand(0), DestTy, &CI); |
| 6476 | Value *RHS = InsertOperandCastBefore(Instruction::BitCast, |
| 6477 | SVI->getOperand(1), DestTy, &CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6478 | // Return a new shuffle vector. Use the same element ID's, as we |
| 6479 | // know the vector types match #elts. |
| 6480 | return new ShuffleVectorInst(LHS, RHS, SVI->getOperand(2)); |
Chris Lattner | 01575b7 | 2006-05-25 23:24:33 +0000 | [diff] [blame] | 6481 | } |
| 6482 | } |
| 6483 | } |
| 6484 | } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 6485 | return 0; |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 6486 | } |
| 6487 | |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6488 | /// GetSelectFoldableOperands - We want to turn code that looks like this: |
| 6489 | /// %C = or %A, %B |
| 6490 | /// %D = select %cond, %C, %A |
| 6491 | /// into: |
| 6492 | /// %C = select %cond, %B, 0 |
| 6493 | /// %D = or %A, %C |
| 6494 | /// |
| 6495 | /// Assuming that the specified instruction is an operand to the select, return |
| 6496 | /// a bitmask indicating which operands of this instruction are foldable if they |
| 6497 | /// equal the other incoming value of the select. |
| 6498 | /// |
| 6499 | static unsigned GetSelectFoldableOperands(Instruction *I) { |
| 6500 | switch (I->getOpcode()) { |
| 6501 | case Instruction::Add: |
| 6502 | case Instruction::Mul: |
| 6503 | case Instruction::And: |
| 6504 | case Instruction::Or: |
| 6505 | case Instruction::Xor: |
| 6506 | return 3; // Can fold through either operand. |
| 6507 | case Instruction::Sub: // Can only fold on the amount subtracted. |
| 6508 | case Instruction::Shl: // Can only fold on the shift amount. |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 6509 | case Instruction::LShr: |
| 6510 | case Instruction::AShr: |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6511 | return 1; |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6512 | default: |
| 6513 | return 0; // Cannot fold |
| 6514 | } |
| 6515 | } |
| 6516 | |
| 6517 | /// GetSelectFoldableConstant - For the same transformation as the previous |
| 6518 | /// function, return the identity constant that goes into the select. |
| 6519 | static Constant *GetSelectFoldableConstant(Instruction *I) { |
| 6520 | switch (I->getOpcode()) { |
| 6521 | default: assert(0 && "This cannot happen!"); abort(); |
| 6522 | case Instruction::Add: |
| 6523 | case Instruction::Sub: |
| 6524 | case Instruction::Or: |
| 6525 | case Instruction::Xor: |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6526 | case Instruction::Shl: |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 6527 | case Instruction::LShr: |
| 6528 | case Instruction::AShr: |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6529 | return Constant::getNullValue(I->getType()); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6530 | case Instruction::And: |
| 6531 | return ConstantInt::getAllOnesValue(I->getType()); |
| 6532 | case Instruction::Mul: |
| 6533 | return ConstantInt::get(I->getType(), 1); |
| 6534 | } |
| 6535 | } |
| 6536 | |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 6537 | /// FoldSelectOpOp - Here we have (select c, TI, FI), and we know that TI and FI |
| 6538 | /// have the same opcode and only one use each. Try to simplify this. |
| 6539 | Instruction *InstCombiner::FoldSelectOpOp(SelectInst &SI, Instruction *TI, |
| 6540 | Instruction *FI) { |
| 6541 | if (TI->getNumOperands() == 1) { |
| 6542 | // If this is a non-volatile load or a cast from the same type, |
| 6543 | // merge. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6544 | if (TI->isCast()) { |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 6545 | if (TI->getOperand(0)->getType() != FI->getOperand(0)->getType()) |
| 6546 | return 0; |
| 6547 | } else { |
| 6548 | return 0; // unknown unary op. |
| 6549 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6550 | |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 6551 | // Fold this by inserting a select from the input values. |
| 6552 | SelectInst *NewSI = new SelectInst(SI.getCondition(), TI->getOperand(0), |
| 6553 | FI->getOperand(0), SI.getName()+".v"); |
| 6554 | InsertNewInstBefore(NewSI, SI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6555 | return CastInst::create(Instruction::CastOps(TI->getOpcode()), NewSI, |
| 6556 | TI->getType()); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 6557 | } |
| 6558 | |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6559 | // Only handle binary operators here. |
| 6560 | if (!isa<BinaryOperator>(TI)) |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 6561 | return 0; |
| 6562 | |
| 6563 | // Figure out if the operations have any operands in common. |
| 6564 | Value *MatchOp, *OtherOpT, *OtherOpF; |
| 6565 | bool MatchIsOpZero; |
| 6566 | if (TI->getOperand(0) == FI->getOperand(0)) { |
| 6567 | MatchOp = TI->getOperand(0); |
| 6568 | OtherOpT = TI->getOperand(1); |
| 6569 | OtherOpF = FI->getOperand(1); |
| 6570 | MatchIsOpZero = true; |
| 6571 | } else if (TI->getOperand(1) == FI->getOperand(1)) { |
| 6572 | MatchOp = TI->getOperand(1); |
| 6573 | OtherOpT = TI->getOperand(0); |
| 6574 | OtherOpF = FI->getOperand(0); |
| 6575 | MatchIsOpZero = false; |
| 6576 | } else if (!TI->isCommutative()) { |
| 6577 | return 0; |
| 6578 | } else if (TI->getOperand(0) == FI->getOperand(1)) { |
| 6579 | MatchOp = TI->getOperand(0); |
| 6580 | OtherOpT = TI->getOperand(1); |
| 6581 | OtherOpF = FI->getOperand(0); |
| 6582 | MatchIsOpZero = true; |
| 6583 | } else if (TI->getOperand(1) == FI->getOperand(0)) { |
| 6584 | MatchOp = TI->getOperand(1); |
| 6585 | OtherOpT = TI->getOperand(0); |
| 6586 | OtherOpF = FI->getOperand(1); |
| 6587 | MatchIsOpZero = true; |
| 6588 | } else { |
| 6589 | return 0; |
| 6590 | } |
| 6591 | |
| 6592 | // If we reach here, they do have operations in common. |
| 6593 | SelectInst *NewSI = new SelectInst(SI.getCondition(), OtherOpT, |
| 6594 | OtherOpF, SI.getName()+".v"); |
| 6595 | InsertNewInstBefore(NewSI, SI); |
| 6596 | |
| 6597 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TI)) { |
| 6598 | if (MatchIsOpZero) |
| 6599 | return BinaryOperator::create(BO->getOpcode(), MatchOp, NewSI); |
| 6600 | else |
| 6601 | return BinaryOperator::create(BO->getOpcode(), NewSI, MatchOp); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 6602 | } |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6603 | assert(0 && "Shouldn't get here"); |
| 6604 | return 0; |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 6605 | } |
| 6606 | |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 6607 | Instruction *InstCombiner::visitSelectInst(SelectInst &SI) { |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 6608 | Value *CondVal = SI.getCondition(); |
| 6609 | Value *TrueVal = SI.getTrueValue(); |
| 6610 | Value *FalseVal = SI.getFalseValue(); |
| 6611 | |
| 6612 | // select true, X, Y -> X |
| 6613 | // select false, X, Y -> Y |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6614 | if (ConstantInt *C = dyn_cast<ConstantInt>(CondVal)) |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 6615 | return ReplaceInstUsesWith(SI, C->getZExtValue() ? TrueVal : FalseVal); |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 6616 | |
| 6617 | // select C, X, X -> X |
| 6618 | if (TrueVal == FalseVal) |
| 6619 | return ReplaceInstUsesWith(SI, TrueVal); |
| 6620 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 6621 | if (isa<UndefValue>(TrueVal)) // select C, undef, X -> X |
| 6622 | return ReplaceInstUsesWith(SI, FalseVal); |
| 6623 | if (isa<UndefValue>(FalseVal)) // select C, X, undef -> X |
| 6624 | return ReplaceInstUsesWith(SI, TrueVal); |
| 6625 | if (isa<UndefValue>(CondVal)) { // select undef, X, Y -> X or Y |
| 6626 | if (isa<Constant>(TrueVal)) |
| 6627 | return ReplaceInstUsesWith(SI, TrueVal); |
| 6628 | else |
| 6629 | return ReplaceInstUsesWith(SI, FalseVal); |
| 6630 | } |
| 6631 | |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 6632 | if (SI.getType() == Type::Int1Ty) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 6633 | if (ConstantInt *C = dyn_cast<ConstantInt>(TrueVal)) { |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 6634 | if (C->getZExtValue()) { |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 6635 | // Change: A = select B, true, C --> A = or B, C |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 6636 | return BinaryOperator::createOr(CondVal, FalseVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 6637 | } else { |
| 6638 | // Change: A = select B, false, C --> A = and !B, C |
| 6639 | Value *NotCond = |
| 6640 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
| 6641 | "not."+CondVal->getName()), SI); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 6642 | return BinaryOperator::createAnd(NotCond, FalseVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 6643 | } |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 6644 | } else if (ConstantInt *C = dyn_cast<ConstantInt>(FalseVal)) { |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 6645 | if (C->getZExtValue() == false) { |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 6646 | // Change: A = select B, C, false --> A = and B, C |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 6647 | return BinaryOperator::createAnd(CondVal, TrueVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 6648 | } else { |
| 6649 | // Change: A = select B, C, true --> A = or !B, C |
| 6650 | Value *NotCond = |
| 6651 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
| 6652 | "not."+CondVal->getName()), SI); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 6653 | return BinaryOperator::createOr(NotCond, TrueVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 6654 | } |
| 6655 | } |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6656 | } |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 6657 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 6658 | // Selecting between two integer constants? |
| 6659 | if (ConstantInt *TrueValC = dyn_cast<ConstantInt>(TrueVal)) |
| 6660 | if (ConstantInt *FalseValC = dyn_cast<ConstantInt>(FalseVal)) { |
| 6661 | // select C, 1, 0 -> cast C to int |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6662 | if (FalseValC->isNullValue() && TrueValC->getZExtValue() == 1) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6663 | return CastInst::create(Instruction::ZExt, CondVal, SI.getType()); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6664 | } else if (TrueValC->isNullValue() && FalseValC->getZExtValue() == 1) { |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 6665 | // select C, 0, 1 -> cast !C to int |
| 6666 | Value *NotCond = |
| 6667 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
Chris Lattner | 82e14fe | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 6668 | "not."+CondVal->getName()), SI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6669 | return CastInst::create(Instruction::ZExt, NotCond, SI.getType()); |
Chris Lattner | 82e14fe | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 6670 | } |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 6671 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6672 | if (ICmpInst *IC = dyn_cast<ICmpInst>(SI.getCondition())) { |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 6673 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6674 | // (x <s 0) ? -1 : 0 -> ashr x, 31 |
| 6675 | // (x >u 2147483647) ? -1 : 0 -> ashr x, 31 |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 6676 | if (TrueValC->isAllOnesValue() && FalseValC->isNullValue()) |
| 6677 | if (ConstantInt *CmpCst = dyn_cast<ConstantInt>(IC->getOperand(1))) { |
| 6678 | bool CanXForm = false; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6679 | if (IC->isSignedPredicate()) |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 6680 | CanXForm = CmpCst->isNullValue() && |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6681 | IC->getPredicate() == ICmpInst::ICMP_SLT; |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 6682 | else { |
| 6683 | unsigned Bits = CmpCst->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6684 | CanXForm = (CmpCst->getZExtValue() == ~0ULL >> (64-Bits+1)) && |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6685 | IC->getPredicate() == ICmpInst::ICMP_UGT; |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 6686 | } |
| 6687 | |
| 6688 | if (CanXForm) { |
| 6689 | // The comparison constant and the result are not neccessarily the |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6690 | // same width. Make an all-ones value by inserting a AShr. |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 6691 | Value *X = IC->getOperand(0); |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 6692 | unsigned Bits = X->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6693 | Constant *ShAmt = ConstantInt::get(X->getType(), Bits-1); |
| 6694 | Instruction *SRA = BinaryOperator::create(Instruction::AShr, X, |
| 6695 | ShAmt, "ones"); |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 6696 | InsertNewInstBefore(SRA, SI); |
| 6697 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6698 | // Finally, convert to the type of the select RHS. We figure out |
| 6699 | // if this requires a SExt, Trunc or BitCast based on the sizes. |
| 6700 | Instruction::CastOps opc = Instruction::BitCast; |
| 6701 | unsigned SRASize = SRA->getType()->getPrimitiveSizeInBits(); |
| 6702 | unsigned SISize = SI.getType()->getPrimitiveSizeInBits(); |
| 6703 | if (SRASize < SISize) |
| 6704 | opc = Instruction::SExt; |
| 6705 | else if (SRASize > SISize) |
| 6706 | opc = Instruction::Trunc; |
| 6707 | return CastInst::create(opc, SRA, SI.getType()); |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 6708 | } |
| 6709 | } |
| 6710 | |
| 6711 | |
| 6712 | // If one of the constants is zero (we know they can't both be) and we |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6713 | // have a fcmp instruction with zero, and we have an 'and' with the |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 6714 | // non-constant value, eliminate this whole mess. This corresponds to |
| 6715 | // cases like this: ((X & 27) ? 27 : 0) |
| 6716 | if (TrueValC->isNullValue() || FalseValC->isNullValue()) |
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 6717 | if (IC->isEquality() && isa<ConstantInt>(IC->getOperand(1)) && |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 6718 | cast<Constant>(IC->getOperand(1))->isNullValue()) |
| 6719 | if (Instruction *ICA = dyn_cast<Instruction>(IC->getOperand(0))) |
| 6720 | if (ICA->getOpcode() == Instruction::And && |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6721 | isa<ConstantInt>(ICA->getOperand(1)) && |
| 6722 | (ICA->getOperand(1) == TrueValC || |
| 6723 | ICA->getOperand(1) == FalseValC) && |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 6724 | isOneBitSet(cast<ConstantInt>(ICA->getOperand(1)))) { |
| 6725 | // Okay, now we know that everything is set up, we just don't |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6726 | // know whether we have a icmp_ne or icmp_eq and whether the |
| 6727 | // true or false val is the zero. |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 6728 | bool ShouldNotVal = !TrueValC->isNullValue(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6729 | ShouldNotVal ^= IC->getPredicate() == ICmpInst::ICMP_NE; |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 6730 | Value *V = ICA; |
| 6731 | if (ShouldNotVal) |
| 6732 | V = InsertNewInstBefore(BinaryOperator::create( |
| 6733 | Instruction::Xor, V, ICA->getOperand(1)), SI); |
| 6734 | return ReplaceInstUsesWith(SI, V); |
| 6735 | } |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 6736 | } |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 6737 | } |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 6738 | |
| 6739 | // See if we are selecting two values based on a comparison of the two values. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6740 | if (FCmpInst *FCI = dyn_cast<FCmpInst>(CondVal)) { |
| 6741 | if (FCI->getOperand(0) == TrueVal && FCI->getOperand(1) == FalseVal) { |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 6742 | // Transform (X == Y) ? X : Y -> Y |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6743 | if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 6744 | return ReplaceInstUsesWith(SI, FalseVal); |
| 6745 | // Transform (X != Y) ? X : Y -> X |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6746 | if (FCI->getPredicate() == FCmpInst::FCMP_ONE) |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 6747 | return ReplaceInstUsesWith(SI, TrueVal); |
| 6748 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 6749 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6750 | } else if (FCI->getOperand(0) == FalseVal && FCI->getOperand(1) == TrueVal){ |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 6751 | // Transform (X == Y) ? Y : X -> X |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6752 | if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) |
Chris Lattner | fbede52 | 2004-04-11 01:39:19 +0000 | [diff] [blame] | 6753 | return ReplaceInstUsesWith(SI, FalseVal); |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 6754 | // Transform (X != Y) ? Y : X -> Y |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6755 | if (FCI->getPredicate() == FCmpInst::FCMP_ONE) |
| 6756 | return ReplaceInstUsesWith(SI, TrueVal); |
| 6757 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 6758 | } |
| 6759 | } |
| 6760 | |
| 6761 | // See if we are selecting two values based on a comparison of the two values. |
| 6762 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(CondVal)) { |
| 6763 | if (ICI->getOperand(0) == TrueVal && ICI->getOperand(1) == FalseVal) { |
| 6764 | // Transform (X == Y) ? X : Y -> Y |
| 6765 | if (ICI->getPredicate() == ICmpInst::ICMP_EQ) |
| 6766 | return ReplaceInstUsesWith(SI, FalseVal); |
| 6767 | // Transform (X != Y) ? X : Y -> X |
| 6768 | if (ICI->getPredicate() == ICmpInst::ICMP_NE) |
| 6769 | return ReplaceInstUsesWith(SI, TrueVal); |
| 6770 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 6771 | |
| 6772 | } else if (ICI->getOperand(0) == FalseVal && ICI->getOperand(1) == TrueVal){ |
| 6773 | // Transform (X == Y) ? Y : X -> X |
| 6774 | if (ICI->getPredicate() == ICmpInst::ICMP_EQ) |
| 6775 | return ReplaceInstUsesWith(SI, FalseVal); |
| 6776 | // Transform (X != Y) ? Y : X -> Y |
| 6777 | if (ICI->getPredicate() == ICmpInst::ICMP_NE) |
Chris Lattner | fbede52 | 2004-04-11 01:39:19 +0000 | [diff] [blame] | 6778 | return ReplaceInstUsesWith(SI, TrueVal); |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 6779 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 6780 | } |
| 6781 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6782 | |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 6783 | if (Instruction *TI = dyn_cast<Instruction>(TrueVal)) |
| 6784 | if (Instruction *FI = dyn_cast<Instruction>(FalseVal)) |
| 6785 | if (TI->hasOneUse() && FI->hasOneUse()) { |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 6786 | Instruction *AddOp = 0, *SubOp = 0; |
| 6787 | |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 6788 | // Turn (select C, (op X, Y), (op X, Z)) -> (op X, (select C, Y, Z)) |
| 6789 | if (TI->getOpcode() == FI->getOpcode()) |
| 6790 | if (Instruction *IV = FoldSelectOpOp(SI, TI, FI)) |
| 6791 | return IV; |
| 6792 | |
| 6793 | // Turn select C, (X+Y), (X-Y) --> (X+(select C, Y, (-Y))). This is |
| 6794 | // even legal for FP. |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 6795 | if (TI->getOpcode() == Instruction::Sub && |
| 6796 | FI->getOpcode() == Instruction::Add) { |
| 6797 | AddOp = FI; SubOp = TI; |
| 6798 | } else if (FI->getOpcode() == Instruction::Sub && |
| 6799 | TI->getOpcode() == Instruction::Add) { |
| 6800 | AddOp = TI; SubOp = FI; |
| 6801 | } |
| 6802 | |
| 6803 | if (AddOp) { |
| 6804 | Value *OtherAddOp = 0; |
| 6805 | if (SubOp->getOperand(0) == AddOp->getOperand(0)) { |
| 6806 | OtherAddOp = AddOp->getOperand(1); |
| 6807 | } else if (SubOp->getOperand(0) == AddOp->getOperand(1)) { |
| 6808 | OtherAddOp = AddOp->getOperand(0); |
| 6809 | } |
| 6810 | |
| 6811 | if (OtherAddOp) { |
Chris Lattner | 97f37a4 | 2006-02-24 18:05:58 +0000 | [diff] [blame] | 6812 | // So at this point we know we have (Y -> OtherAddOp): |
| 6813 | // select C, (add X, Y), (sub X, Z) |
| 6814 | Value *NegVal; // Compute -Z |
| 6815 | if (Constant *C = dyn_cast<Constant>(SubOp->getOperand(1))) { |
| 6816 | NegVal = ConstantExpr::getNeg(C); |
| 6817 | } else { |
| 6818 | NegVal = InsertNewInstBefore( |
| 6819 | BinaryOperator::createNeg(SubOp->getOperand(1), "tmp"), SI); |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 6820 | } |
Chris Lattner | 97f37a4 | 2006-02-24 18:05:58 +0000 | [diff] [blame] | 6821 | |
| 6822 | Value *NewTrueOp = OtherAddOp; |
| 6823 | Value *NewFalseOp = NegVal; |
| 6824 | if (AddOp != TI) |
| 6825 | std::swap(NewTrueOp, NewFalseOp); |
| 6826 | Instruction *NewSel = |
| 6827 | new SelectInst(CondVal, NewTrueOp,NewFalseOp,SI.getName()+".p"); |
| 6828 | |
| 6829 | NewSel = InsertNewInstBefore(NewSel, SI); |
| 6830 | return BinaryOperator::createAdd(SubOp->getOperand(0), NewSel); |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 6831 | } |
| 6832 | } |
| 6833 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6834 | |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6835 | // See if we can fold the select into one of our operands. |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 6836 | if (SI.getType()->isInteger()) { |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6837 | // See the comment above GetSelectFoldableOperands for a description of the |
| 6838 | // transformation we are doing here. |
| 6839 | if (Instruction *TVI = dyn_cast<Instruction>(TrueVal)) |
| 6840 | if (TVI->hasOneUse() && TVI->getNumOperands() == 2 && |
| 6841 | !isa<Constant>(FalseVal)) |
| 6842 | if (unsigned SFO = GetSelectFoldableOperands(TVI)) { |
| 6843 | unsigned OpToFold = 0; |
| 6844 | if ((SFO & 1) && FalseVal == TVI->getOperand(0)) { |
| 6845 | OpToFold = 1; |
| 6846 | } else if ((SFO & 2) && FalseVal == TVI->getOperand(1)) { |
| 6847 | OpToFold = 2; |
| 6848 | } |
| 6849 | |
| 6850 | if (OpToFold) { |
| 6851 | Constant *C = GetSelectFoldableConstant(TVI); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6852 | Instruction *NewSel = |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6853 | new SelectInst(SI.getCondition(), TVI->getOperand(2-OpToFold), C); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6854 | InsertNewInstBefore(NewSel, SI); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6855 | NewSel->takeName(TVI); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6856 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TVI)) |
| 6857 | return BinaryOperator::create(BO->getOpcode(), FalseVal, NewSel); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6858 | else { |
| 6859 | assert(0 && "Unknown instruction!!"); |
| 6860 | } |
| 6861 | } |
| 6862 | } |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 6863 | |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6864 | if (Instruction *FVI = dyn_cast<Instruction>(FalseVal)) |
| 6865 | if (FVI->hasOneUse() && FVI->getNumOperands() == 2 && |
| 6866 | !isa<Constant>(TrueVal)) |
| 6867 | if (unsigned SFO = GetSelectFoldableOperands(FVI)) { |
| 6868 | unsigned OpToFold = 0; |
| 6869 | if ((SFO & 1) && TrueVal == FVI->getOperand(0)) { |
| 6870 | OpToFold = 1; |
| 6871 | } else if ((SFO & 2) && TrueVal == FVI->getOperand(1)) { |
| 6872 | OpToFold = 2; |
| 6873 | } |
| 6874 | |
| 6875 | if (OpToFold) { |
| 6876 | Constant *C = GetSelectFoldableConstant(FVI); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6877 | Instruction *NewSel = |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6878 | new SelectInst(SI.getCondition(), C, FVI->getOperand(2-OpToFold)); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6879 | InsertNewInstBefore(NewSel, SI); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6880 | NewSel->takeName(FVI); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6881 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(FVI)) |
| 6882 | return BinaryOperator::create(BO->getOpcode(), TrueVal, NewSel); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6883 | else |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6884 | assert(0 && "Unknown instruction!!"); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6885 | } |
| 6886 | } |
| 6887 | } |
Chris Lattner | a1df33c | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 6888 | |
| 6889 | if (BinaryOperator::isNot(CondVal)) { |
| 6890 | SI.setOperand(0, BinaryOperator::getNotArgument(CondVal)); |
| 6891 | SI.setOperand(1, FalseVal); |
| 6892 | SI.setOperand(2, TrueVal); |
| 6893 | return &SI; |
| 6894 | } |
| 6895 | |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 6896 | return 0; |
| 6897 | } |
| 6898 | |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 6899 | /// GetKnownAlignment - If the specified pointer has an alignment that we can |
| 6900 | /// determine, return it, otherwise return 0. |
| 6901 | static unsigned GetKnownAlignment(Value *V, TargetData *TD) { |
| 6902 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) { |
| 6903 | unsigned Align = GV->getAlignment(); |
| 6904 | if (Align == 0 && TD) |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame^] | 6905 | Align = TD->getPrefTypeAlignment(GV->getType()->getElementType()); |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 6906 | return Align; |
| 6907 | } else if (AllocationInst *AI = dyn_cast<AllocationInst>(V)) { |
| 6908 | unsigned Align = AI->getAlignment(); |
| 6909 | if (Align == 0 && TD) { |
| 6910 | if (isa<AllocaInst>(AI)) |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame^] | 6911 | Align = TD->getPrefTypeAlignment(AI->getType()->getElementType()); |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 6912 | else if (isa<MallocInst>(AI)) { |
| 6913 | // Malloc returns maximally aligned memory. |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame^] | 6914 | Align = TD->getABITypeAlignment(AI->getType()->getElementType()); |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 6915 | Align = |
| 6916 | std::max(Align, |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame^] | 6917 | (unsigned)TD->getABITypeAlignment(Type::DoubleTy)); |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 6918 | Align = |
| 6919 | std::max(Align, |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame^] | 6920 | (unsigned)TD->getABITypeAlignment(Type::Int64Ty)); |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 6921 | } |
| 6922 | } |
| 6923 | return Align; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6924 | } else if (isa<BitCastInst>(V) || |
Chris Lattner | 51c26e9 | 2006-03-07 01:28:57 +0000 | [diff] [blame] | 6925 | (isa<ConstantExpr>(V) && |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6926 | cast<ConstantExpr>(V)->getOpcode() == Instruction::BitCast)) { |
Chris Lattner | 51c26e9 | 2006-03-07 01:28:57 +0000 | [diff] [blame] | 6927 | User *CI = cast<User>(V); |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 6928 | if (isa<PointerType>(CI->getOperand(0)->getType())) |
| 6929 | return GetKnownAlignment(CI->getOperand(0), TD); |
| 6930 | return 0; |
Chris Lattner | 51c26e9 | 2006-03-07 01:28:57 +0000 | [diff] [blame] | 6931 | } else if (isa<GetElementPtrInst>(V) || |
| 6932 | (isa<ConstantExpr>(V) && |
| 6933 | cast<ConstantExpr>(V)->getOpcode()==Instruction::GetElementPtr)) { |
| 6934 | User *GEPI = cast<User>(V); |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 6935 | unsigned BaseAlignment = GetKnownAlignment(GEPI->getOperand(0), TD); |
| 6936 | if (BaseAlignment == 0) return 0; |
| 6937 | |
| 6938 | // If all indexes are zero, it is just the alignment of the base pointer. |
| 6939 | bool AllZeroOperands = true; |
| 6940 | for (unsigned i = 1, e = GEPI->getNumOperands(); i != e; ++i) |
| 6941 | if (!isa<Constant>(GEPI->getOperand(i)) || |
| 6942 | !cast<Constant>(GEPI->getOperand(i))->isNullValue()) { |
| 6943 | AllZeroOperands = false; |
| 6944 | break; |
| 6945 | } |
| 6946 | if (AllZeroOperands) |
| 6947 | return BaseAlignment; |
| 6948 | |
| 6949 | // Otherwise, if the base alignment is >= the alignment we expect for the |
| 6950 | // base pointer type, then we know that the resultant pointer is aligned at |
| 6951 | // least as much as its type requires. |
| 6952 | if (!TD) return 0; |
| 6953 | |
| 6954 | const Type *BasePtrTy = GEPI->getOperand(0)->getType(); |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 6955 | const PointerType *PtrTy = cast<PointerType>(BasePtrTy); |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame^] | 6956 | if (TD->getABITypeAlignment(PtrTy->getElementType()) |
Chris Lattner | 51c26e9 | 2006-03-07 01:28:57 +0000 | [diff] [blame] | 6957 | <= BaseAlignment) { |
| 6958 | const Type *GEPTy = GEPI->getType(); |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 6959 | const PointerType *GEPPtrTy = cast<PointerType>(GEPTy); |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame^] | 6960 | return TD->getABITypeAlignment(GEPPtrTy->getElementType()); |
Chris Lattner | 51c26e9 | 2006-03-07 01:28:57 +0000 | [diff] [blame] | 6961 | } |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 6962 | return 0; |
| 6963 | } |
| 6964 | return 0; |
| 6965 | } |
| 6966 | |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 6967 | |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 6968 | /// visitCallInst - CallInst simplification. This mostly only handles folding |
| 6969 | /// of intrinsic instructions. For normal calls, it allows visitCallSite to do |
| 6970 | /// the heavy lifting. |
| 6971 | /// |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 6972 | Instruction *InstCombiner::visitCallInst(CallInst &CI) { |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 6973 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(&CI); |
| 6974 | if (!II) return visitCallSite(&CI); |
| 6975 | |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 6976 | // Intrinsics cannot occur in an invoke, so handle them here instead of in |
| 6977 | // visitCallSite. |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 6978 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(II)) { |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 6979 | bool Changed = false; |
| 6980 | |
| 6981 | // memmove/cpy/set of zero bytes is a noop. |
| 6982 | if (Constant *NumBytes = dyn_cast<Constant>(MI->getLength())) { |
| 6983 | if (NumBytes->isNullValue()) return EraseInstFromFunction(CI); |
| 6984 | |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 6985 | if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes)) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6986 | if (CI->getZExtValue() == 1) { |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 6987 | // Replace the instruction with just byte operations. We would |
| 6988 | // transform other cases to loads/stores, but we don't know if |
| 6989 | // alignment is sufficient. |
| 6990 | } |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 6991 | } |
| 6992 | |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 6993 | // If we have a memmove and the source operation is a constant global, |
| 6994 | // then the source and dest pointers can't alias, so we can change this |
| 6995 | // into a call to memcpy. |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 6996 | if (MemMoveInst *MMI = dyn_cast<MemMoveInst>(II)) { |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 6997 | if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource())) |
| 6998 | if (GVSrc->isConstant()) { |
| 6999 | Module *M = CI.getParent()->getParent()->getParent(); |
Chris Lattner | 2195939 | 2006-03-03 01:34:17 +0000 | [diff] [blame] | 7000 | const char *Name; |
Andrew Lenharth | 8ed4c47 | 2006-11-03 22:45:50 +0000 | [diff] [blame] | 7001 | if (CI.getCalledFunction()->getFunctionType()->getParamType(2) == |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 7002 | Type::Int32Ty) |
Chris Lattner | 2195939 | 2006-03-03 01:34:17 +0000 | [diff] [blame] | 7003 | Name = "llvm.memcpy.i32"; |
| 7004 | else |
| 7005 | Name = "llvm.memcpy.i64"; |
Chris Lattner | 9214196 | 2007-01-07 06:58:05 +0000 | [diff] [blame] | 7006 | Constant *MemCpy = M->getOrInsertFunction(Name, |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 7007 | CI.getCalledFunction()->getFunctionType()); |
| 7008 | CI.setOperand(0, MemCpy); |
| 7009 | Changed = true; |
| 7010 | } |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7011 | } |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 7012 | |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7013 | // If we can determine a pointer alignment that is bigger than currently |
| 7014 | // set, update the alignment. |
| 7015 | if (isa<MemCpyInst>(MI) || isa<MemMoveInst>(MI)) { |
| 7016 | unsigned Alignment1 = GetKnownAlignment(MI->getOperand(1), TD); |
| 7017 | unsigned Alignment2 = GetKnownAlignment(MI->getOperand(2), TD); |
| 7018 | unsigned Align = std::min(Alignment1, Alignment2); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7019 | if (MI->getAlignment()->getZExtValue() < Align) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 7020 | MI->setAlignment(ConstantInt::get(Type::Int32Ty, Align)); |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7021 | Changed = true; |
| 7022 | } |
| 7023 | } else if (isa<MemSetInst>(MI)) { |
| 7024 | unsigned Alignment = GetKnownAlignment(MI->getDest(), TD); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7025 | if (MI->getAlignment()->getZExtValue() < Alignment) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 7026 | MI->setAlignment(ConstantInt::get(Type::Int32Ty, Alignment)); |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7027 | Changed = true; |
| 7028 | } |
| 7029 | } |
| 7030 | |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 7031 | if (Changed) return II; |
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 7032 | } else { |
| 7033 | switch (II->getIntrinsicID()) { |
| 7034 | default: break; |
Chris Lattner | 82ed58f | 2006-04-02 05:30:25 +0000 | [diff] [blame] | 7035 | case Intrinsic::ppc_altivec_lvx: |
| 7036 | case Intrinsic::ppc_altivec_lvxl: |
Chris Lattner | fd6bdf0 | 2006-04-17 22:26:56 +0000 | [diff] [blame] | 7037 | case Intrinsic::x86_sse_loadu_ps: |
| 7038 | case Intrinsic::x86_sse2_loadu_pd: |
| 7039 | case Intrinsic::x86_sse2_loadu_dq: |
| 7040 | // Turn PPC lvx -> load if the pointer is known aligned. |
| 7041 | // Turn X86 loadups -> load if the pointer is known aligned. |
Chris Lattner | 82ed58f | 2006-04-02 05:30:25 +0000 | [diff] [blame] | 7042 | if (GetKnownAlignment(II->getOperand(1), TD) >= 16) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7043 | Value *Ptr = InsertCastBefore(Instruction::BitCast, II->getOperand(1), |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 7044 | PointerType::get(II->getType()), CI); |
Chris Lattner | 82ed58f | 2006-04-02 05:30:25 +0000 | [diff] [blame] | 7045 | return new LoadInst(Ptr); |
| 7046 | } |
| 7047 | break; |
| 7048 | case Intrinsic::ppc_altivec_stvx: |
| 7049 | case Intrinsic::ppc_altivec_stvxl: |
| 7050 | // Turn stvx -> store if the pointer is known aligned. |
| 7051 | if (GetKnownAlignment(II->getOperand(2), TD) >= 16) { |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 7052 | const Type *OpPtrTy = PointerType::get(II->getOperand(1)->getType()); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7053 | Value *Ptr = InsertCastBefore(Instruction::BitCast, II->getOperand(2), |
| 7054 | OpPtrTy, CI); |
Chris Lattner | 82ed58f | 2006-04-02 05:30:25 +0000 | [diff] [blame] | 7055 | return new StoreInst(II->getOperand(1), Ptr); |
| 7056 | } |
| 7057 | break; |
Chris Lattner | fd6bdf0 | 2006-04-17 22:26:56 +0000 | [diff] [blame] | 7058 | case Intrinsic::x86_sse_storeu_ps: |
| 7059 | case Intrinsic::x86_sse2_storeu_pd: |
| 7060 | case Intrinsic::x86_sse2_storeu_dq: |
| 7061 | case Intrinsic::x86_sse2_storel_dq: |
| 7062 | // Turn X86 storeu -> store if the pointer is known aligned. |
| 7063 | if (GetKnownAlignment(II->getOperand(1), TD) >= 16) { |
| 7064 | const Type *OpPtrTy = PointerType::get(II->getOperand(2)->getType()); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7065 | Value *Ptr = InsertCastBefore(Instruction::BitCast, II->getOperand(1), |
| 7066 | OpPtrTy, CI); |
Chris Lattner | fd6bdf0 | 2006-04-17 22:26:56 +0000 | [diff] [blame] | 7067 | return new StoreInst(II->getOperand(2), Ptr); |
| 7068 | } |
| 7069 | break; |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 7070 | |
| 7071 | case Intrinsic::x86_sse_cvttss2si: { |
| 7072 | // These intrinsics only demands the 0th element of its input vector. If |
| 7073 | // we can simplify the input based on that, do so now. |
| 7074 | uint64_t UndefElts; |
| 7075 | if (Value *V = SimplifyDemandedVectorElts(II->getOperand(1), 1, |
| 7076 | UndefElts)) { |
| 7077 | II->setOperand(1, V); |
| 7078 | return II; |
| 7079 | } |
| 7080 | break; |
| 7081 | } |
| 7082 | |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 7083 | case Intrinsic::ppc_altivec_vperm: |
| 7084 | // Turn vperm(V1,V2,mask) -> shuffle(V1,V2,mask) if mask is a constant. |
| 7085 | if (ConstantPacked *Mask = dyn_cast<ConstantPacked>(II->getOperand(3))) { |
| 7086 | assert(Mask->getNumOperands() == 16 && "Bad type for intrinsic!"); |
| 7087 | |
| 7088 | // Check that all of the elements are integer constants or undefs. |
| 7089 | bool AllEltsOk = true; |
| 7090 | for (unsigned i = 0; i != 16; ++i) { |
| 7091 | if (!isa<ConstantInt>(Mask->getOperand(i)) && |
| 7092 | !isa<UndefValue>(Mask->getOperand(i))) { |
| 7093 | AllEltsOk = false; |
| 7094 | break; |
| 7095 | } |
| 7096 | } |
| 7097 | |
| 7098 | if (AllEltsOk) { |
| 7099 | // Cast the input vectors to byte vectors. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7100 | Value *Op0 = InsertCastBefore(Instruction::BitCast, |
| 7101 | II->getOperand(1), Mask->getType(), CI); |
| 7102 | Value *Op1 = InsertCastBefore(Instruction::BitCast, |
| 7103 | II->getOperand(2), Mask->getType(), CI); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 7104 | Value *Result = UndefValue::get(Op0->getType()); |
| 7105 | |
| 7106 | // Only extract each element once. |
| 7107 | Value *ExtractedElts[32]; |
| 7108 | memset(ExtractedElts, 0, sizeof(ExtractedElts)); |
| 7109 | |
| 7110 | for (unsigned i = 0; i != 16; ++i) { |
| 7111 | if (isa<UndefValue>(Mask->getOperand(i))) |
| 7112 | continue; |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7113 | unsigned Idx =cast<ConstantInt>(Mask->getOperand(i))->getZExtValue(); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 7114 | Idx &= 31; // Match the hardware behavior. |
| 7115 | |
| 7116 | if (ExtractedElts[Idx] == 0) { |
| 7117 | Instruction *Elt = |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 7118 | new ExtractElementInst(Idx < 16 ? Op0 : Op1, Idx&15, "tmp"); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 7119 | InsertNewInstBefore(Elt, CI); |
| 7120 | ExtractedElts[Idx] = Elt; |
| 7121 | } |
| 7122 | |
| 7123 | // Insert this value into the result vector. |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 7124 | Result = new InsertElementInst(Result, ExtractedElts[Idx], i,"tmp"); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 7125 | InsertNewInstBefore(cast<Instruction>(Result), CI); |
| 7126 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7127 | return CastInst::create(Instruction::BitCast, Result, CI.getType()); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 7128 | } |
| 7129 | } |
| 7130 | break; |
| 7131 | |
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 7132 | case Intrinsic::stackrestore: { |
| 7133 | // If the save is right next to the restore, remove the restore. This can |
| 7134 | // happen when variable allocas are DCE'd. |
| 7135 | if (IntrinsicInst *SS = dyn_cast<IntrinsicInst>(II->getOperand(1))) { |
| 7136 | if (SS->getIntrinsicID() == Intrinsic::stacksave) { |
| 7137 | BasicBlock::iterator BI = SS; |
| 7138 | if (&*++BI == II) |
| 7139 | return EraseInstFromFunction(CI); |
| 7140 | } |
| 7141 | } |
| 7142 | |
| 7143 | // If the stack restore is in a return/unwind block and if there are no |
| 7144 | // allocas or calls between the restore and the return, nuke the restore. |
| 7145 | TerminatorInst *TI = II->getParent()->getTerminator(); |
| 7146 | if (isa<ReturnInst>(TI) || isa<UnwindInst>(TI)) { |
| 7147 | BasicBlock::iterator BI = II; |
| 7148 | bool CannotRemove = false; |
| 7149 | for (++BI; &*BI != TI; ++BI) { |
| 7150 | if (isa<AllocaInst>(BI) || |
| 7151 | (isa<CallInst>(BI) && !isa<IntrinsicInst>(BI))) { |
| 7152 | CannotRemove = true; |
| 7153 | break; |
| 7154 | } |
| 7155 | } |
| 7156 | if (!CannotRemove) |
| 7157 | return EraseInstFromFunction(CI); |
| 7158 | } |
| 7159 | break; |
| 7160 | } |
| 7161 | } |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 7162 | } |
| 7163 | |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 7164 | return visitCallSite(II); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7165 | } |
| 7166 | |
| 7167 | // InvokeInst simplification |
| 7168 | // |
| 7169 | Instruction *InstCombiner::visitInvokeInst(InvokeInst &II) { |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 7170 | return visitCallSite(&II); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7171 | } |
| 7172 | |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 7173 | // visitCallSite - Improvements for call and invoke instructions. |
| 7174 | // |
| 7175 | Instruction *InstCombiner::visitCallSite(CallSite CS) { |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 7176 | bool Changed = false; |
| 7177 | |
| 7178 | // If the callee is a constexpr cast of a function, attempt to move the cast |
| 7179 | // to the arguments of the call/invoke. |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 7180 | if (transformConstExprCastCall(CS)) return 0; |
| 7181 | |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 7182 | Value *Callee = CS.getCalledValue(); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 7183 | |
Chris Lattner | 08b22ec | 2005-05-13 07:09:09 +0000 | [diff] [blame] | 7184 | if (Function *CalleeF = dyn_cast<Function>(Callee)) |
| 7185 | if (CalleeF->getCallingConv() != CS.getCallingConv()) { |
| 7186 | Instruction *OldCall = CS.getInstruction(); |
| 7187 | // If the call and callee calling conventions don't match, this call must |
| 7188 | // be unreachable, as the call is undefined. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 7189 | new StoreInst(ConstantInt::getTrue(), |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 7190 | UndefValue::get(PointerType::get(Type::Int1Ty)), OldCall); |
Chris Lattner | 08b22ec | 2005-05-13 07:09:09 +0000 | [diff] [blame] | 7191 | if (!OldCall->use_empty()) |
| 7192 | OldCall->replaceAllUsesWith(UndefValue::get(OldCall->getType())); |
| 7193 | if (isa<CallInst>(OldCall)) // Not worth removing an invoke here. |
| 7194 | return EraseInstFromFunction(*OldCall); |
| 7195 | return 0; |
| 7196 | } |
| 7197 | |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 7198 | if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) { |
| 7199 | // This instruction is not reachable, just remove it. We insert a store to |
| 7200 | // undef so that we know that this code is not reachable, despite the fact |
| 7201 | // that we can't modify the CFG here. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 7202 | new StoreInst(ConstantInt::getTrue(), |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 7203 | UndefValue::get(PointerType::get(Type::Int1Ty)), |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 7204 | CS.getInstruction()); |
| 7205 | |
| 7206 | if (!CS.getInstruction()->use_empty()) |
| 7207 | CS.getInstruction()-> |
| 7208 | replaceAllUsesWith(UndefValue::get(CS.getInstruction()->getType())); |
| 7209 | |
| 7210 | if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) { |
| 7211 | // Don't break the CFG, insert a dummy cond branch. |
| 7212 | new BranchInst(II->getNormalDest(), II->getUnwindDest(), |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 7213 | ConstantInt::getTrue(), II); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 7214 | } |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 7215 | return EraseInstFromFunction(*CS.getInstruction()); |
| 7216 | } |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 7217 | |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 7218 | const PointerType *PTy = cast<PointerType>(Callee->getType()); |
| 7219 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
| 7220 | if (FTy->isVarArg()) { |
| 7221 | // See if we can optimize any arguments passed through the varargs area of |
| 7222 | // the call. |
| 7223 | for (CallSite::arg_iterator I = CS.arg_begin()+FTy->getNumParams(), |
| 7224 | E = CS.arg_end(); I != E; ++I) |
| 7225 | if (CastInst *CI = dyn_cast<CastInst>(*I)) { |
| 7226 | // If this cast does not effect the value passed through the varargs |
| 7227 | // area, we can eliminate the use of the cast. |
| 7228 | Value *Op = CI->getOperand(0); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7229 | if (CI->isLosslessCast()) { |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 7230 | *I = Op; |
| 7231 | Changed = true; |
| 7232 | } |
| 7233 | } |
| 7234 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7235 | |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 7236 | return Changed ? CS.getInstruction() : 0; |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 7237 | } |
| 7238 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7239 | // transformConstExprCastCall - If the callee is a constexpr cast of a function, |
| 7240 | // attempt to move the cast to the arguments of the call/invoke. |
| 7241 | // |
| 7242 | bool InstCombiner::transformConstExprCastCall(CallSite CS) { |
| 7243 | if (!isa<ConstantExpr>(CS.getCalledValue())) return false; |
| 7244 | ConstantExpr *CE = cast<ConstantExpr>(CS.getCalledValue()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7245 | if (CE->getOpcode() != Instruction::BitCast || |
| 7246 | !isa<Function>(CE->getOperand(0))) |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7247 | return false; |
Reid Spencer | 8863f18 | 2004-07-18 00:38:32 +0000 | [diff] [blame] | 7248 | Function *Callee = cast<Function>(CE->getOperand(0)); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7249 | Instruction *Caller = CS.getInstruction(); |
| 7250 | |
| 7251 | // Okay, this is a cast from a function to a different type. Unless doing so |
| 7252 | // would cause a type conversion of one of our arguments, change this call to |
| 7253 | // be a direct call with arguments casted to the appropriate types. |
| 7254 | // |
| 7255 | const FunctionType *FT = Callee->getFunctionType(); |
| 7256 | const Type *OldRetTy = Caller->getType(); |
| 7257 | |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 7258 | // Check to see if we are changing the return type... |
| 7259 | if (OldRetTy != FT->getReturnType()) { |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 7260 | if (Callee->isDeclaration() && !Caller->use_empty() && |
Chris Lattner | 46013f4 | 2007-01-06 19:53:32 +0000 | [diff] [blame] | 7261 | OldRetTy != FT->getReturnType() && |
| 7262 | // Conversion is ok if changing from pointer to int of same size. |
| 7263 | !(isa<PointerType>(FT->getReturnType()) && |
| 7264 | TD->getIntPtrType() == OldRetTy)) |
Chris Lattner | ec47992 | 2007-01-06 02:09:32 +0000 | [diff] [blame] | 7265 | return false; // Cannot transform this return value. |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 7266 | |
| 7267 | // If the callsite is an invoke instruction, and the return value is used by |
| 7268 | // a PHI node in a successor, we cannot change the return type of the call |
| 7269 | // because there is no place to put the cast instruction (without breaking |
| 7270 | // the critical edge). Bail out in this case. |
| 7271 | if (!Caller->use_empty()) |
| 7272 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) |
| 7273 | for (Value::use_iterator UI = II->use_begin(), E = II->use_end(); |
| 7274 | UI != E; ++UI) |
| 7275 | if (PHINode *PN = dyn_cast<PHINode>(*UI)) |
| 7276 | if (PN->getParent() == II->getNormalDest() || |
Chris Lattner | aeb2a1d | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 7277 | PN->getParent() == II->getUnwindDest()) |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 7278 | return false; |
| 7279 | } |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7280 | |
| 7281 | unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin()); |
| 7282 | unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7283 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7284 | CallSite::arg_iterator AI = CS.arg_begin(); |
| 7285 | for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) { |
| 7286 | const Type *ParamTy = FT->getParamType(i); |
Andrew Lenharth | b8e604c | 2006-06-28 01:01:52 +0000 | [diff] [blame] | 7287 | const Type *ActTy = (*AI)->getType(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7288 | ConstantInt *c = dyn_cast<ConstantInt>(*AI); |
Andrew Lenharth | b8e604c | 2006-06-28 01:01:52 +0000 | [diff] [blame] | 7289 | //Either we can cast directly, or we can upconvert the argument |
Chris Lattner | ec47992 | 2007-01-06 02:09:32 +0000 | [diff] [blame] | 7290 | bool isConvertible = ActTy == ParamTy || |
Chris Lattner | 46013f4 | 2007-01-06 19:53:32 +0000 | [diff] [blame] | 7291 | (isa<PointerType>(ParamTy) && isa<PointerType>(ActTy)) || |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 7292 | (ParamTy->isInteger() && ActTy->isInteger() && |
Reid Spencer | abaa8ca | 2007-01-08 16:32:00 +0000 | [diff] [blame] | 7293 | ParamTy->getPrimitiveSizeInBits() >= ActTy->getPrimitiveSizeInBits()) || |
| 7294 | (c && ParamTy->getPrimitiveSizeInBits() >= ActTy->getPrimitiveSizeInBits() |
| 7295 | && c->getSExtValue() > 0); |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 7296 | if (Callee->isDeclaration() && !isConvertible) return false; |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7297 | } |
| 7298 | |
| 7299 | if (FT->getNumParams() < NumActualArgs && !FT->isVarArg() && |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 7300 | Callee->isDeclaration()) |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7301 | return false; // Do not delete arguments unless we have a function body... |
| 7302 | |
| 7303 | // Okay, we decided that this is a safe thing to do: go ahead and start |
| 7304 | // inserting cast instructions as necessary... |
| 7305 | std::vector<Value*> Args; |
| 7306 | Args.reserve(NumActualArgs); |
| 7307 | |
| 7308 | AI = CS.arg_begin(); |
| 7309 | for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) { |
| 7310 | const Type *ParamTy = FT->getParamType(i); |
| 7311 | if ((*AI)->getType() == ParamTy) { |
| 7312 | Args.push_back(*AI); |
| 7313 | } else { |
Reid Spencer | 8a903db | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 7314 | Instruction::CastOps opcode = CastInst::getCastOpcode(*AI, |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 7315 | false, ParamTy, false); |
Reid Spencer | 8a903db | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 7316 | CastInst *NewCast = CastInst::create(opcode, *AI, ParamTy, "tmp"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7317 | Args.push_back(InsertNewInstBefore(NewCast, *Caller)); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7318 | } |
| 7319 | } |
| 7320 | |
| 7321 | // If the function takes more arguments than the call was taking, add them |
| 7322 | // now... |
| 7323 | for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i) |
| 7324 | Args.push_back(Constant::getNullValue(FT->getParamType(i))); |
| 7325 | |
| 7326 | // If we are removing arguments to the function, emit an obnoxious warning... |
| 7327 | if (FT->getNumParams() < NumActualArgs) |
| 7328 | if (!FT->isVarArg()) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 7329 | cerr << "WARNING: While resolving call to function '" |
| 7330 | << Callee->getName() << "' arguments were dropped!\n"; |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7331 | } else { |
| 7332 | // Add all of the arguments in their promoted form to the arg list... |
| 7333 | for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) { |
| 7334 | const Type *PTy = getPromotedType((*AI)->getType()); |
| 7335 | if (PTy != (*AI)->getType()) { |
| 7336 | // Must promote to pass through va_arg area! |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 7337 | Instruction::CastOps opcode = CastInst::getCastOpcode(*AI, false, |
| 7338 | PTy, false); |
Reid Spencer | 8a903db | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 7339 | Instruction *Cast = CastInst::create(opcode, *AI, PTy, "tmp"); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7340 | InsertNewInstBefore(Cast, *Caller); |
| 7341 | Args.push_back(Cast); |
| 7342 | } else { |
| 7343 | Args.push_back(*AI); |
| 7344 | } |
| 7345 | } |
| 7346 | } |
| 7347 | |
| 7348 | if (FT->getReturnType() == Type::VoidTy) |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 7349 | Caller->setName(""); // Void type should not have a name. |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7350 | |
| 7351 | Instruction *NC; |
| 7352 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
Chris Lattner | aeb2a1d | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 7353 | NC = new InvokeInst(Callee, II->getNormalDest(), II->getUnwindDest(), |
Chris Lattner | 93e985f | 2007-02-13 02:10:56 +0000 | [diff] [blame] | 7354 | &Args[0], Args.size(), Caller->getName(), Caller); |
Chris Lattner | e437026 | 2005-05-14 12:25:32 +0000 | [diff] [blame] | 7355 | cast<InvokeInst>(II)->setCallingConv(II->getCallingConv()); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7356 | } else { |
Chris Lattner | 93e985f | 2007-02-13 02:10:56 +0000 | [diff] [blame] | 7357 | NC = new CallInst(Callee, &Args[0], Args.size(), Caller->getName(), Caller); |
Chris Lattner | a9e9211 | 2005-05-06 06:48:21 +0000 | [diff] [blame] | 7358 | if (cast<CallInst>(Caller)->isTailCall()) |
| 7359 | cast<CallInst>(NC)->setTailCall(); |
Chris Lattner | e437026 | 2005-05-14 12:25:32 +0000 | [diff] [blame] | 7360 | cast<CallInst>(NC)->setCallingConv(cast<CallInst>(Caller)->getCallingConv()); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7361 | } |
| 7362 | |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 7363 | // Insert a cast of the return type as necessary. |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7364 | Value *NV = NC; |
| 7365 | if (Caller->getType() != NV->getType() && !Caller->use_empty()) { |
| 7366 | if (NV->getType() != Type::VoidTy) { |
Reid Spencer | 8a903db | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 7367 | const Type *CallerTy = Caller->getType(); |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 7368 | Instruction::CastOps opcode = CastInst::getCastOpcode(NC, false, |
| 7369 | CallerTy, false); |
Reid Spencer | 8a903db | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 7370 | NV = NC = CastInst::create(opcode, NC, CallerTy, "tmp"); |
Chris Lattner | bb60904 | 2003-10-30 00:46:41 +0000 | [diff] [blame] | 7371 | |
| 7372 | // If this is an invoke instruction, we should insert it after the first |
| 7373 | // non-phi, instruction in the normal successor block. |
| 7374 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
| 7375 | BasicBlock::iterator I = II->getNormalDest()->begin(); |
| 7376 | while (isa<PHINode>(I)) ++I; |
| 7377 | InsertNewInstBefore(NC, *I); |
| 7378 | } else { |
| 7379 | // Otherwise, it's a call, just insert cast right after the call instr |
| 7380 | InsertNewInstBefore(NC, *Caller); |
| 7381 | } |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 7382 | AddUsersToWorkList(*Caller); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7383 | } else { |
Chris Lattner | c30bda7 | 2004-10-17 21:22:38 +0000 | [diff] [blame] | 7384 | NV = UndefValue::get(Caller->getType()); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7385 | } |
| 7386 | } |
| 7387 | |
| 7388 | if (Caller->getType() != Type::VoidTy && !Caller->use_empty()) |
| 7389 | Caller->replaceAllUsesWith(NV); |
| 7390 | Caller->getParent()->getInstList().erase(Caller); |
| 7391 | removeFromWorkList(Caller); |
| 7392 | return true; |
| 7393 | } |
| 7394 | |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7395 | /// FoldPHIArgBinOpIntoPHI - If we have something like phi [add (a,b), add(c,d)] |
| 7396 | /// and if a/b/c/d and the add's all have a single use, turn this into two phi's |
| 7397 | /// and a single binop. |
| 7398 | Instruction *InstCombiner::FoldPHIArgBinOpIntoPHI(PHINode &PN) { |
| 7399 | Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0)); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7400 | assert(isa<BinaryOperator>(FirstInst) || isa<GetElementPtrInst>(FirstInst) || |
| 7401 | isa<CmpInst>(FirstInst)); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7402 | unsigned Opc = FirstInst->getOpcode(); |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 7403 | Value *LHSVal = FirstInst->getOperand(0); |
| 7404 | Value *RHSVal = FirstInst->getOperand(1); |
| 7405 | |
| 7406 | const Type *LHSType = LHSVal->getType(); |
| 7407 | const Type *RHSType = RHSVal->getType(); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7408 | |
| 7409 | // Scan to see if all operands are the same opcode, all have one use, and all |
| 7410 | // kill their operands (i.e. the operands have one use). |
Chris Lattner | a90a24c | 2006-11-01 04:55:47 +0000 | [diff] [blame] | 7411 | for (unsigned i = 0; i != PN.getNumIncomingValues(); ++i) { |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7412 | Instruction *I = dyn_cast<Instruction>(PN.getIncomingValue(i)); |
Chris Lattner | a90a24c | 2006-11-01 04:55:47 +0000 | [diff] [blame] | 7413 | if (!I || I->getOpcode() != Opc || !I->hasOneUse() || |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7414 | // Verify type of the LHS matches so we don't fold cmp's of different |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 7415 | // types or GEP's with different index types. |
| 7416 | I->getOperand(0)->getType() != LHSType || |
| 7417 | I->getOperand(1)->getType() != RHSType) |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7418 | return 0; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7419 | |
| 7420 | // If they are CmpInst instructions, check their predicates |
| 7421 | if (Opc == Instruction::ICmp || Opc == Instruction::FCmp) |
| 7422 | if (cast<CmpInst>(I)->getPredicate() != |
| 7423 | cast<CmpInst>(FirstInst)->getPredicate()) |
| 7424 | return 0; |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 7425 | |
| 7426 | // Keep track of which operand needs a phi node. |
| 7427 | if (I->getOperand(0) != LHSVal) LHSVal = 0; |
| 7428 | if (I->getOperand(1) != RHSVal) RHSVal = 0; |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7429 | } |
| 7430 | |
Chris Lattner | 53738a4 | 2006-11-08 19:42:28 +0000 | [diff] [blame] | 7431 | // Otherwise, this is safe to transform, determine if it is profitable. |
| 7432 | |
| 7433 | // If this is a GEP, and if the index (not the pointer) needs a PHI, bail out. |
| 7434 | // Indexes are often folded into load/store instructions, so we don't want to |
| 7435 | // hide them behind a phi. |
| 7436 | if (isa<GetElementPtrInst>(FirstInst) && RHSVal == 0) |
| 7437 | return 0; |
| 7438 | |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7439 | Value *InLHS = FirstInst->getOperand(0); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7440 | Value *InRHS = FirstInst->getOperand(1); |
Chris Lattner | 53738a4 | 2006-11-08 19:42:28 +0000 | [diff] [blame] | 7441 | PHINode *NewLHS = 0, *NewRHS = 0; |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 7442 | if (LHSVal == 0) { |
| 7443 | NewLHS = new PHINode(LHSType, FirstInst->getOperand(0)->getName()+".pn"); |
| 7444 | NewLHS->reserveOperandSpace(PN.getNumOperands()/2); |
| 7445 | NewLHS->addIncoming(InLHS, PN.getIncomingBlock(0)); |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 7446 | InsertNewInstBefore(NewLHS, PN); |
| 7447 | LHSVal = NewLHS; |
| 7448 | } |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 7449 | |
| 7450 | if (RHSVal == 0) { |
| 7451 | NewRHS = new PHINode(RHSType, FirstInst->getOperand(1)->getName()+".pn"); |
| 7452 | NewRHS->reserveOperandSpace(PN.getNumOperands()/2); |
| 7453 | NewRHS->addIncoming(InRHS, PN.getIncomingBlock(0)); |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 7454 | InsertNewInstBefore(NewRHS, PN); |
| 7455 | RHSVal = NewRHS; |
| 7456 | } |
| 7457 | |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 7458 | // Add all operands to the new PHIs. |
| 7459 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 7460 | if (NewLHS) { |
| 7461 | Value *NewInLHS =cast<Instruction>(PN.getIncomingValue(i))->getOperand(0); |
| 7462 | NewLHS->addIncoming(NewInLHS, PN.getIncomingBlock(i)); |
| 7463 | } |
| 7464 | if (NewRHS) { |
| 7465 | Value *NewInRHS =cast<Instruction>(PN.getIncomingValue(i))->getOperand(1); |
| 7466 | NewRHS->addIncoming(NewInRHS, PN.getIncomingBlock(i)); |
| 7467 | } |
| 7468 | } |
| 7469 | |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7470 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 7471 | return BinaryOperator::create(BinOp->getOpcode(), LHSVal, RHSVal); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7472 | else if (CmpInst *CIOp = dyn_cast<CmpInst>(FirstInst)) |
| 7473 | return CmpInst::create(CIOp->getOpcode(), CIOp->getPredicate(), LHSVal, |
| 7474 | RHSVal); |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 7475 | else { |
| 7476 | assert(isa<GetElementPtrInst>(FirstInst)); |
| 7477 | return new GetElementPtrInst(LHSVal, RHSVal); |
| 7478 | } |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7479 | } |
| 7480 | |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 7481 | /// isSafeToSinkLoad - Return true if we know that it is safe sink the load out |
| 7482 | /// of the block that defines it. This means that it must be obvious the value |
| 7483 | /// of the load is not changed from the point of the load to the end of the |
| 7484 | /// block it is in. |
Chris Lattner | fd905ca | 2007-02-01 22:30:07 +0000 | [diff] [blame] | 7485 | /// |
| 7486 | /// Finally, it is safe, but not profitable, to sink a load targetting a |
| 7487 | /// non-address-taken alloca. Doing so will cause us to not promote the alloca |
| 7488 | /// to a register. |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 7489 | static bool isSafeToSinkLoad(LoadInst *L) { |
| 7490 | BasicBlock::iterator BBI = L, E = L->getParent()->end(); |
| 7491 | |
| 7492 | for (++BBI; BBI != E; ++BBI) |
| 7493 | if (BBI->mayWriteToMemory()) |
| 7494 | return false; |
Chris Lattner | fd905ca | 2007-02-01 22:30:07 +0000 | [diff] [blame] | 7495 | |
| 7496 | // Check for non-address taken alloca. If not address-taken already, it isn't |
| 7497 | // profitable to do this xform. |
| 7498 | if (AllocaInst *AI = dyn_cast<AllocaInst>(L->getOperand(0))) { |
| 7499 | bool isAddressTaken = false; |
| 7500 | for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); |
| 7501 | UI != E; ++UI) { |
| 7502 | if (isa<LoadInst>(UI)) continue; |
| 7503 | if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) { |
| 7504 | // If storing TO the alloca, then the address isn't taken. |
| 7505 | if (SI->getOperand(1) == AI) continue; |
| 7506 | } |
| 7507 | isAddressTaken = true; |
| 7508 | break; |
| 7509 | } |
| 7510 | |
| 7511 | if (!isAddressTaken) |
| 7512 | return false; |
| 7513 | } |
| 7514 | |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 7515 | return true; |
| 7516 | } |
| 7517 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7518 | |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 7519 | // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary" |
| 7520 | // operator and they all are only used by the PHI, PHI together their |
| 7521 | // inputs, and do the operation once, to the result of the PHI. |
| 7522 | Instruction *InstCombiner::FoldPHIArgOpIntoPHI(PHINode &PN) { |
| 7523 | Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0)); |
| 7524 | |
| 7525 | // Scan the instruction, looking for input operations that can be folded away. |
| 7526 | // If all input operands to the phi are the same instruction (e.g. a cast from |
| 7527 | // the same type or "+42") we can pull the operation through the PHI, reducing |
| 7528 | // code size and simplifying code. |
| 7529 | Constant *ConstantOp = 0; |
| 7530 | const Type *CastSrcTy = 0; |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 7531 | bool isVolatile = false; |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 7532 | if (isa<CastInst>(FirstInst)) { |
| 7533 | CastSrcTy = FirstInst->getOperand(0)->getType(); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7534 | } else if (isa<BinaryOperator>(FirstInst) || isa<CmpInst>(FirstInst)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7535 | // Can fold binop, compare or shift here if the RHS is a constant, |
| 7536 | // otherwise call FoldPHIArgBinOpIntoPHI. |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 7537 | ConstantOp = dyn_cast<Constant>(FirstInst->getOperand(1)); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7538 | if (ConstantOp == 0) |
| 7539 | return FoldPHIArgBinOpIntoPHI(PN); |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 7540 | } else if (LoadInst *LI = dyn_cast<LoadInst>(FirstInst)) { |
| 7541 | isVolatile = LI->isVolatile(); |
| 7542 | // We can't sink the load if the loaded value could be modified between the |
| 7543 | // load and the PHI. |
| 7544 | if (LI->getParent() != PN.getIncomingBlock(0) || |
| 7545 | !isSafeToSinkLoad(LI)) |
| 7546 | return 0; |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 7547 | } else if (isa<GetElementPtrInst>(FirstInst)) { |
Chris Lattner | 53738a4 | 2006-11-08 19:42:28 +0000 | [diff] [blame] | 7548 | if (FirstInst->getNumOperands() == 2) |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 7549 | return FoldPHIArgBinOpIntoPHI(PN); |
| 7550 | // Can't handle general GEPs yet. |
| 7551 | return 0; |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 7552 | } else { |
| 7553 | return 0; // Cannot fold this operation. |
| 7554 | } |
| 7555 | |
| 7556 | // Check to see if all arguments are the same operation. |
| 7557 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 7558 | if (!isa<Instruction>(PN.getIncomingValue(i))) return 0; |
| 7559 | Instruction *I = cast<Instruction>(PN.getIncomingValue(i)); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7560 | if (!I->hasOneUse() || !I->isSameOperationAs(FirstInst)) |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 7561 | return 0; |
| 7562 | if (CastSrcTy) { |
| 7563 | if (I->getOperand(0)->getType() != CastSrcTy) |
| 7564 | return 0; // Cast operation must match. |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 7565 | } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7566 | // We can't sink the load if the loaded value could be modified between |
| 7567 | // the load and the PHI. |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 7568 | if (LI->isVolatile() != isVolatile || |
| 7569 | LI->getParent() != PN.getIncomingBlock(i) || |
| 7570 | !isSafeToSinkLoad(LI)) |
| 7571 | return 0; |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 7572 | } else if (I->getOperand(1) != ConstantOp) { |
| 7573 | return 0; |
| 7574 | } |
| 7575 | } |
| 7576 | |
| 7577 | // Okay, they are all the same operation. Create a new PHI node of the |
| 7578 | // correct type, and PHI together all of the LHS's of the instructions. |
| 7579 | PHINode *NewPN = new PHINode(FirstInst->getOperand(0)->getType(), |
| 7580 | PN.getName()+".in"); |
Chris Lattner | 5551706 | 2005-01-29 00:39:08 +0000 | [diff] [blame] | 7581 | NewPN->reserveOperandSpace(PN.getNumOperands()/2); |
Chris Lattner | b589344 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 7582 | |
| 7583 | Value *InVal = FirstInst->getOperand(0); |
| 7584 | NewPN->addIncoming(InVal, PN.getIncomingBlock(0)); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 7585 | |
| 7586 | // Add all operands to the new PHI. |
Chris Lattner | b589344 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 7587 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 7588 | Value *NewInVal = cast<Instruction>(PN.getIncomingValue(i))->getOperand(0); |
| 7589 | if (NewInVal != InVal) |
| 7590 | InVal = 0; |
| 7591 | NewPN->addIncoming(NewInVal, PN.getIncomingBlock(i)); |
| 7592 | } |
| 7593 | |
| 7594 | Value *PhiVal; |
| 7595 | if (InVal) { |
| 7596 | // The new PHI unions all of the same values together. This is really |
| 7597 | // common, so we handle it intelligently here for compile-time speed. |
| 7598 | PhiVal = InVal; |
| 7599 | delete NewPN; |
| 7600 | } else { |
| 7601 | InsertNewInstBefore(NewPN, PN); |
| 7602 | PhiVal = NewPN; |
| 7603 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7604 | |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 7605 | // Insert and return the new operation. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7606 | if (CastInst* FirstCI = dyn_cast<CastInst>(FirstInst)) |
| 7607 | return CastInst::create(FirstCI->getOpcode(), PhiVal, PN.getType()); |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 7608 | else if (isa<LoadInst>(FirstInst)) |
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 7609 | return new LoadInst(PhiVal, "", isVolatile); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 7610 | else if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) |
Chris Lattner | b589344 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 7611 | return BinaryOperator::create(BinOp->getOpcode(), PhiVal, ConstantOp); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7612 | else if (CmpInst *CIOp = dyn_cast<CmpInst>(FirstInst)) |
| 7613 | return CmpInst::create(CIOp->getOpcode(), CIOp->getPredicate(), |
| 7614 | PhiVal, ConstantOp); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 7615 | else |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7616 | assert(0 && "Unknown operation"); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 7617 | } |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 7618 | |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 7619 | /// DeadPHICycle - Return true if this PHI node is only used by a PHI node cycle |
| 7620 | /// that is dead. |
| 7621 | static bool DeadPHICycle(PHINode *PN, std::set<PHINode*> &PotentiallyDeadPHIs) { |
| 7622 | if (PN->use_empty()) return true; |
| 7623 | if (!PN->hasOneUse()) return false; |
| 7624 | |
| 7625 | // Remember this node, and if we find the cycle, return. |
| 7626 | if (!PotentiallyDeadPHIs.insert(PN).second) |
| 7627 | return true; |
| 7628 | |
| 7629 | if (PHINode *PU = dyn_cast<PHINode>(PN->use_back())) |
| 7630 | return DeadPHICycle(PU, PotentiallyDeadPHIs); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7631 | |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 7632 | return false; |
| 7633 | } |
| 7634 | |
Chris Lattner | 473945d | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 7635 | // PHINode simplification |
| 7636 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 7637 | Instruction *InstCombiner::visitPHINode(PHINode &PN) { |
Owen Anderson | b64ab87 | 2006-07-10 22:15:25 +0000 | [diff] [blame] | 7638 | // If LCSSA is around, don't mess with Phi nodes |
| 7639 | if (mustPreserveAnalysisID(LCSSAID)) return 0; |
Owen Anderson | d1b78a1 | 2006-07-10 19:03:49 +0000 | [diff] [blame] | 7640 | |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 7641 | if (Value *V = PN.hasConstantValue()) |
| 7642 | return ReplaceInstUsesWith(PN, V); |
| 7643 | |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 7644 | // If all PHI operands are the same operation, pull them through the PHI, |
| 7645 | // reducing code size. |
| 7646 | if (isa<Instruction>(PN.getIncomingValue(0)) && |
| 7647 | PN.getIncomingValue(0)->hasOneUse()) |
| 7648 | if (Instruction *Result = FoldPHIArgOpIntoPHI(PN)) |
| 7649 | return Result; |
| 7650 | |
| 7651 | // If this is a trivial cycle in the PHI node graph, remove it. Basically, if |
| 7652 | // this PHI only has a single use (a PHI), and if that PHI only has one use (a |
| 7653 | // PHI)... break the cycle. |
Chris Lattner | ff9f13a | 2007-01-15 07:30:06 +0000 | [diff] [blame] | 7654 | if (PN.hasOneUse()) { |
| 7655 | Instruction *PHIUser = cast<Instruction>(PN.use_back()); |
| 7656 | if (PHINode *PU = dyn_cast<PHINode>(PHIUser)) { |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 7657 | std::set<PHINode*> PotentiallyDeadPHIs; |
| 7658 | PotentiallyDeadPHIs.insert(&PN); |
| 7659 | if (DeadPHICycle(PU, PotentiallyDeadPHIs)) |
| 7660 | return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType())); |
| 7661 | } |
Chris Lattner | ff9f13a | 2007-01-15 07:30:06 +0000 | [diff] [blame] | 7662 | |
| 7663 | // If this phi has a single use, and if that use just computes a value for |
| 7664 | // the next iteration of a loop, delete the phi. This occurs with unused |
| 7665 | // induction variables, e.g. "for (int j = 0; ; ++j);". Detecting this |
| 7666 | // common case here is good because the only other things that catch this |
| 7667 | // are induction variable analysis (sometimes) and ADCE, which is only run |
| 7668 | // late. |
| 7669 | if (PHIUser->hasOneUse() && |
| 7670 | (isa<BinaryOperator>(PHIUser) || isa<GetElementPtrInst>(PHIUser)) && |
| 7671 | PHIUser->use_back() == &PN) { |
| 7672 | return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType())); |
| 7673 | } |
| 7674 | } |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 7675 | |
Chris Lattner | 60921c9 | 2003-12-19 05:58:40 +0000 | [diff] [blame] | 7676 | return 0; |
Chris Lattner | 473945d | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 7677 | } |
| 7678 | |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7679 | static Value *InsertCastToIntPtrTy(Value *V, const Type *DTy, |
| 7680 | Instruction *InsertPoint, |
| 7681 | InstCombiner *IC) { |
Reid Spencer | abaa8ca | 2007-01-08 16:32:00 +0000 | [diff] [blame] | 7682 | unsigned PtrSize = DTy->getPrimitiveSizeInBits(); |
| 7683 | unsigned VTySize = V->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7684 | // We must cast correctly to the pointer type. Ensure that we |
| 7685 | // sign extend the integer value if it is smaller as this is |
| 7686 | // used for address computation. |
| 7687 | Instruction::CastOps opcode = |
| 7688 | (VTySize < PtrSize ? Instruction::SExt : |
| 7689 | (VTySize == PtrSize ? Instruction::BitCast : Instruction::Trunc)); |
| 7690 | return IC->InsertCastBefore(opcode, V, DTy, *InsertPoint); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7691 | } |
| 7692 | |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 7693 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 7694 | Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7695 | Value *PtrOp = GEP.getOperand(0); |
Chris Lattner | c54e2b8 | 2003-05-22 19:07:21 +0000 | [diff] [blame] | 7696 | // Is it 'getelementptr %P, long 0' or 'getelementptr %P' |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 7697 | // If so, eliminate the noop. |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 7698 | if (GEP.getNumOperands() == 1) |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7699 | return ReplaceInstUsesWith(GEP, PtrOp); |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 7700 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 7701 | if (isa<UndefValue>(GEP.getOperand(0))) |
| 7702 | return ReplaceInstUsesWith(GEP, UndefValue::get(GEP.getType())); |
| 7703 | |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 7704 | bool HasZeroPointerIndex = false; |
| 7705 | if (Constant *C = dyn_cast<Constant>(GEP.getOperand(1))) |
| 7706 | HasZeroPointerIndex = C->isNullValue(); |
| 7707 | |
| 7708 | if (GEP.getNumOperands() == 2 && HasZeroPointerIndex) |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7709 | return ReplaceInstUsesWith(GEP, PtrOp); |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 7710 | |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7711 | // Eliminate unneeded casts for indices. |
| 7712 | bool MadeChange = false; |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 7713 | gep_type_iterator GTI = gep_type_begin(GEP); |
| 7714 | for (unsigned i = 1, e = GEP.getNumOperands(); i != e; ++i, ++GTI) |
| 7715 | if (isa<SequentialType>(*GTI)) { |
| 7716 | if (CastInst *CI = dyn_cast<CastInst>(GEP.getOperand(i))) { |
Chris Lattner | 76b7a06 | 2007-01-15 07:02:54 +0000 | [diff] [blame] | 7717 | if (CI->getOpcode() == Instruction::ZExt || |
| 7718 | CI->getOpcode() == Instruction::SExt) { |
| 7719 | const Type *SrcTy = CI->getOperand(0)->getType(); |
| 7720 | // We can eliminate a cast from i32 to i64 iff the target |
| 7721 | // is a 32-bit pointer target. |
| 7722 | if (SrcTy->getPrimitiveSizeInBits() >= TD->getPointerSizeInBits()) { |
| 7723 | MadeChange = true; |
| 7724 | GEP.setOperand(i, CI->getOperand(0)); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7725 | } |
| 7726 | } |
| 7727 | } |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 7728 | // If we are using a wider index than needed for this platform, shrink it |
| 7729 | // to what we need. If the incoming value needs a cast instruction, |
| 7730 | // insert it. This explicit cast can make subsequent optimizations more |
| 7731 | // obvious. |
| 7732 | Value *Op = GEP.getOperand(i); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 7733 | if (TD->getTypeSize(Op->getType()) > TD->getPointerSize()) |
Chris Lattner | 4f1134e | 2004-04-17 18:16:10 +0000 | [diff] [blame] | 7734 | if (Constant *C = dyn_cast<Constant>(Op)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7735 | GEP.setOperand(i, ConstantExpr::getTrunc(C, TD->getIntPtrType())); |
Chris Lattner | 4f1134e | 2004-04-17 18:16:10 +0000 | [diff] [blame] | 7736 | MadeChange = true; |
| 7737 | } else { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7738 | Op = InsertCastBefore(Instruction::Trunc, Op, TD->getIntPtrType(), |
| 7739 | GEP); |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 7740 | GEP.setOperand(i, Op); |
| 7741 | MadeChange = true; |
| 7742 | } |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7743 | } |
| 7744 | if (MadeChange) return &GEP; |
| 7745 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 7746 | // Combine Indices - If the source pointer to this getelementptr instruction |
| 7747 | // is a getelementptr instruction, combine the indices of the two |
| 7748 | // getelementptr instructions into a single instruction. |
| 7749 | // |
Chris Lattner | ebd985c | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 7750 | std::vector<Value*> SrcGEPOperands; |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 7751 | if (User *Src = dyn_castGetElementPtr(PtrOp)) |
Chris Lattner | ebd985c | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 7752 | SrcGEPOperands.assign(Src->op_begin(), Src->op_end()); |
Chris Lattner | ebd985c | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 7753 | |
| 7754 | if (!SrcGEPOperands.empty()) { |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7755 | // Note that if our source is a gep chain itself that we wait for that |
| 7756 | // chain to be resolved before we perform this transformation. This |
| 7757 | // avoids us creating a TON of code in some cases. |
| 7758 | // |
| 7759 | if (isa<GetElementPtrInst>(SrcGEPOperands[0]) && |
| 7760 | cast<Instruction>(SrcGEPOperands[0])->getNumOperands() == 2) |
| 7761 | return 0; // Wait until our source is folded to completion. |
| 7762 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 7763 | std::vector<Value *> Indices; |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7764 | |
| 7765 | // Find out whether the last index in the source GEP is a sequential idx. |
| 7766 | bool EndsWithSequential = false; |
| 7767 | for (gep_type_iterator I = gep_type_begin(*cast<User>(PtrOp)), |
| 7768 | E = gep_type_end(*cast<User>(PtrOp)); I != E; ++I) |
Chris Lattner | be97b4e | 2004-05-08 22:41:42 +0000 | [diff] [blame] | 7769 | EndsWithSequential = !isa<StructType>(*I); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7770 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 7771 | // Can we combine the two pointer arithmetics offsets? |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7772 | if (EndsWithSequential) { |
Chris Lattner | decd081 | 2003-03-05 22:33:14 +0000 | [diff] [blame] | 7773 | // Replace: gep (gep %P, long B), long A, ... |
| 7774 | // With: T = long A+B; gep %P, T, ... |
| 7775 | // |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7776 | Value *Sum, *SO1 = SrcGEPOperands.back(), *GO1 = GEP.getOperand(1); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7777 | if (SO1 == Constant::getNullValue(SO1->getType())) { |
| 7778 | Sum = GO1; |
| 7779 | } else if (GO1 == Constant::getNullValue(GO1->getType())) { |
| 7780 | Sum = SO1; |
| 7781 | } else { |
| 7782 | // If they aren't the same type, convert both to an integer of the |
| 7783 | // target's pointer size. |
| 7784 | if (SO1->getType() != GO1->getType()) { |
| 7785 | if (Constant *SO1C = dyn_cast<Constant>(SO1)) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7786 | SO1 = ConstantExpr::getIntegerCast(SO1C, GO1->getType(), true); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7787 | } else if (Constant *GO1C = dyn_cast<Constant>(GO1)) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7788 | GO1 = ConstantExpr::getIntegerCast(GO1C, SO1->getType(), true); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7789 | } else { |
| 7790 | unsigned PS = TD->getPointerSize(); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 7791 | if (TD->getTypeSize(SO1->getType()) == PS) { |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7792 | // Convert GO1 to SO1's type. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7793 | GO1 = InsertCastToIntPtrTy(GO1, SO1->getType(), &GEP, this); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7794 | |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 7795 | } else if (TD->getTypeSize(GO1->getType()) == PS) { |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7796 | // Convert SO1 to GO1's type. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7797 | SO1 = InsertCastToIntPtrTy(SO1, GO1->getType(), &GEP, this); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7798 | } else { |
| 7799 | const Type *PT = TD->getIntPtrType(); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7800 | SO1 = InsertCastToIntPtrTy(SO1, PT, &GEP, this); |
| 7801 | GO1 = InsertCastToIntPtrTy(GO1, PT, &GEP, this); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7802 | } |
| 7803 | } |
| 7804 | } |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7805 | if (isa<Constant>(SO1) && isa<Constant>(GO1)) |
| 7806 | Sum = ConstantExpr::getAdd(cast<Constant>(SO1), cast<Constant>(GO1)); |
| 7807 | else { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 7808 | Sum = BinaryOperator::createAdd(SO1, GO1, PtrOp->getName()+".sum"); |
| 7809 | InsertNewInstBefore(cast<Instruction>(Sum), GEP); |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7810 | } |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7811 | } |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7812 | |
| 7813 | // Recycle the GEP we already have if possible. |
| 7814 | if (SrcGEPOperands.size() == 2) { |
| 7815 | GEP.setOperand(0, SrcGEPOperands[0]); |
| 7816 | GEP.setOperand(1, Sum); |
| 7817 | return &GEP; |
| 7818 | } else { |
| 7819 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, |
| 7820 | SrcGEPOperands.end()-1); |
| 7821 | Indices.push_back(Sum); |
| 7822 | Indices.insert(Indices.end(), GEP.op_begin()+2, GEP.op_end()); |
| 7823 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7824 | } else if (isa<Constant>(*GEP.idx_begin()) && |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7825 | cast<Constant>(*GEP.idx_begin())->isNullValue() && |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7826 | SrcGEPOperands.size() != 1) { |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 7827 | // Otherwise we can do the fold if the first index of the GEP is a zero |
Chris Lattner | ebd985c | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 7828 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, |
| 7829 | SrcGEPOperands.end()); |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 7830 | Indices.insert(Indices.end(), GEP.idx_begin()+1, GEP.idx_end()); |
| 7831 | } |
| 7832 | |
| 7833 | if (!Indices.empty()) |
Chris Lattner | 1ccd185 | 2007-02-12 22:56:41 +0000 | [diff] [blame] | 7834 | return new GetElementPtrInst(SrcGEPOperands[0], &Indices[0], |
| 7835 | Indices.size(), GEP.getName()); |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 7836 | |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7837 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(PtrOp)) { |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 7838 | // GEP of global variable. If all of the indices for this GEP are |
| 7839 | // constants, we can promote this to a constexpr instead of an instruction. |
| 7840 | |
| 7841 | // Scan for nonconstants... |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 7842 | SmallVector<Constant*, 8> Indices; |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 7843 | User::op_iterator I = GEP.idx_begin(), E = GEP.idx_end(); |
| 7844 | for (; I != E && isa<Constant>(*I); ++I) |
| 7845 | Indices.push_back(cast<Constant>(*I)); |
| 7846 | |
| 7847 | if (I == E) { // If they are all constants... |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 7848 | Constant *CE = ConstantExpr::getGetElementPtr(GV, |
| 7849 | &Indices[0],Indices.size()); |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 7850 | |
| 7851 | // Replace all uses of the GEP with the new constexpr... |
| 7852 | return ReplaceInstUsesWith(GEP, CE); |
| 7853 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7854 | } else if (Value *X = getBitCastOperand(PtrOp)) { // Is the operand a cast? |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 7855 | if (!isa<PointerType>(X->getType())) { |
| 7856 | // Not interesting. Source pointer must be a cast from pointer. |
| 7857 | } else if (HasZeroPointerIndex) { |
| 7858 | // transform: GEP (cast [10 x ubyte]* X to [0 x ubyte]*), long 0, ... |
| 7859 | // into : GEP [10 x ubyte]* X, long 0, ... |
| 7860 | // |
| 7861 | // This occurs when the program declares an array extern like "int X[];" |
| 7862 | // |
| 7863 | const PointerType *CPTy = cast<PointerType>(PtrOp->getType()); |
| 7864 | const PointerType *XTy = cast<PointerType>(X->getType()); |
| 7865 | if (const ArrayType *XATy = |
| 7866 | dyn_cast<ArrayType>(XTy->getElementType())) |
| 7867 | if (const ArrayType *CATy = |
| 7868 | dyn_cast<ArrayType>(CPTy->getElementType())) |
| 7869 | if (CATy->getElementType() == XATy->getElementType()) { |
| 7870 | // At this point, we know that the cast source type is a pointer |
| 7871 | // to an array of the same type as the destination pointer |
| 7872 | // array. Because the array type is never stepped over (there |
| 7873 | // is a leading zero) we can fold the cast into this GEP. |
| 7874 | GEP.setOperand(0, X); |
| 7875 | return &GEP; |
| 7876 | } |
| 7877 | } else if (GEP.getNumOperands() == 2) { |
| 7878 | // Transform things like: |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 7879 | // %t = getelementptr ubyte* cast ([2 x int]* %str to uint*), uint %V |
| 7880 | // into: %t1 = getelementptr [2 x int*]* %str, int 0, uint %V; cast |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 7881 | const Type *SrcElTy = cast<PointerType>(X->getType())->getElementType(); |
| 7882 | const Type *ResElTy=cast<PointerType>(PtrOp->getType())->getElementType(); |
| 7883 | if (isa<ArrayType>(SrcElTy) && |
| 7884 | TD->getTypeSize(cast<ArrayType>(SrcElTy)->getElementType()) == |
| 7885 | TD->getTypeSize(ResElTy)) { |
| 7886 | Value *V = InsertNewInstBefore( |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 7887 | new GetElementPtrInst(X, Constant::getNullValue(Type::Int32Ty), |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 7888 | GEP.getOperand(1), GEP.getName()), GEP); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7889 | // V and GEP are both pointer types --> BitCast |
| 7890 | return new BitCastInst(V, GEP.getType()); |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 7891 | } |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 7892 | |
| 7893 | // Transform things like: |
| 7894 | // getelementptr sbyte* cast ([100 x double]* X to sbyte*), int %tmp |
| 7895 | // (where tmp = 8*tmp2) into: |
| 7896 | // getelementptr [100 x double]* %arr, int 0, int %tmp.2 |
| 7897 | |
| 7898 | if (isa<ArrayType>(SrcElTy) && |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 7899 | (ResElTy == Type::Int8Ty || ResElTy == Type::Int8Ty)) { |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 7900 | uint64_t ArrayEltSize = |
| 7901 | TD->getTypeSize(cast<ArrayType>(SrcElTy)->getElementType()); |
| 7902 | |
| 7903 | // Check to see if "tmp" is a scale by a multiple of ArrayEltSize. We |
| 7904 | // allow either a mul, shift, or constant here. |
| 7905 | Value *NewIdx = 0; |
| 7906 | ConstantInt *Scale = 0; |
| 7907 | if (ArrayEltSize == 1) { |
| 7908 | NewIdx = GEP.getOperand(1); |
| 7909 | Scale = ConstantInt::get(NewIdx->getType(), 1); |
| 7910 | } else if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP.getOperand(1))) { |
Chris Lattner | 6e2f843 | 2005-09-14 17:32:56 +0000 | [diff] [blame] | 7911 | NewIdx = ConstantInt::get(CI->getType(), 1); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 7912 | Scale = CI; |
| 7913 | } else if (Instruction *Inst =dyn_cast<Instruction>(GEP.getOperand(1))){ |
| 7914 | if (Inst->getOpcode() == Instruction::Shl && |
| 7915 | isa<ConstantInt>(Inst->getOperand(1))) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7916 | unsigned ShAmt = |
| 7917 | cast<ConstantInt>(Inst->getOperand(1))->getZExtValue(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7918 | Scale = ConstantInt::get(Inst->getType(), 1ULL << ShAmt); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 7919 | NewIdx = Inst->getOperand(0); |
| 7920 | } else if (Inst->getOpcode() == Instruction::Mul && |
| 7921 | isa<ConstantInt>(Inst->getOperand(1))) { |
| 7922 | Scale = cast<ConstantInt>(Inst->getOperand(1)); |
| 7923 | NewIdx = Inst->getOperand(0); |
| 7924 | } |
| 7925 | } |
| 7926 | |
| 7927 | // If the index will be to exactly the right offset with the scale taken |
| 7928 | // out, perform the transformation. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7929 | if (Scale && Scale->getZExtValue() % ArrayEltSize == 0) { |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 7930 | if (isa<ConstantInt>(Scale)) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7931 | Scale = ConstantInt::get(Scale->getType(), |
| 7932 | Scale->getZExtValue() / ArrayEltSize); |
| 7933 | if (Scale->getZExtValue() != 1) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7934 | Constant *C = ConstantExpr::getIntegerCast(Scale, NewIdx->getType(), |
| 7935 | true /*SExt*/); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 7936 | Instruction *Sc = BinaryOperator::createMul(NewIdx, C, "idxscale"); |
| 7937 | NewIdx = InsertNewInstBefore(Sc, GEP); |
| 7938 | } |
| 7939 | |
| 7940 | // Insert the new GEP instruction. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7941 | Instruction *NewGEP = |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 7942 | new GetElementPtrInst(X, Constant::getNullValue(Type::Int32Ty), |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 7943 | NewIdx, GEP.getName()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7944 | NewGEP = InsertNewInstBefore(NewGEP, GEP); |
| 7945 | // The NewGEP must be pointer typed, so must the old one -> BitCast |
| 7946 | return new BitCastInst(NewGEP, GEP.getType()); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 7947 | } |
| 7948 | } |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 7949 | } |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 7950 | } |
| 7951 | |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 7952 | return 0; |
| 7953 | } |
| 7954 | |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 7955 | Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) { |
| 7956 | // Convert: malloc Ty, C - where C is a constant != 1 into: malloc [C x Ty], 1 |
| 7957 | if (AI.isArrayAllocation()) // Check C != 1 |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7958 | if (const ConstantInt *C = dyn_cast<ConstantInt>(AI.getArraySize())) { |
| 7959 | const Type *NewTy = |
| 7960 | ArrayType::get(AI.getAllocatedType(), C->getZExtValue()); |
Chris Lattner | 0006bd7 | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 7961 | AllocationInst *New = 0; |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 7962 | |
| 7963 | // Create and insert the replacement instruction... |
| 7964 | if (isa<MallocInst>(AI)) |
Nate Begeman | 14b0529 | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 7965 | New = new MallocInst(NewTy, 0, AI.getAlignment(), AI.getName()); |
Chris Lattner | 0006bd7 | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 7966 | else { |
| 7967 | assert(isa<AllocaInst>(AI) && "Unknown type of allocation inst!"); |
Nate Begeman | 14b0529 | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 7968 | New = new AllocaInst(NewTy, 0, AI.getAlignment(), AI.getName()); |
Chris Lattner | 0006bd7 | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 7969 | } |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 7970 | |
| 7971 | InsertNewInstBefore(New, AI); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7972 | |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 7973 | // Scan to the end of the allocation instructions, to skip over a block of |
| 7974 | // allocas if possible... |
| 7975 | // |
| 7976 | BasicBlock::iterator It = New; |
| 7977 | while (isa<AllocationInst>(*It)) ++It; |
| 7978 | |
| 7979 | // Now that I is pointing to the first non-allocation-inst in the block, |
| 7980 | // insert our getelementptr instruction... |
| 7981 | // |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 7982 | Value *NullIdx = Constant::getNullValue(Type::Int32Ty); |
Chris Lattner | 693787a | 2005-05-04 19:10:26 +0000 | [diff] [blame] | 7983 | Value *V = new GetElementPtrInst(New, NullIdx, NullIdx, |
| 7984 | New->getName()+".sub", It); |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 7985 | |
| 7986 | // Now make everything use the getelementptr instead of the original |
| 7987 | // allocation. |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 7988 | return ReplaceInstUsesWith(AI, V); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 7989 | } else if (isa<UndefValue>(AI.getArraySize())) { |
| 7990 | return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 7991 | } |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 7992 | |
| 7993 | // If alloca'ing a zero byte object, replace the alloca with a null pointer. |
| 7994 | // Note that we only do this for alloca's, because malloc should allocate and |
| 7995 | // return a unique pointer, even for a zero byte allocation. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7996 | if (isa<AllocaInst>(AI) && AI.getAllocatedType()->isSized() && |
Chris Lattner | cf27afb | 2004-07-02 22:55:47 +0000 | [diff] [blame] | 7997 | TD->getTypeSize(AI.getAllocatedType()) == 0) |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 7998 | return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); |
| 7999 | |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 8000 | return 0; |
| 8001 | } |
| 8002 | |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 8003 | Instruction *InstCombiner::visitFreeInst(FreeInst &FI) { |
| 8004 | Value *Op = FI.getOperand(0); |
| 8005 | |
| 8006 | // Change free <ty>* (cast <ty2>* X to <ty>*) into free <ty2>* X |
| 8007 | if (CastInst *CI = dyn_cast<CastInst>(Op)) |
| 8008 | if (isa<PointerType>(CI->getOperand(0)->getType())) { |
| 8009 | FI.setOperand(0, CI->getOperand(0)); |
| 8010 | return &FI; |
| 8011 | } |
| 8012 | |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8013 | // free undef -> unreachable. |
| 8014 | if (isa<UndefValue>(Op)) { |
| 8015 | // Insert a new store to null because we cannot modify the CFG here. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 8016 | new StoreInst(ConstantInt::getTrue(), |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 8017 | UndefValue::get(PointerType::get(Type::Int1Ty)), &FI); |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8018 | return EraseInstFromFunction(FI); |
| 8019 | } |
| 8020 | |
Chris Lattner | 6160e85 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 8021 | // If we have 'free null' delete the instruction. This can happen in stl code |
| 8022 | // when lots of inlining happens. |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8023 | if (isa<ConstantPointerNull>(Op)) |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 8024 | return EraseInstFromFunction(FI); |
Chris Lattner | 6160e85 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 8025 | |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 8026 | return 0; |
| 8027 | } |
| 8028 | |
| 8029 | |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8030 | /// InstCombineLoadCast - Fold 'load (cast P)' -> cast (load P)' when possible. |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 8031 | static Instruction *InstCombineLoadCast(InstCombiner &IC, LoadInst &LI) { |
| 8032 | User *CI = cast<User>(LI.getOperand(0)); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 8033 | Value *CastOp = CI->getOperand(0); |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 8034 | |
| 8035 | const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType(); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 8036 | if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) { |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 8037 | const Type *SrcPTy = SrcTy->getElementType(); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 8038 | |
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 8039 | if (DestPTy->isInteger() || isa<PointerType>(DestPTy) || |
| 8040 | isa<PackedType>(DestPTy)) { |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 8041 | // If the source is an array, the code below will not succeed. Check to |
| 8042 | // see if a trivial 'gep P, 0, 0' will help matters. Only do this for |
| 8043 | // constants. |
| 8044 | if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy)) |
| 8045 | if (Constant *CSrc = dyn_cast<Constant>(CastOp)) |
| 8046 | if (ASrcTy->getNumElements() != 0) { |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 8047 | Value *Idxs[2]; |
| 8048 | Idxs[0] = Idxs[1] = Constant::getNullValue(Type::Int32Ty); |
| 8049 | CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs, 2); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 8050 | SrcTy = cast<PointerType>(CastOp->getType()); |
| 8051 | SrcPTy = SrcTy->getElementType(); |
| 8052 | } |
| 8053 | |
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 8054 | if ((SrcPTy->isInteger() || isa<PointerType>(SrcPTy) || |
| 8055 | isa<PackedType>(SrcPTy)) && |
Chris Lattner | b1515fe | 2005-03-29 06:37:47 +0000 | [diff] [blame] | 8056 | // Do not allow turning this into a load of an integer, which is then |
| 8057 | // casted to a pointer, this pessimizes pointer analysis a lot. |
| 8058 | (isa<PointerType>(SrcPTy) == isa<PointerType>(LI.getType())) && |
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 8059 | IC.getTargetData().getTypeSizeInBits(SrcPTy) == |
| 8060 | IC.getTargetData().getTypeSizeInBits(DestPTy)) { |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8061 | |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 8062 | // Okay, we are casting from one integer or pointer type to another of |
| 8063 | // the same size. Instead of casting the pointer before the load, cast |
| 8064 | // the result of the loaded value. |
| 8065 | Value *NewLoad = IC.InsertNewInstBefore(new LoadInst(CastOp, |
| 8066 | CI->getName(), |
| 8067 | LI.isVolatile()),LI); |
| 8068 | // Now cast the result of the load. |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 8069 | return new BitCastInst(NewLoad, LI.getType()); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 8070 | } |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 8071 | } |
| 8072 | } |
| 8073 | return 0; |
| 8074 | } |
| 8075 | |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 8076 | /// isSafeToLoadUnconditionally - Return true if we know that executing a load |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 8077 | /// from this value cannot trap. If it is not obviously safe to load from the |
| 8078 | /// specified pointer, we do a quick local scan of the basic block containing |
| 8079 | /// ScanFrom, to determine if the address is already accessed. |
| 8080 | static bool isSafeToLoadUnconditionally(Value *V, Instruction *ScanFrom) { |
| 8081 | // If it is an alloca or global variable, it is always safe to load from. |
| 8082 | if (isa<AllocaInst>(V) || isa<GlobalVariable>(V)) return true; |
| 8083 | |
| 8084 | // Otherwise, be a little bit agressive by scanning the local block where we |
| 8085 | // want to check to see if the pointer is already being loaded or stored |
Alkis Evlogimenos | 7b6ec60 | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 8086 | // from/to. If so, the previous load or store would have already trapped, |
| 8087 | // so there is no harm doing an extra load (also, CSE will later eliminate |
| 8088 | // the load entirely). |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 8089 | BasicBlock::iterator BBI = ScanFrom, E = ScanFrom->getParent()->begin(); |
| 8090 | |
Alkis Evlogimenos | 7b6ec60 | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 8091 | while (BBI != E) { |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 8092 | --BBI; |
| 8093 | |
| 8094 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { |
| 8095 | if (LI->getOperand(0) == V) return true; |
| 8096 | } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) |
| 8097 | if (SI->getOperand(1) == V) return true; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8098 | |
Alkis Evlogimenos | 7b6ec60 | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 8099 | } |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 8100 | return false; |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 8101 | } |
| 8102 | |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 8103 | Instruction *InstCombiner::visitLoadInst(LoadInst &LI) { |
| 8104 | Value *Op = LI.getOperand(0); |
Chris Lattner | 5f16a13 | 2004-01-12 04:13:56 +0000 | [diff] [blame] | 8105 | |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 8106 | // load (cast X) --> cast (load X) iff safe |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 8107 | if (isa<CastInst>(Op)) |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 8108 | if (Instruction *Res = InstCombineLoadCast(*this, LI)) |
| 8109 | return Res; |
| 8110 | |
| 8111 | // None of the following transforms are legal for volatile loads. |
| 8112 | if (LI.isVolatile()) return 0; |
Chris Lattner | 62f254d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 8113 | |
Chris Lattner | 62f254d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 8114 | if (&LI.getParent()->front() != &LI) { |
| 8115 | BasicBlock::iterator BBI = &LI; --BBI; |
Chris Lattner | 9c1f0fd | 2005-09-12 22:21:03 +0000 | [diff] [blame] | 8116 | // If the instruction immediately before this is a store to the same |
| 8117 | // address, do a simple form of store->load forwarding. |
Chris Lattner | 62f254d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 8118 | if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) |
| 8119 | if (SI->getOperand(1) == LI.getOperand(0)) |
| 8120 | return ReplaceInstUsesWith(LI, SI->getOperand(0)); |
Chris Lattner | 9c1f0fd | 2005-09-12 22:21:03 +0000 | [diff] [blame] | 8121 | if (LoadInst *LIB = dyn_cast<LoadInst>(BBI)) |
| 8122 | if (LIB->getOperand(0) == LI.getOperand(0)) |
| 8123 | return ReplaceInstUsesWith(LI, LIB); |
Chris Lattner | 62f254d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 8124 | } |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 8125 | |
| 8126 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) |
| 8127 | if (isa<ConstantPointerNull>(GEPI->getOperand(0)) || |
| 8128 | isa<UndefValue>(GEPI->getOperand(0))) { |
| 8129 | // Insert a new store to null instruction before the load to indicate |
| 8130 | // that this code is not reachable. We do this instead of inserting |
| 8131 | // an unreachable instruction directly because we cannot modify the |
| 8132 | // CFG. |
| 8133 | new StoreInst(UndefValue::get(LI.getType()), |
| 8134 | Constant::getNullValue(Op->getType()), &LI); |
| 8135 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
| 8136 | } |
| 8137 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8138 | if (Constant *C = dyn_cast<Constant>(Op)) { |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 8139 | // load null/undef -> undef |
| 8140 | if ((C->isNullValue() || isa<UndefValue>(C))) { |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8141 | // Insert a new store to null instruction before the load to indicate that |
| 8142 | // this code is not reachable. We do this instead of inserting an |
| 8143 | // unreachable instruction directly because we cannot modify the CFG. |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 8144 | new StoreInst(UndefValue::get(LI.getType()), |
| 8145 | Constant::getNullValue(Op->getType()), &LI); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8146 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8147 | } |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 8148 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8149 | // Instcombine load (constant global) into the value loaded. |
| 8150 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op)) |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 8151 | if (GV->isConstant() && !GV->isDeclaration()) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8152 | return ReplaceInstUsesWith(LI, GV->getInitializer()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8153 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8154 | // Instcombine load (constantexpr_GEP global, 0, ...) into the value loaded. |
| 8155 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op)) |
| 8156 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 8157 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0))) |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 8158 | if (GV->isConstant() && !GV->isDeclaration()) |
Chris Lattner | 363f2a2 | 2005-09-26 05:28:06 +0000 | [diff] [blame] | 8159 | if (Constant *V = |
| 8160 | ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE)) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8161 | return ReplaceInstUsesWith(LI, V); |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 8162 | if (CE->getOperand(0)->isNullValue()) { |
| 8163 | // Insert a new store to null instruction before the load to indicate |
| 8164 | // that this code is not reachable. We do this instead of inserting |
| 8165 | // an unreachable instruction directly because we cannot modify the |
| 8166 | // CFG. |
| 8167 | new StoreInst(UndefValue::get(LI.getType()), |
| 8168 | Constant::getNullValue(Op->getType()), &LI); |
| 8169 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
| 8170 | } |
| 8171 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8172 | } else if (CE->isCast()) { |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8173 | if (Instruction *Res = InstCombineLoadCast(*this, LI)) |
| 8174 | return Res; |
| 8175 | } |
| 8176 | } |
Chris Lattner | f499eac | 2004-04-08 20:39:49 +0000 | [diff] [blame] | 8177 | |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 8178 | if (Op->hasOneUse()) { |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 8179 | // Change select and PHI nodes to select values instead of addresses: this |
| 8180 | // helps alias analysis out a lot, allows many others simplifications, and |
| 8181 | // exposes redundancy in the code. |
| 8182 | // |
| 8183 | // Note that we cannot do the transformation unless we know that the |
| 8184 | // introduced loads cannot trap! Something like this is valid as long as |
| 8185 | // the condition is always false: load (select bool %C, int* null, int* %G), |
| 8186 | // but it would not be valid if we transformed it to load from null |
| 8187 | // unconditionally. |
| 8188 | // |
| 8189 | if (SelectInst *SI = dyn_cast<SelectInst>(Op)) { |
| 8190 | // load (select (Cond, &V1, &V2)) --> select(Cond, load &V1, load &V2). |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 8191 | if (isSafeToLoadUnconditionally(SI->getOperand(1), SI) && |
| 8192 | isSafeToLoadUnconditionally(SI->getOperand(2), SI)) { |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 8193 | Value *V1 = InsertNewInstBefore(new LoadInst(SI->getOperand(1), |
Chris Lattner | 79f0c8e | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 8194 | SI->getOperand(1)->getName()+".val"), LI); |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 8195 | Value *V2 = InsertNewInstBefore(new LoadInst(SI->getOperand(2), |
Chris Lattner | 79f0c8e | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 8196 | SI->getOperand(2)->getName()+".val"), LI); |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 8197 | return new SelectInst(SI->getCondition(), V1, V2); |
| 8198 | } |
| 8199 | |
Chris Lattner | 684fe21 | 2004-09-23 15:46:00 +0000 | [diff] [blame] | 8200 | // load (select (cond, null, P)) -> load P |
| 8201 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(1))) |
| 8202 | if (C->isNullValue()) { |
| 8203 | LI.setOperand(0, SI->getOperand(2)); |
| 8204 | return &LI; |
| 8205 | } |
| 8206 | |
| 8207 | // load (select (cond, P, null)) -> load P |
| 8208 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(2))) |
| 8209 | if (C->isNullValue()) { |
| 8210 | LI.setOperand(0, SI->getOperand(1)); |
| 8211 | return &LI; |
| 8212 | } |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 8213 | } |
| 8214 | } |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 8215 | return 0; |
| 8216 | } |
| 8217 | |
Reid Spencer | 55af2b5 | 2007-01-19 21:20:31 +0000 | [diff] [blame] | 8218 | /// InstCombineStoreToCast - Fold store V, (cast P) -> store (cast V), P |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8219 | /// when possible. |
| 8220 | static Instruction *InstCombineStoreToCast(InstCombiner &IC, StoreInst &SI) { |
| 8221 | User *CI = cast<User>(SI.getOperand(1)); |
| 8222 | Value *CastOp = CI->getOperand(0); |
| 8223 | |
| 8224 | const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType(); |
| 8225 | if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) { |
| 8226 | const Type *SrcPTy = SrcTy->getElementType(); |
| 8227 | |
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 8228 | if (DestPTy->isInteger() || isa<PointerType>(DestPTy)) { |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8229 | // If the source is an array, the code below will not succeed. Check to |
| 8230 | // see if a trivial 'gep P, 0, 0' will help matters. Only do this for |
| 8231 | // constants. |
| 8232 | if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy)) |
| 8233 | if (Constant *CSrc = dyn_cast<Constant>(CastOp)) |
| 8234 | if (ASrcTy->getNumElements() != 0) { |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 8235 | Value* Idxs[2]; |
| 8236 | Idxs[0] = Idxs[1] = Constant::getNullValue(Type::Int32Ty); |
| 8237 | CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs, 2); |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8238 | SrcTy = cast<PointerType>(CastOp->getType()); |
| 8239 | SrcPTy = SrcTy->getElementType(); |
| 8240 | } |
| 8241 | |
Reid Spencer | 67f827c | 2007-01-20 23:35:48 +0000 | [diff] [blame] | 8242 | if ((SrcPTy->isInteger() || isa<PointerType>(SrcPTy)) && |
| 8243 | IC.getTargetData().getTypeSizeInBits(SrcPTy) == |
| 8244 | IC.getTargetData().getTypeSizeInBits(DestPTy)) { |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8245 | |
| 8246 | // Okay, we are casting from one integer or pointer type to another of |
Reid Spencer | 7515396 | 2007-01-18 18:54:33 +0000 | [diff] [blame] | 8247 | // the same size. Instead of casting the pointer before |
| 8248 | // the store, cast the value to be stored. |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8249 | Value *NewCast; |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 8250 | Value *SIOp0 = SI.getOperand(0); |
Reid Spencer | 7515396 | 2007-01-18 18:54:33 +0000 | [diff] [blame] | 8251 | Instruction::CastOps opcode = Instruction::BitCast; |
| 8252 | const Type* CastSrcTy = SIOp0->getType(); |
| 8253 | const Type* CastDstTy = SrcPTy; |
| 8254 | if (isa<PointerType>(CastDstTy)) { |
| 8255 | if (CastSrcTy->isInteger()) |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 8256 | opcode = Instruction::IntToPtr; |
Reid Spencer | 67f827c | 2007-01-20 23:35:48 +0000 | [diff] [blame] | 8257 | } else if (isa<IntegerType>(CastDstTy)) { |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 8258 | if (isa<PointerType>(SIOp0->getType())) |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 8259 | opcode = Instruction::PtrToInt; |
| 8260 | } |
| 8261 | if (Constant *C = dyn_cast<Constant>(SIOp0)) |
Reid Spencer | 7515396 | 2007-01-18 18:54:33 +0000 | [diff] [blame] | 8262 | NewCast = ConstantExpr::getCast(opcode, C, CastDstTy); |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8263 | else |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8264 | NewCast = IC.InsertNewInstBefore( |
Reid Spencer | 7515396 | 2007-01-18 18:54:33 +0000 | [diff] [blame] | 8265 | CastInst::create(opcode, SIOp0, CastDstTy, SIOp0->getName()+".c"), |
| 8266 | SI); |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8267 | return new StoreInst(NewCast, CastOp); |
| 8268 | } |
| 8269 | } |
| 8270 | } |
| 8271 | return 0; |
| 8272 | } |
| 8273 | |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 8274 | Instruction *InstCombiner::visitStoreInst(StoreInst &SI) { |
| 8275 | Value *Val = SI.getOperand(0); |
| 8276 | Value *Ptr = SI.getOperand(1); |
| 8277 | |
| 8278 | if (isa<UndefValue>(Ptr)) { // store X, undef -> noop (even if volatile) |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 8279 | EraseInstFromFunction(SI); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 8280 | ++NumCombined; |
| 8281 | return 0; |
| 8282 | } |
Chris Lattner | 836692d | 2007-01-15 06:51:56 +0000 | [diff] [blame] | 8283 | |
| 8284 | // If the RHS is an alloca with a single use, zapify the store, making the |
| 8285 | // alloca dead. |
| 8286 | if (Ptr->hasOneUse()) { |
| 8287 | if (isa<AllocaInst>(Ptr)) { |
| 8288 | EraseInstFromFunction(SI); |
| 8289 | ++NumCombined; |
| 8290 | return 0; |
| 8291 | } |
| 8292 | |
| 8293 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) |
| 8294 | if (isa<AllocaInst>(GEP->getOperand(0)) && |
| 8295 | GEP->getOperand(0)->hasOneUse()) { |
| 8296 | EraseInstFromFunction(SI); |
| 8297 | ++NumCombined; |
| 8298 | return 0; |
| 8299 | } |
| 8300 | } |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 8301 | |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 8302 | // Do really simple DSE, to catch cases where there are several consequtive |
| 8303 | // stores to the same location, separated by a few arithmetic operations. This |
| 8304 | // situation often occurs with bitfield accesses. |
| 8305 | BasicBlock::iterator BBI = &SI; |
| 8306 | for (unsigned ScanInsts = 6; BBI != SI.getParent()->begin() && ScanInsts; |
| 8307 | --ScanInsts) { |
| 8308 | --BBI; |
| 8309 | |
| 8310 | if (StoreInst *PrevSI = dyn_cast<StoreInst>(BBI)) { |
| 8311 | // Prev store isn't volatile, and stores to the same location? |
| 8312 | if (!PrevSI->isVolatile() && PrevSI->getOperand(1) == SI.getOperand(1)) { |
| 8313 | ++NumDeadStore; |
| 8314 | ++BBI; |
| 8315 | EraseInstFromFunction(*PrevSI); |
| 8316 | continue; |
| 8317 | } |
| 8318 | break; |
| 8319 | } |
| 8320 | |
Chris Lattner | b4db97f | 2006-05-26 19:19:20 +0000 | [diff] [blame] | 8321 | // If this is a load, we have to stop. However, if the loaded value is from |
| 8322 | // the pointer we're loading and is producing the pointer we're storing, |
| 8323 | // then *this* store is dead (X = load P; store X -> P). |
| 8324 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { |
| 8325 | if (LI == Val && LI->getOperand(0) == Ptr) { |
| 8326 | EraseInstFromFunction(SI); |
| 8327 | ++NumCombined; |
| 8328 | return 0; |
| 8329 | } |
| 8330 | // Otherwise, this is a load from some other location. Stores before it |
| 8331 | // may not be dead. |
| 8332 | break; |
| 8333 | } |
| 8334 | |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 8335 | // Don't skip over loads or things that can modify memory. |
Chris Lattner | b4db97f | 2006-05-26 19:19:20 +0000 | [diff] [blame] | 8336 | if (BBI->mayWriteToMemory()) |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 8337 | break; |
| 8338 | } |
| 8339 | |
| 8340 | |
| 8341 | if (SI.isVolatile()) return 0; // Don't hack volatile stores. |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 8342 | |
| 8343 | // store X, null -> turns into 'unreachable' in SimplifyCFG |
| 8344 | if (isa<ConstantPointerNull>(Ptr)) { |
| 8345 | if (!isa<UndefValue>(Val)) { |
| 8346 | SI.setOperand(0, UndefValue::get(Val->getType())); |
| 8347 | if (Instruction *U = dyn_cast<Instruction>(Val)) |
| 8348 | WorkList.push_back(U); // Dropped a use. |
| 8349 | ++NumCombined; |
| 8350 | } |
| 8351 | return 0; // Do not modify these! |
| 8352 | } |
| 8353 | |
| 8354 | // store undef, Ptr -> noop |
| 8355 | if (isa<UndefValue>(Val)) { |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 8356 | EraseInstFromFunction(SI); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 8357 | ++NumCombined; |
| 8358 | return 0; |
| 8359 | } |
| 8360 | |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8361 | // If the pointer destination is a cast, see if we can fold the cast into the |
| 8362 | // source instead. |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 8363 | if (isa<CastInst>(Ptr)) |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8364 | if (Instruction *Res = InstCombineStoreToCast(*this, SI)) |
| 8365 | return Res; |
| 8366 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8367 | if (CE->isCast()) |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8368 | if (Instruction *Res = InstCombineStoreToCast(*this, SI)) |
| 8369 | return Res; |
| 8370 | |
Chris Lattner | 408902b | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 8371 | |
| 8372 | // If this store is the last instruction in the basic block, and if the block |
| 8373 | // ends with an unconditional branch, try to move it to the successor block. |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 8374 | BBI = &SI; ++BBI; |
Chris Lattner | 408902b | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 8375 | if (BranchInst *BI = dyn_cast<BranchInst>(BBI)) |
| 8376 | if (BI->isUnconditional()) { |
| 8377 | // Check to see if the successor block has exactly two incoming edges. If |
| 8378 | // so, see if the other predecessor contains a store to the same location. |
| 8379 | // if so, insert a PHI node (if needed) and move the stores down. |
| 8380 | BasicBlock *Dest = BI->getSuccessor(0); |
| 8381 | |
| 8382 | pred_iterator PI = pred_begin(Dest); |
| 8383 | BasicBlock *Other = 0; |
| 8384 | if (*PI != BI->getParent()) |
| 8385 | Other = *PI; |
| 8386 | ++PI; |
| 8387 | if (PI != pred_end(Dest)) { |
| 8388 | if (*PI != BI->getParent()) |
| 8389 | if (Other) |
| 8390 | Other = 0; |
| 8391 | else |
| 8392 | Other = *PI; |
| 8393 | if (++PI != pred_end(Dest)) |
| 8394 | Other = 0; |
| 8395 | } |
| 8396 | if (Other) { // If only one other pred... |
| 8397 | BBI = Other->getTerminator(); |
| 8398 | // Make sure this other block ends in an unconditional branch and that |
| 8399 | // there is an instruction before the branch. |
| 8400 | if (isa<BranchInst>(BBI) && cast<BranchInst>(BBI)->isUnconditional() && |
| 8401 | BBI != Other->begin()) { |
| 8402 | --BBI; |
| 8403 | StoreInst *OtherStore = dyn_cast<StoreInst>(BBI); |
| 8404 | |
| 8405 | // If this instruction is a store to the same location. |
| 8406 | if (OtherStore && OtherStore->getOperand(1) == SI.getOperand(1)) { |
| 8407 | // Okay, we know we can perform this transformation. Insert a PHI |
| 8408 | // node now if we need it. |
| 8409 | Value *MergedVal = OtherStore->getOperand(0); |
| 8410 | if (MergedVal != SI.getOperand(0)) { |
| 8411 | PHINode *PN = new PHINode(MergedVal->getType(), "storemerge"); |
| 8412 | PN->reserveOperandSpace(2); |
| 8413 | PN->addIncoming(SI.getOperand(0), SI.getParent()); |
| 8414 | PN->addIncoming(OtherStore->getOperand(0), Other); |
| 8415 | MergedVal = InsertNewInstBefore(PN, Dest->front()); |
| 8416 | } |
| 8417 | |
| 8418 | // Advance to a place where it is safe to insert the new store and |
| 8419 | // insert it. |
| 8420 | BBI = Dest->begin(); |
| 8421 | while (isa<PHINode>(BBI)) ++BBI; |
| 8422 | InsertNewInstBefore(new StoreInst(MergedVal, SI.getOperand(1), |
| 8423 | OtherStore->isVolatile()), *BBI); |
| 8424 | |
| 8425 | // Nuke the old stores. |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 8426 | EraseInstFromFunction(SI); |
| 8427 | EraseInstFromFunction(*OtherStore); |
Chris Lattner | 408902b | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 8428 | ++NumCombined; |
| 8429 | return 0; |
| 8430 | } |
| 8431 | } |
| 8432 | } |
| 8433 | } |
| 8434 | |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 8435 | return 0; |
| 8436 | } |
| 8437 | |
| 8438 | |
Chris Lattner | c4d10eb | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 8439 | Instruction *InstCombiner::visitBranchInst(BranchInst &BI) { |
| 8440 | // Change br (not X), label True, label False to: br X, label False, True |
Reid Spencer | 4b828e6 | 2005-06-18 17:37:34 +0000 | [diff] [blame] | 8441 | Value *X = 0; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 8442 | BasicBlock *TrueDest; |
| 8443 | BasicBlock *FalseDest; |
| 8444 | if (match(&BI, m_Br(m_Not(m_Value(X)), TrueDest, FalseDest)) && |
| 8445 | !isa<Constant>(X)) { |
| 8446 | // Swap Destinations and condition... |
| 8447 | BI.setCondition(X); |
| 8448 | BI.setSuccessor(0, FalseDest); |
| 8449 | BI.setSuccessor(1, TrueDest); |
| 8450 | return &BI; |
| 8451 | } |
| 8452 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8453 | // Cannonicalize fcmp_one -> fcmp_oeq |
| 8454 | FCmpInst::Predicate FPred; Value *Y; |
| 8455 | if (match(&BI, m_Br(m_FCmp(FPred, m_Value(X), m_Value(Y)), |
| 8456 | TrueDest, FalseDest))) |
| 8457 | if ((FPred == FCmpInst::FCMP_ONE || FPred == FCmpInst::FCMP_OLE || |
| 8458 | FPred == FCmpInst::FCMP_OGE) && BI.getCondition()->hasOneUse()) { |
| 8459 | FCmpInst *I = cast<FCmpInst>(BI.getCondition()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8460 | FCmpInst::Predicate NewPred = FCmpInst::getInversePredicate(FPred); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 8461 | Instruction *NewSCC = new FCmpInst(NewPred, X, Y, "", I); |
| 8462 | NewSCC->takeName(I); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8463 | // Swap Destinations and condition... |
| 8464 | BI.setCondition(NewSCC); |
| 8465 | BI.setSuccessor(0, FalseDest); |
| 8466 | BI.setSuccessor(1, TrueDest); |
| 8467 | removeFromWorkList(I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 8468 | I->eraseFromParent(); |
| 8469 | WorkList.push_back(NewSCC); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8470 | return &BI; |
| 8471 | } |
| 8472 | |
| 8473 | // Cannonicalize icmp_ne -> icmp_eq |
| 8474 | ICmpInst::Predicate IPred; |
| 8475 | if (match(&BI, m_Br(m_ICmp(IPred, m_Value(X), m_Value(Y)), |
| 8476 | TrueDest, FalseDest))) |
| 8477 | if ((IPred == ICmpInst::ICMP_NE || IPred == ICmpInst::ICMP_ULE || |
| 8478 | IPred == ICmpInst::ICMP_SLE || IPred == ICmpInst::ICMP_UGE || |
| 8479 | IPred == ICmpInst::ICMP_SGE) && BI.getCondition()->hasOneUse()) { |
| 8480 | ICmpInst *I = cast<ICmpInst>(BI.getCondition()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8481 | ICmpInst::Predicate NewPred = ICmpInst::getInversePredicate(IPred); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 8482 | Instruction *NewSCC = new ICmpInst(NewPred, X, Y, "", I); |
| 8483 | NewSCC->takeName(I); |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 8484 | // Swap Destinations and condition... |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 8485 | BI.setCondition(NewSCC); |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 8486 | BI.setSuccessor(0, FalseDest); |
| 8487 | BI.setSuccessor(1, TrueDest); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 8488 | removeFromWorkList(I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 8489 | I->eraseFromParent();; |
| 8490 | WorkList.push_back(NewSCC); |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 8491 | return &BI; |
| 8492 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8493 | |
Chris Lattner | c4d10eb | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 8494 | return 0; |
| 8495 | } |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 8496 | |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 8497 | Instruction *InstCombiner::visitSwitchInst(SwitchInst &SI) { |
| 8498 | Value *Cond = SI.getCondition(); |
| 8499 | if (Instruction *I = dyn_cast<Instruction>(Cond)) { |
| 8500 | if (I->getOpcode() == Instruction::Add) |
| 8501 | if (ConstantInt *AddRHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 8502 | // change 'switch (X+4) case 1:' into 'switch (X) case -3' |
| 8503 | for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8504 | SI.setOperand(i,ConstantExpr::getSub(cast<Constant>(SI.getOperand(i)), |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 8505 | AddRHS)); |
| 8506 | SI.setOperand(0, I->getOperand(0)); |
| 8507 | WorkList.push_back(I); |
| 8508 | return &SI; |
| 8509 | } |
| 8510 | } |
| 8511 | return 0; |
| 8512 | } |
| 8513 | |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 8514 | /// CheapToScalarize - Return true if the value is cheaper to scalarize than it |
| 8515 | /// is to leave as a vector operation. |
| 8516 | static bool CheapToScalarize(Value *V, bool isConstant) { |
| 8517 | if (isa<ConstantAggregateZero>(V)) |
| 8518 | return true; |
| 8519 | if (ConstantPacked *C = dyn_cast<ConstantPacked>(V)) { |
| 8520 | if (isConstant) return true; |
| 8521 | // If all elts are the same, we can extract. |
| 8522 | Constant *Op0 = C->getOperand(0); |
| 8523 | for (unsigned i = 1; i < C->getNumOperands(); ++i) |
| 8524 | if (C->getOperand(i) != Op0) |
| 8525 | return false; |
| 8526 | return true; |
| 8527 | } |
| 8528 | Instruction *I = dyn_cast<Instruction>(V); |
| 8529 | if (!I) return false; |
| 8530 | |
| 8531 | // Insert element gets simplified to the inserted element or is deleted if |
| 8532 | // this is constant idx extract element and its a constant idx insertelt. |
| 8533 | if (I->getOpcode() == Instruction::InsertElement && isConstant && |
| 8534 | isa<ConstantInt>(I->getOperand(2))) |
| 8535 | return true; |
| 8536 | if (I->getOpcode() == Instruction::Load && I->hasOneUse()) |
| 8537 | return true; |
| 8538 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) |
| 8539 | if (BO->hasOneUse() && |
| 8540 | (CheapToScalarize(BO->getOperand(0), isConstant) || |
| 8541 | CheapToScalarize(BO->getOperand(1), isConstant))) |
| 8542 | return true; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8543 | if (CmpInst *CI = dyn_cast<CmpInst>(I)) |
| 8544 | if (CI->hasOneUse() && |
| 8545 | (CheapToScalarize(CI->getOperand(0), isConstant) || |
| 8546 | CheapToScalarize(CI->getOperand(1), isConstant))) |
| 8547 | return true; |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 8548 | |
| 8549 | return false; |
| 8550 | } |
| 8551 | |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame^] | 8552 | /// Read and decode a shufflevector mask. |
| 8553 | /// |
| 8554 | /// It turns undef elements into values that are larger than the number of |
| 8555 | /// elements in the input. |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8556 | static std::vector<unsigned> getShuffleMask(const ShuffleVectorInst *SVI) { |
| 8557 | unsigned NElts = SVI->getType()->getNumElements(); |
| 8558 | if (isa<ConstantAggregateZero>(SVI->getOperand(2))) |
| 8559 | return std::vector<unsigned>(NElts, 0); |
| 8560 | if (isa<UndefValue>(SVI->getOperand(2))) |
| 8561 | return std::vector<unsigned>(NElts, 2*NElts); |
| 8562 | |
| 8563 | std::vector<unsigned> Result; |
| 8564 | const ConstantPacked *CP = cast<ConstantPacked>(SVI->getOperand(2)); |
| 8565 | for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) |
| 8566 | if (isa<UndefValue>(CP->getOperand(i))) |
| 8567 | Result.push_back(NElts*2); // undef -> 8 |
| 8568 | else |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8569 | Result.push_back(cast<ConstantInt>(CP->getOperand(i))->getZExtValue()); |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8570 | return Result; |
| 8571 | } |
| 8572 | |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8573 | /// FindScalarElement - Given a vector and an element number, see if the scalar |
| 8574 | /// value is already around as a register, for example if it were inserted then |
| 8575 | /// extracted from the vector. |
| 8576 | static Value *FindScalarElement(Value *V, unsigned EltNo) { |
| 8577 | assert(isa<PackedType>(V->getType()) && "Not looking at a vector?"); |
| 8578 | const PackedType *PTy = cast<PackedType>(V->getType()); |
Chris Lattner | 389a6f5 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 8579 | unsigned Width = PTy->getNumElements(); |
| 8580 | if (EltNo >= Width) // Out of range access. |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8581 | return UndefValue::get(PTy->getElementType()); |
| 8582 | |
| 8583 | if (isa<UndefValue>(V)) |
| 8584 | return UndefValue::get(PTy->getElementType()); |
| 8585 | else if (isa<ConstantAggregateZero>(V)) |
| 8586 | return Constant::getNullValue(PTy->getElementType()); |
| 8587 | else if (ConstantPacked *CP = dyn_cast<ConstantPacked>(V)) |
| 8588 | return CP->getOperand(EltNo); |
| 8589 | else if (InsertElementInst *III = dyn_cast<InsertElementInst>(V)) { |
| 8590 | // If this is an insert to a variable element, we don't know what it is. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8591 | if (!isa<ConstantInt>(III->getOperand(2))) |
| 8592 | return 0; |
| 8593 | unsigned IIElt = cast<ConstantInt>(III->getOperand(2))->getZExtValue(); |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8594 | |
| 8595 | // If this is an insert to the element we are looking for, return the |
| 8596 | // inserted value. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8597 | if (EltNo == IIElt) |
| 8598 | return III->getOperand(1); |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8599 | |
| 8600 | // Otherwise, the insertelement doesn't modify the value, recurse on its |
| 8601 | // vector input. |
| 8602 | return FindScalarElement(III->getOperand(0), EltNo); |
Chris Lattner | 389a6f5 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 8603 | } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(V)) { |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8604 | unsigned InEl = getShuffleMask(SVI)[EltNo]; |
| 8605 | if (InEl < Width) |
| 8606 | return FindScalarElement(SVI->getOperand(0), InEl); |
| 8607 | else if (InEl < Width*2) |
| 8608 | return FindScalarElement(SVI->getOperand(1), InEl - Width); |
| 8609 | else |
| 8610 | return UndefValue::get(PTy->getElementType()); |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8611 | } |
| 8612 | |
| 8613 | // Otherwise, we don't know. |
| 8614 | return 0; |
| 8615 | } |
| 8616 | |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 8617 | Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) { |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8618 | |
Chris Lattner | 1f13c88 | 2006-03-31 18:25:14 +0000 | [diff] [blame] | 8619 | // If packed val is undef, replace extract with scalar undef. |
| 8620 | if (isa<UndefValue>(EI.getOperand(0))) |
| 8621 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); |
| 8622 | |
| 8623 | // If packed val is constant 0, replace extract with scalar 0. |
| 8624 | if (isa<ConstantAggregateZero>(EI.getOperand(0))) |
| 8625 | return ReplaceInstUsesWith(EI, Constant::getNullValue(EI.getType())); |
| 8626 | |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 8627 | if (ConstantPacked *C = dyn_cast<ConstantPacked>(EI.getOperand(0))) { |
| 8628 | // If packed val is constant with uniform operands, replace EI |
| 8629 | // with that operand |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 8630 | Constant *op0 = C->getOperand(0); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 8631 | for (unsigned i = 1; i < C->getNumOperands(); ++i) |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 8632 | if (C->getOperand(i) != op0) { |
| 8633 | op0 = 0; |
| 8634 | break; |
| 8635 | } |
| 8636 | if (op0) |
| 8637 | return ReplaceInstUsesWith(EI, op0); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 8638 | } |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 8639 | |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8640 | // If extracting a specified index from the vector, see if we can recursively |
| 8641 | // find a previously computed scalar that was inserted into the vector. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8642 | if (ConstantInt *IdxC = dyn_cast<ConstantInt>(EI.getOperand(1))) { |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 8643 | // This instruction only demands the single element from the input vector. |
| 8644 | // If the input vector has a single use, simplify it based on this use |
| 8645 | // property. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8646 | uint64_t IndexVal = IdxC->getZExtValue(); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 8647 | if (EI.getOperand(0)->hasOneUse()) { |
| 8648 | uint64_t UndefElts; |
| 8649 | if (Value *V = SimplifyDemandedVectorElts(EI.getOperand(0), |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8650 | 1 << IndexVal, |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 8651 | UndefElts)) { |
| 8652 | EI.setOperand(0, V); |
| 8653 | return &EI; |
| 8654 | } |
| 8655 | } |
| 8656 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8657 | if (Value *Elt = FindScalarElement(EI.getOperand(0), IndexVal)) |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8658 | return ReplaceInstUsesWith(EI, Elt); |
Chris Lattner | 389a6f5 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 8659 | } |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8660 | |
Chris Lattner | 73fa49d | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 8661 | if (Instruction *I = dyn_cast<Instruction>(EI.getOperand(0))) { |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 8662 | if (I->hasOneUse()) { |
| 8663 | // Push extractelement into predecessor operation if legal and |
| 8664 | // profitable to do so |
| 8665 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) { |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 8666 | bool isConstantElt = isa<ConstantInt>(EI.getOperand(1)); |
| 8667 | if (CheapToScalarize(BO, isConstantElt)) { |
| 8668 | ExtractElementInst *newEI0 = |
| 8669 | new ExtractElementInst(BO->getOperand(0), EI.getOperand(1), |
| 8670 | EI.getName()+".lhs"); |
| 8671 | ExtractElementInst *newEI1 = |
| 8672 | new ExtractElementInst(BO->getOperand(1), EI.getOperand(1), |
| 8673 | EI.getName()+".rhs"); |
| 8674 | InsertNewInstBefore(newEI0, EI); |
| 8675 | InsertNewInstBefore(newEI1, EI); |
| 8676 | return BinaryOperator::create(BO->getOpcode(), newEI0, newEI1); |
| 8677 | } |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 8678 | } else if (isa<LoadInst>(I)) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 8679 | Value *Ptr = InsertCastBefore(Instruction::BitCast, I->getOperand(0), |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 8680 | PointerType::get(EI.getType()), EI); |
| 8681 | GetElementPtrInst *GEP = |
Reid Spencer | de33124 | 2006-11-29 01:11:01 +0000 | [diff] [blame] | 8682 | new GetElementPtrInst(Ptr, EI.getOperand(1), I->getName() + ".gep"); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 8683 | InsertNewInstBefore(GEP, EI); |
| 8684 | return new LoadInst(GEP); |
Chris Lattner | 73fa49d | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 8685 | } |
| 8686 | } |
| 8687 | if (InsertElementInst *IE = dyn_cast<InsertElementInst>(I)) { |
| 8688 | // Extracting the inserted element? |
| 8689 | if (IE->getOperand(2) == EI.getOperand(1)) |
| 8690 | return ReplaceInstUsesWith(EI, IE->getOperand(1)); |
| 8691 | // If the inserted and extracted elements are constants, they must not |
| 8692 | // be the same value, extract from the pre-inserted value instead. |
| 8693 | if (isa<Constant>(IE->getOperand(2)) && |
| 8694 | isa<Constant>(EI.getOperand(1))) { |
| 8695 | AddUsesToWorkList(EI); |
| 8696 | EI.setOperand(0, IE->getOperand(0)); |
| 8697 | return &EI; |
| 8698 | } |
| 8699 | } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(I)) { |
| 8700 | // If this is extracting an element from a shufflevector, figure out where |
| 8701 | // it came from and extract from the appropriate input element instead. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8702 | if (ConstantInt *Elt = dyn_cast<ConstantInt>(EI.getOperand(1))) { |
| 8703 | unsigned SrcIdx = getShuffleMask(SVI)[Elt->getZExtValue()]; |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8704 | Value *Src; |
| 8705 | if (SrcIdx < SVI->getType()->getNumElements()) |
| 8706 | Src = SVI->getOperand(0); |
| 8707 | else if (SrcIdx < SVI->getType()->getNumElements()*2) { |
| 8708 | SrcIdx -= SVI->getType()->getNumElements(); |
| 8709 | Src = SVI->getOperand(1); |
| 8710 | } else { |
| 8711 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); |
Chris Lattner | df084ff | 2006-03-30 22:02:40 +0000 | [diff] [blame] | 8712 | } |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 8713 | return new ExtractElementInst(Src, SrcIdx); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 8714 | } |
| 8715 | } |
Chris Lattner | 73fa49d | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 8716 | } |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 8717 | return 0; |
| 8718 | } |
| 8719 | |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8720 | /// CollectSingleShuffleElements - If V is a shuffle of values that ONLY returns |
| 8721 | /// elements from either LHS or RHS, return the shuffle mask and true. |
| 8722 | /// Otherwise, return false. |
| 8723 | static bool CollectSingleShuffleElements(Value *V, Value *LHS, Value *RHS, |
| 8724 | std::vector<Constant*> &Mask) { |
| 8725 | assert(V->getType() == LHS->getType() && V->getType() == RHS->getType() && |
| 8726 | "Invalid CollectSingleShuffleElements"); |
| 8727 | unsigned NumElts = cast<PackedType>(V->getType())->getNumElements(); |
| 8728 | |
| 8729 | if (isa<UndefValue>(V)) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8730 | Mask.assign(NumElts, UndefValue::get(Type::Int32Ty)); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8731 | return true; |
| 8732 | } else if (V == LHS) { |
| 8733 | for (unsigned i = 0; i != NumElts; ++i) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8734 | Mask.push_back(ConstantInt::get(Type::Int32Ty, i)); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8735 | return true; |
| 8736 | } else if (V == RHS) { |
| 8737 | for (unsigned i = 0; i != NumElts; ++i) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8738 | Mask.push_back(ConstantInt::get(Type::Int32Ty, i+NumElts)); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8739 | return true; |
| 8740 | } else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) { |
| 8741 | // If this is an insert of an extract from some other vector, include it. |
| 8742 | Value *VecOp = IEI->getOperand(0); |
| 8743 | Value *ScalarOp = IEI->getOperand(1); |
| 8744 | Value *IdxOp = IEI->getOperand(2); |
| 8745 | |
Chris Lattner | d929f06 | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 8746 | if (!isa<ConstantInt>(IdxOp)) |
| 8747 | return false; |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8748 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Chris Lattner | d929f06 | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 8749 | |
| 8750 | if (isa<UndefValue>(ScalarOp)) { // inserting undef into vector. |
| 8751 | // Okay, we can handle this if the vector we are insertinting into is |
| 8752 | // transitively ok. |
| 8753 | if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) { |
| 8754 | // If so, update the mask to reflect the inserted undef. |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8755 | Mask[InsertedIdx] = UndefValue::get(Type::Int32Ty); |
Chris Lattner | d929f06 | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 8756 | return true; |
| 8757 | } |
| 8758 | } else if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)){ |
| 8759 | if (isa<ConstantInt>(EI->getOperand(1)) && |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8760 | EI->getOperand(0)->getType() == V->getType()) { |
| 8761 | unsigned ExtractedIdx = |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8762 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8763 | |
| 8764 | // This must be extracting from either LHS or RHS. |
| 8765 | if (EI->getOperand(0) == LHS || EI->getOperand(0) == RHS) { |
| 8766 | // Okay, we can handle this if the vector we are insertinting into is |
| 8767 | // transitively ok. |
| 8768 | if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) { |
| 8769 | // If so, update the mask to reflect the inserted value. |
| 8770 | if (EI->getOperand(0) == LHS) { |
| 8771 | Mask[InsertedIdx & (NumElts-1)] = |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8772 | ConstantInt::get(Type::Int32Ty, ExtractedIdx); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8773 | } else { |
| 8774 | assert(EI->getOperand(0) == RHS); |
| 8775 | Mask[InsertedIdx & (NumElts-1)] = |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8776 | ConstantInt::get(Type::Int32Ty, ExtractedIdx+NumElts); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8777 | |
| 8778 | } |
| 8779 | return true; |
| 8780 | } |
| 8781 | } |
| 8782 | } |
| 8783 | } |
| 8784 | } |
| 8785 | // TODO: Handle shufflevector here! |
| 8786 | |
| 8787 | return false; |
| 8788 | } |
| 8789 | |
| 8790 | /// CollectShuffleElements - We are building a shuffle of V, using RHS as the |
| 8791 | /// RHS of the shuffle instruction, if it is not null. Return a shuffle mask |
| 8792 | /// that computes V and the LHS value of the shuffle. |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8793 | static Value *CollectShuffleElements(Value *V, std::vector<Constant*> &Mask, |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8794 | Value *&RHS) { |
| 8795 | assert(isa<PackedType>(V->getType()) && |
| 8796 | (RHS == 0 || V->getType() == RHS->getType()) && |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8797 | "Invalid shuffle!"); |
| 8798 | unsigned NumElts = cast<PackedType>(V->getType())->getNumElements(); |
| 8799 | |
| 8800 | if (isa<UndefValue>(V)) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8801 | Mask.assign(NumElts, UndefValue::get(Type::Int32Ty)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8802 | return V; |
| 8803 | } else if (isa<ConstantAggregateZero>(V)) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8804 | Mask.assign(NumElts, ConstantInt::get(Type::Int32Ty, 0)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8805 | return V; |
| 8806 | } else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) { |
| 8807 | // If this is an insert of an extract from some other vector, include it. |
| 8808 | Value *VecOp = IEI->getOperand(0); |
| 8809 | Value *ScalarOp = IEI->getOperand(1); |
| 8810 | Value *IdxOp = IEI->getOperand(2); |
| 8811 | |
| 8812 | if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) { |
| 8813 | if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) && |
| 8814 | EI->getOperand(0)->getType() == V->getType()) { |
| 8815 | unsigned ExtractedIdx = |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8816 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
| 8817 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8818 | |
| 8819 | // Either the extracted from or inserted into vector must be RHSVec, |
| 8820 | // otherwise we'd end up with a shuffle of three inputs. |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8821 | if (EI->getOperand(0) == RHS || RHS == 0) { |
| 8822 | RHS = EI->getOperand(0); |
| 8823 | Value *V = CollectShuffleElements(VecOp, Mask, RHS); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8824 | Mask[InsertedIdx & (NumElts-1)] = |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8825 | ConstantInt::get(Type::Int32Ty, NumElts+ExtractedIdx); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8826 | return V; |
| 8827 | } |
| 8828 | |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8829 | if (VecOp == RHS) { |
| 8830 | Value *V = CollectShuffleElements(EI->getOperand(0), Mask, RHS); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8831 | // Everything but the extracted element is replaced with the RHS. |
| 8832 | for (unsigned i = 0; i != NumElts; ++i) { |
| 8833 | if (i != InsertedIdx) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8834 | Mask[i] = ConstantInt::get(Type::Int32Ty, NumElts+i); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8835 | } |
| 8836 | return V; |
| 8837 | } |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8838 | |
| 8839 | // If this insertelement is a chain that comes from exactly these two |
| 8840 | // vectors, return the vector and the effective shuffle. |
| 8841 | if (CollectSingleShuffleElements(IEI, EI->getOperand(0), RHS, Mask)) |
| 8842 | return EI->getOperand(0); |
| 8843 | |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8844 | } |
| 8845 | } |
| 8846 | } |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8847 | // TODO: Handle shufflevector here! |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8848 | |
| 8849 | // Otherwise, can't do anything fancy. Return an identity vector. |
| 8850 | for (unsigned i = 0; i != NumElts; ++i) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8851 | Mask.push_back(ConstantInt::get(Type::Int32Ty, i)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8852 | return V; |
| 8853 | } |
| 8854 | |
| 8855 | Instruction *InstCombiner::visitInsertElementInst(InsertElementInst &IE) { |
| 8856 | Value *VecOp = IE.getOperand(0); |
| 8857 | Value *ScalarOp = IE.getOperand(1); |
| 8858 | Value *IdxOp = IE.getOperand(2); |
| 8859 | |
| 8860 | // If the inserted element was extracted from some other vector, and if the |
| 8861 | // indexes are constant, try to turn this into a shufflevector operation. |
| 8862 | if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) { |
| 8863 | if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) && |
| 8864 | EI->getOperand(0)->getType() == IE.getType()) { |
| 8865 | unsigned NumVectorElts = IE.getType()->getNumElements(); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8866 | unsigned ExtractedIdx=cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
| 8867 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8868 | |
| 8869 | if (ExtractedIdx >= NumVectorElts) // Out of range extract. |
| 8870 | return ReplaceInstUsesWith(IE, VecOp); |
| 8871 | |
| 8872 | if (InsertedIdx >= NumVectorElts) // Out of range insert. |
| 8873 | return ReplaceInstUsesWith(IE, UndefValue::get(IE.getType())); |
| 8874 | |
| 8875 | // If we are extracting a value from a vector, then inserting it right |
| 8876 | // back into the same place, just use the input vector. |
| 8877 | if (EI->getOperand(0) == VecOp && ExtractedIdx == InsertedIdx) |
| 8878 | return ReplaceInstUsesWith(IE, VecOp); |
| 8879 | |
| 8880 | // We could theoretically do this for ANY input. However, doing so could |
| 8881 | // turn chains of insertelement instructions into a chain of shufflevector |
| 8882 | // instructions, and right now we do not merge shufflevectors. As such, |
| 8883 | // only do this in a situation where it is clear that there is benefit. |
| 8884 | if (isa<UndefValue>(VecOp) || isa<ConstantAggregateZero>(VecOp)) { |
| 8885 | // Turn this into shuffle(EIOp0, VecOp, Mask). The result has all of |
| 8886 | // the values of VecOp, except then one read from EIOp0. |
| 8887 | // Build a new shuffle mask. |
| 8888 | std::vector<Constant*> Mask; |
| 8889 | if (isa<UndefValue>(VecOp)) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8890 | Mask.assign(NumVectorElts, UndefValue::get(Type::Int32Ty)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8891 | else { |
| 8892 | assert(isa<ConstantAggregateZero>(VecOp) && "Unknown thing"); |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8893 | Mask.assign(NumVectorElts, ConstantInt::get(Type::Int32Ty, |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8894 | NumVectorElts)); |
| 8895 | } |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8896 | Mask[InsertedIdx] = ConstantInt::get(Type::Int32Ty, ExtractedIdx); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8897 | return new ShuffleVectorInst(EI->getOperand(0), VecOp, |
| 8898 | ConstantPacked::get(Mask)); |
| 8899 | } |
| 8900 | |
| 8901 | // If this insertelement isn't used by some other insertelement, turn it |
| 8902 | // (and any insertelements it points to), into one big shuffle. |
| 8903 | if (!IE.hasOneUse() || !isa<InsertElementInst>(IE.use_back())) { |
| 8904 | std::vector<Constant*> Mask; |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8905 | Value *RHS = 0; |
| 8906 | Value *LHS = CollectShuffleElements(&IE, Mask, RHS); |
| 8907 | if (RHS == 0) RHS = UndefValue::get(LHS->getType()); |
| 8908 | // We now have a shuffle of LHS, RHS, Mask. |
| 8909 | return new ShuffleVectorInst(LHS, RHS, ConstantPacked::get(Mask)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8910 | } |
| 8911 | } |
| 8912 | } |
| 8913 | |
| 8914 | return 0; |
| 8915 | } |
| 8916 | |
| 8917 | |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 8918 | Instruction *InstCombiner::visitShuffleVectorInst(ShuffleVectorInst &SVI) { |
| 8919 | Value *LHS = SVI.getOperand(0); |
| 8920 | Value *RHS = SVI.getOperand(1); |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8921 | std::vector<unsigned> Mask = getShuffleMask(&SVI); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 8922 | |
| 8923 | bool MadeChange = false; |
| 8924 | |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 8925 | // Undefined shuffle mask -> undefined value. |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8926 | if (isa<UndefValue>(SVI.getOperand(2))) |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 8927 | return ReplaceInstUsesWith(SVI, UndefValue::get(SVI.getType())); |
| 8928 | |
Chris Lattner | e4929dd | 2007-01-05 07:36:08 +0000 | [diff] [blame] | 8929 | // If we have shuffle(x, undef, mask) and any elements of mask refer to |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8930 | // the undef, change them to undefs. |
Chris Lattner | e4929dd | 2007-01-05 07:36:08 +0000 | [diff] [blame] | 8931 | if (isa<UndefValue>(SVI.getOperand(1))) { |
| 8932 | // Scan to see if there are any references to the RHS. If so, replace them |
| 8933 | // with undef element refs and set MadeChange to true. |
| 8934 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
| 8935 | if (Mask[i] >= e && Mask[i] != 2*e) { |
| 8936 | Mask[i] = 2*e; |
| 8937 | MadeChange = true; |
| 8938 | } |
| 8939 | } |
| 8940 | |
| 8941 | if (MadeChange) { |
| 8942 | // Remap any references to RHS to use LHS. |
| 8943 | std::vector<Constant*> Elts; |
| 8944 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
| 8945 | if (Mask[i] == 2*e) |
| 8946 | Elts.push_back(UndefValue::get(Type::Int32Ty)); |
| 8947 | else |
| 8948 | Elts.push_back(ConstantInt::get(Type::Int32Ty, Mask[i])); |
| 8949 | } |
| 8950 | SVI.setOperand(2, ConstantPacked::get(Elts)); |
| 8951 | } |
| 8952 | } |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 8953 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8954 | // Canonicalize shuffle(x ,x,mask) -> shuffle(x, undef,mask') |
| 8955 | // Canonicalize shuffle(undef,x,mask) -> shuffle(x, undef,mask'). |
| 8956 | if (LHS == RHS || isa<UndefValue>(LHS)) { |
| 8957 | if (isa<UndefValue>(LHS) && LHS == RHS) { |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 8958 | // shuffle(undef,undef,mask) -> undef. |
| 8959 | return ReplaceInstUsesWith(SVI, LHS); |
| 8960 | } |
| 8961 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8962 | // Remap any references to RHS to use LHS. |
| 8963 | std::vector<Constant*> Elts; |
| 8964 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 8965 | if (Mask[i] >= 2*e) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8966 | Elts.push_back(UndefValue::get(Type::Int32Ty)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 8967 | else { |
| 8968 | if ((Mask[i] >= e && isa<UndefValue>(RHS)) || |
| 8969 | (Mask[i] < e && isa<UndefValue>(LHS))) |
| 8970 | Mask[i] = 2*e; // Turn into undef. |
| 8971 | else |
| 8972 | Mask[i] &= (e-1); // Force to LHS. |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8973 | Elts.push_back(ConstantInt::get(Type::Int32Ty, Mask[i])); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 8974 | } |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 8975 | } |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8976 | SVI.setOperand(0, SVI.getOperand(1)); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 8977 | SVI.setOperand(1, UndefValue::get(RHS->getType())); |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8978 | SVI.setOperand(2, ConstantPacked::get(Elts)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 8979 | LHS = SVI.getOperand(0); |
| 8980 | RHS = SVI.getOperand(1); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 8981 | MadeChange = true; |
| 8982 | } |
| 8983 | |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 8984 | // Analyze the shuffle, are the LHS or RHS and identity shuffles? |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8985 | bool isLHSID = true, isRHSID = true; |
Chris Lattner | 706126d | 2006-04-16 00:03:56 +0000 | [diff] [blame] | 8986 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8987 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
| 8988 | if (Mask[i] >= e*2) continue; // Ignore undef values. |
| 8989 | // Is this an identity shuffle of the LHS value? |
| 8990 | isLHSID &= (Mask[i] == i); |
| 8991 | |
| 8992 | // Is this an identity shuffle of the RHS value? |
| 8993 | isRHSID &= (Mask[i]-e == i); |
Chris Lattner | 706126d | 2006-04-16 00:03:56 +0000 | [diff] [blame] | 8994 | } |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 8995 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8996 | // Eliminate identity shuffles. |
| 8997 | if (isLHSID) return ReplaceInstUsesWith(SVI, LHS); |
| 8998 | if (isRHSID) return ReplaceInstUsesWith(SVI, RHS); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 8999 | |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 9000 | // If the LHS is a shufflevector itself, see if we can combine it with this |
| 9001 | // one without producing an unusual shuffle. Here we are really conservative: |
| 9002 | // we are absolutely afraid of producing a shuffle mask not in the input |
| 9003 | // program, because the code gen may not be smart enough to turn a merged |
| 9004 | // shuffle into two specific shuffles: it may produce worse code. As such, |
| 9005 | // we only merge two shuffles if the result is one of the two input shuffle |
| 9006 | // masks. In this case, merging the shuffles just removes one instruction, |
| 9007 | // which we know is safe. This is good for things like turning: |
| 9008 | // (splat(splat)) -> splat. |
| 9009 | if (ShuffleVectorInst *LHSSVI = dyn_cast<ShuffleVectorInst>(LHS)) { |
| 9010 | if (isa<UndefValue>(RHS)) { |
| 9011 | std::vector<unsigned> LHSMask = getShuffleMask(LHSSVI); |
| 9012 | |
| 9013 | std::vector<unsigned> NewMask; |
| 9014 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) |
| 9015 | if (Mask[i] >= 2*e) |
| 9016 | NewMask.push_back(2*e); |
| 9017 | else |
| 9018 | NewMask.push_back(LHSMask[Mask[i]]); |
| 9019 | |
| 9020 | // If the result mask is equal to the src shuffle or this shuffle mask, do |
| 9021 | // the replacement. |
| 9022 | if (NewMask == LHSMask || NewMask == Mask) { |
| 9023 | std::vector<Constant*> Elts; |
| 9024 | for (unsigned i = 0, e = NewMask.size(); i != e; ++i) { |
| 9025 | if (NewMask[i] >= e*2) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9026 | Elts.push_back(UndefValue::get(Type::Int32Ty)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 9027 | } else { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9028 | Elts.push_back(ConstantInt::get(Type::Int32Ty, NewMask[i])); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 9029 | } |
| 9030 | } |
| 9031 | return new ShuffleVectorInst(LHSSVI->getOperand(0), |
| 9032 | LHSSVI->getOperand(1), |
| 9033 | ConstantPacked::get(Elts)); |
| 9034 | } |
| 9035 | } |
| 9036 | } |
Chris Lattner | c5eff44 | 2007-01-30 22:32:46 +0000 | [diff] [blame] | 9037 | |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 9038 | return MadeChange ? &SVI : 0; |
| 9039 | } |
| 9040 | |
| 9041 | |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 9042 | |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 9043 | void InstCombiner::removeFromWorkList(Instruction *I) { |
| 9044 | WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), I), |
| 9045 | WorkList.end()); |
| 9046 | } |
| 9047 | |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 9048 | |
| 9049 | /// TryToSinkInstruction - Try to move the specified instruction from its |
| 9050 | /// current block into the beginning of DestBlock, which can only happen if it's |
| 9051 | /// safe to move the instruction past all of the instructions between it and the |
| 9052 | /// end of its block. |
| 9053 | static bool TryToSinkInstruction(Instruction *I, BasicBlock *DestBlock) { |
| 9054 | assert(I->hasOneUse() && "Invariants didn't hold!"); |
| 9055 | |
Chris Lattner | 108e902 | 2005-10-27 17:13:11 +0000 | [diff] [blame] | 9056 | // Cannot move control-flow-involving, volatile loads, vaarg, etc. |
| 9057 | if (isa<PHINode>(I) || I->mayWriteToMemory()) return false; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9058 | |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 9059 | // Do not sink alloca instructions out of the entry block. |
| 9060 | if (isa<AllocaInst>(I) && I->getParent() == &DestBlock->getParent()->front()) |
| 9061 | return false; |
| 9062 | |
Chris Lattner | 96a52a6 | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 9063 | // We can only sink load instructions if there is nothing between the load and |
| 9064 | // the end of block that could change the value. |
| 9065 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Chris Lattner | 96a52a6 | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 9066 | for (BasicBlock::iterator Scan = LI, E = LI->getParent()->end(); |
| 9067 | Scan != E; ++Scan) |
| 9068 | if (Scan->mayWriteToMemory()) |
| 9069 | return false; |
Chris Lattner | 96a52a6 | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 9070 | } |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 9071 | |
| 9072 | BasicBlock::iterator InsertPos = DestBlock->begin(); |
| 9073 | while (isa<PHINode>(InsertPos)) ++InsertPos; |
| 9074 | |
Chris Lattner | 4bc5f80 | 2005-08-08 19:11:57 +0000 | [diff] [blame] | 9075 | I->moveBefore(InsertPos); |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 9076 | ++NumSunkInst; |
| 9077 | return true; |
| 9078 | } |
| 9079 | |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9080 | |
| 9081 | /// AddReachableCodeToWorklist - Walk the function in depth-first order, adding |
| 9082 | /// all reachable code to the worklist. |
| 9083 | /// |
| 9084 | /// This has a couple of tricks to make the code faster and more powerful. In |
| 9085 | /// particular, we constant fold and DCE instructions as we go, to avoid adding |
| 9086 | /// them to the worklist (this significantly speeds up instcombine on code where |
| 9087 | /// many instructions are dead or constant). Additionally, if we find a branch |
| 9088 | /// whose condition is a known constant, we only visit the reachable successors. |
| 9089 | /// |
| 9090 | static void AddReachableCodeToWorklist(BasicBlock *BB, |
| 9091 | std::set<BasicBlock*> &Visited, |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 9092 | std::vector<Instruction*> &WorkList, |
| 9093 | const TargetData *TD) { |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9094 | // We have now visited this block! If we've already been here, bail out. |
| 9095 | if (!Visited.insert(BB).second) return; |
| 9096 | |
| 9097 | for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ) { |
| 9098 | Instruction *Inst = BBI++; |
| 9099 | |
| 9100 | // DCE instruction if trivially dead. |
| 9101 | if (isInstructionTriviallyDead(Inst)) { |
| 9102 | ++NumDeadInst; |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 9103 | DOUT << "IC: DCE: " << *Inst; |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9104 | Inst->eraseFromParent(); |
| 9105 | continue; |
| 9106 | } |
| 9107 | |
| 9108 | // ConstantProp instruction if trivially constant. |
Chris Lattner | 0a19ffa | 2007-01-30 23:16:15 +0000 | [diff] [blame] | 9109 | if (Constant *C = ConstantFoldInstruction(Inst, TD)) { |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 9110 | DOUT << "IC: ConstFold to: " << *C << " from: " << *Inst; |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9111 | Inst->replaceAllUsesWith(C); |
| 9112 | ++NumConstProp; |
| 9113 | Inst->eraseFromParent(); |
| 9114 | continue; |
| 9115 | } |
| 9116 | |
| 9117 | WorkList.push_back(Inst); |
| 9118 | } |
| 9119 | |
| 9120 | // Recursively visit successors. If this is a branch or switch on a constant, |
| 9121 | // only visit the reachable successor. |
| 9122 | TerminatorInst *TI = BB->getTerminator(); |
| 9123 | if (BranchInst *BI = dyn_cast<BranchInst>(TI)) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 9124 | if (BI->isConditional() && isa<ConstantInt>(BI->getCondition())) { |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 9125 | bool CondVal = cast<ConstantInt>(BI->getCondition())->getZExtValue(); |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 9126 | AddReachableCodeToWorklist(BI->getSuccessor(!CondVal), Visited, WorkList, |
| 9127 | TD); |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9128 | return; |
| 9129 | } |
| 9130 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) { |
| 9131 | if (ConstantInt *Cond = dyn_cast<ConstantInt>(SI->getCondition())) { |
| 9132 | // See if this is an explicit destination. |
| 9133 | for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i) |
| 9134 | if (SI->getCaseValue(i) == Cond) { |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 9135 | AddReachableCodeToWorklist(SI->getSuccessor(i), Visited, WorkList,TD); |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9136 | return; |
| 9137 | } |
| 9138 | |
| 9139 | // Otherwise it is the default destination. |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 9140 | AddReachableCodeToWorklist(SI->getSuccessor(0), Visited, WorkList, TD); |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9141 | return; |
| 9142 | } |
| 9143 | } |
| 9144 | |
| 9145 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 9146 | AddReachableCodeToWorklist(TI->getSuccessor(i), Visited, WorkList, TD); |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9147 | } |
| 9148 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 9149 | bool InstCombiner::runOnFunction(Function &F) { |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 9150 | bool Changed = false; |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 9151 | TD = &getAnalysis<TargetData>(); |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9152 | |
Chris Lattner | b3d5970 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 9153 | { |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9154 | // Do a depth-first traversal of the function, populate the worklist with |
| 9155 | // the reachable instructions. Ignore blocks that are not reachable. Keep |
| 9156 | // track of which blocks we visit. |
Chris Lattner | b3d5970 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 9157 | std::set<BasicBlock*> Visited; |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 9158 | AddReachableCodeToWorklist(F.begin(), Visited, WorkList, TD); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 9159 | |
Chris Lattner | b3d5970 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 9160 | // Do a quick scan over the function. If we find any blocks that are |
| 9161 | // unreachable, remove any instructions inside of them. This prevents |
| 9162 | // the instcombine code from having to deal with some bad special cases. |
| 9163 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 9164 | if (!Visited.count(BB)) { |
| 9165 | Instruction *Term = BB->getTerminator(); |
| 9166 | while (Term != BB->begin()) { // Remove instrs bottom-up |
| 9167 | BasicBlock::iterator I = Term; --I; |
Chris Lattner | 6ffe551 | 2004-04-27 15:13:33 +0000 | [diff] [blame] | 9168 | |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 9169 | DOUT << "IC: DCE: " << *I; |
Chris Lattner | b3d5970 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 9170 | ++NumDeadInst; |
| 9171 | |
| 9172 | if (!I->use_empty()) |
| 9173 | I->replaceAllUsesWith(UndefValue::get(I->getType())); |
| 9174 | I->eraseFromParent(); |
| 9175 | } |
| 9176 | } |
| 9177 | } |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9178 | |
| 9179 | while (!WorkList.empty()) { |
| 9180 | Instruction *I = WorkList.back(); // Get an instruction from the worklist |
| 9181 | WorkList.pop_back(); |
| 9182 | |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 9183 | // Check to see if we can DCE the instruction. |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 9184 | if (isInstructionTriviallyDead(I)) { |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 9185 | // Add operands to the worklist. |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9186 | if (I->getNumOperands() < 4) |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 9187 | AddUsesToWorkList(*I); |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 9188 | ++NumDeadInst; |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9189 | |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 9190 | DOUT << "IC: DCE: " << *I; |
Chris Lattner | ad5fec1 | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 9191 | |
| 9192 | I->eraseFromParent(); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9193 | removeFromWorkList(I); |
| 9194 | continue; |
| 9195 | } |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 9196 | |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 9197 | // Instruction isn't dead, see if we can constant propagate it. |
Chris Lattner | 0a19ffa | 2007-01-30 23:16:15 +0000 | [diff] [blame] | 9198 | if (Constant *C = ConstantFoldInstruction(I, TD)) { |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 9199 | DOUT << "IC: ConstFold to: " << *C << " from: " << *I; |
Chris Lattner | ad5fec1 | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 9200 | |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 9201 | // Add operands to the worklist. |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 9202 | AddUsesToWorkList(*I); |
Chris Lattner | c736d56 | 2002-12-05 22:41:53 +0000 | [diff] [blame] | 9203 | ReplaceInstUsesWith(*I, C); |
| 9204 | |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 9205 | ++NumConstProp; |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9206 | I->eraseFromParent(); |
Chris Lattner | 6061000 | 2003-10-07 15:17:02 +0000 | [diff] [blame] | 9207 | removeFromWorkList(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9208 | continue; |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 9209 | } |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9210 | |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 9211 | // See if we can trivially sink this instruction to a successor basic block. |
| 9212 | if (I->hasOneUse()) { |
| 9213 | BasicBlock *BB = I->getParent(); |
| 9214 | BasicBlock *UserParent = cast<Instruction>(I->use_back())->getParent(); |
| 9215 | if (UserParent != BB) { |
| 9216 | bool UserIsSuccessor = false; |
| 9217 | // See if the user is one of our successors. |
| 9218 | for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI) |
| 9219 | if (*SI == UserParent) { |
| 9220 | UserIsSuccessor = true; |
| 9221 | break; |
| 9222 | } |
| 9223 | |
| 9224 | // If the user is one of our immediate successors, and if that successor |
| 9225 | // only has us as a predecessors (we'd have to split the critical edge |
| 9226 | // otherwise), we can keep going. |
| 9227 | if (UserIsSuccessor && !isa<PHINode>(I->use_back()) && |
| 9228 | next(pred_begin(UserParent)) == pred_end(UserParent)) |
| 9229 | // Okay, the CFG is simple enough, try to sink this instruction. |
| 9230 | Changed |= TryToSinkInstruction(I, UserParent); |
| 9231 | } |
| 9232 | } |
| 9233 | |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9234 | // Now that we have an instruction, try combining it to simplify it... |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9235 | if (Instruction *Result = visit(*I)) { |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 9236 | ++NumCombined; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 9237 | // Should we replace the old instruction with a new one? |
Chris Lattner | b3bc8fa | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 9238 | if (Result != I) { |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 9239 | DOUT << "IC: Old = " << *I |
| 9240 | << " New = " << *Result; |
Chris Lattner | 0cea42a | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 9241 | |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 9242 | // Everything uses the new instruction now. |
| 9243 | I->replaceAllUsesWith(Result); |
| 9244 | |
| 9245 | // Push the new instruction and any users onto the worklist. |
| 9246 | WorkList.push_back(Result); |
| 9247 | AddUsersToWorkList(*Result); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9248 | |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 9249 | // Move the name to the new instruction first. |
| 9250 | Result->takeName(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9251 | |
| 9252 | // Insert the new instruction into the basic block... |
| 9253 | BasicBlock *InstParent = I->getParent(); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9254 | BasicBlock::iterator InsertPos = I; |
| 9255 | |
| 9256 | if (!isa<PHINode>(Result)) // If combining a PHI, don't insert |
| 9257 | while (isa<PHINode>(InsertPos)) // middle of a block of PHIs. |
| 9258 | ++InsertPos; |
| 9259 | |
| 9260 | InstParent->getInstList().insert(InsertPos, Result); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9261 | |
Chris Lattner | 00d5131 | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 9262 | // Make sure that we reprocess all operands now that we reduced their |
| 9263 | // use counts. |
Chris Lattner | 216d4d8 | 2004-05-01 23:19:52 +0000 | [diff] [blame] | 9264 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 9265 | if (Instruction *OpI = dyn_cast<Instruction>(I->getOperand(i))) |
| 9266 | WorkList.push_back(OpI); |
| 9267 | |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 9268 | // Instructions can end up on the worklist more than once. Make sure |
| 9269 | // we do not process an instruction that has been deleted. |
| 9270 | removeFromWorkList(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9271 | |
| 9272 | // Erase the old instruction. |
| 9273 | InstParent->getInstList().erase(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 9274 | } else { |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 9275 | DOUT << "IC: MOD = " << *I; |
Chris Lattner | 0cea42a | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 9276 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9277 | // If the instruction was modified, it's possible that it is now dead. |
| 9278 | // if so, remove it. |
Chris Lattner | 00d5131 | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 9279 | if (isInstructionTriviallyDead(I)) { |
| 9280 | // Make sure we process all operands now that we are reducing their |
| 9281 | // use counts. |
| 9282 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 9283 | if (Instruction *OpI = dyn_cast<Instruction>(I->getOperand(i))) |
| 9284 | WorkList.push_back(OpI); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9285 | |
Chris Lattner | 00d5131 | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 9286 | // Instructions may end up in the worklist more than once. Erase all |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 9287 | // occurrences of this instruction. |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 9288 | removeFromWorkList(I); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 9289 | I->eraseFromParent(); |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 9290 | } else { |
| 9291 | WorkList.push_back(Result); |
| 9292 | AddUsersToWorkList(*Result); |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9293 | } |
Chris Lattner | b3bc8fa | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 9294 | } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 9295 | Changed = true; |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9296 | } |
| 9297 | } |
| 9298 | |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 9299 | return Changed; |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 9300 | } |
| 9301 | |
Brian Gaeke | 96d4bf7 | 2004-07-27 17:43:21 +0000 | [diff] [blame] | 9302 | FunctionPass *llvm::createInstructionCombiningPass() { |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 9303 | return new InstCombiner(); |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 9304 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 9305 | |