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