Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1 | //===- InstructionCombining.cpp - Combine multiple instructions -----------===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 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. |
| 7 | // |
| 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 | 2cd9196 | 2003-07-23 21:41:57 +0000 | [diff] [blame] | 32 | // N. This list is incomplete |
| 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 | c54e2b8 | 2003-05-22 19:07:21 +0000 | [diff] [blame] | 38 | #include "llvm/Instructions.h" |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 39 | #include "llvm/Intrinsics.h" |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 40 | #include "llvm/Pass.h" |
Chris Lattner | 2a9c847 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 41 | #include "llvm/Constants.h" |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 42 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 43 | #include "llvm/GlobalVariable.h" |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 44 | #include "llvm/Target/TargetData.h" |
| 45 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 46 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 47 | #include "llvm/Support/CallSite.h" |
| 48 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Chris Lattner | 221d688 | 2002-02-12 21:07:25 +0000 | [diff] [blame] | 49 | #include "llvm/Support/InstIterator.h" |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 50 | #include "llvm/Support/InstVisitor.h" |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 51 | #include "llvm/Support/PatternMatch.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 52 | #include "llvm/Support/Debug.h" |
| 53 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | b3bc8fa | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 54 | #include <algorithm> |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 55 | using namespace llvm; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 56 | using namespace llvm::PatternMatch; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 57 | |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 58 | namespace { |
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 59 | Statistic<> NumCombined ("instcombine", "Number of insts combined"); |
| 60 | Statistic<> NumConstProp("instcombine", "Number of constant folds"); |
| 61 | Statistic<> NumDeadInst ("instcombine", "Number of dead inst eliminated"); |
| 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 |
| 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); |
| 115 | Instruction *visitSetCondInst(BinaryOperator &I); |
Chris Lattner | ea34005 | 2003-03-10 19:16:08 +0000 | [diff] [blame] | 116 | Instruction *visitShiftInst(ShiftInst &I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 117 | Instruction *visitCastInst(CastInst &CI); |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 118 | Instruction *visitSelectInst(SelectInst &CI); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 119 | Instruction *visitCallInst(CallInst &CI); |
| 120 | Instruction *visitInvokeInst(InvokeInst &II); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 121 | Instruction *visitPHINode(PHINode &PN); |
| 122 | Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP); |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 123 | Instruction *visitAllocationInst(AllocationInst &AI); |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 124 | Instruction *visitFreeInst(FreeInst &FI); |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 125 | Instruction *visitLoadInst(LoadInst &LI); |
Chris Lattner | c4d10eb | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 126 | Instruction *visitBranchInst(BranchInst &BI); |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 127 | Instruction *visitSwitchInst(SwitchInst &SI); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 128 | |
| 129 | // visitInstruction - Specify what to return for unhandled instructions... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 130 | Instruction *visitInstruction(Instruction &I) { return 0; } |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 131 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 132 | private: |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 133 | Instruction *visitCallSite(CallSite CS); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 134 | bool transformConstExprCastCall(CallSite CS); |
| 135 | |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 136 | public: |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 137 | // InsertNewInstBefore - insert an instruction New before instruction Old |
| 138 | // in the program. Add the new instruction to the worklist. |
| 139 | // |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 140 | Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) { |
Chris Lattner | e6f9a91 | 2002-08-23 18:32:43 +0000 | [diff] [blame] | 141 | assert(New && New->getParent() == 0 && |
| 142 | "New instruction already inserted into a basic block!"); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 143 | BasicBlock *BB = Old.getParent(); |
| 144 | BB->getInstList().insert(&Old, New); // Insert inst |
| 145 | WorkList.push_back(New); // Add to worklist |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 146 | return New; |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 149 | /// InsertCastBefore - Insert a cast of V to TY before the instruction POS. |
| 150 | /// This also adds the cast to the worklist. Finally, this returns the |
| 151 | /// cast. |
| 152 | Value *InsertCastBefore(Value *V, const Type *Ty, Instruction &Pos) { |
| 153 | if (V->getType() == Ty) return V; |
| 154 | |
| 155 | Instruction *C = new CastInst(V, Ty, V->getName(), &Pos); |
| 156 | WorkList.push_back(C); |
| 157 | return C; |
| 158 | } |
| 159 | |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 160 | // ReplaceInstUsesWith - This method is to be used when an instruction is |
| 161 | // found to be dead, replacable with another preexisting expression. Here |
| 162 | // we add all uses of I to the worklist, replace all uses of I with the new |
| 163 | // value, then return I, so that the inst combiner will know that I was |
| 164 | // modified. |
| 165 | // |
| 166 | Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) { |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 167 | AddUsersToWorkList(I); // Add all modified instrs to worklist |
Chris Lattner | 15a76c0 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 168 | if (&I != V) { |
| 169 | I.replaceAllUsesWith(V); |
| 170 | return &I; |
| 171 | } else { |
| 172 | // If we are replacing the instruction with itself, this must be in a |
| 173 | // segment of unreachable code, so just clobber the instruction. |
| 174 | I.replaceAllUsesWith(Constant::getNullValue(I.getType())); |
| 175 | return &I; |
| 176 | } |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 177 | } |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 178 | |
| 179 | // EraseInstFromFunction - When dealing with an instruction that has side |
| 180 | // effects or produces a void value, we can't rely on DCE to delete the |
| 181 | // instruction. Instead, visit methods should return the value returned by |
| 182 | // this function. |
| 183 | Instruction *EraseInstFromFunction(Instruction &I) { |
| 184 | assert(I.use_empty() && "Cannot erase instruction that is used!"); |
| 185 | AddUsesToWorkList(I); |
| 186 | removeFromWorkList(&I); |
| 187 | I.getParent()->getInstList().erase(&I); |
| 188 | return 0; // Don't do anything with FI |
| 189 | } |
| 190 | |
| 191 | |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 192 | private: |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 193 | /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the |
| 194 | /// InsertBefore instruction. This is specialized a bit to avoid inserting |
| 195 | /// casts that are known to not do anything... |
| 196 | /// |
| 197 | Value *InsertOperandCastBefore(Value *V, const Type *DestTy, |
| 198 | Instruction *InsertBefore); |
| 199 | |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 200 | // SimplifyCommutative - This performs a few simplifications for commutative |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 201 | // operators. |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 202 | bool SimplifyCommutative(BinaryOperator &I); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 203 | |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 204 | |
| 205 | // FoldOpIntoPhi - Given a binary operator or cast instruction which has a |
| 206 | // PHI node as operand #0, see if we can fold the instruction into the PHI |
| 207 | // (which is only possible if all operands to the PHI are constants). |
| 208 | Instruction *FoldOpIntoPhi(Instruction &I); |
| 209 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 210 | Instruction *OptAndOp(Instruction *Op, ConstantIntegral *OpRHS, |
| 211 | ConstantIntegral *AndRHS, BinaryOperator &TheAnd); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 212 | |
| 213 | Instruction *InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, |
| 214 | bool Inside, Instruction &IB); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 215 | }; |
Chris Lattner | f629309 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 216 | |
Chris Lattner | a6275cc | 2002-07-26 21:12:46 +0000 | [diff] [blame] | 217 | RegisterOpt<InstCombiner> X("instcombine", "Combine redundant instructions"); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 220 | // getComplexity: Assign a complexity or rank value to LLVM Values... |
| 221 | // 0 -> Constant, 1 -> Other, 2 -> Argument, 2 -> Unary, 3 -> OtherInst |
| 222 | static unsigned getComplexity(Value *V) { |
| 223 | if (isa<Instruction>(V)) { |
| 224 | if (BinaryOperator::isNeg(V) || BinaryOperator::isNot(V)) |
| 225 | return 2; |
| 226 | return 3; |
| 227 | } |
| 228 | if (isa<Argument>(V)) return 2; |
| 229 | return isa<Constant>(V) ? 0 : 1; |
| 230 | } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 231 | |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 232 | // isOnlyUse - Return true if this instruction will be deleted if we stop using |
| 233 | // it. |
| 234 | static bool isOnlyUse(Value *V) { |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 235 | return V->hasOneUse() || isa<Constant>(V); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 238 | // getPromotedType - Return the specified type promoted as it would be to pass |
| 239 | // though a va_arg area... |
| 240 | static const Type *getPromotedType(const Type *Ty) { |
Chris Lattner | 5dd0402 | 2004-06-17 18:16:02 +0000 | [diff] [blame] | 241 | switch (Ty->getTypeID()) { |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 242 | case Type::SByteTyID: |
| 243 | case Type::ShortTyID: return Type::IntTy; |
| 244 | case Type::UByteTyID: |
| 245 | case Type::UShortTyID: return Type::UIntTy; |
| 246 | case Type::FloatTyID: return Type::DoubleTy; |
| 247 | default: return Ty; |
| 248 | } |
| 249 | } |
| 250 | |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 251 | // SimplifyCommutative - This performs a few simplifications for commutative |
| 252 | // operators: |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 253 | // |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 254 | // 1. Order operands such that they are listed from right (least complex) to |
| 255 | // left (most complex). This puts constants before unary operators before |
| 256 | // binary operators. |
| 257 | // |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 258 | // 2. Transform: (op (op V, C1), C2) ==> (op V, (op C1, C2)) |
| 259 | // 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] | 260 | // |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 261 | bool InstCombiner::SimplifyCommutative(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 262 | bool Changed = false; |
| 263 | if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1))) |
| 264 | Changed = !I.swapOperands(); |
| 265 | |
| 266 | if (!I.isAssociative()) return Changed; |
| 267 | Instruction::BinaryOps Opcode = I.getOpcode(); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 268 | if (BinaryOperator *Op = dyn_cast<BinaryOperator>(I.getOperand(0))) |
| 269 | if (Op->getOpcode() == Opcode && isa<Constant>(Op->getOperand(1))) { |
| 270 | if (isa<Constant>(I.getOperand(1))) { |
Chris Lattner | 2a9c847 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 271 | Constant *Folded = ConstantExpr::get(I.getOpcode(), |
| 272 | cast<Constant>(I.getOperand(1)), |
| 273 | cast<Constant>(Op->getOperand(1))); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 274 | I.setOperand(0, Op->getOperand(0)); |
| 275 | I.setOperand(1, Folded); |
| 276 | return true; |
| 277 | } else if (BinaryOperator *Op1=dyn_cast<BinaryOperator>(I.getOperand(1))) |
| 278 | if (Op1->getOpcode() == Opcode && isa<Constant>(Op1->getOperand(1)) && |
| 279 | isOnlyUse(Op) && isOnlyUse(Op1)) { |
| 280 | Constant *C1 = cast<Constant>(Op->getOperand(1)); |
| 281 | Constant *C2 = cast<Constant>(Op1->getOperand(1)); |
| 282 | |
| 283 | // 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] | 284 | Constant *Folded = ConstantExpr::get(I.getOpcode(), C1, C2); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 285 | Instruction *New = BinaryOperator::create(Opcode, Op->getOperand(0), |
| 286 | Op1->getOperand(0), |
| 287 | Op1->getName(), &I); |
| 288 | WorkList.push_back(New); |
| 289 | I.setOperand(0, New); |
| 290 | I.setOperand(1, Folded); |
| 291 | return true; |
| 292 | } |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 293 | } |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 294 | return Changed; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 295 | } |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 296 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 297 | // dyn_castNegVal - Given a 'sub' instruction, return the RHS of the instruction |
| 298 | // if the LHS is a constant zero (which is the 'negate' form). |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 299 | // |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 300 | static inline Value *dyn_castNegVal(Value *V) { |
| 301 | if (BinaryOperator::isNeg(V)) |
| 302 | return BinaryOperator::getNegArgument(cast<BinaryOperator>(V)); |
| 303 | |
Chris Lattner | fe32e0c | 2003-04-30 22:19:10 +0000 | [diff] [blame] | 304 | // Constants can be considered to be negated values if they can be folded... |
| 305 | if (Constant *C = dyn_cast<Constant>(V)) |
Chris Lattner | 448c323 | 2004-06-10 02:12:35 +0000 | [diff] [blame] | 306 | return ConstantExpr::getNeg(C); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 307 | return 0; |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 310 | static inline Value *dyn_castNotVal(Value *V) { |
| 311 | if (BinaryOperator::isNot(V)) |
| 312 | return BinaryOperator::getNotArgument(cast<BinaryOperator>(V)); |
| 313 | |
| 314 | // Constants can be considered to be not'ed values... |
Chris Lattner | 3f2ec39 | 2003-04-30 22:34:06 +0000 | [diff] [blame] | 315 | if (ConstantIntegral *C = dyn_cast<ConstantIntegral>(V)) |
Chris Lattner | 448c323 | 2004-06-10 02:12:35 +0000 | [diff] [blame] | 316 | return ConstantExpr::getNot(C); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 317 | return 0; |
| 318 | } |
| 319 | |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 320 | // dyn_castFoldableMul - If this value is a multiply that can be folded into |
| 321 | // other computations (because it has a constant operand), return the |
| 322 | // non-constant operand of the multiply. |
| 323 | // |
| 324 | static inline Value *dyn_castFoldableMul(Value *V) { |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 325 | if (V->hasOneUse() && V->getType()->isInteger()) |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 326 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 327 | if (I->getOpcode() == Instruction::Mul) |
| 328 | if (isa<Constant>(I->getOperand(1))) |
| 329 | return I->getOperand(0); |
| 330 | return 0; |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 331 | } |
Chris Lattner | af2930e | 2002-08-14 17:51:49 +0000 | [diff] [blame] | 332 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 333 | // Log2 - Calculate the log base 2 for the specified value if it is exactly a |
| 334 | // power of 2. |
| 335 | static unsigned Log2(uint64_t Val) { |
| 336 | assert(Val > 1 && "Values 0 and 1 should be handled elsewhere!"); |
| 337 | unsigned Count = 0; |
| 338 | while (Val != 1) { |
| 339 | if (Val & 1) return 0; // Multiple bits set? |
| 340 | Val >>= 1; |
| 341 | ++Count; |
| 342 | } |
| 343 | return Count; |
Chris Lattner | af2930e | 2002-08-14 17:51:49 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 346 | // AddOne, SubOne - Add or subtract a constant one from an integer constant... |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 347 | static ConstantInt *AddOne(ConstantInt *C) { |
| 348 | return cast<ConstantInt>(ConstantExpr::getAdd(C, |
| 349 | ConstantInt::get(C->getType(), 1))); |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 350 | } |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 351 | static ConstantInt *SubOne(ConstantInt *C) { |
| 352 | return cast<ConstantInt>(ConstantExpr::getSub(C, |
| 353 | ConstantInt::get(C->getType(), 1))); |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | // isTrueWhenEqual - Return true if the specified setcondinst instruction is |
| 357 | // true when both operands are equal... |
| 358 | // |
| 359 | static bool isTrueWhenEqual(Instruction &I) { |
| 360 | return I.getOpcode() == Instruction::SetEQ || |
| 361 | I.getOpcode() == Instruction::SetGE || |
| 362 | I.getOpcode() == Instruction::SetLE; |
| 363 | } |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 364 | |
| 365 | /// AssociativeOpt - Perform an optimization on an associative operator. This |
| 366 | /// function is designed to check a chain of associative operators for a |
| 367 | /// potential to apply a certain optimization. Since the optimization may be |
| 368 | /// applicable if the expression was reassociated, this checks the chain, then |
| 369 | /// reassociates the expression as necessary to expose the optimization |
| 370 | /// opportunity. This makes use of a special Functor, which must define |
| 371 | /// 'shouldApply' and 'apply' methods. |
| 372 | /// |
| 373 | template<typename Functor> |
| 374 | Instruction *AssociativeOpt(BinaryOperator &Root, const Functor &F) { |
| 375 | unsigned Opcode = Root.getOpcode(); |
| 376 | Value *LHS = Root.getOperand(0); |
| 377 | |
| 378 | // Quick check, see if the immediate LHS matches... |
| 379 | if (F.shouldApply(LHS)) |
| 380 | return F.apply(Root); |
| 381 | |
| 382 | // Otherwise, if the LHS is not of the same opcode as the root, return. |
| 383 | Instruction *LHSI = dyn_cast<Instruction>(LHS); |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 384 | while (LHSI && LHSI->getOpcode() == Opcode && LHSI->hasOneUse()) { |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 385 | // Should we apply this transform to the RHS? |
| 386 | bool ShouldApply = F.shouldApply(LHSI->getOperand(1)); |
| 387 | |
| 388 | // If not to the RHS, check to see if we should apply to the LHS... |
| 389 | if (!ShouldApply && F.shouldApply(LHSI->getOperand(0))) { |
| 390 | cast<BinaryOperator>(LHSI)->swapOperands(); // Make the LHS the RHS |
| 391 | ShouldApply = true; |
| 392 | } |
| 393 | |
| 394 | // If the functor wants to apply the optimization to the RHS of LHSI, |
| 395 | // reassociate the expression from ((? op A) op B) to (? op (A op B)) |
| 396 | if (ShouldApply) { |
| 397 | BasicBlock *BB = Root.getParent(); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 398 | |
| 399 | // Now all of the instructions are in the current basic block, go ahead |
| 400 | // and perform the reassociation. |
| 401 | Instruction *TmpLHSI = cast<Instruction>(Root.getOperand(0)); |
| 402 | |
| 403 | // First move the selected RHS to the LHS of the root... |
| 404 | Root.setOperand(0, LHSI->getOperand(1)); |
| 405 | |
| 406 | // Make what used to be the LHS of the root be the user of the root... |
| 407 | Value *ExtraOperand = TmpLHSI->getOperand(1); |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 408 | if (&Root == TmpLHSI) { |
Chris Lattner | 15a76c0 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 409 | Root.replaceAllUsesWith(Constant::getNullValue(TmpLHSI->getType())); |
| 410 | return 0; |
| 411 | } |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 412 | Root.replaceAllUsesWith(TmpLHSI); // Users now use TmpLHSI |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 413 | TmpLHSI->setOperand(1, &Root); // TmpLHSI now uses the root |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 414 | TmpLHSI->getParent()->getInstList().remove(TmpLHSI); |
| 415 | BasicBlock::iterator ARI = &Root; ++ARI; |
| 416 | BB->getInstList().insert(ARI, TmpLHSI); // Move TmpLHSI to after Root |
| 417 | ARI = Root; |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 418 | |
| 419 | // Now propagate the ExtraOperand down the chain of instructions until we |
| 420 | // get to LHSI. |
| 421 | while (TmpLHSI != LHSI) { |
| 422 | Instruction *NextLHSI = cast<Instruction>(TmpLHSI->getOperand(0)); |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 423 | // Move the instruction to immediately before the chain we are |
| 424 | // constructing to avoid breaking dominance properties. |
| 425 | NextLHSI->getParent()->getInstList().remove(NextLHSI); |
| 426 | BB->getInstList().insert(ARI, NextLHSI); |
| 427 | ARI = NextLHSI; |
| 428 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 429 | Value *NextOp = NextLHSI->getOperand(1); |
| 430 | NextLHSI->setOperand(1, ExtraOperand); |
| 431 | TmpLHSI = NextLHSI; |
| 432 | ExtraOperand = NextOp; |
| 433 | } |
| 434 | |
| 435 | // Now that the instructions are reassociated, have the functor perform |
| 436 | // the transformation... |
| 437 | return F.apply(Root); |
| 438 | } |
| 439 | |
| 440 | LHSI = dyn_cast<Instruction>(LHSI->getOperand(0)); |
| 441 | } |
| 442 | return 0; |
| 443 | } |
| 444 | |
| 445 | |
| 446 | // AddRHS - Implements: X + X --> X << 1 |
| 447 | struct AddRHS { |
| 448 | Value *RHS; |
| 449 | AddRHS(Value *rhs) : RHS(rhs) {} |
| 450 | bool shouldApply(Value *LHS) const { return LHS == RHS; } |
| 451 | Instruction *apply(BinaryOperator &Add) const { |
| 452 | return new ShiftInst(Instruction::Shl, Add.getOperand(0), |
| 453 | ConstantInt::get(Type::UByteTy, 1)); |
| 454 | } |
| 455 | }; |
| 456 | |
| 457 | // AddMaskingAnd - Implements (A & C1)+(B & C2) --> (A & C1)|(B & C2) |
| 458 | // iff C1&C2 == 0 |
| 459 | struct AddMaskingAnd { |
| 460 | Constant *C2; |
| 461 | AddMaskingAnd(Constant *c) : C2(c) {} |
| 462 | bool shouldApply(Value *LHS) const { |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 463 | ConstantInt *C1; |
| 464 | return match(LHS, m_And(m_Value(), m_ConstantInt(C1))) && |
| 465 | ConstantExpr::getAnd(C1, C2)->isNullValue(); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 466 | } |
| 467 | Instruction *apply(BinaryOperator &Add) const { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 468 | return BinaryOperator::createOr(Add.getOperand(0), Add.getOperand(1)); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 469 | } |
| 470 | }; |
| 471 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 472 | static Value *FoldOperationIntoSelectOperand(Instruction &BI, Value *SO, |
| 473 | InstCombiner *IC) { |
| 474 | // Figure out if the constant is the left or the right argument. |
| 475 | bool ConstIsRHS = isa<Constant>(BI.getOperand(1)); |
| 476 | Constant *ConstOperand = cast<Constant>(BI.getOperand(ConstIsRHS)); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 477 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 478 | if (Constant *SOC = dyn_cast<Constant>(SO)) { |
| 479 | if (ConstIsRHS) |
| 480 | return ConstantExpr::get(BI.getOpcode(), SOC, ConstOperand); |
| 481 | return ConstantExpr::get(BI.getOpcode(), ConstOperand, SOC); |
| 482 | } |
| 483 | |
| 484 | Value *Op0 = SO, *Op1 = ConstOperand; |
| 485 | if (!ConstIsRHS) |
| 486 | std::swap(Op0, Op1); |
| 487 | Instruction *New; |
| 488 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&BI)) |
| 489 | New = BinaryOperator::create(BO->getOpcode(), Op0, Op1); |
| 490 | else if (ShiftInst *SI = dyn_cast<ShiftInst>(&BI)) |
| 491 | New = new ShiftInst(SI->getOpcode(), Op0, Op1); |
Chris Lattner | 326c0f3 | 2004-04-10 19:15:56 +0000 | [diff] [blame] | 492 | else { |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 493 | assert(0 && "Unknown binary instruction type!"); |
Chris Lattner | 326c0f3 | 2004-04-10 19:15:56 +0000 | [diff] [blame] | 494 | abort(); |
| 495 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 496 | return IC->InsertNewInstBefore(New, BI); |
| 497 | } |
| 498 | |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 499 | |
| 500 | /// FoldOpIntoPhi - Given a binary operator or cast instruction which has a PHI |
| 501 | /// node as operand #0, see if we can fold the instruction into the PHI (which |
| 502 | /// is only possible if all operands to the PHI are constants). |
| 503 | Instruction *InstCombiner::FoldOpIntoPhi(Instruction &I) { |
| 504 | PHINode *PN = cast<PHINode>(I.getOperand(0)); |
| 505 | if (!PN->hasOneUse()) return 0; |
| 506 | |
| 507 | // Check to see if all of the operands of the PHI are constants. If not, we |
| 508 | // cannot do the transformation. |
| 509 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 510 | if (!isa<Constant>(PN->getIncomingValue(i))) |
| 511 | return 0; |
| 512 | |
| 513 | // Okay, we can do the transformation: create the new PHI node. |
| 514 | PHINode *NewPN = new PHINode(I.getType(), I.getName()); |
| 515 | I.setName(""); |
| 516 | NewPN->op_reserve(PN->getNumOperands()); |
| 517 | InsertNewInstBefore(NewPN, *PN); |
| 518 | |
| 519 | // Next, add all of the operands to the PHI. |
| 520 | if (I.getNumOperands() == 2) { |
| 521 | Constant *C = cast<Constant>(I.getOperand(1)); |
| 522 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 523 | Constant *InV = cast<Constant>(PN->getIncomingValue(i)); |
| 524 | NewPN->addIncoming(ConstantExpr::get(I.getOpcode(), InV, C), |
| 525 | PN->getIncomingBlock(i)); |
| 526 | } |
| 527 | } else { |
| 528 | assert(isa<CastInst>(I) && "Unary op should be a cast!"); |
| 529 | const Type *RetTy = I.getType(); |
| 530 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 531 | Constant *InV = cast<Constant>(PN->getIncomingValue(i)); |
| 532 | NewPN->addIncoming(ConstantExpr::getCast(InV, RetTy), |
| 533 | PN->getIncomingBlock(i)); |
| 534 | } |
| 535 | } |
| 536 | return ReplaceInstUsesWith(I, NewPN); |
| 537 | } |
| 538 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 539 | // FoldBinOpIntoSelect - Given an instruction with a select as one operand and a |
| 540 | // constant as the other operand, try to fold the binary operator into the |
| 541 | // select arguments. |
| 542 | static Instruction *FoldBinOpIntoSelect(Instruction &BI, 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)) { |
| 550 | Value *SelectTrueVal = FoldOperationIntoSelectOperand(BI, TV, IC); |
| 551 | Value *SelectFalseVal = FoldOperationIntoSelectOperand(BI, FV, IC); |
| 552 | |
| 553 | return new SelectInst(SI->getCondition(), SelectTrueVal, |
| 554 | SelectFalseVal); |
| 555 | } |
| 556 | return 0; |
| 557 | } |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 558 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 559 | Instruction *InstCombiner::visitAdd(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 560 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 561 | Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 562 | |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 563 | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { |
| 564 | // X + 0 --> X |
| 565 | if (!I.getType()->isFloatingPoint() && // -0 + +0 = +0, so it's not a noop |
| 566 | RHSC->isNullValue()) |
| 567 | return ReplaceInstUsesWith(I, LHS); |
| 568 | |
| 569 | // X + (signbit) --> X ^ signbit |
| 570 | if (ConstantInt *CI = dyn_cast<ConstantInt>(RHSC)) { |
| 571 | unsigned NumBits = CI->getType()->getPrimitiveSize()*8; |
| 572 | uint64_t Val = CI->getRawValue() & (1ULL << NumBits)-1; |
| 573 | if (Val == (1ULL << NumBits-1)) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 574 | return BinaryOperator::createXor(LHS, RHS); |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 575 | } |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 576 | |
| 577 | if (isa<PHINode>(LHS)) |
| 578 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 579 | return NV; |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 580 | } |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 581 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 582 | // X + X --> X << 1 |
Robert Bocchino | 7169828 | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 583 | if (I.getType()->isInteger()) { |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 584 | if (Instruction *Result = AssociativeOpt(I, AddRHS(RHS))) return Result; |
Robert Bocchino | 7169828 | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 585 | } |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 586 | |
Chris Lattner | 5c4afb9 | 2002-05-08 22:46:53 +0000 | [diff] [blame] | 587 | // -A + B --> B - A |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 588 | if (Value *V = dyn_castNegVal(LHS)) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 589 | return BinaryOperator::createSub(RHS, V); |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 590 | |
| 591 | // A + -B --> A - B |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 592 | if (!isa<Constant>(RHS)) |
| 593 | if (Value *V = dyn_castNegVal(RHS)) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 594 | return BinaryOperator::createSub(LHS, V); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 595 | |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 596 | // X*C + X --> X * (C+1) |
| 597 | if (dyn_castFoldableMul(LHS) == RHS) { |
Chris Lattner | 2a9c847 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 598 | Constant *CP1 = |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 599 | ConstantExpr::getAdd( |
Chris Lattner | 2a9c847 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 600 | cast<Constant>(cast<Instruction>(LHS)->getOperand(1)), |
| 601 | ConstantInt::get(I.getType(), 1)); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 602 | return BinaryOperator::createMul(RHS, CP1); |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | // X + X*C --> X * (C+1) |
| 606 | if (dyn_castFoldableMul(RHS) == LHS) { |
Chris Lattner | 2a9c847 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 607 | Constant *CP1 = |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 608 | ConstantExpr::getAdd( |
Chris Lattner | 2a9c847 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 609 | cast<Constant>(cast<Instruction>(RHS)->getOperand(1)), |
| 610 | ConstantInt::get(I.getType(), 1)); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 611 | return BinaryOperator::createMul(LHS, CP1); |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 614 | // (A & C1)+(B & C2) --> (A & C1)|(B & C2) iff C1&C2 == 0 |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 615 | ConstantInt *C2; |
| 616 | if (match(RHS, m_And(m_Value(), m_ConstantInt(C2)))) |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 617 | if (Instruction *R = AssociativeOpt(I, AddMaskingAnd(C2))) return R; |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 618 | |
Chris Lattner | 6b03205 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 619 | if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) { |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 620 | Value *X; |
| 621 | if (match(LHS, m_Not(m_Value(X)))) { // ~X + C --> (C-1) - X |
| 622 | Constant *C= ConstantExpr::getSub(CRHS, ConstantInt::get(I.getType(), 1)); |
| 623 | return BinaryOperator::createSub(C, X); |
Chris Lattner | 6b03205 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 624 | } |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 625 | |
| 626 | // Try to fold constant add into select arguments. |
| 627 | if (SelectInst *SI = dyn_cast<SelectInst>(LHS)) |
| 628 | if (Instruction *R = FoldBinOpIntoSelect(I, SI, this)) |
| 629 | return R; |
Chris Lattner | 6b03205 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 630 | } |
| 631 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 632 | return Changed ? &I : 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 633 | } |
| 634 | |
Chris Lattner | 1ba5bcd | 2003-07-22 21:46:59 +0000 | [diff] [blame] | 635 | // isSignBit - Return true if the value represented by the constant only has the |
| 636 | // highest order bit set. |
| 637 | static bool isSignBit(ConstantInt *CI) { |
| 638 | unsigned NumBits = CI->getType()->getPrimitiveSize()*8; |
| 639 | return (CI->getRawValue() & ~(-1LL << NumBits)) == (1ULL << (NumBits-1)); |
| 640 | } |
| 641 | |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 642 | static unsigned getTypeSizeInBits(const Type *Ty) { |
| 643 | return Ty == Type::BoolTy ? 1 : Ty->getPrimitiveSize()*8; |
| 644 | } |
| 645 | |
Chris Lattner | bfe492b | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 646 | /// RemoveNoopCast - Strip off nonconverting casts from the value. |
| 647 | /// |
| 648 | static Value *RemoveNoopCast(Value *V) { |
| 649 | if (CastInst *CI = dyn_cast<CastInst>(V)) { |
| 650 | const Type *CTy = CI->getType(); |
| 651 | const Type *OpTy = CI->getOperand(0)->getType(); |
| 652 | if (CTy->isInteger() && OpTy->isInteger()) { |
| 653 | if (CTy->getPrimitiveSize() == OpTy->getPrimitiveSize()) |
| 654 | return RemoveNoopCast(CI->getOperand(0)); |
| 655 | } else if (isa<PointerType>(CTy) && isa<PointerType>(OpTy)) |
| 656 | return RemoveNoopCast(CI->getOperand(0)); |
| 657 | } |
| 658 | return V; |
| 659 | } |
| 660 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 661 | Instruction *InstCombiner::visitSub(BinaryOperator &I) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 662 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 663 | |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 664 | if (Op0 == Op1) // sub X, X -> 0 |
| 665 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 666 | |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 667 | // If this is a 'B = x-(-A)', change to B = x+A... |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 668 | if (Value *V = dyn_castNegVal(Op1)) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 669 | return BinaryOperator::createAdd(Op0, V); |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 670 | |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 671 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op0)) { |
| 672 | // Replace (-1 - A) with (~A)... |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 673 | if (C->isAllOnesValue()) |
| 674 | return BinaryOperator::createNot(Op1); |
Chris Lattner | 4037171 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 675 | |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 676 | // C - ~X == X + (1+C) |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 677 | Value *X; |
| 678 | if (match(Op1, m_Not(m_Value(X)))) |
| 679 | return BinaryOperator::createAdd(X, |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 680 | ConstantExpr::getAdd(C, ConstantInt::get(I.getType(), 1))); |
Chris Lattner | 9c29067 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 681 | // -((uint)X >> 31) -> ((int)X >> 31) |
| 682 | // -((int)X >> 31) -> ((uint)X >> 31) |
Chris Lattner | bfe492b | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 683 | if (C->isNullValue()) { |
| 684 | Value *NoopCastedRHS = RemoveNoopCast(Op1); |
| 685 | if (ShiftInst *SI = dyn_cast<ShiftInst>(NoopCastedRHS)) |
Chris Lattner | 9c29067 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 686 | if (SI->getOpcode() == Instruction::Shr) |
| 687 | if (ConstantUInt *CU = dyn_cast<ConstantUInt>(SI->getOperand(1))) { |
| 688 | const Type *NewTy; |
Chris Lattner | bfe492b | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 689 | if (SI->getType()->isSigned()) |
Chris Lattner | 5dd0402 | 2004-06-17 18:16:02 +0000 | [diff] [blame] | 690 | NewTy = SI->getType()->getUnsignedVersion(); |
Chris Lattner | 9c29067 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 691 | else |
Chris Lattner | 5dd0402 | 2004-06-17 18:16:02 +0000 | [diff] [blame] | 692 | NewTy = SI->getType()->getSignedVersion(); |
Chris Lattner | 9c29067 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 693 | // Check to see if we are shifting out everything but the sign bit. |
Chris Lattner | bfe492b | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 694 | if (CU->getValue() == SI->getType()->getPrimitiveSize()*8-1) { |
Chris Lattner | 9c29067 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 695 | // Ok, the transformation is safe. Insert a cast of the incoming |
| 696 | // value, then the new shift, then the new cast. |
| 697 | Instruction *FirstCast = new CastInst(SI->getOperand(0), NewTy, |
| 698 | SI->getOperand(0)->getName()); |
| 699 | Value *InV = InsertNewInstBefore(FirstCast, I); |
| 700 | Instruction *NewShift = new ShiftInst(Instruction::Shr, FirstCast, |
| 701 | CU, SI->getName()); |
Chris Lattner | bfe492b | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 702 | if (NewShift->getType() == I.getType()) |
| 703 | return NewShift; |
| 704 | else { |
| 705 | InV = InsertNewInstBefore(NewShift, I); |
| 706 | return new CastInst(NewShift, I.getType()); |
| 707 | } |
Chris Lattner | 9c29067 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 708 | } |
| 709 | } |
Chris Lattner | bfe492b | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 710 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 711 | |
| 712 | // Try to fold constant sub into select arguments. |
| 713 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
| 714 | if (Instruction *R = FoldBinOpIntoSelect(I, SI, this)) |
| 715 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 716 | |
| 717 | if (isa<PHINode>(Op0)) |
| 718 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 719 | return NV; |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 720 | } |
| 721 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 722 | if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 723 | if (Op1I->hasOneUse()) { |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 724 | // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression |
| 725 | // is not used by anyone else... |
| 726 | // |
Chris Lattner | 0517e72 | 2004-02-02 20:09:56 +0000 | [diff] [blame] | 727 | if (Op1I->getOpcode() == Instruction::Sub && |
| 728 | !Op1I->getType()->isFloatingPoint()) { |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 729 | // Swap the two operands of the subexpr... |
| 730 | Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1); |
| 731 | Op1I->setOperand(0, IIOp1); |
| 732 | Op1I->setOperand(1, IIOp0); |
| 733 | |
| 734 | // Create the new top level add instruction... |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 735 | return BinaryOperator::createAdd(Op0, Op1); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | // Replace (A - (A & B)) with (A & ~B) if this is the only use of (A&B)... |
| 739 | // |
| 740 | if (Op1I->getOpcode() == Instruction::And && |
| 741 | (Op1I->getOperand(0) == Op0 || Op1I->getOperand(1) == Op0)) { |
| 742 | Value *OtherOp = Op1I->getOperand(Op1I->getOperand(0) == Op0); |
| 743 | |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 744 | Value *NewNot = |
| 745 | InsertNewInstBefore(BinaryOperator::createNot(OtherOp, "B.not"), I); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 746 | return BinaryOperator::createAnd(Op0, NewNot); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 747 | } |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 748 | |
Chris Lattner | 91ccc15 | 2004-10-06 15:08:25 +0000 | [diff] [blame^] | 749 | // -(X sdiv C) -> (X sdiv -C) |
| 750 | if (Op1I->getOpcode() == Instruction::Div) |
| 751 | if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(Op0)) |
| 752 | if (CSI->getValue() == 0) |
| 753 | if (Constant *DivRHS = dyn_cast<Constant>(Op1I->getOperand(1))) |
| 754 | return BinaryOperator::createDiv(Op1I->getOperand(0), |
| 755 | ConstantExpr::getNeg(DivRHS)); |
| 756 | |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 757 | // X - X*C --> X * (1-C) |
| 758 | if (dyn_castFoldableMul(Op1I) == Op0) { |
Chris Lattner | 2a9c847 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 759 | Constant *CP1 = |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 760 | ConstantExpr::getSub(ConstantInt::get(I.getType(), 1), |
Chris Lattner | 2a9c847 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 761 | cast<Constant>(cast<Instruction>(Op1)->getOperand(1))); |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 762 | assert(CP1 && "Couldn't constant fold 1-C?"); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 763 | return BinaryOperator::createMul(Op0, CP1); |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 764 | } |
Chris Lattner | 4037171 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 765 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 766 | |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 767 | // X*C - X --> X * (C-1) |
| 768 | if (dyn_castFoldableMul(Op0) == Op1) { |
Chris Lattner | 2a9c847 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 769 | Constant *CP1 = |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 770 | ConstantExpr::getSub(cast<Constant>(cast<Instruction>(Op0)->getOperand(1)), |
Chris Lattner | 2a9c847 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 771 | ConstantInt::get(I.getType(), 1)); |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 772 | assert(CP1 && "Couldn't constant fold C - 1?"); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 773 | return BinaryOperator::createMul(Op1, CP1); |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 774 | } |
| 775 | |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 776 | return 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 777 | } |
| 778 | |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 779 | /// isSignBitCheck - Given an exploded setcc instruction, return true if it is |
| 780 | /// really just returns true if the most significant (sign) bit is set. |
| 781 | static bool isSignBitCheck(unsigned Opcode, Value *LHS, ConstantInt *RHS) { |
| 782 | if (RHS->getType()->isSigned()) { |
| 783 | // True if source is LHS < 0 or LHS <= -1 |
| 784 | return Opcode == Instruction::SetLT && RHS->isNullValue() || |
| 785 | Opcode == Instruction::SetLE && RHS->isAllOnesValue(); |
| 786 | } else { |
| 787 | ConstantUInt *RHSC = cast<ConstantUInt>(RHS); |
| 788 | // True if source is LHS > 127 or LHS >= 128, where the constants depend on |
| 789 | // the size of the integer type. |
| 790 | if (Opcode == Instruction::SetGE) |
| 791 | return RHSC->getValue() == 1ULL<<(RHS->getType()->getPrimitiveSize()*8-1); |
| 792 | if (Opcode == Instruction::SetGT) |
| 793 | return RHSC->getValue() == |
| 794 | (1ULL << (RHS->getType()->getPrimitiveSize()*8-1))-1; |
| 795 | } |
| 796 | return false; |
| 797 | } |
| 798 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 799 | Instruction *InstCombiner::visitMul(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 800 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 801 | Value *Op0 = I.getOperand(0); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 802 | |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 803 | // Simplify mul instructions with a constant RHS... |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 804 | if (Constant *Op1 = dyn_cast<Constant>(I.getOperand(1))) { |
| 805 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 806 | |
| 807 | // ((X << C1)*C2) == (X * (C2 << C1)) |
| 808 | if (ShiftInst *SI = dyn_cast<ShiftInst>(Op0)) |
| 809 | if (SI->getOpcode() == Instruction::Shl) |
| 810 | if (Constant *ShOp = dyn_cast<Constant>(SI->getOperand(1))) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 811 | return BinaryOperator::createMul(SI->getOperand(0), |
| 812 | ConstantExpr::getShl(CI, ShOp)); |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 813 | |
Chris Lattner | 515c97c | 2003-09-11 22:24:54 +0000 | [diff] [blame] | 814 | if (CI->isNullValue()) |
| 815 | return ReplaceInstUsesWith(I, Op1); // X * 0 == 0 |
| 816 | if (CI->equalsInt(1)) // X * 1 == X |
| 817 | return ReplaceInstUsesWith(I, Op0); |
| 818 | if (CI->isAllOnesValue()) // X * -1 == 0 - X |
Chris Lattner | 0af1fab | 2003-06-25 17:09:20 +0000 | [diff] [blame] | 819 | return BinaryOperator::createNeg(Op0, I.getName()); |
Chris Lattner | 6c1ce21 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 820 | |
Chris Lattner | 515c97c | 2003-09-11 22:24:54 +0000 | [diff] [blame] | 821 | int64_t Val = (int64_t)cast<ConstantInt>(CI)->getRawValue(); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 822 | if (uint64_t C = Log2(Val)) // Replace X*(2^C) with X << C |
| 823 | return new ShiftInst(Instruction::Shl, Op0, |
| 824 | ConstantUInt::get(Type::UByteTy, C)); |
Robert Bocchino | 7169828 | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 825 | } else if (ConstantFP *Op1F = dyn_cast<ConstantFP>(Op1)) { |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 826 | if (Op1F->isNullValue()) |
| 827 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 6c1ce21 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 828 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 829 | // "In IEEE floating point, x*1 is not equivalent to x for nans. However, |
| 830 | // ANSI says we can drop signals, so we can do this anyway." (from GCC) |
| 831 | if (Op1F->getValue() == 1.0) |
| 832 | return ReplaceInstUsesWith(I, Op0); // Eliminate 'mul double %X, 1.0' |
| 833 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 834 | |
| 835 | // Try to fold constant mul into select arguments. |
| 836 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 837 | if (Instruction *R = FoldBinOpIntoSelect(I, SI, this)) |
| 838 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 839 | |
| 840 | if (isa<PHINode>(Op0)) |
| 841 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 842 | return NV; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 843 | } |
| 844 | |
Chris Lattner | a4f445b | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 845 | if (Value *Op0v = dyn_castNegVal(Op0)) // -X * -Y = X*Y |
| 846 | if (Value *Op1v = dyn_castNegVal(I.getOperand(1))) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 847 | return BinaryOperator::createMul(Op0v, Op1v); |
Chris Lattner | a4f445b | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 848 | |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 849 | // If one of the operands of the multiply is a cast from a boolean value, then |
| 850 | // we know the bool is either zero or one, so this is a 'masking' multiply. |
| 851 | // See if we can simplify things based on how the boolean was originally |
| 852 | // formed. |
| 853 | CastInst *BoolCast = 0; |
| 854 | if (CastInst *CI = dyn_cast<CastInst>(I.getOperand(0))) |
| 855 | if (CI->getOperand(0)->getType() == Type::BoolTy) |
| 856 | BoolCast = CI; |
| 857 | if (!BoolCast) |
| 858 | if (CastInst *CI = dyn_cast<CastInst>(I.getOperand(1))) |
| 859 | if (CI->getOperand(0)->getType() == Type::BoolTy) |
| 860 | BoolCast = CI; |
| 861 | if (BoolCast) { |
| 862 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(BoolCast->getOperand(0))) { |
| 863 | Value *SCIOp0 = SCI->getOperand(0), *SCIOp1 = SCI->getOperand(1); |
| 864 | const Type *SCOpTy = SCIOp0->getType(); |
| 865 | |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 866 | // If the setcc is true iff the sign bit of X is set, then convert this |
| 867 | // multiply into a shift/and combination. |
| 868 | if (isa<ConstantInt>(SCIOp1) && |
| 869 | isSignBitCheck(SCI->getOpcode(), SCIOp0, cast<ConstantInt>(SCIOp1))) { |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 870 | // Shift the X value right to turn it into "all signbits". |
| 871 | Constant *Amt = ConstantUInt::get(Type::UByteTy, |
| 872 | SCOpTy->getPrimitiveSize()*8-1); |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 873 | if (SCIOp0->getType()->isUnsigned()) { |
Chris Lattner | 5dd0402 | 2004-06-17 18:16:02 +0000 | [diff] [blame] | 874 | const Type *NewTy = SCIOp0->getType()->getSignedVersion(); |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 875 | SCIOp0 = InsertNewInstBefore(new CastInst(SCIOp0, NewTy, |
| 876 | SCIOp0->getName()), I); |
| 877 | } |
| 878 | |
| 879 | Value *V = |
| 880 | InsertNewInstBefore(new ShiftInst(Instruction::Shr, SCIOp0, Amt, |
| 881 | BoolCast->getOperand(0)->getName()+ |
| 882 | ".mask"), I); |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 883 | |
| 884 | // If the multiply type is not the same as the source type, sign extend |
| 885 | // or truncate to the multiply type. |
| 886 | if (I.getType() != V->getType()) |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 887 | V = InsertNewInstBefore(new CastInst(V, I.getType(), V->getName()),I); |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 888 | |
| 889 | Value *OtherOp = Op0 == BoolCast ? I.getOperand(1) : Op0; |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 890 | return BinaryOperator::createAnd(V, OtherOp); |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 891 | } |
| 892 | } |
| 893 | } |
| 894 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 895 | return Changed ? &I : 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 896 | } |
| 897 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 898 | Instruction *InstCombiner::visitDiv(BinaryOperator &I) { |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 899 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I.getOperand(1))) { |
Chris Lattner | 83a2e6e | 2004-04-26 14:01:59 +0000 | [diff] [blame] | 900 | // div X, 1 == X |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 901 | if (RHS->equalsInt(1)) |
| 902 | return ReplaceInstUsesWith(I, I.getOperand(0)); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 903 | |
Chris Lattner | 83a2e6e | 2004-04-26 14:01:59 +0000 | [diff] [blame] | 904 | // div X, -1 == -X |
| 905 | if (RHS->isAllOnesValue()) |
| 906 | return BinaryOperator::createNeg(I.getOperand(0)); |
| 907 | |
Chris Lattner | 18d19ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 908 | if (Instruction *LHS = dyn_cast<Instruction>(I.getOperand(0))) |
| 909 | if (LHS->getOpcode() == Instruction::Div) |
| 910 | if (ConstantInt *LHSRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) { |
Chris Lattner | 18d19ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 911 | // (X / C1) / C2 -> X / (C1*C2) |
| 912 | return BinaryOperator::createDiv(LHS->getOperand(0), |
| 913 | ConstantExpr::getMul(RHS, LHSRHS)); |
| 914 | } |
| 915 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 916 | // Check to see if this is an unsigned division with an exact power of 2, |
| 917 | // if so, convert to a right shift. |
| 918 | if (ConstantUInt *C = dyn_cast<ConstantUInt>(RHS)) |
| 919 | if (uint64_t Val = C->getValue()) // Don't break X / 0 |
| 920 | if (uint64_t C = Log2(Val)) |
| 921 | return new ShiftInst(Instruction::Shr, I.getOperand(0), |
| 922 | ConstantUInt::get(Type::UByteTy, C)); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 923 | |
| 924 | if (isa<PHINode>(I.getOperand(0)) && !RHS->isNullValue()) |
| 925 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 926 | return NV; |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | // 0 / X == 0, we don't need to preserve faults! |
| 930 | if (ConstantInt *LHS = dyn_cast<ConstantInt>(I.getOperand(0))) |
| 931 | if (LHS->equalsInt(0)) |
| 932 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 933 | |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 934 | return 0; |
| 935 | } |
| 936 | |
| 937 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 938 | Instruction *InstCombiner::visitRem(BinaryOperator &I) { |
Chris Lattner | 5b73c08 | 2004-07-06 07:01:22 +0000 | [diff] [blame] | 939 | if (I.getType()->isSigned()) |
| 940 | if (Value *RHSNeg = dyn_castNegVal(I.getOperand(1))) |
Chris Lattner | 1e3564e | 2004-07-06 07:11:42 +0000 | [diff] [blame] | 941 | if (!isa<ConstantSInt>(RHSNeg) || |
Chris Lattner | b49f306 | 2004-08-09 21:05:48 +0000 | [diff] [blame] | 942 | cast<ConstantSInt>(RHSNeg)->getValue() > 0) { |
Chris Lattner | 5b73c08 | 2004-07-06 07:01:22 +0000 | [diff] [blame] | 943 | // X % -Y -> X % Y |
| 944 | AddUsesToWorkList(I); |
| 945 | I.setOperand(1, RHSNeg); |
| 946 | return &I; |
| 947 | } |
| 948 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 949 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I.getOperand(1))) { |
| 950 | if (RHS->equalsInt(1)) // X % 1 == 0 |
| 951 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 952 | |
| 953 | // Check to see if this is an unsigned remainder with an exact power of 2, |
| 954 | // if so, convert to a bitwise and. |
| 955 | if (ConstantUInt *C = dyn_cast<ConstantUInt>(RHS)) |
| 956 | 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] | 957 | if (!(Val & (Val-1))) // Power of 2 |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 958 | return BinaryOperator::createAnd(I.getOperand(0), |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 959 | ConstantUInt::get(I.getType(), Val-1)); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 960 | if (isa<PHINode>(I.getOperand(0)) && !RHS->isNullValue()) |
| 961 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 962 | return NV; |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | // 0 % X == 0, we don't need to preserve faults! |
| 966 | if (ConstantInt *LHS = dyn_cast<ConstantInt>(I.getOperand(0))) |
| 967 | if (LHS->equalsInt(0)) |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 968 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 969 | |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 970 | return 0; |
| 971 | } |
| 972 | |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 973 | // isMaxValueMinusOne - return true if this is Max-1 |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 974 | static bool isMaxValueMinusOne(const ConstantInt *C) { |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 975 | if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(C)) { |
| 976 | // Calculate -1 casted to the right type... |
| 977 | unsigned TypeBits = C->getType()->getPrimitiveSize()*8; |
| 978 | uint64_t Val = ~0ULL; // All ones |
| 979 | Val >>= 64-TypeBits; // Shift out unwanted 1 bits... |
| 980 | return CU->getValue() == Val-1; |
| 981 | } |
| 982 | |
| 983 | const ConstantSInt *CS = cast<ConstantSInt>(C); |
| 984 | |
| 985 | // Calculate 0111111111..11111 |
| 986 | unsigned TypeBits = C->getType()->getPrimitiveSize()*8; |
| 987 | int64_t Val = INT64_MAX; // All ones |
| 988 | Val >>= 64-TypeBits; // Shift out unwanted 1 bits... |
| 989 | return CS->getValue() == Val-1; |
| 990 | } |
| 991 | |
| 992 | // isMinValuePlusOne - return true if this is Min+1 |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 993 | static bool isMinValuePlusOne(const ConstantInt *C) { |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 994 | if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(C)) |
| 995 | return CU->getValue() == 1; |
| 996 | |
| 997 | const ConstantSInt *CS = cast<ConstantSInt>(C); |
| 998 | |
| 999 | // Calculate 1111111111000000000000 |
| 1000 | unsigned TypeBits = C->getType()->getPrimitiveSize()*8; |
| 1001 | int64_t Val = -1; // All ones |
| 1002 | Val <<= TypeBits-1; // Shift over to the right spot |
| 1003 | return CS->getValue() == Val+1; |
| 1004 | } |
| 1005 | |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 1006 | // isOneBitSet - Return true if there is exactly one bit set in the specified |
| 1007 | // constant. |
| 1008 | static bool isOneBitSet(const ConstantInt *CI) { |
| 1009 | uint64_t V = CI->getRawValue(); |
| 1010 | return V && (V & (V-1)) == 0; |
| 1011 | } |
| 1012 | |
Chris Lattner | b20ba0a | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 1013 | #if 0 // Currently unused |
| 1014 | // isLowOnes - Return true if the constant is of the form 0+1+. |
| 1015 | static bool isLowOnes(const ConstantInt *CI) { |
| 1016 | uint64_t V = CI->getRawValue(); |
| 1017 | |
| 1018 | // There won't be bits set in parts that the type doesn't contain. |
| 1019 | V &= ConstantInt::getAllOnesValue(CI->getType())->getRawValue(); |
| 1020 | |
| 1021 | uint64_t U = V+1; // If it is low ones, this should be a power of two. |
| 1022 | return U && V && (U & V) == 0; |
| 1023 | } |
| 1024 | #endif |
| 1025 | |
| 1026 | // isHighOnes - Return true if the constant is of the form 1+0+. |
| 1027 | // This is the same as lowones(~X). |
| 1028 | static bool isHighOnes(const ConstantInt *CI) { |
| 1029 | uint64_t V = ~CI->getRawValue(); |
| 1030 | |
| 1031 | // There won't be bits set in parts that the type doesn't contain. |
| 1032 | V &= ConstantInt::getAllOnesValue(CI->getType())->getRawValue(); |
| 1033 | |
| 1034 | uint64_t U = V+1; // If it is low ones, this should be a power of two. |
| 1035 | return U && V && (U & V) == 0; |
| 1036 | } |
| 1037 | |
| 1038 | |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 1039 | /// getSetCondCode - Encode a setcc opcode into a three bit mask. These bits |
| 1040 | /// are carefully arranged to allow folding of expressions such as: |
| 1041 | /// |
| 1042 | /// (A < B) | (A > B) --> (A != B) |
| 1043 | /// |
| 1044 | /// Bit value '4' represents that the comparison is true if A > B, bit value '2' |
| 1045 | /// represents that the comparison is true if A == B, and bit value '1' is true |
| 1046 | /// if A < B. |
| 1047 | /// |
| 1048 | static unsigned getSetCondCode(const SetCondInst *SCI) { |
| 1049 | switch (SCI->getOpcode()) { |
| 1050 | // False -> 0 |
| 1051 | case Instruction::SetGT: return 1; |
| 1052 | case Instruction::SetEQ: return 2; |
| 1053 | case Instruction::SetGE: return 3; |
| 1054 | case Instruction::SetLT: return 4; |
| 1055 | case Instruction::SetNE: return 5; |
| 1056 | case Instruction::SetLE: return 6; |
| 1057 | // True -> 7 |
| 1058 | default: |
| 1059 | assert(0 && "Invalid SetCC opcode!"); |
| 1060 | return 0; |
| 1061 | } |
| 1062 | } |
| 1063 | |
| 1064 | /// getSetCCValue - This is the complement of getSetCondCode, which turns an |
| 1065 | /// opcode and two operands into either a constant true or false, or a brand new |
| 1066 | /// SetCC instruction. |
| 1067 | static Value *getSetCCValue(unsigned Opcode, Value *LHS, Value *RHS) { |
| 1068 | switch (Opcode) { |
| 1069 | case 0: return ConstantBool::False; |
| 1070 | case 1: return new SetCondInst(Instruction::SetGT, LHS, RHS); |
| 1071 | case 2: return new SetCondInst(Instruction::SetEQ, LHS, RHS); |
| 1072 | case 3: return new SetCondInst(Instruction::SetGE, LHS, RHS); |
| 1073 | case 4: return new SetCondInst(Instruction::SetLT, LHS, RHS); |
| 1074 | case 5: return new SetCondInst(Instruction::SetNE, LHS, RHS); |
| 1075 | case 6: return new SetCondInst(Instruction::SetLE, LHS, RHS); |
| 1076 | case 7: return ConstantBool::True; |
| 1077 | default: assert(0 && "Illegal SetCCCode!"); return 0; |
| 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | // FoldSetCCLogical - Implements (setcc1 A, B) & (setcc2 A, B) --> (setcc3 A, B) |
| 1082 | struct FoldSetCCLogical { |
| 1083 | InstCombiner &IC; |
| 1084 | Value *LHS, *RHS; |
| 1085 | FoldSetCCLogical(InstCombiner &ic, SetCondInst *SCI) |
| 1086 | : IC(ic), LHS(SCI->getOperand(0)), RHS(SCI->getOperand(1)) {} |
| 1087 | bool shouldApply(Value *V) const { |
| 1088 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(V)) |
| 1089 | return (SCI->getOperand(0) == LHS && SCI->getOperand(1) == RHS || |
| 1090 | SCI->getOperand(0) == RHS && SCI->getOperand(1) == LHS); |
| 1091 | return false; |
| 1092 | } |
| 1093 | Instruction *apply(BinaryOperator &Log) const { |
| 1094 | SetCondInst *SCI = cast<SetCondInst>(Log.getOperand(0)); |
| 1095 | if (SCI->getOperand(0) != LHS) { |
| 1096 | assert(SCI->getOperand(1) == LHS); |
| 1097 | SCI->swapOperands(); // Swap the LHS and RHS of the SetCC |
| 1098 | } |
| 1099 | |
| 1100 | unsigned LHSCode = getSetCondCode(SCI); |
| 1101 | unsigned RHSCode = getSetCondCode(cast<SetCondInst>(Log.getOperand(1))); |
| 1102 | unsigned Code; |
| 1103 | switch (Log.getOpcode()) { |
| 1104 | case Instruction::And: Code = LHSCode & RHSCode; break; |
| 1105 | case Instruction::Or: Code = LHSCode | RHSCode; break; |
| 1106 | case Instruction::Xor: Code = LHSCode ^ RHSCode; break; |
Chris Lattner | 021c190 | 2003-09-22 20:33:34 +0000 | [diff] [blame] | 1107 | default: assert(0 && "Illegal logical opcode!"); return 0; |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | Value *RV = getSetCCValue(Code, LHS, RHS); |
| 1111 | if (Instruction *I = dyn_cast<Instruction>(RV)) |
| 1112 | return I; |
| 1113 | // Otherwise, it's a constant boolean value... |
| 1114 | return IC.ReplaceInstUsesWith(Log, RV); |
| 1115 | } |
| 1116 | }; |
| 1117 | |
| 1118 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1119 | // OptAndOp - This handles expressions of the form ((val OP C1) & C2). Where |
| 1120 | // the Op parameter is 'OP', OpRHS is 'C1', and AndRHS is 'C2'. Op is |
| 1121 | // guaranteed to be either a shift instruction or a binary operator. |
| 1122 | Instruction *InstCombiner::OptAndOp(Instruction *Op, |
| 1123 | ConstantIntegral *OpRHS, |
| 1124 | ConstantIntegral *AndRHS, |
| 1125 | BinaryOperator &TheAnd) { |
| 1126 | Value *X = Op->getOperand(0); |
Chris Lattner | 76f7fe2 | 2004-01-12 19:47:05 +0000 | [diff] [blame] | 1127 | Constant *Together = 0; |
| 1128 | if (!isa<ShiftInst>(Op)) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1129 | Together = ConstantExpr::getAnd(AndRHS, OpRHS); |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1130 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1131 | switch (Op->getOpcode()) { |
| 1132 | case Instruction::Xor: |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1133 | if (Together->isNullValue()) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1134 | // (X ^ C1) & C2 --> (X & C2) iff (C1&C2) == 0 |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1135 | return BinaryOperator::createAnd(X, AndRHS); |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 1136 | } else if (Op->hasOneUse()) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1137 | // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2) |
| 1138 | std::string OpName = Op->getName(); Op->setName(""); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1139 | Instruction *And = BinaryOperator::createAnd(X, AndRHS, OpName); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1140 | InsertNewInstBefore(And, TheAnd); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1141 | return BinaryOperator::createXor(And, Together); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1142 | } |
| 1143 | break; |
| 1144 | case Instruction::Or: |
| 1145 | // (X | C1) & C2 --> X & C2 iff C1 & C1 == 0 |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1146 | if (Together->isNullValue()) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1147 | return BinaryOperator::createAnd(X, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1148 | else { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1149 | if (Together == AndRHS) // (X | C) & C --> C |
| 1150 | return ReplaceInstUsesWith(TheAnd, AndRHS); |
| 1151 | |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 1152 | if (Op->hasOneUse() && Together != OpRHS) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1153 | // (X | C1) & C2 --> (X | (C1&C2)) & C2 |
| 1154 | std::string Op0Name = Op->getName(); Op->setName(""); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1155 | Instruction *Or = BinaryOperator::createOr(X, Together, Op0Name); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1156 | InsertNewInstBefore(Or, TheAnd); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1157 | return BinaryOperator::createAnd(Or, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1158 | } |
| 1159 | } |
| 1160 | break; |
| 1161 | case Instruction::Add: |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 1162 | if (Op->hasOneUse()) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1163 | // Adding a one to a single bit bit-field should be turned into an XOR |
| 1164 | // of the bit. First thing to check is to see if this AND is with a |
| 1165 | // single bit constant. |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 1166 | uint64_t AndRHSV = cast<ConstantInt>(AndRHS)->getRawValue(); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1167 | |
| 1168 | // Clear bits that are not part of the constant. |
| 1169 | AndRHSV &= (1ULL << AndRHS->getType()->getPrimitiveSize()*8)-1; |
| 1170 | |
| 1171 | // If there is only one bit set... |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 1172 | if (isOneBitSet(cast<ConstantInt>(AndRHS))) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1173 | // Ok, at this point, we know that we are masking the result of the |
| 1174 | // ADD down to exactly one bit. If the constant we are adding has |
| 1175 | // no bits set below this bit, then we can eliminate the ADD. |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 1176 | uint64_t AddRHS = cast<ConstantInt>(OpRHS)->getRawValue(); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1177 | |
| 1178 | // Check to see if any bits below the one bit set in AndRHSV are set. |
| 1179 | if ((AddRHS & (AndRHSV-1)) == 0) { |
| 1180 | // If not, the only thing that can effect the output of the AND is |
| 1181 | // the bit specified by AndRHSV. If that bit is set, the effect of |
| 1182 | // the XOR is to toggle the bit. If it is clear, then the ADD has |
| 1183 | // no effect. |
| 1184 | if ((AddRHS & AndRHSV) == 0) { // Bit is not set, noop |
| 1185 | TheAnd.setOperand(0, X); |
| 1186 | return &TheAnd; |
| 1187 | } else { |
| 1188 | std::string Name = Op->getName(); Op->setName(""); |
| 1189 | // Pull the XOR out of the AND. |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1190 | Instruction *NewAnd = BinaryOperator::createAnd(X, AndRHS, Name); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1191 | InsertNewInstBefore(NewAnd, TheAnd); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1192 | return BinaryOperator::createXor(NewAnd, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1193 | } |
| 1194 | } |
| 1195 | } |
| 1196 | } |
| 1197 | break; |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 1198 | |
| 1199 | case Instruction::Shl: { |
| 1200 | // We know that the AND will not produce any of the bits shifted in, so if |
| 1201 | // the anded constant includes them, clear them now! |
| 1202 | // |
| 1203 | Constant *AllOne = ConstantIntegral::getAllOnesValue(AndRHS->getType()); |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 1204 | Constant *ShlMask = ConstantExpr::getShl(AllOne, OpRHS); |
| 1205 | Constant *CI = ConstantExpr::getAnd(AndRHS, ShlMask); |
| 1206 | |
| 1207 | if (CI == ShlMask) { // Masking out bits that the shift already masks |
| 1208 | return ReplaceInstUsesWith(TheAnd, Op); // No need for the and. |
| 1209 | } else if (CI != AndRHS) { // Reducing bits set in and. |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 1210 | TheAnd.setOperand(1, CI); |
| 1211 | return &TheAnd; |
| 1212 | } |
| 1213 | break; |
| 1214 | } |
| 1215 | case Instruction::Shr: |
| 1216 | // We know that the AND will not produce any of the bits shifted in, so if |
| 1217 | // the anded constant includes them, clear them now! This only applies to |
| 1218 | // unsigned shifts, because a signed shr may bring in set bits! |
| 1219 | // |
| 1220 | if (AndRHS->getType()->isUnsigned()) { |
| 1221 | Constant *AllOne = ConstantIntegral::getAllOnesValue(AndRHS->getType()); |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 1222 | Constant *ShrMask = ConstantExpr::getShr(AllOne, OpRHS); |
| 1223 | Constant *CI = ConstantExpr::getAnd(AndRHS, ShrMask); |
| 1224 | |
| 1225 | if (CI == ShrMask) { // Masking out bits that the shift already masks. |
| 1226 | return ReplaceInstUsesWith(TheAnd, Op); |
| 1227 | } else if (CI != AndRHS) { |
| 1228 | TheAnd.setOperand(1, CI); // Reduce bits set in and cst. |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 1229 | return &TheAnd; |
| 1230 | } |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 1231 | } else { // Signed shr. |
| 1232 | // See if this is shifting in some sign extension, then masking it out |
| 1233 | // with an and. |
| 1234 | if (Op->hasOneUse()) { |
| 1235 | Constant *AllOne = ConstantIntegral::getAllOnesValue(AndRHS->getType()); |
| 1236 | Constant *ShrMask = ConstantExpr::getUShr(AllOne, OpRHS); |
| 1237 | Constant *CI = ConstantExpr::getAnd(AndRHS, ShrMask); |
| 1238 | if (CI == ShrMask) { // Masking out bits shifted in. |
| 1239 | // Make the argument unsigned. |
| 1240 | Value *ShVal = Op->getOperand(0); |
| 1241 | ShVal = InsertCastBefore(ShVal, |
| 1242 | ShVal->getType()->getUnsignedVersion(), |
| 1243 | TheAnd); |
| 1244 | ShVal = InsertNewInstBefore(new ShiftInst(Instruction::Shr, ShVal, |
| 1245 | OpRHS, Op->getName()), |
| 1246 | TheAnd); |
| 1247 | return new CastInst(ShVal, Op->getType()); |
| 1248 | } |
| 1249 | } |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 1250 | } |
| 1251 | break; |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1252 | } |
| 1253 | return 0; |
| 1254 | } |
| 1255 | |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1256 | |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 1257 | /// InsertRangeTest - Emit a computation of: (V >= Lo && V < Hi) if Inside is |
| 1258 | /// true, otherwise (V < Lo || V >= Hi). In pratice, we emit the more efficient |
| 1259 | /// (V-Lo) <u Hi-Lo. This method expects that Lo <= Hi. IB is the location to |
| 1260 | /// insert new instructions. |
| 1261 | Instruction *InstCombiner::InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, |
| 1262 | bool Inside, Instruction &IB) { |
| 1263 | assert(cast<ConstantBool>(ConstantExpr::getSetLE(Lo, Hi))->getValue() && |
| 1264 | "Lo is not <= Hi in range emission code!"); |
| 1265 | if (Inside) { |
| 1266 | if (Lo == Hi) // Trivially false. |
| 1267 | return new SetCondInst(Instruction::SetNE, V, V); |
| 1268 | if (cast<ConstantIntegral>(Lo)->isMinValue()) |
| 1269 | return new SetCondInst(Instruction::SetLT, V, Hi); |
| 1270 | |
| 1271 | Constant *AddCST = ConstantExpr::getNeg(Lo); |
| 1272 | Instruction *Add = BinaryOperator::createAdd(V, AddCST,V->getName()+".off"); |
| 1273 | InsertNewInstBefore(Add, IB); |
| 1274 | // Convert to unsigned for the comparison. |
| 1275 | const Type *UnsType = Add->getType()->getUnsignedVersion(); |
| 1276 | Value *OffsetVal = InsertCastBefore(Add, UnsType, IB); |
| 1277 | AddCST = ConstantExpr::getAdd(AddCST, Hi); |
| 1278 | AddCST = ConstantExpr::getCast(AddCST, UnsType); |
| 1279 | return new SetCondInst(Instruction::SetLT, OffsetVal, AddCST); |
| 1280 | } |
| 1281 | |
| 1282 | if (Lo == Hi) // Trivially true. |
| 1283 | return new SetCondInst(Instruction::SetEQ, V, V); |
| 1284 | |
| 1285 | Hi = SubOne(cast<ConstantInt>(Hi)); |
| 1286 | if (cast<ConstantIntegral>(Lo)->isMinValue()) // V < 0 || V >= Hi ->'V > Hi-1' |
| 1287 | return new SetCondInst(Instruction::SetGT, V, Hi); |
| 1288 | |
| 1289 | // Emit X-Lo > Hi-Lo-1 |
| 1290 | Constant *AddCST = ConstantExpr::getNeg(Lo); |
| 1291 | Instruction *Add = BinaryOperator::createAdd(V, AddCST, V->getName()+".off"); |
| 1292 | InsertNewInstBefore(Add, IB); |
| 1293 | // Convert to unsigned for the comparison. |
| 1294 | const Type *UnsType = Add->getType()->getUnsignedVersion(); |
| 1295 | Value *OffsetVal = InsertCastBefore(Add, UnsType, IB); |
| 1296 | AddCST = ConstantExpr::getAdd(AddCST, Hi); |
| 1297 | AddCST = ConstantExpr::getCast(AddCST, UnsType); |
| 1298 | return new SetCondInst(Instruction::SetGT, OffsetVal, AddCST); |
| 1299 | } |
| 1300 | |
| 1301 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1302 | Instruction *InstCombiner::visitAnd(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 1303 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1304 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1305 | |
| 1306 | // and X, X = X and X, 0 == 0 |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1307 | if (Op0 == Op1 || Op1 == Constant::getNullValue(I.getType())) |
| 1308 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1309 | |
| 1310 | // and X, -1 == X |
Chris Lattner | c6a8aff | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 1311 | if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1)) { |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1312 | if (RHS->isAllOnesValue()) |
| 1313 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1314 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1315 | // Optimize a variety of ((val OP C1) & C2) combinations... |
| 1316 | if (isa<BinaryOperator>(Op0) || isa<ShiftInst>(Op0)) { |
| 1317 | Instruction *Op0I = cast<Instruction>(Op0); |
Chris Lattner | 06782f8 | 2003-07-23 19:36:21 +0000 | [diff] [blame] | 1318 | Value *X = Op0I->getOperand(0); |
Chris Lattner | 5840326 | 2003-07-23 19:25:52 +0000 | [diff] [blame] | 1319 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 1320 | if (Instruction *Res = OptAndOp(Op0I, Op0CI, RHS, I)) |
| 1321 | return Res; |
Chris Lattner | 06782f8 | 2003-07-23 19:36:21 +0000 | [diff] [blame] | 1322 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1323 | |
| 1324 | // Try to fold constant and into select arguments. |
| 1325 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 1326 | if (Instruction *R = FoldBinOpIntoSelect(I, SI, this)) |
| 1327 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1328 | if (isa<PHINode>(Op0)) |
| 1329 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 1330 | return NV; |
Chris Lattner | c6a8aff | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 1331 | } |
| 1332 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 1333 | Value *Op0NotVal = dyn_castNotVal(Op0); |
| 1334 | Value *Op1NotVal = dyn_castNotVal(Op1); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1335 | |
Chris Lattner | 5b62aa7 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 1336 | if (Op0NotVal == Op1 || Op1NotVal == Op0) // A & ~A == ~A & A == 0 |
| 1337 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
| 1338 | |
Misha Brukman | cb6267b | 2004-07-30 12:50:08 +0000 | [diff] [blame] | 1339 | // (~A & ~B) == (~(A | B)) - De Morgan's Law |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 1340 | if (Op0NotVal && Op1NotVal && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1341 | Instruction *Or = BinaryOperator::createOr(Op0NotVal, Op1NotVal, |
| 1342 | I.getName()+".demorgan"); |
Chris Lattner | c6a8aff | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 1343 | InsertNewInstBefore(Or, I); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1344 | return BinaryOperator::createNot(Or); |
| 1345 | } |
| 1346 | |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 1347 | if (SetCondInst *RHS = dyn_cast<SetCondInst>(Op1)) { |
| 1348 | // (setcc1 A, B) & (setcc2 A, B) --> (setcc3 A, B) |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 1349 | if (Instruction *R = AssociativeOpt(I, FoldSetCCLogical(*this, RHS))) |
| 1350 | return R; |
| 1351 | |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 1352 | Value *LHSVal, *RHSVal; |
| 1353 | ConstantInt *LHSCst, *RHSCst; |
| 1354 | Instruction::BinaryOps LHSCC, RHSCC; |
| 1355 | if (match(Op0, m_SetCond(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst)))) |
| 1356 | if (match(RHS, m_SetCond(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst)))) |
| 1357 | if (LHSVal == RHSVal && // Found (X setcc C1) & (X setcc C2) |
| 1358 | // Set[GL]E X, CST is folded to Set[GL]T elsewhere. |
| 1359 | LHSCC != Instruction::SetGE && LHSCC != Instruction::SetLE && |
| 1360 | RHSCC != Instruction::SetGE && RHSCC != Instruction::SetLE) { |
| 1361 | // Ensure that the larger constant is on the RHS. |
| 1362 | Constant *Cmp = ConstantExpr::getSetGT(LHSCst, RHSCst); |
| 1363 | SetCondInst *LHS = cast<SetCondInst>(Op0); |
| 1364 | if (cast<ConstantBool>(Cmp)->getValue()) { |
| 1365 | std::swap(LHS, RHS); |
| 1366 | std::swap(LHSCst, RHSCst); |
| 1367 | std::swap(LHSCC, RHSCC); |
| 1368 | } |
| 1369 | |
| 1370 | // At this point, we know we have have two setcc instructions |
| 1371 | // comparing a value against two constants and and'ing the result |
| 1372 | // together. Because of the above check, we know that we only have |
| 1373 | // SetEQ, SetNE, SetLT, and SetGT here. We also know (from the |
| 1374 | // FoldSetCCLogical check above), that the two constants are not |
| 1375 | // equal. |
| 1376 | assert(LHSCst != RHSCst && "Compares not folded above?"); |
| 1377 | |
| 1378 | switch (LHSCC) { |
| 1379 | default: assert(0 && "Unknown integer condition code!"); |
| 1380 | case Instruction::SetEQ: |
| 1381 | switch (RHSCC) { |
| 1382 | default: assert(0 && "Unknown integer condition code!"); |
| 1383 | case Instruction::SetEQ: // (X == 13 & X == 15) -> false |
| 1384 | case Instruction::SetGT: // (X == 13 & X > 15) -> false |
| 1385 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 1386 | case Instruction::SetNE: // (X == 13 & X != 15) -> X == 13 |
| 1387 | case Instruction::SetLT: // (X == 13 & X < 15) -> X == 13 |
| 1388 | return ReplaceInstUsesWith(I, LHS); |
| 1389 | } |
| 1390 | case Instruction::SetNE: |
| 1391 | switch (RHSCC) { |
| 1392 | default: assert(0 && "Unknown integer condition code!"); |
| 1393 | case Instruction::SetLT: |
| 1394 | if (LHSCst == SubOne(RHSCst)) // (X != 13 & X < 14) -> X < 13 |
| 1395 | return new SetCondInst(Instruction::SetLT, LHSVal, LHSCst); |
| 1396 | break; // (X != 13 & X < 15) -> no change |
| 1397 | case Instruction::SetEQ: // (X != 13 & X == 15) -> X == 15 |
| 1398 | case Instruction::SetGT: // (X != 13 & X > 15) -> X > 15 |
| 1399 | return ReplaceInstUsesWith(I, RHS); |
| 1400 | case Instruction::SetNE: |
| 1401 | if (LHSCst == SubOne(RHSCst)) {// (X != 13 & X != 14) -> X-13 >u 1 |
| 1402 | Constant *AddCST = ConstantExpr::getNeg(LHSCst); |
| 1403 | Instruction *Add = BinaryOperator::createAdd(LHSVal, AddCST, |
| 1404 | LHSVal->getName()+".off"); |
| 1405 | InsertNewInstBefore(Add, I); |
| 1406 | const Type *UnsType = Add->getType()->getUnsignedVersion(); |
| 1407 | Value *OffsetVal = InsertCastBefore(Add, UnsType, I); |
| 1408 | AddCST = ConstantExpr::getSub(RHSCst, LHSCst); |
| 1409 | AddCST = ConstantExpr::getCast(AddCST, UnsType); |
| 1410 | return new SetCondInst(Instruction::SetGT, OffsetVal, AddCST); |
| 1411 | } |
| 1412 | break; // (X != 13 & X != 15) -> no change |
| 1413 | } |
| 1414 | break; |
| 1415 | case Instruction::SetLT: |
| 1416 | switch (RHSCC) { |
| 1417 | default: assert(0 && "Unknown integer condition code!"); |
| 1418 | case Instruction::SetEQ: // (X < 13 & X == 15) -> false |
| 1419 | case Instruction::SetGT: // (X < 13 & X > 15) -> false |
| 1420 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 1421 | case Instruction::SetNE: // (X < 13 & X != 15) -> X < 13 |
| 1422 | case Instruction::SetLT: // (X < 13 & X < 15) -> X < 13 |
| 1423 | return ReplaceInstUsesWith(I, LHS); |
| 1424 | } |
| 1425 | case Instruction::SetGT: |
| 1426 | switch (RHSCC) { |
| 1427 | default: assert(0 && "Unknown integer condition code!"); |
| 1428 | case Instruction::SetEQ: // (X > 13 & X == 15) -> X > 13 |
| 1429 | return ReplaceInstUsesWith(I, LHS); |
| 1430 | case Instruction::SetGT: // (X > 13 & X > 15) -> X > 15 |
| 1431 | return ReplaceInstUsesWith(I, RHS); |
| 1432 | case Instruction::SetNE: |
| 1433 | if (RHSCst == AddOne(LHSCst)) // (X > 13 & X != 14) -> X > 14 |
| 1434 | return new SetCondInst(Instruction::SetGT, LHSVal, RHSCst); |
| 1435 | break; // (X > 13 & X != 15) -> no change |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 1436 | case Instruction::SetLT: // (X > 13 & X < 15) -> (X-14) <u 1 |
| 1437 | return InsertRangeTest(LHSVal, AddOne(LHSCst), RHSCst, true, I); |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 1438 | } |
| 1439 | } |
| 1440 | } |
| 1441 | } |
| 1442 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1443 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1444 | } |
| 1445 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1446 | Instruction *InstCombiner::visitOr(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 1447 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1448 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1449 | |
| 1450 | // or X, X = X or X, 0 == X |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1451 | if (Op0 == Op1 || Op1 == Constant::getNullValue(I.getType())) |
| 1452 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1453 | |
| 1454 | // or X, -1 == -1 |
Chris Lattner | ad44ebf | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 1455 | if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1)) { |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1456 | if (RHS->isAllOnesValue()) |
| 1457 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1458 | |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1459 | ConstantInt *C1; Value *X; |
| 1460 | // (X & C1) | C2 --> (X | C2) & (C1|C2) |
| 1461 | if (match(Op0, m_And(m_Value(X), m_ConstantInt(C1))) && isOnlyUse(Op0)) { |
| 1462 | std::string Op0Name = Op0->getName(); Op0->setName(""); |
| 1463 | Instruction *Or = BinaryOperator::createOr(X, RHS, Op0Name); |
| 1464 | InsertNewInstBefore(Or, I); |
| 1465 | return BinaryOperator::createAnd(Or, ConstantExpr::getOr(RHS, C1)); |
| 1466 | } |
Chris Lattner | ad44ebf | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 1467 | |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1468 | // (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2) |
| 1469 | if (match(Op0, m_Xor(m_Value(X), m_ConstantInt(C1))) && isOnlyUse(Op0)) { |
| 1470 | std::string Op0Name = Op0->getName(); Op0->setName(""); |
| 1471 | Instruction *Or = BinaryOperator::createOr(X, RHS, Op0Name); |
| 1472 | InsertNewInstBefore(Or, I); |
| 1473 | return BinaryOperator::createXor(Or, |
| 1474 | ConstantExpr::getAnd(C1, ConstantExpr::getNot(RHS))); |
Chris Lattner | ad44ebf | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 1475 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1476 | |
| 1477 | // Try to fold constant and into select arguments. |
| 1478 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 1479 | if (Instruction *R = FoldBinOpIntoSelect(I, SI, this)) |
| 1480 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1481 | if (isa<PHINode>(Op0)) |
| 1482 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 1483 | return NV; |
Chris Lattner | ad44ebf | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 1484 | } |
| 1485 | |
Chris Lattner | 67ca768 | 2003-08-12 19:11:07 +0000 | [diff] [blame] | 1486 | // (A & C1)|(A & C2) == A & (C1|C2) |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1487 | Value *A, *B; ConstantInt *C1, *C2; |
| 1488 | if (match(Op0, m_And(m_Value(A), m_ConstantInt(C1))) && |
| 1489 | match(Op1, m_And(m_Value(B), m_ConstantInt(C2))) && A == B) |
| 1490 | return BinaryOperator::createAnd(A, ConstantExpr::getOr(C1, C2)); |
Chris Lattner | 67ca768 | 2003-08-12 19:11:07 +0000 | [diff] [blame] | 1491 | |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1492 | if (match(Op0, m_Not(m_Value(A)))) { // ~A | Op1 |
| 1493 | if (A == Op1) // ~A | A == -1 |
| 1494 | return ReplaceInstUsesWith(I, |
| 1495 | ConstantIntegral::getAllOnesValue(I.getType())); |
| 1496 | } else { |
| 1497 | A = 0; |
| 1498 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1499 | |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1500 | if (match(Op1, m_Not(m_Value(B)))) { // Op0 | ~B |
| 1501 | if (Op0 == B) |
| 1502 | return ReplaceInstUsesWith(I, |
| 1503 | ConstantIntegral::getAllOnesValue(I.getType())); |
Chris Lattner | a27231a | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 1504 | |
Misha Brukman | cb6267b | 2004-07-30 12:50:08 +0000 | [diff] [blame] | 1505 | // (~A | ~B) == (~(A & B)) - De Morgan's Law |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1506 | if (A && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
| 1507 | Value *And = InsertNewInstBefore(BinaryOperator::createAnd(A, B, |
| 1508 | I.getName()+".demorgan"), I); |
| 1509 | return BinaryOperator::createNot(And); |
| 1510 | } |
Chris Lattner | a27231a | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 1511 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1512 | |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 1513 | // (setcc1 A, B) | (setcc2 A, B) --> (setcc3 A, B) |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 1514 | if (SetCondInst *RHS = dyn_cast<SetCondInst>(I.getOperand(1))) { |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 1515 | if (Instruction *R = AssociativeOpt(I, FoldSetCCLogical(*this, RHS))) |
| 1516 | return R; |
| 1517 | |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 1518 | Value *LHSVal, *RHSVal; |
| 1519 | ConstantInt *LHSCst, *RHSCst; |
| 1520 | Instruction::BinaryOps LHSCC, RHSCC; |
| 1521 | if (match(Op0, m_SetCond(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst)))) |
| 1522 | if (match(RHS, m_SetCond(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst)))) |
| 1523 | if (LHSVal == RHSVal && // Found (X setcc C1) | (X setcc C2) |
| 1524 | // Set[GL]E X, CST is folded to Set[GL]T elsewhere. |
| 1525 | LHSCC != Instruction::SetGE && LHSCC != Instruction::SetLE && |
| 1526 | RHSCC != Instruction::SetGE && RHSCC != Instruction::SetLE) { |
| 1527 | // Ensure that the larger constant is on the RHS. |
| 1528 | Constant *Cmp = ConstantExpr::getSetGT(LHSCst, RHSCst); |
| 1529 | SetCondInst *LHS = cast<SetCondInst>(Op0); |
| 1530 | if (cast<ConstantBool>(Cmp)->getValue()) { |
| 1531 | std::swap(LHS, RHS); |
| 1532 | std::swap(LHSCst, RHSCst); |
| 1533 | std::swap(LHSCC, RHSCC); |
| 1534 | } |
| 1535 | |
| 1536 | // At this point, we know we have have two setcc instructions |
| 1537 | // comparing a value against two constants and or'ing the result |
| 1538 | // together. Because of the above check, we know that we only have |
| 1539 | // SetEQ, SetNE, SetLT, and SetGT here. We also know (from the |
| 1540 | // FoldSetCCLogical check above), that the two constants are not |
| 1541 | // equal. |
| 1542 | assert(LHSCst != RHSCst && "Compares not folded above?"); |
| 1543 | |
| 1544 | switch (LHSCC) { |
| 1545 | default: assert(0 && "Unknown integer condition code!"); |
| 1546 | case Instruction::SetEQ: |
| 1547 | switch (RHSCC) { |
| 1548 | default: assert(0 && "Unknown integer condition code!"); |
| 1549 | case Instruction::SetEQ: |
| 1550 | if (LHSCst == SubOne(RHSCst)) {// (X == 13 | X == 14) -> X-13 <u 2 |
| 1551 | Constant *AddCST = ConstantExpr::getNeg(LHSCst); |
| 1552 | Instruction *Add = BinaryOperator::createAdd(LHSVal, AddCST, |
| 1553 | LHSVal->getName()+".off"); |
| 1554 | InsertNewInstBefore(Add, I); |
| 1555 | const Type *UnsType = Add->getType()->getUnsignedVersion(); |
| 1556 | Value *OffsetVal = InsertCastBefore(Add, UnsType, I); |
| 1557 | AddCST = ConstantExpr::getSub(AddOne(RHSCst), LHSCst); |
| 1558 | AddCST = ConstantExpr::getCast(AddCST, UnsType); |
| 1559 | return new SetCondInst(Instruction::SetLT, OffsetVal, AddCST); |
| 1560 | } |
| 1561 | break; // (X == 13 | X == 15) -> no change |
| 1562 | |
| 1563 | case Instruction::SetGT: |
| 1564 | if (LHSCst == SubOne(RHSCst)) // (X == 13 | X > 14) -> X > 13 |
| 1565 | return new SetCondInst(Instruction::SetGT, LHSVal, LHSCst); |
| 1566 | break; // (X == 13 | X > 15) -> no change |
| 1567 | case Instruction::SetNE: // (X == 13 | X != 15) -> X != 15 |
| 1568 | case Instruction::SetLT: // (X == 13 | X < 15) -> X < 15 |
| 1569 | return ReplaceInstUsesWith(I, RHS); |
| 1570 | } |
| 1571 | break; |
| 1572 | case Instruction::SetNE: |
| 1573 | switch (RHSCC) { |
| 1574 | default: assert(0 && "Unknown integer condition code!"); |
| 1575 | case Instruction::SetLT: // (X != 13 | X < 15) -> X < 15 |
| 1576 | return ReplaceInstUsesWith(I, RHS); |
| 1577 | case Instruction::SetEQ: // (X != 13 | X == 15) -> X != 13 |
| 1578 | case Instruction::SetGT: // (X != 13 | X > 15) -> X != 13 |
| 1579 | return ReplaceInstUsesWith(I, LHS); |
| 1580 | case Instruction::SetNE: // (X != 13 | X != 15) -> true |
| 1581 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 1582 | } |
| 1583 | break; |
| 1584 | case Instruction::SetLT: |
| 1585 | switch (RHSCC) { |
| 1586 | default: assert(0 && "Unknown integer condition code!"); |
| 1587 | case Instruction::SetEQ: // (X < 13 | X == 14) -> no change |
| 1588 | break; |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 1589 | case Instruction::SetGT: // (X < 13 | X > 15) -> (X-13) > 2 |
| 1590 | return InsertRangeTest(LHSVal, LHSCst, AddOne(RHSCst), false, I); |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 1591 | case Instruction::SetNE: // (X < 13 | X != 15) -> X != 15 |
| 1592 | case Instruction::SetLT: // (X < 13 | X < 15) -> X < 15 |
| 1593 | return ReplaceInstUsesWith(I, RHS); |
| 1594 | } |
| 1595 | break; |
| 1596 | case Instruction::SetGT: |
| 1597 | switch (RHSCC) { |
| 1598 | default: assert(0 && "Unknown integer condition code!"); |
| 1599 | case Instruction::SetEQ: // (X > 13 | X == 15) -> X > 13 |
| 1600 | case Instruction::SetGT: // (X > 13 | X > 15) -> X > 13 |
| 1601 | return ReplaceInstUsesWith(I, LHS); |
| 1602 | case Instruction::SetNE: // (X > 13 | X != 15) -> true |
| 1603 | case Instruction::SetLT: // (X > 13 | X < 15) -> true |
| 1604 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 1605 | } |
| 1606 | } |
| 1607 | } |
| 1608 | } |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1609 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1610 | } |
| 1611 | |
Chris Lattner | c317d39 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 1612 | // XorSelf - Implements: X ^ X --> 0 |
| 1613 | struct XorSelf { |
| 1614 | Value *RHS; |
| 1615 | XorSelf(Value *rhs) : RHS(rhs) {} |
| 1616 | bool shouldApply(Value *LHS) const { return LHS == RHS; } |
| 1617 | Instruction *apply(BinaryOperator &Xor) const { |
| 1618 | return &Xor; |
| 1619 | } |
| 1620 | }; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1621 | |
| 1622 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1623 | Instruction *InstCombiner::visitXor(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 1624 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1625 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1626 | |
Chris Lattner | c317d39 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 1627 | // xor X, X = 0, even if X is nested in a sequence of Xor's. |
| 1628 | if (Instruction *Result = AssociativeOpt(I, XorSelf(Op1))) { |
| 1629 | assert(Result == &I && "AssociativeOpt didn't work?"); |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1630 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | c317d39 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 1631 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1632 | |
Chris Lattner | eca0c5c | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 1633 | if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1)) { |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1634 | // xor X, 0 == X |
Chris Lattner | eca0c5c | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 1635 | if (RHS->isNullValue()) |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1636 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1637 | |
Chris Lattner | eca0c5c | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 1638 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) { |
Chris Lattner | 05bd1b2 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 1639 | // xor (setcc A, B), true = not (setcc A, B) = setncc A, B |
Chris Lattner | eca0c5c | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 1640 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(Op0I)) |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 1641 | if (RHS == ConstantBool::True && SCI->hasOneUse()) |
Chris Lattner | 05bd1b2 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 1642 | return new SetCondInst(SCI->getInverseCondition(), |
| 1643 | SCI->getOperand(0), SCI->getOperand(1)); |
Chris Lattner | ad5b4fb | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 1644 | |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 1645 | // ~(c-X) == X-c-1 == X+(-c-1) |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1646 | if (Op0I->getOpcode() == Instruction::Sub && RHS->isAllOnesValue()) |
| 1647 | if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1648 | Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C); |
| 1649 | Constant *ConstantRHS = ConstantExpr::getSub(NegOp0I0C, |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1650 | ConstantInt::get(I.getType(), 1)); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1651 | return BinaryOperator::createAdd(Op0I->getOperand(1), ConstantRHS); |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1652 | } |
Chris Lattner | 5b62aa7 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 1653 | |
| 1654 | // ~(~X & Y) --> (X | ~Y) |
| 1655 | if (Op0I->getOpcode() == Instruction::And && RHS->isAllOnesValue()) { |
| 1656 | if (dyn_castNotVal(Op0I->getOperand(1))) Op0I->swapOperands(); |
| 1657 | if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0))) { |
| 1658 | Instruction *NotY = |
| 1659 | BinaryOperator::createNot(Op0I->getOperand(1), |
| 1660 | Op0I->getOperand(1)->getName()+".not"); |
| 1661 | InsertNewInstBefore(NotY, I); |
| 1662 | return BinaryOperator::createOr(Op0NotVal, NotY); |
| 1663 | } |
| 1664 | } |
Chris Lattner | eca0c5c | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 1665 | |
| 1666 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) |
Chris Lattner | ad5b4fb | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 1667 | switch (Op0I->getOpcode()) { |
| 1668 | case Instruction::Add: |
Chris Lattner | 689d24b | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 1669 | // ~(X-c) --> (-c-1)-X |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1670 | if (RHS->isAllOnesValue()) { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1671 | Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI); |
| 1672 | return BinaryOperator::createSub( |
| 1673 | ConstantExpr::getSub(NegOp0CI, |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1674 | ConstantInt::get(I.getType(), 1)), |
Chris Lattner | 689d24b | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 1675 | Op0I->getOperand(0)); |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 1676 | } |
Chris Lattner | ad5b4fb | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 1677 | break; |
| 1678 | case Instruction::And: |
Chris Lattner | eca0c5c | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 1679 | // (X & C1) ^ C2 --> (X & C1) | C2 iff (C1&C2) == 0 |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1680 | if (ConstantExpr::getAnd(RHS, Op0CI)->isNullValue()) |
| 1681 | return BinaryOperator::createOr(Op0, RHS); |
Chris Lattner | ad5b4fb | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 1682 | break; |
| 1683 | case Instruction::Or: |
Chris Lattner | eca0c5c | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 1684 | // (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2 |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1685 | if (ConstantExpr::getAnd(RHS, Op0CI) == RHS) |
Chris Lattner | 448c323 | 2004-06-10 02:12:35 +0000 | [diff] [blame] | 1686 | return BinaryOperator::createAnd(Op0, ConstantExpr::getNot(RHS)); |
Chris Lattner | ad5b4fb | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 1687 | break; |
| 1688 | default: break; |
Chris Lattner | eca0c5c | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 1689 | } |
Chris Lattner | 05bd1b2 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 1690 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1691 | |
| 1692 | // Try to fold constant and into select arguments. |
| 1693 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 1694 | if (Instruction *R = FoldBinOpIntoSelect(I, SI, this)) |
| 1695 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1696 | if (isa<PHINode>(Op0)) |
| 1697 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 1698 | return NV; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1699 | } |
| 1700 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 1701 | if (Value *X = dyn_castNotVal(Op0)) // ~A ^ A == -1 |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1702 | if (X == Op1) |
| 1703 | return ReplaceInstUsesWith(I, |
| 1704 | ConstantIntegral::getAllOnesValue(I.getType())); |
| 1705 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 1706 | if (Value *X = dyn_castNotVal(Op1)) // A ^ ~A == -1 |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 1707 | if (X == Op0) |
| 1708 | return ReplaceInstUsesWith(I, |
| 1709 | ConstantIntegral::getAllOnesValue(I.getType())); |
| 1710 | |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 1711 | if (Instruction *Op1I = dyn_cast<Instruction>(Op1)) |
Chris Lattner | 26ca7e1 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 1712 | if (Op1I->getOpcode() == Instruction::Or) { |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 1713 | if (Op1I->getOperand(0) == Op0) { // B^(B|A) == (A|B)^B |
| 1714 | cast<BinaryOperator>(Op1I)->swapOperands(); |
| 1715 | I.swapOperands(); |
| 1716 | std::swap(Op0, Op1); |
| 1717 | } else if (Op1I->getOperand(1) == Op0) { // B^(A|B) == (A|B)^B |
| 1718 | I.swapOperands(); |
| 1719 | std::swap(Op0, Op1); |
Chris Lattner | 26ca7e1 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 1720 | } |
| 1721 | } else if (Op1I->getOpcode() == Instruction::Xor) { |
| 1722 | if (Op0 == Op1I->getOperand(0)) // A^(A^B) == B |
| 1723 | return ReplaceInstUsesWith(I, Op1I->getOperand(1)); |
| 1724 | else if (Op0 == Op1I->getOperand(1)) // A^(B^A) == B |
| 1725 | return ReplaceInstUsesWith(I, Op1I->getOperand(0)); |
| 1726 | } |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 1727 | |
| 1728 | if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 1729 | if (Op0I->getOpcode() == Instruction::Or && Op0I->hasOneUse()) { |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 1730 | if (Op0I->getOperand(0) == Op1) // (B|A)^B == (A|B)^B |
| 1731 | cast<BinaryOperator>(Op0I)->swapOperands(); |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 1732 | if (Op0I->getOperand(1) == Op1) { // (A|B)^B == A & ~B |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 1733 | Value *NotB = InsertNewInstBefore(BinaryOperator::createNot(Op1, |
| 1734 | Op1->getName()+".not"), I); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1735 | return BinaryOperator::createAnd(Op0I->getOperand(0), NotB); |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 1736 | } |
Chris Lattner | 26ca7e1 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 1737 | } else if (Op0I->getOpcode() == Instruction::Xor) { |
| 1738 | if (Op1 == Op0I->getOperand(0)) // (A^B)^A == B |
| 1739 | return ReplaceInstUsesWith(I, Op0I->getOperand(1)); |
| 1740 | else if (Op1 == Op0I->getOperand(1)) // (B^A)^A == B |
| 1741 | return ReplaceInstUsesWith(I, Op0I->getOperand(0)); |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 1742 | } |
| 1743 | |
Chris Lattner | 1484089 | 2004-08-01 19:42:59 +0000 | [diff] [blame] | 1744 | // (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0 |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1745 | Value *A, *B; ConstantInt *C1, *C2; |
| 1746 | if (match(Op0, m_And(m_Value(A), m_ConstantInt(C1))) && |
| 1747 | match(Op1, m_And(m_Value(B), m_ConstantInt(C2))) && |
Chris Lattner | 1484089 | 2004-08-01 19:42:59 +0000 | [diff] [blame] | 1748 | ConstantExpr::getAnd(C1, C2)->isNullValue()) |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1749 | return BinaryOperator::createOr(Op0, Op1); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 1750 | |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 1751 | // (setcc1 A, B) ^ (setcc2 A, B) --> (setcc3 A, B) |
| 1752 | if (SetCondInst *RHS = dyn_cast<SetCondInst>(I.getOperand(1))) |
| 1753 | if (Instruction *R = AssociativeOpt(I, FoldSetCCLogical(*this, RHS))) |
| 1754 | return R; |
| 1755 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1756 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1757 | } |
| 1758 | |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 1759 | /// MulWithOverflow - Compute Result = In1*In2, returning true if the result |
| 1760 | /// overflowed for this type. |
| 1761 | static bool MulWithOverflow(ConstantInt *&Result, ConstantInt *In1, |
| 1762 | ConstantInt *In2) { |
| 1763 | Result = cast<ConstantInt>(ConstantExpr::getMul(In1, In2)); |
| 1764 | return !In2->isNullValue() && ConstantExpr::getDiv(Result, In2) != In1; |
| 1765 | } |
| 1766 | |
| 1767 | static bool isPositive(ConstantInt *C) { |
| 1768 | return cast<ConstantSInt>(C)->getValue() >= 0; |
| 1769 | } |
| 1770 | |
| 1771 | /// AddWithOverflow - Compute Result = In1+In2, returning true if the result |
| 1772 | /// overflowed for this type. |
| 1773 | static bool AddWithOverflow(ConstantInt *&Result, ConstantInt *In1, |
| 1774 | ConstantInt *In2) { |
| 1775 | Result = cast<ConstantInt>(ConstantExpr::getAdd(In1, In2)); |
| 1776 | |
| 1777 | if (In1->getType()->isUnsigned()) |
| 1778 | return cast<ConstantUInt>(Result)->getValue() < |
| 1779 | cast<ConstantUInt>(In1)->getValue(); |
| 1780 | if (isPositive(In1) != isPositive(In2)) |
| 1781 | return false; |
| 1782 | if (isPositive(In1)) |
| 1783 | return cast<ConstantSInt>(Result)->getValue() < |
| 1784 | cast<ConstantSInt>(In1)->getValue(); |
| 1785 | return cast<ConstantSInt>(Result)->getValue() > |
| 1786 | cast<ConstantSInt>(In1)->getValue(); |
| 1787 | } |
| 1788 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1789 | Instruction *InstCombiner::visitSetCondInst(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 1790 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1791 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 1792 | const Type *Ty = Op0->getType(); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 1793 | |
| 1794 | // setcc X, X |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1795 | if (Op0 == Op1) |
| 1796 | return ReplaceInstUsesWith(I, ConstantBool::get(isTrueWhenEqual(I))); |
Chris Lattner | 53a5b57 | 2002-05-09 20:11:54 +0000 | [diff] [blame] | 1797 | |
Chris Lattner | 3ccd17e | 2003-08-13 05:38:46 +0000 | [diff] [blame] | 1798 | // setcc <global/alloca*>, 0 - Global/Stack value addresses are never null! |
| 1799 | if (isa<ConstantPointerNull>(Op1) && |
| 1800 | (isa<GlobalValue>(Op0) || isa<AllocaInst>(Op0))) |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1801 | return ReplaceInstUsesWith(I, ConstantBool::get(!isTrueWhenEqual(I))); |
| 1802 | |
Chris Lattner | 3ccd17e | 2003-08-13 05:38:46 +0000 | [diff] [blame] | 1803 | |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1804 | // setcc's with boolean values can always be turned into bitwise operations |
| 1805 | if (Ty == Type::BoolTy) { |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 1806 | switch (I.getOpcode()) { |
| 1807 | default: assert(0 && "Invalid setcc instruction!"); |
| 1808 | case Instruction::SetEQ: { // seteq bool %A, %B -> ~(A^B) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1809 | Instruction *Xor = BinaryOperator::createXor(Op0, Op1, I.getName()+"tmp"); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1810 | InsertNewInstBefore(Xor, I); |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 1811 | return BinaryOperator::createNot(Xor); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1812 | } |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 1813 | case Instruction::SetNE: |
| 1814 | return BinaryOperator::createXor(Op0, Op1); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1815 | |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 1816 | case Instruction::SetGT: |
| 1817 | std::swap(Op0, Op1); // Change setgt -> setlt |
| 1818 | // FALL THROUGH |
| 1819 | case Instruction::SetLT: { // setlt bool A, B -> ~X & Y |
| 1820 | Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp"); |
| 1821 | InsertNewInstBefore(Not, I); |
| 1822 | return BinaryOperator::createAnd(Not, Op1); |
| 1823 | } |
| 1824 | case Instruction::SetGE: |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1825 | std::swap(Op0, Op1); // Change setge -> setle |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 1826 | // FALL THROUGH |
| 1827 | case Instruction::SetLE: { // setle bool %A, %B -> ~A | B |
| 1828 | Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp"); |
| 1829 | InsertNewInstBefore(Not, I); |
| 1830 | return BinaryOperator::createOr(Not, Op1); |
| 1831 | } |
| 1832 | } |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1833 | } |
| 1834 | |
Chris Lattner | 2be51ae | 2004-06-09 04:24:29 +0000 | [diff] [blame] | 1835 | // See if we are doing a comparison between a constant and an instruction that |
| 1836 | // can be folded into the comparison. |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 1837 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 1838 | // Check to see if we are comparing against the minimum or maximum value... |
| 1839 | if (CI->isMinValue()) { |
| 1840 | if (I.getOpcode() == Instruction::SetLT) // A < MIN -> FALSE |
| 1841 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 1842 | if (I.getOpcode() == Instruction::SetGE) // A >= MIN -> TRUE |
| 1843 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 1844 | if (I.getOpcode() == Instruction::SetLE) // A <= MIN -> A == MIN |
| 1845 | return BinaryOperator::createSetEQ(Op0, Op1); |
| 1846 | if (I.getOpcode() == Instruction::SetGT) // A > MIN -> A != MIN |
| 1847 | return BinaryOperator::createSetNE(Op0, Op1); |
| 1848 | |
| 1849 | } else if (CI->isMaxValue()) { |
| 1850 | if (I.getOpcode() == Instruction::SetGT) // A > MAX -> FALSE |
| 1851 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 1852 | if (I.getOpcode() == Instruction::SetLE) // A <= MAX -> TRUE |
| 1853 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 1854 | if (I.getOpcode() == Instruction::SetGE) // A >= MAX -> A == MAX |
| 1855 | return BinaryOperator::createSetEQ(Op0, Op1); |
| 1856 | if (I.getOpcode() == Instruction::SetLT) // A < MAX -> A != MAX |
| 1857 | return BinaryOperator::createSetNE(Op0, Op1); |
| 1858 | |
| 1859 | // Comparing against a value really close to min or max? |
| 1860 | } else if (isMinValuePlusOne(CI)) { |
| 1861 | if (I.getOpcode() == Instruction::SetLT) // A < MIN+1 -> A == MIN |
| 1862 | return BinaryOperator::createSetEQ(Op0, SubOne(CI)); |
| 1863 | if (I.getOpcode() == Instruction::SetGE) // A >= MIN-1 -> A != MIN |
| 1864 | return BinaryOperator::createSetNE(Op0, SubOne(CI)); |
| 1865 | |
| 1866 | } else if (isMaxValueMinusOne(CI)) { |
| 1867 | if (I.getOpcode() == Instruction::SetGT) // A > MAX-1 -> A == MAX |
| 1868 | return BinaryOperator::createSetEQ(Op0, AddOne(CI)); |
| 1869 | if (I.getOpcode() == Instruction::SetLE) // A <= MAX-1 -> A != MAX |
| 1870 | return BinaryOperator::createSetNE(Op0, AddOne(CI)); |
| 1871 | } |
| 1872 | |
| 1873 | // If we still have a setle or setge instruction, turn it into the |
| 1874 | // appropriate setlt or setgt instruction. Since the border cases have |
| 1875 | // already been handled above, this requires little checking. |
| 1876 | // |
| 1877 | if (I.getOpcode() == Instruction::SetLE) |
| 1878 | return BinaryOperator::createSetLT(Op0, AddOne(CI)); |
| 1879 | if (I.getOpcode() == Instruction::SetGE) |
| 1880 | return BinaryOperator::createSetGT(Op0, SubOne(CI)); |
| 1881 | |
Chris Lattner | 3c6a0d4 | 2004-05-25 06:32:08 +0000 | [diff] [blame] | 1882 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
Chris Lattner | 648e3bc | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 1883 | switch (LHSI->getOpcode()) { |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1884 | case Instruction::PHI: |
| 1885 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 1886 | return NV; |
| 1887 | break; |
Chris Lattner | 648e3bc | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 1888 | case Instruction::And: |
| 1889 | if (LHSI->hasOneUse() && isa<ConstantInt>(LHSI->getOperand(1)) && |
| 1890 | LHSI->getOperand(0)->hasOneUse()) { |
| 1891 | // If this is: (X >> C1) & C2 != C3 (where any shift and any compare |
| 1892 | // could exist), turn it into (X & (C2 << C1)) != (C3 << C1). This |
| 1893 | // happens a LOT in code produced by the C front-end, for bitfield |
| 1894 | // access. |
| 1895 | ShiftInst *Shift = dyn_cast<ShiftInst>(LHSI->getOperand(0)); |
| 1896 | ConstantUInt *ShAmt; |
| 1897 | ShAmt = Shift ? dyn_cast<ConstantUInt>(Shift->getOperand(1)) : 0; |
| 1898 | ConstantInt *AndCST = cast<ConstantInt>(LHSI->getOperand(1)); |
| 1899 | const Type *Ty = LHSI->getType(); |
| 1900 | |
| 1901 | // We can fold this as long as we can't shift unknown bits |
| 1902 | // into the mask. This can only happen with signed shift |
| 1903 | // rights, as they sign-extend. |
| 1904 | if (ShAmt) { |
| 1905 | bool CanFold = Shift->getOpcode() != Instruction::Shr || |
Chris Lattner | 0cba71b | 2004-09-28 17:54:07 +0000 | [diff] [blame] | 1906 | Shift->getType()->isUnsigned(); |
Chris Lattner | 648e3bc | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 1907 | if (!CanFold) { |
| 1908 | // To test for the bad case of the signed shr, see if any |
| 1909 | // of the bits shifted in could be tested after the mask. |
| 1910 | Constant *OShAmt = ConstantUInt::get(Type::UByteTy, |
Chris Lattner | f0cacc0 | 2004-07-21 20:14:10 +0000 | [diff] [blame] | 1911 | Ty->getPrimitiveSize()*8-ShAmt->getValue()); |
Chris Lattner | 648e3bc | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 1912 | Constant *ShVal = |
| 1913 | ConstantExpr::getShl(ConstantInt::getAllOnesValue(Ty), OShAmt); |
| 1914 | if (ConstantExpr::getAnd(ShVal, AndCST)->isNullValue()) |
| 1915 | CanFold = true; |
| 1916 | } |
| 1917 | |
| 1918 | if (CanFold) { |
Chris Lattner | 0cba71b | 2004-09-28 17:54:07 +0000 | [diff] [blame] | 1919 | Constant *NewCst; |
| 1920 | if (Shift->getOpcode() == Instruction::Shl) |
| 1921 | NewCst = ConstantExpr::getUShr(CI, ShAmt); |
| 1922 | else |
| 1923 | NewCst = ConstantExpr::getShl(CI, ShAmt); |
Chris Lattner | 83c4ec0 | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 1924 | |
Chris Lattner | 648e3bc | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 1925 | // Check to see if we are shifting out any of the bits being |
| 1926 | // compared. |
| 1927 | if (ConstantExpr::get(Shift->getOpcode(), NewCst, ShAmt) != CI){ |
| 1928 | // If we shifted bits out, the fold is not going to work out. |
| 1929 | // As a special case, check to see if this means that the |
| 1930 | // result is always true or false now. |
| 1931 | if (I.getOpcode() == Instruction::SetEQ) |
| 1932 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 1933 | if (I.getOpcode() == Instruction::SetNE) |
| 1934 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 1935 | } else { |
| 1936 | I.setOperand(1, NewCst); |
Chris Lattner | 0cba71b | 2004-09-28 17:54:07 +0000 | [diff] [blame] | 1937 | Constant *NewAndCST; |
| 1938 | if (Shift->getOpcode() == Instruction::Shl) |
| 1939 | NewAndCST = ConstantExpr::getUShr(AndCST, ShAmt); |
| 1940 | else |
| 1941 | NewAndCST = ConstantExpr::getShl(AndCST, ShAmt); |
| 1942 | LHSI->setOperand(1, NewAndCST); |
Chris Lattner | 648e3bc | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 1943 | LHSI->setOperand(0, Shift->getOperand(0)); |
| 1944 | WorkList.push_back(Shift); // Shift is dead. |
| 1945 | AddUsesToWorkList(I); |
| 1946 | return &I; |
Chris Lattner | 5eb9194 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 1947 | } |
| 1948 | } |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 1949 | } |
Chris Lattner | 648e3bc | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 1950 | } |
| 1951 | break; |
Chris Lattner | 83c4ec0 | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 1952 | |
Chris Lattner | f6d1d7d | 2004-09-29 03:09:18 +0000 | [diff] [blame] | 1953 | case Instruction::Cast: { // (setcc (cast X to larger), CI) |
| 1954 | const Type *SrcTy = LHSI->getOperand(0)->getType(); |
| 1955 | if (SrcTy->isIntegral() && LHSI->getType()->isIntegral()) { |
Chris Lattner | dd763f4 | 2004-09-29 03:16:24 +0000 | [diff] [blame] | 1956 | unsigned SrcBits = SrcTy->getPrimitiveSize()*8; |
Chris Lattner | f6d1d7d | 2004-09-29 03:09:18 +0000 | [diff] [blame] | 1957 | if (SrcTy == Type::BoolTy) SrcBits = 1; |
Chris Lattner | dd763f4 | 2004-09-29 03:16:24 +0000 | [diff] [blame] | 1958 | unsigned DestBits = LHSI->getType()->getPrimitiveSize()*8; |
Chris Lattner | f6d1d7d | 2004-09-29 03:09:18 +0000 | [diff] [blame] | 1959 | if (LHSI->getType() == Type::BoolTy) DestBits = 1; |
| 1960 | if (SrcBits < DestBits) { |
| 1961 | // Check to see if the comparison is always true or false. |
| 1962 | Constant *NewCst = ConstantExpr::getCast(CI, SrcTy); |
| 1963 | if (ConstantExpr::getCast(NewCst, LHSI->getType()) != CI) { |
| 1964 | Constant *Min = ConstantIntegral::getMinValue(SrcTy); |
| 1965 | Constant *Max = ConstantIntegral::getMaxValue(SrcTy); |
| 1966 | Min = ConstantExpr::getCast(Min, LHSI->getType()); |
| 1967 | Max = ConstantExpr::getCast(Max, LHSI->getType()); |
| 1968 | switch (I.getOpcode()) { |
| 1969 | default: assert(0 && "unknown integer comparison"); |
| 1970 | case Instruction::SetEQ: |
| 1971 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 1972 | case Instruction::SetNE: |
| 1973 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 1974 | case Instruction::SetLT: |
| 1975 | return ReplaceInstUsesWith(I, ConstantExpr::getSetLT(Max, CI)); |
Chris Lattner | f6d1d7d | 2004-09-29 03:09:18 +0000 | [diff] [blame] | 1976 | case Instruction::SetGT: |
| 1977 | return ReplaceInstUsesWith(I, ConstantExpr::getSetGT(Min, CI)); |
Chris Lattner | f6d1d7d | 2004-09-29 03:09:18 +0000 | [diff] [blame] | 1978 | } |
| 1979 | } |
| 1980 | |
| 1981 | return new SetCondInst(I.getOpcode(), LHSI->getOperand(0), |
| 1982 | ConstantExpr::getCast(CI, SrcTy)); |
| 1983 | } |
| 1984 | } |
| 1985 | break; |
| 1986 | } |
Chris Lattner | 18d19ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 1987 | case Instruction::Shl: // (setcc (shl X, ShAmt), CI) |
| 1988 | if (ConstantUInt *ShAmt = dyn_cast<ConstantUInt>(LHSI->getOperand(1))) { |
| 1989 | switch (I.getOpcode()) { |
| 1990 | default: break; |
| 1991 | case Instruction::SetEQ: |
| 1992 | case Instruction::SetNE: { |
| 1993 | // If we are comparing against bits always shifted out, the |
| 1994 | // comparison cannot succeed. |
| 1995 | Constant *Comp = |
| 1996 | ConstantExpr::getShl(ConstantExpr::getShr(CI, ShAmt), ShAmt); |
| 1997 | if (Comp != CI) {// Comparing against a bit that we know is zero. |
| 1998 | bool IsSetNE = I.getOpcode() == Instruction::SetNE; |
| 1999 | Constant *Cst = ConstantBool::get(IsSetNE); |
| 2000 | return ReplaceInstUsesWith(I, Cst); |
| 2001 | } |
| 2002 | |
| 2003 | if (LHSI->hasOneUse()) { |
| 2004 | // Otherwise strength reduce the shift into an and. |
| 2005 | unsigned ShAmtVal = ShAmt->getValue(); |
| 2006 | unsigned TypeBits = CI->getType()->getPrimitiveSize()*8; |
| 2007 | uint64_t Val = (1ULL << (TypeBits-ShAmtVal))-1; |
| 2008 | |
| 2009 | Constant *Mask; |
| 2010 | if (CI->getType()->isUnsigned()) { |
| 2011 | Mask = ConstantUInt::get(CI->getType(), Val); |
| 2012 | } else if (ShAmtVal != 0) { |
| 2013 | Mask = ConstantSInt::get(CI->getType(), Val); |
| 2014 | } else { |
| 2015 | Mask = ConstantInt::getAllOnesValue(CI->getType()); |
| 2016 | } |
| 2017 | |
| 2018 | Instruction *AndI = |
| 2019 | BinaryOperator::createAnd(LHSI->getOperand(0), |
| 2020 | Mask, LHSI->getName()+".mask"); |
| 2021 | Value *And = InsertNewInstBefore(AndI, I); |
| 2022 | return new SetCondInst(I.getOpcode(), And, |
| 2023 | ConstantExpr::getUShr(CI, ShAmt)); |
| 2024 | } |
| 2025 | } |
| 2026 | } |
| 2027 | } |
| 2028 | break; |
| 2029 | |
Chris Lattner | 83c4ec0 | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 2030 | case Instruction::Shr: // (setcc (shr X, ShAmt), CI) |
Chris Lattner | f63f647 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 2031 | if (ConstantUInt *ShAmt = dyn_cast<ConstantUInt>(LHSI->getOperand(1))) { |
Chris Lattner | f63f647 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 2032 | switch (I.getOpcode()) { |
| 2033 | default: break; |
| 2034 | case Instruction::SetEQ: |
| 2035 | case Instruction::SetNE: { |
| 2036 | // If we are comparing against bits always shifted out, the |
| 2037 | // comparison cannot succeed. |
| 2038 | Constant *Comp = |
| 2039 | ConstantExpr::getShr(ConstantExpr::getShl(CI, ShAmt), ShAmt); |
| 2040 | |
| 2041 | if (Comp != CI) {// Comparing against a bit that we know is zero. |
| 2042 | bool IsSetNE = I.getOpcode() == Instruction::SetNE; |
| 2043 | Constant *Cst = ConstantBool::get(IsSetNE); |
| 2044 | return ReplaceInstUsesWith(I, Cst); |
| 2045 | } |
| 2046 | |
| 2047 | if (LHSI->hasOneUse() || CI->isNullValue()) { |
Chris Lattner | 18d19ca | 2004-09-28 18:22:15 +0000 | [diff] [blame] | 2048 | unsigned ShAmtVal = ShAmt->getValue(); |
| 2049 | |
Chris Lattner | f63f647 | 2004-09-27 16:18:50 +0000 | [diff] [blame] | 2050 | // Otherwise strength reduce the shift into an and. |
| 2051 | uint64_t Val = ~0ULL; // All ones. |
| 2052 | Val <<= ShAmtVal; // Shift over to the right spot. |
| 2053 | |
| 2054 | Constant *Mask; |
| 2055 | if (CI->getType()->isUnsigned()) { |
| 2056 | unsigned TypeBits = CI->getType()->getPrimitiveSize()*8; |
| 2057 | Val &= (1ULL << TypeBits)-1; |
| 2058 | Mask = ConstantUInt::get(CI->getType(), Val); |
| 2059 | } else { |
| 2060 | Mask = ConstantSInt::get(CI->getType(), Val); |
| 2061 | } |
| 2062 | |
| 2063 | Instruction *AndI = |
| 2064 | BinaryOperator::createAnd(LHSI->getOperand(0), |
| 2065 | Mask, LHSI->getName()+".mask"); |
| 2066 | Value *And = InsertNewInstBefore(AndI, I); |
| 2067 | return new SetCondInst(I.getOpcode(), And, |
| 2068 | ConstantExpr::getShl(CI, ShAmt)); |
| 2069 | } |
| 2070 | break; |
| 2071 | } |
| 2072 | } |
| 2073 | } |
| 2074 | break; |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 2075 | |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2076 | case Instruction::Div: |
| 2077 | // Fold: (div X, C1) op C2 -> range check |
| 2078 | if (ConstantInt *DivRHS = dyn_cast<ConstantInt>(LHSI->getOperand(1))) { |
| 2079 | // Fold this div into the comparison, producing a range check. |
| 2080 | // Determine, based on the divide type, what the range is being |
| 2081 | // checked. If there is an overflow on the low or high side, remember |
| 2082 | // it, otherwise compute the range [low, hi) bounding the new value. |
| 2083 | bool LoOverflow = false, HiOverflow = 0; |
| 2084 | ConstantInt *LoBound = 0, *HiBound = 0; |
| 2085 | |
| 2086 | ConstantInt *Prod; |
| 2087 | bool ProdOV = MulWithOverflow(Prod, CI, DivRHS); |
| 2088 | |
| 2089 | if (DivRHS->isNullValue()) { // Don't hack on divide by zeros. |
| 2090 | } else if (LHSI->getType()->isUnsigned()) { // udiv |
| 2091 | LoBound = Prod; |
| 2092 | LoOverflow = ProdOV; |
| 2093 | HiOverflow = ProdOV || AddWithOverflow(HiBound, LoBound, DivRHS); |
| 2094 | } else if (isPositive(DivRHS)) { // Divisor is > 0. |
| 2095 | if (CI->isNullValue()) { // (X / pos) op 0 |
| 2096 | // Can't overflow. |
| 2097 | LoBound = cast<ConstantInt>(ConstantExpr::getNeg(SubOne(DivRHS))); |
| 2098 | HiBound = DivRHS; |
| 2099 | } else if (isPositive(CI)) { // (X / pos) op pos |
| 2100 | LoBound = Prod; |
| 2101 | LoOverflow = ProdOV; |
| 2102 | HiOverflow = ProdOV || AddWithOverflow(HiBound, Prod, DivRHS); |
| 2103 | } else { // (X / pos) op neg |
| 2104 | Constant *DivRHSH = ConstantExpr::getNeg(SubOne(DivRHS)); |
| 2105 | LoOverflow = AddWithOverflow(LoBound, Prod, |
| 2106 | cast<ConstantInt>(DivRHSH)); |
| 2107 | HiBound = Prod; |
| 2108 | HiOverflow = ProdOV; |
| 2109 | } |
| 2110 | } else { // Divisor is < 0. |
| 2111 | if (CI->isNullValue()) { // (X / neg) op 0 |
| 2112 | LoBound = AddOne(DivRHS); |
| 2113 | HiBound = cast<ConstantInt>(ConstantExpr::getNeg(DivRHS)); |
| 2114 | } else if (isPositive(CI)) { // (X / neg) op pos |
| 2115 | HiOverflow = LoOverflow = ProdOV; |
| 2116 | if (!LoOverflow) |
| 2117 | LoOverflow = AddWithOverflow(LoBound, Prod, AddOne(DivRHS)); |
| 2118 | HiBound = AddOne(Prod); |
| 2119 | } else { // (X / neg) op neg |
| 2120 | LoBound = Prod; |
| 2121 | LoOverflow = HiOverflow = ProdOV; |
| 2122 | HiBound = cast<ConstantInt>(ConstantExpr::getSub(Prod, DivRHS)); |
| 2123 | } |
| 2124 | } |
| 2125 | |
| 2126 | if (LoBound) { |
| 2127 | Value *X = LHSI->getOperand(0); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 2128 | switch (I.getOpcode()) { |
| 2129 | default: assert(0 && "Unhandled setcc opcode!"); |
| 2130 | case Instruction::SetEQ: |
| 2131 | if (LoOverflow && HiOverflow) |
| 2132 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 2133 | else if (HiOverflow) |
| 2134 | return new SetCondInst(Instruction::SetGE, X, LoBound); |
| 2135 | else if (LoOverflow) |
| 2136 | return new SetCondInst(Instruction::SetLT, X, HiBound); |
| 2137 | else |
| 2138 | return InsertRangeTest(X, LoBound, HiBound, true, I); |
| 2139 | case Instruction::SetNE: |
| 2140 | if (LoOverflow && HiOverflow) |
| 2141 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 2142 | else if (HiOverflow) |
| 2143 | return new SetCondInst(Instruction::SetLT, X, LoBound); |
| 2144 | else if (LoOverflow) |
| 2145 | return new SetCondInst(Instruction::SetGE, X, HiBound); |
| 2146 | else |
| 2147 | return InsertRangeTest(X, LoBound, HiBound, false, I); |
| 2148 | case Instruction::SetLT: |
| 2149 | if (LoOverflow) |
| 2150 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 2151 | return new SetCondInst(Instruction::SetLT, X, LoBound); |
| 2152 | case Instruction::SetGT: |
| 2153 | if (HiOverflow) |
| 2154 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 2155 | return new SetCondInst(Instruction::SetGE, X, HiBound); |
| 2156 | } |
| 2157 | } |
| 2158 | } |
| 2159 | break; |
Chris Lattner | 648e3bc | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 2160 | case Instruction::Select: |
| 2161 | // If either operand of the select is a constant, we can fold the |
| 2162 | // comparison into the select arms, which will cause one to be |
| 2163 | // constant folded and the select turned into a bitwise or. |
| 2164 | Value *Op1 = 0, *Op2 = 0; |
| 2165 | if (LHSI->hasOneUse()) { |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2166 | if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) { |
Chris Lattner | 2be51ae | 2004-06-09 04:24:29 +0000 | [diff] [blame] | 2167 | // Fold the known value into the constant operand. |
| 2168 | Op1 = ConstantExpr::get(I.getOpcode(), C, CI); |
| 2169 | // Insert a new SetCC of the other select operand. |
| 2170 | Op2 = InsertNewInstBefore(new SetCondInst(I.getOpcode(), |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2171 | LHSI->getOperand(2), CI, |
Chris Lattner | 2be51ae | 2004-06-09 04:24:29 +0000 | [diff] [blame] | 2172 | I.getName()), I); |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2173 | } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) { |
Chris Lattner | 2be51ae | 2004-06-09 04:24:29 +0000 | [diff] [blame] | 2174 | // Fold the known value into the constant operand. |
| 2175 | Op2 = ConstantExpr::get(I.getOpcode(), C, CI); |
| 2176 | // Insert a new SetCC of the other select operand. |
| 2177 | Op1 = InsertNewInstBefore(new SetCondInst(I.getOpcode(), |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2178 | LHSI->getOperand(1), CI, |
Chris Lattner | 2be51ae | 2004-06-09 04:24:29 +0000 | [diff] [blame] | 2179 | I.getName()), I); |
| 2180 | } |
Chris Lattner | 2be51ae | 2004-06-09 04:24:29 +0000 | [diff] [blame] | 2181 | } |
Chris Lattner | 648e3bc | 2004-09-23 21:52:49 +0000 | [diff] [blame] | 2182 | |
| 2183 | if (Op1) |
| 2184 | return new SelectInst(LHSI->getOperand(0), Op1, Op2); |
| 2185 | break; |
| 2186 | } |
| 2187 | |
Chris Lattner | bc5d414 | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 2188 | // Simplify seteq and setne instructions... |
| 2189 | if (I.getOpcode() == Instruction::SetEQ || |
| 2190 | I.getOpcode() == Instruction::SetNE) { |
| 2191 | bool isSetNE = I.getOpcode() == Instruction::SetNE; |
| 2192 | |
Chris Lattner | 00b1a7e | 2003-07-23 17:26:36 +0000 | [diff] [blame] | 2193 | // 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] | 2194 | // operand is a constant, simplify a bit. |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 2195 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0)) { |
| 2196 | switch (BO->getOpcode()) { |
Chris Lattner | 3571b72 | 2004-07-06 07:38:18 +0000 | [diff] [blame] | 2197 | case Instruction::Rem: |
| 2198 | // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one. |
| 2199 | if (CI->isNullValue() && isa<ConstantSInt>(BO->getOperand(1)) && |
| 2200 | BO->hasOneUse() && |
| 2201 | cast<ConstantSInt>(BO->getOperand(1))->getValue() > 1) |
| 2202 | if (unsigned L2 = |
| 2203 | Log2(cast<ConstantSInt>(BO->getOperand(1))->getValue())) { |
| 2204 | const Type *UTy = BO->getType()->getUnsignedVersion(); |
| 2205 | Value *NewX = InsertNewInstBefore(new CastInst(BO->getOperand(0), |
| 2206 | UTy, "tmp"), I); |
| 2207 | Constant *RHSCst = ConstantUInt::get(UTy, 1ULL << L2); |
| 2208 | Value *NewRem =InsertNewInstBefore(BinaryOperator::createRem(NewX, |
| 2209 | RHSCst, BO->getName()), I); |
| 2210 | return BinaryOperator::create(I.getOpcode(), NewRem, |
| 2211 | Constant::getNullValue(UTy)); |
| 2212 | } |
| 2213 | break; |
| 2214 | |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 2215 | case Instruction::Add: |
Chris Lattner | 15d58b6 | 2004-06-27 22:51:36 +0000 | [diff] [blame] | 2216 | // Replace ((add A, B) != C) with (A != C-B) if B & C are constants. |
| 2217 | if (ConstantInt *BOp1C = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
Chris Lattner | 3d834bf | 2004-09-21 21:35:23 +0000 | [diff] [blame] | 2218 | if (BO->hasOneUse()) |
| 2219 | return new SetCondInst(I.getOpcode(), BO->getOperand(0), |
| 2220 | ConstantExpr::getSub(CI, BOp1C)); |
Chris Lattner | 15d58b6 | 2004-06-27 22:51:36 +0000 | [diff] [blame] | 2221 | } else if (CI->isNullValue()) { |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 2222 | // Replace ((add A, B) != 0) with (A != -B) if A or B is |
| 2223 | // efficiently invertible, or if the add has just this one use. |
| 2224 | Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1); |
Chris Lattner | 15d58b6 | 2004-06-27 22:51:36 +0000 | [diff] [blame] | 2225 | |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 2226 | if (Value *NegVal = dyn_castNegVal(BOp1)) |
| 2227 | return new SetCondInst(I.getOpcode(), BOp0, NegVal); |
| 2228 | else if (Value *NegVal = dyn_castNegVal(BOp0)) |
| 2229 | return new SetCondInst(I.getOpcode(), NegVal, BOp1); |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 2230 | else if (BO->hasOneUse()) { |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 2231 | Instruction *Neg = BinaryOperator::createNeg(BOp1, BO->getName()); |
| 2232 | BO->setName(""); |
| 2233 | InsertNewInstBefore(Neg, I); |
| 2234 | return new SetCondInst(I.getOpcode(), BOp0, Neg); |
| 2235 | } |
| 2236 | } |
| 2237 | break; |
| 2238 | case Instruction::Xor: |
| 2239 | // For the xor case, we can xor two constants together, eliminating |
| 2240 | // the explicit xor. |
| 2241 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) |
| 2242 | return BinaryOperator::create(I.getOpcode(), BO->getOperand(0), |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2243 | ConstantExpr::getXor(CI, BOC)); |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 2244 | |
| 2245 | // FALLTHROUGH |
| 2246 | case Instruction::Sub: |
| 2247 | // Replace (([sub|xor] A, B) != 0) with (A != B) |
| 2248 | if (CI->isNullValue()) |
| 2249 | return new SetCondInst(I.getOpcode(), BO->getOperand(0), |
| 2250 | BO->getOperand(1)); |
| 2251 | break; |
| 2252 | |
| 2253 | case Instruction::Or: |
| 2254 | // If bits are being or'd in that are not present in the constant we |
| 2255 | // are comparing against, then the comparison could never succeed! |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2256 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) { |
Chris Lattner | 448c323 | 2004-06-10 02:12:35 +0000 | [diff] [blame] | 2257 | Constant *NotCI = ConstantExpr::getNot(CI); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2258 | if (!ConstantExpr::getAnd(BOC, NotCI)->isNullValue()) |
Chris Lattner | bc5d414 | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 2259 | return ReplaceInstUsesWith(I, ConstantBool::get(isSetNE)); |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2260 | } |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 2261 | break; |
| 2262 | |
| 2263 | case Instruction::And: |
| 2264 | if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
Chris Lattner | bc5d414 | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 2265 | // If bits are being compared against that are and'd out, then the |
| 2266 | // comparison can never succeed! |
Chris Lattner | 448c323 | 2004-06-10 02:12:35 +0000 | [diff] [blame] | 2267 | if (!ConstantExpr::getAnd(CI, |
| 2268 | ConstantExpr::getNot(BOC))->isNullValue()) |
Chris Lattner | bc5d414 | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 2269 | return ReplaceInstUsesWith(I, ConstantBool::get(isSetNE)); |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 2270 | |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2271 | // If we have ((X & C) == C), turn it into ((X & C) != 0). |
Chris Lattner | 3285a6f | 2004-06-10 02:33:20 +0000 | [diff] [blame] | 2272 | if (CI == BOC && isOneBitSet(CI)) |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2273 | return new SetCondInst(isSetNE ? Instruction::SetEQ : |
| 2274 | Instruction::SetNE, Op0, |
| 2275 | Constant::getNullValue(CI->getType())); |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2276 | |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 2277 | // Replace (and X, (1 << size(X)-1) != 0) with x < 0, converting X |
| 2278 | // to be a signed value as appropriate. |
| 2279 | if (isSignBit(BOC)) { |
| 2280 | Value *X = BO->getOperand(0); |
| 2281 | // If 'X' is not signed, insert a cast now... |
| 2282 | if (!BOC->getType()->isSigned()) { |
Chris Lattner | 5dd0402 | 2004-06-17 18:16:02 +0000 | [diff] [blame] | 2283 | const Type *DestTy = BOC->getType()->getSignedVersion(); |
Chris Lattner | 83c4ec0 | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 2284 | X = InsertCastBefore(X, DestTy, I); |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 2285 | } |
| 2286 | return new SetCondInst(isSetNE ? Instruction::SetLT : |
| 2287 | Instruction::SetGE, X, |
| 2288 | Constant::getNullValue(X->getType())); |
| 2289 | } |
Chris Lattner | b20ba0a | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2290 | |
Chris Lattner | 83c4ec0 | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 2291 | // ((X & ~7) == 0) --> X < 8 |
Chris Lattner | b20ba0a | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2292 | if (CI->isNullValue() && isHighOnes(BOC)) { |
| 2293 | Value *X = BO->getOperand(0); |
Chris Lattner | 83c4ec0 | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 2294 | Constant *NegX = ConstantExpr::getNeg(BOC); |
Chris Lattner | b20ba0a | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2295 | |
| 2296 | // If 'X' is signed, insert a cast now. |
Chris Lattner | 83c4ec0 | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 2297 | if (NegX->getType()->isSigned()) { |
| 2298 | const Type *DestTy = NegX->getType()->getUnsignedVersion(); |
| 2299 | X = InsertCastBefore(X, DestTy, I); |
| 2300 | NegX = ConstantExpr::getCast(NegX, DestTy); |
Chris Lattner | b20ba0a | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2301 | } |
| 2302 | |
| 2303 | return new SetCondInst(isSetNE ? Instruction::SetGE : |
Chris Lattner | 83c4ec0 | 2004-09-27 19:29:18 +0000 | [diff] [blame] | 2304 | Instruction::SetLT, X, NegX); |
Chris Lattner | b20ba0a | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2305 | } |
| 2306 | |
Chris Lattner | bc5d414 | 2003-07-23 17:02:11 +0000 | [diff] [blame] | 2307 | } |
Chris Lattner | 934754b | 2003-08-13 05:33:12 +0000 | [diff] [blame] | 2308 | default: break; |
| 2309 | } |
| 2310 | } |
Chris Lattner | c5943fb | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 2311 | } else { // Not a SetEQ/SetNE |
| 2312 | // If the LHS is a cast from an integral value of the same size, |
| 2313 | if (CastInst *Cast = dyn_cast<CastInst>(Op0)) { |
| 2314 | Value *CastOp = Cast->getOperand(0); |
| 2315 | const Type *SrcTy = CastOp->getType(); |
| 2316 | unsigned SrcTySize = SrcTy->getPrimitiveSize(); |
| 2317 | if (SrcTy != Cast->getType() && SrcTy->isInteger() && |
| 2318 | SrcTySize == Cast->getType()->getPrimitiveSize()) { |
| 2319 | assert((SrcTy->isSigned() ^ Cast->getType()->isSigned()) && |
| 2320 | "Source and destination signednesses should differ!"); |
| 2321 | if (Cast->getType()->isSigned()) { |
| 2322 | // If this is a signed comparison, check for comparisons in the |
| 2323 | // vicinity of zero. |
| 2324 | if (I.getOpcode() == Instruction::SetLT && CI->isNullValue()) |
| 2325 | // X < 0 => x > 127 |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2326 | return BinaryOperator::createSetGT(CastOp, |
Chris Lattner | c5943fb | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 2327 | ConstantUInt::get(SrcTy, (1ULL << (SrcTySize*8-1))-1)); |
| 2328 | else if (I.getOpcode() == Instruction::SetGT && |
| 2329 | cast<ConstantSInt>(CI)->getValue() == -1) |
| 2330 | // X > -1 => x < 128 |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2331 | return BinaryOperator::createSetLT(CastOp, |
Chris Lattner | c5943fb | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 2332 | ConstantUInt::get(SrcTy, 1ULL << (SrcTySize*8-1))); |
| 2333 | } else { |
| 2334 | ConstantUInt *CUI = cast<ConstantUInt>(CI); |
| 2335 | if (I.getOpcode() == Instruction::SetLT && |
| 2336 | CUI->getValue() == 1ULL << (SrcTySize*8-1)) |
| 2337 | // X < 128 => X > -1 |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2338 | return BinaryOperator::createSetGT(CastOp, |
| 2339 | ConstantSInt::get(SrcTy, -1)); |
Chris Lattner | c5943fb | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 2340 | else if (I.getOpcode() == Instruction::SetGT && |
| 2341 | CUI->getValue() == (1ULL << (SrcTySize*8-1))-1) |
| 2342 | // X > 127 => X < 0 |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2343 | return BinaryOperator::createSetLT(CastOp, |
| 2344 | Constant::getNullValue(SrcTy)); |
Chris Lattner | c5943fb | 2004-02-23 07:16:20 +0000 | [diff] [blame] | 2345 | } |
| 2346 | } |
| 2347 | } |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 2348 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2349 | } |
| 2350 | |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 2351 | // Test to see if the operands of the setcc are casted versions of other |
| 2352 | // 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] | 2353 | if (CastInst *CI = dyn_cast<CastInst>(Op0)) { |
| 2354 | Value *CastOp0 = CI->getOperand(0); |
| 2355 | if (CastOp0->getType()->isLosslesslyConvertibleTo(CI->getType()) && |
Chris Lattner | 0cea42a | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 2356 | (isa<Constant>(Op1) || isa<CastInst>(Op1)) && |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 2357 | (I.getOpcode() == Instruction::SetEQ || |
| 2358 | I.getOpcode() == Instruction::SetNE)) { |
| 2359 | // We keep moving the cast from the left operand over to the right |
| 2360 | // operand, where it can often be eliminated completely. |
Chris Lattner | 6870805 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 2361 | Op0 = CastOp0; |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 2362 | |
| 2363 | // If operand #1 is a cast instruction, see if we can eliminate it as |
| 2364 | // well. |
Chris Lattner | 6870805 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 2365 | if (CastInst *CI2 = dyn_cast<CastInst>(Op1)) |
| 2366 | if (CI2->getOperand(0)->getType()->isLosslesslyConvertibleTo( |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 2367 | Op0->getType())) |
Chris Lattner | 6870805 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 2368 | Op1 = CI2->getOperand(0); |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 2369 | |
| 2370 | // If Op1 is a constant, we can fold the cast into the constant. |
| 2371 | if (Op1->getType() != Op0->getType()) |
| 2372 | if (Constant *Op1C = dyn_cast<Constant>(Op1)) { |
| 2373 | Op1 = ConstantExpr::getCast(Op1C, Op0->getType()); |
| 2374 | } else { |
| 2375 | // Otherwise, cast the RHS right before the setcc |
| 2376 | Op1 = new CastInst(Op1, Op0->getType(), Op1->getName()); |
| 2377 | InsertNewInstBefore(cast<Instruction>(Op1), I); |
| 2378 | } |
| 2379 | return BinaryOperator::create(I.getOpcode(), Op0, Op1); |
| 2380 | } |
| 2381 | |
Chris Lattner | 6870805 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 2382 | // Handle the special case of: setcc (cast bool to X), <cst> |
| 2383 | // This comes up when you have code like |
| 2384 | // int X = A < B; |
| 2385 | // if (X) ... |
| 2386 | // For generality, we handle any zero-extension of any operand comparison |
| 2387 | // with a constant. |
| 2388 | if (ConstantInt *ConstantRHS = dyn_cast<ConstantInt>(Op1)) { |
| 2389 | const Type *SrcTy = CastOp0->getType(); |
| 2390 | const Type *DestTy = Op0->getType(); |
| 2391 | if (SrcTy->getPrimitiveSize() < DestTy->getPrimitiveSize() && |
| 2392 | (SrcTy->isUnsigned() || SrcTy == Type::BoolTy)) { |
| 2393 | // Ok, we have an expansion of operand 0 into a new type. Get the |
| 2394 | // constant value, masink off bits which are not set in the RHS. These |
| 2395 | // could be set if the destination value is signed. |
| 2396 | uint64_t ConstVal = ConstantRHS->getRawValue(); |
| 2397 | ConstVal &= (1ULL << DestTy->getPrimitiveSize()*8)-1; |
| 2398 | |
| 2399 | // If the constant we are comparing it with has high bits set, which |
| 2400 | // don't exist in the original value, the values could never be equal, |
| 2401 | // because the source would be zero extended. |
| 2402 | unsigned SrcBits = |
| 2403 | SrcTy == Type::BoolTy ? 1 : SrcTy->getPrimitiveSize()*8; |
Chris Lattner | 1bcc70d | 2003-11-05 17:31:36 +0000 | [diff] [blame] | 2404 | bool HasSignBit = ConstVal & (1ULL << (DestTy->getPrimitiveSize()*8-1)); |
| 2405 | if (ConstVal & ~((1ULL << SrcBits)-1)) { |
Chris Lattner | 6870805 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 2406 | switch (I.getOpcode()) { |
| 2407 | default: assert(0 && "Unknown comparison type!"); |
| 2408 | case Instruction::SetEQ: |
| 2409 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 2410 | case Instruction::SetNE: |
| 2411 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 2412 | case Instruction::SetLT: |
| 2413 | case Instruction::SetLE: |
| 2414 | if (DestTy->isSigned() && HasSignBit) |
| 2415 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 2416 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 2417 | case Instruction::SetGT: |
| 2418 | case Instruction::SetGE: |
| 2419 | if (DestTy->isSigned() && HasSignBit) |
| 2420 | return ReplaceInstUsesWith(I, ConstantBool::True); |
| 2421 | return ReplaceInstUsesWith(I, ConstantBool::False); |
| 2422 | } |
| 2423 | } |
| 2424 | |
| 2425 | // Otherwise, we can replace the setcc with a setcc of the smaller |
| 2426 | // operand value. |
| 2427 | Op1 = ConstantExpr::getCast(cast<Constant>(Op1), SrcTy); |
| 2428 | return BinaryOperator::create(I.getOpcode(), CastOp0, Op1); |
| 2429 | } |
| 2430 | } |
| 2431 | } |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2432 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2433 | } |
| 2434 | |
| 2435 | |
| 2436 | |
Chris Lattner | ea34005 | 2003-03-10 19:16:08 +0000 | [diff] [blame] | 2437 | Instruction *InstCombiner::visitShiftInst(ShiftInst &I) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2438 | assert(I.getOperand(1)->getType() == Type::UByteTy); |
| 2439 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 2440 | bool isLeftShift = I.getOpcode() == Instruction::Shl; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2441 | |
| 2442 | // shl X, 0 == X and shr X, 0 == X |
| 2443 | // shl 0, X == 0 and shr 0, X == 0 |
| 2444 | if (Op1 == Constant::getNullValue(Type::UByteTy) || |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2445 | Op0 == Constant::getNullValue(Op0->getType())) |
| 2446 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2447 | |
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 2448 | // shr int -1, X = -1 (for any arithmetic shift rights of ~0) |
| 2449 | if (!isLeftShift) |
| 2450 | if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(Op0)) |
| 2451 | if (CSI->isAllOnesValue()) |
| 2452 | return ReplaceInstUsesWith(I, CSI); |
| 2453 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2454 | // Try to fold constant and into select arguments. |
| 2455 | if (isa<Constant>(Op0)) |
| 2456 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
| 2457 | if (Instruction *R = FoldBinOpIntoSelect(I, SI, this)) |
| 2458 | return R; |
| 2459 | |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2460 | if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(Op1)) { |
Chris Lattner | 08fd7ab | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 2461 | // shl uint X, 32 = 0 and shr ubyte Y, 9 = 0, ... just don't eliminate shr |
| 2462 | // of a signed value. |
| 2463 | // |
Chris Lattner | ea34005 | 2003-03-10 19:16:08 +0000 | [diff] [blame] | 2464 | unsigned TypeBits = Op0->getType()->getPrimitiveSize()*8; |
Chris Lattner | 8adac75 | 2004-02-23 20:30:06 +0000 | [diff] [blame] | 2465 | if (CUI->getValue() >= TypeBits) { |
| 2466 | if (!Op0->getType()->isSigned() || isLeftShift) |
| 2467 | return ReplaceInstUsesWith(I, Constant::getNullValue(Op0->getType())); |
| 2468 | else { |
| 2469 | I.setOperand(1, ConstantUInt::get(Type::UByteTy, TypeBits-1)); |
| 2470 | return &I; |
| 2471 | } |
| 2472 | } |
Chris Lattner | f283608 | 2002-09-10 23:04:09 +0000 | [diff] [blame] | 2473 | |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 2474 | // ((X*C1) << C2) == (X * (C1 << C2)) |
| 2475 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0)) |
| 2476 | if (BO->getOpcode() == Instruction::Mul && isLeftShift) |
| 2477 | if (Constant *BOOp = dyn_cast<Constant>(BO->getOperand(1))) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2478 | return BinaryOperator::createMul(BO->getOperand(0), |
| 2479 | ConstantExpr::getShl(BOOp, CUI)); |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 2480 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2481 | // Try to fold constant and into select arguments. |
| 2482 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 2483 | if (Instruction *R = FoldBinOpIntoSelect(I, SI, this)) |
| 2484 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2485 | if (isa<PHINode>(Op0)) |
| 2486 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2487 | return NV; |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 2488 | |
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 2489 | // If the operand is an bitwise operator with a constant RHS, and the |
| 2490 | // shift is the only use, we can pull it out of the shift. |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 2491 | if (Op0->hasOneUse()) |
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 2492 | if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0)) |
| 2493 | if (ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand(1))) { |
| 2494 | bool isValid = true; // Valid only for And, Or, Xor |
| 2495 | bool highBitSet = false; // Transform if high bit of constant set? |
| 2496 | |
| 2497 | switch (Op0BO->getOpcode()) { |
| 2498 | default: isValid = false; break; // Do not perform transform! |
| 2499 | case Instruction::Or: |
| 2500 | case Instruction::Xor: |
| 2501 | highBitSet = false; |
| 2502 | break; |
| 2503 | case Instruction::And: |
| 2504 | highBitSet = true; |
| 2505 | break; |
| 2506 | } |
| 2507 | |
| 2508 | // If this is a signed shift right, and the high bit is modified |
| 2509 | // by the logical operation, do not perform the transformation. |
| 2510 | // The highBitSet boolean indicates the value of the high bit of |
| 2511 | // the constant which would cause it to be modified for this |
| 2512 | // operation. |
| 2513 | // |
| 2514 | if (isValid && !isLeftShift && !I.getType()->isUnsigned()) { |
| 2515 | uint64_t Val = Op0C->getRawValue(); |
| 2516 | isValid = ((Val & (1 << (TypeBits-1))) != 0) == highBitSet; |
| 2517 | } |
| 2518 | |
| 2519 | if (isValid) { |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2520 | Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, CUI); |
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 2521 | |
| 2522 | Instruction *NewShift = |
| 2523 | new ShiftInst(I.getOpcode(), Op0BO->getOperand(0), CUI, |
| 2524 | Op0BO->getName()); |
| 2525 | Op0BO->setName(""); |
| 2526 | InsertNewInstBefore(NewShift, I); |
| 2527 | |
| 2528 | return BinaryOperator::create(Op0BO->getOpcode(), NewShift, |
| 2529 | NewRHS); |
| 2530 | } |
| 2531 | } |
| 2532 | |
Chris Lattner | 08fd7ab | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 2533 | // 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] | 2534 | if (ShiftInst *Op0SI = dyn_cast<ShiftInst>(Op0)) |
Chris Lattner | 943c713 | 2003-07-24 18:38:56 +0000 | [diff] [blame] | 2535 | if (ConstantUInt *ShiftAmt1C = |
| 2536 | dyn_cast<ConstantUInt>(Op0SI->getOperand(1))) { |
Chris Lattner | 08fd7ab | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 2537 | unsigned ShiftAmt1 = ShiftAmt1C->getValue(); |
| 2538 | unsigned ShiftAmt2 = CUI->getValue(); |
| 2539 | |
| 2540 | // Check for (A << c1) << c2 and (A >> c1) >> c2 |
| 2541 | if (I.getOpcode() == Op0SI->getOpcode()) { |
| 2542 | unsigned Amt = ShiftAmt1+ShiftAmt2; // Fold into one big shift... |
Chris Lattner | 8adac75 | 2004-02-23 20:30:06 +0000 | [diff] [blame] | 2543 | if (Op0->getType()->getPrimitiveSize()*8 < Amt) |
| 2544 | Amt = Op0->getType()->getPrimitiveSize()*8; |
Chris Lattner | 08fd7ab | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 2545 | return new ShiftInst(I.getOpcode(), Op0SI->getOperand(0), |
| 2546 | ConstantUInt::get(Type::UByteTy, Amt)); |
| 2547 | } |
| 2548 | |
Chris Lattner | 943c713 | 2003-07-24 18:38:56 +0000 | [diff] [blame] | 2549 | // Check for (A << c1) >> c2 or visaversa. If we are dealing with |
| 2550 | // signed types, we can only support the (A >> c1) << c2 configuration, |
| 2551 | // 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] | 2552 | if (I.getType()->isUnsigned() || isLeftShift) { |
Chris Lattner | 08fd7ab | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 2553 | // Calculate bitmask for what gets shifted off the edge... |
| 2554 | Constant *C = ConstantIntegral::getAllOnesValue(I.getType()); |
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 2555 | if (isLeftShift) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2556 | C = ConstantExpr::getShl(C, ShiftAmt1C); |
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 2557 | else |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2558 | C = ConstantExpr::getShr(C, ShiftAmt1C); |
Chris Lattner | 08fd7ab | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 2559 | |
| 2560 | Instruction *Mask = |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2561 | BinaryOperator::createAnd(Op0SI->getOperand(0), C, |
| 2562 | Op0SI->getOperand(0)->getName()+".mask"); |
Chris Lattner | 08fd7ab | 2003-07-24 17:52:58 +0000 | [diff] [blame] | 2563 | InsertNewInstBefore(Mask, I); |
| 2564 | |
| 2565 | // Figure out what flavor of shift we should use... |
| 2566 | if (ShiftAmt1 == ShiftAmt2) |
| 2567 | return ReplaceInstUsesWith(I, Mask); // (A << c) >> c === A & c2 |
| 2568 | else if (ShiftAmt1 < ShiftAmt2) { |
| 2569 | return new ShiftInst(I.getOpcode(), Mask, |
| 2570 | ConstantUInt::get(Type::UByteTy, ShiftAmt2-ShiftAmt1)); |
| 2571 | } else { |
| 2572 | return new ShiftInst(Op0SI->getOpcode(), Mask, |
| 2573 | ConstantUInt::get(Type::UByteTy, ShiftAmt1-ShiftAmt2)); |
| 2574 | } |
| 2575 | } |
| 2576 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2577 | } |
Chris Lattner | 6eaeb57 | 2002-10-08 16:16:40 +0000 | [diff] [blame] | 2578 | |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2579 | return 0; |
| 2580 | } |
| 2581 | |
Chris Lattner | bee7e76 | 2004-07-20 00:59:32 +0000 | [diff] [blame] | 2582 | enum CastType { |
| 2583 | Noop = 0, |
| 2584 | Truncate = 1, |
| 2585 | Signext = 2, |
| 2586 | Zeroext = 3 |
| 2587 | }; |
| 2588 | |
| 2589 | /// getCastType - In the future, we will split the cast instruction into these |
| 2590 | /// various types. Until then, we have to do the analysis here. |
| 2591 | static CastType getCastType(const Type *Src, const Type *Dest) { |
| 2592 | assert(Src->isIntegral() && Dest->isIntegral() && |
| 2593 | "Only works on integral types!"); |
| 2594 | unsigned SrcSize = Src->getPrimitiveSize()*8; |
| 2595 | if (Src == Type::BoolTy) SrcSize = 1; |
| 2596 | unsigned DestSize = Dest->getPrimitiveSize()*8; |
| 2597 | if (Dest == Type::BoolTy) DestSize = 1; |
| 2598 | |
| 2599 | if (SrcSize == DestSize) return Noop; |
| 2600 | if (SrcSize > DestSize) return Truncate; |
| 2601 | if (Src->isSigned()) return Signext; |
| 2602 | return Zeroext; |
| 2603 | } |
| 2604 | |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2605 | |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 2606 | // isEliminableCastOfCast - Return true if it is valid to eliminate the CI |
| 2607 | // instruction. |
| 2608 | // |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 2609 | static inline bool isEliminableCastOfCast(const Type *SrcTy, const Type *MidTy, |
Chris Lattner | 59a2077 | 2004-07-20 05:21:00 +0000 | [diff] [blame] | 2610 | const Type *DstTy, TargetData *TD) { |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 2611 | |
Chris Lattner | 8fd217c | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 2612 | // It is legal to eliminate the instruction if casting A->B->A if the sizes |
| 2613 | // are identical and the bits don't get reinterpreted (for example |
Chris Lattner | 5eb9194 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 2614 | // int->float->int would not be allowed). |
Misha Brukman | f117cc9 | 2003-05-20 18:45:36 +0000 | [diff] [blame] | 2615 | if (SrcTy == DstTy && SrcTy->isLosslesslyConvertibleTo(MidTy)) |
Chris Lattner | 8fd217c | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 2616 | return true; |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 2617 | |
Chris Lattner | e8a7e59 | 2004-07-21 04:27:24 +0000 | [diff] [blame] | 2618 | // If we are casting between pointer and integer types, treat pointers as |
| 2619 | // integers of the appropriate size for the code below. |
| 2620 | if (isa<PointerType>(SrcTy)) SrcTy = TD->getIntPtrType(); |
| 2621 | if (isa<PointerType>(MidTy)) MidTy = TD->getIntPtrType(); |
| 2622 | if (isa<PointerType>(DstTy)) DstTy = TD->getIntPtrType(); |
Chris Lattner | 59a2077 | 2004-07-20 05:21:00 +0000 | [diff] [blame] | 2623 | |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 2624 | // Allow free casting and conversion of sizes as long as the sign doesn't |
| 2625 | // change... |
Chris Lattner | 0c4e886 | 2002-09-03 01:08:28 +0000 | [diff] [blame] | 2626 | if (SrcTy->isIntegral() && MidTy->isIntegral() && DstTy->isIntegral()) { |
Chris Lattner | bee7e76 | 2004-07-20 00:59:32 +0000 | [diff] [blame] | 2627 | CastType FirstCast = getCastType(SrcTy, MidTy); |
| 2628 | CastType SecondCast = getCastType(MidTy, DstTy); |
Chris Lattner | 8fd217c | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 2629 | |
Chris Lattner | bee7e76 | 2004-07-20 00:59:32 +0000 | [diff] [blame] | 2630 | // Capture the effect of these two casts. If the result is a legal cast, |
| 2631 | // the CastType is stored here, otherwise a special code is used. |
| 2632 | static const unsigned CastResult[] = { |
| 2633 | // First cast is noop |
| 2634 | 0, 1, 2, 3, |
| 2635 | // First cast is a truncate |
| 2636 | 1, 1, 4, 4, // trunc->extend is not safe to eliminate |
| 2637 | // First cast is a sign ext |
Chris Lattner | 5eb9194 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 2638 | 2, 5, 2, 4, // signext->zeroext never ok |
Chris Lattner | bee7e76 | 2004-07-20 00:59:32 +0000 | [diff] [blame] | 2639 | // First cast is a zero ext |
Chris Lattner | 5eb9194 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 2640 | 3, 5, 3, 3, |
Chris Lattner | bee7e76 | 2004-07-20 00:59:32 +0000 | [diff] [blame] | 2641 | }; |
| 2642 | |
| 2643 | unsigned Result = CastResult[FirstCast*4+SecondCast]; |
| 2644 | switch (Result) { |
| 2645 | default: assert(0 && "Illegal table value!"); |
| 2646 | case 0: |
| 2647 | case 1: |
| 2648 | case 2: |
| 2649 | case 3: |
| 2650 | // FIXME: in the future, when LLVM has explicit sign/zeroextends and |
| 2651 | // truncates, we could eliminate more casts. |
| 2652 | return (unsigned)getCastType(SrcTy, DstTy) == Result; |
| 2653 | case 4: |
| 2654 | return false; // Not possible to eliminate this here. |
| 2655 | case 5: |
Chris Lattner | 5eb9194 | 2004-07-21 19:50:44 +0000 | [diff] [blame] | 2656 | // Sign or zero extend followed by truncate is always ok if the result |
| 2657 | // is a truncate or noop. |
| 2658 | CastType ResultCast = getCastType(SrcTy, DstTy); |
| 2659 | if (ResultCast == Noop || ResultCast == Truncate) |
| 2660 | return true; |
| 2661 | // Otherwise we are still growing the value, we are only safe if the |
| 2662 | // result will match the sign/zeroextendness of the result. |
| 2663 | return ResultCast == FirstCast; |
Chris Lattner | 3ecce66 | 2002-08-15 16:15:25 +0000 | [diff] [blame] | 2664 | } |
Chris Lattner | 8fd217c | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 2665 | } |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 2666 | return false; |
| 2667 | } |
| 2668 | |
Chris Lattner | 59a2077 | 2004-07-20 05:21:00 +0000 | [diff] [blame] | 2669 | static bool ValueRequiresCast(const Value *V, const Type *Ty, TargetData *TD) { |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 2670 | if (V->getType() == Ty || isa<Constant>(V)) return false; |
| 2671 | if (const CastInst *CI = dyn_cast<CastInst>(V)) |
Chris Lattner | 59a2077 | 2004-07-20 05:21:00 +0000 | [diff] [blame] | 2672 | if (isEliminableCastOfCast(CI->getOperand(0)->getType(), CI->getType(), Ty, |
| 2673 | TD)) |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 2674 | return false; |
| 2675 | return true; |
| 2676 | } |
| 2677 | |
| 2678 | /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the |
| 2679 | /// InsertBefore instruction. This is specialized a bit to avoid inserting |
| 2680 | /// casts that are known to not do anything... |
| 2681 | /// |
| 2682 | Value *InstCombiner::InsertOperandCastBefore(Value *V, const Type *DestTy, |
| 2683 | Instruction *InsertBefore) { |
| 2684 | if (V->getType() == DestTy) return V; |
| 2685 | if (Constant *C = dyn_cast<Constant>(V)) |
| 2686 | return ConstantExpr::getCast(C, DestTy); |
| 2687 | |
| 2688 | CastInst *CI = new CastInst(V, DestTy, V->getName()); |
| 2689 | InsertNewInstBefore(CI, *InsertBefore); |
| 2690 | return CI; |
| 2691 | } |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 2692 | |
| 2693 | // CastInst simplification |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2694 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2695 | Instruction *InstCombiner::visitCastInst(CastInst &CI) { |
Chris Lattner | 79d35b3 | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 2696 | Value *Src = CI.getOperand(0); |
| 2697 | |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 2698 | // If the user is casting a value to the same type, eliminate this cast |
| 2699 | // instruction... |
Chris Lattner | 79d35b3 | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 2700 | if (CI.getType() == Src->getType()) |
| 2701 | return ReplaceInstUsesWith(CI, Src); |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 2702 | |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 2703 | // If casting the result of another cast instruction, try to eliminate this |
| 2704 | // one! |
| 2705 | // |
Chris Lattner | 79d35b3 | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 2706 | if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 2707 | if (isEliminableCastOfCast(CSrc->getOperand(0)->getType(), |
Chris Lattner | 59a2077 | 2004-07-20 05:21:00 +0000 | [diff] [blame] | 2708 | CSrc->getType(), CI.getType(), TD)) { |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 2709 | // This instruction now refers directly to the cast's src operand. This |
| 2710 | // has a good chance of making CSrc dead. |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2711 | CI.setOperand(0, CSrc->getOperand(0)); |
| 2712 | return &CI; |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 2713 | } |
| 2714 | |
Chris Lattner | 8fd217c | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 2715 | // If this is an A->B->A cast, and we are dealing with integral types, try |
| 2716 | // to convert this into a logical 'and' instruction. |
| 2717 | // |
| 2718 | if (CSrc->getOperand(0)->getType() == CI.getType() && |
Chris Lattner | 0c4e886 | 2002-09-03 01:08:28 +0000 | [diff] [blame] | 2719 | CI.getType()->isInteger() && CSrc->getType()->isInteger() && |
Chris Lattner | 8fd217c | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 2720 | CI.getType()->isUnsigned() && CSrc->getType()->isUnsigned() && |
| 2721 | CSrc->getType()->getPrimitiveSize() < CI.getType()->getPrimitiveSize()){ |
| 2722 | assert(CSrc->getType() != Type::ULongTy && |
| 2723 | "Cannot have type bigger than ulong!"); |
Chris Lattner | bd4ecf7 | 2003-05-26 23:41:32 +0000 | [diff] [blame] | 2724 | uint64_t AndValue = (1ULL << CSrc->getType()->getPrimitiveSize()*8)-1; |
Chris Lattner | 8fd217c | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 2725 | Constant *AndOp = ConstantUInt::get(CI.getType(), AndValue); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2726 | return BinaryOperator::createAnd(CSrc->getOperand(0), AndOp); |
Chris Lattner | 8fd217c | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 2727 | } |
| 2728 | } |
| 2729 | |
Chris Lattner | a710ddc | 2004-05-25 04:29:21 +0000 | [diff] [blame] | 2730 | // If this is a cast to bool, turn it into the appropriate setne instruction. |
| 2731 | if (CI.getType() == Type::BoolTy) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2732 | return BinaryOperator::createSetNE(CI.getOperand(0), |
Chris Lattner | a710ddc | 2004-05-25 04:29:21 +0000 | [diff] [blame] | 2733 | Constant::getNullValue(CI.getOperand(0)->getType())); |
| 2734 | |
Chris Lattner | 797249b | 2003-06-21 23:12:02 +0000 | [diff] [blame] | 2735 | // If casting the result of a getelementptr instruction with no offset, turn |
| 2736 | // this into a cast of the original pointer! |
| 2737 | // |
Chris Lattner | 79d35b3 | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 2738 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Src)) { |
Chris Lattner | 797249b | 2003-06-21 23:12:02 +0000 | [diff] [blame] | 2739 | bool AllZeroOperands = true; |
| 2740 | for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i) |
| 2741 | if (!isa<Constant>(GEP->getOperand(i)) || |
| 2742 | !cast<Constant>(GEP->getOperand(i))->isNullValue()) { |
| 2743 | AllZeroOperands = false; |
| 2744 | break; |
| 2745 | } |
| 2746 | if (AllZeroOperands) { |
| 2747 | CI.setOperand(0, GEP->getOperand(0)); |
| 2748 | return &CI; |
| 2749 | } |
| 2750 | } |
| 2751 | |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 2752 | // If we are casting a malloc or alloca to a pointer to a type of the same |
| 2753 | // size, rewrite the allocation instruction to allocate the "right" type. |
| 2754 | // |
| 2755 | if (AllocationInst *AI = dyn_cast<AllocationInst>(Src)) |
Chris Lattner | fc07a34 | 2003-11-02 06:54:48 +0000 | [diff] [blame] | 2756 | if (AI->hasOneUse() && !AI->isArrayAllocation()) |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 2757 | if (const PointerType *PTy = dyn_cast<PointerType>(CI.getType())) { |
| 2758 | // Get the type really allocated and the type casted to... |
| 2759 | const Type *AllocElTy = AI->getAllocatedType(); |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 2760 | const Type *CastElTy = PTy->getElementType(); |
Chris Lattner | fae1010 | 2004-07-06 19:28:42 +0000 | [diff] [blame] | 2761 | if (AllocElTy->isSized() && CastElTy->isSized()) { |
| 2762 | unsigned AllocElTySize = TD->getTypeSize(AllocElTy); |
| 2763 | unsigned CastElTySize = TD->getTypeSize(CastElTy); |
Chris Lattner | 1bcc70d | 2003-11-05 17:31:36 +0000 | [diff] [blame] | 2764 | |
Chris Lattner | fae1010 | 2004-07-06 19:28:42 +0000 | [diff] [blame] | 2765 | // If the allocation is for an even multiple of the cast type size |
| 2766 | if (CastElTySize && (AllocElTySize % CastElTySize == 0)) { |
| 2767 | Value *Amt = ConstantUInt::get(Type::UIntTy, |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 2768 | AllocElTySize/CastElTySize); |
Chris Lattner | fae1010 | 2004-07-06 19:28:42 +0000 | [diff] [blame] | 2769 | std::string Name = AI->getName(); AI->setName(""); |
| 2770 | AllocationInst *New; |
| 2771 | if (isa<MallocInst>(AI)) |
| 2772 | New = new MallocInst(CastElTy, Amt, Name); |
| 2773 | else |
| 2774 | New = new AllocaInst(CastElTy, Amt, Name); |
| 2775 | InsertNewInstBefore(New, *AI); |
| 2776 | return ReplaceInstUsesWith(CI, New); |
| 2777 | } |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 2778 | } |
| 2779 | } |
| 2780 | |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2781 | if (isa<PHINode>(Src)) |
| 2782 | if (Instruction *NV = FoldOpIntoPhi(CI)) |
| 2783 | return NV; |
| 2784 | |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 2785 | // If the source value is an instruction with only this use, we can attempt to |
| 2786 | // propagate the cast into the instruction. Also, only handle integral types |
| 2787 | // for now. |
| 2788 | if (Instruction *SrcI = dyn_cast<Instruction>(Src)) |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 2789 | if (SrcI->hasOneUse() && Src->getType()->isIntegral() && |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 2790 | CI.getType()->isInteger()) { // Don't mess with casts to bool here |
| 2791 | const Type *DestTy = CI.getType(); |
| 2792 | unsigned SrcBitSize = getTypeSizeInBits(Src->getType()); |
| 2793 | unsigned DestBitSize = getTypeSizeInBits(DestTy); |
| 2794 | |
| 2795 | Value *Op0 = SrcI->getNumOperands() > 0 ? SrcI->getOperand(0) : 0; |
| 2796 | Value *Op1 = SrcI->getNumOperands() > 1 ? SrcI->getOperand(1) : 0; |
| 2797 | |
| 2798 | switch (SrcI->getOpcode()) { |
| 2799 | case Instruction::Add: |
| 2800 | case Instruction::Mul: |
| 2801 | case Instruction::And: |
| 2802 | case Instruction::Or: |
| 2803 | case Instruction::Xor: |
| 2804 | // If we are discarding information, or just changing the sign, rewrite. |
| 2805 | if (DestBitSize <= SrcBitSize && DestBitSize != 1) { |
| 2806 | // Don't insert two casts if they cannot be eliminated. We allow two |
| 2807 | // casts to be inserted if the sizes are the same. This could only be |
| 2808 | // converting signedness, which is a noop. |
Chris Lattner | 59a2077 | 2004-07-20 05:21:00 +0000 | [diff] [blame] | 2809 | if (DestBitSize == SrcBitSize || !ValueRequiresCast(Op1, DestTy,TD) || |
| 2810 | !ValueRequiresCast(Op0, DestTy, TD)) { |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 2811 | Value *Op0c = InsertOperandCastBefore(Op0, DestTy, SrcI); |
| 2812 | Value *Op1c = InsertOperandCastBefore(Op1, DestTy, SrcI); |
| 2813 | return BinaryOperator::create(cast<BinaryOperator>(SrcI) |
| 2814 | ->getOpcode(), Op0c, Op1c); |
| 2815 | } |
| 2816 | } |
| 2817 | break; |
| 2818 | case Instruction::Shl: |
| 2819 | // Allow changing the sign of the source operand. Do not allow changing |
| 2820 | // the size of the shift, UNLESS the shift amount is a constant. We |
| 2821 | // mush not change variable sized shifts to a smaller size, because it |
| 2822 | // is undefined to shift more bits out than exist in the value. |
| 2823 | if (DestBitSize == SrcBitSize || |
| 2824 | (DestBitSize < SrcBitSize && isa<Constant>(Op1))) { |
| 2825 | Value *Op0c = InsertOperandCastBefore(Op0, DestTy, SrcI); |
| 2826 | return new ShiftInst(Instruction::Shl, Op0c, Op1); |
| 2827 | } |
| 2828 | break; |
| 2829 | } |
| 2830 | } |
| 2831 | |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2832 | return 0; |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 2833 | } |
| 2834 | |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 2835 | /// GetSelectFoldableOperands - We want to turn code that looks like this: |
| 2836 | /// %C = or %A, %B |
| 2837 | /// %D = select %cond, %C, %A |
| 2838 | /// into: |
| 2839 | /// %C = select %cond, %B, 0 |
| 2840 | /// %D = or %A, %C |
| 2841 | /// |
| 2842 | /// Assuming that the specified instruction is an operand to the select, return |
| 2843 | /// a bitmask indicating which operands of this instruction are foldable if they |
| 2844 | /// equal the other incoming value of the select. |
| 2845 | /// |
| 2846 | static unsigned GetSelectFoldableOperands(Instruction *I) { |
| 2847 | switch (I->getOpcode()) { |
| 2848 | case Instruction::Add: |
| 2849 | case Instruction::Mul: |
| 2850 | case Instruction::And: |
| 2851 | case Instruction::Or: |
| 2852 | case Instruction::Xor: |
| 2853 | return 3; // Can fold through either operand. |
| 2854 | case Instruction::Sub: // Can only fold on the amount subtracted. |
| 2855 | case Instruction::Shl: // Can only fold on the shift amount. |
| 2856 | case Instruction::Shr: |
| 2857 | return 1; |
| 2858 | default: |
| 2859 | return 0; // Cannot fold |
| 2860 | } |
| 2861 | } |
| 2862 | |
| 2863 | /// GetSelectFoldableConstant - For the same transformation as the previous |
| 2864 | /// function, return the identity constant that goes into the select. |
| 2865 | static Constant *GetSelectFoldableConstant(Instruction *I) { |
| 2866 | switch (I->getOpcode()) { |
| 2867 | default: assert(0 && "This cannot happen!"); abort(); |
| 2868 | case Instruction::Add: |
| 2869 | case Instruction::Sub: |
| 2870 | case Instruction::Or: |
| 2871 | case Instruction::Xor: |
| 2872 | return Constant::getNullValue(I->getType()); |
| 2873 | case Instruction::Shl: |
| 2874 | case Instruction::Shr: |
| 2875 | return Constant::getNullValue(Type::UByteTy); |
| 2876 | case Instruction::And: |
| 2877 | return ConstantInt::getAllOnesValue(I->getType()); |
| 2878 | case Instruction::Mul: |
| 2879 | return ConstantInt::get(I->getType(), 1); |
| 2880 | } |
| 2881 | } |
| 2882 | |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 2883 | Instruction *InstCombiner::visitSelectInst(SelectInst &SI) { |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 2884 | Value *CondVal = SI.getCondition(); |
| 2885 | Value *TrueVal = SI.getTrueValue(); |
| 2886 | Value *FalseVal = SI.getFalseValue(); |
| 2887 | |
| 2888 | // select true, X, Y -> X |
| 2889 | // select false, X, Y -> Y |
| 2890 | if (ConstantBool *C = dyn_cast<ConstantBool>(CondVal)) |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 2891 | if (C == ConstantBool::True) |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 2892 | return ReplaceInstUsesWith(SI, TrueVal); |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 2893 | else { |
| 2894 | assert(C == ConstantBool::False); |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 2895 | return ReplaceInstUsesWith(SI, FalseVal); |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 2896 | } |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 2897 | |
| 2898 | // select C, X, X -> X |
| 2899 | if (TrueVal == FalseVal) |
| 2900 | return ReplaceInstUsesWith(SI, TrueVal); |
| 2901 | |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 2902 | if (SI.getType() == Type::BoolTy) |
| 2903 | if (ConstantBool *C = dyn_cast<ConstantBool>(TrueVal)) { |
| 2904 | if (C == ConstantBool::True) { |
| 2905 | // Change: A = select B, true, C --> A = or B, C |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2906 | return BinaryOperator::createOr(CondVal, FalseVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 2907 | } else { |
| 2908 | // Change: A = select B, false, C --> A = and !B, C |
| 2909 | Value *NotCond = |
| 2910 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
| 2911 | "not."+CondVal->getName()), SI); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2912 | return BinaryOperator::createAnd(NotCond, FalseVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 2913 | } |
| 2914 | } else if (ConstantBool *C = dyn_cast<ConstantBool>(FalseVal)) { |
| 2915 | if (C == ConstantBool::False) { |
| 2916 | // Change: A = select B, C, false --> A = and B, C |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2917 | return BinaryOperator::createAnd(CondVal, TrueVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 2918 | } else { |
| 2919 | // Change: A = select B, C, true --> A = or !B, C |
| 2920 | Value *NotCond = |
| 2921 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
| 2922 | "not."+CondVal->getName()), SI); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2923 | return BinaryOperator::createOr(NotCond, TrueVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 2924 | } |
| 2925 | } |
| 2926 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2927 | // Selecting between two integer constants? |
| 2928 | if (ConstantInt *TrueValC = dyn_cast<ConstantInt>(TrueVal)) |
| 2929 | if (ConstantInt *FalseValC = dyn_cast<ConstantInt>(FalseVal)) { |
| 2930 | // select C, 1, 0 -> cast C to int |
| 2931 | if (FalseValC->isNullValue() && TrueValC->getRawValue() == 1) { |
| 2932 | return new CastInst(CondVal, SI.getType()); |
| 2933 | } else if (TrueValC->isNullValue() && FalseValC->getRawValue() == 1) { |
| 2934 | // select C, 0, 1 -> cast !C to int |
| 2935 | Value *NotCond = |
| 2936 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, |
Chris Lattner | 82e14fe | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 2937 | "not."+CondVal->getName()), SI); |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2938 | return new CastInst(NotCond, SI.getType()); |
Chris Lattner | 82e14fe | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 2939 | } |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2940 | |
| 2941 | // If one of the constants is zero (we know they can't both be) and we |
| 2942 | // have a setcc instruction with zero, and we have an 'and' with the |
| 2943 | // non-constant value, eliminate this whole mess. This corresponds to |
| 2944 | // cases like this: ((X & 27) ? 27 : 0) |
| 2945 | if (TrueValC->isNullValue() || FalseValC->isNullValue()) |
| 2946 | if (Instruction *IC = dyn_cast<Instruction>(SI.getCondition())) |
| 2947 | if ((IC->getOpcode() == Instruction::SetEQ || |
| 2948 | IC->getOpcode() == Instruction::SetNE) && |
| 2949 | isa<ConstantInt>(IC->getOperand(1)) && |
| 2950 | cast<Constant>(IC->getOperand(1))->isNullValue()) |
| 2951 | if (Instruction *ICA = dyn_cast<Instruction>(IC->getOperand(0))) |
| 2952 | if (ICA->getOpcode() == Instruction::And && |
| 2953 | isa<ConstantInt>(ICA->getOperand(1)) && |
| 2954 | (ICA->getOperand(1) == TrueValC || |
| 2955 | ICA->getOperand(1) == FalseValC) && |
| 2956 | isOneBitSet(cast<ConstantInt>(ICA->getOperand(1)))) { |
| 2957 | // Okay, now we know that everything is set up, we just don't |
| 2958 | // know whether we have a setne or seteq and whether the true or |
| 2959 | // false val is the zero. |
| 2960 | bool ShouldNotVal = !TrueValC->isNullValue(); |
| 2961 | ShouldNotVal ^= IC->getOpcode() == Instruction::SetNE; |
| 2962 | Value *V = ICA; |
| 2963 | if (ShouldNotVal) |
| 2964 | V = InsertNewInstBefore(BinaryOperator::create( |
| 2965 | Instruction::Xor, V, ICA->getOperand(1)), SI); |
| 2966 | return ReplaceInstUsesWith(SI, V); |
| 2967 | } |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 2968 | } |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 2969 | |
| 2970 | // See if we are selecting two values based on a comparison of the two values. |
| 2971 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(CondVal)) { |
| 2972 | if (SCI->getOperand(0) == TrueVal && SCI->getOperand(1) == FalseVal) { |
| 2973 | // Transform (X == Y) ? X : Y -> Y |
| 2974 | if (SCI->getOpcode() == Instruction::SetEQ) |
| 2975 | return ReplaceInstUsesWith(SI, FalseVal); |
| 2976 | // Transform (X != Y) ? X : Y -> X |
| 2977 | if (SCI->getOpcode() == Instruction::SetNE) |
| 2978 | return ReplaceInstUsesWith(SI, TrueVal); |
| 2979 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 2980 | |
| 2981 | } else if (SCI->getOperand(0) == FalseVal && SCI->getOperand(1) == TrueVal){ |
| 2982 | // Transform (X == Y) ? Y : X -> X |
| 2983 | if (SCI->getOpcode() == Instruction::SetEQ) |
Chris Lattner | fbede52 | 2004-04-11 01:39:19 +0000 | [diff] [blame] | 2984 | return ReplaceInstUsesWith(SI, FalseVal); |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 2985 | // Transform (X != Y) ? Y : X -> Y |
| 2986 | if (SCI->getOpcode() == Instruction::SetNE) |
Chris Lattner | fbede52 | 2004-04-11 01:39:19 +0000 | [diff] [blame] | 2987 | return ReplaceInstUsesWith(SI, TrueVal); |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 2988 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
| 2989 | } |
| 2990 | } |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 2991 | |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 2992 | // See if we can fold the select into one of our operands. |
| 2993 | if (SI.getType()->isInteger()) { |
| 2994 | // See the comment above GetSelectFoldableOperands for a description of the |
| 2995 | // transformation we are doing here. |
| 2996 | if (Instruction *TVI = dyn_cast<Instruction>(TrueVal)) |
| 2997 | if (TVI->hasOneUse() && TVI->getNumOperands() == 2 && |
| 2998 | !isa<Constant>(FalseVal)) |
| 2999 | if (unsigned SFO = GetSelectFoldableOperands(TVI)) { |
| 3000 | unsigned OpToFold = 0; |
| 3001 | if ((SFO & 1) && FalseVal == TVI->getOperand(0)) { |
| 3002 | OpToFold = 1; |
| 3003 | } else if ((SFO & 2) && FalseVal == TVI->getOperand(1)) { |
| 3004 | OpToFold = 2; |
| 3005 | } |
| 3006 | |
| 3007 | if (OpToFold) { |
| 3008 | Constant *C = GetSelectFoldableConstant(TVI); |
| 3009 | std::string Name = TVI->getName(); TVI->setName(""); |
| 3010 | Instruction *NewSel = |
| 3011 | new SelectInst(SI.getCondition(), TVI->getOperand(2-OpToFold), C, |
| 3012 | Name); |
| 3013 | InsertNewInstBefore(NewSel, SI); |
| 3014 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TVI)) |
| 3015 | return BinaryOperator::create(BO->getOpcode(), FalseVal, NewSel); |
| 3016 | else if (ShiftInst *SI = dyn_cast<ShiftInst>(TVI)) |
| 3017 | return new ShiftInst(SI->getOpcode(), FalseVal, NewSel); |
| 3018 | else { |
| 3019 | assert(0 && "Unknown instruction!!"); |
| 3020 | } |
| 3021 | } |
| 3022 | } |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3023 | |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 3024 | if (Instruction *FVI = dyn_cast<Instruction>(FalseVal)) |
| 3025 | if (FVI->hasOneUse() && FVI->getNumOperands() == 2 && |
| 3026 | !isa<Constant>(TrueVal)) |
| 3027 | if (unsigned SFO = GetSelectFoldableOperands(FVI)) { |
| 3028 | unsigned OpToFold = 0; |
| 3029 | if ((SFO & 1) && TrueVal == FVI->getOperand(0)) { |
| 3030 | OpToFold = 1; |
| 3031 | } else if ((SFO & 2) && TrueVal == FVI->getOperand(1)) { |
| 3032 | OpToFold = 2; |
| 3033 | } |
| 3034 | |
| 3035 | if (OpToFold) { |
| 3036 | Constant *C = GetSelectFoldableConstant(FVI); |
| 3037 | std::string Name = FVI->getName(); FVI->setName(""); |
| 3038 | Instruction *NewSel = |
| 3039 | new SelectInst(SI.getCondition(), C, FVI->getOperand(2-OpToFold), |
| 3040 | Name); |
| 3041 | InsertNewInstBefore(NewSel, SI); |
| 3042 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(FVI)) |
| 3043 | return BinaryOperator::create(BO->getOpcode(), TrueVal, NewSel); |
| 3044 | else if (ShiftInst *SI = dyn_cast<ShiftInst>(FVI)) |
| 3045 | return new ShiftInst(SI->getOpcode(), TrueVal, NewSel); |
| 3046 | else { |
| 3047 | assert(0 && "Unknown instruction!!"); |
| 3048 | } |
| 3049 | } |
| 3050 | } |
| 3051 | } |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 3052 | return 0; |
| 3053 | } |
| 3054 | |
| 3055 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 3056 | // CallInst simplification |
| 3057 | // |
| 3058 | Instruction *InstCombiner::visitCallInst(CallInst &CI) { |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 3059 | // Intrinsics cannot occur in an invoke, so handle them here instead of in |
| 3060 | // visitCallSite. |
| 3061 | if (Function *F = CI.getCalledFunction()) |
| 3062 | switch (F->getIntrinsicID()) { |
| 3063 | case Intrinsic::memmove: |
| 3064 | case Intrinsic::memcpy: |
| 3065 | case Intrinsic::memset: |
| 3066 | // memmove/cpy/set of zero bytes is a noop. |
| 3067 | if (Constant *NumBytes = dyn_cast<Constant>(CI.getOperand(3))) { |
| 3068 | if (NumBytes->isNullValue()) |
| 3069 | return EraseInstFromFunction(CI); |
| 3070 | } |
| 3071 | break; |
| 3072 | default: |
| 3073 | break; |
| 3074 | } |
| 3075 | |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 3076 | return visitCallSite(&CI); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 3077 | } |
| 3078 | |
| 3079 | // InvokeInst simplification |
| 3080 | // |
| 3081 | Instruction *InstCombiner::visitInvokeInst(InvokeInst &II) { |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 3082 | return visitCallSite(&II); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 3083 | } |
| 3084 | |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 3085 | // visitCallSite - Improvements for call and invoke instructions. |
| 3086 | // |
| 3087 | Instruction *InstCombiner::visitCallSite(CallSite CS) { |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 3088 | bool Changed = false; |
| 3089 | |
| 3090 | // If the callee is a constexpr cast of a function, attempt to move the cast |
| 3091 | // to the arguments of the call/invoke. |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 3092 | if (transformConstExprCastCall(CS)) return 0; |
| 3093 | |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 3094 | Value *Callee = CS.getCalledValue(); |
| 3095 | const PointerType *PTy = cast<PointerType>(Callee->getType()); |
| 3096 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
| 3097 | if (FTy->isVarArg()) { |
| 3098 | // See if we can optimize any arguments passed through the varargs area of |
| 3099 | // the call. |
| 3100 | for (CallSite::arg_iterator I = CS.arg_begin()+FTy->getNumParams(), |
| 3101 | E = CS.arg_end(); I != E; ++I) |
| 3102 | if (CastInst *CI = dyn_cast<CastInst>(*I)) { |
| 3103 | // If this cast does not effect the value passed through the varargs |
| 3104 | // area, we can eliminate the use of the cast. |
| 3105 | Value *Op = CI->getOperand(0); |
| 3106 | if (CI->getType()->isLosslesslyConvertibleTo(Op->getType())) { |
| 3107 | *I = Op; |
| 3108 | Changed = true; |
| 3109 | } |
| 3110 | } |
| 3111 | } |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 3112 | |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 3113 | return Changed ? CS.getInstruction() : 0; |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 3114 | } |
| 3115 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 3116 | // transformConstExprCastCall - If the callee is a constexpr cast of a function, |
| 3117 | // attempt to move the cast to the arguments of the call/invoke. |
| 3118 | // |
| 3119 | bool InstCombiner::transformConstExprCastCall(CallSite CS) { |
| 3120 | if (!isa<ConstantExpr>(CS.getCalledValue())) return false; |
| 3121 | ConstantExpr *CE = cast<ConstantExpr>(CS.getCalledValue()); |
Chris Lattner | 9db07b9 | 2004-07-18 18:59:44 +0000 | [diff] [blame] | 3122 | if (CE->getOpcode() != Instruction::Cast || !isa<Function>(CE->getOperand(0))) |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 3123 | return false; |
Reid Spencer | 8863f18 | 2004-07-18 00:38:32 +0000 | [diff] [blame] | 3124 | Function *Callee = cast<Function>(CE->getOperand(0)); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 3125 | Instruction *Caller = CS.getInstruction(); |
| 3126 | |
| 3127 | // Okay, this is a cast from a function to a different type. Unless doing so |
| 3128 | // would cause a type conversion of one of our arguments, change this call to |
| 3129 | // be a direct call with arguments casted to the appropriate types. |
| 3130 | // |
| 3131 | const FunctionType *FT = Callee->getFunctionType(); |
| 3132 | const Type *OldRetTy = Caller->getType(); |
| 3133 | |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 3134 | // Check to see if we are changing the return type... |
| 3135 | if (OldRetTy != FT->getReturnType()) { |
| 3136 | if (Callee->isExternal() && |
| 3137 | !OldRetTy->isLosslesslyConvertibleTo(FT->getReturnType()) && |
| 3138 | !Caller->use_empty()) |
| 3139 | return false; // Cannot transform this return value... |
| 3140 | |
| 3141 | // If the callsite is an invoke instruction, and the return value is used by |
| 3142 | // a PHI node in a successor, we cannot change the return type of the call |
| 3143 | // because there is no place to put the cast instruction (without breaking |
| 3144 | // the critical edge). Bail out in this case. |
| 3145 | if (!Caller->use_empty()) |
| 3146 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) |
| 3147 | for (Value::use_iterator UI = II->use_begin(), E = II->use_end(); |
| 3148 | UI != E; ++UI) |
| 3149 | if (PHINode *PN = dyn_cast<PHINode>(*UI)) |
| 3150 | if (PN->getParent() == II->getNormalDest() || |
Chris Lattner | aeb2a1d | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 3151 | PN->getParent() == II->getUnwindDest()) |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 3152 | return false; |
| 3153 | } |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 3154 | |
| 3155 | unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin()); |
| 3156 | unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs); |
| 3157 | |
| 3158 | CallSite::arg_iterator AI = CS.arg_begin(); |
| 3159 | for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) { |
| 3160 | const Type *ParamTy = FT->getParamType(i); |
| 3161 | bool isConvertible = (*AI)->getType()->isLosslesslyConvertibleTo(ParamTy); |
| 3162 | if (Callee->isExternal() && !isConvertible) return false; |
| 3163 | } |
| 3164 | |
| 3165 | if (FT->getNumParams() < NumActualArgs && !FT->isVarArg() && |
| 3166 | Callee->isExternal()) |
| 3167 | return false; // Do not delete arguments unless we have a function body... |
| 3168 | |
| 3169 | // Okay, we decided that this is a safe thing to do: go ahead and start |
| 3170 | // inserting cast instructions as necessary... |
| 3171 | std::vector<Value*> Args; |
| 3172 | Args.reserve(NumActualArgs); |
| 3173 | |
| 3174 | AI = CS.arg_begin(); |
| 3175 | for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) { |
| 3176 | const Type *ParamTy = FT->getParamType(i); |
| 3177 | if ((*AI)->getType() == ParamTy) { |
| 3178 | Args.push_back(*AI); |
| 3179 | } else { |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 3180 | Args.push_back(InsertNewInstBefore(new CastInst(*AI, ParamTy, "tmp"), |
| 3181 | *Caller)); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 3182 | } |
| 3183 | } |
| 3184 | |
| 3185 | // If the function takes more arguments than the call was taking, add them |
| 3186 | // now... |
| 3187 | for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i) |
| 3188 | Args.push_back(Constant::getNullValue(FT->getParamType(i))); |
| 3189 | |
| 3190 | // If we are removing arguments to the function, emit an obnoxious warning... |
| 3191 | if (FT->getNumParams() < NumActualArgs) |
| 3192 | if (!FT->isVarArg()) { |
| 3193 | std::cerr << "WARNING: While resolving call to function '" |
| 3194 | << Callee->getName() << "' arguments were dropped!\n"; |
| 3195 | } else { |
| 3196 | // Add all of the arguments in their promoted form to the arg list... |
| 3197 | for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) { |
| 3198 | const Type *PTy = getPromotedType((*AI)->getType()); |
| 3199 | if (PTy != (*AI)->getType()) { |
| 3200 | // Must promote to pass through va_arg area! |
| 3201 | Instruction *Cast = new CastInst(*AI, PTy, "tmp"); |
| 3202 | InsertNewInstBefore(Cast, *Caller); |
| 3203 | Args.push_back(Cast); |
| 3204 | } else { |
| 3205 | Args.push_back(*AI); |
| 3206 | } |
| 3207 | } |
| 3208 | } |
| 3209 | |
| 3210 | if (FT->getReturnType() == Type::VoidTy) |
| 3211 | Caller->setName(""); // Void type should not have a name... |
| 3212 | |
| 3213 | Instruction *NC; |
| 3214 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
Chris Lattner | aeb2a1d | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 3215 | NC = new InvokeInst(Callee, II->getNormalDest(), II->getUnwindDest(), |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 3216 | Args, Caller->getName(), Caller); |
| 3217 | } else { |
| 3218 | NC = new CallInst(Callee, Args, Caller->getName(), Caller); |
| 3219 | } |
| 3220 | |
| 3221 | // Insert a cast of the return type as necessary... |
| 3222 | Value *NV = NC; |
| 3223 | if (Caller->getType() != NV->getType() && !Caller->use_empty()) { |
| 3224 | if (NV->getType() != Type::VoidTy) { |
| 3225 | NV = NC = new CastInst(NC, Caller->getType(), "tmp"); |
Chris Lattner | bb60904 | 2003-10-30 00:46:41 +0000 | [diff] [blame] | 3226 | |
| 3227 | // If this is an invoke instruction, we should insert it after the first |
| 3228 | // non-phi, instruction in the normal successor block. |
| 3229 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
| 3230 | BasicBlock::iterator I = II->getNormalDest()->begin(); |
| 3231 | while (isa<PHINode>(I)) ++I; |
| 3232 | InsertNewInstBefore(NC, *I); |
| 3233 | } else { |
| 3234 | // Otherwise, it's a call, just insert cast right after the call instr |
| 3235 | InsertNewInstBefore(NC, *Caller); |
| 3236 | } |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 3237 | AddUsersToWorkList(*Caller); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 3238 | } else { |
| 3239 | NV = Constant::getNullValue(Caller->getType()); |
| 3240 | } |
| 3241 | } |
| 3242 | |
| 3243 | if (Caller->getType() != Type::VoidTy && !Caller->use_empty()) |
| 3244 | Caller->replaceAllUsesWith(NV); |
| 3245 | Caller->getParent()->getInstList().erase(Caller); |
| 3246 | removeFromWorkList(Caller); |
| 3247 | return true; |
| 3248 | } |
| 3249 | |
| 3250 | |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 3251 | |
Chris Lattner | 473945d | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 3252 | // PHINode simplification |
| 3253 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3254 | Instruction *InstCombiner::visitPHINode(PHINode &PN) { |
Chris Lattner | 60921c9 | 2003-12-19 05:58:40 +0000 | [diff] [blame] | 3255 | if (Value *V = hasConstantValue(&PN)) |
| 3256 | return ReplaceInstUsesWith(PN, V); |
Chris Lattner | 7059f2e | 2004-02-16 05:07:08 +0000 | [diff] [blame] | 3257 | |
| 3258 | // If the only user of this instruction is a cast instruction, and all of the |
| 3259 | // incoming values are constants, change this PHI to merge together the casted |
| 3260 | // constants. |
| 3261 | if (PN.hasOneUse()) |
| 3262 | if (CastInst *CI = dyn_cast<CastInst>(PN.use_back())) |
| 3263 | if (CI->getType() != PN.getType()) { // noop casts will be folded |
| 3264 | bool AllConstant = true; |
| 3265 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) |
| 3266 | if (!isa<Constant>(PN.getIncomingValue(i))) { |
| 3267 | AllConstant = false; |
| 3268 | break; |
| 3269 | } |
| 3270 | if (AllConstant) { |
| 3271 | // Make a new PHI with all casted values. |
| 3272 | PHINode *New = new PHINode(CI->getType(), PN.getName(), &PN); |
| 3273 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 3274 | Constant *OldArg = cast<Constant>(PN.getIncomingValue(i)); |
| 3275 | New->addIncoming(ConstantExpr::getCast(OldArg, New->getType()), |
| 3276 | PN.getIncomingBlock(i)); |
| 3277 | } |
| 3278 | |
| 3279 | // Update the cast instruction. |
| 3280 | CI->setOperand(0, New); |
| 3281 | WorkList.push_back(CI); // revisit the cast instruction to fold. |
| 3282 | WorkList.push_back(New); // Make sure to revisit the new Phi |
| 3283 | return &PN; // PN is now dead! |
| 3284 | } |
| 3285 | } |
Chris Lattner | 60921c9 | 2003-12-19 05:58:40 +0000 | [diff] [blame] | 3286 | return 0; |
Chris Lattner | 473945d | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 3287 | } |
| 3288 | |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 3289 | static Value *InsertSignExtendToPtrTy(Value *V, const Type *DTy, |
| 3290 | Instruction *InsertPoint, |
| 3291 | InstCombiner *IC) { |
| 3292 | unsigned PS = IC->getTargetData().getPointerSize(); |
| 3293 | const Type *VTy = V->getType(); |
| 3294 | Instruction *Cast; |
| 3295 | if (!VTy->isSigned() && VTy->getPrimitiveSize() < PS) |
| 3296 | // We must insert a cast to ensure we sign-extend. |
| 3297 | V = IC->InsertNewInstBefore(new CastInst(V, VTy->getSignedVersion(), |
| 3298 | V->getName()), *InsertPoint); |
| 3299 | return IC->InsertNewInstBefore(new CastInst(V, DTy, V->getName()), |
| 3300 | *InsertPoint); |
| 3301 | } |
| 3302 | |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 3303 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3304 | Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 3305 | Value *PtrOp = GEP.getOperand(0); |
Chris Lattner | c54e2b8 | 2003-05-22 19:07:21 +0000 | [diff] [blame] | 3306 | // Is it 'getelementptr %P, long 0' or 'getelementptr %P' |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3307 | // If so, eliminate the noop. |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 3308 | if (GEP.getNumOperands() == 1) |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 3309 | return ReplaceInstUsesWith(GEP, PtrOp); |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 3310 | |
| 3311 | bool HasZeroPointerIndex = false; |
| 3312 | if (Constant *C = dyn_cast<Constant>(GEP.getOperand(1))) |
| 3313 | HasZeroPointerIndex = C->isNullValue(); |
| 3314 | |
| 3315 | if (GEP.getNumOperands() == 2 && HasZeroPointerIndex) |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 3316 | return ReplaceInstUsesWith(GEP, PtrOp); |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 3317 | |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 3318 | // Eliminate unneeded casts for indices. |
| 3319 | bool MadeChange = false; |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 3320 | gep_type_iterator GTI = gep_type_begin(GEP); |
| 3321 | for (unsigned i = 1, e = GEP.getNumOperands(); i != e; ++i, ++GTI) |
| 3322 | if (isa<SequentialType>(*GTI)) { |
| 3323 | if (CastInst *CI = dyn_cast<CastInst>(GEP.getOperand(i))) { |
| 3324 | Value *Src = CI->getOperand(0); |
| 3325 | const Type *SrcTy = Src->getType(); |
| 3326 | const Type *DestTy = CI->getType(); |
| 3327 | if (Src->getType()->isInteger()) { |
| 3328 | if (SrcTy->getPrimitiveSize() == DestTy->getPrimitiveSize()) { |
| 3329 | // We can always eliminate a cast from ulong or long to the other. |
| 3330 | // We can always eliminate a cast from uint to int or the other on |
| 3331 | // 32-bit pointer platforms. |
| 3332 | if (DestTy->getPrimitiveSize() >= TD->getPointerSize()) { |
| 3333 | MadeChange = true; |
| 3334 | GEP.setOperand(i, Src); |
| 3335 | } |
| 3336 | } else if (SrcTy->getPrimitiveSize() < DestTy->getPrimitiveSize() && |
| 3337 | SrcTy->getPrimitiveSize() == 4) { |
| 3338 | // We can always eliminate a cast from int to [u]long. We can |
| 3339 | // eliminate a cast from uint to [u]long iff the target is a 32-bit |
| 3340 | // pointer target. |
| 3341 | if (SrcTy->isSigned() || |
| 3342 | SrcTy->getPrimitiveSize() >= TD->getPointerSize()) { |
| 3343 | MadeChange = true; |
| 3344 | GEP.setOperand(i, Src); |
| 3345 | } |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 3346 | } |
| 3347 | } |
| 3348 | } |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 3349 | // If we are using a wider index than needed for this platform, shrink it |
| 3350 | // to what we need. If the incoming value needs a cast instruction, |
| 3351 | // insert it. This explicit cast can make subsequent optimizations more |
| 3352 | // obvious. |
| 3353 | Value *Op = GEP.getOperand(i); |
| 3354 | if (Op->getType()->getPrimitiveSize() > TD->getPointerSize()) |
Chris Lattner | 4f1134e | 2004-04-17 18:16:10 +0000 | [diff] [blame] | 3355 | if (Constant *C = dyn_cast<Constant>(Op)) { |
Chris Lattner | 67769e5 | 2004-07-20 01:48:15 +0000 | [diff] [blame] | 3356 | GEP.setOperand(i, ConstantExpr::getCast(C, |
| 3357 | TD->getIntPtrType()->getSignedVersion())); |
Chris Lattner | 4f1134e | 2004-04-17 18:16:10 +0000 | [diff] [blame] | 3358 | MadeChange = true; |
| 3359 | } else { |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 3360 | Op = InsertNewInstBefore(new CastInst(Op, TD->getIntPtrType(), |
| 3361 | Op->getName()), GEP); |
| 3362 | GEP.setOperand(i, Op); |
| 3363 | MadeChange = true; |
| 3364 | } |
Chris Lattner | 67769e5 | 2004-07-20 01:48:15 +0000 | [diff] [blame] | 3365 | |
| 3366 | // If this is a constant idx, make sure to canonicalize it to be a signed |
| 3367 | // operand, otherwise CSE and other optimizations are pessimized. |
| 3368 | if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(Op)) { |
| 3369 | GEP.setOperand(i, ConstantExpr::getCast(CUI, |
| 3370 | CUI->getType()->getSignedVersion())); |
| 3371 | MadeChange = true; |
| 3372 | } |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 3373 | } |
| 3374 | if (MadeChange) return &GEP; |
| 3375 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 3376 | // Combine Indices - If the source pointer to this getelementptr instruction |
| 3377 | // is a getelementptr instruction, combine the indices of the two |
| 3378 | // getelementptr instructions into a single instruction. |
| 3379 | // |
Chris Lattner | ebd985c | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 3380 | std::vector<Value*> SrcGEPOperands; |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 3381 | if (GetElementPtrInst *Src = dyn_cast<GetElementPtrInst>(PtrOp)) { |
Chris Lattner | ebd985c | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 3382 | SrcGEPOperands.assign(Src->op_begin(), Src->op_end()); |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 3383 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(PtrOp)) { |
Chris Lattner | ebd985c | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 3384 | if (CE->getOpcode() == Instruction::GetElementPtr) |
| 3385 | SrcGEPOperands.assign(CE->op_begin(), CE->op_end()); |
| 3386 | } |
| 3387 | |
| 3388 | if (!SrcGEPOperands.empty()) { |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 3389 | // Note that if our source is a gep chain itself that we wait for that |
| 3390 | // chain to be resolved before we perform this transformation. This |
| 3391 | // avoids us creating a TON of code in some cases. |
| 3392 | // |
| 3393 | if (isa<GetElementPtrInst>(SrcGEPOperands[0]) && |
| 3394 | cast<Instruction>(SrcGEPOperands[0])->getNumOperands() == 2) |
| 3395 | return 0; // Wait until our source is folded to completion. |
| 3396 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 3397 | std::vector<Value *> Indices; |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 3398 | |
| 3399 | // Find out whether the last index in the source GEP is a sequential idx. |
| 3400 | bool EndsWithSequential = false; |
| 3401 | for (gep_type_iterator I = gep_type_begin(*cast<User>(PtrOp)), |
| 3402 | E = gep_type_end(*cast<User>(PtrOp)); I != E; ++I) |
Chris Lattner | be97b4e | 2004-05-08 22:41:42 +0000 | [diff] [blame] | 3403 | EndsWithSequential = !isa<StructType>(*I); |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 3404 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 3405 | // Can we combine the two pointer arithmetics offsets? |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 3406 | if (EndsWithSequential) { |
Chris Lattner | decd081 | 2003-03-05 22:33:14 +0000 | [diff] [blame] | 3407 | // Replace: gep (gep %P, long B), long A, ... |
| 3408 | // With: T = long A+B; gep %P, T, ... |
| 3409 | // |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 3410 | Value *Sum, *SO1 = SrcGEPOperands.back(), *GO1 = GEP.getOperand(1); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 3411 | if (SO1 == Constant::getNullValue(SO1->getType())) { |
| 3412 | Sum = GO1; |
| 3413 | } else if (GO1 == Constant::getNullValue(GO1->getType())) { |
| 3414 | Sum = SO1; |
| 3415 | } else { |
| 3416 | // If they aren't the same type, convert both to an integer of the |
| 3417 | // target's pointer size. |
| 3418 | if (SO1->getType() != GO1->getType()) { |
| 3419 | if (Constant *SO1C = dyn_cast<Constant>(SO1)) { |
| 3420 | SO1 = ConstantExpr::getCast(SO1C, GO1->getType()); |
| 3421 | } else if (Constant *GO1C = dyn_cast<Constant>(GO1)) { |
| 3422 | GO1 = ConstantExpr::getCast(GO1C, SO1->getType()); |
| 3423 | } else { |
| 3424 | unsigned PS = TD->getPointerSize(); |
| 3425 | Instruction *Cast; |
| 3426 | if (SO1->getType()->getPrimitiveSize() == PS) { |
| 3427 | // Convert GO1 to SO1's type. |
| 3428 | GO1 = InsertSignExtendToPtrTy(GO1, SO1->getType(), &GEP, this); |
| 3429 | |
| 3430 | } else if (GO1->getType()->getPrimitiveSize() == PS) { |
| 3431 | // Convert SO1 to GO1's type. |
| 3432 | SO1 = InsertSignExtendToPtrTy(SO1, GO1->getType(), &GEP, this); |
| 3433 | } else { |
| 3434 | const Type *PT = TD->getIntPtrType(); |
| 3435 | SO1 = InsertSignExtendToPtrTy(SO1, PT, &GEP, this); |
| 3436 | GO1 = InsertSignExtendToPtrTy(GO1, PT, &GEP, this); |
| 3437 | } |
| 3438 | } |
| 3439 | } |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 3440 | if (isa<Constant>(SO1) && isa<Constant>(GO1)) |
| 3441 | Sum = ConstantExpr::getAdd(cast<Constant>(SO1), cast<Constant>(GO1)); |
| 3442 | else { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3443 | Sum = BinaryOperator::createAdd(SO1, GO1, PtrOp->getName()+".sum"); |
| 3444 | InsertNewInstBefore(cast<Instruction>(Sum), GEP); |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 3445 | } |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 3446 | } |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 3447 | |
| 3448 | // Recycle the GEP we already have if possible. |
| 3449 | if (SrcGEPOperands.size() == 2) { |
| 3450 | GEP.setOperand(0, SrcGEPOperands[0]); |
| 3451 | GEP.setOperand(1, Sum); |
| 3452 | return &GEP; |
| 3453 | } else { |
| 3454 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, |
| 3455 | SrcGEPOperands.end()-1); |
| 3456 | Indices.push_back(Sum); |
| 3457 | Indices.insert(Indices.end(), GEP.op_begin()+2, GEP.op_end()); |
| 3458 | } |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 3459 | } else if (isa<Constant>(*GEP.idx_begin()) && |
| 3460 | cast<Constant>(*GEP.idx_begin())->isNullValue() && |
Chris Lattner | ebd985c | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 3461 | SrcGEPOperands.size() != 1) { |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 3462 | // 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] | 3463 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, |
| 3464 | SrcGEPOperands.end()); |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 3465 | Indices.insert(Indices.end(), GEP.idx_begin()+1, GEP.idx_end()); |
| 3466 | } |
| 3467 | |
| 3468 | if (!Indices.empty()) |
Chris Lattner | ebd985c | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 3469 | return new GetElementPtrInst(SrcGEPOperands[0], Indices, GEP.getName()); |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 3470 | |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 3471 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(PtrOp)) { |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 3472 | // GEP of global variable. If all of the indices for this GEP are |
| 3473 | // constants, we can promote this to a constexpr instead of an instruction. |
| 3474 | |
| 3475 | // Scan for nonconstants... |
| 3476 | std::vector<Constant*> Indices; |
| 3477 | User::op_iterator I = GEP.idx_begin(), E = GEP.idx_end(); |
| 3478 | for (; I != E && isa<Constant>(*I); ++I) |
| 3479 | Indices.push_back(cast<Constant>(*I)); |
| 3480 | |
| 3481 | if (I == E) { // If they are all constants... |
Chris Lattner | 9db07b9 | 2004-07-18 18:59:44 +0000 | [diff] [blame] | 3482 | Constant *CE = ConstantExpr::getGetElementPtr(GV, Indices); |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 3483 | |
| 3484 | // Replace all uses of the GEP with the new constexpr... |
| 3485 | return ReplaceInstUsesWith(GEP, CE); |
| 3486 | } |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 3487 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(PtrOp)) { |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 3488 | if (CE->getOpcode() == Instruction::Cast) { |
| 3489 | if (HasZeroPointerIndex) { |
| 3490 | // transform: GEP (cast [10 x ubyte]* X to [0 x ubyte]*), long 0, ... |
| 3491 | // into : GEP [10 x ubyte]* X, long 0, ... |
| 3492 | // |
| 3493 | // This occurs when the program declares an array extern like "int X[];" |
| 3494 | // |
| 3495 | Constant *X = CE->getOperand(0); |
| 3496 | const PointerType *CPTy = cast<PointerType>(CE->getType()); |
| 3497 | if (const PointerType *XTy = dyn_cast<PointerType>(X->getType())) |
| 3498 | if (const ArrayType *XATy = |
| 3499 | dyn_cast<ArrayType>(XTy->getElementType())) |
| 3500 | if (const ArrayType *CATy = |
| 3501 | dyn_cast<ArrayType>(CPTy->getElementType())) |
| 3502 | if (CATy->getElementType() == XATy->getElementType()) { |
| 3503 | // At this point, we know that the cast source type is a pointer |
| 3504 | // to an array of the same type as the destination pointer |
| 3505 | // array. Because the array type is never stepped over (there |
| 3506 | // is a leading zero) we can fold the cast into this GEP. |
| 3507 | GEP.setOperand(0, X); |
| 3508 | return &GEP; |
| 3509 | } |
| 3510 | } |
| 3511 | } |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 3512 | } |
| 3513 | |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 3514 | return 0; |
| 3515 | } |
| 3516 | |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 3517 | Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) { |
| 3518 | // Convert: malloc Ty, C - where C is a constant != 1 into: malloc [C x Ty], 1 |
| 3519 | if (AI.isArrayAllocation()) // Check C != 1 |
| 3520 | if (const ConstantUInt *C = dyn_cast<ConstantUInt>(AI.getArraySize())) { |
| 3521 | const Type *NewTy = ArrayType::get(AI.getAllocatedType(), C->getValue()); |
Chris Lattner | 0006bd7 | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 3522 | AllocationInst *New = 0; |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 3523 | |
| 3524 | // Create and insert the replacement instruction... |
| 3525 | if (isa<MallocInst>(AI)) |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 3526 | New = new MallocInst(NewTy, 0, AI.getName()); |
Chris Lattner | 0006bd7 | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 3527 | else { |
| 3528 | assert(isa<AllocaInst>(AI) && "Unknown type of allocation inst!"); |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 3529 | New = new AllocaInst(NewTy, 0, AI.getName()); |
Chris Lattner | 0006bd7 | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 3530 | } |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 3531 | |
| 3532 | InsertNewInstBefore(New, AI); |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 3533 | |
| 3534 | // Scan to the end of the allocation instructions, to skip over a block of |
| 3535 | // allocas if possible... |
| 3536 | // |
| 3537 | BasicBlock::iterator It = New; |
| 3538 | while (isa<AllocationInst>(*It)) ++It; |
| 3539 | |
| 3540 | // Now that I is pointing to the first non-allocation-inst in the block, |
| 3541 | // insert our getelementptr instruction... |
| 3542 | // |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 3543 | std::vector<Value*> Idx(2, Constant::getNullValue(Type::IntTy)); |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 3544 | Value *V = new GetElementPtrInst(New, Idx, New->getName()+".sub", It); |
| 3545 | |
| 3546 | // Now make everything use the getelementptr instead of the original |
| 3547 | // allocation. |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 3548 | return ReplaceInstUsesWith(AI, V); |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 3549 | } |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 3550 | |
| 3551 | // If alloca'ing a zero byte object, replace the alloca with a null pointer. |
| 3552 | // Note that we only do this for alloca's, because malloc should allocate and |
| 3553 | // return a unique pointer, even for a zero byte allocation. |
Chris Lattner | cf27afb | 2004-07-02 22:55:47 +0000 | [diff] [blame] | 3554 | if (isa<AllocaInst>(AI) && AI.getAllocatedType()->isSized() && |
| 3555 | TD->getTypeSize(AI.getAllocatedType()) == 0) |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 3556 | return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); |
| 3557 | |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 3558 | return 0; |
| 3559 | } |
| 3560 | |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 3561 | Instruction *InstCombiner::visitFreeInst(FreeInst &FI) { |
| 3562 | Value *Op = FI.getOperand(0); |
| 3563 | |
| 3564 | // Change free <ty>* (cast <ty2>* X to <ty>*) into free <ty2>* X |
| 3565 | if (CastInst *CI = dyn_cast<CastInst>(Op)) |
| 3566 | if (isa<PointerType>(CI->getOperand(0)->getType())) { |
| 3567 | FI.setOperand(0, CI->getOperand(0)); |
| 3568 | return &FI; |
| 3569 | } |
| 3570 | |
Chris Lattner | 6160e85 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 3571 | // If we have 'free null' delete the instruction. This can happen in stl code |
| 3572 | // when lots of inlining happens. |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 3573 | if (isa<ConstantPointerNull>(Op)) |
| 3574 | return EraseInstFromFunction(FI); |
Chris Lattner | 6160e85 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 3575 | |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 3576 | return 0; |
| 3577 | } |
| 3578 | |
| 3579 | |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 3580 | /// GetGEPGlobalInitializer - Given a constant, and a getelementptr |
| 3581 | /// constantexpr, return the constant value being addressed by the constant |
| 3582 | /// expression, or null if something is funny. |
| 3583 | /// |
| 3584 | static Constant *GetGEPGlobalInitializer(Constant *C, ConstantExpr *CE) { |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 3585 | if (CE->getOperand(1) != Constant::getNullValue(CE->getOperand(1)->getType())) |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 3586 | return 0; // Do not allow stepping over the value! |
| 3587 | |
| 3588 | // Loop over all of the operands, tracking down which value we are |
| 3589 | // addressing... |
Chris Lattner | e1368ae | 2004-05-27 17:30:27 +0000 | [diff] [blame] | 3590 | gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE); |
| 3591 | for (++I; I != E; ++I) |
| 3592 | if (const StructType *STy = dyn_cast<StructType>(*I)) { |
| 3593 | ConstantUInt *CU = cast<ConstantUInt>(I.getOperand()); |
| 3594 | assert(CU->getValue() < STy->getNumElements() && |
| 3595 | "Struct index out of range!"); |
| 3596 | if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) { |
Alkis Evlogimenos | 15876bb | 2004-08-04 08:44:43 +0000 | [diff] [blame] | 3597 | C = CS->getOperand(CU->getValue()); |
Chris Lattner | e1368ae | 2004-05-27 17:30:27 +0000 | [diff] [blame] | 3598 | } else if (isa<ConstantAggregateZero>(C)) { |
| 3599 | C = Constant::getNullValue(STy->getElementType(CU->getValue())); |
| 3600 | } else { |
| 3601 | return 0; |
| 3602 | } |
| 3603 | } else if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand())) { |
| 3604 | const ArrayType *ATy = cast<ArrayType>(*I); |
| 3605 | if ((uint64_t)CI->getRawValue() >= ATy->getNumElements()) return 0; |
| 3606 | if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) |
Alkis Evlogimenos | 15876bb | 2004-08-04 08:44:43 +0000 | [diff] [blame] | 3607 | C = CA->getOperand(CI->getRawValue()); |
Chris Lattner | e1368ae | 2004-05-27 17:30:27 +0000 | [diff] [blame] | 3608 | else if (isa<ConstantAggregateZero>(C)) |
| 3609 | C = Constant::getNullValue(ATy->getElementType()); |
| 3610 | else |
| 3611 | return 0; |
| 3612 | } else { |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 3613 | return 0; |
Chris Lattner | e1368ae | 2004-05-27 17:30:27 +0000 | [diff] [blame] | 3614 | } |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 3615 | return C; |
| 3616 | } |
| 3617 | |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 3618 | static Instruction *InstCombineLoadCast(InstCombiner &IC, LoadInst &LI) { |
| 3619 | User *CI = cast<User>(LI.getOperand(0)); |
| 3620 | |
| 3621 | const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType(); |
| 3622 | if (const PointerType *SrcTy = |
| 3623 | dyn_cast<PointerType>(CI->getOperand(0)->getType())) { |
| 3624 | const Type *SrcPTy = SrcTy->getElementType(); |
| 3625 | if (SrcPTy->isSized() && DestPTy->isSized() && |
| 3626 | IC.getTargetData().getTypeSize(SrcPTy) == |
| 3627 | IC.getTargetData().getTypeSize(DestPTy) && |
| 3628 | (SrcPTy->isInteger() || isa<PointerType>(SrcPTy)) && |
| 3629 | (DestPTy->isInteger() || isa<PointerType>(DestPTy))) { |
| 3630 | // Okay, we are casting from one integer or pointer type to another of |
| 3631 | // the same size. Instead of casting the pointer before the load, cast |
| 3632 | // the result of the loaded value. |
| 3633 | Value *NewLoad = IC.InsertNewInstBefore(new LoadInst(CI->getOperand(0), |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 3634 | CI->getName(), |
| 3635 | LI.isVolatile()),LI); |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 3636 | // Now cast the result of the load. |
| 3637 | return new CastInst(NewLoad, LI.getType()); |
| 3638 | } |
| 3639 | } |
| 3640 | return 0; |
| 3641 | } |
| 3642 | |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 3643 | /// isSafeToLoadUnconditionally - Return true if we know that executing a load |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 3644 | /// from this value cannot trap. If it is not obviously safe to load from the |
| 3645 | /// specified pointer, we do a quick local scan of the basic block containing |
| 3646 | /// ScanFrom, to determine if the address is already accessed. |
| 3647 | static bool isSafeToLoadUnconditionally(Value *V, Instruction *ScanFrom) { |
| 3648 | // If it is an alloca or global variable, it is always safe to load from. |
| 3649 | if (isa<AllocaInst>(V) || isa<GlobalVariable>(V)) return true; |
| 3650 | |
| 3651 | // Otherwise, be a little bit agressive by scanning the local block where we |
| 3652 | // 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] | 3653 | // from/to. If so, the previous load or store would have already trapped, |
| 3654 | // so there is no harm doing an extra load (also, CSE will later eliminate |
| 3655 | // the load entirely). |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 3656 | BasicBlock::iterator BBI = ScanFrom, E = ScanFrom->getParent()->begin(); |
| 3657 | |
Alkis Evlogimenos | 7b6ec60 | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 3658 | while (BBI != E) { |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 3659 | --BBI; |
| 3660 | |
| 3661 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { |
| 3662 | if (LI->getOperand(0) == V) return true; |
| 3663 | } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) |
| 3664 | if (SI->getOperand(1) == V) return true; |
| 3665 | |
Alkis Evlogimenos | 7b6ec60 | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 3666 | } |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 3667 | return false; |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 3668 | } |
| 3669 | |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 3670 | Instruction *InstCombiner::visitLoadInst(LoadInst &LI) { |
| 3671 | Value *Op = LI.getOperand(0); |
Chris Lattner | 5f16a13 | 2004-01-12 04:13:56 +0000 | [diff] [blame] | 3672 | |
Chris Lattner | d9955aa | 2004-04-14 03:28:36 +0000 | [diff] [blame] | 3673 | if (Constant *C = dyn_cast<Constant>(Op)) |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 3674 | if (C->isNullValue() && !LI.isVolatile()) // load null -> 0 |
Chris Lattner | d9955aa | 2004-04-14 03:28:36 +0000 | [diff] [blame] | 3675 | return ReplaceInstUsesWith(LI, Constant::getNullValue(LI.getType())); |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 3676 | |
| 3677 | // Instcombine load (constant global) into the value loaded... |
| 3678 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op)) |
Chris Lattner | 1ba5bcd | 2003-07-22 21:46:59 +0000 | [diff] [blame] | 3679 | if (GV->isConstant() && !GV->isExternal()) |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 3680 | return ReplaceInstUsesWith(LI, GV->getInitializer()); |
| 3681 | |
| 3682 | // Instcombine load (constantexpr_GEP global, 0, ...) into the value loaded... |
| 3683 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op)) |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 3684 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
Reid Spencer | 8863f18 | 2004-07-18 00:38:32 +0000 | [diff] [blame] | 3685 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0))) |
| 3686 | if (GV->isConstant() && !GV->isExternal()) |
| 3687 | if (Constant *V = GetGEPGlobalInitializer(GV->getInitializer(), CE)) |
| 3688 | return ReplaceInstUsesWith(LI, V); |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 3689 | } else if (CE->getOpcode() == Instruction::Cast) { |
| 3690 | if (Instruction *Res = InstCombineLoadCast(*this, LI)) |
| 3691 | return Res; |
| 3692 | } |
Chris Lattner | f499eac | 2004-04-08 20:39:49 +0000 | [diff] [blame] | 3693 | |
| 3694 | // load (cast X) --> cast (load X) iff safe |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 3695 | if (CastInst *CI = dyn_cast<CastInst>(Op)) |
| 3696 | if (Instruction *Res = InstCombineLoadCast(*this, LI)) |
| 3697 | return Res; |
Chris Lattner | f499eac | 2004-04-08 20:39:49 +0000 | [diff] [blame] | 3698 | |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 3699 | if (!LI.isVolatile() && Op->hasOneUse()) { |
| 3700 | // Change select and PHI nodes to select values instead of addresses: this |
| 3701 | // helps alias analysis out a lot, allows many others simplifications, and |
| 3702 | // exposes redundancy in the code. |
| 3703 | // |
| 3704 | // Note that we cannot do the transformation unless we know that the |
| 3705 | // introduced loads cannot trap! Something like this is valid as long as |
| 3706 | // the condition is always false: load (select bool %C, int* null, int* %G), |
| 3707 | // but it would not be valid if we transformed it to load from null |
| 3708 | // unconditionally. |
| 3709 | // |
| 3710 | if (SelectInst *SI = dyn_cast<SelectInst>(Op)) { |
| 3711 | // load (select (Cond, &V1, &V2)) --> select(Cond, load &V1, load &V2). |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 3712 | if (isSafeToLoadUnconditionally(SI->getOperand(1), SI) && |
| 3713 | isSafeToLoadUnconditionally(SI->getOperand(2), SI)) { |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 3714 | Value *V1 = InsertNewInstBefore(new LoadInst(SI->getOperand(1), |
Chris Lattner | 79f0c8e | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 3715 | SI->getOperand(1)->getName()+".val"), LI); |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 3716 | Value *V2 = InsertNewInstBefore(new LoadInst(SI->getOperand(2), |
Chris Lattner | 79f0c8e | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 3717 | SI->getOperand(2)->getName()+".val"), LI); |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 3718 | return new SelectInst(SI->getCondition(), V1, V2); |
| 3719 | } |
| 3720 | |
Chris Lattner | 684fe21 | 2004-09-23 15:46:00 +0000 | [diff] [blame] | 3721 | // load (select (cond, null, P)) -> load P |
| 3722 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(1))) |
| 3723 | if (C->isNullValue()) { |
| 3724 | LI.setOperand(0, SI->getOperand(2)); |
| 3725 | return &LI; |
| 3726 | } |
| 3727 | |
| 3728 | // load (select (cond, P, null)) -> load P |
| 3729 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(2))) |
| 3730 | if (C->isNullValue()) { |
| 3731 | LI.setOperand(0, SI->getOperand(1)); |
| 3732 | return &LI; |
| 3733 | } |
| 3734 | |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 3735 | } else if (PHINode *PN = dyn_cast<PHINode>(Op)) { |
| 3736 | // load (phi (&V1, &V2, &V3)) --> phi(load &V1, load &V2, load &V3) |
Chris Lattner | 79f0c8e | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 3737 | bool Safe = PN->getParent() == LI.getParent(); |
| 3738 | |
| 3739 | // Scan all of the instructions between the PHI and the load to make |
| 3740 | // sure there are no instructions that might possibly alter the value |
| 3741 | // loaded from the PHI. |
| 3742 | if (Safe) { |
| 3743 | BasicBlock::iterator I = &LI; |
| 3744 | for (--I; !isa<PHINode>(I); --I) |
| 3745 | if (isa<StoreInst>(I) || isa<CallInst>(I)) { |
| 3746 | Safe = false; |
| 3747 | break; |
| 3748 | } |
| 3749 | } |
| 3750 | |
| 3751 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e && Safe; ++i) |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 3752 | if (!isSafeToLoadUnconditionally(PN->getIncomingValue(i), |
Chris Lattner | 79f0c8e | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 3753 | PN->getIncomingBlock(i)->getTerminator())) |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 3754 | Safe = false; |
Chris Lattner | 79f0c8e | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 3755 | |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 3756 | if (Safe) { |
| 3757 | // Create the PHI. |
| 3758 | PHINode *NewPN = new PHINode(LI.getType(), PN->getName()); |
| 3759 | InsertNewInstBefore(NewPN, *PN); |
| 3760 | std::map<BasicBlock*,Value*> LoadMap; // Don't insert duplicate loads |
| 3761 | |
| 3762 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 3763 | BasicBlock *BB = PN->getIncomingBlock(i); |
| 3764 | Value *&TheLoad = LoadMap[BB]; |
| 3765 | if (TheLoad == 0) { |
| 3766 | Value *InVal = PN->getIncomingValue(i); |
| 3767 | TheLoad = InsertNewInstBefore(new LoadInst(InVal, |
| 3768 | InVal->getName()+".val"), |
| 3769 | *BB->getTerminator()); |
| 3770 | } |
| 3771 | NewPN->addIncoming(TheLoad, BB); |
| 3772 | } |
| 3773 | return ReplaceInstUsesWith(LI, NewPN); |
| 3774 | } |
| 3775 | } |
| 3776 | } |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 3777 | return 0; |
| 3778 | } |
| 3779 | |
| 3780 | |
Chris Lattner | c4d10eb | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 3781 | Instruction *InstCombiner::visitBranchInst(BranchInst &BI) { |
| 3782 | // 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] | 3783 | Value *X; |
| 3784 | BasicBlock *TrueDest; |
| 3785 | BasicBlock *FalseDest; |
| 3786 | if (match(&BI, m_Br(m_Not(m_Value(X)), TrueDest, FalseDest)) && |
| 3787 | !isa<Constant>(X)) { |
| 3788 | // Swap Destinations and condition... |
| 3789 | BI.setCondition(X); |
| 3790 | BI.setSuccessor(0, FalseDest); |
| 3791 | BI.setSuccessor(1, TrueDest); |
| 3792 | return &BI; |
| 3793 | } |
| 3794 | |
| 3795 | // Cannonicalize setne -> seteq |
| 3796 | Instruction::BinaryOps Op; Value *Y; |
| 3797 | if (match(&BI, m_Br(m_SetCond(Op, m_Value(X), m_Value(Y)), |
| 3798 | TrueDest, FalseDest))) |
| 3799 | if ((Op == Instruction::SetNE || Op == Instruction::SetLE || |
| 3800 | Op == Instruction::SetGE) && BI.getCondition()->hasOneUse()) { |
| 3801 | SetCondInst *I = cast<SetCondInst>(BI.getCondition()); |
| 3802 | std::string Name = I->getName(); I->setName(""); |
| 3803 | Instruction::BinaryOps NewOpcode = SetCondInst::getInverseCondition(Op); |
| 3804 | Value *NewSCC = BinaryOperator::create(NewOpcode, X, Y, Name, I); |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 3805 | // Swap Destinations and condition... |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3806 | BI.setCondition(NewSCC); |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 3807 | BI.setSuccessor(0, FalseDest); |
| 3808 | BI.setSuccessor(1, TrueDest); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3809 | removeFromWorkList(I); |
| 3810 | I->getParent()->getInstList().erase(I); |
| 3811 | WorkList.push_back(cast<Instruction>(NewSCC)); |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 3812 | return &BI; |
| 3813 | } |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3814 | |
Chris Lattner | c4d10eb | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 3815 | return 0; |
| 3816 | } |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 3817 | |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 3818 | Instruction *InstCombiner::visitSwitchInst(SwitchInst &SI) { |
| 3819 | Value *Cond = SI.getCondition(); |
| 3820 | if (Instruction *I = dyn_cast<Instruction>(Cond)) { |
| 3821 | if (I->getOpcode() == Instruction::Add) |
| 3822 | if (ConstantInt *AddRHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 3823 | // change 'switch (X+4) case 1:' into 'switch (X) case -3' |
| 3824 | for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2) |
| 3825 | SI.setOperand(i, ConstantExpr::getSub(cast<Constant>(SI.getOperand(i)), |
| 3826 | AddRHS)); |
| 3827 | SI.setOperand(0, I->getOperand(0)); |
| 3828 | WorkList.push_back(I); |
| 3829 | return &SI; |
| 3830 | } |
| 3831 | } |
| 3832 | return 0; |
| 3833 | } |
| 3834 | |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 3835 | |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 3836 | void InstCombiner::removeFromWorkList(Instruction *I) { |
| 3837 | WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), I), |
| 3838 | WorkList.end()); |
| 3839 | } |
| 3840 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3841 | bool InstCombiner::runOnFunction(Function &F) { |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 3842 | bool Changed = false; |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 3843 | TD = &getAnalysis<TargetData>(); |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 3844 | |
Chris Lattner | 216d4d8 | 2004-05-01 23:19:52 +0000 | [diff] [blame] | 3845 | for (inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i) |
| 3846 | WorkList.push_back(&*i); |
Chris Lattner | 6ffe551 | 2004-04-27 15:13:33 +0000 | [diff] [blame] | 3847 | |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 3848 | |
| 3849 | while (!WorkList.empty()) { |
| 3850 | Instruction *I = WorkList.back(); // Get an instruction from the worklist |
| 3851 | WorkList.pop_back(); |
| 3852 | |
Misha Brukman | a3bbcb5 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 3853 | // Check to see if we can DCE or ConstantPropagate the instruction... |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 3854 | // Check to see if we can DIE the instruction... |
| 3855 | if (isInstructionTriviallyDead(I)) { |
| 3856 | // Add operands to the worklist... |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 3857 | if (I->getNumOperands() < 4) |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 3858 | AddUsesToWorkList(*I); |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 3859 | ++NumDeadInst; |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 3860 | |
| 3861 | I->getParent()->getInstList().erase(I); |
| 3862 | removeFromWorkList(I); |
| 3863 | continue; |
| 3864 | } |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 3865 | |
Misha Brukman | a3bbcb5 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 3866 | // Instruction isn't dead, see if we can constant propagate it... |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 3867 | if (Constant *C = ConstantFoldInstruction(I)) { |
| 3868 | // Add operands to the worklist... |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 3869 | AddUsesToWorkList(*I); |
Chris Lattner | c736d56 | 2002-12-05 22:41:53 +0000 | [diff] [blame] | 3870 | ReplaceInstUsesWith(*I, C); |
| 3871 | |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 3872 | ++NumConstProp; |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 3873 | I->getParent()->getInstList().erase(I); |
Chris Lattner | 6061000 | 2003-10-07 15:17:02 +0000 | [diff] [blame] | 3874 | removeFromWorkList(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 3875 | continue; |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 3876 | } |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 3877 | |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 3878 | // Now that we have an instruction, try combining it to simplify it... |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 3879 | if (Instruction *Result = visit(*I)) { |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 3880 | ++NumCombined; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 3881 | // Should we replace the old instruction with a new one? |
Chris Lattner | b3bc8fa | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 3882 | if (Result != I) { |
Chris Lattner | 0cea42a | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 3883 | DEBUG(std::cerr << "IC: Old = " << *I |
| 3884 | << " New = " << *Result); |
| 3885 | |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 3886 | // Everything uses the new instruction now. |
| 3887 | I->replaceAllUsesWith(Result); |
| 3888 | |
| 3889 | // Push the new instruction and any users onto the worklist. |
| 3890 | WorkList.push_back(Result); |
| 3891 | AddUsersToWorkList(*Result); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 3892 | |
| 3893 | // Move the name to the new instruction first... |
| 3894 | std::string OldName = I->getName(); I->setName(""); |
Chris Lattner | d558dc3 | 2003-10-07 22:58:41 +0000 | [diff] [blame] | 3895 | Result->setName(OldName); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 3896 | |
| 3897 | // Insert the new instruction into the basic block... |
| 3898 | BasicBlock *InstParent = I->getParent(); |
| 3899 | InstParent->getInstList().insert(I, Result); |
| 3900 | |
Chris Lattner | 00d5131 | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 3901 | // Make sure that we reprocess all operands now that we reduced their |
| 3902 | // use counts. |
Chris Lattner | 216d4d8 | 2004-05-01 23:19:52 +0000 | [diff] [blame] | 3903 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 3904 | if (Instruction *OpI = dyn_cast<Instruction>(I->getOperand(i))) |
| 3905 | WorkList.push_back(OpI); |
| 3906 | |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 3907 | // Instructions can end up on the worklist more than once. Make sure |
| 3908 | // we do not process an instruction that has been deleted. |
| 3909 | removeFromWorkList(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 3910 | |
| 3911 | // Erase the old instruction. |
| 3912 | InstParent->getInstList().erase(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3913 | } else { |
Chris Lattner | 0cea42a | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 3914 | DEBUG(std::cerr << "IC: MOD = " << *I); |
| 3915 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 3916 | // If the instruction was modified, it's possible that it is now dead. |
| 3917 | // if so, remove it. |
Chris Lattner | 00d5131 | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 3918 | if (isInstructionTriviallyDead(I)) { |
| 3919 | // Make sure we process all operands now that we are reducing their |
| 3920 | // use counts. |
| 3921 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 3922 | if (Instruction *OpI = dyn_cast<Instruction>(I->getOperand(i))) |
| 3923 | WorkList.push_back(OpI); |
| 3924 | |
| 3925 | // Instructions may end up in the worklist more than once. Erase all |
| 3926 | // occurrances of this instruction. |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 3927 | removeFromWorkList(I); |
Chris Lattner | 00d5131 | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 3928 | I->getParent()->getInstList().erase(I); |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 3929 | } else { |
| 3930 | WorkList.push_back(Result); |
| 3931 | AddUsersToWorkList(*Result); |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 3932 | } |
Chris Lattner | b3bc8fa | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 3933 | } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 3934 | Changed = true; |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 3935 | } |
| 3936 | } |
| 3937 | |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 3938 | return Changed; |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 3939 | } |
| 3940 | |
Brian Gaeke | 96d4bf7 | 2004-07-27 17:43:21 +0000 | [diff] [blame] | 3941 | FunctionPass *llvm::createInstructionCombiningPass() { |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 3942 | return new InstCombiner(); |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 3943 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 3944 | |