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. |
Chris Lattner | 2cd9196 | 2003-07-23 21:41:57 +0000 | [diff] [blame] | 27 | // 3. SetCC instructions are converted from <,>,<=,>= to ==,!= if possible |
| 28 | // 4. All SetCC 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 | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 42 | #include "llvm/Target/TargetData.h" |
| 43 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 44 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 45 | #include "llvm/Support/CallSite.h" |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 46 | #include "llvm/Support/Debug.h" |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 47 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Chris Lattner | 221d688 | 2002-02-12 21:07:25 +0000 | [diff] [blame] | 48 | #include "llvm/Support/InstIterator.h" |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 49 | #include "llvm/Support/InstVisitor.h" |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 50 | #include "llvm/Support/PatternMatch.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 51 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 52 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | b3bc8fa | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 53 | #include <algorithm> |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 54 | using namespace llvm; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 55 | using namespace llvm::PatternMatch; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 56 | |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 57 | namespace { |
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 58 | Statistic<> NumCombined ("instcombine", "Number of insts combined"); |
| 59 | Statistic<> NumConstProp("instcombine", "Number of constant folds"); |
| 60 | Statistic<> NumDeadInst ("instcombine", "Number of dead inst eliminated"); |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 61 | Statistic<> NumSunkInst ("instcombine", "Number of instructions sunk"); |
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 62 | |
Chris Lattner | f57b845 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 63 | class InstCombiner : public FunctionPass, |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 64 | public InstVisitor<InstCombiner, Instruction*> { |
| 65 | // Worklist of all of the instructions that need to be simplified. |
| 66 | std::vector<Instruction*> WorkList; |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 67 | TargetData *TD; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 68 | |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 69 | /// AddUsersToWorkList - When an instruction is simplified, add all users of |
| 70 | /// the instruction to the work lists because they might get more simplified |
| 71 | /// now. |
| 72 | /// |
| 73 | void AddUsersToWorkList(Instruction &I) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 74 | for (Value::use_iterator UI = I.use_begin(), UE = I.use_end(); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 75 | UI != UE; ++UI) |
| 76 | WorkList.push_back(cast<Instruction>(*UI)); |
| 77 | } |
| 78 | |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 79 | /// AddUsesToWorkList - When an instruction is simplified, add operands to |
| 80 | /// the work lists because they might get more simplified now. |
| 81 | /// |
| 82 | void AddUsesToWorkList(Instruction &I) { |
| 83 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) |
| 84 | if (Instruction *Op = dyn_cast<Instruction>(I.getOperand(i))) |
| 85 | WorkList.push_back(Op); |
| 86 | } |
| 87 | |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 88 | // removeFromWorkList - remove all instances of I from the worklist. |
| 89 | void removeFromWorkList(Instruction *I); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 90 | public: |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 91 | virtual bool runOnFunction(Function &F); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 92 | |
Chris Lattner | 97e52e4 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 93 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 94 | AU.addRequired<TargetData>(); |
Chris Lattner | cb2610e | 2002-10-21 20:00:28 +0000 | [diff] [blame] | 95 | AU.setPreservesCFG(); |
Chris Lattner | 97e52e4 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 98 | TargetData &getTargetData() const { return *TD; } |
| 99 | |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 100 | // Visitation implementation - Implement instruction combining for different |
| 101 | // instruction types. The semantics are as follows: |
| 102 | // Return Value: |
| 103 | // null - No change was made |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 104 | // 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] | 105 | // otherwise - Change was made, replace I with returned instruction |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 106 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 107 | Instruction *visitAdd(BinaryOperator &I); |
| 108 | Instruction *visitSub(BinaryOperator &I); |
| 109 | Instruction *visitMul(BinaryOperator &I); |
| 110 | Instruction *visitDiv(BinaryOperator &I); |
| 111 | Instruction *visitRem(BinaryOperator &I); |
| 112 | Instruction *visitAnd(BinaryOperator &I); |
| 113 | Instruction *visitOr (BinaryOperator &I); |
| 114 | Instruction *visitXor(BinaryOperator &I); |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 115 | Instruction *visitSetCondInst(SetCondInst &I); |
| 116 | Instruction *visitSetCondInstWithCastAndCast(SetCondInst &SCI); |
| 117 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 118 | Instruction *FoldGEPSetCC(User *GEPLHS, Value *RHS, |
| 119 | Instruction::BinaryOps Cond, Instruction &I); |
Chris Lattner | ea34005 | 2003-03-10 19:16:08 +0000 | [diff] [blame] | 120 | Instruction *visitShiftInst(ShiftInst &I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 121 | Instruction *visitCastInst(CastInst &CI); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 122 | Instruction *FoldSelectOpOp(SelectInst &SI, Instruction *TI, |
| 123 | Instruction *FI); |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 124 | Instruction *visitSelectInst(SelectInst &CI); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 125 | Instruction *visitCallInst(CallInst &CI); |
| 126 | Instruction *visitInvokeInst(InvokeInst &II); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 127 | Instruction *visitPHINode(PHINode &PN); |
| 128 | Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP); |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 129 | Instruction *visitAllocationInst(AllocationInst &AI); |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 130 | Instruction *visitFreeInst(FreeInst &FI); |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 131 | Instruction *visitLoadInst(LoadInst &LI); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 132 | Instruction *visitStoreInst(StoreInst &SI); |
Chris Lattner | c4d10eb | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 133 | Instruction *visitBranchInst(BranchInst &BI); |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 134 | Instruction *visitSwitchInst(SwitchInst &SI); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 135 | |
| 136 | // visitInstruction - Specify what to return for unhandled instructions... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 137 | Instruction *visitInstruction(Instruction &I) { return 0; } |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 138 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 139 | private: |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 140 | Instruction *visitCallSite(CallSite CS); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 141 | bool transformConstExprCastCall(CallSite CS); |
| 142 | |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 143 | public: |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 144 | // InsertNewInstBefore - insert an instruction New before instruction Old |
| 145 | // in the program. Add the new instruction to the worklist. |
| 146 | // |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 147 | Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) { |
Chris Lattner | e6f9a91 | 2002-08-23 18:32:43 +0000 | [diff] [blame] | 148 | assert(New && New->getParent() == 0 && |
| 149 | "New instruction already inserted into a basic block!"); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 150 | BasicBlock *BB = Old.getParent(); |
| 151 | BB->getInstList().insert(&Old, New); // Insert inst |
| 152 | WorkList.push_back(New); // Add to worklist |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 153 | return New; |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 156 | /// InsertCastBefore - Insert a cast of V to TY before the instruction POS. |
| 157 | /// This also adds the cast to the worklist. Finally, this returns the |
| 158 | /// cast. |
| 159 | Value *InsertCastBefore(Value *V, const Type *Ty, Instruction &Pos) { |
| 160 | if (V->getType() == Ty) return V; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 161 | |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 162 | Instruction *C = new CastInst(V, Ty, V->getName(), &Pos); |
| 163 | WorkList.push_back(C); |
| 164 | return C; |
| 165 | } |
| 166 | |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 167 | // ReplaceInstUsesWith - This method is to be used when an instruction is |
| 168 | // found to be dead, replacable with another preexisting expression. Here |
| 169 | // we add all uses of I to the worklist, replace all uses of I with the new |
| 170 | // value, then return I, so that the inst combiner will know that I was |
| 171 | // modified. |
| 172 | // |
| 173 | Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) { |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 174 | AddUsersToWorkList(I); // Add all modified instrs to worklist |
Chris Lattner | 15a76c0 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 175 | if (&I != V) { |
| 176 | I.replaceAllUsesWith(V); |
| 177 | return &I; |
| 178 | } else { |
| 179 | // If we are replacing the instruction with itself, this must be in a |
| 180 | // segment of unreachable code, so just clobber the instruction. |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 181 | I.replaceAllUsesWith(UndefValue::get(I.getType())); |
Chris Lattner | 15a76c0 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 182 | return &I; |
| 183 | } |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 184 | } |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 185 | |
| 186 | // EraseInstFromFunction - When dealing with an instruction that has side |
| 187 | // effects or produces a void value, we can't rely on DCE to delete the |
| 188 | // instruction. Instead, visit methods should return the value returned by |
| 189 | // this function. |
| 190 | Instruction *EraseInstFromFunction(Instruction &I) { |
| 191 | assert(I.use_empty() && "Cannot erase instruction that is used!"); |
| 192 | AddUsesToWorkList(I); |
| 193 | removeFromWorkList(&I); |
Chris Lattner | 954f66a | 2004-11-18 21:41:39 +0000 | [diff] [blame] | 194 | I.eraseFromParent(); |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 195 | return 0; // Don't do anything with FI |
| 196 | } |
| 197 | |
| 198 | |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 199 | private: |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 200 | /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the |
| 201 | /// InsertBefore instruction. This is specialized a bit to avoid inserting |
| 202 | /// casts that are known to not do anything... |
| 203 | /// |
| 204 | Value *InsertOperandCastBefore(Value *V, const Type *DestTy, |
| 205 | Instruction *InsertBefore); |
| 206 | |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 207 | // SimplifyCommutative - This performs a few simplifications for commutative |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 208 | // operators. |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 209 | bool SimplifyCommutative(BinaryOperator &I); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 210 | |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 211 | |
| 212 | // FoldOpIntoPhi - Given a binary operator or cast instruction which has a |
| 213 | // PHI node as operand #0, see if we can fold the instruction into the PHI |
| 214 | // (which is only possible if all operands to the PHI are constants). |
| 215 | Instruction *FoldOpIntoPhi(Instruction &I); |
| 216 | |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 217 | // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary" |
| 218 | // operator and they all are only used by the PHI, PHI together their |
| 219 | // inputs, and do the operation once, to the result of the PHI. |
| 220 | Instruction *FoldPHIArgOpIntoPHI(PHINode &PN); |
| 221 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 222 | Instruction *OptAndOp(Instruction *Op, ConstantIntegral *OpRHS, |
| 223 | ConstantIntegral *AndRHS, BinaryOperator &TheAnd); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 224 | |
| 225 | Instruction *InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, |
| 226 | bool Inside, Instruction &IB); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 227 | }; |
Chris Lattner | f629309 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 228 | |
Chris Lattner | a6275cc | 2002-07-26 21:12:46 +0000 | [diff] [blame] | 229 | RegisterOpt<InstCombiner> X("instcombine", "Combine redundant instructions"); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 232 | // getComplexity: Assign a complexity or rank value to LLVM Values... |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 233 | // 0 -> undef, 1 -> Const, 2 -> Other, 3 -> Arg, 3 -> Unary, 4 -> OtherInst |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 234 | static unsigned getComplexity(Value *V) { |
| 235 | if (isa<Instruction>(V)) { |
| 236 | if (BinaryOperator::isNeg(V) || BinaryOperator::isNot(V)) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 237 | return 3; |
| 238 | return 4; |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 239 | } |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 240 | if (isa<Argument>(V)) return 3; |
| 241 | return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2; |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 242 | } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 243 | |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 244 | // isOnlyUse - Return true if this instruction will be deleted if we stop using |
| 245 | // it. |
| 246 | static bool isOnlyUse(Value *V) { |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 247 | return V->hasOneUse() || isa<Constant>(V); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 250 | // getPromotedType - Return the specified type promoted as it would be to pass |
| 251 | // though a va_arg area... |
| 252 | static const Type *getPromotedType(const Type *Ty) { |
Chris Lattner | 5dd0402 | 2004-06-17 18:16:02 +0000 | [diff] [blame] | 253 | switch (Ty->getTypeID()) { |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 254 | case Type::SByteTyID: |
| 255 | case Type::ShortTyID: return Type::IntTy; |
| 256 | case Type::UByteTyID: |
| 257 | case Type::UShortTyID: return Type::UIntTy; |
| 258 | case Type::FloatTyID: return Type::DoubleTy; |
| 259 | default: return Ty; |
| 260 | } |
| 261 | } |
| 262 | |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 263 | // SimplifyCommutative - This performs a few simplifications for commutative |
| 264 | // operators: |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 265 | // |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 266 | // 1. Order operands such that they are listed from right (least complex) to |
| 267 | // left (most complex). This puts constants before unary operators before |
| 268 | // binary operators. |
| 269 | // |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 270 | // 2. Transform: (op (op V, C1), C2) ==> (op V, (op C1, C2)) |
| 271 | // 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] | 272 | // |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 273 | bool InstCombiner::SimplifyCommutative(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 274 | bool Changed = false; |
| 275 | if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1))) |
| 276 | Changed = !I.swapOperands(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 277 | |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 278 | if (!I.isAssociative()) return Changed; |
| 279 | Instruction::BinaryOps Opcode = I.getOpcode(); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 280 | if (BinaryOperator *Op = dyn_cast<BinaryOperator>(I.getOperand(0))) |
| 281 | if (Op->getOpcode() == Opcode && isa<Constant>(Op->getOperand(1))) { |
| 282 | if (isa<Constant>(I.getOperand(1))) { |
Chris Lattner | 2a9c847 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 283 | Constant *Folded = ConstantExpr::get(I.getOpcode(), |
| 284 | cast<Constant>(I.getOperand(1)), |
| 285 | cast<Constant>(Op->getOperand(1))); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 286 | I.setOperand(0, Op->getOperand(0)); |
| 287 | I.setOperand(1, Folded); |
| 288 | return true; |
| 289 | } else if (BinaryOperator *Op1=dyn_cast<BinaryOperator>(I.getOperand(1))) |
| 290 | if (Op1->getOpcode() == Opcode && isa<Constant>(Op1->getOperand(1)) && |
| 291 | isOnlyUse(Op) && isOnlyUse(Op1)) { |
| 292 | Constant *C1 = cast<Constant>(Op->getOperand(1)); |
| 293 | Constant *C2 = cast<Constant>(Op1->getOperand(1)); |
| 294 | |
| 295 | // 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] | 296 | Constant *Folded = ConstantExpr::get(I.getOpcode(), C1, C2); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 297 | Instruction *New = BinaryOperator::create(Opcode, Op->getOperand(0), |
| 298 | Op1->getOperand(0), |
| 299 | Op1->getName(), &I); |
| 300 | WorkList.push_back(New); |
| 301 | I.setOperand(0, New); |
| 302 | I.setOperand(1, Folded); |
| 303 | return true; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 304 | } |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 305 | } |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 306 | return Changed; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 307 | } |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 308 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 309 | // dyn_castNegVal - Given a 'sub' instruction, return the RHS of the instruction |
| 310 | // if the LHS is a constant zero (which is the 'negate' form). |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 311 | // |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 312 | static inline Value *dyn_castNegVal(Value *V) { |
| 313 | if (BinaryOperator::isNeg(V)) |
Chris Lattner | a1df33c | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 314 | return BinaryOperator::getNegArgument(V); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 315 | |
Chris Lattner | 0ce8580 | 2004-12-14 20:08:06 +0000 | [diff] [blame] | 316 | // Constants can be considered to be negated values if they can be folded. |
| 317 | if (ConstantInt *C = dyn_cast<ConstantInt>(V)) |
| 318 | return ConstantExpr::getNeg(C); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 319 | return 0; |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 322 | static inline Value *dyn_castNotVal(Value *V) { |
| 323 | if (BinaryOperator::isNot(V)) |
Chris Lattner | a1df33c | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 324 | return BinaryOperator::getNotArgument(V); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 325 | |
| 326 | // Constants can be considered to be not'ed values... |
Chris Lattner | 3f2ec39 | 2003-04-30 22:34:06 +0000 | [diff] [blame] | 327 | if (ConstantIntegral *C = dyn_cast<ConstantIntegral>(V)) |
Chris Lattner | 448c323 | 2004-06-10 02:12:35 +0000 | [diff] [blame] | 328 | return ConstantExpr::getNot(C); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 329 | return 0; |
| 330 | } |
| 331 | |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 332 | // dyn_castFoldableMul - If this value is a multiply that can be folded into |
| 333 | // other computations (because it has a constant operand), return the |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 334 | // non-constant operand of the multiply, and set CST to point to the multiplier. |
| 335 | // Otherwise, return null. |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 336 | // |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 337 | static inline Value *dyn_castFoldableMul(Value *V, ConstantInt *&CST) { |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 338 | if (V->hasOneUse() && V->getType()->isInteger()) |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 339 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 340 | if (I->getOpcode() == Instruction::Mul) |
Chris Lattner | 50e60c7 | 2004-11-15 05:54:07 +0000 | [diff] [blame] | 341 | if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 342 | return I->getOperand(0); |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 343 | if (I->getOpcode() == Instruction::Shl) |
Chris Lattner | 50e60c7 | 2004-11-15 05:54:07 +0000 | [diff] [blame] | 344 | if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) { |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 345 | // The multiplier is really 1 << CST. |
| 346 | Constant *One = ConstantInt::get(V->getType(), 1); |
| 347 | CST = cast<ConstantInt>(ConstantExpr::getShl(One, CST)); |
| 348 | return I->getOperand(0); |
| 349 | } |
| 350 | } |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 351 | return 0; |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 352 | } |
Chris Lattner | af2930e | 2002-08-14 17:51:49 +0000 | [diff] [blame] | 353 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 354 | /// dyn_castGetElementPtr - If this is a getelementptr instruction or constant |
| 355 | /// expression, return it. |
| 356 | static User *dyn_castGetElementPtr(Value *V) { |
| 357 | if (isa<GetElementPtrInst>(V)) return cast<User>(V); |
| 358 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
| 359 | if (CE->getOpcode() == Instruction::GetElementPtr) |
| 360 | return cast<User>(V); |
| 361 | return false; |
| 362 | } |
| 363 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 364 | // Log2 - Calculate the log base 2 for the specified value if it is exactly a |
| 365 | // power of 2. |
| 366 | static unsigned Log2(uint64_t Val) { |
| 367 | assert(Val > 1 && "Values 0 and 1 should be handled elsewhere!"); |
| 368 | unsigned Count = 0; |
| 369 | while (Val != 1) { |
| 370 | if (Val & 1) return 0; // Multiple bits set? |
| 371 | Val >>= 1; |
| 372 | ++Count; |
| 373 | } |
| 374 | return Count; |
Chris Lattner | af2930e | 2002-08-14 17:51:49 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 377 | // AddOne, SubOne - Add or subtract a constant one from an integer constant... |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 378 | static ConstantInt *AddOne(ConstantInt *C) { |
| 379 | return cast<ConstantInt>(ConstantExpr::getAdd(C, |
| 380 | ConstantInt::get(C->getType(), 1))); |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 381 | } |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 382 | static ConstantInt *SubOne(ConstantInt *C) { |
| 383 | return cast<ConstantInt>(ConstantExpr::getSub(C, |
| 384 | ConstantInt::get(C->getType(), 1))); |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | // isTrueWhenEqual - Return true if the specified setcondinst instruction is |
| 388 | // true when both operands are equal... |
| 389 | // |
| 390 | static bool isTrueWhenEqual(Instruction &I) { |
| 391 | return I.getOpcode() == Instruction::SetEQ || |
| 392 | I.getOpcode() == Instruction::SetGE || |
| 393 | I.getOpcode() == Instruction::SetLE; |
| 394 | } |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 395 | |
| 396 | /// AssociativeOpt - Perform an optimization on an associative operator. This |
| 397 | /// function is designed to check a chain of associative operators for a |
| 398 | /// potential to apply a certain optimization. Since the optimization may be |
| 399 | /// applicable if the expression was reassociated, this checks the chain, then |
| 400 | /// reassociates the expression as necessary to expose the optimization |
| 401 | /// opportunity. This makes use of a special Functor, which must define |
| 402 | /// 'shouldApply' and 'apply' methods. |
| 403 | /// |
| 404 | template<typename Functor> |
| 405 | Instruction *AssociativeOpt(BinaryOperator &Root, const Functor &F) { |
| 406 | unsigned Opcode = Root.getOpcode(); |
| 407 | Value *LHS = Root.getOperand(0); |
| 408 | |
| 409 | // Quick check, see if the immediate LHS matches... |
| 410 | if (F.shouldApply(LHS)) |
| 411 | return F.apply(Root); |
| 412 | |
| 413 | // Otherwise, if the LHS is not of the same opcode as the root, return. |
| 414 | Instruction *LHSI = dyn_cast<Instruction>(LHS); |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 415 | while (LHSI && LHSI->getOpcode() == Opcode && LHSI->hasOneUse()) { |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 416 | // Should we apply this transform to the RHS? |
| 417 | bool ShouldApply = F.shouldApply(LHSI->getOperand(1)); |
| 418 | |
| 419 | // If not to the RHS, check to see if we should apply to the LHS... |
| 420 | if (!ShouldApply && F.shouldApply(LHSI->getOperand(0))) { |
| 421 | cast<BinaryOperator>(LHSI)->swapOperands(); // Make the LHS the RHS |
| 422 | ShouldApply = true; |
| 423 | } |
| 424 | |
| 425 | // If the functor wants to apply the optimization to the RHS of LHSI, |
| 426 | // reassociate the expression from ((? op A) op B) to (? op (A op B)) |
| 427 | if (ShouldApply) { |
| 428 | BasicBlock *BB = Root.getParent(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 429 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 430 | // Now all of the instructions are in the current basic block, go ahead |
| 431 | // and perform the reassociation. |
| 432 | Instruction *TmpLHSI = cast<Instruction>(Root.getOperand(0)); |
| 433 | |
| 434 | // First move the selected RHS to the LHS of the root... |
| 435 | Root.setOperand(0, LHSI->getOperand(1)); |
| 436 | |
| 437 | // Make what used to be the LHS of the root be the user of the root... |
| 438 | Value *ExtraOperand = TmpLHSI->getOperand(1); |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 439 | if (&Root == TmpLHSI) { |
Chris Lattner | 15a76c0 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 440 | Root.replaceAllUsesWith(Constant::getNullValue(TmpLHSI->getType())); |
| 441 | return 0; |
| 442 | } |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 443 | Root.replaceAllUsesWith(TmpLHSI); // Users now use TmpLHSI |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 444 | TmpLHSI->setOperand(1, &Root); // TmpLHSI now uses the root |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 445 | TmpLHSI->getParent()->getInstList().remove(TmpLHSI); |
| 446 | BasicBlock::iterator ARI = &Root; ++ARI; |
| 447 | BB->getInstList().insert(ARI, TmpLHSI); // Move TmpLHSI to after Root |
| 448 | ARI = Root; |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 449 | |
| 450 | // Now propagate the ExtraOperand down the chain of instructions until we |
| 451 | // get to LHSI. |
| 452 | while (TmpLHSI != LHSI) { |
| 453 | Instruction *NextLHSI = cast<Instruction>(TmpLHSI->getOperand(0)); |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 454 | // Move the instruction to immediately before the chain we are |
| 455 | // constructing to avoid breaking dominance properties. |
| 456 | NextLHSI->getParent()->getInstList().remove(NextLHSI); |
| 457 | BB->getInstList().insert(ARI, NextLHSI); |
| 458 | ARI = NextLHSI; |
| 459 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 460 | Value *NextOp = NextLHSI->getOperand(1); |
| 461 | NextLHSI->setOperand(1, ExtraOperand); |
| 462 | TmpLHSI = NextLHSI; |
| 463 | ExtraOperand = NextOp; |
| 464 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 465 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 466 | // Now that the instructions are reassociated, have the functor perform |
| 467 | // the transformation... |
| 468 | return F.apply(Root); |
| 469 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 470 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 471 | LHSI = dyn_cast<Instruction>(LHSI->getOperand(0)); |
| 472 | } |
| 473 | return 0; |
| 474 | } |
| 475 | |
| 476 | |
| 477 | // AddRHS - Implements: X + X --> X << 1 |
| 478 | struct AddRHS { |
| 479 | Value *RHS; |
| 480 | AddRHS(Value *rhs) : RHS(rhs) {} |
| 481 | bool shouldApply(Value *LHS) const { return LHS == RHS; } |
| 482 | Instruction *apply(BinaryOperator &Add) const { |
| 483 | return new ShiftInst(Instruction::Shl, Add.getOperand(0), |
| 484 | ConstantInt::get(Type::UByteTy, 1)); |
| 485 | } |
| 486 | }; |
| 487 | |
| 488 | // AddMaskingAnd - Implements (A & C1)+(B & C2) --> (A & C1)|(B & C2) |
| 489 | // iff C1&C2 == 0 |
| 490 | struct AddMaskingAnd { |
| 491 | Constant *C2; |
| 492 | AddMaskingAnd(Constant *c) : C2(c) {} |
| 493 | bool shouldApply(Value *LHS) const { |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 494 | ConstantInt *C1; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 495 | return match(LHS, m_And(m_Value(), m_ConstantInt(C1))) && |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 496 | ConstantExpr::getAnd(C1, C2)->isNullValue(); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 497 | } |
| 498 | Instruction *apply(BinaryOperator &Add) const { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 499 | return BinaryOperator::createOr(Add.getOperand(0), Add.getOperand(1)); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 500 | } |
| 501 | }; |
| 502 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 503 | static Value *FoldOperationIntoSelectOperand(Instruction &I, Value *SO, |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 504 | InstCombiner *IC) { |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 505 | if (isa<CastInst>(I)) { |
| 506 | if (Constant *SOC = dyn_cast<Constant>(SO)) |
| 507 | return ConstantExpr::getCast(SOC, I.getType()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 508 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 509 | return IC->InsertNewInstBefore(new CastInst(SO, I.getType(), |
| 510 | SO->getName() + ".cast"), I); |
| 511 | } |
| 512 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 513 | // Figure out if the constant is the left or the right argument. |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 514 | bool ConstIsRHS = isa<Constant>(I.getOperand(1)); |
| 515 | Constant *ConstOperand = cast<Constant>(I.getOperand(ConstIsRHS)); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 516 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 517 | if (Constant *SOC = dyn_cast<Constant>(SO)) { |
| 518 | if (ConstIsRHS) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 519 | return ConstantExpr::get(I.getOpcode(), SOC, ConstOperand); |
| 520 | return ConstantExpr::get(I.getOpcode(), ConstOperand, SOC); |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | Value *Op0 = SO, *Op1 = ConstOperand; |
| 524 | if (!ConstIsRHS) |
| 525 | std::swap(Op0, Op1); |
| 526 | Instruction *New; |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 527 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I)) |
| 528 | New = BinaryOperator::create(BO->getOpcode(), Op0, Op1,SO->getName()+".op"); |
| 529 | else if (ShiftInst *SI = dyn_cast<ShiftInst>(&I)) |
| 530 | New = new ShiftInst(SI->getOpcode(), Op0, Op1, SO->getName()+".sh"); |
Chris Lattner | 326c0f3 | 2004-04-10 19:15:56 +0000 | [diff] [blame] | 531 | else { |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 532 | assert(0 && "Unknown binary instruction type!"); |
Chris Lattner | 326c0f3 | 2004-04-10 19:15:56 +0000 | [diff] [blame] | 533 | abort(); |
| 534 | } |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 535 | return IC->InsertNewInstBefore(New, I); |
| 536 | } |
| 537 | |
| 538 | // FoldOpIntoSelect - Given an instruction with a select as one operand and a |
| 539 | // constant as the other operand, try to fold the binary operator into the |
| 540 | // select arguments. This also works for Cast instructions, which obviously do |
| 541 | // not have a second operand. |
| 542 | static Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI, |
| 543 | InstCombiner *IC) { |
| 544 | // Don't modify shared select instructions |
| 545 | if (!SI->hasOneUse()) return 0; |
| 546 | Value *TV = SI->getOperand(1); |
| 547 | Value *FV = SI->getOperand(2); |
| 548 | |
| 549 | if (isa<Constant>(TV) || isa<Constant>(FV)) { |
Chris Lattner | 956db27 | 2005-04-21 05:43:13 +0000 | [diff] [blame] | 550 | // Bool selects with constant operands can be folded to logical ops. |
| 551 | if (SI->getType() == Type::BoolTy) return 0; |
| 552 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 553 | Value *SelectTrueVal = FoldOperationIntoSelectOperand(Op, TV, IC); |
| 554 | Value *SelectFalseVal = FoldOperationIntoSelectOperand(Op, FV, IC); |
| 555 | |
| 556 | return new SelectInst(SI->getCondition(), SelectTrueVal, |
| 557 | SelectFalseVal); |
| 558 | } |
| 559 | return 0; |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 560 | } |
| 561 | |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 562 | |
| 563 | /// FoldOpIntoPhi - Given a binary operator or cast instruction which has a PHI |
| 564 | /// node as operand #0, see if we can fold the instruction into the PHI (which |
| 565 | /// is only possible if all operands to the PHI are constants). |
| 566 | Instruction *InstCombiner::FoldOpIntoPhi(Instruction &I) { |
| 567 | PHINode *PN = cast<PHINode>(I.getOperand(0)); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 568 | unsigned NumPHIValues = PN->getNumIncomingValues(); |
| 569 | if (!PN->hasOneUse() || NumPHIValues == 0 || |
| 570 | !isa<Constant>(PN->getIncomingValue(0))) return 0; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 571 | |
| 572 | // Check to see if all of the operands of the PHI are constants. If not, we |
| 573 | // cannot do the transformation. |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 574 | for (unsigned i = 1; i != NumPHIValues; ++i) |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 575 | if (!isa<Constant>(PN->getIncomingValue(i))) |
| 576 | return 0; |
| 577 | |
| 578 | // Okay, we can do the transformation: create the new PHI node. |
| 579 | PHINode *NewPN = new PHINode(I.getType(), I.getName()); |
| 580 | I.setName(""); |
Chris Lattner | 5551706 | 2005-01-29 00:39:08 +0000 | [diff] [blame] | 581 | NewPN->reserveOperandSpace(PN->getNumOperands()/2); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 582 | InsertNewInstBefore(NewPN, *PN); |
| 583 | |
| 584 | // Next, add all of the operands to the PHI. |
| 585 | if (I.getNumOperands() == 2) { |
| 586 | Constant *C = cast<Constant>(I.getOperand(1)); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 587 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 588 | Constant *InV = cast<Constant>(PN->getIncomingValue(i)); |
| 589 | NewPN->addIncoming(ConstantExpr::get(I.getOpcode(), InV, C), |
| 590 | PN->getIncomingBlock(i)); |
| 591 | } |
| 592 | } else { |
| 593 | assert(isa<CastInst>(I) && "Unary op should be a cast!"); |
| 594 | const Type *RetTy = I.getType(); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 595 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 596 | Constant *InV = cast<Constant>(PN->getIncomingValue(i)); |
| 597 | NewPN->addIncoming(ConstantExpr::getCast(InV, RetTy), |
| 598 | PN->getIncomingBlock(i)); |
| 599 | } |
| 600 | } |
| 601 | return ReplaceInstUsesWith(I, NewPN); |
| 602 | } |
| 603 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 604 | Instruction *InstCombiner::visitAdd(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 605 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 606 | Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 607 | |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 608 | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 609 | // X + undef -> undef |
| 610 | if (isa<UndefValue>(RHS)) |
| 611 | return ReplaceInstUsesWith(I, RHS); |
| 612 | |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 613 | // X + 0 --> X |
| 614 | if (!I.getType()->isFloatingPoint() && // -0 + +0 = +0, so it's not a noop |
| 615 | RHSC->isNullValue()) |
| 616 | return ReplaceInstUsesWith(I, LHS); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 617 | |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 618 | // X + (signbit) --> X ^ signbit |
| 619 | if (ConstantInt *CI = dyn_cast<ConstantInt>(RHSC)) { |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 620 | unsigned NumBits = CI->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 621 | uint64_t Val = CI->getRawValue() & (1ULL << NumBits)-1; |
Chris Lattner | f158092 | 2004-11-05 04:45:43 +0000 | [diff] [blame] | 622 | if (Val == (1ULL << (NumBits-1))) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 623 | return BinaryOperator::createXor(LHS, RHS); |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 624 | } |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 625 | |
| 626 | if (isa<PHINode>(LHS)) |
| 627 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 628 | return NV; |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 629 | } |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 630 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 631 | // X + X --> X << 1 |
Robert Bocchino | 7169828 | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 632 | if (I.getType()->isInteger()) { |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 633 | if (Instruction *Result = AssociativeOpt(I, AddRHS(RHS))) return Result; |
Chris Lattner | 7edc8c2 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 634 | |
| 635 | if (Instruction *RHSI = dyn_cast<Instruction>(RHS)) { |
| 636 | if (RHSI->getOpcode() == Instruction::Sub) |
| 637 | if (LHS == RHSI->getOperand(1)) // A + (B - A) --> B |
| 638 | return ReplaceInstUsesWith(I, RHSI->getOperand(0)); |
| 639 | } |
| 640 | if (Instruction *LHSI = dyn_cast<Instruction>(LHS)) { |
| 641 | if (LHSI->getOpcode() == Instruction::Sub) |
| 642 | if (RHS == LHSI->getOperand(1)) // (B - A) + A --> B |
| 643 | return ReplaceInstUsesWith(I, LHSI->getOperand(0)); |
| 644 | } |
Robert Bocchino | 7169828 | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 645 | } |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 646 | |
Chris Lattner | 5c4afb9 | 2002-05-08 22:46:53 +0000 | [diff] [blame] | 647 | // -A + B --> B - A |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 648 | if (Value *V = dyn_castNegVal(LHS)) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 649 | return BinaryOperator::createSub(RHS, V); |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 650 | |
| 651 | // A + -B --> A - B |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 652 | if (!isa<Constant>(RHS)) |
| 653 | if (Value *V = dyn_castNegVal(RHS)) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 654 | return BinaryOperator::createSub(LHS, V); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 655 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 656 | |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 657 | ConstantInt *C2; |
| 658 | if (Value *X = dyn_castFoldableMul(LHS, C2)) { |
| 659 | if (X == RHS) // X*C + X --> X * (C+1) |
| 660 | return BinaryOperator::createMul(RHS, AddOne(C2)); |
| 661 | |
| 662 | // X*C1 + X*C2 --> X * (C1+C2) |
| 663 | ConstantInt *C1; |
| 664 | if (X == dyn_castFoldableMul(RHS, C1)) |
| 665 | return BinaryOperator::createMul(X, ConstantExpr::getAdd(C1, C2)); |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | // X + X*C --> X * (C+1) |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 669 | if (dyn_castFoldableMul(RHS, C2) == LHS) |
| 670 | return BinaryOperator::createMul(LHS, AddOne(C2)); |
| 671 | |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 672 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 673 | // (A & C1)+(B & C2) --> (A & C1)|(B & C2) iff C1&C2 == 0 |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 674 | if (match(RHS, m_And(m_Value(), m_ConstantInt(C2)))) |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 675 | if (Instruction *R = AssociativeOpt(I, AddMaskingAnd(C2))) return R; |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 676 | |
Chris Lattner | 6b03205 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 677 | if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) { |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 678 | Value *X; |
| 679 | if (match(LHS, m_Not(m_Value(X)))) { // ~X + C --> (C-1) - X |
| 680 | Constant *C= ConstantExpr::getSub(CRHS, ConstantInt::get(I.getType(), 1)); |
| 681 | return BinaryOperator::createSub(C, X); |
Chris Lattner | 6b03205 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 682 | } |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 683 | |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 684 | // (X & FF00) + xx00 -> (X+xx00) & FF00 |
| 685 | if (LHS->hasOneUse() && match(LHS, m_And(m_Value(X), m_ConstantInt(C2)))) { |
| 686 | Constant *Anded = ConstantExpr::getAnd(CRHS, C2); |
| 687 | if (Anded == CRHS) { |
| 688 | // See if all bits from the first bit set in the Add RHS up are included |
| 689 | // in the mask. First, get the rightmost bit. |
| 690 | uint64_t AddRHSV = CRHS->getRawValue(); |
| 691 | |
| 692 | // Form a mask of all bits from the lowest bit added through the top. |
| 693 | uint64_t AddRHSHighBits = ~((AddRHSV & -AddRHSV)-1); |
Chris Lattner | f52d681 | 2005-04-24 17:46:05 +0000 | [diff] [blame] | 694 | AddRHSHighBits &= ~0ULL >> (64-C2->getType()->getPrimitiveSizeInBits()); |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 695 | |
| 696 | // See if the and mask includes all of these bits. |
| 697 | uint64_t AddRHSHighBitsAnd = AddRHSHighBits & C2->getRawValue(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 698 | |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 699 | if (AddRHSHighBits == AddRHSHighBitsAnd) { |
| 700 | // Okay, the xform is safe. Insert the new add pronto. |
| 701 | Value *NewAdd = InsertNewInstBefore(BinaryOperator::createAdd(X, CRHS, |
| 702 | LHS->getName()), I); |
| 703 | return BinaryOperator::createAnd(NewAdd, C2); |
| 704 | } |
| 705 | } |
| 706 | } |
| 707 | |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 708 | // Try to fold constant add into select arguments. |
| 709 | if (SelectInst *SI = dyn_cast<SelectInst>(LHS)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 710 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 711 | return R; |
Chris Lattner | 6b03205 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 712 | } |
| 713 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 714 | return Changed ? &I : 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 715 | } |
| 716 | |
Chris Lattner | 1ba5bcd | 2003-07-22 21:46:59 +0000 | [diff] [blame] | 717 | // isSignBit - Return true if the value represented by the constant only has the |
| 718 | // highest order bit set. |
| 719 | static bool isSignBit(ConstantInt *CI) { |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 720 | unsigned NumBits = CI->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | f52d681 | 2005-04-24 17:46:05 +0000 | [diff] [blame] | 721 | return (CI->getRawValue() & (~0ULL >> (64-NumBits))) == (1ULL << (NumBits-1)); |
Chris Lattner | 1ba5bcd | 2003-07-22 21:46:59 +0000 | [diff] [blame] | 722 | } |
| 723 | |
Chris Lattner | bfe492b | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 724 | /// RemoveNoopCast - Strip off nonconverting casts from the value. |
| 725 | /// |
| 726 | static Value *RemoveNoopCast(Value *V) { |
| 727 | if (CastInst *CI = dyn_cast<CastInst>(V)) { |
| 728 | const Type *CTy = CI->getType(); |
| 729 | const Type *OpTy = CI->getOperand(0)->getType(); |
| 730 | if (CTy->isInteger() && OpTy->isInteger()) { |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 731 | if (CTy->getPrimitiveSizeInBits() == OpTy->getPrimitiveSizeInBits()) |
Chris Lattner | bfe492b | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 732 | return RemoveNoopCast(CI->getOperand(0)); |
| 733 | } else if (isa<PointerType>(CTy) && isa<PointerType>(OpTy)) |
| 734 | return RemoveNoopCast(CI->getOperand(0)); |
| 735 | } |
| 736 | return V; |
| 737 | } |
| 738 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 739 | Instruction *InstCombiner::visitSub(BinaryOperator &I) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 740 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 741 | |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 742 | if (Op0 == Op1) // sub X, X -> 0 |
| 743 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 744 | |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 745 | // If this is a 'B = x-(-A)', change to B = x+A... |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 746 | if (Value *V = dyn_castNegVal(Op1)) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 747 | return BinaryOperator::createAdd(Op0, V); |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 748 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 749 | if (isa<UndefValue>(Op0)) |
| 750 | return ReplaceInstUsesWith(I, Op0); // undef - X -> undef |
| 751 | if (isa<UndefValue>(Op1)) |
| 752 | return ReplaceInstUsesWith(I, Op1); // X - undef -> undef |
| 753 | |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 754 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op0)) { |
| 755 | // Replace (-1 - A) with (~A)... |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 756 | if (C->isAllOnesValue()) |
| 757 | return BinaryOperator::createNot(Op1); |
Chris Lattner | 4037171 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 758 | |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 759 | // C - ~X == X + (1+C) |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 760 | Value *X; |
| 761 | if (match(Op1, m_Not(m_Value(X)))) |
| 762 | return BinaryOperator::createAdd(X, |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 763 | ConstantExpr::getAdd(C, ConstantInt::get(I.getType(), 1))); |
Chris Lattner | 9c29067 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 764 | // -((uint)X >> 31) -> ((int)X >> 31) |
| 765 | // -((int)X >> 31) -> ((uint)X >> 31) |
Chris Lattner | bfe492b | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 766 | if (C->isNullValue()) { |
| 767 | Value *NoopCastedRHS = RemoveNoopCast(Op1); |
| 768 | if (ShiftInst *SI = dyn_cast<ShiftInst>(NoopCastedRHS)) |
Chris Lattner | 9c29067 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 769 | if (SI->getOpcode() == Instruction::Shr) |
| 770 | if (ConstantUInt *CU = dyn_cast<ConstantUInt>(SI->getOperand(1))) { |
| 771 | const Type *NewTy; |
Chris Lattner | bfe492b | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 772 | if (SI->getType()->isSigned()) |
Chris Lattner | 5dd0402 | 2004-06-17 18:16:02 +0000 | [diff] [blame] | 773 | NewTy = SI->getType()->getUnsignedVersion(); |
Chris Lattner | 9c29067 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 774 | else |
Chris Lattner | 5dd0402 | 2004-06-17 18:16:02 +0000 | [diff] [blame] | 775 | NewTy = SI->getType()->getSignedVersion(); |
Chris Lattner | 9c29067 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 776 | // Check to see if we are shifting out everything but the sign bit. |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 777 | if (CU->getValue() == SI->getType()->getPrimitiveSizeInBits()-1) { |
Chris Lattner | 9c29067 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 778 | // Ok, the transformation is safe. Insert a cast of the incoming |
| 779 | // value, then the new shift, then the new cast. |
| 780 | Instruction *FirstCast = new CastInst(SI->getOperand(0), NewTy, |
| 781 | SI->getOperand(0)->getName()); |
| 782 | Value *InV = InsertNewInstBefore(FirstCast, I); |
| 783 | Instruction *NewShift = new ShiftInst(Instruction::Shr, FirstCast, |
| 784 | CU, SI->getName()); |
Chris Lattner | bfe492b | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 785 | if (NewShift->getType() == I.getType()) |
| 786 | return NewShift; |
| 787 | else { |
| 788 | InV = InsertNewInstBefore(NewShift, I); |
| 789 | return new CastInst(NewShift, I.getType()); |
| 790 | } |
Chris Lattner | 9c29067 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 791 | } |
| 792 | } |
Chris Lattner | bfe492b | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 793 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 794 | |
| 795 | // Try to fold constant sub into select arguments. |
| 796 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 797 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 798 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 799 | |
| 800 | if (isa<PHINode>(Op0)) |
| 801 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 802 | return NV; |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 803 | } |
| 804 | |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 805 | if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) { |
| 806 | if (Op1I->getOpcode() == Instruction::Add && |
| 807 | !Op0->getType()->isFloatingPoint()) { |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 808 | if (Op1I->getOperand(0) == Op0) // X-(X+Y) == -Y |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 809 | return BinaryOperator::createNeg(Op1I->getOperand(1), I.getName()); |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 810 | else if (Op1I->getOperand(1) == Op0) // X-(Y+X) == -Y |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 811 | return BinaryOperator::createNeg(Op1I->getOperand(0), I.getName()); |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 812 | else if (ConstantInt *CI1 = dyn_cast<ConstantInt>(I.getOperand(0))) { |
| 813 | if (ConstantInt *CI2 = dyn_cast<ConstantInt>(Op1I->getOperand(1))) |
| 814 | // C1-(X+C2) --> (C1-C2)-X |
| 815 | return BinaryOperator::createSub(ConstantExpr::getSub(CI1, CI2), |
| 816 | Op1I->getOperand(0)); |
| 817 | } |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 818 | } |
| 819 | |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 820 | if (Op1I->hasOneUse()) { |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 821 | // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression |
| 822 | // is not used by anyone else... |
| 823 | // |
Chris Lattner | 0517e72 | 2004-02-02 20:09:56 +0000 | [diff] [blame] | 824 | if (Op1I->getOpcode() == Instruction::Sub && |
| 825 | !Op1I->getType()->isFloatingPoint()) { |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 826 | // Swap the two operands of the subexpr... |
| 827 | Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1); |
| 828 | Op1I->setOperand(0, IIOp1); |
| 829 | Op1I->setOperand(1, IIOp0); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 830 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 831 | // Create the new top level add instruction... |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 832 | return BinaryOperator::createAdd(Op0, Op1); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | // Replace (A - (A & B)) with (A & ~B) if this is the only use of (A&B)... |
| 836 | // |
| 837 | if (Op1I->getOpcode() == Instruction::And && |
| 838 | (Op1I->getOperand(0) == Op0 || Op1I->getOperand(1) == Op0)) { |
| 839 | Value *OtherOp = Op1I->getOperand(Op1I->getOperand(0) == Op0); |
| 840 | |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 841 | Value *NewNot = |
| 842 | InsertNewInstBefore(BinaryOperator::createNot(OtherOp, "B.not"), I); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 843 | return BinaryOperator::createAnd(Op0, NewNot); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 844 | } |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 845 | |
Chris Lattner | 91ccc15 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 846 | // -(X sdiv C) -> (X sdiv -C) |
| 847 | if (Op1I->getOpcode() == Instruction::Div) |
| 848 | if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(Op0)) |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 849 | if (CSI->isNullValue()) |
Chris Lattner | 91ccc15 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 850 | if (Constant *DivRHS = dyn_cast<Constant>(Op1I->getOperand(1))) |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 851 | return BinaryOperator::createDiv(Op1I->getOperand(0), |
Chris Lattner | 91ccc15 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 852 | ConstantExpr::getNeg(DivRHS)); |
| 853 | |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 854 | // X - X*C --> X * (1-C) |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 855 | ConstantInt *C2; |
| 856 | if (dyn_castFoldableMul(Op1I, C2) == Op0) { |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 857 | Constant *CP1 = |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 858 | ConstantExpr::getSub(ConstantInt::get(I.getType(), 1), C2); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 859 | return BinaryOperator::createMul(Op0, CP1); |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 860 | } |
Chris Lattner | 4037171 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 861 | } |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 862 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 863 | |
Chris Lattner | 7edc8c2 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 864 | if (!Op0->getType()->isFloatingPoint()) |
| 865 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) |
| 866 | if (Op0I->getOpcode() == Instruction::Add) { |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 867 | if (Op0I->getOperand(0) == Op1) // (Y+X)-Y == X |
| 868 | return ReplaceInstUsesWith(I, Op0I->getOperand(1)); |
| 869 | else if (Op0I->getOperand(1) == Op1) // (X+Y)-Y == X |
| 870 | return ReplaceInstUsesWith(I, Op0I->getOperand(0)); |
Chris Lattner | 7edc8c2 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 871 | } else if (Op0I->getOpcode() == Instruction::Sub) { |
| 872 | if (Op0I->getOperand(0) == Op1) // (X-Y)-X == -Y |
| 873 | return BinaryOperator::createNeg(Op0I->getOperand(1), I.getName()); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 874 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 875 | |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 876 | ConstantInt *C1; |
| 877 | if (Value *X = dyn_castFoldableMul(Op0, C1)) { |
| 878 | if (X == Op1) { // X*C - X --> X * (C-1) |
| 879 | Constant *CP1 = ConstantExpr::getSub(C1, ConstantInt::get(I.getType(),1)); |
| 880 | return BinaryOperator::createMul(Op1, CP1); |
| 881 | } |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 882 | |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 883 | ConstantInt *C2; // X*C1 - X*C2 -> X * (C1-C2) |
| 884 | if (X == dyn_castFoldableMul(Op1, C2)) |
| 885 | return BinaryOperator::createMul(Op1, ConstantExpr::getSub(C1, C2)); |
| 886 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 887 | return 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 888 | } |
| 889 | |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 890 | /// isSignBitCheck - Given an exploded setcc instruction, return true if it is |
| 891 | /// really just returns true if the most significant (sign) bit is set. |
| 892 | static bool isSignBitCheck(unsigned Opcode, Value *LHS, ConstantInt *RHS) { |
| 893 | if (RHS->getType()->isSigned()) { |
| 894 | // True if source is LHS < 0 or LHS <= -1 |
| 895 | return Opcode == Instruction::SetLT && RHS->isNullValue() || |
| 896 | Opcode == Instruction::SetLE && RHS->isAllOnesValue(); |
| 897 | } else { |
| 898 | ConstantUInt *RHSC = cast<ConstantUInt>(RHS); |
| 899 | // True if source is LHS > 127 or LHS >= 128, where the constants depend on |
| 900 | // the size of the integer type. |
| 901 | if (Opcode == Instruction::SetGE) |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 902 | return RHSC->getValue() == |
| 903 | 1ULL << (RHS->getType()->getPrimitiveSizeInBits()-1); |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 904 | if (Opcode == Instruction::SetGT) |
| 905 | return RHSC->getValue() == |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 906 | (1ULL << (RHS->getType()->getPrimitiveSizeInBits()-1))-1; |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 907 | } |
| 908 | return false; |
| 909 | } |
| 910 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 911 | Instruction *InstCombiner::visitMul(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 912 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 913 | Value *Op0 = I.getOperand(0); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 914 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 915 | if (isa<UndefValue>(I.getOperand(1))) // undef * X -> 0 |
| 916 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 917 | |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 918 | // Simplify mul instructions with a constant RHS... |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 919 | if (Constant *Op1 = dyn_cast<Constant>(I.getOperand(1))) { |
| 920 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 921 | |
| 922 | // ((X << C1)*C2) == (X * (C2 << C1)) |
| 923 | if (ShiftInst *SI = dyn_cast<ShiftInst>(Op0)) |
| 924 | if (SI->getOpcode() == Instruction::Shl) |
| 925 | if (Constant *ShOp = dyn_cast<Constant>(SI->getOperand(1))) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 926 | return BinaryOperator::createMul(SI->getOperand(0), |
| 927 | ConstantExpr::getShl(CI, ShOp)); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 928 | |
Chris Lattner | 515c97c | 2003-09-11 22:24:54 +0000 | [diff] [blame] | 929 | if (CI->isNullValue()) |
| 930 | return ReplaceInstUsesWith(I, Op1); // X * 0 == 0 |
| 931 | if (CI->equalsInt(1)) // X * 1 == X |
| 932 | return ReplaceInstUsesWith(I, Op0); |
| 933 | if (CI->isAllOnesValue()) // X * -1 == 0 - X |
Chris Lattner | 0af1fab | 2003-06-25 17:09:20 +0000 | [diff] [blame] | 934 | return BinaryOperator::createNeg(Op0, I.getName()); |
Chris Lattner | 6c1ce21 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 935 | |
Chris Lattner | 515c97c | 2003-09-11 22:24:54 +0000 | [diff] [blame] | 936 | int64_t Val = (int64_t)cast<ConstantInt>(CI)->getRawValue(); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 937 | if (uint64_t C = Log2(Val)) // Replace X*(2^C) with X << C |
| 938 | return new ShiftInst(Instruction::Shl, Op0, |
| 939 | ConstantUInt::get(Type::UByteTy, C)); |
Robert Bocchino | 7169828 | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 940 | } else if (ConstantFP *Op1F = dyn_cast<ConstantFP>(Op1)) { |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 941 | if (Op1F->isNullValue()) |
| 942 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 6c1ce21 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 943 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 944 | // "In IEEE floating point, x*1 is not equivalent to x for nans. However, |
| 945 | // ANSI says we can drop signals, so we can do this anyway." (from GCC) |
| 946 | if (Op1F->getValue() == 1.0) |
| 947 | return ReplaceInstUsesWith(I, Op0); // Eliminate 'mul double %X, 1.0' |
| 948 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 949 | |
| 950 | // Try to fold constant mul into select arguments. |
| 951 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 952 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 953 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 954 | |
| 955 | if (isa<PHINode>(Op0)) |
| 956 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 957 | return NV; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 958 | } |
| 959 | |
Chris Lattner | a4f445b | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 960 | if (Value *Op0v = dyn_castNegVal(Op0)) // -X * -Y = X*Y |
| 961 | if (Value *Op1v = dyn_castNegVal(I.getOperand(1))) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 962 | return BinaryOperator::createMul(Op0v, Op1v); |
Chris Lattner | a4f445b | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 963 | |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 964 | // If one of the operands of the multiply is a cast from a boolean value, then |
| 965 | // we know the bool is either zero or one, so this is a 'masking' multiply. |
| 966 | // See if we can simplify things based on how the boolean was originally |
| 967 | // formed. |
| 968 | CastInst *BoolCast = 0; |
| 969 | if (CastInst *CI = dyn_cast<CastInst>(I.getOperand(0))) |
| 970 | if (CI->getOperand(0)->getType() == Type::BoolTy) |
| 971 | BoolCast = CI; |
| 972 | if (!BoolCast) |
| 973 | if (CastInst *CI = dyn_cast<CastInst>(I.getOperand(1))) |
| 974 | if (CI->getOperand(0)->getType() == Type::BoolTy) |
| 975 | BoolCast = CI; |
| 976 | if (BoolCast) { |
| 977 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(BoolCast->getOperand(0))) { |
| 978 | Value *SCIOp0 = SCI->getOperand(0), *SCIOp1 = SCI->getOperand(1); |
| 979 | const Type *SCOpTy = SCIOp0->getType(); |
| 980 | |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 981 | // If the setcc is true iff the sign bit of X is set, then convert this |
| 982 | // multiply into a shift/and combination. |
| 983 | if (isa<ConstantInt>(SCIOp1) && |
| 984 | isSignBitCheck(SCI->getOpcode(), SCIOp0, cast<ConstantInt>(SCIOp1))) { |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 985 | // Shift the X value right to turn it into "all signbits". |
| 986 | Constant *Amt = ConstantUInt::get(Type::UByteTy, |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 987 | SCOpTy->getPrimitiveSizeInBits()-1); |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 988 | if (SCIOp0->getType()->isUnsigned()) { |
Chris Lattner | 5dd0402 | 2004-06-17 18:16:02 +0000 | [diff] [blame] | 989 | const Type *NewTy = SCIOp0->getType()->getSignedVersion(); |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 990 | SCIOp0 = InsertNewInstBefore(new CastInst(SCIOp0, NewTy, |
| 991 | SCIOp0->getName()), I); |
| 992 | } |
| 993 | |
| 994 | Value *V = |
| 995 | InsertNewInstBefore(new ShiftInst(Instruction::Shr, SCIOp0, Amt, |
| 996 | BoolCast->getOperand(0)->getName()+ |
| 997 | ".mask"), I); |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 998 | |
| 999 | // If the multiply type is not the same as the source type, sign extend |
| 1000 | // or truncate to the multiply type. |
| 1001 | if (I.getType() != V->getType()) |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 1002 | V = InsertNewInstBefore(new CastInst(V, I.getType(), V->getName()),I); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1003 | |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 1004 | Value *OtherOp = Op0 == BoolCast ? I.getOperand(1) : Op0; |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1005 | return BinaryOperator::createAnd(V, OtherOp); |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 1006 | } |
| 1007 | } |
| 1008 | } |
| 1009 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1010 | return Changed ? &I : 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1013 | Instruction *InstCombiner::visitDiv(BinaryOperator &I) { |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1014 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 1015 | |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1016 | if (isa<UndefValue>(Op0)) // undef / X -> 0 |
| 1017 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 1018 | if (isa<UndefValue>(Op1)) |
| 1019 | return ReplaceInstUsesWith(I, Op1); // X / undef -> undef |
| 1020 | |
| 1021 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 83a2e6e | 2004-04-26 14:01:59 +0000 | [diff] [blame] | 1022 | // div X, 1 == X |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1023 | if (RHS->equalsInt(1)) |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1024 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1025 | |
Chris Lattner | 83a2e6e | 2004-04-26 14:01:59 +0000 | [diff] [blame] | 1026 | // div X, -1 == -X |
| 1027 | if (RHS->isAllOnesValue()) |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1028 | return BinaryOperator::createNeg(Op0); |
Chris Lattner | 83a2e6e | 2004-04-26 14:01:59 +0000 | [diff] [blame] | 1029 | |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1030 | if (Instruction *LHS = dyn_cast<Instruction>(Op0)) |
Chris Lattner | 18d19ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 1031 | if (LHS->getOpcode() == Instruction::Div) |
| 1032 | if (ConstantInt *LHSRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) { |
Chris Lattner | 18d19ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 1033 | // (X / C1) / C2 -> X / (C1*C2) |
| 1034 | return BinaryOperator::createDiv(LHS->getOperand(0), |
| 1035 | ConstantExpr::getMul(RHS, LHSRHS)); |
| 1036 | } |
| 1037 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1038 | // Check to see if this is an unsigned division with an exact power of 2, |
| 1039 | // if so, convert to a right shift. |
| 1040 | if (ConstantUInt *C = dyn_cast<ConstantUInt>(RHS)) |
| 1041 | if (uint64_t Val = C->getValue()) // Don't break X / 0 |
| 1042 | if (uint64_t C = Log2(Val)) |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1043 | return new ShiftInst(Instruction::Shr, Op0, |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1044 | ConstantUInt::get(Type::UByteTy, C)); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1045 | |
Chris Lattner | a052f82 | 2004-10-09 02:50:40 +0000 | [diff] [blame] | 1046 | // -X/C -> X/-C |
| 1047 | if (RHS->getType()->isSigned()) |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1048 | if (Value *LHSNeg = dyn_castNegVal(Op0)) |
Chris Lattner | a052f82 | 2004-10-09 02:50:40 +0000 | [diff] [blame] | 1049 | return BinaryOperator::createDiv(LHSNeg, ConstantExpr::getNeg(RHS)); |
| 1050 | |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1051 | if (!RHS->isNullValue()) { |
| 1052 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1053 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1054 | return R; |
| 1055 | if (isa<PHINode>(Op0)) |
| 1056 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 1057 | return NV; |
| 1058 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1061 | // If this is 'udiv X, (Cond ? C1, C2)' where C1&C2 are powers of two, |
| 1062 | // transform this into: '(Cond ? (udiv X, C1) : (udiv X, C2))'. |
| 1063 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
| 1064 | if (ConstantUInt *STO = dyn_cast<ConstantUInt>(SI->getOperand(1))) |
| 1065 | if (ConstantUInt *SFO = dyn_cast<ConstantUInt>(SI->getOperand(2))) { |
| 1066 | if (STO->getValue() == 0) { // Couldn't be this argument. |
| 1067 | I.setOperand(1, SFO); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1068 | return &I; |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1069 | } else if (SFO->getValue() == 0) { |
Chris Lattner | f9c775c | 2005-06-16 04:55:52 +0000 | [diff] [blame] | 1070 | I.setOperand(1, STO); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1071 | return &I; |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
Chris Lattner | bf70b83 | 2005-04-08 04:03:26 +0000 | [diff] [blame] | 1074 | uint64_t TVA = STO->getValue(), FVA = SFO->getValue(); |
| 1075 | unsigned TSA = 0, FSA = 0; |
| 1076 | if ((TVA == 1 || (TSA = Log2(TVA))) && // Log2 fails for 0 & 1. |
| 1077 | (FVA == 1 || (FSA = Log2(FVA)))) { |
| 1078 | Constant *TC = ConstantUInt::get(Type::UByteTy, TSA); |
| 1079 | Instruction *TSI = new ShiftInst(Instruction::Shr, Op0, |
| 1080 | TC, SI->getName()+".t"); |
| 1081 | TSI = InsertNewInstBefore(TSI, I); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1082 | |
Chris Lattner | bf70b83 | 2005-04-08 04:03:26 +0000 | [diff] [blame] | 1083 | Constant *FC = ConstantUInt::get(Type::UByteTy, FSA); |
| 1084 | Instruction *FSI = new ShiftInst(Instruction::Shr, Op0, |
| 1085 | FC, SI->getName()+".f"); |
| 1086 | FSI = InsertNewInstBefore(FSI, I); |
| 1087 | return new SelectInst(SI->getOperand(0), TSI, FSI); |
| 1088 | } |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1089 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1090 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1091 | // 0 / X == 0, we don't need to preserve faults! |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1092 | if (ConstantInt *LHS = dyn_cast<ConstantInt>(Op0)) |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1093 | if (LHS->equalsInt(0)) |
| 1094 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 1095 | |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1096 | return 0; |
| 1097 | } |
| 1098 | |
| 1099 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1100 | Instruction *InstCombiner::visitRem(BinaryOperator &I) { |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1101 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 5b73c08 | 2004-07-06 07:01:22 +0000 | [diff] [blame] | 1102 | if (I.getType()->isSigned()) |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1103 | if (Value *RHSNeg = dyn_castNegVal(Op1)) |
Chris Lattner | 1e3564e | 2004-07-06 07:11:42 +0000 | [diff] [blame] | 1104 | if (!isa<ConstantSInt>(RHSNeg) || |
Chris Lattner | b49f306 | 2004-08-09 21:05:48 +0000 | [diff] [blame] | 1105 | cast<ConstantSInt>(RHSNeg)->getValue() > 0) { |
Chris Lattner | 5b73c08 | 2004-07-06 07:01:22 +0000 | [diff] [blame] | 1106 | // X % -Y -> X % Y |
| 1107 | AddUsesToWorkList(I); |
| 1108 | I.setOperand(1, RHSNeg); |
| 1109 | return &I; |
| 1110 | } |
| 1111 | |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1112 | if (isa<UndefValue>(Op0)) // undef % X -> 0 |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 1113 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1114 | if (isa<UndefValue>(Op1)) |
| 1115 | return ReplaceInstUsesWith(I, Op1); // X % undef -> undef |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 1116 | |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1117 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1118 | if (RHS->equalsInt(1)) // X % 1 == 0 |
| 1119 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 1120 | |
| 1121 | // Check to see if this is an unsigned remainder with an exact power of 2, |
| 1122 | // if so, convert to a bitwise and. |
| 1123 | if (ConstantUInt *C = dyn_cast<ConstantUInt>(RHS)) |
| 1124 | if (uint64_t Val = C->getValue()) // Don't break X % 0 (divide by zero) |
Chris Lattner | 546516c | 2004-05-07 15:35:56 +0000 | [diff] [blame] | 1125 | if (!(Val & (Val-1))) // Power of 2 |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1126 | return BinaryOperator::createAnd(Op0, |
| 1127 | ConstantUInt::get(I.getType(), Val-1)); |
| 1128 | |
| 1129 | if (!RHS->isNullValue()) { |
| 1130 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1131 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1132 | return R; |
| 1133 | if (isa<PHINode>(Op0)) |
| 1134 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 1135 | return NV; |
| 1136 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1137 | } |
| 1138 | |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1139 | // If this is 'urem X, (Cond ? C1, C2)' where C1&C2 are powers of two, |
| 1140 | // transform this into: '(Cond ? (urem X, C1) : (urem X, C2))'. |
| 1141 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
| 1142 | if (ConstantUInt *STO = dyn_cast<ConstantUInt>(SI->getOperand(1))) |
| 1143 | if (ConstantUInt *SFO = dyn_cast<ConstantUInt>(SI->getOperand(2))) { |
| 1144 | if (STO->getValue() == 0) { // Couldn't be this argument. |
| 1145 | I.setOperand(1, SFO); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1146 | return &I; |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1147 | } else if (SFO->getValue() == 0) { |
| 1148 | I.setOperand(1, STO); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1149 | return &I; |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | if (!(STO->getValue() & (STO->getValue()-1)) && |
| 1153 | !(SFO->getValue() & (SFO->getValue()-1))) { |
| 1154 | Value *TrueAnd = InsertNewInstBefore(BinaryOperator::createAnd(Op0, |
| 1155 | SubOne(STO), SI->getName()+".t"), I); |
| 1156 | Value *FalseAnd = InsertNewInstBefore(BinaryOperator::createAnd(Op0, |
| 1157 | SubOne(SFO), SI->getName()+".f"), I); |
| 1158 | return new SelectInst(SI->getOperand(0), TrueAnd, FalseAnd); |
| 1159 | } |
| 1160 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1161 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1162 | // 0 % X == 0, we don't need to preserve faults! |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 1163 | if (ConstantInt *LHS = dyn_cast<ConstantInt>(Op0)) |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1164 | if (LHS->equalsInt(0)) |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1165 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 1166 | |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1167 | return 0; |
| 1168 | } |
| 1169 | |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1170 | // isMaxValueMinusOne - return true if this is Max-1 |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1171 | static bool isMaxValueMinusOne(const ConstantInt *C) { |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1172 | if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(C)) { |
| 1173 | // Calculate -1 casted to the right type... |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 1174 | unsigned TypeBits = C->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1175 | uint64_t Val = ~0ULL; // All ones |
| 1176 | Val >>= 64-TypeBits; // Shift out unwanted 1 bits... |
| 1177 | return CU->getValue() == Val-1; |
| 1178 | } |
| 1179 | |
| 1180 | const ConstantSInt *CS = cast<ConstantSInt>(C); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1181 | |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1182 | // Calculate 0111111111..11111 |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 1183 | unsigned TypeBits = C->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1184 | int64_t Val = INT64_MAX; // All ones |
| 1185 | Val >>= 64-TypeBits; // Shift out unwanted 1 bits... |
| 1186 | return CS->getValue() == Val-1; |
| 1187 | } |
| 1188 | |
| 1189 | // isMinValuePlusOne - return true if this is Min+1 |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1190 | static bool isMinValuePlusOne(const ConstantInt *C) { |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1191 | if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(C)) |
| 1192 | return CU->getValue() == 1; |
| 1193 | |
| 1194 | const ConstantSInt *CS = cast<ConstantSInt>(C); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1195 | |
| 1196 | // Calculate 1111111111000000000000 |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 1197 | unsigned TypeBits = C->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1198 | int64_t Val = -1; // All ones |
| 1199 | Val <<= TypeBits-1; // Shift over to the right spot |
| 1200 | return CS->getValue() == Val+1; |
| 1201 | } |
| 1202 | |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 1203 | // isOneBitSet - Return true if there is exactly one bit set in the specified |
| 1204 | // constant. |
| 1205 | static bool isOneBitSet(const ConstantInt *CI) { |
| 1206 | uint64_t V = CI->getRawValue(); |
| 1207 | return V && (V & (V-1)) == 0; |
| 1208 | } |
| 1209 | |
Chris Lattner | b20ba0a | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 1210 | #if 0 // Currently unused |
| 1211 | // isLowOnes - Return true if the constant is of the form 0+1+. |
| 1212 | static bool isLowOnes(const ConstantInt *CI) { |
| 1213 | uint64_t V = CI->getRawValue(); |
| 1214 | |
| 1215 | // There won't be bits set in parts that the type doesn't contain. |
| 1216 | V &= ConstantInt::getAllOnesValue(CI->getType())->getRawValue(); |
| 1217 | |
| 1218 | uint64_t U = V+1; // If it is low ones, this should be a power of two. |
| 1219 | return U && V && (U & V) == 0; |
| 1220 | } |
| 1221 | #endif |
| 1222 | |
| 1223 | // isHighOnes - Return true if the constant is of the form 1+0+. |
| 1224 | // This is the same as lowones(~X). |
| 1225 | static bool isHighOnes(const ConstantInt *CI) { |
| 1226 | uint64_t V = ~CI->getRawValue(); |
| 1227 | |
| 1228 | // There won't be bits set in parts that the type doesn't contain. |
| 1229 | V &= ConstantInt::getAllOnesValue(CI->getType())->getRawValue(); |
| 1230 | |
| 1231 | uint64_t U = V+1; // If it is low ones, this should be a power of two. |
| 1232 | return U && V && (U & V) == 0; |
| 1233 | } |
| 1234 | |
| 1235 | |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 1236 | /// getSetCondCode - Encode a setcc opcode into a three bit mask. These bits |
| 1237 | /// are carefully arranged to allow folding of expressions such as: |
| 1238 | /// |
| 1239 | /// (A < B) | (A > B) --> (A != B) |
| 1240 | /// |
| 1241 | /// Bit value '4' represents that the comparison is true if A > B, bit value '2' |
| 1242 | /// represents that the comparison is true if A == B, and bit value '1' is true |
| 1243 | /// if A < B. |
| 1244 | /// |
| 1245 | static unsigned getSetCondCode(const SetCondInst *SCI) { |
| 1246 | switch (SCI->getOpcode()) { |
| 1247 | // False -> 0 |
| 1248 | case Instruction::SetGT: return 1; |
| 1249 | case Instruction::SetEQ: return 2; |
| 1250 | case Instruction::SetGE: return 3; |
| 1251 | case Instruction::SetLT: return 4; |
| 1252 | case Instruction::SetNE: return 5; |
| 1253 | case Instruction::SetLE: return 6; |
| 1254 | // True -> 7 |
| 1255 | default: |
| 1256 | assert(0 && "Invalid SetCC opcode!"); |
| 1257 | return 0; |
| 1258 | } |
| 1259 | } |
| 1260 | |
| 1261 | /// getSetCCValue - This is the complement of getSetCondCode, which turns an |
| 1262 | /// opcode and two operands into either a constant true or false, or a brand new |
| 1263 | /// SetCC instruction. |
| 1264 | static Value *getSetCCValue(unsigned Opcode, Value *LHS, Value *RHS) { |
| 1265 | switch (Opcode) { |
| 1266 | case 0: return ConstantBool::False; |
| 1267 | case 1: return new SetCondInst(Instruction::SetGT, LHS, RHS); |
| 1268 | case 2: return new SetCondInst(Instruction::SetEQ, LHS, RHS); |
| 1269 | case 3: return new SetCondInst(Instruction::SetGE, LHS, RHS); |
| 1270 | case 4: return new SetCondInst(Instruction::SetLT, LHS, RHS); |
| 1271 | case 5: return new SetCondInst(Instruction::SetNE, LHS, RHS); |
| 1272 | case 6: return new SetCondInst(Instruction::SetLE, LHS, RHS); |
| 1273 | case 7: return ConstantBool::True; |
| 1274 | default: assert(0 && "Illegal SetCCCode!"); return 0; |
| 1275 | } |
| 1276 | } |
| 1277 | |
| 1278 | // FoldSetCCLogical - Implements (setcc1 A, B) & (setcc2 A, B) --> (setcc3 A, B) |
| 1279 | struct FoldSetCCLogical { |
| 1280 | InstCombiner &IC; |
| 1281 | Value *LHS, *RHS; |
| 1282 | FoldSetCCLogical(InstCombiner &ic, SetCondInst *SCI) |
| 1283 | : IC(ic), LHS(SCI->getOperand(0)), RHS(SCI->getOperand(1)) {} |
| 1284 | bool shouldApply(Value *V) const { |
| 1285 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(V)) |
| 1286 | return (SCI->getOperand(0) == LHS && SCI->getOperand(1) == RHS || |
| 1287 | SCI->getOperand(0) == RHS && SCI->getOperand(1) == LHS); |
| 1288 | return false; |
| 1289 | } |
| 1290 | Instruction *apply(BinaryOperator &Log) const { |
| 1291 | SetCondInst *SCI = cast<SetCondInst>(Log.getOperand(0)); |
| 1292 | if (SCI->getOperand(0) != LHS) { |
| 1293 | assert(SCI->getOperand(1) == LHS); |
| 1294 | SCI->swapOperands(); // Swap the LHS and RHS of the SetCC |
| 1295 | } |
| 1296 | |
| 1297 | unsigned LHSCode = getSetCondCode(SCI); |
| 1298 | unsigned RHSCode = getSetCondCode(cast<SetCondInst>(Log.getOperand(1))); |
| 1299 | unsigned Code; |
| 1300 | switch (Log.getOpcode()) { |
| 1301 | case Instruction::And: Code = LHSCode & RHSCode; break; |
| 1302 | case Instruction::Or: Code = LHSCode | RHSCode; break; |
| 1303 | case Instruction::Xor: Code = LHSCode ^ RHSCode; break; |
Chris Lattner | 021c190 | 2003-09-22 20:33:34 +0000 | [diff] [blame] | 1304 | default: assert(0 && "Illegal logical opcode!"); return 0; |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 1305 | } |
| 1306 | |
| 1307 | Value *RV = getSetCCValue(Code, LHS, RHS); |
| 1308 | if (Instruction *I = dyn_cast<Instruction>(RV)) |
| 1309 | return I; |
| 1310 | // Otherwise, it's a constant boolean value... |
| 1311 | return IC.ReplaceInstUsesWith(Log, RV); |
| 1312 | } |
| 1313 | }; |
| 1314 | |
| 1315 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1316 | /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use |
| 1317 | /// this predicate to simplify operations downstream. V and Mask are known to |
| 1318 | /// be the same type. |
| 1319 | static bool MaskedValueIsZero(Value *V, ConstantIntegral *Mask) { |
| 1320 | if (isa<UndefValue>(V) || Mask->isNullValue()) |
| 1321 | return true; |
| 1322 | if (ConstantIntegral *CI = dyn_cast<ConstantIntegral>(V)) |
| 1323 | return ConstantExpr::getAnd(CI, Mask)->isNullValue(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1324 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1325 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
| 1326 | switch (I->getOpcode()) { |
| 1327 | case Instruction::And: |
| 1328 | // (X & C1) & C2 == 0 iff C1 & C2 == 0. |
| 1329 | if (ConstantIntegral *CI = dyn_cast<ConstantIntegral>(I->getOperand(1))) |
| 1330 | if (ConstantExpr::getAnd(CI, Mask)->isNullValue()) |
| 1331 | return true; |
| 1332 | break; |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 1333 | case Instruction::Or: |
| 1334 | // If the LHS and the RHS are MaskedValueIsZero, the result is also zero. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1335 | return MaskedValueIsZero(I->getOperand(1), Mask) && |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 1336 | MaskedValueIsZero(I->getOperand(0), Mask); |
| 1337 | case Instruction::Select: |
| 1338 | // If the T and F values are MaskedValueIsZero, the result is also zero. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1339 | return MaskedValueIsZero(I->getOperand(2), Mask) && |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 1340 | MaskedValueIsZero(I->getOperand(1), Mask); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1341 | case Instruction::Cast: { |
| 1342 | const Type *SrcTy = I->getOperand(0)->getType(); |
Chris Lattner | d152380 | 2005-05-06 01:53:19 +0000 | [diff] [blame] | 1343 | if (SrcTy == Type::BoolTy) |
| 1344 | return (Mask->getRawValue() & 1) == 0; |
| 1345 | |
| 1346 | if (SrcTy->isInteger()) { |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1347 | // (cast <ty> X to int) & C2 == 0 iff <ty> could not have contained C2. |
| 1348 | if (SrcTy->isUnsigned() && // Only handle zero ext. |
| 1349 | ConstantExpr::getCast(Mask, SrcTy)->isNullValue()) |
| 1350 | return true; |
| 1351 | |
| 1352 | // If this is a noop cast, recurse. |
Chris Lattner | d152380 | 2005-05-06 01:53:19 +0000 | [diff] [blame] | 1353 | if ((SrcTy->isSigned() && SrcTy->getUnsignedVersion() == I->getType())|| |
| 1354 | SrcTy->getSignedVersion() == I->getType()) { |
| 1355 | Constant *NewMask = |
| 1356 | ConstantExpr::getCast(Mask, I->getOperand(0)->getType()); |
| 1357 | return MaskedValueIsZero(I->getOperand(0), |
| 1358 | cast<ConstantIntegral>(NewMask)); |
| 1359 | } |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1360 | } |
| 1361 | break; |
| 1362 | } |
| 1363 | case Instruction::Shl: |
Chris Lattner | eabfa47 | 2005-05-06 04:53:20 +0000 | [diff] [blame] | 1364 | // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0 |
| 1365 | if (ConstantUInt *SA = dyn_cast<ConstantUInt>(I->getOperand(1))) |
| 1366 | return MaskedValueIsZero(I->getOperand(0), |
| 1367 | cast<ConstantIntegral>(ConstantExpr::getUShr(Mask, SA))); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1368 | break; |
| 1369 | case Instruction::Shr: |
| 1370 | // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0 |
| 1371 | if (ConstantUInt *SA = dyn_cast<ConstantUInt>(I->getOperand(1))) |
| 1372 | if (I->getType()->isUnsigned()) { |
| 1373 | Constant *C1 = ConstantIntegral::getAllOnesValue(I->getType()); |
| 1374 | C1 = ConstantExpr::getShr(C1, SA); |
| 1375 | C1 = ConstantExpr::getAnd(C1, Mask); |
| 1376 | if (C1->isNullValue()) |
| 1377 | return true; |
| 1378 | } |
| 1379 | break; |
| 1380 | } |
| 1381 | } |
| 1382 | |
| 1383 | return false; |
| 1384 | } |
| 1385 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1386 | // OptAndOp - This handles expressions of the form ((val OP C1) & C2). Where |
| 1387 | // the Op parameter is 'OP', OpRHS is 'C1', and AndRHS is 'C2'. Op is |
| 1388 | // guaranteed to be either a shift instruction or a binary operator. |
| 1389 | Instruction *InstCombiner::OptAndOp(Instruction *Op, |
| 1390 | ConstantIntegral *OpRHS, |
| 1391 | ConstantIntegral *AndRHS, |
| 1392 | BinaryOperator &TheAnd) { |
| 1393 | Value *X = Op->getOperand(0); |
Chris Lattner | 76f7fe2 | 2004-01-12 19:47:05 +0000 | [diff] [blame] | 1394 | Constant *Together = 0; |
| 1395 | if (!isa<ShiftInst>(Op)) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1396 | Together = ConstantExpr::getAnd(AndRHS, OpRHS); |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1397 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1398 | switch (Op->getOpcode()) { |
| 1399 | case Instruction::Xor: |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1400 | if (Op->hasOneUse()) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1401 | // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2) |
| 1402 | std::string OpName = Op->getName(); Op->setName(""); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1403 | Instruction *And = BinaryOperator::createAnd(X, AndRHS, OpName); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1404 | InsertNewInstBefore(And, TheAnd); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1405 | return BinaryOperator::createXor(And, Together); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1406 | } |
| 1407 | break; |
| 1408 | case Instruction::Or: |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1409 | if (Together == AndRHS) // (X | C) & C --> C |
| 1410 | return ReplaceInstUsesWith(TheAnd, AndRHS); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1411 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1412 | if (Op->hasOneUse() && Together != OpRHS) { |
| 1413 | // (X | C1) & C2 --> (X | (C1&C2)) & C2 |
| 1414 | std::string Op0Name = Op->getName(); Op->setName(""); |
| 1415 | Instruction *Or = BinaryOperator::createOr(X, Together, Op0Name); |
| 1416 | InsertNewInstBefore(Or, TheAnd); |
| 1417 | return BinaryOperator::createAnd(Or, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1418 | } |
| 1419 | break; |
| 1420 | case Instruction::Add: |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 1421 | if (Op->hasOneUse()) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1422 | // Adding a one to a single bit bit-field should be turned into an XOR |
| 1423 | // of the bit. First thing to check is to see if this AND is with a |
| 1424 | // single bit constant. |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 1425 | uint64_t AndRHSV = cast<ConstantInt>(AndRHS)->getRawValue(); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1426 | |
| 1427 | // Clear bits that are not part of the constant. |
Chris Lattner | f52d681 | 2005-04-24 17:46:05 +0000 | [diff] [blame] | 1428 | AndRHSV &= ~0ULL >> (64-AndRHS->getType()->getPrimitiveSizeInBits()); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1429 | |
| 1430 | // If there is only one bit set... |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 1431 | if (isOneBitSet(cast<ConstantInt>(AndRHS))) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1432 | // Ok, at this point, we know that we are masking the result of the |
| 1433 | // ADD down to exactly one bit. If the constant we are adding has |
| 1434 | // no bits set below this bit, then we can eliminate the ADD. |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 1435 | uint64_t AddRHS = cast<ConstantInt>(OpRHS)->getRawValue(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1436 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1437 | // Check to see if any bits below the one bit set in AndRHSV are set. |
| 1438 | if ((AddRHS & (AndRHSV-1)) == 0) { |
| 1439 | // If not, the only thing that can effect the output of the AND is |
| 1440 | // the bit specified by AndRHSV. If that bit is set, the effect of |
| 1441 | // the XOR is to toggle the bit. If it is clear, then the ADD has |
| 1442 | // no effect. |
| 1443 | if ((AddRHS & AndRHSV) == 0) { // Bit is not set, noop |
| 1444 | TheAnd.setOperand(0, X); |
| 1445 | return &TheAnd; |
| 1446 | } else { |
| 1447 | std::string Name = Op->getName(); Op->setName(""); |
| 1448 | // Pull the XOR out of the AND. |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1449 | Instruction *NewAnd = BinaryOperator::createAnd(X, AndRHS, Name); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1450 | InsertNewInstBefore(NewAnd, TheAnd); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1451 | return BinaryOperator::createXor(NewAnd, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1452 | } |
| 1453 | } |
| 1454 | } |
| 1455 | } |
| 1456 | break; |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 1457 | |
| 1458 | case Instruction::Shl: { |
| 1459 | // We know that the AND will not produce any of the bits shifted in, so if |
| 1460 | // the anded constant includes them, clear them now! |
| 1461 | // |
| 1462 | Constant *AllOne = ConstantIntegral::getAllOnesValue(AndRHS->getType()); |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 1463 | Constant *ShlMask = ConstantExpr::getShl(AllOne, OpRHS); |
| 1464 | Constant *CI = ConstantExpr::getAnd(AndRHS, ShlMask); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1465 | |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 1466 | if (CI == ShlMask) { // Masking out bits that the shift already masks |
| 1467 | return ReplaceInstUsesWith(TheAnd, Op); // No need for the and. |
| 1468 | } else if (CI != AndRHS) { // Reducing bits set in and. |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 1469 | TheAnd.setOperand(1, CI); |
| 1470 | return &TheAnd; |
| 1471 | } |
| 1472 | break; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1473 | } |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 1474 | case Instruction::Shr: |
| 1475 | // We know that the AND will not produce any of the bits shifted in, so if |
| 1476 | // the anded constant includes them, clear them now! This only applies to |
| 1477 | // unsigned shifts, because a signed shr may bring in set bits! |
| 1478 | // |
| 1479 | if (AndRHS->getType()->isUnsigned()) { |
| 1480 | Constant *AllOne = ConstantIntegral::getAllOnesValue(AndRHS->getType()); |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 1481 | Constant *ShrMask = ConstantExpr::getShr(AllOne, OpRHS); |
| 1482 | Constant *CI = ConstantExpr::getAnd(AndRHS, ShrMask); |
| 1483 | |
| 1484 | if (CI == ShrMask) { // Masking out bits that the shift already masks. |
| 1485 | return ReplaceInstUsesWith(TheAnd, Op); |
| 1486 | } else if (CI != AndRHS) { |
| 1487 | TheAnd.setOperand(1, CI); // Reduce bits set in and cst. |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 1488 | return &TheAnd; |
| 1489 | } |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 1490 | } else { // Signed shr. |
| 1491 | // See if this is shifting in some sign extension, then masking it out |
| 1492 | // with an and. |
| 1493 | if (Op->hasOneUse()) { |
| 1494 | Constant *AllOne = ConstantIntegral::getAllOnesValue(AndRHS->getType()); |
| 1495 | Constant *ShrMask = ConstantExpr::getUShr(AllOne, OpRHS); |
| 1496 | Constant *CI = ConstantExpr::getAnd(AndRHS, ShrMask); |
Chris Lattner | 9b99182 | 2004-10-22 04:53:16 +0000 | [diff] [blame] | 1497 | if (CI == AndRHS) { // Masking out bits shifted in. |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 1498 | // Make the argument unsigned. |
| 1499 | Value *ShVal = Op->getOperand(0); |
| 1500 | ShVal = InsertCastBefore(ShVal, |
| 1501 | ShVal->getType()->getUnsignedVersion(), |
| 1502 | TheAnd); |
| 1503 | ShVal = InsertNewInstBefore(new ShiftInst(Instruction::Shr, ShVal, |
| 1504 | OpRHS, Op->getName()), |
| 1505 | TheAnd); |
Chris Lattner | dc78122 | 2004-10-27 05:57:15 +0000 | [diff] [blame] | 1506 | Value *AndRHS2 = ConstantExpr::getCast(AndRHS, ShVal->getType()); |
| 1507 | ShVal = InsertNewInstBefore(BinaryOperator::createAnd(ShVal, AndRHS2, |
| 1508 | TheAnd.getName()), |
| 1509 | TheAnd); |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 1510 | return new CastInst(ShVal, Op->getType()); |
| 1511 | } |
| 1512 | } |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 1513 | } |
| 1514 | break; |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1515 | } |
| 1516 | return 0; |
| 1517 | } |
| 1518 | |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1519 | |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 1520 | /// InsertRangeTest - Emit a computation of: (V >= Lo && V < Hi) if Inside is |
| 1521 | /// true, otherwise (V < Lo || V >= Hi). In pratice, we emit the more efficient |
| 1522 | /// (V-Lo) <u Hi-Lo. This method expects that Lo <= Hi. IB is the location to |
| 1523 | /// insert new instructions. |
| 1524 | Instruction *InstCombiner::InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, |
| 1525 | bool Inside, Instruction &IB) { |
| 1526 | assert(cast<ConstantBool>(ConstantExpr::getSetLE(Lo, Hi))->getValue() && |
| 1527 | "Lo is not <= Hi in range emission code!"); |
| 1528 | if (Inside) { |
| 1529 | if (Lo == Hi) // Trivially false. |
| 1530 | return new SetCondInst(Instruction::SetNE, V, V); |
| 1531 | if (cast<ConstantIntegral>(Lo)->isMinValue()) |
| 1532 | return new SetCondInst(Instruction::SetLT, V, Hi); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1533 | |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 1534 | Constant *AddCST = ConstantExpr::getNeg(Lo); |
| 1535 | Instruction *Add = BinaryOperator::createAdd(V, AddCST,V->getName()+".off"); |
| 1536 | InsertNewInstBefore(Add, IB); |
| 1537 | // Convert to unsigned for the comparison. |
| 1538 | const Type *UnsType = Add->getType()->getUnsignedVersion(); |
| 1539 | Value *OffsetVal = InsertCastBefore(Add, UnsType, IB); |
| 1540 | AddCST = ConstantExpr::getAdd(AddCST, Hi); |
| 1541 | AddCST = ConstantExpr::getCast(AddCST, UnsType); |
| 1542 | return new SetCondInst(Instruction::SetLT, OffsetVal, AddCST); |
| 1543 | } |
| 1544 | |
| 1545 | if (Lo == Hi) // Trivially true. |
| 1546 | return new SetCondInst(Instruction::SetEQ, V, V); |
| 1547 | |
| 1548 | Hi = SubOne(cast<ConstantInt>(Hi)); |
| 1549 | if (cast<ConstantIntegral>(Lo)->isMinValue()) // V < 0 || V >= Hi ->'V > Hi-1' |
| 1550 | return new SetCondInst(Instruction::SetGT, V, Hi); |
| 1551 | |
| 1552 | // Emit X-Lo > Hi-Lo-1 |
| 1553 | Constant *AddCST = ConstantExpr::getNeg(Lo); |
| 1554 | Instruction *Add = BinaryOperator::createAdd(V, AddCST, V->getName()+".off"); |
| 1555 | InsertNewInstBefore(Add, IB); |
| 1556 | // Convert to unsigned for the comparison. |
| 1557 | const Type *UnsType = Add->getType()->getUnsignedVersion(); |
| 1558 | Value *OffsetVal = InsertCastBefore(Add, UnsType, IB); |
| 1559 | AddCST = ConstantExpr::getAdd(AddCST, Hi); |
| 1560 | AddCST = ConstantExpr::getCast(AddCST, UnsType); |
| 1561 | return new SetCondInst(Instruction::SetGT, OffsetVal, AddCST); |
| 1562 | } |
| 1563 | |
| 1564 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1565 | Instruction *InstCombiner::visitAnd(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 1566 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1567 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1568 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 1569 | if (isa<UndefValue>(Op1)) // X & undef -> 0 |
| 1570 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 1571 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1572 | // and X, X = X |
| 1573 | if (Op0 == Op1) |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1574 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1575 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1576 | if (ConstantIntegral *AndRHS = dyn_cast<ConstantIntegral>(Op1)) { |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 1577 | // and X, -1 == X |
| 1578 | if (AndRHS->isAllOnesValue()) |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1579 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1580 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1581 | if (MaskedValueIsZero(Op0, AndRHS)) // LHS & RHS == 0 |
| 1582 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 1583 | |
| 1584 | // If the mask is not masking out any bits, there is no reason to do the |
| 1585 | // and in the first place. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1586 | ConstantIntegral *NotAndRHS = |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 1587 | cast<ConstantIntegral>(ConstantExpr::getNot(AndRHS)); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1588 | if (MaskedValueIsZero(Op0, NotAndRHS)) |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 1589 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1590 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1591 | // Optimize a variety of ((val OP C1) & C2) combinations... |
| 1592 | if (isa<BinaryOperator>(Op0) || isa<ShiftInst>(Op0)) { |
| 1593 | Instruction *Op0I = cast<Instruction>(Op0); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1594 | Value *Op0LHS = Op0I->getOperand(0); |
| 1595 | Value *Op0RHS = Op0I->getOperand(1); |
| 1596 | switch (Op0I->getOpcode()) { |
| 1597 | case Instruction::Xor: |
| 1598 | case Instruction::Or: |
| 1599 | // (X ^ V) & C2 --> (X & C2) iff (V & C2) == 0 |
| 1600 | // (X | V) & C2 --> (X & C2) iff (V & C2) == 0 |
| 1601 | if (MaskedValueIsZero(Op0LHS, AndRHS)) |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1602 | return BinaryOperator::createAnd(Op0RHS, AndRHS); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1603 | if (MaskedValueIsZero(Op0RHS, AndRHS)) |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1604 | return BinaryOperator::createAnd(Op0LHS, AndRHS); |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 1605 | |
| 1606 | // If the mask is only needed on one incoming arm, push it up. |
| 1607 | if (Op0I->hasOneUse()) { |
| 1608 | if (MaskedValueIsZero(Op0LHS, NotAndRHS)) { |
| 1609 | // Not masking anything out for the LHS, move to RHS. |
| 1610 | Instruction *NewRHS = BinaryOperator::createAnd(Op0RHS, AndRHS, |
| 1611 | Op0RHS->getName()+".masked"); |
| 1612 | InsertNewInstBefore(NewRHS, I); |
| 1613 | return BinaryOperator::create( |
| 1614 | cast<BinaryOperator>(Op0I)->getOpcode(), Op0LHS, NewRHS); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1615 | } |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 1616 | if (!isa<Constant>(NotAndRHS) && |
| 1617 | MaskedValueIsZero(Op0RHS, NotAndRHS)) { |
| 1618 | // Not masking anything out for the RHS, move to LHS. |
| 1619 | Instruction *NewLHS = BinaryOperator::createAnd(Op0LHS, AndRHS, |
| 1620 | Op0LHS->getName()+".masked"); |
| 1621 | InsertNewInstBefore(NewLHS, I); |
| 1622 | return BinaryOperator::create( |
| 1623 | cast<BinaryOperator>(Op0I)->getOpcode(), NewLHS, Op0RHS); |
| 1624 | } |
| 1625 | } |
| 1626 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1627 | break; |
| 1628 | case Instruction::And: |
| 1629 | // (X & V) & C2 --> 0 iff (V & C2) == 0 |
| 1630 | if (MaskedValueIsZero(Op0LHS, AndRHS) || |
| 1631 | MaskedValueIsZero(Op0RHS, AndRHS)) |
| 1632 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 1633 | break; |
| 1634 | } |
| 1635 | |
Chris Lattner | 5840326 | 2003-07-23 19:25:52 +0000 | [diff] [blame] | 1636 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1637 | if (Instruction *Res = OptAndOp(Op0I, Op0CI, AndRHS, I)) |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1638 | return Res; |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1639 | } else if (CastInst *CI = dyn_cast<CastInst>(Op0)) { |
| 1640 | const Type *SrcTy = CI->getOperand(0)->getType(); |
| 1641 | |
| 1642 | // If this is an integer sign or zero extension instruction. |
| 1643 | if (SrcTy->isIntegral() && |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 1644 | SrcTy->getPrimitiveSizeInBits() < |
| 1645 | CI->getType()->getPrimitiveSizeInBits()) { |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1646 | |
| 1647 | if (SrcTy->isUnsigned()) { |
| 1648 | // See if this and is clearing out bits that are known to be zero |
| 1649 | // anyway (due to the zero extension). |
| 1650 | Constant *Mask = ConstantIntegral::getAllOnesValue(SrcTy); |
| 1651 | Mask = ConstantExpr::getZeroExtend(Mask, CI->getType()); |
| 1652 | Constant *Result = ConstantExpr::getAnd(Mask, AndRHS); |
| 1653 | if (Result == Mask) // The "and" isn't doing anything, remove it. |
| 1654 | return ReplaceInstUsesWith(I, CI); |
| 1655 | if (Result != AndRHS) { // Reduce the and RHS constant. |
| 1656 | I.setOperand(1, Result); |
| 1657 | return &I; |
| 1658 | } |
| 1659 | |
| 1660 | } else { |
| 1661 | if (CI->hasOneUse() && SrcTy->isInteger()) { |
| 1662 | // We can only do this if all of the sign bits brought in are masked |
| 1663 | // out. Compute this by first getting 0000011111, then inverting |
| 1664 | // it. |
| 1665 | Constant *Mask = ConstantIntegral::getAllOnesValue(SrcTy); |
| 1666 | Mask = ConstantExpr::getZeroExtend(Mask, CI->getType()); |
| 1667 | Mask = ConstantExpr::getNot(Mask); // 1's in the new bits. |
| 1668 | if (ConstantExpr::getAnd(Mask, AndRHS)->isNullValue()) { |
| 1669 | // If the and is clearing all of the sign bits, change this to a |
| 1670 | // zero extension cast. To do this, cast the cast input to |
| 1671 | // unsigned, then to the requested size. |
| 1672 | Value *CastOp = CI->getOperand(0); |
| 1673 | Instruction *NC = |
| 1674 | new CastInst(CastOp, CastOp->getType()->getUnsignedVersion(), |
| 1675 | CI->getName()+".uns"); |
| 1676 | NC = InsertNewInstBefore(NC, I); |
| 1677 | // Finally, insert a replacement for CI. |
| 1678 | NC = new CastInst(NC, CI->getType(), CI->getName()); |
| 1679 | CI->setName(""); |
| 1680 | NC = InsertNewInstBefore(NC, I); |
| 1681 | WorkList.push_back(CI); // Delete CI later. |
| 1682 | I.setOperand(0, NC); |
| 1683 | return &I; // The AND operand was modified. |
| 1684 | } |
| 1685 | } |
| 1686 | } |
| 1687 | } |
Chris Lattner | 06782f8 | 2003-07-23 19:36:21 +0000 | [diff] [blame] | 1688 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1689 | |
| 1690 | // Try to fold constant and into select arguments. |
| 1691 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1692 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1693 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1694 | if (isa<PHINode>(Op0)) |
| 1695 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 1696 | return NV; |
Chris Lattner | c6a8aff | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 1697 | } |
| 1698 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 1699 | Value *Op0NotVal = dyn_castNotVal(Op0); |
| 1700 | Value *Op1NotVal = dyn_castNotVal(Op1); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1701 | |
Chris Lattner | 5b62aa7 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 1702 | if (Op0NotVal == Op1 || Op1NotVal == Op0) // A & ~A == ~A & A == 0 |
| 1703 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 1704 | |
Misha Brukman | cb6267b | 2004-07-30 12:50:08 +0000 | [diff] [blame] | 1705 | // (~A & ~B) == (~(A | B)) - De Morgan's Law |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 1706 | if (Op0NotVal && Op1NotVal && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1707 | Instruction *Or = BinaryOperator::createOr(Op0NotVal, Op1NotVal, |
| 1708 | I.getName()+".demorgan"); |
Chris Lattner | c6a8aff | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 1709 | InsertNewInstBefore(Or, I); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1710 | return BinaryOperator::createNot(Or); |
| 1711 | } |
| 1712 | |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 1713 | if (SetCondInst *RHS = dyn_cast<SetCondInst>(Op1)) { |
| 1714 | // (setcc1 A, B) & (setcc2 A, B) --> (setcc3 A, B) |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 1715 | if (Instruction *R = AssociativeOpt(I, FoldSetCCLogical(*this, RHS))) |
| 1716 | return R; |
| 1717 | |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 1718 | Value *LHSVal, *RHSVal; |
| 1719 | ConstantInt *LHSCst, *RHSCst; |
| 1720 | Instruction::BinaryOps LHSCC, RHSCC; |
| 1721 | if (match(Op0, m_SetCond(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst)))) |
| 1722 | if (match(RHS, m_SetCond(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst)))) |
| 1723 | if (LHSVal == RHSVal && // Found (X setcc C1) & (X setcc C2) |
| 1724 | // Set[GL]E X, CST is folded to Set[GL]T elsewhere. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1725 | LHSCC != Instruction::SetGE && LHSCC != Instruction::SetLE && |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 1726 | RHSCC != Instruction::SetGE && RHSCC != Instruction::SetLE) { |
| 1727 | // Ensure that the larger constant is on the RHS. |
| 1728 | Constant *Cmp = ConstantExpr::getSetGT(LHSCst, RHSCst); |
| 1729 | SetCondInst *LHS = cast<SetCondInst>(Op0); |
| 1730 | if (cast<ConstantBool>(Cmp)->getValue()) { |
| 1731 | std::swap(LHS, RHS); |
| 1732 | std::swap(LHSCst, RHSCst); |
| 1733 | std::swap(LHSCC, RHSCC); |
| 1734 | } |
| 1735 | |
| 1736 | // At this point, we know we have have two setcc instructions |
| 1737 | // comparing a value against two constants and and'ing the result |
| 1738 | // together. Because of the above check, we know that we only have |
| 1739 | // SetEQ, SetNE, SetLT, and SetGT here. We also know (from the |
| 1740 | // FoldSetCCLogical check above), that the two constants are not |
| 1741 | // equal. |
| 1742 | assert(LHSCst != RHSCst && "Compares not folded above?"); |
| 1743 | |
| 1744 | switch (LHSCC) { |
| 1745 | default: assert(0 && "Unknown integer condition code!"); |
| 1746 | case Instruction::SetEQ: |
| 1747 | switch (RHSCC) { |
| 1748 | default: assert(0 && "Unknown integer condition code!"); |
| 1749 | case Instruction::SetEQ: // (X == 13 & X == 15) -> false |
| 1750 | case Instruction::SetGT: // (X == 13 & X > 15) -> false |
| 1751 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 1752 | case Instruction::SetNE: // (X == 13 & X != 15) -> X == 13 |
| 1753 | case Instruction::SetLT: // (X == 13 & X < 15) -> X == 13 |
| 1754 | return ReplaceInstUsesWith(I, LHS); |
| 1755 | } |
| 1756 | case Instruction::SetNE: |
| 1757 | switch (RHSCC) { |
| 1758 | default: assert(0 && "Unknown integer condition code!"); |
| 1759 | case Instruction::SetLT: |
| 1760 | if (LHSCst == SubOne(RHSCst)) // (X != 13 & X < 14) -> X < 13 |
| 1761 | return new SetCondInst(Instruction::SetLT, LHSVal, LHSCst); |
| 1762 | break; // (X != 13 & X < 15) -> no change |
| 1763 | case Instruction::SetEQ: // (X != 13 & X == 15) -> X == 15 |
| 1764 | case Instruction::SetGT: // (X != 13 & X > 15) -> X > 15 |
| 1765 | return ReplaceInstUsesWith(I, RHS); |
| 1766 | case Instruction::SetNE: |
| 1767 | if (LHSCst == SubOne(RHSCst)) {// (X != 13 & X != 14) -> X-13 >u 1 |
| 1768 | Constant *AddCST = ConstantExpr::getNeg(LHSCst); |
| 1769 | Instruction *Add = BinaryOperator::createAdd(LHSVal, AddCST, |
| 1770 | LHSVal->getName()+".off"); |
| 1771 | InsertNewInstBefore(Add, I); |
| 1772 | const Type *UnsType = Add->getType()->getUnsignedVersion(); |
| 1773 | Value *OffsetVal = InsertCastBefore(Add, UnsType, I); |
| 1774 | AddCST = ConstantExpr::getSub(RHSCst, LHSCst); |
| 1775 | AddCST = ConstantExpr::getCast(AddCST, UnsType); |
| 1776 | return new SetCondInst(Instruction::SetGT, OffsetVal, AddCST); |
| 1777 | } |
| 1778 | break; // (X != 13 & X != 15) -> no change |
| 1779 | } |
| 1780 | break; |
| 1781 | case Instruction::SetLT: |
| 1782 | switch (RHSCC) { |
| 1783 | default: assert(0 && "Unknown integer condition code!"); |
| 1784 | case Instruction::SetEQ: // (X < 13 & X == 15) -> false |
| 1785 | case Instruction::SetGT: // (X < 13 & X > 15) -> false |
| 1786 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 1787 | case Instruction::SetNE: // (X < 13 & X != 15) -> X < 13 |
| 1788 | case Instruction::SetLT: // (X < 13 & X < 15) -> X < 13 |
| 1789 | return ReplaceInstUsesWith(I, LHS); |
| 1790 | } |
| 1791 | case Instruction::SetGT: |
| 1792 | switch (RHSCC) { |
| 1793 | default: assert(0 && "Unknown integer condition code!"); |
| 1794 | case Instruction::SetEQ: // (X > 13 & X == 15) -> X > 13 |
| 1795 | return ReplaceInstUsesWith(I, LHS); |
| 1796 | case Instruction::SetGT: // (X > 13 & X > 15) -> X > 15 |
| 1797 | return ReplaceInstUsesWith(I, RHS); |
| 1798 | case Instruction::SetNE: |
| 1799 | if (RHSCst == AddOne(LHSCst)) // (X > 13 & X != 14) -> X > 14 |
| 1800 | return new SetCondInst(Instruction::SetGT, LHSVal, RHSCst); |
| 1801 | break; // (X > 13 & X != 15) -> no change |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 1802 | case Instruction::SetLT: // (X > 13 & X < 15) -> (X-14) <u 1 |
| 1803 | return InsertRangeTest(LHSVal, AddOne(LHSCst), RHSCst, true, I); |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 1804 | } |
| 1805 | } |
| 1806 | } |
| 1807 | } |
| 1808 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1809 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1810 | } |
| 1811 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1812 | Instruction *InstCombiner::visitOr(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 1813 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1814 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1815 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 1816 | if (isa<UndefValue>(Op1)) |
| 1817 | return ReplaceInstUsesWith(I, // X | undef -> -1 |
| 1818 | ConstantIntegral::getAllOnesValue(I.getType())); |
| 1819 | |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1820 | // or X, X = X or X, 0 == X |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1821 | if (Op0 == Op1 || Op1 == Constant::getNullValue(I.getType())) |
| 1822 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1823 | |
| 1824 | // or X, -1 == -1 |
Chris Lattner | ad44ebf | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 1825 | if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1)) { |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1826 | // If X is known to only contain bits that already exist in RHS, just |
| 1827 | // replace this instruction with RHS directly. |
| 1828 | if (MaskedValueIsZero(Op0, |
| 1829 | cast<ConstantIntegral>(ConstantExpr::getNot(RHS)))) |
| 1830 | return ReplaceInstUsesWith(I, RHS); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1831 | |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1832 | ConstantInt *C1; Value *X; |
| 1833 | // (X & C1) | C2 --> (X | C2) & (C1|C2) |
| 1834 | if (match(Op0, m_And(m_Value(X), m_ConstantInt(C1))) && isOnlyUse(Op0)) { |
Chris Lattner | 6e4c649 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 1835 | Instruction *Or = BinaryOperator::createOr(X, RHS, Op0->getName()); |
| 1836 | Op0->setName(""); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1837 | InsertNewInstBefore(Or, I); |
| 1838 | return BinaryOperator::createAnd(Or, ConstantExpr::getOr(RHS, C1)); |
| 1839 | } |
Chris Lattner | ad44ebf | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 1840 | |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1841 | // (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2) |
| 1842 | if (match(Op0, m_Xor(m_Value(X), m_ConstantInt(C1))) && isOnlyUse(Op0)) { |
| 1843 | std::string Op0Name = Op0->getName(); Op0->setName(""); |
| 1844 | Instruction *Or = BinaryOperator::createOr(X, RHS, Op0Name); |
| 1845 | InsertNewInstBefore(Or, I); |
| 1846 | return BinaryOperator::createXor(Or, |
| 1847 | ConstantExpr::getAnd(C1, ConstantExpr::getNot(RHS))); |
Chris Lattner | ad44ebf | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 1848 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1849 | |
| 1850 | // Try to fold constant and into select arguments. |
| 1851 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1852 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1853 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1854 | if (isa<PHINode>(Op0)) |
| 1855 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 1856 | return NV; |
Chris Lattner | ad44ebf | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 1857 | } |
| 1858 | |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1859 | Value *A, *B; ConstantInt *C1, *C2; |
Chris Lattner | f4d4c87 | 2005-05-07 23:49:08 +0000 | [diff] [blame] | 1860 | |
| 1861 | if (match(Op0, m_And(m_Value(A), m_Value(B)))) |
| 1862 | if (A == Op1 || B == Op1) // (A & ?) | A --> A |
| 1863 | return ReplaceInstUsesWith(I, Op1); |
| 1864 | if (match(Op1, m_And(m_Value(A), m_Value(B)))) |
| 1865 | if (A == Op0 || B == Op0) // A | (A & ?) --> A |
| 1866 | return ReplaceInstUsesWith(I, Op0); |
| 1867 | |
Chris Lattner | 6e4c649 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 1868 | // (X^C)|Y -> (X|Y)^C iff Y&C == 0 |
| 1869 | if (Op0->hasOneUse() && match(Op0, m_Xor(m_Value(A), m_ConstantInt(C1))) && |
| 1870 | MaskedValueIsZero(Op1, C1)) { |
| 1871 | Instruction *NOr = BinaryOperator::createOr(A, Op1, Op0->getName()); |
| 1872 | Op0->setName(""); |
| 1873 | return BinaryOperator::createXor(InsertNewInstBefore(NOr, I), C1); |
| 1874 | } |
| 1875 | |
| 1876 | // Y|(X^C) -> (X|Y)^C iff Y&C == 0 |
| 1877 | if (Op1->hasOneUse() && match(Op1, m_Xor(m_Value(A), m_ConstantInt(C1))) && |
| 1878 | MaskedValueIsZero(Op0, C1)) { |
| 1879 | Instruction *NOr = BinaryOperator::createOr(A, Op0, Op1->getName()); |
| 1880 | Op0->setName(""); |
| 1881 | return BinaryOperator::createXor(InsertNewInstBefore(NOr, I), C1); |
| 1882 | } |
| 1883 | |
Chris Lattner | f4d4c87 | 2005-05-07 23:49:08 +0000 | [diff] [blame] | 1884 | // (A & C1)|(A & C2) == A & (C1|C2) |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1885 | if (match(Op0, m_And(m_Value(A), m_ConstantInt(C1))) && |
| 1886 | match(Op1, m_And(m_Value(B), m_ConstantInt(C2))) && A == B) |
| 1887 | return BinaryOperator::createAnd(A, ConstantExpr::getOr(C1, C2)); |
Chris Lattner | 67ca768 | 2003-08-12 19:11:07 +0000 | [diff] [blame] | 1888 | |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1889 | if (match(Op0, m_Not(m_Value(A)))) { // ~A | Op1 |
| 1890 | if (A == Op1) // ~A | A == -1 |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1891 | return ReplaceInstUsesWith(I, |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1892 | ConstantIntegral::getAllOnesValue(I.getType())); |
| 1893 | } else { |
| 1894 | A = 0; |
| 1895 | } |
Chris Lattner | f4d4c87 | 2005-05-07 23:49:08 +0000 | [diff] [blame] | 1896 | // Note, A is still live here! |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1897 | if (match(Op1, m_Not(m_Value(B)))) { // Op0 | ~B |
| 1898 | if (Op0 == B) |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1899 | return ReplaceInstUsesWith(I, |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1900 | ConstantIntegral::getAllOnesValue(I.getType())); |
Chris Lattner | a27231a | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 1901 | |
Misha Brukman | cb6267b | 2004-07-30 12:50:08 +0000 | [diff] [blame] | 1902 | // (~A | ~B) == (~(A & B)) - De Morgan's Law |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1903 | if (A && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
| 1904 | Value *And = InsertNewInstBefore(BinaryOperator::createAnd(A, B, |
| 1905 | I.getName()+".demorgan"), I); |
| 1906 | return BinaryOperator::createNot(And); |
| 1907 | } |
Chris Lattner | a27231a | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 1908 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1909 | |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 1910 | // (setcc1 A, B) | (setcc2 A, B) --> (setcc3 A, B) |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 1911 | if (SetCondInst *RHS = dyn_cast<SetCondInst>(I.getOperand(1))) { |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 1912 | if (Instruction *R = AssociativeOpt(I, FoldSetCCLogical(*this, RHS))) |
| 1913 | return R; |
| 1914 | |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 1915 | Value *LHSVal, *RHSVal; |
| 1916 | ConstantInt *LHSCst, *RHSCst; |
| 1917 | Instruction::BinaryOps LHSCC, RHSCC; |
| 1918 | if (match(Op0, m_SetCond(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst)))) |
| 1919 | if (match(RHS, m_SetCond(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst)))) |
| 1920 | if (LHSVal == RHSVal && // Found (X setcc C1) | (X setcc C2) |
| 1921 | // Set[GL]E X, CST is folded to Set[GL]T elsewhere. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1922 | LHSCC != Instruction::SetGE && LHSCC != Instruction::SetLE && |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 1923 | RHSCC != Instruction::SetGE && RHSCC != Instruction::SetLE) { |
| 1924 | // Ensure that the larger constant is on the RHS. |
| 1925 | Constant *Cmp = ConstantExpr::getSetGT(LHSCst, RHSCst); |
| 1926 | SetCondInst *LHS = cast<SetCondInst>(Op0); |
| 1927 | if (cast<ConstantBool>(Cmp)->getValue()) { |
| 1928 | std::swap(LHS, RHS); |
| 1929 | std::swap(LHSCst, RHSCst); |
| 1930 | std::swap(LHSCC, RHSCC); |
| 1931 | } |
| 1932 | |
| 1933 | // At this point, we know we have have two setcc instructions |
| 1934 | // comparing a value against two constants and or'ing the result |
| 1935 | // together. Because of the above check, we know that we only have |
| 1936 | // SetEQ, SetNE, SetLT, and SetGT here. We also know (from the |
| 1937 | // FoldSetCCLogical check above), that the two constants are not |
| 1938 | // equal. |
| 1939 | assert(LHSCst != RHSCst && "Compares not folded above?"); |
| 1940 | |
| 1941 | switch (LHSCC) { |
| 1942 | default: assert(0 && "Unknown integer condition code!"); |
| 1943 | case Instruction::SetEQ: |
| 1944 | switch (RHSCC) { |
| 1945 | default: assert(0 && "Unknown integer condition code!"); |
| 1946 | case Instruction::SetEQ: |
| 1947 | if (LHSCst == SubOne(RHSCst)) {// (X == 13 | X == 14) -> X-13 <u 2 |
| 1948 | Constant *AddCST = ConstantExpr::getNeg(LHSCst); |
| 1949 | Instruction *Add = BinaryOperator::createAdd(LHSVal, AddCST, |
| 1950 | LHSVal->getName()+".off"); |
| 1951 | InsertNewInstBefore(Add, I); |
| 1952 | const Type *UnsType = Add->getType()->getUnsignedVersion(); |
| 1953 | Value *OffsetVal = InsertCastBefore(Add, UnsType, I); |
| 1954 | AddCST = ConstantExpr::getSub(AddOne(RHSCst), LHSCst); |
| 1955 | AddCST = ConstantExpr::getCast(AddCST, UnsType); |
| 1956 | return new SetCondInst(Instruction::SetLT, OffsetVal, AddCST); |
| 1957 | } |
| 1958 | break; // (X == 13 | X == 15) -> no change |
| 1959 | |
Chris Lattner | 240d6f4 | 2005-04-19 06:04:18 +0000 | [diff] [blame] | 1960 | case Instruction::SetGT: // (X == 13 | X > 14) -> no change |
| 1961 | break; |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 1962 | case Instruction::SetNE: // (X == 13 | X != 15) -> X != 15 |
| 1963 | case Instruction::SetLT: // (X == 13 | X < 15) -> X < 15 |
| 1964 | return ReplaceInstUsesWith(I, RHS); |
| 1965 | } |
| 1966 | break; |
| 1967 | case Instruction::SetNE: |
| 1968 | switch (RHSCC) { |
| 1969 | default: assert(0 && "Unknown integer condition code!"); |
| 1970 | case Instruction::SetLT: // (X != 13 | X < 15) -> X < 15 |
| 1971 | return ReplaceInstUsesWith(I, RHS); |
| 1972 | case Instruction::SetEQ: // (X != 13 | X == 15) -> X != 13 |
| 1973 | case Instruction::SetGT: // (X != 13 | X > 15) -> X != 13 |
| 1974 | return ReplaceInstUsesWith(I, LHS); |
| 1975 | case Instruction::SetNE: // (X != 13 | X != 15) -> true |
| 1976 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 1977 | } |
| 1978 | break; |
| 1979 | case Instruction::SetLT: |
| 1980 | switch (RHSCC) { |
| 1981 | default: assert(0 && "Unknown integer condition code!"); |
| 1982 | case Instruction::SetEQ: // (X < 13 | X == 14) -> no change |
| 1983 | break; |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 1984 | case Instruction::SetGT: // (X < 13 | X > 15) -> (X-13) > 2 |
| 1985 | return InsertRangeTest(LHSVal, LHSCst, AddOne(RHSCst), false, I); |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 1986 | case Instruction::SetNE: // (X < 13 | X != 15) -> X != 15 |
| 1987 | case Instruction::SetLT: // (X < 13 | X < 15) -> X < 15 |
| 1988 | return ReplaceInstUsesWith(I, RHS); |
| 1989 | } |
| 1990 | break; |
| 1991 | case Instruction::SetGT: |
| 1992 | switch (RHSCC) { |
| 1993 | default: assert(0 && "Unknown integer condition code!"); |
| 1994 | case Instruction::SetEQ: // (X > 13 | X == 15) -> X > 13 |
| 1995 | case Instruction::SetGT: // (X > 13 | X > 15) -> X > 13 |
| 1996 | return ReplaceInstUsesWith(I, LHS); |
| 1997 | case Instruction::SetNE: // (X > 13 | X != 15) -> true |
| 1998 | case Instruction::SetLT: // (X > 13 | X < 15) -> true |
| 1999 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 2000 | } |
| 2001 | } |
| 2002 | } |
| 2003 | } |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2004 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2005 | } |
| 2006 | |
Chris Lattner | c317d39 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 2007 | // XorSelf - Implements: X ^ X --> 0 |
| 2008 | struct XorSelf { |
| 2009 | Value *RHS; |
| 2010 | XorSelf(Value *rhs) : RHS(rhs) {} |
| 2011 | bool shouldApply(Value *LHS) const { return LHS == RHS; } |
| 2012 | Instruction *apply(BinaryOperator &Xor) const { |
| 2013 | return &Xor; |
| 2014 | } |
| 2015 | }; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2016 | |
| 2017 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2018 | Instruction *InstCombiner::visitXor(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 2019 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2020 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2021 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2022 | if (isa<UndefValue>(Op1)) |
| 2023 | return ReplaceInstUsesWith(I, Op1); // X ^ undef -> undef |
| 2024 | |
Chris Lattner | c317d39 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 2025 | // xor X, X = 0, even if X is nested in a sequence of Xor's. |
| 2026 | if (Instruction *Result = AssociativeOpt(I, XorSelf(Op1))) { |
| 2027 | assert(Result == &I && "AssociativeOpt didn't work?"); |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2028 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | c317d39 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 2029 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2030 | |
Chris Lattner | eca0c5c | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 2031 | if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1)) { |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2032 | // xor X, 0 == X |
Chris Lattner | eca0c5c | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 2033 | if (RHS->isNullValue()) |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2034 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2035 | |
Chris Lattner | eca0c5c | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 2036 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) { |
Chris Lattner | 05bd1b2 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 2037 | // xor (setcc A, B), true = not (setcc A, B) = setncc A, B |
Chris Lattner | eca0c5c | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 2038 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(Op0I)) |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 2039 | if (RHS == ConstantBool::True && SCI->hasOneUse()) |
Chris Lattner | 05bd1b2 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 2040 | return new SetCondInst(SCI->getInverseCondition(), |
| 2041 | SCI->getOperand(0), SCI->getOperand(1)); |
Chris Lattner | ad5b4fb | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 2042 | |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 2043 | // ~(c-X) == X-c-1 == X+(-c-1) |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2044 | if (Op0I->getOpcode() == Instruction::Sub && RHS->isAllOnesValue()) |
| 2045 | if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2046 | Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C); |
| 2047 | Constant *ConstantRHS = ConstantExpr::getSub(NegOp0I0C, |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2048 | ConstantInt::get(I.getType(), 1)); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2049 | return BinaryOperator::createAdd(Op0I->getOperand(1), ConstantRHS); |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2050 | } |
Chris Lattner | 5b62aa7 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 2051 | |
| 2052 | // ~(~X & Y) --> (X | ~Y) |
| 2053 | if (Op0I->getOpcode() == Instruction::And && RHS->isAllOnesValue()) { |
| 2054 | if (dyn_castNotVal(Op0I->getOperand(1))) Op0I->swapOperands(); |
| 2055 | if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0))) { |
| 2056 | Instruction *NotY = |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2057 | BinaryOperator::createNot(Op0I->getOperand(1), |
Chris Lattner | 5b62aa7 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 2058 | Op0I->getOperand(1)->getName()+".not"); |
| 2059 | InsertNewInstBefore(NotY, I); |
| 2060 | return BinaryOperator::createOr(Op0NotVal, NotY); |
| 2061 | } |
| 2062 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2063 | |
Chris Lattner | eca0c5c | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 2064 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) |
Chris Lattner | ad5b4fb | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 2065 | switch (Op0I->getOpcode()) { |
| 2066 | case Instruction::Add: |
Chris Lattner | 689d24b | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 2067 | // ~(X-c) --> (-c-1)-X |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2068 | if (RHS->isAllOnesValue()) { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2069 | Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI); |
| 2070 | return BinaryOperator::createSub( |
| 2071 | ConstantExpr::getSub(NegOp0CI, |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2072 | ConstantInt::get(I.getType(), 1)), |
Chris Lattner | 689d24b | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 2073 | Op0I->getOperand(0)); |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2074 | } |
Chris Lattner | ad5b4fb | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 2075 | break; |
| 2076 | case Instruction::And: |
Chris Lattner | eca0c5c | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 2077 | // (X & C1) ^ C2 --> (X & C1) | C2 iff (C1&C2) == 0 |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2078 | if (ConstantExpr::getAnd(RHS, Op0CI)->isNullValue()) |
| 2079 | return BinaryOperator::createOr(Op0, RHS); |
Chris Lattner | ad5b4fb | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 2080 | break; |
| 2081 | case Instruction::Or: |
Chris Lattner | eca0c5c | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 2082 | // (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2 |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2083 | if (ConstantExpr::getAnd(RHS, Op0CI) == RHS) |
Chris Lattner | 448c323 | 2004-06-10 02:12:35 +0000 | [diff] [blame] | 2084 | return BinaryOperator::createAnd(Op0, ConstantExpr::getNot(RHS)); |
Chris Lattner | ad5b4fb | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 2085 | break; |
| 2086 | default: break; |
Chris Lattner | eca0c5c | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 2087 | } |
Chris Lattner | 05bd1b2 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 2088 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2089 | |
| 2090 | // Try to fold constant and into select arguments. |
| 2091 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2092 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2093 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2094 | if (isa<PHINode>(Op0)) |
| 2095 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2096 | return NV; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2097 | } |
| 2098 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 2099 | if (Value *X = dyn_castNotVal(Op0)) // ~A ^ A == -1 |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2100 | if (X == Op1) |
| 2101 | return ReplaceInstUsesWith(I, |
| 2102 | ConstantIntegral::getAllOnesValue(I.getType())); |
| 2103 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 2104 | if (Value *X = dyn_castNotVal(Op1)) // A ^ ~A == -1 |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2105 | if (X == Op0) |
| 2106 | return ReplaceInstUsesWith(I, |
| 2107 | ConstantIntegral::getAllOnesValue(I.getType())); |
| 2108 | |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 2109 | if (Instruction *Op1I = dyn_cast<Instruction>(Op1)) |
Chris Lattner | 26ca7e1 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 2110 | if (Op1I->getOpcode() == Instruction::Or) { |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 2111 | if (Op1I->getOperand(0) == Op0) { // B^(B|A) == (A|B)^B |
| 2112 | cast<BinaryOperator>(Op1I)->swapOperands(); |
| 2113 | I.swapOperands(); |
| 2114 | std::swap(Op0, Op1); |
| 2115 | } else if (Op1I->getOperand(1) == Op0) { // B^(A|B) == (A|B)^B |
| 2116 | I.swapOperands(); |
| 2117 | std::swap(Op0, Op1); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2118 | } |
Chris Lattner | 26ca7e1 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 2119 | } else if (Op1I->getOpcode() == Instruction::Xor) { |
| 2120 | if (Op0 == Op1I->getOperand(0)) // A^(A^B) == B |
| 2121 | return ReplaceInstUsesWith(I, Op1I->getOperand(1)); |
| 2122 | else if (Op0 == Op1I->getOperand(1)) // A^(B^A) == B |
| 2123 | return ReplaceInstUsesWith(I, Op1I->getOperand(0)); |
| 2124 | } |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 2125 | |
| 2126 | if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 2127 | if (Op0I->getOpcode() == Instruction::Or && Op0I->hasOneUse()) { |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 2128 | if (Op0I->getOperand(0) == Op1) // (B|A)^B == (A|B)^B |
| 2129 | cast<BinaryOperator>(Op0I)->swapOperands(); |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 2130 | if (Op0I->getOperand(1) == Op1) { // (A|B)^B == A & ~B |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 2131 | Value *NotB = InsertNewInstBefore(BinaryOperator::createNot(Op1, |
| 2132 | Op1->getName()+".not"), I); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2133 | return BinaryOperator::createAnd(Op0I->getOperand(0), NotB); |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 2134 | } |
Chris Lattner | 26ca7e1 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 2135 | } else if (Op0I->getOpcode() == Instruction::Xor) { |
| 2136 | if (Op1 == Op0I->getOperand(0)) // (A^B)^A == B |
| 2137 | return ReplaceInstUsesWith(I, Op0I->getOperand(1)); |
| 2138 | else if (Op1 == Op0I->getOperand(1)) // (B^A)^A == B |
| 2139 | return ReplaceInstUsesWith(I, Op0I->getOperand(0)); |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 2140 | } |
| 2141 | |
Chris Lattner | 1484089 | 2004-08-01 19:42:59 +0000 | [diff] [blame] | 2142 | // (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0 |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2143 | Value *A, *B; ConstantInt *C1, *C2; |
| 2144 | if (match(Op0, m_And(m_Value(A), m_ConstantInt(C1))) && |
| 2145 | match(Op1, m_And(m_Value(B), m_ConstantInt(C2))) && |
Chris Lattner | 1484089 | 2004-08-01 19:42:59 +0000 | [diff] [blame] | 2146 | ConstantExpr::getAnd(C1, C2)->isNullValue()) |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2147 | return BinaryOperator::createOr(Op0, Op1); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 2148 | |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2149 | // (setcc1 A, B) ^ (setcc2 A, B) --> (setcc3 A, B) |
| 2150 | if (SetCondInst *RHS = dyn_cast<SetCondInst>(I.getOperand(1))) |
| 2151 | if (Instruction *R = AssociativeOpt(I, FoldSetCCLogical(*this, RHS))) |
| 2152 | return R; |
| 2153 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2154 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2155 | } |
| 2156 | |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2157 | /// MulWithOverflow - Compute Result = In1*In2, returning true if the result |
| 2158 | /// overflowed for this type. |
| 2159 | static bool MulWithOverflow(ConstantInt *&Result, ConstantInt *In1, |
| 2160 | ConstantInt *In2) { |
| 2161 | Result = cast<ConstantInt>(ConstantExpr::getMul(In1, In2)); |
| 2162 | return !In2->isNullValue() && ConstantExpr::getDiv(Result, In2) != In1; |
| 2163 | } |
| 2164 | |
| 2165 | static bool isPositive(ConstantInt *C) { |
| 2166 | return cast<ConstantSInt>(C)->getValue() >= 0; |
| 2167 | } |
| 2168 | |
| 2169 | /// AddWithOverflow - Compute Result = In1+In2, returning true if the result |
| 2170 | /// overflowed for this type. |
| 2171 | static bool AddWithOverflow(ConstantInt *&Result, ConstantInt *In1, |
| 2172 | ConstantInt *In2) { |
| 2173 | Result = cast<ConstantInt>(ConstantExpr::getAdd(In1, In2)); |
| 2174 | |
| 2175 | if (In1->getType()->isUnsigned()) |
| 2176 | return cast<ConstantUInt>(Result)->getValue() < |
| 2177 | cast<ConstantUInt>(In1)->getValue(); |
| 2178 | if (isPositive(In1) != isPositive(In2)) |
| 2179 | return false; |
| 2180 | if (isPositive(In1)) |
| 2181 | return cast<ConstantSInt>(Result)->getValue() < |
| 2182 | cast<ConstantSInt>(In1)->getValue(); |
| 2183 | return cast<ConstantSInt>(Result)->getValue() > |
| 2184 | cast<ConstantSInt>(In1)->getValue(); |
| 2185 | } |
| 2186 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 2187 | /// EmitGEPOffset - Given a getelementptr instruction/constantexpr, emit the |
| 2188 | /// code necessary to compute the offset from the base pointer (without adding |
| 2189 | /// in the base pointer). Return the result as a signed integer of intptr size. |
| 2190 | static Value *EmitGEPOffset(User *GEP, Instruction &I, InstCombiner &IC) { |
| 2191 | TargetData &TD = IC.getTargetData(); |
| 2192 | gep_type_iterator GTI = gep_type_begin(GEP); |
| 2193 | const Type *UIntPtrTy = TD.getIntPtrType(); |
| 2194 | const Type *SIntPtrTy = UIntPtrTy->getSignedVersion(); |
| 2195 | Value *Result = Constant::getNullValue(SIntPtrTy); |
| 2196 | |
| 2197 | // Build a mask for high order bits. |
| 2198 | uint64_t PtrSizeMask = ~0ULL; |
| 2199 | PtrSizeMask >>= 64-(TD.getPointerSize()*8); |
| 2200 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 2201 | for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i, ++GTI) { |
| 2202 | Value *Op = GEP->getOperand(i); |
Chris Lattner | 0b84c80 | 2005-01-13 23:26:48 +0000 | [diff] [blame] | 2203 | uint64_t Size = TD.getTypeSize(GTI.getIndexedType()) & PtrSizeMask; |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 2204 | Constant *Scale = ConstantExpr::getCast(ConstantUInt::get(UIntPtrTy, Size), |
| 2205 | SIntPtrTy); |
| 2206 | if (Constant *OpC = dyn_cast<Constant>(Op)) { |
| 2207 | if (!OpC->isNullValue()) { |
Chris Lattner | 5bdf04c | 2005-01-13 20:40:58 +0000 | [diff] [blame] | 2208 | OpC = ConstantExpr::getCast(OpC, SIntPtrTy); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 2209 | Scale = ConstantExpr::getMul(OpC, Scale); |
| 2210 | if (Constant *RC = dyn_cast<Constant>(Result)) |
| 2211 | Result = ConstantExpr::getAdd(RC, Scale); |
| 2212 | else { |
| 2213 | // Emit an add instruction. |
| 2214 | Result = IC.InsertNewInstBefore( |
| 2215 | BinaryOperator::createAdd(Result, Scale, |
| 2216 | GEP->getName()+".offs"), I); |
| 2217 | } |
| 2218 | } |
| 2219 | } else { |
Chris Lattner | 6f7f02f | 2005-01-14 17:17:59 +0000 | [diff] [blame] | 2220 | // Convert to correct type. |
| 2221 | Op = IC.InsertNewInstBefore(new CastInst(Op, SIntPtrTy, |
| 2222 | Op->getName()+".c"), I); |
| 2223 | if (Size != 1) |
Chris Lattner | 5bdf04c | 2005-01-13 20:40:58 +0000 | [diff] [blame] | 2224 | // We'll let instcombine(mul) convert this to a shl if possible. |
| 2225 | Op = IC.InsertNewInstBefore(BinaryOperator::createMul(Op, Scale, |
| 2226 | GEP->getName()+".idx"), I); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 2227 | |
| 2228 | // Emit an add instruction. |
Chris Lattner | 5bdf04c | 2005-01-13 20:40:58 +0000 | [diff] [blame] | 2229 | Result = IC.InsertNewInstBefore(BinaryOperator::createAdd(Op, Result, |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 2230 | GEP->getName()+".offs"), I); |
| 2231 | } |
| 2232 | } |
| 2233 | return Result; |
| 2234 | } |
| 2235 | |
| 2236 | /// FoldGEPSetCC - Fold comparisons between a GEP instruction and something |
| 2237 | /// else. At this point we know that the GEP is on the LHS of the comparison. |
| 2238 | Instruction *InstCombiner::FoldGEPSetCC(User *GEPLHS, Value *RHS, |
| 2239 | Instruction::BinaryOps Cond, |
| 2240 | Instruction &I) { |
| 2241 | assert(dyn_castGetElementPtr(GEPLHS) && "LHS is not a getelementptr!"); |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 2242 | |
| 2243 | if (CastInst *CI = dyn_cast<CastInst>(RHS)) |
| 2244 | if (isa<PointerType>(CI->getOperand(0)->getType())) |
| 2245 | RHS = CI->getOperand(0); |
| 2246 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 2247 | Value *PtrBase = GEPLHS->getOperand(0); |
| 2248 | if (PtrBase == RHS) { |
| 2249 | // As an optimization, we don't actually have to compute the actual value of |
| 2250 | // OFFSET if this is a seteq or setne comparison, just return whether each |
| 2251 | // index is zero or not. |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 2252 | if (Cond == Instruction::SetEQ || Cond == Instruction::SetNE) { |
| 2253 | Instruction *InVal = 0; |
Chris Lattner | ad5fec1 | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 2254 | gep_type_iterator GTI = gep_type_begin(GEPLHS); |
| 2255 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i, ++GTI) { |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 2256 | bool EmitIt = true; |
| 2257 | if (Constant *C = dyn_cast<Constant>(GEPLHS->getOperand(i))) { |
| 2258 | if (isa<UndefValue>(C)) // undef index -> undef. |
| 2259 | return ReplaceInstUsesWith(I, UndefValue::get(I.getType())); |
| 2260 | if (C->isNullValue()) |
| 2261 | EmitIt = false; |
Chris Lattner | ad5fec1 | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 2262 | else if (TD->getTypeSize(GTI.getIndexedType()) == 0) { |
| 2263 | EmitIt = false; // This is indexing into a zero sized array? |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2264 | } else if (isa<ConstantInt>(C)) |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 2265 | return ReplaceInstUsesWith(I, // No comparison is needed here. |
| 2266 | ConstantBool::get(Cond == Instruction::SetNE)); |
| 2267 | } |
| 2268 | |
| 2269 | if (EmitIt) { |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2270 | Instruction *Comp = |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 2271 | new SetCondInst(Cond, GEPLHS->getOperand(i), |
| 2272 | Constant::getNullValue(GEPLHS->getOperand(i)->getType())); |
| 2273 | if (InVal == 0) |
| 2274 | InVal = Comp; |
| 2275 | else { |
| 2276 | InVal = InsertNewInstBefore(InVal, I); |
| 2277 | InsertNewInstBefore(Comp, I); |
| 2278 | if (Cond == Instruction::SetNE) // True if any are unequal |
| 2279 | InVal = BinaryOperator::createOr(InVal, Comp); |
| 2280 | else // True if all are equal |
| 2281 | InVal = BinaryOperator::createAnd(InVal, Comp); |
| 2282 | } |
| 2283 | } |
| 2284 | } |
| 2285 | |
| 2286 | if (InVal) |
| 2287 | return InVal; |
| 2288 | else |
| 2289 | ReplaceInstUsesWith(I, // No comparison is needed here, all indexes = 0 |
| 2290 | ConstantBool::get(Cond == Instruction::SetEQ)); |
| 2291 | } |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 2292 | |
| 2293 | // Only lower this if the setcc is the only user of the GEP or if we expect |
| 2294 | // the result to fold to a constant! |
| 2295 | if (isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) { |
| 2296 | // ((gep Ptr, OFFSET) cmp Ptr) ---> (OFFSET cmp 0). |
| 2297 | Value *Offset = EmitGEPOffset(GEPLHS, I, *this); |
| 2298 | return new SetCondInst(Cond, Offset, |
| 2299 | Constant::getNullValue(Offset->getType())); |
| 2300 | } |
| 2301 | } else if (User *GEPRHS = dyn_castGetElementPtr(RHS)) { |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 2302 | // If the base pointers are different, but the indices are the same, just |
| 2303 | // compare the base pointer. |
| 2304 | if (PtrBase != GEPRHS->getOperand(0)) { |
| 2305 | bool IndicesTheSame = GEPLHS->getNumOperands()==GEPRHS->getNumOperands(); |
Chris Lattner | 93b94a6 | 2005-04-26 14:40:41 +0000 | [diff] [blame] | 2306 | IndicesTheSame &= GEPLHS->getOperand(0)->getType() == |
| 2307 | GEPRHS->getOperand(0)->getType(); |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 2308 | if (IndicesTheSame) |
| 2309 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i) |
| 2310 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { |
| 2311 | IndicesTheSame = false; |
| 2312 | break; |
| 2313 | } |
| 2314 | |
| 2315 | // If all indices are the same, just compare the base pointers. |
| 2316 | if (IndicesTheSame) |
| 2317 | return new SetCondInst(Cond, GEPLHS->getOperand(0), |
| 2318 | GEPRHS->getOperand(0)); |
| 2319 | |
| 2320 | // Otherwise, the base pointers are different and the indices are |
| 2321 | // different, bail out. |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 2322 | return 0; |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 2323 | } |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 2324 | |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 2325 | // If one of the GEPs has all zero indices, recurse. |
| 2326 | bool AllZeros = true; |
| 2327 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i) |
| 2328 | if (!isa<Constant>(GEPLHS->getOperand(i)) || |
| 2329 | !cast<Constant>(GEPLHS->getOperand(i))->isNullValue()) { |
| 2330 | AllZeros = false; |
| 2331 | break; |
| 2332 | } |
| 2333 | if (AllZeros) |
| 2334 | return FoldGEPSetCC(GEPRHS, GEPLHS->getOperand(0), |
| 2335 | SetCondInst::getSwappedCondition(Cond), I); |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 2336 | |
| 2337 | // If the other GEP has all zero indices, recurse. |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 2338 | AllZeros = true; |
| 2339 | for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i) |
| 2340 | if (!isa<Constant>(GEPRHS->getOperand(i)) || |
| 2341 | !cast<Constant>(GEPRHS->getOperand(i))->isNullValue()) { |
| 2342 | AllZeros = false; |
| 2343 | break; |
| 2344 | } |
| 2345 | if (AllZeros) |
| 2346 | return FoldGEPSetCC(GEPLHS, GEPRHS->getOperand(0), Cond, I); |
| 2347 | |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 2348 | if (GEPLHS->getNumOperands() == GEPRHS->getNumOperands()) { |
| 2349 | // If the GEPs only differ by one index, compare it. |
| 2350 | unsigned NumDifferences = 0; // Keep track of # differences. |
| 2351 | unsigned DiffOperand = 0; // The operand that differs. |
| 2352 | for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i) |
| 2353 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 2354 | if (GEPLHS->getOperand(i)->getType()->getPrimitiveSizeInBits() != |
| 2355 | GEPRHS->getOperand(i)->getType()->getPrimitiveSizeInBits()) { |
Chris Lattner | 45f57b8 | 2005-01-21 23:06:49 +0000 | [diff] [blame] | 2356 | // Irreconcilable differences. |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 2357 | NumDifferences = 2; |
| 2358 | break; |
| 2359 | } else { |
| 2360 | if (NumDifferences++) break; |
| 2361 | DiffOperand = i; |
| 2362 | } |
| 2363 | } |
| 2364 | |
| 2365 | if (NumDifferences == 0) // SAME GEP? |
| 2366 | return ReplaceInstUsesWith(I, // No comparison is needed here. |
| 2367 | ConstantBool::get(Cond == Instruction::SetEQ)); |
| 2368 | else if (NumDifferences == 1) { |
Chris Lattner | 45f57b8 | 2005-01-21 23:06:49 +0000 | [diff] [blame] | 2369 | Value *LHSV = GEPLHS->getOperand(DiffOperand); |
| 2370 | Value *RHSV = GEPRHS->getOperand(DiffOperand); |
| 2371 | if (LHSV->getType() != RHSV->getType()) |
| 2372 | LHSV = InsertNewInstBefore(new CastInst(LHSV, RHSV->getType(), |
| 2373 | LHSV->getName()+".c"), I); |
| 2374 | return new SetCondInst(Cond, LHSV, RHSV); |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 2375 | } |
| 2376 | } |
| 2377 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 2378 | // Only lower this if the setcc is the only user of the GEP or if we expect |
| 2379 | // the result to fold to a constant! |
| 2380 | if ((isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) && |
| 2381 | (isa<ConstantExpr>(GEPRHS) || GEPRHS->hasOneUse())) { |
| 2382 | // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) ---> (OFFSET1 cmp OFFSET2) |
| 2383 | Value *L = EmitGEPOffset(GEPLHS, I, *this); |
| 2384 | Value *R = EmitGEPOffset(GEPRHS, I, *this); |
| 2385 | return new SetCondInst(Cond, L, R); |
| 2386 | } |
| 2387 | } |
| 2388 | return 0; |
| 2389 | } |
| 2390 | |
| 2391 | |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 2392 | Instruction *InstCombiner::visitSetCondInst(SetCondInst &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 2393 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2394 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2395 | const Type *Ty = Op0->getType(); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2396 | |
| 2397 | // setcc X, X |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2398 | if (Op0 == Op1) |
| 2399 | return ReplaceInstUsesWith(I, ConstantBool::get(isTrueWhenEqual(I))); |
Chris Lattner | 53a5b57 | 2002-05-09 20:11:54 +0000 | [diff] [blame] | 2400 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2401 | if (isa<UndefValue>(Op1)) // X setcc undef -> undef |
| 2402 | return ReplaceInstUsesWith(I, UndefValue::get(Type::BoolTy)); |
| 2403 | |
Chris Lattner | 711b340 | 2004-11-14 07:33:16 +0000 | [diff] [blame] | 2404 | // setcc <global/alloca*/null>, <global/alloca*/null> - Global/Stack value |
| 2405 | // addresses never equal each other! We already know that Op0 != Op1. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2406 | if ((isa<GlobalValue>(Op0) || isa<AllocaInst>(Op0) || |
| 2407 | isa<ConstantPointerNull>(Op0)) && |
| 2408 | (isa<GlobalValue>(Op1) || isa<AllocaInst>(Op1) || |
Chris Lattner | 711b340 | 2004-11-14 07:33:16 +0000 | [diff] [blame] | 2409 | isa<ConstantPointerNull>(Op1))) |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2410 | return ReplaceInstUsesWith(I, ConstantBool::get(!isTrueWhenEqual(I))); |
| 2411 | |
| 2412 | // setcc's with boolean values can always be turned into bitwise operations |
| 2413 | if (Ty == Type::BoolTy) { |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 2414 | switch (I.getOpcode()) { |
| 2415 | default: assert(0 && "Invalid setcc instruction!"); |
| 2416 | case Instruction::SetEQ: { // seteq bool %A, %B -> ~(A^B) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2417 | Instruction *Xor = BinaryOperator::createXor(Op0, Op1, I.getName()+"tmp"); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2418 | InsertNewInstBefore(Xor, I); |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 2419 | return BinaryOperator::createNot(Xor); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2420 | } |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 2421 | case Instruction::SetNE: |
| 2422 | return BinaryOperator::createXor(Op0, Op1); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2423 | |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 2424 | case Instruction::SetGT: |
| 2425 | std::swap(Op0, Op1); // Change setgt -> setlt |
| 2426 | // FALL THROUGH |
| 2427 | case Instruction::SetLT: { // setlt bool A, B -> ~X & Y |
| 2428 | Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp"); |
| 2429 | InsertNewInstBefore(Not, I); |
| 2430 | return BinaryOperator::createAnd(Not, Op1); |
| 2431 | } |
| 2432 | case Instruction::SetGE: |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2433 | std::swap(Op0, Op1); // Change setge -> setle |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 2434 | // FALL THROUGH |
| 2435 | case Instruction::SetLE: { // setle bool %A, %B -> ~A | B |
| 2436 | Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp"); |
| 2437 | InsertNewInstBefore(Not, I); |
| 2438 | return BinaryOperator::createOr(Not, Op1); |
| 2439 | } |
| 2440 | } |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2441 | } |
| 2442 | |
Chris Lattner | 2be51ae | 2004-06-09 04:24:29 +0000 | [diff] [blame] | 2443 | // See if we are doing a comparison between a constant and an instruction that |
| 2444 | // can be folded into the comparison. |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2445 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2446 | // Check to see if we are comparing against the minimum or maximum value... |
| 2447 | if (CI->isMinValue()) { |
| 2448 | if (I.getOpcode() == Instruction::SetLT) // A < MIN -> FALSE |
| 2449 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 2450 | if (I.getOpcode() == Instruction::SetGE) // A >= MIN -> TRUE |
| 2451 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 2452 | if (I.getOpcode() == Instruction::SetLE) // A <= MIN -> A == MIN |
| 2453 | return BinaryOperator::createSetEQ(Op0, Op1); |
| 2454 | if (I.getOpcode() == Instruction::SetGT) // A > MIN -> A != MIN |
| 2455 | return BinaryOperator::createSetNE(Op0, Op1); |
| 2456 | |
| 2457 | } else if (CI->isMaxValue()) { |
| 2458 | if (I.getOpcode() == Instruction::SetGT) // A > MAX -> FALSE |
| 2459 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 2460 | if (I.getOpcode() == Instruction::SetLE) // A <= MAX -> TRUE |
| 2461 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 2462 | if (I.getOpcode() == Instruction::SetGE) // A >= MAX -> A == MAX |
| 2463 | return BinaryOperator::createSetEQ(Op0, Op1); |
| 2464 | if (I.getOpcode() == Instruction::SetLT) // A < MAX -> A != MAX |
| 2465 | return BinaryOperator::createSetNE(Op0, Op1); |
| 2466 | |
| 2467 | // Comparing against a value really close to min or max? |
| 2468 | } else if (isMinValuePlusOne(CI)) { |
| 2469 | if (I.getOpcode() == Instruction::SetLT) // A < MIN+1 -> A == MIN |
| 2470 | return BinaryOperator::createSetEQ(Op0, SubOne(CI)); |
| 2471 | if (I.getOpcode() == Instruction::SetGE) // A >= MIN-1 -> A != MIN |
| 2472 | return BinaryOperator::createSetNE(Op0, SubOne(CI)); |
| 2473 | |
| 2474 | } else if (isMaxValueMinusOne(CI)) { |
| 2475 | if (I.getOpcode() == Instruction::SetGT) // A > MAX-1 -> A == MAX |
| 2476 | return BinaryOperator::createSetEQ(Op0, AddOne(CI)); |
| 2477 | if (I.getOpcode() == Instruction::SetLE) // A <= MAX-1 -> A != MAX |
| 2478 | return BinaryOperator::createSetNE(Op0, AddOne(CI)); |
| 2479 | } |
| 2480 | |
| 2481 | // If we still have a setle or setge instruction, turn it into the |
| 2482 | // appropriate setlt or setgt instruction. Since the border cases have |
| 2483 | // already been handled above, this requires little checking. |
| 2484 | // |
| 2485 | if (I.getOpcode() == Instruction::SetLE) |
| 2486 | return BinaryOperator::createSetLT(Op0, AddOne(CI)); |
| 2487 | if (I.getOpcode() == Instruction::SetGE) |
| 2488 | return BinaryOperator::createSetGT(Op0, SubOne(CI)); |
| 2489 | |
Chris Lattner | 3c6a0d4 | 2004-05-25 06:32:08 +0000 | [diff] [blame] | 2490 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
Chris Lattner | 648e3bc | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 2491 | switch (LHSI->getOpcode()) { |
| 2492 | case Instruction::And: |
| 2493 | if (LHSI->hasOneUse() && isa<ConstantInt>(LHSI->getOperand(1)) && |
| 2494 | LHSI->getOperand(0)->hasOneUse()) { |
| 2495 | // If this is: (X >> C1) & C2 != C3 (where any shift and any compare |
| 2496 | // could exist), turn it into (X & (C2 << C1)) != (C3 << C1). This |
| 2497 | // happens a LOT in code produced by the C front-end, for bitfield |
| 2498 | // access. |
| 2499 | ShiftInst *Shift = dyn_cast<ShiftInst>(LHSI->getOperand(0)); |
| 2500 | ConstantUInt *ShAmt; |
| 2501 | ShAmt = Shift ? dyn_cast<ConstantUInt>(Shift->getOperand(1)) : 0; |
| 2502 | ConstantInt *AndCST = cast<ConstantInt>(LHSI->getOperand(1)); |
| 2503 | const Type *Ty = LHSI->getType(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2504 | |
Chris Lattner | 648e3bc | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 2505 | // We can fold this as long as we can't shift unknown bits |
| 2506 | // into the mask. This can only happen with signed shift |
| 2507 | // rights, as they sign-extend. |
| 2508 | if (ShAmt) { |
| 2509 | bool CanFold = Shift->getOpcode() != Instruction::Shr || |
Chris Lattner | 0cba71b | 2004-09-28 17:54:07 +0000 | [diff] [blame] | 2510 | Shift->getType()->isUnsigned(); |
Chris Lattner | 648e3bc | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 2511 | if (!CanFold) { |
| 2512 | // To test for the bad case of the signed shr, see if any |
| 2513 | // of the bits shifted in could be tested after the mask. |
Chris Lattner | d7e31cf | 2005-06-17 01:29:28 +0000 | [diff] [blame^] | 2514 | int ShAmtVal = Ty->getPrimitiveSizeInBits()-ShAmt->getValue(); |
| 2515 | if (ShAmtVal < 0) ShAmtVal = 0; // Out of range shift. |
| 2516 | |
| 2517 | Constant *OShAmt = ConstantUInt::get(Type::UByteTy, ShAmtVal); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2518 | Constant *ShVal = |
Chris Lattner | 648e3bc | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 2519 | ConstantExpr::getShl(ConstantInt::getAllOnesValue(Ty), OShAmt); |
| 2520 | if (ConstantExpr::getAnd(ShVal, AndCST)->isNullValue()) |
| 2521 | CanFold = true; |
| 2522 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2523 | |
Chris Lattner | 648e3bc | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 2524 | if (CanFold) { |
Chris Lattner | 0cba71b | 2004-09-28 17:54:07 +0000 | [diff] [blame] | 2525 | Constant *NewCst; |
| 2526 | if (Shift->getOpcode() == Instruction::Shl) |
| 2527 | NewCst = ConstantExpr::getUShr(CI, ShAmt); |
| 2528 | else |
| 2529 | NewCst = ConstantExpr::getShl(CI, ShAmt); |
Chris Lattner | 83c4ec0 | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 2530 | |
Chris Lattner | 648e3bc | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 2531 | // Check to see if we are shifting out any of the bits being |
| 2532 | // compared. |
| 2533 | if (ConstantExpr::get(Shift->getOpcode(), NewCst, ShAmt) != CI){ |
| 2534 | // If we shifted bits out, the fold is not going to work out. |
| 2535 | // As a special case, check to see if this means that the |
| 2536 | // result is always true or false now. |
| 2537 | if (I.getOpcode() == Instruction::SetEQ) |
| 2538 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 2539 | if (I.getOpcode() == Instruction::SetNE) |
| 2540 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 2541 | } else { |
| 2542 | I.setOperand(1, NewCst); |
Chris Lattner | 0cba71b | 2004-09-28 17:54:07 +0000 | [diff] [blame] | 2543 | Constant *NewAndCST; |
| 2544 | if (Shift->getOpcode() == Instruction::Shl) |
| 2545 | NewAndCST = ConstantExpr::getUShr(AndCST, ShAmt); |
| 2546 | else |
| 2547 | NewAndCST = ConstantExpr::getShl(AndCST, ShAmt); |
| 2548 | LHSI->setOperand(1, NewAndCST); |
Chris Lattner | 648e3bc | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 2549 | LHSI->setOperand(0, Shift->getOperand(0)); |
| 2550 | WorkList.push_back(Shift); // Shift is dead. |
| 2551 | AddUsesToWorkList(I); |
| 2552 | return &I; |
Chris Lattner | 5eb9194 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 2553 | } |
| 2554 | } |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2555 | } |
Chris Lattner | 648e3bc | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 2556 | } |
| 2557 | break; |
Chris Lattner | 83c4ec0 | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 2558 | |
Chris Lattner | 18d19ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 2559 | case Instruction::Shl: // (setcc (shl X, ShAmt), CI) |
| 2560 | if (ConstantUInt *ShAmt = dyn_cast<ConstantUInt>(LHSI->getOperand(1))) { |
| 2561 | switch (I.getOpcode()) { |
| 2562 | default: break; |
| 2563 | case Instruction::SetEQ: |
| 2564 | case Instruction::SetNE: { |
Chris Lattner | e17a128 | 2005-06-15 20:53:31 +0000 | [diff] [blame] | 2565 | unsigned TypeBits = CI->getType()->getPrimitiveSizeInBits(); |
| 2566 | |
| 2567 | // Check that the shift amount is in range. If not, don't perform |
| 2568 | // undefined shifts. When the shift is visited it will be |
| 2569 | // simplified. |
| 2570 | if (ShAmt->getValue() >= TypeBits) |
| 2571 | break; |
| 2572 | |
Chris Lattner | 18d19ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 2573 | // If we are comparing against bits always shifted out, the |
| 2574 | // comparison cannot succeed. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2575 | Constant *Comp = |
Chris Lattner | 18d19ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 2576 | ConstantExpr::getShl(ConstantExpr::getShr(CI, ShAmt), ShAmt); |
| 2577 | if (Comp != CI) {// Comparing against a bit that we know is zero. |
| 2578 | bool IsSetNE = I.getOpcode() == Instruction::SetNE; |
| 2579 | Constant *Cst = ConstantBool::get(IsSetNE); |
| 2580 | return ReplaceInstUsesWith(I, Cst); |
| 2581 | } |
| 2582 | |
| 2583 | if (LHSI->hasOneUse()) { |
| 2584 | // Otherwise strength reduce the shift into an and. |
Chris Lattner | 652f3cf | 2005-01-08 19:42:22 +0000 | [diff] [blame] | 2585 | unsigned ShAmtVal = (unsigned)ShAmt->getValue(); |
Chris Lattner | 18d19ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 2586 | uint64_t Val = (1ULL << (TypeBits-ShAmtVal))-1; |
| 2587 | |
| 2588 | Constant *Mask; |
| 2589 | if (CI->getType()->isUnsigned()) { |
| 2590 | Mask = ConstantUInt::get(CI->getType(), Val); |
| 2591 | } else if (ShAmtVal != 0) { |
| 2592 | Mask = ConstantSInt::get(CI->getType(), Val); |
| 2593 | } else { |
| 2594 | Mask = ConstantInt::getAllOnesValue(CI->getType()); |
| 2595 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2596 | |
Chris Lattner | 18d19ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 2597 | Instruction *AndI = |
| 2598 | BinaryOperator::createAnd(LHSI->getOperand(0), |
| 2599 | Mask, LHSI->getName()+".mask"); |
| 2600 | Value *And = InsertNewInstBefore(AndI, I); |
| 2601 | return new SetCondInst(I.getOpcode(), And, |
| 2602 | ConstantExpr::getUShr(CI, ShAmt)); |
| 2603 | } |
| 2604 | } |
| 2605 | } |
| 2606 | } |
| 2607 | break; |
| 2608 | |
Chris Lattner | 83c4ec0 | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 2609 | case Instruction::Shr: // (setcc (shr X, ShAmt), CI) |
Chris Lattner | f63f647 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 2610 | if (ConstantUInt *ShAmt = dyn_cast<ConstantUInt>(LHSI->getOperand(1))) { |
Chris Lattner | f63f647 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 2611 | switch (I.getOpcode()) { |
| 2612 | default: break; |
| 2613 | case Instruction::SetEQ: |
| 2614 | case Instruction::SetNE: { |
Chris Lattner | e17a128 | 2005-06-15 20:53:31 +0000 | [diff] [blame] | 2615 | |
| 2616 | // Check that the shift amount is in range. If not, don't perform |
| 2617 | // undefined shifts. When the shift is visited it will be |
| 2618 | // simplified. |
Chris Lattner | aa457ac | 2005-06-16 01:52:07 +0000 | [diff] [blame] | 2619 | unsigned TypeBits = CI->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | e17a128 | 2005-06-15 20:53:31 +0000 | [diff] [blame] | 2620 | if (ShAmt->getValue() >= TypeBits) |
| 2621 | break; |
| 2622 | |
Chris Lattner | f63f647 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 2623 | // If we are comparing against bits always shifted out, the |
| 2624 | // comparison cannot succeed. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2625 | Constant *Comp = |
Chris Lattner | f63f647 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 2626 | ConstantExpr::getShr(ConstantExpr::getShl(CI, ShAmt), ShAmt); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2627 | |
Chris Lattner | f63f647 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 2628 | if (Comp != CI) {// Comparing against a bit that we know is zero. |
| 2629 | bool IsSetNE = I.getOpcode() == Instruction::SetNE; |
| 2630 | Constant *Cst = ConstantBool::get(IsSetNE); |
| 2631 | return ReplaceInstUsesWith(I, Cst); |
| 2632 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2633 | |
Chris Lattner | f63f647 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 2634 | if (LHSI->hasOneUse() || CI->isNullValue()) { |
Chris Lattner | 652f3cf | 2005-01-08 19:42:22 +0000 | [diff] [blame] | 2635 | unsigned ShAmtVal = (unsigned)ShAmt->getValue(); |
Chris Lattner | 18d19ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 2636 | |
Chris Lattner | f63f647 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 2637 | // Otherwise strength reduce the shift into an and. |
| 2638 | uint64_t Val = ~0ULL; // All ones. |
| 2639 | Val <<= ShAmtVal; // Shift over to the right spot. |
| 2640 | |
| 2641 | Constant *Mask; |
| 2642 | if (CI->getType()->isUnsigned()) { |
Chris Lattner | f52d681 | 2005-04-24 17:46:05 +0000 | [diff] [blame] | 2643 | Val &= ~0ULL >> (64-TypeBits); |
Chris Lattner | f63f647 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 2644 | Mask = ConstantUInt::get(CI->getType(), Val); |
| 2645 | } else { |
| 2646 | Mask = ConstantSInt::get(CI->getType(), Val); |
| 2647 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2648 | |
Chris Lattner | f63f647 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 2649 | Instruction *AndI = |
| 2650 | BinaryOperator::createAnd(LHSI->getOperand(0), |
| 2651 | Mask, LHSI->getName()+".mask"); |
| 2652 | Value *And = InsertNewInstBefore(AndI, I); |
| 2653 | return new SetCondInst(I.getOpcode(), And, |
| 2654 | ConstantExpr::getShl(CI, ShAmt)); |
| 2655 | } |
| 2656 | break; |
| 2657 | } |
| 2658 | } |
| 2659 | } |
| 2660 | break; |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 2661 | |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2662 | case Instruction::Div: |
| 2663 | // Fold: (div X, C1) op C2 -> range check |
| 2664 | if (ConstantInt *DivRHS = dyn_cast<ConstantInt>(LHSI->getOperand(1))) { |
| 2665 | // Fold this div into the comparison, producing a range check. |
| 2666 | // Determine, based on the divide type, what the range is being |
| 2667 | // checked. If there is an overflow on the low or high side, remember |
| 2668 | // it, otherwise compute the range [low, hi) bounding the new value. |
| 2669 | bool LoOverflow = false, HiOverflow = 0; |
| 2670 | ConstantInt *LoBound = 0, *HiBound = 0; |
| 2671 | |
| 2672 | ConstantInt *Prod; |
| 2673 | bool ProdOV = MulWithOverflow(Prod, CI, DivRHS); |
| 2674 | |
Chris Lattner | 6a9fdfa | 2004-10-11 19:40:04 +0000 | [diff] [blame] | 2675 | Instruction::BinaryOps Opcode = I.getOpcode(); |
| 2676 | |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2677 | if (DivRHS->isNullValue()) { // Don't hack on divide by zeros. |
| 2678 | } else if (LHSI->getType()->isUnsigned()) { // udiv |
| 2679 | LoBound = Prod; |
| 2680 | LoOverflow = ProdOV; |
| 2681 | HiOverflow = ProdOV || AddWithOverflow(HiBound, LoBound, DivRHS); |
| 2682 | } else if (isPositive(DivRHS)) { // Divisor is > 0. |
| 2683 | if (CI->isNullValue()) { // (X / pos) op 0 |
| 2684 | // Can't overflow. |
| 2685 | LoBound = cast<ConstantInt>(ConstantExpr::getNeg(SubOne(DivRHS))); |
| 2686 | HiBound = DivRHS; |
| 2687 | } else if (isPositive(CI)) { // (X / pos) op pos |
| 2688 | LoBound = Prod; |
| 2689 | LoOverflow = ProdOV; |
| 2690 | HiOverflow = ProdOV || AddWithOverflow(HiBound, Prod, DivRHS); |
| 2691 | } else { // (X / pos) op neg |
| 2692 | Constant *DivRHSH = ConstantExpr::getNeg(SubOne(DivRHS)); |
| 2693 | LoOverflow = AddWithOverflow(LoBound, Prod, |
| 2694 | cast<ConstantInt>(DivRHSH)); |
| 2695 | HiBound = Prod; |
| 2696 | HiOverflow = ProdOV; |
| 2697 | } |
| 2698 | } else { // Divisor is < 0. |
| 2699 | if (CI->isNullValue()) { // (X / neg) op 0 |
| 2700 | LoBound = AddOne(DivRHS); |
| 2701 | HiBound = cast<ConstantInt>(ConstantExpr::getNeg(DivRHS)); |
| 2702 | } else if (isPositive(CI)) { // (X / neg) op pos |
| 2703 | HiOverflow = LoOverflow = ProdOV; |
| 2704 | if (!LoOverflow) |
| 2705 | LoOverflow = AddWithOverflow(LoBound, Prod, AddOne(DivRHS)); |
| 2706 | HiBound = AddOne(Prod); |
| 2707 | } else { // (X / neg) op neg |
| 2708 | LoBound = Prod; |
| 2709 | LoOverflow = HiOverflow = ProdOV; |
| 2710 | HiBound = cast<ConstantInt>(ConstantExpr::getSub(Prod, DivRHS)); |
| 2711 | } |
Chris Lattner | 340a05f | 2004-10-08 19:15:44 +0000 | [diff] [blame] | 2712 | |
Chris Lattner | 6a9fdfa | 2004-10-11 19:40:04 +0000 | [diff] [blame] | 2713 | // Dividing by a negate swaps the condition. |
| 2714 | Opcode = SetCondInst::getSwappedCondition(Opcode); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2715 | } |
| 2716 | |
| 2717 | if (LoBound) { |
| 2718 | Value *X = LHSI->getOperand(0); |
Chris Lattner | 6a9fdfa | 2004-10-11 19:40:04 +0000 | [diff] [blame] | 2719 | switch (Opcode) { |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2720 | default: assert(0 && "Unhandled setcc opcode!"); |
| 2721 | case Instruction::SetEQ: |
| 2722 | if (LoOverflow && HiOverflow) |
| 2723 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 2724 | else if (HiOverflow) |
| 2725 | return new SetCondInst(Instruction::SetGE, X, LoBound); |
| 2726 | else if (LoOverflow) |
| 2727 | return new SetCondInst(Instruction::SetLT, X, HiBound); |
| 2728 | else |
| 2729 | return InsertRangeTest(X, LoBound, HiBound, true, I); |
| 2730 | case Instruction::SetNE: |
| 2731 | if (LoOverflow && HiOverflow) |
| 2732 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 2733 | else if (HiOverflow) |
| 2734 | return new SetCondInst(Instruction::SetLT, X, LoBound); |
| 2735 | else if (LoOverflow) |
| 2736 | return new SetCondInst(Instruction::SetGE, X, HiBound); |
| 2737 | else |
| 2738 | return InsertRangeTest(X, LoBound, HiBound, false, I); |
| 2739 | case Instruction::SetLT: |
| 2740 | if (LoOverflow) |
| 2741 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 2742 | return new SetCondInst(Instruction::SetLT, X, LoBound); |
| 2743 | case Instruction::SetGT: |
| 2744 | if (HiOverflow) |
| 2745 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 2746 | return new SetCondInst(Instruction::SetGE, X, HiBound); |
| 2747 | } |
| 2748 | } |
| 2749 | } |
| 2750 | break; |
Chris Lattner | 648e3bc | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 2751 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2752 | |
Chris Lattner | bc5d414 | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 2753 | // Simplify seteq and setne instructions... |
| 2754 | if (I.getOpcode() == Instruction::SetEQ || |
| 2755 | I.getOpcode() == Instruction::SetNE) { |
| 2756 | bool isSetNE = I.getOpcode() == Instruction::SetNE; |
| 2757 | |
Chris Lattner | 00b1a7e | 2003-07-23 17:26:36 +0000 | [diff] [blame] | 2758 | // If the first operand is (and|or|xor) with a constant, and the second |
Chris Lattner | bc5d414 | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 2759 | // operand is a constant, simplify a bit. |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 2760 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0)) { |
| 2761 | switch (BO->getOpcode()) { |
Chris Lattner | 3571b72 | 2004-07-06 07:38:18 +0000 | [diff] [blame] | 2762 | case Instruction::Rem: |
| 2763 | // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one. |
| 2764 | if (CI->isNullValue() && isa<ConstantSInt>(BO->getOperand(1)) && |
| 2765 | BO->hasOneUse() && |
| 2766 | cast<ConstantSInt>(BO->getOperand(1))->getValue() > 1) |
| 2767 | if (unsigned L2 = |
| 2768 | Log2(cast<ConstantSInt>(BO->getOperand(1))->getValue())) { |
| 2769 | const Type *UTy = BO->getType()->getUnsignedVersion(); |
| 2770 | Value *NewX = InsertNewInstBefore(new CastInst(BO->getOperand(0), |
| 2771 | UTy, "tmp"), I); |
| 2772 | Constant *RHSCst = ConstantUInt::get(UTy, 1ULL << L2); |
| 2773 | Value *NewRem =InsertNewInstBefore(BinaryOperator::createRem(NewX, |
| 2774 | RHSCst, BO->getName()), I); |
| 2775 | return BinaryOperator::create(I.getOpcode(), NewRem, |
| 2776 | Constant::getNullValue(UTy)); |
| 2777 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2778 | break; |
Chris Lattner | 3571b72 | 2004-07-06 07:38:18 +0000 | [diff] [blame] | 2779 | |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 2780 | case Instruction::Add: |
Chris Lattner | 15d58b6 | 2004-06-27 22:51:36 +0000 | [diff] [blame] | 2781 | // Replace ((add A, B) != C) with (A != C-B) if B & C are constants. |
| 2782 | if (ConstantInt *BOp1C = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
Chris Lattner | 3d834bf | 2004-09-21 21:35:23 +0000 | [diff] [blame] | 2783 | if (BO->hasOneUse()) |
| 2784 | return new SetCondInst(I.getOpcode(), BO->getOperand(0), |
| 2785 | ConstantExpr::getSub(CI, BOp1C)); |
Chris Lattner | 15d58b6 | 2004-06-27 22:51:36 +0000 | [diff] [blame] | 2786 | } else if (CI->isNullValue()) { |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 2787 | // Replace ((add A, B) != 0) with (A != -B) if A or B is |
| 2788 | // efficiently invertible, or if the add has just this one use. |
| 2789 | Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2790 | |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 2791 | if (Value *NegVal = dyn_castNegVal(BOp1)) |
| 2792 | return new SetCondInst(I.getOpcode(), BOp0, NegVal); |
| 2793 | else if (Value *NegVal = dyn_castNegVal(BOp0)) |
| 2794 | return new SetCondInst(I.getOpcode(), NegVal, BOp1); |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 2795 | else if (BO->hasOneUse()) { |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 2796 | Instruction *Neg = BinaryOperator::createNeg(BOp1, BO->getName()); |
| 2797 | BO->setName(""); |
| 2798 | InsertNewInstBefore(Neg, I); |
| 2799 | return new SetCondInst(I.getOpcode(), BOp0, Neg); |
| 2800 | } |
| 2801 | } |
| 2802 | break; |
| 2803 | case Instruction::Xor: |
| 2804 | // For the xor case, we can xor two constants together, eliminating |
| 2805 | // the explicit xor. |
| 2806 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) |
| 2807 | return BinaryOperator::create(I.getOpcode(), BO->getOperand(0), |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2808 | ConstantExpr::getXor(CI, BOC)); |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 2809 | |
| 2810 | // FALLTHROUGH |
| 2811 | case Instruction::Sub: |
| 2812 | // Replace (([sub|xor] A, B) != 0) with (A != B) |
| 2813 | if (CI->isNullValue()) |
| 2814 | return new SetCondInst(I.getOpcode(), BO->getOperand(0), |
| 2815 | BO->getOperand(1)); |
| 2816 | break; |
| 2817 | |
| 2818 | case Instruction::Or: |
| 2819 | // If bits are being or'd in that are not present in the constant we |
| 2820 | // are comparing against, then the comparison could never succeed! |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2821 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) { |
Chris Lattner | 448c323 | 2004-06-10 02:12:35 +0000 | [diff] [blame] | 2822 | Constant *NotCI = ConstantExpr::getNot(CI); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2823 | if (!ConstantExpr::getAnd(BOC, NotCI)->isNullValue()) |
Chris Lattner | bc5d414 | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 2824 | return ReplaceInstUsesWith(I, ConstantBool::get(isSetNE)); |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2825 | } |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 2826 | break; |
| 2827 | |
| 2828 | case Instruction::And: |
| 2829 | if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
Chris Lattner | bc5d414 | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 2830 | // If bits are being compared against that are and'd out, then the |
| 2831 | // comparison can never succeed! |
Chris Lattner | 448c323 | 2004-06-10 02:12:35 +0000 | [diff] [blame] | 2832 | if (!ConstantExpr::getAnd(CI, |
| 2833 | ConstantExpr::getNot(BOC))->isNullValue()) |
Chris Lattner | bc5d414 | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 2834 | return ReplaceInstUsesWith(I, ConstantBool::get(isSetNE)); |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 2835 | |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2836 | // If we have ((X & C) == C), turn it into ((X & C) != 0). |
Chris Lattner | 3285a6f | 2004-06-10 02:33:20 +0000 | [diff] [blame] | 2837 | if (CI == BOC && isOneBitSet(CI)) |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2838 | return new SetCondInst(isSetNE ? Instruction::SetEQ : |
| 2839 | Instruction::SetNE, Op0, |
| 2840 | Constant::getNullValue(CI->getType())); |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2841 | |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 2842 | // Replace (and X, (1 << size(X)-1) != 0) with x < 0, converting X |
| 2843 | // to be a signed value as appropriate. |
| 2844 | if (isSignBit(BOC)) { |
| 2845 | Value *X = BO->getOperand(0); |
| 2846 | // If 'X' is not signed, insert a cast now... |
| 2847 | if (!BOC->getType()->isSigned()) { |
Chris Lattner | 5dd0402 | 2004-06-17 18:16:02 +0000 | [diff] [blame] | 2848 | const Type *DestTy = BOC->getType()->getSignedVersion(); |
Chris Lattner | 83c4ec0 | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 2849 | X = InsertCastBefore(X, DestTy, I); |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 2850 | } |
| 2851 | return new SetCondInst(isSetNE ? Instruction::SetLT : |
| 2852 | Instruction::SetGE, X, |
| 2853 | Constant::getNullValue(X->getType())); |
| 2854 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2855 | |
Chris Lattner | 83c4ec0 | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 2856 | // ((X & ~7) == 0) --> X < 8 |
Chris Lattner | b20ba0a | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2857 | if (CI->isNullValue() && isHighOnes(BOC)) { |
| 2858 | Value *X = BO->getOperand(0); |
Chris Lattner | 83c4ec0 | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 2859 | Constant *NegX = ConstantExpr::getNeg(BOC); |
Chris Lattner | b20ba0a | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2860 | |
| 2861 | // If 'X' is signed, insert a cast now. |
Chris Lattner | 83c4ec0 | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 2862 | if (NegX->getType()->isSigned()) { |
| 2863 | const Type *DestTy = NegX->getType()->getUnsignedVersion(); |
| 2864 | X = InsertCastBefore(X, DestTy, I); |
| 2865 | NegX = ConstantExpr::getCast(NegX, DestTy); |
Chris Lattner | b20ba0a | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2866 | } |
| 2867 | |
| 2868 | return new SetCondInst(isSetNE ? Instruction::SetGE : |
Chris Lattner | 83c4ec0 | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 2869 | Instruction::SetLT, X, NegX); |
Chris Lattner | b20ba0a | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2870 | } |
| 2871 | |
Chris Lattner | bc5d414 | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 2872 | } |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 2873 | default: break; |
| 2874 | } |
| 2875 | } |
Chris Lattner | c5943fb | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 2876 | } else { // Not a SetEQ/SetNE |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2877 | // If the LHS is a cast from an integral value of the same size, |
Chris Lattner | c5943fb | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 2878 | if (CastInst *Cast = dyn_cast<CastInst>(Op0)) { |
| 2879 | Value *CastOp = Cast->getOperand(0); |
| 2880 | const Type *SrcTy = CastOp->getType(); |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 2881 | unsigned SrcTySize = SrcTy->getPrimitiveSizeInBits(); |
Chris Lattner | c5943fb | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 2882 | if (SrcTy != Cast->getType() && SrcTy->isInteger() && |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 2883 | SrcTySize == Cast->getType()->getPrimitiveSizeInBits()) { |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2884 | assert((SrcTy->isSigned() ^ Cast->getType()->isSigned()) && |
Chris Lattner | c5943fb | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 2885 | "Source and destination signednesses should differ!"); |
| 2886 | if (Cast->getType()->isSigned()) { |
| 2887 | // If this is a signed comparison, check for comparisons in the |
| 2888 | // vicinity of zero. |
| 2889 | if (I.getOpcode() == Instruction::SetLT && CI->isNullValue()) |
| 2890 | // X < 0 => x > 127 |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2891 | return BinaryOperator::createSetGT(CastOp, |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 2892 | ConstantUInt::get(SrcTy, (1ULL << (SrcTySize-1))-1)); |
Chris Lattner | c5943fb | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 2893 | else if (I.getOpcode() == Instruction::SetGT && |
| 2894 | cast<ConstantSInt>(CI)->getValue() == -1) |
| 2895 | // X > -1 => x < 128 |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2896 | return BinaryOperator::createSetLT(CastOp, |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 2897 | ConstantUInt::get(SrcTy, 1ULL << (SrcTySize-1))); |
Chris Lattner | c5943fb | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 2898 | } else { |
| 2899 | ConstantUInt *CUI = cast<ConstantUInt>(CI); |
| 2900 | if (I.getOpcode() == Instruction::SetLT && |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 2901 | CUI->getValue() == 1ULL << (SrcTySize-1)) |
Chris Lattner | c5943fb | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 2902 | // X < 128 => X > -1 |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2903 | return BinaryOperator::createSetGT(CastOp, |
| 2904 | ConstantSInt::get(SrcTy, -1)); |
Chris Lattner | c5943fb | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 2905 | else if (I.getOpcode() == Instruction::SetGT && |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 2906 | CUI->getValue() == (1ULL << (SrcTySize-1))-1) |
Chris Lattner | c5943fb | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 2907 | // X > 127 => X < 0 |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2908 | return BinaryOperator::createSetLT(CastOp, |
| 2909 | Constant::getNullValue(SrcTy)); |
Chris Lattner | c5943fb | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 2910 | } |
| 2911 | } |
| 2912 | } |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 2913 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2914 | } |
| 2915 | |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 2916 | // Handle setcc with constant RHS's that can be integer, FP or pointer. |
| 2917 | if (Constant *RHSC = dyn_cast<Constant>(Op1)) { |
| 2918 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
| 2919 | switch (LHSI->getOpcode()) { |
Chris Lattner | 9fb25db | 2005-05-01 04:42:15 +0000 | [diff] [blame] | 2920 | case Instruction::GetElementPtr: |
| 2921 | if (RHSC->isNullValue()) { |
| 2922 | // Transform setcc GEP P, int 0, int 0, int 0, null -> setcc P, null |
| 2923 | bool isAllZeros = true; |
| 2924 | for (unsigned i = 1, e = LHSI->getNumOperands(); i != e; ++i) |
| 2925 | if (!isa<Constant>(LHSI->getOperand(i)) || |
| 2926 | !cast<Constant>(LHSI->getOperand(i))->isNullValue()) { |
| 2927 | isAllZeros = false; |
| 2928 | break; |
| 2929 | } |
| 2930 | if (isAllZeros) |
| 2931 | return new SetCondInst(I.getOpcode(), LHSI->getOperand(0), |
| 2932 | Constant::getNullValue(LHSI->getOperand(0)->getType())); |
| 2933 | } |
| 2934 | break; |
| 2935 | |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 2936 | case Instruction::PHI: |
| 2937 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2938 | return NV; |
| 2939 | break; |
| 2940 | case Instruction::Select: |
| 2941 | // If either operand of the select is a constant, we can fold the |
| 2942 | // comparison into the select arms, which will cause one to be |
| 2943 | // constant folded and the select turned into a bitwise or. |
| 2944 | Value *Op1 = 0, *Op2 = 0; |
| 2945 | if (LHSI->hasOneUse()) { |
| 2946 | if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) { |
| 2947 | // Fold the known value into the constant operand. |
| 2948 | Op1 = ConstantExpr::get(I.getOpcode(), C, RHSC); |
| 2949 | // Insert a new SetCC of the other select operand. |
| 2950 | Op2 = InsertNewInstBefore(new SetCondInst(I.getOpcode(), |
| 2951 | LHSI->getOperand(2), RHSC, |
| 2952 | I.getName()), I); |
| 2953 | } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) { |
| 2954 | // Fold the known value into the constant operand. |
| 2955 | Op2 = ConstantExpr::get(I.getOpcode(), C, RHSC); |
| 2956 | // Insert a new SetCC of the other select operand. |
| 2957 | Op1 = InsertNewInstBefore(new SetCondInst(I.getOpcode(), |
| 2958 | LHSI->getOperand(1), RHSC, |
| 2959 | I.getName()), I); |
| 2960 | } |
| 2961 | } |
Jeff Cohen | 9d80930 | 2005-04-23 21:38:35 +0000 | [diff] [blame] | 2962 | |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 2963 | if (Op1) |
| 2964 | return new SelectInst(LHSI->getOperand(0), Op1, Op2); |
| 2965 | break; |
| 2966 | } |
| 2967 | } |
| 2968 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 2969 | // If we can optimize a 'setcc GEP, P' or 'setcc P, GEP', do so now. |
| 2970 | if (User *GEP = dyn_castGetElementPtr(Op0)) |
| 2971 | if (Instruction *NI = FoldGEPSetCC(GEP, Op1, I.getOpcode(), I)) |
| 2972 | return NI; |
| 2973 | if (User *GEP = dyn_castGetElementPtr(Op1)) |
| 2974 | if (Instruction *NI = FoldGEPSetCC(GEP, Op0, |
| 2975 | SetCondInst::getSwappedCondition(I.getOpcode()), I)) |
| 2976 | return NI; |
| 2977 | |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 2978 | // Test to see if the operands of the setcc are casted versions of other |
| 2979 | // values. If the cast can be stripped off both arguments, we do so now. |
Chris Lattner | 6870805 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 2980 | if (CastInst *CI = dyn_cast<CastInst>(Op0)) { |
| 2981 | Value *CastOp0 = CI->getOperand(0); |
| 2982 | if (CastOp0->getType()->isLosslesslyConvertibleTo(CI->getType()) && |
Chris Lattner | 0cea42a | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 2983 | (isa<Constant>(Op1) || isa<CastInst>(Op1)) && |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 2984 | (I.getOpcode() == Instruction::SetEQ || |
| 2985 | I.getOpcode() == Instruction::SetNE)) { |
| 2986 | // We keep moving the cast from the left operand over to the right |
| 2987 | // operand, where it can often be eliminated completely. |
Chris Lattner | 6870805 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 2988 | Op0 = CastOp0; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2989 | |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 2990 | // If operand #1 is a cast instruction, see if we can eliminate it as |
| 2991 | // well. |
Chris Lattner | 6870805 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 2992 | if (CastInst *CI2 = dyn_cast<CastInst>(Op1)) |
| 2993 | if (CI2->getOperand(0)->getType()->isLosslesslyConvertibleTo( |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 2994 | Op0->getType())) |
Chris Lattner | 6870805 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 2995 | Op1 = CI2->getOperand(0); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2996 | |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 2997 | // If Op1 is a constant, we can fold the cast into the constant. |
| 2998 | if (Op1->getType() != Op0->getType()) |
| 2999 | if (Constant *Op1C = dyn_cast<Constant>(Op1)) { |
| 3000 | Op1 = ConstantExpr::getCast(Op1C, Op0->getType()); |
| 3001 | } else { |
| 3002 | // Otherwise, cast the RHS right before the setcc |
| 3003 | Op1 = new CastInst(Op1, Op0->getType(), Op1->getName()); |
| 3004 | InsertNewInstBefore(cast<Instruction>(Op1), I); |
| 3005 | } |
| 3006 | return BinaryOperator::create(I.getOpcode(), Op0, Op1); |
| 3007 | } |
| 3008 | |
Chris Lattner | 6870805 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 3009 | // Handle the special case of: setcc (cast bool to X), <cst> |
| 3010 | // This comes up when you have code like |
| 3011 | // int X = A < B; |
| 3012 | // if (X) ... |
| 3013 | // For generality, we handle any zero-extension of any operand comparison |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3014 | // with a constant or another cast from the same type. |
| 3015 | if (isa<ConstantInt>(Op1) || isa<CastInst>(Op1)) |
| 3016 | if (Instruction *R = visitSetCondInstWithCastAndCast(I)) |
| 3017 | return R; |
Chris Lattner | 6870805 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 3018 | } |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3019 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3020 | } |
| 3021 | |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3022 | // visitSetCondInstWithCastAndCast - Handle setcond (cast x to y), (cast/cst). |
| 3023 | // We only handle extending casts so far. |
| 3024 | // |
| 3025 | Instruction *InstCombiner::visitSetCondInstWithCastAndCast(SetCondInst &SCI) { |
| 3026 | Value *LHSCIOp = cast<CastInst>(SCI.getOperand(0))->getOperand(0); |
| 3027 | const Type *SrcTy = LHSCIOp->getType(); |
| 3028 | const Type *DestTy = SCI.getOperand(0)->getType(); |
| 3029 | Value *RHSCIOp; |
| 3030 | |
| 3031 | if (!DestTy->isIntegral() || !SrcTy->isIntegral()) |
Chris Lattner | b352fa5 | 2005-01-17 03:20:02 +0000 | [diff] [blame] | 3032 | return 0; |
| 3033 | |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3034 | unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); |
| 3035 | unsigned DestBits = DestTy->getPrimitiveSizeInBits(); |
| 3036 | if (SrcBits >= DestBits) return 0; // Only handle extending cast. |
| 3037 | |
| 3038 | // Is this a sign or zero extension? |
| 3039 | bool isSignSrc = SrcTy->isSigned(); |
| 3040 | bool isSignDest = DestTy->isSigned(); |
| 3041 | |
| 3042 | if (CastInst *CI = dyn_cast<CastInst>(SCI.getOperand(1))) { |
| 3043 | // Not an extension from the same type? |
| 3044 | RHSCIOp = CI->getOperand(0); |
| 3045 | if (RHSCIOp->getType() != LHSCIOp->getType()) return 0; |
| 3046 | } else if (ConstantInt *CI = dyn_cast<ConstantInt>(SCI.getOperand(1))) { |
| 3047 | // Compute the constant that would happen if we truncated to SrcTy then |
| 3048 | // reextended to DestTy. |
| 3049 | Constant *Res = ConstantExpr::getCast(CI, SrcTy); |
| 3050 | |
| 3051 | if (ConstantExpr::getCast(Res, DestTy) == CI) { |
| 3052 | RHSCIOp = Res; |
| 3053 | } else { |
| 3054 | // If the value cannot be represented in the shorter type, we cannot emit |
| 3055 | // a simple comparison. |
| 3056 | if (SCI.getOpcode() == Instruction::SetEQ) |
| 3057 | return ReplaceInstUsesWith(SCI, ConstantBool::False); |
| 3058 | if (SCI.getOpcode() == Instruction::SetNE) |
| 3059 | return ReplaceInstUsesWith(SCI, ConstantBool::True); |
| 3060 | |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3061 | // Evaluate the comparison for LT. |
| 3062 | Value *Result; |
| 3063 | if (DestTy->isSigned()) { |
| 3064 | // We're performing a signed comparison. |
| 3065 | if (isSignSrc) { |
| 3066 | // Signed extend and signed comparison. |
| 3067 | if (cast<ConstantSInt>(CI)->getValue() < 0) // X < (small) --> false |
| 3068 | Result = ConstantBool::False; |
| 3069 | else |
| 3070 | Result = ConstantBool::True; // X < (large) --> true |
| 3071 | } else { |
| 3072 | // Unsigned extend and signed comparison. |
| 3073 | if (cast<ConstantSInt>(CI)->getValue() < 0) |
| 3074 | Result = ConstantBool::False; |
| 3075 | else |
| 3076 | Result = ConstantBool::True; |
| 3077 | } |
| 3078 | } else { |
| 3079 | // We're performing an unsigned comparison. |
| 3080 | if (!isSignSrc) { |
| 3081 | // Unsigned extend & compare -> always true. |
| 3082 | Result = ConstantBool::True; |
| 3083 | } else { |
| 3084 | // We're performing an unsigned comp with a sign extended value. |
| 3085 | // This is true if the input is >= 0. [aka >s -1] |
| 3086 | Constant *NegOne = ConstantIntegral::getAllOnesValue(SrcTy); |
| 3087 | Result = InsertNewInstBefore(BinaryOperator::createSetGT(LHSCIOp, |
| 3088 | NegOne, SCI.getName()), SCI); |
| 3089 | } |
Reid Spencer | 6731d5c | 2004-11-28 21:31:15 +0000 | [diff] [blame] | 3090 | } |
Chris Lattner | b352fa5 | 2005-01-17 03:20:02 +0000 | [diff] [blame] | 3091 | |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3092 | // Finally, return the value computed. |
| 3093 | if (SCI.getOpcode() == Instruction::SetLT) { |
| 3094 | return ReplaceInstUsesWith(SCI, Result); |
| 3095 | } else { |
| 3096 | assert(SCI.getOpcode()==Instruction::SetGT &&"SetCC should be folded!"); |
| 3097 | if (Constant *CI = dyn_cast<Constant>(Result)) |
| 3098 | return ReplaceInstUsesWith(SCI, ConstantExpr::getNot(CI)); |
| 3099 | else |
| 3100 | return BinaryOperator::createNot(Result); |
| 3101 | } |
Chris Lattner | b352fa5 | 2005-01-17 03:20:02 +0000 | [diff] [blame] | 3102 | } |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3103 | } else { |
| 3104 | return 0; |
Reid Spencer | 6731d5c | 2004-11-28 21:31:15 +0000 | [diff] [blame] | 3105 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3106 | |
Chris Lattner | 8d7089e | 2005-06-16 03:00:08 +0000 | [diff] [blame] | 3107 | // Okay, just insert a compare of the reduced operands now! |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3108 | return BinaryOperator::create(SCI.getOpcode(), LHSCIOp, RHSCIOp); |
| 3109 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3110 | |
Chris Lattner | ea34005 | 2003-03-10 19:16:08 +0000 | [diff] [blame] | 3111 | Instruction *InstCombiner::visitShiftInst(ShiftInst &I) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3112 | assert(I.getOperand(1)->getType() == Type::UByteTy); |
| 3113 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 3114 | bool isLeftShift = I.getOpcode() == Instruction::Shl; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3115 | |
| 3116 | // shl X, 0 == X and shr X, 0 == X |
| 3117 | // shl 0, X == 0 and shr 0, X == 0 |
| 3118 | if (Op1 == Constant::getNullValue(Type::UByteTy) || |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 3119 | Op0 == Constant::getNullValue(Op0->getType())) |
| 3120 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3121 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3122 | if (isa<UndefValue>(Op0)) { // undef >>s X -> undef |
| 3123 | if (!isLeftShift && I.getType()->isSigned()) |
Chris Lattner | 79a564c | 2004-10-16 23:28:04 +0000 | [diff] [blame] | 3124 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3125 | else // undef << X -> 0 AND undef >>u X -> 0 |
| 3126 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 3127 | } |
| 3128 | if (isa<UndefValue>(Op1)) { |
| 3129 | if (isLeftShift || I.getType()->isUnsigned()) |
| 3130 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 3131 | else |
| 3132 | return ReplaceInstUsesWith(I, Op0); // X >>s undef -> X |
| 3133 | } |
| 3134 | |
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 3135 | // shr int -1, X = -1 (for any arithmetic shift rights of ~0) |
| 3136 | if (!isLeftShift) |
| 3137 | if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(Op0)) |
| 3138 | if (CSI->isAllOnesValue()) |
| 3139 | return ReplaceInstUsesWith(I, CSI); |
| 3140 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3141 | // Try to fold constant and into select arguments. |
| 3142 | if (isa<Constant>(Op0)) |
| 3143 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3144 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3145 | return R; |
| 3146 | |
Chris Lattner | 120347e | 2005-05-08 17:34:56 +0000 | [diff] [blame] | 3147 | // See if we can turn a signed shr into an unsigned shr. |
| 3148 | if (!isLeftShift && I.getType()->isSigned()) { |
| 3149 | if (MaskedValueIsZero(Op0, ConstantInt::getMinValue(I.getType()))) { |
| 3150 | Value *V = InsertCastBefore(Op0, I.getType()->getUnsignedVersion(), I); |
| 3151 | V = InsertNewInstBefore(new ShiftInst(Instruction::Shr, V, Op1, |
| 3152 | I.getName()), I); |
| 3153 | return new CastInst(V, I.getType()); |
| 3154 | } |
| 3155 | } |
| 3156 | |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3157 | if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(Op1)) { |
Chris Lattner | 08fd7ab | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 3158 | // shl uint X, 32 = 0 and shr ubyte Y, 9 = 0, ... just don't eliminate shr |
| 3159 | // of a signed value. |
| 3160 | // |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3161 | unsigned TypeBits = Op0->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | 8adac75 | 2004-02-23 20:30:06 +0000 | [diff] [blame] | 3162 | if (CUI->getValue() >= TypeBits) { |
| 3163 | if (!Op0->getType()->isSigned() || isLeftShift) |
| 3164 | return ReplaceInstUsesWith(I, Constant::getNullValue(Op0->getType())); |
| 3165 | else { |
| 3166 | I.setOperand(1, ConstantUInt::get(Type::UByteTy, TypeBits-1)); |
| 3167 | return &I; |
| 3168 | } |
| 3169 | } |
Chris Lattner | f283608 | 2002-09-10 23:04:09 +0000 | [diff] [blame] | 3170 | |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 3171 | // ((X*C1) << C2) == (X * (C1 << C2)) |
| 3172 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0)) |
| 3173 | if (BO->getOpcode() == Instruction::Mul && isLeftShift) |
| 3174 | if (Constant *BOOp = dyn_cast<Constant>(BO->getOperand(1))) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3175 | return BinaryOperator::createMul(BO->getOperand(0), |
| 3176 | ConstantExpr::getShl(BOOp, CUI)); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3177 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3178 | // Try to fold constant and into select arguments. |
| 3179 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3180 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3181 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 3182 | if (isa<PHINode>(Op0)) |
| 3183 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 3184 | return NV; |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 3185 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3186 | if (Op0->hasOneUse()) { |
| 3187 | // If this is a SHL of a sign-extending cast, see if we can turn the input |
| 3188 | // into a zero extending cast (a simple strength reduction). |
| 3189 | if (CastInst *CI = dyn_cast<CastInst>(Op0)) { |
| 3190 | const Type *SrcTy = CI->getOperand(0)->getType(); |
| 3191 | if (isLeftShift && SrcTy->isInteger() && SrcTy->isSigned() && |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3192 | SrcTy->getPrimitiveSizeInBits() < |
| 3193 | CI->getType()->getPrimitiveSizeInBits()) { |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3194 | // We can change it to a zero extension if we are shifting out all of |
| 3195 | // the sign extended bits. To check this, form a mask of all of the |
| 3196 | // sign extend bits, then shift them left and see if we have anything |
| 3197 | // left. |
| 3198 | Constant *Mask = ConstantIntegral::getAllOnesValue(SrcTy); // 1111 |
| 3199 | Mask = ConstantExpr::getZeroExtend(Mask, CI->getType()); // 00001111 |
| 3200 | Mask = ConstantExpr::getNot(Mask); // 1's in the sign bits: 11110000 |
| 3201 | if (ConstantExpr::getShl(Mask, CUI)->isNullValue()) { |
| 3202 | // If the shift is nuking all of the sign bits, change this to a |
| 3203 | // zero extension cast. To do this, cast the cast input to |
| 3204 | // unsigned, then to the requested size. |
| 3205 | Value *CastOp = CI->getOperand(0); |
| 3206 | Instruction *NC = |
| 3207 | new CastInst(CastOp, CastOp->getType()->getUnsignedVersion(), |
| 3208 | CI->getName()+".uns"); |
| 3209 | NC = InsertNewInstBefore(NC, I); |
| 3210 | // Finally, insert a replacement for CI. |
| 3211 | NC = new CastInst(NC, CI->getType(), CI->getName()); |
| 3212 | CI->setName(""); |
| 3213 | NC = InsertNewInstBefore(NC, I); |
| 3214 | WorkList.push_back(CI); // Delete CI later. |
| 3215 | I.setOperand(0, NC); |
| 3216 | return &I; // The SHL operand was modified. |
| 3217 | } |
| 3218 | } |
| 3219 | } |
| 3220 | |
| 3221 | // If the operand is an bitwise operator with a constant RHS, and the |
| 3222 | // shift is the only use, we can pull it out of the shift. |
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 3223 | if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0)) |
| 3224 | if (ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand(1))) { |
| 3225 | bool isValid = true; // Valid only for And, Or, Xor |
| 3226 | bool highBitSet = false; // Transform if high bit of constant set? |
| 3227 | |
| 3228 | switch (Op0BO->getOpcode()) { |
| 3229 | default: isValid = false; break; // Do not perform transform! |
Chris Lattner | 1f7e160 | 2004-10-08 03:46:20 +0000 | [diff] [blame] | 3230 | case Instruction::Add: |
| 3231 | isValid = isLeftShift; |
| 3232 | break; |
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 3233 | case Instruction::Or: |
| 3234 | case Instruction::Xor: |
| 3235 | highBitSet = false; |
| 3236 | break; |
| 3237 | case Instruction::And: |
| 3238 | highBitSet = true; |
| 3239 | break; |
| 3240 | } |
| 3241 | |
| 3242 | // If this is a signed shift right, and the high bit is modified |
| 3243 | // by the logical operation, do not perform the transformation. |
| 3244 | // The highBitSet boolean indicates the value of the high bit of |
| 3245 | // the constant which would cause it to be modified for this |
| 3246 | // operation. |
| 3247 | // |
| 3248 | if (isValid && !isLeftShift && !I.getType()->isUnsigned()) { |
| 3249 | uint64_t Val = Op0C->getRawValue(); |
| 3250 | isValid = ((Val & (1 << (TypeBits-1))) != 0) == highBitSet; |
| 3251 | } |
| 3252 | |
| 3253 | if (isValid) { |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3254 | Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, CUI); |
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 3255 | |
| 3256 | Instruction *NewShift = |
| 3257 | new ShiftInst(I.getOpcode(), Op0BO->getOperand(0), CUI, |
| 3258 | Op0BO->getName()); |
| 3259 | Op0BO->setName(""); |
| 3260 | InsertNewInstBefore(NewShift, I); |
| 3261 | |
| 3262 | return BinaryOperator::create(Op0BO->getOpcode(), NewShift, |
| 3263 | NewRHS); |
| 3264 | } |
| 3265 | } |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3266 | } |
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 3267 | |
Chris Lattner | 08fd7ab | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 3268 | // If this is a shift of a shift, see if we can fold the two together... |
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 3269 | if (ShiftInst *Op0SI = dyn_cast<ShiftInst>(Op0)) |
Chris Lattner | 943c713 | 2003-07-24 18:38:56 +0000 | [diff] [blame] | 3270 | if (ConstantUInt *ShiftAmt1C = |
| 3271 | dyn_cast<ConstantUInt>(Op0SI->getOperand(1))) { |
Chris Lattner | 652f3cf | 2005-01-08 19:42:22 +0000 | [diff] [blame] | 3272 | unsigned ShiftAmt1 = (unsigned)ShiftAmt1C->getValue(); |
| 3273 | unsigned ShiftAmt2 = (unsigned)CUI->getValue(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3274 | |
Chris Lattner | 08fd7ab | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 3275 | // Check for (A << c1) << c2 and (A >> c1) >> c2 |
| 3276 | if (I.getOpcode() == Op0SI->getOpcode()) { |
| 3277 | unsigned Amt = ShiftAmt1+ShiftAmt2; // Fold into one big shift... |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3278 | if (Op0->getType()->getPrimitiveSizeInBits() < Amt) |
| 3279 | Amt = Op0->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | 08fd7ab | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 3280 | return new ShiftInst(I.getOpcode(), Op0SI->getOperand(0), |
| 3281 | ConstantUInt::get(Type::UByteTy, Amt)); |
| 3282 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3283 | |
Chris Lattner | 943c713 | 2003-07-24 18:38:56 +0000 | [diff] [blame] | 3284 | // Check for (A << c1) >> c2 or visaversa. If we are dealing with |
| 3285 | // signed types, we can only support the (A >> c1) << c2 configuration, |
| 3286 | // because it can not turn an arbitrary bit of A into a sign bit. |
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 3287 | if (I.getType()->isUnsigned() || isLeftShift) { |
Chris Lattner | 08fd7ab | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 3288 | // Calculate bitmask for what gets shifted off the edge... |
| 3289 | Constant *C = ConstantIntegral::getAllOnesValue(I.getType()); |
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 3290 | if (isLeftShift) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3291 | C = ConstantExpr::getShl(C, ShiftAmt1C); |
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 3292 | else |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3293 | C = ConstantExpr::getShr(C, ShiftAmt1C); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3294 | |
Chris Lattner | 08fd7ab | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 3295 | Instruction *Mask = |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3296 | BinaryOperator::createAnd(Op0SI->getOperand(0), C, |
| 3297 | Op0SI->getOperand(0)->getName()+".mask"); |
Chris Lattner | 08fd7ab | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 3298 | InsertNewInstBefore(Mask, I); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3299 | |
Chris Lattner | 08fd7ab | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 3300 | // Figure out what flavor of shift we should use... |
| 3301 | if (ShiftAmt1 == ShiftAmt2) |
| 3302 | return ReplaceInstUsesWith(I, Mask); // (A << c) >> c === A & c2 |
| 3303 | else if (ShiftAmt1 < ShiftAmt2) { |
| 3304 | return new ShiftInst(I.getOpcode(), Mask, |
| 3305 | ConstantUInt::get(Type::UByteTy, ShiftAmt2-ShiftAmt1)); |
| 3306 | } else { |
| 3307 | return new ShiftInst(Op0SI->getOpcode(), Mask, |
| 3308 | ConstantUInt::get(Type::UByteTy, ShiftAmt1-ShiftAmt2)); |
| 3309 | } |
| 3310 | } |
| 3311 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3312 | } |
Chris Lattner | 6eaeb57 | 2002-10-08 16:16:40 +0000 | [diff] [blame] | 3313 | |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3314 | return 0; |
| 3315 | } |
| 3316 | |
Chris Lattner | bee7e76 | 2004-07-20 00:59:32 +0000 | [diff] [blame] | 3317 | enum CastType { |
| 3318 | Noop = 0, |
| 3319 | Truncate = 1, |
| 3320 | Signext = 2, |
| 3321 | Zeroext = 3 |
| 3322 | }; |
| 3323 | |
| 3324 | /// getCastType - In the future, we will split the cast instruction into these |
| 3325 | /// various types. Until then, we have to do the analysis here. |
| 3326 | static CastType getCastType(const Type *Src, const Type *Dest) { |
| 3327 | assert(Src->isIntegral() && Dest->isIntegral() && |
| 3328 | "Only works on integral types!"); |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3329 | unsigned SrcSize = Src->getPrimitiveSizeInBits(); |
| 3330 | unsigned DestSize = Dest->getPrimitiveSizeInBits(); |
Chris Lattner | bee7e76 | 2004-07-20 00:59:32 +0000 | [diff] [blame] | 3331 | |
| 3332 | if (SrcSize == DestSize) return Noop; |
| 3333 | if (SrcSize > DestSize) return Truncate; |
| 3334 | if (Src->isSigned()) return Signext; |
| 3335 | return Zeroext; |
| 3336 | } |
| 3337 | |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3338 | |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 3339 | // isEliminableCastOfCast - Return true if it is valid to eliminate the CI |
| 3340 | // instruction. |
| 3341 | // |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 3342 | static inline bool isEliminableCastOfCast(const Type *SrcTy, const Type *MidTy, |
Chris Lattner | 59a2077 | 2004-07-20 05:21:00 +0000 | [diff] [blame] | 3343 | const Type *DstTy, TargetData *TD) { |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 3344 | |
Chris Lattner | 8fd217c | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 3345 | // It is legal to eliminate the instruction if casting A->B->A if the sizes |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3346 | // are identical and the bits don't get reinterpreted (for example |
Chris Lattner | 5eb9194 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 3347 | // int->float->int would not be allowed). |
Misha Brukman | f117cc9 | 2003-05-20 18:45:36 +0000 | [diff] [blame] | 3348 | if (SrcTy == DstTy && SrcTy->isLosslesslyConvertibleTo(MidTy)) |
Chris Lattner | 8fd217c | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 3349 | return true; |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 3350 | |
Chris Lattner | e8a7e59 | 2004-07-21 04:27:24 +0000 | [diff] [blame] | 3351 | // If we are casting between pointer and integer types, treat pointers as |
| 3352 | // integers of the appropriate size for the code below. |
| 3353 | if (isa<PointerType>(SrcTy)) SrcTy = TD->getIntPtrType(); |
| 3354 | if (isa<PointerType>(MidTy)) MidTy = TD->getIntPtrType(); |
| 3355 | if (isa<PointerType>(DstTy)) DstTy = TD->getIntPtrType(); |
Chris Lattner | 59a2077 | 2004-07-20 05:21:00 +0000 | [diff] [blame] | 3356 | |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 3357 | // Allow free casting and conversion of sizes as long as the sign doesn't |
| 3358 | // change... |
Chris Lattner | 0c4e886 | 2002-09-03 01:08:28 +0000 | [diff] [blame] | 3359 | if (SrcTy->isIntegral() && MidTy->isIntegral() && DstTy->isIntegral()) { |
Chris Lattner | bee7e76 | 2004-07-20 00:59:32 +0000 | [diff] [blame] | 3360 | CastType FirstCast = getCastType(SrcTy, MidTy); |
| 3361 | CastType SecondCast = getCastType(MidTy, DstTy); |
Chris Lattner | 8fd217c | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 3362 | |
Chris Lattner | bee7e76 | 2004-07-20 00:59:32 +0000 | [diff] [blame] | 3363 | // Capture the effect of these two casts. If the result is a legal cast, |
| 3364 | // the CastType is stored here, otherwise a special code is used. |
| 3365 | static const unsigned CastResult[] = { |
| 3366 | // First cast is noop |
| 3367 | 0, 1, 2, 3, |
| 3368 | // First cast is a truncate |
| 3369 | 1, 1, 4, 4, // trunc->extend is not safe to eliminate |
| 3370 | // First cast is a sign ext |
Chris Lattner | 5eb9194 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 3371 | 2, 5, 2, 4, // signext->zeroext never ok |
Chris Lattner | bee7e76 | 2004-07-20 00:59:32 +0000 | [diff] [blame] | 3372 | // First cast is a zero ext |
Chris Lattner | 5eb9194 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 3373 | 3, 5, 3, 3, |
Chris Lattner | bee7e76 | 2004-07-20 00:59:32 +0000 | [diff] [blame] | 3374 | }; |
| 3375 | |
| 3376 | unsigned Result = CastResult[FirstCast*4+SecondCast]; |
| 3377 | switch (Result) { |
| 3378 | default: assert(0 && "Illegal table value!"); |
| 3379 | case 0: |
| 3380 | case 1: |
| 3381 | case 2: |
| 3382 | case 3: |
| 3383 | // FIXME: in the future, when LLVM has explicit sign/zeroextends and |
| 3384 | // truncates, we could eliminate more casts. |
| 3385 | return (unsigned)getCastType(SrcTy, DstTy) == Result; |
| 3386 | case 4: |
| 3387 | return false; // Not possible to eliminate this here. |
| 3388 | case 5: |
Chris Lattner | 5eb9194 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 3389 | // Sign or zero extend followed by truncate is always ok if the result |
| 3390 | // is a truncate or noop. |
| 3391 | CastType ResultCast = getCastType(SrcTy, DstTy); |
| 3392 | if (ResultCast == Noop || ResultCast == Truncate) |
| 3393 | return true; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3394 | // Otherwise we are still growing the value, we are only safe if the |
Chris Lattner | 5eb9194 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 3395 | // result will match the sign/zeroextendness of the result. |
| 3396 | return ResultCast == FirstCast; |
Chris Lattner | 3ecce66 | 2002-08-15 16:15:25 +0000 | [diff] [blame] | 3397 | } |
Chris Lattner | 8fd217c | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 3398 | } |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 3399 | return false; |
| 3400 | } |
| 3401 | |
Chris Lattner | 59a2077 | 2004-07-20 05:21:00 +0000 | [diff] [blame] | 3402 | static bool ValueRequiresCast(const Value *V, const Type *Ty, TargetData *TD) { |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 3403 | if (V->getType() == Ty || isa<Constant>(V)) return false; |
| 3404 | if (const CastInst *CI = dyn_cast<CastInst>(V)) |
Chris Lattner | 59a2077 | 2004-07-20 05:21:00 +0000 | [diff] [blame] | 3405 | if (isEliminableCastOfCast(CI->getOperand(0)->getType(), CI->getType(), Ty, |
| 3406 | TD)) |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 3407 | return false; |
| 3408 | return true; |
| 3409 | } |
| 3410 | |
| 3411 | /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the |
| 3412 | /// InsertBefore instruction. This is specialized a bit to avoid inserting |
| 3413 | /// casts that are known to not do anything... |
| 3414 | /// |
| 3415 | Value *InstCombiner::InsertOperandCastBefore(Value *V, const Type *DestTy, |
| 3416 | Instruction *InsertBefore) { |
| 3417 | if (V->getType() == DestTy) return V; |
| 3418 | if (Constant *C = dyn_cast<Constant>(V)) |
| 3419 | return ConstantExpr::getCast(C, DestTy); |
| 3420 | |
| 3421 | CastInst *CI = new CastInst(V, DestTy, V->getName()); |
| 3422 | InsertNewInstBefore(CI, *InsertBefore); |
| 3423 | return CI; |
| 3424 | } |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 3425 | |
| 3426 | // CastInst simplification |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 3427 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3428 | Instruction *InstCombiner::visitCastInst(CastInst &CI) { |
Chris Lattner | 79d35b3 | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 3429 | Value *Src = CI.getOperand(0); |
| 3430 | |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 3431 | // If the user is casting a value to the same type, eliminate this cast |
| 3432 | // instruction... |
Chris Lattner | 79d35b3 | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 3433 | if (CI.getType() == Src->getType()) |
| 3434 | return ReplaceInstUsesWith(CI, Src); |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 3435 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3436 | if (isa<UndefValue>(Src)) // cast undef -> undef |
| 3437 | return ReplaceInstUsesWith(CI, UndefValue::get(CI.getType())); |
| 3438 | |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 3439 | // If casting the result of another cast instruction, try to eliminate this |
| 3440 | // one! |
| 3441 | // |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3442 | if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast |
| 3443 | Value *A = CSrc->getOperand(0); |
| 3444 | if (isEliminableCastOfCast(A->getType(), CSrc->getType(), |
| 3445 | CI.getType(), TD)) { |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 3446 | // This instruction now refers directly to the cast's src operand. This |
| 3447 | // has a good chance of making CSrc dead. |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3448 | CI.setOperand(0, CSrc->getOperand(0)); |
| 3449 | return &CI; |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 3450 | } |
| 3451 | |
Chris Lattner | 8fd217c | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 3452 | // If this is an A->B->A cast, and we are dealing with integral types, try |
| 3453 | // to convert this into a logical 'and' instruction. |
| 3454 | // |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3455 | if (A->getType()->isInteger() && |
Chris Lattner | 0c4e886 | 2002-09-03 01:08:28 +0000 | [diff] [blame] | 3456 | CI.getType()->isInteger() && CSrc->getType()->isInteger() && |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3457 | CSrc->getType()->isUnsigned() && // B->A cast must zero extend |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3458 | CSrc->getType()->getPrimitiveSizeInBits() < |
| 3459 | CI.getType()->getPrimitiveSizeInBits()&& |
| 3460 | A->getType()->getPrimitiveSizeInBits() == |
| 3461 | CI.getType()->getPrimitiveSizeInBits()) { |
Chris Lattner | 8fd217c | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 3462 | assert(CSrc->getType() != Type::ULongTy && |
| 3463 | "Cannot have type bigger than ulong!"); |
Chris Lattner | f52d681 | 2005-04-24 17:46:05 +0000 | [diff] [blame] | 3464 | uint64_t AndValue = ~0ULL>>(64-CSrc->getType()->getPrimitiveSizeInBits()); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3465 | Constant *AndOp = ConstantUInt::get(A->getType()->getUnsignedVersion(), |
| 3466 | AndValue); |
| 3467 | AndOp = ConstantExpr::getCast(AndOp, A->getType()); |
| 3468 | Instruction *And = BinaryOperator::createAnd(CSrc->getOperand(0), AndOp); |
| 3469 | if (And->getType() != CI.getType()) { |
| 3470 | And->setName(CSrc->getName()+".mask"); |
| 3471 | InsertNewInstBefore(And, CI); |
| 3472 | And = new CastInst(And, CI.getType()); |
| 3473 | } |
| 3474 | return And; |
Chris Lattner | 8fd217c | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 3475 | } |
| 3476 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3477 | |
Chris Lattner | a710ddc | 2004-05-25 04:29:21 +0000 | [diff] [blame] | 3478 | // If this is a cast to bool, turn it into the appropriate setne instruction. |
| 3479 | if (CI.getType() == Type::BoolTy) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3480 | return BinaryOperator::createSetNE(CI.getOperand(0), |
Chris Lattner | a710ddc | 2004-05-25 04:29:21 +0000 | [diff] [blame] | 3481 | Constant::getNullValue(CI.getOperand(0)->getType())); |
| 3482 | |
Chris Lattner | 797249b | 2003-06-21 23:12:02 +0000 | [diff] [blame] | 3483 | // If casting the result of a getelementptr instruction with no offset, turn |
| 3484 | // this into a cast of the original pointer! |
| 3485 | // |
Chris Lattner | 79d35b3 | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 3486 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Src)) { |
Chris Lattner | 797249b | 2003-06-21 23:12:02 +0000 | [diff] [blame] | 3487 | bool AllZeroOperands = true; |
| 3488 | for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i) |
| 3489 | if (!isa<Constant>(GEP->getOperand(i)) || |
| 3490 | !cast<Constant>(GEP->getOperand(i))->isNullValue()) { |
| 3491 | AllZeroOperands = false; |
| 3492 | break; |
| 3493 | } |
| 3494 | if (AllZeroOperands) { |
| 3495 | CI.setOperand(0, GEP->getOperand(0)); |
| 3496 | return &CI; |
| 3497 | } |
| 3498 | } |
| 3499 | |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 3500 | // If we are casting a malloc or alloca to a pointer to a type of the same |
| 3501 | // size, rewrite the allocation instruction to allocate the "right" type. |
| 3502 | // |
| 3503 | if (AllocationInst *AI = dyn_cast<AllocationInst>(Src)) |
Chris Lattner | fc07a34 | 2003-11-02 06:54:48 +0000 | [diff] [blame] | 3504 | if (AI->hasOneUse() && !AI->isArrayAllocation()) |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 3505 | if (const PointerType *PTy = dyn_cast<PointerType>(CI.getType())) { |
| 3506 | // Get the type really allocated and the type casted to... |
| 3507 | const Type *AllocElTy = AI->getAllocatedType(); |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 3508 | const Type *CastElTy = PTy->getElementType(); |
Chris Lattner | fae1010 | 2004-07-06 19:28:42 +0000 | [diff] [blame] | 3509 | if (AllocElTy->isSized() && CastElTy->isSized()) { |
Chris Lattner | 652f3cf | 2005-01-08 19:42:22 +0000 | [diff] [blame] | 3510 | uint64_t AllocElTySize = TD->getTypeSize(AllocElTy); |
| 3511 | uint64_t CastElTySize = TD->getTypeSize(CastElTy); |
Chris Lattner | 1bcc70d | 2003-11-05 17:31:36 +0000 | [diff] [blame] | 3512 | |
Chris Lattner | fae1010 | 2004-07-06 19:28:42 +0000 | [diff] [blame] | 3513 | // If the allocation is for an even multiple of the cast type size |
| 3514 | if (CastElTySize && (AllocElTySize % CastElTySize == 0)) { |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3515 | Value *Amt = ConstantUInt::get(Type::UIntTy, |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 3516 | AllocElTySize/CastElTySize); |
Chris Lattner | fae1010 | 2004-07-06 19:28:42 +0000 | [diff] [blame] | 3517 | std::string Name = AI->getName(); AI->setName(""); |
| 3518 | AllocationInst *New; |
| 3519 | if (isa<MallocInst>(AI)) |
| 3520 | New = new MallocInst(CastElTy, Amt, Name); |
| 3521 | else |
| 3522 | New = new AllocaInst(CastElTy, Amt, Name); |
| 3523 | InsertNewInstBefore(New, *AI); |
| 3524 | return ReplaceInstUsesWith(CI, New); |
| 3525 | } |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 3526 | } |
| 3527 | } |
| 3528 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3529 | if (SelectInst *SI = dyn_cast<SelectInst>(Src)) |
| 3530 | if (Instruction *NV = FoldOpIntoSelect(CI, SI, this)) |
| 3531 | return NV; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 3532 | if (isa<PHINode>(Src)) |
| 3533 | if (Instruction *NV = FoldOpIntoPhi(CI)) |
| 3534 | return NV; |
| 3535 | |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 3536 | // If the source value is an instruction with only this use, we can attempt to |
| 3537 | // propagate the cast into the instruction. Also, only handle integral types |
| 3538 | // for now. |
| 3539 | if (Instruction *SrcI = dyn_cast<Instruction>(Src)) |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 3540 | if (SrcI->hasOneUse() && Src->getType()->isIntegral() && |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 3541 | CI.getType()->isInteger()) { // Don't mess with casts to bool here |
| 3542 | const Type *DestTy = CI.getType(); |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 3543 | unsigned SrcBitSize = Src->getType()->getPrimitiveSizeInBits(); |
| 3544 | unsigned DestBitSize = DestTy->getPrimitiveSizeInBits(); |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 3545 | |
| 3546 | Value *Op0 = SrcI->getNumOperands() > 0 ? SrcI->getOperand(0) : 0; |
| 3547 | Value *Op1 = SrcI->getNumOperands() > 1 ? SrcI->getOperand(1) : 0; |
| 3548 | |
| 3549 | switch (SrcI->getOpcode()) { |
| 3550 | case Instruction::Add: |
| 3551 | case Instruction::Mul: |
| 3552 | case Instruction::And: |
| 3553 | case Instruction::Or: |
| 3554 | case Instruction::Xor: |
| 3555 | // If we are discarding information, or just changing the sign, rewrite. |
| 3556 | if (DestBitSize <= SrcBitSize && DestBitSize != 1) { |
| 3557 | // Don't insert two casts if they cannot be eliminated. We allow two |
| 3558 | // casts to be inserted if the sizes are the same. This could only be |
| 3559 | // converting signedness, which is a noop. |
Chris Lattner | 59a2077 | 2004-07-20 05:21:00 +0000 | [diff] [blame] | 3560 | if (DestBitSize == SrcBitSize || !ValueRequiresCast(Op1, DestTy,TD) || |
| 3561 | !ValueRequiresCast(Op0, DestTy, TD)) { |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 3562 | Value *Op0c = InsertOperandCastBefore(Op0, DestTy, SrcI); |
| 3563 | Value *Op1c = InsertOperandCastBefore(Op1, DestTy, SrcI); |
| 3564 | return BinaryOperator::create(cast<BinaryOperator>(SrcI) |
| 3565 | ->getOpcode(), Op0c, Op1c); |
| 3566 | } |
| 3567 | } |
Chris Lattner | 7aed7ac | 2005-05-06 02:07:39 +0000 | [diff] [blame] | 3568 | |
| 3569 | // cast (xor bool X, true) to int --> xor (cast bool X to int), 1 |
| 3570 | if (SrcBitSize == 1 && SrcI->getOpcode() == Instruction::Xor && |
| 3571 | Op1 == ConstantBool::True && |
| 3572 | (!Op0->hasOneUse() || !isa<SetCondInst>(Op0))) { |
| 3573 | Value *New = InsertOperandCastBefore(Op0, DestTy, &CI); |
| 3574 | return BinaryOperator::createXor(New, |
| 3575 | ConstantInt::get(CI.getType(), 1)); |
| 3576 | } |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 3577 | break; |
| 3578 | case Instruction::Shl: |
| 3579 | // Allow changing the sign of the source operand. Do not allow changing |
| 3580 | // the size of the shift, UNLESS the shift amount is a constant. We |
| 3581 | // mush not change variable sized shifts to a smaller size, because it |
| 3582 | // is undefined to shift more bits out than exist in the value. |
| 3583 | if (DestBitSize == SrcBitSize || |
| 3584 | (DestBitSize < SrcBitSize && isa<Constant>(Op1))) { |
| 3585 | Value *Op0c = InsertOperandCastBefore(Op0, DestTy, SrcI); |
| 3586 | return new ShiftInst(Instruction::Shl, Op0c, Op1); |
| 3587 | } |
| 3588 | break; |
Chris Lattner | d7115b0 | 2005-05-06 04:18:52 +0000 | [diff] [blame] | 3589 | case Instruction::Shr: |
| 3590 | // If this is a signed shr, and if all bits shifted in are about to be |
| 3591 | // truncated off, turn it into an unsigned shr to allow greater |
| 3592 | // simplifications. |
| 3593 | if (DestBitSize < SrcBitSize && Src->getType()->isSigned() && |
| 3594 | isa<ConstantInt>(Op1)) { |
| 3595 | unsigned ShiftAmt = cast<ConstantUInt>(Op1)->getValue(); |
| 3596 | if (SrcBitSize > ShiftAmt && SrcBitSize-ShiftAmt >= DestBitSize) { |
| 3597 | // Convert to unsigned. |
| 3598 | Value *N1 = InsertOperandCastBefore(Op0, |
| 3599 | Op0->getType()->getUnsignedVersion(), &CI); |
| 3600 | // Insert the new shift, which is now unsigned. |
| 3601 | N1 = InsertNewInstBefore(new ShiftInst(Instruction::Shr, N1, |
| 3602 | Op1, Src->getName()), CI); |
| 3603 | return new CastInst(N1, CI.getType()); |
| 3604 | } |
| 3605 | } |
| 3606 | break; |
| 3607 | |
Chris Lattner | 693787a | 2005-05-04 19:10:26 +0000 | [diff] [blame] | 3608 | case Instruction::SetNE: |
Chris Lattner | 693787a | 2005-05-04 19:10:26 +0000 | [diff] [blame] | 3609 | if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | d152380 | 2005-05-06 01:53:19 +0000 | [diff] [blame] | 3610 | if (Op1C->getRawValue() == 0) { |
| 3611 | // If the input only has the low bit set, simplify directly. |
Chris Lattner | 693787a | 2005-05-04 19:10:26 +0000 | [diff] [blame] | 3612 | Constant *Not1 = |
| 3613 | ConstantExpr::getNot(ConstantInt::get(Op0->getType(), 1)); |
Chris Lattner | d152380 | 2005-05-06 01:53:19 +0000 | [diff] [blame] | 3614 | // cast (X != 0) to int --> X if X&~1 == 0 |
Chris Lattner | 693787a | 2005-05-04 19:10:26 +0000 | [diff] [blame] | 3615 | if (MaskedValueIsZero(Op0, cast<ConstantIntegral>(Not1))) { |
| 3616 | if (CI.getType() == Op0->getType()) |
| 3617 | return ReplaceInstUsesWith(CI, Op0); |
| 3618 | else |
| 3619 | return new CastInst(Op0, CI.getType()); |
| 3620 | } |
Chris Lattner | d152380 | 2005-05-06 01:53:19 +0000 | [diff] [blame] | 3621 | |
| 3622 | // If the input is an and with a single bit, shift then simplify. |
| 3623 | ConstantInt *AndRHS; |
| 3624 | if (match(Op0, m_And(m_Value(), m_ConstantInt(AndRHS)))) |
| 3625 | if (AndRHS->getRawValue() && |
| 3626 | (AndRHS->getRawValue() & (AndRHS->getRawValue()-1)) == 0) { |
| 3627 | unsigned ShiftAmt = Log2(AndRHS->getRawValue()); |
| 3628 | // Perform an unsigned shr by shiftamt. Convert input to |
| 3629 | // unsigned if it is signed. |
| 3630 | Value *In = Op0; |
| 3631 | if (In->getType()->isSigned()) |
| 3632 | In = InsertNewInstBefore(new CastInst(In, |
| 3633 | In->getType()->getUnsignedVersion(), In->getName()),CI); |
| 3634 | // Insert the shift to put the result in the low bit. |
| 3635 | In = InsertNewInstBefore(new ShiftInst(Instruction::Shr, In, |
| 3636 | ConstantInt::get(Type::UByteTy, ShiftAmt), |
| 3637 | In->getName()+".lobit"), CI); |
Chris Lattner | d152380 | 2005-05-06 01:53:19 +0000 | [diff] [blame] | 3638 | if (CI.getType() == In->getType()) |
| 3639 | return ReplaceInstUsesWith(CI, In); |
| 3640 | else |
| 3641 | return new CastInst(In, CI.getType()); |
| 3642 | } |
| 3643 | } |
| 3644 | } |
| 3645 | break; |
| 3646 | case Instruction::SetEQ: |
| 3647 | // We if we are just checking for a seteq of a single bit and casting it |
| 3648 | // to an integer. If so, shift the bit to the appropriate place then |
| 3649 | // cast to integer to avoid the comparison. |
| 3650 | if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) { |
| 3651 | // Is Op1C a power of two or zero? |
| 3652 | if ((Op1C->getRawValue() & Op1C->getRawValue()-1) == 0) { |
| 3653 | // cast (X == 1) to int -> X iff X has only the low bit set. |
| 3654 | if (Op1C->getRawValue() == 1) { |
| 3655 | Constant *Not1 = |
| 3656 | ConstantExpr::getNot(ConstantInt::get(Op0->getType(), 1)); |
| 3657 | if (MaskedValueIsZero(Op0, cast<ConstantIntegral>(Not1))) { |
| 3658 | if (CI.getType() == Op0->getType()) |
| 3659 | return ReplaceInstUsesWith(CI, Op0); |
| 3660 | else |
| 3661 | return new CastInst(Op0, CI.getType()); |
| 3662 | } |
| 3663 | } |
Chris Lattner | 693787a | 2005-05-04 19:10:26 +0000 | [diff] [blame] | 3664 | } |
| 3665 | } |
| 3666 | break; |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 3667 | } |
| 3668 | } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 3669 | return 0; |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 3670 | } |
| 3671 | |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 3672 | /// GetSelectFoldableOperands - We want to turn code that looks like this: |
| 3673 | /// %C = or %A, %B |
| 3674 | /// %D = select %cond, %C, %A |
| 3675 | /// into: |
| 3676 | /// %C = select %cond, %B, 0 |
| 3677 | /// %D = or %A, %C |
| 3678 | /// |
| 3679 | /// Assuming that the specified instruction is an operand to the select, return |
| 3680 | /// a bitmask indicating which operands of this instruction are foldable if they |
| 3681 | /// equal the other incoming value of the select. |
| 3682 | /// |
| 3683 | static unsigned GetSelectFoldableOperands(Instruction *I) { |
| 3684 | switch (I->getOpcode()) { |
| 3685 | case Instruction::Add: |
| 3686 | case Instruction::Mul: |
| 3687 | case Instruction::And: |
| 3688 | case Instruction::Or: |
| 3689 | case Instruction::Xor: |
| 3690 | return 3; // Can fold through either operand. |
| 3691 | case Instruction::Sub: // Can only fold on the amount subtracted. |
| 3692 | case Instruction::Shl: // Can only fold on the shift amount. |
| 3693 | case Instruction::Shr: |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3694 | return 1; |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 3695 | default: |
| 3696 | return 0; // Cannot fold |
| 3697 | } |
| 3698 | } |
| 3699 | |
| 3700 | /// GetSelectFoldableConstant - For the same transformation as the previous |
| 3701 | /// function, return the identity constant that goes into the select. |
| 3702 | static Constant *GetSelectFoldableConstant(Instruction *I) { |
| 3703 | switch (I->getOpcode()) { |
| 3704 | default: assert(0 && "This cannot happen!"); abort(); |
| 3705 | case Instruction::Add: |
| 3706 | case Instruction::Sub: |
| 3707 | case Instruction::Or: |
| 3708 | case Instruction::Xor: |
| 3709 | return Constant::getNullValue(I->getType()); |
| 3710 | case Instruction::Shl: |
| 3711 | case Instruction::Shr: |
| 3712 | return Constant::getNullValue(Type::UByteTy); |
| 3713 | case Instruction::And: |
| 3714 | return ConstantInt::getAllOnesValue(I->getType()); |
| 3715 | case Instruction::Mul: |
| 3716 | return ConstantInt::get(I->getType(), 1); |
| 3717 | } |
| 3718 | } |
| 3719 | |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 3720 | /// FoldSelectOpOp - Here we have (select c, TI, FI), and we know that TI and FI |
| 3721 | /// have the same opcode and only one use each. Try to simplify this. |
| 3722 | Instruction *InstCombiner::FoldSelectOpOp(SelectInst &SI, Instruction *TI, |
| 3723 | Instruction *FI) { |
| 3724 | if (TI->getNumOperands() == 1) { |
| 3725 | // If this is a non-volatile load or a cast from the same type, |
| 3726 | // merge. |
| 3727 | if (TI->getOpcode() == Instruction::Cast) { |
| 3728 | if (TI->getOperand(0)->getType() != FI->getOperand(0)->getType()) |
| 3729 | return 0; |
| 3730 | } else { |
| 3731 | return 0; // unknown unary op. |
| 3732 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3733 | |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 3734 | // Fold this by inserting a select from the input values. |
| 3735 | SelectInst *NewSI = new SelectInst(SI.getCondition(), TI->getOperand(0), |
| 3736 | FI->getOperand(0), SI.getName()+".v"); |
| 3737 | InsertNewInstBefore(NewSI, SI); |
| 3738 | return new CastInst(NewSI, TI->getType()); |
| 3739 | } |
| 3740 | |
| 3741 | // Only handle binary operators here. |
| 3742 | if (!isa<ShiftInst>(TI) && !isa<BinaryOperator>(TI)) |
| 3743 | return 0; |
| 3744 | |
| 3745 | // Figure out if the operations have any operands in common. |
| 3746 | Value *MatchOp, *OtherOpT, *OtherOpF; |
| 3747 | bool MatchIsOpZero; |
| 3748 | if (TI->getOperand(0) == FI->getOperand(0)) { |
| 3749 | MatchOp = TI->getOperand(0); |
| 3750 | OtherOpT = TI->getOperand(1); |
| 3751 | OtherOpF = FI->getOperand(1); |
| 3752 | MatchIsOpZero = true; |
| 3753 | } else if (TI->getOperand(1) == FI->getOperand(1)) { |
| 3754 | MatchOp = TI->getOperand(1); |
| 3755 | OtherOpT = TI->getOperand(0); |
| 3756 | OtherOpF = FI->getOperand(0); |
| 3757 | MatchIsOpZero = false; |
| 3758 | } else if (!TI->isCommutative()) { |
| 3759 | return 0; |
| 3760 | } else if (TI->getOperand(0) == FI->getOperand(1)) { |
| 3761 | MatchOp = TI->getOperand(0); |
| 3762 | OtherOpT = TI->getOperand(1); |
| 3763 | OtherOpF = FI->getOperand(0); |
| 3764 | MatchIsOpZero = true; |
| 3765 | } else if (TI->getOperand(1) == FI->getOperand(0)) { |
| 3766 | MatchOp = TI->getOperand(1); |
| 3767 | OtherOpT = TI->getOperand(0); |
| 3768 | OtherOpF = FI->getOperand(1); |
| 3769 | MatchIsOpZero = true; |
| 3770 | } else { |
| 3771 | return 0; |
| 3772 | } |
| 3773 | |
| 3774 | // If we reach here, they do have operations in common. |
| 3775 | SelectInst *NewSI = new SelectInst(SI.getCondition(), OtherOpT, |
| 3776 | OtherOpF, SI.getName()+".v"); |
| 3777 | InsertNewInstBefore(NewSI, SI); |
| 3778 | |
| 3779 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TI)) { |
| 3780 | if (MatchIsOpZero) |
| 3781 | return BinaryOperator::create(BO->getOpcode(), MatchOp, NewSI); |
| 3782 | else |
| 3783 | return BinaryOperator::create(BO->getOpcode(), NewSI, MatchOp); |
| 3784 | } else { |
| 3785 | if (MatchIsOpZero) |
| 3786 | return new ShiftInst(cast<ShiftInst>(TI)->getOpcode(), MatchOp, NewSI); |
| 3787 | else |
| 3788 | return new ShiftInst(cast<ShiftInst>(TI)->getOpcode(), NewSI, MatchOp); |
| 3789 | } |
| 3790 | } |
| 3791 | |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 3792 | Instruction *InstCombiner::visitSelectInst(SelectInst &SI) { |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 3793 | Value *CondVal = SI.getCondition(); |
| 3794 | Value *TrueVal = SI.getTrueValue(); |
| 3795 | Value *FalseVal = SI.getFalseValue(); |
| 3796 | |
| 3797 | // select true, X, Y -> X |
| 3798 | // select false, X, Y -> Y |
| 3799 | if (ConstantBool *C = dyn_cast<ConstantBool>(CondVal)) |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 3800 | if (C == ConstantBool::True) |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 3801 | return ReplaceInstUsesWith(SI, TrueVal); |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 3802 | else { |
| 3803 | assert(C == ConstantBool::False); |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 3804 | return ReplaceInstUsesWith(SI, FalseVal); |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 3805 | } |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 3806 | |
| 3807 | // select C, X, X -> X |
| 3808 | if (TrueVal == FalseVal) |
| 3809 | return ReplaceInstUsesWith(SI, TrueVal); |
| 3810 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3811 | if (isa<UndefValue>(TrueVal)) // select C, undef, X -> X |
| 3812 | return ReplaceInstUsesWith(SI, FalseVal); |
| 3813 | if (isa<UndefValue>(FalseVal)) // select C, X, undef -> X |
| 3814 | return ReplaceInstUsesWith(SI, TrueVal); |
| 3815 | if (isa<UndefValue>(CondVal)) { // select undef, X, Y -> X or Y |
| 3816 | if (isa<Constant>(TrueVal)) |
| 3817 | return ReplaceInstUsesWith(SI, TrueVal); |
| 3818 | else |
| 3819 | return ReplaceInstUsesWith(SI, FalseVal); |
| 3820 | } |
| 3821 | |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 3822 | if (SI.getType() == Type::BoolTy) |
| 3823 | if (ConstantBool *C = dyn_cast<ConstantBool>(TrueVal)) { |
| 3824 | if (C == ConstantBool::True) { |
| 3825 | // Change: A = select B, true, C --> A = or B, C |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3826 | return BinaryOperator::createOr(CondVal, FalseVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 3827 | } else { |
| 3828 | // Change: A = select B, false, C --> A = and !B, C |
| 3829 | Value *NotCond = |
| 3830 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
| 3831 | "not."+CondVal->getName()), SI); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3832 | return BinaryOperator::createAnd(NotCond, FalseVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 3833 | } |
| 3834 | } else if (ConstantBool *C = dyn_cast<ConstantBool>(FalseVal)) { |
| 3835 | if (C == ConstantBool::False) { |
| 3836 | // Change: A = select B, C, false --> A = and B, C |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3837 | return BinaryOperator::createAnd(CondVal, TrueVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 3838 | } else { |
| 3839 | // Change: A = select B, C, true --> A = or !B, C |
| 3840 | Value *NotCond = |
| 3841 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
| 3842 | "not."+CondVal->getName()), SI); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3843 | return BinaryOperator::createOr(NotCond, TrueVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 3844 | } |
| 3845 | } |
| 3846 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3847 | // Selecting between two integer constants? |
| 3848 | if (ConstantInt *TrueValC = dyn_cast<ConstantInt>(TrueVal)) |
| 3849 | if (ConstantInt *FalseValC = dyn_cast<ConstantInt>(FalseVal)) { |
| 3850 | // select C, 1, 0 -> cast C to int |
| 3851 | if (FalseValC->isNullValue() && TrueValC->getRawValue() == 1) { |
| 3852 | return new CastInst(CondVal, SI.getType()); |
| 3853 | } else if (TrueValC->isNullValue() && FalseValC->getRawValue() == 1) { |
| 3854 | // select C, 0, 1 -> cast !C to int |
| 3855 | Value *NotCond = |
| 3856 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
Chris Lattner | 82e14fe | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 3857 | "not."+CondVal->getName()), SI); |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3858 | return new CastInst(NotCond, SI.getType()); |
Chris Lattner | 82e14fe | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 3859 | } |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 3860 | |
| 3861 | // If one of the constants is zero (we know they can't both be) and we |
| 3862 | // have a setcc instruction with zero, and we have an 'and' with the |
| 3863 | // non-constant value, eliminate this whole mess. This corresponds to |
| 3864 | // cases like this: ((X & 27) ? 27 : 0) |
| 3865 | if (TrueValC->isNullValue() || FalseValC->isNullValue()) |
| 3866 | if (Instruction *IC = dyn_cast<Instruction>(SI.getCondition())) |
| 3867 | if ((IC->getOpcode() == Instruction::SetEQ || |
| 3868 | IC->getOpcode() == Instruction::SetNE) && |
| 3869 | isa<ConstantInt>(IC->getOperand(1)) && |
| 3870 | cast<Constant>(IC->getOperand(1))->isNullValue()) |
| 3871 | if (Instruction *ICA = dyn_cast<Instruction>(IC->getOperand(0))) |
| 3872 | if (ICA->getOpcode() == Instruction::And && |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3873 | isa<ConstantInt>(ICA->getOperand(1)) && |
| 3874 | (ICA->getOperand(1) == TrueValC || |
| 3875 | ICA->getOperand(1) == FalseValC) && |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 3876 | isOneBitSet(cast<ConstantInt>(ICA->getOperand(1)))) { |
| 3877 | // Okay, now we know that everything is set up, we just don't |
| 3878 | // know whether we have a setne or seteq and whether the true or |
| 3879 | // false val is the zero. |
| 3880 | bool ShouldNotVal = !TrueValC->isNullValue(); |
| 3881 | ShouldNotVal ^= IC->getOpcode() == Instruction::SetNE; |
| 3882 | Value *V = ICA; |
| 3883 | if (ShouldNotVal) |
| 3884 | V = InsertNewInstBefore(BinaryOperator::create( |
| 3885 | Instruction::Xor, V, ICA->getOperand(1)), SI); |
| 3886 | return ReplaceInstUsesWith(SI, V); |
| 3887 | } |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 3888 | } |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 3889 | |
| 3890 | // See if we are selecting two values based on a comparison of the two values. |
| 3891 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(CondVal)) { |
| 3892 | if (SCI->getOperand(0) == TrueVal && SCI->getOperand(1) == FalseVal) { |
| 3893 | // Transform (X == Y) ? X : Y -> Y |
| 3894 | if (SCI->getOpcode() == Instruction::SetEQ) |
| 3895 | return ReplaceInstUsesWith(SI, FalseVal); |
| 3896 | // Transform (X != Y) ? X : Y -> X |
| 3897 | if (SCI->getOpcode() == Instruction::SetNE) |
| 3898 | return ReplaceInstUsesWith(SI, TrueVal); |
| 3899 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 3900 | |
| 3901 | } else if (SCI->getOperand(0) == FalseVal && SCI->getOperand(1) == TrueVal){ |
| 3902 | // Transform (X == Y) ? Y : X -> X |
| 3903 | if (SCI->getOpcode() == Instruction::SetEQ) |
Chris Lattner | fbede52 | 2004-04-11 01:39:19 +0000 | [diff] [blame] | 3904 | return ReplaceInstUsesWith(SI, FalseVal); |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 3905 | // Transform (X != Y) ? Y : X -> Y |
| 3906 | if (SCI->getOpcode() == Instruction::SetNE) |
Chris Lattner | fbede52 | 2004-04-11 01:39:19 +0000 | [diff] [blame] | 3907 | return ReplaceInstUsesWith(SI, TrueVal); |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 3908 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 3909 | } |
| 3910 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3911 | |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 3912 | if (Instruction *TI = dyn_cast<Instruction>(TrueVal)) |
| 3913 | if (Instruction *FI = dyn_cast<Instruction>(FalseVal)) |
| 3914 | if (TI->hasOneUse() && FI->hasOneUse()) { |
| 3915 | bool isInverse = false; |
| 3916 | Instruction *AddOp = 0, *SubOp = 0; |
| 3917 | |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 3918 | // Turn (select C, (op X, Y), (op X, Z)) -> (op X, (select C, Y, Z)) |
| 3919 | if (TI->getOpcode() == FI->getOpcode()) |
| 3920 | if (Instruction *IV = FoldSelectOpOp(SI, TI, FI)) |
| 3921 | return IV; |
| 3922 | |
| 3923 | // Turn select C, (X+Y), (X-Y) --> (X+(select C, Y, (-Y))). This is |
| 3924 | // even legal for FP. |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 3925 | if (TI->getOpcode() == Instruction::Sub && |
| 3926 | FI->getOpcode() == Instruction::Add) { |
| 3927 | AddOp = FI; SubOp = TI; |
| 3928 | } else if (FI->getOpcode() == Instruction::Sub && |
| 3929 | TI->getOpcode() == Instruction::Add) { |
| 3930 | AddOp = TI; SubOp = FI; |
| 3931 | } |
| 3932 | |
| 3933 | if (AddOp) { |
| 3934 | Value *OtherAddOp = 0; |
| 3935 | if (SubOp->getOperand(0) == AddOp->getOperand(0)) { |
| 3936 | OtherAddOp = AddOp->getOperand(1); |
| 3937 | } else if (SubOp->getOperand(0) == AddOp->getOperand(1)) { |
| 3938 | OtherAddOp = AddOp->getOperand(0); |
| 3939 | } |
| 3940 | |
| 3941 | if (OtherAddOp) { |
| 3942 | // So at this point we know we have: |
| 3943 | // select C, (add X, Y), (sub X, ?) |
| 3944 | // We can do the transform profitably if either 'Y' = '?' or '?' is |
| 3945 | // a constant. |
| 3946 | if (SubOp->getOperand(1) == AddOp || |
| 3947 | isa<Constant>(SubOp->getOperand(1))) { |
| 3948 | Value *NegVal; |
| 3949 | if (Constant *C = dyn_cast<Constant>(SubOp->getOperand(1))) { |
| 3950 | NegVal = ConstantExpr::getNeg(C); |
| 3951 | } else { |
| 3952 | NegVal = InsertNewInstBefore( |
| 3953 | BinaryOperator::createNeg(SubOp->getOperand(1)), SI); |
| 3954 | } |
| 3955 | |
Chris Lattner | 906ab50 | 2005-01-14 17:35:12 +0000 | [diff] [blame] | 3956 | Value *NewTrueOp = OtherAddOp; |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 3957 | Value *NewFalseOp = NegVal; |
| 3958 | if (AddOp != TI) |
| 3959 | std::swap(NewTrueOp, NewFalseOp); |
| 3960 | Instruction *NewSel = |
| 3961 | new SelectInst(CondVal, NewTrueOp,NewFalseOp,SI.getName()+".p"); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3962 | |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 3963 | NewSel = InsertNewInstBefore(NewSel, SI); |
Chris Lattner | 906ab50 | 2005-01-14 17:35:12 +0000 | [diff] [blame] | 3964 | return BinaryOperator::createAdd(SubOp->getOperand(0), NewSel); |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 3965 | } |
| 3966 | } |
| 3967 | } |
| 3968 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3969 | |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 3970 | // See if we can fold the select into one of our operands. |
| 3971 | if (SI.getType()->isInteger()) { |
| 3972 | // See the comment above GetSelectFoldableOperands for a description of the |
| 3973 | // transformation we are doing here. |
| 3974 | if (Instruction *TVI = dyn_cast<Instruction>(TrueVal)) |
| 3975 | if (TVI->hasOneUse() && TVI->getNumOperands() == 2 && |
| 3976 | !isa<Constant>(FalseVal)) |
| 3977 | if (unsigned SFO = GetSelectFoldableOperands(TVI)) { |
| 3978 | unsigned OpToFold = 0; |
| 3979 | if ((SFO & 1) && FalseVal == TVI->getOperand(0)) { |
| 3980 | OpToFold = 1; |
| 3981 | } else if ((SFO & 2) && FalseVal == TVI->getOperand(1)) { |
| 3982 | OpToFold = 2; |
| 3983 | } |
| 3984 | |
| 3985 | if (OpToFold) { |
| 3986 | Constant *C = GetSelectFoldableConstant(TVI); |
| 3987 | std::string Name = TVI->getName(); TVI->setName(""); |
| 3988 | Instruction *NewSel = |
| 3989 | new SelectInst(SI.getCondition(), TVI->getOperand(2-OpToFold), C, |
| 3990 | Name); |
| 3991 | InsertNewInstBefore(NewSel, SI); |
| 3992 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TVI)) |
| 3993 | return BinaryOperator::create(BO->getOpcode(), FalseVal, NewSel); |
| 3994 | else if (ShiftInst *SI = dyn_cast<ShiftInst>(TVI)) |
| 3995 | return new ShiftInst(SI->getOpcode(), FalseVal, NewSel); |
| 3996 | else { |
| 3997 | assert(0 && "Unknown instruction!!"); |
| 3998 | } |
| 3999 | } |
| 4000 | } |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4001 | |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 4002 | if (Instruction *FVI = dyn_cast<Instruction>(FalseVal)) |
| 4003 | if (FVI->hasOneUse() && FVI->getNumOperands() == 2 && |
| 4004 | !isa<Constant>(TrueVal)) |
| 4005 | if (unsigned SFO = GetSelectFoldableOperands(FVI)) { |
| 4006 | unsigned OpToFold = 0; |
| 4007 | if ((SFO & 1) && TrueVal == FVI->getOperand(0)) { |
| 4008 | OpToFold = 1; |
| 4009 | } else if ((SFO & 2) && TrueVal == FVI->getOperand(1)) { |
| 4010 | OpToFold = 2; |
| 4011 | } |
| 4012 | |
| 4013 | if (OpToFold) { |
| 4014 | Constant *C = GetSelectFoldableConstant(FVI); |
| 4015 | std::string Name = FVI->getName(); FVI->setName(""); |
| 4016 | Instruction *NewSel = |
| 4017 | new SelectInst(SI.getCondition(), C, FVI->getOperand(2-OpToFold), |
| 4018 | Name); |
| 4019 | InsertNewInstBefore(NewSel, SI); |
| 4020 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(FVI)) |
| 4021 | return BinaryOperator::create(BO->getOpcode(), TrueVal, NewSel); |
| 4022 | else if (ShiftInst *SI = dyn_cast<ShiftInst>(FVI)) |
| 4023 | return new ShiftInst(SI->getOpcode(), TrueVal, NewSel); |
| 4024 | else { |
| 4025 | assert(0 && "Unknown instruction!!"); |
| 4026 | } |
| 4027 | } |
| 4028 | } |
| 4029 | } |
Chris Lattner | a1df33c | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 4030 | |
| 4031 | if (BinaryOperator::isNot(CondVal)) { |
| 4032 | SI.setOperand(0, BinaryOperator::getNotArgument(CondVal)); |
| 4033 | SI.setOperand(1, FalseVal); |
| 4034 | SI.setOperand(2, TrueVal); |
| 4035 | return &SI; |
| 4036 | } |
| 4037 | |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 4038 | return 0; |
| 4039 | } |
| 4040 | |
| 4041 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4042 | // CallInst simplification |
| 4043 | // |
| 4044 | Instruction *InstCombiner::visitCallInst(CallInst &CI) { |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 4045 | // Intrinsics cannot occur in an invoke, so handle them here instead of in |
| 4046 | // visitCallSite. |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 4047 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(&CI)) { |
| 4048 | bool Changed = false; |
| 4049 | |
| 4050 | // memmove/cpy/set of zero bytes is a noop. |
| 4051 | if (Constant *NumBytes = dyn_cast<Constant>(MI->getLength())) { |
| 4052 | if (NumBytes->isNullValue()) return EraseInstFromFunction(CI); |
| 4053 | |
| 4054 | // FIXME: Increase alignment here. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4055 | |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 4056 | if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes)) |
| 4057 | if (CI->getRawValue() == 1) { |
| 4058 | // Replace the instruction with just byte operations. We would |
| 4059 | // transform other cases to loads/stores, but we don't know if |
| 4060 | // alignment is sufficient. |
| 4061 | } |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 4062 | } |
| 4063 | |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 4064 | // If we have a memmove and the source operation is a constant global, |
| 4065 | // then the source and dest pointers can't alias, so we can change this |
| 4066 | // into a call to memcpy. |
| 4067 | if (MemMoveInst *MMI = dyn_cast<MemMoveInst>(MI)) |
| 4068 | if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource())) |
| 4069 | if (GVSrc->isConstant()) { |
| 4070 | Module *M = CI.getParent()->getParent()->getParent(); |
| 4071 | Function *MemCpy = M->getOrInsertFunction("llvm.memcpy", |
| 4072 | CI.getCalledFunction()->getFunctionType()); |
| 4073 | CI.setOperand(0, MemCpy); |
| 4074 | Changed = true; |
| 4075 | } |
| 4076 | |
| 4077 | if (Changed) return &CI; |
Chris Lattner | 954f66a | 2004-11-18 21:41:39 +0000 | [diff] [blame] | 4078 | } else if (DbgStopPointInst *SPI = dyn_cast<DbgStopPointInst>(&CI)) { |
| 4079 | // If this stoppoint is at the same source location as the previous |
| 4080 | // stoppoint in the chain, it is not needed. |
| 4081 | if (DbgStopPointInst *PrevSPI = |
| 4082 | dyn_cast<DbgStopPointInst>(SPI->getChain())) |
| 4083 | if (SPI->getLineNo() == PrevSPI->getLineNo() && |
| 4084 | SPI->getColNo() == PrevSPI->getColNo()) { |
| 4085 | SPI->replaceAllUsesWith(PrevSPI); |
| 4086 | return EraseInstFromFunction(CI); |
| 4087 | } |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 4088 | } |
| 4089 | |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 4090 | return visitCallSite(&CI); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4091 | } |
| 4092 | |
| 4093 | // InvokeInst simplification |
| 4094 | // |
| 4095 | Instruction *InstCombiner::visitInvokeInst(InvokeInst &II) { |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 4096 | return visitCallSite(&II); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4097 | } |
| 4098 | |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 4099 | // visitCallSite - Improvements for call and invoke instructions. |
| 4100 | // |
| 4101 | Instruction *InstCombiner::visitCallSite(CallSite CS) { |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 4102 | bool Changed = false; |
| 4103 | |
| 4104 | // If the callee is a constexpr cast of a function, attempt to move the cast |
| 4105 | // to the arguments of the call/invoke. |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 4106 | if (transformConstExprCastCall(CS)) return 0; |
| 4107 | |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 4108 | Value *Callee = CS.getCalledValue(); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4109 | |
Chris Lattner | 08b22ec | 2005-05-13 07:09:09 +0000 | [diff] [blame] | 4110 | if (Function *CalleeF = dyn_cast<Function>(Callee)) |
| 4111 | if (CalleeF->getCallingConv() != CS.getCallingConv()) { |
| 4112 | Instruction *OldCall = CS.getInstruction(); |
| 4113 | // If the call and callee calling conventions don't match, this call must |
| 4114 | // be unreachable, as the call is undefined. |
| 4115 | new StoreInst(ConstantBool::True, |
| 4116 | UndefValue::get(PointerType::get(Type::BoolTy)), OldCall); |
| 4117 | if (!OldCall->use_empty()) |
| 4118 | OldCall->replaceAllUsesWith(UndefValue::get(OldCall->getType())); |
| 4119 | if (isa<CallInst>(OldCall)) // Not worth removing an invoke here. |
| 4120 | return EraseInstFromFunction(*OldCall); |
| 4121 | return 0; |
| 4122 | } |
| 4123 | |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 4124 | if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) { |
| 4125 | // This instruction is not reachable, just remove it. We insert a store to |
| 4126 | // undef so that we know that this code is not reachable, despite the fact |
| 4127 | // that we can't modify the CFG here. |
| 4128 | new StoreInst(ConstantBool::True, |
| 4129 | UndefValue::get(PointerType::get(Type::BoolTy)), |
| 4130 | CS.getInstruction()); |
| 4131 | |
| 4132 | if (!CS.getInstruction()->use_empty()) |
| 4133 | CS.getInstruction()-> |
| 4134 | replaceAllUsesWith(UndefValue::get(CS.getInstruction()->getType())); |
| 4135 | |
| 4136 | if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) { |
| 4137 | // Don't break the CFG, insert a dummy cond branch. |
| 4138 | new BranchInst(II->getNormalDest(), II->getUnwindDest(), |
| 4139 | ConstantBool::True, II); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4140 | } |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 4141 | return EraseInstFromFunction(*CS.getInstruction()); |
| 4142 | } |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4143 | |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 4144 | const PointerType *PTy = cast<PointerType>(Callee->getType()); |
| 4145 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
| 4146 | if (FTy->isVarArg()) { |
| 4147 | // See if we can optimize any arguments passed through the varargs area of |
| 4148 | // the call. |
| 4149 | for (CallSite::arg_iterator I = CS.arg_begin()+FTy->getNumParams(), |
| 4150 | E = CS.arg_end(); I != E; ++I) |
| 4151 | if (CastInst *CI = dyn_cast<CastInst>(*I)) { |
| 4152 | // If this cast does not effect the value passed through the varargs |
| 4153 | // area, we can eliminate the use of the cast. |
| 4154 | Value *Op = CI->getOperand(0); |
| 4155 | if (CI->getType()->isLosslesslyConvertibleTo(Op->getType())) { |
| 4156 | *I = Op; |
| 4157 | Changed = true; |
| 4158 | } |
| 4159 | } |
| 4160 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4161 | |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 4162 | return Changed ? CS.getInstruction() : 0; |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 4163 | } |
| 4164 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4165 | // transformConstExprCastCall - If the callee is a constexpr cast of a function, |
| 4166 | // attempt to move the cast to the arguments of the call/invoke. |
| 4167 | // |
| 4168 | bool InstCombiner::transformConstExprCastCall(CallSite CS) { |
| 4169 | if (!isa<ConstantExpr>(CS.getCalledValue())) return false; |
| 4170 | ConstantExpr *CE = cast<ConstantExpr>(CS.getCalledValue()); |
Chris Lattner | 9db07b9 | 2004-07-18 18:59:44 +0000 | [diff] [blame] | 4171 | if (CE->getOpcode() != Instruction::Cast || !isa<Function>(CE->getOperand(0))) |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4172 | return false; |
Reid Spencer | 8863f18 | 2004-07-18 00:38:32 +0000 | [diff] [blame] | 4173 | Function *Callee = cast<Function>(CE->getOperand(0)); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4174 | Instruction *Caller = CS.getInstruction(); |
| 4175 | |
| 4176 | // Okay, this is a cast from a function to a different type. Unless doing so |
| 4177 | // would cause a type conversion of one of our arguments, change this call to |
| 4178 | // be a direct call with arguments casted to the appropriate types. |
| 4179 | // |
| 4180 | const FunctionType *FT = Callee->getFunctionType(); |
| 4181 | const Type *OldRetTy = Caller->getType(); |
| 4182 | |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 4183 | // Check to see if we are changing the return type... |
| 4184 | if (OldRetTy != FT->getReturnType()) { |
| 4185 | if (Callee->isExternal() && |
| 4186 | !OldRetTy->isLosslesslyConvertibleTo(FT->getReturnType()) && |
| 4187 | !Caller->use_empty()) |
| 4188 | return false; // Cannot transform this return value... |
| 4189 | |
| 4190 | // If the callsite is an invoke instruction, and the return value is used by |
| 4191 | // a PHI node in a successor, we cannot change the return type of the call |
| 4192 | // because there is no place to put the cast instruction (without breaking |
| 4193 | // the critical edge). Bail out in this case. |
| 4194 | if (!Caller->use_empty()) |
| 4195 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) |
| 4196 | for (Value::use_iterator UI = II->use_begin(), E = II->use_end(); |
| 4197 | UI != E; ++UI) |
| 4198 | if (PHINode *PN = dyn_cast<PHINode>(*UI)) |
| 4199 | if (PN->getParent() == II->getNormalDest() || |
Chris Lattner | aeb2a1d | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 4200 | PN->getParent() == II->getUnwindDest()) |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 4201 | return false; |
| 4202 | } |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4203 | |
| 4204 | unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin()); |
| 4205 | unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4206 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4207 | CallSite::arg_iterator AI = CS.arg_begin(); |
| 4208 | for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) { |
| 4209 | const Type *ParamTy = FT->getParamType(i); |
| 4210 | bool isConvertible = (*AI)->getType()->isLosslesslyConvertibleTo(ParamTy); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4211 | if (Callee->isExternal() && !isConvertible) return false; |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4212 | } |
| 4213 | |
| 4214 | if (FT->getNumParams() < NumActualArgs && !FT->isVarArg() && |
| 4215 | Callee->isExternal()) |
| 4216 | return false; // Do not delete arguments unless we have a function body... |
| 4217 | |
| 4218 | // Okay, we decided that this is a safe thing to do: go ahead and start |
| 4219 | // inserting cast instructions as necessary... |
| 4220 | std::vector<Value*> Args; |
| 4221 | Args.reserve(NumActualArgs); |
| 4222 | |
| 4223 | AI = CS.arg_begin(); |
| 4224 | for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) { |
| 4225 | const Type *ParamTy = FT->getParamType(i); |
| 4226 | if ((*AI)->getType() == ParamTy) { |
| 4227 | Args.push_back(*AI); |
| 4228 | } else { |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 4229 | Args.push_back(InsertNewInstBefore(new CastInst(*AI, ParamTy, "tmp"), |
| 4230 | *Caller)); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4231 | } |
| 4232 | } |
| 4233 | |
| 4234 | // If the function takes more arguments than the call was taking, add them |
| 4235 | // now... |
| 4236 | for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i) |
| 4237 | Args.push_back(Constant::getNullValue(FT->getParamType(i))); |
| 4238 | |
| 4239 | // If we are removing arguments to the function, emit an obnoxious warning... |
| 4240 | if (FT->getNumParams() < NumActualArgs) |
| 4241 | if (!FT->isVarArg()) { |
| 4242 | std::cerr << "WARNING: While resolving call to function '" |
| 4243 | << Callee->getName() << "' arguments were dropped!\n"; |
| 4244 | } else { |
| 4245 | // Add all of the arguments in their promoted form to the arg list... |
| 4246 | for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) { |
| 4247 | const Type *PTy = getPromotedType((*AI)->getType()); |
| 4248 | if (PTy != (*AI)->getType()) { |
| 4249 | // Must promote to pass through va_arg area! |
| 4250 | Instruction *Cast = new CastInst(*AI, PTy, "tmp"); |
| 4251 | InsertNewInstBefore(Cast, *Caller); |
| 4252 | Args.push_back(Cast); |
| 4253 | } else { |
| 4254 | Args.push_back(*AI); |
| 4255 | } |
| 4256 | } |
| 4257 | } |
| 4258 | |
| 4259 | if (FT->getReturnType() == Type::VoidTy) |
| 4260 | Caller->setName(""); // Void type should not have a name... |
| 4261 | |
| 4262 | Instruction *NC; |
| 4263 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
Chris Lattner | aeb2a1d | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 4264 | NC = new InvokeInst(Callee, II->getNormalDest(), II->getUnwindDest(), |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4265 | Args, Caller->getName(), Caller); |
Chris Lattner | e437026 | 2005-05-14 12:25:32 +0000 | [diff] [blame] | 4266 | cast<InvokeInst>(II)->setCallingConv(II->getCallingConv()); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4267 | } else { |
| 4268 | NC = new CallInst(Callee, Args, Caller->getName(), Caller); |
Chris Lattner | a9e9211 | 2005-05-06 06:48:21 +0000 | [diff] [blame] | 4269 | if (cast<CallInst>(Caller)->isTailCall()) |
| 4270 | cast<CallInst>(NC)->setTailCall(); |
Chris Lattner | e437026 | 2005-05-14 12:25:32 +0000 | [diff] [blame] | 4271 | cast<CallInst>(NC)->setCallingConv(cast<CallInst>(Caller)->getCallingConv()); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4272 | } |
| 4273 | |
| 4274 | // Insert a cast of the return type as necessary... |
| 4275 | Value *NV = NC; |
| 4276 | if (Caller->getType() != NV->getType() && !Caller->use_empty()) { |
| 4277 | if (NV->getType() != Type::VoidTy) { |
| 4278 | NV = NC = new CastInst(NC, Caller->getType(), "tmp"); |
Chris Lattner | bb60904 | 2003-10-30 00:46:41 +0000 | [diff] [blame] | 4279 | |
| 4280 | // If this is an invoke instruction, we should insert it after the first |
| 4281 | // non-phi, instruction in the normal successor block. |
| 4282 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
| 4283 | BasicBlock::iterator I = II->getNormalDest()->begin(); |
| 4284 | while (isa<PHINode>(I)) ++I; |
| 4285 | InsertNewInstBefore(NC, *I); |
| 4286 | } else { |
| 4287 | // Otherwise, it's a call, just insert cast right after the call instr |
| 4288 | InsertNewInstBefore(NC, *Caller); |
| 4289 | } |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 4290 | AddUsersToWorkList(*Caller); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4291 | } else { |
Chris Lattner | c30bda7 | 2004-10-17 21:22:38 +0000 | [diff] [blame] | 4292 | NV = UndefValue::get(Caller->getType()); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 4293 | } |
| 4294 | } |
| 4295 | |
| 4296 | if (Caller->getType() != Type::VoidTy && !Caller->use_empty()) |
| 4297 | Caller->replaceAllUsesWith(NV); |
| 4298 | Caller->getParent()->getInstList().erase(Caller); |
| 4299 | removeFromWorkList(Caller); |
| 4300 | return true; |
| 4301 | } |
| 4302 | |
| 4303 | |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 4304 | // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary" |
| 4305 | // operator and they all are only used by the PHI, PHI together their |
| 4306 | // inputs, and do the operation once, to the result of the PHI. |
| 4307 | Instruction *InstCombiner::FoldPHIArgOpIntoPHI(PHINode &PN) { |
| 4308 | Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0)); |
| 4309 | |
| 4310 | // Scan the instruction, looking for input operations that can be folded away. |
| 4311 | // If all input operands to the phi are the same instruction (e.g. a cast from |
| 4312 | // the same type or "+42") we can pull the operation through the PHI, reducing |
| 4313 | // code size and simplifying code. |
| 4314 | Constant *ConstantOp = 0; |
| 4315 | const Type *CastSrcTy = 0; |
| 4316 | if (isa<CastInst>(FirstInst)) { |
| 4317 | CastSrcTy = FirstInst->getOperand(0)->getType(); |
| 4318 | } else if (isa<BinaryOperator>(FirstInst) || isa<ShiftInst>(FirstInst)) { |
| 4319 | // Can fold binop or shift if the RHS is a constant. |
| 4320 | ConstantOp = dyn_cast<Constant>(FirstInst->getOperand(1)); |
| 4321 | if (ConstantOp == 0) return 0; |
| 4322 | } else { |
| 4323 | return 0; // Cannot fold this operation. |
| 4324 | } |
| 4325 | |
| 4326 | // Check to see if all arguments are the same operation. |
| 4327 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 4328 | if (!isa<Instruction>(PN.getIncomingValue(i))) return 0; |
| 4329 | Instruction *I = cast<Instruction>(PN.getIncomingValue(i)); |
| 4330 | if (!I->hasOneUse() || I->getOpcode() != FirstInst->getOpcode()) |
| 4331 | return 0; |
| 4332 | if (CastSrcTy) { |
| 4333 | if (I->getOperand(0)->getType() != CastSrcTy) |
| 4334 | return 0; // Cast operation must match. |
| 4335 | } else if (I->getOperand(1) != ConstantOp) { |
| 4336 | return 0; |
| 4337 | } |
| 4338 | } |
| 4339 | |
| 4340 | // Okay, they are all the same operation. Create a new PHI node of the |
| 4341 | // correct type, and PHI together all of the LHS's of the instructions. |
| 4342 | PHINode *NewPN = new PHINode(FirstInst->getOperand(0)->getType(), |
| 4343 | PN.getName()+".in"); |
Chris Lattner | 5551706 | 2005-01-29 00:39:08 +0000 | [diff] [blame] | 4344 | NewPN->reserveOperandSpace(PN.getNumOperands()/2); |
Chris Lattner | b589344 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 4345 | |
| 4346 | Value *InVal = FirstInst->getOperand(0); |
| 4347 | NewPN->addIncoming(InVal, PN.getIncomingBlock(0)); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 4348 | |
| 4349 | // Add all operands to the new PHI. |
Chris Lattner | b589344 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 4350 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 4351 | Value *NewInVal = cast<Instruction>(PN.getIncomingValue(i))->getOperand(0); |
| 4352 | if (NewInVal != InVal) |
| 4353 | InVal = 0; |
| 4354 | NewPN->addIncoming(NewInVal, PN.getIncomingBlock(i)); |
| 4355 | } |
| 4356 | |
| 4357 | Value *PhiVal; |
| 4358 | if (InVal) { |
| 4359 | // The new PHI unions all of the same values together. This is really |
| 4360 | // common, so we handle it intelligently here for compile-time speed. |
| 4361 | PhiVal = InVal; |
| 4362 | delete NewPN; |
| 4363 | } else { |
| 4364 | InsertNewInstBefore(NewPN, PN); |
| 4365 | PhiVal = NewPN; |
| 4366 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4367 | |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 4368 | // Insert and return the new operation. |
| 4369 | if (isa<CastInst>(FirstInst)) |
Chris Lattner | b589344 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 4370 | return new CastInst(PhiVal, PN.getType()); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 4371 | else if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) |
Chris Lattner | b589344 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 4372 | return BinaryOperator::create(BinOp->getOpcode(), PhiVal, ConstantOp); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 4373 | else |
| 4374 | return new ShiftInst(cast<ShiftInst>(FirstInst)->getOpcode(), |
Chris Lattner | b589344 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 4375 | PhiVal, ConstantOp); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 4376 | } |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 4377 | |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 4378 | /// DeadPHICycle - Return true if this PHI node is only used by a PHI node cycle |
| 4379 | /// that is dead. |
| 4380 | static bool DeadPHICycle(PHINode *PN, std::set<PHINode*> &PotentiallyDeadPHIs) { |
| 4381 | if (PN->use_empty()) return true; |
| 4382 | if (!PN->hasOneUse()) return false; |
| 4383 | |
| 4384 | // Remember this node, and if we find the cycle, return. |
| 4385 | if (!PotentiallyDeadPHIs.insert(PN).second) |
| 4386 | return true; |
| 4387 | |
| 4388 | if (PHINode *PU = dyn_cast<PHINode>(PN->use_back())) |
| 4389 | return DeadPHICycle(PU, PotentiallyDeadPHIs); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4390 | |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 4391 | return false; |
| 4392 | } |
| 4393 | |
Chris Lattner | 473945d | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 4394 | // PHINode simplification |
| 4395 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4396 | Instruction *InstCombiner::visitPHINode(PHINode &PN) { |
Chris Lattner | c30bda7 | 2004-10-17 21:22:38 +0000 | [diff] [blame] | 4397 | if (Value *V = hasConstantValue(&PN)) { |
| 4398 | // If V is an instruction, we have to be certain that it dominates PN. |
| 4399 | // However, because we don't have dom info, we can't do a perfect job. |
| 4400 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
| 4401 | // We know that the instruction dominates the PHI if there are no undef |
| 4402 | // values coming in. |
Chris Lattner | 77bcee7 | 2004-10-18 01:48:31 +0000 | [diff] [blame] | 4403 | if (I->getParent() != &I->getParent()->getParent()->front() || |
| 4404 | isa<InvokeInst>(I)) |
Chris Lattner | ca45930 | 2004-10-17 21:31:34 +0000 | [diff] [blame] | 4405 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) |
| 4406 | if (isa<UndefValue>(PN.getIncomingValue(i))) { |
| 4407 | V = 0; |
| 4408 | break; |
| 4409 | } |
Chris Lattner | c30bda7 | 2004-10-17 21:22:38 +0000 | [diff] [blame] | 4410 | } |
| 4411 | |
| 4412 | if (V) |
| 4413 | return ReplaceInstUsesWith(PN, V); |
| 4414 | } |
Chris Lattner | 7059f2e | 2004-02-16 05:07:08 +0000 | [diff] [blame] | 4415 | |
| 4416 | // If the only user of this instruction is a cast instruction, and all of the |
| 4417 | // incoming values are constants, change this PHI to merge together the casted |
| 4418 | // constants. |
| 4419 | if (PN.hasOneUse()) |
| 4420 | if (CastInst *CI = dyn_cast<CastInst>(PN.use_back())) |
| 4421 | if (CI->getType() != PN.getType()) { // noop casts will be folded |
| 4422 | bool AllConstant = true; |
| 4423 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) |
| 4424 | if (!isa<Constant>(PN.getIncomingValue(i))) { |
| 4425 | AllConstant = false; |
| 4426 | break; |
| 4427 | } |
| 4428 | if (AllConstant) { |
| 4429 | // Make a new PHI with all casted values. |
| 4430 | PHINode *New = new PHINode(CI->getType(), PN.getName(), &PN); |
| 4431 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 4432 | Constant *OldArg = cast<Constant>(PN.getIncomingValue(i)); |
| 4433 | New->addIncoming(ConstantExpr::getCast(OldArg, New->getType()), |
| 4434 | PN.getIncomingBlock(i)); |
| 4435 | } |
| 4436 | |
| 4437 | // Update the cast instruction. |
| 4438 | CI->setOperand(0, New); |
| 4439 | WorkList.push_back(CI); // revisit the cast instruction to fold. |
| 4440 | WorkList.push_back(New); // Make sure to revisit the new Phi |
| 4441 | return &PN; // PN is now dead! |
| 4442 | } |
| 4443 | } |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 4444 | |
| 4445 | // If all PHI operands are the same operation, pull them through the PHI, |
| 4446 | // reducing code size. |
| 4447 | if (isa<Instruction>(PN.getIncomingValue(0)) && |
| 4448 | PN.getIncomingValue(0)->hasOneUse()) |
| 4449 | if (Instruction *Result = FoldPHIArgOpIntoPHI(PN)) |
| 4450 | return Result; |
| 4451 | |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 4452 | // If this is a trivial cycle in the PHI node graph, remove it. Basically, if |
| 4453 | // this PHI only has a single use (a PHI), and if that PHI only has one use (a |
| 4454 | // PHI)... break the cycle. |
| 4455 | if (PN.hasOneUse()) |
| 4456 | if (PHINode *PU = dyn_cast<PHINode>(PN.use_back())) { |
| 4457 | std::set<PHINode*> PotentiallyDeadPHIs; |
| 4458 | PotentiallyDeadPHIs.insert(&PN); |
| 4459 | if (DeadPHICycle(PU, PotentiallyDeadPHIs)) |
| 4460 | return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType())); |
| 4461 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4462 | |
Chris Lattner | 60921c9 | 2003-12-19 05:58:40 +0000 | [diff] [blame] | 4463 | return 0; |
Chris Lattner | 473945d | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 4464 | } |
| 4465 | |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 4466 | static Value *InsertSignExtendToPtrTy(Value *V, const Type *DTy, |
| 4467 | Instruction *InsertPoint, |
| 4468 | InstCombiner *IC) { |
| 4469 | unsigned PS = IC->getTargetData().getPointerSize(); |
| 4470 | const Type *VTy = V->getType(); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 4471 | if (!VTy->isSigned() && VTy->getPrimitiveSize() < PS) |
| 4472 | // We must insert a cast to ensure we sign-extend. |
| 4473 | V = IC->InsertNewInstBefore(new CastInst(V, VTy->getSignedVersion(), |
| 4474 | V->getName()), *InsertPoint); |
| 4475 | return IC->InsertNewInstBefore(new CastInst(V, DTy, V->getName()), |
| 4476 | *InsertPoint); |
| 4477 | } |
| 4478 | |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 4479 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4480 | Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 4481 | Value *PtrOp = GEP.getOperand(0); |
Chris Lattner | c54e2b8 | 2003-05-22 19:07:21 +0000 | [diff] [blame] | 4482 | // Is it 'getelementptr %P, long 0' or 'getelementptr %P' |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4483 | // If so, eliminate the noop. |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 4484 | if (GEP.getNumOperands() == 1) |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 4485 | return ReplaceInstUsesWith(GEP, PtrOp); |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 4486 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4487 | if (isa<UndefValue>(GEP.getOperand(0))) |
| 4488 | return ReplaceInstUsesWith(GEP, UndefValue::get(GEP.getType())); |
| 4489 | |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 4490 | bool HasZeroPointerIndex = false; |
| 4491 | if (Constant *C = dyn_cast<Constant>(GEP.getOperand(1))) |
| 4492 | HasZeroPointerIndex = C->isNullValue(); |
| 4493 | |
| 4494 | if (GEP.getNumOperands() == 2 && HasZeroPointerIndex) |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 4495 | return ReplaceInstUsesWith(GEP, PtrOp); |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 4496 | |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 4497 | // Eliminate unneeded casts for indices. |
| 4498 | bool MadeChange = false; |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 4499 | gep_type_iterator GTI = gep_type_begin(GEP); |
| 4500 | for (unsigned i = 1, e = GEP.getNumOperands(); i != e; ++i, ++GTI) |
| 4501 | if (isa<SequentialType>(*GTI)) { |
| 4502 | if (CastInst *CI = dyn_cast<CastInst>(GEP.getOperand(i))) { |
| 4503 | Value *Src = CI->getOperand(0); |
| 4504 | const Type *SrcTy = Src->getType(); |
| 4505 | const Type *DestTy = CI->getType(); |
| 4506 | if (Src->getType()->isInteger()) { |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4507 | if (SrcTy->getPrimitiveSizeInBits() == |
| 4508 | DestTy->getPrimitiveSizeInBits()) { |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 4509 | // We can always eliminate a cast from ulong or long to the other. |
| 4510 | // We can always eliminate a cast from uint to int or the other on |
| 4511 | // 32-bit pointer platforms. |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4512 | if (DestTy->getPrimitiveSizeInBits() >= TD->getPointerSizeInBits()){ |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 4513 | MadeChange = true; |
| 4514 | GEP.setOperand(i, Src); |
| 4515 | } |
| 4516 | } else if (SrcTy->getPrimitiveSize() < DestTy->getPrimitiveSize() && |
| 4517 | SrcTy->getPrimitiveSize() == 4) { |
| 4518 | // We can always eliminate a cast from int to [u]long. We can |
| 4519 | // eliminate a cast from uint to [u]long iff the target is a 32-bit |
| 4520 | // pointer target. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4521 | if (SrcTy->isSigned() || |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4522 | SrcTy->getPrimitiveSizeInBits() >= TD->getPointerSizeInBits()) { |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 4523 | MadeChange = true; |
| 4524 | GEP.setOperand(i, Src); |
| 4525 | } |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 4526 | } |
| 4527 | } |
| 4528 | } |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 4529 | // If we are using a wider index than needed for this platform, shrink it |
| 4530 | // to what we need. If the incoming value needs a cast instruction, |
| 4531 | // insert it. This explicit cast can make subsequent optimizations more |
| 4532 | // obvious. |
| 4533 | Value *Op = GEP.getOperand(i); |
| 4534 | if (Op->getType()->getPrimitiveSize() > TD->getPointerSize()) |
Chris Lattner | 4f1134e | 2004-04-17 18:16:10 +0000 | [diff] [blame] | 4535 | if (Constant *C = dyn_cast<Constant>(Op)) { |
Chris Lattner | 67769e5 | 2004-07-20 01:48:15 +0000 | [diff] [blame] | 4536 | GEP.setOperand(i, ConstantExpr::getCast(C, |
| 4537 | TD->getIntPtrType()->getSignedVersion())); |
Chris Lattner | 4f1134e | 2004-04-17 18:16:10 +0000 | [diff] [blame] | 4538 | MadeChange = true; |
| 4539 | } else { |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 4540 | Op = InsertNewInstBefore(new CastInst(Op, TD->getIntPtrType(), |
| 4541 | Op->getName()), GEP); |
| 4542 | GEP.setOperand(i, Op); |
| 4543 | MadeChange = true; |
| 4544 | } |
Chris Lattner | 67769e5 | 2004-07-20 01:48:15 +0000 | [diff] [blame] | 4545 | |
| 4546 | // If this is a constant idx, make sure to canonicalize it to be a signed |
| 4547 | // operand, otherwise CSE and other optimizations are pessimized. |
| 4548 | if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(Op)) { |
| 4549 | GEP.setOperand(i, ConstantExpr::getCast(CUI, |
| 4550 | CUI->getType()->getSignedVersion())); |
| 4551 | MadeChange = true; |
| 4552 | } |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 4553 | } |
| 4554 | if (MadeChange) return &GEP; |
| 4555 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 4556 | // Combine Indices - If the source pointer to this getelementptr instruction |
| 4557 | // is a getelementptr instruction, combine the indices of the two |
| 4558 | // getelementptr instructions into a single instruction. |
| 4559 | // |
Chris Lattner | ebd985c | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 4560 | std::vector<Value*> SrcGEPOperands; |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4561 | if (User *Src = dyn_castGetElementPtr(PtrOp)) |
Chris Lattner | ebd985c | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 4562 | SrcGEPOperands.assign(Src->op_begin(), Src->op_end()); |
Chris Lattner | ebd985c | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 4563 | |
| 4564 | if (!SrcGEPOperands.empty()) { |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 4565 | // Note that if our source is a gep chain itself that we wait for that |
| 4566 | // chain to be resolved before we perform this transformation. This |
| 4567 | // avoids us creating a TON of code in some cases. |
| 4568 | // |
| 4569 | if (isa<GetElementPtrInst>(SrcGEPOperands[0]) && |
| 4570 | cast<Instruction>(SrcGEPOperands[0])->getNumOperands() == 2) |
| 4571 | return 0; // Wait until our source is folded to completion. |
| 4572 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 4573 | std::vector<Value *> Indices; |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 4574 | |
| 4575 | // Find out whether the last index in the source GEP is a sequential idx. |
| 4576 | bool EndsWithSequential = false; |
| 4577 | for (gep_type_iterator I = gep_type_begin(*cast<User>(PtrOp)), |
| 4578 | E = gep_type_end(*cast<User>(PtrOp)); I != E; ++I) |
Chris Lattner | be97b4e | 2004-05-08 22:41:42 +0000 | [diff] [blame] | 4579 | EndsWithSequential = !isa<StructType>(*I); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4580 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 4581 | // Can we combine the two pointer arithmetics offsets? |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 4582 | if (EndsWithSequential) { |
Chris Lattner | decd081 | 2003-03-05 22:33:14 +0000 | [diff] [blame] | 4583 | // Replace: gep (gep %P, long B), long A, ... |
| 4584 | // With: T = long A+B; gep %P, T, ... |
| 4585 | // |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 4586 | Value *Sum, *SO1 = SrcGEPOperands.back(), *GO1 = GEP.getOperand(1); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 4587 | if (SO1 == Constant::getNullValue(SO1->getType())) { |
| 4588 | Sum = GO1; |
| 4589 | } else if (GO1 == Constant::getNullValue(GO1->getType())) { |
| 4590 | Sum = SO1; |
| 4591 | } else { |
| 4592 | // If they aren't the same type, convert both to an integer of the |
| 4593 | // target's pointer size. |
| 4594 | if (SO1->getType() != GO1->getType()) { |
| 4595 | if (Constant *SO1C = dyn_cast<Constant>(SO1)) { |
| 4596 | SO1 = ConstantExpr::getCast(SO1C, GO1->getType()); |
| 4597 | } else if (Constant *GO1C = dyn_cast<Constant>(GO1)) { |
| 4598 | GO1 = ConstantExpr::getCast(GO1C, SO1->getType()); |
| 4599 | } else { |
| 4600 | unsigned PS = TD->getPointerSize(); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 4601 | if (SO1->getType()->getPrimitiveSize() == PS) { |
| 4602 | // Convert GO1 to SO1's type. |
| 4603 | GO1 = InsertSignExtendToPtrTy(GO1, SO1->getType(), &GEP, this); |
| 4604 | |
| 4605 | } else if (GO1->getType()->getPrimitiveSize() == PS) { |
| 4606 | // Convert SO1 to GO1's type. |
| 4607 | SO1 = InsertSignExtendToPtrTy(SO1, GO1->getType(), &GEP, this); |
| 4608 | } else { |
| 4609 | const Type *PT = TD->getIntPtrType(); |
| 4610 | SO1 = InsertSignExtendToPtrTy(SO1, PT, &GEP, this); |
| 4611 | GO1 = InsertSignExtendToPtrTy(GO1, PT, &GEP, this); |
| 4612 | } |
| 4613 | } |
| 4614 | } |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 4615 | if (isa<Constant>(SO1) && isa<Constant>(GO1)) |
| 4616 | Sum = ConstantExpr::getAdd(cast<Constant>(SO1), cast<Constant>(GO1)); |
| 4617 | else { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4618 | Sum = BinaryOperator::createAdd(SO1, GO1, PtrOp->getName()+".sum"); |
| 4619 | InsertNewInstBefore(cast<Instruction>(Sum), GEP); |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 4620 | } |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 4621 | } |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 4622 | |
| 4623 | // Recycle the GEP we already have if possible. |
| 4624 | if (SrcGEPOperands.size() == 2) { |
| 4625 | GEP.setOperand(0, SrcGEPOperands[0]); |
| 4626 | GEP.setOperand(1, Sum); |
| 4627 | return &GEP; |
| 4628 | } else { |
| 4629 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, |
| 4630 | SrcGEPOperands.end()-1); |
| 4631 | Indices.push_back(Sum); |
| 4632 | Indices.insert(Indices.end(), GEP.op_begin()+2, GEP.op_end()); |
| 4633 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4634 | } else if (isa<Constant>(*GEP.idx_begin()) && |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 4635 | cast<Constant>(*GEP.idx_begin())->isNullValue() && |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4636 | SrcGEPOperands.size() != 1) { |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 4637 | // 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] | 4638 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, |
| 4639 | SrcGEPOperands.end()); |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 4640 | Indices.insert(Indices.end(), GEP.idx_begin()+1, GEP.idx_end()); |
| 4641 | } |
| 4642 | |
| 4643 | if (!Indices.empty()) |
Chris Lattner | ebd985c | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 4644 | return new GetElementPtrInst(SrcGEPOperands[0], Indices, GEP.getName()); |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 4645 | |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 4646 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(PtrOp)) { |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 4647 | // GEP of global variable. If all of the indices for this GEP are |
| 4648 | // constants, we can promote this to a constexpr instead of an instruction. |
| 4649 | |
| 4650 | // Scan for nonconstants... |
| 4651 | std::vector<Constant*> Indices; |
| 4652 | User::op_iterator I = GEP.idx_begin(), E = GEP.idx_end(); |
| 4653 | for (; I != E && isa<Constant>(*I); ++I) |
| 4654 | Indices.push_back(cast<Constant>(*I)); |
| 4655 | |
| 4656 | if (I == E) { // If they are all constants... |
Chris Lattner | 9db07b9 | 2004-07-18 18:59:44 +0000 | [diff] [blame] | 4657 | Constant *CE = ConstantExpr::getGetElementPtr(GV, Indices); |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 4658 | |
| 4659 | // Replace all uses of the GEP with the new constexpr... |
| 4660 | return ReplaceInstUsesWith(GEP, CE); |
| 4661 | } |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 4662 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(PtrOp)) { |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 4663 | if (CE->getOpcode() == Instruction::Cast) { |
| 4664 | if (HasZeroPointerIndex) { |
| 4665 | // transform: GEP (cast [10 x ubyte]* X to [0 x ubyte]*), long 0, ... |
| 4666 | // into : GEP [10 x ubyte]* X, long 0, ... |
| 4667 | // |
| 4668 | // This occurs when the program declares an array extern like "int X[];" |
| 4669 | // |
| 4670 | Constant *X = CE->getOperand(0); |
| 4671 | const PointerType *CPTy = cast<PointerType>(CE->getType()); |
| 4672 | if (const PointerType *XTy = dyn_cast<PointerType>(X->getType())) |
| 4673 | if (const ArrayType *XATy = |
| 4674 | dyn_cast<ArrayType>(XTy->getElementType())) |
| 4675 | if (const ArrayType *CATy = |
| 4676 | dyn_cast<ArrayType>(CPTy->getElementType())) |
| 4677 | if (CATy->getElementType() == XATy->getElementType()) { |
| 4678 | // At this point, we know that the cast source type is a pointer |
| 4679 | // to an array of the same type as the destination pointer |
| 4680 | // array. Because the array type is never stepped over (there |
| 4681 | // is a leading zero) we can fold the cast into this GEP. |
| 4682 | GEP.setOperand(0, X); |
| 4683 | return &GEP; |
| 4684 | } |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4685 | } else if (GEP.getNumOperands() == 2 && |
| 4686 | isa<PointerType>(CE->getOperand(0)->getType())) { |
Chris Lattner | 646641e | 2004-11-27 17:55:46 +0000 | [diff] [blame] | 4687 | // Transform things like: |
| 4688 | // %t = getelementptr ubyte* cast ([2 x sbyte]* %str to ubyte*), uint %V |
| 4689 | // into: %t1 = getelementptr [2 x sbyte*]* %str, int 0, uint %V; cast |
| 4690 | Constant *X = CE->getOperand(0); |
| 4691 | const Type *SrcElTy = cast<PointerType>(X->getType())->getElementType(); |
| 4692 | const Type *ResElTy =cast<PointerType>(CE->getType())->getElementType(); |
| 4693 | if (isa<ArrayType>(SrcElTy) && |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4694 | TD->getTypeSize(cast<ArrayType>(SrcElTy)->getElementType()) == |
Chris Lattner | 646641e | 2004-11-27 17:55:46 +0000 | [diff] [blame] | 4695 | TD->getTypeSize(ResElTy)) { |
| 4696 | Value *V = InsertNewInstBefore( |
| 4697 | new GetElementPtrInst(X, Constant::getNullValue(Type::IntTy), |
| 4698 | GEP.getOperand(1), GEP.getName()), GEP); |
| 4699 | return new CastInst(V, GEP.getType()); |
| 4700 | } |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 4701 | } |
| 4702 | } |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 4703 | } |
| 4704 | |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 4705 | return 0; |
| 4706 | } |
| 4707 | |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 4708 | Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) { |
| 4709 | // Convert: malloc Ty, C - where C is a constant != 1 into: malloc [C x Ty], 1 |
| 4710 | if (AI.isArrayAllocation()) // Check C != 1 |
| 4711 | if (const ConstantUInt *C = dyn_cast<ConstantUInt>(AI.getArraySize())) { |
| 4712 | const Type *NewTy = ArrayType::get(AI.getAllocatedType(), C->getValue()); |
Chris Lattner | 0006bd7 | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 4713 | AllocationInst *New = 0; |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 4714 | |
| 4715 | // Create and insert the replacement instruction... |
| 4716 | if (isa<MallocInst>(AI)) |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 4717 | New = new MallocInst(NewTy, 0, AI.getName()); |
Chris Lattner | 0006bd7 | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 4718 | else { |
| 4719 | assert(isa<AllocaInst>(AI) && "Unknown type of allocation inst!"); |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 4720 | New = new AllocaInst(NewTy, 0, AI.getName()); |
Chris Lattner | 0006bd7 | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 4721 | } |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 4722 | |
| 4723 | InsertNewInstBefore(New, AI); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4724 | |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 4725 | // Scan to the end of the allocation instructions, to skip over a block of |
| 4726 | // allocas if possible... |
| 4727 | // |
| 4728 | BasicBlock::iterator It = New; |
| 4729 | while (isa<AllocationInst>(*It)) ++It; |
| 4730 | |
| 4731 | // Now that I is pointing to the first non-allocation-inst in the block, |
| 4732 | // insert our getelementptr instruction... |
| 4733 | // |
Chris Lattner | 693787a | 2005-05-04 19:10:26 +0000 | [diff] [blame] | 4734 | Value *NullIdx = Constant::getNullValue(Type::IntTy); |
| 4735 | Value *V = new GetElementPtrInst(New, NullIdx, NullIdx, |
| 4736 | New->getName()+".sub", It); |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 4737 | |
| 4738 | // Now make everything use the getelementptr instead of the original |
| 4739 | // allocation. |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 4740 | return ReplaceInstUsesWith(AI, V); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4741 | } else if (isa<UndefValue>(AI.getArraySize())) { |
| 4742 | return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 4743 | } |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 4744 | |
| 4745 | // If alloca'ing a zero byte object, replace the alloca with a null pointer. |
| 4746 | // Note that we only do this for alloca's, because malloc should allocate and |
| 4747 | // return a unique pointer, even for a zero byte allocation. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4748 | if (isa<AllocaInst>(AI) && AI.getAllocatedType()->isSized() && |
Chris Lattner | cf27afb | 2004-07-02 22:55:47 +0000 | [diff] [blame] | 4749 | TD->getTypeSize(AI.getAllocatedType()) == 0) |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 4750 | return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); |
| 4751 | |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 4752 | return 0; |
| 4753 | } |
| 4754 | |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 4755 | Instruction *InstCombiner::visitFreeInst(FreeInst &FI) { |
| 4756 | Value *Op = FI.getOperand(0); |
| 4757 | |
| 4758 | // Change free <ty>* (cast <ty2>* X to <ty>*) into free <ty2>* X |
| 4759 | if (CastInst *CI = dyn_cast<CastInst>(Op)) |
| 4760 | if (isa<PointerType>(CI->getOperand(0)->getType())) { |
| 4761 | FI.setOperand(0, CI->getOperand(0)); |
| 4762 | return &FI; |
| 4763 | } |
| 4764 | |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 4765 | // free undef -> unreachable. |
| 4766 | if (isa<UndefValue>(Op)) { |
| 4767 | // Insert a new store to null because we cannot modify the CFG here. |
| 4768 | new StoreInst(ConstantBool::True, |
| 4769 | UndefValue::get(PointerType::get(Type::BoolTy)), &FI); |
| 4770 | return EraseInstFromFunction(FI); |
| 4771 | } |
| 4772 | |
Chris Lattner | 6160e85 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 4773 | // If we have 'free null' delete the instruction. This can happen in stl code |
| 4774 | // when lots of inlining happens. |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 4775 | if (isa<ConstantPointerNull>(Op)) |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 4776 | return EraseInstFromFunction(FI); |
Chris Lattner | 6160e85 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 4777 | |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 4778 | return 0; |
| 4779 | } |
| 4780 | |
| 4781 | |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 4782 | /// GetGEPGlobalInitializer - Given a constant, and a getelementptr |
| 4783 | /// constantexpr, return the constant value being addressed by the constant |
| 4784 | /// expression, or null if something is funny. |
| 4785 | /// |
| 4786 | static Constant *GetGEPGlobalInitializer(Constant *C, ConstantExpr *CE) { |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 4787 | if (CE->getOperand(1) != Constant::getNullValue(CE->getOperand(1)->getType())) |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 4788 | return 0; // Do not allow stepping over the value! |
| 4789 | |
| 4790 | // Loop over all of the operands, tracking down which value we are |
| 4791 | // addressing... |
Chris Lattner | e1368ae | 2004-05-27 17:30:27 +0000 | [diff] [blame] | 4792 | gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE); |
| 4793 | for (++I; I != E; ++I) |
| 4794 | if (const StructType *STy = dyn_cast<StructType>(*I)) { |
| 4795 | ConstantUInt *CU = cast<ConstantUInt>(I.getOperand()); |
| 4796 | assert(CU->getValue() < STy->getNumElements() && |
| 4797 | "Struct index out of range!"); |
Chris Lattner | 652f3cf | 2005-01-08 19:42:22 +0000 | [diff] [blame] | 4798 | unsigned El = (unsigned)CU->getValue(); |
Chris Lattner | e1368ae | 2004-05-27 17:30:27 +0000 | [diff] [blame] | 4799 | if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) { |
Chris Lattner | 652f3cf | 2005-01-08 19:42:22 +0000 | [diff] [blame] | 4800 | C = CS->getOperand(El); |
Chris Lattner | e1368ae | 2004-05-27 17:30:27 +0000 | [diff] [blame] | 4801 | } else if (isa<ConstantAggregateZero>(C)) { |
Jeff Cohen | 9d80930 | 2005-04-23 21:38:35 +0000 | [diff] [blame] | 4802 | C = Constant::getNullValue(STy->getElementType(El)); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4803 | } else if (isa<UndefValue>(C)) { |
Jeff Cohen | 9d80930 | 2005-04-23 21:38:35 +0000 | [diff] [blame] | 4804 | C = UndefValue::get(STy->getElementType(El)); |
Chris Lattner | e1368ae | 2004-05-27 17:30:27 +0000 | [diff] [blame] | 4805 | } else { |
| 4806 | return 0; |
| 4807 | } |
| 4808 | } else if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand())) { |
| 4809 | const ArrayType *ATy = cast<ArrayType>(*I); |
| 4810 | if ((uint64_t)CI->getRawValue() >= ATy->getNumElements()) return 0; |
| 4811 | if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) |
Chris Lattner | 652f3cf | 2005-01-08 19:42:22 +0000 | [diff] [blame] | 4812 | C = CA->getOperand((unsigned)CI->getRawValue()); |
Chris Lattner | e1368ae | 2004-05-27 17:30:27 +0000 | [diff] [blame] | 4813 | else if (isa<ConstantAggregateZero>(C)) |
| 4814 | C = Constant::getNullValue(ATy->getElementType()); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4815 | else if (isa<UndefValue>(C)) |
| 4816 | C = UndefValue::get(ATy->getElementType()); |
Chris Lattner | e1368ae | 2004-05-27 17:30:27 +0000 | [diff] [blame] | 4817 | else |
| 4818 | return 0; |
| 4819 | } else { |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 4820 | return 0; |
Chris Lattner | e1368ae | 2004-05-27 17:30:27 +0000 | [diff] [blame] | 4821 | } |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 4822 | return C; |
| 4823 | } |
| 4824 | |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 4825 | /// InstCombineLoadCast - Fold 'load (cast P)' -> cast (load P)' when possible. |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 4826 | static Instruction *InstCombineLoadCast(InstCombiner &IC, LoadInst &LI) { |
| 4827 | User *CI = cast<User>(LI.getOperand(0)); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 4828 | Value *CastOp = CI->getOperand(0); |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 4829 | |
| 4830 | const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType(); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 4831 | if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) { |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 4832 | const Type *SrcPTy = SrcTy->getElementType(); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 4833 | |
| 4834 | if (DestPTy->isInteger() || isa<PointerType>(DestPTy)) { |
| 4835 | // If the source is an array, the code below will not succeed. Check to |
| 4836 | // see if a trivial 'gep P, 0, 0' will help matters. Only do this for |
| 4837 | // constants. |
| 4838 | if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy)) |
| 4839 | if (Constant *CSrc = dyn_cast<Constant>(CastOp)) |
| 4840 | if (ASrcTy->getNumElements() != 0) { |
| 4841 | std::vector<Value*> Idxs(2, Constant::getNullValue(Type::IntTy)); |
| 4842 | CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs); |
| 4843 | SrcTy = cast<PointerType>(CastOp->getType()); |
| 4844 | SrcPTy = SrcTy->getElementType(); |
| 4845 | } |
| 4846 | |
| 4847 | if ((SrcPTy->isInteger() || isa<PointerType>(SrcPTy)) && |
Chris Lattner | b1515fe | 2005-03-29 06:37:47 +0000 | [diff] [blame] | 4848 | // Do not allow turning this into a load of an integer, which is then |
| 4849 | // casted to a pointer, this pessimizes pointer analysis a lot. |
| 4850 | (isa<PointerType>(SrcPTy) == isa<PointerType>(LI.getType())) && |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4851 | IC.getTargetData().getTypeSize(SrcPTy) == |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 4852 | IC.getTargetData().getTypeSize(DestPTy)) { |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4853 | |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 4854 | // Okay, we are casting from one integer or pointer type to another of |
| 4855 | // the same size. Instead of casting the pointer before the load, cast |
| 4856 | // the result of the loaded value. |
| 4857 | Value *NewLoad = IC.InsertNewInstBefore(new LoadInst(CastOp, |
| 4858 | CI->getName(), |
| 4859 | LI.isVolatile()),LI); |
| 4860 | // Now cast the result of the load. |
| 4861 | return new CastInst(NewLoad, LI.getType()); |
| 4862 | } |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 4863 | } |
| 4864 | } |
| 4865 | return 0; |
| 4866 | } |
| 4867 | |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 4868 | /// isSafeToLoadUnconditionally - Return true if we know that executing a load |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 4869 | /// from this value cannot trap. If it is not obviously safe to load from the |
| 4870 | /// specified pointer, we do a quick local scan of the basic block containing |
| 4871 | /// ScanFrom, to determine if the address is already accessed. |
| 4872 | static bool isSafeToLoadUnconditionally(Value *V, Instruction *ScanFrom) { |
| 4873 | // If it is an alloca or global variable, it is always safe to load from. |
| 4874 | if (isa<AllocaInst>(V) || isa<GlobalVariable>(V)) return true; |
| 4875 | |
| 4876 | // Otherwise, be a little bit agressive by scanning the local block where we |
| 4877 | // 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] | 4878 | // from/to. If so, the previous load or store would have already trapped, |
| 4879 | // so there is no harm doing an extra load (also, CSE will later eliminate |
| 4880 | // the load entirely). |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 4881 | BasicBlock::iterator BBI = ScanFrom, E = ScanFrom->getParent()->begin(); |
| 4882 | |
Alkis Evlogimenos | 7b6ec60 | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 4883 | while (BBI != E) { |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 4884 | --BBI; |
| 4885 | |
| 4886 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { |
| 4887 | if (LI->getOperand(0) == V) return true; |
| 4888 | } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) |
| 4889 | if (SI->getOperand(1) == V) return true; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4890 | |
Alkis Evlogimenos | 7b6ec60 | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 4891 | } |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 4892 | return false; |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 4893 | } |
| 4894 | |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 4895 | Instruction *InstCombiner::visitLoadInst(LoadInst &LI) { |
| 4896 | Value *Op = LI.getOperand(0); |
Chris Lattner | 5f16a13 | 2004-01-12 04:13:56 +0000 | [diff] [blame] | 4897 | |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 4898 | // load (cast X) --> cast (load X) iff safe |
| 4899 | if (CastInst *CI = dyn_cast<CastInst>(Op)) |
| 4900 | if (Instruction *Res = InstCombineLoadCast(*this, LI)) |
| 4901 | return Res; |
| 4902 | |
| 4903 | // None of the following transforms are legal for volatile loads. |
| 4904 | if (LI.isVolatile()) return 0; |
| 4905 | |
| 4906 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) |
| 4907 | if (isa<ConstantPointerNull>(GEPI->getOperand(0)) || |
| 4908 | isa<UndefValue>(GEPI->getOperand(0))) { |
| 4909 | // Insert a new store to null instruction before the load to indicate |
| 4910 | // that this code is not reachable. We do this instead of inserting |
| 4911 | // an unreachable instruction directly because we cannot modify the |
| 4912 | // CFG. |
| 4913 | new StoreInst(UndefValue::get(LI.getType()), |
| 4914 | Constant::getNullValue(Op->getType()), &LI); |
| 4915 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
| 4916 | } |
| 4917 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4918 | if (Constant *C = dyn_cast<Constant>(Op)) { |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 4919 | // load null/undef -> undef |
| 4920 | if ((C->isNullValue() || isa<UndefValue>(C))) { |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 4921 | // Insert a new store to null instruction before the load to indicate that |
| 4922 | // this code is not reachable. We do this instead of inserting an |
| 4923 | // unreachable instruction directly because we cannot modify the CFG. |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 4924 | new StoreInst(UndefValue::get(LI.getType()), |
| 4925 | Constant::getNullValue(Op->getType()), &LI); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4926 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 4927 | } |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 4928 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4929 | // Instcombine load (constant global) into the value loaded. |
| 4930 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op)) |
| 4931 | if (GV->isConstant() && !GV->isExternal()) |
| 4932 | return ReplaceInstUsesWith(LI, GV->getInitializer()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4933 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4934 | // Instcombine load (constantexpr_GEP global, 0, ...) into the value loaded. |
| 4935 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op)) |
| 4936 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 4937 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0))) |
| 4938 | if (GV->isConstant() && !GV->isExternal()) |
| 4939 | if (Constant *V = GetGEPGlobalInitializer(GV->getInitializer(), CE)) |
| 4940 | return ReplaceInstUsesWith(LI, V); |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 4941 | if (CE->getOperand(0)->isNullValue()) { |
| 4942 | // Insert a new store to null instruction before the load to indicate |
| 4943 | // that this code is not reachable. We do this instead of inserting |
| 4944 | // an unreachable instruction directly because we cannot modify the |
| 4945 | // CFG. |
| 4946 | new StoreInst(UndefValue::get(LI.getType()), |
| 4947 | Constant::getNullValue(Op->getType()), &LI); |
| 4948 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
| 4949 | } |
| 4950 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4951 | } else if (CE->getOpcode() == Instruction::Cast) { |
| 4952 | if (Instruction *Res = InstCombineLoadCast(*this, LI)) |
| 4953 | return Res; |
| 4954 | } |
| 4955 | } |
Chris Lattner | f499eac | 2004-04-08 20:39:49 +0000 | [diff] [blame] | 4956 | |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 4957 | if (Op->hasOneUse()) { |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 4958 | // Change select and PHI nodes to select values instead of addresses: this |
| 4959 | // helps alias analysis out a lot, allows many others simplifications, and |
| 4960 | // exposes redundancy in the code. |
| 4961 | // |
| 4962 | // Note that we cannot do the transformation unless we know that the |
| 4963 | // introduced loads cannot trap! Something like this is valid as long as |
| 4964 | // the condition is always false: load (select bool %C, int* null, int* %G), |
| 4965 | // but it would not be valid if we transformed it to load from null |
| 4966 | // unconditionally. |
| 4967 | // |
| 4968 | if (SelectInst *SI = dyn_cast<SelectInst>(Op)) { |
| 4969 | // load (select (Cond, &V1, &V2)) --> select(Cond, load &V1, load &V2). |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 4970 | if (isSafeToLoadUnconditionally(SI->getOperand(1), SI) && |
| 4971 | isSafeToLoadUnconditionally(SI->getOperand(2), SI)) { |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 4972 | Value *V1 = InsertNewInstBefore(new LoadInst(SI->getOperand(1), |
Chris Lattner | 79f0c8e | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 4973 | SI->getOperand(1)->getName()+".val"), LI); |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 4974 | Value *V2 = InsertNewInstBefore(new LoadInst(SI->getOperand(2), |
Chris Lattner | 79f0c8e | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 4975 | SI->getOperand(2)->getName()+".val"), LI); |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 4976 | return new SelectInst(SI->getCondition(), V1, V2); |
| 4977 | } |
| 4978 | |
Chris Lattner | 684fe21 | 2004-09-23 15:46:00 +0000 | [diff] [blame] | 4979 | // load (select (cond, null, P)) -> load P |
| 4980 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(1))) |
| 4981 | if (C->isNullValue()) { |
| 4982 | LI.setOperand(0, SI->getOperand(2)); |
| 4983 | return &LI; |
| 4984 | } |
| 4985 | |
| 4986 | // load (select (cond, P, null)) -> load P |
| 4987 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(2))) |
| 4988 | if (C->isNullValue()) { |
| 4989 | LI.setOperand(0, SI->getOperand(1)); |
| 4990 | return &LI; |
| 4991 | } |
| 4992 | |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 4993 | } else if (PHINode *PN = dyn_cast<PHINode>(Op)) { |
| 4994 | // load (phi (&V1, &V2, &V3)) --> phi(load &V1, load &V2, load &V3) |
Chris Lattner | 79f0c8e | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 4995 | bool Safe = PN->getParent() == LI.getParent(); |
| 4996 | |
| 4997 | // Scan all of the instructions between the PHI and the load to make |
| 4998 | // sure there are no instructions that might possibly alter the value |
| 4999 | // loaded from the PHI. |
| 5000 | if (Safe) { |
| 5001 | BasicBlock::iterator I = &LI; |
| 5002 | for (--I; !isa<PHINode>(I); --I) |
| 5003 | if (isa<StoreInst>(I) || isa<CallInst>(I)) { |
| 5004 | Safe = false; |
| 5005 | break; |
| 5006 | } |
| 5007 | } |
| 5008 | |
| 5009 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e && Safe; ++i) |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 5010 | if (!isSafeToLoadUnconditionally(PN->getIncomingValue(i), |
Chris Lattner | 79f0c8e | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 5011 | PN->getIncomingBlock(i)->getTerminator())) |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 5012 | Safe = false; |
Chris Lattner | 79f0c8e | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 5013 | |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 5014 | if (Safe) { |
| 5015 | // Create the PHI. |
| 5016 | PHINode *NewPN = new PHINode(LI.getType(), PN->getName()); |
| 5017 | InsertNewInstBefore(NewPN, *PN); |
| 5018 | std::map<BasicBlock*,Value*> LoadMap; // Don't insert duplicate loads |
| 5019 | |
| 5020 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 5021 | BasicBlock *BB = PN->getIncomingBlock(i); |
| 5022 | Value *&TheLoad = LoadMap[BB]; |
| 5023 | if (TheLoad == 0) { |
| 5024 | Value *InVal = PN->getIncomingValue(i); |
| 5025 | TheLoad = InsertNewInstBefore(new LoadInst(InVal, |
| 5026 | InVal->getName()+".val"), |
| 5027 | *BB->getTerminator()); |
| 5028 | } |
| 5029 | NewPN->addIncoming(TheLoad, BB); |
| 5030 | } |
| 5031 | return ReplaceInstUsesWith(LI, NewPN); |
| 5032 | } |
| 5033 | } |
| 5034 | } |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 5035 | return 0; |
| 5036 | } |
| 5037 | |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 5038 | /// InstCombineStoreToCast - Fold 'store V, (cast P)' -> store (cast V), P' |
| 5039 | /// when possible. |
| 5040 | static Instruction *InstCombineStoreToCast(InstCombiner &IC, StoreInst &SI) { |
| 5041 | User *CI = cast<User>(SI.getOperand(1)); |
| 5042 | Value *CastOp = CI->getOperand(0); |
| 5043 | |
| 5044 | const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType(); |
| 5045 | if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) { |
| 5046 | const Type *SrcPTy = SrcTy->getElementType(); |
| 5047 | |
| 5048 | if (DestPTy->isInteger() || isa<PointerType>(DestPTy)) { |
| 5049 | // If the source is an array, the code below will not succeed. Check to |
| 5050 | // see if a trivial 'gep P, 0, 0' will help matters. Only do this for |
| 5051 | // constants. |
| 5052 | if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy)) |
| 5053 | if (Constant *CSrc = dyn_cast<Constant>(CastOp)) |
| 5054 | if (ASrcTy->getNumElements() != 0) { |
| 5055 | std::vector<Value*> Idxs(2, Constant::getNullValue(Type::IntTy)); |
| 5056 | CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs); |
| 5057 | SrcTy = cast<PointerType>(CastOp->getType()); |
| 5058 | SrcPTy = SrcTy->getElementType(); |
| 5059 | } |
| 5060 | |
| 5061 | if ((SrcPTy->isInteger() || isa<PointerType>(SrcPTy)) && |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5062 | IC.getTargetData().getTypeSize(SrcPTy) == |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 5063 | IC.getTargetData().getTypeSize(DestPTy)) { |
| 5064 | |
| 5065 | // Okay, we are casting from one integer or pointer type to another of |
| 5066 | // the same size. Instead of casting the pointer before the store, cast |
| 5067 | // the value to be stored. |
| 5068 | Value *NewCast; |
| 5069 | if (Constant *C = dyn_cast<Constant>(SI.getOperand(0))) |
| 5070 | NewCast = ConstantExpr::getCast(C, SrcPTy); |
| 5071 | else |
| 5072 | NewCast = IC.InsertNewInstBefore(new CastInst(SI.getOperand(0), |
| 5073 | SrcPTy, |
| 5074 | SI.getOperand(0)->getName()+".c"), SI); |
| 5075 | |
| 5076 | return new StoreInst(NewCast, CastOp); |
| 5077 | } |
| 5078 | } |
| 5079 | } |
| 5080 | return 0; |
| 5081 | } |
| 5082 | |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 5083 | Instruction *InstCombiner::visitStoreInst(StoreInst &SI) { |
| 5084 | Value *Val = SI.getOperand(0); |
| 5085 | Value *Ptr = SI.getOperand(1); |
| 5086 | |
| 5087 | if (isa<UndefValue>(Ptr)) { // store X, undef -> noop (even if volatile) |
| 5088 | removeFromWorkList(&SI); |
| 5089 | SI.eraseFromParent(); |
| 5090 | ++NumCombined; |
| 5091 | return 0; |
| 5092 | } |
| 5093 | |
| 5094 | if (SI.isVolatile()) return 0; // Don't hack volatile loads. |
| 5095 | |
| 5096 | // store X, null -> turns into 'unreachable' in SimplifyCFG |
| 5097 | if (isa<ConstantPointerNull>(Ptr)) { |
| 5098 | if (!isa<UndefValue>(Val)) { |
| 5099 | SI.setOperand(0, UndefValue::get(Val->getType())); |
| 5100 | if (Instruction *U = dyn_cast<Instruction>(Val)) |
| 5101 | WorkList.push_back(U); // Dropped a use. |
| 5102 | ++NumCombined; |
| 5103 | } |
| 5104 | return 0; // Do not modify these! |
| 5105 | } |
| 5106 | |
| 5107 | // store undef, Ptr -> noop |
| 5108 | if (isa<UndefValue>(Val)) { |
| 5109 | removeFromWorkList(&SI); |
| 5110 | SI.eraseFromParent(); |
| 5111 | ++NumCombined; |
| 5112 | return 0; |
| 5113 | } |
| 5114 | |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 5115 | // If the pointer destination is a cast, see if we can fold the cast into the |
| 5116 | // source instead. |
| 5117 | if (CastInst *CI = dyn_cast<CastInst>(Ptr)) |
| 5118 | if (Instruction *Res = InstCombineStoreToCast(*this, SI)) |
| 5119 | return Res; |
| 5120 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) |
| 5121 | if (CE->getOpcode() == Instruction::Cast) |
| 5122 | if (Instruction *Res = InstCombineStoreToCast(*this, SI)) |
| 5123 | return Res; |
| 5124 | |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 5125 | return 0; |
| 5126 | } |
| 5127 | |
| 5128 | |
Chris Lattner | c4d10eb | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 5129 | Instruction *InstCombiner::visitBranchInst(BranchInst &BI) { |
| 5130 | // Change br (not X), label True, label False to: br X, label False, True |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 5131 | Value *X; |
| 5132 | BasicBlock *TrueDest; |
| 5133 | BasicBlock *FalseDest; |
| 5134 | if (match(&BI, m_Br(m_Not(m_Value(X)), TrueDest, FalseDest)) && |
| 5135 | !isa<Constant>(X)) { |
| 5136 | // Swap Destinations and condition... |
| 5137 | BI.setCondition(X); |
| 5138 | BI.setSuccessor(0, FalseDest); |
| 5139 | BI.setSuccessor(1, TrueDest); |
| 5140 | return &BI; |
| 5141 | } |
| 5142 | |
| 5143 | // Cannonicalize setne -> seteq |
| 5144 | Instruction::BinaryOps Op; Value *Y; |
| 5145 | if (match(&BI, m_Br(m_SetCond(Op, m_Value(X), m_Value(Y)), |
| 5146 | TrueDest, FalseDest))) |
| 5147 | if ((Op == Instruction::SetNE || Op == Instruction::SetLE || |
| 5148 | Op == Instruction::SetGE) && BI.getCondition()->hasOneUse()) { |
| 5149 | SetCondInst *I = cast<SetCondInst>(BI.getCondition()); |
| 5150 | std::string Name = I->getName(); I->setName(""); |
| 5151 | Instruction::BinaryOps NewOpcode = SetCondInst::getInverseCondition(Op); |
| 5152 | Value *NewSCC = BinaryOperator::create(NewOpcode, X, Y, Name, I); |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 5153 | // Swap Destinations and condition... |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 5154 | BI.setCondition(NewSCC); |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 5155 | BI.setSuccessor(0, FalseDest); |
| 5156 | BI.setSuccessor(1, TrueDest); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 5157 | removeFromWorkList(I); |
| 5158 | I->getParent()->getInstList().erase(I); |
| 5159 | WorkList.push_back(cast<Instruction>(NewSCC)); |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 5160 | return &BI; |
| 5161 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5162 | |
Chris Lattner | c4d10eb | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 5163 | return 0; |
| 5164 | } |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 5165 | |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 5166 | Instruction *InstCombiner::visitSwitchInst(SwitchInst &SI) { |
| 5167 | Value *Cond = SI.getCondition(); |
| 5168 | if (Instruction *I = dyn_cast<Instruction>(Cond)) { |
| 5169 | if (I->getOpcode() == Instruction::Add) |
| 5170 | if (ConstantInt *AddRHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 5171 | // change 'switch (X+4) case 1:' into 'switch (X) case -3' |
| 5172 | for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5173 | SI.setOperand(i,ConstantExpr::getSub(cast<Constant>(SI.getOperand(i)), |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 5174 | AddRHS)); |
| 5175 | SI.setOperand(0, I->getOperand(0)); |
| 5176 | WorkList.push_back(I); |
| 5177 | return &SI; |
| 5178 | } |
| 5179 | } |
| 5180 | return 0; |
| 5181 | } |
| 5182 | |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 5183 | |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 5184 | void InstCombiner::removeFromWorkList(Instruction *I) { |
| 5185 | WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), I), |
| 5186 | WorkList.end()); |
| 5187 | } |
| 5188 | |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 5189 | |
| 5190 | /// TryToSinkInstruction - Try to move the specified instruction from its |
| 5191 | /// current block into the beginning of DestBlock, which can only happen if it's |
| 5192 | /// safe to move the instruction past all of the instructions between it and the |
| 5193 | /// end of its block. |
| 5194 | static bool TryToSinkInstruction(Instruction *I, BasicBlock *DestBlock) { |
| 5195 | assert(I->hasOneUse() && "Invariants didn't hold!"); |
| 5196 | |
| 5197 | // Cannot move control-flow-involving instructions. |
| 5198 | if (isa<PHINode>(I) || isa<InvokeInst>(I) || isa<CallInst>(I)) return false; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5199 | |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 5200 | // Do not sink alloca instructions out of the entry block. |
| 5201 | if (isa<AllocaInst>(I) && I->getParent() == &DestBlock->getParent()->front()) |
| 5202 | return false; |
| 5203 | |
Chris Lattner | 96a52a6 | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 5204 | // We can only sink load instructions if there is nothing between the load and |
| 5205 | // the end of block that could change the value. |
| 5206 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
| 5207 | if (LI->isVolatile()) return false; // Don't sink volatile loads. |
| 5208 | |
| 5209 | for (BasicBlock::iterator Scan = LI, E = LI->getParent()->end(); |
| 5210 | Scan != E; ++Scan) |
| 5211 | if (Scan->mayWriteToMemory()) |
| 5212 | return false; |
Chris Lattner | 96a52a6 | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 5213 | } |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 5214 | |
| 5215 | BasicBlock::iterator InsertPos = DestBlock->begin(); |
| 5216 | while (isa<PHINode>(InsertPos)) ++InsertPos; |
| 5217 | |
| 5218 | BasicBlock *SrcBlock = I->getParent(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5219 | DestBlock->getInstList().splice(InsertPos, SrcBlock->getInstList(), I); |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 5220 | ++NumSunkInst; |
| 5221 | return true; |
| 5222 | } |
| 5223 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 5224 | bool InstCombiner::runOnFunction(Function &F) { |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 5225 | bool Changed = false; |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 5226 | TD = &getAnalysis<TargetData>(); |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 5227 | |
Chris Lattner | 216d4d8 | 2004-05-01 23:19:52 +0000 | [diff] [blame] | 5228 | for (inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i) |
| 5229 | WorkList.push_back(&*i); |
Chris Lattner | 6ffe551 | 2004-04-27 15:13:33 +0000 | [diff] [blame] | 5230 | |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 5231 | |
| 5232 | while (!WorkList.empty()) { |
| 5233 | Instruction *I = WorkList.back(); // Get an instruction from the worklist |
| 5234 | WorkList.pop_back(); |
| 5235 | |
Misha Brukman | a3bbcb5 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 5236 | // Check to see if we can DCE or ConstantPropagate the instruction... |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 5237 | // Check to see if we can DIE the instruction... |
| 5238 | if (isInstructionTriviallyDead(I)) { |
| 5239 | // Add operands to the worklist... |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 5240 | if (I->getNumOperands() < 4) |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 5241 | AddUsesToWorkList(*I); |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 5242 | ++NumDeadInst; |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 5243 | |
Chris Lattner | ad5fec1 | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 5244 | DEBUG(std::cerr << "IC: DCE: " << *I); |
| 5245 | |
| 5246 | I->eraseFromParent(); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 5247 | removeFromWorkList(I); |
| 5248 | continue; |
| 5249 | } |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 5250 | |
Misha Brukman | a3bbcb5 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 5251 | // Instruction isn't dead, see if we can constant propagate it... |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 5252 | if (Constant *C = ConstantFoldInstruction(I)) { |
Alkis Evlogimenos | 54a96a2 | 2004-12-08 23:10:30 +0000 | [diff] [blame] | 5253 | Value* Ptr = I->getOperand(0); |
Chris Lattner | 061718c | 2004-10-16 19:44:59 +0000 | [diff] [blame] | 5254 | if (isa<GetElementPtrInst>(I) && |
Alkis Evlogimenos | 54a96a2 | 2004-12-08 23:10:30 +0000 | [diff] [blame] | 5255 | cast<Constant>(Ptr)->isNullValue() && |
| 5256 | !isa<ConstantPointerNull>(C) && |
| 5257 | cast<PointerType>(Ptr->getType())->getElementType()->isSized()) { |
Chris Lattner | 061718c | 2004-10-16 19:44:59 +0000 | [diff] [blame] | 5258 | // If this is a constant expr gep that is effectively computing an |
| 5259 | // "offsetof", fold it into 'cast int X to T*' instead of 'gep 0, 0, 12' |
| 5260 | bool isFoldableGEP = true; |
| 5261 | for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i) |
| 5262 | if (!isa<ConstantInt>(I->getOperand(i))) |
| 5263 | isFoldableGEP = false; |
| 5264 | if (isFoldableGEP) { |
Alkis Evlogimenos | 54a96a2 | 2004-12-08 23:10:30 +0000 | [diff] [blame] | 5265 | uint64_t Offset = TD->getIndexedOffset(Ptr->getType(), |
Chris Lattner | 061718c | 2004-10-16 19:44:59 +0000 | [diff] [blame] | 5266 | std::vector<Value*>(I->op_begin()+1, I->op_end())); |
| 5267 | C = ConstantUInt::get(Type::ULongTy, Offset); |
Chris Lattner | 6e758ae | 2004-10-16 19:46:33 +0000 | [diff] [blame] | 5268 | C = ConstantExpr::getCast(C, TD->getIntPtrType()); |
Chris Lattner | 061718c | 2004-10-16 19:44:59 +0000 | [diff] [blame] | 5269 | C = ConstantExpr::getCast(C, I->getType()); |
| 5270 | } |
| 5271 | } |
| 5272 | |
Chris Lattner | ad5fec1 | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 5273 | DEBUG(std::cerr << "IC: ConstFold to: " << *C << " from: " << *I); |
| 5274 | |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 5275 | // Add operands to the worklist... |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 5276 | AddUsesToWorkList(*I); |
Chris Lattner | c736d56 | 2002-12-05 22:41:53 +0000 | [diff] [blame] | 5277 | ReplaceInstUsesWith(*I, C); |
| 5278 | |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 5279 | ++NumConstProp; |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 5280 | I->getParent()->getInstList().erase(I); |
Chris Lattner | 6061000 | 2003-10-07 15:17:02 +0000 | [diff] [blame] | 5281 | removeFromWorkList(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 5282 | continue; |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 5283 | } |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 5284 | |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 5285 | // See if we can trivially sink this instruction to a successor basic block. |
| 5286 | if (I->hasOneUse()) { |
| 5287 | BasicBlock *BB = I->getParent(); |
| 5288 | BasicBlock *UserParent = cast<Instruction>(I->use_back())->getParent(); |
| 5289 | if (UserParent != BB) { |
| 5290 | bool UserIsSuccessor = false; |
| 5291 | // See if the user is one of our successors. |
| 5292 | for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI) |
| 5293 | if (*SI == UserParent) { |
| 5294 | UserIsSuccessor = true; |
| 5295 | break; |
| 5296 | } |
| 5297 | |
| 5298 | // If the user is one of our immediate successors, and if that successor |
| 5299 | // only has us as a predecessors (we'd have to split the critical edge |
| 5300 | // otherwise), we can keep going. |
| 5301 | if (UserIsSuccessor && !isa<PHINode>(I->use_back()) && |
| 5302 | next(pred_begin(UserParent)) == pred_end(UserParent)) |
| 5303 | // Okay, the CFG is simple enough, try to sink this instruction. |
| 5304 | Changed |= TryToSinkInstruction(I, UserParent); |
| 5305 | } |
| 5306 | } |
| 5307 | |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 5308 | // Now that we have an instruction, try combining it to simplify it... |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 5309 | if (Instruction *Result = visit(*I)) { |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 5310 | ++NumCombined; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 5311 | // Should we replace the old instruction with a new one? |
Chris Lattner | b3bc8fa | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 5312 | if (Result != I) { |
Chris Lattner | 0cea42a | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 5313 | DEBUG(std::cerr << "IC: Old = " << *I |
| 5314 | << " New = " << *Result); |
| 5315 | |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 5316 | // Everything uses the new instruction now. |
| 5317 | I->replaceAllUsesWith(Result); |
| 5318 | |
| 5319 | // Push the new instruction and any users onto the worklist. |
| 5320 | WorkList.push_back(Result); |
| 5321 | AddUsersToWorkList(*Result); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 5322 | |
| 5323 | // Move the name to the new instruction first... |
| 5324 | std::string OldName = I->getName(); I->setName(""); |
Chris Lattner | d558dc3 | 2003-10-07 22:58:41 +0000 | [diff] [blame] | 5325 | Result->setName(OldName); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 5326 | |
| 5327 | // Insert the new instruction into the basic block... |
| 5328 | BasicBlock *InstParent = I->getParent(); |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 5329 | BasicBlock::iterator InsertPos = I; |
| 5330 | |
| 5331 | if (!isa<PHINode>(Result)) // If combining a PHI, don't insert |
| 5332 | while (isa<PHINode>(InsertPos)) // middle of a block of PHIs. |
| 5333 | ++InsertPos; |
| 5334 | |
| 5335 | InstParent->getInstList().insert(InsertPos, Result); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 5336 | |
Chris Lattner | 00d5131 | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 5337 | // Make sure that we reprocess all operands now that we reduced their |
| 5338 | // use counts. |
Chris Lattner | 216d4d8 | 2004-05-01 23:19:52 +0000 | [diff] [blame] | 5339 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 5340 | if (Instruction *OpI = dyn_cast<Instruction>(I->getOperand(i))) |
| 5341 | WorkList.push_back(OpI); |
| 5342 | |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 5343 | // Instructions can end up on the worklist more than once. Make sure |
| 5344 | // we do not process an instruction that has been deleted. |
| 5345 | removeFromWorkList(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 5346 | |
| 5347 | // Erase the old instruction. |
| 5348 | InstParent->getInstList().erase(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 5349 | } else { |
Chris Lattner | 0cea42a | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 5350 | DEBUG(std::cerr << "IC: MOD = " << *I); |
| 5351 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 5352 | // If the instruction was modified, it's possible that it is now dead. |
| 5353 | // if so, remove it. |
Chris Lattner | 00d5131 | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 5354 | if (isInstructionTriviallyDead(I)) { |
| 5355 | // Make sure we process all operands now that we are reducing their |
| 5356 | // use counts. |
| 5357 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 5358 | if (Instruction *OpI = dyn_cast<Instruction>(I->getOperand(i))) |
| 5359 | WorkList.push_back(OpI); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5360 | |
Chris Lattner | 00d5131 | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 5361 | // Instructions may end up in the worklist more than once. Erase all |
| 5362 | // occurrances of this instruction. |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 5363 | removeFromWorkList(I); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 5364 | I->eraseFromParent(); |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 5365 | } else { |
| 5366 | WorkList.push_back(Result); |
| 5367 | AddUsersToWorkList(*Result); |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 5368 | } |
Chris Lattner | b3bc8fa | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 5369 | } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 5370 | Changed = true; |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 5371 | } |
| 5372 | } |
| 5373 | |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 5374 | return Changed; |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 5375 | } |
| 5376 | |
Brian Gaeke | 96d4bf7 | 2004-07-27 17:43:21 +0000 | [diff] [blame] | 5377 | FunctionPass *llvm::createInstructionCombiningPass() { |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 5378 | return new InstCombiner(); |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 5379 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 5380 | |